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