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