Home | History | Annotate | Line # | Download | only in nfs
nfs_vnops.c revision 1.124
      1 /*	$NetBSD: nfs_vnops.c,v 1.124 2000/11/27 08:39:51 chs 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.19 (Berkeley) 7/31/95
     39  */
     40 
     41 /*
     42  * vnode op calls for Sun NFS version 2 and 3
     43  */
     44 
     45 #include "opt_nfs.h"
     46 #include "opt_uvmhist.h"
     47 
     48 #include <sys/param.h>
     49 #include <sys/proc.h>
     50 #include <sys/kernel.h>
     51 #include <sys/systm.h>
     52 #include <sys/resourcevar.h>
     53 #include <sys/proc.h>
     54 #include <sys/mount.h>
     55 #include <sys/buf.h>
     56 #include <sys/malloc.h>
     57 #include <sys/mbuf.h>
     58 #include <sys/namei.h>
     59 #include <sys/vnode.h>
     60 #include <sys/dirent.h>
     61 #include <sys/fcntl.h>
     62 #include <sys/lockf.h>
     63 #include <sys/stat.h>
     64 #include <sys/unistd.h>
     65 
     66 #include <uvm/uvm_extern.h>
     67 #include <uvm/uvm.h>
     68 
     69 #include <miscfs/fifofs/fifo.h>
     70 #include <miscfs/genfs/genfs.h>
     71 #include <miscfs/specfs/specdev.h>
     72 
     73 #include <nfs/rpcv2.h>
     74 #include <nfs/nfsproto.h>
     75 #include <nfs/nfs.h>
     76 #include <nfs/nfsnode.h>
     77 #include <nfs/nfsmount.h>
     78 #include <nfs/xdr_subs.h>
     79 #include <nfs/nfsm_subs.h>
     80 #include <nfs/nqnfs.h>
     81 #include <nfs/nfs_var.h>
     82 
     83 #include <net/if.h>
     84 #include <netinet/in.h>
     85 #include <netinet/in_var.h>
     86 
     87 /* Defs */
     88 #define	TRUE	1
     89 #define	FALSE	0
     90 
     91 /*
     92  * Global vfs data structures for nfs
     93  */
     94 int (**nfsv2_vnodeop_p) __P((void *));
     95 struct vnodeopv_entry_desc nfsv2_vnodeop_entries[] = {
     96 	{ &vop_default_desc, vn_default_error },
     97 	{ &vop_lookup_desc, nfs_lookup },		/* lookup */
     98 	{ &vop_create_desc, nfs_create },		/* create */
     99 	{ &vop_mknod_desc, nfs_mknod },			/* mknod */
    100 	{ &vop_open_desc, nfs_open },			/* open */
    101 	{ &vop_close_desc, nfs_close },			/* close */
    102 	{ &vop_access_desc, nfs_access },		/* access */
    103 	{ &vop_getattr_desc, nfs_getattr },		/* getattr */
    104 	{ &vop_setattr_desc, nfs_setattr },		/* setattr */
    105 	{ &vop_read_desc, nfs_read },			/* read */
    106 	{ &vop_write_desc, nfs_write },			/* write */
    107 	{ &vop_lease_desc, nfs_lease_check },		/* lease */
    108 	{ &vop_fcntl_desc, genfs_fcntl },		/* fcntl */
    109 	{ &vop_ioctl_desc, nfs_ioctl },			/* ioctl */
    110 	{ &vop_poll_desc, nfs_poll },			/* poll */
    111 	{ &vop_revoke_desc, nfs_revoke },		/* revoke */
    112 	{ &vop_mmap_desc, nfs_mmap },			/* mmap */
    113 	{ &vop_fsync_desc, nfs_fsync },			/* fsync */
    114 	{ &vop_seek_desc, nfs_seek },			/* seek */
    115 	{ &vop_remove_desc, nfs_remove },		/* remove */
    116 	{ &vop_link_desc, nfs_link },			/* link */
    117 	{ &vop_rename_desc, nfs_rename },		/* rename */
    118 	{ &vop_mkdir_desc, nfs_mkdir },			/* mkdir */
    119 	{ &vop_rmdir_desc, nfs_rmdir },			/* rmdir */
    120 	{ &vop_symlink_desc, nfs_symlink },		/* symlink */
    121 	{ &vop_readdir_desc, nfs_readdir },		/* readdir */
    122 	{ &vop_readlink_desc, nfs_readlink },		/* readlink */
    123 	{ &vop_abortop_desc, nfs_abortop },		/* abortop */
    124 	{ &vop_inactive_desc, nfs_inactive },		/* inactive */
    125 	{ &vop_reclaim_desc, nfs_reclaim },		/* reclaim */
    126 	{ &vop_lock_desc, nfs_lock },			/* lock */
    127 	{ &vop_unlock_desc, nfs_unlock },		/* unlock */
    128 	{ &vop_bmap_desc, nfs_bmap },			/* bmap */
    129 	{ &vop_strategy_desc, nfs_strategy },		/* strategy */
    130 	{ &vop_print_desc, nfs_print },			/* print */
    131 	{ &vop_islocked_desc, nfs_islocked },		/* islocked */
    132 	{ &vop_pathconf_desc, nfs_pathconf },		/* pathconf */
    133 	{ &vop_advlock_desc, nfs_advlock },		/* advlock */
    134 	{ &vop_blkatoff_desc, nfs_blkatoff },		/* blkatoff */
    135 	{ &vop_valloc_desc, nfs_valloc },		/* valloc */
    136 	{ &vop_reallocblks_desc, nfs_reallocblks },	/* reallocblks */
    137 	{ &vop_vfree_desc, nfs_vfree },			/* vfree */
    138 	{ &vop_truncate_desc, nfs_truncate },		/* truncate */
    139 	{ &vop_update_desc, nfs_update },		/* update */
    140 	{ &vop_bwrite_desc, nfs_bwrite },		/* bwrite */
    141 	{ &vop_getpages_desc, nfs_getpages },		/* getpages */
    142 	{ &vop_putpages_desc, nfs_putpages },		/* putpages */
    143 	{ NULL, NULL }
    144 };
    145 struct vnodeopv_desc nfsv2_vnodeop_opv_desc =
    146 	{ &nfsv2_vnodeop_p, nfsv2_vnodeop_entries };
    147 
    148 /*
    149  * Special device vnode ops
    150  */
    151 int (**spec_nfsv2nodeop_p) __P((void *));
    152 struct vnodeopv_entry_desc spec_nfsv2nodeop_entries[] = {
    153 	{ &vop_default_desc, vn_default_error },
    154 	{ &vop_lookup_desc, spec_lookup },		/* lookup */
    155 	{ &vop_create_desc, spec_create },		/* create */
    156 	{ &vop_mknod_desc, spec_mknod },		/* mknod */
    157 	{ &vop_open_desc, spec_open },			/* open */
    158 	{ &vop_close_desc, nfsspec_close },		/* close */
    159 	{ &vop_access_desc, nfsspec_access },		/* access */
    160 	{ &vop_getattr_desc, nfs_getattr },		/* getattr */
    161 	{ &vop_setattr_desc, nfs_setattr },		/* setattr */
    162 	{ &vop_read_desc, nfsspec_read },		/* read */
    163 	{ &vop_write_desc, nfsspec_write },		/* write */
    164 	{ &vop_lease_desc, spec_lease_check },		/* lease */
    165 	{ &vop_fcntl_desc, genfs_fcntl },		/* fcntl */
    166 	{ &vop_ioctl_desc, spec_ioctl },		/* ioctl */
    167 	{ &vop_poll_desc, spec_poll },			/* poll */
    168 	{ &vop_revoke_desc, spec_revoke },		/* revoke */
    169 	{ &vop_mmap_desc, spec_mmap },			/* mmap */
    170 	{ &vop_fsync_desc, spec_fsync },		/* fsync */
    171 	{ &vop_seek_desc, spec_seek },			/* seek */
    172 	{ &vop_remove_desc, spec_remove },		/* remove */
    173 	{ &vop_link_desc, spec_link },			/* link */
    174 	{ &vop_rename_desc, spec_rename },		/* rename */
    175 	{ &vop_mkdir_desc, spec_mkdir },		/* mkdir */
    176 	{ &vop_rmdir_desc, spec_rmdir },		/* rmdir */
    177 	{ &vop_symlink_desc, spec_symlink },		/* symlink */
    178 	{ &vop_readdir_desc, spec_readdir },		/* readdir */
    179 	{ &vop_readlink_desc, spec_readlink },		/* readlink */
    180 	{ &vop_abortop_desc, spec_abortop },		/* abortop */
    181 	{ &vop_inactive_desc, nfs_inactive },		/* inactive */
    182 	{ &vop_reclaim_desc, nfs_reclaim },		/* reclaim */
    183 	{ &vop_lock_desc, nfs_lock },			/* lock */
    184 	{ &vop_unlock_desc, nfs_unlock },		/* unlock */
    185 	{ &vop_bmap_desc, spec_bmap },			/* bmap */
    186 	{ &vop_strategy_desc, spec_strategy },		/* strategy */
    187 	{ &vop_print_desc, nfs_print },			/* print */
    188 	{ &vop_islocked_desc, nfs_islocked },		/* islocked */
    189 	{ &vop_pathconf_desc, spec_pathconf },		/* pathconf */
    190 	{ &vop_advlock_desc, spec_advlock },		/* advlock */
    191 	{ &vop_blkatoff_desc, spec_blkatoff },		/* blkatoff */
    192 	{ &vop_valloc_desc, spec_valloc },		/* valloc */
    193 	{ &vop_reallocblks_desc, spec_reallocblks },	/* reallocblks */
    194 	{ &vop_vfree_desc, spec_vfree },		/* vfree */
    195 	{ &vop_truncate_desc, spec_truncate },		/* truncate */
    196 	{ &vop_update_desc, nfs_update },		/* update */
    197 	{ &vop_bwrite_desc, vn_bwrite },		/* bwrite */
    198 	{ NULL, NULL }
    199 };
    200 struct vnodeopv_desc spec_nfsv2nodeop_opv_desc =
    201 	{ &spec_nfsv2nodeop_p, spec_nfsv2nodeop_entries };
    202 
    203 int (**fifo_nfsv2nodeop_p) __P((void *));
    204 struct vnodeopv_entry_desc fifo_nfsv2nodeop_entries[] = {
    205 	{ &vop_default_desc, vn_default_error },
    206 	{ &vop_lookup_desc, fifo_lookup },		/* lookup */
    207 	{ &vop_create_desc, fifo_create },		/* create */
    208 	{ &vop_mknod_desc, fifo_mknod },		/* mknod */
    209 	{ &vop_open_desc, fifo_open },			/* open */
    210 	{ &vop_close_desc, nfsfifo_close },		/* close */
    211 	{ &vop_access_desc, nfsspec_access },		/* access */
    212 	{ &vop_getattr_desc, nfs_getattr },		/* getattr */
    213 	{ &vop_setattr_desc, nfs_setattr },		/* setattr */
    214 	{ &vop_read_desc, nfsfifo_read },		/* read */
    215 	{ &vop_write_desc, nfsfifo_write },		/* write */
    216 	{ &vop_lease_desc, fifo_lease_check },		/* lease */
    217 	{ &vop_fcntl_desc, genfs_fcntl },		/* fcntl */
    218 	{ &vop_ioctl_desc, fifo_ioctl },		/* ioctl */
    219 	{ &vop_poll_desc, fifo_poll },			/* poll */
    220 	{ &vop_revoke_desc, fifo_revoke },		/* revoke */
    221 	{ &vop_mmap_desc, fifo_mmap },			/* mmap */
    222 	{ &vop_fsync_desc, nfs_fsync },			/* fsync */
    223 	{ &vop_seek_desc, fifo_seek },			/* seek */
    224 	{ &vop_remove_desc, fifo_remove },		/* remove */
    225 	{ &vop_link_desc, fifo_link },			/* link */
    226 	{ &vop_rename_desc, fifo_rename },		/* rename */
    227 	{ &vop_mkdir_desc, fifo_mkdir },		/* mkdir */
    228 	{ &vop_rmdir_desc, fifo_rmdir },		/* rmdir */
    229 	{ &vop_symlink_desc, fifo_symlink },		/* symlink */
    230 	{ &vop_readdir_desc, fifo_readdir },		/* readdir */
    231 	{ &vop_readlink_desc, fifo_readlink },		/* readlink */
    232 	{ &vop_abortop_desc, fifo_abortop },		/* abortop */
    233 	{ &vop_inactive_desc, nfs_inactive },		/* inactive */
    234 	{ &vop_reclaim_desc, nfs_reclaim },		/* reclaim */
    235 	{ &vop_lock_desc, nfs_lock },			/* lock */
    236 	{ &vop_unlock_desc, nfs_unlock },		/* unlock */
    237 	{ &vop_bmap_desc, fifo_bmap },			/* bmap */
    238 	{ &vop_strategy_desc, genfs_badop },		/* strategy */
    239 	{ &vop_print_desc, nfs_print },			/* print */
    240 	{ &vop_islocked_desc, nfs_islocked },		/* islocked */
    241 	{ &vop_pathconf_desc, fifo_pathconf },		/* pathconf */
    242 	{ &vop_advlock_desc, fifo_advlock },		/* advlock */
    243 	{ &vop_blkatoff_desc, fifo_blkatoff },		/* blkatoff */
    244 	{ &vop_valloc_desc, fifo_valloc },		/* valloc */
    245 	{ &vop_reallocblks_desc, fifo_reallocblks },	/* reallocblks */
    246 	{ &vop_vfree_desc, fifo_vfree },		/* vfree */
    247 	{ &vop_truncate_desc, fifo_truncate },		/* truncate */
    248 	{ &vop_update_desc, nfs_update },		/* update */
    249 	{ &vop_bwrite_desc, vn_bwrite },		/* bwrite */
    250 	{ NULL, NULL }
    251 };
    252 struct vnodeopv_desc fifo_nfsv2nodeop_opv_desc =
    253 	{ &fifo_nfsv2nodeop_p, fifo_nfsv2nodeop_entries };
    254 
    255 /*
    256  * Global variables
    257  */
    258 extern u_int32_t nfs_true, nfs_false;
    259 extern u_int32_t nfs_xdrneg1;
    260 extern struct nfsstats nfsstats;
    261 extern nfstype nfsv3_type[9];
    262 struct proc *nfs_iodwant[NFS_MAXASYNCDAEMON];
    263 struct nfsmount *nfs_iodmount[NFS_MAXASYNCDAEMON];
    264 int nfs_numasync = 0;
    265 #define	DIRHDSIZ	(sizeof (struct dirent) - (MAXNAMLEN + 1))
    266 
    267 /*
    268  * nfs null call from vfs.
    269  */
    270 int
    271 nfs_null(vp, cred, procp)
    272 	struct vnode *vp;
    273 	struct ucred *cred;
    274 	struct proc *procp;
    275 {
    276 	caddr_t bpos, dpos;
    277 	int error = 0;
    278 	struct mbuf *mreq, *mrep, *md, *mb;
    279 
    280 	nfsm_reqhead(vp, NFSPROC_NULL, 0);
    281 	nfsm_request(vp, NFSPROC_NULL, procp, cred);
    282 	nfsm_reqdone;
    283 	return (error);
    284 }
    285 
    286 /*
    287  * nfs access vnode op.
    288  * For nfs version 2, just return ok. File accesses may fail later.
    289  * For nfs version 3, use the access rpc to check accessibility. If file modes
    290  * are changed on the server, accesses might still fail later.
    291  */
    292 int
    293 nfs_access(v)
    294 	void *v;
    295 {
    296 	struct vop_access_args /* {
    297 		struct vnode *a_vp;
    298 		int  a_mode;
    299 		struct ucred *a_cred;
    300 		struct proc *a_p;
    301 	} */ *ap = v;
    302 	struct vnode *vp = ap->a_vp;
    303 	u_int32_t *tl;
    304 	caddr_t cp;
    305 	int32_t t1, t2;
    306 	caddr_t bpos, dpos, cp2;
    307 	int error = 0, attrflag, cachevalid;
    308 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
    309 	u_int32_t mode, rmode;
    310 	const int v3 = NFS_ISV3(vp);
    311 	struct nfsnode *np = VTONFS(vp);
    312 
    313 	cachevalid = (np->n_accstamp != -1 &&
    314 	    (time.tv_sec - np->n_accstamp) < NFS_ATTRTIMEO(np) &&
    315 	    np->n_accuid == ap->a_cred->cr_uid);
    316 
    317 	/*
    318 	 * Check access cache first. If this request has been made for this
    319 	 * uid shortly before, use the cached result.
    320 	 */
    321 	if (cachevalid) {
    322 		if (!np->n_accerror) {
    323 			if  ((np->n_accmode & ap->a_mode) == ap->a_mode)
    324 				return np->n_accerror;
    325 		} else if ((np->n_accmode & ap->a_mode) == np->n_accmode)
    326 			return np->n_accerror;
    327 	}
    328 
    329 	/*
    330 	 * For nfs v3, do an access rpc, otherwise you are stuck emulating
    331 	 * ufs_access() locally using the vattr. This may not be correct,
    332 	 * since the server may apply other access criteria such as
    333 	 * client uid-->server uid mapping that we do not know about, but
    334 	 * this is better than just returning anything that is lying about
    335 	 * in the cache.
    336 	 */
    337 	if (v3) {
    338 		nfsstats.rpccnt[NFSPROC_ACCESS]++;
    339 		nfsm_reqhead(vp, NFSPROC_ACCESS, NFSX_FH(v3) + NFSX_UNSIGNED);
    340 		nfsm_fhtom(vp, v3);
    341 		nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
    342 		if (ap->a_mode & VREAD)
    343 			mode = NFSV3ACCESS_READ;
    344 		else
    345 			mode = 0;
    346 		if (vp->v_type != VDIR) {
    347 			if (ap->a_mode & VWRITE)
    348 				mode |= (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND);
    349 			if (ap->a_mode & VEXEC)
    350 				mode |= NFSV3ACCESS_EXECUTE;
    351 		} else {
    352 			if (ap->a_mode & VWRITE)
    353 				mode |= (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND |
    354 					 NFSV3ACCESS_DELETE);
    355 			if (ap->a_mode & VEXEC)
    356 				mode |= NFSV3ACCESS_LOOKUP;
    357 		}
    358 		*tl = txdr_unsigned(mode);
    359 		nfsm_request(vp, NFSPROC_ACCESS, ap->a_p, ap->a_cred);
    360 		nfsm_postop_attr(vp, attrflag);
    361 		if (!error) {
    362 			nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
    363 			rmode = fxdr_unsigned(u_int32_t, *tl);
    364 			/*
    365 			 * The NFS V3 spec does not clarify whether or not
    366 			 * the returned access bits can be a superset of
    367 			 * the ones requested, so...
    368 			 */
    369 			if ((rmode & mode) != mode)
    370 				error = EACCES;
    371 		}
    372 		nfsm_reqdone;
    373 	} else
    374 		return (nfsspec_access(ap));
    375 	/*
    376 	 * Disallow write attempts on filesystems mounted read-only;
    377 	 * unless the file is a socket, fifo, or a block or character
    378 	 * device resident on the filesystem.
    379 	 */
    380 	if (!error && (ap->a_mode & VWRITE) &&
    381 	    (vp->v_mount->mnt_flag & MNT_RDONLY)) {
    382 		switch (vp->v_type) {
    383 		case VREG:
    384 		case VDIR:
    385 		case VLNK:
    386 			error = EROFS;
    387 		default:
    388 			break;
    389 		}
    390 	}
    391 
    392 	if (!error || error == EACCES) {
    393 		/*
    394 		 * If we got the same result as for a previous,
    395 		 * different request, OR it in. Don't update
    396 		 * the timestamp in that case.
    397 		 */
    398 		if (cachevalid && error == np->n_accerror) {
    399 			if (!error)
    400 				np->n_accmode |= ap->a_mode;
    401 			else if ((np->n_accmode & ap->a_mode) == ap->a_mode)
    402 				np->n_accmode = ap->a_mode;
    403 		} else {
    404 			np->n_accstamp = time.tv_sec;
    405 			np->n_accuid = ap->a_cred->cr_uid;
    406 			np->n_accmode = ap->a_mode;
    407 			np->n_accerror = error;
    408 		}
    409 	}
    410 
    411 	return (error);
    412 }
    413 
    414 /*
    415  * nfs open vnode op
    416  * Check to see if the type is ok
    417  * and that deletion is not in progress.
    418  * For paged in text files, you will need to flush the page cache
    419  * if consistency is lost.
    420  */
    421 /* ARGSUSED */
    422 int
    423 nfs_open(v)
    424 	void *v;
    425 {
    426 	struct vop_open_args /* {
    427 		struct vnode *a_vp;
    428 		int  a_mode;
    429 		struct ucred *a_cred;
    430 		struct proc *a_p;
    431 	} */ *ap = v;
    432 	struct vnode *vp = ap->a_vp;
    433 	struct nfsnode *np = VTONFS(vp);
    434 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
    435 	struct vattr vattr;
    436 	int error;
    437 
    438 	if (vp->v_type != VREG && vp->v_type != VDIR && vp->v_type != VLNK) {
    439 		return (EACCES);
    440 	}
    441 
    442 #ifndef NFS_V2_ONLY
    443 	/*
    444 	 * Get a valid lease. If cached data is stale, flush it.
    445 	 */
    446 	if (nmp->nm_flag & NFSMNT_NQNFS) {
    447 		if (NQNFS_CKINVALID(vp, np, ND_READ)) {
    448 		    do {
    449 			error = nqnfs_getlease(vp, ND_READ, ap->a_cred,
    450 			    ap->a_p);
    451 		    } while (error == NQNFS_EXPIRED);
    452 		    if (error)
    453 			return (error);
    454 		    if (np->n_lrev != np->n_brev ||
    455 			(np->n_flag & NQNFSNONCACHE)) {
    456 			if ((error = nfs_vinvalbuf(vp, V_SAVE, ap->a_cred,
    457 				ap->a_p, 1)) == EINTR)
    458 				return (error);
    459 			np->n_brev = np->n_lrev;
    460 		    }
    461 		}
    462 	} else
    463 #endif
    464 	{
    465 		if (np->n_flag & NMODIFIED) {
    466 			if ((error = nfs_vinvalbuf(vp, V_SAVE, ap->a_cred,
    467 				ap->a_p, 1)) == EINTR)
    468 				return (error);
    469 			np->n_attrstamp = 0;
    470 			if (vp->v_type == VDIR) {
    471 				nfs_invaldircache(vp, 0);
    472 				np->n_direofoffset = 0;
    473 			}
    474 			error = VOP_GETATTR(vp, &vattr, ap->a_cred, ap->a_p);
    475 			if (error)
    476 				return (error);
    477 			np->n_mtime = vattr.va_mtime.tv_sec;
    478 		} else {
    479 			error = VOP_GETATTR(vp, &vattr, ap->a_cred, ap->a_p);
    480 			if (error)
    481 				return (error);
    482 			if (np->n_mtime != vattr.va_mtime.tv_sec) {
    483 				if (vp->v_type == VDIR) {
    484 					nfs_invaldircache(vp, 0);
    485 					np->n_direofoffset = 0;
    486 				}
    487 				if ((error = nfs_vinvalbuf(vp, V_SAVE,
    488 					ap->a_cred, ap->a_p, 1)) == EINTR)
    489 					return (error);
    490 				np->n_mtime = vattr.va_mtime.tv_sec;
    491 			}
    492 		}
    493 	}
    494 	if ((nmp->nm_flag & NFSMNT_NQNFS) == 0)
    495 		np->n_attrstamp = 0; /* For Open/Close consistency */
    496 	return (0);
    497 }
    498 
    499 /*
    500  * nfs close vnode op
    501  * What an NFS client should do upon close after writing is a debatable issue.
    502  * Most NFS clients push delayed writes to the server upon close, basically for
    503  * two reasons:
    504  * 1 - So that any write errors may be reported back to the client process
    505  *     doing the close system call. By far the two most likely errors are
    506  *     NFSERR_NOSPC and NFSERR_DQUOT to indicate space allocation failure.
    507  * 2 - To put a worst case upper bound on cache inconsistency between
    508  *     multiple clients for the file.
    509  * There is also a consistency problem for Version 2 of the protocol w.r.t.
    510  * not being able to tell if other clients are writing a file concurrently,
    511  * since there is no way of knowing if the changed modify time in the reply
    512  * is only due to the write for this client.
    513  * (NFS Version 3 provides weak cache consistency data in the reply that
    514  *  should be sufficient to detect and handle this case.)
    515  *
    516  * The current code does the following:
    517  * for NFS Version 2 - play it safe and flush/invalidate all dirty buffers
    518  * for NFS Version 3 - flush dirty buffers to the server but don't invalidate
    519  *                     or commit them (this satisfies 1 and 2 except for the
    520  *                     case where the server crashes after this close but
    521  *                     before the commit RPC, which is felt to be "good
    522  *                     enough". Changing the last argument to nfs_flush() to
    523  *                     a 1 would force a commit operation, if it is felt a
    524  *                     commit is necessary now.
    525  * for NQNFS         - do nothing now, since 2 is dealt with via leases and
    526  *                     1 should be dealt with via an fsync() system call for
    527  *                     cases where write errors are important.
    528  */
    529 /* ARGSUSED */
    530 int
    531 nfs_close(v)
    532 	void *v;
    533 {
    534 	struct vop_close_args /* {
    535 		struct vnodeop_desc *a_desc;
    536 		struct vnode *a_vp;
    537 		int  a_fflag;
    538 		struct ucred *a_cred;
    539 		struct proc *a_p;
    540 	} */ *ap = v;
    541 	struct vnode *vp = ap->a_vp;
    542 	struct nfsnode *np = VTONFS(vp);
    543 	int error = 0;
    544 	UVMHIST_FUNC("nfs_close"); UVMHIST_CALLED(ubchist);
    545 
    546 	if (vp->v_type == VREG) {
    547 	    if ((VFSTONFS(vp->v_mount)->nm_flag & NFSMNT_NQNFS) == 0 &&
    548 		(np->n_flag & NMODIFIED)) {
    549 		if (NFS_ISV3(vp)) {
    550 		    error = nfs_flush(vp, ap->a_cred, MNT_WAIT, ap->a_p, 0);
    551 		    np->n_flag &= ~NMODIFIED;
    552 		} else
    553 		    error = nfs_vinvalbuf(vp, V_SAVE, ap->a_cred, ap->a_p, 1);
    554 		np->n_attrstamp = 0;
    555 	    }
    556 	    if (np->n_flag & NWRITEERR) {
    557 		np->n_flag &= ~NWRITEERR;
    558 		error = np->n_error;
    559 	    }
    560 	}
    561 	UVMHIST_LOG(ubchist, "returning %d", error,0,0,0);
    562 	return (error);
    563 }
    564 
    565 /*
    566  * nfs getattr call from vfs.
    567  */
    568 int
    569 nfs_getattr(v)
    570 	void *v;
    571 {
    572 	struct vop_getattr_args /* {
    573 		struct vnode *a_vp;
    574 		struct vattr *a_vap;
    575 		struct ucred *a_cred;
    576 		struct proc *a_p;
    577 	} */ *ap = v;
    578 	struct vnode *vp = ap->a_vp;
    579 	struct nfsnode *np = VTONFS(vp);
    580 	caddr_t cp;
    581 	u_int32_t *tl;
    582 	int32_t t1, t2;
    583 	caddr_t bpos, dpos;
    584 	int error = 0;
    585 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
    586 	const int v3 = NFS_ISV3(vp);
    587 
    588 	/*
    589 	 * Update local times for special files.
    590 	 */
    591 	if (np->n_flag & (NACC | NUPD))
    592 		np->n_flag |= NCHG;
    593 	/*
    594 	 * First look in the cache.
    595 	 */
    596 	if (nfs_getattrcache(vp, ap->a_vap) == 0)
    597 		return (0);
    598 	nfsstats.rpccnt[NFSPROC_GETATTR]++;
    599 	nfsm_reqhead(vp, NFSPROC_GETATTR, NFSX_FH(v3));
    600 	nfsm_fhtom(vp, v3);
    601 	nfsm_request(vp, NFSPROC_GETATTR, ap->a_p, ap->a_cred);
    602 	if (!error) {
    603 		nfsm_loadattr(vp, ap->a_vap);
    604 		if (vp->v_type == VDIR &&
    605 		    ap->a_vap->va_blocksize < NFS_DIRFRAGSIZ)
    606 			ap->a_vap->va_blocksize = NFS_DIRFRAGSIZ;
    607 	}
    608 	nfsm_reqdone;
    609 	return (error);
    610 }
    611 
    612 /*
    613  * nfs setattr call.
    614  */
    615 int
    616 nfs_setattr(v)
    617 	void *v;
    618 {
    619 	struct vop_setattr_args /* {
    620 		struct vnodeop_desc *a_desc;
    621 		struct vnode *a_vp;
    622 		struct vattr *a_vap;
    623 		struct ucred *a_cred;
    624 		struct proc *a_p;
    625 	} */ *ap = v;
    626 	struct vnode *vp = ap->a_vp;
    627 	struct nfsnode *np = VTONFS(vp);
    628 	struct vattr *vap = ap->a_vap;
    629 	int error = 0;
    630 	u_quad_t tsize = 0;
    631 
    632 	/*
    633 	 * Setting of flags is not supported.
    634 	 */
    635 	if (vap->va_flags != VNOVAL)
    636 		return (EOPNOTSUPP);
    637 
    638 	/*
    639 	 * Disallow write attempts if the filesystem is mounted read-only.
    640 	 */
    641   	if ((vap->va_uid != (uid_t)VNOVAL ||
    642 	    vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_sec != VNOVAL ||
    643 	    vap->va_mtime.tv_sec != VNOVAL || vap->va_mode != (mode_t)VNOVAL) &&
    644 	    (vp->v_mount->mnt_flag & MNT_RDONLY))
    645 		return (EROFS);
    646 	if (vap->va_size != VNOVAL) {
    647  		switch (vp->v_type) {
    648  		case VDIR:
    649  			return (EISDIR);
    650  		case VCHR:
    651  		case VBLK:
    652  		case VSOCK:
    653  		case VFIFO:
    654 			if (vap->va_mtime.tv_sec == VNOVAL &&
    655 			    vap->va_atime.tv_sec == VNOVAL &&
    656 			    vap->va_mode == (mode_t)VNOVAL &&
    657 			    vap->va_uid == (uid_t)VNOVAL &&
    658 			    vap->va_gid == (gid_t)VNOVAL)
    659 				return (0);
    660  			vap->va_size = VNOVAL;
    661  			break;
    662  		default:
    663 			/*
    664 			 * Disallow write attempts if the filesystem is
    665 			 * mounted read-only.
    666 			 */
    667 			if (vp->v_mount->mnt_flag & MNT_RDONLY)
    668 				return (EROFS);
    669  			uvm_vnp_setsize(vp, vap->va_size);
    670  			if (vap->va_size == 0)
    671  				error = nfs_vinvalbuf(vp, 0,
    672  				     ap->a_cred, ap->a_p, 1);
    673 			else
    674 				error = nfs_vinvalbuf(vp, V_SAVE,
    675 				     ap->a_cred, ap->a_p, 1);
    676 			if (error) {
    677 				uvm_vnp_setsize(vp, np->n_size);
    678 				return (error);
    679 			}
    680  			tsize = np->n_size;
    681  			np->n_size = np->n_vattr->va_size = vap->va_size;
    682   		}
    683   	} else if ((vap->va_mtime.tv_sec != VNOVAL ||
    684 		vap->va_atime.tv_sec != VNOVAL) &&
    685 		vp->v_type == VREG &&
    686   		(error = nfs_vinvalbuf(vp, V_SAVE, ap->a_cred,
    687 		 ap->a_p, 1)) == EINTR)
    688 		return (error);
    689 	error = nfs_setattrrpc(vp, vap, ap->a_cred, ap->a_p);
    690 	if (error && vap->va_size != VNOVAL) {
    691 		np->n_size = np->n_vattr->va_size = tsize;
    692 		uvm_vnp_setsize(vp, np->n_size);
    693 	}
    694 	return (error);
    695 }
    696 
    697 /*
    698  * Do an nfs setattr rpc.
    699  */
    700 int
    701 nfs_setattrrpc(vp, vap, cred, procp)
    702 	struct vnode *vp;
    703 	struct vattr *vap;
    704 	struct ucred *cred;
    705 	struct proc *procp;
    706 {
    707 	struct nfsv2_sattr *sp;
    708 	caddr_t cp;
    709 	int32_t t1, t2;
    710 	caddr_t bpos, dpos, cp2;
    711 	u_int32_t *tl;
    712 	int error = 0, wccflag = NFSV3_WCCRATTR;
    713 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
    714 	const int v3 = NFS_ISV3(vp);
    715 
    716 	nfsstats.rpccnt[NFSPROC_SETATTR]++;
    717 	nfsm_reqhead(vp, NFSPROC_SETATTR, NFSX_FH(v3) + NFSX_SATTR(v3));
    718 	nfsm_fhtom(vp, v3);
    719 	if (v3) {
    720 		nfsm_v3attrbuild(vap, TRUE);
    721 		nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
    722 		*tl = nfs_false;
    723 	} else {
    724 		nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
    725 		if (vap->va_mode == (mode_t)VNOVAL)
    726 			sp->sa_mode = nfs_xdrneg1;
    727 		else
    728 			sp->sa_mode = vtonfsv2_mode(vp->v_type, vap->va_mode);
    729 		if (vap->va_uid == (uid_t)VNOVAL)
    730 			sp->sa_uid = nfs_xdrneg1;
    731 		else
    732 			sp->sa_uid = txdr_unsigned(vap->va_uid);
    733 		if (vap->va_gid == (gid_t)VNOVAL)
    734 			sp->sa_gid = nfs_xdrneg1;
    735 		else
    736 			sp->sa_gid = txdr_unsigned(vap->va_gid);
    737 		sp->sa_size = txdr_unsigned(vap->va_size);
    738 		txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
    739 		txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
    740 	}
    741 	nfsm_request(vp, NFSPROC_SETATTR, procp, cred);
    742 	if (v3) {
    743 		nfsm_wcc_data(vp, wccflag);
    744 	} else
    745 		nfsm_loadattr(vp, (struct vattr *)0);
    746 	nfsm_reqdone;
    747 	return (error);
    748 }
    749 
    750 /*
    751  * nfs lookup call, one step at a time...
    752  * First look in cache
    753  * If not found, unlock the directory nfsnode and do the rpc
    754  */
    755 int
    756 nfs_lookup(v)
    757 	void *v;
    758 {
    759 	struct vop_lookup_args /* {
    760 		struct vnodeop_desc *a_desc;
    761 		struct vnode *a_dvp;
    762 		struct vnode **a_vpp;
    763 		struct componentname *a_cnp;
    764 	} */ *ap = v;
    765 	struct componentname *cnp = ap->a_cnp;
    766 	struct vnode *dvp = ap->a_dvp;
    767 	struct vnode **vpp = ap->a_vpp;
    768 	int flags;
    769 	struct vnode *newvp;
    770 	u_int32_t *tl;
    771 	caddr_t cp;
    772 	int32_t t1, t2;
    773 	struct nfsmount *nmp;
    774 	caddr_t bpos, dpos, cp2;
    775 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
    776 	long len;
    777 	nfsfh_t *fhp;
    778 	struct nfsnode *np;
    779 	int lockparent, wantparent, error = 0, attrflag, fhsize;
    780 	const int v3 = NFS_ISV3(dvp);
    781 	cnp->cn_flags &= ~PDIRUNLOCK;
    782 	flags = cnp->cn_flags;
    783 
    784 	*vpp = NULLVP;
    785 	if ((flags & ISLASTCN) && (dvp->v_mount->mnt_flag & MNT_RDONLY) &&
    786 	    (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
    787 		return (EROFS);
    788 	if (dvp->v_type != VDIR)
    789 		return (ENOTDIR);
    790 
    791 	lockparent = flags & LOCKPARENT;
    792 	wantparent = flags & (LOCKPARENT|WANTPARENT);
    793 	nmp = VFSTONFS(dvp->v_mount);
    794 	np = VTONFS(dvp);
    795 
    796 	/*
    797 	 * Before tediously performing a linear scan of the directory,
    798 	 * check the name cache to see if the directory/name pair
    799 	 * we are looking for is known already.
    800 	 * If the directory/name pair is found in the name cache,
    801 	 * we have to ensure the directory has not changed from
    802 	 * the time the cache entry has been created. If it has,
    803 	 * the cache entry has to be ignored
    804 	 */
    805 	if ((error = cache_lookup(dvp, vpp, cnp)) >= 0) {
    806 		struct vattr vattr;
    807 		int err2;
    808 
    809 		if (error && error != ENOENT) {
    810 			*vpp = NULLVP;
    811 			return (error);
    812 		}
    813 
    814 		err2 = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred, cnp->cn_proc);
    815 		if (err2) {
    816 			*vpp = NULLVP;
    817 			return (err2);
    818 		}
    819 
    820 		if (error == ENOENT) {
    821 			if (!VOP_GETATTR(dvp, &vattr, cnp->cn_cred,
    822 			    cnp->cn_proc) && vattr.va_mtime.tv_sec ==
    823 			    VTONFS(dvp)->n_nctime)
    824 				return (ENOENT);
    825 			cache_purge(dvp);
    826 			np->n_nctime = 0;
    827 			goto dorpc;
    828 		} else if (error > 0) {
    829 			*vpp = NULLVP;
    830 			return error;
    831 		}
    832 
    833 		newvp = *vpp;
    834 		if (!VOP_GETATTR(newvp, &vattr, cnp->cn_cred, cnp->cn_proc)
    835 			&& vattr.va_ctime.tv_sec == VTONFS(newvp)->n_ctime)
    836 		{
    837 			nfsstats.lookupcache_hits++;
    838 			if (cnp->cn_nameiop != LOOKUP && (flags & ISLASTCN))
    839 				cnp->cn_flags |= SAVENAME;
    840 			return (0);
    841 		}
    842 		/* XXX cache_lookup() returns the vnode locked; if nfs
    843 		 * would have real vnode locking, we should call VOP_UNLOCK()
    844 		 * here; as it has no real locking, don't bother to do
    845 		 * anything */
    846 		/* VOP_UNLOCK(newvp, 0); */
    847 		cache_purge(newvp);
    848 		vrele(newvp);
    849 		*vpp = NULLVP;
    850 	}
    851 dorpc:
    852 	error = 0;
    853 	newvp = NULLVP;
    854 	nfsstats.lookupcache_misses++;
    855 	nfsstats.rpccnt[NFSPROC_LOOKUP]++;
    856 	len = cnp->cn_namelen;
    857 	nfsm_reqhead(dvp, NFSPROC_LOOKUP,
    858 		NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len));
    859 	nfsm_fhtom(dvp, v3);
    860 	nfsm_strtom(cnp->cn_nameptr, len, NFS_MAXNAMLEN);
    861 	nfsm_request(dvp, NFSPROC_LOOKUP, cnp->cn_proc, cnp->cn_cred);
    862 	if (error) {
    863 		nfsm_postop_attr(dvp, attrflag);
    864 		m_freem(mrep);
    865 		goto nfsmout;
    866 	}
    867 	nfsm_getfh(fhp, fhsize, v3);
    868 
    869 	/*
    870 	 * Handle RENAME case...
    871 	 */
    872 	if (cnp->cn_nameiop == RENAME && wantparent && (flags & ISLASTCN)) {
    873 		if (NFS_CMPFH(np, fhp, fhsize)) {
    874 			m_freem(mrep);
    875 			return (EISDIR);
    876 		}
    877 		error = nfs_nget(dvp->v_mount, fhp, fhsize, &np);
    878 		if (error) {
    879 			m_freem(mrep);
    880 			return (error);
    881 		}
    882 		newvp = NFSTOV(np);
    883 		if (v3) {
    884 			nfsm_postop_attr(newvp, attrflag);
    885 			nfsm_postop_attr(dvp, attrflag);
    886 		} else
    887 			nfsm_loadattr(newvp, (struct vattr *)0);
    888 		*vpp = newvp;
    889 		m_freem(mrep);
    890 		cnp->cn_flags |= SAVENAME;
    891 		if (!lockparent || !(flags & ISLASTCN))
    892 			cnp->cn_flags |= PDIRUNLOCK;
    893 		return (0);
    894 	}
    895 
    896 	if (NFS_CMPFH(np, fhp, fhsize)) {
    897 		VREF(dvp);
    898 		newvp = dvp;
    899 	} else {
    900 		error = nfs_nget(dvp->v_mount, fhp, fhsize, &np);
    901 		if (error) {
    902 			m_freem(mrep);
    903 			return (error);
    904 		}
    905 		newvp = NFSTOV(np);
    906 	}
    907 	if (v3) {
    908 		nfsm_postop_attr(newvp, attrflag);
    909 		nfsm_postop_attr(dvp, attrflag);
    910 	} else
    911 		nfsm_loadattr(newvp, (struct vattr *)0);
    912 	if (cnp->cn_nameiop != LOOKUP && (flags & ISLASTCN))
    913 		cnp->cn_flags |= SAVENAME;
    914 	if ((cnp->cn_flags & MAKEENTRY) &&
    915 	    (cnp->cn_nameiop != DELETE || !(flags & ISLASTCN))) {
    916 		np->n_ctime = np->n_vattr->va_ctime.tv_sec;
    917 		cache_enter(dvp, newvp, cnp);
    918 	}
    919 	*vpp = newvp;
    920 	nfsm_reqdone;
    921 	if (error) {
    922 		if (error == ENOENT && (cnp->cn_flags & MAKEENTRY) &&
    923 		    cnp->cn_nameiop != CREATE) {
    924 			if (VTONFS(dvp)->n_nctime == 0)
    925 				VTONFS(dvp)->n_nctime =
    926 				    VTONFS(dvp)->n_vattr->va_mtime.tv_sec;
    927 			cache_enter(dvp, NULL, cnp);
    928 		}
    929 		if (newvp != NULLVP)
    930 			vrele(newvp);
    931 		if ((cnp->cn_nameiop == CREATE || cnp->cn_nameiop == RENAME) &&
    932 		    (flags & ISLASTCN) && error == ENOENT) {
    933 			if (dvp->v_mount->mnt_flag & MNT_RDONLY)
    934 				error = EROFS;
    935 			else
    936 				error = EJUSTRETURN;
    937 		}
    938 		if (cnp->cn_nameiop != LOOKUP && (flags & ISLASTCN))
    939 			cnp->cn_flags |= SAVENAME;
    940 	} else {
    941 		if (!lockparent || !(flags & ISLASTCN))
    942 			cnp->cn_flags |= PDIRUNLOCK;
    943 	}
    944 	return (error);
    945 }
    946 
    947 /*
    948  * nfs read call.
    949  * Just call nfs_bioread() to do the work.
    950  */
    951 int
    952 nfs_read(v)
    953 	void *v;
    954 {
    955 	struct vop_read_args /* {
    956 		struct vnode *a_vp;
    957 		struct uio *a_uio;
    958 		int  a_ioflag;
    959 		struct ucred *a_cred;
    960 	} */ *ap = v;
    961 	struct vnode *vp = ap->a_vp;
    962 
    963 	if (vp->v_type != VREG)
    964 		return (EPERM);
    965 	return (nfs_bioread(vp, ap->a_uio, ap->a_ioflag, ap->a_cred, 0));
    966 }
    967 
    968 /*
    969  * nfs readlink call
    970  */
    971 int
    972 nfs_readlink(v)
    973 	void *v;
    974 {
    975 	struct vop_readlink_args /* {
    976 		struct vnode *a_vp;
    977 		struct uio *a_uio;
    978 		struct ucred *a_cred;
    979 	} */ *ap = v;
    980 	struct vnode *vp = ap->a_vp;
    981 
    982 	if (vp->v_type != VLNK)
    983 		return (EPERM);
    984 	return (nfs_bioread(vp, ap->a_uio, 0, ap->a_cred, 0));
    985 }
    986 
    987 /*
    988  * Do a readlink rpc.
    989  * Called by nfs_doio() from below the buffer cache.
    990  */
    991 int
    992 nfs_readlinkrpc(vp, uiop, cred)
    993 	struct vnode *vp;
    994 	struct uio *uiop;
    995 	struct ucred *cred;
    996 {
    997 	u_int32_t *tl;
    998 	caddr_t cp;
    999 	int32_t t1, t2;
   1000 	caddr_t bpos, dpos, cp2;
   1001 	int error = 0, len, attrflag;
   1002 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
   1003 	const int v3 = NFS_ISV3(vp);
   1004 
   1005 	nfsstats.rpccnt[NFSPROC_READLINK]++;
   1006 	nfsm_reqhead(vp, NFSPROC_READLINK, NFSX_FH(v3));
   1007 	nfsm_fhtom(vp, v3);
   1008 	nfsm_request(vp, NFSPROC_READLINK, uiop->uio_procp, cred);
   1009 	if (v3)
   1010 		nfsm_postop_attr(vp, attrflag);
   1011 	if (!error) {
   1012 		nfsm_strsiz(len, NFS_MAXPATHLEN);
   1013 		nfsm_mtouio(uiop, len);
   1014 	}
   1015 	nfsm_reqdone;
   1016 	return (error);
   1017 }
   1018 
   1019 /*
   1020  * nfs read rpc call
   1021  * Ditto above
   1022  */
   1023 int
   1024 nfs_readrpc(vp, uiop)
   1025 	struct vnode *vp;
   1026 	struct uio *uiop;
   1027 {
   1028 	u_int32_t *tl;
   1029 	caddr_t cp;
   1030 	int32_t t1, t2;
   1031 	caddr_t bpos, dpos, cp2;
   1032 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
   1033 	struct nfsmount *nmp;
   1034 	int error = 0, len, retlen, tsiz, eof, attrflag;
   1035 	const int v3 = NFS_ISV3(vp);
   1036 
   1037 #ifndef nolint
   1038 	eof = 0;
   1039 #endif
   1040 	nmp = VFSTONFS(vp->v_mount);
   1041 	tsiz = uiop->uio_resid;
   1042 	if (uiop->uio_offset + tsiz > nmp->nm_maxfilesize)
   1043 		return (EFBIG);
   1044 	while (tsiz > 0) {
   1045 		nfsstats.rpccnt[NFSPROC_READ]++;
   1046 		len = (tsiz > nmp->nm_rsize) ? nmp->nm_rsize : tsiz;
   1047 		nfsm_reqhead(vp, NFSPROC_READ, NFSX_FH(v3) + NFSX_UNSIGNED * 3);
   1048 		nfsm_fhtom(vp, v3);
   1049 		nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED * 3);
   1050 		if (v3) {
   1051 			txdr_hyper(uiop->uio_offset, tl);
   1052 			*(tl + 2) = txdr_unsigned(len);
   1053 		} else {
   1054 			*tl++ = txdr_unsigned(uiop->uio_offset);
   1055 			*tl++ = txdr_unsigned(len);
   1056 			*tl = 0;
   1057 		}
   1058 		nfsm_request(vp, NFSPROC_READ, uiop->uio_procp,
   1059 			     VTONFS(vp)->n_rcred);
   1060 		if (v3) {
   1061 			nfsm_postop_attr(vp, attrflag);
   1062 			if (error) {
   1063 				m_freem(mrep);
   1064 				goto nfsmout;
   1065 			}
   1066 			nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
   1067 			eof = fxdr_unsigned(int, *(tl + 1));
   1068 		} else
   1069 			nfsm_loadattr(vp, (struct vattr *)0);
   1070 		nfsm_strsiz(retlen, nmp->nm_rsize);
   1071 		nfsm_mtouio(uiop, retlen);
   1072 		m_freem(mrep);
   1073 		tsiz -= retlen;
   1074 		if (v3) {
   1075 			if (eof || retlen == 0)
   1076 				tsiz = 0;
   1077 		} else if (retlen < len)
   1078 			tsiz = 0;
   1079 	}
   1080 nfsmout:
   1081 	return (error);
   1082 }
   1083 
   1084 /*
   1085  * nfs write call
   1086  */
   1087 int
   1088 nfs_writerpc(vp, uiop, iomode, must_commit)
   1089 	struct vnode *vp;
   1090 	struct uio *uiop;
   1091 	int *iomode, *must_commit;
   1092 {
   1093 	u_int32_t *tl;
   1094 	caddr_t cp;
   1095 	int32_t t1, t2, backup;
   1096 	caddr_t bpos, dpos, cp2;
   1097 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
   1098 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
   1099 	int error = 0, len, tsiz, wccflag = NFSV3_WCCRATTR, rlen, commit;
   1100 	const int v3 = NFS_ISV3(vp);
   1101 	int committed = NFSV3WRITE_FILESYNC;
   1102 
   1103 #ifndef DIAGNOSTIC
   1104 	if (uiop->uio_iovcnt != 1)
   1105 		panic("nfs: writerpc iovcnt > 1");
   1106 #endif
   1107 	*must_commit = 0;
   1108 	tsiz = uiop->uio_resid;
   1109 	if (uiop->uio_offset + tsiz > nmp->nm_maxfilesize)
   1110 		return (EFBIG);
   1111 	while (tsiz > 0) {
   1112 		nfsstats.rpccnt[NFSPROC_WRITE]++;
   1113 		len = min(tsiz, nmp->nm_wsize);
   1114 		nfsm_reqhead(vp, NFSPROC_WRITE,
   1115 			NFSX_FH(v3) + 5 * NFSX_UNSIGNED + nfsm_rndup(len));
   1116 		nfsm_fhtom(vp, v3);
   1117 		if (v3) {
   1118 			nfsm_build(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
   1119 			txdr_hyper(uiop->uio_offset, tl);
   1120 			tl += 2;
   1121 			*tl++ = txdr_unsigned(len);
   1122 			*tl++ = txdr_unsigned(*iomode);
   1123 			*tl = txdr_unsigned(len);
   1124 		} else {
   1125 			u_int32_t x;
   1126 
   1127 			nfsm_build(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
   1128 			/* Set both "begin" and "current" to non-garbage. */
   1129 			x = txdr_unsigned((u_int32_t)uiop->uio_offset);
   1130 			*tl++ = x;      /* "begin offset" */
   1131 			*tl++ = x;      /* "current offset" */
   1132 			x = txdr_unsigned(len);
   1133 			*tl++ = x;      /* total to this offset */
   1134 			*tl = x;        /* size of this write */
   1135 
   1136 		}
   1137 		nfsm_uiotom(uiop, len);
   1138 		nfsm_request(vp, NFSPROC_WRITE, uiop->uio_procp,
   1139 			     VTONFS(vp)->n_wcred);
   1140 		if (v3) {
   1141 			wccflag = NFSV3_WCCCHK;
   1142 			nfsm_wcc_data(vp, wccflag);
   1143 			if (!error) {
   1144 				nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED
   1145 					+ NFSX_V3WRITEVERF);
   1146 				rlen = fxdr_unsigned(int, *tl++);
   1147 				if (rlen == 0) {
   1148 					error = NFSERR_IO;
   1149 					m_freem(mrep);
   1150 					break;
   1151 				} else if (rlen < len) {
   1152 					backup = len - rlen;
   1153 					(caddr_t)uiop->uio_iov->iov_base -=
   1154 					    backup;
   1155 					uiop->uio_iov->iov_len += backup;
   1156 					uiop->uio_offset -= backup;
   1157 					uiop->uio_resid += backup;
   1158 					len = rlen;
   1159 				}
   1160 				commit = fxdr_unsigned(int, *tl++);
   1161 
   1162 				/*
   1163 				 * Return the lowest committment level
   1164 				 * obtained by any of the RPCs.
   1165 				 */
   1166 				if (committed == NFSV3WRITE_FILESYNC)
   1167 					committed = commit;
   1168 				else if (committed == NFSV3WRITE_DATASYNC &&
   1169 					commit == NFSV3WRITE_UNSTABLE)
   1170 					committed = commit;
   1171 				if ((nmp->nm_iflag & NFSMNT_HASWRITEVERF) == 0){
   1172 				    memcpy((caddr_t)nmp->nm_verf, (caddr_t)tl,
   1173 					NFSX_V3WRITEVERF);
   1174 				    nmp->nm_iflag |= NFSMNT_HASWRITEVERF;
   1175 				} else if (memcmp((caddr_t)tl,
   1176 				    (caddr_t)nmp->nm_verf, NFSX_V3WRITEVERF)) {
   1177 				    *must_commit = 1;
   1178 				    memcpy((caddr_t)nmp->nm_verf, (caddr_t)tl,
   1179 					NFSX_V3WRITEVERF);
   1180 				}
   1181 			}
   1182 		} else
   1183 		    nfsm_loadattr(vp, (struct vattr *)0);
   1184 		if (wccflag)
   1185 		    VTONFS(vp)->n_mtime = VTONFS(vp)->n_vattr->va_mtime.tv_sec;
   1186 		m_freem(mrep);
   1187 		if (error)
   1188 			break;
   1189 		tsiz -= len;
   1190 	}
   1191 nfsmout:
   1192 	*iomode = committed;
   1193 	if (error)
   1194 		uiop->uio_resid = tsiz;
   1195 	return (error);
   1196 }
   1197 
   1198 /*
   1199  * nfs mknod rpc
   1200  * For NFS v2 this is a kludge. Use a create rpc but with the IFMT bits of the
   1201  * mode set to specify the file type and the size field for rdev.
   1202  */
   1203 int
   1204 nfs_mknodrpc(dvp, vpp, cnp, vap)
   1205 	struct vnode *dvp;
   1206 	struct vnode **vpp;
   1207 	struct componentname *cnp;
   1208 	struct vattr *vap;
   1209 {
   1210 	struct nfsv2_sattr *sp;
   1211 	u_int32_t *tl;
   1212 	caddr_t cp;
   1213 	int32_t t1, t2;
   1214 	struct vnode *newvp = (struct vnode *)0;
   1215 	struct nfsnode *np;
   1216 	char *cp2;
   1217 	caddr_t bpos, dpos;
   1218 	int error = 0, wccflag = NFSV3_WCCRATTR, gotvp = 0;
   1219 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
   1220 	u_int32_t rdev;
   1221 	const int v3 = NFS_ISV3(dvp);
   1222 
   1223 	if (vap->va_type == VCHR || vap->va_type == VBLK)
   1224 		rdev = txdr_unsigned(vap->va_rdev);
   1225 	else if (vap->va_type == VFIFO || vap->va_type == VSOCK)
   1226 		rdev = nfs_xdrneg1;
   1227 	else {
   1228 		VOP_ABORTOP(dvp, cnp);
   1229 		vput(dvp);
   1230 		return (EOPNOTSUPP);
   1231 	}
   1232 	nfsstats.rpccnt[NFSPROC_MKNOD]++;
   1233 	nfsm_reqhead(dvp, NFSPROC_MKNOD, NFSX_FH(v3) + 4 * NFSX_UNSIGNED +
   1234 		+ nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(v3));
   1235 	nfsm_fhtom(dvp, v3);
   1236 	nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
   1237 	if (v3) {
   1238 		nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
   1239 		*tl++ = vtonfsv3_type(vap->va_type);
   1240 		nfsm_v3attrbuild(vap, FALSE);
   1241 		if (vap->va_type == VCHR || vap->va_type == VBLK) {
   1242 			nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
   1243 			*tl++ = txdr_unsigned(major(vap->va_rdev));
   1244 			*tl = txdr_unsigned(minor(vap->va_rdev));
   1245 		}
   1246 	} else {
   1247 		nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
   1248 		sp->sa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
   1249 		sp->sa_uid = nfs_xdrneg1;
   1250 		sp->sa_gid = nfs_xdrneg1;
   1251 		sp->sa_size = rdev;
   1252 		txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
   1253 		txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
   1254 	}
   1255 	nfsm_request(dvp, NFSPROC_MKNOD, cnp->cn_proc, cnp->cn_cred);
   1256 	if (!error) {
   1257 		nfsm_mtofh(dvp, newvp, v3, gotvp);
   1258 		if (!gotvp) {
   1259 			if (newvp) {
   1260 				vrele(newvp);
   1261 				newvp = (struct vnode *)0;
   1262 			}
   1263 			error = nfs_lookitup(dvp, cnp->cn_nameptr,
   1264 			    cnp->cn_namelen, cnp->cn_cred, cnp->cn_proc, &np);
   1265 			if (!error)
   1266 				newvp = NFSTOV(np);
   1267 		}
   1268 	}
   1269 	if (v3)
   1270 		nfsm_wcc_data(dvp, wccflag);
   1271 	nfsm_reqdone;
   1272 	if (error) {
   1273 		if (newvp)
   1274 			vrele(newvp);
   1275 	} else {
   1276 		if (cnp->cn_flags & MAKEENTRY)
   1277 			cache_enter(dvp, newvp, cnp);
   1278 		*vpp = newvp;
   1279 	}
   1280 	PNBUF_PUT(cnp->cn_pnbuf);
   1281 	VTONFS(dvp)->n_flag |= NMODIFIED;
   1282 	if (!wccflag)
   1283 		VTONFS(dvp)->n_attrstamp = 0;
   1284 	vrele(dvp);
   1285 	return (error);
   1286 }
   1287 
   1288 /*
   1289  * nfs mknod vop
   1290  * just call nfs_mknodrpc() to do the work.
   1291  */
   1292 /* ARGSUSED */
   1293 int
   1294 nfs_mknod(v)
   1295 	void *v;
   1296 {
   1297 	struct vop_mknod_args /* {
   1298 		struct vnode *a_dvp;
   1299 		struct vnode **a_vpp;
   1300 		struct componentname *a_cnp;
   1301 		struct vattr *a_vap;
   1302 	} */ *ap = v;
   1303 	struct vnode *newvp;
   1304 	int error;
   1305 
   1306 	error = nfs_mknodrpc(ap->a_dvp, &newvp, ap->a_cnp, ap->a_vap);
   1307 	if (!error)
   1308 		vrele(newvp);
   1309 	return (error);
   1310 }
   1311 
   1312 static u_long create_verf;
   1313 /*
   1314  * nfs file create call
   1315  */
   1316 int
   1317 nfs_create(v)
   1318 	void *v;
   1319 {
   1320 	struct vop_create_args /* {
   1321 		struct vnode *a_dvp;
   1322 		struct vnode **a_vpp;
   1323 		struct componentname *a_cnp;
   1324 		struct vattr *a_vap;
   1325 	} */ *ap = v;
   1326 	struct vnode *dvp = ap->a_dvp;
   1327 	struct vattr *vap = ap->a_vap;
   1328 	struct componentname *cnp = ap->a_cnp;
   1329 	struct nfsv2_sattr *sp;
   1330 	u_int32_t *tl;
   1331 	caddr_t cp;
   1332 	int32_t t1, t2;
   1333 	struct nfsnode *np = (struct nfsnode *)0;
   1334 	struct vnode *newvp = (struct vnode *)0;
   1335 	caddr_t bpos, dpos, cp2;
   1336 	int error, wccflag = NFSV3_WCCRATTR, gotvp = 0, fmode = 0;
   1337 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
   1338 	const int v3 = NFS_ISV3(dvp);
   1339 
   1340 	/*
   1341 	 * Oops, not for me..
   1342 	 */
   1343 	if (vap->va_type == VSOCK)
   1344 		return (nfs_mknodrpc(dvp, ap->a_vpp, cnp, vap));
   1345 
   1346 #ifdef VA_EXCLUSIVE
   1347 	if (vap->va_vaflags & VA_EXCLUSIVE)
   1348 		fmode |= O_EXCL;
   1349 #endif
   1350 again:
   1351 	error = 0;
   1352 	nfsstats.rpccnt[NFSPROC_CREATE]++;
   1353 	nfsm_reqhead(dvp, NFSPROC_CREATE, NFSX_FH(v3) + 2 * NFSX_UNSIGNED +
   1354 		nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(v3));
   1355 	nfsm_fhtom(dvp, v3);
   1356 	nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
   1357 	if (v3) {
   1358 		nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
   1359 		if (fmode & O_EXCL) {
   1360 			*tl = txdr_unsigned(NFSV3CREATE_EXCLUSIVE);
   1361 			nfsm_build(tl, u_int32_t *, NFSX_V3CREATEVERF);
   1362 #ifdef INET
   1363 			if (in_ifaddr.tqh_first)
   1364 				*tl++ = in_ifaddr.tqh_first->ia_addr.sin_addr.s_addr;
   1365 			else
   1366 				*tl++ = create_verf;
   1367 #else
   1368 			*tl++ = create_verf;
   1369 #endif
   1370 			*tl = ++create_verf;
   1371 		} else {
   1372 			*tl = txdr_unsigned(NFSV3CREATE_UNCHECKED);
   1373 			nfsm_v3attrbuild(vap, FALSE);
   1374 		}
   1375 	} else {
   1376 		nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
   1377 		sp->sa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
   1378 		sp->sa_uid = nfs_xdrneg1;
   1379 		sp->sa_gid = nfs_xdrneg1;
   1380 		sp->sa_size = 0;
   1381 		txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
   1382 		txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
   1383 	}
   1384 	nfsm_request(dvp, NFSPROC_CREATE, cnp->cn_proc, cnp->cn_cred);
   1385 	if (!error) {
   1386 		nfsm_mtofh(dvp, newvp, v3, gotvp);
   1387 		if (!gotvp) {
   1388 			if (newvp) {
   1389 				vrele(newvp);
   1390 				newvp = (struct vnode *)0;
   1391 			}
   1392 			error = nfs_lookitup(dvp, cnp->cn_nameptr,
   1393 			    cnp->cn_namelen, cnp->cn_cred, cnp->cn_proc, &np);
   1394 			if (!error)
   1395 				newvp = NFSTOV(np);
   1396 		}
   1397 	}
   1398 	if (v3)
   1399 		nfsm_wcc_data(dvp, wccflag);
   1400 	nfsm_reqdone;
   1401 	if (error) {
   1402 		if (v3 && (fmode & O_EXCL) && error == NFSERR_NOTSUPP) {
   1403 			fmode &= ~O_EXCL;
   1404 			goto again;
   1405 		}
   1406 		if (newvp)
   1407 			vrele(newvp);
   1408 	} else if (v3 && (fmode & O_EXCL))
   1409 		error = nfs_setattrrpc(newvp, vap, cnp->cn_cred, cnp->cn_proc);
   1410 	if (!error) {
   1411 		if (cnp->cn_flags & MAKEENTRY)
   1412 			cache_enter(dvp, newvp, cnp);
   1413 		*ap->a_vpp = newvp;
   1414 	}
   1415 	PNBUF_PUT(cnp->cn_pnbuf);
   1416 	VTONFS(dvp)->n_flag |= NMODIFIED;
   1417 	if (!wccflag)
   1418 		VTONFS(dvp)->n_attrstamp = 0;
   1419 	vrele(dvp);
   1420 	return (error);
   1421 }
   1422 
   1423 /*
   1424  * nfs file remove call
   1425  * To try and make nfs semantics closer to ufs semantics, a file that has
   1426  * other processes using the vnode is renamed instead of removed and then
   1427  * removed later on the last close.
   1428  * - If v_usecount > 1
   1429  *	  If a rename is not already in the works
   1430  *	     call nfs_sillyrename() to set it up
   1431  *     else
   1432  *	  do the remove rpc
   1433  */
   1434 int
   1435 nfs_remove(v)
   1436 	void *v;
   1437 {
   1438 	struct vop_remove_args /* {
   1439 		struct vnodeop_desc *a_desc;
   1440 		struct vnode * a_dvp;
   1441 		struct vnode * a_vp;
   1442 		struct componentname * a_cnp;
   1443 	} */ *ap = v;
   1444 	struct vnode *vp = ap->a_vp;
   1445 	struct vnode *dvp = ap->a_dvp;
   1446 	struct componentname *cnp = ap->a_cnp;
   1447 	struct nfsnode *np = VTONFS(vp);
   1448 	int error = 0;
   1449 	struct vattr vattr;
   1450 
   1451 #ifndef DIAGNOSTIC
   1452 	if ((cnp->cn_flags & HASBUF) == 0)
   1453 		panic("nfs_remove: no name");
   1454 	if (vp->v_usecount < 1)
   1455 		panic("nfs_remove: bad v_usecount");
   1456 #endif
   1457 	if (vp->v_type == VDIR)
   1458 		error = EPERM;
   1459 	else if (vp->v_usecount == 1 || (np->n_sillyrename &&
   1460 	    VOP_GETATTR(vp, &vattr, cnp->cn_cred, cnp->cn_proc) == 0 &&
   1461 	    vattr.va_nlink > 1)) {
   1462 		/*
   1463 		 * Purge the name cache so that the chance of a lookup for
   1464 		 * the name succeeding while the remove is in progress is
   1465 		 * minimized. Without node locking it can still happen, such
   1466 		 * that an I/O op returns ESTALE, but since you get this if
   1467 		 * another host removes the file..
   1468 		 */
   1469 		cache_purge(vp);
   1470 		/*
   1471 		 * throw away biocache buffers, mainly to avoid
   1472 		 * unnecessary delayed writes later.
   1473 		 */
   1474 		error = nfs_vinvalbuf(vp, 0, cnp->cn_cred, cnp->cn_proc, 1);
   1475 		/* Do the rpc */
   1476 		if (error != EINTR)
   1477 			error = nfs_removerpc(dvp, cnp->cn_nameptr,
   1478 				cnp->cn_namelen, cnp->cn_cred, cnp->cn_proc);
   1479 		/*
   1480 		 * Kludge City: If the first reply to the remove rpc is lost..
   1481 		 *   the reply to the retransmitted request will be ENOENT
   1482 		 *   since the file was in fact removed
   1483 		 *   Therefore, we cheat and return success.
   1484 		 */
   1485 		if (error == ENOENT)
   1486 			error = 0;
   1487 	} else if (!np->n_sillyrename)
   1488 		error = nfs_sillyrename(dvp, vp, cnp);
   1489 	PNBUF_PUT(cnp->cn_pnbuf);
   1490 	np->n_attrstamp = 0;
   1491 	vrele(dvp);
   1492 	vrele(vp);
   1493 	return (error);
   1494 }
   1495 
   1496 /*
   1497  * nfs file remove rpc called from nfs_inactive
   1498  */
   1499 int
   1500 nfs_removeit(sp)
   1501 	struct sillyrename *sp;
   1502 {
   1503 
   1504 	return (nfs_removerpc(sp->s_dvp, sp->s_name, sp->s_namlen, sp->s_cred,
   1505 		(struct proc *)0));
   1506 }
   1507 
   1508 /*
   1509  * Nfs remove rpc, called from nfs_remove() and nfs_removeit().
   1510  */
   1511 int
   1512 nfs_removerpc(dvp, name, namelen, cred, proc)
   1513 	struct vnode *dvp;
   1514 	const char *name;
   1515 	int namelen;
   1516 	struct ucred *cred;
   1517 	struct proc *proc;
   1518 {
   1519 	u_int32_t *tl;
   1520 	caddr_t cp;
   1521 	int32_t t1, t2;
   1522 	caddr_t bpos, dpos, cp2;
   1523 	int error = 0, wccflag = NFSV3_WCCRATTR;
   1524 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
   1525 	const int v3 = NFS_ISV3(dvp);
   1526 
   1527 	nfsstats.rpccnt[NFSPROC_REMOVE]++;
   1528 	nfsm_reqhead(dvp, NFSPROC_REMOVE,
   1529 		NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(namelen));
   1530 	nfsm_fhtom(dvp, v3);
   1531 	nfsm_strtom(name, namelen, NFS_MAXNAMLEN);
   1532 	nfsm_request(dvp, NFSPROC_REMOVE, proc, cred);
   1533 	if (v3)
   1534 		nfsm_wcc_data(dvp, wccflag);
   1535 	nfsm_reqdone;
   1536 	VTONFS(dvp)->n_flag |= NMODIFIED;
   1537 	if (!wccflag)
   1538 		VTONFS(dvp)->n_attrstamp = 0;
   1539 	return (error);
   1540 }
   1541 
   1542 /*
   1543  * nfs file rename call
   1544  */
   1545 int
   1546 nfs_rename(v)
   1547 	void *v;
   1548 {
   1549 	struct vop_rename_args  /* {
   1550 		struct vnode *a_fdvp;
   1551 		struct vnode *a_fvp;
   1552 		struct componentname *a_fcnp;
   1553 		struct vnode *a_tdvp;
   1554 		struct vnode *a_tvp;
   1555 		struct componentname *a_tcnp;
   1556 	} */ *ap = v;
   1557 	struct vnode *fvp = ap->a_fvp;
   1558 	struct vnode *tvp = ap->a_tvp;
   1559 	struct vnode *fdvp = ap->a_fdvp;
   1560 	struct vnode *tdvp = ap->a_tdvp;
   1561 	struct componentname *tcnp = ap->a_tcnp;
   1562 	struct componentname *fcnp = ap->a_fcnp;
   1563 	int error;
   1564 
   1565 #ifndef DIAGNOSTIC
   1566 	if ((tcnp->cn_flags & HASBUF) == 0 ||
   1567 	    (fcnp->cn_flags & HASBUF) == 0)
   1568 		panic("nfs_rename: no name");
   1569 #endif
   1570 	/* Check for cross-device rename */
   1571 	if ((fvp->v_mount != tdvp->v_mount) ||
   1572 	    (tvp && (fvp->v_mount != tvp->v_mount))) {
   1573 		error = EXDEV;
   1574 		goto out;
   1575 	}
   1576 
   1577 	/*
   1578 	 * If the tvp exists and is in use, sillyrename it before doing the
   1579 	 * rename of the new file over it.
   1580 	 */
   1581 	if (tvp && tvp->v_usecount > 1 && !VTONFS(tvp)->n_sillyrename &&
   1582 		tvp->v_type != VDIR && !nfs_sillyrename(tdvp, tvp, tcnp)) {
   1583 		vrele(tvp);
   1584 		tvp = NULL;
   1585 	}
   1586 
   1587 	error = nfs_renamerpc(fdvp, fcnp->cn_nameptr, fcnp->cn_namelen,
   1588 		tdvp, tcnp->cn_nameptr, tcnp->cn_namelen, tcnp->cn_cred,
   1589 		tcnp->cn_proc);
   1590 
   1591 	if (fvp->v_type == VDIR) {
   1592 		if (tvp != NULL && tvp->v_type == VDIR)
   1593 			cache_purge(tdvp);
   1594 		cache_purge(fdvp);
   1595 	}
   1596 out:
   1597 	if (tdvp == tvp)
   1598 		vrele(tdvp);
   1599 	else
   1600 		vput(tdvp);
   1601 	if (tvp)
   1602 		vput(tvp);
   1603 	vrele(fdvp);
   1604 	vrele(fvp);
   1605 	/*
   1606 	 * Kludge: Map ENOENT => 0 assuming that it is a reply to a retry.
   1607 	 */
   1608 	if (error == ENOENT)
   1609 		error = 0;
   1610 	return (error);
   1611 }
   1612 
   1613 /*
   1614  * nfs file rename rpc called from nfs_remove() above
   1615  */
   1616 int
   1617 nfs_renameit(sdvp, scnp, sp)
   1618 	struct vnode *sdvp;
   1619 	struct componentname *scnp;
   1620 	struct sillyrename *sp;
   1621 {
   1622 	return (nfs_renamerpc(sdvp, scnp->cn_nameptr, scnp->cn_namelen,
   1623 		sdvp, sp->s_name, sp->s_namlen, scnp->cn_cred, scnp->cn_proc));
   1624 }
   1625 
   1626 /*
   1627  * Do an nfs rename rpc. Called from nfs_rename() and nfs_renameit().
   1628  */
   1629 int
   1630 nfs_renamerpc(fdvp, fnameptr, fnamelen, tdvp, tnameptr, tnamelen, cred, proc)
   1631 	struct vnode *fdvp;
   1632 	const char *fnameptr;
   1633 	int fnamelen;
   1634 	struct vnode *tdvp;
   1635 	const char *tnameptr;
   1636 	int tnamelen;
   1637 	struct ucred *cred;
   1638 	struct proc *proc;
   1639 {
   1640 	u_int32_t *tl;
   1641 	caddr_t cp;
   1642 	int32_t t1, t2;
   1643 	caddr_t bpos, dpos, cp2;
   1644 	int error = 0, fwccflag = NFSV3_WCCRATTR, twccflag = NFSV3_WCCRATTR;
   1645 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
   1646 	const int v3 = NFS_ISV3(fdvp);
   1647 
   1648 	nfsstats.rpccnt[NFSPROC_RENAME]++;
   1649 	nfsm_reqhead(fdvp, NFSPROC_RENAME,
   1650 		(NFSX_FH(v3) + NFSX_UNSIGNED)*2 + nfsm_rndup(fnamelen) +
   1651 		nfsm_rndup(tnamelen));
   1652 	nfsm_fhtom(fdvp, v3);
   1653 	nfsm_strtom(fnameptr, fnamelen, NFS_MAXNAMLEN);
   1654 	nfsm_fhtom(tdvp, v3);
   1655 	nfsm_strtom(tnameptr, tnamelen, NFS_MAXNAMLEN);
   1656 	nfsm_request(fdvp, NFSPROC_RENAME, proc, cred);
   1657 	if (v3) {
   1658 		nfsm_wcc_data(fdvp, fwccflag);
   1659 		nfsm_wcc_data(tdvp, twccflag);
   1660 	}
   1661 	nfsm_reqdone;
   1662 	VTONFS(fdvp)->n_flag |= NMODIFIED;
   1663 	VTONFS(tdvp)->n_flag |= NMODIFIED;
   1664 	if (!fwccflag)
   1665 		VTONFS(fdvp)->n_attrstamp = 0;
   1666 	if (!twccflag)
   1667 		VTONFS(tdvp)->n_attrstamp = 0;
   1668 	return (error);
   1669 }
   1670 
   1671 /*
   1672  * nfs hard link create call
   1673  */
   1674 int
   1675 nfs_link(v)
   1676 	void *v;
   1677 {
   1678 	struct vop_link_args /* {
   1679 		struct vnode *a_dvp;
   1680 		struct vnode *a_vp;
   1681 		struct componentname *a_cnp;
   1682 	} */ *ap = v;
   1683 	struct vnode *vp = ap->a_vp;
   1684 	struct vnode *dvp = ap->a_dvp;
   1685 	struct componentname *cnp = ap->a_cnp;
   1686 	u_int32_t *tl;
   1687 	caddr_t cp;
   1688 	int32_t t1, t2;
   1689 	caddr_t bpos, dpos, cp2;
   1690 	int error = 0, wccflag = NFSV3_WCCRATTR, attrflag = 0;
   1691 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
   1692 	/* XXX Should be const and initialised? */
   1693 	int v3;
   1694 
   1695 	if (dvp->v_mount != vp->v_mount) {
   1696 		VOP_ABORTOP(vp, cnp);
   1697 		vput(dvp);
   1698 		return (EXDEV);
   1699 	}
   1700 
   1701 	/*
   1702 	 * Push all writes to the server, so that the attribute cache
   1703 	 * doesn't get "out of sync" with the server.
   1704 	 * XXX There should be a better way!
   1705 	 */
   1706 	VOP_FSYNC(vp, cnp->cn_cred, FSYNC_WAIT, 0, 0, cnp->cn_proc);
   1707 
   1708 	v3 = NFS_ISV3(vp);
   1709 	nfsstats.rpccnt[NFSPROC_LINK]++;
   1710 	nfsm_reqhead(vp, NFSPROC_LINK,
   1711 		NFSX_FH(v3)*2 + NFSX_UNSIGNED + nfsm_rndup(cnp->cn_namelen));
   1712 	nfsm_fhtom(vp, v3);
   1713 	nfsm_fhtom(dvp, v3);
   1714 	nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
   1715 	nfsm_request(vp, NFSPROC_LINK, cnp->cn_proc, cnp->cn_cred);
   1716 	if (v3) {
   1717 		nfsm_postop_attr(vp, attrflag);
   1718 		nfsm_wcc_data(dvp, wccflag);
   1719 	}
   1720 	nfsm_reqdone;
   1721 	PNBUF_PUT(cnp->cn_pnbuf);
   1722 	VTONFS(dvp)->n_flag |= NMODIFIED;
   1723 	if (!attrflag)
   1724 		VTONFS(vp)->n_attrstamp = 0;
   1725 	if (!wccflag)
   1726 		VTONFS(dvp)->n_attrstamp = 0;
   1727 	vput(dvp);
   1728 	/*
   1729 	 * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry.
   1730 	 */
   1731 	if (error == EEXIST)
   1732 		error = 0;
   1733 	return (error);
   1734 }
   1735 
   1736 /*
   1737  * nfs symbolic link create call
   1738  */
   1739 int
   1740 nfs_symlink(v)
   1741 	void *v;
   1742 {
   1743 	struct vop_symlink_args /* {
   1744 		struct vnode *a_dvp;
   1745 		struct vnode **a_vpp;
   1746 		struct componentname *a_cnp;
   1747 		struct vattr *a_vap;
   1748 		char *a_target;
   1749 	} */ *ap = v;
   1750 	struct vnode *dvp = ap->a_dvp;
   1751 	struct vattr *vap = ap->a_vap;
   1752 	struct componentname *cnp = ap->a_cnp;
   1753 	struct nfsv2_sattr *sp;
   1754 	u_int32_t *tl;
   1755 	caddr_t cp;
   1756 	int32_t t1, t2;
   1757 	caddr_t bpos, dpos, cp2;
   1758 	int slen, error = 0, wccflag = NFSV3_WCCRATTR, gotvp;
   1759 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
   1760 	struct vnode *newvp = (struct vnode *)0;
   1761 	const int v3 = NFS_ISV3(dvp);
   1762 
   1763 	nfsstats.rpccnt[NFSPROC_SYMLINK]++;
   1764 	slen = strlen(ap->a_target);
   1765 	nfsm_reqhead(dvp, NFSPROC_SYMLINK, NFSX_FH(v3) + 2*NFSX_UNSIGNED +
   1766 	    nfsm_rndup(cnp->cn_namelen) + nfsm_rndup(slen) + NFSX_SATTR(v3));
   1767 	nfsm_fhtom(dvp, v3);
   1768 	nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
   1769 	if (v3)
   1770 		nfsm_v3attrbuild(vap, FALSE);
   1771 	nfsm_strtom(ap->a_target, slen, NFS_MAXPATHLEN);
   1772 	if (!v3) {
   1773 		nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
   1774 		sp->sa_mode = vtonfsv2_mode(VLNK, vap->va_mode);
   1775 		sp->sa_uid = nfs_xdrneg1;
   1776 		sp->sa_gid = nfs_xdrneg1;
   1777 		sp->sa_size = nfs_xdrneg1;
   1778 		txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
   1779 		txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
   1780 	}
   1781 	nfsm_request(dvp, NFSPROC_SYMLINK, cnp->cn_proc, cnp->cn_cred);
   1782 	if (v3) {
   1783 		if (!error)
   1784 			nfsm_mtofh(dvp, newvp, v3, gotvp);
   1785 		nfsm_wcc_data(dvp, wccflag);
   1786 	}
   1787 	nfsm_reqdone;
   1788 	if (newvp)
   1789 		vrele(newvp);
   1790 	PNBUF_PUT(cnp->cn_pnbuf);
   1791 	VTONFS(dvp)->n_flag |= NMODIFIED;
   1792 	if (!wccflag)
   1793 		VTONFS(dvp)->n_attrstamp = 0;
   1794 	vrele(dvp);
   1795 	/*
   1796 	 * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry.
   1797 	 */
   1798 	if (error == EEXIST)
   1799 		error = 0;
   1800 	return (error);
   1801 }
   1802 
   1803 /*
   1804  * nfs make dir call
   1805  */
   1806 int
   1807 nfs_mkdir(v)
   1808 	void *v;
   1809 {
   1810 	struct vop_mkdir_args /* {
   1811 		struct vnode *a_dvp;
   1812 		struct vnode **a_vpp;
   1813 		struct componentname *a_cnp;
   1814 		struct vattr *a_vap;
   1815 	} */ *ap = v;
   1816 	struct vnode *dvp = ap->a_dvp;
   1817 	struct vattr *vap = ap->a_vap;
   1818 	struct componentname *cnp = ap->a_cnp;
   1819 	struct nfsv2_sattr *sp;
   1820 	u_int32_t *tl;
   1821 	caddr_t cp;
   1822 	int32_t t1, t2;
   1823 	int len;
   1824 	struct nfsnode *np = (struct nfsnode *)0;
   1825 	struct vnode *newvp = (struct vnode *)0;
   1826 	caddr_t bpos, dpos, cp2;
   1827 	int error = 0, wccflag = NFSV3_WCCRATTR;
   1828 	int gotvp = 0;
   1829 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
   1830 	const int v3 = NFS_ISV3(dvp);
   1831 
   1832 	len = cnp->cn_namelen;
   1833 	nfsstats.rpccnt[NFSPROC_MKDIR]++;
   1834 	nfsm_reqhead(dvp, NFSPROC_MKDIR,
   1835 	  NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len) + NFSX_SATTR(v3));
   1836 	nfsm_fhtom(dvp, v3);
   1837 	nfsm_strtom(cnp->cn_nameptr, len, NFS_MAXNAMLEN);
   1838 	if (v3) {
   1839 		nfsm_v3attrbuild(vap, FALSE);
   1840 	} else {
   1841 		nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
   1842 		sp->sa_mode = vtonfsv2_mode(VDIR, vap->va_mode);
   1843 		sp->sa_uid = nfs_xdrneg1;
   1844 		sp->sa_gid = nfs_xdrneg1;
   1845 		sp->sa_size = nfs_xdrneg1;
   1846 		txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
   1847 		txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
   1848 	}
   1849 	nfsm_request(dvp, NFSPROC_MKDIR, cnp->cn_proc, cnp->cn_cred);
   1850 	if (!error)
   1851 		nfsm_mtofh(dvp, newvp, v3, gotvp);
   1852 	if (v3)
   1853 		nfsm_wcc_data(dvp, wccflag);
   1854 	nfsm_reqdone;
   1855 	VTONFS(dvp)->n_flag |= NMODIFIED;
   1856 	if (!wccflag)
   1857 		VTONFS(dvp)->n_attrstamp = 0;
   1858 	/*
   1859 	 * Kludge: Map EEXIST => 0 assuming that you have a reply to a retry
   1860 	 * if we can succeed in looking up the directory.
   1861 	 */
   1862 	if (error == EEXIST || (!error && !gotvp)) {
   1863 		if (newvp) {
   1864 			vrele(newvp);
   1865 			newvp = (struct vnode *)0;
   1866 		}
   1867 		error = nfs_lookitup(dvp, cnp->cn_nameptr, len, cnp->cn_cred,
   1868 			cnp->cn_proc, &np);
   1869 		if (!error) {
   1870 			newvp = NFSTOV(np);
   1871 			if (newvp->v_type != VDIR)
   1872 				error = EEXIST;
   1873 		}
   1874 	}
   1875 	if (error) {
   1876 		if (newvp)
   1877 			vrele(newvp);
   1878 	} else {
   1879 		if (cnp->cn_flags & MAKEENTRY)
   1880 			cache_enter(dvp, newvp, cnp);
   1881 		*ap->a_vpp = newvp;
   1882 	}
   1883 	PNBUF_PUT(cnp->cn_pnbuf);
   1884 	vrele(dvp);
   1885 	return (error);
   1886 }
   1887 
   1888 /*
   1889  * nfs remove directory call
   1890  */
   1891 int
   1892 nfs_rmdir(v)
   1893 	void *v;
   1894 {
   1895 	struct vop_rmdir_args /* {
   1896 		struct vnode *a_dvp;
   1897 		struct vnode *a_vp;
   1898 		struct componentname *a_cnp;
   1899 	} */ *ap = v;
   1900 	struct vnode *vp = ap->a_vp;
   1901 	struct vnode *dvp = ap->a_dvp;
   1902 	struct componentname *cnp = ap->a_cnp;
   1903 	u_int32_t *tl;
   1904 	caddr_t cp;
   1905 	int32_t t1, t2;
   1906 	caddr_t bpos, dpos, cp2;
   1907 	int error = 0, wccflag = NFSV3_WCCRATTR;
   1908 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
   1909 	const int v3 = NFS_ISV3(dvp);
   1910 
   1911 	if (dvp == vp) {
   1912 		vrele(dvp);
   1913 		vrele(dvp);
   1914 		PNBUF_PUT(cnp->cn_pnbuf);
   1915 		return (EINVAL);
   1916 	}
   1917 	nfsstats.rpccnt[NFSPROC_RMDIR]++;
   1918 	nfsm_reqhead(dvp, NFSPROC_RMDIR,
   1919 		NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(cnp->cn_namelen));
   1920 	nfsm_fhtom(dvp, v3);
   1921 	nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
   1922 	nfsm_request(dvp, NFSPROC_RMDIR, cnp->cn_proc, cnp->cn_cred);
   1923 	if (v3)
   1924 		nfsm_wcc_data(dvp, wccflag);
   1925 	nfsm_reqdone;
   1926 	PNBUF_PUT(cnp->cn_pnbuf);
   1927 	VTONFS(dvp)->n_flag |= NMODIFIED;
   1928 	if (!wccflag)
   1929 		VTONFS(dvp)->n_attrstamp = 0;
   1930 	cache_purge(dvp);
   1931 	cache_purge(vp);
   1932 	vrele(vp);
   1933 	vrele(dvp);
   1934 	/*
   1935 	 * Kludge: Map ENOENT => 0 assuming that you have a reply to a retry.
   1936 	 */
   1937 	if (error == ENOENT)
   1938 		error = 0;
   1939 	return (error);
   1940 }
   1941 
   1942 /*
   1943  * nfs readdir call
   1944  */
   1945 int
   1946 nfs_readdir(v)
   1947 	void *v;
   1948 {
   1949 	struct vop_readdir_args /* {
   1950 		struct vnode *a_vp;
   1951 		struct uio *a_uio;
   1952 		struct ucred *a_cred;
   1953 		int *a_eofflag;
   1954 		off_t **a_cookies;
   1955 		int *a_ncookies;
   1956 	} */ *ap = v;
   1957 	struct vnode *vp = ap->a_vp;
   1958 	struct uio *uio = ap->a_uio;
   1959 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
   1960 	char *base = uio->uio_iov->iov_base;
   1961 	int tresid, error;
   1962 	size_t count, lost;
   1963 	struct dirent *dp;
   1964 	off_t *cookies = NULL;
   1965 	int ncookies = 0, nc;
   1966 
   1967 	if (vp->v_type != VDIR)
   1968 		return (EPERM);
   1969 
   1970 	lost = uio->uio_resid & (NFS_DIRFRAGSIZ - 1);
   1971 	count = uio->uio_resid - lost;
   1972 	if (count <= 0)
   1973 		return (EINVAL);
   1974 
   1975 	/*
   1976 	 * Call nfs_bioread() to do the real work.
   1977 	 */
   1978 	tresid = uio->uio_resid = count;
   1979 	error = nfs_bioread(vp, uio, 0, ap->a_cred,
   1980 		    ap->a_cookies ? NFSBIO_CACHECOOKIES : 0);
   1981 
   1982 	if (!error && ap->a_cookies) {
   1983 		ncookies = count / 16;
   1984 		cookies = malloc(sizeof (off_t) * ncookies, M_TEMP, M_WAITOK);
   1985 		*ap->a_cookies = cookies;
   1986 	}
   1987 
   1988 	if (!error && uio->uio_resid == tresid) {
   1989 		uio->uio_resid += lost;
   1990 		nfsstats.direofcache_misses++;
   1991 		if (ap->a_cookies)
   1992 			*ap->a_ncookies = 0;
   1993 		*ap->a_eofflag = 1;
   1994 		return (0);
   1995 	}
   1996 
   1997 	if (!error && ap->a_cookies) {
   1998 		/*
   1999 		 * Only the NFS server and emulations use cookies, and they
   2000 		 * load the directory block into system space, so we can
   2001 		 * just look at it directly.
   2002 		 */
   2003 		if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1)
   2004 			panic("nfs_readdir: lost in space");
   2005 		for (nc = 0; ncookies-- &&
   2006 		     base < (char *)uio->uio_iov->iov_base; nc++){
   2007 			dp = (struct dirent *) base;
   2008 			if (dp->d_reclen == 0)
   2009 				break;
   2010 			if (nmp->nm_flag & NFSMNT_XLATECOOKIE)
   2011 				*(cookies++) = (off_t)NFS_GETCOOKIE32(dp);
   2012 			else
   2013 				*(cookies++) = NFS_GETCOOKIE(dp);
   2014 			base += dp->d_reclen;
   2015 		}
   2016 		uio->uio_resid +=
   2017 		    ((caddr_t)uio->uio_iov->iov_base - base);
   2018 		uio->uio_iov->iov_len +=
   2019 		    ((caddr_t)uio->uio_iov->iov_base - base);
   2020 		uio->uio_iov->iov_base = base;
   2021 		*ap->a_ncookies = nc;
   2022 	}
   2023 
   2024 	uio->uio_resid += lost;
   2025 	*ap->a_eofflag = 0;
   2026 	return (error);
   2027 }
   2028 
   2029 /*
   2030  * Readdir rpc call.
   2031  * Called from below the buffer cache by nfs_doio().
   2032  */
   2033 int
   2034 nfs_readdirrpc(vp, uiop, cred)
   2035 	struct vnode *vp;
   2036 	struct uio *uiop;
   2037 	struct ucred *cred;
   2038 {
   2039 	int len, left;
   2040 	struct dirent *dp = NULL;
   2041 	u_int32_t *tl;
   2042 	caddr_t cp;
   2043 	int32_t t1, t2;
   2044 	caddr_t bpos, dpos, cp2;
   2045 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
   2046 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
   2047 	struct nfsnode *dnp = VTONFS(vp);
   2048 	u_quad_t fileno;
   2049 	int error = 0, tlen, more_dirs = 1, blksiz = 0, bigenough = 1;
   2050 	int attrflag, nrpcs = 0, reclen;
   2051 	const int v3 = NFS_ISV3(vp);
   2052 	nfsquad_t cookie;
   2053 
   2054 #ifdef DIAGNOSTIC
   2055 	/*
   2056 	 * Should be called from buffer cache, so only amount of
   2057 	 * NFS_DIRBLKSIZ will be requested.
   2058 	 */
   2059 	if (uiop->uio_iovcnt != 1 || (uiop->uio_resid & (NFS_DIRBLKSIZ - 1)))
   2060 		panic("nfs readdirrpc bad uio");
   2061 #endif
   2062 
   2063 	/*
   2064 	 * Loop around doing readdir rpc's of size nm_readdirsize
   2065 	 * truncated to a multiple of NFS_DIRFRAGSIZ.
   2066 	 * The stopping criteria is EOF or buffer full.
   2067 	 */
   2068 	while (more_dirs && bigenough) {
   2069 		/*
   2070 		 * Heuristic: don't bother to do another RPC to further
   2071 		 * fill up this block if there is not much room left. (< 50%
   2072 		 * of the readdir RPC size). This wastes some buffer space
   2073 		 * but can save up to 50% in RPC calls.
   2074 		 */
   2075 		if (nrpcs > 0 && uiop->uio_resid < (nmp->nm_readdirsize / 2)) {
   2076 			bigenough = 0;
   2077 			break;
   2078 		}
   2079 		nfsstats.rpccnt[NFSPROC_READDIR]++;
   2080 		nfsm_reqhead(vp, NFSPROC_READDIR, NFSX_FH(v3) +
   2081 			NFSX_READDIR(v3));
   2082 		nfsm_fhtom(vp, v3);
   2083 		if (v3) {
   2084 			nfsm_build(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
   2085 			cookie.qval = uiop->uio_offset;
   2086 			if (nmp->nm_iflag & NFSMNT_SWAPCOOKIE) {
   2087 				txdr_swapcookie3(uiop->uio_offset, tl);
   2088 			} else {
   2089 				txdr_cookie3(uiop->uio_offset, tl);
   2090 			}
   2091 			tl += 2;
   2092 			*tl++ = dnp->n_cookieverf.nfsuquad[0];
   2093 			*tl++ = dnp->n_cookieverf.nfsuquad[1];
   2094 		} else {
   2095 			nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
   2096 			*tl++ = txdr_unsigned(uiop->uio_offset);
   2097 		}
   2098 		*tl = txdr_unsigned(nmp->nm_readdirsize);
   2099 		nfsm_request(vp, NFSPROC_READDIR, uiop->uio_procp, cred);
   2100 		nrpcs++;
   2101 		if (v3) {
   2102 			nfsm_postop_attr(vp, attrflag);
   2103 			if (!error) {
   2104 				nfsm_dissect(tl, u_int32_t *,
   2105 				    2 * NFSX_UNSIGNED);
   2106 				dnp->n_cookieverf.nfsuquad[0] = *tl++;
   2107 				dnp->n_cookieverf.nfsuquad[1] = *tl;
   2108 			} else {
   2109 				m_freem(mrep);
   2110 				goto nfsmout;
   2111 			}
   2112 		}
   2113 		nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
   2114 		more_dirs = fxdr_unsigned(int, *tl);
   2115 
   2116 		/* loop thru the dir entries, doctoring them to 4bsd form */
   2117 		while (more_dirs && bigenough) {
   2118 			if (v3) {
   2119 				nfsm_dissect(tl, u_int32_t *,
   2120 				    3 * NFSX_UNSIGNED);
   2121 				fileno = fxdr_hyper(tl);
   2122 				len = fxdr_unsigned(int, *(tl + 2));
   2123 			} else {
   2124 				nfsm_dissect(tl, u_int32_t *,
   2125 				    2 * NFSX_UNSIGNED);
   2126 				fileno = fxdr_unsigned(u_quad_t, *tl++);
   2127 				len = fxdr_unsigned(int, *tl);
   2128 			}
   2129 			if (len <= 0 || len > NFS_MAXNAMLEN) {
   2130 				error = EBADRPC;
   2131 				m_freem(mrep);
   2132 				goto nfsmout;
   2133 			}
   2134 			tlen = nfsm_rndup(len);
   2135 			if (tlen == len)
   2136 				tlen += 4;	/* To ensure null termination */
   2137 			tlen += sizeof (off_t) + sizeof (int);
   2138 			reclen = ALIGN(tlen + DIRHDSIZ);
   2139 			tlen = reclen - DIRHDSIZ;
   2140 			left = NFS_DIRFRAGSIZ - blksiz;
   2141 			if (reclen > left) {
   2142 				dp->d_reclen += left;
   2143 				(caddr_t)uiop->uio_iov->iov_base += left;
   2144 				uiop->uio_iov->iov_len -= left;
   2145 				uiop->uio_resid -= left;
   2146 				blksiz = 0;
   2147 				NFS_STASHCOOKIE(dp, uiop->uio_offset);
   2148 			}
   2149 			if (reclen > uiop->uio_resid)
   2150 				bigenough = 0;
   2151 			if (bigenough) {
   2152 				dp = (struct dirent *)uiop->uio_iov->iov_base;
   2153 				dp->d_fileno = (int)fileno;
   2154 				dp->d_namlen = len;
   2155 				dp->d_reclen = reclen;
   2156 				dp->d_type = DT_UNKNOWN;
   2157 				blksiz += dp->d_reclen;
   2158 				if (blksiz == NFS_DIRFRAGSIZ)
   2159 					blksiz = 0;
   2160 				uiop->uio_resid -= DIRHDSIZ;
   2161 				(caddr_t)uiop->uio_iov->iov_base += DIRHDSIZ;
   2162 				uiop->uio_iov->iov_len -= DIRHDSIZ;
   2163 				nfsm_mtouio(uiop, len);
   2164 				cp = uiop->uio_iov->iov_base;
   2165 				tlen -= len;
   2166 				*cp = '\0';	/* null terminate */
   2167 				(caddr_t)uiop->uio_iov->iov_base += tlen;
   2168 				uiop->uio_iov->iov_len -= tlen;
   2169 				uiop->uio_resid -= tlen;
   2170 			} else
   2171 				nfsm_adv(nfsm_rndup(len));
   2172 			if (v3) {
   2173 				nfsm_dissect(tl, u_int32_t *,
   2174 				    3 * NFSX_UNSIGNED);
   2175 			} else {
   2176 				nfsm_dissect(tl, u_int32_t *,
   2177 				    2 * NFSX_UNSIGNED);
   2178 			}
   2179 			if (bigenough) {
   2180 				if (v3) {
   2181 					if (nmp->nm_iflag & NFSMNT_SWAPCOOKIE)
   2182 						uiop->uio_offset =
   2183 						    fxdr_swapcookie3(tl);
   2184 					else
   2185 						uiop->uio_offset =
   2186 						    fxdr_cookie3(tl);
   2187 				}
   2188 				else {
   2189 					uiop->uio_offset =
   2190 					    fxdr_unsigned(off_t, *tl);
   2191 				}
   2192 				NFS_STASHCOOKIE(dp, uiop->uio_offset);
   2193 			}
   2194 			if (v3)
   2195 				tl += 2;
   2196 			else
   2197 				tl++;
   2198 			more_dirs = fxdr_unsigned(int, *tl);
   2199 		}
   2200 		/*
   2201 		 * If at end of rpc data, get the eof boolean
   2202 		 */
   2203 		if (!more_dirs) {
   2204 			nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
   2205 			more_dirs = (fxdr_unsigned(int, *tl) == 0);
   2206 		}
   2207 		m_freem(mrep);
   2208 	}
   2209 	/*
   2210 	 * Fill last record, iff any, out to a multiple of NFS_DIRFRAGSIZ
   2211 	 * by increasing d_reclen for the last record.
   2212 	 */
   2213 	if (blksiz > 0) {
   2214 		left = NFS_DIRFRAGSIZ - blksiz;
   2215 		dp->d_reclen += left;
   2216 		NFS_STASHCOOKIE(dp, uiop->uio_offset);
   2217 		(caddr_t)uiop->uio_iov->iov_base += left;
   2218 		uiop->uio_iov->iov_len -= left;
   2219 		uiop->uio_resid -= left;
   2220 	}
   2221 
   2222 	/*
   2223 	 * We are now either at the end of the directory or have filled the
   2224 	 * block.
   2225 	 */
   2226 	if (bigenough)
   2227 		dnp->n_direofoffset = uiop->uio_offset;
   2228 nfsmout:
   2229 	return (error);
   2230 }
   2231 
   2232 /*
   2233  * NFS V3 readdir plus RPC. Used in place of nfs_readdirrpc().
   2234  */
   2235 int
   2236 nfs_readdirplusrpc(vp, uiop, cred)
   2237 	struct vnode *vp;
   2238 	struct uio *uiop;
   2239 	struct ucred *cred;
   2240 {
   2241 	int len, left;
   2242 	struct dirent *dp = NULL;
   2243 	u_int32_t *tl;
   2244 	caddr_t cp;
   2245 	int32_t t1, t2;
   2246 	struct vnode *newvp;
   2247 	caddr_t bpos, dpos, cp2;
   2248 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
   2249 	struct nameidata nami, *ndp = &nami;
   2250 	struct componentname *cnp = &ndp->ni_cnd;
   2251 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
   2252 	struct nfsnode *dnp = VTONFS(vp), *np;
   2253 	const unsigned char *hcp;
   2254 	nfsfh_t *fhp;
   2255 	u_quad_t fileno;
   2256 	int error = 0, tlen, more_dirs = 1, blksiz = 0, doit, bigenough = 1, i;
   2257 	int attrflag, fhsize, nrpcs = 0, reclen;
   2258 	struct nfs_fattr fattr, *fp;
   2259 
   2260 #ifdef DIAGNOSTIC
   2261 	if (uiop->uio_iovcnt != 1 || (uiop->uio_resid & (NFS_DIRBLKSIZ - 1)))
   2262 		panic("nfs readdirplusrpc bad uio");
   2263 #endif
   2264 	ndp->ni_dvp = vp;
   2265 	newvp = NULLVP;
   2266 
   2267 	/*
   2268 	 * Loop around doing readdir rpc's of size nm_readdirsize
   2269 	 * truncated to a multiple of NFS_DIRFRAGSIZ.
   2270 	 * The stopping criteria is EOF or buffer full.
   2271 	 */
   2272 	while (more_dirs && bigenough) {
   2273 		if (nrpcs > 0 && uiop->uio_resid < (nmp->nm_readdirsize / 2)) {
   2274 			bigenough = 0;
   2275 			break;
   2276 		}
   2277 		nfsstats.rpccnt[NFSPROC_READDIRPLUS]++;
   2278 		nfsm_reqhead(vp, NFSPROC_READDIRPLUS,
   2279 			NFSX_FH(1) + 6 * NFSX_UNSIGNED);
   2280 		nfsm_fhtom(vp, 1);
   2281  		nfsm_build(tl, u_int32_t *, 6 * NFSX_UNSIGNED);
   2282 		if (nmp->nm_iflag & NFSMNT_SWAPCOOKIE) {
   2283 			txdr_swapcookie3(uiop->uio_offset, tl);
   2284 		} else {
   2285 			txdr_cookie3(uiop->uio_offset, tl);
   2286 		}
   2287 		tl += 2;
   2288 		*tl++ = dnp->n_cookieverf.nfsuquad[0];
   2289 		*tl++ = dnp->n_cookieverf.nfsuquad[1];
   2290 		*tl++ = txdr_unsigned(nmp->nm_readdirsize);
   2291 		*tl = txdr_unsigned(nmp->nm_rsize);
   2292 		nfsm_request(vp, NFSPROC_READDIRPLUS, uiop->uio_procp, cred);
   2293 		nfsm_postop_attr(vp, attrflag);
   2294 		if (error) {
   2295 			m_freem(mrep);
   2296 			goto nfsmout;
   2297 		}
   2298 		nrpcs++;
   2299 		nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
   2300 		dnp->n_cookieverf.nfsuquad[0] = *tl++;
   2301 		dnp->n_cookieverf.nfsuquad[1] = *tl++;
   2302 		more_dirs = fxdr_unsigned(int, *tl);
   2303 
   2304 		/* loop thru the dir entries, doctoring them to 4bsd form */
   2305 		while (more_dirs && bigenough) {
   2306 			nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
   2307 			fileno = fxdr_hyper(tl);
   2308 			len = fxdr_unsigned(int, *(tl + 2));
   2309 			if (len <= 0 || len > NFS_MAXNAMLEN) {
   2310 				error = EBADRPC;
   2311 				m_freem(mrep);
   2312 				goto nfsmout;
   2313 			}
   2314 			tlen = nfsm_rndup(len);
   2315 			if (tlen == len)
   2316 				tlen += 4;	/* To ensure null termination*/
   2317 			tlen += sizeof (off_t) + sizeof (int);
   2318 			reclen = ALIGN(tlen + DIRHDSIZ);
   2319 			tlen = reclen - DIRHDSIZ;
   2320 			left = NFS_DIRFRAGSIZ - blksiz;
   2321 			if (reclen > left) {
   2322 				/*
   2323 				 * DIRFRAGSIZ is aligned, no need to align
   2324 				 * again here.
   2325 				 */
   2326 				dp->d_reclen += left;
   2327 				(caddr_t)uiop->uio_iov->iov_base += left;
   2328 				uiop->uio_iov->iov_len -= left;
   2329 				uiop->uio_resid -= left;
   2330 				NFS_STASHCOOKIE(dp, uiop->uio_offset);
   2331 				blksiz = 0;
   2332 			}
   2333 			if (reclen > uiop->uio_resid)
   2334 				bigenough = 0;
   2335 			if (bigenough) {
   2336 				dp = (struct dirent *)uiop->uio_iov->iov_base;
   2337 				dp->d_fileno = (int)fileno;
   2338 				dp->d_namlen = len;
   2339 				dp->d_reclen = reclen;
   2340 				dp->d_type = DT_UNKNOWN;
   2341 				blksiz += dp->d_reclen;
   2342 				if (blksiz == NFS_DIRFRAGSIZ)
   2343 					blksiz = 0;
   2344 				uiop->uio_resid -= DIRHDSIZ;
   2345 				(caddr_t)uiop->uio_iov->iov_base += DIRHDSIZ;
   2346 				uiop->uio_iov->iov_len -= DIRHDSIZ;
   2347 				cnp->cn_nameptr = uiop->uio_iov->iov_base;
   2348 				cnp->cn_namelen = len;
   2349 				nfsm_mtouio(uiop, len);
   2350 				cp = uiop->uio_iov->iov_base;
   2351 				tlen -= len;
   2352 				*cp = '\0';
   2353 				(caddr_t)uiop->uio_iov->iov_base += tlen;
   2354 				uiop->uio_iov->iov_len -= tlen;
   2355 				uiop->uio_resid -= tlen;
   2356 			} else
   2357 				nfsm_adv(nfsm_rndup(len));
   2358 			nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
   2359 			if (bigenough) {
   2360 				if (nmp->nm_iflag & NFSMNT_SWAPCOOKIE)
   2361 					uiop->uio_offset =
   2362 						fxdr_swapcookie3(tl);
   2363 				else
   2364 					uiop->uio_offset =
   2365 						fxdr_cookie3(tl);
   2366 				NFS_STASHCOOKIE(dp, uiop->uio_offset);
   2367 			}
   2368 			tl += 2;
   2369 
   2370 			/*
   2371 			 * Since the attributes are before the file handle
   2372 			 * (sigh), we must skip over the attributes and then
   2373 			 * come back and get them.
   2374 			 */
   2375 			attrflag = fxdr_unsigned(int, *tl);
   2376 			if (attrflag) {
   2377 			    nfsm_dissect(fp, struct nfs_fattr *, NFSX_V3FATTR);
   2378 			    memcpy(&fattr, fp, NFSX_V3FATTR);
   2379 			    nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
   2380 			    doit = fxdr_unsigned(int, *tl);
   2381 			    if (doit) {
   2382 				nfsm_getfh(fhp, fhsize, 1);
   2383 				if (NFS_CMPFH(dnp, fhp, fhsize)) {
   2384 				    VREF(vp);
   2385 				    newvp = vp;
   2386 				    np = dnp;
   2387 				} else {
   2388 				    error = nfs_nget(vp->v_mount, fhp,
   2389 					fhsize, &np);
   2390 				    if (!error)
   2391 					newvp = NFSTOV(np);
   2392 				}
   2393 				if (!error) {
   2394 				    nfs_loadattrcache(&newvp, &fattr, 0);
   2395 				    dp->d_type =
   2396 				        IFTODT(VTTOIF(np->n_vattr->va_type));
   2397 				    ndp->ni_vp = newvp;
   2398 				    cnp->cn_hash = 0;
   2399 				    for (hcp = cnp->cn_nameptr, i = 1; i <= len;
   2400 				        i++, hcp++)
   2401 				        cnp->cn_hash += *hcp * i;
   2402 				    if (cnp->cn_namelen <= NCHNAMLEN)
   2403 				        cache_enter(ndp->ni_dvp, ndp->ni_vp,
   2404 						    cnp);
   2405 				}
   2406 			   }
   2407 			} else {
   2408 			    /* Just skip over the file handle */
   2409 			    nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
   2410 			    i = fxdr_unsigned(int, *tl);
   2411 			    nfsm_adv(nfsm_rndup(i));
   2412 			}
   2413 			if (newvp != NULLVP) {
   2414 			    vrele(newvp);
   2415 			    newvp = NULLVP;
   2416 			}
   2417 			nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
   2418 			more_dirs = fxdr_unsigned(int, *tl);
   2419 		}
   2420 		/*
   2421 		 * If at end of rpc data, get the eof boolean
   2422 		 */
   2423 		if (!more_dirs) {
   2424 			nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
   2425 			more_dirs = (fxdr_unsigned(int, *tl) == 0);
   2426 		}
   2427 		m_freem(mrep);
   2428 	}
   2429 	/*
   2430 	 * Fill last record, iff any, out to a multiple of NFS_DIRFRAGSIZ
   2431 	 * by increasing d_reclen for the last record.
   2432 	 */
   2433 	if (blksiz > 0) {
   2434 		left = NFS_DIRFRAGSIZ - blksiz;
   2435 		dp->d_reclen += left;
   2436 		NFS_STASHCOOKIE(dp, uiop->uio_offset);
   2437 		(caddr_t)uiop->uio_iov->iov_base += left;
   2438 		uiop->uio_iov->iov_len -= left;
   2439 		uiop->uio_resid -= left;
   2440 	}
   2441 
   2442 	/*
   2443 	 * We are now either at the end of the directory or have filled the
   2444 	 * block.
   2445 	 */
   2446 	if (bigenough)
   2447 		dnp->n_direofoffset = uiop->uio_offset;
   2448 nfsmout:
   2449 	if (newvp != NULLVP)
   2450 		vrele(newvp);
   2451 	return (error);
   2452 }
   2453 static char hextoasc[] = "0123456789abcdef";
   2454 
   2455 /*
   2456  * Silly rename. To make the NFS filesystem that is stateless look a little
   2457  * more like the "ufs" a remove of an active vnode is translated to a rename
   2458  * to a funny looking filename that is removed by nfs_inactive on the
   2459  * nfsnode. There is the potential for another process on a different client
   2460  * to create the same funny name between the nfs_lookitup() fails and the
   2461  * nfs_rename() completes, but...
   2462  */
   2463 int
   2464 nfs_sillyrename(dvp, vp, cnp)
   2465 	struct vnode *dvp, *vp;
   2466 	struct componentname *cnp;
   2467 {
   2468 	struct sillyrename *sp;
   2469 	struct nfsnode *np;
   2470 	int error;
   2471 	short pid;
   2472 
   2473 	cache_purge(dvp);
   2474 	np = VTONFS(vp);
   2475 #ifndef DIAGNOSTIC
   2476 	if (vp->v_type == VDIR)
   2477 		panic("nfs: sillyrename dir");
   2478 #endif
   2479 	MALLOC(sp, struct sillyrename *, sizeof (struct sillyrename),
   2480 		M_NFSREQ, M_WAITOK);
   2481 	sp->s_cred = crdup(cnp->cn_cred);
   2482 	sp->s_dvp = dvp;
   2483 	VREF(dvp);
   2484 
   2485 	/* Fudge together a funny name */
   2486 	pid = cnp->cn_proc->p_pid;
   2487 	memcpy(sp->s_name, ".nfsAxxxx4.4", 13);
   2488 	sp->s_namlen = 12;
   2489 	sp->s_name[8] = hextoasc[pid & 0xf];
   2490 	sp->s_name[7] = hextoasc[(pid >> 4) & 0xf];
   2491 	sp->s_name[6] = hextoasc[(pid >> 8) & 0xf];
   2492 	sp->s_name[5] = hextoasc[(pid >> 12) & 0xf];
   2493 
   2494 	/* Try lookitups until we get one that isn't there */
   2495 	while (nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
   2496 		cnp->cn_proc, (struct nfsnode **)0) == 0) {
   2497 		sp->s_name[4]++;
   2498 		if (sp->s_name[4] > 'z') {
   2499 			error = EINVAL;
   2500 			goto bad;
   2501 		}
   2502 	}
   2503 	error = nfs_renameit(dvp, cnp, sp);
   2504 	if (error)
   2505 		goto bad;
   2506 	error = nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
   2507 		cnp->cn_proc, &np);
   2508 	np->n_sillyrename = sp;
   2509 	return (0);
   2510 bad:
   2511 	vrele(sp->s_dvp);
   2512 	crfree(sp->s_cred);
   2513 	free((caddr_t)sp, M_NFSREQ);
   2514 	return (error);
   2515 }
   2516 
   2517 /*
   2518  * Look up a file name and optionally either update the file handle or
   2519  * allocate an nfsnode, depending on the value of npp.
   2520  * npp == NULL	--> just do the lookup
   2521  * *npp == NULL --> allocate a new nfsnode and make sure attributes are
   2522  *			handled too
   2523  * *npp != NULL --> update the file handle in the vnode
   2524  */
   2525 int
   2526 nfs_lookitup(dvp, name, len, cred, procp, npp)
   2527 	struct vnode *dvp;
   2528 	const char *name;
   2529 	int len;
   2530 	struct ucred *cred;
   2531 	struct proc *procp;
   2532 	struct nfsnode **npp;
   2533 {
   2534 	u_int32_t *tl;
   2535 	caddr_t cp;
   2536 	int32_t t1, t2;
   2537 	struct vnode *newvp = (struct vnode *)0;
   2538 	struct nfsnode *np, *dnp = VTONFS(dvp);
   2539 	caddr_t bpos, dpos, cp2;
   2540 	int error = 0, fhlen, attrflag;
   2541 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
   2542 	nfsfh_t *nfhp;
   2543 	const int v3 = NFS_ISV3(dvp);
   2544 
   2545 	nfsstats.rpccnt[NFSPROC_LOOKUP]++;
   2546 	nfsm_reqhead(dvp, NFSPROC_LOOKUP,
   2547 		NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len));
   2548 	nfsm_fhtom(dvp, v3);
   2549 	nfsm_strtom(name, len, NFS_MAXNAMLEN);
   2550 	nfsm_request(dvp, NFSPROC_LOOKUP, procp, cred);
   2551 	if (npp && !error) {
   2552 		nfsm_getfh(nfhp, fhlen, v3);
   2553 		if (*npp) {
   2554 		    np = *npp;
   2555 		    if (np->n_fhsize > NFS_SMALLFH && fhlen <= NFS_SMALLFH) {
   2556 			free((caddr_t)np->n_fhp, M_NFSBIGFH);
   2557 			np->n_fhp = &np->n_fh;
   2558 		    } else if (np->n_fhsize <= NFS_SMALLFH && fhlen>NFS_SMALLFH)
   2559 			np->n_fhp =(nfsfh_t *)malloc(fhlen,M_NFSBIGFH,M_WAITOK);
   2560 		    memcpy((caddr_t)np->n_fhp, (caddr_t)nfhp, fhlen);
   2561 		    np->n_fhsize = fhlen;
   2562 		    newvp = NFSTOV(np);
   2563 		} else if (NFS_CMPFH(dnp, nfhp, fhlen)) {
   2564 		    VREF(dvp);
   2565 		    newvp = dvp;
   2566 		} else {
   2567 		    error = nfs_nget(dvp->v_mount, nfhp, fhlen, &np);
   2568 		    if (error) {
   2569 			m_freem(mrep);
   2570 			return (error);
   2571 		    }
   2572 		    newvp = NFSTOV(np);
   2573 		}
   2574 		if (v3) {
   2575 			nfsm_postop_attr(newvp, attrflag);
   2576 			if (!attrflag && *npp == NULL) {
   2577 				m_freem(mrep);
   2578 				vrele(newvp);
   2579 				return (ENOENT);
   2580 			}
   2581 		} else
   2582 			nfsm_loadattr(newvp, (struct vattr *)0);
   2583 	}
   2584 	nfsm_reqdone;
   2585 	if (npp && *npp == NULL) {
   2586 		if (error) {
   2587 			if (newvp)
   2588 				vrele(newvp);
   2589 		} else
   2590 			*npp = np;
   2591 	}
   2592 	return (error);
   2593 }
   2594 
   2595 /*
   2596  * Nfs Version 3 commit rpc
   2597  */
   2598 int
   2599 nfs_commit(vp, offset, cnt, procp)
   2600 	struct vnode *vp;
   2601 	off_t offset;
   2602 	uint32_t cnt;
   2603 	struct proc *procp;
   2604 {
   2605 	caddr_t cp;
   2606 	u_int32_t *tl;
   2607 	int32_t t1, t2;
   2608 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
   2609 	caddr_t bpos, dpos, cp2;
   2610 	int error = 0, wccflag = NFSV3_WCCRATTR;
   2611 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
   2612 
   2613 #ifdef fvdl_debug
   2614 	printf("commit %lu - %lu\n", (unsigned long)offset,
   2615 	    (unsigned long)(offset + cnt));
   2616 #endif
   2617 
   2618 	if ((nmp->nm_iflag & NFSMNT_HASWRITEVERF) == 0)
   2619 		return (0);
   2620 	nfsstats.rpccnt[NFSPROC_COMMIT]++;
   2621 	nfsm_reqhead(vp, NFSPROC_COMMIT, NFSX_FH(1));
   2622 	nfsm_fhtom(vp, 1);
   2623 	nfsm_build(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
   2624 	txdr_hyper(offset, tl);
   2625 	tl += 2;
   2626 	*tl = txdr_unsigned(cnt);
   2627 	nfsm_request(vp, NFSPROC_COMMIT, procp, VTONFS(vp)->n_wcred);
   2628 	nfsm_wcc_data(vp, wccflag);
   2629 	if (!error) {
   2630 		nfsm_dissect(tl, u_int32_t *, NFSX_V3WRITEVERF);
   2631 		if (memcmp((caddr_t)nmp->nm_verf, (caddr_t)tl,
   2632 			NFSX_V3WRITEVERF)) {
   2633 			memcpy((caddr_t)nmp->nm_verf, (caddr_t)tl,
   2634 				NFSX_V3WRITEVERF);
   2635 			error = NFSERR_STALEWRITEVERF;
   2636 		}
   2637 	}
   2638 	nfsm_reqdone;
   2639 	return (error);
   2640 }
   2641 
   2642 /*
   2643  * Kludge City..
   2644  * - make nfs_bmap() essentially a no-op that does no translation
   2645  * - do nfs_strategy() by doing I/O with nfs_readrpc/nfs_writerpc
   2646  *   (Maybe I could use the process's page mapping, but I was concerned that
   2647  *    Kernel Write might not be enabled and also figured copyout() would do
   2648  *    a lot more work than memcpy() and also it currently happens in the
   2649  *    context of the swapper process (2).
   2650  */
   2651 int
   2652 nfs_bmap(v)
   2653 	void *v;
   2654 {
   2655 	struct vop_bmap_args /* {
   2656 		struct vnode *a_vp;
   2657 		daddr_t  a_bn;
   2658 		struct vnode **a_vpp;
   2659 		daddr_t *a_bnp;
   2660 		int *a_runp;
   2661 	} */ *ap = v;
   2662 	struct vnode *vp = ap->a_vp;
   2663 
   2664 	if (ap->a_vpp != NULL)
   2665 		*ap->a_vpp = vp;
   2666 	if (ap->a_bnp != NULL)
   2667 		*ap->a_bnp = ap->a_bn * btodb(vp->v_mount->mnt_stat.f_iosize);
   2668 	return (0);
   2669 }
   2670 
   2671 /*
   2672  * Strategy routine.
   2673  * For async requests when nfsiod(s) are running, queue the request by
   2674  * calling nfs_asyncio(), otherwise just all nfs_doio() to do the
   2675  * request.
   2676  */
   2677 int
   2678 nfs_strategy(v)
   2679 	void *v;
   2680 {
   2681 	struct vop_strategy_args *ap = v;
   2682 	struct buf *bp = ap->a_bp;
   2683 	struct proc *p;
   2684 	int error = 0;
   2685 
   2686 	if ((bp->b_flags & (B_PHYS|B_ASYNC)) == (B_PHYS|B_ASYNC))
   2687 		panic("nfs physio/async");
   2688 	if (bp->b_flags & B_ASYNC)
   2689 		p = NULL;
   2690 	else
   2691 		p = curproc;	/* XXX */
   2692 
   2693 	/*
   2694 	 * If the op is asynchronous and an i/o daemon is waiting
   2695 	 * queue the request, wake it up and wait for completion
   2696 	 * otherwise just do it ourselves.
   2697 	 */
   2698 
   2699 	if ((bp->b_flags & B_ASYNC) == 0 ||
   2700 	    nfs_asyncio(bp))
   2701 		error = nfs_doio(bp, p);
   2702 	return (error);
   2703 }
   2704 
   2705 /*
   2706  * Mmap a file
   2707  *
   2708  * NB Currently unsupported.
   2709  */
   2710 /* ARGSUSED */
   2711 int
   2712 nfs_mmap(v)
   2713 	void *v;
   2714 {
   2715 #if 0
   2716 	struct vop_mmap_args /* {
   2717 		struct vnode *a_vp;
   2718 		int a_fflags;
   2719 		struct ucred *a_cred;
   2720 		struct proc *a_p;
   2721 	} */ *ap = v;
   2722 #endif
   2723 
   2724 	return (EINVAL);
   2725 }
   2726 
   2727 /*
   2728  * fsync vnode op. Just call nfs_flush() with commit == 1.
   2729  */
   2730 /* ARGSUSED */
   2731 int
   2732 nfs_fsync(v)
   2733 	void *v;
   2734 {
   2735 	struct vop_fsync_args /* {
   2736 		struct vnodeop_desc *a_desc;
   2737 		struct vnode * a_vp;
   2738 		struct ucred * a_cred;
   2739 		int  a_flags;
   2740 		off_t offlo;
   2741 		off_t offhi;
   2742 		struct proc * a_p;
   2743 	} */ *ap = v;
   2744 
   2745 	return (nfs_flush(ap->a_vp, ap->a_cred,
   2746 	    (ap->a_flags & FSYNC_WAIT) != 0 ? MNT_WAIT : 0, ap->a_p, 1));
   2747 }
   2748 
   2749 /*
   2750  * Flush all the data associated with a vnode.
   2751  */
   2752 int
   2753 nfs_flush(vp, cred, waitfor, p, commit)
   2754 	struct vnode *vp;
   2755 	struct ucred *cred;
   2756 	int waitfor;
   2757 	struct proc *p;
   2758 	int commit;
   2759 {
   2760 	struct uvm_object *uobj = &vp->v_uvm.u_obj;
   2761 	struct nfsnode *np = VTONFS(vp);
   2762 	int error;
   2763 	int flushflags = PGO_ALLPAGES|PGO_CLEANIT|PGO_SYNCIO;
   2764 	int rv;
   2765 	UVMHIST_FUNC("nfs_flush"); UVMHIST_CALLED(ubchist);
   2766 
   2767 	error = 0;
   2768 	simple_lock(&uobj->vmobjlock);
   2769 	rv = (uobj->pgops->pgo_flush)(uobj, 0, 0, flushflags);
   2770 	simple_unlock(&uobj->vmobjlock);
   2771 	if (!rv) {
   2772 		error = EIO;
   2773 	}
   2774 	if (np->n_flag & NWRITEERR) {
   2775 		error = np->n_error;
   2776 		np->n_flag &= ~NWRITEERR;
   2777 	}
   2778 	UVMHIST_LOG(ubchist, "returning %d", error,0,0,0);
   2779 	return (error);
   2780 }
   2781 
   2782 /*
   2783  * Return POSIX pathconf information applicable to nfs.
   2784  *
   2785  * N.B. The NFS V2 protocol doesn't support this RPC.
   2786  */
   2787 /* ARGSUSED */
   2788 int
   2789 nfs_pathconf(v)
   2790 	void *v;
   2791 {
   2792 	struct vop_pathconf_args /* {
   2793 		struct vnode *a_vp;
   2794 		int a_name;
   2795 		register_t *a_retval;
   2796 	} */ *ap = v;
   2797 	struct nfsv3_pathconf *pcp;
   2798 	struct vnode *vp = ap->a_vp;
   2799 	struct nfsmount *nmp;
   2800 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
   2801 	int32_t t1, t2;
   2802 	u_int32_t *tl;
   2803 	caddr_t bpos, dpos, cp, cp2;
   2804 	int error = 0, attrflag;
   2805 	unsigned int l;
   2806 	u_int64_t maxsize;
   2807 	const int v3 = NFS_ISV3(vp);
   2808 
   2809 	switch (ap->a_name) {
   2810 		/* Names that can be resolved locally. */
   2811 	case _PC_PIPE_BUF:
   2812 		*ap->a_retval = PIPE_BUF;
   2813 		break;
   2814 	case _PC_SYNC_IO:
   2815 		*ap->a_retval = 1;
   2816 		break;
   2817 	/* Names that cannot be resolved locally; do an RPC, if possible. */
   2818 	case _PC_LINK_MAX:
   2819 	case _PC_NAME_MAX:
   2820 	case _PC_CHOWN_RESTRICTED:
   2821 	case _PC_NO_TRUNC:
   2822 		if (!v3) {
   2823 			error = EINVAL;
   2824 			break;
   2825 		}
   2826 		nfsstats.rpccnt[NFSPROC_PATHCONF]++;
   2827 		nfsm_reqhead(vp, NFSPROC_PATHCONF, NFSX_FH(1));
   2828 		nfsm_fhtom(vp, 1);
   2829 		nfsm_request(vp, NFSPROC_PATHCONF,
   2830 		    curproc, curproc->p_ucred);	/* XXX */
   2831 		nfsm_postop_attr(vp, attrflag);
   2832 		if (!error) {
   2833 			nfsm_dissect(pcp, struct nfsv3_pathconf *,
   2834 			    NFSX_V3PATHCONF);
   2835 			switch (ap->a_name) {
   2836 			case _PC_LINK_MAX:
   2837 				*ap->a_retval =
   2838 				    fxdr_unsigned(register_t, pcp->pc_linkmax);
   2839 				break;
   2840 			case _PC_NAME_MAX:
   2841 				*ap->a_retval =
   2842 				    fxdr_unsigned(register_t, pcp->pc_namemax);
   2843 				break;
   2844 			case _PC_CHOWN_RESTRICTED:
   2845 				*ap->a_retval =
   2846 				    (pcp->pc_chownrestricted == nfs_true);
   2847 				break;
   2848 			case _PC_NO_TRUNC:
   2849 				*ap->a_retval =
   2850 				    (pcp->pc_notrunc == nfs_true);
   2851 				break;
   2852 			}
   2853 		}
   2854 		nfsm_reqdone;
   2855 		break;
   2856 	case _PC_FILESIZEBITS:
   2857 		if (v3) {
   2858 			nmp = VFSTONFS(vp->v_mount);
   2859 			if ((nmp->nm_iflag & NFSMNT_GOTFSINFO) == 0)
   2860 				if ((error = nfs_fsinfo(nmp, vp,
   2861 				    curproc->p_ucred, curproc)) != 0) /* XXX */
   2862 					break;
   2863 			for (l = 0, maxsize = nmp->nm_maxfilesize;
   2864 			    (maxsize >> l) > 0; l++)
   2865 				;
   2866 			*ap->a_retval = l + 1;
   2867 		} else {
   2868 			*ap->a_retval = 32;	/* NFS V2 limitation */
   2869 		}
   2870 		break;
   2871 	default:
   2872 		error = EINVAL;
   2873 		break;
   2874 	}
   2875 
   2876 	return (error);
   2877 }
   2878 
   2879 /*
   2880  * NFS advisory byte-level locks.
   2881  */
   2882 int
   2883 nfs_advlock(v)
   2884 	void *v;
   2885 {
   2886 	struct vop_advlock_args /* {
   2887 		struct vnode *a_vp;
   2888 		caddr_t  a_id;
   2889 		int  a_op;
   2890 		struct flock *a_fl;
   2891 		int  a_flags;
   2892 	} */ *ap = v;
   2893 	struct nfsnode *np = VTONFS(ap->a_vp);
   2894 
   2895 	return lf_advlock(ap, &np->n_lockf, np->n_size);
   2896 }
   2897 
   2898 /*
   2899  * Print out the contents of an nfsnode.
   2900  */
   2901 int
   2902 nfs_print(v)
   2903 	void *v;
   2904 {
   2905 	struct vop_print_args /* {
   2906 		struct vnode *a_vp;
   2907 	} */ *ap = v;
   2908 	struct vnode *vp = ap->a_vp;
   2909 	struct nfsnode *np = VTONFS(vp);
   2910 
   2911 	printf("tag VT_NFS, fileid %ld fsid 0x%lx",
   2912 	    np->n_vattr->va_fileid, np->n_vattr->va_fsid);
   2913 	if (vp->v_type == VFIFO)
   2914 		fifo_printinfo(vp);
   2915 	printf("\n");
   2916 	return (0);
   2917 }
   2918 
   2919 /*
   2920  * NFS file truncation.
   2921  */
   2922 int
   2923 nfs_truncate(v)
   2924 	void *v;
   2925 {
   2926 #if 0
   2927 	struct vop_truncate_args /* {
   2928 		struct vnode *a_vp;
   2929 		off_t a_length;
   2930 		int a_flags;
   2931 		struct ucred *a_cred;
   2932 		struct proc *a_p;
   2933 	} */ *ap = v;
   2934 #endif
   2935 
   2936 	/* Use nfs_setattr */
   2937 	return (EOPNOTSUPP);
   2938 }
   2939 
   2940 /*
   2941  * NFS update.
   2942  */
   2943 int
   2944 nfs_update(v)
   2945 	void *v;
   2946 #if 0
   2947 	struct vop_update_args /* {
   2948 		struct vnode *a_vp;
   2949 		struct timespec *a_ta;
   2950 		struct timespec *a_tm;
   2951 		int a_waitfor;
   2952 	} */ *ap = v;
   2953 #endif
   2954 {
   2955 
   2956 	/* Use nfs_setattr */
   2957 	return (EOPNOTSUPP);
   2958 }
   2959 
   2960 /*
   2961  * Just call bwrite().
   2962  */
   2963 int
   2964 nfs_bwrite(v)
   2965 	void *v;
   2966 {
   2967 	struct vop_bwrite_args /* {
   2968 		struct vnode *a_bp;
   2969 	} */ *ap = v;
   2970 
   2971 	return (bwrite(ap->a_bp));
   2972 }
   2973 
   2974 /*
   2975  * nfs special file access vnode op.
   2976  * Essentially just get vattr and then imitate iaccess() since the device is
   2977  * local to the client.
   2978  */
   2979 int
   2980 nfsspec_access(v)
   2981 	void *v;
   2982 {
   2983 	struct vop_access_args /* {
   2984 		struct vnode *a_vp;
   2985 		int  a_mode;
   2986 		struct ucred *a_cred;
   2987 		struct proc *a_p;
   2988 	} */ *ap = v;
   2989 	struct vattr va;
   2990 	struct vnode *vp = ap->a_vp;
   2991 	int error;
   2992 
   2993 	error = VOP_GETATTR(vp, &va, ap->a_cred, ap->a_p);
   2994 	if (error)
   2995 		return (error);
   2996 
   2997         /*
   2998 	 * Disallow write attempts on filesystems mounted read-only;
   2999 	 * unless the file is a socket, fifo, or a block or character
   3000 	 * device resident on the filesystem.
   3001 	 */
   3002 	if ((ap->a_mode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY)) {
   3003 		switch (vp->v_type) {
   3004 		case VREG:
   3005 		case VDIR:
   3006 		case VLNK:
   3007 			return (EROFS);
   3008 		default:
   3009 			break;
   3010 		}
   3011 	}
   3012 
   3013 	return (vaccess(va.va_type, va.va_mode,
   3014 	    va.va_uid, va.va_gid, ap->a_mode, ap->a_cred));
   3015 }
   3016 
   3017 /*
   3018  * Read wrapper for special devices.
   3019  */
   3020 int
   3021 nfsspec_read(v)
   3022 	void *v;
   3023 {
   3024 	struct vop_read_args /* {
   3025 		struct vnode *a_vp;
   3026 		struct uio *a_uio;
   3027 		int  a_ioflag;
   3028 		struct ucred *a_cred;
   3029 	} */ *ap = v;
   3030 	struct nfsnode *np = VTONFS(ap->a_vp);
   3031 
   3032 	/*
   3033 	 * Set access flag.
   3034 	 */
   3035 	np->n_flag |= NACC;
   3036 	np->n_atim.tv_sec = time.tv_sec;
   3037 	np->n_atim.tv_nsec = time.tv_usec * 1000;
   3038 	return (VOCALL(spec_vnodeop_p, VOFFSET(vop_read), ap));
   3039 }
   3040 
   3041 /*
   3042  * Write wrapper for special devices.
   3043  */
   3044 int
   3045 nfsspec_write(v)
   3046 	void *v;
   3047 {
   3048 	struct vop_write_args /* {
   3049 		struct vnode *a_vp;
   3050 		struct uio *a_uio;
   3051 		int  a_ioflag;
   3052 		struct ucred *a_cred;
   3053 	} */ *ap = v;
   3054 	struct nfsnode *np = VTONFS(ap->a_vp);
   3055 
   3056 	/*
   3057 	 * Set update flag.
   3058 	 */
   3059 	np->n_flag |= NUPD;
   3060 	np->n_mtim.tv_sec = time.tv_sec;
   3061 	np->n_mtim.tv_nsec = time.tv_usec * 1000;
   3062 	return (VOCALL(spec_vnodeop_p, VOFFSET(vop_write), ap));
   3063 }
   3064 
   3065 /*
   3066  * Close wrapper for special devices.
   3067  *
   3068  * Update the times on the nfsnode then do device close.
   3069  */
   3070 int
   3071 nfsspec_close(v)
   3072 	void *v;
   3073 {
   3074 	struct vop_close_args /* {
   3075 		struct vnode *a_vp;
   3076 		int  a_fflag;
   3077 		struct ucred *a_cred;
   3078 		struct proc *a_p;
   3079 	} */ *ap = v;
   3080 	struct vnode *vp = ap->a_vp;
   3081 	struct nfsnode *np = VTONFS(vp);
   3082 	struct vattr vattr;
   3083 
   3084 	if (np->n_flag & (NACC | NUPD)) {
   3085 		np->n_flag |= NCHG;
   3086 		if (vp->v_usecount == 1 &&
   3087 		    (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
   3088 			VATTR_NULL(&vattr);
   3089 			if (np->n_flag & NACC)
   3090 				vattr.va_atime = np->n_atim;
   3091 			if (np->n_flag & NUPD)
   3092 				vattr.va_mtime = np->n_mtim;
   3093 			(void)VOP_SETATTR(vp, &vattr, ap->a_cred, ap->a_p);
   3094 		}
   3095 	}
   3096 	return (VOCALL(spec_vnodeop_p, VOFFSET(vop_close), ap));
   3097 }
   3098 
   3099 /*
   3100  * Read wrapper for fifos.
   3101  */
   3102 int
   3103 nfsfifo_read(v)
   3104 	void *v;
   3105 {
   3106 	struct vop_read_args /* {
   3107 		struct vnode *a_vp;
   3108 		struct uio *a_uio;
   3109 		int  a_ioflag;
   3110 		struct ucred *a_cred;
   3111 	} */ *ap = v;
   3112 	struct nfsnode *np = VTONFS(ap->a_vp);
   3113 
   3114 	/*
   3115 	 * Set access flag.
   3116 	 */
   3117 	np->n_flag |= NACC;
   3118 	np->n_atim.tv_sec = time.tv_sec;
   3119 	np->n_atim.tv_nsec = time.tv_usec * 1000;
   3120 	return (VOCALL(fifo_vnodeop_p, VOFFSET(vop_read), ap));
   3121 }
   3122 
   3123 /*
   3124  * Write wrapper for fifos.
   3125  */
   3126 int
   3127 nfsfifo_write(v)
   3128 	void *v;
   3129 {
   3130 	struct vop_write_args /* {
   3131 		struct vnode *a_vp;
   3132 		struct uio *a_uio;
   3133 		int  a_ioflag;
   3134 		struct ucred *a_cred;
   3135 	} */ *ap = v;
   3136 	struct nfsnode *np = VTONFS(ap->a_vp);
   3137 
   3138 	/*
   3139 	 * Set update flag.
   3140 	 */
   3141 	np->n_flag |= NUPD;
   3142 	np->n_mtim.tv_sec = time.tv_sec;
   3143 	np->n_mtim.tv_nsec = time.tv_usec * 1000;
   3144 	return (VOCALL(fifo_vnodeop_p, VOFFSET(vop_write), ap));
   3145 }
   3146 
   3147 /*
   3148  * Close wrapper for fifos.
   3149  *
   3150  * Update the times on the nfsnode then do fifo close.
   3151  */
   3152 int
   3153 nfsfifo_close(v)
   3154 	void *v;
   3155 {
   3156 	struct vop_close_args /* {
   3157 		struct vnode *a_vp;
   3158 		int  a_fflag;
   3159 		struct ucred *a_cred;
   3160 		struct proc *a_p;
   3161 	} */ *ap = v;
   3162 	struct vnode *vp = ap->a_vp;
   3163 	struct nfsnode *np = VTONFS(vp);
   3164 	struct vattr vattr;
   3165 
   3166 	if (np->n_flag & (NACC | NUPD)) {
   3167 		if (np->n_flag & NACC) {
   3168 			np->n_atim.tv_sec = time.tv_sec;
   3169 			np->n_atim.tv_nsec = time.tv_usec * 1000;
   3170 		}
   3171 		if (np->n_flag & NUPD) {
   3172 			np->n_mtim.tv_sec = time.tv_sec;
   3173 			np->n_mtim.tv_nsec = time.tv_usec * 1000;
   3174 		}
   3175 		np->n_flag |= NCHG;
   3176 		if (vp->v_usecount == 1 &&
   3177 		    (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
   3178 			VATTR_NULL(&vattr);
   3179 			if (np->n_flag & NACC)
   3180 				vattr.va_atime = np->n_atim;
   3181 			if (np->n_flag & NUPD)
   3182 				vattr.va_mtime = np->n_mtim;
   3183 			(void)VOP_SETATTR(vp, &vattr, ap->a_cred, ap->a_p);
   3184 		}
   3185 	}
   3186 	return (VOCALL(fifo_vnodeop_p, VOFFSET(vop_close), ap));
   3187 }
   3188