Home | History | Annotate | Line # | Download | only in genfs
genfs_vnops.c revision 1.8
      1 /*	$NetBSD: genfs_vnops.c,v 1.8 1998/06/25 22:15:30 thorpej 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 	struct timespec ts;
     85 
     86 	vflushbuf(vp, (ap->a_flags & FSYNC_WAIT) != 0);
     87 	if ((ap->a_flags & FSYNC_DATAONLY) != 0) {
     88 		return (0);
     89 	} else {
     90 		TIMEVAL_TO_TIMESPEC(&time, &ts);
     91 		return (VOP_UPDATE(ap->a_vp, &ts, &ts,
     92 		    (ap->a_flags & FSYNC_WAIT) != 0));
     93 	}
     94 }
     95 
     96 int
     97 genfs_seek(v)
     98 	void *v;
     99 {
    100 	struct vop_seek_args /* {
    101 		struct vnode *a_vp;
    102 		off_t a_oldoff;
    103 		off_t a_newoff;
    104 		struct ucred *a_ucred;
    105 	} */ *ap = v;
    106 
    107 	if (ap->a_newoff < 0)
    108 		return (EINVAL);
    109 
    110 	return (0);
    111 }
    112 
    113 int
    114 genfs_abortop(v)
    115 	void *v;
    116 {
    117 	struct vop_abortop_args /* {
    118 		struct vnode *a_dvp;
    119 		struct componentname *a_cnp;
    120 	} */ *ap = v;
    121 
    122 	if ((ap->a_cnp->cn_flags & (HASBUF | SAVESTART)) == HASBUF)
    123 		FREE(ap->a_cnp->cn_pnbuf, M_NAMEI);
    124 	return (0);
    125 }
    126 
    127 /*ARGSUSED*/
    128 int
    129 genfs_badop(v)
    130 	void *v;
    131 {
    132 
    133 	panic("genfs: bad op");
    134 }
    135 
    136 /*ARGSUSED*/
    137 int
    138 genfs_nullop(v)
    139 	void *v;
    140 {
    141 
    142 	return (0);
    143 }
    144 
    145 /*ARGSUSED*/
    146 int
    147 genfs_eopnotsupp(v)
    148 	void *v;
    149 {
    150 
    151 	return (EOPNOTSUPP);
    152 }
    153 
    154 /*ARGSUSED*/
    155 int
    156 genfs_ebadf(v)
    157 	void *v;
    158 {
    159 
    160 	return (EBADF);
    161 }
    162 
    163 
    164 /*
    165  * Eliminate all activity associated with  the requested vnode
    166  * and with all vnodes aliased to the requested vnode.
    167  */
    168 int
    169 genfs_revoke(v)
    170 	void *v;
    171 {
    172 	struct vop_revoke_args /* {
    173 		struct vnode *a_vp;
    174 		int a_flags;
    175 	} */ *ap = v;
    176 	struct vnode *vp, *vq;
    177 	struct proc *p = curproc;	/* XXX */
    178 
    179 #ifdef DIAGNOSTIC
    180 	if ((ap->a_flags & REVOKEALL) == 0)
    181 		panic("genfs_revoke: not revokeall");
    182 #endif
    183 
    184 	vp = ap->a_vp;
    185 	simple_lock(&vp->v_interlock);
    186 
    187 	if (vp->v_flag & VALIASED) {
    188 		/*
    189 		 * If a vgone (or vclean) is already in progress,
    190 		 * wait until it is done and return.
    191 		 */
    192 		if (vp->v_flag & VXLOCK) {
    193 			vp->v_flag |= VXWANT;
    194 			simple_unlock(&vp->v_interlock);
    195 			tsleep((caddr_t)vp, PINOD, "vop_revokeall", 0);
    196 			return (0);
    197 		}
    198 		/*
    199 		 * Ensure that vp will not be vgone'd while we
    200 		 * are eliminating its aliases.
    201 		 */
    202 		vp->v_flag |= VXLOCK;
    203 		simple_unlock(&vp->v_interlock);
    204 		while (vp->v_flag & VALIASED) {
    205 			simple_lock(&spechash_slock);
    206 			for (vq = *vp->v_hashchain; vq; vq = vq->v_specnext) {
    207 				if (vq->v_rdev != vp->v_rdev ||
    208 				    vq->v_type != vp->v_type || vp == vq)
    209 					continue;
    210 				simple_unlock(&spechash_slock);
    211 				vgone(vq);
    212 				break;
    213 			}
    214 			if (vq == NULLVP)
    215 				simple_unlock(&spechash_slock);
    216 		}
    217 		/*
    218 		 * Remove the lock so that vgone below will
    219 		 * really eliminate the vnode after which time
    220 		 * vgone will awaken any sleepers.
    221 		 */
    222 		simple_lock(&vp->v_interlock);
    223 		vp->v_flag &= ~VXLOCK;
    224 	}
    225 	vgonel(vp, p);
    226 	return (0);
    227 }
    228 
    229 
    230 /*
    231  * Stubs to use when there is no locking to be done on the underlying object.
    232  * A minimal shared lock is necessary to ensure that the underlying object
    233  * is not revoked while an operation is in progress. So, an active shared
    234  * count is maintained in an auxillary vnode lock structure.
    235  */
    236 int
    237 genfs_nolock(v)
    238 	void *v;
    239 {
    240 	struct vop_lock_args /* {
    241 		struct vnode *a_vp;
    242 		int a_flags;
    243 		struct proc *a_p;
    244 	} */ *ap = v;
    245 
    246 #ifdef notyet
    247 	/*
    248 	 * This code cannot be used until all the non-locking filesystems
    249 	 * (notably NFS) are converted to properly lock and release nodes.
    250 	 * Also, certain vnode operations change the locking state within
    251 	 * the operation (create, mknod, remove, link, rename, mkdir, rmdir,
    252 	 * and symlink). Ideally these operations should not change the
    253 	 * lock state, but should be changed to let the caller of the
    254 	 * function unlock them. Otherwise all intermediate vnode layers
    255 	 * (such as union, umapfs, etc) must catch these functions to do
    256 	 * the necessary locking at their layer. Note that the inactive
    257 	 * and lookup operations also change their lock state, but this
    258 	 * cannot be avoided, so these two operations will always need
    259 	 * to be handled in intermediate layers.
    260 	 */
    261 	struct vnode *vp = ap->a_vp;
    262 	int vnflags, flags = ap->a_flags;
    263 
    264 	if (vp->v_vnlock == NULL) {
    265 		if ((flags & LK_TYPE_MASK) == LK_DRAIN)
    266 			return (0);
    267 		MALLOC(vp->v_vnlock, struct lock *, sizeof(struct lock),
    268 		    M_VNODE, M_WAITOK);
    269 		lockinit(vp->v_vnlock, PVFS, "vnlock", 0, 0);
    270 	}
    271 	switch (flags & LK_TYPE_MASK) {
    272 	case LK_DRAIN:
    273 		vnflags = LK_DRAIN;
    274 		break;
    275 	case LK_EXCLUSIVE:
    276 	case LK_SHARED:
    277 		vnflags = LK_SHARED;
    278 		break;
    279 	case LK_UPGRADE:
    280 	case LK_EXCLUPGRADE:
    281 	case LK_DOWNGRADE:
    282 		return (0);
    283 	case LK_RELEASE:
    284 	default:
    285 		panic("vop_nolock: bad operation %d", flags & LK_TYPE_MASK);
    286 	}
    287 	if (flags & LK_INTERLOCK)
    288 		vnflags |= LK_INTERLOCK;
    289 	return(lockmgr(vp->v_vnlock, vnflags, &vp->v_interlock));
    290 #else /* for now */
    291 	/*
    292 	 * Since we are not using the lock manager, we must clear
    293 	 * the interlock here.
    294 	 */
    295 	if (ap->a_flags & LK_INTERLOCK)
    296 		simple_unlock(&ap->a_vp->v_interlock);
    297 	return (0);
    298 #endif
    299 }
    300 
    301 /*
    302  * Decrement the active use count.
    303  */
    304 int
    305 genfs_nounlock(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 	if (vp->v_vnlock == NULL)
    316 		return (0);
    317 	return (lockmgr(vp->v_vnlock, LK_RELEASE, NULL));
    318 }
    319 
    320 /*
    321  * Return whether or not the node is in use.
    322  */
    323 int
    324 genfs_noislocked(v)
    325 	void *v;
    326 {
    327 	struct vop_islocked_args /* {
    328 		struct vnode *a_vp;
    329 	} */ *ap = v;
    330 	struct vnode *vp = ap->a_vp;
    331 
    332 	if (vp->v_vnlock == NULL)
    333 		return (0);
    334 	return (lockstatus(vp->v_vnlock));
    335 }
    336 
    337 /*
    338  * Local lease check for NFS servers.  Just set up args and let
    339  * nqsrv_getlease() do the rest.  If NFSSERVER is not in the kernel,
    340  * this is a null operation.
    341  */
    342 int
    343 genfs_lease_check(v)
    344 	void *v;
    345 {
    346 #ifdef NFSSERVER
    347 	struct vop_lease_args /* {
    348 		struct vnode *a_vp;
    349 		struct proc *a_p;
    350 		struct ucred *a_cred;
    351 		int a_flag;
    352 	} */ *ap = v;
    353 	u_int32_t duration = 0;
    354 	int cache;
    355 	u_quad_t frev;
    356 
    357 	(void) nqsrv_getlease(ap->a_vp, &duration, ND_CHECK | ap->a_flag,
    358 	    NQLOCALSLP, ap->a_p, (struct mbuf *)0, &cache, &frev, ap->a_cred);
    359 	return (0);
    360 #else
    361 	return (0);
    362 #endif /* NFSSERVER */
    363 }
    364