4 * (C) Copyright Al Viro 2000, 2001
5 * Released under GPL v2.
7 * Based on code from fs/super.c, copyright Linus Torvalds and others.
11 #include <linux/config.h>
12 #include <linux/slab.h>
13 #include <linux/sched.h>
14 #include <linux/smp_lock.h>
15 #include <linux/init.h>
16 #include <linux/quotaops.h>
17 #include <linux/acct.h>
18 #include <linux/module.h>
19 #include <linux/seq_file.h>
20 #include <linux/namespace.h>
21 #include <linux/namei.h>
22 #include <linux/security.h>
23 #include <linux/mount.h>
24 #include <asm/uaccess.h>
26 extern int __init
init_rootfs(void);
27 extern int __init
sysfs_init(void);
29 /* spinlock for vfsmount related operations, inplace of dcache_lock */
30 spinlock_t vfsmount_lock __cacheline_aligned_in_smp
= SPIN_LOCK_UNLOCKED
;
31 static struct list_head
*mount_hashtable
;
32 static int hash_mask
, hash_bits
;
33 static kmem_cache_t
*mnt_cache
;
35 static inline unsigned long hash(struct vfsmount
*mnt
, struct dentry
*dentry
)
37 unsigned long tmp
= ((unsigned long) mnt
/ L1_CACHE_BYTES
);
38 tmp
+= ((unsigned long) dentry
/ L1_CACHE_BYTES
);
39 tmp
= tmp
+ (tmp
>> hash_bits
);
40 return tmp
& hash_mask
;
43 struct vfsmount
*alloc_vfsmnt(const char *name
)
45 struct vfsmount
*mnt
= kmem_cache_alloc(mnt_cache
, GFP_KERNEL
);
47 memset(mnt
, 0, sizeof(struct vfsmount
));
48 atomic_set(&mnt
->mnt_count
,1);
49 INIT_LIST_HEAD(&mnt
->mnt_hash
);
50 INIT_LIST_HEAD(&mnt
->mnt_child
);
51 INIT_LIST_HEAD(&mnt
->mnt_mounts
);
52 INIT_LIST_HEAD(&mnt
->mnt_list
);
54 int size
= strlen(name
)+1;
55 char *newname
= kmalloc(size
, GFP_KERNEL
);
57 memcpy(newname
, name
, size
);
58 mnt
->mnt_devname
= newname
;
65 void free_vfsmnt(struct vfsmount
*mnt
)
67 kfree(mnt
->mnt_devname
);
68 kmem_cache_free(mnt_cache
, mnt
);
72 * Now, lookup_mnt increments the ref count before returning
73 * the vfsmount struct.
75 struct vfsmount
*lookup_mnt(struct vfsmount
*mnt
, struct dentry
*dentry
)
77 struct list_head
* head
= mount_hashtable
+ hash(mnt
, dentry
);
78 struct list_head
* tmp
= head
;
79 struct vfsmount
*p
, *found
= NULL
;
81 spin_lock(&vfsmount_lock
);
87 p
= list_entry(tmp
, struct vfsmount
, mnt_hash
);
88 if (p
->mnt_parent
== mnt
&& p
->mnt_mountpoint
== dentry
) {
93 spin_unlock(&vfsmount_lock
);
97 EXPORT_SYMBOL(lookup_mnt
);
99 static int check_mnt(struct vfsmount
*mnt
)
101 spin_lock(&vfsmount_lock
);
102 while (mnt
->mnt_parent
!= mnt
)
103 mnt
= mnt
->mnt_parent
;
104 spin_unlock(&vfsmount_lock
);
105 return mnt
== current
->namespace->root
;
108 static void detach_mnt(struct vfsmount
*mnt
, struct nameidata
*old_nd
)
110 old_nd
->dentry
= mnt
->mnt_mountpoint
;
111 old_nd
->mnt
= mnt
->mnt_parent
;
112 mnt
->mnt_parent
= mnt
;
113 mnt
->mnt_mountpoint
= mnt
->mnt_root
;
114 list_del_init(&mnt
->mnt_child
);
115 list_del_init(&mnt
->mnt_hash
);
116 old_nd
->dentry
->d_mounted
--;
119 static void attach_mnt(struct vfsmount
*mnt
, struct nameidata
*nd
)
121 mnt
->mnt_parent
= mntget(nd
->mnt
);
122 mnt
->mnt_mountpoint
= dget(nd
->dentry
);
123 list_add(&mnt
->mnt_hash
, mount_hashtable
+hash(nd
->mnt
, nd
->dentry
));
124 list_add_tail(&mnt
->mnt_child
, &nd
->mnt
->mnt_mounts
);
125 nd
->dentry
->d_mounted
++;
128 static struct vfsmount
*next_mnt(struct vfsmount
*p
, struct vfsmount
*root
)
130 struct list_head
*next
= p
->mnt_mounts
.next
;
131 if (next
== &p
->mnt_mounts
) {
135 next
= p
->mnt_child
.next
;
136 if (next
!= &p
->mnt_parent
->mnt_mounts
)
141 return list_entry(next
, struct vfsmount
, mnt_child
);
144 static struct vfsmount
*
145 clone_mnt(struct vfsmount
*old
, struct dentry
*root
)
147 struct super_block
*sb
= old
->mnt_sb
;
148 struct vfsmount
*mnt
= alloc_vfsmnt(old
->mnt_devname
);
151 mnt
->mnt_flags
= old
->mnt_flags
;
152 atomic_inc(&sb
->s_active
);
154 mnt
->mnt_root
= dget(root
);
155 mnt
->mnt_mountpoint
= mnt
->mnt_root
;
156 mnt
->mnt_parent
= mnt
;
161 void __mntput(struct vfsmount
*mnt
)
163 struct super_block
*sb
= mnt
->mnt_sb
;
166 deactivate_super(sb
);
169 EXPORT_SYMBOL(__mntput
);
172 static void *m_start(struct seq_file
*m
, loff_t
*pos
)
174 struct namespace *n
= m
->private;
179 list_for_each(p
, &n
->list
)
181 return list_entry(p
, struct vfsmount
, mnt_list
);
185 static void *m_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
187 struct namespace *n
= m
->private;
188 struct list_head
*p
= ((struct vfsmount
*)v
)->mnt_list
.next
;
190 return p
==&n
->list
? NULL
: list_entry(p
, struct vfsmount
, mnt_list
);
193 static void m_stop(struct seq_file
*m
, void *v
)
195 struct namespace *n
= m
->private;
199 static inline void mangle(struct seq_file
*m
, const char *s
)
201 seq_escape(m
, s
, " \t\n\\");
204 static int show_vfsmnt(struct seq_file
*m
, void *v
)
206 struct vfsmount
*mnt
= v
;
208 static struct proc_fs_info
{
212 { MS_SYNCHRONOUS
, ",sync" },
213 { MS_DIRSYNC
, ",dirsync" },
214 { MS_MANDLOCK
, ",mand" },
215 { MS_NOATIME
, ",noatime" },
216 { MS_NODIRATIME
, ",nodiratime" },
219 static struct proc_fs_info mnt_info
[] = {
220 { MNT_NOSUID
, ",nosuid" },
221 { MNT_NODEV
, ",nodev" },
222 { MNT_NOEXEC
, ",noexec" },
225 struct proc_fs_info
*fs_infop
;
227 mangle(m
, mnt
->mnt_devname
? mnt
->mnt_devname
: "none");
229 seq_path(m
, mnt
, mnt
->mnt_root
, " \t\n\\");
231 mangle(m
, mnt
->mnt_sb
->s_type
->name
);
232 seq_puts(m
, mnt
->mnt_sb
->s_flags
& MS_RDONLY
? " ro" : " rw");
233 for (fs_infop
= fs_info
; fs_infop
->flag
; fs_infop
++) {
234 if (mnt
->mnt_sb
->s_flags
& fs_infop
->flag
)
235 seq_puts(m
, fs_infop
->str
);
237 for (fs_infop
= mnt_info
; fs_infop
->flag
; fs_infop
++) {
238 if (mnt
->mnt_flags
& fs_infop
->flag
)
239 seq_puts(m
, fs_infop
->str
);
241 if (mnt
->mnt_sb
->s_op
->show_options
)
242 err
= mnt
->mnt_sb
->s_op
->show_options(m
, mnt
);
243 seq_puts(m
, " 0 0\n");
247 struct seq_operations mounts_op
= {
255 * Doesn't take quota and stuff into account. IOW, in some cases it will
256 * give false negatives. The main reason why it's here is that we need
257 * a non-destructive way to look for easily umountable filesystems.
259 int may_umount(struct vfsmount
*mnt
)
261 if (atomic_read(&mnt
->mnt_count
) > 2)
266 EXPORT_SYMBOL(may_umount
);
268 void umount_tree(struct vfsmount
*mnt
)
273 for (p
= mnt
; p
; p
= next_mnt(p
, mnt
)) {
274 list_del(&p
->mnt_list
);
275 list_add(&p
->mnt_list
, &kill
);
278 while (!list_empty(&kill
)) {
279 mnt
= list_entry(kill
.next
, struct vfsmount
, mnt_list
);
280 list_del_init(&mnt
->mnt_list
);
281 if (mnt
->mnt_parent
== mnt
) {
282 spin_unlock(&vfsmount_lock
);
284 struct nameidata old_nd
;
285 detach_mnt(mnt
, &old_nd
);
286 spin_unlock(&vfsmount_lock
);
287 path_release(&old_nd
);
290 spin_lock(&vfsmount_lock
);
294 static int do_umount(struct vfsmount
*mnt
, int flags
)
296 struct super_block
* sb
= mnt
->mnt_sb
;
299 retval
= security_sb_umount(mnt
, flags
);
304 * If we may have to abort operations to get out of this
305 * mount, and they will themselves hold resources we must
306 * allow the fs to do things. In the Unix tradition of
307 * 'Gee thats tricky lets do it in userspace' the umount_begin
308 * might fail to complete on the first run through as other tasks
309 * must return, and the like. Thats for the mount program to worry
310 * about for the moment.
314 if( (flags
&MNT_FORCE
) && sb
->s_op
->umount_begin
)
315 sb
->s_op
->umount_begin(sb
);
319 * No sense to grab the lock for this test, but test itself looks
320 * somewhat bogus. Suggestions for better replacement?
321 * Ho-hum... In principle, we might treat that as umount + switch
322 * to rootfs. GC would eventually take care of the old vfsmount.
323 * Actually it makes sense, especially if rootfs would contain a
324 * /reboot - static binary that would close all descriptors and
325 * call reboot(9). Then init(8) could umount root and exec /reboot.
327 if (mnt
== current
->fs
->rootmnt
&& !(flags
& MNT_DETACH
)) {
329 * Special case for "unmounting" root ...
330 * we just try to remount it readonly.
332 down_write(&sb
->s_umount
);
333 if (!(sb
->s_flags
& MS_RDONLY
)) {
335 retval
= do_remount_sb(sb
, MS_RDONLY
, 0, 0);
338 up_write(&sb
->s_umount
);
342 down_write(¤t
->namespace->sem
);
343 spin_lock(&vfsmount_lock
);
345 if (atomic_read(&sb
->s_active
) == 1) {
346 /* last instance - try to be smart */
347 spin_unlock(&vfsmount_lock
);
352 security_sb_umount_close(mnt
);
353 spin_lock(&vfsmount_lock
);
356 if (atomic_read(&mnt
->mnt_count
) == 2 || flags
& MNT_DETACH
) {
357 if (!list_empty(&mnt
->mnt_list
))
361 spin_unlock(&vfsmount_lock
);
363 security_sb_umount_busy(mnt
);
364 up_write(¤t
->namespace->sem
);
369 * Now umount can handle mount points as well as block devices.
370 * This is important for filesystems which use unnamed block devices.
372 * We now support a flag for forced unmount like the other 'big iron'
373 * unixes. Our API is identical to OSF/1 to avoid making a mess of AMD
376 asmlinkage
long sys_umount(char __user
* name
, int flags
)
381 retval
= __user_walk(name
, LOOKUP_FOLLOW
, &nd
);
385 if (nd
.dentry
!= nd
.mnt
->mnt_root
)
387 if (!check_mnt(nd
.mnt
))
391 if (!capable(CAP_SYS_ADMIN
))
394 retval
= do_umount(nd
.mnt
, flags
);
402 * The 2.0 compatible umount. No flags.
405 asmlinkage
long sys_oldumount(char __user
* name
)
407 return sys_umount(name
,0);
410 static int mount_is_safe(struct nameidata
*nd
)
412 if (capable(CAP_SYS_ADMIN
))
416 if (S_ISLNK(nd
->dentry
->d_inode
->i_mode
))
418 if (nd
->dentry
->d_inode
->i_mode
& S_ISVTX
) {
419 if (current
->uid
!= nd
->dentry
->d_inode
->i_uid
)
422 if (permission(nd
->dentry
->d_inode
, MAY_WRITE
, nd
))
429 lives_below_in_same_fs(struct dentry
*d
, struct dentry
*dentry
)
434 if (d
== NULL
|| d
== d
->d_parent
)
440 static struct vfsmount
*copy_tree(struct vfsmount
*mnt
, struct dentry
*dentry
)
442 struct vfsmount
*res
, *p
, *q
, *r
, *s
;
446 res
= q
= clone_mnt(mnt
, dentry
);
449 q
->mnt_mountpoint
= mnt
->mnt_mountpoint
;
452 for (h
= mnt
->mnt_mounts
.next
; h
!= &mnt
->mnt_mounts
; h
= h
->next
) {
453 r
= list_entry(h
, struct vfsmount
, mnt_child
);
454 if (!lives_below_in_same_fs(r
->mnt_mountpoint
, dentry
))
457 for (s
= r
; s
; s
= next_mnt(s
, r
)) {
458 while (p
!= s
->mnt_parent
) {
464 nd
.dentry
= p
->mnt_mountpoint
;
465 q
= clone_mnt(p
, p
->mnt_root
);
468 spin_lock(&vfsmount_lock
);
469 list_add_tail(&q
->mnt_list
, &res
->mnt_list
);
471 spin_unlock(&vfsmount_lock
);
477 spin_lock(&vfsmount_lock
);
479 spin_unlock(&vfsmount_lock
);
484 static int graft_tree(struct vfsmount
*mnt
, struct nameidata
*nd
)
487 if (mnt
->mnt_sb
->s_flags
& MS_NOUSER
)
490 if (S_ISDIR(nd
->dentry
->d_inode
->i_mode
) !=
491 S_ISDIR(mnt
->mnt_root
->d_inode
->i_mode
))
495 down(&nd
->dentry
->d_inode
->i_sem
);
496 if (IS_DEADDIR(nd
->dentry
->d_inode
))
499 err
= security_sb_check_sb(mnt
, nd
);
504 spin_lock(&vfsmount_lock
);
505 if (IS_ROOT(nd
->dentry
) || !d_unhashed(nd
->dentry
)) {
506 struct list_head head
;
509 list_add_tail(&head
, &mnt
->mnt_list
);
510 list_splice(&head
, current
->namespace->list
.prev
);
514 spin_unlock(&vfsmount_lock
);
516 up(&nd
->dentry
->d_inode
->i_sem
);
518 security_sb_post_addmount(mnt
, nd
);
525 static int do_loopback(struct nameidata
*nd
, char *old_name
, int recurse
)
527 struct nameidata old_nd
;
528 struct vfsmount
*mnt
= NULL
;
529 int err
= mount_is_safe(nd
);
532 if (!old_name
|| !*old_name
)
534 err
= path_lookup(old_name
, LOOKUP_FOLLOW
, &old_nd
);
538 down_write(¤t
->namespace->sem
);
540 if (check_mnt(nd
->mnt
) && (!recurse
|| check_mnt(old_nd
.mnt
))) {
543 mnt
= copy_tree(old_nd
.mnt
, old_nd
.dentry
);
545 mnt
= clone_mnt(old_nd
.mnt
, old_nd
.dentry
);
549 err
= graft_tree(mnt
, nd
);
551 spin_lock(&vfsmount_lock
);
553 spin_unlock(&vfsmount_lock
);
558 up_write(¤t
->namespace->sem
);
559 path_release(&old_nd
);
564 * change filesystem flags. dir should be a physical root of filesystem.
565 * If you've mounted a non-root directory somewhere and want to do remount
566 * on it - tough luck.
569 static int do_remount(struct nameidata
*nd
,int flags
,int mnt_flags
,void *data
)
572 struct super_block
* sb
= nd
->mnt
->mnt_sb
;
574 if (!capable(CAP_SYS_ADMIN
))
577 if (!check_mnt(nd
->mnt
))
580 if (nd
->dentry
!= nd
->mnt
->mnt_root
)
583 down_write(&sb
->s_umount
);
584 err
= do_remount_sb(sb
, flags
, data
, 0);
586 nd
->mnt
->mnt_flags
=mnt_flags
;
587 up_write(&sb
->s_umount
);
589 security_sb_post_remount(nd
->mnt
, flags
, data
);
593 static int do_move_mount(struct nameidata
*nd
, char *old_name
)
595 struct nameidata old_nd
, parent_nd
;
598 if (!capable(CAP_SYS_ADMIN
))
600 if (!old_name
|| !*old_name
)
602 err
= path_lookup(old_name
, LOOKUP_FOLLOW
, &old_nd
);
606 down_write(¤t
->namespace->sem
);
607 while(d_mountpoint(nd
->dentry
) && follow_down(&nd
->mnt
, &nd
->dentry
))
610 if (!check_mnt(nd
->mnt
) || !check_mnt(old_nd
.mnt
))
614 down(&nd
->dentry
->d_inode
->i_sem
);
615 if (IS_DEADDIR(nd
->dentry
->d_inode
))
618 spin_lock(&vfsmount_lock
);
619 if (!IS_ROOT(nd
->dentry
) && d_unhashed(nd
->dentry
))
623 if (old_nd
.dentry
!= old_nd
.mnt
->mnt_root
)
626 if (old_nd
.mnt
== old_nd
.mnt
->mnt_parent
)
629 if (S_ISDIR(nd
->dentry
->d_inode
->i_mode
) !=
630 S_ISDIR(old_nd
.dentry
->d_inode
->i_mode
))
634 for (p
= nd
->mnt
; p
->mnt_parent
!=p
; p
= p
->mnt_parent
)
639 detach_mnt(old_nd
.mnt
, &parent_nd
);
640 attach_mnt(old_nd
.mnt
, nd
);
642 spin_unlock(&vfsmount_lock
);
644 up(&nd
->dentry
->d_inode
->i_sem
);
646 up_write(¤t
->namespace->sem
);
648 path_release(&parent_nd
);
649 path_release(&old_nd
);
653 static int do_add_mount(struct nameidata
*nd
, char *type
, int flags
,
654 int mnt_flags
, char *name
, void *data
)
656 struct vfsmount
*mnt
;
659 if (!type
|| !memchr(type
, 0, PAGE_SIZE
))
662 /* we need capabilities... */
663 if (!capable(CAP_SYS_ADMIN
))
666 mnt
= do_kern_mount(type
, flags
, name
, data
);
671 down_write(¤t
->namespace->sem
);
672 /* Something was mounted here while we slept */
673 while(d_mountpoint(nd
->dentry
) && follow_down(&nd
->mnt
, &nd
->dentry
))
676 if (!check_mnt(nd
->mnt
))
679 /* Refuse the same filesystem on the same mount point */
681 if (nd
->mnt
->mnt_sb
== mnt
->mnt_sb
&& nd
->mnt
->mnt_root
== nd
->dentry
)
685 if (S_ISLNK(mnt
->mnt_root
->d_inode
->i_mode
))
688 mnt
->mnt_flags
= mnt_flags
;
689 err
= graft_tree(mnt
, nd
);
691 up_write(¤t
->namespace->sem
);
697 int copy_mount_options (const void __user
*data
, unsigned long *where
)
707 if (!(page
= __get_free_page(GFP_KERNEL
)))
710 /* We only care that *some* data at the address the user
711 * gave us is valid. Just in case, we'll zero
712 * the remainder of the page.
714 /* copy_from_user cannot cross TASK_SIZE ! */
715 size
= TASK_SIZE
- (unsigned long)data
;
716 if (size
> PAGE_SIZE
)
719 i
= size
- copy_from_user((void *)page
, data
, size
);
725 memset((char *)page
+ i
, 0, PAGE_SIZE
- i
);
731 * Flags is a 32-bit value that allows up to 31 non-fs dependent flags to
732 * be given to the mount() call (ie: read-only, no-dev, no-suid etc).
734 * data is a (void *) that can point to any structure up to
735 * PAGE_SIZE-1 bytes, which can contain arbitrary fs-dependent
736 * information (or be NULL).
738 * Pre-0.97 versions of mount() didn't have a flags word.
739 * When the flags word was introduced its top half was required
740 * to have the magic value 0xC0ED, and this remained so until 2.4.0-test9.
741 * Therefore, if this magic number is present, it carries no information
742 * and must be discarded.
744 long do_mount(char * dev_name
, char * dir_name
, char *type_page
,
745 unsigned long flags
, void *data_page
)
752 if ((flags
& MS_MGC_MSK
) == MS_MGC_VAL
)
753 flags
&= ~MS_MGC_MSK
;
755 /* Basic sanity checks */
757 if (!dir_name
|| !*dir_name
|| !memchr(dir_name
, 0, PAGE_SIZE
))
759 if (dev_name
&& !memchr(dev_name
, 0, PAGE_SIZE
))
763 ((char *)data_page
)[PAGE_SIZE
- 1] = 0;
765 /* Separate the per-mountpoint flags */
766 if (flags
& MS_NOSUID
)
767 mnt_flags
|= MNT_NOSUID
;
768 if (flags
& MS_NODEV
)
769 mnt_flags
|= MNT_NODEV
;
770 if (flags
& MS_NOEXEC
)
771 mnt_flags
|= MNT_NOEXEC
;
772 flags
&= ~(MS_NOSUID
|MS_NOEXEC
|MS_NODEV
);
774 /* ... and get the mountpoint */
775 retval
= path_lookup(dir_name
, LOOKUP_FOLLOW
, &nd
);
779 retval
= security_sb_mount(dev_name
, &nd
, type_page
, flags
, data_page
);
783 if (flags
& MS_REMOUNT
)
784 retval
= do_remount(&nd
, flags
& ~MS_REMOUNT
, mnt_flags
,
786 else if (flags
& MS_BIND
)
787 retval
= do_loopback(&nd
, dev_name
, flags
& MS_REC
);
788 else if (flags
& MS_MOVE
)
789 retval
= do_move_mount(&nd
, dev_name
);
791 retval
= do_add_mount(&nd
, type_page
, flags
, mnt_flags
,
792 dev_name
, data_page
);
798 int copy_namespace(int flags
, struct task_struct
*tsk
)
800 struct namespace *namespace = tsk
->namespace;
801 struct namespace *new_ns
;
802 struct vfsmount
*rootmnt
= NULL
, *pwdmnt
= NULL
, *altrootmnt
= NULL
;
803 struct fs_struct
*fs
= tsk
->fs
;
808 get_namespace(namespace);
810 if (!(flags
& CLONE_NEWNS
))
813 if (!capable(CAP_SYS_ADMIN
)) {
814 put_namespace(namespace);
818 new_ns
= kmalloc(sizeof(struct namespace), GFP_KERNEL
);
822 atomic_set(&new_ns
->count
, 1);
823 init_rwsem(&new_ns
->sem
);
824 INIT_LIST_HEAD(&new_ns
->list
);
826 down_write(&tsk
->namespace->sem
);
827 /* First pass: copy the tree topology */
828 new_ns
->root
= copy_tree(namespace->root
, namespace->root
->mnt_root
);
830 up_write(&tsk
->namespace->sem
);
834 spin_lock(&vfsmount_lock
);
835 list_add_tail(&new_ns
->list
, &new_ns
->root
->mnt_list
);
836 spin_unlock(&vfsmount_lock
);
838 /* Second pass: switch the tsk->fs->* elements */
840 struct vfsmount
*p
, *q
;
841 write_lock(&fs
->lock
);
846 if (p
== fs
->rootmnt
) {
848 fs
->rootmnt
= mntget(q
);
850 if (p
== fs
->pwdmnt
) {
852 fs
->pwdmnt
= mntget(q
);
854 if (p
== fs
->altrootmnt
) {
856 fs
->altrootmnt
= mntget(q
);
858 p
= next_mnt(p
, namespace->root
);
859 q
= next_mnt(q
, new_ns
->root
);
861 write_unlock(&fs
->lock
);
863 up_write(&tsk
->namespace->sem
);
865 tsk
->namespace = new_ns
;
874 put_namespace(namespace);
878 put_namespace(namespace);
882 asmlinkage
long sys_mount(char __user
* dev_name
, char __user
* dir_name
,
883 char __user
* type
, unsigned long flags
,
887 unsigned long data_page
;
888 unsigned long type_page
;
889 unsigned long dev_page
;
892 retval
= copy_mount_options (type
, &type_page
);
896 dir_page
= getname(dir_name
);
897 retval
= PTR_ERR(dir_page
);
898 if (IS_ERR(dir_page
))
901 retval
= copy_mount_options (dev_name
, &dev_page
);
905 retval
= copy_mount_options (data
, &data_page
);
910 retval
= do_mount((char*)dev_page
, dir_page
, (char*)type_page
,
911 flags
, (void*)data_page
);
913 free_page(data_page
);
920 free_page(type_page
);
925 * Replace the fs->{rootmnt,root} with {mnt,dentry}. Put the old values.
926 * It can block. Requires the big lock held.
928 void set_fs_root(struct fs_struct
*fs
, struct vfsmount
*mnt
,
929 struct dentry
*dentry
)
931 struct dentry
*old_root
;
932 struct vfsmount
*old_rootmnt
;
933 write_lock(&fs
->lock
);
935 old_rootmnt
= fs
->rootmnt
;
936 fs
->rootmnt
= mntget(mnt
);
937 fs
->root
= dget(dentry
);
938 write_unlock(&fs
->lock
);
945 EXPORT_SYMBOL(set_fs_root
);
948 * Replace the fs->{pwdmnt,pwd} with {mnt,dentry}. Put the old values.
949 * It can block. Requires the big lock held.
951 void set_fs_pwd(struct fs_struct
*fs
, struct vfsmount
*mnt
,
952 struct dentry
*dentry
)
954 struct dentry
*old_pwd
;
955 struct vfsmount
*old_pwdmnt
;
957 write_lock(&fs
->lock
);
959 old_pwdmnt
= fs
->pwdmnt
;
960 fs
->pwdmnt
= mntget(mnt
);
961 fs
->pwd
= dget(dentry
);
962 write_unlock(&fs
->lock
);
970 EXPORT_SYMBOL(set_fs_pwd
);
972 static void chroot_fs_refs(struct nameidata
*old_nd
, struct nameidata
*new_nd
)
974 struct task_struct
*g
, *p
;
975 struct fs_struct
*fs
;
977 read_lock(&tasklist_lock
);
978 do_each_thread(g
, p
) {
982 atomic_inc(&fs
->count
);
984 if (fs
->root
==old_nd
->dentry
&&fs
->rootmnt
==old_nd
->mnt
)
985 set_fs_root(fs
, new_nd
->mnt
, new_nd
->dentry
);
986 if (fs
->pwd
==old_nd
->dentry
&&fs
->pwdmnt
==old_nd
->mnt
)
987 set_fs_pwd(fs
, new_nd
->mnt
, new_nd
->dentry
);
991 } while_each_thread(g
, p
);
992 read_unlock(&tasklist_lock
);
996 * Moves the current root to put_root, and sets root/cwd of all processes
997 * which had them on the old root to new_root.
1000 * - we don't move root/cwd if they are not at the root (reason: if something
1001 * cared enough to change them, it's probably wrong to force them elsewhere)
1002 * - it's okay to pick a root that isn't the root of a file system, e.g.
1003 * /nfs/my_root where /nfs is the mount point. It must be a mountpoint,
1004 * though, so you may need to say mount --bind /nfs/my_root /nfs/my_root
1008 asmlinkage
long sys_pivot_root(const char __user
*new_root
, const char __user
*put_old
)
1010 struct vfsmount
*tmp
;
1011 struct nameidata new_nd
, old_nd
, parent_nd
, root_parent
, user_nd
;
1014 if (!capable(CAP_SYS_ADMIN
))
1019 error
= __user_walk(new_root
, LOOKUP_FOLLOW
|LOOKUP_DIRECTORY
, &new_nd
);
1023 if (!check_mnt(new_nd
.mnt
))
1026 error
= __user_walk(put_old
, LOOKUP_FOLLOW
|LOOKUP_DIRECTORY
, &old_nd
);
1030 error
= security_sb_pivotroot(&old_nd
, &new_nd
);
1032 path_release(&old_nd
);
1036 read_lock(¤t
->fs
->lock
);
1037 user_nd
.mnt
= mntget(current
->fs
->rootmnt
);
1038 user_nd
.dentry
= dget(current
->fs
->root
);
1039 read_unlock(¤t
->fs
->lock
);
1040 down_write(¤t
->namespace->sem
);
1041 down(&old_nd
.dentry
->d_inode
->i_sem
);
1043 if (!check_mnt(user_nd
.mnt
))
1046 if (IS_DEADDIR(new_nd
.dentry
->d_inode
))
1048 if (d_unhashed(new_nd
.dentry
) && !IS_ROOT(new_nd
.dentry
))
1050 if (d_unhashed(old_nd
.dentry
) && !IS_ROOT(old_nd
.dentry
))
1053 if (new_nd
.mnt
== user_nd
.mnt
|| old_nd
.mnt
== user_nd
.mnt
)
1054 goto out2
; /* loop */
1056 if (user_nd
.mnt
->mnt_root
!= user_nd
.dentry
)
1058 if (new_nd
.mnt
->mnt_root
!= new_nd
.dentry
)
1059 goto out2
; /* not a mountpoint */
1060 tmp
= old_nd
.mnt
; /* make sure we can reach put_old from new_root */
1061 spin_lock(&vfsmount_lock
);
1062 if (tmp
!= new_nd
.mnt
) {
1064 if (tmp
->mnt_parent
== tmp
)
1066 if (tmp
->mnt_parent
== new_nd
.mnt
)
1068 tmp
= tmp
->mnt_parent
;
1070 if (!is_subdir(tmp
->mnt_mountpoint
, new_nd
.dentry
))
1072 } else if (!is_subdir(old_nd
.dentry
, new_nd
.dentry
))
1074 detach_mnt(new_nd
.mnt
, &parent_nd
);
1075 detach_mnt(user_nd
.mnt
, &root_parent
);
1076 attach_mnt(user_nd
.mnt
, &old_nd
);
1077 attach_mnt(new_nd
.mnt
, &root_parent
);
1078 spin_unlock(&vfsmount_lock
);
1079 chroot_fs_refs(&user_nd
, &new_nd
);
1080 security_sb_post_pivotroot(&user_nd
, &new_nd
);
1082 path_release(&root_parent
);
1083 path_release(&parent_nd
);
1085 up(&old_nd
.dentry
->d_inode
->i_sem
);
1086 up_write(¤t
->namespace->sem
);
1087 path_release(&user_nd
);
1088 path_release(&old_nd
);
1090 path_release(&new_nd
);
1095 spin_unlock(&vfsmount_lock
);
1099 static void __init
init_mount_tree(void)
1101 struct vfsmount
*mnt
;
1102 struct namespace *namespace;
1103 struct task_struct
*g
, *p
;
1105 mnt
= do_kern_mount("rootfs", 0, "rootfs", NULL
);
1107 panic("Can't create rootfs");
1108 namespace = kmalloc(sizeof(*namespace), GFP_KERNEL
);
1110 panic("Can't allocate initial namespace");
1111 atomic_set(&namespace->count
, 1);
1112 INIT_LIST_HEAD(&namespace->list
);
1113 init_rwsem(&namespace->sem
);
1114 list_add(&mnt
->mnt_list
, &namespace->list
);
1115 namespace->root
= mnt
;
1117 init_task
.namespace = namespace;
1118 read_lock(&tasklist_lock
);
1119 do_each_thread(g
, p
) {
1120 get_namespace(namespace);
1121 p
->namespace = namespace;
1122 } while_each_thread(g
, p
);
1123 read_unlock(&tasklist_lock
);
1125 set_fs_pwd(current
->fs
, namespace->root
, namespace->root
->mnt_root
);
1126 set_fs_root(current
->fs
, namespace->root
, namespace->root
->mnt_root
);
1129 void __init
mnt_init(unsigned long mempages
)
1131 struct list_head
*d
;
1132 unsigned long order
;
1133 unsigned int nr_hash
;
1136 mnt_cache
= kmem_cache_create("mnt_cache", sizeof(struct vfsmount
),
1137 0, SLAB_HWCACHE_ALIGN
, NULL
, NULL
);
1139 panic("Cannot create vfsmount cache");
1142 mount_hashtable
= (struct list_head
*)
1143 __get_free_pages(GFP_ATOMIC
, order
);
1145 if (!mount_hashtable
)
1146 panic("Failed to allocate mount hash table\n");
1149 * Find the power-of-two list-heads that can fit into the allocation..
1150 * We don't guarantee that "sizeof(struct list_head)" is necessarily
1153 nr_hash
= (1UL << order
) * PAGE_SIZE
/ sizeof(struct list_head
);
1157 } while ((nr_hash
>> hash_bits
) != 0);
1161 * Re-calculate the actual number of entries and the mask
1162 * from the number of bits we can fit.
1164 nr_hash
= 1UL << hash_bits
;
1165 hash_mask
= nr_hash
-1;
1167 printk("Mount-cache hash table entries: %d (order: %ld, %ld bytes)\n",
1168 nr_hash
, order
, (PAGE_SIZE
<< order
));
1170 /* And initialize the newly allocated array */
1171 d
= mount_hashtable
;
1183 void __put_namespace(struct namespace *namespace)
1185 down_write(&namespace->sem
);
1186 spin_lock(&vfsmount_lock
);
1187 umount_tree(namespace->root
);
1188 spin_unlock(&vfsmount_lock
);
1189 up_write(&namespace->sem
);