xref: /freebsd/sys/fs/nfsclient/nfs_clrpcops.c (revision 4ed925457ab06e83238a5db33e89ccc94b99a713)
1 /*-
2  * Copyright (c) 1989, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Rick Macklem at The University of Guelph.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 4. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  */
33 
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
36 
37 /*
38  * Rpc op calls, generally called from the vnode op calls or through the
39  * buffer cache, for NFS v2, 3 and 4.
40  * These do not normally make any changes to vnode arguments or use
41  * structures that might change between the VFS variants. The returned
42  * arguments are all at the end, after the NFSPROC_T *p one.
43  */
44 
45 #ifndef APPLEKEXT
46 #include <fs/nfs/nfsport.h>
47 
48 /*
49  * Global variables
50  */
51 extern int nfs_numnfscbd;
52 extern struct timeval nfsboottime;
53 extern u_int32_t newnfs_false, newnfs_true;
54 extern nfstype nfsv34_type[9];
55 extern int nfsrv_useacl;
56 extern char nfsv4_callbackaddr[INET6_ADDRSTRLEN];
57 NFSCLSTATEMUTEX;
58 int nfstest_outofseq = 0;
59 int nfscl_assumeposixlocks = 1;
60 int nfscl_enablecallb = 0;
61 short nfsv4_cbport = NFSV4_CBPORT;
62 int nfstest_openallsetattr = 0;
63 #endif	/* !APPLEKEXT */
64 
65 #define	DIRHDSIZ	(sizeof (struct dirent) - (MAXNAMLEN + 1))
66 
67 static int nfsrpc_setattrrpc(vnode_t , struct vattr *, nfsv4stateid_t *,
68     struct ucred *, NFSPROC_T *, struct nfsvattr *, int *, void *);
69 static int nfsrpc_readrpc(vnode_t , struct uio *, struct ucred *,
70     nfsv4stateid_t *, NFSPROC_T *, struct nfsvattr *, int *, void *);
71 static int nfsrpc_writerpc(vnode_t , struct uio *, int *, u_char *,
72     struct ucred *, nfsv4stateid_t *, NFSPROC_T *, struct nfsvattr *, int *,
73     void *);
74 static int nfsrpc_createv23(vnode_t , char *, int, struct vattr *,
75     nfsquad_t, int, struct ucred *, NFSPROC_T *, struct nfsvattr *,
76     struct nfsvattr *, struct nfsfh **, int *, int *, void *);
77 static int nfsrpc_createv4(vnode_t , char *, int, struct vattr *,
78     nfsquad_t, int, struct nfsclowner *, struct nfscldeleg **, struct ucred *,
79     NFSPROC_T *, struct nfsvattr *, struct nfsvattr *, struct nfsfh **, int *,
80     int *, void *, int *);
81 static int nfsrpc_locku(struct nfsrv_descript *, struct nfsmount *,
82     struct nfscllockowner *, u_int64_t, u_int64_t,
83     u_int32_t, struct ucred *, NFSPROC_T *, int);
84 static int nfsrpc_setaclrpc(vnode_t, struct ucred *, NFSPROC_T *,
85     struct acl *, nfsv4stateid_t *, void *);
86 
87 /*
88  * nfs null call from vfs.
89  */
90 APPLESTATIC int
91 nfsrpc_null(vnode_t vp, struct ucred *cred, NFSPROC_T *p)
92 {
93 	int error;
94 	struct nfsrv_descript nfsd, *nd = &nfsd;
95 
96 	NFSCL_REQSTART(nd, NFSPROC_NULL, vp);
97 	error = nfscl_request(nd, vp, p, cred, NULL);
98 	if (nd->nd_repstat && !error)
99 		error = nd->nd_repstat;
100 	mbuf_freem(nd->nd_mrep);
101 	return (error);
102 }
103 
104 /*
105  * nfs access rpc op.
106  * For nfs version 3 and 4, use the access rpc to check accessibility. If file
107  * modes are changed on the server, accesses might still fail later.
108  */
109 APPLESTATIC int
110 nfsrpc_access(vnode_t vp, int acmode, struct ucred *cred,
111     NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp)
112 {
113 	int error;
114 	u_int32_t mode, rmode;
115 
116 	if (acmode & VREAD)
117 		mode = NFSACCESS_READ;
118 	else
119 		mode = 0;
120 	if (vnode_vtype(vp) == VDIR) {
121 		if (acmode & VWRITE)
122 			mode |= (NFSACCESS_MODIFY | NFSACCESS_EXTEND |
123 				 NFSACCESS_DELETE);
124 		if (acmode & VEXEC)
125 			mode |= NFSACCESS_LOOKUP;
126 	} else {
127 		if (acmode & VWRITE)
128 			mode |= (NFSACCESS_MODIFY | NFSACCESS_EXTEND);
129 		if (acmode & VEXEC)
130 			mode |= NFSACCESS_EXECUTE;
131 	}
132 
133 	/*
134 	 * Now, just call nfsrpc_accessrpc() to do the actual RPC.
135 	 */
136 	error = nfsrpc_accessrpc(vp, mode, cred, p, nap, attrflagp, &rmode,
137 	    NULL);
138 
139 	/*
140 	 * The NFS V3 spec does not clarify whether or not
141 	 * the returned access bits can be a superset of
142 	 * the ones requested, so...
143 	 */
144 	if (!error && (rmode & mode) != mode)
145 		error = EACCES;
146 	return (error);
147 }
148 
149 /*
150  * The actual rpc, separated out for Darwin.
151  */
152 APPLESTATIC int
153 nfsrpc_accessrpc(vnode_t vp, u_int32_t mode, struct ucred *cred,
154     NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, u_int32_t *rmodep,
155     void *stuff)
156 {
157 	u_int32_t *tl;
158 	u_int32_t supported, rmode;
159 	int error;
160 	struct nfsrv_descript nfsd, *nd = &nfsd;
161 	nfsattrbit_t attrbits;
162 
163 	*attrflagp = 0;
164 	supported = mode;
165 	NFSCL_REQSTART(nd, NFSPROC_ACCESS, vp);
166 	NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
167 	*tl = txdr_unsigned(mode);
168 	if (nd->nd_flag & ND_NFSV4) {
169 		/*
170 		 * And do a Getattr op.
171 		 */
172 		NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
173 		*tl = txdr_unsigned(NFSV4OP_GETATTR);
174 		NFSGETATTR_ATTRBIT(&attrbits);
175 		(void) nfsrv_putattrbit(nd, &attrbits);
176 	}
177 	error = nfscl_request(nd, vp, p, cred, stuff);
178 	if (error)
179 		return (error);
180 	if (nd->nd_flag & ND_NFSV3) {
181 		error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
182 		if (error)
183 			goto nfsmout;
184 	}
185 	if (!nd->nd_repstat) {
186 		if (nd->nd_flag & ND_NFSV4) {
187 			NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
188 			supported = fxdr_unsigned(u_int32_t, *tl++);
189 		} else {
190 			NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
191 		}
192 		rmode = fxdr_unsigned(u_int32_t, *tl);
193 		if (nd->nd_flag & ND_NFSV4)
194 			error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
195 
196 		/*
197 		 * It's not obvious what should be done about
198 		 * unsupported access modes. For now, be paranoid
199 		 * and clear the unsupported ones.
200 		 */
201 		rmode &= supported;
202 		*rmodep = rmode;
203 	} else
204 		error = nd->nd_repstat;
205 nfsmout:
206 	mbuf_freem(nd->nd_mrep);
207 	return (error);
208 }
209 
210 /*
211  * nfs open rpc
212  */
213 APPLESTATIC int
214 nfsrpc_open(vnode_t vp, int amode, struct ucred *cred, NFSPROC_T *p)
215 {
216 	struct nfsclopen *op;
217 	struct nfscldeleg *dp;
218 	struct nfsfh *nfhp;
219 	struct nfsnode *np = VTONFS(vp);
220 	struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
221 	u_int32_t mode, clidrev;
222 	int ret, newone, error, expireret = 0, retrycnt;
223 
224 	/*
225 	 * For NFSv4, Open Ops are only done on Regular Files.
226 	 */
227 	if (vnode_vtype(vp) != VREG)
228 		return (0);
229 	mode = 0;
230 	if (amode & FREAD)
231 		mode |= NFSV4OPEN_ACCESSREAD;
232 	if (amode & FWRITE)
233 		mode |= NFSV4OPEN_ACCESSWRITE;
234 	nfhp = np->n_fhp;
235 
236 	retrycnt = 0;
237 #ifdef notdef
238 { char name[100]; int namel;
239 namel = (np->n_v4->n4_namelen < 100) ? np->n_v4->n4_namelen : 99;
240 bcopy(NFS4NODENAME(np->n_v4), name, namel);
241 name[namel] = '\0';
242 printf("rpcopen p=0x%x name=%s",p->p_pid,name);
243 if (nfhp->nfh_len > 0) printf(" fh=0x%x\n",nfhp->nfh_fh[12]);
244 else printf(" fhl=0\n");
245 }
246 #endif
247 	do {
248 	    dp = NULL;
249 	    error = nfscl_open(vp, nfhp->nfh_fh, nfhp->nfh_len, mode, 1,
250 		cred, p, NULL, &op, &newone, &ret, 1);
251 	    if (error) {
252 		return (error);
253 	    }
254 	    if (nmp->nm_clp != NULL)
255 		clidrev = nmp->nm_clp->nfsc_clientidrev;
256 	    else
257 		clidrev = 0;
258 	    if (ret == NFSCLOPEN_DOOPEN) {
259 		if (np->n_v4 != NULL) {
260 			error = nfsrpc_openrpc(nmp, vp, np->n_v4->n4_data,
261 			   np->n_v4->n4_fhlen, np->n_fhp->nfh_fh,
262 			   np->n_fhp->nfh_len, mode, op,
263 			   NFS4NODENAME(np->n_v4), np->n_v4->n4_namelen, &dp,
264 			   0, 0x0, cred, p, 0, 0);
265 			if (dp != NULL) {
266 #ifdef APPLE
267 				OSBitAndAtomic((int32_t)~NDELEGMOD, (UInt32 *)&np->n_flag);
268 #else
269 				NFSLOCKNODE(np);
270 				np->n_flag &= ~NDELEGMOD;
271 				NFSUNLOCKNODE(np);
272 #endif
273 				(void) nfscl_deleg(nmp->nm_mountp,
274 				    op->nfso_own->nfsow_clp,
275 				    nfhp->nfh_fh, nfhp->nfh_len, cred, p, &dp);
276 			}
277 		} else {
278 			error = EIO;
279 		}
280 		newnfs_copyincred(cred, &op->nfso_cred);
281 	    }
282 
283 	    /*
284 	     * nfso_opencnt is the count of how many VOP_OPEN()s have
285 	     * been done on this Open successfully and a VOP_CLOSE()
286 	     * is expected for each of these.
287 	     * If error is non-zero, don't increment it, since the Open
288 	     * hasn't succeeded yet.
289 	     */
290 	    if (!error)
291 		op->nfso_opencnt++;
292 	    nfscl_openrelease(op, error, newone);
293 	    if (error == NFSERR_GRACE || error == NFSERR_STALECLIENTID ||
294 		error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY) {
295 		(void) nfs_catnap(PZERO, "nfs_open");
296 	    } else if ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID)
297 		&& clidrev != 0) {
298 		expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
299 		retrycnt++;
300 	    }
301 	} while (error == NFSERR_GRACE || error == NFSERR_STALECLIENTID ||
302 	    error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
303 	    ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
304 	     expireret == 0 && clidrev != 0 && retrycnt < 4));
305 	if (error && retrycnt >= 4)
306 		error = EIO;
307 	return (error);
308 }
309 
310 /*
311  * the actual open rpc
312  */
313 APPLESTATIC int
314 nfsrpc_openrpc(struct nfsmount *nmp, vnode_t vp, u_int8_t *nfhp, int fhlen,
315     u_int8_t *newfhp, int newfhlen, u_int32_t mode, struct nfsclopen *op,
316     u_int8_t *name, int namelen, struct nfscldeleg **dpp,
317     int reclaim, u_int32_t delegtype, struct ucred *cred, NFSPROC_T *p,
318     int syscred, int recursed)
319 {
320 	u_int32_t *tl;
321 	struct nfsrv_descript nfsd, *nd = &nfsd;
322 	struct nfscldeleg *dp, *ndp = NULL;
323 	struct nfsvattr nfsva;
324 	u_int32_t rflags, deleg;
325 	nfsattrbit_t attrbits;
326 	int error, ret, acesize, limitby;
327 
328 	dp = *dpp;
329 	*dpp = NULL;
330 	nfscl_reqstart(nd, NFSPROC_OPEN, nmp, nfhp, fhlen, NULL);
331 	NFSM_BUILD(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
332 	*tl++ = txdr_unsigned(op->nfso_own->nfsow_seqid);
333 	*tl++ = txdr_unsigned(mode & NFSV4OPEN_ACCESSBOTH);
334 	*tl++ = txdr_unsigned((mode >> NFSLCK_SHIFT) & NFSV4OPEN_DENYBOTH);
335 	*tl++ = op->nfso_own->nfsow_clp->nfsc_clientid.lval[0];
336 	*tl = op->nfso_own->nfsow_clp->nfsc_clientid.lval[1];
337 	(void) nfsm_strtom(nd, op->nfso_own->nfsow_owner, NFSV4CL_LOCKNAMELEN);
338 	NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
339 	*tl++ = txdr_unsigned(NFSV4OPEN_NOCREATE);
340 	if (reclaim) {
341 		*tl = txdr_unsigned(NFSV4OPEN_CLAIMPREVIOUS);
342 		NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
343 		*tl = txdr_unsigned(delegtype);
344 	} else {
345 		if (dp != NULL) {
346 			*tl = txdr_unsigned(NFSV4OPEN_CLAIMDELEGATECUR);
347 			NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID);
348 			*tl++ = dp->nfsdl_stateid.seqid;
349 			*tl++ = dp->nfsdl_stateid.other[0];
350 			*tl++ = dp->nfsdl_stateid.other[1];
351 			*tl = dp->nfsdl_stateid.other[2];
352 		} else {
353 			*tl = txdr_unsigned(NFSV4OPEN_CLAIMNULL);
354 		}
355 		(void) nfsm_strtom(nd, name, namelen);
356 	}
357 	NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
358 	*tl = txdr_unsigned(NFSV4OP_GETATTR);
359 	NFSZERO_ATTRBIT(&attrbits);
360 	NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_CHANGE);
361 	NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEMODIFY);
362 	(void) nfsrv_putattrbit(nd, &attrbits);
363 	if (syscred)
364 		nd->nd_flag |= ND_USEGSSNAME;
365 	error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, vp, p, cred,
366 	    NFS_PROG, NFS_VER4, NULL, 1, NULL);
367 	if (error)
368 		return (error);
369 	NFSCL_INCRSEQID(op->nfso_own->nfsow_seqid, nd);
370 	if (!nd->nd_repstat) {
371 		NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
372 		    6 * NFSX_UNSIGNED);
373 		op->nfso_stateid.seqid = *tl++;
374 		op->nfso_stateid.other[0] = *tl++;
375 		op->nfso_stateid.other[1] = *tl++;
376 		op->nfso_stateid.other[2] = *tl;
377 		rflags = fxdr_unsigned(u_int32_t, *(tl + 6));
378 		error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
379 		if (error)
380 			goto nfsmout;
381 		NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
382 		deleg = fxdr_unsigned(u_int32_t, *tl);
383 		if (deleg == NFSV4OPEN_DELEGATEREAD ||
384 		    deleg == NFSV4OPEN_DELEGATEWRITE) {
385 			if (!(op->nfso_own->nfsow_clp->nfsc_flags &
386 			      NFSCLFLAGS_FIRSTDELEG))
387 				op->nfso_own->nfsow_clp->nfsc_flags |=
388 				  (NFSCLFLAGS_FIRSTDELEG | NFSCLFLAGS_GOTDELEG);
389 			MALLOC(ndp, struct nfscldeleg *,
390 			    sizeof (struct nfscldeleg) + newfhlen,
391 			    M_NFSCLDELEG, M_WAITOK);
392 			LIST_INIT(&ndp->nfsdl_owner);
393 			LIST_INIT(&ndp->nfsdl_lock);
394 			ndp->nfsdl_clp = op->nfso_own->nfsow_clp;
395 			ndp->nfsdl_fhlen = newfhlen;
396 			NFSBCOPY(newfhp, ndp->nfsdl_fh, newfhlen);
397 			newnfs_copyincred(cred, &ndp->nfsdl_cred);
398 			nfscl_lockinit(&ndp->nfsdl_rwlock);
399 			NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
400 			    NFSX_UNSIGNED);
401 			ndp->nfsdl_stateid.seqid = *tl++;
402 			ndp->nfsdl_stateid.other[0] = *tl++;
403 			ndp->nfsdl_stateid.other[1] = *tl++;
404 			ndp->nfsdl_stateid.other[2] = *tl++;
405 			ret = fxdr_unsigned(int, *tl);
406 			if (deleg == NFSV4OPEN_DELEGATEWRITE) {
407 				ndp->nfsdl_flags = NFSCLDL_WRITE;
408 				/*
409 				 * Indicates how much the file can grow.
410 				 */
411 				NFSM_DISSECT(tl, u_int32_t *,
412 				    3 * NFSX_UNSIGNED);
413 				limitby = fxdr_unsigned(int, *tl++);
414 				switch (limitby) {
415 				case NFSV4OPEN_LIMITSIZE:
416 					ndp->nfsdl_sizelimit = fxdr_hyper(tl);
417 					break;
418 				case NFSV4OPEN_LIMITBLOCKS:
419 					ndp->nfsdl_sizelimit =
420 					    fxdr_unsigned(u_int64_t, *tl++);
421 					ndp->nfsdl_sizelimit *=
422 					    fxdr_unsigned(u_int64_t, *tl);
423 					break;
424 				default:
425 					error = NFSERR_BADXDR;
426 					goto nfsmout;
427 				};
428 			} else {
429 				ndp->nfsdl_flags = NFSCLDL_READ;
430 			}
431 			if (ret)
432 				ndp->nfsdl_flags |= NFSCLDL_RECALL;
433 			error = nfsrv_dissectace(nd, &ndp->nfsdl_ace, &ret,
434 			    &acesize, p);
435 			if (error)
436 				goto nfsmout;
437 		} else if (deleg != NFSV4OPEN_DELEGATENONE) {
438 			error = NFSERR_BADXDR;
439 			goto nfsmout;
440 		}
441 		NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
442 		error = nfsv4_loadattr(nd, NULL, &nfsva, NULL,
443 		    NULL, 0, NULL, NULL, NULL, NULL, NULL, 0,
444 		    NULL, NULL, NULL, p, cred);
445 		if (error)
446 			goto nfsmout;
447 		if (ndp != NULL) {
448 			ndp->nfsdl_change = nfsva.na_filerev;
449 			ndp->nfsdl_modtime = nfsva.na_mtime;
450 			ndp->nfsdl_flags |= NFSCLDL_MODTIMESET;
451 		}
452 		if (!reclaim && (rflags & NFSV4OPEN_RESULTCONFIRM)) {
453 		    do {
454 			ret = nfsrpc_openconfirm(vp, newfhp, newfhlen, op,
455 			    cred, p);
456 			if (ret == NFSERR_DELAY)
457 			    (void) nfs_catnap(PZERO, "nfs_open");
458 		    } while (ret == NFSERR_DELAY);
459 		    error = ret;
460 		}
461 		if ((rflags & NFSV4OPEN_LOCKTYPEPOSIX) ||
462 		    nfscl_assumeposixlocks)
463 		    op->nfso_posixlock = 1;
464 		else
465 		    op->nfso_posixlock = 0;
466 
467 		/*
468 		 * If the server is handing out delegations, but we didn't
469 		 * get one because an OpenConfirm was required, try the
470 		 * Open again, to get a delegation. This is a harmless no-op,
471 		 * from a server's point of view.
472 		 */
473 		if (!reclaim && (rflags & NFSV4OPEN_RESULTCONFIRM) &&
474 		    (op->nfso_own->nfsow_clp->nfsc_flags & NFSCLFLAGS_GOTDELEG)
475 		    && !error && dp == NULL && ndp == NULL && !recursed) {
476 		    do {
477 			ret = nfsrpc_openrpc(nmp, vp, nfhp, fhlen, newfhp,
478 			    newfhlen, mode, op, name, namelen, &ndp, 0, 0x0,
479 			    cred, p, syscred, 1);
480 			if (ret == NFSERR_DELAY)
481 			    (void) nfs_catnap(PZERO, "nfs_open2");
482 		    } while (ret == NFSERR_DELAY);
483 		    if (ret) {
484 			if (ndp != NULL)
485 				FREE((caddr_t)ndp, M_NFSCLDELEG);
486 			if (ret == NFSERR_STALECLIENTID ||
487 			    ret == NFSERR_STALEDONTRECOVER)
488 				error = ret;
489 		    }
490 		}
491 	}
492 	if (nd->nd_repstat != 0 && error == 0)
493 		error = nd->nd_repstat;
494 	if (error == NFSERR_STALECLIENTID)
495 		nfscl_initiate_recovery(op->nfso_own->nfsow_clp);
496 nfsmout:
497 	if (!error)
498 		*dpp = ndp;
499 	else if (ndp != NULL)
500 		FREE((caddr_t)ndp, M_NFSCLDELEG);
501 	mbuf_freem(nd->nd_mrep);
502 	return (error);
503 }
504 
505 /*
506  * open downgrade rpc
507  */
508 APPLESTATIC int
509 nfsrpc_opendowngrade(vnode_t vp, u_int32_t mode, struct nfsclopen *op,
510     struct ucred *cred, NFSPROC_T *p)
511 {
512 	u_int32_t *tl;
513 	struct nfsrv_descript nfsd, *nd = &nfsd;
514 	int error;
515 
516 	NFSCL_REQSTART(nd, NFSPROC_OPENDOWNGRADE, vp);
517 	NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID + 3 * NFSX_UNSIGNED);
518 	*tl++ = op->nfso_stateid.seqid;
519 	*tl++ = op->nfso_stateid.other[0];
520 	*tl++ = op->nfso_stateid.other[1];
521 	*tl++ = op->nfso_stateid.other[2];
522 	*tl++ = txdr_unsigned(op->nfso_own->nfsow_seqid);
523 	*tl++ = txdr_unsigned(mode & NFSV4OPEN_ACCESSBOTH);
524 	*tl = txdr_unsigned((mode >> NFSLCK_SHIFT) & NFSV4OPEN_DENYBOTH);
525 	error = nfscl_request(nd, vp, p, cred, NULL);
526 	if (error)
527 		return (error);
528 	NFSCL_INCRSEQID(op->nfso_own->nfsow_seqid, nd);
529 	if (!nd->nd_repstat) {
530 		NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID);
531 		op->nfso_stateid.seqid = *tl++;
532 		op->nfso_stateid.other[0] = *tl++;
533 		op->nfso_stateid.other[1] = *tl++;
534 		op->nfso_stateid.other[2] = *tl;
535 	}
536 	if (nd->nd_repstat && error == 0)
537 		error = nd->nd_repstat;
538 	if (error == NFSERR_STALESTATEID)
539 		nfscl_initiate_recovery(op->nfso_own->nfsow_clp);
540 nfsmout:
541 	mbuf_freem(nd->nd_mrep);
542 	return (error);
543 }
544 
545 /*
546  * V4 Close operation.
547  */
548 APPLESTATIC int
549 nfsrpc_close(vnode_t vp, int doclose, NFSPROC_T *p)
550 {
551 	struct nfsclclient *clp;
552 	int error;
553 
554 	if (vnode_vtype(vp) != VREG)
555 		return (0);
556 	if (doclose)
557 		error = nfscl_doclose(vp, &clp, p);
558 	else
559 		error = nfscl_getclose(vp, &clp);
560 	if (error)
561 		return (error);
562 
563 	nfscl_clientrelease(clp);
564 	return (0);
565 }
566 
567 /*
568  * Close the open.
569  */
570 APPLESTATIC void
571 nfsrpc_doclose(struct nfsmount *nmp, struct nfsclopen *op, NFSPROC_T *p)
572 {
573 	struct nfsrv_descript nfsd, *nd = &nfsd;
574 	struct nfscllockowner *lp;
575 	struct nfscllock *lop, *nlop;
576 	struct ucred *tcred;
577 	u_int64_t off = 0, len = 0;
578 	u_int32_t type = NFSV4LOCKT_READ;
579 	int error, do_unlock, trycnt;
580 
581 	tcred = newnfs_getcred();
582 	newnfs_copycred(&op->nfso_cred, tcred);
583 	/*
584 	 * (Theoretically this could be done in the same
585 	 *  compound as the close, but having multiple
586 	 *  sequenced Ops in the same compound might be
587 	 *  too scary for some servers.)
588 	 */
589 	if (op->nfso_posixlock) {
590 		off = 0;
591 		len = NFS64BITSSET;
592 		type = NFSV4LOCKT_READ;
593 	}
594 
595 	/*
596 	 * Since this function is only called from VOP_INACTIVE(), no
597 	 * other thread will be manipulating this Open. As such, the
598 	 * lock lists are not being changed by other threads, so it should
599 	 * be safe to do this without locking.
600 	 */
601 	LIST_FOREACH(lp, &op->nfso_lock, nfsl_list) {
602 		do_unlock = 1;
603 		LIST_FOREACH_SAFE(lop, &lp->nfsl_lock, nfslo_list, nlop) {
604 			if (op->nfso_posixlock == 0) {
605 				off = lop->nfslo_first;
606 				len = lop->nfslo_end - lop->nfslo_first;
607 				if (lop->nfslo_type == F_WRLCK)
608 					type = NFSV4LOCKT_WRITE;
609 				else
610 					type = NFSV4LOCKT_READ;
611 			}
612 			if (do_unlock) {
613 				trycnt = 0;
614 				do {
615 					error = nfsrpc_locku(nd, nmp, lp, off,
616 					    len, type, tcred, p, 0);
617 					if ((nd->nd_repstat == NFSERR_GRACE ||
618 					    nd->nd_repstat == NFSERR_DELAY) &&
619 					    error == 0)
620 						(void) nfs_catnap(PZERO,
621 						    "nfs_close");
622 				} while ((nd->nd_repstat == NFSERR_GRACE ||
623 				    nd->nd_repstat == NFSERR_DELAY) &&
624 				    error == 0 && trycnt++ < 5);
625 				if (op->nfso_posixlock)
626 					do_unlock = 0;
627 			}
628 			nfscl_freelock(lop, 0);
629 		}
630 	}
631 
632 	/*
633 	 * There could be other Opens for different files on the same
634 	 * OpenOwner, so locking is required.
635 	 */
636 	NFSLOCKCLSTATE();
637 	nfscl_lockexcl(&op->nfso_own->nfsow_rwlock, NFSCLSTATEMUTEXPTR);
638 	NFSUNLOCKCLSTATE();
639 	do {
640 		error = nfscl_tryclose(op, tcred, nmp, p);
641 		if (error == NFSERR_GRACE)
642 			(void) nfs_catnap(PZERO, "nfs_close");
643 	} while (error == NFSERR_GRACE);
644 	NFSLOCKCLSTATE();
645 	nfscl_lockunlock(&op->nfso_own->nfsow_rwlock);
646 
647 	/*
648 	 * Move the lockowner to nfsc_defunctlockowner,
649 	 * so the Renew thread will do the ReleaseLockOwner
650 	 * Op on it later. There might still be other
651 	 * opens using the same lockowner name.
652 	 */
653 	lp = LIST_FIRST(&op->nfso_lock);
654 	if (lp != NULL) {
655 		while (LIST_NEXT(lp, nfsl_list) != NULL)
656 			lp = LIST_NEXT(lp, nfsl_list);
657 		LIST_PREPEND(&nmp->nm_clp->nfsc_defunctlockowner,
658 		    &op->nfso_lock, lp, nfsl_list);
659 		LIST_INIT(&op->nfso_lock);
660 	}
661 	nfscl_freeopen(op, 0);
662 	NFSUNLOCKCLSTATE();
663 	NFSFREECRED(tcred);
664 }
665 
666 /*
667  * The actual Close RPC.
668  */
669 APPLESTATIC int
670 nfsrpc_closerpc(struct nfsrv_descript *nd, struct nfsmount *nmp,
671     struct nfsclopen *op, struct ucred *cred, NFSPROC_T *p,
672     int syscred)
673 {
674 	u_int32_t *tl;
675 	int error;
676 
677 	nfscl_reqstart(nd, NFSPROC_CLOSE, nmp, op->nfso_fh,
678 	    op->nfso_fhlen, NULL);
679 	NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED + NFSX_STATEID);
680 	*tl++ = txdr_unsigned(op->nfso_own->nfsow_seqid);
681 	*tl++ = op->nfso_stateid.seqid;
682 	*tl++ = op->nfso_stateid.other[0];
683 	*tl++ = op->nfso_stateid.other[1];
684 	*tl = op->nfso_stateid.other[2];
685 	if (syscred)
686 		nd->nd_flag |= ND_USEGSSNAME;
687 	error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
688 	    NFS_PROG, NFS_VER4, NULL, 1, NULL);
689 	if (error)
690 		return (error);
691 	NFSCL_INCRSEQID(op->nfso_own->nfsow_seqid, nd);
692 	if (nd->nd_repstat == 0)
693 		NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID);
694 	error = nd->nd_repstat;
695 	if (error == NFSERR_STALESTATEID)
696 		nfscl_initiate_recovery(op->nfso_own->nfsow_clp);
697 nfsmout:
698 	mbuf_freem(nd->nd_mrep);
699 	return (error);
700 }
701 
702 /*
703  * V4 Open Confirm RPC.
704  */
705 APPLESTATIC int
706 nfsrpc_openconfirm(vnode_t vp, u_int8_t *nfhp, int fhlen,
707     struct nfsclopen *op, struct ucred *cred, NFSPROC_T *p)
708 {
709 	u_int32_t *tl;
710 	struct nfsrv_descript nfsd, *nd = &nfsd;
711 	int error;
712 
713 	nfscl_reqstart(nd, NFSPROC_OPENCONFIRM, VFSTONFS(vnode_mount(vp)),
714 	    nfhp, fhlen, NULL);
715 	NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED + NFSX_STATEID);
716 	*tl++ = op->nfso_stateid.seqid;
717 	*tl++ = op->nfso_stateid.other[0];
718 	*tl++ = op->nfso_stateid.other[1];
719 	*tl++ = op->nfso_stateid.other[2];
720 	*tl = txdr_unsigned(op->nfso_own->nfsow_seqid);
721 	error = nfscl_request(nd, vp, p, cred, NULL);
722 	if (error)
723 		return (error);
724 	NFSCL_INCRSEQID(op->nfso_own->nfsow_seqid, nd);
725 	if (!nd->nd_repstat) {
726 		NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID);
727 		op->nfso_stateid.seqid = *tl++;
728 		op->nfso_stateid.other[0] = *tl++;
729 		op->nfso_stateid.other[1] = *tl++;
730 		op->nfso_stateid.other[2] = *tl;
731 	}
732 	error = nd->nd_repstat;
733 	if (error == NFSERR_STALESTATEID)
734 		nfscl_initiate_recovery(op->nfso_own->nfsow_clp);
735 nfsmout:
736 	mbuf_freem(nd->nd_mrep);
737 	return (error);
738 }
739 
740 /*
741  * Do the setclientid and setclientid confirm RPCs. Called from nfs_statfs()
742  * when a mount has just occurred and when the server replies NFSERR_EXPIRED.
743  */
744 APPLESTATIC int
745 nfsrpc_setclient(struct nfsmount *nmp, struct nfsclclient *clp,
746     struct ucred *cred, NFSPROC_T *p)
747 {
748 	u_int32_t *tl;
749 	struct nfsrv_descript nfsd;
750 	struct nfsrv_descript *nd = &nfsd;
751 	nfsattrbit_t attrbits;
752 	u_int8_t *cp = NULL, *cp2, addr[INET6_ADDRSTRLEN + 9];
753 	u_short port;
754 	int error, isinet6 = 0, callblen;
755 	nfsquad_t confirm;
756 	u_int32_t lease;
757 	static u_int32_t rev = 0;
758 
759 	if (nfsboottime.tv_sec == 0)
760 		NFSSETBOOTTIME(nfsboottime);
761 	nfscl_reqstart(nd, NFSPROC_SETCLIENTID, nmp, NULL, 0, NULL);
762 	NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
763 	*tl++ = txdr_unsigned(nfsboottime.tv_sec);
764 	*tl = txdr_unsigned(rev++);
765 	(void) nfsm_strtom(nd, clp->nfsc_id, clp->nfsc_idlen);
766 
767 	/*
768 	 * set up the callback address
769 	 */
770 	NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
771 	*tl = txdr_unsigned(NFS_CALLBCKPROG);
772 	callblen = strlen(nfsv4_callbackaddr);
773 	if (callblen == 0)
774 		cp = nfscl_getmyip(nmp, &isinet6);
775 	if (nfscl_enablecallb && nfs_numnfscbd > 0 &&
776 	    (callblen > 0 || cp != NULL)) {
777 		port = htons(nfsv4_cbport);
778 		cp2 = (u_int8_t *)&port;
779 #ifdef INET6
780 		if ((callblen > 0 &&
781 		     strchr(nfsv4_callbackaddr, ':')) || isinet6) {
782 			char ip6buf[INET6_ADDRSTRLEN], *ip6add;
783 
784 			(void) nfsm_strtom(nd, "tcp6", 4);
785 			if (callblen == 0) {
786 				ip6_sprintf(ip6buf, (struct in6_addr *)cp);
787 				ip6add = ip6buf;
788 			} else {
789 				ip6add = nfsv4_callbackaddr;
790 			}
791 			snprintf(addr, INET6_ADDRSTRLEN + 9, "%s.%d.%d",
792 			    ip6add, cp2[0], cp2[1]);
793 		} else
794 #endif
795 		{
796 			(void) nfsm_strtom(nd, "tcp", 3);
797 			if (callblen == 0)
798 				snprintf(addr, INET6_ADDRSTRLEN + 9,
799 				    "%d.%d.%d.%d.%d.%d", cp[0], cp[1],
800 				    cp[2], cp[3], cp2[0], cp2[1]);
801 			else
802 				snprintf(addr, INET6_ADDRSTRLEN + 9,
803 				    "%s.%d.%d", nfsv4_callbackaddr,
804 				    cp2[0], cp2[1]);
805 		}
806 		(void) nfsm_strtom(nd, addr, strlen(addr));
807 	} else {
808 		(void) nfsm_strtom(nd, "tcp", 3);
809 		(void) nfsm_strtom(nd, "0.0.0.0.0.0", 11);
810 	}
811 	NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
812 	*tl = txdr_unsigned(clp->nfsc_cbident);
813 	nd->nd_flag |= ND_USEGSSNAME;
814 	error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
815 		NFS_PROG, NFS_VER4, NULL, 1, NULL);
816 	if (error)
817 		return (error);
818 	if (nd->nd_repstat == 0) {
819 	    NFSM_DISSECT(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
820 	    clp->nfsc_clientid.lval[0] = *tl++;
821 	    clp->nfsc_clientid.lval[1] = *tl++;
822 	    confirm.lval[0] = *tl++;
823 	    confirm.lval[1] = *tl;
824 	    mbuf_freem(nd->nd_mrep);
825 	    nd->nd_mrep = NULL;
826 
827 	    /*
828 	     * and confirm it.
829 	     */
830 	    nfscl_reqstart(nd, NFSPROC_SETCLIENTIDCFRM, nmp, NULL, 0, NULL);
831 	    NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
832 	    *tl++ = clp->nfsc_clientid.lval[0];
833 	    *tl++ = clp->nfsc_clientid.lval[1];
834 	    *tl++ = confirm.lval[0];
835 	    *tl = confirm.lval[1];
836 	    nd->nd_flag |= ND_USEGSSNAME;
837 	    error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p,
838 		cred, NFS_PROG, NFS_VER4, NULL, 1, NULL);
839 	    if (error)
840 		return (error);
841 	    mbuf_freem(nd->nd_mrep);
842 	    nd->nd_mrep = NULL;
843 	    if (nd->nd_repstat == 0) {
844 		nfscl_reqstart(nd, NFSPROC_GETATTR, nmp, nmp->nm_fh,
845 		    nmp->nm_fhsize, NULL);
846 		NFSZERO_ATTRBIT(&attrbits);
847 		NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_LEASETIME);
848 		(void) nfsrv_putattrbit(nd, &attrbits);
849 		nd->nd_flag |= ND_USEGSSNAME;
850 		error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p,
851 		    cred, NFS_PROG, NFS_VER4, NULL, 1, NULL);
852 		if (error)
853 		    return (error);
854 		if (nd->nd_repstat == 0) {
855 		    error = nfsv4_loadattr(nd, NULL, NULL, NULL, NULL, 0, NULL,
856 			NULL, NULL, NULL, NULL, 0, NULL, &lease, NULL, p, cred);
857 		    if (error)
858 			goto nfsmout;
859 		    clp->nfsc_renew = NFSCL_RENEW(lease);
860 		    clp->nfsc_expire = NFSD_MONOSEC + clp->nfsc_renew;
861 		    clp->nfsc_clientidrev++;
862 		    if (clp->nfsc_clientidrev == 0)
863 			clp->nfsc_clientidrev++;
864 		}
865 	    }
866 	}
867 	error = nd->nd_repstat;
868 nfsmout:
869 	mbuf_freem(nd->nd_mrep);
870 	return (error);
871 }
872 
873 /*
874  * nfs getattr call.
875  */
876 APPLESTATIC int
877 nfsrpc_getattr(vnode_t vp, struct ucred *cred, NFSPROC_T *p,
878     struct nfsvattr *nap, void *stuff)
879 {
880 	struct nfsrv_descript nfsd, *nd = &nfsd;
881 	int error;
882 	nfsattrbit_t attrbits;
883 
884 	NFSCL_REQSTART(nd, NFSPROC_GETATTR, vp);
885 	if (nd->nd_flag & ND_NFSV4) {
886 		NFSGETATTR_ATTRBIT(&attrbits);
887 		(void) nfsrv_putattrbit(nd, &attrbits);
888 	}
889 	error = nfscl_request(nd, vp, p, cred, stuff);
890 	if (error)
891 		return (error);
892 	if (!nd->nd_repstat)
893 		error = nfsm_loadattr(nd, nap);
894 	else
895 		error = nd->nd_repstat;
896 	mbuf_freem(nd->nd_mrep);
897 	return (error);
898 }
899 
900 /*
901  * nfs getattr call with non-vnode arguemnts.
902  */
903 APPLESTATIC int
904 nfsrpc_getattrnovp(struct nfsmount *nmp, u_int8_t *fhp, int fhlen, int syscred,
905     struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, u_int64_t *xidp)
906 {
907 	struct nfsrv_descript nfsd, *nd = &nfsd;
908 	int error, vers = NFS_VER2;
909 	nfsattrbit_t attrbits;
910 
911 	nfscl_reqstart(nd, NFSPROC_GETATTR, nmp, fhp, fhlen, NULL);
912 	if (nd->nd_flag & ND_NFSV4) {
913 		vers = NFS_VER4;
914 		NFSGETATTR_ATTRBIT(&attrbits);
915 		(void) nfsrv_putattrbit(nd, &attrbits);
916 	} else if (nd->nd_flag & ND_NFSV3) {
917 		vers = NFS_VER3;
918 	}
919 	if (syscred)
920 		nd->nd_flag |= ND_USEGSSNAME;
921 	error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
922 	    NFS_PROG, vers, NULL, 1, xidp);
923 	if (error)
924 		return (error);
925 	if (!nd->nd_repstat)
926 		error = nfsm_loadattr(nd, nap);
927 	else
928 		error = nd->nd_repstat;
929 	mbuf_freem(nd->nd_mrep);
930 	return (error);
931 }
932 
933 /*
934  * Do an nfs setattr operation.
935  */
936 APPLESTATIC int
937 nfsrpc_setattr(vnode_t vp, struct vattr *vap, NFSACL_T *aclp,
938     struct ucred *cred, NFSPROC_T *p, struct nfsvattr *rnap, int *attrflagp,
939     void *stuff)
940 {
941 	int error, expireret = 0, openerr, retrycnt;
942 	u_int32_t clidrev = 0, mode;
943 	struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
944 	struct nfsfh *nfhp;
945 	nfsv4stateid_t stateid;
946 	void *lckp;
947 
948 	if (nmp->nm_clp != NULL)
949 		clidrev = nmp->nm_clp->nfsc_clientidrev;
950 	if (vap != NULL && NFSATTRISSET(u_quad_t, vap, va_size))
951 		mode = NFSV4OPEN_ACCESSWRITE;
952 	else
953 		mode = NFSV4OPEN_ACCESSREAD;
954 	retrycnt = 0;
955 	do {
956 		lckp = NULL;
957 		openerr = 1;
958 		if (NFSHASNFSV4(nmp)) {
959 			nfhp = VTONFS(vp)->n_fhp;
960 			error = nfscl_getstateid(vp, nfhp->nfh_fh,
961 			    nfhp->nfh_len, mode, cred, p, &stateid, &lckp);
962 			if (error && vnode_vtype(vp) == VREG &&
963 			    (mode == NFSV4OPEN_ACCESSWRITE ||
964 			     nfstest_openallsetattr)) {
965 				/*
966 				 * No Open stateid, so try and open the file
967 				 * now.
968 				 */
969 				if (mode == NFSV4OPEN_ACCESSWRITE)
970 					openerr = nfsrpc_open(vp, FWRITE, cred,
971 					    p);
972 				else
973 					openerr = nfsrpc_open(vp, FREAD, cred,
974 					    p);
975 				if (!openerr)
976 					(void) nfscl_getstateid(vp,
977 					    nfhp->nfh_fh, nfhp->nfh_len,
978 					    mode, cred, p, &stateid, &lckp);
979 			}
980 		}
981 		if (vap != NULL)
982 			error = nfsrpc_setattrrpc(vp, vap, &stateid, cred, p,
983 			    rnap, attrflagp, stuff);
984 		else
985 			error = nfsrpc_setaclrpc(vp, cred, p, aclp, &stateid,
986 			    stuff);
987 		if (error == NFSERR_STALESTATEID)
988 			nfscl_initiate_recovery(nmp->nm_clp);
989 		if (lckp != NULL)
990 			nfscl_lockderef(lckp);
991 		if (!openerr)
992 			(void) nfsrpc_close(vp, 0, p);
993 		if (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
994 		    error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
995 		    error == NFSERR_OLDSTATEID) {
996 			(void) nfs_catnap(PZERO, "nfs_setattr");
997 		} else if ((error == NFSERR_EXPIRED ||
998 		    error == NFSERR_BADSTATEID) && clidrev != 0) {
999 			expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
1000 		}
1001 		retrycnt++;
1002 	} while (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
1003 	    error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
1004 	    (error == NFSERR_OLDSTATEID && retrycnt < 20) ||
1005 	    ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
1006 	     expireret == 0 && clidrev != 0 && retrycnt < 4));
1007 	if (error && retrycnt >= 4)
1008 		error = EIO;
1009 	return (error);
1010 }
1011 
1012 static int
1013 nfsrpc_setattrrpc(vnode_t vp, struct vattr *vap,
1014     nfsv4stateid_t *stateidp, struct ucred *cred, NFSPROC_T *p,
1015     struct nfsvattr *rnap, int *attrflagp, void *stuff)
1016 {
1017 	u_int32_t *tl;
1018 	struct nfsrv_descript nfsd, *nd = &nfsd;
1019 	int error;
1020 	nfsattrbit_t attrbits;
1021 
1022 	*attrflagp = 0;
1023 	NFSCL_REQSTART(nd, NFSPROC_SETATTR, vp);
1024 	if (nd->nd_flag & ND_NFSV4)
1025 		nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID);
1026 	vap->va_type = vnode_vtype(vp);
1027 	nfscl_fillsattr(nd, vap, vp, NFSSATTR_FULL, 0);
1028 	if (nd->nd_flag & ND_NFSV3) {
1029 		NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1030 		*tl = newnfs_false;
1031 	} else if (nd->nd_flag & ND_NFSV4) {
1032 		NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1033 		*tl = txdr_unsigned(NFSV4OP_GETATTR);
1034 		NFSGETATTR_ATTRBIT(&attrbits);
1035 		(void) nfsrv_putattrbit(nd, &attrbits);
1036 	}
1037 	error = nfscl_request(nd, vp, p, cred, stuff);
1038 	if (error)
1039 		return (error);
1040 	if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4))
1041 		error = nfscl_wcc_data(nd, vp, rnap, attrflagp, NULL, stuff);
1042 	if ((nd->nd_flag & ND_NFSV4) && !error)
1043 		error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
1044 	if (!(nd->nd_flag & ND_NFSV3) && !nd->nd_repstat && !error)
1045 		error = nfscl_postop_attr(nd, rnap, attrflagp, stuff);
1046 	mbuf_freem(nd->nd_mrep);
1047 	if (nd->nd_repstat && !error)
1048 		error = nd->nd_repstat;
1049 	return (error);
1050 }
1051 
1052 /*
1053  * nfs lookup rpc
1054  */
1055 APPLESTATIC int
1056 nfsrpc_lookup(vnode_t dvp, char *name, int len, struct ucred *cred,
1057     NFSPROC_T *p, struct nfsvattr *dnap, struct nfsvattr *nap,
1058     struct nfsfh **nfhpp, int *attrflagp, int *dattrflagp, void *stuff)
1059 {
1060 	u_int32_t *tl;
1061 	struct nfsrv_descript nfsd, *nd = &nfsd;
1062 	struct nfsmount *nmp;
1063 	struct nfsnode *np;
1064 	struct nfsfh *nfhp;
1065 	nfsattrbit_t attrbits;
1066 	int error = 0, lookupp = 0;
1067 
1068 	*attrflagp = 0;
1069 	*dattrflagp = 0;
1070 	if (vnode_vtype(dvp) != VDIR)
1071 		return (ENOTDIR);
1072 	nmp = VFSTONFS(vnode_mount(dvp));
1073 	if (len > NFS_MAXNAMLEN)
1074 		return (ENAMETOOLONG);
1075 	if (NFSHASNFSV4(nmp) && len == 1 &&
1076 		name[0] == '.') {
1077 		/*
1078 		 * Just return the current dir's fh.
1079 		 */
1080 		np = VTONFS(dvp);
1081 		MALLOC(nfhp, struct nfsfh *, sizeof (struct nfsfh) +
1082 			np->n_fhp->nfh_len, M_NFSFH, M_WAITOK);
1083 		nfhp->nfh_len = np->n_fhp->nfh_len;
1084 		NFSBCOPY(np->n_fhp->nfh_fh, nfhp->nfh_fh, nfhp->nfh_len);
1085 		*nfhpp = nfhp;
1086 		return (0);
1087 	}
1088 	if (NFSHASNFSV4(nmp) && len == 2 &&
1089 		name[0] == '.' && name[1] == '.') {
1090 		lookupp = 1;
1091 		NFSCL_REQSTART(nd, NFSPROC_LOOKUPP, dvp);
1092 	} else {
1093 		NFSCL_REQSTART(nd, NFSPROC_LOOKUP, dvp);
1094 		(void) nfsm_strtom(nd, name, len);
1095 	}
1096 	if (nd->nd_flag & ND_NFSV4) {
1097 		NFSGETATTR_ATTRBIT(&attrbits);
1098 		NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1099 		*tl++ = txdr_unsigned(NFSV4OP_GETFH);
1100 		*tl = txdr_unsigned(NFSV4OP_GETATTR);
1101 		(void) nfsrv_putattrbit(nd, &attrbits);
1102 	}
1103 	error = nfscl_request(nd, dvp, p, cred, stuff);
1104 	if (error)
1105 		return (error);
1106 	if (nd->nd_repstat) {
1107 		/*
1108 		 * When an NFSv4 Lookupp returns ENOENT, it means that
1109 		 * the lookup is at the root of an fs, so return this dir.
1110 		 */
1111 		if (nd->nd_repstat == NFSERR_NOENT && lookupp) {
1112 		    np = VTONFS(dvp);
1113 		    MALLOC(nfhp, struct nfsfh *, sizeof (struct nfsfh) +
1114 			np->n_fhp->nfh_len, M_NFSFH, M_WAITOK);
1115 		    nfhp->nfh_len = np->n_fhp->nfh_len;
1116 		    NFSBCOPY(np->n_fhp->nfh_fh, nfhp->nfh_fh, nfhp->nfh_len);
1117 		    *nfhpp = nfhp;
1118 		    mbuf_freem(nd->nd_mrep);
1119 		    return (0);
1120 		}
1121 		if (nd->nd_flag & ND_NFSV3)
1122 		    error = nfscl_postop_attr(nd, dnap, dattrflagp, stuff);
1123 		goto nfsmout;
1124 	}
1125 	if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) == ND_NFSV4) {
1126 		NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1127 		if (*(tl + 1)) {
1128 			nd->nd_flag |= ND_NOMOREDATA;
1129 			goto nfsmout;
1130 		}
1131 	}
1132 	error = nfsm_getfh(nd, nfhpp);
1133 	if (error)
1134 		goto nfsmout;
1135 
1136 	error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
1137 	if ((nd->nd_flag & ND_NFSV3) && !error)
1138 		error = nfscl_postop_attr(nd, dnap, dattrflagp, stuff);
1139 nfsmout:
1140 	mbuf_freem(nd->nd_mrep);
1141 	if (!error && nd->nd_repstat)
1142 		error = nd->nd_repstat;
1143 	return (error);
1144 }
1145 
1146 /*
1147  * Do a readlink rpc.
1148  */
1149 APPLESTATIC int
1150 nfsrpc_readlink(vnode_t vp, struct uio *uiop, struct ucred *cred,
1151     NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, void *stuff)
1152 {
1153 	u_int32_t *tl;
1154 	struct nfsrv_descript nfsd, *nd = &nfsd;
1155 	struct nfsnode *np = VTONFS(vp);
1156 	nfsattrbit_t attrbits;
1157 	int error, len, cangetattr = 1;
1158 
1159 	*attrflagp = 0;
1160 	NFSCL_REQSTART(nd, NFSPROC_READLINK, vp);
1161 	if (nd->nd_flag & ND_NFSV4) {
1162 		/*
1163 		 * And do a Getattr op.
1164 		 */
1165 		NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1166 		*tl = txdr_unsigned(NFSV4OP_GETATTR);
1167 		NFSGETATTR_ATTRBIT(&attrbits);
1168 		(void) nfsrv_putattrbit(nd, &attrbits);
1169 	}
1170 	error = nfscl_request(nd, vp, p, cred, stuff);
1171 	if (error)
1172 		return (error);
1173 	if (nd->nd_flag & ND_NFSV3)
1174 		error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
1175 	if (!nd->nd_repstat && !error) {
1176 		NFSM_STRSIZ(len, NFS_MAXPATHLEN);
1177 		/*
1178 		 * This seems weird to me, but must have been added to
1179 		 * FreeBSD for some reason. The only thing I can think of
1180 		 * is that there was/is some server that replies with
1181 		 * more link data than it should?
1182 		 */
1183 		if (len == NFS_MAXPATHLEN) {
1184 			NFSLOCKNODE(np);
1185 			if (np->n_size > 0 && np->n_size < NFS_MAXPATHLEN) {
1186 				len = np->n_size;
1187 				cangetattr = 0;
1188 			}
1189 			NFSUNLOCKNODE(np);
1190 		}
1191 		error = nfsm_mbufuio(nd, uiop, len);
1192 		if ((nd->nd_flag & ND_NFSV4) && !error && cangetattr)
1193 			error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
1194 	}
1195 	if (nd->nd_repstat && !error)
1196 		error = nd->nd_repstat;
1197 nfsmout:
1198 	mbuf_freem(nd->nd_mrep);
1199 	return (error);
1200 }
1201 
1202 /*
1203  * Read operation.
1204  */
1205 APPLESTATIC int
1206 nfsrpc_read(vnode_t vp, struct uio *uiop, struct ucred *cred,
1207     NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, void *stuff)
1208 {
1209 	int error, expireret = 0, retrycnt;
1210 	u_int32_t clidrev = 0;
1211 	struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
1212 	struct nfsnode *np = VTONFS(vp);
1213 	struct ucred *newcred;
1214 	struct nfsfh *nfhp = NULL;
1215 	nfsv4stateid_t stateid;
1216 	void *lckp;
1217 
1218 	if (nmp->nm_clp != NULL)
1219 		clidrev = nmp->nm_clp->nfsc_clientidrev;
1220 	newcred = cred;
1221 	if (NFSHASNFSV4(nmp)) {
1222 		nfhp = np->n_fhp;
1223 		if (p == NULL)
1224 			newcred = NFSNEWCRED(cred);
1225 	}
1226 	retrycnt = 0;
1227 	do {
1228 		lckp = NULL;
1229 		if (NFSHASNFSV4(nmp))
1230 			(void)nfscl_getstateid(vp, nfhp->nfh_fh, nfhp->nfh_len,
1231 			    NFSV4OPEN_ACCESSREAD, newcred, p, &stateid, &lckp);
1232 		error = nfsrpc_readrpc(vp, uiop, newcred, &stateid, p, nap,
1233 		    attrflagp, stuff);
1234 		if (error == NFSERR_STALESTATEID)
1235 			nfscl_initiate_recovery(nmp->nm_clp);
1236 		if (lckp != NULL)
1237 			nfscl_lockderef(lckp);
1238 		if (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
1239 		    error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
1240 		    error == NFSERR_OLDSTATEID) {
1241 			(void) nfs_catnap(PZERO, "nfs_read");
1242 		} else if ((error == NFSERR_EXPIRED ||
1243 		    error == NFSERR_BADSTATEID) && clidrev != 0) {
1244 			expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
1245 		}
1246 		retrycnt++;
1247 	} while (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
1248 	    error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
1249 	    (error == NFSERR_OLDSTATEID && retrycnt < 20) ||
1250 	    ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
1251 	     expireret == 0 && clidrev != 0 && retrycnt < 4));
1252 	if (error && retrycnt >= 4)
1253 		error = EIO;
1254 	if (NFSHASNFSV4(nmp) && p == NULL)
1255 		NFSFREECRED(newcred);
1256 	return (error);
1257 }
1258 
1259 /*
1260  * The actual read RPC.
1261  */
1262 static int
1263 nfsrpc_readrpc(vnode_t vp, struct uio *uiop, struct ucred *cred,
1264     nfsv4stateid_t *stateidp, NFSPROC_T *p, struct nfsvattr *nap,
1265     int *attrflagp, void *stuff)
1266 {
1267 	u_int32_t *tl;
1268 	int error = 0, len, retlen, tsiz, eof = 0;
1269 	struct nfsrv_descript nfsd;
1270 	struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
1271 	struct nfsrv_descript *nd = &nfsd;
1272 
1273 	*attrflagp = 0;
1274 	tsiz = uio_uio_resid(uiop);
1275 	if (uiop->uio_offset + tsiz > 0xffffffff &&
1276 	    !NFSHASNFSV3OR4(nmp))
1277 		return (EFBIG);
1278 	nd->nd_mrep = NULL;
1279 	while (tsiz > 0) {
1280 		*attrflagp = 0;
1281 		len = (tsiz > nmp->nm_rsize) ? nmp->nm_rsize : tsiz;
1282 		NFSCL_REQSTART(nd, NFSPROC_READ, vp);
1283 		if (nd->nd_flag & ND_NFSV4)
1284 			nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID);
1285 		NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED * 3);
1286 		if (nd->nd_flag & ND_NFSV2) {
1287 			*tl++ = txdr_unsigned(uiop->uio_offset);
1288 			*tl++ = txdr_unsigned(len);
1289 			*tl = 0;
1290 		} else {
1291 			txdr_hyper(uiop->uio_offset, tl);
1292 			*(tl + 2) = txdr_unsigned(len);
1293 		}
1294 		/*
1295 		 * Since I can't do a Getattr for NFSv4 for Write, there
1296 		 * doesn't seem any point in doing one here, either.
1297 		 * (See the comment in nfsrpc_writerpc() for more info.)
1298 		 */
1299 		error = nfscl_request(nd, vp, p, cred, stuff);
1300 		if (error)
1301 			return (error);
1302 		if (nd->nd_flag & ND_NFSV3) {
1303 			error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
1304 		} else if (!nd->nd_repstat && (nd->nd_flag & ND_NFSV2)) {
1305 			error = nfsm_loadattr(nd, nap);
1306 			if (!error)
1307 				*attrflagp = 1;
1308 		}
1309 		if (nd->nd_repstat || error) {
1310 			if (!error)
1311 				error = nd->nd_repstat;
1312 			goto nfsmout;
1313 		}
1314 		if (nd->nd_flag & ND_NFSV3) {
1315 			NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1316 			eof = fxdr_unsigned(int, *(tl + 1));
1317 		} else if (nd->nd_flag & ND_NFSV4) {
1318 			NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
1319 			eof = fxdr_unsigned(int, *tl);
1320 		}
1321 		NFSM_STRSIZ(retlen, nmp->nm_rsize);
1322 		error = nfsm_mbufuio(nd, uiop, retlen);
1323 		if (error)
1324 			goto nfsmout;
1325 		mbuf_freem(nd->nd_mrep);
1326 		nd->nd_mrep = NULL;
1327 		tsiz -= retlen;
1328 		if (!(nd->nd_flag & ND_NFSV2)) {
1329 			if (eof || retlen == 0)
1330 				tsiz = 0;
1331 		} else if (retlen < len)
1332 			tsiz = 0;
1333 	}
1334 	return (0);
1335 nfsmout:
1336 	if (nd->nd_mrep != NULL)
1337 		mbuf_freem(nd->nd_mrep);
1338 	return (error);
1339 }
1340 
1341 /*
1342  * nfs write operation
1343  */
1344 APPLESTATIC int
1345 nfsrpc_write(vnode_t vp, struct uio *uiop, int *iomode, u_char *verfp,
1346     struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp,
1347     void *stuff)
1348 {
1349 	int error, expireret = 0, retrycnt, nostateid;
1350 	u_int32_t clidrev = 0;
1351 	struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
1352 	struct nfsnode *np = VTONFS(vp);
1353 	struct ucred *newcred;
1354 	struct nfsfh *nfhp = NULL;
1355 	nfsv4stateid_t stateid;
1356 	void *lckp;
1357 
1358 	if (nmp->nm_clp != NULL)
1359 		clidrev = nmp->nm_clp->nfsc_clientidrev;
1360 	newcred = cred;
1361 	if (NFSHASNFSV4(nmp)) {
1362 		if (p == NULL)
1363 			newcred = NFSNEWCRED(cred);
1364 		nfhp = np->n_fhp;
1365 	}
1366 	retrycnt = 0;
1367 	do {
1368 		lckp = NULL;
1369 		nostateid = 0;
1370 		if (NFSHASNFSV4(nmp)) {
1371 			(void)nfscl_getstateid(vp, nfhp->nfh_fh, nfhp->nfh_len,
1372 			    NFSV4OPEN_ACCESSWRITE, newcred, p, &stateid, &lckp);
1373 			if (stateid.other[0] == 0 && stateid.other[1] == 0 &&
1374 			    stateid.other[2] == 0) {
1375 				nostateid = 1;
1376 				printf("stateid0 in write\n");
1377 			}
1378 		}
1379 
1380 		/*
1381 		 * If there is no stateid for NFSv4, it means this is an
1382 		 * extraneous write after close. Basically a poorly
1383 		 * implemented buffer cache. Just don't do the write.
1384 		 */
1385 		if (nostateid)
1386 			error = 0;
1387 		else
1388 			error = nfsrpc_writerpc(vp, uiop, iomode, verfp,
1389 			    newcred, &stateid, p, nap, attrflagp, stuff);
1390 if (error == NFSERR_BADSTATEID) {
1391 printf("st=0x%x 0x%x 0x%x\n",stateid.other[0],stateid.other[1],stateid.other[2]);
1392 nfscl_dumpstate(nmp, 1, 1, 0, 0);
1393 }
1394 		if (error == NFSERR_STALESTATEID)
1395 			nfscl_initiate_recovery(nmp->nm_clp);
1396 		if (lckp != NULL)
1397 			nfscl_lockderef(lckp);
1398 		if (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
1399 		    error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
1400 		    error == NFSERR_OLDSTATEID) {
1401 			(void) nfs_catnap(PZERO, "nfs_write");
1402 		} else if ((error == NFSERR_EXPIRED ||
1403 		    error == NFSERR_BADSTATEID) && clidrev != 0) {
1404 			expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
1405 		}
1406 		retrycnt++;
1407 	} while (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
1408 	    error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
1409 	    (error == NFSERR_OLDSTATEID && retrycnt < 20) ||
1410 	    ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
1411 	     expireret == 0 && clidrev != 0 && retrycnt < 4));
1412 	if (error && retrycnt >= 4)
1413 		error = EIO;
1414 	if (NFSHASNFSV4(nmp) && p == NULL)
1415 		NFSFREECRED(newcred);
1416 	return (error);
1417 }
1418 
1419 /*
1420  * The actual write RPC.
1421  */
1422 static int
1423 nfsrpc_writerpc(vnode_t vp, struct uio *uiop, int *iomode,
1424     u_char *verfp, struct ucred *cred, nfsv4stateid_t *stateidp,
1425     NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, void *stuff)
1426 {
1427 	u_int32_t *tl;
1428 	struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
1429 	struct nfsnode *np = VTONFS(vp);
1430 	int error = 0, len, tsiz, rlen, commit, committed = NFSWRITE_FILESYNC;
1431 	int wccflag = 0, wsize;
1432 	int32_t backup;
1433 	struct nfsrv_descript nfsd;
1434 	struct nfsrv_descript *nd = &nfsd;
1435 	nfsattrbit_t attrbits;
1436 
1437 #ifdef DIAGNOSTIC
1438 	if (uiop->uio_iovcnt != 1)
1439 		panic("nfs: writerpc iovcnt > 1");
1440 #endif
1441 	*attrflagp = 0;
1442 	tsiz = uio_uio_resid(uiop);
1443 	NFSLOCKMNT(nmp);
1444 	if (uiop->uio_offset + tsiz > 0xffffffff &&
1445 	    !NFSHASNFSV3OR4(nmp)) {
1446 		NFSUNLOCKMNT(nmp);
1447 		return (EFBIG);
1448 	}
1449 	wsize = nmp->nm_wsize;
1450 	NFSUNLOCKMNT(nmp);
1451 	nd->nd_mrep = NULL;	/* NFSv2 sometimes does a write with */
1452 	nd->nd_repstat = 0;	/* uio_resid == 0, so the while is not done */
1453 	while (tsiz > 0) {
1454 		nmp = VFSTONFS(vnode_mount(vp));
1455 		if (nmp == NULL) {
1456 			error = ENXIO;
1457 			goto nfsmout;
1458 		}
1459 		*attrflagp = 0;
1460 		len = (tsiz > wsize) ? wsize : tsiz;
1461 		NFSCL_REQSTART(nd, NFSPROC_WRITE, vp);
1462 		if (nd->nd_flag & ND_NFSV4) {
1463 			nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID);
1464 			NFSM_BUILD(tl, u_int32_t *, NFSX_HYPER+2*NFSX_UNSIGNED);
1465 			txdr_hyper(uiop->uio_offset, tl);
1466 			tl += 2;
1467 			*tl++ = txdr_unsigned(*iomode);
1468 			*tl = txdr_unsigned(len);
1469 		} else if (nd->nd_flag & ND_NFSV3) {
1470 			NFSM_BUILD(tl, u_int32_t *, NFSX_HYPER+3*NFSX_UNSIGNED);
1471 			txdr_hyper(uiop->uio_offset, tl);
1472 			tl += 2;
1473 			*tl++ = txdr_unsigned(len);
1474 			*tl++ = txdr_unsigned(*iomode);
1475 			*tl = txdr_unsigned(len);
1476 		} else {
1477 			u_int32_t x;
1478 
1479 			NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1480 			/*
1481 			 * Not sure why someone changed this, since the
1482 			 * RFC clearly states that "beginoffset" and
1483 			 * "totalcount" are ignored, but it wouldn't
1484 			 * surprise me if there's a busted server out there.
1485 			 */
1486 			/* Set both "begin" and "current" to non-garbage. */
1487 			x = txdr_unsigned((u_int32_t)uiop->uio_offset);
1488 			*tl++ = x;      /* "begin offset" */
1489 			*tl++ = x;      /* "current offset" */
1490 			x = txdr_unsigned(len);
1491 			*tl++ = x;      /* total to this offset */
1492 			*tl = x;        /* size of this write */
1493 
1494 		}
1495 		nfsm_uiombuf(nd, uiop, len);
1496 		/*
1497 		 * Although it is tempting to do a normal Getattr Op in the
1498 		 * NFSv4 compound, the result can be a nearly hung client
1499 		 * system if the Getattr asks for Owner and/or OwnerGroup.
1500 		 * It occurs when the client can't map either the Owner or
1501 		 * Owner_group name in the Getattr reply to a uid/gid. When
1502 		 * there is a cache miss, the kernel does an upcall to the
1503 		 * nfsuserd. Then, it can try and read the local /etc/passwd
1504 		 * or /etc/group file. It can then block in getnewbuf(),
1505 		 * waiting for dirty writes to be pushed to the NFS server.
1506 		 * The only reason this doesn't result in a complete
1507 		 * deadlock, is that the upcall times out and allows
1508 		 * the write to complete. However, progress is so slow
1509 		 * that it might just as well be deadlocked.
1510 		 * So, we just get the attributes that change with each
1511 		 * write Op.
1512 		 * nb: nfscl_loadattrcache() needs to be told that these
1513 		 *     partial attributes from a write rpc are being
1514 		 *     passed in, via a argument flag.
1515 		 */
1516 		if (nd->nd_flag & ND_NFSV4) {
1517 			NFSWRITEGETATTR_ATTRBIT(&attrbits);
1518 			NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1519 			*tl = txdr_unsigned(NFSV4OP_GETATTR);
1520 			(void) nfsrv_putattrbit(nd, &attrbits);
1521 		}
1522 		error = nfscl_request(nd, vp, p, cred, stuff);
1523 		if (error)
1524 			return (error);
1525 		if (nd->nd_repstat) {
1526 			/*
1527 			 * In case the rpc gets retried, roll
1528 			 * the uio fileds changed by nfsm_uiombuf()
1529 			 * back.
1530 			 */
1531 			uiop->uio_offset -= len;
1532 			uio_uio_resid_add(uiop, len);
1533 			uio_iov_base_add(uiop, -len);
1534 			uio_iov_len_add(uiop, len);
1535 		}
1536 		if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) {
1537 			error = nfscl_wcc_data(nd, vp, nap, attrflagp,
1538 			    &wccflag, stuff);
1539 			if (error)
1540 				goto nfsmout;
1541 		}
1542 		if (!nd->nd_repstat) {
1543 			if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) {
1544 				NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED
1545 					+ NFSX_VERF);
1546 				rlen = fxdr_unsigned(int, *tl++);
1547 				if (rlen == 0) {
1548 					error = NFSERR_IO;
1549 					goto nfsmout;
1550 				} else if (rlen < len) {
1551 					backup = len - rlen;
1552 					uio_iov_base_add(uiop, -(backup));
1553 					uio_iov_len_add(uiop, backup);
1554 					uiop->uio_offset -= backup;
1555 					uio_uio_resid_add(uiop, backup);
1556 					len = rlen;
1557 				}
1558 				commit = fxdr_unsigned(int, *tl++);
1559 
1560 				/*
1561 				 * Return the lowest committment level
1562 				 * obtained by any of the RPCs.
1563 				 */
1564 				if (committed == NFSWRITE_FILESYNC)
1565 					committed = commit;
1566 				else if (committed == NFSWRITE_DATASYNC &&
1567 					commit == NFSWRITE_UNSTABLE)
1568 					committed = commit;
1569 				if (verfp != NULL)
1570 					NFSBCOPY((caddr_t)tl, verfp, NFSX_VERF);
1571 				NFSLOCKMNT(nmp);
1572 				if (!NFSHASWRITEVERF(nmp)) {
1573 					NFSBCOPY((caddr_t)tl,
1574 					    (caddr_t)&nmp->nm_verf[0],
1575 					    NFSX_VERF);
1576 					NFSSETWRITEVERF(nmp);
1577 				}
1578 				NFSUNLOCKMNT(nmp);
1579 			}
1580 			if (nd->nd_flag & ND_NFSV4)
1581 				NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1582 			if (nd->nd_flag & (ND_NFSV2 | ND_NFSV4)) {
1583 				error = nfsm_loadattr(nd, nap);
1584 				if (!error)
1585 					*attrflagp = NFS_LATTR_NOSHRINK;
1586 			}
1587 		} else {
1588 			error = nd->nd_repstat;
1589 		}
1590 		if (error)
1591 			goto nfsmout;
1592 		NFSWRITERPC_SETTIME(wccflag, np, (nd->nd_flag & ND_NFSV4));
1593 		mbuf_freem(nd->nd_mrep);
1594 		nd->nd_mrep = NULL;
1595 		tsiz -= len;
1596 	}
1597 nfsmout:
1598 	if (nd->nd_mrep != NULL)
1599 		mbuf_freem(nd->nd_mrep);
1600 	*iomode = committed;
1601 	if (nd->nd_repstat && !error)
1602 		error = nd->nd_repstat;
1603 	return (error);
1604 }
1605 
1606 /*
1607  * nfs mknod rpc
1608  * For NFS v2 this is a kludge. Use a create rpc but with the IFMT bits of the
1609  * mode set to specify the file type and the size field for rdev.
1610  */
1611 APPLESTATIC int
1612 nfsrpc_mknod(vnode_t dvp, char *name, int namelen, struct vattr *vap,
1613     u_int32_t rdev, enum vtype vtyp, struct ucred *cred, NFSPROC_T *p,
1614     struct nfsvattr *dnap, struct nfsvattr *nnap, struct nfsfh **nfhpp,
1615     int *attrflagp, int *dattrflagp, void *dstuff)
1616 {
1617 	u_int32_t *tl;
1618 	int error = 0;
1619 	struct nfsrv_descript nfsd, *nd = &nfsd;
1620 	nfsattrbit_t attrbits;
1621 
1622 	*nfhpp = NULL;
1623 	*attrflagp = 0;
1624 	*dattrflagp = 0;
1625 	if (namelen > NFS_MAXNAMLEN)
1626 		return (ENAMETOOLONG);
1627 	NFSCL_REQSTART(nd, NFSPROC_MKNOD, dvp);
1628 	if (nd->nd_flag & ND_NFSV4) {
1629 		if (vtyp == VBLK || vtyp == VCHR) {
1630 			NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
1631 			*tl++ = vtonfsv34_type(vtyp);
1632 			*tl++ = txdr_unsigned(NFSMAJOR(rdev));
1633 			*tl = txdr_unsigned(NFSMINOR(rdev));
1634 		} else {
1635 			NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1636 			*tl = vtonfsv34_type(vtyp);
1637 		}
1638 	}
1639 	(void) nfsm_strtom(nd, name, namelen);
1640 	if (nd->nd_flag & ND_NFSV3) {
1641 		NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1642 		*tl = vtonfsv34_type(vtyp);
1643 	}
1644 	if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4))
1645 		nfscl_fillsattr(nd, vap, dvp, 0, 0);
1646 	if ((nd->nd_flag & ND_NFSV3) &&
1647 	    (vtyp == VCHR || vtyp == VBLK)) {
1648 		NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1649 		*tl++ = txdr_unsigned(NFSMAJOR(rdev));
1650 		*tl = txdr_unsigned(NFSMINOR(rdev));
1651 	}
1652 	if (nd->nd_flag & ND_NFSV4) {
1653 		NFSGETATTR_ATTRBIT(&attrbits);
1654 		NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1655 		*tl++ = txdr_unsigned(NFSV4OP_GETFH);
1656 		*tl = txdr_unsigned(NFSV4OP_GETATTR);
1657 		(void) nfsrv_putattrbit(nd, &attrbits);
1658 	}
1659 	if (nd->nd_flag & ND_NFSV2)
1660 		nfscl_fillsattr(nd, vap, dvp, NFSSATTR_SIZERDEV, rdev);
1661 	error = nfscl_request(nd, dvp, p, cred, dstuff);
1662 	if (error)
1663 		return (error);
1664 	if (nd->nd_flag & ND_NFSV4)
1665 		error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
1666 	if (!nd->nd_repstat) {
1667 		if (nd->nd_flag & ND_NFSV4) {
1668 			NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
1669 			error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
1670 			if (error)
1671 				goto nfsmout;
1672 		}
1673 		error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp);
1674 		if (error)
1675 			goto nfsmout;
1676 	}
1677 	if (nd->nd_flag & ND_NFSV3)
1678 		error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
1679 	if (!error && nd->nd_repstat)
1680 		error = nd->nd_repstat;
1681 nfsmout:
1682 	mbuf_freem(nd->nd_mrep);
1683 	return (error);
1684 }
1685 
1686 /*
1687  * nfs file create call
1688  * Mostly just call the approriate routine. (I separated out v4, so that
1689  * error recovery wouldn't be as difficult.)
1690  */
1691 APPLESTATIC int
1692 nfsrpc_create(vnode_t dvp, char *name, int namelen, struct vattr *vap,
1693     nfsquad_t cverf, int fmode, struct ucred *cred, NFSPROC_T *p,
1694     struct nfsvattr *dnap, struct nfsvattr *nnap, struct nfsfh **nfhpp,
1695     int *attrflagp, int *dattrflagp, void *dstuff)
1696 {
1697 	int error = 0, newone, expireret = 0, retrycnt, unlocked;
1698 	struct nfsclowner *owp;
1699 	struct nfscldeleg *dp;
1700 	struct nfsmount *nmp = VFSTONFS(vnode_mount(dvp));
1701 	u_int32_t clidrev;
1702 
1703 	if (NFSHASNFSV4(nmp)) {
1704 	    retrycnt = 0;
1705 	    do {
1706 		dp = NULL;
1707 		error = nfscl_open(dvp, NULL, 0, (NFSV4OPEN_ACCESSWRITE |
1708 		    NFSV4OPEN_ACCESSREAD), 0, cred, p, &owp, NULL, &newone,
1709 		    NULL, 1);
1710 		if (error)
1711 			return (error);
1712 		if (nmp->nm_clp != NULL)
1713 			clidrev = nmp->nm_clp->nfsc_clientidrev;
1714 		else
1715 			clidrev = 0;
1716 		error = nfsrpc_createv4(dvp, name, namelen, vap, cverf, fmode,
1717 		  owp, &dp, cred, p, dnap, nnap, nfhpp, attrflagp, dattrflagp,
1718 		  dstuff, &unlocked);
1719 		if (dp != NULL)
1720 			(void) nfscl_deleg(nmp->nm_mountp, owp->nfsow_clp,
1721 			    (*nfhpp)->nfh_fh, (*nfhpp)->nfh_len, cred, p, &dp);
1722 		nfscl_ownerrelease(owp, error, newone, unlocked);
1723 		if (error == NFSERR_GRACE || error == NFSERR_STALECLIENTID ||
1724 		    error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY) {
1725 			(void) nfs_catnap(PZERO, "nfs_open");
1726 		} else if ((error == NFSERR_EXPIRED ||
1727 		    error == NFSERR_BADSTATEID) && clidrev != 0) {
1728 			expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
1729 			retrycnt++;
1730 		}
1731 	    } while (error == NFSERR_GRACE || error == NFSERR_STALECLIENTID ||
1732 		error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
1733 		((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
1734 		 expireret == 0 && clidrev != 0 && retrycnt < 4));
1735 	    if (error && retrycnt >= 4)
1736 		    error = EIO;
1737 	} else {
1738 		error = nfsrpc_createv23(dvp, name, namelen, vap, cverf,
1739 		    fmode, cred, p, dnap, nnap, nfhpp, attrflagp, dattrflagp,
1740 		    dstuff);
1741 	}
1742 	return (error);
1743 }
1744 
1745 /*
1746  * The create rpc for v2 and 3.
1747  */
1748 static int
1749 nfsrpc_createv23(vnode_t dvp, char *name, int namelen, struct vattr *vap,
1750     nfsquad_t cverf, int fmode, struct ucred *cred, NFSPROC_T *p,
1751     struct nfsvattr *dnap, struct nfsvattr *nnap, struct nfsfh **nfhpp,
1752     int *attrflagp, int *dattrflagp, void *dstuff)
1753 {
1754 	u_int32_t *tl;
1755 	int error = 0;
1756 	struct nfsrv_descript nfsd, *nd = &nfsd;
1757 
1758 	*nfhpp = NULL;
1759 	*attrflagp = 0;
1760 	*dattrflagp = 0;
1761 	if (namelen > NFS_MAXNAMLEN)
1762 		return (ENAMETOOLONG);
1763 	NFSCL_REQSTART(nd, NFSPROC_CREATE, dvp);
1764 	(void) nfsm_strtom(nd, name, namelen);
1765 	if (nd->nd_flag & ND_NFSV3) {
1766 		NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1767 		if (fmode & O_EXCL) {
1768 			*tl = txdr_unsigned(NFSCREATE_EXCLUSIVE);
1769 			NFSM_BUILD(tl, u_int32_t *, NFSX_VERF);
1770 			*tl++ = cverf.lval[0];
1771 			*tl = cverf.lval[1];
1772 		} else {
1773 			*tl = txdr_unsigned(NFSCREATE_UNCHECKED);
1774 			nfscl_fillsattr(nd, vap, dvp, 0, 0);
1775 		}
1776 	} else {
1777 		nfscl_fillsattr(nd, vap, dvp, NFSSATTR_SIZE0, 0);
1778 	}
1779 	error = nfscl_request(nd, dvp, p, cred, dstuff);
1780 	if (error)
1781 		return (error);
1782 	if (nd->nd_repstat == 0) {
1783 		error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp);
1784 		if (error)
1785 			goto nfsmout;
1786 	}
1787 	if (nd->nd_flag & ND_NFSV3)
1788 		error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
1789 	if (nd->nd_repstat != 0 && error == 0)
1790 		error = nd->nd_repstat;
1791 nfsmout:
1792 	mbuf_freem(nd->nd_mrep);
1793 	return (error);
1794 }
1795 
1796 static int
1797 nfsrpc_createv4(vnode_t dvp, char *name, int namelen, struct vattr *vap,
1798     nfsquad_t cverf, int fmode, struct nfsclowner *owp, struct nfscldeleg **dpp,
1799     struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap,
1800     struct nfsvattr *nnap, struct nfsfh **nfhpp, int *attrflagp,
1801     int *dattrflagp, void *dstuff, int *unlockedp)
1802 {
1803 	u_int32_t *tl;
1804 	int error = 0, deleg, newone, ret, acesize, limitby;
1805 	struct nfsrv_descript nfsd, *nd = &nfsd;
1806 	struct nfsclopen *op;
1807 	struct nfscldeleg *dp = NULL;
1808 	struct nfsnode *np;
1809 	struct nfsfh *nfhp;
1810 	nfsattrbit_t attrbits;
1811 	nfsv4stateid_t stateid;
1812 	u_int32_t rflags;
1813 
1814 	*unlockedp = 0;
1815 	*nfhpp = NULL;
1816 	*dpp = NULL;
1817 	*attrflagp = 0;
1818 	*dattrflagp = 0;
1819 	if (namelen > NFS_MAXNAMLEN)
1820 		return (ENAMETOOLONG);
1821 	NFSCL_REQSTART(nd, NFSPROC_CREATE, dvp);
1822 	/*
1823 	 * For V4, this is actually an Open op.
1824 	 */
1825 	NFSM_BUILD(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
1826 	*tl++ = txdr_unsigned(owp->nfsow_seqid);
1827 	*tl++ = txdr_unsigned(NFSV4OPEN_ACCESSWRITE |
1828 	    NFSV4OPEN_ACCESSREAD);
1829 	*tl++ = txdr_unsigned(NFSV4OPEN_DENYNONE);
1830 	*tl++ = owp->nfsow_clp->nfsc_clientid.lval[0];
1831 	*tl = owp->nfsow_clp->nfsc_clientid.lval[1];
1832 	(void) nfsm_strtom(nd, owp->nfsow_owner, NFSV4CL_LOCKNAMELEN);
1833 	NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1834 	*tl++ = txdr_unsigned(NFSV4OPEN_CREATE);
1835 	if (fmode & O_EXCL) {
1836 		*tl = txdr_unsigned(NFSCREATE_EXCLUSIVE);
1837 		NFSM_BUILD(tl, u_int32_t *, NFSX_VERF);
1838 		*tl++ = cverf.lval[0];
1839 		*tl = cverf.lval[1];
1840 	} else {
1841 		*tl = txdr_unsigned(NFSCREATE_UNCHECKED);
1842 		nfscl_fillsattr(nd, vap, dvp, 0, 0);
1843 	}
1844 	NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1845 	*tl = txdr_unsigned(NFSV4OPEN_CLAIMNULL);
1846 	(void) nfsm_strtom(nd, name, namelen);
1847 	NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1848 	*tl++ = txdr_unsigned(NFSV4OP_GETFH);
1849 	*tl = txdr_unsigned(NFSV4OP_GETATTR);
1850 	NFSGETATTR_ATTRBIT(&attrbits);
1851 	(void) nfsrv_putattrbit(nd, &attrbits);
1852 	error = nfscl_request(nd, dvp, p, cred, dstuff);
1853 	if (error)
1854 		return (error);
1855 	error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
1856 	if (error)
1857 		goto nfsmout;
1858 	NFSCL_INCRSEQID(owp->nfsow_seqid, nd);
1859 	if (nd->nd_repstat == 0) {
1860 		NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
1861 		    6 * NFSX_UNSIGNED);
1862 		stateid.seqid = *tl++;
1863 		stateid.other[0] = *tl++;
1864 		stateid.other[1] = *tl++;
1865 		stateid.other[2] = *tl;
1866 		rflags = fxdr_unsigned(u_int32_t, *(tl + 6));
1867 		(void) nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
1868 		NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
1869 		deleg = fxdr_unsigned(int, *tl);
1870 		if (deleg == NFSV4OPEN_DELEGATEREAD ||
1871 		    deleg == NFSV4OPEN_DELEGATEWRITE) {
1872 			if (!(owp->nfsow_clp->nfsc_flags &
1873 			      NFSCLFLAGS_FIRSTDELEG))
1874 				owp->nfsow_clp->nfsc_flags |=
1875 				  (NFSCLFLAGS_FIRSTDELEG | NFSCLFLAGS_GOTDELEG);
1876 			MALLOC(dp, struct nfscldeleg *,
1877 			    sizeof (struct nfscldeleg) + NFSX_V4FHMAX,
1878 			    M_NFSCLDELEG, M_WAITOK);
1879 			LIST_INIT(&dp->nfsdl_owner);
1880 			LIST_INIT(&dp->nfsdl_lock);
1881 			dp->nfsdl_clp = owp->nfsow_clp;
1882 			newnfs_copyincred(cred, &dp->nfsdl_cred);
1883 			nfscl_lockinit(&dp->nfsdl_rwlock);
1884 			NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
1885 			    NFSX_UNSIGNED);
1886 			dp->nfsdl_stateid.seqid = *tl++;
1887 			dp->nfsdl_stateid.other[0] = *tl++;
1888 			dp->nfsdl_stateid.other[1] = *tl++;
1889 			dp->nfsdl_stateid.other[2] = *tl++;
1890 			ret = fxdr_unsigned(int, *tl);
1891 			if (deleg == NFSV4OPEN_DELEGATEWRITE) {
1892 				dp->nfsdl_flags = NFSCLDL_WRITE;
1893 				/*
1894 				 * Indicates how much the file can grow.
1895 				 */
1896 				NFSM_DISSECT(tl, u_int32_t *,
1897 				    3 * NFSX_UNSIGNED);
1898 				limitby = fxdr_unsigned(int, *tl++);
1899 				switch (limitby) {
1900 				case NFSV4OPEN_LIMITSIZE:
1901 					dp->nfsdl_sizelimit = fxdr_hyper(tl);
1902 					break;
1903 				case NFSV4OPEN_LIMITBLOCKS:
1904 					dp->nfsdl_sizelimit =
1905 					    fxdr_unsigned(u_int64_t, *tl++);
1906 					dp->nfsdl_sizelimit *=
1907 					    fxdr_unsigned(u_int64_t, *tl);
1908 					break;
1909 				default:
1910 					error = NFSERR_BADXDR;
1911 					goto nfsmout;
1912 				};
1913 			} else {
1914 				dp->nfsdl_flags = NFSCLDL_READ;
1915 			}
1916 			if (ret)
1917 				dp->nfsdl_flags |= NFSCLDL_RECALL;
1918 			error = nfsrv_dissectace(nd, &dp->nfsdl_ace, &ret,
1919 			    &acesize, p);
1920 			if (error)
1921 				goto nfsmout;
1922 		} else if (deleg != NFSV4OPEN_DELEGATENONE) {
1923 			error = NFSERR_BADXDR;
1924 			goto nfsmout;
1925 		}
1926 		error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp);
1927 		if (error)
1928 			goto nfsmout;
1929 		if (dp != NULL && *attrflagp) {
1930 			dp->nfsdl_change = nnap->na_filerev;
1931 			dp->nfsdl_modtime = nnap->na_mtime;
1932 			dp->nfsdl_flags |= NFSCLDL_MODTIMESET;
1933 		}
1934 		/*
1935 		 * We can now complete the Open state.
1936 		 */
1937 		nfhp = *nfhpp;
1938 		if (dp != NULL) {
1939 			dp->nfsdl_fhlen = nfhp->nfh_len;
1940 			NFSBCOPY(nfhp->nfh_fh, dp->nfsdl_fh, nfhp->nfh_len);
1941 		}
1942 		/*
1943 		 * Get an Open structure that will be
1944 		 * attached to the OpenOwner, acquired already.
1945 		 */
1946 		error = nfscl_open(dvp, nfhp->nfh_fh, nfhp->nfh_len,
1947 		    (NFSV4OPEN_ACCESSWRITE | NFSV4OPEN_ACCESSREAD), 0,
1948 		    cred, p, NULL, &op, &newone, NULL, 0);
1949 		if (error)
1950 			goto nfsmout;
1951 		op->nfso_stateid = stateid;
1952 		newnfs_copyincred(cred, &op->nfso_cred);
1953 		if ((rflags & NFSV4OPEN_RESULTCONFIRM)) {
1954 		    do {
1955 			ret = nfsrpc_openconfirm(dvp, nfhp->nfh_fh,
1956 			    nfhp->nfh_len, op, cred, p);
1957 			if (ret == NFSERR_DELAY)
1958 			    (void) nfs_catnap(PZERO, "nfs_create");
1959 		    } while (ret == NFSERR_DELAY);
1960 		    error = ret;
1961 		}
1962 
1963 		/*
1964 		 * If the server is handing out delegations, but we didn't
1965 		 * get one because an OpenConfirm was required, try the
1966 		 * Open again, to get a delegation. This is a harmless no-op,
1967 		 * from a server's point of view.
1968 		 */
1969 		if ((rflags & NFSV4OPEN_RESULTCONFIRM) &&
1970 		    (owp->nfsow_clp->nfsc_flags & NFSCLFLAGS_GOTDELEG) &&
1971 		    !error && dp == NULL) {
1972 		    np = VTONFS(dvp);
1973 		    do {
1974 			ret = nfsrpc_openrpc(VFSTONFS(vnode_mount(dvp)), dvp,
1975 			    np->n_fhp->nfh_fh, np->n_fhp->nfh_len,
1976 			    nfhp->nfh_fh, nfhp->nfh_len,
1977 			    (NFSV4OPEN_ACCESSWRITE | NFSV4OPEN_ACCESSREAD), op,
1978 			    name, namelen, &dp, 0, 0x0, cred, p, 0, 1);
1979 			if (ret == NFSERR_DELAY)
1980 			    (void) nfs_catnap(PZERO, "nfs_crt2");
1981 		    } while (ret == NFSERR_DELAY);
1982 		    if (ret) {
1983 			if (dp != NULL)
1984 				FREE((caddr_t)dp, M_NFSCLDELEG);
1985 			if (ret == NFSERR_STALECLIENTID ||
1986 			    ret == NFSERR_STALEDONTRECOVER)
1987 				error = ret;
1988 		    }
1989 		}
1990 		nfscl_openrelease(op, error, newone);
1991 		*unlockedp = 1;
1992 	}
1993 	if (nd->nd_repstat != 0 && error == 0)
1994 		error = nd->nd_repstat;
1995 	if (error == NFSERR_STALECLIENTID)
1996 		nfscl_initiate_recovery(owp->nfsow_clp);
1997 nfsmout:
1998 	if (!error)
1999 		*dpp = dp;
2000 	else if (dp != NULL)
2001 		FREE((caddr_t)dp, M_NFSCLDELEG);
2002 	mbuf_freem(nd->nd_mrep);
2003 	return (error);
2004 }
2005 
2006 /*
2007  * Nfs remove rpc
2008  */
2009 APPLESTATIC int
2010 nfsrpc_remove(vnode_t dvp, char *name, int namelen, vnode_t vp,
2011     struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap, int *dattrflagp,
2012     void *dstuff)
2013 {
2014 	u_int32_t *tl;
2015 	struct nfsrv_descript nfsd, *nd = &nfsd;
2016 	struct nfsnode *np;
2017 	struct nfsmount *nmp;
2018 	nfsv4stateid_t dstateid;
2019 	int error, ret = 0, i;
2020 
2021 	*dattrflagp = 0;
2022 	if (namelen > NFS_MAXNAMLEN)
2023 		return (ENAMETOOLONG);
2024 	nmp = VFSTONFS(vnode_mount(dvp));
2025 tryagain:
2026 	if (NFSHASNFSV4(nmp) && ret == 0) {
2027 		ret = nfscl_removedeleg(vp, p, &dstateid);
2028 		if (ret == 1) {
2029 			NFSCL_REQSTART(nd, NFSPROC_RETDELEGREMOVE, vp);
2030 			NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID +
2031 			    NFSX_UNSIGNED);
2032 			*tl++ = dstateid.seqid;
2033 			*tl++ = dstateid.other[0];
2034 			*tl++ = dstateid.other[1];
2035 			*tl++ = dstateid.other[2];
2036 			*tl = txdr_unsigned(NFSV4OP_PUTFH);
2037 			np = VTONFS(dvp);
2038 			(void) nfsm_fhtom(nd, np->n_fhp->nfh_fh,
2039 			    np->n_fhp->nfh_len, 0);
2040 			NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2041 			*tl = txdr_unsigned(NFSV4OP_REMOVE);
2042 		}
2043 	} else {
2044 		ret = 0;
2045 	}
2046 	if (ret == 0)
2047 		NFSCL_REQSTART(nd, NFSPROC_REMOVE, dvp);
2048 	(void) nfsm_strtom(nd, name, namelen);
2049 	error = nfscl_request(nd, dvp, p, cred, dstuff);
2050 	if (error)
2051 		return (error);
2052 	if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) {
2053 		/* For NFSv4, parse out any Delereturn replies. */
2054 		if (ret > 0 && nd->nd_repstat != 0 &&
2055 		    (nd->nd_flag & ND_NOMOREDATA)) {
2056 			/*
2057 			 * If the Delegreturn failed, try again without
2058 			 * it. The server will Recall, as required.
2059 			 */
2060 			mbuf_freem(nd->nd_mrep);
2061 			goto tryagain;
2062 		}
2063 		for (i = 0; i < (ret * 2); i++) {
2064 			if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) ==
2065 			    ND_NFSV4) {
2066 			    NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2067 			    if (*(tl + 1))
2068 				nd->nd_flag |= ND_NOMOREDATA;
2069 			}
2070 		}
2071 		error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
2072 	}
2073 	if (nd->nd_repstat && !error)
2074 		error = nd->nd_repstat;
2075 nfsmout:
2076 	mbuf_freem(nd->nd_mrep);
2077 	return (error);
2078 }
2079 
2080 /*
2081  * Do an nfs rename rpc.
2082  */
2083 APPLESTATIC int
2084 nfsrpc_rename(vnode_t fdvp, vnode_t fvp, char *fnameptr, int fnamelen,
2085     vnode_t tdvp, vnode_t tvp, char *tnameptr, int tnamelen, struct ucred *cred,
2086     NFSPROC_T *p, struct nfsvattr *fnap, struct nfsvattr *tnap,
2087     int *fattrflagp, int *tattrflagp, void *fstuff, void *tstuff)
2088 {
2089 	u_int32_t *tl;
2090 	struct nfsrv_descript nfsd, *nd = &nfsd;
2091 	struct nfsmount *nmp;
2092 	struct nfsnode *np;
2093 	nfsattrbit_t attrbits;
2094 	nfsv4stateid_t fdstateid, tdstateid;
2095 	int error = 0, ret = 0, gottd = 0, gotfd = 0, i;
2096 
2097 	*fattrflagp = 0;
2098 	*tattrflagp = 0;
2099 	nmp = VFSTONFS(vnode_mount(fdvp));
2100 	if (fnamelen > NFS_MAXNAMLEN || tnamelen > NFS_MAXNAMLEN)
2101 		return (ENAMETOOLONG);
2102 tryagain:
2103 	if (NFSHASNFSV4(nmp) && ret == 0) {
2104 		ret = nfscl_renamedeleg(fvp, &fdstateid, &gotfd, tvp,
2105 		    &tdstateid, &gottd, p);
2106 		if (gotfd && gottd) {
2107 			NFSCL_REQSTART(nd, NFSPROC_RETDELEGRENAME2, fvp);
2108 		} else if (gotfd) {
2109 			NFSCL_REQSTART(nd, NFSPROC_RETDELEGRENAME1, fvp);
2110 		} else if (gottd) {
2111 			NFSCL_REQSTART(nd, NFSPROC_RETDELEGRENAME1, tvp);
2112 		}
2113 		if (gotfd) {
2114 			NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID);
2115 			*tl++ = fdstateid.seqid;
2116 			*tl++ = fdstateid.other[0];
2117 			*tl++ = fdstateid.other[1];
2118 			*tl = fdstateid.other[2];
2119 			if (gottd) {
2120 				NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2121 				*tl = txdr_unsigned(NFSV4OP_PUTFH);
2122 				np = VTONFS(tvp);
2123 				(void) nfsm_fhtom(nd, np->n_fhp->nfh_fh,
2124 				    np->n_fhp->nfh_len, 0);
2125 				NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2126 				*tl = txdr_unsigned(NFSV4OP_DELEGRETURN);
2127 			}
2128 		}
2129 		if (gottd) {
2130 			NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID);
2131 			*tl++ = tdstateid.seqid;
2132 			*tl++ = tdstateid.other[0];
2133 			*tl++ = tdstateid.other[1];
2134 			*tl = tdstateid.other[2];
2135 		}
2136 		if (ret > 0) {
2137 			NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2138 			*tl = txdr_unsigned(NFSV4OP_PUTFH);
2139 			np = VTONFS(fdvp);
2140 			(void) nfsm_fhtom(nd, np->n_fhp->nfh_fh,
2141 			    np->n_fhp->nfh_len, 0);
2142 			NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2143 			*tl = txdr_unsigned(NFSV4OP_SAVEFH);
2144 		}
2145 	} else {
2146 		ret = 0;
2147 	}
2148 	if (ret == 0)
2149 		NFSCL_REQSTART(nd, NFSPROC_RENAME, fdvp);
2150 	if (nd->nd_flag & ND_NFSV4) {
2151 		NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2152 		*tl = txdr_unsigned(NFSV4OP_GETATTR);
2153 		NFSWCCATTR_ATTRBIT(&attrbits);
2154 		(void) nfsrv_putattrbit(nd, &attrbits);
2155 		NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2156 		*tl = txdr_unsigned(NFSV4OP_PUTFH);
2157 		(void) nfsm_fhtom(nd, VTONFS(tdvp)->n_fhp->nfh_fh,
2158 		    VTONFS(tdvp)->n_fhp->nfh_len, 0);
2159 		NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2160 		*tl = txdr_unsigned(NFSV4OP_GETATTR);
2161 		(void) nfsrv_putattrbit(nd, &attrbits);
2162 		nd->nd_flag |= ND_V4WCCATTR;
2163 		NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2164 		*tl = txdr_unsigned(NFSV4OP_RENAME);
2165 	}
2166 	(void) nfsm_strtom(nd, fnameptr, fnamelen);
2167 	if (!(nd->nd_flag & ND_NFSV4))
2168 		(void) nfsm_fhtom(nd, VTONFS(tdvp)->n_fhp->nfh_fh,
2169 			VTONFS(tdvp)->n_fhp->nfh_len, 0);
2170 	(void) nfsm_strtom(nd, tnameptr, tnamelen);
2171 	error = nfscl_request(nd, fdvp, p, cred, fstuff);
2172 	if (error)
2173 		return (error);
2174 	if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) {
2175 		/* For NFSv4, parse out any Delereturn replies. */
2176 		if (ret > 0 && nd->nd_repstat != 0 &&
2177 		    (nd->nd_flag & ND_NOMOREDATA)) {
2178 			/*
2179 			 * If the Delegreturn failed, try again without
2180 			 * it. The server will Recall, as required.
2181 			 */
2182 			mbuf_freem(nd->nd_mrep);
2183 			goto tryagain;
2184 		}
2185 		for (i = 0; i < (ret * 2); i++) {
2186 			if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) ==
2187 			    ND_NFSV4) {
2188 			    NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2189 			    if (*(tl + 1)) {
2190 				if (i == 0 && ret > 1) {
2191 				    /*
2192 				     * If the Delegreturn failed, try again
2193 				     * without it. The server will Recall, as
2194 				     * required.
2195 				     * If ret > 1, the first iteration of this
2196 				     * loop is the second DelegReturn result.
2197 				     */
2198 				    mbuf_freem(nd->nd_mrep);
2199 				    goto tryagain;
2200 				} else {
2201 				    nd->nd_flag |= ND_NOMOREDATA;
2202 				}
2203 			    }
2204 			}
2205 		}
2206 		/* Now, the first wcc attribute reply. */
2207 		if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) == ND_NFSV4) {
2208 			NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2209 			if (*(tl + 1))
2210 				nd->nd_flag |= ND_NOMOREDATA;
2211 		}
2212 		error = nfscl_wcc_data(nd, fdvp, fnap, fattrflagp, NULL,
2213 		    fstuff);
2214 		/* and the second wcc attribute reply. */
2215 		if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) == ND_NFSV4 &&
2216 		    !error) {
2217 			NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2218 			if (*(tl + 1))
2219 				nd->nd_flag |= ND_NOMOREDATA;
2220 		}
2221 		if (!error)
2222 			error = nfscl_wcc_data(nd, tdvp, tnap, tattrflagp,
2223 			    NULL, tstuff);
2224 	}
2225 	if (nd->nd_repstat && !error)
2226 		error = nd->nd_repstat;
2227 nfsmout:
2228 	mbuf_freem(nd->nd_mrep);
2229 	return (error);
2230 }
2231 
2232 /*
2233  * nfs hard link create rpc
2234  */
2235 APPLESTATIC int
2236 nfsrpc_link(vnode_t dvp, vnode_t vp, char *name, int namelen,
2237     struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap,
2238     struct nfsvattr *nap, int *attrflagp, int *dattrflagp, void *dstuff)
2239 {
2240 	u_int32_t *tl;
2241 	struct nfsrv_descript nfsd, *nd = &nfsd;
2242 	nfsattrbit_t attrbits;
2243 	int error = 0;
2244 
2245 	*attrflagp = 0;
2246 	*dattrflagp = 0;
2247 	if (namelen > NFS_MAXNAMLEN)
2248 		return (ENAMETOOLONG);
2249 	NFSCL_REQSTART(nd, NFSPROC_LINK, vp);
2250 	if (nd->nd_flag & ND_NFSV4) {
2251 		NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2252 		*tl = txdr_unsigned(NFSV4OP_PUTFH);
2253 	}
2254 	(void) nfsm_fhtom(nd, VTONFS(dvp)->n_fhp->nfh_fh,
2255 		VTONFS(dvp)->n_fhp->nfh_len, 0);
2256 	if (nd->nd_flag & ND_NFSV4) {
2257 		NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2258 		*tl = txdr_unsigned(NFSV4OP_GETATTR);
2259 		NFSWCCATTR_ATTRBIT(&attrbits);
2260 		(void) nfsrv_putattrbit(nd, &attrbits);
2261 		nd->nd_flag |= ND_V4WCCATTR;
2262 		NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2263 		*tl = txdr_unsigned(NFSV4OP_LINK);
2264 	}
2265 	(void) nfsm_strtom(nd, name, namelen);
2266 	error = nfscl_request(nd, vp, p, cred, dstuff);
2267 	if (error)
2268 		return (error);
2269 	if (nd->nd_flag & ND_NFSV3) {
2270 		error = nfscl_postop_attr(nd, nap, attrflagp, dstuff);
2271 		if (!error)
2272 			error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp,
2273 			    NULL, dstuff);
2274 	} else if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) == ND_NFSV4) {
2275 		/*
2276 		 * First, parse out the PutFH and Getattr result.
2277 		 */
2278 		NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2279 		if (!(*(tl + 1)))
2280 			NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2281 		if (*(tl + 1))
2282 			nd->nd_flag |= ND_NOMOREDATA;
2283 		/*
2284 		 * Get the pre-op attributes.
2285 		 */
2286 		error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
2287 	}
2288 	if (nd->nd_repstat && !error)
2289 		error = nd->nd_repstat;
2290 nfsmout:
2291 	mbuf_freem(nd->nd_mrep);
2292 	return (error);
2293 }
2294 
2295 /*
2296  * nfs symbolic link create rpc
2297  */
2298 APPLESTATIC int
2299 nfsrpc_symlink(vnode_t dvp, char *name, int namelen, char *target,
2300     struct vattr *vap, struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap,
2301     struct nfsvattr *nnap, struct nfsfh **nfhpp, int *attrflagp,
2302     int *dattrflagp, void *dstuff)
2303 {
2304 	u_int32_t *tl;
2305 	struct nfsrv_descript nfsd, *nd = &nfsd;
2306 	struct nfsmount *nmp;
2307 	int slen, error = 0;
2308 
2309 	*nfhpp = NULL;
2310 	*attrflagp = 0;
2311 	*dattrflagp = 0;
2312 	nmp = VFSTONFS(vnode_mount(dvp));
2313 	slen = strlen(target);
2314 	if (slen > NFS_MAXPATHLEN || namelen > NFS_MAXNAMLEN)
2315 		return (ENAMETOOLONG);
2316 	NFSCL_REQSTART(nd, NFSPROC_SYMLINK, dvp);
2317 	if (nd->nd_flag & ND_NFSV4) {
2318 		NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2319 		*tl = txdr_unsigned(NFLNK);
2320 		(void) nfsm_strtom(nd, target, slen);
2321 	}
2322 	(void) nfsm_strtom(nd, name, namelen);
2323 	if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4))
2324 		nfscl_fillsattr(nd, vap, dvp, 0, 0);
2325 	if (!(nd->nd_flag & ND_NFSV4))
2326 		(void) nfsm_strtom(nd, target, slen);
2327 	if (nd->nd_flag & ND_NFSV2)
2328 		nfscl_fillsattr(nd, vap, dvp, NFSSATTR_SIZENEG1, 0);
2329 	error = nfscl_request(nd, dvp, p, cred, dstuff);
2330 	if (error)
2331 		return (error);
2332 	if (nd->nd_flag & ND_NFSV4)
2333 		error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
2334 	if ((nd->nd_flag & ND_NFSV3) && !error) {
2335 		if (!nd->nd_repstat)
2336 			error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp);
2337 		if (!error)
2338 			error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp,
2339 			    NULL, dstuff);
2340 	}
2341 	if (nd->nd_repstat && !error)
2342 		error = nd->nd_repstat;
2343 	mbuf_freem(nd->nd_mrep);
2344 	/*
2345 	 * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry.
2346 	 */
2347 	if (error == EEXIST)
2348 		error = 0;
2349 	return (error);
2350 }
2351 
2352 /*
2353  * nfs make dir rpc
2354  */
2355 APPLESTATIC int
2356 nfsrpc_mkdir(vnode_t dvp, char *name, int namelen, struct vattr *vap,
2357     struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap,
2358     struct nfsvattr *nnap, struct nfsfh **nfhpp, int *attrflagp,
2359     int *dattrflagp, void *dstuff)
2360 {
2361 	u_int32_t *tl;
2362 	struct nfsrv_descript nfsd, *nd = &nfsd;
2363 	nfsattrbit_t attrbits;
2364 	int error = 0;
2365 
2366 	*nfhpp = NULL;
2367 	*attrflagp = 0;
2368 	*dattrflagp = 0;
2369 	if (namelen > NFS_MAXNAMLEN)
2370 		return (ENAMETOOLONG);
2371 	NFSCL_REQSTART(nd, NFSPROC_MKDIR, dvp);
2372 	if (nd->nd_flag & ND_NFSV4) {
2373 		NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2374 		*tl = txdr_unsigned(NFDIR);
2375 	}
2376 	(void) nfsm_strtom(nd, name, namelen);
2377 	nfscl_fillsattr(nd, vap, dvp, NFSSATTR_SIZENEG1, 0);
2378 	if (nd->nd_flag & ND_NFSV4) {
2379 		NFSGETATTR_ATTRBIT(&attrbits);
2380 		NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2381 		*tl++ = txdr_unsigned(NFSV4OP_GETFH);
2382 		*tl = txdr_unsigned(NFSV4OP_GETATTR);
2383 		(void) nfsrv_putattrbit(nd, &attrbits);
2384 	}
2385 	error = nfscl_request(nd, dvp, p, cred, dstuff);
2386 	if (error)
2387 		return (error);
2388 	if (nd->nd_flag & ND_NFSV4)
2389 		error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
2390 	if (!nd->nd_repstat && !error) {
2391 		if (nd->nd_flag & ND_NFSV4) {
2392 			NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
2393 			error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
2394 		}
2395 		if (!error)
2396 			error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp);
2397 	}
2398 	if ((nd->nd_flag & ND_NFSV3) && !error)
2399 		error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
2400 	if (nd->nd_repstat && !error)
2401 		error = nd->nd_repstat;
2402 nfsmout:
2403 	mbuf_freem(nd->nd_mrep);
2404 	/*
2405 	 * Kludge: Map EEXIST => 0 assuming that you have a reply to a retry.
2406 	 */
2407 	if (error == EEXIST)
2408 		error = 0;
2409 	return (error);
2410 }
2411 
2412 /*
2413  * nfs remove directory call
2414  */
2415 APPLESTATIC int
2416 nfsrpc_rmdir(vnode_t dvp, char *name, int namelen, struct ucred *cred,
2417     NFSPROC_T *p, struct nfsvattr *dnap, int *dattrflagp, void *dstuff)
2418 {
2419 	struct nfsrv_descript nfsd, *nd = &nfsd;
2420 	int error = 0;
2421 
2422 	*dattrflagp = 0;
2423 	if (namelen > NFS_MAXNAMLEN)
2424 		return (ENAMETOOLONG);
2425 	NFSCL_REQSTART(nd, NFSPROC_RMDIR, dvp);
2426 	(void) nfsm_strtom(nd, name, namelen);
2427 	error = nfscl_request(nd, dvp, p, cred, dstuff);
2428 	if (error)
2429 		return (error);
2430 	if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4))
2431 		error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
2432 	if (nd->nd_repstat && !error)
2433 		error = nd->nd_repstat;
2434 	mbuf_freem(nd->nd_mrep);
2435 	/*
2436 	 * Kludge: Map ENOENT => 0 assuming that you have a reply to a retry.
2437 	 */
2438 	if (error == ENOENT)
2439 		error = 0;
2440 	return (error);
2441 }
2442 
2443 /*
2444  * Readdir rpc.
2445  * Always returns with either uio_resid unchanged, if you are at the
2446  * end of the directory, or uio_resid == 0, with all DIRBLKSIZ chunks
2447  * filled in.
2448  * I felt this would allow caching of directory blocks more easily
2449  * than returning a pertially filled block.
2450  * Directory offset cookies:
2451  * Oh my, what to do with them...
2452  * I can think of three ways to deal with them:
2453  * 1 - have the layer above these RPCs maintain a map between logical
2454  *     directory byte offsets and the NFS directory offset cookies
2455  * 2 - pass the opaque directory offset cookies up into userland
2456  *     and let the libc functions deal with them, via the system call
2457  * 3 - return them to userland in the "struct dirent", so future versions
2458  *     of libc can use them and do whatever is necessary to amke things work
2459  *     above these rpc calls, in the meantime
2460  * For now, I do #3 by "hiding" the directory offset cookies after the
2461  * d_name field in struct dirent. This is space inside d_reclen that
2462  * will be ignored by anything that doesn't know about them.
2463  * The directory offset cookies are filled in as the last 8 bytes of
2464  * each directory entry, after d_name. Someday, the userland libc
2465  * functions may be able to use these. In the meantime, it satisfies
2466  * OpenBSD's requirements for cookies being returned.
2467  * If expects the directory offset cookie for the read to be in uio_offset
2468  * and returns the one for the next entry after this directory block in
2469  * there, as well.
2470  */
2471 APPLESTATIC int
2472 nfsrpc_readdir(vnode_t vp, struct uio *uiop, nfsuint64 *cookiep,
2473     struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp,
2474     int *eofp, void *stuff)
2475 {
2476 	int len, left;
2477 	struct dirent *dp = NULL;
2478 	u_int32_t *tl;
2479 	nfsquad_t cookie, ncookie;
2480 	struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
2481 	struct nfsnode *dnp = VTONFS(vp);
2482 	struct nfsvattr nfsva;
2483 	struct nfsrv_descript nfsd, *nd = &nfsd;
2484 	int error = 0, tlen, more_dirs = 1, blksiz = 0, bigenough = 1;
2485 	int reqsize, tryformoredirs = 1, readsize, eof = 0, gotmnton = 0;
2486 	long dotfileid, dotdotfileid = 0;
2487 	u_int32_t fakefileno = 0xffffffff, rderr;
2488 	char *cp;
2489 	nfsattrbit_t attrbits, dattrbits;
2490 	u_int32_t *tl2 = NULL;
2491 	size_t tresid;
2492 
2493 #ifdef DIAGNOSTIC
2494 	if (uiop->uio_iovcnt != 1 || (uio_uio_resid(uiop) & (DIRBLKSIZ - 1)))
2495 		panic("nfs readdirrpc bad uio");
2496 #endif
2497 
2498 	/*
2499 	 * There is no point in reading a lot more than uio_resid, however
2500 	 * adding one additional DIRBLKSIZ makes sense. Since uio_resid
2501 	 * and nm_readdirsize are both exact multiples of DIRBLKSIZ, this
2502 	 * will never make readsize > nm_readdirsize.
2503 	 */
2504 	readsize = nmp->nm_readdirsize;
2505 	if (readsize > uio_uio_resid(uiop))
2506 		readsize = uio_uio_resid(uiop) + DIRBLKSIZ;
2507 
2508 	*attrflagp = 0;
2509 	if (eofp)
2510 		*eofp = 0;
2511 	tresid = uio_uio_resid(uiop);
2512 	cookie.lval[0] = cookiep->nfsuquad[0];
2513 	cookie.lval[1] = cookiep->nfsuquad[1];
2514 	nd->nd_mrep = NULL;
2515 
2516 	/*
2517 	 * For NFSv4, first create the "." and ".." entries.
2518 	 */
2519 	if (NFSHASNFSV4(nmp)) {
2520 		reqsize = 6 * NFSX_UNSIGNED;
2521 		NFSGETATTR_ATTRBIT(&dattrbits);
2522 		NFSZERO_ATTRBIT(&attrbits);
2523 		NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_FILEID);
2524 		NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TYPE);
2525 		if (NFSISSET_ATTRBIT(&dnp->n_vattr.na_suppattr,
2526 		    NFSATTRBIT_MOUNTEDONFILEID)) {
2527 			NFSSETBIT_ATTRBIT(&attrbits,
2528 			    NFSATTRBIT_MOUNTEDONFILEID);
2529 			gotmnton = 1;
2530 		} else {
2531 			/*
2532 			 * Must fake it. Use the fileno, except when the
2533 			 * fsid is != to that of the directory. For that
2534 			 * case, generate a fake fileno that is not the same.
2535 			 */
2536 			NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_FSID);
2537 			gotmnton = 0;
2538 		}
2539 
2540 		/*
2541 		 * Joy, oh joy. For V4 we get to hand craft '.' and '..'.
2542 		 */
2543 		if (uiop->uio_offset == 0) {
2544 #if defined(__FreeBSD_version) && __FreeBSD_version >= 800000
2545 			error = VOP_GETATTR(vp, &nfsva.na_vattr, cred);
2546 #else
2547 			error = VOP_GETATTR(vp, &nfsva.na_vattr, cred, p);
2548 #endif
2549 			if (error)
2550 			    return (error);
2551 			dotfileid = nfsva.na_fileid;
2552 			NFSCL_REQSTART(nd, NFSPROC_LOOKUPP, vp);
2553 			NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2554 			*tl++ = txdr_unsigned(NFSV4OP_GETFH);
2555 			*tl = txdr_unsigned(NFSV4OP_GETATTR);
2556 			(void) nfsrv_putattrbit(nd, &attrbits);
2557 			error = nfscl_request(nd, vp, p, cred, stuff);
2558 			if (error)
2559 			    return (error);
2560 			if (nd->nd_repstat == 0) {
2561 			    NFSM_DISSECT(tl, u_int32_t *, 3*NFSX_UNSIGNED);
2562 			    len = fxdr_unsigned(int, *(tl + 2));
2563 			    if (len > 0 && len <= NFSX_V4FHMAX)
2564 				error = nfsm_advance(nd, NFSM_RNDUP(len), -1);
2565 			    else
2566 				error = EPERM;
2567 			    if (!error) {
2568 				NFSM_DISSECT(tl, u_int32_t *, 2*NFSX_UNSIGNED);
2569 				nfsva.na_mntonfileno = 0xffffffff;
2570 				error = nfsv4_loadattr(nd, NULL, &nfsva, NULL,
2571 				    NULL, 0, NULL, NULL, NULL, NULL, NULL, 0,
2572 				    NULL, NULL, NULL, p, cred);
2573 				if (error) {
2574 				    dotdotfileid = dotfileid;
2575 				} else if (gotmnton) {
2576 				    if (nfsva.na_mntonfileno != 0xffffffff)
2577 					dotdotfileid = nfsva.na_mntonfileno;
2578 				    else
2579 					dotdotfileid = nfsva.na_fileid;
2580 				} else if (nfsva.na_filesid[0] ==
2581 				    dnp->n_vattr.na_filesid[0] &&
2582 				    nfsva.na_filesid[1] ==
2583 				    dnp->n_vattr.na_filesid[1]) {
2584 				    dotdotfileid = nfsva.na_fileid;
2585 				} else {
2586 				    do {
2587 					fakefileno--;
2588 				    } while (fakefileno ==
2589 					nfsva.na_fileid);
2590 				    dotdotfileid = fakefileno;
2591 				}
2592 			    }
2593 			} else if (nd->nd_repstat == NFSERR_NOENT) {
2594 			    /*
2595 			     * Lookupp returns NFSERR_NOENT when we are
2596 			     * at the root, so just use the current dir.
2597 			     */
2598 			    nd->nd_repstat = 0;
2599 			    dotdotfileid = dotfileid;
2600 			} else {
2601 			    error = nd->nd_repstat;
2602 			}
2603 			mbuf_freem(nd->nd_mrep);
2604 			if (error)
2605 			    return (error);
2606 			nd->nd_mrep = NULL;
2607 			dp = (struct dirent *) CAST_DOWN(caddr_t, uio_iov_base(uiop));
2608 			dp->d_type = DT_DIR;
2609 			dp->d_fileno = dotfileid;
2610 			dp->d_namlen = 1;
2611 			dp->d_name[0] = '.';
2612 			dp->d_name[1] = '\0';
2613 			dp->d_reclen = DIRENT_SIZE(dp) + NFSX_HYPER;
2614 			/*
2615 			 * Just make these offset cookie 0.
2616 			 */
2617 			tl = (u_int32_t *)&dp->d_name[4];
2618 			*tl++ = 0;
2619 			*tl = 0;
2620 			blksiz += dp->d_reclen;
2621 			uio_uio_resid_add(uiop, -(dp->d_reclen));
2622 			uiop->uio_offset += dp->d_reclen;
2623 			uio_iov_base_add(uiop, dp->d_reclen);
2624 			uio_iov_len_add(uiop, -(dp->d_reclen));
2625 			dp = (struct dirent *) CAST_DOWN(caddr_t, uio_iov_base(uiop));
2626 			dp->d_type = DT_DIR;
2627 			dp->d_fileno = dotdotfileid;
2628 			dp->d_namlen = 2;
2629 			dp->d_name[0] = '.';
2630 			dp->d_name[1] = '.';
2631 			dp->d_name[2] = '\0';
2632 			dp->d_reclen = DIRENT_SIZE(dp) + NFSX_HYPER;
2633 			/*
2634 			 * Just make these offset cookie 0.
2635 			 */
2636 			tl = (u_int32_t *)&dp->d_name[4];
2637 			*tl++ = 0;
2638 			*tl = 0;
2639 			blksiz += dp->d_reclen;
2640 			uio_uio_resid_add(uiop, -(dp->d_reclen));
2641 			uiop->uio_offset += dp->d_reclen;
2642 			uio_iov_base_add(uiop, dp->d_reclen);
2643 			uio_iov_len_add(uiop, -(dp->d_reclen));
2644 		}
2645 		NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_RDATTRERROR);
2646 	} else {
2647 		reqsize = 5 * NFSX_UNSIGNED;
2648 	}
2649 
2650 
2651 	/*
2652 	 * Loop around doing readdir rpc's of size readsize.
2653 	 * The stopping criteria is EOF or buffer full.
2654 	 */
2655 	while (more_dirs && bigenough) {
2656 		*attrflagp = 0;
2657 		NFSCL_REQSTART(nd, NFSPROC_READDIR, vp);
2658 		if (nd->nd_flag & ND_NFSV2) {
2659 			NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2660 			*tl++ = cookie.lval[1];
2661 			*tl = txdr_unsigned(readsize);
2662 		} else {
2663 			NFSM_BUILD(tl, u_int32_t *, reqsize);
2664 			*tl++ = cookie.lval[0];
2665 			*tl++ = cookie.lval[1];
2666 			if (cookie.qval == 0) {
2667 				*tl++ = 0;
2668 				*tl++ = 0;
2669 			} else {
2670 				NFSLOCKNODE(dnp);
2671 				*tl++ = dnp->n_cookieverf.nfsuquad[0];
2672 				*tl++ = dnp->n_cookieverf.nfsuquad[1];
2673 				NFSUNLOCKNODE(dnp);
2674 			}
2675 			if (nd->nd_flag & ND_NFSV4) {
2676 				*tl++ = txdr_unsigned(readsize);
2677 				*tl = txdr_unsigned(readsize);
2678 				(void) nfsrv_putattrbit(nd, &attrbits);
2679 				NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2680 				*tl = txdr_unsigned(NFSV4OP_GETATTR);
2681 				(void) nfsrv_putattrbit(nd, &dattrbits);
2682 			} else {
2683 				*tl = txdr_unsigned(readsize);
2684 			}
2685 		}
2686 		error = nfscl_request(nd, vp, p, cred, stuff);
2687 		if (error)
2688 			return (error);
2689 		if (!(nd->nd_flag & ND_NFSV2)) {
2690 			if (nd->nd_flag & ND_NFSV3)
2691 				error = nfscl_postop_attr(nd, nap, attrflagp,
2692 				    stuff);
2693 			if (!nd->nd_repstat && !error) {
2694 				NFSM_DISSECT(tl, u_int32_t *, NFSX_HYPER);
2695 				NFSLOCKNODE(dnp);
2696 				dnp->n_cookieverf.nfsuquad[0] = *tl++;
2697 				dnp->n_cookieverf.nfsuquad[1] = *tl;
2698 				NFSUNLOCKNODE(dnp);
2699 			}
2700 		}
2701 		if (nd->nd_repstat || error) {
2702 			if (!error)
2703 				error = nd->nd_repstat;
2704 			goto nfsmout;
2705 		}
2706 		NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2707 		more_dirs = fxdr_unsigned(int, *tl);
2708 		if (!more_dirs)
2709 			tryformoredirs = 0;
2710 
2711 		/* loop thru the dir entries, doctoring them to 4bsd form */
2712 		while (more_dirs && bigenough) {
2713 			if (nd->nd_flag & ND_NFSV4) {
2714 				NFSM_DISSECT(tl, u_int32_t *, 3*NFSX_UNSIGNED);
2715 				ncookie.lval[0] = *tl++;
2716 				ncookie.lval[1] = *tl++;
2717 				len = fxdr_unsigned(int, *tl);
2718 			} else if (nd->nd_flag & ND_NFSV3) {
2719 				NFSM_DISSECT(tl, u_int32_t *, 3*NFSX_UNSIGNED);
2720 				nfsva.na_fileid =
2721 				    fxdr_unsigned(long, *++tl);
2722 				len = fxdr_unsigned(int, *++tl);
2723 			} else {
2724 				NFSM_DISSECT(tl, u_int32_t *, 2*NFSX_UNSIGNED);
2725 				nfsva.na_fileid =
2726 				    fxdr_unsigned(long, *tl++);
2727 				len = fxdr_unsigned(int, *tl);
2728 			}
2729 			if (len <= 0 || len > NFS_MAXNAMLEN) {
2730 				error = EBADRPC;
2731 				goto nfsmout;
2732 			}
2733 			tlen = NFSM_RNDUP(len);
2734 			if (tlen == len)
2735 				tlen += 4;  /* To ensure null termination */
2736 			left = DIRBLKSIZ - blksiz;
2737 			if ((int)(tlen + DIRHDSIZ + NFSX_HYPER) > left) {
2738 				dp->d_reclen += left;
2739 				uio_iov_base_add(uiop, left);
2740 				uio_iov_len_add(uiop, -(left));
2741 				uio_uio_resid_add(uiop, -(left));
2742 				uiop->uio_offset += left;
2743 				blksiz = 0;
2744 			}
2745 			if ((int)(tlen + DIRHDSIZ + NFSX_HYPER) > uio_uio_resid(uiop))
2746 				bigenough = 0;
2747 			if (bigenough) {
2748 				dp = (struct dirent *) CAST_DOWN(caddr_t, uio_iov_base(uiop));
2749 				dp->d_namlen = len;
2750 				dp->d_reclen = tlen + DIRHDSIZ + NFSX_HYPER;
2751 				dp->d_type = DT_UNKNOWN;
2752 				blksiz += dp->d_reclen;
2753 				if (blksiz == DIRBLKSIZ)
2754 					blksiz = 0;
2755 				uio_uio_resid_add(uiop, -(DIRHDSIZ));
2756 				uiop->uio_offset += DIRHDSIZ;
2757 				uio_iov_base_add(uiop, DIRHDSIZ);
2758 				uio_iov_len_add(uiop, -(DIRHDSIZ));
2759 				error = nfsm_mbufuio(nd, uiop, len);
2760 				if (error)
2761 					goto nfsmout;
2762 				cp = CAST_DOWN(caddr_t, uio_iov_base(uiop));
2763 				tlen -= len;
2764 				*cp = '\0';	/* null terminate */
2765 				cp += tlen;	/* points to cookie storage */
2766 				tl2 = (u_int32_t *)cp;
2767 				uio_iov_base_add(uiop, (tlen + NFSX_HYPER));
2768 				uio_iov_len_add(uiop, -(tlen + NFSX_HYPER));
2769 				uio_uio_resid_add(uiop, -(tlen + NFSX_HYPER));
2770 				uiop->uio_offset += (tlen + NFSX_HYPER);
2771 			} else {
2772 				error = nfsm_advance(nd, NFSM_RNDUP(len), -1);
2773 				if (error)
2774 					goto nfsmout;
2775 			}
2776 			if (nd->nd_flag & ND_NFSV4) {
2777 				rderr = 0;
2778 				nfsva.na_mntonfileno = 0xffffffff;
2779 				error = nfsv4_loadattr(nd, NULL, &nfsva, NULL,
2780 				    NULL, 0, NULL, NULL, NULL, NULL, NULL, 0,
2781 				    NULL, NULL, &rderr, p, cred);
2782 				if (error)
2783 					goto nfsmout;
2784 				NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2785 			} else if (nd->nd_flag & ND_NFSV3) {
2786 				NFSM_DISSECT(tl, u_int32_t *, 3*NFSX_UNSIGNED);
2787 				ncookie.lval[0] = *tl++;
2788 				ncookie.lval[1] = *tl++;
2789 			} else {
2790 				NFSM_DISSECT(tl, u_int32_t *, 2*NFSX_UNSIGNED);
2791 				ncookie.lval[0] = 0;
2792 				ncookie.lval[1] = *tl++;
2793 			}
2794 			if (bigenough) {
2795 			    if (nd->nd_flag & ND_NFSV4) {
2796 				if (rderr) {
2797 				    dp->d_fileno = 0;
2798 				} else {
2799 				    if (gotmnton) {
2800 					if (nfsva.na_mntonfileno != 0xffffffff)
2801 					    dp->d_fileno = nfsva.na_mntonfileno;
2802 					else
2803 					    dp->d_fileno = nfsva.na_fileid;
2804 				    } else if (nfsva.na_filesid[0] ==
2805 					dnp->n_vattr.na_filesid[0] &&
2806 					nfsva.na_filesid[1] ==
2807 					dnp->n_vattr.na_filesid[1]) {
2808 					dp->d_fileno = nfsva.na_fileid;
2809 				    } else {
2810 					do {
2811 					    fakefileno--;
2812 					} while (fakefileno ==
2813 					    nfsva.na_fileid);
2814 					dp->d_fileno = fakefileno;
2815 				    }
2816 				    dp->d_type = vtonfs_dtype(nfsva.na_type);
2817 				}
2818 			    } else {
2819 				dp->d_fileno = nfsva.na_fileid;
2820 			    }
2821 			    *tl2++ = cookiep->nfsuquad[0] = cookie.lval[0] =
2822 				ncookie.lval[0];
2823 			    *tl2 = cookiep->nfsuquad[1] = cookie.lval[1] =
2824 				ncookie.lval[1];
2825 			}
2826 			more_dirs = fxdr_unsigned(int, *tl);
2827 		}
2828 		/*
2829 		 * If at end of rpc data, get the eof boolean
2830 		 */
2831 		if (!more_dirs) {
2832 			NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2833 			eof = fxdr_unsigned(int, *tl);
2834 			if (tryformoredirs)
2835 				more_dirs = !eof;
2836 			if (nd->nd_flag & ND_NFSV4) {
2837 				error = nfscl_postop_attr(nd, nap, attrflagp,
2838 				    stuff);
2839 				if (error)
2840 					goto nfsmout;
2841 			}
2842 		}
2843 		mbuf_freem(nd->nd_mrep);
2844 		nd->nd_mrep = NULL;
2845 	}
2846 	/*
2847 	 * Fill last record, iff any, out to a multiple of DIRBLKSIZ
2848 	 * by increasing d_reclen for the last record.
2849 	 */
2850 	if (blksiz > 0) {
2851 		left = DIRBLKSIZ - blksiz;
2852 		dp->d_reclen += left;
2853 		uio_iov_base_add(uiop, left);
2854 		uio_iov_len_add(uiop, -(left));
2855 		uio_uio_resid_add(uiop, -(left));
2856 		uiop->uio_offset += left;
2857 	}
2858 
2859 	/*
2860 	 * If returning no data, assume end of file.
2861 	 * If not bigenough, return not end of file, since you aren't
2862 	 *    returning all the data
2863 	 * Otherwise, return the eof flag from the server.
2864 	 */
2865 	if (eofp) {
2866 		if (tresid == ((size_t)(uio_uio_resid(uiop))))
2867 			*eofp = 1;
2868 		else if (!bigenough)
2869 			*eofp = 0;
2870 		else
2871 			*eofp = eof;
2872 	}
2873 
2874 	/*
2875 	 * Add extra empty records to any remaining DIRBLKSIZ chunks.
2876 	 */
2877 	while (uio_uio_resid(uiop) > 0 && ((size_t)(uio_uio_resid(uiop))) != tresid) {
2878 		dp = (struct dirent *) CAST_DOWN(caddr_t, uio_iov_base(uiop));
2879 		dp->d_type = DT_UNKNOWN;
2880 		dp->d_fileno = 0;
2881 		dp->d_namlen = 0;
2882 		dp->d_name[0] = '\0';
2883 		tl = (u_int32_t *)&dp->d_name[4];
2884 		*tl++ = cookie.lval[0];
2885 		*tl = cookie.lval[1];
2886 		dp->d_reclen = DIRBLKSIZ;
2887 		uio_iov_base_add(uiop, DIRBLKSIZ);
2888 		uio_iov_len_add(uiop, -(DIRBLKSIZ));
2889 		uio_uio_resid_add(uiop, -(DIRBLKSIZ));
2890 		uiop->uio_offset += DIRBLKSIZ;
2891 	}
2892 
2893 nfsmout:
2894 	if (nd->nd_mrep != NULL)
2895 		mbuf_freem(nd->nd_mrep);
2896 	return (error);
2897 }
2898 
2899 #ifndef APPLE
2900 /*
2901  * NFS V3 readdir plus RPC. Used in place of nfsrpc_readdir().
2902  * (Also used for NFS V4 when mount flag set.)
2903  * (ditto above w.r.t. multiple of DIRBLKSIZ, etc.)
2904  */
2905 APPLESTATIC int
2906 nfsrpc_readdirplus(vnode_t vp, struct uio *uiop, nfsuint64 *cookiep,
2907     struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp,
2908     int *eofp, void *stuff)
2909 {
2910 	int len, left;
2911 	struct dirent *dp = NULL;
2912 	u_int32_t *tl;
2913 	vnode_t newvp = NULLVP;
2914 	struct nfsrv_descript nfsd, *nd = &nfsd;
2915 	struct nameidata nami, *ndp = &nami;
2916 	struct componentname *cnp = &ndp->ni_cnd;
2917 	struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
2918 	struct nfsnode *dnp = VTONFS(vp), *np;
2919 	struct nfsvattr nfsva;
2920 	struct nfsfh *nfhp;
2921 	nfsquad_t cookie, ncookie;
2922 	int error = 0, tlen, more_dirs = 1, blksiz = 0, bigenough = 1;
2923 	int attrflag, tryformoredirs = 1, eof = 0, gotmnton = 0;
2924 	int unlocknewvp = 0;
2925 	long dotfileid, dotdotfileid = 0, fileno = 0;
2926 	char *cp;
2927 	nfsattrbit_t attrbits, dattrbits;
2928 	size_t tresid;
2929 	u_int32_t *tl2 = NULL, fakefileno = 0xffffffff, rderr;
2930 
2931 #ifdef DIAGNOSTIC
2932 	if (uiop->uio_iovcnt != 1 || (uio_uio_resid(uiop) & (DIRBLKSIZ - 1)))
2933 		panic("nfs readdirplusrpc bad uio");
2934 #endif
2935 	*attrflagp = 0;
2936 	if (eofp != NULL)
2937 		*eofp = 0;
2938 	ndp->ni_dvp = vp;
2939 	nd->nd_mrep = NULL;
2940 	cookie.lval[0] = cookiep->nfsuquad[0];
2941 	cookie.lval[1] = cookiep->nfsuquad[1];
2942 	tresid = uio_uio_resid(uiop);
2943 
2944 	/*
2945 	 * For NFSv4, first create the "." and ".." entries.
2946 	 */
2947 	if (NFSHASNFSV4(nmp)) {
2948 		NFSGETATTR_ATTRBIT(&dattrbits);
2949 		NFSZERO_ATTRBIT(&attrbits);
2950 		NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_FILEID);
2951 		if (NFSISSET_ATTRBIT(&dnp->n_vattr.na_suppattr,
2952 		    NFSATTRBIT_MOUNTEDONFILEID)) {
2953 			NFSSETBIT_ATTRBIT(&attrbits,
2954 			    NFSATTRBIT_MOUNTEDONFILEID);
2955 			gotmnton = 1;
2956 		} else {
2957 			/*
2958 			 * Must fake it. Use the fileno, except when the
2959 			 * fsid is != to that of the directory. For that
2960 			 * case, generate a fake fileno that is not the same.
2961 			 */
2962 			NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_FSID);
2963 			gotmnton = 0;
2964 		}
2965 
2966 		/*
2967 		 * Joy, oh joy. For V4 we get to hand craft '.' and '..'.
2968 		 */
2969 		if (uiop->uio_offset == 0) {
2970 #if defined(__FreeBSD_version) && __FreeBSD_version >= 800000
2971 			error = VOP_GETATTR(vp, &nfsva.na_vattr, cred);
2972 #else
2973 			error = VOP_GETATTR(vp, &nfsva.na_vattr, cred, p);
2974 #endif
2975 			if (error)
2976 			    return (error);
2977 			dotfileid = nfsva.na_fileid;
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 			if (nd->nd_repstat == 0) {
2987 			    NFSM_DISSECT(tl, u_int32_t *, 3*NFSX_UNSIGNED);
2988 			    len = fxdr_unsigned(int, *(tl + 2));
2989 			    if (len > 0 && len <= NFSX_V4FHMAX)
2990 				error = nfsm_advance(nd, NFSM_RNDUP(len), -1);
2991 			    else
2992 				error = EPERM;
2993 			    if (!error) {
2994 				NFSM_DISSECT(tl, u_int32_t *, 2*NFSX_UNSIGNED);
2995 				nfsva.na_mntonfileno = 0xffffffff;
2996 				error = nfsv4_loadattr(nd, NULL, &nfsva, NULL,
2997 				    NULL, 0, NULL, NULL, NULL, NULL, NULL, 0,
2998 				    NULL, NULL, NULL, p, cred);
2999 				if (error) {
3000 				    dotdotfileid = dotfileid;
3001 				} else if (gotmnton) {
3002 				    if (nfsva.na_mntonfileno != 0xffffffff)
3003 					dotdotfileid = nfsva.na_mntonfileno;
3004 				    else
3005 					dotdotfileid = nfsva.na_fileid;
3006 				} else if (nfsva.na_filesid[0] ==
3007 				    dnp->n_vattr.na_filesid[0] &&
3008 				    nfsva.na_filesid[1] ==
3009 				    dnp->n_vattr.na_filesid[1]) {
3010 				    dotdotfileid = nfsva.na_fileid;
3011 				} else {
3012 				    do {
3013 					fakefileno--;
3014 				    } while (fakefileno ==
3015 					nfsva.na_fileid);
3016 				    dotdotfileid = fakefileno;
3017 				}
3018 			    }
3019 			} else if (nd->nd_repstat == NFSERR_NOENT) {
3020 			    /*
3021 			     * Lookupp returns NFSERR_NOENT when we are
3022 			     * at the root, so just use the current dir.
3023 			     */
3024 			    nd->nd_repstat = 0;
3025 			    dotdotfileid = dotfileid;
3026 			} else {
3027 			    error = nd->nd_repstat;
3028 			}
3029 			mbuf_freem(nd->nd_mrep);
3030 			if (error)
3031 			    return (error);
3032 			nd->nd_mrep = NULL;
3033 			dp = (struct dirent *)uio_iov_base(uiop);
3034 			dp->d_type = DT_DIR;
3035 			dp->d_fileno = dotfileid;
3036 			dp->d_namlen = 1;
3037 			dp->d_name[0] = '.';
3038 			dp->d_name[1] = '\0';
3039 			dp->d_reclen = DIRENT_SIZE(dp) + NFSX_HYPER;
3040 			/*
3041 			 * Just make these offset cookie 0.
3042 			 */
3043 			tl = (u_int32_t *)&dp->d_name[4];
3044 			*tl++ = 0;
3045 			*tl = 0;
3046 			blksiz += dp->d_reclen;
3047 			uio_uio_resid_add(uiop, -(dp->d_reclen));
3048 			uiop->uio_offset += dp->d_reclen;
3049 			uio_iov_base_add(uiop, dp->d_reclen);
3050 			uio_iov_len_add(uiop, -(dp->d_reclen));
3051 			dp = (struct dirent *)uio_iov_base(uiop);
3052 			dp->d_type = DT_DIR;
3053 			dp->d_fileno = dotdotfileid;
3054 			dp->d_namlen = 2;
3055 			dp->d_name[0] = '.';
3056 			dp->d_name[1] = '.';
3057 			dp->d_name[2] = '\0';
3058 			dp->d_reclen = DIRENT_SIZE(dp) + NFSX_HYPER;
3059 			/*
3060 			 * Just make these offset cookie 0.
3061 			 */
3062 			tl = (u_int32_t *)&dp->d_name[4];
3063 			*tl++ = 0;
3064 			*tl = 0;
3065 			blksiz += dp->d_reclen;
3066 			uio_uio_resid_add(uiop, -(dp->d_reclen));
3067 			uiop->uio_offset += dp->d_reclen;
3068 			uio_iov_base_add(uiop, dp->d_reclen);
3069 			uio_iov_len_add(uiop, -(dp->d_reclen));
3070 		}
3071 		NFSREADDIRPLUS_ATTRBIT(&attrbits);
3072 		if (gotmnton)
3073 			NFSSETBIT_ATTRBIT(&attrbits,
3074 			    NFSATTRBIT_MOUNTEDONFILEID);
3075 	}
3076 
3077 	/*
3078 	 * Loop around doing readdir rpc's of size nm_readdirsize.
3079 	 * The stopping criteria is EOF or buffer full.
3080 	 */
3081 	while (more_dirs && bigenough) {
3082 		*attrflagp = 0;
3083 		NFSCL_REQSTART(nd, NFSPROC_READDIRPLUS, vp);
3084  		NFSM_BUILD(tl, u_int32_t *, 6 * NFSX_UNSIGNED);
3085 		*tl++ = cookie.lval[0];
3086 		*tl++ = cookie.lval[1];
3087 		if (cookie.qval == 0) {
3088 			*tl++ = 0;
3089 			*tl++ = 0;
3090 		} else {
3091 			NFSLOCKNODE(dnp);
3092 			*tl++ = dnp->n_cookieverf.nfsuquad[0];
3093 			*tl++ = dnp->n_cookieverf.nfsuquad[1];
3094 			NFSUNLOCKNODE(dnp);
3095 		}
3096 		*tl++ = txdr_unsigned(nmp->nm_readdirsize);
3097 		*tl = txdr_unsigned(nmp->nm_readdirsize);
3098 		if (nd->nd_flag & ND_NFSV4) {
3099 			(void) nfsrv_putattrbit(nd, &attrbits);
3100 			NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
3101 			*tl = txdr_unsigned(NFSV4OP_GETATTR);
3102 			(void) nfsrv_putattrbit(nd, &dattrbits);
3103 		}
3104 		error = nfscl_request(nd, vp, p, cred, stuff);
3105 		if (error)
3106 			return (error);
3107 		if (nd->nd_flag & ND_NFSV3)
3108 			error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
3109 		if (nd->nd_repstat || error) {
3110 			if (!error)
3111 				error = nd->nd_repstat;
3112 			goto nfsmout;
3113 		}
3114 		NFSM_DISSECT(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
3115 		NFSLOCKNODE(dnp);
3116 		dnp->n_cookieverf.nfsuquad[0] = *tl++;
3117 		dnp->n_cookieverf.nfsuquad[1] = *tl++;
3118 		NFSUNLOCKNODE(dnp);
3119 		more_dirs = fxdr_unsigned(int, *tl);
3120 		if (!more_dirs)
3121 			tryformoredirs = 0;
3122 
3123 		/* loop thru the dir entries, doctoring them to 4bsd form */
3124 		while (more_dirs && bigenough) {
3125 			NFSM_DISSECT(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
3126 			if (nd->nd_flag & ND_NFSV4) {
3127 				ncookie.lval[0] = *tl++;
3128 				ncookie.lval[1] = *tl++;
3129 			} else {
3130 				fileno = fxdr_unsigned(long, *++tl);
3131 				tl++;
3132 			}
3133 			len = fxdr_unsigned(int, *tl);
3134 			if (len <= 0 || len > NFS_MAXNAMLEN) {
3135 				error = EBADRPC;
3136 				goto nfsmout;
3137 			}
3138 			tlen = NFSM_RNDUP(len);
3139 			if (tlen == len)
3140 				tlen += 4;  /* To ensure null termination */
3141 			left = DIRBLKSIZ - blksiz;
3142 			if ((tlen + DIRHDSIZ + NFSX_HYPER) > left) {
3143 				dp->d_reclen += left;
3144 				uio_iov_base_add(uiop, left);
3145 				uio_iov_len_add(uiop, -(left));
3146 				uio_uio_resid_add(uiop, -(left));
3147 				uiop->uio_offset += left;
3148 				blksiz = 0;
3149 			}
3150 			if ((tlen + DIRHDSIZ + NFSX_HYPER) > uio_uio_resid(uiop))
3151 				bigenough = 0;
3152 			if (bigenough) {
3153 				dp = (struct dirent *)uio_iov_base(uiop);
3154 				dp->d_namlen = len;
3155 				dp->d_reclen = tlen + DIRHDSIZ + NFSX_HYPER;
3156 				dp->d_type = DT_UNKNOWN;
3157 				blksiz += dp->d_reclen;
3158 				if (blksiz == DIRBLKSIZ)
3159 					blksiz = 0;
3160 				uio_uio_resid_add(uiop, -(DIRHDSIZ));
3161 				uiop->uio_offset += DIRHDSIZ;
3162 				uio_iov_base_add(uiop, DIRHDSIZ);
3163 				uio_iov_len_add(uiop, -(DIRHDSIZ));
3164 				cnp->cn_nameptr = uio_iov_base(uiop);
3165 				cnp->cn_namelen = len;
3166 				NFSCNHASHZERO(cnp);
3167 				error = nfsm_mbufuio(nd, uiop, len);
3168 				if (error)
3169 					goto nfsmout;
3170 				cp = uio_iov_base(uiop);
3171 				tlen -= len;
3172 				*cp = '\0';
3173 				cp += tlen;	/* points to cookie storage */
3174 				tl2 = (u_int32_t *)cp;
3175 				uio_iov_base_add(uiop, (tlen + NFSX_HYPER));
3176 				uio_iov_len_add(uiop, -(tlen + NFSX_HYPER));
3177 				uio_uio_resid_add(uiop, -(tlen + NFSX_HYPER));
3178 				uiop->uio_offset += (tlen + NFSX_HYPER);
3179 			} else {
3180 				error = nfsm_advance(nd, NFSM_RNDUP(len), -1);
3181 				if (error)
3182 					goto nfsmout;
3183 			}
3184 			nfhp = NULL;
3185 			if (nd->nd_flag & ND_NFSV3) {
3186 				NFSM_DISSECT(tl, u_int32_t *, 3*NFSX_UNSIGNED);
3187 				ncookie.lval[0] = *tl++;
3188 				ncookie.lval[1] = *tl++;
3189 				attrflag = fxdr_unsigned(int, *tl);
3190 				if (attrflag) {
3191 				  error = nfsm_loadattr(nd, &nfsva);
3192 				  if (error)
3193 					goto nfsmout;
3194 				}
3195 				NFSM_DISSECT(tl,u_int32_t *,NFSX_UNSIGNED);
3196 				if (*tl) {
3197 					error = nfsm_getfh(nd, &nfhp);
3198 					if (error)
3199 					    goto nfsmout;
3200 				}
3201 				if (!attrflag && nfhp != NULL) {
3202 					FREE((caddr_t)nfhp, M_NFSFH);
3203 					nfhp = NULL;
3204 				}
3205 			} else {
3206 				rderr = 0;
3207 				nfsva.na_mntonfileno = 0xffffffff;
3208 				error = nfsv4_loadattr(nd, NULL, &nfsva, &nfhp,
3209 				    NULL, 0, NULL, NULL, NULL, NULL, NULL, 0,
3210 				    NULL, NULL, &rderr, p, cred);
3211 				if (error)
3212 					goto nfsmout;
3213 			}
3214 
3215 			if (bigenough) {
3216 			    if (nd->nd_flag & ND_NFSV4) {
3217 				if (rderr) {
3218 				    dp->d_fileno = 0;
3219 				} else if (gotmnton) {
3220 				    if (nfsva.na_mntonfileno != 0xffffffff)
3221 					dp->d_fileno = nfsva.na_mntonfileno;
3222 				    else
3223 					dp->d_fileno = nfsva.na_fileid;
3224 				} else if (nfsva.na_filesid[0] ==
3225 				    dnp->n_vattr.na_filesid[0] &&
3226 				    nfsva.na_filesid[1] ==
3227 				    dnp->n_vattr.na_filesid[1]) {
3228 				    dp->d_fileno = nfsva.na_fileid;
3229 				} else {
3230 				    do {
3231 					fakefileno--;
3232 				    } while (fakefileno ==
3233 					nfsva.na_fileid);
3234 				    dp->d_fileno = fakefileno;
3235 				}
3236 			    } else {
3237 				dp->d_fileno = fileno;
3238 			    }
3239 			    *tl2++ = cookiep->nfsuquad[0] = cookie.lval[0] =
3240 				ncookie.lval[0];
3241 			    *tl2 = cookiep->nfsuquad[1] = cookie.lval[1] =
3242 				ncookie.lval[1];
3243 
3244 			    if (nfhp != NULL) {
3245 				if (NFSRV_CMPFH(nfhp->nfh_fh, nfhp->nfh_len,
3246 				    dnp->n_fhp->nfh_fh, dnp->n_fhp->nfh_len)) {
3247 				    VREF(vp);
3248 				    newvp = vp;
3249 				    unlocknewvp = 0;
3250 				    FREE((caddr_t)nfhp, M_NFSFH);
3251 				    np = dnp;
3252 				} else {
3253 				    error = nfscl_nget(vnode_mount(vp), vp,
3254 				      nfhp, cnp, p, &np, NULL);
3255 				    if (!error) {
3256 					newvp = NFSTOV(np);
3257 					unlocknewvp = 1;
3258 				    }
3259 				}
3260 				nfhp = NULL;
3261 				if (newvp != NULLVP) {
3262 				    error = nfscl_loadattrcache(&newvp,
3263 					&nfsva, NULL, NULL, 0, 0);
3264 				    if (error) {
3265 					if (unlocknewvp)
3266 					    vput(newvp);
3267 					else
3268 					    vrele(newvp);
3269 					goto nfsmout;
3270 				    }
3271 				    dp->d_type =
3272 					vtonfs_dtype(np->n_vattr.na_type);
3273 				    ndp->ni_vp = newvp;
3274 				    NFSCNHASH(cnp, HASHINIT);
3275 				    if (cnp->cn_namelen <= NCHNAMLEN) {
3276 					np->n_ctime =
3277 					  np->n_vattr.na_ctime.tv_sec;
3278 					cache_enter(ndp->ni_dvp,ndp->ni_vp,cnp);
3279 				    }
3280 				    if (unlocknewvp)
3281 					vput(newvp);
3282 				    else
3283 					vrele(newvp);
3284 				    newvp = NULLVP;
3285 				}
3286 			    }
3287 			} else if (nfhp != NULL) {
3288 			    FREE((caddr_t)nfhp, M_NFSFH);
3289 			}
3290 			NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3291 			more_dirs = fxdr_unsigned(int, *tl);
3292 		}
3293 		/*
3294 		 * If at end of rpc data, get the eof boolean
3295 		 */
3296 		if (!more_dirs) {
3297 			NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3298 			eof = fxdr_unsigned(int, *tl);
3299 			if (tryformoredirs)
3300 				more_dirs = !eof;
3301 			if (nd->nd_flag & ND_NFSV4) {
3302 				error = nfscl_postop_attr(nd, nap, attrflagp,
3303 				    stuff);
3304 				if (error)
3305 					goto nfsmout;
3306 			}
3307 		}
3308 		mbuf_freem(nd->nd_mrep);
3309 		nd->nd_mrep = NULL;
3310 	}
3311 	/*
3312 	 * Fill last record, iff any, out to a multiple of DIRBLKSIZ
3313 	 * by increasing d_reclen for the last record.
3314 	 */
3315 	if (blksiz > 0) {
3316 		left = DIRBLKSIZ - blksiz;
3317 		dp->d_reclen += left;
3318 		uio_iov_base_add(uiop, left);
3319 		uio_iov_len_add(uiop, -(left));
3320 		uio_uio_resid_add(uiop, -(left));
3321 		uiop->uio_offset += left;
3322 	}
3323 
3324 	/*
3325 	 * If returning no data, assume end of file.
3326 	 * If not bigenough, return not end of file, since you aren't
3327 	 *    returning all the data
3328 	 * Otherwise, return the eof flag from the server.
3329 	 */
3330 	if (eofp != NULL) {
3331 		if (tresid == uio_uio_resid(uiop))
3332 			*eofp = 1;
3333 		else if (!bigenough)
3334 			*eofp = 0;
3335 		else
3336 			*eofp = eof;
3337 	}
3338 
3339 	/*
3340 	 * Add extra empty records to any remaining DIRBLKSIZ chunks.
3341 	 */
3342 	while (uio_uio_resid(uiop) > 0 && uio_uio_resid(uiop) != tresid) {
3343 		dp = (struct dirent *)uio_iov_base(uiop);
3344 		dp->d_type = DT_UNKNOWN;
3345 		dp->d_fileno = 0;
3346 		dp->d_namlen = 0;
3347 		dp->d_name[0] = '\0';
3348 		tl = (u_int32_t *)&dp->d_name[4];
3349 		*tl++ = cookie.lval[0];
3350 		*tl = cookie.lval[1];
3351 		dp->d_reclen = DIRBLKSIZ;
3352 		uio_iov_base_add(uiop, DIRBLKSIZ);
3353 		uio_iov_len_add(uiop, -(DIRBLKSIZ));
3354 		uio_uio_resid_add(uiop, -(DIRBLKSIZ));
3355 		uiop->uio_offset += DIRBLKSIZ;
3356 	}
3357 
3358 nfsmout:
3359 	if (nd->nd_mrep != NULL)
3360 		mbuf_freem(nd->nd_mrep);
3361 	return (error);
3362 }
3363 #endif	/* !APPLE */
3364 
3365 /*
3366  * Nfs commit rpc
3367  */
3368 APPLESTATIC int
3369 nfsrpc_commit(vnode_t vp, u_quad_t offset, int cnt, struct ucred *cred,
3370     NFSPROC_T *p, u_char *verfp, struct nfsvattr *nap, int *attrflagp,
3371     void *stuff)
3372 {
3373 	u_int32_t *tl;
3374 	struct nfsrv_descript nfsd, *nd = &nfsd;
3375 	nfsattrbit_t attrbits;
3376 	int error;
3377 
3378 	*attrflagp = 0;
3379 	NFSCL_REQSTART(nd, NFSPROC_COMMIT, vp);
3380 	NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
3381 	txdr_hyper(offset, tl);
3382 	tl += 2;
3383 	*tl = txdr_unsigned(cnt);
3384 	if (nd->nd_flag & ND_NFSV4) {
3385 		/*
3386 		 * And do a Getattr op.
3387 		 */
3388 		NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
3389 		*tl = txdr_unsigned(NFSV4OP_GETATTR);
3390 		NFSGETATTR_ATTRBIT(&attrbits);
3391 		(void) nfsrv_putattrbit(nd, &attrbits);
3392 	}
3393 	error = nfscl_request(nd, vp, p, cred, stuff);
3394 	if (error)
3395 		return (error);
3396 	error = nfscl_wcc_data(nd, vp, nap, attrflagp, NULL, stuff);
3397 	if (!error && !nd->nd_repstat) {
3398 		NFSM_DISSECT(tl, u_int32_t *, NFSX_VERF);
3399 		NFSBCOPY((caddr_t)tl, verfp, NFSX_VERF);
3400 		if (nd->nd_flag & ND_NFSV4)
3401 			error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
3402 	}
3403 nfsmout:
3404 	if (!error && nd->nd_repstat)
3405 		error = nd->nd_repstat;
3406 	mbuf_freem(nd->nd_mrep);
3407 	return (error);
3408 }
3409 
3410 /*
3411  * NFS byte range lock rpc.
3412  * (Mostly just calls one of the three lower level RPC routines.)
3413  */
3414 APPLESTATIC int
3415 nfsrpc_advlock(vnode_t vp, off_t size, int op, struct flock *fl,
3416     int reclaim, struct ucred *cred, NFSPROC_T *p)
3417 {
3418 	struct nfscllockowner *lp;
3419 	struct nfsclclient *clp;
3420 	struct nfsfh *nfhp;
3421 	struct nfsrv_descript nfsd, *nd = &nfsd;
3422 	struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
3423 	u_int64_t off, len;
3424 	off_t start, end;
3425 	u_int32_t clidrev = 0;
3426 	int error = 0, newone = 0, expireret = 0, retrycnt, donelocally;
3427 	int callcnt, dorpc;
3428 
3429 	/*
3430 	 * Convert the flock structure into a start and end and do POSIX
3431 	 * bounds checking.
3432 	 */
3433 	switch (fl->l_whence) {
3434 	case SEEK_SET:
3435 	case SEEK_CUR:
3436 		/*
3437 		 * Caller is responsible for adding any necessary offset
3438 		 * when SEEK_CUR is used.
3439 		 */
3440 		start = fl->l_start;
3441 		off = fl->l_start;
3442 		break;
3443 	case SEEK_END:
3444 		start = size + fl->l_start;
3445 		off = size + fl->l_start;
3446 		break;
3447 	default:
3448 		return (EINVAL);
3449 	};
3450 	if (start < 0)
3451 		return (EINVAL);
3452 	if (fl->l_len != 0) {
3453 		end = start + fl->l_len - 1;
3454 		if (end < start)
3455 			return (EINVAL);
3456 	}
3457 
3458 	len = fl->l_len;
3459 	if (len == 0)
3460 		len = NFS64BITSSET;
3461 	retrycnt = 0;
3462 	do {
3463 	    nd->nd_repstat = 0;
3464 	    if (op == F_GETLK) {
3465 		error = nfscl_getcl(vp, cred, p, &clp);
3466 		if (error)
3467 			return (error);
3468 		error = nfscl_lockt(vp, clp, off, len, fl, p);
3469 		if (!error) {
3470 			clidrev = clp->nfsc_clientidrev;
3471 			error = nfsrpc_lockt(nd, vp, clp, off, len, fl, cred,
3472 			    p);
3473 		} else if (error == -1) {
3474 			error = 0;
3475 		}
3476 		nfscl_clientrelease(clp);
3477 	    } else if (op == F_UNLCK && fl->l_type == F_UNLCK) {
3478 		/*
3479 		 * We must loop around for all lockowner cases.
3480 		 */
3481 		callcnt = 0;
3482 		error = nfscl_getcl(vp, cred, p, &clp);
3483 		if (error)
3484 			return (error);
3485 		do {
3486 		    error = nfscl_relbytelock(vp, off, len, cred, p, callcnt,
3487 			clp, &lp, &dorpc);
3488 		    /*
3489 		     * If it returns a NULL lp, we're done.
3490 		     */
3491 		    if (lp == NULL) {
3492 			if (callcnt == 0)
3493 			    nfscl_clientrelease(clp);
3494 			else
3495 			    nfscl_releasealllocks(clp, vp, p);
3496 			return (error);
3497 		    }
3498 		    if (nmp->nm_clp != NULL)
3499 			clidrev = nmp->nm_clp->nfsc_clientidrev;
3500 		    else
3501 			clidrev = 0;
3502 		    /*
3503 		     * If the server doesn't support Posix lock semantics,
3504 		     * only allow locks on the entire file, since it won't
3505 		     * handle overlapping byte ranges.
3506 		     * There might still be a problem when a lock
3507 		     * upgrade/downgrade (read<->write) occurs, since the
3508 		     * server "might" expect an unlock first?
3509 		     */
3510 		    if (dorpc && (lp->nfsl_open->nfso_posixlock ||
3511 			(off == 0 && len == NFS64BITSSET))) {
3512 			/*
3513 			 * Since the lock records will go away, we must
3514 			 * wait for grace and delay here.
3515 			 */
3516 			do {
3517 			    error = nfsrpc_locku(nd, nmp, lp, off, len,
3518 				NFSV4LOCKT_READ, cred, p, 0);
3519 			    if ((nd->nd_repstat == NFSERR_GRACE ||
3520 				 nd->nd_repstat == NFSERR_DELAY) &&
3521 				error == 0)
3522 				(void) nfs_catnap(PZERO, "nfs_advlock");
3523 			} while ((nd->nd_repstat == NFSERR_GRACE ||
3524 			    nd->nd_repstat == NFSERR_DELAY) && error == 0);
3525 		    }
3526 		    callcnt++;
3527 		} while (error == 0 && nd->nd_repstat == 0);
3528 		nfscl_releasealllocks(clp, vp, p);
3529 	    } else if (op == F_SETLK) {
3530 		error = nfscl_getbytelock(vp, off, len, fl->l_type, cred, p,
3531 		    NULL, 0, NULL, NULL, &lp, &newone, &donelocally);
3532 		if (error || donelocally) {
3533 			return (error);
3534 		}
3535 		if (nmp->nm_clp != NULL)
3536 			clidrev = nmp->nm_clp->nfsc_clientidrev;
3537 		else
3538 			clidrev = 0;
3539 		nfhp = VTONFS(vp)->n_fhp;
3540 		if (!lp->nfsl_open->nfso_posixlock &&
3541 		    (off != 0 || len != NFS64BITSSET)) {
3542 			error = EINVAL;
3543 		} else {
3544 			error = nfsrpc_lock(nd, nmp, vp, nfhp->nfh_fh,
3545 			    nfhp->nfh_len, lp, newone, reclaim, off,
3546 			    len, fl->l_type, cred, p, 0);
3547 		}
3548 		if (!error)
3549 			error = nd->nd_repstat;
3550 		nfscl_lockrelease(lp, error, newone);
3551 	    } else {
3552 		error = EINVAL;
3553 	    }
3554 	    if (!error)
3555 	        error = nd->nd_repstat;
3556 	    if (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
3557 		error == NFSERR_STALEDONTRECOVER ||
3558 		error == NFSERR_STALECLIENTID || error == NFSERR_DELAY) {
3559 		(void) nfs_catnap(PZERO, "nfs_advlock");
3560 	    } else if ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID)
3561 		&& clidrev != 0) {
3562 		expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
3563 		retrycnt++;
3564 	    }
3565 	} while (error == NFSERR_GRACE ||
3566 	    error == NFSERR_STALECLIENTID || error == NFSERR_DELAY ||
3567 	    error == NFSERR_STALEDONTRECOVER || error == NFSERR_STALESTATEID ||
3568 	    ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
3569 	     expireret == 0 && clidrev != 0 && retrycnt < 4));
3570 	if (error && retrycnt >= 4)
3571 		error = EIO;
3572 	return (error);
3573 }
3574 
3575 /*
3576  * The lower level routine for the LockT case.
3577  */
3578 APPLESTATIC int
3579 nfsrpc_lockt(struct nfsrv_descript *nd, vnode_t vp,
3580     struct nfsclclient *clp, u_int64_t off, u_int64_t len, struct flock *fl,
3581     struct ucred *cred, NFSPROC_T *p)
3582 {
3583 	u_int32_t *tl;
3584 	int error, type, size;
3585 	u_int8_t own[NFSV4CL_LOCKNAMELEN];
3586 
3587 	NFSCL_REQSTART(nd, NFSPROC_LOCKT, vp);
3588 	NFSM_BUILD(tl, u_int32_t *, 7 * NFSX_UNSIGNED);
3589 	if (fl->l_type == F_RDLCK)
3590 		*tl++ = txdr_unsigned(NFSV4LOCKT_READ);
3591 	else
3592 		*tl++ = txdr_unsigned(NFSV4LOCKT_WRITE);
3593 	txdr_hyper(off, tl);
3594 	tl += 2;
3595 	txdr_hyper(len, tl);
3596 	tl += 2;
3597 	*tl++ = clp->nfsc_clientid.lval[0];
3598 	*tl = clp->nfsc_clientid.lval[1];
3599 	nfscl_filllockowner(p, own);
3600 	(void) nfsm_strtom(nd, own, NFSV4CL_LOCKNAMELEN);
3601 	error = nfscl_request(nd, vp, p, cred, NULL);
3602 	if (error)
3603 		return (error);
3604 	if (nd->nd_repstat == 0) {
3605 		fl->l_type = F_UNLCK;
3606 	} else if (nd->nd_repstat == NFSERR_DENIED) {
3607 		nd->nd_repstat = 0;
3608 		fl->l_whence = SEEK_SET;
3609 		NFSM_DISSECT(tl, u_int32_t *, 8 * NFSX_UNSIGNED);
3610 		fl->l_start = fxdr_hyper(tl);
3611 		tl += 2;
3612 		len = fxdr_hyper(tl);
3613 		tl += 2;
3614 		if (len == NFS64BITSSET)
3615 			fl->l_len = 0;
3616 		else
3617 			fl->l_len = len;
3618 		type = fxdr_unsigned(int, *tl++);
3619 		if (type == NFSV4LOCKT_WRITE)
3620 			fl->l_type = F_WRLCK;
3621 		else
3622 			fl->l_type = F_RDLCK;
3623 		/*
3624 		 * XXX For now, I have no idea what to do with the
3625 		 * conflicting lock_owner, so I'll just set the pid == 0
3626 		 * and skip over the lock_owner.
3627 		 */
3628 		fl->l_pid = (pid_t)0;
3629 		tl += 2;
3630 		size = fxdr_unsigned(int, *tl);
3631 		if (size < 0 || size > NFSV4_OPAQUELIMIT)
3632 			error = EBADRPC;
3633 		if (!error)
3634 			error = nfsm_advance(nd, NFSM_RNDUP(size), -1);
3635 	} else if (nd->nd_repstat == NFSERR_STALECLIENTID)
3636 		nfscl_initiate_recovery(clp);
3637 nfsmout:
3638 	mbuf_freem(nd->nd_mrep);
3639 	return (error);
3640 }
3641 
3642 /*
3643  * Lower level function that performs the LockU RPC.
3644  */
3645 static int
3646 nfsrpc_locku(struct nfsrv_descript *nd, struct nfsmount *nmp,
3647     struct nfscllockowner *lp, u_int64_t off, u_int64_t len,
3648     u_int32_t type, struct ucred *cred, NFSPROC_T *p, int syscred)
3649 {
3650 	u_int32_t *tl;
3651 	int error;
3652 
3653 	nfscl_reqstart(nd, NFSPROC_LOCKU, nmp, lp->nfsl_open->nfso_fh,
3654 	    lp->nfsl_open->nfso_fhlen, NULL);
3655 	NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID + 6 * NFSX_UNSIGNED);
3656 	*tl++ = txdr_unsigned(type);
3657 	*tl = txdr_unsigned(lp->nfsl_seqid);
3658 	if (nfstest_outofseq &&
3659 	    (arc4random() % nfstest_outofseq) == 0)
3660 		*tl = txdr_unsigned(lp->nfsl_seqid + 1);
3661 	tl++;
3662 	*tl++ = lp->nfsl_stateid.seqid;
3663 	*tl++ = lp->nfsl_stateid.other[0];
3664 	*tl++ = lp->nfsl_stateid.other[1];
3665 	*tl++ = lp->nfsl_stateid.other[2];
3666 	txdr_hyper(off, tl);
3667 	tl += 2;
3668 	txdr_hyper(len, tl);
3669 	if (syscred)
3670 		nd->nd_flag |= ND_USEGSSNAME;
3671 	error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
3672 	    NFS_PROG, NFS_VER4, NULL, 1, NULL);
3673 	NFSCL_INCRSEQID(lp->nfsl_seqid, nd);
3674 	if (error)
3675 		return (error);
3676 	if (nd->nd_repstat == 0) {
3677 		NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID);
3678 		lp->nfsl_stateid.seqid = *tl++;
3679 		lp->nfsl_stateid.other[0] = *tl++;
3680 		lp->nfsl_stateid.other[1] = *tl++;
3681 		lp->nfsl_stateid.other[2] = *tl;
3682 	} else if (nd->nd_repstat == NFSERR_STALESTATEID)
3683 		nfscl_initiate_recovery(lp->nfsl_open->nfso_own->nfsow_clp);
3684 nfsmout:
3685 	mbuf_freem(nd->nd_mrep);
3686 	return (error);
3687 }
3688 
3689 /*
3690  * The actual Lock RPC.
3691  */
3692 APPLESTATIC int
3693 nfsrpc_lock(struct nfsrv_descript *nd, struct nfsmount *nmp, vnode_t vp,
3694     u_int8_t *nfhp, int fhlen, struct nfscllockowner *lp, int newone,
3695     int reclaim, u_int64_t off, u_int64_t len, short type, struct ucred *cred,
3696     NFSPROC_T *p, int syscred)
3697 {
3698 	u_int32_t *tl;
3699 	int error, size;
3700 
3701 	nfscl_reqstart(nd, NFSPROC_LOCK, nmp, nfhp, fhlen, NULL);
3702 	NFSM_BUILD(tl, u_int32_t *, 7 * NFSX_UNSIGNED);
3703 	if (type == F_RDLCK)
3704 		*tl++ = txdr_unsigned(NFSV4LOCKT_READ);
3705 	else
3706 		*tl++ = txdr_unsigned(NFSV4LOCKT_WRITE);
3707 	*tl++ = txdr_unsigned(reclaim);
3708 	txdr_hyper(off, tl);
3709 	tl += 2;
3710 	txdr_hyper(len, tl);
3711 	tl += 2;
3712 	if (newone) {
3713 	    *tl = newnfs_true;
3714 	    NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID +
3715 		2 * NFSX_UNSIGNED + NFSX_HYPER);
3716 	    *tl++ = txdr_unsigned(lp->nfsl_open->nfso_own->nfsow_seqid);
3717 	    *tl++ = lp->nfsl_open->nfso_stateid.seqid;
3718 	    *tl++ = lp->nfsl_open->nfso_stateid.other[0];
3719 	    *tl++ = lp->nfsl_open->nfso_stateid.other[1];
3720 	    *tl++ = lp->nfsl_open->nfso_stateid.other[2];
3721 	    *tl++ = txdr_unsigned(lp->nfsl_seqid);
3722 	    *tl++ = lp->nfsl_open->nfso_own->nfsow_clp->nfsc_clientid.lval[0];
3723 	    *tl = lp->nfsl_open->nfso_own->nfsow_clp->nfsc_clientid.lval[1];
3724 	    (void) nfsm_strtom(nd, lp->nfsl_owner, NFSV4CL_LOCKNAMELEN);
3725 	} else {
3726 	    *tl = newnfs_false;
3727 	    NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID + NFSX_UNSIGNED);
3728 	    *tl++ = lp->nfsl_stateid.seqid;
3729 	    *tl++ = lp->nfsl_stateid.other[0];
3730 	    *tl++ = lp->nfsl_stateid.other[1];
3731 	    *tl++ = lp->nfsl_stateid.other[2];
3732 	    *tl = txdr_unsigned(lp->nfsl_seqid);
3733 	    if (nfstest_outofseq &&
3734 		(arc4random() % nfstest_outofseq) == 0)
3735 		    *tl = txdr_unsigned(lp->nfsl_seqid + 1);
3736 	}
3737 	if (syscred)
3738 		nd->nd_flag |= ND_USEGSSNAME;
3739 	error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, vp, p, cred,
3740 	    NFS_PROG, NFS_VER4, NULL, 1, NULL);
3741 	if (error)
3742 		return (error);
3743 	if (newone)
3744 	    NFSCL_INCRSEQID(lp->nfsl_open->nfso_own->nfsow_seqid, nd);
3745 	NFSCL_INCRSEQID(lp->nfsl_seqid, nd);
3746 	if (nd->nd_repstat == 0) {
3747 		NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID);
3748 		lp->nfsl_stateid.seqid = *tl++;
3749 		lp->nfsl_stateid.other[0] = *tl++;
3750 		lp->nfsl_stateid.other[1] = *tl++;
3751 		lp->nfsl_stateid.other[2] = *tl;
3752 	} else if (nd->nd_repstat == NFSERR_DENIED) {
3753 		NFSM_DISSECT(tl, u_int32_t *, 8 * NFSX_UNSIGNED);
3754 		size = fxdr_unsigned(int, *(tl + 7));
3755 		if (size < 0 || size > NFSV4_OPAQUELIMIT)
3756 			error = EBADRPC;
3757 		if (!error)
3758 			error = nfsm_advance(nd, NFSM_RNDUP(size), -1);
3759 	} else if (nd->nd_repstat == NFSERR_STALESTATEID)
3760 		nfscl_initiate_recovery(lp->nfsl_open->nfso_own->nfsow_clp);
3761 nfsmout:
3762 	mbuf_freem(nd->nd_mrep);
3763 	return (error);
3764 }
3765 
3766 /*
3767  * nfs statfs rpc
3768  * (always called with the vp for the mount point)
3769  */
3770 APPLESTATIC int
3771 nfsrpc_statfs(vnode_t vp, struct nfsstatfs *sbp, struct nfsfsinfo *fsp,
3772     struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp,
3773     void *stuff)
3774 {
3775 	u_int32_t *tl = NULL;
3776 	struct nfsrv_descript nfsd, *nd = &nfsd;
3777 	struct nfsmount *nmp;
3778 	nfsattrbit_t attrbits;
3779 	int error;
3780 
3781 	*attrflagp = 0;
3782 	nmp = VFSTONFS(vnode_mount(vp));
3783 	if (NFSHASNFSV4(nmp)) {
3784 		/*
3785 		 * For V4, you actually do a getattr.
3786 		 */
3787 		NFSCL_REQSTART(nd, NFSPROC_GETATTR, vp);
3788 		NFSSTATFS_GETATTRBIT(&attrbits);
3789 		(void) nfsrv_putattrbit(nd, &attrbits);
3790 		nd->nd_flag |= ND_USEGSSNAME;
3791 		error = nfscl_request(nd, vp, p, cred, stuff);
3792 		if (error)
3793 			return (error);
3794 		if (nd->nd_repstat == 0) {
3795 			error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0,
3796 			    NULL, NULL, sbp, fsp, NULL, 0, NULL, NULL, NULL, p,
3797 			    cred);
3798 			if (!error) {
3799 				nmp->nm_fsid[0] = nap->na_filesid[0];
3800 				nmp->nm_fsid[1] = nap->na_filesid[1];
3801 				NFSSETHASSETFSID(nmp);
3802 				*attrflagp = 1;
3803 			}
3804 		} else {
3805 			error = nd->nd_repstat;
3806 		}
3807 		if (error)
3808 			goto nfsmout;
3809 	} else {
3810 		NFSCL_REQSTART(nd, NFSPROC_FSSTAT, vp);
3811 		error = nfscl_request(nd, vp, p, cred, stuff);
3812 		if (error)
3813 			return (error);
3814 		if (nd->nd_flag & ND_NFSV3) {
3815 			error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
3816 			if (error)
3817 				goto nfsmout;
3818 		}
3819 		if (nd->nd_repstat) {
3820 			error = nd->nd_repstat;
3821 			goto nfsmout;
3822 		}
3823 		NFSM_DISSECT(tl, u_int32_t *,
3824 		    NFSX_STATFS(nd->nd_flag & ND_NFSV3));
3825 	}
3826 	if (NFSHASNFSV3(nmp)) {
3827 		sbp->sf_tbytes = fxdr_hyper(tl); tl += 2;
3828 		sbp->sf_fbytes = fxdr_hyper(tl); tl += 2;
3829 		sbp->sf_abytes = fxdr_hyper(tl); tl += 2;
3830 		sbp->sf_tfiles = fxdr_hyper(tl); tl += 2;
3831 		sbp->sf_ffiles = fxdr_hyper(tl); tl += 2;
3832 		sbp->sf_afiles = fxdr_hyper(tl); tl += 2;
3833 		sbp->sf_invarsec = fxdr_unsigned(u_int32_t, *tl);
3834 	} else if (NFSHASNFSV4(nmp) == 0) {
3835 		sbp->sf_tsize = fxdr_unsigned(u_int32_t, *tl++);
3836 		sbp->sf_bsize = fxdr_unsigned(u_int32_t, *tl++);
3837 		sbp->sf_blocks = fxdr_unsigned(u_int32_t, *tl++);
3838 		sbp->sf_bfree = fxdr_unsigned(u_int32_t, *tl++);
3839 		sbp->sf_bavail = fxdr_unsigned(u_int32_t, *tl);
3840 	}
3841 nfsmout:
3842 	mbuf_freem(nd->nd_mrep);
3843 	return (error);
3844 }
3845 
3846 /*
3847  * nfs pathconf rpc
3848  */
3849 APPLESTATIC int
3850 nfsrpc_pathconf(vnode_t vp, struct nfsv3_pathconf *pc,
3851     struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp,
3852     void *stuff)
3853 {
3854 	struct nfsrv_descript nfsd, *nd = &nfsd;
3855 	struct nfsmount *nmp;
3856 	u_int32_t *tl;
3857 	nfsattrbit_t attrbits;
3858 	int error;
3859 
3860 	*attrflagp = 0;
3861 	nmp = VFSTONFS(vnode_mount(vp));
3862 	if (NFSHASNFSV4(nmp)) {
3863 		/*
3864 		 * For V4, you actually do a getattr.
3865 		 */
3866 		NFSCL_REQSTART(nd, NFSPROC_GETATTR, vp);
3867 		NFSPATHCONF_GETATTRBIT(&attrbits);
3868 		(void) nfsrv_putattrbit(nd, &attrbits);
3869 		nd->nd_flag |= ND_USEGSSNAME;
3870 		error = nfscl_request(nd, vp, p, cred, stuff);
3871 		if (error)
3872 			return (error);
3873 		if (nd->nd_repstat == 0) {
3874 			error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0,
3875 			    pc, NULL, NULL, NULL, NULL, 0, NULL, NULL, NULL, p,
3876 			    cred);
3877 			if (!error)
3878 				*attrflagp = 1;
3879 		} else {
3880 			error = nd->nd_repstat;
3881 		}
3882 	} else {
3883 		NFSCL_REQSTART(nd, NFSPROC_PATHCONF, vp);
3884 		error = nfscl_request(nd, vp, p, cred, stuff);
3885 		if (error)
3886 			return (error);
3887 		error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
3888 		if (nd->nd_repstat && !error)
3889 			error = nd->nd_repstat;
3890 		if (!error) {
3891 			NFSM_DISSECT(tl, u_int32_t *, NFSX_V3PATHCONF);
3892 			pc->pc_linkmax = fxdr_unsigned(u_int32_t, *tl++);
3893 			pc->pc_namemax = fxdr_unsigned(u_int32_t, *tl++);
3894 			pc->pc_notrunc = fxdr_unsigned(u_int32_t, *tl++);
3895 			pc->pc_chownrestricted =
3896 			    fxdr_unsigned(u_int32_t, *tl++);
3897 			pc->pc_caseinsensitive =
3898 			    fxdr_unsigned(u_int32_t, *tl++);
3899 			pc->pc_casepreserving = fxdr_unsigned(u_int32_t, *tl);
3900 		}
3901 	}
3902 nfsmout:
3903 	mbuf_freem(nd->nd_mrep);
3904 	return (error);
3905 }
3906 
3907 /*
3908  * nfs version 3 fsinfo rpc call
3909  */
3910 APPLESTATIC int
3911 nfsrpc_fsinfo(vnode_t vp, struct nfsfsinfo *fsp, struct ucred *cred,
3912     NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, void *stuff)
3913 {
3914 	u_int32_t *tl;
3915 	struct nfsrv_descript nfsd, *nd = &nfsd;
3916 	int error;
3917 
3918 	*attrflagp = 0;
3919 	NFSCL_REQSTART(nd, NFSPROC_FSINFO, vp);
3920 	error = nfscl_request(nd, vp, p, cred, stuff);
3921 	if (error)
3922 		return (error);
3923 	error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
3924 	if (nd->nd_repstat && !error)
3925 		error = nd->nd_repstat;
3926 	if (!error) {
3927 		NFSM_DISSECT(tl, u_int32_t *, NFSX_V3FSINFO);
3928 		fsp->fs_rtmax = fxdr_unsigned(u_int32_t, *tl++);
3929 		fsp->fs_rtpref = fxdr_unsigned(u_int32_t, *tl++);
3930 		fsp->fs_rtmult = fxdr_unsigned(u_int32_t, *tl++);
3931 		fsp->fs_wtmax = fxdr_unsigned(u_int32_t, *tl++);
3932 		fsp->fs_wtpref = fxdr_unsigned(u_int32_t, *tl++);
3933 		fsp->fs_wtmult = fxdr_unsigned(u_int32_t, *tl++);
3934 		fsp->fs_dtpref = fxdr_unsigned(u_int32_t, *tl++);
3935 		fsp->fs_maxfilesize = fxdr_hyper(tl);
3936 		tl += 2;
3937 		fxdr_nfsv3time(tl, &fsp->fs_timedelta);
3938 		tl += 2;
3939 		fsp->fs_properties = fxdr_unsigned(u_int32_t, *tl);
3940 	}
3941 nfsmout:
3942 	mbuf_freem(nd->nd_mrep);
3943 	return (error);
3944 }
3945 
3946 /*
3947  * This function performs the Renew RPC.
3948  */
3949 APPLESTATIC int
3950 nfsrpc_renew(struct nfsclclient *clp, struct ucred *cred, NFSPROC_T *p)
3951 {
3952 	u_int32_t *tl;
3953 	struct nfsrv_descript nfsd;
3954 	struct nfsrv_descript *nd = &nfsd;
3955 	struct nfsmount *nmp;
3956 	int error;
3957 
3958 	nmp = clp->nfsc_nmp;
3959 	if (nmp == NULL)
3960 		return (0);
3961 	nfscl_reqstart(nd, NFSPROC_RENEW, nmp, NULL, 0, NULL);
3962 	NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
3963 	*tl++ = clp->nfsc_clientid.lval[0];
3964 	*tl = clp->nfsc_clientid.lval[1];
3965 	nd->nd_flag |= ND_USEGSSNAME;
3966 	error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
3967 		NFS_PROG, NFS_VER4, NULL, 1, NULL);
3968 	if (error)
3969 		return (error);
3970 	error = nd->nd_repstat;
3971 	mbuf_freem(nd->nd_mrep);
3972 	return (error);
3973 }
3974 
3975 /*
3976  * This function performs the Releaselockowner RPC.
3977  */
3978 APPLESTATIC int
3979 nfsrpc_rellockown(struct nfsmount *nmp, struct nfscllockowner *lp,
3980     struct ucred *cred, NFSPROC_T *p)
3981 {
3982 	struct nfsrv_descript nfsd, *nd = &nfsd;
3983 	u_int32_t *tl;
3984 	int error;
3985 
3986 	nfscl_reqstart(nd, NFSPROC_RELEASELCKOWN, nmp, NULL, 0, NULL);
3987 	NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
3988 	*tl++ = nmp->nm_clp->nfsc_clientid.lval[0];
3989 	*tl = nmp->nm_clp->nfsc_clientid.lval[1];
3990 	(void) nfsm_strtom(nd, lp->nfsl_owner, NFSV4CL_LOCKNAMELEN);
3991 	nd->nd_flag |= ND_USEGSSNAME;
3992 	error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
3993 	    NFS_PROG, NFS_VER4, NULL, 1, NULL);
3994 	if (error)
3995 		return (error);
3996 	error = nd->nd_repstat;
3997 	mbuf_freem(nd->nd_mrep);
3998 	return (error);
3999 }
4000 
4001 /*
4002  * This function performs the Compound to get the mount pt FH.
4003  */
4004 APPLESTATIC int
4005 nfsrpc_getdirpath(struct nfsmount *nmp, u_char *dirpath, struct ucred *cred,
4006     NFSPROC_T *p)
4007 {
4008 	u_int32_t *tl;
4009 	struct nfsrv_descript nfsd;
4010 	struct nfsrv_descript *nd = &nfsd;
4011 	u_char *cp, *cp2;
4012 	int error, cnt, len, setnil;
4013 	u_int32_t *opcntp;
4014 
4015 	nfscl_reqstart(nd, NFSPROC_PUTROOTFH, nmp, NULL, 0, &opcntp);
4016 	cp = dirpath;
4017 	cnt = 0;
4018 	do {
4019 		setnil = 0;
4020 		while (*cp == '/')
4021 			cp++;
4022 		cp2 = cp;
4023 		while (*cp2 != '\0' && *cp2 != '/')
4024 			cp2++;
4025 		if (*cp2 == '/') {
4026 			setnil = 1;
4027 			*cp2 = '\0';
4028 		}
4029 		if (cp2 != cp) {
4030 			NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
4031 			*tl = txdr_unsigned(NFSV4OP_LOOKUP);
4032 			nfsm_strtom(nd, cp, strlen(cp));
4033 			cnt++;
4034 		}
4035 		if (setnil)
4036 			*cp2++ = '/';
4037 		cp = cp2;
4038 	} while (*cp != '\0');
4039 	*opcntp = txdr_unsigned(2 + cnt);
4040 	NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
4041 	*tl = txdr_unsigned(NFSV4OP_GETFH);
4042 	nd->nd_flag |= ND_USEGSSNAME;
4043 	error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4044 		NFS_PROG, NFS_VER4, NULL, 1, NULL);
4045 	if (error)
4046 		return (error);
4047 	if (nd->nd_repstat == 0) {
4048 		NFSM_DISSECT(tl, u_int32_t *, (3 + 2 * cnt) * NFSX_UNSIGNED);
4049 		tl += (2 + 2 * cnt);
4050 		if ((len = fxdr_unsigned(int, *tl)) <= 0 ||
4051 			len > NFSX_FHMAX) {
4052 			nd->nd_repstat = NFSERR_BADXDR;
4053 		} else {
4054 			nd->nd_repstat = nfsrv_mtostr(nd, nmp->nm_fh, len);
4055 			if (nd->nd_repstat == 0)
4056 				nmp->nm_fhsize = len;
4057 		}
4058 	}
4059 	error = nd->nd_repstat;
4060 nfsmout:
4061 	mbuf_freem(nd->nd_mrep);
4062 	return (error);
4063 }
4064 
4065 /*
4066  * This function performs the Delegreturn RPC.
4067  */
4068 APPLESTATIC int
4069 nfsrpc_delegreturn(struct nfscldeleg *dp, struct ucred *cred,
4070     struct nfsmount *nmp, NFSPROC_T *p, int syscred)
4071 {
4072 	u_int32_t *tl;
4073 	struct nfsrv_descript nfsd;
4074 	struct nfsrv_descript *nd = &nfsd;
4075 	int error;
4076 
4077 	nfscl_reqstart(nd, NFSPROC_DELEGRETURN, nmp, dp->nfsdl_fh,
4078 	    dp->nfsdl_fhlen, NULL);
4079 	NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID);
4080 	*tl++ = dp->nfsdl_stateid.seqid;
4081 	*tl++ = dp->nfsdl_stateid.other[0];
4082 	*tl++ = dp->nfsdl_stateid.other[1];
4083 	*tl = dp->nfsdl_stateid.other[2];
4084 	if (syscred)
4085 		nd->nd_flag |= ND_USEGSSNAME;
4086 	error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4087 	    NFS_PROG, NFS_VER4, NULL, 1, NULL);
4088 	if (error)
4089 		return (error);
4090 	error = nd->nd_repstat;
4091 	mbuf_freem(nd->nd_mrep);
4092 	return (error);
4093 }
4094 
4095 /*
4096  * nfs getacl call.
4097  */
4098 APPLESTATIC int
4099 nfsrpc_getacl(vnode_t vp, struct ucred *cred, NFSPROC_T *p,
4100     struct acl *aclp, void *stuff)
4101 {
4102 	struct nfsrv_descript nfsd, *nd = &nfsd;
4103 	int error;
4104 	nfsattrbit_t attrbits;
4105 	struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
4106 
4107 	if (nfsrv_useacl == 0 || !NFSHASNFSV4(nmp))
4108 		return (EOPNOTSUPP);
4109 	NFSCL_REQSTART(nd, NFSPROC_GETACL, vp);
4110 	NFSZERO_ATTRBIT(&attrbits);
4111 	NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_ACL);
4112 	(void) nfsrv_putattrbit(nd, &attrbits);
4113 	error = nfscl_request(nd, vp, p, cred, stuff);
4114 	if (error)
4115 		return (error);
4116 	if (!nd->nd_repstat)
4117 		error = nfsv4_loadattr(nd, vp, NULL, NULL, NULL, 0, NULL,
4118 		    NULL, NULL, NULL, aclp, 0, NULL, NULL, NULL, p, cred);
4119 	else
4120 		error = nd->nd_repstat;
4121 	mbuf_freem(nd->nd_mrep);
4122 	return (error);
4123 }
4124 
4125 /*
4126  * nfs setacl call.
4127  */
4128 APPLESTATIC int
4129 nfsrpc_setacl(vnode_t vp, struct ucred *cred, NFSPROC_T *p,
4130     struct acl *aclp, void *stuff)
4131 {
4132 	int error;
4133 	struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
4134 
4135 	if (nfsrv_useacl == 0 || !NFSHASNFSV4(nmp))
4136 		return (EOPNOTSUPP);
4137 	error = nfsrpc_setattr(vp, NULL, aclp, cred, p, NULL, NULL, stuff);
4138 	return (error);
4139 }
4140 
4141 /*
4142  * nfs setacl call.
4143  */
4144 static int
4145 nfsrpc_setaclrpc(vnode_t vp, struct ucred *cred, NFSPROC_T *p,
4146     struct acl *aclp, nfsv4stateid_t *stateidp, void *stuff)
4147 {
4148 	struct nfsrv_descript nfsd, *nd = &nfsd;
4149 	int error;
4150 	nfsattrbit_t attrbits;
4151 	struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
4152 
4153 	if (!NFSHASNFSV4(nmp))
4154 		return (EOPNOTSUPP);
4155 	NFSCL_REQSTART(nd, NFSPROC_SETACL, vp);
4156 	nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID);
4157 	NFSZERO_ATTRBIT(&attrbits);
4158 	NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_ACL);
4159 	(void) nfsv4_fillattr(nd, vp, aclp, NULL, NULL, 0, &attrbits,
4160 	    NULL, NULL, 0, 0);
4161 	error = nfscl_request(nd, vp, p, cred, stuff);
4162 	if (error)
4163 		return (error);
4164 	/* Don't care about the pre/postop attributes */
4165 	mbuf_freem(nd->nd_mrep);
4166 	return (nd->nd_repstat);
4167 }
4168