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