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