Home | History | Annotate | Line # | Download | only in nfs
nfs_vnops.c revision 1.100.2.2
      1 /*	$NetBSD: nfs_vnops.c,v 1.100.2.2 2000/01/05 23:39:50 he Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1989, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * This code is derived from software contributed to Berkeley by
      8  * Rick Macklem at The University of Guelph.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by the University of
     21  *	California, Berkeley and its contributors.
     22  * 4. Neither the name of the University nor the names of its contributors
     23  *    may be used to endorse or promote products derived from this software
     24  *    without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36  * SUCH DAMAGE.
     37  *
     38  *	@(#)nfs_vnops.c	8.19 (Berkeley) 7/31/95
     39  */
     40 
     41 /*
     42  * vnode op calls for Sun NFS version 2 and 3
     43  */
     44 
     45 #include <sys/param.h>
     46 #include <sys/proc.h>
     47 #include <sys/kernel.h>
     48 #include <sys/systm.h>
     49 #include <sys/resourcevar.h>
     50 #include <sys/proc.h>
     51 #include <sys/mount.h>
     52 #include <sys/buf.h>
     53 #include <sys/malloc.h>
     54 #include <sys/mbuf.h>
     55 #include <sys/conf.h>
     56 #include <sys/namei.h>
     57 #include <sys/vnode.h>
     58 #include <sys/dirent.h>
     59 #include <sys/fcntl.h>
     60 #include <sys/lockf.h>
     61 #include <sys/stat.h>
     62 #include <sys/unistd.h>
     63 
     64 #include <vm/vm.h>
     65 
     66 #include <uvm/uvm_extern.h>
     67 
     68 #include <miscfs/fifofs/fifo.h>
     69 #include <miscfs/genfs/genfs.h>
     70 #include <miscfs/specfs/specdev.h>
     71 
     72 #include <nfs/rpcv2.h>
     73 #include <nfs/nfsproto.h>
     74 #include <nfs/nfs.h>
     75 #include <nfs/nfsnode.h>
     76 #include <nfs/nfsmount.h>
     77 #include <nfs/xdr_subs.h>
     78 #include <nfs/nfsm_subs.h>
     79 #include <nfs/nqnfs.h>
     80 #include <nfs/nfs_var.h>
     81 
     82 #include <net/if.h>
     83 #include <netinet/in.h>
     84 #include <netinet/in_var.h>
     85 
     86 /* Defs */
     87 #define	TRUE	1
     88 #define	FALSE	0
     89 
     90 /*
     91  * Global vfs data structures for nfs
     92  */
     93 int (**nfsv2_vnodeop_p) __P((void *));
     94 struct vnodeopv_entry_desc nfsv2_vnodeop_entries[] = {
     95 	{ &vop_default_desc, vn_default_error },
     96 	{ &vop_lookup_desc, nfs_lookup },		/* lookup */
     97 	{ &vop_create_desc, nfs_create },		/* create */
     98 	{ &vop_mknod_desc, nfs_mknod },			/* mknod */
     99 	{ &vop_open_desc, nfs_open },			/* open */
    100 	{ &vop_close_desc, nfs_close },			/* close */
    101 	{ &vop_access_desc, nfs_access },		/* access */
    102 	{ &vop_getattr_desc, nfs_getattr },		/* getattr */
    103 	{ &vop_setattr_desc, nfs_setattr },		/* setattr */
    104 	{ &vop_read_desc, nfs_read },			/* read */
    105 	{ &vop_write_desc, nfs_write },			/* write */
    106 	{ &vop_lease_desc, nfs_lease_check },		/* lease */
    107 	{ &vop_ioctl_desc, nfs_ioctl },			/* ioctl */
    108 	{ &vop_poll_desc, nfs_poll },			/* poll */
    109 	{ &vop_revoke_desc, nfs_revoke },		/* revoke */
    110 	{ &vop_mmap_desc, nfs_mmap },			/* mmap */
    111 	{ &vop_fsync_desc, nfs_fsync },			/* fsync */
    112 	{ &vop_seek_desc, nfs_seek },			/* seek */
    113 	{ &vop_remove_desc, nfs_remove },		/* remove */
    114 	{ &vop_link_desc, nfs_link },			/* link */
    115 	{ &vop_rename_desc, nfs_rename },		/* rename */
    116 	{ &vop_mkdir_desc, nfs_mkdir },			/* mkdir */
    117 	{ &vop_rmdir_desc, nfs_rmdir },			/* rmdir */
    118 	{ &vop_symlink_desc, nfs_symlink },		/* symlink */
    119 	{ &vop_readdir_desc, nfs_readdir },		/* readdir */
    120 	{ &vop_readlink_desc, nfs_readlink },		/* readlink */
    121 	{ &vop_abortop_desc, nfs_abortop },		/* abortop */
    122 	{ &vop_inactive_desc, nfs_inactive },		/* inactive */
    123 	{ &vop_reclaim_desc, nfs_reclaim },		/* reclaim */
    124 	{ &vop_lock_desc, nfs_lock },			/* lock */
    125 	{ &vop_unlock_desc, nfs_unlock },		/* unlock */
    126 	{ &vop_bmap_desc, nfs_bmap },			/* bmap */
    127 	{ &vop_strategy_desc, nfs_strategy },		/* strategy */
    128 	{ &vop_print_desc, nfs_print },			/* print */
    129 	{ &vop_islocked_desc, nfs_islocked },		/* islocked */
    130 	{ &vop_pathconf_desc, nfs_pathconf },		/* pathconf */
    131 	{ &vop_advlock_desc, nfs_advlock },		/* advlock */
    132 	{ &vop_blkatoff_desc, nfs_blkatoff },		/* blkatoff */
    133 	{ &vop_valloc_desc, nfs_valloc },		/* valloc */
    134 	{ &vop_reallocblks_desc, nfs_reallocblks },	/* reallocblks */
    135 	{ &vop_vfree_desc, nfs_vfree },			/* vfree */
    136 	{ &vop_truncate_desc, nfs_truncate },		/* truncate */
    137 	{ &vop_update_desc, nfs_update },		/* update */
    138 	{ &vop_bwrite_desc, nfs_bwrite },		/* bwrite */
    139 	{ (struct vnodeop_desc*)NULL, (int(*) __P((void *)))NULL }
    140 };
    141 struct vnodeopv_desc nfsv2_vnodeop_opv_desc =
    142 	{ &nfsv2_vnodeop_p, nfsv2_vnodeop_entries };
    143 
    144 /*
    145  * Special device vnode ops
    146  */
    147 int (**spec_nfsv2nodeop_p) __P((void *));
    148 struct vnodeopv_entry_desc spec_nfsv2nodeop_entries[] = {
    149 	{ &vop_default_desc, vn_default_error },
    150 	{ &vop_lookup_desc, spec_lookup },		/* lookup */
    151 	{ &vop_create_desc, spec_create },		/* create */
    152 	{ &vop_mknod_desc, spec_mknod },		/* mknod */
    153 	{ &vop_open_desc, spec_open },			/* open */
    154 	{ &vop_close_desc, nfsspec_close },		/* close */
    155 	{ &vop_access_desc, nfsspec_access },		/* access */
    156 	{ &vop_getattr_desc, nfs_getattr },		/* getattr */
    157 	{ &vop_setattr_desc, nfs_setattr },		/* setattr */
    158 	{ &vop_read_desc, nfsspec_read },		/* read */
    159 	{ &vop_write_desc, nfsspec_write },		/* write */
    160 	{ &vop_lease_desc, spec_lease_check },		/* lease */
    161 	{ &vop_ioctl_desc, spec_ioctl },		/* ioctl */
    162 	{ &vop_poll_desc, spec_poll },			/* poll */
    163 	{ &vop_revoke_desc, spec_revoke },		/* revoke */
    164 	{ &vop_mmap_desc, spec_mmap },			/* mmap */
    165 	{ &vop_fsync_desc, nfs_fsync },			/* fsync */
    166 	{ &vop_seek_desc, spec_seek },			/* seek */
    167 	{ &vop_remove_desc, spec_remove },		/* remove */
    168 	{ &vop_link_desc, spec_link },			/* link */
    169 	{ &vop_rename_desc, spec_rename },		/* rename */
    170 	{ &vop_mkdir_desc, spec_mkdir },		/* mkdir */
    171 	{ &vop_rmdir_desc, spec_rmdir },		/* rmdir */
    172 	{ &vop_symlink_desc, spec_symlink },		/* symlink */
    173 	{ &vop_readdir_desc, spec_readdir },		/* readdir */
    174 	{ &vop_readlink_desc, spec_readlink },		/* readlink */
    175 	{ &vop_abortop_desc, spec_abortop },		/* abortop */
    176 	{ &vop_inactive_desc, nfs_inactive },		/* inactive */
    177 	{ &vop_reclaim_desc, nfs_reclaim },		/* reclaim */
    178 	{ &vop_lock_desc, nfs_lock },			/* lock */
    179 	{ &vop_unlock_desc, nfs_unlock },		/* unlock */
    180 	{ &vop_bmap_desc, spec_bmap },			/* bmap */
    181 	{ &vop_strategy_desc, spec_strategy },		/* strategy */
    182 	{ &vop_print_desc, nfs_print },			/* print */
    183 	{ &vop_islocked_desc, nfs_islocked },		/* islocked */
    184 	{ &vop_pathconf_desc, spec_pathconf },		/* pathconf */
    185 	{ &vop_advlock_desc, spec_advlock },		/* advlock */
    186 	{ &vop_blkatoff_desc, spec_blkatoff },		/* blkatoff */
    187 	{ &vop_valloc_desc, spec_valloc },		/* valloc */
    188 	{ &vop_reallocblks_desc, spec_reallocblks },	/* reallocblks */
    189 	{ &vop_vfree_desc, spec_vfree },		/* vfree */
    190 	{ &vop_truncate_desc, spec_truncate },		/* truncate */
    191 	{ &vop_update_desc, nfs_update },		/* update */
    192 	{ &vop_bwrite_desc, vn_bwrite },		/* bwrite */
    193 	{ (struct vnodeop_desc*)NULL, (int(*) __P((void *)))NULL }
    194 };
    195 struct vnodeopv_desc spec_nfsv2nodeop_opv_desc =
    196 	{ &spec_nfsv2nodeop_p, spec_nfsv2nodeop_entries };
    197 
    198 int (**fifo_nfsv2nodeop_p) __P((void *));
    199 struct vnodeopv_entry_desc fifo_nfsv2nodeop_entries[] = {
    200 	{ &vop_default_desc, vn_default_error },
    201 	{ &vop_lookup_desc, fifo_lookup },		/* lookup */
    202 	{ &vop_create_desc, fifo_create },		/* create */
    203 	{ &vop_mknod_desc, fifo_mknod },		/* mknod */
    204 	{ &vop_open_desc, fifo_open },			/* open */
    205 	{ &vop_close_desc, nfsfifo_close },		/* close */
    206 	{ &vop_access_desc, nfsspec_access },		/* access */
    207 	{ &vop_getattr_desc, nfs_getattr },		/* getattr */
    208 	{ &vop_setattr_desc, nfs_setattr },		/* setattr */
    209 	{ &vop_read_desc, nfsfifo_read },		/* read */
    210 	{ &vop_write_desc, nfsfifo_write },		/* write */
    211 	{ &vop_lease_desc, fifo_lease_check },		/* lease */
    212 	{ &vop_ioctl_desc, fifo_ioctl },		/* ioctl */
    213 	{ &vop_poll_desc, fifo_poll },			/* poll */
    214 	{ &vop_revoke_desc, fifo_revoke },		/* revoke */
    215 	{ &vop_mmap_desc, fifo_mmap },			/* mmap */
    216 	{ &vop_fsync_desc, nfs_fsync },			/* fsync */
    217 	{ &vop_seek_desc, fifo_seek },			/* seek */
    218 	{ &vop_remove_desc, fifo_remove },		/* remove */
    219 	{ &vop_link_desc, fifo_link },			/* link */
    220 	{ &vop_rename_desc, fifo_rename },		/* rename */
    221 	{ &vop_mkdir_desc, fifo_mkdir },		/* mkdir */
    222 	{ &vop_rmdir_desc, fifo_rmdir },		/* rmdir */
    223 	{ &vop_symlink_desc, fifo_symlink },		/* symlink */
    224 	{ &vop_readdir_desc, fifo_readdir },		/* readdir */
    225 	{ &vop_readlink_desc, fifo_readlink },		/* readlink */
    226 	{ &vop_abortop_desc, fifo_abortop },		/* abortop */
    227 	{ &vop_inactive_desc, nfs_inactive },		/* inactive */
    228 	{ &vop_reclaim_desc, nfs_reclaim },		/* reclaim */
    229 	{ &vop_lock_desc, nfs_lock },			/* lock */
    230 	{ &vop_unlock_desc, nfs_unlock },		/* unlock */
    231 	{ &vop_bmap_desc, fifo_bmap },			/* bmap */
    232 	{ &vop_strategy_desc, genfs_badop },		/* strategy */
    233 	{ &vop_print_desc, nfs_print },			/* print */
    234 	{ &vop_islocked_desc, nfs_islocked },		/* islocked */
    235 	{ &vop_pathconf_desc, fifo_pathconf },		/* pathconf */
    236 	{ &vop_advlock_desc, fifo_advlock },		/* advlock */
    237 	{ &vop_blkatoff_desc, fifo_blkatoff },		/* blkatoff */
    238 	{ &vop_valloc_desc, fifo_valloc },		/* valloc */
    239 	{ &vop_reallocblks_desc, fifo_reallocblks },	/* reallocblks */
    240 	{ &vop_vfree_desc, fifo_vfree },		/* vfree */
    241 	{ &vop_truncate_desc, fifo_truncate },		/* truncate */
    242 	{ &vop_update_desc, nfs_update },		/* update */
    243 	{ &vop_bwrite_desc, vn_bwrite },		/* bwrite */
    244 	{ (struct vnodeop_desc*)NULL, (int(*) __P((void *)))NULL }
    245 };
    246 struct vnodeopv_desc fifo_nfsv2nodeop_opv_desc =
    247 	{ &fifo_nfsv2nodeop_p, fifo_nfsv2nodeop_entries };
    248 
    249 /*
    250  * Global variables
    251  */
    252 extern u_int32_t nfs_true, nfs_false;
    253 extern u_int32_t nfs_xdrneg1;
    254 extern struct nfsstats nfsstats;
    255 extern nfstype nfsv3_type[9];
    256 struct proc *nfs_iodwant[NFS_MAXASYNCDAEMON];
    257 struct nfsmount *nfs_iodmount[NFS_MAXASYNCDAEMON];
    258 int nfs_numasync = 0;
    259 #define	DIRHDSIZ	(sizeof (struct dirent) - (MAXNAMLEN + 1))
    260 
    261 /*
    262  * nfs null call from vfs.
    263  */
    264 int
    265 nfs_null(vp, cred, procp)
    266 	struct vnode *vp;
    267 	struct ucred *cred;
    268 	struct proc *procp;
    269 {
    270 	caddr_t bpos, dpos;
    271 	int error = 0;
    272 	struct mbuf *mreq, *mrep, *md, *mb;
    273 
    274 	nfsm_reqhead(vp, NFSPROC_NULL, 0);
    275 	nfsm_request(vp, NFSPROC_NULL, procp, cred);
    276 	nfsm_reqdone;
    277 	return (error);
    278 }
    279 
    280 /*
    281  * nfs access vnode op.
    282  * For nfs version 2, just return ok. File accesses may fail later.
    283  * For nfs version 3, use the access rpc to check accessibility. If file modes
    284  * are changed on the server, accesses might still fail later.
    285  */
    286 int
    287 nfs_access(v)
    288 	void *v;
    289 {
    290 	struct vop_access_args /* {
    291 		struct vnode *a_vp;
    292 		int  a_mode;
    293 		struct ucred *a_cred;
    294 		struct proc *a_p;
    295 	} */ *ap = v;
    296 	register struct vnode *vp = ap->a_vp;
    297 	register u_int32_t *tl;
    298 	register caddr_t cp;
    299 	register int32_t t1, t2;
    300 	caddr_t bpos, dpos, cp2;
    301 	int error = 0, attrflag, cachevalid;
    302 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
    303 	u_int32_t mode, rmode;
    304 	int v3 = NFS_ISV3(vp);
    305 	struct nfsnode *np = VTONFS(vp);
    306 
    307 	cachevalid = (np->n_accstamp != -1 &&
    308 	    (time.tv_sec - np->n_accstamp) < NFS_ATTRTIMEO(np) &&
    309 	    np->n_accuid == ap->a_cred->cr_uid);
    310 
    311 	/*
    312 	 * Check access cache first. If this request has been made for this
    313 	 * uid shortly before, use the cached result.
    314 	 */
    315 	if (cachevalid && ((np->n_accmode & ap->a_mode) == ap->a_mode))
    316 		return np->n_accerror;
    317 
    318 	/*
    319 	 * For nfs v3, do an access rpc, otherwise you are stuck emulating
    320 	 * ufs_access() locally using the vattr. This may not be correct,
    321 	 * since the server may apply other access criteria such as
    322 	 * client uid-->server uid mapping that we do not know about, but
    323 	 * this is better than just returning anything that is lying about
    324 	 * in the cache.
    325 	 */
    326 	if (v3) {
    327 		nfsstats.rpccnt[NFSPROC_ACCESS]++;
    328 		nfsm_reqhead(vp, NFSPROC_ACCESS, NFSX_FH(v3) + NFSX_UNSIGNED);
    329 		nfsm_fhtom(vp, v3);
    330 		nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
    331 		if (ap->a_mode & VREAD)
    332 			mode = NFSV3ACCESS_READ;
    333 		else
    334 			mode = 0;
    335 		if (vp->v_type != VDIR) {
    336 			if (ap->a_mode & VWRITE)
    337 				mode |= (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND);
    338 			if (ap->a_mode & VEXEC)
    339 				mode |= NFSV3ACCESS_EXECUTE;
    340 		} else {
    341 			if (ap->a_mode & VWRITE)
    342 				mode |= (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND |
    343 					 NFSV3ACCESS_DELETE);
    344 			if (ap->a_mode & VEXEC)
    345 				mode |= NFSV3ACCESS_LOOKUP;
    346 		}
    347 		*tl = txdr_unsigned(mode);
    348 		nfsm_request(vp, NFSPROC_ACCESS, ap->a_p, ap->a_cred);
    349 		nfsm_postop_attr(vp, attrflag);
    350 		if (!error) {
    351 			nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
    352 			rmode = fxdr_unsigned(u_int32_t, *tl);
    353 			/*
    354 			 * The NFS V3 spec does not clarify whether or not
    355 			 * the returned access bits can be a superset of
    356 			 * the ones requested, so...
    357 			 */
    358 			if ((rmode & mode) != mode)
    359 				error = EACCES;
    360 		}
    361 		nfsm_reqdone;
    362 	} else
    363 		return (nfsspec_access(ap));
    364 	/*
    365 	 * Disallow write attempts on filesystems mounted read-only;
    366 	 * unless the file is a socket, fifo, or a block or character
    367 	 * device resident on the filesystem.
    368 	 */
    369 	if (!error && (ap->a_mode & VWRITE) &&
    370 	    (vp->v_mount->mnt_flag & MNT_RDONLY)) {
    371 		switch (vp->v_type) {
    372 		case VREG:
    373 		case VDIR:
    374 		case VLNK:
    375 			error = EROFS;
    376 		default:
    377 			break;
    378 		}
    379 	}
    380 
    381 	if (!error || error == EACCES) {
    382 		/*
    383 		 * If we got the same result as for a previous,
    384 		 * different request, OR it in. Don't update
    385 		 * the timestamp in that case.
    386 		 */
    387 		if (cachevalid && error == np->n_accerror)
    388 			np->n_accmode |= ap->a_mode;
    389 		else {
    390 			np->n_accstamp = time.tv_sec;
    391 			np->n_accuid = ap->a_cred->cr_uid;
    392 			np->n_accmode = ap->a_mode;
    393 			np->n_accerror = error;
    394 		}
    395 	}
    396 
    397 	return (error);
    398 }
    399 
    400 /*
    401  * nfs open vnode op
    402  * Check to see if the type is ok
    403  * and that deletion is not in progress.
    404  * For paged in text files, you will need to flush the page cache
    405  * if consistency is lost.
    406  */
    407 /* ARGSUSED */
    408 int
    409 nfs_open(v)
    410 	void *v;
    411 {
    412 	struct vop_open_args /* {
    413 		struct vnode *a_vp;
    414 		int  a_mode;
    415 		struct ucred *a_cred;
    416 		struct proc *a_p;
    417 	} */ *ap = v;
    418 	register struct vnode *vp = ap->a_vp;
    419 	struct nfsnode *np = VTONFS(vp);
    420 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
    421 	struct vattr vattr;
    422 	int error;
    423 
    424 	if (vp->v_type != VREG && vp->v_type != VDIR && vp->v_type != VLNK) {
    425 #ifdef DIAGNOSTIC
    426 		printf("open eacces vtyp=%d\n",vp->v_type);
    427 #endif
    428 		return (EACCES);
    429 	}
    430 	/*
    431 	 * Get a valid lease. If cached data is stale, flush it.
    432 	 */
    433 	if (nmp->nm_flag & NFSMNT_NQNFS) {
    434 		if (NQNFS_CKINVALID(vp, np, ND_READ)) {
    435 		    do {
    436 			error = nqnfs_getlease(vp, ND_READ, ap->a_cred,
    437 			    ap->a_p);
    438 		    } while (error == NQNFS_EXPIRED);
    439 		    if (error)
    440 			return (error);
    441 		    if (np->n_lrev != np->n_brev ||
    442 			(np->n_flag & NQNFSNONCACHE)) {
    443 			if ((error = nfs_vinvalbuf(vp, V_SAVE, ap->a_cred,
    444 				ap->a_p, 1)) == EINTR)
    445 				return (error);
    446 			(void) uvm_vnp_uncache(vp);
    447 			np->n_brev = np->n_lrev;
    448 		    }
    449 		}
    450 	} else {
    451 		if (np->n_flag & NMODIFIED) {
    452 			if ((error = nfs_vinvalbuf(vp, V_SAVE, ap->a_cred,
    453 				ap->a_p, 1)) == EINTR)
    454 				return (error);
    455 			(void) uvm_vnp_uncache(vp);
    456 			np->n_attrstamp = 0;
    457 			if (vp->v_type == VDIR) {
    458 				nfs_invaldircache(vp, 0);
    459 				np->n_direofoffset = 0;
    460 			}
    461 			error = VOP_GETATTR(vp, &vattr, ap->a_cred, ap->a_p);
    462 			if (error)
    463 				return (error);
    464 			np->n_mtime = vattr.va_mtime.tv_sec;
    465 		} else {
    466 			error = VOP_GETATTR(vp, &vattr, ap->a_cred, ap->a_p);
    467 			if (error)
    468 				return (error);
    469 			if (np->n_mtime != vattr.va_mtime.tv_sec) {
    470 				if (vp->v_type == VDIR) {
    471 					nfs_invaldircache(vp, 0);
    472 					np->n_direofoffset = 0;
    473 				}
    474 				if ((error = nfs_vinvalbuf(vp, V_SAVE,
    475 					ap->a_cred, ap->a_p, 1)) == EINTR)
    476 					return (error);
    477 				(void) uvm_vnp_uncache(vp);
    478 				np->n_mtime = vattr.va_mtime.tv_sec;
    479 			}
    480 		}
    481 	}
    482 	if ((nmp->nm_flag & NFSMNT_NQNFS) == 0)
    483 		np->n_attrstamp = 0; /* For Open/Close consistency */
    484 	return (0);
    485 }
    486 
    487 /*
    488  * nfs close vnode op
    489  * What an NFS client should do upon close after writing is a debatable issue.
    490  * Most NFS clients push delayed writes to the server upon close, basically for
    491  * two reasons:
    492  * 1 - So that any write errors may be reported back to the client process
    493  *     doing the close system call. By far the two most likely errors are
    494  *     NFSERR_NOSPC and NFSERR_DQUOT to indicate space allocation failure.
    495  * 2 - To put a worst case upper bound on cache inconsistency between
    496  *     multiple clients for the file.
    497  * There is also a consistency problem for Version 2 of the protocol w.r.t.
    498  * not being able to tell if other clients are writing a file concurrently,
    499  * since there is no way of knowing if the changed modify time in the reply
    500  * is only due to the write for this client.
    501  * (NFS Version 3 provides weak cache consistency data in the reply that
    502  *  should be sufficient to detect and handle this case.)
    503  *
    504  * The current code does the following:
    505  * for NFS Version 2 - play it safe and flush/invalidate all dirty buffers
    506  * for NFS Version 3 - flush dirty buffers to the server but don't invalidate
    507  *                     or commit them (this satisfies 1 and 2 except for the
    508  *                     case where the server crashes after this close but
    509  *                     before the commit RPC, which is felt to be "good
    510  *                     enough". Changing the last argument to nfs_flush() to
    511  *                     a 1 would force a commit operation, if it is felt a
    512  *                     commit is necessary now.
    513  * for NQNFS         - do nothing now, since 2 is dealt with via leases and
    514  *                     1 should be dealt with via an fsync() system call for
    515  *                     cases where write errors are important.
    516  */
    517 /* ARGSUSED */
    518 int
    519 nfs_close(v)
    520 	void *v;
    521 {
    522 	struct vop_close_args /* {
    523 		struct vnodeop_desc *a_desc;
    524 		struct vnode *a_vp;
    525 		int  a_fflag;
    526 		struct ucred *a_cred;
    527 		struct proc *a_p;
    528 	} */ *ap = v;
    529 	register struct vnode *vp = ap->a_vp;
    530 	register struct nfsnode *np = VTONFS(vp);
    531 	int error = 0;
    532 
    533 	if (vp->v_type == VREG) {
    534 	    if ((VFSTONFS(vp->v_mount)->nm_flag & NFSMNT_NQNFS) == 0 &&
    535 		(np->n_flag & NMODIFIED)) {
    536 		if (NFS_ISV3(vp)) {
    537 		    error = nfs_flush(vp, ap->a_cred, MNT_WAIT, ap->a_p, 0);
    538 		    np->n_flag &= ~NMODIFIED;
    539 		} else
    540 		    error = nfs_vinvalbuf(vp, V_SAVE, ap->a_cred, ap->a_p, 1);
    541 		np->n_attrstamp = 0;
    542 	    }
    543 	    if (np->n_flag & NWRITEERR) {
    544 		np->n_flag &= ~NWRITEERR;
    545 		error = np->n_error;
    546 	    }
    547 	}
    548 	return (error);
    549 }
    550 
    551 /*
    552  * nfs getattr call from vfs.
    553  */
    554 int
    555 nfs_getattr(v)
    556 	void *v;
    557 {
    558 	struct vop_getattr_args /* {
    559 		struct vnode *a_vp;
    560 		struct vattr *a_vap;
    561 		struct ucred *a_cred;
    562 		struct proc *a_p;
    563 	} */ *ap = v;
    564 	register struct vnode *vp = ap->a_vp;
    565 	register struct nfsnode *np = VTONFS(vp);
    566 	register caddr_t cp;
    567 	register u_int32_t *tl;
    568 	register int32_t t1, t2;
    569 	caddr_t bpos, dpos;
    570 	int error = 0;
    571 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
    572 	int v3 = NFS_ISV3(vp);
    573 
    574 	/*
    575 	 * Update local times for special files.
    576 	 */
    577 	if (np->n_flag & (NACC | NUPD))
    578 		np->n_flag |= NCHG;
    579 	/*
    580 	 * First look in the cache.
    581 	 */
    582 	if (nfs_getattrcache(vp, ap->a_vap) == 0)
    583 		return (0);
    584 	nfsstats.rpccnt[NFSPROC_GETATTR]++;
    585 	nfsm_reqhead(vp, NFSPROC_GETATTR, NFSX_FH(v3));
    586 	nfsm_fhtom(vp, v3);
    587 	nfsm_request(vp, NFSPROC_GETATTR, ap->a_p, ap->a_cred);
    588 	if (!error) {
    589 		nfsm_loadattr(vp, ap->a_vap);
    590 		if (vp->v_type == VDIR &&
    591 		    ap->a_vap->va_blocksize < NFS_DIRFRAGSIZ)
    592 			ap->a_vap->va_blocksize = NFS_DIRFRAGSIZ;
    593 	}
    594 	nfsm_reqdone;
    595 	return (error);
    596 }
    597 
    598 /*
    599  * nfs setattr call.
    600  */
    601 int
    602 nfs_setattr(v)
    603 	void *v;
    604 {
    605 	struct vop_setattr_args /* {
    606 		struct vnodeop_desc *a_desc;
    607 		struct vnode *a_vp;
    608 		struct vattr *a_vap;
    609 		struct ucred *a_cred;
    610 		struct proc *a_p;
    611 	} */ *ap = v;
    612 	register struct vnode *vp = ap->a_vp;
    613 	register struct nfsnode *np = VTONFS(vp);
    614 	register struct vattr *vap = ap->a_vap;
    615 	int error = 0;
    616 	u_quad_t tsize = 0;
    617 
    618 	/*
    619 	 * Setting of flags is not supported.
    620 	 */
    621 	if (vap->va_flags != VNOVAL)
    622 		return (EOPNOTSUPP);
    623 
    624 	/*
    625 	 * Disallow write attempts if the filesystem is mounted read-only.
    626 	 */
    627   	if ((vap->va_uid != (uid_t)VNOVAL ||
    628 	    vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_sec != VNOVAL ||
    629 	    vap->va_mtime.tv_sec != VNOVAL || vap->va_mode != (mode_t)VNOVAL) &&
    630 	    (vp->v_mount->mnt_flag & MNT_RDONLY))
    631 		return (EROFS);
    632 	if (vap->va_size != VNOVAL) {
    633  		switch (vp->v_type) {
    634  		case VDIR:
    635  			return (EISDIR);
    636  		case VCHR:
    637  		case VBLK:
    638  		case VSOCK:
    639  		case VFIFO:
    640 			if (vap->va_mtime.tv_sec == VNOVAL &&
    641 			    vap->va_atime.tv_sec == VNOVAL &&
    642 			    vap->va_mode == (mode_t)VNOVAL &&
    643 			    vap->va_uid == (uid_t)VNOVAL &&
    644 			    vap->va_gid == (gid_t)VNOVAL)
    645 				return (0);
    646  			vap->va_size = VNOVAL;
    647  			break;
    648  		default:
    649 			/*
    650 			 * Disallow write attempts if the filesystem is
    651 			 * mounted read-only.
    652 			 */
    653 			if (vp->v_mount->mnt_flag & MNT_RDONLY)
    654 				return (EROFS);
    655  			uvm_vnp_setsize(vp, vap->va_size);
    656  			if (vap->va_size == 0)
    657  				error = nfs_vinvalbuf(vp, 0,
    658  				     ap->a_cred, ap->a_p, 1);
    659 			else
    660 				error = nfs_vinvalbuf(vp, V_SAVE,
    661 				     ap->a_cred, ap->a_p, 1);
    662 			if (error) {
    663 				uvm_vnp_setsize(vp, np->n_size);
    664 				return (error);
    665 			}
    666  			tsize = np->n_size;
    667  			np->n_size = np->n_vattr->va_size = vap->va_size;
    668   		}
    669   	} else if ((vap->va_mtime.tv_sec != VNOVAL ||
    670 		vap->va_atime.tv_sec != VNOVAL) &&
    671 		vp->v_type == VREG &&
    672   		(error = nfs_vinvalbuf(vp, V_SAVE, ap->a_cred,
    673 		 ap->a_p, 1)) == EINTR)
    674 		return (error);
    675 	error = nfs_setattrrpc(vp, vap, ap->a_cred, ap->a_p);
    676 	if (error && vap->va_size != VNOVAL) {
    677 		np->n_size = np->n_vattr->va_size = tsize;
    678 		uvm_vnp_setsize(vp, np->n_size);
    679 	}
    680 	return (error);
    681 }
    682 
    683 /*
    684  * Do an nfs setattr rpc.
    685  */
    686 int
    687 nfs_setattrrpc(vp, vap, cred, procp)
    688 	register struct vnode *vp;
    689 	register struct vattr *vap;
    690 	struct ucred *cred;
    691 	struct proc *procp;
    692 {
    693 	register struct nfsv2_sattr *sp;
    694 	register caddr_t cp;
    695 	register int32_t t1, t2;
    696 	caddr_t bpos, dpos, cp2;
    697 	u_int32_t *tl;
    698 	int error = 0, wccflag = NFSV3_WCCRATTR;
    699 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
    700 	int v3 = NFS_ISV3(vp);
    701 
    702 	nfsstats.rpccnt[NFSPROC_SETATTR]++;
    703 	nfsm_reqhead(vp, NFSPROC_SETATTR, NFSX_FH(v3) + NFSX_SATTR(v3));
    704 	nfsm_fhtom(vp, v3);
    705 	if (v3) {
    706 		nfsm_v3attrbuild(vap, TRUE);
    707 		nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
    708 		*tl = nfs_false;
    709 	} else {
    710 		nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
    711 		if (vap->va_mode == (mode_t)VNOVAL)
    712 			sp->sa_mode = nfs_xdrneg1;
    713 		else
    714 			sp->sa_mode = vtonfsv2_mode(vp->v_type, vap->va_mode);
    715 		if (vap->va_uid == (uid_t)VNOVAL)
    716 			sp->sa_uid = nfs_xdrneg1;
    717 		else
    718 			sp->sa_uid = txdr_unsigned(vap->va_uid);
    719 		if (vap->va_gid == (gid_t)VNOVAL)
    720 			sp->sa_gid = nfs_xdrneg1;
    721 		else
    722 			sp->sa_gid = txdr_unsigned(vap->va_gid);
    723 		sp->sa_size = txdr_unsigned(vap->va_size);
    724 		txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
    725 		txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
    726 	}
    727 	nfsm_request(vp, NFSPROC_SETATTR, procp, cred);
    728 	if (v3) {
    729 		nfsm_wcc_data(vp, wccflag);
    730 	} else
    731 		nfsm_loadattr(vp, (struct vattr *)0);
    732 	nfsm_reqdone;
    733 	return (error);
    734 }
    735 
    736 /*
    737  * nfs lookup call, one step at a time...
    738  * First look in cache
    739  * If not found, unlock the directory nfsnode and do the rpc
    740  */
    741 int
    742 nfs_lookup(v)
    743 	void *v;
    744 {
    745 	struct vop_lookup_args /* {
    746 		struct vnodeop_desc *a_desc;
    747 		struct vnode *a_dvp;
    748 		struct vnode **a_vpp;
    749 		struct componentname *a_cnp;
    750 	} */ *ap = v;
    751 	struct componentname *cnp = ap->a_cnp;
    752 	struct vnode *dvp = ap->a_dvp;
    753 	struct vnode **vpp = ap->a_vpp;
    754 	int flags = cnp->cn_flags;
    755 	struct vnode *newvp;
    756 	u_int32_t *tl;
    757 	caddr_t cp;
    758 	int32_t t1, t2;
    759 	struct nfsmount *nmp;
    760 	caddr_t bpos, dpos, cp2;
    761 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
    762 	long len;
    763 	nfsfh_t *fhp;
    764 	struct nfsnode *np;
    765 	int lockparent, wantparent, error = 0, attrflag, fhsize;
    766 	int v3 = NFS_ISV3(dvp);
    767 
    768 	*vpp = NULLVP;
    769 	if ((flags & ISLASTCN) && (dvp->v_mount->mnt_flag & MNT_RDONLY) &&
    770 	    (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
    771 		return (EROFS);
    772 	if (dvp->v_type != VDIR)
    773 		return (ENOTDIR);
    774 
    775 	lockparent = flags & LOCKPARENT;
    776 	wantparent = flags & (LOCKPARENT|WANTPARENT);
    777 	nmp = VFSTONFS(dvp->v_mount);
    778 	np = VTONFS(dvp);
    779 	if ((error = cache_lookup(dvp, vpp, cnp)) != 0) {
    780 		struct vattr vattr;
    781 		int vpid, err2;
    782 
    783 		err2 = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred, cnp->cn_proc);
    784 		if (err2) {
    785 			*vpp = NULLVP;
    786 			return (err2);
    787 		}
    788 
    789 		if (error == ENOENT) {
    790 			if (!VOP_GETATTR(dvp, &vattr, cnp->cn_cred,
    791 			    cnp->cn_proc) && vattr.va_mtime.tv_sec ==
    792 			    VTONFS(dvp)->n_nctime)
    793 				return (ENOENT);
    794 			cache_purge(dvp);
    795 			np->n_nctime = 0;
    796 			goto dorpc;
    797 		}
    798 
    799 		newvp = *vpp;
    800 		vpid = newvp->v_id;
    801 		/*
    802 		 * See the comment starting `Step through' in ufs/ufs_lookup.c
    803 		 * for an explanation of the locking protocol
    804 		 */
    805 		if (dvp == newvp) {
    806 			VREF(newvp);
    807 			error = 0;
    808 		} else
    809 			error = vget(newvp, LK_EXCLUSIVE);
    810 		if (!error) {
    811 			if (vpid == newvp->v_id) {
    812 			   if (!VOP_GETATTR(newvp, &vattr, cnp->cn_cred, cnp->cn_proc)
    813 			    && vattr.va_ctime.tv_sec == VTONFS(newvp)->n_ctime) {
    814 				nfsstats.lookupcache_hits++;
    815 				if (cnp->cn_nameiop != LOOKUP &&
    816 				    (flags & ISLASTCN))
    817 					cnp->cn_flags |= SAVENAME;
    818 				return (0);
    819 			   }
    820 			   cache_purge(newvp);
    821 			}
    822 			vrele(newvp);
    823 		}
    824 		*vpp = NULLVP;
    825 	}
    826 dorpc:
    827 	error = 0;
    828 	newvp = NULLVP;
    829 	nfsstats.lookupcache_misses++;
    830 	nfsstats.rpccnt[NFSPROC_LOOKUP]++;
    831 	len = cnp->cn_namelen;
    832 	nfsm_reqhead(dvp, NFSPROC_LOOKUP,
    833 		NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len));
    834 	nfsm_fhtom(dvp, v3);
    835 	nfsm_strtom(cnp->cn_nameptr, len, NFS_MAXNAMLEN);
    836 	nfsm_request(dvp, NFSPROC_LOOKUP, cnp->cn_proc, cnp->cn_cred);
    837 	if (error) {
    838 		nfsm_postop_attr(dvp, attrflag);
    839 		m_freem(mrep);
    840 		goto nfsmout;
    841 	}
    842 	nfsm_getfh(fhp, fhsize, v3);
    843 
    844 	/*
    845 	 * Handle RENAME case...
    846 	 */
    847 	if (cnp->cn_nameiop == RENAME && wantparent && (flags & ISLASTCN)) {
    848 		if (NFS_CMPFH(np, fhp, fhsize)) {
    849 			m_freem(mrep);
    850 			return (EISDIR);
    851 		}
    852 		error = nfs_nget(dvp->v_mount, fhp, fhsize, &np);
    853 		if (error) {
    854 			m_freem(mrep);
    855 			return (error);
    856 		}
    857 		newvp = NFSTOV(np);
    858 		if (v3) {
    859 			nfsm_postop_attr(newvp, attrflag);
    860 			nfsm_postop_attr(dvp, attrflag);
    861 		} else
    862 			nfsm_loadattr(newvp, (struct vattr *)0);
    863 		*vpp = newvp;
    864 		m_freem(mrep);
    865 		cnp->cn_flags |= SAVENAME;
    866 		return (0);
    867 	}
    868 
    869 	if (NFS_CMPFH(np, fhp, fhsize)) {
    870 		VREF(dvp);
    871 		newvp = dvp;
    872 	} else {
    873 		error = nfs_nget(dvp->v_mount, fhp, fhsize, &np);
    874 		if (error) {
    875 			m_freem(mrep);
    876 			return (error);
    877 		}
    878 		newvp = NFSTOV(np);
    879 	}
    880 	if (v3) {
    881 		nfsm_postop_attr(newvp, attrflag);
    882 		nfsm_postop_attr(dvp, attrflag);
    883 	} else
    884 		nfsm_loadattr(newvp, (struct vattr *)0);
    885 	if (cnp->cn_nameiop != LOOKUP && (flags & ISLASTCN))
    886 		cnp->cn_flags |= SAVENAME;
    887 	if ((cnp->cn_flags & MAKEENTRY) &&
    888 	    (cnp->cn_nameiop != DELETE || !(flags & ISLASTCN))) {
    889 		np->n_ctime = np->n_vattr->va_ctime.tv_sec;
    890 		cache_enter(dvp, newvp, cnp);
    891 	}
    892 	*vpp = newvp;
    893 	nfsm_reqdone;
    894 	if (error) {
    895 		if (error == ENOENT && (cnp->cn_flags & MAKEENTRY) &&
    896 		    cnp->cn_nameiop != CREATE) {
    897 			if (VTONFS(dvp)->n_nctime == 0)
    898 				VTONFS(dvp)->n_nctime =
    899 				    VTONFS(dvp)->n_vattr->va_mtime.tv_sec;
    900 			cache_enter(dvp, NULL, cnp);
    901 		}
    902 		if (newvp != NULLVP)
    903 			vrele(newvp);
    904 		if ((cnp->cn_nameiop == CREATE || cnp->cn_nameiop == RENAME) &&
    905 		    (flags & ISLASTCN) && error == ENOENT) {
    906 			if (dvp->v_mount->mnt_flag & MNT_RDONLY)
    907 				error = EROFS;
    908 			else
    909 				error = EJUSTRETURN;
    910 		}
    911 		if (cnp->cn_nameiop != LOOKUP && (flags & ISLASTCN))
    912 			cnp->cn_flags |= SAVENAME;
    913 	}
    914 	return (error);
    915 }
    916 
    917 /*
    918  * nfs read call.
    919  * Just call nfs_bioread() to do the work.
    920  */
    921 int
    922 nfs_read(v)
    923 	void *v;
    924 {
    925 	struct vop_read_args /* {
    926 		struct vnode *a_vp;
    927 		struct uio *a_uio;
    928 		int  a_ioflag;
    929 		struct ucred *a_cred;
    930 	} */ *ap = v;
    931 	register struct vnode *vp = ap->a_vp;
    932 
    933 	if (vp->v_type != VREG)
    934 		return (EPERM);
    935 	return (nfs_bioread(vp, ap->a_uio, ap->a_ioflag, ap->a_cred, 0));
    936 }
    937 
    938 /*
    939  * nfs readlink call
    940  */
    941 int
    942 nfs_readlink(v)
    943 	void *v;
    944 {
    945 	struct vop_readlink_args /* {
    946 		struct vnode *a_vp;
    947 		struct uio *a_uio;
    948 		struct ucred *a_cred;
    949 	} */ *ap = v;
    950 	register struct vnode *vp = ap->a_vp;
    951 
    952 	if (vp->v_type != VLNK)
    953 		return (EPERM);
    954 	return (nfs_bioread(vp, ap->a_uio, 0, ap->a_cred, 0));
    955 }
    956 
    957 /*
    958  * Do a readlink rpc.
    959  * Called by nfs_doio() from below the buffer cache.
    960  */
    961 int
    962 nfs_readlinkrpc(vp, uiop, cred)
    963 	register struct vnode *vp;
    964 	struct uio *uiop;
    965 	struct ucred *cred;
    966 {
    967 	register u_int32_t *tl;
    968 	register caddr_t cp;
    969 	register int32_t t1, t2;
    970 	caddr_t bpos, dpos, cp2;
    971 	int error = 0, len, attrflag;
    972 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
    973 	int v3 = NFS_ISV3(vp);
    974 
    975 	nfsstats.rpccnt[NFSPROC_READLINK]++;
    976 	nfsm_reqhead(vp, NFSPROC_READLINK, NFSX_FH(v3));
    977 	nfsm_fhtom(vp, v3);
    978 	nfsm_request(vp, NFSPROC_READLINK, uiop->uio_procp, cred);
    979 	if (v3)
    980 		nfsm_postop_attr(vp, attrflag);
    981 	if (!error) {
    982 		nfsm_strsiz(len, NFS_MAXPATHLEN);
    983 		nfsm_mtouio(uiop, len);
    984 	}
    985 	nfsm_reqdone;
    986 	return (error);
    987 }
    988 
    989 /*
    990  * nfs read rpc call
    991  * Ditto above
    992  */
    993 int
    994 nfs_readrpc(vp, uiop, cred)
    995 	register struct vnode *vp;
    996 	struct uio *uiop;
    997 	struct ucred *cred;
    998 {
    999 	register u_int32_t *tl;
   1000 	register caddr_t cp;
   1001 	register int32_t t1, t2;
   1002 	caddr_t bpos, dpos, cp2;
   1003 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
   1004 	struct nfsmount *nmp;
   1005 	int error = 0, len, retlen, tsiz, eof, attrflag;
   1006 	int v3 = NFS_ISV3(vp);
   1007 
   1008 #ifndef nolint
   1009 	eof = 0;
   1010 #endif
   1011 	nmp = VFSTONFS(vp->v_mount);
   1012 	tsiz = uiop->uio_resid;
   1013 	if (uiop->uio_offset + tsiz > nmp->nm_maxfilesize)
   1014 		return (EFBIG);
   1015 	while (tsiz > 0) {
   1016 		nfsstats.rpccnt[NFSPROC_READ]++;
   1017 		len = (tsiz > nmp->nm_rsize) ? nmp->nm_rsize : tsiz;
   1018 		nfsm_reqhead(vp, NFSPROC_READ, NFSX_FH(v3) + NFSX_UNSIGNED * 3);
   1019 		nfsm_fhtom(vp, v3);
   1020 		nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED * 3);
   1021 		if (v3) {
   1022 			txdr_hyper(uiop->uio_offset, tl);
   1023 			*(tl + 2) = txdr_unsigned(len);
   1024 		} else {
   1025 			*tl++ = txdr_unsigned(uiop->uio_offset);
   1026 			*tl++ = txdr_unsigned(len);
   1027 			*tl = 0;
   1028 		}
   1029 		nfsm_request(vp, NFSPROC_READ, uiop->uio_procp, cred);
   1030 		if (v3) {
   1031 			nfsm_postop_attr(vp, attrflag);
   1032 			if (error) {
   1033 				m_freem(mrep);
   1034 				goto nfsmout;
   1035 			}
   1036 			nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
   1037 			eof = fxdr_unsigned(int, *(tl + 1));
   1038 		} else
   1039 			nfsm_loadattr(vp, (struct vattr *)0);
   1040 		nfsm_strsiz(retlen, nmp->nm_rsize);
   1041 		nfsm_mtouio(uiop, retlen);
   1042 		m_freem(mrep);
   1043 		tsiz -= retlen;
   1044 		if (v3) {
   1045 			if (eof || retlen == 0)
   1046 				tsiz = 0;
   1047 		} else if (retlen < len)
   1048 			tsiz = 0;
   1049 	}
   1050 nfsmout:
   1051 	return (error);
   1052 }
   1053 
   1054 /*
   1055  * nfs write call
   1056  */
   1057 int
   1058 nfs_writerpc(vp, uiop, cred, iomode, must_commit)
   1059 	register struct vnode *vp;
   1060 	register struct uio *uiop;
   1061 	struct ucred *cred;
   1062 	int *iomode, *must_commit;
   1063 {
   1064 	register u_int32_t *tl;
   1065 	register caddr_t cp;
   1066 	register int32_t t1, t2, backup;
   1067 	caddr_t bpos, dpos, cp2;
   1068 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
   1069 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
   1070 	int error = 0, len, tsiz, wccflag = NFSV3_WCCRATTR, rlen, commit;
   1071 	int v3 = NFS_ISV3(vp), committed = NFSV3WRITE_FILESYNC;
   1072 
   1073 #ifndef DIAGNOSTIC
   1074 	if (uiop->uio_iovcnt != 1)
   1075 		panic("nfs: writerpc iovcnt > 1");
   1076 #endif
   1077 	*must_commit = 0;
   1078 	tsiz = uiop->uio_resid;
   1079 	if (uiop->uio_offset + tsiz > nmp->nm_maxfilesize)
   1080 		return (EFBIG);
   1081 	while (tsiz > 0) {
   1082 		nfsstats.rpccnt[NFSPROC_WRITE]++;
   1083 		len = (tsiz > nmp->nm_wsize) ? nmp->nm_wsize : tsiz;
   1084 		nfsm_reqhead(vp, NFSPROC_WRITE,
   1085 			NFSX_FH(v3) + 5 * NFSX_UNSIGNED + nfsm_rndup(len));
   1086 		nfsm_fhtom(vp, v3);
   1087 		if (v3) {
   1088 			nfsm_build(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
   1089 			txdr_hyper(uiop->uio_offset, tl);
   1090 			tl += 2;
   1091 			*tl++ = txdr_unsigned(len);
   1092 			*tl++ = txdr_unsigned(*iomode);
   1093 			*tl = txdr_unsigned(len);
   1094 		} else {
   1095 			register u_int32_t x;
   1096 
   1097 			nfsm_build(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
   1098 			/* Set both "begin" and "current" to non-garbage. */
   1099 			x = txdr_unsigned((u_int32_t)uiop->uio_offset);
   1100 			*tl++ = x;      /* "begin offset" */
   1101 			*tl++ = x;      /* "current offset" */
   1102 			x = txdr_unsigned(len);
   1103 			*tl++ = x;      /* total to this offset */
   1104 			*tl = x;        /* size of this write */
   1105 
   1106 		}
   1107 		nfsm_uiotom(uiop, len);
   1108 		nfsm_request(vp, NFSPROC_WRITE, uiop->uio_procp, cred);
   1109 		if (v3) {
   1110 			wccflag = NFSV3_WCCCHK;
   1111 			nfsm_wcc_data(vp, wccflag);
   1112 			if (!error) {
   1113 				nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED
   1114 					+ NFSX_V3WRITEVERF);
   1115 				rlen = fxdr_unsigned(int, *tl++);
   1116 				if (rlen == 0) {
   1117 					error = NFSERR_IO;
   1118 					m_freem(mrep);
   1119 					break;
   1120 				} else if (rlen < len) {
   1121 					backup = len - rlen;
   1122 					(caddr_t)uiop->uio_iov->iov_base -=
   1123 					    backup;
   1124 					uiop->uio_iov->iov_len += backup;
   1125 					uiop->uio_offset -= backup;
   1126 					uiop->uio_resid += backup;
   1127 					len = rlen;
   1128 				}
   1129 				commit = fxdr_unsigned(int, *tl++);
   1130 
   1131 				/*
   1132 				 * Return the lowest committment level
   1133 				 * obtained by any of the RPCs.
   1134 				 */
   1135 				if (committed == NFSV3WRITE_FILESYNC)
   1136 					committed = commit;
   1137 				else if (committed == NFSV3WRITE_DATASYNC &&
   1138 					commit == NFSV3WRITE_UNSTABLE)
   1139 					committed = commit;
   1140 				if ((nmp->nm_iflag & NFSMNT_HASWRITEVERF) == 0){
   1141 				    memcpy((caddr_t)nmp->nm_verf, (caddr_t)tl,
   1142 					NFSX_V3WRITEVERF);
   1143 				    nmp->nm_iflag |= NFSMNT_HASWRITEVERF;
   1144 				} else if (memcmp((caddr_t)tl,
   1145 				    (caddr_t)nmp->nm_verf, NFSX_V3WRITEVERF)) {
   1146 				    *must_commit = 1;
   1147 				    memcpy((caddr_t)nmp->nm_verf, (caddr_t)tl,
   1148 					NFSX_V3WRITEVERF);
   1149 				}
   1150 			}
   1151 		} else
   1152 		    nfsm_loadattr(vp, (struct vattr *)0);
   1153 		if (wccflag)
   1154 		    VTONFS(vp)->n_mtime = VTONFS(vp)->n_vattr->va_mtime.tv_sec;
   1155 		m_freem(mrep);
   1156 		if (error)
   1157 			break;
   1158 		tsiz -= len;
   1159 	}
   1160 nfsmout:
   1161 	*iomode = committed;
   1162 	if (error)
   1163 		uiop->uio_resid = tsiz;
   1164 	return (error);
   1165 }
   1166 
   1167 /*
   1168  * nfs mknod rpc
   1169  * For NFS v2 this is a kludge. Use a create rpc but with the IFMT bits of the
   1170  * mode set to specify the file type and the size field for rdev.
   1171  */
   1172 int
   1173 nfs_mknodrpc(dvp, vpp, cnp, vap)
   1174 	register struct vnode *dvp;
   1175 	register struct vnode **vpp;
   1176 	register struct componentname *cnp;
   1177 	register struct vattr *vap;
   1178 {
   1179 	register struct nfsv2_sattr *sp;
   1180 	register u_int32_t *tl;
   1181 	register caddr_t cp;
   1182 	register int32_t t1, t2;
   1183 	struct vnode *newvp = (struct vnode *)0;
   1184 	struct nfsnode *np;
   1185 	char *cp2;
   1186 	caddr_t bpos, dpos;
   1187 	int error = 0, wccflag = NFSV3_WCCRATTR, gotvp = 0;
   1188 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
   1189 	u_int32_t rdev;
   1190 	int v3 = NFS_ISV3(dvp);
   1191 
   1192 	if (vap->va_type == VCHR || vap->va_type == VBLK)
   1193 		rdev = txdr_unsigned(vap->va_rdev);
   1194 	else if (vap->va_type == VFIFO || vap->va_type == VSOCK)
   1195 		rdev = nfs_xdrneg1;
   1196 	else {
   1197 		VOP_ABORTOP(dvp, cnp);
   1198 		vput(dvp);
   1199 		return (EOPNOTSUPP);
   1200 	}
   1201 	nfsstats.rpccnt[NFSPROC_MKNOD]++;
   1202 	nfsm_reqhead(dvp, NFSPROC_MKNOD, NFSX_FH(v3) + 4 * NFSX_UNSIGNED +
   1203 		+ nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(v3));
   1204 	nfsm_fhtom(dvp, v3);
   1205 	nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
   1206 	if (v3) {
   1207 		nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
   1208 		*tl++ = vtonfsv3_type(vap->va_type);
   1209 		nfsm_v3attrbuild(vap, FALSE);
   1210 		if (vap->va_type == VCHR || vap->va_type == VBLK) {
   1211 			nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
   1212 			*tl++ = txdr_unsigned(major(vap->va_rdev));
   1213 			*tl = txdr_unsigned(minor(vap->va_rdev));
   1214 		}
   1215 	} else {
   1216 		nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
   1217 		sp->sa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
   1218 		sp->sa_uid = nfs_xdrneg1;
   1219 		sp->sa_gid = nfs_xdrneg1;
   1220 		sp->sa_size = rdev;
   1221 		txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
   1222 		txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
   1223 	}
   1224 	nfsm_request(dvp, NFSPROC_MKNOD, cnp->cn_proc, cnp->cn_cred);
   1225 	if (!error) {
   1226 		nfsm_mtofh(dvp, newvp, v3, gotvp);
   1227 		if (!gotvp) {
   1228 			if (newvp) {
   1229 				vrele(newvp);
   1230 				newvp = (struct vnode *)0;
   1231 			}
   1232 			error = nfs_lookitup(dvp, cnp->cn_nameptr,
   1233 			    cnp->cn_namelen, cnp->cn_cred, cnp->cn_proc, &np);
   1234 			if (!error)
   1235 				newvp = NFSTOV(np);
   1236 		}
   1237 	}
   1238 	if (v3)
   1239 		nfsm_wcc_data(dvp, wccflag);
   1240 	nfsm_reqdone;
   1241 	if (error) {
   1242 		if (newvp)
   1243 			vrele(newvp);
   1244 	} else {
   1245 		if (cnp->cn_flags & MAKEENTRY)
   1246 			cache_enter(dvp, newvp, cnp);
   1247 		*vpp = newvp;
   1248 	}
   1249 	FREE(cnp->cn_pnbuf, M_NAMEI);
   1250 	VTONFS(dvp)->n_flag |= NMODIFIED;
   1251 	if (!wccflag)
   1252 		VTONFS(dvp)->n_attrstamp = 0;
   1253 	vrele(dvp);
   1254 	return (error);
   1255 }
   1256 
   1257 /*
   1258  * nfs mknod vop
   1259  * just call nfs_mknodrpc() to do the work.
   1260  */
   1261 /* ARGSUSED */
   1262 int
   1263 nfs_mknod(v)
   1264 	void *v;
   1265 {
   1266 	struct vop_mknod_args /* {
   1267 		struct vnode *a_dvp;
   1268 		struct vnode **a_vpp;
   1269 		struct componentname *a_cnp;
   1270 		struct vattr *a_vap;
   1271 	} */ *ap = v;
   1272 	struct vnode *newvp;
   1273 	int error;
   1274 
   1275 	error = nfs_mknodrpc(ap->a_dvp, &newvp, ap->a_cnp, ap->a_vap);
   1276 	if (!error)
   1277 		vrele(newvp);
   1278 	return (error);
   1279 }
   1280 
   1281 static u_long create_verf;
   1282 /*
   1283  * nfs file create call
   1284  */
   1285 int
   1286 nfs_create(v)
   1287 	void *v;
   1288 {
   1289 	struct vop_create_args /* {
   1290 		struct vnode *a_dvp;
   1291 		struct vnode **a_vpp;
   1292 		struct componentname *a_cnp;
   1293 		struct vattr *a_vap;
   1294 	} */ *ap = v;
   1295 	register struct vnode *dvp = ap->a_dvp;
   1296 	register struct vattr *vap = ap->a_vap;
   1297 	register struct componentname *cnp = ap->a_cnp;
   1298 	register struct nfsv2_sattr *sp;
   1299 	register u_int32_t *tl;
   1300 	register caddr_t cp;
   1301 	register int32_t t1, t2;
   1302 	struct nfsnode *np = (struct nfsnode *)0;
   1303 	struct vnode *newvp = (struct vnode *)0;
   1304 	caddr_t bpos, dpos, cp2;
   1305 	int error = 0, wccflag = NFSV3_WCCRATTR, gotvp = 0, fmode = 0;
   1306 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
   1307 	int v3 = NFS_ISV3(dvp);
   1308 
   1309 	/*
   1310 	 * Oops, not for me..
   1311 	 */
   1312 	if (vap->va_type == VSOCK)
   1313 		return (nfs_mknodrpc(dvp, ap->a_vpp, cnp, vap));
   1314 
   1315 #ifdef VA_EXCLUSIVE
   1316 	if (vap->va_vaflags & VA_EXCLUSIVE)
   1317 		fmode |= O_EXCL;
   1318 #endif
   1319 again:
   1320 	nfsstats.rpccnt[NFSPROC_CREATE]++;
   1321 	nfsm_reqhead(dvp, NFSPROC_CREATE, NFSX_FH(v3) + 2 * NFSX_UNSIGNED +
   1322 		nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(v3));
   1323 	nfsm_fhtom(dvp, v3);
   1324 	nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
   1325 	if (v3) {
   1326 		nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
   1327 		if (fmode & O_EXCL) {
   1328 			*tl = txdr_unsigned(NFSV3CREATE_EXCLUSIVE);
   1329 			nfsm_build(tl, u_int32_t *, NFSX_V3CREATEVERF);
   1330 			if (in_ifaddr.tqh_first)
   1331 				*tl++ = in_ifaddr.tqh_first->ia_addr.sin_addr.s_addr;
   1332 			else
   1333 				*tl++ = create_verf;
   1334 			*tl = ++create_verf;
   1335 		} else {
   1336 			*tl = txdr_unsigned(NFSV3CREATE_UNCHECKED);
   1337 			nfsm_v3attrbuild(vap, FALSE);
   1338 		}
   1339 	} else {
   1340 		nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
   1341 		sp->sa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
   1342 		sp->sa_uid = nfs_xdrneg1;
   1343 		sp->sa_gid = nfs_xdrneg1;
   1344 		sp->sa_size = 0;
   1345 		txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
   1346 		txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
   1347 	}
   1348 	nfsm_request(dvp, NFSPROC_CREATE, cnp->cn_proc, cnp->cn_cred);
   1349 	if (!error) {
   1350 		nfsm_mtofh(dvp, newvp, v3, gotvp);
   1351 		if (!gotvp) {
   1352 			if (newvp) {
   1353 				vrele(newvp);
   1354 				newvp = (struct vnode *)0;
   1355 			}
   1356 			error = nfs_lookitup(dvp, cnp->cn_nameptr,
   1357 			    cnp->cn_namelen, cnp->cn_cred, cnp->cn_proc, &np);
   1358 			if (!error)
   1359 				newvp = NFSTOV(np);
   1360 		}
   1361 	}
   1362 	if (v3)
   1363 		nfsm_wcc_data(dvp, wccflag);
   1364 	nfsm_reqdone;
   1365 	if (error) {
   1366 		if (v3 && (fmode & O_EXCL) && error == NFSERR_NOTSUPP) {
   1367 			fmode &= ~O_EXCL;
   1368 			goto again;
   1369 		}
   1370 		if (newvp)
   1371 			vrele(newvp);
   1372 	} else if (v3 && (fmode & O_EXCL))
   1373 		error = nfs_setattrrpc(newvp, vap, cnp->cn_cred, cnp->cn_proc);
   1374 	if (!error) {
   1375 		if (cnp->cn_flags & MAKEENTRY)
   1376 			cache_enter(dvp, newvp, cnp);
   1377 		*ap->a_vpp = newvp;
   1378 	}
   1379 	FREE(cnp->cn_pnbuf, M_NAMEI);
   1380 	VTONFS(dvp)->n_flag |= NMODIFIED;
   1381 	if (!wccflag)
   1382 		VTONFS(dvp)->n_attrstamp = 0;
   1383 	vrele(dvp);
   1384 	return (error);
   1385 }
   1386 
   1387 /*
   1388  * nfs file remove call
   1389  * To try and make nfs semantics closer to ufs semantics, a file that has
   1390  * other processes using the vnode is renamed instead of removed and then
   1391  * removed later on the last close.
   1392  * - If v_usecount > 1
   1393  *	  If a rename is not already in the works
   1394  *	     call nfs_sillyrename() to set it up
   1395  *     else
   1396  *	  do the remove rpc
   1397  */
   1398 int
   1399 nfs_remove(v)
   1400 	void *v;
   1401 {
   1402 	struct vop_remove_args /* {
   1403 		struct vnodeop_desc *a_desc;
   1404 		struct vnode * a_dvp;
   1405 		struct vnode * a_vp;
   1406 		struct componentname * a_cnp;
   1407 	} */ *ap = v;
   1408 	register struct vnode *vp = ap->a_vp;
   1409 	register struct vnode *dvp = ap->a_dvp;
   1410 	register struct componentname *cnp = ap->a_cnp;
   1411 	register struct nfsnode *np = VTONFS(vp);
   1412 	int error = 0;
   1413 	struct vattr vattr;
   1414 
   1415 #ifndef DIAGNOSTIC
   1416 	if ((cnp->cn_flags & HASBUF) == 0)
   1417 		panic("nfs_remove: no name");
   1418 	if (vp->v_usecount < 1)
   1419 		panic("nfs_remove: bad v_usecount");
   1420 #endif
   1421 	if (vp->v_type == VDIR)
   1422 		error = EPERM;
   1423 	else if (vp->v_usecount == 1 || (np->n_sillyrename &&
   1424 	    VOP_GETATTR(vp, &vattr, cnp->cn_cred, cnp->cn_proc) == 0 &&
   1425 	    vattr.va_nlink > 1)) {
   1426 		/*
   1427 		 * Purge the name cache so that the chance of a lookup for
   1428 		 * the name succeeding while the remove is in progress is
   1429 		 * minimized. Without node locking it can still happen, such
   1430 		 * that an I/O op returns ESTALE, but since you get this if
   1431 		 * another host removes the file..
   1432 		 */
   1433 		cache_purge(vp);
   1434 		/*
   1435 		 * throw away biocache buffers, mainly to avoid
   1436 		 * unnecessary delayed writes later.
   1437 		 */
   1438 		error = nfs_vinvalbuf(vp, 0, cnp->cn_cred, cnp->cn_proc, 1);
   1439 		/* Do the rpc */
   1440 		if (error != EINTR)
   1441 			error = nfs_removerpc(dvp, cnp->cn_nameptr,
   1442 				cnp->cn_namelen, cnp->cn_cred, cnp->cn_proc);
   1443 		/*
   1444 		 * Kludge City: If the first reply to the remove rpc is lost..
   1445 		 *   the reply to the retransmitted request will be ENOENT
   1446 		 *   since the file was in fact removed
   1447 		 *   Therefore, we cheat and return success.
   1448 		 */
   1449 		if (error == ENOENT)
   1450 			error = 0;
   1451 	} else if (!np->n_sillyrename)
   1452 		error = nfs_sillyrename(dvp, vp, cnp);
   1453 	FREE(cnp->cn_pnbuf, M_NAMEI);
   1454 	np->n_attrstamp = 0;
   1455 	vrele(dvp);
   1456 	vrele(vp);
   1457 	return (error);
   1458 }
   1459 
   1460 /*
   1461  * nfs file remove rpc called from nfs_inactive
   1462  */
   1463 int
   1464 nfs_removeit(sp)
   1465 	register struct sillyrename *sp;
   1466 {
   1467 
   1468 	return (nfs_removerpc(sp->s_dvp, sp->s_name, sp->s_namlen, sp->s_cred,
   1469 		(struct proc *)0));
   1470 }
   1471 
   1472 /*
   1473  * Nfs remove rpc, called from nfs_remove() and nfs_removeit().
   1474  */
   1475 int
   1476 nfs_removerpc(dvp, name, namelen, cred, proc)
   1477 	register struct vnode *dvp;
   1478 	const char *name;
   1479 	int namelen;
   1480 	struct ucred *cred;
   1481 	struct proc *proc;
   1482 {
   1483 	register u_int32_t *tl;
   1484 	register caddr_t cp;
   1485 	register int32_t t1, t2;
   1486 	caddr_t bpos, dpos, cp2;
   1487 	int error = 0, wccflag = NFSV3_WCCRATTR;
   1488 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
   1489 	int v3 = NFS_ISV3(dvp);
   1490 
   1491 	nfsstats.rpccnt[NFSPROC_REMOVE]++;
   1492 	nfsm_reqhead(dvp, NFSPROC_REMOVE,
   1493 		NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(namelen));
   1494 	nfsm_fhtom(dvp, v3);
   1495 	nfsm_strtom(name, namelen, NFS_MAXNAMLEN);
   1496 	nfsm_request(dvp, NFSPROC_REMOVE, proc, cred);
   1497 	if (v3)
   1498 		nfsm_wcc_data(dvp, wccflag);
   1499 	nfsm_reqdone;
   1500 	VTONFS(dvp)->n_flag |= NMODIFIED;
   1501 	if (!wccflag)
   1502 		VTONFS(dvp)->n_attrstamp = 0;
   1503 	return (error);
   1504 }
   1505 
   1506 /*
   1507  * nfs file rename call
   1508  */
   1509 int
   1510 nfs_rename(v)
   1511 	void *v;
   1512 {
   1513 	struct vop_rename_args  /* {
   1514 		struct vnode *a_fdvp;
   1515 		struct vnode *a_fvp;
   1516 		struct componentname *a_fcnp;
   1517 		struct vnode *a_tdvp;
   1518 		struct vnode *a_tvp;
   1519 		struct componentname *a_tcnp;
   1520 	} */ *ap = v;
   1521 	register struct vnode *fvp = ap->a_fvp;
   1522 	register struct vnode *tvp = ap->a_tvp;
   1523 	register struct vnode *fdvp = ap->a_fdvp;
   1524 	register struct vnode *tdvp = ap->a_tdvp;
   1525 	register struct componentname *tcnp = ap->a_tcnp;
   1526 	register struct componentname *fcnp = ap->a_fcnp;
   1527 	int error;
   1528 
   1529 #ifndef DIAGNOSTIC
   1530 	if ((tcnp->cn_flags & HASBUF) == 0 ||
   1531 	    (fcnp->cn_flags & HASBUF) == 0)
   1532 		panic("nfs_rename: no name");
   1533 #endif
   1534 	/* Check for cross-device rename */
   1535 	if ((fvp->v_mount != tdvp->v_mount) ||
   1536 	    (tvp && (fvp->v_mount != tvp->v_mount))) {
   1537 		error = EXDEV;
   1538 		goto out;
   1539 	}
   1540 
   1541 	/*
   1542 	 * If the tvp exists and is in use, sillyrename it before doing the
   1543 	 * rename of the new file over it.
   1544 	 */
   1545 	if (tvp && tvp->v_usecount > 1 && !VTONFS(tvp)->n_sillyrename &&
   1546 		tvp->v_type != VDIR && !nfs_sillyrename(tdvp, tvp, tcnp)) {
   1547 		vrele(tvp);
   1548 		tvp = NULL;
   1549 	}
   1550 
   1551 	error = nfs_renamerpc(fdvp, fcnp->cn_nameptr, fcnp->cn_namelen,
   1552 		tdvp, tcnp->cn_nameptr, tcnp->cn_namelen, tcnp->cn_cred,
   1553 		tcnp->cn_proc);
   1554 
   1555 	if (fvp->v_type == VDIR) {
   1556 		if (tvp != NULL && tvp->v_type == VDIR)
   1557 			cache_purge(tdvp);
   1558 		cache_purge(fdvp);
   1559 	}
   1560 out:
   1561 	if (tdvp == tvp)
   1562 		vrele(tdvp);
   1563 	else
   1564 		vput(tdvp);
   1565 	if (tvp)
   1566 		vput(tvp);
   1567 	vrele(fdvp);
   1568 	vrele(fvp);
   1569 	/*
   1570 	 * Kludge: Map ENOENT => 0 assuming that it is a reply to a retry.
   1571 	 */
   1572 	if (error == ENOENT)
   1573 		error = 0;
   1574 	return (error);
   1575 }
   1576 
   1577 /*
   1578  * nfs file rename rpc called from nfs_remove() above
   1579  */
   1580 int
   1581 nfs_renameit(sdvp, scnp, sp)
   1582 	struct vnode *sdvp;
   1583 	struct componentname *scnp;
   1584 	register struct sillyrename *sp;
   1585 {
   1586 	return (nfs_renamerpc(sdvp, scnp->cn_nameptr, scnp->cn_namelen,
   1587 		sdvp, sp->s_name, sp->s_namlen, scnp->cn_cred, scnp->cn_proc));
   1588 }
   1589 
   1590 /*
   1591  * Do an nfs rename rpc. Called from nfs_rename() and nfs_renameit().
   1592  */
   1593 int
   1594 nfs_renamerpc(fdvp, fnameptr, fnamelen, tdvp, tnameptr, tnamelen, cred, proc)
   1595 	register struct vnode *fdvp;
   1596 	const char *fnameptr;
   1597 	int fnamelen;
   1598 	register struct vnode *tdvp;
   1599 	const char *tnameptr;
   1600 	int tnamelen;
   1601 	struct ucred *cred;
   1602 	struct proc *proc;
   1603 {
   1604 	register u_int32_t *tl;
   1605 	register caddr_t cp;
   1606 	register int32_t t1, t2;
   1607 	caddr_t bpos, dpos, cp2;
   1608 	int error = 0, fwccflag = NFSV3_WCCRATTR, twccflag = NFSV3_WCCRATTR;
   1609 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
   1610 	int v3 = NFS_ISV3(fdvp);
   1611 
   1612 	nfsstats.rpccnt[NFSPROC_RENAME]++;
   1613 	nfsm_reqhead(fdvp, NFSPROC_RENAME,
   1614 		(NFSX_FH(v3) + NFSX_UNSIGNED)*2 + nfsm_rndup(fnamelen) +
   1615 		nfsm_rndup(tnamelen));
   1616 	nfsm_fhtom(fdvp, v3);
   1617 	nfsm_strtom(fnameptr, fnamelen, NFS_MAXNAMLEN);
   1618 	nfsm_fhtom(tdvp, v3);
   1619 	nfsm_strtom(tnameptr, tnamelen, NFS_MAXNAMLEN);
   1620 	nfsm_request(fdvp, NFSPROC_RENAME, proc, cred);
   1621 	if (v3) {
   1622 		nfsm_wcc_data(fdvp, fwccflag);
   1623 		nfsm_wcc_data(tdvp, twccflag);
   1624 	}
   1625 	nfsm_reqdone;
   1626 	VTONFS(fdvp)->n_flag |= NMODIFIED;
   1627 	VTONFS(tdvp)->n_flag |= NMODIFIED;
   1628 	if (!fwccflag)
   1629 		VTONFS(fdvp)->n_attrstamp = 0;
   1630 	if (!twccflag)
   1631 		VTONFS(tdvp)->n_attrstamp = 0;
   1632 	return (error);
   1633 }
   1634 
   1635 /*
   1636  * nfs hard link create call
   1637  */
   1638 int
   1639 nfs_link(v)
   1640 	void *v;
   1641 {
   1642 	struct vop_link_args /* {
   1643 		struct vnode *a_dvp;
   1644 		struct vnode *a_vp;
   1645 		struct componentname *a_cnp;
   1646 	} */ *ap = v;
   1647 	register struct vnode *vp = ap->a_vp;
   1648 	register struct vnode *dvp = ap->a_dvp;
   1649 	register struct componentname *cnp = ap->a_cnp;
   1650 	register u_int32_t *tl;
   1651 	register caddr_t cp;
   1652 	register int32_t t1, t2;
   1653 	caddr_t bpos, dpos, cp2;
   1654 	int error = 0, wccflag = NFSV3_WCCRATTR, attrflag = 0;
   1655 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
   1656 	int v3;
   1657 
   1658 	if (dvp->v_mount != vp->v_mount) {
   1659 		VOP_ABORTOP(vp, cnp);
   1660 		vput(dvp);
   1661 		return (EXDEV);
   1662 	}
   1663 
   1664 	/*
   1665 	 * Push all writes to the server, so that the attribute cache
   1666 	 * doesn't get "out of sync" with the server.
   1667 	 * XXX There should be a better way!
   1668 	 */
   1669 	VOP_FSYNC(vp, cnp->cn_cred, FSYNC_WAIT, cnp->cn_proc);
   1670 
   1671 	v3 = NFS_ISV3(vp);
   1672 	nfsstats.rpccnt[NFSPROC_LINK]++;
   1673 	nfsm_reqhead(vp, NFSPROC_LINK,
   1674 		NFSX_FH(v3)*2 + NFSX_UNSIGNED + nfsm_rndup(cnp->cn_namelen));
   1675 	nfsm_fhtom(vp, v3);
   1676 	nfsm_fhtom(dvp, v3);
   1677 	nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
   1678 	nfsm_request(vp, NFSPROC_LINK, cnp->cn_proc, cnp->cn_cred);
   1679 	if (v3) {
   1680 		nfsm_postop_attr(vp, attrflag);
   1681 		nfsm_wcc_data(dvp, wccflag);
   1682 	}
   1683 	nfsm_reqdone;
   1684 	FREE(cnp->cn_pnbuf, M_NAMEI);
   1685 	VTONFS(dvp)->n_flag |= NMODIFIED;
   1686 	if (!attrflag)
   1687 		VTONFS(vp)->n_attrstamp = 0;
   1688 	if (!wccflag)
   1689 		VTONFS(dvp)->n_attrstamp = 0;
   1690 	vput(dvp);
   1691 	/*
   1692 	 * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry.
   1693 	 */
   1694 	if (error == EEXIST)
   1695 		error = 0;
   1696 	return (error);
   1697 }
   1698 
   1699 /*
   1700  * nfs symbolic link create call
   1701  */
   1702 int
   1703 nfs_symlink(v)
   1704 	void *v;
   1705 {
   1706 	struct vop_symlink_args /* {
   1707 		struct vnode *a_dvp;
   1708 		struct vnode **a_vpp;
   1709 		struct componentname *a_cnp;
   1710 		struct vattr *a_vap;
   1711 		char *a_target;
   1712 	} */ *ap = v;
   1713 	register struct vnode *dvp = ap->a_dvp;
   1714 	register struct vattr *vap = ap->a_vap;
   1715 	register struct componentname *cnp = ap->a_cnp;
   1716 	register struct nfsv2_sattr *sp;
   1717 	register u_int32_t *tl;
   1718 	register caddr_t cp;
   1719 	register int32_t t1, t2;
   1720 	caddr_t bpos, dpos, cp2;
   1721 	int slen, error = 0, wccflag = NFSV3_WCCRATTR, gotvp;
   1722 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
   1723 	struct vnode *newvp = (struct vnode *)0;
   1724 	int v3 = NFS_ISV3(dvp);
   1725 
   1726 	nfsstats.rpccnt[NFSPROC_SYMLINK]++;
   1727 	slen = strlen(ap->a_target);
   1728 	nfsm_reqhead(dvp, NFSPROC_SYMLINK, NFSX_FH(v3) + 2*NFSX_UNSIGNED +
   1729 	    nfsm_rndup(cnp->cn_namelen) + nfsm_rndup(slen) + NFSX_SATTR(v3));
   1730 	nfsm_fhtom(dvp, v3);
   1731 	nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
   1732 	if (v3)
   1733 		nfsm_v3attrbuild(vap, FALSE);
   1734 	nfsm_strtom(ap->a_target, slen, NFS_MAXPATHLEN);
   1735 	if (!v3) {
   1736 		nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
   1737 		sp->sa_mode = vtonfsv2_mode(VLNK, vap->va_mode);
   1738 		sp->sa_uid = nfs_xdrneg1;
   1739 		sp->sa_gid = nfs_xdrneg1;
   1740 		sp->sa_size = nfs_xdrneg1;
   1741 		txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
   1742 		txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
   1743 	}
   1744 	nfsm_request(dvp, NFSPROC_SYMLINK, cnp->cn_proc, cnp->cn_cred);
   1745 	if (v3) {
   1746 		if (!error)
   1747 			nfsm_mtofh(dvp, newvp, v3, gotvp);
   1748 		nfsm_wcc_data(dvp, wccflag);
   1749 	}
   1750 	nfsm_reqdone;
   1751 	if (newvp)
   1752 		vrele(newvp);
   1753 	FREE(cnp->cn_pnbuf, M_NAMEI);
   1754 	VTONFS(dvp)->n_flag |= NMODIFIED;
   1755 	if (!wccflag)
   1756 		VTONFS(dvp)->n_attrstamp = 0;
   1757 	vrele(dvp);
   1758 	/*
   1759 	 * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry.
   1760 	 */
   1761 	if (error == EEXIST)
   1762 		error = 0;
   1763 	return (error);
   1764 }
   1765 
   1766 /*
   1767  * nfs make dir call
   1768  */
   1769 int
   1770 nfs_mkdir(v)
   1771 	void *v;
   1772 {
   1773 	struct vop_mkdir_args /* {
   1774 		struct vnode *a_dvp;
   1775 		struct vnode **a_vpp;
   1776 		struct componentname *a_cnp;
   1777 		struct vattr *a_vap;
   1778 	} */ *ap = v;
   1779 	register struct vnode *dvp = ap->a_dvp;
   1780 	register struct vattr *vap = ap->a_vap;
   1781 	register struct componentname *cnp = ap->a_cnp;
   1782 	register struct nfsv2_sattr *sp;
   1783 	register u_int32_t *tl;
   1784 	register caddr_t cp;
   1785 	register int32_t t1, t2;
   1786 	register int len;
   1787 	struct nfsnode *np = (struct nfsnode *)0;
   1788 	struct vnode *newvp = (struct vnode *)0;
   1789 	caddr_t bpos, dpos, cp2;
   1790 	int error = 0, wccflag = NFSV3_WCCRATTR;
   1791 	int gotvp = 0;
   1792 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
   1793 	int v3 = NFS_ISV3(dvp);
   1794 
   1795 	len = cnp->cn_namelen;
   1796 	nfsstats.rpccnt[NFSPROC_MKDIR]++;
   1797 	nfsm_reqhead(dvp, NFSPROC_MKDIR,
   1798 	  NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len) + NFSX_SATTR(v3));
   1799 	nfsm_fhtom(dvp, v3);
   1800 	nfsm_strtom(cnp->cn_nameptr, len, NFS_MAXNAMLEN);
   1801 	if (v3) {
   1802 		nfsm_v3attrbuild(vap, FALSE);
   1803 	} else {
   1804 		nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
   1805 		sp->sa_mode = vtonfsv2_mode(VDIR, vap->va_mode);
   1806 		sp->sa_uid = nfs_xdrneg1;
   1807 		sp->sa_gid = nfs_xdrneg1;
   1808 		sp->sa_size = nfs_xdrneg1;
   1809 		txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
   1810 		txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
   1811 	}
   1812 	nfsm_request(dvp, NFSPROC_MKDIR, cnp->cn_proc, cnp->cn_cred);
   1813 	if (!error)
   1814 		nfsm_mtofh(dvp, newvp, v3, gotvp);
   1815 	if (v3)
   1816 		nfsm_wcc_data(dvp, wccflag);
   1817 	nfsm_reqdone;
   1818 	VTONFS(dvp)->n_flag |= NMODIFIED;
   1819 	if (!wccflag)
   1820 		VTONFS(dvp)->n_attrstamp = 0;
   1821 	/*
   1822 	 * Kludge: Map EEXIST => 0 assuming that you have a reply to a retry
   1823 	 * if we can succeed in looking up the directory.
   1824 	 */
   1825 	if (error == EEXIST || (!error && !gotvp)) {
   1826 		if (newvp) {
   1827 			vrele(newvp);
   1828 			newvp = (struct vnode *)0;
   1829 		}
   1830 		error = nfs_lookitup(dvp, cnp->cn_nameptr, len, cnp->cn_cred,
   1831 			cnp->cn_proc, &np);
   1832 		if (!error) {
   1833 			newvp = NFSTOV(np);
   1834 			if (newvp->v_type != VDIR)
   1835 				error = EEXIST;
   1836 		}
   1837 	}
   1838 	if (error) {
   1839 		if (newvp)
   1840 			vrele(newvp);
   1841 	} else {
   1842 		if (cnp->cn_flags & MAKEENTRY)
   1843 			cache_enter(dvp, newvp, cnp);
   1844 		*ap->a_vpp = newvp;
   1845 	}
   1846 	FREE(cnp->cn_pnbuf, M_NAMEI);
   1847 	vrele(dvp);
   1848 	return (error);
   1849 }
   1850 
   1851 /*
   1852  * nfs remove directory call
   1853  */
   1854 int
   1855 nfs_rmdir(v)
   1856 	void *v;
   1857 {
   1858 	struct vop_rmdir_args /* {
   1859 		struct vnode *a_dvp;
   1860 		struct vnode *a_vp;
   1861 		struct componentname *a_cnp;
   1862 	} */ *ap = v;
   1863 	register struct vnode *vp = ap->a_vp;
   1864 	register struct vnode *dvp = ap->a_dvp;
   1865 	register struct componentname *cnp = ap->a_cnp;
   1866 	register u_int32_t *tl;
   1867 	register caddr_t cp;
   1868 	register int32_t t1, t2;
   1869 	caddr_t bpos, dpos, cp2;
   1870 	int error = 0, wccflag = NFSV3_WCCRATTR;
   1871 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
   1872 	int v3 = NFS_ISV3(dvp);
   1873 
   1874 	if (dvp == vp) {
   1875 		vrele(dvp);
   1876 		vrele(dvp);
   1877 		FREE(cnp->cn_pnbuf, M_NAMEI);
   1878 		return (EINVAL);
   1879 	}
   1880 	nfsstats.rpccnt[NFSPROC_RMDIR]++;
   1881 	nfsm_reqhead(dvp, NFSPROC_RMDIR,
   1882 		NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(cnp->cn_namelen));
   1883 	nfsm_fhtom(dvp, v3);
   1884 	nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
   1885 	nfsm_request(dvp, NFSPROC_RMDIR, cnp->cn_proc, cnp->cn_cred);
   1886 	if (v3)
   1887 		nfsm_wcc_data(dvp, wccflag);
   1888 	nfsm_reqdone;
   1889 	FREE(cnp->cn_pnbuf, M_NAMEI);
   1890 	VTONFS(dvp)->n_flag |= NMODIFIED;
   1891 	if (!wccflag)
   1892 		VTONFS(dvp)->n_attrstamp = 0;
   1893 	cache_purge(dvp);
   1894 	cache_purge(vp);
   1895 	vrele(vp);
   1896 	vrele(dvp);
   1897 	/*
   1898 	 * Kludge: Map ENOENT => 0 assuming that you have a reply to a retry.
   1899 	 */
   1900 	if (error == ENOENT)
   1901 		error = 0;
   1902 	return (error);
   1903 }
   1904 
   1905 /*
   1906  * nfs readdir call
   1907  */
   1908 int
   1909 nfs_readdir(v)
   1910 	void *v;
   1911 {
   1912 	struct vop_readdir_args /* {
   1913 		struct vnode *a_vp;
   1914 		struct uio *a_uio;
   1915 		struct ucred *a_cred;
   1916 		int *a_eofflag;
   1917 		off_t **a_cookies;
   1918 		int *a_ncookies;
   1919 	} */ *ap = v;
   1920 	register struct vnode *vp = ap->a_vp;
   1921 	register struct uio *uio = ap->a_uio;
   1922 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
   1923 	char *base = uio->uio_iov->iov_base;
   1924 	int tresid, error;
   1925 	size_t count, lost;
   1926 	struct dirent *dp;
   1927 	off_t *cookies = NULL;
   1928 	int ncookies = 0, nc;
   1929 
   1930 	if (vp->v_type != VDIR)
   1931 		return (EPERM);
   1932 
   1933 	lost = uio->uio_resid & (NFS_DIRFRAGSIZ - 1);
   1934 	count = uio->uio_resid - lost;
   1935 	if (count <= 0)
   1936 		return (EINVAL);
   1937 
   1938 	/*
   1939 	 * Call nfs_bioread() to do the real work.
   1940 	 */
   1941 	tresid = uio->uio_resid = count;
   1942 	error = nfs_bioread(vp, uio, 0, ap->a_cred,
   1943 		    ap->a_cookies ? NFSBIO_CACHECOOKIES : 0);
   1944 
   1945 	if (!error && ap->a_cookies) {
   1946 		ncookies = count / 16;
   1947 		MALLOC(cookies, off_t *, sizeof (off_t) * ncookies, M_TEMP,
   1948 		    M_WAITOK);
   1949 		*ap->a_cookies = cookies;
   1950 	}
   1951 
   1952 	if (!error && uio->uio_resid == tresid) {
   1953 		uio->uio_resid += lost;
   1954 		nfsstats.direofcache_misses++;
   1955 		if (ap->a_cookies)
   1956 			*ap->a_ncookies = 0;
   1957 		*ap->a_eofflag = 1;
   1958 		return (0);
   1959 	}
   1960 
   1961 	if (!error && ap->a_cookies) {
   1962 		/*
   1963 		 * Only the NFS server and emulations use cookies, and they
   1964 		 * load the directory block into system space, so we can
   1965 		 * just look at it directly.
   1966 		 */
   1967 		if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1)
   1968 			panic("nfs_readdir: lost in space");
   1969 		for (nc = 0; ncookies-- &&
   1970 		     base < (char *)uio->uio_iov->iov_base; nc++){
   1971 			dp = (struct dirent *) base;
   1972 			if (dp->d_reclen == 0)
   1973 				break;
   1974 			if (nmp->nm_flag & NFSMNT_XLATECOOKIE)
   1975 				*(cookies++) = (off_t)NFS_GETCOOKIE32(dp);
   1976 			else
   1977 				*(cookies++) = NFS_GETCOOKIE(dp);
   1978 			base += dp->d_reclen;
   1979 		}
   1980 		uio->uio_resid +=
   1981 		    ((caddr_t)uio->uio_iov->iov_base - base);
   1982 		uio->uio_iov->iov_len +=
   1983 		    ((caddr_t)uio->uio_iov->iov_base - base);
   1984 		uio->uio_iov->iov_base = base;
   1985 		*ap->a_ncookies = nc;
   1986 	}
   1987 
   1988 	uio->uio_resid += lost;
   1989 	*ap->a_eofflag = 0;
   1990 	return (error);
   1991 }
   1992 
   1993 /*
   1994  * Readdir rpc call.
   1995  * Called from below the buffer cache by nfs_doio().
   1996  */
   1997 int
   1998 nfs_readdirrpc(vp, uiop, cred)
   1999 	struct vnode *vp;
   2000 	register struct uio *uiop;
   2001 	struct ucred *cred;
   2002 {
   2003 	register int len, left;
   2004 	register struct dirent *dp = NULL;
   2005 	register u_int32_t *tl;
   2006 	register caddr_t cp;
   2007 	register int32_t t1, t2;
   2008 	caddr_t bpos, dpos, cp2;
   2009 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
   2010 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
   2011 	struct nfsnode *dnp = VTONFS(vp);
   2012 	u_quad_t fileno;
   2013 	int error = 0, tlen, more_dirs = 1, blksiz = 0, bigenough = 1;
   2014 	int attrflag, nrpcs = 0, reclen;
   2015 	int v3 = NFS_ISV3(vp);
   2016 	nfsquad_t cookie;
   2017 
   2018 #ifdef DIAGNOSTIC
   2019 	/*
   2020 	 * Should be called from buffer cache, so only amount of
   2021 	 * NFS_DIRBLKSIZ will be requested.
   2022 	 */
   2023 	if (uiop->uio_iovcnt != 1 || (uiop->uio_resid & (NFS_DIRBLKSIZ - 1)))
   2024 		panic("nfs readdirrpc bad uio");
   2025 #endif
   2026 
   2027 	/*
   2028 	 * Loop around doing readdir rpc's of size nm_readdirsize
   2029 	 * truncated to a multiple of NFS_DIRFRAGSIZ.
   2030 	 * The stopping criteria is EOF or buffer full.
   2031 	 */
   2032 	while (more_dirs && bigenough) {
   2033 		/*
   2034 		 * Heuristic: don't bother to do another RPC to further
   2035 		 * fill up this block if there is not much room left. (< 50%
   2036 		 * of the readdir RPC size). This wastes some buffer space
   2037 		 * but can save up to 50% in RPC calls.
   2038 		 */
   2039 		if (nrpcs > 0 && uiop->uio_resid < (nmp->nm_readdirsize / 2)) {
   2040 			bigenough = 0;
   2041 			break;
   2042 		}
   2043 		nfsstats.rpccnt[NFSPROC_READDIR]++;
   2044 		nfsm_reqhead(vp, NFSPROC_READDIR, NFSX_FH(v3) +
   2045 			NFSX_READDIR(v3));
   2046 		nfsm_fhtom(vp, v3);
   2047 		if (v3) {
   2048 			nfsm_build(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
   2049 			cookie.qval = uiop->uio_offset;
   2050 			if (nmp->nm_iflag & NFSMNT_SWAPCOOKIE) {
   2051 				txdr_swapcookie3(uiop->uio_offset, tl);
   2052 			} else {
   2053 				txdr_cookie3(uiop->uio_offset, tl);
   2054 			}
   2055 			tl += 2;
   2056 			*tl++ = dnp->n_cookieverf.nfsuquad[0];
   2057 			*tl++ = dnp->n_cookieverf.nfsuquad[1];
   2058 		} else {
   2059 			nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
   2060 			*tl++ = txdr_unsigned(uiop->uio_offset);
   2061 		}
   2062 		*tl = txdr_unsigned(nmp->nm_readdirsize);
   2063 		nfsm_request(vp, NFSPROC_READDIR, uiop->uio_procp, cred);
   2064 		nrpcs++;
   2065 		if (v3) {
   2066 			nfsm_postop_attr(vp, attrflag);
   2067 			if (!error) {
   2068 				nfsm_dissect(tl, u_int32_t *,
   2069 				    2 * NFSX_UNSIGNED);
   2070 				dnp->n_cookieverf.nfsuquad[0] = *tl++;
   2071 				dnp->n_cookieverf.nfsuquad[1] = *tl;
   2072 			} else {
   2073 				m_freem(mrep);
   2074 				goto nfsmout;
   2075 			}
   2076 		}
   2077 		nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
   2078 		more_dirs = fxdr_unsigned(int, *tl);
   2079 
   2080 		/* loop thru the dir entries, doctoring them to 4bsd form */
   2081 		while (more_dirs && bigenough) {
   2082 			if (v3) {
   2083 				nfsm_dissect(tl, u_int32_t *,
   2084 				    3 * NFSX_UNSIGNED);
   2085 				fileno = fxdr_hyper(tl);
   2086 				len = fxdr_unsigned(int, *(tl + 2));
   2087 			} else {
   2088 				nfsm_dissect(tl, u_int32_t *,
   2089 				    2 * NFSX_UNSIGNED);
   2090 				fileno = fxdr_unsigned(u_quad_t, *tl++);
   2091 				len = fxdr_unsigned(int, *tl);
   2092 			}
   2093 			if (len <= 0 || len > NFS_MAXNAMLEN) {
   2094 				error = EBADRPC;
   2095 				m_freem(mrep);
   2096 				goto nfsmout;
   2097 			}
   2098 			tlen = nfsm_rndup(len);
   2099 			if (tlen == len)
   2100 				tlen += 4;	/* To ensure null termination */
   2101 			tlen += sizeof (off_t) + sizeof (int);
   2102 			reclen = ALIGN(tlen + DIRHDSIZ);
   2103 			tlen = reclen - DIRHDSIZ;
   2104 			left = NFS_DIRFRAGSIZ - blksiz;
   2105 			if (reclen > left) {
   2106 				dp->d_reclen += left;
   2107 				(caddr_t)uiop->uio_iov->iov_base += left;
   2108 				uiop->uio_iov->iov_len -= left;
   2109 				uiop->uio_resid -= left;
   2110 				blksiz = 0;
   2111 				NFS_STASHCOOKIE(dp, uiop->uio_offset);
   2112 			}
   2113 			if (reclen > uiop->uio_resid)
   2114 				bigenough = 0;
   2115 			if (bigenough) {
   2116 				dp = (struct dirent *)uiop->uio_iov->iov_base;
   2117 				dp->d_fileno = (int)fileno;
   2118 				dp->d_namlen = len;
   2119 				dp->d_reclen = reclen;
   2120 				dp->d_type = DT_UNKNOWN;
   2121 				blksiz += dp->d_reclen;
   2122 				if (blksiz == NFS_DIRFRAGSIZ)
   2123 					blksiz = 0;
   2124 				uiop->uio_resid -= DIRHDSIZ;
   2125 				(caddr_t)uiop->uio_iov->iov_base += DIRHDSIZ;
   2126 				uiop->uio_iov->iov_len -= DIRHDSIZ;
   2127 				nfsm_mtouio(uiop, len);
   2128 				cp = uiop->uio_iov->iov_base;
   2129 				tlen -= len;
   2130 				*cp = '\0';	/* null terminate */
   2131 				(caddr_t)uiop->uio_iov->iov_base += tlen;
   2132 				uiop->uio_iov->iov_len -= tlen;
   2133 				uiop->uio_resid -= tlen;
   2134 			} else
   2135 				nfsm_adv(nfsm_rndup(len));
   2136 			if (v3) {
   2137 				nfsm_dissect(tl, u_int32_t *,
   2138 				    3 * NFSX_UNSIGNED);
   2139 			} else {
   2140 				nfsm_dissect(tl, u_int32_t *,
   2141 				    2 * NFSX_UNSIGNED);
   2142 			}
   2143 			if (bigenough) {
   2144 				if (v3) {
   2145 					if (nmp->nm_iflag & NFSMNT_SWAPCOOKIE)
   2146 						uiop->uio_offset =
   2147 						    fxdr_swapcookie3(tl);
   2148 					else
   2149 						uiop->uio_offset =
   2150 						    fxdr_cookie3(tl);
   2151 				}
   2152 				else {
   2153 					uiop->uio_offset =
   2154 					    fxdr_unsigned(off_t, *tl);
   2155 				}
   2156 				NFS_STASHCOOKIE(dp, uiop->uio_offset);
   2157 			}
   2158 			if (v3)
   2159 				tl += 2;
   2160 			else
   2161 				tl++;
   2162 			more_dirs = fxdr_unsigned(int, *tl);
   2163 		}
   2164 		/*
   2165 		 * If at end of rpc data, get the eof boolean
   2166 		 */
   2167 		if (!more_dirs) {
   2168 			nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
   2169 			more_dirs = (fxdr_unsigned(int, *tl) == 0);
   2170 		}
   2171 		m_freem(mrep);
   2172 	}
   2173 	/*
   2174 	 * Fill last record, iff any, out to a multiple of NFS_DIRFRAGSIZ
   2175 	 * by increasing d_reclen for the last record.
   2176 	 */
   2177 	if (blksiz > 0) {
   2178 		left = NFS_DIRFRAGSIZ - blksiz;
   2179 		dp->d_reclen += left;
   2180 		NFS_STASHCOOKIE(dp, uiop->uio_offset);
   2181 		(caddr_t)uiop->uio_iov->iov_base += left;
   2182 		uiop->uio_iov->iov_len -= left;
   2183 		uiop->uio_resid -= left;
   2184 	}
   2185 
   2186 	/*
   2187 	 * We are now either at the end of the directory or have filled the
   2188 	 * block.
   2189 	 */
   2190 	if (bigenough)
   2191 		dnp->n_direofoffset = uiop->uio_offset;
   2192 nfsmout:
   2193 	return (error);
   2194 }
   2195 
   2196 /*
   2197  * NFS V3 readdir plus RPC. Used in place of nfs_readdirrpc().
   2198  */
   2199 int
   2200 nfs_readdirplusrpc(vp, uiop, cred)
   2201 	struct vnode *vp;
   2202 	register struct uio *uiop;
   2203 	struct ucred *cred;
   2204 {
   2205 	register int len, left;
   2206 	register struct dirent *dp = NULL;
   2207 	register u_int32_t *tl;
   2208 	register caddr_t cp;
   2209 	register int32_t t1, t2;
   2210 	struct vnode *newvp;
   2211 	caddr_t bpos, dpos, cp2;
   2212 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
   2213 	struct nameidata nami, *ndp = &nami;
   2214 	struct componentname *cnp = &ndp->ni_cnd;
   2215 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
   2216 	struct nfsnode *dnp = VTONFS(vp), *np;
   2217 	const unsigned char *hcp;
   2218 	nfsfh_t *fhp;
   2219 	u_quad_t fileno;
   2220 	int error = 0, tlen, more_dirs = 1, blksiz = 0, doit, bigenough = 1, i;
   2221 	int attrflag, fhsize, nrpcs = 0, reclen;
   2222 	struct nfs_fattr fattr, *fp;
   2223 
   2224 #ifdef DIAGNOSTIC
   2225 	if (uiop->uio_iovcnt != 1 || (uiop->uio_resid & (NFS_DIRBLKSIZ - 1)))
   2226 		panic("nfs readdirplusrpc bad uio");
   2227 #endif
   2228 	ndp->ni_dvp = vp;
   2229 	newvp = NULLVP;
   2230 
   2231 	/*
   2232 	 * Loop around doing readdir rpc's of size nm_readdirsize
   2233 	 * truncated to a multiple of NFS_DIRFRAGSIZ.
   2234 	 * The stopping criteria is EOF or buffer full.
   2235 	 */
   2236 	while (more_dirs && bigenough) {
   2237 		if (nrpcs > 0 && uiop->uio_resid < (nmp->nm_readdirsize / 2)) {
   2238 			bigenough = 0;
   2239 			break;
   2240 		}
   2241 		nfsstats.rpccnt[NFSPROC_READDIRPLUS]++;
   2242 		nfsm_reqhead(vp, NFSPROC_READDIRPLUS,
   2243 			NFSX_FH(1) + 6 * NFSX_UNSIGNED);
   2244 		nfsm_fhtom(vp, 1);
   2245  		nfsm_build(tl, u_int32_t *, 6 * NFSX_UNSIGNED);
   2246 		if (nmp->nm_iflag & NFSMNT_SWAPCOOKIE) {
   2247 			txdr_swapcookie3(uiop->uio_offset, tl);
   2248 		} else {
   2249 			txdr_cookie3(uiop->uio_offset, tl);
   2250 		}
   2251 		tl += 2;
   2252 		*tl++ = dnp->n_cookieverf.nfsuquad[0];
   2253 		*tl++ = dnp->n_cookieverf.nfsuquad[1];
   2254 		*tl++ = txdr_unsigned(nmp->nm_readdirsize);
   2255 		*tl = txdr_unsigned(nmp->nm_rsize);
   2256 		nfsm_request(vp, NFSPROC_READDIRPLUS, uiop->uio_procp, cred);
   2257 		nfsm_postop_attr(vp, attrflag);
   2258 		if (error) {
   2259 			m_freem(mrep);
   2260 			goto nfsmout;
   2261 		}
   2262 		nrpcs++;
   2263 		nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
   2264 		dnp->n_cookieverf.nfsuquad[0] = *tl++;
   2265 		dnp->n_cookieverf.nfsuquad[1] = *tl++;
   2266 		more_dirs = fxdr_unsigned(int, *tl);
   2267 
   2268 		/* loop thru the dir entries, doctoring them to 4bsd form */
   2269 		while (more_dirs && bigenough) {
   2270 			nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
   2271 			fileno = fxdr_hyper(tl);
   2272 			len = fxdr_unsigned(int, *(tl + 2));
   2273 			if (len <= 0 || len > NFS_MAXNAMLEN) {
   2274 				error = EBADRPC;
   2275 				m_freem(mrep);
   2276 				goto nfsmout;
   2277 			}
   2278 			tlen = nfsm_rndup(len);
   2279 			if (tlen == len)
   2280 				tlen += 4;	/* To ensure null termination*/
   2281 			tlen += sizeof (off_t) + sizeof (int);
   2282 			reclen = ALIGN(tlen + DIRHDSIZ);
   2283 			tlen = reclen - DIRHDSIZ;
   2284 			left = NFS_DIRFRAGSIZ - blksiz;
   2285 			if (reclen > left) {
   2286 				/*
   2287 				 * DIRFRAGSIZ is aligned, no need to align
   2288 				 * again here.
   2289 				 */
   2290 				dp->d_reclen += left;
   2291 				(caddr_t)uiop->uio_iov->iov_base += left;
   2292 				uiop->uio_iov->iov_len -= left;
   2293 				uiop->uio_resid -= left;
   2294 				NFS_STASHCOOKIE(dp, uiop->uio_offset);
   2295 				blksiz = 0;
   2296 			}
   2297 			if (reclen > uiop->uio_resid)
   2298 				bigenough = 0;
   2299 			if (bigenough) {
   2300 				dp = (struct dirent *)uiop->uio_iov->iov_base;
   2301 				dp->d_fileno = (int)fileno;
   2302 				dp->d_namlen = len;
   2303 				dp->d_reclen = reclen;
   2304 				dp->d_type = DT_UNKNOWN;
   2305 				blksiz += dp->d_reclen;
   2306 				if (blksiz == NFS_DIRFRAGSIZ)
   2307 					blksiz = 0;
   2308 				uiop->uio_resid -= DIRHDSIZ;
   2309 				(caddr_t)uiop->uio_iov->iov_base += DIRHDSIZ;
   2310 				uiop->uio_iov->iov_len -= DIRHDSIZ;
   2311 				cnp->cn_nameptr = uiop->uio_iov->iov_base;
   2312 				cnp->cn_namelen = len;
   2313 				nfsm_mtouio(uiop, len);
   2314 				cp = uiop->uio_iov->iov_base;
   2315 				tlen -= len;
   2316 				*cp = '\0';
   2317 				(caddr_t)uiop->uio_iov->iov_base += tlen;
   2318 				uiop->uio_iov->iov_len -= tlen;
   2319 				uiop->uio_resid -= tlen;
   2320 			} else
   2321 				nfsm_adv(nfsm_rndup(len));
   2322 			nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
   2323 			if (bigenough) {
   2324 				if (nmp->nm_iflag & NFSMNT_SWAPCOOKIE)
   2325 					uiop->uio_offset =
   2326 						fxdr_swapcookie3(tl);
   2327 				else
   2328 					uiop->uio_offset =
   2329 						fxdr_cookie3(tl);
   2330 				NFS_STASHCOOKIE(dp, uiop->uio_offset);
   2331 			}
   2332 			tl += 2;
   2333 
   2334 			/*
   2335 			 * Since the attributes are before the file handle
   2336 			 * (sigh), we must skip over the attributes and then
   2337 			 * come back and get them.
   2338 			 */
   2339 			attrflag = fxdr_unsigned(int, *tl);
   2340 			if (attrflag) {
   2341 			    nfsm_dissect(fp, struct nfs_fattr *, NFSX_V3FATTR);
   2342 			    memcpy(&fattr, fp, NFSX_V3FATTR);
   2343 			    nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
   2344 			    doit = fxdr_unsigned(int, *tl);
   2345 			    if (doit) {
   2346 				nfsm_getfh(fhp, fhsize, 1);
   2347 				if (NFS_CMPFH(dnp, fhp, fhsize)) {
   2348 				    VREF(vp);
   2349 				    newvp = vp;
   2350 				    np = dnp;
   2351 				} else {
   2352 				    error = nfs_nget(vp->v_mount, fhp,
   2353 					fhsize, &np);
   2354 				    if (!error)
   2355 					newvp = NFSTOV(np);
   2356 				}
   2357 				if (!error) {
   2358 				    nfs_loadattrcache(&newvp, &fattr, 0);
   2359 				    dp->d_type =
   2360 				        IFTODT(VTTOIF(np->n_vattr->va_type));
   2361 				    ndp->ni_vp = newvp;
   2362 				    cnp->cn_hash = 0;
   2363 				    for (hcp = cnp->cn_nameptr, i = 1; i <= len;
   2364 				        i++, hcp++)
   2365 				        cnp->cn_hash += *hcp * i;
   2366 				    if (cnp->cn_namelen <= NCHNAMLEN)
   2367 				        cache_enter(ndp->ni_dvp, ndp->ni_vp,
   2368 						    cnp);
   2369 				}
   2370 			   }
   2371 			} else {
   2372 			    /* Just skip over the file handle */
   2373 			    nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
   2374 			    i = fxdr_unsigned(int, *tl);
   2375 			    nfsm_adv(nfsm_rndup(i));
   2376 			}
   2377 			if (newvp != NULLVP) {
   2378 			    vrele(newvp);
   2379 			    newvp = NULLVP;
   2380 			}
   2381 			nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
   2382 			more_dirs = fxdr_unsigned(int, *tl);
   2383 		}
   2384 		/*
   2385 		 * If at end of rpc data, get the eof boolean
   2386 		 */
   2387 		if (!more_dirs) {
   2388 			nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
   2389 			more_dirs = (fxdr_unsigned(int, *tl) == 0);
   2390 		}
   2391 		m_freem(mrep);
   2392 	}
   2393 	/*
   2394 	 * Fill last record, iff any, out to a multiple of NFS_DIRFRAGSIZ
   2395 	 * by increasing d_reclen for the last record.
   2396 	 */
   2397 	if (blksiz > 0) {
   2398 		left = NFS_DIRFRAGSIZ - blksiz;
   2399 		dp->d_reclen += left;
   2400 		NFS_STASHCOOKIE(dp, uiop->uio_offset);
   2401 		(caddr_t)uiop->uio_iov->iov_base += left;
   2402 		uiop->uio_iov->iov_len -= left;
   2403 		uiop->uio_resid -= left;
   2404 	}
   2405 
   2406 	/*
   2407 	 * We are now either at the end of the directory or have filled the
   2408 	 * block.
   2409 	 */
   2410 	if (bigenough)
   2411 		dnp->n_direofoffset = uiop->uio_offset;
   2412 nfsmout:
   2413 	if (newvp != NULLVP)
   2414 		vrele(newvp);
   2415 	return (error);
   2416 }
   2417 static char hextoasc[] = "0123456789abcdef";
   2418 
   2419 /*
   2420  * Silly rename. To make the NFS filesystem that is stateless look a little
   2421  * more like the "ufs" a remove of an active vnode is translated to a rename
   2422  * to a funny looking filename that is removed by nfs_inactive on the
   2423  * nfsnode. There is the potential for another process on a different client
   2424  * to create the same funny name between the nfs_lookitup() fails and the
   2425  * nfs_rename() completes, but...
   2426  */
   2427 int
   2428 nfs_sillyrename(dvp, vp, cnp)
   2429 	struct vnode *dvp, *vp;
   2430 	struct componentname *cnp;
   2431 {
   2432 	register struct sillyrename *sp;
   2433 	struct nfsnode *np;
   2434 	int error;
   2435 	short pid;
   2436 
   2437 	cache_purge(dvp);
   2438 	np = VTONFS(vp);
   2439 #ifndef DIAGNOSTIC
   2440 	if (vp->v_type == VDIR)
   2441 		panic("nfs: sillyrename dir");
   2442 #endif
   2443 	MALLOC(sp, struct sillyrename *, sizeof (struct sillyrename),
   2444 		M_NFSREQ, M_WAITOK);
   2445 	sp->s_cred = crdup(cnp->cn_cred);
   2446 	sp->s_dvp = dvp;
   2447 	VREF(dvp);
   2448 
   2449 	/* Fudge together a funny name */
   2450 	pid = cnp->cn_proc->p_pid;
   2451 	memcpy(sp->s_name, ".nfsAxxxx4.4", 13);
   2452 	sp->s_namlen = 12;
   2453 	sp->s_name[8] = hextoasc[pid & 0xf];
   2454 	sp->s_name[7] = hextoasc[(pid >> 4) & 0xf];
   2455 	sp->s_name[6] = hextoasc[(pid >> 8) & 0xf];
   2456 	sp->s_name[5] = hextoasc[(pid >> 12) & 0xf];
   2457 
   2458 	/* Try lookitups until we get one that isn't there */
   2459 	while (nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
   2460 		cnp->cn_proc, (struct nfsnode **)0) == 0) {
   2461 		sp->s_name[4]++;
   2462 		if (sp->s_name[4] > 'z') {
   2463 			error = EINVAL;
   2464 			goto bad;
   2465 		}
   2466 	}
   2467 	error = nfs_renameit(dvp, cnp, sp);
   2468 	if (error)
   2469 		goto bad;
   2470 	error = nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
   2471 		cnp->cn_proc, &np);
   2472 	np->n_sillyrename = sp;
   2473 	return (0);
   2474 bad:
   2475 	vrele(sp->s_dvp);
   2476 	crfree(sp->s_cred);
   2477 	free((caddr_t)sp, M_NFSREQ);
   2478 	return (error);
   2479 }
   2480 
   2481 /*
   2482  * Look up a file name and optionally either update the file handle or
   2483  * allocate an nfsnode, depending on the value of npp.
   2484  * npp == NULL	--> just do the lookup
   2485  * *npp == NULL --> allocate a new nfsnode and make sure attributes are
   2486  *			handled too
   2487  * *npp != NULL --> update the file handle in the vnode
   2488  */
   2489 int
   2490 nfs_lookitup(dvp, name, len, cred, procp, npp)
   2491 	register struct vnode *dvp;
   2492 	const char *name;
   2493 	int len;
   2494 	struct ucred *cred;
   2495 	struct proc *procp;
   2496 	struct nfsnode **npp;
   2497 {
   2498 	register u_int32_t *tl;
   2499 	register caddr_t cp;
   2500 	register int32_t t1, t2;
   2501 	struct vnode *newvp = (struct vnode *)0;
   2502 	struct nfsnode *np, *dnp = VTONFS(dvp);
   2503 	caddr_t bpos, dpos, cp2;
   2504 	int error = 0, fhlen, attrflag;
   2505 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
   2506 	nfsfh_t *nfhp;
   2507 	int v3 = NFS_ISV3(dvp);
   2508 
   2509 	nfsstats.rpccnt[NFSPROC_LOOKUP]++;
   2510 	nfsm_reqhead(dvp, NFSPROC_LOOKUP,
   2511 		NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len));
   2512 	nfsm_fhtom(dvp, v3);
   2513 	nfsm_strtom(name, len, NFS_MAXNAMLEN);
   2514 	nfsm_request(dvp, NFSPROC_LOOKUP, procp, cred);
   2515 	if (npp && !error) {
   2516 		nfsm_getfh(nfhp, fhlen, v3);
   2517 		if (*npp) {
   2518 		    np = *npp;
   2519 		    if (np->n_fhsize > NFS_SMALLFH && fhlen <= NFS_SMALLFH) {
   2520 			free((caddr_t)np->n_fhp, M_NFSBIGFH);
   2521 			np->n_fhp = &np->n_fh;
   2522 		    } else if (np->n_fhsize <= NFS_SMALLFH && fhlen>NFS_SMALLFH)
   2523 			np->n_fhp =(nfsfh_t *)malloc(fhlen,M_NFSBIGFH,M_WAITOK);
   2524 		    memcpy((caddr_t)np->n_fhp, (caddr_t)nfhp, fhlen);
   2525 		    np->n_fhsize = fhlen;
   2526 		    newvp = NFSTOV(np);
   2527 		} else if (NFS_CMPFH(dnp, nfhp, fhlen)) {
   2528 		    VREF(dvp);
   2529 		    newvp = dvp;
   2530 		} else {
   2531 		    error = nfs_nget(dvp->v_mount, nfhp, fhlen, &np);
   2532 		    if (error) {
   2533 			m_freem(mrep);
   2534 			return (error);
   2535 		    }
   2536 		    newvp = NFSTOV(np);
   2537 		}
   2538 		if (v3) {
   2539 			nfsm_postop_attr(newvp, attrflag);
   2540 			if (!attrflag && *npp == NULL) {
   2541 				m_freem(mrep);
   2542 				vrele(newvp);
   2543 				return (ENOENT);
   2544 			}
   2545 		} else
   2546 			nfsm_loadattr(newvp, (struct vattr *)0);
   2547 	}
   2548 	nfsm_reqdone;
   2549 	if (npp && *npp == NULL) {
   2550 		if (error) {
   2551 			if (newvp)
   2552 				vrele(newvp);
   2553 		} else
   2554 			*npp = np;
   2555 	}
   2556 	return (error);
   2557 }
   2558 
   2559 /*
   2560  * Nfs Version 3 commit rpc
   2561  */
   2562 int
   2563 nfs_commit(vp, offset, cnt, cred, procp)
   2564 	register struct vnode *vp;
   2565 	u_quad_t offset;
   2566 	int cnt;
   2567 	struct ucred *cred;
   2568 	struct proc *procp;
   2569 {
   2570 	register caddr_t cp;
   2571 	register u_int32_t *tl;
   2572 	register int32_t t1, t2;
   2573 	register struct nfsmount *nmp = VFSTONFS(vp->v_mount);
   2574 	caddr_t bpos, dpos, cp2;
   2575 	int error = 0, wccflag = NFSV3_WCCRATTR;
   2576 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
   2577 
   2578 	if ((nmp->nm_iflag & NFSMNT_HASWRITEVERF) == 0)
   2579 		return (0);
   2580 	nfsstats.rpccnt[NFSPROC_COMMIT]++;
   2581 	nfsm_reqhead(vp, NFSPROC_COMMIT, NFSX_FH(1));
   2582 	nfsm_fhtom(vp, 1);
   2583 	nfsm_build(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
   2584 	txdr_hyper(offset, tl);
   2585 	tl += 2;
   2586 	*tl = txdr_unsigned(cnt);
   2587 	nfsm_request(vp, NFSPROC_COMMIT, procp, cred);
   2588 	nfsm_wcc_data(vp, wccflag);
   2589 	if (!error) {
   2590 		nfsm_dissect(tl, u_int32_t *, NFSX_V3WRITEVERF);
   2591 		if (memcmp((caddr_t)nmp->nm_verf, (caddr_t)tl,
   2592 			NFSX_V3WRITEVERF)) {
   2593 			memcpy((caddr_t)nmp->nm_verf, (caddr_t)tl,
   2594 				NFSX_V3WRITEVERF);
   2595 			error = NFSERR_STALEWRITEVERF;
   2596 		}
   2597 	}
   2598 	nfsm_reqdone;
   2599 	return (error);
   2600 }
   2601 
   2602 /*
   2603  * Kludge City..
   2604  * - make nfs_bmap() essentially a no-op that does no translation
   2605  * - do nfs_strategy() by doing I/O with nfs_readrpc/nfs_writerpc
   2606  *   (Maybe I could use the process's page mapping, but I was concerned that
   2607  *    Kernel Write might not be enabled and also figured copyout() would do
   2608  *    a lot more work than memcpy() and also it currently happens in the
   2609  *    context of the swapper process (2).
   2610  */
   2611 int
   2612 nfs_bmap(v)
   2613 	void *v;
   2614 {
   2615 	struct vop_bmap_args /* {
   2616 		struct vnode *a_vp;
   2617 		daddr_t  a_bn;
   2618 		struct vnode **a_vpp;
   2619 		daddr_t *a_bnp;
   2620 		int *a_runp;
   2621 	} */ *ap = v;
   2622 	register struct vnode *vp = ap->a_vp;
   2623 
   2624 	if (ap->a_vpp != NULL)
   2625 		*ap->a_vpp = vp;
   2626 	if (ap->a_bnp != NULL)
   2627 		*ap->a_bnp = ap->a_bn * btodb(vp->v_mount->mnt_stat.f_iosize);
   2628 	return (0);
   2629 }
   2630 
   2631 /*
   2632  * Strategy routine.
   2633  * For async requests when nfsiod(s) are running, queue the request by
   2634  * calling nfs_asyncio(), otherwise just all nfs_doio() to do the
   2635  * request.
   2636  */
   2637 int
   2638 nfs_strategy(v)
   2639 	void *v;
   2640 {
   2641 	struct vop_strategy_args *ap = v;
   2642 	register struct buf *bp = ap->a_bp;
   2643 	struct ucred *cr;
   2644 	struct proc *p;
   2645 	int error = 0;
   2646 
   2647 	if ((bp->b_flags & (B_PHYS|B_ASYNC)) == (B_PHYS|B_ASYNC))
   2648 		panic("nfs physio/async");
   2649 	if (bp->b_flags & B_ASYNC)
   2650 		p = (struct proc *)0;
   2651 	else
   2652 		p = curproc;	/* XXX */
   2653 	if (bp->b_flags & B_READ)
   2654 		cr = bp->b_rcred;
   2655 	else
   2656 		cr = bp->b_wcred;
   2657 	/*
   2658 	 * If the op is asynchronous and an i/o daemon is waiting
   2659 	 * queue the request, wake it up and wait for completion
   2660 	 * otherwise just do it ourselves.
   2661 	 */
   2662 	if ((bp->b_flags & B_ASYNC) == 0 ||
   2663 		nfs_asyncio(bp, NOCRED))
   2664 		error = nfs_doio(bp, cr, p);
   2665 	return (error);
   2666 }
   2667 
   2668 /*
   2669  * Mmap a file
   2670  *
   2671  * NB Currently unsupported.
   2672  */
   2673 /* ARGSUSED */
   2674 int
   2675 nfs_mmap(v)
   2676 	void *v;
   2677 {
   2678 #if 0
   2679 	struct vop_mmap_args /* {
   2680 		struct vnode *a_vp;
   2681 		int a_fflags;
   2682 		struct ucred *a_cred;
   2683 		struct proc *a_p;
   2684 	} */ *ap = v;
   2685 #endif
   2686 
   2687 	return (EINVAL);
   2688 }
   2689 
   2690 /*
   2691  * fsync vnode op. Just call nfs_flush() with commit == 1.
   2692  */
   2693 /* ARGSUSED */
   2694 int
   2695 nfs_fsync(v)
   2696 	void *v;
   2697 {
   2698 	struct vop_fsync_args /* {
   2699 		struct vnodeop_desc *a_desc;
   2700 		struct vnode * a_vp;
   2701 		struct ucred * a_cred;
   2702 		int  a_flags;
   2703 		struct proc * a_p;
   2704 	} */ *ap = v;
   2705 
   2706 	return (nfs_flush(ap->a_vp, ap->a_cred,
   2707 	    (ap->a_flags & FSYNC_WAIT) != 0 ? MNT_WAIT : 0, ap->a_p, 1));
   2708 }
   2709 
   2710 /*
   2711  * Flush all the blocks associated with a vnode.
   2712  * 	Walk through the buffer pool and push any dirty pages
   2713  *	associated with the vnode.
   2714  */
   2715 int
   2716 nfs_flush(vp, cred, waitfor, p, commit)
   2717 	register struct vnode *vp;
   2718 	struct ucred *cred;
   2719 	int waitfor;
   2720 	struct proc *p;
   2721 	int commit;
   2722 {
   2723 	register struct nfsnode *np = VTONFS(vp);
   2724 	register struct buf *bp;
   2725 	register int i;
   2726 	struct buf *nbp;
   2727 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
   2728 	int s, error = 0, slptimeo = 0, slpflag = 0, retv, bvecpos;
   2729 	int passone = 1;
   2730 	u_quad_t off = (u_quad_t)-1, endoff = 0, toff;
   2731 	struct ucred* wcred = NULL;
   2732 #ifndef NFS_COMMITBVECSIZ
   2733 #define NFS_COMMITBVECSIZ	20
   2734 #endif
   2735 	struct buf *bvec[NFS_COMMITBVECSIZ];
   2736 
   2737 	if (nmp->nm_flag & NFSMNT_INT)
   2738 		slpflag = PCATCH;
   2739 	if (!commit)
   2740 		passone = 0;
   2741 	/*
   2742 	 * A b_flags == (B_DELWRI | B_NEEDCOMMIT) block has been written to the
   2743 	 * server, but nas not been committed to stable storage on the server
   2744 	 * yet. On the first pass, the byte range is worked out and the commit
   2745 	 * rpc is done. On the second pass, nfs_writebp() is called to do the
   2746 	 * job.
   2747 	 */
   2748 again:
   2749 	bvecpos = 0;
   2750 	if (NFS_ISV3(vp) && commit) {
   2751 		s = splbio();
   2752 		for (bp = vp->v_dirtyblkhd.lh_first; bp; bp = nbp) {
   2753 			nbp = bp->b_vnbufs.le_next;
   2754 			if (bvecpos >= NFS_COMMITBVECSIZ)
   2755 				break;
   2756 			if ((bp->b_flags & (B_BUSY | B_DELWRI | B_NEEDCOMMIT))
   2757 				!= (B_DELWRI | B_NEEDCOMMIT))
   2758 				continue;
   2759 			bremfree(bp);
   2760 			/*
   2761 			 * Work out if all buffers are using the same cred
   2762 			 * so we can deal with them all with one commit.
   2763 			 */
   2764 			if (wcred == NULL)
   2765 				wcred = bp->b_wcred;
   2766 			else if (wcred != bp->b_wcred)
   2767 				wcred = NOCRED;
   2768 			bp->b_flags |= (B_BUSY | B_WRITEINPROG);
   2769 			/*
   2770 			 * A list of these buffers is kept so that the
   2771 			 * second loop knows which buffers have actually
   2772 			 * been committed. This is necessary, since there
   2773 			 * may be a race between the commit rpc and new
   2774 			 * uncommitted writes on the file.
   2775 			 */
   2776 			bvec[bvecpos++] = bp;
   2777 			toff = ((u_quad_t)bp->b_blkno) * DEV_BSIZE +
   2778 				bp->b_dirtyoff;
   2779 			if (toff < off)
   2780 				off = toff;
   2781 			toff += (u_quad_t)(bp->b_dirtyend - bp->b_dirtyoff);
   2782 			if (toff > endoff)
   2783 				endoff = toff;
   2784 		}
   2785 		splx(s);
   2786 	}
   2787 	if (bvecpos > 0) {
   2788 		/*
   2789 		 * Commit data on the server, as required.
   2790 		 * If all bufs are using the same wcred, then use that with
   2791 		 * one call for all of them, otherwise commit each one
   2792 		 * separately.
   2793 		 */
   2794 		if (wcred != NOCRED)
   2795 			retv = nfs_commit(vp, off, (int)(endoff - off),
   2796 					  wcred, p);
   2797 		else {
   2798 			retv = 0;
   2799 			for (i = 0; i < bvecpos; i++) {
   2800 				off_t off, size;
   2801 				bp = bvec[i];
   2802 				off = ((u_quad_t)bp->b_blkno) * DEV_BSIZE +
   2803 					bp->b_dirtyoff;
   2804 				size = (u_quad_t)(bp->b_dirtyend
   2805 						  - bp->b_dirtyoff);
   2806 				retv = nfs_commit(vp, off, (int)size,
   2807 						  bp->b_wcred, p);
   2808 				if (retv) break;
   2809 			}
   2810 		}
   2811 
   2812 		if (retv == NFSERR_STALEWRITEVERF)
   2813 			nfs_clearcommit(vp->v_mount);
   2814 		/*
   2815 		 * Now, either mark the blocks I/O done or mark the
   2816 		 * blocks dirty, depending on whether the commit
   2817 		 * succeeded.
   2818 		 */
   2819 		for (i = 0; i < bvecpos; i++) {
   2820 			bp = bvec[i];
   2821 			bp->b_flags &= ~(B_NEEDCOMMIT | B_WRITEINPROG);
   2822 			if (retv)
   2823 			    brelse(bp);
   2824 			else {
   2825 			    vp->v_numoutput++;
   2826 			    bp->b_flags |= B_ASYNC;
   2827 			    bp->b_flags &= ~(B_READ|B_DONE|B_ERROR|B_DELWRI);
   2828 			    bp->b_dirtyoff = bp->b_dirtyend = 0;
   2829 			    reassignbuf(bp, vp);
   2830 			    biodone(bp);
   2831 			}
   2832 		}
   2833 	}
   2834 
   2835 	/*
   2836 	 * Start/do any write(s) that are required.
   2837 	 */
   2838 loop:
   2839 	s = splbio();
   2840 	for (bp = vp->v_dirtyblkhd.lh_first; bp; bp = nbp) {
   2841 		nbp = bp->b_vnbufs.le_next;
   2842 		if (bp->b_flags & B_BUSY) {
   2843 			if (waitfor != MNT_WAIT || passone)
   2844 				continue;
   2845 			bp->b_flags |= B_WANTED;
   2846 			error = tsleep((caddr_t)bp, slpflag | (PRIBIO + 1),
   2847 				"nfsfsync", slptimeo);
   2848 			splx(s);
   2849 			if (error) {
   2850 			    if (nfs_sigintr(nmp, (struct nfsreq *)0, p))
   2851 				return (EINTR);
   2852 			    if (slpflag == PCATCH) {
   2853 				slpflag = 0;
   2854 				slptimeo = 2 * hz;
   2855 			    }
   2856 			}
   2857 			goto loop;
   2858 		}
   2859 		if ((bp->b_flags & B_DELWRI) == 0)
   2860 			panic("nfs_fsync: not dirty");
   2861 		if ((passone || !commit) && (bp->b_flags & B_NEEDCOMMIT))
   2862 			continue;
   2863 		bremfree(bp);
   2864 		if (passone || !commit)
   2865 		    bp->b_flags |= (B_BUSY|B_ASYNC);
   2866 		else
   2867 		    bp->b_flags |= (B_BUSY|B_ASYNC|B_WRITEINPROG|B_NEEDCOMMIT);
   2868 		splx(s);
   2869 		VOP_BWRITE(bp);
   2870 		goto loop;
   2871 	}
   2872 	splx(s);
   2873 	if (passone) {
   2874 		passone = 0;
   2875 		goto again;
   2876 	}
   2877 	if (waitfor == MNT_WAIT) {
   2878 		while (vp->v_numoutput) {
   2879 			vp->v_flag |= VBWAIT;
   2880 			error = tsleep((caddr_t)&vp->v_numoutput,
   2881 				slpflag | (PRIBIO + 1), "nfsfsync", slptimeo);
   2882 			if (error) {
   2883 			    if (nfs_sigintr(nmp, (struct nfsreq *)0, p))
   2884 				return (EINTR);
   2885 			    if (slpflag == PCATCH) {
   2886 				slpflag = 0;
   2887 				slptimeo = 2 * hz;
   2888 			    }
   2889 			}
   2890 		}
   2891 		if (vp->v_dirtyblkhd.lh_first && commit) {
   2892 #if 0
   2893 			vprint("nfs_fsync: dirty", vp);
   2894 #endif
   2895 			goto loop;
   2896 		}
   2897 	}
   2898 	if (np->n_flag & NWRITEERR) {
   2899 		error = np->n_error;
   2900 		np->n_flag &= ~NWRITEERR;
   2901 	}
   2902 	return (error);
   2903 }
   2904 
   2905 /*
   2906  * Return POSIX pathconf information applicable to nfs.
   2907  *
   2908  * N.B. The NFS V2 protocol doesn't support this RPC.
   2909  */
   2910 /* ARGSUSED */
   2911 int
   2912 nfs_pathconf(v)
   2913 	void *v;
   2914 {
   2915 	struct vop_pathconf_args /* {
   2916 		struct vnode *a_vp;
   2917 		int a_name;
   2918 		register_t *a_retval;
   2919 	} */ *ap = v;
   2920 	struct nfsv3_pathconf *pcp;
   2921 	struct vnode *vp = ap->a_vp;
   2922 	struct nfsmount *nmp;
   2923 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
   2924 	int32_t t1, t2;
   2925 	u_int32_t *tl;
   2926 	caddr_t bpos, dpos, cp, cp2;
   2927 	int error = 0, attrflag;
   2928 	unsigned int l;
   2929 	u_int64_t maxsize;
   2930 	int v3 = NFS_ISV3(vp);
   2931 
   2932 	switch (ap->a_name) {
   2933 		/* Names that can be resolved locally. */
   2934 	case _PC_PIPE_BUF:
   2935 		*ap->a_retval = PIPE_BUF;
   2936 		break;
   2937 	case _PC_SYNC_IO:
   2938 		*ap->a_retval = 1;
   2939 		break;
   2940 	/* Names that cannot be resolved locally; do an RPC, if possible. */
   2941 	case _PC_LINK_MAX:
   2942 	case _PC_NAME_MAX:
   2943 	case _PC_CHOWN_RESTRICTED:
   2944 	case _PC_NO_TRUNC:
   2945 		if (!v3) {
   2946 			error = EINVAL;
   2947 			break;
   2948 		}
   2949 		nfsstats.rpccnt[NFSPROC_PATHCONF]++;
   2950 		nfsm_reqhead(vp, NFSPROC_PATHCONF, NFSX_FH(1));
   2951 		nfsm_fhtom(vp, 1);
   2952 		nfsm_request(vp, NFSPROC_PATHCONF,
   2953 		    curproc, curproc->p_ucred);	/* XXX */
   2954 		nfsm_postop_attr(vp, attrflag);
   2955 		if (!error) {
   2956 			nfsm_dissect(pcp, struct nfsv3_pathconf *,
   2957 			    NFSX_V3PATHCONF);
   2958 			switch (ap->a_name) {
   2959 			case _PC_LINK_MAX:
   2960 				*ap->a_retval =
   2961 				    fxdr_unsigned(register_t, pcp->pc_linkmax);
   2962 				break;
   2963 			case _PC_NAME_MAX:
   2964 				*ap->a_retval =
   2965 				    fxdr_unsigned(register_t, pcp->pc_namemax);
   2966 				break;
   2967 			case _PC_CHOWN_RESTRICTED:
   2968 				*ap->a_retval =
   2969 				    (pcp->pc_chownrestricted == nfs_true);
   2970 				break;
   2971 			case _PC_NO_TRUNC:
   2972 				*ap->a_retval =
   2973 				    (pcp->pc_notrunc == nfs_true);
   2974 				break;
   2975 			}
   2976 		}
   2977 		nfsm_reqdone;
   2978 		break;
   2979 	case _PC_FILESIZEBITS:
   2980 		if (v3) {
   2981 			nmp = VFSTONFS(vp->v_mount);
   2982 			if ((nmp->nm_iflag & NFSMNT_GOTFSINFO) == 0)
   2983 				if ((error = nfs_fsinfo(nmp, vp,
   2984 				    curproc->p_ucred, curproc)) != 0) /* XXX */
   2985 					break;
   2986 			for (l = 0, maxsize = nmp->nm_maxfilesize;
   2987 			    (maxsize >> l) > 0; l++)
   2988 				;
   2989 			*ap->a_retval = l + 1;
   2990 		} else {
   2991 			*ap->a_retval = 32;	/* NFS V2 limitation */
   2992 		}
   2993 		break;
   2994 	default:
   2995 		error = EINVAL;
   2996 		break;
   2997 	}
   2998 
   2999 	return (error);
   3000 }
   3001 
   3002 /*
   3003  * NFS advisory byte-level locks.
   3004  */
   3005 int
   3006 nfs_advlock(v)
   3007 	void *v;
   3008 {
   3009 	struct vop_advlock_args /* {
   3010 		struct vnode *a_vp;
   3011 		caddr_t  a_id;
   3012 		int  a_op;
   3013 		struct flock *a_fl;
   3014 		int  a_flags;
   3015 	} */ *ap = v;
   3016 	register struct nfsnode *np = VTONFS(ap->a_vp);
   3017 
   3018 	return (lf_advlock(&np->n_lockf, np->n_size, ap->a_id, ap->a_op,
   3019 	    ap->a_fl, ap->a_flags));
   3020 }
   3021 
   3022 /*
   3023  * Print out the contents of an nfsnode.
   3024  */
   3025 int
   3026 nfs_print(v)
   3027 	void *v;
   3028 {
   3029 	struct vop_print_args /* {
   3030 		struct vnode *a_vp;
   3031 	} */ *ap = v;
   3032 	register struct vnode *vp = ap->a_vp;
   3033 	register struct nfsnode *np = VTONFS(vp);
   3034 
   3035 	printf("tag VT_NFS, fileid %ld fsid 0x%lx",
   3036 	    np->n_vattr->va_fileid, np->n_vattr->va_fsid);
   3037 	if (vp->v_type == VFIFO)
   3038 		fifo_printinfo(vp);
   3039 	printf("\n");
   3040 	return (0);
   3041 }
   3042 
   3043 /*
   3044  * NFS file truncation.
   3045  */
   3046 int
   3047 nfs_truncate(v)
   3048 	void *v;
   3049 {
   3050 #if 0
   3051 	struct vop_truncate_args /* {
   3052 		struct vnode *a_vp;
   3053 		off_t a_length;
   3054 		int a_flags;
   3055 		struct ucred *a_cred;
   3056 		struct proc *a_p;
   3057 	} */ *ap = v;
   3058 #endif
   3059 
   3060 	/* Use nfs_setattr */
   3061 	return (EOPNOTSUPP);
   3062 }
   3063 
   3064 /*
   3065  * NFS update.
   3066  */
   3067 int
   3068 nfs_update(v)
   3069 	void *v;
   3070 #if 0
   3071 	struct vop_update_args /* {
   3072 		struct vnode *a_vp;
   3073 		struct timespec *a_ta;
   3074 		struct timespec *a_tm;
   3075 		int a_waitfor;
   3076 	} */ *ap = v;
   3077 #endif
   3078 {
   3079 
   3080 	/* Use nfs_setattr */
   3081 	return (EOPNOTSUPP);
   3082 }
   3083 
   3084 /*
   3085  * Just call nfs_writebp() with the force argument set to 1.
   3086  */
   3087 int
   3088 nfs_bwrite(v)
   3089 	void *v;
   3090 {
   3091 	struct vop_bwrite_args /* {
   3092 		struct vnode *a_bp;
   3093 	} */ *ap = v;
   3094 
   3095 	return (nfs_writebp(ap->a_bp, 1));
   3096 }
   3097 
   3098 /*
   3099  * This is a clone of vn_bwrite(), except that B_WRITEINPROG isn't set unless
   3100  * the force flag is one and it also handles the B_NEEDCOMMIT flag.
   3101  */
   3102 int
   3103 nfs_writebp(bp, force)
   3104 	register struct buf *bp;
   3105 	int force;
   3106 {
   3107 	register int oldflags = bp->b_flags, retv = 1;
   3108 	register struct proc *p = curproc;	/* XXX */
   3109 	off_t off;
   3110 
   3111 	if(!(bp->b_flags & B_BUSY))
   3112 		panic("bwrite: buffer is not busy???");
   3113 
   3114 #ifdef fvdl_debug
   3115 	printf("nfs_writebp(%x): vp %x voff %d vend %d doff %d dend %d\n",
   3116 	    bp, bp->b_vp, bp->b_validoff, bp->b_validend, bp->b_dirtyoff,
   3117 	    bp->b_dirtyend);
   3118 #endif
   3119 	bp->b_flags &= ~(B_READ|B_DONE|B_ERROR|B_DELWRI|B_AGE);
   3120 
   3121 	if (oldflags & B_ASYNC) {
   3122 		if (oldflags & B_DELWRI) {
   3123 			reassignbuf(bp, bp->b_vp);
   3124 		} else if (p) {
   3125 			++p->p_stats->p_ru.ru_oublock;
   3126 		}
   3127 	}
   3128 	bp->b_vp->v_numoutput++;
   3129 
   3130 	/*
   3131 	 * If B_NEEDCOMMIT is set, a commit rpc may do the trick. If not
   3132 	 * an actual write will have to be scheduled via. VOP_STRATEGY().
   3133 	 * If B_WRITEINPROG is already set, then push it with a write anyhow.
   3134 	 */
   3135 	if ((oldflags & (B_NEEDCOMMIT | B_WRITEINPROG)) == B_NEEDCOMMIT) {
   3136 		off = ((u_quad_t)bp->b_blkno) * DEV_BSIZE + bp->b_dirtyoff;
   3137 		bp->b_flags |= B_WRITEINPROG;
   3138 		retv = nfs_commit(bp->b_vp, off, bp->b_dirtyend-bp->b_dirtyoff,
   3139 			bp->b_wcred, bp->b_proc);
   3140 		bp->b_flags &= ~B_WRITEINPROG;
   3141 		if (!retv) {
   3142 			bp->b_dirtyoff = bp->b_dirtyend = 0;
   3143 			bp->b_flags &= ~B_NEEDCOMMIT;
   3144 			biodone(bp);
   3145 		} else if (retv == NFSERR_STALEWRITEVERF)
   3146 			nfs_clearcommit(bp->b_vp->v_mount);
   3147 	}
   3148 	if (retv) {
   3149 		if (force)
   3150 			bp->b_flags |= B_WRITEINPROG;
   3151 		VOP_STRATEGY(bp);
   3152 	}
   3153 
   3154 	if( (oldflags & B_ASYNC) == 0) {
   3155 		int rtval = biowait(bp);
   3156 		if (oldflags & B_DELWRI) {
   3157 			reassignbuf(bp, bp->b_vp);
   3158 		} else if (p) {
   3159 			++p->p_stats->p_ru.ru_oublock;
   3160 		}
   3161 		brelse(bp);
   3162 		return (rtval);
   3163 	}
   3164 
   3165 	return (0);
   3166 }
   3167 
   3168 /*
   3169  * nfs special file access vnode op.
   3170  * Essentially just get vattr and then imitate iaccess() since the device is
   3171  * local to the client.
   3172  */
   3173 int
   3174 nfsspec_access(v)
   3175 	void *v;
   3176 {
   3177 	struct vop_access_args /* {
   3178 		struct vnode *a_vp;
   3179 		int  a_mode;
   3180 		struct ucred *a_cred;
   3181 		struct proc *a_p;
   3182 	} */ *ap = v;
   3183 	struct vattr va;
   3184 	struct vnode *vp = ap->a_vp;
   3185 	int error;
   3186 
   3187 	error = VOP_GETATTR(vp, &va, ap->a_cred, ap->a_p);
   3188 	if (error)
   3189 		return (error);
   3190 
   3191         /*
   3192 	 * Disallow write attempts on filesystems mounted read-only;
   3193 	 * unless the file is a socket, fifo, or a block or character
   3194 	 * device resident on the filesystem.
   3195 	 */
   3196 	if ((ap->a_mode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY)) {
   3197 		switch (vp->v_type) {
   3198 		case VREG:
   3199 		case VDIR:
   3200 		case VLNK:
   3201 			return (EROFS);
   3202 		default:
   3203 			break;
   3204 		}
   3205 	}
   3206 
   3207 	return (vaccess(va.va_type, va.va_mode,
   3208 	    va.va_uid, va.va_gid, ap->a_mode, ap->a_cred));
   3209 }
   3210 
   3211 /*
   3212  * Read wrapper for special devices.
   3213  */
   3214 int
   3215 nfsspec_read(v)
   3216 	void *v;
   3217 {
   3218 	struct vop_read_args /* {
   3219 		struct vnode *a_vp;
   3220 		struct uio *a_uio;
   3221 		int  a_ioflag;
   3222 		struct ucred *a_cred;
   3223 	} */ *ap = v;
   3224 	register struct nfsnode *np = VTONFS(ap->a_vp);
   3225 
   3226 	/*
   3227 	 * Set access flag.
   3228 	 */
   3229 	np->n_flag |= NACC;
   3230 	np->n_atim.tv_sec = time.tv_sec;
   3231 	np->n_atim.tv_nsec = time.tv_usec * 1000;
   3232 	return (VOCALL(spec_vnodeop_p, VOFFSET(vop_read), ap));
   3233 }
   3234 
   3235 /*
   3236  * Write wrapper for special devices.
   3237  */
   3238 int
   3239 nfsspec_write(v)
   3240 	void *v;
   3241 {
   3242 	struct vop_write_args /* {
   3243 		struct vnode *a_vp;
   3244 		struct uio *a_uio;
   3245 		int  a_ioflag;
   3246 		struct ucred *a_cred;
   3247 	} */ *ap = v;
   3248 	register struct nfsnode *np = VTONFS(ap->a_vp);
   3249 
   3250 	/*
   3251 	 * Set update flag.
   3252 	 */
   3253 	np->n_flag |= NUPD;
   3254 	np->n_mtim.tv_sec = time.tv_sec;
   3255 	np->n_mtim.tv_nsec = time.tv_usec * 1000;
   3256 	return (VOCALL(spec_vnodeop_p, VOFFSET(vop_write), ap));
   3257 }
   3258 
   3259 /*
   3260  * Close wrapper for special devices.
   3261  *
   3262  * Update the times on the nfsnode then do device close.
   3263  */
   3264 int
   3265 nfsspec_close(v)
   3266 	void *v;
   3267 {
   3268 	struct vop_close_args /* {
   3269 		struct vnode *a_vp;
   3270 		int  a_fflag;
   3271 		struct ucred *a_cred;
   3272 		struct proc *a_p;
   3273 	} */ *ap = v;
   3274 	register struct vnode *vp = ap->a_vp;
   3275 	register struct nfsnode *np = VTONFS(vp);
   3276 	struct vattr vattr;
   3277 
   3278 	if (np->n_flag & (NACC | NUPD)) {
   3279 		np->n_flag |= NCHG;
   3280 		if (vp->v_usecount == 1 &&
   3281 		    (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
   3282 			VATTR_NULL(&vattr);
   3283 			if (np->n_flag & NACC)
   3284 				vattr.va_atime = np->n_atim;
   3285 			if (np->n_flag & NUPD)
   3286 				vattr.va_mtime = np->n_mtim;
   3287 			(void)VOP_SETATTR(vp, &vattr, ap->a_cred, ap->a_p);
   3288 		}
   3289 	}
   3290 	return (VOCALL(spec_vnodeop_p, VOFFSET(vop_close), ap));
   3291 }
   3292 
   3293 /*
   3294  * Read wrapper for fifos.
   3295  */
   3296 int
   3297 nfsfifo_read(v)
   3298 	void *v;
   3299 {
   3300 	struct vop_read_args /* {
   3301 		struct vnode *a_vp;
   3302 		struct uio *a_uio;
   3303 		int  a_ioflag;
   3304 		struct ucred *a_cred;
   3305 	} */ *ap = v;
   3306 	extern int (**fifo_vnodeop_p) __P((void *));
   3307 	register struct nfsnode *np = VTONFS(ap->a_vp);
   3308 
   3309 	/*
   3310 	 * Set access flag.
   3311 	 */
   3312 	np->n_flag |= NACC;
   3313 	np->n_atim.tv_sec = time.tv_sec;
   3314 	np->n_atim.tv_nsec = time.tv_usec * 1000;
   3315 	return (VOCALL(fifo_vnodeop_p, VOFFSET(vop_read), ap));
   3316 }
   3317 
   3318 /*
   3319  * Write wrapper for fifos.
   3320  */
   3321 int
   3322 nfsfifo_write(v)
   3323 	void *v;
   3324 {
   3325 	struct vop_write_args /* {
   3326 		struct vnode *a_vp;
   3327 		struct uio *a_uio;
   3328 		int  a_ioflag;
   3329 		struct ucred *a_cred;
   3330 	} */ *ap = v;
   3331 	extern int (**fifo_vnodeop_p) __P((void *));
   3332 	register struct nfsnode *np = VTONFS(ap->a_vp);
   3333 
   3334 	/*
   3335 	 * Set update flag.
   3336 	 */
   3337 	np->n_flag |= NUPD;
   3338 	np->n_mtim.tv_sec = time.tv_sec;
   3339 	np->n_mtim.tv_nsec = time.tv_usec * 1000;
   3340 	return (VOCALL(fifo_vnodeop_p, VOFFSET(vop_write), ap));
   3341 }
   3342 
   3343 /*
   3344  * Close wrapper for fifos.
   3345  *
   3346  * Update the times on the nfsnode then do fifo close.
   3347  */
   3348 int
   3349 nfsfifo_close(v)
   3350 	void *v;
   3351 {
   3352 	struct vop_close_args /* {
   3353 		struct vnode *a_vp;
   3354 		int  a_fflag;
   3355 		struct ucred *a_cred;
   3356 		struct proc *a_p;
   3357 	} */ *ap = v;
   3358 	register struct vnode *vp = ap->a_vp;
   3359 	register struct nfsnode *np = VTONFS(vp);
   3360 	struct vattr vattr;
   3361 	extern int (**fifo_vnodeop_p) __P((void *));
   3362 
   3363 	if (np->n_flag & (NACC | NUPD)) {
   3364 		if (np->n_flag & NACC) {
   3365 			np->n_atim.tv_sec = time.tv_sec;
   3366 			np->n_atim.tv_nsec = time.tv_usec * 1000;
   3367 		}
   3368 		if (np->n_flag & NUPD) {
   3369 			np->n_mtim.tv_sec = time.tv_sec;
   3370 			np->n_mtim.tv_nsec = time.tv_usec * 1000;
   3371 		}
   3372 		np->n_flag |= NCHG;
   3373 		if (vp->v_usecount == 1 &&
   3374 		    (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
   3375 			VATTR_NULL(&vattr);
   3376 			if (np->n_flag & NACC)
   3377 				vattr.va_atime = np->n_atim;
   3378 			if (np->n_flag & NUPD)
   3379 				vattr.va_mtime = np->n_mtim;
   3380 			(void)VOP_SETATTR(vp, &vattr, ap->a_cred, ap->a_p);
   3381 		}
   3382 	}
   3383 	return (VOCALL(fifo_vnodeop_p, VOFFSET(vop_close), ap));
   3384 }
   3385