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 {
229 { MS_SYNCHRONOUS, ",sync" },
230 { MS_DIRSYNC, ",dirsync" },
231 { MS_MANDLOCK, ",mand" },
232 { MS_NOATIME, ",noatime" },
233 { MS_NODIRATIME, ",nodiratime" },
236 static struct proc_fs_info mnt_info[] = {
237 { MNT_NOSUID, ",nosuid" },
238 { MNT_NODEV, ",nodev" },
239 { MNT_NOEXEC, ",noexec" },
242 struct proc_fs_info *fs_infop;
244 if (vx_flags(VXF_HIDE_MOUNT, 0))
247 mangle(m, mnt->mnt_devname ? mnt->mnt_devname : "none");
249 seq_path(m, mnt, mnt->mnt_root, " \t\n\\");
251 mangle(m, mnt->mnt_sb->s_type->name);
252 seq_puts(m, mnt->mnt_sb->s_flags & MS_RDONLY ? " ro" : " rw");
253 for (fs_infop = fs_info; fs_infop->flag; fs_infop++) {
254 if (mnt->mnt_sb->s_flags & fs_infop->flag)
255 seq_puts(m, fs_infop->str);
257 for (fs_infop = mnt_info; fs_infop->flag; fs_infop++) {
258 if (mnt->mnt_flags & fs_infop->flag)
259 seq_puts(m, fs_infop->str);
261 if (mnt->mnt_sb->s_op->show_options)
262 err = mnt->mnt_sb->s_op->show_options(m, mnt);
263 seq_puts(m, " 0 0\n");
267 struct seq_operations mounts_op = {
275 * may_umount_tree - check if a mount tree is busy
276 * @mnt: root of mount tree
278 * This is called to check if a tree of mounts has any
279 * open files, pwds, chroots or sub mounts that are
282 int may_umount_tree(struct vfsmount *mnt)
284 struct list_head *next;
285 struct vfsmount *this_parent = mnt;
289 spin_lock(&vfsmount_lock);
290 actual_refs = atomic_read(&mnt->mnt_count);
293 next = this_parent->mnt_mounts.next;
295 while (next != &this_parent->mnt_mounts) {
296 struct vfsmount *p = list_entry(next, struct vfsmount, mnt_child);
300 actual_refs += atomic_read(&p->mnt_count);
303 if (!list_empty(&p->mnt_mounts)) {
309 if (this_parent != mnt) {
310 next = this_parent->mnt_child.next;
311 this_parent = this_parent->mnt_parent;
314 spin_unlock(&vfsmount_lock);
316 if (actual_refs > minimum_refs)
322 EXPORT_SYMBOL(may_umount_tree);
325 * may_umount - check if a mount point is busy
326 * @mnt: root of mount
328 * This is called to check if a mount point has any
329 * open files, pwds, chroots or sub mounts. If the
330 * mount has sub mounts this will return busy
331 * regardless of whether the sub mounts are busy.
333 * Doesn't take quota and stuff into account. IOW, in some cases it will
334 * give false negatives. The main reason why it's here is that we need
335 * a non-destructive way to look for easily umountable filesystems.
337 int may_umount(struct vfsmount *mnt)
339 if (atomic_read(&mnt->mnt_count) > 2)
344 EXPORT_SYMBOL(may_umount);
346 static inline void __umount_tree(struct vfsmount *mnt, struct list_head *kill)
348 while (!list_empty(kill)) {
349 mnt = list_entry(kill->next, struct vfsmount, mnt_list);
350 list_del_init(&mnt->mnt_list);
351 list_del_init(&mnt->mnt_fslink);
352 if (mnt->mnt_parent == mnt) {
353 spin_unlock(&vfsmount_lock);
355 struct nameidata old_nd;
356 detach_mnt(mnt, &old_nd);
357 spin_unlock(&vfsmount_lock);
358 path_release(&old_nd);
361 spin_lock(&vfsmount_lock);
365 void umount_tree(struct vfsmount *mnt)
370 for (p = mnt; p; p = next_mnt(p, mnt)) {
371 list_del(&p->mnt_list);
372 list_add(&p->mnt_list, &kill);
374 __umount_tree(mnt, &kill);
377 void umount_unused(struct vfsmount *mnt, struct fs_struct *fs)
382 for (p = mnt; p; p = next_mnt(p, mnt)) {
383 if (p == fs->rootmnt || p == fs->pwdmnt)
385 list_del(&p->mnt_list);
386 list_add(&p->mnt_list, &kill);
388 __umount_tree(mnt, &kill);
391 static int do_umount(struct vfsmount *mnt, int flags)
393 struct super_block * sb = mnt->mnt_sb;
396 retval = security_sb_umount(mnt, flags);
401 * Allow userspace to request a mountpoint be expired rather than
402 * unmounting unconditionally. Unmount only happens if:
403 * (1) the mark is already set (the mark is cleared by mntput())
404 * (2) the usage count == 1 [parent vfsmount] + 1 [sys_umount]
406 if (flags & MNT_EXPIRE) {
407 if (mnt == current->fs->rootmnt ||
408 flags & (MNT_FORCE | MNT_DETACH))
411 if (atomic_read(&mnt->mnt_count) != 2)
414 if (!xchg(&mnt->mnt_expiry_mark, 1))
419 * If we may have to abort operations to get out of this
420 * mount, and they will themselves hold resources we must
421 * allow the fs to do things. In the Unix tradition of
422 * 'Gee thats tricky lets do it in userspace' the umount_begin
423 * might fail to complete on the first run through as other tasks
424 * must return, and the like. Thats for the mount program to worry
425 * about for the moment.
429 if( (flags&MNT_FORCE) && sb->s_op->umount_begin)
430 sb->s_op->umount_begin(sb);
434 * No sense to grab the lock for this test, but test itself looks
435 * somewhat bogus. Suggestions for better replacement?
436 * Ho-hum... In principle, we might treat that as umount + switch
437 * to rootfs. GC would eventually take care of the old vfsmount.
438 * Actually it makes sense, especially if rootfs would contain a
439 * /reboot - static binary that would close all descriptors and
440 * call reboot(9). Then init(8) could umount root and exec /reboot.
442 if (mnt == current->fs->rootmnt && !(flags & MNT_DETACH)) {
444 * Special case for "unmounting" root ...
445 * we just try to remount it readonly.
447 down_write(&sb->s_umount);
448 if (!(sb->s_flags & MS_RDONLY)) {
450 retval = do_remount_sb(sb, MS_RDONLY, NULL, 0);
453 up_write(&sb->s_umount);
457 down_write(¤t->namespace->sem);
458 spin_lock(&vfsmount_lock);
460 if (atomic_read(&sb->s_active) == 1) {
461 /* last instance - try to be smart */
462 spin_unlock(&vfsmount_lock);
467 security_sb_umount_close(mnt);
468 spin_lock(&vfsmount_lock);
471 if (atomic_read(&mnt->mnt_count) == 2 || flags & MNT_DETACH) {
472 if (!list_empty(&mnt->mnt_list))
476 spin_unlock(&vfsmount_lock);
478 security_sb_umount_busy(mnt);
479 up_write(¤t->namespace->sem);
484 * Now umount can handle mount points as well as block devices.
485 * This is important for filesystems which use unnamed block devices.
487 * We now support a flag for forced unmount like the other 'big iron'
488 * unixes. Our API is identical to OSF/1 to avoid making a mess of AMD
491 asmlinkage long sys_umount(char __user * name, int flags)
496 retval = __user_walk(name, LOOKUP_FOLLOW, &nd);
500 if (nd.dentry != nd.mnt->mnt_root)
502 if (!check_mnt(nd.mnt))
506 if (!capable(CAP_SYS_ADMIN) && !vx_ccaps(VXC_SECURE_MOUNT))
509 retval = do_umount(nd.mnt, flags);
511 path_release_on_umount(&nd);
516 #ifdef __ARCH_WANT_SYS_OLDUMOUNT
519 * The 2.0 compatible umount. No flags.
522 asmlinkage long sys_oldumount(char __user * name)
524 return sys_umount(name,0);
529 static int mount_is_safe(struct nameidata *nd)
531 if (capable(CAP_SYS_ADMIN))
533 if (vx_ccaps(VXC_SECURE_MOUNT))
537 if (S_ISLNK(nd->dentry->d_inode->i_mode))
539 if (nd->dentry->d_inode->i_mode & S_ISVTX) {
540 if (current->uid != nd->dentry->d_inode->i_uid)
543 if (permission(nd->dentry->d_inode, MAY_WRITE, nd))
550 lives_below_in_same_fs(struct dentry *d, struct dentry *dentry)
555 if (d == NULL || d == d->d_parent)
561 static struct vfsmount *copy_tree(struct vfsmount *mnt, struct dentry *dentry)
563 struct vfsmount *res, *p, *q, *r, *s;
567 res = q = clone_mnt(mnt, dentry);
570 q->mnt_mountpoint = mnt->mnt_mountpoint;
573 for (h = mnt->mnt_mounts.next; h != &mnt->mnt_mounts; h = h->next) {
574 r = list_entry(h, struct vfsmount, mnt_child);
575 if (!lives_below_in_same_fs(r->mnt_mountpoint, dentry))
578 for (s = r; s; s = next_mnt(s, r)) {
579 while (p != s->mnt_parent) {
585 nd.dentry = p->mnt_mountpoint;
586 q = clone_mnt(p, p->mnt_root);
589 spin_lock(&vfsmount_lock);
590 list_add_tail(&q->mnt_list, &res->mnt_list);
592 spin_unlock(&vfsmount_lock);
598 spin_lock(&vfsmount_lock);
600 spin_unlock(&vfsmount_lock);
605 static int graft_tree(struct vfsmount *mnt, struct nameidata *nd)
608 if (mnt->mnt_sb->s_flags & MS_NOUSER)
611 if (S_ISDIR(nd->dentry->d_inode->i_mode) !=
612 S_ISDIR(mnt->mnt_root->d_inode->i_mode))
616 down(&nd->dentry->d_inode->i_sem);
617 if (IS_DEADDIR(nd->dentry->d_inode))
620 err = security_sb_check_sb(mnt, nd);
625 spin_lock(&vfsmount_lock);
626 if (IS_ROOT(nd->dentry) || !d_unhashed(nd->dentry)) {
627 struct list_head head;
630 list_add_tail(&head, &mnt->mnt_list);
631 list_splice(&head, current->namespace->list.prev);
635 spin_unlock(&vfsmount_lock);
637 up(&nd->dentry->d_inode->i_sem);
639 security_sb_post_addmount(mnt, nd);
646 static int do_loopback(struct nameidata *nd, char *old_name, int recurse)
648 struct nameidata old_nd;
649 struct vfsmount *mnt = NULL;
650 int err = mount_is_safe(nd);
653 if (!old_name || !*old_name)
655 err = path_lookup(old_name, LOOKUP_FOLLOW, &old_nd);
659 down_write(¤t->namespace->sem);
661 if (check_mnt(nd->mnt) && (!recurse || check_mnt(old_nd.mnt))) {
664 mnt = copy_tree(old_nd.mnt, old_nd.dentry);
666 mnt = clone_mnt(old_nd.mnt, old_nd.dentry);
670 /* stop bind mounts from expiring */
671 spin_lock(&vfsmount_lock);
672 list_del_init(&mnt->mnt_fslink);
673 spin_unlock(&vfsmount_lock);
675 err = graft_tree(mnt, nd);
677 spin_lock(&vfsmount_lock);
679 spin_unlock(&vfsmount_lock);
684 up_write(¤t->namespace->sem);
685 path_release(&old_nd);
690 * change filesystem flags. dir should be a physical root of filesystem.
691 * If you've mounted a non-root directory somewhere and want to do remount
692 * on it - tough luck.
695 static int do_remount(struct nameidata *nd, int flags, int mnt_flags,
699 struct super_block * sb = nd->mnt->mnt_sb;
701 if (!capable(CAP_SYS_ADMIN))
704 if (!check_mnt(nd->mnt))
707 if (nd->dentry != nd->mnt->mnt_root)
710 down_write(&sb->s_umount);
711 err = do_remount_sb(sb, flags, data, 0);
713 nd->mnt->mnt_flags=mnt_flags;
714 up_write(&sb->s_umount);
716 security_sb_post_remount(nd->mnt, flags, data);
720 static int do_move_mount(struct nameidata *nd, char *old_name)
722 struct nameidata old_nd, parent_nd;
725 if (!capable(CAP_SYS_ADMIN))
727 if (!old_name || !*old_name)
729 err = path_lookup(old_name, LOOKUP_FOLLOW, &old_nd);
733 down_write(¤t->namespace->sem);
734 while(d_mountpoint(nd->dentry) && follow_down(&nd->mnt, &nd->dentry))
737 if (!check_mnt(nd->mnt) || !check_mnt(old_nd.mnt))
741 down(&nd->dentry->d_inode->i_sem);
742 if (IS_DEADDIR(nd->dentry->d_inode))
745 spin_lock(&vfsmount_lock);
746 if (!IS_ROOT(nd->dentry) && d_unhashed(nd->dentry))
750 if (old_nd.dentry != old_nd.mnt->mnt_root)
753 if (old_nd.mnt == old_nd.mnt->mnt_parent)
756 if (S_ISDIR(nd->dentry->d_inode->i_mode) !=
757 S_ISDIR(old_nd.dentry->d_inode->i_mode))
761 for (p = nd->mnt; p->mnt_parent!=p; p = p->mnt_parent)
766 detach_mnt(old_nd.mnt, &parent_nd);
767 attach_mnt(old_nd.mnt, nd);
769 /* if the mount is moved, it should no longer be expire
771 list_del_init(&old_nd.mnt->mnt_fslink);
773 spin_unlock(&vfsmount_lock);
775 up(&nd->dentry->d_inode->i_sem);
777 up_write(¤t->namespace->sem);
779 path_release(&parent_nd);
780 path_release(&old_nd);
785 * create a new mount for userspace and request it to be added into the
788 static int do_new_mount(struct nameidata *nd, char *type, int flags,
789 int mnt_flags, char *name, void *data)
791 struct vfsmount *mnt;
793 if (!type || !memchr(type, 0, PAGE_SIZE))
796 /* we need capabilities... */
797 if (!capable(CAP_SYS_ADMIN) && !vx_ccaps(VXC_SECURE_MOUNT))
800 mnt = do_kern_mount(type, flags, name, data);
804 return do_add_mount(mnt, nd, mnt_flags, NULL);
808 * add a mount into a namespace's mount tree
809 * - provide the option of adding the new mount to an expiration list
811 int do_add_mount(struct vfsmount *newmnt, struct nameidata *nd,
812 int mnt_flags, struct list_head *fslist)
816 down_write(¤t->namespace->sem);
817 /* Something was mounted here while we slept */
818 while(d_mountpoint(nd->dentry) && follow_down(&nd->mnt, &nd->dentry))
821 if (!check_mnt(nd->mnt))
824 /* Refuse the same filesystem on the same mount point */
826 if (nd->mnt->mnt_sb == newmnt->mnt_sb &&
827 nd->mnt->mnt_root == nd->dentry)
831 if (S_ISLNK(newmnt->mnt_root->d_inode->i_mode))
834 newmnt->mnt_flags = mnt_flags;
835 err = graft_tree(newmnt, nd);
837 if (err == 0 && fslist) {
838 /* add to the specified expiration list */
839 spin_lock(&vfsmount_lock);
840 list_add_tail(&newmnt->mnt_fslink, fslist);
841 spin_unlock(&vfsmount_lock);
845 up_write(¤t->namespace->sem);
850 EXPORT_SYMBOL_GPL(do_add_mount);
853 * process a list of expirable mountpoints with the intent of discarding any
854 * mountpoints that aren't in use and haven't been touched since last we came
857 void mark_mounts_for_expiry(struct list_head *mounts)
859 struct namespace *namespace;
860 struct vfsmount *mnt, *next;
861 LIST_HEAD(graveyard);
863 if (list_empty(mounts))
866 spin_lock(&vfsmount_lock);
868 /* extract from the expiration list every vfsmount that matches the
869 * following criteria:
870 * - only referenced by its parent vfsmount
871 * - still marked for expiry (marked on the last call here; marks are
872 * cleared by mntput())
874 list_for_each_entry_safe(mnt, next, mounts, mnt_fslink) {
875 if (!xchg(&mnt->mnt_expiry_mark, 1) ||
876 atomic_read(&mnt->mnt_count) != 1)
880 list_move(&mnt->mnt_fslink, &graveyard);
884 * go through the vfsmounts we've just consigned to the graveyard to
885 * - check that they're still dead
886 * - delete the vfsmount from the appropriate namespace under lock
887 * - dispose of the corpse
889 while (!list_empty(&graveyard)) {
890 mnt = list_entry(graveyard.next, struct vfsmount, mnt_fslink);
891 list_del_init(&mnt->mnt_fslink);
893 /* don't do anything if the namespace is dead - all the
894 * vfsmounts from it are going away anyway */
895 namespace = mnt->mnt_namespace;
896 if (!namespace || atomic_read(&namespace->count) <= 0)
898 get_namespace(namespace);
900 spin_unlock(&vfsmount_lock);
901 down_write(&namespace->sem);
902 spin_lock(&vfsmount_lock);
904 /* check that it is still dead: the count should now be 2 - as
905 * contributed by the vfsmount parent and the mntget above */
906 if (atomic_read(&mnt->mnt_count) == 2) {
907 struct vfsmount *xdmnt;
908 struct dentry *xdentry;
910 /* delete from the namespace */
911 list_del_init(&mnt->mnt_list);
912 list_del_init(&mnt->mnt_child);
913 list_del_init(&mnt->mnt_hash);
914 mnt->mnt_mountpoint->d_mounted--;
916 xdentry = mnt->mnt_mountpoint;
917 mnt->mnt_mountpoint = mnt->mnt_root;
918 xdmnt = mnt->mnt_parent;
919 mnt->mnt_parent = mnt;
921 spin_unlock(&vfsmount_lock);
926 /* now lay it to rest if this was the last ref on the
928 if (atomic_read(&mnt->mnt_sb->s_active) == 1) {
929 /* last instance - try to be smart */
931 DQUOT_OFF(mnt->mnt_sb);
932 acct_auto_close(mnt->mnt_sb);
938 /* someone brought it back to life whilst we didn't
939 * have any locks held so return it to the expiration
941 list_add_tail(&mnt->mnt_fslink, mounts);
942 spin_unlock(&vfsmount_lock);
945 up_write(&namespace->sem);
948 put_namespace(namespace);
950 spin_lock(&vfsmount_lock);
953 spin_unlock(&vfsmount_lock);
956 EXPORT_SYMBOL_GPL(mark_mounts_for_expiry);
958 int copy_mount_options (const void __user *data, unsigned long *where)
968 if (!(page = __get_free_page(GFP_KERNEL)))
971 /* We only care that *some* data at the address the user
972 * gave us is valid. Just in case, we'll zero
973 * the remainder of the page.
975 /* copy_from_user cannot cross TASK_SIZE ! */
976 size = TASK_SIZE - (unsigned long)data;
977 if (size > PAGE_SIZE)
980 i = size - copy_from_user((void *)page, data, size);
986 memset((char *)page + i, 0, PAGE_SIZE - i);
992 * Flags is a 32-bit value that allows up to 31 non-fs dependent flags to
993 * be given to the mount() call (ie: read-only, no-dev, no-suid etc).
995 * data is a (void *) that can point to any structure up to
996 * PAGE_SIZE-1 bytes, which can contain arbitrary fs-dependent
997 * information (or be NULL).
999 * Pre-0.97 versions of mount() didn't have a flags word.
1000 * When the flags word was introduced its top half was required
1001 * to have the magic value 0xC0ED, and this remained so until 2.4.0-test9.
1002 * Therefore, if this magic number is present, it carries no information
1003 * and must be discarded.
1005 long do_mount(char * dev_name, char * dir_name, char *type_page,
1006 unsigned long flags, void *data_page)
1008 struct nameidata nd;
1013 if ((flags & MS_MGC_MSK) == MS_MGC_VAL)
1014 flags &= ~MS_MGC_MSK;
1016 /* Basic sanity checks */
1018 if (!dir_name || !*dir_name || !memchr(dir_name, 0, PAGE_SIZE))
1020 if (dev_name && !memchr(dev_name, 0, PAGE_SIZE))
1024 ((char *)data_page)[PAGE_SIZE - 1] = 0;
1026 /* Separate the per-mountpoint flags */
1027 if (flags & MS_NOSUID)
1028 mnt_flags |= MNT_NOSUID;
1029 if (flags & MS_NODEV)
1030 mnt_flags |= MNT_NODEV;
1031 if (flags & MS_NOEXEC)
1032 mnt_flags |= MNT_NOEXEC;
1033 flags &= ~(MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_ACTIVE);
1035 if (vx_ccaps(VXC_SECURE_MOUNT))
1036 mnt_flags |= MNT_NODEV;
1038 /* ... and get the mountpoint */
1039 retval = path_lookup(dir_name, LOOKUP_FOLLOW, &nd);
1043 retval = security_sb_mount(dev_name, &nd, type_page, flags, data_page);
1047 if (flags & MS_REMOUNT)
1048 retval = do_remount(&nd, flags & ~MS_REMOUNT, mnt_flags,
1050 else if (flags & MS_BIND)
1051 retval = do_loopback(&nd, dev_name, flags & MS_REC);
1052 else if (flags & MS_MOVE)
1053 retval = do_move_mount(&nd, dev_name);
1055 retval = do_new_mount(&nd, type_page, flags, mnt_flags,
1056 dev_name, data_page);
1062 int copy_namespace(int flags, struct task_struct *tsk)
1064 struct namespace *namespace = tsk->namespace;
1065 struct namespace *new_ns;
1066 struct vfsmount *rootmnt = NULL, *pwdmnt = NULL, *altrootmnt = NULL;
1067 struct fs_struct *fs = tsk->fs;
1072 get_namespace(namespace);
1074 if (!(flags & CLONE_NEWNS))
1077 if (!capable(CAP_SYS_ADMIN)) {
1078 put_namespace(namespace);
1082 new_ns = kmalloc(sizeof(struct namespace), GFP_KERNEL);
1086 atomic_set(&new_ns->count, 1);
1087 init_rwsem(&new_ns->sem);
1088 INIT_LIST_HEAD(&new_ns->list);
1090 down_write(&tsk->namespace->sem);
1091 /* First pass: copy the tree topology */
1092 new_ns->root = copy_tree(namespace->root, namespace->root->mnt_root);
1093 if (!new_ns->root) {
1094 up_write(&tsk->namespace->sem);
1098 spin_lock(&vfsmount_lock);
1099 list_add_tail(&new_ns->list, &new_ns->root->mnt_list);
1100 spin_unlock(&vfsmount_lock);
1102 /* Second pass: switch the tsk->fs->* elements */
1104 struct vfsmount *p, *q;
1105 write_lock(&fs->lock);
1107 p = namespace->root;
1110 if (p == fs->rootmnt) {
1112 fs->rootmnt = mntget(q);
1114 if (p == fs->pwdmnt) {
1116 fs->pwdmnt = mntget(q);
1118 if (p == fs->altrootmnt) {
1120 fs->altrootmnt = mntget(q);
1122 p = next_mnt(p, namespace->root);
1123 q = next_mnt(q, new_ns->root);
1125 write_unlock(&fs->lock);
1127 up_write(&tsk->namespace->sem);
1129 tsk->namespace = new_ns;
1138 put_namespace(namespace);
1142 put_namespace(namespace);
1146 asmlinkage long sys_mount(char __user * dev_name, char __user * dir_name,
1147 char __user * type, unsigned long flags,
1151 unsigned long data_page;
1152 unsigned long type_page;
1153 unsigned long dev_page;
1156 retval = copy_mount_options (type, &type_page);
1160 dir_page = getname(dir_name);
1161 retval = PTR_ERR(dir_page);
1162 if (IS_ERR(dir_page))
1165 retval = copy_mount_options (dev_name, &dev_page);
1169 retval = copy_mount_options (data, &data_page);
1174 retval = do_mount((char*)dev_page, dir_page, (char*)type_page,
1175 flags, (void*)data_page);
1177 free_page(data_page);
1180 free_page(dev_page);
1184 free_page(type_page);
1189 * Replace the fs->{rootmnt,root} with {mnt,dentry}. Put the old values.
1190 * It can block. Requires the big lock held.
1192 void set_fs_root(struct fs_struct *fs, struct vfsmount *mnt,
1193 struct dentry *dentry)
1195 struct dentry *old_root;
1196 struct vfsmount *old_rootmnt;
1197 write_lock(&fs->lock);
1198 old_root = fs->root;
1199 old_rootmnt = fs->rootmnt;
1200 fs->rootmnt = mntget(mnt);
1201 fs->root = dget(dentry);
1202 write_unlock(&fs->lock);
1205 mntput(old_rootmnt);
1209 EXPORT_SYMBOL(set_fs_root);
1212 * Replace the fs->{pwdmnt,pwd} with {mnt,dentry}. Put the old values.
1213 * It can block. Requires the big lock held.
1215 void set_fs_pwd(struct fs_struct *fs, struct vfsmount *mnt,
1216 struct dentry *dentry)
1218 struct dentry *old_pwd;
1219 struct vfsmount *old_pwdmnt;
1221 write_lock(&fs->lock);
1223 old_pwdmnt = fs->pwdmnt;
1224 fs->pwdmnt = mntget(mnt);
1225 fs->pwd = dget(dentry);
1226 write_unlock(&fs->lock);
1234 EXPORT_SYMBOL(set_fs_pwd);
1236 static void chroot_fs_refs(struct nameidata *old_nd, struct nameidata *new_nd)
1238 struct task_struct *g, *p;
1239 struct fs_struct *fs;
1241 read_lock(&tasklist_lock);
1242 do_each_thread(g, p) {
1246 atomic_inc(&fs->count);
1248 if (fs->root==old_nd->dentry&&fs->rootmnt==old_nd->mnt)
1249 set_fs_root(fs, new_nd->mnt, new_nd->dentry);
1250 if (fs->pwd==old_nd->dentry&&fs->pwdmnt==old_nd->mnt)
1251 set_fs_pwd(fs, new_nd->mnt, new_nd->dentry);
1255 } while_each_thread(g, p);
1256 read_unlock(&tasklist_lock);
1260 * Moves the current root to put_root, and sets root/cwd of all processes
1261 * which had them on the old root to new_root.
1264 * - we don't move root/cwd if they are not at the root (reason: if something
1265 * cared enough to change them, it's probably wrong to force them elsewhere)
1266 * - it's okay to pick a root that isn't the root of a file system, e.g.
1267 * /nfs/my_root where /nfs is the mount point. It must be a mountpoint,
1268 * though, so you may need to say mount --bind /nfs/my_root /nfs/my_root
1272 asmlinkage long sys_pivot_root(const char __user *new_root, const char __user *put_old)
1274 struct vfsmount *tmp;
1275 struct nameidata new_nd, old_nd, parent_nd, root_parent, user_nd;
1278 if (!capable(CAP_SYS_ADMIN))
1283 error = __user_walk(new_root, LOOKUP_FOLLOW|LOOKUP_DIRECTORY, &new_nd);
1287 if (!check_mnt(new_nd.mnt))
1290 error = __user_walk(put_old, LOOKUP_FOLLOW|LOOKUP_DIRECTORY, &old_nd);
1294 error = security_sb_pivotroot(&old_nd, &new_nd);
1296 path_release(&old_nd);
1300 read_lock(¤t->fs->lock);
1301 user_nd.mnt = mntget(current->fs->rootmnt);
1302 user_nd.dentry = dget(current->fs->root);
1303 read_unlock(¤t->fs->lock);
1304 down_write(¤t->namespace->sem);
1305 down(&old_nd.dentry->d_inode->i_sem);
1307 if (!check_mnt(user_nd.mnt))
1310 if (IS_DEADDIR(new_nd.dentry->d_inode))
1312 if (d_unhashed(new_nd.dentry) && !IS_ROOT(new_nd.dentry))
1314 if (d_unhashed(old_nd.dentry) && !IS_ROOT(old_nd.dentry))
1317 if (new_nd.mnt == user_nd.mnt || old_nd.mnt == user_nd.mnt)
1318 goto out2; /* loop */
1320 if (user_nd.mnt->mnt_root != user_nd.dentry)
1322 if (new_nd.mnt->mnt_root != new_nd.dentry)
1323 goto out2; /* not a mountpoint */
1324 tmp = old_nd.mnt; /* make sure we can reach put_old from new_root */
1325 spin_lock(&vfsmount_lock);
1326 if (tmp != new_nd.mnt) {
1328 if (tmp->mnt_parent == tmp)
1330 if (tmp->mnt_parent == new_nd.mnt)
1332 tmp = tmp->mnt_parent;
1334 if (!is_subdir(tmp->mnt_mountpoint, new_nd.dentry))
1336 } else if (!is_subdir(old_nd.dentry, new_nd.dentry))
1338 detach_mnt(new_nd.mnt, &parent_nd);
1339 detach_mnt(user_nd.mnt, &root_parent);
1340 attach_mnt(user_nd.mnt, &old_nd);
1341 attach_mnt(new_nd.mnt, &root_parent);
1342 spin_unlock(&vfsmount_lock);
1343 chroot_fs_refs(&user_nd, &new_nd);
1344 security_sb_post_pivotroot(&user_nd, &new_nd);
1346 path_release(&root_parent);
1347 path_release(&parent_nd);
1349 up(&old_nd.dentry->d_inode->i_sem);
1350 up_write(¤t->namespace->sem);
1351 path_release(&user_nd);
1352 path_release(&old_nd);
1354 path_release(&new_nd);
1359 spin_unlock(&vfsmount_lock);
1363 static void __init init_mount_tree(void)
1365 struct vfsmount *mnt;
1366 struct namespace *namespace;
1367 struct task_struct *g, *p;
1369 mnt = do_kern_mount("rootfs", 0, "rootfs", NULL);
1371 panic("Can't create rootfs");
1372 namespace = kmalloc(sizeof(*namespace), GFP_KERNEL);
1374 panic("Can't allocate initial namespace");
1375 atomic_set(&namespace->count, 1);
1376 INIT_LIST_HEAD(&namespace->list);
1377 init_rwsem(&namespace->sem);
1378 list_add(&mnt->mnt_list, &namespace->list);
1379 namespace->root = mnt;
1380 mnt->mnt_namespace = namespace;
1382 init_task.namespace = namespace;
1383 read_lock(&tasklist_lock);
1384 do_each_thread(g, p) {
1385 get_namespace(namespace);
1386 p->namespace = namespace;
1387 } while_each_thread(g, p);
1388 read_unlock(&tasklist_lock);
1390 set_fs_pwd(current->fs, namespace->root, namespace->root->mnt_root);
1391 set_fs_root(current->fs, namespace->root, namespace->root->mnt_root);
1394 void __init mnt_init(unsigned long mempages)
1396 struct list_head *d;
1397 unsigned long order;
1398 unsigned int nr_hash;
1401 mnt_cache = kmem_cache_create("mnt_cache", sizeof(struct vfsmount),
1402 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
1405 mount_hashtable = (struct list_head *)
1406 __get_free_pages(GFP_ATOMIC, order);
1408 if (!mount_hashtable)
1409 panic("Failed to allocate mount hash table\n");
1412 * Find the power-of-two list-heads that can fit into the allocation..
1413 * We don't guarantee that "sizeof(struct list_head)" is necessarily
1416 nr_hash = (1UL << order) * PAGE_SIZE / sizeof(struct list_head);
1420 } while ((nr_hash >> hash_bits) != 0);
1424 * Re-calculate the actual number of entries and the mask
1425 * from the number of bits we can fit.
1427 nr_hash = 1UL << hash_bits;
1428 hash_mask = nr_hash-1;
1430 printk("Mount-cache hash table entries: %d (order: %ld, %ld bytes)\n",
1431 nr_hash, order, (PAGE_SIZE << order));
1433 /* And initialize the newly allocated array */
1434 d = mount_hashtable;
1446 void __put_namespace(struct namespace *namespace)
1448 struct vfsmount *mnt;
1450 down_write(&namespace->sem);
1451 spin_lock(&vfsmount_lock);
1453 list_for_each_entry(mnt, &namespace->list, mnt_list) {
1454 mnt->mnt_namespace = NULL;
1457 umount_tree(namespace->root);
1458 spin_unlock(&vfsmount_lock);
1459 up_write(&namespace->sem);