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