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