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/smp_lock.h>
14 #include <linux/init.h>
15 #include <linux/quotaops.h>
16 #include <linux/acct.h>
17 #include <linux/module.h>
18 #include <linux/devfs_fs_kernel.h>
20 #include <asm/uaccess.h>
22 #include <linux/nfs_fs.h>
23 #include <linux/nfs_fs_sb.h>
24 #include <linux/nfs_mount.h>
26 struct vfsmount *do_kern_mount(char *type, int flags, char *name, void *data);
27 int do_remount_sb(struct super_block *sb, int flags, void * data);
28 void kill_super(struct super_block *sb);
30 static struct list_head *mount_hashtable;
31 static int hash_mask, hash_bits;
32 static kmem_cache_t *mnt_cache;
34 static LIST_HEAD(vfsmntlist);
35 static DECLARE_MUTEX(mount_sem);
38 struct vfsmount *root_vfsmnt;
40 static inline unsigned long hash(struct vfsmount *mnt, struct dentry *dentry)
42 unsigned long tmp = ((unsigned long) mnt / L1_CACHE_BYTES);
43 tmp += ((unsigned long) dentry / L1_CACHE_BYTES);
44 tmp = tmp + (tmp >> hash_bits);
45 return tmp & hash_mask;
48 struct vfsmount *alloc_vfsmnt(void)
50 struct vfsmount *mnt = kmem_cache_alloc(mnt_cache, GFP_KERNEL);
52 memset(mnt, 0, sizeof(struct vfsmount));
53 atomic_set(&mnt->mnt_count,1);
54 INIT_LIST_HEAD(&mnt->mnt_hash);
55 INIT_LIST_HEAD(&mnt->mnt_child);
56 INIT_LIST_HEAD(&mnt->mnt_mounts);
57 INIT_LIST_HEAD(&mnt->mnt_list);
62 void free_vfsmnt(struct vfsmount *mnt)
65 kfree(mnt->mnt_devname);
66 kmem_cache_free(mnt_cache, mnt);
69 void set_devname(struct vfsmount *mnt, const char *name)
72 int size = strlen(name)+1;
73 char * newname = kmalloc(size, GFP_KERNEL);
75 memcpy(newname, name, size);
76 mnt->mnt_devname = newname;
81 struct vfsmount *lookup_mnt(struct vfsmount *mnt, struct dentry *dentry)
83 struct list_head * head = mount_hashtable + hash(mnt, dentry);
84 struct list_head * tmp = head;
92 p = list_entry(tmp, struct vfsmount, mnt_hash);
93 if (p->mnt_parent == mnt && p->mnt_mountpoint == dentry)
99 static int check_mnt(struct vfsmount *mnt)
101 spin_lock(&dcache_lock);
102 while (mnt->mnt_parent != mnt)
103 mnt = mnt->mnt_parent;
104 spin_unlock(&dcache_lock);
105 return mnt == root_vfsmnt;
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(&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();
151 mnt->mnt_flags = old->mnt_flags;
152 set_devname(mnt, old->mnt_devname);
153 atomic_inc(&sb->s_active);
155 mnt->mnt_root = dget(root);
160 void __mntput(struct vfsmount *mnt)
162 struct super_block *sb = mnt->mnt_sb;
168 /* Use octal escapes, like mount does, for embedded spaces etc. */
169 static unsigned char need_escaping[] = { ' ', '\t', '\n', '\\' };
172 mangle(const unsigned char *s, char *buf, int len) {
177 while(*s && sp-buf < len-3) {
178 for (n = 0; n < sizeof(need_escaping); n++) {
179 if (*s == need_escaping[n]) {
181 *sp++ = '0' + ((*s & 0300) >> 6);
182 *sp++ = '0' + ((*s & 070) >> 3);
183 *sp++ = '0' + (*s & 07);
191 return sp - buf; /* no trailing NUL */
194 static struct proc_fs_info {
198 { MS_SYNCHRONOUS, ",sync" },
199 { MS_MANDLOCK, ",mand" },
200 { MS_NOATIME, ",noatime" },
201 { MS_NODIRATIME, ",nodiratime" },
205 static struct proc_fs_info mnt_info[] = {
206 { MNT_NOSUID, ",nosuid" },
207 { MNT_NODEV, ",nodev" },
208 { MNT_NOEXEC, ",noexec" },
212 static struct proc_nfs_info {
217 { NFS_MOUNT_SOFT, ",soft", ",hard" },
218 { NFS_MOUNT_INTR, ",intr", "" },
219 { NFS_MOUNT_POSIX, ",posix", "" },
220 { NFS_MOUNT_TCP, ",tcp", ",udp" },
221 { NFS_MOUNT_NOCTO, ",nocto", "" },
222 { NFS_MOUNT_NOAC, ",noac", "" },
223 { NFS_MOUNT_NONLM, ",nolock", ",lock" },
224 { NFS_MOUNT_BROKEN_SUID, ",broken_suid", "" },
228 int get_filesystem_info( char *buf )
231 struct proc_fs_info *fs_infop;
232 struct proc_nfs_info *nfs_infop;
233 struct nfs_server *nfss;
235 char *path, *buffer = (char *) __get_free_page(GFP_KERNEL);
237 if (!buffer) return 0;
240 #define FREEROOM ((int)PAGE_SIZE-200-len)
241 #define MANGLE(s) len += mangle((s), buf+len, FREEROOM);
243 for (p = vfsmntlist.next; p != &vfsmntlist; p = p->next) {
244 struct vfsmount *tmp = list_entry(p, struct vfsmount, mnt_list);
245 path = d_path(tmp->mnt_root, tmp, buffer, PAGE_SIZE);
248 MANGLE(tmp->mnt_devname ? tmp->mnt_devname : "none");
252 MANGLE(tmp->mnt_sb->s_type->name);
253 len += sprintf(buf+len, " %s",
254 tmp->mnt_sb->s_flags & MS_RDONLY ? "ro" : "rw");
255 for (fs_infop = fs_info; fs_infop->flag; fs_infop++) {
256 if (tmp->mnt_sb->s_flags & fs_infop->flag)
257 MANGLE(fs_infop->str);
259 for (fs_infop = mnt_info; fs_infop->flag; fs_infop++) {
260 if (tmp->mnt_flags & fs_infop->flag)
261 MANGLE(fs_infop->str);
263 if (!strcmp("nfs", tmp->mnt_sb->s_type->name)) {
264 nfss = &tmp->mnt_sb->u.nfs_sb.s_server;
265 len += sprintf(buf+len, ",v%d", nfss->rpc_ops->version);
267 len += sprintf(buf+len, ",rsize=%d", nfss->rsize);
269 len += sprintf(buf+len, ",wsize=%d", nfss->wsize);
271 if (nfss->timeo != 7*HZ/10) {
272 len += sprintf(buf+len, ",timeo=%d",
275 if (nfss->retrans != 3) {
276 len += sprintf(buf+len, ",retrans=%d",
280 if (nfss->acregmin != 3*HZ) {
281 len += sprintf(buf+len, ",acregmin=%d",
284 if (nfss->acregmax != 60*HZ) {
285 len += sprintf(buf+len, ",acregmax=%d",
288 if (nfss->acdirmin != 30*HZ) {
289 len += sprintf(buf+len, ",acdirmin=%d",
292 if (nfss->acdirmax != 60*HZ) {
293 len += sprintf(buf+len, ",acdirmax=%d",
296 for (nfs_infop = nfs_info; nfs_infop->flag; nfs_infop++) {
298 if (nfss->flags & nfs_infop->flag)
299 str = nfs_infop->str;
301 str = nfs_infop->nostr;
304 len += sprintf(buf+len, ",addr=");
305 MANGLE(nfss->hostname);
307 len += sprintf(buf + len, " 0 0\n");
310 len += sprintf(buf+len, "# truncated\n");
316 free_page((unsigned long) buffer);
323 * Doesn't take quota and stuff into account. IOW, in some cases it will
324 * give false negatives. The main reason why it's here is that we need
325 * a non-destructive way to look for easily umountable filesystems.
327 int may_umount(struct vfsmount *mnt)
329 if (atomic_read(&mnt->mnt_count) > 2)
334 void umount_tree(struct vfsmount *mnt)
339 if (list_empty(&mnt->mnt_list))
342 for (p = mnt; p; p = next_mnt(p, mnt)) {
343 list_del(&p->mnt_list);
344 list_add(&p->mnt_list, &kill);
347 while (!list_empty(&kill)) {
348 mnt = list_entry(kill.next, struct vfsmount, mnt_list);
349 list_del_init(&mnt->mnt_list);
350 if (mnt->mnt_parent == mnt) {
351 spin_unlock(&dcache_lock);
353 struct nameidata old_nd;
354 detach_mnt(mnt, &old_nd);
355 spin_unlock(&dcache_lock);
356 path_release(&old_nd);
359 spin_lock(&dcache_lock);
363 static int do_umount(struct vfsmount *mnt, int flags)
365 struct super_block * sb = mnt->mnt_sb;
369 * If we may have to abort operations to get out of this
370 * mount, and they will themselves hold resources we must
371 * allow the fs to do things. In the Unix tradition of
372 * 'Gee thats tricky lets do it in userspace' the umount_begin
373 * might fail to complete on the first run through as other tasks
374 * must return, and the like. Thats for the mount program to worry
375 * about for the moment.
379 if( (flags&MNT_FORCE) && sb->s_op->umount_begin)
380 sb->s_op->umount_begin(sb);
384 * No sense to grab the lock for this test, but test itself looks
385 * somewhat bogus. Suggestions for better replacement?
386 * Ho-hum... In principle, we might treat that as umount + switch
387 * to rootfs. GC would eventually take care of the old vfsmount.
388 * Actually it makes sense, especially if rootfs would contain a
389 * /reboot - static binary that would close all descriptors and
390 * call reboot(9). Then init(8) could umount root and exec /reboot.
392 if (mnt == current->fs->rootmnt && !(flags & MNT_DETACH)) {
394 * Special case for "unmounting" root ...
395 * we just try to remount it readonly.
397 down_write(&sb->s_umount);
398 if (!(sb->s_flags & MS_RDONLY)) {
400 retval = do_remount_sb(sb, MS_RDONLY, 0);
403 up_write(&sb->s_umount);
408 spin_lock(&dcache_lock);
410 if (atomic_read(&sb->s_active) == 1) {
411 /* last instance - try to be smart */
412 spin_unlock(&dcache_lock);
415 acct_auto_close(sb->s_dev);
417 spin_lock(&dcache_lock);
420 if (atomic_read(&mnt->mnt_count) == 2 || flags & MNT_DETACH) {
424 spin_unlock(&dcache_lock);
430 * Now umount can handle mount points as well as block devices.
431 * This is important for filesystems which use unnamed block devices.
433 * We now support a flag for forced unmount like the other 'big iron'
434 * unixes. Our API is identical to OSF/1 to avoid making a mess of AMD
437 asmlinkage long sys_umount(char * name, int flags)
443 kname = getname(name);
444 retval = PTR_ERR(kname);
448 if (path_init(kname, LOOKUP_POSITIVE|LOOKUP_FOLLOW, &nd))
449 retval = path_walk(kname, &nd);
454 if (nd.dentry != nd.mnt->mnt_root)
456 if (!check_mnt(nd.mnt))
460 if (!capable(CAP_SYS_ADMIN))
463 retval = do_umount(nd.mnt, flags);
471 * The 2.0 compatible umount. No flags.
474 asmlinkage long sys_oldumount(char * name)
476 return sys_umount(name,0);
479 static int mount_is_safe(struct nameidata *nd)
481 if (capable(CAP_SYS_ADMIN))
485 if (S_ISLNK(nd->dentry->d_inode->i_mode))
487 if (nd->dentry->d_inode->i_mode & S_ISVTX) {
488 if (current->uid != nd->dentry->d_inode->i_uid)
491 if (permission(nd->dentry->d_inode, MAY_WRITE))
497 static struct vfsmount *copy_tree(struct vfsmount *mnt, struct dentry *dentry)
499 struct vfsmount *p, *next, *q, *res;
503 res = nd.mnt = q = clone_mnt(p, dentry);
507 q->mnt_mountpoint = p->mnt_mountpoint;
509 while ( (next = next_mnt(p, mnt)) != NULL) {
510 while (p != next->mnt_parent) {
516 nd.dentry = p->mnt_mountpoint;
517 q = clone_mnt(p, p->mnt_root);
520 spin_lock(&dcache_lock);
521 list_add_tail(&q->mnt_list, &res->mnt_list);
523 spin_unlock(&dcache_lock);
528 spin_lock(&dcache_lock);
530 spin_unlock(&dcache_lock);
535 /* Will become static */
536 int graft_tree(struct vfsmount *mnt, struct nameidata *nd)
539 if (mnt->mnt_sb->s_flags & MS_NOUSER)
542 if (S_ISDIR(nd->dentry->d_inode->i_mode) !=
543 S_ISDIR(mnt->mnt_root->d_inode->i_mode))
547 down(&nd->dentry->d_inode->i_zombie);
548 if (IS_DEADDIR(nd->dentry->d_inode))
551 spin_lock(&dcache_lock);
552 if (IS_ROOT(nd->dentry) || !d_unhashed(nd->dentry)) {
553 struct list_head head;
555 list_add_tail(&head, &mnt->mnt_list);
556 list_splice(&head, vfsmntlist.prev);
560 spin_unlock(&dcache_lock);
562 up(&nd->dentry->d_inode->i_zombie);
569 static int do_loopback(struct nameidata *nd, char *old_name, int recurse)
571 struct nameidata old_nd;
572 struct vfsmount *mnt = NULL;
573 int err = mount_is_safe(nd);
576 if (!old_name || !*old_name)
578 if (path_init(old_name, LOOKUP_POSITIVE|LOOKUP_FOLLOW, &old_nd))
579 err = path_walk(old_name, &old_nd);
585 if (check_mnt(nd->mnt) && (!recurse || check_mnt(old_nd.mnt))) {
588 mnt = copy_tree(old_nd.mnt, old_nd.dentry);
590 mnt = clone_mnt(old_nd.mnt, old_nd.dentry);
594 err = graft_tree(mnt, nd);
601 path_release(&old_nd);
606 * change filesystem flags. dir should be a physical root of filesystem.
607 * If you've mounted a non-root directory somewhere and want to do remount
608 * on it - tough luck.
611 static int do_remount(struct nameidata *nd,int flags,int mnt_flags,void *data)
614 struct super_block * sb = nd->mnt->mnt_sb;
616 if (!capable(CAP_SYS_ADMIN))
619 if (!check_mnt(nd->mnt))
622 if (nd->dentry != nd->mnt->mnt_root)
625 down_write(&sb->s_umount);
626 err = do_remount_sb(sb, flags, data);
628 nd->mnt->mnt_flags=mnt_flags;
629 up_write(&sb->s_umount);
633 static int do_add_mount(struct nameidata *nd, char *type, int flags,
634 int mnt_flags, char *name, void *data)
636 struct vfsmount *mnt = do_kern_mount(type, flags, name, data);
637 int err = PTR_ERR(mnt);
643 /* Something was mounted here while we slept */
644 while(d_mountpoint(nd->dentry) && follow_down(&nd->mnt, &nd->dentry))
647 if (!check_mnt(nd->mnt))
650 /* Refuse the same filesystem on the same mount point */
652 if (nd->mnt->mnt_sb == mnt->mnt_sb && nd->mnt->mnt_root == nd->dentry)
655 mnt->mnt_flags = mnt_flags;
656 err = graft_tree(mnt, nd);
664 static int copy_mount_options (const void *data, unsigned long *where)
674 if (!(page = __get_free_page(GFP_KERNEL)))
677 /* We only care that *some* data at the address the user
678 * gave us is valid. Just in case, we'll zero
679 * the remainder of the page.
681 /* copy_from_user cannot cross TASK_SIZE ! */
682 size = TASK_SIZE - (unsigned long)data;
683 if (size > PAGE_SIZE)
686 i = size - copy_from_user((void *)page, data, size);
692 memset((char *)page + i, 0, PAGE_SIZE - i);
698 * Flags is a 32-bit value that allows up to 31 non-fs dependent flags to
699 * be given to the mount() call (ie: read-only, no-dev, no-suid etc).
701 * data is a (void *) that can point to any structure up to
702 * PAGE_SIZE-1 bytes, which can contain arbitrary fs-dependent
703 * information (or be NULL).
705 * Pre-0.97 versions of mount() didn't have a flags word.
706 * When the flags word was introduced its top half was required
707 * to have the magic value 0xC0ED, and this remained so until 2.4.0-test9.
708 * Therefore, if this magic number is present, it carries no information
709 * and must be discarded.
711 long do_mount(char * dev_name, char * dir_name, char *type_page,
712 unsigned long flags, void *data_page)
719 if ((flags & MS_MGC_MSK) == MS_MGC_VAL)
720 flags &= ~MS_MGC_MSK;
722 /* Basic sanity checks */
724 if (!dir_name || !*dir_name || !memchr(dir_name, 0, PAGE_SIZE))
726 if (dev_name && !memchr(dev_name, 0, PAGE_SIZE))
729 /* Separate the per-mountpoint flags */
730 if (flags & MS_NOSUID)
731 mnt_flags |= MNT_NOSUID;
732 if (flags & MS_NODEV)
733 mnt_flags |= MNT_NODEV;
734 if (flags & MS_NOEXEC)
735 mnt_flags |= MNT_NOEXEC;
736 flags &= ~(MS_NOSUID|MS_NOEXEC|MS_NODEV);
738 /* ... and get the mountpoint */
739 if (path_init(dir_name, LOOKUP_FOLLOW|LOOKUP_POSITIVE, &nd))
740 retval = path_walk(dir_name, &nd);
744 if (flags & MS_REMOUNT)
745 retval = do_remount(&nd, flags & ~MS_REMOUNT, mnt_flags,
747 else if (flags & MS_BIND)
748 retval = do_loopback(&nd, dev_name, flags & MS_REC);
750 retval = do_add_mount(&nd, type_page, flags, mnt_flags,
751 dev_name, data_page);
756 asmlinkage long sys_mount(char * dev_name, char * dir_name, char * type,
757 unsigned long flags, void * data)
760 unsigned long data_page;
761 unsigned long type_page;
762 unsigned long dev_page;
765 retval = copy_mount_options (type, &type_page);
769 dir_page = getname(dir_name);
770 retval = PTR_ERR(dir_page);
771 if (IS_ERR(dir_page))
774 retval = copy_mount_options (dev_name, &dev_page);
778 retval = copy_mount_options (data, &data_page);
783 retval = do_mount((char*)dev_page, dir_page, (char*)type_page,
784 flags, (void*)data_page);
786 free_page(data_page);
793 free_page(type_page);
797 static void chroot_fs_refs(struct nameidata *old_nd, struct nameidata *new_nd)
799 struct task_struct *p;
800 struct fs_struct *fs;
802 read_lock(&tasklist_lock);
807 atomic_inc(&fs->count);
809 if (fs->root==old_nd->dentry&&fs->rootmnt==old_nd->mnt)
810 set_fs_root(fs, new_nd->mnt, new_nd->dentry);
811 if (fs->pwd==old_nd->dentry&&fs->pwdmnt==old_nd->mnt)
812 set_fs_pwd(fs, new_nd->mnt, new_nd->dentry);
817 read_unlock(&tasklist_lock);
821 * Moves the current root to put_root, and sets root/cwd of all processes
822 * which had them on the old root to new_root.
825 * - we don't move root/cwd if they are not at the root (reason: if something
826 * cared enough to change them, it's probably wrong to force them elsewhere)
827 * - it's okay to pick a root that isn't the root of a file system, e.g.
828 * /nfs/my_root where /nfs is the mount point. It must be a mountpoint,
829 * though, so you may need to say mount --bind /nfs/my_root /nfs/my_root
833 asmlinkage long sys_pivot_root(const char *new_root, const char *put_old)
835 struct vfsmount *tmp;
836 struct nameidata new_nd, old_nd, parent_nd, root_parent, user_nd;
840 if (!capable(CAP_SYS_ADMIN))
845 name = getname(new_root);
846 error = PTR_ERR(name);
850 if (path_init(name, LOOKUP_POSITIVE|LOOKUP_FOLLOW|LOOKUP_DIRECTORY, &new_nd))
851 error = path_walk(name, &new_nd);
856 if (!check_mnt(new_nd.mnt))
859 name = getname(put_old);
860 error = PTR_ERR(name);
864 if (path_init(name, LOOKUP_POSITIVE|LOOKUP_FOLLOW|LOOKUP_DIRECTORY, &old_nd))
865 error = path_walk(name, &old_nd);
870 read_lock(¤t->fs->lock);
871 user_nd.mnt = mntget(current->fs->rootmnt);
872 user_nd.dentry = dget(current->fs->root);
873 read_unlock(¤t->fs->lock);
875 down(&old_nd.dentry->d_inode->i_zombie);
877 if (!check_mnt(user_nd.mnt))
880 if (IS_DEADDIR(new_nd.dentry->d_inode))
882 if (d_unhashed(new_nd.dentry) && !IS_ROOT(new_nd.dentry))
884 if (d_unhashed(old_nd.dentry) && !IS_ROOT(old_nd.dentry))
887 if (new_nd.mnt == user_nd.mnt || old_nd.mnt == user_nd.mnt)
888 goto out2; /* loop */
890 if (user_nd.mnt->mnt_root != user_nd.dentry)
892 if (new_nd.mnt->mnt_root != new_nd.dentry)
893 goto out2; /* not a mountpoint */
894 tmp = old_nd.mnt; /* make sure we can reach put_old from new_root */
895 spin_lock(&dcache_lock);
896 if (tmp != new_nd.mnt) {
898 if (tmp->mnt_parent == tmp)
900 if (tmp->mnt_parent == new_nd.mnt)
902 tmp = tmp->mnt_parent;
904 if (!is_subdir(tmp->mnt_mountpoint, new_nd.dentry))
906 } else if (!is_subdir(old_nd.dentry, new_nd.dentry))
908 detach_mnt(new_nd.mnt, &parent_nd);
909 detach_mnt(user_nd.mnt, &root_parent);
910 attach_mnt(user_nd.mnt, &old_nd);
911 attach_mnt(new_nd.mnt, &root_parent);
912 spin_unlock(&dcache_lock);
913 chroot_fs_refs(&user_nd, &new_nd);
915 path_release(&root_parent);
916 path_release(&parent_nd);
918 up(&old_nd.dentry->d_inode->i_zombie);
920 path_release(&user_nd);
921 path_release(&old_nd);
923 path_release(&new_nd);
928 spin_unlock(&dcache_lock);
933 * Absolutely minimal fake fs - only empty root directory and nothing else.
934 * In 2.5 we'll use ramfs or tmpfs, but for now it's all we need - just
935 * something to go with root vfsmount.
937 static struct dentry *rootfs_lookup(struct inode *dir, struct dentry *dentry)
942 static struct file_operations rootfs_dir_operations = {
943 read: generic_read_dir,
944 readdir: dcache_readdir,
946 static struct inode_operations rootfs_dir_inode_operations = {
947 lookup: rootfs_lookup,
949 static struct super_block *rootfs_read_super(struct super_block * sb, void * data, int silent)
951 struct inode * inode;
952 struct dentry * root;
953 static struct super_operations s_ops = {};
955 inode = new_inode(sb);
958 inode->i_mode = S_IFDIR|0555;
959 inode->i_uid = inode->i_gid = 0;
960 inode->i_op = &rootfs_dir_inode_operations;
961 inode->i_fop = &rootfs_dir_operations;
962 root = d_alloc_root(inode);
970 static DECLARE_FSTYPE(root_fs_type, "rootfs", rootfs_read_super, FS_NOMOUNT);
972 static void __init init_mount_tree(void)
974 register_filesystem(&root_fs_type);
975 root_vfsmnt = do_kern_mount("rootfs", 0, "rootfs", NULL);
976 if (IS_ERR(root_vfsmnt))
977 panic("can't allocate root vfsmount");
980 void __init mnt_init(unsigned long mempages)
984 unsigned int nr_hash;
987 mnt_cache = kmem_cache_create("mnt_cache", sizeof(struct vfsmount),
988 0, SLAB_HWCACHE_ALIGN, NULL, NULL);
990 panic("Cannot create vfsmount cache");
992 mempages >>= (16 - PAGE_SHIFT);
993 mempages *= sizeof(struct list_head);
994 for (order = 0; ((1UL << order) << PAGE_SHIFT) < mempages; order++)
998 mount_hashtable = (struct list_head *)
999 __get_free_pages(GFP_ATOMIC, order);
1000 } while (mount_hashtable == NULL && --order >= 0);
1002 if (!mount_hashtable)
1003 panic("Failed to allocate mount hash table\n");
1006 * Find the power-of-two list-heads that can fit into the allocation..
1007 * We don't guarantee that "sizeof(struct list_head)" is necessarily
1010 nr_hash = (1UL << order) * PAGE_SIZE / sizeof(struct list_head);
1014 } while ((nr_hash >> hash_bits) != 0);
1018 * Re-calculate the actual number of entries and the mask
1019 * from the number of bits we can fit.
1021 nr_hash = 1UL << hash_bits;
1022 hash_mask = nr_hash-1;
1024 printk("Mount-cache hash table entries: %d (order: %ld, %ld bytes)\n",
1025 nr_hash, order, (PAGE_SIZE << order));
1027 /* And initialize the newly allocated array */
1028 d = mount_hashtable;
1038 #ifdef CONFIG_BLK_DEV_INITRD
1040 int __init change_root(kdev_t new_root_dev,const char *put_old)
1042 struct vfsmount *old_rootmnt;
1043 struct nameidata devfs_nd, nd;
1044 struct nameidata parent_nd;
1045 char *new_devname = kmalloc(strlen("/dev/root.old")+1, GFP_KERNEL);
1049 strcpy(new_devname, "/dev/root.old");
1051 read_lock(¤t->fs->lock);
1052 old_rootmnt = mntget(current->fs->rootmnt);
1053 read_unlock(¤t->fs->lock);
1054 /* First unmount devfs if mounted */
1055 if (path_init("/dev", LOOKUP_FOLLOW|LOOKUP_POSITIVE, &devfs_nd))
1056 error = path_walk("/dev", &devfs_nd);
1058 if (devfs_nd.mnt->mnt_sb->s_magic == DEVFS_SUPER_MAGIC &&
1059 devfs_nd.dentry == devfs_nd.mnt->mnt_root) {
1060 do_umount(devfs_nd.mnt, 0);
1062 path_release(&devfs_nd);
1064 spin_lock(&dcache_lock);
1065 detach_mnt(old_rootmnt, &parent_nd);
1066 spin_unlock(&dcache_lock);
1067 ROOT_DEV = new_root_dev;
1071 printk("change_root: old root has d_count=%d\n",
1072 atomic_read(&old_rootmnt->mnt_root->d_count));
1076 * Get the new mount directory
1079 if (path_init(put_old, LOOKUP_FOLLOW|LOOKUP_POSITIVE|LOOKUP_DIRECTORY, &nd))
1080 error = path_walk(put_old, &nd);
1083 struct block_device *ramdisk = old_rootmnt->mnt_sb->s_bdev;
1085 atomic_inc(&ramdisk->bd_count);
1086 blivet = blkdev_get(ramdisk, FMODE_READ, 0, BDEV_FS);
1087 printk(KERN_NOTICE "Trying to unmount old root ... ");
1089 spin_lock(&dcache_lock);
1090 list_del(&old_rootmnt->mnt_list);
1091 spin_unlock(&dcache_lock);
1092 mntput(old_rootmnt);
1093 mntput(old_rootmnt);
1094 blivet = ioctl_by_bdev(ramdisk, BLKFLSBUF, 0);
1095 path_release(&parent_nd);
1096 blkdev_put(ramdisk, BDEV_FS);
1099 printk(KERN_ERR "error %d\n", blivet);
1108 spin_lock(&dcache_lock);
1109 attach_mnt(old_rootmnt, &nd);
1111 if (old_rootmnt->mnt_devname)
1112 kfree(old_rootmnt->mnt_devname);
1113 old_rootmnt->mnt_devname = new_devname;
1115 spin_unlock(&dcache_lock);
1117 /* put the old stuff */
1118 path_release(&parent_nd);
1119 mntput(old_rootmnt);