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>
26 #include <asm/uaccess.h>
27 #include <asm/unistd.h>
29 extern int __init init_rootfs(void);
32 extern int __init sysfs_init(void);
34 static inline int sysfs_init(void)
40 /* spinlock for vfsmount related operations, inplace of dcache_lock */
41 spinlock_t vfsmount_lock __cacheline_aligned_in_smp = SPIN_LOCK_UNLOCKED;
43 static struct list_head *mount_hashtable;
44 static int hash_mask, hash_bits;
45 static kmem_cache_t *mnt_cache;
47 static inline unsigned long hash(struct vfsmount *mnt, struct dentry *dentry)
49 unsigned long tmp = ((unsigned long) mnt / L1_CACHE_BYTES);
50 tmp += ((unsigned long) dentry / L1_CACHE_BYTES);
51 tmp = tmp + (tmp >> hash_bits);
52 return tmp & hash_mask;
55 struct vfsmount *alloc_vfsmnt(const char *name)
57 struct vfsmount *mnt = kmem_cache_alloc(mnt_cache, GFP_KERNEL);
59 memset(mnt, 0, sizeof(struct vfsmount));
60 atomic_set(&mnt->mnt_count,1);
61 INIT_LIST_HEAD(&mnt->mnt_hash);
62 INIT_LIST_HEAD(&mnt->mnt_child);
63 INIT_LIST_HEAD(&mnt->mnt_mounts);
64 INIT_LIST_HEAD(&mnt->mnt_list);
65 INIT_LIST_HEAD(&mnt->mnt_fslink);
67 int size = strlen(name)+1;
68 char *newname = kmalloc(size, GFP_KERNEL);
70 memcpy(newname, name, size);
71 mnt->mnt_devname = newname;
78 void free_vfsmnt(struct vfsmount *mnt)
80 kfree(mnt->mnt_devname);
81 kmem_cache_free(mnt_cache, mnt);
85 * Now, lookup_mnt increments the ref count before returning
86 * the vfsmount struct.
88 struct vfsmount *lookup_mnt(struct vfsmount *mnt, struct dentry *dentry)
90 struct list_head * head = mount_hashtable + hash(mnt, dentry);
91 struct list_head * tmp = head;
92 struct vfsmount *p, *found = NULL;
94 spin_lock(&vfsmount_lock);
100 p = list_entry(tmp, struct vfsmount, mnt_hash);
101 if (p->mnt_parent == mnt && p->mnt_mountpoint == dentry) {
106 spin_unlock(&vfsmount_lock);
110 EXPORT_SYMBOL(lookup_mnt);
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;
168 /* stick the duplicate mount on the same expiry list
169 * as the original if that was on one */
170 spin_lock(&vfsmount_lock);
171 if (!list_empty(&old->mnt_fslink))
172 list_add(&mnt->mnt_fslink, &old->mnt_fslink);
173 spin_unlock(&vfsmount_lock);
178 void __mntput(struct vfsmount *mnt)
180 struct super_block *sb = mnt->mnt_sb;
183 deactivate_super(sb);
186 EXPORT_SYMBOL(__mntput);
189 static void *m_start(struct seq_file *m, loff_t *pos)
191 struct namespace *n = m->private;
196 list_for_each(p, &n->list)
198 return list_entry(p, struct vfsmount, mnt_list);
202 static void *m_next(struct seq_file *m, void *v, loff_t *pos)
204 struct namespace *n = m->private;
205 struct list_head *p = ((struct vfsmount *)v)->mnt_list.next;
207 return p==&n->list ? NULL : list_entry(p, struct vfsmount, mnt_list);
210 static void m_stop(struct seq_file *m, void *v)
212 struct namespace *n = m->private;
216 static inline void mangle(struct seq_file *m, const char *s)
218 seq_escape(m, s, " \t\n\\");
221 static int show_vfsmnt(struct seq_file *m, void *v)
223 struct vfsmount *mnt = v;
225 static struct proc_fs_info {
231 { MS_RDONLY, MNT_RDONLY, "ro", "rw" },
232 { MS_SYNCHRONOUS, 0, ",sync", NULL },
233 { MS_DIRSYNC, 0, ",dirsync", NULL },
234 { MS_MANDLOCK, 0, ",mand", NULL },
235 { MS_NOATIME, MNT_NOATIME, ",noatime", NULL },
236 { MS_NODIRATIME, MNT_NODIRATIME, ",nodiratime", NULL },
237 { MS_TAGXID, MS_TAGXID, ",tagxid", NULL },
238 { 0, MNT_NOSUID, ",nosuid", NULL },
239 { 0, MNT_NODEV, ",nodev", NULL },
240 { 0, MNT_NOEXEC, ",noexec", NULL },
243 struct proc_fs_info *p;
244 unsigned long s_flags = mnt->mnt_sb->s_flags;
245 int mnt_flags = mnt->mnt_flags;
247 if (vx_flags(VXF_HIDE_MOUNT, 0))
250 mangle(m, mnt->mnt_devname ? mnt->mnt_devname : "none");
252 seq_path(m, mnt, mnt->mnt_root, " \t\n\\");
254 mangle(m, mnt->mnt_sb->s_type->name);
256 for (p = fs_info; (p->s_flag | p->mnt_flag) ; p++) {
257 if ((s_flags & p->s_flag) || (mnt_flags & p->mnt_flag)) {
259 seq_puts(m, p->set_str);
262 seq_puts(m, p->unset_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, unsigned long flags, int mnt_flags)
652 struct nameidata old_nd;
653 struct vfsmount *mnt = NULL;
654 int recurse = flags & MS_REC;
655 int err = mount_is_safe(nd);
659 if (!old_name || !*old_name)
661 err = path_lookup(old_name, LOOKUP_FOLLOW, &old_nd);
665 down_write(¤t->namespace->sem);
667 if (check_mnt(nd->mnt) && (!recurse || check_mnt(old_nd.mnt))) {
670 mnt = copy_tree(old_nd.mnt, old_nd.dentry);
672 mnt = clone_mnt(old_nd.mnt, old_nd.dentry);
676 /* stop bind mounts from expiring */
677 spin_lock(&vfsmount_lock);
678 list_del_init(&mnt->mnt_fslink);
679 spin_unlock(&vfsmount_lock);
681 err = graft_tree(mnt, nd);
683 spin_lock(&vfsmount_lock);
685 spin_unlock(&vfsmount_lock);
688 mnt->mnt_flags = mnt_flags;
691 up_write(¤t->namespace->sem);
692 path_release(&old_nd);
697 * change filesystem flags. dir should be a physical root of filesystem.
698 * If you've mounted a non-root directory somewhere and want to do remount
699 * on it - tough luck.
702 static int do_remount(struct nameidata *nd, int flags, int mnt_flags,
706 struct super_block * sb = nd->mnt->mnt_sb;
708 if (!capable(CAP_SYS_ADMIN))
711 if (!check_mnt(nd->mnt))
714 if (nd->dentry != nd->mnt->mnt_root)
717 down_write(&sb->s_umount);
718 err = do_remount_sb(sb, flags, data, 0);
720 nd->mnt->mnt_flags=mnt_flags;
721 up_write(&sb->s_umount);
723 security_sb_post_remount(nd->mnt, flags, data);
727 static int do_move_mount(struct nameidata *nd, char *old_name)
729 struct nameidata old_nd, parent_nd;
732 if (!capable(CAP_SYS_ADMIN))
734 if (!old_name || !*old_name)
736 err = path_lookup(old_name, LOOKUP_FOLLOW, &old_nd);
740 down_write(¤t->namespace->sem);
741 while(d_mountpoint(nd->dentry) && follow_down(&nd->mnt, &nd->dentry))
744 if (!check_mnt(nd->mnt) || !check_mnt(old_nd.mnt))
748 down(&nd->dentry->d_inode->i_sem);
749 if (IS_DEADDIR(nd->dentry->d_inode))
752 spin_lock(&vfsmount_lock);
753 if (!IS_ROOT(nd->dentry) && d_unhashed(nd->dentry))
757 if (old_nd.dentry != old_nd.mnt->mnt_root)
760 if (old_nd.mnt == old_nd.mnt->mnt_parent)
763 if (S_ISDIR(nd->dentry->d_inode->i_mode) !=
764 S_ISDIR(old_nd.dentry->d_inode->i_mode))
768 for (p = nd->mnt; p->mnt_parent!=p; p = p->mnt_parent)
773 detach_mnt(old_nd.mnt, &parent_nd);
774 attach_mnt(old_nd.mnt, nd);
776 /* if the mount is moved, it should no longer be expire
778 list_del_init(&old_nd.mnt->mnt_fslink);
780 spin_unlock(&vfsmount_lock);
782 up(&nd->dentry->d_inode->i_sem);
784 up_write(¤t->namespace->sem);
786 path_release(&parent_nd);
787 path_release(&old_nd);
792 * create a new mount for userspace and request it to be added into the
795 static int do_new_mount(struct nameidata *nd, char *type, int flags,
796 int mnt_flags, char *name, void *data)
798 struct vfsmount *mnt;
800 if (!type || !memchr(type, 0, PAGE_SIZE))
803 /* we need capabilities... */
804 if (!capable(CAP_SYS_ADMIN) && !vx_ccaps(VXC_SECURE_MOUNT))
807 mnt = do_kern_mount(type, flags, name, data);
811 return do_add_mount(mnt, nd, mnt_flags, NULL);
815 * add a mount into a namespace's mount tree
816 * - provide the option of adding the new mount to an expiration list
818 int do_add_mount(struct vfsmount *newmnt, struct nameidata *nd,
819 int mnt_flags, struct list_head *fslist)
823 down_write(¤t->namespace->sem);
824 /* Something was mounted here while we slept */
825 while(d_mountpoint(nd->dentry) && follow_down(&nd->mnt, &nd->dentry))
828 if (!check_mnt(nd->mnt))
831 /* Refuse the same filesystem on the same mount point */
833 if (nd->mnt->mnt_sb == newmnt->mnt_sb &&
834 nd->mnt->mnt_root == nd->dentry)
838 if (S_ISLNK(newmnt->mnt_root->d_inode->i_mode))
841 newmnt->mnt_flags = mnt_flags;
842 err = graft_tree(newmnt, nd);
844 if (err == 0 && fslist) {
845 /* add to the specified expiration list */
846 spin_lock(&vfsmount_lock);
847 list_add_tail(&newmnt->mnt_fslink, fslist);
848 spin_unlock(&vfsmount_lock);
852 up_write(¤t->namespace->sem);
857 EXPORT_SYMBOL_GPL(do_add_mount);
860 * process a list of expirable mountpoints with the intent of discarding any
861 * mountpoints that aren't in use and haven't been touched since last we came
864 void mark_mounts_for_expiry(struct list_head *mounts)
866 struct namespace *namespace;
867 struct vfsmount *mnt, *next;
868 LIST_HEAD(graveyard);
870 if (list_empty(mounts))
873 spin_lock(&vfsmount_lock);
875 /* extract from the expiration list every vfsmount that matches the
876 * following criteria:
877 * - only referenced by its parent vfsmount
878 * - still marked for expiry (marked on the last call here; marks are
879 * cleared by mntput())
881 list_for_each_entry_safe(mnt, next, mounts, mnt_fslink) {
882 if (!xchg(&mnt->mnt_expiry_mark, 1) ||
883 atomic_read(&mnt->mnt_count) != 1)
887 list_move(&mnt->mnt_fslink, &graveyard);
891 * go through the vfsmounts we've just consigned to the graveyard to
892 * - check that they're still dead
893 * - delete the vfsmount from the appropriate namespace under lock
894 * - dispose of the corpse
896 while (!list_empty(&graveyard)) {
897 mnt = list_entry(graveyard.next, struct vfsmount, mnt_fslink);
898 list_del_init(&mnt->mnt_fslink);
900 /* don't do anything if the namespace is dead - all the
901 * vfsmounts from it are going away anyway */
902 namespace = mnt->mnt_namespace;
903 if (!namespace || atomic_read(&namespace->count) <= 0)
905 get_namespace(namespace);
907 spin_unlock(&vfsmount_lock);
908 down_write(&namespace->sem);
909 spin_lock(&vfsmount_lock);
911 /* check that it is still dead: the count should now be 2 - as
912 * contributed by the vfsmount parent and the mntget above */
913 if (atomic_read(&mnt->mnt_count) == 2) {
914 struct vfsmount *xdmnt;
915 struct dentry *xdentry;
917 /* delete from the namespace */
918 list_del_init(&mnt->mnt_list);
919 list_del_init(&mnt->mnt_child);
920 list_del_init(&mnt->mnt_hash);
921 mnt->mnt_mountpoint->d_mounted--;
923 xdentry = mnt->mnt_mountpoint;
924 mnt->mnt_mountpoint = mnt->mnt_root;
925 xdmnt = mnt->mnt_parent;
926 mnt->mnt_parent = mnt;
928 spin_unlock(&vfsmount_lock);
933 /* now lay it to rest if this was the last ref on the
935 if (atomic_read(&mnt->mnt_sb->s_active) == 1) {
936 /* last instance - try to be smart */
938 DQUOT_OFF(mnt->mnt_sb);
939 acct_auto_close(mnt->mnt_sb);
945 /* someone brought it back to life whilst we didn't
946 * have any locks held so return it to the expiration
948 list_add_tail(&mnt->mnt_fslink, mounts);
949 spin_unlock(&vfsmount_lock);
952 up_write(&namespace->sem);
955 put_namespace(namespace);
957 spin_lock(&vfsmount_lock);
960 spin_unlock(&vfsmount_lock);
963 EXPORT_SYMBOL_GPL(mark_mounts_for_expiry);
965 int copy_mount_options (const void __user *data, unsigned long *where)
975 if (!(page = __get_free_page(GFP_KERNEL)))
978 /* We only care that *some* data at the address the user
979 * gave us is valid. Just in case, we'll zero
980 * the remainder of the page.
982 /* copy_from_user cannot cross TASK_SIZE ! */
983 size = TASK_SIZE - (unsigned long)data;
984 if (size > PAGE_SIZE)
987 i = size - copy_from_user((void *)page, data, size);
993 memset((char *)page + i, 0, PAGE_SIZE - i);
999 * Flags is a 32-bit value that allows up to 31 non-fs dependent flags to
1000 * be given to the mount() call (ie: read-only, no-dev, no-suid etc).
1002 * data is a (void *) that can point to any structure up to
1003 * PAGE_SIZE-1 bytes, which can contain arbitrary fs-dependent
1004 * information (or be NULL).
1006 * Pre-0.97 versions of mount() didn't have a flags word.
1007 * When the flags word was introduced its top half was required
1008 * to have the magic value 0xC0ED, and this remained so until 2.4.0-test9.
1009 * Therefore, if this magic number is present, it carries no information
1010 * and must be discarded.
1012 long do_mount(char * dev_name, char * dir_name, char *type_page,
1013 unsigned long flags, void *data_page)
1015 struct nameidata nd;
1020 if ((flags & MS_MGC_MSK) == MS_MGC_VAL)
1021 flags &= ~MS_MGC_MSK;
1023 /* Basic sanity checks */
1025 if (!dir_name || !*dir_name || !memchr(dir_name, 0, PAGE_SIZE))
1027 if (dev_name && !memchr(dev_name, 0, PAGE_SIZE))
1031 ((char *)data_page)[PAGE_SIZE - 1] = 0;
1033 /* Separate the per-mountpoint flags */
1034 if (flags & MS_RDONLY)
1035 mnt_flags |= MNT_RDONLY;
1036 if (flags & MS_NOSUID)
1037 mnt_flags |= MNT_NOSUID;
1038 if (flags & MS_NODEV)
1039 mnt_flags |= MNT_NODEV;
1040 if (flags & MS_NOEXEC)
1041 mnt_flags |= MNT_NOEXEC;
1042 if (flags & MS_NOATIME)
1043 mnt_flags |= MNT_NOATIME;
1044 if (flags & MS_NODIRATIME)
1045 mnt_flags |= MNT_NODIRATIME;
1046 flags &= ~(MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_ACTIVE);
1048 if (vx_ccaps(VXC_SECURE_MOUNT))
1049 mnt_flags |= MNT_NODEV;
1051 /* ... and get the mountpoint */
1052 retval = path_lookup(dir_name, LOOKUP_FOLLOW, &nd);
1056 retval = security_sb_mount(dev_name, &nd, type_page, flags, data_page);
1060 if (flags & MS_REMOUNT)
1061 retval = do_remount(&nd, flags & ~MS_REMOUNT, mnt_flags,
1063 else if (flags & MS_BIND)
1064 retval = do_loopback(&nd, dev_name, flags, mnt_flags);
1065 else if (flags & MS_MOVE)
1066 retval = do_move_mount(&nd, dev_name);
1068 retval = do_new_mount(&nd, type_page, flags, mnt_flags,
1069 dev_name, data_page);
1075 int copy_namespace(int flags, struct task_struct *tsk)
1077 struct namespace *namespace = tsk->namespace;
1078 struct namespace *new_ns;
1079 struct vfsmount *rootmnt = NULL, *pwdmnt = NULL, *altrootmnt = NULL;
1080 struct fs_struct *fs = tsk->fs;
1081 struct vfsmount *p, *q;
1086 get_namespace(namespace);
1088 if (!(flags & CLONE_NEWNS))
1091 if (!capable(CAP_SYS_ADMIN)) {
1092 put_namespace(namespace);
1096 new_ns = kmalloc(sizeof(struct namespace), GFP_KERNEL);
1100 atomic_set(&new_ns->count, 1);
1101 init_rwsem(&new_ns->sem);
1102 INIT_LIST_HEAD(&new_ns->list);
1104 down_write(&tsk->namespace->sem);
1105 /* First pass: copy the tree topology */
1106 new_ns->root = copy_tree(namespace->root, namespace->root->mnt_root);
1107 if (!new_ns->root) {
1108 up_write(&tsk->namespace->sem);
1112 spin_lock(&vfsmount_lock);
1113 list_add_tail(&new_ns->list, &new_ns->root->mnt_list);
1114 spin_unlock(&vfsmount_lock);
1117 * Second pass: switch the tsk->fs->* elements and mark new vfsmounts
1118 * as belonging to new namespace. We have already acquired a private
1119 * fs_struct, so tsk->fs->lock is not needed.
1121 p = namespace->root;
1124 q->mnt_namespace = new_ns;
1126 if (p == fs->rootmnt) {
1128 fs->rootmnt = mntget(q);
1130 if (p == fs->pwdmnt) {
1132 fs->pwdmnt = mntget(q);
1134 if (p == fs->altrootmnt) {
1136 fs->altrootmnt = mntget(q);
1139 p = next_mnt(p, namespace->root);
1140 q = next_mnt(q, new_ns->root);
1142 up_write(&tsk->namespace->sem);
1144 tsk->namespace = new_ns;
1153 put_namespace(namespace);
1157 put_namespace(namespace);
1161 asmlinkage long sys_mount(char __user * dev_name, char __user * dir_name,
1162 char __user * type, unsigned long flags,
1166 unsigned long data_page;
1167 unsigned long type_page;
1168 unsigned long dev_page;
1171 retval = copy_mount_options (type, &type_page);
1175 dir_page = getname(dir_name);
1176 retval = PTR_ERR(dir_page);
1177 if (IS_ERR(dir_page))
1180 retval = copy_mount_options (dev_name, &dev_page);
1184 retval = copy_mount_options (data, &data_page);
1189 retval = do_mount((char*)dev_page, dir_page, (char*)type_page,
1190 flags, (void*)data_page);
1192 free_page(data_page);
1195 free_page(dev_page);
1199 free_page(type_page);
1204 * Replace the fs->{rootmnt,root} with {mnt,dentry}. Put the old values.
1205 * It can block. Requires the big lock held.
1207 void set_fs_root(struct fs_struct *fs, struct vfsmount *mnt,
1208 struct dentry *dentry)
1210 struct dentry *old_root;
1211 struct vfsmount *old_rootmnt;
1212 write_lock(&fs->lock);
1213 old_root = fs->root;
1214 old_rootmnt = fs->rootmnt;
1215 fs->rootmnt = mntget(mnt);
1216 fs->root = dget(dentry);
1217 write_unlock(&fs->lock);
1220 mntput(old_rootmnt);
1224 EXPORT_SYMBOL(set_fs_root);
1227 * Replace the fs->{pwdmnt,pwd} with {mnt,dentry}. Put the old values.
1228 * It can block. Requires the big lock held.
1230 void set_fs_pwd(struct fs_struct *fs, struct vfsmount *mnt,
1231 struct dentry *dentry)
1233 struct dentry *old_pwd;
1234 struct vfsmount *old_pwdmnt;
1236 write_lock(&fs->lock);
1238 old_pwdmnt = fs->pwdmnt;
1239 fs->pwdmnt = mntget(mnt);
1240 fs->pwd = dget(dentry);
1241 write_unlock(&fs->lock);
1249 EXPORT_SYMBOL(set_fs_pwd);
1251 static void chroot_fs_refs(struct nameidata *old_nd, struct nameidata *new_nd)
1253 struct task_struct *g, *p;
1254 struct fs_struct *fs;
1256 read_lock(&tasklist_lock);
1257 do_each_thread(g, p) {
1261 atomic_inc(&fs->count);
1263 if (fs->root==old_nd->dentry&&fs->rootmnt==old_nd->mnt)
1264 set_fs_root(fs, new_nd->mnt, new_nd->dentry);
1265 if (fs->pwd==old_nd->dentry&&fs->pwdmnt==old_nd->mnt)
1266 set_fs_pwd(fs, new_nd->mnt, new_nd->dentry);
1270 } while_each_thread(g, p);
1271 read_unlock(&tasklist_lock);
1275 * Moves the current root to put_root, and sets root/cwd of all processes
1276 * which had them on the old root to new_root.
1279 * - we don't move root/cwd if they are not at the root (reason: if something
1280 * cared enough to change them, it's probably wrong to force them elsewhere)
1281 * - it's okay to pick a root that isn't the root of a file system, e.g.
1282 * /nfs/my_root where /nfs is the mount point. It must be a mountpoint,
1283 * though, so you may need to say mount --bind /nfs/my_root /nfs/my_root
1287 asmlinkage long sys_pivot_root(const char __user *new_root, const char __user *put_old)
1289 struct vfsmount *tmp;
1290 struct nameidata new_nd, old_nd, parent_nd, root_parent, user_nd;
1293 if (!capable(CAP_SYS_ADMIN))
1298 error = __user_walk(new_root, LOOKUP_FOLLOW|LOOKUP_DIRECTORY, &new_nd);
1302 if (!check_mnt(new_nd.mnt))
1305 error = __user_walk(put_old, LOOKUP_FOLLOW|LOOKUP_DIRECTORY, &old_nd);
1309 error = security_sb_pivotroot(&old_nd, &new_nd);
1311 path_release(&old_nd);
1315 read_lock(¤t->fs->lock);
1316 user_nd.mnt = mntget(current->fs->rootmnt);
1317 user_nd.dentry = dget(current->fs->root);
1318 read_unlock(¤t->fs->lock);
1319 down_write(¤t->namespace->sem);
1320 down(&old_nd.dentry->d_inode->i_sem);
1322 if (!check_mnt(user_nd.mnt))
1325 if (IS_DEADDIR(new_nd.dentry->d_inode))
1327 if (d_unhashed(new_nd.dentry) && !IS_ROOT(new_nd.dentry))
1329 if (d_unhashed(old_nd.dentry) && !IS_ROOT(old_nd.dentry))
1332 if (new_nd.mnt == user_nd.mnt || old_nd.mnt == user_nd.mnt)
1333 goto out2; /* loop */
1335 if (user_nd.mnt->mnt_root != user_nd.dentry)
1337 if (new_nd.mnt->mnt_root != new_nd.dentry)
1338 goto out2; /* not a mountpoint */
1339 tmp = old_nd.mnt; /* make sure we can reach put_old from new_root */
1340 spin_lock(&vfsmount_lock);
1341 if (tmp != new_nd.mnt) {
1343 if (tmp->mnt_parent == tmp)
1345 if (tmp->mnt_parent == new_nd.mnt)
1347 tmp = tmp->mnt_parent;
1349 if (!is_subdir(tmp->mnt_mountpoint, new_nd.dentry))
1351 } else if (!is_subdir(old_nd.dentry, new_nd.dentry))
1353 detach_mnt(new_nd.mnt, &parent_nd);
1354 detach_mnt(user_nd.mnt, &root_parent);
1355 attach_mnt(user_nd.mnt, &old_nd);
1356 attach_mnt(new_nd.mnt, &root_parent);
1357 spin_unlock(&vfsmount_lock);
1358 chroot_fs_refs(&user_nd, &new_nd);
1359 security_sb_post_pivotroot(&user_nd, &new_nd);
1361 path_release(&root_parent);
1362 path_release(&parent_nd);
1364 up(&old_nd.dentry->d_inode->i_sem);
1365 up_write(¤t->namespace->sem);
1366 path_release(&user_nd);
1367 path_release(&old_nd);
1369 path_release(&new_nd);
1374 spin_unlock(&vfsmount_lock);
1378 static void __init init_mount_tree(void)
1380 struct vfsmount *mnt;
1381 struct namespace *namespace;
1382 struct task_struct *g, *p;
1384 mnt = do_kern_mount("rootfs", 0, "rootfs", NULL);
1386 panic("Can't create rootfs");
1387 namespace = kmalloc(sizeof(*namespace), GFP_KERNEL);
1389 panic("Can't allocate initial namespace");
1390 atomic_set(&namespace->count, 1);
1391 INIT_LIST_HEAD(&namespace->list);
1392 init_rwsem(&namespace->sem);
1393 list_add(&mnt->mnt_list, &namespace->list);
1394 namespace->root = mnt;
1395 mnt->mnt_namespace = namespace;
1397 init_task.namespace = namespace;
1398 read_lock(&tasklist_lock);
1399 do_each_thread(g, p) {
1400 get_namespace(namespace);
1401 p->namespace = namespace;
1402 } while_each_thread(g, p);
1403 read_unlock(&tasklist_lock);
1405 set_fs_pwd(current->fs, namespace->root, namespace->root->mnt_root);
1406 set_fs_root(current->fs, namespace->root, namespace->root->mnt_root);
1409 void __init mnt_init(unsigned long mempages)
1411 struct list_head *d;
1412 unsigned long order;
1413 unsigned int nr_hash;
1416 mnt_cache = kmem_cache_create("mnt_cache", sizeof(struct vfsmount),
1417 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
1420 mount_hashtable = (struct list_head *)
1421 __get_free_pages(GFP_ATOMIC, order);
1423 if (!mount_hashtable)
1424 panic("Failed to allocate mount hash table\n");
1427 * Find the power-of-two list-heads that can fit into the allocation..
1428 * We don't guarantee that "sizeof(struct list_head)" is necessarily
1431 nr_hash = (1UL << order) * PAGE_SIZE / sizeof(struct list_head);
1435 } while ((nr_hash >> hash_bits) != 0);
1439 * Re-calculate the actual number of entries and the mask
1440 * from the number of bits we can fit.
1442 nr_hash = 1UL << hash_bits;
1443 hash_mask = nr_hash-1;
1445 printk("Mount-cache hash table entries: %d (order: %ld, %ld bytes)\n",
1446 nr_hash, order, (PAGE_SIZE << order));
1448 /* And initialize the newly allocated array */
1449 d = mount_hashtable;
1461 void __put_namespace(struct namespace *namespace)
1463 struct vfsmount *mnt;
1465 down_write(&namespace->sem);
1466 spin_lock(&vfsmount_lock);
1468 list_for_each_entry(mnt, &namespace->list, mnt_list) {
1469 mnt->mnt_namespace = NULL;
1472 umount_tree(namespace->root);
1473 spin_unlock(&vfsmount_lock);
1474 up_write(&namespace->sem);