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