From Geert: Quickfix to get DDB compile.
[linux-2.6/linux-mips.git] / fs / super.c
blob57d3698d3453b600c1a0dfc97e080be3d4a5ece2
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 static 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
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).
300 static struct vfsmount *add_vfsmnt(struct nameidata *nd,
301 struct dentry *root,
302 const char *dev_name)
304 struct vfsmount *mnt;
305 struct super_block *sb = root->d_inode->i_sb;
306 char *name;
308 mnt = kmalloc(sizeof(struct vfsmount), GFP_KERNEL);
309 if (!mnt)
310 goto out;
311 memset(mnt, 0, sizeof(struct vfsmount));
313 /* It may be NULL, but who cares? */
314 if (dev_name) {
315 name = kmalloc(strlen(dev_name)+1, GFP_KERNEL);
316 if (name) {
317 strcpy(name, dev_name);
318 mnt->mnt_devname = name;
321 mnt->mnt_owner = current->uid;
322 atomic_set(&mnt->mnt_count,1);
323 mnt->mnt_sb = sb;
325 if (nd && !IS_ROOT(nd->dentry) && d_unhashed(nd->dentry))
326 goto fail;
327 mnt->mnt_root = dget(root);
328 mnt->mnt_mountpoint = nd ? dget(nd->dentry) : dget(root);
329 mnt->mnt_parent = nd ? mntget(nd->mnt) : mnt;
331 if (nd) {
332 list_add(&mnt->mnt_child, &nd->mnt->mnt_mounts);
333 list_add(&mnt->mnt_clash, &nd->dentry->d_vfsmnt);
334 } else {
335 INIT_LIST_HEAD(&mnt->mnt_child);
336 INIT_LIST_HEAD(&mnt->mnt_clash);
338 INIT_LIST_HEAD(&mnt->mnt_mounts);
339 list_add(&mnt->mnt_instances, &sb->s_mounts);
340 list_add(&mnt->mnt_list, vfsmntlist.prev);
341 out:
342 return mnt;
343 fail:
344 kfree(mnt->mnt_devname);
345 kfree(mnt);
346 return NULL;
349 static void move_vfsmnt(struct vfsmount *mnt,
350 struct dentry *mountpoint,
351 struct vfsmount *parent,
352 const char *dev_name)
354 struct dentry *old_mountpoint;
355 struct vfsmount *old_parent;
356 char *new_devname = NULL;
358 if (dev_name) {
359 new_devname = kmalloc(strlen(dev_name)+1, GFP_KERNEL);
360 if (new_devname)
361 strcpy(new_devname, dev_name);
364 old_mountpoint = mnt->mnt_mountpoint;
365 old_parent = mnt->mnt_parent;
367 /* flip names */
368 if (new_devname) {
369 kfree(mnt->mnt_devname);
370 mnt->mnt_devname = new_devname;
373 /* flip the linkage */
374 mnt->mnt_mountpoint = dget(mountpoint);
375 mnt->mnt_parent = parent ? mntget(parent) : mnt;
376 list_del(&mnt->mnt_clash);
377 list_del(&mnt->mnt_child);
378 if (parent) {
379 list_add(&mnt->mnt_child, &parent->mnt_mounts);
380 list_add(&mnt->mnt_clash, &mountpoint->d_vfsmnt);
381 } else {
382 INIT_LIST_HEAD(&mnt->mnt_child);
383 INIT_LIST_HEAD(&mnt->mnt_clash);
386 /* put the old stuff */
387 dput(old_mountpoint);
388 if (old_parent != mnt)
389 mntput(old_parent);
392 static void remove_vfsmnt(struct vfsmount *mnt)
394 /* First of all, remove it from all lists */
395 list_del(&mnt->mnt_instances);
396 list_del(&mnt->mnt_clash);
397 list_del(&mnt->mnt_list);
398 list_del(&mnt->mnt_child);
399 /* Now we can work safely */
400 if (mnt->mnt_parent != mnt)
401 mntput(mnt->mnt_parent);
403 dput(mnt->mnt_mountpoint);
404 dput(mnt->mnt_root);
405 kfree(mnt->mnt_devname);
406 kfree(mnt);
409 static struct proc_fs_info {
410 int flag;
411 char *str;
412 } fs_info[] = {
413 { MS_NOEXEC, ",noexec" },
414 { MS_NOSUID, ",nosuid" },
415 { MS_NODEV, ",nodev" },
416 { MS_SYNCHRONOUS, ",sync" },
417 { MS_MANDLOCK, ",mand" },
418 { MS_NOATIME, ",noatime" },
419 { MS_NODIRATIME, ",nodiratime" },
420 #ifdef MS_NOSUB /* Can't find this except in mount.c */
421 { MS_NOSUB, ",nosub" },
422 #endif
423 { 0, NULL }
426 static struct proc_nfs_info {
427 int flag;
428 char *str;
429 char *nostr;
430 } nfs_info[] = {
431 { NFS_MOUNT_SOFT, ",soft", ",hard" },
432 { NFS_MOUNT_INTR, ",intr", "" },
433 { NFS_MOUNT_POSIX, ",posix", "" },
434 { NFS_MOUNT_TCP, ",tcp", ",udp" },
435 { NFS_MOUNT_NOCTO, ",nocto", "" },
436 { NFS_MOUNT_NOAC, ",noac", "" },
437 { NFS_MOUNT_NONLM, ",nolock", ",lock" },
438 { 0, NULL, NULL }
441 int get_filesystem_info( char *buf )
443 struct list_head *p;
444 struct proc_fs_info *fs_infop;
445 struct proc_nfs_info *nfs_infop;
446 struct nfs_server *nfss;
447 int len = 0;
448 char *path,*buffer = (char *) __get_free_page(GFP_KERNEL);
450 if (!buffer) return 0;
451 for (p = vfsmntlist.next; p!=&vfsmntlist && len < PAGE_SIZE - 160;
452 p = p->next) {
453 struct vfsmount *tmp = list_entry(p, struct vfsmount, mnt_list);
454 path = d_path(tmp->mnt_root, tmp, buffer, PAGE_SIZE);
455 if (!path)
456 continue;
457 len += sprintf( buf + len, "%s %s %s %s",
458 tmp->mnt_devname, path,
459 tmp->mnt_sb->s_type->name,
460 tmp->mnt_sb->s_flags & MS_RDONLY ? "ro" : "rw" );
461 for (fs_infop = fs_info; fs_infop->flag; fs_infop++) {
462 if (tmp->mnt_sb->s_flags & fs_infop->flag) {
463 strcpy(buf + len, fs_infop->str);
464 len += strlen(fs_infop->str);
467 if (!strcmp("nfs", tmp->mnt_sb->s_type->name)) {
468 nfss = &tmp->mnt_sb->u.nfs_sb.s_server;
469 len += sprintf(buf+len, ",v%d", nfss->rpc_ops->version);
471 len += sprintf(buf+len, ",rsize=%d", nfss->rsize);
473 len += sprintf(buf+len, ",wsize=%d", nfss->wsize);
474 #if 0
475 if (nfss->timeo != 7*HZ/10) {
476 len += sprintf(buf+len, ",timeo=%d",
477 nfss->timeo*10/HZ);
479 if (nfss->retrans != 3) {
480 len += sprintf(buf+len, ",retrans=%d",
481 nfss->retrans);
483 #endif
484 if (nfss->acregmin != 3*HZ) {
485 len += sprintf(buf+len, ",acregmin=%d",
486 nfss->acregmin/HZ);
488 if (nfss->acregmax != 60*HZ) {
489 len += sprintf(buf+len, ",acregmax=%d",
490 nfss->acregmax/HZ);
492 if (nfss->acdirmin != 30*HZ) {
493 len += sprintf(buf+len, ",acdirmin=%d",
494 nfss->acdirmin/HZ);
496 if (nfss->acdirmax != 60*HZ) {
497 len += sprintf(buf+len, ",acdirmax=%d",
498 nfss->acdirmax/HZ);
500 for (nfs_infop = nfs_info; nfs_infop->flag; nfs_infop++) {
501 char *str;
502 if (nfss->flags & nfs_infop->flag)
503 str = nfs_infop->str;
504 else
505 str = nfs_infop->nostr;
506 strcpy(buf + len, str);
507 len += strlen(str);
509 len += sprintf(buf+len, ",addr=%s",
510 nfss->hostname);
512 len += sprintf( buf + len, " 0 0\n" );
515 free_page((unsigned long) buffer);
516 return len;
520 * __wait_on_super - wait on a superblock
521 * @sb: superblock to wait on
523 * Waits for a superblock to become unlocked and then returns. It does
524 * not take the lock. This is an internal function. See wait_on_super().
527 void __wait_on_super(struct super_block * sb)
529 DECLARE_WAITQUEUE(wait, current);
531 add_wait_queue(&sb->s_wait, &wait);
532 repeat:
533 set_current_state(TASK_UNINTERRUPTIBLE);
534 if (sb->s_lock) {
535 schedule();
536 goto repeat;
538 remove_wait_queue(&sb->s_wait, &wait);
539 current->state = TASK_RUNNING;
543 * Note: check the dirty flag before waiting, so we don't
544 * hold up the sync while mounting a device. (The newly
545 * mounted device won't need syncing.)
547 void sync_supers(kdev_t dev)
549 struct super_block * sb;
551 for (sb = sb_entry(super_blocks.next);
552 sb != sb_entry(&super_blocks);
553 sb = sb_entry(sb->s_list.next)) {
554 if (!sb->s_dev)
555 continue;
556 if (dev && sb->s_dev != dev)
557 continue;
558 if (!sb->s_dirt)
559 continue;
560 lock_super(sb);
561 if (sb->s_dev && sb->s_dirt && (!dev || dev == sb->s_dev))
562 if (sb->s_op && sb->s_op->write_super)
563 sb->s_op->write_super(sb);
564 unlock_super(sb);
569 * get_super - get the superblock of a device
570 * @dev: device to get the superblock for
572 * Scans the superblock list and finds the superblock of the file system
573 * mounted on the device given. %NULL is returned if no match is found.
576 struct super_block * get_super(kdev_t dev)
578 struct super_block * s;
580 if (!dev)
581 return NULL;
582 restart:
583 s = sb_entry(super_blocks.next);
584 while (s != sb_entry(&super_blocks))
585 if (s->s_dev == dev) {
586 wait_on_super(s);
587 if (s->s_dev == dev)
588 return s;
589 goto restart;
590 } else
591 s = sb_entry(s->s_list.next);
592 return NULL;
595 asmlinkage long sys_ustat(dev_t dev, struct ustat * ubuf)
597 struct super_block *s;
598 struct ustat tmp;
599 struct statfs sbuf;
600 int err = -EINVAL;
602 lock_kernel();
603 s = get_super(to_kdev_t(dev));
604 if (s == NULL)
605 goto out;
606 err = vfs_statfs(s, &sbuf);
607 if (err)
608 goto out;
610 memset(&tmp,0,sizeof(struct ustat));
611 tmp.f_tfree = sbuf.f_bfree;
612 tmp.f_tinode = sbuf.f_ffree;
614 err = copy_to_user(ubuf,&tmp,sizeof(struct ustat)) ? -EFAULT : 0;
615 out:
616 unlock_kernel();
617 return err;
621 * get_empty_super - find empty superblocks
623 * Find a superblock with no device assigned. A free superblock is
624 * found and returned. If neccessary new superblocks are allocated.
625 * %NULL is returned if there are insufficient resources to complete
626 * the request.
629 struct super_block *get_empty_super(void)
631 struct super_block *s;
633 for (s = sb_entry(super_blocks.next);
634 s != sb_entry(&super_blocks);
635 s = sb_entry(s->s_list.next)) {
636 if (s->s_dev)
637 continue;
638 if (!s->s_lock)
639 return s;
640 printk("VFS: empty superblock %p locked!\n", s);
642 /* Need a new one... */
643 if (nr_super_blocks >= max_super_blocks)
644 return NULL;
645 s = kmalloc(sizeof(struct super_block), GFP_USER);
646 if (s) {
647 nr_super_blocks++;
648 memset(s, 0, sizeof(struct super_block));
649 INIT_LIST_HEAD(&s->s_dirty);
650 list_add (&s->s_list, super_blocks.prev);
651 init_waitqueue_head(&s->s_wait);
652 INIT_LIST_HEAD(&s->s_files);
653 INIT_LIST_HEAD(&s->s_mounts);
655 return s;
658 static struct super_block * read_super(kdev_t dev, struct block_device *bdev,
659 struct file_system_type *type, int flags,
660 void *data, int silent)
662 struct super_block * s;
663 s = get_empty_super();
664 if (!s)
665 goto out;
666 s->s_dev = dev;
667 s->s_bdev = bdev;
668 s->s_flags = flags;
669 s->s_dirt = 0;
670 sema_init(&s->s_vfs_rename_sem,1);
671 sema_init(&s->s_nfsd_free_path_sem,1);
672 s->s_type = type;
673 sema_init(&s->s_dquot.dqio_sem, 1);
674 sema_init(&s->s_dquot.dqoff_sem, 1);
675 s->s_dquot.flags = 0;
676 lock_super(s);
677 if (!type->read_super(s, data, silent))
678 goto out_fail;
679 unlock_super(s);
680 /* tell bdcache that we are going to keep this one */
681 if (bdev)
682 atomic_inc(&bdev->bd_count);
683 out:
684 return s;
686 out_fail:
687 s->s_dev = 0;
688 s->s_bdev = 0;
689 s->s_type = NULL;
690 unlock_super(s);
691 return NULL;
695 * Unnamed block devices are dummy devices used by virtual
696 * filesystems which don't use real block-devices. -- jrs
699 static unsigned int unnamed_dev_in_use[256/(8*sizeof(unsigned int))];
701 kdev_t get_unnamed_dev(void)
703 int i;
705 for (i = 1; i < 256; i++) {
706 if (!test_and_set_bit(i,unnamed_dev_in_use))
707 return MKDEV(UNNAMED_MAJOR, i);
709 return 0;
712 void put_unnamed_dev(kdev_t dev)
714 if (!dev || MAJOR(dev) != UNNAMED_MAJOR)
715 return;
716 if (test_and_clear_bit(MINOR(dev), unnamed_dev_in_use))
717 return;
718 printk("VFS: put_unnamed_dev: freeing unused device %s\n",
719 kdevname(dev));
722 static struct super_block *get_sb_bdev(struct file_system_type *fs_type,
723 char *dev_name, int flags, void * data)
725 struct inode *inode;
726 struct block_device *bdev;
727 struct block_device_operations *bdops;
728 struct super_block * sb;
729 struct nameidata nd;
730 kdev_t dev;
731 int error = 0;
732 /* What device it is? */
733 if (!dev_name || !*dev_name)
734 return ERR_PTR(-EINVAL);
735 if (path_init(dev_name, LOOKUP_FOLLOW|LOOKUP_POSITIVE, &nd))
736 error = path_walk(dev_name, &nd);
737 if (error)
738 return ERR_PTR(error);
739 inode = nd.dentry->d_inode;
740 error = -ENOTBLK;
741 if (!S_ISBLK(inode->i_mode))
742 goto out;
743 error = -EACCES;
744 if (IS_NODEV(inode))
745 goto out;
746 bdev = inode->i_bdev;
747 bdops = devfs_get_ops ( devfs_get_handle_from_inode (inode) );
748 if (bdops) bdev->bd_op = bdops;
749 /* Done with lookups, semaphore down */
750 down(&mount_sem);
751 dev = to_kdev_t(bdev->bd_dev);
752 sb = get_super(dev);
753 if (sb) {
754 if (fs_type == sb->s_type) {
755 path_release(&nd);
756 return sb;
758 } else {
759 mode_t mode = FMODE_READ; /* we always need it ;-) */
760 if (!(flags & MS_RDONLY))
761 mode |= FMODE_WRITE;
762 error = blkdev_get(bdev, mode, 0, BDEV_FS);
763 if (error)
764 goto out;
765 check_disk_change(dev);
766 error = -EACCES;
767 if (!(flags & MS_RDONLY) && is_read_only(dev))
768 goto out1;
769 error = -EINVAL;
770 sb = read_super(dev, bdev, fs_type, flags, data, 0);
771 if (sb) {
772 get_filesystem(fs_type);
773 path_release(&nd);
774 return sb;
776 out1:
777 blkdev_put(bdev, BDEV_FS);
779 out:
780 path_release(&nd);
781 up(&mount_sem);
782 return ERR_PTR(error);
785 static struct super_block *get_sb_nodev(struct file_system_type *fs_type,
786 int flags, void * data)
788 kdev_t dev;
789 int error = -EMFILE;
790 down(&mount_sem);
791 dev = get_unnamed_dev();
792 if (dev) {
793 struct super_block * sb;
794 error = -EINVAL;
795 sb = read_super(dev, NULL, fs_type, flags, data, 0);
796 if (sb) {
797 get_filesystem(fs_type);
798 return sb;
800 put_unnamed_dev(dev);
802 up(&mount_sem);
803 return ERR_PTR(error);
806 static struct super_block *get_sb_single(struct file_system_type *fs_type,
807 int flags, void *data)
809 struct super_block * sb;
811 * Get the superblock of kernel-wide instance, but
812 * keep the reference to fs_type.
814 down(&mount_sem);
815 sb = fs_type->kern_mnt->mnt_sb;
816 if (!sb)
817 BUG();
818 get_filesystem(fs_type);
819 do_remount_sb(sb, flags, data);
820 return sb;
823 static struct block_device *kill_super(struct super_block *sb, int umount_root)
825 struct block_device *bdev;
826 kdev_t dev;
827 dput(sb->s_root);
828 sb->s_root = NULL;
829 lock_super(sb);
830 if (sb->s_op) {
831 if (sb->s_op->write_super && sb->s_dirt)
832 sb->s_op->write_super(sb);
833 if (sb->s_op->put_super)
834 sb->s_op->put_super(sb);
837 /* Forget any remaining inodes */
838 if (invalidate_inodes(sb)) {
839 printk("VFS: Busy inodes after unmount. "
840 "Self-destruct in 5 seconds. Have a nice day...\n");
843 dev = sb->s_dev;
844 sb->s_dev = 0; /* Free the superblock */
845 bdev = sb->s_bdev;
846 sb->s_bdev = NULL;
847 put_filesystem(sb->s_type);
848 sb->s_type = NULL;
849 unlock_super(sb);
850 if (umount_root) {
851 /* special: the old device driver is going to be
852 a ramdisk and the point of this call is to free its
853 protected memory (even if dirty). */
854 destroy_buffers(dev);
856 if (bdev) {
857 blkdev_put(bdev, BDEV_FS);
858 bdput(bdev);
859 } else
860 put_unnamed_dev(dev);
861 return bdev;
865 * Alters the mount flags of a mounted file system. Only the mount point
866 * is used as a reference - file system type and the device are ignored.
869 static int do_remount_sb(struct super_block *sb, int flags, char *data)
871 int retval;
873 if (!(flags & MS_RDONLY) && sb->s_dev && is_read_only(sb->s_dev))
874 return -EACCES;
875 /*flags |= MS_RDONLY;*/
876 /* If we are remounting RDONLY, make sure there are no rw files open */
877 if ((flags & MS_RDONLY) && !(sb->s_flags & MS_RDONLY))
878 if (!fs_may_remount_ro(sb))
879 return -EBUSY;
880 if (sb->s_op && sb->s_op->remount_fs) {
881 lock_super(sb);
882 retval = sb->s_op->remount_fs(sb, &flags, data);
883 unlock_super(sb);
884 if (retval)
885 return retval;
887 sb->s_flags = (sb->s_flags & ~MS_RMT_MASK) | (flags & MS_RMT_MASK);
890 * We can't invalidate inodes as we can loose data when remounting
891 * (someone might manage to alter data while we are waiting in lock_super()
892 * or in foo_remount_fs()))
895 return 0;
898 struct vfsmount *kern_mount(struct file_system_type *type)
900 kdev_t dev = get_unnamed_dev();
901 struct super_block *sb;
902 struct vfsmount *mnt;
903 if (!dev)
904 return ERR_PTR(-EMFILE);
905 sb = read_super(dev, NULL, type, 0, NULL, 0);
906 if (!sb) {
907 put_unnamed_dev(dev);
908 return ERR_PTR(-EINVAL);
910 mnt = add_vfsmnt(NULL, sb->s_root, "none");
911 if (!mnt) {
912 kill_super(sb, 0);
913 return ERR_PTR(-ENOMEM);
915 type->kern_mnt = mnt;
916 return mnt;
919 /* Call only after unregister_filesystem() - it's a final cleanup */
921 void kern_umount(struct vfsmount *mnt)
923 struct super_block *sb = mnt->mnt_sb;
924 remove_vfsmnt(mnt);
925 kill_super(sb, 0);
929 * Doesn't take quota and stuff into account. IOW, in some cases it will
930 * give false negatives. The main reason why it's here is that we need
931 * a non-destructive way to look for easily umountable filesystems.
933 int may_umount(struct vfsmount *mnt)
935 if (atomic_read(&mnt->mnt_count) > 2)
936 return -EBUSY;
937 return 0;
940 static int do_umount(struct vfsmount *mnt, int umount_root, int flags)
942 struct super_block * sb = mnt->mnt_sb;
945 * No sense to grab the lock for this test, but test itself looks
946 * somewhat bogus. Suggestions for better replacement?
947 * Ho-hum... In principle, we might treat that as umount + switch
948 * to rootfs. GC would eventually take care of the old vfsmount.
949 * The problem being: we have to implement rootfs and GC for that ;-)
950 * Actually it makes sense, especially if rootfs would contain a
951 * /reboot - static binary that would close all descriptors and
952 * call reboot(9). Then init(8) could umount root and exec /reboot.
954 if (mnt == current->fs->rootmnt && !umount_root) {
955 int retval = 0;
957 * Special case for "unmounting" root ...
958 * we just try to remount it readonly.
960 mntput(mnt);
961 if (!(sb->s_flags & MS_RDONLY))
962 retval = do_remount_sb(sb, MS_RDONLY, 0);
963 return retval;
966 if (atomic_read(&mnt->mnt_count) > 2) {
967 mntput(mnt);
968 return -EBUSY;
971 if (mnt->mnt_instances.next != mnt->mnt_instances.prev) {
972 if (sb->s_type->fs_flags & FS_SINGLE)
973 put_filesystem(sb->s_type);
974 /* We hold two references, so mntput() is safe */
975 mntput(mnt);
976 remove_vfsmnt(mnt);
977 return 0;
981 * Before checking whether the filesystem is still busy,
982 * make sure the kernel doesn't hold any quota files open
983 * on the device. If the umount fails, too bad -- there
984 * are no quotas running any more. Just turn them on again.
986 DQUOT_OFF(sb);
987 acct_auto_close(sb->s_dev);
990 * If we may have to abort operations to get out of this
991 * mount, and they will themselves hold resources we must
992 * allow the fs to do things. In the Unix tradition of
993 * 'Gee thats tricky lets do it in userspace' the umount_begin
994 * might fail to complete on the first run through as other tasks
995 * must return, and the like. Thats for the mount program to worry
996 * about for the moment.
999 if( (flags&MNT_FORCE) && sb->s_op->umount_begin)
1000 sb->s_op->umount_begin(sb);
1003 * Shrink dcache, then fsync. This guarantees that if the
1004 * filesystem is quiescent at this point, then (a) only the
1005 * root entry should be in use and (b) that root entry is
1006 * clean.
1008 shrink_dcache_sb(sb);
1009 fsync_dev(sb->s_dev);
1011 if (sb->s_root->d_inode->i_state) {
1012 mntput(mnt);
1013 return -EBUSY;
1016 /* Something might grab it again - redo checks */
1018 if (atomic_read(&mnt->mnt_count) > 2) {
1019 mntput(mnt);
1020 return -EBUSY;
1023 /* OK, that's the point of no return */
1024 mntput(mnt);
1025 remove_vfsmnt(mnt);
1027 kill_super(sb, umount_root);
1028 return 0;
1032 * Now umount can handle mount points as well as block devices.
1033 * This is important for filesystems which use unnamed block devices.
1035 * We now support a flag for forced unmount like the other 'big iron'
1036 * unixes. Our API is identical to OSF/1 to avoid making a mess of AMD
1039 asmlinkage long sys_umount(char * name, int flags)
1041 struct nameidata nd;
1042 char *kname;
1043 int retval;
1045 lock_kernel();
1046 kname = getname(name);
1047 retval = PTR_ERR(kname);
1048 if (IS_ERR(kname))
1049 goto out;
1050 retval = 0;
1051 if (path_init(kname, LOOKUP_POSITIVE|LOOKUP_FOLLOW, &nd))
1052 retval = path_walk(kname, &nd);
1053 putname(kname);
1054 if (retval)
1055 goto out;
1056 retval = -EINVAL;
1057 if (nd.dentry!=nd.mnt->mnt_root)
1058 goto dput_and_out;
1060 retval = -EPERM;
1061 if (!capable(CAP_SYS_ADMIN) && current->uid!=nd.mnt->mnt_owner)
1062 goto dput_and_out;
1064 dput(nd.dentry);
1065 /* puts nd.mnt */
1066 down(&mount_sem);
1067 retval = do_umount(nd.mnt, 0, flags);
1068 up(&mount_sem);
1069 goto out;
1070 dput_and_out:
1071 path_release(&nd);
1072 out:
1073 unlock_kernel();
1074 return retval;
1078 * The 2.0 compatible umount. No flags.
1081 asmlinkage long sys_oldumount(char * name)
1083 return sys_umount(name,0);
1086 static int mount_is_safe(struct nameidata *nd)
1088 if (capable(CAP_SYS_ADMIN))
1089 return 0;
1090 return -EPERM;
1091 #ifdef notyet
1092 if (S_ISLNK(nd->dentry->d_inode->i_mode))
1093 return -EPERM;
1094 if (nd->dentry->d_inode->i_mode & S_ISVTX) {
1095 if (current->uid != nd->dentry->d_inode->i_uid)
1096 return -EPERM;
1098 if (permission(nd->dentry->d_inode, MAY_WRITE))
1099 return -EPERM;
1100 return 0;
1101 #endif
1105 * do loopback mount.
1107 static int do_loopback(char *old_name, char *new_name)
1109 struct nameidata old_nd, new_nd;
1110 int err = 0;
1111 if (!old_name || !*old_name)
1112 return -EINVAL;
1113 if (path_init(old_name, LOOKUP_POSITIVE, &old_nd))
1114 err = path_walk(old_name, &old_nd);
1115 if (err)
1116 goto out;
1117 if (path_init(new_name, LOOKUP_POSITIVE, &new_nd))
1118 err = path_walk(new_name, &new_nd);
1119 if (err)
1120 goto out1;
1121 err = mount_is_safe(&new_nd);
1122 if (err)
1123 goto out2;
1124 err = -EINVAL;
1125 if (S_ISDIR(new_nd.dentry->d_inode->i_mode) !=
1126 S_ISDIR(old_nd.dentry->d_inode->i_mode))
1127 goto out2;
1129 err = -ENOMEM;
1130 if (old_nd.mnt->mnt_sb->s_type->fs_flags & FS_SINGLE)
1131 get_filesystem(old_nd.mnt->mnt_sb->s_type);
1133 down(&mount_sem);
1134 /* there we go */
1135 down(&new_nd.dentry->d_inode->i_zombie);
1136 if (IS_DEADDIR(new_nd.dentry->d_inode))
1137 err = -ENOENT;
1138 else if (add_vfsmnt(&new_nd, old_nd.dentry, old_nd.mnt->mnt_devname))
1139 err = 0;
1140 up(&new_nd.dentry->d_inode->i_zombie);
1141 up(&mount_sem);
1142 if (err && old_nd.mnt->mnt_sb->s_type->fs_flags & FS_SINGLE)
1143 put_filesystem(old_nd.mnt->mnt_sb->s_type);
1144 out2:
1145 path_release(&new_nd);
1146 out1:
1147 path_release(&old_nd);
1148 out:
1149 return err;
1153 * change filesystem flags. dir should be a physical root of filesystem.
1154 * If you've mounted a non-root directory somewhere and want to do remount
1155 * on it - tough luck.
1158 static int do_remount(const char *dir,int flags,char *data)
1160 struct nameidata nd;
1161 int retval = 0;
1163 if (!capable(CAP_SYS_ADMIN))
1164 return -EPERM;
1166 if (path_init(dir, LOOKUP_FOLLOW|LOOKUP_POSITIVE, &nd))
1167 retval = path_walk(dir, &nd);
1168 if (!retval) {
1169 struct super_block * sb = nd.dentry->d_inode->i_sb;
1170 retval = -ENODEV;
1171 if (sb) {
1172 retval = -EINVAL;
1173 if (nd.dentry == sb->s_root) {
1175 * Shrink the dcache and sync the device.
1177 shrink_dcache_sb(sb);
1178 fsync_dev(sb->s_dev);
1179 if (flags & MS_RDONLY)
1180 acct_auto_close(sb->s_dev);
1181 retval = do_remount_sb(sb, flags, data);
1184 path_release(&nd);
1186 return retval;
1189 static int copy_mount_options (const void *data, unsigned long *where)
1191 int i;
1192 unsigned long page;
1193 unsigned long size;
1195 *where = 0;
1196 if (!data)
1197 return 0;
1199 if (!(page = __get_free_page(GFP_KERNEL)))
1200 return -ENOMEM;
1202 /* We only care that *some* data at the address the user
1203 * gave us is valid. Just in case, we'll zero
1204 * the remainder of the page.
1206 /* copy_from_user cannot cross TASK_SIZE ! */
1207 size = TASK_SIZE - (unsigned long)data;
1208 if (size > PAGE_SIZE)
1209 size = PAGE_SIZE;
1211 i = size - copy_from_user((void *)page, data, size);
1212 if (!i) {
1213 free_page(page);
1214 return -EFAULT;
1216 if (i != PAGE_SIZE)
1217 memset((char *)page + i, 0, PAGE_SIZE - i);
1218 *where = page;
1219 return 0;
1223 * Flags is a 16-bit value that allows up to 16 non-fs dependent flags to
1224 * be given to the mount() call (ie: read-only, no-dev, no-suid etc).
1226 * data is a (void *) that can point to any structure up to
1227 * PAGE_SIZE-1 bytes, which can contain arbitrary fs-dependent
1228 * information (or be NULL).
1230 * NOTE! As old versions of mount() didn't use this setup, the flags
1231 * have to have a special 16-bit magic number in the high word:
1232 * 0xC0ED. If this magic word isn't present, the flags and data info
1233 * aren't used, as the syscall assumes we are talking to an older
1234 * version that didn't understand them.
1236 long do_mount(char * dev_name, char * dir_name, char *type_page,
1237 unsigned long new_flags, void *data_page)
1239 struct file_system_type * fstype;
1240 struct nameidata nd;
1241 struct vfsmount *mnt = NULL;
1242 struct super_block *sb;
1243 int retval = 0;
1244 unsigned long flags = 0;
1246 /* Basic sanity checks */
1248 if (!dir_name || !*dir_name || !memchr(dir_name, 0, PAGE_SIZE))
1249 return -EINVAL;
1250 if (dev_name && !memchr(dev_name, 0, PAGE_SIZE))
1251 return -EINVAL;
1253 /* OK, looks good, now let's see what do they want */
1255 /* just change the flags? - capabilities are checked in do_remount() */
1256 if ((new_flags & (MS_MGC_MSK|MS_REMOUNT)) == (MS_MGC_VAL|MS_REMOUNT))
1257 return do_remount(dir_name, new_flags&~(MS_MGC_MSK|MS_REMOUNT),
1258 (char *) data_page);
1260 if ((new_flags & MS_MGC_MSK) == MS_MGC_VAL)
1261 flags = new_flags & ~MS_MGC_MSK;
1263 /* For the rest we need the type */
1265 if (!type_page || !memchr(type_page, 0, PAGE_SIZE))
1266 return -EINVAL;
1268 /* loopback mount? This is special - requires fewer capabilities */
1269 if (strcmp(type_page, "bind")==0)
1270 return do_loopback(dev_name, dir_name);
1272 /* for the rest we _really_ need capabilities... */
1273 if (!capable(CAP_SYS_ADMIN))
1274 return -EPERM;
1276 /* ... filesystem driver... */
1277 fstype = get_fs_type(type_page);
1278 if (!fstype)
1279 return -ENODEV;
1281 /* ... and mountpoint. Do the lookup first to force automounting. */
1282 if (path_init(dir_name, LOOKUP_FOLLOW|LOOKUP_POSITIVE|LOOKUP_DIRECTORY, &nd))
1283 retval = path_walk(dir_name, &nd);
1284 if (retval)
1285 goto fs_out;
1287 /* get superblock, locks mount_sem on success */
1288 if (fstype->fs_flags & FS_NOMOUNT)
1289 sb = ERR_PTR(-EINVAL);
1290 else if (fstype->fs_flags & FS_REQUIRES_DEV)
1291 sb = get_sb_bdev(fstype, dev_name,flags, data_page);
1292 else if (fstype->fs_flags & FS_SINGLE)
1293 sb = get_sb_single(fstype, flags, data_page);
1294 else
1295 sb = get_sb_nodev(fstype, flags, data_page);
1297 retval = PTR_ERR(sb);
1298 if (IS_ERR(sb))
1299 goto dput_out;
1301 /* Something was mounted here while we slept */
1302 while(d_mountpoint(nd.dentry) && follow_down(&nd.mnt, &nd.dentry))
1304 retval = -ENOENT;
1305 if (!nd.dentry->d_inode)
1306 goto fail;
1307 down(&nd.dentry->d_inode->i_zombie);
1308 if (!IS_DEADDIR(nd.dentry->d_inode)) {
1309 retval = -ENOMEM;
1310 mnt = add_vfsmnt(&nd, sb->s_root, dev_name);
1312 up(&nd.dentry->d_inode->i_zombie);
1313 if (!mnt)
1314 goto fail;
1315 retval = 0;
1316 unlock_out:
1317 up(&mount_sem);
1318 dput_out:
1319 path_release(&nd);
1320 fs_out:
1321 put_filesystem(fstype);
1322 return retval;
1324 fail:
1325 if (list_empty(&sb->s_mounts))
1326 kill_super(sb, 0);
1327 goto unlock_out;
1330 asmlinkage long sys_mount(char * dev_name, char * dir_name, char * type,
1331 unsigned long new_flags, void * data)
1333 int retval;
1334 unsigned long data_page;
1335 unsigned long type_page;
1336 unsigned long dev_page;
1337 char *dir_page;
1339 retval = copy_mount_options (type, &type_page);
1340 if (retval < 0)
1341 return retval;
1343 dir_page = getname(dir_name);
1344 retval = PTR_ERR(dir_page);
1345 if (IS_ERR(dir_page))
1346 goto out1;
1348 retval = copy_mount_options (dev_name, &dev_page);
1349 if (retval < 0)
1350 goto out2;
1351 retval = copy_mount_options (data, &data_page);
1352 if (retval >= 0) {
1353 lock_kernel();
1354 retval = do_mount((char*)dev_page,dir_page,(char*)type_page,
1355 new_flags, (void*)data_page);
1356 unlock_kernel();
1357 free_page(data_page);
1359 free_page(dev_page);
1360 out2:
1361 putname(dir_page);
1362 out1:
1363 free_page(type_page);
1364 return retval;
1367 void __init mount_root(void)
1369 struct file_system_type * fs_type;
1370 struct super_block * sb;
1371 struct vfsmount *vfsmnt;
1372 struct block_device *bdev = NULL;
1373 mode_t mode;
1374 int retval;
1375 void *handle;
1376 char path[64];
1377 int path_start = -1;
1379 #ifdef CONFIG_ROOT_NFS
1380 void *data;
1381 if (MAJOR(ROOT_DEV) != UNNAMED_MAJOR)
1382 goto skip_nfs;
1383 fs_type = get_fs_type("nfs");
1384 if (!fs_type)
1385 goto no_nfs;
1386 ROOT_DEV = get_unnamed_dev();
1387 if (!ROOT_DEV)
1389 * Your /linuxrc sucks worse than MSExchange - that's the
1390 * only way you could run out of anon devices at that point.
1392 goto no_anon;
1393 data = nfs_root_data();
1394 if (!data)
1395 goto no_server;
1396 sb = read_super(ROOT_DEV, NULL, fs_type, root_mountflags, data, 1);
1397 if (sb)
1399 * We _can_ fail there, but if that will happen we have no
1400 * chance anyway (no memory for vfsmnt and we _will_ need it,
1401 * no matter which fs we try to mount).
1403 goto mount_it;
1404 no_server:
1405 put_unnamed_dev(ROOT_DEV);
1406 no_anon:
1407 put_filesystem(fs_type);
1408 no_nfs:
1409 printk(KERN_ERR "VFS: Unable to mount root fs via NFS, trying floppy.\n");
1410 ROOT_DEV = MKDEV(FLOPPY_MAJOR, 0);
1411 skip_nfs:
1412 #endif
1414 #ifdef CONFIG_BLK_DEV_FD
1415 if (MAJOR(ROOT_DEV) == FLOPPY_MAJOR) {
1416 #ifdef CONFIG_BLK_DEV_RAM
1417 extern int rd_doload;
1418 extern void rd_load_secondary(void);
1419 #endif
1420 floppy_eject();
1421 #ifndef CONFIG_BLK_DEV_RAM
1422 printk(KERN_NOTICE "(Warning, this kernel has no ramdisk support)\n");
1423 #else
1424 /* rd_doload is 2 for a dual initrd/ramload setup */
1425 if(rd_doload==2)
1426 rd_load_secondary();
1427 else
1428 #endif
1430 printk(KERN_NOTICE "VFS: Insert root floppy and press ENTER\n");
1431 wait_for_keypress();
1434 #endif
1436 devfs_make_root (root_device_name);
1437 handle = devfs_find_handle (NULL, ROOT_DEVICE_NAME, 0,
1438 MAJOR (ROOT_DEV), MINOR (ROOT_DEV),
1439 DEVFS_SPECIAL_BLK, 1);
1440 if (handle) /* Sigh: bd*() functions only paper over the cracks */
1442 unsigned major, minor;
1444 devfs_get_maj_min (handle, &major, &minor);
1445 ROOT_DEV = MKDEV (major, minor);
1449 * Probably pure paranoia, but I'm less than happy about delving into
1450 * devfs crap and checking it right now. Later.
1452 if (!ROOT_DEV)
1453 panic("I have no root and I want to scream");
1455 bdev = bdget(kdev_t_to_nr(ROOT_DEV));
1456 if (!bdev)
1457 panic(__FUNCTION__ ": unable to allocate root device");
1458 bdev->bd_op = devfs_get_ops (handle);
1459 path_start = devfs_generate_path (handle, path + 5, sizeof (path) - 5);
1460 mode = FMODE_READ;
1461 if (!(root_mountflags & MS_RDONLY))
1462 mode |= FMODE_WRITE;
1463 retval = blkdev_get(bdev, mode, 0, BDEV_FS);
1464 if (retval == -EROFS) {
1465 root_mountflags |= MS_RDONLY;
1466 retval = blkdev_get(bdev, FMODE_READ, 0, BDEV_FS);
1468 if (retval) {
1470 * Allow the user to distinguish between failed open
1471 * and bad superblock on root device.
1473 printk ("VFS: Cannot open root device \"%s\" or %s\n",
1474 root_device_name, kdevname (ROOT_DEV));
1475 printk ("Please append a correct \"root=\" boot option\n");
1476 panic("VFS: Unable to mount root fs on %s",
1477 kdevname(ROOT_DEV));
1480 check_disk_change(ROOT_DEV);
1481 sb = get_super(ROOT_DEV);
1482 if (sb) {
1483 fs_type = sb->s_type;
1484 goto mount_it;
1487 read_lock(&file_systems_lock);
1488 for (fs_type = file_systems ; fs_type ; fs_type = fs_type->next) {
1489 if (!(fs_type->fs_flags & FS_REQUIRES_DEV))
1490 continue;
1491 if (!try_inc_mod_count(fs_type->owner))
1492 continue;
1493 read_unlock(&file_systems_lock);
1494 sb = read_super(ROOT_DEV,bdev,fs_type,root_mountflags,NULL,1);
1495 if (sb)
1496 goto mount_it;
1497 read_lock(&file_systems_lock);
1498 put_filesystem(fs_type);
1500 read_unlock(&file_systems_lock);
1501 panic("VFS: Unable to mount root fs on %s", kdevname(ROOT_DEV));
1503 mount_it:
1504 printk ("VFS: Mounted root (%s filesystem)%s.\n",
1505 fs_type->name,
1506 (sb->s_flags & MS_RDONLY) ? " readonly" : "");
1507 if (path_start >= 0) {
1508 devfs_mk_symlink (NULL,
1509 "root", 0, DEVFS_FL_DEFAULT,
1510 path + 5 + path_start, 0,
1511 NULL, NULL);
1512 memcpy (path + path_start, "/dev/", 5);
1513 vfsmnt = add_vfsmnt(NULL, sb->s_root, path + path_start);
1515 else
1516 vfsmnt = add_vfsmnt(NULL, sb->s_root, "/dev/root");
1517 /* FIXME: if something will try to umount us right now... */
1518 if (vfsmnt) {
1519 set_fs_root(current->fs, vfsmnt, sb->s_root);
1520 set_fs_pwd(current->fs, vfsmnt, sb->s_root);
1521 if (bdev)
1522 bdput(bdev); /* sb holds a reference */
1523 return;
1525 panic("VFS: add_vfsmnt failed for root fs");
1529 static void chroot_fs_refs(struct dentry *old_root,
1530 struct vfsmount *old_rootmnt,
1531 struct dentry *new_root,
1532 struct vfsmount *new_rootmnt)
1534 struct task_struct *p;
1536 read_lock(&tasklist_lock);
1537 for_each_task(p) {
1538 /* FIXME - unprotected usage of ->fs + (harmless) race */
1539 if (!p->fs) continue;
1540 if (p->fs->root == old_root && p->fs->rootmnt == old_rootmnt)
1541 set_fs_root(p->fs, new_rootmnt, new_root);
1542 if (p->fs->pwd == old_root && p->fs->pwdmnt == old_rootmnt)
1543 set_fs_pwd(p->fs, new_rootmnt, new_root);
1545 read_unlock(&tasklist_lock);
1549 * Moves the current root to put_root, and sets root/cwd of all processes
1550 * which had them on the old root to new_root.
1552 * Note:
1553 * - we don't move root/cwd if they are not at the root (reason: if something
1554 * cared enough to change them, it's probably wrong to force them elsewhere)
1555 * - it's okay to pick a root that isn't the root of a file system, e.g.
1556 * /nfs/my_root where /nfs is the mount point. Better avoid creating
1557 * unreachable mount points this way, though.
1560 asmlinkage long sys_pivot_root(const char *new_root, const char *put_old)
1562 struct dentry *root;
1563 struct vfsmount *root_mnt;
1564 struct vfsmount *tmp;
1565 struct nameidata new_nd, old_nd;
1566 char *name;
1567 int error;
1569 if (!capable(CAP_SYS_ADMIN))
1570 return -EPERM;
1572 lock_kernel();
1574 name = getname(new_root);
1575 error = PTR_ERR(name);
1576 if (IS_ERR(name))
1577 goto out0;
1578 error = 0;
1579 if (path_init(name, LOOKUP_POSITIVE|LOOKUP_FOLLOW|LOOKUP_DIRECTORY, &new_nd))
1580 error = path_walk(name, &new_nd);
1581 putname(name);
1582 if (error)
1583 goto out0;
1585 name = getname(put_old);
1586 error = PTR_ERR(name);
1587 if (IS_ERR(name))
1588 goto out0;
1589 error = 0;
1590 if (path_init(name, LOOKUP_POSITIVE|LOOKUP_FOLLOW|LOOKUP_DIRECTORY, &old_nd))
1591 error = path_walk(name, &old_nd);
1592 putname(name);
1593 if (error)
1594 goto out1;
1596 root_mnt = mntget(current->fs->rootmnt);
1597 root = dget(current->fs->root);
1598 down(&mount_sem);
1599 down(&old_nd.dentry->d_inode->i_zombie);
1600 error = -ENOENT;
1601 if (IS_DEADDIR(new_nd.dentry->d_inode))
1602 goto out2;
1603 if (d_unhashed(new_nd.dentry) && !IS_ROOT(new_nd.dentry))
1604 goto out2;
1605 if (d_unhashed(old_nd.dentry) && !IS_ROOT(old_nd.dentry))
1606 goto out2;
1607 error = -EBUSY;
1608 if (new_nd.mnt == root_mnt || old_nd.mnt == root_mnt)
1609 goto out2; /* loop */
1610 error = -EINVAL;
1611 tmp = old_nd.mnt; /* make sure we can reach put_old from new_root */
1612 if (tmp != new_nd.mnt) {
1613 for (;;) {
1614 if (tmp->mnt_parent == tmp)
1615 goto out2;
1616 if (tmp->mnt_parent == new_nd.mnt)
1617 break;
1618 tmp = tmp->mnt_parent;
1620 if (!is_subdir(tmp->mnt_mountpoint, new_nd.dentry))
1621 goto out2;
1622 } else if (!is_subdir(old_nd.dentry, new_nd.dentry))
1623 goto out2;
1625 move_vfsmnt(new_nd.mnt, new_nd.dentry, NULL, NULL);
1626 move_vfsmnt(root_mnt, old_nd.dentry, old_nd.mnt, NULL);
1627 chroot_fs_refs(root,root_mnt,new_nd.dentry,new_nd.mnt);
1628 error = 0;
1629 out2:
1630 up(&old_nd.dentry->d_inode->i_zombie);
1631 up(&mount_sem);
1632 dput(root);
1633 mntput(root_mnt);
1634 path_release(&old_nd);
1635 out1:
1636 path_release(&new_nd);
1637 out0:
1638 unlock_kernel();
1639 return error;
1643 #ifdef CONFIG_BLK_DEV_INITRD
1645 int __init change_root(kdev_t new_root_dev,const char *put_old)
1647 kdev_t old_root_dev = ROOT_DEV;
1648 struct vfsmount *old_rootmnt;
1649 struct nameidata devfs_nd, nd;
1650 int error = 0;
1652 old_rootmnt = mntget(current->fs->rootmnt);
1653 /* First unmount devfs if mounted */
1654 if (path_init("/dev", LOOKUP_FOLLOW|LOOKUP_POSITIVE, &devfs_nd))
1655 error = path_walk("/dev", &devfs_nd);
1656 if (!error) {
1657 struct super_block *sb = devfs_nd.dentry->d_inode->i_sb;
1659 if (devfs_nd.mnt->mnt_sb->s_magic == DEVFS_SUPER_MAGIC &&
1660 devfs_nd.dentry == devfs_nd.mnt->mnt_root) {
1661 dput(devfs_nd.dentry);
1662 down(&mount_sem);
1663 /* puts devfs_nd.mnt */
1664 do_umount(devfs_nd.mnt, 0, 0);
1665 up(&mount_sem);
1666 } else
1667 path_release(&devfs_nd);
1669 ROOT_DEV = new_root_dev;
1670 mount_root();
1671 #if 1
1672 shrink_dcache();
1673 printk("change_root: old root has d_count=%d\n",
1674 old_rootmnt->mnt_root->d_count);
1675 #endif
1676 mount_devfs_fs ();
1678 * Get the new mount directory
1680 error = 0;
1681 if (path_init(put_old, LOOKUP_FOLLOW|LOOKUP_POSITIVE|LOOKUP_DIRECTORY, &nd))
1682 error = path_walk(put_old, &nd);
1683 if (error) {
1684 int blivet;
1686 printk(KERN_NOTICE "Trying to unmount old root ... ");
1687 blivet = do_umount(old_rootmnt, 1, 0);
1688 if (!blivet) {
1689 printk("okay\n");
1690 return 0;
1692 printk(KERN_ERR "error %ld\n",blivet);
1693 return error;
1695 /* FIXME: we should hold i_zombie on nd.dentry */
1696 move_vfsmnt(old_rootmnt, nd.dentry, nd.mnt, "/dev/root.old");
1697 mntput(old_rootmnt);
1698 path_release(&nd);
1699 return 0;
1702 #endif