Merge with Linux 2.4.0-test6-pre9.
[linux-2.6/linux-mips.git] / fs / super.c
blob8576d79d0b3367b4f272e7dc367d46b0e61570f0
1 /*
2 * linux/fs/super.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
6 * super.c contains code to handle: - mount structures
7 * - super-block tables
8 * - filesystem drivers list
9 * - mount system call
10 * - umount system call
11 * - ustat system call
13 * Added options to /proc/mounts
14 * Torbjörn Lindh (torbjorn.lindh@gopta.se), April 14, 1996.
16 * GK 2/5/95 - Changed to support mounting the root fs via NFS
18 * Added kerneld support: Jacques Gelinas and Bjorn Ekwall
19 * Added change_root: Werner Almesberger & Hans Lermen, Feb '96
20 * Added devfs support: Richard Gooch <rgooch@atnf.csiro.au>, 13-JAN-1998
21 * Heavily rewritten for 'one fs - one tree' dcache architecture. AV, Mar 2000
24 #include <linux/config.h>
25 #include <linux/string.h>
26 #include <linux/malloc.h>
27 #include <linux/locks.h>
28 #include <linux/smp_lock.h>
29 #include <linux/devfs_fs_kernel.h>
30 #include <linux/fd.h>
31 #include <linux/init.h>
32 #include <linux/quotaops.h>
33 #include <linux/acct.h>
35 #include <asm/uaccess.h>
37 #include <linux/nfs_fs.h>
38 #include <linux/nfs_fs_sb.h>
39 #include <linux/nfs_mount.h>
41 #include <linux/kmod.h>
42 #define __NO_VERSION__
43 #include <linux/module.h>
46 * We use a semaphore to synchronize all mount/umount
47 * activity - imagine the mess if we have a race between
48 * unmounting a filesystem and re-mounting it (or something
49 * else).
51 static DECLARE_MUTEX(mount_sem);
53 extern void wait_for_keypress(void);
55 extern int root_mountflags;
57 static int do_remount_sb(struct super_block *sb, int flags, char * data);
59 /* this is initialized in init/main.c */
60 kdev_t ROOT_DEV;
62 int nr_super_blocks;
63 int max_super_blocks = NR_SUPER;
64 LIST_HEAD(super_blocks);
67 * Handling of filesystem drivers list.
68 * Rules:
69 * Inclusion to/removals from/scanning of list are protected by spinlock.
70 * During the unload module must call unregister_filesystem().
71 * We can access the fields of list element if:
72 * 1) spinlock is held or
73 * 2) we hold the reference to the module.
74 * The latter can be guaranteed by call of try_inc_mod_count(); if it
75 * returned 0 we must skip the element, otherwise we got the reference.
76 * Once the reference is obtained we can drop the spinlock.
79 static struct file_system_type *file_systems;
80 static rwlock_t file_systems_lock = RW_LOCK_UNLOCKED;
82 /* WARNING: This can be used only if we _already_ own a reference */
83 static void get_filesystem(struct file_system_type *fs)
85 if (fs->owner)
86 __MOD_INC_USE_COUNT(fs->owner);
89 static void put_filesystem(struct file_system_type *fs)
91 if (fs->owner)
92 __MOD_DEC_USE_COUNT(fs->owner);
95 static struct file_system_type **find_filesystem(const char *name)
97 struct file_system_type **p;
98 for (p=&file_systems; *p; p=&(*p)->next)
99 if (strcmp((*p)->name,name) == 0)
100 break;
101 return p;
105 * register_filesystem - register a new filesystem
106 * @fs: the file system structure
108 * Adds the file system passed to the list of file systems the kernel
109 * is aware of for mount and other syscalls. Returns 0 on success,
110 * or a negative errno code on an error.
112 * The &struct file_system_type that is passed is linked into the kernel
113 * structures and must not be freed until the file system has been
114 * unregistered.
117 int register_filesystem(struct file_system_type * fs)
119 int res = 0;
120 struct file_system_type ** p;
122 if (!fs)
123 return -EINVAL;
124 if (fs->next)
125 return -EBUSY;
126 write_lock(&file_systems_lock);
127 p = find_filesystem(fs->name);
128 if (*p)
129 res = -EBUSY;
130 else
131 *p = fs;
132 write_unlock(&file_systems_lock);
133 return res;
137 * unregister_filesystem - unregister a file system
138 * @fs: filesystem to unregister
140 * Remove a file system that was previously successfully registered
141 * with the kernel. An error is returned if the file system is not found.
142 * Zero is returned on a success.
144 * Once this function has returned the &struct file_system_type structure
145 * may be freed or reused.
148 int unregister_filesystem(struct file_system_type * fs)
150 struct file_system_type ** tmp;
152 write_lock(&file_systems_lock);
153 tmp = &file_systems;
154 while (*tmp) {
155 if (fs == *tmp) {
156 *tmp = fs->next;
157 fs->next = NULL;
158 write_unlock(&file_systems_lock);
159 return 0;
161 tmp = &(*tmp)->next;
163 write_unlock(&file_systems_lock);
164 return -EINVAL;
167 static int fs_index(const char * __name)
169 struct file_system_type * tmp;
170 char * name;
171 int err, index;
173 name = getname(__name);
174 err = PTR_ERR(name);
175 if (IS_ERR(name))
176 return err;
178 err = -EINVAL;
179 read_lock(&file_systems_lock);
180 for (tmp=file_systems, index=0 ; tmp ; tmp=tmp->next, index++) {
181 if (strcmp(tmp->name,name) == 0) {
182 err = index;
183 break;
186 read_unlock(&file_systems_lock);
187 putname(name);
188 return err;
191 static int fs_name(unsigned int index, char * buf)
193 struct file_system_type * tmp;
194 int len, res;
196 read_lock(&file_systems_lock);
197 for (tmp = file_systems; tmp; tmp = tmp->next, index--)
198 if (index <= 0 && try_inc_mod_count(tmp->owner))
199 break;
200 read_unlock(&file_systems_lock);
201 if (!tmp)
202 return -EINVAL;
204 /* OK, we got the reference, so we can safely block */
205 len = strlen(tmp->name) + 1;
206 res = copy_to_user(buf, tmp->name, len) ? -EFAULT : 0;
207 put_filesystem(tmp);
208 return res;
211 static int fs_maxindex(void)
213 struct file_system_type * tmp;
214 int index;
216 read_lock(&file_systems_lock);
217 for (tmp = file_systems, index = 0 ; tmp ; tmp = tmp->next, index++)
219 read_unlock(&file_systems_lock);
220 return index;
224 * Whee.. Weird sysv syscall.
226 asmlinkage long sys_sysfs(int option, unsigned long arg1, unsigned long arg2)
228 int retval = -EINVAL;
230 switch (option) {
231 case 1:
232 retval = fs_index((const char *) arg1);
233 break;
235 case 2:
236 retval = fs_name(arg1, (char *) arg2);
237 break;
239 case 3:
240 retval = fs_maxindex();
241 break;
243 return retval;
246 int get_filesystem_list(char * buf)
248 int len = 0;
249 struct file_system_type * tmp;
251 read_lock(&file_systems_lock);
252 tmp = file_systems;
253 while (tmp && len < PAGE_SIZE - 80) {
254 len += sprintf(buf+len, "%s\t%s\n",
255 (tmp->fs_flags & FS_REQUIRES_DEV) ? "" : "nodev",
256 tmp->name);
257 tmp = tmp->next;
259 read_unlock(&file_systems_lock);
260 return len;
263 struct file_system_type *get_fs_type(const char *name)
265 struct file_system_type *fs;
267 read_lock(&file_systems_lock);
268 fs = *(find_filesystem(name));
269 if (fs && !try_inc_mod_count(fs->owner))
270 fs = NULL;
271 read_unlock(&file_systems_lock);
272 if (!fs && (request_module(name) == 0)) {
273 read_lock(&file_systems_lock);
274 fs = *(find_filesystem(name));
275 if (fs && !try_inc_mod_count(fs->owner))
276 fs = NULL;
277 read_unlock(&file_systems_lock);
279 return fs;
282 static LIST_HEAD(vfsmntlist);
285 * add_vfsmnt - add a new mount node
286 * @nd: location of mountpoint or %NULL if we want a root node
287 * @root: root of (sub)tree to be mounted
288 * @dev_name: device name to show in /proc/mounts or %NULL (for "none").
290 * This is VFS idea of mount. New node is allocated, bound to a tree
291 * we are mounting and optionally (OK, usually) registered as mounted
292 * on a given mountpoint. Returns a pointer to new node or %NULL in
293 * case of failure.
295 * Potential reason for failure (aside of trivial lack of memory) is a
296 * deleted mountpoint. Caller must hold ->i_zombie on mountpoint
297 * dentry (if any).
299 * Node is marked as MNT_VISIBLE (visible in /proc/mounts) unless both
300 * @nd and @devname are %NULL. It works since we pass non-%NULL @devname
301 * when we are mounting root and kern_mount() filesystems are deviceless.
302 * If we will get a kern_mount() filesystem with nontrivial @devname we
303 * will have to pass the visibility flag explicitly, so if we will add
304 * support for such beasts we'll have to change prototype.
307 static struct vfsmount *add_vfsmnt(struct nameidata *nd,
308 struct dentry *root,
309 const char *dev_name)
311 struct vfsmount *mnt;
312 struct super_block *sb = root->d_inode->i_sb;
313 char *name;
315 mnt = kmalloc(sizeof(struct vfsmount), GFP_KERNEL);
316 if (!mnt)
317 goto out;
318 memset(mnt, 0, sizeof(struct vfsmount));
320 if (nd || dev_name)
321 mnt->mnt_flags = MNT_VISIBLE;
323 /* It may be NULL, but who cares? */
324 if (dev_name) {
325 name = kmalloc(strlen(dev_name)+1, GFP_KERNEL);
326 if (name) {
327 strcpy(name, dev_name);
328 mnt->mnt_devname = name;
331 mnt->mnt_owner = current->uid;
332 atomic_set(&mnt->mnt_count,1);
333 mnt->mnt_sb = sb;
335 spin_lock(&dcache_lock);
336 if (nd && !IS_ROOT(nd->dentry) && d_unhashed(nd->dentry))
337 goto fail;
338 mnt->mnt_root = dget(root);
339 mnt->mnt_mountpoint = nd ? dget(nd->dentry) : dget(root);
340 mnt->mnt_parent = nd ? mntget(nd->mnt) : mnt;
342 if (nd) {
343 list_add(&mnt->mnt_child, &nd->mnt->mnt_mounts);
344 list_add(&mnt->mnt_clash, &nd->dentry->d_vfsmnt);
345 } else {
346 INIT_LIST_HEAD(&mnt->mnt_child);
347 INIT_LIST_HEAD(&mnt->mnt_clash);
349 INIT_LIST_HEAD(&mnt->mnt_mounts);
350 list_add(&mnt->mnt_instances, &sb->s_mounts);
351 list_add(&mnt->mnt_list, vfsmntlist.prev);
352 spin_unlock(&dcache_lock);
353 out:
354 return mnt;
355 fail:
356 spin_unlock(&dcache_lock);
357 if (mnt->mnt_devname)
358 kfree(mnt->mnt_devname);
359 kfree(mnt);
360 return NULL;
363 static void move_vfsmnt(struct vfsmount *mnt,
364 struct dentry *mountpoint,
365 struct vfsmount *parent,
366 const char *dev_name)
368 struct dentry *old_mountpoint;
369 struct vfsmount *old_parent;
370 char *new_devname = NULL;
372 if (dev_name) {
373 new_devname = kmalloc(strlen(dev_name)+1, GFP_KERNEL);
374 if (new_devname)
375 strcpy(new_devname, dev_name);
378 spin_lock(&dcache_lock);
379 old_mountpoint = mnt->mnt_mountpoint;
380 old_parent = mnt->mnt_parent;
382 /* flip names */
383 if (new_devname) {
384 if (mnt->mnt_devname)
385 kfree(mnt->mnt_devname);
386 mnt->mnt_devname = new_devname;
389 /* flip the linkage */
390 mnt->mnt_mountpoint = dget(mountpoint);
391 mnt->mnt_parent = parent ? mntget(parent) : mnt;
392 list_del(&mnt->mnt_clash);
393 list_del(&mnt->mnt_child);
394 if (parent) {
395 list_add(&mnt->mnt_child, &parent->mnt_mounts);
396 list_add(&mnt->mnt_clash, &mountpoint->d_vfsmnt);
397 } else {
398 INIT_LIST_HEAD(&mnt->mnt_child);
399 INIT_LIST_HEAD(&mnt->mnt_clash);
401 spin_unlock(&dcache_lock);
403 /* put the old stuff */
404 dput(old_mountpoint);
405 if (old_parent != mnt)
406 mntput(old_parent);
410 * Called with spinlock held, releases it.
412 static void remove_vfsmnt(struct vfsmount *mnt)
414 /* First of all, remove it from all lists */
415 list_del(&mnt->mnt_instances);
416 list_del(&mnt->mnt_clash);
417 list_del(&mnt->mnt_list);
418 list_del(&mnt->mnt_child);
419 spin_unlock(&dcache_lock);
420 /* Now we can work safely */
421 if (mnt->mnt_parent != mnt)
422 mntput(mnt->mnt_parent);
424 dput(mnt->mnt_mountpoint);
425 dput(mnt->mnt_root);
426 if (mnt->mnt_devname)
427 kfree(mnt->mnt_devname);
428 kfree(mnt);
431 static struct proc_fs_info {
432 int flag;
433 char *str;
434 } fs_info[] = {
435 { MS_NOEXEC, ",noexec" },
436 { MS_NOSUID, ",nosuid" },
437 { MS_NODEV, ",nodev" },
438 { MS_SYNCHRONOUS, ",sync" },
439 { MS_MANDLOCK, ",mand" },
440 { MS_NOATIME, ",noatime" },
441 { MS_NODIRATIME, ",nodiratime" },
442 #ifdef MS_NOSUB /* Can't find this except in mount.c */
443 { MS_NOSUB, ",nosub" },
444 #endif
445 { 0, NULL }
448 static struct proc_nfs_info {
449 int flag;
450 char *str;
451 char *nostr;
452 } nfs_info[] = {
453 { NFS_MOUNT_SOFT, ",soft", ",hard" },
454 { NFS_MOUNT_INTR, ",intr", "" },
455 { NFS_MOUNT_POSIX, ",posix", "" },
456 { NFS_MOUNT_TCP, ",tcp", ",udp" },
457 { NFS_MOUNT_NOCTO, ",nocto", "" },
458 { NFS_MOUNT_NOAC, ",noac", "" },
459 { NFS_MOUNT_NONLM, ",nolock", ",lock" },
460 { 0, NULL, NULL }
463 int get_filesystem_info( char *buf )
465 struct list_head *p;
466 struct proc_fs_info *fs_infop;
467 struct proc_nfs_info *nfs_infop;
468 struct nfs_server *nfss;
469 int len = 0;
470 char *path,*buffer = (char *) __get_free_page(GFP_KERNEL);
472 if (!buffer) return 0;
473 for (p = vfsmntlist.next; p!=&vfsmntlist && len < PAGE_SIZE - 160;
474 p = p->next) {
475 struct vfsmount *tmp = list_entry(p, struct vfsmount, mnt_list);
476 if (!(tmp->mnt_flags & MNT_VISIBLE))
477 continue;
478 path = d_path(tmp->mnt_root, tmp, buffer, PAGE_SIZE);
479 if (!path)
480 continue;
481 len += sprintf( buf + len, "%s %s %s %s",
482 tmp->mnt_devname ? tmp->mnt_devname : "none", path,
483 tmp->mnt_sb->s_type->name,
484 tmp->mnt_sb->s_flags & MS_RDONLY ? "ro" : "rw" );
485 for (fs_infop = fs_info; fs_infop->flag; fs_infop++) {
486 if (tmp->mnt_sb->s_flags & fs_infop->flag) {
487 strcpy(buf + len, fs_infop->str);
488 len += strlen(fs_infop->str);
491 if (!strcmp("nfs", tmp->mnt_sb->s_type->name)) {
492 nfss = &tmp->mnt_sb->u.nfs_sb.s_server;
493 len += sprintf(buf+len, ",v%d", nfss->rpc_ops->version);
495 len += sprintf(buf+len, ",rsize=%d", nfss->rsize);
497 len += sprintf(buf+len, ",wsize=%d", nfss->wsize);
498 #if 0
499 if (nfss->timeo != 7*HZ/10) {
500 len += sprintf(buf+len, ",timeo=%d",
501 nfss->timeo*10/HZ);
503 if (nfss->retrans != 3) {
504 len += sprintf(buf+len, ",retrans=%d",
505 nfss->retrans);
507 #endif
508 if (nfss->acregmin != 3*HZ) {
509 len += sprintf(buf+len, ",acregmin=%d",
510 nfss->acregmin/HZ);
512 if (nfss->acregmax != 60*HZ) {
513 len += sprintf(buf+len, ",acregmax=%d",
514 nfss->acregmax/HZ);
516 if (nfss->acdirmin != 30*HZ) {
517 len += sprintf(buf+len, ",acdirmin=%d",
518 nfss->acdirmin/HZ);
520 if (nfss->acdirmax != 60*HZ) {
521 len += sprintf(buf+len, ",acdirmax=%d",
522 nfss->acdirmax/HZ);
524 for (nfs_infop = nfs_info; nfs_infop->flag; nfs_infop++) {
525 char *str;
526 if (nfss->flags & nfs_infop->flag)
527 str = nfs_infop->str;
528 else
529 str = nfs_infop->nostr;
530 strcpy(buf + len, str);
531 len += strlen(str);
533 len += sprintf(buf+len, ",addr=%s",
534 nfss->hostname);
536 len += sprintf( buf + len, " 0 0\n" );
539 free_page((unsigned long) buffer);
540 return len;
544 * __wait_on_super - wait on a superblock
545 * @sb: superblock to wait on
547 * Waits for a superblock to become unlocked and then returns. It does
548 * not take the lock. This is an internal function. See wait_on_super().
551 void __wait_on_super(struct super_block * sb)
553 DECLARE_WAITQUEUE(wait, current);
555 add_wait_queue(&sb->s_wait, &wait);
556 repeat:
557 set_current_state(TASK_UNINTERRUPTIBLE);
558 if (sb->s_lock) {
559 schedule();
560 goto repeat;
562 remove_wait_queue(&sb->s_wait, &wait);
563 current->state = TASK_RUNNING;
567 * Note: check the dirty flag before waiting, so we don't
568 * hold up the sync while mounting a device. (The newly
569 * mounted device won't need syncing.)
571 void sync_supers(kdev_t dev)
573 struct super_block * sb;
575 for (sb = sb_entry(super_blocks.next);
576 sb != sb_entry(&super_blocks);
577 sb = sb_entry(sb->s_list.next)) {
578 if (!sb->s_dev)
579 continue;
580 if (dev && sb->s_dev != dev)
581 continue;
582 if (!sb->s_dirt)
583 continue;
584 lock_super(sb);
585 if (sb->s_dev && sb->s_dirt && (!dev || dev == sb->s_dev))
586 if (sb->s_op && sb->s_op->write_super)
587 sb->s_op->write_super(sb);
588 unlock_super(sb);
593 * get_super - get the superblock of a device
594 * @dev: device to get the superblock for
596 * Scans the superblock list and finds the superblock of the file system
597 * mounted on the device given. %NULL is returned if no match is found.
600 struct super_block * get_super(kdev_t dev)
602 struct super_block * s;
604 if (!dev)
605 return NULL;
606 restart:
607 s = sb_entry(super_blocks.next);
608 while (s != sb_entry(&super_blocks))
609 if (s->s_dev == dev) {
610 wait_on_super(s);
611 if (s->s_dev == dev)
612 return s;
613 goto restart;
614 } else
615 s = sb_entry(s->s_list.next);
616 return NULL;
619 asmlinkage long sys_ustat(dev_t dev, struct ustat * ubuf)
621 struct super_block *s;
622 struct ustat tmp;
623 struct statfs sbuf;
624 int err = -EINVAL;
626 lock_kernel();
627 s = get_super(to_kdev_t(dev));
628 unlock_kernel();
629 if (s == NULL)
630 goto out;
631 err = vfs_statfs(s, &sbuf);
632 if (err)
633 goto out;
635 memset(&tmp,0,sizeof(struct ustat));
636 tmp.f_tfree = sbuf.f_bfree;
637 tmp.f_tinode = sbuf.f_ffree;
639 err = copy_to_user(ubuf,&tmp,sizeof(struct ustat)) ? -EFAULT : 0;
640 out:
641 return err;
645 * get_empty_super - find empty superblocks
647 * Find a superblock with no device assigned. A free superblock is
648 * found and returned. If neccessary new superblocks are allocated.
649 * %NULL is returned if there are insufficient resources to complete
650 * the request.
653 struct super_block *get_empty_super(void)
655 struct super_block *s;
657 for (s = sb_entry(super_blocks.next);
658 s != sb_entry(&super_blocks);
659 s = sb_entry(s->s_list.next)) {
660 if (s->s_dev)
661 continue;
662 if (!s->s_lock)
663 return s;
664 printk("VFS: empty superblock %p locked!\n", s);
666 /* Need a new one... */
667 if (nr_super_blocks >= max_super_blocks)
668 return NULL;
669 s = kmalloc(sizeof(struct super_block), GFP_USER);
670 if (s) {
671 nr_super_blocks++;
672 memset(s, 0, sizeof(struct super_block));
673 INIT_LIST_HEAD(&s->s_dirty);
674 list_add (&s->s_list, super_blocks.prev);
675 init_waitqueue_head(&s->s_wait);
676 INIT_LIST_HEAD(&s->s_files);
677 INIT_LIST_HEAD(&s->s_mounts);
679 return s;
682 static struct super_block * read_super(kdev_t dev, struct block_device *bdev,
683 struct file_system_type *type, int flags,
684 void *data, int silent)
686 struct super_block * s;
687 s = get_empty_super();
688 if (!s)
689 goto out;
690 s->s_dev = dev;
691 s->s_bdev = bdev;
692 s->s_flags = flags;
693 s->s_dirt = 0;
694 sema_init(&s->s_vfs_rename_sem,1);
695 sema_init(&s->s_nfsd_free_path_sem,1);
696 s->s_type = type;
697 sema_init(&s->s_dquot.dqio_sem, 1);
698 sema_init(&s->s_dquot.dqoff_sem, 1);
699 s->s_dquot.flags = 0;
700 lock_super(s);
701 if (!type->read_super(s, data, silent))
702 goto out_fail;
703 unlock_super(s);
704 /* tell bdcache that we are going to keep this one */
705 if (bdev)
706 atomic_inc(&bdev->bd_count);
707 out:
708 return s;
710 out_fail:
711 s->s_dev = 0;
712 s->s_bdev = 0;
713 s->s_type = NULL;
714 unlock_super(s);
715 return NULL;
719 * Unnamed block devices are dummy devices used by virtual
720 * filesystems which don't use real block-devices. -- jrs
723 static unsigned int unnamed_dev_in_use[256/(8*sizeof(unsigned int))];
725 kdev_t get_unnamed_dev(void)
727 int i;
729 for (i = 1; i < 256; i++) {
730 if (!test_and_set_bit(i,unnamed_dev_in_use))
731 return MKDEV(UNNAMED_MAJOR, i);
733 return 0;
736 void put_unnamed_dev(kdev_t dev)
738 if (!dev || MAJOR(dev) != UNNAMED_MAJOR)
739 return;
740 if (test_and_clear_bit(MINOR(dev), unnamed_dev_in_use))
741 return;
742 printk("VFS: put_unnamed_dev: freeing unused device %s\n",
743 kdevname(dev));
746 static struct super_block *get_sb_bdev(struct file_system_type *fs_type,
747 char *dev_name, int flags, void * data)
749 struct inode *inode;
750 struct block_device *bdev;
751 struct block_device_operations *bdops;
752 struct super_block * sb;
753 struct nameidata nd;
754 kdev_t dev;
755 int error = 0;
756 /* What device it is? */
757 if (!dev_name || !*dev_name)
758 return ERR_PTR(-EINVAL);
759 if (path_init(dev_name, LOOKUP_FOLLOW|LOOKUP_POSITIVE, &nd))
760 error = path_walk(dev_name, &nd);
761 if (error)
762 return ERR_PTR(error);
763 inode = nd.dentry->d_inode;
764 error = -ENOTBLK;
765 if (!S_ISBLK(inode->i_mode))
766 goto out;
767 error = -EACCES;
768 if (IS_NODEV(inode))
769 goto out;
770 bdev = inode->i_bdev;
771 bdops = devfs_get_ops ( devfs_get_handle_from_inode (inode) );
772 if (bdops) bdev->bd_op = bdops;
773 /* Done with lookups, semaphore down */
774 down(&mount_sem);
775 dev = to_kdev_t(bdev->bd_dev);
776 sb = get_super(dev);
777 if (sb) {
778 if (fs_type == sb->s_type) {
779 path_release(&nd);
780 return sb;
782 } else {
783 mode_t mode = FMODE_READ; /* we always need it ;-) */
784 if (!(flags & MS_RDONLY))
785 mode |= FMODE_WRITE;
786 error = blkdev_get(bdev, mode, 0, BDEV_FS);
787 if (error)
788 goto out;
789 check_disk_change(dev);
790 error = -EACCES;
791 if (!(flags & MS_RDONLY) && is_read_only(dev))
792 goto out1;
793 error = -EINVAL;
794 sb = read_super(dev, bdev, fs_type, flags, data, 0);
795 if (sb) {
796 get_filesystem(fs_type);
797 path_release(&nd);
798 return sb;
800 out1:
801 blkdev_put(bdev, BDEV_FS);
803 out:
804 path_release(&nd);
805 up(&mount_sem);
806 return ERR_PTR(error);
809 static struct super_block *get_sb_nodev(struct file_system_type *fs_type,
810 int flags, void * data)
812 kdev_t dev;
813 int error = -EMFILE;
814 down(&mount_sem);
815 dev = get_unnamed_dev();
816 if (dev) {
817 struct super_block * sb;
818 error = -EINVAL;
819 sb = read_super(dev, NULL, fs_type, flags, data, 0);
820 if (sb) {
821 get_filesystem(fs_type);
822 return sb;
824 put_unnamed_dev(dev);
826 up(&mount_sem);
827 return ERR_PTR(error);
830 static struct super_block *get_sb_single(struct file_system_type *fs_type,
831 int flags, void *data)
833 struct super_block * sb;
835 * Get the superblock of kernel-wide instance, but
836 * keep the reference to fs_type.
838 down(&mount_sem);
839 sb = fs_type->kern_mnt->mnt_sb;
840 if (!sb)
841 BUG();
842 get_filesystem(fs_type);
843 do_remount_sb(sb, flags, data);
844 return sb;
847 static struct block_device *kill_super(struct super_block *sb, int umount_root)
849 struct block_device *bdev;
850 kdev_t dev;
851 struct dentry *root = sb->s_root;
852 sb->s_root = NULL;
853 /* Need to clean after the sucker */
854 if (sb->s_type->fs_flags & FS_LITTER)
855 d_genocide(root);
856 if (sb->s_type->fs_flags & (FS_SINGLE|FS_LITTER))
857 shrink_dcache_parent(root);
858 dput(root);
859 lock_super(sb);
860 if (sb->s_op) {
861 if (sb->s_op->write_super && sb->s_dirt)
862 sb->s_op->write_super(sb);
863 if (sb->s_op->put_super)
864 sb->s_op->put_super(sb);
867 /* Forget any remaining inodes */
868 if (invalidate_inodes(sb)) {
869 printk("VFS: Busy inodes after unmount. "
870 "Self-destruct in 5 seconds. Have a nice day...\n");
873 dev = sb->s_dev;
874 sb->s_dev = 0; /* Free the superblock */
875 bdev = sb->s_bdev;
876 sb->s_bdev = NULL;
877 put_filesystem(sb->s_type);
878 sb->s_type = NULL;
879 unlock_super(sb);
880 if (umount_root) {
881 /* special: the old device driver is going to be
882 a ramdisk and the point of this call is to free its
883 protected memory (even if dirty). */
884 destroy_buffers(dev);
886 if (bdev) {
887 blkdev_put(bdev, BDEV_FS);
888 bdput(bdev);
889 } else
890 put_unnamed_dev(dev);
891 return bdev;
895 * Alters the mount flags of a mounted file system. Only the mount point
896 * is used as a reference - file system type and the device are ignored.
899 static int do_remount_sb(struct super_block *sb, int flags, char *data)
901 int retval;
903 if (!(flags & MS_RDONLY) && sb->s_dev && is_read_only(sb->s_dev))
904 return -EACCES;
905 /*flags |= MS_RDONLY;*/
906 /* If we are remounting RDONLY, make sure there are no rw files open */
907 if ((flags & MS_RDONLY) && !(sb->s_flags & MS_RDONLY))
908 if (!fs_may_remount_ro(sb))
909 return -EBUSY;
910 if (sb->s_op && sb->s_op->remount_fs) {
911 lock_super(sb);
912 retval = sb->s_op->remount_fs(sb, &flags, data);
913 unlock_super(sb);
914 if (retval)
915 return retval;
917 sb->s_flags = (sb->s_flags & ~MS_RMT_MASK) | (flags & MS_RMT_MASK);
920 * We can't invalidate inodes as we can loose data when remounting
921 * (someone might manage to alter data while we are waiting in lock_super()
922 * or in foo_remount_fs()))
925 return 0;
928 struct vfsmount *kern_mount(struct file_system_type *type)
930 kdev_t dev = get_unnamed_dev();
931 struct super_block *sb;
932 struct vfsmount *mnt;
933 if (!dev)
934 return ERR_PTR(-EMFILE);
935 sb = read_super(dev, NULL, type, 0, NULL, 0);
936 if (!sb) {
937 put_unnamed_dev(dev);
938 return ERR_PTR(-EINVAL);
940 mnt = add_vfsmnt(NULL, sb->s_root, NULL);
941 if (!mnt) {
942 kill_super(sb, 0);
943 return ERR_PTR(-ENOMEM);
945 type->kern_mnt = mnt;
946 return mnt;
949 /* Call only after unregister_filesystem() - it's a final cleanup */
951 void kern_umount(struct vfsmount *mnt)
953 struct super_block *sb = mnt->mnt_sb;
954 spin_lock(&dcache_lock);
955 remove_vfsmnt(mnt);
956 kill_super(sb, 0);
960 * Doesn't take quota and stuff into account. IOW, in some cases it will
961 * give false negatives. The main reason why it's here is that we need
962 * a non-destructive way to look for easily umountable filesystems.
964 int may_umount(struct vfsmount *mnt)
966 if (atomic_read(&mnt->mnt_count) > 2)
967 return -EBUSY;
968 return 0;
971 static int do_umount(struct vfsmount *mnt, int umount_root, int flags)
973 struct super_block * sb = mnt->mnt_sb;
976 * No sense to grab the lock for this test, but test itself looks
977 * somewhat bogus. Suggestions for better replacement?
978 * Ho-hum... In principle, we might treat that as umount + switch
979 * to rootfs. GC would eventually take care of the old vfsmount.
980 * The problem being: we have to implement rootfs and GC for that ;-)
981 * Actually it makes sense, especially if rootfs would contain a
982 * /reboot - static binary that would close all descriptors and
983 * call reboot(9). Then init(8) could umount root and exec /reboot.
985 if (mnt == current->fs->rootmnt && !umount_root) {
986 int retval = 0;
988 * Special case for "unmounting" root ...
989 * we just try to remount it readonly.
991 mntput(mnt);
992 if (!(sb->s_flags & MS_RDONLY))
993 retval = do_remount_sb(sb, MS_RDONLY, 0);
994 return retval;
997 spin_lock(&dcache_lock);
998 if (atomic_read(&mnt->mnt_count) > 2) {
999 spin_unlock(&dcache_lock);
1000 mntput(mnt);
1001 return -EBUSY;
1004 if (mnt->mnt_instances.next != mnt->mnt_instances.prev) {
1005 if (sb->s_type->fs_flags & FS_SINGLE)
1006 put_filesystem(sb->s_type);
1007 /* We hold two references, so mntput() is safe */
1008 mntput(mnt);
1009 remove_vfsmnt(mnt);
1010 return 0;
1012 spin_unlock(&dcache_lock);
1015 * Before checking whether the filesystem is still busy,
1016 * make sure the kernel doesn't hold any quota files open
1017 * on the device. If the umount fails, too bad -- there
1018 * are no quotas running any more. Just turn them on again.
1020 DQUOT_OFF(sb);
1021 acct_auto_close(sb->s_dev);
1024 * If we may have to abort operations to get out of this
1025 * mount, and they will themselves hold resources we must
1026 * allow the fs to do things. In the Unix tradition of
1027 * 'Gee thats tricky lets do it in userspace' the umount_begin
1028 * might fail to complete on the first run through as other tasks
1029 * must return, and the like. Thats for the mount program to worry
1030 * about for the moment.
1033 if( (flags&MNT_FORCE) && sb->s_op->umount_begin)
1034 sb->s_op->umount_begin(sb);
1037 * Shrink dcache, then fsync. This guarantees that if the
1038 * filesystem is quiescent at this point, then (a) only the
1039 * root entry should be in use and (b) that root entry is
1040 * clean.
1042 shrink_dcache_sb(sb);
1043 fsync_dev(sb->s_dev);
1045 if (sb->s_root->d_inode->i_state) {
1046 mntput(mnt);
1047 return -EBUSY;
1050 /* Something might grab it again - redo checks */
1052 spin_lock(&dcache_lock);
1053 if (atomic_read(&mnt->mnt_count) > 2) {
1054 spin_unlock(&dcache_lock);
1055 mntput(mnt);
1056 return -EBUSY;
1059 /* OK, that's the point of no return */
1060 mntput(mnt);
1061 remove_vfsmnt(mnt);
1063 kill_super(sb, umount_root);
1064 return 0;
1068 * Now umount can handle mount points as well as block devices.
1069 * This is important for filesystems which use unnamed block devices.
1071 * We now support a flag for forced unmount like the other 'big iron'
1072 * unixes. Our API is identical to OSF/1 to avoid making a mess of AMD
1075 asmlinkage long sys_umount(char * name, int flags)
1077 struct nameidata nd;
1078 char *kname;
1079 int retval;
1081 lock_kernel();
1082 kname = getname(name);
1083 retval = PTR_ERR(kname);
1084 if (IS_ERR(kname))
1085 goto out;
1086 retval = 0;
1087 if (path_init(kname, LOOKUP_POSITIVE|LOOKUP_FOLLOW, &nd))
1088 retval = path_walk(kname, &nd);
1089 putname(kname);
1090 if (retval)
1091 goto out;
1092 retval = -EINVAL;
1093 if (nd.dentry!=nd.mnt->mnt_root)
1094 goto dput_and_out;
1096 retval = -EPERM;
1097 if (!capable(CAP_SYS_ADMIN) && current->uid!=nd.mnt->mnt_owner)
1098 goto dput_and_out;
1100 dput(nd.dentry);
1101 /* puts nd.mnt */
1102 down(&mount_sem);
1103 retval = do_umount(nd.mnt, 0, flags);
1104 up(&mount_sem);
1105 goto out;
1106 dput_and_out:
1107 path_release(&nd);
1108 out:
1109 unlock_kernel();
1110 return retval;
1114 * The 2.0 compatible umount. No flags.
1117 asmlinkage long sys_oldumount(char * name)
1119 return sys_umount(name,0);
1122 static int mount_is_safe(struct nameidata *nd)
1124 if (capable(CAP_SYS_ADMIN))
1125 return 0;
1126 return -EPERM;
1127 #ifdef notyet
1128 if (S_ISLNK(nd->dentry->d_inode->i_mode))
1129 return -EPERM;
1130 if (nd->dentry->d_inode->i_mode & S_ISVTX) {
1131 if (current->uid != nd->dentry->d_inode->i_uid)
1132 return -EPERM;
1134 if (permission(nd->dentry->d_inode, MAY_WRITE))
1135 return -EPERM;
1136 return 0;
1137 #endif
1141 * do loopback mount.
1143 static int do_loopback(char *old_name, char *new_name)
1145 struct nameidata old_nd, new_nd;
1146 int err = 0;
1147 if (!old_name || !*old_name)
1148 return -EINVAL;
1149 if (path_init(old_name, LOOKUP_POSITIVE, &old_nd))
1150 err = path_walk(old_name, &old_nd);
1151 if (err)
1152 goto out;
1153 if (path_init(new_name, LOOKUP_POSITIVE, &new_nd))
1154 err = path_walk(new_name, &new_nd);
1155 if (err)
1156 goto out1;
1157 err = mount_is_safe(&new_nd);
1158 if (err)
1159 goto out2;
1160 err = -EINVAL;
1161 if (S_ISDIR(new_nd.dentry->d_inode->i_mode) !=
1162 S_ISDIR(old_nd.dentry->d_inode->i_mode))
1163 goto out2;
1165 err = -ENOMEM;
1166 if (old_nd.mnt->mnt_sb->s_type->fs_flags & FS_SINGLE)
1167 get_filesystem(old_nd.mnt->mnt_sb->s_type);
1169 down(&mount_sem);
1170 /* there we go */
1171 down(&new_nd.dentry->d_inode->i_zombie);
1172 if (IS_DEADDIR(new_nd.dentry->d_inode))
1173 err = -ENOENT;
1174 else if (add_vfsmnt(&new_nd, old_nd.dentry, old_nd.mnt->mnt_devname))
1175 err = 0;
1176 up(&new_nd.dentry->d_inode->i_zombie);
1177 up(&mount_sem);
1178 if (err && old_nd.mnt->mnt_sb->s_type->fs_flags & FS_SINGLE)
1179 put_filesystem(old_nd.mnt->mnt_sb->s_type);
1180 out2:
1181 path_release(&new_nd);
1182 out1:
1183 path_release(&old_nd);
1184 out:
1185 return err;
1189 * change filesystem flags. dir should be a physical root of filesystem.
1190 * If you've mounted a non-root directory somewhere and want to do remount
1191 * on it - tough luck.
1194 static int do_remount(const char *dir,int flags,char *data)
1196 struct nameidata nd;
1197 int retval = 0;
1199 if (!capable(CAP_SYS_ADMIN))
1200 return -EPERM;
1202 if (path_init(dir, LOOKUP_FOLLOW|LOOKUP_POSITIVE, &nd))
1203 retval = path_walk(dir, &nd);
1204 if (!retval) {
1205 struct super_block * sb = nd.dentry->d_inode->i_sb;
1206 retval = -ENODEV;
1207 if (sb) {
1208 retval = -EINVAL;
1209 if (nd.dentry == sb->s_root) {
1211 * Shrink the dcache and sync the device.
1213 shrink_dcache_sb(sb);
1214 fsync_dev(sb->s_dev);
1215 if (flags & MS_RDONLY)
1216 acct_auto_close(sb->s_dev);
1217 retval = do_remount_sb(sb, flags, data);
1220 path_release(&nd);
1222 return retval;
1225 static int copy_mount_options (const void *data, unsigned long *where)
1227 int i;
1228 unsigned long page;
1229 unsigned long size;
1231 *where = 0;
1232 if (!data)
1233 return 0;
1235 if (!(page = __get_free_page(GFP_KERNEL)))
1236 return -ENOMEM;
1238 /* We only care that *some* data at the address the user
1239 * gave us is valid. Just in case, we'll zero
1240 * the remainder of the page.
1242 /* copy_from_user cannot cross TASK_SIZE ! */
1243 size = TASK_SIZE - (unsigned long)data;
1244 if (size > PAGE_SIZE)
1245 size = PAGE_SIZE;
1247 i = size - copy_from_user((void *)page, data, size);
1248 if (!i) {
1249 free_page(page);
1250 return -EFAULT;
1252 if (i != PAGE_SIZE)
1253 memset((char *)page + i, 0, PAGE_SIZE - i);
1254 *where = page;
1255 return 0;
1259 * Flags is a 16-bit value that allows up to 16 non-fs dependent flags to
1260 * be given to the mount() call (ie: read-only, no-dev, no-suid etc).
1262 * data is a (void *) that can point to any structure up to
1263 * PAGE_SIZE-1 bytes, which can contain arbitrary fs-dependent
1264 * information (or be NULL).
1266 * NOTE! As old versions of mount() didn't use this setup, the flags
1267 * have to have a special 16-bit magic number in the high word:
1268 * 0xC0ED. If this magic word isn't present, the flags and data info
1269 * aren't used, as the syscall assumes we are talking to an older
1270 * version that didn't understand them.
1272 long do_mount(char * dev_name, char * dir_name, char *type_page,
1273 unsigned long new_flags, void *data_page)
1275 struct file_system_type * fstype;
1276 struct nameidata nd;
1277 struct vfsmount *mnt = NULL;
1278 struct super_block *sb;
1279 int retval = 0;
1280 unsigned long flags = 0;
1282 /* Basic sanity checks */
1284 if (!dir_name || !*dir_name || !memchr(dir_name, 0, PAGE_SIZE))
1285 return -EINVAL;
1286 if (dev_name && !memchr(dev_name, 0, PAGE_SIZE))
1287 return -EINVAL;
1289 /* OK, looks good, now let's see what do they want */
1291 /* just change the flags? - capabilities are checked in do_remount() */
1292 if ((new_flags & (MS_MGC_MSK|MS_REMOUNT)) == (MS_MGC_VAL|MS_REMOUNT))
1293 return do_remount(dir_name, new_flags&~(MS_MGC_MSK|MS_REMOUNT),
1294 (char *) data_page);
1296 if ((new_flags & MS_MGC_MSK) == MS_MGC_VAL)
1297 flags = new_flags & ~MS_MGC_MSK;
1299 /* For the rest we need the type */
1301 if (!type_page || !memchr(type_page, 0, PAGE_SIZE))
1302 return -EINVAL;
1304 /* loopback mount? This is special - requires fewer capabilities */
1305 if (strcmp(type_page, "bind")==0)
1306 return do_loopback(dev_name, dir_name);
1308 /* for the rest we _really_ need capabilities... */
1309 if (!capable(CAP_SYS_ADMIN))
1310 return -EPERM;
1312 /* ... filesystem driver... */
1313 fstype = get_fs_type(type_page);
1314 if (!fstype)
1315 return -ENODEV;
1317 /* ... and mountpoint. Do the lookup first to force automounting. */
1318 if (path_init(dir_name, LOOKUP_FOLLOW|LOOKUP_POSITIVE|LOOKUP_DIRECTORY, &nd))
1319 retval = path_walk(dir_name, &nd);
1320 if (retval)
1321 goto fs_out;
1323 /* get superblock, locks mount_sem on success */
1324 if (fstype->fs_flags & FS_NOMOUNT)
1325 sb = ERR_PTR(-EINVAL);
1326 else if (fstype->fs_flags & FS_REQUIRES_DEV)
1327 sb = get_sb_bdev(fstype, dev_name,flags, data_page);
1328 else if (fstype->fs_flags & FS_SINGLE)
1329 sb = get_sb_single(fstype, flags, data_page);
1330 else
1331 sb = get_sb_nodev(fstype, flags, data_page);
1333 retval = PTR_ERR(sb);
1334 if (IS_ERR(sb))
1335 goto dput_out;
1337 /* Something was mounted here while we slept */
1338 while(d_mountpoint(nd.dentry) && follow_down(&nd.mnt, &nd.dentry))
1340 retval = -ENOENT;
1341 if (!nd.dentry->d_inode)
1342 goto fail;
1343 down(&nd.dentry->d_inode->i_zombie);
1344 if (!IS_DEADDIR(nd.dentry->d_inode)) {
1345 retval = -ENOMEM;
1346 mnt = add_vfsmnt(&nd, sb->s_root, dev_name);
1348 up(&nd.dentry->d_inode->i_zombie);
1349 if (!mnt)
1350 goto fail;
1351 retval = 0;
1352 unlock_out:
1353 up(&mount_sem);
1354 dput_out:
1355 path_release(&nd);
1356 fs_out:
1357 put_filesystem(fstype);
1358 return retval;
1360 fail:
1361 if (list_empty(&sb->s_mounts))
1362 kill_super(sb, 0);
1363 goto unlock_out;
1366 asmlinkage long sys_mount(char * dev_name, char * dir_name, char * type,
1367 unsigned long new_flags, void * data)
1369 int retval;
1370 unsigned long data_page;
1371 unsigned long type_page;
1372 unsigned long dev_page;
1373 char *dir_page;
1375 retval = copy_mount_options (type, &type_page);
1376 if (retval < 0)
1377 return retval;
1379 dir_page = getname(dir_name);
1380 retval = PTR_ERR(dir_page);
1381 if (IS_ERR(dir_page))
1382 goto out1;
1384 retval = copy_mount_options (dev_name, &dev_page);
1385 if (retval < 0)
1386 goto out2;
1387 retval = copy_mount_options (data, &data_page);
1388 if (retval >= 0) {
1389 lock_kernel();
1390 retval = do_mount((char*)dev_page,dir_page,(char*)type_page,
1391 new_flags, (void*)data_page);
1392 unlock_kernel();
1393 free_page(data_page);
1395 free_page(dev_page);
1396 out2:
1397 putname(dir_page);
1398 out1:
1399 free_page(type_page);
1400 return retval;
1403 void __init mount_root(void)
1405 struct file_system_type * fs_type;
1406 struct super_block * sb;
1407 struct vfsmount *vfsmnt;
1408 struct block_device *bdev = NULL;
1409 mode_t mode;
1410 int retval;
1411 void *handle;
1412 char path[64];
1413 int path_start = -1;
1415 #ifdef CONFIG_ROOT_NFS
1416 void *data;
1417 if (MAJOR(ROOT_DEV) != UNNAMED_MAJOR)
1418 goto skip_nfs;
1419 fs_type = get_fs_type("nfs");
1420 if (!fs_type)
1421 goto no_nfs;
1422 ROOT_DEV = get_unnamed_dev();
1423 if (!ROOT_DEV)
1425 * Your /linuxrc sucks worse than MSExchange - that's the
1426 * only way you could run out of anon devices at that point.
1428 goto no_anon;
1429 data = nfs_root_data();
1430 if (!data)
1431 goto no_server;
1432 sb = read_super(ROOT_DEV, NULL, fs_type, root_mountflags, data, 1);
1433 if (sb)
1435 * We _can_ fail there, but if that will happen we have no
1436 * chance anyway (no memory for vfsmnt and we _will_ need it,
1437 * no matter which fs we try to mount).
1439 goto mount_it;
1440 no_server:
1441 put_unnamed_dev(ROOT_DEV);
1442 no_anon:
1443 put_filesystem(fs_type);
1444 no_nfs:
1445 printk(KERN_ERR "VFS: Unable to mount root fs via NFS, trying floppy.\n");
1446 ROOT_DEV = MKDEV(FLOPPY_MAJOR, 0);
1447 skip_nfs:
1448 #endif
1450 #ifdef CONFIG_BLK_DEV_FD
1451 if (MAJOR(ROOT_DEV) == FLOPPY_MAJOR) {
1452 #ifdef CONFIG_BLK_DEV_RAM
1453 extern int rd_doload;
1454 extern void rd_load_secondary(void);
1455 #endif
1456 floppy_eject();
1457 #ifndef CONFIG_BLK_DEV_RAM
1458 printk(KERN_NOTICE "(Warning, this kernel has no ramdisk support)\n");
1459 #else
1460 /* rd_doload is 2 for a dual initrd/ramload setup */
1461 if(rd_doload==2)
1462 rd_load_secondary();
1463 else
1464 #endif
1466 printk(KERN_NOTICE "VFS: Insert root floppy and press ENTER\n");
1467 wait_for_keypress();
1470 #endif
1472 devfs_make_root (root_device_name);
1473 handle = devfs_find_handle (NULL, ROOT_DEVICE_NAME,
1474 MAJOR (ROOT_DEV), MINOR (ROOT_DEV),
1475 DEVFS_SPECIAL_BLK, 1);
1476 if (handle) /* Sigh: bd*() functions only paper over the cracks */
1478 unsigned major, minor;
1480 devfs_get_maj_min (handle, &major, &minor);
1481 ROOT_DEV = MKDEV (major, minor);
1485 * Probably pure paranoia, but I'm less than happy about delving into
1486 * devfs crap and checking it right now. Later.
1488 if (!ROOT_DEV)
1489 panic("I have no root and I want to scream");
1491 bdev = bdget(kdev_t_to_nr(ROOT_DEV));
1492 if (!bdev)
1493 panic(__FUNCTION__ ": unable to allocate root device");
1494 bdev->bd_op = devfs_get_ops (handle);
1495 path_start = devfs_generate_path (handle, path + 5, sizeof (path) - 5);
1496 mode = FMODE_READ;
1497 if (!(root_mountflags & MS_RDONLY))
1498 mode |= FMODE_WRITE;
1499 retval = blkdev_get(bdev, mode, 0, BDEV_FS);
1500 if (retval == -EROFS) {
1501 root_mountflags |= MS_RDONLY;
1502 retval = blkdev_get(bdev, FMODE_READ, 0, BDEV_FS);
1504 if (retval) {
1506 * Allow the user to distinguish between failed open
1507 * and bad superblock on root device.
1509 printk ("VFS: Cannot open root device \"%s\" or %s\n",
1510 root_device_name, kdevname (ROOT_DEV));
1511 printk ("Please append a correct \"root=\" boot option\n");
1512 panic("VFS: Unable to mount root fs on %s",
1513 kdevname(ROOT_DEV));
1516 check_disk_change(ROOT_DEV);
1517 sb = get_super(ROOT_DEV);
1518 if (sb) {
1519 fs_type = sb->s_type;
1520 goto mount_it;
1523 read_lock(&file_systems_lock);
1524 for (fs_type = file_systems ; fs_type ; fs_type = fs_type->next) {
1525 if (!(fs_type->fs_flags & FS_REQUIRES_DEV))
1526 continue;
1527 if (!try_inc_mod_count(fs_type->owner))
1528 continue;
1529 read_unlock(&file_systems_lock);
1530 sb = read_super(ROOT_DEV,bdev,fs_type,root_mountflags,NULL,1);
1531 if (sb)
1532 goto mount_it;
1533 read_lock(&file_systems_lock);
1534 put_filesystem(fs_type);
1536 read_unlock(&file_systems_lock);
1537 panic("VFS: Unable to mount root fs on %s", kdevname(ROOT_DEV));
1539 mount_it:
1540 printk ("VFS: Mounted root (%s filesystem)%s.\n",
1541 fs_type->name,
1542 (sb->s_flags & MS_RDONLY) ? " readonly" : "");
1543 if (path_start >= 0) {
1544 devfs_mk_symlink (NULL, "root", DEVFS_FL_DEFAULT,
1545 path + 5 + path_start, NULL, NULL);
1546 memcpy (path + path_start, "/dev/", 5);
1547 vfsmnt = add_vfsmnt(NULL, sb->s_root, path + path_start);
1549 else
1550 vfsmnt = add_vfsmnt(NULL, sb->s_root, "/dev/root");
1551 /* FIXME: if something will try to umount us right now... */
1552 if (vfsmnt) {
1553 set_fs_root(current->fs, vfsmnt, sb->s_root);
1554 set_fs_pwd(current->fs, vfsmnt, sb->s_root);
1555 if (bdev)
1556 bdput(bdev); /* sb holds a reference */
1557 return;
1559 panic("VFS: add_vfsmnt failed for root fs");
1563 static void chroot_fs_refs(struct dentry *old_root,
1564 struct vfsmount *old_rootmnt,
1565 struct dentry *new_root,
1566 struct vfsmount *new_rootmnt)
1568 struct task_struct *p;
1569 struct fs_struct *fs;
1571 read_lock(&tasklist_lock);
1572 for_each_task(p) {
1573 task_lock(p);
1574 fs = p->fs;
1575 if (fs) {
1576 atomic_inc(&fs->count);
1577 task_unlock(p);
1578 if (fs->root==old_root && fs->rootmnt==old_rootmnt)
1579 set_fs_root(fs, new_rootmnt, new_root);
1580 if (fs->pwd==old_root && fs->pwdmnt==old_rootmnt)
1581 set_fs_pwd(fs, new_rootmnt, new_root);
1582 put_fs_struct(fs);
1583 } else
1584 task_unlock(p);
1586 read_unlock(&tasklist_lock);
1590 * Moves the current root to put_root, and sets root/cwd of all processes
1591 * which had them on the old root to new_root.
1593 * Note:
1594 * - we don't move root/cwd if they are not at the root (reason: if something
1595 * cared enough to change them, it's probably wrong to force them elsewhere)
1596 * - it's okay to pick a root that isn't the root of a file system, e.g.
1597 * /nfs/my_root where /nfs is the mount point. Better avoid creating
1598 * unreachable mount points this way, though.
1601 asmlinkage long sys_pivot_root(const char *new_root, const char *put_old)
1603 struct dentry *root;
1604 struct vfsmount *root_mnt;
1605 struct vfsmount *tmp;
1606 struct nameidata new_nd, old_nd;
1607 char *name;
1608 int error;
1610 if (!capable(CAP_SYS_ADMIN))
1611 return -EPERM;
1613 lock_kernel();
1615 name = getname(new_root);
1616 error = PTR_ERR(name);
1617 if (IS_ERR(name))
1618 goto out0;
1619 error = 0;
1620 if (path_init(name, LOOKUP_POSITIVE|LOOKUP_FOLLOW|LOOKUP_DIRECTORY, &new_nd))
1621 error = path_walk(name, &new_nd);
1622 putname(name);
1623 if (error)
1624 goto out0;
1626 name = getname(put_old);
1627 error = PTR_ERR(name);
1628 if (IS_ERR(name))
1629 goto out0;
1630 error = 0;
1631 if (path_init(name, LOOKUP_POSITIVE|LOOKUP_FOLLOW|LOOKUP_DIRECTORY, &old_nd))
1632 error = path_walk(name, &old_nd);
1633 putname(name);
1634 if (error)
1635 goto out1;
1637 read_lock(&current->fs->lock);
1638 root_mnt = mntget(current->fs->rootmnt);
1639 root = dget(current->fs->root);
1640 read_unlock(&current->fs->lock);
1641 down(&mount_sem);
1642 down(&old_nd.dentry->d_inode->i_zombie);
1643 error = -ENOENT;
1644 if (IS_DEADDIR(new_nd.dentry->d_inode))
1645 goto out2;
1646 if (d_unhashed(new_nd.dentry) && !IS_ROOT(new_nd.dentry))
1647 goto out2;
1648 if (d_unhashed(old_nd.dentry) && !IS_ROOT(old_nd.dentry))
1649 goto out2;
1650 error = -EBUSY;
1651 if (new_nd.mnt == root_mnt || old_nd.mnt == root_mnt)
1652 goto out2; /* loop */
1653 error = -EINVAL;
1654 tmp = old_nd.mnt; /* make sure we can reach put_old from new_root */
1655 spin_lock(&dcache_lock);
1656 if (tmp != new_nd.mnt) {
1657 for (;;) {
1658 if (tmp->mnt_parent == tmp)
1659 goto out3;
1660 if (tmp->mnt_parent == new_nd.mnt)
1661 break;
1662 tmp = tmp->mnt_parent;
1664 if (!is_subdir(tmp->mnt_mountpoint, new_nd.dentry))
1665 goto out3;
1666 } else if (!is_subdir(old_nd.dentry, new_nd.dentry))
1667 goto out3;
1668 spin_unlock(&dcache_lock);
1670 move_vfsmnt(new_nd.mnt, new_nd.dentry, NULL, NULL);
1671 move_vfsmnt(root_mnt, old_nd.dentry, old_nd.mnt, NULL);
1672 chroot_fs_refs(root,root_mnt,new_nd.dentry,new_nd.mnt);
1673 error = 0;
1674 out2:
1675 up(&old_nd.dentry->d_inode->i_zombie);
1676 up(&mount_sem);
1677 dput(root);
1678 mntput(root_mnt);
1679 path_release(&old_nd);
1680 out1:
1681 path_release(&new_nd);
1682 out0:
1683 unlock_kernel();
1684 return error;
1685 out3:
1686 spin_unlock(&dcache_lock);
1687 goto out2;
1691 #ifdef CONFIG_BLK_DEV_INITRD
1693 int __init change_root(kdev_t new_root_dev,const char *put_old)
1695 struct vfsmount *old_rootmnt;
1696 struct nameidata devfs_nd, nd;
1697 int error = 0;
1699 read_lock(&current->fs->lock);
1700 old_rootmnt = mntget(current->fs->rootmnt);
1701 read_unlock(&current->fs->lock);
1702 /* First unmount devfs if mounted */
1703 if (path_init("/dev", LOOKUP_FOLLOW|LOOKUP_POSITIVE, &devfs_nd))
1704 error = path_walk("/dev", &devfs_nd);
1705 if (!error) {
1706 if (devfs_nd.mnt->mnt_sb->s_magic == DEVFS_SUPER_MAGIC &&
1707 devfs_nd.dentry == devfs_nd.mnt->mnt_root) {
1708 dput(devfs_nd.dentry);
1709 down(&mount_sem);
1710 /* puts devfs_nd.mnt */
1711 do_umount(devfs_nd.mnt, 0, 0);
1712 up(&mount_sem);
1713 } else
1714 path_release(&devfs_nd);
1716 ROOT_DEV = new_root_dev;
1717 mount_root();
1718 #if 1
1719 shrink_dcache();
1720 printk("change_root: old root has d_count=%d\n",
1721 atomic_read(&old_rootmnt->mnt_root->d_count));
1722 #endif
1723 mount_devfs_fs ();
1725 * Get the new mount directory
1727 error = 0;
1728 if (path_init(put_old, LOOKUP_FOLLOW|LOOKUP_POSITIVE|LOOKUP_DIRECTORY, &nd))
1729 error = path_walk(put_old, &nd);
1730 if (error) {
1731 int blivet;
1733 printk(KERN_NOTICE "Trying to unmount old root ... ");
1734 blivet = do_umount(old_rootmnt, 1, 0);
1735 if (!blivet) {
1736 printk("okay\n");
1737 return 0;
1739 printk(KERN_ERR "error %d\n",blivet);
1740 return error;
1742 /* FIXME: we should hold i_zombie on nd.dentry */
1743 move_vfsmnt(old_rootmnt, nd.dentry, nd.mnt, "/dev/root.old");
1744 mntput(old_rootmnt);
1745 path_release(&nd);
1746 return 0;
1749 #endif