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