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>
23 #include <asm/uaccess.h>
25 extern struct vfsmount
*do_kern_mount(const char *type
, int flags
, char *name
, void *data
);
26 extern int do_remount_sb(struct super_block
*sb
, int flags
, void * data
);
27 extern int __init
init_rootfs(void);
29 static struct list_head
*mount_hashtable
;
30 static int hash_mask
, hash_bits
;
31 static kmem_cache_t
*mnt_cache
;
33 static inline unsigned long hash(struct vfsmount
*mnt
, struct dentry
*dentry
)
35 unsigned long tmp
= ((unsigned long) mnt
/ L1_CACHE_BYTES
);
36 tmp
+= ((unsigned long) dentry
/ L1_CACHE_BYTES
);
37 tmp
= tmp
+ (tmp
>> hash_bits
);
38 return tmp
& hash_mask
;
41 struct vfsmount
*alloc_vfsmnt(char *name
)
43 struct vfsmount
*mnt
= kmem_cache_alloc(mnt_cache
, GFP_KERNEL
);
45 memset(mnt
, 0, sizeof(struct vfsmount
));
46 atomic_set(&mnt
->mnt_count
,1);
47 INIT_LIST_HEAD(&mnt
->mnt_hash
);
48 INIT_LIST_HEAD(&mnt
->mnt_child
);
49 INIT_LIST_HEAD(&mnt
->mnt_mounts
);
50 INIT_LIST_HEAD(&mnt
->mnt_list
);
52 int size
= strlen(name
)+1;
53 char * newname
= kmalloc(size
, GFP_KERNEL
);
55 memcpy(newname
, name
, size
);
56 mnt
->mnt_devname
= newname
;
63 void free_vfsmnt(struct vfsmount
*mnt
)
66 kfree(mnt
->mnt_devname
);
67 kmem_cache_free(mnt_cache
, mnt
);
70 struct vfsmount
*lookup_mnt(struct vfsmount
*mnt
, struct dentry
*dentry
)
72 struct list_head
* head
= mount_hashtable
+ hash(mnt
, dentry
);
73 struct list_head
* tmp
= head
;
81 p
= list_entry(tmp
, struct vfsmount
, mnt_hash
);
82 if (p
->mnt_parent
== mnt
&& p
->mnt_mountpoint
== dentry
)
88 static int check_mnt(struct vfsmount
*mnt
)
90 spin_lock(&dcache_lock
);
91 while (mnt
->mnt_parent
!= mnt
)
92 mnt
= mnt
->mnt_parent
;
93 spin_unlock(&dcache_lock
);
94 return mnt
== current
->namespace->root
;
97 static void detach_mnt(struct vfsmount
*mnt
, struct nameidata
*old_nd
)
99 old_nd
->dentry
= mnt
->mnt_mountpoint
;
100 old_nd
->mnt
= mnt
->mnt_parent
;
101 mnt
->mnt_parent
= mnt
;
102 mnt
->mnt_mountpoint
= mnt
->mnt_root
;
103 list_del_init(&mnt
->mnt_child
);
104 list_del_init(&mnt
->mnt_hash
);
105 old_nd
->dentry
->d_mounted
--;
108 static void attach_mnt(struct vfsmount
*mnt
, struct nameidata
*nd
)
110 mnt
->mnt_parent
= mntget(nd
->mnt
);
111 mnt
->mnt_mountpoint
= dget(nd
->dentry
);
112 list_add(&mnt
->mnt_hash
, mount_hashtable
+hash(nd
->mnt
, nd
->dentry
));
113 list_add(&mnt
->mnt_child
, &nd
->mnt
->mnt_mounts
);
114 nd
->dentry
->d_mounted
++;
117 static struct vfsmount
*next_mnt(struct vfsmount
*p
, struct vfsmount
*root
)
119 struct list_head
*next
= p
->mnt_mounts
.next
;
120 if (next
== &p
->mnt_mounts
) {
124 next
= p
->mnt_child
.next
;
125 if (next
!= &p
->mnt_parent
->mnt_mounts
)
130 return list_entry(next
, struct vfsmount
, mnt_child
);
133 static struct vfsmount
*
134 clone_mnt(struct vfsmount
*old
, struct dentry
*root
)
136 struct super_block
*sb
= old
->mnt_sb
;
137 struct vfsmount
*mnt
= alloc_vfsmnt(old
->mnt_devname
);
140 mnt
->mnt_flags
= old
->mnt_flags
;
141 atomic_inc(&sb
->s_active
);
143 mnt
->mnt_root
= dget(root
);
144 mnt
->mnt_mountpoint
= mnt
->mnt_root
;
145 mnt
->mnt_parent
= mnt
;
150 void __mntput(struct vfsmount
*mnt
)
152 struct super_block
*sb
= mnt
->mnt_sb
;
155 deactivate_super(sb
);
159 static void *m_start(struct seq_file
*m
, loff_t
*pos
)
161 struct namespace *n
= m
->private;
166 list_for_each(p
, &n
->list
)
168 return list_entry(p
, struct vfsmount
, mnt_list
);
172 static void *m_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
174 struct namespace *n
= m
->private;
175 struct list_head
*p
= ((struct vfsmount
*)v
)->mnt_list
.next
;
177 return p
==&n
->list
? NULL
: list_entry(p
, struct vfsmount
, mnt_list
);
180 static void m_stop(struct seq_file
*m
, void *v
)
182 struct namespace *n
= m
->private;
186 static inline void mangle(struct seq_file
*m
, const char *s
)
188 seq_escape(m
, s
, " \t\n\\");
191 static int show_vfsmnt(struct seq_file
*m
, void *v
)
193 struct vfsmount
*mnt
= v
;
195 static struct proc_fs_info
{
199 { MS_SYNCHRONOUS
, ",sync" },
200 { MS_DIRSYNC
, ",dirsync" },
201 { MS_MANDLOCK
, ",mand" },
202 { MS_NOATIME
, ",noatime" },
203 { MS_NODIRATIME
, ",nodiratime" },
206 static struct proc_fs_info mnt_info
[] = {
207 { MNT_NOSUID
, ",nosuid" },
208 { MNT_NODEV
, ",nodev" },
209 { MNT_NOEXEC
, ",noexec" },
212 struct proc_fs_info
*fs_infop
;
213 char *path_buf
, *path
;
215 path_buf
= (char *) __get_free_page(GFP_KERNEL
);
218 path
= d_path(mnt
->mnt_root
, mnt
, path_buf
, PAGE_SIZE
);
220 mangle(m
, mnt
->mnt_devname
? mnt
->mnt_devname
: "none");
223 free_page((unsigned long) path_buf
);
225 mangle(m
, mnt
->mnt_sb
->s_type
->name
);
226 seq_puts(m
, mnt
->mnt_sb
->s_flags
& MS_RDONLY
? " ro" : " rw");
227 for (fs_infop
= fs_info
; fs_infop
->flag
; fs_infop
++) {
228 if (mnt
->mnt_sb
->s_flags
& fs_infop
->flag
)
229 seq_puts(m
, fs_infop
->str
);
231 for (fs_infop
= mnt_info
; fs_infop
->flag
; fs_infop
++) {
232 if (mnt
->mnt_flags
& fs_infop
->flag
)
233 seq_puts(m
, fs_infop
->str
);
235 if (mnt
->mnt_sb
->s_op
->show_options
)
236 err
= mnt
->mnt_sb
->s_op
->show_options(m
, mnt
);
237 seq_puts(m
, " 0 0\n");
241 struct seq_operations mounts_op
= {
249 * Doesn't take quota and stuff into account. IOW, in some cases it will
250 * give false negatives. The main reason why it's here is that we need
251 * a non-destructive way to look for easily umountable filesystems.
253 int may_umount(struct vfsmount
*mnt
)
255 if (atomic_read(&mnt
->mnt_count
) > 2)
260 void umount_tree(struct vfsmount
*mnt
)
265 for (p
= mnt
; p
; p
= next_mnt(p
, mnt
)) {
266 list_del(&p
->mnt_list
);
267 list_add(&p
->mnt_list
, &kill
);
270 while (!list_empty(&kill
)) {
271 mnt
= list_entry(kill
.next
, struct vfsmount
, mnt_list
);
272 list_del_init(&mnt
->mnt_list
);
273 if (mnt
->mnt_parent
== mnt
) {
274 spin_unlock(&dcache_lock
);
276 struct nameidata old_nd
;
277 detach_mnt(mnt
, &old_nd
);
278 spin_unlock(&dcache_lock
);
279 path_release(&old_nd
);
282 spin_lock(&dcache_lock
);
286 static int do_umount(struct vfsmount
*mnt
, int flags
)
288 struct super_block
* sb
= mnt
->mnt_sb
;
291 retval
= security_ops
->sb_umount(mnt
, flags
);
296 * If we may have to abort operations to get out of this
297 * mount, and they will themselves hold resources we must
298 * allow the fs to do things. In the Unix tradition of
299 * 'Gee thats tricky lets do it in userspace' the umount_begin
300 * might fail to complete on the first run through as other tasks
301 * must return, and the like. Thats for the mount program to worry
302 * about for the moment.
306 if( (flags
&MNT_FORCE
) && sb
->s_op
->umount_begin
)
307 sb
->s_op
->umount_begin(sb
);
311 * No sense to grab the lock for this test, but test itself looks
312 * somewhat bogus. Suggestions for better replacement?
313 * Ho-hum... In principle, we might treat that as umount + switch
314 * to rootfs. GC would eventually take care of the old vfsmount.
315 * Actually it makes sense, especially if rootfs would contain a
316 * /reboot - static binary that would close all descriptors and
317 * call reboot(9). Then init(8) could umount root and exec /reboot.
319 if (mnt
== current
->fs
->rootmnt
&& !(flags
& MNT_DETACH
)) {
321 * Special case for "unmounting" root ...
322 * we just try to remount it readonly.
324 down_write(&sb
->s_umount
);
325 if (!(sb
->s_flags
& MS_RDONLY
)) {
327 retval
= do_remount_sb(sb
, MS_RDONLY
, 0);
330 up_write(&sb
->s_umount
);
334 down_write(¤t
->namespace->sem
);
335 spin_lock(&dcache_lock
);
337 if (atomic_read(&sb
->s_active
) == 1) {
338 /* last instance - try to be smart */
339 spin_unlock(&dcache_lock
);
344 security_ops
->sb_umount_close(mnt
);
345 spin_lock(&dcache_lock
);
348 if (atomic_read(&mnt
->mnt_count
) == 2 || flags
& MNT_DETACH
) {
349 if (!list_empty(&mnt
->mnt_list
))
353 spin_unlock(&dcache_lock
);
355 security_ops
->sb_umount_busy(mnt
);
356 up_write(¤t
->namespace->sem
);
361 * Now umount can handle mount points as well as block devices.
362 * This is important for filesystems which use unnamed block devices.
364 * We now support a flag for forced unmount like the other 'big iron'
365 * unixes. Our API is identical to OSF/1 to avoid making a mess of AMD
368 asmlinkage
long sys_umount(char * name
, int flags
)
373 retval
= __user_walk(name
, LOOKUP_FOLLOW
, &nd
);
377 if (nd
.dentry
!= nd
.mnt
->mnt_root
)
379 if (!check_mnt(nd
.mnt
))
383 if (!capable(CAP_SYS_ADMIN
))
386 retval
= do_umount(nd
.mnt
, flags
);
394 * The 2.0 compatible umount. No flags.
397 asmlinkage
long sys_oldumount(char * name
)
399 return sys_umount(name
,0);
402 static int mount_is_safe(struct nameidata
*nd
)
404 if (capable(CAP_SYS_ADMIN
))
408 if (S_ISLNK(nd
->dentry
->d_inode
->i_mode
))
410 if (nd
->dentry
->d_inode
->i_mode
& S_ISVTX
) {
411 if (current
->uid
!= nd
->dentry
->d_inode
->i_uid
)
414 if (permission(nd
->dentry
->d_inode
, MAY_WRITE
))
420 static struct vfsmount
*copy_tree(struct vfsmount
*mnt
, struct dentry
*dentry
)
422 struct vfsmount
*p
, *next
, *q
, *res
;
426 res
= nd
.mnt
= q
= clone_mnt(p
, dentry
);
430 q
->mnt_mountpoint
= p
->mnt_mountpoint
;
432 while ( (next
= next_mnt(p
, mnt
)) != NULL
) {
433 while (p
!= next
->mnt_parent
) {
439 nd
.dentry
= p
->mnt_mountpoint
;
440 q
= clone_mnt(p
, p
->mnt_root
);
443 spin_lock(&dcache_lock
);
444 list_add_tail(&q
->mnt_list
, &res
->mnt_list
);
446 spin_unlock(&dcache_lock
);
451 spin_lock(&dcache_lock
);
453 spin_unlock(&dcache_lock
);
458 static int graft_tree(struct vfsmount
*mnt
, struct nameidata
*nd
)
461 if (mnt
->mnt_sb
->s_flags
& MS_NOUSER
)
464 if (S_ISDIR(nd
->dentry
->d_inode
->i_mode
) !=
465 S_ISDIR(mnt
->mnt_root
->d_inode
->i_mode
))
469 down(&nd
->dentry
->d_inode
->i_sem
);
470 if (IS_DEADDIR(nd
->dentry
->d_inode
))
473 err
= security_ops
->sb_check_sb(mnt
, nd
);
477 spin_lock(&dcache_lock
);
478 if (IS_ROOT(nd
->dentry
) || !d_unhashed(nd
->dentry
)) {
479 struct list_head head
;
481 list_add_tail(&head
, &mnt
->mnt_list
);
482 list_splice(&head
, current
->namespace->list
.prev
);
486 spin_unlock(&dcache_lock
);
488 up(&nd
->dentry
->d_inode
->i_sem
);
490 security_ops
->sb_post_addmount(mnt
, nd
);
497 static int do_loopback(struct nameidata
*nd
, char *old_name
, int recurse
)
499 struct nameidata old_nd
;
500 struct vfsmount
*mnt
= NULL
;
501 int err
= mount_is_safe(nd
);
504 if (!old_name
|| !*old_name
)
506 err
= path_lookup(old_name
, LOOKUP_FOLLOW
, &old_nd
);
510 down_write(¤t
->namespace->sem
);
512 if (check_mnt(nd
->mnt
) && (!recurse
|| check_mnt(old_nd
.mnt
))) {
515 mnt
= copy_tree(old_nd
.mnt
, old_nd
.dentry
);
517 mnt
= clone_mnt(old_nd
.mnt
, old_nd
.dentry
);
521 err
= graft_tree(mnt
, nd
);
523 spin_lock(&dcache_lock
);
525 spin_unlock(&dcache_lock
);
530 up_write(¤t
->namespace->sem
);
531 path_release(&old_nd
);
536 * change filesystem flags. dir should be a physical root of filesystem.
537 * If you've mounted a non-root directory somewhere and want to do remount
538 * on it - tough luck.
541 static int do_remount(struct nameidata
*nd
,int flags
,int mnt_flags
,void *data
)
544 struct super_block
* sb
= nd
->mnt
->mnt_sb
;
546 if (!capable(CAP_SYS_ADMIN
))
549 if (!check_mnt(nd
->mnt
))
552 if (nd
->dentry
!= nd
->mnt
->mnt_root
)
555 down_write(&sb
->s_umount
);
556 err
= do_remount_sb(sb
, flags
, data
);
558 nd
->mnt
->mnt_flags
=mnt_flags
;
559 up_write(&sb
->s_umount
);
561 security_ops
->sb_post_remount(nd
->mnt
, flags
, data
);
565 static int do_move_mount(struct nameidata
*nd
, char *old_name
)
567 struct nameidata old_nd
, parent_nd
;
570 if (!capable(CAP_SYS_ADMIN
))
572 if (!old_name
|| !*old_name
)
574 err
= path_lookup(old_name
, LOOKUP_FOLLOW
, &old_nd
);
578 down_write(¤t
->namespace->sem
);
579 while(d_mountpoint(nd
->dentry
) && follow_down(&nd
->mnt
, &nd
->dentry
))
582 if (!check_mnt(nd
->mnt
) || !check_mnt(old_nd
.mnt
))
586 down(&nd
->dentry
->d_inode
->i_sem
);
587 if (IS_DEADDIR(nd
->dentry
->d_inode
))
590 spin_lock(&dcache_lock
);
591 if (!IS_ROOT(nd
->dentry
) && d_unhashed(nd
->dentry
))
595 if (old_nd
.dentry
!= old_nd
.mnt
->mnt_root
)
598 if (old_nd
.mnt
== old_nd
.mnt
->mnt_parent
)
601 if (S_ISDIR(nd
->dentry
->d_inode
->i_mode
) !=
602 S_ISDIR(old_nd
.dentry
->d_inode
->i_mode
))
606 for (p
= nd
->mnt
; p
->mnt_parent
!=p
; p
= p
->mnt_parent
)
611 detach_mnt(old_nd
.mnt
, &parent_nd
);
612 attach_mnt(old_nd
.mnt
, nd
);
614 spin_unlock(&dcache_lock
);
616 up(&nd
->dentry
->d_inode
->i_sem
);
618 up_write(¤t
->namespace->sem
);
620 path_release(&parent_nd
);
621 path_release(&old_nd
);
625 static int do_add_mount(struct nameidata
*nd
, char *type
, int flags
,
626 int mnt_flags
, char *name
, void *data
)
628 struct vfsmount
*mnt
;
631 if (!type
|| !memchr(type
, 0, PAGE_SIZE
))
634 /* we need capabilities... */
635 if (!capable(CAP_SYS_ADMIN
))
638 mnt
= do_kern_mount(type
, flags
, name
, data
);
643 down_write(¤t
->namespace->sem
);
644 /* Something was mounted here while we slept */
645 while(d_mountpoint(nd
->dentry
) && follow_down(&nd
->mnt
, &nd
->dentry
))
648 if (!check_mnt(nd
->mnt
))
651 /* Refuse the same filesystem on the same mount point */
653 if (nd
->mnt
->mnt_sb
== mnt
->mnt_sb
&& nd
->mnt
->mnt_root
== nd
->dentry
)
656 mnt
->mnt_flags
= mnt_flags
;
657 err
= graft_tree(mnt
, nd
);
659 up_write(¤t
->namespace->sem
);
665 static int copy_mount_options (const void *data
, unsigned long *where
)
675 if (!(page
= __get_free_page(GFP_KERNEL
)))
678 /* We only care that *some* data at the address the user
679 * gave us is valid. Just in case, we'll zero
680 * the remainder of the page.
682 /* copy_from_user cannot cross TASK_SIZE ! */
683 size
= TASK_SIZE
- (unsigned long)data
;
684 if (size
> PAGE_SIZE
)
687 i
= size
- copy_from_user((void *)page
, data
, size
);
693 memset((char *)page
+ i
, 0, PAGE_SIZE
- i
);
699 * Flags is a 32-bit value that allows up to 31 non-fs dependent flags to
700 * be given to the mount() call (ie: read-only, no-dev, no-suid etc).
702 * data is a (void *) that can point to any structure up to
703 * PAGE_SIZE-1 bytes, which can contain arbitrary fs-dependent
704 * information (or be NULL).
706 * Pre-0.97 versions of mount() didn't have a flags word.
707 * When the flags word was introduced its top half was required
708 * to have the magic value 0xC0ED, and this remained so until 2.4.0-test9.
709 * Therefore, if this magic number is present, it carries no information
710 * and must be discarded.
712 long do_mount(char * dev_name
, char * dir_name
, char *type_page
,
713 unsigned long flags
, void *data_page
)
720 if ((flags
& MS_MGC_MSK
) == MS_MGC_VAL
)
721 flags
&= ~MS_MGC_MSK
;
723 /* Basic sanity checks */
725 if (!dir_name
|| !*dir_name
|| !memchr(dir_name
, 0, PAGE_SIZE
))
727 if (dev_name
&& !memchr(dev_name
, 0, PAGE_SIZE
))
730 /* Separate the per-mountpoint flags */
731 if (flags
& MS_NOSUID
)
732 mnt_flags
|= MNT_NOSUID
;
733 if (flags
& MS_NODEV
)
734 mnt_flags
|= MNT_NODEV
;
735 if (flags
& MS_NOEXEC
)
736 mnt_flags
|= MNT_NOEXEC
;
737 flags
&= ~(MS_NOSUID
|MS_NOEXEC
|MS_NODEV
);
739 /* ... and get the mountpoint */
740 retval
= path_lookup(dir_name
, LOOKUP_FOLLOW
, &nd
);
744 retval
= security_ops
->sb_mount(dev_name
, &nd
, type_page
, flags
, data_page
);
748 if (flags
& MS_REMOUNT
)
749 retval
= do_remount(&nd
, flags
& ~MS_REMOUNT
, mnt_flags
,
751 else if (flags
& MS_BIND
)
752 retval
= do_loopback(&nd
, dev_name
, flags
& MS_REC
);
753 else if (flags
& MS_MOVE
)
754 retval
= do_move_mount(&nd
, dev_name
);
756 retval
= do_add_mount(&nd
, type_page
, flags
, mnt_flags
,
757 dev_name
, data_page
);
763 int copy_namespace(int flags
, struct task_struct
*tsk
)
765 struct namespace *namespace = tsk
->namespace;
766 struct namespace *new_ns
;
767 struct vfsmount
*rootmnt
= NULL
, *pwdmnt
= NULL
, *altrootmnt
= NULL
;
768 struct fs_struct
*fs
= tsk
->fs
;
773 get_namespace(namespace);
775 if (! (flags
& CLONE_NEWNS
))
778 if (!capable(CAP_SYS_ADMIN
)) {
779 put_namespace(namespace);
783 new_ns
= kmalloc(sizeof(struct namespace *), GFP_KERNEL
);
787 atomic_set(&new_ns
->count
, 1);
788 init_rwsem(&new_ns
->sem
);
790 INIT_LIST_HEAD(&new_ns
->list
);
792 down_write(&tsk
->namespace->sem
);
793 /* First pass: copy the tree topology */
794 new_ns
->root
= copy_tree(namespace->root
, namespace->root
->mnt_root
);
795 spin_lock(&dcache_lock
);
796 list_add_tail(&new_ns
->list
, &new_ns
->root
->mnt_list
);
797 spin_unlock(&dcache_lock
);
799 /* Second pass: switch the tsk->fs->* elements */
801 struct vfsmount
*p
, *q
;
802 write_lock(&fs
->lock
);
807 if (p
== fs
->rootmnt
) {
809 fs
->rootmnt
= mntget(q
);
811 if (p
== fs
->pwdmnt
) {
813 fs
->pwdmnt
= mntget(q
);
815 if (p
== fs
->altrootmnt
) {
817 fs
->altrootmnt
= mntget(q
);
819 p
= next_mnt(p
, namespace->root
);
820 q
= next_mnt(q
, new_ns
->root
);
822 write_unlock(&fs
->lock
);
824 up_write(&tsk
->namespace->sem
);
826 tsk
->namespace = new_ns
;
835 put_namespace(namespace);
839 put_namespace(namespace);
843 asmlinkage
long sys_mount(char * dev_name
, char * dir_name
, char * type
,
844 unsigned long flags
, void * data
)
847 unsigned long data_page
;
848 unsigned long type_page
;
849 unsigned long dev_page
;
852 retval
= copy_mount_options (type
, &type_page
);
856 dir_page
= getname(dir_name
);
857 retval
= PTR_ERR(dir_page
);
858 if (IS_ERR(dir_page
))
861 retval
= copy_mount_options (dev_name
, &dev_page
);
865 retval
= copy_mount_options (data
, &data_page
);
870 retval
= do_mount((char*)dev_page
, dir_page
, (char*)type_page
,
871 flags
, (void*)data_page
);
873 free_page(data_page
);
880 free_page(type_page
);
884 static void chroot_fs_refs(struct nameidata
*old_nd
, struct nameidata
*new_nd
)
886 struct task_struct
*g
, *p
;
887 struct fs_struct
*fs
;
889 read_lock(&tasklist_lock
);
890 do_each_thread(g
, p
) {
894 atomic_inc(&fs
->count
);
896 if (fs
->root
==old_nd
->dentry
&&fs
->rootmnt
==old_nd
->mnt
)
897 set_fs_root(fs
, new_nd
->mnt
, new_nd
->dentry
);
898 if (fs
->pwd
==old_nd
->dentry
&&fs
->pwdmnt
==old_nd
->mnt
)
899 set_fs_pwd(fs
, new_nd
->mnt
, new_nd
->dentry
);
903 } while_each_thread(g
, p
);
904 read_unlock(&tasklist_lock
);
908 * Moves the current root to put_root, and sets root/cwd of all processes
909 * which had them on the old root to new_root.
912 * - we don't move root/cwd if they are not at the root (reason: if something
913 * cared enough to change them, it's probably wrong to force them elsewhere)
914 * - it's okay to pick a root that isn't the root of a file system, e.g.
915 * /nfs/my_root where /nfs is the mount point. It must be a mountpoint,
916 * though, so you may need to say mount --bind /nfs/my_root /nfs/my_root
920 asmlinkage
long sys_pivot_root(const char *new_root
, const char *put_old
)
922 struct vfsmount
*tmp
;
923 struct nameidata new_nd
, old_nd
, parent_nd
, root_parent
, user_nd
;
926 if (!capable(CAP_SYS_ADMIN
))
931 error
= __user_walk(new_root
, LOOKUP_FOLLOW
|LOOKUP_DIRECTORY
, &new_nd
);
935 if (!check_mnt(new_nd
.mnt
))
938 error
= __user_walk(put_old
, LOOKUP_FOLLOW
|LOOKUP_DIRECTORY
, &old_nd
);
942 error
= security_ops
->sb_pivotroot(&old_nd
, &new_nd
);
944 path_release(&old_nd
);
948 read_lock(¤t
->fs
->lock
);
949 user_nd
.mnt
= mntget(current
->fs
->rootmnt
);
950 user_nd
.dentry
= dget(current
->fs
->root
);
951 read_unlock(¤t
->fs
->lock
);
952 down_write(¤t
->namespace->sem
);
953 down(&old_nd
.dentry
->d_inode
->i_sem
);
955 if (!check_mnt(user_nd
.mnt
))
958 if (IS_DEADDIR(new_nd
.dentry
->d_inode
))
960 if (d_unhashed(new_nd
.dentry
) && !IS_ROOT(new_nd
.dentry
))
962 if (d_unhashed(old_nd
.dentry
) && !IS_ROOT(old_nd
.dentry
))
965 if (new_nd
.mnt
== user_nd
.mnt
|| old_nd
.mnt
== user_nd
.mnt
)
966 goto out2
; /* loop */
968 if (user_nd
.mnt
->mnt_root
!= user_nd
.dentry
)
970 if (new_nd
.mnt
->mnt_root
!= new_nd
.dentry
)
971 goto out2
; /* not a mountpoint */
972 tmp
= old_nd
.mnt
; /* make sure we can reach put_old from new_root */
973 spin_lock(&dcache_lock
);
974 if (tmp
!= new_nd
.mnt
) {
976 if (tmp
->mnt_parent
== tmp
)
978 if (tmp
->mnt_parent
== new_nd
.mnt
)
980 tmp
= tmp
->mnt_parent
;
982 if (!is_subdir(tmp
->mnt_mountpoint
, new_nd
.dentry
))
984 } else if (!is_subdir(old_nd
.dentry
, new_nd
.dentry
))
986 detach_mnt(new_nd
.mnt
, &parent_nd
);
987 detach_mnt(user_nd
.mnt
, &root_parent
);
988 attach_mnt(user_nd
.mnt
, &old_nd
);
989 attach_mnt(new_nd
.mnt
, &root_parent
);
990 spin_unlock(&dcache_lock
);
991 chroot_fs_refs(&user_nd
, &new_nd
);
992 security_ops
->sb_post_pivotroot(&user_nd
, &new_nd
);
994 path_release(&root_parent
);
995 path_release(&parent_nd
);
997 up(&old_nd
.dentry
->d_inode
->i_sem
);
998 up_write(¤t
->namespace->sem
);
999 path_release(&user_nd
);
1000 path_release(&old_nd
);
1002 path_release(&new_nd
);
1007 spin_unlock(&dcache_lock
);
1011 static void __init
init_mount_tree(void)
1013 struct vfsmount
*mnt
;
1014 struct namespace *namespace;
1015 struct task_struct
*g
, *p
;
1017 mnt
= do_kern_mount("rootfs", 0, "rootfs", NULL
);
1019 panic("Can't create rootfs");
1020 namespace = kmalloc(sizeof(*namespace), GFP_KERNEL
);
1022 panic("Can't allocate initial namespace");
1023 atomic_set(&namespace->count
, 1);
1024 INIT_LIST_HEAD(&namespace->list
);
1025 init_rwsem(&namespace->sem
);
1026 list_add(&mnt
->mnt_list
, &namespace->list
);
1027 namespace->root
= mnt
;
1029 init_task
.namespace = namespace;
1030 read_lock(&tasklist_lock
);
1031 do_each_thread(g
, p
) {
1032 get_namespace(namespace);
1033 p
->namespace = namespace;
1034 } while_each_thread(g
, p
);
1035 read_unlock(&tasklist_lock
);
1037 set_fs_pwd(current
->fs
, namespace->root
, namespace->root
->mnt_root
);
1038 set_fs_root(current
->fs
, namespace->root
, namespace->root
->mnt_root
);
1041 void __init
mnt_init(unsigned long mempages
)
1043 struct list_head
*d
;
1044 unsigned long order
;
1045 unsigned int nr_hash
;
1048 mnt_cache
= kmem_cache_create("mnt_cache", sizeof(struct vfsmount
),
1049 0, SLAB_HWCACHE_ALIGN
, NULL
, NULL
);
1051 panic("Cannot create vfsmount cache");
1054 mount_hashtable
= (struct list_head
*)
1055 __get_free_pages(GFP_ATOMIC
, order
);
1057 if (!mount_hashtable
)
1058 panic("Failed to allocate mount hash table\n");
1061 * Find the power-of-two list-heads that can fit into the allocation..
1062 * We don't guarantee that "sizeof(struct list_head)" is necessarily
1065 nr_hash
= (1UL << order
) * PAGE_SIZE
/ sizeof(struct list_head
);
1069 } while ((nr_hash
>> hash_bits
) != 0);
1073 * Re-calculate the actual number of entries and the mask
1074 * from the number of bits we can fit.
1076 nr_hash
= 1UL << hash_bits
;
1077 hash_mask
= nr_hash
-1;
1079 printk("Mount-cache hash table entries: %d (order: %ld, %ld bytes)\n",
1080 nr_hash
, order
, (PAGE_SIZE
<< order
));
1082 /* And initialize the newly allocated array */
1083 d
= mount_hashtable
;