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