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