Home | History | Annotate | Line # | Download | only in mfs
mfs_vfsops.c revision 1.1
      1  1.1  mycroft /*
      2  1.1  mycroft  * Copyright (c) 1989, 1990, 1993, 1994
      3  1.1  mycroft  *	The Regents of the University of California.  All rights reserved.
      4  1.1  mycroft  *
      5  1.1  mycroft  * Redistribution and use in source and binary forms, with or without
      6  1.1  mycroft  * modification, are permitted provided that the following conditions
      7  1.1  mycroft  * are met:
      8  1.1  mycroft  * 1. Redistributions of source code must retain the above copyright
      9  1.1  mycroft  *    notice, this list of conditions and the following disclaimer.
     10  1.1  mycroft  * 2. Redistributions in binary form must reproduce the above copyright
     11  1.1  mycroft  *    notice, this list of conditions and the following disclaimer in the
     12  1.1  mycroft  *    documentation and/or other materials provided with the distribution.
     13  1.1  mycroft  * 3. All advertising materials mentioning features or use of this software
     14  1.1  mycroft  *    must display the following acknowledgement:
     15  1.1  mycroft  *	This product includes software developed by the University of
     16  1.1  mycroft  *	California, Berkeley and its contributors.
     17  1.1  mycroft  * 4. Neither the name of the University nor the names of its contributors
     18  1.1  mycroft  *    may be used to endorse or promote products derived from this software
     19  1.1  mycroft  *    without specific prior written permission.
     20  1.1  mycroft  *
     21  1.1  mycroft  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     22  1.1  mycroft  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23  1.1  mycroft  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  1.1  mycroft  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     25  1.1  mycroft  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  1.1  mycroft  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27  1.1  mycroft  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28  1.1  mycroft  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  1.1  mycroft  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  1.1  mycroft  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  1.1  mycroft  * SUCH DAMAGE.
     32  1.1  mycroft  *
     33  1.1  mycroft  *	from: @(#)mfs_vfsops.c	8.4 (Berkeley) 4/16/94
     34  1.1  mycroft  *	$Id: mfs_vfsops.c,v 1.1 1994/06/08 11:42:55 mycroft Exp $
     35  1.1  mycroft  */
     36  1.1  mycroft 
     37  1.1  mycroft #include <sys/param.h>
     38  1.1  mycroft #include <sys/systm.h>
     39  1.1  mycroft #include <sys/time.h>
     40  1.1  mycroft #include <sys/kernel.h>
     41  1.1  mycroft #include <sys/proc.h>
     42  1.1  mycroft #include <sys/buf.h>
     43  1.1  mycroft #include <sys/mount.h>
     44  1.1  mycroft #include <sys/signalvar.h>
     45  1.1  mycroft #include <sys/vnode.h>
     46  1.1  mycroft #include <sys/malloc.h>
     47  1.1  mycroft 
     48  1.1  mycroft #include <ufs/ufs/quota.h>
     49  1.1  mycroft #include <ufs/ufs/inode.h>
     50  1.1  mycroft #include <ufs/ufs/ufsmount.h>
     51  1.1  mycroft #include <ufs/ufs/ufs_extern.h>
     52  1.1  mycroft 
     53  1.1  mycroft #include <ufs/ffs/fs.h>
     54  1.1  mycroft #include <ufs/ffs/ffs_extern.h>
     55  1.1  mycroft 
     56  1.1  mycroft #include <ufs/mfs/mfsnode.h>
     57  1.1  mycroft #include <ufs/mfs/mfs_extern.h>
     58  1.1  mycroft 
     59  1.1  mycroft caddr_t	mfs_rootbase;	/* address of mini-root in kernel virtual memory */
     60  1.1  mycroft u_long	mfs_rootsize;	/* size of mini-root in bytes */
     61  1.1  mycroft 
     62  1.1  mycroft static	int mfs_minor;	/* used for building internal dev_t */
     63  1.1  mycroft 
     64  1.1  mycroft extern int (**mfs_vnodeop_p)();
     65  1.1  mycroft 
     66  1.1  mycroft /*
     67  1.1  mycroft  * mfs vfs operations.
     68  1.1  mycroft  */
     69  1.1  mycroft struct vfsops mfs_vfsops = {
     70  1.1  mycroft 	MOUNT_MFS,
     71  1.1  mycroft 	mfs_mount,
     72  1.1  mycroft 	mfs_start,
     73  1.1  mycroft 	ffs_unmount,
     74  1.1  mycroft 	ufs_root,
     75  1.1  mycroft 	ufs_quotactl,
     76  1.1  mycroft 	mfs_statfs,
     77  1.1  mycroft 	ffs_sync,
     78  1.1  mycroft 	ffs_vget,
     79  1.1  mycroft 	ffs_fhtovp,
     80  1.1  mycroft 	ffs_vptofh,
     81  1.1  mycroft 	mfs_init,
     82  1.1  mycroft };
     83  1.1  mycroft 
     84  1.1  mycroft /*
     85  1.1  mycroft  * Called by main() when mfs is going to be mounted as root.
     86  1.1  mycroft  *
     87  1.1  mycroft  * Name is updated by mount(8) after booting.
     88  1.1  mycroft  */
     89  1.1  mycroft #define ROOTNAME	"mfs_root"
     90  1.1  mycroft 
     91  1.1  mycroft mfs_mountroot()
     92  1.1  mycroft {
     93  1.1  mycroft 	extern struct vnode *rootvp;
     94  1.1  mycroft 	register struct fs *fs;
     95  1.1  mycroft 	register struct mount *mp;
     96  1.1  mycroft 	struct proc *p = curproc;	/* XXX */
     97  1.1  mycroft 	struct ufsmount *ump;
     98  1.1  mycroft 	struct mfsnode *mfsp;
     99  1.1  mycroft 	u_int size;
    100  1.1  mycroft 	int error;
    101  1.1  mycroft 
    102  1.1  mycroft 	/*
    103  1.1  mycroft 	 * Get vnodes for swapdev and rootdev.
    104  1.1  mycroft 	 */
    105  1.1  mycroft 	if (bdevvp(swapdev, &swapdev_vp) || bdevvp(rootdev, &rootvp))
    106  1.1  mycroft 		panic("mfs_mountroot: can't setup bdevvp's");
    107  1.1  mycroft 
    108  1.1  mycroft 	mp = malloc((u_long)sizeof(struct mount), M_MOUNT, M_WAITOK);
    109  1.1  mycroft 	bzero((char *)mp, (u_long)sizeof(struct mount));
    110  1.1  mycroft 	mp->mnt_op = &mfs_vfsops;
    111  1.1  mycroft 	mp->mnt_flag = MNT_RDONLY;
    112  1.1  mycroft 	mfsp = malloc(sizeof *mfsp, M_MFSNODE, M_WAITOK);
    113  1.1  mycroft 	rootvp->v_data = mfsp;
    114  1.1  mycroft 	rootvp->v_op = mfs_vnodeop_p;
    115  1.1  mycroft 	rootvp->v_tag = VT_MFS;
    116  1.1  mycroft 	mfsp->mfs_baseoff = mfs_rootbase;
    117  1.1  mycroft 	mfsp->mfs_size = mfs_rootsize;
    118  1.1  mycroft 	mfsp->mfs_vnode = rootvp;
    119  1.1  mycroft 	mfsp->mfs_pid = p->p_pid;
    120  1.1  mycroft 	mfsp->mfs_buflist = (struct buf *)0;
    121  1.1  mycroft 	if (error = ffs_mountfs(rootvp, mp, p)) {
    122  1.1  mycroft 		free(mp, M_MOUNT);
    123  1.1  mycroft 		free(mfsp, M_MFSNODE);
    124  1.1  mycroft 		return (error);
    125  1.1  mycroft 	}
    126  1.1  mycroft 	if (error = vfs_lock(mp)) {
    127  1.1  mycroft 		(void)ffs_unmount(mp, 0, p);
    128  1.1  mycroft 		free(mp, M_MOUNT);
    129  1.1  mycroft 		free(mfsp, M_MFSNODE);
    130  1.1  mycroft 		return (error);
    131  1.1  mycroft 	}
    132  1.1  mycroft 	TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list);
    133  1.1  mycroft 	mp->mnt_flag |= MNT_ROOTFS;
    134  1.1  mycroft 	mp->mnt_vnodecovered = NULLVP;
    135  1.1  mycroft 	ump = VFSTOUFS(mp);
    136  1.1  mycroft 	fs = ump->um_fs;
    137  1.1  mycroft 	bzero(fs->fs_fsmnt, sizeof(fs->fs_fsmnt));
    138  1.1  mycroft 	fs->fs_fsmnt[0] = '/';
    139  1.1  mycroft 	bcopy((caddr_t)fs->fs_fsmnt, (caddr_t)mp->mnt_stat.f_mntonname,
    140  1.1  mycroft 	    MNAMELEN);
    141  1.1  mycroft 	(void) copystr(ROOTNAME, mp->mnt_stat.f_mntfromname, MNAMELEN - 1,
    142  1.1  mycroft 	    &size);
    143  1.1  mycroft 	bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
    144  1.1  mycroft 	(void)ffs_statfs(mp, &mp->mnt_stat, p);
    145  1.1  mycroft 	vfs_unlock(mp);
    146  1.1  mycroft 	inittodr((time_t)0);
    147  1.1  mycroft 	return (0);
    148  1.1  mycroft }
    149  1.1  mycroft 
    150  1.1  mycroft /*
    151  1.1  mycroft  * This is called early in boot to set the base address and size
    152  1.1  mycroft  * of the mini-root.
    153  1.1  mycroft  */
    154  1.1  mycroft mfs_initminiroot(base)
    155  1.1  mycroft 	caddr_t base;
    156  1.1  mycroft {
    157  1.1  mycroft 	struct fs *fs = (struct fs *)(base + SBOFF);
    158  1.1  mycroft 	extern int (*mountroot)();
    159  1.1  mycroft 
    160  1.1  mycroft 	/* check for valid super block */
    161  1.1  mycroft 	if (fs->fs_magic != FS_MAGIC || fs->fs_bsize > MAXBSIZE ||
    162  1.1  mycroft 	    fs->fs_bsize < sizeof(struct fs))
    163  1.1  mycroft 		return (0);
    164  1.1  mycroft 	mountroot = mfs_mountroot;
    165  1.1  mycroft 	mfs_rootbase = base;
    166  1.1  mycroft 	mfs_rootsize = fs->fs_fsize * fs->fs_size;
    167  1.1  mycroft 	rootdev = makedev(255, mfs_minor++);
    168  1.1  mycroft 	return (mfs_rootsize);
    169  1.1  mycroft }
    170  1.1  mycroft 
    171  1.1  mycroft /*
    172  1.1  mycroft  * VFS Operations.
    173  1.1  mycroft  *
    174  1.1  mycroft  * mount system call
    175  1.1  mycroft  */
    176  1.1  mycroft /* ARGSUSED */
    177  1.1  mycroft int
    178  1.1  mycroft mfs_mount(mp, path, data, ndp, p)
    179  1.1  mycroft 	register struct mount *mp;
    180  1.1  mycroft 	char *path;
    181  1.1  mycroft 	caddr_t data;
    182  1.1  mycroft 	struct nameidata *ndp;
    183  1.1  mycroft 	struct proc *p;
    184  1.1  mycroft {
    185  1.1  mycroft 	struct vnode *devvp;
    186  1.1  mycroft 	struct mfs_args args;
    187  1.1  mycroft 	struct ufsmount *ump;
    188  1.1  mycroft 	register struct fs *fs;
    189  1.1  mycroft 	register struct mfsnode *mfsp;
    190  1.1  mycroft 	u_int size;
    191  1.1  mycroft 	int flags, error;
    192  1.1  mycroft 
    193  1.1  mycroft 	if (error = copyin(data, (caddr_t)&args, sizeof (struct mfs_args)))
    194  1.1  mycroft 		return (error);
    195  1.1  mycroft 
    196  1.1  mycroft 	/*
    197  1.1  mycroft 	 * If updating, check whether changing from read-only to
    198  1.1  mycroft 	 * read/write; if there is no device name, that's all we do.
    199  1.1  mycroft 	 */
    200  1.1  mycroft 	if (mp->mnt_flag & MNT_UPDATE) {
    201  1.1  mycroft 		ump = VFSTOUFS(mp);
    202  1.1  mycroft 		fs = ump->um_fs;
    203  1.1  mycroft 		if (fs->fs_ronly == 0 && (mp->mnt_flag & MNT_RDONLY)) {
    204  1.1  mycroft 			flags = WRITECLOSE;
    205  1.1  mycroft 			if (mp->mnt_flag & MNT_FORCE)
    206  1.1  mycroft 				flags |= FORCECLOSE;
    207  1.1  mycroft 			if (vfs_busy(mp))
    208  1.1  mycroft 				return (EBUSY);
    209  1.1  mycroft 			error = ffs_flushfiles(mp, flags, p);
    210  1.1  mycroft 			vfs_unbusy(mp);
    211  1.1  mycroft 			if (error)
    212  1.1  mycroft 				return (error);
    213  1.1  mycroft 		}
    214  1.1  mycroft 		if (fs->fs_ronly && (mp->mnt_flag & MNT_WANTRDWR))
    215  1.1  mycroft 			fs->fs_ronly = 0;
    216  1.1  mycroft #ifdef EXPORTMFS
    217  1.1  mycroft 		if (args.fspec == 0)
    218  1.1  mycroft 			return (vfs_export(mp, &ump->um_export, &args.export));
    219  1.1  mycroft #endif
    220  1.1  mycroft 		return (0);
    221  1.1  mycroft 	}
    222  1.1  mycroft 	error = getnewvnode(VT_MFS, (struct mount *)0, mfs_vnodeop_p, &devvp);
    223  1.1  mycroft 	if (error)
    224  1.1  mycroft 		return (error);
    225  1.1  mycroft 	devvp->v_type = VBLK;
    226  1.1  mycroft 	if (checkalias(devvp, makedev(255, mfs_minor++), (struct mount *)0))
    227  1.1  mycroft 		panic("mfs_mount: dup dev");
    228  1.1  mycroft 	mfsp = (struct mfsnode *)malloc(sizeof *mfsp, M_MFSNODE, M_WAITOK);
    229  1.1  mycroft 	devvp->v_data = mfsp;
    230  1.1  mycroft 	mfsp->mfs_baseoff = args.base;
    231  1.1  mycroft 	mfsp->mfs_size = args.size;
    232  1.1  mycroft 	mfsp->mfs_vnode = devvp;
    233  1.1  mycroft 	mfsp->mfs_pid = p->p_pid;
    234  1.1  mycroft 	mfsp->mfs_buflist = (struct buf *)0;
    235  1.1  mycroft 	if (error = ffs_mountfs(devvp, mp, p)) {
    236  1.1  mycroft 		mfsp->mfs_buflist = (struct buf *)-1;
    237  1.1  mycroft 		vrele(devvp);
    238  1.1  mycroft 		return (error);
    239  1.1  mycroft 	}
    240  1.1  mycroft 	ump = VFSTOUFS(mp);
    241  1.1  mycroft 	fs = ump->um_fs;
    242  1.1  mycroft 	(void) copyinstr(path, fs->fs_fsmnt, sizeof(fs->fs_fsmnt) - 1, &size);
    243  1.1  mycroft 	bzero(fs->fs_fsmnt + size, sizeof(fs->fs_fsmnt) - size);
    244  1.1  mycroft 	bcopy((caddr_t)fs->fs_fsmnt, (caddr_t)mp->mnt_stat.f_mntonname,
    245  1.1  mycroft 		MNAMELEN);
    246  1.1  mycroft 	(void) copyinstr(args.fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1,
    247  1.1  mycroft 		&size);
    248  1.1  mycroft 	bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
    249  1.1  mycroft 	(void) mfs_statfs(mp, &mp->mnt_stat, p);
    250  1.1  mycroft 	return (0);
    251  1.1  mycroft }
    252  1.1  mycroft 
    253  1.1  mycroft int	mfs_pri = PWAIT | PCATCH;		/* XXX prob. temp */
    254  1.1  mycroft 
    255  1.1  mycroft /*
    256  1.1  mycroft  * Used to grab the process and keep it in the kernel to service
    257  1.1  mycroft  * memory filesystem I/O requests.
    258  1.1  mycroft  *
    259  1.1  mycroft  * Loop servicing I/O requests.
    260  1.1  mycroft  * Copy the requested data into or out of the memory filesystem
    261  1.1  mycroft  * address space.
    262  1.1  mycroft  */
    263  1.1  mycroft /* ARGSUSED */
    264  1.1  mycroft int
    265  1.1  mycroft mfs_start(mp, flags, p)
    266  1.1  mycroft 	struct mount *mp;
    267  1.1  mycroft 	int flags;
    268  1.1  mycroft 	struct proc *p;
    269  1.1  mycroft {
    270  1.1  mycroft 	register struct vnode *vp = VFSTOUFS(mp)->um_devvp;
    271  1.1  mycroft 	register struct mfsnode *mfsp = VTOMFS(vp);
    272  1.1  mycroft 	register struct buf *bp;
    273  1.1  mycroft 	register caddr_t base;
    274  1.1  mycroft 	int error = 0;
    275  1.1  mycroft 
    276  1.1  mycroft 	base = mfsp->mfs_baseoff;
    277  1.1  mycroft 	while (mfsp->mfs_buflist != (struct buf *)(-1)) {
    278  1.1  mycroft 		while (bp = mfsp->mfs_buflist) {
    279  1.1  mycroft 			mfsp->mfs_buflist = bp->b_actf;
    280  1.1  mycroft 			mfs_doio(bp, base);
    281  1.1  mycroft 			wakeup((caddr_t)bp);
    282  1.1  mycroft 		}
    283  1.1  mycroft 		/*
    284  1.1  mycroft 		 * If a non-ignored signal is received, try to unmount.
    285  1.1  mycroft 		 * If that fails, clear the signal (it has been "processed"),
    286  1.1  mycroft 		 * otherwise we will loop here, as tsleep will always return
    287  1.1  mycroft 		 * EINTR/ERESTART.
    288  1.1  mycroft 		 */
    289  1.1  mycroft 		if (error = tsleep((caddr_t)vp, mfs_pri, "mfsidl", 0))
    290  1.1  mycroft 			if (dounmount(mp, 0, p) != 0)
    291  1.1  mycroft 				CLRSIG(p, CURSIG(p));
    292  1.1  mycroft 	}
    293  1.1  mycroft 	return (error);
    294  1.1  mycroft }
    295  1.1  mycroft 
    296  1.1  mycroft /*
    297  1.1  mycroft  * Get file system statistics.
    298  1.1  mycroft  */
    299  1.1  mycroft mfs_statfs(mp, sbp, p)
    300  1.1  mycroft 	struct mount *mp;
    301  1.1  mycroft 	struct statfs *sbp;
    302  1.1  mycroft 	struct proc *p;
    303  1.1  mycroft {
    304  1.1  mycroft 	int error;
    305  1.1  mycroft 
    306  1.1  mycroft 	error = ffs_statfs(mp, sbp, p);
    307  1.1  mycroft #ifdef COMPAT_09
    308  1.1  mycroft 	sbp->f_type = 3;
    309  1.1  mycroft #else
    310  1.1  mycroft 	sbp->f_type = 0;
    311  1.1  mycroft #endif
    312  1.1  mycroft 	strncpy(&sbp->f_fstypename[0], mp->mnt_op->vfs_name, MFSNAMELEN);
    313  1.1  mycroft 	sbp->f_fstypename[MFSNAMELEN] = '\0';
    314  1.1  mycroft 	return (error);
    315  1.1  mycroft }
    316