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