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