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