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