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