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