Home | History | Annotate | Line # | Download | only in nfs
nfs_vnops.c revision 1.58
      1 /*	$NetBSD: nfs_vnops.c,v 1.58 1996/02/09 21:48:41 christos 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.10 (Berkeley) 8/11/94
     39  */
     40 
     41 /*
     42  * vnode op calls for sun nfs version 2
     43  */
     44 
     45 #include <sys/param.h>
     46 #include <sys/proc.h>
     47 #include <sys/kernel.h>
     48 #include <sys/systm.h>
     49 #include <sys/mount.h>
     50 #include <sys/buf.h>
     51 #include <sys/malloc.h>
     52 #include <sys/mbuf.h>
     53 #include <sys/conf.h>
     54 #include <sys/namei.h>
     55 #include <sys/vnode.h>
     56 #include <sys/map.h>
     57 #include <sys/dirent.h>
     58 #include <sys/lockf.h>
     59 
     60 #include <vm/vm.h>
     61 
     62 #include <miscfs/specfs/specdev.h>
     63 #include <miscfs/fifofs/fifo.h>
     64 
     65 #include <nfs/rpcv2.h>
     66 #include <nfs/nfsv2.h>
     67 #include <nfs/nfs.h>
     68 #include <nfs/nfsnode.h>
     69 #include <nfs/nfsmount.h>
     70 #include <nfs/xdr_subs.h>
     71 #include <nfs/nfsm_subs.h>
     72 #include <nfs/nqnfs.h>
     73 #include <nfs/nfs_var.h>
     74 
     75 /* Defs */
     76 #define	TRUE	1
     77 #define	FALSE	0
     78 
     79 /*
     80  * Global vfs data structures for nfs
     81  */
     82 int (**nfsv2_vnodeop_p) __P((void *));
     83 struct vnodeopv_entry_desc nfsv2_vnodeop_entries[] = {
     84 	{ &vop_default_desc, vn_default_error },
     85 	{ &vop_lookup_desc, nfs_lookup },	/* lookup */
     86 	{ &vop_create_desc, nfs_create },	/* create */
     87 	{ &vop_mknod_desc, nfs_mknod },		/* mknod */
     88 	{ &vop_open_desc, nfs_open },		/* open */
     89 	{ &vop_close_desc, nfs_close },		/* close */
     90 	{ &vop_access_desc, nfs_access },	/* access */
     91 	{ &vop_getattr_desc, nfs_getattr },	/* getattr */
     92 	{ &vop_setattr_desc, nfs_setattr },	/* setattr */
     93 	{ &vop_read_desc, nfs_read },		/* read */
     94 	{ &vop_write_desc, nfs_write },		/* write */
     95 	{ &vop_lease_desc, nfs_lease_check },	/* lease */
     96 	{ &vop_ioctl_desc, nfs_ioctl },		/* ioctl */
     97 	{ &vop_select_desc, nfs_select },	/* select */
     98 	{ &vop_mmap_desc, nfs_mmap },		/* mmap */
     99 	{ &vop_fsync_desc, nfs_fsync },		/* fsync */
    100 	{ &vop_seek_desc, nfs_seek },		/* seek */
    101 	{ &vop_remove_desc, nfs_remove },	/* remove */
    102 	{ &vop_link_desc, nfs_link },		/* link */
    103 	{ &vop_rename_desc, nfs_rename },	/* rename */
    104 	{ &vop_mkdir_desc, nfs_mkdir },		/* mkdir */
    105 	{ &vop_rmdir_desc, nfs_rmdir },		/* rmdir */
    106 	{ &vop_symlink_desc, nfs_symlink },	/* symlink */
    107 	{ &vop_readdir_desc, nfs_readdir },	/* readdir */
    108 	{ &vop_readlink_desc, nfs_readlink },	/* readlink */
    109 	{ &vop_abortop_desc, nfs_abortop },	/* abortop */
    110 	{ &vop_inactive_desc, nfs_inactive },	/* inactive */
    111 	{ &vop_reclaim_desc, nfs_reclaim },	/* reclaim */
    112 	{ &vop_lock_desc, nfs_lock },		/* lock */
    113 	{ &vop_unlock_desc, nfs_unlock },	/* unlock */
    114 	{ &vop_bmap_desc, nfs_bmap },		/* bmap */
    115 	{ &vop_strategy_desc, nfs_strategy },	/* strategy */
    116 	{ &vop_print_desc, nfs_print },		/* print */
    117 	{ &vop_islocked_desc, nfs_islocked },	/* islocked */
    118 	{ &vop_pathconf_desc, nfs_pathconf },	/* pathconf */
    119 	{ &vop_advlock_desc, nfs_advlock },	/* advlock */
    120 	{ &vop_blkatoff_desc, nfs_blkatoff },	/* blkatoff */
    121 	{ &vop_valloc_desc, nfs_valloc },	/* valloc */
    122 	{ &vop_reallocblks_desc, nfs_reallocblks },	/* reallocblks */
    123 	{ &vop_vfree_desc, nfs_vfree },		/* vfree */
    124 	{ &vop_truncate_desc, nfs_truncate },	/* truncate */
    125 	{ &vop_update_desc, nfs_update },	/* update */
    126 	{ &vop_bwrite_desc, vn_bwrite },
    127 	{ (struct vnodeop_desc*)NULL, (int(*) __P((void *)))NULL }
    128 };
    129 struct vnodeopv_desc nfsv2_vnodeop_opv_desc =
    130 	{ &nfsv2_vnodeop_p, nfsv2_vnodeop_entries };
    131 
    132 /*
    133  * Special device vnode ops
    134  */
    135 int (**spec_nfsv2nodeop_p) __P((void *));
    136 struct vnodeopv_entry_desc spec_nfsv2nodeop_entries[] = {
    137 	{ &vop_default_desc, vn_default_error },
    138 	{ &vop_lookup_desc, spec_lookup },	/* lookup */
    139 	{ &vop_create_desc, spec_create },	/* create */
    140 	{ &vop_mknod_desc, spec_mknod },	/* mknod */
    141 	{ &vop_open_desc, spec_open },		/* open */
    142 	{ &vop_close_desc, nfsspec_close },	/* close */
    143 	{ &vop_access_desc, nfsspec_access },	/* access */
    144 	{ &vop_getattr_desc, nfs_getattr },	/* getattr */
    145 	{ &vop_setattr_desc, nfs_setattr },	/* setattr */
    146 	{ &vop_read_desc, nfsspec_read },	/* read */
    147 	{ &vop_write_desc, nfsspec_write },	/* write */
    148 	{ &vop_lease_desc, spec_lease_check },	/* lease */
    149 	{ &vop_ioctl_desc, spec_ioctl },	/* ioctl */
    150 	{ &vop_select_desc, spec_select },	/* select */
    151 	{ &vop_mmap_desc, spec_mmap },		/* mmap */
    152 	{ &vop_fsync_desc, nfs_fsync },		/* fsync */
    153 	{ &vop_seek_desc, spec_seek },		/* seek */
    154 	{ &vop_remove_desc, spec_remove },	/* remove */
    155 	{ &vop_link_desc, spec_link },		/* link */
    156 	{ &vop_rename_desc, spec_rename },	/* rename */
    157 	{ &vop_mkdir_desc, spec_mkdir },	/* mkdir */
    158 	{ &vop_rmdir_desc, spec_rmdir },	/* rmdir */
    159 	{ &vop_symlink_desc, spec_symlink },	/* symlink */
    160 	{ &vop_readdir_desc, spec_readdir },	/* readdir */
    161 	{ &vop_readlink_desc, spec_readlink },	/* readlink */
    162 	{ &vop_abortop_desc, spec_abortop },	/* abortop */
    163 	{ &vop_inactive_desc, nfs_inactive },	/* inactive */
    164 	{ &vop_reclaim_desc, nfs_reclaim },	/* reclaim */
    165 	{ &vop_lock_desc, nfs_lock },		/* lock */
    166 	{ &vop_unlock_desc, nfs_unlock },	/* unlock */
    167 	{ &vop_bmap_desc, spec_bmap },		/* bmap */
    168 	{ &vop_strategy_desc, spec_strategy },	/* strategy */
    169 	{ &vop_print_desc, nfs_print },		/* print */
    170 	{ &vop_islocked_desc, nfs_islocked },	/* islocked */
    171 	{ &vop_pathconf_desc, spec_pathconf },	/* pathconf */
    172 	{ &vop_advlock_desc, spec_advlock },	/* advlock */
    173 	{ &vop_blkatoff_desc, spec_blkatoff },	/* blkatoff */
    174 	{ &vop_valloc_desc, spec_valloc },	/* valloc */
    175 	{ &vop_reallocblks_desc, spec_reallocblks },	/* reallocblks */
    176 	{ &vop_vfree_desc, spec_vfree },	/* vfree */
    177 	{ &vop_truncate_desc, spec_truncate },	/* truncate */
    178 	{ &vop_update_desc, nfs_update },	/* update */
    179 	{ &vop_bwrite_desc, vn_bwrite },
    180 	{ (struct vnodeop_desc*)NULL, (int(*) __P((void *)))NULL }
    181 };
    182 struct vnodeopv_desc spec_nfsv2nodeop_opv_desc =
    183 	{ &spec_nfsv2nodeop_p, spec_nfsv2nodeop_entries };
    184 
    185 #ifdef FIFO
    186 int (**fifo_nfsv2nodeop_p) __P((void *));
    187 struct vnodeopv_entry_desc fifo_nfsv2nodeop_entries[] = {
    188 	{ &vop_default_desc, vn_default_error },
    189 	{ &vop_lookup_desc, fifo_lookup },	/* lookup */
    190 	{ &vop_create_desc, fifo_create },	/* create */
    191 	{ &vop_mknod_desc, fifo_mknod },	/* mknod */
    192 	{ &vop_open_desc, fifo_open },		/* open */
    193 	{ &vop_close_desc, nfsfifo_close },	/* close */
    194 	{ &vop_access_desc, nfsspec_access },	/* access */
    195 	{ &vop_getattr_desc, nfs_getattr },	/* getattr */
    196 	{ &vop_setattr_desc, nfs_setattr },	/* setattr */
    197 	{ &vop_read_desc, nfsfifo_read },	/* read */
    198 	{ &vop_write_desc, nfsfifo_write },	/* write */
    199 	{ &vop_lease_desc, fifo_lease_check },	/* lease */
    200 	{ &vop_ioctl_desc, fifo_ioctl },	/* ioctl */
    201 	{ &vop_select_desc, fifo_select },	/* select */
    202 	{ &vop_mmap_desc, fifo_mmap },		/* mmap */
    203 	{ &vop_fsync_desc, nfs_fsync },		/* fsync */
    204 	{ &vop_seek_desc, fifo_seek },		/* seek */
    205 	{ &vop_remove_desc, fifo_remove },	/* remove */
    206 	{ &vop_link_desc, fifo_link },		/* link */
    207 	{ &vop_rename_desc, fifo_rename },	/* rename */
    208 	{ &vop_mkdir_desc, fifo_mkdir },	/* mkdir */
    209 	{ &vop_rmdir_desc, fifo_rmdir },	/* rmdir */
    210 	{ &vop_symlink_desc, fifo_symlink },	/* symlink */
    211 	{ &vop_readdir_desc, fifo_readdir },	/* readdir */
    212 	{ &vop_readlink_desc, fifo_readlink },	/* readlink */
    213 	{ &vop_abortop_desc, fifo_abortop },	/* abortop */
    214 	{ &vop_inactive_desc, nfs_inactive },	/* inactive */
    215 	{ &vop_reclaim_desc, nfs_reclaim },	/* reclaim */
    216 	{ &vop_lock_desc, nfs_lock },		/* lock */
    217 	{ &vop_unlock_desc, nfs_unlock },	/* unlock */
    218 	{ &vop_bmap_desc, fifo_bmap },		/* bmap */
    219 	{ &vop_strategy_desc, fifo_badop },	/* strategy */
    220 	{ &vop_print_desc, nfs_print },		/* print */
    221 	{ &vop_islocked_desc, nfs_islocked },	/* islocked */
    222 	{ &vop_pathconf_desc, fifo_pathconf },	/* pathconf */
    223 	{ &vop_advlock_desc, fifo_advlock },	/* advlock */
    224 	{ &vop_blkatoff_desc, fifo_blkatoff },	/* blkatoff */
    225 	{ &vop_valloc_desc, fifo_valloc },	/* valloc */
    226 	{ &vop_reallocblks_desc, fifo_reallocblks },	/* reallocblks */
    227 	{ &vop_vfree_desc, fifo_vfree },	/* vfree */
    228 	{ &vop_truncate_desc, fifo_truncate },	/* truncate */
    229 	{ &vop_update_desc, nfs_update },	/* update */
    230 	{ &vop_bwrite_desc, vn_bwrite },
    231 	{ (struct vnodeop_desc*)NULL, (int(*) __P((void *)))NULL }
    232 };
    233 struct vnodeopv_desc fifo_nfsv2nodeop_opv_desc =
    234 	{ &fifo_nfsv2nodeop_p, fifo_nfsv2nodeop_entries };
    235 #endif /* FIFO */
    236 
    237 /*
    238  * Global variables
    239  */
    240 extern u_int32_t nfs_procids[NFS_NPROCS];
    241 extern u_int32_t nfs_xdrneg1;
    242 extern u_int32_t nfs_prog, nfs_vers, nfs_true, nfs_false;
    243 struct proc *nfs_iodwant[NFS_MAXASYNCDAEMON];
    244 int nfs_numasync = 0;
    245 #define	DIRHDSIZ	(sizeof (struct dirent) - (MAXNAMLEN + 1))
    246 
    247 /*
    248  * nfs null call from vfs.
    249  */
    250 int
    251 nfs_null(vp, cred, procp)
    252 	struct vnode *vp;
    253 	struct ucred *cred;
    254 	struct proc *procp;
    255 {
    256 	caddr_t bpos, dpos;
    257 	int error = 0;
    258 	struct mbuf *mreq, *mrep, *md, *mb;
    259 
    260 	nfsm_reqhead(vp, NFSPROC_NULL, 0);
    261 	nfsm_request(vp, NFSPROC_NULL, procp, cred);
    262 	nfsm_reqdone;
    263 	return (error);
    264 }
    265 
    266 /*
    267  * nfs access vnode op.
    268  * For nfs, just return ok. File accesses may fail later.
    269  * For nqnfs, use the access rpc to check accessibility. If file modes are
    270  * changed on the server, accesses might still fail later.
    271  */
    272 int
    273 nfs_access(v)
    274 	void *v;
    275 {
    276 	struct vop_access_args /* {
    277 		struct vnode *a_vp;
    278 		int  a_mode;
    279 		struct ucred *a_cred;
    280 		struct proc *a_p;
    281 	} */ *ap = v;
    282 	register struct vnode *vp = ap->a_vp;
    283 	register u_int32_t *tl;
    284 	register caddr_t cp;
    285 	caddr_t bpos, dpos;
    286 	int error = 0;
    287 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
    288 
    289 	/*
    290 	 * For nqnfs, do an access rpc, otherwise you are stuck emulating
    291 	 * ufs_access() locally using the vattr. This may not be correct,
    292 	 * since the server may apply other access criteria such as
    293 	 * client uid-->server uid mapping that we do not know about, but
    294 	 * this is better than just returning anything that is lying about
    295 	 * in the cache.
    296 	 */
    297 	if (VFSTONFS(vp->v_mount)->nm_flag & NFSMNT_NQNFS) {
    298 		nfsstats.rpccnt[NQNFSPROC_ACCESS]++;
    299 		nfsm_reqhead(vp, NQNFSPROC_ACCESS, NFSX_FH + 3 * NFSX_UNSIGNED);
    300 		nfsm_fhtom(vp);
    301 		nfsm_build(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
    302 		if (ap->a_mode & VREAD)
    303 			*tl++ = nfs_true;
    304 		else
    305 			*tl++ = nfs_false;
    306 		if (ap->a_mode & VWRITE)
    307 			*tl++ = nfs_true;
    308 		else
    309 			*tl++ = nfs_false;
    310 		if (ap->a_mode & VEXEC)
    311 			*tl = nfs_true;
    312 		else
    313 			*tl = nfs_false;
    314 		nfsm_request(vp, NQNFSPROC_ACCESS, ap->a_p, ap->a_cred);
    315 		nfsm_reqdone;
    316 		return (error);
    317 	} else
    318 		return (nfsspec_access(ap));
    319 }
    320 
    321 /*
    322  * nfs open vnode op
    323  * Check to see if the type is ok
    324  * and that deletion is not in progress.
    325  * For paged in text files, you will need to flush the page cache
    326  * if consistency is lost.
    327  */
    328 /* ARGSUSED */
    329 int
    330 nfs_open(v)
    331 	void *v;
    332 {
    333 	struct vop_open_args /* {
    334 		struct vnode *a_vp;
    335 		int  a_mode;
    336 		struct ucred *a_cred;
    337 		struct proc *a_p;
    338 	} */ *ap = v;
    339 	register struct vnode *vp = ap->a_vp;
    340 	struct nfsnode *np = VTONFS(vp);
    341 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
    342 	struct vattr vattr;
    343 	int error;
    344 
    345 	if (vp->v_type != VREG && vp->v_type != VDIR && vp->v_type != VLNK)
    346 		return (EACCES);
    347 	if (vp->v_flag & VTEXT) {
    348 	    /*
    349 	     * Get a valid lease. If cached data is stale, flush it.
    350 	     */
    351 	    if (nmp->nm_flag & NFSMNT_NQNFS) {
    352 		if (NQNFS_CKINVALID(vp, np, NQL_READ)) {
    353 		    do {
    354 			error = nqnfs_getlease(vp, NQL_READ, ap->a_cred, ap->a_p);
    355 		    } while (error == NQNFS_EXPIRED);
    356 		    if (error)
    357 			return (error);
    358 		    if (np->n_lrev != np->n_brev ||
    359 			(np->n_flag & NQNFSNONCACHE)) {
    360 			if ((error = nfs_vinvalbuf(vp, V_SAVE, ap->a_cred,
    361 				ap->a_p, 1)) == EINTR)
    362 				return (error);
    363 			(void) vnode_pager_uncache(vp);
    364 			np->n_brev = np->n_lrev;
    365 		    }
    366 		}
    367 	    } else {
    368 		if (np->n_flag & NMODIFIED) {
    369 			if ((error = nfs_vinvalbuf(vp, V_SAVE, ap->a_cred,
    370 				ap->a_p, 1)) == EINTR)
    371 				return (error);
    372 			(void) vnode_pager_uncache(vp);
    373 			np->n_attrstamp = 0;
    374 			np->n_direofoffset = 0;
    375 			error = VOP_GETATTR(vp, &vattr, ap->a_cred, ap->a_p);
    376 			if (error)
    377 				return (error);
    378 			np->n_mtime = vattr.va_mtime.tv_sec;
    379 		} else {
    380 			error = VOP_GETATTR(vp, &vattr, ap->a_cred, ap->a_p);
    381 			if (error)
    382 				return (error);
    383 			if (np->n_mtime != vattr.va_mtime.tv_sec) {
    384 				np->n_direofoffset = 0;
    385 				if ((error = nfs_vinvalbuf(vp, V_SAVE,
    386 					ap->a_cred, ap->a_p, 1)) == EINTR)
    387 					return (error);
    388 				(void) vnode_pager_uncache(vp);
    389 				np->n_mtime = vattr.va_mtime.tv_sec;
    390 			}
    391 		}
    392 	    }
    393 	} else if ((nmp->nm_flag & NFSMNT_NQNFS) == 0)
    394 		np->n_attrstamp = 0; /* For Open/Close consistency */
    395 	return (0);
    396 }
    397 
    398 /*
    399  * nfs close vnode op
    400  * For reg files, invalidate any buffer cache entries.
    401  */
    402 /* ARGSUSED */
    403 int
    404 nfs_close(v)
    405 	void *v;
    406 {
    407 	struct vop_close_args /* {
    408 		struct vnodeop_desc *a_desc;
    409 		struct vnode *a_vp;
    410 		int  a_fflag;
    411 		struct ucred *a_cred;
    412 		struct proc *a_p;
    413 	} */ *ap = v;
    414 	register struct vnode *vp = ap->a_vp;
    415 	register struct nfsnode *np = VTONFS(vp);
    416 	int error = 0;
    417 
    418 	if (vp->v_type == VREG) {
    419 	    if ((VFSTONFS(vp->v_mount)->nm_flag & NFSMNT_NQNFS) == 0 &&
    420 		(np->n_flag & NMODIFIED)) {
    421 		error = nfs_vinvalbuf(vp, V_SAVE, ap->a_cred, ap->a_p, 1);
    422 		np->n_attrstamp = 0;
    423 	    }
    424 	    if (np->n_flag & NWRITEERR) {
    425 		np->n_flag &= ~NWRITEERR;
    426 		error = np->n_error;
    427 	    }
    428 	}
    429 	return (error);
    430 }
    431 
    432 /*
    433  * nfs getattr call from vfs.
    434  */
    435 int
    436 nfs_getattr(v)
    437 	void *v;
    438 {
    439 	struct vop_getattr_args /* {
    440 		struct vnode *a_vp;
    441 		struct vattr *a_vap;
    442 		struct ucred *a_cred;
    443 		struct proc *a_p;
    444 	} */ *ap = v;
    445 	register struct vnode *vp = ap->a_vp;
    446 	register struct nfsnode *np = VTONFS(vp);
    447 	register caddr_t cp;
    448 	caddr_t bpos, dpos;
    449 	int error = 0;
    450 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
    451 
    452 	/*
    453 	 * Update local times for special files.
    454 	 */
    455 	if (np->n_flag & (NACC | NUPD))
    456 		np->n_flag |= NCHG;
    457 	/*
    458 	 * First look in the cache.
    459 	 */
    460 	if (nfs_getattrcache(vp, ap->a_vap) == 0)
    461 		return (0);
    462 	nfsstats.rpccnt[NFSPROC_GETATTR]++;
    463 	nfsm_reqhead(vp, NFSPROC_GETATTR, NFSX_FH);
    464 	nfsm_fhtom(vp);
    465 	nfsm_request(vp, NFSPROC_GETATTR, ap->a_p, ap->a_cred);
    466 	nfsm_loadattr(vp, ap->a_vap);
    467 	nfsm_reqdone;
    468 	return (error);
    469 }
    470 
    471 /*
    472  * nfs setattr call.
    473  */
    474 int
    475 nfs_setattr(v)
    476 	void *v;
    477 {
    478 	struct vop_setattr_args /* {
    479 		struct vnodeop_desc *a_desc;
    480 		struct vnode *a_vp;
    481 		struct vattr *a_vap;
    482 		struct ucred *a_cred;
    483 		struct proc *a_p;
    484 	} */ *ap = v;
    485 	register struct nfsv2_sattr *sp;
    486 	register caddr_t cp;
    487 	register int32_t t1;
    488 	caddr_t bpos, dpos, cp2;
    489 	u_int32_t *tl;
    490 	int error = 0, isnq;
    491 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
    492 	register struct vnode *vp = ap->a_vp;
    493 	register struct nfsnode *np = VTONFS(vp);
    494 	register struct vattr *vap = ap->a_vap;
    495 	u_quad_t frev, tsize = 0;
    496 
    497 	if (vap->va_size != VNOVAL) {
    498 		switch (vp->v_type) {
    499 		case VDIR:
    500 			return (EISDIR);
    501 		case VCHR:
    502 		case VBLK:
    503 			if (vap->va_mtime.tv_sec == VNOVAL &&
    504 			    vap->va_atime.tv_sec == VNOVAL &&
    505 			    vap->va_mode == (u_short)VNOVAL &&
    506 			    vap->va_uid == VNOVAL &&
    507 			    vap->va_gid == VNOVAL)
    508 				return (0);
    509 			vap->va_size = VNOVAL;
    510 			break;
    511 		default:
    512 			if (np->n_flag & NMODIFIED) {
    513 				if (vap->va_size == 0)
    514 					error = nfs_vinvalbuf(vp, 0,
    515 						ap->a_cred, ap->a_p, 1);
    516 				else
    517 					error = nfs_vinvalbuf(vp, V_SAVE,
    518 						ap->a_cred, ap->a_p, 1);
    519 				if (error)
    520 					return (error);
    521 			}
    522 			tsize = np->n_size;
    523 			np->n_size = np->n_vattr.va_size = vap->va_size;
    524 			vnode_pager_setsize(vp, (u_long)np->n_size);
    525 		}
    526 	} else if ((vap->va_mtime.tv_sec != VNOVAL ||
    527 	    vap->va_atime.tv_sec != VNOVAL) && (np->n_flag & NMODIFIED)) {
    528 		error = nfs_vinvalbuf(vp, V_SAVE, ap->a_cred, ap->a_p, 1);
    529 		if (error == EINTR)
    530 			return (error);
    531 	}
    532 	nfsstats.rpccnt[NFSPROC_SETATTR]++;
    533 	isnq = (VFSTONFS(vp->v_mount)->nm_flag & NFSMNT_NQNFS);
    534 	nfsm_reqhead(vp, NFSPROC_SETATTR, NFSX_FH+NFSX_SATTR(isnq));
    535 	nfsm_fhtom(vp);
    536 	nfsm_build(sp, struct nfsv2_sattr *, NFSX_SATTR(isnq));
    537 	if (vap->va_mode == (u_short)VNOVAL)
    538 		sp->sa_mode = nfs_xdrneg1;
    539 	else
    540 		sp->sa_mode = vtonfs_mode(vp->v_type, vap->va_mode);
    541 	if (vap->va_uid == VNOVAL)
    542 		sp->sa_uid = nfs_xdrneg1;
    543 	else
    544 		sp->sa_uid = txdr_unsigned(vap->va_uid);
    545 	if (vap->va_gid == VNOVAL)
    546 		sp->sa_gid = nfs_xdrneg1;
    547 	else
    548 		sp->sa_gid = txdr_unsigned(vap->va_gid);
    549 	if (isnq) {
    550 		txdr_hyper(&vap->va_size, &sp->sa_nqsize);
    551 		txdr_nqtime(&vap->va_atime, &sp->sa_nqatime);
    552 		txdr_nqtime(&vap->va_mtime, &sp->sa_nqmtime);
    553 		sp->sa_nqflags = txdr_unsigned(vap->va_flags);
    554 		sp->sa_nqrdev = nfs_xdrneg1;
    555 	} else {
    556 		sp->sa_nfssize = txdr_unsigned(vap->va_size);
    557 		txdr_nfstime(&vap->va_atime, &sp->sa_nfsatime);
    558 		txdr_nfstime(&vap->va_mtime, &sp->sa_nfsmtime);
    559 	}
    560 	nfsm_request(vp, NFSPROC_SETATTR, ap->a_p, ap->a_cred);
    561 	nfsm_loadattr(vp, (struct vattr *)0);
    562 	if ((VFSTONFS(vp->v_mount)->nm_flag & NFSMNT_NQNFS) &&
    563 	    NQNFS_CKCACHABLE(vp, NQL_WRITE)) {
    564 		nfsm_dissect(tl, u_int32_t *, 2*NFSX_UNSIGNED);
    565 		fxdr_hyper(tl, &frev);
    566 		if (frev > np->n_brev)
    567 			np->n_brev = frev;
    568 	}
    569 	nfsm_reqdone;
    570 	if (error) {
    571 		np->n_size = np->n_vattr.va_size = tsize;
    572 		vnode_pager_setsize(vp, (u_long)np->n_size);
    573 	}
    574 	return (error);
    575 }
    576 
    577 /*
    578  * nfs lookup call, one step at a time...
    579  * First look in cache
    580  * If not found, unlock the directory nfsnode and do the rpc
    581  */
    582 int
    583 nfs_lookup(v)
    584 	void *v;
    585 {
    586 	struct vop_lookup_args /* {
    587 		struct vnodeop_desc *a_desc;
    588 		struct vnode *a_dvp;
    589 		struct vnode **a_vpp;
    590 		struct componentname *a_cnp;
    591 	} */ *ap = v;
    592 	register struct componentname *cnp = ap->a_cnp;
    593 	register struct vnode *dvp = ap->a_dvp;
    594 	register struct vnode **vpp = ap->a_vpp;
    595 	register int flags = cnp->cn_flags;
    596 	register struct vnode *vdp;
    597 	register u_int32_t *tl;
    598 	register caddr_t cp;
    599 	register int32_t t1, t2;
    600 	struct nfsmount *nmp;
    601 	caddr_t bpos, dpos, cp2;
    602 	time_t reqtime = 0;
    603 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
    604 	struct vnode *newvp;
    605 	long len;
    606 	nfsv2fh_t *fhp;
    607 	struct nfsnode *np;
    608 	int lockparent, wantparent, error = 0;
    609 	int nqlflag = 0, cachable = 0;
    610 	u_quad_t frev;
    611 
    612 	*vpp = NULL;
    613 	if (dvp->v_type != VDIR)
    614 		return (ENOTDIR);
    615 	lockparent = flags & LOCKPARENT;
    616 	wantparent = flags & (LOCKPARENT|WANTPARENT);
    617 	nmp = VFSTONFS(dvp->v_mount);
    618 	np = VTONFS(dvp);
    619 	if ((error = cache_lookup(dvp, vpp, cnp)) && error != ENOENT) {
    620 		struct vattr vattr;
    621 		int vpid;
    622 
    623 		vdp = *vpp;
    624 		vpid = vdp->v_id;
    625 		/*
    626 		 * See the comment starting `Step through' in ufs/ufs_lookup.c
    627 		 * for an explanation of the locking protocol
    628 		 */
    629 		if (dvp == vdp) {
    630 			VREF(vdp);
    631 			error = 0;
    632 		} else
    633 			error = vget(vdp, 1);
    634 		if (!error) {
    635 			if (vpid == vdp->v_id) {
    636 			   if (nmp->nm_flag & NFSMNT_NQNFS) {
    637 				if ((nmp->nm_flag & NFSMNT_NQLOOKLEASE) == 0) {
    638 				cachehit:
    639 					nfsstats.lookupcache_hits++;
    640 					if (cnp->cn_nameiop != LOOKUP &&
    641 					    (flags & ISLASTCN))
    642 						cnp->cn_flags |= SAVENAME;
    643 					return (0);
    644 			        } else if (NQNFS_CKCACHABLE(dvp, NQL_READ)) {
    645 					if (np->n_lrev != np->n_brev ||
    646 					    (np->n_flag & NMODIFIED)) {
    647 						np->n_direofoffset = 0;
    648 						cache_purge(dvp);
    649 						error = nfs_vinvalbuf(dvp, 0,
    650 						    cnp->cn_cred, cnp->cn_proc,
    651 						    1);
    652 						if (error == EINTR)
    653 							return (error);
    654 						np->n_brev = np->n_lrev;
    655 					} else
    656 						goto cachehit;
    657 				}
    658 			   } else if (!VOP_GETATTR(vdp, &vattr, cnp->cn_cred, cnp->cn_proc) &&
    659 			       vattr.va_ctime.tv_sec == VTONFS(vdp)->n_ctime)
    660 				goto cachehit;
    661 			   cache_purge(vdp);
    662 			}
    663 			vrele(vdp);
    664 		}
    665 		*vpp = NULLVP;
    666 	}
    667 	error = 0;
    668 	nfsstats.lookupcache_misses++;
    669 	nfsstats.rpccnt[NFSPROC_LOOKUP]++;
    670 	len = cnp->cn_namelen;
    671 	nfsm_reqhead(dvp, NFSPROC_LOOKUP, NFSX_FH+NFSX_UNSIGNED+nfsm_rndup(len));
    672 
    673 	/*
    674 	 * For nqnfs optionally piggyback a getlease request for the name
    675 	 * being looked up.
    676 	 */
    677 	if (nmp->nm_flag & NFSMNT_NQNFS) {
    678 		nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
    679 		if ((nmp->nm_flag & NFSMNT_NQLOOKLEASE) &&
    680 		    ((cnp->cn_flags & MAKEENTRY) &&
    681 		    (cnp->cn_nameiop != DELETE || !(flags & ISLASTCN))))
    682 			*tl = txdr_unsigned(nmp->nm_leaseterm);
    683 		else
    684 			*tl = 0;
    685 	}
    686 	nfsm_fhtom(dvp);
    687 	nfsm_strtom(cnp->cn_nameptr, len, NFS_MAXNAMLEN);
    688 	reqtime = time.tv_sec;
    689 	nfsm_request(dvp, NFSPROC_LOOKUP, cnp->cn_proc, cnp->cn_cred);
    690 nfsmout:
    691 	if (error) {
    692 		if ((cnp->cn_nameiop == CREATE || cnp->cn_nameiop == RENAME) &&
    693 		    (flags & ISLASTCN) && error == ENOENT)
    694 			error = EJUSTRETURN;
    695 		if (cnp->cn_nameiop != LOOKUP && (flags & ISLASTCN))
    696 			cnp->cn_flags |= SAVENAME;
    697 		return (error);
    698 	}
    699 	if (nmp->nm_flag & NFSMNT_NQNFS) {
    700 		nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
    701 		if (*tl) {
    702 			nqlflag = fxdr_unsigned(int, *tl);
    703 			nfsm_dissect(tl, u_int32_t *, 4*NFSX_UNSIGNED);
    704 			cachable = fxdr_unsigned(int, *tl++);
    705 			reqtime += fxdr_unsigned(int, *tl++);
    706 			fxdr_hyper(tl, &frev);
    707 		} else
    708 			nqlflag = 0;
    709 	}
    710 	nfsm_dissect(fhp, nfsv2fh_t *, NFSX_FH);
    711 
    712 	/*
    713 	 * Handle RENAME case...
    714 	 */
    715 	if (cnp->cn_nameiop == RENAME && wantparent && (flags & ISLASTCN)) {
    716 		if (!bcmp(np->n_fh.fh_bytes, (caddr_t)fhp, NFSX_FH)) {
    717 			m_freem(mrep);
    718 			return (EISDIR);
    719 		}
    720 		if ((error = nfs_nget(dvp->v_mount, fhp, &np)) != 0) {
    721 			m_freem(mrep);
    722 			return (error);
    723 		}
    724 		newvp = NFSTOV(np);
    725 		error = nfs_loadattrcache(&newvp, &md, &dpos, NULL);
    726 		if (error) {
    727 			vrele(newvp);
    728 			m_freem(mrep);
    729 			return (error);
    730 		}
    731 		*vpp = newvp;
    732 		m_freem(mrep);
    733 		cnp->cn_flags |= SAVENAME;
    734 		return (0);
    735 	}
    736 
    737 	if (!bcmp(np->n_fh.fh_bytes, (caddr_t)fhp, NFSX_FH)) {
    738 		VREF(dvp);
    739 		newvp = dvp;
    740 	} else {
    741 		if ((error = nfs_nget(dvp->v_mount, fhp, &np)) != 0) {
    742 			m_freem(mrep);
    743 			return (error);
    744 		}
    745 		newvp = NFSTOV(np);
    746 	}
    747 	error = nfs_loadattrcache(&newvp, &md, &dpos, (struct vattr *)0);
    748 	if (error) {
    749 		vrele(newvp);
    750 		m_freem(mrep);
    751 		return (error);
    752 	}
    753 	m_freem(mrep);
    754 	*vpp = newvp;
    755 	if (cnp->cn_nameiop != LOOKUP && (flags & ISLASTCN))
    756 		cnp->cn_flags |= SAVENAME;
    757 	if ((cnp->cn_flags & MAKEENTRY) &&
    758 	    (cnp->cn_nameiop != DELETE || !(flags & ISLASTCN))) {
    759 		if ((nmp->nm_flag & NFSMNT_NQNFS) == 0)
    760 			np->n_ctime = np->n_vattr.va_ctime.tv_sec;
    761 		else if (nqlflag && reqtime > time.tv_sec)
    762 			nqnfs_clientlease(nmp, np, nqlflag, cachable, reqtime,
    763 				frev);
    764 		cache_enter(dvp, *vpp, cnp);
    765 	}
    766 	return (0);
    767 }
    768 
    769 /*
    770  * nfs read call.
    771  * Just call nfs_bioread() to do the work.
    772  */
    773 int
    774 nfs_read(v)
    775 	void *v;
    776 {
    777 	struct vop_read_args /* {
    778 		struct vnode *a_vp;
    779 		struct uio *a_uio;
    780 		int  a_ioflag;
    781 		struct ucred *a_cred;
    782 	} */ *ap = v;
    783 	register struct vnode *vp = ap->a_vp;
    784 
    785 	if (vp->v_type != VREG)
    786 		return (EPERM);
    787 	return (nfs_bioread(vp, ap->a_uio, ap->a_ioflag, ap->a_cred));
    788 }
    789 
    790 /*
    791  * nfs readlink call
    792  */
    793 int
    794 nfs_readlink(v)
    795 	void *v;
    796 {
    797 	struct vop_readlink_args /* {
    798 		struct vnode *a_vp;
    799 		struct uio *a_uio;
    800 		struct ucred *a_cred;
    801 	} */ *ap = v;
    802 	register struct vnode *vp = ap->a_vp;
    803 
    804 	if (vp->v_type != VLNK)
    805 		return (EPERM);
    806 	return (nfs_bioread(vp, ap->a_uio, 0, ap->a_cred));
    807 }
    808 
    809 /*
    810  * Do a readlink rpc.
    811  * Called by nfs_doio() from below the buffer cache.
    812  */
    813 int
    814 nfs_readlinkrpc(vp, uiop, cred)
    815 	register struct vnode *vp;
    816 	struct uio *uiop;
    817 	struct ucred *cred;
    818 {
    819 	register u_int32_t *tl;
    820 	register caddr_t cp;
    821 	register int32_t t1;
    822 	caddr_t bpos, dpos, cp2;
    823 	int error = 0;
    824 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
    825 	long len;
    826 
    827 	nfsstats.rpccnt[NFSPROC_READLINK]++;
    828 	nfsm_reqhead(vp, NFSPROC_READLINK, NFSX_FH);
    829 	nfsm_fhtom(vp);
    830 	nfsm_request(vp, NFSPROC_READLINK, uiop->uio_procp, cred);
    831 	nfsm_strsiz(len, NFS_MAXPATHLEN);
    832 	nfsm_mtouio(uiop, len);
    833 	nfsm_reqdone;
    834 	return (error);
    835 }
    836 
    837 /*
    838  * nfs read rpc call
    839  * Ditto above
    840  */
    841 int
    842 nfs_readrpc(vp, uiop, cred)
    843 	register struct vnode *vp;
    844 	struct uio *uiop;
    845 	struct ucred *cred;
    846 {
    847 	register u_int32_t *tl;
    848 	register caddr_t cp;
    849 	register int32_t t1;
    850 	caddr_t bpos, dpos, cp2;
    851 	int error = 0;
    852 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
    853 	struct nfsmount *nmp;
    854 	long len, retlen, tsiz;
    855 
    856 	nmp = VFSTONFS(vp->v_mount);
    857 	tsiz = uiop->uio_resid;
    858 	if (uiop->uio_offset + tsiz > 0xffffffff &&
    859 	    (nmp->nm_flag & NFSMNT_NQNFS) == 0)
    860 		return (EFBIG);
    861 	while (tsiz > 0) {
    862 		nfsstats.rpccnt[NFSPROC_READ]++;
    863 		len = (tsiz > nmp->nm_rsize) ? nmp->nm_rsize : tsiz;
    864 		nfsm_reqhead(vp, NFSPROC_READ, NFSX_FH+NFSX_UNSIGNED*3);
    865 		nfsm_fhtom(vp);
    866 		nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED*3);
    867 		if (nmp->nm_flag & NFSMNT_NQNFS) {
    868 			txdr_hyper(&uiop->uio_offset, tl);
    869 			*(tl + 2) = txdr_unsigned(len);
    870 		} else {
    871 			*tl++ = txdr_unsigned(uiop->uio_offset);
    872 			*tl++ = txdr_unsigned(len);
    873 			*tl = 0;
    874 		}
    875 		nfsm_request(vp, NFSPROC_READ, uiop->uio_procp, cred);
    876 		nfsm_loadattr(vp, (struct vattr *)0);
    877 		nfsm_strsiz(retlen, nmp->nm_rsize);
    878 		nfsm_mtouio(uiop, retlen);
    879 		m_freem(mrep);
    880 		if (retlen < len)
    881 			tsiz = 0;
    882 		else
    883 			tsiz -= len;
    884 	}
    885 nfsmout:
    886 	return (error);
    887 }
    888 
    889 /*
    890  * nfs write call
    891  */
    892 int
    893 nfs_writerpc(vp, uiop, cred, ioflags)
    894 	register struct vnode *vp;
    895 	struct uio *uiop;
    896 	struct ucred *cred;
    897 	int ioflags;
    898 {
    899 	register u_int32_t *tl;
    900 	register caddr_t cp;
    901 	register int32_t t1;
    902 	caddr_t bpos, dpos, cp2;
    903 	int error = 0;
    904 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
    905 	struct nfsmount *nmp;
    906 	struct nfsnode *np = VTONFS(vp);
    907 	u_quad_t frev;
    908 	long len, tsiz;
    909 
    910 	nmp = VFSTONFS(vp->v_mount);
    911 	tsiz = uiop->uio_resid;
    912 	if (uiop->uio_offset + tsiz > 0xffffffff &&
    913 	    (nmp->nm_flag & NFSMNT_NQNFS) == 0)
    914 		return (EFBIG);
    915 	while (tsiz > 0) {
    916 		nfsstats.rpccnt[NFSPROC_WRITE]++;
    917 		len = (tsiz > nmp->nm_wsize) ? nmp->nm_wsize : tsiz;
    918 		nfsm_reqhead(vp, NFSPROC_WRITE,
    919 			NFSX_FH+NFSX_UNSIGNED*4+nfsm_rndup(len));
    920 		nfsm_fhtom(vp);
    921 		nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED * 4);
    922 		if (nmp->nm_flag & NFSMNT_NQNFS) {
    923 			txdr_hyper(&uiop->uio_offset, tl);
    924 			tl += 2;
    925 			*tl++ = 0;
    926 			*tl = txdr_unsigned(len);
    927 		} else {
    928 			register u_int32_t x;
    929 			/* Set both "begin" and "current" to non-garbage. */
    930 			x = txdr_unsigned((u_int32_t)uiop->uio_offset);
    931 			*tl++ = x;	/* "begin offset" */
    932 			*tl++ = x;	/* "current offset" */
    933 			x = txdr_unsigned(len);
    934 			*tl++ = x;	/* total to this offset */
    935 			*tl = x;	/* size of this write */
    936 		}
    937 		nfsm_uiotom(uiop, len);
    938 		nfsm_request(vp, NFSPROC_WRITE, uiop->uio_procp, cred);
    939 		nfsm_loadattr(vp, (struct vattr *)0);
    940 		if (nmp->nm_flag & NFSMNT_MYWRITE)
    941 			VTONFS(vp)->n_mtime = VTONFS(vp)->n_vattr.va_mtime.tv_sec;
    942 		else if ((nmp->nm_flag & NFSMNT_NQNFS) &&
    943 			 NQNFS_CKCACHABLE(vp, NQL_WRITE)) {
    944 			nfsm_dissect(tl, u_int32_t *, 2*NFSX_UNSIGNED);
    945 			fxdr_hyper(tl, &frev);
    946 			if (frev > np->n_brev)
    947 				np->n_brev = frev;
    948 		}
    949 		m_freem(mrep);
    950 		tsiz -= len;
    951 	}
    952 nfsmout:
    953 	if (error)
    954 		uiop->uio_resid = tsiz;
    955 	return (error);
    956 }
    957 
    958 /*
    959  * nfs mknod call
    960  * This is a kludge. Use a create rpc but with the IFMT bits of the mode
    961  * set to specify the file type and the size field for rdev.
    962  */
    963 /* ARGSUSED */
    964 int
    965 nfs_mknod(v)
    966 	void *v;
    967 {
    968 	struct vop_mknod_args /* {
    969 		struct vnode *a_dvp;
    970 		struct vnode **a_vpp;
    971 		struct componentname *a_cnp;
    972 		struct vattr *a_vap;
    973 	} */ *ap = v;
    974 	register struct vnode *dvp = ap->a_dvp;
    975 	register struct vattr *vap = ap->a_vap;
    976 	register struct componentname *cnp = ap->a_cnp;
    977 	register struct nfsv2_sattr *sp;
    978 	register u_int32_t *tl;
    979 	register caddr_t cp;
    980 	register int32_t t1, t2;
    981 	struct vnode *newvp;
    982 	char *cp2;
    983 	caddr_t bpos, dpos;
    984 	int error = 0, isnq;
    985 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
    986 	u_int32_t rdev;
    987 
    988 	if (vap->va_type == VCHR || vap->va_type == VBLK)
    989 		rdev = txdr_unsigned(vap->va_rdev);
    990 #ifdef FIFO
    991 	else if (vap->va_type == VFIFO)
    992 		rdev = 0xffffffff;
    993 #endif /* FIFO */
    994 	else {
    995 		VOP_ABORTOP(dvp, cnp);
    996 		vput(dvp);
    997 		return (EOPNOTSUPP);
    998 	}
    999 	newvp = NULLVP;
   1000 	nfsstats.rpccnt[NFSPROC_CREATE]++;
   1001 	isnq = (VFSTONFS(dvp->v_mount)->nm_flag & NFSMNT_NQNFS);
   1002 	nfsm_reqhead(dvp, NFSPROC_CREATE,
   1003 	  NFSX_FH+NFSX_UNSIGNED+nfsm_rndup(cnp->cn_namelen)+NFSX_SATTR(isnq));
   1004 	nfsm_fhtom(dvp);
   1005 	nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
   1006 	nfsm_build(sp, struct nfsv2_sattr *, NFSX_SATTR(isnq));
   1007 	sp->sa_mode = vtonfs_mode(vap->va_type, vap->va_mode);
   1008 	sp->sa_uid = nfs_xdrneg1;
   1009 	sp->sa_gid = nfs_xdrneg1;
   1010 	if (isnq) {
   1011 		sp->sa_nqrdev = rdev;
   1012 		sp->sa_nqflags = 0;
   1013 		txdr_nqtime(&vap->va_atime, &sp->sa_nqatime);
   1014 		txdr_nqtime(&vap->va_mtime, &sp->sa_nqmtime);
   1015 	} else {
   1016 		sp->sa_nfssize = rdev;
   1017 		txdr_nfstime(&vap->va_atime, &sp->sa_nfsatime);
   1018 		txdr_nfstime(&vap->va_mtime, &sp->sa_nfsmtime);
   1019 	}
   1020 	nfsm_request(dvp, NFSPROC_CREATE, cnp->cn_proc, cnp->cn_cred);
   1021 	nfsm_mtofh(dvp, newvp);
   1022 	nfsm_reqdone;
   1023 	if (!error && (cnp->cn_flags & MAKEENTRY))
   1024 		cache_enter(dvp, newvp, cnp);
   1025 	FREE(cnp->cn_pnbuf, M_NAMEI);
   1026 	VTONFS(dvp)->n_flag |= NMODIFIED;
   1027 	VTONFS(dvp)->n_attrstamp = 0;
   1028 	vrele(dvp);
   1029 	if (newvp != NULLVP)
   1030 		vrele(newvp);
   1031 	return (error);
   1032 }
   1033 
   1034 /*
   1035  * nfs file create call
   1036  */
   1037 int
   1038 nfs_create(v)
   1039 	void *v;
   1040 {
   1041 	struct vop_create_args /* {
   1042 		struct vnode *a_dvp;
   1043 		struct vnode **a_vpp;
   1044 		struct componentname *a_cnp;
   1045 		struct vattr *a_vap;
   1046 	} */ *ap = v;
   1047 	register struct vnode *dvp = ap->a_dvp;
   1048 	register struct vattr *vap = ap->a_vap;
   1049 	register struct componentname *cnp = ap->a_cnp;
   1050 	register struct nfsv2_sattr *sp;
   1051 	register u_int32_t *tl;
   1052 	register caddr_t cp;
   1053 	register int32_t t1, t2;
   1054 	caddr_t bpos, dpos, cp2;
   1055 	int error = 0, isnq;
   1056 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
   1057 
   1058 	nfsstats.rpccnt[NFSPROC_CREATE]++;
   1059 	isnq = (VFSTONFS(dvp->v_mount)->nm_flag & NFSMNT_NQNFS);
   1060 	nfsm_reqhead(dvp, NFSPROC_CREATE,
   1061 	  NFSX_FH+NFSX_UNSIGNED+nfsm_rndup(cnp->cn_namelen)+NFSX_SATTR(isnq));
   1062 	nfsm_fhtom(dvp);
   1063 	nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
   1064 	nfsm_build(sp, struct nfsv2_sattr *, NFSX_SATTR(isnq));
   1065 	sp->sa_mode = vtonfs_mode(vap->va_type, vap->va_mode);
   1066 	sp->sa_uid = nfs_xdrneg1;
   1067 	sp->sa_gid = nfs_xdrneg1;
   1068 	if (isnq) {
   1069 		u_quad_t qval = 0;
   1070 
   1071 		txdr_hyper(&qval, &sp->sa_nqsize);
   1072 		sp->sa_nqrdev = nfs_xdrneg1;
   1073 		sp->sa_nqflags = 0;
   1074 		txdr_nqtime(&vap->va_atime, &sp->sa_nqatime);
   1075 		txdr_nqtime(&vap->va_mtime, &sp->sa_nqmtime);
   1076 	} else {
   1077 		sp->sa_nfssize = 0;
   1078 		txdr_nfstime(&vap->va_atime, &sp->sa_nfsatime);
   1079 		txdr_nfstime(&vap->va_mtime, &sp->sa_nfsmtime);
   1080 	}
   1081 	nfsm_request(dvp, NFSPROC_CREATE, cnp->cn_proc, cnp->cn_cred);
   1082 	nfsm_mtofh(dvp, *ap->a_vpp);
   1083 	nfsm_reqdone;
   1084 	if (!error && (cnp->cn_flags & MAKEENTRY))
   1085 		cache_enter(dvp, *ap->a_vpp, cnp);
   1086 	FREE(cnp->cn_pnbuf, M_NAMEI);
   1087 	VTONFS(dvp)->n_flag |= NMODIFIED;
   1088 	VTONFS(dvp)->n_attrstamp = 0;
   1089 	vrele(dvp);
   1090 	return (error);
   1091 }
   1092 
   1093 /*
   1094  * nfs file remove call
   1095  * To try and make nfs semantics closer to ufs semantics, a file that has
   1096  * other processes using the vnode is renamed instead of removed and then
   1097  * removed later on the last close.
   1098  * - If v_usecount > 1
   1099  *	  If a rename is not already in the works
   1100  *	     call nfs_sillyrename() to set it up
   1101  *     else
   1102  *	  do the remove rpc
   1103  */
   1104 int
   1105 nfs_remove(v)
   1106 	void *v;
   1107 {
   1108 	struct vop_remove_args /* {
   1109 		struct vnodeop_desc *a_desc;
   1110 		struct vnode * a_dvp;
   1111 		struct vnode * a_vp;
   1112 		struct componentname * a_cnp;
   1113 	} */ *ap = v;
   1114 	register struct vnode *vp = ap->a_vp;
   1115 	register struct vnode *dvp = ap->a_dvp;
   1116 	register struct componentname *cnp = ap->a_cnp;
   1117 	register struct nfsnode *np = VTONFS(vp);
   1118 	register u_int32_t *tl;
   1119 	register caddr_t cp;
   1120 	register int32_t t2;
   1121 	caddr_t bpos, dpos;
   1122 	int error = 0;
   1123 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
   1124 	struct vattr vattr;
   1125 
   1126 	if (vp->v_usecount > 1) {
   1127 		if (!np->n_sillyrename)
   1128 			error = nfs_sillyrename(dvp, vp, cnp);
   1129 		else if (VOP_GETATTR(vp, &vattr, cnp->cn_cred, cnp->cn_proc)
   1130 			 == 0 && vattr.va_nlink > 1)
   1131 			/*
   1132 			 * If we already have a silly name but there are more
   1133 			 * than one links, just proceed with the NFS remove
   1134 			 * request, as the bits will remain available (modulo
   1135 			 * network races). This avoids silently ignoring the
   1136 			 * attempted removal of a non-silly entry.
   1137 			 */
   1138 			goto doit;
   1139 	} else {
   1140 	doit:
   1141 		/*
   1142 		 * Purge the name cache so that the chance of a lookup for
   1143 		 * the name succeeding while the remove is in progress is
   1144 		 * minimized. Without node locking it can still happen, such
   1145 		 * that an I/O op returns ESTALE, but since you get this if
   1146 		 * another host removes the file..
   1147 		 */
   1148 		cache_purge(vp);
   1149 		/*
   1150 		 * Throw away biocache buffers. Mainly to avoid
   1151 		 * unnecessary delayed writes.
   1152 		 */
   1153 		error = nfs_vinvalbuf(vp, 0, cnp->cn_cred, cnp->cn_proc, 1);
   1154 		if (error == EINTR)
   1155 			return (error);
   1156 		/* Do the rpc */
   1157 		nfsstats.rpccnt[NFSPROC_REMOVE]++;
   1158 		nfsm_reqhead(dvp, NFSPROC_REMOVE,
   1159 			NFSX_FH+NFSX_UNSIGNED+nfsm_rndup(cnp->cn_namelen));
   1160 		nfsm_fhtom(dvp);
   1161 		nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
   1162 		nfsm_request(dvp, NFSPROC_REMOVE, cnp->cn_proc, cnp->cn_cred);
   1163 		nfsm_reqdone;
   1164 		FREE(cnp->cn_pnbuf, M_NAMEI);
   1165 		VTONFS(dvp)->n_flag |= NMODIFIED;
   1166 		VTONFS(dvp)->n_attrstamp = 0;
   1167 		/*
   1168 		 * Kludge City: If the first reply to the remove rpc is lost..
   1169 		 *   the reply to the retransmitted request will be ENOENT
   1170 		 *   since the file was in fact removed
   1171 		 *   Therefore, we cheat and return success.
   1172 		 */
   1173 		if (error == ENOENT)
   1174 			error = 0;
   1175 	}
   1176 	np->n_attrstamp = 0;
   1177 	vrele(dvp);
   1178 	vrele(vp);
   1179 	return (error);
   1180 }
   1181 
   1182 /*
   1183  * nfs file remove rpc called from nfs_inactive
   1184  */
   1185 int
   1186 nfs_removeit(sp)
   1187 	register struct sillyrename *sp;
   1188 {
   1189 	register u_int32_t *tl;
   1190 	register caddr_t cp;
   1191 	register int32_t t2;
   1192 	caddr_t bpos, dpos;
   1193 	int error = 0;
   1194 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
   1195 
   1196 	nfsstats.rpccnt[NFSPROC_REMOVE]++;
   1197 	nfsm_reqhead(sp->s_dvp, NFSPROC_REMOVE,
   1198 		NFSX_FH+NFSX_UNSIGNED+nfsm_rndup(sp->s_namlen));
   1199 	nfsm_fhtom(sp->s_dvp);
   1200 	nfsm_strtom(sp->s_name, sp->s_namlen, NFS_MAXNAMLEN);
   1201 	nfsm_request(sp->s_dvp, NFSPROC_REMOVE, NULL, sp->s_cred);
   1202 	nfsm_reqdone;
   1203 	VTONFS(sp->s_dvp)->n_flag |= NMODIFIED;
   1204 	VTONFS(sp->s_dvp)->n_attrstamp = 0;
   1205 	return (error);
   1206 }
   1207 
   1208 /*
   1209  * nfs file rename call
   1210  */
   1211 int
   1212 nfs_rename(v)
   1213 	void *v;
   1214 {
   1215 	struct vop_rename_args  /* {
   1216 		struct vnode *a_fdvp;
   1217 		struct vnode *a_fvp;
   1218 		struct componentname *a_fcnp;
   1219 		struct vnode *a_tdvp;
   1220 		struct vnode *a_tvp;
   1221 		struct componentname *a_tcnp;
   1222 	} */ *ap = v;
   1223 	register struct vnode *fvp = ap->a_fvp;
   1224 	register struct vnode *tvp = ap->a_tvp;
   1225 	register struct vnode *fdvp = ap->a_fdvp;
   1226 	register struct vnode *tdvp = ap->a_tdvp;
   1227 	register struct componentname *tcnp = ap->a_tcnp;
   1228 	register struct componentname *fcnp = ap->a_fcnp;
   1229 	register u_int32_t *tl;
   1230 	register caddr_t cp;
   1231 	register int32_t t2;
   1232 	caddr_t bpos, dpos;
   1233 	int error = 0;
   1234 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
   1235 
   1236 	/* Check for cross-device rename */
   1237 	if ((fvp->v_mount != tdvp->v_mount) ||
   1238 	    (tvp && (fvp->v_mount != tvp->v_mount))) {
   1239 		error = EXDEV;
   1240 		goto out;
   1241 	}
   1242 
   1243 
   1244 	nfsstats.rpccnt[NFSPROC_RENAME]++;
   1245 	nfsm_reqhead(fdvp, NFSPROC_RENAME,
   1246 		(NFSX_FH+NFSX_UNSIGNED)*2+nfsm_rndup(fcnp->cn_namelen)+
   1247 		nfsm_rndup(fcnp->cn_namelen)); /* or fcnp->cn_cred?*/
   1248 	nfsm_fhtom(fdvp);
   1249 	nfsm_strtom(fcnp->cn_nameptr, fcnp->cn_namelen, NFS_MAXNAMLEN);
   1250 	nfsm_fhtom(tdvp);
   1251 	nfsm_strtom(tcnp->cn_nameptr, tcnp->cn_namelen, NFS_MAXNAMLEN);
   1252 	nfsm_request(fdvp, NFSPROC_RENAME, tcnp->cn_proc, tcnp->cn_cred);
   1253 	nfsm_reqdone;
   1254 	VTONFS(fdvp)->n_flag |= NMODIFIED;
   1255 	VTONFS(fdvp)->n_attrstamp = 0;
   1256 	VTONFS(tdvp)->n_flag |= NMODIFIED;
   1257 	VTONFS(tdvp)->n_attrstamp = 0;
   1258 	if (fvp->v_type == VDIR) {
   1259 		if (tvp != NULL && tvp->v_type == VDIR)
   1260 			cache_purge(tdvp);
   1261 		cache_purge(fdvp);
   1262 	}
   1263 out:
   1264 	if (tdvp == tvp)
   1265 		vrele(tdvp);
   1266 	else
   1267 		vput(tdvp);
   1268 	if (tvp)
   1269 		vput(tvp);
   1270 	vrele(fdvp);
   1271 	vrele(fvp);
   1272 	/*
   1273 	 * Kludge: Map ENOENT => 0 assuming that it is a reply to a retry.
   1274 	 */
   1275 	if (error == ENOENT)
   1276 		error = 0;
   1277 	return (error);
   1278 }
   1279 
   1280 /*
   1281  * nfs file rename rpc called from nfs_remove() above
   1282  */
   1283 int
   1284 nfs_renameit(sdvp, scnp, sp)
   1285 	struct vnode *sdvp;
   1286 	struct componentname *scnp;
   1287 	register struct sillyrename *sp;
   1288 {
   1289 	register u_int32_t *tl;
   1290 	register caddr_t cp;
   1291 	register int32_t t2;
   1292 	caddr_t bpos, dpos;
   1293 	int error = 0;
   1294 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
   1295 
   1296 	nfsstats.rpccnt[NFSPROC_RENAME]++;
   1297 	nfsm_reqhead(sdvp, NFSPROC_RENAME,
   1298 		(NFSX_FH+NFSX_UNSIGNED)*2+nfsm_rndup(scnp->cn_namelen)+
   1299 		nfsm_rndup(sp->s_namlen));
   1300 	nfsm_fhtom(sdvp);
   1301 	nfsm_strtom(scnp->cn_nameptr, scnp->cn_namelen, NFS_MAXNAMLEN);
   1302 	nfsm_fhtom(sdvp);
   1303 	nfsm_strtom(sp->s_name, sp->s_namlen, NFS_MAXNAMLEN);
   1304 	nfsm_request(sdvp, NFSPROC_RENAME, scnp->cn_proc, scnp->cn_cred);
   1305 	nfsm_reqdone;
   1306 	FREE(scnp->cn_pnbuf, M_NAMEI);
   1307 	VTONFS(sdvp)->n_flag |= NMODIFIED;
   1308 	VTONFS(sdvp)->n_attrstamp = 0;
   1309 	return (error);
   1310 }
   1311 
   1312 /*
   1313  * nfs hard link create call
   1314  */
   1315 int
   1316 nfs_link(v)
   1317 	void *v;
   1318 {
   1319 	struct vop_link_args /* {
   1320 		struct vnode *a_dvp;
   1321 		struct vnode *a_vp;
   1322 		struct componentname *a_cnp;
   1323 	} */ *ap = v;
   1324 	register struct vnode *dvp = ap->a_dvp;
   1325 	register struct vnode *vp = ap->a_vp;
   1326 	register struct componentname *cnp = ap->a_cnp;
   1327 	register u_int32_t *tl;
   1328 	register caddr_t cp;
   1329 	register int32_t t2;
   1330 	caddr_t bpos, dpos;
   1331 	int error = 0;
   1332 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
   1333 
   1334 	if (dvp->v_mount != vp->v_mount) {
   1335 		/*VOP_ABORTOP(dvp, cnp);*/
   1336 		if (vp == dvp)
   1337 			vrele(dvp);
   1338 		else
   1339 			vput(dvp);
   1340 		return (EXDEV);
   1341 	}
   1342 
   1343 	/*
   1344 	 * Push all writes to the server, so that the attribute cache
   1345 	 * doesn't get "out of sync" with the server.
   1346 	 * XXX There should be a better way!
   1347 	 */
   1348 	VOP_FSYNC(vp, cnp->cn_cred, MNT_WAIT, cnp->cn_proc);
   1349 
   1350 	nfsstats.rpccnt[NFSPROC_LINK]++;
   1351 	nfsm_reqhead(vp, NFSPROC_LINK,
   1352 		NFSX_FH*2+NFSX_UNSIGNED+nfsm_rndup(cnp->cn_namelen));
   1353 	nfsm_fhtom(vp);
   1354 	nfsm_fhtom(dvp);
   1355 	nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
   1356 	nfsm_request(vp, NFSPROC_LINK, cnp->cn_proc, cnp->cn_cred);
   1357 	nfsm_reqdone;
   1358 	FREE(cnp->cn_pnbuf, M_NAMEI);
   1359 	VTONFS(vp)->n_attrstamp = 0;
   1360 	VTONFS(dvp)->n_flag |= NMODIFIED;
   1361 	VTONFS(dvp)->n_attrstamp = 0;
   1362 	vrele(dvp);
   1363 	/*
   1364 	 * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry.
   1365 	 */
   1366 	if (error == EEXIST)
   1367 		error = 0;
   1368 	return (error);
   1369 }
   1370 
   1371 /*
   1372  * nfs symbolic link create call
   1373  */
   1374 /* start here */
   1375 int
   1376 nfs_symlink(v)
   1377 	void *v;
   1378 {
   1379 	struct vop_symlink_args /* {
   1380 		struct vnode *a_dvp;
   1381 		struct vnode **a_vpp;
   1382 		struct componentname *a_cnp;
   1383 		struct vattr *a_vap;
   1384 		char *a_target;
   1385 	} */ *ap = v;
   1386 	register struct vnode *dvp = ap->a_dvp;
   1387 	register struct vattr *vap = ap->a_vap;
   1388 	register struct componentname *cnp = ap->a_cnp;
   1389 	register struct nfsv2_sattr *sp;
   1390 	register u_int32_t *tl;
   1391 	register caddr_t cp;
   1392 	register int32_t t2;
   1393 	caddr_t bpos, dpos;
   1394 	int slen, error = 0, isnq;
   1395 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
   1396 
   1397 	nfsstats.rpccnt[NFSPROC_SYMLINK]++;
   1398 	slen = strlen(ap->a_target);
   1399 	isnq = (VFSTONFS(dvp->v_mount)->nm_flag & NFSMNT_NQNFS);
   1400 	nfsm_reqhead(dvp, NFSPROC_SYMLINK, NFSX_FH+2*NFSX_UNSIGNED+
   1401 	    nfsm_rndup(cnp->cn_namelen)+nfsm_rndup(slen)+NFSX_SATTR(isnq));
   1402 	nfsm_fhtom(dvp);
   1403 	nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
   1404 	nfsm_strtom(ap->a_target, slen, NFS_MAXPATHLEN);
   1405 	nfsm_build(sp, struct nfsv2_sattr *, NFSX_SATTR(isnq));
   1406 	sp->sa_mode = vtonfs_mode(VLNK, vap->va_mode);
   1407 	sp->sa_uid = nfs_xdrneg1;
   1408 	sp->sa_gid = nfs_xdrneg1;
   1409 	if (isnq) {
   1410 		quad_t qval = -1;
   1411 
   1412 		txdr_hyper(&qval, &sp->sa_nqsize);
   1413 		sp->sa_nqflags = 0;
   1414 		txdr_nqtime(&vap->va_atime, &sp->sa_nqatime);
   1415 		txdr_nqtime(&vap->va_mtime, &sp->sa_nqmtime);
   1416 	} else {
   1417 		sp->sa_nfssize = nfs_xdrneg1;
   1418 		txdr_nfstime(&vap->va_atime, &sp->sa_nfsatime);
   1419 		txdr_nfstime(&vap->va_mtime, &sp->sa_nfsmtime);
   1420 	}
   1421 	nfsm_request(dvp, NFSPROC_SYMLINK, cnp->cn_proc, cnp->cn_cred);
   1422 	nfsm_reqdone;
   1423 	FREE(cnp->cn_pnbuf, M_NAMEI);
   1424 	VTONFS(dvp)->n_flag |= NMODIFIED;
   1425 	VTONFS(dvp)->n_attrstamp = 0;
   1426 	vrele(dvp);
   1427 	/*
   1428 	 * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry.
   1429 	 */
   1430 	if (error == EEXIST)
   1431 		error = 0;
   1432 	return (error);
   1433 }
   1434 
   1435 /*
   1436  * nfs make dir call
   1437  */
   1438 int
   1439 nfs_mkdir(v)
   1440 	void *v;
   1441 {
   1442 	struct vop_mkdir_args /* {
   1443 		struct vnode *a_dvp;
   1444 		struct vnode **a_vpp;
   1445 		struct componentname *a_cnp;
   1446 		struct vattr *a_vap;
   1447 	} */ *ap = v;
   1448 	register struct vnode *dvp = ap->a_dvp;
   1449 	register struct vattr *vap = ap->a_vap;
   1450 	register struct componentname *cnp = ap->a_cnp;
   1451 	register struct vnode **vpp = ap->a_vpp;
   1452 	register struct nfsv2_sattr *sp;
   1453 	register u_int32_t *tl;
   1454 	register caddr_t cp;
   1455 	register int32_t t1, t2;
   1456 	register int len;
   1457 	caddr_t bpos, dpos, cp2;
   1458 	int error = 0, firsttry = 1, isnq;
   1459 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
   1460 
   1461 	len = cnp->cn_namelen;
   1462 	isnq = (VFSTONFS(dvp->v_mount)->nm_flag & NFSMNT_NQNFS);
   1463 	nfsstats.rpccnt[NFSPROC_MKDIR]++;
   1464 	nfsm_reqhead(dvp, NFSPROC_MKDIR,
   1465 	  NFSX_FH+NFSX_UNSIGNED+nfsm_rndup(len)+NFSX_SATTR(isnq));
   1466 	nfsm_fhtom(dvp);
   1467 	nfsm_strtom(cnp->cn_nameptr, len, NFS_MAXNAMLEN);
   1468 	nfsm_build(sp, struct nfsv2_sattr *, NFSX_SATTR(isnq));
   1469 	sp->sa_mode = vtonfs_mode(VDIR, vap->va_mode);
   1470 	sp->sa_uid = nfs_xdrneg1;
   1471 	sp->sa_gid = nfs_xdrneg1;
   1472 	if (isnq) {
   1473 		quad_t qval = -1;
   1474 
   1475 		txdr_hyper(&qval, &sp->sa_nqsize);
   1476 		sp->sa_nqflags = 0;
   1477 		txdr_nqtime(&vap->va_atime, &sp->sa_nqatime);
   1478 		txdr_nqtime(&vap->va_mtime, &sp->sa_nqmtime);
   1479 	} else {
   1480 		sp->sa_nfssize = nfs_xdrneg1;
   1481 		txdr_nfstime(&vap->va_atime, &sp->sa_nfsatime);
   1482 		txdr_nfstime(&vap->va_mtime, &sp->sa_nfsmtime);
   1483 	}
   1484 	nfsm_request(dvp, NFSPROC_MKDIR, cnp->cn_proc, cnp->cn_cred);
   1485 	nfsm_mtofh(dvp, *vpp);
   1486 	nfsm_reqdone;
   1487 	VTONFS(dvp)->n_flag |= NMODIFIED;
   1488 	VTONFS(dvp)->n_attrstamp = 0;
   1489 	/*
   1490 	 * Kludge: Map EEXIST => 0 assuming that you have a reply to a retry
   1491 	 * if we can succeed in looking up the directory.
   1492 	 * "firsttry" is necessary since the macros may "goto nfsmout" which
   1493 	 * is above the if on errors. (Ugh)
   1494 	 */
   1495 	if (error == EEXIST && firsttry) {
   1496 		firsttry = 0;
   1497 		error = 0;
   1498 		nfsstats.rpccnt[NFSPROC_LOOKUP]++;
   1499 		*vpp = NULL;
   1500 		nfsm_reqhead(dvp, NFSPROC_LOOKUP,
   1501 		    NFSX_FH+NFSX_UNSIGNED+nfsm_rndup(len));
   1502 		nfsm_fhtom(dvp);
   1503 		nfsm_strtom(cnp->cn_nameptr, len, NFS_MAXNAMLEN);
   1504 		nfsm_request(dvp, NFSPROC_LOOKUP, cnp->cn_proc, cnp->cn_cred);
   1505 		nfsm_mtofh(dvp, *vpp);
   1506 		if ((*vpp)->v_type != VDIR) {
   1507 			vput(*vpp);
   1508 			error = EEXIST;
   1509 		}
   1510 		m_freem(mrep);
   1511 	}
   1512 	FREE(cnp->cn_pnbuf, M_NAMEI);
   1513 	vrele(dvp);
   1514 	return (error);
   1515 }
   1516 
   1517 /*
   1518  * nfs remove directory call
   1519  */
   1520 int
   1521 nfs_rmdir(v)
   1522 	void *v;
   1523 {
   1524 	struct vop_rmdir_args /* {
   1525 		struct vnode *a_dvp;
   1526 		struct vnode *a_vp;
   1527 		struct componentname *a_cnp;
   1528 	} */ *ap = v;
   1529 	register struct vnode *vp = ap->a_vp;
   1530 	register struct vnode *dvp = ap->a_dvp;
   1531 	register struct componentname *cnp = ap->a_cnp;
   1532 	register u_int32_t *tl;
   1533 	register caddr_t cp;
   1534 	register int32_t t2;
   1535 	caddr_t bpos, dpos;
   1536 	int error = 0;
   1537 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
   1538 
   1539 	if (dvp == vp) {
   1540 		vrele(dvp);
   1541 		vrele(dvp);
   1542 		FREE(cnp->cn_pnbuf, M_NAMEI);
   1543 		return (EINVAL);
   1544 	}
   1545 	nfsstats.rpccnt[NFSPROC_RMDIR]++;
   1546 	nfsm_reqhead(dvp, NFSPROC_RMDIR,
   1547 		NFSX_FH+NFSX_UNSIGNED+nfsm_rndup(cnp->cn_namelen));
   1548 	nfsm_fhtom(dvp);
   1549 	nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
   1550 	nfsm_request(dvp, NFSPROC_RMDIR, cnp->cn_proc, cnp->cn_cred);
   1551 	nfsm_reqdone;
   1552 	FREE(cnp->cn_pnbuf, M_NAMEI);
   1553 	VTONFS(dvp)->n_flag |= NMODIFIED;
   1554 	VTONFS(dvp)->n_attrstamp = 0;
   1555 	cache_purge(dvp);
   1556 	cache_purge(vp);
   1557 	vrele(vp);
   1558 	vrele(dvp);
   1559 	/*
   1560 	 * Kludge: Map ENOENT => 0 assuming that you have a reply to a retry.
   1561 	 */
   1562 	if (error == ENOENT)
   1563 		error = 0;
   1564 	return (error);
   1565 }
   1566 
   1567 /*
   1568  * nfs readdir call
   1569  * Although cookie is defined as opaque, I translate it to/from net byte
   1570  * order so that it looks more sensible. This appears consistent with the
   1571  * Ultrix implementation of NFS.
   1572  */
   1573 int
   1574 nfs_readdir(v)
   1575 	void *v;
   1576 {
   1577 	struct vop_readdir_args /* {
   1578 		struct vnode *a_vp;
   1579 		struct uio *a_uio;
   1580 		struct ucred *a_cred;
   1581 		int *a_eofflag;
   1582 		u_long *a_cookies;
   1583 		int a_ncookies;
   1584 	} */ *ap = v;
   1585 	register struct vnode *vp = ap->a_vp;
   1586 	register struct nfsnode *np = VTONFS(vp);
   1587 	register struct uio *uio = ap->a_uio;
   1588 	char *base = uio->uio_iov->iov_base;
   1589 	int tresid, error;
   1590 	struct vattr vattr;
   1591 
   1592 	if (vp->v_type != VDIR)
   1593 		return (EPERM);
   1594 	/*
   1595 	 * First, check for hit on the EOF offset cache
   1596 	 */
   1597 	if (uio->uio_offset != 0 && uio->uio_offset == np->n_direofoffset &&
   1598 	    (np->n_flag & NMODIFIED) == 0) {
   1599 		if (VFSTONFS(vp->v_mount)->nm_flag & NFSMNT_NQNFS) {
   1600 			if (NQNFS_CKCACHABLE(vp, NQL_READ)) {
   1601 				nfsstats.direofcache_hits++;
   1602 				return (0);
   1603 			}
   1604 		} else if (VOP_GETATTR(vp, &vattr, ap->a_cred, uio->uio_procp) == 0 &&
   1605 			np->n_mtime == vattr.va_mtime.tv_sec) {
   1606 			nfsstats.direofcache_hits++;
   1607 			return (0);
   1608 		}
   1609 	}
   1610 
   1611 	/*
   1612 	 * Call nfs_bioread() to do the real work.
   1613 	 */
   1614 	tresid = uio->uio_resid;
   1615 	error = nfs_bioread(vp, uio, 0, ap->a_cred);
   1616 
   1617 	if (!error && uio->uio_resid == tresid)
   1618 		nfsstats.direofcache_misses++;
   1619 
   1620 	if (!error && ap->a_cookies) {
   1621 		struct dirent *dp;
   1622 		u_long *cptr, *cookies = ap->a_cookies;
   1623 		int ncookies = ap->a_ncookies;
   1624 
   1625 		/*
   1626 		 * Only the NFS server and emulations use cookies, and they
   1627 		 * load the directory block into system space, so we can
   1628 		 * just look at it directly.
   1629 		 */
   1630 		if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1)
   1631 			panic("ufs_readdir: lost in space");
   1632 		while (ncookies-- && base < uio->uio_iov->iov_base) {
   1633 			dp = (struct dirent *) base;
   1634 			if (dp->d_reclen == 0)
   1635 				break;
   1636 			cptr = (u_long *)((caddr_t)dp + dp->d_reclen -
   1637 					  sizeof(u_long));
   1638 			*(cookies++) = *cptr;
   1639 			base += dp->d_reclen;
   1640 		}
   1641 		uio->uio_resid += (uio->uio_iov->iov_base - base);
   1642 		uio->uio_iov->iov_len += (uio->uio_iov->iov_base - base);
   1643 		uio->uio_iov->iov_base = base;
   1644 	}
   1645 
   1646 	return (error);
   1647 }
   1648 
   1649 /*
   1650  * Readdir rpc call.
   1651  * Called from below the buffer cache by nfs_doio().
   1652  */
   1653 int
   1654 nfs_readdirrpc(vp, uiop, cred)
   1655 	register struct vnode *vp;
   1656 	struct uio *uiop;
   1657 	struct ucred *cred;
   1658 {
   1659 	register long len;
   1660 	register struct dirent *dp = NULL;
   1661 	register u_int32_t *tl;
   1662 	register caddr_t cp;
   1663 	register int32_t t1;
   1664 	long tlen, lastlen = 0;
   1665 	caddr_t bpos, dpos, cp2;
   1666 	int error = 0;
   1667 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
   1668 	struct mbuf *md2;
   1669 	caddr_t dpos2;
   1670 	int siz;
   1671 	int more_dirs = 1;
   1672 	u_long off = 0, savoff = 0;
   1673 	struct dirent *savdp = NULL;
   1674 	struct nfsmount *nmp;
   1675 	struct nfsnode *np = VTONFS(vp);
   1676 	long tresid, extra;
   1677 
   1678 	nmp = VFSTONFS(vp->v_mount);
   1679 	extra = uiop->uio_resid & (NFS_DIRBLKSIZ - 1);
   1680 	uiop->uio_resid -= extra;
   1681 	tresid = uiop->uio_resid;
   1682 	/*
   1683 	 * Loop around doing readdir rpc's of size uio_resid or nm_rsize,
   1684 	 * whichever is smaller, truncated to a multiple of NFS_DIRBLKSIZ.
   1685 	 * The stopping criteria is EOF or buffer full.
   1686 	 */
   1687 	while (more_dirs && uiop->uio_resid >= NFS_DIRBLKSIZ) {
   1688 		nfsstats.rpccnt[NFSPROC_READDIR]++;
   1689 		nfsm_reqhead(vp, NFSPROC_READDIR,
   1690 			NFSX_FH + 2 * NFSX_UNSIGNED);
   1691 		nfsm_fhtom(vp);
   1692 		nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
   1693 		off = (u_long)uiop->uio_offset;
   1694 		*tl++ = txdr_unsigned(off);
   1695 		*tl = txdr_unsigned(((uiop->uio_resid > nmp->nm_rsize) ?
   1696 			nmp->nm_rsize : uiop->uio_resid) & ~(NFS_DIRBLKSIZ-1));
   1697 		nfsm_request(vp, NFSPROC_READDIR, uiop->uio_procp, cred);
   1698 		siz = 0;
   1699 		nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
   1700 		more_dirs = fxdr_unsigned(int, *tl);
   1701 
   1702 		/* Save the position so that we can do nfsm_mtouio() later */
   1703 		dpos2 = dpos;
   1704 		md2 = md;
   1705 
   1706 		/* loop thru the dir entries, doctoring them to 4bsd form */
   1707 #ifdef lint
   1708 		dp = (struct dirent *)0;
   1709 #endif /* lint */
   1710 		while (more_dirs && siz < uiop->uio_resid) {
   1711 			savoff = off;		/* Hold onto offset and dp */
   1712 			savdp = dp;
   1713 			nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
   1714 			dp = (struct dirent *)tl;
   1715 			dp->d_fileno = fxdr_unsigned(u_int32_t, *tl++);
   1716 			len = fxdr_unsigned(int, *tl);
   1717 			if (len <= 0 || len > NFS_MAXNAMLEN) {
   1718 				error = EBADRPC;
   1719 				m_freem(mrep);
   1720 				goto nfsmout;
   1721 			}
   1722 			dp->d_namlen = (u_char)len;
   1723 			dp->d_type = DT_UNKNOWN;
   1724 			nfsm_adv(len);		/* Point past name */
   1725 			tlen = nfsm_rndup(len);
   1726 			/*
   1727 			 * This should not be necessary, but some servers have
   1728 			 * broken XDR such that these bytes are not null filled.
   1729 			 */
   1730 			if (tlen != len) {
   1731 				*dpos = '\0';	/* Null-terminate */
   1732 				nfsm_adv(tlen - len);
   1733 				len = tlen;
   1734 			}
   1735 			nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
   1736 			off = fxdr_unsigned(u_int32_t, *tl);
   1737 			*tl++ = 0;	/* Ensures null termination of name */
   1738 			more_dirs = fxdr_unsigned(int, *tl);
   1739 			*tl = off;	/* Store offset for cookie retrieval */
   1740 			dp->d_reclen = len + 4 * NFSX_UNSIGNED;
   1741 			siz += dp->d_reclen;
   1742 		}
   1743 		/*
   1744 		 * If at end of rpc data, get the eof boolean
   1745 		 */
   1746 		if (!more_dirs) {
   1747 			nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
   1748 			more_dirs = (fxdr_unsigned(int, *tl) == 0);
   1749 
   1750 			/*
   1751 			 * If at EOF, cache directory offset
   1752 			 */
   1753 			if (!more_dirs)
   1754 				np->n_direofoffset = off;
   1755 		}
   1756 		/*
   1757 		 * If there is too much to fit in the data buffer, use savoff and
   1758 		 * savdp to trim off the last record.
   1759 		 * --> we are not at eof
   1760 		 */
   1761 		if (siz > uiop->uio_resid) {
   1762 			off = savoff;
   1763 			siz -= dp->d_reclen;
   1764 			dp = savdp;
   1765 			more_dirs = 0;	/* Paranoia */
   1766 		}
   1767 		if (siz > 0) {
   1768 			lastlen = dp->d_reclen;
   1769 			md = md2;
   1770 			dpos = dpos2;
   1771 			nfsm_mtouio(uiop, siz);
   1772 			uiop->uio_offset = (off_t)off;
   1773 		} else
   1774 			more_dirs = 0;	/* Ugh, never happens, but in case.. */
   1775 		m_freem(mrep);
   1776 	}
   1777 	/*
   1778 	 * Fill last record, iff any, out to a multiple of NFS_DIRBLKSIZ
   1779 	 * by increasing d_reclen for the last record.
   1780 	 */
   1781 	if (uiop->uio_resid < tresid) {
   1782 		len = uiop->uio_resid & (NFS_DIRBLKSIZ - 1);
   1783 		if (len > 0) {
   1784 			dp = (struct dirent *)
   1785 				(uiop->uio_iov->iov_base - lastlen);
   1786 			dp->d_reclen += len;
   1787 			*(u_long *)((caddr_t)dp + dp->d_reclen -
   1788 				    sizeof(u_long)) = off;
   1789 			uiop->uio_iov->iov_base += len;
   1790 			uiop->uio_iov->iov_len -= len;
   1791 			uiop->uio_resid -= len;
   1792 		}
   1793 	}
   1794 nfsmout:
   1795 	uiop->uio_resid += extra;
   1796 	return (error);
   1797 }
   1798 
   1799 /*
   1800  * Nqnfs readdir_and_lookup RPC. Used in place of nfs_readdirrpc().
   1801  */
   1802 int
   1803 nfs_readdirlookrpc(vp, uiop, cred)
   1804 	struct vnode *vp;
   1805 	register struct uio *uiop;
   1806 	struct ucred *cred;
   1807 {
   1808 	register int len;
   1809 	register struct dirent *dp = NULL;
   1810 	register u_int32_t *tl;
   1811 	register caddr_t cp;
   1812 	register int32_t t1;
   1813 	caddr_t bpos, dpos, cp2;
   1814 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
   1815 	struct nameidata nami, *ndp = &nami;
   1816 	struct componentname *cnp = &ndp->ni_cnd;
   1817 	u_long off = 0, endoff = 0, fileno;
   1818 	time_t reqtime, ltime = 0;
   1819 	struct nfsmount *nmp;
   1820 	struct nfsnode *np;
   1821 	struct vnode *newvp;
   1822 	nfsv2fh_t *fhp;
   1823 	u_quad_t frev;
   1824 	int error = 0, tlen, more_dirs = 1, tresid, doit, bigenough, i;
   1825 	int cachable = 0;
   1826 
   1827 	if (uiop->uio_iovcnt != 1)
   1828 		panic("nfs rdirlook");
   1829 	nmp = VFSTONFS(vp->v_mount);
   1830 	tresid = uiop->uio_resid;
   1831 	ndp->ni_dvp = vp;
   1832 	newvp = NULLVP;
   1833 	/*
   1834 	 * Loop around doing readdir rpc's of size uio_resid or nm_rsize,
   1835 	 * whichever is smaller, truncated to a multiple of NFS_DIRBLKSIZ.
   1836 	 * The stopping criteria is EOF or buffer full.
   1837 	 */
   1838 	while (more_dirs && uiop->uio_resid >= NFS_DIRBLKSIZ) {
   1839 		nfsstats.rpccnt[NQNFSPROC_READDIRLOOK]++;
   1840 		nfsm_reqhead(vp, NQNFSPROC_READDIRLOOK,
   1841 			NFSX_FH + 3 * NFSX_UNSIGNED);
   1842 		nfsm_fhtom(vp);
   1843  		nfsm_build(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
   1844 		off = (u_long)uiop->uio_offset;
   1845 		*tl++ = txdr_unsigned(off);
   1846 		*tl++ = txdr_unsigned(((uiop->uio_resid > nmp->nm_rsize) ?
   1847 			nmp->nm_rsize : uiop->uio_resid) & ~(NFS_DIRBLKSIZ-1));
   1848 		if (nmp->nm_flag & NFSMNT_NQLOOKLEASE)
   1849 			*tl = txdr_unsigned(nmp->nm_leaseterm);
   1850 		else
   1851 			*tl = 0;
   1852 		reqtime = time.tv_sec;
   1853 		nfsm_request(vp, NQNFSPROC_READDIRLOOK, uiop->uio_procp, cred);
   1854 		nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
   1855 		more_dirs = fxdr_unsigned(int, *tl);
   1856 
   1857 		/* loop thru the dir entries, doctoring them to 4bsd form */
   1858 		bigenough = 1;
   1859 		while (more_dirs && bigenough) {
   1860 			doit = 1;
   1861 			nfsm_dissect(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
   1862 			if (nmp->nm_flag & NFSMNT_NQLOOKLEASE) {
   1863 				cachable = fxdr_unsigned(int, *tl++);
   1864 				ltime = reqtime + fxdr_unsigned(int, *tl++);
   1865 				fxdr_hyper(tl, &frev);
   1866 			}
   1867 			nfsm_dissect(fhp, nfsv2fh_t *, NFSX_FH);
   1868 			if (!bcmp(VTONFS(vp)->n_fh.fh_bytes, (caddr_t)fhp, NFSX_FH)) {
   1869 				VREF(vp);
   1870 				newvp = vp;
   1871 				np = VTONFS(vp);
   1872 			} else {
   1873 				error = nfs_nget(vp->v_mount, fhp, &np);
   1874 				if (error)
   1875 					doit = 0;
   1876 				newvp = NFSTOV(np);
   1877 			}
   1878 			error = nfs_loadattrcache(&newvp, &md, &dpos, NULL);
   1879 			if (error)
   1880 				doit = 0;
   1881 			nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
   1882 			fileno = fxdr_unsigned(u_int32_t, *tl++);
   1883 			len = fxdr_unsigned(int, *tl);
   1884 			if (len <= 0 || len > NFS_MAXNAMLEN) {
   1885 				error = EBADRPC;
   1886 				m_freem(mrep);
   1887 				goto nfsmout;
   1888 			}
   1889 			tlen = (len + 4) & ~0x3;
   1890 			if ((tlen + DIRHDSIZ) > uiop->uio_resid)
   1891 				bigenough = 0;
   1892 			if (bigenough && doit) {
   1893 				dp = (struct dirent *)uiop->uio_iov->iov_base;
   1894 				dp->d_fileno = fileno;
   1895 				dp->d_namlen = len;
   1896 				dp->d_reclen = tlen + DIRHDSIZ;
   1897 				dp->d_type =
   1898 				    IFTODT(VTTOIF(np->n_vattr.va_type));
   1899 				uiop->uio_resid -= DIRHDSIZ;
   1900 				uiop->uio_iov->iov_base += DIRHDSIZ;
   1901 				uiop->uio_iov->iov_len -= DIRHDSIZ;
   1902 				cnp->cn_nameptr = uiop->uio_iov->iov_base;
   1903 				cnp->cn_namelen = len;
   1904 				ndp->ni_vp = newvp;
   1905 				nfsm_mtouio(uiop, len);
   1906 				cp = uiop->uio_iov->iov_base;
   1907 				tlen -= len;
   1908 				for (i = 0; i < tlen; i++)
   1909 					*cp++ = '\0';
   1910 				uiop->uio_iov->iov_base += tlen;
   1911 				uiop->uio_iov->iov_len -= tlen;
   1912 				uiop->uio_resid -= tlen;
   1913 				cnp->cn_hash = 0;
   1914 				for (cp = cnp->cn_nameptr, i = 1; i <= len; i++, cp++)
   1915 					cnp->cn_hash += (unsigned char)*cp * i;
   1916 				if ((nmp->nm_flag & NFSMNT_NQLOOKLEASE) &&
   1917 					ltime > time.tv_sec)
   1918 					nqnfs_clientlease(nmp, np, NQL_READ,
   1919 						cachable, ltime, frev);
   1920 				if (cnp->cn_namelen <= NCHNAMLEN)
   1921 				    cache_enter(ndp->ni_dvp, ndp->ni_vp, cnp);
   1922 			} else {
   1923 				nfsm_adv(nfsm_rndup(len));
   1924 			}
   1925 			if (newvp != NULLVP) {
   1926 				vrele(newvp);
   1927 				newvp = NULLVP;
   1928 			}
   1929 			nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
   1930 			if (bigenough)
   1931 				endoff = off = fxdr_unsigned(u_int32_t, *tl++);
   1932 			else
   1933 				endoff = fxdr_unsigned(u_int32_t, *tl++);
   1934 			more_dirs = fxdr_unsigned(int, *tl);
   1935 			*tl = endoff;	/* Store offset for cookies. */
   1936 		}
   1937 		/*
   1938 		 * If at end of rpc data, get the eof boolean
   1939 		 */
   1940 		if (!more_dirs) {
   1941 			nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
   1942 			more_dirs = (fxdr_unsigned(int, *tl) == 0);
   1943 
   1944 			/*
   1945 			 * If at EOF, cache directory offset
   1946 			 */
   1947 			if (!more_dirs)
   1948 				VTONFS(vp)->n_direofoffset = endoff;
   1949 		}
   1950 		if (uiop->uio_resid < tresid)
   1951 			uiop->uio_offset = (off_t)off;
   1952 		else
   1953 			more_dirs = 0;
   1954 		m_freem(mrep);
   1955 	}
   1956 	/*
   1957 	 * Fill last record, iff any, out to a multiple of NFS_DIRBLKSIZ
   1958 	 * by increasing d_reclen for the last record.
   1959 	 */
   1960 	if (uiop->uio_resid < tresid) {
   1961 		len = uiop->uio_resid & (NFS_DIRBLKSIZ - 1);
   1962 		if (len > 0) {
   1963 			dp->d_reclen += len;
   1964 			*(u_long *)((caddr_t)dp + dp->d_reclen -
   1965 				    sizeof(u_long)) = off;
   1966 			uiop->uio_iov->iov_base += len;
   1967 			uiop->uio_iov->iov_len -= len;
   1968 			uiop->uio_resid -= len;
   1969 		}
   1970 	}
   1971 nfsmout:
   1972 	if (newvp != NULLVP)
   1973 		vrele(newvp);
   1974 	return (error);
   1975 }
   1976 static char hextoasc[] = "0123456789abcdef";
   1977 
   1978 /*
   1979  * Silly rename. To make the NFS filesystem that is stateless look a little
   1980  * more like the "ufs" a remove of an active vnode is translated to a rename
   1981  * to a funny looking filename that is removed by nfs_inactive on the
   1982  * nfsnode. There is the potential for another process on a different client
   1983  * to create the same funny name between the nfs_lookitup() fails and the
   1984  * nfs_rename() completes, but...
   1985  */
   1986 int
   1987 nfs_sillyrename(dvp, vp, cnp)
   1988 	struct vnode *dvp, *vp;
   1989 	struct componentname *cnp;
   1990 {
   1991 	register struct nfsnode *np;
   1992 	register struct sillyrename *sp;
   1993 	int error;
   1994 	short pid;
   1995 
   1996 	cache_purge(dvp);
   1997 	np = VTONFS(vp);
   1998 #ifdef SILLYSEPARATE
   1999 	MALLOC(sp, struct sillyrename *, sizeof (struct sillyrename),
   2000 		M_NFSREQ, M_WAITOK);
   2001 #else
   2002 	sp = &np->n_silly;
   2003 #endif
   2004 	sp->s_cred = crdup(cnp->cn_cred);
   2005 	sp->s_dvp = dvp;
   2006 	VREF(dvp);
   2007 
   2008 	/* Fudge together a funny name */
   2009 	pid = cnp->cn_proc->p_pid;
   2010 	bcopy(".nfsAxxxx4.4", sp->s_name, 13);
   2011 	sp->s_namlen = 12;
   2012 	sp->s_name[8] = hextoasc[pid & 0xf];
   2013 	sp->s_name[7] = hextoasc[(pid >> 4) & 0xf];
   2014 	sp->s_name[6] = hextoasc[(pid >> 8) & 0xf];
   2015 	sp->s_name[5] = hextoasc[(pid >> 12) & 0xf];
   2016 
   2017 	/* Try lookitups until we get one that isn't there */
   2018 	while (nfs_lookitup(sp, (nfsv2fh_t *)0, cnp->cn_proc) == 0) {
   2019 		sp->s_name[4]++;
   2020 		if (sp->s_name[4] > 'z') {
   2021 			error = EINVAL;
   2022 			goto bad;
   2023 		}
   2024 	}
   2025 	if ((error = nfs_renameit(dvp, cnp, sp)) != 0)
   2026 		goto bad;
   2027 	nfs_lookitup(sp, &np->n_fh, cnp->cn_proc);
   2028 	np->n_sillyrename = sp;
   2029 	return (0);
   2030 bad:
   2031 	vrele(sp->s_dvp);
   2032 	crfree(sp->s_cred);
   2033 #ifdef SILLYSEPARATE
   2034 	free((caddr_t)sp, M_NFSREQ);
   2035 #endif
   2036 	return (error);
   2037 }
   2038 
   2039 /*
   2040  * Look up a file name for silly rename stuff.
   2041  * Just like nfs_lookup() except that it doesn't load returned values
   2042  * into the nfsnode table.
   2043  * If fhp != NULL it copies the returned file handle out
   2044  */
   2045 int
   2046 nfs_lookitup(sp, fhp, procp)
   2047 	register struct sillyrename *sp;
   2048 	nfsv2fh_t *fhp;
   2049 	struct proc *procp;
   2050 {
   2051 	register struct vnode *vp = sp->s_dvp;
   2052 	register u_int32_t *tl;
   2053 	register caddr_t cp;
   2054 	register int32_t t1, t2;
   2055 	caddr_t bpos, dpos, cp2;
   2056 	int error = 0, isnq;
   2057 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
   2058 	long len;
   2059 
   2060 	isnq = (VFSTONFS(vp->v_mount)->nm_flag & NFSMNT_NQNFS);
   2061 	nfsstats.rpccnt[NFSPROC_LOOKUP]++;
   2062 	len = sp->s_namlen;
   2063 	nfsm_reqhead(vp, NFSPROC_LOOKUP, NFSX_FH+NFSX_UNSIGNED+nfsm_rndup(len));
   2064 	if (isnq) {
   2065 		nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
   2066 		*tl = 0;
   2067 	}
   2068 	nfsm_fhtom(vp);
   2069 	nfsm_strtom(sp->s_name, len, NFS_MAXNAMLEN);
   2070 	nfsm_request(vp, NFSPROC_LOOKUP, procp, sp->s_cred);
   2071 	if (fhp != NULL) {
   2072 		if (isnq)
   2073 			nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
   2074 		nfsm_dissect(cp, caddr_t, NFSX_FH);
   2075 		bcopy(cp, (caddr_t)fhp, NFSX_FH);
   2076 	}
   2077 	nfsm_reqdone;
   2078 	return (error);
   2079 }
   2080 
   2081 /*
   2082  * Kludge City..
   2083  * - make nfs_bmap() essentially a no-op that does no translation
   2084  * - do nfs_strategy() by faking physical I/O with nfs_readrpc/nfs_writerpc
   2085  *   after mapping the physical addresses into Kernel Virtual space in the
   2086  *   nfsiobuf area.
   2087  *   (Maybe I could use the process's page mapping, but I was concerned that
   2088  *    Kernel Write might not be enabled and also figured copyout() would do
   2089  *    a lot more work than bcopy() and also it currently happens in the
   2090  *    context of the swapper process (2).
   2091  */
   2092 int
   2093 nfs_bmap(v)
   2094 	void *v;
   2095 {
   2096 	struct vop_bmap_args /* {
   2097 		struct vnode *a_vp;
   2098 		daddr_t  a_bn;
   2099 		struct vnode **a_vpp;
   2100 		daddr_t *a_bnp;
   2101 		int *a_runp;
   2102 	} */ *ap = v;
   2103 	register struct vnode *vp = ap->a_vp;
   2104 
   2105 	if (ap->a_vpp != NULL)
   2106 		*ap->a_vpp = vp;
   2107 	if (ap->a_bnp != NULL)
   2108 		*ap->a_bnp = ap->a_bn * btodb(vp->v_mount->mnt_stat.f_iosize);
   2109 	return (0);
   2110 }
   2111 
   2112 /*
   2113  * Strategy routine.
   2114  * For async requests when nfsiod(s) are running, queue the request by
   2115  * calling nfs_asyncio(), otherwise just all nfs_doio() to do the
   2116  * request.
   2117  */
   2118 int
   2119 nfs_strategy(v)
   2120 	void *v;
   2121 {
   2122 	struct vop_strategy_args *ap = v;
   2123 	register struct buf *bp = ap->a_bp;
   2124 	struct ucred *cr;
   2125 	struct proc *p;
   2126 	int error = 0;
   2127 
   2128 	if ((bp->b_flags & (B_PHYS|B_ASYNC)) == (B_PHYS|B_ASYNC))
   2129 		panic("nfs physio/async");
   2130 	if (bp->b_flags & B_ASYNC)
   2131 		p = (struct proc *)0;
   2132 	else
   2133 		p = curproc;	/* XXX */
   2134 	if (bp->b_flags & B_READ)
   2135 		cr = bp->b_rcred;
   2136 	else
   2137 		cr = bp->b_wcred;
   2138 	/*
   2139 	 * If the op is asynchronous and an i/o daemon is waiting
   2140 	 * queue the request, wake it up and wait for completion
   2141 	 * otherwise just do it ourselves.
   2142 	 */
   2143 	if ((bp->b_flags & B_ASYNC) == 0 ||
   2144 		nfs_asyncio(bp, NOCRED))
   2145 		error = nfs_doio(bp, cr, p);
   2146 	return (error);
   2147 }
   2148 
   2149 /*
   2150  * Mmap a file
   2151  *
   2152  * NB Currently unsupported.
   2153  */
   2154 /* ARGSUSED */
   2155 int
   2156 nfs_mmap(v)
   2157 	void *v;
   2158 {
   2159 #if 0
   2160 	struct vop_mmap_args /* {
   2161 		struct vnode *a_vp;
   2162 		int  a_fflags;
   2163 		struct ucred *a_cred;
   2164 		struct proc *a_p;
   2165 	} */ *ap = v;
   2166 #endif
   2167 
   2168 	return (EINVAL);
   2169 }
   2170 
   2171 /*
   2172  * Flush all the blocks associated with a vnode.
   2173  * 	Walk through the buffer pool and push any dirty pages
   2174  *	associated with the vnode.
   2175  */
   2176 /* ARGSUSED */
   2177 int
   2178 nfs_fsync(v)
   2179 	void *v;
   2180 {
   2181 	struct vop_fsync_args /* {
   2182 		struct vnodeop_desc *a_desc;
   2183 		struct vnode * a_vp;
   2184 		struct ucred * a_cred;
   2185 		int  a_waitfor;
   2186 		struct proc * a_p;
   2187 	} */ *ap = v;
   2188 	register struct vnode *vp = ap->a_vp;
   2189 	register struct nfsnode *np = VTONFS(vp);
   2190 	register struct buf *bp;
   2191 	struct buf *nbp;
   2192 	struct nfsmount *nmp;
   2193 	int s, error = 0, slptimeo = 0, slpflag = 0;
   2194 
   2195 	nmp = VFSTONFS(vp->v_mount);
   2196 	if (nmp->nm_flag & NFSMNT_INT)
   2197 		slpflag = PCATCH;
   2198 loop:
   2199 	s = splbio();
   2200 	for (bp = vp->v_dirtyblkhd.lh_first; bp; bp = nbp) {
   2201 		nbp = bp->b_vnbufs.le_next;
   2202 		if (bp->b_flags & B_BUSY) {
   2203 			if (ap->a_waitfor != MNT_WAIT)
   2204 				continue;
   2205 			bp->b_flags |= B_WANTED;
   2206 			error = tsleep((caddr_t)bp, slpflag | (PRIBIO + 1),
   2207 				"nfsfsync", slptimeo);
   2208 			splx(s);
   2209 			if (error) {
   2210 			    if (nfs_sigintr(nmp, (struct nfsreq *)0, ap->a_p))
   2211 				return (EINTR);
   2212 			    if (slpflag == PCATCH) {
   2213 				slpflag = 0;
   2214 				slptimeo = 2 * hz;
   2215 			    }
   2216 			}
   2217 			goto loop;
   2218 		}
   2219 		if ((bp->b_flags & B_DELWRI) == 0)
   2220 			panic("nfs_fsync: not dirty");
   2221 		bremfree(bp);
   2222 		bp->b_flags |= B_BUSY;
   2223 		splx(s);
   2224 		bp->b_flags |= B_ASYNC;
   2225 		VOP_BWRITE(bp);
   2226 		goto loop;
   2227 	}
   2228 	splx(s);
   2229 	if (ap->a_waitfor == MNT_WAIT) {
   2230 		while (vp->v_numoutput) {
   2231 			vp->v_flag |= VBWAIT;
   2232 			error = tsleep((caddr_t)&vp->v_numoutput,
   2233 				slpflag | (PRIBIO + 1), "nfsfsync", slptimeo);
   2234 			if (error) {
   2235 			    if (nfs_sigintr(nmp, (struct nfsreq *)0, ap->a_p))
   2236 				return (EINTR);
   2237 			    if (slpflag == PCATCH) {
   2238 				slpflag = 0;
   2239 				slptimeo = 2 * hz;
   2240 			    }
   2241 			}
   2242 		}
   2243 		if (vp->v_dirtyblkhd.lh_first) {
   2244 #ifdef DIAGNOSTIC
   2245 			vprint("nfs_fsync: dirty", vp);
   2246 #endif
   2247 			goto loop;
   2248 		}
   2249 	}
   2250 	if (np->n_flag & NWRITEERR) {
   2251 		error = np->n_error;
   2252 		np->n_flag &= ~NWRITEERR;
   2253 	}
   2254 	return (error);
   2255 }
   2256 
   2257 /*
   2258  * Return POSIX pathconf information applicable to nfs.
   2259  *
   2260  * Currently the NFS protocol does not support getting such
   2261  * information from the remote server.
   2262  */
   2263 /* ARGSUSED */
   2264 int
   2265 nfs_pathconf(v)
   2266 	void *v;
   2267 {
   2268 #if 0
   2269 	struct vop_pathconf_args /* {
   2270 		struct vnode *a_vp;
   2271 		int a_name;
   2272 		register_t *a_retval;
   2273 	} */ *ap = v;
   2274 #endif
   2275 
   2276 	return (EINVAL);
   2277 }
   2278 
   2279 /*
   2280  * NFS advisory byte-level locks.
   2281  */
   2282 int
   2283 nfs_advlock(v)
   2284 	void *v;
   2285 {
   2286 	struct vop_advlock_args /* {
   2287 		struct vnode *a_vp;
   2288 		caddr_t  a_id;
   2289 		int  a_op;
   2290 		struct flock *a_fl;
   2291 		int  a_flags;
   2292 	} */ *ap = v;
   2293 	register struct nfsnode *np = VTONFS(ap->a_vp);
   2294 
   2295 	return (lf_advlock(&np->n_lockf, np->n_size, ap->a_id, ap->a_op,
   2296 	    ap->a_fl, ap->a_flags));
   2297 }
   2298 
   2299 /*
   2300  * Print out the contents of an nfsnode.
   2301  */
   2302 int
   2303 nfs_print(v)
   2304 	void *v;
   2305 {
   2306 	struct vop_print_args /* {
   2307 		struct vnode *a_vp;
   2308 	} */ *ap = v;
   2309 	register struct vnode *vp = ap->a_vp;
   2310 	register struct nfsnode *np = VTONFS(vp);
   2311 
   2312 	printf("tag VT_NFS, fileid %d fsid 0x%x",
   2313 		np->n_vattr.va_fileid, np->n_vattr.va_fsid);
   2314 #ifdef FIFO
   2315 	if (vp->v_type == VFIFO)
   2316 		fifo_printinfo(vp);
   2317 #endif /* FIFO */
   2318 	printf("\n");
   2319 	return 0;
   2320 }
   2321 
   2322 /*
   2323  * NFS directory offset lookup.
   2324  * Currently unsupported.
   2325  */
   2326 int
   2327 nfs_blkatoff(v)
   2328 	void *v;
   2329 {
   2330 #if 0
   2331 	struct vop_blkatoff_args /* {
   2332 		struct vnode *a_vp;
   2333 		off_t a_offset;
   2334 		char **a_res;
   2335 		struct buf **a_bpp;
   2336 	} */ *ap = v;
   2337 #endif
   2338 
   2339 	return (EOPNOTSUPP);
   2340 }
   2341 
   2342 /*
   2343  * NFS flat namespace allocation.
   2344  * Currently unsupported.
   2345  */
   2346 int
   2347 nfs_valloc(v)
   2348 	void *v;
   2349 {
   2350 #if 0
   2351 	struct vop_valloc_args /* {
   2352 		struct vnode *a_pvp;
   2353 		int a_mode;
   2354 		struct ucred *a_cred;
   2355 		struct vnode **a_vpp;
   2356 	} */ *ap = v;
   2357 #endif
   2358 
   2359 	return (EOPNOTSUPP);
   2360 }
   2361 
   2362 /*
   2363  * NFS flat namespace free.
   2364  * Currently unsupported.
   2365  */
   2366 int
   2367 nfs_vfree(v)
   2368 	void *v;
   2369 {
   2370 #if 0
   2371 	struct vop_vfree_args /* {
   2372 		struct vnode *a_pvp;
   2373 		ino_t a_ino;
   2374 		int a_mode;
   2375 	} */ *ap = v;
   2376 #endif
   2377 
   2378 	return (EOPNOTSUPP);
   2379 }
   2380 
   2381 /*
   2382  * NFS file truncation.
   2383  */
   2384 int
   2385 nfs_truncate(v)
   2386 	void *v;
   2387 {
   2388 #if 0
   2389 	struct vop_truncate_args /* {
   2390 		struct vnode *a_vp;
   2391 		off_t a_length;
   2392 		int a_flags;
   2393 		struct ucred *a_cred;
   2394 		struct proc *a_p;
   2395 	} */ *ap = v;
   2396 #endif
   2397 
   2398 	/* Use nfs_setattr */
   2399 	printf("nfs_truncate: need to implement!!");
   2400 	return (EOPNOTSUPP);
   2401 }
   2402 
   2403 /*
   2404  * NFS update.
   2405  */
   2406 int
   2407 nfs_update(v)
   2408 	void *v;
   2409 {
   2410 #if 0
   2411 	struct vop_update_args /* {
   2412 		struct vnode *a_vp;
   2413 		struct timeval *a_ta;
   2414 		struct timeval *a_tm;
   2415 		int a_waitfor;
   2416 	} */ *ap = v;
   2417 #endif
   2418 
   2419 	/* Use nfs_setattr */
   2420 	printf("nfs_update: need to implement!!");
   2421 	return (EOPNOTSUPP);
   2422 }
   2423 
   2424 /*
   2425  * nfs special file access vnode op.
   2426  * Essentially just get vattr and then imitate iaccess() since the device is
   2427  * local to the client.
   2428  */
   2429 int
   2430 nfsspec_access(v)
   2431 	void *v;
   2432 {
   2433 	struct vop_access_args /* {
   2434 		struct vnode *a_vp;
   2435 		int  a_mode;
   2436 		struct ucred *a_cred;
   2437 		struct proc *a_p;
   2438 	} */ *ap = v;
   2439 	struct vattr va;
   2440 	int error;
   2441 
   2442 	if ((error = VOP_GETATTR(ap->a_vp, &va, ap->a_cred, ap->a_p)) != 0)
   2443 		return (error);
   2444 
   2445 	return (vaccess(va.va_mode, va.va_uid, va.va_gid, ap->a_mode,
   2446 	    ap->a_cred));
   2447 }
   2448 
   2449 /*
   2450  * Read wrapper for special devices.
   2451  */
   2452 int
   2453 nfsspec_read(v)
   2454 	void *v;
   2455 {
   2456 	struct vop_read_args /* {
   2457 		struct vnode *a_vp;
   2458 		struct uio *a_uio;
   2459 		int  a_ioflag;
   2460 		struct ucred *a_cred;
   2461 	} */ *ap = v;
   2462 	register struct nfsnode *np = VTONFS(ap->a_vp);
   2463 
   2464 	/*
   2465 	 * Set access flag.
   2466 	 */
   2467 	np->n_flag |= NACC;
   2468 	np->n_atim = time;
   2469 	return (VOCALL(spec_vnodeop_p, VOFFSET(vop_read), ap));
   2470 }
   2471 
   2472 /*
   2473  * Write wrapper for special devices.
   2474  */
   2475 int
   2476 nfsspec_write(v)
   2477 	void *v;
   2478 {
   2479 	struct vop_write_args /* {
   2480 		struct vnode *a_vp;
   2481 		struct uio *a_uio;
   2482 		int  a_ioflag;
   2483 		struct ucred *a_cred;
   2484 	} */ *ap = v;
   2485 	register struct nfsnode *np = VTONFS(ap->a_vp);
   2486 
   2487 	/*
   2488 	 * Set update flag.
   2489 	 */
   2490 	np->n_flag |= NUPD;
   2491 	np->n_mtim = time;
   2492 	return (VOCALL(spec_vnodeop_p, VOFFSET(vop_write), ap));
   2493 }
   2494 
   2495 /*
   2496  * Close wrapper for special devices.
   2497  *
   2498  * Update the times on the nfsnode then do device close.
   2499  */
   2500 int
   2501 nfsspec_close(v)
   2502 	void *v;
   2503 {
   2504 	struct vop_close_args /* {
   2505 		struct vnode *a_vp;
   2506 		int  a_fflag;
   2507 		struct ucred *a_cred;
   2508 		struct proc *a_p;
   2509 	} */ *ap = v;
   2510 	register struct vnode *vp = ap->a_vp;
   2511 	register struct nfsnode *np = VTONFS(vp);
   2512 	struct vattr vattr;
   2513 
   2514 	if (np->n_flag & (NACC | NUPD)) {
   2515 		np->n_flag |= NCHG;
   2516 		if (vp->v_usecount == 1 &&
   2517 		    (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
   2518 			VATTR_NULL(&vattr);
   2519 			if (np->n_flag & NACC) {
   2520 				vattr.va_atime.tv_sec = np->n_atim.tv_sec;
   2521 				vattr.va_atime.tv_nsec =
   2522 				    np->n_atim.tv_usec * 1000;
   2523 			}
   2524 			if (np->n_flag & NUPD) {
   2525 				vattr.va_mtime.tv_sec = np->n_mtim.tv_sec;
   2526 				vattr.va_mtime.tv_nsec =
   2527 				    np->n_mtim.tv_usec * 1000;
   2528 			}
   2529 			(void)VOP_SETATTR(vp, &vattr, ap->a_cred, ap->a_p);
   2530 		}
   2531 	}
   2532 	return (VOCALL(spec_vnodeop_p, VOFFSET(vop_close), ap));
   2533 }
   2534 
   2535 #ifdef FIFO
   2536 /*
   2537  * Read wrapper for fifos.
   2538  */
   2539 int
   2540 nfsfifo_read(v)
   2541 	void *v;
   2542 {
   2543 	struct vop_read_args /* {
   2544 		struct vnode *a_vp;
   2545 		struct uio *a_uio;
   2546 		int  a_ioflag;
   2547 		struct ucred *a_cred;
   2548 	} */ *ap = v;
   2549 	extern int (**fifo_vnodeop_p) __P((void *));
   2550 	register struct nfsnode *np = VTONFS(ap->a_vp);
   2551 
   2552 	/*
   2553 	 * Set access flag.
   2554 	 */
   2555 	np->n_flag |= NACC;
   2556 	np->n_atim = time;
   2557 	return (VOCALL(fifo_vnodeop_p, VOFFSET(vop_read), ap));
   2558 }
   2559 
   2560 /*
   2561  * Write wrapper for fifos.
   2562  */
   2563 int
   2564 nfsfifo_write(v)
   2565 	void *v;
   2566 {
   2567 	struct vop_write_args /* {
   2568 		struct vnode *a_vp;
   2569 		struct uio *a_uio;
   2570 		int  a_ioflag;
   2571 		struct ucred *a_cred;
   2572 	} */ *ap = v;
   2573 	extern int (**fifo_vnodeop_p) __P((void *));
   2574 	register struct nfsnode *np = VTONFS(ap->a_vp);
   2575 
   2576 	/*
   2577 	 * Set update flag.
   2578 	 */
   2579 	np->n_flag |= NUPD;
   2580 	np->n_mtim = time;
   2581 	return (VOCALL(fifo_vnodeop_p, VOFFSET(vop_write), ap));
   2582 }
   2583 
   2584 /*
   2585  * Close wrapper for fifos.
   2586  *
   2587  * Update the times on the nfsnode then do fifo close.
   2588  */
   2589 int
   2590 nfsfifo_close(v)
   2591 	void *v;
   2592 {
   2593 	struct vop_close_args /* {
   2594 		struct vnode *a_vp;
   2595 		int  a_fflag;
   2596 		struct ucred *a_cred;
   2597 		struct proc *a_p;
   2598 	} */ *ap = v;
   2599 	register struct vnode *vp = ap->a_vp;
   2600 	register struct nfsnode *np = VTONFS(vp);
   2601 	struct vattr vattr;
   2602 	extern int (**fifo_vnodeop_p) __P((void *));
   2603 
   2604 	if (np->n_flag & (NACC | NUPD)) {
   2605 		if (np->n_flag & NACC)
   2606 			np->n_atim = time;
   2607 		if (np->n_flag & NUPD)
   2608 			np->n_mtim = time;
   2609 		np->n_flag |= NCHG;
   2610 		if (vp->v_usecount == 1 &&
   2611 		    (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
   2612 			VATTR_NULL(&vattr);
   2613 			if (np->n_flag & NACC) {
   2614 				vattr.va_atime.tv_sec = np->n_atim.tv_sec;
   2615 				vattr.va_atime.tv_nsec =
   2616 				    np->n_atim.tv_usec * 1000;
   2617 			}
   2618 			if (np->n_flag & NUPD) {
   2619 				vattr.va_mtime.tv_sec = np->n_mtim.tv_sec;
   2620 				vattr.va_mtime.tv_nsec =
   2621 				    np->n_mtim.tv_usec * 1000;
   2622 			}
   2623 			(void)VOP_SETATTR(vp, &vattr, ap->a_cred, ap->a_p);
   2624 		}
   2625 	}
   2626 	return (VOCALL(fifo_vnodeop_p, VOFFSET(vop_close), ap));
   2627 }
   2628 #endif /* FIFO */
   2629