xref: /freebsd/sys/fs/nfsclient/nfs_clrpcops.c (revision 5f0216bd883edee71bf81051e3c20505e4820903)
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 nfsmout:
3076 	if (nd->nd_mrep != NULL)
3077 		mbuf_freem(nd->nd_mrep);
3078 	return (error);
3079 }
3080 
3081 #ifndef APPLE
3082 /*
3083  * NFS V3 readdir plus RPC. Used in place of nfsrpc_readdir().
3084  * (Also used for NFS V4 when mount flag set.)
3085  * (ditto above w.r.t. multiple of DIRBLKSIZ, etc.)
3086  */
3087 APPLESTATIC int
3088 nfsrpc_readdirplus(vnode_t vp, struct uio *uiop, nfsuint64 *cookiep,
3089     struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp,
3090     int *eofp, void *stuff)
3091 {
3092 	int len, left;
3093 	struct dirent *dp = NULL;
3094 	u_int32_t *tl;
3095 	vnode_t newvp = NULLVP;
3096 	struct nfsrv_descript nfsd, *nd = &nfsd;
3097 	struct nameidata nami, *ndp = &nami;
3098 	struct componentname *cnp = &ndp->ni_cnd;
3099 	struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
3100 	struct nfsnode *dnp = VTONFS(vp), *np;
3101 	struct nfsvattr nfsva;
3102 	struct nfsfh *nfhp;
3103 	nfsquad_t cookie, ncookie;
3104 	int error = 0, tlen, more_dirs = 1, blksiz = 0, bigenough = 1;
3105 	int attrflag, tryformoredirs = 1, eof = 0, gotmnton = 0;
3106 	int isdotdot = 0, unlocknewvp = 0;
3107 	long dotfileid, dotdotfileid = 0, fileno = 0;
3108 	char *cp;
3109 	nfsattrbit_t attrbits, dattrbits;
3110 	size_t tresid;
3111 	u_int32_t *tl2 = NULL, fakefileno = 0xffffffff, rderr;
3112 	struct timespec dctime;
3113 
3114 	KASSERT(uiop->uio_iovcnt == 1 &&
3115 	    (uio_uio_resid(uiop) & (DIRBLKSIZ - 1)) == 0,
3116 	    ("nfs readdirplusrpc bad uio"));
3117 	timespecclear(&dctime);
3118 	*attrflagp = 0;
3119 	if (eofp != NULL)
3120 		*eofp = 0;
3121 	ndp->ni_dvp = vp;
3122 	nd->nd_mrep = NULL;
3123 	cookie.lval[0] = cookiep->nfsuquad[0];
3124 	cookie.lval[1] = cookiep->nfsuquad[1];
3125 	tresid = uio_uio_resid(uiop);
3126 
3127 	/*
3128 	 * For NFSv4, first create the "." and ".." entries.
3129 	 */
3130 	if (NFSHASNFSV4(nmp)) {
3131 		NFSGETATTR_ATTRBIT(&dattrbits);
3132 		NFSZERO_ATTRBIT(&attrbits);
3133 		NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_FILEID);
3134 		if (NFSISSET_ATTRBIT(&dnp->n_vattr.na_suppattr,
3135 		    NFSATTRBIT_MOUNTEDONFILEID)) {
3136 			NFSSETBIT_ATTRBIT(&attrbits,
3137 			    NFSATTRBIT_MOUNTEDONFILEID);
3138 			gotmnton = 1;
3139 		} else {
3140 			/*
3141 			 * Must fake it. Use the fileno, except when the
3142 			 * fsid is != to that of the directory. For that
3143 			 * case, generate a fake fileno that is not the same.
3144 			 */
3145 			NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_FSID);
3146 			gotmnton = 0;
3147 		}
3148 
3149 		/*
3150 		 * Joy, oh joy. For V4 we get to hand craft '.' and '..'.
3151 		 */
3152 		if (uiop->uio_offset == 0) {
3153 			NFSCL_REQSTART(nd, NFSPROC_LOOKUPP, vp);
3154 			NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
3155 			*tl++ = txdr_unsigned(NFSV4OP_GETFH);
3156 			*tl = txdr_unsigned(NFSV4OP_GETATTR);
3157 			(void) nfsrv_putattrbit(nd, &attrbits);
3158 			error = nfscl_request(nd, vp, p, cred, stuff);
3159 			if (error)
3160 			    return (error);
3161 			dotfileid = 0;	/* Fake out the compiler. */
3162 			if ((nd->nd_flag & ND_NOMOREDATA) == 0) {
3163 			    error = nfsm_loadattr(nd, &nfsva);
3164 			    if (error != 0)
3165 				goto nfsmout;
3166 			    dctime = nfsva.na_ctime;
3167 			    dotfileid = nfsva.na_fileid;
3168 			}
3169 			if (nd->nd_repstat == 0) {
3170 			    NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
3171 			    len = fxdr_unsigned(int, *(tl + 4));
3172 			    if (len > 0 && len <= NFSX_V4FHMAX)
3173 				error = nfsm_advance(nd, NFSM_RNDUP(len), -1);
3174 			    else
3175 				error = EPERM;
3176 			    if (!error) {
3177 				NFSM_DISSECT(tl, u_int32_t *, 2*NFSX_UNSIGNED);
3178 				nfsva.na_mntonfileno = 0xffffffff;
3179 				error = nfsv4_loadattr(nd, NULL, &nfsva, NULL,
3180 				    NULL, 0, NULL, NULL, NULL, NULL, NULL, 0,
3181 				    NULL, NULL, NULL, p, cred);
3182 				if (error) {
3183 				    dotdotfileid = dotfileid;
3184 				} else if (gotmnton) {
3185 				    if (nfsva.na_mntonfileno != 0xffffffff)
3186 					dotdotfileid = nfsva.na_mntonfileno;
3187 				    else
3188 					dotdotfileid = nfsva.na_fileid;
3189 				} else if (nfsva.na_filesid[0] ==
3190 				    dnp->n_vattr.na_filesid[0] &&
3191 				    nfsva.na_filesid[1] ==
3192 				    dnp->n_vattr.na_filesid[1]) {
3193 				    dotdotfileid = nfsva.na_fileid;
3194 				} else {
3195 				    do {
3196 					fakefileno--;
3197 				    } while (fakefileno ==
3198 					nfsva.na_fileid);
3199 				    dotdotfileid = fakefileno;
3200 				}
3201 			    }
3202 			} else if (nd->nd_repstat == NFSERR_NOENT) {
3203 			    /*
3204 			     * Lookupp returns NFSERR_NOENT when we are
3205 			     * at the root, so just use the current dir.
3206 			     */
3207 			    nd->nd_repstat = 0;
3208 			    dotdotfileid = dotfileid;
3209 			} else {
3210 			    error = nd->nd_repstat;
3211 			}
3212 			mbuf_freem(nd->nd_mrep);
3213 			if (error)
3214 			    return (error);
3215 			nd->nd_mrep = NULL;
3216 			dp = (struct dirent *)uio_iov_base(uiop);
3217 			dp->d_type = DT_DIR;
3218 			dp->d_fileno = dotfileid;
3219 			dp->d_namlen = 1;
3220 			dp->d_name[0] = '.';
3221 			dp->d_name[1] = '\0';
3222 			dp->d_reclen = DIRENT_SIZE(dp) + NFSX_HYPER;
3223 			/*
3224 			 * Just make these offset cookie 0.
3225 			 */
3226 			tl = (u_int32_t *)&dp->d_name[4];
3227 			*tl++ = 0;
3228 			*tl = 0;
3229 			blksiz += dp->d_reclen;
3230 			uio_uio_resid_add(uiop, -(dp->d_reclen));
3231 			uiop->uio_offset += dp->d_reclen;
3232 			uio_iov_base_add(uiop, dp->d_reclen);
3233 			uio_iov_len_add(uiop, -(dp->d_reclen));
3234 			dp = (struct dirent *)uio_iov_base(uiop);
3235 			dp->d_type = DT_DIR;
3236 			dp->d_fileno = dotdotfileid;
3237 			dp->d_namlen = 2;
3238 			dp->d_name[0] = '.';
3239 			dp->d_name[1] = '.';
3240 			dp->d_name[2] = '\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 		}
3254 		NFSREADDIRPLUS_ATTRBIT(&attrbits);
3255 		if (gotmnton)
3256 			NFSSETBIT_ATTRBIT(&attrbits,
3257 			    NFSATTRBIT_MOUNTEDONFILEID);
3258 	}
3259 
3260 	/*
3261 	 * Loop around doing readdir rpc's of size nm_readdirsize.
3262 	 * The stopping criteria is EOF or buffer full.
3263 	 */
3264 	while (more_dirs && bigenough) {
3265 		*attrflagp = 0;
3266 		NFSCL_REQSTART(nd, NFSPROC_READDIRPLUS, vp);
3267  		NFSM_BUILD(tl, u_int32_t *, 6 * NFSX_UNSIGNED);
3268 		*tl++ = cookie.lval[0];
3269 		*tl++ = cookie.lval[1];
3270 		if (cookie.qval == 0) {
3271 			*tl++ = 0;
3272 			*tl++ = 0;
3273 		} else {
3274 			NFSLOCKNODE(dnp);
3275 			*tl++ = dnp->n_cookieverf.nfsuquad[0];
3276 			*tl++ = dnp->n_cookieverf.nfsuquad[1];
3277 			NFSUNLOCKNODE(dnp);
3278 		}
3279 		*tl++ = txdr_unsigned(nmp->nm_readdirsize);
3280 		*tl = txdr_unsigned(nmp->nm_readdirsize);
3281 		if (nd->nd_flag & ND_NFSV4) {
3282 			(void) nfsrv_putattrbit(nd, &attrbits);
3283 			NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
3284 			*tl = txdr_unsigned(NFSV4OP_GETATTR);
3285 			(void) nfsrv_putattrbit(nd, &dattrbits);
3286 		}
3287 		error = nfscl_request(nd, vp, p, cred, stuff);
3288 		if (error)
3289 			return (error);
3290 		if (nd->nd_flag & ND_NFSV3)
3291 			error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
3292 		if (nd->nd_repstat || error) {
3293 			if (!error)
3294 				error = nd->nd_repstat;
3295 			goto nfsmout;
3296 		}
3297 		if ((nd->nd_flag & ND_NFSV3) != 0 && *attrflagp != 0)
3298 			dctime = nap->na_ctime;
3299 		NFSM_DISSECT(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
3300 		NFSLOCKNODE(dnp);
3301 		dnp->n_cookieverf.nfsuquad[0] = *tl++;
3302 		dnp->n_cookieverf.nfsuquad[1] = *tl++;
3303 		NFSUNLOCKNODE(dnp);
3304 		more_dirs = fxdr_unsigned(int, *tl);
3305 		if (!more_dirs)
3306 			tryformoredirs = 0;
3307 
3308 		/* loop thru the dir entries, doctoring them to 4bsd form */
3309 		while (more_dirs && bigenough) {
3310 			NFSM_DISSECT(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
3311 			if (nd->nd_flag & ND_NFSV4) {
3312 				ncookie.lval[0] = *tl++;
3313 				ncookie.lval[1] = *tl++;
3314 			} else {
3315 				fileno = fxdr_unsigned(long, *++tl);
3316 				tl++;
3317 			}
3318 			len = fxdr_unsigned(int, *tl);
3319 			if (len <= 0 || len > NFS_MAXNAMLEN) {
3320 				error = EBADRPC;
3321 				goto nfsmout;
3322 			}
3323 			tlen = NFSM_RNDUP(len);
3324 			if (tlen == len)
3325 				tlen += 4;  /* To ensure null termination */
3326 			left = DIRBLKSIZ - blksiz;
3327 			if ((tlen + DIRHDSIZ + NFSX_HYPER) > left) {
3328 				dp->d_reclen += left;
3329 				uio_iov_base_add(uiop, left);
3330 				uio_iov_len_add(uiop, -(left));
3331 				uio_uio_resid_add(uiop, -(left));
3332 				uiop->uio_offset += left;
3333 				blksiz = 0;
3334 			}
3335 			if ((tlen + DIRHDSIZ + NFSX_HYPER) > uio_uio_resid(uiop))
3336 				bigenough = 0;
3337 			if (bigenough) {
3338 				dp = (struct dirent *)uio_iov_base(uiop);
3339 				dp->d_namlen = len;
3340 				dp->d_reclen = tlen + DIRHDSIZ + NFSX_HYPER;
3341 				dp->d_type = DT_UNKNOWN;
3342 				blksiz += dp->d_reclen;
3343 				if (blksiz == DIRBLKSIZ)
3344 					blksiz = 0;
3345 				uio_uio_resid_add(uiop, -(DIRHDSIZ));
3346 				uiop->uio_offset += DIRHDSIZ;
3347 				uio_iov_base_add(uiop, DIRHDSIZ);
3348 				uio_iov_len_add(uiop, -(DIRHDSIZ));
3349 				cnp->cn_nameptr = uio_iov_base(uiop);
3350 				cnp->cn_namelen = len;
3351 				NFSCNHASHZERO(cnp);
3352 				error = nfsm_mbufuio(nd, uiop, len);
3353 				if (error)
3354 					goto nfsmout;
3355 				cp = uio_iov_base(uiop);
3356 				tlen -= len;
3357 				*cp = '\0';
3358 				cp += tlen;	/* points to cookie storage */
3359 				tl2 = (u_int32_t *)cp;
3360 				if (len == 2 && cnp->cn_nameptr[0] == '.' &&
3361 				    cnp->cn_nameptr[1] == '.')
3362 					isdotdot = 1;
3363 				else
3364 					isdotdot = 0;
3365 				uio_iov_base_add(uiop, (tlen + NFSX_HYPER));
3366 				uio_iov_len_add(uiop, -(tlen + NFSX_HYPER));
3367 				uio_uio_resid_add(uiop, -(tlen + NFSX_HYPER));
3368 				uiop->uio_offset += (tlen + NFSX_HYPER);
3369 			} else {
3370 				error = nfsm_advance(nd, NFSM_RNDUP(len), -1);
3371 				if (error)
3372 					goto nfsmout;
3373 			}
3374 			nfhp = NULL;
3375 			if (nd->nd_flag & ND_NFSV3) {
3376 				NFSM_DISSECT(tl, u_int32_t *, 3*NFSX_UNSIGNED);
3377 				ncookie.lval[0] = *tl++;
3378 				ncookie.lval[1] = *tl++;
3379 				attrflag = fxdr_unsigned(int, *tl);
3380 				if (attrflag) {
3381 				  error = nfsm_loadattr(nd, &nfsva);
3382 				  if (error)
3383 					goto nfsmout;
3384 				}
3385 				NFSM_DISSECT(tl,u_int32_t *,NFSX_UNSIGNED);
3386 				if (*tl) {
3387 					error = nfsm_getfh(nd, &nfhp);
3388 					if (error)
3389 					    goto nfsmout;
3390 				}
3391 				if (!attrflag && nfhp != NULL) {
3392 					FREE((caddr_t)nfhp, M_NFSFH);
3393 					nfhp = NULL;
3394 				}
3395 			} else {
3396 				rderr = 0;
3397 				nfsva.na_mntonfileno = 0xffffffff;
3398 				error = nfsv4_loadattr(nd, NULL, &nfsva, &nfhp,
3399 				    NULL, 0, NULL, NULL, NULL, NULL, NULL, 0,
3400 				    NULL, NULL, &rderr, p, cred);
3401 				if (error)
3402 					goto nfsmout;
3403 			}
3404 
3405 			if (bigenough) {
3406 			    if (nd->nd_flag & ND_NFSV4) {
3407 				if (rderr) {
3408 				    dp->d_fileno = 0;
3409 				} else if (gotmnton) {
3410 				    if (nfsva.na_mntonfileno != 0xffffffff)
3411 					dp->d_fileno = nfsva.na_mntonfileno;
3412 				    else
3413 					dp->d_fileno = nfsva.na_fileid;
3414 				} else if (nfsva.na_filesid[0] ==
3415 				    dnp->n_vattr.na_filesid[0] &&
3416 				    nfsva.na_filesid[1] ==
3417 				    dnp->n_vattr.na_filesid[1]) {
3418 				    dp->d_fileno = nfsva.na_fileid;
3419 				} else {
3420 				    do {
3421 					fakefileno--;
3422 				    } while (fakefileno ==
3423 					nfsva.na_fileid);
3424 				    dp->d_fileno = fakefileno;
3425 				}
3426 			    } else {
3427 				dp->d_fileno = fileno;
3428 			    }
3429 			    *tl2++ = cookiep->nfsuquad[0] = cookie.lval[0] =
3430 				ncookie.lval[0];
3431 			    *tl2 = cookiep->nfsuquad[1] = cookie.lval[1] =
3432 				ncookie.lval[1];
3433 
3434 			    if (nfhp != NULL) {
3435 				if (NFSRV_CMPFH(nfhp->nfh_fh, nfhp->nfh_len,
3436 				    dnp->n_fhp->nfh_fh, dnp->n_fhp->nfh_len)) {
3437 				    VREF(vp);
3438 				    newvp = vp;
3439 				    unlocknewvp = 0;
3440 				    FREE((caddr_t)nfhp, M_NFSFH);
3441 				    np = dnp;
3442 				} else if (isdotdot != 0) {
3443 				    /*
3444 				     * Skip doing a nfscl_nget() call for "..".
3445 				     * There's a race between acquiring the nfs
3446 				     * node here and lookups that look for the
3447 				     * directory being read (in the parent).
3448 				     * It would try to get a lock on ".." here,
3449 				     * owning the lock on the directory being
3450 				     * read. Lookup will hold the lock on ".."
3451 				     * and try to acquire the lock on the
3452 				     * directory being read.
3453 				     * If the directory is unlocked/relocked,
3454 				     * then there is a LOR with the buflock
3455 				     * vp is relocked.
3456 				     */
3457 				    free(nfhp, M_NFSFH);
3458 				} else {
3459 				    error = nfscl_nget(vnode_mount(vp), vp,
3460 				      nfhp, cnp, p, &np, NULL, LK_EXCLUSIVE);
3461 				    if (!error) {
3462 					newvp = NFSTOV(np);
3463 					unlocknewvp = 1;
3464 				    }
3465 				}
3466 				nfhp = NULL;
3467 				if (newvp != NULLVP) {
3468 				    error = nfscl_loadattrcache(&newvp,
3469 					&nfsva, NULL, NULL, 0, 0);
3470 				    if (error) {
3471 					if (unlocknewvp)
3472 					    vput(newvp);
3473 					else
3474 					    vrele(newvp);
3475 					goto nfsmout;
3476 				    }
3477 				    dp->d_type =
3478 					vtonfs_dtype(np->n_vattr.na_type);
3479 				    ndp->ni_vp = newvp;
3480 				    NFSCNHASH(cnp, HASHINIT);
3481 				    if (cnp->cn_namelen <= NCHNAMLEN &&
3482 					(newvp->v_type != VDIR ||
3483 					 dctime.tv_sec != 0)) {
3484 					cache_enter_time(ndp->ni_dvp,
3485 					    ndp->ni_vp, cnp,
3486 					    &nfsva.na_ctime,
3487 					    newvp->v_type != VDIR ? NULL :
3488 					    &dctime);
3489 				    }
3490 				    if (unlocknewvp)
3491 					vput(newvp);
3492 				    else
3493 					vrele(newvp);
3494 				    newvp = NULLVP;
3495 				}
3496 			    }
3497 			} else if (nfhp != NULL) {
3498 			    FREE((caddr_t)nfhp, M_NFSFH);
3499 			}
3500 			NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3501 			more_dirs = fxdr_unsigned(int, *tl);
3502 		}
3503 		/*
3504 		 * If at end of rpc data, get the eof boolean
3505 		 */
3506 		if (!more_dirs) {
3507 			NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3508 			eof = fxdr_unsigned(int, *tl);
3509 			if (tryformoredirs)
3510 				more_dirs = !eof;
3511 			if (nd->nd_flag & ND_NFSV4) {
3512 				error = nfscl_postop_attr(nd, nap, attrflagp,
3513 				    stuff);
3514 				if (error)
3515 					goto nfsmout;
3516 			}
3517 		}
3518 		mbuf_freem(nd->nd_mrep);
3519 		nd->nd_mrep = NULL;
3520 	}
3521 	/*
3522 	 * Fill last record, iff any, out to a multiple of DIRBLKSIZ
3523 	 * by increasing d_reclen for the last record.
3524 	 */
3525 	if (blksiz > 0) {
3526 		left = DIRBLKSIZ - blksiz;
3527 		dp->d_reclen += left;
3528 		uio_iov_base_add(uiop, left);
3529 		uio_iov_len_add(uiop, -(left));
3530 		uio_uio_resid_add(uiop, -(left));
3531 		uiop->uio_offset += left;
3532 	}
3533 
3534 	/*
3535 	 * If returning no data, assume end of file.
3536 	 * If not bigenough, return not end of file, since you aren't
3537 	 *    returning all the data
3538 	 * Otherwise, return the eof flag from the server.
3539 	 */
3540 	if (eofp != NULL) {
3541 		if (tresid == uio_uio_resid(uiop))
3542 			*eofp = 1;
3543 		else if (!bigenough)
3544 			*eofp = 0;
3545 		else
3546 			*eofp = eof;
3547 	}
3548 
3549 nfsmout:
3550 	if (nd->nd_mrep != NULL)
3551 		mbuf_freem(nd->nd_mrep);
3552 	return (error);
3553 }
3554 #endif	/* !APPLE */
3555 
3556 /*
3557  * Nfs commit rpc
3558  */
3559 APPLESTATIC int
3560 nfsrpc_commit(vnode_t vp, u_quad_t offset, int cnt, struct ucred *cred,
3561     NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, void *stuff)
3562 {
3563 	u_int32_t *tl;
3564 	struct nfsrv_descript nfsd, *nd = &nfsd;
3565 	nfsattrbit_t attrbits;
3566 	int error;
3567 	struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
3568 
3569 	*attrflagp = 0;
3570 	NFSCL_REQSTART(nd, NFSPROC_COMMIT, vp);
3571 	NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
3572 	txdr_hyper(offset, tl);
3573 	tl += 2;
3574 	*tl = txdr_unsigned(cnt);
3575 	if (nd->nd_flag & ND_NFSV4) {
3576 		/*
3577 		 * And do a Getattr op.
3578 		 */
3579 		NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
3580 		*tl = txdr_unsigned(NFSV4OP_GETATTR);
3581 		NFSGETATTR_ATTRBIT(&attrbits);
3582 		(void) nfsrv_putattrbit(nd, &attrbits);
3583 	}
3584 	error = nfscl_request(nd, vp, p, cred, stuff);
3585 	if (error)
3586 		return (error);
3587 	error = nfscl_wcc_data(nd, vp, nap, attrflagp, NULL, stuff);
3588 	if (!error && !nd->nd_repstat) {
3589 		NFSM_DISSECT(tl, u_int32_t *, NFSX_VERF);
3590 		NFSLOCKMNT(nmp);
3591 		if (NFSBCMP(nmp->nm_verf, tl, NFSX_VERF)) {
3592 			NFSBCOPY(tl, nmp->nm_verf, NFSX_VERF);
3593 			nd->nd_repstat = NFSERR_STALEWRITEVERF;
3594 		}
3595 		NFSUNLOCKMNT(nmp);
3596 		if (nd->nd_flag & ND_NFSV4)
3597 			error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
3598 	}
3599 nfsmout:
3600 	if (!error && nd->nd_repstat)
3601 		error = nd->nd_repstat;
3602 	mbuf_freem(nd->nd_mrep);
3603 	return (error);
3604 }
3605 
3606 /*
3607  * NFS byte range lock rpc.
3608  * (Mostly just calls one of the three lower level RPC routines.)
3609  */
3610 APPLESTATIC int
3611 nfsrpc_advlock(vnode_t vp, off_t size, int op, struct flock *fl,
3612     int reclaim, struct ucred *cred, NFSPROC_T *p, void *id, int flags)
3613 {
3614 	struct nfscllockowner *lp;
3615 	struct nfsclclient *clp;
3616 	struct nfsfh *nfhp;
3617 	struct nfsrv_descript nfsd, *nd = &nfsd;
3618 	struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
3619 	u_int64_t off, len;
3620 	off_t start, end;
3621 	u_int32_t clidrev = 0;
3622 	int error = 0, newone = 0, expireret = 0, retrycnt, donelocally;
3623 	int callcnt, dorpc;
3624 
3625 	/*
3626 	 * Convert the flock structure into a start and end and do POSIX
3627 	 * bounds checking.
3628 	 */
3629 	switch (fl->l_whence) {
3630 	case SEEK_SET:
3631 	case SEEK_CUR:
3632 		/*
3633 		 * Caller is responsible for adding any necessary offset
3634 		 * when SEEK_CUR is used.
3635 		 */
3636 		start = fl->l_start;
3637 		off = fl->l_start;
3638 		break;
3639 	case SEEK_END:
3640 		start = size + fl->l_start;
3641 		off = size + fl->l_start;
3642 		break;
3643 	default:
3644 		return (EINVAL);
3645 	};
3646 	if (start < 0)
3647 		return (EINVAL);
3648 	if (fl->l_len != 0) {
3649 		end = start + fl->l_len - 1;
3650 		if (end < start)
3651 			return (EINVAL);
3652 	}
3653 
3654 	len = fl->l_len;
3655 	if (len == 0)
3656 		len = NFS64BITSSET;
3657 	retrycnt = 0;
3658 	do {
3659 	    nd->nd_repstat = 0;
3660 	    if (op == F_GETLK) {
3661 		error = nfscl_getcl(vnode_mount(vp), cred, p, 1, &clp);
3662 		if (error)
3663 			return (error);
3664 		error = nfscl_lockt(vp, clp, off, len, fl, p, id, flags);
3665 		if (!error) {
3666 			clidrev = clp->nfsc_clientidrev;
3667 			error = nfsrpc_lockt(nd, vp, clp, off, len, fl, cred,
3668 			    p, id, flags);
3669 		} else if (error == -1) {
3670 			error = 0;
3671 		}
3672 		nfscl_clientrelease(clp);
3673 	    } else if (op == F_UNLCK && fl->l_type == F_UNLCK) {
3674 		/*
3675 		 * We must loop around for all lockowner cases.
3676 		 */
3677 		callcnt = 0;
3678 		error = nfscl_getcl(vnode_mount(vp), cred, p, 1, &clp);
3679 		if (error)
3680 			return (error);
3681 		do {
3682 		    error = nfscl_relbytelock(vp, off, len, cred, p, callcnt,
3683 			clp, id, flags, &lp, &dorpc);
3684 		    /*
3685 		     * If it returns a NULL lp, we're done.
3686 		     */
3687 		    if (lp == NULL) {
3688 			if (callcnt == 0)
3689 			    nfscl_clientrelease(clp);
3690 			else
3691 			    nfscl_releasealllocks(clp, vp, p, id, flags);
3692 			return (error);
3693 		    }
3694 		    if (nmp->nm_clp != NULL)
3695 			clidrev = nmp->nm_clp->nfsc_clientidrev;
3696 		    else
3697 			clidrev = 0;
3698 		    /*
3699 		     * If the server doesn't support Posix lock semantics,
3700 		     * only allow locks on the entire file, since it won't
3701 		     * handle overlapping byte ranges.
3702 		     * There might still be a problem when a lock
3703 		     * upgrade/downgrade (read<->write) occurs, since the
3704 		     * server "might" expect an unlock first?
3705 		     */
3706 		    if (dorpc && (lp->nfsl_open->nfso_posixlock ||
3707 			(off == 0 && len == NFS64BITSSET))) {
3708 			/*
3709 			 * Since the lock records will go away, we must
3710 			 * wait for grace and delay here.
3711 			 */
3712 			do {
3713 			    error = nfsrpc_locku(nd, nmp, lp, off, len,
3714 				NFSV4LOCKT_READ, cred, p, 0);
3715 			    if ((nd->nd_repstat == NFSERR_GRACE ||
3716 				 nd->nd_repstat == NFSERR_DELAY) &&
3717 				error == 0)
3718 				(void) nfs_catnap(PZERO, (int)nd->nd_repstat,
3719 				    "nfs_advlock");
3720 			} while ((nd->nd_repstat == NFSERR_GRACE ||
3721 			    nd->nd_repstat == NFSERR_DELAY) && error == 0);
3722 		    }
3723 		    callcnt++;
3724 		} while (error == 0 && nd->nd_repstat == 0);
3725 		nfscl_releasealllocks(clp, vp, p, id, flags);
3726 	    } else if (op == F_SETLK) {
3727 		error = nfscl_getbytelock(vp, off, len, fl->l_type, cred, p,
3728 		    NULL, 0, id, flags, NULL, NULL, &lp, &newone, &donelocally);
3729 		if (error || donelocally) {
3730 			return (error);
3731 		}
3732 		if (nmp->nm_clp != NULL)
3733 			clidrev = nmp->nm_clp->nfsc_clientidrev;
3734 		else
3735 			clidrev = 0;
3736 		nfhp = VTONFS(vp)->n_fhp;
3737 		if (!lp->nfsl_open->nfso_posixlock &&
3738 		    (off != 0 || len != NFS64BITSSET)) {
3739 			error = EINVAL;
3740 		} else {
3741 			error = nfsrpc_lock(nd, nmp, vp, nfhp->nfh_fh,
3742 			    nfhp->nfh_len, lp, newone, reclaim, off,
3743 			    len, fl->l_type, cred, p, 0);
3744 		}
3745 		if (!error)
3746 			error = nd->nd_repstat;
3747 		nfscl_lockrelease(lp, error, newone);
3748 	    } else {
3749 		error = EINVAL;
3750 	    }
3751 	    if (!error)
3752 	        error = nd->nd_repstat;
3753 	    if (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
3754 		error == NFSERR_STALEDONTRECOVER ||
3755 		error == NFSERR_STALECLIENTID || error == NFSERR_DELAY ||
3756 		error == NFSERR_BADSESSION) {
3757 		(void) nfs_catnap(PZERO, error, "nfs_advlock");
3758 	    } else if ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID)
3759 		&& clidrev != 0) {
3760 		expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
3761 		retrycnt++;
3762 	    }
3763 	} while (error == NFSERR_GRACE ||
3764 	    error == NFSERR_STALECLIENTID || error == NFSERR_DELAY ||
3765 	    error == NFSERR_STALEDONTRECOVER || error == NFSERR_STALESTATEID ||
3766 	    error == NFSERR_BADSESSION ||
3767 	    ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
3768 	     expireret == 0 && clidrev != 0 && retrycnt < 4));
3769 	if (error && retrycnt >= 4)
3770 		error = EIO;
3771 	return (error);
3772 }
3773 
3774 /*
3775  * The lower level routine for the LockT case.
3776  */
3777 APPLESTATIC int
3778 nfsrpc_lockt(struct nfsrv_descript *nd, vnode_t vp,
3779     struct nfsclclient *clp, u_int64_t off, u_int64_t len, struct flock *fl,
3780     struct ucred *cred, NFSPROC_T *p, void *id, int flags)
3781 {
3782 	u_int32_t *tl;
3783 	int error, type, size;
3784 	uint8_t own[NFSV4CL_LOCKNAMELEN + NFSX_V4FHMAX];
3785 	struct nfsnode *np;
3786 	struct nfsmount *nmp;
3787 
3788 	nmp = VFSTONFS(vp->v_mount);
3789 	NFSCL_REQSTART(nd, NFSPROC_LOCKT, vp);
3790 	NFSM_BUILD(tl, u_int32_t *, 7 * NFSX_UNSIGNED);
3791 	if (fl->l_type == F_RDLCK)
3792 		*tl++ = txdr_unsigned(NFSV4LOCKT_READ);
3793 	else
3794 		*tl++ = txdr_unsigned(NFSV4LOCKT_WRITE);
3795 	txdr_hyper(off, tl);
3796 	tl += 2;
3797 	txdr_hyper(len, tl);
3798 	tl += 2;
3799 	*tl++ = NFSMNT_MDSSESSION(nmp)->nfsess_clientid.lval[0];
3800 	*tl = NFSMNT_MDSSESSION(nmp)->nfsess_clientid.lval[1];
3801 	nfscl_filllockowner(id, own, flags);
3802 	np = VTONFS(vp);
3803 	NFSBCOPY(np->n_fhp->nfh_fh, &own[NFSV4CL_LOCKNAMELEN],
3804 	    np->n_fhp->nfh_len);
3805 	(void)nfsm_strtom(nd, own, NFSV4CL_LOCKNAMELEN + np->n_fhp->nfh_len);
3806 	error = nfscl_request(nd, vp, p, cred, NULL);
3807 	if (error)
3808 		return (error);
3809 	if (nd->nd_repstat == 0) {
3810 		fl->l_type = F_UNLCK;
3811 	} else if (nd->nd_repstat == NFSERR_DENIED) {
3812 		nd->nd_repstat = 0;
3813 		fl->l_whence = SEEK_SET;
3814 		NFSM_DISSECT(tl, u_int32_t *, 8 * NFSX_UNSIGNED);
3815 		fl->l_start = fxdr_hyper(tl);
3816 		tl += 2;
3817 		len = fxdr_hyper(tl);
3818 		tl += 2;
3819 		if (len == NFS64BITSSET)
3820 			fl->l_len = 0;
3821 		else
3822 			fl->l_len = len;
3823 		type = fxdr_unsigned(int, *tl++);
3824 		if (type == NFSV4LOCKT_WRITE)
3825 			fl->l_type = F_WRLCK;
3826 		else
3827 			fl->l_type = F_RDLCK;
3828 		/*
3829 		 * XXX For now, I have no idea what to do with the
3830 		 * conflicting lock_owner, so I'll just set the pid == 0
3831 		 * and skip over the lock_owner.
3832 		 */
3833 		fl->l_pid = (pid_t)0;
3834 		tl += 2;
3835 		size = fxdr_unsigned(int, *tl);
3836 		if (size < 0 || size > NFSV4_OPAQUELIMIT)
3837 			error = EBADRPC;
3838 		if (!error)
3839 			error = nfsm_advance(nd, NFSM_RNDUP(size), -1);
3840 	} else if (nd->nd_repstat == NFSERR_STALECLIENTID ||
3841 	    nd->nd_repstat == NFSERR_BADSESSION)
3842 		nfscl_initiate_recovery(clp);
3843 nfsmout:
3844 	mbuf_freem(nd->nd_mrep);
3845 	return (error);
3846 }
3847 
3848 /*
3849  * Lower level function that performs the LockU RPC.
3850  */
3851 static int
3852 nfsrpc_locku(struct nfsrv_descript *nd, struct nfsmount *nmp,
3853     struct nfscllockowner *lp, u_int64_t off, u_int64_t len,
3854     u_int32_t type, struct ucred *cred, NFSPROC_T *p, int syscred)
3855 {
3856 	u_int32_t *tl;
3857 	int error;
3858 
3859 	nfscl_reqstart(nd, NFSPROC_LOCKU, nmp, lp->nfsl_open->nfso_fh,
3860 	    lp->nfsl_open->nfso_fhlen, NULL, NULL);
3861 	NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID + 6 * NFSX_UNSIGNED);
3862 	*tl++ = txdr_unsigned(type);
3863 	*tl = txdr_unsigned(lp->nfsl_seqid);
3864 	if (nfstest_outofseq &&
3865 	    (arc4random() % nfstest_outofseq) == 0)
3866 		*tl = txdr_unsigned(lp->nfsl_seqid + 1);
3867 	tl++;
3868 	if (NFSHASNFSV4N(nmp))
3869 		*tl++ = 0;
3870 	else
3871 		*tl++ = lp->nfsl_stateid.seqid;
3872 	*tl++ = lp->nfsl_stateid.other[0];
3873 	*tl++ = lp->nfsl_stateid.other[1];
3874 	*tl++ = lp->nfsl_stateid.other[2];
3875 	txdr_hyper(off, tl);
3876 	tl += 2;
3877 	txdr_hyper(len, tl);
3878 	if (syscred)
3879 		nd->nd_flag |= ND_USEGSSNAME;
3880 	error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
3881 	    NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
3882 	NFSCL_INCRSEQID(lp->nfsl_seqid, nd);
3883 	if (error)
3884 		return (error);
3885 	if (nd->nd_repstat == 0) {
3886 		NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID);
3887 		lp->nfsl_stateid.seqid = *tl++;
3888 		lp->nfsl_stateid.other[0] = *tl++;
3889 		lp->nfsl_stateid.other[1] = *tl++;
3890 		lp->nfsl_stateid.other[2] = *tl;
3891 	} else if (nd->nd_repstat == NFSERR_STALESTATEID ||
3892 	    nd->nd_repstat == NFSERR_BADSESSION)
3893 		nfscl_initiate_recovery(lp->nfsl_open->nfso_own->nfsow_clp);
3894 nfsmout:
3895 	mbuf_freem(nd->nd_mrep);
3896 	return (error);
3897 }
3898 
3899 /*
3900  * The actual Lock RPC.
3901  */
3902 APPLESTATIC int
3903 nfsrpc_lock(struct nfsrv_descript *nd, struct nfsmount *nmp, vnode_t vp,
3904     u_int8_t *nfhp, int fhlen, struct nfscllockowner *lp, int newone,
3905     int reclaim, u_int64_t off, u_int64_t len, short type, struct ucred *cred,
3906     NFSPROC_T *p, int syscred)
3907 {
3908 	u_int32_t *tl;
3909 	int error, size;
3910 	uint8_t own[NFSV4CL_LOCKNAMELEN + NFSX_V4FHMAX];
3911 
3912 	nfscl_reqstart(nd, NFSPROC_LOCK, nmp, nfhp, fhlen, NULL, NULL);
3913 	NFSM_BUILD(tl, u_int32_t *, 7 * NFSX_UNSIGNED);
3914 	if (type == F_RDLCK)
3915 		*tl++ = txdr_unsigned(NFSV4LOCKT_READ);
3916 	else
3917 		*tl++ = txdr_unsigned(NFSV4LOCKT_WRITE);
3918 	*tl++ = txdr_unsigned(reclaim);
3919 	txdr_hyper(off, tl);
3920 	tl += 2;
3921 	txdr_hyper(len, tl);
3922 	tl += 2;
3923 	if (newone) {
3924 	    *tl = newnfs_true;
3925 	    NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID +
3926 		2 * NFSX_UNSIGNED + NFSX_HYPER);
3927 	    *tl++ = txdr_unsigned(lp->nfsl_open->nfso_own->nfsow_seqid);
3928 	    if (NFSHASNFSV4N(nmp))
3929 		*tl++ = 0;
3930 	    else
3931 		*tl++ = lp->nfsl_open->nfso_stateid.seqid;
3932 	    *tl++ = lp->nfsl_open->nfso_stateid.other[0];
3933 	    *tl++ = lp->nfsl_open->nfso_stateid.other[1];
3934 	    *tl++ = lp->nfsl_open->nfso_stateid.other[2];
3935 	    *tl++ = txdr_unsigned(lp->nfsl_seqid);
3936 	    *tl++ = NFSMNT_MDSSESSION(nmp)->nfsess_clientid.lval[0];
3937 	    *tl = NFSMNT_MDSSESSION(nmp)->nfsess_clientid.lval[1];
3938 	    NFSBCOPY(lp->nfsl_owner, own, NFSV4CL_LOCKNAMELEN);
3939 	    NFSBCOPY(nfhp, &own[NFSV4CL_LOCKNAMELEN], fhlen);
3940 	    (void)nfsm_strtom(nd, own, NFSV4CL_LOCKNAMELEN + fhlen);
3941 	} else {
3942 	    *tl = newnfs_false;
3943 	    NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID + NFSX_UNSIGNED);
3944 	    if (NFSHASNFSV4N(nmp))
3945 		*tl++ = 0;
3946 	    else
3947 		*tl++ = lp->nfsl_stateid.seqid;
3948 	    *tl++ = lp->nfsl_stateid.other[0];
3949 	    *tl++ = lp->nfsl_stateid.other[1];
3950 	    *tl++ = lp->nfsl_stateid.other[2];
3951 	    *tl = txdr_unsigned(lp->nfsl_seqid);
3952 	    if (nfstest_outofseq &&
3953 		(arc4random() % nfstest_outofseq) == 0)
3954 		    *tl = txdr_unsigned(lp->nfsl_seqid + 1);
3955 	}
3956 	if (syscred)
3957 		nd->nd_flag |= ND_USEGSSNAME;
3958 	error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, vp, p, cred,
3959 	    NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
3960 	if (error)
3961 		return (error);
3962 	if (newone)
3963 	    NFSCL_INCRSEQID(lp->nfsl_open->nfso_own->nfsow_seqid, nd);
3964 	NFSCL_INCRSEQID(lp->nfsl_seqid, nd);
3965 	if (nd->nd_repstat == 0) {
3966 		NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID);
3967 		lp->nfsl_stateid.seqid = *tl++;
3968 		lp->nfsl_stateid.other[0] = *tl++;
3969 		lp->nfsl_stateid.other[1] = *tl++;
3970 		lp->nfsl_stateid.other[2] = *tl;
3971 	} else if (nd->nd_repstat == NFSERR_DENIED) {
3972 		NFSM_DISSECT(tl, u_int32_t *, 8 * NFSX_UNSIGNED);
3973 		size = fxdr_unsigned(int, *(tl + 7));
3974 		if (size < 0 || size > NFSV4_OPAQUELIMIT)
3975 			error = EBADRPC;
3976 		if (!error)
3977 			error = nfsm_advance(nd, NFSM_RNDUP(size), -1);
3978 	} else if (nd->nd_repstat == NFSERR_STALESTATEID ||
3979 	    nd->nd_repstat == NFSERR_BADSESSION)
3980 		nfscl_initiate_recovery(lp->nfsl_open->nfso_own->nfsow_clp);
3981 nfsmout:
3982 	mbuf_freem(nd->nd_mrep);
3983 	return (error);
3984 }
3985 
3986 /*
3987  * nfs statfs rpc
3988  * (always called with the vp for the mount point)
3989  */
3990 APPLESTATIC int
3991 nfsrpc_statfs(vnode_t vp, struct nfsstatfs *sbp, struct nfsfsinfo *fsp,
3992     struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp,
3993     void *stuff)
3994 {
3995 	u_int32_t *tl = NULL;
3996 	struct nfsrv_descript nfsd, *nd = &nfsd;
3997 	struct nfsmount *nmp;
3998 	nfsattrbit_t attrbits;
3999 	int error;
4000 
4001 	*attrflagp = 0;
4002 	nmp = VFSTONFS(vnode_mount(vp));
4003 	if (NFSHASNFSV4(nmp)) {
4004 		/*
4005 		 * For V4, you actually do a getattr.
4006 		 */
4007 		NFSCL_REQSTART(nd, NFSPROC_GETATTR, vp);
4008 		NFSSTATFS_GETATTRBIT(&attrbits);
4009 		(void) nfsrv_putattrbit(nd, &attrbits);
4010 		nd->nd_flag |= ND_USEGSSNAME;
4011 		error = nfscl_request(nd, vp, p, cred, stuff);
4012 		if (error)
4013 			return (error);
4014 		if (nd->nd_repstat == 0) {
4015 			error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0,
4016 			    NULL, NULL, sbp, fsp, NULL, 0, NULL, NULL, NULL, p,
4017 			    cred);
4018 			if (!error) {
4019 				nmp->nm_fsid[0] = nap->na_filesid[0];
4020 				nmp->nm_fsid[1] = nap->na_filesid[1];
4021 				NFSSETHASSETFSID(nmp);
4022 				*attrflagp = 1;
4023 			}
4024 		} else {
4025 			error = nd->nd_repstat;
4026 		}
4027 		if (error)
4028 			goto nfsmout;
4029 	} else {
4030 		NFSCL_REQSTART(nd, NFSPROC_FSSTAT, vp);
4031 		error = nfscl_request(nd, vp, p, cred, stuff);
4032 		if (error)
4033 			return (error);
4034 		if (nd->nd_flag & ND_NFSV3) {
4035 			error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
4036 			if (error)
4037 				goto nfsmout;
4038 		}
4039 		if (nd->nd_repstat) {
4040 			error = nd->nd_repstat;
4041 			goto nfsmout;
4042 		}
4043 		NFSM_DISSECT(tl, u_int32_t *,
4044 		    NFSX_STATFS(nd->nd_flag & ND_NFSV3));
4045 	}
4046 	if (NFSHASNFSV3(nmp)) {
4047 		sbp->sf_tbytes = fxdr_hyper(tl); tl += 2;
4048 		sbp->sf_fbytes = fxdr_hyper(tl); tl += 2;
4049 		sbp->sf_abytes = fxdr_hyper(tl); tl += 2;
4050 		sbp->sf_tfiles = fxdr_hyper(tl); tl += 2;
4051 		sbp->sf_ffiles = fxdr_hyper(tl); tl += 2;
4052 		sbp->sf_afiles = fxdr_hyper(tl); tl += 2;
4053 		sbp->sf_invarsec = fxdr_unsigned(u_int32_t, *tl);
4054 	} else if (NFSHASNFSV4(nmp) == 0) {
4055 		sbp->sf_tsize = fxdr_unsigned(u_int32_t, *tl++);
4056 		sbp->sf_bsize = fxdr_unsigned(u_int32_t, *tl++);
4057 		sbp->sf_blocks = fxdr_unsigned(u_int32_t, *tl++);
4058 		sbp->sf_bfree = fxdr_unsigned(u_int32_t, *tl++);
4059 		sbp->sf_bavail = fxdr_unsigned(u_int32_t, *tl);
4060 	}
4061 nfsmout:
4062 	mbuf_freem(nd->nd_mrep);
4063 	return (error);
4064 }
4065 
4066 /*
4067  * nfs pathconf rpc
4068  */
4069 APPLESTATIC int
4070 nfsrpc_pathconf(vnode_t vp, struct nfsv3_pathconf *pc,
4071     struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp,
4072     void *stuff)
4073 {
4074 	struct nfsrv_descript nfsd, *nd = &nfsd;
4075 	struct nfsmount *nmp;
4076 	u_int32_t *tl;
4077 	nfsattrbit_t attrbits;
4078 	int error;
4079 
4080 	*attrflagp = 0;
4081 	nmp = VFSTONFS(vnode_mount(vp));
4082 	if (NFSHASNFSV4(nmp)) {
4083 		/*
4084 		 * For V4, you actually do a getattr.
4085 		 */
4086 		NFSCL_REQSTART(nd, NFSPROC_GETATTR, vp);
4087 		NFSPATHCONF_GETATTRBIT(&attrbits);
4088 		(void) nfsrv_putattrbit(nd, &attrbits);
4089 		nd->nd_flag |= ND_USEGSSNAME;
4090 		error = nfscl_request(nd, vp, p, cred, stuff);
4091 		if (error)
4092 			return (error);
4093 		if (nd->nd_repstat == 0) {
4094 			error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0,
4095 			    pc, NULL, NULL, NULL, NULL, 0, NULL, NULL, NULL, p,
4096 			    cred);
4097 			if (!error)
4098 				*attrflagp = 1;
4099 		} else {
4100 			error = nd->nd_repstat;
4101 		}
4102 	} else {
4103 		NFSCL_REQSTART(nd, NFSPROC_PATHCONF, vp);
4104 		error = nfscl_request(nd, vp, p, cred, stuff);
4105 		if (error)
4106 			return (error);
4107 		error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
4108 		if (nd->nd_repstat && !error)
4109 			error = nd->nd_repstat;
4110 		if (!error) {
4111 			NFSM_DISSECT(tl, u_int32_t *, NFSX_V3PATHCONF);
4112 			pc->pc_linkmax = fxdr_unsigned(u_int32_t, *tl++);
4113 			pc->pc_namemax = fxdr_unsigned(u_int32_t, *tl++);
4114 			pc->pc_notrunc = fxdr_unsigned(u_int32_t, *tl++);
4115 			pc->pc_chownrestricted =
4116 			    fxdr_unsigned(u_int32_t, *tl++);
4117 			pc->pc_caseinsensitive =
4118 			    fxdr_unsigned(u_int32_t, *tl++);
4119 			pc->pc_casepreserving = fxdr_unsigned(u_int32_t, *tl);
4120 		}
4121 	}
4122 nfsmout:
4123 	mbuf_freem(nd->nd_mrep);
4124 	return (error);
4125 }
4126 
4127 /*
4128  * nfs version 3 fsinfo rpc call
4129  */
4130 APPLESTATIC int
4131 nfsrpc_fsinfo(vnode_t vp, struct nfsfsinfo *fsp, struct ucred *cred,
4132     NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, void *stuff)
4133 {
4134 	u_int32_t *tl;
4135 	struct nfsrv_descript nfsd, *nd = &nfsd;
4136 	int error;
4137 
4138 	*attrflagp = 0;
4139 	NFSCL_REQSTART(nd, NFSPROC_FSINFO, vp);
4140 	error = nfscl_request(nd, vp, p, cred, stuff);
4141 	if (error)
4142 		return (error);
4143 	error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
4144 	if (nd->nd_repstat && !error)
4145 		error = nd->nd_repstat;
4146 	if (!error) {
4147 		NFSM_DISSECT(tl, u_int32_t *, NFSX_V3FSINFO);
4148 		fsp->fs_rtmax = fxdr_unsigned(u_int32_t, *tl++);
4149 		fsp->fs_rtpref = fxdr_unsigned(u_int32_t, *tl++);
4150 		fsp->fs_rtmult = fxdr_unsigned(u_int32_t, *tl++);
4151 		fsp->fs_wtmax = fxdr_unsigned(u_int32_t, *tl++);
4152 		fsp->fs_wtpref = fxdr_unsigned(u_int32_t, *tl++);
4153 		fsp->fs_wtmult = fxdr_unsigned(u_int32_t, *tl++);
4154 		fsp->fs_dtpref = fxdr_unsigned(u_int32_t, *tl++);
4155 		fsp->fs_maxfilesize = fxdr_hyper(tl);
4156 		tl += 2;
4157 		fxdr_nfsv3time(tl, &fsp->fs_timedelta);
4158 		tl += 2;
4159 		fsp->fs_properties = fxdr_unsigned(u_int32_t, *tl);
4160 	}
4161 nfsmout:
4162 	mbuf_freem(nd->nd_mrep);
4163 	return (error);
4164 }
4165 
4166 /*
4167  * This function performs the Renew RPC.
4168  */
4169 APPLESTATIC int
4170 nfsrpc_renew(struct nfsclclient *clp, struct nfsclds *dsp, struct ucred *cred,
4171     NFSPROC_T *p)
4172 {
4173 	u_int32_t *tl;
4174 	struct nfsrv_descript nfsd;
4175 	struct nfsrv_descript *nd = &nfsd;
4176 	struct nfsmount *nmp;
4177 	int error;
4178 	struct nfssockreq *nrp;
4179 
4180 	nmp = clp->nfsc_nmp;
4181 	if (nmp == NULL)
4182 		return (0);
4183 	nfscl_reqstart(nd, NFSPROC_RENEW, nmp, NULL, 0, NULL,
4184 	    &dsp->nfsclds_sess);
4185 	if (!NFSHASNFSV4N(nmp)) {
4186 		/* NFSv4.1 just uses a Sequence Op and not a Renew. */
4187 		NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
4188 		*tl++ = NFSMNT_MDSSESSION(nmp)->nfsess_clientid.lval[0];
4189 		*tl = NFSMNT_MDSSESSION(nmp)->nfsess_clientid.lval[1];
4190 	}
4191 	nrp = dsp->nfsclds_sockp;
4192 	if (nrp == NULL)
4193 		/* If NULL, use the MDS socket. */
4194 		nrp = &nmp->nm_sockreq;
4195 	nd->nd_flag |= ND_USEGSSNAME;
4196 	error = newnfs_request(nd, nmp, NULL, nrp, NULL, p, cred,
4197 	    NFS_PROG, NFS_VER4, NULL, 1, NULL, &dsp->nfsclds_sess);
4198 	if (error)
4199 		return (error);
4200 	error = nd->nd_repstat;
4201 	mbuf_freem(nd->nd_mrep);
4202 	return (error);
4203 }
4204 
4205 /*
4206  * This function performs the Releaselockowner RPC.
4207  */
4208 APPLESTATIC int
4209 nfsrpc_rellockown(struct nfsmount *nmp, struct nfscllockowner *lp,
4210     uint8_t *fh, int fhlen, struct ucred *cred, NFSPROC_T *p)
4211 {
4212 	struct nfsrv_descript nfsd, *nd = &nfsd;
4213 	u_int32_t *tl;
4214 	int error;
4215 	uint8_t own[NFSV4CL_LOCKNAMELEN + NFSX_V4FHMAX];
4216 
4217 	if (NFSHASNFSV4N(nmp)) {
4218 		/* For NFSv4.1, do a FreeStateID. */
4219 		nfscl_reqstart(nd, NFSPROC_FREESTATEID, nmp, NULL, 0, NULL,
4220 		    NULL);
4221 		nfsm_stateidtom(nd, &lp->nfsl_stateid, NFSSTATEID_PUTSTATEID);
4222 	} else {
4223 		nfscl_reqstart(nd, NFSPROC_RELEASELCKOWN, nmp, NULL, 0, NULL,
4224 		    NULL);
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 		NFSBCOPY(lp->nfsl_owner, own, NFSV4CL_LOCKNAMELEN);
4229 		NFSBCOPY(fh, &own[NFSV4CL_LOCKNAMELEN], fhlen);
4230 		(void)nfsm_strtom(nd, own, NFSV4CL_LOCKNAMELEN + fhlen);
4231 	}
4232 	nd->nd_flag |= ND_USEGSSNAME;
4233 	error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4234 	    NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4235 	if (error)
4236 		return (error);
4237 	error = nd->nd_repstat;
4238 	mbuf_freem(nd->nd_mrep);
4239 	return (error);
4240 }
4241 
4242 /*
4243  * This function performs the Compound to get the mount pt FH.
4244  */
4245 APPLESTATIC int
4246 nfsrpc_getdirpath(struct nfsmount *nmp, u_char *dirpath, struct ucred *cred,
4247     NFSPROC_T *p)
4248 {
4249 	u_int32_t *tl;
4250 	struct nfsrv_descript nfsd;
4251 	struct nfsrv_descript *nd = &nfsd;
4252 	u_char *cp, *cp2;
4253 	int error, cnt, len, setnil;
4254 	u_int32_t *opcntp;
4255 
4256 	nfscl_reqstart(nd, NFSPROC_PUTROOTFH, nmp, NULL, 0, &opcntp, NULL);
4257 	cp = dirpath;
4258 	cnt = 0;
4259 	do {
4260 		setnil = 0;
4261 		while (*cp == '/')
4262 			cp++;
4263 		cp2 = cp;
4264 		while (*cp2 != '\0' && *cp2 != '/')
4265 			cp2++;
4266 		if (*cp2 == '/') {
4267 			setnil = 1;
4268 			*cp2 = '\0';
4269 		}
4270 		if (cp2 != cp) {
4271 			NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
4272 			*tl = txdr_unsigned(NFSV4OP_LOOKUP);
4273 			nfsm_strtom(nd, cp, strlen(cp));
4274 			cnt++;
4275 		}
4276 		if (setnil)
4277 			*cp2++ = '/';
4278 		cp = cp2;
4279 	} while (*cp != '\0');
4280 	if (NFSHASNFSV4N(nmp))
4281 		/* Has a Sequence Op done by nfscl_reqstart(). */
4282 		*opcntp = txdr_unsigned(3 + cnt);
4283 	else
4284 		*opcntp = txdr_unsigned(2 + cnt);
4285 	NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
4286 	*tl = txdr_unsigned(NFSV4OP_GETFH);
4287 	nd->nd_flag |= ND_USEGSSNAME;
4288 	error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4289 		NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4290 	if (error)
4291 		return (error);
4292 	if (nd->nd_repstat == 0) {
4293 		NFSM_DISSECT(tl, u_int32_t *, (3 + 2 * cnt) * NFSX_UNSIGNED);
4294 		tl += (2 + 2 * cnt);
4295 		if ((len = fxdr_unsigned(int, *tl)) <= 0 ||
4296 			len > NFSX_FHMAX) {
4297 			nd->nd_repstat = NFSERR_BADXDR;
4298 		} else {
4299 			nd->nd_repstat = nfsrv_mtostr(nd, nmp->nm_fh, len);
4300 			if (nd->nd_repstat == 0)
4301 				nmp->nm_fhsize = len;
4302 		}
4303 	}
4304 	error = nd->nd_repstat;
4305 nfsmout:
4306 	mbuf_freem(nd->nd_mrep);
4307 	return (error);
4308 }
4309 
4310 /*
4311  * This function performs the Delegreturn RPC.
4312  */
4313 APPLESTATIC int
4314 nfsrpc_delegreturn(struct nfscldeleg *dp, struct ucred *cred,
4315     struct nfsmount *nmp, NFSPROC_T *p, int syscred)
4316 {
4317 	u_int32_t *tl;
4318 	struct nfsrv_descript nfsd;
4319 	struct nfsrv_descript *nd = &nfsd;
4320 	int error;
4321 
4322 	nfscl_reqstart(nd, NFSPROC_DELEGRETURN, nmp, dp->nfsdl_fh,
4323 	    dp->nfsdl_fhlen, NULL, NULL);
4324 	NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID);
4325 	if (NFSHASNFSV4N(nmp))
4326 		*tl++ = 0;
4327 	else
4328 		*tl++ = dp->nfsdl_stateid.seqid;
4329 	*tl++ = dp->nfsdl_stateid.other[0];
4330 	*tl++ = dp->nfsdl_stateid.other[1];
4331 	*tl = dp->nfsdl_stateid.other[2];
4332 	if (syscred)
4333 		nd->nd_flag |= ND_USEGSSNAME;
4334 	error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4335 	    NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4336 	if (error)
4337 		return (error);
4338 	error = nd->nd_repstat;
4339 	mbuf_freem(nd->nd_mrep);
4340 	return (error);
4341 }
4342 
4343 /*
4344  * nfs getacl call.
4345  */
4346 APPLESTATIC int
4347 nfsrpc_getacl(vnode_t vp, struct ucred *cred, NFSPROC_T *p,
4348     struct acl *aclp, void *stuff)
4349 {
4350 	struct nfsrv_descript nfsd, *nd = &nfsd;
4351 	int error;
4352 	nfsattrbit_t attrbits;
4353 	struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
4354 
4355 	if (nfsrv_useacl == 0 || !NFSHASNFSV4(nmp))
4356 		return (EOPNOTSUPP);
4357 	NFSCL_REQSTART(nd, NFSPROC_GETACL, vp);
4358 	NFSZERO_ATTRBIT(&attrbits);
4359 	NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_ACL);
4360 	(void) nfsrv_putattrbit(nd, &attrbits);
4361 	error = nfscl_request(nd, vp, p, cred, stuff);
4362 	if (error)
4363 		return (error);
4364 	if (!nd->nd_repstat)
4365 		error = nfsv4_loadattr(nd, vp, NULL, NULL, NULL, 0, NULL,
4366 		    NULL, NULL, NULL, aclp, 0, NULL, NULL, NULL, p, cred);
4367 	else
4368 		error = nd->nd_repstat;
4369 	mbuf_freem(nd->nd_mrep);
4370 	return (error);
4371 }
4372 
4373 /*
4374  * nfs setacl call.
4375  */
4376 APPLESTATIC int
4377 nfsrpc_setacl(vnode_t vp, struct ucred *cred, NFSPROC_T *p,
4378     struct acl *aclp, void *stuff)
4379 {
4380 	int error;
4381 	struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
4382 
4383 	if (nfsrv_useacl == 0 || !NFSHASNFSV4(nmp))
4384 		return (EOPNOTSUPP);
4385 	error = nfsrpc_setattr(vp, NULL, aclp, cred, p, NULL, NULL, stuff);
4386 	return (error);
4387 }
4388 
4389 /*
4390  * nfs setacl call.
4391  */
4392 static int
4393 nfsrpc_setaclrpc(vnode_t vp, struct ucred *cred, NFSPROC_T *p,
4394     struct acl *aclp, nfsv4stateid_t *stateidp, void *stuff)
4395 {
4396 	struct nfsrv_descript nfsd, *nd = &nfsd;
4397 	int error;
4398 	nfsattrbit_t attrbits;
4399 	struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
4400 
4401 	if (!NFSHASNFSV4(nmp))
4402 		return (EOPNOTSUPP);
4403 	NFSCL_REQSTART(nd, NFSPROC_SETACL, vp);
4404 	nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID);
4405 	NFSZERO_ATTRBIT(&attrbits);
4406 	NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_ACL);
4407 	(void) nfsv4_fillattr(nd, vnode_mount(vp), vp, aclp, NULL, NULL, 0,
4408 	    &attrbits, NULL, NULL, 0, 0, 0, 0, (uint64_t)0);
4409 	error = nfscl_request(nd, vp, p, cred, stuff);
4410 	if (error)
4411 		return (error);
4412 	/* Don't care about the pre/postop attributes */
4413 	mbuf_freem(nd->nd_mrep);
4414 	return (nd->nd_repstat);
4415 }
4416 
4417 /*
4418  * Do the NFSv4.1 Exchange ID.
4419  */
4420 int
4421 nfsrpc_exchangeid(struct nfsmount *nmp, struct nfsclclient *clp,
4422     struct nfssockreq *nrp, uint32_t exchflags, struct nfsclds **dspp,
4423     struct ucred *cred, NFSPROC_T *p)
4424 {
4425 	uint32_t *tl, v41flags;
4426 	struct nfsrv_descript nfsd;
4427 	struct nfsrv_descript *nd = &nfsd;
4428 	struct nfsclds *dsp;
4429 	struct timespec verstime;
4430 	int error, len;
4431 
4432 	*dspp = NULL;
4433 	nfscl_reqstart(nd, NFSPROC_EXCHANGEID, nmp, NULL, 0, NULL, NULL);
4434 	NFSM_BUILD(tl, uint32_t *, 2 * NFSX_UNSIGNED);
4435 	*tl++ = txdr_unsigned(nfsboottime.tv_sec);	/* Client owner */
4436 	*tl = txdr_unsigned(clp->nfsc_rev);
4437 	(void) nfsm_strtom(nd, clp->nfsc_id, clp->nfsc_idlen);
4438 
4439 	NFSM_BUILD(tl, uint32_t *, 3 * NFSX_UNSIGNED);
4440 	*tl++ = txdr_unsigned(exchflags);
4441 	*tl++ = txdr_unsigned(NFSV4EXCH_SP4NONE);
4442 
4443 	/* Set the implementation id4 */
4444 	*tl = txdr_unsigned(1);
4445 	(void) nfsm_strtom(nd, "freebsd.org", strlen("freebsd.org"));
4446 	(void) nfsm_strtom(nd, version, strlen(version));
4447 	NFSM_BUILD(tl, uint32_t *, NFSX_V4TIME);
4448 	verstime.tv_sec = 1293840000;		/* Jan 1, 2011 */
4449 	verstime.tv_nsec = 0;
4450 	txdr_nfsv4time(&verstime, tl);
4451 	nd->nd_flag |= ND_USEGSSNAME;
4452 	error = newnfs_request(nd, nmp, NULL, nrp, NULL, p, cred,
4453 	    NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4454 	NFSCL_DEBUG(1, "exchangeid err=%d reps=%d\n", error,
4455 	    (int)nd->nd_repstat);
4456 	if (error != 0)
4457 		return (error);
4458 	if (nd->nd_repstat == 0) {
4459 		NFSM_DISSECT(tl, uint32_t *, 6 * NFSX_UNSIGNED + NFSX_HYPER);
4460 		len = fxdr_unsigned(int, *(tl + 7));
4461 		if (len < 0 || len > NFSV4_OPAQUELIMIT) {
4462 			error = NFSERR_BADXDR;
4463 			goto nfsmout;
4464 		}
4465 		dsp = malloc(sizeof(struct nfsclds) + len, M_NFSCLDS,
4466 		    M_WAITOK | M_ZERO);
4467 		dsp->nfsclds_expire = NFSD_MONOSEC + clp->nfsc_renew;
4468 		dsp->nfsclds_servownlen = len;
4469 		dsp->nfsclds_sess.nfsess_clientid.lval[0] = *tl++;
4470 		dsp->nfsclds_sess.nfsess_clientid.lval[1] = *tl++;
4471 		dsp->nfsclds_sess.nfsess_sequenceid =
4472 		    fxdr_unsigned(uint32_t, *tl++);
4473 		v41flags = fxdr_unsigned(uint32_t, *tl);
4474 		if ((v41flags & NFSV4EXCH_USEPNFSMDS) != 0 &&
4475 		    NFSHASPNFSOPT(nmp)) {
4476 			NFSCL_DEBUG(1, "set PNFS\n");
4477 			NFSLOCKMNT(nmp);
4478 			nmp->nm_state |= NFSSTA_PNFS;
4479 			NFSUNLOCKMNT(nmp);
4480 			dsp->nfsclds_flags |= NFSCLDS_MDS;
4481 		}
4482 		if ((v41flags & NFSV4EXCH_USEPNFSDS) != 0)
4483 			dsp->nfsclds_flags |= NFSCLDS_DS;
4484 		if (len > 0)
4485 			nd->nd_repstat = nfsrv_mtostr(nd,
4486 			    dsp->nfsclds_serverown, len);
4487 		if (nd->nd_repstat == 0) {
4488 			mtx_init(&dsp->nfsclds_mtx, "nfsds", NULL, MTX_DEF);
4489 			mtx_init(&dsp->nfsclds_sess.nfsess_mtx, "nfssession",
4490 			    NULL, MTX_DEF);
4491 			nfscl_initsessionslots(&dsp->nfsclds_sess);
4492 			*dspp = dsp;
4493 		} else
4494 			free(dsp, M_NFSCLDS);
4495 	}
4496 	error = nd->nd_repstat;
4497 nfsmout:
4498 	mbuf_freem(nd->nd_mrep);
4499 	return (error);
4500 }
4501 
4502 /*
4503  * Do the NFSv4.1 Create Session.
4504  */
4505 int
4506 nfsrpc_createsession(struct nfsmount *nmp, struct nfsclsession *sep,
4507     struct nfssockreq *nrp, uint32_t sequenceid, int mds, struct ucred *cred,
4508     NFSPROC_T *p)
4509 {
4510 	uint32_t crflags, *tl;
4511 	struct nfsrv_descript nfsd;
4512 	struct nfsrv_descript *nd = &nfsd;
4513 	int error, irdcnt;
4514 
4515 	nfscl_reqstart(nd, NFSPROC_CREATESESSION, nmp, NULL, 0, NULL, NULL);
4516 	NFSM_BUILD(tl, uint32_t *, 4 * NFSX_UNSIGNED);
4517 	*tl++ = sep->nfsess_clientid.lval[0];
4518 	*tl++ = sep->nfsess_clientid.lval[1];
4519 	*tl++ = txdr_unsigned(sequenceid);
4520 	crflags = (NFSMNT_RDONLY(nmp->nm_mountp) ? 0 : NFSV4CRSESS_PERSIST);
4521 	if (nfscl_enablecallb != 0 && nfs_numnfscbd > 0)
4522 		crflags |= NFSV4CRSESS_CONNBACKCHAN;
4523 	*tl = txdr_unsigned(crflags);
4524 
4525 	/* Fill in fore channel attributes. */
4526 	NFSM_BUILD(tl, uint32_t *, 7 * NFSX_UNSIGNED);
4527 	*tl++ = 0;				/* Header pad size */
4528 	*tl++ = txdr_unsigned(100000);		/* Max request size */
4529 	*tl++ = txdr_unsigned(100000);		/* Max response size */
4530 	*tl++ = txdr_unsigned(4096);		/* Max response size cached */
4531 	*tl++ = txdr_unsigned(20);		/* Max operations */
4532 	*tl++ = txdr_unsigned(64);		/* Max slots */
4533 	*tl = 0;				/* No rdma ird */
4534 
4535 	/* Fill in back channel attributes. */
4536 	NFSM_BUILD(tl, uint32_t *, 7 * NFSX_UNSIGNED);
4537 	*tl++ = 0;				/* Header pad size */
4538 	*tl++ = txdr_unsigned(10000);		/* Max request size */
4539 	*tl++ = txdr_unsigned(10000);		/* Max response size */
4540 	*tl++ = txdr_unsigned(4096);		/* Max response size cached */
4541 	*tl++ = txdr_unsigned(4);		/* Max operations */
4542 	*tl++ = txdr_unsigned(NFSV4_CBSLOTS);	/* Max slots */
4543 	*tl = 0;				/* No rdma ird */
4544 
4545 	NFSM_BUILD(tl, uint32_t *, 8 * NFSX_UNSIGNED);
4546 	*tl++ = txdr_unsigned(NFS_CALLBCKPROG);	/* Call back prog # */
4547 
4548 	/* Allow AUTH_SYS callbacks as uid, gid == 0. */
4549 	*tl++ = txdr_unsigned(1);		/* Auth_sys only */
4550 	*tl++ = txdr_unsigned(AUTH_SYS);	/* AUTH_SYS type */
4551 	*tl++ = txdr_unsigned(nfsboottime.tv_sec); /* time stamp */
4552 	*tl++ = 0;				/* Null machine name */
4553 	*tl++ = 0;				/* Uid == 0 */
4554 	*tl++ = 0;				/* Gid == 0 */
4555 	*tl = 0;				/* No additional gids */
4556 	nd->nd_flag |= ND_USEGSSNAME;
4557 	error = newnfs_request(nd, nmp, NULL, nrp, NULL, p, cred, NFS_PROG,
4558 	    NFS_VER4, NULL, 1, NULL, NULL);
4559 	if (error != 0)
4560 		return (error);
4561 	if (nd->nd_repstat == 0) {
4562 		NFSM_DISSECT(tl, uint32_t *, NFSX_V4SESSIONID +
4563 		    2 * NFSX_UNSIGNED);
4564 		bcopy(tl, sep->nfsess_sessionid, NFSX_V4SESSIONID);
4565 		tl += NFSX_V4SESSIONID / NFSX_UNSIGNED;
4566 		sep->nfsess_sequenceid = fxdr_unsigned(uint32_t, *tl++);
4567 		crflags = fxdr_unsigned(uint32_t, *tl);
4568 		if ((crflags & NFSV4CRSESS_PERSIST) != 0 && mds != 0) {
4569 			NFSLOCKMNT(nmp);
4570 			nmp->nm_state |= NFSSTA_SESSPERSIST;
4571 			NFSUNLOCKMNT(nmp);
4572 		}
4573 
4574 		/* Get the fore channel slot count. */
4575 		NFSM_DISSECT(tl, uint32_t *, 7 * NFSX_UNSIGNED);
4576 		tl += 3;		/* Skip the other counts. */
4577 		sep->nfsess_maxcache = fxdr_unsigned(int, *tl++);
4578 		tl++;
4579 		sep->nfsess_foreslots = fxdr_unsigned(uint16_t, *tl++);
4580 		NFSCL_DEBUG(4, "fore slots=%d\n", (int)sep->nfsess_foreslots);
4581 		irdcnt = fxdr_unsigned(int, *tl);
4582 		if (irdcnt > 0)
4583 			NFSM_DISSECT(tl, uint32_t *, irdcnt * NFSX_UNSIGNED);
4584 
4585 		/* and the back channel slot count. */
4586 		NFSM_DISSECT(tl, uint32_t *, 7 * NFSX_UNSIGNED);
4587 		tl += 5;
4588 		sep->nfsess_backslots = fxdr_unsigned(uint16_t, *tl);
4589 		NFSCL_DEBUG(4, "back slots=%d\n", (int)sep->nfsess_backslots);
4590 	}
4591 	error = nd->nd_repstat;
4592 nfsmout:
4593 	mbuf_freem(nd->nd_mrep);
4594 	return (error);
4595 }
4596 
4597 /*
4598  * Do the NFSv4.1 Destroy Session.
4599  */
4600 int
4601 nfsrpc_destroysession(struct nfsmount *nmp, struct nfsclclient *clp,
4602     struct ucred *cred, NFSPROC_T *p)
4603 {
4604 	uint32_t *tl;
4605 	struct nfsrv_descript nfsd;
4606 	struct nfsrv_descript *nd = &nfsd;
4607 	int error;
4608 
4609 	nfscl_reqstart(nd, NFSPROC_DESTROYSESSION, nmp, NULL, 0, NULL, NULL);
4610 	NFSM_BUILD(tl, uint32_t *, NFSX_V4SESSIONID);
4611 	bcopy(NFSMNT_MDSSESSION(nmp)->nfsess_sessionid, tl, NFSX_V4SESSIONID);
4612 	nd->nd_flag |= ND_USEGSSNAME;
4613 	error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4614 	    NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4615 	if (error != 0)
4616 		return (error);
4617 	error = nd->nd_repstat;
4618 	mbuf_freem(nd->nd_mrep);
4619 	return (error);
4620 }
4621 
4622 /*
4623  * Do the NFSv4.1 Destroy Client.
4624  */
4625 int
4626 nfsrpc_destroyclient(struct nfsmount *nmp, struct nfsclclient *clp,
4627     struct ucred *cred, NFSPROC_T *p)
4628 {
4629 	uint32_t *tl;
4630 	struct nfsrv_descript nfsd;
4631 	struct nfsrv_descript *nd = &nfsd;
4632 	int error;
4633 
4634 	nfscl_reqstart(nd, NFSPROC_DESTROYCLIENT, nmp, NULL, 0, NULL, NULL);
4635 	NFSM_BUILD(tl, uint32_t *, 2 * NFSX_UNSIGNED);
4636 	*tl++ = NFSMNT_MDSSESSION(nmp)->nfsess_clientid.lval[0];
4637 	*tl = NFSMNT_MDSSESSION(nmp)->nfsess_clientid.lval[1];
4638 	nd->nd_flag |= ND_USEGSSNAME;
4639 	error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4640 	    NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4641 	if (error != 0)
4642 		return (error);
4643 	error = nd->nd_repstat;
4644 	mbuf_freem(nd->nd_mrep);
4645 	return (error);
4646 }
4647 
4648 /*
4649  * Do the NFSv4.1 LayoutGet.
4650  */
4651 int
4652 nfsrpc_layoutget(struct nfsmount *nmp, uint8_t *fhp, int fhlen, int iomode,
4653     uint64_t offset, uint64_t len, uint64_t minlen, int layoutlen,
4654     nfsv4stateid_t *stateidp, int *retonclosep, struct nfsclflayouthead *flhp,
4655     struct ucred *cred, NFSPROC_T *p, void *stuff)
4656 {
4657 	uint32_t *tl;
4658 	struct nfsrv_descript nfsd, *nd = &nfsd;
4659 	struct nfsfh *nfhp;
4660 	struct nfsclflayout *flp, *prevflp, *tflp;
4661 	int cnt, error, gotiomode, fhcnt, nfhlen, i, j;
4662 	uint8_t *cp;
4663 	uint64_t retlen;
4664 
4665 	flp = NULL;
4666 	gotiomode = -1;
4667 	nfscl_reqstart(nd, NFSPROC_LAYOUTGET, nmp, fhp, fhlen, NULL, NULL);
4668 	NFSM_BUILD(tl, uint32_t *, 4 * NFSX_UNSIGNED + 3 * NFSX_HYPER +
4669 	    NFSX_STATEID);
4670 	*tl++ = newnfs_false;		/* Don't signal availability. */
4671 	*tl++ = txdr_unsigned(NFSLAYOUT_NFSV4_1_FILES);
4672 	*tl++ = txdr_unsigned(iomode);
4673 	txdr_hyper(offset, tl);
4674 	tl += 2;
4675 	txdr_hyper(len, tl);
4676 	tl += 2;
4677 	txdr_hyper(minlen, tl);
4678 	tl += 2;
4679 	*tl++ = txdr_unsigned(stateidp->seqid);
4680 	NFSCL_DEBUG(4, "layget seq=%d\n", (int)stateidp->seqid);
4681 	*tl++ = stateidp->other[0];
4682 	*tl++ = stateidp->other[1];
4683 	*tl++ = stateidp->other[2];
4684 	*tl = txdr_unsigned(layoutlen);
4685 	nd->nd_flag |= ND_USEGSSNAME;
4686 	error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4687 	    NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4688 	if (error != 0)
4689 		return (error);
4690 	if (nd->nd_repstat == 0) {
4691 		NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED + NFSX_STATEID);
4692 		if (*tl++ != 0)
4693 			*retonclosep = 1;
4694 		else
4695 			*retonclosep = 0;
4696 		stateidp->seqid = fxdr_unsigned(uint32_t, *tl++);
4697 		NFSCL_DEBUG(4, "retoncls=%d stseq=%d\n", *retonclosep,
4698 		    (int)stateidp->seqid);
4699 		stateidp->other[0] = *tl++;
4700 		stateidp->other[1] = *tl++;
4701 		stateidp->other[2] = *tl++;
4702 		cnt = fxdr_unsigned(int, *tl);
4703 		NFSCL_DEBUG(4, "layg cnt=%d\n", cnt);
4704 		if (cnt <= 0 || cnt > 10000) {
4705 			/* Don't accept more than 10000 layouts in reply. */
4706 			error = NFSERR_BADXDR;
4707 			goto nfsmout;
4708 		}
4709 		for (i = 0; i < cnt; i++) {
4710 			/* Dissect all the way to the file handle cnt. */
4711 			NFSM_DISSECT(tl, uint32_t *, 3 * NFSX_HYPER +
4712 			    6 * NFSX_UNSIGNED + NFSX_V4DEVICEID);
4713 			fhcnt = fxdr_unsigned(int, *(tl + 11 +
4714 			    NFSX_V4DEVICEID / NFSX_UNSIGNED));
4715 			NFSCL_DEBUG(4, "fhcnt=%d\n", fhcnt);
4716 			if (fhcnt < 0 || fhcnt > 100) {
4717 				/* Don't accept more than 100 file handles. */
4718 				error = NFSERR_BADXDR;
4719 				goto nfsmout;
4720 			}
4721 			if (fhcnt > 1)
4722 				flp = malloc(sizeof(*flp) + (fhcnt - 1) *
4723 				    sizeof(struct nfsfh *),
4724 				    M_NFSFLAYOUT, M_WAITOK);
4725 			else
4726 				flp = malloc(sizeof(*flp),
4727 				    M_NFSFLAYOUT, M_WAITOK);
4728 			flp->nfsfl_flags = 0;
4729 			flp->nfsfl_fhcnt = 0;
4730 			flp->nfsfl_devp = NULL;
4731 			flp->nfsfl_off = fxdr_hyper(tl); tl += 2;
4732 			retlen = fxdr_hyper(tl); tl += 2;
4733 			if (flp->nfsfl_off + retlen < flp->nfsfl_off)
4734 				flp->nfsfl_end = UINT64_MAX - flp->nfsfl_off;
4735 			else
4736 				flp->nfsfl_end = flp->nfsfl_off + retlen;
4737 			flp->nfsfl_iomode = fxdr_unsigned(int, *tl++);
4738 			if (gotiomode == -1)
4739 				gotiomode = flp->nfsfl_iomode;
4740 			NFSCL_DEBUG(4, "layg reqiom=%d retiom=%d\n", iomode,
4741 			    (int)flp->nfsfl_iomode);
4742 			if (fxdr_unsigned(int, *tl++) !=
4743 			    NFSLAYOUT_NFSV4_1_FILES) {
4744 				printf("NFSv4.1: got non-files layout\n");
4745 				error = NFSERR_BADXDR;
4746 				goto nfsmout;
4747 			}
4748 			NFSBCOPY(++tl, flp->nfsfl_dev, NFSX_V4DEVICEID);
4749 			tl += (NFSX_V4DEVICEID / NFSX_UNSIGNED);
4750 			flp->nfsfl_util = fxdr_unsigned(uint32_t, *tl++);
4751 			NFSCL_DEBUG(4, "flutil=0x%x\n", flp->nfsfl_util);
4752 			flp->nfsfl_stripe1 = fxdr_unsigned(uint32_t, *tl++);
4753 			flp->nfsfl_patoff = fxdr_hyper(tl); tl += 2;
4754 			if (fxdr_unsigned(int, *tl) != fhcnt) {
4755 				printf("EEK! bad fhcnt\n");
4756 				error = NFSERR_BADXDR;
4757 				goto nfsmout;
4758 			}
4759 			for (j = 0; j < fhcnt; j++) {
4760 				NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
4761 				nfhlen = fxdr_unsigned(int, *tl);
4762 				if (nfhlen <= 0 || nfhlen > NFSX_V4FHMAX) {
4763 					error = NFSERR_BADXDR;
4764 					goto nfsmout;
4765 				}
4766 				nfhp = malloc(sizeof(*nfhp) + nfhlen - 1,
4767 				    M_NFSFH, M_WAITOK);
4768 				flp->nfsfl_fh[j] = nfhp;
4769 				flp->nfsfl_fhcnt++;
4770 				nfhp->nfh_len = nfhlen;
4771 				NFSM_DISSECT(cp, uint8_t *, NFSM_RNDUP(nfhlen));
4772 				NFSBCOPY(cp, nfhp->nfh_fh, nfhlen);
4773 			}
4774 			if (flp->nfsfl_iomode == gotiomode) {
4775 				/* Keep the list in increasing offset order. */
4776 				tflp = LIST_FIRST(flhp);
4777 				prevflp = NULL;
4778 				while (tflp != NULL &&
4779 				    tflp->nfsfl_off < flp->nfsfl_off) {
4780 					prevflp = tflp;
4781 					tflp = LIST_NEXT(tflp, nfsfl_list);
4782 				}
4783 				if (prevflp == NULL)
4784 					LIST_INSERT_HEAD(flhp, flp, nfsfl_list);
4785 				else
4786 					LIST_INSERT_AFTER(prevflp, flp,
4787 					    nfsfl_list);
4788 			} else {
4789 				printf("nfscl_layoutget(): got wrong iomode\n");
4790 				nfscl_freeflayout(flp);
4791 			}
4792 			flp = NULL;
4793 		}
4794 	}
4795 	if (nd->nd_repstat != 0 && error == 0)
4796 		error = nd->nd_repstat;
4797 nfsmout:
4798 	if (error != 0 && flp != NULL)
4799 		nfscl_freeflayout(flp);
4800 	mbuf_freem(nd->nd_mrep);
4801 	return (error);
4802 }
4803 
4804 /*
4805  * Do the NFSv4.1 Get Device Info.
4806  */
4807 int
4808 nfsrpc_getdeviceinfo(struct nfsmount *nmp, uint8_t *deviceid, int layouttype,
4809     uint32_t *notifybitsp, struct nfscldevinfo **ndip, struct ucred *cred,
4810     NFSPROC_T *p)
4811 {
4812 	uint32_t cnt, *tl;
4813 	struct nfsrv_descript nfsd;
4814 	struct nfsrv_descript *nd = &nfsd;
4815 	struct sockaddr_storage ss;
4816 	struct nfsclds *dsp = NULL, **dspp;
4817 	struct nfscldevinfo *ndi;
4818 	int addrcnt, bitcnt, error, i, isudp, j, pos, safilled, stripecnt;
4819 	uint8_t stripeindex;
4820 
4821 	*ndip = NULL;
4822 	ndi = NULL;
4823 	nfscl_reqstart(nd, NFSPROC_GETDEVICEINFO, nmp, NULL, 0, NULL, NULL);
4824 	NFSM_BUILD(tl, uint32_t *, NFSX_V4DEVICEID + 3 * NFSX_UNSIGNED);
4825 	NFSBCOPY(deviceid, tl, NFSX_V4DEVICEID);
4826 	tl += (NFSX_V4DEVICEID / NFSX_UNSIGNED);
4827 	*tl++ = txdr_unsigned(layouttype);
4828 	*tl++ = txdr_unsigned(100000);
4829 	if (notifybitsp != NULL && *notifybitsp != 0) {
4830 		*tl = txdr_unsigned(1);		/* One word of bits. */
4831 		NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
4832 		*tl = txdr_unsigned(*notifybitsp);
4833 	} else
4834 		*tl = txdr_unsigned(0);
4835 	nd->nd_flag |= ND_USEGSSNAME;
4836 	error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4837 	    NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4838 	if (error != 0)
4839 		return (error);
4840 	if (nd->nd_repstat == 0) {
4841 		NFSM_DISSECT(tl, uint32_t *, 3 * NFSX_UNSIGNED);
4842 		if (layouttype != fxdr_unsigned(int, *tl++))
4843 			printf("EEK! devinfo layout type not same!\n");
4844 		stripecnt = fxdr_unsigned(int, *++tl);
4845 		NFSCL_DEBUG(4, "stripecnt=%d\n", stripecnt);
4846 		if (stripecnt < 1 || stripecnt > 4096) {
4847 			printf("NFS devinfo stripecnt %d: out of range\n",
4848 			    stripecnt);
4849 			error = NFSERR_BADXDR;
4850 			goto nfsmout;
4851 		}
4852 		NFSM_DISSECT(tl, uint32_t *, (stripecnt + 1) * NFSX_UNSIGNED);
4853 		addrcnt = fxdr_unsigned(int, *(tl + stripecnt));
4854 		NFSCL_DEBUG(4, "addrcnt=%d\n", addrcnt);
4855 		if (addrcnt < 1 || addrcnt > 128) {
4856 			printf("NFS devinfo addrcnt %d: out of range\n",
4857 			    addrcnt);
4858 			error = NFSERR_BADXDR;
4859 			goto nfsmout;
4860 		}
4861 
4862 		/*
4863 		 * Now we know how many stripe indices and addresses, so
4864 		 * we can allocate the structure the correct size.
4865 		 */
4866 		i = (stripecnt * sizeof(uint8_t)) / sizeof(struct nfsclds *)
4867 		    + 1;
4868 		NFSCL_DEBUG(4, "stripeindices=%d\n", i);
4869 		ndi = malloc(sizeof(*ndi) + (addrcnt + i) *
4870 		    sizeof(struct nfsclds *), M_NFSDEVINFO, M_WAITOK | M_ZERO);
4871 		NFSBCOPY(deviceid, ndi->nfsdi_deviceid, NFSX_V4DEVICEID);
4872 		ndi->nfsdi_refcnt = 0;
4873 		ndi->nfsdi_stripecnt = stripecnt;
4874 		ndi->nfsdi_addrcnt = addrcnt;
4875 		/* Fill in the stripe indices. */
4876 		for (i = 0; i < stripecnt; i++) {
4877 			stripeindex = fxdr_unsigned(uint8_t, *tl++);
4878 			NFSCL_DEBUG(4, "stripeind=%d\n", stripeindex);
4879 			if (stripeindex >= addrcnt) {
4880 				printf("NFS devinfo stripeindex %d: too big\n",
4881 				    (int)stripeindex);
4882 				error = NFSERR_BADXDR;
4883 				goto nfsmout;
4884 			}
4885 			nfsfldi_setstripeindex(ndi, i, stripeindex);
4886 		}
4887 
4888 		/* Now, dissect the server address(es). */
4889 		safilled = 0;
4890 		for (i = 0; i < addrcnt; i++) {
4891 			NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
4892 			cnt = fxdr_unsigned(uint32_t, *tl);
4893 			if (cnt == 0) {
4894 				printf("NFS devinfo 0 len addrlist\n");
4895 				error = NFSERR_BADXDR;
4896 				goto nfsmout;
4897 			}
4898 			dspp = nfsfldi_addr(ndi, i);
4899 			pos = arc4random() % cnt;	/* Choose one. */
4900 			safilled = 0;
4901 			for (j = 0; j < cnt; j++) {
4902 				error = nfsv4_getipaddr(nd, &ss, &isudp);
4903 				if (error != 0 && error != EPERM) {
4904 					error = NFSERR_BADXDR;
4905 					goto nfsmout;
4906 				}
4907 				if (error == 0 && isudp == 0) {
4908 					/*
4909 					 * The algorithm is:
4910 					 * - use "pos" entry if it is of the
4911 					 *   same af_family or none of them
4912 					 *   is of the same af_family
4913 					 * else
4914 					 * - use the first one of the same
4915 					 *   af_family.
4916 					 */
4917 					if ((safilled == 0 && ss.ss_family ==
4918 					     nmp->nm_nam->sa_family) ||
4919 					    (j == pos &&
4920 					     (safilled == 0 || ss.ss_family ==
4921 					      nmp->nm_nam->sa_family)) ||
4922 					    (safilled == 1 && ss.ss_family ==
4923 					     nmp->nm_nam->sa_family)) {
4924 						error = nfsrpc_fillsa(nmp, &ss,
4925 						    &dsp, p);
4926 						if (error == 0) {
4927 							*dspp = dsp;
4928 							if (ss.ss_family ==
4929 							 nmp->nm_nam->sa_family)
4930 								safilled = 2;
4931 							else
4932 								safilled = 1;
4933 						}
4934 					}
4935 				}
4936 			}
4937 			if (safilled == 0)
4938 				break;
4939 		}
4940 
4941 		/* And the notify bits. */
4942 		NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
4943 		if (safilled != 0) {
4944 			bitcnt = fxdr_unsigned(int, *tl);
4945 			if (bitcnt > 0) {
4946 				NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
4947 				if (notifybitsp != NULL)
4948 					*notifybitsp =
4949 					    fxdr_unsigned(uint32_t, *tl);
4950 			}
4951 			*ndip = ndi;
4952 		} else
4953 			error = EPERM;
4954 	}
4955 	if (nd->nd_repstat != 0)
4956 		error = nd->nd_repstat;
4957 nfsmout:
4958 	if (error != 0 && ndi != NULL)
4959 		nfscl_freedevinfo(ndi);
4960 	mbuf_freem(nd->nd_mrep);
4961 	return (error);
4962 }
4963 
4964 /*
4965  * Do the NFSv4.1 LayoutCommit.
4966  */
4967 int
4968 nfsrpc_layoutcommit(struct nfsmount *nmp, uint8_t *fh, int fhlen, int reclaim,
4969     uint64_t off, uint64_t len, uint64_t lastbyte, nfsv4stateid_t *stateidp,
4970     int layouttype, int layoutupdatecnt, uint8_t *layp, struct ucred *cred,
4971     NFSPROC_T *p, void *stuff)
4972 {
4973 	uint32_t *tl;
4974 	struct nfsrv_descript nfsd, *nd = &nfsd;
4975 	int error, outcnt, i;
4976 	uint8_t *cp;
4977 
4978 	nfscl_reqstart(nd, NFSPROC_LAYOUTCOMMIT, nmp, fh, fhlen, NULL, NULL);
4979 	NFSM_BUILD(tl, uint32_t *, 5 * NFSX_UNSIGNED + 3 * NFSX_HYPER +
4980 	    NFSX_STATEID);
4981 	txdr_hyper(off, tl);
4982 	tl += 2;
4983 	txdr_hyper(len, tl);
4984 	tl += 2;
4985 	if (reclaim != 0)
4986 		*tl++ = newnfs_true;
4987 	else
4988 		*tl++ = newnfs_false;
4989 	*tl++ = txdr_unsigned(stateidp->seqid);
4990 	*tl++ = stateidp->other[0];
4991 	*tl++ = stateidp->other[1];
4992 	*tl++ = stateidp->other[2];
4993 	*tl++ = newnfs_true;
4994 	if (lastbyte < off)
4995 		lastbyte = off;
4996 	else if (lastbyte >= (off + len))
4997 		lastbyte = off + len - 1;
4998 	txdr_hyper(lastbyte, tl);
4999 	tl += 2;
5000 	*tl++ = newnfs_false;
5001 	*tl++ = txdr_unsigned(layouttype);
5002 	*tl = txdr_unsigned(layoutupdatecnt);
5003 	if (layoutupdatecnt > 0) {
5004 		KASSERT(layouttype != NFSLAYOUT_NFSV4_1_FILES,
5005 		    ("Must be nil for Files Layout"));
5006 		outcnt = NFSM_RNDUP(layoutupdatecnt);
5007 		NFSM_BUILD(cp, uint8_t *, outcnt);
5008 		NFSBCOPY(layp, cp, layoutupdatecnt);
5009 		cp += layoutupdatecnt;
5010 		for (i = 0; i < (outcnt - layoutupdatecnt); i++)
5011 			*cp++ = 0x0;
5012 	}
5013 	nd->nd_flag |= ND_USEGSSNAME;
5014 	error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
5015 	    NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
5016 	if (error != 0)
5017 		return (error);
5018 	error = nd->nd_repstat;
5019 	mbuf_freem(nd->nd_mrep);
5020 	return (error);
5021 }
5022 
5023 /*
5024  * Do the NFSv4.1 LayoutReturn.
5025  */
5026 int
5027 nfsrpc_layoutreturn(struct nfsmount *nmp, uint8_t *fh, int fhlen, int reclaim,
5028     int layouttype, uint32_t iomode, int layoutreturn, uint64_t offset,
5029     uint64_t len, nfsv4stateid_t *stateidp, int layoutcnt, uint32_t *layp,
5030     struct ucred *cred, NFSPROC_T *p, void *stuff)
5031 {
5032 	uint32_t *tl;
5033 	struct nfsrv_descript nfsd, *nd = &nfsd;
5034 	int error, outcnt, i;
5035 	uint8_t *cp;
5036 
5037 	nfscl_reqstart(nd, NFSPROC_LAYOUTRETURN, nmp, fh, fhlen, NULL, NULL);
5038 	NFSM_BUILD(tl, uint32_t *, 4 * NFSX_UNSIGNED);
5039 	if (reclaim != 0)
5040 		*tl++ = newnfs_true;
5041 	else
5042 		*tl++ = newnfs_false;
5043 	*tl++ = txdr_unsigned(layouttype);
5044 	*tl++ = txdr_unsigned(iomode);
5045 	*tl = txdr_unsigned(layoutreturn);
5046 	if (layoutreturn == NFSLAYOUTRETURN_FILE) {
5047 		NFSM_BUILD(tl, uint32_t *, 2 * NFSX_HYPER + NFSX_STATEID +
5048 		    NFSX_UNSIGNED);
5049 		txdr_hyper(offset, tl);
5050 		tl += 2;
5051 		txdr_hyper(len, tl);
5052 		tl += 2;
5053 		NFSCL_DEBUG(4, "layoutret stseq=%d\n", (int)stateidp->seqid);
5054 		*tl++ = txdr_unsigned(stateidp->seqid);
5055 		*tl++ = stateidp->other[0];
5056 		*tl++ = stateidp->other[1];
5057 		*tl++ = stateidp->other[2];
5058 		*tl = txdr_unsigned(layoutcnt);
5059 		if (layoutcnt > 0) {
5060 			outcnt = NFSM_RNDUP(layoutcnt);
5061 			NFSM_BUILD(cp, uint8_t *, outcnt);
5062 			NFSBCOPY(layp, cp, layoutcnt);
5063 			cp += layoutcnt;
5064 			for (i = 0; i < (outcnt - layoutcnt); i++)
5065 				*cp++ = 0x0;
5066 		}
5067 	}
5068 	nd->nd_flag |= ND_USEGSSNAME;
5069 	error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
5070 	    NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
5071 	if (error != 0)
5072 		return (error);
5073 	if (nd->nd_repstat == 0) {
5074 		NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
5075 		if (*tl != 0) {
5076 			NFSM_DISSECT(tl, uint32_t *, NFSX_STATEID);
5077 			stateidp->seqid = fxdr_unsigned(uint32_t, *tl++);
5078 			stateidp->other[0] = *tl++;
5079 			stateidp->other[1] = *tl++;
5080 			stateidp->other[2] = *tl;
5081 		}
5082 	} else
5083 		error = nd->nd_repstat;
5084 nfsmout:
5085 	mbuf_freem(nd->nd_mrep);
5086 	return (error);
5087 }
5088 
5089 /*
5090  * Acquire a layout and devinfo, if possible. The caller must have acquired
5091  * a reference count on the nfsclclient structure before calling this.
5092  * Return the layout in lypp with a reference count on it, if successful.
5093  */
5094 static int
5095 nfsrpc_getlayout(struct nfsmount *nmp, vnode_t vp, struct nfsfh *nfhp,
5096     int iomode, uint32_t *notifybitsp, nfsv4stateid_t *stateidp, uint64_t off,
5097     struct nfscllayout **lypp, struct ucred *cred, NFSPROC_T *p)
5098 {
5099 	struct nfscllayout *lyp;
5100 	struct nfsclflayout *flp, *tflp;
5101 	struct nfscldevinfo *dip;
5102 	struct nfsclflayouthead flh;
5103 	int error = 0, islocked, layoutlen, recalled, retonclose;
5104 	nfsv4stateid_t stateid;
5105 
5106 	*lypp = NULL;
5107 	/*
5108 	 * If lyp is returned non-NULL, there will be a refcnt (shared lock)
5109 	 * on it, iff flp != NULL or a lock (exclusive lock) on it iff
5110 	 * flp == NULL.
5111 	 */
5112 	lyp = nfscl_getlayout(nmp->nm_clp, nfhp->nfh_fh, nfhp->nfh_len,
5113 	    off, &flp, &recalled);
5114 	islocked = 0;
5115 	if (lyp == NULL || flp == NULL) {
5116 		if (recalled != 0)
5117 			return (EIO);
5118 		LIST_INIT(&flh);
5119 		layoutlen = NFSMNT_MDSSESSION(nmp)->nfsess_maxcache -
5120 		    (NFSX_STATEID + 3 * NFSX_UNSIGNED);
5121 		if (lyp == NULL) {
5122 			stateid.seqid = 0;
5123 			stateid.other[0] = stateidp->other[0];
5124 			stateid.other[1] = stateidp->other[1];
5125 			stateid.other[2] = stateidp->other[2];
5126 			error = nfsrpc_layoutget(nmp, nfhp->nfh_fh,
5127 			    nfhp->nfh_len, iomode, (uint64_t)0, INT64_MAX,
5128 			    (uint64_t)0, layoutlen, &stateid, &retonclose,
5129 			    &flh, cred, p, NULL);
5130 		} else {
5131 			islocked = 1;
5132 			stateid.seqid = lyp->nfsly_stateid.seqid;
5133 			stateid.other[0] = lyp->nfsly_stateid.other[0];
5134 			stateid.other[1] = lyp->nfsly_stateid.other[1];
5135 			stateid.other[2] = lyp->nfsly_stateid.other[2];
5136 			error = nfsrpc_layoutget(nmp, nfhp->nfh_fh,
5137 			    nfhp->nfh_len, iomode, off, INT64_MAX,
5138 			    (uint64_t)0, layoutlen, &stateid, &retonclose,
5139 			    &flh, cred, p, NULL);
5140 		}
5141 		if (error == 0)
5142 			LIST_FOREACH(tflp, &flh, nfsfl_list) {
5143 				error = nfscl_adddevinfo(nmp, NULL, tflp);
5144 				if (error != 0) {
5145 					error = nfsrpc_getdeviceinfo(nmp,
5146 					    tflp->nfsfl_dev,
5147 					    NFSLAYOUT_NFSV4_1_FILES,
5148 					    notifybitsp, &dip, cred, p);
5149 					if (error != 0)
5150 						break;
5151 					error = nfscl_adddevinfo(nmp, dip,
5152 					    tflp);
5153 					if (error != 0)
5154 						printf(
5155 						    "getlayout: cannot add\n");
5156 				}
5157 			}
5158 		if (error == 0) {
5159 			/*
5160 			 * nfscl_layout() always returns with the nfsly_lock
5161 			 * set to a refcnt (shared lock).
5162 			 */
5163 			error = nfscl_layout(nmp, vp, nfhp->nfh_fh,
5164 			    nfhp->nfh_len, &stateid, retonclose, &flh, &lyp,
5165 			    cred, p);
5166 			if (error == 0)
5167 				*lypp = lyp;
5168 		} else if (islocked != 0)
5169 			nfsv4_unlock(&lyp->nfsly_lock, 0);
5170 	} else
5171 		*lypp = lyp;
5172 	return (error);
5173 }
5174 
5175 /*
5176  * Do a TCP connection plus exchange id and create session.
5177  * If successful, a "struct nfsclds" is linked into the list for the
5178  * mount point and a pointer to it is returned.
5179  */
5180 static int
5181 nfsrpc_fillsa(struct nfsmount *nmp, struct sockaddr_storage *ssp,
5182     struct nfsclds **dspp, NFSPROC_T *p)
5183 {
5184 	struct sockaddr_in *msad, *sad, *ssd;
5185 	struct sockaddr_in6 *msad6, *sad6, *ssd6;
5186 	struct nfsclclient *clp;
5187 	struct nfssockreq *nrp;
5188 	struct nfsclds *dsp, *tdsp;
5189 	int error;
5190 	enum nfsclds_state retv;
5191 	uint32_t sequenceid;
5192 
5193 	KASSERT(nmp->nm_sockreq.nr_cred != NULL,
5194 	    ("nfsrpc_fillsa: NULL nr_cred"));
5195 	NFSLOCKCLSTATE();
5196 	clp = nmp->nm_clp;
5197 	NFSUNLOCKCLSTATE();
5198 	if (clp == NULL)
5199 		return (EPERM);
5200 	if (ssp->ss_family == AF_INET) {
5201 		ssd = (struct sockaddr_in *)ssp;
5202 		NFSLOCKMNT(nmp);
5203 
5204 		/*
5205 		 * Check to see if we already have a session for this
5206 		 * address that is usable for a DS.
5207 		 * Note that the MDS's address is in a different place
5208 		 * than the sessions already acquired for DS's.
5209 		 */
5210 		msad = (struct sockaddr_in *)nmp->nm_sockreq.nr_nam;
5211 		tdsp = TAILQ_FIRST(&nmp->nm_sess);
5212 		while (tdsp != NULL) {
5213 			if (msad != NULL && msad->sin_family == AF_INET &&
5214 			    ssd->sin_addr.s_addr == msad->sin_addr.s_addr &&
5215 			    ssd->sin_port == msad->sin_port &&
5216 			    (tdsp->nfsclds_flags & NFSCLDS_DS) != 0) {
5217 				*dspp = tdsp;
5218 				NFSUNLOCKMNT(nmp);
5219 				NFSCL_DEBUG(4, "fnd same addr\n");
5220 				return (0);
5221 			}
5222 			tdsp = TAILQ_NEXT(tdsp, nfsclds_list);
5223 			if (tdsp != NULL && tdsp->nfsclds_sockp != NULL)
5224 				msad = (struct sockaddr_in *)
5225 				    tdsp->nfsclds_sockp->nr_nam;
5226 			else
5227 				msad = NULL;
5228 		}
5229 		NFSUNLOCKMNT(nmp);
5230 
5231 		/* No IP address match, so look for new/trunked one. */
5232 		sad = malloc(sizeof(*sad), M_SONAME, M_WAITOK | M_ZERO);
5233 		sad->sin_len = sizeof(*sad);
5234 		sad->sin_family = AF_INET;
5235 		sad->sin_port = ssd->sin_port;
5236 		sad->sin_addr.s_addr = ssd->sin_addr.s_addr;
5237 		nrp = malloc(sizeof(*nrp), M_NFSSOCKREQ, M_WAITOK | M_ZERO);
5238 		nrp->nr_nam = (struct sockaddr *)sad;
5239 	} else if (ssp->ss_family == AF_INET6) {
5240 		ssd6 = (struct sockaddr_in6 *)ssp;
5241 		NFSLOCKMNT(nmp);
5242 
5243 		/*
5244 		 * Check to see if we already have a session for this
5245 		 * address that is usable for a DS.
5246 		 * Note that the MDS's address is in a different place
5247 		 * than the sessions already acquired for DS's.
5248 		 */
5249 		msad6 = (struct sockaddr_in6 *)nmp->nm_sockreq.nr_nam;
5250 		tdsp = TAILQ_FIRST(&nmp->nm_sess);
5251 		while (tdsp != NULL) {
5252 			if (msad6 != NULL && msad6->sin6_family == AF_INET6 &&
5253 			    IN6_ARE_ADDR_EQUAL(&ssd6->sin6_addr,
5254 			    &msad6->sin6_addr) &&
5255 			    ssd6->sin6_port == msad6->sin6_port &&
5256 			    (tdsp->nfsclds_flags & NFSCLDS_DS) != 0) {
5257 				*dspp = tdsp;
5258 				NFSUNLOCKMNT(nmp);
5259 				return (0);
5260 			}
5261 			tdsp = TAILQ_NEXT(tdsp, nfsclds_list);
5262 			if (tdsp != NULL && tdsp->nfsclds_sockp != NULL)
5263 				msad6 = (struct sockaddr_in6 *)
5264 				    tdsp->nfsclds_sockp->nr_nam;
5265 			else
5266 				msad6 = NULL;
5267 		}
5268 		NFSUNLOCKMNT(nmp);
5269 
5270 		/* No IP address match, so look for new/trunked one. */
5271 		sad6 = malloc(sizeof(*sad6), M_SONAME, M_WAITOK | M_ZERO);
5272 		sad6->sin6_len = sizeof(*sad6);
5273 		sad6->sin6_family = AF_INET6;
5274 		sad6->sin6_port = ssd6->sin6_port;
5275 		NFSBCOPY(&ssd6->sin6_addr, &sad6->sin6_addr,
5276 		    sizeof(struct in6_addr));
5277 		nrp = malloc(sizeof(*nrp), M_NFSSOCKREQ, M_WAITOK | M_ZERO);
5278 		nrp->nr_nam = (struct sockaddr *)sad6;
5279 	} else
5280 		return (EPERM);
5281 
5282 	nrp->nr_sotype = SOCK_STREAM;
5283 	mtx_init(&nrp->nr_mtx, "nfssock", NULL, MTX_DEF);
5284 	nrp->nr_prog = NFS_PROG;
5285 	nrp->nr_vers = NFS_VER4;
5286 
5287 	/*
5288 	 * Use the credentials that were used for the mount, which are
5289 	 * in nmp->nm_sockreq.nr_cred for newnfs_connect() etc.
5290 	 * Ref. counting the credentials with crhold() is probably not
5291 	 * necessary, since nm_sockreq.nr_cred won't be crfree()'d until
5292 	 * unmount, but I did it anyhow.
5293 	 */
5294 	nrp->nr_cred = crhold(nmp->nm_sockreq.nr_cred);
5295 	error = newnfs_connect(nmp, nrp, NULL, p, 0);
5296 	NFSCL_DEBUG(3, "DS connect=%d\n", error);
5297 
5298 	/* Now, do the exchangeid and create session. */
5299 	if (error == 0)
5300 		error = nfsrpc_exchangeid(nmp, clp, nrp, NFSV4EXCH_USEPNFSDS,
5301 		    &dsp, nrp->nr_cred, p);
5302 	NFSCL_DEBUG(3, "DS exchangeid=%d\n", error);
5303 	if (error == 0) {
5304 		dsp->nfsclds_sockp = nrp;
5305 		NFSLOCKMNT(nmp);
5306 		retv = nfscl_getsameserver(nmp, dsp, &tdsp);
5307 		NFSCL_DEBUG(3, "getsame ret=%d\n", retv);
5308 		if (retv == NFSDSP_USETHISSESSION) {
5309 			NFSUNLOCKMNT(nmp);
5310 			/*
5311 			 * If there is already a session for this server,
5312 			 * use it.
5313 			 */
5314 			(void)newnfs_disconnect(nrp);
5315 			nfscl_freenfsclds(dsp);
5316 			*dspp = tdsp;
5317 			return (0);
5318 		}
5319 		if (retv == NFSDSP_SEQTHISSESSION)
5320 			sequenceid = tdsp->nfsclds_sess.nfsess_sequenceid;
5321 		else
5322 			sequenceid = dsp->nfsclds_sess.nfsess_sequenceid;
5323 		NFSUNLOCKMNT(nmp);
5324 		error = nfsrpc_createsession(nmp, &dsp->nfsclds_sess,
5325 		    nrp, sequenceid, 0, nrp->nr_cred, p);
5326 		NFSCL_DEBUG(3, "DS createsess=%d\n", error);
5327 	} else {
5328 		NFSFREECRED(nrp->nr_cred);
5329 		NFSFREEMUTEX(&nrp->nr_mtx);
5330 		free(nrp->nr_nam, M_SONAME);
5331 		free(nrp, M_NFSSOCKREQ);
5332 	}
5333 	if (error == 0) {
5334 		NFSCL_DEBUG(3, "add DS session\n");
5335 		/*
5336 		 * Put it at the end of the list. That way the list
5337 		 * is ordered by when the entry was added. This matters
5338 		 * since the one done first is the one that should be
5339 		 * used for sequencid'ing any subsequent create sessions.
5340 		 */
5341 		NFSLOCKMNT(nmp);
5342 		TAILQ_INSERT_TAIL(&nmp->nm_sess, dsp, nfsclds_list);
5343 		NFSUNLOCKMNT(nmp);
5344 		*dspp = dsp;
5345 	} else if (dsp != NULL)
5346 		nfscl_freenfsclds(dsp);
5347 	return (error);
5348 }
5349 
5350 /*
5351  * Do the NFSv4.1 Reclaim Complete.
5352  */
5353 int
5354 nfsrpc_reclaimcomplete(struct nfsmount *nmp, struct ucred *cred, NFSPROC_T *p)
5355 {
5356 	uint32_t *tl;
5357 	struct nfsrv_descript nfsd;
5358 	struct nfsrv_descript *nd = &nfsd;
5359 	int error;
5360 
5361 	nfscl_reqstart(nd, NFSPROC_RECLAIMCOMPL, nmp, NULL, 0, NULL, NULL);
5362 	NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
5363 	*tl = newnfs_false;
5364 	nd->nd_flag |= ND_USEGSSNAME;
5365 	error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
5366 	    NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
5367 	if (error != 0)
5368 		return (error);
5369 	error = nd->nd_repstat;
5370 	mbuf_freem(nd->nd_mrep);
5371 	return (error);
5372 }
5373 
5374 /*
5375  * Initialize the slot tables for a session.
5376  */
5377 static void
5378 nfscl_initsessionslots(struct nfsclsession *sep)
5379 {
5380 	int i;
5381 
5382 	for (i = 0; i < NFSV4_CBSLOTS; i++) {
5383 		if (sep->nfsess_cbslots[i].nfssl_reply != NULL)
5384 			m_freem(sep->nfsess_cbslots[i].nfssl_reply);
5385 		NFSBZERO(&sep->nfsess_cbslots[i], sizeof(struct nfsslot));
5386 	}
5387 	for (i = 0; i < 64; i++)
5388 		sep->nfsess_slotseq[i] = 0;
5389 	sep->nfsess_slots = 0;
5390 }
5391 
5392 /*
5393  * Called to try and do an I/O operation via an NFSv4.1 Data Server (DS).
5394  */
5395 int
5396 nfscl_doiods(vnode_t vp, struct uio *uiop, int *iomode, int *must_commit,
5397     uint32_t rwaccess, struct ucred *cred, NFSPROC_T *p)
5398 {
5399 	struct nfsnode *np = VTONFS(vp);
5400 	struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
5401 	struct nfscllayout *layp;
5402 	struct nfscldevinfo *dip;
5403 	struct nfsclflayout *rflp;
5404 	nfsv4stateid_t stateid;
5405 	struct ucred *newcred;
5406 	uint64_t lastbyte, len, off, oresid, xfer;
5407 	int eof, error, iolaymode, recalled;
5408 	void *lckp;
5409 
5410 	if (!NFSHASPNFS(nmp) || nfscl_enablecallb == 0 || nfs_numnfscbd == 0 ||
5411 	    (np->n_flag & NNOLAYOUT) != 0)
5412 		return (EIO);
5413 	/* Now, get a reference cnt on the clientid for this mount. */
5414 	if (nfscl_getref(nmp) == 0)
5415 		return (EIO);
5416 
5417 	/* Find an appropriate stateid. */
5418 	newcred = NFSNEWCRED(cred);
5419 	error = nfscl_getstateid(vp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len,
5420 	    rwaccess, 1, newcred, p, &stateid, &lckp);
5421 	if (error != 0) {
5422 		NFSFREECRED(newcred);
5423 		nfscl_relref(nmp);
5424 		return (error);
5425 	}
5426 	/* Search for a layout for this file. */
5427 	off = uiop->uio_offset;
5428 	layp = nfscl_getlayout(nmp->nm_clp, np->n_fhp->nfh_fh,
5429 	    np->n_fhp->nfh_len, off, &rflp, &recalled);
5430 	if (layp == NULL || rflp == NULL) {
5431 		if (recalled != 0) {
5432 			NFSFREECRED(newcred);
5433 			nfscl_relref(nmp);
5434 			return (EIO);
5435 		}
5436 		if (layp != NULL) {
5437 			nfscl_rellayout(layp, (rflp == NULL) ? 1 : 0);
5438 			layp = NULL;
5439 		}
5440 		/* Try and get a Layout, if it is supported. */
5441 		if (rwaccess == NFSV4OPEN_ACCESSWRITE ||
5442 		    (np->n_flag & NWRITEOPENED) != 0)
5443 			iolaymode = NFSLAYOUTIOMODE_RW;
5444 		else
5445 			iolaymode = NFSLAYOUTIOMODE_READ;
5446 		error = nfsrpc_getlayout(nmp, vp, np->n_fhp, iolaymode,
5447 		    NULL, &stateid, off, &layp, newcred, p);
5448 		if (error != 0) {
5449 			NFSLOCKNODE(np);
5450 			np->n_flag |= NNOLAYOUT;
5451 			NFSUNLOCKNODE(np);
5452 			if (lckp != NULL)
5453 				nfscl_lockderef(lckp);
5454 			NFSFREECRED(newcred);
5455 			if (layp != NULL)
5456 				nfscl_rellayout(layp, 0);
5457 			nfscl_relref(nmp);
5458 			return (error);
5459 		}
5460 	}
5461 
5462 	/*
5463 	 * Loop around finding a layout that works for the first part of
5464 	 * this I/O operation, and then call the function that actually
5465 	 * does the RPC.
5466 	 */
5467 	eof = 0;
5468 	len = (uint64_t)uiop->uio_resid;
5469 	while (len > 0 && error == 0 && eof == 0) {
5470 		off = uiop->uio_offset;
5471 		error = nfscl_findlayoutforio(layp, off, rwaccess, &rflp);
5472 		if (error == 0) {
5473 			oresid = xfer = (uint64_t)uiop->uio_resid;
5474 			if (xfer > (rflp->nfsfl_end - rflp->nfsfl_off))
5475 				xfer = rflp->nfsfl_end - rflp->nfsfl_off;
5476 			dip = nfscl_getdevinfo(nmp->nm_clp, rflp->nfsfl_dev,
5477 			    rflp->nfsfl_devp);
5478 			if (dip != NULL) {
5479 				error = nfscl_doflayoutio(vp, uiop, iomode,
5480 				    must_commit, &eof, &stateid, rwaccess, dip,
5481 				    layp, rflp, off, xfer, newcred, p);
5482 				nfscl_reldevinfo(dip);
5483 				lastbyte = off + xfer - 1;
5484 				if (error == 0) {
5485 					NFSLOCKCLSTATE();
5486 					if (lastbyte > layp->nfsly_lastbyte)
5487 						layp->nfsly_lastbyte = lastbyte;
5488 					NFSUNLOCKCLSTATE();
5489 				}
5490 			} else
5491 				error = EIO;
5492 			if (error == 0)
5493 				len -= (oresid - (uint64_t)uiop->uio_resid);
5494 		}
5495 	}
5496 	if (lckp != NULL)
5497 		nfscl_lockderef(lckp);
5498 	NFSFREECRED(newcred);
5499 	nfscl_rellayout(layp, 0);
5500 	nfscl_relref(nmp);
5501 	return (error);
5502 }
5503 
5504 /*
5505  * Find a file layout that will handle the first bytes of the requested
5506  * range and return the information from it needed to to the I/O operation.
5507  */
5508 int
5509 nfscl_findlayoutforio(struct nfscllayout *lyp, uint64_t off, uint32_t rwaccess,
5510     struct nfsclflayout **retflpp)
5511 {
5512 	struct nfsclflayout *flp, *nflp, *rflp;
5513 	uint32_t rw;
5514 
5515 	rflp = NULL;
5516 	rw = rwaccess;
5517 	/* For reading, do the Read list first and then the Write list. */
5518 	do {
5519 		if (rw == NFSV4OPEN_ACCESSREAD)
5520 			flp = LIST_FIRST(&lyp->nfsly_flayread);
5521 		else
5522 			flp = LIST_FIRST(&lyp->nfsly_flayrw);
5523 		while (flp != NULL) {
5524 			nflp = LIST_NEXT(flp, nfsfl_list);
5525 			if (flp->nfsfl_off > off)
5526 				break;
5527 			if (flp->nfsfl_end > off &&
5528 			    (rflp == NULL || rflp->nfsfl_end < flp->nfsfl_end))
5529 				rflp = flp;
5530 			flp = nflp;
5531 		}
5532 		if (rw == NFSV4OPEN_ACCESSREAD)
5533 			rw = NFSV4OPEN_ACCESSWRITE;
5534 		else
5535 			rw = 0;
5536 	} while (rw != 0);
5537 	if (rflp != NULL) {
5538 		/* This one covers the most bytes starting at off. */
5539 		*retflpp = rflp;
5540 		return (0);
5541 	}
5542 	return (EIO);
5543 }
5544 
5545 /*
5546  * Do I/O using an NFSv4.1 file layout.
5547  */
5548 static int
5549 nfscl_doflayoutio(vnode_t vp, struct uio *uiop, int *iomode, int *must_commit,
5550     int *eofp, nfsv4stateid_t *stateidp, int rwflag, struct nfscldevinfo *dp,
5551     struct nfscllayout *lyp, struct nfsclflayout *flp, uint64_t off,
5552     uint64_t len, struct ucred *cred, NFSPROC_T *p)
5553 {
5554 	uint64_t io_off, rel_off, stripe_unit_size, transfer, xfer;
5555 	int commit_thru_mds, error = 0, stripe_index, stripe_pos;
5556 	struct nfsnode *np;
5557 	struct nfsfh *fhp;
5558 	struct nfsclds **dspp;
5559 
5560 	np = VTONFS(vp);
5561 	rel_off = off - flp->nfsfl_patoff;
5562 	stripe_unit_size = (flp->nfsfl_util >> 6) & 0x3ffffff;
5563 	stripe_pos = (rel_off / stripe_unit_size + flp->nfsfl_stripe1) %
5564 	    dp->nfsdi_stripecnt;
5565 	transfer = stripe_unit_size - (rel_off % stripe_unit_size);
5566 
5567 	/* Loop around, doing I/O for each stripe unit. */
5568 	while (len > 0 && error == 0) {
5569 		stripe_index = nfsfldi_stripeindex(dp, stripe_pos);
5570 		dspp = nfsfldi_addr(dp, stripe_index);
5571 		if (len > transfer)
5572 			xfer = transfer;
5573 		else
5574 			xfer = len;
5575 		if ((flp->nfsfl_util & NFSFLAYUTIL_DENSE) != 0) {
5576 			/* Dense layout. */
5577 			if (stripe_pos >= flp->nfsfl_fhcnt)
5578 				return (EIO);
5579 			fhp = flp->nfsfl_fh[stripe_pos];
5580 			io_off = (rel_off / (stripe_unit_size *
5581 			    dp->nfsdi_stripecnt)) * stripe_unit_size +
5582 			    rel_off % stripe_unit_size;
5583 		} else {
5584 			/* Sparse layout. */
5585 			if (flp->nfsfl_fhcnt > 1) {
5586 				if (stripe_index >= flp->nfsfl_fhcnt)
5587 					return (EIO);
5588 				fhp = flp->nfsfl_fh[stripe_index];
5589 			} else if (flp->nfsfl_fhcnt == 1)
5590 				fhp = flp->nfsfl_fh[0];
5591 			else
5592 				fhp = np->n_fhp;
5593 			io_off = off;
5594 		}
5595 		if ((flp->nfsfl_util & NFSFLAYUTIL_COMMIT_THRU_MDS) != 0)
5596 			commit_thru_mds = 1;
5597 		else
5598 			commit_thru_mds = 0;
5599 		if (rwflag == FREAD)
5600 			error = nfsrpc_readds(vp, uiop, stateidp, eofp, *dspp,
5601 			    io_off, xfer, fhp, cred, p);
5602 		else {
5603 			error = nfsrpc_writeds(vp, uiop, iomode, must_commit,
5604 			    stateidp, *dspp, io_off, xfer, fhp, commit_thru_mds,
5605 			    cred, p);
5606 			if (error == 0) {
5607 				NFSLOCKCLSTATE();
5608 				lyp->nfsly_flags |= NFSLY_WRITTEN;
5609 				NFSUNLOCKCLSTATE();
5610 			}
5611 		}
5612 		if (error == 0) {
5613 			transfer = stripe_unit_size;
5614 			stripe_pos = (stripe_pos + 1) % dp->nfsdi_stripecnt;
5615 			len -= xfer;
5616 			off += xfer;
5617 		}
5618 	}
5619 	return (error);
5620 }
5621 
5622 /*
5623  * The actual read RPC done to a DS.
5624  */
5625 static int
5626 nfsrpc_readds(vnode_t vp, struct uio *uiop, nfsv4stateid_t *stateidp, int *eofp,
5627     struct nfsclds *dsp, uint64_t io_off, int len, struct nfsfh *fhp,
5628     struct ucred *cred, NFSPROC_T *p)
5629 {
5630 	uint32_t *tl;
5631 	int error, retlen;
5632 	struct nfsrv_descript nfsd;
5633 	struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
5634 	struct nfsrv_descript *nd = &nfsd;
5635 	struct nfssockreq *nrp;
5636 
5637 	nd->nd_mrep = NULL;
5638 	nfscl_reqstart(nd, NFSPROC_READDS, nmp, fhp->nfh_fh, fhp->nfh_len,
5639 	    NULL, &dsp->nfsclds_sess);
5640 	nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSEQIDZERO);
5641 	NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED * 3);
5642 	txdr_hyper(io_off, tl);
5643 	*(tl + 2) = txdr_unsigned(len);
5644 	nrp = dsp->nfsclds_sockp;
5645 	if (nrp == NULL)
5646 		/* If NULL, use the MDS socket. */
5647 		nrp = &nmp->nm_sockreq;
5648 	error = newnfs_request(nd, nmp, NULL, nrp, vp, p, cred,
5649 	    NFS_PROG, NFS_VER4, NULL, 1, NULL, &dsp->nfsclds_sess);
5650 	if (error != 0)
5651 		return (error);
5652 	if (nd->nd_repstat != 0) {
5653 		error = nd->nd_repstat;
5654 		goto nfsmout;
5655 	}
5656 	NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
5657 	*eofp = fxdr_unsigned(int, *tl);
5658 	NFSM_STRSIZ(retlen, len);
5659 	error = nfsm_mbufuio(nd, uiop, retlen);
5660 nfsmout:
5661 	if (nd->nd_mrep != NULL)
5662 		mbuf_freem(nd->nd_mrep);
5663 	return (error);
5664 }
5665 
5666 /*
5667  * The actual write RPC done to a DS.
5668  */
5669 static int
5670 nfsrpc_writeds(vnode_t vp, struct uio *uiop, int *iomode, int *must_commit,
5671     nfsv4stateid_t *stateidp, struct nfsclds *dsp, uint64_t io_off, int len,
5672     struct nfsfh *fhp, int commit_thru_mds, struct ucred *cred, NFSPROC_T *p)
5673 {
5674 	uint32_t *tl;
5675 	struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
5676 	int error, rlen, commit, committed = NFSWRITE_FILESYNC;
5677 	int32_t backup;
5678 	struct nfsrv_descript nfsd;
5679 	struct nfsrv_descript *nd = &nfsd;
5680 	struct nfssockreq *nrp;
5681 
5682 	KASSERT(uiop->uio_iovcnt == 1, ("nfs: writerpc iovcnt > 1"));
5683 	nd->nd_mrep = NULL;
5684 	nfscl_reqstart(nd, NFSPROC_WRITEDS, nmp, fhp->nfh_fh, fhp->nfh_len,
5685 	    NULL, &dsp->nfsclds_sess);
5686 	nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSEQIDZERO);
5687 	NFSM_BUILD(tl, uint32_t *, NFSX_HYPER + 2 * NFSX_UNSIGNED);
5688 	txdr_hyper(io_off, tl);
5689 	tl += 2;
5690 	*tl++ = txdr_unsigned(*iomode);
5691 	*tl = txdr_unsigned(len);
5692 	nfsm_uiombuf(nd, uiop, len);
5693 	nrp = dsp->nfsclds_sockp;
5694 	if (nrp == NULL)
5695 		/* If NULL, use the MDS socket. */
5696 		nrp = &nmp->nm_sockreq;
5697 	error = newnfs_request(nd, nmp, NULL, nrp, vp, p, cred,
5698 	    NFS_PROG, NFS_VER4, NULL, 1, NULL, &dsp->nfsclds_sess);
5699 	if (error != 0)
5700 		return (error);
5701 	if (nd->nd_repstat != 0) {
5702 		/*
5703 		 * In case the rpc gets retried, roll
5704 		 * the uio fileds changed by nfsm_uiombuf()
5705 		 * back.
5706 		 */
5707 		uiop->uio_offset -= len;
5708 		uio_uio_resid_add(uiop, len);
5709 		uio_iov_base_add(uiop, -len);
5710 		uio_iov_len_add(uiop, len);
5711 		error = nd->nd_repstat;
5712 	} else {
5713 		NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED + NFSX_VERF);
5714 		rlen = fxdr_unsigned(int, *tl++);
5715 		if (rlen == 0) {
5716 			error = NFSERR_IO;
5717 			goto nfsmout;
5718 		} else if (rlen < len) {
5719 			backup = len - rlen;
5720 			uio_iov_base_add(uiop, -(backup));
5721 			uio_iov_len_add(uiop, backup);
5722 			uiop->uio_offset -= backup;
5723 			uio_uio_resid_add(uiop, backup);
5724 			len = rlen;
5725 		}
5726 		commit = fxdr_unsigned(int, *tl++);
5727 
5728 		/*
5729 		 * Return the lowest committment level
5730 		 * obtained by any of the RPCs.
5731 		 */
5732 		if (committed == NFSWRITE_FILESYNC)
5733 			committed = commit;
5734 		else if (committed == NFSWRITE_DATASYNC &&
5735 		    commit == NFSWRITE_UNSTABLE)
5736 			committed = commit;
5737 		if (commit_thru_mds != 0) {
5738 			NFSLOCKMNT(nmp);
5739 			if (!NFSHASWRITEVERF(nmp)) {
5740 				NFSBCOPY(tl, nmp->nm_verf, NFSX_VERF);
5741 				NFSSETWRITEVERF(nmp);
5742 	    		} else if (NFSBCMP(tl, nmp->nm_verf, NFSX_VERF)) {
5743 				*must_commit = 1;
5744 				NFSBCOPY(tl, nmp->nm_verf, NFSX_VERF);
5745 			}
5746 			NFSUNLOCKMNT(nmp);
5747 		} else {
5748 			NFSLOCKDS(dsp);
5749 			if ((dsp->nfsclds_flags & NFSCLDS_HASWRITEVERF) == 0) {
5750 				NFSBCOPY(tl, dsp->nfsclds_verf, NFSX_VERF);
5751 				dsp->nfsclds_flags |= NFSCLDS_HASWRITEVERF;
5752 			} else if (NFSBCMP(tl, dsp->nfsclds_verf, NFSX_VERF)) {
5753 				*must_commit = 1;
5754 				NFSBCOPY(tl, dsp->nfsclds_verf, NFSX_VERF);
5755 			}
5756 			NFSUNLOCKDS(dsp);
5757 		}
5758 	}
5759 nfsmout:
5760 	if (nd->nd_mrep != NULL)
5761 		mbuf_freem(nd->nd_mrep);
5762 	*iomode = committed;
5763 	if (nd->nd_repstat != 0 && error == 0)
5764 		error = nd->nd_repstat;
5765 	return (error);
5766 }
5767 
5768 /*
5769  * Free up the nfsclds structure.
5770  */
5771 void
5772 nfscl_freenfsclds(struct nfsclds *dsp)
5773 {
5774 	int i;
5775 
5776 	if (dsp == NULL)
5777 		return;
5778 	if (dsp->nfsclds_sockp != NULL) {
5779 		NFSFREECRED(dsp->nfsclds_sockp->nr_cred);
5780 		NFSFREEMUTEX(&dsp->nfsclds_sockp->nr_mtx);
5781 		free(dsp->nfsclds_sockp->nr_nam, M_SONAME);
5782 		free(dsp->nfsclds_sockp, M_NFSSOCKREQ);
5783 	}
5784 	NFSFREEMUTEX(&dsp->nfsclds_mtx);
5785 	NFSFREEMUTEX(&dsp->nfsclds_sess.nfsess_mtx);
5786 	for (i = 0; i < NFSV4_CBSLOTS; i++) {
5787 		if (dsp->nfsclds_sess.nfsess_cbslots[i].nfssl_reply != NULL)
5788 			m_freem(
5789 			    dsp->nfsclds_sess.nfsess_cbslots[i].nfssl_reply);
5790 	}
5791 	free(dsp, M_NFSCLDS);
5792 }
5793 
5794 static enum nfsclds_state
5795 nfscl_getsameserver(struct nfsmount *nmp, struct nfsclds *newdsp,
5796     struct nfsclds **retdspp)
5797 {
5798 	struct nfsclds *dsp, *cur_dsp;
5799 
5800 	/*
5801 	 * Search the list of nfsclds structures for one with the same
5802 	 * server.
5803 	 */
5804 	cur_dsp = NULL;
5805 	TAILQ_FOREACH(dsp, &nmp->nm_sess, nfsclds_list) {
5806 		if (dsp->nfsclds_servownlen == newdsp->nfsclds_servownlen &&
5807 		    dsp->nfsclds_servownlen != 0 &&
5808 		    !NFSBCMP(dsp->nfsclds_serverown, newdsp->nfsclds_serverown,
5809 		    dsp->nfsclds_servownlen)) {
5810 			NFSCL_DEBUG(4, "fnd same fdsp=%p dsp=%p flg=0x%x\n",
5811 			    TAILQ_FIRST(&nmp->nm_sess), dsp,
5812 			    dsp->nfsclds_flags);
5813 			/* Server major id matches. */
5814 			if ((dsp->nfsclds_flags & NFSCLDS_DS) != 0) {
5815 				*retdspp = dsp;
5816 				return (NFSDSP_USETHISSESSION);
5817 			}
5818 
5819 			/*
5820 			 * Note the first match, so it can be used for
5821 			 * sequence'ing new sessions.
5822 			 */
5823 			if (cur_dsp == NULL)
5824 				cur_dsp = dsp;
5825 		}
5826 	}
5827 	if (cur_dsp != NULL) {
5828 		*retdspp = cur_dsp;
5829 		return (NFSDSP_SEQTHISSESSION);
5830 	}
5831 	return (NFSDSP_NOTFOUND);
5832 }
5833 
5834 #ifdef notyet
5835 /*
5836  * NFS commit rpc to a DS.
5837  */
5838 static int
5839 nfsrpc_commitds(vnode_t vp, uint64_t offset, int cnt, struct nfsclds *dsp,
5840     struct nfsfh *fhp, struct ucred *cred, NFSPROC_T *p, void *stuff)
5841 {
5842 	uint32_t *tl;
5843 	struct nfsrv_descript nfsd, *nd = &nfsd;
5844 	struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
5845 	struct nfssockreq *nrp;
5846 	int error;
5847 
5848 	nfscl_reqstart(nd, NFSPROC_COMMITDS, nmp, fhp->nfh_fh, fhp->nfh_len,
5849 	    NULL, &dsp->nfsclds_sess);
5850 	NFSM_BUILD(tl, uint32_t *, NFSX_HYPER + NFSX_UNSIGNED);
5851 	txdr_hyper(offset, tl);
5852 	tl += 2;
5853 	*tl = txdr_unsigned(cnt);
5854 	nrp = dsp->nfsclds_sockp;
5855 	if (nrp == NULL)
5856 		/* If NULL, use the MDS socket. */
5857 		nrp = &nmp->nm_sockreq;
5858 	error = newnfs_request(nd, nmp, NULL, nrp, vp, p, cred,
5859 	    NFS_PROG, NFS_VER4, NULL, 1, NULL, &dsp->nfsclds_sess);
5860 	if (error)
5861 		return (error);
5862 	if (nd->nd_repstat == 0) {
5863 		NFSM_DISSECT(tl, u_int32_t *, NFSX_VERF);
5864 		NFSLOCKDS(dsp);
5865 		if (NFSBCMP(tl, dsp->nfsclds_verf, NFSX_VERF)) {
5866 			NFSBCOPY(tl, dsp->nfsclds_verf, NFSX_VERF);
5867 			error = NFSERR_STALEWRITEVERF;
5868 		}
5869 		NFSUNLOCKDS(dsp);
5870 	}
5871 nfsmout:
5872 	if (error == 0 && nd->nd_repstat != 0)
5873 		error = nd->nd_repstat;
5874 	mbuf_freem(nd->nd_mrep);
5875 	return (error);
5876 }
5877 #endif
5878 
5879