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 <asm/uaccess.h>
26 #include <asm/unistd.h>
28 extern int __init init_rootfs(void);
30 #define CL_EXPIRE 0x01
33 extern int __init sysfs_init(void);
35 static inline int sysfs_init(void)
41 /* spinlock for vfsmount related operations, inplace of dcache_lock */
42 __cacheline_aligned_in_smp DEFINE_SPINLOCK(vfsmount_lock);
46 static struct list_head *mount_hashtable;
47 static int hash_mask __read_mostly, hash_bits __read_mostly;
48 static kmem_cache_t *mnt_cache;
50 static inline unsigned long hash(struct vfsmount *mnt, struct dentry *dentry)
52 unsigned long tmp = ((unsigned long)mnt / L1_CACHE_BYTES);
53 tmp += ((unsigned long)dentry / L1_CACHE_BYTES);
54 tmp = tmp + (tmp >> hash_bits);
55 return tmp & hash_mask;
58 struct vfsmount *alloc_vfsmnt(const char *name)
60 struct vfsmount *mnt = kmem_cache_alloc(mnt_cache, GFP_KERNEL);
62 memset(mnt, 0, sizeof(struct vfsmount));
63 atomic_set(&mnt->mnt_count, 1);
64 INIT_LIST_HEAD(&mnt->mnt_hash);
65 INIT_LIST_HEAD(&mnt->mnt_child);
66 INIT_LIST_HEAD(&mnt->mnt_mounts);
67 INIT_LIST_HEAD(&mnt->mnt_list);
68 INIT_LIST_HEAD(&mnt->mnt_expire);
70 int size = strlen(name) + 1;
71 char *newname = kmalloc(size, GFP_KERNEL);
73 memcpy(newname, name, size);
74 mnt->mnt_devname = newname;
81 void free_vfsmnt(struct vfsmount *mnt)
83 kfree(mnt->mnt_devname);
84 kmem_cache_free(mnt_cache, mnt);
88 * Now, lookup_mnt increments the ref count before returning
89 * the vfsmount struct.
91 struct vfsmount *lookup_mnt(struct vfsmount *mnt, struct dentry *dentry)
93 struct list_head *head = mount_hashtable + hash(mnt, dentry);
94 struct list_head *tmp = head;
95 struct vfsmount *p, *found = NULL;
97 spin_lock(&vfsmount_lock);
103 p = list_entry(tmp, struct vfsmount, mnt_hash);
104 if (p->mnt_parent == mnt && p->mnt_mountpoint == dentry) {
109 spin_unlock(&vfsmount_lock);
113 static inline int check_mnt(struct vfsmount *mnt)
115 return mnt->mnt_namespace == current->namespace;
118 static void touch_namespace(struct namespace *ns)
122 wake_up_interruptible(&ns->poll);
126 static void __touch_namespace(struct namespace *ns)
128 if (ns && ns->event != event) {
130 wake_up_interruptible(&ns->poll);
134 static void detach_mnt(struct vfsmount *mnt, struct nameidata *old_nd)
136 old_nd->dentry = mnt->mnt_mountpoint;
137 old_nd->mnt = mnt->mnt_parent;
138 mnt->mnt_parent = mnt;
139 mnt->mnt_mountpoint = mnt->mnt_root;
140 list_del_init(&mnt->mnt_child);
141 list_del_init(&mnt->mnt_hash);
142 old_nd->dentry->d_mounted--;
145 static void attach_mnt(struct vfsmount *mnt, struct nameidata *nd)
147 mnt->mnt_parent = mntget(nd->mnt);
148 mnt->mnt_mountpoint = dget(nd->dentry);
149 list_add(&mnt->mnt_hash, mount_hashtable + hash(nd->mnt, nd->dentry));
150 list_add_tail(&mnt->mnt_child, &nd->mnt->mnt_mounts);
151 nd->dentry->d_mounted++;
154 static struct vfsmount *next_mnt(struct vfsmount *p, struct vfsmount *root)
156 struct list_head *next = p->mnt_mounts.next;
157 if (next == &p->mnt_mounts) {
161 next = p->mnt_child.next;
162 if (next != &p->mnt_parent->mnt_mounts)
167 return list_entry(next, struct vfsmount, mnt_child);
170 static struct vfsmount *clone_mnt(struct vfsmount *old, struct dentry *root,
173 struct super_block *sb = old->mnt_sb;
174 struct vfsmount *mnt = alloc_vfsmnt(old->mnt_devname);
177 mnt->mnt_flags = old->mnt_flags;
178 atomic_inc(&sb->s_active);
180 mnt->mnt_root = dget(root);
181 mnt->mnt_mountpoint = mnt->mnt_root;
182 mnt->mnt_parent = mnt;
183 mnt->mnt_namespace = current->namespace;
185 /* stick the duplicate mount on the same expiry list
186 * as the original if that was on one */
187 if (flag & CL_EXPIRE) {
188 spin_lock(&vfsmount_lock);
189 if (!list_empty(&old->mnt_expire))
190 list_add(&mnt->mnt_expire, &old->mnt_expire);
191 spin_unlock(&vfsmount_lock);
197 static inline void __mntput(struct vfsmount *mnt)
199 struct super_block *sb = mnt->mnt_sb;
202 deactivate_super(sb);
205 void mntput_no_expire(struct vfsmount *mnt)
208 if (atomic_dec_and_lock(&mnt->mnt_count, &vfsmount_lock)) {
209 if (likely(!mnt->mnt_pinned)) {
210 spin_unlock(&vfsmount_lock);
214 atomic_add(mnt->mnt_pinned + 1, &mnt->mnt_count);
216 spin_unlock(&vfsmount_lock);
217 acct_auto_close_mnt(mnt);
218 security_sb_umount_close(mnt);
223 EXPORT_SYMBOL(mntput_no_expire);
225 void mnt_pin(struct vfsmount *mnt)
227 spin_lock(&vfsmount_lock);
229 spin_unlock(&vfsmount_lock);
232 EXPORT_SYMBOL(mnt_pin);
234 void mnt_unpin(struct vfsmount *mnt)
236 spin_lock(&vfsmount_lock);
237 if (mnt->mnt_pinned) {
238 atomic_inc(&mnt->mnt_count);
241 spin_unlock(&vfsmount_lock);
244 EXPORT_SYMBOL(mnt_unpin);
247 static void *m_start(struct seq_file *m, loff_t *pos)
249 struct namespace *n = m->private;
254 list_for_each(p, &n->list)
256 return list_entry(p, struct vfsmount, mnt_list);
260 static void *m_next(struct seq_file *m, void *v, loff_t *pos)
262 struct namespace *n = m->private;
263 struct list_head *p = ((struct vfsmount *)v)->mnt_list.next;
265 return p == &n->list ? NULL : list_entry(p, struct vfsmount, mnt_list);
268 static void m_stop(struct seq_file *m, void *v)
270 struct namespace *n = m->private;
274 static inline void mangle(struct seq_file *m, const char *s)
276 seq_escape(m, s, " \t\n\\");
279 static int show_vfsmnt(struct seq_file *m, void *v)
281 struct vfsmount *mnt = v;
283 static struct proc_fs_info {
287 { MS_SYNCHRONOUS, ",sync" },
288 { MS_DIRSYNC, ",dirsync" },
289 { MS_MANDLOCK, ",mand" },
290 { MS_NOATIME, ",noatime" },
291 { MS_NODIRATIME, ",nodiratime" },
294 static struct proc_fs_info mnt_info[] = {
295 { MNT_NOSUID, ",nosuid" },
296 { MNT_NODEV, ",nodev" },
297 { MNT_NOEXEC, ",noexec" },
300 struct proc_fs_info *fs_infop;
302 mangle(m, mnt->mnt_devname ? mnt->mnt_devname : "none");
304 seq_path(m, mnt, mnt->mnt_root, " \t\n\\");
306 mangle(m, mnt->mnt_sb->s_type->name);
307 seq_puts(m, mnt->mnt_sb->s_flags & MS_RDONLY ? " ro" : " rw");
308 for (fs_infop = fs_info; fs_infop->flag; fs_infop++) {
309 if (mnt->mnt_sb->s_flags & fs_infop->flag)
310 seq_puts(m, fs_infop->str);
312 for (fs_infop = mnt_info; fs_infop->flag; fs_infop++) {
313 if (mnt->mnt_flags & fs_infop->flag)
314 seq_puts(m, fs_infop->str);
316 if (mnt->mnt_sb->s_op->show_options)
317 err = mnt->mnt_sb->s_op->show_options(m, mnt);
318 seq_puts(m, " 0 0\n");
322 struct seq_operations mounts_op = {
330 * may_umount_tree - check if a mount tree is busy
331 * @mnt: root of mount tree
333 * This is called to check if a tree of mounts has any
334 * open files, pwds, chroots or sub mounts that are
337 int may_umount_tree(struct vfsmount *mnt)
340 int minimum_refs = 0;
343 spin_lock(&vfsmount_lock);
344 for (p = mnt; p; p = next_mnt(p, mnt)) {
345 actual_refs += atomic_read(&p->mnt_count);
348 spin_unlock(&vfsmount_lock);
350 if (actual_refs > minimum_refs)
356 EXPORT_SYMBOL(may_umount_tree);
359 * may_umount - check if a mount point is busy
360 * @mnt: root of mount
362 * This is called to check if a mount point has any
363 * open files, pwds, chroots or sub mounts. If the
364 * mount has sub mounts this will return busy
365 * regardless of whether the sub mounts are busy.
367 * Doesn't take quota and stuff into account. IOW, in some cases it will
368 * give false negatives. The main reason why it's here is that we need
369 * a non-destructive way to look for easily umountable filesystems.
371 int may_umount(struct vfsmount *mnt)
373 if (atomic_read(&mnt->mnt_count) > 2)
378 EXPORT_SYMBOL(may_umount);
380 static void release_mounts(struct list_head *head)
382 struct vfsmount *mnt;
383 while(!list_empty(head)) {
384 mnt = list_entry(head->next, struct vfsmount, mnt_hash);
385 list_del_init(&mnt->mnt_hash);
386 if (mnt->mnt_parent != mnt) {
387 struct dentry *dentry;
389 spin_lock(&vfsmount_lock);
390 dentry = mnt->mnt_mountpoint;
392 mnt->mnt_mountpoint = mnt->mnt_root;
393 mnt->mnt_parent = mnt;
394 spin_unlock(&vfsmount_lock);
402 static void umount_tree(struct vfsmount *mnt, struct list_head *kill)
406 for (p = mnt; p; p = next_mnt(p, mnt)) {
407 list_del(&p->mnt_hash);
408 list_add(&p->mnt_hash, kill);
411 list_for_each_entry(p, kill, mnt_hash) {
412 list_del_init(&p->mnt_expire);
413 list_del_init(&p->mnt_list);
414 __touch_namespace(p->mnt_namespace);
415 p->mnt_namespace = NULL;
416 list_del_init(&p->mnt_child);
417 if (p->mnt_parent != p)
418 mnt->mnt_mountpoint->d_mounted--;
422 static int do_umount(struct vfsmount *mnt, int flags)
424 struct super_block *sb = mnt->mnt_sb;
426 LIST_HEAD(umount_list);
428 retval = security_sb_umount(mnt, flags);
433 * Allow userspace to request a mountpoint be expired rather than
434 * unmounting unconditionally. Unmount only happens if:
435 * (1) the mark is already set (the mark is cleared by mntput())
436 * (2) the usage count == 1 [parent vfsmount] + 1 [sys_umount]
438 if (flags & MNT_EXPIRE) {
439 if (mnt == current->fs->rootmnt ||
440 flags & (MNT_FORCE | MNT_DETACH))
443 if (atomic_read(&mnt->mnt_count) != 2)
446 if (!xchg(&mnt->mnt_expiry_mark, 1))
451 * If we may have to abort operations to get out of this
452 * mount, and they will themselves hold resources we must
453 * allow the fs to do things. In the Unix tradition of
454 * 'Gee thats tricky lets do it in userspace' the umount_begin
455 * might fail to complete on the first run through as other tasks
456 * must return, and the like. Thats for the mount program to worry
457 * about for the moment.
461 if ((flags & MNT_FORCE) && sb->s_op->umount_begin)
462 sb->s_op->umount_begin(sb);
466 * No sense to grab the lock for this test, but test itself looks
467 * somewhat bogus. Suggestions for better replacement?
468 * Ho-hum... In principle, we might treat that as umount + switch
469 * to rootfs. GC would eventually take care of the old vfsmount.
470 * Actually it makes sense, especially if rootfs would contain a
471 * /reboot - static binary that would close all descriptors and
472 * call reboot(9). Then init(8) could umount root and exec /reboot.
474 if (mnt == current->fs->rootmnt && !(flags & MNT_DETACH)) {
476 * Special case for "unmounting" root ...
477 * we just try to remount it readonly.
479 down_write(&sb->s_umount);
480 if (!(sb->s_flags & MS_RDONLY)) {
483 retval = do_remount_sb(sb, MS_RDONLY, NULL, 0);
486 up_write(&sb->s_umount);
490 down_write(¤t->namespace->sem);
491 spin_lock(&vfsmount_lock);
495 if (atomic_read(&mnt->mnt_count) == 2 || flags & MNT_DETACH) {
496 if (!list_empty(&mnt->mnt_list))
497 umount_tree(mnt, &umount_list);
500 spin_unlock(&vfsmount_lock);
502 security_sb_umount_busy(mnt);
503 up_write(¤t->namespace->sem);
504 release_mounts(&umount_list);
509 * Now umount can handle mount points as well as block devices.
510 * This is important for filesystems which use unnamed block devices.
512 * We now support a flag for forced unmount like the other 'big iron'
513 * unixes. Our API is identical to OSF/1 to avoid making a mess of AMD
516 asmlinkage long sys_umount(char __user * name, int flags)
521 retval = __user_walk(name, LOOKUP_FOLLOW, &nd);
525 if (nd.dentry != nd.mnt->mnt_root)
527 if (!check_mnt(nd.mnt))
531 if (!capable(CAP_SYS_ADMIN))
534 retval = do_umount(nd.mnt, flags);
536 path_release_on_umount(&nd);
541 #ifdef __ARCH_WANT_SYS_OLDUMOUNT
544 * The 2.0 compatible umount. No flags.
546 asmlinkage long sys_oldumount(char __user * name)
548 return sys_umount(name, 0);
553 static int mount_is_safe(struct nameidata *nd)
555 if (capable(CAP_SYS_ADMIN))
559 if (S_ISLNK(nd->dentry->d_inode->i_mode))
561 if (nd->dentry->d_inode->i_mode & S_ISVTX) {
562 if (current->uid != nd->dentry->d_inode->i_uid)
565 if (permission(nd->dentry->d_inode, MAY_WRITE, nd))
571 static int lives_below_in_same_fs(struct dentry *d, struct dentry *dentry)
576 if (d == NULL || d == d->d_parent)
582 static struct vfsmount *copy_tree(struct vfsmount *mnt, struct dentry *dentry,
585 struct vfsmount *res, *p, *q, *r, *s;
588 res = q = clone_mnt(mnt, dentry, flag);
591 q->mnt_mountpoint = mnt->mnt_mountpoint;
594 list_for_each_entry(r, &mnt->mnt_mounts, mnt_child) {
595 if (!lives_below_in_same_fs(r->mnt_mountpoint, dentry))
598 for (s = r; s; s = next_mnt(s, r)) {
599 while (p != s->mnt_parent) {
605 nd.dentry = p->mnt_mountpoint;
606 q = clone_mnt(p, p->mnt_root, flag);
609 spin_lock(&vfsmount_lock);
610 list_add_tail(&q->mnt_list, &res->mnt_list);
612 spin_unlock(&vfsmount_lock);
618 LIST_HEAD(umount_list);
619 spin_lock(&vfsmount_lock);
620 umount_tree(res, &umount_list);
621 spin_unlock(&vfsmount_lock);
622 release_mounts(&umount_list);
627 static int graft_tree(struct vfsmount *mnt, struct nameidata *nd)
630 if (mnt->mnt_sb->s_flags & MS_NOUSER)
633 if (S_ISDIR(nd->dentry->d_inode->i_mode) !=
634 S_ISDIR(mnt->mnt_root->d_inode->i_mode))
638 down(&nd->dentry->d_inode->i_sem);
639 if (IS_DEADDIR(nd->dentry->d_inode))
642 err = security_sb_check_sb(mnt, nd);
647 spin_lock(&vfsmount_lock);
648 if (IS_ROOT(nd->dentry) || !d_unhashed(nd->dentry)) {
649 struct list_head head;
652 list_add_tail(&head, &mnt->mnt_list);
653 list_splice(&head, current->namespace->list.prev);
655 touch_namespace(current->namespace);
657 spin_unlock(&vfsmount_lock);
659 up(&nd->dentry->d_inode->i_sem);
661 security_sb_post_addmount(mnt, nd);
668 static int do_loopback(struct nameidata *nd, char *old_name, int recurse)
670 struct nameidata old_nd;
671 struct vfsmount *mnt = NULL;
672 int err = mount_is_safe(nd);
675 if (!old_name || !*old_name)
677 err = path_lookup(old_name, LOOKUP_FOLLOW, &old_nd);
681 down_write(¤t->namespace->sem);
683 if (!check_mnt(nd->mnt) || !check_mnt(old_nd.mnt))
688 mnt = copy_tree(old_nd.mnt, old_nd.dentry, 0);
690 mnt = clone_mnt(old_nd.mnt, old_nd.dentry, 0);
695 err = graft_tree(mnt, nd);
697 LIST_HEAD(umount_list);
698 spin_lock(&vfsmount_lock);
699 umount_tree(mnt, &umount_list);
700 spin_unlock(&vfsmount_lock);
701 release_mounts(&umount_list);
705 up_write(¤t->namespace->sem);
706 path_release(&old_nd);
711 * change filesystem flags. dir should be a physical root of filesystem.
712 * If you've mounted a non-root directory somewhere and want to do remount
713 * on it - tough luck.
715 static int do_remount(struct nameidata *nd, int flags, int mnt_flags,
719 struct super_block *sb = nd->mnt->mnt_sb;
721 if (!capable(CAP_SYS_ADMIN))
724 if (!check_mnt(nd->mnt))
727 if (nd->dentry != nd->mnt->mnt_root)
730 down_write(&sb->s_umount);
731 err = do_remount_sb(sb, flags, data, 0);
733 nd->mnt->mnt_flags = mnt_flags;
734 up_write(&sb->s_umount);
736 security_sb_post_remount(nd->mnt, flags, data);
740 static int do_move_mount(struct nameidata *nd, char *old_name)
742 struct nameidata old_nd, parent_nd;
745 if (!capable(CAP_SYS_ADMIN))
747 if (!old_name || !*old_name)
749 err = path_lookup(old_name, LOOKUP_FOLLOW, &old_nd);
753 down_write(¤t->namespace->sem);
754 while (d_mountpoint(nd->dentry) && follow_down(&nd->mnt, &nd->dentry))
757 if (!check_mnt(nd->mnt) || !check_mnt(old_nd.mnt))
761 down(&nd->dentry->d_inode->i_sem);
762 if (IS_DEADDIR(nd->dentry->d_inode))
765 spin_lock(&vfsmount_lock);
766 if (!IS_ROOT(nd->dentry) && d_unhashed(nd->dentry))
770 if (old_nd.dentry != old_nd.mnt->mnt_root)
773 if (old_nd.mnt == old_nd.mnt->mnt_parent)
776 if (S_ISDIR(nd->dentry->d_inode->i_mode) !=
777 S_ISDIR(old_nd.dentry->d_inode->i_mode))
781 for (p = nd->mnt; p->mnt_parent != p; p = p->mnt_parent)
786 detach_mnt(old_nd.mnt, &parent_nd);
787 attach_mnt(old_nd.mnt, nd);
788 touch_namespace(current->namespace);
790 /* if the mount is moved, it should no longer be expire
792 list_del_init(&old_nd.mnt->mnt_expire);
794 spin_unlock(&vfsmount_lock);
796 up(&nd->dentry->d_inode->i_sem);
798 up_write(¤t->namespace->sem);
800 path_release(&parent_nd);
801 path_release(&old_nd);
806 * create a new mount for userspace and request it to be added into the
809 static int do_new_mount(struct nameidata *nd, char *type, int flags,
810 int mnt_flags, char *name, void *data)
812 struct vfsmount *mnt;
814 if (!type || !memchr(type, 0, PAGE_SIZE))
817 /* we need capabilities... */
818 if (!capable(CAP_SYS_ADMIN))
821 mnt = do_kern_mount(type, flags, name, data);
825 return do_add_mount(mnt, nd, mnt_flags, NULL);
829 * add a mount into a namespace's mount tree
830 * - provide the option of adding the new mount to an expiration list
832 int do_add_mount(struct vfsmount *newmnt, struct nameidata *nd,
833 int mnt_flags, struct list_head *fslist)
837 down_write(¤t->namespace->sem);
838 /* Something was mounted here while we slept */
839 while (d_mountpoint(nd->dentry) && follow_down(&nd->mnt, &nd->dentry))
842 if (!check_mnt(nd->mnt))
845 /* Refuse the same filesystem on the same mount point */
847 if (nd->mnt->mnt_sb == newmnt->mnt_sb &&
848 nd->mnt->mnt_root == nd->dentry)
852 if (S_ISLNK(newmnt->mnt_root->d_inode->i_mode))
855 newmnt->mnt_flags = mnt_flags;
856 if ((err = graft_tree(newmnt, nd)))
860 /* add to the specified expiration list */
861 spin_lock(&vfsmount_lock);
862 list_add_tail(&newmnt->mnt_expire, fslist);
863 spin_unlock(&vfsmount_lock);
865 up_write(¤t->namespace->sem);
869 up_write(¤t->namespace->sem);
874 EXPORT_SYMBOL_GPL(do_add_mount);
876 static void expire_mount(struct vfsmount *mnt, struct list_head *mounts,
877 struct list_head *umounts)
879 spin_lock(&vfsmount_lock);
882 * Check if mount is still attached, if not, let whoever holds it deal
885 if (mnt->mnt_parent == mnt) {
886 spin_unlock(&vfsmount_lock);
891 * Check that it is still dead: the count should now be 2 - as
892 * contributed by the vfsmount parent and the mntget above
894 if (atomic_read(&mnt->mnt_count) == 2) {
895 /* delete from the namespace */
896 touch_namespace(mnt->mnt_namespace);
897 list_del_init(&mnt->mnt_list);
898 mnt->mnt_namespace = NULL;
899 umount_tree(mnt, umounts);
900 spin_unlock(&vfsmount_lock);
903 * Someone brought it back to life whilst we didn't have any
904 * locks held so return it to the expiration list
906 list_add_tail(&mnt->mnt_expire, mounts);
907 spin_unlock(&vfsmount_lock);
912 * process a list of expirable mountpoints with the intent of discarding any
913 * mountpoints that aren't in use and haven't been touched since last we came
916 void mark_mounts_for_expiry(struct list_head *mounts)
918 struct namespace *namespace;
919 struct vfsmount *mnt, *next;
920 LIST_HEAD(graveyard);
922 if (list_empty(mounts))
925 spin_lock(&vfsmount_lock);
927 /* extract from the expiration list every vfsmount that matches the
928 * following criteria:
929 * - only referenced by its parent vfsmount
930 * - still marked for expiry (marked on the last call here; marks are
931 * cleared by mntput())
933 list_for_each_entry_safe(mnt, next, mounts, mnt_expire) {
934 if (!xchg(&mnt->mnt_expiry_mark, 1) ||
935 atomic_read(&mnt->mnt_count) != 1)
939 list_move(&mnt->mnt_expire, &graveyard);
943 * go through the vfsmounts we've just consigned to the graveyard to
944 * - check that they're still dead
945 * - delete the vfsmount from the appropriate namespace under lock
946 * - dispose of the corpse
948 while (!list_empty(&graveyard)) {
950 mnt = list_entry(graveyard.next, struct vfsmount, mnt_expire);
951 list_del_init(&mnt->mnt_expire);
953 /* don't do anything if the namespace is dead - all the
954 * vfsmounts from it are going away anyway */
955 namespace = mnt->mnt_namespace;
956 if (!namespace || !namespace->root)
958 get_namespace(namespace);
960 spin_unlock(&vfsmount_lock);
961 down_write(&namespace->sem);
962 expire_mount(mnt, mounts, &umounts);
963 up_write(&namespace->sem);
964 release_mounts(&umounts);
966 put_namespace(namespace);
967 spin_lock(&vfsmount_lock);
970 spin_unlock(&vfsmount_lock);
973 EXPORT_SYMBOL_GPL(mark_mounts_for_expiry);
976 * Some copy_from_user() implementations do not return the exact number of
977 * bytes remaining to copy on a fault. But copy_mount_options() requires that.
978 * Note that this function differs from copy_from_user() in that it will oops
979 * on bad values of `to', rather than returning a short copy.
981 static long exact_copy_from_user(void *to, const void __user * from,
985 const char __user *f = from;
988 if (!access_ok(VERIFY_READ, from, n))
992 if (__get_user(c, f)) {
1003 int copy_mount_options(const void __user * data, unsigned long *where)
1013 if (!(page = __get_free_page(GFP_KERNEL)))
1016 /* We only care that *some* data at the address the user
1017 * gave us is valid. Just in case, we'll zero
1018 * the remainder of the page.
1020 /* copy_from_user cannot cross TASK_SIZE ! */
1021 size = TASK_SIZE - (unsigned long)data;
1022 if (size > PAGE_SIZE)
1025 i = size - exact_copy_from_user((void *)page, data, size);
1031 memset((char *)page + i, 0, PAGE_SIZE - i);
1037 * Flags is a 32-bit value that allows up to 31 non-fs dependent flags to
1038 * be given to the mount() call (ie: read-only, no-dev, no-suid etc).
1040 * data is a (void *) that can point to any structure up to
1041 * PAGE_SIZE-1 bytes, which can contain arbitrary fs-dependent
1042 * information (or be NULL).
1044 * Pre-0.97 versions of mount() didn't have a flags word.
1045 * When the flags word was introduced its top half was required
1046 * to have the magic value 0xC0ED, and this remained so until 2.4.0-test9.
1047 * Therefore, if this magic number is present, it carries no information
1048 * and must be discarded.
1050 long do_mount(char *dev_name, char *dir_name, char *type_page,
1051 unsigned long flags, void *data_page)
1053 struct nameidata nd;
1058 if ((flags & MS_MGC_MSK) == MS_MGC_VAL)
1059 flags &= ~MS_MGC_MSK;
1061 /* Basic sanity checks */
1063 if (!dir_name || !*dir_name || !memchr(dir_name, 0, PAGE_SIZE))
1065 if (dev_name && !memchr(dev_name, 0, PAGE_SIZE))
1069 ((char *)data_page)[PAGE_SIZE - 1] = 0;
1071 /* Separate the per-mountpoint flags */
1072 if (flags & MS_NOSUID)
1073 mnt_flags |= MNT_NOSUID;
1074 if (flags & MS_NODEV)
1075 mnt_flags |= MNT_NODEV;
1076 if (flags & MS_NOEXEC)
1077 mnt_flags |= MNT_NOEXEC;
1078 flags &= ~(MS_NOSUID | MS_NOEXEC | MS_NODEV | MS_ACTIVE);
1080 /* ... and get the mountpoint */
1081 retval = path_lookup(dir_name, LOOKUP_FOLLOW, &nd);
1085 retval = security_sb_mount(dev_name, &nd, type_page, flags, data_page);
1089 if (flags & MS_REMOUNT)
1090 retval = do_remount(&nd, flags & ~MS_REMOUNT, mnt_flags,
1092 else if (flags & MS_BIND)
1093 retval = do_loopback(&nd, dev_name, flags & MS_REC);
1094 else if (flags & MS_MOVE)
1095 retval = do_move_mount(&nd, dev_name);
1097 retval = do_new_mount(&nd, type_page, flags, mnt_flags,
1098 dev_name, data_page);
1104 int copy_namespace(int flags, struct task_struct *tsk)
1106 struct namespace *namespace = tsk->namespace;
1107 struct namespace *new_ns;
1108 struct vfsmount *rootmnt = NULL, *pwdmnt = NULL, *altrootmnt = NULL;
1109 struct fs_struct *fs = tsk->fs;
1110 struct vfsmount *p, *q;
1115 get_namespace(namespace);
1117 if (!(flags & CLONE_NEWNS))
1120 if (!capable(CAP_SYS_ADMIN)) {
1121 put_namespace(namespace);
1125 new_ns = kmalloc(sizeof(struct namespace), GFP_KERNEL);
1129 atomic_set(&new_ns->count, 1);
1130 init_rwsem(&new_ns->sem);
1131 INIT_LIST_HEAD(&new_ns->list);
1132 init_waitqueue_head(&new_ns->poll);
1135 down_write(&tsk->namespace->sem);
1136 /* First pass: copy the tree topology */
1137 new_ns->root = copy_tree(namespace->root, namespace->root->mnt_root,
1139 if (!new_ns->root) {
1140 up_write(&tsk->namespace->sem);
1144 spin_lock(&vfsmount_lock);
1145 list_add_tail(&new_ns->list, &new_ns->root->mnt_list);
1146 spin_unlock(&vfsmount_lock);
1149 * Second pass: switch the tsk->fs->* elements and mark new vfsmounts
1150 * as belonging to new namespace. We have already acquired a private
1151 * fs_struct, so tsk->fs->lock is not needed.
1153 p = namespace->root;
1156 q->mnt_namespace = new_ns;
1158 if (p == fs->rootmnt) {
1160 fs->rootmnt = mntget(q);
1162 if (p == fs->pwdmnt) {
1164 fs->pwdmnt = mntget(q);
1166 if (p == fs->altrootmnt) {
1168 fs->altrootmnt = mntget(q);
1171 p = next_mnt(p, namespace->root);
1172 q = next_mnt(q, new_ns->root);
1174 up_write(&tsk->namespace->sem);
1176 tsk->namespace = new_ns;
1185 put_namespace(namespace);
1189 put_namespace(namespace);
1193 asmlinkage long sys_mount(char __user * dev_name, char __user * dir_name,
1194 char __user * type, unsigned long flags,
1198 unsigned long data_page;
1199 unsigned long type_page;
1200 unsigned long dev_page;
1203 retval = copy_mount_options(type, &type_page);
1207 dir_page = getname(dir_name);
1208 retval = PTR_ERR(dir_page);
1209 if (IS_ERR(dir_page))
1212 retval = copy_mount_options(dev_name, &dev_page);
1216 retval = copy_mount_options(data, &data_page);
1221 retval = do_mount((char *)dev_page, dir_page, (char *)type_page,
1222 flags, (void *)data_page);
1224 free_page(data_page);
1227 free_page(dev_page);
1231 free_page(type_page);
1236 * Replace the fs->{rootmnt,root} with {mnt,dentry}. Put the old values.
1237 * It can block. Requires the big lock held.
1239 void set_fs_root(struct fs_struct *fs, struct vfsmount *mnt,
1240 struct dentry *dentry)
1242 struct dentry *old_root;
1243 struct vfsmount *old_rootmnt;
1244 write_lock(&fs->lock);
1245 old_root = fs->root;
1246 old_rootmnt = fs->rootmnt;
1247 fs->rootmnt = mntget(mnt);
1248 fs->root = dget(dentry);
1249 write_unlock(&fs->lock);
1252 mntput(old_rootmnt);
1257 * Replace the fs->{pwdmnt,pwd} with {mnt,dentry}. Put the old values.
1258 * It can block. Requires the big lock held.
1260 void set_fs_pwd(struct fs_struct *fs, struct vfsmount *mnt,
1261 struct dentry *dentry)
1263 struct dentry *old_pwd;
1264 struct vfsmount *old_pwdmnt;
1266 write_lock(&fs->lock);
1268 old_pwdmnt = fs->pwdmnt;
1269 fs->pwdmnt = mntget(mnt);
1270 fs->pwd = dget(dentry);
1271 write_unlock(&fs->lock);
1279 static void chroot_fs_refs(struct nameidata *old_nd, struct nameidata *new_nd)
1281 struct task_struct *g, *p;
1282 struct fs_struct *fs;
1284 read_lock(&tasklist_lock);
1285 do_each_thread(g, p) {
1289 atomic_inc(&fs->count);
1291 if (fs->root == old_nd->dentry
1292 && fs->rootmnt == old_nd->mnt)
1293 set_fs_root(fs, new_nd->mnt, new_nd->dentry);
1294 if (fs->pwd == old_nd->dentry
1295 && fs->pwdmnt == old_nd->mnt)
1296 set_fs_pwd(fs, new_nd->mnt, new_nd->dentry);
1300 } while_each_thread(g, p);
1301 read_unlock(&tasklist_lock);
1305 * pivot_root Semantics:
1306 * Moves the root file system of the current process to the directory put_old,
1307 * makes new_root as the new root file system of the current process, and sets
1308 * root/cwd of all processes which had them on the current root to new_root.
1311 * The new_root and put_old must be directories, and must not be on the
1312 * same file system as the current process root. The put_old must be
1313 * underneath new_root, i.e. adding a non-zero number of /.. to the string
1314 * pointed to by put_old must yield the same directory as new_root. No other
1315 * file system may be mounted on put_old. After all, new_root is a mountpoint.
1318 * - we don't move root/cwd if they are not at the root (reason: if something
1319 * cared enough to change them, it's probably wrong to force them elsewhere)
1320 * - it's okay to pick a root that isn't the root of a file system, e.g.
1321 * /nfs/my_root where /nfs is the mount point. It must be a mountpoint,
1322 * though, so you may need to say mount --bind /nfs/my_root /nfs/my_root
1325 asmlinkage long sys_pivot_root(const char __user * new_root,
1326 const char __user * put_old)
1328 struct vfsmount *tmp;
1329 struct nameidata new_nd, old_nd, parent_nd, root_parent, user_nd;
1332 if (!capable(CAP_SYS_ADMIN))
1337 error = __user_walk(new_root, LOOKUP_FOLLOW | LOOKUP_DIRECTORY,
1342 if (!check_mnt(new_nd.mnt))
1345 error = __user_walk(put_old, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &old_nd);
1349 error = security_sb_pivotroot(&old_nd, &new_nd);
1351 path_release(&old_nd);
1355 read_lock(¤t->fs->lock);
1356 user_nd.mnt = mntget(current->fs->rootmnt);
1357 user_nd.dentry = dget(current->fs->root);
1358 read_unlock(¤t->fs->lock);
1359 down_write(¤t->namespace->sem);
1360 down(&old_nd.dentry->d_inode->i_sem);
1362 if (!check_mnt(user_nd.mnt))
1365 if (IS_DEADDIR(new_nd.dentry->d_inode))
1367 if (d_unhashed(new_nd.dentry) && !IS_ROOT(new_nd.dentry))
1369 if (d_unhashed(old_nd.dentry) && !IS_ROOT(old_nd.dentry))
1372 if (new_nd.mnt == user_nd.mnt || old_nd.mnt == user_nd.mnt)
1373 goto out2; /* loop, on the same file system */
1375 if (user_nd.mnt->mnt_root != user_nd.dentry)
1376 goto out2; /* not a mountpoint */
1377 if (user_nd.mnt->mnt_parent == user_nd.mnt)
1378 goto out2; /* not attached */
1379 if (new_nd.mnt->mnt_root != new_nd.dentry)
1380 goto out2; /* not a mountpoint */
1381 if (new_nd.mnt->mnt_parent == new_nd.mnt)
1382 goto out2; /* not attached */
1383 tmp = old_nd.mnt; /* make sure we can reach put_old from new_root */
1384 spin_lock(&vfsmount_lock);
1385 if (tmp != new_nd.mnt) {
1387 if (tmp->mnt_parent == tmp)
1388 goto out3; /* already mounted on put_old */
1389 if (tmp->mnt_parent == new_nd.mnt)
1391 tmp = tmp->mnt_parent;
1393 if (!is_subdir(tmp->mnt_mountpoint, new_nd.dentry))
1395 } else if (!is_subdir(old_nd.dentry, new_nd.dentry))
1397 detach_mnt(new_nd.mnt, &parent_nd);
1398 detach_mnt(user_nd.mnt, &root_parent);
1399 attach_mnt(user_nd.mnt, &old_nd); /* mount old root on put_old */
1400 attach_mnt(new_nd.mnt, &root_parent); /* mount new_root on / */
1401 touch_namespace(current->namespace);
1402 spin_unlock(&vfsmount_lock);
1403 chroot_fs_refs(&user_nd, &new_nd);
1404 security_sb_post_pivotroot(&user_nd, &new_nd);
1406 path_release(&root_parent);
1407 path_release(&parent_nd);
1409 up(&old_nd.dentry->d_inode->i_sem);
1410 up_write(¤t->namespace->sem);
1411 path_release(&user_nd);
1412 path_release(&old_nd);
1414 path_release(&new_nd);
1419 spin_unlock(&vfsmount_lock);
1423 static void __init init_mount_tree(void)
1425 struct vfsmount *mnt;
1426 struct namespace *namespace;
1427 struct task_struct *g, *p;
1429 mnt = do_kern_mount("rootfs", 0, "rootfs", NULL);
1431 panic("Can't create rootfs");
1432 namespace = kmalloc(sizeof(*namespace), GFP_KERNEL);
1434 panic("Can't allocate initial namespace");
1435 atomic_set(&namespace->count, 1);
1436 INIT_LIST_HEAD(&namespace->list);
1437 init_rwsem(&namespace->sem);
1438 init_waitqueue_head(&namespace->poll);
1439 namespace->event = 0;
1440 list_add(&mnt->mnt_list, &namespace->list);
1441 namespace->root = mnt;
1442 mnt->mnt_namespace = namespace;
1444 init_task.namespace = namespace;
1445 read_lock(&tasklist_lock);
1446 do_each_thread(g, p) {
1447 get_namespace(namespace);
1448 p->namespace = namespace;
1449 } while_each_thread(g, p);
1450 read_unlock(&tasklist_lock);
1452 set_fs_pwd(current->fs, namespace->root, namespace->root->mnt_root);
1453 set_fs_root(current->fs, namespace->root, namespace->root->mnt_root);
1456 void __init mnt_init(unsigned long mempages)
1458 struct list_head *d;
1459 unsigned int nr_hash;
1462 mnt_cache = kmem_cache_create("mnt_cache", sizeof(struct vfsmount),
1463 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL, NULL);
1465 mount_hashtable = (struct list_head *)__get_free_page(GFP_ATOMIC);
1467 if (!mount_hashtable)
1468 panic("Failed to allocate mount hash table\n");
1471 * Find the power-of-two list-heads that can fit into the allocation..
1472 * We don't guarantee that "sizeof(struct list_head)" is necessarily
1475 nr_hash = PAGE_SIZE / sizeof(struct list_head);
1479 } while ((nr_hash >> hash_bits) != 0);
1483 * Re-calculate the actual number of entries and the mask
1484 * from the number of bits we can fit.
1486 nr_hash = 1UL << hash_bits;
1487 hash_mask = nr_hash - 1;
1489 printk("Mount-cache hash table entries: %d\n", nr_hash);
1491 /* And initialize the newly allocated array */
1492 d = mount_hashtable;
1504 void __put_namespace(struct namespace *namespace)
1506 struct vfsmount *root = namespace->root;
1507 LIST_HEAD(umount_list);
1508 namespace->root = NULL;
1509 spin_unlock(&vfsmount_lock);
1510 down_write(&namespace->sem);
1511 spin_lock(&vfsmount_lock);
1512 umount_tree(root, &umount_list);
1513 spin_unlock(&vfsmount_lock);
1514 up_write(&namespace->sem);
1515 release_mounts(&umount_list);