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