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