xref: /freebsd/sys/fs/nfsclient/nfs_clrpcops.c (revision c99b67a7947ea215f9c1d44ec022680e98920cd1)
1 /*-
2  * Copyright (c) 1989, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Rick Macklem at The University of Guelph.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  */
33 
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
36 
37 /*
38  * Rpc op calls, generally called from the vnode op calls or through the
39  * buffer cache, for NFS v2, 3 and 4.
40  * These do not normally make any changes to vnode arguments or use
41  * structures that might change between the VFS variants. The returned
42  * arguments are all at the end, after the NFSPROC_T *p one.
43  */
44 
45 #ifndef APPLEKEXT
46 #include "opt_inet6.h"
47 
48 #include <fs/nfs/nfsport.h>
49 #include <sys/sysctl.h>
50 
51 SYSCTL_DECL(_vfs_nfs);
52 
53 static int	nfsignore_eexist = 0;
54 SYSCTL_INT(_vfs_nfs, OID_AUTO, ignore_eexist, CTLFLAG_RW,
55     &nfsignore_eexist, 0, "NFS ignore EEXIST replies for mkdir/symlink");
56 
57 /*
58  * Global variables
59  */
60 extern int nfs_numnfscbd;
61 extern struct timeval nfsboottime;
62 extern u_int32_t newnfs_false, newnfs_true;
63 extern nfstype nfsv34_type[9];
64 extern int nfsrv_useacl;
65 extern char nfsv4_callbackaddr[INET6_ADDRSTRLEN];
66 extern int nfscl_debuglevel;
67 NFSCLSTATEMUTEX;
68 int nfstest_outofseq = 0;
69 int nfscl_assumeposixlocks = 1;
70 int nfscl_enablecallb = 0;
71 short nfsv4_cbport = NFSV4_CBPORT;
72 int nfstest_openallsetattr = 0;
73 #endif	/* !APPLEKEXT */
74 
75 #define	DIRHDSIZ	offsetof(struct dirent, d_name)
76 
77 /*
78  * nfscl_getsameserver() can return one of three values:
79  * NFSDSP_USETHISSESSION - Use this session for the DS.
80  * NFSDSP_SEQTHISSESSION - Use the nfsclds_sequence field of this dsp for new
81  *     session.
82  * NFSDSP_NOTFOUND - No matching server was found.
83  */
84 enum nfsclds_state {
85 	NFSDSP_USETHISSESSION = 0,
86 	NFSDSP_SEQTHISSESSION = 1,
87 	NFSDSP_NOTFOUND = 2,
88 };
89 
90 static int nfsrpc_setattrrpc(vnode_t , struct vattr *, nfsv4stateid_t *,
91     struct ucred *, NFSPROC_T *, struct nfsvattr *, int *, void *);
92 static int nfsrpc_readrpc(vnode_t , struct uio *, struct ucred *,
93     nfsv4stateid_t *, NFSPROC_T *, struct nfsvattr *, int *, void *);
94 static int nfsrpc_writerpc(vnode_t , struct uio *, int *, int *,
95     struct ucred *, nfsv4stateid_t *, NFSPROC_T *, struct nfsvattr *, int *,
96     void *);
97 static int nfsrpc_createv23(vnode_t , char *, int, struct vattr *,
98     nfsquad_t, int, struct ucred *, NFSPROC_T *, struct nfsvattr *,
99     struct nfsvattr *, struct nfsfh **, int *, int *, void *);
100 static int nfsrpc_createv4(vnode_t , char *, int, struct vattr *,
101     nfsquad_t, int, struct nfsclowner *, struct nfscldeleg **, struct ucred *,
102     NFSPROC_T *, struct nfsvattr *, struct nfsvattr *, struct nfsfh **, int *,
103     int *, void *, int *);
104 static int nfsrpc_locku(struct nfsrv_descript *, struct nfsmount *,
105     struct nfscllockowner *, u_int64_t, u_int64_t,
106     u_int32_t, struct ucred *, NFSPROC_T *, int);
107 static int nfsrpc_setaclrpc(vnode_t, struct ucred *, NFSPROC_T *,
108     struct acl *, nfsv4stateid_t *, void *);
109 static int nfsrpc_getlayout(struct nfsmount *, vnode_t, struct nfsfh *, int,
110     uint32_t *, nfsv4stateid_t *, uint64_t, struct nfscllayout **,
111     struct ucred *, NFSPROC_T *);
112 static int nfsrpc_fillsa(struct nfsmount *, struct sockaddr_storage *,
113     struct nfsclds **, NFSPROC_T *);
114 static void nfscl_initsessionslots(struct nfsclsession *);
115 static int nfscl_doflayoutio(vnode_t, struct uio *, int *, int *, int *,
116     nfsv4stateid_t *, int, struct nfscldevinfo *, struct nfscllayout *,
117     struct nfsclflayout *, uint64_t, uint64_t, struct ucred *, NFSPROC_T *);
118 static int nfsrpc_readds(vnode_t, struct uio *, nfsv4stateid_t *, int *,
119     struct nfsclds *, uint64_t, int, struct nfsfh *, struct ucred *,
120     NFSPROC_T *);
121 static int nfsrpc_writeds(vnode_t, struct uio *, int *, int *,
122     nfsv4stateid_t *, struct nfsclds *, uint64_t, int,
123     struct nfsfh *, int, struct ucred *, NFSPROC_T *);
124 static enum nfsclds_state nfscl_getsameserver(struct nfsmount *,
125     struct nfsclds *, struct nfsclds **);
126 #ifdef notyet
127 static int nfsrpc_commitds(vnode_t, uint64_t, int, struct nfsclds *,
128     struct nfsfh *, struct ucred *, NFSPROC_T *, void *);
129 #endif
130 
131 /*
132  * nfs null call from vfs.
133  */
134 APPLESTATIC int
135 nfsrpc_null(vnode_t vp, struct ucred *cred, NFSPROC_T *p)
136 {
137 	int error;
138 	struct nfsrv_descript nfsd, *nd = &nfsd;
139 
140 	NFSCL_REQSTART(nd, NFSPROC_NULL, vp);
141 	error = nfscl_request(nd, vp, p, cred, NULL);
142 	if (nd->nd_repstat && !error)
143 		error = nd->nd_repstat;
144 	mbuf_freem(nd->nd_mrep);
145 	return (error);
146 }
147 
148 /*
149  * nfs access rpc op.
150  * For nfs version 3 and 4, use the access rpc to check accessibility. If file
151  * modes are changed on the server, accesses might still fail later.
152  */
153 APPLESTATIC int
154 nfsrpc_access(vnode_t vp, int acmode, struct ucred *cred,
155     NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp)
156 {
157 	int error;
158 	u_int32_t mode, rmode;
159 
160 	if (acmode & VREAD)
161 		mode = NFSACCESS_READ;
162 	else
163 		mode = 0;
164 	if (vnode_vtype(vp) == VDIR) {
165 		if (acmode & VWRITE)
166 			mode |= (NFSACCESS_MODIFY | NFSACCESS_EXTEND |
167 				 NFSACCESS_DELETE);
168 		if (acmode & VEXEC)
169 			mode |= NFSACCESS_LOOKUP;
170 	} else {
171 		if (acmode & VWRITE)
172 			mode |= (NFSACCESS_MODIFY | NFSACCESS_EXTEND);
173 		if (acmode & VEXEC)
174 			mode |= NFSACCESS_EXECUTE;
175 	}
176 
177 	/*
178 	 * Now, just call nfsrpc_accessrpc() to do the actual RPC.
179 	 */
180 	error = nfsrpc_accessrpc(vp, mode, cred, p, nap, attrflagp, &rmode,
181 	    NULL);
182 
183 	/*
184 	 * The NFS V3 spec does not clarify whether or not
185 	 * the returned access bits can be a superset of
186 	 * the ones requested, so...
187 	 */
188 	if (!error && (rmode & mode) != mode)
189 		error = EACCES;
190 	return (error);
191 }
192 
193 /*
194  * The actual rpc, separated out for Darwin.
195  */
196 APPLESTATIC int
197 nfsrpc_accessrpc(vnode_t vp, u_int32_t mode, struct ucred *cred,
198     NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, u_int32_t *rmodep,
199     void *stuff)
200 {
201 	u_int32_t *tl;
202 	u_int32_t supported, rmode;
203 	int error;
204 	struct nfsrv_descript nfsd, *nd = &nfsd;
205 	nfsattrbit_t attrbits;
206 
207 	*attrflagp = 0;
208 	supported = mode;
209 	NFSCL_REQSTART(nd, NFSPROC_ACCESS, vp);
210 	NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
211 	*tl = txdr_unsigned(mode);
212 	if (nd->nd_flag & ND_NFSV4) {
213 		/*
214 		 * And do a Getattr op.
215 		 */
216 		NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
217 		*tl = txdr_unsigned(NFSV4OP_GETATTR);
218 		NFSGETATTR_ATTRBIT(&attrbits);
219 		(void) nfsrv_putattrbit(nd, &attrbits);
220 	}
221 	error = nfscl_request(nd, vp, p, cred, stuff);
222 	if (error)
223 		return (error);
224 	if (nd->nd_flag & ND_NFSV3) {
225 		error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
226 		if (error)
227 			goto nfsmout;
228 	}
229 	if (!nd->nd_repstat) {
230 		if (nd->nd_flag & ND_NFSV4) {
231 			NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
232 			supported = fxdr_unsigned(u_int32_t, *tl++);
233 		} else {
234 			NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
235 		}
236 		rmode = fxdr_unsigned(u_int32_t, *tl);
237 		if (nd->nd_flag & ND_NFSV4)
238 			error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
239 
240 		/*
241 		 * It's not obvious what should be done about
242 		 * unsupported access modes. For now, be paranoid
243 		 * and clear the unsupported ones.
244 		 */
245 		rmode &= supported;
246 		*rmodep = rmode;
247 	} else
248 		error = nd->nd_repstat;
249 nfsmout:
250 	mbuf_freem(nd->nd_mrep);
251 	return (error);
252 }
253 
254 /*
255  * nfs open rpc
256  */
257 APPLESTATIC int
258 nfsrpc_open(vnode_t vp, int amode, struct ucred *cred, NFSPROC_T *p)
259 {
260 	struct nfsclopen *op;
261 	struct nfscldeleg *dp;
262 	struct nfsfh *nfhp;
263 	struct nfsnode *np = VTONFS(vp);
264 	struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
265 	u_int32_t mode, clidrev;
266 	int ret, newone, error, expireret = 0, retrycnt;
267 
268 	/*
269 	 * For NFSv4, Open Ops are only done on Regular Files.
270 	 */
271 	if (vnode_vtype(vp) != VREG)
272 		return (0);
273 	mode = 0;
274 	if (amode & FREAD)
275 		mode |= NFSV4OPEN_ACCESSREAD;
276 	if (amode & FWRITE)
277 		mode |= NFSV4OPEN_ACCESSWRITE;
278 	nfhp = np->n_fhp;
279 
280 	retrycnt = 0;
281 #ifdef notdef
282 { char name[100]; int namel;
283 namel = (np->n_v4->n4_namelen < 100) ? np->n_v4->n4_namelen : 99;
284 bcopy(NFS4NODENAME(np->n_v4), name, namel);
285 name[namel] = '\0';
286 printf("rpcopen p=0x%x name=%s",p->p_pid,name);
287 if (nfhp->nfh_len > 0) printf(" fh=0x%x\n",nfhp->nfh_fh[12]);
288 else printf(" fhl=0\n");
289 }
290 #endif
291 	do {
292 	    dp = NULL;
293 	    error = nfscl_open(vp, nfhp->nfh_fh, nfhp->nfh_len, mode, 1,
294 		cred, p, NULL, &op, &newone, &ret, 1);
295 	    if (error) {
296 		return (error);
297 	    }
298 	    if (nmp->nm_clp != NULL)
299 		clidrev = nmp->nm_clp->nfsc_clientidrev;
300 	    else
301 		clidrev = 0;
302 	    if (ret == NFSCLOPEN_DOOPEN) {
303 		if (np->n_v4 != NULL) {
304 			error = nfsrpc_openrpc(nmp, vp, np->n_v4->n4_data,
305 			   np->n_v4->n4_fhlen, np->n_fhp->nfh_fh,
306 			   np->n_fhp->nfh_len, mode, op,
307 			   NFS4NODENAME(np->n_v4), np->n_v4->n4_namelen, &dp,
308 			   0, 0x0, cred, p, 0, 0);
309 			if (dp != NULL) {
310 #ifdef APPLE
311 				OSBitAndAtomic((int32_t)~NDELEGMOD, (UInt32 *)&np->n_flag);
312 #else
313 				NFSLOCKNODE(np);
314 				np->n_flag &= ~NDELEGMOD;
315 				/*
316 				 * Invalidate the attribute cache, so that
317 				 * attributes that pre-date the issue of a
318 				 * delegation are not cached, since the
319 				 * cached attributes will remain valid while
320 				 * the delegation is held.
321 				 */
322 				NFSINVALATTRCACHE(np);
323 				NFSUNLOCKNODE(np);
324 #endif
325 				(void) nfscl_deleg(nmp->nm_mountp,
326 				    op->nfso_own->nfsow_clp,
327 				    nfhp->nfh_fh, nfhp->nfh_len, cred, p, &dp);
328 			}
329 		} else {
330 			error = EIO;
331 		}
332 		newnfs_copyincred(cred, &op->nfso_cred);
333 	    } else if (ret == NFSCLOPEN_SETCRED)
334 		/*
335 		 * This is a new local open on a delegation. It needs
336 		 * to have credentials so that an open can be done
337 		 * against the server during recovery.
338 		 */
339 		newnfs_copyincred(cred, &op->nfso_cred);
340 
341 	    /*
342 	     * nfso_opencnt is the count of how many VOP_OPEN()s have
343 	     * been done on this Open successfully and a VOP_CLOSE()
344 	     * is expected for each of these.
345 	     * If error is non-zero, don't increment it, since the Open
346 	     * hasn't succeeded yet.
347 	     */
348 	    if (!error)
349 		op->nfso_opencnt++;
350 	    nfscl_openrelease(nmp, op, error, newone);
351 	    if (error == NFSERR_GRACE || error == NFSERR_STALECLIENTID ||
352 		error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
353 		error == NFSERR_BADSESSION) {
354 		(void) nfs_catnap(PZERO, error, "nfs_open");
355 	    } else if ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID)
356 		&& clidrev != 0) {
357 		expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
358 		retrycnt++;
359 	    }
360 	} while (error == NFSERR_GRACE || error == NFSERR_STALECLIENTID ||
361 	    error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
362 	    error == NFSERR_BADSESSION ||
363 	    ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
364 	     expireret == 0 && clidrev != 0 && retrycnt < 4));
365 	if (error && retrycnt >= 4)
366 		error = EIO;
367 	return (error);
368 }
369 
370 /*
371  * the actual open rpc
372  */
373 APPLESTATIC int
374 nfsrpc_openrpc(struct nfsmount *nmp, vnode_t vp, u_int8_t *nfhp, int fhlen,
375     u_int8_t *newfhp, int newfhlen, u_int32_t mode, struct nfsclopen *op,
376     u_int8_t *name, int namelen, struct nfscldeleg **dpp,
377     int reclaim, u_int32_t delegtype, struct ucred *cred, NFSPROC_T *p,
378     int syscred, int recursed)
379 {
380 	u_int32_t *tl;
381 	struct nfsrv_descript nfsd, *nd = &nfsd;
382 	struct nfscldeleg *dp, *ndp = NULL;
383 	struct nfsvattr nfsva;
384 	u_int32_t rflags, deleg;
385 	nfsattrbit_t attrbits;
386 	int error, ret, acesize, limitby;
387 	struct nfsclsession *tsep;
388 
389 	dp = *dpp;
390 	*dpp = NULL;
391 	nfscl_reqstart(nd, NFSPROC_OPEN, nmp, nfhp, fhlen, NULL, NULL);
392 	NFSM_BUILD(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
393 	*tl++ = txdr_unsigned(op->nfso_own->nfsow_seqid);
394 	*tl++ = txdr_unsigned(mode & NFSV4OPEN_ACCESSBOTH);
395 	*tl++ = txdr_unsigned((mode >> NFSLCK_SHIFT) & NFSV4OPEN_DENYBOTH);
396 	tsep = nfsmnt_mdssession(nmp);
397 	*tl++ = tsep->nfsess_clientid.lval[0];
398 	*tl = tsep->nfsess_clientid.lval[1];
399 	(void) nfsm_strtom(nd, op->nfso_own->nfsow_owner, NFSV4CL_LOCKNAMELEN);
400 	NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
401 	*tl++ = txdr_unsigned(NFSV4OPEN_NOCREATE);
402 	if (reclaim) {
403 		*tl = txdr_unsigned(NFSV4OPEN_CLAIMPREVIOUS);
404 		NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
405 		*tl = txdr_unsigned(delegtype);
406 	} else {
407 		if (dp != NULL) {
408 			*tl = txdr_unsigned(NFSV4OPEN_CLAIMDELEGATECUR);
409 			NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID);
410 			if (NFSHASNFSV4N(nmp))
411 				*tl++ = 0;
412 			else
413 				*tl++ = dp->nfsdl_stateid.seqid;
414 			*tl++ = dp->nfsdl_stateid.other[0];
415 			*tl++ = dp->nfsdl_stateid.other[1];
416 			*tl = dp->nfsdl_stateid.other[2];
417 		} else {
418 			*tl = txdr_unsigned(NFSV4OPEN_CLAIMNULL);
419 		}
420 		(void) nfsm_strtom(nd, name, namelen);
421 	}
422 	NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
423 	*tl = txdr_unsigned(NFSV4OP_GETATTR);
424 	NFSZERO_ATTRBIT(&attrbits);
425 	NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_CHANGE);
426 	NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEMODIFY);
427 	(void) nfsrv_putattrbit(nd, &attrbits);
428 	if (syscred)
429 		nd->nd_flag |= ND_USEGSSNAME;
430 	error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, vp, p, cred,
431 	    NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
432 	if (error)
433 		return (error);
434 	NFSCL_INCRSEQID(op->nfso_own->nfsow_seqid, nd);
435 	if (!nd->nd_repstat) {
436 		NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
437 		    6 * NFSX_UNSIGNED);
438 		op->nfso_stateid.seqid = *tl++;
439 		op->nfso_stateid.other[0] = *tl++;
440 		op->nfso_stateid.other[1] = *tl++;
441 		op->nfso_stateid.other[2] = *tl;
442 		rflags = fxdr_unsigned(u_int32_t, *(tl + 6));
443 		error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
444 		if (error)
445 			goto nfsmout;
446 		NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
447 		deleg = fxdr_unsigned(u_int32_t, *tl);
448 		if (deleg == NFSV4OPEN_DELEGATEREAD ||
449 		    deleg == NFSV4OPEN_DELEGATEWRITE) {
450 			if (!(op->nfso_own->nfsow_clp->nfsc_flags &
451 			      NFSCLFLAGS_FIRSTDELEG))
452 				op->nfso_own->nfsow_clp->nfsc_flags |=
453 				  (NFSCLFLAGS_FIRSTDELEG | NFSCLFLAGS_GOTDELEG);
454 			MALLOC(ndp, struct nfscldeleg *,
455 			    sizeof (struct nfscldeleg) + newfhlen,
456 			    M_NFSCLDELEG, M_WAITOK);
457 			LIST_INIT(&ndp->nfsdl_owner);
458 			LIST_INIT(&ndp->nfsdl_lock);
459 			ndp->nfsdl_clp = op->nfso_own->nfsow_clp;
460 			ndp->nfsdl_fhlen = newfhlen;
461 			NFSBCOPY(newfhp, ndp->nfsdl_fh, newfhlen);
462 			newnfs_copyincred(cred, &ndp->nfsdl_cred);
463 			nfscl_lockinit(&ndp->nfsdl_rwlock);
464 			NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
465 			    NFSX_UNSIGNED);
466 			ndp->nfsdl_stateid.seqid = *tl++;
467 			ndp->nfsdl_stateid.other[0] = *tl++;
468 			ndp->nfsdl_stateid.other[1] = *tl++;
469 			ndp->nfsdl_stateid.other[2] = *tl++;
470 			ret = fxdr_unsigned(int, *tl);
471 			if (deleg == NFSV4OPEN_DELEGATEWRITE) {
472 				ndp->nfsdl_flags = NFSCLDL_WRITE;
473 				/*
474 				 * Indicates how much the file can grow.
475 				 */
476 				NFSM_DISSECT(tl, u_int32_t *,
477 				    3 * NFSX_UNSIGNED);
478 				limitby = fxdr_unsigned(int, *tl++);
479 				switch (limitby) {
480 				case NFSV4OPEN_LIMITSIZE:
481 					ndp->nfsdl_sizelimit = fxdr_hyper(tl);
482 					break;
483 				case NFSV4OPEN_LIMITBLOCKS:
484 					ndp->nfsdl_sizelimit =
485 					    fxdr_unsigned(u_int64_t, *tl++);
486 					ndp->nfsdl_sizelimit *=
487 					    fxdr_unsigned(u_int64_t, *tl);
488 					break;
489 				default:
490 					error = NFSERR_BADXDR;
491 					goto nfsmout;
492 				}
493 			} else {
494 				ndp->nfsdl_flags = NFSCLDL_READ;
495 			}
496 			if (ret)
497 				ndp->nfsdl_flags |= NFSCLDL_RECALL;
498 			error = nfsrv_dissectace(nd, &ndp->nfsdl_ace, &ret,
499 			    &acesize, p);
500 			if (error)
501 				goto nfsmout;
502 		} else if (deleg != NFSV4OPEN_DELEGATENONE) {
503 			error = NFSERR_BADXDR;
504 			goto nfsmout;
505 		}
506 		NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
507 		error = nfsv4_loadattr(nd, NULL, &nfsva, NULL,
508 		    NULL, 0, NULL, NULL, NULL, NULL, NULL, 0,
509 		    NULL, NULL, NULL, p, cred);
510 		if (error)
511 			goto nfsmout;
512 		if (ndp != NULL) {
513 			ndp->nfsdl_change = nfsva.na_filerev;
514 			ndp->nfsdl_modtime = nfsva.na_mtime;
515 			ndp->nfsdl_flags |= NFSCLDL_MODTIMESET;
516 		}
517 		if (!reclaim && (rflags & NFSV4OPEN_RESULTCONFIRM)) {
518 		    do {
519 			ret = nfsrpc_openconfirm(vp, newfhp, newfhlen, op,
520 			    cred, p);
521 			if (ret == NFSERR_DELAY)
522 			    (void) nfs_catnap(PZERO, ret, "nfs_open");
523 		    } while (ret == NFSERR_DELAY);
524 		    error = ret;
525 		}
526 		if ((rflags & NFSV4OPEN_LOCKTYPEPOSIX) ||
527 		    nfscl_assumeposixlocks)
528 		    op->nfso_posixlock = 1;
529 		else
530 		    op->nfso_posixlock = 0;
531 
532 		/*
533 		 * If the server is handing out delegations, but we didn't
534 		 * get one because an OpenConfirm was required, try the
535 		 * Open again, to get a delegation. This is a harmless no-op,
536 		 * from a server's point of view.
537 		 */
538 		if (!reclaim && (rflags & NFSV4OPEN_RESULTCONFIRM) &&
539 		    (op->nfso_own->nfsow_clp->nfsc_flags & NFSCLFLAGS_GOTDELEG)
540 		    && !error && dp == NULL && ndp == NULL && !recursed) {
541 		    do {
542 			ret = nfsrpc_openrpc(nmp, vp, nfhp, fhlen, newfhp,
543 			    newfhlen, mode, op, name, namelen, &ndp, 0, 0x0,
544 			    cred, p, syscred, 1);
545 			if (ret == NFSERR_DELAY)
546 			    (void) nfs_catnap(PZERO, ret, "nfs_open2");
547 		    } while (ret == NFSERR_DELAY);
548 		    if (ret) {
549 			if (ndp != NULL) {
550 				FREE((caddr_t)ndp, M_NFSCLDELEG);
551 				ndp = NULL;
552 			}
553 			if (ret == NFSERR_STALECLIENTID ||
554 			    ret == NFSERR_STALEDONTRECOVER ||
555 			    ret == NFSERR_BADSESSION)
556 				error = ret;
557 		    }
558 		}
559 	}
560 	if (nd->nd_repstat != 0 && error == 0)
561 		error = nd->nd_repstat;
562 	if (error == NFSERR_STALECLIENTID)
563 		nfscl_initiate_recovery(op->nfso_own->nfsow_clp);
564 nfsmout:
565 	if (!error)
566 		*dpp = ndp;
567 	else if (ndp != NULL)
568 		FREE((caddr_t)ndp, M_NFSCLDELEG);
569 	mbuf_freem(nd->nd_mrep);
570 	return (error);
571 }
572 
573 /*
574  * open downgrade rpc
575  */
576 APPLESTATIC int
577 nfsrpc_opendowngrade(vnode_t vp, u_int32_t mode, struct nfsclopen *op,
578     struct ucred *cred, NFSPROC_T *p)
579 {
580 	u_int32_t *tl;
581 	struct nfsrv_descript nfsd, *nd = &nfsd;
582 	int error;
583 
584 	NFSCL_REQSTART(nd, NFSPROC_OPENDOWNGRADE, vp);
585 	NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID + 3 * NFSX_UNSIGNED);
586 	if (NFSHASNFSV4N(VFSTONFS(vnode_mount(vp))))
587 		*tl++ = 0;
588 	else
589 		*tl++ = op->nfso_stateid.seqid;
590 	*tl++ = op->nfso_stateid.other[0];
591 	*tl++ = op->nfso_stateid.other[1];
592 	*tl++ = op->nfso_stateid.other[2];
593 	*tl++ = txdr_unsigned(op->nfso_own->nfsow_seqid);
594 	*tl++ = txdr_unsigned(mode & NFSV4OPEN_ACCESSBOTH);
595 	*tl = txdr_unsigned((mode >> NFSLCK_SHIFT) & NFSV4OPEN_DENYBOTH);
596 	error = nfscl_request(nd, vp, p, cred, NULL);
597 	if (error)
598 		return (error);
599 	NFSCL_INCRSEQID(op->nfso_own->nfsow_seqid, nd);
600 	if (!nd->nd_repstat) {
601 		NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID);
602 		op->nfso_stateid.seqid = *tl++;
603 		op->nfso_stateid.other[0] = *tl++;
604 		op->nfso_stateid.other[1] = *tl++;
605 		op->nfso_stateid.other[2] = *tl;
606 	}
607 	if (nd->nd_repstat && error == 0)
608 		error = nd->nd_repstat;
609 	if (error == NFSERR_STALESTATEID)
610 		nfscl_initiate_recovery(op->nfso_own->nfsow_clp);
611 nfsmout:
612 	mbuf_freem(nd->nd_mrep);
613 	return (error);
614 }
615 
616 /*
617  * V4 Close operation.
618  */
619 APPLESTATIC int
620 nfsrpc_close(vnode_t vp, int doclose, NFSPROC_T *p)
621 {
622 	struct nfsclclient *clp;
623 	int error;
624 
625 	if (vnode_vtype(vp) != VREG)
626 		return (0);
627 	if (doclose)
628 		error = nfscl_doclose(vp, &clp, p);
629 	else
630 		error = nfscl_getclose(vp, &clp);
631 	if (error)
632 		return (error);
633 
634 	nfscl_clientrelease(clp);
635 	return (0);
636 }
637 
638 /*
639  * Close the open.
640  */
641 APPLESTATIC void
642 nfsrpc_doclose(struct nfsmount *nmp, struct nfsclopen *op, NFSPROC_T *p)
643 {
644 	struct nfsrv_descript nfsd, *nd = &nfsd;
645 	struct nfscllockowner *lp, *nlp;
646 	struct nfscllock *lop, *nlop;
647 	struct ucred *tcred;
648 	u_int64_t off = 0, len = 0;
649 	u_int32_t type = NFSV4LOCKT_READ;
650 	int error, do_unlock, trycnt;
651 
652 	tcred = newnfs_getcred();
653 	newnfs_copycred(&op->nfso_cred, tcred);
654 	/*
655 	 * (Theoretically this could be done in the same
656 	 *  compound as the close, but having multiple
657 	 *  sequenced Ops in the same compound might be
658 	 *  too scary for some servers.)
659 	 */
660 	if (op->nfso_posixlock) {
661 		off = 0;
662 		len = NFS64BITSSET;
663 		type = NFSV4LOCKT_READ;
664 	}
665 
666 	/*
667 	 * Since this function is only called from VOP_INACTIVE(), no
668 	 * other thread will be manipulating this Open. As such, the
669 	 * lock lists are not being changed by other threads, so it should
670 	 * be safe to do this without locking.
671 	 */
672 	LIST_FOREACH(lp, &op->nfso_lock, nfsl_list) {
673 		do_unlock = 1;
674 		LIST_FOREACH_SAFE(lop, &lp->nfsl_lock, nfslo_list, nlop) {
675 			if (op->nfso_posixlock == 0) {
676 				off = lop->nfslo_first;
677 				len = lop->nfslo_end - lop->nfslo_first;
678 				if (lop->nfslo_type == F_WRLCK)
679 					type = NFSV4LOCKT_WRITE;
680 				else
681 					type = NFSV4LOCKT_READ;
682 			}
683 			if (do_unlock) {
684 				trycnt = 0;
685 				do {
686 					error = nfsrpc_locku(nd, nmp, lp, off,
687 					    len, type, tcred, p, 0);
688 					if ((nd->nd_repstat == NFSERR_GRACE ||
689 					    nd->nd_repstat == NFSERR_DELAY) &&
690 					    error == 0)
691 						(void) nfs_catnap(PZERO,
692 						    (int)nd->nd_repstat,
693 						    "nfs_close");
694 				} while ((nd->nd_repstat == NFSERR_GRACE ||
695 				    nd->nd_repstat == NFSERR_DELAY) &&
696 				    error == 0 && trycnt++ < 5);
697 				if (op->nfso_posixlock)
698 					do_unlock = 0;
699 			}
700 			nfscl_freelock(lop, 0);
701 		}
702 		/*
703 		 * Do a ReleaseLockOwner.
704 		 * The lock owner name nfsl_owner may be used by other opens for
705 		 * other files but the lock_owner4 name that nfsrpc_rellockown()
706 		 * puts on the wire has the file handle for this file appended
707 		 * to it, so it can be done now.
708 		 */
709 		(void)nfsrpc_rellockown(nmp, lp, lp->nfsl_open->nfso_fh,
710 		    lp->nfsl_open->nfso_fhlen, tcred, p);
711 	}
712 
713 	/*
714 	 * There could be other Opens for different files on the same
715 	 * OpenOwner, so locking is required.
716 	 */
717 	NFSLOCKCLSTATE();
718 	nfscl_lockexcl(&op->nfso_own->nfsow_rwlock, NFSCLSTATEMUTEXPTR);
719 	NFSUNLOCKCLSTATE();
720 	do {
721 		error = nfscl_tryclose(op, tcred, nmp, p);
722 		if (error == NFSERR_GRACE)
723 			(void) nfs_catnap(PZERO, error, "nfs_close");
724 	} while (error == NFSERR_GRACE);
725 	NFSLOCKCLSTATE();
726 	nfscl_lockunlock(&op->nfso_own->nfsow_rwlock);
727 
728 	LIST_FOREACH_SAFE(lp, &op->nfso_lock, nfsl_list, nlp)
729 		nfscl_freelockowner(lp, 0);
730 	nfscl_freeopen(op, 0);
731 	NFSUNLOCKCLSTATE();
732 	NFSFREECRED(tcred);
733 }
734 
735 /*
736  * The actual Close RPC.
737  */
738 APPLESTATIC int
739 nfsrpc_closerpc(struct nfsrv_descript *nd, struct nfsmount *nmp,
740     struct nfsclopen *op, struct ucred *cred, NFSPROC_T *p,
741     int syscred)
742 {
743 	u_int32_t *tl;
744 	int error;
745 
746 	nfscl_reqstart(nd, NFSPROC_CLOSE, nmp, op->nfso_fh,
747 	    op->nfso_fhlen, NULL, NULL);
748 	NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED + NFSX_STATEID);
749 	*tl++ = txdr_unsigned(op->nfso_own->nfsow_seqid);
750 	if (NFSHASNFSV4N(nmp))
751 		*tl++ = 0;
752 	else
753 		*tl++ = op->nfso_stateid.seqid;
754 	*tl++ = op->nfso_stateid.other[0];
755 	*tl++ = op->nfso_stateid.other[1];
756 	*tl = op->nfso_stateid.other[2];
757 	if (syscred)
758 		nd->nd_flag |= ND_USEGSSNAME;
759 	error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
760 	    NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
761 	if (error)
762 		return (error);
763 	NFSCL_INCRSEQID(op->nfso_own->nfsow_seqid, nd);
764 	if (nd->nd_repstat == 0)
765 		NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID);
766 	error = nd->nd_repstat;
767 	if (error == NFSERR_STALESTATEID)
768 		nfscl_initiate_recovery(op->nfso_own->nfsow_clp);
769 nfsmout:
770 	mbuf_freem(nd->nd_mrep);
771 	return (error);
772 }
773 
774 /*
775  * V4 Open Confirm RPC.
776  */
777 APPLESTATIC int
778 nfsrpc_openconfirm(vnode_t vp, u_int8_t *nfhp, int fhlen,
779     struct nfsclopen *op, struct ucred *cred, NFSPROC_T *p)
780 {
781 	u_int32_t *tl;
782 	struct nfsrv_descript nfsd, *nd = &nfsd;
783 	struct nfsmount *nmp;
784 	int error;
785 
786 	nmp = VFSTONFS(vnode_mount(vp));
787 	if (NFSHASNFSV4N(nmp))
788 		return (0);		/* No confirmation for NFSv4.1. */
789 	nfscl_reqstart(nd, NFSPROC_OPENCONFIRM, nmp, nfhp, fhlen, NULL, NULL);
790 	NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED + NFSX_STATEID);
791 	*tl++ = op->nfso_stateid.seqid;
792 	*tl++ = op->nfso_stateid.other[0];
793 	*tl++ = op->nfso_stateid.other[1];
794 	*tl++ = op->nfso_stateid.other[2];
795 	*tl = txdr_unsigned(op->nfso_own->nfsow_seqid);
796 	error = nfscl_request(nd, vp, p, cred, NULL);
797 	if (error)
798 		return (error);
799 	NFSCL_INCRSEQID(op->nfso_own->nfsow_seqid, nd);
800 	if (!nd->nd_repstat) {
801 		NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID);
802 		op->nfso_stateid.seqid = *tl++;
803 		op->nfso_stateid.other[0] = *tl++;
804 		op->nfso_stateid.other[1] = *tl++;
805 		op->nfso_stateid.other[2] = *tl;
806 	}
807 	error = nd->nd_repstat;
808 	if (error == NFSERR_STALESTATEID)
809 		nfscl_initiate_recovery(op->nfso_own->nfsow_clp);
810 nfsmout:
811 	mbuf_freem(nd->nd_mrep);
812 	return (error);
813 }
814 
815 /*
816  * Do the setclientid and setclientid confirm RPCs. Called from nfs_statfs()
817  * when a mount has just occurred and when the server replies NFSERR_EXPIRED.
818  */
819 APPLESTATIC int
820 nfsrpc_setclient(struct nfsmount *nmp, struct nfsclclient *clp, int reclaim,
821     struct ucred *cred, NFSPROC_T *p)
822 {
823 	u_int32_t *tl;
824 	struct nfsrv_descript nfsd;
825 	struct nfsrv_descript *nd = &nfsd;
826 	nfsattrbit_t attrbits;
827 	u_int8_t *cp = NULL, *cp2, addr[INET6_ADDRSTRLEN + 9];
828 	u_short port;
829 	int error, isinet6 = 0, callblen;
830 	nfsquad_t confirm;
831 	u_int32_t lease;
832 	static u_int32_t rev = 0;
833 	struct nfsclds *dsp;
834 	struct in6_addr a6;
835 	struct nfsclsession *tsep;
836 
837 	if (nfsboottime.tv_sec == 0)
838 		NFSSETBOOTTIME(nfsboottime);
839 	clp->nfsc_rev = rev++;
840 	if (NFSHASNFSV4N(nmp)) {
841 		/*
842 		 * Either there was no previous session or the
843 		 * previous session has failed, so...
844 		 * do an ExchangeID followed by the CreateSession.
845 		 */
846 		error = nfsrpc_exchangeid(nmp, clp, &nmp->nm_sockreq,
847 		    NFSV4EXCH_USEPNFSMDS | NFSV4EXCH_USENONPNFS, &dsp, cred, p);
848 		NFSCL_DEBUG(1, "aft exch=%d\n", error);
849 		if (error == 0)
850 			error = nfsrpc_createsession(nmp, &dsp->nfsclds_sess,
851 			    &nmp->nm_sockreq,
852 			    dsp->nfsclds_sess.nfsess_sequenceid, 1, cred, p);
853 		if (error == 0) {
854 			NFSLOCKMNT(nmp);
855 			/*
856 			 * The old sessions cannot be safely free'd
857 			 * here, since they may still be used by
858 			 * in-progress RPCs.
859 			 */
860 			tsep = NULL;
861 			if (TAILQ_FIRST(&nmp->nm_sess) != NULL)
862 				tsep = NFSMNT_MDSSESSION(nmp);
863 			TAILQ_INSERT_HEAD(&nmp->nm_sess, dsp,
864 			    nfsclds_list);
865 			/*
866 			 * Wake up RPCs waiting for a slot on the
867 			 * old session. These will then fail with
868 			 * NFSERR_BADSESSION and be retried with the
869 			 * new session by nfsv4_setsequence().
870 			 * Also wakeup() processes waiting for the
871 			 * new session.
872 			 */
873 			if (tsep != NULL)
874 				wakeup(&tsep->nfsess_slots);
875 			wakeup(&nmp->nm_sess);
876 			NFSUNLOCKMNT(nmp);
877 		} else
878 			nfscl_freenfsclds(dsp);
879 		NFSCL_DEBUG(1, "aft createsess=%d\n", error);
880 		if (error == 0 && reclaim == 0) {
881 			error = nfsrpc_reclaimcomplete(nmp, cred, p);
882 			NFSCL_DEBUG(1, "aft reclaimcomp=%d\n", error);
883 			if (error == NFSERR_COMPLETEALREADY ||
884 			    error == NFSERR_NOTSUPP)
885 				/* Ignore this error. */
886 				error = 0;
887 		}
888 		return (error);
889 	}
890 
891 	/*
892 	 * Allocate a single session structure for NFSv4.0, because some of
893 	 * the fields are used by NFSv4.0 although it doesn't do a session.
894 	 */
895 	dsp = malloc(sizeof(struct nfsclds), M_NFSCLDS, M_WAITOK | M_ZERO);
896 	mtx_init(&dsp->nfsclds_mtx, "nfsds", NULL, MTX_DEF);
897 	mtx_init(&dsp->nfsclds_sess.nfsess_mtx, "nfssession", NULL, MTX_DEF);
898 	NFSLOCKMNT(nmp);
899 	TAILQ_INSERT_HEAD(&nmp->nm_sess, dsp, nfsclds_list);
900 	tsep = NFSMNT_MDSSESSION(nmp);
901 	NFSUNLOCKMNT(nmp);
902 
903 	nfscl_reqstart(nd, NFSPROC_SETCLIENTID, nmp, NULL, 0, NULL, NULL);
904 	NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
905 	*tl++ = txdr_unsigned(nfsboottime.tv_sec);
906 	*tl = txdr_unsigned(clp->nfsc_rev);
907 	(void) nfsm_strtom(nd, clp->nfsc_id, clp->nfsc_idlen);
908 
909 	/*
910 	 * set up the callback address
911 	 */
912 	NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
913 	*tl = txdr_unsigned(NFS_CALLBCKPROG);
914 	callblen = strlen(nfsv4_callbackaddr);
915 	if (callblen == 0)
916 		cp = nfscl_getmyip(nmp, &a6, &isinet6);
917 	if (nfscl_enablecallb && nfs_numnfscbd > 0 &&
918 	    (callblen > 0 || cp != NULL)) {
919 		port = htons(nfsv4_cbport);
920 		cp2 = (u_int8_t *)&port;
921 #ifdef INET6
922 		if ((callblen > 0 &&
923 		     strchr(nfsv4_callbackaddr, ':')) || isinet6) {
924 			char ip6buf[INET6_ADDRSTRLEN], *ip6add;
925 
926 			(void) nfsm_strtom(nd, "tcp6", 4);
927 			if (callblen == 0) {
928 				ip6_sprintf(ip6buf, (struct in6_addr *)cp);
929 				ip6add = ip6buf;
930 			} else {
931 				ip6add = nfsv4_callbackaddr;
932 			}
933 			snprintf(addr, INET6_ADDRSTRLEN + 9, "%s.%d.%d",
934 			    ip6add, cp2[0], cp2[1]);
935 		} else
936 #endif
937 		{
938 			(void) nfsm_strtom(nd, "tcp", 3);
939 			if (callblen == 0)
940 				snprintf(addr, INET6_ADDRSTRLEN + 9,
941 				    "%d.%d.%d.%d.%d.%d", cp[0], cp[1],
942 				    cp[2], cp[3], cp2[0], cp2[1]);
943 			else
944 				snprintf(addr, INET6_ADDRSTRLEN + 9,
945 				    "%s.%d.%d", nfsv4_callbackaddr,
946 				    cp2[0], cp2[1]);
947 		}
948 		(void) nfsm_strtom(nd, addr, strlen(addr));
949 	} else {
950 		(void) nfsm_strtom(nd, "tcp", 3);
951 		(void) nfsm_strtom(nd, "0.0.0.0.0.0", 11);
952 	}
953 	NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
954 	*tl = txdr_unsigned(clp->nfsc_cbident);
955 	nd->nd_flag |= ND_USEGSSNAME;
956 	error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
957 		NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
958 	if (error)
959 		return (error);
960 	if (nd->nd_repstat == 0) {
961 	    NFSM_DISSECT(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
962 	    tsep->nfsess_clientid.lval[0] = *tl++;
963 	    tsep->nfsess_clientid.lval[1] = *tl++;
964 	    confirm.lval[0] = *tl++;
965 	    confirm.lval[1] = *tl;
966 	    mbuf_freem(nd->nd_mrep);
967 	    nd->nd_mrep = NULL;
968 
969 	    /*
970 	     * and confirm it.
971 	     */
972 	    nfscl_reqstart(nd, NFSPROC_SETCLIENTIDCFRM, nmp, NULL, 0, NULL,
973 		NULL);
974 	    NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
975 	    *tl++ = tsep->nfsess_clientid.lval[0];
976 	    *tl++ = tsep->nfsess_clientid.lval[1];
977 	    *tl++ = confirm.lval[0];
978 	    *tl = confirm.lval[1];
979 	    nd->nd_flag |= ND_USEGSSNAME;
980 	    error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p,
981 		cred, NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
982 	    if (error)
983 		return (error);
984 	    mbuf_freem(nd->nd_mrep);
985 	    nd->nd_mrep = NULL;
986 	    if (nd->nd_repstat == 0) {
987 		nfscl_reqstart(nd, NFSPROC_GETATTR, nmp, nmp->nm_fh,
988 		    nmp->nm_fhsize, NULL, NULL);
989 		NFSZERO_ATTRBIT(&attrbits);
990 		NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_LEASETIME);
991 		(void) nfsrv_putattrbit(nd, &attrbits);
992 		nd->nd_flag |= ND_USEGSSNAME;
993 		error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p,
994 		    cred, NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
995 		if (error)
996 		    return (error);
997 		if (nd->nd_repstat == 0) {
998 		    error = nfsv4_loadattr(nd, NULL, NULL, NULL, NULL, 0, NULL,
999 			NULL, NULL, NULL, NULL, 0, NULL, &lease, NULL, p, cred);
1000 		    if (error)
1001 			goto nfsmout;
1002 		    clp->nfsc_renew = NFSCL_RENEW(lease);
1003 		    clp->nfsc_expire = NFSD_MONOSEC + clp->nfsc_renew;
1004 		    clp->nfsc_clientidrev++;
1005 		    if (clp->nfsc_clientidrev == 0)
1006 			clp->nfsc_clientidrev++;
1007 		}
1008 	    }
1009 	}
1010 	error = nd->nd_repstat;
1011 nfsmout:
1012 	mbuf_freem(nd->nd_mrep);
1013 	return (error);
1014 }
1015 
1016 /*
1017  * nfs getattr call.
1018  */
1019 APPLESTATIC int
1020 nfsrpc_getattr(vnode_t vp, struct ucred *cred, NFSPROC_T *p,
1021     struct nfsvattr *nap, void *stuff)
1022 {
1023 	struct nfsrv_descript nfsd, *nd = &nfsd;
1024 	int error;
1025 	nfsattrbit_t attrbits;
1026 
1027 	NFSCL_REQSTART(nd, NFSPROC_GETATTR, vp);
1028 	if (nd->nd_flag & ND_NFSV4) {
1029 		NFSGETATTR_ATTRBIT(&attrbits);
1030 		(void) nfsrv_putattrbit(nd, &attrbits);
1031 	}
1032 	error = nfscl_request(nd, vp, p, cred, stuff);
1033 	if (error)
1034 		return (error);
1035 	if (!nd->nd_repstat)
1036 		error = nfsm_loadattr(nd, nap);
1037 	else
1038 		error = nd->nd_repstat;
1039 	mbuf_freem(nd->nd_mrep);
1040 	return (error);
1041 }
1042 
1043 /*
1044  * nfs getattr call with non-vnode arguemnts.
1045  */
1046 APPLESTATIC int
1047 nfsrpc_getattrnovp(struct nfsmount *nmp, u_int8_t *fhp, int fhlen, int syscred,
1048     struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, u_int64_t *xidp,
1049     uint32_t *leasep)
1050 {
1051 	struct nfsrv_descript nfsd, *nd = &nfsd;
1052 	int error, vers = NFS_VER2;
1053 	nfsattrbit_t attrbits;
1054 
1055 	nfscl_reqstart(nd, NFSPROC_GETATTR, nmp, fhp, fhlen, NULL, NULL);
1056 	if (nd->nd_flag & ND_NFSV4) {
1057 		vers = NFS_VER4;
1058 		NFSGETATTR_ATTRBIT(&attrbits);
1059 		NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_LEASETIME);
1060 		(void) nfsrv_putattrbit(nd, &attrbits);
1061 	} else if (nd->nd_flag & ND_NFSV3) {
1062 		vers = NFS_VER3;
1063 	}
1064 	if (syscred)
1065 		nd->nd_flag |= ND_USEGSSNAME;
1066 	error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
1067 	    NFS_PROG, vers, NULL, 1, xidp, NULL);
1068 	if (error)
1069 		return (error);
1070 	if (nd->nd_repstat == 0) {
1071 		if ((nd->nd_flag & ND_NFSV4) != 0)
1072 			error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0,
1073 			    NULL, NULL, NULL, NULL, NULL, 0, NULL, leasep, NULL,
1074 			    NULL, NULL);
1075 		else
1076 			error = nfsm_loadattr(nd, nap);
1077 	} else
1078 		error = nd->nd_repstat;
1079 	mbuf_freem(nd->nd_mrep);
1080 	return (error);
1081 }
1082 
1083 /*
1084  * Do an nfs setattr operation.
1085  */
1086 APPLESTATIC int
1087 nfsrpc_setattr(vnode_t vp, struct vattr *vap, NFSACL_T *aclp,
1088     struct ucred *cred, NFSPROC_T *p, struct nfsvattr *rnap, int *attrflagp,
1089     void *stuff)
1090 {
1091 	int error, expireret = 0, openerr, retrycnt;
1092 	u_int32_t clidrev = 0, mode;
1093 	struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
1094 	struct nfsfh *nfhp;
1095 	nfsv4stateid_t stateid;
1096 	void *lckp;
1097 
1098 	if (nmp->nm_clp != NULL)
1099 		clidrev = nmp->nm_clp->nfsc_clientidrev;
1100 	if (vap != NULL && NFSATTRISSET(u_quad_t, vap, va_size))
1101 		mode = NFSV4OPEN_ACCESSWRITE;
1102 	else
1103 		mode = NFSV4OPEN_ACCESSREAD;
1104 	retrycnt = 0;
1105 	do {
1106 		lckp = NULL;
1107 		openerr = 1;
1108 		if (NFSHASNFSV4(nmp)) {
1109 			nfhp = VTONFS(vp)->n_fhp;
1110 			error = nfscl_getstateid(vp, nfhp->nfh_fh,
1111 			    nfhp->nfh_len, mode, 0, cred, p, &stateid, &lckp);
1112 			if (error && vnode_vtype(vp) == VREG &&
1113 			    (mode == NFSV4OPEN_ACCESSWRITE ||
1114 			     nfstest_openallsetattr)) {
1115 				/*
1116 				 * No Open stateid, so try and open the file
1117 				 * now.
1118 				 */
1119 				if (mode == NFSV4OPEN_ACCESSWRITE)
1120 					openerr = nfsrpc_open(vp, FWRITE, cred,
1121 					    p);
1122 				else
1123 					openerr = nfsrpc_open(vp, FREAD, cred,
1124 					    p);
1125 				if (!openerr)
1126 					(void) nfscl_getstateid(vp,
1127 					    nfhp->nfh_fh, nfhp->nfh_len,
1128 					    mode, 0, cred, p, &stateid, &lckp);
1129 			}
1130 		}
1131 		if (vap != NULL)
1132 			error = nfsrpc_setattrrpc(vp, vap, &stateid, cred, p,
1133 			    rnap, attrflagp, stuff);
1134 		else
1135 			error = nfsrpc_setaclrpc(vp, cred, p, aclp, &stateid,
1136 			    stuff);
1137 		if (error == NFSERR_OPENMODE && mode == NFSV4OPEN_ACCESSREAD) {
1138 			NFSLOCKMNT(nmp);
1139 			nmp->nm_state |= NFSSTA_OPENMODE;
1140 			NFSUNLOCKMNT(nmp);
1141 		}
1142 		if (error == NFSERR_STALESTATEID)
1143 			nfscl_initiate_recovery(nmp->nm_clp);
1144 		if (lckp != NULL)
1145 			nfscl_lockderef(lckp);
1146 		if (!openerr)
1147 			(void) nfsrpc_close(vp, 0, p);
1148 		if (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
1149 		    error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
1150 		    error == NFSERR_OLDSTATEID || error == NFSERR_BADSESSION) {
1151 			(void) nfs_catnap(PZERO, error, "nfs_setattr");
1152 		} else if ((error == NFSERR_EXPIRED ||
1153 		    error == NFSERR_BADSTATEID) && clidrev != 0) {
1154 			expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
1155 		}
1156 		retrycnt++;
1157 	} while (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
1158 	    error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
1159 	    error == NFSERR_BADSESSION ||
1160 	    (error == NFSERR_OLDSTATEID && retrycnt < 20) ||
1161 	    ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
1162 	     expireret == 0 && clidrev != 0 && retrycnt < 4) ||
1163 	    (error == NFSERR_OPENMODE && mode == NFSV4OPEN_ACCESSREAD &&
1164 	     retrycnt < 4));
1165 	if (error && retrycnt >= 4)
1166 		error = EIO;
1167 	return (error);
1168 }
1169 
1170 static int
1171 nfsrpc_setattrrpc(vnode_t vp, struct vattr *vap,
1172     nfsv4stateid_t *stateidp, struct ucred *cred, NFSPROC_T *p,
1173     struct nfsvattr *rnap, int *attrflagp, void *stuff)
1174 {
1175 	u_int32_t *tl;
1176 	struct nfsrv_descript nfsd, *nd = &nfsd;
1177 	int error;
1178 	nfsattrbit_t attrbits;
1179 
1180 	*attrflagp = 0;
1181 	NFSCL_REQSTART(nd, NFSPROC_SETATTR, vp);
1182 	if (nd->nd_flag & ND_NFSV4)
1183 		nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID);
1184 	vap->va_type = vnode_vtype(vp);
1185 	nfscl_fillsattr(nd, vap, vp, NFSSATTR_FULL, 0);
1186 	if (nd->nd_flag & ND_NFSV3) {
1187 		NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1188 		*tl = newnfs_false;
1189 	} else if (nd->nd_flag & ND_NFSV4) {
1190 		NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1191 		*tl = txdr_unsigned(NFSV4OP_GETATTR);
1192 		NFSGETATTR_ATTRBIT(&attrbits);
1193 		(void) nfsrv_putattrbit(nd, &attrbits);
1194 	}
1195 	error = nfscl_request(nd, vp, p, cred, stuff);
1196 	if (error)
1197 		return (error);
1198 	if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4))
1199 		error = nfscl_wcc_data(nd, vp, rnap, attrflagp, NULL, stuff);
1200 	if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) == ND_NFSV4 && !error)
1201 		error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
1202 	if (!(nd->nd_flag & ND_NFSV3) && !nd->nd_repstat && !error)
1203 		error = nfscl_postop_attr(nd, rnap, attrflagp, stuff);
1204 	mbuf_freem(nd->nd_mrep);
1205 	if (nd->nd_repstat && !error)
1206 		error = nd->nd_repstat;
1207 	return (error);
1208 }
1209 
1210 /*
1211  * nfs lookup rpc
1212  */
1213 APPLESTATIC int
1214 nfsrpc_lookup(vnode_t dvp, char *name, int len, struct ucred *cred,
1215     NFSPROC_T *p, struct nfsvattr *dnap, struct nfsvattr *nap,
1216     struct nfsfh **nfhpp, int *attrflagp, int *dattrflagp, void *stuff)
1217 {
1218 	u_int32_t *tl;
1219 	struct nfsrv_descript nfsd, *nd = &nfsd;
1220 	struct nfsmount *nmp;
1221 	struct nfsnode *np;
1222 	struct nfsfh *nfhp;
1223 	nfsattrbit_t attrbits;
1224 	int error = 0, lookupp = 0;
1225 
1226 	*attrflagp = 0;
1227 	*dattrflagp = 0;
1228 	if (vnode_vtype(dvp) != VDIR)
1229 		return (ENOTDIR);
1230 	nmp = VFSTONFS(vnode_mount(dvp));
1231 	if (len > NFS_MAXNAMLEN)
1232 		return (ENAMETOOLONG);
1233 	if (NFSHASNFSV4(nmp) && len == 1 &&
1234 		name[0] == '.') {
1235 		/*
1236 		 * Just return the current dir's fh.
1237 		 */
1238 		np = VTONFS(dvp);
1239 		MALLOC(nfhp, struct nfsfh *, sizeof (struct nfsfh) +
1240 			np->n_fhp->nfh_len, M_NFSFH, M_WAITOK);
1241 		nfhp->nfh_len = np->n_fhp->nfh_len;
1242 		NFSBCOPY(np->n_fhp->nfh_fh, nfhp->nfh_fh, nfhp->nfh_len);
1243 		*nfhpp = nfhp;
1244 		return (0);
1245 	}
1246 	if (NFSHASNFSV4(nmp) && len == 2 &&
1247 		name[0] == '.' && name[1] == '.') {
1248 		lookupp = 1;
1249 		NFSCL_REQSTART(nd, NFSPROC_LOOKUPP, dvp);
1250 	} else {
1251 		NFSCL_REQSTART(nd, NFSPROC_LOOKUP, dvp);
1252 		(void) nfsm_strtom(nd, name, len);
1253 	}
1254 	if (nd->nd_flag & ND_NFSV4) {
1255 		NFSGETATTR_ATTRBIT(&attrbits);
1256 		NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1257 		*tl++ = txdr_unsigned(NFSV4OP_GETFH);
1258 		*tl = txdr_unsigned(NFSV4OP_GETATTR);
1259 		(void) nfsrv_putattrbit(nd, &attrbits);
1260 	}
1261 	error = nfscl_request(nd, dvp, p, cred, stuff);
1262 	if (error)
1263 		return (error);
1264 	if (nd->nd_repstat) {
1265 		/*
1266 		 * When an NFSv4 Lookupp returns ENOENT, it means that
1267 		 * the lookup is at the root of an fs, so return this dir.
1268 		 */
1269 		if (nd->nd_repstat == NFSERR_NOENT && lookupp) {
1270 		    np = VTONFS(dvp);
1271 		    MALLOC(nfhp, struct nfsfh *, sizeof (struct nfsfh) +
1272 			np->n_fhp->nfh_len, M_NFSFH, M_WAITOK);
1273 		    nfhp->nfh_len = np->n_fhp->nfh_len;
1274 		    NFSBCOPY(np->n_fhp->nfh_fh, nfhp->nfh_fh, nfhp->nfh_len);
1275 		    *nfhpp = nfhp;
1276 		    mbuf_freem(nd->nd_mrep);
1277 		    return (0);
1278 		}
1279 		if (nd->nd_flag & ND_NFSV3)
1280 		    error = nfscl_postop_attr(nd, dnap, dattrflagp, stuff);
1281 		else if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) ==
1282 		    ND_NFSV4) {
1283 			/* Load the directory attributes. */
1284 			error = nfsm_loadattr(nd, dnap);
1285 			if (error == 0)
1286 				*dattrflagp = 1;
1287 		}
1288 		goto nfsmout;
1289 	}
1290 	if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) == ND_NFSV4) {
1291 		/* Load the directory attributes. */
1292 		error = nfsm_loadattr(nd, dnap);
1293 		if (error != 0)
1294 			goto nfsmout;
1295 		*dattrflagp = 1;
1296 		/* Skip over the Lookup and GetFH operation status values. */
1297 		NFSM_DISSECT(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1298 	}
1299 	error = nfsm_getfh(nd, nfhpp);
1300 	if (error)
1301 		goto nfsmout;
1302 
1303 	error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
1304 	if ((nd->nd_flag & ND_NFSV3) && !error)
1305 		error = nfscl_postop_attr(nd, dnap, dattrflagp, stuff);
1306 nfsmout:
1307 	mbuf_freem(nd->nd_mrep);
1308 	if (!error && nd->nd_repstat)
1309 		error = nd->nd_repstat;
1310 	return (error);
1311 }
1312 
1313 /*
1314  * Do a readlink rpc.
1315  */
1316 APPLESTATIC int
1317 nfsrpc_readlink(vnode_t vp, struct uio *uiop, struct ucred *cred,
1318     NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, void *stuff)
1319 {
1320 	u_int32_t *tl;
1321 	struct nfsrv_descript nfsd, *nd = &nfsd;
1322 	struct nfsnode *np = VTONFS(vp);
1323 	nfsattrbit_t attrbits;
1324 	int error, len, cangetattr = 1;
1325 
1326 	*attrflagp = 0;
1327 	NFSCL_REQSTART(nd, NFSPROC_READLINK, vp);
1328 	if (nd->nd_flag & ND_NFSV4) {
1329 		/*
1330 		 * And do a Getattr op.
1331 		 */
1332 		NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1333 		*tl = txdr_unsigned(NFSV4OP_GETATTR);
1334 		NFSGETATTR_ATTRBIT(&attrbits);
1335 		(void) nfsrv_putattrbit(nd, &attrbits);
1336 	}
1337 	error = nfscl_request(nd, vp, p, cred, stuff);
1338 	if (error)
1339 		return (error);
1340 	if (nd->nd_flag & ND_NFSV3)
1341 		error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
1342 	if (!nd->nd_repstat && !error) {
1343 		NFSM_STRSIZ(len, NFS_MAXPATHLEN);
1344 		/*
1345 		 * This seems weird to me, but must have been added to
1346 		 * FreeBSD for some reason. The only thing I can think of
1347 		 * is that there was/is some server that replies with
1348 		 * more link data than it should?
1349 		 */
1350 		if (len == NFS_MAXPATHLEN) {
1351 			NFSLOCKNODE(np);
1352 			if (np->n_size > 0 && np->n_size < NFS_MAXPATHLEN) {
1353 				len = np->n_size;
1354 				cangetattr = 0;
1355 			}
1356 			NFSUNLOCKNODE(np);
1357 		}
1358 		error = nfsm_mbufuio(nd, uiop, len);
1359 		if ((nd->nd_flag & ND_NFSV4) && !error && cangetattr)
1360 			error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
1361 	}
1362 	if (nd->nd_repstat && !error)
1363 		error = nd->nd_repstat;
1364 nfsmout:
1365 	mbuf_freem(nd->nd_mrep);
1366 	return (error);
1367 }
1368 
1369 /*
1370  * Read operation.
1371  */
1372 APPLESTATIC int
1373 nfsrpc_read(vnode_t vp, struct uio *uiop, struct ucred *cred,
1374     NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, void *stuff)
1375 {
1376 	int error, expireret = 0, retrycnt;
1377 	u_int32_t clidrev = 0;
1378 	struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
1379 	struct nfsnode *np = VTONFS(vp);
1380 	struct ucred *newcred;
1381 	struct nfsfh *nfhp = NULL;
1382 	nfsv4stateid_t stateid;
1383 	void *lckp;
1384 
1385 	if (nmp->nm_clp != NULL)
1386 		clidrev = nmp->nm_clp->nfsc_clientidrev;
1387 	newcred = cred;
1388 	if (NFSHASNFSV4(nmp)) {
1389 		nfhp = np->n_fhp;
1390 		newcred = NFSNEWCRED(cred);
1391 	}
1392 	retrycnt = 0;
1393 	do {
1394 		lckp = NULL;
1395 		if (NFSHASNFSV4(nmp))
1396 			(void)nfscl_getstateid(vp, nfhp->nfh_fh, nfhp->nfh_len,
1397 			    NFSV4OPEN_ACCESSREAD, 0, newcred, p, &stateid,
1398 			    &lckp);
1399 		error = nfsrpc_readrpc(vp, uiop, newcred, &stateid, p, nap,
1400 		    attrflagp, stuff);
1401 		if (error == NFSERR_OPENMODE) {
1402 			NFSLOCKMNT(nmp);
1403 			nmp->nm_state |= NFSSTA_OPENMODE;
1404 			NFSUNLOCKMNT(nmp);
1405 		}
1406 		if (error == NFSERR_STALESTATEID)
1407 			nfscl_initiate_recovery(nmp->nm_clp);
1408 		if (lckp != NULL)
1409 			nfscl_lockderef(lckp);
1410 		if (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
1411 		    error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
1412 		    error == NFSERR_OLDSTATEID || error == NFSERR_BADSESSION) {
1413 			(void) nfs_catnap(PZERO, error, "nfs_read");
1414 		} else if ((error == NFSERR_EXPIRED ||
1415 		    error == NFSERR_BADSTATEID) && clidrev != 0) {
1416 			expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
1417 		}
1418 		retrycnt++;
1419 	} while (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
1420 	    error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
1421 	    error == NFSERR_BADSESSION ||
1422 	    (error == NFSERR_OLDSTATEID && retrycnt < 20) ||
1423 	    ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
1424 	     expireret == 0 && clidrev != 0 && retrycnt < 4) ||
1425 	    (error == NFSERR_OPENMODE && retrycnt < 4));
1426 	if (error && retrycnt >= 4)
1427 		error = EIO;
1428 	if (NFSHASNFSV4(nmp))
1429 		NFSFREECRED(newcred);
1430 	return (error);
1431 }
1432 
1433 /*
1434  * The actual read RPC.
1435  */
1436 static int
1437 nfsrpc_readrpc(vnode_t vp, struct uio *uiop, struct ucred *cred,
1438     nfsv4stateid_t *stateidp, NFSPROC_T *p, struct nfsvattr *nap,
1439     int *attrflagp, void *stuff)
1440 {
1441 	u_int32_t *tl;
1442 	int error = 0, len, retlen, tsiz, eof = 0;
1443 	struct nfsrv_descript nfsd;
1444 	struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
1445 	struct nfsrv_descript *nd = &nfsd;
1446 	int rsize;
1447 	off_t tmp_off;
1448 
1449 	*attrflagp = 0;
1450 	tsiz = uio_uio_resid(uiop);
1451 	tmp_off = uiop->uio_offset + tsiz;
1452 	NFSLOCKMNT(nmp);
1453 	if (tmp_off > nmp->nm_maxfilesize || tmp_off < uiop->uio_offset) {
1454 		NFSUNLOCKMNT(nmp);
1455 		return (EFBIG);
1456 	}
1457 	rsize = nmp->nm_rsize;
1458 	NFSUNLOCKMNT(nmp);
1459 	nd->nd_mrep = NULL;
1460 	while (tsiz > 0) {
1461 		*attrflagp = 0;
1462 		len = (tsiz > rsize) ? rsize : tsiz;
1463 		NFSCL_REQSTART(nd, NFSPROC_READ, vp);
1464 		if (nd->nd_flag & ND_NFSV4)
1465 			nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID);
1466 		NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED * 3);
1467 		if (nd->nd_flag & ND_NFSV2) {
1468 			*tl++ = txdr_unsigned(uiop->uio_offset);
1469 			*tl++ = txdr_unsigned(len);
1470 			*tl = 0;
1471 		} else {
1472 			txdr_hyper(uiop->uio_offset, tl);
1473 			*(tl + 2) = txdr_unsigned(len);
1474 		}
1475 		/*
1476 		 * Since I can't do a Getattr for NFSv4 for Write, there
1477 		 * doesn't seem any point in doing one here, either.
1478 		 * (See the comment in nfsrpc_writerpc() for more info.)
1479 		 */
1480 		error = nfscl_request(nd, vp, p, cred, stuff);
1481 		if (error)
1482 			return (error);
1483 		if (nd->nd_flag & ND_NFSV3) {
1484 			error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
1485 		} else if (!nd->nd_repstat && (nd->nd_flag & ND_NFSV2)) {
1486 			error = nfsm_loadattr(nd, nap);
1487 			if (!error)
1488 				*attrflagp = 1;
1489 		}
1490 		if (nd->nd_repstat || error) {
1491 			if (!error)
1492 				error = nd->nd_repstat;
1493 			goto nfsmout;
1494 		}
1495 		if (nd->nd_flag & ND_NFSV3) {
1496 			NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1497 			eof = fxdr_unsigned(int, *(tl + 1));
1498 		} else if (nd->nd_flag & ND_NFSV4) {
1499 			NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
1500 			eof = fxdr_unsigned(int, *tl);
1501 		}
1502 		NFSM_STRSIZ(retlen, len);
1503 		error = nfsm_mbufuio(nd, uiop, retlen);
1504 		if (error)
1505 			goto nfsmout;
1506 		mbuf_freem(nd->nd_mrep);
1507 		nd->nd_mrep = NULL;
1508 		tsiz -= retlen;
1509 		if (!(nd->nd_flag & ND_NFSV2)) {
1510 			if (eof || retlen == 0)
1511 				tsiz = 0;
1512 		} else if (retlen < len)
1513 			tsiz = 0;
1514 	}
1515 	return (0);
1516 nfsmout:
1517 	if (nd->nd_mrep != NULL)
1518 		mbuf_freem(nd->nd_mrep);
1519 	return (error);
1520 }
1521 
1522 /*
1523  * nfs write operation
1524  * When called_from_strategy != 0, it should return EIO for an error that
1525  * indicates recovery is in progress, so that the buffer will be left
1526  * dirty and be written back to the server later. If it loops around,
1527  * the recovery thread could get stuck waiting for the buffer and recovery
1528  * will then deadlock.
1529  */
1530 APPLESTATIC int
1531 nfsrpc_write(vnode_t vp, struct uio *uiop, int *iomode, int *must_commit,
1532     struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp,
1533     void *stuff, int called_from_strategy)
1534 {
1535 	int error, expireret = 0, retrycnt, nostateid;
1536 	u_int32_t clidrev = 0;
1537 	struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
1538 	struct nfsnode *np = VTONFS(vp);
1539 	struct ucred *newcred;
1540 	struct nfsfh *nfhp = NULL;
1541 	nfsv4stateid_t stateid;
1542 	void *lckp;
1543 
1544 	*must_commit = 0;
1545 	if (nmp->nm_clp != NULL)
1546 		clidrev = nmp->nm_clp->nfsc_clientidrev;
1547 	newcred = cred;
1548 	if (NFSHASNFSV4(nmp)) {
1549 		newcred = NFSNEWCRED(cred);
1550 		nfhp = np->n_fhp;
1551 	}
1552 	retrycnt = 0;
1553 	do {
1554 		lckp = NULL;
1555 		nostateid = 0;
1556 		if (NFSHASNFSV4(nmp)) {
1557 			(void)nfscl_getstateid(vp, nfhp->nfh_fh, nfhp->nfh_len,
1558 			    NFSV4OPEN_ACCESSWRITE, 0, newcred, p, &stateid,
1559 			    &lckp);
1560 			if (stateid.other[0] == 0 && stateid.other[1] == 0 &&
1561 			    stateid.other[2] == 0) {
1562 				nostateid = 1;
1563 				NFSCL_DEBUG(1, "stateid0 in write\n");
1564 			}
1565 		}
1566 
1567 		/*
1568 		 * If there is no stateid for NFSv4, it means this is an
1569 		 * extraneous write after close. Basically a poorly
1570 		 * implemented buffer cache. Just don't do the write.
1571 		 */
1572 		if (nostateid)
1573 			error = 0;
1574 		else
1575 			error = nfsrpc_writerpc(vp, uiop, iomode, must_commit,
1576 			    newcred, &stateid, p, nap, attrflagp, stuff);
1577 		if (error == NFSERR_STALESTATEID)
1578 			nfscl_initiate_recovery(nmp->nm_clp);
1579 		if (lckp != NULL)
1580 			nfscl_lockderef(lckp);
1581 		if (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
1582 		    error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
1583 		    error == NFSERR_OLDSTATEID || error == NFSERR_BADSESSION) {
1584 			(void) nfs_catnap(PZERO, error, "nfs_write");
1585 		} else if ((error == NFSERR_EXPIRED ||
1586 		    error == NFSERR_BADSTATEID) && clidrev != 0) {
1587 			expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
1588 		}
1589 		retrycnt++;
1590 	} while (error == NFSERR_GRACE || error == NFSERR_DELAY ||
1591 	    ((error == NFSERR_STALESTATEID || error == NFSERR_BADSESSION ||
1592 	      error == NFSERR_STALEDONTRECOVER) && called_from_strategy == 0) ||
1593 	    (error == NFSERR_OLDSTATEID && retrycnt < 20) ||
1594 	    ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
1595 	     expireret == 0 && clidrev != 0 && retrycnt < 4));
1596 	if (error != 0 && (retrycnt >= 4 ||
1597 	    ((error == NFSERR_STALESTATEID || error == NFSERR_BADSESSION ||
1598 	      error == NFSERR_STALEDONTRECOVER) && called_from_strategy != 0)))
1599 		error = EIO;
1600 	if (NFSHASNFSV4(nmp))
1601 		NFSFREECRED(newcred);
1602 	return (error);
1603 }
1604 
1605 /*
1606  * The actual write RPC.
1607  */
1608 static int
1609 nfsrpc_writerpc(vnode_t vp, struct uio *uiop, int *iomode,
1610     int *must_commit, struct ucred *cred, nfsv4stateid_t *stateidp,
1611     NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, void *stuff)
1612 {
1613 	u_int32_t *tl;
1614 	struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
1615 	struct nfsnode *np = VTONFS(vp);
1616 	int error = 0, len, tsiz, rlen, commit, committed = NFSWRITE_FILESYNC;
1617 	int wccflag = 0, wsize;
1618 	int32_t backup;
1619 	struct nfsrv_descript nfsd;
1620 	struct nfsrv_descript *nd = &nfsd;
1621 	nfsattrbit_t attrbits;
1622 	off_t tmp_off;
1623 
1624 	KASSERT(uiop->uio_iovcnt == 1, ("nfs: writerpc iovcnt > 1"));
1625 	*attrflagp = 0;
1626 	tsiz = uio_uio_resid(uiop);
1627 	tmp_off = uiop->uio_offset + tsiz;
1628 	NFSLOCKMNT(nmp);
1629 	if (tmp_off > nmp->nm_maxfilesize || tmp_off < uiop->uio_offset) {
1630 		NFSUNLOCKMNT(nmp);
1631 		return (EFBIG);
1632 	}
1633 	wsize = nmp->nm_wsize;
1634 	NFSUNLOCKMNT(nmp);
1635 	nd->nd_mrep = NULL;	/* NFSv2 sometimes does a write with */
1636 	nd->nd_repstat = 0;	/* uio_resid == 0, so the while is not done */
1637 	while (tsiz > 0) {
1638 		*attrflagp = 0;
1639 		len = (tsiz > wsize) ? wsize : tsiz;
1640 		NFSCL_REQSTART(nd, NFSPROC_WRITE, vp);
1641 		if (nd->nd_flag & ND_NFSV4) {
1642 			nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID);
1643 			NFSM_BUILD(tl, u_int32_t *, NFSX_HYPER+2*NFSX_UNSIGNED);
1644 			txdr_hyper(uiop->uio_offset, tl);
1645 			tl += 2;
1646 			*tl++ = txdr_unsigned(*iomode);
1647 			*tl = txdr_unsigned(len);
1648 		} else if (nd->nd_flag & ND_NFSV3) {
1649 			NFSM_BUILD(tl, u_int32_t *, NFSX_HYPER+3*NFSX_UNSIGNED);
1650 			txdr_hyper(uiop->uio_offset, tl);
1651 			tl += 2;
1652 			*tl++ = txdr_unsigned(len);
1653 			*tl++ = txdr_unsigned(*iomode);
1654 			*tl = txdr_unsigned(len);
1655 		} else {
1656 			u_int32_t x;
1657 
1658 			NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1659 			/*
1660 			 * Not sure why someone changed this, since the
1661 			 * RFC clearly states that "beginoffset" and
1662 			 * "totalcount" are ignored, but it wouldn't
1663 			 * surprise me if there's a busted server out there.
1664 			 */
1665 			/* Set both "begin" and "current" to non-garbage. */
1666 			x = txdr_unsigned((u_int32_t)uiop->uio_offset);
1667 			*tl++ = x;      /* "begin offset" */
1668 			*tl++ = x;      /* "current offset" */
1669 			x = txdr_unsigned(len);
1670 			*tl++ = x;      /* total to this offset */
1671 			*tl = x;        /* size of this write */
1672 
1673 		}
1674 		nfsm_uiombuf(nd, uiop, len);
1675 		/*
1676 		 * Although it is tempting to do a normal Getattr Op in the
1677 		 * NFSv4 compound, the result can be a nearly hung client
1678 		 * system if the Getattr asks for Owner and/or OwnerGroup.
1679 		 * It occurs when the client can't map either the Owner or
1680 		 * Owner_group name in the Getattr reply to a uid/gid. When
1681 		 * there is a cache miss, the kernel does an upcall to the
1682 		 * nfsuserd. Then, it can try and read the local /etc/passwd
1683 		 * or /etc/group file. It can then block in getnewbuf(),
1684 		 * waiting for dirty writes to be pushed to the NFS server.
1685 		 * The only reason this doesn't result in a complete
1686 		 * deadlock, is that the upcall times out and allows
1687 		 * the write to complete. However, progress is so slow
1688 		 * that it might just as well be deadlocked.
1689 		 * As such, we get the rest of the attributes, but not
1690 		 * Owner or Owner_group.
1691 		 * nb: nfscl_loadattrcache() needs to be told that these
1692 		 *     partial attributes from a write rpc are being
1693 		 *     passed in, via a argument flag.
1694 		 */
1695 		if (nd->nd_flag & ND_NFSV4) {
1696 			NFSWRITEGETATTR_ATTRBIT(&attrbits);
1697 			NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1698 			*tl = txdr_unsigned(NFSV4OP_GETATTR);
1699 			(void) nfsrv_putattrbit(nd, &attrbits);
1700 		}
1701 		error = nfscl_request(nd, vp, p, cred, stuff);
1702 		if (error)
1703 			return (error);
1704 		if (nd->nd_repstat) {
1705 			/*
1706 			 * In case the rpc gets retried, roll
1707 			 * the uio fileds changed by nfsm_uiombuf()
1708 			 * back.
1709 			 */
1710 			uiop->uio_offset -= len;
1711 			uio_uio_resid_add(uiop, len);
1712 			uio_iov_base_add(uiop, -len);
1713 			uio_iov_len_add(uiop, len);
1714 		}
1715 		if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) {
1716 			error = nfscl_wcc_data(nd, vp, nap, attrflagp,
1717 			    &wccflag, stuff);
1718 			if (error)
1719 				goto nfsmout;
1720 		}
1721 		if (!nd->nd_repstat) {
1722 			if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) {
1723 				NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED
1724 					+ NFSX_VERF);
1725 				rlen = fxdr_unsigned(int, *tl++);
1726 				if (rlen == 0) {
1727 					error = NFSERR_IO;
1728 					goto nfsmout;
1729 				} else if (rlen < len) {
1730 					backup = len - rlen;
1731 					uio_iov_base_add(uiop, -(backup));
1732 					uio_iov_len_add(uiop, backup);
1733 					uiop->uio_offset -= backup;
1734 					uio_uio_resid_add(uiop, backup);
1735 					len = rlen;
1736 				}
1737 				commit = fxdr_unsigned(int, *tl++);
1738 
1739 				/*
1740 				 * Return the lowest commitment level
1741 				 * obtained by any of the RPCs.
1742 				 */
1743 				if (committed == NFSWRITE_FILESYNC)
1744 					committed = commit;
1745 				else if (committed == NFSWRITE_DATASYNC &&
1746 					commit == NFSWRITE_UNSTABLE)
1747 					committed = commit;
1748 				NFSLOCKMNT(nmp);
1749 				if (!NFSHASWRITEVERF(nmp)) {
1750 					NFSBCOPY((caddr_t)tl,
1751 					    (caddr_t)&nmp->nm_verf[0],
1752 					    NFSX_VERF);
1753 					NFSSETWRITEVERF(nmp);
1754 	    			} else if (NFSBCMP(tl, nmp->nm_verf,
1755 				    NFSX_VERF)) {
1756 					*must_commit = 1;
1757 					NFSBCOPY(tl, nmp->nm_verf, NFSX_VERF);
1758 				}
1759 				NFSUNLOCKMNT(nmp);
1760 			}
1761 			if (nd->nd_flag & ND_NFSV4)
1762 				NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1763 			if (nd->nd_flag & (ND_NFSV2 | ND_NFSV4)) {
1764 				error = nfsm_loadattr(nd, nap);
1765 				if (!error)
1766 					*attrflagp = NFS_LATTR_NOSHRINK;
1767 			}
1768 		} else {
1769 			error = nd->nd_repstat;
1770 		}
1771 		if (error)
1772 			goto nfsmout;
1773 		NFSWRITERPC_SETTIME(wccflag, np, nap, (nd->nd_flag & ND_NFSV4));
1774 		mbuf_freem(nd->nd_mrep);
1775 		nd->nd_mrep = NULL;
1776 		tsiz -= len;
1777 	}
1778 nfsmout:
1779 	if (nd->nd_mrep != NULL)
1780 		mbuf_freem(nd->nd_mrep);
1781 	*iomode = committed;
1782 	if (nd->nd_repstat && !error)
1783 		error = nd->nd_repstat;
1784 	return (error);
1785 }
1786 
1787 /*
1788  * nfs mknod rpc
1789  * For NFS v2 this is a kludge. Use a create rpc but with the IFMT bits of the
1790  * mode set to specify the file type and the size field for rdev.
1791  */
1792 APPLESTATIC int
1793 nfsrpc_mknod(vnode_t dvp, char *name, int namelen, struct vattr *vap,
1794     u_int32_t rdev, enum vtype vtyp, struct ucred *cred, NFSPROC_T *p,
1795     struct nfsvattr *dnap, struct nfsvattr *nnap, struct nfsfh **nfhpp,
1796     int *attrflagp, int *dattrflagp, void *dstuff)
1797 {
1798 	u_int32_t *tl;
1799 	int error = 0;
1800 	struct nfsrv_descript nfsd, *nd = &nfsd;
1801 	nfsattrbit_t attrbits;
1802 
1803 	*nfhpp = NULL;
1804 	*attrflagp = 0;
1805 	*dattrflagp = 0;
1806 	if (namelen > NFS_MAXNAMLEN)
1807 		return (ENAMETOOLONG);
1808 	NFSCL_REQSTART(nd, NFSPROC_MKNOD, dvp);
1809 	if (nd->nd_flag & ND_NFSV4) {
1810 		if (vtyp == VBLK || vtyp == VCHR) {
1811 			NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
1812 			*tl++ = vtonfsv34_type(vtyp);
1813 			*tl++ = txdr_unsigned(NFSMAJOR(rdev));
1814 			*tl = txdr_unsigned(NFSMINOR(rdev));
1815 		} else {
1816 			NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1817 			*tl = vtonfsv34_type(vtyp);
1818 		}
1819 	}
1820 	(void) nfsm_strtom(nd, name, namelen);
1821 	if (nd->nd_flag & ND_NFSV3) {
1822 		NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1823 		*tl = vtonfsv34_type(vtyp);
1824 	}
1825 	if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4))
1826 		nfscl_fillsattr(nd, vap, dvp, 0, 0);
1827 	if ((nd->nd_flag & ND_NFSV3) &&
1828 	    (vtyp == VCHR || vtyp == VBLK)) {
1829 		NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1830 		*tl++ = txdr_unsigned(NFSMAJOR(rdev));
1831 		*tl = txdr_unsigned(NFSMINOR(rdev));
1832 	}
1833 	if (nd->nd_flag & ND_NFSV4) {
1834 		NFSGETATTR_ATTRBIT(&attrbits);
1835 		NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1836 		*tl++ = txdr_unsigned(NFSV4OP_GETFH);
1837 		*tl = txdr_unsigned(NFSV4OP_GETATTR);
1838 		(void) nfsrv_putattrbit(nd, &attrbits);
1839 	}
1840 	if (nd->nd_flag & ND_NFSV2)
1841 		nfscl_fillsattr(nd, vap, dvp, NFSSATTR_SIZERDEV, rdev);
1842 	error = nfscl_request(nd, dvp, p, cred, dstuff);
1843 	if (error)
1844 		return (error);
1845 	if (nd->nd_flag & ND_NFSV4)
1846 		error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
1847 	if (!nd->nd_repstat) {
1848 		if (nd->nd_flag & ND_NFSV4) {
1849 			NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
1850 			error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
1851 			if (error)
1852 				goto nfsmout;
1853 		}
1854 		error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp);
1855 		if (error)
1856 			goto nfsmout;
1857 	}
1858 	if (nd->nd_flag & ND_NFSV3)
1859 		error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
1860 	if (!error && nd->nd_repstat)
1861 		error = nd->nd_repstat;
1862 nfsmout:
1863 	mbuf_freem(nd->nd_mrep);
1864 	return (error);
1865 }
1866 
1867 /*
1868  * nfs file create call
1869  * Mostly just call the approriate routine. (I separated out v4, so that
1870  * error recovery wouldn't be as difficult.)
1871  */
1872 APPLESTATIC int
1873 nfsrpc_create(vnode_t dvp, char *name, int namelen, struct vattr *vap,
1874     nfsquad_t cverf, int fmode, struct ucred *cred, NFSPROC_T *p,
1875     struct nfsvattr *dnap, struct nfsvattr *nnap, struct nfsfh **nfhpp,
1876     int *attrflagp, int *dattrflagp, void *dstuff)
1877 {
1878 	int error = 0, newone, expireret = 0, retrycnt, unlocked;
1879 	struct nfsclowner *owp;
1880 	struct nfscldeleg *dp;
1881 	struct nfsmount *nmp = VFSTONFS(vnode_mount(dvp));
1882 	u_int32_t clidrev;
1883 
1884 	if (NFSHASNFSV4(nmp)) {
1885 	    retrycnt = 0;
1886 	    do {
1887 		dp = NULL;
1888 		error = nfscl_open(dvp, NULL, 0, (NFSV4OPEN_ACCESSWRITE |
1889 		    NFSV4OPEN_ACCESSREAD), 0, cred, p, &owp, NULL, &newone,
1890 		    NULL, 1);
1891 		if (error)
1892 			return (error);
1893 		if (nmp->nm_clp != NULL)
1894 			clidrev = nmp->nm_clp->nfsc_clientidrev;
1895 		else
1896 			clidrev = 0;
1897 		error = nfsrpc_createv4(dvp, name, namelen, vap, cverf, fmode,
1898 		  owp, &dp, cred, p, dnap, nnap, nfhpp, attrflagp, dattrflagp,
1899 		  dstuff, &unlocked);
1900 		/*
1901 		 * There is no need to invalidate cached attributes here,
1902 		 * since new post-delegation issue attributes are always
1903 		 * returned by nfsrpc_createv4() and these will update the
1904 		 * attribute cache.
1905 		 */
1906 		if (dp != NULL)
1907 			(void) nfscl_deleg(nmp->nm_mountp, owp->nfsow_clp,
1908 			    (*nfhpp)->nfh_fh, (*nfhpp)->nfh_len, cred, p, &dp);
1909 		nfscl_ownerrelease(nmp, owp, error, newone, unlocked);
1910 		if (error == NFSERR_GRACE || error == NFSERR_STALECLIENTID ||
1911 		    error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
1912 		    error == NFSERR_BADSESSION) {
1913 			(void) nfs_catnap(PZERO, error, "nfs_open");
1914 		} else if ((error == NFSERR_EXPIRED ||
1915 		    error == NFSERR_BADSTATEID) && clidrev != 0) {
1916 			expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
1917 			retrycnt++;
1918 		}
1919 	    } while (error == NFSERR_GRACE || error == NFSERR_STALECLIENTID ||
1920 		error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
1921 		error == NFSERR_BADSESSION ||
1922 		((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
1923 		 expireret == 0 && clidrev != 0 && retrycnt < 4));
1924 	    if (error && retrycnt >= 4)
1925 		    error = EIO;
1926 	} else {
1927 		error = nfsrpc_createv23(dvp, name, namelen, vap, cverf,
1928 		    fmode, cred, p, dnap, nnap, nfhpp, attrflagp, dattrflagp,
1929 		    dstuff);
1930 	}
1931 	return (error);
1932 }
1933 
1934 /*
1935  * The create rpc for v2 and 3.
1936  */
1937 static int
1938 nfsrpc_createv23(vnode_t dvp, char *name, int namelen, struct vattr *vap,
1939     nfsquad_t cverf, int fmode, struct ucred *cred, NFSPROC_T *p,
1940     struct nfsvattr *dnap, struct nfsvattr *nnap, struct nfsfh **nfhpp,
1941     int *attrflagp, int *dattrflagp, void *dstuff)
1942 {
1943 	u_int32_t *tl;
1944 	int error = 0;
1945 	struct nfsrv_descript nfsd, *nd = &nfsd;
1946 
1947 	*nfhpp = NULL;
1948 	*attrflagp = 0;
1949 	*dattrflagp = 0;
1950 	if (namelen > NFS_MAXNAMLEN)
1951 		return (ENAMETOOLONG);
1952 	NFSCL_REQSTART(nd, NFSPROC_CREATE, dvp);
1953 	(void) nfsm_strtom(nd, name, namelen);
1954 	if (nd->nd_flag & ND_NFSV3) {
1955 		NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1956 		if (fmode & O_EXCL) {
1957 			*tl = txdr_unsigned(NFSCREATE_EXCLUSIVE);
1958 			NFSM_BUILD(tl, u_int32_t *, NFSX_VERF);
1959 			*tl++ = cverf.lval[0];
1960 			*tl = cverf.lval[1];
1961 		} else {
1962 			*tl = txdr_unsigned(NFSCREATE_UNCHECKED);
1963 			nfscl_fillsattr(nd, vap, dvp, 0, 0);
1964 		}
1965 	} else {
1966 		nfscl_fillsattr(nd, vap, dvp, NFSSATTR_SIZE0, 0);
1967 	}
1968 	error = nfscl_request(nd, dvp, p, cred, dstuff);
1969 	if (error)
1970 		return (error);
1971 	if (nd->nd_repstat == 0) {
1972 		error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp);
1973 		if (error)
1974 			goto nfsmout;
1975 	}
1976 	if (nd->nd_flag & ND_NFSV3)
1977 		error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
1978 	if (nd->nd_repstat != 0 && error == 0)
1979 		error = nd->nd_repstat;
1980 nfsmout:
1981 	mbuf_freem(nd->nd_mrep);
1982 	return (error);
1983 }
1984 
1985 static int
1986 nfsrpc_createv4(vnode_t dvp, char *name, int namelen, struct vattr *vap,
1987     nfsquad_t cverf, int fmode, struct nfsclowner *owp, struct nfscldeleg **dpp,
1988     struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap,
1989     struct nfsvattr *nnap, struct nfsfh **nfhpp, int *attrflagp,
1990     int *dattrflagp, void *dstuff, int *unlockedp)
1991 {
1992 	u_int32_t *tl;
1993 	int error = 0, deleg, newone, ret, acesize, limitby;
1994 	struct nfsrv_descript nfsd, *nd = &nfsd;
1995 	struct nfsclopen *op;
1996 	struct nfscldeleg *dp = NULL;
1997 	struct nfsnode *np;
1998 	struct nfsfh *nfhp;
1999 	nfsattrbit_t attrbits;
2000 	nfsv4stateid_t stateid;
2001 	u_int32_t rflags;
2002 	struct nfsmount *nmp;
2003 	struct nfsclsession *tsep;
2004 
2005 	nmp = VFSTONFS(dvp->v_mount);
2006 	np = VTONFS(dvp);
2007 	*unlockedp = 0;
2008 	*nfhpp = NULL;
2009 	*dpp = NULL;
2010 	*attrflagp = 0;
2011 	*dattrflagp = 0;
2012 	if (namelen > NFS_MAXNAMLEN)
2013 		return (ENAMETOOLONG);
2014 	NFSCL_REQSTART(nd, NFSPROC_CREATE, dvp);
2015 	/*
2016 	 * For V4, this is actually an Open op.
2017 	 */
2018 	NFSM_BUILD(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
2019 	*tl++ = txdr_unsigned(owp->nfsow_seqid);
2020 	*tl++ = txdr_unsigned(NFSV4OPEN_ACCESSWRITE |
2021 	    NFSV4OPEN_ACCESSREAD);
2022 	*tl++ = txdr_unsigned(NFSV4OPEN_DENYNONE);
2023 	tsep = nfsmnt_mdssession(nmp);
2024 	*tl++ = tsep->nfsess_clientid.lval[0];
2025 	*tl = tsep->nfsess_clientid.lval[1];
2026 	(void) nfsm_strtom(nd, owp->nfsow_owner, NFSV4CL_LOCKNAMELEN);
2027 	NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2028 	*tl++ = txdr_unsigned(NFSV4OPEN_CREATE);
2029 	if (fmode & O_EXCL) {
2030 		if (NFSHASNFSV4N(nmp)) {
2031 			if (NFSHASSESSPERSIST(nmp)) {
2032 				/* Use GUARDED for persistent sessions. */
2033 				*tl = txdr_unsigned(NFSCREATE_GUARDED);
2034 				nfscl_fillsattr(nd, vap, dvp, 0, 0);
2035 			} else {
2036 				/* Otherwise, use EXCLUSIVE4_1. */
2037 				*tl = txdr_unsigned(NFSCREATE_EXCLUSIVE41);
2038 				NFSM_BUILD(tl, u_int32_t *, NFSX_VERF);
2039 				*tl++ = cverf.lval[0];
2040 				*tl = cverf.lval[1];
2041 				nfscl_fillsattr(nd, vap, dvp, 0, 0);
2042 			}
2043 		} else {
2044 			/* NFSv4.0 */
2045 			*tl = txdr_unsigned(NFSCREATE_EXCLUSIVE);
2046 			NFSM_BUILD(tl, u_int32_t *, NFSX_VERF);
2047 			*tl++ = cverf.lval[0];
2048 			*tl = cverf.lval[1];
2049 		}
2050 	} else {
2051 		*tl = txdr_unsigned(NFSCREATE_UNCHECKED);
2052 		nfscl_fillsattr(nd, vap, dvp, 0, 0);
2053 	}
2054 	NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2055 	*tl = txdr_unsigned(NFSV4OPEN_CLAIMNULL);
2056 	(void) nfsm_strtom(nd, name, namelen);
2057 	/* Get the new file's handle and attributes. */
2058 	NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2059 	*tl++ = txdr_unsigned(NFSV4OP_GETFH);
2060 	*tl = txdr_unsigned(NFSV4OP_GETATTR);
2061 	NFSGETATTR_ATTRBIT(&attrbits);
2062 	(void) nfsrv_putattrbit(nd, &attrbits);
2063 	/* Get the directory's post-op attributes. */
2064 	NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2065 	*tl = txdr_unsigned(NFSV4OP_PUTFH);
2066 	(void) nfsm_fhtom(nd, np->n_fhp->nfh_fh, np->n_fhp->nfh_len, 0);
2067 	NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2068 	*tl = txdr_unsigned(NFSV4OP_GETATTR);
2069 	(void) nfsrv_putattrbit(nd, &attrbits);
2070 	error = nfscl_request(nd, dvp, p, cred, dstuff);
2071 	if (error)
2072 		return (error);
2073 	NFSCL_INCRSEQID(owp->nfsow_seqid, nd);
2074 	if (nd->nd_repstat == 0) {
2075 		NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
2076 		    6 * NFSX_UNSIGNED);
2077 		stateid.seqid = *tl++;
2078 		stateid.other[0] = *tl++;
2079 		stateid.other[1] = *tl++;
2080 		stateid.other[2] = *tl;
2081 		rflags = fxdr_unsigned(u_int32_t, *(tl + 6));
2082 		(void) nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
2083 		NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2084 		deleg = fxdr_unsigned(int, *tl);
2085 		if (deleg == NFSV4OPEN_DELEGATEREAD ||
2086 		    deleg == NFSV4OPEN_DELEGATEWRITE) {
2087 			if (!(owp->nfsow_clp->nfsc_flags &
2088 			      NFSCLFLAGS_FIRSTDELEG))
2089 				owp->nfsow_clp->nfsc_flags |=
2090 				  (NFSCLFLAGS_FIRSTDELEG | NFSCLFLAGS_GOTDELEG);
2091 			MALLOC(dp, struct nfscldeleg *,
2092 			    sizeof (struct nfscldeleg) + NFSX_V4FHMAX,
2093 			    M_NFSCLDELEG, M_WAITOK);
2094 			LIST_INIT(&dp->nfsdl_owner);
2095 			LIST_INIT(&dp->nfsdl_lock);
2096 			dp->nfsdl_clp = owp->nfsow_clp;
2097 			newnfs_copyincred(cred, &dp->nfsdl_cred);
2098 			nfscl_lockinit(&dp->nfsdl_rwlock);
2099 			NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
2100 			    NFSX_UNSIGNED);
2101 			dp->nfsdl_stateid.seqid = *tl++;
2102 			dp->nfsdl_stateid.other[0] = *tl++;
2103 			dp->nfsdl_stateid.other[1] = *tl++;
2104 			dp->nfsdl_stateid.other[2] = *tl++;
2105 			ret = fxdr_unsigned(int, *tl);
2106 			if (deleg == NFSV4OPEN_DELEGATEWRITE) {
2107 				dp->nfsdl_flags = NFSCLDL_WRITE;
2108 				/*
2109 				 * Indicates how much the file can grow.
2110 				 */
2111 				NFSM_DISSECT(tl, u_int32_t *,
2112 				    3 * NFSX_UNSIGNED);
2113 				limitby = fxdr_unsigned(int, *tl++);
2114 				switch (limitby) {
2115 				case NFSV4OPEN_LIMITSIZE:
2116 					dp->nfsdl_sizelimit = fxdr_hyper(tl);
2117 					break;
2118 				case NFSV4OPEN_LIMITBLOCKS:
2119 					dp->nfsdl_sizelimit =
2120 					    fxdr_unsigned(u_int64_t, *tl++);
2121 					dp->nfsdl_sizelimit *=
2122 					    fxdr_unsigned(u_int64_t, *tl);
2123 					break;
2124 				default:
2125 					error = NFSERR_BADXDR;
2126 					goto nfsmout;
2127 				}
2128 			} else {
2129 				dp->nfsdl_flags = NFSCLDL_READ;
2130 			}
2131 			if (ret)
2132 				dp->nfsdl_flags |= NFSCLDL_RECALL;
2133 			error = nfsrv_dissectace(nd, &dp->nfsdl_ace, &ret,
2134 			    &acesize, p);
2135 			if (error)
2136 				goto nfsmout;
2137 		} else if (deleg != NFSV4OPEN_DELEGATENONE) {
2138 			error = NFSERR_BADXDR;
2139 			goto nfsmout;
2140 		}
2141 		error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp);
2142 		if (error)
2143 			goto nfsmout;
2144 		/* Get rid of the PutFH and Getattr status values. */
2145 		NFSM_DISSECT(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
2146 		/* Load the directory attributes. */
2147 		error = nfsm_loadattr(nd, dnap);
2148 		if (error)
2149 			goto nfsmout;
2150 		*dattrflagp = 1;
2151 		if (dp != NULL && *attrflagp) {
2152 			dp->nfsdl_change = nnap->na_filerev;
2153 			dp->nfsdl_modtime = nnap->na_mtime;
2154 			dp->nfsdl_flags |= NFSCLDL_MODTIMESET;
2155 		}
2156 		/*
2157 		 * We can now complete the Open state.
2158 		 */
2159 		nfhp = *nfhpp;
2160 		if (dp != NULL) {
2161 			dp->nfsdl_fhlen = nfhp->nfh_len;
2162 			NFSBCOPY(nfhp->nfh_fh, dp->nfsdl_fh, nfhp->nfh_len);
2163 		}
2164 		/*
2165 		 * Get an Open structure that will be
2166 		 * attached to the OpenOwner, acquired already.
2167 		 */
2168 		error = nfscl_open(dvp, nfhp->nfh_fh, nfhp->nfh_len,
2169 		    (NFSV4OPEN_ACCESSWRITE | NFSV4OPEN_ACCESSREAD), 0,
2170 		    cred, p, NULL, &op, &newone, NULL, 0);
2171 		if (error)
2172 			goto nfsmout;
2173 		op->nfso_stateid = stateid;
2174 		newnfs_copyincred(cred, &op->nfso_cred);
2175 		if ((rflags & NFSV4OPEN_RESULTCONFIRM)) {
2176 		    do {
2177 			ret = nfsrpc_openconfirm(dvp, nfhp->nfh_fh,
2178 			    nfhp->nfh_len, op, cred, p);
2179 			if (ret == NFSERR_DELAY)
2180 			    (void) nfs_catnap(PZERO, ret, "nfs_create");
2181 		    } while (ret == NFSERR_DELAY);
2182 		    error = ret;
2183 		}
2184 
2185 		/*
2186 		 * If the server is handing out delegations, but we didn't
2187 		 * get one because an OpenConfirm was required, try the
2188 		 * Open again, to get a delegation. This is a harmless no-op,
2189 		 * from a server's point of view.
2190 		 */
2191 		if ((rflags & NFSV4OPEN_RESULTCONFIRM) &&
2192 		    (owp->nfsow_clp->nfsc_flags & NFSCLFLAGS_GOTDELEG) &&
2193 		    !error && dp == NULL) {
2194 		    do {
2195 			ret = nfsrpc_openrpc(VFSTONFS(vnode_mount(dvp)), dvp,
2196 			    np->n_fhp->nfh_fh, np->n_fhp->nfh_len,
2197 			    nfhp->nfh_fh, nfhp->nfh_len,
2198 			    (NFSV4OPEN_ACCESSWRITE | NFSV4OPEN_ACCESSREAD), op,
2199 			    name, namelen, &dp, 0, 0x0, cred, p, 0, 1);
2200 			if (ret == NFSERR_DELAY)
2201 			    (void) nfs_catnap(PZERO, ret, "nfs_crt2");
2202 		    } while (ret == NFSERR_DELAY);
2203 		    if (ret) {
2204 			if (dp != NULL) {
2205 				FREE((caddr_t)dp, M_NFSCLDELEG);
2206 				dp = NULL;
2207 			}
2208 			if (ret == NFSERR_STALECLIENTID ||
2209 			    ret == NFSERR_STALEDONTRECOVER ||
2210 			    ret == NFSERR_BADSESSION)
2211 				error = ret;
2212 		    }
2213 		}
2214 		nfscl_openrelease(nmp, op, error, newone);
2215 		*unlockedp = 1;
2216 	}
2217 	if (nd->nd_repstat != 0 && error == 0)
2218 		error = nd->nd_repstat;
2219 	if (error == NFSERR_STALECLIENTID)
2220 		nfscl_initiate_recovery(owp->nfsow_clp);
2221 nfsmout:
2222 	if (!error)
2223 		*dpp = dp;
2224 	else if (dp != NULL)
2225 		FREE((caddr_t)dp, M_NFSCLDELEG);
2226 	mbuf_freem(nd->nd_mrep);
2227 	return (error);
2228 }
2229 
2230 /*
2231  * Nfs remove rpc
2232  */
2233 APPLESTATIC int
2234 nfsrpc_remove(vnode_t dvp, char *name, int namelen, vnode_t vp,
2235     struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap, int *dattrflagp,
2236     void *dstuff)
2237 {
2238 	u_int32_t *tl;
2239 	struct nfsrv_descript nfsd, *nd = &nfsd;
2240 	struct nfsnode *np;
2241 	struct nfsmount *nmp;
2242 	nfsv4stateid_t dstateid;
2243 	int error, ret = 0, i;
2244 
2245 	*dattrflagp = 0;
2246 	if (namelen > NFS_MAXNAMLEN)
2247 		return (ENAMETOOLONG);
2248 	nmp = VFSTONFS(vnode_mount(dvp));
2249 tryagain:
2250 	if (NFSHASNFSV4(nmp) && ret == 0) {
2251 		ret = nfscl_removedeleg(vp, p, &dstateid);
2252 		if (ret == 1) {
2253 			NFSCL_REQSTART(nd, NFSPROC_RETDELEGREMOVE, vp);
2254 			NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID +
2255 			    NFSX_UNSIGNED);
2256 			if (NFSHASNFSV4N(nmp))
2257 				*tl++ = 0;
2258 			else
2259 				*tl++ = dstateid.seqid;
2260 			*tl++ = dstateid.other[0];
2261 			*tl++ = dstateid.other[1];
2262 			*tl++ = dstateid.other[2];
2263 			*tl = txdr_unsigned(NFSV4OP_PUTFH);
2264 			np = VTONFS(dvp);
2265 			(void) nfsm_fhtom(nd, np->n_fhp->nfh_fh,
2266 			    np->n_fhp->nfh_len, 0);
2267 			NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2268 			*tl = txdr_unsigned(NFSV4OP_REMOVE);
2269 		}
2270 	} else {
2271 		ret = 0;
2272 	}
2273 	if (ret == 0)
2274 		NFSCL_REQSTART(nd, NFSPROC_REMOVE, dvp);
2275 	(void) nfsm_strtom(nd, name, namelen);
2276 	error = nfscl_request(nd, dvp, p, cred, dstuff);
2277 	if (error)
2278 		return (error);
2279 	if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) {
2280 		/* For NFSv4, parse out any Delereturn replies. */
2281 		if (ret > 0 && nd->nd_repstat != 0 &&
2282 		    (nd->nd_flag & ND_NOMOREDATA)) {
2283 			/*
2284 			 * If the Delegreturn failed, try again without
2285 			 * it. The server will Recall, as required.
2286 			 */
2287 			mbuf_freem(nd->nd_mrep);
2288 			goto tryagain;
2289 		}
2290 		for (i = 0; i < (ret * 2); i++) {
2291 			if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) ==
2292 			    ND_NFSV4) {
2293 			    NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2294 			    if (*(tl + 1))
2295 				nd->nd_flag |= ND_NOMOREDATA;
2296 			}
2297 		}
2298 		error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
2299 	}
2300 	if (nd->nd_repstat && !error)
2301 		error = nd->nd_repstat;
2302 nfsmout:
2303 	mbuf_freem(nd->nd_mrep);
2304 	return (error);
2305 }
2306 
2307 /*
2308  * Do an nfs rename rpc.
2309  */
2310 APPLESTATIC int
2311 nfsrpc_rename(vnode_t fdvp, vnode_t fvp, char *fnameptr, int fnamelen,
2312     vnode_t tdvp, vnode_t tvp, char *tnameptr, int tnamelen, struct ucred *cred,
2313     NFSPROC_T *p, struct nfsvattr *fnap, struct nfsvattr *tnap,
2314     int *fattrflagp, int *tattrflagp, void *fstuff, void *tstuff)
2315 {
2316 	u_int32_t *tl;
2317 	struct nfsrv_descript nfsd, *nd = &nfsd;
2318 	struct nfsmount *nmp;
2319 	struct nfsnode *np;
2320 	nfsattrbit_t attrbits;
2321 	nfsv4stateid_t fdstateid, tdstateid;
2322 	int error = 0, ret = 0, gottd = 0, gotfd = 0, i;
2323 
2324 	*fattrflagp = 0;
2325 	*tattrflagp = 0;
2326 	nmp = VFSTONFS(vnode_mount(fdvp));
2327 	if (fnamelen > NFS_MAXNAMLEN || tnamelen > NFS_MAXNAMLEN)
2328 		return (ENAMETOOLONG);
2329 tryagain:
2330 	if (NFSHASNFSV4(nmp) && ret == 0) {
2331 		ret = nfscl_renamedeleg(fvp, &fdstateid, &gotfd, tvp,
2332 		    &tdstateid, &gottd, p);
2333 		if (gotfd && gottd) {
2334 			NFSCL_REQSTART(nd, NFSPROC_RETDELEGRENAME2, fvp);
2335 		} else if (gotfd) {
2336 			NFSCL_REQSTART(nd, NFSPROC_RETDELEGRENAME1, fvp);
2337 		} else if (gottd) {
2338 			NFSCL_REQSTART(nd, NFSPROC_RETDELEGRENAME1, tvp);
2339 		}
2340 		if (gotfd) {
2341 			NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID);
2342 			if (NFSHASNFSV4N(nmp))
2343 				*tl++ = 0;
2344 			else
2345 				*tl++ = fdstateid.seqid;
2346 			*tl++ = fdstateid.other[0];
2347 			*tl++ = fdstateid.other[1];
2348 			*tl = fdstateid.other[2];
2349 			if (gottd) {
2350 				NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2351 				*tl = txdr_unsigned(NFSV4OP_PUTFH);
2352 				np = VTONFS(tvp);
2353 				(void) nfsm_fhtom(nd, np->n_fhp->nfh_fh,
2354 				    np->n_fhp->nfh_len, 0);
2355 				NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2356 				*tl = txdr_unsigned(NFSV4OP_DELEGRETURN);
2357 			}
2358 		}
2359 		if (gottd) {
2360 			NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID);
2361 			if (NFSHASNFSV4N(nmp))
2362 				*tl++ = 0;
2363 			else
2364 				*tl++ = tdstateid.seqid;
2365 			*tl++ = tdstateid.other[0];
2366 			*tl++ = tdstateid.other[1];
2367 			*tl = tdstateid.other[2];
2368 		}
2369 		if (ret > 0) {
2370 			NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2371 			*tl = txdr_unsigned(NFSV4OP_PUTFH);
2372 			np = VTONFS(fdvp);
2373 			(void) nfsm_fhtom(nd, np->n_fhp->nfh_fh,
2374 			    np->n_fhp->nfh_len, 0);
2375 			NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2376 			*tl = txdr_unsigned(NFSV4OP_SAVEFH);
2377 		}
2378 	} else {
2379 		ret = 0;
2380 	}
2381 	if (ret == 0)
2382 		NFSCL_REQSTART(nd, NFSPROC_RENAME, fdvp);
2383 	if (nd->nd_flag & ND_NFSV4) {
2384 		NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2385 		*tl = txdr_unsigned(NFSV4OP_GETATTR);
2386 		NFSWCCATTR_ATTRBIT(&attrbits);
2387 		(void) nfsrv_putattrbit(nd, &attrbits);
2388 		NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2389 		*tl = txdr_unsigned(NFSV4OP_PUTFH);
2390 		(void) nfsm_fhtom(nd, VTONFS(tdvp)->n_fhp->nfh_fh,
2391 		    VTONFS(tdvp)->n_fhp->nfh_len, 0);
2392 		NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2393 		*tl = txdr_unsigned(NFSV4OP_GETATTR);
2394 		(void) nfsrv_putattrbit(nd, &attrbits);
2395 		nd->nd_flag |= ND_V4WCCATTR;
2396 		NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2397 		*tl = txdr_unsigned(NFSV4OP_RENAME);
2398 	}
2399 	(void) nfsm_strtom(nd, fnameptr, fnamelen);
2400 	if (!(nd->nd_flag & ND_NFSV4))
2401 		(void) nfsm_fhtom(nd, VTONFS(tdvp)->n_fhp->nfh_fh,
2402 			VTONFS(tdvp)->n_fhp->nfh_len, 0);
2403 	(void) nfsm_strtom(nd, tnameptr, tnamelen);
2404 	error = nfscl_request(nd, fdvp, p, cred, fstuff);
2405 	if (error)
2406 		return (error);
2407 	if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) {
2408 		/* For NFSv4, parse out any Delereturn replies. */
2409 		if (ret > 0 && nd->nd_repstat != 0 &&
2410 		    (nd->nd_flag & ND_NOMOREDATA)) {
2411 			/*
2412 			 * If the Delegreturn failed, try again without
2413 			 * it. The server will Recall, as required.
2414 			 */
2415 			mbuf_freem(nd->nd_mrep);
2416 			goto tryagain;
2417 		}
2418 		for (i = 0; i < (ret * 2); i++) {
2419 			if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) ==
2420 			    ND_NFSV4) {
2421 			    NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2422 			    if (*(tl + 1)) {
2423 				if (i == 0 && ret > 1) {
2424 				    /*
2425 				     * If the Delegreturn failed, try again
2426 				     * without it. The server will Recall, as
2427 				     * required.
2428 				     * If ret > 1, the first iteration of this
2429 				     * loop is the second DelegReturn result.
2430 				     */
2431 				    mbuf_freem(nd->nd_mrep);
2432 				    goto tryagain;
2433 				} else {
2434 				    nd->nd_flag |= ND_NOMOREDATA;
2435 				}
2436 			    }
2437 			}
2438 		}
2439 		/* Now, the first wcc attribute reply. */
2440 		if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) == ND_NFSV4) {
2441 			NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2442 			if (*(tl + 1))
2443 				nd->nd_flag |= ND_NOMOREDATA;
2444 		}
2445 		error = nfscl_wcc_data(nd, fdvp, fnap, fattrflagp, NULL,
2446 		    fstuff);
2447 		/* and the second wcc attribute reply. */
2448 		if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) == ND_NFSV4 &&
2449 		    !error) {
2450 			NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2451 			if (*(tl + 1))
2452 				nd->nd_flag |= ND_NOMOREDATA;
2453 		}
2454 		if (!error)
2455 			error = nfscl_wcc_data(nd, tdvp, tnap, tattrflagp,
2456 			    NULL, tstuff);
2457 	}
2458 	if (nd->nd_repstat && !error)
2459 		error = nd->nd_repstat;
2460 nfsmout:
2461 	mbuf_freem(nd->nd_mrep);
2462 	return (error);
2463 }
2464 
2465 /*
2466  * nfs hard link create rpc
2467  */
2468 APPLESTATIC int
2469 nfsrpc_link(vnode_t dvp, vnode_t vp, char *name, int namelen,
2470     struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap,
2471     struct nfsvattr *nap, int *attrflagp, int *dattrflagp, void *dstuff)
2472 {
2473 	u_int32_t *tl;
2474 	struct nfsrv_descript nfsd, *nd = &nfsd;
2475 	nfsattrbit_t attrbits;
2476 	int error = 0;
2477 
2478 	*attrflagp = 0;
2479 	*dattrflagp = 0;
2480 	if (namelen > NFS_MAXNAMLEN)
2481 		return (ENAMETOOLONG);
2482 	NFSCL_REQSTART(nd, NFSPROC_LINK, vp);
2483 	if (nd->nd_flag & ND_NFSV4) {
2484 		NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2485 		*tl = txdr_unsigned(NFSV4OP_PUTFH);
2486 	}
2487 	(void) nfsm_fhtom(nd, VTONFS(dvp)->n_fhp->nfh_fh,
2488 		VTONFS(dvp)->n_fhp->nfh_len, 0);
2489 	if (nd->nd_flag & ND_NFSV4) {
2490 		NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2491 		*tl = txdr_unsigned(NFSV4OP_GETATTR);
2492 		NFSWCCATTR_ATTRBIT(&attrbits);
2493 		(void) nfsrv_putattrbit(nd, &attrbits);
2494 		nd->nd_flag |= ND_V4WCCATTR;
2495 		NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2496 		*tl = txdr_unsigned(NFSV4OP_LINK);
2497 	}
2498 	(void) nfsm_strtom(nd, name, namelen);
2499 	error = nfscl_request(nd, vp, p, cred, dstuff);
2500 	if (error)
2501 		return (error);
2502 	if (nd->nd_flag & ND_NFSV3) {
2503 		error = nfscl_postop_attr(nd, nap, attrflagp, dstuff);
2504 		if (!error)
2505 			error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp,
2506 			    NULL, dstuff);
2507 	} else if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) == ND_NFSV4) {
2508 		/*
2509 		 * First, parse out the PutFH and Getattr result.
2510 		 */
2511 		NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2512 		if (!(*(tl + 1)))
2513 			NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2514 		if (*(tl + 1))
2515 			nd->nd_flag |= ND_NOMOREDATA;
2516 		/*
2517 		 * Get the pre-op attributes.
2518 		 */
2519 		error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
2520 	}
2521 	if (nd->nd_repstat && !error)
2522 		error = nd->nd_repstat;
2523 nfsmout:
2524 	mbuf_freem(nd->nd_mrep);
2525 	return (error);
2526 }
2527 
2528 /*
2529  * nfs symbolic link create rpc
2530  */
2531 APPLESTATIC int
2532 nfsrpc_symlink(vnode_t dvp, char *name, int namelen, char *target,
2533     struct vattr *vap, struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap,
2534     struct nfsvattr *nnap, struct nfsfh **nfhpp, int *attrflagp,
2535     int *dattrflagp, void *dstuff)
2536 {
2537 	u_int32_t *tl;
2538 	struct nfsrv_descript nfsd, *nd = &nfsd;
2539 	struct nfsmount *nmp;
2540 	int slen, error = 0;
2541 
2542 	*nfhpp = NULL;
2543 	*attrflagp = 0;
2544 	*dattrflagp = 0;
2545 	nmp = VFSTONFS(vnode_mount(dvp));
2546 	slen = strlen(target);
2547 	if (slen > NFS_MAXPATHLEN || namelen > NFS_MAXNAMLEN)
2548 		return (ENAMETOOLONG);
2549 	NFSCL_REQSTART(nd, NFSPROC_SYMLINK, dvp);
2550 	if (nd->nd_flag & ND_NFSV4) {
2551 		NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2552 		*tl = txdr_unsigned(NFLNK);
2553 		(void) nfsm_strtom(nd, target, slen);
2554 	}
2555 	(void) nfsm_strtom(nd, name, namelen);
2556 	if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4))
2557 		nfscl_fillsattr(nd, vap, dvp, 0, 0);
2558 	if (!(nd->nd_flag & ND_NFSV4))
2559 		(void) nfsm_strtom(nd, target, slen);
2560 	if (nd->nd_flag & ND_NFSV2)
2561 		nfscl_fillsattr(nd, vap, dvp, NFSSATTR_SIZENEG1, 0);
2562 	error = nfscl_request(nd, dvp, p, cred, dstuff);
2563 	if (error)
2564 		return (error);
2565 	if (nd->nd_flag & ND_NFSV4)
2566 		error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
2567 	if ((nd->nd_flag & ND_NFSV3) && !error) {
2568 		if (!nd->nd_repstat)
2569 			error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp);
2570 		if (!error)
2571 			error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp,
2572 			    NULL, dstuff);
2573 	}
2574 	if (nd->nd_repstat && !error)
2575 		error = nd->nd_repstat;
2576 	mbuf_freem(nd->nd_mrep);
2577 	/*
2578 	 * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry.
2579 	 * Only do this if vfs.nfs.ignore_eexist is set.
2580 	 * Never do this for NFSv4.1 or later minor versions, since sessions
2581 	 * should guarantee "exactly once" RPC semantics.
2582 	 */
2583 	if (error == EEXIST && nfsignore_eexist != 0 && (!NFSHASNFSV4(nmp) ||
2584 	    nmp->nm_minorvers == 0))
2585 		error = 0;
2586 	return (error);
2587 }
2588 
2589 /*
2590  * nfs make dir rpc
2591  */
2592 APPLESTATIC int
2593 nfsrpc_mkdir(vnode_t dvp, char *name, int namelen, struct vattr *vap,
2594     struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap,
2595     struct nfsvattr *nnap, struct nfsfh **nfhpp, int *attrflagp,
2596     int *dattrflagp, void *dstuff)
2597 {
2598 	u_int32_t *tl;
2599 	struct nfsrv_descript nfsd, *nd = &nfsd;
2600 	nfsattrbit_t attrbits;
2601 	int error = 0;
2602 	struct nfsfh *fhp;
2603 	struct nfsmount *nmp;
2604 
2605 	*nfhpp = NULL;
2606 	*attrflagp = 0;
2607 	*dattrflagp = 0;
2608 	nmp = VFSTONFS(vnode_mount(dvp));
2609 	fhp = VTONFS(dvp)->n_fhp;
2610 	if (namelen > NFS_MAXNAMLEN)
2611 		return (ENAMETOOLONG);
2612 	NFSCL_REQSTART(nd, NFSPROC_MKDIR, dvp);
2613 	if (nd->nd_flag & ND_NFSV4) {
2614 		NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2615 		*tl = txdr_unsigned(NFDIR);
2616 	}
2617 	(void) nfsm_strtom(nd, name, namelen);
2618 	nfscl_fillsattr(nd, vap, dvp, NFSSATTR_SIZENEG1, 0);
2619 	if (nd->nd_flag & ND_NFSV4) {
2620 		NFSGETATTR_ATTRBIT(&attrbits);
2621 		NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2622 		*tl++ = txdr_unsigned(NFSV4OP_GETFH);
2623 		*tl = txdr_unsigned(NFSV4OP_GETATTR);
2624 		(void) nfsrv_putattrbit(nd, &attrbits);
2625 		NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2626 		*tl = txdr_unsigned(NFSV4OP_PUTFH);
2627 		(void) nfsm_fhtom(nd, fhp->nfh_fh, fhp->nfh_len, 0);
2628 		NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2629 		*tl = txdr_unsigned(NFSV4OP_GETATTR);
2630 		(void) nfsrv_putattrbit(nd, &attrbits);
2631 	}
2632 	error = nfscl_request(nd, dvp, p, cred, dstuff);
2633 	if (error)
2634 		return (error);
2635 	if (nd->nd_flag & ND_NFSV4)
2636 		error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
2637 	if (!nd->nd_repstat && !error) {
2638 		if (nd->nd_flag & ND_NFSV4) {
2639 			NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
2640 			error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
2641 		}
2642 		if (!error)
2643 			error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp);
2644 		if (error == 0 && (nd->nd_flag & ND_NFSV4) != 0) {
2645 			/* Get rid of the PutFH and Getattr status values. */
2646 			NFSM_DISSECT(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
2647 			/* Load the directory attributes. */
2648 			error = nfsm_loadattr(nd, dnap);
2649 			if (error == 0)
2650 				*dattrflagp = 1;
2651 		}
2652 	}
2653 	if ((nd->nd_flag & ND_NFSV3) && !error)
2654 		error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
2655 	if (nd->nd_repstat && !error)
2656 		error = nd->nd_repstat;
2657 nfsmout:
2658 	mbuf_freem(nd->nd_mrep);
2659 	/*
2660 	 * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry.
2661 	 * Only do this if vfs.nfs.ignore_eexist is set.
2662 	 * Never do this for NFSv4.1 or later minor versions, since sessions
2663 	 * should guarantee "exactly once" RPC semantics.
2664 	 */
2665 	if (error == EEXIST && nfsignore_eexist != 0 && (!NFSHASNFSV4(nmp) ||
2666 	    nmp->nm_minorvers == 0))
2667 		error = 0;
2668 	return (error);
2669 }
2670 
2671 /*
2672  * nfs remove directory call
2673  */
2674 APPLESTATIC int
2675 nfsrpc_rmdir(vnode_t dvp, char *name, int namelen, struct ucred *cred,
2676     NFSPROC_T *p, struct nfsvattr *dnap, int *dattrflagp, void *dstuff)
2677 {
2678 	struct nfsrv_descript nfsd, *nd = &nfsd;
2679 	int error = 0;
2680 
2681 	*dattrflagp = 0;
2682 	if (namelen > NFS_MAXNAMLEN)
2683 		return (ENAMETOOLONG);
2684 	NFSCL_REQSTART(nd, NFSPROC_RMDIR, dvp);
2685 	(void) nfsm_strtom(nd, name, namelen);
2686 	error = nfscl_request(nd, dvp, p, cred, dstuff);
2687 	if (error)
2688 		return (error);
2689 	if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4))
2690 		error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
2691 	if (nd->nd_repstat && !error)
2692 		error = nd->nd_repstat;
2693 	mbuf_freem(nd->nd_mrep);
2694 	/*
2695 	 * Kludge: Map ENOENT => 0 assuming that you have a reply to a retry.
2696 	 */
2697 	if (error == ENOENT)
2698 		error = 0;
2699 	return (error);
2700 }
2701 
2702 /*
2703  * Readdir rpc.
2704  * Always returns with either uio_resid unchanged, if you are at the
2705  * end of the directory, or uio_resid == 0, with all DIRBLKSIZ chunks
2706  * filled in.
2707  * I felt this would allow caching of directory blocks more easily
2708  * than returning a pertially filled block.
2709  * Directory offset cookies:
2710  * Oh my, what to do with them...
2711  * I can think of three ways to deal with them:
2712  * 1 - have the layer above these RPCs maintain a map between logical
2713  *     directory byte offsets and the NFS directory offset cookies
2714  * 2 - pass the opaque directory offset cookies up into userland
2715  *     and let the libc functions deal with them, via the system call
2716  * 3 - return them to userland in the "struct dirent", so future versions
2717  *     of libc can use them and do whatever is necessary to make things work
2718  *     above these rpc calls, in the meantime
2719  * For now, I do #3 by "hiding" the directory offset cookies after the
2720  * d_name field in struct dirent. This is space inside d_reclen that
2721  * will be ignored by anything that doesn't know about them.
2722  * The directory offset cookies are filled in as the last 8 bytes of
2723  * each directory entry, after d_name. Someday, the userland libc
2724  * functions may be able to use these. In the meantime, it satisfies
2725  * OpenBSD's requirements for cookies being returned.
2726  * If expects the directory offset cookie for the read to be in uio_offset
2727  * and returns the one for the next entry after this directory block in
2728  * there, as well.
2729  */
2730 APPLESTATIC int
2731 nfsrpc_readdir(vnode_t vp, struct uio *uiop, nfsuint64 *cookiep,
2732     struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp,
2733     int *eofp, void *stuff)
2734 {
2735 	int len, left;
2736 	struct dirent *dp = NULL;
2737 	u_int32_t *tl;
2738 	nfsquad_t cookie, ncookie;
2739 	struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
2740 	struct nfsnode *dnp = VTONFS(vp);
2741 	struct nfsvattr nfsva;
2742 	struct nfsrv_descript nfsd, *nd = &nfsd;
2743 	int error = 0, tlen, more_dirs = 1, blksiz = 0, bigenough = 1;
2744 	int reqsize, tryformoredirs = 1, readsize, eof = 0, gotmnton = 0;
2745 	u_int64_t dotfileid, dotdotfileid = 0, fakefileno = UINT64_MAX;
2746 	char *cp;
2747 	nfsattrbit_t attrbits, dattrbits;
2748 	u_int32_t rderr, *tl2 = NULL;
2749 	size_t tresid;
2750 
2751 	KASSERT(uiop->uio_iovcnt == 1 &&
2752 	    (uio_uio_resid(uiop) & (DIRBLKSIZ - 1)) == 0,
2753 	    ("nfs readdirrpc bad uio"));
2754 
2755 	/*
2756 	 * There is no point in reading a lot more than uio_resid, however
2757 	 * adding one additional DIRBLKSIZ makes sense. Since uio_resid
2758 	 * and nm_readdirsize are both exact multiples of DIRBLKSIZ, this
2759 	 * will never make readsize > nm_readdirsize.
2760 	 */
2761 	readsize = nmp->nm_readdirsize;
2762 	if (readsize > uio_uio_resid(uiop))
2763 		readsize = uio_uio_resid(uiop) + DIRBLKSIZ;
2764 
2765 	*attrflagp = 0;
2766 	if (eofp)
2767 		*eofp = 0;
2768 	tresid = uio_uio_resid(uiop);
2769 	cookie.lval[0] = cookiep->nfsuquad[0];
2770 	cookie.lval[1] = cookiep->nfsuquad[1];
2771 	nd->nd_mrep = NULL;
2772 
2773 	/*
2774 	 * For NFSv4, first create the "." and ".." entries.
2775 	 */
2776 	if (NFSHASNFSV4(nmp)) {
2777 		reqsize = 6 * NFSX_UNSIGNED;
2778 		NFSGETATTR_ATTRBIT(&dattrbits);
2779 		NFSZERO_ATTRBIT(&attrbits);
2780 		NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_FILEID);
2781 		NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TYPE);
2782 		if (NFSISSET_ATTRBIT(&dnp->n_vattr.na_suppattr,
2783 		    NFSATTRBIT_MOUNTEDONFILEID)) {
2784 			NFSSETBIT_ATTRBIT(&attrbits,
2785 			    NFSATTRBIT_MOUNTEDONFILEID);
2786 			gotmnton = 1;
2787 		} else {
2788 			/*
2789 			 * Must fake it. Use the fileno, except when the
2790 			 * fsid is != to that of the directory. For that
2791 			 * case, generate a fake fileno that is not the same.
2792 			 */
2793 			NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_FSID);
2794 			gotmnton = 0;
2795 		}
2796 
2797 		/*
2798 		 * Joy, oh joy. For V4 we get to hand craft '.' and '..'.
2799 		 */
2800 		if (uiop->uio_offset == 0) {
2801 			NFSCL_REQSTART(nd, NFSPROC_LOOKUPP, vp);
2802 			NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2803 			*tl++ = txdr_unsigned(NFSV4OP_GETFH);
2804 			*tl = txdr_unsigned(NFSV4OP_GETATTR);
2805 			(void) nfsrv_putattrbit(nd, &attrbits);
2806 			error = nfscl_request(nd, vp, p, cred, stuff);
2807 			if (error)
2808 			    return (error);
2809 			dotfileid = 0;	/* Fake out the compiler. */
2810 			if ((nd->nd_flag & ND_NOMOREDATA) == 0) {
2811 			    error = nfsm_loadattr(nd, &nfsva);
2812 			    if (error != 0)
2813 				goto nfsmout;
2814 			    dotfileid = nfsva.na_fileid;
2815 			}
2816 			if (nd->nd_repstat == 0) {
2817 			    NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
2818 			    len = fxdr_unsigned(int, *(tl + 4));
2819 			    if (len > 0 && len <= NFSX_V4FHMAX)
2820 				error = nfsm_advance(nd, NFSM_RNDUP(len), -1);
2821 			    else
2822 				error = EPERM;
2823 			    if (!error) {
2824 				NFSM_DISSECT(tl, u_int32_t *, 2*NFSX_UNSIGNED);
2825 				nfsva.na_mntonfileno = UINT64_MAX;
2826 				error = nfsv4_loadattr(nd, NULL, &nfsva, NULL,
2827 				    NULL, 0, NULL, NULL, NULL, NULL, NULL, 0,
2828 				    NULL, NULL, NULL, p, cred);
2829 				if (error) {
2830 				    dotdotfileid = dotfileid;
2831 				} else if (gotmnton) {
2832 				    if (nfsva.na_mntonfileno != UINT64_MAX)
2833 					dotdotfileid = nfsva.na_mntonfileno;
2834 				    else
2835 					dotdotfileid = nfsva.na_fileid;
2836 				} else if (nfsva.na_filesid[0] ==
2837 				    dnp->n_vattr.na_filesid[0] &&
2838 				    nfsva.na_filesid[1] ==
2839 				    dnp->n_vattr.na_filesid[1]) {
2840 				    dotdotfileid = nfsva.na_fileid;
2841 				} else {
2842 				    do {
2843 					fakefileno--;
2844 				    } while (fakefileno ==
2845 					nfsva.na_fileid);
2846 				    dotdotfileid = fakefileno;
2847 				}
2848 			    }
2849 			} else if (nd->nd_repstat == NFSERR_NOENT) {
2850 			    /*
2851 			     * Lookupp returns NFSERR_NOENT when we are
2852 			     * at the root, so just use the current dir.
2853 			     */
2854 			    nd->nd_repstat = 0;
2855 			    dotdotfileid = dotfileid;
2856 			} else {
2857 			    error = nd->nd_repstat;
2858 			}
2859 			mbuf_freem(nd->nd_mrep);
2860 			if (error)
2861 			    return (error);
2862 			nd->nd_mrep = NULL;
2863 			dp = (struct dirent *)uio_iov_base(uiop);
2864 			dp->d_off = 0;
2865 			dp->d_type = DT_DIR;
2866 			dp->d_fileno = dotfileid;
2867 			dp->d_namlen = 1;
2868 			*((uint64_t *)dp->d_name) = 0;	/* Zero pad it. */
2869 			dp->d_name[0] = '.';
2870 			dp->d_reclen = _GENERIC_DIRSIZ(dp) + NFSX_HYPER;
2871 			/*
2872 			 * Just make these offset cookie 0.
2873 			 */
2874 			tl = (u_int32_t *)&dp->d_name[8];
2875 			*tl++ = 0;
2876 			*tl = 0;
2877 			blksiz += dp->d_reclen;
2878 			uio_uio_resid_add(uiop, -(dp->d_reclen));
2879 			uiop->uio_offset += dp->d_reclen;
2880 			uio_iov_base_add(uiop, dp->d_reclen);
2881 			uio_iov_len_add(uiop, -(dp->d_reclen));
2882 			dp = (struct dirent *)uio_iov_base(uiop);
2883 			dp->d_off = 0;
2884 			dp->d_type = DT_DIR;
2885 			dp->d_fileno = dotdotfileid;
2886 			dp->d_namlen = 2;
2887 			*((uint64_t *)dp->d_name) = 0;
2888 			dp->d_name[0] = '.';
2889 			dp->d_name[1] = '.';
2890 			dp->d_reclen = _GENERIC_DIRSIZ(dp) + NFSX_HYPER;
2891 			/*
2892 			 * Just make these offset cookie 0.
2893 			 */
2894 			tl = (u_int32_t *)&dp->d_name[8];
2895 			*tl++ = 0;
2896 			*tl = 0;
2897 			blksiz += dp->d_reclen;
2898 			uio_uio_resid_add(uiop, -(dp->d_reclen));
2899 			uiop->uio_offset += dp->d_reclen;
2900 			uio_iov_base_add(uiop, dp->d_reclen);
2901 			uio_iov_len_add(uiop, -(dp->d_reclen));
2902 		}
2903 		NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_RDATTRERROR);
2904 	} else {
2905 		reqsize = 5 * NFSX_UNSIGNED;
2906 	}
2907 
2908 
2909 	/*
2910 	 * Loop around doing readdir rpc's of size readsize.
2911 	 * The stopping criteria is EOF or buffer full.
2912 	 */
2913 	while (more_dirs && bigenough) {
2914 		*attrflagp = 0;
2915 		NFSCL_REQSTART(nd, NFSPROC_READDIR, vp);
2916 		if (nd->nd_flag & ND_NFSV2) {
2917 			NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2918 			*tl++ = cookie.lval[1];
2919 			*tl = txdr_unsigned(readsize);
2920 		} else {
2921 			NFSM_BUILD(tl, u_int32_t *, reqsize);
2922 			*tl++ = cookie.lval[0];
2923 			*tl++ = cookie.lval[1];
2924 			if (cookie.qval == 0) {
2925 				*tl++ = 0;
2926 				*tl++ = 0;
2927 			} else {
2928 				NFSLOCKNODE(dnp);
2929 				*tl++ = dnp->n_cookieverf.nfsuquad[0];
2930 				*tl++ = dnp->n_cookieverf.nfsuquad[1];
2931 				NFSUNLOCKNODE(dnp);
2932 			}
2933 			if (nd->nd_flag & ND_NFSV4) {
2934 				*tl++ = txdr_unsigned(readsize);
2935 				*tl = txdr_unsigned(readsize);
2936 				(void) nfsrv_putattrbit(nd, &attrbits);
2937 				NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2938 				*tl = txdr_unsigned(NFSV4OP_GETATTR);
2939 				(void) nfsrv_putattrbit(nd, &dattrbits);
2940 			} else {
2941 				*tl = txdr_unsigned(readsize);
2942 			}
2943 		}
2944 		error = nfscl_request(nd, vp, p, cred, stuff);
2945 		if (error)
2946 			return (error);
2947 		if (!(nd->nd_flag & ND_NFSV2)) {
2948 			if (nd->nd_flag & ND_NFSV3)
2949 				error = nfscl_postop_attr(nd, nap, attrflagp,
2950 				    stuff);
2951 			if (!nd->nd_repstat && !error) {
2952 				NFSM_DISSECT(tl, u_int32_t *, NFSX_HYPER);
2953 				NFSLOCKNODE(dnp);
2954 				dnp->n_cookieverf.nfsuquad[0] = *tl++;
2955 				dnp->n_cookieverf.nfsuquad[1] = *tl;
2956 				NFSUNLOCKNODE(dnp);
2957 			}
2958 		}
2959 		if (nd->nd_repstat || error) {
2960 			if (!error)
2961 				error = nd->nd_repstat;
2962 			goto nfsmout;
2963 		}
2964 		NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2965 		more_dirs = fxdr_unsigned(int, *tl);
2966 		if (!more_dirs)
2967 			tryformoredirs = 0;
2968 
2969 		/* loop through the dir entries, doctoring them to 4bsd form */
2970 		while (more_dirs && bigenough) {
2971 			if (nd->nd_flag & ND_NFSV4) {
2972 				NFSM_DISSECT(tl, u_int32_t *, 3*NFSX_UNSIGNED);
2973 				ncookie.lval[0] = *tl++;
2974 				ncookie.lval[1] = *tl++;
2975 				len = fxdr_unsigned(int, *tl);
2976 			} else if (nd->nd_flag & ND_NFSV3) {
2977 				NFSM_DISSECT(tl, u_int32_t *, 3*NFSX_UNSIGNED);
2978 				nfsva.na_fileid = fxdr_hyper(tl);
2979 				tl += 2;
2980 				len = fxdr_unsigned(int, *tl);
2981 			} else {
2982 				NFSM_DISSECT(tl, u_int32_t *, 2*NFSX_UNSIGNED);
2983 				nfsva.na_fileid = fxdr_unsigned(uint64_t,
2984 				    *tl++);
2985 				len = fxdr_unsigned(int, *tl);
2986 			}
2987 			if (len <= 0 || len > NFS_MAXNAMLEN) {
2988 				error = EBADRPC;
2989 				goto nfsmout;
2990 			}
2991 			tlen = roundup2(len, 8);
2992 			if (tlen == len)
2993 				tlen += 8;  /* To ensure null termination. */
2994 			left = DIRBLKSIZ - blksiz;
2995 			if (_GENERIC_DIRLEN(len) + NFSX_HYPER > left) {
2996 				dp->d_reclen += left;
2997 				uio_iov_base_add(uiop, left);
2998 				uio_iov_len_add(uiop, -(left));
2999 				uio_uio_resid_add(uiop, -(left));
3000 				uiop->uio_offset += left;
3001 				blksiz = 0;
3002 			}
3003 			if (_GENERIC_DIRLEN(len) + NFSX_HYPER >
3004 			    uio_uio_resid(uiop))
3005 				bigenough = 0;
3006 			if (bigenough) {
3007 				dp = (struct dirent *)uio_iov_base(uiop);
3008 				dp->d_off = 0;
3009 				dp->d_namlen = len;
3010 				dp->d_reclen = _GENERIC_DIRLEN(len) +
3011 				    NFSX_HYPER;
3012 				dp->d_type = DT_UNKNOWN;
3013 				blksiz += dp->d_reclen;
3014 				if (blksiz == DIRBLKSIZ)
3015 					blksiz = 0;
3016 				uio_uio_resid_add(uiop, -(DIRHDSIZ));
3017 				uiop->uio_offset += DIRHDSIZ;
3018 				uio_iov_base_add(uiop, DIRHDSIZ);
3019 				uio_iov_len_add(uiop, -(DIRHDSIZ));
3020 				error = nfsm_mbufuio(nd, uiop, len);
3021 				if (error)
3022 					goto nfsmout;
3023 				cp = uio_iov_base(uiop);
3024 				tlen -= len;
3025 				*cp = '\0';	/* null terminate */
3026 				cp += tlen;	/* points to cookie storage */
3027 				tl2 = (u_int32_t *)cp;
3028 				uio_iov_base_add(uiop, (tlen + NFSX_HYPER));
3029 				uio_iov_len_add(uiop, -(tlen + NFSX_HYPER));
3030 				uio_uio_resid_add(uiop, -(tlen + NFSX_HYPER));
3031 				uiop->uio_offset += (tlen + NFSX_HYPER);
3032 			} else {
3033 				error = nfsm_advance(nd, NFSM_RNDUP(len), -1);
3034 				if (error)
3035 					goto nfsmout;
3036 			}
3037 			if (nd->nd_flag & ND_NFSV4) {
3038 				rderr = 0;
3039 				nfsva.na_mntonfileno = UINT64_MAX;
3040 				error = nfsv4_loadattr(nd, NULL, &nfsva, NULL,
3041 				    NULL, 0, NULL, NULL, NULL, NULL, NULL, 0,
3042 				    NULL, NULL, &rderr, p, cred);
3043 				if (error)
3044 					goto nfsmout;
3045 				NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3046 			} else if (nd->nd_flag & ND_NFSV3) {
3047 				NFSM_DISSECT(tl, u_int32_t *, 3*NFSX_UNSIGNED);
3048 				ncookie.lval[0] = *tl++;
3049 				ncookie.lval[1] = *tl++;
3050 			} else {
3051 				NFSM_DISSECT(tl, u_int32_t *, 2*NFSX_UNSIGNED);
3052 				ncookie.lval[0] = 0;
3053 				ncookie.lval[1] = *tl++;
3054 			}
3055 			if (bigenough) {
3056 			    if (nd->nd_flag & ND_NFSV4) {
3057 				if (rderr) {
3058 				    dp->d_fileno = 0;
3059 				} else {
3060 				    if (gotmnton) {
3061 					if (nfsva.na_mntonfileno != UINT64_MAX)
3062 					    dp->d_fileno = nfsva.na_mntonfileno;
3063 					else
3064 					    dp->d_fileno = nfsva.na_fileid;
3065 				    } else if (nfsva.na_filesid[0] ==
3066 					dnp->n_vattr.na_filesid[0] &&
3067 					nfsva.na_filesid[1] ==
3068 					dnp->n_vattr.na_filesid[1]) {
3069 					dp->d_fileno = nfsva.na_fileid;
3070 				    } else {
3071 					do {
3072 					    fakefileno--;
3073 					} while (fakefileno ==
3074 					    nfsva.na_fileid);
3075 					dp->d_fileno = fakefileno;
3076 				    }
3077 				    dp->d_type = vtonfs_dtype(nfsva.na_type);
3078 				}
3079 			    } else {
3080 				dp->d_fileno = nfsva.na_fileid;
3081 			    }
3082 			    *tl2++ = cookiep->nfsuquad[0] = cookie.lval[0] =
3083 				ncookie.lval[0];
3084 			    *tl2 = cookiep->nfsuquad[1] = cookie.lval[1] =
3085 				ncookie.lval[1];
3086 			}
3087 			more_dirs = fxdr_unsigned(int, *tl);
3088 		}
3089 		/*
3090 		 * If at end of rpc data, get the eof boolean
3091 		 */
3092 		if (!more_dirs) {
3093 			NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3094 			eof = fxdr_unsigned(int, *tl);
3095 			if (tryformoredirs)
3096 				more_dirs = !eof;
3097 			if (nd->nd_flag & ND_NFSV4) {
3098 				error = nfscl_postop_attr(nd, nap, attrflagp,
3099 				    stuff);
3100 				if (error)
3101 					goto nfsmout;
3102 			}
3103 		}
3104 		mbuf_freem(nd->nd_mrep);
3105 		nd->nd_mrep = NULL;
3106 	}
3107 	/*
3108 	 * Fill last record, iff any, out to a multiple of DIRBLKSIZ
3109 	 * by increasing d_reclen for the last record.
3110 	 */
3111 	if (blksiz > 0) {
3112 		left = DIRBLKSIZ - blksiz;
3113 		dp->d_reclen += left;
3114 		uio_iov_base_add(uiop, left);
3115 		uio_iov_len_add(uiop, -(left));
3116 		uio_uio_resid_add(uiop, -(left));
3117 		uiop->uio_offset += left;
3118 	}
3119 
3120 	/*
3121 	 * If returning no data, assume end of file.
3122 	 * If not bigenough, return not end of file, since you aren't
3123 	 *    returning all the data
3124 	 * Otherwise, return the eof flag from the server.
3125 	 */
3126 	if (eofp) {
3127 		if (tresid == ((size_t)(uio_uio_resid(uiop))))
3128 			*eofp = 1;
3129 		else if (!bigenough)
3130 			*eofp = 0;
3131 		else
3132 			*eofp = eof;
3133 	}
3134 
3135 	/*
3136 	 * Add extra empty records to any remaining DIRBLKSIZ chunks.
3137 	 */
3138 	while (uio_uio_resid(uiop) > 0 && uio_uio_resid(uiop) != tresid) {
3139 		dp = (struct dirent *)uio_iov_base(uiop);
3140 		dp->d_type = DT_UNKNOWN;
3141 		dp->d_fileno = 0;
3142 		dp->d_namlen = 0;
3143 		dp->d_name[0] = '\0';
3144 		tl = (u_int32_t *)&dp->d_name[4];
3145 		*tl++ = cookie.lval[0];
3146 		*tl = cookie.lval[1];
3147 		dp->d_reclen = DIRBLKSIZ;
3148 		uio_iov_base_add(uiop, DIRBLKSIZ);
3149 		uio_iov_len_add(uiop, -(DIRBLKSIZ));
3150 		uio_uio_resid_add(uiop, -(DIRBLKSIZ));
3151 		uiop->uio_offset += DIRBLKSIZ;
3152 	}
3153 
3154 nfsmout:
3155 	if (nd->nd_mrep != NULL)
3156 		mbuf_freem(nd->nd_mrep);
3157 	return (error);
3158 }
3159 
3160 #ifndef APPLE
3161 /*
3162  * NFS V3 readdir plus RPC. Used in place of nfsrpc_readdir().
3163  * (Also used for NFS V4 when mount flag set.)
3164  * (ditto above w.r.t. multiple of DIRBLKSIZ, etc.)
3165  */
3166 APPLESTATIC int
3167 nfsrpc_readdirplus(vnode_t vp, struct uio *uiop, nfsuint64 *cookiep,
3168     struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp,
3169     int *eofp, void *stuff)
3170 {
3171 	int len, left;
3172 	struct dirent *dp = NULL;
3173 	u_int32_t *tl;
3174 	vnode_t newvp = NULLVP;
3175 	struct nfsrv_descript nfsd, *nd = &nfsd;
3176 	struct nameidata nami, *ndp = &nami;
3177 	struct componentname *cnp = &ndp->ni_cnd;
3178 	struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
3179 	struct nfsnode *dnp = VTONFS(vp), *np;
3180 	struct nfsvattr nfsva;
3181 	struct nfsfh *nfhp;
3182 	nfsquad_t cookie, ncookie;
3183 	int error = 0, tlen, more_dirs = 1, blksiz = 0, bigenough = 1;
3184 	int attrflag, tryformoredirs = 1, eof = 0, gotmnton = 0;
3185 	int isdotdot = 0, unlocknewvp = 0;
3186 	u_int64_t dotfileid, dotdotfileid = 0, fakefileno = UINT64_MAX;
3187 	u_int64_t fileno = 0;
3188 	char *cp;
3189 	nfsattrbit_t attrbits, dattrbits;
3190 	size_t tresid;
3191 	u_int32_t *tl2 = NULL, rderr;
3192 	struct timespec dctime;
3193 
3194 	KASSERT(uiop->uio_iovcnt == 1 &&
3195 	    (uio_uio_resid(uiop) & (DIRBLKSIZ - 1)) == 0,
3196 	    ("nfs readdirplusrpc bad uio"));
3197 	timespecclear(&dctime);
3198 	*attrflagp = 0;
3199 	if (eofp != NULL)
3200 		*eofp = 0;
3201 	ndp->ni_dvp = vp;
3202 	nd->nd_mrep = NULL;
3203 	cookie.lval[0] = cookiep->nfsuquad[0];
3204 	cookie.lval[1] = cookiep->nfsuquad[1];
3205 	tresid = uio_uio_resid(uiop);
3206 
3207 	/*
3208 	 * For NFSv4, first create the "." and ".." entries.
3209 	 */
3210 	if (NFSHASNFSV4(nmp)) {
3211 		NFSGETATTR_ATTRBIT(&dattrbits);
3212 		NFSZERO_ATTRBIT(&attrbits);
3213 		NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_FILEID);
3214 		if (NFSISSET_ATTRBIT(&dnp->n_vattr.na_suppattr,
3215 		    NFSATTRBIT_MOUNTEDONFILEID)) {
3216 			NFSSETBIT_ATTRBIT(&attrbits,
3217 			    NFSATTRBIT_MOUNTEDONFILEID);
3218 			gotmnton = 1;
3219 		} else {
3220 			/*
3221 			 * Must fake it. Use the fileno, except when the
3222 			 * fsid is != to that of the directory. For that
3223 			 * case, generate a fake fileno that is not the same.
3224 			 */
3225 			NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_FSID);
3226 			gotmnton = 0;
3227 		}
3228 
3229 		/*
3230 		 * Joy, oh joy. For V4 we get to hand craft '.' and '..'.
3231 		 */
3232 		if (uiop->uio_offset == 0) {
3233 			NFSCL_REQSTART(nd, NFSPROC_LOOKUPP, vp);
3234 			NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
3235 			*tl++ = txdr_unsigned(NFSV4OP_GETFH);
3236 			*tl = txdr_unsigned(NFSV4OP_GETATTR);
3237 			(void) nfsrv_putattrbit(nd, &attrbits);
3238 			error = nfscl_request(nd, vp, p, cred, stuff);
3239 			if (error)
3240 			    return (error);
3241 			dotfileid = 0;	/* Fake out the compiler. */
3242 			if ((nd->nd_flag & ND_NOMOREDATA) == 0) {
3243 			    error = nfsm_loadattr(nd, &nfsva);
3244 			    if (error != 0)
3245 				goto nfsmout;
3246 			    dctime = nfsva.na_ctime;
3247 			    dotfileid = nfsva.na_fileid;
3248 			}
3249 			if (nd->nd_repstat == 0) {
3250 			    NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
3251 			    len = fxdr_unsigned(int, *(tl + 4));
3252 			    if (len > 0 && len <= NFSX_V4FHMAX)
3253 				error = nfsm_advance(nd, NFSM_RNDUP(len), -1);
3254 			    else
3255 				error = EPERM;
3256 			    if (!error) {
3257 				NFSM_DISSECT(tl, u_int32_t *, 2*NFSX_UNSIGNED);
3258 				nfsva.na_mntonfileno = UINT64_MAX;
3259 				error = nfsv4_loadattr(nd, NULL, &nfsva, NULL,
3260 				    NULL, 0, NULL, NULL, NULL, NULL, NULL, 0,
3261 				    NULL, NULL, NULL, p, cred);
3262 				if (error) {
3263 				    dotdotfileid = dotfileid;
3264 				} else if (gotmnton) {
3265 				    if (nfsva.na_mntonfileno != UINT64_MAX)
3266 					dotdotfileid = nfsva.na_mntonfileno;
3267 				    else
3268 					dotdotfileid = nfsva.na_fileid;
3269 				} else if (nfsva.na_filesid[0] ==
3270 				    dnp->n_vattr.na_filesid[0] &&
3271 				    nfsva.na_filesid[1] ==
3272 				    dnp->n_vattr.na_filesid[1]) {
3273 				    dotdotfileid = nfsva.na_fileid;
3274 				} else {
3275 				    do {
3276 					fakefileno--;
3277 				    } while (fakefileno ==
3278 					nfsva.na_fileid);
3279 				    dotdotfileid = fakefileno;
3280 				}
3281 			    }
3282 			} else if (nd->nd_repstat == NFSERR_NOENT) {
3283 			    /*
3284 			     * Lookupp returns NFSERR_NOENT when we are
3285 			     * at the root, so just use the current dir.
3286 			     */
3287 			    nd->nd_repstat = 0;
3288 			    dotdotfileid = dotfileid;
3289 			} else {
3290 			    error = nd->nd_repstat;
3291 			}
3292 			mbuf_freem(nd->nd_mrep);
3293 			if (error)
3294 			    return (error);
3295 			nd->nd_mrep = NULL;
3296 			dp = (struct dirent *)uio_iov_base(uiop);
3297 			dp->d_off = 0;
3298 			dp->d_type = DT_DIR;
3299 			dp->d_fileno = dotfileid;
3300 			dp->d_namlen = 1;
3301 			*((uint64_t *)dp->d_name) = 0;	/* Zero pad it. */
3302 			dp->d_name[0] = '.';
3303 			dp->d_reclen = _GENERIC_DIRSIZ(dp) + NFSX_HYPER;
3304 			/*
3305 			 * Just make these offset cookie 0.
3306 			 */
3307 			tl = (u_int32_t *)&dp->d_name[8];
3308 			*tl++ = 0;
3309 			*tl = 0;
3310 			blksiz += dp->d_reclen;
3311 			uio_uio_resid_add(uiop, -(dp->d_reclen));
3312 			uiop->uio_offset += dp->d_reclen;
3313 			uio_iov_base_add(uiop, dp->d_reclen);
3314 			uio_iov_len_add(uiop, -(dp->d_reclen));
3315 			dp = (struct dirent *)uio_iov_base(uiop);
3316 			dp->d_off = 0;
3317 			dp->d_type = DT_DIR;
3318 			dp->d_fileno = dotdotfileid;
3319 			dp->d_namlen = 2;
3320 			*((uint64_t *)dp->d_name) = 0;
3321 			dp->d_name[0] = '.';
3322 			dp->d_name[1] = '.';
3323 			dp->d_reclen = _GENERIC_DIRSIZ(dp) + NFSX_HYPER;
3324 			/*
3325 			 * Just make these offset cookie 0.
3326 			 */
3327 			tl = (u_int32_t *)&dp->d_name[8];
3328 			*tl++ = 0;
3329 			*tl = 0;
3330 			blksiz += dp->d_reclen;
3331 			uio_uio_resid_add(uiop, -(dp->d_reclen));
3332 			uiop->uio_offset += dp->d_reclen;
3333 			uio_iov_base_add(uiop, dp->d_reclen);
3334 			uio_iov_len_add(uiop, -(dp->d_reclen));
3335 		}
3336 		NFSREADDIRPLUS_ATTRBIT(&attrbits);
3337 		if (gotmnton)
3338 			NFSSETBIT_ATTRBIT(&attrbits,
3339 			    NFSATTRBIT_MOUNTEDONFILEID);
3340 	}
3341 
3342 	/*
3343 	 * Loop around doing readdir rpc's of size nm_readdirsize.
3344 	 * The stopping criteria is EOF or buffer full.
3345 	 */
3346 	while (more_dirs && bigenough) {
3347 		*attrflagp = 0;
3348 		NFSCL_REQSTART(nd, NFSPROC_READDIRPLUS, vp);
3349  		NFSM_BUILD(tl, u_int32_t *, 6 * NFSX_UNSIGNED);
3350 		*tl++ = cookie.lval[0];
3351 		*tl++ = cookie.lval[1];
3352 		if (cookie.qval == 0) {
3353 			*tl++ = 0;
3354 			*tl++ = 0;
3355 		} else {
3356 			NFSLOCKNODE(dnp);
3357 			*tl++ = dnp->n_cookieverf.nfsuquad[0];
3358 			*tl++ = dnp->n_cookieverf.nfsuquad[1];
3359 			NFSUNLOCKNODE(dnp);
3360 		}
3361 		*tl++ = txdr_unsigned(nmp->nm_readdirsize);
3362 		*tl = txdr_unsigned(nmp->nm_readdirsize);
3363 		if (nd->nd_flag & ND_NFSV4) {
3364 			(void) nfsrv_putattrbit(nd, &attrbits);
3365 			NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
3366 			*tl = txdr_unsigned(NFSV4OP_GETATTR);
3367 			(void) nfsrv_putattrbit(nd, &dattrbits);
3368 		}
3369 		error = nfscl_request(nd, vp, p, cred, stuff);
3370 		if (error)
3371 			return (error);
3372 		if (nd->nd_flag & ND_NFSV3)
3373 			error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
3374 		if (nd->nd_repstat || error) {
3375 			if (!error)
3376 				error = nd->nd_repstat;
3377 			goto nfsmout;
3378 		}
3379 		if ((nd->nd_flag & ND_NFSV3) != 0 && *attrflagp != 0)
3380 			dctime = nap->na_ctime;
3381 		NFSM_DISSECT(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
3382 		NFSLOCKNODE(dnp);
3383 		dnp->n_cookieverf.nfsuquad[0] = *tl++;
3384 		dnp->n_cookieverf.nfsuquad[1] = *tl++;
3385 		NFSUNLOCKNODE(dnp);
3386 		more_dirs = fxdr_unsigned(int, *tl);
3387 		if (!more_dirs)
3388 			tryformoredirs = 0;
3389 
3390 		/* loop through the dir entries, doctoring them to 4bsd form */
3391 		while (more_dirs && bigenough) {
3392 			NFSM_DISSECT(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
3393 			if (nd->nd_flag & ND_NFSV4) {
3394 				ncookie.lval[0] = *tl++;
3395 				ncookie.lval[1] = *tl++;
3396 			} else {
3397 				fileno = fxdr_hyper(tl);
3398 				tl += 2;
3399 			}
3400 			len = fxdr_unsigned(int, *tl);
3401 			if (len <= 0 || len > NFS_MAXNAMLEN) {
3402 				error = EBADRPC;
3403 				goto nfsmout;
3404 			}
3405 			tlen = roundup2(len, 8);
3406 			if (tlen == len)
3407 				tlen += 8;  /* To ensure null termination. */
3408 			left = DIRBLKSIZ - blksiz;
3409 			if (_GENERIC_DIRLEN(len) + NFSX_HYPER > left) {
3410 				dp->d_reclen += left;
3411 				uio_iov_base_add(uiop, left);
3412 				uio_iov_len_add(uiop, -(left));
3413 				uio_uio_resid_add(uiop, -(left));
3414 				uiop->uio_offset += left;
3415 				blksiz = 0;
3416 			}
3417 			if (_GENERIC_DIRLEN(len) + NFSX_HYPER >
3418 			    uio_uio_resid(uiop))
3419 				bigenough = 0;
3420 			if (bigenough) {
3421 				dp = (struct dirent *)uio_iov_base(uiop);
3422 				dp->d_off = 0;
3423 				dp->d_namlen = len;
3424 				dp->d_reclen = _GENERIC_DIRLEN(len) +
3425 				    NFSX_HYPER;
3426 				dp->d_type = DT_UNKNOWN;
3427 				blksiz += dp->d_reclen;
3428 				if (blksiz == DIRBLKSIZ)
3429 					blksiz = 0;
3430 				uio_uio_resid_add(uiop, -(DIRHDSIZ));
3431 				uiop->uio_offset += DIRHDSIZ;
3432 				uio_iov_base_add(uiop, DIRHDSIZ);
3433 				uio_iov_len_add(uiop, -(DIRHDSIZ));
3434 				cnp->cn_nameptr = uio_iov_base(uiop);
3435 				cnp->cn_namelen = len;
3436 				NFSCNHASHZERO(cnp);
3437 				error = nfsm_mbufuio(nd, uiop, len);
3438 				if (error)
3439 					goto nfsmout;
3440 				cp = uio_iov_base(uiop);
3441 				tlen -= len;
3442 				*cp = '\0';
3443 				cp += tlen;	/* points to cookie storage */
3444 				tl2 = (u_int32_t *)cp;
3445 				if (len == 2 && cnp->cn_nameptr[0] == '.' &&
3446 				    cnp->cn_nameptr[1] == '.')
3447 					isdotdot = 1;
3448 				else
3449 					isdotdot = 0;
3450 				uio_iov_base_add(uiop, (tlen + NFSX_HYPER));
3451 				uio_iov_len_add(uiop, -(tlen + NFSX_HYPER));
3452 				uio_uio_resid_add(uiop, -(tlen + NFSX_HYPER));
3453 				uiop->uio_offset += (tlen + NFSX_HYPER);
3454 			} else {
3455 				error = nfsm_advance(nd, NFSM_RNDUP(len), -1);
3456 				if (error)
3457 					goto nfsmout;
3458 			}
3459 			nfhp = NULL;
3460 			if (nd->nd_flag & ND_NFSV3) {
3461 				NFSM_DISSECT(tl, u_int32_t *, 3*NFSX_UNSIGNED);
3462 				ncookie.lval[0] = *tl++;
3463 				ncookie.lval[1] = *tl++;
3464 				attrflag = fxdr_unsigned(int, *tl);
3465 				if (attrflag) {
3466 				  error = nfsm_loadattr(nd, &nfsva);
3467 				  if (error)
3468 					goto nfsmout;
3469 				}
3470 				NFSM_DISSECT(tl,u_int32_t *,NFSX_UNSIGNED);
3471 				if (*tl) {
3472 					error = nfsm_getfh(nd, &nfhp);
3473 					if (error)
3474 					    goto nfsmout;
3475 				}
3476 				if (!attrflag && nfhp != NULL) {
3477 					FREE((caddr_t)nfhp, M_NFSFH);
3478 					nfhp = NULL;
3479 				}
3480 			} else {
3481 				rderr = 0;
3482 				nfsva.na_mntonfileno = 0xffffffff;
3483 				error = nfsv4_loadattr(nd, NULL, &nfsva, &nfhp,
3484 				    NULL, 0, NULL, NULL, NULL, NULL, NULL, 0,
3485 				    NULL, NULL, &rderr, p, cred);
3486 				if (error)
3487 					goto nfsmout;
3488 			}
3489 
3490 			if (bigenough) {
3491 			    if (nd->nd_flag & ND_NFSV4) {
3492 				if (rderr) {
3493 				    dp->d_fileno = 0;
3494 				} else if (gotmnton) {
3495 				    if (nfsva.na_mntonfileno != 0xffffffff)
3496 					dp->d_fileno = nfsva.na_mntonfileno;
3497 				    else
3498 					dp->d_fileno = nfsva.na_fileid;
3499 				} else if (nfsva.na_filesid[0] ==
3500 				    dnp->n_vattr.na_filesid[0] &&
3501 				    nfsva.na_filesid[1] ==
3502 				    dnp->n_vattr.na_filesid[1]) {
3503 				    dp->d_fileno = nfsva.na_fileid;
3504 				} else {
3505 				    do {
3506 					fakefileno--;
3507 				    } while (fakefileno ==
3508 					nfsva.na_fileid);
3509 				    dp->d_fileno = fakefileno;
3510 				}
3511 			    } else {
3512 				dp->d_fileno = fileno;
3513 			    }
3514 			    *tl2++ = cookiep->nfsuquad[0] = cookie.lval[0] =
3515 				ncookie.lval[0];
3516 			    *tl2 = cookiep->nfsuquad[1] = cookie.lval[1] =
3517 				ncookie.lval[1];
3518 
3519 			    if (nfhp != NULL) {
3520 				if (NFSRV_CMPFH(nfhp->nfh_fh, nfhp->nfh_len,
3521 				    dnp->n_fhp->nfh_fh, dnp->n_fhp->nfh_len)) {
3522 				    VREF(vp);
3523 				    newvp = vp;
3524 				    unlocknewvp = 0;
3525 				    FREE((caddr_t)nfhp, M_NFSFH);
3526 				    np = dnp;
3527 				} else if (isdotdot != 0) {
3528 				    /*
3529 				     * Skip doing a nfscl_nget() call for "..".
3530 				     * There's a race between acquiring the nfs
3531 				     * node here and lookups that look for the
3532 				     * directory being read (in the parent).
3533 				     * It would try to get a lock on ".." here,
3534 				     * owning the lock on the directory being
3535 				     * read. Lookup will hold the lock on ".."
3536 				     * and try to acquire the lock on the
3537 				     * directory being read.
3538 				     * If the directory is unlocked/relocked,
3539 				     * then there is a LOR with the buflock
3540 				     * vp is relocked.
3541 				     */
3542 				    free(nfhp, M_NFSFH);
3543 				} else {
3544 				    error = nfscl_nget(vnode_mount(vp), vp,
3545 				      nfhp, cnp, p, &np, NULL, LK_EXCLUSIVE);
3546 				    if (!error) {
3547 					newvp = NFSTOV(np);
3548 					unlocknewvp = 1;
3549 				    }
3550 				}
3551 				nfhp = NULL;
3552 				if (newvp != NULLVP) {
3553 				    error = nfscl_loadattrcache(&newvp,
3554 					&nfsva, NULL, NULL, 0, 0);
3555 				    if (error) {
3556 					if (unlocknewvp)
3557 					    vput(newvp);
3558 					else
3559 					    vrele(newvp);
3560 					goto nfsmout;
3561 				    }
3562 				    dp->d_type =
3563 					vtonfs_dtype(np->n_vattr.na_type);
3564 				    ndp->ni_vp = newvp;
3565 				    NFSCNHASH(cnp, HASHINIT);
3566 				    if (cnp->cn_namelen <= NCHNAMLEN &&
3567 					(newvp->v_type != VDIR ||
3568 					 dctime.tv_sec != 0)) {
3569 					cache_enter_time(ndp->ni_dvp,
3570 					    ndp->ni_vp, cnp,
3571 					    &nfsva.na_ctime,
3572 					    newvp->v_type != VDIR ? NULL :
3573 					    &dctime);
3574 				    }
3575 				    if (unlocknewvp)
3576 					vput(newvp);
3577 				    else
3578 					vrele(newvp);
3579 				    newvp = NULLVP;
3580 				}
3581 			    }
3582 			} else if (nfhp != NULL) {
3583 			    FREE((caddr_t)nfhp, M_NFSFH);
3584 			}
3585 			NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3586 			more_dirs = fxdr_unsigned(int, *tl);
3587 		}
3588 		/*
3589 		 * If at end of rpc data, get the eof boolean
3590 		 */
3591 		if (!more_dirs) {
3592 			NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3593 			eof = fxdr_unsigned(int, *tl);
3594 			if (tryformoredirs)
3595 				more_dirs = !eof;
3596 			if (nd->nd_flag & ND_NFSV4) {
3597 				error = nfscl_postop_attr(nd, nap, attrflagp,
3598 				    stuff);
3599 				if (error)
3600 					goto nfsmout;
3601 			}
3602 		}
3603 		mbuf_freem(nd->nd_mrep);
3604 		nd->nd_mrep = NULL;
3605 	}
3606 	/*
3607 	 * Fill last record, iff any, out to a multiple of DIRBLKSIZ
3608 	 * by increasing d_reclen for the last record.
3609 	 */
3610 	if (blksiz > 0) {
3611 		left = DIRBLKSIZ - blksiz;
3612 		dp->d_reclen += left;
3613 		uio_iov_base_add(uiop, left);
3614 		uio_iov_len_add(uiop, -(left));
3615 		uio_uio_resid_add(uiop, -(left));
3616 		uiop->uio_offset += left;
3617 	}
3618 
3619 	/*
3620 	 * If returning no data, assume end of file.
3621 	 * If not bigenough, return not end of file, since you aren't
3622 	 *    returning all the data
3623 	 * Otherwise, return the eof flag from the server.
3624 	 */
3625 	if (eofp != NULL) {
3626 		if (tresid == uio_uio_resid(uiop))
3627 			*eofp = 1;
3628 		else if (!bigenough)
3629 			*eofp = 0;
3630 		else
3631 			*eofp = eof;
3632 	}
3633 
3634 	/*
3635 	 * Add extra empty records to any remaining DIRBLKSIZ chunks.
3636 	 */
3637 	while (uio_uio_resid(uiop) > 0 && uio_uio_resid(uiop) != tresid) {
3638 		dp = (struct dirent *)uio_iov_base(uiop);
3639 		dp->d_type = DT_UNKNOWN;
3640 		dp->d_fileno = 0;
3641 		dp->d_namlen = 0;
3642 		dp->d_name[0] = '\0';
3643 		tl = (u_int32_t *)&dp->d_name[4];
3644 		*tl++ = cookie.lval[0];
3645 		*tl = cookie.lval[1];
3646 		dp->d_reclen = DIRBLKSIZ;
3647 		uio_iov_base_add(uiop, DIRBLKSIZ);
3648 		uio_iov_len_add(uiop, -(DIRBLKSIZ));
3649 		uio_uio_resid_add(uiop, -(DIRBLKSIZ));
3650 		uiop->uio_offset += DIRBLKSIZ;
3651 	}
3652 
3653 nfsmout:
3654 	if (nd->nd_mrep != NULL)
3655 		mbuf_freem(nd->nd_mrep);
3656 	return (error);
3657 }
3658 #endif	/* !APPLE */
3659 
3660 /*
3661  * Nfs commit rpc
3662  */
3663 APPLESTATIC int
3664 nfsrpc_commit(vnode_t vp, u_quad_t offset, int cnt, struct ucred *cred,
3665     NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, void *stuff)
3666 {
3667 	u_int32_t *tl;
3668 	struct nfsrv_descript nfsd, *nd = &nfsd;
3669 	nfsattrbit_t attrbits;
3670 	int error;
3671 	struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
3672 
3673 	*attrflagp = 0;
3674 	NFSCL_REQSTART(nd, NFSPROC_COMMIT, vp);
3675 	NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
3676 	txdr_hyper(offset, tl);
3677 	tl += 2;
3678 	*tl = txdr_unsigned(cnt);
3679 	if (nd->nd_flag & ND_NFSV4) {
3680 		/*
3681 		 * And do a Getattr op.
3682 		 */
3683 		NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
3684 		*tl = txdr_unsigned(NFSV4OP_GETATTR);
3685 		NFSGETATTR_ATTRBIT(&attrbits);
3686 		(void) nfsrv_putattrbit(nd, &attrbits);
3687 	}
3688 	error = nfscl_request(nd, vp, p, cred, stuff);
3689 	if (error)
3690 		return (error);
3691 	error = nfscl_wcc_data(nd, vp, nap, attrflagp, NULL, stuff);
3692 	if (!error && !nd->nd_repstat) {
3693 		NFSM_DISSECT(tl, u_int32_t *, NFSX_VERF);
3694 		NFSLOCKMNT(nmp);
3695 		if (NFSBCMP(nmp->nm_verf, tl, NFSX_VERF)) {
3696 			NFSBCOPY(tl, nmp->nm_verf, NFSX_VERF);
3697 			nd->nd_repstat = NFSERR_STALEWRITEVERF;
3698 		}
3699 		NFSUNLOCKMNT(nmp);
3700 		if (nd->nd_flag & ND_NFSV4)
3701 			error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
3702 	}
3703 nfsmout:
3704 	if (!error && nd->nd_repstat)
3705 		error = nd->nd_repstat;
3706 	mbuf_freem(nd->nd_mrep);
3707 	return (error);
3708 }
3709 
3710 /*
3711  * NFS byte range lock rpc.
3712  * (Mostly just calls one of the three lower level RPC routines.)
3713  */
3714 APPLESTATIC int
3715 nfsrpc_advlock(vnode_t vp, off_t size, int op, struct flock *fl,
3716     int reclaim, struct ucred *cred, NFSPROC_T *p, void *id, int flags)
3717 {
3718 	struct nfscllockowner *lp;
3719 	struct nfsclclient *clp;
3720 	struct nfsfh *nfhp;
3721 	struct nfsrv_descript nfsd, *nd = &nfsd;
3722 	struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
3723 	u_int64_t off, len;
3724 	off_t start, end;
3725 	u_int32_t clidrev = 0;
3726 	int error = 0, newone = 0, expireret = 0, retrycnt, donelocally;
3727 	int callcnt, dorpc;
3728 
3729 	/*
3730 	 * Convert the flock structure into a start and end and do POSIX
3731 	 * bounds checking.
3732 	 */
3733 	switch (fl->l_whence) {
3734 	case SEEK_SET:
3735 	case SEEK_CUR:
3736 		/*
3737 		 * Caller is responsible for adding any necessary offset
3738 		 * when SEEK_CUR is used.
3739 		 */
3740 		start = fl->l_start;
3741 		off = fl->l_start;
3742 		break;
3743 	case SEEK_END:
3744 		start = size + fl->l_start;
3745 		off = size + fl->l_start;
3746 		break;
3747 	default:
3748 		return (EINVAL);
3749 	}
3750 	if (start < 0)
3751 		return (EINVAL);
3752 	if (fl->l_len != 0) {
3753 		end = start + fl->l_len - 1;
3754 		if (end < start)
3755 			return (EINVAL);
3756 	}
3757 
3758 	len = fl->l_len;
3759 	if (len == 0)
3760 		len = NFS64BITSSET;
3761 	retrycnt = 0;
3762 	do {
3763 	    nd->nd_repstat = 0;
3764 	    if (op == F_GETLK) {
3765 		error = nfscl_getcl(vnode_mount(vp), cred, p, 1, &clp);
3766 		if (error)
3767 			return (error);
3768 		error = nfscl_lockt(vp, clp, off, len, fl, p, id, flags);
3769 		if (!error) {
3770 			clidrev = clp->nfsc_clientidrev;
3771 			error = nfsrpc_lockt(nd, vp, clp, off, len, fl, cred,
3772 			    p, id, flags);
3773 		} else if (error == -1) {
3774 			error = 0;
3775 		}
3776 		nfscl_clientrelease(clp);
3777 	    } else if (op == F_UNLCK && fl->l_type == F_UNLCK) {
3778 		/*
3779 		 * We must loop around for all lockowner cases.
3780 		 */
3781 		callcnt = 0;
3782 		error = nfscl_getcl(vnode_mount(vp), cred, p, 1, &clp);
3783 		if (error)
3784 			return (error);
3785 		do {
3786 		    error = nfscl_relbytelock(vp, off, len, cred, p, callcnt,
3787 			clp, id, flags, &lp, &dorpc);
3788 		    /*
3789 		     * If it returns a NULL lp, we're done.
3790 		     */
3791 		    if (lp == NULL) {
3792 			if (callcnt == 0)
3793 			    nfscl_clientrelease(clp);
3794 			else
3795 			    nfscl_releasealllocks(clp, vp, p, id, flags);
3796 			return (error);
3797 		    }
3798 		    if (nmp->nm_clp != NULL)
3799 			clidrev = nmp->nm_clp->nfsc_clientidrev;
3800 		    else
3801 			clidrev = 0;
3802 		    /*
3803 		     * If the server doesn't support Posix lock semantics,
3804 		     * only allow locks on the entire file, since it won't
3805 		     * handle overlapping byte ranges.
3806 		     * There might still be a problem when a lock
3807 		     * upgrade/downgrade (read<->write) occurs, since the
3808 		     * server "might" expect an unlock first?
3809 		     */
3810 		    if (dorpc && (lp->nfsl_open->nfso_posixlock ||
3811 			(off == 0 && len == NFS64BITSSET))) {
3812 			/*
3813 			 * Since the lock records will go away, we must
3814 			 * wait for grace and delay here.
3815 			 */
3816 			do {
3817 			    error = nfsrpc_locku(nd, nmp, lp, off, len,
3818 				NFSV4LOCKT_READ, cred, p, 0);
3819 			    if ((nd->nd_repstat == NFSERR_GRACE ||
3820 				 nd->nd_repstat == NFSERR_DELAY) &&
3821 				error == 0)
3822 				(void) nfs_catnap(PZERO, (int)nd->nd_repstat,
3823 				    "nfs_advlock");
3824 			} while ((nd->nd_repstat == NFSERR_GRACE ||
3825 			    nd->nd_repstat == NFSERR_DELAY) && error == 0);
3826 		    }
3827 		    callcnt++;
3828 		} while (error == 0 && nd->nd_repstat == 0);
3829 		nfscl_releasealllocks(clp, vp, p, id, flags);
3830 	    } else if (op == F_SETLK) {
3831 		error = nfscl_getbytelock(vp, off, len, fl->l_type, cred, p,
3832 		    NULL, 0, id, flags, NULL, NULL, &lp, &newone, &donelocally);
3833 		if (error || donelocally) {
3834 			return (error);
3835 		}
3836 		if (nmp->nm_clp != NULL)
3837 			clidrev = nmp->nm_clp->nfsc_clientidrev;
3838 		else
3839 			clidrev = 0;
3840 		nfhp = VTONFS(vp)->n_fhp;
3841 		if (!lp->nfsl_open->nfso_posixlock &&
3842 		    (off != 0 || len != NFS64BITSSET)) {
3843 			error = EINVAL;
3844 		} else {
3845 			error = nfsrpc_lock(nd, nmp, vp, nfhp->nfh_fh,
3846 			    nfhp->nfh_len, lp, newone, reclaim, off,
3847 			    len, fl->l_type, cred, p, 0);
3848 		}
3849 		if (!error)
3850 			error = nd->nd_repstat;
3851 		nfscl_lockrelease(lp, error, newone);
3852 	    } else {
3853 		error = EINVAL;
3854 	    }
3855 	    if (!error)
3856 	        error = nd->nd_repstat;
3857 	    if (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
3858 		error == NFSERR_STALEDONTRECOVER ||
3859 		error == NFSERR_STALECLIENTID || error == NFSERR_DELAY ||
3860 		error == NFSERR_BADSESSION) {
3861 		(void) nfs_catnap(PZERO, error, "nfs_advlock");
3862 	    } else if ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID)
3863 		&& clidrev != 0) {
3864 		expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
3865 		retrycnt++;
3866 	    }
3867 	} while (error == NFSERR_GRACE ||
3868 	    error == NFSERR_STALECLIENTID || error == NFSERR_DELAY ||
3869 	    error == NFSERR_STALEDONTRECOVER || error == NFSERR_STALESTATEID ||
3870 	    error == NFSERR_BADSESSION ||
3871 	    ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
3872 	     expireret == 0 && clidrev != 0 && retrycnt < 4));
3873 	if (error && retrycnt >= 4)
3874 		error = EIO;
3875 	return (error);
3876 }
3877 
3878 /*
3879  * The lower level routine for the LockT case.
3880  */
3881 APPLESTATIC int
3882 nfsrpc_lockt(struct nfsrv_descript *nd, vnode_t vp,
3883     struct nfsclclient *clp, u_int64_t off, u_int64_t len, struct flock *fl,
3884     struct ucred *cred, NFSPROC_T *p, void *id, int flags)
3885 {
3886 	u_int32_t *tl;
3887 	int error, type, size;
3888 	uint8_t own[NFSV4CL_LOCKNAMELEN + NFSX_V4FHMAX];
3889 	struct nfsnode *np;
3890 	struct nfsmount *nmp;
3891 	struct nfsclsession *tsep;
3892 
3893 	nmp = VFSTONFS(vp->v_mount);
3894 	NFSCL_REQSTART(nd, NFSPROC_LOCKT, vp);
3895 	NFSM_BUILD(tl, u_int32_t *, 7 * NFSX_UNSIGNED);
3896 	if (fl->l_type == F_RDLCK)
3897 		*tl++ = txdr_unsigned(NFSV4LOCKT_READ);
3898 	else
3899 		*tl++ = txdr_unsigned(NFSV4LOCKT_WRITE);
3900 	txdr_hyper(off, tl);
3901 	tl += 2;
3902 	txdr_hyper(len, tl);
3903 	tl += 2;
3904 	tsep = nfsmnt_mdssession(nmp);
3905 	*tl++ = tsep->nfsess_clientid.lval[0];
3906 	*tl = tsep->nfsess_clientid.lval[1];
3907 	nfscl_filllockowner(id, own, flags);
3908 	np = VTONFS(vp);
3909 	NFSBCOPY(np->n_fhp->nfh_fh, &own[NFSV4CL_LOCKNAMELEN],
3910 	    np->n_fhp->nfh_len);
3911 	(void)nfsm_strtom(nd, own, NFSV4CL_LOCKNAMELEN + np->n_fhp->nfh_len);
3912 	error = nfscl_request(nd, vp, p, cred, NULL);
3913 	if (error)
3914 		return (error);
3915 	if (nd->nd_repstat == 0) {
3916 		fl->l_type = F_UNLCK;
3917 	} else if (nd->nd_repstat == NFSERR_DENIED) {
3918 		nd->nd_repstat = 0;
3919 		fl->l_whence = SEEK_SET;
3920 		NFSM_DISSECT(tl, u_int32_t *, 8 * NFSX_UNSIGNED);
3921 		fl->l_start = fxdr_hyper(tl);
3922 		tl += 2;
3923 		len = fxdr_hyper(tl);
3924 		tl += 2;
3925 		if (len == NFS64BITSSET)
3926 			fl->l_len = 0;
3927 		else
3928 			fl->l_len = len;
3929 		type = fxdr_unsigned(int, *tl++);
3930 		if (type == NFSV4LOCKT_WRITE)
3931 			fl->l_type = F_WRLCK;
3932 		else
3933 			fl->l_type = F_RDLCK;
3934 		/*
3935 		 * XXX For now, I have no idea what to do with the
3936 		 * conflicting lock_owner, so I'll just set the pid == 0
3937 		 * and skip over the lock_owner.
3938 		 */
3939 		fl->l_pid = (pid_t)0;
3940 		tl += 2;
3941 		size = fxdr_unsigned(int, *tl);
3942 		if (size < 0 || size > NFSV4_OPAQUELIMIT)
3943 			error = EBADRPC;
3944 		if (!error)
3945 			error = nfsm_advance(nd, NFSM_RNDUP(size), -1);
3946 	} else if (nd->nd_repstat == NFSERR_STALECLIENTID)
3947 		nfscl_initiate_recovery(clp);
3948 nfsmout:
3949 	mbuf_freem(nd->nd_mrep);
3950 	return (error);
3951 }
3952 
3953 /*
3954  * Lower level function that performs the LockU RPC.
3955  */
3956 static int
3957 nfsrpc_locku(struct nfsrv_descript *nd, struct nfsmount *nmp,
3958     struct nfscllockowner *lp, u_int64_t off, u_int64_t len,
3959     u_int32_t type, struct ucred *cred, NFSPROC_T *p, int syscred)
3960 {
3961 	u_int32_t *tl;
3962 	int error;
3963 
3964 	nfscl_reqstart(nd, NFSPROC_LOCKU, nmp, lp->nfsl_open->nfso_fh,
3965 	    lp->nfsl_open->nfso_fhlen, NULL, NULL);
3966 	NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID + 6 * NFSX_UNSIGNED);
3967 	*tl++ = txdr_unsigned(type);
3968 	*tl = txdr_unsigned(lp->nfsl_seqid);
3969 	if (nfstest_outofseq &&
3970 	    (arc4random() % nfstest_outofseq) == 0)
3971 		*tl = txdr_unsigned(lp->nfsl_seqid + 1);
3972 	tl++;
3973 	if (NFSHASNFSV4N(nmp))
3974 		*tl++ = 0;
3975 	else
3976 		*tl++ = lp->nfsl_stateid.seqid;
3977 	*tl++ = lp->nfsl_stateid.other[0];
3978 	*tl++ = lp->nfsl_stateid.other[1];
3979 	*tl++ = lp->nfsl_stateid.other[2];
3980 	txdr_hyper(off, tl);
3981 	tl += 2;
3982 	txdr_hyper(len, tl);
3983 	if (syscred)
3984 		nd->nd_flag |= ND_USEGSSNAME;
3985 	error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
3986 	    NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
3987 	NFSCL_INCRSEQID(lp->nfsl_seqid, nd);
3988 	if (error)
3989 		return (error);
3990 	if (nd->nd_repstat == 0) {
3991 		NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID);
3992 		lp->nfsl_stateid.seqid = *tl++;
3993 		lp->nfsl_stateid.other[0] = *tl++;
3994 		lp->nfsl_stateid.other[1] = *tl++;
3995 		lp->nfsl_stateid.other[2] = *tl;
3996 	} else if (nd->nd_repstat == NFSERR_STALESTATEID)
3997 		nfscl_initiate_recovery(lp->nfsl_open->nfso_own->nfsow_clp);
3998 nfsmout:
3999 	mbuf_freem(nd->nd_mrep);
4000 	return (error);
4001 }
4002 
4003 /*
4004  * The actual Lock RPC.
4005  */
4006 APPLESTATIC int
4007 nfsrpc_lock(struct nfsrv_descript *nd, struct nfsmount *nmp, vnode_t vp,
4008     u_int8_t *nfhp, int fhlen, struct nfscllockowner *lp, int newone,
4009     int reclaim, u_int64_t off, u_int64_t len, short type, struct ucred *cred,
4010     NFSPROC_T *p, int syscred)
4011 {
4012 	u_int32_t *tl;
4013 	int error, size;
4014 	uint8_t own[NFSV4CL_LOCKNAMELEN + NFSX_V4FHMAX];
4015 	struct nfsclsession *tsep;
4016 
4017 	nfscl_reqstart(nd, NFSPROC_LOCK, nmp, nfhp, fhlen, NULL, NULL);
4018 	NFSM_BUILD(tl, u_int32_t *, 7 * NFSX_UNSIGNED);
4019 	if (type == F_RDLCK)
4020 		*tl++ = txdr_unsigned(NFSV4LOCKT_READ);
4021 	else
4022 		*tl++ = txdr_unsigned(NFSV4LOCKT_WRITE);
4023 	*tl++ = txdr_unsigned(reclaim);
4024 	txdr_hyper(off, tl);
4025 	tl += 2;
4026 	txdr_hyper(len, tl);
4027 	tl += 2;
4028 	if (newone) {
4029 	    *tl = newnfs_true;
4030 	    NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID +
4031 		2 * NFSX_UNSIGNED + NFSX_HYPER);
4032 	    *tl++ = txdr_unsigned(lp->nfsl_open->nfso_own->nfsow_seqid);
4033 	    if (NFSHASNFSV4N(nmp))
4034 		*tl++ = 0;
4035 	    else
4036 		*tl++ = lp->nfsl_open->nfso_stateid.seqid;
4037 	    *tl++ = lp->nfsl_open->nfso_stateid.other[0];
4038 	    *tl++ = lp->nfsl_open->nfso_stateid.other[1];
4039 	    *tl++ = lp->nfsl_open->nfso_stateid.other[2];
4040 	    *tl++ = txdr_unsigned(lp->nfsl_seqid);
4041 	    tsep = nfsmnt_mdssession(nmp);
4042 	    *tl++ = tsep->nfsess_clientid.lval[0];
4043 	    *tl = tsep->nfsess_clientid.lval[1];
4044 	    NFSBCOPY(lp->nfsl_owner, own, NFSV4CL_LOCKNAMELEN);
4045 	    NFSBCOPY(nfhp, &own[NFSV4CL_LOCKNAMELEN], fhlen);
4046 	    (void)nfsm_strtom(nd, own, NFSV4CL_LOCKNAMELEN + fhlen);
4047 	} else {
4048 	    *tl = newnfs_false;
4049 	    NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID + NFSX_UNSIGNED);
4050 	    if (NFSHASNFSV4N(nmp))
4051 		*tl++ = 0;
4052 	    else
4053 		*tl++ = lp->nfsl_stateid.seqid;
4054 	    *tl++ = lp->nfsl_stateid.other[0];
4055 	    *tl++ = lp->nfsl_stateid.other[1];
4056 	    *tl++ = lp->nfsl_stateid.other[2];
4057 	    *tl = txdr_unsigned(lp->nfsl_seqid);
4058 	    if (nfstest_outofseq &&
4059 		(arc4random() % nfstest_outofseq) == 0)
4060 		    *tl = txdr_unsigned(lp->nfsl_seqid + 1);
4061 	}
4062 	if (syscred)
4063 		nd->nd_flag |= ND_USEGSSNAME;
4064 	error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, vp, p, cred,
4065 	    NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4066 	if (error)
4067 		return (error);
4068 	if (newone)
4069 	    NFSCL_INCRSEQID(lp->nfsl_open->nfso_own->nfsow_seqid, nd);
4070 	NFSCL_INCRSEQID(lp->nfsl_seqid, nd);
4071 	if (nd->nd_repstat == 0) {
4072 		NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID);
4073 		lp->nfsl_stateid.seqid = *tl++;
4074 		lp->nfsl_stateid.other[0] = *tl++;
4075 		lp->nfsl_stateid.other[1] = *tl++;
4076 		lp->nfsl_stateid.other[2] = *tl;
4077 	} else if (nd->nd_repstat == NFSERR_DENIED) {
4078 		NFSM_DISSECT(tl, u_int32_t *, 8 * NFSX_UNSIGNED);
4079 		size = fxdr_unsigned(int, *(tl + 7));
4080 		if (size < 0 || size > NFSV4_OPAQUELIMIT)
4081 			error = EBADRPC;
4082 		if (!error)
4083 			error = nfsm_advance(nd, NFSM_RNDUP(size), -1);
4084 	} else if (nd->nd_repstat == NFSERR_STALESTATEID)
4085 		nfscl_initiate_recovery(lp->nfsl_open->nfso_own->nfsow_clp);
4086 nfsmout:
4087 	mbuf_freem(nd->nd_mrep);
4088 	return (error);
4089 }
4090 
4091 /*
4092  * nfs statfs rpc
4093  * (always called with the vp for the mount point)
4094  */
4095 APPLESTATIC int
4096 nfsrpc_statfs(vnode_t vp, struct nfsstatfs *sbp, struct nfsfsinfo *fsp,
4097     struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp,
4098     void *stuff)
4099 {
4100 	u_int32_t *tl = NULL;
4101 	struct nfsrv_descript nfsd, *nd = &nfsd;
4102 	struct nfsmount *nmp;
4103 	nfsattrbit_t attrbits;
4104 	int error;
4105 
4106 	*attrflagp = 0;
4107 	nmp = VFSTONFS(vnode_mount(vp));
4108 	if (NFSHASNFSV4(nmp)) {
4109 		/*
4110 		 * For V4, you actually do a getattr.
4111 		 */
4112 		NFSCL_REQSTART(nd, NFSPROC_GETATTR, vp);
4113 		NFSSTATFS_GETATTRBIT(&attrbits);
4114 		(void) nfsrv_putattrbit(nd, &attrbits);
4115 		nd->nd_flag |= ND_USEGSSNAME;
4116 		error = nfscl_request(nd, vp, p, cred, stuff);
4117 		if (error)
4118 			return (error);
4119 		if (nd->nd_repstat == 0) {
4120 			error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0,
4121 			    NULL, NULL, sbp, fsp, NULL, 0, NULL, NULL, NULL, p,
4122 			    cred);
4123 			if (!error) {
4124 				nmp->nm_fsid[0] = nap->na_filesid[0];
4125 				nmp->nm_fsid[1] = nap->na_filesid[1];
4126 				NFSSETHASSETFSID(nmp);
4127 				*attrflagp = 1;
4128 			}
4129 		} else {
4130 			error = nd->nd_repstat;
4131 		}
4132 		if (error)
4133 			goto nfsmout;
4134 	} else {
4135 		NFSCL_REQSTART(nd, NFSPROC_FSSTAT, vp);
4136 		error = nfscl_request(nd, vp, p, cred, stuff);
4137 		if (error)
4138 			return (error);
4139 		if (nd->nd_flag & ND_NFSV3) {
4140 			error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
4141 			if (error)
4142 				goto nfsmout;
4143 		}
4144 		if (nd->nd_repstat) {
4145 			error = nd->nd_repstat;
4146 			goto nfsmout;
4147 		}
4148 		NFSM_DISSECT(tl, u_int32_t *,
4149 		    NFSX_STATFS(nd->nd_flag & ND_NFSV3));
4150 	}
4151 	if (NFSHASNFSV3(nmp)) {
4152 		sbp->sf_tbytes = fxdr_hyper(tl); tl += 2;
4153 		sbp->sf_fbytes = fxdr_hyper(tl); tl += 2;
4154 		sbp->sf_abytes = fxdr_hyper(tl); tl += 2;
4155 		sbp->sf_tfiles = fxdr_hyper(tl); tl += 2;
4156 		sbp->sf_ffiles = fxdr_hyper(tl); tl += 2;
4157 		sbp->sf_afiles = fxdr_hyper(tl); tl += 2;
4158 		sbp->sf_invarsec = fxdr_unsigned(u_int32_t, *tl);
4159 	} else if (NFSHASNFSV4(nmp) == 0) {
4160 		sbp->sf_tsize = fxdr_unsigned(u_int32_t, *tl++);
4161 		sbp->sf_bsize = fxdr_unsigned(u_int32_t, *tl++);
4162 		sbp->sf_blocks = fxdr_unsigned(u_int32_t, *tl++);
4163 		sbp->sf_bfree = fxdr_unsigned(u_int32_t, *tl++);
4164 		sbp->sf_bavail = fxdr_unsigned(u_int32_t, *tl);
4165 	}
4166 nfsmout:
4167 	mbuf_freem(nd->nd_mrep);
4168 	return (error);
4169 }
4170 
4171 /*
4172  * nfs pathconf rpc
4173  */
4174 APPLESTATIC int
4175 nfsrpc_pathconf(vnode_t vp, struct nfsv3_pathconf *pc,
4176     struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp,
4177     void *stuff)
4178 {
4179 	struct nfsrv_descript nfsd, *nd = &nfsd;
4180 	struct nfsmount *nmp;
4181 	u_int32_t *tl;
4182 	nfsattrbit_t attrbits;
4183 	int error;
4184 
4185 	*attrflagp = 0;
4186 	nmp = VFSTONFS(vnode_mount(vp));
4187 	if (NFSHASNFSV4(nmp)) {
4188 		/*
4189 		 * For V4, you actually do a getattr.
4190 		 */
4191 		NFSCL_REQSTART(nd, NFSPROC_GETATTR, vp);
4192 		NFSPATHCONF_GETATTRBIT(&attrbits);
4193 		(void) nfsrv_putattrbit(nd, &attrbits);
4194 		nd->nd_flag |= ND_USEGSSNAME;
4195 		error = nfscl_request(nd, vp, p, cred, stuff);
4196 		if (error)
4197 			return (error);
4198 		if (nd->nd_repstat == 0) {
4199 			error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0,
4200 			    pc, NULL, NULL, NULL, NULL, 0, NULL, NULL, NULL, p,
4201 			    cred);
4202 			if (!error)
4203 				*attrflagp = 1;
4204 		} else {
4205 			error = nd->nd_repstat;
4206 		}
4207 	} else {
4208 		NFSCL_REQSTART(nd, NFSPROC_PATHCONF, vp);
4209 		error = nfscl_request(nd, vp, p, cred, stuff);
4210 		if (error)
4211 			return (error);
4212 		error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
4213 		if (nd->nd_repstat && !error)
4214 			error = nd->nd_repstat;
4215 		if (!error) {
4216 			NFSM_DISSECT(tl, u_int32_t *, NFSX_V3PATHCONF);
4217 			pc->pc_linkmax = fxdr_unsigned(u_int32_t, *tl++);
4218 			pc->pc_namemax = fxdr_unsigned(u_int32_t, *tl++);
4219 			pc->pc_notrunc = fxdr_unsigned(u_int32_t, *tl++);
4220 			pc->pc_chownrestricted =
4221 			    fxdr_unsigned(u_int32_t, *tl++);
4222 			pc->pc_caseinsensitive =
4223 			    fxdr_unsigned(u_int32_t, *tl++);
4224 			pc->pc_casepreserving = fxdr_unsigned(u_int32_t, *tl);
4225 		}
4226 	}
4227 nfsmout:
4228 	mbuf_freem(nd->nd_mrep);
4229 	return (error);
4230 }
4231 
4232 /*
4233  * nfs version 3 fsinfo rpc call
4234  */
4235 APPLESTATIC int
4236 nfsrpc_fsinfo(vnode_t vp, struct nfsfsinfo *fsp, struct ucred *cred,
4237     NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, void *stuff)
4238 {
4239 	u_int32_t *tl;
4240 	struct nfsrv_descript nfsd, *nd = &nfsd;
4241 	int error;
4242 
4243 	*attrflagp = 0;
4244 	NFSCL_REQSTART(nd, NFSPROC_FSINFO, vp);
4245 	error = nfscl_request(nd, vp, p, cred, stuff);
4246 	if (error)
4247 		return (error);
4248 	error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
4249 	if (nd->nd_repstat && !error)
4250 		error = nd->nd_repstat;
4251 	if (!error) {
4252 		NFSM_DISSECT(tl, u_int32_t *, NFSX_V3FSINFO);
4253 		fsp->fs_rtmax = fxdr_unsigned(u_int32_t, *tl++);
4254 		fsp->fs_rtpref = fxdr_unsigned(u_int32_t, *tl++);
4255 		fsp->fs_rtmult = fxdr_unsigned(u_int32_t, *tl++);
4256 		fsp->fs_wtmax = fxdr_unsigned(u_int32_t, *tl++);
4257 		fsp->fs_wtpref = fxdr_unsigned(u_int32_t, *tl++);
4258 		fsp->fs_wtmult = fxdr_unsigned(u_int32_t, *tl++);
4259 		fsp->fs_dtpref = fxdr_unsigned(u_int32_t, *tl++);
4260 		fsp->fs_maxfilesize = fxdr_hyper(tl);
4261 		tl += 2;
4262 		fxdr_nfsv3time(tl, &fsp->fs_timedelta);
4263 		tl += 2;
4264 		fsp->fs_properties = fxdr_unsigned(u_int32_t, *tl);
4265 	}
4266 nfsmout:
4267 	mbuf_freem(nd->nd_mrep);
4268 	return (error);
4269 }
4270 
4271 /*
4272  * This function performs the Renew RPC.
4273  */
4274 APPLESTATIC int
4275 nfsrpc_renew(struct nfsclclient *clp, struct nfsclds *dsp, struct ucred *cred,
4276     NFSPROC_T *p)
4277 {
4278 	u_int32_t *tl;
4279 	struct nfsrv_descript nfsd;
4280 	struct nfsrv_descript *nd = &nfsd;
4281 	struct nfsmount *nmp;
4282 	int error;
4283 	struct nfssockreq *nrp;
4284 	struct nfsclsession *tsep;
4285 
4286 	nmp = clp->nfsc_nmp;
4287 	if (nmp == NULL)
4288 		return (0);
4289 	if (dsp == NULL)
4290 		nfscl_reqstart(nd, NFSPROC_RENEW, nmp, NULL, 0, NULL, NULL);
4291 	else
4292 		nfscl_reqstart(nd, NFSPROC_RENEW, nmp, NULL, 0, NULL,
4293 		    &dsp->nfsclds_sess);
4294 	if (!NFSHASNFSV4N(nmp)) {
4295 		/* NFSv4.1 just uses a Sequence Op and not a Renew. */
4296 		NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
4297 		tsep = nfsmnt_mdssession(nmp);
4298 		*tl++ = tsep->nfsess_clientid.lval[0];
4299 		*tl = tsep->nfsess_clientid.lval[1];
4300 	}
4301 	nrp = NULL;
4302 	if (dsp != NULL)
4303 		nrp = dsp->nfsclds_sockp;
4304 	if (nrp == NULL)
4305 		/* If NULL, use the MDS socket. */
4306 		nrp = &nmp->nm_sockreq;
4307 	nd->nd_flag |= ND_USEGSSNAME;
4308 	if (dsp == NULL)
4309 		error = newnfs_request(nd, nmp, NULL, nrp, NULL, p, cred,
4310 		    NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4311 	else
4312 		error = newnfs_request(nd, nmp, NULL, nrp, NULL, p, cred,
4313 		    NFS_PROG, NFS_VER4, NULL, 1, NULL, &dsp->nfsclds_sess);
4314 	if (error)
4315 		return (error);
4316 	error = nd->nd_repstat;
4317 	mbuf_freem(nd->nd_mrep);
4318 	return (error);
4319 }
4320 
4321 /*
4322  * This function performs the Releaselockowner RPC.
4323  */
4324 APPLESTATIC int
4325 nfsrpc_rellockown(struct nfsmount *nmp, struct nfscllockowner *lp,
4326     uint8_t *fh, int fhlen, struct ucred *cred, NFSPROC_T *p)
4327 {
4328 	struct nfsrv_descript nfsd, *nd = &nfsd;
4329 	u_int32_t *tl;
4330 	int error;
4331 	uint8_t own[NFSV4CL_LOCKNAMELEN + NFSX_V4FHMAX];
4332 	struct nfsclsession *tsep;
4333 
4334 	if (NFSHASNFSV4N(nmp)) {
4335 		/* For NFSv4.1, do a FreeStateID. */
4336 		nfscl_reqstart(nd, NFSPROC_FREESTATEID, nmp, NULL, 0, NULL,
4337 		    NULL);
4338 		nfsm_stateidtom(nd, &lp->nfsl_stateid, NFSSTATEID_PUTSTATEID);
4339 	} else {
4340 		nfscl_reqstart(nd, NFSPROC_RELEASELCKOWN, nmp, NULL, 0, NULL,
4341 		    NULL);
4342 		NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
4343 		tsep = nfsmnt_mdssession(nmp);
4344 		*tl++ = tsep->nfsess_clientid.lval[0];
4345 		*tl = tsep->nfsess_clientid.lval[1];
4346 		NFSBCOPY(lp->nfsl_owner, own, NFSV4CL_LOCKNAMELEN);
4347 		NFSBCOPY(fh, &own[NFSV4CL_LOCKNAMELEN], fhlen);
4348 		(void)nfsm_strtom(nd, own, NFSV4CL_LOCKNAMELEN + fhlen);
4349 	}
4350 	nd->nd_flag |= ND_USEGSSNAME;
4351 	error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4352 	    NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4353 	if (error)
4354 		return (error);
4355 	error = nd->nd_repstat;
4356 	mbuf_freem(nd->nd_mrep);
4357 	return (error);
4358 }
4359 
4360 /*
4361  * This function performs the Compound to get the mount pt FH.
4362  */
4363 APPLESTATIC int
4364 nfsrpc_getdirpath(struct nfsmount *nmp, u_char *dirpath, struct ucred *cred,
4365     NFSPROC_T *p)
4366 {
4367 	u_int32_t *tl;
4368 	struct nfsrv_descript nfsd;
4369 	struct nfsrv_descript *nd = &nfsd;
4370 	u_char *cp, *cp2;
4371 	int error, cnt, len, setnil;
4372 	u_int32_t *opcntp;
4373 
4374 	nfscl_reqstart(nd, NFSPROC_PUTROOTFH, nmp, NULL, 0, &opcntp, NULL);
4375 	cp = dirpath;
4376 	cnt = 0;
4377 	do {
4378 		setnil = 0;
4379 		while (*cp == '/')
4380 			cp++;
4381 		cp2 = cp;
4382 		while (*cp2 != '\0' && *cp2 != '/')
4383 			cp2++;
4384 		if (*cp2 == '/') {
4385 			setnil = 1;
4386 			*cp2 = '\0';
4387 		}
4388 		if (cp2 != cp) {
4389 			NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
4390 			*tl = txdr_unsigned(NFSV4OP_LOOKUP);
4391 			nfsm_strtom(nd, cp, strlen(cp));
4392 			cnt++;
4393 		}
4394 		if (setnil)
4395 			*cp2++ = '/';
4396 		cp = cp2;
4397 	} while (*cp != '\0');
4398 	if (NFSHASNFSV4N(nmp))
4399 		/* Has a Sequence Op done by nfscl_reqstart(). */
4400 		*opcntp = txdr_unsigned(3 + cnt);
4401 	else
4402 		*opcntp = txdr_unsigned(2 + cnt);
4403 	NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
4404 	*tl = txdr_unsigned(NFSV4OP_GETFH);
4405 	nd->nd_flag |= ND_USEGSSNAME;
4406 	error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4407 		NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4408 	if (error)
4409 		return (error);
4410 	if (nd->nd_repstat == 0) {
4411 		NFSM_DISSECT(tl, u_int32_t *, (3 + 2 * cnt) * NFSX_UNSIGNED);
4412 		tl += (2 + 2 * cnt);
4413 		if ((len = fxdr_unsigned(int, *tl)) <= 0 ||
4414 			len > NFSX_FHMAX) {
4415 			nd->nd_repstat = NFSERR_BADXDR;
4416 		} else {
4417 			nd->nd_repstat = nfsrv_mtostr(nd, nmp->nm_fh, len);
4418 			if (nd->nd_repstat == 0)
4419 				nmp->nm_fhsize = len;
4420 		}
4421 	}
4422 	error = nd->nd_repstat;
4423 nfsmout:
4424 	mbuf_freem(nd->nd_mrep);
4425 	return (error);
4426 }
4427 
4428 /*
4429  * This function performs the Delegreturn RPC.
4430  */
4431 APPLESTATIC int
4432 nfsrpc_delegreturn(struct nfscldeleg *dp, struct ucred *cred,
4433     struct nfsmount *nmp, NFSPROC_T *p, int syscred)
4434 {
4435 	u_int32_t *tl;
4436 	struct nfsrv_descript nfsd;
4437 	struct nfsrv_descript *nd = &nfsd;
4438 	int error;
4439 
4440 	nfscl_reqstart(nd, NFSPROC_DELEGRETURN, nmp, dp->nfsdl_fh,
4441 	    dp->nfsdl_fhlen, NULL, NULL);
4442 	NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID);
4443 	if (NFSHASNFSV4N(nmp))
4444 		*tl++ = 0;
4445 	else
4446 		*tl++ = dp->nfsdl_stateid.seqid;
4447 	*tl++ = dp->nfsdl_stateid.other[0];
4448 	*tl++ = dp->nfsdl_stateid.other[1];
4449 	*tl = dp->nfsdl_stateid.other[2];
4450 	if (syscred)
4451 		nd->nd_flag |= ND_USEGSSNAME;
4452 	error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4453 	    NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4454 	if (error)
4455 		return (error);
4456 	error = nd->nd_repstat;
4457 	mbuf_freem(nd->nd_mrep);
4458 	return (error);
4459 }
4460 
4461 /*
4462  * nfs getacl call.
4463  */
4464 APPLESTATIC int
4465 nfsrpc_getacl(vnode_t vp, struct ucred *cred, NFSPROC_T *p,
4466     struct acl *aclp, void *stuff)
4467 {
4468 	struct nfsrv_descript nfsd, *nd = &nfsd;
4469 	int error;
4470 	nfsattrbit_t attrbits;
4471 	struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
4472 
4473 	if (nfsrv_useacl == 0 || !NFSHASNFSV4(nmp))
4474 		return (EOPNOTSUPP);
4475 	NFSCL_REQSTART(nd, NFSPROC_GETACL, vp);
4476 	NFSZERO_ATTRBIT(&attrbits);
4477 	NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_ACL);
4478 	(void) nfsrv_putattrbit(nd, &attrbits);
4479 	error = nfscl_request(nd, vp, p, cred, stuff);
4480 	if (error)
4481 		return (error);
4482 	if (!nd->nd_repstat)
4483 		error = nfsv4_loadattr(nd, vp, NULL, NULL, NULL, 0, NULL,
4484 		    NULL, NULL, NULL, aclp, 0, NULL, NULL, NULL, p, cred);
4485 	else
4486 		error = nd->nd_repstat;
4487 	mbuf_freem(nd->nd_mrep);
4488 	return (error);
4489 }
4490 
4491 /*
4492  * nfs setacl call.
4493  */
4494 APPLESTATIC int
4495 nfsrpc_setacl(vnode_t vp, struct ucred *cred, NFSPROC_T *p,
4496     struct acl *aclp, void *stuff)
4497 {
4498 	int error;
4499 	struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
4500 
4501 	if (nfsrv_useacl == 0 || !NFSHASNFSV4(nmp))
4502 		return (EOPNOTSUPP);
4503 	error = nfsrpc_setattr(vp, NULL, aclp, cred, p, NULL, NULL, stuff);
4504 	return (error);
4505 }
4506 
4507 /*
4508  * nfs setacl call.
4509  */
4510 static int
4511 nfsrpc_setaclrpc(vnode_t vp, struct ucred *cred, NFSPROC_T *p,
4512     struct acl *aclp, nfsv4stateid_t *stateidp, void *stuff)
4513 {
4514 	struct nfsrv_descript nfsd, *nd = &nfsd;
4515 	int error;
4516 	nfsattrbit_t attrbits;
4517 	struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
4518 
4519 	if (!NFSHASNFSV4(nmp))
4520 		return (EOPNOTSUPP);
4521 	NFSCL_REQSTART(nd, NFSPROC_SETACL, vp);
4522 	nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID);
4523 	NFSZERO_ATTRBIT(&attrbits);
4524 	NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_ACL);
4525 	(void) nfsv4_fillattr(nd, vnode_mount(vp), vp, aclp, NULL, NULL, 0,
4526 	    &attrbits, NULL, NULL, 0, 0, 0, 0, (uint64_t)0);
4527 	error = nfscl_request(nd, vp, p, cred, stuff);
4528 	if (error)
4529 		return (error);
4530 	/* Don't care about the pre/postop attributes */
4531 	mbuf_freem(nd->nd_mrep);
4532 	return (nd->nd_repstat);
4533 }
4534 
4535 /*
4536  * Do the NFSv4.1 Exchange ID.
4537  */
4538 int
4539 nfsrpc_exchangeid(struct nfsmount *nmp, struct nfsclclient *clp,
4540     struct nfssockreq *nrp, uint32_t exchflags, struct nfsclds **dspp,
4541     struct ucred *cred, NFSPROC_T *p)
4542 {
4543 	uint32_t *tl, v41flags;
4544 	struct nfsrv_descript nfsd;
4545 	struct nfsrv_descript *nd = &nfsd;
4546 	struct nfsclds *dsp;
4547 	struct timespec verstime;
4548 	int error, len;
4549 
4550 	*dspp = NULL;
4551 	nfscl_reqstart(nd, NFSPROC_EXCHANGEID, nmp, NULL, 0, NULL, NULL);
4552 	NFSM_BUILD(tl, uint32_t *, 2 * NFSX_UNSIGNED);
4553 	*tl++ = txdr_unsigned(nfsboottime.tv_sec);	/* Client owner */
4554 	*tl = txdr_unsigned(clp->nfsc_rev);
4555 	(void) nfsm_strtom(nd, clp->nfsc_id, clp->nfsc_idlen);
4556 
4557 	NFSM_BUILD(tl, uint32_t *, 3 * NFSX_UNSIGNED);
4558 	*tl++ = txdr_unsigned(exchflags);
4559 	*tl++ = txdr_unsigned(NFSV4EXCH_SP4NONE);
4560 
4561 	/* Set the implementation id4 */
4562 	*tl = txdr_unsigned(1);
4563 	(void) nfsm_strtom(nd, "freebsd.org", strlen("freebsd.org"));
4564 	(void) nfsm_strtom(nd, version, strlen(version));
4565 	NFSM_BUILD(tl, uint32_t *, NFSX_V4TIME);
4566 	verstime.tv_sec = 1293840000;		/* Jan 1, 2011 */
4567 	verstime.tv_nsec = 0;
4568 	txdr_nfsv4time(&verstime, tl);
4569 	nd->nd_flag |= ND_USEGSSNAME;
4570 	error = newnfs_request(nd, nmp, NULL, nrp, NULL, p, cred,
4571 	    NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4572 	NFSCL_DEBUG(1, "exchangeid err=%d reps=%d\n", error,
4573 	    (int)nd->nd_repstat);
4574 	if (error != 0)
4575 		return (error);
4576 	if (nd->nd_repstat == 0) {
4577 		NFSM_DISSECT(tl, uint32_t *, 6 * NFSX_UNSIGNED + NFSX_HYPER);
4578 		len = fxdr_unsigned(int, *(tl + 7));
4579 		if (len < 0 || len > NFSV4_OPAQUELIMIT) {
4580 			error = NFSERR_BADXDR;
4581 			goto nfsmout;
4582 		}
4583 		dsp = malloc(sizeof(struct nfsclds) + len + 1, M_NFSCLDS,
4584 		    M_WAITOK | M_ZERO);
4585 		dsp->nfsclds_expire = NFSD_MONOSEC + clp->nfsc_renew;
4586 		dsp->nfsclds_servownlen = len;
4587 		dsp->nfsclds_sess.nfsess_clientid.lval[0] = *tl++;
4588 		dsp->nfsclds_sess.nfsess_clientid.lval[1] = *tl++;
4589 		dsp->nfsclds_sess.nfsess_sequenceid =
4590 		    fxdr_unsigned(uint32_t, *tl++);
4591 		v41flags = fxdr_unsigned(uint32_t, *tl);
4592 		if ((v41flags & NFSV4EXCH_USEPNFSMDS) != 0 &&
4593 		    NFSHASPNFSOPT(nmp)) {
4594 			NFSCL_DEBUG(1, "set PNFS\n");
4595 			NFSLOCKMNT(nmp);
4596 			nmp->nm_state |= NFSSTA_PNFS;
4597 			NFSUNLOCKMNT(nmp);
4598 			dsp->nfsclds_flags |= NFSCLDS_MDS;
4599 		}
4600 		if ((v41flags & NFSV4EXCH_USEPNFSDS) != 0)
4601 			dsp->nfsclds_flags |= NFSCLDS_DS;
4602 		if (len > 0)
4603 			nd->nd_repstat = nfsrv_mtostr(nd,
4604 			    dsp->nfsclds_serverown, len);
4605 		if (nd->nd_repstat == 0) {
4606 			mtx_init(&dsp->nfsclds_mtx, "nfsds", NULL, MTX_DEF);
4607 			mtx_init(&dsp->nfsclds_sess.nfsess_mtx, "nfssession",
4608 			    NULL, MTX_DEF);
4609 			nfscl_initsessionslots(&dsp->nfsclds_sess);
4610 			*dspp = dsp;
4611 		} else
4612 			free(dsp, M_NFSCLDS);
4613 	}
4614 	error = nd->nd_repstat;
4615 nfsmout:
4616 	mbuf_freem(nd->nd_mrep);
4617 	return (error);
4618 }
4619 
4620 /*
4621  * Do the NFSv4.1 Create Session.
4622  */
4623 int
4624 nfsrpc_createsession(struct nfsmount *nmp, struct nfsclsession *sep,
4625     struct nfssockreq *nrp, uint32_t sequenceid, int mds, struct ucred *cred,
4626     NFSPROC_T *p)
4627 {
4628 	uint32_t crflags, maxval, *tl;
4629 	struct nfsrv_descript nfsd;
4630 	struct nfsrv_descript *nd = &nfsd;
4631 	int error, irdcnt;
4632 
4633 	nfscl_reqstart(nd, NFSPROC_CREATESESSION, nmp, NULL, 0, NULL, NULL);
4634 	NFSM_BUILD(tl, uint32_t *, 4 * NFSX_UNSIGNED);
4635 	*tl++ = sep->nfsess_clientid.lval[0];
4636 	*tl++ = sep->nfsess_clientid.lval[1];
4637 	*tl++ = txdr_unsigned(sequenceid);
4638 	crflags = (NFSMNT_RDONLY(nmp->nm_mountp) ? 0 : NFSV4CRSESS_PERSIST);
4639 	if (nfscl_enablecallb != 0 && nfs_numnfscbd > 0 && mds != 0)
4640 		crflags |= NFSV4CRSESS_CONNBACKCHAN;
4641 	*tl = txdr_unsigned(crflags);
4642 
4643 	/* Fill in fore channel attributes. */
4644 	NFSM_BUILD(tl, uint32_t *, 7 * NFSX_UNSIGNED);
4645 	*tl++ = 0;				/* Header pad size */
4646 	*tl++ = txdr_unsigned(nmp->nm_wsize + NFS_MAXXDR);/* Max request size */
4647 	*tl++ = txdr_unsigned(nmp->nm_rsize + NFS_MAXXDR);/* Max reply size */
4648 	*tl++ = txdr_unsigned(4096);		/* Max response size cached */
4649 	*tl++ = txdr_unsigned(20);		/* Max operations */
4650 	*tl++ = txdr_unsigned(64);		/* Max slots */
4651 	*tl = 0;				/* No rdma ird */
4652 
4653 	/* Fill in back channel attributes. */
4654 	NFSM_BUILD(tl, uint32_t *, 7 * NFSX_UNSIGNED);
4655 	*tl++ = 0;				/* Header pad size */
4656 	*tl++ = txdr_unsigned(10000);		/* Max request size */
4657 	*tl++ = txdr_unsigned(10000);		/* Max response size */
4658 	*tl++ = txdr_unsigned(4096);		/* Max response size cached */
4659 	*tl++ = txdr_unsigned(4);		/* Max operations */
4660 	*tl++ = txdr_unsigned(NFSV4_CBSLOTS);	/* Max slots */
4661 	*tl = 0;				/* No rdma ird */
4662 
4663 	NFSM_BUILD(tl, uint32_t *, 8 * NFSX_UNSIGNED);
4664 	*tl++ = txdr_unsigned(NFS_CALLBCKPROG);	/* Call back prog # */
4665 
4666 	/* Allow AUTH_SYS callbacks as uid, gid == 0. */
4667 	*tl++ = txdr_unsigned(1);		/* Auth_sys only */
4668 	*tl++ = txdr_unsigned(AUTH_SYS);	/* AUTH_SYS type */
4669 	*tl++ = txdr_unsigned(nfsboottime.tv_sec); /* time stamp */
4670 	*tl++ = 0;				/* Null machine name */
4671 	*tl++ = 0;				/* Uid == 0 */
4672 	*tl++ = 0;				/* Gid == 0 */
4673 	*tl = 0;				/* No additional gids */
4674 	nd->nd_flag |= ND_USEGSSNAME;
4675 	error = newnfs_request(nd, nmp, NULL, nrp, NULL, p, cred, NFS_PROG,
4676 	    NFS_VER4, NULL, 1, NULL, NULL);
4677 	if (error != 0)
4678 		return (error);
4679 	if (nd->nd_repstat == 0) {
4680 		NFSM_DISSECT(tl, uint32_t *, NFSX_V4SESSIONID +
4681 		    2 * NFSX_UNSIGNED);
4682 		bcopy(tl, sep->nfsess_sessionid, NFSX_V4SESSIONID);
4683 		tl += NFSX_V4SESSIONID / NFSX_UNSIGNED;
4684 		sep->nfsess_sequenceid = fxdr_unsigned(uint32_t, *tl++);
4685 		crflags = fxdr_unsigned(uint32_t, *tl);
4686 		if ((crflags & NFSV4CRSESS_PERSIST) != 0 && mds != 0) {
4687 			NFSLOCKMNT(nmp);
4688 			nmp->nm_state |= NFSSTA_SESSPERSIST;
4689 			NFSUNLOCKMNT(nmp);
4690 		}
4691 
4692 		/* Get the fore channel slot count. */
4693 		NFSM_DISSECT(tl, uint32_t *, 7 * NFSX_UNSIGNED);
4694 		tl++;			/* Skip the header pad size. */
4695 
4696 		/* Make sure nm_wsize is small enough. */
4697 		maxval = fxdr_unsigned(uint32_t, *tl++);
4698 		while (maxval < nmp->nm_wsize + NFS_MAXXDR) {
4699 			if (nmp->nm_wsize > 8096)
4700 				nmp->nm_wsize /= 2;
4701 			else
4702 				break;
4703 		}
4704 
4705 		/* Make sure nm_rsize is small enough. */
4706 		maxval = fxdr_unsigned(uint32_t, *tl++);
4707 		while (maxval < nmp->nm_rsize + NFS_MAXXDR) {
4708 			if (nmp->nm_rsize > 8096)
4709 				nmp->nm_rsize /= 2;
4710 			else
4711 				break;
4712 		}
4713 
4714 		sep->nfsess_maxcache = fxdr_unsigned(int, *tl++);
4715 		tl++;
4716 		sep->nfsess_foreslots = fxdr_unsigned(uint16_t, *tl++);
4717 		NFSCL_DEBUG(4, "fore slots=%d\n", (int)sep->nfsess_foreslots);
4718 		irdcnt = fxdr_unsigned(int, *tl);
4719 		if (irdcnt > 0)
4720 			NFSM_DISSECT(tl, uint32_t *, irdcnt * NFSX_UNSIGNED);
4721 
4722 		/* and the back channel slot count. */
4723 		NFSM_DISSECT(tl, uint32_t *, 7 * NFSX_UNSIGNED);
4724 		tl += 5;
4725 		sep->nfsess_backslots = fxdr_unsigned(uint16_t, *tl);
4726 		NFSCL_DEBUG(4, "back slots=%d\n", (int)sep->nfsess_backslots);
4727 	}
4728 	error = nd->nd_repstat;
4729 nfsmout:
4730 	mbuf_freem(nd->nd_mrep);
4731 	return (error);
4732 }
4733 
4734 /*
4735  * Do the NFSv4.1 Destroy Session.
4736  */
4737 int
4738 nfsrpc_destroysession(struct nfsmount *nmp, struct nfsclclient *clp,
4739     struct ucred *cred, NFSPROC_T *p)
4740 {
4741 	uint32_t *tl;
4742 	struct nfsrv_descript nfsd;
4743 	struct nfsrv_descript *nd = &nfsd;
4744 	int error;
4745 	struct nfsclsession *tsep;
4746 
4747 	nfscl_reqstart(nd, NFSPROC_DESTROYSESSION, nmp, NULL, 0, NULL, NULL);
4748 	NFSM_BUILD(tl, uint32_t *, NFSX_V4SESSIONID);
4749 	tsep = nfsmnt_mdssession(nmp);
4750 	bcopy(tsep->nfsess_sessionid, tl, NFSX_V4SESSIONID);
4751 	nd->nd_flag |= ND_USEGSSNAME;
4752 	error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4753 	    NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4754 	if (error != 0)
4755 		return (error);
4756 	error = nd->nd_repstat;
4757 	mbuf_freem(nd->nd_mrep);
4758 	return (error);
4759 }
4760 
4761 /*
4762  * Do the NFSv4.1 Destroy Client.
4763  */
4764 int
4765 nfsrpc_destroyclient(struct nfsmount *nmp, struct nfsclclient *clp,
4766     struct ucred *cred, NFSPROC_T *p)
4767 {
4768 	uint32_t *tl;
4769 	struct nfsrv_descript nfsd;
4770 	struct nfsrv_descript *nd = &nfsd;
4771 	int error;
4772 	struct nfsclsession *tsep;
4773 
4774 	nfscl_reqstart(nd, NFSPROC_DESTROYCLIENT, nmp, NULL, 0, NULL, NULL);
4775 	NFSM_BUILD(tl, uint32_t *, 2 * NFSX_UNSIGNED);
4776 	tsep = nfsmnt_mdssession(nmp);
4777 	*tl++ = tsep->nfsess_clientid.lval[0];
4778 	*tl = tsep->nfsess_clientid.lval[1];
4779 	nd->nd_flag |= ND_USEGSSNAME;
4780 	error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4781 	    NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4782 	if (error != 0)
4783 		return (error);
4784 	error = nd->nd_repstat;
4785 	mbuf_freem(nd->nd_mrep);
4786 	return (error);
4787 }
4788 
4789 /*
4790  * Do the NFSv4.1 LayoutGet.
4791  */
4792 int
4793 nfsrpc_layoutget(struct nfsmount *nmp, uint8_t *fhp, int fhlen, int iomode,
4794     uint64_t offset, uint64_t len, uint64_t minlen, int layoutlen,
4795     nfsv4stateid_t *stateidp, int *retonclosep, struct nfsclflayouthead *flhp,
4796     struct ucred *cred, NFSPROC_T *p, void *stuff)
4797 {
4798 	uint32_t *tl;
4799 	struct nfsrv_descript nfsd, *nd = &nfsd;
4800 	struct nfsfh *nfhp;
4801 	struct nfsclflayout *flp, *prevflp, *tflp;
4802 	int cnt, error, gotiomode, fhcnt, nfhlen, i, j;
4803 	uint8_t *cp;
4804 	uint64_t retlen;
4805 
4806 	flp = NULL;
4807 	gotiomode = -1;
4808 	nfscl_reqstart(nd, NFSPROC_LAYOUTGET, nmp, fhp, fhlen, NULL, NULL);
4809 	NFSM_BUILD(tl, uint32_t *, 4 * NFSX_UNSIGNED + 3 * NFSX_HYPER +
4810 	    NFSX_STATEID);
4811 	*tl++ = newnfs_false;		/* Don't signal availability. */
4812 	*tl++ = txdr_unsigned(NFSLAYOUT_NFSV4_1_FILES);
4813 	*tl++ = txdr_unsigned(iomode);
4814 	txdr_hyper(offset, tl);
4815 	tl += 2;
4816 	txdr_hyper(len, tl);
4817 	tl += 2;
4818 	txdr_hyper(minlen, tl);
4819 	tl += 2;
4820 	*tl++ = txdr_unsigned(stateidp->seqid);
4821 	NFSCL_DEBUG(4, "layget seq=%d\n", (int)stateidp->seqid);
4822 	*tl++ = stateidp->other[0];
4823 	*tl++ = stateidp->other[1];
4824 	*tl++ = stateidp->other[2];
4825 	*tl = txdr_unsigned(layoutlen);
4826 	nd->nd_flag |= ND_USEGSSNAME;
4827 	error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4828 	    NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4829 	if (error != 0)
4830 		return (error);
4831 	if (nd->nd_repstat == 0) {
4832 		NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED + NFSX_STATEID);
4833 		if (*tl++ != 0)
4834 			*retonclosep = 1;
4835 		else
4836 			*retonclosep = 0;
4837 		stateidp->seqid = fxdr_unsigned(uint32_t, *tl++);
4838 		NFSCL_DEBUG(4, "retoncls=%d stseq=%d\n", *retonclosep,
4839 		    (int)stateidp->seqid);
4840 		stateidp->other[0] = *tl++;
4841 		stateidp->other[1] = *tl++;
4842 		stateidp->other[2] = *tl++;
4843 		cnt = fxdr_unsigned(int, *tl);
4844 		NFSCL_DEBUG(4, "layg cnt=%d\n", cnt);
4845 		if (cnt <= 0 || cnt > 10000) {
4846 			/* Don't accept more than 10000 layouts in reply. */
4847 			error = NFSERR_BADXDR;
4848 			goto nfsmout;
4849 		}
4850 		for (i = 0; i < cnt; i++) {
4851 			/* Dissect all the way to the file handle cnt. */
4852 			NFSM_DISSECT(tl, uint32_t *, 3 * NFSX_HYPER +
4853 			    6 * NFSX_UNSIGNED + NFSX_V4DEVICEID);
4854 			fhcnt = fxdr_unsigned(int, *(tl + 11 +
4855 			    NFSX_V4DEVICEID / NFSX_UNSIGNED));
4856 			NFSCL_DEBUG(4, "fhcnt=%d\n", fhcnt);
4857 			if (fhcnt < 0 || fhcnt > 100) {
4858 				/* Don't accept more than 100 file handles. */
4859 				error = NFSERR_BADXDR;
4860 				goto nfsmout;
4861 			}
4862 			if (fhcnt > 1)
4863 				flp = malloc(sizeof(*flp) + (fhcnt - 1) *
4864 				    sizeof(struct nfsfh *),
4865 				    M_NFSFLAYOUT, M_WAITOK);
4866 			else
4867 				flp = malloc(sizeof(*flp),
4868 				    M_NFSFLAYOUT, M_WAITOK);
4869 			flp->nfsfl_flags = 0;
4870 			flp->nfsfl_fhcnt = 0;
4871 			flp->nfsfl_devp = NULL;
4872 			flp->nfsfl_off = fxdr_hyper(tl); tl += 2;
4873 			retlen = fxdr_hyper(tl); tl += 2;
4874 			if (flp->nfsfl_off + retlen < flp->nfsfl_off)
4875 				flp->nfsfl_end = UINT64_MAX - flp->nfsfl_off;
4876 			else
4877 				flp->nfsfl_end = flp->nfsfl_off + retlen;
4878 			flp->nfsfl_iomode = fxdr_unsigned(int, *tl++);
4879 			if (gotiomode == -1)
4880 				gotiomode = flp->nfsfl_iomode;
4881 			NFSCL_DEBUG(4, "layg reqiom=%d retiom=%d\n", iomode,
4882 			    (int)flp->nfsfl_iomode);
4883 			if (fxdr_unsigned(int, *tl++) !=
4884 			    NFSLAYOUT_NFSV4_1_FILES) {
4885 				printf("NFSv4.1: got non-files layout\n");
4886 				error = NFSERR_BADXDR;
4887 				goto nfsmout;
4888 			}
4889 			NFSBCOPY(++tl, flp->nfsfl_dev, NFSX_V4DEVICEID);
4890 			tl += (NFSX_V4DEVICEID / NFSX_UNSIGNED);
4891 			flp->nfsfl_util = fxdr_unsigned(uint32_t, *tl++);
4892 			NFSCL_DEBUG(4, "flutil=0x%x\n", flp->nfsfl_util);
4893 			flp->nfsfl_stripe1 = fxdr_unsigned(uint32_t, *tl++);
4894 			flp->nfsfl_patoff = fxdr_hyper(tl); tl += 2;
4895 			if (fxdr_unsigned(int, *tl) != fhcnt) {
4896 				printf("EEK! bad fhcnt\n");
4897 				error = NFSERR_BADXDR;
4898 				goto nfsmout;
4899 			}
4900 			for (j = 0; j < fhcnt; j++) {
4901 				NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
4902 				nfhlen = fxdr_unsigned(int, *tl);
4903 				if (nfhlen <= 0 || nfhlen > NFSX_V4FHMAX) {
4904 					error = NFSERR_BADXDR;
4905 					goto nfsmout;
4906 				}
4907 				nfhp = malloc(sizeof(*nfhp) + nfhlen - 1,
4908 				    M_NFSFH, M_WAITOK);
4909 				flp->nfsfl_fh[j] = nfhp;
4910 				flp->nfsfl_fhcnt++;
4911 				nfhp->nfh_len = nfhlen;
4912 				NFSM_DISSECT(cp, uint8_t *, NFSM_RNDUP(nfhlen));
4913 				NFSBCOPY(cp, nfhp->nfh_fh, nfhlen);
4914 			}
4915 			if (flp->nfsfl_iomode == gotiomode) {
4916 				/* Keep the list in increasing offset order. */
4917 				tflp = LIST_FIRST(flhp);
4918 				prevflp = NULL;
4919 				while (tflp != NULL &&
4920 				    tflp->nfsfl_off < flp->nfsfl_off) {
4921 					prevflp = tflp;
4922 					tflp = LIST_NEXT(tflp, nfsfl_list);
4923 				}
4924 				if (prevflp == NULL)
4925 					LIST_INSERT_HEAD(flhp, flp, nfsfl_list);
4926 				else
4927 					LIST_INSERT_AFTER(prevflp, flp,
4928 					    nfsfl_list);
4929 			} else {
4930 				printf("nfscl_layoutget(): got wrong iomode\n");
4931 				nfscl_freeflayout(flp);
4932 			}
4933 			flp = NULL;
4934 		}
4935 	}
4936 	if (nd->nd_repstat != 0 && error == 0)
4937 		error = nd->nd_repstat;
4938 nfsmout:
4939 	if (error != 0 && flp != NULL)
4940 		nfscl_freeflayout(flp);
4941 	mbuf_freem(nd->nd_mrep);
4942 	return (error);
4943 }
4944 
4945 /*
4946  * Do the NFSv4.1 Get Device Info.
4947  */
4948 int
4949 nfsrpc_getdeviceinfo(struct nfsmount *nmp, uint8_t *deviceid, int layouttype,
4950     uint32_t *notifybitsp, struct nfscldevinfo **ndip, struct ucred *cred,
4951     NFSPROC_T *p)
4952 {
4953 	uint32_t cnt, *tl;
4954 	struct nfsrv_descript nfsd;
4955 	struct nfsrv_descript *nd = &nfsd;
4956 	struct sockaddr_storage ss;
4957 	struct nfsclds *dsp = NULL, **dspp;
4958 	struct nfscldevinfo *ndi;
4959 	int addrcnt, bitcnt, error, i, isudp, j, pos, safilled, stripecnt;
4960 	uint8_t stripeindex;
4961 
4962 	*ndip = NULL;
4963 	ndi = NULL;
4964 	nfscl_reqstart(nd, NFSPROC_GETDEVICEINFO, nmp, NULL, 0, NULL, NULL);
4965 	NFSM_BUILD(tl, uint32_t *, NFSX_V4DEVICEID + 3 * NFSX_UNSIGNED);
4966 	NFSBCOPY(deviceid, tl, NFSX_V4DEVICEID);
4967 	tl += (NFSX_V4DEVICEID / NFSX_UNSIGNED);
4968 	*tl++ = txdr_unsigned(layouttype);
4969 	*tl++ = txdr_unsigned(100000);
4970 	if (notifybitsp != NULL && *notifybitsp != 0) {
4971 		*tl = txdr_unsigned(1);		/* One word of bits. */
4972 		NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
4973 		*tl = txdr_unsigned(*notifybitsp);
4974 	} else
4975 		*tl = txdr_unsigned(0);
4976 	nd->nd_flag |= ND_USEGSSNAME;
4977 	error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4978 	    NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4979 	if (error != 0)
4980 		return (error);
4981 	if (nd->nd_repstat == 0) {
4982 		NFSM_DISSECT(tl, uint32_t *, 3 * NFSX_UNSIGNED);
4983 		if (layouttype != fxdr_unsigned(int, *tl++))
4984 			printf("EEK! devinfo layout type not same!\n");
4985 		stripecnt = fxdr_unsigned(int, *++tl);
4986 		NFSCL_DEBUG(4, "stripecnt=%d\n", stripecnt);
4987 		if (stripecnt < 1 || stripecnt > 4096) {
4988 			printf("NFS devinfo stripecnt %d: out of range\n",
4989 			    stripecnt);
4990 			error = NFSERR_BADXDR;
4991 			goto nfsmout;
4992 		}
4993 		NFSM_DISSECT(tl, uint32_t *, (stripecnt + 1) * NFSX_UNSIGNED);
4994 		addrcnt = fxdr_unsigned(int, *(tl + stripecnt));
4995 		NFSCL_DEBUG(4, "addrcnt=%d\n", addrcnt);
4996 		if (addrcnt < 1 || addrcnt > 128) {
4997 			printf("NFS devinfo addrcnt %d: out of range\n",
4998 			    addrcnt);
4999 			error = NFSERR_BADXDR;
5000 			goto nfsmout;
5001 		}
5002 
5003 		/*
5004 		 * Now we know how many stripe indices and addresses, so
5005 		 * we can allocate the structure the correct size.
5006 		 */
5007 		i = (stripecnt * sizeof(uint8_t)) / sizeof(struct nfsclds *)
5008 		    + 1;
5009 		NFSCL_DEBUG(4, "stripeindices=%d\n", i);
5010 		ndi = malloc(sizeof(*ndi) + (addrcnt + i) *
5011 		    sizeof(struct nfsclds *), M_NFSDEVINFO, M_WAITOK | M_ZERO);
5012 		NFSBCOPY(deviceid, ndi->nfsdi_deviceid, NFSX_V4DEVICEID);
5013 		ndi->nfsdi_refcnt = 0;
5014 		ndi->nfsdi_stripecnt = stripecnt;
5015 		ndi->nfsdi_addrcnt = addrcnt;
5016 		/* Fill in the stripe indices. */
5017 		for (i = 0; i < stripecnt; i++) {
5018 			stripeindex = fxdr_unsigned(uint8_t, *tl++);
5019 			NFSCL_DEBUG(4, "stripeind=%d\n", stripeindex);
5020 			if (stripeindex >= addrcnt) {
5021 				printf("NFS devinfo stripeindex %d: too big\n",
5022 				    (int)stripeindex);
5023 				error = NFSERR_BADXDR;
5024 				goto nfsmout;
5025 			}
5026 			nfsfldi_setstripeindex(ndi, i, stripeindex);
5027 		}
5028 
5029 		/* Now, dissect the server address(es). */
5030 		safilled = 0;
5031 		for (i = 0; i < addrcnt; i++) {
5032 			NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
5033 			cnt = fxdr_unsigned(uint32_t, *tl);
5034 			if (cnt == 0) {
5035 				printf("NFS devinfo 0 len addrlist\n");
5036 				error = NFSERR_BADXDR;
5037 				goto nfsmout;
5038 			}
5039 			dspp = nfsfldi_addr(ndi, i);
5040 			pos = arc4random() % cnt;	/* Choose one. */
5041 			safilled = 0;
5042 			for (j = 0; j < cnt; j++) {
5043 				error = nfsv4_getipaddr(nd, &ss, &isudp);
5044 				if (error != 0 && error != EPERM) {
5045 					error = NFSERR_BADXDR;
5046 					goto nfsmout;
5047 				}
5048 				if (error == 0 && isudp == 0) {
5049 					/*
5050 					 * The algorithm is:
5051 					 * - use "pos" entry if it is of the
5052 					 *   same af_family or none of them
5053 					 *   is of the same af_family
5054 					 * else
5055 					 * - use the first one of the same
5056 					 *   af_family.
5057 					 */
5058 					if ((safilled == 0 && ss.ss_family ==
5059 					     nmp->nm_nam->sa_family) ||
5060 					    (j == pos &&
5061 					     (safilled == 0 || ss.ss_family ==
5062 					      nmp->nm_nam->sa_family)) ||
5063 					    (safilled == 1 && ss.ss_family ==
5064 					     nmp->nm_nam->sa_family)) {
5065 						error = nfsrpc_fillsa(nmp, &ss,
5066 						    &dsp, p);
5067 						if (error == 0) {
5068 							*dspp = dsp;
5069 							if (ss.ss_family ==
5070 							 nmp->nm_nam->sa_family)
5071 								safilled = 2;
5072 							else
5073 								safilled = 1;
5074 						}
5075 					}
5076 				}
5077 			}
5078 			if (safilled == 0)
5079 				break;
5080 		}
5081 
5082 		/* And the notify bits. */
5083 		NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
5084 		if (safilled != 0) {
5085 			bitcnt = fxdr_unsigned(int, *tl);
5086 			if (bitcnt > 0) {
5087 				NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
5088 				if (notifybitsp != NULL)
5089 					*notifybitsp =
5090 					    fxdr_unsigned(uint32_t, *tl);
5091 			}
5092 			*ndip = ndi;
5093 		} else
5094 			error = EPERM;
5095 	}
5096 	if (nd->nd_repstat != 0)
5097 		error = nd->nd_repstat;
5098 nfsmout:
5099 	if (error != 0 && ndi != NULL)
5100 		nfscl_freedevinfo(ndi);
5101 	mbuf_freem(nd->nd_mrep);
5102 	return (error);
5103 }
5104 
5105 /*
5106  * Do the NFSv4.1 LayoutCommit.
5107  */
5108 int
5109 nfsrpc_layoutcommit(struct nfsmount *nmp, uint8_t *fh, int fhlen, int reclaim,
5110     uint64_t off, uint64_t len, uint64_t lastbyte, nfsv4stateid_t *stateidp,
5111     int layouttype, int layoutupdatecnt, uint8_t *layp, struct ucred *cred,
5112     NFSPROC_T *p, void *stuff)
5113 {
5114 	uint32_t *tl;
5115 	struct nfsrv_descript nfsd, *nd = &nfsd;
5116 	int error, outcnt, i;
5117 	uint8_t *cp;
5118 
5119 	nfscl_reqstart(nd, NFSPROC_LAYOUTCOMMIT, nmp, fh, fhlen, NULL, NULL);
5120 	NFSM_BUILD(tl, uint32_t *, 5 * NFSX_UNSIGNED + 3 * NFSX_HYPER +
5121 	    NFSX_STATEID);
5122 	txdr_hyper(off, tl);
5123 	tl += 2;
5124 	txdr_hyper(len, tl);
5125 	tl += 2;
5126 	if (reclaim != 0)
5127 		*tl++ = newnfs_true;
5128 	else
5129 		*tl++ = newnfs_false;
5130 	*tl++ = txdr_unsigned(stateidp->seqid);
5131 	*tl++ = stateidp->other[0];
5132 	*tl++ = stateidp->other[1];
5133 	*tl++ = stateidp->other[2];
5134 	*tl++ = newnfs_true;
5135 	if (lastbyte < off)
5136 		lastbyte = off;
5137 	else if (lastbyte >= (off + len))
5138 		lastbyte = off + len - 1;
5139 	txdr_hyper(lastbyte, tl);
5140 	tl += 2;
5141 	*tl++ = newnfs_false;
5142 	*tl++ = txdr_unsigned(layouttype);
5143 	*tl = txdr_unsigned(layoutupdatecnt);
5144 	if (layoutupdatecnt > 0) {
5145 		KASSERT(layouttype != NFSLAYOUT_NFSV4_1_FILES,
5146 		    ("Must be nil for Files Layout"));
5147 		outcnt = NFSM_RNDUP(layoutupdatecnt);
5148 		NFSM_BUILD(cp, uint8_t *, outcnt);
5149 		NFSBCOPY(layp, cp, layoutupdatecnt);
5150 		cp += layoutupdatecnt;
5151 		for (i = 0; i < (outcnt - layoutupdatecnt); i++)
5152 			*cp++ = 0x0;
5153 	}
5154 	nd->nd_flag |= ND_USEGSSNAME;
5155 	error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
5156 	    NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
5157 	if (error != 0)
5158 		return (error);
5159 	error = nd->nd_repstat;
5160 	mbuf_freem(nd->nd_mrep);
5161 	return (error);
5162 }
5163 
5164 /*
5165  * Do the NFSv4.1 LayoutReturn.
5166  */
5167 int
5168 nfsrpc_layoutreturn(struct nfsmount *nmp, uint8_t *fh, int fhlen, int reclaim,
5169     int layouttype, uint32_t iomode, int layoutreturn, uint64_t offset,
5170     uint64_t len, nfsv4stateid_t *stateidp, int layoutcnt, uint32_t *layp,
5171     struct ucred *cred, NFSPROC_T *p, void *stuff)
5172 {
5173 	uint32_t *tl;
5174 	struct nfsrv_descript nfsd, *nd = &nfsd;
5175 	int error, outcnt, i;
5176 	uint8_t *cp;
5177 
5178 	nfscl_reqstart(nd, NFSPROC_LAYOUTRETURN, nmp, fh, fhlen, NULL, NULL);
5179 	NFSM_BUILD(tl, uint32_t *, 4 * NFSX_UNSIGNED);
5180 	if (reclaim != 0)
5181 		*tl++ = newnfs_true;
5182 	else
5183 		*tl++ = newnfs_false;
5184 	*tl++ = txdr_unsigned(layouttype);
5185 	*tl++ = txdr_unsigned(iomode);
5186 	*tl = txdr_unsigned(layoutreturn);
5187 	if (layoutreturn == NFSLAYOUTRETURN_FILE) {
5188 		NFSM_BUILD(tl, uint32_t *, 2 * NFSX_HYPER + NFSX_STATEID +
5189 		    NFSX_UNSIGNED);
5190 		txdr_hyper(offset, tl);
5191 		tl += 2;
5192 		txdr_hyper(len, tl);
5193 		tl += 2;
5194 		NFSCL_DEBUG(4, "layoutret stseq=%d\n", (int)stateidp->seqid);
5195 		*tl++ = txdr_unsigned(stateidp->seqid);
5196 		*tl++ = stateidp->other[0];
5197 		*tl++ = stateidp->other[1];
5198 		*tl++ = stateidp->other[2];
5199 		*tl = txdr_unsigned(layoutcnt);
5200 		if (layoutcnt > 0) {
5201 			outcnt = NFSM_RNDUP(layoutcnt);
5202 			NFSM_BUILD(cp, uint8_t *, outcnt);
5203 			NFSBCOPY(layp, cp, layoutcnt);
5204 			cp += layoutcnt;
5205 			for (i = 0; i < (outcnt - layoutcnt); i++)
5206 				*cp++ = 0x0;
5207 		}
5208 	}
5209 	nd->nd_flag |= ND_USEGSSNAME;
5210 	error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
5211 	    NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
5212 	if (error != 0)
5213 		return (error);
5214 	if (nd->nd_repstat == 0) {
5215 		NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
5216 		if (*tl != 0) {
5217 			NFSM_DISSECT(tl, uint32_t *, NFSX_STATEID);
5218 			stateidp->seqid = fxdr_unsigned(uint32_t, *tl++);
5219 			stateidp->other[0] = *tl++;
5220 			stateidp->other[1] = *tl++;
5221 			stateidp->other[2] = *tl;
5222 		}
5223 	} else
5224 		error = nd->nd_repstat;
5225 nfsmout:
5226 	mbuf_freem(nd->nd_mrep);
5227 	return (error);
5228 }
5229 
5230 /*
5231  * Acquire a layout and devinfo, if possible. The caller must have acquired
5232  * a reference count on the nfsclclient structure before calling this.
5233  * Return the layout in lypp with a reference count on it, if successful.
5234  */
5235 static int
5236 nfsrpc_getlayout(struct nfsmount *nmp, vnode_t vp, struct nfsfh *nfhp,
5237     int iomode, uint32_t *notifybitsp, nfsv4stateid_t *stateidp, uint64_t off,
5238     struct nfscllayout **lypp, struct ucred *cred, NFSPROC_T *p)
5239 {
5240 	struct nfscllayout *lyp;
5241 	struct nfsclflayout *flp, *tflp;
5242 	struct nfscldevinfo *dip;
5243 	struct nfsclflayouthead flh;
5244 	int error = 0, islocked, layoutlen, recalled, retonclose;
5245 	nfsv4stateid_t stateid;
5246 	struct nfsclsession *tsep;
5247 
5248 	*lypp = NULL;
5249 	/*
5250 	 * If lyp is returned non-NULL, there will be a refcnt (shared lock)
5251 	 * on it, iff flp != NULL or a lock (exclusive lock) on it iff
5252 	 * flp == NULL.
5253 	 */
5254 	lyp = nfscl_getlayout(nmp->nm_clp, nfhp->nfh_fh, nfhp->nfh_len,
5255 	    off, &flp, &recalled);
5256 	islocked = 0;
5257 	if (lyp == NULL || flp == NULL) {
5258 		if (recalled != 0)
5259 			return (EIO);
5260 		LIST_INIT(&flh);
5261 		tsep = nfsmnt_mdssession(nmp);
5262 		layoutlen = tsep->nfsess_maxcache -
5263 		    (NFSX_STATEID + 3 * NFSX_UNSIGNED);
5264 		if (lyp == NULL) {
5265 			stateid.seqid = 0;
5266 			stateid.other[0] = stateidp->other[0];
5267 			stateid.other[1] = stateidp->other[1];
5268 			stateid.other[2] = stateidp->other[2];
5269 			error = nfsrpc_layoutget(nmp, nfhp->nfh_fh,
5270 			    nfhp->nfh_len, iomode, (uint64_t)0, UINT64_MAX,
5271 			    (uint64_t)0, layoutlen, &stateid, &retonclose,
5272 			    &flh, cred, p, NULL);
5273 		} else {
5274 			islocked = 1;
5275 			stateid.seqid = lyp->nfsly_stateid.seqid;
5276 			stateid.other[0] = lyp->nfsly_stateid.other[0];
5277 			stateid.other[1] = lyp->nfsly_stateid.other[1];
5278 			stateid.other[2] = lyp->nfsly_stateid.other[2];
5279 			error = nfsrpc_layoutget(nmp, nfhp->nfh_fh,
5280 			    nfhp->nfh_len, iomode, off, UINT64_MAX,
5281 			    (uint64_t)0, layoutlen, &stateid, &retonclose,
5282 			    &flh, cred, p, NULL);
5283 		}
5284 		if (error == 0)
5285 			LIST_FOREACH(tflp, &flh, nfsfl_list) {
5286 				error = nfscl_adddevinfo(nmp, NULL, tflp);
5287 				if (error != 0) {
5288 					error = nfsrpc_getdeviceinfo(nmp,
5289 					    tflp->nfsfl_dev,
5290 					    NFSLAYOUT_NFSV4_1_FILES,
5291 					    notifybitsp, &dip, cred, p);
5292 					if (error != 0)
5293 						break;
5294 					error = nfscl_adddevinfo(nmp, dip,
5295 					    tflp);
5296 					if (error != 0)
5297 						printf(
5298 						    "getlayout: cannot add\n");
5299 				}
5300 			}
5301 		if (error == 0) {
5302 			/*
5303 			 * nfscl_layout() always returns with the nfsly_lock
5304 			 * set to a refcnt (shared lock).
5305 			 */
5306 			error = nfscl_layout(nmp, vp, nfhp->nfh_fh,
5307 			    nfhp->nfh_len, &stateid, retonclose, &flh, &lyp,
5308 			    cred, p);
5309 			if (error == 0)
5310 				*lypp = lyp;
5311 		} else if (islocked != 0)
5312 			nfsv4_unlock(&lyp->nfsly_lock, 0);
5313 	} else
5314 		*lypp = lyp;
5315 	return (error);
5316 }
5317 
5318 /*
5319  * Do a TCP connection plus exchange id and create session.
5320  * If successful, a "struct nfsclds" is linked into the list for the
5321  * mount point and a pointer to it is returned.
5322  */
5323 static int
5324 nfsrpc_fillsa(struct nfsmount *nmp, struct sockaddr_storage *ssp,
5325     struct nfsclds **dspp, NFSPROC_T *p)
5326 {
5327 	struct sockaddr_in *msad, *sad, *ssd;
5328 	struct sockaddr_in6 *msad6, *sad6, *ssd6;
5329 	struct nfsclclient *clp;
5330 	struct nfssockreq *nrp;
5331 	struct nfsclds *dsp, *tdsp;
5332 	int error;
5333 	enum nfsclds_state retv;
5334 	uint32_t sequenceid;
5335 
5336 	KASSERT(nmp->nm_sockreq.nr_cred != NULL,
5337 	    ("nfsrpc_fillsa: NULL nr_cred"));
5338 	NFSLOCKCLSTATE();
5339 	clp = nmp->nm_clp;
5340 	NFSUNLOCKCLSTATE();
5341 	if (clp == NULL)
5342 		return (EPERM);
5343 	if (ssp->ss_family == AF_INET) {
5344 		ssd = (struct sockaddr_in *)ssp;
5345 		NFSLOCKMNT(nmp);
5346 
5347 		/*
5348 		 * Check to see if we already have a session for this
5349 		 * address that is usable for a DS.
5350 		 * Note that the MDS's address is in a different place
5351 		 * than the sessions already acquired for DS's.
5352 		 */
5353 		msad = (struct sockaddr_in *)nmp->nm_sockreq.nr_nam;
5354 		tdsp = TAILQ_FIRST(&nmp->nm_sess);
5355 		while (tdsp != NULL) {
5356 			if (msad != NULL && msad->sin_family == AF_INET &&
5357 			    ssd->sin_addr.s_addr == msad->sin_addr.s_addr &&
5358 			    ssd->sin_port == msad->sin_port &&
5359 			    (tdsp->nfsclds_flags & NFSCLDS_DS) != 0 &&
5360 			    tdsp->nfsclds_sess.nfsess_defunct == 0) {
5361 				*dspp = tdsp;
5362 				NFSUNLOCKMNT(nmp);
5363 				NFSCL_DEBUG(4, "fnd same addr\n");
5364 				return (0);
5365 			}
5366 			tdsp = TAILQ_NEXT(tdsp, nfsclds_list);
5367 			if (tdsp != NULL && tdsp->nfsclds_sockp != NULL)
5368 				msad = (struct sockaddr_in *)
5369 				    tdsp->nfsclds_sockp->nr_nam;
5370 			else
5371 				msad = NULL;
5372 		}
5373 		NFSUNLOCKMNT(nmp);
5374 
5375 		/* No IP address match, so look for new/trunked one. */
5376 		sad = malloc(sizeof(*sad), M_SONAME, M_WAITOK | M_ZERO);
5377 		sad->sin_len = sizeof(*sad);
5378 		sad->sin_family = AF_INET;
5379 		sad->sin_port = ssd->sin_port;
5380 		sad->sin_addr.s_addr = ssd->sin_addr.s_addr;
5381 		nrp = malloc(sizeof(*nrp), M_NFSSOCKREQ, M_WAITOK | M_ZERO);
5382 		nrp->nr_nam = (struct sockaddr *)sad;
5383 	} else if (ssp->ss_family == AF_INET6) {
5384 		ssd6 = (struct sockaddr_in6 *)ssp;
5385 		NFSLOCKMNT(nmp);
5386 
5387 		/*
5388 		 * Check to see if we already have a session for this
5389 		 * address that is usable for a DS.
5390 		 * Note that the MDS's address is in a different place
5391 		 * than the sessions already acquired for DS's.
5392 		 */
5393 		msad6 = (struct sockaddr_in6 *)nmp->nm_sockreq.nr_nam;
5394 		tdsp = TAILQ_FIRST(&nmp->nm_sess);
5395 		while (tdsp != NULL) {
5396 			if (msad6 != NULL && msad6->sin6_family == AF_INET6 &&
5397 			    IN6_ARE_ADDR_EQUAL(&ssd6->sin6_addr,
5398 			    &msad6->sin6_addr) &&
5399 			    ssd6->sin6_port == msad6->sin6_port &&
5400 			    (tdsp->nfsclds_flags & NFSCLDS_DS) != 0 &&
5401 			    tdsp->nfsclds_sess.nfsess_defunct == 0) {
5402 				*dspp = tdsp;
5403 				NFSUNLOCKMNT(nmp);
5404 				return (0);
5405 			}
5406 			tdsp = TAILQ_NEXT(tdsp, nfsclds_list);
5407 			if (tdsp != NULL && tdsp->nfsclds_sockp != NULL)
5408 				msad6 = (struct sockaddr_in6 *)
5409 				    tdsp->nfsclds_sockp->nr_nam;
5410 			else
5411 				msad6 = NULL;
5412 		}
5413 		NFSUNLOCKMNT(nmp);
5414 
5415 		/* No IP address match, so look for new/trunked one. */
5416 		sad6 = malloc(sizeof(*sad6), M_SONAME, M_WAITOK | M_ZERO);
5417 		sad6->sin6_len = sizeof(*sad6);
5418 		sad6->sin6_family = AF_INET6;
5419 		sad6->sin6_port = ssd6->sin6_port;
5420 		NFSBCOPY(&ssd6->sin6_addr, &sad6->sin6_addr,
5421 		    sizeof(struct in6_addr));
5422 		nrp = malloc(sizeof(*nrp), M_NFSSOCKREQ, M_WAITOK | M_ZERO);
5423 		nrp->nr_nam = (struct sockaddr *)sad6;
5424 	} else
5425 		return (EPERM);
5426 
5427 	nrp->nr_sotype = SOCK_STREAM;
5428 	mtx_init(&nrp->nr_mtx, "nfssock", NULL, MTX_DEF);
5429 	nrp->nr_prog = NFS_PROG;
5430 	nrp->nr_vers = NFS_VER4;
5431 
5432 	/*
5433 	 * Use the credentials that were used for the mount, which are
5434 	 * in nmp->nm_sockreq.nr_cred for newnfs_connect() etc.
5435 	 * Ref. counting the credentials with crhold() is probably not
5436 	 * necessary, since nm_sockreq.nr_cred won't be crfree()'d until
5437 	 * unmount, but I did it anyhow.
5438 	 */
5439 	nrp->nr_cred = crhold(nmp->nm_sockreq.nr_cred);
5440 	error = newnfs_connect(nmp, nrp, NULL, p, 0);
5441 	NFSCL_DEBUG(3, "DS connect=%d\n", error);
5442 
5443 	/* Now, do the exchangeid and create session. */
5444 	if (error == 0) {
5445 		error = nfsrpc_exchangeid(nmp, clp, nrp, NFSV4EXCH_USEPNFSDS,
5446 		    &dsp, nrp->nr_cred, p);
5447 		NFSCL_DEBUG(3, "DS exchangeid=%d\n", error);
5448 		if (error != 0)
5449 			newnfs_disconnect(nrp);
5450 	}
5451 	if (error == 0) {
5452 		dsp->nfsclds_sockp = nrp;
5453 		NFSLOCKMNT(nmp);
5454 		retv = nfscl_getsameserver(nmp, dsp, &tdsp);
5455 		NFSCL_DEBUG(3, "getsame ret=%d\n", retv);
5456 		if (retv == NFSDSP_USETHISSESSION) {
5457 			NFSUNLOCKMNT(nmp);
5458 			/*
5459 			 * If there is already a session for this server,
5460 			 * use it.
5461 			 */
5462 			(void)newnfs_disconnect(nrp);
5463 			nfscl_freenfsclds(dsp);
5464 			*dspp = tdsp;
5465 			return (0);
5466 		}
5467 		if (retv == NFSDSP_SEQTHISSESSION)
5468 			sequenceid = tdsp->nfsclds_sess.nfsess_sequenceid;
5469 		else
5470 			sequenceid = dsp->nfsclds_sess.nfsess_sequenceid;
5471 		NFSUNLOCKMNT(nmp);
5472 		error = nfsrpc_createsession(nmp, &dsp->nfsclds_sess,
5473 		    nrp, sequenceid, 0, nrp->nr_cred, p);
5474 		NFSCL_DEBUG(3, "DS createsess=%d\n", error);
5475 	} else {
5476 		NFSFREECRED(nrp->nr_cred);
5477 		NFSFREEMUTEX(&nrp->nr_mtx);
5478 		free(nrp->nr_nam, M_SONAME);
5479 		free(nrp, M_NFSSOCKREQ);
5480 	}
5481 	if (error == 0) {
5482 		NFSCL_DEBUG(3, "add DS session\n");
5483 		/*
5484 		 * Put it at the end of the list. That way the list
5485 		 * is ordered by when the entry was added. This matters
5486 		 * since the one done first is the one that should be
5487 		 * used for sequencid'ing any subsequent create sessions.
5488 		 */
5489 		NFSLOCKMNT(nmp);
5490 		TAILQ_INSERT_TAIL(&nmp->nm_sess, dsp, nfsclds_list);
5491 		NFSUNLOCKMNT(nmp);
5492 		*dspp = dsp;
5493 	} else if (dsp != NULL) {
5494 		newnfs_disconnect(nrp);
5495 		nfscl_freenfsclds(dsp);
5496 	}
5497 	return (error);
5498 }
5499 
5500 /*
5501  * Do the NFSv4.1 Reclaim Complete.
5502  */
5503 int
5504 nfsrpc_reclaimcomplete(struct nfsmount *nmp, struct ucred *cred, NFSPROC_T *p)
5505 {
5506 	uint32_t *tl;
5507 	struct nfsrv_descript nfsd;
5508 	struct nfsrv_descript *nd = &nfsd;
5509 	int error;
5510 
5511 	nfscl_reqstart(nd, NFSPROC_RECLAIMCOMPL, nmp, NULL, 0, NULL, NULL);
5512 	NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
5513 	*tl = newnfs_false;
5514 	nd->nd_flag |= ND_USEGSSNAME;
5515 	error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
5516 	    NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
5517 	if (error != 0)
5518 		return (error);
5519 	error = nd->nd_repstat;
5520 	mbuf_freem(nd->nd_mrep);
5521 	return (error);
5522 }
5523 
5524 /*
5525  * Initialize the slot tables for a session.
5526  */
5527 static void
5528 nfscl_initsessionslots(struct nfsclsession *sep)
5529 {
5530 	int i;
5531 
5532 	for (i = 0; i < NFSV4_CBSLOTS; i++) {
5533 		if (sep->nfsess_cbslots[i].nfssl_reply != NULL)
5534 			m_freem(sep->nfsess_cbslots[i].nfssl_reply);
5535 		NFSBZERO(&sep->nfsess_cbslots[i], sizeof(struct nfsslot));
5536 	}
5537 	for (i = 0; i < 64; i++)
5538 		sep->nfsess_slotseq[i] = 0;
5539 	sep->nfsess_slots = 0;
5540 }
5541 
5542 /*
5543  * Called to try and do an I/O operation via an NFSv4.1 Data Server (DS).
5544  */
5545 int
5546 nfscl_doiods(vnode_t vp, struct uio *uiop, int *iomode, int *must_commit,
5547     uint32_t rwaccess, struct ucred *cred, NFSPROC_T *p)
5548 {
5549 	struct nfsnode *np = VTONFS(vp);
5550 	struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
5551 	struct nfscllayout *layp;
5552 	struct nfscldevinfo *dip;
5553 	struct nfsclflayout *rflp;
5554 	nfsv4stateid_t stateid;
5555 	struct ucred *newcred;
5556 	uint64_t lastbyte, len, off, oresid, xfer;
5557 	int eof, error, iolaymode, recalled;
5558 	void *lckp;
5559 
5560 	if (!NFSHASPNFS(nmp) || nfscl_enablecallb == 0 || nfs_numnfscbd == 0 ||
5561 	    (np->n_flag & NNOLAYOUT) != 0)
5562 		return (EIO);
5563 	/* Now, get a reference cnt on the clientid for this mount. */
5564 	if (nfscl_getref(nmp) == 0)
5565 		return (EIO);
5566 
5567 	/* Find an appropriate stateid. */
5568 	newcred = NFSNEWCRED(cred);
5569 	error = nfscl_getstateid(vp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len,
5570 	    rwaccess, 1, newcred, p, &stateid, &lckp);
5571 	if (error != 0) {
5572 		NFSFREECRED(newcred);
5573 		nfscl_relref(nmp);
5574 		return (error);
5575 	}
5576 	/* Search for a layout for this file. */
5577 	off = uiop->uio_offset;
5578 	layp = nfscl_getlayout(nmp->nm_clp, np->n_fhp->nfh_fh,
5579 	    np->n_fhp->nfh_len, off, &rflp, &recalled);
5580 	if (layp == NULL || rflp == NULL) {
5581 		if (recalled != 0) {
5582 			NFSFREECRED(newcred);
5583 			nfscl_relref(nmp);
5584 			return (EIO);
5585 		}
5586 		if (layp != NULL) {
5587 			nfscl_rellayout(layp, (rflp == NULL) ? 1 : 0);
5588 			layp = NULL;
5589 		}
5590 		/* Try and get a Layout, if it is supported. */
5591 		if (rwaccess == NFSV4OPEN_ACCESSWRITE ||
5592 		    (np->n_flag & NWRITEOPENED) != 0)
5593 			iolaymode = NFSLAYOUTIOMODE_RW;
5594 		else
5595 			iolaymode = NFSLAYOUTIOMODE_READ;
5596 		error = nfsrpc_getlayout(nmp, vp, np->n_fhp, iolaymode,
5597 		    NULL, &stateid, off, &layp, newcred, p);
5598 		if (error != 0) {
5599 			NFSLOCKNODE(np);
5600 			np->n_flag |= NNOLAYOUT;
5601 			NFSUNLOCKNODE(np);
5602 			if (lckp != NULL)
5603 				nfscl_lockderef(lckp);
5604 			NFSFREECRED(newcred);
5605 			if (layp != NULL)
5606 				nfscl_rellayout(layp, 0);
5607 			nfscl_relref(nmp);
5608 			return (error);
5609 		}
5610 	}
5611 
5612 	/*
5613 	 * Loop around finding a layout that works for the first part of
5614 	 * this I/O operation, and then call the function that actually
5615 	 * does the RPC.
5616 	 */
5617 	eof = 0;
5618 	len = (uint64_t)uiop->uio_resid;
5619 	while (len > 0 && error == 0 && eof == 0) {
5620 		off = uiop->uio_offset;
5621 		error = nfscl_findlayoutforio(layp, off, rwaccess, &rflp);
5622 		if (error == 0) {
5623 			oresid = xfer = (uint64_t)uiop->uio_resid;
5624 			if (xfer > (rflp->nfsfl_end - rflp->nfsfl_off))
5625 				xfer = rflp->nfsfl_end - rflp->nfsfl_off;
5626 			dip = nfscl_getdevinfo(nmp->nm_clp, rflp->nfsfl_dev,
5627 			    rflp->nfsfl_devp);
5628 			if (dip != NULL) {
5629 				error = nfscl_doflayoutio(vp, uiop, iomode,
5630 				    must_commit, &eof, &stateid, rwaccess, dip,
5631 				    layp, rflp, off, xfer, newcred, p);
5632 				nfscl_reldevinfo(dip);
5633 				lastbyte = off + xfer - 1;
5634 				if (error == 0) {
5635 					NFSLOCKCLSTATE();
5636 					if (lastbyte > layp->nfsly_lastbyte)
5637 						layp->nfsly_lastbyte = lastbyte;
5638 					NFSUNLOCKCLSTATE();
5639 				} else if (error == NFSERR_OPENMODE &&
5640 				    rwaccess == NFSV4OPEN_ACCESSREAD) {
5641 					NFSLOCKMNT(nmp);
5642 					nmp->nm_state |= NFSSTA_OPENMODE;
5643 					NFSUNLOCKMNT(nmp);
5644 				}
5645 			} else
5646 				error = EIO;
5647 			if (error == 0)
5648 				len -= (oresid - (uint64_t)uiop->uio_resid);
5649 		}
5650 	}
5651 	if (lckp != NULL)
5652 		nfscl_lockderef(lckp);
5653 	NFSFREECRED(newcred);
5654 	nfscl_rellayout(layp, 0);
5655 	nfscl_relref(nmp);
5656 	return (error);
5657 }
5658 
5659 /*
5660  * Find a file layout that will handle the first bytes of the requested
5661  * range and return the information from it needed to to the I/O operation.
5662  */
5663 int
5664 nfscl_findlayoutforio(struct nfscllayout *lyp, uint64_t off, uint32_t rwaccess,
5665     struct nfsclflayout **retflpp)
5666 {
5667 	struct nfsclflayout *flp, *nflp, *rflp;
5668 	uint32_t rw;
5669 
5670 	rflp = NULL;
5671 	rw = rwaccess;
5672 	/* For reading, do the Read list first and then the Write list. */
5673 	do {
5674 		if (rw == NFSV4OPEN_ACCESSREAD)
5675 			flp = LIST_FIRST(&lyp->nfsly_flayread);
5676 		else
5677 			flp = LIST_FIRST(&lyp->nfsly_flayrw);
5678 		while (flp != NULL) {
5679 			nflp = LIST_NEXT(flp, nfsfl_list);
5680 			if (flp->nfsfl_off > off)
5681 				break;
5682 			if (flp->nfsfl_end > off &&
5683 			    (rflp == NULL || rflp->nfsfl_end < flp->nfsfl_end))
5684 				rflp = flp;
5685 			flp = nflp;
5686 		}
5687 		if (rw == NFSV4OPEN_ACCESSREAD)
5688 			rw = NFSV4OPEN_ACCESSWRITE;
5689 		else
5690 			rw = 0;
5691 	} while (rw != 0);
5692 	if (rflp != NULL) {
5693 		/* This one covers the most bytes starting at off. */
5694 		*retflpp = rflp;
5695 		return (0);
5696 	}
5697 	return (EIO);
5698 }
5699 
5700 /*
5701  * Do I/O using an NFSv4.1 file layout.
5702  */
5703 static int
5704 nfscl_doflayoutio(vnode_t vp, struct uio *uiop, int *iomode, int *must_commit,
5705     int *eofp, nfsv4stateid_t *stateidp, int rwflag, struct nfscldevinfo *dp,
5706     struct nfscllayout *lyp, struct nfsclflayout *flp, uint64_t off,
5707     uint64_t len, struct ucred *cred, NFSPROC_T *p)
5708 {
5709 	uint64_t io_off, rel_off, stripe_unit_size, transfer, xfer;
5710 	int commit_thru_mds, error = 0, stripe_index, stripe_pos;
5711 	struct nfsnode *np;
5712 	struct nfsfh *fhp;
5713 	struct nfsclds **dspp;
5714 
5715 	np = VTONFS(vp);
5716 	rel_off = off - flp->nfsfl_patoff;
5717 	stripe_unit_size = (flp->nfsfl_util >> 6) & 0x3ffffff;
5718 	stripe_pos = (rel_off / stripe_unit_size + flp->nfsfl_stripe1) %
5719 	    dp->nfsdi_stripecnt;
5720 	transfer = stripe_unit_size - (rel_off % stripe_unit_size);
5721 
5722 	/* Loop around, doing I/O for each stripe unit. */
5723 	while (len > 0 && error == 0) {
5724 		stripe_index = nfsfldi_stripeindex(dp, stripe_pos);
5725 		dspp = nfsfldi_addr(dp, stripe_index);
5726 		if (len > transfer)
5727 			xfer = transfer;
5728 		else
5729 			xfer = len;
5730 		if ((flp->nfsfl_util & NFSFLAYUTIL_DENSE) != 0) {
5731 			/* Dense layout. */
5732 			if (stripe_pos >= flp->nfsfl_fhcnt)
5733 				return (EIO);
5734 			fhp = flp->nfsfl_fh[stripe_pos];
5735 			io_off = (rel_off / (stripe_unit_size *
5736 			    dp->nfsdi_stripecnt)) * stripe_unit_size +
5737 			    rel_off % stripe_unit_size;
5738 		} else {
5739 			/* Sparse layout. */
5740 			if (flp->nfsfl_fhcnt > 1) {
5741 				if (stripe_index >= flp->nfsfl_fhcnt)
5742 					return (EIO);
5743 				fhp = flp->nfsfl_fh[stripe_index];
5744 			} else if (flp->nfsfl_fhcnt == 1)
5745 				fhp = flp->nfsfl_fh[0];
5746 			else
5747 				fhp = np->n_fhp;
5748 			io_off = off;
5749 		}
5750 		if ((flp->nfsfl_util & NFSFLAYUTIL_COMMIT_THRU_MDS) != 0)
5751 			commit_thru_mds = 1;
5752 		else
5753 			commit_thru_mds = 0;
5754 		if (rwflag == FREAD)
5755 			error = nfsrpc_readds(vp, uiop, stateidp, eofp, *dspp,
5756 			    io_off, xfer, fhp, cred, p);
5757 		else {
5758 			error = nfsrpc_writeds(vp, uiop, iomode, must_commit,
5759 			    stateidp, *dspp, io_off, xfer, fhp, commit_thru_mds,
5760 			    cred, p);
5761 			if (error == 0) {
5762 				NFSLOCKCLSTATE();
5763 				lyp->nfsly_flags |= NFSLY_WRITTEN;
5764 				NFSUNLOCKCLSTATE();
5765 			}
5766 		}
5767 		if (error == 0) {
5768 			transfer = stripe_unit_size;
5769 			stripe_pos = (stripe_pos + 1) % dp->nfsdi_stripecnt;
5770 			len -= xfer;
5771 			off += xfer;
5772 		}
5773 	}
5774 	return (error);
5775 }
5776 
5777 /*
5778  * The actual read RPC done to a DS.
5779  */
5780 static int
5781 nfsrpc_readds(vnode_t vp, struct uio *uiop, nfsv4stateid_t *stateidp, int *eofp,
5782     struct nfsclds *dsp, uint64_t io_off, int len, struct nfsfh *fhp,
5783     struct ucred *cred, NFSPROC_T *p)
5784 {
5785 	uint32_t *tl;
5786 	int error, retlen;
5787 	struct nfsrv_descript nfsd;
5788 	struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
5789 	struct nfsrv_descript *nd = &nfsd;
5790 	struct nfssockreq *nrp;
5791 
5792 	nd->nd_mrep = NULL;
5793 	nfscl_reqstart(nd, NFSPROC_READDS, nmp, fhp->nfh_fh, fhp->nfh_len,
5794 	    NULL, &dsp->nfsclds_sess);
5795 	nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSEQIDZERO);
5796 	NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED * 3);
5797 	txdr_hyper(io_off, tl);
5798 	*(tl + 2) = txdr_unsigned(len);
5799 	nrp = dsp->nfsclds_sockp;
5800 	if (nrp == NULL)
5801 		/* If NULL, use the MDS socket. */
5802 		nrp = &nmp->nm_sockreq;
5803 	error = newnfs_request(nd, nmp, NULL, nrp, vp, p, cred,
5804 	    NFS_PROG, NFS_VER4, NULL, 1, NULL, &dsp->nfsclds_sess);
5805 	if (error != 0)
5806 		return (error);
5807 	if (nd->nd_repstat != 0) {
5808 		error = nd->nd_repstat;
5809 		goto nfsmout;
5810 	}
5811 	NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
5812 	*eofp = fxdr_unsigned(int, *tl);
5813 	NFSM_STRSIZ(retlen, len);
5814 	error = nfsm_mbufuio(nd, uiop, retlen);
5815 nfsmout:
5816 	if (nd->nd_mrep != NULL)
5817 		mbuf_freem(nd->nd_mrep);
5818 	return (error);
5819 }
5820 
5821 /*
5822  * The actual write RPC done to a DS.
5823  */
5824 static int
5825 nfsrpc_writeds(vnode_t vp, struct uio *uiop, int *iomode, int *must_commit,
5826     nfsv4stateid_t *stateidp, struct nfsclds *dsp, uint64_t io_off, int len,
5827     struct nfsfh *fhp, int commit_thru_mds, struct ucred *cred, NFSPROC_T *p)
5828 {
5829 	uint32_t *tl;
5830 	struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
5831 	int error, rlen, commit, committed = NFSWRITE_FILESYNC;
5832 	int32_t backup;
5833 	struct nfsrv_descript nfsd;
5834 	struct nfsrv_descript *nd = &nfsd;
5835 	struct nfssockreq *nrp;
5836 
5837 	KASSERT(uiop->uio_iovcnt == 1, ("nfs: writerpc iovcnt > 1"));
5838 	nd->nd_mrep = NULL;
5839 	nfscl_reqstart(nd, NFSPROC_WRITEDS, nmp, fhp->nfh_fh, fhp->nfh_len,
5840 	    NULL, &dsp->nfsclds_sess);
5841 	nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSEQIDZERO);
5842 	NFSM_BUILD(tl, uint32_t *, NFSX_HYPER + 2 * NFSX_UNSIGNED);
5843 	txdr_hyper(io_off, tl);
5844 	tl += 2;
5845 	*tl++ = txdr_unsigned(*iomode);
5846 	*tl = txdr_unsigned(len);
5847 	nfsm_uiombuf(nd, uiop, len);
5848 	nrp = dsp->nfsclds_sockp;
5849 	if (nrp == NULL)
5850 		/* If NULL, use the MDS socket. */
5851 		nrp = &nmp->nm_sockreq;
5852 	error = newnfs_request(nd, nmp, NULL, nrp, vp, p, cred,
5853 	    NFS_PROG, NFS_VER4, NULL, 1, NULL, &dsp->nfsclds_sess);
5854 	if (error != 0)
5855 		return (error);
5856 	if (nd->nd_repstat != 0) {
5857 		/*
5858 		 * In case the rpc gets retried, roll
5859 		 * the uio fileds changed by nfsm_uiombuf()
5860 		 * back.
5861 		 */
5862 		uiop->uio_offset -= len;
5863 		uio_uio_resid_add(uiop, len);
5864 		uio_iov_base_add(uiop, -len);
5865 		uio_iov_len_add(uiop, len);
5866 		error = nd->nd_repstat;
5867 	} else {
5868 		NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED + NFSX_VERF);
5869 		rlen = fxdr_unsigned(int, *tl++);
5870 		if (rlen == 0) {
5871 			error = NFSERR_IO;
5872 			goto nfsmout;
5873 		} else if (rlen < len) {
5874 			backup = len - rlen;
5875 			uio_iov_base_add(uiop, -(backup));
5876 			uio_iov_len_add(uiop, backup);
5877 			uiop->uio_offset -= backup;
5878 			uio_uio_resid_add(uiop, backup);
5879 			len = rlen;
5880 		}
5881 		commit = fxdr_unsigned(int, *tl++);
5882 
5883 		/*
5884 		 * Return the lowest commitment level
5885 		 * obtained by any of the RPCs.
5886 		 */
5887 		if (committed == NFSWRITE_FILESYNC)
5888 			committed = commit;
5889 		else if (committed == NFSWRITE_DATASYNC &&
5890 		    commit == NFSWRITE_UNSTABLE)
5891 			committed = commit;
5892 		if (commit_thru_mds != 0) {
5893 			NFSLOCKMNT(nmp);
5894 			if (!NFSHASWRITEVERF(nmp)) {
5895 				NFSBCOPY(tl, nmp->nm_verf, NFSX_VERF);
5896 				NFSSETWRITEVERF(nmp);
5897 	    		} else if (NFSBCMP(tl, nmp->nm_verf, NFSX_VERF)) {
5898 				*must_commit = 1;
5899 				NFSBCOPY(tl, nmp->nm_verf, NFSX_VERF);
5900 			}
5901 			NFSUNLOCKMNT(nmp);
5902 		} else {
5903 			NFSLOCKDS(dsp);
5904 			if ((dsp->nfsclds_flags & NFSCLDS_HASWRITEVERF) == 0) {
5905 				NFSBCOPY(tl, dsp->nfsclds_verf, NFSX_VERF);
5906 				dsp->nfsclds_flags |= NFSCLDS_HASWRITEVERF;
5907 			} else if (NFSBCMP(tl, dsp->nfsclds_verf, NFSX_VERF)) {
5908 				*must_commit = 1;
5909 				NFSBCOPY(tl, dsp->nfsclds_verf, NFSX_VERF);
5910 			}
5911 			NFSUNLOCKDS(dsp);
5912 		}
5913 	}
5914 nfsmout:
5915 	if (nd->nd_mrep != NULL)
5916 		mbuf_freem(nd->nd_mrep);
5917 	*iomode = committed;
5918 	if (nd->nd_repstat != 0 && error == 0)
5919 		error = nd->nd_repstat;
5920 	return (error);
5921 }
5922 
5923 /*
5924  * Free up the nfsclds structure.
5925  */
5926 void
5927 nfscl_freenfsclds(struct nfsclds *dsp)
5928 {
5929 	int i;
5930 
5931 	if (dsp == NULL)
5932 		return;
5933 	if (dsp->nfsclds_sockp != NULL) {
5934 		NFSFREECRED(dsp->nfsclds_sockp->nr_cred);
5935 		NFSFREEMUTEX(&dsp->nfsclds_sockp->nr_mtx);
5936 		free(dsp->nfsclds_sockp->nr_nam, M_SONAME);
5937 		free(dsp->nfsclds_sockp, M_NFSSOCKREQ);
5938 	}
5939 	NFSFREEMUTEX(&dsp->nfsclds_mtx);
5940 	NFSFREEMUTEX(&dsp->nfsclds_sess.nfsess_mtx);
5941 	for (i = 0; i < NFSV4_CBSLOTS; i++) {
5942 		if (dsp->nfsclds_sess.nfsess_cbslots[i].nfssl_reply != NULL)
5943 			m_freem(
5944 			    dsp->nfsclds_sess.nfsess_cbslots[i].nfssl_reply);
5945 	}
5946 	free(dsp, M_NFSCLDS);
5947 }
5948 
5949 static enum nfsclds_state
5950 nfscl_getsameserver(struct nfsmount *nmp, struct nfsclds *newdsp,
5951     struct nfsclds **retdspp)
5952 {
5953 	struct nfsclds *dsp, *cur_dsp;
5954 
5955 	/*
5956 	 * Search the list of nfsclds structures for one with the same
5957 	 * server.
5958 	 */
5959 	cur_dsp = NULL;
5960 	TAILQ_FOREACH(dsp, &nmp->nm_sess, nfsclds_list) {
5961 		if (dsp->nfsclds_servownlen == newdsp->nfsclds_servownlen &&
5962 		    dsp->nfsclds_servownlen != 0 &&
5963 		    !NFSBCMP(dsp->nfsclds_serverown, newdsp->nfsclds_serverown,
5964 		    dsp->nfsclds_servownlen) &&
5965 		    dsp->nfsclds_sess.nfsess_defunct == 0) {
5966 			NFSCL_DEBUG(4, "fnd same fdsp=%p dsp=%p flg=0x%x\n",
5967 			    TAILQ_FIRST(&nmp->nm_sess), dsp,
5968 			    dsp->nfsclds_flags);
5969 			/* Server major id matches. */
5970 			if ((dsp->nfsclds_flags & NFSCLDS_DS) != 0) {
5971 				*retdspp = dsp;
5972 				return (NFSDSP_USETHISSESSION);
5973 			}
5974 
5975 			/*
5976 			 * Note the first match, so it can be used for
5977 			 * sequence'ing new sessions.
5978 			 */
5979 			if (cur_dsp == NULL)
5980 				cur_dsp = dsp;
5981 		}
5982 	}
5983 	if (cur_dsp != NULL) {
5984 		*retdspp = cur_dsp;
5985 		return (NFSDSP_SEQTHISSESSION);
5986 	}
5987 	return (NFSDSP_NOTFOUND);
5988 }
5989 
5990 #ifdef notyet
5991 /*
5992  * NFS commit rpc to a DS.
5993  */
5994 static int
5995 nfsrpc_commitds(vnode_t vp, uint64_t offset, int cnt, struct nfsclds *dsp,
5996     struct nfsfh *fhp, struct ucred *cred, NFSPROC_T *p, void *stuff)
5997 {
5998 	uint32_t *tl;
5999 	struct nfsrv_descript nfsd, *nd = &nfsd;
6000 	struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
6001 	struct nfssockreq *nrp;
6002 	int error;
6003 
6004 	nfscl_reqstart(nd, NFSPROC_COMMITDS, nmp, fhp->nfh_fh, fhp->nfh_len,
6005 	    NULL, &dsp->nfsclds_sess);
6006 	NFSM_BUILD(tl, uint32_t *, NFSX_HYPER + NFSX_UNSIGNED);
6007 	txdr_hyper(offset, tl);
6008 	tl += 2;
6009 	*tl = txdr_unsigned(cnt);
6010 	nrp = dsp->nfsclds_sockp;
6011 	if (nrp == NULL)
6012 		/* If NULL, use the MDS socket. */
6013 		nrp = &nmp->nm_sockreq;
6014 	error = newnfs_request(nd, nmp, NULL, nrp, vp, p, cred,
6015 	    NFS_PROG, NFS_VER4, NULL, 1, NULL, &dsp->nfsclds_sess);
6016 	if (error)
6017 		return (error);
6018 	if (nd->nd_repstat == 0) {
6019 		NFSM_DISSECT(tl, u_int32_t *, NFSX_VERF);
6020 		NFSLOCKDS(dsp);
6021 		if (NFSBCMP(tl, dsp->nfsclds_verf, NFSX_VERF)) {
6022 			NFSBCOPY(tl, dsp->nfsclds_verf, NFSX_VERF);
6023 			error = NFSERR_STALEWRITEVERF;
6024 		}
6025 		NFSUNLOCKDS(dsp);
6026 	}
6027 nfsmout:
6028 	if (error == 0 && nd->nd_repstat != 0)
6029 		error = nd->nd_repstat;
6030 	mbuf_freem(nd->nd_mrep);
6031 	return (error);
6032 }
6033 #endif
6034 
6035