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