xref: /freebsd/sys/fs/nfsserver/nfs_nfsdserv.c (revision 13d826ff947d9026f98e317e7385b22abfc0eace)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1989, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Rick Macklem at The University of Guelph.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  */
35 
36 #include <sys/cdefs.h>
37 #include "opt_inet.h"
38 #include "opt_inet6.h"
39 /*
40  * nfs version 2, 3 and 4 server calls to vnode ops
41  * - these routines generally have 3 phases
42  *   1 - break down and validate rpc request in mbuf list
43  *   2 - do the vnode ops for the request, usually by calling a nfsvno_XXX()
44  *       function in nfsd_port.c
45  *   3 - build the rpc reply in an mbuf list
46  * For nfsv4, these functions are called for each Op within the Compound RPC.
47  */
48 
49 #include <fs/nfs/nfsport.h>
50 #include <sys/extattr.h>
51 #include <sys/filio.h>
52 
53 /* Global vars */
54 extern u_int32_t newnfs_false, newnfs_true;
55 extern __enum_uint8(vtype) nv34tov_type[8];
56 extern struct timeval nfsboottime;
57 extern int nfsrv_enable_crossmntpt;
58 extern int nfsrv_statehashsize;
59 extern int nfsrv_layouthashsize;
60 extern time_t nfsdev_time;
61 extern volatile int nfsrv_devidcnt;
62 extern int nfsd_debuglevel;
63 extern u_long sb_max_adj;
64 extern int nfsrv_pnfsatime;
65 extern int nfsrv_maxpnfsmirror;
66 extern uint32_t nfs_srvmaxio;
67 
68 static int	nfs_async = 0;
69 SYSCTL_DECL(_vfs_nfsd);
70 SYSCTL_INT(_vfs_nfsd, OID_AUTO, async, CTLFLAG_RW, &nfs_async, 0,
71     "Tell client that writes were synced even though they were not");
72 extern int	nfsrv_doflexfile;
73 SYSCTL_INT(_vfs_nfsd, OID_AUTO, default_flexfile, CTLFLAG_RW,
74     &nfsrv_doflexfile, 0, "Make Flex File Layout the default for pNFS");
75 static int	nfsrv_linux42server = 1;
76 SYSCTL_INT(_vfs_nfsd, OID_AUTO, linux42server, CTLFLAG_RW,
77     &nfsrv_linux42server, 0,
78     "Enable Linux style NFSv4.2 server (non-RFC compliant)");
79 static bool	nfsrv_openaccess = true;
80 SYSCTL_BOOL(_vfs_nfsd, OID_AUTO, v4openaccess, CTLFLAG_RW,
81     &nfsrv_openaccess, 0,
82     "Enable Linux style NFSv4 Open access check");
83 static char nfsrv_scope[NFSV4_OPAQUELIMIT];
84 SYSCTL_STRING(_vfs_nfsd, OID_AUTO, scope, CTLFLAG_RWTUN,
85     &nfsrv_scope, NFSV4_OPAQUELIMIT, "Server scope");
86 static char nfsrv_owner_major[NFSV4_OPAQUELIMIT];
87 SYSCTL_STRING(_vfs_nfsd, OID_AUTO, owner_major, CTLFLAG_RWTUN,
88     &nfsrv_owner_major, NFSV4_OPAQUELIMIT, "Server owner major");
89 static uint64_t nfsrv_owner_minor;
90 SYSCTL_U64(_vfs_nfsd, OID_AUTO, owner_minor, CTLFLAG_RWTUN,
91     &nfsrv_owner_minor, 0, "Server owner minor");
92 /*
93  * Only enable this if all your exported file systems
94  * (or pNFS DSs for the pNFS case) support VOP_ALLOCATE.
95  */
96 static bool	nfsrv_doallocate = false;
97 SYSCTL_BOOL(_vfs_nfsd, OID_AUTO, enable_v42allocate, CTLFLAG_RW,
98     &nfsrv_doallocate, 0,
99     "Enable NFSv4.2 Allocate operation");
100 static uint64_t nfsrv_maxcopyrange = SSIZE_MAX;
101 SYSCTL_U64(_vfs_nfsd, OID_AUTO, maxcopyrange, CTLFLAG_RW,
102     &nfsrv_maxcopyrange, 0, "Max size of a Copy so RPC times reasonable");
103 
104 /*
105  * This list defines the GSS mechanisms supported.
106  * (Don't ask me how you get these strings from the RFC stuff like
107  *  iso(1), org(3)... but someone did it, so I don't need to know.)
108  */
109 static struct nfsgss_mechlist nfsgss_mechlist[] = {
110 	{ 9, "\052\206\110\206\367\022\001\002\002", 11 },
111 	{ 0, "", 0 },
112 };
113 
114 /* local functions */
115 static void nfsrvd_symlinksub(struct nfsrv_descript *nd, struct nameidata *ndp,
116     struct nfsvattr *nvap, fhandle_t *fhp, vnode_t *vpp,
117     vnode_t dirp, struct nfsvattr *dirforp, struct nfsvattr *diraftp,
118     int *diraft_retp, nfsattrbit_t *attrbitp,
119     NFSACL_T *aclp, NFSPROC_T *p, struct nfsexstuff *exp, char *pathcp,
120     int pathlen);
121 static void nfsrvd_mkdirsub(struct nfsrv_descript *nd, struct nameidata *ndp,
122     struct nfsvattr *nvap, fhandle_t *fhp, vnode_t *vpp,
123     vnode_t dirp, struct nfsvattr *dirforp, struct nfsvattr *diraftp,
124     int *diraft_retp, nfsattrbit_t *attrbitp, NFSACL_T *aclp,
125     NFSPROC_T *p, struct nfsexstuff *exp);
126 
127 /*
128  * nfs access service (not a part of NFS V2)
129  */
130 int
131 nfsrvd_access(struct nfsrv_descript *nd, __unused int isdgram,
132     vnode_t vp, struct nfsexstuff *exp)
133 {
134 	u_int32_t *tl;
135 	int getret, error = 0;
136 	struct nfsvattr nva;
137 	u_int32_t testmode, nfsmode, supported = 0;
138 	accmode_t deletebit;
139 	struct thread *p = curthread;
140 
141 	if (nd->nd_repstat) {
142 		nfsrv_postopattr(nd, 1, &nva);
143 		goto out;
144 	}
145 	NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
146 	nfsmode = fxdr_unsigned(u_int32_t, *tl);
147 	if ((nd->nd_flag & ND_NFSV4) &&
148 	    (nfsmode & ~(NFSACCESS_READ | NFSACCESS_LOOKUP |
149 	     NFSACCESS_MODIFY | NFSACCESS_EXTEND | NFSACCESS_DELETE |
150 	     NFSACCESS_EXECUTE | NFSACCESS_XAREAD | NFSACCESS_XAWRITE |
151 	     NFSACCESS_XALIST))) {
152 		nd->nd_repstat = NFSERR_INVAL;
153 		vput(vp);
154 		goto out;
155 	}
156 	if (nfsmode & NFSACCESS_READ) {
157 		supported |= NFSACCESS_READ;
158 		if (nfsvno_accchk(vp, VREAD, nd->nd_cred, exp, p,
159 		    NFSACCCHK_NOOVERRIDE, NFSACCCHK_VPISLOCKED, &supported))
160 			nfsmode &= ~NFSACCESS_READ;
161 	}
162 	if (nfsmode & NFSACCESS_MODIFY) {
163 		supported |= NFSACCESS_MODIFY;
164 		if (nfsvno_accchk(vp, VWRITE, nd->nd_cred, exp, p,
165 		    NFSACCCHK_NOOVERRIDE, NFSACCCHK_VPISLOCKED, &supported))
166 			nfsmode &= ~NFSACCESS_MODIFY;
167 	}
168 	if (nfsmode & NFSACCESS_EXTEND) {
169 		supported |= NFSACCESS_EXTEND;
170 		if (nfsvno_accchk(vp, VWRITE | VAPPEND, nd->nd_cred, exp, p,
171 		    NFSACCCHK_NOOVERRIDE, NFSACCCHK_VPISLOCKED, &supported))
172 			nfsmode &= ~NFSACCESS_EXTEND;
173 	}
174 	if (nfsmode & NFSACCESS_XAREAD) {
175 		supported |= NFSACCESS_XAREAD;
176 		if (nfsvno_accchk(vp, VREAD, nd->nd_cred, exp, p,
177 		    NFSACCCHK_NOOVERRIDE, NFSACCCHK_VPISLOCKED, &supported))
178 			nfsmode &= ~NFSACCESS_XAREAD;
179 	}
180 	if (nfsmode & NFSACCESS_XAWRITE) {
181 		supported |= NFSACCESS_XAWRITE;
182 		if (nfsvno_accchk(vp, VWRITE, nd->nd_cred, exp, p,
183 		    NFSACCCHK_NOOVERRIDE, NFSACCCHK_VPISLOCKED, &supported))
184 			nfsmode &= ~NFSACCESS_XAWRITE;
185 	}
186 	if (nfsmode & NFSACCESS_XALIST) {
187 		supported |= NFSACCESS_XALIST;
188 		if (nfsvno_accchk(vp, VREAD, nd->nd_cred, exp, p,
189 		    NFSACCCHK_NOOVERRIDE, NFSACCCHK_VPISLOCKED, &supported))
190 			nfsmode &= ~NFSACCESS_XALIST;
191 	}
192 	if (nfsmode & NFSACCESS_DELETE) {
193 		supported |= NFSACCESS_DELETE;
194 		if (vp->v_type == VDIR)
195 			deletebit = VDELETE_CHILD;
196 		else
197 			deletebit = VDELETE;
198 		if (nfsvno_accchk(vp, deletebit, nd->nd_cred, exp, p,
199 		    NFSACCCHK_NOOVERRIDE, NFSACCCHK_VPISLOCKED, &supported))
200 			nfsmode &= ~NFSACCESS_DELETE;
201 	}
202 	if (vp->v_type == VDIR)
203 		testmode = NFSACCESS_LOOKUP;
204 	else
205 		testmode = NFSACCESS_EXECUTE;
206 	if (nfsmode & testmode) {
207 		supported |= (nfsmode & testmode);
208 		if (nfsvno_accchk(vp, VEXEC, nd->nd_cred, exp, p,
209 		    NFSACCCHK_NOOVERRIDE, NFSACCCHK_VPISLOCKED, &supported))
210 			nfsmode &= ~testmode;
211 	}
212 	nfsmode &= supported;
213 	if (nd->nd_flag & ND_NFSV3) {
214 		getret = nfsvno_getattr(vp, &nva, nd, p, 1, NULL);
215 		nfsrv_postopattr(nd, getret, &nva);
216 	}
217 	vput(vp);
218 	if (nd->nd_flag & ND_NFSV4) {
219 		NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
220 		*tl++ = txdr_unsigned(supported);
221 	} else
222 		NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
223 	*tl = txdr_unsigned(nfsmode);
224 
225 out:
226 	NFSEXITCODE2(0, nd);
227 	return (0);
228 nfsmout:
229 	vput(vp);
230 	NFSEXITCODE2(error, nd);
231 	return (error);
232 }
233 
234 /*
235  * nfs getattr service
236  */
237 int
238 nfsrvd_getattr(struct nfsrv_descript *nd, int isdgram,
239     vnode_t vp, __unused struct nfsexstuff *exp)
240 {
241 	struct nfsvattr nva;
242 	fhandle_t fh;
243 	int at_root = 0, error = 0, supports_nfsv4acls;
244 	struct nfsreferral *refp;
245 	nfsattrbit_t attrbits, tmpbits;
246 	struct mount *mp;
247 	struct vnode *tvp = NULL;
248 	struct vattr va;
249 	uint64_t mounted_on_fileno = 0;
250 	accmode_t accmode;
251 	struct thread *p = curthread;
252 
253 	if (nd->nd_repstat)
254 		goto out;
255 	if (nd->nd_flag & ND_NFSV4) {
256 		error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
257 		if (error) {
258 			vput(vp);
259 			goto out;
260 		}
261 
262 		/*
263 		 * Check for a referral.
264 		 */
265 		refp = nfsv4root_getreferral(vp, NULL, 0);
266 		if (refp != NULL) {
267 			(void) nfsrv_putreferralattr(nd, &attrbits, refp, 1,
268 			    &nd->nd_repstat);
269 			vput(vp);
270 			goto out;
271 		}
272 		if (nd->nd_repstat == 0) {
273 			accmode = 0;
274 			NFSSET_ATTRBIT(&tmpbits, &attrbits);
275 
276 			/*
277 			 * GETATTR with write-only attr time_access_set and time_modify_set
278 			 * should return NFS4ERR_INVAL.
279 			 */
280 			if (NFSISSET_ATTRBIT(&tmpbits, NFSATTRBIT_TIMEACCESSSET) ||
281 					NFSISSET_ATTRBIT(&tmpbits, NFSATTRBIT_TIMEMODIFYSET)){
282 				error = NFSERR_INVAL;
283 				vput(vp);
284 				goto out;
285 			}
286 			if (NFSISSET_ATTRBIT(&tmpbits, NFSATTRBIT_ACL)) {
287 				NFSCLRBIT_ATTRBIT(&tmpbits, NFSATTRBIT_ACL);
288 				accmode |= VREAD_ACL;
289 			}
290 			if (NFSNONZERO_ATTRBIT(&tmpbits))
291 				accmode |= VREAD_ATTRIBUTES;
292 			if (accmode != 0)
293 				nd->nd_repstat = nfsvno_accchk(vp, accmode,
294 				    nd->nd_cred, exp, p, NFSACCCHK_NOOVERRIDE,
295 				    NFSACCCHK_VPISLOCKED, NULL);
296 		}
297 	}
298 	if (!nd->nd_repstat)
299 		nd->nd_repstat = nfsvno_getattr(vp, &nva, nd, p, 1, &attrbits);
300 	if (!nd->nd_repstat) {
301 		if (nd->nd_flag & ND_NFSV4) {
302 			if (NFSISSET_ATTRBIT(&attrbits, NFSATTRBIT_FILEHANDLE))
303 				nd->nd_repstat = nfsvno_getfh(vp, &fh, p);
304 			if (!nd->nd_repstat)
305 				nd->nd_repstat = nfsrv_checkgetattr(nd, vp,
306 				    &nva, &attrbits, p);
307 			if (nd->nd_repstat == 0) {
308 				supports_nfsv4acls = nfs_supportsnfsv4acls(vp);
309 				mp = vp->v_mount;
310 				if (nfsrv_enable_crossmntpt != 0 &&
311 				    vp->v_type == VDIR &&
312 				    (vp->v_vflag & VV_ROOT) != 0 &&
313 				    vp != rootvnode) {
314 					tvp = mp->mnt_vnodecovered;
315 					VREF(tvp);
316 					at_root = 1;
317 				} else
318 					at_root = 0;
319 				vfs_ref(mp);
320 				NFSVOPUNLOCK(vp);
321 				if (at_root != 0) {
322 					if ((nd->nd_repstat =
323 					     NFSVOPLOCK(tvp, LK_SHARED)) == 0) {
324 						nd->nd_repstat = VOP_GETATTR(
325 						    tvp, &va, nd->nd_cred);
326 						vput(tvp);
327 					} else
328 						vrele(tvp);
329 					if (nd->nd_repstat == 0)
330 						mounted_on_fileno = (uint64_t)
331 						    va.va_fileid;
332 					else
333 						at_root = 0;
334 				}
335 				if (nd->nd_repstat == 0)
336 					nd->nd_repstat = vfs_busy(mp, 0);
337 				vfs_rel(mp);
338 				if (nd->nd_repstat == 0) {
339 					(void)nfsvno_fillattr(nd, mp, vp, &nva,
340 					    &fh, 0, &attrbits, nd->nd_cred, p,
341 					    isdgram, 1, supports_nfsv4acls,
342 					    at_root, mounted_on_fileno);
343 					vfs_unbusy(mp);
344 				}
345 				vrele(vp);
346 			} else
347 				vput(vp);
348 		} else {
349 			nfsrv_fillattr(nd, &nva);
350 			vput(vp);
351 		}
352 	} else {
353 		vput(vp);
354 	}
355 
356 out:
357 	NFSEXITCODE2(error, nd);
358 	return (error);
359 }
360 
361 /*
362  * nfs setattr service
363  */
364 int
365 nfsrvd_setattr(struct nfsrv_descript *nd, __unused int isdgram,
366     vnode_t vp, struct nfsexstuff *exp)
367 {
368 	struct nfsvattr nva, nva2;
369 	u_int32_t *tl;
370 	int preat_ret = 1, postat_ret = 1, gcheck = 0, error = 0;
371 	int gotproxystateid;
372 	struct timespec guard = { 0, 0 };
373 	nfsattrbit_t attrbits, retbits;
374 	nfsv4stateid_t stateid;
375 	NFSACL_T *aclp = NULL;
376 	struct thread *p = curthread;
377 
378 	if (nd->nd_repstat) {
379 		nfsrv_wcc(nd, preat_ret, &nva2, postat_ret, &nva);
380 		goto out;
381 	}
382 #ifdef NFS4_ACL_EXTATTR_NAME
383 	aclp = acl_alloc(M_WAITOK);
384 	aclp->acl_cnt = 0;
385 #endif
386 	gotproxystateid = 0;
387 	NFSVNO_ATTRINIT(&nva);
388 	if (nd->nd_flag & ND_NFSV4) {
389 		NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID);
390 		stateid.seqid = fxdr_unsigned(u_int32_t, *tl++);
391 		stateid.other[0] = *tl++;
392 		stateid.other[1] = *tl++;
393 		stateid.other[2] = *tl;
394 		if (stateid.other[0] == 0x55555555 &&
395 		    stateid.other[1] == 0x55555555 &&
396 		    stateid.other[2] == 0x55555555 &&
397 		    stateid.seqid == 0xffffffff)
398 			gotproxystateid = 1;
399 	}
400 	error = nfsrv_sattr(nd, vp, &nva, &attrbits, aclp, p);
401 	if (error)
402 		goto nfsmout;
403 
404 	/* For NFSv4, only va_uid is used from nva2. */
405 	NFSZERO_ATTRBIT(&retbits);
406 	NFSSETBIT_ATTRBIT(&retbits, NFSATTRBIT_OWNER);
407 	preat_ret = nfsvno_getattr(vp, &nva2, nd, p, 1, &retbits);
408 	if (!nd->nd_repstat)
409 		nd->nd_repstat = preat_ret;
410 
411 	NFSZERO_ATTRBIT(&retbits);
412 	if (nd->nd_flag & ND_NFSV3) {
413 		NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
414 		gcheck = fxdr_unsigned(int, *tl);
415 		if (gcheck) {
416 			NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
417 			fxdr_nfsv3time(tl, &guard);
418 		}
419 		if (!nd->nd_repstat && gcheck &&
420 		    (nva2.na_ctime.tv_sec != guard.tv_sec ||
421 		     nva2.na_ctime.tv_nsec != guard.tv_nsec))
422 			nd->nd_repstat = NFSERR_NOT_SYNC;
423 		if (nd->nd_repstat) {
424 			vput(vp);
425 #ifdef NFS4_ACL_EXTATTR_NAME
426 			acl_free(aclp);
427 #endif
428 			nfsrv_wcc(nd, preat_ret, &nva2, postat_ret, &nva);
429 			goto out;
430 		}
431 	} else if (!nd->nd_repstat && (nd->nd_flag & ND_NFSV4))
432 		nd->nd_repstat = nfsrv_checkuidgid(nd, &nva);
433 
434 	/*
435 	 * Now that we have all the fields, lets do it.
436 	 * If the size is being changed write access is required, otherwise
437 	 * just check for a read only file system.
438 	 */
439 	if (!nd->nd_repstat) {
440 		if (NFSVNO_NOTSETSIZE(&nva)) {
441 			if (NFSVNO_EXRDONLY(exp) ||
442 			    (vp->v_mount->mnt_flag & MNT_RDONLY))
443 				nd->nd_repstat = EROFS;
444 		} else {
445 			if (vp->v_type != VREG)
446 				nd->nd_repstat = EINVAL;
447 			else if (nva2.na_uid != nd->nd_cred->cr_uid ||
448 			    NFSVNO_EXSTRICTACCESS(exp))
449 				nd->nd_repstat = nfsvno_accchk(vp,
450 				    VWRITE, nd->nd_cred, exp, p,
451 				    NFSACCCHK_NOOVERRIDE,
452 				    NFSACCCHK_VPISLOCKED, NULL);
453 		}
454 	}
455 	/*
456 	 * Proxy operations from the MDS are allowed via the all 0s special
457 	 * stateid.
458 	 */
459 	if (nd->nd_repstat == 0 && (nd->nd_flag & ND_NFSV4) != 0 &&
460 	    gotproxystateid == 0)
461 		nd->nd_repstat = nfsrv_checksetattr(vp, nd, &stateid,
462 		    &nva, &attrbits, exp, p);
463 
464 	if (!nd->nd_repstat && (nd->nd_flag & ND_NFSV4)) {
465 	    /*
466 	     * For V4, try setting the attributes in sets, so that the
467 	     * reply bitmap will be correct for an error case.
468 	     */
469 	    if (NFSISSET_ATTRBIT(&attrbits, NFSATTRBIT_OWNER) ||
470 		NFSISSET_ATTRBIT(&attrbits, NFSATTRBIT_OWNERGROUP)) {
471 		NFSVNO_ATTRINIT(&nva2);
472 		NFSVNO_SETATTRVAL(&nva2, uid, nva.na_uid);
473 		NFSVNO_SETATTRVAL(&nva2, gid, nva.na_gid);
474 		nd->nd_repstat = nfsvno_setattr(vp, &nva2, nd->nd_cred, p,
475 		    exp);
476 		if (!nd->nd_repstat) {
477 		    if (NFSISSET_ATTRBIT(&attrbits, NFSATTRBIT_OWNER))
478 			NFSSETBIT_ATTRBIT(&retbits, NFSATTRBIT_OWNER);
479 		    if (NFSISSET_ATTRBIT(&attrbits, NFSATTRBIT_OWNERGROUP))
480 			NFSSETBIT_ATTRBIT(&retbits, NFSATTRBIT_OWNERGROUP);
481 		}
482 	    }
483 	    if (!nd->nd_repstat &&
484 		NFSISSET_ATTRBIT(&attrbits, NFSATTRBIT_SIZE)) {
485 		NFSVNO_ATTRINIT(&nva2);
486 		NFSVNO_SETATTRVAL(&nva2, size, nva.na_size);
487 		nd->nd_repstat = nfsvno_setattr(vp, &nva2, nd->nd_cred, p,
488 		    exp);
489 		if (!nd->nd_repstat)
490 		    NFSSETBIT_ATTRBIT(&retbits, NFSATTRBIT_SIZE);
491 	    }
492 	    if (!nd->nd_repstat &&
493 		(NFSISSET_ATTRBIT(&attrbits, NFSATTRBIT_TIMEACCESSSET) ||
494 		 NFSISSET_ATTRBIT(&attrbits, NFSATTRBIT_TIMEMODIFYSET))) {
495 		NFSVNO_ATTRINIT(&nva2);
496 		NFSVNO_SETATTRVAL(&nva2, atime, nva.na_atime);
497 		NFSVNO_SETATTRVAL(&nva2, mtime, nva.na_mtime);
498 		if (nva.na_vaflags & VA_UTIMES_NULL) {
499 			nva2.na_vaflags |= VA_UTIMES_NULL;
500 			NFSVNO_SETACTIVE(&nva2, vaflags);
501 		}
502 		nd->nd_repstat = nfsvno_setattr(vp, &nva2, nd->nd_cred, p,
503 		    exp);
504 		if (!nd->nd_repstat) {
505 		    if (NFSISSET_ATTRBIT(&attrbits, NFSATTRBIT_TIMEACCESSSET))
506 			NFSSETBIT_ATTRBIT(&retbits, NFSATTRBIT_TIMEACCESSSET);
507 		    if (NFSISSET_ATTRBIT(&attrbits, NFSATTRBIT_TIMEMODIFYSET))
508 			NFSSETBIT_ATTRBIT(&retbits, NFSATTRBIT_TIMEMODIFYSET);
509 		}
510 	    }
511 	    if (!nd->nd_repstat &&
512 		NFSISSET_ATTRBIT(&attrbits, NFSATTRBIT_TIMECREATE)) {
513 		NFSVNO_ATTRINIT(&nva2);
514 		NFSVNO_SETATTRVAL(&nva2, btime, nva.na_btime);
515 		nd->nd_repstat = nfsvno_setattr(vp, &nva2, nd->nd_cred, p,
516 		    exp);
517 		if (!nd->nd_repstat)
518 		    NFSSETBIT_ATTRBIT(&retbits, NFSATTRBIT_TIMECREATE);
519 	    }
520 	    if (!nd->nd_repstat &&
521 		(NFSISSET_ATTRBIT(&attrbits, NFSATTRBIT_MODE) ||
522 		 NFSISSET_ATTRBIT(&attrbits, NFSATTRBIT_MODESETMASKED))) {
523 		NFSVNO_ATTRINIT(&nva2);
524 		NFSVNO_SETATTRVAL(&nva2, mode, nva.na_mode);
525 		nd->nd_repstat = nfsvno_setattr(vp, &nva2, nd->nd_cred, p,
526 		    exp);
527 		if (!nd->nd_repstat) {
528 		    if (NFSISSET_ATTRBIT(&attrbits, NFSATTRBIT_MODE))
529 			NFSSETBIT_ATTRBIT(&retbits, NFSATTRBIT_MODE);
530 		    if (NFSISSET_ATTRBIT(&attrbits, NFSATTRBIT_MODESETMASKED))
531 			NFSSETBIT_ATTRBIT(&retbits, NFSATTRBIT_MODESETMASKED);
532 		}
533 	    }
534 
535 #ifdef NFS4_ACL_EXTATTR_NAME
536 	    if (!nd->nd_repstat && aclp->acl_cnt > 0 &&
537 		NFSISSET_ATTRBIT(&attrbits, NFSATTRBIT_ACL)) {
538 		nd->nd_repstat = nfsrv_setacl(vp, aclp, nd->nd_cred, p);
539 		if (!nd->nd_repstat)
540 		    NFSSETBIT_ATTRBIT(&retbits, NFSATTRBIT_ACL);
541 	    }
542 #endif
543 	} else if (!nd->nd_repstat) {
544 		nd->nd_repstat = nfsvno_setattr(vp, &nva, nd->nd_cred, p,
545 		    exp);
546 	}
547 	if (nd->nd_flag & (ND_NFSV2 | ND_NFSV3)) {
548 		postat_ret = nfsvno_getattr(vp, &nva, nd, p, 1, NULL);
549 		if (!nd->nd_repstat)
550 			nd->nd_repstat = postat_ret;
551 	}
552 	vput(vp);
553 #ifdef NFS4_ACL_EXTATTR_NAME
554 	acl_free(aclp);
555 #endif
556 	if (nd->nd_flag & ND_NFSV3)
557 		nfsrv_wcc(nd, preat_ret, &nva2, postat_ret, &nva);
558 	else if (nd->nd_flag & ND_NFSV4)
559 		(void) nfsrv_putattrbit(nd, &retbits);
560 	else if (!nd->nd_repstat)
561 		nfsrv_fillattr(nd, &nva);
562 
563 out:
564 	NFSEXITCODE2(0, nd);
565 	return (0);
566 nfsmout:
567 	vput(vp);
568 #ifdef NFS4_ACL_EXTATTR_NAME
569 	acl_free(aclp);
570 #endif
571 	if (nd->nd_flag & ND_NFSV4) {
572 		/*
573 		 * For all nd_repstat, the V4 reply includes a bitmap,
574 		 * even NFSERR_BADXDR, which is what this will end up
575 		 * returning.
576 		 */
577 		(void) nfsrv_putattrbit(nd, &retbits);
578 	}
579 	NFSEXITCODE2(error, nd);
580 	return (error);
581 }
582 
583 /*
584  * nfs lookup rpc
585  * (Also performs lookup parent for v4)
586  */
587 int
588 nfsrvd_lookup(struct nfsrv_descript *nd, __unused int isdgram,
589     vnode_t dp, vnode_t *vpp, fhandle_t *fhp, struct nfsexstuff *exp)
590 {
591 	struct nameidata named;
592 	vnode_t vp, dirp = NULL;
593 	int error = 0, dattr_ret = 1;
594 	struct nfsvattr nva, dattr;
595 	char *bufp;
596 	u_long *hashp;
597 	struct thread *p = curthread;
598 
599 	if (nd->nd_repstat) {
600 		nfsrv_postopattr(nd, dattr_ret, &dattr);
601 		goto out;
602 	}
603 
604 	/*
605 	 * For some reason, if dp is a symlink, the error
606 	 * returned is supposed to be NFSERR_SYMLINK and not NFSERR_NOTDIR.
607 	 */
608 	if (dp->v_type == VLNK && (nd->nd_flag & ND_NFSV4)) {
609 		nd->nd_repstat = NFSERR_SYMLINK;
610 		vrele(dp);
611 		goto out;
612 	}
613 
614 	NFSNAMEICNDSET(&named.ni_cnd, nd->nd_cred, LOOKUP,
615 	    LOCKLEAF);
616 	nfsvno_setpathbuf(&named, &bufp, &hashp);
617 	error = nfsrv_parsename(nd, bufp, hashp, &named.ni_pathlen);
618 	if (error) {
619 		vrele(dp);
620 		nfsvno_relpathbuf(&named);
621 		goto out;
622 	}
623 	if (!nd->nd_repstat) {
624 		nd->nd_repstat = nfsvno_namei(nd, &named, dp, 0, exp, &dirp);
625 	} else {
626 		vrele(dp);
627 		nfsvno_relpathbuf(&named);
628 	}
629 	if (nd->nd_repstat) {
630 		if (dirp) {
631 			if (nd->nd_flag & ND_NFSV3)
632 				dattr_ret = nfsvno_getattr(dirp, &dattr, nd, p,
633 				    0, NULL);
634 			vrele(dirp);
635 		}
636 		if (nd->nd_flag & ND_NFSV3)
637 			nfsrv_postopattr(nd, dattr_ret, &dattr);
638 		goto out;
639 	}
640 	nfsvno_relpathbuf(&named);
641 	vp = named.ni_vp;
642 	if ((nd->nd_flag & ND_NFSV4) != 0 && !NFSVNO_EXPORTED(exp) &&
643 	    vp->v_type != VDIR && vp->v_type != VLNK)
644 		/*
645 		 * Only allow lookup of VDIR and VLNK for traversal of
646 		 * non-exported volumes during NFSv4 mounting.
647 		 */
648 		nd->nd_repstat = ENOENT;
649 	if (nd->nd_repstat == 0) {
650 		nd->nd_repstat = nfsvno_getfh(vp, fhp, p);
651 		/*
652 		 * EOPNOTSUPP indicates the file system cannot be exported,
653 		 * so just pretend the entry does not exist.
654 		 */
655 		if (nd->nd_repstat == EOPNOTSUPP)
656 			nd->nd_repstat = ENOENT;
657 	}
658 	if (!(nd->nd_flag & ND_NFSV4) && !nd->nd_repstat)
659 		nd->nd_repstat = nfsvno_getattr(vp, &nva, nd, p, 1, NULL);
660 	if (vpp != NULL && nd->nd_repstat == 0)
661 		*vpp = vp;
662 	else
663 		vput(vp);
664 	if (dirp) {
665 		if (nd->nd_flag & ND_NFSV3)
666 			dattr_ret = nfsvno_getattr(dirp, &dattr, nd, p, 0,
667 			    NULL);
668 		vrele(dirp);
669 	}
670 	if (nd->nd_repstat) {
671 		if (nd->nd_flag & ND_NFSV3)
672 			nfsrv_postopattr(nd, dattr_ret, &dattr);
673 		goto out;
674 	}
675 	if (nd->nd_flag & ND_NFSV2) {
676 		(void)nfsm_fhtom(NULL, nd, (u_int8_t *)fhp, 0, 0);
677 		nfsrv_fillattr(nd, &nva);
678 	} else if (nd->nd_flag & ND_NFSV3) {
679 		(void)nfsm_fhtom(NULL, nd, (u_int8_t *)fhp, 0, 0);
680 		nfsrv_postopattr(nd, 0, &nva);
681 		nfsrv_postopattr(nd, dattr_ret, &dattr);
682 	}
683 
684 out:
685 	NFSEXITCODE2(error, nd);
686 	return (error);
687 }
688 
689 /*
690  * nfs readlink service
691  */
692 int
693 nfsrvd_readlink(struct nfsrv_descript *nd, __unused int isdgram,
694     vnode_t vp, __unused struct nfsexstuff *exp)
695 {
696 	u_int32_t *tl;
697 	struct mbuf *mp = NULL, *mpend = NULL;
698 	int getret = 1, len;
699 	struct nfsvattr nva;
700 	struct thread *p = curthread;
701 	uint16_t off;
702 
703 	if (nd->nd_repstat) {
704 		nfsrv_postopattr(nd, getret, &nva);
705 		goto out;
706 	}
707 	if (vp->v_type != VLNK) {
708 		if (nd->nd_flag & ND_NFSV2)
709 			nd->nd_repstat = ENXIO;
710 		else
711 			nd->nd_repstat = EINVAL;
712 	}
713 	if (nd->nd_repstat == 0) {
714 		if ((nd->nd_flag & ND_EXTPG) != 0)
715 			nd->nd_repstat = nfsvno_readlink(vp, nd->nd_cred,
716 			    nd->nd_maxextsiz, p, &mp, &mpend, &len);
717 		else
718 			nd->nd_repstat = nfsvno_readlink(vp, nd->nd_cred,
719 			    0, p, &mp, &mpend, &len);
720 	}
721 	if (nd->nd_flag & ND_NFSV3)
722 		getret = nfsvno_getattr(vp, &nva, nd, p, 1, NULL);
723 	vput(vp);
724 	if (nd->nd_flag & ND_NFSV3)
725 		nfsrv_postopattr(nd, getret, &nva);
726 	if (nd->nd_repstat)
727 		goto out;
728 	NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
729 	*tl = txdr_unsigned(len);
730 	if (mp != NULL) {
731 		nd->nd_mb->m_next = mp;
732 		nd->nd_mb = mpend;
733 		if ((mpend->m_flags & M_EXTPG) != 0) {
734 			nd->nd_bextpg = mpend->m_epg_npgs - 1;
735 			nd->nd_bpos = (char *)(void *)
736 			    PHYS_TO_DMAP(mpend->m_epg_pa[nd->nd_bextpg]);
737 			off = (nd->nd_bextpg == 0) ? mpend->m_epg_1st_off : 0;
738 			nd->nd_bpos += off + mpend->m_epg_last_len;
739 			nd->nd_bextpgsiz = PAGE_SIZE - mpend->m_epg_last_len -
740 			    off;
741 		} else
742 			nd->nd_bpos = mtod(mpend, char *) + mpend->m_len;
743 	}
744 
745 out:
746 	NFSEXITCODE2(0, nd);
747 	return (0);
748 }
749 
750 /*
751  * nfs read service
752  */
753 int
754 nfsrvd_read(struct nfsrv_descript *nd, __unused int isdgram,
755     vnode_t vp, struct nfsexstuff *exp)
756 {
757 	u_int32_t *tl;
758 	int error = 0, cnt, getret = 1, gotproxystateid, reqlen, eof = 0;
759 	struct mbuf *m2, *m3;
760 	struct nfsvattr nva;
761 	off_t off = 0x0;
762 	struct nfsstate st, *stp = &st;
763 	struct nfslock lo, *lop = &lo;
764 	nfsv4stateid_t stateid;
765 	nfsquad_t clientid;
766 	struct thread *p = curthread;
767 	uint16_t poff;
768 
769 	if (nd->nd_repstat) {
770 		nfsrv_postopattr(nd, getret, &nva);
771 		goto out;
772 	}
773 	if (nd->nd_flag & ND_NFSV2) {
774 		NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
775 		off = (off_t)fxdr_unsigned(u_int32_t, *tl++);
776 		reqlen = fxdr_unsigned(int, *tl);
777 	} else if (nd->nd_flag & ND_NFSV3) {
778 		NFSM_DISSECT(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
779 		off = fxdr_hyper(tl);
780 		tl += 2;
781 		reqlen = fxdr_unsigned(int, *tl);
782 	} else {
783 		NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID + 3*NFSX_UNSIGNED);
784 		reqlen = fxdr_unsigned(int, *(tl + 6));
785 	}
786 	if (reqlen > NFS_SRVMAXDATA(nd)) {
787 		reqlen = NFS_SRVMAXDATA(nd);
788 	} else if (reqlen < 0) {
789 		error = EBADRPC;
790 		goto nfsmout;
791 	}
792 	gotproxystateid = 0;
793 	if (nd->nd_flag & ND_NFSV4) {
794 		stp->ls_flags = (NFSLCK_CHECK | NFSLCK_READACCESS);
795 		lop->lo_flags = NFSLCK_READ;
796 		stp->ls_ownerlen = 0;
797 		stp->ls_op = NULL;
798 		stp->ls_uid = nd->nd_cred->cr_uid;
799 		stp->ls_stateid.seqid = fxdr_unsigned(u_int32_t, *tl++);
800 		clientid.lval[0] = stp->ls_stateid.other[0] = *tl++;
801 		clientid.lval[1] = stp->ls_stateid.other[1] = *tl++;
802 		if ((nd->nd_flag & ND_IMPLIEDCLID) != 0) {
803 			if ((nd->nd_flag & ND_NFSV41) != 0)
804 				clientid.qval = nd->nd_clientid.qval;
805 			else if (nd->nd_clientid.qval != clientid.qval)
806 				printf("EEK1 multiple clids\n");
807 		} else {
808 			if ((nd->nd_flag & ND_NFSV41) != 0)
809 				printf("EEK! no clientid from session\n");
810 			nd->nd_flag |= ND_IMPLIEDCLID;
811 			nd->nd_clientid.qval = clientid.qval;
812 		}
813 		stp->ls_stateid.other[2] = *tl++;
814 		/*
815 		 * Don't allow the client to use a special stateid for a DS op.
816 		 */
817 		if ((nd->nd_flag & ND_DSSERVER) != 0 &&
818 		    ((stp->ls_stateid.other[0] == 0x0 &&
819 		    stp->ls_stateid.other[1] == 0x0 &&
820 		    stp->ls_stateid.other[2] == 0x0) ||
821 		    (stp->ls_stateid.other[0] == 0xffffffff &&
822 		    stp->ls_stateid.other[1] == 0xffffffff &&
823 		    stp->ls_stateid.other[2] == 0xffffffff) ||
824 		    stp->ls_stateid.seqid != 0))
825 			nd->nd_repstat = NFSERR_BADSTATEID;
826 		/* However, allow the proxy stateid. */
827 		if (stp->ls_stateid.seqid == 0xffffffff &&
828 		    stp->ls_stateid.other[0] == 0x55555555 &&
829 		    stp->ls_stateid.other[1] == 0x55555555 &&
830 		    stp->ls_stateid.other[2] == 0x55555555)
831 			gotproxystateid = 1;
832 		off = fxdr_hyper(tl);
833 		lop->lo_first = off;
834 		tl += 2;
835 		lop->lo_end = off + reqlen;
836 		/*
837 		 * Paranoia, just in case it wraps around.
838 		 */
839 		if (lop->lo_end < off)
840 			lop->lo_end = NFS64BITSSET;
841 	}
842 	if (vp->v_type != VREG) {
843 		if (nd->nd_flag & ND_NFSV3)
844 			nd->nd_repstat = EINVAL;
845 		else
846 			nd->nd_repstat = (vp->v_type == VDIR) ? EISDIR :
847 			    EINVAL;
848 	}
849 	getret = nfsvno_getattr(vp, &nva, nd, p, 1, NULL);
850 	if (!nd->nd_repstat)
851 		nd->nd_repstat = getret;
852 	if (!nd->nd_repstat &&
853 	    (nva.na_uid != nd->nd_cred->cr_uid ||
854 	     NFSVNO_EXSTRICTACCESS(exp))) {
855 		nd->nd_repstat = nfsvno_accchk(vp, VREAD,
856 		    nd->nd_cred, exp, p,
857 		    NFSACCCHK_ALLOWOWNER, NFSACCCHK_VPISLOCKED, NULL);
858 		if (nd->nd_repstat)
859 			nd->nd_repstat = nfsvno_accchk(vp, VEXEC,
860 			    nd->nd_cred, exp, p, NFSACCCHK_ALLOWOWNER,
861 			    NFSACCCHK_VPISLOCKED, NULL);
862 	}
863 	/*
864 	 * DS reads are marked by ND_DSSERVER or use the proxy special
865 	 * stateid.
866 	 */
867 	if (nd->nd_repstat == 0 && (nd->nd_flag & (ND_NFSV4 | ND_DSSERVER)) ==
868 	    ND_NFSV4 && gotproxystateid == 0)
869 		nd->nd_repstat = nfsrv_lockctrl(vp, &stp, &lop, NULL, clientid,
870 		    &stateid, exp, nd, p);
871 	if (nd->nd_repstat) {
872 		vput(vp);
873 		if (nd->nd_flag & ND_NFSV3)
874 			nfsrv_postopattr(nd, getret, &nva);
875 		goto out;
876 	}
877 	if (off >= nva.na_size) {
878 		cnt = 0;
879 		eof = 1;
880 	} else if (reqlen == 0)
881 		cnt = 0;
882 	else if ((off + reqlen) >= nva.na_size) {
883 		cnt = nva.na_size - off;
884 		eof = 1;
885 	} else
886 		cnt = reqlen;
887 	m3 = NULL;
888 	if (cnt > 0) {
889 		/*
890 		 * If cnt > MCLBYTES and the reply will not be saved, use
891 		 * ext_pgs mbufs for TLS.
892 		 * For NFSv4.0, we do not know for sure if the reply will
893 		 * be saved, so do not use ext_pgs mbufs for NFSv4.0.
894 		 * Always use ext_pgs mbufs if ND_EXTPG is set.
895 		 */
896 		if ((nd->nd_flag & ND_EXTPG) != 0 || (cnt > MCLBYTES &&
897 		    (nd->nd_flag & (ND_TLS | ND_SAVEREPLY)) == ND_TLS &&
898 		    (nd->nd_flag & (ND_NFSV4 | ND_NFSV41)) != ND_NFSV4))
899 			nd->nd_repstat = nfsvno_read(vp, off, cnt, nd->nd_cred,
900 			    nd->nd_maxextsiz, p, &m3, &m2);
901 		else
902 			nd->nd_repstat = nfsvno_read(vp, off, cnt, nd->nd_cred,
903 			    0, p, &m3, &m2);
904 		if (!(nd->nd_flag & ND_NFSV4)) {
905 			getret = nfsvno_getattr(vp, &nva, nd, p, 1, NULL);
906 			if (!nd->nd_repstat)
907 				nd->nd_repstat = getret;
908 		}
909 		if (nd->nd_repstat) {
910 			vput(vp);
911 			if (m3)
912 				m_freem(m3);
913 			if (nd->nd_flag & ND_NFSV3)
914 				nfsrv_postopattr(nd, getret, &nva);
915 			goto out;
916 		}
917 	}
918 	vput(vp);
919 	if (nd->nd_flag & ND_NFSV2) {
920 		nfsrv_fillattr(nd, &nva);
921 		NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
922 	} else {
923 		if (nd->nd_flag & ND_NFSV3) {
924 			nfsrv_postopattr(nd, getret, &nva);
925 			NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
926 			*tl++ = txdr_unsigned(cnt);
927 		} else
928 			NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
929 		if (eof)
930 			*tl++ = newnfs_true;
931 		else
932 			*tl++ = newnfs_false;
933 	}
934 	*tl = txdr_unsigned(cnt);
935 	if (m3) {
936 		nd->nd_mb->m_next = m3;
937 		nd->nd_mb = m2;
938 		if ((m2->m_flags & M_EXTPG) != 0) {
939 			nd->nd_flag |= ND_EXTPG;
940 			nd->nd_bextpg = m2->m_epg_npgs - 1;
941 			nd->nd_bpos = (char *)(void *)
942 			    PHYS_TO_DMAP(m2->m_epg_pa[nd->nd_bextpg]);
943 			poff = (nd->nd_bextpg == 0) ? m2->m_epg_1st_off : 0;
944 			nd->nd_bpos += poff + m2->m_epg_last_len;
945 			nd->nd_bextpgsiz = PAGE_SIZE - m2->m_epg_last_len -
946 			    poff;
947 		} else
948 			nd->nd_bpos = mtod(m2, char *) + m2->m_len;
949 	}
950 
951 out:
952 	NFSEXITCODE2(0, nd);
953 	return (0);
954 nfsmout:
955 	vput(vp);
956 	NFSEXITCODE2(error, nd);
957 	return (error);
958 }
959 
960 /*
961  * nfs write service
962  */
963 int
964 nfsrvd_write(struct nfsrv_descript *nd, __unused int isdgram,
965     vnode_t vp, struct nfsexstuff *exp)
966 {
967 	u_int32_t *tl;
968 	struct nfsvattr nva, forat;
969 	int aftat_ret = 1, retlen, len, error = 0, forat_ret = 1;
970 	int gotproxystateid, stable = NFSWRITE_FILESYNC;
971 	off_t off;
972 	struct nfsstate st, *stp = &st;
973 	struct nfslock lo, *lop = &lo;
974 	nfsv4stateid_t stateid;
975 	nfsquad_t clientid;
976 	nfsattrbit_t attrbits;
977 	struct thread *p = curthread;
978 
979 	if (nd->nd_repstat) {
980 		nfsrv_wcc(nd, forat_ret, &forat, aftat_ret, &nva);
981 		goto out;
982 	}
983 	gotproxystateid = 0;
984 	if (nd->nd_flag & ND_NFSV2) {
985 		NFSM_DISSECT(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
986 		off = (off_t)fxdr_unsigned(u_int32_t, *++tl);
987 		tl += 2;
988 		retlen = len = fxdr_unsigned(int32_t, *tl);
989 	} else if (nd->nd_flag & ND_NFSV3) {
990 		NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
991 		off = fxdr_hyper(tl);
992 		tl += 3;
993 		stable = fxdr_unsigned(int, *tl++);
994 		retlen = len = fxdr_unsigned(int32_t, *tl);
995 	} else {
996 		NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID + 4 * NFSX_UNSIGNED);
997 		stp->ls_flags = (NFSLCK_CHECK | NFSLCK_WRITEACCESS);
998 		lop->lo_flags = NFSLCK_WRITE;
999 		stp->ls_ownerlen = 0;
1000 		stp->ls_op = NULL;
1001 		stp->ls_uid = nd->nd_cred->cr_uid;
1002 		stp->ls_stateid.seqid = fxdr_unsigned(u_int32_t, *tl++);
1003 		clientid.lval[0] = stp->ls_stateid.other[0] = *tl++;
1004 		clientid.lval[1] = stp->ls_stateid.other[1] = *tl++;
1005 		if ((nd->nd_flag & ND_IMPLIEDCLID) != 0) {
1006 			if ((nd->nd_flag & ND_NFSV41) != 0)
1007 				clientid.qval = nd->nd_clientid.qval;
1008 			else if (nd->nd_clientid.qval != clientid.qval)
1009 				printf("EEK2 multiple clids\n");
1010 		} else {
1011 			if ((nd->nd_flag & ND_NFSV41) != 0)
1012 				printf("EEK! no clientid from session\n");
1013 			nd->nd_flag |= ND_IMPLIEDCLID;
1014 			nd->nd_clientid.qval = clientid.qval;
1015 		}
1016 		stp->ls_stateid.other[2] = *tl++;
1017 		/*
1018 		 * Don't allow the client to use a special stateid for a DS op.
1019 		 */
1020 		if ((nd->nd_flag & ND_DSSERVER) != 0 &&
1021 		    ((stp->ls_stateid.other[0] == 0x0 &&
1022 		    stp->ls_stateid.other[1] == 0x0 &&
1023 		    stp->ls_stateid.other[2] == 0x0) ||
1024 		    (stp->ls_stateid.other[0] == 0xffffffff &&
1025 		    stp->ls_stateid.other[1] == 0xffffffff &&
1026 		    stp->ls_stateid.other[2] == 0xffffffff) ||
1027 		    stp->ls_stateid.seqid != 0))
1028 			nd->nd_repstat = NFSERR_BADSTATEID;
1029 		/* However, allow the proxy stateid. */
1030 		if (stp->ls_stateid.seqid == 0xffffffff &&
1031 		    stp->ls_stateid.other[0] == 0x55555555 &&
1032 		    stp->ls_stateid.other[1] == 0x55555555 &&
1033 		    stp->ls_stateid.other[2] == 0x55555555)
1034 			gotproxystateid = 1;
1035 		off = fxdr_hyper(tl);
1036 		lop->lo_first = off;
1037 		tl += 2;
1038 		stable = fxdr_unsigned(int, *tl++);
1039 		retlen = len = fxdr_unsigned(int32_t, *tl);
1040 		lop->lo_end = off + len;
1041 		/*
1042 		 * Paranoia, just in case it wraps around, which shouldn't
1043 		 * ever happen anyhow.
1044 		 */
1045 		if (lop->lo_end < lop->lo_first)
1046 			lop->lo_end = NFS64BITSSET;
1047 	}
1048 
1049 	if (retlen > nfs_srvmaxio || retlen < 0)
1050 		nd->nd_repstat = EIO;
1051 	if (vp->v_type != VREG && !nd->nd_repstat) {
1052 		if (nd->nd_flag & ND_NFSV3)
1053 			nd->nd_repstat = EINVAL;
1054 		else
1055 			nd->nd_repstat = (vp->v_type == VDIR) ? EISDIR :
1056 			    EINVAL;
1057 	}
1058 	NFSZERO_ATTRBIT(&attrbits);
1059 	NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_OWNER);
1060 	forat_ret = nfsvno_getattr(vp, &forat, nd, p, 1, &attrbits);
1061 	if (!nd->nd_repstat)
1062 		nd->nd_repstat = forat_ret;
1063 	if (!nd->nd_repstat &&
1064 	    (forat.na_uid != nd->nd_cred->cr_uid ||
1065 	     NFSVNO_EXSTRICTACCESS(exp)))
1066 		nd->nd_repstat = nfsvno_accchk(vp, VWRITE,
1067 		    nd->nd_cred, exp, p,
1068 		    NFSACCCHK_ALLOWOWNER, NFSACCCHK_VPISLOCKED, NULL);
1069 	/*
1070 	 * DS reads are marked by ND_DSSERVER or use the proxy special
1071 	 * stateid.
1072 	 */
1073 	if (nd->nd_repstat == 0 && (nd->nd_flag & (ND_NFSV4 | ND_DSSERVER)) ==
1074 	    ND_NFSV4 && gotproxystateid == 0)
1075 		nd->nd_repstat = nfsrv_lockctrl(vp, &stp, &lop, NULL, clientid,
1076 		    &stateid, exp, nd, p);
1077 	if (nd->nd_repstat) {
1078 		vput(vp);
1079 		if (nd->nd_flag & ND_NFSV3)
1080 			nfsrv_wcc(nd, forat_ret, &forat, aftat_ret, &nva);
1081 		goto out;
1082 	}
1083 
1084 	/*
1085 	 * For NFS Version 2, it is not obvious what a write of zero length
1086 	 * should do, but I might as well be consistent with Version 3,
1087 	 * which is to return ok so long as there are no permission problems.
1088 	 */
1089 	if (retlen > 0) {
1090 		nd->nd_repstat = nfsvno_write(vp, off, retlen, &stable,
1091 		    nd->nd_md, nd->nd_dpos, nd->nd_cred, p);
1092 		error = nfsm_advance(nd, NFSM_RNDUP(retlen), -1);
1093 		if (error)
1094 			goto nfsmout;
1095 	}
1096 	if (nd->nd_flag & ND_NFSV4)
1097 		aftat_ret = 0;
1098 	else
1099 		aftat_ret = nfsvno_getattr(vp, &nva, nd, p, 1, NULL);
1100 	vput(vp);
1101 	if (!nd->nd_repstat)
1102 		nd->nd_repstat = aftat_ret;
1103 	if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) {
1104 		if (nd->nd_flag & ND_NFSV3)
1105 			nfsrv_wcc(nd, forat_ret, &forat, aftat_ret, &nva);
1106 		if (nd->nd_repstat)
1107 			goto out;
1108 		NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1109 		*tl++ = txdr_unsigned(retlen);
1110 		/*
1111 		 * If nfs_async is set, then pretend the write was FILESYNC.
1112 		 * Warning: Doing this violates RFC1813 and runs a risk
1113 		 * of data written by a client being lost when the server
1114 		 * crashes/reboots.
1115 		 */
1116 		if (stable == NFSWRITE_UNSTABLE && nfs_async == 0)
1117 			*tl++ = txdr_unsigned(stable);
1118 		else
1119 			*tl++ = txdr_unsigned(NFSWRITE_FILESYNC);
1120 		/*
1121 		 * Actually, there is no need to txdr these fields,
1122 		 * but it may make the values more human readable,
1123 		 * for debugging purposes.
1124 		 */
1125 		*tl++ = txdr_unsigned(nfsboottime.tv_sec);
1126 		*tl = txdr_unsigned(nfsboottime.tv_usec);
1127 	} else if (!nd->nd_repstat)
1128 		nfsrv_fillattr(nd, &nva);
1129 
1130 out:
1131 	NFSEXITCODE2(0, nd);
1132 	return (0);
1133 nfsmout:
1134 	vput(vp);
1135 	NFSEXITCODE2(error, nd);
1136 	return (error);
1137 }
1138 
1139 /*
1140  * nfs create service (creates regular files for V2 and V3. Spec. files for V2.)
1141  * now does a truncate to 0 length via. setattr if it already exists
1142  * The core creation routine has been extracted out into nfsrv_creatsub(),
1143  * so it can also be used by nfsrv_open() for V4.
1144  */
1145 int
1146 nfsrvd_create(struct nfsrv_descript *nd, __unused int isdgram,
1147     vnode_t dp, struct nfsexstuff *exp)
1148 {
1149 	struct nfsvattr nva, dirfor, diraft;
1150 	struct nfsv2_sattr *sp;
1151 	struct nameidata named;
1152 	u_int32_t *tl;
1153 	int error = 0, tsize, dirfor_ret = 1, diraft_ret = 1;
1154 	int how = NFSCREATE_UNCHECKED, exclusive_flag = 0;
1155 	NFSDEV_T rdev = 0;
1156 	vnode_t vp = NULL, dirp = NULL;
1157 	fhandle_t fh;
1158 	char *bufp;
1159 	u_long *hashp;
1160 	__enum_uint8(vtype) vtyp;
1161 	int32_t cverf[2], tverf[2] = { 0, 0 };
1162 	struct thread *p = curthread;
1163 
1164 	if (nd->nd_repstat) {
1165 		nfsrv_wcc(nd, dirfor_ret, &dirfor, diraft_ret, &diraft);
1166 		goto out;
1167 	}
1168 	NFSNAMEICNDSET(&named.ni_cnd, nd->nd_cred, CREATE,
1169 	    LOCKPARENT | LOCKLEAF | NOCACHE);
1170 	nfsvno_setpathbuf(&named, &bufp, &hashp);
1171 	error = nfsrv_parsename(nd, bufp, hashp, &named.ni_pathlen);
1172 	if (error)
1173 		goto nfsmout;
1174 	if (!nd->nd_repstat) {
1175 		NFSVNO_ATTRINIT(&nva);
1176 		if (nd->nd_flag & ND_NFSV2) {
1177 			NFSM_DISSECT(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1178 			vtyp = IFTOVT(fxdr_unsigned(u_int32_t, sp->sa_mode));
1179 			if (vtyp == VNON)
1180 				vtyp = VREG;
1181 			NFSVNO_SETATTRVAL(&nva, type, vtyp);
1182 			NFSVNO_SETATTRVAL(&nva, mode,
1183 			    nfstov_mode(sp->sa_mode));
1184 			switch (nva.na_type) {
1185 			case VREG:
1186 				tsize = fxdr_unsigned(int32_t, sp->sa_size);
1187 				if (tsize != -1)
1188 					NFSVNO_SETATTRVAL(&nva, size,
1189 					    (u_quad_t)tsize);
1190 				break;
1191 			case VCHR:
1192 			case VBLK:
1193 			case VFIFO:
1194 				rdev = fxdr_unsigned(NFSDEV_T, sp->sa_size);
1195 				break;
1196 			default:
1197 				break;
1198 			}
1199 		} else {
1200 			NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
1201 			how = fxdr_unsigned(int, *tl);
1202 			switch (how) {
1203 			case NFSCREATE_GUARDED:
1204 			case NFSCREATE_UNCHECKED:
1205 				error = nfsrv_sattr(nd, NULL, &nva, NULL, NULL, p);
1206 				if (error)
1207 					goto nfsmout;
1208 				break;
1209 			case NFSCREATE_EXCLUSIVE:
1210 				NFSM_DISSECT(tl, u_int32_t *, NFSX_VERF);
1211 				cverf[0] = *tl++;
1212 				cverf[1] = *tl;
1213 				exclusive_flag = 1;
1214 				break;
1215 			}
1216 			NFSVNO_SETATTRVAL(&nva, type, VREG);
1217 		}
1218 	}
1219 	if (nd->nd_repstat) {
1220 		nfsvno_relpathbuf(&named);
1221 		if (nd->nd_flag & ND_NFSV3) {
1222 			dirfor_ret = nfsvno_getattr(dp, &dirfor, nd, p, 1,
1223 			    NULL);
1224 			nfsrv_wcc(nd, dirfor_ret, &dirfor, diraft_ret,
1225 			    &diraft);
1226 		}
1227 		vput(dp);
1228 		goto out;
1229 	}
1230 
1231 	nd->nd_repstat = nfsvno_namei(nd, &named, dp, 1, exp, &dirp);
1232 	if (dirp) {
1233 		if (nd->nd_flag & ND_NFSV2) {
1234 			vrele(dirp);
1235 			dirp = NULL;
1236 		} else {
1237 			dirfor_ret = nfsvno_getattr(dirp, &dirfor, nd, p, 0,
1238 			    NULL);
1239 		}
1240 	}
1241 	if (nd->nd_repstat) {
1242 		if (nd->nd_flag & ND_NFSV3)
1243 			nfsrv_wcc(nd, dirfor_ret, &dirfor, diraft_ret,
1244 			    &diraft);
1245 		if (dirp)
1246 			vrele(dirp);
1247 		goto out;
1248 	}
1249 
1250 	if (!(nd->nd_flag & ND_NFSV2)) {
1251 		switch (how) {
1252 		case NFSCREATE_GUARDED:
1253 			if (named.ni_vp)
1254 				nd->nd_repstat = EEXIST;
1255 			break;
1256 		case NFSCREATE_UNCHECKED:
1257 			break;
1258 		case NFSCREATE_EXCLUSIVE:
1259 			if (named.ni_vp == NULL)
1260 				NFSVNO_SETATTRVAL(&nva, mode, 0);
1261 			break;
1262 		}
1263 	}
1264 
1265 	/*
1266 	 * Iff doesn't exist, create it
1267 	 * otherwise just truncate to 0 length
1268 	 *   should I set the mode too ?
1269 	 */
1270 	nd->nd_repstat = nfsvno_createsub(nd, &named, &vp, &nva,
1271 	    &exclusive_flag, cverf, rdev, exp);
1272 
1273 	if (!nd->nd_repstat) {
1274 		nd->nd_repstat = nfsvno_getfh(vp, &fh, p);
1275 		if (!nd->nd_repstat)
1276 			nd->nd_repstat = nfsvno_getattr(vp, &nva, nd, p, 1,
1277 			    NULL);
1278 		vput(vp);
1279 		if (!nd->nd_repstat) {
1280 			tverf[0] = nva.na_atime.tv_sec;
1281 			tverf[1] = nva.na_atime.tv_nsec;
1282 		}
1283 	}
1284 	if (nd->nd_flag & ND_NFSV2) {
1285 		if (!nd->nd_repstat) {
1286 			(void)nfsm_fhtom(NULL, nd, (u_int8_t *)&fh, 0, 0);
1287 			nfsrv_fillattr(nd, &nva);
1288 		}
1289 	} else {
1290 		if (exclusive_flag && !nd->nd_repstat && (cverf[0] != tverf[0]
1291 		    || cverf[1] != tverf[1]))
1292 			nd->nd_repstat = EEXIST;
1293 		diraft_ret = nfsvno_getattr(dirp, &diraft, nd, p, 0, NULL);
1294 		vrele(dirp);
1295 		if (!nd->nd_repstat) {
1296 			(void)nfsm_fhtom(NULL, nd, (u_int8_t *)&fh, 0, 1);
1297 			nfsrv_postopattr(nd, 0, &nva);
1298 		}
1299 		nfsrv_wcc(nd, dirfor_ret, &dirfor, diraft_ret, &diraft);
1300 	}
1301 
1302 out:
1303 	NFSEXITCODE2(0, nd);
1304 	return (0);
1305 nfsmout:
1306 	vput(dp);
1307 	nfsvno_relpathbuf(&named);
1308 	NFSEXITCODE2(error, nd);
1309 	return (error);
1310 }
1311 
1312 /*
1313  * nfs v3 mknod service (and v4 create)
1314  */
1315 int
1316 nfsrvd_mknod(struct nfsrv_descript *nd, __unused int isdgram,
1317     vnode_t dp, vnode_t *vpp, fhandle_t *fhp, struct nfsexstuff *exp)
1318 {
1319 	struct nfsvattr nva, dirfor, diraft;
1320 	u_int32_t *tl;
1321 	struct nameidata named;
1322 	int error = 0, dirfor_ret = 1, diraft_ret = 1, pathlen;
1323 	u_int32_t major, minor;
1324 	__enum_uint8(vtype) vtyp = VNON;
1325 	nfstype nfs4type = NFNON;
1326 	vnode_t vp, dirp = NULL;
1327 	nfsattrbit_t attrbits;
1328 	char *bufp = NULL, *pathcp = NULL;
1329 	u_long *hashp, cnflags;
1330 	NFSACL_T *aclp = NULL;
1331 	struct thread *p = curthread;
1332 
1333 	NFSVNO_ATTRINIT(&nva);
1334 	cnflags = LOCKPARENT;
1335 	if (nd->nd_repstat) {
1336 		nfsrv_wcc(nd, dirfor_ret, &dirfor, diraft_ret, &diraft);
1337 		goto out;
1338 	}
1339 #ifdef NFS4_ACL_EXTATTR_NAME
1340 	aclp = acl_alloc(M_WAITOK);
1341 	aclp->acl_cnt = 0;
1342 #endif
1343 
1344 	/*
1345 	 * For V4, the creation stuff is here, Yuck!
1346 	 */
1347 	if (nd->nd_flag & ND_NFSV4) {
1348 		NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
1349 		vtyp = nfsv34tov_type(*tl);
1350 		nfs4type = fxdr_unsigned(nfstype, *tl);
1351 		switch (nfs4type) {
1352 		case NFLNK:
1353 			error = nfsvno_getsymlink(nd, &nva, p, &pathcp,
1354 			    &pathlen);
1355 			if (error)
1356 				goto nfsmout;
1357 			break;
1358 		case NFCHR:
1359 		case NFBLK:
1360 			NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1361 			major = fxdr_unsigned(u_int32_t, *tl++);
1362 			minor = fxdr_unsigned(u_int32_t, *tl);
1363 			nva.na_rdev = NFSMAKEDEV(major, minor);
1364 			break;
1365 		case NFSOCK:
1366 		case NFFIFO:
1367 			break;
1368 		case NFDIR:
1369 			cnflags = LOCKPARENT;
1370 			break;
1371 		default:
1372 			nd->nd_repstat = NFSERR_BADTYPE;
1373 			vrele(dp);
1374 #ifdef NFS4_ACL_EXTATTR_NAME
1375 			acl_free(aclp);
1376 #endif
1377 			goto out;
1378 		}
1379 	}
1380 	NFSNAMEICNDSET(&named.ni_cnd, nd->nd_cred, CREATE, cnflags | NOCACHE);
1381 	nfsvno_setpathbuf(&named, &bufp, &hashp);
1382 	error = nfsrv_parsename(nd, bufp, hashp, &named.ni_pathlen);
1383 	if (error)
1384 		goto nfsmout;
1385 	if (!nd->nd_repstat) {
1386 		if (nd->nd_flag & ND_NFSV3) {
1387 			NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
1388 			vtyp = nfsv34tov_type(*tl);
1389 		}
1390 		error = nfsrv_sattr(nd, NULL, &nva, &attrbits, aclp, p);
1391 		if (error)
1392 			goto nfsmout;
1393 		nva.na_type = vtyp;
1394 		if (!nd->nd_repstat && (nd->nd_flag & ND_NFSV3) &&
1395 		    (vtyp == VCHR || vtyp == VBLK)) {
1396 			NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1397 			major = fxdr_unsigned(u_int32_t, *tl++);
1398 			minor = fxdr_unsigned(u_int32_t, *tl);
1399 			nva.na_rdev = NFSMAKEDEV(major, minor);
1400 		}
1401 	}
1402 
1403 	dirfor_ret = nfsvno_getattr(dp, &dirfor, nd, p, 0, NULL);
1404 	if (!nd->nd_repstat && (nd->nd_flag & ND_NFSV4)) {
1405 		if (!dirfor_ret && NFSVNO_ISSETGID(&nva) &&
1406 		    dirfor.na_gid == nva.na_gid)
1407 			NFSVNO_UNSET(&nva, gid);
1408 		nd->nd_repstat = nfsrv_checkuidgid(nd, &nva);
1409 	}
1410 	if (nd->nd_repstat) {
1411 		vrele(dp);
1412 #ifdef NFS4_ACL_EXTATTR_NAME
1413 		acl_free(aclp);
1414 #endif
1415 		nfsvno_relpathbuf(&named);
1416 		if (pathcp)
1417 			free(pathcp, M_TEMP);
1418 		if (nd->nd_flag & ND_NFSV3)
1419 			nfsrv_wcc(nd, dirfor_ret, &dirfor, diraft_ret,
1420 			    &diraft);
1421 		goto out;
1422 	}
1423 
1424 	/*
1425 	 * Yuck! For V4, mkdir and link are here and some V4 clients don't fill
1426 	 * in va_mode, so we'll have to set a default here.
1427 	 */
1428 	if (NFSVNO_NOTSETMODE(&nva)) {
1429 		if (vtyp == VLNK)
1430 			nva.na_mode = 0755;
1431 		else
1432 			nva.na_mode = 0400;
1433 	}
1434 
1435 	if (vtyp == VDIR)
1436 		named.ni_cnd.cn_flags |= WILLBEDIR;
1437 	nd->nd_repstat = nfsvno_namei(nd, &named, dp, 0, exp, &dirp);
1438 	if (nd->nd_repstat) {
1439 		if (dirp) {
1440 			if (nd->nd_flag & ND_NFSV3)
1441 				dirfor_ret = nfsvno_getattr(dirp, &dirfor, nd,
1442 				    p, 0, NULL);
1443 			vrele(dirp);
1444 		}
1445 #ifdef NFS4_ACL_EXTATTR_NAME
1446 		acl_free(aclp);
1447 #endif
1448 		if (nd->nd_flag & ND_NFSV3)
1449 			nfsrv_wcc(nd, dirfor_ret, &dirfor, diraft_ret,
1450 			    &diraft);
1451 		goto out;
1452 	}
1453 	if (dirp)
1454 		dirfor_ret = nfsvno_getattr(dirp, &dirfor, nd, p, 0, NULL);
1455 
1456 	if ((nd->nd_flag & ND_NFSV4) && (vtyp == VDIR || vtyp == VLNK)) {
1457 		if (vtyp == VDIR) {
1458 			nfsrvd_mkdirsub(nd, &named, &nva, fhp, vpp, dirp,
1459 			    &dirfor, &diraft, &diraft_ret, &attrbits, aclp, p,
1460 			    exp);
1461 #ifdef NFS4_ACL_EXTATTR_NAME
1462 			acl_free(aclp);
1463 #endif
1464 			goto out;
1465 		} else if (vtyp == VLNK) {
1466 			nfsrvd_symlinksub(nd, &named, &nva, fhp, vpp, dirp,
1467 			    &dirfor, &diraft, &diraft_ret, &attrbits,
1468 			    aclp, p, exp, pathcp, pathlen);
1469 #ifdef NFS4_ACL_EXTATTR_NAME
1470 			acl_free(aclp);
1471 #endif
1472 			free(pathcp, M_TEMP);
1473 			goto out;
1474 		}
1475 	}
1476 
1477 	nd->nd_repstat = nfsvno_mknod(&named, &nva, nd->nd_cred, p);
1478 	if (!nd->nd_repstat) {
1479 		vp = named.ni_vp;
1480 		nfsrv_fixattr(nd, vp, &nva, aclp, p, &attrbits, exp);
1481 		nd->nd_repstat = nfsvno_getfh(vp, fhp, p);
1482 		if ((nd->nd_flag & ND_NFSV3) && !nd->nd_repstat)
1483 			nd->nd_repstat = nfsvno_getattr(vp, &nva, nd, p, 1,
1484 			    NULL);
1485 		if (vpp != NULL && nd->nd_repstat == 0) {
1486 			NFSVOPUNLOCK(vp);
1487 			*vpp = vp;
1488 		} else
1489 			vput(vp);
1490 	}
1491 
1492 	diraft_ret = nfsvno_getattr(dirp, &diraft, nd, p, 0, NULL);
1493 	vrele(dirp);
1494 	if (!nd->nd_repstat) {
1495 		if (nd->nd_flag & ND_NFSV3) {
1496 			(void)nfsm_fhtom(NULL, nd, (u_int8_t *)fhp, 0, 1);
1497 			nfsrv_postopattr(nd, 0, &nva);
1498 		} else {
1499 			NFSM_BUILD(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
1500 			*tl++ = newnfs_false;
1501 			txdr_hyper(dirfor.na_filerev, tl);
1502 			tl += 2;
1503 			txdr_hyper(diraft.na_filerev, tl);
1504 			(void) nfsrv_putattrbit(nd, &attrbits);
1505 		}
1506 	}
1507 	if (nd->nd_flag & ND_NFSV3)
1508 		nfsrv_wcc(nd, dirfor_ret, &dirfor, diraft_ret, &diraft);
1509 #ifdef NFS4_ACL_EXTATTR_NAME
1510 	acl_free(aclp);
1511 #endif
1512 
1513 out:
1514 	NFSEXITCODE2(0, nd);
1515 	return (0);
1516 nfsmout:
1517 	vrele(dp);
1518 #ifdef NFS4_ACL_EXTATTR_NAME
1519 	acl_free(aclp);
1520 #endif
1521 	if (bufp)
1522 		nfsvno_relpathbuf(&named);
1523 	if (pathcp)
1524 		free(pathcp, M_TEMP);
1525 
1526 	NFSEXITCODE2(error, nd);
1527 	return (error);
1528 }
1529 
1530 /*
1531  * nfs remove service
1532  */
1533 int
1534 nfsrvd_remove(struct nfsrv_descript *nd, __unused int isdgram,
1535     vnode_t dp, struct nfsexstuff *exp)
1536 {
1537 	struct nameidata named;
1538 	u_int32_t *tl;
1539 	int error = 0, dirfor_ret = 1, diraft_ret = 1;
1540 	vnode_t dirp = NULL;
1541 	struct nfsvattr dirfor, diraft;
1542 	char *bufp;
1543 	u_long *hashp;
1544 	struct thread *p = curthread;
1545 
1546 	if (nd->nd_repstat) {
1547 		nfsrv_wcc(nd, dirfor_ret, &dirfor, diraft_ret, &diraft);
1548 		goto out;
1549 	}
1550 	NFSNAMEICNDSET(&named.ni_cnd, nd->nd_cred, DELETE,
1551 	    LOCKPARENT | LOCKLEAF);
1552 	nfsvno_setpathbuf(&named, &bufp, &hashp);
1553 	error = nfsrv_parsename(nd, bufp, hashp, &named.ni_pathlen);
1554 	if (error) {
1555 		vput(dp);
1556 		nfsvno_relpathbuf(&named);
1557 		goto out;
1558 	}
1559 	if (!nd->nd_repstat) {
1560 		nd->nd_repstat = nfsvno_namei(nd, &named, dp, 1, exp, &dirp);
1561 	} else {
1562 		vput(dp);
1563 		nfsvno_relpathbuf(&named);
1564 	}
1565 	if (dirp) {
1566 		if (!(nd->nd_flag & ND_NFSV2)) {
1567 			dirfor_ret = nfsvno_getattr(dirp, &dirfor, nd, p, 0,
1568 			    NULL);
1569 		} else {
1570 			vrele(dirp);
1571 			dirp = NULL;
1572 		}
1573 	}
1574 	if (!nd->nd_repstat) {
1575 		if (nd->nd_flag & ND_NFSV4) {
1576 			if (named.ni_vp->v_type == VDIR)
1577 				nd->nd_repstat = nfsvno_rmdirsub(&named, 1,
1578 				    nd->nd_cred, p, exp);
1579 			else
1580 				nd->nd_repstat = nfsvno_removesub(&named, 1,
1581 				    nd->nd_cred, p, exp);
1582 		} else if (nd->nd_procnum == NFSPROC_RMDIR) {
1583 			nd->nd_repstat = nfsvno_rmdirsub(&named, 0,
1584 			    nd->nd_cred, p, exp);
1585 		} else {
1586 			nd->nd_repstat = nfsvno_removesub(&named, 0,
1587 			    nd->nd_cred, p, exp);
1588 		}
1589 	}
1590 	if (!(nd->nd_flag & ND_NFSV2)) {
1591 		if (dirp) {
1592 			diraft_ret = nfsvno_getattr(dirp, &diraft, nd, p, 0,
1593 			    NULL);
1594 			vrele(dirp);
1595 		}
1596 		if (nd->nd_flag & ND_NFSV3) {
1597 			nfsrv_wcc(nd, dirfor_ret, &dirfor, diraft_ret,
1598 			    &diraft);
1599 		} else if (!nd->nd_repstat) {
1600 			NFSM_BUILD(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
1601 			*tl++ = newnfs_false;
1602 			txdr_hyper(dirfor.na_filerev, tl);
1603 			tl += 2;
1604 			txdr_hyper(diraft.na_filerev, tl);
1605 		}
1606 	}
1607 
1608 out:
1609 	NFSEXITCODE2(error, nd);
1610 	return (error);
1611 }
1612 
1613 /*
1614  * nfs rename service
1615  */
1616 int
1617 nfsrvd_rename(struct nfsrv_descript *nd, int isdgram,
1618     vnode_t dp, vnode_t todp, struct nfsexstuff *exp, struct nfsexstuff *toexp)
1619 {
1620 	u_int32_t *tl;
1621 	int error = 0, fdirfor_ret = 1, fdiraft_ret = 1;
1622 	int tdirfor_ret = 1, tdiraft_ret = 1;
1623 	struct nameidata fromnd, tond;
1624 	vnode_t fdirp = NULL, tdirp = NULL, tdp = NULL;
1625 	struct nfsvattr fdirfor, fdiraft, tdirfor, tdiraft;
1626 	struct nfsexstuff tnes;
1627 	struct nfsrvfh tfh;
1628 	char *bufp, *tbufp = NULL;
1629 	u_long *hashp;
1630 	fhandle_t fh;
1631 	struct thread *p = curthread;
1632 
1633 	if (nd->nd_repstat) {
1634 		nfsrv_wcc(nd, fdirfor_ret, &fdirfor, fdiraft_ret, &fdiraft);
1635 		nfsrv_wcc(nd, tdirfor_ret, &tdirfor, tdiraft_ret, &tdiraft);
1636 		goto out;
1637 	}
1638 	if (!(nd->nd_flag & ND_NFSV2))
1639 		fdirfor_ret = nfsvno_getattr(dp, &fdirfor, nd, p, 1, NULL);
1640 	tond.ni_cnd.cn_nameiop = 0;
1641 	tond.ni_startdir = NULL;
1642 	NFSNAMEICNDSET(&fromnd.ni_cnd, nd->nd_cred, DELETE, WANTPARENT);
1643 	nfsvno_setpathbuf(&fromnd, &bufp, &hashp);
1644 	error = nfsrv_parsename(nd, bufp, hashp, &fromnd.ni_pathlen);
1645 	if (error) {
1646 		vput(dp);
1647 		if (todp)
1648 			vrele(todp);
1649 		nfsvno_relpathbuf(&fromnd);
1650 		goto out;
1651 	}
1652 	/*
1653 	 * Unlock dp in this code section, so it is unlocked before
1654 	 * tdp gets locked. This avoids a potential LOR if tdp is the
1655 	 * parent directory of dp.
1656 	 */
1657 	if (nd->nd_flag & ND_NFSV4) {
1658 		tdp = todp;
1659 		tnes = *toexp;
1660 		if (dp != tdp) {
1661 			NFSVOPUNLOCK(dp);
1662 			/* Might lock tdp. */
1663 			tdirfor_ret = nfsvno_getattr(tdp, &tdirfor, nd, p, 0,
1664 			    NULL);
1665 		} else {
1666 			tdirfor_ret = nfsvno_getattr(tdp, &tdirfor, nd, p, 1,
1667 			    NULL);
1668 			NFSVOPUNLOCK(dp);
1669 		}
1670 	} else {
1671 		tfh.nfsrvfh_len = 0;
1672 		error = nfsrv_mtofh(nd, &tfh);
1673 		if (error == 0)
1674 			error = nfsvno_getfh(dp, &fh, p);
1675 		if (error) {
1676 			vput(dp);
1677 			/* todp is always NULL except NFSv4 */
1678 			nfsvno_relpathbuf(&fromnd);
1679 			goto out;
1680 		}
1681 
1682 		/* If this is the same file handle, just VREF() the vnode. */
1683 		if (tfh.nfsrvfh_len == NFSX_MYFH &&
1684 		    !NFSBCMP(tfh.nfsrvfh_data, &fh, NFSX_MYFH)) {
1685 			VREF(dp);
1686 			tdp = dp;
1687 			tnes = *exp;
1688 			tdirfor_ret = nfsvno_getattr(tdp, &tdirfor, nd, p, 1,
1689 			    NULL);
1690 			NFSVOPUNLOCK(dp);
1691 		} else {
1692 			NFSVOPUNLOCK(dp);
1693 			nd->nd_cred->cr_uid = nd->nd_saveduid;
1694 			nfsd_fhtovp(nd, &tfh, LK_EXCLUSIVE, &tdp, &tnes, NULL,
1695 			    0, -1);	/* Locks tdp. */
1696 			if (tdp) {
1697 				tdirfor_ret = nfsvno_getattr(tdp, &tdirfor, nd,
1698 				    p, 1, NULL);
1699 				NFSVOPUNLOCK(tdp);
1700 			}
1701 		}
1702 	}
1703 	NFSNAMEICNDSET(&tond.ni_cnd, nd->nd_cred, RENAME, LOCKPARENT | LOCKLEAF | NOCACHE);
1704 	nfsvno_setpathbuf(&tond, &tbufp, &hashp);
1705 	if (!nd->nd_repstat) {
1706 		error = nfsrv_parsename(nd, tbufp, hashp, &tond.ni_pathlen);
1707 		if (error) {
1708 			if (tdp)
1709 				vrele(tdp);
1710 			vrele(dp);
1711 			nfsvno_relpathbuf(&fromnd);
1712 			nfsvno_relpathbuf(&tond);
1713 			goto out;
1714 		}
1715 	}
1716 	if (nd->nd_repstat) {
1717 		if (nd->nd_flag & ND_NFSV3) {
1718 			nfsrv_wcc(nd, fdirfor_ret, &fdirfor, fdiraft_ret,
1719 			    &fdiraft);
1720 			nfsrv_wcc(nd, tdirfor_ret, &tdirfor, tdiraft_ret,
1721 			    &tdiraft);
1722 		}
1723 		if (tdp)
1724 			vrele(tdp);
1725 		vrele(dp);
1726 		nfsvno_relpathbuf(&fromnd);
1727 		nfsvno_relpathbuf(&tond);
1728 		goto out;
1729 	}
1730 
1731 	/*
1732 	 * Done parsing, now down to business.
1733 	 */
1734 	nd->nd_repstat = nfsvno_namei(nd, &fromnd, dp, 0, exp, &fdirp);
1735 	if (nd->nd_repstat) {
1736 		if (nd->nd_flag & ND_NFSV3) {
1737 			nfsrv_wcc(nd, fdirfor_ret, &fdirfor, fdiraft_ret,
1738 			    &fdiraft);
1739 			nfsrv_wcc(nd, tdirfor_ret, &tdirfor, tdiraft_ret,
1740 			    &tdiraft);
1741 		}
1742 		if (fdirp)
1743 			vrele(fdirp);
1744 		if (tdp)
1745 			vrele(tdp);
1746 		nfsvno_relpathbuf(&tond);
1747 		goto out;
1748 	}
1749 	if (fromnd.ni_vp->v_type == VDIR)
1750 		tond.ni_cnd.cn_flags |= WILLBEDIR;
1751 	nd->nd_repstat = nfsvno_namei(nd, &tond, tdp, 0, &tnes, &tdirp);
1752 	nd->nd_repstat = nfsvno_rename(&fromnd, &tond, nd->nd_repstat,
1753 	    nd->nd_flag, nd->nd_cred, p);
1754 	if (fdirp)
1755 		fdiraft_ret = nfsvno_getattr(fdirp, &fdiraft, nd, p, 0, NULL);
1756 	if (tdirp)
1757 		tdiraft_ret = nfsvno_getattr(tdirp, &tdiraft, nd, p, 0, NULL);
1758 	if (fdirp)
1759 		vrele(fdirp);
1760 	if (tdirp)
1761 		vrele(tdirp);
1762 	if (nd->nd_flag & ND_NFSV3) {
1763 		nfsrv_wcc(nd, fdirfor_ret, &fdirfor, fdiraft_ret, &fdiraft);
1764 		nfsrv_wcc(nd, tdirfor_ret, &tdirfor, tdiraft_ret, &tdiraft);
1765 	} else if ((nd->nd_flag & ND_NFSV4) && !nd->nd_repstat) {
1766 		NFSM_BUILD(tl, u_int32_t *, 10 * NFSX_UNSIGNED);
1767 		*tl++ = newnfs_false;
1768 		txdr_hyper(fdirfor.na_filerev, tl);
1769 		tl += 2;
1770 		txdr_hyper(fdiraft.na_filerev, tl);
1771 		tl += 2;
1772 		*tl++ = newnfs_false;
1773 		txdr_hyper(tdirfor.na_filerev, tl);
1774 		tl += 2;
1775 		txdr_hyper(tdiraft.na_filerev, tl);
1776 	}
1777 
1778 out:
1779 	NFSEXITCODE2(error, nd);
1780 	return (error);
1781 }
1782 
1783 /*
1784  * nfs link service
1785  */
1786 int
1787 nfsrvd_link(struct nfsrv_descript *nd, int isdgram,
1788     vnode_t vp, vnode_t tovp, struct nfsexstuff *exp, struct nfsexstuff *toexp)
1789 {
1790 	struct nameidata named;
1791 	u_int32_t *tl;
1792 	int error = 0, dirfor_ret = 1, diraft_ret = 1, getret = 1;
1793 	vnode_t dirp = NULL, dp = NULL;
1794 	struct nfsvattr dirfor, diraft, at;
1795 	struct nfsexstuff tnes;
1796 	struct nfsrvfh dfh;
1797 	char *bufp;
1798 	u_long *hashp;
1799 	struct thread *p = curthread;
1800 
1801 	if (nd->nd_repstat) {
1802 		nfsrv_postopattr(nd, getret, &at);
1803 		nfsrv_wcc(nd, dirfor_ret, &dirfor, diraft_ret, &diraft);
1804 		goto out;
1805 	}
1806 	NFSVOPUNLOCK(vp);
1807 	if (vp->v_type == VDIR) {
1808 		if (nd->nd_flag & ND_NFSV4)
1809 			nd->nd_repstat = NFSERR_ISDIR;
1810 		else
1811 			nd->nd_repstat = NFSERR_INVAL;
1812 		if (tovp)
1813 			vrele(tovp);
1814 	}
1815 	if (!nd->nd_repstat) {
1816 		if (nd->nd_flag & ND_NFSV4) {
1817 			dp = tovp;
1818 			tnes = *toexp;
1819 		} else {
1820 			error = nfsrv_mtofh(nd, &dfh);
1821 			if (error) {
1822 				vrele(vp);
1823 				/* tovp is always NULL unless NFSv4 */
1824 				goto out;
1825 			}
1826 			nfsd_fhtovp(nd, &dfh, LK_EXCLUSIVE, &dp, &tnes, NULL,
1827 			    0, -1);
1828 			if (dp)
1829 				NFSVOPUNLOCK(dp);
1830 		}
1831 	}
1832 	NFSNAMEICNDSET(&named.ni_cnd, nd->nd_cred, CREATE, LOCKPARENT | NOCACHE);
1833 	if (!nd->nd_repstat) {
1834 		nfsvno_setpathbuf(&named, &bufp, &hashp);
1835 		error = nfsrv_parsename(nd, bufp, hashp, &named.ni_pathlen);
1836 		if (error) {
1837 			vrele(vp);
1838 			if (dp)
1839 				vrele(dp);
1840 			nfsvno_relpathbuf(&named);
1841 			goto out;
1842 		}
1843 		if (!nd->nd_repstat) {
1844 			nd->nd_repstat = nfsvno_namei(nd, &named, dp, 0, &tnes,
1845 			    &dirp);
1846 		} else {
1847 			if (dp)
1848 				vrele(dp);
1849 			nfsvno_relpathbuf(&named);
1850 		}
1851 	}
1852 	if (dirp) {
1853 		if (nd->nd_flag & ND_NFSV2) {
1854 			vrele(dirp);
1855 			dirp = NULL;
1856 		} else {
1857 			dirfor_ret = nfsvno_getattr(dirp, &dirfor, nd, p, 0,
1858 			    NULL);
1859 		}
1860 	}
1861 	if (!nd->nd_repstat)
1862 		nd->nd_repstat = nfsvno_link(&named, vp, nd->nd_cred, p, exp);
1863 	if (nd->nd_flag & ND_NFSV3)
1864 		getret = nfsvno_getattr(vp, &at, nd, p, 0, NULL);
1865 	if (dirp) {
1866 		diraft_ret = nfsvno_getattr(dirp, &diraft, nd, p, 0, NULL);
1867 		vrele(dirp);
1868 	}
1869 	vrele(vp);
1870 	if (nd->nd_flag & ND_NFSV3) {
1871 		nfsrv_postopattr(nd, getret, &at);
1872 		nfsrv_wcc(nd, dirfor_ret, &dirfor, diraft_ret, &diraft);
1873 	} else if ((nd->nd_flag & ND_NFSV4) && !nd->nd_repstat) {
1874 		NFSM_BUILD(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
1875 		*tl++ = newnfs_false;
1876 		txdr_hyper(dirfor.na_filerev, tl);
1877 		tl += 2;
1878 		txdr_hyper(diraft.na_filerev, tl);
1879 	}
1880 
1881 out:
1882 	NFSEXITCODE2(error, nd);
1883 	return (error);
1884 }
1885 
1886 /*
1887  * nfs symbolic link service
1888  */
1889 int
1890 nfsrvd_symlink(struct nfsrv_descript *nd, __unused int isdgram,
1891     vnode_t dp, vnode_t *vpp, fhandle_t *fhp, struct nfsexstuff *exp)
1892 {
1893 	struct nfsvattr nva, dirfor, diraft;
1894 	struct nameidata named;
1895 	int error = 0, dirfor_ret = 1, diraft_ret = 1, pathlen;
1896 	vnode_t dirp = NULL;
1897 	char *bufp, *pathcp = NULL;
1898 	u_long *hashp;
1899 	struct thread *p = curthread;
1900 
1901 	if (nd->nd_repstat) {
1902 		nfsrv_wcc(nd, dirfor_ret, &dirfor, diraft_ret, &diraft);
1903 		goto out;
1904 	}
1905 	if (vpp)
1906 		*vpp = NULL;
1907 	NFSVNO_ATTRINIT(&nva);
1908 	NFSNAMEICNDSET(&named.ni_cnd, nd->nd_cred, CREATE,
1909 	    LOCKPARENT | NOCACHE);
1910 	nfsvno_setpathbuf(&named, &bufp, &hashp);
1911 	error = nfsrv_parsename(nd, bufp, hashp, &named.ni_pathlen);
1912 	if (!error && !nd->nd_repstat)
1913 		error = nfsvno_getsymlink(nd, &nva, p, &pathcp, &pathlen);
1914 	if (error) {
1915 		vrele(dp);
1916 		nfsvno_relpathbuf(&named);
1917 		goto out;
1918 	}
1919 	if (!nd->nd_repstat) {
1920 		nd->nd_repstat = nfsvno_namei(nd, &named, dp, 0, exp, &dirp);
1921 	} else {
1922 		vrele(dp);
1923 		nfsvno_relpathbuf(&named);
1924 	}
1925 	if (dirp != NULL && !(nd->nd_flag & ND_NFSV3)) {
1926 		vrele(dirp);
1927 		dirp = NULL;
1928 	}
1929 
1930 	/*
1931 	 * And call nfsrvd_symlinksub() to do the common code. It will
1932 	 * return EBADRPC upon a parsing error, 0 otherwise.
1933 	 */
1934 	if (!nd->nd_repstat) {
1935 		if (dirp != NULL)
1936 			dirfor_ret = nfsvno_getattr(dirp, &dirfor, nd, p, 0,
1937 			    NULL);
1938 		nfsrvd_symlinksub(nd, &named, &nva, fhp, vpp, dirp,
1939 		    &dirfor, &diraft, &diraft_ret, NULL, NULL, p, exp,
1940 		    pathcp, pathlen);
1941 	} else if (dirp != NULL) {
1942 		dirfor_ret = nfsvno_getattr(dirp, &dirfor, nd, p, 0, NULL);
1943 		vrele(dirp);
1944 	}
1945 	if (pathcp)
1946 		free(pathcp, M_TEMP);
1947 
1948 	if (nd->nd_flag & ND_NFSV3) {
1949 		if (!nd->nd_repstat) {
1950 			(void)nfsm_fhtom(NULL, nd, (u_int8_t *)fhp, 0, 1);
1951 			nfsrv_postopattr(nd, 0, &nva);
1952 		}
1953 		nfsrv_wcc(nd, dirfor_ret, &dirfor, diraft_ret, &diraft);
1954 	}
1955 
1956 out:
1957 	NFSEXITCODE2(error, nd);
1958 	return (error);
1959 }
1960 
1961 /*
1962  * Common code for creating a symbolic link.
1963  */
1964 static void
1965 nfsrvd_symlinksub(struct nfsrv_descript *nd, struct nameidata *ndp,
1966     struct nfsvattr *nvap, fhandle_t *fhp, vnode_t *vpp,
1967     vnode_t dirp, struct nfsvattr *dirforp, struct nfsvattr *diraftp,
1968     int *diraft_retp, nfsattrbit_t *attrbitp,
1969     NFSACL_T *aclp, NFSPROC_T *p, struct nfsexstuff *exp, char *pathcp,
1970     int pathlen)
1971 {
1972 	u_int32_t *tl;
1973 
1974 	nd->nd_repstat = nfsvno_symlink(ndp, nvap, pathcp, pathlen,
1975 	    !(nd->nd_flag & ND_NFSV2), nd->nd_saveduid, nd->nd_cred, p, exp);
1976 	if (!nd->nd_repstat && !(nd->nd_flag & ND_NFSV2)) {
1977 		nfsrv_fixattr(nd, ndp->ni_vp, nvap, aclp, p, attrbitp, exp);
1978 		if (nd->nd_flag & ND_NFSV3) {
1979 			nd->nd_repstat = nfsvno_getfh(ndp->ni_vp, fhp, p);
1980 			if (!nd->nd_repstat)
1981 				nd->nd_repstat = nfsvno_getattr(ndp->ni_vp,
1982 				    nvap, nd, p, 1, NULL);
1983 		}
1984 		if (vpp != NULL && nd->nd_repstat == 0) {
1985 			NFSVOPUNLOCK(ndp->ni_vp);
1986 			*vpp = ndp->ni_vp;
1987 		} else
1988 			vput(ndp->ni_vp);
1989 	}
1990 	if (dirp) {
1991 		*diraft_retp = nfsvno_getattr(dirp, diraftp, nd, p, 0, NULL);
1992 		vrele(dirp);
1993 	}
1994 	if ((nd->nd_flag & ND_NFSV4) && !nd->nd_repstat) {
1995 		NFSM_BUILD(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
1996 		*tl++ = newnfs_false;
1997 		txdr_hyper(dirforp->na_filerev, tl);
1998 		tl += 2;
1999 		txdr_hyper(diraftp->na_filerev, tl);
2000 		(void) nfsrv_putattrbit(nd, attrbitp);
2001 	}
2002 
2003 	NFSEXITCODE2(0, nd);
2004 }
2005 
2006 /*
2007  * nfs mkdir service
2008  */
2009 int
2010 nfsrvd_mkdir(struct nfsrv_descript *nd, __unused int isdgram,
2011     vnode_t dp, vnode_t *vpp, fhandle_t *fhp, struct nfsexstuff *exp)
2012 {
2013 	struct nfsvattr nva, dirfor, diraft;
2014 	struct nameidata named;
2015 	u_int32_t *tl;
2016 	int error = 0, dirfor_ret = 1, diraft_ret = 1;
2017 	vnode_t dirp = NULL;
2018 	char *bufp;
2019 	u_long *hashp;
2020 	struct thread *p = curthread;
2021 
2022 	if (nd->nd_repstat) {
2023 		nfsrv_wcc(nd, dirfor_ret, &dirfor, diraft_ret, &diraft);
2024 		goto out;
2025 	}
2026 	NFSNAMEICNDSET(&named.ni_cnd, nd->nd_cred, CREATE, LOCKPARENT | NOCACHE);
2027 	nfsvno_setpathbuf(&named, &bufp, &hashp);
2028 	error = nfsrv_parsename(nd, bufp, hashp, &named.ni_pathlen);
2029 	if (error)
2030 		goto nfsmout;
2031 	if (!nd->nd_repstat) {
2032 		NFSVNO_ATTRINIT(&nva);
2033 		if (nd->nd_flag & ND_NFSV3) {
2034 			error = nfsrv_sattr(nd, NULL, &nva, NULL, NULL, p);
2035 			if (error)
2036 				goto nfsmout;
2037 		} else {
2038 			NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2039 			nva.na_mode = nfstov_mode(*tl++);
2040 		}
2041 	}
2042 	if (!nd->nd_repstat) {
2043 		nd->nd_repstat = nfsvno_namei(nd, &named, dp, 0, exp, &dirp);
2044 	} else {
2045 		vrele(dp);
2046 		nfsvno_relpathbuf(&named);
2047 	}
2048 	if (dirp != NULL && !(nd->nd_flag & ND_NFSV3)) {
2049 		vrele(dirp);
2050 		dirp = NULL;
2051 	}
2052 	if (nd->nd_repstat) {
2053 		if (dirp != NULL) {
2054 			dirfor_ret = nfsvno_getattr(dirp, &dirfor, nd, p, 0,
2055 			    NULL);
2056 			vrele(dirp);
2057 		}
2058 		if (nd->nd_flag & ND_NFSV3)
2059 			nfsrv_wcc(nd, dirfor_ret, &dirfor, diraft_ret,
2060 			    &diraft);
2061 		goto out;
2062 	}
2063 	if (dirp != NULL)
2064 		dirfor_ret = nfsvno_getattr(dirp, &dirfor, nd, p, 0, NULL);
2065 
2066 	/*
2067 	 * Call nfsrvd_mkdirsub() for the code common to V4 as well.
2068 	 */
2069 	nfsrvd_mkdirsub(nd, &named, &nva, fhp, vpp, dirp, &dirfor, &diraft,
2070 	    &diraft_ret, NULL, NULL, p, exp);
2071 
2072 	if (nd->nd_flag & ND_NFSV3) {
2073 		if (!nd->nd_repstat) {
2074 			(void)nfsm_fhtom(NULL, nd, (u_int8_t *)fhp, 0, 1);
2075 			nfsrv_postopattr(nd, 0, &nva);
2076 		}
2077 		nfsrv_wcc(nd, dirfor_ret, &dirfor, diraft_ret, &diraft);
2078 	} else if (!nd->nd_repstat) {
2079 		(void)nfsm_fhtom(NULL, nd, (u_int8_t *)fhp, 0, 0);
2080 		nfsrv_fillattr(nd, &nva);
2081 	}
2082 
2083 out:
2084 	NFSEXITCODE2(0, nd);
2085 	return (0);
2086 nfsmout:
2087 	vrele(dp);
2088 	nfsvno_relpathbuf(&named);
2089 	NFSEXITCODE2(error, nd);
2090 	return (error);
2091 }
2092 
2093 /*
2094  * Code common to mkdir for V2,3 and 4.
2095  */
2096 static void
2097 nfsrvd_mkdirsub(struct nfsrv_descript *nd, struct nameidata *ndp,
2098     struct nfsvattr *nvap, fhandle_t *fhp, vnode_t *vpp,
2099     vnode_t dirp, struct nfsvattr *dirforp, struct nfsvattr *diraftp,
2100     int *diraft_retp, nfsattrbit_t *attrbitp, NFSACL_T *aclp,
2101     NFSPROC_T *p, struct nfsexstuff *exp)
2102 {
2103 	vnode_t vp;
2104 	u_int32_t *tl;
2105 
2106 	NFSVNO_SETATTRVAL(nvap, type, VDIR);
2107 	nd->nd_repstat = nfsvno_mkdir(ndp, nvap, nd->nd_saveduid,
2108 	    nd->nd_cred, p, exp);
2109 	if (!nd->nd_repstat) {
2110 		vp = ndp->ni_vp;
2111 		nfsrv_fixattr(nd, vp, nvap, aclp, p, attrbitp, exp);
2112 		nd->nd_repstat = nfsvno_getfh(vp, fhp, p);
2113 		if (!(nd->nd_flag & ND_NFSV4) && !nd->nd_repstat)
2114 			nd->nd_repstat = nfsvno_getattr(vp, nvap, nd, p, 1,
2115 			    NULL);
2116 		if (vpp && !nd->nd_repstat) {
2117 			NFSVOPUNLOCK(vp);
2118 			*vpp = vp;
2119 		} else {
2120 			vput(vp);
2121 		}
2122 	}
2123 	if (dirp) {
2124 		*diraft_retp = nfsvno_getattr(dirp, diraftp, nd, p, 0, NULL);
2125 		vrele(dirp);
2126 	}
2127 	if ((nd->nd_flag & ND_NFSV4) && !nd->nd_repstat) {
2128 		NFSM_BUILD(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
2129 		*tl++ = newnfs_false;
2130 		txdr_hyper(dirforp->na_filerev, tl);
2131 		tl += 2;
2132 		txdr_hyper(diraftp->na_filerev, tl);
2133 		(void) nfsrv_putattrbit(nd, attrbitp);
2134 	}
2135 
2136 	NFSEXITCODE2(0, nd);
2137 }
2138 
2139 /*
2140  * nfs commit service
2141  */
2142 int
2143 nfsrvd_commit(struct nfsrv_descript *nd, __unused int isdgram,
2144     vnode_t vp, __unused struct nfsexstuff *exp)
2145 {
2146 	struct nfsvattr bfor, aft;
2147 	u_int32_t *tl;
2148 	int error = 0, for_ret = 1, aft_ret = 1, cnt;
2149 	u_int64_t off;
2150 	struct thread *p = curthread;
2151 
2152        if (nd->nd_repstat) {
2153 		nfsrv_wcc(nd, for_ret, &bfor, aft_ret, &aft);
2154 		goto out;
2155 	}
2156 
2157 	/* Return NFSERR_ISDIR in NFSv4 when commit on a directory. */
2158 	if (vp->v_type != VREG) {
2159 		if (nd->nd_flag & ND_NFSV3)
2160 			error = NFSERR_NOTSUPP;
2161 		else
2162 			error = (vp->v_type == VDIR) ? NFSERR_ISDIR : NFSERR_INVAL;
2163 		goto nfsmout;
2164 	}
2165 	NFSM_DISSECT(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2166 
2167 	/*
2168 	 * XXX At this time VOP_FSYNC() does not accept offset and byte
2169 	 * count parameters, so these arguments are useless (someday maybe).
2170 	 */
2171 	off = fxdr_hyper(tl);
2172 	tl += 2;
2173 	cnt = fxdr_unsigned(int, *tl);
2174 	if (nd->nd_flag & ND_NFSV3)
2175 		for_ret = nfsvno_getattr(vp, &bfor, nd, p, 1, NULL);
2176 	nd->nd_repstat = nfsvno_fsync(vp, off, cnt, nd->nd_cred, p);
2177 	if (nd->nd_flag & ND_NFSV3) {
2178 		aft_ret = nfsvno_getattr(vp, &aft, nd, p, 1, NULL);
2179 		nfsrv_wcc(nd, for_ret, &bfor, aft_ret, &aft);
2180 	}
2181 	vput(vp);
2182 	if (!nd->nd_repstat) {
2183 		NFSM_BUILD(tl, u_int32_t *, NFSX_VERF);
2184 		*tl++ = txdr_unsigned(nfsboottime.tv_sec);
2185 		*tl = txdr_unsigned(nfsboottime.tv_usec);
2186 	}
2187 
2188 out:
2189 	NFSEXITCODE2(0, nd);
2190 	return (0);
2191 nfsmout:
2192 	vput(vp);
2193 	NFSEXITCODE2(error, nd);
2194 	return (error);
2195 }
2196 
2197 /*
2198  * nfs statfs service
2199  */
2200 int
2201 nfsrvd_statfs(struct nfsrv_descript *nd, __unused int isdgram,
2202     vnode_t vp, __unused struct nfsexstuff *exp)
2203 {
2204 	struct statfs *sf;
2205 	u_int32_t *tl;
2206 	int getret = 1;
2207 	struct nfsvattr at;
2208 	u_quad_t tval;
2209 	struct thread *p = curthread;
2210 
2211 	sf = NULL;
2212 	if (nd->nd_repstat) {
2213 		nfsrv_postopattr(nd, getret, &at);
2214 		goto out;
2215 	}
2216 	sf = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
2217 	nd->nd_repstat = nfsvno_statfs(vp, sf);
2218 	getret = nfsvno_getattr(vp, &at, nd, p, 1, NULL);
2219 	vput(vp);
2220 	if (nd->nd_flag & ND_NFSV3)
2221 		nfsrv_postopattr(nd, getret, &at);
2222 	if (nd->nd_repstat)
2223 		goto out;
2224 	if (nd->nd_flag & ND_NFSV2) {
2225 		NFSM_BUILD(tl, u_int32_t *, NFSX_V2STATFS);
2226 		*tl++ = txdr_unsigned(NFS_V2MAXDATA);
2227 		*tl++ = txdr_unsigned(sf->f_bsize);
2228 		*tl++ = txdr_unsigned(sf->f_blocks);
2229 		*tl++ = txdr_unsigned(sf->f_bfree);
2230 		*tl = txdr_unsigned(sf->f_bavail);
2231 	} else {
2232 		NFSM_BUILD(tl, u_int32_t *, NFSX_V3STATFS);
2233 		tval = (u_quad_t)sf->f_blocks;
2234 		tval *= (u_quad_t)sf->f_bsize;
2235 		txdr_hyper(tval, tl); tl += 2;
2236 		tval = (u_quad_t)sf->f_bfree;
2237 		tval *= (u_quad_t)sf->f_bsize;
2238 		txdr_hyper(tval, tl); tl += 2;
2239 		tval = (u_quad_t)sf->f_bavail;
2240 		tval *= (u_quad_t)sf->f_bsize;
2241 		txdr_hyper(tval, tl); tl += 2;
2242 		tval = (u_quad_t)sf->f_files;
2243 		txdr_hyper(tval, tl); tl += 2;
2244 		tval = (u_quad_t)sf->f_ffree;
2245 		txdr_hyper(tval, tl); tl += 2;
2246 		tval = (u_quad_t)sf->f_ffree;
2247 		txdr_hyper(tval, tl); tl += 2;
2248 		*tl = 0;
2249 	}
2250 
2251 out:
2252 	free(sf, M_STATFS);
2253 	NFSEXITCODE2(0, nd);
2254 	return (0);
2255 }
2256 
2257 /*
2258  * nfs fsinfo service
2259  */
2260 int
2261 nfsrvd_fsinfo(struct nfsrv_descript *nd, int isdgram,
2262     vnode_t vp, __unused struct nfsexstuff *exp)
2263 {
2264 	u_int32_t *tl;
2265 	struct nfsfsinfo fs;
2266 	int getret = 1;
2267 	struct nfsvattr at;
2268 	struct thread *p = curthread;
2269 
2270 	if (nd->nd_repstat) {
2271 		nfsrv_postopattr(nd, getret, &at);
2272 		goto out;
2273 	}
2274 	getret = nfsvno_getattr(vp, &at, nd, p, 1, NULL);
2275 	nfsvno_getfs(&fs, isdgram);
2276 	vput(vp);
2277 	nfsrv_postopattr(nd, getret, &at);
2278 	NFSM_BUILD(tl, u_int32_t *, NFSX_V3FSINFO);
2279 	*tl++ = txdr_unsigned(fs.fs_rtmax);
2280 	*tl++ = txdr_unsigned(fs.fs_rtpref);
2281 	*tl++ = txdr_unsigned(fs.fs_rtmult);
2282 	*tl++ = txdr_unsigned(fs.fs_wtmax);
2283 	*tl++ = txdr_unsigned(fs.fs_wtpref);
2284 	*tl++ = txdr_unsigned(fs.fs_wtmult);
2285 	*tl++ = txdr_unsigned(fs.fs_dtpref);
2286 	txdr_hyper(fs.fs_maxfilesize, tl);
2287 	tl += 2;
2288 	txdr_nfsv3time(&fs.fs_timedelta, tl);
2289 	tl += 2;
2290 	*tl = txdr_unsigned(fs.fs_properties);
2291 
2292 out:
2293 	NFSEXITCODE2(0, nd);
2294 	return (0);
2295 }
2296 
2297 /*
2298  * nfs pathconf service
2299  */
2300 int
2301 nfsrvd_pathconf(struct nfsrv_descript *nd, __unused int isdgram,
2302     vnode_t vp, __unused struct nfsexstuff *exp)
2303 {
2304 	struct nfsv3_pathconf *pc;
2305 	int getret = 1;
2306 	long linkmax, namemax, chownres, notrunc;
2307 	struct nfsvattr at;
2308 	struct thread *p = curthread;
2309 
2310 	if (nd->nd_repstat) {
2311 		nfsrv_postopattr(nd, getret, &at);
2312 		goto out;
2313 	}
2314 	nd->nd_repstat = nfsvno_pathconf(vp, _PC_LINK_MAX, &linkmax,
2315 	    nd->nd_cred, p);
2316 	if (!nd->nd_repstat)
2317 		nd->nd_repstat = nfsvno_pathconf(vp, _PC_NAME_MAX, &namemax,
2318 		    nd->nd_cred, p);
2319 	if (!nd->nd_repstat)
2320 		nd->nd_repstat=nfsvno_pathconf(vp, _PC_CHOWN_RESTRICTED,
2321 		    &chownres, nd->nd_cred, p);
2322 	if (!nd->nd_repstat)
2323 		nd->nd_repstat = nfsvno_pathconf(vp, _PC_NO_TRUNC, &notrunc,
2324 		    nd->nd_cred, p);
2325 	getret = nfsvno_getattr(vp, &at, nd, p, 1, NULL);
2326 	vput(vp);
2327 	nfsrv_postopattr(nd, getret, &at);
2328 	if (!nd->nd_repstat) {
2329 		NFSM_BUILD(pc, struct nfsv3_pathconf *, NFSX_V3PATHCONF);
2330 		pc->pc_linkmax = txdr_unsigned(linkmax);
2331 		pc->pc_namemax = txdr_unsigned(namemax);
2332 		pc->pc_notrunc = txdr_unsigned(notrunc);
2333 		pc->pc_chownrestricted = txdr_unsigned(chownres);
2334 
2335 		/*
2336 		 * These should probably be supported by VOP_PATHCONF(), but
2337 		 * until msdosfs is exportable (why would you want to?), the
2338 		 * Unix defaults should be ok.
2339 		 */
2340 		pc->pc_caseinsensitive = newnfs_false;
2341 		pc->pc_casepreserving = newnfs_true;
2342 	}
2343 
2344 out:
2345 	NFSEXITCODE2(0, nd);
2346 	return (0);
2347 }
2348 
2349 /*
2350  * nfsv4 lock service
2351  */
2352 int
2353 nfsrvd_lock(struct nfsrv_descript *nd, __unused int isdgram,
2354     vnode_t vp, struct nfsexstuff *exp)
2355 {
2356 	u_int32_t *tl;
2357 	int i;
2358 	struct nfsstate *stp = NULL;
2359 	struct nfslock *lop;
2360 	struct nfslockconflict cf;
2361 	int error = 0;
2362 	u_short flags = NFSLCK_LOCK, lflags;
2363 	u_int64_t offset, len;
2364 	nfsv4stateid_t stateid;
2365 	nfsquad_t clientid;
2366 	struct thread *p = curthread;
2367 
2368 	NFSM_DISSECT(tl, u_int32_t *, 7 * NFSX_UNSIGNED);
2369 	i = fxdr_unsigned(int, *tl++);
2370 	switch (i) {
2371 	case NFSV4LOCKT_READW:
2372 		flags |= NFSLCK_BLOCKING;
2373 	case NFSV4LOCKT_READ:
2374 		lflags = NFSLCK_READ;
2375 		break;
2376 	case NFSV4LOCKT_WRITEW:
2377 		flags |= NFSLCK_BLOCKING;
2378 	case NFSV4LOCKT_WRITE:
2379 		lflags = NFSLCK_WRITE;
2380 		break;
2381 	default:
2382 		nd->nd_repstat = NFSERR_BADXDR;
2383 		goto nfsmout;
2384 	}
2385 	if (*tl++ == newnfs_true)
2386 		flags |= NFSLCK_RECLAIM;
2387 	offset = fxdr_hyper(tl);
2388 	tl += 2;
2389 	len = fxdr_hyper(tl);
2390 	tl += 2;
2391 	if (*tl == newnfs_true)
2392 		flags |= NFSLCK_OPENTOLOCK;
2393 	if (flags & NFSLCK_OPENTOLOCK) {
2394 		NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED + NFSX_STATEID);
2395 		i = fxdr_unsigned(int, *(tl+4+(NFSX_STATEID / NFSX_UNSIGNED)));
2396 		if (i <= 0 || i > NFSV4_OPAQUELIMIT) {
2397 			nd->nd_repstat = NFSERR_BADXDR;
2398 			goto nfsmout;
2399 		}
2400 		stp = malloc(sizeof (struct nfsstate) + i,
2401 			M_NFSDSTATE, M_WAITOK);
2402 		stp->ls_ownerlen = i;
2403 		stp->ls_op = nd->nd_rp;
2404 		stp->ls_seq = fxdr_unsigned(int, *tl++);
2405 		stp->ls_stateid.seqid = fxdr_unsigned(u_int32_t, *tl++);
2406 		NFSBCOPY((caddr_t)tl, (caddr_t)stp->ls_stateid.other,
2407 			NFSX_STATEIDOTHER);
2408 		tl += (NFSX_STATEIDOTHER / NFSX_UNSIGNED);
2409 
2410 		/*
2411 		 * For the special stateid of other all 0s and seqid == 1, set
2412 		 * the stateid to the current stateid, if it is set.
2413 		 */
2414 		if ((nd->nd_flag & ND_NFSV41) != 0 &&
2415 		    stp->ls_stateid.seqid == 1 &&
2416 		    stp->ls_stateid.other[0] == 0 &&
2417 		    stp->ls_stateid.other[1] == 0 &&
2418 		    stp->ls_stateid.other[2] == 0) {
2419 			if ((nd->nd_flag & ND_CURSTATEID) != 0) {
2420 				stp->ls_stateid = nd->nd_curstateid;
2421 				stp->ls_stateid.seqid = 0;
2422 			} else {
2423 				nd->nd_repstat = NFSERR_BADSTATEID;
2424 				goto nfsmout;
2425 			}
2426 		}
2427 
2428 		stp->ls_opentolockseq = fxdr_unsigned(int, *tl++);
2429 		clientid.lval[0] = *tl++;
2430 		clientid.lval[1] = *tl++;
2431 		if ((nd->nd_flag & ND_IMPLIEDCLID) != 0) {
2432 			if ((nd->nd_flag & ND_NFSV41) != 0)
2433 				clientid.qval = nd->nd_clientid.qval;
2434 			else if (nd->nd_clientid.qval != clientid.qval)
2435 				printf("EEK3 multiple clids\n");
2436 		} else {
2437 			if ((nd->nd_flag & ND_NFSV41) != 0)
2438 				printf("EEK! no clientid from session\n");
2439 			nd->nd_flag |= ND_IMPLIEDCLID;
2440 			nd->nd_clientid.qval = clientid.qval;
2441 		}
2442 		error = nfsrv_mtostr(nd, stp->ls_owner, stp->ls_ownerlen);
2443 		if (error)
2444 			goto nfsmout;
2445 	} else {
2446 		NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID + NFSX_UNSIGNED);
2447 		stp = malloc(sizeof (struct nfsstate),
2448 			M_NFSDSTATE, M_WAITOK);
2449 		stp->ls_ownerlen = 0;
2450 		stp->ls_op = nd->nd_rp;
2451 		stp->ls_stateid.seqid = fxdr_unsigned(u_int32_t, *tl++);
2452 		NFSBCOPY((caddr_t)tl, (caddr_t)stp->ls_stateid.other,
2453 			NFSX_STATEIDOTHER);
2454 		tl += (NFSX_STATEIDOTHER / NFSX_UNSIGNED);
2455 
2456 		/*
2457 		 * For the special stateid of other all 0s and seqid == 1, set
2458 		 * the stateid to the current stateid, if it is set.
2459 		 */
2460 		if ((nd->nd_flag & ND_NFSV41) != 0 &&
2461 		    stp->ls_stateid.seqid == 1 &&
2462 		    stp->ls_stateid.other[0] == 0 &&
2463 		    stp->ls_stateid.other[1] == 0 &&
2464 		    stp->ls_stateid.other[2] == 0) {
2465 			if ((nd->nd_flag & ND_CURSTATEID) != 0) {
2466 				stp->ls_stateid = nd->nd_curstateid;
2467 				stp->ls_stateid.seqid = 0;
2468 			} else {
2469 				nd->nd_repstat = NFSERR_BADSTATEID;
2470 				goto nfsmout;
2471 			}
2472 		}
2473 
2474 		stp->ls_seq = fxdr_unsigned(int, *tl);
2475 		clientid.lval[0] = stp->ls_stateid.other[0];
2476 		clientid.lval[1] = stp->ls_stateid.other[1];
2477 		if ((nd->nd_flag & ND_IMPLIEDCLID) != 0) {
2478 			if ((nd->nd_flag & ND_NFSV41) != 0)
2479 				clientid.qval = nd->nd_clientid.qval;
2480 			else if (nd->nd_clientid.qval != clientid.qval)
2481 				printf("EEK4 multiple clids\n");
2482 		} else {
2483 			if ((nd->nd_flag & ND_NFSV41) != 0)
2484 				printf("EEK! no clientid from session\n");
2485 			nd->nd_flag |= ND_IMPLIEDCLID;
2486 			nd->nd_clientid.qval = clientid.qval;
2487 		}
2488 	}
2489 	lop = malloc(sizeof (struct nfslock),
2490 		M_NFSDLOCK, M_WAITOK);
2491 	lop->lo_first = offset;
2492 	if (len == NFS64BITSSET) {
2493 		lop->lo_end = NFS64BITSSET;
2494 	} else {
2495 		lop->lo_end = offset + len;
2496 		if (lop->lo_end <= lop->lo_first)
2497 			nd->nd_repstat = NFSERR_INVAL;
2498 	}
2499 	lop->lo_flags = lflags;
2500 	stp->ls_flags = flags;
2501 	stp->ls_uid = nd->nd_cred->cr_uid;
2502 
2503 	/*
2504 	 * Do basic access checking.
2505 	 */
2506 	if (!nd->nd_repstat && vp->v_type != VREG) {
2507 	    if (vp->v_type == VDIR)
2508 		nd->nd_repstat = NFSERR_ISDIR;
2509 	    else
2510 		nd->nd_repstat = NFSERR_INVAL;
2511 	}
2512 	if (!nd->nd_repstat) {
2513 	    if (lflags & NFSLCK_WRITE) {
2514 		nd->nd_repstat = nfsvno_accchk(vp, VWRITE,
2515 		    nd->nd_cred, exp, p, NFSACCCHK_ALLOWOWNER,
2516 		    NFSACCCHK_VPISLOCKED, NULL);
2517 	    } else {
2518 		nd->nd_repstat = nfsvno_accchk(vp, VREAD,
2519 		    nd->nd_cred, exp, p, NFSACCCHK_ALLOWOWNER,
2520 		    NFSACCCHK_VPISLOCKED, NULL);
2521 		if (nd->nd_repstat)
2522 		    nd->nd_repstat = nfsvno_accchk(vp, VEXEC,
2523 			nd->nd_cred, exp, p, NFSACCCHK_ALLOWOWNER,
2524 			NFSACCCHK_VPISLOCKED, NULL);
2525 	    }
2526 	}
2527 
2528 	/*
2529 	 * We call nfsrv_lockctrl() even if nd_repstat set, so that the
2530 	 * seqid# gets updated. nfsrv_lockctrl() will return the value
2531 	 * of nd_repstat, if it gets that far.
2532 	 */
2533 	nd->nd_repstat = nfsrv_lockctrl(vp, &stp, &lop, &cf, clientid,
2534 		&stateid, exp, nd, p);
2535 	if (lop)
2536 		free(lop, M_NFSDLOCK);
2537 	if (stp)
2538 		free(stp, M_NFSDSTATE);
2539 	if (!nd->nd_repstat) {
2540 		/* For NFSv4.1, set the Current StateID. */
2541 		if ((nd->nd_flag & ND_NFSV41) != 0) {
2542 			nd->nd_curstateid = stateid;
2543 			nd->nd_flag |= ND_CURSTATEID;
2544 		}
2545 		NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID);
2546 		*tl++ = txdr_unsigned(stateid.seqid);
2547 		NFSBCOPY((caddr_t)stateid.other,(caddr_t)tl,NFSX_STATEIDOTHER);
2548 	} else if (nd->nd_repstat == NFSERR_DENIED) {
2549 		NFSM_BUILD(tl, u_int32_t *, 7 * NFSX_UNSIGNED);
2550 		txdr_hyper(cf.cl_first, tl);
2551 		tl += 2;
2552 		if (cf.cl_end == NFS64BITSSET)
2553 			len = NFS64BITSSET;
2554 		else
2555 			len = cf.cl_end - cf.cl_first;
2556 		txdr_hyper(len, tl);
2557 		tl += 2;
2558 		if (cf.cl_flags == NFSLCK_WRITE)
2559 			*tl++ = txdr_unsigned(NFSV4LOCKT_WRITE);
2560 		else
2561 			*tl++ = txdr_unsigned(NFSV4LOCKT_READ);
2562 		*tl++ = stateid.other[0];
2563 		*tl = stateid.other[1];
2564 		(void) nfsm_strtom(nd, cf.cl_owner, cf.cl_ownerlen);
2565 	}
2566 	vput(vp);
2567 	NFSEXITCODE2(0, nd);
2568 	return (0);
2569 nfsmout:
2570 	vput(vp);
2571 	if (stp)
2572 		free(stp, M_NFSDSTATE);
2573 	NFSEXITCODE2(error, nd);
2574 	return (error);
2575 }
2576 
2577 /*
2578  * nfsv4 lock test service
2579  */
2580 int
2581 nfsrvd_lockt(struct nfsrv_descript *nd, __unused int isdgram,
2582     vnode_t vp, struct nfsexstuff *exp)
2583 {
2584 	u_int32_t *tl;
2585 	int i;
2586 	struct nfsstate *stp = NULL;
2587 	struct nfslock lo, *lop = &lo;
2588 	struct nfslockconflict cf;
2589 	int error = 0;
2590 	nfsv4stateid_t stateid;
2591 	nfsquad_t clientid;
2592 	u_int64_t len;
2593 	struct thread *p = curthread;
2594 
2595 	NFSM_DISSECT(tl, u_int32_t *, 8 * NFSX_UNSIGNED);
2596 	i = fxdr_unsigned(int, *(tl + 7));
2597 	if (i <= 0 || i > NFSV4_OPAQUELIMIT) {
2598 		nd->nd_repstat = NFSERR_BADXDR;
2599 		goto nfsmout;
2600 	}
2601 	stp = malloc(sizeof (struct nfsstate) + i,
2602 	    M_NFSDSTATE, M_WAITOK);
2603 	stp->ls_ownerlen = i;
2604 	stp->ls_op = NULL;
2605 	stp->ls_flags = NFSLCK_TEST;
2606 	stp->ls_uid = nd->nd_cred->cr_uid;
2607 	i = fxdr_unsigned(int, *tl++);
2608 	switch (i) {
2609 	case NFSV4LOCKT_READW:
2610 		stp->ls_flags |= NFSLCK_BLOCKING;
2611 	case NFSV4LOCKT_READ:
2612 		lo.lo_flags = NFSLCK_READ;
2613 		break;
2614 	case NFSV4LOCKT_WRITEW:
2615 		stp->ls_flags |= NFSLCK_BLOCKING;
2616 	case NFSV4LOCKT_WRITE:
2617 		lo.lo_flags = NFSLCK_WRITE;
2618 		break;
2619 	default:
2620 		nd->nd_repstat = NFSERR_BADXDR;
2621 		goto nfsmout;
2622 	}
2623 	lo.lo_first = fxdr_hyper(tl);
2624 	tl += 2;
2625 	len = fxdr_hyper(tl);
2626 	if (len == NFS64BITSSET) {
2627 		lo.lo_end = NFS64BITSSET;
2628 	} else {
2629 		lo.lo_end = lo.lo_first + len;
2630 		if (lo.lo_end <= lo.lo_first)
2631 			nd->nd_repstat = NFSERR_INVAL;
2632 	}
2633 	tl += 2;
2634 	clientid.lval[0] = *tl++;
2635 	clientid.lval[1] = *tl;
2636 	if ((nd->nd_flag & ND_IMPLIEDCLID) != 0) {
2637 		if ((nd->nd_flag & ND_NFSV41) != 0)
2638 			clientid.qval = nd->nd_clientid.qval;
2639 		else if (nd->nd_clientid.qval != clientid.qval)
2640 			printf("EEK5 multiple clids\n");
2641 	} else {
2642 		if ((nd->nd_flag & ND_NFSV41) != 0)
2643 			printf("EEK! no clientid from session\n");
2644 		nd->nd_flag |= ND_IMPLIEDCLID;
2645 		nd->nd_clientid.qval = clientid.qval;
2646 	}
2647 	error = nfsrv_mtostr(nd, stp->ls_owner, stp->ls_ownerlen);
2648 	if (error)
2649 		goto nfsmout;
2650 	if (!nd->nd_repstat && vp->v_type != VREG) {
2651 	    if (vp->v_type == VDIR)
2652 		nd->nd_repstat = NFSERR_ISDIR;
2653 	    else
2654 		nd->nd_repstat = NFSERR_INVAL;
2655 	}
2656 	if (!nd->nd_repstat)
2657 	  nd->nd_repstat = nfsrv_lockctrl(vp, &stp, &lop, &cf, clientid,
2658 	    &stateid, exp, nd, p);
2659 	if (nd->nd_repstat) {
2660 	    if (nd->nd_repstat == NFSERR_DENIED) {
2661 		NFSM_BUILD(tl, u_int32_t *, 7 * NFSX_UNSIGNED);
2662 		txdr_hyper(cf.cl_first, tl);
2663 		tl += 2;
2664 		if (cf.cl_end == NFS64BITSSET)
2665 			len = NFS64BITSSET;
2666 		else
2667 			len = cf.cl_end - cf.cl_first;
2668 		txdr_hyper(len, tl);
2669 		tl += 2;
2670 		if (cf.cl_flags == NFSLCK_WRITE)
2671 			*tl++ = txdr_unsigned(NFSV4LOCKT_WRITE);
2672 		else
2673 			*tl++ = txdr_unsigned(NFSV4LOCKT_READ);
2674 		*tl++ = stp->ls_stateid.other[0];
2675 		*tl = stp->ls_stateid.other[1];
2676 		(void) nfsm_strtom(nd, cf.cl_owner, cf.cl_ownerlen);
2677 	    }
2678 	}
2679 	vput(vp);
2680 	if (stp)
2681 		free(stp, M_NFSDSTATE);
2682 	NFSEXITCODE2(0, nd);
2683 	return (0);
2684 nfsmout:
2685 	vput(vp);
2686 	if (stp)
2687 		free(stp, M_NFSDSTATE);
2688 	NFSEXITCODE2(error, nd);
2689 	return (error);
2690 }
2691 
2692 /*
2693  * nfsv4 unlock service
2694  */
2695 int
2696 nfsrvd_locku(struct nfsrv_descript *nd, __unused int isdgram,
2697     vnode_t vp, struct nfsexstuff *exp)
2698 {
2699 	u_int32_t *tl;
2700 	int i;
2701 	struct nfsstate *stp;
2702 	struct nfslock *lop;
2703 	int error = 0;
2704 	nfsv4stateid_t stateid;
2705 	nfsquad_t clientid;
2706 	u_int64_t len;
2707 	struct thread *p = curthread;
2708 
2709 	NFSM_DISSECT(tl, u_int32_t *, 6 * NFSX_UNSIGNED + NFSX_STATEID);
2710 	stp = malloc(sizeof (struct nfsstate),
2711 	    M_NFSDSTATE, M_WAITOK);
2712 	lop = malloc(sizeof (struct nfslock),
2713 	    M_NFSDLOCK, M_WAITOK);
2714 	stp->ls_flags = NFSLCK_UNLOCK;
2715 	lop->lo_flags = NFSLCK_UNLOCK;
2716 	stp->ls_op = nd->nd_rp;
2717 	i = fxdr_unsigned(int, *tl++);
2718 	switch (i) {
2719 	case NFSV4LOCKT_READW:
2720 		stp->ls_flags |= NFSLCK_BLOCKING;
2721 	case NFSV4LOCKT_READ:
2722 		break;
2723 	case NFSV4LOCKT_WRITEW:
2724 		stp->ls_flags |= NFSLCK_BLOCKING;
2725 	case NFSV4LOCKT_WRITE:
2726 		break;
2727 	default:
2728 		nd->nd_repstat = NFSERR_BADXDR;
2729 		free(stp, M_NFSDSTATE);
2730 		free(lop, M_NFSDLOCK);
2731 		goto nfsmout;
2732 	}
2733 	stp->ls_ownerlen = 0;
2734 	stp->ls_uid = nd->nd_cred->cr_uid;
2735 	stp->ls_seq = fxdr_unsigned(int, *tl++);
2736 	stp->ls_stateid.seqid = fxdr_unsigned(u_int32_t, *tl++);
2737 	NFSBCOPY((caddr_t)tl, (caddr_t)stp->ls_stateid.other,
2738 	    NFSX_STATEIDOTHER);
2739 	tl += (NFSX_STATEIDOTHER / NFSX_UNSIGNED);
2740 
2741 	/*
2742 	 * For the special stateid of other all 0s and seqid == 1, set the
2743 	 * stateid to the current stateid, if it is set.
2744 	 */
2745 	if ((nd->nd_flag & ND_NFSV41) != 0 && stp->ls_stateid.seqid == 1 &&
2746 	    stp->ls_stateid.other[0] == 0 && stp->ls_stateid.other[1] == 0 &&
2747 	    stp->ls_stateid.other[2] == 0) {
2748 		if ((nd->nd_flag & ND_CURSTATEID) != 0) {
2749 			stp->ls_stateid = nd->nd_curstateid;
2750 			stp->ls_stateid.seqid = 0;
2751 		} else {
2752 			nd->nd_repstat = NFSERR_BADSTATEID;
2753 			free(stp, M_NFSDSTATE);
2754 			free(lop, M_NFSDLOCK);
2755 			goto nfsmout;
2756 		}
2757 	}
2758 
2759 	lop->lo_first = fxdr_hyper(tl);
2760 	tl += 2;
2761 	len = fxdr_hyper(tl);
2762 	if (len == NFS64BITSSET) {
2763 		lop->lo_end = NFS64BITSSET;
2764 	} else {
2765 		lop->lo_end = lop->lo_first + len;
2766 		if (lop->lo_end <= lop->lo_first)
2767 			nd->nd_repstat = NFSERR_INVAL;
2768 	}
2769 	clientid.lval[0] = stp->ls_stateid.other[0];
2770 	clientid.lval[1] = stp->ls_stateid.other[1];
2771 	if ((nd->nd_flag & ND_IMPLIEDCLID) != 0) {
2772 		if ((nd->nd_flag & ND_NFSV41) != 0)
2773 			clientid.qval = nd->nd_clientid.qval;
2774 		else if (nd->nd_clientid.qval != clientid.qval)
2775 			printf("EEK6 multiple clids\n");
2776 	} else {
2777 		if ((nd->nd_flag & ND_NFSV41) != 0)
2778 			printf("EEK! no clientid from session\n");
2779 		nd->nd_flag |= ND_IMPLIEDCLID;
2780 		nd->nd_clientid.qval = clientid.qval;
2781 	}
2782 	if (!nd->nd_repstat && vp->v_type != VREG) {
2783 	    if (vp->v_type == VDIR)
2784 		nd->nd_repstat = NFSERR_ISDIR;
2785 	    else
2786 		nd->nd_repstat = NFSERR_INVAL;
2787 	}
2788 	/*
2789 	 * Call nfsrv_lockctrl() even if nd_repstat is set, so that the
2790 	 * seqid# gets incremented. nfsrv_lockctrl() will return the
2791 	 * value of nd_repstat, if it gets that far.
2792 	 */
2793 	nd->nd_repstat = nfsrv_lockctrl(vp, &stp, &lop, NULL, clientid,
2794 	    &stateid, exp, nd, p);
2795 	if (stp)
2796 		free(stp, M_NFSDSTATE);
2797 	if (lop)
2798 		free(lop, M_NFSDLOCK);
2799 	if (!nd->nd_repstat) {
2800 		NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID);
2801 		*tl++ = txdr_unsigned(stateid.seqid);
2802 		NFSBCOPY((caddr_t)stateid.other,(caddr_t)tl,NFSX_STATEIDOTHER);
2803 	}
2804 nfsmout:
2805 	vput(vp);
2806 	NFSEXITCODE2(error, nd);
2807 	return (error);
2808 }
2809 
2810 /*
2811  * nfsv4 open service
2812  */
2813 int
2814 nfsrvd_open(struct nfsrv_descript *nd, __unused int isdgram,
2815     vnode_t dp, vnode_t *vpp, __unused fhandle_t *fhp, struct nfsexstuff *exp)
2816 {
2817 	u_int32_t *tl;
2818 	int i, retext;
2819 	struct nfsstate *stp = NULL;
2820 	int error = 0, create, claim, exclusive_flag = 0, override;
2821 	u_int32_t rflags = NFSV4OPEN_LOCKTYPEPOSIX, acemask;
2822 	int how = NFSCREATE_UNCHECKED;
2823 	int32_t cverf[2], tverf[2] = { 0, 0 };
2824 	vnode_t vp = NULL, dirp = NULL;
2825 	struct nfsvattr nva, dirfor, diraft;
2826 	struct nameidata named;
2827 	nfsv4stateid_t stateid, delegstateid;
2828 	nfsattrbit_t attrbits;
2829 	nfsquad_t clientid;
2830 	char *bufp = NULL;
2831 	u_long *hashp;
2832 	NFSACL_T *aclp = NULL;
2833 	struct thread *p = curthread;
2834 	bool done_namei;
2835 
2836 #ifdef NFS4_ACL_EXTATTR_NAME
2837 	aclp = acl_alloc(M_WAITOK);
2838 	aclp->acl_cnt = 0;
2839 #endif
2840 	NFSZERO_ATTRBIT(&attrbits);
2841 	done_namei = false;
2842 	named.ni_cnd.cn_nameiop = 0;
2843 	NFSM_DISSECT(tl, u_int32_t *, 6 * NFSX_UNSIGNED);
2844 	i = fxdr_unsigned(int, *(tl + 5));
2845 	if (i <= 0 || i > NFSV4_OPAQUELIMIT) {
2846 		nd->nd_repstat = NFSERR_BADXDR;
2847 		goto nfsmout;
2848 	}
2849 	stp = malloc(sizeof (struct nfsstate) + i,
2850 	    M_NFSDSTATE, M_WAITOK);
2851 	stp->ls_ownerlen = i;
2852 	stp->ls_op = nd->nd_rp;
2853 	stp->ls_flags = NFSLCK_OPEN;
2854 	stp->ls_uid = nd->nd_cred->cr_uid;
2855 	stp->ls_seq = fxdr_unsigned(u_int32_t, *tl++);
2856 	i = fxdr_unsigned(int, *tl++);
2857 	retext = 0;
2858 	if ((i & (NFSV4OPEN_WANTDELEGMASK | NFSV4OPEN_WANTSIGNALDELEG |
2859 	    NFSV4OPEN_WANTPUSHDELEG)) != 0 && (nd->nd_flag & ND_NFSV41) != 0) {
2860 		retext = 1;
2861 		/* For now, ignore these. */
2862 		i &= ~(NFSV4OPEN_WANTPUSHDELEG | NFSV4OPEN_WANTSIGNALDELEG);
2863 		switch (i & NFSV4OPEN_WANTDELEGMASK) {
2864 		case NFSV4OPEN_WANTANYDELEG:
2865 			stp->ls_flags |= (NFSLCK_WANTRDELEG |
2866 			    NFSLCK_WANTWDELEG);
2867 			i &= ~NFSV4OPEN_WANTDELEGMASK;
2868 			break;
2869 		case NFSV4OPEN_WANTREADDELEG:
2870 			stp->ls_flags |= NFSLCK_WANTRDELEG;
2871 			i &= ~NFSV4OPEN_WANTDELEGMASK;
2872 			break;
2873 		case NFSV4OPEN_WANTWRITEDELEG:
2874 			stp->ls_flags |= NFSLCK_WANTWDELEG;
2875 			i &= ~NFSV4OPEN_WANTDELEGMASK;
2876 			break;
2877 		case NFSV4OPEN_WANTNODELEG:
2878 			stp->ls_flags |= NFSLCK_WANTNODELEG;
2879 			i &= ~NFSV4OPEN_WANTDELEGMASK;
2880 			break;
2881 		case NFSV4OPEN_WANTCANCEL:
2882 			printf("NFSv4: ignore Open WantCancel\n");
2883 			i &= ~NFSV4OPEN_WANTDELEGMASK;
2884 			break;
2885 		default:
2886 			/* nd_repstat will be set to NFSERR_INVAL below. */
2887 			break;
2888 		}
2889 	}
2890 	switch (i) {
2891 	case NFSV4OPEN_ACCESSREAD:
2892 		stp->ls_flags |= NFSLCK_READACCESS;
2893 		break;
2894 	case NFSV4OPEN_ACCESSWRITE:
2895 		stp->ls_flags |= NFSLCK_WRITEACCESS;
2896 		break;
2897 	case NFSV4OPEN_ACCESSBOTH:
2898 		stp->ls_flags |= (NFSLCK_READACCESS | NFSLCK_WRITEACCESS);
2899 		break;
2900 	default:
2901 		nd->nd_repstat = NFSERR_INVAL;
2902 	}
2903 	i = fxdr_unsigned(int, *tl++);
2904 	switch (i) {
2905 	case NFSV4OPEN_DENYNONE:
2906 		break;
2907 	case NFSV4OPEN_DENYREAD:
2908 		stp->ls_flags |= NFSLCK_READDENY;
2909 		break;
2910 	case NFSV4OPEN_DENYWRITE:
2911 		stp->ls_flags |= NFSLCK_WRITEDENY;
2912 		break;
2913 	case NFSV4OPEN_DENYBOTH:
2914 		stp->ls_flags |= (NFSLCK_READDENY | NFSLCK_WRITEDENY);
2915 		break;
2916 	default:
2917 		nd->nd_repstat = NFSERR_INVAL;
2918 	}
2919 	clientid.lval[0] = *tl++;
2920 	clientid.lval[1] = *tl;
2921 	if ((nd->nd_flag & ND_IMPLIEDCLID) != 0) {
2922 		if ((nd->nd_flag & ND_NFSV41) != 0)
2923 			clientid.qval = nd->nd_clientid.qval;
2924 		else if (nd->nd_clientid.qval != clientid.qval)
2925 			printf("EEK7 multiple clids\n");
2926 	} else {
2927 		if ((nd->nd_flag & ND_NFSV41) != 0)
2928 			printf("EEK! no clientid from session\n");
2929 		nd->nd_flag |= ND_IMPLIEDCLID;
2930 		nd->nd_clientid.qval = clientid.qval;
2931 	}
2932 	error = nfsrv_mtostr(nd, stp->ls_owner, stp->ls_ownerlen);
2933 	if (error)
2934 		goto nfsmout;
2935 	NFSVNO_ATTRINIT(&nva);
2936 	NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2937 	create = fxdr_unsigned(int, *tl);
2938 	if (!nd->nd_repstat)
2939 		nd->nd_repstat = nfsvno_getattr(dp, &dirfor, nd, p, 0, NULL);
2940 	if (create == NFSV4OPEN_CREATE) {
2941 		nva.na_type = VREG;
2942 		nva.na_mode = 0;
2943 		NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2944 		how = fxdr_unsigned(int, *tl);
2945 		switch (how) {
2946 		case NFSCREATE_UNCHECKED:
2947 		case NFSCREATE_GUARDED:
2948 			error = nfsv4_sattr(nd, NULL, &nva, &attrbits, aclp, p);
2949 			if (error)
2950 				goto nfsmout;
2951 			/*
2952 			 * If the na_gid being set is the same as that of
2953 			 * the directory it is going in, clear it, since
2954 			 * that is what will be set by default. This allows
2955 			 * a user that isn't in that group to do the create.
2956 			 */
2957 			if (!nd->nd_repstat && NFSVNO_ISSETGID(&nva) &&
2958 			    nva.na_gid == dirfor.na_gid)
2959 				NFSVNO_UNSET(&nva, gid);
2960 			if (!nd->nd_repstat)
2961 				nd->nd_repstat = nfsrv_checkuidgid(nd, &nva);
2962 			break;
2963 		case NFSCREATE_EXCLUSIVE:
2964 			NFSM_DISSECT(tl, u_int32_t *, NFSX_VERF);
2965 			cverf[0] = *tl++;
2966 			cverf[1] = *tl;
2967 			break;
2968 		case NFSCREATE_EXCLUSIVE41:
2969 			NFSM_DISSECT(tl, u_int32_t *, NFSX_VERF);
2970 			cverf[0] = *tl++;
2971 			cverf[1] = *tl;
2972 			error = nfsv4_sattr(nd, NULL, &nva, &attrbits, aclp, p);
2973 			if (error != 0)
2974 				goto nfsmout;
2975 			if (NFSISSET_ATTRBIT(&attrbits,
2976 			    NFSATTRBIT_TIMEACCESSSET))
2977 				nd->nd_repstat = NFSERR_INVAL;
2978 			/*
2979 			 * If the na_gid being set is the same as that of
2980 			 * the directory it is going in, clear it, since
2981 			 * that is what will be set by default. This allows
2982 			 * a user that isn't in that group to do the create.
2983 			 */
2984 			if (nd->nd_repstat == 0 && NFSVNO_ISSETGID(&nva) &&
2985 			    nva.na_gid == dirfor.na_gid)
2986 				NFSVNO_UNSET(&nva, gid);
2987 			if (nd->nd_repstat == 0)
2988 				nd->nd_repstat = nfsrv_checkuidgid(nd, &nva);
2989 			break;
2990 		default:
2991 			nd->nd_repstat = NFSERR_BADXDR;
2992 			goto nfsmout;
2993 		}
2994 	} else if (create != NFSV4OPEN_NOCREATE) {
2995 		nd->nd_repstat = NFSERR_BADXDR;
2996 		goto nfsmout;
2997 	}
2998 
2999 	/*
3000 	 * Now, handle the claim, which usually includes looking up a
3001 	 * name in the directory referenced by dp. The exception is
3002 	 * NFSV4OPEN_CLAIMPREVIOUS.
3003 	 */
3004 	NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3005 	claim = fxdr_unsigned(int, *tl);
3006 	if (claim == NFSV4OPEN_CLAIMDELEGATECUR || claim ==
3007 	    NFSV4OPEN_CLAIMDELEGATECURFH) {
3008 		NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID);
3009 		stateid.seqid = fxdr_unsigned(u_int32_t, *tl++);
3010 		NFSBCOPY((caddr_t)tl,(caddr_t)stateid.other,NFSX_STATEIDOTHER);
3011 		stp->ls_flags |= NFSLCK_DELEGCUR;
3012 	} else if (claim == NFSV4OPEN_CLAIMDELEGATEPREV || claim ==
3013 	    NFSV4OPEN_CLAIMDELEGATEPREVFH) {
3014 		stp->ls_flags |= NFSLCK_DELEGPREV;
3015 	}
3016 	if (claim == NFSV4OPEN_CLAIMNULL || claim == NFSV4OPEN_CLAIMDELEGATECUR
3017 	    || claim == NFSV4OPEN_CLAIMDELEGATEPREV) {
3018 		if (!nd->nd_repstat && create == NFSV4OPEN_CREATE &&
3019 		    claim != NFSV4OPEN_CLAIMNULL)
3020 			nd->nd_repstat = NFSERR_INVAL;
3021 		if (nd->nd_repstat) {
3022 			nd->nd_repstat = nfsrv_opencheck(clientid,
3023 			    &stateid, stp, NULL, nd, p, nd->nd_repstat);
3024 			goto nfsmout;
3025 		}
3026 		if (create == NFSV4OPEN_CREATE)
3027 		    NFSNAMEICNDSET(&named.ni_cnd, nd->nd_cred, CREATE,
3028 			LOCKPARENT | LOCKLEAF | NOCACHE);
3029 		else
3030 		    NFSNAMEICNDSET(&named.ni_cnd, nd->nd_cred, LOOKUP,
3031 			LOCKLEAF);
3032 		nfsvno_setpathbuf(&named, &bufp, &hashp);
3033 		error = nfsrv_parsename(nd, bufp, hashp, &named.ni_pathlen);
3034 		if (error) {
3035 			vrele(dp);
3036 #ifdef NFS4_ACL_EXTATTR_NAME
3037 			acl_free(aclp);
3038 #endif
3039 			free(stp, M_NFSDSTATE);
3040 			nfsvno_relpathbuf(&named);
3041 			NFSEXITCODE2(error, nd);
3042 			return (error);
3043 		}
3044 		if (!nd->nd_repstat) {
3045 			nd->nd_repstat = nfsvno_namei(nd, &named, dp, 0, exp,
3046 			    &dirp);
3047 		} else {
3048 			vrele(dp);
3049 			nfsvno_relpathbuf(&named);
3050 		}
3051 		if (create == NFSV4OPEN_CREATE) {
3052 		    switch (how) {
3053 		    case NFSCREATE_UNCHECKED:
3054 			if (nd->nd_repstat == 0 && named.ni_vp != NULL) {
3055 				/*
3056 				 * Clear the setable attribute bits, except
3057 				 * for Size, if it is being truncated.
3058 				 */
3059 				NFSZERO_ATTRBIT(&attrbits);
3060 				if (NFSVNO_ISSETSIZE(&nva))
3061 					NFSSETBIT_ATTRBIT(&attrbits,
3062 					    NFSATTRBIT_SIZE);
3063 			}
3064 			break;
3065 		    case NFSCREATE_GUARDED:
3066 			if (nd->nd_repstat == 0 && named.ni_vp != NULL) {
3067 				nd->nd_repstat = EEXIST;
3068 				done_namei = true;
3069 			}
3070 			break;
3071 		    case NFSCREATE_EXCLUSIVE:
3072 			exclusive_flag = 1;
3073 			if (nd->nd_repstat == 0 && named.ni_vp == NULL)
3074 				nva.na_mode = 0;
3075 			break;
3076 		    case NFSCREATE_EXCLUSIVE41:
3077 			exclusive_flag = 1;
3078 			break;
3079 		    }
3080 		}
3081 		nfsvno_open(nd, &named, clientid, &stateid, stp,
3082 		    &exclusive_flag, &nva, cverf, create, aclp, &attrbits,
3083 		    nd->nd_cred, done_namei, exp, &vp);
3084 	} else if (claim == NFSV4OPEN_CLAIMPREVIOUS || claim ==
3085 	    NFSV4OPEN_CLAIMFH || claim == NFSV4OPEN_CLAIMDELEGATECURFH ||
3086 	    claim == NFSV4OPEN_CLAIMDELEGATEPREVFH) {
3087 		if (claim == NFSV4OPEN_CLAIMPREVIOUS) {
3088 			NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3089 			i = fxdr_unsigned(int, *tl);
3090 			switch (i) {
3091 			case NFSV4OPEN_DELEGATEREAD:
3092 				stp->ls_flags |= NFSLCK_DELEGREAD;
3093 				break;
3094 			case NFSV4OPEN_DELEGATEWRITE:
3095 				stp->ls_flags |= NFSLCK_DELEGWRITE;
3096 			case NFSV4OPEN_DELEGATENONE:
3097 				break;
3098 			default:
3099 				nd->nd_repstat = NFSERR_BADXDR;
3100 				goto nfsmout;
3101 			}
3102 			stp->ls_flags |= NFSLCK_RECLAIM;
3103 		} else {
3104 			if (nd->nd_repstat == 0 && create == NFSV4OPEN_CREATE)
3105 				nd->nd_repstat = NFSERR_INVAL;
3106 		}
3107 		vp = dp;
3108 		NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY);
3109 		if (!VN_IS_DOOMED(vp))
3110 			nd->nd_repstat = nfsrv_opencheck(clientid, &stateid,
3111 			    stp, vp, nd, p, nd->nd_repstat);
3112 		else
3113 			nd->nd_repstat = NFSERR_PERM;
3114 	} else {
3115 		nd->nd_repstat = NFSERR_BADXDR;
3116 		goto nfsmout;
3117 	}
3118 
3119 	/*
3120 	 * Do basic access checking.
3121 	 */
3122 	if (!nd->nd_repstat && vp->v_type != VREG) {
3123 		/*
3124 		 * The IETF working group decided that this is the correct
3125 		 * error return for all non-regular files.
3126 		 */
3127 		nd->nd_repstat = (vp->v_type == VDIR) ? NFSERR_ISDIR : NFSERR_SYMLINK;
3128 	}
3129 
3130 	/*
3131 	 * If the Open is being done for a file that already exists, apply
3132 	 * normal permission checking including for the file owner, if
3133 	 * vfs.nfsd.v4openaccess is set.
3134 	 * Previously, the owner was always allowed to open the file to
3135 	 * be consistent with the NFS tradition of always allowing the
3136 	 * owner of the file to write to the file regardless of permissions.
3137 	 * It now appears that the Linux client expects the owner
3138 	 * permissions to be checked for opens that are not creating the
3139 	 * file.  I believe the correct approach is to use the Access
3140 	 * operation's results to be consistent with NFSv3, but that is
3141 	 * not what the current Linux client appears to be doing.
3142 	 * Since both the Linux and OpenSolaris NFSv4 servers do this check,
3143 	 * I have enabled it by default.  Since Linux does not apply this
3144 	 * check for claim_delegate_cur, this code does the same.
3145 	 * If this semantic change causes a problem, it can be disabled by
3146 	 * setting the sysctl vfs.nfsd.v4openaccess to 0 to re-enable the
3147 	 * previous semantics.
3148 	 */
3149 	if (nfsrv_openaccess && create == NFSV4OPEN_NOCREATE &&
3150 	    (stp->ls_flags & NFSLCK_DELEGCUR) == 0)
3151 		override = NFSACCCHK_NOOVERRIDE;
3152 	else
3153 		override = NFSACCCHK_ALLOWOWNER;
3154 	if (!nd->nd_repstat && (stp->ls_flags & NFSLCK_WRITEACCESS))
3155 	    nd->nd_repstat = nfsvno_accchk(vp, VWRITE, nd->nd_cred,
3156 	        exp, p, override, NFSACCCHK_VPISLOCKED, NULL);
3157 	if (!nd->nd_repstat && (stp->ls_flags & NFSLCK_READACCESS)) {
3158 	    nd->nd_repstat = nfsvno_accchk(vp, VREAD, nd->nd_cred,
3159 	        exp, p, override, NFSACCCHK_VPISLOCKED, NULL);
3160 	    if (nd->nd_repstat)
3161 		nd->nd_repstat = nfsvno_accchk(vp, VEXEC,
3162 		    nd->nd_cred, exp, p, override,
3163 		    NFSACCCHK_VPISLOCKED, NULL);
3164 	}
3165 
3166 	if (!nd->nd_repstat) {
3167 		nd->nd_repstat = nfsvno_getattr(vp, &nva, nd, p, 1, NULL);
3168 		if (!nd->nd_repstat) {
3169 			tverf[0] = nva.na_atime.tv_sec;
3170 			tverf[1] = nva.na_atime.tv_nsec;
3171 		}
3172 	}
3173 	if (!nd->nd_repstat && exclusive_flag && (cverf[0] != tverf[0] ||
3174 	    cverf[1] != tverf[1]))
3175 		nd->nd_repstat = EEXIST;
3176 	/*
3177 	 * Do the open locking/delegation stuff.
3178 	 */
3179 	if (!nd->nd_repstat)
3180 	    nd->nd_repstat = nfsrv_openctrl(nd, vp, &stp, clientid, &stateid,
3181 		&delegstateid, &rflags, exp, p, nva.na_filerev);
3182 
3183 	/*
3184 	 * vp must be unlocked before the call to nfsvno_getattr(dirp,...)
3185 	 * below, to avoid a deadlock with the lookup in nfsvno_namei() above.
3186 	 * (ie: Leave the NFSVOPUNLOCK() about here.)
3187 	 */
3188 	if (vp)
3189 		NFSVOPUNLOCK(vp);
3190 	if (stp)
3191 		free(stp, M_NFSDSTATE);
3192 	if (!nd->nd_repstat && dirp)
3193 		nd->nd_repstat = nfsvno_getattr(dirp, &diraft, nd, p, 0, NULL);
3194 	if (!nd->nd_repstat) {
3195 		/* For NFSv4.1, set the Current StateID. */
3196 		if ((nd->nd_flag & ND_NFSV41) != 0) {
3197 			nd->nd_curstateid = stateid;
3198 			nd->nd_flag |= ND_CURSTATEID;
3199 		}
3200 		NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID + 6 * NFSX_UNSIGNED);
3201 		*tl++ = txdr_unsigned(stateid.seqid);
3202 		NFSBCOPY((caddr_t)stateid.other,(caddr_t)tl,NFSX_STATEIDOTHER);
3203 		tl += (NFSX_STATEIDOTHER / NFSX_UNSIGNED);
3204 		if (claim == NFSV4OPEN_CLAIMPREVIOUS) {
3205 			*tl++ = newnfs_true;
3206 			*tl++ = 0;
3207 			*tl++ = 0;
3208 			*tl++ = 0;
3209 			*tl++ = 0;
3210 		} else {
3211 			*tl++ = newnfs_false;	/* Since dirp is not locked */
3212 			txdr_hyper(dirfor.na_filerev, tl);
3213 			tl += 2;
3214 			txdr_hyper(diraft.na_filerev, tl);
3215 			tl += 2;
3216 		}
3217 		*tl = txdr_unsigned(rflags & NFSV4OPEN_RFLAGS);
3218 		(void) nfsrv_putattrbit(nd, &attrbits);
3219 		NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
3220 		if (rflags & NFSV4OPEN_READDELEGATE)
3221 			*tl = txdr_unsigned(NFSV4OPEN_DELEGATEREAD);
3222 		else if (rflags & NFSV4OPEN_WRITEDELEGATE)
3223 			*tl = txdr_unsigned(NFSV4OPEN_DELEGATEWRITE);
3224 		else if (retext != 0) {
3225 			*tl = txdr_unsigned(NFSV4OPEN_DELEGATENONEEXT);
3226 			if ((rflags & NFSV4OPEN_WDNOTWANTED) != 0) {
3227 				NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
3228 				*tl = txdr_unsigned(NFSV4OPEN_NOTWANTED);
3229 			} else if ((rflags & NFSV4OPEN_WDSUPPFTYPE) != 0) {
3230 				NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
3231 				*tl = txdr_unsigned(NFSV4OPEN_NOTSUPPFTYPE);
3232 			} else if ((rflags & NFSV4OPEN_WDCONTENTION) != 0) {
3233 				NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
3234 				*tl++ = txdr_unsigned(NFSV4OPEN_CONTENTION);
3235 				*tl = newnfs_false;
3236 			} else if ((rflags & NFSV4OPEN_WDRESOURCE) != 0) {
3237 				NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
3238 				*tl++ = txdr_unsigned(NFSV4OPEN_RESOURCE);
3239 				*tl = newnfs_false;
3240 			} else {
3241 				NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
3242 				*tl = txdr_unsigned(NFSV4OPEN_NOTWANTED);
3243 			}
3244 		} else
3245 			*tl = txdr_unsigned(NFSV4OPEN_DELEGATENONE);
3246 		if (rflags & (NFSV4OPEN_READDELEGATE|NFSV4OPEN_WRITEDELEGATE)) {
3247 			NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID+NFSX_UNSIGNED);
3248 			*tl++ = txdr_unsigned(delegstateid.seqid);
3249 			NFSBCOPY((caddr_t)delegstateid.other, (caddr_t)tl,
3250 			    NFSX_STATEIDOTHER);
3251 			tl += (NFSX_STATEIDOTHER / NFSX_UNSIGNED);
3252 			if (rflags & NFSV4OPEN_RECALL)
3253 				*tl = newnfs_true;
3254 			else
3255 				*tl = newnfs_false;
3256 			if (rflags & NFSV4OPEN_WRITEDELEGATE) {
3257 				NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
3258 				*tl++ = txdr_unsigned(NFSV4OPEN_LIMITSIZE);
3259 				txdr_hyper(nva.na_size, tl);
3260 			}
3261 			NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
3262 			*tl++ = txdr_unsigned(NFSV4ACE_ALLOWEDTYPE);
3263 			*tl++ = txdr_unsigned(0x0);
3264 			acemask = NFSV4ACE_ALLFILESMASK;
3265 			if (nva.na_mode & S_IRUSR)
3266 			    acemask |= NFSV4ACE_READMASK;
3267 			if (nva.na_mode & S_IWUSR)
3268 			    acemask |= NFSV4ACE_WRITEMASK;
3269 			if (nva.na_mode & S_IXUSR)
3270 			    acemask |= NFSV4ACE_EXECUTEMASK;
3271 			*tl = txdr_unsigned(acemask);
3272 			(void) nfsm_strtom(nd, "OWNER@", 6);
3273 		}
3274 		*vpp = vp;
3275 	} else if (vp) {
3276 		vrele(vp);
3277 	}
3278 	if (dirp)
3279 		vrele(dirp);
3280 #ifdef NFS4_ACL_EXTATTR_NAME
3281 	acl_free(aclp);
3282 #endif
3283 	NFSEXITCODE2(0, nd);
3284 	return (0);
3285 nfsmout:
3286 	vrele(dp);
3287 #ifdef NFS4_ACL_EXTATTR_NAME
3288 	acl_free(aclp);
3289 #endif
3290 	if (stp)
3291 		free(stp, M_NFSDSTATE);
3292 	NFSEXITCODE2(error, nd);
3293 	return (error);
3294 }
3295 
3296 /*
3297  * nfsv4 close service
3298  */
3299 int
3300 nfsrvd_close(struct nfsrv_descript *nd, __unused int isdgram,
3301     vnode_t vp, __unused struct nfsexstuff *exp)
3302 {
3303 	u_int32_t *tl;
3304 	struct nfsstate st, *stp = &st;
3305 	int error = 0, writeacc;
3306 	nfsv4stateid_t stateid;
3307 	nfsquad_t clientid;
3308 	struct nfsvattr na;
3309 	struct thread *p = curthread;
3310 
3311 	NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED + NFSX_STATEID);
3312 	stp->ls_seq = fxdr_unsigned(u_int32_t, *tl++);
3313 	stp->ls_ownerlen = 0;
3314 	stp->ls_op = nd->nd_rp;
3315 	stp->ls_uid = nd->nd_cred->cr_uid;
3316 	stp->ls_stateid.seqid = fxdr_unsigned(u_int32_t, *tl++);
3317 	NFSBCOPY((caddr_t)tl, (caddr_t)stp->ls_stateid.other,
3318 	    NFSX_STATEIDOTHER);
3319 
3320 	/*
3321 	 * For the special stateid of other all 0s and seqid == 1, set the
3322 	 * stateid to the current stateid, if it is set.
3323 	 */
3324 	if ((nd->nd_flag & ND_NFSV41) != 0 && stp->ls_stateid.seqid == 1 &&
3325 	    stp->ls_stateid.other[0] == 0 && stp->ls_stateid.other[1] == 0 &&
3326 	    stp->ls_stateid.other[2] == 0) {
3327 		if ((nd->nd_flag & ND_CURSTATEID) != 0)
3328 			stp->ls_stateid = nd->nd_curstateid;
3329 		else {
3330 			nd->nd_repstat = NFSERR_BADSTATEID;
3331 			goto nfsmout;
3332 		}
3333 	}
3334 
3335 	stp->ls_flags = NFSLCK_CLOSE;
3336 	clientid.lval[0] = stp->ls_stateid.other[0];
3337 	clientid.lval[1] = stp->ls_stateid.other[1];
3338 	if ((nd->nd_flag & ND_IMPLIEDCLID) != 0) {
3339 		if ((nd->nd_flag & ND_NFSV41) != 0)
3340 			clientid.qval = nd->nd_clientid.qval;
3341 		else if (nd->nd_clientid.qval != clientid.qval)
3342 			printf("EEK8 multiple clids\n");
3343 	} else {
3344 		if ((nd->nd_flag & ND_NFSV41) != 0)
3345 			printf("EEK! no clientid from session\n");
3346 		nd->nd_flag |= ND_IMPLIEDCLID;
3347 		nd->nd_clientid.qval = clientid.qval;
3348 	}
3349 	nd->nd_repstat = nfsrv_openupdate(vp, stp, clientid, &stateid, nd, p,
3350 	    &writeacc);
3351 	/* For pNFS, update the attributes. */
3352 	if (writeacc != 0 || nfsrv_pnfsatime != 0)
3353 		nfsrv_updatemdsattr(vp, &na, p);
3354 	vput(vp);
3355 	if (!nd->nd_repstat) {
3356 		/*
3357 		 * If the stateid that has been closed is the current stateid,
3358 		 * unset it.
3359 		 */
3360 		if ((nd->nd_flag & ND_CURSTATEID) != 0 &&
3361 		    stateid.other[0] == nd->nd_curstateid.other[0] &&
3362 		    stateid.other[1] == nd->nd_curstateid.other[1] &&
3363 		    stateid.other[2] == nd->nd_curstateid.other[2])
3364 			nd->nd_flag &= ~ND_CURSTATEID;
3365 		NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID);
3366 		*tl++ = txdr_unsigned(stateid.seqid);
3367 		NFSBCOPY((caddr_t)stateid.other,(caddr_t)tl,NFSX_STATEIDOTHER);
3368 	}
3369 	NFSEXITCODE2(0, nd);
3370 	return (0);
3371 nfsmout:
3372 	vput(vp);
3373 	NFSEXITCODE2(error, nd);
3374 	return (error);
3375 }
3376 
3377 /*
3378  * nfsv4 delegpurge service
3379  */
3380 int
3381 nfsrvd_delegpurge(struct nfsrv_descript *nd, __unused int isdgram,
3382     __unused vnode_t vp, __unused struct nfsexstuff *exp)
3383 {
3384 	u_int32_t *tl;
3385 	int error = 0;
3386 	nfsquad_t clientid;
3387 	struct thread *p = curthread;
3388 
3389 	if ((nd->nd_repstat = nfsd_checkrootexp(nd)) != 0)
3390 		goto nfsmout;
3391 	NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
3392 	clientid.lval[0] = *tl++;
3393 	clientid.lval[1] = *tl;
3394 	if ((nd->nd_flag & ND_IMPLIEDCLID) != 0) {
3395 		if ((nd->nd_flag & ND_NFSV41) != 0)
3396 			clientid.qval = nd->nd_clientid.qval;
3397 		else if (nd->nd_clientid.qval != clientid.qval)
3398 			printf("EEK9 multiple clids\n");
3399 	} else {
3400 		if ((nd->nd_flag & ND_NFSV41) != 0)
3401 			printf("EEK! no clientid from session\n");
3402 		nd->nd_flag |= ND_IMPLIEDCLID;
3403 		nd->nd_clientid.qval = clientid.qval;
3404 	}
3405 	nd->nd_repstat = nfsrv_delegupdate(nd, clientid, NULL, NULL,
3406 	    NFSV4OP_DELEGPURGE, nd->nd_cred, p, NULL);
3407 nfsmout:
3408 	NFSEXITCODE2(error, nd);
3409 	return (error);
3410 }
3411 
3412 /*
3413  * nfsv4 delegreturn service
3414  */
3415 int
3416 nfsrvd_delegreturn(struct nfsrv_descript *nd, __unused int isdgram,
3417     vnode_t vp, __unused struct nfsexstuff *exp)
3418 {
3419 	u_int32_t *tl;
3420 	int error = 0, writeacc;
3421 	nfsv4stateid_t stateid;
3422 	nfsquad_t clientid;
3423 	struct nfsvattr na;
3424 	struct thread *p = curthread;
3425 
3426 	NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID);
3427 	stateid.seqid = fxdr_unsigned(u_int32_t, *tl++);
3428 	NFSBCOPY((caddr_t)tl, (caddr_t)stateid.other, NFSX_STATEIDOTHER);
3429 	clientid.lval[0] = stateid.other[0];
3430 	clientid.lval[1] = stateid.other[1];
3431 	if ((nd->nd_flag & ND_IMPLIEDCLID) != 0) {
3432 		if ((nd->nd_flag & ND_NFSV41) != 0)
3433 			clientid.qval = nd->nd_clientid.qval;
3434 		else if (nd->nd_clientid.qval != clientid.qval)
3435 			printf("EEK10 multiple clids\n");
3436 	} else {
3437 		if ((nd->nd_flag & ND_NFSV41) != 0)
3438 			printf("EEK! no clientid from session\n");
3439 		nd->nd_flag |= ND_IMPLIEDCLID;
3440 		nd->nd_clientid.qval = clientid.qval;
3441 	}
3442 	nd->nd_repstat = nfsrv_delegupdate(nd, clientid, &stateid, vp,
3443 	    NFSV4OP_DELEGRETURN, nd->nd_cred, p, &writeacc);
3444 	/* For pNFS, update the attributes. */
3445 	if (writeacc != 0 || nfsrv_pnfsatime != 0)
3446 		nfsrv_updatemdsattr(vp, &na, p);
3447 nfsmout:
3448 	vput(vp);
3449 	NFSEXITCODE2(error, nd);
3450 	return (error);
3451 }
3452 
3453 /*
3454  * nfsv4 get file handle service
3455  */
3456 int
3457 nfsrvd_getfh(struct nfsrv_descript *nd, __unused int isdgram,
3458     vnode_t vp, __unused struct nfsexstuff *exp)
3459 {
3460 	fhandle_t fh;
3461 	struct thread *p = curthread;
3462 
3463 	nd->nd_repstat = nfsvno_getfh(vp, &fh, p);
3464 	vput(vp);
3465 	if (!nd->nd_repstat)
3466 		(void)nfsm_fhtom(NULL, nd, (u_int8_t *)&fh, 0, 0);
3467 	NFSEXITCODE2(0, nd);
3468 	return (0);
3469 }
3470 
3471 /*
3472  * nfsv4 open confirm service
3473  */
3474 int
3475 nfsrvd_openconfirm(struct nfsrv_descript *nd, __unused int isdgram,
3476     vnode_t vp, __unused struct nfsexstuff *exp)
3477 {
3478 	u_int32_t *tl;
3479 	struct nfsstate st, *stp = &st;
3480 	int error = 0;
3481 	nfsv4stateid_t stateid;
3482 	nfsquad_t clientid;
3483 	struct thread *p = curthread;
3484 
3485 	if ((nd->nd_flag & ND_NFSV41) != 0) {
3486 		nd->nd_repstat = NFSERR_NOTSUPP;
3487 		goto nfsmout;
3488 	}
3489 	NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID + NFSX_UNSIGNED);
3490 	stp->ls_ownerlen = 0;
3491 	stp->ls_op = nd->nd_rp;
3492 	stp->ls_uid = nd->nd_cred->cr_uid;
3493 	stp->ls_stateid.seqid = fxdr_unsigned(u_int32_t, *tl++);
3494 	NFSBCOPY((caddr_t)tl, (caddr_t)stp->ls_stateid.other,
3495 	    NFSX_STATEIDOTHER);
3496 	tl += (NFSX_STATEIDOTHER / NFSX_UNSIGNED);
3497 	stp->ls_seq = fxdr_unsigned(u_int32_t, *tl);
3498 	stp->ls_flags = NFSLCK_CONFIRM;
3499 	clientid.lval[0] = stp->ls_stateid.other[0];
3500 	clientid.lval[1] = stp->ls_stateid.other[1];
3501 	if ((nd->nd_flag & ND_IMPLIEDCLID) != 0) {
3502 		if ((nd->nd_flag & ND_NFSV41) != 0)
3503 			clientid.qval = nd->nd_clientid.qval;
3504 		else if (nd->nd_clientid.qval != clientid.qval)
3505 			printf("EEK11 multiple clids\n");
3506 	} else {
3507 		if ((nd->nd_flag & ND_NFSV41) != 0)
3508 			printf("EEK! no clientid from session\n");
3509 		nd->nd_flag |= ND_IMPLIEDCLID;
3510 		nd->nd_clientid.qval = clientid.qval;
3511 	}
3512 	nd->nd_repstat = nfsrv_openupdate(vp, stp, clientid, &stateid, nd, p,
3513 	    NULL);
3514 	if (!nd->nd_repstat) {
3515 		NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID);
3516 		*tl++ = txdr_unsigned(stateid.seqid);
3517 		NFSBCOPY((caddr_t)stateid.other,(caddr_t)tl,NFSX_STATEIDOTHER);
3518 	}
3519 nfsmout:
3520 	vput(vp);
3521 	NFSEXITCODE2(error, nd);
3522 	return (error);
3523 }
3524 
3525 /*
3526  * nfsv4 open downgrade service
3527  */
3528 int
3529 nfsrvd_opendowngrade(struct nfsrv_descript *nd, __unused int isdgram,
3530     vnode_t vp, __unused struct nfsexstuff *exp)
3531 {
3532 	u_int32_t *tl;
3533 	int i;
3534 	struct nfsstate st, *stp = &st;
3535 	int error = 0;
3536 	nfsv4stateid_t stateid;
3537 	nfsquad_t clientid;
3538 	struct thread *p = curthread;
3539 
3540 	/* opendowngrade can only work on a file object.*/
3541 	if (vp->v_type != VREG) {
3542 		error = NFSERR_INVAL;
3543 		goto nfsmout;
3544 	}
3545 	NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID + 3 * NFSX_UNSIGNED);
3546 	stp->ls_ownerlen = 0;
3547 	stp->ls_op = nd->nd_rp;
3548 	stp->ls_uid = nd->nd_cred->cr_uid;
3549 	stp->ls_stateid.seqid = fxdr_unsigned(u_int32_t, *tl++);
3550 	NFSBCOPY((caddr_t)tl, (caddr_t)stp->ls_stateid.other,
3551 	    NFSX_STATEIDOTHER);
3552 	tl += (NFSX_STATEIDOTHER / NFSX_UNSIGNED);
3553 
3554 	/*
3555 	 * For the special stateid of other all 0s and seqid == 1, set the
3556 	 * stateid to the current stateid, if it is set.
3557 	 */
3558 	if ((nd->nd_flag & ND_NFSV41) != 0 && stp->ls_stateid.seqid == 1 &&
3559 	    stp->ls_stateid.other[0] == 0 && stp->ls_stateid.other[1] == 0 &&
3560 	    stp->ls_stateid.other[2] == 0) {
3561 		if ((nd->nd_flag & ND_CURSTATEID) != 0)
3562 			stp->ls_stateid = nd->nd_curstateid;
3563 		else {
3564 			nd->nd_repstat = NFSERR_BADSTATEID;
3565 			goto nfsmout;
3566 		}
3567 	}
3568 
3569 	stp->ls_seq = fxdr_unsigned(u_int32_t, *tl++);
3570 	i = fxdr_unsigned(int, *tl++);
3571 	if ((nd->nd_flag & ND_NFSV41) != 0)
3572 		i &= ~NFSV4OPEN_WANTDELEGMASK;
3573 	switch (i) {
3574 	case NFSV4OPEN_ACCESSREAD:
3575 		stp->ls_flags = (NFSLCK_READACCESS | NFSLCK_DOWNGRADE);
3576 		break;
3577 	case NFSV4OPEN_ACCESSWRITE:
3578 		stp->ls_flags = (NFSLCK_WRITEACCESS | NFSLCK_DOWNGRADE);
3579 		break;
3580 	case NFSV4OPEN_ACCESSBOTH:
3581 		stp->ls_flags = (NFSLCK_READACCESS | NFSLCK_WRITEACCESS |
3582 		    NFSLCK_DOWNGRADE);
3583 		break;
3584 	default:
3585 		nd->nd_repstat = NFSERR_INVAL;
3586 	}
3587 	i = fxdr_unsigned(int, *tl);
3588 	switch (i) {
3589 	case NFSV4OPEN_DENYNONE:
3590 		break;
3591 	case NFSV4OPEN_DENYREAD:
3592 		stp->ls_flags |= NFSLCK_READDENY;
3593 		break;
3594 	case NFSV4OPEN_DENYWRITE:
3595 		stp->ls_flags |= NFSLCK_WRITEDENY;
3596 		break;
3597 	case NFSV4OPEN_DENYBOTH:
3598 		stp->ls_flags |= (NFSLCK_READDENY | NFSLCK_WRITEDENY);
3599 		break;
3600 	default:
3601 		nd->nd_repstat = NFSERR_INVAL;
3602 	}
3603 
3604 	clientid.lval[0] = stp->ls_stateid.other[0];
3605 	clientid.lval[1] = stp->ls_stateid.other[1];
3606 	if ((nd->nd_flag & ND_IMPLIEDCLID) != 0) {
3607 		if ((nd->nd_flag & ND_NFSV41) != 0)
3608 			clientid.qval = nd->nd_clientid.qval;
3609 		else if (nd->nd_clientid.qval != clientid.qval)
3610 			printf("EEK12 multiple clids\n");
3611 	} else {
3612 		if ((nd->nd_flag & ND_NFSV41) != 0)
3613 			printf("EEK! no clientid from session\n");
3614 		nd->nd_flag |= ND_IMPLIEDCLID;
3615 		nd->nd_clientid.qval = clientid.qval;
3616 	}
3617 	if (!nd->nd_repstat)
3618 		nd->nd_repstat = nfsrv_openupdate(vp, stp, clientid, &stateid,
3619 		    nd, p, NULL);
3620 	if (!nd->nd_repstat) {
3621 		/* For NFSv4.1, set the Current StateID. */
3622 		if ((nd->nd_flag & ND_NFSV41) != 0) {
3623 			nd->nd_curstateid = stateid;
3624 			nd->nd_flag |= ND_CURSTATEID;
3625 		}
3626 		NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID);
3627 		*tl++ = txdr_unsigned(stateid.seqid);
3628 		NFSBCOPY((caddr_t)stateid.other,(caddr_t)tl,NFSX_STATEIDOTHER);
3629 	}
3630 nfsmout:
3631 	vput(vp);
3632 	NFSEXITCODE2(error, nd);
3633 	return (error);
3634 }
3635 
3636 /*
3637  * nfsv4 renew lease service
3638  */
3639 int
3640 nfsrvd_renew(struct nfsrv_descript *nd, __unused int isdgram,
3641     __unused vnode_t vp, __unused struct nfsexstuff *exp)
3642 {
3643 	u_int32_t *tl;
3644 	int error = 0;
3645 	nfsquad_t clientid;
3646 	struct thread *p = curthread;
3647 
3648 	if ((nd->nd_flag & ND_NFSV41) != 0) {
3649 		nd->nd_repstat = NFSERR_NOTSUPP;
3650 		goto nfsmout;
3651 	}
3652 	if ((nd->nd_repstat = nfsd_checkrootexp(nd)) != 0)
3653 		goto nfsmout;
3654 	NFSM_DISSECT(tl, u_int32_t *, NFSX_HYPER);
3655 	clientid.lval[0] = *tl++;
3656 	clientid.lval[1] = *tl;
3657 	if ((nd->nd_flag & ND_IMPLIEDCLID) != 0) {
3658 		if ((nd->nd_flag & ND_NFSV41) != 0)
3659 			clientid.qval = nd->nd_clientid.qval;
3660 		else if (nd->nd_clientid.qval != clientid.qval)
3661 			printf("EEK13 multiple clids\n");
3662 	} else {
3663 		if ((nd->nd_flag & ND_NFSV41) != 0)
3664 			printf("EEK! no clientid from session\n");
3665 		nd->nd_flag |= ND_IMPLIEDCLID;
3666 		nd->nd_clientid.qval = clientid.qval;
3667 	}
3668 	nd->nd_repstat = nfsrv_getclient(clientid, (CLOPS_RENEWOP|CLOPS_RENEW),
3669 	    NULL, NULL, (nfsquad_t)((u_quad_t)0), 0, nd, p);
3670 nfsmout:
3671 	NFSEXITCODE2(error, nd);
3672 	return (error);
3673 }
3674 
3675 /*
3676  * nfsv4 security info service
3677  */
3678 int
3679 nfsrvd_secinfo(struct nfsrv_descript *nd, int isdgram,
3680     vnode_t dp, struct nfsexstuff *exp)
3681 {
3682 	u_int32_t *tl;
3683 	int len;
3684 	struct nameidata named;
3685 	vnode_t dirp = NULL, vp;
3686 	struct nfsrvfh fh;
3687 	struct nfsexstuff retnes;
3688 	u_int32_t *sizp;
3689 	int error = 0, i;
3690 	uint64_t savflag;
3691 	char *bufp;
3692 	u_long *hashp;
3693 	struct thread *p = curthread;
3694 
3695 	/*
3696 	 * All this just to get the export flags for the name.
3697 	 */
3698 	NFSNAMEICNDSET(&named.ni_cnd, nd->nd_cred, LOOKUP,
3699 	    LOCKLEAF);
3700 	nfsvno_setpathbuf(&named, &bufp, &hashp);
3701 	error = nfsrv_parsename(nd, bufp, hashp, &named.ni_pathlen);
3702 	if (error) {
3703 		vput(dp);
3704 		nfsvno_relpathbuf(&named);
3705 		goto out;
3706 	}
3707 	if (!nd->nd_repstat) {
3708 		nd->nd_repstat = nfsvno_namei(nd, &named, dp, 1, exp, &dirp);
3709 	} else {
3710 		vput(dp);
3711 		nfsvno_relpathbuf(&named);
3712 	}
3713 	if (dirp)
3714 		vrele(dirp);
3715 	if (nd->nd_repstat)
3716 		goto out;
3717 	nfsvno_relpathbuf(&named);
3718 	fh.nfsrvfh_len = NFSX_MYFH;
3719 	vp = named.ni_vp;
3720 	nd->nd_repstat = nfsvno_getfh(vp, (fhandle_t *)fh.nfsrvfh_data, p);
3721 	vput(vp);
3722 	savflag = nd->nd_flag;
3723 	if (!nd->nd_repstat) {
3724 		/*
3725 		 * Pretend the next op is Secinfo, so that no wrongsec
3726 		 * test will be done.
3727 		 */
3728 		nfsd_fhtovp(nd, &fh, LK_SHARED, &vp, &retnes, NULL, 0,
3729 		    NFSV4OP_SECINFO);
3730 		if (vp)
3731 			vput(vp);
3732 	}
3733 	nd->nd_flag = savflag;
3734 	if (nd->nd_repstat)
3735 		goto out;
3736 
3737 	/*
3738 	 * Finally have the export flags for name, so we can create
3739 	 * the security info.
3740 	 */
3741 	len = 0;
3742 	NFSM_BUILD(sizp, u_int32_t *, NFSX_UNSIGNED);
3743 
3744 	/* If nes_numsecflavor == 0, all are allowed. */
3745 	if (retnes.nes_numsecflavor == 0) {
3746 		NFSM_BUILD(tl, uint32_t *, 2 * NFSX_UNSIGNED);
3747 		*tl++ = txdr_unsigned(RPCAUTH_UNIX);
3748 		*tl = txdr_unsigned(RPCAUTH_GSS);
3749 		nfsm_strtom(nd, nfsgss_mechlist[KERBV_MECH].str,
3750 		    nfsgss_mechlist[KERBV_MECH].len);
3751 		NFSM_BUILD(tl, uint32_t *, 3 * NFSX_UNSIGNED);
3752 		*tl++ = txdr_unsigned(GSS_KERBV_QOP);
3753 		*tl++ = txdr_unsigned(RPCAUTHGSS_SVCNONE);
3754 		*tl = txdr_unsigned(RPCAUTH_GSS);
3755 		nfsm_strtom(nd, nfsgss_mechlist[KERBV_MECH].str,
3756 		    nfsgss_mechlist[KERBV_MECH].len);
3757 		NFSM_BUILD(tl, uint32_t *, 3 * NFSX_UNSIGNED);
3758 		*tl++ = txdr_unsigned(GSS_KERBV_QOP);
3759 		*tl++ = txdr_unsigned(RPCAUTHGSS_SVCINTEGRITY);
3760 		*tl = txdr_unsigned(RPCAUTH_GSS);
3761 		nfsm_strtom(nd, nfsgss_mechlist[KERBV_MECH].str,
3762 		    nfsgss_mechlist[KERBV_MECH].len);
3763 		NFSM_BUILD(tl, uint32_t *, 2 * NFSX_UNSIGNED);
3764 		*tl++ = txdr_unsigned(GSS_KERBV_QOP);
3765 		*tl = txdr_unsigned(RPCAUTHGSS_SVCPRIVACY);
3766 		len = 4;
3767 	}
3768 	for (i = 0; i < retnes.nes_numsecflavor; i++) {
3769 		if (retnes.nes_secflavors[i] == AUTH_SYS) {
3770 			NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
3771 			*tl = txdr_unsigned(RPCAUTH_UNIX);
3772 			len++;
3773 		} else if (retnes.nes_secflavors[i] == RPCSEC_GSS_KRB5) {
3774 			NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
3775 			*tl++ = txdr_unsigned(RPCAUTH_GSS);
3776 			(void) nfsm_strtom(nd, nfsgss_mechlist[KERBV_MECH].str,
3777 			    nfsgss_mechlist[KERBV_MECH].len);
3778 			NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
3779 			*tl++ = txdr_unsigned(GSS_KERBV_QOP);
3780 			*tl = txdr_unsigned(RPCAUTHGSS_SVCNONE);
3781 			len++;
3782 		} else if (retnes.nes_secflavors[i] == RPCSEC_GSS_KRB5I) {
3783 			NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
3784 			*tl++ = txdr_unsigned(RPCAUTH_GSS);
3785 			(void) nfsm_strtom(nd, nfsgss_mechlist[KERBV_MECH].str,
3786 			    nfsgss_mechlist[KERBV_MECH].len);
3787 			NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
3788 			*tl++ = txdr_unsigned(GSS_KERBV_QOP);
3789 			*tl = txdr_unsigned(RPCAUTHGSS_SVCINTEGRITY);
3790 			len++;
3791 		} else if (retnes.nes_secflavors[i] == RPCSEC_GSS_KRB5P) {
3792 			NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
3793 			*tl++ = txdr_unsigned(RPCAUTH_GSS);
3794 			(void) nfsm_strtom(nd, nfsgss_mechlist[KERBV_MECH].str,
3795 			    nfsgss_mechlist[KERBV_MECH].len);
3796 			NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
3797 			*tl++ = txdr_unsigned(GSS_KERBV_QOP);
3798 			*tl = txdr_unsigned(RPCAUTHGSS_SVCPRIVACY);
3799 			len++;
3800 		}
3801 	}
3802 	*sizp = txdr_unsigned(len);
3803 
3804 out:
3805 	NFSEXITCODE2(error, nd);
3806 	return (error);
3807 }
3808 
3809 /*
3810  * nfsv4 security info no name service
3811  */
3812 int
3813 nfsrvd_secinfononame(struct nfsrv_descript *nd, int isdgram,
3814     vnode_t dp, struct nfsexstuff *exp)
3815 {
3816 	uint32_t *tl, *sizp;
3817 	struct nameidata named;
3818 	vnode_t dirp = NULL, vp;
3819 	struct nfsrvfh fh;
3820 	struct nfsexstuff retnes;
3821 	int error = 0, fhstyle, i, len;
3822 	uint64_t savflag;
3823 	char *bufp;
3824 	u_long *hashp;
3825 	struct thread *p = curthread;
3826 
3827 	NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
3828 	fhstyle = fxdr_unsigned(int, *tl);
3829 	switch (fhstyle) {
3830 	case NFSSECINFONONAME_PARENT:
3831 		if (dp->v_type != VDIR) {
3832 			vput(dp);
3833 			nd->nd_repstat = NFSERR_NOTDIR;
3834 			goto nfsmout;
3835 		}
3836 		NFSNAMEICNDSET(&named.ni_cnd, nd->nd_cred, LOOKUP,
3837 		    LOCKLEAF);
3838 		nfsvno_setpathbuf(&named, &bufp, &hashp);
3839 		error = nfsrv_parsename(nd, bufp, hashp, &named.ni_pathlen);
3840 		if (error != 0) {
3841 			vput(dp);
3842 			nfsvno_relpathbuf(&named);
3843 			goto nfsmout;
3844 		}
3845 		if (nd->nd_repstat == 0)
3846 			nd->nd_repstat = nfsvno_namei(nd, &named, dp, 1, exp, &dirp);
3847 		else
3848 			vput(dp);
3849 		if (dirp != NULL)
3850 			vrele(dirp);
3851 		nfsvno_relpathbuf(&named);
3852 		vp = named.ni_vp;
3853 		break;
3854 	case NFSSECINFONONAME_CURFH:
3855 		vp = dp;
3856 		break;
3857 	default:
3858 		nd->nd_repstat = NFSERR_INVAL;
3859 		vput(dp);
3860 	}
3861 	if (nd->nd_repstat != 0)
3862 		goto nfsmout;
3863 	fh.nfsrvfh_len = NFSX_MYFH;
3864 	nd->nd_repstat = nfsvno_getfh(vp, (fhandle_t *)fh.nfsrvfh_data, p);
3865 	vput(vp);
3866 	savflag = nd->nd_flag;
3867 	if (nd->nd_repstat == 0) {
3868 		/*
3869 		 * Pretend the next op is Secinfo, so that no wrongsec
3870 		 * test will be done.
3871 		 */
3872 		nfsd_fhtovp(nd, &fh, LK_SHARED, &vp, &retnes, NULL, 0,
3873 		    NFSV4OP_SECINFO);
3874 		if (vp != NULL)
3875 			vput(vp);
3876 	}
3877 	nd->nd_flag = savflag;
3878 	if (nd->nd_repstat != 0)
3879 		goto nfsmout;
3880 
3881 	/*
3882 	 * Finally have the export flags for fh/parent, so we can create
3883 	 * the security info.
3884 	 */
3885 	len = 0;
3886 	NFSM_BUILD(sizp, uint32_t *, NFSX_UNSIGNED);
3887 
3888 	/* If nes_numsecflavor == 0, all are allowed. */
3889 	if (retnes.nes_numsecflavor == 0) {
3890 		NFSM_BUILD(tl, uint32_t *, 2 * NFSX_UNSIGNED);
3891 		*tl++ = txdr_unsigned(RPCAUTH_UNIX);
3892 		*tl = txdr_unsigned(RPCAUTH_GSS);
3893 		nfsm_strtom(nd, nfsgss_mechlist[KERBV_MECH].str,
3894 		    nfsgss_mechlist[KERBV_MECH].len);
3895 		NFSM_BUILD(tl, uint32_t *, 3 * NFSX_UNSIGNED);
3896 		*tl++ = txdr_unsigned(GSS_KERBV_QOP);
3897 		*tl++ = txdr_unsigned(RPCAUTHGSS_SVCNONE);
3898 		*tl = txdr_unsigned(RPCAUTH_GSS);
3899 		nfsm_strtom(nd, nfsgss_mechlist[KERBV_MECH].str,
3900 		    nfsgss_mechlist[KERBV_MECH].len);
3901 		NFSM_BUILD(tl, uint32_t *, 3 * NFSX_UNSIGNED);
3902 		*tl++ = txdr_unsigned(GSS_KERBV_QOP);
3903 		*tl++ = txdr_unsigned(RPCAUTHGSS_SVCINTEGRITY);
3904 		*tl = txdr_unsigned(RPCAUTH_GSS);
3905 		nfsm_strtom(nd, nfsgss_mechlist[KERBV_MECH].str,
3906 		    nfsgss_mechlist[KERBV_MECH].len);
3907 		NFSM_BUILD(tl, uint32_t *, 2 * NFSX_UNSIGNED);
3908 		*tl++ = txdr_unsigned(GSS_KERBV_QOP);
3909 		*tl = txdr_unsigned(RPCAUTHGSS_SVCPRIVACY);
3910 		len = 4;
3911 	}
3912 	for (i = 0; i < retnes.nes_numsecflavor; i++) {
3913 		if (retnes.nes_secflavors[i] == AUTH_SYS) {
3914 			NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
3915 			*tl = txdr_unsigned(RPCAUTH_UNIX);
3916 			len++;
3917 		} else if (retnes.nes_secflavors[i] == RPCSEC_GSS_KRB5) {
3918 			NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
3919 			*tl = txdr_unsigned(RPCAUTH_GSS);
3920 			nfsm_strtom(nd, nfsgss_mechlist[KERBV_MECH].str,
3921 			    nfsgss_mechlist[KERBV_MECH].len);
3922 			NFSM_BUILD(tl, uint32_t *, 2 * NFSX_UNSIGNED);
3923 			*tl++ = txdr_unsigned(GSS_KERBV_QOP);
3924 			*tl = txdr_unsigned(RPCAUTHGSS_SVCNONE);
3925 			len++;
3926 		} else if (retnes.nes_secflavors[i] == RPCSEC_GSS_KRB5I) {
3927 			NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
3928 			*tl = txdr_unsigned(RPCAUTH_GSS);
3929 			nfsm_strtom(nd, nfsgss_mechlist[KERBV_MECH].str,
3930 			    nfsgss_mechlist[KERBV_MECH].len);
3931 			NFSM_BUILD(tl, uint32_t *, 2 * NFSX_UNSIGNED);
3932 			*tl++ = txdr_unsigned(GSS_KERBV_QOP);
3933 			*tl = txdr_unsigned(RPCAUTHGSS_SVCINTEGRITY);
3934 			len++;
3935 		} else if (retnes.nes_secflavors[i] == RPCSEC_GSS_KRB5P) {
3936 			NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
3937 			*tl = txdr_unsigned(RPCAUTH_GSS);
3938 			nfsm_strtom(nd, nfsgss_mechlist[KERBV_MECH].str,
3939 			    nfsgss_mechlist[KERBV_MECH].len);
3940 			NFSM_BUILD(tl, uint32_t *, 2 * NFSX_UNSIGNED);
3941 			*tl++ = txdr_unsigned(GSS_KERBV_QOP);
3942 			*tl = txdr_unsigned(RPCAUTHGSS_SVCPRIVACY);
3943 			len++;
3944 		}
3945 	}
3946 	*sizp = txdr_unsigned(len);
3947 
3948 nfsmout:
3949 	NFSEXITCODE2(error, nd);
3950 	return (error);
3951 }
3952 
3953 /*
3954  * nfsv4 set client id service
3955  */
3956 int
3957 nfsrvd_setclientid(struct nfsrv_descript *nd, __unused int isdgram,
3958     __unused vnode_t vp, __unused struct nfsexstuff *exp)
3959 {
3960 	u_int32_t *tl;
3961 	int i;
3962 	int error = 0, idlen;
3963 	struct nfsclient *clp = NULL;
3964 #ifdef INET
3965 	struct sockaddr_in *rin;
3966 #endif
3967 #ifdef INET6
3968 	struct sockaddr_in6 *rin6;
3969 #endif
3970 #if defined(INET) || defined(INET6)
3971 	u_char *ucp, *ucp2;
3972 #endif
3973 	u_char *verf, *addrbuf;
3974 	nfsquad_t clientid, confirm;
3975 	struct thread *p = curthread;
3976 
3977 	if ((nd->nd_flag & ND_NFSV41) != 0) {
3978 		nd->nd_repstat = NFSERR_NOTSUPP;
3979 		goto nfsmout;
3980 	}
3981 	if ((nd->nd_repstat = nfsd_checkrootexp(nd)) != 0)
3982 		goto out;
3983 	NFSM_DISSECT(tl, u_int32_t *, NFSX_VERF + NFSX_UNSIGNED);
3984 	verf = (u_char *)tl;
3985 	tl += (NFSX_VERF / NFSX_UNSIGNED);
3986 	i = fxdr_unsigned(int, *tl);
3987 	if (i > NFSV4_OPAQUELIMIT || i <= 0) {
3988 		nd->nd_repstat = NFSERR_BADXDR;
3989 		goto nfsmout;
3990 	}
3991 	idlen = i;
3992 	if (nd->nd_flag & ND_GSS)
3993 		i += nd->nd_princlen;
3994 	clp = malloc(sizeof(struct nfsclient) + i, M_NFSDCLIENT, M_WAITOK |
3995 	    M_ZERO);
3996 	clp->lc_stateid = malloc(sizeof(struct nfsstatehead) *
3997 	    nfsrv_statehashsize, M_NFSDCLIENT, M_WAITOK);
3998 	NFSINITSOCKMUTEX(&clp->lc_req.nr_mtx);
3999 	/* Allocated large enough for an AF_INET or AF_INET6 socket. */
4000 	clp->lc_req.nr_nam = malloc(sizeof(struct sockaddr_in6), M_SONAME,
4001 	    M_WAITOK | M_ZERO);
4002 	clp->lc_req.nr_cred = NULL;
4003 	NFSBCOPY(verf, clp->lc_verf, NFSX_VERF);
4004 	clp->lc_idlen = idlen;
4005 	error = nfsrv_mtostr(nd, clp->lc_id, idlen);
4006 	if (error)
4007 		goto nfsmout;
4008 	if (nd->nd_flag & ND_GSS) {
4009 		clp->lc_flags = LCL_GSS;
4010 		if (nd->nd_flag & ND_GSSINTEGRITY)
4011 			clp->lc_flags |= LCL_GSSINTEGRITY;
4012 		else if (nd->nd_flag & ND_GSSPRIVACY)
4013 			clp->lc_flags |= LCL_GSSPRIVACY;
4014 	} else {
4015 		clp->lc_flags = 0;
4016 	}
4017 	if ((nd->nd_flag & ND_GSS) && nd->nd_princlen > 0) {
4018 		clp->lc_flags |= LCL_NAME;
4019 		clp->lc_namelen = nd->nd_princlen;
4020 		clp->lc_name = &clp->lc_id[idlen];
4021 		NFSBCOPY(nd->nd_principal, clp->lc_name, clp->lc_namelen);
4022 	} else {
4023 		clp->lc_uid = nd->nd_cred->cr_uid;
4024 		clp->lc_gid = nd->nd_cred->cr_gid;
4025 	}
4026 
4027 	/* If the client is using TLS, do so for the callback connection. */
4028 	if (nd->nd_flag & ND_TLS)
4029 		clp->lc_flags |= LCL_TLSCB;
4030 
4031 	NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
4032 	clp->lc_program = fxdr_unsigned(u_int32_t, *tl);
4033 	error = nfsrv_getclientipaddr(nd, clp);
4034 	if (error)
4035 		goto nfsmout;
4036 	NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
4037 	clp->lc_callback = fxdr_unsigned(u_int32_t, *tl);
4038 
4039 	/*
4040 	 * nfsrv_setclient() does the actual work of adding it to the
4041 	 * client list. If there is no error, the structure has been
4042 	 * linked into the client list and clp should no longer be used
4043 	 * here. When an error is returned, it has not been linked in,
4044 	 * so it should be free'd.
4045 	 */
4046 	nd->nd_repstat = nfsrv_setclient(nd, &clp, &clientid, &confirm, p);
4047 	if (nd->nd_repstat == NFSERR_CLIDINUSE) {
4048 		/*
4049 		 * 8 is the maximum length of the port# string.
4050 		 */
4051 		addrbuf = malloc(INET6_ADDRSTRLEN + 8, M_TEMP, M_WAITOK);
4052 		switch (clp->lc_req.nr_nam->sa_family) {
4053 #ifdef INET
4054 		case AF_INET:
4055 			if (clp->lc_flags & LCL_TCPCALLBACK)
4056 				(void) nfsm_strtom(nd, "tcp", 3);
4057 			else
4058 				(void) nfsm_strtom(nd, "udp", 3);
4059 			rin = (struct sockaddr_in *)clp->lc_req.nr_nam;
4060 			ucp = (u_char *)&rin->sin_addr.s_addr;
4061 			ucp2 = (u_char *)&rin->sin_port;
4062 			sprintf(addrbuf, "%d.%d.%d.%d.%d.%d", ucp[0] & 0xff,
4063 			    ucp[1] & 0xff, ucp[2] & 0xff, ucp[3] & 0xff,
4064 			    ucp2[0] & 0xff, ucp2[1] & 0xff);
4065 			break;
4066 #endif
4067 #ifdef INET6
4068 		case AF_INET6:
4069 			if (clp->lc_flags & LCL_TCPCALLBACK)
4070 				(void) nfsm_strtom(nd, "tcp6", 4);
4071 			else
4072 				(void) nfsm_strtom(nd, "udp6", 4);
4073 			rin6 = (struct sockaddr_in6 *)clp->lc_req.nr_nam;
4074 			ucp = inet_ntop(AF_INET6, &rin6->sin6_addr, addrbuf,
4075 			    INET6_ADDRSTRLEN);
4076 			if (ucp != NULL)
4077 				i = strlen(ucp);
4078 			else
4079 				i = 0;
4080 			ucp2 = (u_char *)&rin6->sin6_port;
4081 			sprintf(&addrbuf[i], ".%d.%d", ucp2[0] & 0xff,
4082 			    ucp2[1] & 0xff);
4083 			break;
4084 #endif
4085 		}
4086 		(void) nfsm_strtom(nd, addrbuf, strlen(addrbuf));
4087 		free(addrbuf, M_TEMP);
4088 	}
4089 	if (clp) {
4090 		free(clp->lc_req.nr_nam, M_SONAME);
4091 		NFSFREEMUTEX(&clp->lc_req.nr_mtx);
4092 		free(clp->lc_stateid, M_NFSDCLIENT);
4093 		free(clp, M_NFSDCLIENT);
4094 	}
4095 	if (!nd->nd_repstat) {
4096 		NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_HYPER);
4097 		*tl++ = clientid.lval[0];
4098 		*tl++ = clientid.lval[1];
4099 		*tl++ = confirm.lval[0];
4100 		*tl = confirm.lval[1];
4101 	}
4102 
4103 out:
4104 	NFSEXITCODE2(0, nd);
4105 	return (0);
4106 nfsmout:
4107 	if (clp) {
4108 		free(clp->lc_req.nr_nam, M_SONAME);
4109 		NFSFREEMUTEX(&clp->lc_req.nr_mtx);
4110 		free(clp->lc_stateid, M_NFSDCLIENT);
4111 		free(clp, M_NFSDCLIENT);
4112 	}
4113 	NFSEXITCODE2(error, nd);
4114 	return (error);
4115 }
4116 
4117 /*
4118  * nfsv4 set client id confirm service
4119  */
4120 int
4121 nfsrvd_setclientidcfrm(struct nfsrv_descript *nd,
4122     __unused int isdgram, __unused vnode_t vp,
4123     __unused struct nfsexstuff *exp)
4124 {
4125 	u_int32_t *tl;
4126 	int error = 0;
4127 	nfsquad_t clientid, confirm;
4128 	struct thread *p = curthread;
4129 
4130 	if ((nd->nd_flag & ND_NFSV41) != 0) {
4131 		nd->nd_repstat = NFSERR_NOTSUPP;
4132 		goto nfsmout;
4133 	}
4134 	if ((nd->nd_repstat = nfsd_checkrootexp(nd)) != 0)
4135 		goto nfsmout;
4136 	NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_HYPER);
4137 	clientid.lval[0] = *tl++;
4138 	clientid.lval[1] = *tl++;
4139 	confirm.lval[0] = *tl++;
4140 	confirm.lval[1] = *tl;
4141 
4142 	/*
4143 	 * nfsrv_getclient() searches the client list for a match and
4144 	 * returns the appropriate NFSERR status.
4145 	 */
4146 	nd->nd_repstat = nfsrv_getclient(clientid, (CLOPS_CONFIRM|CLOPS_RENEW),
4147 	    NULL, NULL, confirm, 0, nd, p);
4148 nfsmout:
4149 	NFSEXITCODE2(error, nd);
4150 	return (error);
4151 }
4152 
4153 /*
4154  * nfsv4 verify service
4155  */
4156 int
4157 nfsrvd_verify(struct nfsrv_descript *nd, int isdgram,
4158     vnode_t vp, __unused struct nfsexstuff *exp)
4159 {
4160 	int error = 0, ret, fhsize = NFSX_MYFH;
4161 	struct nfsvattr nva;
4162 	struct statfs *sf;
4163 	struct nfsfsinfo fs;
4164 	fhandle_t fh;
4165 	struct thread *p = curthread;
4166 
4167 	sf = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
4168 	nd->nd_repstat = nfsvno_getattr(vp, &nva, nd, p, 1, NULL);
4169 	if (!nd->nd_repstat)
4170 		nd->nd_repstat = nfsvno_statfs(vp, sf);
4171 	if (!nd->nd_repstat)
4172 		nd->nd_repstat = nfsvno_getfh(vp, &fh, p);
4173 	if (!nd->nd_repstat) {
4174 		nfsvno_getfs(&fs, isdgram);
4175 		error = nfsv4_loadattr(nd, vp, &nva, NULL, &fh, fhsize, NULL,
4176 		    sf, NULL, &fs, NULL, 1, &ret, NULL, NULL, p, nd->nd_cred);
4177 		if (!error) {
4178 			if (nd->nd_procnum == NFSV4OP_NVERIFY) {
4179 				if (ret == 0)
4180 					nd->nd_repstat = NFSERR_SAME;
4181 				else if (ret != NFSERR_NOTSAME)
4182 					nd->nd_repstat = ret;
4183 			} else if (ret)
4184 				nd->nd_repstat = ret;
4185 		}
4186 	}
4187 	vput(vp);
4188 	free(sf, M_STATFS);
4189 	NFSEXITCODE2(error, nd);
4190 	return (error);
4191 }
4192 
4193 /*
4194  * nfs openattr rpc
4195  */
4196 int
4197 nfsrvd_openattr(struct nfsrv_descript *nd, __unused int isdgram,
4198     vnode_t dp, __unused vnode_t *vpp, __unused fhandle_t *fhp,
4199     __unused struct nfsexstuff *exp)
4200 {
4201 	u_int32_t *tl;
4202 	int error = 0, createdir __unused;
4203 
4204 	NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
4205 	createdir = fxdr_unsigned(int, *tl);
4206 	nd->nd_repstat = NFSERR_NOTSUPP;
4207 nfsmout:
4208 	vrele(dp);
4209 	NFSEXITCODE2(error, nd);
4210 	return (error);
4211 }
4212 
4213 /*
4214  * nfsv4 release lock owner service
4215  */
4216 int
4217 nfsrvd_releaselckown(struct nfsrv_descript *nd, __unused int isdgram,
4218     __unused vnode_t vp, __unused struct nfsexstuff *exp)
4219 {
4220 	u_int32_t *tl;
4221 	struct nfsstate *stp = NULL;
4222 	int error = 0, len;
4223 	nfsquad_t clientid;
4224 	struct thread *p = curthread;
4225 
4226 	if ((nd->nd_flag & ND_NFSV41) != 0) {
4227 		nd->nd_repstat = NFSERR_NOTSUPP;
4228 		goto nfsmout;
4229 	}
4230 	if ((nd->nd_repstat = nfsd_checkrootexp(nd)) != 0)
4231 		goto nfsmout;
4232 	NFSM_DISSECT(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
4233 	len = fxdr_unsigned(int, *(tl + 2));
4234 	if (len <= 0 || len > NFSV4_OPAQUELIMIT) {
4235 		nd->nd_repstat = NFSERR_BADXDR;
4236 		goto nfsmout;
4237 	}
4238 	stp = malloc(sizeof (struct nfsstate) + len,
4239 	    M_NFSDSTATE, M_WAITOK);
4240 	stp->ls_ownerlen = len;
4241 	stp->ls_op = NULL;
4242 	stp->ls_flags = NFSLCK_RELEASE;
4243 	stp->ls_uid = nd->nd_cred->cr_uid;
4244 	clientid.lval[0] = *tl++;
4245 	clientid.lval[1] = *tl;
4246 	if ((nd->nd_flag & ND_IMPLIEDCLID) != 0) {
4247 		if ((nd->nd_flag & ND_NFSV41) != 0)
4248 			clientid.qval = nd->nd_clientid.qval;
4249 		else if (nd->nd_clientid.qval != clientid.qval)
4250 			printf("EEK14 multiple clids\n");
4251 	} else {
4252 		if ((nd->nd_flag & ND_NFSV41) != 0)
4253 			printf("EEK! no clientid from session\n");
4254 		nd->nd_flag |= ND_IMPLIEDCLID;
4255 		nd->nd_clientid.qval = clientid.qval;
4256 	}
4257 	error = nfsrv_mtostr(nd, stp->ls_owner, len);
4258 	if (error)
4259 		goto nfsmout;
4260 	nd->nd_repstat = nfsrv_releaselckown(stp, clientid, p);
4261 	free(stp, M_NFSDSTATE);
4262 
4263 	NFSEXITCODE2(0, nd);
4264 	return (0);
4265 nfsmout:
4266 	if (stp)
4267 		free(stp, M_NFSDSTATE);
4268 	NFSEXITCODE2(error, nd);
4269 	return (error);
4270 }
4271 
4272 /*
4273  * nfsv4 exchange_id service
4274  */
4275 int
4276 nfsrvd_exchangeid(struct nfsrv_descript *nd, __unused int isdgram,
4277     __unused vnode_t vp, __unused struct nfsexstuff *exp)
4278 {
4279 	uint32_t *tl;
4280 	int error = 0, i, idlen;
4281 	struct nfsclient *clp = NULL;
4282 	nfsquad_t clientid, confirm;
4283 	uint8_t *verf;
4284 	uint32_t sp4type, v41flags;
4285 	struct timespec verstime;
4286 	nfsopbit_t mustops, allowops;
4287 #ifdef INET
4288 	struct sockaddr_in *sin, *rin;
4289 #endif
4290 #ifdef INET6
4291 	struct sockaddr_in6 *sin6, *rin6;
4292 #endif
4293 	struct thread *p = curthread;
4294 	char *s;
4295 
4296 	if ((nd->nd_repstat = nfsd_checkrootexp(nd)) != 0)
4297 		goto nfsmout;
4298 	NFSM_DISSECT(tl, u_int32_t *, NFSX_VERF + NFSX_UNSIGNED);
4299 	verf = (uint8_t *)tl;
4300 	tl += (NFSX_VERF / NFSX_UNSIGNED);
4301 	i = fxdr_unsigned(int, *tl);
4302 	if (i > NFSV4_OPAQUELIMIT || i <= 0) {
4303 		nd->nd_repstat = NFSERR_BADXDR;
4304 		goto nfsmout;
4305 	}
4306 	idlen = i;
4307 	if (nd->nd_flag & ND_GSS)
4308 		i += nd->nd_princlen;
4309 	clp = malloc(sizeof(struct nfsclient) + i, M_NFSDCLIENT, M_WAITOK |
4310 	    M_ZERO);
4311 	clp->lc_stateid = malloc(sizeof(struct nfsstatehead) *
4312 	    nfsrv_statehashsize, M_NFSDCLIENT, M_WAITOK);
4313 	NFSINITSOCKMUTEX(&clp->lc_req.nr_mtx);
4314 	/* Allocated large enough for an AF_INET or AF_INET6 socket. */
4315 	clp->lc_req.nr_nam = malloc(sizeof(struct sockaddr_in6), M_SONAME,
4316 	    M_WAITOK | M_ZERO);
4317 	switch (nd->nd_nam->sa_family) {
4318 #ifdef INET
4319 	case AF_INET:
4320 		rin = (struct sockaddr_in *)clp->lc_req.nr_nam;
4321 		sin = (struct sockaddr_in *)nd->nd_nam;
4322 		rin->sin_family = AF_INET;
4323 		rin->sin_len = sizeof(struct sockaddr_in);
4324 		rin->sin_port = 0;
4325 		rin->sin_addr.s_addr = sin->sin_addr.s_addr;
4326 		break;
4327 #endif
4328 #ifdef INET6
4329 	case AF_INET6:
4330 		rin6 = (struct sockaddr_in6 *)clp->lc_req.nr_nam;
4331 		sin6 = (struct sockaddr_in6 *)nd->nd_nam;
4332 		rin6->sin6_family = AF_INET6;
4333 		rin6->sin6_len = sizeof(struct sockaddr_in6);
4334 		rin6->sin6_port = 0;
4335 		rin6->sin6_addr = sin6->sin6_addr;
4336 		break;
4337 #endif
4338 	}
4339 	clp->lc_req.nr_cred = NULL;
4340 	NFSBCOPY(verf, clp->lc_verf, NFSX_VERF);
4341 	clp->lc_idlen = idlen;
4342 	error = nfsrv_mtostr(nd, clp->lc_id, idlen);
4343 	if (error != 0)
4344 		goto nfsmout;
4345 	if ((nd->nd_flag & ND_GSS) != 0) {
4346 		clp->lc_flags = LCL_GSS | LCL_NFSV41;
4347 		if ((nd->nd_flag & ND_GSSINTEGRITY) != 0)
4348 			clp->lc_flags |= LCL_GSSINTEGRITY;
4349 		else if ((nd->nd_flag & ND_GSSPRIVACY) != 0)
4350 			clp->lc_flags |= LCL_GSSPRIVACY;
4351 	} else
4352 		clp->lc_flags = LCL_NFSV41;
4353 	if ((nd->nd_flag & ND_NFSV42) != 0)
4354 		clp->lc_flags |= LCL_NFSV42;
4355 	if ((nd->nd_flag & ND_GSS) != 0 && nd->nd_princlen > 0) {
4356 		clp->lc_flags |= LCL_NAME;
4357 		clp->lc_namelen = nd->nd_princlen;
4358 		clp->lc_name = &clp->lc_id[idlen];
4359 		NFSBCOPY(nd->nd_principal, clp->lc_name, clp->lc_namelen);
4360 	} else {
4361 		clp->lc_uid = nd->nd_cred->cr_uid;
4362 		clp->lc_gid = nd->nd_cred->cr_gid;
4363 	}
4364 	NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
4365 	v41flags = fxdr_unsigned(uint32_t, *tl++);
4366 	if ((v41flags & ~(NFSV4EXCH_SUPPMOVEDREFER | NFSV4EXCH_SUPPMOVEDMIGR |
4367 	    NFSV4EXCH_BINDPRINCSTATEID | NFSV4EXCH_MASKPNFS |
4368 	    NFSV4EXCH_UPDCONFIRMEDRECA)) != 0) {
4369 		nd->nd_repstat = NFSERR_INVAL;
4370 		goto nfsmout;
4371 	}
4372 	if ((v41flags & NFSV4EXCH_UPDCONFIRMEDRECA) != 0)
4373 		confirm.lval[1] = 1;
4374 	else
4375 		confirm.lval[1] = 0;
4376 	if (nfsrv_devidcnt == 0)
4377 		v41flags = NFSV4EXCH_USENONPNFS | NFSV4EXCH_USEPNFSDS;
4378  	else
4379  		v41flags = NFSV4EXCH_USEPNFSMDS;
4380 	sp4type = fxdr_unsigned(uint32_t, *tl);
4381 	if (sp4type == NFSV4EXCH_SP4MACHCRED) {
4382 		if ((nd->nd_flag & (ND_GSSINTEGRITY | ND_GSSPRIVACY)) == 0 ||
4383 		    nd->nd_princlen == 0)
4384 			nd->nd_repstat = (NFSERR_AUTHERR | AUTH_TOOWEAK);
4385 		if (nd->nd_repstat == 0)
4386 			nd->nd_repstat = nfsrv_getopbits(nd, &mustops, NULL);
4387 		if (nd->nd_repstat == 0)
4388 			nd->nd_repstat = nfsrv_getopbits(nd, &allowops, NULL);
4389 		if (nd->nd_repstat != 0)
4390 			goto nfsmout;
4391 		NFSOPBIT_CLRNOTMUST(&mustops);
4392 		NFSSET_OPBIT(&clp->lc_mustops, &mustops);
4393 		NFSOPBIT_CLRNOTALLOWED(&allowops);
4394 		NFSSET_OPBIT(&clp->lc_allowops, &allowops);
4395 		clp->lc_flags |= LCL_MACHCRED;
4396 	} else if (sp4type != NFSV4EXCH_SP4NONE) {
4397 		nd->nd_repstat = NFSERR_NOTSUPP;
4398 		goto nfsmout;
4399 	}
4400 
4401 	/*
4402 	 * nfsrv_setclient() does the actual work of adding it to the
4403 	 * client list. If there is no error, the structure has been
4404 	 * linked into the client list and clp should no longer be used
4405 	 * here. When an error is returned, it has not been linked in,
4406 	 * so it should be free'd.
4407 	 */
4408 	nd->nd_repstat = nfsrv_setclient(nd, &clp, &clientid, &confirm, p);
4409 	if (clp != NULL) {
4410 		free(clp->lc_req.nr_nam, M_SONAME);
4411 		NFSFREEMUTEX(&clp->lc_req.nr_mtx);
4412 		free(clp->lc_stateid, M_NFSDCLIENT);
4413 		free(clp, M_NFSDCLIENT);
4414 	}
4415 	if (nd->nd_repstat == 0) {
4416 		if (confirm.lval[1] != 0)
4417 			v41flags |= NFSV4EXCH_CONFIRMEDR;
4418 		NFSM_BUILD(tl, uint32_t *, NFSX_HYPER + 3 * NFSX_UNSIGNED);
4419 		*tl++ = clientid.lval[0];			/* ClientID */
4420 		*tl++ = clientid.lval[1];
4421 		*tl++ = txdr_unsigned(confirm.lval[0]);		/* SequenceID */
4422 		*tl++ = txdr_unsigned(v41flags);		/* Exch flags */
4423 		*tl = txdr_unsigned(sp4type);			/* No SSV */
4424 		if (sp4type == NFSV4EXCH_SP4MACHCRED) {
4425 			nfsrv_putopbit(nd, &mustops);
4426 			nfsrv_putopbit(nd, &allowops);
4427 		}
4428 		NFSM_BUILD(tl, uint32_t *, NFSX_HYPER);
4429 		txdr_hyper(nfsrv_owner_minor, tl);	/* Owner Minor */
4430 		if (nfsrv_owner_major[0] != 0)
4431 			s = nfsrv_owner_major;
4432 		else
4433 			s = nd->nd_cred->cr_prison->pr_hostuuid;
4434 		nfsm_strtom(nd, s, strlen(s));		/* Owner Major */
4435 		if (nfsrv_scope[0] != 0)
4436 			s = nfsrv_scope;
4437 		else
4438 			s = nd->nd_cred->cr_prison->pr_hostuuid;
4439 		nfsm_strtom(nd, s, strlen(s)	);		/* Scope */
4440 		NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
4441 		*tl = txdr_unsigned(1);
4442 		(void)nfsm_strtom(nd, "freebsd.org", strlen("freebsd.org"));
4443 		(void)nfsm_strtom(nd, version, strlen(version));
4444 		NFSM_BUILD(tl, uint32_t *, NFSX_V4TIME);
4445 		verstime.tv_sec = 1293840000;		/* Jan 1, 2011 */
4446 		verstime.tv_nsec = 0;
4447 		txdr_nfsv4time(&verstime, tl);
4448 	}
4449 	NFSEXITCODE2(0, nd);
4450 	return (0);
4451 nfsmout:
4452 	if (clp != NULL) {
4453 		free(clp->lc_req.nr_nam, M_SONAME);
4454 		NFSFREEMUTEX(&clp->lc_req.nr_mtx);
4455 		free(clp->lc_stateid, M_NFSDCLIENT);
4456 		free(clp, M_NFSDCLIENT);
4457 	}
4458 	NFSEXITCODE2(error, nd);
4459 	return (error);
4460 }
4461 
4462 /*
4463  * nfsv4 create session service
4464  */
4465 int
4466 nfsrvd_createsession(struct nfsrv_descript *nd, __unused int isdgram,
4467     __unused vnode_t vp, __unused struct nfsexstuff *exp)
4468 {
4469 	uint32_t *tl;
4470 	int error = 0;
4471 	nfsquad_t clientid, confirm;
4472 	struct nfsdsession *sep = NULL;
4473 	uint32_t rdmacnt;
4474 	struct thread *p = curthread;
4475 	static bool do_printf = true;
4476 
4477 	if ((nd->nd_repstat = nfsd_checkrootexp(nd)) != 0)
4478 		goto nfsmout;
4479 	sep = (struct nfsdsession *)malloc(sizeof(struct nfsdsession),
4480 	    M_NFSDSESSION, M_WAITOK | M_ZERO);
4481 	sep->sess_refcnt = 1;
4482 	mtx_init(&sep->sess_cbsess.nfsess_mtx, "nfscbsession", NULL, MTX_DEF);
4483 	NFSM_DISSECT(tl, uint32_t *, NFSX_HYPER + 2 * NFSX_UNSIGNED);
4484 	clientid.lval[0] = *tl++;
4485 	clientid.lval[1] = *tl++;
4486 	confirm.lval[0] = fxdr_unsigned(uint32_t, *tl++);
4487 	sep->sess_crflags = fxdr_unsigned(uint32_t, *tl);
4488 	/* Persistent sessions and RDMA are not supported. */
4489 	sep->sess_crflags &= NFSV4CRSESS_CONNBACKCHAN;
4490 
4491 	/* Fore channel attributes. */
4492 	NFSM_DISSECT(tl, uint32_t *, 7 * NFSX_UNSIGNED);
4493 	tl++;					/* Header pad always 0. */
4494 	sep->sess_maxreq = fxdr_unsigned(uint32_t, *tl++);
4495 	if (sep->sess_maxreq > sb_max_adj - NFS_MAXXDR) {
4496 		sep->sess_maxreq = sb_max_adj - NFS_MAXXDR;
4497 		if (do_printf)
4498 			printf("Consider increasing kern.ipc.maxsockbuf\n");
4499 		do_printf = false;
4500 	}
4501 	sep->sess_maxresp = fxdr_unsigned(uint32_t, *tl++);
4502 	if (sep->sess_maxresp > sb_max_adj - NFS_MAXXDR) {
4503 		sep->sess_maxresp = sb_max_adj - NFS_MAXXDR;
4504 		if (do_printf)
4505 			printf("Consider increasing kern.ipc.maxsockbuf\n");
4506 		do_printf = false;
4507 	}
4508 	sep->sess_maxrespcached = fxdr_unsigned(uint32_t, *tl++);
4509 	sep->sess_maxops = fxdr_unsigned(uint32_t, *tl++);
4510 	sep->sess_maxslots = fxdr_unsigned(uint32_t, *tl++);
4511 	if (sep->sess_maxslots > NFSV4_SLOTS)
4512 		sep->sess_maxslots = NFSV4_SLOTS;
4513 	rdmacnt = fxdr_unsigned(uint32_t, *tl);
4514 	if (rdmacnt > 1) {
4515 		nd->nd_repstat = NFSERR_BADXDR;
4516 		goto nfsmout;
4517 	} else if (rdmacnt == 1)
4518 		NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
4519 
4520 	/* Back channel attributes. */
4521 	NFSM_DISSECT(tl, uint32_t *, 7 * NFSX_UNSIGNED);
4522 	tl++;					/* Header pad always 0. */
4523 	sep->sess_cbmaxreq = fxdr_unsigned(uint32_t, *tl++);
4524 	sep->sess_cbmaxresp = fxdr_unsigned(uint32_t, *tl++);
4525 	sep->sess_cbmaxrespcached = fxdr_unsigned(uint32_t, *tl++);
4526 	sep->sess_cbmaxops = fxdr_unsigned(uint32_t, *tl++);
4527 	sep->sess_cbsess.nfsess_foreslots = fxdr_unsigned(uint32_t, *tl++);
4528 	rdmacnt = fxdr_unsigned(uint32_t, *tl);
4529 	if (rdmacnt > 1) {
4530 		nd->nd_repstat = NFSERR_BADXDR;
4531 		goto nfsmout;
4532 	} else if (rdmacnt == 1)
4533 		NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
4534 
4535 	NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
4536 	sep->sess_cbprogram = fxdr_unsigned(uint32_t, *tl);
4537 
4538 	/*
4539 	 * nfsrv_getclient() searches the client list for a match and
4540 	 * returns the appropriate NFSERR status.
4541 	 */
4542 	nd->nd_repstat = nfsrv_getclient(clientid, CLOPS_CONFIRM | CLOPS_RENEW,
4543 	    NULL, sep, confirm, sep->sess_cbprogram, nd, p);
4544 	if (nd->nd_repstat == 0) {
4545 		NFSM_BUILD(tl, uint32_t *, NFSX_V4SESSIONID);
4546 		NFSBCOPY(sep->sess_sessionid, tl, NFSX_V4SESSIONID);
4547 		NFSM_BUILD(tl, uint32_t *, 18 * NFSX_UNSIGNED);
4548 		*tl++ = txdr_unsigned(confirm.lval[0]);	/* sequenceid */
4549 		*tl++ = txdr_unsigned(sep->sess_crflags);
4550 
4551 		/* Fore channel attributes. */
4552 		*tl++ = 0;
4553 		*tl++ = txdr_unsigned(sep->sess_maxreq);
4554 		*tl++ = txdr_unsigned(sep->sess_maxresp);
4555 		*tl++ = txdr_unsigned(sep->sess_maxrespcached);
4556 		*tl++ = txdr_unsigned(sep->sess_maxops);
4557 		*tl++ = txdr_unsigned(sep->sess_maxslots);
4558 		*tl++ = txdr_unsigned(1);
4559 		*tl++ = txdr_unsigned(0);			/* No RDMA. */
4560 
4561 		/* Back channel attributes. */
4562 		*tl++ = 0;
4563 		*tl++ = txdr_unsigned(sep->sess_cbmaxreq);
4564 		*tl++ = txdr_unsigned(sep->sess_cbmaxresp);
4565 		*tl++ = txdr_unsigned(sep->sess_cbmaxrespcached);
4566 		*tl++ = txdr_unsigned(sep->sess_cbmaxops);
4567 		*tl++ = txdr_unsigned(sep->sess_cbsess.nfsess_foreslots);
4568 		*tl++ = txdr_unsigned(1);
4569 		*tl = txdr_unsigned(0);			/* No RDMA. */
4570 	}
4571 nfsmout:
4572 	if (nd->nd_repstat != 0 && sep != NULL)
4573 		free(sep, M_NFSDSESSION);
4574 	NFSEXITCODE2(error, nd);
4575 	return (error);
4576 }
4577 
4578 /*
4579  * nfsv4 sequence service
4580  */
4581 int
4582 nfsrvd_sequence(struct nfsrv_descript *nd, __unused int isdgram,
4583     __unused vnode_t vp, __unused struct nfsexstuff *exp)
4584 {
4585 	uint32_t *tl;
4586 	uint32_t highest_slotid, sequenceid, sflags, target_highest_slotid;
4587 	int cache_this, error = 0;
4588 	struct thread *p = curthread;
4589 
4590 	if ((nd->nd_repstat = nfsd_checkrootexp(nd)) != 0)
4591 		goto nfsmout;
4592 	NFSM_DISSECT(tl, uint32_t *, NFSX_V4SESSIONID);
4593 	NFSBCOPY(tl, nd->nd_sessionid, NFSX_V4SESSIONID);
4594 	NFSM_DISSECT(tl, uint32_t *, 4 * NFSX_UNSIGNED);
4595 	sequenceid = fxdr_unsigned(uint32_t, *tl++);
4596 	nd->nd_slotid = fxdr_unsigned(uint32_t, *tl++);
4597 	highest_slotid = fxdr_unsigned(uint32_t, *tl++);
4598 	if (*tl == newnfs_true)
4599 		cache_this = 1;
4600 	else
4601 		cache_this = 0;
4602 	nd->nd_repstat = nfsrv_checksequence(nd, sequenceid, &highest_slotid,
4603 	    &target_highest_slotid, cache_this, &sflags, p);
4604 	if (nd->nd_repstat != NFSERR_BADSLOT)
4605 		nd->nd_flag |= ND_HASSEQUENCE;
4606 	if (nd->nd_repstat == 0) {
4607 		NFSM_BUILD(tl, uint32_t *, NFSX_V4SESSIONID);
4608 		NFSBCOPY(nd->nd_sessionid, tl, NFSX_V4SESSIONID);
4609 		NFSM_BUILD(tl, uint32_t *, 5 * NFSX_UNSIGNED);
4610 		*tl++ = txdr_unsigned(sequenceid);
4611 		*tl++ = txdr_unsigned(nd->nd_slotid);
4612 		*tl++ = txdr_unsigned(highest_slotid);
4613 		*tl++ = txdr_unsigned(target_highest_slotid);
4614 		*tl = txdr_unsigned(sflags);
4615 	}
4616 nfsmout:
4617 	NFSEXITCODE2(error, nd);
4618 	return (error);
4619 }
4620 
4621 /*
4622  * nfsv4 reclaim complete service
4623  */
4624 int
4625 nfsrvd_reclaimcomplete(struct nfsrv_descript *nd, __unused int isdgram,
4626     __unused vnode_t vp, __unused struct nfsexstuff *exp)
4627 {
4628 	uint32_t *tl;
4629 	int error = 0, onefs;
4630 
4631 	NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
4632 	/*
4633 	 * I believe that a ReclaimComplete with rca_one_fs == TRUE is only
4634 	 * to be used after a file system has been transferred to a different
4635 	 * file server.  However, RFC5661 is somewhat vague w.r.t. this and
4636 	 * the ESXi 6.7 client does both a ReclaimComplete with rca_one_fs
4637 	 * == TRUE and one with ReclaimComplete with rca_one_fs == FALSE.
4638 	 * Therefore, just ignore the rca_one_fs == TRUE operation and return
4639 	 * NFS_OK without doing anything.
4640 	 */
4641 	onefs = 0;
4642 	if (*tl == newnfs_true)
4643 		onefs = 1;
4644 	nd->nd_repstat = nfsrv_checkreclaimcomplete(nd, onefs);
4645 nfsmout:
4646 	NFSEXITCODE2(error, nd);
4647 	return (error);
4648 }
4649 
4650 /*
4651  * nfsv4 destroy clientid service
4652  */
4653 int
4654 nfsrvd_destroyclientid(struct nfsrv_descript *nd, __unused int isdgram,
4655     __unused vnode_t vp, __unused struct nfsexstuff *exp)
4656 {
4657 	uint32_t *tl;
4658 	nfsquad_t clientid;
4659 	int error = 0;
4660 	struct thread *p = curthread;
4661 
4662 	if ((nd->nd_repstat = nfsd_checkrootexp(nd)) != 0)
4663 		goto nfsmout;
4664 	NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
4665 	clientid.lval[0] = *tl++;
4666 	clientid.lval[1] = *tl;
4667 	nd->nd_repstat = nfsrv_destroyclient(nd, clientid, p);
4668 nfsmout:
4669 	NFSEXITCODE2(error, nd);
4670 	return (error);
4671 }
4672 
4673 /*
4674  * nfsv4 bind connection to session service
4675  */
4676 int
4677 nfsrvd_bindconnsess(struct nfsrv_descript *nd, __unused int isdgram,
4678     __unused vnode_t vp, __unused struct nfsexstuff *exp)
4679 {
4680 	uint32_t *tl;
4681 	uint8_t sessid[NFSX_V4SESSIONID];
4682 	int error = 0, foreaft;
4683 
4684 	if ((nd->nd_repstat = nfsd_checkrootexp(nd)) != 0)
4685 		goto nfsmout;
4686 	NFSM_DISSECT(tl, uint32_t *, NFSX_V4SESSIONID + 2 * NFSX_UNSIGNED);
4687 	NFSBCOPY(tl, sessid, NFSX_V4SESSIONID);
4688 	tl += (NFSX_V4SESSIONID / NFSX_UNSIGNED);
4689 	foreaft = fxdr_unsigned(int, *tl++);
4690 	if (*tl == newnfs_true) {
4691 		/* RDMA is not supported. */
4692 		nd->nd_repstat = NFSERR_NOTSUPP;
4693 		goto nfsmout;
4694 	}
4695 
4696 	nd->nd_repstat = nfsrv_bindconnsess(nd, sessid, &foreaft);
4697 	if (nd->nd_repstat == 0) {
4698 		NFSM_BUILD(tl, uint32_t *, NFSX_V4SESSIONID + 2 *
4699 		    NFSX_UNSIGNED);
4700 		NFSBCOPY(sessid, tl, NFSX_V4SESSIONID);
4701 		tl += (NFSX_V4SESSIONID / NFSX_UNSIGNED);
4702 		*tl++ = txdr_unsigned(foreaft);
4703 		*tl = newnfs_false;
4704 	}
4705 nfsmout:
4706 	NFSEXITCODE2(error, nd);
4707 	return (error);
4708 }
4709 
4710 /*
4711  * nfsv4 destroy session service
4712  */
4713 int
4714 nfsrvd_destroysession(struct nfsrv_descript *nd, __unused int isdgram,
4715     __unused vnode_t vp, __unused struct nfsexstuff *exp)
4716 {
4717 	uint8_t *cp, sessid[NFSX_V4SESSIONID];
4718 	int error = 0;
4719 
4720 	if ((nd->nd_repstat = nfsd_checkrootexp(nd)) != 0)
4721 		goto nfsmout;
4722 	NFSM_DISSECT(cp, uint8_t *, NFSX_V4SESSIONID);
4723 	NFSBCOPY(cp, sessid, NFSX_V4SESSIONID);
4724 	nd->nd_repstat = nfsrv_destroysession(nd, sessid);
4725 nfsmout:
4726 	NFSEXITCODE2(error, nd);
4727 	return (error);
4728 }
4729 
4730 /*
4731  * nfsv4 free stateid service
4732  */
4733 int
4734 nfsrvd_freestateid(struct nfsrv_descript *nd, __unused int isdgram,
4735     __unused vnode_t vp, __unused struct nfsexstuff *exp)
4736 {
4737 	uint32_t *tl;
4738 	nfsv4stateid_t stateid;
4739 	int error = 0;
4740 	struct thread *p = curthread;
4741 
4742 	NFSM_DISSECT(tl, uint32_t *, NFSX_STATEID);
4743 	stateid.seqid = fxdr_unsigned(uint32_t, *tl++);
4744 	NFSBCOPY(tl, stateid.other, NFSX_STATEIDOTHER);
4745 
4746 	/*
4747 	 * For the special stateid of other all 0s and seqid == 1, set the
4748 	 * stateid to the current stateid, if it is set.
4749 	 */
4750 	if (stateid.seqid == 1 && stateid.other[0] == 0 &&
4751 	    stateid.other[1] == 0 && stateid.other[2] == 0) {
4752 		if ((nd->nd_flag & ND_CURSTATEID) != 0) {
4753 			stateid = nd->nd_curstateid;
4754 			stateid.seqid = 0;
4755 		} else {
4756 			nd->nd_repstat = NFSERR_BADSTATEID;
4757 			goto nfsmout;
4758 		}
4759 	}
4760 
4761 	nd->nd_repstat = nfsrv_freestateid(nd, &stateid, p);
4762 
4763 	/* If the current stateid has been free'd, unset it. */
4764 	if (nd->nd_repstat == 0 && (nd->nd_flag & ND_CURSTATEID) != 0 &&
4765 	    stateid.other[0] == nd->nd_curstateid.other[0] &&
4766 	    stateid.other[1] == nd->nd_curstateid.other[1] &&
4767 	    stateid.other[2] == nd->nd_curstateid.other[2])
4768 		nd->nd_flag &= ~ND_CURSTATEID;
4769 nfsmout:
4770 	NFSEXITCODE2(error, nd);
4771 	return (error);
4772 }
4773 
4774 /*
4775  * nfsv4 layoutget service
4776  */
4777 int
4778 nfsrvd_layoutget(struct nfsrv_descript *nd, __unused int isdgram,
4779     vnode_t vp, struct nfsexstuff *exp)
4780 {
4781 	uint32_t *tl;
4782 	nfsv4stateid_t stateid;
4783 	int error = 0, layoutlen, layouttype, iomode, maxcnt, retonclose;
4784 	uint64_t offset, len, minlen;
4785 	char *layp;
4786 	struct thread *p = curthread;
4787 
4788 	NFSM_DISSECT(tl, uint32_t *, 4 * NFSX_UNSIGNED + 3 * NFSX_HYPER +
4789 	    NFSX_STATEID);
4790 	tl++;		/* Signal layout available. Ignore for now. */
4791 	layouttype = fxdr_unsigned(int, *tl++);
4792 	iomode = fxdr_unsigned(int, *tl++);
4793 	offset = fxdr_hyper(tl); tl += 2;
4794 	len = fxdr_hyper(tl); tl += 2;
4795 	minlen = fxdr_hyper(tl); tl += 2;
4796 	stateid.seqid = fxdr_unsigned(uint32_t, *tl++);
4797 	NFSBCOPY(tl, stateid.other, NFSX_STATEIDOTHER);
4798 	tl += (NFSX_STATEIDOTHER / NFSX_UNSIGNED);
4799 	maxcnt = fxdr_unsigned(int, *tl);
4800 	NFSD_DEBUG(4, "layoutget ltyp=%d iom=%d off=%ju len=%ju mlen=%ju\n",
4801 	    layouttype, iomode, (uintmax_t)offset, (uintmax_t)len,
4802 	    (uintmax_t)minlen);
4803 	if (len < minlen ||
4804 	    (minlen != UINT64_MAX && offset + minlen < offset) ||
4805 	    (len != UINT64_MAX && offset + len < offset)) {
4806 		nd->nd_repstat = NFSERR_INVAL;
4807 		goto nfsmout;
4808 	}
4809 
4810 	/*
4811 	 * For the special stateid of other all 0s and seqid == 1, set the
4812 	 * stateid to the current stateid, if it is set.
4813 	 */
4814 	if (stateid.seqid == 1 && stateid.other[0] == 0 &&
4815 	    stateid.other[1] == 0 && stateid.other[2] == 0) {
4816 		if ((nd->nd_flag & ND_CURSTATEID) != 0) {
4817 			stateid = nd->nd_curstateid;
4818 			stateid.seqid = 0;
4819 		} else {
4820 			nd->nd_repstat = NFSERR_BADSTATEID;
4821 			goto nfsmout;
4822 		}
4823 	}
4824 
4825 	layp = NULL;
4826 	if (layouttype == NFSLAYOUT_NFSV4_1_FILES && nfsrv_maxpnfsmirror == 1)
4827 		layp = malloc(NFSX_V4FILELAYOUT, M_TEMP, M_WAITOK);
4828 	else if (layouttype == NFSLAYOUT_FLEXFILE)
4829 		layp = malloc(NFSX_V4FLEXLAYOUT(nfsrv_maxpnfsmirror), M_TEMP,
4830 		    M_WAITOK);
4831 	else
4832 		nd->nd_repstat = NFSERR_UNKNLAYOUTTYPE;
4833 	if (layp != NULL)
4834 		nd->nd_repstat = nfsrv_layoutget(nd, vp, exp, layouttype,
4835 		    &iomode, &offset, &len, minlen, &stateid, maxcnt,
4836 		    &retonclose, &layoutlen, layp, nd->nd_cred, p);
4837 	NFSD_DEBUG(4, "nfsrv_layoutget stat=%u layoutlen=%d\n", nd->nd_repstat,
4838 	    layoutlen);
4839 	if (nd->nd_repstat == 0) {
4840 		/* For NFSv4.1, set the Current StateID. */
4841 		if ((nd->nd_flag & ND_NFSV41) != 0) {
4842 			nd->nd_curstateid = stateid;
4843 			nd->nd_flag |= ND_CURSTATEID;
4844 		}
4845 		NFSM_BUILD(tl, uint32_t *, 4 * NFSX_UNSIGNED + NFSX_STATEID +
4846 		    2 * NFSX_HYPER);
4847 		*tl++ = txdr_unsigned(retonclose);
4848 		*tl++ = txdr_unsigned(stateid.seqid);
4849 		NFSBCOPY(stateid.other, tl, NFSX_STATEIDOTHER);
4850 		tl += (NFSX_STATEIDOTHER / NFSX_UNSIGNED);
4851 		*tl++ = txdr_unsigned(1);	/* Only returns one layout. */
4852 		txdr_hyper(offset, tl); tl += 2;
4853 		txdr_hyper(len, tl); tl += 2;
4854 		*tl++ = txdr_unsigned(iomode);
4855 		*tl = txdr_unsigned(layouttype);
4856 		nfsm_strtom(nd, layp, layoutlen);
4857 	} else if (nd->nd_repstat == NFSERR_LAYOUTTRYLATER) {
4858 		NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
4859 		*tl = newnfs_false;
4860 	}
4861 	free(layp, M_TEMP);
4862 nfsmout:
4863 	vput(vp);
4864 	NFSEXITCODE2(error, nd);
4865 	return (error);
4866 }
4867 
4868 /*
4869  * nfsv4 layoutcommit service
4870  */
4871 int
4872 nfsrvd_layoutcommit(struct nfsrv_descript *nd, __unused int isdgram,
4873     vnode_t vp, struct nfsexstuff *exp)
4874 {
4875 	uint32_t *tl;
4876 	nfsv4stateid_t stateid;
4877 	int error = 0, hasnewoff, hasnewmtime, layouttype, maxcnt, reclaim;
4878 	int hasnewsize;
4879 	uint64_t offset, len, newoff = 0, newsize;
4880 	struct timespec newmtime;
4881 	char *layp;
4882 	struct thread *p = curthread;
4883 
4884 	layp = NULL;
4885 	NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED + 2 * NFSX_HYPER +
4886 	    NFSX_STATEID);
4887 	offset = fxdr_hyper(tl); tl += 2;
4888 	len = fxdr_hyper(tl); tl += 2;
4889 	reclaim = fxdr_unsigned(int, *tl++);
4890 	stateid.seqid = fxdr_unsigned(uint32_t, *tl++);
4891 	NFSBCOPY(tl, stateid.other, NFSX_STATEIDOTHER);
4892 	tl += (NFSX_STATEIDOTHER / NFSX_UNSIGNED);
4893 	/*
4894 	 * For the special stateid of other all 0s and seqid == 1, set the
4895 	 * stateid to the current stateid, if it is set.
4896 	 */
4897 	if (stateid.seqid == 1 && stateid.other[0] == 0 &&
4898 	    stateid.other[1] == 0 && stateid.other[2] == 0) {
4899 		if ((nd->nd_flag & ND_CURSTATEID) != 0) {
4900 			stateid = nd->nd_curstateid;
4901 			stateid.seqid = 0;
4902 		} else {
4903 			nd->nd_repstat = NFSERR_BADSTATEID;
4904 			goto nfsmout;
4905 		}
4906 	}
4907 
4908 	hasnewoff = fxdr_unsigned(int, *tl);
4909 	if (hasnewoff != 0) {
4910 		NFSM_DISSECT(tl, uint32_t *, NFSX_HYPER + NFSX_UNSIGNED);
4911 		newoff = fxdr_hyper(tl); tl += 2;
4912 	} else
4913 		NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
4914 	hasnewmtime = fxdr_unsigned(int, *tl);
4915 	if (hasnewmtime != 0) {
4916 		NFSM_DISSECT(tl, uint32_t *, NFSX_V4TIME + 2 * NFSX_UNSIGNED);
4917 		fxdr_nfsv4time(tl, &newmtime);
4918 		tl += (NFSX_V4TIME / NFSX_UNSIGNED);
4919 	} else
4920 		NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
4921 	layouttype = fxdr_unsigned(int, *tl++);
4922 	maxcnt = fxdr_unsigned(int, *tl);
4923 	if (maxcnt > 0) {
4924 		layp = malloc(maxcnt + 1, M_TEMP, M_WAITOK);
4925 		error = nfsrv_mtostr(nd, layp, maxcnt);
4926 		if (error != 0)
4927 			goto nfsmout;
4928 	}
4929 	nd->nd_repstat = nfsrv_layoutcommit(nd, vp, layouttype, hasnewoff,
4930 	    newoff, offset, len, hasnewmtime, &newmtime, reclaim, &stateid,
4931 	    maxcnt, layp, &hasnewsize, &newsize, nd->nd_cred, p);
4932 	NFSD_DEBUG(4, "nfsrv_layoutcommit stat=%u\n", nd->nd_repstat);
4933 	if (nd->nd_repstat == 0) {
4934 		if (hasnewsize != 0) {
4935 			NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED + NFSX_HYPER);
4936 			*tl++ = newnfs_true;
4937 			txdr_hyper(newsize, tl);
4938 		} else {
4939 			NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
4940 			*tl = newnfs_false;
4941 		}
4942 	}
4943 nfsmout:
4944 	free(layp, M_TEMP);
4945 	vput(vp);
4946 	NFSEXITCODE2(error, nd);
4947 	return (error);
4948 }
4949 
4950 /*
4951  * nfsv4 layoutreturn service
4952  */
4953 int
4954 nfsrvd_layoutreturn(struct nfsrv_descript *nd, __unused int isdgram,
4955     vnode_t vp, struct nfsexstuff *exp)
4956 {
4957 	uint32_t *tl, *layp;
4958 	nfsv4stateid_t stateid;
4959 	int error = 0, fnd, kind, layouttype, iomode, maxcnt, reclaim;
4960 	uint64_t offset, len;
4961 	struct thread *p = curthread;
4962 
4963 	layp = NULL;
4964 	NFSM_DISSECT(tl, uint32_t *, 4 * NFSX_UNSIGNED);
4965 	reclaim = *tl++;
4966 	layouttype = fxdr_unsigned(int, *tl++);
4967 	iomode = fxdr_unsigned(int, *tl++);
4968 	kind = fxdr_unsigned(int, *tl);
4969 	NFSD_DEBUG(4, "layoutreturn recl=%d ltyp=%d iom=%d kind=%d\n", reclaim,
4970 	    layouttype, iomode, kind);
4971 	if (kind == NFSV4LAYOUTRET_FILE) {
4972 		NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_HYPER + NFSX_STATEID +
4973 		    NFSX_UNSIGNED);
4974 		offset = fxdr_hyper(tl); tl += 2;
4975 		len = fxdr_hyper(tl); tl += 2;
4976 		stateid.seqid = fxdr_unsigned(uint32_t, *tl++);
4977 		NFSBCOPY(tl, stateid.other, NFSX_STATEIDOTHER);
4978 		tl += (NFSX_STATEIDOTHER / NFSX_UNSIGNED);
4979 
4980 		/*
4981 		 * For the special stateid of other all 0s and seqid == 1, set
4982 		 * the stateid to the current stateid, if it is set.
4983 		 */
4984 		if (stateid.seqid == 1 && stateid.other[0] == 0 &&
4985 		    stateid.other[1] == 0 && stateid.other[2] == 0) {
4986 			if ((nd->nd_flag & ND_CURSTATEID) != 0) {
4987 				stateid = nd->nd_curstateid;
4988 				stateid.seqid = 0;
4989 			} else {
4990 				nd->nd_repstat = NFSERR_BADSTATEID;
4991 				goto nfsmout;
4992 			}
4993 		}
4994 
4995 		maxcnt = fxdr_unsigned(int, *tl);
4996 		/*
4997 		 * There is no fixed upper bound defined in the RFCs,
4998 		 * but 128Kbytes should be more than sufficient.
4999 		 */
5000 		if (maxcnt < 0 || maxcnt > 131072)
5001 			maxcnt = 0;
5002 		if (maxcnt > 0) {
5003 			layp = malloc(maxcnt + 1, M_TEMP, M_WAITOK);
5004 			error = nfsrv_mtostr(nd, (char *)layp, maxcnt);
5005 			if (error != 0)
5006 				goto nfsmout;
5007 		}
5008 	} else {
5009 		if (reclaim == newnfs_true) {
5010 			nd->nd_repstat = NFSERR_INVAL;
5011 			goto nfsmout;
5012 		}
5013 		offset = len = 0;
5014 		maxcnt = 0;
5015 	}
5016 	nd->nd_repstat = nfsrv_layoutreturn(nd, vp, layouttype, iomode,
5017 	    offset, len, reclaim, kind, &stateid, maxcnt, layp, &fnd,
5018 	    nd->nd_cred, p);
5019 	NFSD_DEBUG(4, "nfsrv_layoutreturn stat=%u fnd=%d\n", nd->nd_repstat,
5020 	    fnd);
5021 	if (nd->nd_repstat == 0) {
5022 		NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
5023 		if (fnd != 0) {
5024 			*tl = newnfs_true;
5025 			NFSM_BUILD(tl, uint32_t *, NFSX_STATEID);
5026 			*tl++ = txdr_unsigned(stateid.seqid);
5027 			NFSBCOPY(stateid.other, tl, NFSX_STATEIDOTHER);
5028 		} else
5029 			*tl = newnfs_false;
5030 	}
5031 nfsmout:
5032 	free(layp, M_TEMP);
5033 	vput(vp);
5034 	NFSEXITCODE2(error, nd);
5035 	return (error);
5036 }
5037 
5038 /*
5039  * nfsv4 layout error service
5040  */
5041 int
5042 nfsrvd_layouterror(struct nfsrv_descript *nd, __unused int isdgram,
5043     vnode_t vp, struct nfsexstuff *exp)
5044 {
5045 	uint32_t *tl;
5046 	nfsv4stateid_t stateid;
5047 	int cnt, error = 0, i, stat;
5048 	int opnum __unused;
5049 	char devid[NFSX_V4DEVICEID];
5050 	uint64_t offset, len;
5051 
5052 	NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_HYPER + NFSX_STATEID +
5053 	    NFSX_UNSIGNED);
5054 	offset = fxdr_hyper(tl); tl += 2;
5055 	len = fxdr_hyper(tl); tl += 2;
5056 	stateid.seqid = fxdr_unsigned(uint32_t, *tl++);
5057 	NFSBCOPY(tl, stateid.other, NFSX_STATEIDOTHER);
5058 	tl += (NFSX_STATEIDOTHER / NFSX_UNSIGNED);
5059 	cnt = fxdr_unsigned(int, *tl);
5060 	NFSD_DEBUG(4, "layouterror off=%ju len=%ju cnt=%d\n", (uintmax_t)offset,
5061 	    (uintmax_t)len, cnt);
5062 	/*
5063 	 * For the special stateid of other all 0s and seqid == 1, set
5064 	 * the stateid to the current stateid, if it is set.
5065 	 */
5066 	if (stateid.seqid == 1 && stateid.other[0] == 0 &&
5067 	    stateid.other[1] == 0 && stateid.other[2] == 0) {
5068 		if ((nd->nd_flag & ND_CURSTATEID) != 0) {
5069 			stateid = nd->nd_curstateid;
5070 			stateid.seqid = 0;
5071 		} else {
5072 			nd->nd_repstat = NFSERR_BADSTATEID;
5073 			goto nfsmout;
5074 		}
5075 	}
5076 
5077 	/*
5078 	 * Ignore offset, len and stateid for now.
5079 	 */
5080 	for (i = 0; i < cnt; i++) {
5081 		NFSM_DISSECT(tl, uint32_t *, NFSX_V4DEVICEID + 2 *
5082 		    NFSX_UNSIGNED);
5083 		NFSBCOPY(tl, devid, NFSX_V4DEVICEID);
5084 		tl += (NFSX_V4DEVICEID / NFSX_UNSIGNED);
5085 		stat = fxdr_unsigned(int, *tl++);
5086 		opnum = fxdr_unsigned(int, *tl);
5087 		NFSD_DEBUG(4, "nfsrvd_layouterr op=%d stat=%d\n", opnum, stat);
5088 		/*
5089 		 * Except for NFSERR_ACCES, NFSERR_STALE and NFSERR_NOSPC
5090 		 * errors, disable the mirror.
5091 		 */
5092 		if (stat != NFSERR_ACCES && stat != NFSERR_STALE &&
5093 		    stat != NFSERR_NOSPC)
5094 			nfsrv_delds(devid, curthread);
5095 
5096 		/* For NFSERR_NOSPC, mark all deviceids and layouts. */
5097 		if (stat == NFSERR_NOSPC)
5098 			nfsrv_marknospc(devid, true);
5099 	}
5100 nfsmout:
5101 	vput(vp);
5102 	NFSEXITCODE2(error, nd);
5103 	return (error);
5104 }
5105 
5106 /*
5107  * nfsv4 layout stats service
5108  */
5109 int
5110 nfsrvd_layoutstats(struct nfsrv_descript *nd, __unused int isdgram,
5111     vnode_t vp, struct nfsexstuff *exp)
5112 {
5113 	uint32_t *tl;
5114 	nfsv4stateid_t stateid;
5115 	int cnt, error = 0;
5116 	int layouttype __unused;
5117 	char devid[NFSX_V4DEVICEID] __unused;
5118 	uint64_t offset __unused, len __unused, readcount __unused;
5119 	uint64_t readbytes __unused, writecount __unused, writebytes __unused;
5120 
5121 	NFSM_DISSECT(tl, uint32_t *, 6 * NFSX_HYPER + NFSX_STATEID +
5122 	    NFSX_V4DEVICEID + 2 * NFSX_UNSIGNED);
5123 	offset = fxdr_hyper(tl); tl += 2;
5124 	len = fxdr_hyper(tl); tl += 2;
5125 	stateid.seqid = fxdr_unsigned(uint32_t, *tl++);
5126 	NFSBCOPY(tl, stateid.other, NFSX_STATEIDOTHER);
5127 	tl += (NFSX_STATEIDOTHER / NFSX_UNSIGNED);
5128 	readcount = fxdr_hyper(tl); tl += 2;
5129 	readbytes = fxdr_hyper(tl); tl += 2;
5130 	writecount = fxdr_hyper(tl); tl += 2;
5131 	writebytes = fxdr_hyper(tl); tl += 2;
5132 	NFSBCOPY(tl, devid, NFSX_V4DEVICEID);
5133 	tl += (NFSX_V4DEVICEID / NFSX_UNSIGNED);
5134 	layouttype = fxdr_unsigned(int, *tl++);
5135 	cnt = fxdr_unsigned(int, *tl);
5136 	error = nfsm_advance(nd, NFSM_RNDUP(cnt), -1);
5137 	if (error != 0)
5138 		goto nfsmout;
5139 	NFSD_DEBUG(4, "layoutstats cnt=%d\n", cnt);
5140 	/*
5141 	 * For the special stateid of other all 0s and seqid == 1, set
5142 	 * the stateid to the current stateid, if it is set.
5143 	 */
5144 	if (stateid.seqid == 1 && stateid.other[0] == 0 &&
5145 	    stateid.other[1] == 0 && stateid.other[2] == 0) {
5146 		if ((nd->nd_flag & ND_CURSTATEID) != 0) {
5147 			stateid = nd->nd_curstateid;
5148 			stateid.seqid = 0;
5149 		} else {
5150 			nd->nd_repstat = NFSERR_BADSTATEID;
5151 			goto nfsmout;
5152 		}
5153 	}
5154 
5155 	/*
5156 	 * No use for the stats for now.
5157 	 */
5158 nfsmout:
5159 	vput(vp);
5160 	NFSEXITCODE2(error, nd);
5161 	return (error);
5162 }
5163 
5164 /*
5165  * nfsv4 io_advise service
5166  */
5167 int
5168 nfsrvd_ioadvise(struct nfsrv_descript *nd, __unused int isdgram,
5169     vnode_t vp, struct nfsexstuff *exp)
5170 {
5171 	uint32_t *tl;
5172 	nfsv4stateid_t stateid;
5173 	nfsattrbit_t hints;
5174 	int error = 0, ret;
5175 	off_t offset, len;
5176 
5177 	NFSM_DISSECT(tl, uint32_t *, NFSX_STATEID + 2 * NFSX_HYPER);
5178 	stateid.seqid = fxdr_unsigned(uint32_t, *tl++);
5179 	NFSBCOPY(tl, stateid.other, NFSX_STATEIDOTHER);
5180 	tl += (NFSX_STATEIDOTHER / NFSX_UNSIGNED);
5181 	offset = fxdr_hyper(tl); tl += 2;
5182 	len = fxdr_hyper(tl);
5183 	error = nfsrv_getattrbits(nd, &hints, NULL, NULL);
5184 	if (error != 0)
5185 		goto nfsmout;
5186 	/*
5187 	 * For the special stateid of other all 0s and seqid == 1, set
5188 	 * the stateid to the current stateid, if it is set.
5189 	 */
5190 	if (stateid.seqid == 1 && stateid.other[0] == 0 &&
5191 	    stateid.other[1] == 0 && stateid.other[2] == 0) {
5192 		if ((nd->nd_flag & ND_CURSTATEID) != 0) {
5193 			stateid = nd->nd_curstateid;
5194 			stateid.seqid = 0;
5195 		} else {
5196 			nd->nd_repstat = NFSERR_BADSTATEID;
5197 			goto nfsmout;
5198 		}
5199 	}
5200 
5201 	if (offset < 0) {
5202 		nd->nd_repstat = NFSERR_INVAL;
5203 		goto nfsmout;
5204 	}
5205 	if (len < 0)
5206 		len = 0;
5207 	if (vp->v_type != VREG) {
5208 		if (vp->v_type == VDIR)
5209 			nd->nd_repstat = NFSERR_ISDIR;
5210 		else
5211 			nd->nd_repstat = NFSERR_WRONGTYPE;
5212 		goto nfsmout;
5213 	}
5214 
5215 	/*
5216 	 * For now, we can only handle WILLNEED and DONTNEED and don't use
5217 	 * the stateid.
5218 	 */
5219 	if ((NFSISSET_ATTRBIT(&hints, NFSV4IOHINT_WILLNEED) &&
5220 	    !NFSISSET_ATTRBIT(&hints, NFSV4IOHINT_DONTNEED)) ||
5221 	    (NFSISSET_ATTRBIT(&hints, NFSV4IOHINT_DONTNEED) &&
5222 	    !NFSISSET_ATTRBIT(&hints, NFSV4IOHINT_WILLNEED))) {
5223 		NFSVOPUNLOCK(vp);
5224 		if (NFSISSET_ATTRBIT(&hints, NFSV4IOHINT_WILLNEED)) {
5225 			ret = VOP_ADVISE(vp, offset, len, POSIX_FADV_WILLNEED);
5226 			NFSZERO_ATTRBIT(&hints);
5227 			if (ret == 0)
5228 				NFSSETBIT_ATTRBIT(&hints, NFSV4IOHINT_WILLNEED);
5229 			else
5230 				NFSSETBIT_ATTRBIT(&hints, NFSV4IOHINT_NORMAL);
5231 		} else {
5232 			ret = VOP_ADVISE(vp, offset, len, POSIX_FADV_DONTNEED);
5233 			NFSZERO_ATTRBIT(&hints);
5234 			if (ret == 0)
5235 				NFSSETBIT_ATTRBIT(&hints, NFSV4IOHINT_DONTNEED);
5236 			else
5237 				NFSSETBIT_ATTRBIT(&hints, NFSV4IOHINT_NORMAL);
5238 		}
5239 		vrele(vp);
5240 	} else {
5241 		NFSZERO_ATTRBIT(&hints);
5242 		NFSSETBIT_ATTRBIT(&hints, NFSV4IOHINT_NORMAL);
5243 		vput(vp);
5244 	}
5245 	nfsrv_putattrbit(nd, &hints);
5246 	NFSEXITCODE2(error, nd);
5247 	return (error);
5248 nfsmout:
5249 	vput(vp);
5250 	NFSEXITCODE2(error, nd);
5251 	return (error);
5252 }
5253 
5254 /*
5255  * nfsv4 getdeviceinfo service
5256  */
5257 int
5258 nfsrvd_getdevinfo(struct nfsrv_descript *nd, __unused int isdgram,
5259     __unused vnode_t vp, __unused struct nfsexstuff *exp)
5260 {
5261 	uint32_t *tl, maxcnt, notify[NFSV4_NOTIFYBITMAP];
5262 	int cnt, devaddrlen, error = 0, i, layouttype;
5263 	char devid[NFSX_V4DEVICEID], *devaddr;
5264 	time_t dev_time;
5265 
5266 	NFSM_DISSECT(tl, uint32_t *, 3 * NFSX_UNSIGNED + NFSX_V4DEVICEID);
5267 	NFSBCOPY(tl, devid, NFSX_V4DEVICEID);
5268 	tl += (NFSX_V4DEVICEID / NFSX_UNSIGNED);
5269 	layouttype = fxdr_unsigned(int, *tl++);
5270 	maxcnt = fxdr_unsigned(uint32_t, *tl++);
5271 	cnt = fxdr_unsigned(int, *tl);
5272 	NFSD_DEBUG(4, "getdevinfo ltyp=%d maxcnt=%u bitcnt=%d\n", layouttype,
5273 	    maxcnt, cnt);
5274 	if (cnt > NFSV4_NOTIFYBITMAP || cnt < 0) {
5275 		nd->nd_repstat = NFSERR_INVAL;
5276 		goto nfsmout;
5277 	}
5278 	if (cnt > 0) {
5279 		NFSM_DISSECT(tl, uint32_t *, cnt * NFSX_UNSIGNED);
5280 		for (i = 0; i < cnt; i++)
5281 			notify[i] = fxdr_unsigned(uint32_t, *tl++);
5282 	}
5283 	for (i = cnt; i < NFSV4_NOTIFYBITMAP; i++)
5284 		notify[i] = 0;
5285 
5286 	/*
5287 	 * Check that the device id is not stale.  Device ids are recreated
5288 	 * each time the nfsd threads are restarted.
5289 	 */
5290 	NFSBCOPY(devid, &dev_time, sizeof(dev_time));
5291 	if (dev_time != nfsdev_time) {
5292 		nd->nd_repstat = NFSERR_NOENT;
5293 		goto nfsmout;
5294 	}
5295 
5296 	/* Look for the device id. */
5297 	nd->nd_repstat = nfsrv_getdevinfo(devid, layouttype, &maxcnt,
5298 	    notify, &devaddrlen, &devaddr);
5299 	NFSD_DEBUG(4, "nfsrv_getdevinfo stat=%u\n", nd->nd_repstat);
5300 	if (nd->nd_repstat == 0) {
5301 		NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
5302 		*tl = txdr_unsigned(layouttype);
5303 		nfsm_strtom(nd, devaddr, devaddrlen);
5304 		cnt = 0;
5305 		for (i = 0; i < NFSV4_NOTIFYBITMAP; i++) {
5306 			if (notify[i] != 0)
5307 				cnt = i + 1;
5308 		}
5309 		NFSM_BUILD(tl, uint32_t *, (cnt + 1) * NFSX_UNSIGNED);
5310 		*tl++ = txdr_unsigned(cnt);
5311 		for (i = 0; i < cnt; i++)
5312 			*tl++ = txdr_unsigned(notify[i]);
5313 	} else if (nd->nd_repstat == NFSERR_TOOSMALL) {
5314 		NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
5315 		*tl = txdr_unsigned(maxcnt);
5316 	}
5317 nfsmout:
5318 	NFSEXITCODE2(error, nd);
5319 	return (error);
5320 }
5321 
5322 /*
5323  * nfsv4 test stateid service
5324  */
5325 int
5326 nfsrvd_teststateid(struct nfsrv_descript *nd, __unused int isdgram,
5327     __unused vnode_t vp, __unused struct nfsexstuff *exp)
5328 {
5329 	uint32_t *tl;
5330 	nfsv4stateid_t *stateidp = NULL, *tstateidp;
5331 	int cnt, error = 0, i, ret;
5332 	struct thread *p = curthread;
5333 
5334 	NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
5335 	cnt = fxdr_unsigned(int, *tl);
5336 	if (cnt <= 0 || cnt > 1024) {
5337 		nd->nd_repstat = NFSERR_BADXDR;
5338 		goto nfsmout;
5339 	}
5340 	stateidp = mallocarray(cnt, sizeof(nfsv4stateid_t), M_TEMP, M_WAITOK);
5341 	tstateidp = stateidp;
5342 	for (i = 0; i < cnt; i++) {
5343 		NFSM_DISSECT(tl, uint32_t *, NFSX_STATEID);
5344 		tstateidp->seqid = fxdr_unsigned(uint32_t, *tl++);
5345 		NFSBCOPY(tl, tstateidp->other, NFSX_STATEIDOTHER);
5346 		tstateidp++;
5347 	}
5348 	NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
5349 	*tl = txdr_unsigned(cnt);
5350 	tstateidp = stateidp;
5351 	for (i = 0; i < cnt; i++) {
5352 		ret = nfsrv_teststateid(nd, tstateidp, p);
5353 		NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
5354 		*tl = txdr_unsigned(ret);
5355 		tstateidp++;
5356 	}
5357 nfsmout:
5358 	free(stateidp, M_TEMP);
5359 	NFSEXITCODE2(error, nd);
5360 	return (error);
5361 }
5362 
5363 /*
5364  * nfs allocate service
5365  */
5366 int
5367 nfsrvd_allocate(struct nfsrv_descript *nd, __unused int isdgram,
5368     vnode_t vp, struct nfsexstuff *exp)
5369 {
5370 	uint32_t *tl;
5371 	struct nfsvattr forat;
5372 	int error = 0, forat_ret = 1, gotproxystateid;
5373 	off_t off, len;
5374 	struct nfsstate st, *stp = &st;
5375 	struct nfslock lo, *lop = &lo;
5376 	nfsv4stateid_t stateid;
5377 	nfsquad_t clientid;
5378 	nfsattrbit_t attrbits;
5379 
5380 	if (!nfsrv_doallocate) {
5381 		/*
5382 		 * If any exported file system, such as a ZFS one, cannot
5383 		 * do VOP_ALLOCATE(), this operation cannot be supported
5384 		 * for NFSv4.2.  This cannot be done 'per filesystem', but
5385 		 * must be for the entire nfsd NFSv4.2 service.
5386 		 */
5387 		nd->nd_repstat = NFSERR_NOTSUPP;
5388 		goto nfsmout;
5389 	}
5390 	gotproxystateid = 0;
5391 	NFSM_DISSECT(tl, uint32_t *, NFSX_STATEID + 2 * NFSX_HYPER);
5392 	stp->ls_flags = (NFSLCK_CHECK | NFSLCK_WRITEACCESS);
5393 	lop->lo_flags = NFSLCK_WRITE;
5394 	stp->ls_ownerlen = 0;
5395 	stp->ls_op = NULL;
5396 	stp->ls_uid = nd->nd_cred->cr_uid;
5397 	stp->ls_stateid.seqid = fxdr_unsigned(u_int32_t, *tl++);
5398 	clientid.lval[0] = stp->ls_stateid.other[0] = *tl++;
5399 	clientid.lval[1] = stp->ls_stateid.other[1] = *tl++;
5400 	if ((nd->nd_flag & ND_IMPLIEDCLID) != 0) {
5401 		if ((nd->nd_flag & ND_NFSV41) != 0)
5402 			clientid.qval = nd->nd_clientid.qval;
5403 		else if (nd->nd_clientid.qval != clientid.qval)
5404 			printf("EEK2 multiple clids\n");
5405 	} else {
5406 		if ((nd->nd_flag & ND_NFSV41) != 0)
5407 			printf("EEK! no clientid from session\n");
5408 		nd->nd_flag |= ND_IMPLIEDCLID;
5409 		nd->nd_clientid.qval = clientid.qval;
5410 	}
5411 	stp->ls_stateid.other[2] = *tl++;
5412 	/*
5413 	 * Don't allow this to be done for a DS.
5414 	 */
5415 	if ((nd->nd_flag & ND_DSSERVER) != 0)
5416 		nd->nd_repstat = NFSERR_NOTSUPP;
5417 	/* However, allow the proxy stateid. */
5418 	if (stp->ls_stateid.seqid == 0xffffffff &&
5419 	    stp->ls_stateid.other[0] == 0x55555555 &&
5420 	    stp->ls_stateid.other[1] == 0x55555555 &&
5421 	    stp->ls_stateid.other[2] == 0x55555555)
5422 		gotproxystateid = 1;
5423 	off = fxdr_hyper(tl); tl += 2;
5424 	lop->lo_first = off;
5425 	len = fxdr_hyper(tl);
5426 	lop->lo_end = lop->lo_first + len;
5427 	/*
5428 	 * Sanity check the offset and length.
5429 	 * off and len are off_t (signed int64_t) whereas
5430 	 * lo_first and lo_end are uint64_t and, as such,
5431 	 * if off >= 0 && len > 0, lo_end cannot overflow
5432 	 * unless off_t is changed to something other than
5433 	 * int64_t.  Check lo_end < lo_first in case that
5434 	 * is someday the case.
5435 	 */
5436 	if (nd->nd_repstat == 0 && (len <= 0 || off < 0 || lop->lo_end >
5437 	    OFF_MAX || lop->lo_end < lop->lo_first))
5438 		nd->nd_repstat = NFSERR_INVAL;
5439 
5440 	if (nd->nd_repstat == 0 && vp->v_type != VREG)
5441 		nd->nd_repstat = NFSERR_WRONGTYPE;
5442 	NFSZERO_ATTRBIT(&attrbits);
5443 	NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_OWNER);
5444 	forat_ret = nfsvno_getattr(vp, &forat, nd, curthread, 1, &attrbits);
5445 	if (nd->nd_repstat == 0)
5446 		nd->nd_repstat = forat_ret;
5447 	if (nd->nd_repstat == 0 && (forat.na_uid != nd->nd_cred->cr_uid ||
5448 	     NFSVNO_EXSTRICTACCESS(exp)))
5449 		nd->nd_repstat = nfsvno_accchk(vp, VWRITE, nd->nd_cred, exp,
5450 		    curthread, NFSACCCHK_ALLOWOWNER, NFSACCCHK_VPISLOCKED,
5451 		    NULL);
5452 	if (nd->nd_repstat == 0 && gotproxystateid == 0)
5453 		nd->nd_repstat = nfsrv_lockctrl(vp, &stp, &lop, NULL, clientid,
5454 		    &stateid, exp, nd, curthread);
5455 
5456 	NFSD_DEBUG(4, "nfsrvd_allocate: off=%jd len=%jd stat=%d\n",
5457 	    (intmax_t)off, (intmax_t)len, nd->nd_repstat);
5458 	if (nd->nd_repstat == 0)
5459 		nd->nd_repstat = nfsvno_allocate(vp, off, len, nd->nd_cred,
5460 		    curthread);
5461 	NFSD_DEBUG(4, "nfsrvd_allocate: aft nfsvno_allocate=%d\n",
5462 	    nd->nd_repstat);
5463 	vput(vp);
5464 	NFSEXITCODE2(0, nd);
5465 	return (0);
5466 nfsmout:
5467 	vput(vp);
5468 	NFSEXITCODE2(error, nd);
5469 	return (error);
5470 }
5471 
5472 /*
5473  * nfs deallocate service
5474  */
5475 int
5476 nfsrvd_deallocate(struct nfsrv_descript *nd, __unused int isdgram,
5477     vnode_t vp, struct nfsexstuff *exp)
5478 {
5479 	uint32_t *tl;
5480 	struct nfsvattr forat;
5481 	int error = 0, forat_ret = 1, gotproxystateid;
5482 	off_t off, len;
5483 	struct nfsstate st, *stp = &st;
5484 	struct nfslock lo, *lop = &lo;
5485 	nfsv4stateid_t stateid;
5486 	nfsquad_t clientid;
5487 	nfsattrbit_t attrbits;
5488 
5489 	gotproxystateid = 0;
5490 	NFSM_DISSECT(tl, uint32_t *, NFSX_STATEID + 2 * NFSX_HYPER);
5491 	stp->ls_flags = (NFSLCK_CHECK | NFSLCK_WRITEACCESS);
5492 	lop->lo_flags = NFSLCK_WRITE;
5493 	stp->ls_ownerlen = 0;
5494 	stp->ls_op = NULL;
5495 	stp->ls_uid = nd->nd_cred->cr_uid;
5496 	stp->ls_stateid.seqid = fxdr_unsigned(u_int32_t, *tl++);
5497 	clientid.lval[0] = stp->ls_stateid.other[0] = *tl++;
5498 	clientid.lval[1] = stp->ls_stateid.other[1] = *tl++;
5499 	if ((nd->nd_flag & ND_IMPLIEDCLID) != 0) {
5500 		if ((nd->nd_flag & ND_NFSV41) != 0)
5501 			clientid.qval = nd->nd_clientid.qval;
5502 		else if (nd->nd_clientid.qval != clientid.qval)
5503 			printf("EEK2 multiple clids\n");
5504 	} else {
5505 		if ((nd->nd_flag & ND_NFSV41) != 0)
5506 			printf("EEK! no clientid from session\n");
5507 		nd->nd_flag |= ND_IMPLIEDCLID;
5508 		nd->nd_clientid.qval = clientid.qval;
5509 	}
5510 	stp->ls_stateid.other[2] = *tl++;
5511 	/*
5512 	 * Don't allow this to be done for a DS.
5513 	 */
5514 	if ((nd->nd_flag & ND_DSSERVER) != 0)
5515 		nd->nd_repstat = NFSERR_NOTSUPP;
5516 	/* However, allow the proxy stateid. */
5517 	if (stp->ls_stateid.seqid == 0xffffffff &&
5518 	    stp->ls_stateid.other[0] == 0x55555555 &&
5519 	    stp->ls_stateid.other[1] == 0x55555555 &&
5520 	    stp->ls_stateid.other[2] == 0x55555555)
5521 		gotproxystateid = 1;
5522 	off = fxdr_hyper(tl); tl += 2;
5523 	lop->lo_first = off;
5524 	len = fxdr_hyper(tl);
5525 	if (len < 0)
5526 		len = OFF_MAX;
5527 	NFSD_DEBUG(4, "dealloc: off=%jd len=%jd\n", (intmax_t)off,
5528 	    (intmax_t)len);
5529 	lop->lo_end = lop->lo_first + len;
5530 	/*
5531 	 * Sanity check the offset and length.
5532 	 * off and len are off_t (signed int64_t) whereas
5533 	 * lo_first and lo_end are uint64_t and, as such,
5534 	 * if off >= 0 && len > 0, lo_end cannot overflow
5535 	 * unless off_t is changed to something other than
5536 	 * int64_t.  Check lo_end < lo_first in case that
5537 	 * is someday the case.
5538 	 * The error to return is not specified by RFC 7862 so I
5539 	 * made this compatible with the Linux knfsd.
5540 	 */
5541 	if (nd->nd_repstat == 0) {
5542 		if (off < 0 || lop->lo_end > NFSRV_MAXFILESIZE)
5543 			nd->nd_repstat = NFSERR_FBIG;
5544 		else if (len == 0 || lop->lo_end < lop->lo_first)
5545 			nd->nd_repstat = NFSERR_INVAL;
5546 	}
5547 
5548 	if (nd->nd_repstat == 0 && vp->v_type != VREG)
5549 		nd->nd_repstat = NFSERR_WRONGTYPE;
5550 	NFSZERO_ATTRBIT(&attrbits);
5551 	NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_OWNER);
5552 	forat_ret = nfsvno_getattr(vp, &forat, nd, curthread, 1, &attrbits);
5553 	if (nd->nd_repstat == 0)
5554 		nd->nd_repstat = forat_ret;
5555 	if (nd->nd_repstat == 0 && (forat.na_uid != nd->nd_cred->cr_uid ||
5556 	     NFSVNO_EXSTRICTACCESS(exp)))
5557 		nd->nd_repstat = nfsvno_accchk(vp, VWRITE, nd->nd_cred, exp,
5558 		    curthread, NFSACCCHK_ALLOWOWNER, NFSACCCHK_VPISLOCKED,
5559 		    NULL);
5560 	if (nd->nd_repstat == 0 && gotproxystateid == 0)
5561 		nd->nd_repstat = nfsrv_lockctrl(vp, &stp, &lop, NULL, clientid,
5562 		    &stateid, exp, nd, curthread);
5563 
5564 	if (nd->nd_repstat == 0)
5565 		nd->nd_repstat = nfsvno_deallocate(vp, off, len, nd->nd_cred,
5566 		    curthread);
5567 	vput(vp);
5568 	NFSD_DEBUG(4, "eo deallocate=%d\n", nd->nd_repstat);
5569 	NFSEXITCODE2(0, nd);
5570 	return (0);
5571 nfsmout:
5572 	vput(vp);
5573 	NFSEXITCODE2(error, nd);
5574 	return (error);
5575 }
5576 
5577 /*
5578  * nfs copy service
5579  */
5580 int
5581 nfsrvd_copy_file_range(struct nfsrv_descript *nd, __unused int isdgram,
5582     vnode_t vp, vnode_t tovp, struct nfsexstuff *exp, struct nfsexstuff *toexp)
5583 {
5584 	uint32_t *tl;
5585 	struct nfsvattr at;
5586 	int cnt, error = 0, ret;
5587 	off_t inoff, outoff;
5588 	uint64_t len;
5589 	size_t xfer;
5590 	struct nfsstate inst, outst, *instp = &inst, *outstp = &outst;
5591 	struct nfslock inlo, outlo, *inlop = &inlo, *outlop = &outlo;
5592 	nfsquad_t clientid;
5593 	nfsv4stateid_t stateid;
5594 	nfsattrbit_t attrbits;
5595 	void *rl_rcookie, *rl_wcookie;
5596 
5597 	rl_rcookie = rl_wcookie = NULL;
5598 	if (nfsrv_maxcopyrange == 0 || nfsrv_devidcnt > 0) {
5599 		/*
5600 		 * For a pNFS server, reply NFSERR_NOTSUPP so that the client
5601 		 * will do the copy via I/O on the DS(s).
5602 		 * If vfs.nfsd.maxcopyrange set to 0, disable Copy.
5603 		 */
5604 		nd->nd_repstat = NFSERR_NOTSUPP;
5605 		goto nfsmout;
5606 	}
5607 	if (vp == tovp) {
5608 		/* Copying a byte range within the same file is not allowed. */
5609 		nd->nd_repstat = NFSERR_INVAL;
5610 		goto nfsmout;
5611 	}
5612 	NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_STATEID + 3 * NFSX_HYPER +
5613 	    3 * NFSX_UNSIGNED);
5614 	instp->ls_flags = (NFSLCK_CHECK | NFSLCK_READACCESS);
5615 	inlop->lo_flags = NFSLCK_READ;
5616 	instp->ls_ownerlen = 0;
5617 	instp->ls_op = NULL;
5618 	instp->ls_uid = nd->nd_cred->cr_uid;
5619 	instp->ls_stateid.seqid = fxdr_unsigned(uint32_t, *tl++);
5620 	clientid.lval[0] = instp->ls_stateid.other[0] = *tl++;
5621 	clientid.lval[1] = instp->ls_stateid.other[1] = *tl++;
5622 	if ((nd->nd_flag & ND_IMPLIEDCLID) != 0)
5623 		clientid.qval = nd->nd_clientid.qval;
5624 	instp->ls_stateid.other[2] = *tl++;
5625 	outstp->ls_flags = (NFSLCK_CHECK | NFSLCK_WRITEACCESS);
5626 	outlop->lo_flags = NFSLCK_WRITE;
5627 	outstp->ls_ownerlen = 0;
5628 	outstp->ls_op = NULL;
5629 	outstp->ls_uid = nd->nd_cred->cr_uid;
5630 	outstp->ls_stateid.seqid = fxdr_unsigned(uint32_t, *tl++);
5631 	outstp->ls_stateid.other[0] = *tl++;
5632 	outstp->ls_stateid.other[1] = *tl++;
5633 	outstp->ls_stateid.other[2] = *tl++;
5634 	inoff = fxdr_hyper(tl); tl += 2;
5635 	inlop->lo_first = inoff;
5636 	outoff = fxdr_hyper(tl); tl += 2;
5637 	outlop->lo_first = outoff;
5638 	len = fxdr_hyper(tl); tl += 2;
5639 	if (len == 0) {
5640 		/* len == 0 means to EOF. */
5641 		inlop->lo_end = OFF_MAX;
5642 		outlop->lo_end = OFF_MAX;
5643 	} else {
5644 		inlop->lo_end = inlop->lo_first + len;
5645 		outlop->lo_end = outlop->lo_first + len;
5646 	}
5647 
5648 	/*
5649 	 * At this time only consecutive, synchronous copy is supported,
5650 	 * so ca_consecutive and ca_synchronous can be ignored.
5651 	 */
5652 	tl += 2;
5653 
5654 	cnt = fxdr_unsigned(int, *tl);
5655 	if ((nd->nd_flag & ND_DSSERVER) != 0 || cnt != 0)
5656 		nd->nd_repstat = NFSERR_NOTSUPP;
5657 	if (nd->nd_repstat == 0 && (inoff > OFF_MAX || outoff > OFF_MAX ||
5658 	    inlop->lo_end > OFF_MAX || outlop->lo_end > OFF_MAX ||
5659 	    inlop->lo_end < inlop->lo_first || outlop->lo_end <
5660 	    outlop->lo_first))
5661 		nd->nd_repstat = NFSERR_INVAL;
5662 
5663 	if (nd->nd_repstat == 0 && vp->v_type != VREG)
5664 		nd->nd_repstat = NFSERR_WRONGTYPE;
5665 
5666 	/* Check permissions for the input file. */
5667 	NFSZERO_ATTRBIT(&attrbits);
5668 	NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_OWNER);
5669 	ret = nfsvno_getattr(vp, &at, nd, curthread, 1, &attrbits);
5670 	if (nd->nd_repstat == 0)
5671 		nd->nd_repstat = ret;
5672 	if (nd->nd_repstat == 0 && (at.na_uid != nd->nd_cred->cr_uid ||
5673 	     NFSVNO_EXSTRICTACCESS(exp)))
5674 		nd->nd_repstat = nfsvno_accchk(vp, VREAD, nd->nd_cred, exp,
5675 		    curthread, NFSACCCHK_ALLOWOWNER, NFSACCCHK_VPISLOCKED,
5676 		    NULL);
5677 	if (nd->nd_repstat == 0)
5678 		nd->nd_repstat = nfsrv_lockctrl(vp, &instp, &inlop, NULL,
5679 		    clientid, &stateid, exp, nd, curthread);
5680 	NFSVOPUNLOCK(vp);
5681 	if (nd->nd_repstat != 0)
5682 		goto out;
5683 
5684 	error = NFSVOPLOCK(tovp, LK_SHARED);
5685 	if (error != 0)
5686 		goto out;
5687 	if (tovp->v_type != VREG)
5688 		nd->nd_repstat = NFSERR_WRONGTYPE;
5689 
5690 	/* For the output file, we only need the Owner attribute. */
5691 	ret = nfsvno_getattr(tovp, &at, nd, curthread, 1, &attrbits);
5692 	if (nd->nd_repstat == 0)
5693 		nd->nd_repstat = ret;
5694 	if (nd->nd_repstat == 0 && (at.na_uid != nd->nd_cred->cr_uid ||
5695 	     NFSVNO_EXSTRICTACCESS(exp)))
5696 		nd->nd_repstat = nfsvno_accchk(tovp, VWRITE, nd->nd_cred, toexp,
5697 		    curthread, NFSACCCHK_ALLOWOWNER, NFSACCCHK_VPISLOCKED,
5698 		    NULL);
5699 	if (nd->nd_repstat == 0)
5700 		nd->nd_repstat = nfsrv_lockctrl(tovp, &outstp, &outlop, NULL,
5701 		    clientid, &stateid, toexp, nd, curthread);
5702 	NFSVOPUNLOCK(tovp);
5703 
5704 	/* Range lock the byte ranges for both invp and outvp. */
5705 	if (nd->nd_repstat == 0) {
5706 		for (;;) {
5707 			if (len == 0) {
5708 				rl_wcookie = vn_rangelock_wlock(tovp, outoff,
5709 				    OFF_MAX);
5710 				rl_rcookie = vn_rangelock_tryrlock(vp, inoff,
5711 				    OFF_MAX);
5712 			} else {
5713 				rl_wcookie = vn_rangelock_wlock(tovp, outoff,
5714 				    outoff + len);
5715 				rl_rcookie = vn_rangelock_tryrlock(vp, inoff,
5716 				    inoff + len);
5717 			}
5718 			if (rl_rcookie != NULL)
5719 				break;
5720 			vn_rangelock_unlock(tovp, rl_wcookie);
5721 			if (len == 0)
5722 				rl_rcookie = vn_rangelock_rlock(vp, inoff,
5723 				    OFF_MAX);
5724 			else
5725 				rl_rcookie = vn_rangelock_rlock(vp, inoff,
5726 				    inoff + len);
5727 			vn_rangelock_unlock(vp, rl_rcookie);
5728 		}
5729 
5730 		error = NFSVOPLOCK(vp, LK_SHARED);
5731 		if (error == 0) {
5732 			ret = nfsvno_getattr(vp, &at, nd, curthread, 1, NULL);
5733 			if (ret == 0) {
5734 				/*
5735 				 * Since invp is range locked, na_size should
5736 				 * not change.
5737 				 */
5738 				if (len == 0 && at.na_size > inoff) {
5739 					/*
5740 					 * If len == 0, set it based on invp's
5741 					 * size. If offset is past EOF, just
5742 					 * leave len == 0.
5743 					 */
5744 					len = at.na_size - inoff;
5745 				} else if (nfsrv_linux42server == 0 &&
5746 				    inoff + len > at.na_size) {
5747 					/*
5748 					 * RFC-7862 says that NFSERR_INVAL must
5749 					 * be returned when inoff + len exceeds
5750 					 * the file size, however the NFSv4.2
5751 					 * Linux client likes to do this, so
5752 					 * only check if nfsrv_linux42server
5753 					 * is not set.
5754 					 */
5755 					nd->nd_repstat = NFSERR_INVAL;
5756 				}
5757 			}
5758 			NFSVOPUNLOCK(vp);
5759 			if (ret != 0 && nd->nd_repstat == 0)
5760 				nd->nd_repstat = ret;
5761 		} else if (nd->nd_repstat == 0)
5762 			nd->nd_repstat = error;
5763 	}
5764 
5765 	/*
5766 	 * Do the actual copy to an upper limit of vfs.nfsd.maxcopyrange.
5767 	 * This size limit can be set to limit the time a copy RPC will
5768 	 * take.
5769 	 */
5770 	if (len > nfsrv_maxcopyrange)
5771 		xfer = nfsrv_maxcopyrange;
5772 	else
5773 		xfer = len;
5774 	if (nd->nd_repstat == 0) {
5775 		nd->nd_repstat = vn_copy_file_range(vp, &inoff, tovp, &outoff,
5776 		    &xfer, COPY_FILE_RANGE_TIMEO1SEC, nd->nd_cred, nd->nd_cred,
5777 		    NULL);
5778 		if (nd->nd_repstat == 0)
5779 			len = xfer;
5780 	}
5781 
5782 	/* Unlock the ranges. */
5783 	if (rl_rcookie != NULL)
5784 		vn_rangelock_unlock(vp, rl_rcookie);
5785 	if (rl_wcookie != NULL)
5786 		vn_rangelock_unlock(tovp, rl_wcookie);
5787 
5788 	if (nd->nd_repstat == 0) {
5789 		NFSM_BUILD(tl, uint32_t *, 4 * NFSX_UNSIGNED + NFSX_HYPER +
5790 		    NFSX_VERF);
5791 		*tl++ = txdr_unsigned(0);	/* No callback ids. */
5792 		txdr_hyper(len, tl); tl += 2;
5793 		*tl++ = txdr_unsigned(NFSWRITE_UNSTABLE);
5794 		*tl++ = txdr_unsigned(nfsboottime.tv_sec);
5795 		*tl++ = txdr_unsigned(nfsboottime.tv_usec);
5796 		*tl++ = newnfs_true;
5797 		*tl = newnfs_true;
5798 	}
5799 out:
5800 	vrele(vp);
5801 	vrele(tovp);
5802 	NFSEXITCODE2(error, nd);
5803 	return (error);
5804 nfsmout:
5805 	vput(vp);
5806 	vrele(tovp);
5807 	NFSEXITCODE2(error, nd);
5808 	return (error);
5809 }
5810 
5811 /*
5812  * nfs seek service
5813  */
5814 int
5815 nfsrvd_seek(struct nfsrv_descript *nd, __unused int isdgram,
5816     vnode_t vp, struct nfsexstuff *exp)
5817 {
5818 	uint32_t *tl;
5819 	struct nfsvattr at;
5820 	int content, error = 0;
5821 	off_t off;
5822 	u_long cmd;
5823 	nfsattrbit_t attrbits;
5824 	bool eof;
5825 
5826 	NFSM_DISSECT(tl, uint32_t *, NFSX_STATEID + NFSX_HYPER + NFSX_UNSIGNED);
5827 	/* Ignore the stateid for now. */
5828 	tl += (NFSX_STATEID / NFSX_UNSIGNED);
5829 	off = fxdr_hyper(tl); tl += 2;
5830 	content = fxdr_unsigned(int, *tl);
5831 	if (content == NFSV4CONTENT_DATA)
5832 		cmd = FIOSEEKDATA;
5833 	else if (content == NFSV4CONTENT_HOLE)
5834 		cmd = FIOSEEKHOLE;
5835 	else
5836 		nd->nd_repstat = NFSERR_BADXDR;
5837 	if (nd->nd_repstat == 0 && vp->v_type == VDIR)
5838 		nd->nd_repstat = NFSERR_ISDIR;
5839 	if (nd->nd_repstat == 0 && vp->v_type != VREG)
5840 		nd->nd_repstat = NFSERR_WRONGTYPE;
5841 	if (nd->nd_repstat == 0 && off < 0)
5842 		nd->nd_repstat = NFSERR_NXIO;
5843 	if (nd->nd_repstat == 0) {
5844 		/* Check permissions for the input file. */
5845 		NFSZERO_ATTRBIT(&attrbits);
5846 		NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_OWNER);
5847 		nd->nd_repstat = nfsvno_getattr(vp, &at, nd, curthread, 1,
5848 		    &attrbits);
5849 	}
5850 	if (nd->nd_repstat == 0 && (at.na_uid != nd->nd_cred->cr_uid ||
5851 	     NFSVNO_EXSTRICTACCESS(exp)))
5852 		nd->nd_repstat = nfsvno_accchk(vp, VREAD, nd->nd_cred, exp,
5853 		    curthread, NFSACCCHK_ALLOWOWNER, NFSACCCHK_VPISLOCKED,
5854 		    NULL);
5855 	if (nd->nd_repstat != 0)
5856 		goto nfsmout;
5857 
5858 	/* nfsvno_seek() unlocks and vrele()s the vp. */
5859 	nd->nd_repstat = nfsvno_seek(nd, vp, cmd, &off, content, &eof,
5860 	    nd->nd_cred, curthread);
5861 	if (nd->nd_repstat == 0 && eof && content == NFSV4CONTENT_DATA &&
5862 	    nfsrv_linux42server != 0)
5863 		nd->nd_repstat = NFSERR_NXIO;
5864 	if (nd->nd_repstat == 0) {
5865 		NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED + NFSX_HYPER);
5866 		if (eof)
5867 			*tl++ = newnfs_true;
5868 		else
5869 			*tl++ = newnfs_false;
5870 		txdr_hyper(off, tl);
5871 	}
5872 	NFSEXITCODE2(error, nd);
5873 	return (error);
5874 nfsmout:
5875 	vput(vp);
5876 	NFSEXITCODE2(error, nd);
5877 	return (error);
5878 }
5879 
5880 /*
5881  * nfs get extended attribute service
5882  */
5883 int
5884 nfsrvd_getxattr(struct nfsrv_descript *nd, __unused int isdgram,
5885     vnode_t vp, __unused struct nfsexstuff *exp)
5886 {
5887 	uint32_t *tl;
5888 	struct mbuf *mp = NULL, *mpend = NULL;
5889 	int error, len;
5890 	char *name;
5891 	struct thread *p = curthread;
5892 	uint16_t off;
5893 
5894 	error = 0;
5895 	NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
5896 	len = fxdr_unsigned(int, *tl);
5897 	if (len <= 0) {
5898 		nd->nd_repstat = NFSERR_BADXDR;
5899 		goto nfsmout;
5900 	}
5901 	if (len > EXTATTR_MAXNAMELEN) {
5902 		nd->nd_repstat = NFSERR_NOXATTR;
5903 		goto nfsmout;
5904 	}
5905 	name = malloc(len + 1, M_TEMP, M_WAITOK);
5906 	nd->nd_repstat = nfsrv_mtostr(nd, name, len);
5907 	if (nd->nd_repstat == 0)
5908 		nd->nd_repstat = nfsvno_getxattr(vp, name,
5909 		    nd->nd_maxresp, nd->nd_cred, nd->nd_flag,
5910 		    nd->nd_maxextsiz, p, &mp, &mpend, &len);
5911 	if (nd->nd_repstat == ENOATTR)
5912 		nd->nd_repstat = NFSERR_NOXATTR;
5913 	else if (nd->nd_repstat == EOPNOTSUPP)
5914 		nd->nd_repstat = NFSERR_NOTSUPP;
5915 	if (nd->nd_repstat == 0) {
5916 		NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
5917 		*tl = txdr_unsigned(len);
5918 		if (len > 0) {
5919 			nd->nd_mb->m_next = mp;
5920 			nd->nd_mb = mpend;
5921 			if ((mpend->m_flags & M_EXTPG) != 0) {
5922 				nd->nd_flag |= ND_EXTPG;
5923 				nd->nd_bextpg = mpend->m_epg_npgs - 1;
5924 				nd->nd_bpos = (char *)(void *)
5925 				   PHYS_TO_DMAP(mpend->m_epg_pa[nd->nd_bextpg]);
5926 				off = (nd->nd_bextpg == 0) ?
5927 				    mpend->m_epg_1st_off : 0;
5928 				nd->nd_bpos += off + mpend->m_epg_last_len;
5929 				nd->nd_bextpgsiz = PAGE_SIZE -
5930 				    mpend->m_epg_last_len - off;
5931 			} else
5932 				nd->nd_bpos = mtod(mpend, char *) +
5933 				    mpend->m_len;
5934 		}
5935 	}
5936 	free(name, M_TEMP);
5937 
5938 nfsmout:
5939 	if (nd->nd_repstat == 0)
5940 		nd->nd_repstat = error;
5941 	vput(vp);
5942 	NFSEXITCODE2(0, nd);
5943 	return (0);
5944 }
5945 
5946 /*
5947  * nfs set extended attribute service
5948  */
5949 int
5950 nfsrvd_setxattr(struct nfsrv_descript *nd, __unused int isdgram,
5951     vnode_t vp, __unused struct nfsexstuff *exp)
5952 {
5953 	uint32_t *tl;
5954 	struct nfsvattr ova, nva;
5955 	nfsattrbit_t attrbits;
5956 	int error, len, opt;
5957 	char *name;
5958 	size_t siz;
5959 	struct thread *p = curthread;
5960 
5961 	error = 0;
5962 	name = NULL;
5963 	NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
5964 	opt = fxdr_unsigned(int, *tl++);
5965 	len = fxdr_unsigned(int, *tl);
5966 	if (len <= 0) {
5967 		nd->nd_repstat = NFSERR_BADXDR;
5968 		goto nfsmout;
5969 	}
5970 	if (len > EXTATTR_MAXNAMELEN) {
5971 		nd->nd_repstat = NFSERR_NOXATTR;
5972 		goto nfsmout;
5973 	}
5974 	name = malloc(len + 1, M_TEMP, M_WAITOK);
5975 	error = nfsrv_mtostr(nd, name, len);
5976 	if (error != 0)
5977 		goto nfsmout;
5978 	NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
5979 	len = fxdr_unsigned(int, *tl);
5980 	if (len < 0 || len > IOSIZE_MAX) {
5981 		nd->nd_repstat = NFSERR_XATTR2BIG;
5982 		goto nfsmout;
5983 	}
5984 	switch (opt) {
5985 	case NFSV4SXATTR_CREATE:
5986 		error = VOP_GETEXTATTR(vp, EXTATTR_NAMESPACE_USER, name, NULL,
5987 		    &siz, nd->nd_cred, p);
5988 		if (error != ENOATTR)
5989 			nd->nd_repstat = NFSERR_EXIST;
5990 		error = 0;
5991 		break;
5992 	case NFSV4SXATTR_REPLACE:
5993 		error = VOP_GETEXTATTR(vp, EXTATTR_NAMESPACE_USER, name, NULL,
5994 		    &siz, nd->nd_cred, p);
5995 		if (error != 0)
5996 			nd->nd_repstat = NFSERR_NOXATTR;
5997 		break;
5998 	case NFSV4SXATTR_EITHER:
5999 		break;
6000 	default:
6001 		nd->nd_repstat = NFSERR_BADXDR;
6002 	}
6003 	if (nd->nd_repstat != 0)
6004 		goto nfsmout;
6005 
6006 	/* Now, do the Set Extended attribute, with Change before and after. */
6007 	NFSZERO_ATTRBIT(&attrbits);
6008 	NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_CHANGE);
6009 	nd->nd_repstat = nfsvno_getattr(vp, &ova, nd, p, 1, &attrbits);
6010 	if (nd->nd_repstat == 0) {
6011 		nd->nd_repstat = nfsvno_setxattr(vp, name, len, nd->nd_md,
6012 		    nd->nd_dpos, nd->nd_cred, p);
6013 		if (nd->nd_repstat == ENXIO)
6014 			nd->nd_repstat = NFSERR_XATTR2BIG;
6015 	}
6016 	if (nd->nd_repstat == 0 && len > 0)
6017 		nd->nd_repstat = nfsm_advance(nd, NFSM_RNDUP(len), -1);
6018 	if (nd->nd_repstat == 0)
6019 		nd->nd_repstat = nfsvno_getattr(vp, &nva, nd, p, 1, &attrbits);
6020 	if (nd->nd_repstat == 0) {
6021 		NFSM_BUILD(tl, uint32_t *, 2 * NFSX_HYPER + NFSX_UNSIGNED);
6022 		*tl++ = newnfs_true;
6023 		txdr_hyper(ova.na_filerev, tl); tl += 2;
6024 		txdr_hyper(nva.na_filerev, tl);
6025 	}
6026 
6027 nfsmout:
6028 	free(name, M_TEMP);
6029 	if (nd->nd_repstat == 0)
6030 		nd->nd_repstat = error;
6031 	vput(vp);
6032 	NFSEXITCODE2(0, nd);
6033 	return (0);
6034 }
6035 
6036 /*
6037  * nfs remove extended attribute service
6038  */
6039 int
6040 nfsrvd_rmxattr(struct nfsrv_descript *nd, __unused int isdgram,
6041     vnode_t vp, __unused struct nfsexstuff *exp)
6042 {
6043 	uint32_t *tl;
6044 	struct nfsvattr ova, nva;
6045 	nfsattrbit_t attrbits;
6046 	int error, len;
6047 	char *name;
6048 	struct thread *p = curthread;
6049 
6050 	error = 0;
6051 	name = NULL;
6052 	NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
6053 	len = fxdr_unsigned(int, *tl);
6054 	if (len <= 0) {
6055 		nd->nd_repstat = NFSERR_BADXDR;
6056 		goto nfsmout;
6057 	}
6058 	if (len > EXTATTR_MAXNAMELEN) {
6059 		nd->nd_repstat = NFSERR_NOXATTR;
6060 		goto nfsmout;
6061 	}
6062 	name = malloc(len + 1, M_TEMP, M_WAITOK);
6063 	error = nfsrv_mtostr(nd, name, len);
6064 	if (error != 0)
6065 		goto nfsmout;
6066 
6067 	if ((nd->nd_flag & ND_IMPLIEDCLID) == 0) {
6068 		printf("EEK! nfsrvd_rmxattr: no implied clientid\n");
6069 		error = NFSERR_NOXATTR;
6070 		goto nfsmout;
6071 	}
6072 	/*
6073 	 * Now, do the Remove Extended attribute, with Change before and
6074 	 * after.
6075 	*/
6076 	NFSZERO_ATTRBIT(&attrbits);
6077 	NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_CHANGE);
6078 	nd->nd_repstat = nfsvno_getattr(vp, &ova, nd, p, 1, &attrbits);
6079 	if (nd->nd_repstat == 0) {
6080 		nd->nd_repstat = nfsvno_rmxattr(nd, vp, name, nd->nd_cred, p);
6081 		if (nd->nd_repstat == ENOATTR)
6082 			nd->nd_repstat = NFSERR_NOXATTR;
6083 	}
6084 	if (nd->nd_repstat == 0)
6085 		nd->nd_repstat = nfsvno_getattr(vp, &nva, nd, p, 1, &attrbits);
6086 	if (nd->nd_repstat == 0) {
6087 		NFSM_BUILD(tl, uint32_t *, 2 * NFSX_HYPER + NFSX_UNSIGNED);
6088 		*tl++ = newnfs_true;
6089 		txdr_hyper(ova.na_filerev, tl); tl += 2;
6090 		txdr_hyper(nva.na_filerev, tl);
6091 	}
6092 
6093 nfsmout:
6094 	free(name, M_TEMP);
6095 	if (nd->nd_repstat == 0)
6096 		nd->nd_repstat = error;
6097 	vput(vp);
6098 	NFSEXITCODE2(0, nd);
6099 	return (0);
6100 }
6101 
6102 /*
6103  * nfs list extended attribute service
6104  */
6105 int
6106 nfsrvd_listxattr(struct nfsrv_descript *nd, __unused int isdgram,
6107     vnode_t vp, __unused struct nfsexstuff *exp)
6108 {
6109 	uint32_t cnt, *tl, len, len2, i, pos, retlen;
6110 	int error;
6111 	uint64_t cookie, cookie2;
6112 	u_char *buf;
6113 	bool eof;
6114 	struct thread *p = curthread;
6115 
6116 	error = 0;
6117 	buf = NULL;
6118 	NFSM_DISSECT(tl, uint32_t *, NFSX_HYPER + NFSX_UNSIGNED);
6119 	/*
6120 	 * The cookie doesn't need to be in net byte order, but FreeBSD
6121 	 * does so to make it more readable in packet traces.
6122 	 */
6123 	cookie = fxdr_hyper(tl); tl += 2;
6124 	len = fxdr_unsigned(uint32_t, *tl);
6125 	if (len == 0 || cookie >= IOSIZE_MAX) {
6126 		nd->nd_repstat = NFSERR_BADXDR;
6127 		goto nfsmout;
6128 	}
6129 	if (len > nd->nd_maxresp - NFS_MAXXDR)
6130 		len = nd->nd_maxresp - NFS_MAXXDR;
6131 	len2 = len;
6132 	nd->nd_repstat = nfsvno_listxattr(vp, cookie, nd->nd_cred, p, &buf,
6133 	    &len, &eof);
6134 	if (nd->nd_repstat == EOPNOTSUPP)
6135 		nd->nd_repstat = NFSERR_NOTSUPP;
6136 	if (nd->nd_repstat == 0) {
6137 		cookie2 = cookie + len;
6138 		if (cookie2 < cookie)
6139 			nd->nd_repstat = NFSERR_BADXDR;
6140 	}
6141 	retlen = NFSX_HYPER + 2 * NFSX_UNSIGNED;
6142 	if (nd->nd_repstat == 0 && len2 < retlen)
6143 		nd->nd_repstat = NFSERR_TOOSMALL;
6144 	if (nd->nd_repstat == 0) {
6145 		/* Now copy the entries out. */
6146 		if (len == 0) {
6147 			/* The cookie was at eof. */
6148 			NFSM_BUILD(tl, uint32_t *, NFSX_HYPER + 2 *
6149 			    NFSX_UNSIGNED);
6150 			txdr_hyper(cookie2, tl); tl += 2;
6151 			*tl++ = txdr_unsigned(0);
6152 			*tl = newnfs_true;
6153 			goto nfsmout;
6154 		}
6155 
6156 		/* Sanity check the cookie. */
6157 		for (pos = 0; pos < len; pos += (i + 1)) {
6158 			if (pos == cookie)
6159 				break;
6160 			i = buf[pos];
6161 		}
6162 		if (pos != cookie) {
6163 			nd->nd_repstat = NFSERR_INVAL;
6164 			goto nfsmout;
6165 		}
6166 
6167 		/* Loop around copying the entrie(s) out. */
6168 		cnt = 0;
6169 		len -= cookie;
6170 		i = buf[pos];
6171 		while (i < len && len2 >= retlen + NFSM_RNDUP(i) +
6172 		    NFSX_UNSIGNED) {
6173 			if (cnt == 0) {
6174 				NFSM_BUILD(tl, uint32_t *, NFSX_HYPER +
6175 				    NFSX_UNSIGNED);
6176 				txdr_hyper(cookie2, tl); tl += 2;
6177 			}
6178 			retlen += nfsm_strtom(nd, &buf[pos + 1], i);
6179 			len -= (i + 1);
6180 			pos += (i + 1);
6181 			i = buf[pos];
6182 			cnt++;
6183 		}
6184 		/*
6185 		 * eof is set true/false by nfsvno_listxattr(), but if we
6186 		 * can't copy all entries returned by nfsvno_listxattr(),
6187 		 * we are not at eof.
6188 		 */
6189 		if (len > 0)
6190 			eof = false;
6191 		if (cnt > 0) {
6192 			/* *tl is set above. */
6193 			*tl = txdr_unsigned(cnt);
6194 			NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
6195 			if (eof)
6196 				*tl = newnfs_true;
6197 			else
6198 				*tl = newnfs_false;
6199 		} else
6200 			nd->nd_repstat = NFSERR_TOOSMALL;
6201 	}
6202 
6203 nfsmout:
6204 	free(buf, M_TEMP);
6205 	if (nd->nd_repstat == 0)
6206 		nd->nd_repstat = error;
6207 	vput(vp);
6208 	NFSEXITCODE2(0, nd);
6209 	return (0);
6210 }
6211 
6212 /*
6213  * nfsv4 service not supported
6214  */
6215 int
6216 nfsrvd_notsupp(struct nfsrv_descript *nd, __unused int isdgram,
6217     __unused vnode_t vp, __unused struct nfsexstuff *exp)
6218 {
6219 
6220 	nd->nd_repstat = NFSERR_NOTSUPP;
6221 	NFSEXITCODE2(0, nd);
6222 	return (0);
6223 }
6224