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/vs_base.h>
26 #include <linux/vserver/namespace.h>
28 #include <asm/uaccess.h>
29 #include <asm/unistd.h>
31 extern int __init init_rootfs(void);
34 extern int __init sysfs_init(void);
36 static inline int sysfs_init(void)
42 /* spinlock for vfsmount related operations, inplace of dcache_lock */
43 spinlock_t vfsmount_lock __cacheline_aligned_in_smp = SPIN_LOCK_UNLOCKED;
45 static struct list_head *mount_hashtable;
46 static int hash_mask, hash_bits;
47 static kmem_cache_t *mnt_cache;
49 static inline unsigned long hash(struct vfsmount *mnt, struct dentry *dentry)
51 unsigned long tmp = ((unsigned long) mnt / L1_CACHE_BYTES);
52 tmp += ((unsigned long) dentry / L1_CACHE_BYTES);
53 tmp = tmp + (tmp >> hash_bits);
54 return tmp & hash_mask;
57 struct vfsmount *alloc_vfsmnt(const char *name)
59 struct vfsmount *mnt = kmem_cache_alloc(mnt_cache, GFP_KERNEL);
61 memset(mnt, 0, sizeof(struct vfsmount));
62 atomic_set(&mnt->mnt_count,1);
63 INIT_LIST_HEAD(&mnt->mnt_hash);
64 INIT_LIST_HEAD(&mnt->mnt_child);
65 INIT_LIST_HEAD(&mnt->mnt_mounts);
66 INIT_LIST_HEAD(&mnt->mnt_list);
67 INIT_LIST_HEAD(&mnt->mnt_fslink);
69 int size = strlen(name)+1;
70 char *newname = kmalloc(size, GFP_KERNEL);
72 memcpy(newname, name, size);
73 mnt->mnt_devname = newname;
80 void free_vfsmnt(struct vfsmount *mnt)
82 kfree(mnt->mnt_devname);
83 kmem_cache_free(mnt_cache, mnt);
87 * Now, lookup_mnt increments the ref count before returning
88 * the vfsmount struct.
90 struct vfsmount *lookup_mnt(struct vfsmount *mnt, struct dentry *dentry)
92 struct list_head * head = mount_hashtable + hash(mnt, dentry);
93 struct list_head * tmp = head;
94 struct vfsmount *p, *found = NULL;
96 spin_lock(&vfsmount_lock);
102 p = list_entry(tmp, struct vfsmount, mnt_hash);
103 if (p->mnt_parent == mnt && p->mnt_mountpoint == dentry) {
108 spin_unlock(&vfsmount_lock);
112 static inline int check_mnt(struct vfsmount *mnt)
114 return mnt->mnt_namespace == current->namespace;
117 static void detach_mnt(struct vfsmount *mnt, struct nameidata *old_nd)
119 old_nd->dentry = mnt->mnt_mountpoint;
120 old_nd->mnt = mnt->mnt_parent;
121 mnt->mnt_parent = mnt;
122 mnt->mnt_mountpoint = mnt->mnt_root;
123 list_del_init(&mnt->mnt_child);
124 list_del_init(&mnt->mnt_hash);
125 old_nd->dentry->d_mounted--;
128 static void attach_mnt(struct vfsmount *mnt, struct nameidata *nd)
130 mnt->mnt_parent = mntget(nd->mnt);
131 mnt->mnt_mountpoint = dget(nd->dentry);
132 list_add(&mnt->mnt_hash, mount_hashtable+hash(nd->mnt, nd->dentry));
133 list_add_tail(&mnt->mnt_child, &nd->mnt->mnt_mounts);
134 nd->dentry->d_mounted++;
137 static struct vfsmount *next_mnt(struct vfsmount *p, struct vfsmount *root)
139 struct list_head *next = p->mnt_mounts.next;
140 if (next == &p->mnt_mounts) {
144 next = p->mnt_child.next;
145 if (next != &p->mnt_parent->mnt_mounts)
150 return list_entry(next, struct vfsmount, mnt_child);
153 static struct vfsmount *
154 clone_mnt(struct vfsmount *old, struct dentry *root)
156 struct super_block *sb = old->mnt_sb;
157 struct vfsmount *mnt = alloc_vfsmnt(old->mnt_devname);
160 mnt->mnt_flags = old->mnt_flags;
161 atomic_inc(&sb->s_active);
163 mnt->mnt_root = dget(root);
164 mnt->mnt_mountpoint = mnt->mnt_root;
165 mnt->mnt_parent = mnt;
166 mnt->mnt_namespace = old->mnt_namespace;
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))
249 if (!vx_check_vfsmount(current->vx_info, mnt))
252 mangle(m, mnt->mnt_devname ? mnt->mnt_devname : "none");
254 seq_path(m, mnt, mnt->mnt_root, " \t\n\\");
256 mangle(m, mnt->mnt_sb->s_type->name);
258 for (p = fs_info; (p->s_flag | p->mnt_flag) ; p++) {
259 if ((s_flags & p->s_flag) || (mnt_flags & p->mnt_flag)) {
261 seq_puts(m, p->set_str);
264 seq_puts(m, p->unset_str);
267 if (mnt->mnt_sb->s_op->show_options)
268 err = mnt->mnt_sb->s_op->show_options(m, mnt);
269 seq_puts(m, " 0 0\n");
273 struct seq_operations mounts_op = {
281 * may_umount_tree - check if a mount tree is busy
282 * @mnt: root of mount tree
284 * This is called to check if a tree of mounts has any
285 * open files, pwds, chroots or sub mounts that are
288 int may_umount_tree(struct vfsmount *mnt)
290 struct list_head *next;
291 struct vfsmount *this_parent = mnt;
295 spin_lock(&vfsmount_lock);
296 actual_refs = atomic_read(&mnt->mnt_count);
299 next = this_parent->mnt_mounts.next;
301 while (next != &this_parent->mnt_mounts) {
302 struct vfsmount *p = list_entry(next, struct vfsmount, mnt_child);
306 actual_refs += atomic_read(&p->mnt_count);
309 if (!list_empty(&p->mnt_mounts)) {
315 if (this_parent != mnt) {
316 next = this_parent->mnt_child.next;
317 this_parent = this_parent->mnt_parent;
320 spin_unlock(&vfsmount_lock);
322 if (actual_refs > minimum_refs)
328 EXPORT_SYMBOL(may_umount_tree);
331 * may_umount - check if a mount point is busy
332 * @mnt: root of mount
334 * This is called to check if a mount point has any
335 * open files, pwds, chroots or sub mounts. If the
336 * mount has sub mounts this will return busy
337 * regardless of whether the sub mounts are busy.
339 * Doesn't take quota and stuff into account. IOW, in some cases it will
340 * give false negatives. The main reason why it's here is that we need
341 * a non-destructive way to look for easily umountable filesystems.
343 int may_umount(struct vfsmount *mnt)
345 if (atomic_read(&mnt->mnt_count) > 2)
350 EXPORT_SYMBOL(may_umount);
352 static inline void __umount_tree(struct vfsmount *mnt, struct list_head *kill)
354 while (!list_empty(kill)) {
355 mnt = list_entry(kill->next, struct vfsmount, mnt_list);
356 list_del_init(&mnt->mnt_list);
357 list_del_init(&mnt->mnt_fslink);
358 if (mnt->mnt_parent == mnt) {
359 spin_unlock(&vfsmount_lock);
361 struct nameidata old_nd;
362 detach_mnt(mnt, &old_nd);
363 spin_unlock(&vfsmount_lock);
364 path_release(&old_nd);
367 spin_lock(&vfsmount_lock);
371 void umount_tree(struct vfsmount *mnt)
376 for (p = mnt; p; p = next_mnt(p, mnt)) {
377 list_del(&p->mnt_list);
378 list_add(&p->mnt_list, &kill);
380 __umount_tree(mnt, &kill);
383 void umount_unused(struct vfsmount *mnt, struct fs_struct *fs)
388 for (p = mnt; p; p = next_mnt(p, mnt)) {
389 if (p == fs->rootmnt || p == fs->pwdmnt)
391 list_del(&p->mnt_list);
392 list_add(&p->mnt_list, &kill);
394 __umount_tree(mnt, &kill);
397 static int do_umount(struct vfsmount *mnt, int flags)
399 struct super_block * sb = mnt->mnt_sb;
402 retval = security_sb_umount(mnt, flags);
407 * Allow userspace to request a mountpoint be expired rather than
408 * unmounting unconditionally. Unmount only happens if:
409 * (1) the mark is already set (the mark is cleared by mntput())
410 * (2) the usage count == 1 [parent vfsmount] + 1 [sys_umount]
412 if (flags & MNT_EXPIRE) {
413 if (mnt == current->fs->rootmnt ||
414 flags & (MNT_FORCE | MNT_DETACH))
417 if (atomic_read(&mnt->mnt_count) != 2)
420 if (!xchg(&mnt->mnt_expiry_mark, 1))
425 * If we may have to abort operations to get out of this
426 * mount, and they will themselves hold resources we must
427 * allow the fs to do things. In the Unix tradition of
428 * 'Gee thats tricky lets do it in userspace' the umount_begin
429 * might fail to complete on the first run through as other tasks
430 * must return, and the like. Thats for the mount program to worry
431 * about for the moment.
435 if( (flags&MNT_FORCE) && sb->s_op->umount_begin)
436 sb->s_op->umount_begin(sb);
440 * No sense to grab the lock for this test, but test itself looks
441 * somewhat bogus. Suggestions for better replacement?
442 * Ho-hum... In principle, we might treat that as umount + switch
443 * to rootfs. GC would eventually take care of the old vfsmount.
444 * Actually it makes sense, especially if rootfs would contain a
445 * /reboot - static binary that would close all descriptors and
446 * call reboot(9). Then init(8) could umount root and exec /reboot.
448 if (mnt == current->fs->rootmnt && !(flags & MNT_DETACH)) {
450 * Special case for "unmounting" root ...
451 * we just try to remount it readonly.
453 down_write(&sb->s_umount);
454 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);
1260 * Replace the fs->{pwdmnt,pwd} with {mnt,dentry}. Put the old values.
1261 * It can block. Requires the big lock held.
1263 void set_fs_pwd(struct fs_struct *fs, struct vfsmount *mnt,
1264 struct dentry *dentry)
1266 struct dentry *old_pwd;
1267 struct vfsmount *old_pwdmnt;
1269 write_lock(&fs->lock);
1271 old_pwdmnt = fs->pwdmnt;
1272 fs->pwdmnt = mntget(mnt);
1273 fs->pwd = dget(dentry);
1274 write_unlock(&fs->lock);
1282 static void chroot_fs_refs(struct nameidata *old_nd, struct nameidata *new_nd)
1284 struct task_struct *g, *p;
1285 struct fs_struct *fs;
1287 read_lock(&tasklist_lock);
1288 do_each_thread(g, p) {
1292 atomic_inc(&fs->count);
1294 if (fs->root==old_nd->dentry&&fs->rootmnt==old_nd->mnt)
1295 set_fs_root(fs, new_nd->mnt, new_nd->dentry);
1296 if (fs->pwd==old_nd->dentry&&fs->pwdmnt==old_nd->mnt)
1297 set_fs_pwd(fs, new_nd->mnt, new_nd->dentry);
1301 } while_each_thread(g, p);
1302 read_unlock(&tasklist_lock);
1306 * Moves the current root to put_root, and sets root/cwd of all processes
1307 * which had them on the old root to new_root.
1310 * - we don't move root/cwd if they are not at the root (reason: if something
1311 * cared enough to change them, it's probably wrong to force them elsewhere)
1312 * - it's okay to pick a root that isn't the root of a file system, e.g.
1313 * /nfs/my_root where /nfs is the mount point. It must be a mountpoint,
1314 * though, so you may need to say mount --bind /nfs/my_root /nfs/my_root
1318 asmlinkage long sys_pivot_root(const char __user *new_root, const char __user *put_old)
1320 struct vfsmount *tmp;
1321 struct nameidata new_nd, old_nd, parent_nd, root_parent, user_nd;
1324 if (!capable(CAP_SYS_ADMIN))
1329 error = __user_walk(new_root, LOOKUP_FOLLOW|LOOKUP_DIRECTORY, &new_nd);
1333 if (!check_mnt(new_nd.mnt))
1336 error = __user_walk(put_old, LOOKUP_FOLLOW|LOOKUP_DIRECTORY, &old_nd);
1340 error = security_sb_pivotroot(&old_nd, &new_nd);
1342 path_release(&old_nd);
1346 read_lock(¤t->fs->lock);
1347 user_nd.mnt = mntget(current->fs->rootmnt);
1348 user_nd.dentry = dget(current->fs->root);
1349 read_unlock(¤t->fs->lock);
1350 down_write(¤t->namespace->sem);
1351 down(&old_nd.dentry->d_inode->i_sem);
1353 if (!check_mnt(user_nd.mnt))
1356 if (IS_DEADDIR(new_nd.dentry->d_inode))
1358 if (d_unhashed(new_nd.dentry) && !IS_ROOT(new_nd.dentry))
1360 if (d_unhashed(old_nd.dentry) && !IS_ROOT(old_nd.dentry))
1363 if (new_nd.mnt == user_nd.mnt || old_nd.mnt == user_nd.mnt)
1364 goto out2; /* loop */
1366 if (user_nd.mnt->mnt_root != user_nd.dentry)
1368 if (new_nd.mnt->mnt_root != new_nd.dentry)
1369 goto out2; /* not a mountpoint */
1370 tmp = old_nd.mnt; /* make sure we can reach put_old from new_root */
1371 spin_lock(&vfsmount_lock);
1372 if (tmp != new_nd.mnt) {
1374 if (tmp->mnt_parent == tmp)
1376 if (tmp->mnt_parent == new_nd.mnt)
1378 tmp = tmp->mnt_parent;
1380 if (!is_subdir(tmp->mnt_mountpoint, new_nd.dentry))
1382 } else if (!is_subdir(old_nd.dentry, new_nd.dentry))
1384 detach_mnt(new_nd.mnt, &parent_nd);
1385 detach_mnt(user_nd.mnt, &root_parent);
1386 attach_mnt(user_nd.mnt, &old_nd);
1387 attach_mnt(new_nd.mnt, &root_parent);
1388 spin_unlock(&vfsmount_lock);
1389 chroot_fs_refs(&user_nd, &new_nd);
1390 security_sb_post_pivotroot(&user_nd, &new_nd);
1392 path_release(&root_parent);
1393 path_release(&parent_nd);
1395 up(&old_nd.dentry->d_inode->i_sem);
1396 up_write(¤t->namespace->sem);
1397 path_release(&user_nd);
1398 path_release(&old_nd);
1400 path_release(&new_nd);
1405 spin_unlock(&vfsmount_lock);
1409 static void __init init_mount_tree(void)
1411 struct vfsmount *mnt;
1412 struct namespace *namespace;
1413 struct task_struct *g, *p;
1415 mnt = do_kern_mount("rootfs", 0, "rootfs", NULL);
1417 panic("Can't create rootfs");
1418 namespace = kmalloc(sizeof(*namespace), GFP_KERNEL);
1420 panic("Can't allocate initial namespace");
1421 atomic_set(&namespace->count, 1);
1422 INIT_LIST_HEAD(&namespace->list);
1423 init_rwsem(&namespace->sem);
1424 list_add(&mnt->mnt_list, &namespace->list);
1425 namespace->root = mnt;
1426 mnt->mnt_namespace = namespace;
1428 init_task.namespace = namespace;
1429 read_lock(&tasklist_lock);
1430 do_each_thread(g, p) {
1431 get_namespace(namespace);
1432 p->namespace = namespace;
1433 } while_each_thread(g, p);
1434 read_unlock(&tasklist_lock);
1436 set_fs_pwd(current->fs, namespace->root, namespace->root->mnt_root);
1437 set_fs_root(current->fs, namespace->root, namespace->root->mnt_root);
1440 void __init mnt_init(unsigned long mempages)
1442 struct list_head *d;
1443 unsigned long order;
1444 unsigned int nr_hash;
1447 mnt_cache = kmem_cache_create("mnt_cache", sizeof(struct vfsmount),
1448 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
1451 mount_hashtable = (struct list_head *)
1452 __get_free_pages(GFP_ATOMIC, order);
1454 if (!mount_hashtable)
1455 panic("Failed to allocate mount hash table\n");
1458 * Find the power-of-two list-heads that can fit into the allocation..
1459 * We don't guarantee that "sizeof(struct list_head)" is necessarily
1462 nr_hash = (1UL << order) * PAGE_SIZE / sizeof(struct list_head);
1466 } while ((nr_hash >> hash_bits) != 0);
1470 * Re-calculate the actual number of entries and the mask
1471 * from the number of bits we can fit.
1473 nr_hash = 1UL << hash_bits;
1474 hash_mask = nr_hash-1;
1476 printk("Mount-cache hash table entries: %d (order: %ld, %ld bytes)\n",
1477 nr_hash, order, (PAGE_SIZE << order));
1479 /* And initialize the newly allocated array */
1480 d = mount_hashtable;
1492 void __put_namespace(struct namespace *namespace)
1494 struct vfsmount *mnt;
1496 down_write(&namespace->sem);
1497 spin_lock(&vfsmount_lock);
1499 list_for_each_entry(mnt, &namespace->list, mnt_list) {
1500 mnt->mnt_namespace = NULL;
1503 umount_tree(namespace->root);
1504 spin_unlock(&vfsmount_lock);
1505 up_write(&namespace->sem);