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>
29 extern int __init
init_rootfs(void);
31 #define CL_EXPIRE 0x01
34 extern int __init
sysfs_init(void);
36 static inline int sysfs_init(void)
42 /* spinlock for vfsmount related operations, inplace of dcache_lock */
43 __cacheline_aligned_in_smp
DEFINE_SPINLOCK(vfsmount_lock
);
47 static struct list_head
*mount_hashtable
;
48 static int hash_mask __read_mostly
, hash_bits __read_mostly
;
49 static kmem_cache_t
*mnt_cache
;
50 static struct rw_semaphore namespace_sem
;
52 static inline unsigned long hash(struct vfsmount
*mnt
, struct dentry
*dentry
)
54 unsigned long tmp
= ((unsigned long)mnt
/ L1_CACHE_BYTES
);
55 tmp
+= ((unsigned long)dentry
/ L1_CACHE_BYTES
);
56 tmp
= tmp
+ (tmp
>> hash_bits
);
57 return tmp
& hash_mask
;
60 struct vfsmount
*alloc_vfsmnt(const char *name
)
62 struct vfsmount
*mnt
= kmem_cache_alloc(mnt_cache
, GFP_KERNEL
);
64 memset(mnt
, 0, sizeof(struct vfsmount
));
65 atomic_set(&mnt
->mnt_count
, 1);
66 INIT_LIST_HEAD(&mnt
->mnt_hash
);
67 INIT_LIST_HEAD(&mnt
->mnt_child
);
68 INIT_LIST_HEAD(&mnt
->mnt_mounts
);
69 INIT_LIST_HEAD(&mnt
->mnt_list
);
70 INIT_LIST_HEAD(&mnt
->mnt_expire
);
72 int size
= strlen(name
) + 1;
73 char *newname
= kmalloc(size
, GFP_KERNEL
);
75 memcpy(newname
, name
, size
);
76 mnt
->mnt_devname
= newname
;
83 void free_vfsmnt(struct vfsmount
*mnt
)
85 kfree(mnt
->mnt_devname
);
86 kmem_cache_free(mnt_cache
, mnt
);
90 * Now, lookup_mnt increments the ref count before returning
91 * the vfsmount struct.
93 struct vfsmount
*lookup_mnt(struct vfsmount
*mnt
, struct dentry
*dentry
)
95 struct list_head
*head
= mount_hashtable
+ hash(mnt
, dentry
);
96 struct list_head
*tmp
= head
;
97 struct vfsmount
*p
, *found
= NULL
;
99 spin_lock(&vfsmount_lock
);
105 p
= list_entry(tmp
, struct vfsmount
, mnt_hash
);
106 if (p
->mnt_parent
== mnt
&& p
->mnt_mountpoint
== dentry
) {
111 spin_unlock(&vfsmount_lock
);
115 static inline int check_mnt(struct vfsmount
*mnt
)
117 return mnt
->mnt_namespace
== current
->namespace;
120 static void touch_namespace(struct namespace *ns
)
124 wake_up_interruptible(&ns
->poll
);
128 static void __touch_namespace(struct namespace *ns
)
130 if (ns
&& ns
->event
!= event
) {
132 wake_up_interruptible(&ns
->poll
);
136 static void detach_mnt(struct vfsmount
*mnt
, struct nameidata
*old_nd
)
138 old_nd
->dentry
= mnt
->mnt_mountpoint
;
139 old_nd
->mnt
= mnt
->mnt_parent
;
140 mnt
->mnt_parent
= mnt
;
141 mnt
->mnt_mountpoint
= mnt
->mnt_root
;
142 list_del_init(&mnt
->mnt_child
);
143 list_del_init(&mnt
->mnt_hash
);
144 old_nd
->dentry
->d_mounted
--;
147 static void attach_mnt(struct vfsmount
*mnt
, struct nameidata
*nd
)
149 mnt
->mnt_parent
= mntget(nd
->mnt
);
150 mnt
->mnt_mountpoint
= dget(nd
->dentry
);
151 list_add(&mnt
->mnt_hash
, mount_hashtable
+ hash(nd
->mnt
, nd
->dentry
));
152 list_add_tail(&mnt
->mnt_child
, &nd
->mnt
->mnt_mounts
);
153 nd
->dentry
->d_mounted
++;
156 static struct vfsmount
*next_mnt(struct vfsmount
*p
, struct vfsmount
*root
)
158 struct list_head
*next
= p
->mnt_mounts
.next
;
159 if (next
== &p
->mnt_mounts
) {
163 next
= p
->mnt_child
.next
;
164 if (next
!= &p
->mnt_parent
->mnt_mounts
)
169 return list_entry(next
, struct vfsmount
, mnt_child
);
172 static struct vfsmount
*clone_mnt(struct vfsmount
*old
, struct dentry
*root
,
175 struct super_block
*sb
= old
->mnt_sb
;
176 struct vfsmount
*mnt
= alloc_vfsmnt(old
->mnt_devname
);
179 mnt
->mnt_flags
= old
->mnt_flags
;
180 atomic_inc(&sb
->s_active
);
182 mnt
->mnt_root
= dget(root
);
183 mnt
->mnt_mountpoint
= mnt
->mnt_root
;
184 mnt
->mnt_parent
= mnt
;
185 mnt
->mnt_namespace
= current
->namespace;
187 /* stick the duplicate mount on the same expiry list
188 * as the original if that was on one */
189 if (flag
& CL_EXPIRE
) {
190 spin_lock(&vfsmount_lock
);
191 if (!list_empty(&old
->mnt_expire
))
192 list_add(&mnt
->mnt_expire
, &old
->mnt_expire
);
193 spin_unlock(&vfsmount_lock
);
199 static inline void __mntput(struct vfsmount
*mnt
)
201 struct super_block
*sb
= mnt
->mnt_sb
;
204 deactivate_super(sb
);
207 void mntput_no_expire(struct vfsmount
*mnt
)
210 if (atomic_dec_and_lock(&mnt
->mnt_count
, &vfsmount_lock
)) {
211 if (likely(!mnt
->mnt_pinned
)) {
212 spin_unlock(&vfsmount_lock
);
216 atomic_add(mnt
->mnt_pinned
+ 1, &mnt
->mnt_count
);
218 spin_unlock(&vfsmount_lock
);
219 acct_auto_close_mnt(mnt
);
220 security_sb_umount_close(mnt
);
225 EXPORT_SYMBOL(mntput_no_expire
);
227 void mnt_pin(struct vfsmount
*mnt
)
229 spin_lock(&vfsmount_lock
);
231 spin_unlock(&vfsmount_lock
);
234 EXPORT_SYMBOL(mnt_pin
);
236 void mnt_unpin(struct vfsmount
*mnt
)
238 spin_lock(&vfsmount_lock
);
239 if (mnt
->mnt_pinned
) {
240 atomic_inc(&mnt
->mnt_count
);
243 spin_unlock(&vfsmount_lock
);
246 EXPORT_SYMBOL(mnt_unpin
);
249 static void *m_start(struct seq_file
*m
, loff_t
*pos
)
251 struct namespace *n
= m
->private;
255 down_read(&namespace_sem
);
256 list_for_each(p
, &n
->list
)
258 return list_entry(p
, struct vfsmount
, mnt_list
);
262 static void *m_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
264 struct namespace *n
= m
->private;
265 struct list_head
*p
= ((struct vfsmount
*)v
)->mnt_list
.next
;
267 return p
== &n
->list
? NULL
: list_entry(p
, struct vfsmount
, mnt_list
);
270 static void m_stop(struct seq_file
*m
, void *v
)
272 up_read(&namespace_sem
);
275 static inline void mangle(struct seq_file
*m
, const char *s
)
277 seq_escape(m
, s
, " \t\n\\");
280 static int show_vfsmnt(struct seq_file
*m
, void *v
)
282 struct vfsmount
*mnt
= v
;
284 static struct proc_fs_info
{
288 { MS_SYNCHRONOUS
, ",sync" },
289 { MS_DIRSYNC
, ",dirsync" },
290 { MS_MANDLOCK
, ",mand" },
291 { MS_NOATIME
, ",noatime" },
292 { MS_NODIRATIME
, ",nodiratime" },
295 static struct proc_fs_info mnt_info
[] = {
296 { MNT_NOSUID
, ",nosuid" },
297 { MNT_NODEV
, ",nodev" },
298 { MNT_NOEXEC
, ",noexec" },
301 struct proc_fs_info
*fs_infop
;
303 mangle(m
, mnt
->mnt_devname
? mnt
->mnt_devname
: "none");
305 seq_path(m
, mnt
, mnt
->mnt_root
, " \t\n\\");
307 mangle(m
, mnt
->mnt_sb
->s_type
->name
);
308 seq_puts(m
, mnt
->mnt_sb
->s_flags
& MS_RDONLY
? " ro" : " rw");
309 for (fs_infop
= fs_info
; fs_infop
->flag
; fs_infop
++) {
310 if (mnt
->mnt_sb
->s_flags
& fs_infop
->flag
)
311 seq_puts(m
, fs_infop
->str
);
313 for (fs_infop
= mnt_info
; fs_infop
->flag
; fs_infop
++) {
314 if (mnt
->mnt_flags
& fs_infop
->flag
)
315 seq_puts(m
, fs_infop
->str
);
317 if (mnt
->mnt_sb
->s_op
->show_options
)
318 err
= mnt
->mnt_sb
->s_op
->show_options(m
, mnt
);
319 seq_puts(m
, " 0 0\n");
323 struct seq_operations mounts_op
= {
331 * may_umount_tree - check if a mount tree is busy
332 * @mnt: root of mount tree
334 * This is called to check if a tree of mounts has any
335 * open files, pwds, chroots or sub mounts that are
338 int may_umount_tree(struct vfsmount
*mnt
)
341 int minimum_refs
= 0;
344 spin_lock(&vfsmount_lock
);
345 for (p
= mnt
; p
; p
= next_mnt(p
, mnt
)) {
346 actual_refs
+= atomic_read(&p
->mnt_count
);
349 spin_unlock(&vfsmount_lock
);
351 if (actual_refs
> minimum_refs
)
357 EXPORT_SYMBOL(may_umount_tree
);
360 * may_umount - check if a mount point is busy
361 * @mnt: root of mount
363 * This is called to check if a mount point has any
364 * open files, pwds, chroots or sub mounts. If the
365 * mount has sub mounts this will return busy
366 * regardless of whether the sub mounts are busy.
368 * Doesn't take quota and stuff into account. IOW, in some cases it will
369 * give false negatives. The main reason why it's here is that we need
370 * a non-destructive way to look for easily umountable filesystems.
372 int may_umount(struct vfsmount
*mnt
)
374 if (atomic_read(&mnt
->mnt_count
) > 2)
379 EXPORT_SYMBOL(may_umount
);
381 static void release_mounts(struct list_head
*head
)
383 struct vfsmount
*mnt
;
384 while(!list_empty(head
)) {
385 mnt
= list_entry(head
->next
, struct vfsmount
, mnt_hash
);
386 list_del_init(&mnt
->mnt_hash
);
387 if (mnt
->mnt_parent
!= mnt
) {
388 struct dentry
*dentry
;
390 spin_lock(&vfsmount_lock
);
391 dentry
= mnt
->mnt_mountpoint
;
393 mnt
->mnt_mountpoint
= mnt
->mnt_root
;
394 mnt
->mnt_parent
= mnt
;
395 spin_unlock(&vfsmount_lock
);
403 static void umount_tree(struct vfsmount
*mnt
, struct list_head
*kill
)
407 for (p
= mnt
; p
; p
= next_mnt(p
, mnt
)) {
408 list_del(&p
->mnt_hash
);
409 list_add(&p
->mnt_hash
, kill
);
412 list_for_each_entry(p
, kill
, mnt_hash
) {
413 list_del_init(&p
->mnt_expire
);
414 list_del_init(&p
->mnt_list
);
415 __touch_namespace(p
->mnt_namespace
);
416 p
->mnt_namespace
= NULL
;
417 list_del_init(&p
->mnt_child
);
418 if (p
->mnt_parent
!= p
)
419 mnt
->mnt_mountpoint
->d_mounted
--;
423 static int do_umount(struct vfsmount
*mnt
, int flags
)
425 struct super_block
*sb
= mnt
->mnt_sb
;
427 LIST_HEAD(umount_list
);
429 retval
= security_sb_umount(mnt
, flags
);
434 * Allow userspace to request a mountpoint be expired rather than
435 * unmounting unconditionally. Unmount only happens if:
436 * (1) the mark is already set (the mark is cleared by mntput())
437 * (2) the usage count == 1 [parent vfsmount] + 1 [sys_umount]
439 if (flags
& MNT_EXPIRE
) {
440 if (mnt
== current
->fs
->rootmnt
||
441 flags
& (MNT_FORCE
| MNT_DETACH
))
444 if (atomic_read(&mnt
->mnt_count
) != 2)
447 if (!xchg(&mnt
->mnt_expiry_mark
, 1))
452 * If we may have to abort operations to get out of this
453 * mount, and they will themselves hold resources we must
454 * allow the fs to do things. In the Unix tradition of
455 * 'Gee thats tricky lets do it in userspace' the umount_begin
456 * might fail to complete on the first run through as other tasks
457 * must return, and the like. Thats for the mount program to worry
458 * about for the moment.
462 if ((flags
& MNT_FORCE
) && sb
->s_op
->umount_begin
)
463 sb
->s_op
->umount_begin(sb
);
467 * No sense to grab the lock for this test, but test itself looks
468 * somewhat bogus. Suggestions for better replacement?
469 * Ho-hum... In principle, we might treat that as umount + switch
470 * to rootfs. GC would eventually take care of the old vfsmount.
471 * Actually it makes sense, especially if rootfs would contain a
472 * /reboot - static binary that would close all descriptors and
473 * call reboot(9). Then init(8) could umount root and exec /reboot.
475 if (mnt
== current
->fs
->rootmnt
&& !(flags
& MNT_DETACH
)) {
477 * Special case for "unmounting" root ...
478 * we just try to remount it readonly.
480 down_write(&sb
->s_umount
);
481 if (!(sb
->s_flags
& MS_RDONLY
)) {
484 retval
= do_remount_sb(sb
, MS_RDONLY
, NULL
, 0);
487 up_write(&sb
->s_umount
);
491 down_write(&namespace_sem
);
492 spin_lock(&vfsmount_lock
);
496 if (atomic_read(&mnt
->mnt_count
) == 2 || flags
& MNT_DETACH
) {
497 if (!list_empty(&mnt
->mnt_list
))
498 umount_tree(mnt
, &umount_list
);
501 spin_unlock(&vfsmount_lock
);
503 security_sb_umount_busy(mnt
);
504 up_write(&namespace_sem
);
505 release_mounts(&umount_list
);
510 * Now umount can handle mount points as well as block devices.
511 * This is important for filesystems which use unnamed block devices.
513 * We now support a flag for forced unmount like the other 'big iron'
514 * unixes. Our API is identical to OSF/1 to avoid making a mess of AMD
517 asmlinkage
long sys_umount(char __user
* name
, int flags
)
522 retval
= __user_walk(name
, LOOKUP_FOLLOW
, &nd
);
526 if (nd
.dentry
!= nd
.mnt
->mnt_root
)
528 if (!check_mnt(nd
.mnt
))
532 if (!capable(CAP_SYS_ADMIN
))
535 retval
= do_umount(nd
.mnt
, flags
);
537 path_release_on_umount(&nd
);
542 #ifdef __ARCH_WANT_SYS_OLDUMOUNT
545 * The 2.0 compatible umount. No flags.
547 asmlinkage
long sys_oldumount(char __user
* name
)
549 return sys_umount(name
, 0);
554 static int mount_is_safe(struct nameidata
*nd
)
556 if (capable(CAP_SYS_ADMIN
))
560 if (S_ISLNK(nd
->dentry
->d_inode
->i_mode
))
562 if (nd
->dentry
->d_inode
->i_mode
& S_ISVTX
) {
563 if (current
->uid
!= nd
->dentry
->d_inode
->i_uid
)
566 if (permission(nd
->dentry
->d_inode
, MAY_WRITE
, nd
))
572 static int lives_below_in_same_fs(struct dentry
*d
, struct dentry
*dentry
)
577 if (d
== NULL
|| d
== d
->d_parent
)
583 static struct vfsmount
*copy_tree(struct vfsmount
*mnt
, struct dentry
*dentry
,
586 struct vfsmount
*res
, *p
, *q
, *r
, *s
;
589 res
= q
= clone_mnt(mnt
, dentry
, flag
);
592 q
->mnt_mountpoint
= mnt
->mnt_mountpoint
;
595 list_for_each_entry(r
, &mnt
->mnt_mounts
, mnt_child
) {
596 if (!lives_below_in_same_fs(r
->mnt_mountpoint
, dentry
))
599 for (s
= r
; s
; s
= next_mnt(s
, r
)) {
600 while (p
!= s
->mnt_parent
) {
606 nd
.dentry
= p
->mnt_mountpoint
;
607 q
= clone_mnt(p
, p
->mnt_root
, flag
);
610 spin_lock(&vfsmount_lock
);
611 list_add_tail(&q
->mnt_list
, &res
->mnt_list
);
613 spin_unlock(&vfsmount_lock
);
619 LIST_HEAD(umount_list
);
620 spin_lock(&vfsmount_lock
);
621 umount_tree(res
, &umount_list
);
622 spin_unlock(&vfsmount_lock
);
623 release_mounts(&umount_list
);
628 static int graft_tree(struct vfsmount
*mnt
, struct nameidata
*nd
)
631 if (mnt
->mnt_sb
->s_flags
& MS_NOUSER
)
634 if (S_ISDIR(nd
->dentry
->d_inode
->i_mode
) !=
635 S_ISDIR(mnt
->mnt_root
->d_inode
->i_mode
))
639 down(&nd
->dentry
->d_inode
->i_sem
);
640 if (IS_DEADDIR(nd
->dentry
->d_inode
))
643 err
= security_sb_check_sb(mnt
, nd
);
648 spin_lock(&vfsmount_lock
);
649 if (IS_ROOT(nd
->dentry
) || !d_unhashed(nd
->dentry
)) {
650 struct list_head head
;
653 list_add_tail(&head
, &mnt
->mnt_list
);
654 list_splice(&head
, current
->namespace->list
.prev
);
656 touch_namespace(current
->namespace);
658 spin_unlock(&vfsmount_lock
);
660 up(&nd
->dentry
->d_inode
->i_sem
);
662 security_sb_post_addmount(mnt
, nd
);
667 * recursively change the type of the mountpoint.
669 static int do_change_type(struct nameidata
*nd
, int flag
)
671 struct vfsmount
*m
, *mnt
= nd
->mnt
;
672 int recurse
= flag
& MS_REC
;
673 int type
= flag
& ~MS_REC
;
675 if (nd
->dentry
!= nd
->mnt
->mnt_root
)
678 down_write(&namespace_sem
);
679 spin_lock(&vfsmount_lock
);
680 for (m
= mnt
; m
; m
= (recurse
? next_mnt(m
, mnt
) : NULL
))
681 change_mnt_propagation(m
, type
);
682 spin_unlock(&vfsmount_lock
);
683 up_write(&namespace_sem
);
690 static int do_loopback(struct nameidata
*nd
, char *old_name
, int recurse
)
692 struct nameidata old_nd
;
693 struct vfsmount
*mnt
= NULL
;
694 int err
= mount_is_safe(nd
);
697 if (!old_name
|| !*old_name
)
699 err
= path_lookup(old_name
, LOOKUP_FOLLOW
, &old_nd
);
703 down_write(&namespace_sem
);
705 if (!check_mnt(nd
->mnt
) || !check_mnt(old_nd
.mnt
))
710 mnt
= copy_tree(old_nd
.mnt
, old_nd
.dentry
, 0);
712 mnt
= clone_mnt(old_nd
.mnt
, old_nd
.dentry
, 0);
717 err
= graft_tree(mnt
, nd
);
719 LIST_HEAD(umount_list
);
720 spin_lock(&vfsmount_lock
);
721 umount_tree(mnt
, &umount_list
);
722 spin_unlock(&vfsmount_lock
);
723 release_mounts(&umount_list
);
727 up_write(&namespace_sem
);
728 path_release(&old_nd
);
733 * change filesystem flags. dir should be a physical root of filesystem.
734 * If you've mounted a non-root directory somewhere and want to do remount
735 * on it - tough luck.
737 static int do_remount(struct nameidata
*nd
, int flags
, int mnt_flags
,
741 struct super_block
*sb
= nd
->mnt
->mnt_sb
;
743 if (!capable(CAP_SYS_ADMIN
))
746 if (!check_mnt(nd
->mnt
))
749 if (nd
->dentry
!= nd
->mnt
->mnt_root
)
752 down_write(&sb
->s_umount
);
753 err
= do_remount_sb(sb
, flags
, data
, 0);
755 nd
->mnt
->mnt_flags
= mnt_flags
;
756 up_write(&sb
->s_umount
);
758 security_sb_post_remount(nd
->mnt
, flags
, data
);
762 static int do_move_mount(struct nameidata
*nd
, char *old_name
)
764 struct nameidata old_nd
, parent_nd
;
767 if (!capable(CAP_SYS_ADMIN
))
769 if (!old_name
|| !*old_name
)
771 err
= path_lookup(old_name
, LOOKUP_FOLLOW
, &old_nd
);
775 down_write(&namespace_sem
);
776 while (d_mountpoint(nd
->dentry
) && follow_down(&nd
->mnt
, &nd
->dentry
))
779 if (!check_mnt(nd
->mnt
) || !check_mnt(old_nd
.mnt
))
783 down(&nd
->dentry
->d_inode
->i_sem
);
784 if (IS_DEADDIR(nd
->dentry
->d_inode
))
787 spin_lock(&vfsmount_lock
);
788 if (!IS_ROOT(nd
->dentry
) && d_unhashed(nd
->dentry
))
792 if (old_nd
.dentry
!= old_nd
.mnt
->mnt_root
)
795 if (old_nd
.mnt
== old_nd
.mnt
->mnt_parent
)
798 if (S_ISDIR(nd
->dentry
->d_inode
->i_mode
) !=
799 S_ISDIR(old_nd
.dentry
->d_inode
->i_mode
))
803 for (p
= nd
->mnt
; p
->mnt_parent
!= p
; p
= p
->mnt_parent
)
808 detach_mnt(old_nd
.mnt
, &parent_nd
);
809 attach_mnt(old_nd
.mnt
, nd
);
810 touch_namespace(current
->namespace);
812 /* if the mount is moved, it should no longer be expire
814 list_del_init(&old_nd
.mnt
->mnt_expire
);
816 spin_unlock(&vfsmount_lock
);
818 up(&nd
->dentry
->d_inode
->i_sem
);
820 up_write(&namespace_sem
);
822 path_release(&parent_nd
);
823 path_release(&old_nd
);
828 * create a new mount for userspace and request it to be added into the
831 static int do_new_mount(struct nameidata
*nd
, char *type
, int flags
,
832 int mnt_flags
, char *name
, void *data
)
834 struct vfsmount
*mnt
;
836 if (!type
|| !memchr(type
, 0, PAGE_SIZE
))
839 /* we need capabilities... */
840 if (!capable(CAP_SYS_ADMIN
))
843 mnt
= do_kern_mount(type
, flags
, name
, data
);
847 return do_add_mount(mnt
, nd
, mnt_flags
, NULL
);
851 * add a mount into a namespace's mount tree
852 * - provide the option of adding the new mount to an expiration list
854 int do_add_mount(struct vfsmount
*newmnt
, struct nameidata
*nd
,
855 int mnt_flags
, struct list_head
*fslist
)
859 down_write(&namespace_sem
);
860 /* Something was mounted here while we slept */
861 while (d_mountpoint(nd
->dentry
) && follow_down(&nd
->mnt
, &nd
->dentry
))
864 if (!check_mnt(nd
->mnt
))
867 /* Refuse the same filesystem on the same mount point */
869 if (nd
->mnt
->mnt_sb
== newmnt
->mnt_sb
&&
870 nd
->mnt
->mnt_root
== nd
->dentry
)
874 if (S_ISLNK(newmnt
->mnt_root
->d_inode
->i_mode
))
877 newmnt
->mnt_flags
= mnt_flags
;
878 if ((err
= graft_tree(newmnt
, nd
)))
882 /* add to the specified expiration list */
883 spin_lock(&vfsmount_lock
);
884 list_add_tail(&newmnt
->mnt_expire
, fslist
);
885 spin_unlock(&vfsmount_lock
);
887 up_write(&namespace_sem
);
891 up_write(&namespace_sem
);
896 EXPORT_SYMBOL_GPL(do_add_mount
);
898 static void expire_mount(struct vfsmount
*mnt
, struct list_head
*mounts
,
899 struct list_head
*umounts
)
901 spin_lock(&vfsmount_lock
);
904 * Check if mount is still attached, if not, let whoever holds it deal
907 if (mnt
->mnt_parent
== mnt
) {
908 spin_unlock(&vfsmount_lock
);
913 * Check that it is still dead: the count should now be 2 - as
914 * contributed by the vfsmount parent and the mntget above
916 if (atomic_read(&mnt
->mnt_count
) == 2) {
917 /* delete from the namespace */
918 touch_namespace(mnt
->mnt_namespace
);
919 list_del_init(&mnt
->mnt_list
);
920 mnt
->mnt_namespace
= NULL
;
921 umount_tree(mnt
, umounts
);
922 spin_unlock(&vfsmount_lock
);
925 * Someone brought it back to life whilst we didn't have any
926 * locks held so return it to the expiration list
928 list_add_tail(&mnt
->mnt_expire
, mounts
);
929 spin_unlock(&vfsmount_lock
);
934 * process a list of expirable mountpoints with the intent of discarding any
935 * mountpoints that aren't in use and haven't been touched since last we came
938 void mark_mounts_for_expiry(struct list_head
*mounts
)
940 struct namespace *namespace;
941 struct vfsmount
*mnt
, *next
;
942 LIST_HEAD(graveyard
);
944 if (list_empty(mounts
))
947 spin_lock(&vfsmount_lock
);
949 /* extract from the expiration list every vfsmount that matches the
950 * following criteria:
951 * - only referenced by its parent vfsmount
952 * - still marked for expiry (marked on the last call here; marks are
953 * cleared by mntput())
955 list_for_each_entry_safe(mnt
, next
, mounts
, mnt_expire
) {
956 if (!xchg(&mnt
->mnt_expiry_mark
, 1) ||
957 atomic_read(&mnt
->mnt_count
) != 1)
961 list_move(&mnt
->mnt_expire
, &graveyard
);
965 * go through the vfsmounts we've just consigned to the graveyard to
966 * - check that they're still dead
967 * - delete the vfsmount from the appropriate namespace under lock
968 * - dispose of the corpse
970 while (!list_empty(&graveyard
)) {
972 mnt
= list_entry(graveyard
.next
, struct vfsmount
, mnt_expire
);
973 list_del_init(&mnt
->mnt_expire
);
975 /* don't do anything if the namespace is dead - all the
976 * vfsmounts from it are going away anyway */
977 namespace = mnt
->mnt_namespace
;
978 if (!namespace || !namespace->root
)
980 get_namespace(namespace);
982 spin_unlock(&vfsmount_lock
);
983 down_write(&namespace_sem
);
984 expire_mount(mnt
, mounts
, &umounts
);
985 up_write(&namespace_sem
);
986 release_mounts(&umounts
);
988 put_namespace(namespace);
989 spin_lock(&vfsmount_lock
);
992 spin_unlock(&vfsmount_lock
);
995 EXPORT_SYMBOL_GPL(mark_mounts_for_expiry
);
998 * Some copy_from_user() implementations do not return the exact number of
999 * bytes remaining to copy on a fault. But copy_mount_options() requires that.
1000 * Note that this function differs from copy_from_user() in that it will oops
1001 * on bad values of `to', rather than returning a short copy.
1003 static long exact_copy_from_user(void *to
, const void __user
* from
,
1007 const char __user
*f
= from
;
1010 if (!access_ok(VERIFY_READ
, from
, n
))
1014 if (__get_user(c
, f
)) {
1025 int copy_mount_options(const void __user
* data
, unsigned long *where
)
1035 if (!(page
= __get_free_page(GFP_KERNEL
)))
1038 /* We only care that *some* data at the address the user
1039 * gave us is valid. Just in case, we'll zero
1040 * the remainder of the page.
1042 /* copy_from_user cannot cross TASK_SIZE ! */
1043 size
= TASK_SIZE
- (unsigned long)data
;
1044 if (size
> PAGE_SIZE
)
1047 i
= size
- exact_copy_from_user((void *)page
, data
, size
);
1053 memset((char *)page
+ i
, 0, PAGE_SIZE
- i
);
1059 * Flags is a 32-bit value that allows up to 31 non-fs dependent flags to
1060 * be given to the mount() call (ie: read-only, no-dev, no-suid etc).
1062 * data is a (void *) that can point to any structure up to
1063 * PAGE_SIZE-1 bytes, which can contain arbitrary fs-dependent
1064 * information (or be NULL).
1066 * Pre-0.97 versions of mount() didn't have a flags word.
1067 * When the flags word was introduced its top half was required
1068 * to have the magic value 0xC0ED, and this remained so until 2.4.0-test9.
1069 * Therefore, if this magic number is present, it carries no information
1070 * and must be discarded.
1072 long do_mount(char *dev_name
, char *dir_name
, char *type_page
,
1073 unsigned long flags
, void *data_page
)
1075 struct nameidata nd
;
1080 if ((flags
& MS_MGC_MSK
) == MS_MGC_VAL
)
1081 flags
&= ~MS_MGC_MSK
;
1083 /* Basic sanity checks */
1085 if (!dir_name
|| !*dir_name
|| !memchr(dir_name
, 0, PAGE_SIZE
))
1087 if (dev_name
&& !memchr(dev_name
, 0, PAGE_SIZE
))
1091 ((char *)data_page
)[PAGE_SIZE
- 1] = 0;
1093 /* Separate the per-mountpoint flags */
1094 if (flags
& MS_NOSUID
)
1095 mnt_flags
|= MNT_NOSUID
;
1096 if (flags
& MS_NODEV
)
1097 mnt_flags
|= MNT_NODEV
;
1098 if (flags
& MS_NOEXEC
)
1099 mnt_flags
|= MNT_NOEXEC
;
1100 flags
&= ~(MS_NOSUID
| MS_NOEXEC
| MS_NODEV
| MS_ACTIVE
);
1102 /* ... and get the mountpoint */
1103 retval
= path_lookup(dir_name
, LOOKUP_FOLLOW
, &nd
);
1107 retval
= security_sb_mount(dev_name
, &nd
, type_page
, flags
, data_page
);
1111 if (flags
& MS_REMOUNT
)
1112 retval
= do_remount(&nd
, flags
& ~MS_REMOUNT
, mnt_flags
,
1114 else if (flags
& MS_BIND
)
1115 retval
= do_loopback(&nd
, dev_name
, flags
& MS_REC
);
1116 else if (flags
& MS_PRIVATE
)
1117 retval
= do_change_type(&nd
, flags
);
1118 else if (flags
& MS_MOVE
)
1119 retval
= do_move_mount(&nd
, dev_name
);
1121 retval
= do_new_mount(&nd
, type_page
, flags
, mnt_flags
,
1122 dev_name
, data_page
);
1128 int copy_namespace(int flags
, struct task_struct
*tsk
)
1130 struct namespace *namespace = tsk
->namespace;
1131 struct namespace *new_ns
;
1132 struct vfsmount
*rootmnt
= NULL
, *pwdmnt
= NULL
, *altrootmnt
= NULL
;
1133 struct fs_struct
*fs
= tsk
->fs
;
1134 struct vfsmount
*p
, *q
;
1139 get_namespace(namespace);
1141 if (!(flags
& CLONE_NEWNS
))
1144 if (!capable(CAP_SYS_ADMIN
)) {
1145 put_namespace(namespace);
1149 new_ns
= kmalloc(sizeof(struct namespace), GFP_KERNEL
);
1153 atomic_set(&new_ns
->count
, 1);
1154 INIT_LIST_HEAD(&new_ns
->list
);
1155 init_waitqueue_head(&new_ns
->poll
);
1158 down_write(&namespace_sem
);
1159 /* First pass: copy the tree topology */
1160 new_ns
->root
= copy_tree(namespace->root
, namespace->root
->mnt_root
,
1162 if (!new_ns
->root
) {
1163 up_write(&namespace_sem
);
1167 spin_lock(&vfsmount_lock
);
1168 list_add_tail(&new_ns
->list
, &new_ns
->root
->mnt_list
);
1169 spin_unlock(&vfsmount_lock
);
1172 * Second pass: switch the tsk->fs->* elements and mark new vfsmounts
1173 * as belonging to new namespace. We have already acquired a private
1174 * fs_struct, so tsk->fs->lock is not needed.
1176 p
= namespace->root
;
1179 q
->mnt_namespace
= new_ns
;
1181 if (p
== fs
->rootmnt
) {
1183 fs
->rootmnt
= mntget(q
);
1185 if (p
== fs
->pwdmnt
) {
1187 fs
->pwdmnt
= mntget(q
);
1189 if (p
== fs
->altrootmnt
) {
1191 fs
->altrootmnt
= mntget(q
);
1194 p
= next_mnt(p
, namespace->root
);
1195 q
= next_mnt(q
, new_ns
->root
);
1197 up_write(&namespace_sem
);
1199 tsk
->namespace = new_ns
;
1208 put_namespace(namespace);
1212 put_namespace(namespace);
1216 asmlinkage
long sys_mount(char __user
* dev_name
, char __user
* dir_name
,
1217 char __user
* type
, unsigned long flags
,
1221 unsigned long data_page
;
1222 unsigned long type_page
;
1223 unsigned long dev_page
;
1226 retval
= copy_mount_options(type
, &type_page
);
1230 dir_page
= getname(dir_name
);
1231 retval
= PTR_ERR(dir_page
);
1232 if (IS_ERR(dir_page
))
1235 retval
= copy_mount_options(dev_name
, &dev_page
);
1239 retval
= copy_mount_options(data
, &data_page
);
1244 retval
= do_mount((char *)dev_page
, dir_page
, (char *)type_page
,
1245 flags
, (void *)data_page
);
1247 free_page(data_page
);
1250 free_page(dev_page
);
1254 free_page(type_page
);
1259 * Replace the fs->{rootmnt,root} with {mnt,dentry}. Put the old values.
1260 * It can block. Requires the big lock held.
1262 void set_fs_root(struct fs_struct
*fs
, struct vfsmount
*mnt
,
1263 struct dentry
*dentry
)
1265 struct dentry
*old_root
;
1266 struct vfsmount
*old_rootmnt
;
1267 write_lock(&fs
->lock
);
1268 old_root
= fs
->root
;
1269 old_rootmnt
= fs
->rootmnt
;
1270 fs
->rootmnt
= mntget(mnt
);
1271 fs
->root
= dget(dentry
);
1272 write_unlock(&fs
->lock
);
1275 mntput(old_rootmnt
);
1280 * Replace the fs->{pwdmnt,pwd} with {mnt,dentry}. Put the old values.
1281 * It can block. Requires the big lock held.
1283 void set_fs_pwd(struct fs_struct
*fs
, struct vfsmount
*mnt
,
1284 struct dentry
*dentry
)
1286 struct dentry
*old_pwd
;
1287 struct vfsmount
*old_pwdmnt
;
1289 write_lock(&fs
->lock
);
1291 old_pwdmnt
= fs
->pwdmnt
;
1292 fs
->pwdmnt
= mntget(mnt
);
1293 fs
->pwd
= dget(dentry
);
1294 write_unlock(&fs
->lock
);
1302 static void chroot_fs_refs(struct nameidata
*old_nd
, struct nameidata
*new_nd
)
1304 struct task_struct
*g
, *p
;
1305 struct fs_struct
*fs
;
1307 read_lock(&tasklist_lock
);
1308 do_each_thread(g
, p
) {
1312 atomic_inc(&fs
->count
);
1314 if (fs
->root
== old_nd
->dentry
1315 && fs
->rootmnt
== old_nd
->mnt
)
1316 set_fs_root(fs
, new_nd
->mnt
, new_nd
->dentry
);
1317 if (fs
->pwd
== old_nd
->dentry
1318 && fs
->pwdmnt
== old_nd
->mnt
)
1319 set_fs_pwd(fs
, new_nd
->mnt
, new_nd
->dentry
);
1323 } while_each_thread(g
, p
);
1324 read_unlock(&tasklist_lock
);
1328 * pivot_root Semantics:
1329 * Moves the root file system of the current process to the directory put_old,
1330 * makes new_root as the new root file system of the current process, and sets
1331 * root/cwd of all processes which had them on the current root to new_root.
1334 * The new_root and put_old must be directories, and must not be on the
1335 * same file system as the current process root. The put_old must be
1336 * underneath new_root, i.e. adding a non-zero number of /.. to the string
1337 * pointed to by put_old must yield the same directory as new_root. No other
1338 * file system may be mounted on put_old. After all, new_root is a mountpoint.
1341 * - we don't move root/cwd if they are not at the root (reason: if something
1342 * cared enough to change them, it's probably wrong to force them elsewhere)
1343 * - it's okay to pick a root that isn't the root of a file system, e.g.
1344 * /nfs/my_root where /nfs is the mount point. It must be a mountpoint,
1345 * though, so you may need to say mount --bind /nfs/my_root /nfs/my_root
1348 asmlinkage
long sys_pivot_root(const char __user
* new_root
,
1349 const char __user
* put_old
)
1351 struct vfsmount
*tmp
;
1352 struct nameidata new_nd
, old_nd
, parent_nd
, root_parent
, user_nd
;
1355 if (!capable(CAP_SYS_ADMIN
))
1360 error
= __user_walk(new_root
, LOOKUP_FOLLOW
| LOOKUP_DIRECTORY
,
1365 if (!check_mnt(new_nd
.mnt
))
1368 error
= __user_walk(put_old
, LOOKUP_FOLLOW
| LOOKUP_DIRECTORY
, &old_nd
);
1372 error
= security_sb_pivotroot(&old_nd
, &new_nd
);
1374 path_release(&old_nd
);
1378 read_lock(¤t
->fs
->lock
);
1379 user_nd
.mnt
= mntget(current
->fs
->rootmnt
);
1380 user_nd
.dentry
= dget(current
->fs
->root
);
1381 read_unlock(¤t
->fs
->lock
);
1382 down_write(&namespace_sem
);
1383 down(&old_nd
.dentry
->d_inode
->i_sem
);
1385 if (!check_mnt(user_nd
.mnt
))
1388 if (IS_DEADDIR(new_nd
.dentry
->d_inode
))
1390 if (d_unhashed(new_nd
.dentry
) && !IS_ROOT(new_nd
.dentry
))
1392 if (d_unhashed(old_nd
.dentry
) && !IS_ROOT(old_nd
.dentry
))
1395 if (new_nd
.mnt
== user_nd
.mnt
|| old_nd
.mnt
== user_nd
.mnt
)
1396 goto out2
; /* loop, on the same file system */
1398 if (user_nd
.mnt
->mnt_root
!= user_nd
.dentry
)
1399 goto out2
; /* not a mountpoint */
1400 if (user_nd
.mnt
->mnt_parent
== user_nd
.mnt
)
1401 goto out2
; /* not attached */
1402 if (new_nd
.mnt
->mnt_root
!= new_nd
.dentry
)
1403 goto out2
; /* not a mountpoint */
1404 if (new_nd
.mnt
->mnt_parent
== new_nd
.mnt
)
1405 goto out2
; /* not attached */
1406 tmp
= old_nd
.mnt
; /* make sure we can reach put_old from new_root */
1407 spin_lock(&vfsmount_lock
);
1408 if (tmp
!= new_nd
.mnt
) {
1410 if (tmp
->mnt_parent
== tmp
)
1411 goto out3
; /* already mounted on put_old */
1412 if (tmp
->mnt_parent
== new_nd
.mnt
)
1414 tmp
= tmp
->mnt_parent
;
1416 if (!is_subdir(tmp
->mnt_mountpoint
, new_nd
.dentry
))
1418 } else if (!is_subdir(old_nd
.dentry
, new_nd
.dentry
))
1420 detach_mnt(new_nd
.mnt
, &parent_nd
);
1421 detach_mnt(user_nd
.mnt
, &root_parent
);
1422 attach_mnt(user_nd
.mnt
, &old_nd
); /* mount old root on put_old */
1423 attach_mnt(new_nd
.mnt
, &root_parent
); /* mount new_root on / */
1424 touch_namespace(current
->namespace);
1425 spin_unlock(&vfsmount_lock
);
1426 chroot_fs_refs(&user_nd
, &new_nd
);
1427 security_sb_post_pivotroot(&user_nd
, &new_nd
);
1429 path_release(&root_parent
);
1430 path_release(&parent_nd
);
1432 up(&old_nd
.dentry
->d_inode
->i_sem
);
1433 up_write(&namespace_sem
);
1434 path_release(&user_nd
);
1435 path_release(&old_nd
);
1437 path_release(&new_nd
);
1442 spin_unlock(&vfsmount_lock
);
1446 static void __init
init_mount_tree(void)
1448 struct vfsmount
*mnt
;
1449 struct namespace *namespace;
1450 struct task_struct
*g
, *p
;
1452 mnt
= do_kern_mount("rootfs", 0, "rootfs", NULL
);
1454 panic("Can't create rootfs");
1455 namespace = kmalloc(sizeof(*namespace), GFP_KERNEL
);
1457 panic("Can't allocate initial namespace");
1458 atomic_set(&namespace->count
, 1);
1459 INIT_LIST_HEAD(&namespace->list
);
1460 init_waitqueue_head(&namespace->poll
);
1461 namespace->event
= 0;
1462 list_add(&mnt
->mnt_list
, &namespace->list
);
1463 namespace->root
= mnt
;
1464 mnt
->mnt_namespace
= namespace;
1466 init_task
.namespace = namespace;
1467 read_lock(&tasklist_lock
);
1468 do_each_thread(g
, p
) {
1469 get_namespace(namespace);
1470 p
->namespace = namespace;
1471 } while_each_thread(g
, p
);
1472 read_unlock(&tasklist_lock
);
1474 set_fs_pwd(current
->fs
, namespace->root
, namespace->root
->mnt_root
);
1475 set_fs_root(current
->fs
, namespace->root
, namespace->root
->mnt_root
);
1478 void __init
mnt_init(unsigned long mempages
)
1480 struct list_head
*d
;
1481 unsigned int nr_hash
;
1484 init_rwsem(&namespace_sem
);
1486 mnt_cache
= kmem_cache_create("mnt_cache", sizeof(struct vfsmount
),
1487 0, SLAB_HWCACHE_ALIGN
| SLAB_PANIC
, NULL
, NULL
);
1489 mount_hashtable
= (struct list_head
*)__get_free_page(GFP_ATOMIC
);
1491 if (!mount_hashtable
)
1492 panic("Failed to allocate mount hash table\n");
1495 * Find the power-of-two list-heads that can fit into the allocation..
1496 * We don't guarantee that "sizeof(struct list_head)" is necessarily
1499 nr_hash
= PAGE_SIZE
/ sizeof(struct list_head
);
1503 } while ((nr_hash
>> hash_bits
) != 0);
1507 * Re-calculate the actual number of entries and the mask
1508 * from the number of bits we can fit.
1510 nr_hash
= 1UL << hash_bits
;
1511 hash_mask
= nr_hash
- 1;
1513 printk("Mount-cache hash table entries: %d\n", nr_hash
);
1515 /* And initialize the newly allocated array */
1516 d
= mount_hashtable
;
1528 void __put_namespace(struct namespace *namespace)
1530 struct vfsmount
*root
= namespace->root
;
1531 LIST_HEAD(umount_list
);
1532 namespace->root
= NULL
;
1533 spin_unlock(&vfsmount_lock
);
1534 down_write(&namespace_sem
);
1535 spin_lock(&vfsmount_lock
);
1536 umount_tree(root
, &umount_list
);
1537 spin_unlock(&vfsmount_lock
);
1538 up_write(&namespace_sem
);
1539 release_mounts(&umount_list
);