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