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