Home | History | Annotate | Line # | Download | only in mfs
mfs_vnops.c revision 1.49
      1 /*	$NetBSD: mfs_vnops.c,v 1.49 2008/03/26 14:19:43 ad 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. Neither the name of the University nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  *
     31  *	@(#)mfs_vnops.c	8.11 (Berkeley) 5/22/95
     32  */
     33 
     34 #include <sys/cdefs.h>
     35 __KERNEL_RCSID(0, "$NetBSD: mfs_vnops.c,v 1.49 2008/03/26 14:19:43 ad Exp $");
     36 
     37 #include <sys/param.h>
     38 #include <sys/systm.h>
     39 #include <sys/time.h>
     40 #include <sys/kernel.h>
     41 #include <sys/proc.h>
     42 #include <sys/buf.h>
     43 #include <sys/bufq.h>
     44 #include <sys/vnode.h>
     45 #include <sys/kmem.h>
     46 
     47 #include <miscfs/genfs/genfs.h>
     48 #include <miscfs/specfs/specdev.h>
     49 
     50 #include <machine/vmparam.h>
     51 
     52 #include <ufs/mfs/mfsnode.h>
     53 #include <ufs/mfs/mfs_extern.h>
     54 
     55 /*
     56  * mfs vnode operations.
     57  */
     58 int (**mfs_vnodeop_p)(void *);
     59 const struct vnodeopv_entry_desc mfs_vnodeop_entries[] = {
     60 	{ &vop_default_desc, vn_default_error },
     61 	{ &vop_lookup_desc, mfs_lookup },		/* lookup */
     62 	{ &vop_create_desc, mfs_create },		/* create */
     63 	{ &vop_mknod_desc, mfs_mknod },			/* mknod */
     64 	{ &vop_open_desc, mfs_open },			/* open */
     65 	{ &vop_close_desc, mfs_close },			/* close */
     66 	{ &vop_access_desc, mfs_access },		/* access */
     67 	{ &vop_getattr_desc, mfs_getattr },		/* getattr */
     68 	{ &vop_setattr_desc, mfs_setattr },		/* setattr */
     69 	{ &vop_read_desc, mfs_read },			/* read */
     70 	{ &vop_write_desc, mfs_write },			/* write */
     71 	{ &vop_ioctl_desc, mfs_ioctl },			/* ioctl */
     72 	{ &vop_poll_desc, mfs_poll },			/* poll */
     73 	{ &vop_revoke_desc, mfs_revoke },		/* revoke */
     74 	{ &vop_mmap_desc, mfs_mmap },			/* mmap */
     75 	{ &vop_fsync_desc, spec_fsync },		/* fsync */
     76 	{ &vop_seek_desc, mfs_seek },			/* seek */
     77 	{ &vop_remove_desc, mfs_remove },		/* remove */
     78 	{ &vop_link_desc, mfs_link },			/* link */
     79 	{ &vop_rename_desc, mfs_rename },		/* rename */
     80 	{ &vop_mkdir_desc, mfs_mkdir },			/* mkdir */
     81 	{ &vop_rmdir_desc, mfs_rmdir },			/* rmdir */
     82 	{ &vop_symlink_desc, mfs_symlink },		/* symlink */
     83 	{ &vop_readdir_desc, mfs_readdir },		/* readdir */
     84 	{ &vop_readlink_desc, mfs_readlink },		/* readlink */
     85 	{ &vop_abortop_desc, mfs_abortop },		/* abortop */
     86 	{ &vop_inactive_desc, mfs_inactive },		/* inactive */
     87 	{ &vop_reclaim_desc, mfs_reclaim },		/* reclaim */
     88 	{ &vop_lock_desc, genfs_nolock },		/* lock */
     89 	{ &vop_unlock_desc, genfs_nounlock },		/* unlock */
     90 	{ &vop_bmap_desc, mfs_bmap },			/* bmap */
     91 	{ &vop_strategy_desc, mfs_strategy },		/* strategy */
     92 	{ &vop_print_desc, mfs_print },			/* print */
     93 	{ &vop_islocked_desc, mfs_islocked },		/* islocked */
     94 	{ &vop_pathconf_desc, mfs_pathconf },		/* pathconf */
     95 	{ &vop_advlock_desc, mfs_advlock },		/* advlock */
     96 	{ &vop_bwrite_desc, mfs_bwrite },		/* bwrite */
     97 	{ &vop_putpages_desc, mfs_putpages },		/* putpages */
     98 	{ NULL, NULL }
     99 };
    100 const struct vnodeopv_desc mfs_vnodeop_opv_desc =
    101 	{ &mfs_vnodeop_p, mfs_vnodeop_entries };
    102 
    103 /*
    104  * Vnode Operations.
    105  *
    106  * Open called to allow memory filesystem to initialize and
    107  * validate before actual IO. Record our process identifier
    108  * so we can tell when we are doing I/O to ourself.
    109  */
    110 /* ARGSUSED */
    111 int
    112 mfs_open(void *v)
    113 {
    114 	struct vop_open_args /* {
    115 		struct vnode *a_vp;
    116 		int  a_mode;
    117 		kauth_cred_t a_cred;
    118 	} */ *ap = v;
    119 
    120 	if (ap->a_vp->v_type != VBLK) {
    121 		panic("mfs_ioctl not VBLK");
    122 		/* NOTREACHED */
    123 	}
    124 	return (0);
    125 }
    126 
    127 /*
    128  * Pass I/O requests to the memory filesystem process.
    129  */
    130 int
    131 mfs_strategy(void *v)
    132 {
    133 	struct vop_strategy_args /* {
    134 		struct vnode *a_vp;
    135 		struct buf *a_bp;
    136 	} */ *ap = v;
    137 	struct vnode *vp = ap->a_vp;
    138 	struct buf *bp = ap->a_bp;
    139 	struct mfsnode *mfsp;
    140 
    141 	if (vp->v_type != VBLK || vp->v_usecount == 0)
    142 		panic("mfs_strategy: bad dev");
    143 	mfsp = VTOMFS(vp);
    144 	/* check for mini-root access */
    145 	if (mfsp->mfs_proc == NULL) {
    146 		void *base;
    147 
    148 		base = (char *)mfsp->mfs_baseoff + (bp->b_blkno << DEV_BSHIFT);
    149 		if (bp->b_flags & B_READ)
    150 			memcpy(bp->b_data, base, bp->b_bcount);
    151 		else
    152 			memcpy(base, bp->b_data, bp->b_bcount);
    153 		bp->b_resid = 0;
    154 		biodone(bp);
    155 	} else if (mfsp->mfs_proc == curproc) {
    156 		mfs_doio(bp, mfsp->mfs_baseoff);
    157 	} else if (doing_shutdown) {
    158 		/*
    159 		 * bitbucket I/O during shutdown.
    160 		 * Note that reads should *not* happen here, but..
    161 		 */
    162 		if (bp->b_flags & B_READ)
    163 			printf("warning: mfs read during shutdown\n");
    164 		bp->b_resid = 0;
    165 		biodone(bp);
    166 	} else {
    167 		mutex_enter(&mfs_lock);
    168 		BUFQ_PUT(mfsp->mfs_buflist, bp);
    169 		cv_broadcast(&mfsp->mfs_cv);
    170 		mutex_exit(&mfs_lock);
    171 	}
    172 	return (0);
    173 }
    174 
    175 /*
    176  * Memory file system I/O.
    177  */
    178 void
    179 mfs_doio(struct buf *bp, void *base)
    180 {
    181 
    182 	base = (char *)base + (bp->b_blkno << DEV_BSHIFT);
    183 	if (bp->b_flags & B_READ)
    184 		bp->b_error = copyin(base, bp->b_data, bp->b_bcount);
    185 	else
    186 		bp->b_error = copyout(bp->b_data, base, bp->b_bcount);
    187 	if (bp->b_error == 0)
    188 		bp->b_resid = 0;
    189 	biodone(bp);
    190 }
    191 
    192 /*
    193  * This is a noop, simply returning what one has been given.
    194  */
    195 int
    196 mfs_bmap(void *v)
    197 {
    198 	struct vop_bmap_args /* {
    199 		struct vnode *a_vp;
    200 		daddr_t  a_bn;
    201 		struct vnode **a_vpp;
    202 		daddr_t *a_bnp;
    203 		int *a_runp;
    204 	} */ *ap = v;
    205 
    206 	if (ap->a_vpp != NULL)
    207 		*ap->a_vpp = ap->a_vp;
    208 	if (ap->a_bnp != NULL)
    209 		*ap->a_bnp = ap->a_bn;
    210 	if (ap->a_runp != NULL)
    211 		 *ap->a_runp = 0;
    212 	return (0);
    213 }
    214 
    215 /*
    216  * Memory filesystem close routine
    217  */
    218 /* ARGSUSED */
    219 int
    220 mfs_close(void *v)
    221 {
    222 	struct vop_close_args /* {
    223 		struct vnode *a_vp;
    224 		int  a_fflag;
    225 		kauth_cred_t a_cred;
    226 	} */ *ap = v;
    227 	struct vnode *vp = ap->a_vp;
    228 	struct mfsnode *mfsp = VTOMFS(vp);
    229 	struct buf *bp;
    230 	int error;
    231 
    232 	/*
    233 	 * Finish any pending I/O requests.
    234 	 */
    235 	mutex_enter(&mfs_lock);
    236 	while ((bp = BUFQ_GET(mfsp->mfs_buflist)) != NULL) {
    237 		mutex_exit(&mfs_lock);
    238 		mfs_doio(bp, mfsp->mfs_baseoff);
    239 		mutex_enter(&mfs_lock);
    240 	}
    241 	mutex_exit(&mfs_lock);
    242 	/*
    243 	 * On last close of a memory filesystem
    244 	 * we must invalidate any in core blocks, so that
    245 	 * we can, free up its vnode.
    246 	 */
    247 	if ((error = vinvalbuf(vp, V_SAVE, ap->a_cred, curlwp, 0, 0)) != 0)
    248 		return (error);
    249 	/*
    250 	 * There should be no way to have any more uses of this
    251 	 * vnode, so if we find any other uses, it is a panic.
    252 	 */
    253 	if (BUFQ_PEEK(mfsp->mfs_buflist) != NULL)
    254 		panic("mfs_close");
    255 	/*
    256 	 * Send a request to the filesystem server to exit.
    257 	 */
    258 	mutex_enter(&mfs_lock);
    259 	mfsp->mfs_shutdown = 1;
    260 	cv_broadcast(&mfsp->mfs_cv);
    261 	mutex_exit(&mfs_lock);
    262 	return (0);
    263 }
    264 
    265 /*
    266  * Memory filesystem inactive routine
    267  */
    268 /* ARGSUSED */
    269 int
    270 mfs_inactive(void *v)
    271 {
    272 	struct vop_inactive_args /* {
    273 		struct vnode *a_vp;
    274 	} */ *ap = v;
    275 	struct vnode *vp = ap->a_vp;
    276 	struct mfsnode *mfsp = VTOMFS(vp);
    277 
    278 	if (BUFQ_PEEK(mfsp->mfs_buflist) != NULL)
    279 		panic("mfs_inactive: not inactive (mfs_buflist %p)",
    280 			BUFQ_PEEK(mfsp->mfs_buflist));
    281 	VOP_UNLOCK(vp, 0);
    282 	return (0);
    283 }
    284 
    285 /*
    286  * Reclaim a memory filesystem devvp so that it can be reused.
    287  */
    288 int
    289 mfs_reclaim(void *v)
    290 {
    291 	struct vop_reclaim_args /* {
    292 		struct vnode *a_vp;
    293 	} */ *ap = v;
    294 	struct vnode *vp = ap->a_vp;
    295 	struct mfsnode *mfsp = VTOMFS(vp);
    296 	int refcnt;
    297 
    298 	mutex_enter(&mfs_lock);
    299 	vp->v_data = NULL;
    300 	refcnt = --mfsp->mfs_refcnt;
    301 	mutex_exit(&mfs_lock);
    302 
    303 	if (refcnt == 0) {
    304 		bufq_free(mfsp->mfs_buflist);
    305 		cv_destroy(&mfsp->mfs_cv);
    306 		kmem_free(mfsp, sizeof(*mfsp));
    307 	}
    308 
    309 	return (0);
    310 }
    311 
    312 /*
    313  * Print out the contents of an mfsnode.
    314  */
    315 int
    316 mfs_print(void *v)
    317 {
    318 	struct vop_print_args /* {
    319 		struct vnode *a_vp;
    320 	} */ *ap = v;
    321 	struct mfsnode *mfsp = VTOMFS(ap->a_vp);
    322 
    323 	printf("tag VT_MFS, pid %d, base %p, size %ld\n",
    324 	    (mfsp->mfs_proc != NULL) ? mfsp->mfs_proc->p_pid : 0,
    325 	    mfsp->mfs_baseoff, mfsp->mfs_size);
    326 	return (0);
    327 }
    328