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