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