Home | History | Annotate | Line # | Download | only in genfs
genfs_vnops.c revision 1.12
      1 /*	$NetBSD: genfs_vnops.c,v 1.12 1999/07/08 01:18:59 wrstuden Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1982, 1986, 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  */
     36 
     37 #include "opt_nfsserver.h"
     38 
     39 #include <sys/param.h>
     40 #include <sys/systm.h>
     41 #include <sys/proc.h>
     42 #include <sys/kernel.h>
     43 #include <sys/mount.h>
     44 #include <sys/namei.h>
     45 #include <sys/vnode.h>
     46 #include <sys/malloc.h>
     47 #include <sys/poll.h>
     48 
     49 #include <miscfs/genfs/genfs.h>
     50 #include <miscfs/specfs/specdev.h>
     51 
     52 #ifdef NFSSERVER
     53 #include <nfs/rpcv2.h>
     54 #include <nfs/nfsproto.h>
     55 #include <nfs/nfs.h>
     56 #include <nfs/nqnfs.h>
     57 #include <nfs/nfs_var.h>
     58 #endif
     59 
     60 int
     61 genfs_poll(v)
     62 	void *v;
     63 {
     64 	struct vop_poll_args /* {
     65 		struct vnode *a_vp;
     66 		int a_events;
     67 		struct proc *a_p;
     68 	} */ *ap = v;
     69 
     70 	return (ap->a_events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM));
     71 }
     72 
     73 int
     74 genfs_fsync(v)
     75 	void *v;
     76 {
     77 	struct vop_fsync_args /* {
     78 		struct vnode *a_vp;
     79 		struct ucred *a_cred;
     80 		int a_flags;
     81 		struct proc *a_p;
     82 	} */ *ap = v;
     83 	register struct vnode *vp = ap->a_vp;
     84 	int wait;
     85 
     86 	wait = (ap->a_flags & FSYNC_WAIT) != 0;
     87 	vflushbuf(vp, wait);
     88 	if ((ap->a_flags & FSYNC_DATAONLY) != 0)
     89 		return (0);
     90 	else
     91 		return (VOP_UPDATE(ap->a_vp, NULL, NULL, wait));
     92 }
     93 
     94 int
     95 genfs_seek(v)
     96 	void *v;
     97 {
     98 	struct vop_seek_args /* {
     99 		struct vnode *a_vp;
    100 		off_t a_oldoff;
    101 		off_t a_newoff;
    102 		struct ucred *a_ucred;
    103 	} */ *ap = v;
    104 
    105 	if (ap->a_newoff < 0)
    106 		return (EINVAL);
    107 
    108 	return (0);
    109 }
    110 
    111 int
    112 genfs_abortop(v)
    113 	void *v;
    114 {
    115 	struct vop_abortop_args /* {
    116 		struct vnode *a_dvp;
    117 		struct componentname *a_cnp;
    118 	} */ *ap = v;
    119 
    120 	if ((ap->a_cnp->cn_flags & (HASBUF | SAVESTART)) == HASBUF)
    121 		FREE(ap->a_cnp->cn_pnbuf, M_NAMEI);
    122 	return (0);
    123 }
    124 
    125 /*ARGSUSED*/
    126 int
    127 genfs_badop(v)
    128 	void *v;
    129 {
    130 
    131 	panic("genfs: bad op");
    132 }
    133 
    134 /*ARGSUSED*/
    135 int
    136 genfs_nullop(v)
    137 	void *v;
    138 {
    139 
    140 	return (0);
    141 }
    142 
    143 /*ARGSUSED*/
    144 int
    145 genfs_einval(v)
    146 	void *v;
    147 {
    148 
    149 	return (EINVAL);
    150 }
    151 
    152 /*ARGSUSED*/
    153 int
    154 genfs_eopnotsupp(v)
    155 	void *v;
    156 {
    157 
    158 	return (EOPNOTSUPP);
    159 }
    160 
    161 /*
    162  * Called when an fs doesn't support a particular vop but the vop needs to
    163  * vrele, vput, or vunlock passed in vnodes.
    164  */
    165 int
    166 genfs_eopnotsupp_rele(v)
    167 	void *v;
    168 {
    169 	struct vop_generic_args /*
    170 		struct vnodeop_desc *a_desc;
    171 		/ * other random data follows, presumably * /
    172 	} */ *ap = v;
    173 	struct vnodeop_desc *desc = ap->a_desc;
    174 	struct vnode *vp;
    175 	int flags, i, j, offset;
    176 
    177 	flags = desc->vdesc_flags;
    178 	for (i = 0; i < VDESC_MAX_VPS; flags >>=1, i++) {
    179 		if ((offset = desc->vdesc_vp_offsets[i]) == VDESC_NO_OFFSET)
    180 			break;	/* stop at end of list */
    181 		if ((j = flags & VDESC_VP0_WILLPUT)) {
    182 			vp = *VOPARG_OFFSETTO(struct vnode**,offset,ap);
    183 			switch (j) {
    184 			case VDESC_VP0_WILLPUT:
    185 				vput(vp);
    186 				break;
    187 			case VDESC_VP0_WILLUNLOCK:
    188 				VOP_UNLOCK(vp, 0);
    189 				break;
    190 			case VDESC_VP0_WILLRELE:
    191 				vrele(vp);
    192 				break;
    193 			}
    194 		}
    195 	}
    196 
    197 	return (EOPNOTSUPP);
    198 }
    199 
    200 /*ARGSUSED*/
    201 int
    202 genfs_ebadf(v)
    203 	void *v;
    204 {
    205 
    206 	return (EBADF);
    207 }
    208 
    209 /* ARGSUSED */
    210 int
    211 genfs_enoioctl(v)
    212 	void *v;
    213 {
    214 
    215 	return (ENOTTY);
    216 }
    217 
    218 
    219 /*
    220  * Eliminate all activity associated with  the requested vnode
    221  * and with all vnodes aliased to the requested vnode.
    222  */
    223 int
    224 genfs_revoke(v)
    225 	void *v;
    226 {
    227 	struct vop_revoke_args /* {
    228 		struct vnode *a_vp;
    229 		int a_flags;
    230 	} */ *ap = v;
    231 	struct vnode *vp, *vq;
    232 	struct proc *p = curproc;	/* XXX */
    233 
    234 #ifdef DIAGNOSTIC
    235 	if ((ap->a_flags & REVOKEALL) == 0)
    236 		panic("genfs_revoke: not revokeall");
    237 #endif
    238 
    239 	vp = ap->a_vp;
    240 	simple_lock(&vp->v_interlock);
    241 
    242 	if (vp->v_flag & VALIASED) {
    243 		/*
    244 		 * If a vgone (or vclean) is already in progress,
    245 		 * wait until it is done and return.
    246 		 */
    247 		if (vp->v_flag & VXLOCK) {
    248 			vp->v_flag |= VXWANT;
    249 			simple_unlock(&vp->v_interlock);
    250 			tsleep((caddr_t)vp, PINOD, "vop_revokeall", 0);
    251 			return (0);
    252 		}
    253 		/*
    254 		 * Ensure that vp will not be vgone'd while we
    255 		 * are eliminating its aliases.
    256 		 */
    257 		vp->v_flag |= VXLOCK;
    258 		simple_unlock(&vp->v_interlock);
    259 		while (vp->v_flag & VALIASED) {
    260 			simple_lock(&spechash_slock);
    261 			for (vq = *vp->v_hashchain; vq; vq = vq->v_specnext) {
    262 				if (vq->v_rdev != vp->v_rdev ||
    263 				    vq->v_type != vp->v_type || vp == vq)
    264 					continue;
    265 				simple_unlock(&spechash_slock);
    266 				vgone(vq);
    267 				break;
    268 			}
    269 			if (vq == NULLVP)
    270 				simple_unlock(&spechash_slock);
    271 		}
    272 		/*
    273 		 * Remove the lock so that vgone below will
    274 		 * really eliminate the vnode after which time
    275 		 * vgone will awaken any sleepers.
    276 		 */
    277 		simple_lock(&vp->v_interlock);
    278 		vp->v_flag &= ~VXLOCK;
    279 	}
    280 	vgonel(vp, p);
    281 	return (0);
    282 }
    283 
    284 /*
    285  * Lock the node.
    286  */
    287 int
    288 genfs_lock(v)
    289 	void *v;
    290 {
    291 	struct vop_lock_args /* {
    292 		struct vnode *a_vp;
    293 		int a_flags;
    294 		struct proc *a_p;
    295 	} */ *ap = v;
    296 	struct vnode *vp = ap->a_vp;
    297 
    298 	return (lockmgr(&vp->v_lock, ap->a_flags, &vp->v_interlock));
    299 }
    300 
    301 /*
    302  * Unlock the node.
    303  */
    304 int
    305 genfs_unlock(v)
    306 	void *v;
    307 {
    308 	struct vop_unlock_args /* {
    309 		struct vnode *a_vp;
    310 		int a_flags;
    311 		struct proc *a_p;
    312 	} */ *ap = v;
    313 	struct vnode *vp = ap->a_vp;
    314 
    315 	return (lockmgr(&vp->v_lock, ap->a_flags | LK_RELEASE,
    316 		&vp->v_interlock));
    317 }
    318 
    319 /*
    320  * Return whether or not the node is locked.
    321  */
    322 int
    323 genfs_islocked(v)
    324 	void *v;
    325 {
    326 	struct vop_islocked_args /* {
    327 		struct vnode *a_vp;
    328 	} */ *ap = v;
    329 	struct vnode *vp = ap->a_vp;
    330 
    331 	return (lockstatus(&vp->v_lock));
    332 }
    333 
    334 /*
    335  * Stubs to use when there is no locking to be done on the underlying object.
    336  */
    337 int
    338 genfs_nolock(v)
    339 	void *v;
    340 {
    341 	struct vop_lock_args /* {
    342 		struct vnode *a_vp;
    343 		int a_flags;
    344 		struct proc *a_p;
    345 	} */ *ap = v;
    346 
    347 	/*
    348 	 * Since we are not using the lock manager, we must clear
    349 	 * the interlock here.
    350 	 */
    351 	if (ap->a_flags & LK_INTERLOCK)
    352 		simple_unlock(&ap->a_vp->v_interlock);
    353 	return (0);
    354 }
    355 
    356 int
    357 genfs_nounlock(v)
    358 	void *v;
    359 {
    360 	return (0);
    361 }
    362 
    363 int
    364 genfs_noislocked(v)
    365 	void *v;
    366 {
    367 	return (0);
    368 }
    369 
    370 /*
    371  * Local lease check for NFS servers.  Just set up args and let
    372  * nqsrv_getlease() do the rest.  If NFSSERVER is not in the kernel,
    373  * this is a null operation.
    374  */
    375 int
    376 genfs_lease_check(v)
    377 	void *v;
    378 {
    379 #ifdef NFSSERVER
    380 	struct vop_lease_args /* {
    381 		struct vnode *a_vp;
    382 		struct proc *a_p;
    383 		struct ucred *a_cred;
    384 		int a_flag;
    385 	} */ *ap = v;
    386 	u_int32_t duration = 0;
    387 	int cache;
    388 	u_quad_t frev;
    389 
    390 	(void) nqsrv_getlease(ap->a_vp, &duration, ND_CHECK | ap->a_flag,
    391 	    NQLOCALSLP, ap->a_p, (struct mbuf *)0, &cache, &frev, ap->a_cred);
    392 	return (0);
    393 #else
    394 	return (0);
    395 #endif /* NFSSERVER */
    396 }
    397