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