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