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