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