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