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