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