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 __cacheline_aligned_in_smp DEFINE_SPINLOCK(vfsmount_lock);
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 mnt_is_reachable(struct vfsmount *mnt)
224 struct vfsmount *root_mnt;
225 struct dentry *root, *point;
230 if (mnt == mnt->mnt_namespace->root)
233 spin_lock(&dcache_lock);
234 root_mnt = current->fs->rootmnt;
235 root = current->fs->root;
238 while ((mnt != mnt->mnt_parent) && (mnt != root_mnt)) {
239 point = mnt->mnt_mountpoint;
240 mnt = mnt->mnt_parent;
243 ret = (mnt == root_mnt) && is_subdir(point, root);
245 spin_unlock(&dcache_lock);
250 static int show_vfsmnt(struct seq_file *m, void *v)
252 struct vfsmount *mnt = v;
254 static struct proc_fs_info {
258 { MS_SYNCHRONOUS, ",sync" },
259 { MS_DIRSYNC, ",dirsync" },
260 { MS_MANDLOCK, ",mand" },
261 { MS_NOATIME, ",noatime" },
262 { MS_NODIRATIME, ",nodiratime" },
263 { MS_TAGXID, ",tagxid" },
266 static struct proc_fs_info mnt_info[] = {
267 { MNT_NOSUID, ",nosuid" },
268 { MNT_NODEV, ",nodev" },
269 { MNT_NOEXEC, ",noexec" },
272 struct proc_fs_info *fs_infop;
274 if (vx_flags(VXF_HIDE_MOUNT, 0))
276 if (!mnt_is_reachable(mnt))
279 if (!vx_check(0, VX_ADMIN|VX_WATCH) &&
280 mnt == current->fs->rootmnt) {
281 seq_puts(m, "/dev/root / ");
283 mangle(m, mnt->mnt_devname ? mnt->mnt_devname : "none");
285 seq_path(m, mnt, mnt->mnt_root, " \t\n\\");
288 mangle(m, mnt->mnt_sb->s_type->name);
289 seq_puts(m, mnt->mnt_sb->s_flags & MS_RDONLY ? " ro" : " rw");
290 for (fs_infop = fs_info; fs_infop->flag; fs_infop++) {
291 if (mnt->mnt_sb->s_flags & fs_infop->flag)
292 seq_puts(m, fs_infop->str);
294 for (fs_infop = mnt_info; fs_infop->flag; fs_infop++) {
295 if (mnt->mnt_flags & fs_infop->flag)
296 seq_puts(m, fs_infop->str);
298 if (mnt->mnt_flags & MNT_XID)
299 seq_printf(m, ",xid=%d", mnt->mnt_xid);
300 if (mnt->mnt_sb->s_op->show_options)
301 err = mnt->mnt_sb->s_op->show_options(m, mnt);
302 seq_puts(m, " 0 0\n");
306 struct seq_operations mounts_op = {
314 * may_umount_tree - check if a mount tree is busy
315 * @mnt: root of mount tree
317 * This is called to check if a tree of mounts has any
318 * open files, pwds, chroots or sub mounts that are
321 int may_umount_tree(struct vfsmount *mnt)
323 struct list_head *next;
324 struct vfsmount *this_parent = mnt;
328 spin_lock(&vfsmount_lock);
329 actual_refs = atomic_read(&mnt->mnt_count);
332 next = this_parent->mnt_mounts.next;
334 while (next != &this_parent->mnt_mounts) {
335 struct vfsmount *p = list_entry(next, struct vfsmount, mnt_child);
339 actual_refs += atomic_read(&p->mnt_count);
342 if (!list_empty(&p->mnt_mounts)) {
348 if (this_parent != mnt) {
349 next = this_parent->mnt_child.next;
350 this_parent = this_parent->mnt_parent;
353 spin_unlock(&vfsmount_lock);
355 if (actual_refs > minimum_refs)
361 EXPORT_SYMBOL(may_umount_tree);
364 * may_umount - check if a mount point is busy
365 * @mnt: root of mount
367 * This is called to check if a mount point has any
368 * open files, pwds, chroots or sub mounts. If the
369 * mount has sub mounts this will return busy
370 * regardless of whether the sub mounts are busy.
372 * Doesn't take quota and stuff into account. IOW, in some cases it will
373 * give false negatives. The main reason why it's here is that we need
374 * a non-destructive way to look for easily umountable filesystems.
376 int may_umount(struct vfsmount *mnt)
378 if (atomic_read(&mnt->mnt_count) > 2)
383 EXPORT_SYMBOL(may_umount);
385 static inline void __umount_list(struct list_head *kill)
387 struct vfsmount *mnt;
389 while (!list_empty(kill)) {
390 mnt = list_entry(kill->next, struct vfsmount, mnt_list);
391 list_del_init(&mnt->mnt_list);
392 list_del_init(&mnt->mnt_fslink);
393 if (mnt->mnt_parent == mnt) {
394 spin_unlock(&vfsmount_lock);
396 struct nameidata old_nd;
397 detach_mnt(mnt, &old_nd);
398 spin_unlock(&vfsmount_lock);
399 path_release(&old_nd);
402 spin_lock(&vfsmount_lock);
406 void umount_tree(struct vfsmount *mnt)
411 for (p = mnt; p; p = next_mnt(p, mnt)) {
412 list_del(&p->mnt_list);
413 list_add(&p->mnt_list, &kill);
415 __umount_list(&kill);
418 void umount_unused(struct vfsmount *mnt, struct fs_struct *fs)
423 for (p = mnt; p; p = next_mnt(p, mnt)) {
424 if (p == fs->rootmnt || p == fs->pwdmnt)
426 list_del(&p->mnt_list);
427 list_add(&p->mnt_list, &kill);
429 __umount_list(&kill);
432 static int do_umount(struct vfsmount *mnt, int flags)
434 struct super_block * sb = mnt->mnt_sb;
437 retval = security_sb_umount(mnt, flags);
442 * Allow userspace to request a mountpoint be expired rather than
443 * unmounting unconditionally. Unmount only happens if:
444 * (1) the mark is already set (the mark is cleared by mntput())
445 * (2) the usage count == 1 [parent vfsmount] + 1 [sys_umount]
447 if (flags & MNT_EXPIRE) {
448 if (mnt == current->fs->rootmnt ||
449 flags & (MNT_FORCE | MNT_DETACH))
452 if (atomic_read(&mnt->mnt_count) != 2)
455 if (!xchg(&mnt->mnt_expiry_mark, 1))
460 * If we may have to abort operations to get out of this
461 * mount, and they will themselves hold resources we must
462 * allow the fs to do things. In the Unix tradition of
463 * 'Gee thats tricky lets do it in userspace' the umount_begin
464 * might fail to complete on the first run through as other tasks
465 * must return, and the like. Thats for the mount program to worry
466 * about for the moment.
470 if( (flags&MNT_FORCE) && sb->s_op->umount_begin)
471 sb->s_op->umount_begin(sb);
475 * No sense to grab the lock for this test, but test itself looks
476 * somewhat bogus. Suggestions for better replacement?
477 * Ho-hum... In principle, we might treat that as umount + switch
478 * to rootfs. GC would eventually take care of the old vfsmount.
479 * Actually it makes sense, especially if rootfs would contain a
480 * /reboot - static binary that would close all descriptors and
481 * call reboot(9). Then init(8) could umount root and exec /reboot.
483 if (mnt == current->fs->rootmnt && !(flags & MNT_DETACH)) {
485 * Special case for "unmounting" root ...
486 * we just try to remount it readonly.
488 down_write(&sb->s_umount);
489 if (!(sb->s_flags & MS_RDONLY)) {
492 retval = do_remount_sb(sb, MS_RDONLY, NULL, 0);
495 up_write(&sb->s_umount);
499 down_write(¤t->namespace->sem);
500 spin_lock(&vfsmount_lock);
502 if (atomic_read(&sb->s_active) == 1) {
503 /* last instance - try to be smart */
504 spin_unlock(&vfsmount_lock);
509 security_sb_umount_close(mnt);
510 spin_lock(&vfsmount_lock);
513 if (atomic_read(&mnt->mnt_count) == 2 || flags & MNT_DETACH) {
514 if (!list_empty(&mnt->mnt_list))
518 spin_unlock(&vfsmount_lock);
520 security_sb_umount_busy(mnt);
521 up_write(¤t->namespace->sem);
526 * Now umount can handle mount points as well as block devices.
527 * This is important for filesystems which use unnamed block devices.
529 * We now support a flag for forced unmount like the other 'big iron'
530 * unixes. Our API is identical to OSF/1 to avoid making a mess of AMD
533 asmlinkage long sys_umount(char __user * name, int flags)
538 retval = __user_walk(name, LOOKUP_FOLLOW, &nd);
542 if (nd.dentry != nd.mnt->mnt_root)
544 if (!check_mnt(nd.mnt))
548 if (!capable(CAP_SYS_ADMIN) && !vx_ccaps(VXC_SECURE_MOUNT))
551 retval = do_umount(nd.mnt, flags);
553 path_release_on_umount(&nd);
558 #ifdef __ARCH_WANT_SYS_OLDUMOUNT
561 * The 2.0 compatible umount. No flags.
564 asmlinkage long sys_oldumount(char __user * name)
566 return sys_umount(name,0);
571 static int mount_is_safe(struct nameidata *nd)
573 if (capable(CAP_SYS_ADMIN))
575 if (vx_ccaps(VXC_SECURE_MOUNT))
579 if (S_ISLNK(nd->dentry->d_inode->i_mode))
581 if (nd->dentry->d_inode->i_mode & S_ISVTX) {
582 if (current->uid != nd->dentry->d_inode->i_uid)
585 if (permission(nd->dentry->d_inode, MAY_WRITE, nd))
592 lives_below_in_same_fs(struct dentry *d, struct dentry *dentry)
597 if (d == NULL || d == d->d_parent)
603 static struct vfsmount *copy_tree(struct vfsmount *mnt, struct dentry *dentry)
605 struct vfsmount *res, *p, *q, *r, *s;
609 res = q = clone_mnt(mnt, dentry);
612 q->mnt_mountpoint = mnt->mnt_mountpoint;
615 for (h = mnt->mnt_mounts.next; h != &mnt->mnt_mounts; h = h->next) {
616 r = list_entry(h, struct vfsmount, mnt_child);
617 if (!lives_below_in_same_fs(r->mnt_mountpoint, dentry))
620 for (s = r; s; s = next_mnt(s, r)) {
621 while (p != s->mnt_parent) {
627 nd.dentry = p->mnt_mountpoint;
628 q = clone_mnt(p, p->mnt_root);
631 spin_lock(&vfsmount_lock);
632 list_add_tail(&q->mnt_list, &res->mnt_list);
634 spin_unlock(&vfsmount_lock);
640 spin_lock(&vfsmount_lock);
642 spin_unlock(&vfsmount_lock);
647 static int graft_tree(struct vfsmount *mnt, struct nameidata *nd)
650 if (mnt->mnt_sb->s_flags & MS_NOUSER)
653 if (S_ISDIR(nd->dentry->d_inode->i_mode) !=
654 S_ISDIR(mnt->mnt_root->d_inode->i_mode))
658 down(&nd->dentry->d_inode->i_sem);
659 if (IS_DEADDIR(nd->dentry->d_inode))
662 err = security_sb_check_sb(mnt, nd);
667 spin_lock(&vfsmount_lock);
668 if (IS_ROOT(nd->dentry) || !d_unhashed(nd->dentry)) {
669 struct list_head head;
672 list_add_tail(&head, &mnt->mnt_list);
673 list_splice(&head, current->namespace->list.prev);
677 spin_unlock(&vfsmount_lock);
679 up(&nd->dentry->d_inode->i_sem);
681 security_sb_post_addmount(mnt, nd);
688 static int do_loopback(struct nameidata *nd, char *old_name, xid_t xid, int flags)
690 struct nameidata old_nd;
691 struct vfsmount *mnt = NULL;
692 int err = mount_is_safe(nd);
693 int recurse = flags & MS_REC;
696 if (!old_name || !*old_name)
698 err = path_lookup(old_name, LOOKUP_FOLLOW, &old_nd);
702 down_write(¤t->namespace->sem);
704 if (check_mnt(nd->mnt) && (!recurse || check_mnt(old_nd.mnt))) {
707 mnt = copy_tree(old_nd.mnt, old_nd.dentry);
709 mnt = clone_mnt(old_nd.mnt, old_nd.dentry);
713 /* stop bind mounts from expiring */
714 spin_lock(&vfsmount_lock);
715 list_del_init(&mnt->mnt_fslink);
716 spin_unlock(&vfsmount_lock);
718 if (flags & MS_XID) {
720 mnt->mnt_flags |= MNT_XID;
722 err = graft_tree(mnt, nd);
724 spin_lock(&vfsmount_lock);
726 spin_unlock(&vfsmount_lock);
731 up_write(¤t->namespace->sem);
732 path_release(&old_nd);
737 * change filesystem flags. dir should be a physical root of filesystem.
738 * If you've mounted a non-root directory somewhere and want to do remount
739 * on it - tough luck.
742 static int do_remount(struct nameidata *nd, int flags, int mnt_flags,
743 void *data, xid_t xid)
746 struct super_block * sb = nd->mnt->mnt_sb;
748 if (!capable(CAP_SYS_ADMIN) && !vx_ccaps(VXC_SECURE_REMOUNT))
751 if (!check_mnt(nd->mnt))
754 if (nd->dentry != nd->mnt->mnt_root)
757 if (vx_ccaps(VXC_SECURE_REMOUNT))
758 mnt_flags |= MNT_NODEV;
759 down_write(&sb->s_umount);
760 err = do_remount_sb(sb, flags, data, 0);
762 nd->mnt->mnt_flags=mnt_flags;
764 nd->mnt->mnt_xid = xid;
766 up_write(&sb->s_umount);
768 security_sb_post_remount(nd->mnt, flags, data);
772 static int do_move_mount(struct nameidata *nd, char *old_name)
774 struct nameidata old_nd, parent_nd;
777 if (!capable(CAP_SYS_ADMIN) && !vx_ccaps(VXC_SECURE_MOUNT))
779 if (!old_name || !*old_name)
781 err = path_lookup(old_name, LOOKUP_FOLLOW, &old_nd);
785 down_write(¤t->namespace->sem);
786 while(d_mountpoint(nd->dentry) && follow_down(&nd->mnt, &nd->dentry))
789 if (!check_mnt(nd->mnt) || !check_mnt(old_nd.mnt))
793 down(&nd->dentry->d_inode->i_sem);
794 if (IS_DEADDIR(nd->dentry->d_inode))
797 spin_lock(&vfsmount_lock);
798 if (!IS_ROOT(nd->dentry) && d_unhashed(nd->dentry))
802 if (old_nd.dentry != old_nd.mnt->mnt_root)
805 if (old_nd.mnt == old_nd.mnt->mnt_parent)
808 if (S_ISDIR(nd->dentry->d_inode->i_mode) !=
809 S_ISDIR(old_nd.dentry->d_inode->i_mode))
813 for (p = nd->mnt; p->mnt_parent!=p; p = p->mnt_parent)
818 detach_mnt(old_nd.mnt, &parent_nd);
819 attach_mnt(old_nd.mnt, nd);
821 /* if the mount is moved, it should no longer be expire
823 list_del_init(&old_nd.mnt->mnt_fslink);
825 spin_unlock(&vfsmount_lock);
827 up(&nd->dentry->d_inode->i_sem);
829 up_write(¤t->namespace->sem);
831 path_release(&parent_nd);
832 path_release(&old_nd);
837 * create a new mount for userspace and request it to be added into the
840 static int do_new_mount(struct nameidata *nd, char *type, int flags,
841 int mnt_flags, char *name, void *data)
843 struct vfsmount *mnt;
845 if (!type || !memchr(type, 0, PAGE_SIZE))
848 /* we need capabilities... */
849 if (!capable(CAP_SYS_ADMIN) && !vx_ccaps(VXC_SECURE_MOUNT))
852 mnt = do_kern_mount(type, flags, name, data);
856 return do_add_mount(mnt, nd, mnt_flags, NULL);
860 * add a mount into a namespace's mount tree
861 * - provide the option of adding the new mount to an expiration list
863 int do_add_mount(struct vfsmount *newmnt, struct nameidata *nd,
864 int mnt_flags, struct list_head *fslist)
868 down_write(¤t->namespace->sem);
869 /* Something was mounted here while we slept */
870 while(d_mountpoint(nd->dentry) && follow_down(&nd->mnt, &nd->dentry))
873 if (!check_mnt(nd->mnt))
876 /* Refuse the same filesystem on the same mount point */
878 if (nd->mnt->mnt_sb == newmnt->mnt_sb &&
879 nd->mnt->mnt_root == nd->dentry)
883 if (S_ISLNK(newmnt->mnt_root->d_inode->i_mode))
886 newmnt->mnt_flags = mnt_flags;
887 err = graft_tree(newmnt, nd);
889 if (err == 0 && fslist) {
890 /* add to the specified expiration list */
891 spin_lock(&vfsmount_lock);
892 list_add_tail(&newmnt->mnt_fslink, fslist);
893 spin_unlock(&vfsmount_lock);
897 up_write(¤t->namespace->sem);
902 EXPORT_SYMBOL_GPL(do_add_mount);
905 * process a list of expirable mountpoints with the intent of discarding any
906 * mountpoints that aren't in use and haven't been touched since last we came
909 void mark_mounts_for_expiry(struct list_head *mounts)
911 struct namespace *namespace;
912 struct vfsmount *mnt, *next;
913 LIST_HEAD(graveyard);
915 if (list_empty(mounts))
918 spin_lock(&vfsmount_lock);
920 /* extract from the expiration list every vfsmount that matches the
921 * following criteria:
922 * - only referenced by its parent vfsmount
923 * - still marked for expiry (marked on the last call here; marks are
924 * cleared by mntput())
926 list_for_each_entry_safe(mnt, next, mounts, mnt_fslink) {
927 if (!xchg(&mnt->mnt_expiry_mark, 1) ||
928 atomic_read(&mnt->mnt_count) != 1)
932 list_move(&mnt->mnt_fslink, &graveyard);
936 * go through the vfsmounts we've just consigned to the graveyard to
937 * - check that they're still dead
938 * - delete the vfsmount from the appropriate namespace under lock
939 * - dispose of the corpse
941 while (!list_empty(&graveyard)) {
942 mnt = list_entry(graveyard.next, struct vfsmount, mnt_fslink);
943 list_del_init(&mnt->mnt_fslink);
945 /* don't do anything if the namespace is dead - all the
946 * vfsmounts from it are going away anyway */
947 namespace = mnt->mnt_namespace;
948 if (!namespace || atomic_read(&namespace->count) <= 0)
950 get_namespace(namespace);
952 spin_unlock(&vfsmount_lock);
953 down_write(&namespace->sem);
954 spin_lock(&vfsmount_lock);
956 /* check that it is still dead: the count should now be 2 - as
957 * contributed by the vfsmount parent and the mntget above */
958 if (atomic_read(&mnt->mnt_count) == 2) {
959 struct vfsmount *xdmnt;
960 struct dentry *xdentry;
962 /* delete from the namespace */
963 list_del_init(&mnt->mnt_list);
964 list_del_init(&mnt->mnt_child);
965 list_del_init(&mnt->mnt_hash);
966 mnt->mnt_mountpoint->d_mounted--;
968 xdentry = mnt->mnt_mountpoint;
969 mnt->mnt_mountpoint = mnt->mnt_root;
970 xdmnt = mnt->mnt_parent;
971 mnt->mnt_parent = mnt;
973 spin_unlock(&vfsmount_lock);
978 /* now lay it to rest if this was the last ref on the
980 if (atomic_read(&mnt->mnt_sb->s_active) == 1) {
981 /* last instance - try to be smart */
983 DQUOT_OFF(mnt->mnt_sb);
984 acct_auto_close(mnt->mnt_sb);
990 /* someone brought it back to life whilst we didn't
991 * have any locks held so return it to the expiration
993 list_add_tail(&mnt->mnt_fslink, mounts);
994 spin_unlock(&vfsmount_lock);
997 up_write(&namespace->sem);
1000 put_namespace(namespace);
1002 spin_lock(&vfsmount_lock);
1005 spin_unlock(&vfsmount_lock);
1008 EXPORT_SYMBOL_GPL(mark_mounts_for_expiry);
1011 * Some copy_from_user() implementations do not return the exact number of
1012 * bytes remaining to copy on a fault. But copy_mount_options() requires that.
1013 * Note that this function differs from copy_from_user() in that it will oops
1014 * on bad values of `to', rather than returning a short copy.
1017 exact_copy_from_user(void *to, const void __user *from, unsigned long n)
1020 const char __user *f = from;
1023 if (!access_ok(VERIFY_READ, from, n))
1027 if (__get_user(c, f)) {
1038 int copy_mount_options(const void __user *data, unsigned long *where)
1048 if (!(page = __get_free_page(GFP_KERNEL)))
1051 /* We only care that *some* data at the address the user
1052 * gave us is valid. Just in case, we'll zero
1053 * the remainder of the page.
1055 /* copy_from_user cannot cross TASK_SIZE ! */
1056 size = TASK_SIZE - (unsigned long)data;
1057 if (size > PAGE_SIZE)
1060 i = size - exact_copy_from_user((void *)page, data, size);
1066 memset((char *)page + i, 0, PAGE_SIZE - i);
1072 * Flags is a 32-bit value that allows up to 31 non-fs dependent flags to
1073 * be given to the mount() call (ie: read-only, no-dev, no-suid etc).
1075 * data is a (void *) that can point to any structure up to
1076 * PAGE_SIZE-1 bytes, which can contain arbitrary fs-dependent
1077 * information (or be NULL).
1079 * Pre-0.97 versions of mount() didn't have a flags word.
1080 * When the flags word was introduced its top half was required
1081 * to have the magic value 0xC0ED, and this remained so until 2.4.0-test9.
1082 * Therefore, if this magic number is present, it carries no information
1083 * and must be discarded.
1085 long do_mount(char * dev_name, char * dir_name, char *type_page,
1086 unsigned long flags, void *data_page)
1088 struct nameidata nd;
1094 if ((flags & MS_MGC_MSK) == MS_MGC_VAL)
1095 flags &= ~MS_MGC_MSK;
1097 /* Basic sanity checks */
1099 if (!dir_name || !*dir_name || !memchr(dir_name, 0, PAGE_SIZE))
1101 if (dev_name && !memchr(dev_name, 0, PAGE_SIZE))
1105 ((char *)data_page)[PAGE_SIZE - 1] = 0;
1107 retval = vx_parse_xid(data_page, &xid, 1);
1109 mnt_flags |= MNT_XID;
1110 /* bind and re-mounts get xid flag */
1111 if (flags & (MS_BIND|MS_REMOUNT))
1115 /* Separate the per-mountpoint flags */
1116 if (flags & MS_NOSUID)
1117 mnt_flags |= MNT_NOSUID;
1118 if (flags & MS_NODEV)
1119 mnt_flags |= MNT_NODEV;
1120 if (flags & MS_NOEXEC)
1121 mnt_flags |= MNT_NOEXEC;
1122 flags &= ~(MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_ACTIVE);
1124 if (vx_ccaps(VXC_SECURE_MOUNT))
1125 mnt_flags |= MNT_NODEV;
1127 /* ... and get the mountpoint */
1128 retval = path_lookup(dir_name, LOOKUP_FOLLOW, &nd);
1132 retval = security_sb_mount(dev_name, &nd, type_page, flags, data_page);
1136 if (flags & MS_REMOUNT)
1137 retval = do_remount(&nd, flags & ~MS_REMOUNT, mnt_flags,
1139 else if (flags & MS_BIND)
1140 retval = do_loopback(&nd, dev_name, xid, flags);
1141 else if (flags & MS_MOVE)
1142 retval = do_move_mount(&nd, dev_name);
1144 retval = do_new_mount(&nd, type_page, flags, mnt_flags,
1145 dev_name, data_page);
1151 int copy_namespace(int flags, struct task_struct *tsk)
1153 struct namespace *namespace = tsk->namespace;
1154 struct namespace *new_ns;
1155 struct vfsmount *rootmnt = NULL, *pwdmnt = NULL, *altrootmnt = NULL;
1156 struct fs_struct *fs = tsk->fs;
1157 struct vfsmount *p, *q;
1162 get_namespace(namespace);
1164 if (!(flags & CLONE_NEWNS))
1167 if (!capable(CAP_SYS_ADMIN) && !vx_ccaps(VXC_SECURE_MOUNT)) {
1168 put_namespace(namespace);
1172 new_ns = kmalloc(sizeof(struct namespace), GFP_KERNEL);
1176 atomic_set(&new_ns->count, 1);
1177 init_rwsem(&new_ns->sem);
1178 INIT_LIST_HEAD(&new_ns->list);
1180 down_write(&tsk->namespace->sem);
1181 /* First pass: copy the tree topology */
1182 new_ns->root = copy_tree(namespace->root, namespace->root->mnt_root);
1183 if (!new_ns->root) {
1184 up_write(&tsk->namespace->sem);
1188 spin_lock(&vfsmount_lock);
1189 list_add_tail(&new_ns->list, &new_ns->root->mnt_list);
1190 spin_unlock(&vfsmount_lock);
1193 * Second pass: switch the tsk->fs->* elements and mark new vfsmounts
1194 * as belonging to new namespace. We have already acquired a private
1195 * fs_struct, so tsk->fs->lock is not needed.
1197 p = namespace->root;
1200 q->mnt_namespace = new_ns;
1202 if (p == fs->rootmnt) {
1204 fs->rootmnt = mntget(q);
1206 if (p == fs->pwdmnt) {
1208 fs->pwdmnt = mntget(q);
1210 if (p == fs->altrootmnt) {
1212 fs->altrootmnt = mntget(q);
1215 p = next_mnt(p, namespace->root);
1216 q = next_mnt(q, new_ns->root);
1218 up_write(&tsk->namespace->sem);
1220 tsk->namespace = new_ns;
1229 put_namespace(namespace);
1233 put_namespace(namespace);
1237 asmlinkage long sys_mount(char __user * dev_name, char __user * dir_name,
1238 char __user * type, unsigned long flags,
1242 unsigned long data_page;
1243 unsigned long type_page;
1244 unsigned long dev_page;
1247 retval = copy_mount_options (type, &type_page);
1251 dir_page = getname(dir_name);
1252 retval = PTR_ERR(dir_page);
1253 if (IS_ERR(dir_page))
1256 retval = copy_mount_options (dev_name, &dev_page);
1260 retval = copy_mount_options (data, &data_page);
1265 retval = do_mount((char*)dev_page, dir_page, (char*)type_page,
1266 flags, (void*)data_page);
1268 free_page(data_page);
1271 free_page(dev_page);
1275 free_page(type_page);
1280 * Replace the fs->{rootmnt,root} with {mnt,dentry}. Put the old values.
1281 * It can block. Requires the big lock held.
1283 void set_fs_root(struct fs_struct *fs, struct vfsmount *mnt,
1284 struct dentry *dentry)
1286 struct dentry *old_root;
1287 struct vfsmount *old_rootmnt;
1288 write_lock(&fs->lock);
1289 old_root = fs->root;
1290 old_rootmnt = fs->rootmnt;
1291 fs->rootmnt = mntget(mnt);
1292 fs->root = dget(dentry);
1293 write_unlock(&fs->lock);
1296 mntput(old_rootmnt);
1301 * Replace the fs->{pwdmnt,pwd} with {mnt,dentry}. Put the old values.
1302 * It can block. Requires the big lock held.
1304 void set_fs_pwd(struct fs_struct *fs, struct vfsmount *mnt,
1305 struct dentry *dentry)
1307 struct dentry *old_pwd;
1308 struct vfsmount *old_pwdmnt;
1310 write_lock(&fs->lock);
1312 old_pwdmnt = fs->pwdmnt;
1313 fs->pwdmnt = mntget(mnt);
1314 fs->pwd = dget(dentry);
1315 write_unlock(&fs->lock);
1323 static void chroot_fs_refs(struct nameidata *old_nd, struct nameidata *new_nd)
1325 struct task_struct *g, *p;
1326 struct fs_struct *fs;
1328 read_lock(&tasklist_lock);
1329 do_each_thread(g, p) {
1333 atomic_inc(&fs->count);
1335 if (fs->root==old_nd->dentry&&fs->rootmnt==old_nd->mnt)
1336 set_fs_root(fs, new_nd->mnt, new_nd->dentry);
1337 if (fs->pwd==old_nd->dentry&&fs->pwdmnt==old_nd->mnt)
1338 set_fs_pwd(fs, new_nd->mnt, new_nd->dentry);
1342 } while_each_thread(g, p);
1343 read_unlock(&tasklist_lock);
1347 * Moves the current root to put_root, and sets root/cwd of all processes
1348 * which had them on the old root to new_root.
1351 * - we don't move root/cwd if they are not at the root (reason: if something
1352 * cared enough to change them, it's probably wrong to force them elsewhere)
1353 * - it's okay to pick a root that isn't the root of a file system, e.g.
1354 * /nfs/my_root where /nfs is the mount point. It must be a mountpoint,
1355 * though, so you may need to say mount --bind /nfs/my_root /nfs/my_root
1359 asmlinkage long sys_pivot_root(const char __user *new_root, const char __user *put_old)
1361 struct vfsmount *tmp;
1362 struct nameidata new_nd, old_nd, parent_nd, root_parent, user_nd;
1365 if (!capable(CAP_SYS_ADMIN))
1370 error = __user_walk(new_root, LOOKUP_FOLLOW|LOOKUP_DIRECTORY, &new_nd);
1374 if (!check_mnt(new_nd.mnt))
1377 error = __user_walk(put_old, LOOKUP_FOLLOW|LOOKUP_DIRECTORY, &old_nd);
1381 error = security_sb_pivotroot(&old_nd, &new_nd);
1383 path_release(&old_nd);
1387 read_lock(¤t->fs->lock);
1388 user_nd.mnt = mntget(current->fs->rootmnt);
1389 user_nd.dentry = dget(current->fs->root);
1390 read_unlock(¤t->fs->lock);
1391 down_write(¤t->namespace->sem);
1392 down(&old_nd.dentry->d_inode->i_sem);
1394 if (!check_mnt(user_nd.mnt))
1397 if (IS_DEADDIR(new_nd.dentry->d_inode))
1399 if (d_unhashed(new_nd.dentry) && !IS_ROOT(new_nd.dentry))
1401 if (d_unhashed(old_nd.dentry) && !IS_ROOT(old_nd.dentry))
1404 if (new_nd.mnt == user_nd.mnt || old_nd.mnt == user_nd.mnt)
1405 goto out2; /* loop */
1407 if (user_nd.mnt->mnt_root != user_nd.dentry)
1409 if (new_nd.mnt->mnt_root != new_nd.dentry)
1410 goto out2; /* not a mountpoint */
1411 tmp = old_nd.mnt; /* make sure we can reach put_old from new_root */
1412 spin_lock(&vfsmount_lock);
1413 if (tmp != new_nd.mnt) {
1415 if (tmp->mnt_parent == tmp)
1417 if (tmp->mnt_parent == new_nd.mnt)
1419 tmp = tmp->mnt_parent;
1421 if (!is_subdir(tmp->mnt_mountpoint, new_nd.dentry))
1423 } else if (!is_subdir(old_nd.dentry, new_nd.dentry))
1425 detach_mnt(new_nd.mnt, &parent_nd);
1426 detach_mnt(user_nd.mnt, &root_parent);
1427 attach_mnt(user_nd.mnt, &old_nd);
1428 attach_mnt(new_nd.mnt, &root_parent);
1429 spin_unlock(&vfsmount_lock);
1430 chroot_fs_refs(&user_nd, &new_nd);
1431 security_sb_post_pivotroot(&user_nd, &new_nd);
1433 path_release(&root_parent);
1434 path_release(&parent_nd);
1436 up(&old_nd.dentry->d_inode->i_sem);
1437 up_write(¤t->namespace->sem);
1438 path_release(&user_nd);
1439 path_release(&old_nd);
1441 path_release(&new_nd);
1446 spin_unlock(&vfsmount_lock);
1450 static void __init init_mount_tree(void)
1452 struct vfsmount *mnt;
1453 struct namespace *namespace;
1454 struct task_struct *g, *p;
1456 mnt = do_kern_mount("rootfs", 0, "rootfs", NULL);
1458 panic("Can't create rootfs");
1459 namespace = kmalloc(sizeof(*namespace), GFP_KERNEL);
1461 panic("Can't allocate initial namespace");
1462 atomic_set(&namespace->count, 1);
1463 INIT_LIST_HEAD(&namespace->list);
1464 init_rwsem(&namespace->sem);
1465 list_add(&mnt->mnt_list, &namespace->list);
1466 namespace->root = mnt;
1467 mnt->mnt_namespace = namespace;
1469 init_task.namespace = namespace;
1470 read_lock(&tasklist_lock);
1471 do_each_thread(g, p) {
1472 get_namespace(namespace);
1473 p->namespace = namespace;
1474 } while_each_thread(g, p);
1475 read_unlock(&tasklist_lock);
1477 set_fs_pwd(current->fs, namespace->root, namespace->root->mnt_root);
1478 set_fs_root(current->fs, namespace->root, namespace->root->mnt_root);
1481 void __init mnt_init(unsigned long mempages)
1483 struct list_head *d;
1484 unsigned long order;
1485 unsigned int nr_hash;
1488 mnt_cache = kmem_cache_create("mnt_cache", sizeof(struct vfsmount),
1489 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
1492 mount_hashtable = (struct list_head *)
1493 __get_free_pages(GFP_ATOMIC, order);
1495 if (!mount_hashtable)
1496 panic("Failed to allocate mount hash table\n");
1499 * Find the power-of-two list-heads that can fit into the allocation..
1500 * We don't guarantee that "sizeof(struct list_head)" is necessarily
1503 nr_hash = (1UL << order) * PAGE_SIZE / sizeof(struct list_head);
1507 } while ((nr_hash >> hash_bits) != 0);
1511 * Re-calculate the actual number of entries and the mask
1512 * from the number of bits we can fit.
1514 nr_hash = 1UL << hash_bits;
1515 hash_mask = nr_hash-1;
1517 printk("Mount-cache hash table entries: %d (order: %ld, %ld bytes)\n",
1518 nr_hash, order, (PAGE_SIZE << order));
1520 /* And initialize the newly allocated array */
1521 d = mount_hashtable;
1533 void __put_namespace(struct namespace *namespace)
1535 struct vfsmount *mnt;
1537 down_write(&namespace->sem);
1538 spin_lock(&vfsmount_lock);
1540 list_for_each_entry(mnt, &namespace->list, mnt_list) {
1541 mnt->mnt_namespace = NULL;
1544 umount_tree(namespace->root);
1545 spin_unlock(&vfsmount_lock);
1546 up_write(&namespace->sem);