Home | History | Annotate | Line # | Download | only in genfs
genfs_vnops.c revision 1.9
      1 /*	$NetBSD: genfs_vnops.c,v 1.9 1998/08/10 08:11:11 matthias 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 /* ARGSUSED */
    164 int
    165 genfs_enoioctl(v)
    166 	void *v;
    167 {
    168 
    169 	return (ENOTTY);
    170 }
    171 
    172 
    173 /*
    174  * Eliminate all activity associated with  the requested vnode
    175  * and with all vnodes aliased to the requested vnode.
    176  */
    177 int
    178 genfs_revoke(v)
    179 	void *v;
    180 {
    181 	struct vop_revoke_args /* {
    182 		struct vnode *a_vp;
    183 		int a_flags;
    184 	} */ *ap = v;
    185 	struct vnode *vp, *vq;
    186 	struct proc *p = curproc;	/* XXX */
    187 
    188 #ifdef DIAGNOSTIC
    189 	if ((ap->a_flags & REVOKEALL) == 0)
    190 		panic("genfs_revoke: not revokeall");
    191 #endif
    192 
    193 	vp = ap->a_vp;
    194 	simple_lock(&vp->v_interlock);
    195 
    196 	if (vp->v_flag & VALIASED) {
    197 		/*
    198 		 * If a vgone (or vclean) is already in progress,
    199 		 * wait until it is done and return.
    200 		 */
    201 		if (vp->v_flag & VXLOCK) {
    202 			vp->v_flag |= VXWANT;
    203 			simple_unlock(&vp->v_interlock);
    204 			tsleep((caddr_t)vp, PINOD, "vop_revokeall", 0);
    205 			return (0);
    206 		}
    207 		/*
    208 		 * Ensure that vp will not be vgone'd while we
    209 		 * are eliminating its aliases.
    210 		 */
    211 		vp->v_flag |= VXLOCK;
    212 		simple_unlock(&vp->v_interlock);
    213 		while (vp->v_flag & VALIASED) {
    214 			simple_lock(&spechash_slock);
    215 			for (vq = *vp->v_hashchain; vq; vq = vq->v_specnext) {
    216 				if (vq->v_rdev != vp->v_rdev ||
    217 				    vq->v_type != vp->v_type || vp == vq)
    218 					continue;
    219 				simple_unlock(&spechash_slock);
    220 				vgone(vq);
    221 				break;
    222 			}
    223 			if (vq == NULLVP)
    224 				simple_unlock(&spechash_slock);
    225 		}
    226 		/*
    227 		 * Remove the lock so that vgone below will
    228 		 * really eliminate the vnode after which time
    229 		 * vgone will awaken any sleepers.
    230 		 */
    231 		simple_lock(&vp->v_interlock);
    232 		vp->v_flag &= ~VXLOCK;
    233 	}
    234 	vgonel(vp, p);
    235 	return (0);
    236 }
    237 
    238 
    239 /*
    240  * Stubs to use when there is no locking to be done on the underlying object.
    241  * A minimal shared lock is necessary to ensure that the underlying object
    242  * is not revoked while an operation is in progress. So, an active shared
    243  * count is maintained in an auxillary vnode lock structure.
    244  */
    245 int
    246 genfs_nolock(v)
    247 	void *v;
    248 {
    249 	struct vop_lock_args /* {
    250 		struct vnode *a_vp;
    251 		int a_flags;
    252 		struct proc *a_p;
    253 	} */ *ap = v;
    254 
    255 #ifdef notyet
    256 	/*
    257 	 * This code cannot be used until all the non-locking filesystems
    258 	 * (notably NFS) are converted to properly lock and release nodes.
    259 	 * Also, certain vnode operations change the locking state within
    260 	 * the operation (create, mknod, remove, link, rename, mkdir, rmdir,
    261 	 * and symlink). Ideally these operations should not change the
    262 	 * lock state, but should be changed to let the caller of the
    263 	 * function unlock them. Otherwise all intermediate vnode layers
    264 	 * (such as union, umapfs, etc) must catch these functions to do
    265 	 * the necessary locking at their layer. Note that the inactive
    266 	 * and lookup operations also change their lock state, but this
    267 	 * cannot be avoided, so these two operations will always need
    268 	 * to be handled in intermediate layers.
    269 	 */
    270 	struct vnode *vp = ap->a_vp;
    271 	int vnflags, flags = ap->a_flags;
    272 
    273 	if (vp->v_vnlock == NULL) {
    274 		if ((flags & LK_TYPE_MASK) == LK_DRAIN)
    275 			return (0);
    276 		MALLOC(vp->v_vnlock, struct lock *, sizeof(struct lock),
    277 		    M_VNODE, M_WAITOK);
    278 		lockinit(vp->v_vnlock, PVFS, "vnlock", 0, 0);
    279 	}
    280 	switch (flags & LK_TYPE_MASK) {
    281 	case LK_DRAIN:
    282 		vnflags = LK_DRAIN;
    283 		break;
    284 	case LK_EXCLUSIVE:
    285 	case LK_SHARED:
    286 		vnflags = LK_SHARED;
    287 		break;
    288 	case LK_UPGRADE:
    289 	case LK_EXCLUPGRADE:
    290 	case LK_DOWNGRADE:
    291 		return (0);
    292 	case LK_RELEASE:
    293 	default:
    294 		panic("vop_nolock: bad operation %d", flags & LK_TYPE_MASK);
    295 	}
    296 	if (flags & LK_INTERLOCK)
    297 		vnflags |= LK_INTERLOCK;
    298 	return(lockmgr(vp->v_vnlock, vnflags, &vp->v_interlock));
    299 #else /* for now */
    300 	/*
    301 	 * Since we are not using the lock manager, we must clear
    302 	 * the interlock here.
    303 	 */
    304 	if (ap->a_flags & LK_INTERLOCK)
    305 		simple_unlock(&ap->a_vp->v_interlock);
    306 	return (0);
    307 #endif
    308 }
    309 
    310 /*
    311  * Decrement the active use count.
    312  */
    313 int
    314 genfs_nounlock(v)
    315 	void *v;
    316 {
    317 	struct vop_unlock_args /* {
    318 		struct vnode *a_vp;
    319 		int a_flags;
    320 		struct proc *a_p;
    321 	} */ *ap = v;
    322 	struct vnode *vp = ap->a_vp;
    323 
    324 	if (vp->v_vnlock == NULL)
    325 		return (0);
    326 	return (lockmgr(vp->v_vnlock, LK_RELEASE, NULL));
    327 }
    328 
    329 /*
    330  * Return whether or not the node is in use.
    331  */
    332 int
    333 genfs_noislocked(v)
    334 	void *v;
    335 {
    336 	struct vop_islocked_args /* {
    337 		struct vnode *a_vp;
    338 	} */ *ap = v;
    339 	struct vnode *vp = ap->a_vp;
    340 
    341 	if (vp->v_vnlock == NULL)
    342 		return (0);
    343 	return (lockstatus(vp->v_vnlock));
    344 }
    345 
    346 /*
    347  * Local lease check for NFS servers.  Just set up args and let
    348  * nqsrv_getlease() do the rest.  If NFSSERVER is not in the kernel,
    349  * this is a null operation.
    350  */
    351 int
    352 genfs_lease_check(v)
    353 	void *v;
    354 {
    355 #ifdef NFSSERVER
    356 	struct vop_lease_args /* {
    357 		struct vnode *a_vp;
    358 		struct proc *a_p;
    359 		struct ucred *a_cred;
    360 		int a_flag;
    361 	} */ *ap = v;
    362 	u_int32_t duration = 0;
    363 	int cache;
    364 	u_quad_t frev;
    365 
    366 	(void) nqsrv_getlease(ap->a_vp, &duration, ND_CHECK | ap->a_flag,
    367 	    NQLOCALSLP, ap->a_p, (struct mbuf *)0, &cache, &frev, ap->a_cred);
    368 	return (0);
    369 #else
    370 	return (0);
    371 #endif /* NFSSERVER */
    372 }
    373