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