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