xref: /netbsd-src/sys/fs/nfs/client/nfs_clrpcops.c (revision 481d3881954fd794ca5f2d880b68c53a5db8620e)
1*481d3881Srin /*	$NetBSD: nfs_clrpcops.c,v 1.4 2024/07/05 04:31:52 rin Exp $	*/
26ca35587Sdholland /*-
36ca35587Sdholland  * Copyright (c) 1989, 1993
46ca35587Sdholland  *	The Regents of the University of California.  All rights reserved.
56ca35587Sdholland  *
66ca35587Sdholland  * This code is derived from software contributed to Berkeley by
76ca35587Sdholland  * Rick Macklem at The University of Guelph.
86ca35587Sdholland  *
96ca35587Sdholland  * Redistribution and use in source and binary forms, with or without
106ca35587Sdholland  * modification, are permitted provided that the following conditions
116ca35587Sdholland  * are met:
126ca35587Sdholland  * 1. Redistributions of source code must retain the above copyright
136ca35587Sdholland  *    notice, this list of conditions and the following disclaimer.
146ca35587Sdholland  * 2. Redistributions in binary form must reproduce the above copyright
156ca35587Sdholland  *    notice, this list of conditions and the following disclaimer in the
166ca35587Sdholland  *    documentation and/or other materials provided with the distribution.
176ca35587Sdholland  * 4. Neither the name of the University nor the names of its contributors
186ca35587Sdholland  *    may be used to endorse or promote products derived from this software
196ca35587Sdholland  *    without specific prior written permission.
206ca35587Sdholland  *
216ca35587Sdholland  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
226ca35587Sdholland  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
236ca35587Sdholland  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
246ca35587Sdholland  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
256ca35587Sdholland  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
266ca35587Sdholland  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
276ca35587Sdholland  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
286ca35587Sdholland  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
296ca35587Sdholland  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
306ca35587Sdholland  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
316ca35587Sdholland  * SUCH DAMAGE.
326ca35587Sdholland  *
336ca35587Sdholland  */
346ca35587Sdholland 
356ca35587Sdholland #include <sys/cdefs.h>
36e81f0ea2Spgoyette /* __FBSDID("FreeBSD: head/sys/fs/nfsclient/nfs_clrpcops.c 298788 2016-04-29 16:07:25Z pfg "); */
37*481d3881Srin __RCSID("$NetBSD: nfs_clrpcops.c,v 1.4 2024/07/05 04:31:52 rin Exp $");
386ca35587Sdholland 
396ca35587Sdholland /*
406ca35587Sdholland  * Rpc op calls, generally called from the vnode op calls or through the
416ca35587Sdholland  * buffer cache, for NFS v2, 3 and 4.
426ca35587Sdholland  * These do not normally make any changes to vnode arguments or use
436ca35587Sdholland  * structures that might change between the VFS variants. The returned
446ca35587Sdholland  * arguments are all at the end, after the NFSPROC_T *p one.
456ca35587Sdholland  */
466ca35587Sdholland 
476ca35587Sdholland #ifndef APPLEKEXT
483be2222bSpgoyette #ifdef _KERNEL_OPT
496ca35587Sdholland #include "opt_inet6.h"
503be2222bSpgoyette #endif
516ca35587Sdholland 
523be2222bSpgoyette #include <fs/nfs/common/nfsport.h>
53e81f0ea2Spgoyette #include <sys/sysctl.h>
54e81f0ea2Spgoyette 
55e81f0ea2Spgoyette SYSCTL_DECL(_vfs_nfs);
56e81f0ea2Spgoyette 
57e81f0ea2Spgoyette static int	nfsignore_eexist = 0;
58e81f0ea2Spgoyette SYSCTL_INT(_vfs_nfs, OID_AUTO, ignore_eexist, CTLFLAG_RW,
59e81f0ea2Spgoyette     &nfsignore_eexist, 0, "NFS ignore EEXIST replies for mkdir/symlink");
606ca35587Sdholland 
616ca35587Sdholland /*
626ca35587Sdholland  * Global variables
636ca35587Sdholland  */
646ca35587Sdholland extern int nfs_numnfscbd;
656ca35587Sdholland extern struct timeval nfsboottime;
666ca35587Sdholland extern u_int32_t newnfs_false, newnfs_true;
676ca35587Sdholland extern nfstype nfsv34_type[9];
686ca35587Sdholland extern int nfsrv_useacl;
696ca35587Sdholland extern char nfsv4_callbackaddr[INET6_ADDRSTRLEN];
706ca35587Sdholland extern int nfscl_debuglevel;
716ca35587Sdholland NFSCLSTATEMUTEX;
726ca35587Sdholland int nfstest_outofseq = 0;
736ca35587Sdholland int nfscl_assumeposixlocks = 1;
746ca35587Sdholland int nfscl_enablecallb = 0;
756ca35587Sdholland short nfsv4_cbport = NFSV4_CBPORT;
766ca35587Sdholland int nfstest_openallsetattr = 0;
776ca35587Sdholland #endif	/* !APPLEKEXT */
786ca35587Sdholland 
796ca35587Sdholland #define	DIRHDSIZ	(sizeof (struct dirent) - (MAXNAMLEN + 1))
806ca35587Sdholland 
816ca35587Sdholland /*
826ca35587Sdholland  * nfscl_getsameserver() can return one of three values:
836ca35587Sdholland  * NFSDSP_USETHISSESSION - Use this session for the DS.
846ca35587Sdholland  * NFSDSP_SEQTHISSESSION - Use the nfsclds_sequence field of this dsp for new
856ca35587Sdholland  *     session.
866ca35587Sdholland  * NFSDSP_NOTFOUND - No matching server was found.
876ca35587Sdholland  */
886ca35587Sdholland enum nfsclds_state {
896ca35587Sdholland 	NFSDSP_USETHISSESSION = 0,
906ca35587Sdholland 	NFSDSP_SEQTHISSESSION = 1,
916ca35587Sdholland 	NFSDSP_NOTFOUND = 2,
926ca35587Sdholland };
936ca35587Sdholland 
946ca35587Sdholland static int nfsrpc_setattrrpc(vnode_t , struct vattr *, nfsv4stateid_t *,
956ca35587Sdholland     struct ucred *, NFSPROC_T *, struct nfsvattr *, int *, void *);
966ca35587Sdholland static int nfsrpc_readrpc(vnode_t , struct uio *, struct ucred *,
976ca35587Sdholland     nfsv4stateid_t *, NFSPROC_T *, struct nfsvattr *, int *, void *);
986ca35587Sdholland static int nfsrpc_writerpc(vnode_t , struct uio *, int *, int *,
996ca35587Sdholland     struct ucred *, nfsv4stateid_t *, NFSPROC_T *, struct nfsvattr *, int *,
1006ca35587Sdholland     void *);
1016ca35587Sdholland static int nfsrpc_createv23(vnode_t , char *, int, struct vattr *,
1026ca35587Sdholland     nfsquad_t, int, struct ucred *, NFSPROC_T *, struct nfsvattr *,
1036ca35587Sdholland     struct nfsvattr *, struct nfsfh **, int *, int *, void *);
1046ca35587Sdholland static int nfsrpc_createv4(vnode_t , char *, int, struct vattr *,
1056ca35587Sdholland     nfsquad_t, int, struct nfsclowner *, struct nfscldeleg **, struct ucred *,
1066ca35587Sdholland     NFSPROC_T *, struct nfsvattr *, struct nfsvattr *, struct nfsfh **, int *,
1076ca35587Sdholland     int *, void *, int *);
1086ca35587Sdholland static int nfsrpc_locku(struct nfsrv_descript *, struct nfsmount *,
1096ca35587Sdholland     struct nfscllockowner *, u_int64_t, u_int64_t,
1106ca35587Sdholland     u_int32_t, struct ucred *, NFSPROC_T *, int);
1116ca35587Sdholland static int nfsrpc_setaclrpc(vnode_t, struct ucred *, NFSPROC_T *,
1126ca35587Sdholland     struct acl *, nfsv4stateid_t *, void *);
1136ca35587Sdholland static int nfsrpc_getlayout(struct nfsmount *, vnode_t, struct nfsfh *, int,
1146ca35587Sdholland     uint32_t *, nfsv4stateid_t *, uint64_t, struct nfscllayout **,
1156ca35587Sdholland     struct ucred *, NFSPROC_T *);
1166ca35587Sdholland static int nfsrpc_fillsa(struct nfsmount *, struct sockaddr_storage *,
1176ca35587Sdholland     struct nfsclds **, NFSPROC_T *);
1186ca35587Sdholland static void nfscl_initsessionslots(struct nfsclsession *);
1196ca35587Sdholland static int nfscl_doflayoutio(vnode_t, struct uio *, int *, int *, int *,
1206ca35587Sdholland     nfsv4stateid_t *, int, struct nfscldevinfo *, struct nfscllayout *,
1216ca35587Sdholland     struct nfsclflayout *, uint64_t, uint64_t, struct ucred *, NFSPROC_T *);
1226ca35587Sdholland static int nfsrpc_readds(vnode_t, struct uio *, nfsv4stateid_t *, int *,
1236ca35587Sdholland     struct nfsclds *, uint64_t, int, struct nfsfh *, struct ucred *,
1246ca35587Sdholland     NFSPROC_T *);
1256ca35587Sdholland static int nfsrpc_writeds(vnode_t, struct uio *, int *, int *,
1266ca35587Sdholland     nfsv4stateid_t *, struct nfsclds *, uint64_t, int,
1276ca35587Sdholland     struct nfsfh *, int, struct ucred *, NFSPROC_T *);
1286ca35587Sdholland static enum nfsclds_state nfscl_getsameserver(struct nfsmount *,
1296ca35587Sdholland     struct nfsclds *, struct nfsclds **);
1306ca35587Sdholland #ifdef notyet
1316ca35587Sdholland static int nfsrpc_commitds(vnode_t, uint64_t, int, struct nfsclds *,
1326ca35587Sdholland     struct nfsfh *, struct ucred *, NFSPROC_T *, void *);
1336ca35587Sdholland #endif
1346ca35587Sdholland 
1356ca35587Sdholland /*
1366ca35587Sdholland  * nfs null call from vfs.
1376ca35587Sdholland  */
1386ca35587Sdholland APPLESTATIC int
nfsrpc_null(vnode_t vp,struct ucred * cred,NFSPROC_T * p)1396ca35587Sdholland nfsrpc_null(vnode_t vp, struct ucred *cred, NFSPROC_T *p)
1406ca35587Sdholland {
1416ca35587Sdholland 	int error;
1426ca35587Sdholland 	struct nfsrv_descript nfsd, *nd = &nfsd;
1436ca35587Sdholland 
1446ca35587Sdholland 	NFSCL_REQSTART(nd, NFSPROC_NULL, vp);
1456ca35587Sdholland 	error = nfscl_request(nd, vp, p, cred, NULL);
1466ca35587Sdholland 	if (nd->nd_repstat && !error)
1476ca35587Sdholland 		error = nd->nd_repstat;
1486ca35587Sdholland 	mbuf_freem(nd->nd_mrep);
1496ca35587Sdholland 	return (error);
1506ca35587Sdholland }
1516ca35587Sdholland 
1526ca35587Sdholland /*
1536ca35587Sdholland  * nfs access rpc op.
1546ca35587Sdholland  * For nfs version 3 and 4, use the access rpc to check accessibility. If file
1556ca35587Sdholland  * modes are changed on the server, accesses might still fail later.
1566ca35587Sdholland  */
1576ca35587Sdholland APPLESTATIC int
nfsrpc_access(vnode_t vp,int acmode,struct ucred * cred,NFSPROC_T * p,struct nfsvattr * nap,int * attrflagp)1586ca35587Sdholland nfsrpc_access(vnode_t vp, int acmode, struct ucred *cred,
1596ca35587Sdholland     NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp)
1606ca35587Sdholland {
1616ca35587Sdholland 	int error;
1626ca35587Sdholland 	u_int32_t mode, rmode;
1636ca35587Sdholland 
1646ca35587Sdholland 	if (acmode & VREAD)
1656ca35587Sdholland 		mode = NFSACCESS_READ;
1666ca35587Sdholland 	else
1676ca35587Sdholland 		mode = 0;
1686ca35587Sdholland 	if (vnode_vtype(vp) == VDIR) {
1696ca35587Sdholland 		if (acmode & VWRITE)
1706ca35587Sdholland 			mode |= (NFSACCESS_MODIFY | NFSACCESS_EXTEND |
1716ca35587Sdholland 				 NFSACCESS_DELETE);
1726ca35587Sdholland 		if (acmode & VEXEC)
1736ca35587Sdholland 			mode |= NFSACCESS_LOOKUP;
1746ca35587Sdholland 	} else {
1756ca35587Sdholland 		if (acmode & VWRITE)
1766ca35587Sdholland 			mode |= (NFSACCESS_MODIFY | NFSACCESS_EXTEND);
1776ca35587Sdholland 		if (acmode & VEXEC)
1786ca35587Sdholland 			mode |= NFSACCESS_EXECUTE;
1796ca35587Sdholland 	}
1806ca35587Sdholland 
1816ca35587Sdholland 	/*
1826ca35587Sdholland 	 * Now, just call nfsrpc_accessrpc() to do the actual RPC.
1836ca35587Sdholland 	 */
1846ca35587Sdholland 	error = nfsrpc_accessrpc(vp, mode, cred, p, nap, attrflagp, &rmode,
1856ca35587Sdholland 	    NULL);
1866ca35587Sdholland 
1876ca35587Sdholland 	/*
1886ca35587Sdholland 	 * The NFS V3 spec does not clarify whether or not
1896ca35587Sdholland 	 * the returned access bits can be a superset of
1906ca35587Sdholland 	 * the ones requested, so...
1916ca35587Sdholland 	 */
1926ca35587Sdholland 	if (!error && (rmode & mode) != mode)
1936ca35587Sdholland 		error = EACCES;
1946ca35587Sdholland 	return (error);
1956ca35587Sdholland }
1966ca35587Sdholland 
1976ca35587Sdholland /*
1986ca35587Sdholland  * The actual rpc, separated out for Darwin.
1996ca35587Sdholland  */
2006ca35587Sdholland APPLESTATIC int
nfsrpc_accessrpc(vnode_t vp,u_int32_t mode,struct ucred * cred,NFSPROC_T * p,struct nfsvattr * nap,int * attrflagp,u_int32_t * rmodep,void * stuff)2016ca35587Sdholland nfsrpc_accessrpc(vnode_t vp, u_int32_t mode, struct ucred *cred,
2026ca35587Sdholland     NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, u_int32_t *rmodep,
2036ca35587Sdholland     void *stuff)
2046ca35587Sdholland {
2056ca35587Sdholland 	u_int32_t *tl;
2066ca35587Sdholland 	u_int32_t supported, rmode;
2076ca35587Sdholland 	int error;
2086ca35587Sdholland 	struct nfsrv_descript nfsd, *nd = &nfsd;
2096ca35587Sdholland 	nfsattrbit_t attrbits;
2106ca35587Sdholland 
2116ca35587Sdholland 	*attrflagp = 0;
2126ca35587Sdholland 	supported = mode;
2136ca35587Sdholland 	NFSCL_REQSTART(nd, NFSPROC_ACCESS, vp);
2146ca35587Sdholland 	NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2156ca35587Sdholland 	*tl = txdr_unsigned(mode);
2166ca35587Sdholland 	if (nd->nd_flag & ND_NFSV4) {
2176ca35587Sdholland 		/*
2186ca35587Sdholland 		 * And do a Getattr op.
2196ca35587Sdholland 		 */
2206ca35587Sdholland 		NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2216ca35587Sdholland 		*tl = txdr_unsigned(NFSV4OP_GETATTR);
2226ca35587Sdholland 		NFSGETATTR_ATTRBIT(&attrbits);
2236ca35587Sdholland 		(void) nfsrv_putattrbit(nd, &attrbits);
2246ca35587Sdholland 	}
2256ca35587Sdholland 	error = nfscl_request(nd, vp, p, cred, stuff);
2266ca35587Sdholland 	if (error)
2276ca35587Sdholland 		return (error);
2286ca35587Sdholland 	if (nd->nd_flag & ND_NFSV3) {
2296ca35587Sdholland 		error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
2306ca35587Sdholland 		if (error)
2316ca35587Sdholland 			goto nfsmout;
2326ca35587Sdholland 	}
2336ca35587Sdholland 	if (!nd->nd_repstat) {
2346ca35587Sdholland 		if (nd->nd_flag & ND_NFSV4) {
2356ca35587Sdholland 			NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2366ca35587Sdholland 			supported = fxdr_unsigned(u_int32_t, *tl++);
2376ca35587Sdholland 		} else {
2386ca35587Sdholland 			NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2396ca35587Sdholland 		}
2406ca35587Sdholland 		rmode = fxdr_unsigned(u_int32_t, *tl);
2416ca35587Sdholland 		if (nd->nd_flag & ND_NFSV4)
2426ca35587Sdholland 			error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
2436ca35587Sdholland 
2446ca35587Sdholland 		/*
2456ca35587Sdholland 		 * It's not obvious what should be done about
2466ca35587Sdholland 		 * unsupported access modes. For now, be paranoid
2476ca35587Sdholland 		 * and clear the unsupported ones.
2486ca35587Sdholland 		 */
2496ca35587Sdholland 		rmode &= supported;
2506ca35587Sdholland 		*rmodep = rmode;
2516ca35587Sdholland 	} else
2526ca35587Sdholland 		error = nd->nd_repstat;
2536ca35587Sdholland nfsmout:
2546ca35587Sdholland 	mbuf_freem(nd->nd_mrep);
2556ca35587Sdholland 	return (error);
2566ca35587Sdholland }
2576ca35587Sdholland 
2586ca35587Sdholland /*
2596ca35587Sdholland  * nfs open rpc
2606ca35587Sdholland  */
2616ca35587Sdholland APPLESTATIC int
nfsrpc_open(vnode_t vp,int amode,struct ucred * cred,NFSPROC_T * p)2626ca35587Sdholland nfsrpc_open(vnode_t vp, int amode, struct ucred *cred, NFSPROC_T *p)
2636ca35587Sdholland {
2646ca35587Sdholland 	struct nfsclopen *op;
2656ca35587Sdholland 	struct nfscldeleg *dp;
2666ca35587Sdholland 	struct nfsfh *nfhp;
2676ca35587Sdholland 	struct nfsnode *np = VTONFS(vp);
2686ca35587Sdholland 	struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
2696ca35587Sdholland 	u_int32_t mode, clidrev;
2706ca35587Sdholland 	int ret, newone, error, expireret = 0, retrycnt;
2716ca35587Sdholland 
2726ca35587Sdholland 	/*
2736ca35587Sdholland 	 * For NFSv4, Open Ops are only done on Regular Files.
2746ca35587Sdholland 	 */
2756ca35587Sdholland 	if (vnode_vtype(vp) != VREG)
2766ca35587Sdholland 		return (0);
2776ca35587Sdholland 	mode = 0;
2786ca35587Sdholland 	if (amode & FREAD)
2796ca35587Sdholland 		mode |= NFSV4OPEN_ACCESSREAD;
2806ca35587Sdholland 	if (amode & FWRITE)
2816ca35587Sdholland 		mode |= NFSV4OPEN_ACCESSWRITE;
2826ca35587Sdholland 	nfhp = np->n_fhp;
2836ca35587Sdholland 
2846ca35587Sdholland 	retrycnt = 0;
2856ca35587Sdholland #ifdef notdef
2866ca35587Sdholland { char name[100]; int namel;
2876ca35587Sdholland namel = (np->n_v4->n4_namelen < 100) ? np->n_v4->n4_namelen : 99;
2886ca35587Sdholland bcopy(NFS4NODENAME(np->n_v4), name, namel);
2896ca35587Sdholland name[namel] = '\0';
2906ca35587Sdholland printf("rpcopen p=0x%x name=%s",p->p_pid,name);
2916ca35587Sdholland if (nfhp->nfh_len > 0) printf(" fh=0x%x\n",nfhp->nfh_fh[12]);
2926ca35587Sdholland else printf(" fhl=0\n");
2936ca35587Sdholland }
2946ca35587Sdholland #endif
2956ca35587Sdholland 	do {
2966ca35587Sdholland 	    dp = NULL;
2976ca35587Sdholland 	    error = nfscl_open(vp, nfhp->nfh_fh, nfhp->nfh_len, mode, 1,
2986ca35587Sdholland 		cred, p, NULL, &op, &newone, &ret, 1);
2996ca35587Sdholland 	    if (error) {
3006ca35587Sdholland 		return (error);
3016ca35587Sdholland 	    }
3026ca35587Sdholland 	    if (nmp->nm_clp != NULL)
3036ca35587Sdholland 		clidrev = nmp->nm_clp->nfsc_clientidrev;
3046ca35587Sdholland 	    else
3056ca35587Sdholland 		clidrev = 0;
3066ca35587Sdholland 	    if (ret == NFSCLOPEN_DOOPEN) {
3076ca35587Sdholland 		if (np->n_v4 != NULL) {
3086ca35587Sdholland 			error = nfsrpc_openrpc(nmp, vp, np->n_v4->n4_data,
3096ca35587Sdholland 			   np->n_v4->n4_fhlen, np->n_fhp->nfh_fh,
3106ca35587Sdholland 			   np->n_fhp->nfh_len, mode, op,
3116ca35587Sdholland 			   NFS4NODENAME(np->n_v4), np->n_v4->n4_namelen, &dp,
3126ca35587Sdholland 			   0, 0x0, cred, p, 0, 0);
3136ca35587Sdholland 			if (dp != NULL) {
3146ca35587Sdholland #ifdef APPLE
3156ca35587Sdholland 				OSBitAndAtomic((int32_t)~NDELEGMOD, (UInt32 *)&np->n_flag);
3166ca35587Sdholland #else
3176ca35587Sdholland 				NFSLOCKNODE(np);
3186ca35587Sdholland 				np->n_flag &= ~NDELEGMOD;
3196ca35587Sdholland 				/*
3206ca35587Sdholland 				 * Invalidate the attribute cache, so that
3216ca35587Sdholland 				 * attributes that pre-date the issue of a
3226ca35587Sdholland 				 * delegation are not cached, since the
3236ca35587Sdholland 				 * cached attributes will remain valid while
3246ca35587Sdholland 				 * the delegation is held.
3256ca35587Sdholland 				 */
3266ca35587Sdholland 				NFSINVALATTRCACHE(np);
3276ca35587Sdholland 				NFSUNLOCKNODE(np);
3286ca35587Sdholland #endif
3296ca35587Sdholland 				(void) nfscl_deleg(nmp->nm_mountp,
3306ca35587Sdholland 				    op->nfso_own->nfsow_clp,
3316ca35587Sdholland 				    nfhp->nfh_fh, nfhp->nfh_len, cred, p, &dp);
3326ca35587Sdholland 			}
3336ca35587Sdholland 		} else {
3346ca35587Sdholland 			error = EIO;
3356ca35587Sdholland 		}
3366ca35587Sdholland 		newnfs_copyincred(cred, &op->nfso_cred);
3376ca35587Sdholland 	    } else if (ret == NFSCLOPEN_SETCRED)
3386ca35587Sdholland 		/*
3396ca35587Sdholland 		 * This is a new local open on a delegation. It needs
3406ca35587Sdholland 		 * to have credentials so that an open can be done
3416ca35587Sdholland 		 * against the server during recovery.
3426ca35587Sdholland 		 */
3436ca35587Sdholland 		newnfs_copyincred(cred, &op->nfso_cred);
3446ca35587Sdholland 
3456ca35587Sdholland 	    /*
3466ca35587Sdholland 	     * nfso_opencnt is the count of how many VOP_OPEN()s have
3476ca35587Sdholland 	     * been done on this Open successfully and a VOP_CLOSE()
3486ca35587Sdholland 	     * is expected for each of these.
3496ca35587Sdholland 	     * If error is non-zero, don't increment it, since the Open
3506ca35587Sdholland 	     * hasn't succeeded yet.
3516ca35587Sdholland 	     */
3526ca35587Sdholland 	    if (!error)
3536ca35587Sdholland 		op->nfso_opencnt++;
3546ca35587Sdholland 	    nfscl_openrelease(op, error, newone);
3556ca35587Sdholland 	    if (error == NFSERR_GRACE || error == NFSERR_STALECLIENTID ||
3566ca35587Sdholland 		error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
3576ca35587Sdholland 		error == NFSERR_BADSESSION) {
3586ca35587Sdholland 		(void) nfs_catnap(PZERO, error, "nfs_open");
3596ca35587Sdholland 	    } else if ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID)
3606ca35587Sdholland 		&& clidrev != 0) {
3616ca35587Sdholland 		expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
3626ca35587Sdholland 		retrycnt++;
3636ca35587Sdholland 	    }
3646ca35587Sdholland 	} while (error == NFSERR_GRACE || error == NFSERR_STALECLIENTID ||
3656ca35587Sdholland 	    error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
3666ca35587Sdholland 	    error == NFSERR_BADSESSION ||
3676ca35587Sdholland 	    ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
3686ca35587Sdholland 	     expireret == 0 && clidrev != 0 && retrycnt < 4));
3696ca35587Sdholland 	if (error && retrycnt >= 4)
3706ca35587Sdholland 		error = EIO;
3716ca35587Sdholland 	return (error);
3726ca35587Sdholland }
3736ca35587Sdholland 
3746ca35587Sdholland /*
3756ca35587Sdholland  * the actual open rpc
3766ca35587Sdholland  */
3776ca35587Sdholland APPLESTATIC int
nfsrpc_openrpc(struct nfsmount * nmp,vnode_t vp,u_int8_t * nfhp,int fhlen,u_int8_t * newfhp,int newfhlen,u_int32_t mode,struct nfsclopen * op,u_int8_t * name,int namelen,struct nfscldeleg ** dpp,int reclaim,u_int32_t delegtype,struct ucred * cred,NFSPROC_T * p,int syscred,int recursed)3786ca35587Sdholland nfsrpc_openrpc(struct nfsmount *nmp, vnode_t vp, u_int8_t *nfhp, int fhlen,
3796ca35587Sdholland     u_int8_t *newfhp, int newfhlen, u_int32_t mode, struct nfsclopen *op,
3806ca35587Sdholland     u_int8_t *name, int namelen, struct nfscldeleg **dpp,
3816ca35587Sdholland     int reclaim, u_int32_t delegtype, struct ucred *cred, NFSPROC_T *p,
3826ca35587Sdholland     int syscred, int recursed)
3836ca35587Sdholland {
3846ca35587Sdholland 	u_int32_t *tl;
3856ca35587Sdholland 	struct nfsrv_descript nfsd, *nd = &nfsd;
3866ca35587Sdholland 	struct nfscldeleg *dp, *ndp = NULL;
3876ca35587Sdholland 	struct nfsvattr nfsva;
3886ca35587Sdholland 	u_int32_t rflags, deleg;
3896ca35587Sdholland 	nfsattrbit_t attrbits;
3906ca35587Sdholland 	int error, ret, acesize, limitby;
3916ca35587Sdholland 
3926ca35587Sdholland 	dp = *dpp;
3936ca35587Sdholland 	*dpp = NULL;
3946ca35587Sdholland 	nfscl_reqstart(nd, NFSPROC_OPEN, nmp, nfhp, fhlen, NULL, NULL);
3956ca35587Sdholland 	NFSM_BUILD(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
3966ca35587Sdholland 	*tl++ = txdr_unsigned(op->nfso_own->nfsow_seqid);
3976ca35587Sdholland 	*tl++ = txdr_unsigned(mode & NFSV4OPEN_ACCESSBOTH);
3986ca35587Sdholland 	*tl++ = txdr_unsigned((mode >> NFSLCK_SHIFT) & NFSV4OPEN_DENYBOTH);
3996ca35587Sdholland 	*tl++ = NFSMNT_MDSSESSION(nmp)->nfsess_clientid.lval[0];
4006ca35587Sdholland 	*tl = NFSMNT_MDSSESSION(nmp)->nfsess_clientid.lval[1];
4016ca35587Sdholland 	(void) nfsm_strtom(nd, op->nfso_own->nfsow_owner, NFSV4CL_LOCKNAMELEN);
4026ca35587Sdholland 	NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
4036ca35587Sdholland 	*tl++ = txdr_unsigned(NFSV4OPEN_NOCREATE);
4046ca35587Sdholland 	if (reclaim) {
4056ca35587Sdholland 		*tl = txdr_unsigned(NFSV4OPEN_CLAIMPREVIOUS);
4066ca35587Sdholland 		NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
4076ca35587Sdholland 		*tl = txdr_unsigned(delegtype);
4086ca35587Sdholland 	} else {
4096ca35587Sdholland 		if (dp != NULL) {
4106ca35587Sdholland 			*tl = txdr_unsigned(NFSV4OPEN_CLAIMDELEGATECUR);
4116ca35587Sdholland 			NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID);
4126ca35587Sdholland 			if (NFSHASNFSV4N(nmp))
4136ca35587Sdholland 				*tl++ = 0;
4146ca35587Sdholland 			else
4156ca35587Sdholland 				*tl++ = dp->nfsdl_stateid.seqid;
4166ca35587Sdholland 			*tl++ = dp->nfsdl_stateid.other[0];
4176ca35587Sdholland 			*tl++ = dp->nfsdl_stateid.other[1];
4186ca35587Sdholland 			*tl = dp->nfsdl_stateid.other[2];
4196ca35587Sdholland 		} else {
4206ca35587Sdholland 			*tl = txdr_unsigned(NFSV4OPEN_CLAIMNULL);
4216ca35587Sdholland 		}
4226ca35587Sdholland 		(void) nfsm_strtom(nd, name, namelen);
4236ca35587Sdholland 	}
4246ca35587Sdholland 	NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
4256ca35587Sdholland 	*tl = txdr_unsigned(NFSV4OP_GETATTR);
4266ca35587Sdholland 	NFSZERO_ATTRBIT(&attrbits);
4276ca35587Sdholland 	NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_CHANGE);
4286ca35587Sdholland 	NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEMODIFY);
4296ca35587Sdholland 	(void) nfsrv_putattrbit(nd, &attrbits);
4306ca35587Sdholland 	if (syscred)
4316ca35587Sdholland 		nd->nd_flag |= ND_USEGSSNAME;
4326ca35587Sdholland 	error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, vp, p, cred,
4336ca35587Sdholland 	    NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4346ca35587Sdholland 	if (error)
4356ca35587Sdholland 		return (error);
4366ca35587Sdholland 	NFSCL_INCRSEQID(op->nfso_own->nfsow_seqid, nd);
4376ca35587Sdholland 	if (!nd->nd_repstat) {
4386ca35587Sdholland 		NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
4396ca35587Sdholland 		    6 * NFSX_UNSIGNED);
4406ca35587Sdholland 		op->nfso_stateid.seqid = *tl++;
4416ca35587Sdholland 		op->nfso_stateid.other[0] = *tl++;
4426ca35587Sdholland 		op->nfso_stateid.other[1] = *tl++;
4436ca35587Sdholland 		op->nfso_stateid.other[2] = *tl;
4446ca35587Sdholland 		rflags = fxdr_unsigned(u_int32_t, *(tl + 6));
4456ca35587Sdholland 		error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
4466ca35587Sdholland 		if (error)
4476ca35587Sdholland 			goto nfsmout;
4486ca35587Sdholland 		NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
4496ca35587Sdholland 		deleg = fxdr_unsigned(u_int32_t, *tl);
4506ca35587Sdholland 		if (deleg == NFSV4OPEN_DELEGATEREAD ||
4516ca35587Sdholland 		    deleg == NFSV4OPEN_DELEGATEWRITE) {
4526ca35587Sdholland 			if (!(op->nfso_own->nfsow_clp->nfsc_flags &
4536ca35587Sdholland 			      NFSCLFLAGS_FIRSTDELEG))
4546ca35587Sdholland 				op->nfso_own->nfsow_clp->nfsc_flags |=
4556ca35587Sdholland 				  (NFSCLFLAGS_FIRSTDELEG | NFSCLFLAGS_GOTDELEG);
4566ca35587Sdholland 			MALLOC(ndp, struct nfscldeleg *,
4576ca35587Sdholland 			    sizeof (struct nfscldeleg) + newfhlen,
4586ca35587Sdholland 			    M_NFSCLDELEG, M_WAITOK);
4596ca35587Sdholland 			LIST_INIT(&ndp->nfsdl_owner);
4606ca35587Sdholland 			LIST_INIT(&ndp->nfsdl_lock);
4616ca35587Sdholland 			ndp->nfsdl_clp = op->nfso_own->nfsow_clp;
4626ca35587Sdholland 			ndp->nfsdl_fhlen = newfhlen;
4636ca35587Sdholland 			NFSBCOPY(newfhp, ndp->nfsdl_fh, newfhlen);
4646ca35587Sdholland 			newnfs_copyincred(cred, &ndp->nfsdl_cred);
4656ca35587Sdholland 			nfscl_lockinit(&ndp->nfsdl_rwlock);
4666ca35587Sdholland 			NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
4676ca35587Sdholland 			    NFSX_UNSIGNED);
4686ca35587Sdholland 			ndp->nfsdl_stateid.seqid = *tl++;
4696ca35587Sdholland 			ndp->nfsdl_stateid.other[0] = *tl++;
4706ca35587Sdholland 			ndp->nfsdl_stateid.other[1] = *tl++;
4716ca35587Sdholland 			ndp->nfsdl_stateid.other[2] = *tl++;
4726ca35587Sdholland 			ret = fxdr_unsigned(int, *tl);
4736ca35587Sdholland 			if (deleg == NFSV4OPEN_DELEGATEWRITE) {
4746ca35587Sdholland 				ndp->nfsdl_flags = NFSCLDL_WRITE;
4756ca35587Sdholland 				/*
4766ca35587Sdholland 				 * Indicates how much the file can grow.
4776ca35587Sdholland 				 */
4786ca35587Sdholland 				NFSM_DISSECT(tl, u_int32_t *,
4796ca35587Sdholland 				    3 * NFSX_UNSIGNED);
4806ca35587Sdholland 				limitby = fxdr_unsigned(int, *tl++);
4816ca35587Sdholland 				switch (limitby) {
4826ca35587Sdholland 				case NFSV4OPEN_LIMITSIZE:
4836ca35587Sdholland 					ndp->nfsdl_sizelimit = fxdr_hyper(tl);
4846ca35587Sdholland 					break;
4856ca35587Sdholland 				case NFSV4OPEN_LIMITBLOCKS:
4866ca35587Sdholland 					ndp->nfsdl_sizelimit =
4876ca35587Sdholland 					    fxdr_unsigned(u_int64_t, *tl++);
4886ca35587Sdholland 					ndp->nfsdl_sizelimit *=
4896ca35587Sdholland 					    fxdr_unsigned(u_int64_t, *tl);
4906ca35587Sdholland 					break;
4916ca35587Sdholland 				default:
4926ca35587Sdholland 					error = NFSERR_BADXDR;
4936ca35587Sdholland 					goto nfsmout;
494e81f0ea2Spgoyette 				}
4956ca35587Sdholland 			} else {
4966ca35587Sdholland 				ndp->nfsdl_flags = NFSCLDL_READ;
4976ca35587Sdholland 			}
4986ca35587Sdholland 			if (ret)
4996ca35587Sdholland 				ndp->nfsdl_flags |= NFSCLDL_RECALL;
5006ca35587Sdholland 			error = nfsrv_dissectace(nd, &ndp->nfsdl_ace, &ret,
5016ca35587Sdholland 			    &acesize, p);
5026ca35587Sdholland 			if (error)
5036ca35587Sdholland 				goto nfsmout;
5046ca35587Sdholland 		} else if (deleg != NFSV4OPEN_DELEGATENONE) {
5056ca35587Sdholland 			error = NFSERR_BADXDR;
5066ca35587Sdholland 			goto nfsmout;
5076ca35587Sdholland 		}
5086ca35587Sdholland 		NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
5096ca35587Sdholland 		error = nfsv4_loadattr(nd, NULL, &nfsva, NULL,
5106ca35587Sdholland 		    NULL, 0, NULL, NULL, NULL, NULL, NULL, 0,
5116ca35587Sdholland 		    NULL, NULL, NULL, p, cred);
5126ca35587Sdholland 		if (error)
5136ca35587Sdholland 			goto nfsmout;
5146ca35587Sdholland 		if (ndp != NULL) {
5156ca35587Sdholland 			ndp->nfsdl_change = nfsva.na_filerev;
5166ca35587Sdholland 			ndp->nfsdl_modtime = nfsva.na_mtime;
5176ca35587Sdholland 			ndp->nfsdl_flags |= NFSCLDL_MODTIMESET;
5186ca35587Sdholland 		}
5196ca35587Sdholland 		if (!reclaim && (rflags & NFSV4OPEN_RESULTCONFIRM)) {
5206ca35587Sdholland 		    do {
5216ca35587Sdholland 			ret = nfsrpc_openconfirm(vp, newfhp, newfhlen, op,
5226ca35587Sdholland 			    cred, p);
5236ca35587Sdholland 			if (ret == NFSERR_DELAY)
5246ca35587Sdholland 			    (void) nfs_catnap(PZERO, ret, "nfs_open");
5256ca35587Sdholland 		    } while (ret == NFSERR_DELAY);
5266ca35587Sdholland 		    error = ret;
5276ca35587Sdholland 		}
5286ca35587Sdholland 		if ((rflags & NFSV4OPEN_LOCKTYPEPOSIX) ||
5296ca35587Sdholland 		    nfscl_assumeposixlocks)
5306ca35587Sdholland 		    op->nfso_posixlock = 1;
5316ca35587Sdholland 		else
5326ca35587Sdholland 		    op->nfso_posixlock = 0;
5336ca35587Sdholland 
5346ca35587Sdholland 		/*
5356ca35587Sdholland 		 * If the server is handing out delegations, but we didn't
5366ca35587Sdholland 		 * get one because an OpenConfirm was required, try the
5376ca35587Sdholland 		 * Open again, to get a delegation. This is a harmless no-op,
5386ca35587Sdholland 		 * from a server's point of view.
5396ca35587Sdholland 		 */
5406ca35587Sdholland 		if (!reclaim && (rflags & NFSV4OPEN_RESULTCONFIRM) &&
5416ca35587Sdholland 		    (op->nfso_own->nfsow_clp->nfsc_flags & NFSCLFLAGS_GOTDELEG)
5426ca35587Sdholland 		    && !error && dp == NULL && ndp == NULL && !recursed) {
5436ca35587Sdholland 		    do {
5446ca35587Sdholland 			ret = nfsrpc_openrpc(nmp, vp, nfhp, fhlen, newfhp,
5456ca35587Sdholland 			    newfhlen, mode, op, name, namelen, &ndp, 0, 0x0,
5466ca35587Sdholland 			    cred, p, syscred, 1);
5476ca35587Sdholland 			if (ret == NFSERR_DELAY)
5486ca35587Sdholland 			    (void) nfs_catnap(PZERO, ret, "nfs_open2");
5496ca35587Sdholland 		    } while (ret == NFSERR_DELAY);
5506ca35587Sdholland 		    if (ret) {
551e81f0ea2Spgoyette 			if (ndp != NULL) {
5526ca35587Sdholland 				FREE((caddr_t)ndp, M_NFSCLDELEG);
553e81f0ea2Spgoyette 				ndp = NULL;
554e81f0ea2Spgoyette 			}
5556ca35587Sdholland 			if (ret == NFSERR_STALECLIENTID ||
5566ca35587Sdholland 			    ret == NFSERR_STALEDONTRECOVER ||
5576ca35587Sdholland 			    ret == NFSERR_BADSESSION)
5586ca35587Sdholland 				error = ret;
5596ca35587Sdholland 		    }
5606ca35587Sdholland 		}
5616ca35587Sdholland 	}
5626ca35587Sdholland 	if (nd->nd_repstat != 0 && error == 0)
5636ca35587Sdholland 		error = nd->nd_repstat;
5646ca35587Sdholland 	if (error == NFSERR_STALECLIENTID || error == NFSERR_BADSESSION)
5656ca35587Sdholland 		nfscl_initiate_recovery(op->nfso_own->nfsow_clp);
5666ca35587Sdholland nfsmout:
5676ca35587Sdholland 	if (!error)
5686ca35587Sdholland 		*dpp = ndp;
5696ca35587Sdholland 	else if (ndp != NULL)
5706ca35587Sdholland 		FREE((caddr_t)ndp, M_NFSCLDELEG);
5716ca35587Sdholland 	mbuf_freem(nd->nd_mrep);
5726ca35587Sdholland 	return (error);
5736ca35587Sdholland }
5746ca35587Sdholland 
5756ca35587Sdholland /*
5766ca35587Sdholland  * open downgrade rpc
5776ca35587Sdholland  */
5786ca35587Sdholland APPLESTATIC int
nfsrpc_opendowngrade(vnode_t vp,u_int32_t mode,struct nfsclopen * op,struct ucred * cred,NFSPROC_T * p)5796ca35587Sdholland nfsrpc_opendowngrade(vnode_t vp, u_int32_t mode, struct nfsclopen *op,
5806ca35587Sdholland     struct ucred *cred, NFSPROC_T *p)
5816ca35587Sdholland {
5826ca35587Sdholland 	u_int32_t *tl;
5836ca35587Sdholland 	struct nfsrv_descript nfsd, *nd = &nfsd;
5846ca35587Sdholland 	int error;
5856ca35587Sdholland 
5866ca35587Sdholland 	NFSCL_REQSTART(nd, NFSPROC_OPENDOWNGRADE, vp);
5876ca35587Sdholland 	NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID + 3 * NFSX_UNSIGNED);
5886ca35587Sdholland 	if (NFSHASNFSV4N(VFSTONFS(vnode_mount(vp))))
5896ca35587Sdholland 		*tl++ = 0;
5906ca35587Sdholland 	else
5916ca35587Sdholland 		*tl++ = op->nfso_stateid.seqid;
5926ca35587Sdholland 	*tl++ = op->nfso_stateid.other[0];
5936ca35587Sdholland 	*tl++ = op->nfso_stateid.other[1];
5946ca35587Sdholland 	*tl++ = op->nfso_stateid.other[2];
5956ca35587Sdholland 	*tl++ = txdr_unsigned(op->nfso_own->nfsow_seqid);
5966ca35587Sdholland 	*tl++ = txdr_unsigned(mode & NFSV4OPEN_ACCESSBOTH);
5976ca35587Sdholland 	*tl = txdr_unsigned((mode >> NFSLCK_SHIFT) & NFSV4OPEN_DENYBOTH);
5986ca35587Sdholland 	error = nfscl_request(nd, vp, p, cred, NULL);
5996ca35587Sdholland 	if (error)
6006ca35587Sdholland 		return (error);
6016ca35587Sdholland 	NFSCL_INCRSEQID(op->nfso_own->nfsow_seqid, nd);
6026ca35587Sdholland 	if (!nd->nd_repstat) {
6036ca35587Sdholland 		NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID);
6046ca35587Sdholland 		op->nfso_stateid.seqid = *tl++;
6056ca35587Sdholland 		op->nfso_stateid.other[0] = *tl++;
6066ca35587Sdholland 		op->nfso_stateid.other[1] = *tl++;
6076ca35587Sdholland 		op->nfso_stateid.other[2] = *tl;
6086ca35587Sdholland 	}
6096ca35587Sdholland 	if (nd->nd_repstat && error == 0)
6106ca35587Sdholland 		error = nd->nd_repstat;
6116ca35587Sdholland 	if (error == NFSERR_STALESTATEID || error == NFSERR_BADSESSION)
6126ca35587Sdholland 		nfscl_initiate_recovery(op->nfso_own->nfsow_clp);
6136ca35587Sdholland nfsmout:
6146ca35587Sdholland 	mbuf_freem(nd->nd_mrep);
6156ca35587Sdholland 	return (error);
6166ca35587Sdholland }
6176ca35587Sdholland 
6186ca35587Sdholland /*
6196ca35587Sdholland  * V4 Close operation.
6206ca35587Sdholland  */
6216ca35587Sdholland APPLESTATIC int
nfsrpc_close(vnode_t vp,int doclose,NFSPROC_T * p)6226ca35587Sdholland nfsrpc_close(vnode_t vp, int doclose, NFSPROC_T *p)
6236ca35587Sdholland {
6246ca35587Sdholland 	struct nfsclclient *clp;
6256ca35587Sdholland 	int error;
6266ca35587Sdholland 
6276ca35587Sdholland 	if (vnode_vtype(vp) != VREG)
6286ca35587Sdholland 		return (0);
6296ca35587Sdholland 	if (doclose)
6306ca35587Sdholland 		error = nfscl_doclose(vp, &clp, p);
6316ca35587Sdholland 	else
6326ca35587Sdholland 		error = nfscl_getclose(vp, &clp);
6336ca35587Sdholland 	if (error)
6346ca35587Sdholland 		return (error);
6356ca35587Sdholland 
6366ca35587Sdholland 	nfscl_clientrelease(clp);
6376ca35587Sdholland 	return (0);
6386ca35587Sdholland }
6396ca35587Sdholland 
6406ca35587Sdholland /*
6416ca35587Sdholland  * Close the open.
6426ca35587Sdholland  */
6436ca35587Sdholland APPLESTATIC void
nfsrpc_doclose(struct nfsmount * nmp,struct nfsclopen * op,NFSPROC_T * p)6446ca35587Sdholland nfsrpc_doclose(struct nfsmount *nmp, struct nfsclopen *op, NFSPROC_T *p)
6456ca35587Sdholland {
6466ca35587Sdholland 	struct nfsrv_descript nfsd, *nd = &nfsd;
6476ca35587Sdholland 	struct nfscllockowner *lp, *nlp;
6486ca35587Sdholland 	struct nfscllock *lop, *nlop;
6496ca35587Sdholland 	struct ucred *tcred;
6506ca35587Sdholland 	u_int64_t off = 0, len = 0;
6516ca35587Sdholland 	u_int32_t type = NFSV4LOCKT_READ;
6526ca35587Sdholland 	int error, do_unlock, trycnt;
6536ca35587Sdholland 
6546ca35587Sdholland 	tcred = newnfs_getcred();
6556ca35587Sdholland 	newnfs_copycred(&op->nfso_cred, tcred);
6566ca35587Sdholland 	/*
6576ca35587Sdholland 	 * (Theoretically this could be done in the same
6586ca35587Sdholland 	 *  compound as the close, but having multiple
6596ca35587Sdholland 	 *  sequenced Ops in the same compound might be
6606ca35587Sdholland 	 *  too scary for some servers.)
6616ca35587Sdholland 	 */
6626ca35587Sdholland 	if (op->nfso_posixlock) {
6636ca35587Sdholland 		off = 0;
6646ca35587Sdholland 		len = NFS64BITSSET;
6656ca35587Sdholland 		type = NFSV4LOCKT_READ;
6666ca35587Sdholland 	}
6676ca35587Sdholland 
6686ca35587Sdholland 	/*
6696ca35587Sdholland 	 * Since this function is only called from VOP_INACTIVE(), no
6706ca35587Sdholland 	 * other thread will be manipulating this Open. As such, the
6716ca35587Sdholland 	 * lock lists are not being changed by other threads, so it should
6726ca35587Sdholland 	 * be safe to do this without locking.
6736ca35587Sdholland 	 */
6746ca35587Sdholland 	LIST_FOREACH(lp, &op->nfso_lock, nfsl_list) {
6756ca35587Sdholland 		do_unlock = 1;
6766ca35587Sdholland 		LIST_FOREACH_SAFE(lop, &lp->nfsl_lock, nfslo_list, nlop) {
6776ca35587Sdholland 			if (op->nfso_posixlock == 0) {
6786ca35587Sdholland 				off = lop->nfslo_first;
6796ca35587Sdholland 				len = lop->nfslo_end - lop->nfslo_first;
6806ca35587Sdholland 				if (lop->nfslo_type == F_WRLCK)
6816ca35587Sdholland 					type = NFSV4LOCKT_WRITE;
6826ca35587Sdholland 				else
6836ca35587Sdholland 					type = NFSV4LOCKT_READ;
6846ca35587Sdholland 			}
6856ca35587Sdholland 			if (do_unlock) {
6866ca35587Sdholland 				trycnt = 0;
6876ca35587Sdholland 				do {
6886ca35587Sdholland 					error = nfsrpc_locku(nd, nmp, lp, off,
6896ca35587Sdholland 					    len, type, tcred, p, 0);
6906ca35587Sdholland 					if ((nd->nd_repstat == NFSERR_GRACE ||
6916ca35587Sdholland 					    nd->nd_repstat == NFSERR_DELAY) &&
6926ca35587Sdholland 					    error == 0)
6936ca35587Sdholland 						(void) nfs_catnap(PZERO,
6946ca35587Sdholland 						    (int)nd->nd_repstat,
6956ca35587Sdholland 						    "nfs_close");
6966ca35587Sdholland 				} while ((nd->nd_repstat == NFSERR_GRACE ||
6976ca35587Sdholland 				    nd->nd_repstat == NFSERR_DELAY) &&
6986ca35587Sdholland 				    error == 0 && trycnt++ < 5);
6996ca35587Sdholland 				if (op->nfso_posixlock)
7006ca35587Sdholland 					do_unlock = 0;
7016ca35587Sdholland 			}
7026ca35587Sdholland 			nfscl_freelock(lop, 0);
7036ca35587Sdholland 		}
7046ca35587Sdholland 		/*
7056ca35587Sdholland 		 * Do a ReleaseLockOwner.
7066ca35587Sdholland 		 * The lock owner name nfsl_owner may be used by other opens for
7076ca35587Sdholland 		 * other files but the lock_owner4 name that nfsrpc_rellockown()
7086ca35587Sdholland 		 * puts on the wire has the file handle for this file appended
7096ca35587Sdholland 		 * to it, so it can be done now.
7106ca35587Sdholland 		 */
7116ca35587Sdholland 		(void)nfsrpc_rellockown(nmp, lp, lp->nfsl_open->nfso_fh,
7126ca35587Sdholland 		    lp->nfsl_open->nfso_fhlen, tcred, p);
7136ca35587Sdholland 	}
7146ca35587Sdholland 
7156ca35587Sdholland 	/*
7166ca35587Sdholland 	 * There could be other Opens for different files on the same
7176ca35587Sdholland 	 * OpenOwner, so locking is required.
7186ca35587Sdholland 	 */
7196ca35587Sdholland 	NFSLOCKCLSTATE();
7206ca35587Sdholland 	nfscl_lockexcl(&op->nfso_own->nfsow_rwlock, NFSCLSTATEMUTEXPTR);
7216ca35587Sdholland 	NFSUNLOCKCLSTATE();
7226ca35587Sdholland 	do {
7236ca35587Sdholland 		error = nfscl_tryclose(op, tcred, nmp, p);
7246ca35587Sdholland 		if (error == NFSERR_GRACE)
7256ca35587Sdholland 			(void) nfs_catnap(PZERO, error, "nfs_close");
7266ca35587Sdholland 	} while (error == NFSERR_GRACE);
7276ca35587Sdholland 	NFSLOCKCLSTATE();
7286ca35587Sdholland 	nfscl_lockunlock(&op->nfso_own->nfsow_rwlock);
7296ca35587Sdholland 
7306ca35587Sdholland 	LIST_FOREACH_SAFE(lp, &op->nfso_lock, nfsl_list, nlp)
7316ca35587Sdholland 		nfscl_freelockowner(lp, 0);
7326ca35587Sdholland 	nfscl_freeopen(op, 0);
7336ca35587Sdholland 	NFSUNLOCKCLSTATE();
7346ca35587Sdholland 	NFSFREECRED(tcred);
7356ca35587Sdholland }
7366ca35587Sdholland 
7376ca35587Sdholland /*
7386ca35587Sdholland  * The actual Close RPC.
7396ca35587Sdholland  */
7406ca35587Sdholland APPLESTATIC int
nfsrpc_closerpc(struct nfsrv_descript * nd,struct nfsmount * nmp,struct nfsclopen * op,struct ucred * cred,NFSPROC_T * p,int syscred)7416ca35587Sdholland nfsrpc_closerpc(struct nfsrv_descript *nd, struct nfsmount *nmp,
7426ca35587Sdholland     struct nfsclopen *op, struct ucred *cred, NFSPROC_T *p,
7436ca35587Sdholland     int syscred)
7446ca35587Sdholland {
7456ca35587Sdholland 	u_int32_t *tl;
7466ca35587Sdholland 	int error;
7476ca35587Sdholland 
7486ca35587Sdholland 	nfscl_reqstart(nd, NFSPROC_CLOSE, nmp, op->nfso_fh,
7496ca35587Sdholland 	    op->nfso_fhlen, NULL, NULL);
7506ca35587Sdholland 	NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED + NFSX_STATEID);
7516ca35587Sdholland 	*tl++ = txdr_unsigned(op->nfso_own->nfsow_seqid);
7526ca35587Sdholland 	if (NFSHASNFSV4N(nmp))
7536ca35587Sdholland 		*tl++ = 0;
7546ca35587Sdholland 	else
7556ca35587Sdholland 		*tl++ = op->nfso_stateid.seqid;
7566ca35587Sdholland 	*tl++ = op->nfso_stateid.other[0];
7576ca35587Sdholland 	*tl++ = op->nfso_stateid.other[1];
7586ca35587Sdholland 	*tl = op->nfso_stateid.other[2];
7596ca35587Sdholland 	if (syscred)
7606ca35587Sdholland 		nd->nd_flag |= ND_USEGSSNAME;
7616ca35587Sdholland 	error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
7626ca35587Sdholland 	    NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
7636ca35587Sdholland 	if (error)
7646ca35587Sdholland 		return (error);
7656ca35587Sdholland 	NFSCL_INCRSEQID(op->nfso_own->nfsow_seqid, nd);
7666ca35587Sdholland 	if (nd->nd_repstat == 0)
7676ca35587Sdholland 		NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID);
7686ca35587Sdholland 	error = nd->nd_repstat;
7696ca35587Sdholland 	if (error == NFSERR_STALESTATEID || error == NFSERR_BADSESSION)
7706ca35587Sdholland 		nfscl_initiate_recovery(op->nfso_own->nfsow_clp);
7716ca35587Sdholland nfsmout:
7726ca35587Sdholland 	mbuf_freem(nd->nd_mrep);
7736ca35587Sdholland 	return (error);
7746ca35587Sdholland }
7756ca35587Sdholland 
7766ca35587Sdholland /*
7776ca35587Sdholland  * V4 Open Confirm RPC.
7786ca35587Sdholland  */
7796ca35587Sdholland APPLESTATIC int
nfsrpc_openconfirm(vnode_t vp,u_int8_t * nfhp,int fhlen,struct nfsclopen * op,struct ucred * cred,NFSPROC_T * p)7806ca35587Sdholland nfsrpc_openconfirm(vnode_t vp, u_int8_t *nfhp, int fhlen,
7816ca35587Sdholland     struct nfsclopen *op, struct ucred *cred, NFSPROC_T *p)
7826ca35587Sdholland {
7836ca35587Sdholland 	u_int32_t *tl;
7846ca35587Sdholland 	struct nfsrv_descript nfsd, *nd = &nfsd;
7856ca35587Sdholland 	struct nfsmount *nmp;
7866ca35587Sdholland 	int error;
7876ca35587Sdholland 
7886ca35587Sdholland 	nmp = VFSTONFS(vnode_mount(vp));
7896ca35587Sdholland 	if (NFSHASNFSV4N(nmp))
7906ca35587Sdholland 		return (0);		/* No confirmation for NFSv4.1. */
7916ca35587Sdholland 	nfscl_reqstart(nd, NFSPROC_OPENCONFIRM, nmp, nfhp, fhlen, NULL, NULL);
7926ca35587Sdholland 	NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED + NFSX_STATEID);
7936ca35587Sdholland 	*tl++ = op->nfso_stateid.seqid;
7946ca35587Sdholland 	*tl++ = op->nfso_stateid.other[0];
7956ca35587Sdholland 	*tl++ = op->nfso_stateid.other[1];
7966ca35587Sdholland 	*tl++ = op->nfso_stateid.other[2];
7976ca35587Sdholland 	*tl = txdr_unsigned(op->nfso_own->nfsow_seqid);
7986ca35587Sdholland 	error = nfscl_request(nd, vp, p, cred, NULL);
7996ca35587Sdholland 	if (error)
8006ca35587Sdholland 		return (error);
8016ca35587Sdholland 	NFSCL_INCRSEQID(op->nfso_own->nfsow_seqid, nd);
8026ca35587Sdholland 	if (!nd->nd_repstat) {
8036ca35587Sdholland 		NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID);
8046ca35587Sdholland 		op->nfso_stateid.seqid = *tl++;
8056ca35587Sdholland 		op->nfso_stateid.other[0] = *tl++;
8066ca35587Sdholland 		op->nfso_stateid.other[1] = *tl++;
8076ca35587Sdholland 		op->nfso_stateid.other[2] = *tl;
8086ca35587Sdholland 	}
8096ca35587Sdholland 	error = nd->nd_repstat;
8106ca35587Sdholland 	if (error == NFSERR_STALESTATEID || error == NFSERR_BADSESSION)
8116ca35587Sdholland 		nfscl_initiate_recovery(op->nfso_own->nfsow_clp);
8126ca35587Sdholland nfsmout:
8136ca35587Sdholland 	mbuf_freem(nd->nd_mrep);
8146ca35587Sdholland 	return (error);
8156ca35587Sdholland }
8166ca35587Sdholland 
8176ca35587Sdholland /*
8186ca35587Sdholland  * Do the setclientid and setclientid confirm RPCs. Called from nfs_statfs()
8196ca35587Sdholland  * when a mount has just occurred and when the server replies NFSERR_EXPIRED.
8206ca35587Sdholland  */
8216ca35587Sdholland APPLESTATIC int
nfsrpc_setclient(struct nfsmount * nmp,struct nfsclclient * clp,int reclaim,struct ucred * cred,NFSPROC_T * p)8226ca35587Sdholland nfsrpc_setclient(struct nfsmount *nmp, struct nfsclclient *clp, int reclaim,
8236ca35587Sdholland     struct ucred *cred, NFSPROC_T *p)
8246ca35587Sdholland {
8256ca35587Sdholland 	u_int32_t *tl;
8266ca35587Sdholland 	struct nfsrv_descript nfsd;
8276ca35587Sdholland 	struct nfsrv_descript *nd = &nfsd;
8286ca35587Sdholland 	nfsattrbit_t attrbits;
8296ca35587Sdholland 	u_int8_t *cp = NULL, *cp2, addr[INET6_ADDRSTRLEN + 9];
8306ca35587Sdholland 	u_short port;
8316ca35587Sdholland 	int error, isinet6 = 0, callblen;
8326ca35587Sdholland 	nfsquad_t confirm;
8336ca35587Sdholland 	u_int32_t lease;
8346ca35587Sdholland 	static u_int32_t rev = 0;
8356ca35587Sdholland 	struct nfsclds *dsp, *ndsp, *tdsp;
836e81f0ea2Spgoyette 	struct in6_addr a6;
8376ca35587Sdholland 
8386ca35587Sdholland 	if (nfsboottime.tv_sec == 0)
8396ca35587Sdholland 		NFSSETBOOTTIME(nfsboottime);
8406ca35587Sdholland 	clp->nfsc_rev = rev++;
8416ca35587Sdholland 	if (NFSHASNFSV4N(nmp)) {
8426ca35587Sdholland 		error = nfsrpc_exchangeid(nmp, clp, &nmp->nm_sockreq,
8436ca35587Sdholland 		    NFSV4EXCH_USEPNFSMDS | NFSV4EXCH_USENONPNFS, &dsp, cred, p);
8446ca35587Sdholland 		NFSCL_DEBUG(1, "aft exch=%d\n", error);
8456ca35587Sdholland 		if (error == 0) {
8466ca35587Sdholland 			error = nfsrpc_createsession(nmp, &dsp->nfsclds_sess,
8476ca35587Sdholland 			    &nmp->nm_sockreq,
8486ca35587Sdholland 			    dsp->nfsclds_sess.nfsess_sequenceid, 1, cred, p);
8496ca35587Sdholland 			if (error == 0) {
8506ca35587Sdholland 				NFSLOCKMNT(nmp);
8516ca35587Sdholland 				TAILQ_FOREACH_SAFE(tdsp, &nmp->nm_sess,
8526ca35587Sdholland 				    nfsclds_list, ndsp)
8536ca35587Sdholland 					nfscl_freenfsclds(tdsp);
8546ca35587Sdholland 				TAILQ_INIT(&nmp->nm_sess);
8556ca35587Sdholland 				TAILQ_INSERT_HEAD(&nmp->nm_sess, dsp,
8566ca35587Sdholland 				    nfsclds_list);
8576ca35587Sdholland 				NFSUNLOCKMNT(nmp);
8586ca35587Sdholland 			} else
8596ca35587Sdholland 				nfscl_freenfsclds(dsp);
8606ca35587Sdholland 			NFSCL_DEBUG(1, "aft createsess=%d\n", error);
8616ca35587Sdholland 		}
8626ca35587Sdholland 		if (error == 0 && reclaim == 0) {
8636ca35587Sdholland 			error = nfsrpc_reclaimcomplete(nmp, cred, p);
8646ca35587Sdholland 			NFSCL_DEBUG(1, "aft reclaimcomp=%d\n", error);
8656ca35587Sdholland 			if (error == NFSERR_COMPLETEALREADY ||
8666ca35587Sdholland 			    error == NFSERR_NOTSUPP)
8676ca35587Sdholland 				/* Ignore this error. */
8686ca35587Sdholland 				error = 0;
8696ca35587Sdholland 		}
8706ca35587Sdholland 		return (error);
8716ca35587Sdholland 	}
8726ca35587Sdholland 
8736ca35587Sdholland 	/*
8746ca35587Sdholland 	 * Allocate a single session structure for NFSv4.0, because some of
8756ca35587Sdholland 	 * the fields are used by NFSv4.0 although it doesn't do a session.
8766ca35587Sdholland 	 */
8776ca35587Sdholland 	dsp = malloc(sizeof(struct nfsclds), M_NFSCLDS, M_WAITOK | M_ZERO);
8786ca35587Sdholland 	mtx_init(&dsp->nfsclds_mtx, "nfsds", NULL, MTX_DEF);
8796ca35587Sdholland 	mtx_init(&dsp->nfsclds_sess.nfsess_mtx, "nfssession", NULL, MTX_DEF);
8806ca35587Sdholland 	NFSLOCKMNT(nmp);
8816ca35587Sdholland 	TAILQ_INSERT_HEAD(&nmp->nm_sess, dsp, nfsclds_list);
8826ca35587Sdholland 	NFSUNLOCKMNT(nmp);
8836ca35587Sdholland 
8846ca35587Sdholland 	nfscl_reqstart(nd, NFSPROC_SETCLIENTID, nmp, NULL, 0, NULL, NULL);
8856ca35587Sdholland 	NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
8866ca35587Sdholland 	*tl++ = txdr_unsigned(nfsboottime.tv_sec);
8876ca35587Sdholland 	*tl = txdr_unsigned(clp->nfsc_rev);
8886ca35587Sdholland 	(void) nfsm_strtom(nd, clp->nfsc_id, clp->nfsc_idlen);
8896ca35587Sdholland 
8906ca35587Sdholland 	/*
8916ca35587Sdholland 	 * set up the callback address
8926ca35587Sdholland 	 */
8936ca35587Sdholland 	NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
8946ca35587Sdholland 	*tl = txdr_unsigned(NFS_CALLBCKPROG);
8956ca35587Sdholland 	callblen = strlen(nfsv4_callbackaddr);
8966ca35587Sdholland 	if (callblen == 0)
897e81f0ea2Spgoyette 		cp = nfscl_getmyip(nmp, &a6, &isinet6);
8986ca35587Sdholland 	if (nfscl_enablecallb && nfs_numnfscbd > 0 &&
8996ca35587Sdholland 	    (callblen > 0 || cp != NULL)) {
9006ca35587Sdholland 		port = htons(nfsv4_cbport);
9016ca35587Sdholland 		cp2 = (u_int8_t *)&port;
9026ca35587Sdholland #ifdef INET6
9036ca35587Sdholland 		if ((callblen > 0 &&
9046ca35587Sdholland 		     strchr(nfsv4_callbackaddr, ':')) || isinet6) {
9056ca35587Sdholland 			char ip6buf[INET6_ADDRSTRLEN], *ip6add;
9066ca35587Sdholland 
9076ca35587Sdholland 			(void) nfsm_strtom(nd, "tcp6", 4);
9086ca35587Sdholland 			if (callblen == 0) {
9096ca35587Sdholland 				ip6_sprintf(ip6buf, (struct in6_addr *)cp);
9106ca35587Sdholland 				ip6add = ip6buf;
9116ca35587Sdholland 			} else {
9126ca35587Sdholland 				ip6add = nfsv4_callbackaddr;
9136ca35587Sdholland 			}
9146ca35587Sdholland 			snprintf(addr, INET6_ADDRSTRLEN + 9, "%s.%d.%d",
9156ca35587Sdholland 			    ip6add, cp2[0], cp2[1]);
9166ca35587Sdholland 		} else
9176ca35587Sdholland #endif
9186ca35587Sdholland 		{
9196ca35587Sdholland 			(void) nfsm_strtom(nd, "tcp", 3);
9206ca35587Sdholland 			if (callblen == 0)
9216ca35587Sdholland 				snprintf(addr, INET6_ADDRSTRLEN + 9,
9226ca35587Sdholland 				    "%d.%d.%d.%d.%d.%d", cp[0], cp[1],
9236ca35587Sdholland 				    cp[2], cp[3], cp2[0], cp2[1]);
9246ca35587Sdholland 			else
9256ca35587Sdholland 				snprintf(addr, INET6_ADDRSTRLEN + 9,
9266ca35587Sdholland 				    "%s.%d.%d", nfsv4_callbackaddr,
9276ca35587Sdholland 				    cp2[0], cp2[1]);
9286ca35587Sdholland 		}
9296ca35587Sdholland 		(void) nfsm_strtom(nd, addr, strlen(addr));
9306ca35587Sdholland 	} else {
9316ca35587Sdholland 		(void) nfsm_strtom(nd, "tcp", 3);
9326ca35587Sdholland 		(void) nfsm_strtom(nd, "0.0.0.0.0.0", 11);
9336ca35587Sdholland 	}
9346ca35587Sdholland 	NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
9356ca35587Sdholland 	*tl = txdr_unsigned(clp->nfsc_cbident);
9366ca35587Sdholland 	nd->nd_flag |= ND_USEGSSNAME;
9376ca35587Sdholland 	error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
9386ca35587Sdholland 		NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
9396ca35587Sdholland 	if (error)
9406ca35587Sdholland 		return (error);
9416ca35587Sdholland 	if (nd->nd_repstat == 0) {
9426ca35587Sdholland 	    NFSM_DISSECT(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
9436ca35587Sdholland 	    NFSMNT_MDSSESSION(nmp)->nfsess_clientid.lval[0] = *tl++;
9446ca35587Sdholland 	    NFSMNT_MDSSESSION(nmp)->nfsess_clientid.lval[1] = *tl++;
9456ca35587Sdholland 	    confirm.lval[0] = *tl++;
9466ca35587Sdholland 	    confirm.lval[1] = *tl;
9476ca35587Sdholland 	    mbuf_freem(nd->nd_mrep);
9486ca35587Sdholland 	    nd->nd_mrep = NULL;
9496ca35587Sdholland 
9506ca35587Sdholland 	    /*
9516ca35587Sdholland 	     * and confirm it.
9526ca35587Sdholland 	     */
9536ca35587Sdholland 	    nfscl_reqstart(nd, NFSPROC_SETCLIENTIDCFRM, nmp, NULL, 0, NULL,
9546ca35587Sdholland 		NULL);
9556ca35587Sdholland 	    NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
9566ca35587Sdholland 	    *tl++ = NFSMNT_MDSSESSION(nmp)->nfsess_clientid.lval[0];
9576ca35587Sdholland 	    *tl++ = NFSMNT_MDSSESSION(nmp)->nfsess_clientid.lval[1];
9586ca35587Sdholland 	    *tl++ = confirm.lval[0];
9596ca35587Sdholland 	    *tl = confirm.lval[1];
9606ca35587Sdholland 	    nd->nd_flag |= ND_USEGSSNAME;
9616ca35587Sdholland 	    error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p,
9626ca35587Sdholland 		cred, NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
9636ca35587Sdholland 	    if (error)
9646ca35587Sdholland 		return (error);
9656ca35587Sdholland 	    mbuf_freem(nd->nd_mrep);
9666ca35587Sdholland 	    nd->nd_mrep = NULL;
9676ca35587Sdholland 	    if (nd->nd_repstat == 0) {
9686ca35587Sdholland 		nfscl_reqstart(nd, NFSPROC_GETATTR, nmp, nmp->nm_fh,
9696ca35587Sdholland 		    nmp->nm_fhsize, NULL, NULL);
9706ca35587Sdholland 		NFSZERO_ATTRBIT(&attrbits);
9716ca35587Sdholland 		NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_LEASETIME);
9726ca35587Sdholland 		(void) nfsrv_putattrbit(nd, &attrbits);
9736ca35587Sdholland 		nd->nd_flag |= ND_USEGSSNAME;
9746ca35587Sdholland 		error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p,
9756ca35587Sdholland 		    cred, NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
9766ca35587Sdholland 		if (error)
9776ca35587Sdholland 		    return (error);
9786ca35587Sdholland 		if (nd->nd_repstat == 0) {
9796ca35587Sdholland 		    error = nfsv4_loadattr(nd, NULL, NULL, NULL, NULL, 0, NULL,
9806ca35587Sdholland 			NULL, NULL, NULL, NULL, 0, NULL, &lease, NULL, p, cred);
9816ca35587Sdholland 		    if (error)
9826ca35587Sdholland 			goto nfsmout;
9836ca35587Sdholland 		    clp->nfsc_renew = NFSCL_RENEW(lease);
9846ca35587Sdholland 		    clp->nfsc_expire = NFSD_MONOSEC + clp->nfsc_renew;
9856ca35587Sdholland 		    clp->nfsc_clientidrev++;
9866ca35587Sdholland 		    if (clp->nfsc_clientidrev == 0)
9876ca35587Sdholland 			clp->nfsc_clientidrev++;
9886ca35587Sdholland 		}
9896ca35587Sdholland 	    }
9906ca35587Sdholland 	}
9916ca35587Sdholland 	error = nd->nd_repstat;
9926ca35587Sdholland nfsmout:
9936ca35587Sdholland 	mbuf_freem(nd->nd_mrep);
9946ca35587Sdholland 	return (error);
9956ca35587Sdholland }
9966ca35587Sdholland 
9976ca35587Sdholland /*
9986ca35587Sdholland  * nfs getattr call.
9996ca35587Sdholland  */
10006ca35587Sdholland APPLESTATIC int
nfsrpc_getattr(vnode_t vp,struct ucred * cred,NFSPROC_T * p,struct nfsvattr * nap,void * stuff)10016ca35587Sdholland nfsrpc_getattr(vnode_t vp, struct ucred *cred, NFSPROC_T *p,
10026ca35587Sdholland     struct nfsvattr *nap, void *stuff)
10036ca35587Sdholland {
10046ca35587Sdholland 	struct nfsrv_descript nfsd, *nd = &nfsd;
10056ca35587Sdholland 	int error;
10066ca35587Sdholland 	nfsattrbit_t attrbits;
10076ca35587Sdholland 
10086ca35587Sdholland 	NFSCL_REQSTART(nd, NFSPROC_GETATTR, vp);
10096ca35587Sdholland 	if (nd->nd_flag & ND_NFSV4) {
10106ca35587Sdholland 		NFSGETATTR_ATTRBIT(&attrbits);
10116ca35587Sdholland 		(void) nfsrv_putattrbit(nd, &attrbits);
10126ca35587Sdholland 	}
10136ca35587Sdholland 	error = nfscl_request(nd, vp, p, cred, stuff);
10146ca35587Sdholland 	if (error)
10156ca35587Sdholland 		return (error);
10166ca35587Sdholland 	if (!nd->nd_repstat)
10176ca35587Sdholland 		error = nfsm_loadattr(nd, nap);
10186ca35587Sdholland 	else
10196ca35587Sdholland 		error = nd->nd_repstat;
10206ca35587Sdholland 	mbuf_freem(nd->nd_mrep);
10216ca35587Sdholland 	return (error);
10226ca35587Sdholland }
10236ca35587Sdholland 
10246ca35587Sdholland /*
1025d3789543Sandvar  * nfs getattr call with non-vnode arguments.
10266ca35587Sdholland  */
10276ca35587Sdholland APPLESTATIC int
nfsrpc_getattrnovp(struct nfsmount * nmp,u_int8_t * fhp,int fhlen,int syscred,struct ucred * cred,NFSPROC_T * p,struct nfsvattr * nap,u_int64_t * xidp,uint32_t * leasep)10286ca35587Sdholland nfsrpc_getattrnovp(struct nfsmount *nmp, u_int8_t *fhp, int fhlen, int syscred,
10296ca35587Sdholland     struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, u_int64_t *xidp,
10306ca35587Sdholland     uint32_t *leasep)
10316ca35587Sdholland {
10326ca35587Sdholland 	struct nfsrv_descript nfsd, *nd = &nfsd;
10336ca35587Sdholland 	int error, vers = NFS_VER2;
10346ca35587Sdholland 	nfsattrbit_t attrbits;
10356ca35587Sdholland 
10366ca35587Sdholland 	nfscl_reqstart(nd, NFSPROC_GETATTR, nmp, fhp, fhlen, NULL, NULL);
10376ca35587Sdholland 	if (nd->nd_flag & ND_NFSV4) {
10386ca35587Sdholland 		vers = NFS_VER4;
10396ca35587Sdholland 		NFSGETATTR_ATTRBIT(&attrbits);
10406ca35587Sdholland 		NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_LEASETIME);
10416ca35587Sdholland 		(void) nfsrv_putattrbit(nd, &attrbits);
10426ca35587Sdholland 	} else if (nd->nd_flag & ND_NFSV3) {
10436ca35587Sdholland 		vers = NFS_VER3;
10446ca35587Sdholland 	}
10456ca35587Sdholland 	if (syscred)
10466ca35587Sdholland 		nd->nd_flag |= ND_USEGSSNAME;
10476ca35587Sdholland 	error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
10486ca35587Sdholland 	    NFS_PROG, vers, NULL, 1, xidp, NULL);
10496ca35587Sdholland 	if (error)
10506ca35587Sdholland 		return (error);
10516ca35587Sdholland 	if (nd->nd_repstat == 0) {
10526ca35587Sdholland 		if ((nd->nd_flag & ND_NFSV4) != 0)
10536ca35587Sdholland 			error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0,
10546ca35587Sdholland 			    NULL, NULL, NULL, NULL, NULL, 0, NULL, leasep, NULL,
10556ca35587Sdholland 			    NULL, NULL);
10566ca35587Sdholland 		else
10576ca35587Sdholland 			error = nfsm_loadattr(nd, nap);
10586ca35587Sdholland 	} else
10596ca35587Sdholland 		error = nd->nd_repstat;
10606ca35587Sdholland 	mbuf_freem(nd->nd_mrep);
10616ca35587Sdholland 	return (error);
10626ca35587Sdholland }
10636ca35587Sdholland 
10646ca35587Sdholland /*
10656ca35587Sdholland  * Do an nfs setattr operation.
10666ca35587Sdholland  */
10676ca35587Sdholland APPLESTATIC int
nfsrpc_setattr(vnode_t vp,struct vattr * vap,NFSACL_T * aclp,struct ucred * cred,NFSPROC_T * p,struct nfsvattr * rnap,int * attrflagp,void * stuff)10686ca35587Sdholland nfsrpc_setattr(vnode_t vp, struct vattr *vap, NFSACL_T *aclp,
10696ca35587Sdholland     struct ucred *cred, NFSPROC_T *p, struct nfsvattr *rnap, int *attrflagp,
10706ca35587Sdholland     void *stuff)
10716ca35587Sdholland {
10726ca35587Sdholland 	int error, expireret = 0, openerr, retrycnt;
10736ca35587Sdholland 	u_int32_t clidrev = 0, mode;
10746ca35587Sdholland 	struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
10756ca35587Sdholland 	struct nfsfh *nfhp;
10766ca35587Sdholland 	nfsv4stateid_t stateid;
10776ca35587Sdholland 	void *lckp;
10786ca35587Sdholland 
10796ca35587Sdholland 	if (nmp->nm_clp != NULL)
10806ca35587Sdholland 		clidrev = nmp->nm_clp->nfsc_clientidrev;
10816ca35587Sdholland 	if (vap != NULL && NFSATTRISSET(u_quad_t, vap, va_size))
10826ca35587Sdholland 		mode = NFSV4OPEN_ACCESSWRITE;
10836ca35587Sdholland 	else
10846ca35587Sdholland 		mode = NFSV4OPEN_ACCESSREAD;
10856ca35587Sdholland 	retrycnt = 0;
10866ca35587Sdholland 	do {
10876ca35587Sdholland 		lckp = NULL;
10886ca35587Sdholland 		openerr = 1;
10896ca35587Sdholland 		if (NFSHASNFSV4(nmp)) {
10906ca35587Sdholland 			nfhp = VTONFS(vp)->n_fhp;
10916ca35587Sdholland 			error = nfscl_getstateid(vp, nfhp->nfh_fh,
10926ca35587Sdholland 			    nfhp->nfh_len, mode, 0, cred, p, &stateid, &lckp);
10936ca35587Sdholland 			if (error && vnode_vtype(vp) == VREG &&
10946ca35587Sdholland 			    (mode == NFSV4OPEN_ACCESSWRITE ||
10956ca35587Sdholland 			     nfstest_openallsetattr)) {
10966ca35587Sdholland 				/*
10976ca35587Sdholland 				 * No Open stateid, so try and open the file
10986ca35587Sdholland 				 * now.
10996ca35587Sdholland 				 */
11006ca35587Sdholland 				if (mode == NFSV4OPEN_ACCESSWRITE)
11016ca35587Sdholland 					openerr = nfsrpc_open(vp, FWRITE, cred,
11026ca35587Sdholland 					    p);
11036ca35587Sdholland 				else
11046ca35587Sdholland 					openerr = nfsrpc_open(vp, FREAD, cred,
11056ca35587Sdholland 					    p);
11066ca35587Sdholland 				if (!openerr)
11076ca35587Sdholland 					(void) nfscl_getstateid(vp,
11086ca35587Sdholland 					    nfhp->nfh_fh, nfhp->nfh_len,
11096ca35587Sdholland 					    mode, 0, cred, p, &stateid, &lckp);
11106ca35587Sdholland 			}
11116ca35587Sdholland 		}
11126ca35587Sdholland 		if (vap != NULL)
11136ca35587Sdholland 			error = nfsrpc_setattrrpc(vp, vap, &stateid, cred, p,
11146ca35587Sdholland 			    rnap, attrflagp, stuff);
11156ca35587Sdholland 		else
11166ca35587Sdholland 			error = nfsrpc_setaclrpc(vp, cred, p, aclp, &stateid,
11176ca35587Sdholland 			    stuff);
11186ca35587Sdholland 		if (error == NFSERR_STALESTATEID || error == NFSERR_BADSESSION)
11196ca35587Sdholland 			nfscl_initiate_recovery(nmp->nm_clp);
11206ca35587Sdholland 		if (lckp != NULL)
11216ca35587Sdholland 			nfscl_lockderef(lckp);
11226ca35587Sdholland 		if (!openerr)
11236ca35587Sdholland 			(void) nfsrpc_close(vp, 0, p);
11246ca35587Sdholland 		if (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
11256ca35587Sdholland 		    error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
11266ca35587Sdholland 		    error == NFSERR_OLDSTATEID || error == NFSERR_BADSESSION) {
11276ca35587Sdholland 			(void) nfs_catnap(PZERO, error, "nfs_setattr");
11286ca35587Sdholland 		} else if ((error == NFSERR_EXPIRED ||
11296ca35587Sdholland 		    error == NFSERR_BADSTATEID) && clidrev != 0) {
11306ca35587Sdholland 			expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
11316ca35587Sdholland 		}
11326ca35587Sdholland 		retrycnt++;
11336ca35587Sdholland 	} while (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
11346ca35587Sdholland 	    error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
11356ca35587Sdholland 	    error == NFSERR_BADSESSION ||
11366ca35587Sdholland 	    (error == NFSERR_OLDSTATEID && retrycnt < 20) ||
11376ca35587Sdholland 	    ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
11386ca35587Sdholland 	     expireret == 0 && clidrev != 0 && retrycnt < 4));
11396ca35587Sdholland 	if (error && retrycnt >= 4)
11406ca35587Sdholland 		error = EIO;
11416ca35587Sdholland 	return (error);
11426ca35587Sdholland }
11436ca35587Sdholland 
11446ca35587Sdholland static int
nfsrpc_setattrrpc(vnode_t vp,struct vattr * vap,nfsv4stateid_t * stateidp,struct ucred * cred,NFSPROC_T * p,struct nfsvattr * rnap,int * attrflagp,void * stuff)11456ca35587Sdholland nfsrpc_setattrrpc(vnode_t vp, struct vattr *vap,
11466ca35587Sdholland     nfsv4stateid_t *stateidp, struct ucred *cred, NFSPROC_T *p,
11476ca35587Sdholland     struct nfsvattr *rnap, int *attrflagp, void *stuff)
11486ca35587Sdholland {
11496ca35587Sdholland 	u_int32_t *tl;
11506ca35587Sdholland 	struct nfsrv_descript nfsd, *nd = &nfsd;
11516ca35587Sdholland 	int error;
11526ca35587Sdholland 	nfsattrbit_t attrbits;
11536ca35587Sdholland 
11546ca35587Sdholland 	*attrflagp = 0;
11556ca35587Sdholland 	NFSCL_REQSTART(nd, NFSPROC_SETATTR, vp);
11566ca35587Sdholland 	if (nd->nd_flag & ND_NFSV4)
11576ca35587Sdholland 		nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID);
11586ca35587Sdholland 	vap->va_type = vnode_vtype(vp);
11596ca35587Sdholland 	nfscl_fillsattr(nd, vap, vp, NFSSATTR_FULL, 0);
11606ca35587Sdholland 	if (nd->nd_flag & ND_NFSV3) {
11616ca35587Sdholland 		NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
11626ca35587Sdholland 		*tl = newnfs_false;
11636ca35587Sdholland 	} else if (nd->nd_flag & ND_NFSV4) {
11646ca35587Sdholland 		NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
11656ca35587Sdholland 		*tl = txdr_unsigned(NFSV4OP_GETATTR);
11666ca35587Sdholland 		NFSGETATTR_ATTRBIT(&attrbits);
11676ca35587Sdholland 		(void) nfsrv_putattrbit(nd, &attrbits);
11686ca35587Sdholland 	}
11696ca35587Sdholland 	error = nfscl_request(nd, vp, p, cred, stuff);
11706ca35587Sdholland 	if (error)
11716ca35587Sdholland 		return (error);
11726ca35587Sdholland 	if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4))
11736ca35587Sdholland 		error = nfscl_wcc_data(nd, vp, rnap, attrflagp, NULL, stuff);
11746ca35587Sdholland 	if ((nd->nd_flag & ND_NFSV4) && !error)
11756ca35587Sdholland 		error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
11766ca35587Sdholland 	if (!(nd->nd_flag & ND_NFSV3) && !nd->nd_repstat && !error)
11776ca35587Sdholland 		error = nfscl_postop_attr(nd, rnap, attrflagp, stuff);
11786ca35587Sdholland 	mbuf_freem(nd->nd_mrep);
11796ca35587Sdholland 	if (nd->nd_repstat && !error)
11806ca35587Sdholland 		error = nd->nd_repstat;
11816ca35587Sdholland 	return (error);
11826ca35587Sdholland }
11836ca35587Sdholland 
11846ca35587Sdholland /*
11856ca35587Sdholland  * nfs lookup rpc
11866ca35587Sdholland  */
11876ca35587Sdholland APPLESTATIC int
nfsrpc_lookup(vnode_t dvp,char * name,int len,struct ucred * cred,NFSPROC_T * p,struct nfsvattr * dnap,struct nfsvattr * nap,struct nfsfh ** nfhpp,int * attrflagp,int * dattrflagp,void * stuff)11886ca35587Sdholland nfsrpc_lookup(vnode_t dvp, char *name, int len, struct ucred *cred,
11896ca35587Sdholland     NFSPROC_T *p, struct nfsvattr *dnap, struct nfsvattr *nap,
11906ca35587Sdholland     struct nfsfh **nfhpp, int *attrflagp, int *dattrflagp, void *stuff)
11916ca35587Sdholland {
11926ca35587Sdholland 	u_int32_t *tl;
11936ca35587Sdholland 	struct nfsrv_descript nfsd, *nd = &nfsd;
11946ca35587Sdholland 	struct nfsmount *nmp;
11956ca35587Sdholland 	struct nfsnode *np;
11966ca35587Sdholland 	struct nfsfh *nfhp;
11976ca35587Sdholland 	nfsattrbit_t attrbits;
11986ca35587Sdholland 	int error = 0, lookupp = 0;
11996ca35587Sdholland 
12006ca35587Sdholland 	*attrflagp = 0;
12016ca35587Sdholland 	*dattrflagp = 0;
12026ca35587Sdholland 	if (vnode_vtype(dvp) != VDIR)
12036ca35587Sdholland 		return (ENOTDIR);
12046ca35587Sdholland 	nmp = VFSTONFS(vnode_mount(dvp));
12056ca35587Sdholland 	if (len > NFS_MAXNAMLEN)
12066ca35587Sdholland 		return (ENAMETOOLONG);
12076ca35587Sdholland 	if (NFSHASNFSV4(nmp) && len == 1 &&
12086ca35587Sdholland 		name[0] == '.') {
12096ca35587Sdholland 		/*
12106ca35587Sdholland 		 * Just return the current dir's fh.
12116ca35587Sdholland 		 */
12126ca35587Sdholland 		np = VTONFS(dvp);
12136ca35587Sdholland 		MALLOC(nfhp, struct nfsfh *, sizeof (struct nfsfh) +
12146ca35587Sdholland 			np->n_fhp->nfh_len, M_NFSFH, M_WAITOK);
12156ca35587Sdholland 		nfhp->nfh_len = np->n_fhp->nfh_len;
12166ca35587Sdholland 		NFSBCOPY(np->n_fhp->nfh_fh, nfhp->nfh_fh, nfhp->nfh_len);
12176ca35587Sdholland 		*nfhpp = nfhp;
12186ca35587Sdholland 		return (0);
12196ca35587Sdholland 	}
12206ca35587Sdholland 	if (NFSHASNFSV4(nmp) && len == 2 &&
12216ca35587Sdholland 		name[0] == '.' && name[1] == '.') {
12226ca35587Sdholland 		lookupp = 1;
12236ca35587Sdholland 		NFSCL_REQSTART(nd, NFSPROC_LOOKUPP, dvp);
12246ca35587Sdholland 	} else {
12256ca35587Sdholland 		NFSCL_REQSTART(nd, NFSPROC_LOOKUP, dvp);
12266ca35587Sdholland 		(void) nfsm_strtom(nd, name, len);
12276ca35587Sdholland 	}
12286ca35587Sdholland 	if (nd->nd_flag & ND_NFSV4) {
12296ca35587Sdholland 		NFSGETATTR_ATTRBIT(&attrbits);
12306ca35587Sdholland 		NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
12316ca35587Sdholland 		*tl++ = txdr_unsigned(NFSV4OP_GETFH);
12326ca35587Sdholland 		*tl = txdr_unsigned(NFSV4OP_GETATTR);
12336ca35587Sdholland 		(void) nfsrv_putattrbit(nd, &attrbits);
12346ca35587Sdholland 	}
12356ca35587Sdholland 	error = nfscl_request(nd, dvp, p, cred, stuff);
12366ca35587Sdholland 	if (error)
12376ca35587Sdholland 		return (error);
12386ca35587Sdholland 	if (nd->nd_repstat) {
12396ca35587Sdholland 		/*
12406ca35587Sdholland 		 * When an NFSv4 Lookupp returns ENOENT, it means that
12416ca35587Sdholland 		 * the lookup is at the root of an fs, so return this dir.
12426ca35587Sdholland 		 */
12436ca35587Sdholland 		if (nd->nd_repstat == NFSERR_NOENT && lookupp) {
12446ca35587Sdholland 		    np = VTONFS(dvp);
12456ca35587Sdholland 		    MALLOC(nfhp, struct nfsfh *, sizeof (struct nfsfh) +
12466ca35587Sdholland 			np->n_fhp->nfh_len, M_NFSFH, M_WAITOK);
12476ca35587Sdholland 		    nfhp->nfh_len = np->n_fhp->nfh_len;
12486ca35587Sdholland 		    NFSBCOPY(np->n_fhp->nfh_fh, nfhp->nfh_fh, nfhp->nfh_len);
12496ca35587Sdholland 		    *nfhpp = nfhp;
12506ca35587Sdholland 		    mbuf_freem(nd->nd_mrep);
12516ca35587Sdholland 		    return (0);
12526ca35587Sdholland 		}
12536ca35587Sdholland 		if (nd->nd_flag & ND_NFSV3)
12546ca35587Sdholland 		    error = nfscl_postop_attr(nd, dnap, dattrflagp, stuff);
1255e81f0ea2Spgoyette 		else if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) ==
1256e81f0ea2Spgoyette 		    ND_NFSV4) {
1257e81f0ea2Spgoyette 			/* Load the directory attributes. */
1258e81f0ea2Spgoyette 			error = nfsm_loadattr(nd, dnap);
1259e81f0ea2Spgoyette 			if (error == 0)
1260e81f0ea2Spgoyette 				*dattrflagp = 1;
1261e81f0ea2Spgoyette 		}
12626ca35587Sdholland 		goto nfsmout;
12636ca35587Sdholland 	}
12646ca35587Sdholland 	if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) == ND_NFSV4) {
1265e81f0ea2Spgoyette 		/* Load the directory attributes. */
1266e81f0ea2Spgoyette 		error = nfsm_loadattr(nd, dnap);
1267e81f0ea2Spgoyette 		if (error != 0)
12686ca35587Sdholland 			goto nfsmout;
1269e81f0ea2Spgoyette 		*dattrflagp = 1;
1270e81f0ea2Spgoyette 		/* Skip over the Lookup and GetFH operation status values. */
1271e81f0ea2Spgoyette 		NFSM_DISSECT(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
12726ca35587Sdholland 	}
12736ca35587Sdholland 	error = nfsm_getfh(nd, nfhpp);
12746ca35587Sdholland 	if (error)
12756ca35587Sdholland 		goto nfsmout;
12766ca35587Sdholland 
12776ca35587Sdholland 	error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
12786ca35587Sdholland 	if ((nd->nd_flag & ND_NFSV3) && !error)
12796ca35587Sdholland 		error = nfscl_postop_attr(nd, dnap, dattrflagp, stuff);
12806ca35587Sdholland nfsmout:
12816ca35587Sdholland 	mbuf_freem(nd->nd_mrep);
12826ca35587Sdholland 	if (!error && nd->nd_repstat)
12836ca35587Sdholland 		error = nd->nd_repstat;
12846ca35587Sdholland 	return (error);
12856ca35587Sdholland }
12866ca35587Sdholland 
12876ca35587Sdholland /*
12886ca35587Sdholland  * Do a readlink rpc.
12896ca35587Sdholland  */
12906ca35587Sdholland APPLESTATIC int
nfsrpc_readlink(vnode_t vp,struct uio * uiop,struct ucred * cred,NFSPROC_T * p,struct nfsvattr * nap,int * attrflagp,void * stuff)12916ca35587Sdholland nfsrpc_readlink(vnode_t vp, struct uio *uiop, struct ucred *cred,
12926ca35587Sdholland     NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, void *stuff)
12936ca35587Sdholland {
12946ca35587Sdholland 	u_int32_t *tl;
12956ca35587Sdholland 	struct nfsrv_descript nfsd, *nd = &nfsd;
12966ca35587Sdholland 	struct nfsnode *np = VTONFS(vp);
12976ca35587Sdholland 	nfsattrbit_t attrbits;
12986ca35587Sdholland 	int error, len, cangetattr = 1;
12996ca35587Sdholland 
13006ca35587Sdholland 	*attrflagp = 0;
13016ca35587Sdholland 	NFSCL_REQSTART(nd, NFSPROC_READLINK, vp);
13026ca35587Sdholland 	if (nd->nd_flag & ND_NFSV4) {
13036ca35587Sdholland 		/*
13046ca35587Sdholland 		 * And do a Getattr op.
13056ca35587Sdholland 		 */
13066ca35587Sdholland 		NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
13076ca35587Sdholland 		*tl = txdr_unsigned(NFSV4OP_GETATTR);
13086ca35587Sdholland 		NFSGETATTR_ATTRBIT(&attrbits);
13096ca35587Sdholland 		(void) nfsrv_putattrbit(nd, &attrbits);
13106ca35587Sdholland 	}
13116ca35587Sdholland 	error = nfscl_request(nd, vp, p, cred, stuff);
13126ca35587Sdholland 	if (error)
13136ca35587Sdholland 		return (error);
13146ca35587Sdholland 	if (nd->nd_flag & ND_NFSV3)
13156ca35587Sdholland 		error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
13166ca35587Sdholland 	if (!nd->nd_repstat && !error) {
13176ca35587Sdholland 		NFSM_STRSIZ(len, NFS_MAXPATHLEN);
13186ca35587Sdholland 		/*
13196ca35587Sdholland 		 * This seems weird to me, but must have been added to
13206ca35587Sdholland 		 * FreeBSD for some reason. The only thing I can think of
13216ca35587Sdholland 		 * is that there was/is some server that replies with
13226ca35587Sdholland 		 * more link data than it should?
13236ca35587Sdholland 		 */
13246ca35587Sdholland 		if (len == NFS_MAXPATHLEN) {
13256ca35587Sdholland 			NFSLOCKNODE(np);
13266ca35587Sdholland 			if (np->n_size > 0 && np->n_size < NFS_MAXPATHLEN) {
13276ca35587Sdholland 				len = np->n_size;
13286ca35587Sdholland 				cangetattr = 0;
13296ca35587Sdholland 			}
13306ca35587Sdholland 			NFSUNLOCKNODE(np);
13316ca35587Sdholland 		}
13326ca35587Sdholland 		error = nfsm_mbufuio(nd, uiop, len);
13336ca35587Sdholland 		if ((nd->nd_flag & ND_NFSV4) && !error && cangetattr)
13346ca35587Sdholland 			error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
13356ca35587Sdholland 	}
13366ca35587Sdholland 	if (nd->nd_repstat && !error)
13376ca35587Sdholland 		error = nd->nd_repstat;
13386ca35587Sdholland nfsmout:
13396ca35587Sdholland 	mbuf_freem(nd->nd_mrep);
13406ca35587Sdholland 	return (error);
13416ca35587Sdholland }
13426ca35587Sdholland 
13436ca35587Sdholland /*
13446ca35587Sdholland  * Read operation.
13456ca35587Sdholland  */
13466ca35587Sdholland APPLESTATIC int
nfsrpc_read(vnode_t vp,struct uio * uiop,struct ucred * cred,NFSPROC_T * p,struct nfsvattr * nap,int * attrflagp,void * stuff)13476ca35587Sdholland nfsrpc_read(vnode_t vp, struct uio *uiop, struct ucred *cred,
13486ca35587Sdholland     NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, void *stuff)
13496ca35587Sdholland {
13506ca35587Sdholland 	int error, expireret = 0, retrycnt;
13516ca35587Sdholland 	u_int32_t clidrev = 0;
13526ca35587Sdholland 	struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
13536ca35587Sdholland 	struct nfsnode *np = VTONFS(vp);
13546ca35587Sdholland 	struct ucred *newcred;
13556ca35587Sdholland 	struct nfsfh *nfhp = NULL;
13566ca35587Sdholland 	nfsv4stateid_t stateid;
13576ca35587Sdholland 	void *lckp;
13586ca35587Sdholland 
13596ca35587Sdholland 	if (nmp->nm_clp != NULL)
13606ca35587Sdholland 		clidrev = nmp->nm_clp->nfsc_clientidrev;
13616ca35587Sdholland 	newcred = cred;
13626ca35587Sdholland 	if (NFSHASNFSV4(nmp)) {
13636ca35587Sdholland 		nfhp = np->n_fhp;
13646ca35587Sdholland 		newcred = NFSNEWCRED(cred);
13656ca35587Sdholland 	}
13666ca35587Sdholland 	retrycnt = 0;
13676ca35587Sdholland 	do {
13686ca35587Sdholland 		lckp = NULL;
13696ca35587Sdholland 		if (NFSHASNFSV4(nmp))
13706ca35587Sdholland 			(void)nfscl_getstateid(vp, nfhp->nfh_fh, nfhp->nfh_len,
13716ca35587Sdholland 			    NFSV4OPEN_ACCESSREAD, 0, newcred, p, &stateid,
13726ca35587Sdholland 			    &lckp);
13736ca35587Sdholland 		error = nfsrpc_readrpc(vp, uiop, newcred, &stateid, p, nap,
13746ca35587Sdholland 		    attrflagp, stuff);
13756ca35587Sdholland 		if (error == NFSERR_STALESTATEID || error == NFSERR_BADSESSION)
13766ca35587Sdholland 			nfscl_initiate_recovery(nmp->nm_clp);
13776ca35587Sdholland 		if (lckp != NULL)
13786ca35587Sdholland 			nfscl_lockderef(lckp);
13796ca35587Sdholland 		if (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
13806ca35587Sdholland 		    error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
13816ca35587Sdholland 		    error == NFSERR_OLDSTATEID || error == NFSERR_BADSESSION) {
13826ca35587Sdholland 			(void) nfs_catnap(PZERO, error, "nfs_read");
13836ca35587Sdholland 		} else if ((error == NFSERR_EXPIRED ||
13846ca35587Sdholland 		    error == NFSERR_BADSTATEID) && clidrev != 0) {
13856ca35587Sdholland 			expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
13866ca35587Sdholland 		}
13876ca35587Sdholland 		retrycnt++;
13886ca35587Sdholland 	} while (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
13896ca35587Sdholland 	    error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
13906ca35587Sdholland 	    error == NFSERR_BADSESSION ||
13916ca35587Sdholland 	    (error == NFSERR_OLDSTATEID && retrycnt < 20) ||
13926ca35587Sdholland 	    ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
13936ca35587Sdholland 	     expireret == 0 && clidrev != 0 && retrycnt < 4));
13946ca35587Sdholland 	if (error && retrycnt >= 4)
13956ca35587Sdholland 		error = EIO;
13966ca35587Sdholland 	if (NFSHASNFSV4(nmp))
13976ca35587Sdholland 		NFSFREECRED(newcred);
13986ca35587Sdholland 	return (error);
13996ca35587Sdholland }
14006ca35587Sdholland 
14016ca35587Sdholland /*
14026ca35587Sdholland  * The actual read RPC.
14036ca35587Sdholland  */
14046ca35587Sdholland static int
nfsrpc_readrpc(vnode_t vp,struct uio * uiop,struct ucred * cred,nfsv4stateid_t * stateidp,NFSPROC_T * p,struct nfsvattr * nap,int * attrflagp,void * stuff)14056ca35587Sdholland nfsrpc_readrpc(vnode_t vp, struct uio *uiop, struct ucred *cred,
14066ca35587Sdholland     nfsv4stateid_t *stateidp, NFSPROC_T *p, struct nfsvattr *nap,
14076ca35587Sdholland     int *attrflagp, void *stuff)
14086ca35587Sdholland {
14096ca35587Sdholland 	u_int32_t *tl;
14106ca35587Sdholland 	int error = 0, len, retlen, tsiz, eof = 0;
14116ca35587Sdholland 	struct nfsrv_descript nfsd;
14126ca35587Sdholland 	struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
14136ca35587Sdholland 	struct nfsrv_descript *nd = &nfsd;
14146ca35587Sdholland 	int rsize;
14156ca35587Sdholland 	off_t tmp_off;
14166ca35587Sdholland 
14176ca35587Sdholland 	*attrflagp = 0;
14186ca35587Sdholland 	tsiz = uio_uio_resid(uiop);
14196ca35587Sdholland 	tmp_off = uiop->uio_offset + tsiz;
14206ca35587Sdholland 	NFSLOCKMNT(nmp);
14216ca35587Sdholland 	if (tmp_off > nmp->nm_maxfilesize || tmp_off < uiop->uio_offset) {
14226ca35587Sdholland 		NFSUNLOCKMNT(nmp);
14236ca35587Sdholland 		return (EFBIG);
14246ca35587Sdholland 	}
14256ca35587Sdholland 	rsize = nmp->nm_rsize;
14266ca35587Sdholland 	NFSUNLOCKMNT(nmp);
14276ca35587Sdholland 	nd->nd_mrep = NULL;
14286ca35587Sdholland 	while (tsiz > 0) {
14296ca35587Sdholland 		*attrflagp = 0;
14306ca35587Sdholland 		len = (tsiz > rsize) ? rsize : tsiz;
14316ca35587Sdholland 		NFSCL_REQSTART(nd, NFSPROC_READ, vp);
14326ca35587Sdholland 		if (nd->nd_flag & ND_NFSV4)
14336ca35587Sdholland 			nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID);
14346ca35587Sdholland 		NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED * 3);
14356ca35587Sdholland 		if (nd->nd_flag & ND_NFSV2) {
14366ca35587Sdholland 			*tl++ = txdr_unsigned(uiop->uio_offset);
14376ca35587Sdholland 			*tl++ = txdr_unsigned(len);
14386ca35587Sdholland 			*tl = 0;
14396ca35587Sdholland 		} else {
14406ca35587Sdholland 			txdr_hyper(uiop->uio_offset, tl);
14416ca35587Sdholland 			*(tl + 2) = txdr_unsigned(len);
14426ca35587Sdholland 		}
14436ca35587Sdholland 		/*
14446ca35587Sdholland 		 * Since I can't do a Getattr for NFSv4 for Write, there
14456ca35587Sdholland 		 * doesn't seem any point in doing one here, either.
14466ca35587Sdholland 		 * (See the comment in nfsrpc_writerpc() for more info.)
14476ca35587Sdholland 		 */
14486ca35587Sdholland 		error = nfscl_request(nd, vp, p, cred, stuff);
14496ca35587Sdholland 		if (error)
14506ca35587Sdholland 			return (error);
14516ca35587Sdholland 		if (nd->nd_flag & ND_NFSV3) {
14526ca35587Sdholland 			error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
14536ca35587Sdholland 		} else if (!nd->nd_repstat && (nd->nd_flag & ND_NFSV2)) {
14546ca35587Sdholland 			error = nfsm_loadattr(nd, nap);
14556ca35587Sdholland 			if (!error)
14566ca35587Sdholland 				*attrflagp = 1;
14576ca35587Sdholland 		}
14586ca35587Sdholland 		if (nd->nd_repstat || error) {
14596ca35587Sdholland 			if (!error)
14606ca35587Sdholland 				error = nd->nd_repstat;
14616ca35587Sdholland 			goto nfsmout;
14626ca35587Sdholland 		}
14636ca35587Sdholland 		if (nd->nd_flag & ND_NFSV3) {
14646ca35587Sdholland 			NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
14656ca35587Sdholland 			eof = fxdr_unsigned(int, *(tl + 1));
14666ca35587Sdholland 		} else if (nd->nd_flag & ND_NFSV4) {
14676ca35587Sdholland 			NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
14686ca35587Sdholland 			eof = fxdr_unsigned(int, *tl);
14696ca35587Sdholland 		}
14706ca35587Sdholland 		NFSM_STRSIZ(retlen, len);
14716ca35587Sdholland 		error = nfsm_mbufuio(nd, uiop, retlen);
14726ca35587Sdholland 		if (error)
14736ca35587Sdholland 			goto nfsmout;
14746ca35587Sdholland 		mbuf_freem(nd->nd_mrep);
14756ca35587Sdholland 		nd->nd_mrep = NULL;
14766ca35587Sdholland 		tsiz -= retlen;
14776ca35587Sdholland 		if (!(nd->nd_flag & ND_NFSV2)) {
14786ca35587Sdholland 			if (eof || retlen == 0)
14796ca35587Sdholland 				tsiz = 0;
14806ca35587Sdholland 		} else if (retlen < len)
14816ca35587Sdholland 			tsiz = 0;
14826ca35587Sdholland 	}
14836ca35587Sdholland 	return (0);
14846ca35587Sdholland nfsmout:
14856ca35587Sdholland 	mbuf_freem(nd->nd_mrep);
14866ca35587Sdholland 	return (error);
14876ca35587Sdholland }
14886ca35587Sdholland 
14896ca35587Sdholland /*
14906ca35587Sdholland  * nfs write operation
14916ca35587Sdholland  * When called_from_strategy != 0, it should return EIO for an error that
14926ca35587Sdholland  * indicates recovery is in progress, so that the buffer will be left
14936ca35587Sdholland  * dirty and be written back to the server later. If it loops around,
14946ca35587Sdholland  * the recovery thread could get stuck waiting for the buffer and recovery
14956ca35587Sdholland  * will then deadlock.
14966ca35587Sdholland  */
14976ca35587Sdholland APPLESTATIC int
nfsrpc_write(vnode_t vp,struct uio * uiop,int * iomode,int * must_commit,struct ucred * cred,NFSPROC_T * p,struct nfsvattr * nap,int * attrflagp,void * stuff,int called_from_strategy)14986ca35587Sdholland nfsrpc_write(vnode_t vp, struct uio *uiop, int *iomode, int *must_commit,
14996ca35587Sdholland     struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp,
15006ca35587Sdholland     void *stuff, int called_from_strategy)
15016ca35587Sdholland {
15026ca35587Sdholland 	int error, expireret = 0, retrycnt, nostateid;
15036ca35587Sdholland 	u_int32_t clidrev = 0;
15046ca35587Sdholland 	struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
15056ca35587Sdholland 	struct nfsnode *np = VTONFS(vp);
15066ca35587Sdholland 	struct ucred *newcred;
15076ca35587Sdholland 	struct nfsfh *nfhp = NULL;
15086ca35587Sdholland 	nfsv4stateid_t stateid;
15096ca35587Sdholland 	void *lckp;
15106ca35587Sdholland 
15116ca35587Sdholland 	*must_commit = 0;
15126ca35587Sdholland 	if (nmp->nm_clp != NULL)
15136ca35587Sdholland 		clidrev = nmp->nm_clp->nfsc_clientidrev;
15146ca35587Sdholland 	newcred = cred;
15156ca35587Sdholland 	if (NFSHASNFSV4(nmp)) {
15166ca35587Sdholland 		newcred = NFSNEWCRED(cred);
15176ca35587Sdholland 		nfhp = np->n_fhp;
15186ca35587Sdholland 	}
15196ca35587Sdholland 	retrycnt = 0;
15206ca35587Sdholland 	do {
15216ca35587Sdholland 		lckp = NULL;
15226ca35587Sdholland 		nostateid = 0;
15236ca35587Sdholland 		if (NFSHASNFSV4(nmp)) {
15246ca35587Sdholland 			(void)nfscl_getstateid(vp, nfhp->nfh_fh, nfhp->nfh_len,
15256ca35587Sdholland 			    NFSV4OPEN_ACCESSWRITE, 0, newcred, p, &stateid,
15266ca35587Sdholland 			    &lckp);
15276ca35587Sdholland 			if (stateid.other[0] == 0 && stateid.other[1] == 0 &&
15286ca35587Sdholland 			    stateid.other[2] == 0) {
15296ca35587Sdholland 				nostateid = 1;
15306ca35587Sdholland 				NFSCL_DEBUG(1, "stateid0 in write\n");
15316ca35587Sdholland 			}
15326ca35587Sdholland 		}
15336ca35587Sdholland 
15346ca35587Sdholland 		/*
15356ca35587Sdholland 		 * If there is no stateid for NFSv4, it means this is an
15366ca35587Sdholland 		 * extraneous write after close. Basically a poorly
15376ca35587Sdholland 		 * implemented buffer cache. Just don't do the write.
15386ca35587Sdholland 		 */
15396ca35587Sdholland 		if (nostateid)
15406ca35587Sdholland 			error = 0;
15416ca35587Sdholland 		else
15426ca35587Sdholland 			error = nfsrpc_writerpc(vp, uiop, iomode, must_commit,
15436ca35587Sdholland 			    newcred, &stateid, p, nap, attrflagp, stuff);
15446ca35587Sdholland 		if (error == NFSERR_STALESTATEID || error == NFSERR_BADSESSION)
15456ca35587Sdholland 			nfscl_initiate_recovery(nmp->nm_clp);
15466ca35587Sdholland 		if (lckp != NULL)
15476ca35587Sdholland 			nfscl_lockderef(lckp);
15486ca35587Sdholland 		if (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
15496ca35587Sdholland 		    error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
15506ca35587Sdholland 		    error == NFSERR_OLDSTATEID || error == NFSERR_BADSESSION) {
15516ca35587Sdholland 			(void) nfs_catnap(PZERO, error, "nfs_write");
15526ca35587Sdholland 		} else if ((error == NFSERR_EXPIRED ||
15536ca35587Sdholland 		    error == NFSERR_BADSTATEID) && clidrev != 0) {
15546ca35587Sdholland 			expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
15556ca35587Sdholland 		}
15566ca35587Sdholland 		retrycnt++;
15576ca35587Sdholland 	} while (error == NFSERR_GRACE || error == NFSERR_DELAY ||
15586ca35587Sdholland 	    ((error == NFSERR_STALESTATEID || error == NFSERR_BADSESSION ||
15596ca35587Sdholland 	      error == NFSERR_STALEDONTRECOVER) && called_from_strategy == 0) ||
15606ca35587Sdholland 	    (error == NFSERR_OLDSTATEID && retrycnt < 20) ||
15616ca35587Sdholland 	    ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
15626ca35587Sdholland 	     expireret == 0 && clidrev != 0 && retrycnt < 4));
15636ca35587Sdholland 	if (error != 0 && (retrycnt >= 4 ||
15646ca35587Sdholland 	    ((error == NFSERR_STALESTATEID || error == NFSERR_BADSESSION ||
15656ca35587Sdholland 	      error == NFSERR_STALEDONTRECOVER) && called_from_strategy != 0)))
15666ca35587Sdholland 		error = EIO;
15676ca35587Sdholland 	if (NFSHASNFSV4(nmp))
15686ca35587Sdholland 		NFSFREECRED(newcred);
15696ca35587Sdholland 	return (error);
15706ca35587Sdholland }
15716ca35587Sdholland 
15726ca35587Sdholland /*
15736ca35587Sdholland  * The actual write RPC.
15746ca35587Sdholland  */
15756ca35587Sdholland static int
nfsrpc_writerpc(vnode_t vp,struct uio * uiop,int * iomode,int * must_commit,struct ucred * cred,nfsv4stateid_t * stateidp,NFSPROC_T * p,struct nfsvattr * nap,int * attrflagp,void * stuff)15766ca35587Sdholland nfsrpc_writerpc(vnode_t vp, struct uio *uiop, int *iomode,
15776ca35587Sdholland     int *must_commit, struct ucred *cred, nfsv4stateid_t *stateidp,
15786ca35587Sdholland     NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, void *stuff)
15796ca35587Sdholland {
15806ca35587Sdholland 	u_int32_t *tl;
15816ca35587Sdholland 	struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
15826ca35587Sdholland 	struct nfsnode *np = VTONFS(vp);
15836ca35587Sdholland 	int error = 0, len, tsiz, rlen, commit, committed = NFSWRITE_FILESYNC;
15846ca35587Sdholland 	int wccflag = 0, wsize;
15856ca35587Sdholland 	int32_t backup;
15866ca35587Sdholland 	struct nfsrv_descript nfsd;
15876ca35587Sdholland 	struct nfsrv_descript *nd = &nfsd;
15886ca35587Sdholland 	nfsattrbit_t attrbits;
15896ca35587Sdholland 	off_t tmp_off;
15906ca35587Sdholland 
15916ca35587Sdholland 	KASSERT(uiop->uio_iovcnt == 1, ("nfs: writerpc iovcnt > 1"));
15926ca35587Sdholland 	*attrflagp = 0;
15936ca35587Sdholland 	tsiz = uio_uio_resid(uiop);
15946ca35587Sdholland 	tmp_off = uiop->uio_offset + tsiz;
15956ca35587Sdholland 	NFSLOCKMNT(nmp);
15966ca35587Sdholland 	if (tmp_off > nmp->nm_maxfilesize || tmp_off < uiop->uio_offset) {
15976ca35587Sdholland 		NFSUNLOCKMNT(nmp);
15986ca35587Sdholland 		return (EFBIG);
15996ca35587Sdholland 	}
16006ca35587Sdholland 	wsize = nmp->nm_wsize;
16016ca35587Sdholland 	NFSUNLOCKMNT(nmp);
16026ca35587Sdholland 	nd->nd_mrep = NULL;	/* NFSv2 sometimes does a write with */
16036ca35587Sdholland 	nd->nd_repstat = 0;	/* uio_resid == 0, so the while is not done */
16046ca35587Sdholland 	while (tsiz > 0) {
16056ca35587Sdholland 		*attrflagp = 0;
16066ca35587Sdholland 		len = (tsiz > wsize) ? wsize : tsiz;
16076ca35587Sdholland 		NFSCL_REQSTART(nd, NFSPROC_WRITE, vp);
16086ca35587Sdholland 		if (nd->nd_flag & ND_NFSV4) {
16096ca35587Sdholland 			nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID);
16106ca35587Sdholland 			NFSM_BUILD(tl, u_int32_t *, NFSX_HYPER+2*NFSX_UNSIGNED);
16116ca35587Sdholland 			txdr_hyper(uiop->uio_offset, tl);
16126ca35587Sdholland 			tl += 2;
16136ca35587Sdholland 			*tl++ = txdr_unsigned(*iomode);
16146ca35587Sdholland 			*tl = txdr_unsigned(len);
16156ca35587Sdholland 		} else if (nd->nd_flag & ND_NFSV3) {
16166ca35587Sdholland 			NFSM_BUILD(tl, u_int32_t *, NFSX_HYPER+3*NFSX_UNSIGNED);
16176ca35587Sdholland 			txdr_hyper(uiop->uio_offset, tl);
16186ca35587Sdholland 			tl += 2;
16196ca35587Sdholland 			*tl++ = txdr_unsigned(len);
16206ca35587Sdholland 			*tl++ = txdr_unsigned(*iomode);
16216ca35587Sdholland 			*tl = txdr_unsigned(len);
16226ca35587Sdholland 		} else {
16236ca35587Sdholland 			u_int32_t x;
16246ca35587Sdholland 
16256ca35587Sdholland 			NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
16266ca35587Sdholland 			/*
16276ca35587Sdholland 			 * Not sure why someone changed this, since the
16286ca35587Sdholland 			 * RFC clearly states that "beginoffset" and
16296ca35587Sdholland 			 * "totalcount" are ignored, but it wouldn't
16306ca35587Sdholland 			 * surprise me if there's a busted server out there.
16316ca35587Sdholland 			 */
16326ca35587Sdholland 			/* Set both "begin" and "current" to non-garbage. */
16336ca35587Sdholland 			x = txdr_unsigned((u_int32_t)uiop->uio_offset);
16346ca35587Sdholland 			*tl++ = x;      /* "begin offset" */
16356ca35587Sdholland 			*tl++ = x;      /* "current offset" */
16366ca35587Sdholland 			x = txdr_unsigned(len);
16376ca35587Sdholland 			*tl++ = x;      /* total to this offset */
16386ca35587Sdholland 			*tl = x;        /* size of this write */
16396ca35587Sdholland 
16406ca35587Sdholland 		}
16416ca35587Sdholland 		nfsm_uiombuf(nd, uiop, len);
16426ca35587Sdholland 		/*
16436ca35587Sdholland 		 * Although it is tempting to do a normal Getattr Op in the
16446ca35587Sdholland 		 * NFSv4 compound, the result can be a nearly hung client
16456ca35587Sdholland 		 * system if the Getattr asks for Owner and/or OwnerGroup.
16466ca35587Sdholland 		 * It occurs when the client can't map either the Owner or
16476ca35587Sdholland 		 * Owner_group name in the Getattr reply to a uid/gid. When
16486ca35587Sdholland 		 * there is a cache miss, the kernel does an upcall to the
16496ca35587Sdholland 		 * nfsuserd. Then, it can try and read the local /etc/passwd
16506ca35587Sdholland 		 * or /etc/group file. It can then block in getnewbuf(),
16516ca35587Sdholland 		 * waiting for dirty writes to be pushed to the NFS server.
16526ca35587Sdholland 		 * The only reason this doesn't result in a complete
16536ca35587Sdholland 		 * deadlock, is that the upcall times out and allows
16546ca35587Sdholland 		 * the write to complete. However, progress is so slow
16556ca35587Sdholland 		 * that it might just as well be deadlocked.
16566ca35587Sdholland 		 * As such, we get the rest of the attributes, but not
16576ca35587Sdholland 		 * Owner or Owner_group.
16586ca35587Sdholland 		 * nb: nfscl_loadattrcache() needs to be told that these
16596ca35587Sdholland 		 *     partial attributes from a write rpc are being
16606ca35587Sdholland 		 *     passed in, via a argument flag.
16616ca35587Sdholland 		 */
16626ca35587Sdholland 		if (nd->nd_flag & ND_NFSV4) {
16636ca35587Sdholland 			NFSWRITEGETATTR_ATTRBIT(&attrbits);
16646ca35587Sdholland 			NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
16656ca35587Sdholland 			*tl = txdr_unsigned(NFSV4OP_GETATTR);
16666ca35587Sdholland 			(void) nfsrv_putattrbit(nd, &attrbits);
16676ca35587Sdholland 		}
16686ca35587Sdholland 		error = nfscl_request(nd, vp, p, cred, stuff);
16696ca35587Sdholland 		if (error)
16706ca35587Sdholland 			return (error);
16716ca35587Sdholland 		if (nd->nd_repstat) {
16726ca35587Sdholland 			/*
16736ca35587Sdholland 			 * In case the rpc gets retried, roll
16746ca35587Sdholland 			 * the uio fileds changed by nfsm_uiombuf()
16756ca35587Sdholland 			 * back.
16766ca35587Sdholland 			 */
16776ca35587Sdholland 			uiop->uio_offset -= len;
16786ca35587Sdholland 			uio_uio_resid_add(uiop, len);
16796ca35587Sdholland 			uio_iov_base_add(uiop, -len);
16806ca35587Sdholland 			uio_iov_len_add(uiop, len);
16816ca35587Sdholland 		}
16826ca35587Sdholland 		if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) {
16836ca35587Sdholland 			error = nfscl_wcc_data(nd, vp, nap, attrflagp,
16846ca35587Sdholland 			    &wccflag, stuff);
16856ca35587Sdholland 			if (error)
16866ca35587Sdholland 				goto nfsmout;
16876ca35587Sdholland 		}
16886ca35587Sdholland 		if (!nd->nd_repstat) {
16896ca35587Sdholland 			if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) {
16906ca35587Sdholland 				NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED
16916ca35587Sdholland 					+ NFSX_VERF);
16926ca35587Sdholland 				rlen = fxdr_unsigned(int, *tl++);
16936ca35587Sdholland 				if (rlen == 0) {
16946ca35587Sdholland 					error = NFSERR_IO;
16956ca35587Sdholland 					goto nfsmout;
16966ca35587Sdholland 				} else if (rlen < len) {
16976ca35587Sdholland 					backup = len - rlen;
16986ca35587Sdholland 					uio_iov_base_add(uiop, -(backup));
16996ca35587Sdholland 					uio_iov_len_add(uiop, backup);
17006ca35587Sdholland 					uiop->uio_offset -= backup;
17016ca35587Sdholland 					uio_uio_resid_add(uiop, backup);
17026ca35587Sdholland 					len = rlen;
17036ca35587Sdholland 				}
17046ca35587Sdholland 				commit = fxdr_unsigned(int, *tl++);
17056ca35587Sdholland 
17066ca35587Sdholland 				/*
1707e81f0ea2Spgoyette 				 * Return the lowest commitment level
17086ca35587Sdholland 				 * obtained by any of the RPCs.
17096ca35587Sdholland 				 */
17106ca35587Sdholland 				if (committed == NFSWRITE_FILESYNC)
17116ca35587Sdholland 					committed = commit;
17126ca35587Sdholland 				else if (committed == NFSWRITE_DATASYNC &&
17136ca35587Sdholland 					commit == NFSWRITE_UNSTABLE)
17146ca35587Sdholland 					committed = commit;
17156ca35587Sdholland 				NFSLOCKMNT(nmp);
17166ca35587Sdholland 				if (!NFSHASWRITEVERF(nmp)) {
17176ca35587Sdholland 					NFSBCOPY((caddr_t)tl,
17186ca35587Sdholland 					    (caddr_t)&nmp->nm_verf[0],
17196ca35587Sdholland 					    NFSX_VERF);
17206ca35587Sdholland 					NFSSETWRITEVERF(nmp);
17216ca35587Sdholland 	    			} else if (NFSBCMP(tl, nmp->nm_verf,
17226ca35587Sdholland 				    NFSX_VERF)) {
17236ca35587Sdholland 					*must_commit = 1;
17246ca35587Sdholland 					NFSBCOPY(tl, nmp->nm_verf, NFSX_VERF);
17256ca35587Sdholland 				}
17266ca35587Sdholland 				NFSUNLOCKMNT(nmp);
17276ca35587Sdholland 			}
17286ca35587Sdholland 			if (nd->nd_flag & ND_NFSV4)
17296ca35587Sdholland 				NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
17306ca35587Sdholland 			if (nd->nd_flag & (ND_NFSV2 | ND_NFSV4)) {
17316ca35587Sdholland 				error = nfsm_loadattr(nd, nap);
17326ca35587Sdholland 				if (!error)
17336ca35587Sdholland 					*attrflagp = NFS_LATTR_NOSHRINK;
17346ca35587Sdholland 			}
17356ca35587Sdholland 		} else {
17366ca35587Sdholland 			error = nd->nd_repstat;
17376ca35587Sdholland 		}
17386ca35587Sdholland 		if (error)
17396ca35587Sdholland 			goto nfsmout;
1740e81f0ea2Spgoyette 		NFSWRITERPC_SETTIME(wccflag, np, nap, (nd->nd_flag & ND_NFSV4));
17416ca35587Sdholland 		mbuf_freem(nd->nd_mrep);
17426ca35587Sdholland 		nd->nd_mrep = NULL;
17436ca35587Sdholland 		tsiz -= len;
17446ca35587Sdholland 	}
17456ca35587Sdholland nfsmout:
17466ca35587Sdholland 	mbuf_freem(nd->nd_mrep);
17476ca35587Sdholland 	*iomode = committed;
17486ca35587Sdholland 	if (nd->nd_repstat && !error)
17496ca35587Sdholland 		error = nd->nd_repstat;
17506ca35587Sdholland 	return (error);
17516ca35587Sdholland }
17526ca35587Sdholland 
17536ca35587Sdholland /*
17546ca35587Sdholland  * nfs mknod rpc
17556ca35587Sdholland  * For NFS v2 this is a kludge. Use a create rpc but with the IFMT bits of the
17566ca35587Sdholland  * mode set to specify the file type and the size field for rdev.
17576ca35587Sdholland  */
17586ca35587Sdholland APPLESTATIC int
nfsrpc_mknod(vnode_t dvp,char * name,int namelen,struct vattr * vap,u_int32_t rdev,enum vtype vtyp,struct ucred * cred,NFSPROC_T * p,struct nfsvattr * dnap,struct nfsvattr * nnap,struct nfsfh ** nfhpp,int * attrflagp,int * dattrflagp,void * dstuff)17596ca35587Sdholland nfsrpc_mknod(vnode_t dvp, char *name, int namelen, struct vattr *vap,
17606ca35587Sdholland     u_int32_t rdev, enum vtype vtyp, struct ucred *cred, NFSPROC_T *p,
17616ca35587Sdholland     struct nfsvattr *dnap, struct nfsvattr *nnap, struct nfsfh **nfhpp,
17626ca35587Sdholland     int *attrflagp, int *dattrflagp, void *dstuff)
17636ca35587Sdholland {
17646ca35587Sdholland 	u_int32_t *tl;
17656ca35587Sdholland 	int error = 0;
17666ca35587Sdholland 	struct nfsrv_descript nfsd, *nd = &nfsd;
17676ca35587Sdholland 	nfsattrbit_t attrbits;
17686ca35587Sdholland 
17696ca35587Sdholland 	*nfhpp = NULL;
17706ca35587Sdholland 	*attrflagp = 0;
17716ca35587Sdholland 	*dattrflagp = 0;
17726ca35587Sdholland 	if (namelen > NFS_MAXNAMLEN)
17736ca35587Sdholland 		return (ENAMETOOLONG);
17746ca35587Sdholland 	NFSCL_REQSTART(nd, NFSPROC_MKNOD, dvp);
17756ca35587Sdholland 	if (nd->nd_flag & ND_NFSV4) {
17766ca35587Sdholland 		if (vtyp == VBLK || vtyp == VCHR) {
17776ca35587Sdholland 			NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
17786ca35587Sdholland 			*tl++ = vtonfsv34_type(vtyp);
17796ca35587Sdholland 			*tl++ = txdr_unsigned(NFSMAJOR(rdev));
17806ca35587Sdholland 			*tl = txdr_unsigned(NFSMINOR(rdev));
17816ca35587Sdholland 		} else {
17826ca35587Sdholland 			NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
17836ca35587Sdholland 			*tl = vtonfsv34_type(vtyp);
17846ca35587Sdholland 		}
17856ca35587Sdholland 	}
17866ca35587Sdholland 	(void) nfsm_strtom(nd, name, namelen);
17876ca35587Sdholland 	if (nd->nd_flag & ND_NFSV3) {
17886ca35587Sdholland 		NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
17896ca35587Sdholland 		*tl = vtonfsv34_type(vtyp);
17906ca35587Sdholland 	}
17916ca35587Sdholland 	if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4))
17926ca35587Sdholland 		nfscl_fillsattr(nd, vap, dvp, 0, 0);
17936ca35587Sdholland 	if ((nd->nd_flag & ND_NFSV3) &&
17946ca35587Sdholland 	    (vtyp == VCHR || vtyp == VBLK)) {
17956ca35587Sdholland 		NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
17966ca35587Sdholland 		*tl++ = txdr_unsigned(NFSMAJOR(rdev));
17976ca35587Sdholland 		*tl = txdr_unsigned(NFSMINOR(rdev));
17986ca35587Sdholland 	}
17996ca35587Sdholland 	if (nd->nd_flag & ND_NFSV4) {
18006ca35587Sdholland 		NFSGETATTR_ATTRBIT(&attrbits);
18016ca35587Sdholland 		NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
18026ca35587Sdholland 		*tl++ = txdr_unsigned(NFSV4OP_GETFH);
18036ca35587Sdholland 		*tl = txdr_unsigned(NFSV4OP_GETATTR);
18046ca35587Sdholland 		(void) nfsrv_putattrbit(nd, &attrbits);
18056ca35587Sdholland 	}
18066ca35587Sdholland 	if (nd->nd_flag & ND_NFSV2)
18076ca35587Sdholland 		nfscl_fillsattr(nd, vap, dvp, NFSSATTR_SIZERDEV, rdev);
18086ca35587Sdholland 	error = nfscl_request(nd, dvp, p, cred, dstuff);
18096ca35587Sdholland 	if (error)
18106ca35587Sdholland 		return (error);
18116ca35587Sdholland 	if (nd->nd_flag & ND_NFSV4)
18126ca35587Sdholland 		error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
18136ca35587Sdholland 	if (!nd->nd_repstat) {
18146ca35587Sdholland 		if (nd->nd_flag & ND_NFSV4) {
18156ca35587Sdholland 			NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
18166ca35587Sdholland 			error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
18176ca35587Sdholland 			if (error)
18186ca35587Sdholland 				goto nfsmout;
18196ca35587Sdholland 		}
18206ca35587Sdholland 		error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp);
18216ca35587Sdholland 		if (error)
18226ca35587Sdholland 			goto nfsmout;
18236ca35587Sdholland 	}
18246ca35587Sdholland 	if (nd->nd_flag & ND_NFSV3)
18256ca35587Sdholland 		error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
18266ca35587Sdholland 	if (!error && nd->nd_repstat)
18276ca35587Sdholland 		error = nd->nd_repstat;
18286ca35587Sdholland nfsmout:
18296ca35587Sdholland 	mbuf_freem(nd->nd_mrep);
18306ca35587Sdholland 	return (error);
18316ca35587Sdholland }
18326ca35587Sdholland 
18336ca35587Sdholland /*
18346ca35587Sdholland  * nfs file create call
18356ca35587Sdholland  * Mostly just call the approriate routine. (I separated out v4, so that
18366ca35587Sdholland  * error recovery wouldn't be as difficult.)
18376ca35587Sdholland  */
18386ca35587Sdholland APPLESTATIC int
nfsrpc_create(vnode_t dvp,char * name,int namelen,struct vattr * vap,nfsquad_t cverf,int fmode,struct ucred * cred,NFSPROC_T * p,struct nfsvattr * dnap,struct nfsvattr * nnap,struct nfsfh ** nfhpp,int * attrflagp,int * dattrflagp,void * dstuff)18396ca35587Sdholland nfsrpc_create(vnode_t dvp, char *name, int namelen, struct vattr *vap,
18406ca35587Sdholland     nfsquad_t cverf, int fmode, struct ucred *cred, NFSPROC_T *p,
18416ca35587Sdholland     struct nfsvattr *dnap, struct nfsvattr *nnap, struct nfsfh **nfhpp,
18426ca35587Sdholland     int *attrflagp, int *dattrflagp, void *dstuff)
18436ca35587Sdholland {
18446ca35587Sdholland 	int error = 0, newone, expireret = 0, retrycnt, unlocked;
18456ca35587Sdholland 	struct nfsclowner *owp;
18466ca35587Sdholland 	struct nfscldeleg *dp;
18476ca35587Sdholland 	struct nfsmount *nmp = VFSTONFS(vnode_mount(dvp));
18486ca35587Sdholland 	u_int32_t clidrev;
18496ca35587Sdholland 
18506ca35587Sdholland 	if (NFSHASNFSV4(nmp)) {
18516ca35587Sdholland 	    retrycnt = 0;
18526ca35587Sdholland 	    do {
18536ca35587Sdholland 		dp = NULL;
18546ca35587Sdholland 		error = nfscl_open(dvp, NULL, 0, (NFSV4OPEN_ACCESSWRITE |
18556ca35587Sdholland 		    NFSV4OPEN_ACCESSREAD), 0, cred, p, &owp, NULL, &newone,
18566ca35587Sdholland 		    NULL, 1);
18576ca35587Sdholland 		if (error)
18586ca35587Sdholland 			return (error);
18596ca35587Sdholland 		if (nmp->nm_clp != NULL)
18606ca35587Sdholland 			clidrev = nmp->nm_clp->nfsc_clientidrev;
18616ca35587Sdholland 		else
18626ca35587Sdholland 			clidrev = 0;
18636ca35587Sdholland 		error = nfsrpc_createv4(dvp, name, namelen, vap, cverf, fmode,
18646ca35587Sdholland 		  owp, &dp, cred, p, dnap, nnap, nfhpp, attrflagp, dattrflagp,
18656ca35587Sdholland 		  dstuff, &unlocked);
18666ca35587Sdholland 		/*
18676ca35587Sdholland 		 * There is no need to invalidate cached attributes here,
18686ca35587Sdholland 		 * since new post-delegation issue attributes are always
18696ca35587Sdholland 		 * returned by nfsrpc_createv4() and these will update the
18706ca35587Sdholland 		 * attribute cache.
18716ca35587Sdholland 		 */
18726ca35587Sdholland 		if (dp != NULL)
18736ca35587Sdholland 			(void) nfscl_deleg(nmp->nm_mountp, owp->nfsow_clp,
18746ca35587Sdholland 			    (*nfhpp)->nfh_fh, (*nfhpp)->nfh_len, cred, p, &dp);
18756ca35587Sdholland 		nfscl_ownerrelease(owp, error, newone, unlocked);
18766ca35587Sdholland 		if (error == NFSERR_GRACE || error == NFSERR_STALECLIENTID ||
18776ca35587Sdholland 		    error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
18786ca35587Sdholland 		    error == NFSERR_BADSESSION) {
18796ca35587Sdholland 			(void) nfs_catnap(PZERO, error, "nfs_open");
18806ca35587Sdholland 		} else if ((error == NFSERR_EXPIRED ||
18816ca35587Sdholland 		    error == NFSERR_BADSTATEID) && clidrev != 0) {
18826ca35587Sdholland 			expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
18836ca35587Sdholland 			retrycnt++;
18846ca35587Sdholland 		}
18856ca35587Sdholland 	    } while (error == NFSERR_GRACE || error == NFSERR_STALECLIENTID ||
18866ca35587Sdholland 		error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
18876ca35587Sdholland 		error == NFSERR_BADSESSION ||
18886ca35587Sdholland 		((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
18896ca35587Sdholland 		 expireret == 0 && clidrev != 0 && retrycnt < 4));
18906ca35587Sdholland 	    if (error && retrycnt >= 4)
18916ca35587Sdholland 		    error = EIO;
18926ca35587Sdholland 	} else {
18936ca35587Sdholland 		error = nfsrpc_createv23(dvp, name, namelen, vap, cverf,
18946ca35587Sdholland 		    fmode, cred, p, dnap, nnap, nfhpp, attrflagp, dattrflagp,
18956ca35587Sdholland 		    dstuff);
18966ca35587Sdholland 	}
18976ca35587Sdholland 	return (error);
18986ca35587Sdholland }
18996ca35587Sdholland 
19006ca35587Sdholland /*
19016ca35587Sdholland  * The create rpc for v2 and 3.
19026ca35587Sdholland  */
19036ca35587Sdholland static int
nfsrpc_createv23(vnode_t dvp,char * name,int namelen,struct vattr * vap,nfsquad_t cverf,int fmode,struct ucred * cred,NFSPROC_T * p,struct nfsvattr * dnap,struct nfsvattr * nnap,struct nfsfh ** nfhpp,int * attrflagp,int * dattrflagp,void * dstuff)19046ca35587Sdholland nfsrpc_createv23(vnode_t dvp, char *name, int namelen, struct vattr *vap,
19056ca35587Sdholland     nfsquad_t cverf, int fmode, struct ucred *cred, NFSPROC_T *p,
19066ca35587Sdholland     struct nfsvattr *dnap, struct nfsvattr *nnap, struct nfsfh **nfhpp,
19076ca35587Sdholland     int *attrflagp, int *dattrflagp, void *dstuff)
19086ca35587Sdholland {
19096ca35587Sdholland 	u_int32_t *tl;
19106ca35587Sdholland 	int error = 0;
19116ca35587Sdholland 	struct nfsrv_descript nfsd, *nd = &nfsd;
19126ca35587Sdholland 
19136ca35587Sdholland 	*nfhpp = NULL;
19146ca35587Sdholland 	*attrflagp = 0;
19156ca35587Sdholland 	*dattrflagp = 0;
19166ca35587Sdholland 	if (namelen > NFS_MAXNAMLEN)
19176ca35587Sdholland 		return (ENAMETOOLONG);
19186ca35587Sdholland 	NFSCL_REQSTART(nd, NFSPROC_CREATE, dvp);
19196ca35587Sdholland 	(void) nfsm_strtom(nd, name, namelen);
19206ca35587Sdholland 	if (nd->nd_flag & ND_NFSV3) {
19216ca35587Sdholland 		NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
19226ca35587Sdholland 		if (fmode & O_EXCL) {
19236ca35587Sdholland 			*tl = txdr_unsigned(NFSCREATE_EXCLUSIVE);
19246ca35587Sdholland 			NFSM_BUILD(tl, u_int32_t *, NFSX_VERF);
19256ca35587Sdholland 			*tl++ = cverf.lval[0];
19266ca35587Sdholland 			*tl = cverf.lval[1];
19276ca35587Sdholland 		} else {
19286ca35587Sdholland 			*tl = txdr_unsigned(NFSCREATE_UNCHECKED);
19296ca35587Sdholland 			nfscl_fillsattr(nd, vap, dvp, 0, 0);
19306ca35587Sdholland 		}
19316ca35587Sdholland 	} else {
19326ca35587Sdholland 		nfscl_fillsattr(nd, vap, dvp, NFSSATTR_SIZE0, 0);
19336ca35587Sdholland 	}
19346ca35587Sdholland 	error = nfscl_request(nd, dvp, p, cred, dstuff);
19356ca35587Sdholland 	if (error)
19366ca35587Sdholland 		return (error);
19376ca35587Sdholland 	if (nd->nd_repstat == 0) {
19386ca35587Sdholland 		error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp);
19396ca35587Sdholland 		if (error)
19406ca35587Sdholland 			goto nfsmout;
19416ca35587Sdholland 	}
19426ca35587Sdholland 	if (nd->nd_flag & ND_NFSV3)
19436ca35587Sdholland 		error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
19446ca35587Sdholland 	if (nd->nd_repstat != 0 && error == 0)
19456ca35587Sdholland 		error = nd->nd_repstat;
19466ca35587Sdholland nfsmout:
19476ca35587Sdholland 	mbuf_freem(nd->nd_mrep);
19486ca35587Sdholland 	return (error);
19496ca35587Sdholland }
19506ca35587Sdholland 
19516ca35587Sdholland static int
nfsrpc_createv4(vnode_t dvp,char * name,int namelen,struct vattr * vap,nfsquad_t cverf,int fmode,struct nfsclowner * owp,struct nfscldeleg ** dpp,struct ucred * cred,NFSPROC_T * p,struct nfsvattr * dnap,struct nfsvattr * nnap,struct nfsfh ** nfhpp,int * attrflagp,int * dattrflagp,void * dstuff,int * unlockedp)19526ca35587Sdholland nfsrpc_createv4(vnode_t dvp, char *name, int namelen, struct vattr *vap,
19536ca35587Sdholland     nfsquad_t cverf, int fmode, struct nfsclowner *owp, struct nfscldeleg **dpp,
19546ca35587Sdholland     struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap,
19556ca35587Sdholland     struct nfsvattr *nnap, struct nfsfh **nfhpp, int *attrflagp,
19566ca35587Sdholland     int *dattrflagp, void *dstuff, int *unlockedp)
19576ca35587Sdholland {
19586ca35587Sdholland 	u_int32_t *tl;
19596ca35587Sdholland 	int error = 0, deleg, newone, ret, acesize, limitby;
19606ca35587Sdholland 	struct nfsrv_descript nfsd, *nd = &nfsd;
19616ca35587Sdholland 	struct nfsclopen *op;
19626ca35587Sdholland 	struct nfscldeleg *dp = NULL;
19636ca35587Sdholland 	struct nfsnode *np;
19646ca35587Sdholland 	struct nfsfh *nfhp;
19656ca35587Sdholland 	nfsattrbit_t attrbits;
19666ca35587Sdholland 	nfsv4stateid_t stateid;
19676ca35587Sdholland 	u_int32_t rflags;
19686ca35587Sdholland 	struct nfsmount *nmp;
19696ca35587Sdholland 
19706ca35587Sdholland 	nmp = VFSTONFS(dvp->v_mount);
1971e81f0ea2Spgoyette 	np = VTONFS(dvp);
19726ca35587Sdholland 	*unlockedp = 0;
19736ca35587Sdholland 	*nfhpp = NULL;
19746ca35587Sdholland 	*dpp = NULL;
19756ca35587Sdholland 	*attrflagp = 0;
19766ca35587Sdholland 	*dattrflagp = 0;
19776ca35587Sdholland 	if (namelen > NFS_MAXNAMLEN)
19786ca35587Sdholland 		return (ENAMETOOLONG);
19796ca35587Sdholland 	NFSCL_REQSTART(nd, NFSPROC_CREATE, dvp);
19806ca35587Sdholland 	/*
19816ca35587Sdholland 	 * For V4, this is actually an Open op.
19826ca35587Sdholland 	 */
19836ca35587Sdholland 	NFSM_BUILD(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
19846ca35587Sdholland 	*tl++ = txdr_unsigned(owp->nfsow_seqid);
19856ca35587Sdholland 	*tl++ = txdr_unsigned(NFSV4OPEN_ACCESSWRITE |
19866ca35587Sdholland 	    NFSV4OPEN_ACCESSREAD);
19876ca35587Sdholland 	*tl++ = txdr_unsigned(NFSV4OPEN_DENYNONE);
19886ca35587Sdholland 	*tl++ = NFSMNT_MDSSESSION(nmp)->nfsess_clientid.lval[0];
19896ca35587Sdholland 	*tl = NFSMNT_MDSSESSION(nmp)->nfsess_clientid.lval[1];
19906ca35587Sdholland 	(void) nfsm_strtom(nd, owp->nfsow_owner, NFSV4CL_LOCKNAMELEN);
19916ca35587Sdholland 	NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
19926ca35587Sdholland 	*tl++ = txdr_unsigned(NFSV4OPEN_CREATE);
19936ca35587Sdholland 	if (fmode & O_EXCL) {
19946ca35587Sdholland 		if (NFSHASNFSV4N(nmp)) {
19956ca35587Sdholland 			if (NFSHASSESSPERSIST(nmp)) {
19966ca35587Sdholland 				/* Use GUARDED for persistent sessions. */
19976ca35587Sdholland 				*tl = txdr_unsigned(NFSCREATE_GUARDED);
19986ca35587Sdholland 				nfscl_fillsattr(nd, vap, dvp, 0, 0);
19996ca35587Sdholland 			} else {
20006ca35587Sdholland 				/* Otherwise, use EXCLUSIVE4_1. */
20016ca35587Sdholland 				*tl = txdr_unsigned(NFSCREATE_EXCLUSIVE41);
20026ca35587Sdholland 				NFSM_BUILD(tl, u_int32_t *, NFSX_VERF);
20036ca35587Sdholland 				*tl++ = cverf.lval[0];
20046ca35587Sdholland 				*tl = cverf.lval[1];
20056ca35587Sdholland 				nfscl_fillsattr(nd, vap, dvp, 0, 0);
20066ca35587Sdholland 			}
20076ca35587Sdholland 		} else {
20086ca35587Sdholland 			/* NFSv4.0 */
20096ca35587Sdholland 			*tl = txdr_unsigned(NFSCREATE_EXCLUSIVE);
20106ca35587Sdholland 			NFSM_BUILD(tl, u_int32_t *, NFSX_VERF);
20116ca35587Sdholland 			*tl++ = cverf.lval[0];
20126ca35587Sdholland 			*tl = cverf.lval[1];
20136ca35587Sdholland 		}
20146ca35587Sdholland 	} else {
20156ca35587Sdholland 		*tl = txdr_unsigned(NFSCREATE_UNCHECKED);
20166ca35587Sdholland 		nfscl_fillsattr(nd, vap, dvp, 0, 0);
20176ca35587Sdholland 	}
20186ca35587Sdholland 	NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
20196ca35587Sdholland 	*tl = txdr_unsigned(NFSV4OPEN_CLAIMNULL);
20206ca35587Sdholland 	(void) nfsm_strtom(nd, name, namelen);
2021e81f0ea2Spgoyette 	/* Get the new file's handle and attributes. */
20226ca35587Sdholland 	NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
20236ca35587Sdholland 	*tl++ = txdr_unsigned(NFSV4OP_GETFH);
20246ca35587Sdholland 	*tl = txdr_unsigned(NFSV4OP_GETATTR);
20256ca35587Sdholland 	NFSGETATTR_ATTRBIT(&attrbits);
20266ca35587Sdholland 	(void) nfsrv_putattrbit(nd, &attrbits);
2027e81f0ea2Spgoyette 	/* Get the directory's post-op attributes. */
2028e81f0ea2Spgoyette 	NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2029e81f0ea2Spgoyette 	*tl = txdr_unsigned(NFSV4OP_PUTFH);
2030e81f0ea2Spgoyette 	(void) nfsm_fhtom(nd, np->n_fhp->nfh_fh, np->n_fhp->nfh_len, 0);
2031e81f0ea2Spgoyette 	NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2032e81f0ea2Spgoyette 	*tl = txdr_unsigned(NFSV4OP_GETATTR);
2033e81f0ea2Spgoyette 	(void) nfsrv_putattrbit(nd, &attrbits);
20346ca35587Sdholland 	error = nfscl_request(nd, dvp, p, cred, dstuff);
20356ca35587Sdholland 	if (error)
20366ca35587Sdholland 		return (error);
20376ca35587Sdholland 	NFSCL_INCRSEQID(owp->nfsow_seqid, nd);
20386ca35587Sdholland 	if (nd->nd_repstat == 0) {
20396ca35587Sdholland 		NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
20406ca35587Sdholland 		    6 * NFSX_UNSIGNED);
20416ca35587Sdholland 		stateid.seqid = *tl++;
20426ca35587Sdholland 		stateid.other[0] = *tl++;
20436ca35587Sdholland 		stateid.other[1] = *tl++;
20446ca35587Sdholland 		stateid.other[2] = *tl;
20456ca35587Sdholland 		rflags = fxdr_unsigned(u_int32_t, *(tl + 6));
20466ca35587Sdholland 		(void) nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
20476ca35587Sdholland 		NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
20486ca35587Sdholland 		deleg = fxdr_unsigned(int, *tl);
20496ca35587Sdholland 		if (deleg == NFSV4OPEN_DELEGATEREAD ||
20506ca35587Sdholland 		    deleg == NFSV4OPEN_DELEGATEWRITE) {
20516ca35587Sdholland 			if (!(owp->nfsow_clp->nfsc_flags &
20526ca35587Sdholland 			      NFSCLFLAGS_FIRSTDELEG))
20536ca35587Sdholland 				owp->nfsow_clp->nfsc_flags |=
20546ca35587Sdholland 				  (NFSCLFLAGS_FIRSTDELEG | NFSCLFLAGS_GOTDELEG);
20556ca35587Sdholland 			MALLOC(dp, struct nfscldeleg *,
20566ca35587Sdholland 			    sizeof (struct nfscldeleg) + NFSX_V4FHMAX,
20576ca35587Sdholland 			    M_NFSCLDELEG, M_WAITOK);
20586ca35587Sdholland 			LIST_INIT(&dp->nfsdl_owner);
20596ca35587Sdholland 			LIST_INIT(&dp->nfsdl_lock);
20606ca35587Sdholland 			dp->nfsdl_clp = owp->nfsow_clp;
20616ca35587Sdholland 			newnfs_copyincred(cred, &dp->nfsdl_cred);
20626ca35587Sdholland 			nfscl_lockinit(&dp->nfsdl_rwlock);
20636ca35587Sdholland 			NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
20646ca35587Sdholland 			    NFSX_UNSIGNED);
20656ca35587Sdholland 			dp->nfsdl_stateid.seqid = *tl++;
20666ca35587Sdholland 			dp->nfsdl_stateid.other[0] = *tl++;
20676ca35587Sdholland 			dp->nfsdl_stateid.other[1] = *tl++;
20686ca35587Sdholland 			dp->nfsdl_stateid.other[2] = *tl++;
20696ca35587Sdholland 			ret = fxdr_unsigned(int, *tl);
20706ca35587Sdholland 			if (deleg == NFSV4OPEN_DELEGATEWRITE) {
20716ca35587Sdholland 				dp->nfsdl_flags = NFSCLDL_WRITE;
20726ca35587Sdholland 				/*
20736ca35587Sdholland 				 * Indicates how much the file can grow.
20746ca35587Sdholland 				 */
20756ca35587Sdholland 				NFSM_DISSECT(tl, u_int32_t *,
20766ca35587Sdholland 				    3 * NFSX_UNSIGNED);
20776ca35587Sdholland 				limitby = fxdr_unsigned(int, *tl++);
20786ca35587Sdholland 				switch (limitby) {
20796ca35587Sdholland 				case NFSV4OPEN_LIMITSIZE:
20806ca35587Sdholland 					dp->nfsdl_sizelimit = fxdr_hyper(tl);
20816ca35587Sdholland 					break;
20826ca35587Sdholland 				case NFSV4OPEN_LIMITBLOCKS:
20836ca35587Sdholland 					dp->nfsdl_sizelimit =
20846ca35587Sdholland 					    fxdr_unsigned(u_int64_t, *tl++);
20856ca35587Sdholland 					dp->nfsdl_sizelimit *=
20866ca35587Sdholland 					    fxdr_unsigned(u_int64_t, *tl);
20876ca35587Sdholland 					break;
20886ca35587Sdholland 				default:
20896ca35587Sdholland 					error = NFSERR_BADXDR;
20906ca35587Sdholland 					goto nfsmout;
2091e81f0ea2Spgoyette 				}
20926ca35587Sdholland 			} else {
20936ca35587Sdholland 				dp->nfsdl_flags = NFSCLDL_READ;
20946ca35587Sdholland 			}
20956ca35587Sdholland 			if (ret)
20966ca35587Sdholland 				dp->nfsdl_flags |= NFSCLDL_RECALL;
20976ca35587Sdholland 			error = nfsrv_dissectace(nd, &dp->nfsdl_ace, &ret,
20986ca35587Sdholland 			    &acesize, p);
20996ca35587Sdholland 			if (error)
21006ca35587Sdholland 				goto nfsmout;
21016ca35587Sdholland 		} else if (deleg != NFSV4OPEN_DELEGATENONE) {
21026ca35587Sdholland 			error = NFSERR_BADXDR;
21036ca35587Sdholland 			goto nfsmout;
21046ca35587Sdholland 		}
21056ca35587Sdholland 		error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp);
21066ca35587Sdholland 		if (error)
21076ca35587Sdholland 			goto nfsmout;
2108e81f0ea2Spgoyette 		/* Get rid of the PutFH and Getattr status values. */
2109e81f0ea2Spgoyette 		NFSM_DISSECT(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
2110e81f0ea2Spgoyette 		/* Load the directory attributes. */
2111e81f0ea2Spgoyette 		error = nfsm_loadattr(nd, dnap);
2112e81f0ea2Spgoyette 		if (error)
2113e81f0ea2Spgoyette 			goto nfsmout;
2114e81f0ea2Spgoyette 		*dattrflagp = 1;
21156ca35587Sdholland 		if (dp != NULL && *attrflagp) {
21166ca35587Sdholland 			dp->nfsdl_change = nnap->na_filerev;
21176ca35587Sdholland 			dp->nfsdl_modtime = nnap->na_mtime;
21186ca35587Sdholland 			dp->nfsdl_flags |= NFSCLDL_MODTIMESET;
21196ca35587Sdholland 		}
21206ca35587Sdholland 		/*
21216ca35587Sdholland 		 * We can now complete the Open state.
21226ca35587Sdholland 		 */
21236ca35587Sdholland 		nfhp = *nfhpp;
21246ca35587Sdholland 		if (dp != NULL) {
21256ca35587Sdholland 			dp->nfsdl_fhlen = nfhp->nfh_len;
21266ca35587Sdholland 			NFSBCOPY(nfhp->nfh_fh, dp->nfsdl_fh, nfhp->nfh_len);
21276ca35587Sdholland 		}
21286ca35587Sdholland 		/*
21296ca35587Sdholland 		 * Get an Open structure that will be
21306ca35587Sdholland 		 * attached to the OpenOwner, acquired already.
21316ca35587Sdholland 		 */
21326ca35587Sdholland 		error = nfscl_open(dvp, nfhp->nfh_fh, nfhp->nfh_len,
21336ca35587Sdholland 		    (NFSV4OPEN_ACCESSWRITE | NFSV4OPEN_ACCESSREAD), 0,
21346ca35587Sdholland 		    cred, p, NULL, &op, &newone, NULL, 0);
21356ca35587Sdholland 		if (error)
21366ca35587Sdholland 			goto nfsmout;
21376ca35587Sdholland 		op->nfso_stateid = stateid;
21386ca35587Sdholland 		newnfs_copyincred(cred, &op->nfso_cred);
21396ca35587Sdholland 		if ((rflags & NFSV4OPEN_RESULTCONFIRM)) {
21406ca35587Sdholland 		    do {
21416ca35587Sdholland 			ret = nfsrpc_openconfirm(dvp, nfhp->nfh_fh,
21426ca35587Sdholland 			    nfhp->nfh_len, op, cred, p);
21436ca35587Sdholland 			if (ret == NFSERR_DELAY)
21446ca35587Sdholland 			    (void) nfs_catnap(PZERO, ret, "nfs_create");
21456ca35587Sdholland 		    } while (ret == NFSERR_DELAY);
21466ca35587Sdholland 		    error = ret;
21476ca35587Sdholland 		}
21486ca35587Sdholland 
21496ca35587Sdholland 		/*
21506ca35587Sdholland 		 * If the server is handing out delegations, but we didn't
21516ca35587Sdholland 		 * get one because an OpenConfirm was required, try the
21526ca35587Sdholland 		 * Open again, to get a delegation. This is a harmless no-op,
21536ca35587Sdholland 		 * from a server's point of view.
21546ca35587Sdholland 		 */
21556ca35587Sdholland 		if ((rflags & NFSV4OPEN_RESULTCONFIRM) &&
21566ca35587Sdholland 		    (owp->nfsow_clp->nfsc_flags & NFSCLFLAGS_GOTDELEG) &&
21576ca35587Sdholland 		    !error && dp == NULL) {
21586ca35587Sdholland 		    do {
21596ca35587Sdholland 			ret = nfsrpc_openrpc(VFSTONFS(vnode_mount(dvp)), dvp,
21606ca35587Sdholland 			    np->n_fhp->nfh_fh, np->n_fhp->nfh_len,
21616ca35587Sdholland 			    nfhp->nfh_fh, nfhp->nfh_len,
21626ca35587Sdholland 			    (NFSV4OPEN_ACCESSWRITE | NFSV4OPEN_ACCESSREAD), op,
21636ca35587Sdholland 			    name, namelen, &dp, 0, 0x0, cred, p, 0, 1);
21646ca35587Sdholland 			if (ret == NFSERR_DELAY)
21656ca35587Sdholland 			    (void) nfs_catnap(PZERO, ret, "nfs_crt2");
21666ca35587Sdholland 		    } while (ret == NFSERR_DELAY);
21676ca35587Sdholland 		    if (ret) {
2168e81f0ea2Spgoyette 			if (dp != NULL) {
21696ca35587Sdholland 				FREE((caddr_t)dp, M_NFSCLDELEG);
2170e81f0ea2Spgoyette 				dp = NULL;
2171e81f0ea2Spgoyette 			}
21726ca35587Sdholland 			if (ret == NFSERR_STALECLIENTID ||
21736ca35587Sdholland 			    ret == NFSERR_STALEDONTRECOVER ||
21746ca35587Sdholland 			    ret == NFSERR_BADSESSION)
21756ca35587Sdholland 				error = ret;
21766ca35587Sdholland 		    }
21776ca35587Sdholland 		}
21786ca35587Sdholland 		nfscl_openrelease(op, error, newone);
21796ca35587Sdholland 		*unlockedp = 1;
21806ca35587Sdholland 	}
21816ca35587Sdholland 	if (nd->nd_repstat != 0 && error == 0)
21826ca35587Sdholland 		error = nd->nd_repstat;
21836ca35587Sdholland 	if (error == NFSERR_STALECLIENTID || error == NFSERR_BADSESSION)
21846ca35587Sdholland 		nfscl_initiate_recovery(owp->nfsow_clp);
21856ca35587Sdholland nfsmout:
21866ca35587Sdholland 	if (!error)
21876ca35587Sdholland 		*dpp = dp;
21886ca35587Sdholland 	else if (dp != NULL)
21896ca35587Sdholland 		FREE((caddr_t)dp, M_NFSCLDELEG);
21906ca35587Sdholland 	mbuf_freem(nd->nd_mrep);
21916ca35587Sdholland 	return (error);
21926ca35587Sdholland }
21936ca35587Sdholland 
21946ca35587Sdholland /*
21956ca35587Sdholland  * Nfs remove rpc
21966ca35587Sdholland  */
21976ca35587Sdholland APPLESTATIC int
nfsrpc_remove(vnode_t dvp,char * name,int namelen,vnode_t vp,struct ucred * cred,NFSPROC_T * p,struct nfsvattr * dnap,int * dattrflagp,void * dstuff)21986ca35587Sdholland nfsrpc_remove(vnode_t dvp, char *name, int namelen, vnode_t vp,
21996ca35587Sdholland     struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap, int *dattrflagp,
22006ca35587Sdholland     void *dstuff)
22016ca35587Sdholland {
22026ca35587Sdholland 	u_int32_t *tl;
22036ca35587Sdholland 	struct nfsrv_descript nfsd, *nd = &nfsd;
22046ca35587Sdholland 	struct nfsnode *np;
22056ca35587Sdholland 	struct nfsmount *nmp;
22066ca35587Sdholland 	nfsv4stateid_t dstateid;
22076ca35587Sdholland 	int error, ret = 0, i;
22086ca35587Sdholland 
22096ca35587Sdholland 	*dattrflagp = 0;
22106ca35587Sdholland 	if (namelen > NFS_MAXNAMLEN)
22116ca35587Sdholland 		return (ENAMETOOLONG);
22126ca35587Sdholland 	nmp = VFSTONFS(vnode_mount(dvp));
22136ca35587Sdholland tryagain:
22146ca35587Sdholland 	if (NFSHASNFSV4(nmp) && ret == 0) {
22156ca35587Sdholland 		ret = nfscl_removedeleg(vp, p, &dstateid);
22166ca35587Sdholland 		if (ret == 1) {
22176ca35587Sdholland 			NFSCL_REQSTART(nd, NFSPROC_RETDELEGREMOVE, vp);
22186ca35587Sdholland 			NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID +
22196ca35587Sdholland 			    NFSX_UNSIGNED);
22206ca35587Sdholland 			if (NFSHASNFSV4N(nmp))
22216ca35587Sdholland 				*tl++ = 0;
22226ca35587Sdholland 			else
22236ca35587Sdholland 				*tl++ = dstateid.seqid;
22246ca35587Sdholland 			*tl++ = dstateid.other[0];
22256ca35587Sdholland 			*tl++ = dstateid.other[1];
22266ca35587Sdholland 			*tl++ = dstateid.other[2];
22276ca35587Sdholland 			*tl = txdr_unsigned(NFSV4OP_PUTFH);
22286ca35587Sdholland 			np = VTONFS(dvp);
22296ca35587Sdholland 			(void) nfsm_fhtom(nd, np->n_fhp->nfh_fh,
22306ca35587Sdholland 			    np->n_fhp->nfh_len, 0);
22316ca35587Sdholland 			NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
22326ca35587Sdholland 			*tl = txdr_unsigned(NFSV4OP_REMOVE);
22336ca35587Sdholland 		}
22346ca35587Sdholland 	} else {
22356ca35587Sdholland 		ret = 0;
22366ca35587Sdholland 	}
22376ca35587Sdholland 	if (ret == 0)
22386ca35587Sdholland 		NFSCL_REQSTART(nd, NFSPROC_REMOVE, dvp);
22396ca35587Sdholland 	(void) nfsm_strtom(nd, name, namelen);
22406ca35587Sdholland 	error = nfscl_request(nd, dvp, p, cred, dstuff);
22416ca35587Sdholland 	if (error)
22426ca35587Sdholland 		return (error);
22436ca35587Sdholland 	if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) {
22446ca35587Sdholland 		/* For NFSv4, parse out any Delereturn replies. */
22456ca35587Sdholland 		if (ret > 0 && nd->nd_repstat != 0 &&
22466ca35587Sdholland 		    (nd->nd_flag & ND_NOMOREDATA)) {
22476ca35587Sdholland 			/*
22486ca35587Sdholland 			 * If the Delegreturn failed, try again without
22496ca35587Sdholland 			 * it. The server will Recall, as required.
22506ca35587Sdholland 			 */
22516ca35587Sdholland 			mbuf_freem(nd->nd_mrep);
22526ca35587Sdholland 			goto tryagain;
22536ca35587Sdholland 		}
22546ca35587Sdholland 		for (i = 0; i < (ret * 2); i++) {
22556ca35587Sdholland 			if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) ==
22566ca35587Sdholland 			    ND_NFSV4) {
22576ca35587Sdholland 			    NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
22586ca35587Sdholland 			    if (*(tl + 1))
22596ca35587Sdholland 				nd->nd_flag |= ND_NOMOREDATA;
22606ca35587Sdholland 			}
22616ca35587Sdholland 		}
22626ca35587Sdholland 		error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
22636ca35587Sdholland 	}
22646ca35587Sdholland 	if (nd->nd_repstat && !error)
22656ca35587Sdholland 		error = nd->nd_repstat;
22666ca35587Sdholland nfsmout:
22676ca35587Sdholland 	mbuf_freem(nd->nd_mrep);
22686ca35587Sdholland 	return (error);
22696ca35587Sdholland }
22706ca35587Sdholland 
22716ca35587Sdholland /*
22726ca35587Sdholland  * Do an nfs rename rpc.
22736ca35587Sdholland  */
22746ca35587Sdholland APPLESTATIC int
nfsrpc_rename(vnode_t fdvp,vnode_t fvp,char * fnameptr,int fnamelen,vnode_t tdvp,vnode_t tvp,char * tnameptr,int tnamelen,struct ucred * cred,NFSPROC_T * p,struct nfsvattr * fnap,struct nfsvattr * tnap,int * fattrflagp,int * tattrflagp,void * fstuff,void * tstuff)22756ca35587Sdholland nfsrpc_rename(vnode_t fdvp, vnode_t fvp, char *fnameptr, int fnamelen,
22766ca35587Sdholland     vnode_t tdvp, vnode_t tvp, char *tnameptr, int tnamelen, struct ucred *cred,
22776ca35587Sdholland     NFSPROC_T *p, struct nfsvattr *fnap, struct nfsvattr *tnap,
22786ca35587Sdholland     int *fattrflagp, int *tattrflagp, void *fstuff, void *tstuff)
22796ca35587Sdholland {
22806ca35587Sdholland 	u_int32_t *tl;
22816ca35587Sdholland 	struct nfsrv_descript nfsd, *nd = &nfsd;
22826ca35587Sdholland 	struct nfsmount *nmp;
22836ca35587Sdholland 	struct nfsnode *np;
22846ca35587Sdholland 	nfsattrbit_t attrbits;
22856ca35587Sdholland 	nfsv4stateid_t fdstateid, tdstateid;
22866ca35587Sdholland 	int error = 0, ret = 0, gottd = 0, gotfd = 0, i;
22876ca35587Sdholland 
22886ca35587Sdholland 	*fattrflagp = 0;
22896ca35587Sdholland 	*tattrflagp = 0;
22906ca35587Sdholland 	nmp = VFSTONFS(vnode_mount(fdvp));
22916ca35587Sdholland 	if (fnamelen > NFS_MAXNAMLEN || tnamelen > NFS_MAXNAMLEN)
22926ca35587Sdholland 		return (ENAMETOOLONG);
22936ca35587Sdholland tryagain:
22946ca35587Sdholland 	if (NFSHASNFSV4(nmp) && ret == 0) {
22956ca35587Sdholland 		ret = nfscl_renamedeleg(fvp, &fdstateid, &gotfd, tvp,
22966ca35587Sdholland 		    &tdstateid, &gottd, p);
22976ca35587Sdholland 		if (gotfd && gottd) {
22986ca35587Sdholland 			NFSCL_REQSTART(nd, NFSPROC_RETDELEGRENAME2, fvp);
22996ca35587Sdholland 		} else if (gotfd) {
23006ca35587Sdholland 			NFSCL_REQSTART(nd, NFSPROC_RETDELEGRENAME1, fvp);
23016ca35587Sdholland 		} else if (gottd) {
23026ca35587Sdholland 			NFSCL_REQSTART(nd, NFSPROC_RETDELEGRENAME1, tvp);
23036ca35587Sdholland 		}
23046ca35587Sdholland 		if (gotfd) {
23056ca35587Sdholland 			NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID);
23066ca35587Sdholland 			if (NFSHASNFSV4N(nmp))
23076ca35587Sdholland 				*tl++ = 0;
23086ca35587Sdholland 			else
23096ca35587Sdholland 				*tl++ = fdstateid.seqid;
23106ca35587Sdholland 			*tl++ = fdstateid.other[0];
23116ca35587Sdholland 			*tl++ = fdstateid.other[1];
23126ca35587Sdholland 			*tl = fdstateid.other[2];
23136ca35587Sdholland 			if (gottd) {
23146ca35587Sdholland 				NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
23156ca35587Sdholland 				*tl = txdr_unsigned(NFSV4OP_PUTFH);
23166ca35587Sdholland 				np = VTONFS(tvp);
23176ca35587Sdholland 				(void) nfsm_fhtom(nd, np->n_fhp->nfh_fh,
23186ca35587Sdholland 				    np->n_fhp->nfh_len, 0);
23196ca35587Sdholland 				NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
23206ca35587Sdholland 				*tl = txdr_unsigned(NFSV4OP_DELEGRETURN);
23216ca35587Sdholland 			}
23226ca35587Sdholland 		}
23236ca35587Sdholland 		if (gottd) {
23246ca35587Sdholland 			NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID);
23256ca35587Sdholland 			if (NFSHASNFSV4N(nmp))
23266ca35587Sdholland 				*tl++ = 0;
23276ca35587Sdholland 			else
23286ca35587Sdholland 				*tl++ = tdstateid.seqid;
23296ca35587Sdholland 			*tl++ = tdstateid.other[0];
23306ca35587Sdholland 			*tl++ = tdstateid.other[1];
23316ca35587Sdholland 			*tl = tdstateid.other[2];
23326ca35587Sdholland 		}
23336ca35587Sdholland 		if (ret > 0) {
23346ca35587Sdholland 			NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
23356ca35587Sdholland 			*tl = txdr_unsigned(NFSV4OP_PUTFH);
23366ca35587Sdholland 			np = VTONFS(fdvp);
23376ca35587Sdholland 			(void) nfsm_fhtom(nd, np->n_fhp->nfh_fh,
23386ca35587Sdholland 			    np->n_fhp->nfh_len, 0);
23396ca35587Sdholland 			NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
23406ca35587Sdholland 			*tl = txdr_unsigned(NFSV4OP_SAVEFH);
23416ca35587Sdholland 		}
23426ca35587Sdholland 	} else {
23436ca35587Sdholland 		ret = 0;
23446ca35587Sdholland 	}
23456ca35587Sdholland 	if (ret == 0)
23466ca35587Sdholland 		NFSCL_REQSTART(nd, NFSPROC_RENAME, fdvp);
23476ca35587Sdholland 	if (nd->nd_flag & ND_NFSV4) {
23486ca35587Sdholland 		NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
23496ca35587Sdholland 		*tl = txdr_unsigned(NFSV4OP_GETATTR);
23506ca35587Sdholland 		NFSWCCATTR_ATTRBIT(&attrbits);
23516ca35587Sdholland 		(void) nfsrv_putattrbit(nd, &attrbits);
23526ca35587Sdholland 		NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
23536ca35587Sdholland 		*tl = txdr_unsigned(NFSV4OP_PUTFH);
23546ca35587Sdholland 		(void) nfsm_fhtom(nd, VTONFS(tdvp)->n_fhp->nfh_fh,
23556ca35587Sdholland 		    VTONFS(tdvp)->n_fhp->nfh_len, 0);
23566ca35587Sdholland 		NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
23576ca35587Sdholland 		*tl = txdr_unsigned(NFSV4OP_GETATTR);
23586ca35587Sdholland 		(void) nfsrv_putattrbit(nd, &attrbits);
23596ca35587Sdholland 		nd->nd_flag |= ND_V4WCCATTR;
23606ca35587Sdholland 		NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
23616ca35587Sdholland 		*tl = txdr_unsigned(NFSV4OP_RENAME);
23626ca35587Sdholland 	}
23636ca35587Sdholland 	(void) nfsm_strtom(nd, fnameptr, fnamelen);
23646ca35587Sdholland 	if (!(nd->nd_flag & ND_NFSV4))
23656ca35587Sdholland 		(void) nfsm_fhtom(nd, VTONFS(tdvp)->n_fhp->nfh_fh,
23666ca35587Sdholland 			VTONFS(tdvp)->n_fhp->nfh_len, 0);
23676ca35587Sdholland 	(void) nfsm_strtom(nd, tnameptr, tnamelen);
23686ca35587Sdholland 	error = nfscl_request(nd, fdvp, p, cred, fstuff);
23696ca35587Sdholland 	if (error)
23706ca35587Sdholland 		return (error);
23716ca35587Sdholland 	if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) {
23726ca35587Sdholland 		/* For NFSv4, parse out any Delereturn replies. */
23736ca35587Sdholland 		if (ret > 0 && nd->nd_repstat != 0 &&
23746ca35587Sdholland 		    (nd->nd_flag & ND_NOMOREDATA)) {
23756ca35587Sdholland 			/*
23766ca35587Sdholland 			 * If the Delegreturn failed, try again without
23776ca35587Sdholland 			 * it. The server will Recall, as required.
23786ca35587Sdholland 			 */
23796ca35587Sdholland 			mbuf_freem(nd->nd_mrep);
23806ca35587Sdholland 			goto tryagain;
23816ca35587Sdholland 		}
23826ca35587Sdholland 		for (i = 0; i < (ret * 2); i++) {
23836ca35587Sdholland 			if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) ==
23846ca35587Sdholland 			    ND_NFSV4) {
23856ca35587Sdholland 			    NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
23866ca35587Sdholland 			    if (*(tl + 1)) {
23876ca35587Sdholland 				if (i == 0 && ret > 1) {
23886ca35587Sdholland 				    /*
23896ca35587Sdholland 				     * If the Delegreturn failed, try again
23906ca35587Sdholland 				     * without it. The server will Recall, as
23916ca35587Sdholland 				     * required.
23926ca35587Sdholland 				     * If ret > 1, the first iteration of this
23936ca35587Sdholland 				     * loop is the second DelegReturn result.
23946ca35587Sdholland 				     */
23956ca35587Sdholland 				    mbuf_freem(nd->nd_mrep);
23966ca35587Sdholland 				    goto tryagain;
23976ca35587Sdholland 				} else {
23986ca35587Sdholland 				    nd->nd_flag |= ND_NOMOREDATA;
23996ca35587Sdholland 				}
24006ca35587Sdholland 			    }
24016ca35587Sdholland 			}
24026ca35587Sdholland 		}
24036ca35587Sdholland 		/* Now, the first wcc attribute reply. */
24046ca35587Sdholland 		if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) == ND_NFSV4) {
24056ca35587Sdholland 			NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
24066ca35587Sdholland 			if (*(tl + 1))
24076ca35587Sdholland 				nd->nd_flag |= ND_NOMOREDATA;
24086ca35587Sdholland 		}
24096ca35587Sdholland 		error = nfscl_wcc_data(nd, fdvp, fnap, fattrflagp, NULL,
24106ca35587Sdholland 		    fstuff);
24116ca35587Sdholland 		/* and the second wcc attribute reply. */
24126ca35587Sdholland 		if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) == ND_NFSV4 &&
24136ca35587Sdholland 		    !error) {
24146ca35587Sdholland 			NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
24156ca35587Sdholland 			if (*(tl + 1))
24166ca35587Sdholland 				nd->nd_flag |= ND_NOMOREDATA;
24176ca35587Sdholland 		}
24186ca35587Sdholland 		if (!error)
24196ca35587Sdholland 			error = nfscl_wcc_data(nd, tdvp, tnap, tattrflagp,
24206ca35587Sdholland 			    NULL, tstuff);
24216ca35587Sdholland 	}
24226ca35587Sdholland 	if (nd->nd_repstat && !error)
24236ca35587Sdholland 		error = nd->nd_repstat;
24246ca35587Sdholland nfsmout:
24256ca35587Sdholland 	mbuf_freem(nd->nd_mrep);
24266ca35587Sdholland 	return (error);
24276ca35587Sdholland }
24286ca35587Sdholland 
24296ca35587Sdholland /*
24306ca35587Sdholland  * nfs hard link create rpc
24316ca35587Sdholland  */
24326ca35587Sdholland APPLESTATIC int
nfsrpc_link(vnode_t dvp,vnode_t vp,char * name,int namelen,struct ucred * cred,NFSPROC_T * p,struct nfsvattr * dnap,struct nfsvattr * nap,int * attrflagp,int * dattrflagp,void * dstuff)24336ca35587Sdholland nfsrpc_link(vnode_t dvp, vnode_t vp, char *name, int namelen,
24346ca35587Sdholland     struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap,
24356ca35587Sdholland     struct nfsvattr *nap, int *attrflagp, int *dattrflagp, void *dstuff)
24366ca35587Sdholland {
24376ca35587Sdholland 	u_int32_t *tl;
24386ca35587Sdholland 	struct nfsrv_descript nfsd, *nd = &nfsd;
24396ca35587Sdholland 	nfsattrbit_t attrbits;
24406ca35587Sdholland 	int error = 0;
24416ca35587Sdholland 
24426ca35587Sdholland 	*attrflagp = 0;
24436ca35587Sdholland 	*dattrflagp = 0;
24446ca35587Sdholland 	if (namelen > NFS_MAXNAMLEN)
24456ca35587Sdholland 		return (ENAMETOOLONG);
24466ca35587Sdholland 	NFSCL_REQSTART(nd, NFSPROC_LINK, vp);
24476ca35587Sdholland 	if (nd->nd_flag & ND_NFSV4) {
24486ca35587Sdholland 		NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
24496ca35587Sdholland 		*tl = txdr_unsigned(NFSV4OP_PUTFH);
24506ca35587Sdholland 	}
24516ca35587Sdholland 	(void) nfsm_fhtom(nd, VTONFS(dvp)->n_fhp->nfh_fh,
24526ca35587Sdholland 		VTONFS(dvp)->n_fhp->nfh_len, 0);
24536ca35587Sdholland 	if (nd->nd_flag & ND_NFSV4) {
24546ca35587Sdholland 		NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
24556ca35587Sdholland 		*tl = txdr_unsigned(NFSV4OP_GETATTR);
24566ca35587Sdholland 		NFSWCCATTR_ATTRBIT(&attrbits);
24576ca35587Sdholland 		(void) nfsrv_putattrbit(nd, &attrbits);
24586ca35587Sdholland 		nd->nd_flag |= ND_V4WCCATTR;
24596ca35587Sdholland 		NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
24606ca35587Sdholland 		*tl = txdr_unsigned(NFSV4OP_LINK);
24616ca35587Sdholland 	}
24626ca35587Sdholland 	(void) nfsm_strtom(nd, name, namelen);
24636ca35587Sdholland 	error = nfscl_request(nd, vp, p, cred, dstuff);
24646ca35587Sdholland 	if (error)
24656ca35587Sdholland 		return (error);
24666ca35587Sdholland 	if (nd->nd_flag & ND_NFSV3) {
24676ca35587Sdholland 		error = nfscl_postop_attr(nd, nap, attrflagp, dstuff);
24686ca35587Sdholland 		if (!error)
24696ca35587Sdholland 			error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp,
24706ca35587Sdholland 			    NULL, dstuff);
24716ca35587Sdholland 	} else if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) == ND_NFSV4) {
24726ca35587Sdholland 		/*
24736ca35587Sdholland 		 * First, parse out the PutFH and Getattr result.
24746ca35587Sdholland 		 */
24756ca35587Sdholland 		NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
24766ca35587Sdholland 		if (!(*(tl + 1)))
24776ca35587Sdholland 			NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
24786ca35587Sdholland 		if (*(tl + 1))
24796ca35587Sdholland 			nd->nd_flag |= ND_NOMOREDATA;
24806ca35587Sdholland 		/*
24816ca35587Sdholland 		 * Get the pre-op attributes.
24826ca35587Sdholland 		 */
24836ca35587Sdholland 		error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
24846ca35587Sdholland 	}
24856ca35587Sdholland 	if (nd->nd_repstat && !error)
24866ca35587Sdholland 		error = nd->nd_repstat;
24876ca35587Sdholland nfsmout:
24886ca35587Sdholland 	mbuf_freem(nd->nd_mrep);
24896ca35587Sdholland 	return (error);
24906ca35587Sdholland }
24916ca35587Sdholland 
24926ca35587Sdholland /*
24936ca35587Sdholland  * nfs symbolic link create rpc
24946ca35587Sdholland  */
24956ca35587Sdholland APPLESTATIC int
nfsrpc_symlink(vnode_t dvp,char * name,int namelen,char * target,struct vattr * vap,struct ucred * cred,NFSPROC_T * p,struct nfsvattr * dnap,struct nfsvattr * nnap,struct nfsfh ** nfhpp,int * attrflagp,int * dattrflagp,void * dstuff)24966ca35587Sdholland nfsrpc_symlink(vnode_t dvp, char *name, int namelen, char *target,
24976ca35587Sdholland     struct vattr *vap, struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap,
24986ca35587Sdholland     struct nfsvattr *nnap, struct nfsfh **nfhpp, int *attrflagp,
24996ca35587Sdholland     int *dattrflagp, void *dstuff)
25006ca35587Sdholland {
25016ca35587Sdholland 	u_int32_t *tl;
25026ca35587Sdholland 	struct nfsrv_descript nfsd, *nd = &nfsd;
25036ca35587Sdholland 	struct nfsmount *nmp;
25046ca35587Sdholland 	int slen, error = 0;
25056ca35587Sdholland 
25066ca35587Sdholland 	*nfhpp = NULL;
25076ca35587Sdholland 	*attrflagp = 0;
25086ca35587Sdholland 	*dattrflagp = 0;
25096ca35587Sdholland 	nmp = VFSTONFS(vnode_mount(dvp));
25106ca35587Sdholland 	slen = strlen(target);
25116ca35587Sdholland 	if (slen > NFS_MAXPATHLEN || namelen > NFS_MAXNAMLEN)
25126ca35587Sdholland 		return (ENAMETOOLONG);
25136ca35587Sdholland 	NFSCL_REQSTART(nd, NFSPROC_SYMLINK, dvp);
25146ca35587Sdholland 	if (nd->nd_flag & ND_NFSV4) {
25156ca35587Sdholland 		NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
25166ca35587Sdholland 		*tl = txdr_unsigned(NFLNK);
25176ca35587Sdholland 		(void) nfsm_strtom(nd, target, slen);
25186ca35587Sdholland 	}
25196ca35587Sdholland 	(void) nfsm_strtom(nd, name, namelen);
25206ca35587Sdholland 	if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4))
25216ca35587Sdholland 		nfscl_fillsattr(nd, vap, dvp, 0, 0);
25226ca35587Sdholland 	if (!(nd->nd_flag & ND_NFSV4))
25236ca35587Sdholland 		(void) nfsm_strtom(nd, target, slen);
25246ca35587Sdholland 	if (nd->nd_flag & ND_NFSV2)
25256ca35587Sdholland 		nfscl_fillsattr(nd, vap, dvp, NFSSATTR_SIZENEG1, 0);
25266ca35587Sdholland 	error = nfscl_request(nd, dvp, p, cred, dstuff);
25276ca35587Sdholland 	if (error)
25286ca35587Sdholland 		return (error);
25296ca35587Sdholland 	if (nd->nd_flag & ND_NFSV4)
25306ca35587Sdholland 		error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
25316ca35587Sdholland 	if ((nd->nd_flag & ND_NFSV3) && !error) {
25326ca35587Sdholland 		if (!nd->nd_repstat)
25336ca35587Sdholland 			error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp);
25346ca35587Sdholland 		if (!error)
25356ca35587Sdholland 			error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp,
25366ca35587Sdholland 			    NULL, dstuff);
25376ca35587Sdholland 	}
25386ca35587Sdholland 	if (nd->nd_repstat && !error)
25396ca35587Sdholland 		error = nd->nd_repstat;
25406ca35587Sdholland 	mbuf_freem(nd->nd_mrep);
25416ca35587Sdholland 	/*
25426ca35587Sdholland 	 * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry.
2543e81f0ea2Spgoyette 	 * Only do this if vfs.nfs.ignore_eexist is set.
2544e81f0ea2Spgoyette 	 * Never do this for NFSv4.1 or later minor versions, since sessions
2545e81f0ea2Spgoyette 	 * should guarantee "exactly once" RPC semantics.
25466ca35587Sdholland 	 */
2547e81f0ea2Spgoyette 	if (error == EEXIST && nfsignore_eexist != 0 && (!NFSHASNFSV4(nmp) ||
2548e81f0ea2Spgoyette 	    nmp->nm_minorvers == 0))
25496ca35587Sdholland 		error = 0;
25506ca35587Sdholland 	return (error);
25516ca35587Sdholland }
25526ca35587Sdholland 
25536ca35587Sdholland /*
25546ca35587Sdholland  * nfs make dir rpc
25556ca35587Sdholland  */
25566ca35587Sdholland APPLESTATIC int
nfsrpc_mkdir(vnode_t dvp,char * name,int namelen,struct vattr * vap,struct ucred * cred,NFSPROC_T * p,struct nfsvattr * dnap,struct nfsvattr * nnap,struct nfsfh ** nfhpp,int * attrflagp,int * dattrflagp,void * dstuff)25576ca35587Sdholland nfsrpc_mkdir(vnode_t dvp, char *name, int namelen, struct vattr *vap,
25586ca35587Sdholland     struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap,
25596ca35587Sdholland     struct nfsvattr *nnap, struct nfsfh **nfhpp, int *attrflagp,
25606ca35587Sdholland     int *dattrflagp, void *dstuff)
25616ca35587Sdholland {
25626ca35587Sdholland 	u_int32_t *tl;
25636ca35587Sdholland 	struct nfsrv_descript nfsd, *nd = &nfsd;
25646ca35587Sdholland 	nfsattrbit_t attrbits;
25656ca35587Sdholland 	int error = 0;
2566e81f0ea2Spgoyette 	struct nfsfh *fhp;
2567e81f0ea2Spgoyette 	struct nfsmount *nmp;
25686ca35587Sdholland 
25696ca35587Sdholland 	*nfhpp = NULL;
25706ca35587Sdholland 	*attrflagp = 0;
25716ca35587Sdholland 	*dattrflagp = 0;
2572e81f0ea2Spgoyette 	nmp = VFSTONFS(vnode_mount(dvp));
2573e81f0ea2Spgoyette 	fhp = VTONFS(dvp)->n_fhp;
25746ca35587Sdholland 	if (namelen > NFS_MAXNAMLEN)
25756ca35587Sdholland 		return (ENAMETOOLONG);
25766ca35587Sdholland 	NFSCL_REQSTART(nd, NFSPROC_MKDIR, dvp);
25776ca35587Sdholland 	if (nd->nd_flag & ND_NFSV4) {
25786ca35587Sdholland 		NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
25796ca35587Sdholland 		*tl = txdr_unsigned(NFDIR);
25806ca35587Sdholland 	}
25816ca35587Sdholland 	(void) nfsm_strtom(nd, name, namelen);
25826ca35587Sdholland 	nfscl_fillsattr(nd, vap, dvp, NFSSATTR_SIZENEG1, 0);
25836ca35587Sdholland 	if (nd->nd_flag & ND_NFSV4) {
25846ca35587Sdholland 		NFSGETATTR_ATTRBIT(&attrbits);
25856ca35587Sdholland 		NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
25866ca35587Sdholland 		*tl++ = txdr_unsigned(NFSV4OP_GETFH);
25876ca35587Sdholland 		*tl = txdr_unsigned(NFSV4OP_GETATTR);
25886ca35587Sdholland 		(void) nfsrv_putattrbit(nd, &attrbits);
2589e81f0ea2Spgoyette 		NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2590e81f0ea2Spgoyette 		*tl = txdr_unsigned(NFSV4OP_PUTFH);
2591e81f0ea2Spgoyette 		(void) nfsm_fhtom(nd, fhp->nfh_fh, fhp->nfh_len, 0);
2592e81f0ea2Spgoyette 		NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2593e81f0ea2Spgoyette 		*tl = txdr_unsigned(NFSV4OP_GETATTR);
2594e81f0ea2Spgoyette 		(void) nfsrv_putattrbit(nd, &attrbits);
25956ca35587Sdholland 	}
25966ca35587Sdholland 	error = nfscl_request(nd, dvp, p, cred, dstuff);
25976ca35587Sdholland 	if (error)
25986ca35587Sdholland 		return (error);
25996ca35587Sdholland 	if (nd->nd_flag & ND_NFSV4)
26006ca35587Sdholland 		error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
26016ca35587Sdholland 	if (!nd->nd_repstat && !error) {
26026ca35587Sdholland 		if (nd->nd_flag & ND_NFSV4) {
26036ca35587Sdholland 			NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
26046ca35587Sdholland 			error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
26056ca35587Sdholland 		}
26066ca35587Sdholland 		if (!error)
26076ca35587Sdholland 			error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp);
2608e81f0ea2Spgoyette 		if (error == 0 && (nd->nd_flag & ND_NFSV4) != 0) {
2609e81f0ea2Spgoyette 			/* Get rid of the PutFH and Getattr status values. */
2610e81f0ea2Spgoyette 			NFSM_DISSECT(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
2611e81f0ea2Spgoyette 			/* Load the directory attributes. */
2612e81f0ea2Spgoyette 			error = nfsm_loadattr(nd, dnap);
2613e81f0ea2Spgoyette 			if (error == 0)
2614e81f0ea2Spgoyette 				*dattrflagp = 1;
2615e81f0ea2Spgoyette 		}
26166ca35587Sdholland 	}
26176ca35587Sdholland 	if ((nd->nd_flag & ND_NFSV3) && !error)
26186ca35587Sdholland 		error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
26196ca35587Sdholland 	if (nd->nd_repstat && !error)
26206ca35587Sdholland 		error = nd->nd_repstat;
26216ca35587Sdholland nfsmout:
26226ca35587Sdholland 	mbuf_freem(nd->nd_mrep);
26236ca35587Sdholland 	/*
2624e81f0ea2Spgoyette 	 * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry.
2625e81f0ea2Spgoyette 	 * Only do this if vfs.nfs.ignore_eexist is set.
2626e81f0ea2Spgoyette 	 * Never do this for NFSv4.1 or later minor versions, since sessions
2627e81f0ea2Spgoyette 	 * should guarantee "exactly once" RPC semantics.
26286ca35587Sdholland 	 */
2629e81f0ea2Spgoyette 	if (error == EEXIST && nfsignore_eexist != 0 && (!NFSHASNFSV4(nmp) ||
2630e81f0ea2Spgoyette 	    nmp->nm_minorvers == 0))
26316ca35587Sdholland 		error = 0;
26326ca35587Sdholland 	return (error);
26336ca35587Sdholland }
26346ca35587Sdholland 
26356ca35587Sdholland /*
26366ca35587Sdholland  * nfs remove directory call
26376ca35587Sdholland  */
26386ca35587Sdholland APPLESTATIC int
nfsrpc_rmdir(vnode_t dvp,char * name,int namelen,struct ucred * cred,NFSPROC_T * p,struct nfsvattr * dnap,int * dattrflagp,void * dstuff)26396ca35587Sdholland nfsrpc_rmdir(vnode_t dvp, char *name, int namelen, struct ucred *cred,
26406ca35587Sdholland     NFSPROC_T *p, struct nfsvattr *dnap, int *dattrflagp, void *dstuff)
26416ca35587Sdholland {
26426ca35587Sdholland 	struct nfsrv_descript nfsd, *nd = &nfsd;
26436ca35587Sdholland 	int error = 0;
26446ca35587Sdholland 
26456ca35587Sdholland 	*dattrflagp = 0;
26466ca35587Sdholland 	if (namelen > NFS_MAXNAMLEN)
26476ca35587Sdholland 		return (ENAMETOOLONG);
26486ca35587Sdholland 	NFSCL_REQSTART(nd, NFSPROC_RMDIR, dvp);
26496ca35587Sdholland 	(void) nfsm_strtom(nd, name, namelen);
26506ca35587Sdholland 	error = nfscl_request(nd, dvp, p, cred, dstuff);
26516ca35587Sdholland 	if (error)
26526ca35587Sdholland 		return (error);
26536ca35587Sdholland 	if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4))
26546ca35587Sdholland 		error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
26556ca35587Sdholland 	if (nd->nd_repstat && !error)
26566ca35587Sdholland 		error = nd->nd_repstat;
26576ca35587Sdholland 	mbuf_freem(nd->nd_mrep);
26586ca35587Sdholland 	/*
26596ca35587Sdholland 	 * Kludge: Map ENOENT => 0 assuming that you have a reply to a retry.
26606ca35587Sdholland 	 */
26616ca35587Sdholland 	if (error == ENOENT)
26626ca35587Sdholland 		error = 0;
26636ca35587Sdholland 	return (error);
26646ca35587Sdholland }
26656ca35587Sdholland 
26666ca35587Sdholland /*
26676ca35587Sdholland  * Readdir rpc.
26686ca35587Sdholland  * Always returns with either uio_resid unchanged, if you are at the
26696ca35587Sdholland  * end of the directory, or uio_resid == 0, with all DIRBLKSIZ chunks
26706ca35587Sdholland  * filled in.
26716ca35587Sdholland  * I felt this would allow caching of directory blocks more easily
26726ca35587Sdholland  * than returning a pertially filled block.
26736ca35587Sdholland  * Directory offset cookies:
26746ca35587Sdholland  * Oh my, what to do with them...
26756ca35587Sdholland  * I can think of three ways to deal with them:
26766ca35587Sdholland  * 1 - have the layer above these RPCs maintain a map between logical
26776ca35587Sdholland  *     directory byte offsets and the NFS directory offset cookies
26786ca35587Sdholland  * 2 - pass the opaque directory offset cookies up into userland
26796ca35587Sdholland  *     and let the libc functions deal with them, via the system call
26806ca35587Sdholland  * 3 - return them to userland in the "struct dirent", so future versions
2681e81f0ea2Spgoyette  *     of libc can use them and do whatever is necessary to make things work
26826ca35587Sdholland  *     above these rpc calls, in the meantime
26836ca35587Sdholland  * For now, I do #3 by "hiding" the directory offset cookies after the
26846ca35587Sdholland  * d_name field in struct dirent. This is space inside d_reclen that
26856ca35587Sdholland  * will be ignored by anything that doesn't know about them.
26866ca35587Sdholland  * The directory offset cookies are filled in as the last 8 bytes of
26876ca35587Sdholland  * each directory entry, after d_name. Someday, the userland libc
26886ca35587Sdholland  * functions may be able to use these. In the meantime, it satisfies
26896ca35587Sdholland  * OpenBSD's requirements for cookies being returned.
26906ca35587Sdholland  * If expects the directory offset cookie for the read to be in uio_offset
26916ca35587Sdholland  * and returns the one for the next entry after this directory block in
26926ca35587Sdholland  * there, as well.
26936ca35587Sdholland  */
26946ca35587Sdholland APPLESTATIC int
nfsrpc_readdir(vnode_t vp,struct uio * uiop,nfsuint64 * cookiep,struct ucred * cred,NFSPROC_T * p,struct nfsvattr * nap,int * attrflagp,int * eofp,void * stuff)26956ca35587Sdholland nfsrpc_readdir(vnode_t vp, struct uio *uiop, nfsuint64 *cookiep,
26966ca35587Sdholland     struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp,
26976ca35587Sdholland     int *eofp, void *stuff)
26986ca35587Sdholland {
26996ca35587Sdholland 	int len, left;
27006ca35587Sdholland 	struct dirent *dp = NULL;
27016ca35587Sdholland 	u_int32_t *tl;
27026ca35587Sdholland 	nfsquad_t cookie, ncookie;
27036ca35587Sdholland 	struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
27046ca35587Sdholland 	struct nfsnode *dnp = VTONFS(vp);
27056ca35587Sdholland 	struct nfsvattr nfsva;
27066ca35587Sdholland 	struct nfsrv_descript nfsd, *nd = &nfsd;
27076ca35587Sdholland 	int error = 0, tlen, more_dirs = 1, blksiz = 0, bigenough = 1;
27086ca35587Sdholland 	int reqsize, tryformoredirs = 1, readsize, eof = 0, gotmnton = 0;
27096ca35587Sdholland 	long dotfileid, dotdotfileid = 0;
27106ca35587Sdholland 	u_int32_t fakefileno = 0xffffffff, rderr;
27116ca35587Sdholland 	char *cp;
27126ca35587Sdholland 	nfsattrbit_t attrbits, dattrbits;
27136ca35587Sdholland 	u_int32_t *tl2 = NULL;
27146ca35587Sdholland 	size_t tresid;
27156ca35587Sdholland 
27166ca35587Sdholland 	KASSERT(uiop->uio_iovcnt == 1 &&
27176ca35587Sdholland 	    (uio_uio_resid(uiop) & (DIRBLKSIZ - 1)) == 0,
27186ca35587Sdholland 	    ("nfs readdirrpc bad uio"));
27196ca35587Sdholland 
27206ca35587Sdholland 	/*
27216ca35587Sdholland 	 * There is no point in reading a lot more than uio_resid, however
27226ca35587Sdholland 	 * adding one additional DIRBLKSIZ makes sense. Since uio_resid
27236ca35587Sdholland 	 * and nm_readdirsize are both exact multiples of DIRBLKSIZ, this
27246ca35587Sdholland 	 * will never make readsize > nm_readdirsize.
27256ca35587Sdholland 	 */
27266ca35587Sdholland 	readsize = nmp->nm_readdirsize;
27276ca35587Sdholland 	if (readsize > uio_uio_resid(uiop))
27286ca35587Sdholland 		readsize = uio_uio_resid(uiop) + DIRBLKSIZ;
27296ca35587Sdholland 
27306ca35587Sdholland 	*attrflagp = 0;
27316ca35587Sdholland 	if (eofp)
27326ca35587Sdholland 		*eofp = 0;
27336ca35587Sdholland 	tresid = uio_uio_resid(uiop);
27346ca35587Sdholland 	cookie.lval[0] = cookiep->nfsuquad[0];
27356ca35587Sdholland 	cookie.lval[1] = cookiep->nfsuquad[1];
27366ca35587Sdholland 	nd->nd_mrep = NULL;
27376ca35587Sdholland 
27386ca35587Sdholland 	/*
27396ca35587Sdholland 	 * For NFSv4, first create the "." and ".." entries.
27406ca35587Sdholland 	 */
27416ca35587Sdholland 	if (NFSHASNFSV4(nmp)) {
27426ca35587Sdholland 		reqsize = 6 * NFSX_UNSIGNED;
27436ca35587Sdholland 		NFSGETATTR_ATTRBIT(&dattrbits);
27446ca35587Sdholland 		NFSZERO_ATTRBIT(&attrbits);
27456ca35587Sdholland 		NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_FILEID);
27466ca35587Sdholland 		NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TYPE);
27476ca35587Sdholland 		if (NFSISSET_ATTRBIT(&dnp->n_vattr.na_suppattr,
27486ca35587Sdholland 		    NFSATTRBIT_MOUNTEDONFILEID)) {
27496ca35587Sdholland 			NFSSETBIT_ATTRBIT(&attrbits,
27506ca35587Sdholland 			    NFSATTRBIT_MOUNTEDONFILEID);
27516ca35587Sdholland 			gotmnton = 1;
27526ca35587Sdholland 		} else {
27536ca35587Sdholland 			/*
27546ca35587Sdholland 			 * Must fake it. Use the fileno, except when the
27556ca35587Sdholland 			 * fsid is != to that of the directory. For that
27566ca35587Sdholland 			 * case, generate a fake fileno that is not the same.
27576ca35587Sdholland 			 */
27586ca35587Sdholland 			NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_FSID);
27596ca35587Sdholland 			gotmnton = 0;
27606ca35587Sdholland 		}
27616ca35587Sdholland 
27626ca35587Sdholland 		/*
27636ca35587Sdholland 		 * Joy, oh joy. For V4 we get to hand craft '.' and '..'.
27646ca35587Sdholland 		 */
27656ca35587Sdholland 		if (uiop->uio_offset == 0) {
27666ca35587Sdholland 			NFSCL_REQSTART(nd, NFSPROC_LOOKUPP, vp);
27676ca35587Sdholland 			NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
27686ca35587Sdholland 			*tl++ = txdr_unsigned(NFSV4OP_GETFH);
27696ca35587Sdholland 			*tl = txdr_unsigned(NFSV4OP_GETATTR);
27706ca35587Sdholland 			(void) nfsrv_putattrbit(nd, &attrbits);
27716ca35587Sdholland 			error = nfscl_request(nd, vp, p, cred, stuff);
27726ca35587Sdholland 			if (error)
27736ca35587Sdholland 			    return (error);
2774e81f0ea2Spgoyette 			dotfileid = 0;	/* Fake out the compiler. */
2775e81f0ea2Spgoyette 			if ((nd->nd_flag & ND_NOMOREDATA) == 0) {
2776e81f0ea2Spgoyette 			    error = nfsm_loadattr(nd, &nfsva);
2777e81f0ea2Spgoyette 			    if (error != 0)
2778e81f0ea2Spgoyette 				goto nfsmout;
2779e81f0ea2Spgoyette 			    dotfileid = nfsva.na_fileid;
2780e81f0ea2Spgoyette 			}
27816ca35587Sdholland 			if (nd->nd_repstat == 0) {
2782e81f0ea2Spgoyette 			    NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
2783e81f0ea2Spgoyette 			    len = fxdr_unsigned(int, *(tl + 4));
27846ca35587Sdholland 			    if (len > 0 && len <= NFSX_V4FHMAX)
27856ca35587Sdholland 				error = nfsm_advance(nd, NFSM_RNDUP(len), -1);
27866ca35587Sdholland 			    else
27876ca35587Sdholland 				error = EPERM;
27886ca35587Sdholland 			    if (!error) {
27896ca35587Sdholland 				NFSM_DISSECT(tl, u_int32_t *, 2*NFSX_UNSIGNED);
27906ca35587Sdholland 				nfsva.na_mntonfileno = 0xffffffff;
27916ca35587Sdholland 				error = nfsv4_loadattr(nd, NULL, &nfsva, NULL,
27926ca35587Sdholland 				    NULL, 0, NULL, NULL, NULL, NULL, NULL, 0,
27936ca35587Sdholland 				    NULL, NULL, NULL, p, cred);
27946ca35587Sdholland 				if (error) {
27956ca35587Sdholland 				    dotdotfileid = dotfileid;
27966ca35587Sdholland 				} else if (gotmnton) {
27976ca35587Sdholland 				    if (nfsva.na_mntonfileno != 0xffffffff)
27986ca35587Sdholland 					dotdotfileid = nfsva.na_mntonfileno;
27996ca35587Sdholland 				    else
28006ca35587Sdholland 					dotdotfileid = nfsva.na_fileid;
28016ca35587Sdholland 				} else if (nfsva.na_filesid[0] ==
28026ca35587Sdholland 				    dnp->n_vattr.na_filesid[0] &&
28036ca35587Sdholland 				    nfsva.na_filesid[1] ==
28046ca35587Sdholland 				    dnp->n_vattr.na_filesid[1]) {
28056ca35587Sdholland 				    dotdotfileid = nfsva.na_fileid;
28066ca35587Sdholland 				} else {
28076ca35587Sdholland 				    do {
28086ca35587Sdholland 					fakefileno--;
28096ca35587Sdholland 				    } while (fakefileno ==
28106ca35587Sdholland 					nfsva.na_fileid);
28116ca35587Sdholland 				    dotdotfileid = fakefileno;
28126ca35587Sdholland 				}
28136ca35587Sdholland 			    }
28146ca35587Sdholland 			} else if (nd->nd_repstat == NFSERR_NOENT) {
28156ca35587Sdholland 			    /*
28166ca35587Sdholland 			     * Lookupp returns NFSERR_NOENT when we are
28176ca35587Sdholland 			     * at the root, so just use the current dir.
28186ca35587Sdholland 			     */
28196ca35587Sdholland 			    nd->nd_repstat = 0;
28206ca35587Sdholland 			    dotdotfileid = dotfileid;
28216ca35587Sdholland 			} else {
28226ca35587Sdholland 			    error = nd->nd_repstat;
28236ca35587Sdholland 			}
28246ca35587Sdholland 			mbuf_freem(nd->nd_mrep);
28256ca35587Sdholland 			if (error)
28266ca35587Sdholland 			    return (error);
28276ca35587Sdholland 			nd->nd_mrep = NULL;
28286ca35587Sdholland 			dp = (struct dirent *) CAST_DOWN(caddr_t, uio_iov_base(uiop));
28296ca35587Sdholland 			dp->d_type = DT_DIR;
28306ca35587Sdholland 			dp->d_fileno = dotfileid;
28316ca35587Sdholland 			dp->d_namlen = 1;
28326ca35587Sdholland 			dp->d_name[0] = '.';
28336ca35587Sdholland 			dp->d_name[1] = '\0';
28346ca35587Sdholland 			dp->d_reclen = DIRENT_SIZE(dp) + NFSX_HYPER;
28356ca35587Sdholland 			/*
28366ca35587Sdholland 			 * Just make these offset cookie 0.
28376ca35587Sdholland 			 */
28386ca35587Sdholland 			tl = (u_int32_t *)&dp->d_name[4];
28396ca35587Sdholland 			*tl++ = 0;
28406ca35587Sdholland 			*tl = 0;
28416ca35587Sdholland 			blksiz += dp->d_reclen;
28426ca35587Sdholland 			uio_uio_resid_add(uiop, -(dp->d_reclen));
28436ca35587Sdholland 			uiop->uio_offset += dp->d_reclen;
28446ca35587Sdholland 			uio_iov_base_add(uiop, dp->d_reclen);
28456ca35587Sdholland 			uio_iov_len_add(uiop, -(dp->d_reclen));
28466ca35587Sdholland 			dp = (struct dirent *) CAST_DOWN(caddr_t, uio_iov_base(uiop));
28476ca35587Sdholland 			dp->d_type = DT_DIR;
28486ca35587Sdholland 			dp->d_fileno = dotdotfileid;
28496ca35587Sdholland 			dp->d_namlen = 2;
28506ca35587Sdholland 			dp->d_name[0] = '.';
28516ca35587Sdholland 			dp->d_name[1] = '.';
28526ca35587Sdholland 			dp->d_name[2] = '\0';
28536ca35587Sdholland 			dp->d_reclen = DIRENT_SIZE(dp) + NFSX_HYPER;
28546ca35587Sdholland 			/*
28556ca35587Sdholland 			 * Just make these offset cookie 0.
28566ca35587Sdholland 			 */
28576ca35587Sdholland 			tl = (u_int32_t *)&dp->d_name[4];
28586ca35587Sdholland 			*tl++ = 0;
28596ca35587Sdholland 			*tl = 0;
28606ca35587Sdholland 			blksiz += dp->d_reclen;
28616ca35587Sdholland 			uio_uio_resid_add(uiop, -(dp->d_reclen));
28626ca35587Sdholland 			uiop->uio_offset += dp->d_reclen;
28636ca35587Sdholland 			uio_iov_base_add(uiop, dp->d_reclen);
28646ca35587Sdholland 			uio_iov_len_add(uiop, -(dp->d_reclen));
28656ca35587Sdholland 		}
28666ca35587Sdholland 		NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_RDATTRERROR);
28676ca35587Sdholland 	} else {
28686ca35587Sdholland 		reqsize = 5 * NFSX_UNSIGNED;
28696ca35587Sdholland 	}
28706ca35587Sdholland 
28716ca35587Sdholland 
28726ca35587Sdholland 	/*
28736ca35587Sdholland 	 * Loop around doing readdir rpc's of size readsize.
28746ca35587Sdholland 	 * The stopping criteria is EOF or buffer full.
28756ca35587Sdholland 	 */
28766ca35587Sdholland 	while (more_dirs && bigenough) {
28776ca35587Sdholland 		*attrflagp = 0;
28786ca35587Sdholland 		NFSCL_REQSTART(nd, NFSPROC_READDIR, vp);
28796ca35587Sdholland 		if (nd->nd_flag & ND_NFSV2) {
28806ca35587Sdholland 			NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
28816ca35587Sdholland 			*tl++ = cookie.lval[1];
28826ca35587Sdholland 			*tl = txdr_unsigned(readsize);
28836ca35587Sdholland 		} else {
28846ca35587Sdholland 			NFSM_BUILD(tl, u_int32_t *, reqsize);
28856ca35587Sdholland 			*tl++ = cookie.lval[0];
28866ca35587Sdholland 			*tl++ = cookie.lval[1];
28876ca35587Sdholland 			if (cookie.qval == 0) {
28886ca35587Sdholland 				*tl++ = 0;
28896ca35587Sdholland 				*tl++ = 0;
28906ca35587Sdholland 			} else {
28916ca35587Sdholland 				NFSLOCKNODE(dnp);
28926ca35587Sdholland 				*tl++ = dnp->n_cookieverf.nfsuquad[0];
28936ca35587Sdholland 				*tl++ = dnp->n_cookieverf.nfsuquad[1];
28946ca35587Sdholland 				NFSUNLOCKNODE(dnp);
28956ca35587Sdholland 			}
28966ca35587Sdholland 			if (nd->nd_flag & ND_NFSV4) {
28976ca35587Sdholland 				*tl++ = txdr_unsigned(readsize);
28986ca35587Sdholland 				*tl = txdr_unsigned(readsize);
28996ca35587Sdholland 				(void) nfsrv_putattrbit(nd, &attrbits);
29006ca35587Sdholland 				NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
29016ca35587Sdholland 				*tl = txdr_unsigned(NFSV4OP_GETATTR);
29026ca35587Sdholland 				(void) nfsrv_putattrbit(nd, &dattrbits);
29036ca35587Sdholland 			} else {
29046ca35587Sdholland 				*tl = txdr_unsigned(readsize);
29056ca35587Sdholland 			}
29066ca35587Sdholland 		}
29076ca35587Sdholland 		error = nfscl_request(nd, vp, p, cred, stuff);
29086ca35587Sdholland 		if (error)
29096ca35587Sdholland 			return (error);
29106ca35587Sdholland 		if (!(nd->nd_flag & ND_NFSV2)) {
29116ca35587Sdholland 			if (nd->nd_flag & ND_NFSV3)
29126ca35587Sdholland 				error = nfscl_postop_attr(nd, nap, attrflagp,
29136ca35587Sdholland 				    stuff);
29146ca35587Sdholland 			if (!nd->nd_repstat && !error) {
29156ca35587Sdholland 				NFSM_DISSECT(tl, u_int32_t *, NFSX_HYPER);
29166ca35587Sdholland 				NFSLOCKNODE(dnp);
29176ca35587Sdholland 				dnp->n_cookieverf.nfsuquad[0] = *tl++;
29186ca35587Sdholland 				dnp->n_cookieverf.nfsuquad[1] = *tl;
29196ca35587Sdholland 				NFSUNLOCKNODE(dnp);
29206ca35587Sdholland 			}
29216ca35587Sdholland 		}
29226ca35587Sdholland 		if (nd->nd_repstat || error) {
29236ca35587Sdholland 			if (!error)
29246ca35587Sdholland 				error = nd->nd_repstat;
29256ca35587Sdholland 			goto nfsmout;
29266ca35587Sdholland 		}
29276ca35587Sdholland 		NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
29286ca35587Sdholland 		more_dirs = fxdr_unsigned(int, *tl);
29296ca35587Sdholland 		if (!more_dirs)
29306ca35587Sdholland 			tryformoredirs = 0;
29316ca35587Sdholland 
2932e81f0ea2Spgoyette 		/* loop through the dir entries, doctoring them to 4bsd form */
29336ca35587Sdholland 		while (more_dirs && bigenough) {
29346ca35587Sdholland 			if (nd->nd_flag & ND_NFSV4) {
29356ca35587Sdholland 				NFSM_DISSECT(tl, u_int32_t *, 3*NFSX_UNSIGNED);
29366ca35587Sdholland 				ncookie.lval[0] = *tl++;
29376ca35587Sdholland 				ncookie.lval[1] = *tl++;
29386ca35587Sdholland 				len = fxdr_unsigned(int, *tl);
29396ca35587Sdholland 			} else if (nd->nd_flag & ND_NFSV3) {
29406ca35587Sdholland 				NFSM_DISSECT(tl, u_int32_t *, 3*NFSX_UNSIGNED);
29416ca35587Sdholland 				nfsva.na_fileid = fxdr_hyper(tl);
29426ca35587Sdholland 				tl += 2;
29436ca35587Sdholland 				len = fxdr_unsigned(int, *tl);
29446ca35587Sdholland 			} else {
29456ca35587Sdholland 				NFSM_DISSECT(tl, u_int32_t *, 2*NFSX_UNSIGNED);
29466ca35587Sdholland 				nfsva.na_fileid =
29476ca35587Sdholland 				    fxdr_unsigned(long, *tl++);
29486ca35587Sdholland 				len = fxdr_unsigned(int, *tl);
29496ca35587Sdholland 			}
29506ca35587Sdholland 			if (len <= 0 || len > NFS_MAXNAMLEN) {
29516ca35587Sdholland 				error = EBADRPC;
29526ca35587Sdholland 				goto nfsmout;
29536ca35587Sdholland 			}
29546ca35587Sdholland 			tlen = NFSM_RNDUP(len);
29556ca35587Sdholland 			if (tlen == len)
29566ca35587Sdholland 				tlen += 4;  /* To ensure null termination */
29576ca35587Sdholland 			left = DIRBLKSIZ - blksiz;
29586ca35587Sdholland 			if ((int)(tlen + DIRHDSIZ + NFSX_HYPER) > left) {
29596ca35587Sdholland 				dp->d_reclen += left;
29606ca35587Sdholland 				uio_iov_base_add(uiop, left);
29616ca35587Sdholland 				uio_iov_len_add(uiop, -(left));
29626ca35587Sdholland 				uio_uio_resid_add(uiop, -(left));
29636ca35587Sdholland 				uiop->uio_offset += left;
29646ca35587Sdholland 				blksiz = 0;
29656ca35587Sdholland 			}
29666ca35587Sdholland 			if ((int)(tlen + DIRHDSIZ + NFSX_HYPER) > uio_uio_resid(uiop))
29676ca35587Sdholland 				bigenough = 0;
29686ca35587Sdholland 			if (bigenough) {
29696ca35587Sdholland 				dp = (struct dirent *) CAST_DOWN(caddr_t, uio_iov_base(uiop));
29706ca35587Sdholland 				dp->d_namlen = len;
29716ca35587Sdholland 				dp->d_reclen = tlen + DIRHDSIZ + NFSX_HYPER;
29726ca35587Sdholland 				dp->d_type = DT_UNKNOWN;
29736ca35587Sdholland 				blksiz += dp->d_reclen;
29746ca35587Sdholland 				if (blksiz == DIRBLKSIZ)
29756ca35587Sdholland 					blksiz = 0;
29766ca35587Sdholland 				uio_uio_resid_add(uiop, -(DIRHDSIZ));
29776ca35587Sdholland 				uiop->uio_offset += DIRHDSIZ;
29786ca35587Sdholland 				uio_iov_base_add(uiop, DIRHDSIZ);
29796ca35587Sdholland 				uio_iov_len_add(uiop, -(DIRHDSIZ));
29806ca35587Sdholland 				error = nfsm_mbufuio(nd, uiop, len);
29816ca35587Sdholland 				if (error)
29826ca35587Sdholland 					goto nfsmout;
29836ca35587Sdholland 				cp = CAST_DOWN(caddr_t, uio_iov_base(uiop));
29846ca35587Sdholland 				tlen -= len;
29856ca35587Sdholland 				*cp = '\0';	/* null terminate */
29866ca35587Sdholland 				cp += tlen;	/* points to cookie storage */
29876ca35587Sdholland 				tl2 = (u_int32_t *)cp;
29886ca35587Sdholland 				uio_iov_base_add(uiop, (tlen + NFSX_HYPER));
29896ca35587Sdholland 				uio_iov_len_add(uiop, -(tlen + NFSX_HYPER));
29906ca35587Sdholland 				uio_uio_resid_add(uiop, -(tlen + NFSX_HYPER));
29916ca35587Sdholland 				uiop->uio_offset += (tlen + NFSX_HYPER);
29926ca35587Sdholland 			} else {
29936ca35587Sdholland 				error = nfsm_advance(nd, NFSM_RNDUP(len), -1);
29946ca35587Sdholland 				if (error)
29956ca35587Sdholland 					goto nfsmout;
29966ca35587Sdholland 			}
29976ca35587Sdholland 			if (nd->nd_flag & ND_NFSV4) {
29986ca35587Sdholland 				rderr = 0;
29996ca35587Sdholland 				nfsva.na_mntonfileno = 0xffffffff;
30006ca35587Sdholland 				error = nfsv4_loadattr(nd, NULL, &nfsva, NULL,
30016ca35587Sdholland 				    NULL, 0, NULL, NULL, NULL, NULL, NULL, 0,
30026ca35587Sdholland 				    NULL, NULL, &rderr, p, cred);
30036ca35587Sdholland 				if (error)
30046ca35587Sdholland 					goto nfsmout;
30056ca35587Sdholland 				NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
30066ca35587Sdholland 			} else if (nd->nd_flag & ND_NFSV3) {
30076ca35587Sdholland 				NFSM_DISSECT(tl, u_int32_t *, 3*NFSX_UNSIGNED);
30086ca35587Sdholland 				ncookie.lval[0] = *tl++;
30096ca35587Sdholland 				ncookie.lval[1] = *tl++;
30106ca35587Sdholland 			} else {
30116ca35587Sdholland 				NFSM_DISSECT(tl, u_int32_t *, 2*NFSX_UNSIGNED);
30126ca35587Sdholland 				ncookie.lval[0] = 0;
30136ca35587Sdholland 				ncookie.lval[1] = *tl++;
30146ca35587Sdholland 			}
30156ca35587Sdholland 			if (bigenough) {
30166ca35587Sdholland 			    if (nd->nd_flag & ND_NFSV4) {
30176ca35587Sdholland 				if (rderr) {
30186ca35587Sdholland 				    dp->d_fileno = 0;
30196ca35587Sdholland 				} else {
30206ca35587Sdholland 				    if (gotmnton) {
30216ca35587Sdholland 					if (nfsva.na_mntonfileno != 0xffffffff)
30226ca35587Sdholland 					    dp->d_fileno = nfsva.na_mntonfileno;
30236ca35587Sdholland 					else
30246ca35587Sdholland 					    dp->d_fileno = nfsva.na_fileid;
30256ca35587Sdholland 				    } else if (nfsva.na_filesid[0] ==
30266ca35587Sdholland 					dnp->n_vattr.na_filesid[0] &&
30276ca35587Sdholland 					nfsva.na_filesid[1] ==
30286ca35587Sdholland 					dnp->n_vattr.na_filesid[1]) {
30296ca35587Sdholland 					dp->d_fileno = nfsva.na_fileid;
30306ca35587Sdholland 				    } else {
30316ca35587Sdholland 					do {
30326ca35587Sdholland 					    fakefileno--;
30336ca35587Sdholland 					} while (fakefileno ==
30346ca35587Sdholland 					    nfsva.na_fileid);
30356ca35587Sdholland 					dp->d_fileno = fakefileno;
30366ca35587Sdholland 				    }
30376ca35587Sdholland 				    dp->d_type = vtonfs_dtype(nfsva.na_type);
30386ca35587Sdholland 				}
30396ca35587Sdholland 			    } else {
30406ca35587Sdholland 				dp->d_fileno = nfsva.na_fileid;
30416ca35587Sdholland 			    }
30426ca35587Sdholland 			    *tl2++ = cookiep->nfsuquad[0] = cookie.lval[0] =
30436ca35587Sdholland 				ncookie.lval[0];
30446ca35587Sdholland 			    *tl2 = cookiep->nfsuquad[1] = cookie.lval[1] =
30456ca35587Sdholland 				ncookie.lval[1];
30466ca35587Sdholland 			}
30476ca35587Sdholland 			more_dirs = fxdr_unsigned(int, *tl);
30486ca35587Sdholland 		}
30496ca35587Sdholland 		/*
30506ca35587Sdholland 		 * If at end of rpc data, get the eof boolean
30516ca35587Sdholland 		 */
30526ca35587Sdholland 		if (!more_dirs) {
30536ca35587Sdholland 			NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
30546ca35587Sdholland 			eof = fxdr_unsigned(int, *tl);
30556ca35587Sdholland 			if (tryformoredirs)
30566ca35587Sdholland 				more_dirs = !eof;
30576ca35587Sdholland 			if (nd->nd_flag & ND_NFSV4) {
30586ca35587Sdholland 				error = nfscl_postop_attr(nd, nap, attrflagp,
30596ca35587Sdholland 				    stuff);
30606ca35587Sdholland 				if (error)
30616ca35587Sdholland 					goto nfsmout;
30626ca35587Sdholland 			}
30636ca35587Sdholland 		}
30646ca35587Sdholland 		mbuf_freem(nd->nd_mrep);
30656ca35587Sdholland 		nd->nd_mrep = NULL;
30666ca35587Sdholland 	}
30676ca35587Sdholland 	/*
30686ca35587Sdholland 	 * Fill last record, iff any, out to a multiple of DIRBLKSIZ
30696ca35587Sdholland 	 * by increasing d_reclen for the last record.
30706ca35587Sdholland 	 */
30716ca35587Sdholland 	if (blksiz > 0) {
30726ca35587Sdholland 		left = DIRBLKSIZ - blksiz;
30736ca35587Sdholland 		dp->d_reclen += left;
30746ca35587Sdholland 		uio_iov_base_add(uiop, left);
30756ca35587Sdholland 		uio_iov_len_add(uiop, -(left));
30766ca35587Sdholland 		uio_uio_resid_add(uiop, -(left));
30776ca35587Sdholland 		uiop->uio_offset += left;
30786ca35587Sdholland 	}
30796ca35587Sdholland 
30806ca35587Sdholland 	/*
30816ca35587Sdholland 	 * If returning no data, assume end of file.
30826ca35587Sdholland 	 * If not bigenough, return not end of file, since you aren't
30836ca35587Sdholland 	 *    returning all the data
30846ca35587Sdholland 	 * Otherwise, return the eof flag from the server.
30856ca35587Sdholland 	 */
30866ca35587Sdholland 	if (eofp) {
30876ca35587Sdholland 		if (tresid == ((size_t)(uio_uio_resid(uiop))))
30886ca35587Sdholland 			*eofp = 1;
30896ca35587Sdholland 		else if (!bigenough)
30906ca35587Sdholland 			*eofp = 0;
30916ca35587Sdholland 		else
30926ca35587Sdholland 			*eofp = eof;
30936ca35587Sdholland 	}
30946ca35587Sdholland 
30956ca35587Sdholland 	/*
30966ca35587Sdholland 	 * Add extra empty records to any remaining DIRBLKSIZ chunks.
30976ca35587Sdholland 	 */
30986ca35587Sdholland 	while (uio_uio_resid(uiop) > 0 && ((size_t)(uio_uio_resid(uiop))) != tresid) {
30996ca35587Sdholland 		dp = (struct dirent *) CAST_DOWN(caddr_t, uio_iov_base(uiop));
31006ca35587Sdholland 		dp->d_type = DT_UNKNOWN;
31016ca35587Sdholland 		dp->d_fileno = 0;
31026ca35587Sdholland 		dp->d_namlen = 0;
31036ca35587Sdholland 		dp->d_name[0] = '\0';
31046ca35587Sdholland 		tl = (u_int32_t *)&dp->d_name[4];
31056ca35587Sdholland 		*tl++ = cookie.lval[0];
31066ca35587Sdholland 		*tl = cookie.lval[1];
31076ca35587Sdholland 		dp->d_reclen = DIRBLKSIZ;
31086ca35587Sdholland 		uio_iov_base_add(uiop, DIRBLKSIZ);
31096ca35587Sdholland 		uio_iov_len_add(uiop, -(DIRBLKSIZ));
31106ca35587Sdholland 		uio_uio_resid_add(uiop, -(DIRBLKSIZ));
31116ca35587Sdholland 		uiop->uio_offset += DIRBLKSIZ;
31126ca35587Sdholland 	}
31136ca35587Sdholland 
31146ca35587Sdholland nfsmout:
31156ca35587Sdholland 	mbuf_freem(nd->nd_mrep);
31166ca35587Sdholland 	return (error);
31176ca35587Sdholland }
31186ca35587Sdholland 
31196ca35587Sdholland #ifndef APPLE
31206ca35587Sdholland /*
31216ca35587Sdholland  * NFS V3 readdir plus RPC. Used in place of nfsrpc_readdir().
31226ca35587Sdholland  * (Also used for NFS V4 when mount flag set.)
31236ca35587Sdholland  * (ditto above w.r.t. multiple of DIRBLKSIZ, etc.)
31246ca35587Sdholland  */
31256ca35587Sdholland APPLESTATIC int
nfsrpc_readdirplus(vnode_t vp,struct uio * uiop,nfsuint64 * cookiep,struct ucred * cred,NFSPROC_T * p,struct nfsvattr * nap,int * attrflagp,int * eofp,void * stuff)31266ca35587Sdholland nfsrpc_readdirplus(vnode_t vp, struct uio *uiop, nfsuint64 *cookiep,
31276ca35587Sdholland     struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp,
31286ca35587Sdholland     int *eofp, void *stuff)
31296ca35587Sdholland {
31306ca35587Sdholland 	int len, left;
31316ca35587Sdholland 	struct dirent *dp = NULL;
31326ca35587Sdholland 	u_int32_t *tl;
31336ca35587Sdholland 	vnode_t newvp = NULLVP;
31346ca35587Sdholland 	struct nfsrv_descript nfsd, *nd = &nfsd;
31356ca35587Sdholland 	struct nameidata nami, *ndp = &nami;
31366ca35587Sdholland 	struct componentname *cnp = &ndp->ni_cnd;
31376ca35587Sdholland 	struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
31386ca35587Sdholland 	struct nfsnode *dnp = VTONFS(vp), *np;
31396ca35587Sdholland 	struct nfsvattr nfsva;
31406ca35587Sdholland 	struct nfsfh *nfhp;
31416ca35587Sdholland 	nfsquad_t cookie, ncookie;
31426ca35587Sdholland 	int error = 0, tlen, more_dirs = 1, blksiz = 0, bigenough = 1;
31436ca35587Sdholland 	int attrflag, tryformoredirs = 1, eof = 0, gotmnton = 0;
31446ca35587Sdholland 	int isdotdot = 0, unlocknewvp = 0;
31456ca35587Sdholland 	long dotfileid, dotdotfileid = 0, fileno = 0;
31466ca35587Sdholland 	char *cp;
31476ca35587Sdholland 	nfsattrbit_t attrbits, dattrbits;
31486ca35587Sdholland 	size_t tresid;
31496ca35587Sdholland 	u_int32_t *tl2 = NULL, fakefileno = 0xffffffff, rderr;
31506ca35587Sdholland 	struct timespec dctime;
31516ca35587Sdholland 
31526ca35587Sdholland 	KASSERT(uiop->uio_iovcnt == 1 &&
31536ca35587Sdholland 	    (uio_uio_resid(uiop) & (DIRBLKSIZ - 1)) == 0,
31546ca35587Sdholland 	    ("nfs readdirplusrpc bad uio"));
31556ca35587Sdholland 	timespecclear(&dctime);
31566ca35587Sdholland 	*attrflagp = 0;
31576ca35587Sdholland 	if (eofp != NULL)
31586ca35587Sdholland 		*eofp = 0;
31596ca35587Sdholland 	ndp->ni_dvp = vp;
31606ca35587Sdholland 	nd->nd_mrep = NULL;
31616ca35587Sdholland 	cookie.lval[0] = cookiep->nfsuquad[0];
31626ca35587Sdholland 	cookie.lval[1] = cookiep->nfsuquad[1];
31636ca35587Sdholland 	tresid = uio_uio_resid(uiop);
31646ca35587Sdholland 
31656ca35587Sdholland 	/*
31666ca35587Sdholland 	 * For NFSv4, first create the "." and ".." entries.
31676ca35587Sdholland 	 */
31686ca35587Sdholland 	if (NFSHASNFSV4(nmp)) {
31696ca35587Sdholland 		NFSGETATTR_ATTRBIT(&dattrbits);
31706ca35587Sdholland 		NFSZERO_ATTRBIT(&attrbits);
31716ca35587Sdholland 		NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_FILEID);
31726ca35587Sdholland 		if (NFSISSET_ATTRBIT(&dnp->n_vattr.na_suppattr,
31736ca35587Sdholland 		    NFSATTRBIT_MOUNTEDONFILEID)) {
31746ca35587Sdholland 			NFSSETBIT_ATTRBIT(&attrbits,
31756ca35587Sdholland 			    NFSATTRBIT_MOUNTEDONFILEID);
31766ca35587Sdholland 			gotmnton = 1;
31776ca35587Sdholland 		} else {
31786ca35587Sdholland 			/*
31796ca35587Sdholland 			 * Must fake it. Use the fileno, except when the
31806ca35587Sdholland 			 * fsid is != to that of the directory. For that
31816ca35587Sdholland 			 * case, generate a fake fileno that is not the same.
31826ca35587Sdholland 			 */
31836ca35587Sdholland 			NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_FSID);
31846ca35587Sdholland 			gotmnton = 0;
31856ca35587Sdholland 		}
31866ca35587Sdholland 
31876ca35587Sdholland 		/*
31886ca35587Sdholland 		 * Joy, oh joy. For V4 we get to hand craft '.' and '..'.
31896ca35587Sdholland 		 */
31906ca35587Sdholland 		if (uiop->uio_offset == 0) {
31916ca35587Sdholland 			NFSCL_REQSTART(nd, NFSPROC_LOOKUPP, vp);
31926ca35587Sdholland 			NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
31936ca35587Sdholland 			*tl++ = txdr_unsigned(NFSV4OP_GETFH);
31946ca35587Sdholland 			*tl = txdr_unsigned(NFSV4OP_GETATTR);
31956ca35587Sdholland 			(void) nfsrv_putattrbit(nd, &attrbits);
31966ca35587Sdholland 			error = nfscl_request(nd, vp, p, cred, stuff);
31976ca35587Sdholland 			if (error)
31986ca35587Sdholland 			    return (error);
3199e81f0ea2Spgoyette 			dotfileid = 0;	/* Fake out the compiler. */
3200e81f0ea2Spgoyette 			if ((nd->nd_flag & ND_NOMOREDATA) == 0) {
3201e81f0ea2Spgoyette 			    error = nfsm_loadattr(nd, &nfsva);
3202e81f0ea2Spgoyette 			    if (error != 0)
3203e81f0ea2Spgoyette 				goto nfsmout;
3204e81f0ea2Spgoyette 			    dctime = nfsva.na_ctime;
3205e81f0ea2Spgoyette 			    dotfileid = nfsva.na_fileid;
3206e81f0ea2Spgoyette 			}
32076ca35587Sdholland 			if (nd->nd_repstat == 0) {
3208e81f0ea2Spgoyette 			    NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
3209e81f0ea2Spgoyette 			    len = fxdr_unsigned(int, *(tl + 4));
32106ca35587Sdholland 			    if (len > 0 && len <= NFSX_V4FHMAX)
32116ca35587Sdholland 				error = nfsm_advance(nd, NFSM_RNDUP(len), -1);
32126ca35587Sdholland 			    else
32136ca35587Sdholland 				error = EPERM;
32146ca35587Sdholland 			    if (!error) {
32156ca35587Sdholland 				NFSM_DISSECT(tl, u_int32_t *, 2*NFSX_UNSIGNED);
32166ca35587Sdholland 				nfsva.na_mntonfileno = 0xffffffff;
32176ca35587Sdholland 				error = nfsv4_loadattr(nd, NULL, &nfsva, NULL,
32186ca35587Sdholland 				    NULL, 0, NULL, NULL, NULL, NULL, NULL, 0,
32196ca35587Sdholland 				    NULL, NULL, NULL, p, cred);
32206ca35587Sdholland 				if (error) {
32216ca35587Sdholland 				    dotdotfileid = dotfileid;
32226ca35587Sdholland 				} else if (gotmnton) {
32236ca35587Sdholland 				    if (nfsva.na_mntonfileno != 0xffffffff)
32246ca35587Sdholland 					dotdotfileid = nfsva.na_mntonfileno;
32256ca35587Sdholland 				    else
32266ca35587Sdholland 					dotdotfileid = nfsva.na_fileid;
32276ca35587Sdholland 				} else if (nfsva.na_filesid[0] ==
32286ca35587Sdholland 				    dnp->n_vattr.na_filesid[0] &&
32296ca35587Sdholland 				    nfsva.na_filesid[1] ==
32306ca35587Sdholland 				    dnp->n_vattr.na_filesid[1]) {
32316ca35587Sdholland 				    dotdotfileid = nfsva.na_fileid;
32326ca35587Sdholland 				} else {
32336ca35587Sdholland 				    do {
32346ca35587Sdholland 					fakefileno--;
32356ca35587Sdholland 				    } while (fakefileno ==
32366ca35587Sdholland 					nfsva.na_fileid);
32376ca35587Sdholland 				    dotdotfileid = fakefileno;
32386ca35587Sdholland 				}
32396ca35587Sdholland 			    }
32406ca35587Sdholland 			} else if (nd->nd_repstat == NFSERR_NOENT) {
32416ca35587Sdholland 			    /*
32426ca35587Sdholland 			     * Lookupp returns NFSERR_NOENT when we are
32436ca35587Sdholland 			     * at the root, so just use the current dir.
32446ca35587Sdholland 			     */
32456ca35587Sdholland 			    nd->nd_repstat = 0;
32466ca35587Sdholland 			    dotdotfileid = dotfileid;
32476ca35587Sdholland 			} else {
32486ca35587Sdholland 			    error = nd->nd_repstat;
32496ca35587Sdholland 			}
32506ca35587Sdholland 			mbuf_freem(nd->nd_mrep);
32516ca35587Sdholland 			if (error)
32526ca35587Sdholland 			    return (error);
32536ca35587Sdholland 			nd->nd_mrep = NULL;
32546ca35587Sdholland 			dp = (struct dirent *)uio_iov_base(uiop);
32556ca35587Sdholland 			dp->d_type = DT_DIR;
32566ca35587Sdholland 			dp->d_fileno = dotfileid;
32576ca35587Sdholland 			dp->d_namlen = 1;
32586ca35587Sdholland 			dp->d_name[0] = '.';
32596ca35587Sdholland 			dp->d_name[1] = '\0';
32606ca35587Sdholland 			dp->d_reclen = DIRENT_SIZE(dp) + NFSX_HYPER;
32616ca35587Sdholland 			/*
32626ca35587Sdholland 			 * Just make these offset cookie 0.
32636ca35587Sdholland 			 */
32646ca35587Sdholland 			tl = (u_int32_t *)&dp->d_name[4];
32656ca35587Sdholland 			*tl++ = 0;
32666ca35587Sdholland 			*tl = 0;
32676ca35587Sdholland 			blksiz += dp->d_reclen;
32686ca35587Sdholland 			uio_uio_resid_add(uiop, -(dp->d_reclen));
32696ca35587Sdholland 			uiop->uio_offset += dp->d_reclen;
32706ca35587Sdholland 			uio_iov_base_add(uiop, dp->d_reclen);
32716ca35587Sdholland 			uio_iov_len_add(uiop, -(dp->d_reclen));
32726ca35587Sdholland 			dp = (struct dirent *)uio_iov_base(uiop);
32736ca35587Sdholland 			dp->d_type = DT_DIR;
32746ca35587Sdholland 			dp->d_fileno = dotdotfileid;
32756ca35587Sdholland 			dp->d_namlen = 2;
32766ca35587Sdholland 			dp->d_name[0] = '.';
32776ca35587Sdholland 			dp->d_name[1] = '.';
32786ca35587Sdholland 			dp->d_name[2] = '\0';
32796ca35587Sdholland 			dp->d_reclen = DIRENT_SIZE(dp) + NFSX_HYPER;
32806ca35587Sdholland 			/*
32816ca35587Sdholland 			 * Just make these offset cookie 0.
32826ca35587Sdholland 			 */
32836ca35587Sdholland 			tl = (u_int32_t *)&dp->d_name[4];
32846ca35587Sdholland 			*tl++ = 0;
32856ca35587Sdholland 			*tl = 0;
32866ca35587Sdholland 			blksiz += dp->d_reclen;
32876ca35587Sdholland 			uio_uio_resid_add(uiop, -(dp->d_reclen));
32886ca35587Sdholland 			uiop->uio_offset += dp->d_reclen;
32896ca35587Sdholland 			uio_iov_base_add(uiop, dp->d_reclen);
32906ca35587Sdholland 			uio_iov_len_add(uiop, -(dp->d_reclen));
32916ca35587Sdholland 		}
32926ca35587Sdholland 		NFSREADDIRPLUS_ATTRBIT(&attrbits);
32936ca35587Sdholland 		if (gotmnton)
32946ca35587Sdholland 			NFSSETBIT_ATTRBIT(&attrbits,
32956ca35587Sdholland 			    NFSATTRBIT_MOUNTEDONFILEID);
32966ca35587Sdholland 	}
32976ca35587Sdholland 
32986ca35587Sdholland 	/*
32996ca35587Sdholland 	 * Loop around doing readdir rpc's of size nm_readdirsize.
33006ca35587Sdholland 	 * The stopping criteria is EOF or buffer full.
33016ca35587Sdholland 	 */
33026ca35587Sdholland 	while (more_dirs && bigenough) {
33036ca35587Sdholland 		*attrflagp = 0;
33046ca35587Sdholland 		NFSCL_REQSTART(nd, NFSPROC_READDIRPLUS, vp);
33056ca35587Sdholland  		NFSM_BUILD(tl, u_int32_t *, 6 * NFSX_UNSIGNED);
33066ca35587Sdholland 		*tl++ = cookie.lval[0];
33076ca35587Sdholland 		*tl++ = cookie.lval[1];
33086ca35587Sdholland 		if (cookie.qval == 0) {
33096ca35587Sdholland 			*tl++ = 0;
33106ca35587Sdholland 			*tl++ = 0;
33116ca35587Sdholland 		} else {
33126ca35587Sdholland 			NFSLOCKNODE(dnp);
33136ca35587Sdholland 			*tl++ = dnp->n_cookieverf.nfsuquad[0];
33146ca35587Sdholland 			*tl++ = dnp->n_cookieverf.nfsuquad[1];
33156ca35587Sdholland 			NFSUNLOCKNODE(dnp);
33166ca35587Sdholland 		}
33176ca35587Sdholland 		*tl++ = txdr_unsigned(nmp->nm_readdirsize);
33186ca35587Sdholland 		*tl = txdr_unsigned(nmp->nm_readdirsize);
33196ca35587Sdholland 		if (nd->nd_flag & ND_NFSV4) {
33206ca35587Sdholland 			(void) nfsrv_putattrbit(nd, &attrbits);
33216ca35587Sdholland 			NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
33226ca35587Sdholland 			*tl = txdr_unsigned(NFSV4OP_GETATTR);
33236ca35587Sdholland 			(void) nfsrv_putattrbit(nd, &dattrbits);
33246ca35587Sdholland 		}
33256ca35587Sdholland 		error = nfscl_request(nd, vp, p, cred, stuff);
33266ca35587Sdholland 		if (error)
33276ca35587Sdholland 			return (error);
33286ca35587Sdholland 		if (nd->nd_flag & ND_NFSV3)
33296ca35587Sdholland 			error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
33306ca35587Sdholland 		if (nd->nd_repstat || error) {
33316ca35587Sdholland 			if (!error)
33326ca35587Sdholland 				error = nd->nd_repstat;
33336ca35587Sdholland 			goto nfsmout;
33346ca35587Sdholland 		}
33356ca35587Sdholland 		if ((nd->nd_flag & ND_NFSV3) != 0 && *attrflagp != 0)
33366ca35587Sdholland 			dctime = nap->na_ctime;
33376ca35587Sdholland 		NFSM_DISSECT(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
33386ca35587Sdholland 		NFSLOCKNODE(dnp);
33396ca35587Sdholland 		dnp->n_cookieverf.nfsuquad[0] = *tl++;
33406ca35587Sdholland 		dnp->n_cookieverf.nfsuquad[1] = *tl++;
33416ca35587Sdholland 		NFSUNLOCKNODE(dnp);
33426ca35587Sdholland 		more_dirs = fxdr_unsigned(int, *tl);
33436ca35587Sdholland 		if (!more_dirs)
33446ca35587Sdholland 			tryformoredirs = 0;
33456ca35587Sdholland 
3346e81f0ea2Spgoyette 		/* loop through the dir entries, doctoring them to 4bsd form */
33476ca35587Sdholland 		while (more_dirs && bigenough) {
33486ca35587Sdholland 			NFSM_DISSECT(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
33496ca35587Sdholland 			if (nd->nd_flag & ND_NFSV4) {
33506ca35587Sdholland 				ncookie.lval[0] = *tl++;
33516ca35587Sdholland 				ncookie.lval[1] = *tl++;
33526ca35587Sdholland 			} else {
33536ca35587Sdholland 				fileno = fxdr_unsigned(long, *++tl);
33546ca35587Sdholland 				tl++;
33556ca35587Sdholland 			}
33566ca35587Sdholland 			len = fxdr_unsigned(int, *tl);
33576ca35587Sdholland 			if (len <= 0 || len > NFS_MAXNAMLEN) {
33586ca35587Sdholland 				error = EBADRPC;
33596ca35587Sdholland 				goto nfsmout;
33606ca35587Sdholland 			}
33616ca35587Sdholland 			tlen = NFSM_RNDUP(len);
33626ca35587Sdholland 			if (tlen == len)
33636ca35587Sdholland 				tlen += 4;  /* To ensure null termination */
33646ca35587Sdholland 			left = DIRBLKSIZ - blksiz;
33656ca35587Sdholland 			if ((tlen + DIRHDSIZ + NFSX_HYPER) > left) {
33666ca35587Sdholland 				dp->d_reclen += left;
33676ca35587Sdholland 				uio_iov_base_add(uiop, left);
33686ca35587Sdholland 				uio_iov_len_add(uiop, -(left));
33696ca35587Sdholland 				uio_uio_resid_add(uiop, -(left));
33706ca35587Sdholland 				uiop->uio_offset += left;
33716ca35587Sdholland 				blksiz = 0;
33726ca35587Sdholland 			}
33736ca35587Sdholland 			if ((tlen + DIRHDSIZ + NFSX_HYPER) > uio_uio_resid(uiop))
33746ca35587Sdholland 				bigenough = 0;
33756ca35587Sdholland 			if (bigenough) {
33766ca35587Sdholland 				dp = (struct dirent *)uio_iov_base(uiop);
33776ca35587Sdholland 				dp->d_namlen = len;
33786ca35587Sdholland 				dp->d_reclen = tlen + DIRHDSIZ + NFSX_HYPER;
33796ca35587Sdholland 				dp->d_type = DT_UNKNOWN;
33806ca35587Sdholland 				blksiz += dp->d_reclen;
33816ca35587Sdholland 				if (blksiz == DIRBLKSIZ)
33826ca35587Sdholland 					blksiz = 0;
33836ca35587Sdholland 				uio_uio_resid_add(uiop, -(DIRHDSIZ));
33846ca35587Sdholland 				uiop->uio_offset += DIRHDSIZ;
33856ca35587Sdholland 				uio_iov_base_add(uiop, DIRHDSIZ);
33866ca35587Sdholland 				uio_iov_len_add(uiop, -(DIRHDSIZ));
33876ca35587Sdholland 				cnp->cn_nameptr = uio_iov_base(uiop);
33886ca35587Sdholland 				cnp->cn_namelen = len;
33896ca35587Sdholland 				NFSCNHASHZERO(cnp);
33906ca35587Sdholland 				error = nfsm_mbufuio(nd, uiop, len);
33916ca35587Sdholland 				if (error)
33926ca35587Sdholland 					goto nfsmout;
33936ca35587Sdholland 				cp = uio_iov_base(uiop);
33946ca35587Sdholland 				tlen -= len;
33956ca35587Sdholland 				*cp = '\0';
33966ca35587Sdholland 				cp += tlen;	/* points to cookie storage */
33976ca35587Sdholland 				tl2 = (u_int32_t *)cp;
33986ca35587Sdholland 				if (len == 2 && cnp->cn_nameptr[0] == '.' &&
33996ca35587Sdholland 				    cnp->cn_nameptr[1] == '.')
34006ca35587Sdholland 					isdotdot = 1;
34016ca35587Sdholland 				else
34026ca35587Sdholland 					isdotdot = 0;
34036ca35587Sdholland 				uio_iov_base_add(uiop, (tlen + NFSX_HYPER));
34046ca35587Sdholland 				uio_iov_len_add(uiop, -(tlen + NFSX_HYPER));
34056ca35587Sdholland 				uio_uio_resid_add(uiop, -(tlen + NFSX_HYPER));
34066ca35587Sdholland 				uiop->uio_offset += (tlen + NFSX_HYPER);
34076ca35587Sdholland 			} else {
34086ca35587Sdholland 				error = nfsm_advance(nd, NFSM_RNDUP(len), -1);
34096ca35587Sdholland 				if (error)
34106ca35587Sdholland 					goto nfsmout;
34116ca35587Sdholland 			}
34126ca35587Sdholland 			nfhp = NULL;
34136ca35587Sdholland 			if (nd->nd_flag & ND_NFSV3) {
34146ca35587Sdholland 				NFSM_DISSECT(tl, u_int32_t *, 3*NFSX_UNSIGNED);
34156ca35587Sdholland 				ncookie.lval[0] = *tl++;
34166ca35587Sdholland 				ncookie.lval[1] = *tl++;
34176ca35587Sdholland 				attrflag = fxdr_unsigned(int, *tl);
34186ca35587Sdholland 				if (attrflag) {
34196ca35587Sdholland 				  error = nfsm_loadattr(nd, &nfsva);
34206ca35587Sdholland 				  if (error)
34216ca35587Sdholland 					goto nfsmout;
34226ca35587Sdholland 				}
34236ca35587Sdholland 				NFSM_DISSECT(tl,u_int32_t *,NFSX_UNSIGNED);
34246ca35587Sdholland 				if (*tl) {
34256ca35587Sdholland 					error = nfsm_getfh(nd, &nfhp);
34266ca35587Sdholland 					if (error)
34276ca35587Sdholland 					    goto nfsmout;
34286ca35587Sdholland 				}
34296ca35587Sdholland 				if (!attrflag && nfhp != NULL) {
34306ca35587Sdholland 					FREE((caddr_t)nfhp, M_NFSFH);
34316ca35587Sdholland 					nfhp = NULL;
34326ca35587Sdholland 				}
34336ca35587Sdholland 			} else {
34346ca35587Sdholland 				rderr = 0;
34356ca35587Sdholland 				nfsva.na_mntonfileno = 0xffffffff;
34366ca35587Sdholland 				error = nfsv4_loadattr(nd, NULL, &nfsva, &nfhp,
34376ca35587Sdholland 				    NULL, 0, NULL, NULL, NULL, NULL, NULL, 0,
34386ca35587Sdholland 				    NULL, NULL, &rderr, p, cred);
34396ca35587Sdholland 				if (error)
34406ca35587Sdholland 					goto nfsmout;
34416ca35587Sdholland 			}
34426ca35587Sdholland 
34436ca35587Sdholland 			if (bigenough) {
34446ca35587Sdholland 			    if (nd->nd_flag & ND_NFSV4) {
34456ca35587Sdholland 				if (rderr) {
34466ca35587Sdholland 				    dp->d_fileno = 0;
34476ca35587Sdholland 				} else if (gotmnton) {
34486ca35587Sdholland 				    if (nfsva.na_mntonfileno != 0xffffffff)
34496ca35587Sdholland 					dp->d_fileno = nfsva.na_mntonfileno;
34506ca35587Sdholland 				    else
34516ca35587Sdholland 					dp->d_fileno = nfsva.na_fileid;
34526ca35587Sdholland 				} else if (nfsva.na_filesid[0] ==
34536ca35587Sdholland 				    dnp->n_vattr.na_filesid[0] &&
34546ca35587Sdholland 				    nfsva.na_filesid[1] ==
34556ca35587Sdholland 				    dnp->n_vattr.na_filesid[1]) {
34566ca35587Sdholland 				    dp->d_fileno = nfsva.na_fileid;
34576ca35587Sdholland 				} else {
34586ca35587Sdholland 				    do {
34596ca35587Sdholland 					fakefileno--;
34606ca35587Sdholland 				    } while (fakefileno ==
34616ca35587Sdholland 					nfsva.na_fileid);
34626ca35587Sdholland 				    dp->d_fileno = fakefileno;
34636ca35587Sdholland 				}
34646ca35587Sdholland 			    } else {
34656ca35587Sdholland 				dp->d_fileno = fileno;
34666ca35587Sdholland 			    }
34676ca35587Sdholland 			    *tl2++ = cookiep->nfsuquad[0] = cookie.lval[0] =
34686ca35587Sdholland 				ncookie.lval[0];
34696ca35587Sdholland 			    *tl2 = cookiep->nfsuquad[1] = cookie.lval[1] =
34706ca35587Sdholland 				ncookie.lval[1];
34716ca35587Sdholland 
34726ca35587Sdholland 			    if (nfhp != NULL) {
34736ca35587Sdholland 				if (NFSRV_CMPFH(nfhp->nfh_fh, nfhp->nfh_len,
34746ca35587Sdholland 				    dnp->n_fhp->nfh_fh, dnp->n_fhp->nfh_len)) {
34756ca35587Sdholland 				    VREF(vp);
34766ca35587Sdholland 				    newvp = vp;
34776ca35587Sdholland 				    unlocknewvp = 0;
34786ca35587Sdholland 				    FREE((caddr_t)nfhp, M_NFSFH);
34796ca35587Sdholland 				    np = dnp;
34806ca35587Sdholland 				} else if (isdotdot != 0) {
34816ca35587Sdholland 				    /*
34826ca35587Sdholland 				     * Skip doing a nfscl_nget() call for "..".
34836ca35587Sdholland 				     * There's a race between acquiring the nfs
34846ca35587Sdholland 				     * node here and lookups that look for the
34856ca35587Sdholland 				     * directory being read (in the parent).
34866ca35587Sdholland 				     * It would try to get a lock on ".." here,
34876ca35587Sdholland 				     * owning the lock on the directory being
34886ca35587Sdholland 				     * read. Lookup will hold the lock on ".."
34896ca35587Sdholland 				     * and try to acquire the lock on the
34906ca35587Sdholland 				     * directory being read.
34916ca35587Sdholland 				     * If the directory is unlocked/relocked,
34926ca35587Sdholland 				     * then there is a LOR with the buflock
34936ca35587Sdholland 				     * vp is relocked.
34946ca35587Sdholland 				     */
34956ca35587Sdholland 				    free(nfhp, M_NFSFH);
34966ca35587Sdholland 				} else {
34976ca35587Sdholland 				    error = nfscl_nget(vnode_mount(vp), vp,
34986ca35587Sdholland 				      nfhp, cnp, p, &np, NULL, LK_EXCLUSIVE);
34996ca35587Sdholland 				    if (!error) {
35006ca35587Sdholland 					newvp = NFSTOV(np);
35016ca35587Sdholland 					unlocknewvp = 1;
35026ca35587Sdholland 				    }
35036ca35587Sdholland 				}
35046ca35587Sdholland 				nfhp = NULL;
35056ca35587Sdholland 				if (newvp != NULLVP) {
35066ca35587Sdholland 				    error = nfscl_loadattrcache(&newvp,
35076ca35587Sdholland 					&nfsva, NULL, NULL, 0, 0);
35086ca35587Sdholland 				    if (error) {
35096ca35587Sdholland 					if (unlocknewvp)
35106ca35587Sdholland 					    vput(newvp);
35116ca35587Sdholland 					else
35126ca35587Sdholland 					    vrele(newvp);
35136ca35587Sdholland 					goto nfsmout;
35146ca35587Sdholland 				    }
35156ca35587Sdholland 				    dp->d_type =
35166ca35587Sdholland 					vtonfs_dtype(np->n_vattr.na_type);
35176ca35587Sdholland 				    ndp->ni_vp = newvp;
35186ca35587Sdholland 				    NFSCNHASH(cnp, HASHINIT);
35196ca35587Sdholland 				    if (cnp->cn_namelen <= NCHNAMLEN &&
35206ca35587Sdholland 					(newvp->v_type != VDIR ||
35216ca35587Sdholland 					 dctime.tv_sec != 0)) {
35226ca35587Sdholland 					cache_enter_time(ndp->ni_dvp,
35236ca35587Sdholland 					    ndp->ni_vp, cnp,
35246ca35587Sdholland 					    &nfsva.na_ctime,
35256ca35587Sdholland 					    newvp->v_type != VDIR ? NULL :
35266ca35587Sdholland 					    &dctime);
35276ca35587Sdholland 				    }
35286ca35587Sdholland 				    if (unlocknewvp)
35296ca35587Sdholland 					vput(newvp);
35306ca35587Sdholland 				    else
35316ca35587Sdholland 					vrele(newvp);
35326ca35587Sdholland 				    newvp = NULLVP;
35336ca35587Sdholland 				}
35346ca35587Sdholland 			    }
35356ca35587Sdholland 			} else if (nfhp != NULL) {
35366ca35587Sdholland 			    FREE((caddr_t)nfhp, M_NFSFH);
35376ca35587Sdholland 			}
35386ca35587Sdholland 			NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
35396ca35587Sdholland 			more_dirs = fxdr_unsigned(int, *tl);
35406ca35587Sdholland 		}
35416ca35587Sdholland 		/*
35426ca35587Sdholland 		 * If at end of rpc data, get the eof boolean
35436ca35587Sdholland 		 */
35446ca35587Sdholland 		if (!more_dirs) {
35456ca35587Sdholland 			NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
35466ca35587Sdholland 			eof = fxdr_unsigned(int, *tl);
35476ca35587Sdholland 			if (tryformoredirs)
35486ca35587Sdholland 				more_dirs = !eof;
35496ca35587Sdholland 			if (nd->nd_flag & ND_NFSV4) {
35506ca35587Sdholland 				error = nfscl_postop_attr(nd, nap, attrflagp,
35516ca35587Sdholland 				    stuff);
35526ca35587Sdholland 				if (error)
35536ca35587Sdholland 					goto nfsmout;
35546ca35587Sdholland 			}
35556ca35587Sdholland 		}
35566ca35587Sdholland 		mbuf_freem(nd->nd_mrep);
35576ca35587Sdholland 		nd->nd_mrep = NULL;
35586ca35587Sdholland 	}
35596ca35587Sdholland 	/*
35606ca35587Sdholland 	 * Fill last record, iff any, out to a multiple of DIRBLKSIZ
35616ca35587Sdholland 	 * by increasing d_reclen for the last record.
35626ca35587Sdholland 	 */
35636ca35587Sdholland 	if (blksiz > 0) {
35646ca35587Sdholland 		left = DIRBLKSIZ - blksiz;
35656ca35587Sdholland 		dp->d_reclen += left;
35666ca35587Sdholland 		uio_iov_base_add(uiop, left);
35676ca35587Sdholland 		uio_iov_len_add(uiop, -(left));
35686ca35587Sdholland 		uio_uio_resid_add(uiop, -(left));
35696ca35587Sdholland 		uiop->uio_offset += left;
35706ca35587Sdholland 	}
35716ca35587Sdholland 
35726ca35587Sdholland 	/*
35736ca35587Sdholland 	 * If returning no data, assume end of file.
35746ca35587Sdholland 	 * If not bigenough, return not end of file, since you aren't
35756ca35587Sdholland 	 *    returning all the data
35766ca35587Sdholland 	 * Otherwise, return the eof flag from the server.
35776ca35587Sdholland 	 */
35786ca35587Sdholland 	if (eofp != NULL) {
35796ca35587Sdholland 		if (tresid == uio_uio_resid(uiop))
35806ca35587Sdholland 			*eofp = 1;
35816ca35587Sdholland 		else if (!bigenough)
35826ca35587Sdholland 			*eofp = 0;
35836ca35587Sdholland 		else
35846ca35587Sdholland 			*eofp = eof;
35856ca35587Sdholland 	}
35866ca35587Sdholland 
35876ca35587Sdholland 	/*
35886ca35587Sdholland 	 * Add extra empty records to any remaining DIRBLKSIZ chunks.
35896ca35587Sdholland 	 */
35906ca35587Sdholland 	while (uio_uio_resid(uiop) > 0 && uio_uio_resid(uiop) != tresid) {
35916ca35587Sdholland 		dp = (struct dirent *)uio_iov_base(uiop);
35926ca35587Sdholland 		dp->d_type = DT_UNKNOWN;
35936ca35587Sdholland 		dp->d_fileno = 0;
35946ca35587Sdholland 		dp->d_namlen = 0;
35956ca35587Sdholland 		dp->d_name[0] = '\0';
35966ca35587Sdholland 		tl = (u_int32_t *)&dp->d_name[4];
35976ca35587Sdholland 		*tl++ = cookie.lval[0];
35986ca35587Sdholland 		*tl = cookie.lval[1];
35996ca35587Sdholland 		dp->d_reclen = DIRBLKSIZ;
36006ca35587Sdholland 		uio_iov_base_add(uiop, DIRBLKSIZ);
36016ca35587Sdholland 		uio_iov_len_add(uiop, -(DIRBLKSIZ));
36026ca35587Sdholland 		uio_uio_resid_add(uiop, -(DIRBLKSIZ));
36036ca35587Sdholland 		uiop->uio_offset += DIRBLKSIZ;
36046ca35587Sdholland 	}
36056ca35587Sdholland 
36066ca35587Sdholland nfsmout:
36076ca35587Sdholland 	mbuf_freem(nd->nd_mrep);
36086ca35587Sdholland 	return (error);
36096ca35587Sdholland }
36106ca35587Sdholland #endif	/* !APPLE */
36116ca35587Sdholland 
36126ca35587Sdholland /*
36136ca35587Sdholland  * Nfs commit rpc
36146ca35587Sdholland  */
36156ca35587Sdholland APPLESTATIC int
nfsrpc_commit(vnode_t vp,u_quad_t offset,int cnt,struct ucred * cred,NFSPROC_T * p,struct nfsvattr * nap,int * attrflagp,void * stuff)36166ca35587Sdholland nfsrpc_commit(vnode_t vp, u_quad_t offset, int cnt, struct ucred *cred,
36176ca35587Sdholland     NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, void *stuff)
36186ca35587Sdholland {
36196ca35587Sdholland 	u_int32_t *tl;
36206ca35587Sdholland 	struct nfsrv_descript nfsd, *nd = &nfsd;
36216ca35587Sdholland 	nfsattrbit_t attrbits;
36226ca35587Sdholland 	int error;
36236ca35587Sdholland 	struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
36246ca35587Sdholland 
36256ca35587Sdholland 	*attrflagp = 0;
36266ca35587Sdholland 	NFSCL_REQSTART(nd, NFSPROC_COMMIT, vp);
36276ca35587Sdholland 	NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
36286ca35587Sdholland 	txdr_hyper(offset, tl);
36296ca35587Sdholland 	tl += 2;
36306ca35587Sdholland 	*tl = txdr_unsigned(cnt);
36316ca35587Sdholland 	if (nd->nd_flag & ND_NFSV4) {
36326ca35587Sdholland 		/*
36336ca35587Sdholland 		 * And do a Getattr op.
36346ca35587Sdholland 		 */
36356ca35587Sdholland 		NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
36366ca35587Sdholland 		*tl = txdr_unsigned(NFSV4OP_GETATTR);
36376ca35587Sdholland 		NFSGETATTR_ATTRBIT(&attrbits);
36386ca35587Sdholland 		(void) nfsrv_putattrbit(nd, &attrbits);
36396ca35587Sdholland 	}
36406ca35587Sdholland 	error = nfscl_request(nd, vp, p, cred, stuff);
36416ca35587Sdholland 	if (error)
36426ca35587Sdholland 		return (error);
36436ca35587Sdholland 	error = nfscl_wcc_data(nd, vp, nap, attrflagp, NULL, stuff);
36446ca35587Sdholland 	if (!error && !nd->nd_repstat) {
36456ca35587Sdholland 		NFSM_DISSECT(tl, u_int32_t *, NFSX_VERF);
36466ca35587Sdholland 		NFSLOCKMNT(nmp);
36476ca35587Sdholland 		if (NFSBCMP(nmp->nm_verf, tl, NFSX_VERF)) {
36486ca35587Sdholland 			NFSBCOPY(tl, nmp->nm_verf, NFSX_VERF);
36496ca35587Sdholland 			nd->nd_repstat = NFSERR_STALEWRITEVERF;
36506ca35587Sdholland 		}
36516ca35587Sdholland 		NFSUNLOCKMNT(nmp);
36526ca35587Sdholland 		if (nd->nd_flag & ND_NFSV4)
36536ca35587Sdholland 			error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
36546ca35587Sdholland 	}
36556ca35587Sdholland nfsmout:
36566ca35587Sdholland 	if (!error && nd->nd_repstat)
36576ca35587Sdholland 		error = nd->nd_repstat;
36586ca35587Sdholland 	mbuf_freem(nd->nd_mrep);
36596ca35587Sdholland 	return (error);
36606ca35587Sdholland }
36616ca35587Sdholland 
36626ca35587Sdholland /*
36636ca35587Sdholland  * NFS byte range lock rpc.
36646ca35587Sdholland  * (Mostly just calls one of the three lower level RPC routines.)
36656ca35587Sdholland  */
36666ca35587Sdholland APPLESTATIC int
nfsrpc_advlock(vnode_t vp,off_t size,int op,struct flock * fl,int reclaim,struct ucred * cred,NFSPROC_T * p,void * id,int flags)36676ca35587Sdholland nfsrpc_advlock(vnode_t vp, off_t size, int op, struct flock *fl,
36686ca35587Sdholland     int reclaim, struct ucred *cred, NFSPROC_T *p, void *id, int flags)
36696ca35587Sdholland {
36706ca35587Sdholland 	struct nfscllockowner *lp;
36716ca35587Sdholland 	struct nfsclclient *clp;
36726ca35587Sdholland 	struct nfsfh *nfhp;
36736ca35587Sdholland 	struct nfsrv_descript nfsd, *nd = &nfsd;
36746ca35587Sdholland 	struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
36756ca35587Sdholland 	u_int64_t off, len;
36766ca35587Sdholland 	off_t start, end;
36776ca35587Sdholland 	u_int32_t clidrev = 0;
36786ca35587Sdholland 	int error = 0, newone = 0, expireret = 0, retrycnt, donelocally;
36796ca35587Sdholland 	int callcnt, dorpc;
36806ca35587Sdholland 
36816ca35587Sdholland 	/*
36826ca35587Sdholland 	 * Convert the flock structure into a start and end and do POSIX
36836ca35587Sdholland 	 * bounds checking.
36846ca35587Sdholland 	 */
36856ca35587Sdholland 	switch (fl->l_whence) {
36866ca35587Sdholland 	case SEEK_SET:
36876ca35587Sdholland 	case SEEK_CUR:
36886ca35587Sdholland 		/*
36896ca35587Sdholland 		 * Caller is responsible for adding any necessary offset
36906ca35587Sdholland 		 * when SEEK_CUR is used.
36916ca35587Sdholland 		 */
36926ca35587Sdholland 		start = fl->l_start;
36936ca35587Sdholland 		off = fl->l_start;
36946ca35587Sdholland 		break;
36956ca35587Sdholland 	case SEEK_END:
36966ca35587Sdholland 		start = size + fl->l_start;
36976ca35587Sdholland 		off = size + fl->l_start;
36986ca35587Sdholland 		break;
36996ca35587Sdholland 	default:
37006ca35587Sdholland 		return (EINVAL);
3701e81f0ea2Spgoyette 	}
37026ca35587Sdholland 	if (start < 0)
37036ca35587Sdholland 		return (EINVAL);
37046ca35587Sdholland 	if (fl->l_len != 0) {
37056ca35587Sdholland 		end = start + fl->l_len - 1;
37066ca35587Sdholland 		if (end < start)
37076ca35587Sdholland 			return (EINVAL);
37086ca35587Sdholland 	}
37096ca35587Sdholland 
37106ca35587Sdholland 	len = fl->l_len;
37116ca35587Sdholland 	if (len == 0)
37126ca35587Sdholland 		len = NFS64BITSSET;
37136ca35587Sdholland 	retrycnt = 0;
37146ca35587Sdholland 	do {
37156ca35587Sdholland 	    nd->nd_repstat = 0;
37166ca35587Sdholland 	    if (op == F_GETLK) {
37176ca35587Sdholland 		error = nfscl_getcl(vnode_mount(vp), cred, p, 1, &clp);
37186ca35587Sdholland 		if (error)
37196ca35587Sdholland 			return (error);
37206ca35587Sdholland 		error = nfscl_lockt(vp, clp, off, len, fl, p, id, flags);
37216ca35587Sdholland 		if (!error) {
37226ca35587Sdholland 			clidrev = clp->nfsc_clientidrev;
37236ca35587Sdholland 			error = nfsrpc_lockt(nd, vp, clp, off, len, fl, cred,
37246ca35587Sdholland 			    p, id, flags);
37256ca35587Sdholland 		} else if (error == -1) {
37266ca35587Sdholland 			error = 0;
37276ca35587Sdholland 		}
37286ca35587Sdholland 		nfscl_clientrelease(clp);
37296ca35587Sdholland 	    } else if (op == F_UNLCK && fl->l_type == F_UNLCK) {
37306ca35587Sdholland 		/*
37316ca35587Sdholland 		 * We must loop around for all lockowner cases.
37326ca35587Sdholland 		 */
37336ca35587Sdholland 		callcnt = 0;
37346ca35587Sdholland 		error = nfscl_getcl(vnode_mount(vp), cred, p, 1, &clp);
37356ca35587Sdholland 		if (error)
37366ca35587Sdholland 			return (error);
37376ca35587Sdholland 		do {
37386ca35587Sdholland 		    error = nfscl_relbytelock(vp, off, len, cred, p, callcnt,
37396ca35587Sdholland 			clp, id, flags, &lp, &dorpc);
37406ca35587Sdholland 		    /*
37416ca35587Sdholland 		     * If it returns a NULL lp, we're done.
37426ca35587Sdholland 		     */
37436ca35587Sdholland 		    if (lp == NULL) {
37446ca35587Sdholland 			if (callcnt == 0)
37456ca35587Sdholland 			    nfscl_clientrelease(clp);
37466ca35587Sdholland 			else
37476ca35587Sdholland 			    nfscl_releasealllocks(clp, vp, p, id, flags);
37486ca35587Sdholland 			return (error);
37496ca35587Sdholland 		    }
37506ca35587Sdholland 		    if (nmp->nm_clp != NULL)
37516ca35587Sdholland 			clidrev = nmp->nm_clp->nfsc_clientidrev;
37526ca35587Sdholland 		    else
37536ca35587Sdholland 			clidrev = 0;
37546ca35587Sdholland 		    /*
37556ca35587Sdholland 		     * If the server doesn't support Posix lock semantics,
37566ca35587Sdholland 		     * only allow locks on the entire file, since it won't
37576ca35587Sdholland 		     * handle overlapping byte ranges.
37586ca35587Sdholland 		     * There might still be a problem when a lock
37596ca35587Sdholland 		     * upgrade/downgrade (read<->write) occurs, since the
37606ca35587Sdholland 		     * server "might" expect an unlock first?
37616ca35587Sdholland 		     */
37626ca35587Sdholland 		    if (dorpc && (lp->nfsl_open->nfso_posixlock ||
37636ca35587Sdholland 			(off == 0 && len == NFS64BITSSET))) {
37646ca35587Sdholland 			/*
37656ca35587Sdholland 			 * Since the lock records will go away, we must
37666ca35587Sdholland 			 * wait for grace and delay here.
37676ca35587Sdholland 			 */
37686ca35587Sdholland 			do {
37696ca35587Sdholland 			    error = nfsrpc_locku(nd, nmp, lp, off, len,
37706ca35587Sdholland 				NFSV4LOCKT_READ, cred, p, 0);
37716ca35587Sdholland 			    if ((nd->nd_repstat == NFSERR_GRACE ||
37726ca35587Sdholland 				 nd->nd_repstat == NFSERR_DELAY) &&
37736ca35587Sdholland 				error == 0)
37746ca35587Sdholland 				(void) nfs_catnap(PZERO, (int)nd->nd_repstat,
37756ca35587Sdholland 				    "nfs_advlock");
37766ca35587Sdholland 			} while ((nd->nd_repstat == NFSERR_GRACE ||
37776ca35587Sdholland 			    nd->nd_repstat == NFSERR_DELAY) && error == 0);
37786ca35587Sdholland 		    }
37796ca35587Sdholland 		    callcnt++;
37806ca35587Sdholland 		} while (error == 0 && nd->nd_repstat == 0);
37816ca35587Sdholland 		nfscl_releasealllocks(clp, vp, p, id, flags);
37826ca35587Sdholland 	    } else if (op == F_SETLK) {
37836ca35587Sdholland 		error = nfscl_getbytelock(vp, off, len, fl->l_type, cred, p,
37846ca35587Sdholland 		    NULL, 0, id, flags, NULL, NULL, &lp, &newone, &donelocally);
37856ca35587Sdholland 		if (error || donelocally) {
37866ca35587Sdholland 			return (error);
37876ca35587Sdholland 		}
37886ca35587Sdholland 		if (nmp->nm_clp != NULL)
37896ca35587Sdholland 			clidrev = nmp->nm_clp->nfsc_clientidrev;
37906ca35587Sdholland 		else
37916ca35587Sdholland 			clidrev = 0;
37926ca35587Sdholland 		nfhp = VTONFS(vp)->n_fhp;
37936ca35587Sdholland 		if (!lp->nfsl_open->nfso_posixlock &&
37946ca35587Sdholland 		    (off != 0 || len != NFS64BITSSET)) {
37956ca35587Sdholland 			error = EINVAL;
37966ca35587Sdholland 		} else {
37976ca35587Sdholland 			error = nfsrpc_lock(nd, nmp, vp, nfhp->nfh_fh,
37986ca35587Sdholland 			    nfhp->nfh_len, lp, newone, reclaim, off,
37996ca35587Sdholland 			    len, fl->l_type, cred, p, 0);
38006ca35587Sdholland 		}
38016ca35587Sdholland 		if (!error)
38026ca35587Sdholland 			error = nd->nd_repstat;
38036ca35587Sdholland 		nfscl_lockrelease(lp, error, newone);
38046ca35587Sdholland 	    } else {
38056ca35587Sdholland 		error = EINVAL;
38066ca35587Sdholland 	    }
38076ca35587Sdholland 	    if (!error)
38086ca35587Sdholland 	        error = nd->nd_repstat;
38096ca35587Sdholland 	    if (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
38106ca35587Sdholland 		error == NFSERR_STALEDONTRECOVER ||
38116ca35587Sdholland 		error == NFSERR_STALECLIENTID || error == NFSERR_DELAY ||
38126ca35587Sdholland 		error == NFSERR_BADSESSION) {
38136ca35587Sdholland 		(void) nfs_catnap(PZERO, error, "nfs_advlock");
38146ca35587Sdholland 	    } else if ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID)
38156ca35587Sdholland 		&& clidrev != 0) {
38166ca35587Sdholland 		expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
38176ca35587Sdholland 		retrycnt++;
38186ca35587Sdholland 	    }
38196ca35587Sdholland 	} while (error == NFSERR_GRACE ||
38206ca35587Sdholland 	    error == NFSERR_STALECLIENTID || error == NFSERR_DELAY ||
38216ca35587Sdholland 	    error == NFSERR_STALEDONTRECOVER || error == NFSERR_STALESTATEID ||
38226ca35587Sdholland 	    error == NFSERR_BADSESSION ||
38236ca35587Sdholland 	    ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
38246ca35587Sdholland 	     expireret == 0 && clidrev != 0 && retrycnt < 4));
38256ca35587Sdholland 	if (error && retrycnt >= 4)
38266ca35587Sdholland 		error = EIO;
38276ca35587Sdholland 	return (error);
38286ca35587Sdholland }
38296ca35587Sdholland 
38306ca35587Sdholland /*
38316ca35587Sdholland  * The lower level routine for the LockT case.
38326ca35587Sdholland  */
38336ca35587Sdholland APPLESTATIC int
nfsrpc_lockt(struct nfsrv_descript * nd,vnode_t vp,struct nfsclclient * clp,u_int64_t off,u_int64_t len,struct flock * fl,struct ucred * cred,NFSPROC_T * p,void * id,int flags)38346ca35587Sdholland nfsrpc_lockt(struct nfsrv_descript *nd, vnode_t vp,
38356ca35587Sdholland     struct nfsclclient *clp, u_int64_t off, u_int64_t len, struct flock *fl,
38366ca35587Sdholland     struct ucred *cred, NFSPROC_T *p, void *id, int flags)
38376ca35587Sdholland {
38386ca35587Sdholland 	u_int32_t *tl;
38396ca35587Sdholland 	int error, type, size;
38406ca35587Sdholland 	uint8_t own[NFSV4CL_LOCKNAMELEN + NFSX_V4FHMAX];
38416ca35587Sdholland 	struct nfsnode *np;
38426ca35587Sdholland 	struct nfsmount *nmp;
38436ca35587Sdholland 
38446ca35587Sdholland 	nmp = VFSTONFS(vp->v_mount);
38456ca35587Sdholland 	NFSCL_REQSTART(nd, NFSPROC_LOCKT, vp);
38466ca35587Sdholland 	NFSM_BUILD(tl, u_int32_t *, 7 * NFSX_UNSIGNED);
38476ca35587Sdholland 	if (fl->l_type == F_RDLCK)
38486ca35587Sdholland 		*tl++ = txdr_unsigned(NFSV4LOCKT_READ);
38496ca35587Sdholland 	else
38506ca35587Sdholland 		*tl++ = txdr_unsigned(NFSV4LOCKT_WRITE);
38516ca35587Sdholland 	txdr_hyper(off, tl);
38526ca35587Sdholland 	tl += 2;
38536ca35587Sdholland 	txdr_hyper(len, tl);
38546ca35587Sdholland 	tl += 2;
38556ca35587Sdholland 	*tl++ = NFSMNT_MDSSESSION(nmp)->nfsess_clientid.lval[0];
38566ca35587Sdholland 	*tl = NFSMNT_MDSSESSION(nmp)->nfsess_clientid.lval[1];
38576ca35587Sdholland 	nfscl_filllockowner(id, own, flags);
38586ca35587Sdholland 	np = VTONFS(vp);
38596ca35587Sdholland 	NFSBCOPY(np->n_fhp->nfh_fh, &own[NFSV4CL_LOCKNAMELEN],
38606ca35587Sdholland 	    np->n_fhp->nfh_len);
38616ca35587Sdholland 	(void)nfsm_strtom(nd, own, NFSV4CL_LOCKNAMELEN + np->n_fhp->nfh_len);
38626ca35587Sdholland 	error = nfscl_request(nd, vp, p, cred, NULL);
38636ca35587Sdholland 	if (error)
38646ca35587Sdholland 		return (error);
38656ca35587Sdholland 	if (nd->nd_repstat == 0) {
38666ca35587Sdholland 		fl->l_type = F_UNLCK;
38676ca35587Sdholland 	} else if (nd->nd_repstat == NFSERR_DENIED) {
38686ca35587Sdholland 		nd->nd_repstat = 0;
38696ca35587Sdholland 		fl->l_whence = SEEK_SET;
38706ca35587Sdholland 		NFSM_DISSECT(tl, u_int32_t *, 8 * NFSX_UNSIGNED);
38716ca35587Sdholland 		fl->l_start = fxdr_hyper(tl);
38726ca35587Sdholland 		tl += 2;
38736ca35587Sdholland 		len = fxdr_hyper(tl);
38746ca35587Sdholland 		tl += 2;
38756ca35587Sdholland 		if (len == NFS64BITSSET)
38766ca35587Sdholland 			fl->l_len = 0;
38776ca35587Sdholland 		else
38786ca35587Sdholland 			fl->l_len = len;
38796ca35587Sdholland 		type = fxdr_unsigned(int, *tl++);
38806ca35587Sdholland 		if (type == NFSV4LOCKT_WRITE)
38816ca35587Sdholland 			fl->l_type = F_WRLCK;
38826ca35587Sdholland 		else
38836ca35587Sdholland 			fl->l_type = F_RDLCK;
38846ca35587Sdholland 		/*
38856ca35587Sdholland 		 * XXX For now, I have no idea what to do with the
38866ca35587Sdholland 		 * conflicting lock_owner, so I'll just set the pid == 0
38876ca35587Sdholland 		 * and skip over the lock_owner.
38886ca35587Sdholland 		 */
38896ca35587Sdholland 		fl->l_pid = (pid_t)0;
38906ca35587Sdholland 		tl += 2;
38916ca35587Sdholland 		size = fxdr_unsigned(int, *tl);
38926ca35587Sdholland 		if (size < 0 || size > NFSV4_OPAQUELIMIT)
38936ca35587Sdholland 			error = EBADRPC;
38946ca35587Sdholland 		if (!error)
38956ca35587Sdholland 			error = nfsm_advance(nd, NFSM_RNDUP(size), -1);
38966ca35587Sdholland 	} else if (nd->nd_repstat == NFSERR_STALECLIENTID ||
38976ca35587Sdholland 	    nd->nd_repstat == NFSERR_BADSESSION)
38986ca35587Sdholland 		nfscl_initiate_recovery(clp);
38996ca35587Sdholland nfsmout:
39006ca35587Sdholland 	mbuf_freem(nd->nd_mrep);
39016ca35587Sdholland 	return (error);
39026ca35587Sdholland }
39036ca35587Sdholland 
39046ca35587Sdholland /*
39056ca35587Sdholland  * Lower level function that performs the LockU RPC.
39066ca35587Sdholland  */
39076ca35587Sdholland static int
nfsrpc_locku(struct nfsrv_descript * nd,struct nfsmount * nmp,struct nfscllockowner * lp,u_int64_t off,u_int64_t len,u_int32_t type,struct ucred * cred,NFSPROC_T * p,int syscred)39086ca35587Sdholland nfsrpc_locku(struct nfsrv_descript *nd, struct nfsmount *nmp,
39096ca35587Sdholland     struct nfscllockowner *lp, u_int64_t off, u_int64_t len,
39106ca35587Sdholland     u_int32_t type, struct ucred *cred, NFSPROC_T *p, int syscred)
39116ca35587Sdholland {
39126ca35587Sdholland 	u_int32_t *tl;
39136ca35587Sdholland 	int error;
39146ca35587Sdholland 
39156ca35587Sdholland 	nfscl_reqstart(nd, NFSPROC_LOCKU, nmp, lp->nfsl_open->nfso_fh,
39166ca35587Sdholland 	    lp->nfsl_open->nfso_fhlen, NULL, NULL);
39176ca35587Sdholland 	NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID + 6 * NFSX_UNSIGNED);
39186ca35587Sdholland 	*tl++ = txdr_unsigned(type);
39196ca35587Sdholland 	*tl = txdr_unsigned(lp->nfsl_seqid);
39206ca35587Sdholland 	if (nfstest_outofseq &&
39216ca35587Sdholland 	    (arc4random() % nfstest_outofseq) == 0)
39226ca35587Sdholland 		*tl = txdr_unsigned(lp->nfsl_seqid + 1);
39236ca35587Sdholland 	tl++;
39246ca35587Sdholland 	if (NFSHASNFSV4N(nmp))
39256ca35587Sdholland 		*tl++ = 0;
39266ca35587Sdholland 	else
39276ca35587Sdholland 		*tl++ = lp->nfsl_stateid.seqid;
39286ca35587Sdholland 	*tl++ = lp->nfsl_stateid.other[0];
39296ca35587Sdholland 	*tl++ = lp->nfsl_stateid.other[1];
39306ca35587Sdholland 	*tl++ = lp->nfsl_stateid.other[2];
39316ca35587Sdholland 	txdr_hyper(off, tl);
39326ca35587Sdholland 	tl += 2;
39336ca35587Sdholland 	txdr_hyper(len, tl);
39346ca35587Sdholland 	if (syscred)
39356ca35587Sdholland 		nd->nd_flag |= ND_USEGSSNAME;
39366ca35587Sdholland 	error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
39376ca35587Sdholland 	    NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
39386ca35587Sdholland 	NFSCL_INCRSEQID(lp->nfsl_seqid, nd);
39396ca35587Sdholland 	if (error)
39406ca35587Sdholland 		return (error);
39416ca35587Sdholland 	if (nd->nd_repstat == 0) {
39426ca35587Sdholland 		NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID);
39436ca35587Sdholland 		lp->nfsl_stateid.seqid = *tl++;
39446ca35587Sdholland 		lp->nfsl_stateid.other[0] = *tl++;
39456ca35587Sdholland 		lp->nfsl_stateid.other[1] = *tl++;
39466ca35587Sdholland 		lp->nfsl_stateid.other[2] = *tl;
39476ca35587Sdholland 	} else if (nd->nd_repstat == NFSERR_STALESTATEID ||
39486ca35587Sdholland 	    nd->nd_repstat == NFSERR_BADSESSION)
39496ca35587Sdholland 		nfscl_initiate_recovery(lp->nfsl_open->nfso_own->nfsow_clp);
39506ca35587Sdholland nfsmout:
39516ca35587Sdholland 	mbuf_freem(nd->nd_mrep);
39526ca35587Sdholland 	return (error);
39536ca35587Sdholland }
39546ca35587Sdholland 
39556ca35587Sdholland /*
39566ca35587Sdholland  * The actual Lock RPC.
39576ca35587Sdholland  */
39586ca35587Sdholland APPLESTATIC int
nfsrpc_lock(struct nfsrv_descript * nd,struct nfsmount * nmp,vnode_t vp,u_int8_t * nfhp,int fhlen,struct nfscllockowner * lp,int newone,int reclaim,u_int64_t off,u_int64_t len,short type,struct ucred * cred,NFSPROC_T * p,int syscred)39596ca35587Sdholland nfsrpc_lock(struct nfsrv_descript *nd, struct nfsmount *nmp, vnode_t vp,
39606ca35587Sdholland     u_int8_t *nfhp, int fhlen, struct nfscllockowner *lp, int newone,
39616ca35587Sdholland     int reclaim, u_int64_t off, u_int64_t len, short type, struct ucred *cred,
39626ca35587Sdholland     NFSPROC_T *p, int syscred)
39636ca35587Sdholland {
39646ca35587Sdholland 	u_int32_t *tl;
39656ca35587Sdholland 	int error, size;
39666ca35587Sdholland 	uint8_t own[NFSV4CL_LOCKNAMELEN + NFSX_V4FHMAX];
39676ca35587Sdholland 
39686ca35587Sdholland 	nfscl_reqstart(nd, NFSPROC_LOCK, nmp, nfhp, fhlen, NULL, NULL);
39696ca35587Sdholland 	NFSM_BUILD(tl, u_int32_t *, 7 * NFSX_UNSIGNED);
39706ca35587Sdholland 	if (type == F_RDLCK)
39716ca35587Sdholland 		*tl++ = txdr_unsigned(NFSV4LOCKT_READ);
39726ca35587Sdholland 	else
39736ca35587Sdholland 		*tl++ = txdr_unsigned(NFSV4LOCKT_WRITE);
39746ca35587Sdholland 	*tl++ = txdr_unsigned(reclaim);
39756ca35587Sdholland 	txdr_hyper(off, tl);
39766ca35587Sdholland 	tl += 2;
39776ca35587Sdholland 	txdr_hyper(len, tl);
39786ca35587Sdholland 	tl += 2;
39796ca35587Sdholland 	if (newone) {
39806ca35587Sdholland 	    *tl = newnfs_true;
39816ca35587Sdholland 	    NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID +
39826ca35587Sdholland 		2 * NFSX_UNSIGNED + NFSX_HYPER);
39836ca35587Sdholland 	    *tl++ = txdr_unsigned(lp->nfsl_open->nfso_own->nfsow_seqid);
39846ca35587Sdholland 	    if (NFSHASNFSV4N(nmp))
39856ca35587Sdholland 		*tl++ = 0;
39866ca35587Sdholland 	    else
39876ca35587Sdholland 		*tl++ = lp->nfsl_open->nfso_stateid.seqid;
39886ca35587Sdholland 	    *tl++ = lp->nfsl_open->nfso_stateid.other[0];
39896ca35587Sdholland 	    *tl++ = lp->nfsl_open->nfso_stateid.other[1];
39906ca35587Sdholland 	    *tl++ = lp->nfsl_open->nfso_stateid.other[2];
39916ca35587Sdholland 	    *tl++ = txdr_unsigned(lp->nfsl_seqid);
39926ca35587Sdholland 	    *tl++ = NFSMNT_MDSSESSION(nmp)->nfsess_clientid.lval[0];
39936ca35587Sdholland 	    *tl = NFSMNT_MDSSESSION(nmp)->nfsess_clientid.lval[1];
39946ca35587Sdholland 	    NFSBCOPY(lp->nfsl_owner, own, NFSV4CL_LOCKNAMELEN);
39956ca35587Sdholland 	    NFSBCOPY(nfhp, &own[NFSV4CL_LOCKNAMELEN], fhlen);
39966ca35587Sdholland 	    (void)nfsm_strtom(nd, own, NFSV4CL_LOCKNAMELEN + fhlen);
39976ca35587Sdholland 	} else {
39986ca35587Sdholland 	    *tl = newnfs_false;
39996ca35587Sdholland 	    NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID + NFSX_UNSIGNED);
40006ca35587Sdholland 	    if (NFSHASNFSV4N(nmp))
40016ca35587Sdholland 		*tl++ = 0;
40026ca35587Sdholland 	    else
40036ca35587Sdholland 		*tl++ = lp->nfsl_stateid.seqid;
40046ca35587Sdholland 	    *tl++ = lp->nfsl_stateid.other[0];
40056ca35587Sdholland 	    *tl++ = lp->nfsl_stateid.other[1];
40066ca35587Sdholland 	    *tl++ = lp->nfsl_stateid.other[2];
40076ca35587Sdholland 	    *tl = txdr_unsigned(lp->nfsl_seqid);
40086ca35587Sdholland 	    if (nfstest_outofseq &&
40096ca35587Sdholland 		(arc4random() % nfstest_outofseq) == 0)
40106ca35587Sdholland 		    *tl = txdr_unsigned(lp->nfsl_seqid + 1);
40116ca35587Sdholland 	}
40126ca35587Sdholland 	if (syscred)
40136ca35587Sdholland 		nd->nd_flag |= ND_USEGSSNAME;
40146ca35587Sdholland 	error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, vp, p, cred,
40156ca35587Sdholland 	    NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
40166ca35587Sdholland 	if (error)
40176ca35587Sdholland 		return (error);
40186ca35587Sdholland 	if (newone)
40196ca35587Sdholland 	    NFSCL_INCRSEQID(lp->nfsl_open->nfso_own->nfsow_seqid, nd);
40206ca35587Sdholland 	NFSCL_INCRSEQID(lp->nfsl_seqid, nd);
40216ca35587Sdholland 	if (nd->nd_repstat == 0) {
40226ca35587Sdholland 		NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID);
40236ca35587Sdholland 		lp->nfsl_stateid.seqid = *tl++;
40246ca35587Sdholland 		lp->nfsl_stateid.other[0] = *tl++;
40256ca35587Sdholland 		lp->nfsl_stateid.other[1] = *tl++;
40266ca35587Sdholland 		lp->nfsl_stateid.other[2] = *tl;
40276ca35587Sdholland 	} else if (nd->nd_repstat == NFSERR_DENIED) {
40286ca35587Sdholland 		NFSM_DISSECT(tl, u_int32_t *, 8 * NFSX_UNSIGNED);
40296ca35587Sdholland 		size = fxdr_unsigned(int, *(tl + 7));
40306ca35587Sdholland 		if (size < 0 || size > NFSV4_OPAQUELIMIT)
40316ca35587Sdholland 			error = EBADRPC;
40326ca35587Sdholland 		if (!error)
40336ca35587Sdholland 			error = nfsm_advance(nd, NFSM_RNDUP(size), -1);
40346ca35587Sdholland 	} else if (nd->nd_repstat == NFSERR_STALESTATEID ||
40356ca35587Sdholland 	    nd->nd_repstat == NFSERR_BADSESSION)
40366ca35587Sdholland 		nfscl_initiate_recovery(lp->nfsl_open->nfso_own->nfsow_clp);
40376ca35587Sdholland nfsmout:
40386ca35587Sdholland 	mbuf_freem(nd->nd_mrep);
40396ca35587Sdholland 	return (error);
40406ca35587Sdholland }
40416ca35587Sdholland 
40426ca35587Sdholland /*
40436ca35587Sdholland  * nfs statfs rpc
40446ca35587Sdholland  * (always called with the vp for the mount point)
40456ca35587Sdholland  */
40466ca35587Sdholland APPLESTATIC int
nfsrpc_statfs(vnode_t vp,struct nfsstatfs * sbp,struct nfsfsinfo * fsp,struct ucred * cred,NFSPROC_T * p,struct nfsvattr * nap,int * attrflagp,void * stuff)40476ca35587Sdholland nfsrpc_statfs(vnode_t vp, struct nfsstatfs *sbp, struct nfsfsinfo *fsp,
40486ca35587Sdholland     struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp,
40496ca35587Sdholland     void *stuff)
40506ca35587Sdholland {
40516ca35587Sdholland 	u_int32_t *tl = NULL;
40526ca35587Sdholland 	struct nfsrv_descript nfsd, *nd = &nfsd;
40536ca35587Sdholland 	struct nfsmount *nmp;
40546ca35587Sdholland 	nfsattrbit_t attrbits;
40556ca35587Sdholland 	int error;
40566ca35587Sdholland 
40576ca35587Sdholland 	*attrflagp = 0;
40586ca35587Sdholland 	nmp = VFSTONFS(vnode_mount(vp));
40596ca35587Sdholland 	if (NFSHASNFSV4(nmp)) {
40606ca35587Sdholland 		/*
40616ca35587Sdholland 		 * For V4, you actually do a getattr.
40626ca35587Sdholland 		 */
40636ca35587Sdholland 		NFSCL_REQSTART(nd, NFSPROC_GETATTR, vp);
40646ca35587Sdholland 		NFSSTATFS_GETATTRBIT(&attrbits);
40656ca35587Sdholland 		(void) nfsrv_putattrbit(nd, &attrbits);
40666ca35587Sdholland 		nd->nd_flag |= ND_USEGSSNAME;
40676ca35587Sdholland 		error = nfscl_request(nd, vp, p, cred, stuff);
40686ca35587Sdholland 		if (error)
40696ca35587Sdholland 			return (error);
40706ca35587Sdholland 		if (nd->nd_repstat == 0) {
40716ca35587Sdholland 			error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0,
40726ca35587Sdholland 			    NULL, NULL, sbp, fsp, NULL, 0, NULL, NULL, NULL, p,
40736ca35587Sdholland 			    cred);
40746ca35587Sdholland 			if (!error) {
40756ca35587Sdholland 				nmp->nm_fsid[0] = nap->na_filesid[0];
40766ca35587Sdholland 				nmp->nm_fsid[1] = nap->na_filesid[1];
40776ca35587Sdholland 				NFSSETHASSETFSID(nmp);
40786ca35587Sdholland 				*attrflagp = 1;
40796ca35587Sdholland 			}
40806ca35587Sdholland 		} else {
40816ca35587Sdholland 			error = nd->nd_repstat;
40826ca35587Sdholland 		}
40836ca35587Sdholland 		if (error)
40846ca35587Sdholland 			goto nfsmout;
40856ca35587Sdholland 	} else {
40866ca35587Sdholland 		NFSCL_REQSTART(nd, NFSPROC_FSSTAT, vp);
40876ca35587Sdholland 		error = nfscl_request(nd, vp, p, cred, stuff);
40886ca35587Sdholland 		if (error)
40896ca35587Sdholland 			return (error);
40906ca35587Sdholland 		if (nd->nd_flag & ND_NFSV3) {
40916ca35587Sdholland 			error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
40926ca35587Sdholland 			if (error)
40936ca35587Sdholland 				goto nfsmout;
40946ca35587Sdholland 		}
40956ca35587Sdholland 		if (nd->nd_repstat) {
40966ca35587Sdholland 			error = nd->nd_repstat;
40976ca35587Sdholland 			goto nfsmout;
40986ca35587Sdholland 		}
40996ca35587Sdholland 		NFSM_DISSECT(tl, u_int32_t *,
41006ca35587Sdholland 		    NFSX_STATFS(nd->nd_flag & ND_NFSV3));
41016ca35587Sdholland 	}
41026ca35587Sdholland 	if (NFSHASNFSV3(nmp)) {
41036ca35587Sdholland 		sbp->sf_tbytes = fxdr_hyper(tl); tl += 2;
41046ca35587Sdholland 		sbp->sf_fbytes = fxdr_hyper(tl); tl += 2;
41056ca35587Sdholland 		sbp->sf_abytes = fxdr_hyper(tl); tl += 2;
41066ca35587Sdholland 		sbp->sf_tfiles = fxdr_hyper(tl); tl += 2;
41076ca35587Sdholland 		sbp->sf_ffiles = fxdr_hyper(tl); tl += 2;
41086ca35587Sdholland 		sbp->sf_afiles = fxdr_hyper(tl); tl += 2;
41096ca35587Sdholland 		sbp->sf_invarsec = fxdr_unsigned(u_int32_t, *tl);
41106ca35587Sdholland 	} else if (NFSHASNFSV4(nmp) == 0) {
41116ca35587Sdholland 		sbp->sf_tsize = fxdr_unsigned(u_int32_t, *tl++);
41126ca35587Sdholland 		sbp->sf_bsize = fxdr_unsigned(u_int32_t, *tl++);
41136ca35587Sdholland 		sbp->sf_blocks = fxdr_unsigned(u_int32_t, *tl++);
41146ca35587Sdholland 		sbp->sf_bfree = fxdr_unsigned(u_int32_t, *tl++);
41156ca35587Sdholland 		sbp->sf_bavail = fxdr_unsigned(u_int32_t, *tl);
41166ca35587Sdholland 	}
41176ca35587Sdholland nfsmout:
41186ca35587Sdholland 	mbuf_freem(nd->nd_mrep);
41196ca35587Sdholland 	return (error);
41206ca35587Sdholland }
41216ca35587Sdholland 
41226ca35587Sdholland /*
41236ca35587Sdholland  * nfs pathconf rpc
41246ca35587Sdholland  */
41256ca35587Sdholland APPLESTATIC int
nfsrpc_pathconf(vnode_t vp,struct nfsv3_pathconf * pc,struct ucred * cred,NFSPROC_T * p,struct nfsvattr * nap,int * attrflagp,void * stuff)41266ca35587Sdholland nfsrpc_pathconf(vnode_t vp, struct nfsv3_pathconf *pc,
41276ca35587Sdholland     struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp,
41286ca35587Sdholland     void *stuff)
41296ca35587Sdholland {
41306ca35587Sdholland 	struct nfsrv_descript nfsd, *nd = &nfsd;
41316ca35587Sdholland 	struct nfsmount *nmp;
41326ca35587Sdholland 	u_int32_t *tl;
41336ca35587Sdholland 	nfsattrbit_t attrbits;
41346ca35587Sdholland 	int error;
41356ca35587Sdholland 
41366ca35587Sdholland 	*attrflagp = 0;
41376ca35587Sdholland 	nmp = VFSTONFS(vnode_mount(vp));
41386ca35587Sdholland 	if (NFSHASNFSV4(nmp)) {
41396ca35587Sdholland 		/*
41406ca35587Sdholland 		 * For V4, you actually do a getattr.
41416ca35587Sdholland 		 */
41426ca35587Sdholland 		NFSCL_REQSTART(nd, NFSPROC_GETATTR, vp);
41436ca35587Sdholland 		NFSPATHCONF_GETATTRBIT(&attrbits);
41446ca35587Sdholland 		(void) nfsrv_putattrbit(nd, &attrbits);
41456ca35587Sdholland 		nd->nd_flag |= ND_USEGSSNAME;
41466ca35587Sdholland 		error = nfscl_request(nd, vp, p, cred, stuff);
41476ca35587Sdholland 		if (error)
41486ca35587Sdholland 			return (error);
41496ca35587Sdholland 		if (nd->nd_repstat == 0) {
41506ca35587Sdholland 			error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0,
41516ca35587Sdholland 			    pc, NULL, NULL, NULL, NULL, 0, NULL, NULL, NULL, p,
41526ca35587Sdholland 			    cred);
41536ca35587Sdholland 			if (!error)
41546ca35587Sdholland 				*attrflagp = 1;
41556ca35587Sdholland 		} else {
41566ca35587Sdholland 			error = nd->nd_repstat;
41576ca35587Sdholland 		}
41586ca35587Sdholland 	} else {
41596ca35587Sdholland 		NFSCL_REQSTART(nd, NFSPROC_PATHCONF, vp);
41606ca35587Sdholland 		error = nfscl_request(nd, vp, p, cred, stuff);
41616ca35587Sdholland 		if (error)
41626ca35587Sdholland 			return (error);
41636ca35587Sdholland 		error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
41646ca35587Sdholland 		if (nd->nd_repstat && !error)
41656ca35587Sdholland 			error = nd->nd_repstat;
41666ca35587Sdholland 		if (!error) {
41676ca35587Sdholland 			NFSM_DISSECT(tl, u_int32_t *, NFSX_V3PATHCONF);
41686ca35587Sdholland 			pc->pc_linkmax = fxdr_unsigned(u_int32_t, *tl++);
41696ca35587Sdholland 			pc->pc_namemax = fxdr_unsigned(u_int32_t, *tl++);
41706ca35587Sdholland 			pc->pc_notrunc = fxdr_unsigned(u_int32_t, *tl++);
41716ca35587Sdholland 			pc->pc_chownrestricted =
41726ca35587Sdholland 			    fxdr_unsigned(u_int32_t, *tl++);
41736ca35587Sdholland 			pc->pc_caseinsensitive =
41746ca35587Sdholland 			    fxdr_unsigned(u_int32_t, *tl++);
41756ca35587Sdholland 			pc->pc_casepreserving = fxdr_unsigned(u_int32_t, *tl);
41766ca35587Sdholland 		}
41776ca35587Sdholland 	}
41786ca35587Sdholland nfsmout:
41796ca35587Sdholland 	mbuf_freem(nd->nd_mrep);
41806ca35587Sdholland 	return (error);
41816ca35587Sdholland }
41826ca35587Sdholland 
41836ca35587Sdholland /*
41846ca35587Sdholland  * nfs version 3 fsinfo rpc call
41856ca35587Sdholland  */
41866ca35587Sdholland APPLESTATIC int
nfsrpc_fsinfo(vnode_t vp,struct nfsfsinfo * fsp,struct ucred * cred,NFSPROC_T * p,struct nfsvattr * nap,int * attrflagp,void * stuff)41876ca35587Sdholland nfsrpc_fsinfo(vnode_t vp, struct nfsfsinfo *fsp, struct ucred *cred,
41886ca35587Sdholland     NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, void *stuff)
41896ca35587Sdholland {
41906ca35587Sdholland 	u_int32_t *tl;
41916ca35587Sdholland 	struct nfsrv_descript nfsd, *nd = &nfsd;
41926ca35587Sdholland 	int error;
41936ca35587Sdholland 
41946ca35587Sdholland 	*attrflagp = 0;
41956ca35587Sdholland 	NFSCL_REQSTART(nd, NFSPROC_FSINFO, vp);
41966ca35587Sdholland 	error = nfscl_request(nd, vp, p, cred, stuff);
41976ca35587Sdholland 	if (error)
41986ca35587Sdholland 		return (error);
41996ca35587Sdholland 	error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
42006ca35587Sdholland 	if (nd->nd_repstat && !error)
42016ca35587Sdholland 		error = nd->nd_repstat;
42026ca35587Sdholland 	if (!error) {
42036ca35587Sdholland 		NFSM_DISSECT(tl, u_int32_t *, NFSX_V3FSINFO);
42046ca35587Sdholland 		fsp->fs_rtmax = fxdr_unsigned(u_int32_t, *tl++);
42056ca35587Sdholland 		fsp->fs_rtpref = fxdr_unsigned(u_int32_t, *tl++);
42066ca35587Sdholland 		fsp->fs_rtmult = fxdr_unsigned(u_int32_t, *tl++);
42076ca35587Sdholland 		fsp->fs_wtmax = fxdr_unsigned(u_int32_t, *tl++);
42086ca35587Sdholland 		fsp->fs_wtpref = fxdr_unsigned(u_int32_t, *tl++);
42096ca35587Sdholland 		fsp->fs_wtmult = fxdr_unsigned(u_int32_t, *tl++);
42106ca35587Sdholland 		fsp->fs_dtpref = fxdr_unsigned(u_int32_t, *tl++);
42116ca35587Sdholland 		fsp->fs_maxfilesize = fxdr_hyper(tl);
42126ca35587Sdholland 		tl += 2;
42136ca35587Sdholland 		fxdr_nfsv3time(tl, &fsp->fs_timedelta);
42146ca35587Sdholland 		tl += 2;
42156ca35587Sdholland 		fsp->fs_properties = fxdr_unsigned(u_int32_t, *tl);
42166ca35587Sdholland 	}
42176ca35587Sdholland nfsmout:
42186ca35587Sdholland 	mbuf_freem(nd->nd_mrep);
42196ca35587Sdholland 	return (error);
42206ca35587Sdholland }
42216ca35587Sdholland 
42226ca35587Sdholland /*
42236ca35587Sdholland  * This function performs the Renew RPC.
42246ca35587Sdholland  */
42256ca35587Sdholland APPLESTATIC int
nfsrpc_renew(struct nfsclclient * clp,struct nfsclds * dsp,struct ucred * cred,NFSPROC_T * p)42266ca35587Sdholland nfsrpc_renew(struct nfsclclient *clp, struct nfsclds *dsp, struct ucred *cred,
42276ca35587Sdholland     NFSPROC_T *p)
42286ca35587Sdholland {
42296ca35587Sdholland 	u_int32_t *tl;
42306ca35587Sdholland 	struct nfsrv_descript nfsd;
42316ca35587Sdholland 	struct nfsrv_descript *nd = &nfsd;
42326ca35587Sdholland 	struct nfsmount *nmp;
42336ca35587Sdholland 	int error;
42346ca35587Sdholland 	struct nfssockreq *nrp;
42356ca35587Sdholland 
42366ca35587Sdholland 	nmp = clp->nfsc_nmp;
42376ca35587Sdholland 	if (nmp == NULL)
42386ca35587Sdholland 		return (0);
42396ca35587Sdholland 	nfscl_reqstart(nd, NFSPROC_RENEW, nmp, NULL, 0, NULL,
42406ca35587Sdholland 	    &dsp->nfsclds_sess);
42416ca35587Sdholland 	if (!NFSHASNFSV4N(nmp)) {
42426ca35587Sdholland 		/* NFSv4.1 just uses a Sequence Op and not a Renew. */
42436ca35587Sdholland 		NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
42446ca35587Sdholland 		*tl++ = NFSMNT_MDSSESSION(nmp)->nfsess_clientid.lval[0];
42456ca35587Sdholland 		*tl = NFSMNT_MDSSESSION(nmp)->nfsess_clientid.lval[1];
42466ca35587Sdholland 	}
42476ca35587Sdholland 	nrp = dsp->nfsclds_sockp;
42486ca35587Sdholland 	if (nrp == NULL)
42496ca35587Sdholland 		/* If NULL, use the MDS socket. */
42506ca35587Sdholland 		nrp = &nmp->nm_sockreq;
42516ca35587Sdholland 	nd->nd_flag |= ND_USEGSSNAME;
42526ca35587Sdholland 	error = newnfs_request(nd, nmp, NULL, nrp, NULL, p, cred,
42536ca35587Sdholland 	    NFS_PROG, NFS_VER4, NULL, 1, NULL, &dsp->nfsclds_sess);
42546ca35587Sdholland 	if (error)
42556ca35587Sdholland 		return (error);
42566ca35587Sdholland 	error = nd->nd_repstat;
42576ca35587Sdholland 	mbuf_freem(nd->nd_mrep);
42586ca35587Sdholland 	return (error);
42596ca35587Sdholland }
42606ca35587Sdholland 
42616ca35587Sdholland /*
42626ca35587Sdholland  * This function performs the Releaselockowner RPC.
42636ca35587Sdholland  */
42646ca35587Sdholland APPLESTATIC int
nfsrpc_rellockown(struct nfsmount * nmp,struct nfscllockowner * lp,uint8_t * fh,int fhlen,struct ucred * cred,NFSPROC_T * p)42656ca35587Sdholland nfsrpc_rellockown(struct nfsmount *nmp, struct nfscllockowner *lp,
42666ca35587Sdholland     uint8_t *fh, int fhlen, struct ucred *cred, NFSPROC_T *p)
42676ca35587Sdholland {
42686ca35587Sdholland 	struct nfsrv_descript nfsd, *nd = &nfsd;
42696ca35587Sdholland 	u_int32_t *tl;
42706ca35587Sdholland 	int error;
42716ca35587Sdholland 	uint8_t own[NFSV4CL_LOCKNAMELEN + NFSX_V4FHMAX];
42726ca35587Sdholland 
42736ca35587Sdholland 	if (NFSHASNFSV4N(nmp)) {
42746ca35587Sdholland 		/* For NFSv4.1, do a FreeStateID. */
42756ca35587Sdholland 		nfscl_reqstart(nd, NFSPROC_FREESTATEID, nmp, NULL, 0, NULL,
42766ca35587Sdholland 		    NULL);
42776ca35587Sdholland 		nfsm_stateidtom(nd, &lp->nfsl_stateid, NFSSTATEID_PUTSTATEID);
42786ca35587Sdholland 	} else {
42796ca35587Sdholland 		nfscl_reqstart(nd, NFSPROC_RELEASELCKOWN, nmp, NULL, 0, NULL,
42806ca35587Sdholland 		    NULL);
42816ca35587Sdholland 		NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
42826ca35587Sdholland 		*tl++ = NFSMNT_MDSSESSION(nmp)->nfsess_clientid.lval[0];
42836ca35587Sdholland 		*tl = NFSMNT_MDSSESSION(nmp)->nfsess_clientid.lval[1];
42846ca35587Sdholland 		NFSBCOPY(lp->nfsl_owner, own, NFSV4CL_LOCKNAMELEN);
42856ca35587Sdholland 		NFSBCOPY(fh, &own[NFSV4CL_LOCKNAMELEN], fhlen);
42866ca35587Sdholland 		(void)nfsm_strtom(nd, own, NFSV4CL_LOCKNAMELEN + fhlen);
42876ca35587Sdholland 	}
42886ca35587Sdholland 	nd->nd_flag |= ND_USEGSSNAME;
42896ca35587Sdholland 	error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
42906ca35587Sdholland 	    NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
42916ca35587Sdholland 	if (error)
42926ca35587Sdholland 		return (error);
42936ca35587Sdholland 	error = nd->nd_repstat;
42946ca35587Sdholland 	mbuf_freem(nd->nd_mrep);
42956ca35587Sdholland 	return (error);
42966ca35587Sdholland }
42976ca35587Sdholland 
42986ca35587Sdholland /*
42996ca35587Sdholland  * This function performs the Compound to get the mount pt FH.
43006ca35587Sdholland  */
43016ca35587Sdholland APPLESTATIC int
nfsrpc_getdirpath(struct nfsmount * nmp,u_char * dirpath,struct ucred * cred,NFSPROC_T * p)43026ca35587Sdholland nfsrpc_getdirpath(struct nfsmount *nmp, u_char *dirpath, struct ucred *cred,
43036ca35587Sdholland     NFSPROC_T *p)
43046ca35587Sdholland {
43056ca35587Sdholland 	u_int32_t *tl;
43066ca35587Sdholland 	struct nfsrv_descript nfsd;
43076ca35587Sdholland 	struct nfsrv_descript *nd = &nfsd;
43086ca35587Sdholland 	u_char *cp, *cp2;
43096ca35587Sdholland 	int error, cnt, len, setnil;
43106ca35587Sdholland 	u_int32_t *opcntp;
43116ca35587Sdholland 
43126ca35587Sdholland 	nfscl_reqstart(nd, NFSPROC_PUTROOTFH, nmp, NULL, 0, &opcntp, NULL);
43136ca35587Sdholland 	cp = dirpath;
43146ca35587Sdholland 	cnt = 0;
43156ca35587Sdholland 	do {
43166ca35587Sdholland 		setnil = 0;
43176ca35587Sdholland 		while (*cp == '/')
43186ca35587Sdholland 			cp++;
43196ca35587Sdholland 		cp2 = cp;
43206ca35587Sdholland 		while (*cp2 != '\0' && *cp2 != '/')
43216ca35587Sdholland 			cp2++;
43226ca35587Sdholland 		if (*cp2 == '/') {
43236ca35587Sdholland 			setnil = 1;
43246ca35587Sdholland 			*cp2 = '\0';
43256ca35587Sdholland 		}
43266ca35587Sdholland 		if (cp2 != cp) {
43276ca35587Sdholland 			NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
43286ca35587Sdholland 			*tl = txdr_unsigned(NFSV4OP_LOOKUP);
43296ca35587Sdholland 			nfsm_strtom(nd, cp, strlen(cp));
43306ca35587Sdholland 			cnt++;
43316ca35587Sdholland 		}
43326ca35587Sdholland 		if (setnil)
43336ca35587Sdholland 			*cp2++ = '/';
43346ca35587Sdholland 		cp = cp2;
43356ca35587Sdholland 	} while (*cp != '\0');
43366ca35587Sdholland 	if (NFSHASNFSV4N(nmp))
43376ca35587Sdholland 		/* Has a Sequence Op done by nfscl_reqstart(). */
43386ca35587Sdholland 		*opcntp = txdr_unsigned(3 + cnt);
43396ca35587Sdholland 	else
43406ca35587Sdholland 		*opcntp = txdr_unsigned(2 + cnt);
43416ca35587Sdholland 	NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
43426ca35587Sdholland 	*tl = txdr_unsigned(NFSV4OP_GETFH);
43436ca35587Sdholland 	nd->nd_flag |= ND_USEGSSNAME;
43446ca35587Sdholland 	error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
43456ca35587Sdholland 		NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
43466ca35587Sdholland 	if (error)
43476ca35587Sdholland 		return (error);
43486ca35587Sdholland 	if (nd->nd_repstat == 0) {
43496ca35587Sdholland 		NFSM_DISSECT(tl, u_int32_t *, (3 + 2 * cnt) * NFSX_UNSIGNED);
43506ca35587Sdholland 		tl += (2 + 2 * cnt);
43516ca35587Sdholland 		if ((len = fxdr_unsigned(int, *tl)) <= 0 ||
43526ca35587Sdholland 			len > NFSX_FHMAX) {
43536ca35587Sdholland 			nd->nd_repstat = NFSERR_BADXDR;
43546ca35587Sdholland 		} else {
43556ca35587Sdholland 			nd->nd_repstat = nfsrv_mtostr(nd, nmp->nm_fh, len);
43566ca35587Sdholland 			if (nd->nd_repstat == 0)
43576ca35587Sdholland 				nmp->nm_fhsize = len;
43586ca35587Sdholland 		}
43596ca35587Sdholland 	}
43606ca35587Sdholland 	error = nd->nd_repstat;
43616ca35587Sdholland nfsmout:
43626ca35587Sdholland 	mbuf_freem(nd->nd_mrep);
43636ca35587Sdholland 	return (error);
43646ca35587Sdholland }
43656ca35587Sdholland 
43666ca35587Sdholland /*
43676ca35587Sdholland  * This function performs the Delegreturn RPC.
43686ca35587Sdholland  */
43696ca35587Sdholland APPLESTATIC int
nfsrpc_delegreturn(struct nfscldeleg * dp,struct ucred * cred,struct nfsmount * nmp,NFSPROC_T * p,int syscred)43706ca35587Sdholland nfsrpc_delegreturn(struct nfscldeleg *dp, struct ucred *cred,
43716ca35587Sdholland     struct nfsmount *nmp, NFSPROC_T *p, int syscred)
43726ca35587Sdholland {
43736ca35587Sdholland 	u_int32_t *tl;
43746ca35587Sdholland 	struct nfsrv_descript nfsd;
43756ca35587Sdholland 	struct nfsrv_descript *nd = &nfsd;
43766ca35587Sdholland 	int error;
43776ca35587Sdholland 
43786ca35587Sdholland 	nfscl_reqstart(nd, NFSPROC_DELEGRETURN, nmp, dp->nfsdl_fh,
43796ca35587Sdholland 	    dp->nfsdl_fhlen, NULL, NULL);
43806ca35587Sdholland 	NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID);
43816ca35587Sdholland 	if (NFSHASNFSV4N(nmp))
43826ca35587Sdholland 		*tl++ = 0;
43836ca35587Sdholland 	else
43846ca35587Sdholland 		*tl++ = dp->nfsdl_stateid.seqid;
43856ca35587Sdholland 	*tl++ = dp->nfsdl_stateid.other[0];
43866ca35587Sdholland 	*tl++ = dp->nfsdl_stateid.other[1];
43876ca35587Sdholland 	*tl = dp->nfsdl_stateid.other[2];
43886ca35587Sdholland 	if (syscred)
43896ca35587Sdholland 		nd->nd_flag |= ND_USEGSSNAME;
43906ca35587Sdholland 	error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
43916ca35587Sdholland 	    NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
43926ca35587Sdholland 	if (error)
43936ca35587Sdholland 		return (error);
43946ca35587Sdholland 	error = nd->nd_repstat;
43956ca35587Sdholland 	mbuf_freem(nd->nd_mrep);
43966ca35587Sdholland 	return (error);
43976ca35587Sdholland }
43986ca35587Sdholland 
43996ca35587Sdholland /*
44006ca35587Sdholland  * nfs getacl call.
44016ca35587Sdholland  */
44026ca35587Sdholland APPLESTATIC int
nfsrpc_getacl(vnode_t vp,struct ucred * cred,NFSPROC_T * p,struct acl * aclp,void * stuff)44036ca35587Sdholland nfsrpc_getacl(vnode_t vp, struct ucred *cred, NFSPROC_T *p,
44046ca35587Sdholland     struct acl *aclp, void *stuff)
44056ca35587Sdholland {
44066ca35587Sdholland 	struct nfsrv_descript nfsd, *nd = &nfsd;
44076ca35587Sdholland 	int error;
44086ca35587Sdholland 	nfsattrbit_t attrbits;
44096ca35587Sdholland 	struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
44106ca35587Sdholland 
44116ca35587Sdholland 	if (nfsrv_useacl == 0 || !NFSHASNFSV4(nmp))
44126ca35587Sdholland 		return (EOPNOTSUPP);
44136ca35587Sdholland 	NFSCL_REQSTART(nd, NFSPROC_GETACL, vp);
44146ca35587Sdholland 	NFSZERO_ATTRBIT(&attrbits);
44156ca35587Sdholland 	NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_ACL);
44166ca35587Sdholland 	(void) nfsrv_putattrbit(nd, &attrbits);
44176ca35587Sdholland 	error = nfscl_request(nd, vp, p, cred, stuff);
44186ca35587Sdholland 	if (error)
44196ca35587Sdholland 		return (error);
44206ca35587Sdholland 	if (!nd->nd_repstat)
44216ca35587Sdholland 		error = nfsv4_loadattr(nd, vp, NULL, NULL, NULL, 0, NULL,
44226ca35587Sdholland 		    NULL, NULL, NULL, aclp, 0, NULL, NULL, NULL, p, cred);
44236ca35587Sdholland 	else
44246ca35587Sdholland 		error = nd->nd_repstat;
44256ca35587Sdholland 	mbuf_freem(nd->nd_mrep);
44266ca35587Sdholland 	return (error);
44276ca35587Sdholland }
44286ca35587Sdholland 
44296ca35587Sdholland /*
44306ca35587Sdholland  * nfs setacl call.
44316ca35587Sdholland  */
44326ca35587Sdholland APPLESTATIC int
nfsrpc_setacl(vnode_t vp,struct ucred * cred,NFSPROC_T * p,struct acl * aclp,void * stuff)44336ca35587Sdholland nfsrpc_setacl(vnode_t vp, struct ucred *cred, NFSPROC_T *p,
44346ca35587Sdholland     struct acl *aclp, void *stuff)
44356ca35587Sdholland {
44366ca35587Sdholland 	int error;
44376ca35587Sdholland 	struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
44386ca35587Sdholland 
44396ca35587Sdholland 	if (nfsrv_useacl == 0 || !NFSHASNFSV4(nmp))
44406ca35587Sdholland 		return (EOPNOTSUPP);
44416ca35587Sdholland 	error = nfsrpc_setattr(vp, NULL, aclp, cred, p, NULL, NULL, stuff);
44426ca35587Sdholland 	return (error);
44436ca35587Sdholland }
44446ca35587Sdholland 
44456ca35587Sdholland /*
44466ca35587Sdholland  * nfs setacl call.
44476ca35587Sdholland  */
44486ca35587Sdholland static int
nfsrpc_setaclrpc(vnode_t vp,struct ucred * cred,NFSPROC_T * p,struct acl * aclp,nfsv4stateid_t * stateidp,void * stuff)44496ca35587Sdholland nfsrpc_setaclrpc(vnode_t vp, struct ucred *cred, NFSPROC_T *p,
44506ca35587Sdholland     struct acl *aclp, nfsv4stateid_t *stateidp, void *stuff)
44516ca35587Sdholland {
44526ca35587Sdholland 	struct nfsrv_descript nfsd, *nd = &nfsd;
44536ca35587Sdholland 	int error;
44546ca35587Sdholland 	nfsattrbit_t attrbits;
44556ca35587Sdholland 	struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
44566ca35587Sdholland 
44576ca35587Sdholland 	if (!NFSHASNFSV4(nmp))
44586ca35587Sdholland 		return (EOPNOTSUPP);
44596ca35587Sdholland 	NFSCL_REQSTART(nd, NFSPROC_SETACL, vp);
44606ca35587Sdholland 	nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID);
44616ca35587Sdholland 	NFSZERO_ATTRBIT(&attrbits);
44626ca35587Sdholland 	NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_ACL);
44636ca35587Sdholland 	(void) nfsv4_fillattr(nd, vnode_mount(vp), vp, aclp, NULL, NULL, 0,
44646ca35587Sdholland 	    &attrbits, NULL, NULL, 0, 0, 0, 0, (uint64_t)0);
44656ca35587Sdholland 	error = nfscl_request(nd, vp, p, cred, stuff);
44666ca35587Sdholland 	if (error)
44676ca35587Sdholland 		return (error);
44686ca35587Sdholland 	/* Don't care about the pre/postop attributes */
44696ca35587Sdholland 	mbuf_freem(nd->nd_mrep);
44706ca35587Sdholland 	return (nd->nd_repstat);
44716ca35587Sdholland }
44726ca35587Sdholland 
44736ca35587Sdholland /*
44746ca35587Sdholland  * Do the NFSv4.1 Exchange ID.
44756ca35587Sdholland  */
44766ca35587Sdholland int
nfsrpc_exchangeid(struct nfsmount * nmp,struct nfsclclient * clp,struct nfssockreq * nrp,uint32_t exchflags,struct nfsclds ** dspp,struct ucred * cred,NFSPROC_T * p)44776ca35587Sdholland nfsrpc_exchangeid(struct nfsmount *nmp, struct nfsclclient *clp,
44786ca35587Sdholland     struct nfssockreq *nrp, uint32_t exchflags, struct nfsclds **dspp,
44796ca35587Sdholland     struct ucred *cred, NFSPROC_T *p)
44806ca35587Sdholland {
44816ca35587Sdholland 	uint32_t *tl, v41flags;
44826ca35587Sdholland 	struct nfsrv_descript nfsd;
44836ca35587Sdholland 	struct nfsrv_descript *nd = &nfsd;
44846ca35587Sdholland 	struct nfsclds *dsp;
44856ca35587Sdholland 	struct timespec verstime;
44866ca35587Sdholland 	int error, len;
44876ca35587Sdholland 
44886ca35587Sdholland 	*dspp = NULL;
44896ca35587Sdholland 	nfscl_reqstart(nd, NFSPROC_EXCHANGEID, nmp, NULL, 0, NULL, NULL);
44906ca35587Sdholland 	NFSM_BUILD(tl, uint32_t *, 2 * NFSX_UNSIGNED);
44916ca35587Sdholland 	*tl++ = txdr_unsigned(nfsboottime.tv_sec);	/* Client owner */
44926ca35587Sdholland 	*tl = txdr_unsigned(clp->nfsc_rev);
44936ca35587Sdholland 	(void) nfsm_strtom(nd, clp->nfsc_id, clp->nfsc_idlen);
44946ca35587Sdholland 
44956ca35587Sdholland 	NFSM_BUILD(tl, uint32_t *, 3 * NFSX_UNSIGNED);
44966ca35587Sdholland 	*tl++ = txdr_unsigned(exchflags);
44976ca35587Sdholland 	*tl++ = txdr_unsigned(NFSV4EXCH_SP4NONE);
44986ca35587Sdholland 
44996ca35587Sdholland 	/* Set the implementation id4 */
45006ca35587Sdholland 	*tl = txdr_unsigned(1);
45016ca35587Sdholland 	(void) nfsm_strtom(nd, "freebsd.org", strlen("freebsd.org"));
45026ca35587Sdholland 	(void) nfsm_strtom(nd, version, strlen(version));
45036ca35587Sdholland 	NFSM_BUILD(tl, uint32_t *, NFSX_V4TIME);
45046ca35587Sdholland 	verstime.tv_sec = 1293840000;		/* Jan 1, 2011 */
45056ca35587Sdholland 	verstime.tv_nsec = 0;
45066ca35587Sdholland 	txdr_nfsv4time(&verstime, tl);
45076ca35587Sdholland 	nd->nd_flag |= ND_USEGSSNAME;
45086ca35587Sdholland 	error = newnfs_request(nd, nmp, NULL, nrp, NULL, p, cred,
45096ca35587Sdholland 	    NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
45106ca35587Sdholland 	NFSCL_DEBUG(1, "exchangeid err=%d reps=%d\n", error,
45116ca35587Sdholland 	    (int)nd->nd_repstat);
45126ca35587Sdholland 	if (error != 0)
45136ca35587Sdholland 		return (error);
45146ca35587Sdholland 	if (nd->nd_repstat == 0) {
45156ca35587Sdholland 		NFSM_DISSECT(tl, uint32_t *, 6 * NFSX_UNSIGNED + NFSX_HYPER);
45166ca35587Sdholland 		len = fxdr_unsigned(int, *(tl + 7));
45176ca35587Sdholland 		if (len < 0 || len > NFSV4_OPAQUELIMIT) {
45186ca35587Sdholland 			error = NFSERR_BADXDR;
45196ca35587Sdholland 			goto nfsmout;
45206ca35587Sdholland 		}
45216ca35587Sdholland 		dsp = malloc(sizeof(struct nfsclds) + len, M_NFSCLDS,
45226ca35587Sdholland 		    M_WAITOK | M_ZERO);
45236ca35587Sdholland 		dsp->nfsclds_expire = NFSD_MONOSEC + clp->nfsc_renew;
45246ca35587Sdholland 		dsp->nfsclds_servownlen = len;
45256ca35587Sdholland 		dsp->nfsclds_sess.nfsess_clientid.lval[0] = *tl++;
45266ca35587Sdholland 		dsp->nfsclds_sess.nfsess_clientid.lval[1] = *tl++;
45276ca35587Sdholland 		dsp->nfsclds_sess.nfsess_sequenceid =
45286ca35587Sdholland 		    fxdr_unsigned(uint32_t, *tl++);
45296ca35587Sdholland 		v41flags = fxdr_unsigned(uint32_t, *tl);
45306ca35587Sdholland 		if ((v41flags & NFSV4EXCH_USEPNFSMDS) != 0 &&
45316ca35587Sdholland 		    NFSHASPNFSOPT(nmp)) {
45326ca35587Sdholland 			NFSCL_DEBUG(1, "set PNFS\n");
45336ca35587Sdholland 			NFSLOCKMNT(nmp);
45346ca35587Sdholland 			nmp->nm_state |= NFSSTA_PNFS;
45356ca35587Sdholland 			NFSUNLOCKMNT(nmp);
45366ca35587Sdholland 			dsp->nfsclds_flags |= NFSCLDS_MDS;
45376ca35587Sdholland 		}
45386ca35587Sdholland 		if ((v41flags & NFSV4EXCH_USEPNFSDS) != 0)
45396ca35587Sdholland 			dsp->nfsclds_flags |= NFSCLDS_DS;
45406ca35587Sdholland 		if (len > 0)
45416ca35587Sdholland 			nd->nd_repstat = nfsrv_mtostr(nd,
45426ca35587Sdholland 			    dsp->nfsclds_serverown, len);
45436ca35587Sdholland 		if (nd->nd_repstat == 0) {
45446ca35587Sdholland 			mtx_init(&dsp->nfsclds_mtx, "nfsds", NULL, MTX_DEF);
45456ca35587Sdholland 			mtx_init(&dsp->nfsclds_sess.nfsess_mtx, "nfssession",
45466ca35587Sdholland 			    NULL, MTX_DEF);
45476ca35587Sdholland 			nfscl_initsessionslots(&dsp->nfsclds_sess);
45486ca35587Sdholland 			*dspp = dsp;
45496ca35587Sdholland 		} else
45506ca35587Sdholland 			free(dsp, M_NFSCLDS);
45516ca35587Sdholland 	}
45526ca35587Sdholland 	error = nd->nd_repstat;
45536ca35587Sdholland nfsmout:
45546ca35587Sdholland 	mbuf_freem(nd->nd_mrep);
45556ca35587Sdholland 	return (error);
45566ca35587Sdholland }
45576ca35587Sdholland 
45586ca35587Sdholland /*
45596ca35587Sdholland  * Do the NFSv4.1 Create Session.
45606ca35587Sdholland  */
45616ca35587Sdholland int
nfsrpc_createsession(struct nfsmount * nmp,struct nfsclsession * sep,struct nfssockreq * nrp,uint32_t sequenceid,int mds,struct ucred * cred,NFSPROC_T * p)45626ca35587Sdholland nfsrpc_createsession(struct nfsmount *nmp, struct nfsclsession *sep,
45636ca35587Sdholland     struct nfssockreq *nrp, uint32_t sequenceid, int mds, struct ucred *cred,
45646ca35587Sdholland     NFSPROC_T *p)
45656ca35587Sdholland {
45666ca35587Sdholland 	uint32_t crflags, *tl;
45676ca35587Sdholland 	struct nfsrv_descript nfsd;
45686ca35587Sdholland 	struct nfsrv_descript *nd = &nfsd;
45696ca35587Sdholland 	int error, irdcnt;
45706ca35587Sdholland 
45716ca35587Sdholland 	nfscl_reqstart(nd, NFSPROC_CREATESESSION, nmp, NULL, 0, NULL, NULL);
45726ca35587Sdholland 	NFSM_BUILD(tl, uint32_t *, 4 * NFSX_UNSIGNED);
45736ca35587Sdholland 	*tl++ = sep->nfsess_clientid.lval[0];
45746ca35587Sdholland 	*tl++ = sep->nfsess_clientid.lval[1];
45756ca35587Sdholland 	*tl++ = txdr_unsigned(sequenceid);
45766ca35587Sdholland 	crflags = (NFSMNT_RDONLY(nmp->nm_mountp) ? 0 : NFSV4CRSESS_PERSIST);
45776ca35587Sdholland 	if (nfscl_enablecallb != 0 && nfs_numnfscbd > 0)
45786ca35587Sdholland 		crflags |= NFSV4CRSESS_CONNBACKCHAN;
45796ca35587Sdholland 	*tl = txdr_unsigned(crflags);
45806ca35587Sdholland 
45816ca35587Sdholland 	/* Fill in fore channel attributes. */
45826ca35587Sdholland 	NFSM_BUILD(tl, uint32_t *, 7 * NFSX_UNSIGNED);
45836ca35587Sdholland 	*tl++ = 0;				/* Header pad size */
45846ca35587Sdholland 	*tl++ = txdr_unsigned(100000);		/* Max request size */
45856ca35587Sdholland 	*tl++ = txdr_unsigned(100000);		/* Max response size */
45866ca35587Sdholland 	*tl++ = txdr_unsigned(4096);		/* Max response size cached */
45876ca35587Sdholland 	*tl++ = txdr_unsigned(20);		/* Max operations */
45886ca35587Sdholland 	*tl++ = txdr_unsigned(64);		/* Max slots */
45896ca35587Sdholland 	*tl = 0;				/* No rdma ird */
45906ca35587Sdholland 
45916ca35587Sdholland 	/* Fill in back channel attributes. */
45926ca35587Sdholland 	NFSM_BUILD(tl, uint32_t *, 7 * NFSX_UNSIGNED);
45936ca35587Sdholland 	*tl++ = 0;				/* Header pad size */
45946ca35587Sdholland 	*tl++ = txdr_unsigned(10000);		/* Max request size */
45956ca35587Sdholland 	*tl++ = txdr_unsigned(10000);		/* Max response size */
45966ca35587Sdholland 	*tl++ = txdr_unsigned(4096);		/* Max response size cached */
45976ca35587Sdholland 	*tl++ = txdr_unsigned(4);		/* Max operations */
45986ca35587Sdholland 	*tl++ = txdr_unsigned(NFSV4_CBSLOTS);	/* Max slots */
45996ca35587Sdholland 	*tl = 0;				/* No rdma ird */
46006ca35587Sdholland 
46016ca35587Sdholland 	NFSM_BUILD(tl, uint32_t *, 8 * NFSX_UNSIGNED);
46026ca35587Sdholland 	*tl++ = txdr_unsigned(NFS_CALLBCKPROG);	/* Call back prog # */
46036ca35587Sdholland 
46046ca35587Sdholland 	/* Allow AUTH_SYS callbacks as uid, gid == 0. */
46056ca35587Sdholland 	*tl++ = txdr_unsigned(1);		/* Auth_sys only */
46066ca35587Sdholland 	*tl++ = txdr_unsigned(AUTH_SYS);	/* AUTH_SYS type */
46076ca35587Sdholland 	*tl++ = txdr_unsigned(nfsboottime.tv_sec); /* time stamp */
46086ca35587Sdholland 	*tl++ = 0;				/* Null machine name */
46096ca35587Sdholland 	*tl++ = 0;				/* Uid == 0 */
46106ca35587Sdholland 	*tl++ = 0;				/* Gid == 0 */
46116ca35587Sdholland 	*tl = 0;				/* No additional gids */
46126ca35587Sdholland 	nd->nd_flag |= ND_USEGSSNAME;
46136ca35587Sdholland 	error = newnfs_request(nd, nmp, NULL, nrp, NULL, p, cred, NFS_PROG,
46146ca35587Sdholland 	    NFS_VER4, NULL, 1, NULL, NULL);
46156ca35587Sdholland 	if (error != 0)
46166ca35587Sdholland 		return (error);
46176ca35587Sdholland 	if (nd->nd_repstat == 0) {
46186ca35587Sdholland 		NFSM_DISSECT(tl, uint32_t *, NFSX_V4SESSIONID +
46196ca35587Sdholland 		    2 * NFSX_UNSIGNED);
46206ca35587Sdholland 		bcopy(tl, sep->nfsess_sessionid, NFSX_V4SESSIONID);
46216ca35587Sdholland 		tl += NFSX_V4SESSIONID / NFSX_UNSIGNED;
46226ca35587Sdholland 		sep->nfsess_sequenceid = fxdr_unsigned(uint32_t, *tl++);
46236ca35587Sdholland 		crflags = fxdr_unsigned(uint32_t, *tl);
46246ca35587Sdholland 		if ((crflags & NFSV4CRSESS_PERSIST) != 0 && mds != 0) {
46256ca35587Sdholland 			NFSLOCKMNT(nmp);
46266ca35587Sdholland 			nmp->nm_state |= NFSSTA_SESSPERSIST;
46276ca35587Sdholland 			NFSUNLOCKMNT(nmp);
46286ca35587Sdholland 		}
46296ca35587Sdholland 
46306ca35587Sdholland 		/* Get the fore channel slot count. */
46316ca35587Sdholland 		NFSM_DISSECT(tl, uint32_t *, 7 * NFSX_UNSIGNED);
46326ca35587Sdholland 		tl += 3;		/* Skip the other counts. */
46336ca35587Sdholland 		sep->nfsess_maxcache = fxdr_unsigned(int, *tl++);
46346ca35587Sdholland 		tl++;
46356ca35587Sdholland 		sep->nfsess_foreslots = fxdr_unsigned(uint16_t, *tl++);
46366ca35587Sdholland 		NFSCL_DEBUG(4, "fore slots=%d\n", (int)sep->nfsess_foreslots);
46376ca35587Sdholland 		irdcnt = fxdr_unsigned(int, *tl);
46386ca35587Sdholland 		if (irdcnt > 0)
46396ca35587Sdholland 			NFSM_DISSECT(tl, uint32_t *, irdcnt * NFSX_UNSIGNED);
46406ca35587Sdholland 
46416ca35587Sdholland 		/* and the back channel slot count. */
46426ca35587Sdholland 		NFSM_DISSECT(tl, uint32_t *, 7 * NFSX_UNSIGNED);
46436ca35587Sdholland 		tl += 5;
46446ca35587Sdholland 		sep->nfsess_backslots = fxdr_unsigned(uint16_t, *tl);
46456ca35587Sdholland 		NFSCL_DEBUG(4, "back slots=%d\n", (int)sep->nfsess_backslots);
46466ca35587Sdholland 	}
46476ca35587Sdholland 	error = nd->nd_repstat;
46486ca35587Sdholland nfsmout:
46496ca35587Sdholland 	mbuf_freem(nd->nd_mrep);
46506ca35587Sdholland 	return (error);
46516ca35587Sdholland }
46526ca35587Sdholland 
46536ca35587Sdholland /*
46546ca35587Sdholland  * Do the NFSv4.1 Destroy Session.
46556ca35587Sdholland  */
46566ca35587Sdholland int
nfsrpc_destroysession(struct nfsmount * nmp,struct nfsclclient * clp,struct ucred * cred,NFSPROC_T * p)46576ca35587Sdholland nfsrpc_destroysession(struct nfsmount *nmp, struct nfsclclient *clp,
46586ca35587Sdholland     struct ucred *cred, NFSPROC_T *p)
46596ca35587Sdholland {
46606ca35587Sdholland 	uint32_t *tl;
46616ca35587Sdholland 	struct nfsrv_descript nfsd;
46626ca35587Sdholland 	struct nfsrv_descript *nd = &nfsd;
46636ca35587Sdholland 	int error;
46646ca35587Sdholland 
46656ca35587Sdholland 	nfscl_reqstart(nd, NFSPROC_DESTROYSESSION, nmp, NULL, 0, NULL, NULL);
46666ca35587Sdholland 	NFSM_BUILD(tl, uint32_t *, NFSX_V4SESSIONID);
46676ca35587Sdholland 	bcopy(NFSMNT_MDSSESSION(nmp)->nfsess_sessionid, tl, NFSX_V4SESSIONID);
46686ca35587Sdholland 	nd->nd_flag |= ND_USEGSSNAME;
46696ca35587Sdholland 	error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
46706ca35587Sdholland 	    NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
46716ca35587Sdholland 	if (error != 0)
46726ca35587Sdholland 		return (error);
46736ca35587Sdholland 	error = nd->nd_repstat;
46746ca35587Sdholland 	mbuf_freem(nd->nd_mrep);
46756ca35587Sdholland 	return (error);
46766ca35587Sdholland }
46776ca35587Sdholland 
46786ca35587Sdholland /*
46796ca35587Sdholland  * Do the NFSv4.1 Destroy Client.
46806ca35587Sdholland  */
46816ca35587Sdholland int
nfsrpc_destroyclient(struct nfsmount * nmp,struct nfsclclient * clp,struct ucred * cred,NFSPROC_T * p)46826ca35587Sdholland nfsrpc_destroyclient(struct nfsmount *nmp, struct nfsclclient *clp,
46836ca35587Sdholland     struct ucred *cred, NFSPROC_T *p)
46846ca35587Sdholland {
46856ca35587Sdholland 	uint32_t *tl;
46866ca35587Sdholland 	struct nfsrv_descript nfsd;
46876ca35587Sdholland 	struct nfsrv_descript *nd = &nfsd;
46886ca35587Sdholland 	int error;
46896ca35587Sdholland 
46906ca35587Sdholland 	nfscl_reqstart(nd, NFSPROC_DESTROYCLIENT, nmp, NULL, 0, NULL, NULL);
46916ca35587Sdholland 	NFSM_BUILD(tl, uint32_t *, 2 * NFSX_UNSIGNED);
46926ca35587Sdholland 	*tl++ = NFSMNT_MDSSESSION(nmp)->nfsess_clientid.lval[0];
46936ca35587Sdholland 	*tl = NFSMNT_MDSSESSION(nmp)->nfsess_clientid.lval[1];
46946ca35587Sdholland 	nd->nd_flag |= ND_USEGSSNAME;
46956ca35587Sdholland 	error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
46966ca35587Sdholland 	    NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
46976ca35587Sdholland 	if (error != 0)
46986ca35587Sdholland 		return (error);
46996ca35587Sdholland 	error = nd->nd_repstat;
47006ca35587Sdholland 	mbuf_freem(nd->nd_mrep);
47016ca35587Sdholland 	return (error);
47026ca35587Sdholland }
47036ca35587Sdholland 
47046ca35587Sdholland /*
47056ca35587Sdholland  * Do the NFSv4.1 LayoutGet.
47066ca35587Sdholland  */
47076ca35587Sdholland int
nfsrpc_layoutget(struct nfsmount * nmp,uint8_t * fhp,int fhlen,int iomode,uint64_t offset,uint64_t len,uint64_t minlen,int layoutlen,nfsv4stateid_t * stateidp,int * retonclosep,struct nfsclflayouthead * flhp,struct ucred * cred,NFSPROC_T * p,void * stuff)47086ca35587Sdholland nfsrpc_layoutget(struct nfsmount *nmp, uint8_t *fhp, int fhlen, int iomode,
47096ca35587Sdholland     uint64_t offset, uint64_t len, uint64_t minlen, int layoutlen,
47106ca35587Sdholland     nfsv4stateid_t *stateidp, int *retonclosep, struct nfsclflayouthead *flhp,
47116ca35587Sdholland     struct ucred *cred, NFSPROC_T *p, void *stuff)
47126ca35587Sdholland {
47136ca35587Sdholland 	uint32_t *tl;
47146ca35587Sdholland 	struct nfsrv_descript nfsd, *nd = &nfsd;
47156ca35587Sdholland 	struct nfsfh *nfhp;
47166ca35587Sdholland 	struct nfsclflayout *flp, *prevflp, *tflp;
47176ca35587Sdholland 	int cnt, error, gotiomode, fhcnt, nfhlen, i, j;
47186ca35587Sdholland 	uint8_t *cp;
47196ca35587Sdholland 	uint64_t retlen;
47206ca35587Sdholland 
47216ca35587Sdholland 	flp = NULL;
47226ca35587Sdholland 	gotiomode = -1;
47236ca35587Sdholland 	nfscl_reqstart(nd, NFSPROC_LAYOUTGET, nmp, fhp, fhlen, NULL, NULL);
47246ca35587Sdholland 	NFSM_BUILD(tl, uint32_t *, 4 * NFSX_UNSIGNED + 3 * NFSX_HYPER +
47256ca35587Sdholland 	    NFSX_STATEID);
47266ca35587Sdholland 	*tl++ = newnfs_false;		/* Don't signal availability. */
47276ca35587Sdholland 	*tl++ = txdr_unsigned(NFSLAYOUT_NFSV4_1_FILES);
47286ca35587Sdholland 	*tl++ = txdr_unsigned(iomode);
47296ca35587Sdholland 	txdr_hyper(offset, tl);
47306ca35587Sdholland 	tl += 2;
47316ca35587Sdholland 	txdr_hyper(len, tl);
47326ca35587Sdholland 	tl += 2;
47336ca35587Sdholland 	txdr_hyper(minlen, tl);
47346ca35587Sdholland 	tl += 2;
47356ca35587Sdholland 	*tl++ = txdr_unsigned(stateidp->seqid);
47366ca35587Sdholland 	NFSCL_DEBUG(4, "layget seq=%d\n", (int)stateidp->seqid);
47376ca35587Sdholland 	*tl++ = stateidp->other[0];
47386ca35587Sdholland 	*tl++ = stateidp->other[1];
47396ca35587Sdholland 	*tl++ = stateidp->other[2];
47406ca35587Sdholland 	*tl = txdr_unsigned(layoutlen);
47416ca35587Sdholland 	nd->nd_flag |= ND_USEGSSNAME;
47426ca35587Sdholland 	error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
47436ca35587Sdholland 	    NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
47446ca35587Sdholland 	if (error != 0)
47456ca35587Sdholland 		return (error);
47466ca35587Sdholland 	if (nd->nd_repstat == 0) {
47476ca35587Sdholland 		NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED + NFSX_STATEID);
47486ca35587Sdholland 		if (*tl++ != 0)
47496ca35587Sdholland 			*retonclosep = 1;
47506ca35587Sdholland 		else
47516ca35587Sdholland 			*retonclosep = 0;
47526ca35587Sdholland 		stateidp->seqid = fxdr_unsigned(uint32_t, *tl++);
47536ca35587Sdholland 		NFSCL_DEBUG(4, "retoncls=%d stseq=%d\n", *retonclosep,
47546ca35587Sdholland 		    (int)stateidp->seqid);
47556ca35587Sdholland 		stateidp->other[0] = *tl++;
47566ca35587Sdholland 		stateidp->other[1] = *tl++;
47576ca35587Sdholland 		stateidp->other[2] = *tl++;
47586ca35587Sdholland 		cnt = fxdr_unsigned(int, *tl);
47596ca35587Sdholland 		NFSCL_DEBUG(4, "layg cnt=%d\n", cnt);
47606ca35587Sdholland 		if (cnt <= 0 || cnt > 10000) {
47616ca35587Sdholland 			/* Don't accept more than 10000 layouts in reply. */
47626ca35587Sdholland 			error = NFSERR_BADXDR;
47636ca35587Sdholland 			goto nfsmout;
47646ca35587Sdholland 		}
47656ca35587Sdholland 		for (i = 0; i < cnt; i++) {
47666ca35587Sdholland 			/* Dissect all the way to the file handle cnt. */
47676ca35587Sdholland 			NFSM_DISSECT(tl, uint32_t *, 3 * NFSX_HYPER +
47686ca35587Sdholland 			    6 * NFSX_UNSIGNED + NFSX_V4DEVICEID);
47696ca35587Sdholland 			fhcnt = fxdr_unsigned(int, *(tl + 11 +
47706ca35587Sdholland 			    NFSX_V4DEVICEID / NFSX_UNSIGNED));
47716ca35587Sdholland 			NFSCL_DEBUG(4, "fhcnt=%d\n", fhcnt);
47726ca35587Sdholland 			if (fhcnt < 0 || fhcnt > 100) {
47736ca35587Sdholland 				/* Don't accept more than 100 file handles. */
47746ca35587Sdholland 				error = NFSERR_BADXDR;
47756ca35587Sdholland 				goto nfsmout;
47766ca35587Sdholland 			}
47776ca35587Sdholland 			if (fhcnt > 1)
47786ca35587Sdholland 				flp = malloc(sizeof(*flp) + (fhcnt - 1) *
47796ca35587Sdholland 				    sizeof(struct nfsfh *),
47806ca35587Sdholland 				    M_NFSFLAYOUT, M_WAITOK);
47816ca35587Sdholland 			else
47826ca35587Sdholland 				flp = malloc(sizeof(*flp),
47836ca35587Sdholland 				    M_NFSFLAYOUT, M_WAITOK);
47846ca35587Sdholland 			flp->nfsfl_flags = 0;
47856ca35587Sdholland 			flp->nfsfl_fhcnt = 0;
47866ca35587Sdholland 			flp->nfsfl_devp = NULL;
47876ca35587Sdholland 			flp->nfsfl_off = fxdr_hyper(tl); tl += 2;
47886ca35587Sdholland 			retlen = fxdr_hyper(tl); tl += 2;
47896ca35587Sdholland 			if (flp->nfsfl_off + retlen < flp->nfsfl_off)
47906ca35587Sdholland 				flp->nfsfl_end = UINT64_MAX - flp->nfsfl_off;
47916ca35587Sdholland 			else
47926ca35587Sdholland 				flp->nfsfl_end = flp->nfsfl_off + retlen;
47936ca35587Sdholland 			flp->nfsfl_iomode = fxdr_unsigned(int, *tl++);
47946ca35587Sdholland 			if (gotiomode == -1)
47956ca35587Sdholland 				gotiomode = flp->nfsfl_iomode;
47966ca35587Sdholland 			NFSCL_DEBUG(4, "layg reqiom=%d retiom=%d\n", iomode,
47976ca35587Sdholland 			    (int)flp->nfsfl_iomode);
47986ca35587Sdholland 			if (fxdr_unsigned(int, *tl++) !=
47996ca35587Sdholland 			    NFSLAYOUT_NFSV4_1_FILES) {
48006ca35587Sdholland 				printf("NFSv4.1: got non-files layout\n");
48016ca35587Sdholland 				error = NFSERR_BADXDR;
48026ca35587Sdholland 				goto nfsmout;
48036ca35587Sdholland 			}
48046ca35587Sdholland 			NFSBCOPY(++tl, flp->nfsfl_dev, NFSX_V4DEVICEID);
48056ca35587Sdholland 			tl += (NFSX_V4DEVICEID / NFSX_UNSIGNED);
48066ca35587Sdholland 			flp->nfsfl_util = fxdr_unsigned(uint32_t, *tl++);
48076ca35587Sdholland 			NFSCL_DEBUG(4, "flutil=0x%x\n", flp->nfsfl_util);
48086ca35587Sdholland 			flp->nfsfl_stripe1 = fxdr_unsigned(uint32_t, *tl++);
48096ca35587Sdholland 			flp->nfsfl_patoff = fxdr_hyper(tl); tl += 2;
48106ca35587Sdholland 			if (fxdr_unsigned(int, *tl) != fhcnt) {
48116ca35587Sdholland 				printf("EEK! bad fhcnt\n");
48126ca35587Sdholland 				error = NFSERR_BADXDR;
48136ca35587Sdholland 				goto nfsmout;
48146ca35587Sdholland 			}
48156ca35587Sdholland 			for (j = 0; j < fhcnt; j++) {
48166ca35587Sdholland 				NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
48176ca35587Sdholland 				nfhlen = fxdr_unsigned(int, *tl);
48186ca35587Sdholland 				if (nfhlen <= 0 || nfhlen > NFSX_V4FHMAX) {
48196ca35587Sdholland 					error = NFSERR_BADXDR;
48206ca35587Sdholland 					goto nfsmout;
48216ca35587Sdholland 				}
48226ca35587Sdholland 				nfhp = malloc(sizeof(*nfhp) + nfhlen - 1,
48236ca35587Sdholland 				    M_NFSFH, M_WAITOK);
48246ca35587Sdholland 				flp->nfsfl_fh[j] = nfhp;
48256ca35587Sdholland 				flp->nfsfl_fhcnt++;
48266ca35587Sdholland 				nfhp->nfh_len = nfhlen;
48276ca35587Sdholland 				NFSM_DISSECT(cp, uint8_t *, NFSM_RNDUP(nfhlen));
48286ca35587Sdholland 				NFSBCOPY(cp, nfhp->nfh_fh, nfhlen);
48296ca35587Sdholland 			}
48306ca35587Sdholland 			if (flp->nfsfl_iomode == gotiomode) {
48316ca35587Sdholland 				/* Keep the list in increasing offset order. */
48326ca35587Sdholland 				tflp = LIST_FIRST(flhp);
48336ca35587Sdholland 				prevflp = NULL;
48346ca35587Sdholland 				while (tflp != NULL &&
48356ca35587Sdholland 				    tflp->nfsfl_off < flp->nfsfl_off) {
48366ca35587Sdholland 					prevflp = tflp;
48376ca35587Sdholland 					tflp = LIST_NEXT(tflp, nfsfl_list);
48386ca35587Sdholland 				}
48396ca35587Sdholland 				if (prevflp == NULL)
48406ca35587Sdholland 					LIST_INSERT_HEAD(flhp, flp, nfsfl_list);
48416ca35587Sdholland 				else
48426ca35587Sdholland 					LIST_INSERT_AFTER(prevflp, flp,
48436ca35587Sdholland 					    nfsfl_list);
48446ca35587Sdholland 			} else {
48456ca35587Sdholland 				printf("nfscl_layoutget(): got wrong iomode\n");
48466ca35587Sdholland 				nfscl_freeflayout(flp);
48476ca35587Sdholland 			}
48486ca35587Sdholland 			flp = NULL;
48496ca35587Sdholland 		}
48506ca35587Sdholland 	}
48516ca35587Sdholland 	if (nd->nd_repstat != 0 && error == 0)
48526ca35587Sdholland 		error = nd->nd_repstat;
48536ca35587Sdholland nfsmout:
48546ca35587Sdholland 	if (error != 0 && flp != NULL)
48556ca35587Sdholland 		nfscl_freeflayout(flp);
48566ca35587Sdholland 	mbuf_freem(nd->nd_mrep);
48576ca35587Sdholland 	return (error);
48586ca35587Sdholland }
48596ca35587Sdholland 
48606ca35587Sdholland /*
48616ca35587Sdholland  * Do the NFSv4.1 Get Device Info.
48626ca35587Sdholland  */
48636ca35587Sdholland int
nfsrpc_getdeviceinfo(struct nfsmount * nmp,uint8_t * deviceid,int layouttype,uint32_t * notifybitsp,struct nfscldevinfo ** ndip,struct ucred * cred,NFSPROC_T * p)48646ca35587Sdholland nfsrpc_getdeviceinfo(struct nfsmount *nmp, uint8_t *deviceid, int layouttype,
48656ca35587Sdholland     uint32_t *notifybitsp, struct nfscldevinfo **ndip, struct ucred *cred,
48666ca35587Sdholland     NFSPROC_T *p)
48676ca35587Sdholland {
48686ca35587Sdholland 	uint32_t cnt, *tl;
48696ca35587Sdholland 	struct nfsrv_descript nfsd;
48706ca35587Sdholland 	struct nfsrv_descript *nd = &nfsd;
48716ca35587Sdholland 	struct sockaddr_storage ss;
48726ca35587Sdholland 	struct nfsclds *dsp = NULL, **dspp;
48736ca35587Sdholland 	struct nfscldevinfo *ndi;
48746ca35587Sdholland 	int addrcnt, bitcnt, error, i, isudp, j, pos, safilled, stripecnt;
48756ca35587Sdholland 	uint8_t stripeindex;
48766ca35587Sdholland 
48776ca35587Sdholland 	*ndip = NULL;
48786ca35587Sdholland 	ndi = NULL;
48796ca35587Sdholland 	nfscl_reqstart(nd, NFSPROC_GETDEVICEINFO, nmp, NULL, 0, NULL, NULL);
48806ca35587Sdholland 	NFSM_BUILD(tl, uint32_t *, NFSX_V4DEVICEID + 3 * NFSX_UNSIGNED);
48816ca35587Sdholland 	NFSBCOPY(deviceid, tl, NFSX_V4DEVICEID);
48826ca35587Sdholland 	tl += (NFSX_V4DEVICEID / NFSX_UNSIGNED);
48836ca35587Sdholland 	*tl++ = txdr_unsigned(layouttype);
48846ca35587Sdholland 	*tl++ = txdr_unsigned(100000);
48856ca35587Sdholland 	if (notifybitsp != NULL && *notifybitsp != 0) {
48866ca35587Sdholland 		*tl = txdr_unsigned(1);		/* One word of bits. */
48876ca35587Sdholland 		NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
48886ca35587Sdholland 		*tl = txdr_unsigned(*notifybitsp);
48896ca35587Sdholland 	} else
48906ca35587Sdholland 		*tl = txdr_unsigned(0);
48916ca35587Sdholland 	nd->nd_flag |= ND_USEGSSNAME;
48926ca35587Sdholland 	error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
48936ca35587Sdholland 	    NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
48946ca35587Sdholland 	if (error != 0)
48956ca35587Sdholland 		return (error);
48966ca35587Sdholland 	if (nd->nd_repstat == 0) {
48976ca35587Sdholland 		NFSM_DISSECT(tl, uint32_t *, 3 * NFSX_UNSIGNED);
48986ca35587Sdholland 		if (layouttype != fxdr_unsigned(int, *tl++))
48996ca35587Sdholland 			printf("EEK! devinfo layout type not same!\n");
49006ca35587Sdholland 		stripecnt = fxdr_unsigned(int, *++tl);
49016ca35587Sdholland 		NFSCL_DEBUG(4, "stripecnt=%d\n", stripecnt);
49026ca35587Sdholland 		if (stripecnt < 1 || stripecnt > 4096) {
49036ca35587Sdholland 			printf("NFS devinfo stripecnt %d: out of range\n",
49046ca35587Sdholland 			    stripecnt);
49056ca35587Sdholland 			error = NFSERR_BADXDR;
49066ca35587Sdholland 			goto nfsmout;
49076ca35587Sdholland 		}
49086ca35587Sdholland 		NFSM_DISSECT(tl, uint32_t *, (stripecnt + 1) * NFSX_UNSIGNED);
49096ca35587Sdholland 		addrcnt = fxdr_unsigned(int, *(tl + stripecnt));
49106ca35587Sdholland 		NFSCL_DEBUG(4, "addrcnt=%d\n", addrcnt);
49116ca35587Sdholland 		if (addrcnt < 1 || addrcnt > 128) {
49126ca35587Sdholland 			printf("NFS devinfo addrcnt %d: out of range\n",
49136ca35587Sdholland 			    addrcnt);
49146ca35587Sdholland 			error = NFSERR_BADXDR;
49156ca35587Sdholland 			goto nfsmout;
49166ca35587Sdholland 		}
49176ca35587Sdholland 
49186ca35587Sdholland 		/*
49196ca35587Sdholland 		 * Now we know how many stripe indices and addresses, so
49206ca35587Sdholland 		 * we can allocate the structure the correct size.
49216ca35587Sdholland 		 */
49226ca35587Sdholland 		i = (stripecnt * sizeof(uint8_t)) / sizeof(struct nfsclds *)
49236ca35587Sdholland 		    + 1;
49246ca35587Sdholland 		NFSCL_DEBUG(4, "stripeindices=%d\n", i);
49256ca35587Sdholland 		ndi = malloc(sizeof(*ndi) + (addrcnt + i) *
49266ca35587Sdholland 		    sizeof(struct nfsclds *), M_NFSDEVINFO, M_WAITOK | M_ZERO);
49276ca35587Sdholland 		NFSBCOPY(deviceid, ndi->nfsdi_deviceid, NFSX_V4DEVICEID);
49286ca35587Sdholland 		ndi->nfsdi_refcnt = 0;
49296ca35587Sdholland 		ndi->nfsdi_stripecnt = stripecnt;
49306ca35587Sdholland 		ndi->nfsdi_addrcnt = addrcnt;
49316ca35587Sdholland 		/* Fill in the stripe indices. */
49326ca35587Sdholland 		for (i = 0; i < stripecnt; i++) {
49336ca35587Sdholland 			stripeindex = fxdr_unsigned(uint8_t, *tl++);
49346ca35587Sdholland 			NFSCL_DEBUG(4, "stripeind=%d\n", stripeindex);
49356ca35587Sdholland 			if (stripeindex >= addrcnt) {
49366ca35587Sdholland 				printf("NFS devinfo stripeindex %d: too big\n",
49376ca35587Sdholland 				    (int)stripeindex);
49386ca35587Sdholland 				error = NFSERR_BADXDR;
49396ca35587Sdholland 				goto nfsmout;
49406ca35587Sdholland 			}
49416ca35587Sdholland 			nfsfldi_setstripeindex(ndi, i, stripeindex);
49426ca35587Sdholland 		}
49436ca35587Sdholland 
49446ca35587Sdholland 		/* Now, dissect the server address(es). */
49456ca35587Sdholland 		safilled = 0;
49466ca35587Sdholland 		for (i = 0; i < addrcnt; i++) {
49476ca35587Sdholland 			NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
49486ca35587Sdholland 			cnt = fxdr_unsigned(uint32_t, *tl);
49496ca35587Sdholland 			if (cnt == 0) {
49506ca35587Sdholland 				printf("NFS devinfo 0 len addrlist\n");
49516ca35587Sdholland 				error = NFSERR_BADXDR;
49526ca35587Sdholland 				goto nfsmout;
49536ca35587Sdholland 			}
49546ca35587Sdholland 			dspp = nfsfldi_addr(ndi, i);
49556ca35587Sdholland 			pos = arc4random() % cnt;	/* Choose one. */
49566ca35587Sdholland 			safilled = 0;
49576ca35587Sdholland 			for (j = 0; j < cnt; j++) {
49586ca35587Sdholland 				error = nfsv4_getipaddr(nd, &ss, &isudp);
49596ca35587Sdholland 				if (error != 0 && error != EPERM) {
49606ca35587Sdholland 					error = NFSERR_BADXDR;
49616ca35587Sdholland 					goto nfsmout;
49626ca35587Sdholland 				}
49636ca35587Sdholland 				if (error == 0 && isudp == 0) {
49646ca35587Sdholland 					/*
49656ca35587Sdholland 					 * The algorithm is:
49666ca35587Sdholland 					 * - use "pos" entry if it is of the
49676ca35587Sdholland 					 *   same af_family or none of them
49686ca35587Sdholland 					 *   is of the same af_family
49696ca35587Sdholland 					 * else
49706ca35587Sdholland 					 * - use the first one of the same
49716ca35587Sdholland 					 *   af_family.
49726ca35587Sdholland 					 */
49736ca35587Sdholland 					if ((safilled == 0 && ss.ss_family ==
49746ca35587Sdholland 					     nmp->nm_nam->sa_family) ||
49756ca35587Sdholland 					    (j == pos &&
49766ca35587Sdholland 					     (safilled == 0 || ss.ss_family ==
49776ca35587Sdholland 					      nmp->nm_nam->sa_family)) ||
49786ca35587Sdholland 					    (safilled == 1 && ss.ss_family ==
49796ca35587Sdholland 					     nmp->nm_nam->sa_family)) {
49806ca35587Sdholland 						error = nfsrpc_fillsa(nmp, &ss,
49816ca35587Sdholland 						    &dsp, p);
49826ca35587Sdholland 						if (error == 0) {
49836ca35587Sdholland 							*dspp = dsp;
49846ca35587Sdholland 							if (ss.ss_family ==
49856ca35587Sdholland 							 nmp->nm_nam->sa_family)
49866ca35587Sdholland 								safilled = 2;
49876ca35587Sdholland 							else
49886ca35587Sdholland 								safilled = 1;
49896ca35587Sdholland 						}
49906ca35587Sdholland 					}
49916ca35587Sdholland 				}
49926ca35587Sdholland 			}
49936ca35587Sdholland 			if (safilled == 0)
49946ca35587Sdholland 				break;
49956ca35587Sdholland 		}
49966ca35587Sdholland 
49976ca35587Sdholland 		/* And the notify bits. */
49986ca35587Sdholland 		NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
49996ca35587Sdholland 		if (safilled != 0) {
50006ca35587Sdholland 			bitcnt = fxdr_unsigned(int, *tl);
50016ca35587Sdholland 			if (bitcnt > 0) {
50026ca35587Sdholland 				NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
50036ca35587Sdholland 				if (notifybitsp != NULL)
50046ca35587Sdholland 					*notifybitsp =
50056ca35587Sdholland 					    fxdr_unsigned(uint32_t, *tl);
50066ca35587Sdholland 			}
50076ca35587Sdholland 			*ndip = ndi;
50086ca35587Sdholland 		} else
50096ca35587Sdholland 			error = EPERM;
50106ca35587Sdholland 	}
50116ca35587Sdholland 	if (nd->nd_repstat != 0)
50126ca35587Sdholland 		error = nd->nd_repstat;
50136ca35587Sdholland nfsmout:
50146ca35587Sdholland 	if (error != 0 && ndi != NULL)
50156ca35587Sdholland 		nfscl_freedevinfo(ndi);
50166ca35587Sdholland 	mbuf_freem(nd->nd_mrep);
50176ca35587Sdholland 	return (error);
50186ca35587Sdholland }
50196ca35587Sdholland 
50206ca35587Sdholland /*
50216ca35587Sdholland  * Do the NFSv4.1 LayoutCommit.
50226ca35587Sdholland  */
50236ca35587Sdholland int
nfsrpc_layoutcommit(struct nfsmount * nmp,uint8_t * fh,int fhlen,int reclaim,uint64_t off,uint64_t len,uint64_t lastbyte,nfsv4stateid_t * stateidp,int layouttype,int layoutupdatecnt,uint8_t * layp,struct ucred * cred,NFSPROC_T * p,void * stuff)50246ca35587Sdholland nfsrpc_layoutcommit(struct nfsmount *nmp, uint8_t *fh, int fhlen, int reclaim,
50256ca35587Sdholland     uint64_t off, uint64_t len, uint64_t lastbyte, nfsv4stateid_t *stateidp,
50266ca35587Sdholland     int layouttype, int layoutupdatecnt, uint8_t *layp, struct ucred *cred,
50276ca35587Sdholland     NFSPROC_T *p, void *stuff)
50286ca35587Sdholland {
50296ca35587Sdholland 	uint32_t *tl;
50306ca35587Sdholland 	struct nfsrv_descript nfsd, *nd = &nfsd;
50316ca35587Sdholland 	int error, outcnt, i;
50326ca35587Sdholland 	uint8_t *cp;
50336ca35587Sdholland 
50346ca35587Sdholland 	nfscl_reqstart(nd, NFSPROC_LAYOUTCOMMIT, nmp, fh, fhlen, NULL, NULL);
50356ca35587Sdholland 	NFSM_BUILD(tl, uint32_t *, 5 * NFSX_UNSIGNED + 3 * NFSX_HYPER +
50366ca35587Sdholland 	    NFSX_STATEID);
50376ca35587Sdholland 	txdr_hyper(off, tl);
50386ca35587Sdholland 	tl += 2;
50396ca35587Sdholland 	txdr_hyper(len, tl);
50406ca35587Sdholland 	tl += 2;
50416ca35587Sdholland 	if (reclaim != 0)
50426ca35587Sdholland 		*tl++ = newnfs_true;
50436ca35587Sdholland 	else
50446ca35587Sdholland 		*tl++ = newnfs_false;
50456ca35587Sdholland 	*tl++ = txdr_unsigned(stateidp->seqid);
50466ca35587Sdholland 	*tl++ = stateidp->other[0];
50476ca35587Sdholland 	*tl++ = stateidp->other[1];
50486ca35587Sdholland 	*tl++ = stateidp->other[2];
50496ca35587Sdholland 	*tl++ = newnfs_true;
50506ca35587Sdholland 	if (lastbyte < off)
50516ca35587Sdholland 		lastbyte = off;
50526ca35587Sdholland 	else if (lastbyte >= (off + len))
50536ca35587Sdholland 		lastbyte = off + len - 1;
50546ca35587Sdholland 	txdr_hyper(lastbyte, tl);
50556ca35587Sdholland 	tl += 2;
50566ca35587Sdholland 	*tl++ = newnfs_false;
50576ca35587Sdholland 	*tl++ = txdr_unsigned(layouttype);
50586ca35587Sdholland 	*tl = txdr_unsigned(layoutupdatecnt);
50596ca35587Sdholland 	if (layoutupdatecnt > 0) {
50606ca35587Sdholland 		KASSERT(layouttype != NFSLAYOUT_NFSV4_1_FILES,
50616ca35587Sdholland 		    ("Must be nil for Files Layout"));
50626ca35587Sdholland 		outcnt = NFSM_RNDUP(layoutupdatecnt);
50636ca35587Sdholland 		NFSM_BUILD(cp, uint8_t *, outcnt);
50646ca35587Sdholland 		NFSBCOPY(layp, cp, layoutupdatecnt);
50656ca35587Sdholland 		cp += layoutupdatecnt;
50666ca35587Sdholland 		for (i = 0; i < (outcnt - layoutupdatecnt); i++)
50676ca35587Sdholland 			*cp++ = 0x0;
50686ca35587Sdholland 	}
50696ca35587Sdholland 	nd->nd_flag |= ND_USEGSSNAME;
50706ca35587Sdholland 	error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
50716ca35587Sdholland 	    NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
50726ca35587Sdholland 	if (error != 0)
50736ca35587Sdholland 		return (error);
50746ca35587Sdholland 	error = nd->nd_repstat;
50756ca35587Sdholland 	mbuf_freem(nd->nd_mrep);
50766ca35587Sdholland 	return (error);
50776ca35587Sdholland }
50786ca35587Sdholland 
50796ca35587Sdholland /*
50806ca35587Sdholland  * Do the NFSv4.1 LayoutReturn.
50816ca35587Sdholland  */
50826ca35587Sdholland int
nfsrpc_layoutreturn(struct nfsmount * nmp,uint8_t * fh,int fhlen,int reclaim,int layouttype,uint32_t iomode,int layoutreturn,uint64_t offset,uint64_t len,nfsv4stateid_t * stateidp,int layoutcnt,uint32_t * layp,struct ucred * cred,NFSPROC_T * p,void * stuff)50836ca35587Sdholland nfsrpc_layoutreturn(struct nfsmount *nmp, uint8_t *fh, int fhlen, int reclaim,
50846ca35587Sdholland     int layouttype, uint32_t iomode, int layoutreturn, uint64_t offset,
50856ca35587Sdholland     uint64_t len, nfsv4stateid_t *stateidp, int layoutcnt, uint32_t *layp,
50866ca35587Sdholland     struct ucred *cred, NFSPROC_T *p, void *stuff)
50876ca35587Sdholland {
50886ca35587Sdholland 	uint32_t *tl;
50896ca35587Sdholland 	struct nfsrv_descript nfsd, *nd = &nfsd;
50906ca35587Sdholland 	int error, outcnt, i;
50916ca35587Sdholland 	uint8_t *cp;
50926ca35587Sdholland 
50936ca35587Sdholland 	nfscl_reqstart(nd, NFSPROC_LAYOUTRETURN, nmp, fh, fhlen, NULL, NULL);
50946ca35587Sdholland 	NFSM_BUILD(tl, uint32_t *, 4 * NFSX_UNSIGNED);
50956ca35587Sdholland 	if (reclaim != 0)
50966ca35587Sdholland 		*tl++ = newnfs_true;
50976ca35587Sdholland 	else
50986ca35587Sdholland 		*tl++ = newnfs_false;
50996ca35587Sdholland 	*tl++ = txdr_unsigned(layouttype);
51006ca35587Sdholland 	*tl++ = txdr_unsigned(iomode);
51016ca35587Sdholland 	*tl = txdr_unsigned(layoutreturn);
51026ca35587Sdholland 	if (layoutreturn == NFSLAYOUTRETURN_FILE) {
51036ca35587Sdholland 		NFSM_BUILD(tl, uint32_t *, 2 * NFSX_HYPER + NFSX_STATEID +
51046ca35587Sdholland 		    NFSX_UNSIGNED);
51056ca35587Sdholland 		txdr_hyper(offset, tl);
51066ca35587Sdholland 		tl += 2;
51076ca35587Sdholland 		txdr_hyper(len, tl);
51086ca35587Sdholland 		tl += 2;
51096ca35587Sdholland 		NFSCL_DEBUG(4, "layoutret stseq=%d\n", (int)stateidp->seqid);
51106ca35587Sdholland 		*tl++ = txdr_unsigned(stateidp->seqid);
51116ca35587Sdholland 		*tl++ = stateidp->other[0];
51126ca35587Sdholland 		*tl++ = stateidp->other[1];
51136ca35587Sdholland 		*tl++ = stateidp->other[2];
51146ca35587Sdholland 		*tl = txdr_unsigned(layoutcnt);
51156ca35587Sdholland 		if (layoutcnt > 0) {
51166ca35587Sdholland 			outcnt = NFSM_RNDUP(layoutcnt);
51176ca35587Sdholland 			NFSM_BUILD(cp, uint8_t *, outcnt);
51186ca35587Sdholland 			NFSBCOPY(layp, cp, layoutcnt);
51196ca35587Sdholland 			cp += layoutcnt;
51206ca35587Sdholland 			for (i = 0; i < (outcnt - layoutcnt); i++)
51216ca35587Sdholland 				*cp++ = 0x0;
51226ca35587Sdholland 		}
51236ca35587Sdholland 	}
51246ca35587Sdholland 	nd->nd_flag |= ND_USEGSSNAME;
51256ca35587Sdholland 	error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
51266ca35587Sdholland 	    NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
51276ca35587Sdholland 	if (error != 0)
51286ca35587Sdholland 		return (error);
51296ca35587Sdholland 	if (nd->nd_repstat == 0) {
51306ca35587Sdholland 		NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
51316ca35587Sdholland 		if (*tl != 0) {
51326ca35587Sdholland 			NFSM_DISSECT(tl, uint32_t *, NFSX_STATEID);
51336ca35587Sdholland 			stateidp->seqid = fxdr_unsigned(uint32_t, *tl++);
51346ca35587Sdholland 			stateidp->other[0] = *tl++;
51356ca35587Sdholland 			stateidp->other[1] = *tl++;
51366ca35587Sdholland 			stateidp->other[2] = *tl;
51376ca35587Sdholland 		}
51386ca35587Sdholland 	} else
51396ca35587Sdholland 		error = nd->nd_repstat;
51406ca35587Sdholland nfsmout:
51416ca35587Sdholland 	mbuf_freem(nd->nd_mrep);
51426ca35587Sdholland 	return (error);
51436ca35587Sdholland }
51446ca35587Sdholland 
51456ca35587Sdholland /*
51466ca35587Sdholland  * Acquire a layout and devinfo, if possible. The caller must have acquired
51476ca35587Sdholland  * a reference count on the nfsclclient structure before calling this.
51486ca35587Sdholland  * Return the layout in lypp with a reference count on it, if successful.
51496ca35587Sdholland  */
51506ca35587Sdholland static int
nfsrpc_getlayout(struct nfsmount * nmp,vnode_t vp,struct nfsfh * nfhp,int iomode,uint32_t * notifybitsp,nfsv4stateid_t * stateidp,uint64_t off,struct nfscllayout ** lypp,struct ucred * cred,NFSPROC_T * p)51516ca35587Sdholland nfsrpc_getlayout(struct nfsmount *nmp, vnode_t vp, struct nfsfh *nfhp,
51526ca35587Sdholland     int iomode, uint32_t *notifybitsp, nfsv4stateid_t *stateidp, uint64_t off,
51536ca35587Sdholland     struct nfscllayout **lypp, struct ucred *cred, NFSPROC_T *p)
51546ca35587Sdholland {
51556ca35587Sdholland 	struct nfscllayout *lyp;
51566ca35587Sdholland 	struct nfsclflayout *flp, *tflp;
51576ca35587Sdholland 	struct nfscldevinfo *dip;
51586ca35587Sdholland 	struct nfsclflayouthead flh;
51596ca35587Sdholland 	int error = 0, islocked, layoutlen, recalled, retonclose;
51606ca35587Sdholland 	nfsv4stateid_t stateid;
51616ca35587Sdholland 
51626ca35587Sdholland 	*lypp = NULL;
51636ca35587Sdholland 	/*
51646ca35587Sdholland 	 * If lyp is returned non-NULL, there will be a refcnt (shared lock)
51656ca35587Sdholland 	 * on it, iff flp != NULL or a lock (exclusive lock) on it iff
51666ca35587Sdholland 	 * flp == NULL.
51676ca35587Sdholland 	 */
51686ca35587Sdholland 	lyp = nfscl_getlayout(nmp->nm_clp, nfhp->nfh_fh, nfhp->nfh_len,
51696ca35587Sdholland 	    off, &flp, &recalled);
51706ca35587Sdholland 	islocked = 0;
51716ca35587Sdholland 	if (lyp == NULL || flp == NULL) {
51726ca35587Sdholland 		if (recalled != 0)
51736ca35587Sdholland 			return (EIO);
51746ca35587Sdholland 		LIST_INIT(&flh);
51756ca35587Sdholland 		layoutlen = NFSMNT_MDSSESSION(nmp)->nfsess_maxcache -
51766ca35587Sdholland 		    (NFSX_STATEID + 3 * NFSX_UNSIGNED);
51776ca35587Sdholland 		if (lyp == NULL) {
51786ca35587Sdholland 			stateid.seqid = 0;
51796ca35587Sdholland 			stateid.other[0] = stateidp->other[0];
51806ca35587Sdholland 			stateid.other[1] = stateidp->other[1];
51816ca35587Sdholland 			stateid.other[2] = stateidp->other[2];
51826ca35587Sdholland 			error = nfsrpc_layoutget(nmp, nfhp->nfh_fh,
51836ca35587Sdholland 			    nfhp->nfh_len, iomode, (uint64_t)0, INT64_MAX,
51846ca35587Sdholland 			    (uint64_t)0, layoutlen, &stateid, &retonclose,
51856ca35587Sdholland 			    &flh, cred, p, NULL);
51866ca35587Sdholland 		} else {
51876ca35587Sdholland 			islocked = 1;
51886ca35587Sdholland 			stateid.seqid = lyp->nfsly_stateid.seqid;
51896ca35587Sdholland 			stateid.other[0] = lyp->nfsly_stateid.other[0];
51906ca35587Sdholland 			stateid.other[1] = lyp->nfsly_stateid.other[1];
51916ca35587Sdholland 			stateid.other[2] = lyp->nfsly_stateid.other[2];
51926ca35587Sdholland 			error = nfsrpc_layoutget(nmp, nfhp->nfh_fh,
51936ca35587Sdholland 			    nfhp->nfh_len, iomode, off, INT64_MAX,
51946ca35587Sdholland 			    (uint64_t)0, layoutlen, &stateid, &retonclose,
51956ca35587Sdholland 			    &flh, cred, p, NULL);
51966ca35587Sdholland 		}
51976ca35587Sdholland 		if (error == 0)
51986ca35587Sdholland 			LIST_FOREACH(tflp, &flh, nfsfl_list) {
51996ca35587Sdholland 				error = nfscl_adddevinfo(nmp, NULL, tflp);
52006ca35587Sdholland 				if (error != 0) {
52016ca35587Sdholland 					error = nfsrpc_getdeviceinfo(nmp,
52026ca35587Sdholland 					    tflp->nfsfl_dev,
52036ca35587Sdholland 					    NFSLAYOUT_NFSV4_1_FILES,
52046ca35587Sdholland 					    notifybitsp, &dip, cred, p);
52056ca35587Sdholland 					if (error != 0)
52066ca35587Sdholland 						break;
52076ca35587Sdholland 					error = nfscl_adddevinfo(nmp, dip,
52086ca35587Sdholland 					    tflp);
52096ca35587Sdholland 					if (error != 0)
52106ca35587Sdholland 						printf(
52116ca35587Sdholland 						    "getlayout: cannot add\n");
52126ca35587Sdholland 				}
52136ca35587Sdholland 			}
52146ca35587Sdholland 		if (error == 0) {
52156ca35587Sdholland 			/*
52166ca35587Sdholland 			 * nfscl_layout() always returns with the nfsly_lock
52176ca35587Sdholland 			 * set to a refcnt (shared lock).
52186ca35587Sdholland 			 */
52196ca35587Sdholland 			error = nfscl_layout(nmp, vp, nfhp->nfh_fh,
52206ca35587Sdholland 			    nfhp->nfh_len, &stateid, retonclose, &flh, &lyp,
52216ca35587Sdholland 			    cred, p);
52226ca35587Sdholland 			if (error == 0)
52236ca35587Sdholland 				*lypp = lyp;
52246ca35587Sdholland 		} else if (islocked != 0)
52256ca35587Sdholland 			nfsv4_unlock(&lyp->nfsly_lock, 0);
52266ca35587Sdholland 	} else
52276ca35587Sdholland 		*lypp = lyp;
52286ca35587Sdholland 	return (error);
52296ca35587Sdholland }
52306ca35587Sdholland 
52316ca35587Sdholland /*
52326ca35587Sdholland  * Do a TCP connection plus exchange id and create session.
52336ca35587Sdholland  * If successful, a "struct nfsclds" is linked into the list for the
52346ca35587Sdholland  * mount point and a pointer to it is returned.
52356ca35587Sdholland  */
52366ca35587Sdholland static int
nfsrpc_fillsa(struct nfsmount * nmp,struct sockaddr_storage * ssp,struct nfsclds ** dspp,NFSPROC_T * p)52376ca35587Sdholland nfsrpc_fillsa(struct nfsmount *nmp, struct sockaddr_storage *ssp,
52386ca35587Sdholland     struct nfsclds **dspp, NFSPROC_T *p)
52396ca35587Sdholland {
52406ca35587Sdholland 	struct sockaddr_in *msad, *sad, *ssd;
52416ca35587Sdholland 	struct sockaddr_in6 *msad6, *sad6, *ssd6;
52426ca35587Sdholland 	struct nfsclclient *clp;
52436ca35587Sdholland 	struct nfssockreq *nrp;
52446ca35587Sdholland 	struct nfsclds *dsp, *tdsp;
52456ca35587Sdholland 	int error;
52466ca35587Sdholland 	enum nfsclds_state retv;
52476ca35587Sdholland 	uint32_t sequenceid;
52486ca35587Sdholland 
52496ca35587Sdholland 	KASSERT(nmp->nm_sockreq.nr_cred != NULL,
52506ca35587Sdholland 	    ("nfsrpc_fillsa: NULL nr_cred"));
52516ca35587Sdholland 	NFSLOCKCLSTATE();
52526ca35587Sdholland 	clp = nmp->nm_clp;
52536ca35587Sdholland 	NFSUNLOCKCLSTATE();
52546ca35587Sdholland 	if (clp == NULL)
52556ca35587Sdholland 		return (EPERM);
52566ca35587Sdholland 	if (ssp->ss_family == AF_INET) {
52576ca35587Sdholland 		ssd = (struct sockaddr_in *)ssp;
52586ca35587Sdholland 		NFSLOCKMNT(nmp);
52596ca35587Sdholland 
52606ca35587Sdholland 		/*
52616ca35587Sdholland 		 * Check to see if we already have a session for this
52626ca35587Sdholland 		 * address that is usable for a DS.
52636ca35587Sdholland 		 * Note that the MDS's address is in a different place
52646ca35587Sdholland 		 * than the sessions already acquired for DS's.
52656ca35587Sdholland 		 */
52666ca35587Sdholland 		msad = (struct sockaddr_in *)nmp->nm_sockreq.nr_nam;
52676ca35587Sdholland 		tdsp = TAILQ_FIRST(&nmp->nm_sess);
52686ca35587Sdholland 		while (tdsp != NULL) {
52696ca35587Sdholland 			if (msad != NULL && msad->sin_family == AF_INET &&
52706ca35587Sdholland 			    ssd->sin_addr.s_addr == msad->sin_addr.s_addr &&
52716ca35587Sdholland 			    ssd->sin_port == msad->sin_port &&
52726ca35587Sdholland 			    (tdsp->nfsclds_flags & NFSCLDS_DS) != 0) {
52736ca35587Sdholland 				*dspp = tdsp;
52746ca35587Sdholland 				NFSUNLOCKMNT(nmp);
52756ca35587Sdholland 				NFSCL_DEBUG(4, "fnd same addr\n");
52766ca35587Sdholland 				return (0);
52776ca35587Sdholland 			}
52786ca35587Sdholland 			tdsp = TAILQ_NEXT(tdsp, nfsclds_list);
52796ca35587Sdholland 			if (tdsp != NULL && tdsp->nfsclds_sockp != NULL)
52806ca35587Sdholland 				msad = (struct sockaddr_in *)
52816ca35587Sdholland 				    tdsp->nfsclds_sockp->nr_nam;
52826ca35587Sdholland 			else
52836ca35587Sdholland 				msad = NULL;
52846ca35587Sdholland 		}
52856ca35587Sdholland 		NFSUNLOCKMNT(nmp);
52866ca35587Sdholland 
52876ca35587Sdholland 		/* No IP address match, so look for new/trunked one. */
52886ca35587Sdholland 		sad = malloc(sizeof(*sad), M_SONAME, M_WAITOK | M_ZERO);
52896ca35587Sdholland 		sad->sin_len = sizeof(*sad);
52906ca35587Sdholland 		sad->sin_family = AF_INET;
52916ca35587Sdholland 		sad->sin_port = ssd->sin_port;
52926ca35587Sdholland 		sad->sin_addr.s_addr = ssd->sin_addr.s_addr;
52936ca35587Sdholland 		nrp = malloc(sizeof(*nrp), M_NFSSOCKREQ, M_WAITOK | M_ZERO);
52946ca35587Sdholland 		nrp->nr_nam = (struct sockaddr *)sad;
52956ca35587Sdholland 	} else if (ssp->ss_family == AF_INET6) {
52966ca35587Sdholland 		ssd6 = (struct sockaddr_in6 *)ssp;
52976ca35587Sdholland 		NFSLOCKMNT(nmp);
52986ca35587Sdholland 
52996ca35587Sdholland 		/*
53006ca35587Sdholland 		 * Check to see if we already have a session for this
53016ca35587Sdholland 		 * address that is usable for a DS.
53026ca35587Sdholland 		 * Note that the MDS's address is in a different place
53036ca35587Sdholland 		 * than the sessions already acquired for DS's.
53046ca35587Sdholland 		 */
53056ca35587Sdholland 		msad6 = (struct sockaddr_in6 *)nmp->nm_sockreq.nr_nam;
53066ca35587Sdholland 		tdsp = TAILQ_FIRST(&nmp->nm_sess);
53076ca35587Sdholland 		while (tdsp != NULL) {
53086ca35587Sdholland 			if (msad6 != NULL && msad6->sin6_family == AF_INET6 &&
53096ca35587Sdholland 			    IN6_ARE_ADDR_EQUAL(&ssd6->sin6_addr,
53106ca35587Sdholland 			    &msad6->sin6_addr) &&
53116ca35587Sdholland 			    ssd6->sin6_port == msad6->sin6_port &&
53126ca35587Sdholland 			    (tdsp->nfsclds_flags & NFSCLDS_DS) != 0) {
53136ca35587Sdholland 				*dspp = tdsp;
53146ca35587Sdholland 				NFSUNLOCKMNT(nmp);
53156ca35587Sdholland 				return (0);
53166ca35587Sdholland 			}
53176ca35587Sdholland 			tdsp = TAILQ_NEXT(tdsp, nfsclds_list);
53186ca35587Sdholland 			if (tdsp != NULL && tdsp->nfsclds_sockp != NULL)
53196ca35587Sdholland 				msad6 = (struct sockaddr_in6 *)
53206ca35587Sdholland 				    tdsp->nfsclds_sockp->nr_nam;
53216ca35587Sdholland 			else
53226ca35587Sdholland 				msad6 = NULL;
53236ca35587Sdholland 		}
53246ca35587Sdholland 		NFSUNLOCKMNT(nmp);
53256ca35587Sdholland 
53266ca35587Sdholland 		/* No IP address match, so look for new/trunked one. */
53276ca35587Sdholland 		sad6 = malloc(sizeof(*sad6), M_SONAME, M_WAITOK | M_ZERO);
53286ca35587Sdholland 		sad6->sin6_len = sizeof(*sad6);
53296ca35587Sdholland 		sad6->sin6_family = AF_INET6;
53306ca35587Sdholland 		sad6->sin6_port = ssd6->sin6_port;
53316ca35587Sdholland 		NFSBCOPY(&ssd6->sin6_addr, &sad6->sin6_addr,
53326ca35587Sdholland 		    sizeof(struct in6_addr));
53336ca35587Sdholland 		nrp = malloc(sizeof(*nrp), M_NFSSOCKREQ, M_WAITOK | M_ZERO);
53346ca35587Sdholland 		nrp->nr_nam = (struct sockaddr *)sad6;
53356ca35587Sdholland 	} else
53366ca35587Sdholland 		return (EPERM);
53376ca35587Sdholland 
53386ca35587Sdholland 	nrp->nr_sotype = SOCK_STREAM;
53396ca35587Sdholland 	mtx_init(&nrp->nr_mtx, "nfssock", NULL, MTX_DEF);
53406ca35587Sdholland 	nrp->nr_prog = NFS_PROG;
53416ca35587Sdholland 	nrp->nr_vers = NFS_VER4;
53426ca35587Sdholland 
53436ca35587Sdholland 	/*
53446ca35587Sdholland 	 * Use the credentials that were used for the mount, which are
53456ca35587Sdholland 	 * in nmp->nm_sockreq.nr_cred for newnfs_connect() etc.
53466ca35587Sdholland 	 * Ref. counting the credentials with crhold() is probably not
53476ca35587Sdholland 	 * necessary, since nm_sockreq.nr_cred won't be crfree()'d until
53486ca35587Sdholland 	 * unmount, but I did it anyhow.
53496ca35587Sdholland 	 */
53506ca35587Sdholland 	nrp->nr_cred = crhold(nmp->nm_sockreq.nr_cred);
53516ca35587Sdholland 	error = newnfs_connect(nmp, nrp, NULL, p, 0);
53526ca35587Sdholland 	NFSCL_DEBUG(3, "DS connect=%d\n", error);
53536ca35587Sdholland 
53546ca35587Sdholland 	/* Now, do the exchangeid and create session. */
53556ca35587Sdholland 	if (error == 0)
53566ca35587Sdholland 		error = nfsrpc_exchangeid(nmp, clp, nrp, NFSV4EXCH_USEPNFSDS,
53576ca35587Sdholland 		    &dsp, nrp->nr_cred, p);
53586ca35587Sdholland 	NFSCL_DEBUG(3, "DS exchangeid=%d\n", error);
53596ca35587Sdholland 	if (error == 0) {
53606ca35587Sdholland 		dsp->nfsclds_sockp = nrp;
53616ca35587Sdholland 		NFSLOCKMNT(nmp);
53626ca35587Sdholland 		retv = nfscl_getsameserver(nmp, dsp, &tdsp);
53636ca35587Sdholland 		NFSCL_DEBUG(3, "getsame ret=%d\n", retv);
53646ca35587Sdholland 		if (retv == NFSDSP_USETHISSESSION) {
53656ca35587Sdholland 			NFSUNLOCKMNT(nmp);
53666ca35587Sdholland 			/*
53676ca35587Sdholland 			 * If there is already a session for this server,
53686ca35587Sdholland 			 * use it.
53696ca35587Sdholland 			 */
53706ca35587Sdholland 			(void)newnfs_disconnect(nrp);
53716ca35587Sdholland 			nfscl_freenfsclds(dsp);
53726ca35587Sdholland 			*dspp = tdsp;
53736ca35587Sdholland 			return (0);
53746ca35587Sdholland 		}
53756ca35587Sdholland 		if (retv == NFSDSP_SEQTHISSESSION)
53766ca35587Sdholland 			sequenceid = tdsp->nfsclds_sess.nfsess_sequenceid;
53776ca35587Sdholland 		else
53786ca35587Sdholland 			sequenceid = dsp->nfsclds_sess.nfsess_sequenceid;
53796ca35587Sdholland 		NFSUNLOCKMNT(nmp);
53806ca35587Sdholland 		error = nfsrpc_createsession(nmp, &dsp->nfsclds_sess,
53816ca35587Sdholland 		    nrp, sequenceid, 0, nrp->nr_cred, p);
53826ca35587Sdholland 		NFSCL_DEBUG(3, "DS createsess=%d\n", error);
53836ca35587Sdholland 	} else {
53846ca35587Sdholland 		NFSFREECRED(nrp->nr_cred);
53856ca35587Sdholland 		NFSFREEMUTEX(&nrp->nr_mtx);
53866ca35587Sdholland 		free(nrp->nr_nam, M_SONAME);
53876ca35587Sdholland 		free(nrp, M_NFSSOCKREQ);
53886ca35587Sdholland 	}
53896ca35587Sdholland 	if (error == 0) {
53906ca35587Sdholland 		NFSCL_DEBUG(3, "add DS session\n");
53916ca35587Sdholland 		/*
53926ca35587Sdholland 		 * Put it at the end of the list. That way the list
53936ca35587Sdholland 		 * is ordered by when the entry was added. This matters
53946ca35587Sdholland 		 * since the one done first is the one that should be
53956ca35587Sdholland 		 * used for sequencid'ing any subsequent create sessions.
53966ca35587Sdholland 		 */
53976ca35587Sdholland 		NFSLOCKMNT(nmp);
53986ca35587Sdholland 		TAILQ_INSERT_TAIL(&nmp->nm_sess, dsp, nfsclds_list);
53996ca35587Sdholland 		NFSUNLOCKMNT(nmp);
54006ca35587Sdholland 		*dspp = dsp;
54016ca35587Sdholland 	} else if (dsp != NULL)
54026ca35587Sdholland 		nfscl_freenfsclds(dsp);
54036ca35587Sdholland 	return (error);
54046ca35587Sdholland }
54056ca35587Sdholland 
54066ca35587Sdholland /*
54076ca35587Sdholland  * Do the NFSv4.1 Reclaim Complete.
54086ca35587Sdholland  */
54096ca35587Sdholland int
nfsrpc_reclaimcomplete(struct nfsmount * nmp,struct ucred * cred,NFSPROC_T * p)54106ca35587Sdholland nfsrpc_reclaimcomplete(struct nfsmount *nmp, struct ucred *cred, NFSPROC_T *p)
54116ca35587Sdholland {
54126ca35587Sdholland 	uint32_t *tl;
54136ca35587Sdholland 	struct nfsrv_descript nfsd;
54146ca35587Sdholland 	struct nfsrv_descript *nd = &nfsd;
54156ca35587Sdholland 	int error;
54166ca35587Sdholland 
54176ca35587Sdholland 	nfscl_reqstart(nd, NFSPROC_RECLAIMCOMPL, nmp, NULL, 0, NULL, NULL);
54186ca35587Sdholland 	NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
54196ca35587Sdholland 	*tl = newnfs_false;
54206ca35587Sdholland 	nd->nd_flag |= ND_USEGSSNAME;
54216ca35587Sdholland 	error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
54226ca35587Sdholland 	    NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
54236ca35587Sdholland 	if (error != 0)
54246ca35587Sdholland 		return (error);
54256ca35587Sdholland 	error = nd->nd_repstat;
54266ca35587Sdholland 	mbuf_freem(nd->nd_mrep);
54276ca35587Sdholland 	return (error);
54286ca35587Sdholland }
54296ca35587Sdholland 
54306ca35587Sdholland /*
54316ca35587Sdholland  * Initialize the slot tables for a session.
54326ca35587Sdholland  */
54336ca35587Sdholland static void
nfscl_initsessionslots(struct nfsclsession * sep)54346ca35587Sdholland nfscl_initsessionslots(struct nfsclsession *sep)
54356ca35587Sdholland {
54366ca35587Sdholland 	int i;
54376ca35587Sdholland 
54386ca35587Sdholland 	for (i = 0; i < NFSV4_CBSLOTS; i++) {
54396ca35587Sdholland 		m_freem(sep->nfsess_cbslots[i].nfssl_reply);
54406ca35587Sdholland 		NFSBZERO(&sep->nfsess_cbslots[i], sizeof(struct nfsslot));
54416ca35587Sdholland 	}
54426ca35587Sdholland 	for (i = 0; i < 64; i++)
54436ca35587Sdholland 		sep->nfsess_slotseq[i] = 0;
54446ca35587Sdholland 	sep->nfsess_slots = 0;
54456ca35587Sdholland }
54466ca35587Sdholland 
54476ca35587Sdholland /*
54486ca35587Sdholland  * Called to try and do an I/O operation via an NFSv4.1 Data Server (DS).
54496ca35587Sdholland  */
54506ca35587Sdholland int
nfscl_doiods(vnode_t vp,struct uio * uiop,int * iomode,int * must_commit,uint32_t rwaccess,struct ucred * cred,NFSPROC_T * p)54516ca35587Sdholland nfscl_doiods(vnode_t vp, struct uio *uiop, int *iomode, int *must_commit,
54526ca35587Sdholland     uint32_t rwaccess, struct ucred *cred, NFSPROC_T *p)
54536ca35587Sdholland {
54546ca35587Sdholland 	struct nfsnode *np = VTONFS(vp);
54556ca35587Sdholland 	struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
54566ca35587Sdholland 	struct nfscllayout *layp;
54576ca35587Sdholland 	struct nfscldevinfo *dip;
54586ca35587Sdholland 	struct nfsclflayout *rflp;
54596ca35587Sdholland 	nfsv4stateid_t stateid;
54606ca35587Sdholland 	struct ucred *newcred;
54616ca35587Sdholland 	uint64_t lastbyte, len, off, oresid, xfer;
54626ca35587Sdholland 	int eof, error, iolaymode, recalled;
54636ca35587Sdholland 	void *lckp;
54646ca35587Sdholland 
54656ca35587Sdholland 	if (!NFSHASPNFS(nmp) || nfscl_enablecallb == 0 || nfs_numnfscbd == 0 ||
54666ca35587Sdholland 	    (np->n_flag & NNOLAYOUT) != 0)
54676ca35587Sdholland 		return (EIO);
54686ca35587Sdholland 	/* Now, get a reference cnt on the clientid for this mount. */
54696ca35587Sdholland 	if (nfscl_getref(nmp) == 0)
54706ca35587Sdholland 		return (EIO);
54716ca35587Sdholland 
54726ca35587Sdholland 	/* Find an appropriate stateid. */
54736ca35587Sdholland 	newcred = NFSNEWCRED(cred);
54746ca35587Sdholland 	error = nfscl_getstateid(vp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len,
54756ca35587Sdholland 	    rwaccess, 1, newcred, p, &stateid, &lckp);
54766ca35587Sdholland 	if (error != 0) {
54776ca35587Sdholland 		NFSFREECRED(newcred);
54786ca35587Sdholland 		nfscl_relref(nmp);
54796ca35587Sdholland 		return (error);
54806ca35587Sdholland 	}
54816ca35587Sdholland 	/* Search for a layout for this file. */
54826ca35587Sdholland 	off = uiop->uio_offset;
54836ca35587Sdholland 	layp = nfscl_getlayout(nmp->nm_clp, np->n_fhp->nfh_fh,
54846ca35587Sdholland 	    np->n_fhp->nfh_len, off, &rflp, &recalled);
54856ca35587Sdholland 	if (layp == NULL || rflp == NULL) {
54866ca35587Sdholland 		if (recalled != 0) {
54876ca35587Sdholland 			NFSFREECRED(newcred);
54886ca35587Sdholland 			nfscl_relref(nmp);
54896ca35587Sdholland 			return (EIO);
54906ca35587Sdholland 		}
54916ca35587Sdholland 		if (layp != NULL) {
54926ca35587Sdholland 			nfscl_rellayout(layp, (rflp == NULL) ? 1 : 0);
54936ca35587Sdholland 			layp = NULL;
54946ca35587Sdholland 		}
54956ca35587Sdholland 		/* Try and get a Layout, if it is supported. */
54966ca35587Sdholland 		if (rwaccess == NFSV4OPEN_ACCESSWRITE ||
54976ca35587Sdholland 		    (np->n_flag & NWRITEOPENED) != 0)
54986ca35587Sdholland 			iolaymode = NFSLAYOUTIOMODE_RW;
54996ca35587Sdholland 		else
55006ca35587Sdholland 			iolaymode = NFSLAYOUTIOMODE_READ;
55016ca35587Sdholland 		error = nfsrpc_getlayout(nmp, vp, np->n_fhp, iolaymode,
55026ca35587Sdholland 		    NULL, &stateid, off, &layp, newcred, p);
55036ca35587Sdholland 		if (error != 0) {
55046ca35587Sdholland 			NFSLOCKNODE(np);
55056ca35587Sdholland 			np->n_flag |= NNOLAYOUT;
55066ca35587Sdholland 			NFSUNLOCKNODE(np);
55076ca35587Sdholland 			if (lckp != NULL)
55086ca35587Sdholland 				nfscl_lockderef(lckp);
55096ca35587Sdholland 			NFSFREECRED(newcred);
55106ca35587Sdholland 			if (layp != NULL)
55116ca35587Sdholland 				nfscl_rellayout(layp, 0);
55126ca35587Sdholland 			nfscl_relref(nmp);
55136ca35587Sdholland 			return (error);
55146ca35587Sdholland 		}
55156ca35587Sdholland 	}
55166ca35587Sdholland 
55176ca35587Sdholland 	/*
55186ca35587Sdholland 	 * Loop around finding a layout that works for the first part of
55196ca35587Sdholland 	 * this I/O operation, and then call the function that actually
55206ca35587Sdholland 	 * does the RPC.
55216ca35587Sdholland 	 */
55226ca35587Sdholland 	eof = 0;
55236ca35587Sdholland 	len = (uint64_t)uiop->uio_resid;
55246ca35587Sdholland 	while (len > 0 && error == 0 && eof == 0) {
55256ca35587Sdholland 		off = uiop->uio_offset;
55266ca35587Sdholland 		error = nfscl_findlayoutforio(layp, off, rwaccess, &rflp);
55276ca35587Sdholland 		if (error == 0) {
55286ca35587Sdholland 			oresid = xfer = (uint64_t)uiop->uio_resid;
55296ca35587Sdholland 			if (xfer > (rflp->nfsfl_end - rflp->nfsfl_off))
55306ca35587Sdholland 				xfer = rflp->nfsfl_end - rflp->nfsfl_off;
55316ca35587Sdholland 			dip = nfscl_getdevinfo(nmp->nm_clp, rflp->nfsfl_dev,
55326ca35587Sdholland 			    rflp->nfsfl_devp);
55336ca35587Sdholland 			if (dip != NULL) {
55346ca35587Sdholland 				error = nfscl_doflayoutio(vp, uiop, iomode,
55356ca35587Sdholland 				    must_commit, &eof, &stateid, rwaccess, dip,
55366ca35587Sdholland 				    layp, rflp, off, xfer, newcred, p);
55376ca35587Sdholland 				nfscl_reldevinfo(dip);
55386ca35587Sdholland 				lastbyte = off + xfer - 1;
55396ca35587Sdholland 				if (error == 0) {
55406ca35587Sdholland 					NFSLOCKCLSTATE();
55416ca35587Sdholland 					if (lastbyte > layp->nfsly_lastbyte)
55426ca35587Sdholland 						layp->nfsly_lastbyte = lastbyte;
55436ca35587Sdholland 					NFSUNLOCKCLSTATE();
55446ca35587Sdholland 				}
55456ca35587Sdholland 			} else
55466ca35587Sdholland 				error = EIO;
55476ca35587Sdholland 			if (error == 0)
55486ca35587Sdholland 				len -= (oresid - (uint64_t)uiop->uio_resid);
55496ca35587Sdholland 		}
55506ca35587Sdholland 	}
55516ca35587Sdholland 	if (lckp != NULL)
55526ca35587Sdholland 		nfscl_lockderef(lckp);
55536ca35587Sdholland 	NFSFREECRED(newcred);
55546ca35587Sdholland 	nfscl_rellayout(layp, 0);
55556ca35587Sdholland 	nfscl_relref(nmp);
55566ca35587Sdholland 	return (error);
55576ca35587Sdholland }
55586ca35587Sdholland 
55596ca35587Sdholland /*
55606ca35587Sdholland  * Find a file layout that will handle the first bytes of the requested
55616ca35587Sdholland  * range and return the information from it needed to to the I/O operation.
55626ca35587Sdholland  */
55636ca35587Sdholland int
nfscl_findlayoutforio(struct nfscllayout * lyp,uint64_t off,uint32_t rwaccess,struct nfsclflayout ** retflpp)55646ca35587Sdholland nfscl_findlayoutforio(struct nfscllayout *lyp, uint64_t off, uint32_t rwaccess,
55656ca35587Sdholland     struct nfsclflayout **retflpp)
55666ca35587Sdholland {
55676ca35587Sdholland 	struct nfsclflayout *flp, *nflp, *rflp;
55686ca35587Sdholland 	uint32_t rw;
55696ca35587Sdholland 
55706ca35587Sdholland 	rflp = NULL;
55716ca35587Sdholland 	rw = rwaccess;
55726ca35587Sdholland 	/* For reading, do the Read list first and then the Write list. */
55736ca35587Sdholland 	do {
55746ca35587Sdholland 		if (rw == NFSV4OPEN_ACCESSREAD)
55756ca35587Sdholland 			flp = LIST_FIRST(&lyp->nfsly_flayread);
55766ca35587Sdholland 		else
55776ca35587Sdholland 			flp = LIST_FIRST(&lyp->nfsly_flayrw);
55786ca35587Sdholland 		while (flp != NULL) {
55796ca35587Sdholland 			nflp = LIST_NEXT(flp, nfsfl_list);
55806ca35587Sdholland 			if (flp->nfsfl_off > off)
55816ca35587Sdholland 				break;
55826ca35587Sdholland 			if (flp->nfsfl_end > off &&
55836ca35587Sdholland 			    (rflp == NULL || rflp->nfsfl_end < flp->nfsfl_end))
55846ca35587Sdholland 				rflp = flp;
55856ca35587Sdholland 			flp = nflp;
55866ca35587Sdholland 		}
55876ca35587Sdholland 		if (rw == NFSV4OPEN_ACCESSREAD)
55886ca35587Sdholland 			rw = NFSV4OPEN_ACCESSWRITE;
55896ca35587Sdholland 		else
55906ca35587Sdholland 			rw = 0;
55916ca35587Sdholland 	} while (rw != 0);
55926ca35587Sdholland 	if (rflp != NULL) {
55936ca35587Sdholland 		/* This one covers the most bytes starting at off. */
55946ca35587Sdholland 		*retflpp = rflp;
55956ca35587Sdholland 		return (0);
55966ca35587Sdholland 	}
55976ca35587Sdholland 	return (EIO);
55986ca35587Sdholland }
55996ca35587Sdholland 
56006ca35587Sdholland /*
56016ca35587Sdholland  * Do I/O using an NFSv4.1 file layout.
56026ca35587Sdholland  */
56036ca35587Sdholland static int
nfscl_doflayoutio(vnode_t vp,struct uio * uiop,int * iomode,int * must_commit,int * eofp,nfsv4stateid_t * stateidp,int rwflag,struct nfscldevinfo * dp,struct nfscllayout * lyp,struct nfsclflayout * flp,uint64_t off,uint64_t len,struct ucred * cred,NFSPROC_T * p)56046ca35587Sdholland nfscl_doflayoutio(vnode_t vp, struct uio *uiop, int *iomode, int *must_commit,
56056ca35587Sdholland     int *eofp, nfsv4stateid_t *stateidp, int rwflag, struct nfscldevinfo *dp,
56066ca35587Sdholland     struct nfscllayout *lyp, struct nfsclflayout *flp, uint64_t off,
56076ca35587Sdholland     uint64_t len, struct ucred *cred, NFSPROC_T *p)
56086ca35587Sdholland {
56096ca35587Sdholland 	uint64_t io_off, rel_off, stripe_unit_size, transfer, xfer;
56106ca35587Sdholland 	int commit_thru_mds, error = 0, stripe_index, stripe_pos;
56116ca35587Sdholland 	struct nfsnode *np;
56126ca35587Sdholland 	struct nfsfh *fhp;
56136ca35587Sdholland 	struct nfsclds **dspp;
56146ca35587Sdholland 
56156ca35587Sdholland 	np = VTONFS(vp);
56166ca35587Sdholland 	rel_off = off - flp->nfsfl_patoff;
56176ca35587Sdholland 	stripe_unit_size = (flp->nfsfl_util >> 6) & 0x3ffffff;
56186ca35587Sdholland 	stripe_pos = (rel_off / stripe_unit_size + flp->nfsfl_stripe1) %
56196ca35587Sdholland 	    dp->nfsdi_stripecnt;
56206ca35587Sdholland 	transfer = stripe_unit_size - (rel_off % stripe_unit_size);
56216ca35587Sdholland 
56226ca35587Sdholland 	/* Loop around, doing I/O for each stripe unit. */
56236ca35587Sdholland 	while (len > 0 && error == 0) {
56246ca35587Sdholland 		stripe_index = nfsfldi_stripeindex(dp, stripe_pos);
56256ca35587Sdholland 		dspp = nfsfldi_addr(dp, stripe_index);
56266ca35587Sdholland 		if (len > transfer)
56276ca35587Sdholland 			xfer = transfer;
56286ca35587Sdholland 		else
56296ca35587Sdholland 			xfer = len;
56306ca35587Sdholland 		if ((flp->nfsfl_util & NFSFLAYUTIL_DENSE) != 0) {
56316ca35587Sdholland 			/* Dense layout. */
56326ca35587Sdholland 			if (stripe_pos >= flp->nfsfl_fhcnt)
56336ca35587Sdholland 				return (EIO);
56346ca35587Sdholland 			fhp = flp->nfsfl_fh[stripe_pos];
56356ca35587Sdholland 			io_off = (rel_off / (stripe_unit_size *
56366ca35587Sdholland 			    dp->nfsdi_stripecnt)) * stripe_unit_size +
56376ca35587Sdholland 			    rel_off % stripe_unit_size;
56386ca35587Sdholland 		} else {
56396ca35587Sdholland 			/* Sparse layout. */
56406ca35587Sdholland 			if (flp->nfsfl_fhcnt > 1) {
56416ca35587Sdholland 				if (stripe_index >= flp->nfsfl_fhcnt)
56426ca35587Sdholland 					return (EIO);
56436ca35587Sdholland 				fhp = flp->nfsfl_fh[stripe_index];
56446ca35587Sdholland 			} else if (flp->nfsfl_fhcnt == 1)
56456ca35587Sdholland 				fhp = flp->nfsfl_fh[0];
56466ca35587Sdholland 			else
56476ca35587Sdholland 				fhp = np->n_fhp;
56486ca35587Sdholland 			io_off = off;
56496ca35587Sdholland 		}
56506ca35587Sdholland 		if ((flp->nfsfl_util & NFSFLAYUTIL_COMMIT_THRU_MDS) != 0)
56516ca35587Sdholland 			commit_thru_mds = 1;
56526ca35587Sdholland 		else
56536ca35587Sdholland 			commit_thru_mds = 0;
56546ca35587Sdholland 		if (rwflag == FREAD)
56556ca35587Sdholland 			error = nfsrpc_readds(vp, uiop, stateidp, eofp, *dspp,
56566ca35587Sdholland 			    io_off, xfer, fhp, cred, p);
56576ca35587Sdholland 		else {
56586ca35587Sdholland 			error = nfsrpc_writeds(vp, uiop, iomode, must_commit,
56596ca35587Sdholland 			    stateidp, *dspp, io_off, xfer, fhp, commit_thru_mds,
56606ca35587Sdholland 			    cred, p);
56616ca35587Sdholland 			if (error == 0) {
56626ca35587Sdholland 				NFSLOCKCLSTATE();
56636ca35587Sdholland 				lyp->nfsly_flags |= NFSLY_WRITTEN;
56646ca35587Sdholland 				NFSUNLOCKCLSTATE();
56656ca35587Sdholland 			}
56666ca35587Sdholland 		}
56676ca35587Sdholland 		if (error == 0) {
56686ca35587Sdholland 			transfer = stripe_unit_size;
56696ca35587Sdholland 			stripe_pos = (stripe_pos + 1) % dp->nfsdi_stripecnt;
56706ca35587Sdholland 			len -= xfer;
56716ca35587Sdholland 			off += xfer;
56726ca35587Sdholland 		}
56736ca35587Sdholland 	}
56746ca35587Sdholland 	return (error);
56756ca35587Sdholland }
56766ca35587Sdholland 
56776ca35587Sdholland /*
56786ca35587Sdholland  * The actual read RPC done to a DS.
56796ca35587Sdholland  */
56806ca35587Sdholland static int
nfsrpc_readds(vnode_t vp,struct uio * uiop,nfsv4stateid_t * stateidp,int * eofp,struct nfsclds * dsp,uint64_t io_off,int len,struct nfsfh * fhp,struct ucred * cred,NFSPROC_T * p)56816ca35587Sdholland nfsrpc_readds(vnode_t vp, struct uio *uiop, nfsv4stateid_t *stateidp, int *eofp,
56826ca35587Sdholland     struct nfsclds *dsp, uint64_t io_off, int len, struct nfsfh *fhp,
56836ca35587Sdholland     struct ucred *cred, NFSPROC_T *p)
56846ca35587Sdholland {
56856ca35587Sdholland 	uint32_t *tl;
56866ca35587Sdholland 	int error, retlen;
56876ca35587Sdholland 	struct nfsrv_descript nfsd;
56886ca35587Sdholland 	struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
56896ca35587Sdholland 	struct nfsrv_descript *nd = &nfsd;
56906ca35587Sdholland 	struct nfssockreq *nrp;
56916ca35587Sdholland 
56926ca35587Sdholland 	nd->nd_mrep = NULL;
56936ca35587Sdholland 	nfscl_reqstart(nd, NFSPROC_READDS, nmp, fhp->nfh_fh, fhp->nfh_len,
56946ca35587Sdholland 	    NULL, &dsp->nfsclds_sess);
56956ca35587Sdholland 	nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSEQIDZERO);
56966ca35587Sdholland 	NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED * 3);
56976ca35587Sdholland 	txdr_hyper(io_off, tl);
56986ca35587Sdholland 	*(tl + 2) = txdr_unsigned(len);
56996ca35587Sdholland 	nrp = dsp->nfsclds_sockp;
57006ca35587Sdholland 	if (nrp == NULL)
57016ca35587Sdholland 		/* If NULL, use the MDS socket. */
57026ca35587Sdholland 		nrp = &nmp->nm_sockreq;
57036ca35587Sdholland 	error = newnfs_request(nd, nmp, NULL, nrp, vp, p, cred,
57046ca35587Sdholland 	    NFS_PROG, NFS_VER4, NULL, 1, NULL, &dsp->nfsclds_sess);
57056ca35587Sdholland 	if (error != 0)
57066ca35587Sdholland 		return (error);
57076ca35587Sdholland 	if (nd->nd_repstat != 0) {
57086ca35587Sdholland 		error = nd->nd_repstat;
57096ca35587Sdholland 		goto nfsmout;
57106ca35587Sdholland 	}
57116ca35587Sdholland 	NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
57126ca35587Sdholland 	*eofp = fxdr_unsigned(int, *tl);
57136ca35587Sdholland 	NFSM_STRSIZ(retlen, len);
57146ca35587Sdholland 	error = nfsm_mbufuio(nd, uiop, retlen);
57156ca35587Sdholland nfsmout:
57166ca35587Sdholland 	mbuf_freem(nd->nd_mrep);
57176ca35587Sdholland 	return (error);
57186ca35587Sdholland }
57196ca35587Sdholland 
57206ca35587Sdholland /*
57216ca35587Sdholland  * The actual write RPC done to a DS.
57226ca35587Sdholland  */
57236ca35587Sdholland static int
nfsrpc_writeds(vnode_t vp,struct uio * uiop,int * iomode,int * must_commit,nfsv4stateid_t * stateidp,struct nfsclds * dsp,uint64_t io_off,int len,struct nfsfh * fhp,int commit_thru_mds,struct ucred * cred,NFSPROC_T * p)57246ca35587Sdholland nfsrpc_writeds(vnode_t vp, struct uio *uiop, int *iomode, int *must_commit,
57256ca35587Sdholland     nfsv4stateid_t *stateidp, struct nfsclds *dsp, uint64_t io_off, int len,
57266ca35587Sdholland     struct nfsfh *fhp, int commit_thru_mds, struct ucred *cred, NFSPROC_T *p)
57276ca35587Sdholland {
57286ca35587Sdholland 	uint32_t *tl;
57296ca35587Sdholland 	struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
57306ca35587Sdholland 	int error, rlen, commit, committed = NFSWRITE_FILESYNC;
57316ca35587Sdholland 	int32_t backup;
57326ca35587Sdholland 	struct nfsrv_descript nfsd;
57336ca35587Sdholland 	struct nfsrv_descript *nd = &nfsd;
57346ca35587Sdholland 	struct nfssockreq *nrp;
57356ca35587Sdholland 
57366ca35587Sdholland 	KASSERT(uiop->uio_iovcnt == 1, ("nfs: writerpc iovcnt > 1"));
57376ca35587Sdholland 	nd->nd_mrep = NULL;
57386ca35587Sdholland 	nfscl_reqstart(nd, NFSPROC_WRITEDS, nmp, fhp->nfh_fh, fhp->nfh_len,
57396ca35587Sdholland 	    NULL, &dsp->nfsclds_sess);
57406ca35587Sdholland 	nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSEQIDZERO);
57416ca35587Sdholland 	NFSM_BUILD(tl, uint32_t *, NFSX_HYPER + 2 * NFSX_UNSIGNED);
57426ca35587Sdholland 	txdr_hyper(io_off, tl);
57436ca35587Sdholland 	tl += 2;
57446ca35587Sdholland 	*tl++ = txdr_unsigned(*iomode);
57456ca35587Sdholland 	*tl = txdr_unsigned(len);
57466ca35587Sdholland 	nfsm_uiombuf(nd, uiop, len);
57476ca35587Sdholland 	nrp = dsp->nfsclds_sockp;
57486ca35587Sdholland 	if (nrp == NULL)
57496ca35587Sdholland 		/* If NULL, use the MDS socket. */
57506ca35587Sdholland 		nrp = &nmp->nm_sockreq;
57516ca35587Sdholland 	error = newnfs_request(nd, nmp, NULL, nrp, vp, p, cred,
57526ca35587Sdholland 	    NFS_PROG, NFS_VER4, NULL, 1, NULL, &dsp->nfsclds_sess);
57536ca35587Sdholland 	if (error != 0)
57546ca35587Sdholland 		return (error);
57556ca35587Sdholland 	if (nd->nd_repstat != 0) {
57566ca35587Sdholland 		/*
57576ca35587Sdholland 		 * In case the rpc gets retried, roll
57586ca35587Sdholland 		 * the uio fileds changed by nfsm_uiombuf()
57596ca35587Sdholland 		 * back.
57606ca35587Sdholland 		 */
57616ca35587Sdholland 		uiop->uio_offset -= len;
57626ca35587Sdholland 		uio_uio_resid_add(uiop, len);
57636ca35587Sdholland 		uio_iov_base_add(uiop, -len);
57646ca35587Sdholland 		uio_iov_len_add(uiop, len);
57656ca35587Sdholland 		error = nd->nd_repstat;
57666ca35587Sdholland 	} else {
57676ca35587Sdholland 		NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED + NFSX_VERF);
57686ca35587Sdholland 		rlen = fxdr_unsigned(int, *tl++);
57696ca35587Sdholland 		if (rlen == 0) {
57706ca35587Sdholland 			error = NFSERR_IO;
57716ca35587Sdholland 			goto nfsmout;
57726ca35587Sdholland 		} else if (rlen < len) {
57736ca35587Sdholland 			backup = len - rlen;
57746ca35587Sdholland 			uio_iov_base_add(uiop, -(backup));
57756ca35587Sdholland 			uio_iov_len_add(uiop, backup);
57766ca35587Sdholland 			uiop->uio_offset -= backup;
57776ca35587Sdholland 			uio_uio_resid_add(uiop, backup);
57786ca35587Sdholland 			len = rlen;
57796ca35587Sdholland 		}
57806ca35587Sdholland 		commit = fxdr_unsigned(int, *tl++);
57816ca35587Sdholland 
57826ca35587Sdholland 		/*
5783e81f0ea2Spgoyette 		 * Return the lowest commitment level
57846ca35587Sdholland 		 * obtained by any of the RPCs.
57856ca35587Sdholland 		 */
57866ca35587Sdholland 		if (committed == NFSWRITE_FILESYNC)
57876ca35587Sdholland 			committed = commit;
57886ca35587Sdholland 		else if (committed == NFSWRITE_DATASYNC &&
57896ca35587Sdholland 		    commit == NFSWRITE_UNSTABLE)
57906ca35587Sdholland 			committed = commit;
57916ca35587Sdholland 		if (commit_thru_mds != 0) {
57926ca35587Sdholland 			NFSLOCKMNT(nmp);
57936ca35587Sdholland 			if (!NFSHASWRITEVERF(nmp)) {
57946ca35587Sdholland 				NFSBCOPY(tl, nmp->nm_verf, NFSX_VERF);
57956ca35587Sdholland 				NFSSETWRITEVERF(nmp);
57966ca35587Sdholland 	    		} else if (NFSBCMP(tl, nmp->nm_verf, NFSX_VERF)) {
57976ca35587Sdholland 				*must_commit = 1;
57986ca35587Sdholland 				NFSBCOPY(tl, nmp->nm_verf, NFSX_VERF);
57996ca35587Sdholland 			}
58006ca35587Sdholland 			NFSUNLOCKMNT(nmp);
58016ca35587Sdholland 		} else {
58026ca35587Sdholland 			NFSLOCKDS(dsp);
58036ca35587Sdholland 			if ((dsp->nfsclds_flags & NFSCLDS_HASWRITEVERF) == 0) {
58046ca35587Sdholland 				NFSBCOPY(tl, dsp->nfsclds_verf, NFSX_VERF);
58056ca35587Sdholland 				dsp->nfsclds_flags |= NFSCLDS_HASWRITEVERF;
58066ca35587Sdholland 			} else if (NFSBCMP(tl, dsp->nfsclds_verf, NFSX_VERF)) {
58076ca35587Sdholland 				*must_commit = 1;
58086ca35587Sdholland 				NFSBCOPY(tl, dsp->nfsclds_verf, NFSX_VERF);
58096ca35587Sdholland 			}
58106ca35587Sdholland 			NFSUNLOCKDS(dsp);
58116ca35587Sdholland 		}
58126ca35587Sdholland 	}
58136ca35587Sdholland nfsmout:
58146ca35587Sdholland 	mbuf_freem(nd->nd_mrep);
58156ca35587Sdholland 	*iomode = committed;
58166ca35587Sdholland 	if (nd->nd_repstat != 0 && error == 0)
58176ca35587Sdholland 		error = nd->nd_repstat;
58186ca35587Sdholland 	return (error);
58196ca35587Sdholland }
58206ca35587Sdholland 
58216ca35587Sdholland /*
58226ca35587Sdholland  * Free up the nfsclds structure.
58236ca35587Sdholland  */
58246ca35587Sdholland void
nfscl_freenfsclds(struct nfsclds * dsp)58256ca35587Sdholland nfscl_freenfsclds(struct nfsclds *dsp)
58266ca35587Sdholland {
58276ca35587Sdholland 	int i;
58286ca35587Sdholland 
58296ca35587Sdholland 	if (dsp == NULL)
58306ca35587Sdholland 		return;
58316ca35587Sdholland 	if (dsp->nfsclds_sockp != NULL) {
58326ca35587Sdholland 		NFSFREECRED(dsp->nfsclds_sockp->nr_cred);
58336ca35587Sdholland 		NFSFREEMUTEX(&dsp->nfsclds_sockp->nr_mtx);
58346ca35587Sdholland 		free(dsp->nfsclds_sockp->nr_nam, M_SONAME);
58356ca35587Sdholland 		free(dsp->nfsclds_sockp, M_NFSSOCKREQ);
58366ca35587Sdholland 	}
58376ca35587Sdholland 	NFSFREEMUTEX(&dsp->nfsclds_mtx);
58386ca35587Sdholland 	NFSFREEMUTEX(&dsp->nfsclds_sess.nfsess_mtx);
58396ca35587Sdholland 	for (i = 0; i < NFSV4_CBSLOTS; i++) {
5840*481d3881Srin 		m_freem(dsp->nfsclds_sess.nfsess_cbslots[i].nfssl_reply);
58416ca35587Sdholland 	}
58426ca35587Sdholland 	free(dsp, M_NFSCLDS);
58436ca35587Sdholland }
58446ca35587Sdholland 
58456ca35587Sdholland static enum nfsclds_state
nfscl_getsameserver(struct nfsmount * nmp,struct nfsclds * newdsp,struct nfsclds ** retdspp)58466ca35587Sdholland nfscl_getsameserver(struct nfsmount *nmp, struct nfsclds *newdsp,
58476ca35587Sdholland     struct nfsclds **retdspp)
58486ca35587Sdholland {
58496ca35587Sdholland 	struct nfsclds *dsp, *cur_dsp;
58506ca35587Sdholland 
58516ca35587Sdholland 	/*
58526ca35587Sdholland 	 * Search the list of nfsclds structures for one with the same
58536ca35587Sdholland 	 * server.
58546ca35587Sdholland 	 */
58556ca35587Sdholland 	cur_dsp = NULL;
58566ca35587Sdholland 	TAILQ_FOREACH(dsp, &nmp->nm_sess, nfsclds_list) {
58576ca35587Sdholland 		if (dsp->nfsclds_servownlen == newdsp->nfsclds_servownlen &&
58586ca35587Sdholland 		    dsp->nfsclds_servownlen != 0 &&
58596ca35587Sdholland 		    !NFSBCMP(dsp->nfsclds_serverown, newdsp->nfsclds_serverown,
58606ca35587Sdholland 		    dsp->nfsclds_servownlen)) {
58616ca35587Sdholland 			NFSCL_DEBUG(4, "fnd same fdsp=%p dsp=%p flg=0x%x\n",
58626ca35587Sdholland 			    TAILQ_FIRST(&nmp->nm_sess), dsp,
58636ca35587Sdholland 			    dsp->nfsclds_flags);
58646ca35587Sdholland 			/* Server major id matches. */
58656ca35587Sdholland 			if ((dsp->nfsclds_flags & NFSCLDS_DS) != 0) {
58666ca35587Sdholland 				*retdspp = dsp;
58676ca35587Sdholland 				return (NFSDSP_USETHISSESSION);
58686ca35587Sdholland 			}
58696ca35587Sdholland 
58706ca35587Sdholland 			/*
58716ca35587Sdholland 			 * Note the first match, so it can be used for
58726ca35587Sdholland 			 * sequence'ing new sessions.
58736ca35587Sdholland 			 */
58746ca35587Sdholland 			if (cur_dsp == NULL)
58756ca35587Sdholland 				cur_dsp = dsp;
58766ca35587Sdholland 		}
58776ca35587Sdholland 	}
58786ca35587Sdholland 	if (cur_dsp != NULL) {
58796ca35587Sdholland 		*retdspp = cur_dsp;
58806ca35587Sdholland 		return (NFSDSP_SEQTHISSESSION);
58816ca35587Sdholland 	}
58826ca35587Sdholland 	return (NFSDSP_NOTFOUND);
58836ca35587Sdholland }
58846ca35587Sdholland 
58856ca35587Sdholland #ifdef notyet
58866ca35587Sdholland /*
58876ca35587Sdholland  * NFS commit rpc to a DS.
58886ca35587Sdholland  */
58896ca35587Sdholland static int
nfsrpc_commitds(vnode_t vp,uint64_t offset,int cnt,struct nfsclds * dsp,struct nfsfh * fhp,struct ucred * cred,NFSPROC_T * p,void * stuff)58906ca35587Sdholland nfsrpc_commitds(vnode_t vp, uint64_t offset, int cnt, struct nfsclds *dsp,
58916ca35587Sdholland     struct nfsfh *fhp, struct ucred *cred, NFSPROC_T *p, void *stuff)
58926ca35587Sdholland {
58936ca35587Sdholland 	uint32_t *tl;
58946ca35587Sdholland 	struct nfsrv_descript nfsd, *nd = &nfsd;
58956ca35587Sdholland 	struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
58966ca35587Sdholland 	struct nfssockreq *nrp;
58976ca35587Sdholland 	int error;
58986ca35587Sdholland 
58996ca35587Sdholland 	nfscl_reqstart(nd, NFSPROC_COMMITDS, nmp, fhp->nfh_fh, fhp->nfh_len,
59006ca35587Sdholland 	    NULL, &dsp->nfsclds_sess);
59016ca35587Sdholland 	NFSM_BUILD(tl, uint32_t *, NFSX_HYPER + NFSX_UNSIGNED);
59026ca35587Sdholland 	txdr_hyper(offset, tl);
59036ca35587Sdholland 	tl += 2;
59046ca35587Sdholland 	*tl = txdr_unsigned(cnt);
59056ca35587Sdholland 	nrp = dsp->nfsclds_sockp;
59066ca35587Sdholland 	if (nrp == NULL)
59076ca35587Sdholland 		/* If NULL, use the MDS socket. */
59086ca35587Sdholland 		nrp = &nmp->nm_sockreq;
59096ca35587Sdholland 	error = newnfs_request(nd, nmp, NULL, nrp, vp, p, cred,
59106ca35587Sdholland 	    NFS_PROG, NFS_VER4, NULL, 1, NULL, &dsp->nfsclds_sess);
59116ca35587Sdholland 	if (error)
59126ca35587Sdholland 		return (error);
59136ca35587Sdholland 	if (nd->nd_repstat == 0) {
59146ca35587Sdholland 		NFSM_DISSECT(tl, u_int32_t *, NFSX_VERF);
59156ca35587Sdholland 		NFSLOCKDS(dsp);
59166ca35587Sdholland 		if (NFSBCMP(tl, dsp->nfsclds_verf, NFSX_VERF)) {
59176ca35587Sdholland 			NFSBCOPY(tl, dsp->nfsclds_verf, NFSX_VERF);
59186ca35587Sdholland 			error = NFSERR_STALEWRITEVERF;
59196ca35587Sdholland 		}
59206ca35587Sdholland 		NFSUNLOCKDS(dsp);
59216ca35587Sdholland 	}
59226ca35587Sdholland nfsmout:
59236ca35587Sdholland 	if (error == 0 && nd->nd_repstat != 0)
59246ca35587Sdholland 		error = nd->nd_repstat;
59256ca35587Sdholland 	mbuf_freem(nd->nd_mrep);
59266ca35587Sdholland 	return (error);
59276ca35587Sdholland }
59286ca35587Sdholland #endif
59296ca35587Sdholland 
5930