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