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