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