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);
31 extern int __init sysfs_init(void);
33 static inline int sysfs_init(void)
39 /* spinlock for vfsmount related operations, inplace of dcache_lock */
40 __cacheline_aligned_in_smp DEFINE_SPINLOCK(vfsmount_lock);
42 static struct list_head *mount_hashtable;
43 static int hash_mask __read_mostly, hash_bits __read_mostly;
44 static kmem_cache_t *mnt_cache;
46 static inline unsigned long hash(struct vfsmount *mnt, struct dentry *dentry)
48 unsigned long tmp = ((unsigned long) mnt / L1_CACHE_BYTES);
49 tmp += ((unsigned long) dentry / L1_CACHE_BYTES);
50 tmp = tmp + (tmp >> hash_bits);
51 return tmp & hash_mask;
54 struct vfsmount *alloc_vfsmnt(const char *name)
56 struct vfsmount *mnt = kmem_cache_alloc(mnt_cache, GFP_KERNEL);
58 memset(mnt, 0, sizeof(struct vfsmount));
59 atomic_set(&mnt->mnt_count,1);
60 INIT_LIST_HEAD(&mnt->mnt_hash);
61 INIT_LIST_HEAD(&mnt->mnt_child);
62 INIT_LIST_HEAD(&mnt->mnt_mounts);
63 INIT_LIST_HEAD(&mnt->mnt_list);
64 INIT_LIST_HEAD(&mnt->mnt_expire);
66 int size = strlen(name)+1;
67 char *newname = kmalloc(size, GFP_KERNEL);
69 memcpy(newname, name, size);
70 mnt->mnt_devname = newname;
77 void free_vfsmnt(struct vfsmount *mnt)
79 kfree(mnt->mnt_devname);
80 kmem_cache_free(mnt_cache, mnt);
84 * Now, lookup_mnt increments the ref count before returning
85 * the vfsmount struct.
87 struct vfsmount *lookup_mnt(struct vfsmount *mnt, struct dentry *dentry)
89 struct list_head * head = mount_hashtable + hash(mnt, dentry);
90 struct list_head * tmp = head;
91 struct vfsmount *p, *found = NULL;
93 spin_lock(&vfsmount_lock);
99 p = list_entry(tmp, struct vfsmount, mnt_hash);
100 if (p->mnt_parent == mnt && p->mnt_mountpoint == dentry) {
105 spin_unlock(&vfsmount_lock);
109 static inline int check_mnt(struct vfsmount *mnt)
111 return mnt->mnt_namespace == current->namespace;
114 static void detach_mnt(struct vfsmount *mnt, struct nameidata *old_nd)
116 old_nd->dentry = mnt->mnt_mountpoint;
117 old_nd->mnt = mnt->mnt_parent;
118 mnt->mnt_parent = mnt;
119 mnt->mnt_mountpoint = mnt->mnt_root;
120 list_del_init(&mnt->mnt_child);
121 list_del_init(&mnt->mnt_hash);
122 old_nd->dentry->d_mounted--;
125 static void attach_mnt(struct vfsmount *mnt, struct nameidata *nd)
127 mnt->mnt_parent = mntget(nd->mnt);
128 mnt->mnt_mountpoint = dget(nd->dentry);
129 list_add(&mnt->mnt_hash, mount_hashtable+hash(nd->mnt, nd->dentry));
130 list_add_tail(&mnt->mnt_child, &nd->mnt->mnt_mounts);
131 nd->dentry->d_mounted++;
134 static struct vfsmount *next_mnt(struct vfsmount *p, struct vfsmount *root)
136 struct list_head *next = p->mnt_mounts.next;
137 if (next == &p->mnt_mounts) {
141 next = p->mnt_child.next;
142 if (next != &p->mnt_parent->mnt_mounts)
147 return list_entry(next, struct vfsmount, mnt_child);
150 static struct vfsmount *
151 clone_mnt(struct vfsmount *old, struct dentry *root)
153 struct super_block *sb = old->mnt_sb;
154 struct vfsmount *mnt = alloc_vfsmnt(old->mnt_devname);
157 mnt->mnt_flags = old->mnt_flags;
158 atomic_inc(&sb->s_active);
160 mnt->mnt_root = dget(root);
161 mnt->mnt_mountpoint = mnt->mnt_root;
162 mnt->mnt_parent = mnt;
163 mnt->mnt_namespace = current->namespace;
165 /* stick the duplicate mount on the same expiry list
166 * as the original if that was on one */
167 spin_lock(&vfsmount_lock);
168 if (!list_empty(&old->mnt_expire))
169 list_add(&mnt->mnt_expire, &old->mnt_expire);
170 spin_unlock(&vfsmount_lock);
175 static inline void __mntput(struct vfsmount *mnt)
177 struct super_block *sb = mnt->mnt_sb;
180 deactivate_super(sb);
183 void mntput_no_expire(struct vfsmount *mnt)
186 if (atomic_dec_and_lock(&mnt->mnt_count, &vfsmount_lock)) {
187 if (likely(!mnt->mnt_pinned)) {
188 spin_unlock(&vfsmount_lock);
192 atomic_add(mnt->mnt_pinned + 1, &mnt->mnt_count);
194 spin_unlock(&vfsmount_lock);
195 acct_auto_close_mnt(mnt);
196 security_sb_umount_close(mnt);
201 EXPORT_SYMBOL(mntput_no_expire);
203 void mnt_pin(struct vfsmount *mnt)
205 spin_lock(&vfsmount_lock);
207 spin_unlock(&vfsmount_lock);
210 EXPORT_SYMBOL(mnt_pin);
212 void mnt_unpin(struct vfsmount *mnt)
214 spin_lock(&vfsmount_lock);
215 if (mnt->mnt_pinned) {
216 atomic_inc(&mnt->mnt_count);
219 spin_unlock(&vfsmount_lock);
222 EXPORT_SYMBOL(mnt_unpin);
225 static void *m_start(struct seq_file *m, loff_t *pos)
227 struct namespace *n = m->private;
232 list_for_each(p, &n->list)
234 return list_entry(p, struct vfsmount, mnt_list);
238 static void *m_next(struct seq_file *m, void *v, loff_t *pos)
240 struct namespace *n = m->private;
241 struct list_head *p = ((struct vfsmount *)v)->mnt_list.next;
243 return p==&n->list ? NULL : list_entry(p, struct vfsmount, mnt_list);
246 static void m_stop(struct seq_file *m, void *v)
248 struct namespace *n = m->private;
252 static inline void mangle(struct seq_file *m, const char *s)
254 seq_escape(m, s, " \t\n\\");
257 static int show_vfsmnt(struct seq_file *m, void *v)
259 struct vfsmount *mnt = v;
261 static struct proc_fs_info {
265 { MS_SYNCHRONOUS, ",sync" },
266 { MS_DIRSYNC, ",dirsync" },
267 { MS_MANDLOCK, ",mand" },
268 { MS_NOATIME, ",noatime" },
269 { MS_NODIRATIME, ",nodiratime" },
272 static struct proc_fs_info mnt_info[] = {
273 { MNT_NOSUID, ",nosuid" },
274 { MNT_NODEV, ",nodev" },
275 { MNT_NOEXEC, ",noexec" },
278 struct proc_fs_info *fs_infop;
280 mangle(m, mnt->mnt_devname ? mnt->mnt_devname : "none");
282 seq_path(m, mnt, mnt->mnt_root, " \t\n\\");
284 mangle(m, mnt->mnt_sb->s_type->name);
285 seq_puts(m, mnt->mnt_sb->s_flags & MS_RDONLY ? " ro" : " rw");
286 for (fs_infop = fs_info; fs_infop->flag; fs_infop++) {
287 if (mnt->mnt_sb->s_flags & fs_infop->flag)
288 seq_puts(m, fs_infop->str);
290 for (fs_infop = mnt_info; fs_infop->flag; fs_infop++) {
291 if (mnt->mnt_flags & fs_infop->flag)
292 seq_puts(m, fs_infop->str);
294 if (mnt->mnt_sb->s_op->show_options)
295 err = mnt->mnt_sb->s_op->show_options(m, mnt);
296 seq_puts(m, " 0 0\n");
300 struct seq_operations mounts_op = {
308 * may_umount_tree - check if a mount tree is busy
309 * @mnt: root of mount tree
311 * This is called to check if a tree of mounts has any
312 * open files, pwds, chroots or sub mounts that are
315 int may_umount_tree(struct vfsmount *mnt)
317 struct list_head *next;
318 struct vfsmount *this_parent = mnt;
322 spin_lock(&vfsmount_lock);
323 actual_refs = atomic_read(&mnt->mnt_count);
326 next = this_parent->mnt_mounts.next;
328 while (next != &this_parent->mnt_mounts) {
329 struct vfsmount *p = list_entry(next, struct vfsmount, mnt_child);
333 actual_refs += atomic_read(&p->mnt_count);
336 if (!list_empty(&p->mnt_mounts)) {
342 if (this_parent != mnt) {
343 next = this_parent->mnt_child.next;
344 this_parent = this_parent->mnt_parent;
347 spin_unlock(&vfsmount_lock);
349 if (actual_refs > minimum_refs)
355 EXPORT_SYMBOL(may_umount_tree);
358 * may_umount - check if a mount point is busy
359 * @mnt: root of mount
361 * This is called to check if a mount point has any
362 * open files, pwds, chroots or sub mounts. If the
363 * mount has sub mounts this will return busy
364 * regardless of whether the sub mounts are busy.
366 * Doesn't take quota and stuff into account. IOW, in some cases it will
367 * give false negatives. The main reason why it's here is that we need
368 * a non-destructive way to look for easily umountable filesystems.
370 int may_umount(struct vfsmount *mnt)
372 if (atomic_read(&mnt->mnt_count) > 2)
377 EXPORT_SYMBOL(may_umount);
379 static void umount_tree(struct vfsmount *mnt)
384 for (p = mnt; p; p = next_mnt(p, mnt)) {
385 list_del(&p->mnt_list);
386 list_add(&p->mnt_list, &kill);
387 p->mnt_namespace = NULL;
390 while (!list_empty(&kill)) {
391 mnt = list_entry(kill.next, struct vfsmount, mnt_list);
392 list_del_init(&mnt->mnt_list);
393 list_del_init(&mnt->mnt_expire);
394 if (mnt->mnt_parent == mnt) {
395 spin_unlock(&vfsmount_lock);
397 struct nameidata old_nd;
398 detach_mnt(mnt, &old_nd);
399 spin_unlock(&vfsmount_lock);
400 path_release(&old_nd);
403 spin_lock(&vfsmount_lock);
407 static int do_umount(struct vfsmount *mnt, int flags)
409 struct super_block * sb = mnt->mnt_sb;
412 retval = security_sb_umount(mnt, flags);
417 * Allow userspace to request a mountpoint be expired rather than
418 * unmounting unconditionally. Unmount only happens if:
419 * (1) the mark is already set (the mark is cleared by mntput())
420 * (2) the usage count == 1 [parent vfsmount] + 1 [sys_umount]
422 if (flags & MNT_EXPIRE) {
423 if (mnt == current->fs->rootmnt ||
424 flags & (MNT_FORCE | MNT_DETACH))
427 if (atomic_read(&mnt->mnt_count) != 2)
430 if (!xchg(&mnt->mnt_expiry_mark, 1))
435 * If we may have to abort operations to get out of this
436 * mount, and they will themselves hold resources we must
437 * allow the fs to do things. In the Unix tradition of
438 * 'Gee thats tricky lets do it in userspace' the umount_begin
439 * might fail to complete on the first run through as other tasks
440 * must return, and the like. Thats for the mount program to worry
441 * about for the moment.
445 if( (flags&MNT_FORCE) && sb->s_op->umount_begin)
446 sb->s_op->umount_begin(sb);
450 * No sense to grab the lock for this test, but test itself looks
451 * somewhat bogus. Suggestions for better replacement?
452 * Ho-hum... In principle, we might treat that as umount + switch
453 * to rootfs. GC would eventually take care of the old vfsmount.
454 * Actually it makes sense, especially if rootfs would contain a
455 * /reboot - static binary that would close all descriptors and
456 * call reboot(9). Then init(8) could umount root and exec /reboot.
458 if (mnt == current->fs->rootmnt && !(flags & MNT_DETACH)) {
460 * Special case for "unmounting" root ...
461 * we just try to remount it readonly.
463 down_write(&sb->s_umount);
464 if (!(sb->s_flags & MS_RDONLY)) {
467 retval = do_remount_sb(sb, MS_RDONLY, NULL, 0);
470 up_write(&sb->s_umount);
474 down_write(¤t->namespace->sem);
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))
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))
542 if (S_ISLNK(nd->dentry->d_inode->i_mode))
544 if (nd->dentry->d_inode->i_mode & S_ISVTX) {
545 if (current->uid != nd->dentry->d_inode->i_uid)
548 if (permission(nd->dentry->d_inode, MAY_WRITE, nd))
555 lives_below_in_same_fs(struct dentry *d, struct dentry *dentry)
560 if (d == NULL || d == d->d_parent)
566 static struct vfsmount *copy_tree(struct vfsmount *mnt, struct dentry *dentry)
568 struct vfsmount *res, *p, *q, *r, *s;
571 res = q = clone_mnt(mnt, dentry);
574 q->mnt_mountpoint = mnt->mnt_mountpoint;
577 list_for_each_entry(r, &mnt->mnt_mounts, mnt_child) {
578 if (!lives_below_in_same_fs(r->mnt_mountpoint, dentry))
581 for (s = r; s; s = next_mnt(s, r)) {
582 while (p != s->mnt_parent) {
588 nd.dentry = p->mnt_mountpoint;
589 q = clone_mnt(p, p->mnt_root);
592 spin_lock(&vfsmount_lock);
593 list_add_tail(&q->mnt_list, &res->mnt_list);
595 spin_unlock(&vfsmount_lock);
601 spin_lock(&vfsmount_lock);
603 spin_unlock(&vfsmount_lock);
608 static int graft_tree(struct vfsmount *mnt, struct nameidata *nd)
611 if (mnt->mnt_sb->s_flags & MS_NOUSER)
614 if (S_ISDIR(nd->dentry->d_inode->i_mode) !=
615 S_ISDIR(mnt->mnt_root->d_inode->i_mode))
619 down(&nd->dentry->d_inode->i_sem);
620 if (IS_DEADDIR(nd->dentry->d_inode))
623 err = security_sb_check_sb(mnt, nd);
628 spin_lock(&vfsmount_lock);
629 if (IS_ROOT(nd->dentry) || !d_unhashed(nd->dentry)) {
630 struct list_head head;
633 list_add_tail(&head, &mnt->mnt_list);
634 list_splice(&head, current->namespace->list.prev);
638 spin_unlock(&vfsmount_lock);
640 up(&nd->dentry->d_inode->i_sem);
642 security_sb_post_addmount(mnt, nd);
649 static int do_loopback(struct nameidata *nd, char *old_name, int recurse)
651 struct nameidata old_nd;
652 struct vfsmount *mnt = NULL;
653 int err = mount_is_safe(nd);
656 if (!old_name || !*old_name)
658 err = path_lookup(old_name, LOOKUP_FOLLOW, &old_nd);
662 down_write(¤t->namespace->sem);
664 if (check_mnt(nd->mnt) && (!recurse || check_mnt(old_nd.mnt))) {
667 mnt = copy_tree(old_nd.mnt, old_nd.dentry);
669 mnt = clone_mnt(old_nd.mnt, old_nd.dentry);
673 /* stop bind mounts from expiring */
674 spin_lock(&vfsmount_lock);
675 list_del_init(&mnt->mnt_expire);
676 spin_unlock(&vfsmount_lock);
678 err = graft_tree(mnt, nd);
680 spin_lock(&vfsmount_lock);
682 spin_unlock(&vfsmount_lock);
687 up_write(¤t->namespace->sem);
688 path_release(&old_nd);
693 * change filesystem flags. dir should be a physical root of filesystem.
694 * If you've mounted a non-root directory somewhere and want to do remount
695 * on it - tough luck.
698 static int do_remount(struct nameidata *nd, int flags, int mnt_flags,
702 struct super_block * sb = nd->mnt->mnt_sb;
704 if (!capable(CAP_SYS_ADMIN))
707 if (!check_mnt(nd->mnt))
710 if (nd->dentry != nd->mnt->mnt_root)
713 down_write(&sb->s_umount);
714 err = do_remount_sb(sb, flags, data, 0);
716 nd->mnt->mnt_flags=mnt_flags;
717 up_write(&sb->s_umount);
719 security_sb_post_remount(nd->mnt, flags, data);
723 static int do_move_mount(struct nameidata *nd, char *old_name)
725 struct nameidata old_nd, parent_nd;
728 if (!capable(CAP_SYS_ADMIN))
730 if (!old_name || !*old_name)
732 err = path_lookup(old_name, LOOKUP_FOLLOW, &old_nd);
736 down_write(¤t->namespace->sem);
737 while(d_mountpoint(nd->dentry) && follow_down(&nd->mnt, &nd->dentry))
740 if (!check_mnt(nd->mnt) || !check_mnt(old_nd.mnt))
744 down(&nd->dentry->d_inode->i_sem);
745 if (IS_DEADDIR(nd->dentry->d_inode))
748 spin_lock(&vfsmount_lock);
749 if (!IS_ROOT(nd->dentry) && d_unhashed(nd->dentry))
753 if (old_nd.dentry != old_nd.mnt->mnt_root)
756 if (old_nd.mnt == old_nd.mnt->mnt_parent)
759 if (S_ISDIR(nd->dentry->d_inode->i_mode) !=
760 S_ISDIR(old_nd.dentry->d_inode->i_mode))
764 for (p = nd->mnt; p->mnt_parent!=p; p = p->mnt_parent)
769 detach_mnt(old_nd.mnt, &parent_nd);
770 attach_mnt(old_nd.mnt, nd);
772 /* if the mount is moved, it should no longer be expire
774 list_del_init(&old_nd.mnt->mnt_expire);
776 spin_unlock(&vfsmount_lock);
778 up(&nd->dentry->d_inode->i_sem);
780 up_write(¤t->namespace->sem);
782 path_release(&parent_nd);
783 path_release(&old_nd);
788 * create a new mount for userspace and request it to be added into the
791 static int do_new_mount(struct nameidata *nd, char *type, int flags,
792 int mnt_flags, char *name, void *data)
794 struct vfsmount *mnt;
796 if (!type || !memchr(type, 0, PAGE_SIZE))
799 /* we need capabilities... */
800 if (!capable(CAP_SYS_ADMIN))
803 mnt = do_kern_mount(type, flags, name, data);
807 return do_add_mount(mnt, nd, mnt_flags, NULL);
811 * add a mount into a namespace's mount tree
812 * - provide the option of adding the new mount to an expiration list
814 int do_add_mount(struct vfsmount *newmnt, struct nameidata *nd,
815 int mnt_flags, struct list_head *fslist)
819 down_write(¤t->namespace->sem);
820 /* Something was mounted here while we slept */
821 while(d_mountpoint(nd->dentry) && follow_down(&nd->mnt, &nd->dentry))
824 if (!check_mnt(nd->mnt))
827 /* Refuse the same filesystem on the same mount point */
829 if (nd->mnt->mnt_sb == newmnt->mnt_sb &&
830 nd->mnt->mnt_root == nd->dentry)
834 if (S_ISLNK(newmnt->mnt_root->d_inode->i_mode))
837 newmnt->mnt_flags = mnt_flags;
838 newmnt->mnt_namespace = current->namespace;
839 err = graft_tree(newmnt, nd);
841 if (err == 0 && fslist) {
842 /* add to the specified expiration list */
843 spin_lock(&vfsmount_lock);
844 list_add_tail(&newmnt->mnt_expire, fslist);
845 spin_unlock(&vfsmount_lock);
849 up_write(¤t->namespace->sem);
854 EXPORT_SYMBOL_GPL(do_add_mount);
856 static void expire_mount(struct vfsmount *mnt, struct list_head *mounts)
858 spin_lock(&vfsmount_lock);
861 * Check if mount is still attached, if not, let whoever holds it deal
864 if (mnt->mnt_parent == mnt) {
865 spin_unlock(&vfsmount_lock);
870 * Check that it is still dead: the count should now be 2 - as
871 * contributed by the vfsmount parent and the mntget above
873 if (atomic_read(&mnt->mnt_count) == 2) {
874 struct nameidata old_nd;
876 /* delete from the namespace */
877 list_del_init(&mnt->mnt_list);
878 mnt->mnt_namespace = NULL;
879 detach_mnt(mnt, &old_nd);
880 spin_unlock(&vfsmount_lock);
881 path_release(&old_nd);
885 * Someone brought it back to life whilst we didn't have any
886 * locks held so return it to the expiration list
888 list_add_tail(&mnt->mnt_expire, mounts);
889 spin_unlock(&vfsmount_lock);
894 * process a list of expirable mountpoints with the intent of discarding any
895 * mountpoints that aren't in use and haven't been touched since last we came
898 void mark_mounts_for_expiry(struct list_head *mounts)
900 struct namespace *namespace;
901 struct vfsmount *mnt, *next;
902 LIST_HEAD(graveyard);
904 if (list_empty(mounts))
907 spin_lock(&vfsmount_lock);
909 /* extract from the expiration list every vfsmount that matches the
910 * following criteria:
911 * - only referenced by its parent vfsmount
912 * - still marked for expiry (marked on the last call here; marks are
913 * cleared by mntput())
915 list_for_each_entry_safe(mnt, next, mounts, mnt_expire) {
916 if (!xchg(&mnt->mnt_expiry_mark, 1) ||
917 atomic_read(&mnt->mnt_count) != 1)
921 list_move(&mnt->mnt_expire, &graveyard);
925 * go through the vfsmounts we've just consigned to the graveyard to
926 * - check that they're still dead
927 * - delete the vfsmount from the appropriate namespace under lock
928 * - dispose of the corpse
930 while (!list_empty(&graveyard)) {
931 mnt = list_entry(graveyard.next, struct vfsmount, mnt_expire);
932 list_del_init(&mnt->mnt_expire);
934 /* don't do anything if the namespace is dead - all the
935 * vfsmounts from it are going away anyway */
936 namespace = mnt->mnt_namespace;
937 if (!namespace || !namespace->root)
939 get_namespace(namespace);
941 spin_unlock(&vfsmount_lock);
942 down_write(&namespace->sem);
943 expire_mount(mnt, mounts);
944 up_write(&namespace->sem);
947 put_namespace(namespace);
949 spin_lock(&vfsmount_lock);
952 spin_unlock(&vfsmount_lock);
955 EXPORT_SYMBOL_GPL(mark_mounts_for_expiry);
958 * Some copy_from_user() implementations do not return the exact number of
959 * bytes remaining to copy on a fault. But copy_mount_options() requires that.
960 * Note that this function differs from copy_from_user() in that it will oops
961 * on bad values of `to', rather than returning a short copy.
964 exact_copy_from_user(void *to, const void __user *from, unsigned long n)
967 const char __user *f = from;
970 if (!access_ok(VERIFY_READ, from, n))
974 if (__get_user(c, f)) {
985 int copy_mount_options(const void __user *data, unsigned long *where)
995 if (!(page = __get_free_page(GFP_KERNEL)))
998 /* We only care that *some* data at the address the user
999 * gave us is valid. Just in case, we'll zero
1000 * the remainder of the page.
1002 /* copy_from_user cannot cross TASK_SIZE ! */
1003 size = TASK_SIZE - (unsigned long)data;
1004 if (size > PAGE_SIZE)
1007 i = size - exact_copy_from_user((void *)page, data, size);
1013 memset((char *)page + i, 0, PAGE_SIZE - i);
1019 * Flags is a 32-bit value that allows up to 31 non-fs dependent flags to
1020 * be given to the mount() call (ie: read-only, no-dev, no-suid etc).
1022 * data is a (void *) that can point to any structure up to
1023 * PAGE_SIZE-1 bytes, which can contain arbitrary fs-dependent
1024 * information (or be NULL).
1026 * Pre-0.97 versions of mount() didn't have a flags word.
1027 * When the flags word was introduced its top half was required
1028 * to have the magic value 0xC0ED, and this remained so until 2.4.0-test9.
1029 * Therefore, if this magic number is present, it carries no information
1030 * and must be discarded.
1032 long do_mount(char * dev_name, char * dir_name, char *type_page,
1033 unsigned long flags, void *data_page)
1035 struct nameidata nd;
1040 if ((flags & MS_MGC_MSK) == MS_MGC_VAL)
1041 flags &= ~MS_MGC_MSK;
1043 /* Basic sanity checks */
1045 if (!dir_name || !*dir_name || !memchr(dir_name, 0, PAGE_SIZE))
1047 if (dev_name && !memchr(dev_name, 0, PAGE_SIZE))
1051 ((char *)data_page)[PAGE_SIZE - 1] = 0;
1053 /* Separate the per-mountpoint flags */
1054 if (flags & MS_NOSUID)
1055 mnt_flags |= MNT_NOSUID;
1056 if (flags & MS_NODEV)
1057 mnt_flags |= MNT_NODEV;
1058 if (flags & MS_NOEXEC)
1059 mnt_flags |= MNT_NOEXEC;
1060 flags &= ~(MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_ACTIVE);
1062 /* ... and get the mountpoint */
1063 retval = path_lookup(dir_name, LOOKUP_FOLLOW, &nd);
1067 retval = security_sb_mount(dev_name, &nd, type_page, flags, data_page);
1071 if (flags & MS_REMOUNT)
1072 retval = do_remount(&nd, flags & ~MS_REMOUNT, mnt_flags,
1074 else if (flags & MS_BIND)
1075 retval = do_loopback(&nd, dev_name, flags & MS_REC);
1076 else if (flags & MS_MOVE)
1077 retval = do_move_mount(&nd, dev_name);
1079 retval = do_new_mount(&nd, type_page, flags, mnt_flags,
1080 dev_name, data_page);
1086 int copy_namespace(int flags, struct task_struct *tsk)
1088 struct namespace *namespace = tsk->namespace;
1089 struct namespace *new_ns;
1090 struct vfsmount *rootmnt = NULL, *pwdmnt = NULL, *altrootmnt = NULL;
1091 struct fs_struct *fs = tsk->fs;
1092 struct vfsmount *p, *q;
1097 get_namespace(namespace);
1099 if (!(flags & CLONE_NEWNS))
1102 if (!capable(CAP_SYS_ADMIN)) {
1103 put_namespace(namespace);
1107 new_ns = kmalloc(sizeof(struct namespace), GFP_KERNEL);
1111 atomic_set(&new_ns->count, 1);
1112 init_rwsem(&new_ns->sem);
1113 INIT_LIST_HEAD(&new_ns->list);
1115 down_write(&tsk->namespace->sem);
1116 /* First pass: copy the tree topology */
1117 new_ns->root = copy_tree(namespace->root, namespace->root->mnt_root);
1118 if (!new_ns->root) {
1119 up_write(&tsk->namespace->sem);
1123 spin_lock(&vfsmount_lock);
1124 list_add_tail(&new_ns->list, &new_ns->root->mnt_list);
1125 spin_unlock(&vfsmount_lock);
1128 * Second pass: switch the tsk->fs->* elements and mark new vfsmounts
1129 * as belonging to new namespace. We have already acquired a private
1130 * fs_struct, so tsk->fs->lock is not needed.
1132 p = namespace->root;
1135 q->mnt_namespace = new_ns;
1137 if (p == fs->rootmnt) {
1139 fs->rootmnt = mntget(q);
1141 if (p == fs->pwdmnt) {
1143 fs->pwdmnt = mntget(q);
1145 if (p == fs->altrootmnt) {
1147 fs->altrootmnt = mntget(q);
1150 p = next_mnt(p, namespace->root);
1151 q = next_mnt(q, new_ns->root);
1153 up_write(&tsk->namespace->sem);
1155 tsk->namespace = new_ns;
1164 put_namespace(namespace);
1168 put_namespace(namespace);
1172 asmlinkage long sys_mount(char __user * dev_name, char __user * dir_name,
1173 char __user * type, unsigned long flags,
1177 unsigned long data_page;
1178 unsigned long type_page;
1179 unsigned long dev_page;
1182 retval = copy_mount_options (type, &type_page);
1186 dir_page = getname(dir_name);
1187 retval = PTR_ERR(dir_page);
1188 if (IS_ERR(dir_page))
1191 retval = copy_mount_options (dev_name, &dev_page);
1195 retval = copy_mount_options (data, &data_page);
1200 retval = do_mount((char*)dev_page, dir_page, (char*)type_page,
1201 flags, (void*)data_page);
1203 free_page(data_page);
1206 free_page(dev_page);
1210 free_page(type_page);
1215 * Replace the fs->{rootmnt,root} with {mnt,dentry}. Put the old values.
1216 * It can block. Requires the big lock held.
1218 void set_fs_root(struct fs_struct *fs, struct vfsmount *mnt,
1219 struct dentry *dentry)
1221 struct dentry *old_root;
1222 struct vfsmount *old_rootmnt;
1223 write_lock(&fs->lock);
1224 old_root = fs->root;
1225 old_rootmnt = fs->rootmnt;
1226 fs->rootmnt = mntget(mnt);
1227 fs->root = dget(dentry);
1228 write_unlock(&fs->lock);
1231 mntput(old_rootmnt);
1236 * Replace the fs->{pwdmnt,pwd} with {mnt,dentry}. Put the old values.
1237 * It can block. Requires the big lock held.
1239 void set_fs_pwd(struct fs_struct *fs, struct vfsmount *mnt,
1240 struct dentry *dentry)
1242 struct dentry *old_pwd;
1243 struct vfsmount *old_pwdmnt;
1245 write_lock(&fs->lock);
1247 old_pwdmnt = fs->pwdmnt;
1248 fs->pwdmnt = mntget(mnt);
1249 fs->pwd = dget(dentry);
1250 write_unlock(&fs->lock);
1258 static void chroot_fs_refs(struct nameidata *old_nd, struct nameidata *new_nd)
1260 struct task_struct *g, *p;
1261 struct fs_struct *fs;
1263 read_lock(&tasklist_lock);
1264 do_each_thread(g, p) {
1268 atomic_inc(&fs->count);
1270 if (fs->root==old_nd->dentry&&fs->rootmnt==old_nd->mnt)
1271 set_fs_root(fs, new_nd->mnt, new_nd->dentry);
1272 if (fs->pwd==old_nd->dentry&&fs->pwdmnt==old_nd->mnt)
1273 set_fs_pwd(fs, new_nd->mnt, new_nd->dentry);
1277 } while_each_thread(g, p);
1278 read_unlock(&tasklist_lock);
1282 * pivot_root Semantics:
1283 * Moves the root file system of the current process to the directory put_old,
1284 * makes new_root as the new root file system of the current process, and sets
1285 * root/cwd of all processes which had them on the current root to new_root.
1288 * The new_root and put_old must be directories, and must not be on the
1289 * same file system as the current process root. The put_old must be
1290 * underneath new_root, i.e. adding a non-zero number of /.. to the string
1291 * pointed to by put_old must yield the same directory as new_root. No other
1292 * file system may be mounted on put_old. After all, new_root is a mountpoint.
1295 * - we don't move root/cwd if they are not at the root (reason: if something
1296 * cared enough to change them, it's probably wrong to force them elsewhere)
1297 * - it's okay to pick a root that isn't the root of a file system, e.g.
1298 * /nfs/my_root where /nfs is the mount point. It must be a mountpoint,
1299 * though, so you may need to say mount --bind /nfs/my_root /nfs/my_root
1303 asmlinkage long sys_pivot_root(const char __user *new_root, const char __user *put_old)
1305 struct vfsmount *tmp;
1306 struct nameidata new_nd, old_nd, parent_nd, root_parent, user_nd;
1309 if (!capable(CAP_SYS_ADMIN))
1314 error = __user_walk(new_root, LOOKUP_FOLLOW|LOOKUP_DIRECTORY, &new_nd);
1318 if (!check_mnt(new_nd.mnt))
1321 error = __user_walk(put_old, LOOKUP_FOLLOW|LOOKUP_DIRECTORY, &old_nd);
1325 error = security_sb_pivotroot(&old_nd, &new_nd);
1327 path_release(&old_nd);
1331 read_lock(¤t->fs->lock);
1332 user_nd.mnt = mntget(current->fs->rootmnt);
1333 user_nd.dentry = dget(current->fs->root);
1334 read_unlock(¤t->fs->lock);
1335 down_write(¤t->namespace->sem);
1336 down(&old_nd.dentry->d_inode->i_sem);
1338 if (!check_mnt(user_nd.mnt))
1341 if (IS_DEADDIR(new_nd.dentry->d_inode))
1343 if (d_unhashed(new_nd.dentry) && !IS_ROOT(new_nd.dentry))
1345 if (d_unhashed(old_nd.dentry) && !IS_ROOT(old_nd.dentry))
1348 if (new_nd.mnt == user_nd.mnt || old_nd.mnt == user_nd.mnt)
1349 goto out2; /* loop, on the same file system */
1351 if (user_nd.mnt->mnt_root != user_nd.dentry)
1352 goto out2; /* not a mountpoint */
1353 if (user_nd.mnt->mnt_parent == user_nd.mnt)
1354 goto out2; /* not attached */
1355 if (new_nd.mnt->mnt_root != new_nd.dentry)
1356 goto out2; /* not a mountpoint */
1357 if (new_nd.mnt->mnt_parent == new_nd.mnt)
1358 goto out2; /* not attached */
1359 tmp = old_nd.mnt; /* make sure we can reach put_old from new_root */
1360 spin_lock(&vfsmount_lock);
1361 if (tmp != new_nd.mnt) {
1363 if (tmp->mnt_parent == tmp)
1364 goto out3; /* already mounted on put_old */
1365 if (tmp->mnt_parent == new_nd.mnt)
1367 tmp = tmp->mnt_parent;
1369 if (!is_subdir(tmp->mnt_mountpoint, new_nd.dentry))
1371 } else if (!is_subdir(old_nd.dentry, new_nd.dentry))
1373 detach_mnt(new_nd.mnt, &parent_nd);
1374 detach_mnt(user_nd.mnt, &root_parent);
1375 attach_mnt(user_nd.mnt, &old_nd); /* mount old root on put_old */
1376 attach_mnt(new_nd.mnt, &root_parent); /* mount new_root on / */
1377 spin_unlock(&vfsmount_lock);
1378 chroot_fs_refs(&user_nd, &new_nd);
1379 security_sb_post_pivotroot(&user_nd, &new_nd);
1381 path_release(&root_parent);
1382 path_release(&parent_nd);
1384 up(&old_nd.dentry->d_inode->i_sem);
1385 up_write(¤t->namespace->sem);
1386 path_release(&user_nd);
1387 path_release(&old_nd);
1389 path_release(&new_nd);
1394 spin_unlock(&vfsmount_lock);
1398 static void __init init_mount_tree(void)
1400 struct vfsmount *mnt;
1401 struct namespace *namespace;
1402 struct task_struct *g, *p;
1404 mnt = do_kern_mount("rootfs", 0, "rootfs", NULL);
1406 panic("Can't create rootfs");
1407 namespace = kmalloc(sizeof(*namespace), GFP_KERNEL);
1409 panic("Can't allocate initial namespace");
1410 atomic_set(&namespace->count, 1);
1411 INIT_LIST_HEAD(&namespace->list);
1412 init_rwsem(&namespace->sem);
1413 list_add(&mnt->mnt_list, &namespace->list);
1414 namespace->root = mnt;
1415 mnt->mnt_namespace = namespace;
1417 init_task.namespace = namespace;
1418 read_lock(&tasklist_lock);
1419 do_each_thread(g, p) {
1420 get_namespace(namespace);
1421 p->namespace = namespace;
1422 } while_each_thread(g, p);
1423 read_unlock(&tasklist_lock);
1425 set_fs_pwd(current->fs, namespace->root, namespace->root->mnt_root);
1426 set_fs_root(current->fs, namespace->root, namespace->root->mnt_root);
1429 void __init mnt_init(unsigned long mempages)
1431 struct list_head *d;
1432 unsigned int nr_hash;
1435 mnt_cache = kmem_cache_create("mnt_cache", sizeof(struct vfsmount),
1436 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
1438 mount_hashtable = (struct list_head *)
1439 __get_free_page(GFP_ATOMIC);
1441 if (!mount_hashtable)
1442 panic("Failed to allocate mount hash table\n");
1445 * Find the power-of-two list-heads that can fit into the allocation..
1446 * We don't guarantee that "sizeof(struct list_head)" is necessarily
1449 nr_hash = PAGE_SIZE / sizeof(struct list_head);
1453 } while ((nr_hash >> hash_bits) != 0);
1457 * Re-calculate the actual number of entries and the mask
1458 * from the number of bits we can fit.
1460 nr_hash = 1UL << hash_bits;
1461 hash_mask = nr_hash-1;
1463 printk("Mount-cache hash table entries: %d\n", nr_hash);
1465 /* And initialize the newly allocated array */
1466 d = mount_hashtable;
1478 void __put_namespace(struct namespace *namespace)
1480 struct vfsmount *root = namespace->root;
1481 namespace->root = NULL;
1482 spin_unlock(&vfsmount_lock);
1483 down_write(&namespace->sem);
1484 spin_lock(&vfsmount_lock);
1486 spin_unlock(&vfsmount_lock);
1487 up_write(&namespace->sem);