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 {
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 mangle(m, mnt->mnt_devname ? mnt->mnt_devname : "none");
255 seq_path(m, mnt, mnt->mnt_root, " \t\n\\");
257 mangle(m, mnt->mnt_sb->s_type->name);
259 for (p = fs_info; (p->s_flag | p->mnt_flag) ; p++) {
260 if ((s_flags & p->s_flag) || (mnt_flags & p->mnt_flag)) {
262 seq_puts(m, p->set_str);
265 seq_puts(m, p->unset_str);
268 if (mnt->mnt_sb->s_op->show_options)
269 err = mnt->mnt_sb->s_op->show_options(m, mnt);
270 seq_puts(m, " 0 0\n");
274 struct seq_operations mounts_op = {
282 * may_umount_tree - check if a mount tree is busy
283 * @mnt: root of mount tree
285 * This is called to check if a tree of mounts has any
286 * open files, pwds, chroots or sub mounts that are
289 int may_umount_tree(struct vfsmount *mnt)
291 struct list_head *next;
292 struct vfsmount *this_parent = mnt;
296 spin_lock(&vfsmount_lock);
297 actual_refs = atomic_read(&mnt->mnt_count);
300 next = this_parent->mnt_mounts.next;
302 while (next != &this_parent->mnt_mounts) {
303 struct vfsmount *p = list_entry(next, struct vfsmount, mnt_child);
307 actual_refs += atomic_read(&p->mnt_count);
310 if (!list_empty(&p->mnt_mounts)) {
316 if (this_parent != mnt) {
317 next = this_parent->mnt_child.next;
318 this_parent = this_parent->mnt_parent;
321 spin_unlock(&vfsmount_lock);
323 if (actual_refs > minimum_refs)
329 EXPORT_SYMBOL(may_umount_tree);
332 * may_umount - check if a mount point is busy
333 * @mnt: root of mount
335 * This is called to check if a mount point has any
336 * open files, pwds, chroots or sub mounts. If the
337 * mount has sub mounts this will return busy
338 * regardless of whether the sub mounts are busy.
340 * Doesn't take quota and stuff into account. IOW, in some cases it will
341 * give false negatives. The main reason why it's here is that we need
342 * a non-destructive way to look for easily umountable filesystems.
344 int may_umount(struct vfsmount *mnt)
346 if (atomic_read(&mnt->mnt_count) > 2)
351 EXPORT_SYMBOL(may_umount);
353 static inline void __umount_tree(struct vfsmount *mnt, struct list_head *kill)
355 while (!list_empty(kill)) {
356 mnt = list_entry(kill->next, struct vfsmount, mnt_list);
357 list_del_init(&mnt->mnt_list);
358 list_del_init(&mnt->mnt_fslink);
359 if (mnt->mnt_parent == mnt) {
360 spin_unlock(&vfsmount_lock);
362 struct nameidata old_nd;
363 detach_mnt(mnt, &old_nd);
364 spin_unlock(&vfsmount_lock);
365 path_release(&old_nd);
368 spin_lock(&vfsmount_lock);
372 void umount_tree(struct vfsmount *mnt)
377 for (p = mnt; p; p = next_mnt(p, mnt)) {
378 list_del(&p->mnt_list);
379 list_add(&p->mnt_list, &kill);
381 __umount_tree(mnt, &kill);
384 void umount_unused(struct vfsmount *mnt, struct fs_struct *fs)
389 for (p = mnt; p; p = next_mnt(p, mnt)) {
390 if (p == fs->rootmnt || p == fs->pwdmnt)
392 list_del(&p->mnt_list);
393 list_add(&p->mnt_list, &kill);
395 __umount_tree(mnt, &kill);
398 static int do_umount(struct vfsmount *mnt, int flags)
400 struct super_block * sb = mnt->mnt_sb;
403 retval = security_sb_umount(mnt, flags);
408 * Allow userspace to request a mountpoint be expired rather than
409 * unmounting unconditionally. Unmount only happens if:
410 * (1) the mark is already set (the mark is cleared by mntput())
411 * (2) the usage count == 1 [parent vfsmount] + 1 [sys_umount]
413 if (flags & MNT_EXPIRE) {
414 if (mnt == current->fs->rootmnt ||
415 flags & (MNT_FORCE | MNT_DETACH))
418 if (atomic_read(&mnt->mnt_count) != 2)
421 if (!xchg(&mnt->mnt_expiry_mark, 1))
426 * If we may have to abort operations to get out of this
427 * mount, and they will themselves hold resources we must
428 * allow the fs to do things. In the Unix tradition of
429 * 'Gee thats tricky lets do it in userspace' the umount_begin
430 * might fail to complete on the first run through as other tasks
431 * must return, and the like. Thats for the mount program to worry
432 * about for the moment.
436 if( (flags&MNT_FORCE) && sb->s_op->umount_begin)
437 sb->s_op->umount_begin(sb);
441 * No sense to grab the lock for this test, but test itself looks
442 * somewhat bogus. Suggestions for better replacement?
443 * Ho-hum... In principle, we might treat that as umount + switch
444 * to rootfs. GC would eventually take care of the old vfsmount.
445 * Actually it makes sense, especially if rootfs would contain a
446 * /reboot - static binary that would close all descriptors and
447 * call reboot(9). Then init(8) could umount root and exec /reboot.
449 if (mnt == current->fs->rootmnt && !(flags & MNT_DETACH)) {
451 * Special case for "unmounting" root ...
452 * we just try to remount it readonly.
454 down_write(&sb->s_umount);
455 if (!(sb->s_flags & MS_RDONLY)) {
457 retval = do_remount_sb(sb, MS_RDONLY, NULL, 0);
460 up_write(&sb->s_umount);
464 down_write(¤t->namespace->sem);
465 spin_lock(&vfsmount_lock);
467 if (atomic_read(&sb->s_active) == 1) {
468 /* last instance - try to be smart */
469 spin_unlock(&vfsmount_lock);
474 security_sb_umount_close(mnt);
475 spin_lock(&vfsmount_lock);
478 if (atomic_read(&mnt->mnt_count) == 2 || flags & MNT_DETACH) {
479 if (!list_empty(&mnt->mnt_list))
483 spin_unlock(&vfsmount_lock);
485 security_sb_umount_busy(mnt);
486 up_write(¤t->namespace->sem);
491 * Now umount can handle mount points as well as block devices.
492 * This is important for filesystems which use unnamed block devices.
494 * We now support a flag for forced unmount like the other 'big iron'
495 * unixes. Our API is identical to OSF/1 to avoid making a mess of AMD
498 asmlinkage long sys_umount(char __user * name, int flags)
503 retval = __user_walk(name, LOOKUP_FOLLOW, &nd);
507 if (nd.dentry != nd.mnt->mnt_root)
509 if (!check_mnt(nd.mnt))
513 if (!capable(CAP_SYS_ADMIN) && !vx_ccaps(VXC_SECURE_MOUNT))
516 retval = do_umount(nd.mnt, flags);
518 path_release_on_umount(&nd);
523 #ifdef __ARCH_WANT_SYS_OLDUMOUNT
526 * The 2.0 compatible umount. No flags.
529 asmlinkage long sys_oldumount(char __user * name)
531 return sys_umount(name,0);
536 static int mount_is_safe(struct nameidata *nd)
538 if (capable(CAP_SYS_ADMIN))
540 if (vx_ccaps(VXC_SECURE_MOUNT))
544 if (S_ISLNK(nd->dentry->d_inode->i_mode))
546 if (nd->dentry->d_inode->i_mode & S_ISVTX) {
547 if (current->uid != nd->dentry->d_inode->i_uid)
550 if (permission(nd->dentry->d_inode, MAY_WRITE, nd))
557 lives_below_in_same_fs(struct dentry *d, struct dentry *dentry)
562 if (d == NULL || d == d->d_parent)
568 static struct vfsmount *copy_tree(struct vfsmount *mnt, struct dentry *dentry)
570 struct vfsmount *res, *p, *q, *r, *s;
574 res = q = clone_mnt(mnt, dentry);
577 q->mnt_mountpoint = mnt->mnt_mountpoint;
580 for (h = mnt->mnt_mounts.next; h != &mnt->mnt_mounts; h = h->next) {
581 r = list_entry(h, struct vfsmount, mnt_child);
582 if (!lives_below_in_same_fs(r->mnt_mountpoint, dentry))
585 for (s = r; s; s = next_mnt(s, r)) {
586 while (p != s->mnt_parent) {
592 nd.dentry = p->mnt_mountpoint;
593 q = clone_mnt(p, p->mnt_root);
596 spin_lock(&vfsmount_lock);
597 list_add_tail(&q->mnt_list, &res->mnt_list);
599 spin_unlock(&vfsmount_lock);
605 spin_lock(&vfsmount_lock);
607 spin_unlock(&vfsmount_lock);
612 static int graft_tree(struct vfsmount *mnt, struct nameidata *nd)
615 if (mnt->mnt_sb->s_flags & MS_NOUSER)
618 if (S_ISDIR(nd->dentry->d_inode->i_mode) !=
619 S_ISDIR(mnt->mnt_root->d_inode->i_mode))
623 down(&nd->dentry->d_inode->i_sem);
624 if (IS_DEADDIR(nd->dentry->d_inode))
627 err = security_sb_check_sb(mnt, nd);
632 spin_lock(&vfsmount_lock);
633 if (IS_ROOT(nd->dentry) || !d_unhashed(nd->dentry)) {
634 struct list_head head;
637 list_add_tail(&head, &mnt->mnt_list);
638 list_splice(&head, current->namespace->list.prev);
642 spin_unlock(&vfsmount_lock);
644 up(&nd->dentry->d_inode->i_sem);
646 security_sb_post_addmount(mnt, nd);
653 static int do_loopback(struct nameidata *nd, char *old_name, unsigned long flags, int mnt_flags)
655 struct nameidata old_nd;
656 struct vfsmount *mnt = NULL;
657 int recurse = flags & MS_REC;
658 int err = mount_is_safe(nd);
662 if (!old_name || !*old_name)
664 err = path_lookup(old_name, LOOKUP_FOLLOW, &old_nd);
668 down_write(¤t->namespace->sem);
670 if (check_mnt(nd->mnt) && (!recurse || check_mnt(old_nd.mnt))) {
673 mnt = copy_tree(old_nd.mnt, old_nd.dentry);
675 mnt = clone_mnt(old_nd.mnt, old_nd.dentry);
679 /* stop bind mounts from expiring */
680 spin_lock(&vfsmount_lock);
681 list_del_init(&mnt->mnt_fslink);
682 spin_unlock(&vfsmount_lock);
684 err = graft_tree(mnt, nd);
686 spin_lock(&vfsmount_lock);
688 spin_unlock(&vfsmount_lock);
691 mnt->mnt_flags = mnt_flags;
694 up_write(¤t->namespace->sem);
695 path_release(&old_nd);
700 * change filesystem flags. dir should be a physical root of filesystem.
701 * If you've mounted a non-root directory somewhere and want to do remount
702 * on it - tough luck.
705 static int do_remount(struct nameidata *nd, int flags, int mnt_flags,
709 struct super_block * sb = nd->mnt->mnt_sb;
711 if (!capable(CAP_SYS_ADMIN) && !vx_ccaps(VXC_SECURE_REMOUNT))
714 if (!check_mnt(nd->mnt))
717 if (nd->dentry != nd->mnt->mnt_root)
720 if (vx_ccaps(VXC_SECURE_REMOUNT))
721 mnt_flags |= MNT_NODEV;
722 down_write(&sb->s_umount);
723 err = do_remount_sb(sb, flags, data, 0);
725 nd->mnt->mnt_flags=mnt_flags;
726 up_write(&sb->s_umount);
728 security_sb_post_remount(nd->mnt, flags, data);
732 static int do_move_mount(struct nameidata *nd, char *old_name)
734 struct nameidata old_nd, parent_nd;
737 if (!capable(CAP_SYS_ADMIN) && !vx_ccaps(VXC_SECURE_MOUNT))
739 if (!old_name || !*old_name)
741 err = path_lookup(old_name, LOOKUP_FOLLOW, &old_nd);
745 down_write(¤t->namespace->sem);
746 while(d_mountpoint(nd->dentry) && follow_down(&nd->mnt, &nd->dentry))
749 if (!check_mnt(nd->mnt) || !check_mnt(old_nd.mnt))
753 down(&nd->dentry->d_inode->i_sem);
754 if (IS_DEADDIR(nd->dentry->d_inode))
757 spin_lock(&vfsmount_lock);
758 if (!IS_ROOT(nd->dentry) && d_unhashed(nd->dentry))
762 if (old_nd.dentry != old_nd.mnt->mnt_root)
765 if (old_nd.mnt == old_nd.mnt->mnt_parent)
768 if (S_ISDIR(nd->dentry->d_inode->i_mode) !=
769 S_ISDIR(old_nd.dentry->d_inode->i_mode))
773 for (p = nd->mnt; p->mnt_parent!=p; p = p->mnt_parent)
778 detach_mnt(old_nd.mnt, &parent_nd);
779 attach_mnt(old_nd.mnt, nd);
781 /* if the mount is moved, it should no longer be expire
783 list_del_init(&old_nd.mnt->mnt_fslink);
785 spin_unlock(&vfsmount_lock);
787 up(&nd->dentry->d_inode->i_sem);
789 up_write(¤t->namespace->sem);
791 path_release(&parent_nd);
792 path_release(&old_nd);
797 * create a new mount for userspace and request it to be added into the
800 static int do_new_mount(struct nameidata *nd, char *type, int flags,
801 int mnt_flags, char *name, void *data)
803 struct vfsmount *mnt;
805 if (!type || !memchr(type, 0, PAGE_SIZE))
808 /* we need capabilities... */
809 if (!capable(CAP_SYS_ADMIN) && !vx_ccaps(VXC_SECURE_MOUNT))
812 mnt = do_kern_mount(type, flags, name, data);
816 return do_add_mount(mnt, nd, mnt_flags, NULL);
820 * add a mount into a namespace's mount tree
821 * - provide the option of adding the new mount to an expiration list
823 int do_add_mount(struct vfsmount *newmnt, struct nameidata *nd,
824 int mnt_flags, struct list_head *fslist)
828 down_write(¤t->namespace->sem);
829 /* Something was mounted here while we slept */
830 while(d_mountpoint(nd->dentry) && follow_down(&nd->mnt, &nd->dentry))
833 if (!check_mnt(nd->mnt))
836 /* Refuse the same filesystem on the same mount point */
838 if (nd->mnt->mnt_sb == newmnt->mnt_sb &&
839 nd->mnt->mnt_root == nd->dentry)
843 if (S_ISLNK(newmnt->mnt_root->d_inode->i_mode))
846 newmnt->mnt_flags = mnt_flags;
847 err = graft_tree(newmnt, nd);
849 if (err == 0 && fslist) {
850 /* add to the specified expiration list */
851 spin_lock(&vfsmount_lock);
852 list_add_tail(&newmnt->mnt_fslink, fslist);
853 spin_unlock(&vfsmount_lock);
857 up_write(¤t->namespace->sem);
862 EXPORT_SYMBOL_GPL(do_add_mount);
865 * process a list of expirable mountpoints with the intent of discarding any
866 * mountpoints that aren't in use and haven't been touched since last we came
869 void mark_mounts_for_expiry(struct list_head *mounts)
871 struct namespace *namespace;
872 struct vfsmount *mnt, *next;
873 LIST_HEAD(graveyard);
875 if (list_empty(mounts))
878 spin_lock(&vfsmount_lock);
880 /* extract from the expiration list every vfsmount that matches the
881 * following criteria:
882 * - only referenced by its parent vfsmount
883 * - still marked for expiry (marked on the last call here; marks are
884 * cleared by mntput())
886 list_for_each_entry_safe(mnt, next, mounts, mnt_fslink) {
887 if (!xchg(&mnt->mnt_expiry_mark, 1) ||
888 atomic_read(&mnt->mnt_count) != 1)
892 list_move(&mnt->mnt_fslink, &graveyard);
896 * go through the vfsmounts we've just consigned to the graveyard to
897 * - check that they're still dead
898 * - delete the vfsmount from the appropriate namespace under lock
899 * - dispose of the corpse
901 while (!list_empty(&graveyard)) {
902 mnt = list_entry(graveyard.next, struct vfsmount, mnt_fslink);
903 list_del_init(&mnt->mnt_fslink);
905 /* don't do anything if the namespace is dead - all the
906 * vfsmounts from it are going away anyway */
907 namespace = mnt->mnt_namespace;
908 if (!namespace || atomic_read(&namespace->count) <= 0)
910 get_namespace(namespace);
912 spin_unlock(&vfsmount_lock);
913 down_write(&namespace->sem);
914 spin_lock(&vfsmount_lock);
916 /* check that it is still dead: the count should now be 2 - as
917 * contributed by the vfsmount parent and the mntget above */
918 if (atomic_read(&mnt->mnt_count) == 2) {
919 struct vfsmount *xdmnt;
920 struct dentry *xdentry;
922 /* delete from the namespace */
923 list_del_init(&mnt->mnt_list);
924 list_del_init(&mnt->mnt_child);
925 list_del_init(&mnt->mnt_hash);
926 mnt->mnt_mountpoint->d_mounted--;
928 xdentry = mnt->mnt_mountpoint;
929 mnt->mnt_mountpoint = mnt->mnt_root;
930 xdmnt = mnt->mnt_parent;
931 mnt->mnt_parent = mnt;
933 spin_unlock(&vfsmount_lock);
938 /* now lay it to rest if this was the last ref on the
940 if (atomic_read(&mnt->mnt_sb->s_active) == 1) {
941 /* last instance - try to be smart */
943 DQUOT_OFF(mnt->mnt_sb);
944 acct_auto_close(mnt->mnt_sb);
950 /* someone brought it back to life whilst we didn't
951 * have any locks held so return it to the expiration
953 list_add_tail(&mnt->mnt_fslink, mounts);
954 spin_unlock(&vfsmount_lock);
957 up_write(&namespace->sem);
960 put_namespace(namespace);
962 spin_lock(&vfsmount_lock);
965 spin_unlock(&vfsmount_lock);
968 EXPORT_SYMBOL_GPL(mark_mounts_for_expiry);
971 * Some copy_from_user() implementations do not return the exact number of
972 * bytes remaining to copy on a fault. But copy_mount_options() requires that.
973 * Note that this function differs from copy_from_user() in that it will oops
974 * on bad values of `to', rather than returning a short copy.
977 exact_copy_from_user(void *to, const void __user *from, unsigned long n)
980 const char __user *f = from;
983 if (!access_ok(VERIFY_READ, from, n))
987 if (__get_user(c, f)) {
998 int copy_mount_options(const void __user *data, unsigned long *where)
1008 if (!(page = __get_free_page(GFP_KERNEL)))
1011 /* We only care that *some* data at the address the user
1012 * gave us is valid. Just in case, we'll zero
1013 * the remainder of the page.
1015 /* copy_from_user cannot cross TASK_SIZE ! */
1016 size = TASK_SIZE - (unsigned long)data;
1017 if (size > PAGE_SIZE)
1020 i = size - exact_copy_from_user((void *)page, data, size);
1026 memset((char *)page + i, 0, PAGE_SIZE - i);
1032 * Flags is a 32-bit value that allows up to 31 non-fs dependent flags to
1033 * be given to the mount() call (ie: read-only, no-dev, no-suid etc).
1035 * data is a (void *) that can point to any structure up to
1036 * PAGE_SIZE-1 bytes, which can contain arbitrary fs-dependent
1037 * information (or be NULL).
1039 * Pre-0.97 versions of mount() didn't have a flags word.
1040 * When the flags word was introduced its top half was required
1041 * to have the magic value 0xC0ED, and this remained so until 2.4.0-test9.
1042 * Therefore, if this magic number is present, it carries no information
1043 * and must be discarded.
1045 long do_mount(char * dev_name, char * dir_name, char *type_page,
1046 unsigned long flags, void *data_page)
1048 struct nameidata nd;
1053 if ((flags & MS_MGC_MSK) == MS_MGC_VAL)
1054 flags &= ~MS_MGC_MSK;
1056 /* Basic sanity checks */
1058 if (!dir_name || !*dir_name || !memchr(dir_name, 0, PAGE_SIZE))
1060 if (dev_name && !memchr(dev_name, 0, PAGE_SIZE))
1064 ((char *)data_page)[PAGE_SIZE - 1] = 0;
1066 /* Separate the per-mountpoint flags */
1067 if (flags & MS_RDONLY)
1068 mnt_flags |= MNT_RDONLY;
1069 if (flags & MS_NOSUID)
1070 mnt_flags |= MNT_NOSUID;
1071 if (flags & MS_NODEV)
1072 mnt_flags |= MNT_NODEV;
1073 if (flags & MS_NOEXEC)
1074 mnt_flags |= MNT_NOEXEC;
1075 if (flags & MS_NOATIME)
1076 mnt_flags |= MNT_NOATIME;
1077 if (flags & MS_NODIRATIME)
1078 mnt_flags |= MNT_NODIRATIME;
1079 flags &= ~(MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_ACTIVE);
1081 if (vx_ccaps(VXC_SECURE_MOUNT))
1082 mnt_flags |= MNT_NODEV;
1084 /* ... and get the mountpoint */
1085 retval = path_lookup(dir_name, LOOKUP_FOLLOW, &nd);
1089 retval = security_sb_mount(dev_name, &nd, type_page, flags, data_page);
1093 if (flags & MS_REMOUNT)
1094 retval = do_remount(&nd, flags & ~MS_REMOUNT, mnt_flags,
1096 else if (flags & MS_BIND)
1097 retval = do_loopback(&nd, dev_name, flags, mnt_flags);
1098 else if (flags & MS_MOVE)
1099 retval = do_move_mount(&nd, dev_name);
1101 retval = do_new_mount(&nd, type_page, flags, mnt_flags,
1102 dev_name, data_page);
1108 int copy_namespace(int flags, struct task_struct *tsk)
1110 struct namespace *namespace = tsk->namespace;
1111 struct namespace *new_ns;
1112 struct vfsmount *rootmnt = NULL, *pwdmnt = NULL, *altrootmnt = NULL;
1113 struct fs_struct *fs = tsk->fs;
1114 struct vfsmount *p, *q;
1119 get_namespace(namespace);
1121 if (!(flags & CLONE_NEWNS))
1124 if (!capable(CAP_SYS_ADMIN) && !vx_ccaps(VXC_SECURE_MOUNT)) {
1125 put_namespace(namespace);
1129 new_ns = kmalloc(sizeof(struct namespace), GFP_KERNEL);
1133 atomic_set(&new_ns->count, 1);
1134 init_rwsem(&new_ns->sem);
1135 INIT_LIST_HEAD(&new_ns->list);
1137 down_write(&tsk->namespace->sem);
1138 /* First pass: copy the tree topology */
1139 new_ns->root = copy_tree(namespace->root, namespace->root->mnt_root);
1140 if (!new_ns->root) {
1141 up_write(&tsk->namespace->sem);
1145 spin_lock(&vfsmount_lock);
1146 list_add_tail(&new_ns->list, &new_ns->root->mnt_list);
1147 spin_unlock(&vfsmount_lock);
1150 * Second pass: switch the tsk->fs->* elements and mark new vfsmounts
1151 * as belonging to new namespace. We have already acquired a private
1152 * fs_struct, so tsk->fs->lock is not needed.
1154 p = namespace->root;
1157 q->mnt_namespace = new_ns;
1159 if (p == fs->rootmnt) {
1161 fs->rootmnt = mntget(q);
1163 if (p == fs->pwdmnt) {
1165 fs->pwdmnt = mntget(q);
1167 if (p == fs->altrootmnt) {
1169 fs->altrootmnt = mntget(q);
1172 p = next_mnt(p, namespace->root);
1173 q = next_mnt(q, new_ns->root);
1175 up_write(&tsk->namespace->sem);
1177 tsk->namespace = new_ns;
1186 put_namespace(namespace);
1190 put_namespace(namespace);
1194 asmlinkage long sys_mount(char __user * dev_name, char __user * dir_name,
1195 char __user * type, unsigned long flags,
1199 unsigned long data_page;
1200 unsigned long type_page;
1201 unsigned long dev_page;
1204 retval = copy_mount_options (type, &type_page);
1208 dir_page = getname(dir_name);
1209 retval = PTR_ERR(dir_page);
1210 if (IS_ERR(dir_page))
1213 retval = copy_mount_options (dev_name, &dev_page);
1217 retval = copy_mount_options (data, &data_page);
1222 retval = do_mount((char*)dev_page, dir_page, (char*)type_page,
1223 flags, (void*)data_page);
1225 free_page(data_page);
1228 free_page(dev_page);
1232 free_page(type_page);
1237 * Replace the fs->{rootmnt,root} with {mnt,dentry}. Put the old values.
1238 * It can block. Requires the big lock held.
1240 void set_fs_root(struct fs_struct *fs, struct vfsmount *mnt,
1241 struct dentry *dentry)
1243 struct dentry *old_root;
1244 struct vfsmount *old_rootmnt;
1245 write_lock(&fs->lock);
1246 old_root = fs->root;
1247 old_rootmnt = fs->rootmnt;
1248 fs->rootmnt = mntget(mnt);
1249 fs->root = dget(dentry);
1250 write_unlock(&fs->lock);
1253 mntput(old_rootmnt);
1257 EXPORT_SYMBOL_GPL(set_fs_root);
1259 * Replace the fs->{pwdmnt,pwd} with {mnt,dentry}. Put the old values.
1260 * It can block. Requires the big lock held.
1262 void set_fs_pwd(struct fs_struct *fs, struct vfsmount *mnt,
1263 struct dentry *dentry)
1265 struct dentry *old_pwd;
1266 struct vfsmount *old_pwdmnt;
1268 write_lock(&fs->lock);
1270 old_pwdmnt = fs->pwdmnt;
1271 fs->pwdmnt = mntget(mnt);
1272 fs->pwd = dget(dentry);
1273 write_unlock(&fs->lock);
1281 static void chroot_fs_refs(struct nameidata *old_nd, struct nameidata *new_nd)
1283 struct task_struct *g, *p;
1284 struct fs_struct *fs;
1286 read_lock(&tasklist_lock);
1287 do_each_thread(g, p) {
1291 atomic_inc(&fs->count);
1293 if (fs->root==old_nd->dentry&&fs->rootmnt==old_nd->mnt)
1294 set_fs_root(fs, new_nd->mnt, new_nd->dentry);
1295 if (fs->pwd==old_nd->dentry&&fs->pwdmnt==old_nd->mnt)
1296 set_fs_pwd(fs, new_nd->mnt, new_nd->dentry);
1300 } while_each_thread(g, p);
1301 read_unlock(&tasklist_lock);
1305 * Moves the current root to put_root, and sets root/cwd of all processes
1306 * which had them on the old root to new_root.
1309 * - we don't move root/cwd if they are not at the root (reason: if something
1310 * cared enough to change them, it's probably wrong to force them elsewhere)
1311 * - it's okay to pick a root that isn't the root of a file system, e.g.
1312 * /nfs/my_root where /nfs is the mount point. It must be a mountpoint,
1313 * though, so you may need to say mount --bind /nfs/my_root /nfs/my_root
1317 asmlinkage long sys_pivot_root(const char __user *new_root, const char __user *put_old)
1319 struct vfsmount *tmp;
1320 struct nameidata new_nd, old_nd, parent_nd, root_parent, user_nd;
1323 if (!capable(CAP_SYS_ADMIN))
1328 error = __user_walk(new_root, LOOKUP_FOLLOW|LOOKUP_DIRECTORY, &new_nd);
1332 if (!check_mnt(new_nd.mnt))
1335 error = __user_walk(put_old, LOOKUP_FOLLOW|LOOKUP_DIRECTORY, &old_nd);
1339 error = security_sb_pivotroot(&old_nd, &new_nd);
1341 path_release(&old_nd);
1345 read_lock(¤t->fs->lock);
1346 user_nd.mnt = mntget(current->fs->rootmnt);
1347 user_nd.dentry = dget(current->fs->root);
1348 read_unlock(¤t->fs->lock);
1349 down_write(¤t->namespace->sem);
1350 down(&old_nd.dentry->d_inode->i_sem);
1352 if (!check_mnt(user_nd.mnt))
1355 if (IS_DEADDIR(new_nd.dentry->d_inode))
1357 if (d_unhashed(new_nd.dentry) && !IS_ROOT(new_nd.dentry))
1359 if (d_unhashed(old_nd.dentry) && !IS_ROOT(old_nd.dentry))
1362 if (new_nd.mnt == user_nd.mnt || old_nd.mnt == user_nd.mnt)
1363 goto out2; /* loop */
1365 if (user_nd.mnt->mnt_root != user_nd.dentry)
1367 if (new_nd.mnt->mnt_root != new_nd.dentry)
1368 goto out2; /* not a mountpoint */
1369 tmp = old_nd.mnt; /* make sure we can reach put_old from new_root */
1370 spin_lock(&vfsmount_lock);
1371 if (tmp != new_nd.mnt) {
1373 if (tmp->mnt_parent == tmp)
1375 if (tmp->mnt_parent == new_nd.mnt)
1377 tmp = tmp->mnt_parent;
1379 if (!is_subdir(tmp->mnt_mountpoint, new_nd.dentry))
1381 } else if (!is_subdir(old_nd.dentry, new_nd.dentry))
1383 detach_mnt(new_nd.mnt, &parent_nd);
1384 detach_mnt(user_nd.mnt, &root_parent);
1385 attach_mnt(user_nd.mnt, &old_nd);
1386 attach_mnt(new_nd.mnt, &root_parent);
1387 spin_unlock(&vfsmount_lock);
1388 chroot_fs_refs(&user_nd, &new_nd);
1389 security_sb_post_pivotroot(&user_nd, &new_nd);
1391 path_release(&root_parent);
1392 path_release(&parent_nd);
1394 up(&old_nd.dentry->d_inode->i_sem);
1395 up_write(¤t->namespace->sem);
1396 path_release(&user_nd);
1397 path_release(&old_nd);
1399 path_release(&new_nd);
1404 spin_unlock(&vfsmount_lock);
1408 static void __init init_mount_tree(void)
1410 struct vfsmount *mnt;
1411 struct namespace *namespace;
1412 struct task_struct *g, *p;
1414 mnt = do_kern_mount("rootfs", 0, "rootfs", NULL);
1416 panic("Can't create rootfs");
1417 namespace = kmalloc(sizeof(*namespace), GFP_KERNEL);
1419 panic("Can't allocate initial namespace");
1420 atomic_set(&namespace->count, 1);
1421 INIT_LIST_HEAD(&namespace->list);
1422 init_rwsem(&namespace->sem);
1423 list_add(&mnt->mnt_list, &namespace->list);
1424 namespace->root = mnt;
1425 mnt->mnt_namespace = namespace;
1427 init_task.namespace = namespace;
1428 read_lock(&tasklist_lock);
1429 do_each_thread(g, p) {
1430 get_namespace(namespace);
1431 p->namespace = namespace;
1432 } while_each_thread(g, p);
1433 read_unlock(&tasklist_lock);
1435 set_fs_pwd(current->fs, namespace->root, namespace->root->mnt_root);
1436 set_fs_root(current->fs, namespace->root, namespace->root->mnt_root);
1439 void __init mnt_init(unsigned long mempages)
1441 struct list_head *d;
1442 unsigned long order;
1443 unsigned int nr_hash;
1446 mnt_cache = kmem_cache_create("mnt_cache", sizeof(struct vfsmount),
1447 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
1450 mount_hashtable = (struct list_head *)
1451 __get_free_pages(GFP_ATOMIC, order);
1453 if (!mount_hashtable)
1454 panic("Failed to allocate mount hash table\n");
1457 * Find the power-of-two list-heads that can fit into the allocation..
1458 * We don't guarantee that "sizeof(struct list_head)" is necessarily
1461 nr_hash = (1UL << order) * PAGE_SIZE / sizeof(struct list_head);
1465 } while ((nr_hash >> hash_bits) != 0);
1469 * Re-calculate the actual number of entries and the mask
1470 * from the number of bits we can fit.
1472 nr_hash = 1UL << hash_bits;
1473 hash_mask = nr_hash-1;
1475 printk("Mount-cache hash table entries: %d (order: %ld, %ld bytes)\n",
1476 nr_hash, order, (PAGE_SIZE << order));
1478 /* And initialize the newly allocated array */
1479 d = mount_hashtable;
1491 void __put_namespace(struct namespace *namespace)
1493 struct vfsmount *mnt;
1495 down_write(&namespace->sem);
1496 spin_lock(&vfsmount_lock);
1498 list_for_each_entry(mnt, &namespace->list, mnt_list) {
1499 mnt->mnt_namespace = NULL;
1502 umount_tree(namespace->root);
1503 spin_unlock(&vfsmount_lock);
1504 up_write(&namespace->sem);