4 * Copyright (C) 1991, 1992 Linus Torvalds
6 * super.c contains code to handle: - mount structures
8 * - filesystem drivers list
10 * - umount 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>
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
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 */
63 int max_super_blocks
= NR_SUPER
;
64 LIST_HEAD(super_blocks
);
67 * Handling of filesystem drivers list.
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
)
86 __MOD_INC_USE_COUNT(fs
->owner
);
89 static void put_filesystem(struct file_system_type
*fs
)
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)
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
117 int register_filesystem(struct file_system_type
* fs
)
120 struct file_system_type
** p
;
126 write_lock(&file_systems_lock
);
127 p
= find_filesystem(fs
->name
);
132 write_unlock(&file_systems_lock
);
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
);
158 write_unlock(&file_systems_lock
);
163 write_unlock(&file_systems_lock
);
167 static int fs_index(const char * __name
)
169 struct file_system_type
* tmp
;
173 name
= getname(__name
);
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) {
186 read_unlock(&file_systems_lock
);
191 static int fs_name(unsigned int index
, char * buf
)
193 struct file_system_type
* tmp
;
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
))
200 read_unlock(&file_systems_lock
);
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;
211 static int fs_maxindex(void)
213 struct file_system_type
* tmp
;
216 read_lock(&file_systems_lock
);
217 for (tmp
= file_systems
, index
= 0 ; tmp
; tmp
= tmp
->next
, index
++)
219 read_unlock(&file_systems_lock
);
224 * Whee.. Weird sysv syscall.
226 asmlinkage
long sys_sysfs(int option
, unsigned long arg1
, unsigned long arg2
)
228 int retval
= -EINVAL
;
232 retval
= fs_index((const char *) arg1
);
236 retval
= fs_name(arg1
, (char *) arg2
);
240 retval
= fs_maxindex();
246 int get_filesystem_list(char * buf
)
249 struct file_system_type
* tmp
;
251 read_lock(&file_systems_lock
);
253 while (tmp
&& len
< PAGE_SIZE
- 80) {
254 len
+= sprintf(buf
+len
, "%s\t%s\n",
255 (tmp
->fs_flags
& FS_REQUIRES_DEV
) ? "" : "nodev",
259 read_unlock(&file_systems_lock
);
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
))
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
))
277 read_unlock(&file_systems_lock
);
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
295 * Potential reason for failure (aside of trivial lack of memory) is a
296 * deleted mountpoint. Caller must hold ->i_zombie on mountpoint
300 static struct vfsmount
*add_vfsmnt(struct nameidata
*nd
,
302 const char *dev_name
)
304 struct vfsmount
*mnt
;
305 struct super_block
*sb
= root
->d_inode
->i_sb
;
308 mnt
= kmalloc(sizeof(struct vfsmount
), GFP_KERNEL
);
311 memset(mnt
, 0, sizeof(struct vfsmount
));
313 /* It may be NULL, but who cares? */
315 name
= kmalloc(strlen(dev_name
)+1, GFP_KERNEL
);
317 strcpy(name
, dev_name
);
318 mnt
->mnt_devname
= name
;
321 mnt
->mnt_owner
= current
->uid
;
322 atomic_set(&mnt
->mnt_count
,1);
325 if (nd
&& !IS_ROOT(nd
->dentry
) && d_unhashed(nd
->dentry
))
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
;
332 list_add(&mnt
->mnt_child
, &nd
->mnt
->mnt_mounts
);
333 list_add(&mnt
->mnt_clash
, &nd
->dentry
->d_vfsmnt
);
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
);
344 kfree(mnt
->mnt_devname
);
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
;
359 new_devname
= kmalloc(strlen(dev_name
)+1, GFP_KERNEL
);
361 strcpy(new_devname
, dev_name
);
364 old_mountpoint
= mnt
->mnt_mountpoint
;
365 old_parent
= mnt
->mnt_parent
;
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
);
379 list_add(&mnt
->mnt_child
, &parent
->mnt_mounts
);
380 list_add(&mnt
->mnt_clash
, &mountpoint
->d_vfsmnt
);
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
)
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
);
405 kfree(mnt
->mnt_devname
);
409 static struct proc_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" },
426 static struct proc_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" },
441 int get_filesystem_info( char *buf
)
444 struct proc_fs_info
*fs_infop
;
445 struct proc_nfs_info
*nfs_infop
;
446 struct nfs_server
*nfss
;
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;
453 struct vfsmount
*tmp
= list_entry(p
, struct vfsmount
, mnt_list
);
454 path
= d_path(tmp
->mnt_root
, tmp
, buffer
, PAGE_SIZE
);
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
);
475 if (nfss
->timeo
!= 7*HZ
/10) {
476 len
+= sprintf(buf
+len
, ",timeo=%d",
479 if (nfss
->retrans
!= 3) {
480 len
+= sprintf(buf
+len
, ",retrans=%d",
484 if (nfss
->acregmin
!= 3*HZ
) {
485 len
+= sprintf(buf
+len
, ",acregmin=%d",
488 if (nfss
->acregmax
!= 60*HZ
) {
489 len
+= sprintf(buf
+len
, ",acregmax=%d",
492 if (nfss
->acdirmin
!= 30*HZ
) {
493 len
+= sprintf(buf
+len
, ",acdirmin=%d",
496 if (nfss
->acdirmax
!= 60*HZ
) {
497 len
+= sprintf(buf
+len
, ",acdirmax=%d",
500 for (nfs_infop
= nfs_info
; nfs_infop
->flag
; nfs_infop
++) {
502 if (nfss
->flags
& nfs_infop
->flag
)
503 str
= nfs_infop
->str
;
505 str
= nfs_infop
->nostr
;
506 strcpy(buf
+ len
, str
);
509 len
+= sprintf(buf
+len
, ",addr=%s",
512 len
+= sprintf( buf
+ len
, " 0 0\n" );
515 free_page((unsigned long) buffer
);
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
);
533 set_current_state(TASK_UNINTERRUPTIBLE
);
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
)) {
556 if (dev
&& sb
->s_dev
!= dev
)
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
);
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
;
583 s
= sb_entry(super_blocks
.next
);
584 while (s
!= sb_entry(&super_blocks
))
585 if (s
->s_dev
== dev
) {
591 s
= sb_entry(s
->s_list
.next
);
595 asmlinkage
long sys_ustat(dev_t dev
, struct ustat
* ubuf
)
597 struct super_block
*s
;
603 s
= get_super(to_kdev_t(dev
));
606 err
= vfs_statfs(s
, &sbuf
);
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;
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
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
)) {
640 printk("VFS: empty superblock %p locked!\n", s
);
642 /* Need a new one... */
643 if (nr_super_blocks
>= max_super_blocks
)
645 s
= kmalloc(sizeof(struct super_block
), GFP_USER
);
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
);
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();
670 sema_init(&s
->s_vfs_rename_sem
,1);
671 sema_init(&s
->s_nfsd_free_path_sem
,1);
673 sema_init(&s
->s_dquot
.dqio_sem
, 1);
674 sema_init(&s
->s_dquot
.dqoff_sem
, 1);
675 s
->s_dquot
.flags
= 0;
677 if (!type
->read_super(s
, data
, silent
))
680 /* tell bdcache that we are going to keep this one */
682 atomic_inc(&bdev
->bd_count
);
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)
705 for (i
= 1; i
< 256; i
++) {
706 if (!test_and_set_bit(i
,unnamed_dev_in_use
))
707 return MKDEV(UNNAMED_MAJOR
, i
);
712 void put_unnamed_dev(kdev_t dev
)
714 if (!dev
|| MAJOR(dev
) != UNNAMED_MAJOR
)
716 if (test_and_clear_bit(MINOR(dev
), unnamed_dev_in_use
))
718 printk("VFS: put_unnamed_dev: freeing unused device %s\n",
722 static struct super_block
*get_sb_bdev(struct file_system_type
*fs_type
,
723 char *dev_name
, int flags
, void * data
)
726 struct block_device
*bdev
;
727 struct block_device_operations
*bdops
;
728 struct super_block
* sb
;
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
);
738 return ERR_PTR(error
);
739 inode
= nd
.dentry
->d_inode
;
741 if (!S_ISBLK(inode
->i_mode
))
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 */
751 dev
= to_kdev_t(bdev
->bd_dev
);
754 if (fs_type
== sb
->s_type
) {
759 mode_t mode
= FMODE_READ
; /* we always need it ;-) */
760 if (!(flags
& MS_RDONLY
))
762 error
= blkdev_get(bdev
, mode
, 0, BDEV_FS
);
765 check_disk_change(dev
);
767 if (!(flags
& MS_RDONLY
) && is_read_only(dev
))
770 sb
= read_super(dev
, bdev
, fs_type
, flags
, data
, 0);
772 get_filesystem(fs_type
);
777 blkdev_put(bdev
, BDEV_FS
);
782 return ERR_PTR(error
);
785 static struct super_block
*get_sb_nodev(struct file_system_type
*fs_type
,
786 int flags
, void * data
)
791 dev
= get_unnamed_dev();
793 struct super_block
* sb
;
795 sb
= read_super(dev
, NULL
, fs_type
, flags
, data
, 0);
797 get_filesystem(fs_type
);
800 put_unnamed_dev(dev
);
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.
815 sb
= fs_type
->kern_mnt
->mnt_sb
;
818 get_filesystem(fs_type
);
819 do_remount_sb(sb
, flags
, data
);
823 static struct block_device
*kill_super(struct super_block
*sb
, int umount_root
)
825 struct block_device
*bdev
;
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");
844 sb
->s_dev
= 0; /* Free the superblock */
847 put_filesystem(sb
->s_type
);
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
);
857 blkdev_put(bdev
, BDEV_FS
);
860 put_unnamed_dev(dev
);
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
)
873 if (!(flags
& MS_RDONLY
) && sb
->s_dev
&& is_read_only(sb
->s_dev
))
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
))
880 if (sb
->s_op
&& sb
->s_op
->remount_fs
) {
882 retval
= sb
->s_op
->remount_fs(sb
, &flags
, data
);
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()))
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
;
904 return ERR_PTR(-EMFILE
);
905 sb
= read_super(dev
, NULL
, type
, 0, NULL
, 0);
907 put_unnamed_dev(dev
);
908 return ERR_PTR(-EINVAL
);
910 mnt
= add_vfsmnt(NULL
, sb
->s_root
, "none");
913 return ERR_PTR(-ENOMEM
);
915 type
->kern_mnt
= 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
;
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)
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
) {
957 * Special case for "unmounting" root ...
958 * we just try to remount it readonly.
961 if (!(sb
->s_flags
& MS_RDONLY
))
962 retval
= do_remount_sb(sb
, MS_RDONLY
, 0);
966 if (atomic_read(&mnt
->mnt_count
) > 2) {
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 */
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.
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
1008 shrink_dcache_sb(sb
);
1009 fsync_dev(sb
->s_dev
);
1011 if (sb
->s_root
->d_inode
->i_state
) {
1016 /* Something might grab it again - redo checks */
1018 if (atomic_read(&mnt
->mnt_count
) > 2) {
1023 /* OK, that's the point of no return */
1027 kill_super(sb
, umount_root
);
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
;
1046 kname
= getname(name
);
1047 retval
= PTR_ERR(kname
);
1051 if (path_init(kname
, LOOKUP_POSITIVE
|LOOKUP_FOLLOW
, &nd
))
1052 retval
= path_walk(kname
, &nd
);
1057 if (nd
.dentry
!=nd
.mnt
->mnt_root
)
1061 if (!capable(CAP_SYS_ADMIN
) && current
->uid
!=nd
.mnt
->mnt_owner
)
1067 retval
= do_umount(nd
.mnt
, 0, flags
);
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
))
1092 if (S_ISLNK(nd
->dentry
->d_inode
->i_mode
))
1094 if (nd
->dentry
->d_inode
->i_mode
& S_ISVTX
) {
1095 if (current
->uid
!= nd
->dentry
->d_inode
->i_uid
)
1098 if (permission(nd
->dentry
->d_inode
, MAY_WRITE
))
1105 * do loopback mount.
1107 static int do_loopback(char *old_name
, char *new_name
)
1109 struct nameidata old_nd
, new_nd
;
1111 if (!old_name
|| !*old_name
)
1113 if (path_init(old_name
, LOOKUP_POSITIVE
, &old_nd
))
1114 err
= path_walk(old_name
, &old_nd
);
1117 if (path_init(new_name
, LOOKUP_POSITIVE
, &new_nd
))
1118 err
= path_walk(new_name
, &new_nd
);
1121 err
= mount_is_safe(&new_nd
);
1125 if (S_ISDIR(new_nd
.dentry
->d_inode
->i_mode
) !=
1126 S_ISDIR(old_nd
.dentry
->d_inode
->i_mode
))
1130 if (old_nd
.mnt
->mnt_sb
->s_type
->fs_flags
& FS_SINGLE
)
1131 get_filesystem(old_nd
.mnt
->mnt_sb
->s_type
);
1135 down(&new_nd
.dentry
->d_inode
->i_zombie
);
1136 if (IS_DEADDIR(new_nd
.dentry
->d_inode
))
1138 else if (add_vfsmnt(&new_nd
, old_nd
.dentry
, old_nd
.mnt
->mnt_devname
))
1140 up(&new_nd
.dentry
->d_inode
->i_zombie
);
1142 if (err
&& old_nd
.mnt
->mnt_sb
->s_type
->fs_flags
& FS_SINGLE
)
1143 put_filesystem(old_nd
.mnt
->mnt_sb
->s_type
);
1145 path_release(&new_nd
);
1147 path_release(&old_nd
);
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
;
1163 if (!capable(CAP_SYS_ADMIN
))
1166 if (path_init(dir
, LOOKUP_FOLLOW
|LOOKUP_POSITIVE
, &nd
))
1167 retval
= path_walk(dir
, &nd
);
1169 struct super_block
* sb
= nd
.dentry
->d_inode
->i_sb
;
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
);
1189 static int copy_mount_options (const void *data
, unsigned long *where
)
1199 if (!(page
= __get_free_page(GFP_KERNEL
)))
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
)
1211 i
= size
- copy_from_user((void *)page
, data
, size
);
1217 memset((char *)page
+ i
, 0, PAGE_SIZE
- i
);
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
;
1244 unsigned long flags
= 0;
1246 /* Basic sanity checks */
1248 if (!dir_name
|| !*dir_name
|| !memchr(dir_name
, 0, PAGE_SIZE
))
1250 if (dev_name
&& !memchr(dev_name
, 0, PAGE_SIZE
))
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
))
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
))
1276 /* ... filesystem driver... */
1277 fstype
= get_fs_type(type_page
);
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
);
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
);
1295 sb
= get_sb_nodev(fstype
, flags
, data_page
);
1297 retval
= PTR_ERR(sb
);
1301 /* Something was mounted here while we slept */
1302 while(d_mountpoint(nd
.dentry
) && follow_down(&nd
.mnt
, &nd
.dentry
))
1305 if (!nd
.dentry
->d_inode
)
1307 down(&nd
.dentry
->d_inode
->i_zombie
);
1308 if (!IS_DEADDIR(nd
.dentry
->d_inode
)) {
1310 mnt
= add_vfsmnt(&nd
, sb
->s_root
, dev_name
);
1312 up(&nd
.dentry
->d_inode
->i_zombie
);
1321 put_filesystem(fstype
);
1325 if (list_empty(&sb
->s_mounts
))
1330 asmlinkage
long sys_mount(char * dev_name
, char * dir_name
, char * type
,
1331 unsigned long new_flags
, void * data
)
1334 unsigned long data_page
;
1335 unsigned long type_page
;
1336 unsigned long dev_page
;
1339 retval
= copy_mount_options (type
, &type_page
);
1343 dir_page
= getname(dir_name
);
1344 retval
= PTR_ERR(dir_page
);
1345 if (IS_ERR(dir_page
))
1348 retval
= copy_mount_options (dev_name
, &dev_page
);
1351 retval
= copy_mount_options (data
, &data_page
);
1354 retval
= do_mount((char*)dev_page
,dir_page
,(char*)type_page
,
1355 new_flags
, (void*)data_page
);
1357 free_page(data_page
);
1359 free_page(dev_page
);
1363 free_page(type_page
);
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
;
1377 int path_start
= -1;
1379 #ifdef CONFIG_ROOT_NFS
1381 if (MAJOR(ROOT_DEV
) != UNNAMED_MAJOR
)
1383 fs_type
= get_fs_type("nfs");
1386 ROOT_DEV
= get_unnamed_dev();
1389 * Your /linuxrc sucks worse than MSExchange - that's the
1390 * only way you could run out of anon devices at that point.
1393 data
= nfs_root_data();
1396 sb
= read_super(ROOT_DEV
, NULL
, fs_type
, root_mountflags
, data
, 1);
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).
1405 put_unnamed_dev(ROOT_DEV
);
1407 put_filesystem(fs_type
);
1409 printk(KERN_ERR
"VFS: Unable to mount root fs via NFS, trying floppy.\n");
1410 ROOT_DEV
= MKDEV(FLOPPY_MAJOR
, 0);
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);
1421 #ifndef CONFIG_BLK_DEV_RAM
1422 printk(KERN_NOTICE
"(Warning, this kernel has no ramdisk support)\n");
1424 /* rd_doload is 2 for a dual initrd/ramload setup */
1426 rd_load_secondary();
1430 printk(KERN_NOTICE
"VFS: Insert root floppy and press ENTER\n");
1431 wait_for_keypress();
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.
1453 panic("I have no root and I want to scream");
1455 bdev
= bdget(kdev_t_to_nr(ROOT_DEV
));
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);
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
);
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
);
1483 fs_type
= sb
->s_type
;
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
))
1491 if (!try_inc_mod_count(fs_type
->owner
))
1493 read_unlock(&file_systems_lock
);
1494 sb
= read_super(ROOT_DEV
,bdev
,fs_type
,root_mountflags
,NULL
,1);
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
));
1504 printk ("VFS: Mounted root (%s filesystem)%s.\n",
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,
1512 memcpy (path
+ path_start
, "/dev/", 5);
1513 vfsmnt
= add_vfsmnt(NULL
, sb
->s_root
, path
+ path_start
);
1516 vfsmnt
= add_vfsmnt(NULL
, sb
->s_root
, "/dev/root");
1517 /* FIXME: if something will try to umount us right now... */
1519 set_fs_root(current
->fs
, vfsmnt
, sb
->s_root
);
1520 set_fs_pwd(current
->fs
, vfsmnt
, sb
->s_root
);
1522 bdput(bdev
); /* sb holds a reference */
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
);
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.
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
;
1569 if (!capable(CAP_SYS_ADMIN
))
1574 name
= getname(new_root
);
1575 error
= PTR_ERR(name
);
1579 if (path_init(name
, LOOKUP_POSITIVE
|LOOKUP_FOLLOW
|LOOKUP_DIRECTORY
, &new_nd
))
1580 error
= path_walk(name
, &new_nd
);
1585 name
= getname(put_old
);
1586 error
= PTR_ERR(name
);
1590 if (path_init(name
, LOOKUP_POSITIVE
|LOOKUP_FOLLOW
|LOOKUP_DIRECTORY
, &old_nd
))
1591 error
= path_walk(name
, &old_nd
);
1596 root_mnt
= mntget(current
->fs
->rootmnt
);
1597 root
= dget(current
->fs
->root
);
1599 down(&old_nd
.dentry
->d_inode
->i_zombie
);
1601 if (IS_DEADDIR(new_nd
.dentry
->d_inode
))
1603 if (d_unhashed(new_nd
.dentry
) && !IS_ROOT(new_nd
.dentry
))
1605 if (d_unhashed(old_nd
.dentry
) && !IS_ROOT(old_nd
.dentry
))
1608 if (new_nd
.mnt
== root_mnt
|| old_nd
.mnt
== root_mnt
)
1609 goto out2
; /* loop */
1611 tmp
= old_nd
.mnt
; /* make sure we can reach put_old from new_root */
1612 if (tmp
!= new_nd
.mnt
) {
1614 if (tmp
->mnt_parent
== tmp
)
1616 if (tmp
->mnt_parent
== new_nd
.mnt
)
1618 tmp
= tmp
->mnt_parent
;
1620 if (!is_subdir(tmp
->mnt_mountpoint
, new_nd
.dentry
))
1622 } else if (!is_subdir(old_nd
.dentry
, new_nd
.dentry
))
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
);
1630 up(&old_nd
.dentry
->d_inode
->i_zombie
);
1634 path_release(&old_nd
);
1636 path_release(&new_nd
);
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
;
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
);
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
);
1663 /* puts devfs_nd.mnt */
1664 do_umount(devfs_nd
.mnt
, 0, 0);
1667 path_release(&devfs_nd
);
1669 ROOT_DEV
= new_root_dev
;
1673 printk("change_root: old root has d_count=%d\n",
1674 old_rootmnt
->mnt_root
->d_count
);
1678 * Get the new mount directory
1681 if (path_init(put_old
, LOOKUP_FOLLOW
|LOOKUP_POSITIVE
|LOOKUP_DIRECTORY
, &nd
))
1682 error
= path_walk(put_old
, &nd
);
1686 printk(KERN_NOTICE
"Trying to unmount old root ... ");
1687 blivet
= do_umount(old_rootmnt
, 1, 0);
1692 printk(KERN_ERR
"error %ld\n",blivet
);
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
);