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