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