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