Home | History | Annotate | Line # | Download | only in mfs
mfs_vnops.c revision 1.9
      1 /*	$NetBSD: mfs_vnops.c,v 1.9 1996/09/01 23:49:35 mycroft Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1989, 1993
      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_vnops.c	8.5 (Berkeley) 7/28/94
     36  */
     37 
     38 #include <sys/param.h>
     39 #include <sys/systm.h>
     40 #include <sys/time.h>
     41 #include <sys/kernel.h>
     42 #include <sys/proc.h>
     43 #include <sys/buf.h>
     44 #include <sys/map.h>
     45 #include <sys/vnode.h>
     46 #include <sys/malloc.h>
     47 
     48 #include <miscfs/genfs/genfs.h>
     49 #include <miscfs/specfs/specdev.h>
     50 
     51 #include <machine/vmparam.h>
     52 
     53 #include <ufs/mfs/mfsnode.h>
     54 #include <ufs/mfs/mfsiom.h>
     55 #include <ufs/mfs/mfs_extern.h>
     56 
     57 /*
     58  * mfs vnode operations.
     59  */
     60 int (**mfs_vnodeop_p) __P((void *));
     61 struct vnodeopv_entry_desc mfs_vnodeop_entries[] = {
     62 	{ &vop_default_desc, vn_default_error },
     63 	{ &vop_lookup_desc, mfs_lookup },		/* lookup */
     64 	{ &vop_create_desc, mfs_create },		/* create */
     65 	{ &vop_mknod_desc, mfs_mknod },			/* mknod */
     66 	{ &vop_open_desc, mfs_open },			/* open */
     67 	{ &vop_close_desc, mfs_close },			/* close */
     68 	{ &vop_access_desc, mfs_access },		/* access */
     69 	{ &vop_getattr_desc, mfs_getattr },		/* getattr */
     70 	{ &vop_setattr_desc, mfs_setattr },		/* setattr */
     71 	{ &vop_read_desc, mfs_read },			/* read */
     72 	{ &vop_write_desc, mfs_write },			/* write */
     73 	{ &vop_ioctl_desc, mfs_ioctl },			/* ioctl */
     74 	{ &vop_select_desc, mfs_select },		/* select */
     75 	{ &vop_mmap_desc, mfs_mmap },			/* mmap */
     76 	{ &vop_fsync_desc, spec_fsync },		/* fsync */
     77 	{ &vop_seek_desc, mfs_seek },			/* seek */
     78 	{ &vop_remove_desc, mfs_remove },		/* remove */
     79 	{ &vop_link_desc, mfs_link },			/* link */
     80 	{ &vop_rename_desc, mfs_rename },		/* rename */
     81 	{ &vop_mkdir_desc, mfs_mkdir },			/* mkdir */
     82 	{ &vop_rmdir_desc, mfs_rmdir },			/* rmdir */
     83 	{ &vop_symlink_desc, mfs_symlink },		/* symlink */
     84 	{ &vop_readdir_desc, mfs_readdir },		/* readdir */
     85 	{ &vop_readlink_desc, mfs_readlink },		/* readlink */
     86 	{ &vop_abortop_desc, mfs_abortop },		/* abortop */
     87 	{ &vop_inactive_desc, mfs_inactive },		/* inactive */
     88 	{ &vop_reclaim_desc, mfs_reclaim },		/* reclaim */
     89 	{ &vop_lock_desc, mfs_lock },			/* lock */
     90 	{ &vop_unlock_desc, mfs_unlock },		/* unlock */
     91 	{ &vop_bmap_desc, mfs_bmap },			/* bmap */
     92 	{ &vop_strategy_desc, mfs_strategy },		/* strategy */
     93 	{ &vop_print_desc, mfs_print },			/* print */
     94 	{ &vop_islocked_desc, mfs_islocked },		/* islocked */
     95 	{ &vop_pathconf_desc, mfs_pathconf },		/* pathconf */
     96 	{ &vop_advlock_desc, mfs_advlock },		/* advlock */
     97 	{ &vop_blkatoff_desc, mfs_blkatoff },		/* blkatoff */
     98 	{ &vop_valloc_desc, mfs_valloc },		/* valloc */
     99 	{ &vop_vfree_desc, mfs_vfree },			/* vfree */
    100 	{ &vop_truncate_desc, mfs_truncate },		/* truncate */
    101 	{ &vop_update_desc, mfs_update },		/* update */
    102 	{ &vop_bwrite_desc, mfs_bwrite },		/* bwrite */
    103 	{ (struct vnodeop_desc*)NULL, (int(*) __P((void *)))NULL }
    104 };
    105 struct vnodeopv_desc mfs_vnodeop_opv_desc =
    106 	{ &mfs_vnodeop_p, mfs_vnodeop_entries };
    107 
    108 /*
    109  * Vnode Operations.
    110  *
    111  * Open called to allow memory filesystem to initialize and
    112  * validate before actual IO. Record our process identifier
    113  * so we can tell when we are doing I/O to ourself.
    114  */
    115 /* ARGSUSED */
    116 int
    117 mfs_open(v)
    118 	void *v;
    119 {
    120 	struct vop_open_args /* {
    121 		struct vnode *a_vp;
    122 		int  a_mode;
    123 		struct ucred *a_cred;
    124 		struct proc *a_p;
    125 	} */ *ap = v;
    126 
    127 	if (ap->a_vp->v_type != VBLK) {
    128 		panic("mfs_ioctl not VBLK");
    129 		/* NOTREACHED */
    130 	}
    131 	return (0);
    132 }
    133 
    134 /*
    135  * Ioctl operation.
    136  */
    137 /* ARGSUSED */
    138 int
    139 mfs_ioctl(v)
    140 	void *v;
    141 {
    142 #if 0
    143 	struct vop_ioctl_args /* {
    144 		struct vnode *a_vp;
    145 		u_long a_command;
    146 		caddr_t  a_data;
    147 		int  a_fflag;
    148 		struct ucred *a_cred;
    149 		struct proc *a_p;
    150 	} */ *ap = v;
    151 #endif
    152 
    153 	return (ENOTTY);
    154 }
    155 
    156 /*
    157  * Pass I/O requests to the memory filesystem process.
    158  */
    159 int
    160 mfs_strategy(v)
    161 	void *v;
    162 {
    163 	struct vop_strategy_args /* {
    164 		struct buf *a_bp;
    165 	} */ *ap = v;
    166 	register struct buf *bp = ap->a_bp;
    167 	register struct mfsnode *mfsp;
    168 	struct vnode *vp;
    169 	struct proc *p = curproc;		/* XXX */
    170 
    171 	if (!vfinddev(bp->b_dev, VBLK, &vp) || vp->v_usecount == 0)
    172 		panic("mfs_strategy: bad dev");
    173 	mfsp = VTOMFS(vp);
    174 	/* check for mini-root access */
    175 	if (mfsp->mfs_pid == 0) {
    176 		caddr_t base;
    177 
    178 		base = mfsp->mfs_baseoff + (bp->b_blkno << DEV_BSHIFT);
    179 		if (bp->b_flags & B_READ)
    180 			bcopy(base, bp->b_data, bp->b_bcount);
    181 		else
    182 			bcopy(bp->b_data, base, bp->b_bcount);
    183 		biodone(bp);
    184 	} else if (mfsp->mfs_pid == p->p_pid) {
    185 		mfs_doio(bp, mfsp->mfs_baseoff);
    186 	} else {
    187 		bp->b_actf = mfsp->mfs_buflist;
    188 		mfsp->mfs_buflist = bp;
    189 		wakeup((caddr_t)vp);
    190 	}
    191 	return (0);
    192 }
    193 
    194 /*
    195  * Memory file system I/O.
    196  *
    197  * Trivial on the HP since buffer has already been mapping into KVA space.
    198  */
    199 void
    200 mfs_doio(bp, base)
    201 	register struct buf *bp;
    202 	caddr_t base;
    203 {
    204 
    205 	base += (bp->b_blkno << DEV_BSHIFT);
    206 	if (bp->b_flags & B_READ)
    207 		bp->b_error = copyin(base, bp->b_data, bp->b_bcount);
    208 	else
    209 		bp->b_error = copyout(bp->b_data, base, bp->b_bcount);
    210 	if (bp->b_error)
    211 		bp->b_flags |= B_ERROR;
    212 	biodone(bp);
    213 }
    214 
    215 /*
    216  * This is a noop, simply returning what one has been given.
    217  */
    218 int
    219 mfs_bmap(v)
    220 	void *v;
    221 {
    222 	struct vop_bmap_args /* {
    223 		struct vnode *a_vp;
    224 		daddr_t  a_bn;
    225 		struct vnode **a_vpp;
    226 		daddr_t *a_bnp;
    227 		int *a_runp;
    228 	} */ *ap = v;
    229 
    230 	if (ap->a_vpp != NULL)
    231 		*ap->a_vpp = ap->a_vp;
    232 	if (ap->a_bnp != NULL)
    233 		*ap->a_bnp = ap->a_bn;
    234 	return (0);
    235 }
    236 
    237 /*
    238  * Memory filesystem close routine
    239  */
    240 /* ARGSUSED */
    241 int
    242 mfs_close(v)
    243 	void *v;
    244 {
    245 	struct vop_close_args /* {
    246 		struct vnode *a_vp;
    247 		int  a_fflag;
    248 		struct ucred *a_cred;
    249 		struct proc *a_p;
    250 	} */ *ap = v;
    251 	register struct vnode *vp = ap->a_vp;
    252 	register struct mfsnode *mfsp = VTOMFS(vp);
    253 	register struct buf *bp;
    254 	int error;
    255 
    256 	/*
    257 	 * Finish any pending I/O requests.
    258 	 */
    259 	while ((bp = mfsp->mfs_buflist) != NULL) {
    260 		mfsp->mfs_buflist = bp->b_actf;
    261 		mfs_doio(bp, mfsp->mfs_baseoff);
    262 		wakeup((caddr_t)bp);
    263 	}
    264 	/*
    265 	 * On last close of a memory filesystem
    266 	 * we must invalidate any in core blocks, so that
    267 	 * we can, free up its vnode.
    268 	 */
    269 	if ((error = vinvalbuf(vp, 1, ap->a_cred, ap->a_p, 0, 0)) != 0)
    270 		return (error);
    271 	/*
    272 	 * There should be no way to have any more uses of this
    273 	 * vnode, so if we find any other uses, it is a panic.
    274 	 */
    275 	if (vp->v_usecount > 1)
    276 		printf("mfs_close: ref count %d > 1\n", vp->v_usecount);
    277 	if (vp->v_usecount > 1 || mfsp->mfs_buflist)
    278 		panic("mfs_close");
    279 	/*
    280 	 * Send a request to the filesystem server to exit.
    281 	 */
    282 	mfsp->mfs_buflist = (struct buf *)(-1);
    283 	wakeup((caddr_t)vp);
    284 	return (0);
    285 }
    286 
    287 /*
    288  * Memory filesystem inactive routine
    289  */
    290 /* ARGSUSED */
    291 int
    292 mfs_inactive(v)
    293 	void *v;
    294 {
    295 	struct vop_inactive_args /* {
    296 		struct vnode *a_vp;
    297 	} */ *ap = v;
    298 	register struct mfsnode *mfsp = VTOMFS(ap->a_vp);
    299 
    300 	if (mfsp->mfs_buflist && mfsp->mfs_buflist != (struct buf *)(-1))
    301 		panic("mfs_inactive: not inactive (mfs_buflist %p)",
    302 			mfsp->mfs_buflist);
    303 	return (0);
    304 }
    305 
    306 /*
    307  * Reclaim a memory filesystem devvp so that it can be reused.
    308  */
    309 int
    310 mfs_reclaim(v)
    311 	void *v;
    312 {
    313 	struct vop_reclaim_args /* {
    314 		struct vnode *a_vp;
    315 	} */ *ap = v;
    316 	register struct vnode *vp = ap->a_vp;
    317 
    318 	FREE(vp->v_data, M_MFSNODE);
    319 	vp->v_data = NULL;
    320 	return (0);
    321 }
    322 
    323 /*
    324  * Print out the contents of an mfsnode.
    325  */
    326 int
    327 mfs_print(v)
    328 	void *v;
    329 {
    330 	struct vop_print_args /* {
    331 		struct vnode *a_vp;
    332 	} */ *ap = v;
    333 	register struct mfsnode *mfsp = VTOMFS(ap->a_vp);
    334 
    335 	printf("tag VT_MFS, pid %d, base %p, size %ld\n", mfsp->mfs_pid,
    336 	    mfsp->mfs_baseoff, mfsp->mfs_size);
    337 	return (0);
    338 }
    339 
    340 /*
    341  * Memory based filesystem initialization.
    342  */
    343 void
    344 mfs_init()
    345 {
    346 
    347 }
    348