2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Rick Macklem at The University of Guelph.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
40 #include "opt_inet6.h"
42 * nfs version 2, 3 and 4 server calls to vnode ops
43 * - these routines generally have 3 phases
44 * 1 - break down and validate rpc request in mbuf list
45 * 2 - do the vnode ops for the request, usually by calling a nfsvno_XXX()
46 * function in nfsd_port.c
47 * 3 - build the rpc reply in an mbuf list
48 * For nfsv4, these functions are called for each Op within the Compound RPC.
51 #include <fs/nfs/nfsport.h>
52 #include <sys/extattr.h>
53 #include <sys/filio.h>
56 extern u_int32_t newnfs_false, newnfs_true;
57 extern enum vtype nv34tov_type[8];
58 extern struct timeval nfsboottime;
59 extern int nfsrv_enable_crossmntpt;
60 extern int nfsrv_statehashsize;
61 extern int nfsrv_layouthashsize;
62 extern time_t nfsdev_time;
63 extern volatile int nfsrv_devidcnt;
64 extern int nfsd_debuglevel;
65 extern u_long sb_max_adj;
66 extern int nfsrv_pnfsatime;
67 extern int nfsrv_maxpnfsmirror;
68 extern uint32_t nfs_srvmaxio;
70 static int nfs_async = 0;
71 SYSCTL_DECL(_vfs_nfsd);
72 SYSCTL_INT(_vfs_nfsd, OID_AUTO, async, CTLFLAG_RW, &nfs_async, 0,
73 "Tell client that writes were synced even though they were not");
74 extern int nfsrv_doflexfile;
75 SYSCTL_INT(_vfs_nfsd, OID_AUTO, default_flexfile, CTLFLAG_RW,
76 &nfsrv_doflexfile, 0, "Make Flex File Layout the default for pNFS");
77 static int nfsrv_linux42server = 1;
78 SYSCTL_INT(_vfs_nfsd, OID_AUTO, linux42server, CTLFLAG_RW,
79 &nfsrv_linux42server, 0,
80 "Enable Linux style NFSv4.2 server (non-RFC compliant)");
81 static bool nfsrv_openaccess = true;
82 SYSCTL_BOOL(_vfs_nfsd, OID_AUTO, v4openaccess, CTLFLAG_RW,
84 "Enable Linux style NFSv4 Open access check");
85 static char nfsrv_scope[NFSV4_OPAQUELIMIT];
86 SYSCTL_STRING(_vfs_nfsd, OID_AUTO, scope, CTLFLAG_RWTUN,
87 &nfsrv_scope, NFSV4_OPAQUELIMIT, "Server scope");
88 static char nfsrv_owner_major[NFSV4_OPAQUELIMIT];
89 SYSCTL_STRING(_vfs_nfsd, OID_AUTO, owner_major, CTLFLAG_RWTUN,
90 &nfsrv_owner_major, NFSV4_OPAQUELIMIT, "Server owner major");
91 static uint64_t nfsrv_owner_minor;
92 SYSCTL_U64(_vfs_nfsd, OID_AUTO, owner_minor, CTLFLAG_RWTUN,
93 &nfsrv_owner_minor, 0, "Server owner minor");
95 * Only enable this if all your exported file systems
96 * (or pNFS DSs for the pNFS case) support VOP_ALLOCATE.
98 static bool nfsrv_doallocate = false;
99 SYSCTL_BOOL(_vfs_nfsd, OID_AUTO, enable_v42allocate, CTLFLAG_RW,
100 &nfsrv_doallocate, 0,
101 "Enable NFSv4.2 Allocate operation");
104 * This list defines the GSS mechanisms supported.
105 * (Don't ask me how you get these strings from the RFC stuff like
106 * iso(1), org(3)... but someone did it, so I don't need to know.)
108 static struct nfsgss_mechlist nfsgss_mechlist[] = {
109 { 9, "\052\206\110\206\367\022\001\002\002", 11 },
113 /* local functions */
114 static void nfsrvd_symlinksub(struct nfsrv_descript *nd, struct nameidata *ndp,
115 struct nfsvattr *nvap, fhandle_t *fhp, vnode_t *vpp,
116 vnode_t dirp, struct nfsvattr *dirforp, struct nfsvattr *diraftp,
117 int *diraft_retp, nfsattrbit_t *attrbitp,
118 NFSACL_T *aclp, NFSPROC_T *p, struct nfsexstuff *exp, char *pathcp,
120 static void nfsrvd_mkdirsub(struct nfsrv_descript *nd, struct nameidata *ndp,
121 struct nfsvattr *nvap, fhandle_t *fhp, vnode_t *vpp,
122 vnode_t dirp, struct nfsvattr *dirforp, struct nfsvattr *diraftp,
123 int *diraft_retp, nfsattrbit_t *attrbitp, NFSACL_T *aclp,
124 NFSPROC_T *p, struct nfsexstuff *exp);
127 * nfs access service (not a part of NFS V2)
130 nfsrvd_access(struct nfsrv_descript *nd, __unused int isdgram,
131 vnode_t vp, struct nfsexstuff *exp)
134 int getret, error = 0;
136 u_int32_t testmode, nfsmode, supported = 0;
138 struct thread *p = curthread;
140 if (nd->nd_repstat) {
141 nfsrv_postopattr(nd, 1, &nva);
144 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
145 nfsmode = fxdr_unsigned(u_int32_t, *tl);
146 if ((nd->nd_flag & ND_NFSV4) &&
147 (nfsmode & ~(NFSACCESS_READ | NFSACCESS_LOOKUP |
148 NFSACCESS_MODIFY | NFSACCESS_EXTEND | NFSACCESS_DELETE |
149 NFSACCESS_EXECUTE | NFSACCESS_XAREAD | NFSACCESS_XAWRITE |
150 NFSACCESS_XALIST))) {
151 nd->nd_repstat = NFSERR_INVAL;
155 if (nfsmode & NFSACCESS_READ) {
156 supported |= NFSACCESS_READ;
157 if (nfsvno_accchk(vp, VREAD, nd->nd_cred, exp, p,
158 NFSACCCHK_NOOVERRIDE, NFSACCCHK_VPISLOCKED, &supported))
159 nfsmode &= ~NFSACCESS_READ;
161 if (nfsmode & NFSACCESS_MODIFY) {
162 supported |= NFSACCESS_MODIFY;
163 if (nfsvno_accchk(vp, VWRITE, nd->nd_cred, exp, p,
164 NFSACCCHK_NOOVERRIDE, NFSACCCHK_VPISLOCKED, &supported))
165 nfsmode &= ~NFSACCESS_MODIFY;
167 if (nfsmode & NFSACCESS_EXTEND) {
168 supported |= NFSACCESS_EXTEND;
169 if (nfsvno_accchk(vp, VWRITE | VAPPEND, nd->nd_cred, exp, p,
170 NFSACCCHK_NOOVERRIDE, NFSACCCHK_VPISLOCKED, &supported))
171 nfsmode &= ~NFSACCESS_EXTEND;
173 if (nfsmode & NFSACCESS_XAREAD) {
174 supported |= NFSACCESS_XAREAD;
175 if (nfsvno_accchk(vp, VREAD, nd->nd_cred, exp, p,
176 NFSACCCHK_NOOVERRIDE, NFSACCCHK_VPISLOCKED, &supported))
177 nfsmode &= ~NFSACCESS_XAREAD;
179 if (nfsmode & NFSACCESS_XAWRITE) {
180 supported |= NFSACCESS_XAWRITE;
181 if (nfsvno_accchk(vp, VWRITE, nd->nd_cred, exp, p,
182 NFSACCCHK_NOOVERRIDE, NFSACCCHK_VPISLOCKED, &supported))
183 nfsmode &= ~NFSACCESS_XAWRITE;
185 if (nfsmode & NFSACCESS_XALIST) {
186 supported |= NFSACCESS_XALIST;
187 if (nfsvno_accchk(vp, VREAD, nd->nd_cred, exp, p,
188 NFSACCCHK_NOOVERRIDE, NFSACCCHK_VPISLOCKED, &supported))
189 nfsmode &= ~NFSACCESS_XALIST;
191 if (nfsmode & NFSACCESS_DELETE) {
192 supported |= NFSACCESS_DELETE;
193 if (vp->v_type == VDIR)
194 deletebit = VDELETE_CHILD;
197 if (nfsvno_accchk(vp, deletebit, nd->nd_cred, exp, p,
198 NFSACCCHK_NOOVERRIDE, NFSACCCHK_VPISLOCKED, &supported))
199 nfsmode &= ~NFSACCESS_DELETE;
201 if (vp->v_type == VDIR)
202 testmode = NFSACCESS_LOOKUP;
204 testmode = NFSACCESS_EXECUTE;
205 if (nfsmode & testmode) {
206 supported |= (nfsmode & testmode);
207 if (nfsvno_accchk(vp, VEXEC, nd->nd_cred, exp, p,
208 NFSACCCHK_NOOVERRIDE, NFSACCCHK_VPISLOCKED, &supported))
209 nfsmode &= ~testmode;
211 nfsmode &= supported;
212 if (nd->nd_flag & ND_NFSV3) {
213 getret = nfsvno_getattr(vp, &nva, nd, p, 1, NULL);
214 nfsrv_postopattr(nd, getret, &nva);
217 if (nd->nd_flag & ND_NFSV4) {
218 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
219 *tl++ = txdr_unsigned(supported);
221 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
222 *tl = txdr_unsigned(nfsmode);
229 NFSEXITCODE2(error, nd);
234 * nfs getattr service
237 nfsrvd_getattr(struct nfsrv_descript *nd, int isdgram,
238 vnode_t vp, __unused struct nfsexstuff *exp)
242 int at_root = 0, error = 0, supports_nfsv4acls;
243 struct nfsreferral *refp;
244 nfsattrbit_t attrbits, tmpbits;
246 struct vnode *tvp = NULL;
248 uint64_t mounted_on_fileno = 0;
250 struct thread *p = curthread;
254 if (nd->nd_flag & ND_NFSV4) {
255 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
262 * Check for a referral.
264 refp = nfsv4root_getreferral(vp, NULL, 0);
266 (void) nfsrv_putreferralattr(nd, &attrbits, refp, 1,
271 if (nd->nd_repstat == 0) {
273 NFSSET_ATTRBIT(&tmpbits, &attrbits);
276 * GETATTR with write-only attr time_access_set and time_modify_set
277 * should return NFS4ERR_INVAL.
279 if (NFSISSET_ATTRBIT(&tmpbits, NFSATTRBIT_TIMEACCESSSET) ||
280 NFSISSET_ATTRBIT(&tmpbits, NFSATTRBIT_TIMEMODIFYSET)){
281 error = NFSERR_INVAL;
285 if (NFSISSET_ATTRBIT(&tmpbits, NFSATTRBIT_ACL)) {
286 NFSCLRBIT_ATTRBIT(&tmpbits, NFSATTRBIT_ACL);
287 accmode |= VREAD_ACL;
289 if (NFSNONZERO_ATTRBIT(&tmpbits))
290 accmode |= VREAD_ATTRIBUTES;
292 nd->nd_repstat = nfsvno_accchk(vp, accmode,
293 nd->nd_cred, exp, p, NFSACCCHK_NOOVERRIDE,
294 NFSACCCHK_VPISLOCKED, NULL);
298 nd->nd_repstat = nfsvno_getattr(vp, &nva, nd, p, 1, &attrbits);
299 if (!nd->nd_repstat) {
300 if (nd->nd_flag & ND_NFSV4) {
301 if (NFSISSET_ATTRBIT(&attrbits, NFSATTRBIT_FILEHANDLE))
302 nd->nd_repstat = nfsvno_getfh(vp, &fh, p);
304 nd->nd_repstat = nfsrv_checkgetattr(nd, vp,
306 if (nd->nd_repstat == 0) {
307 supports_nfsv4acls = nfs_supportsnfsv4acls(vp);
309 if (nfsrv_enable_crossmntpt != 0 &&
310 vp->v_type == VDIR &&
311 (vp->v_vflag & VV_ROOT) != 0 &&
313 tvp = mp->mnt_vnodecovered;
321 if ((nd->nd_repstat =
322 NFSVOPLOCK(tvp, LK_SHARED)) == 0) {
323 nd->nd_repstat = VOP_GETATTR(
324 tvp, &va, nd->nd_cred);
328 if (nd->nd_repstat == 0)
329 mounted_on_fileno = (uint64_t)
334 if (nd->nd_repstat == 0)
335 nd->nd_repstat = vfs_busy(mp, 0);
337 if (nd->nd_repstat == 0) {
338 (void)nfsvno_fillattr(nd, mp, vp, &nva,
339 &fh, 0, &attrbits, nd->nd_cred, p,
340 isdgram, 1, supports_nfsv4acls,
341 at_root, mounted_on_fileno);
348 nfsrv_fillattr(nd, &nva);
356 NFSEXITCODE2(error, nd);
361 * nfs setattr service
364 nfsrvd_setattr(struct nfsrv_descript *nd, __unused int isdgram,
365 vnode_t vp, struct nfsexstuff *exp)
367 struct nfsvattr nva, nva2;
369 int preat_ret = 1, postat_ret = 1, gcheck = 0, error = 0;
371 struct timespec guard = { 0, 0 };
372 nfsattrbit_t attrbits, retbits;
373 nfsv4stateid_t stateid;
374 NFSACL_T *aclp = NULL;
375 struct thread *p = curthread;
377 if (nd->nd_repstat) {
378 nfsrv_wcc(nd, preat_ret, &nva2, postat_ret, &nva);
381 #ifdef NFS4_ACL_EXTATTR_NAME
382 aclp = acl_alloc(M_WAITOK);
386 NFSVNO_ATTRINIT(&nva);
387 if (nd->nd_flag & ND_NFSV4) {
388 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID);
389 stateid.seqid = fxdr_unsigned(u_int32_t, *tl++);
390 stateid.other[0] = *tl++;
391 stateid.other[1] = *tl++;
392 stateid.other[2] = *tl;
393 if (stateid.other[0] == 0x55555555 &&
394 stateid.other[1] == 0x55555555 &&
395 stateid.other[2] == 0x55555555 &&
396 stateid.seqid == 0xffffffff)
399 error = nfsrv_sattr(nd, vp, &nva, &attrbits, aclp, p);
403 /* For NFSv4, only va_uid is used from nva2. */
404 NFSZERO_ATTRBIT(&retbits);
405 NFSSETBIT_ATTRBIT(&retbits, NFSATTRBIT_OWNER);
406 preat_ret = nfsvno_getattr(vp, &nva2, nd, p, 1, &retbits);
408 nd->nd_repstat = preat_ret;
410 NFSZERO_ATTRBIT(&retbits);
411 if (nd->nd_flag & ND_NFSV3) {
412 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
413 gcheck = fxdr_unsigned(int, *tl);
415 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
416 fxdr_nfsv3time(tl, &guard);
418 if (!nd->nd_repstat && gcheck &&
419 (nva2.na_ctime.tv_sec != guard.tv_sec ||
420 nva2.na_ctime.tv_nsec != guard.tv_nsec))
421 nd->nd_repstat = NFSERR_NOT_SYNC;
422 if (nd->nd_repstat) {
424 #ifdef NFS4_ACL_EXTATTR_NAME
427 nfsrv_wcc(nd, preat_ret, &nva2, postat_ret, &nva);
430 } else if (!nd->nd_repstat && (nd->nd_flag & ND_NFSV4))
431 nd->nd_repstat = nfsrv_checkuidgid(nd, &nva);
434 * Now that we have all the fields, lets do it.
435 * If the size is being changed write access is required, otherwise
436 * just check for a read only file system.
438 if (!nd->nd_repstat) {
439 if (NFSVNO_NOTSETSIZE(&nva)) {
440 if (NFSVNO_EXRDONLY(exp) ||
441 (vp->v_mount->mnt_flag & MNT_RDONLY))
442 nd->nd_repstat = EROFS;
444 if (vp->v_type != VREG)
445 nd->nd_repstat = EINVAL;
446 else if (nva2.na_uid != nd->nd_cred->cr_uid ||
447 NFSVNO_EXSTRICTACCESS(exp))
448 nd->nd_repstat = nfsvno_accchk(vp,
449 VWRITE, nd->nd_cred, exp, p,
450 NFSACCCHK_NOOVERRIDE,
451 NFSACCCHK_VPISLOCKED, NULL);
455 * Proxy operations from the MDS are allowed via the all 0s special
458 if (nd->nd_repstat == 0 && (nd->nd_flag & ND_NFSV4) != 0 &&
459 gotproxystateid == 0)
460 nd->nd_repstat = nfsrv_checksetattr(vp, nd, &stateid,
461 &nva, &attrbits, exp, p);
463 if (!nd->nd_repstat && (nd->nd_flag & ND_NFSV4)) {
465 * For V4, try setting the attributes in sets, so that the
466 * reply bitmap will be correct for an error case.
468 if (NFSISSET_ATTRBIT(&attrbits, NFSATTRBIT_OWNER) ||
469 NFSISSET_ATTRBIT(&attrbits, NFSATTRBIT_OWNERGROUP)) {
470 NFSVNO_ATTRINIT(&nva2);
471 NFSVNO_SETATTRVAL(&nva2, uid, nva.na_uid);
472 NFSVNO_SETATTRVAL(&nva2, gid, nva.na_gid);
473 nd->nd_repstat = nfsvno_setattr(vp, &nva2, nd->nd_cred, p,
475 if (!nd->nd_repstat) {
476 if (NFSISSET_ATTRBIT(&attrbits, NFSATTRBIT_OWNER))
477 NFSSETBIT_ATTRBIT(&retbits, NFSATTRBIT_OWNER);
478 if (NFSISSET_ATTRBIT(&attrbits, NFSATTRBIT_OWNERGROUP))
479 NFSSETBIT_ATTRBIT(&retbits, NFSATTRBIT_OWNERGROUP);
482 if (!nd->nd_repstat &&
483 NFSISSET_ATTRBIT(&attrbits, NFSATTRBIT_SIZE)) {
484 NFSVNO_ATTRINIT(&nva2);
485 NFSVNO_SETATTRVAL(&nva2, size, nva.na_size);
486 nd->nd_repstat = nfsvno_setattr(vp, &nva2, nd->nd_cred, p,
489 NFSSETBIT_ATTRBIT(&retbits, NFSATTRBIT_SIZE);
491 if (!nd->nd_repstat &&
492 (NFSISSET_ATTRBIT(&attrbits, NFSATTRBIT_TIMEACCESSSET) ||
493 NFSISSET_ATTRBIT(&attrbits, NFSATTRBIT_TIMEMODIFYSET))) {
494 NFSVNO_ATTRINIT(&nva2);
495 NFSVNO_SETATTRVAL(&nva2, atime, nva.na_atime);
496 NFSVNO_SETATTRVAL(&nva2, mtime, nva.na_mtime);
497 if (nva.na_vaflags & VA_UTIMES_NULL) {
498 nva2.na_vaflags |= VA_UTIMES_NULL;
499 NFSVNO_SETACTIVE(&nva2, vaflags);
501 nd->nd_repstat = nfsvno_setattr(vp, &nva2, nd->nd_cred, p,
503 if (!nd->nd_repstat) {
504 if (NFSISSET_ATTRBIT(&attrbits, NFSATTRBIT_TIMEACCESSSET))
505 NFSSETBIT_ATTRBIT(&retbits, NFSATTRBIT_TIMEACCESSSET);
506 if (NFSISSET_ATTRBIT(&attrbits, NFSATTRBIT_TIMEMODIFYSET))
507 NFSSETBIT_ATTRBIT(&retbits, NFSATTRBIT_TIMEMODIFYSET);
510 if (!nd->nd_repstat &&
511 NFSISSET_ATTRBIT(&attrbits, NFSATTRBIT_TIMECREATE)) {
512 NFSVNO_ATTRINIT(&nva2);
513 NFSVNO_SETATTRVAL(&nva2, btime, nva.na_btime);
514 nd->nd_repstat = nfsvno_setattr(vp, &nva2, nd->nd_cred, p,
517 NFSSETBIT_ATTRBIT(&retbits, NFSATTRBIT_TIMECREATE);
519 if (!nd->nd_repstat &&
520 (NFSISSET_ATTRBIT(&attrbits, NFSATTRBIT_MODE) ||
521 NFSISSET_ATTRBIT(&attrbits, NFSATTRBIT_MODESETMASKED))) {
522 NFSVNO_ATTRINIT(&nva2);
523 NFSVNO_SETATTRVAL(&nva2, mode, nva.na_mode);
524 nd->nd_repstat = nfsvno_setattr(vp, &nva2, nd->nd_cred, p,
526 if (!nd->nd_repstat) {
527 if (NFSISSET_ATTRBIT(&attrbits, NFSATTRBIT_MODE))
528 NFSSETBIT_ATTRBIT(&retbits, NFSATTRBIT_MODE);
529 if (NFSISSET_ATTRBIT(&attrbits, NFSATTRBIT_MODESETMASKED))
530 NFSSETBIT_ATTRBIT(&retbits, NFSATTRBIT_MODESETMASKED);
534 #ifdef NFS4_ACL_EXTATTR_NAME
535 if (!nd->nd_repstat && aclp->acl_cnt > 0 &&
536 NFSISSET_ATTRBIT(&attrbits, NFSATTRBIT_ACL)) {
537 nd->nd_repstat = nfsrv_setacl(vp, aclp, nd->nd_cred, p);
539 NFSSETBIT_ATTRBIT(&retbits, NFSATTRBIT_ACL);
542 } else if (!nd->nd_repstat) {
543 nd->nd_repstat = nfsvno_setattr(vp, &nva, nd->nd_cred, p,
546 if (nd->nd_flag & (ND_NFSV2 | ND_NFSV3)) {
547 postat_ret = nfsvno_getattr(vp, &nva, nd, p, 1, NULL);
549 nd->nd_repstat = postat_ret;
552 #ifdef NFS4_ACL_EXTATTR_NAME
555 if (nd->nd_flag & ND_NFSV3)
556 nfsrv_wcc(nd, preat_ret, &nva2, postat_ret, &nva);
557 else if (nd->nd_flag & ND_NFSV4)
558 (void) nfsrv_putattrbit(nd, &retbits);
559 else if (!nd->nd_repstat)
560 nfsrv_fillattr(nd, &nva);
567 #ifdef NFS4_ACL_EXTATTR_NAME
570 if (nd->nd_flag & ND_NFSV4) {
572 * For all nd_repstat, the V4 reply includes a bitmap,
573 * even NFSERR_BADXDR, which is what this will end up
576 (void) nfsrv_putattrbit(nd, &retbits);
578 NFSEXITCODE2(error, nd);
584 * (Also performs lookup parent for v4)
587 nfsrvd_lookup(struct nfsrv_descript *nd, __unused int isdgram,
588 vnode_t dp, vnode_t *vpp, fhandle_t *fhp, struct nfsexstuff *exp)
590 struct nameidata named;
591 vnode_t vp, dirp = NULL;
592 int error = 0, dattr_ret = 1;
593 struct nfsvattr nva, dattr;
596 struct thread *p = curthread;
598 if (nd->nd_repstat) {
599 nfsrv_postopattr(nd, dattr_ret, &dattr);
604 * For some reason, if dp is a symlink, the error
605 * returned is supposed to be NFSERR_SYMLINK and not NFSERR_NOTDIR.
607 if (dp->v_type == VLNK && (nd->nd_flag & ND_NFSV4)) {
608 nd->nd_repstat = NFSERR_SYMLINK;
613 NFSNAMEICNDSET(&named.ni_cnd, nd->nd_cred, LOOKUP,
615 nfsvno_setpathbuf(&named, &bufp, &hashp);
616 error = nfsrv_parsename(nd, bufp, hashp, &named.ni_pathlen);
619 nfsvno_relpathbuf(&named);
622 if (!nd->nd_repstat) {
623 nd->nd_repstat = nfsvno_namei(nd, &named, dp, 0, exp, &dirp);
626 nfsvno_relpathbuf(&named);
628 if (nd->nd_repstat) {
630 if (nd->nd_flag & ND_NFSV3)
631 dattr_ret = nfsvno_getattr(dirp, &dattr, nd, p,
635 if (nd->nd_flag & ND_NFSV3)
636 nfsrv_postopattr(nd, dattr_ret, &dattr);
639 nfsvno_relpathbuf(&named);
641 if ((nd->nd_flag & ND_NFSV4) != 0 && !NFSVNO_EXPORTED(exp) &&
642 vp->v_type != VDIR && vp->v_type != VLNK)
644 * Only allow lookup of VDIR and VLNK for traversal of
645 * non-exported volumes during NFSv4 mounting.
647 nd->nd_repstat = ENOENT;
648 if (nd->nd_repstat == 0) {
649 nd->nd_repstat = nfsvno_getfh(vp, fhp, p);
651 * EOPNOTSUPP indicates the file system cannot be exported,
652 * so just pretend the entry does not exist.
654 if (nd->nd_repstat == EOPNOTSUPP)
655 nd->nd_repstat = ENOENT;
657 if (!(nd->nd_flag & ND_NFSV4) && !nd->nd_repstat)
658 nd->nd_repstat = nfsvno_getattr(vp, &nva, nd, p, 1, NULL);
659 if (vpp != NULL && nd->nd_repstat == 0)
664 if (nd->nd_flag & ND_NFSV3)
665 dattr_ret = nfsvno_getattr(dirp, &dattr, nd, p, 0,
669 if (nd->nd_repstat) {
670 if (nd->nd_flag & ND_NFSV3)
671 nfsrv_postopattr(nd, dattr_ret, &dattr);
674 if (nd->nd_flag & ND_NFSV2) {
675 (void) nfsm_fhtom(nd, (u_int8_t *)fhp, 0, 0);
676 nfsrv_fillattr(nd, &nva);
677 } else if (nd->nd_flag & ND_NFSV3) {
678 (void) nfsm_fhtom(nd, (u_int8_t *)fhp, 0, 0);
679 nfsrv_postopattr(nd, 0, &nva);
680 nfsrv_postopattr(nd, dattr_ret, &dattr);
684 NFSEXITCODE2(error, nd);
689 * nfs readlink service
692 nfsrvd_readlink(struct nfsrv_descript *nd, __unused int isdgram,
693 vnode_t vp, __unused struct nfsexstuff *exp)
696 struct mbuf *mp = NULL, *mpend = NULL;
699 struct thread *p = curthread;
702 if (nd->nd_repstat) {
703 nfsrv_postopattr(nd, getret, &nva);
706 if (vp->v_type != VLNK) {
707 if (nd->nd_flag & ND_NFSV2)
708 nd->nd_repstat = ENXIO;
710 nd->nd_repstat = EINVAL;
712 if (nd->nd_repstat == 0) {
713 if ((nd->nd_flag & ND_EXTPG) != 0)
714 nd->nd_repstat = nfsvno_readlink(vp, nd->nd_cred,
715 nd->nd_maxextsiz, p, &mp, &mpend, &len);
717 nd->nd_repstat = nfsvno_readlink(vp, nd->nd_cred,
718 0, p, &mp, &mpend, &len);
720 if (nd->nd_flag & ND_NFSV3)
721 getret = nfsvno_getattr(vp, &nva, nd, p, 1, NULL);
723 if (nd->nd_flag & ND_NFSV3)
724 nfsrv_postopattr(nd, getret, &nva);
727 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
728 *tl = txdr_unsigned(len);
730 nd->nd_mb->m_next = mp;
732 if ((mpend->m_flags & M_EXTPG) != 0) {
733 nd->nd_bextpg = mpend->m_epg_npgs - 1;
734 nd->nd_bpos = (char *)(void *)
735 PHYS_TO_DMAP(mpend->m_epg_pa[nd->nd_bextpg]);
736 off = (nd->nd_bextpg == 0) ? mpend->m_epg_1st_off : 0;
737 nd->nd_bpos += off + mpend->m_epg_last_len;
738 nd->nd_bextpgsiz = PAGE_SIZE - mpend->m_epg_last_len -
741 nd->nd_bpos = mtod(mpend, char *) + mpend->m_len;
753 nfsrvd_read(struct nfsrv_descript *nd, __unused int isdgram,
754 vnode_t vp, struct nfsexstuff *exp)
757 int error = 0, cnt, getret = 1, gotproxystateid, reqlen, eof = 0;
758 struct mbuf *m2, *m3;
761 struct nfsstate st, *stp = &st;
762 struct nfslock lo, *lop = &lo;
763 nfsv4stateid_t stateid;
765 struct thread *p = curthread;
768 if (nd->nd_repstat) {
769 nfsrv_postopattr(nd, getret, &nva);
772 if (nd->nd_flag & ND_NFSV2) {
773 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
774 off = (off_t)fxdr_unsigned(u_int32_t, *tl++);
775 reqlen = fxdr_unsigned(int, *tl);
776 } else if (nd->nd_flag & ND_NFSV3) {
777 NFSM_DISSECT(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
778 off = fxdr_hyper(tl);
780 reqlen = fxdr_unsigned(int, *tl);
782 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID + 3*NFSX_UNSIGNED);
783 reqlen = fxdr_unsigned(int, *(tl + 6));
785 if (reqlen > NFS_SRVMAXDATA(nd)) {
786 reqlen = NFS_SRVMAXDATA(nd);
787 } else if (reqlen < 0) {
792 if (nd->nd_flag & ND_NFSV4) {
793 stp->ls_flags = (NFSLCK_CHECK | NFSLCK_READACCESS);
794 lop->lo_flags = NFSLCK_READ;
795 stp->ls_ownerlen = 0;
797 stp->ls_uid = nd->nd_cred->cr_uid;
798 stp->ls_stateid.seqid = fxdr_unsigned(u_int32_t, *tl++);
799 clientid.lval[0] = stp->ls_stateid.other[0] = *tl++;
800 clientid.lval[1] = stp->ls_stateid.other[1] = *tl++;
801 if ((nd->nd_flag & ND_IMPLIEDCLID) != 0) {
802 if ((nd->nd_flag & ND_NFSV41) != 0)
803 clientid.qval = nd->nd_clientid.qval;
804 else if (nd->nd_clientid.qval != clientid.qval)
805 printf("EEK1 multiple clids\n");
807 if ((nd->nd_flag & ND_NFSV41) != 0)
808 printf("EEK! no clientid from session\n");
809 nd->nd_flag |= ND_IMPLIEDCLID;
810 nd->nd_clientid.qval = clientid.qval;
812 stp->ls_stateid.other[2] = *tl++;
814 * Don't allow the client to use a special stateid for a DS op.
816 if ((nd->nd_flag & ND_DSSERVER) != 0 &&
817 ((stp->ls_stateid.other[0] == 0x0 &&
818 stp->ls_stateid.other[1] == 0x0 &&
819 stp->ls_stateid.other[2] == 0x0) ||
820 (stp->ls_stateid.other[0] == 0xffffffff &&
821 stp->ls_stateid.other[1] == 0xffffffff &&
822 stp->ls_stateid.other[2] == 0xffffffff) ||
823 stp->ls_stateid.seqid != 0))
824 nd->nd_repstat = NFSERR_BADSTATEID;
825 /* However, allow the proxy stateid. */
826 if (stp->ls_stateid.seqid == 0xffffffff &&
827 stp->ls_stateid.other[0] == 0x55555555 &&
828 stp->ls_stateid.other[1] == 0x55555555 &&
829 stp->ls_stateid.other[2] == 0x55555555)
831 off = fxdr_hyper(tl);
834 lop->lo_end = off + reqlen;
836 * Paranoia, just in case it wraps around.
838 if (lop->lo_end < off)
839 lop->lo_end = NFS64BITSSET;
841 if (vp->v_type != VREG) {
842 if (nd->nd_flag & ND_NFSV3)
843 nd->nd_repstat = EINVAL;
845 nd->nd_repstat = (vp->v_type == VDIR) ? EISDIR :
848 getret = nfsvno_getattr(vp, &nva, nd, p, 1, NULL);
850 nd->nd_repstat = getret;
851 if (!nd->nd_repstat &&
852 (nva.na_uid != nd->nd_cred->cr_uid ||
853 NFSVNO_EXSTRICTACCESS(exp))) {
854 nd->nd_repstat = nfsvno_accchk(vp, VREAD,
856 NFSACCCHK_ALLOWOWNER, NFSACCCHK_VPISLOCKED, NULL);
858 nd->nd_repstat = nfsvno_accchk(vp, VEXEC,
859 nd->nd_cred, exp, p, NFSACCCHK_ALLOWOWNER,
860 NFSACCCHK_VPISLOCKED, NULL);
863 * DS reads are marked by ND_DSSERVER or use the proxy special
866 if (nd->nd_repstat == 0 && (nd->nd_flag & (ND_NFSV4 | ND_DSSERVER)) ==
867 ND_NFSV4 && gotproxystateid == 0)
868 nd->nd_repstat = nfsrv_lockctrl(vp, &stp, &lop, NULL, clientid,
869 &stateid, exp, nd, p);
870 if (nd->nd_repstat) {
872 if (nd->nd_flag & ND_NFSV3)
873 nfsrv_postopattr(nd, getret, &nva);
876 if (off >= nva.na_size) {
879 } else if (reqlen == 0)
881 else if ((off + reqlen) >= nva.na_size) {
882 cnt = nva.na_size - off;
889 * If cnt > MCLBYTES and the reply will not be saved, use
890 * ext_pgs mbufs for TLS.
891 * For NFSv4.0, we do not know for sure if the reply will
892 * be saved, so do not use ext_pgs mbufs for NFSv4.0.
893 * Always use ext_pgs mbufs if ND_EXTPG is set.
895 if ((nd->nd_flag & ND_EXTPG) != 0 || (cnt > MCLBYTES &&
896 (nd->nd_flag & (ND_TLS | ND_SAVEREPLY)) == ND_TLS &&
897 (nd->nd_flag & (ND_NFSV4 | ND_NFSV41)) != ND_NFSV4))
898 nd->nd_repstat = nfsvno_read(vp, off, cnt, nd->nd_cred,
899 nd->nd_maxextsiz, p, &m3, &m2);
901 nd->nd_repstat = nfsvno_read(vp, off, cnt, nd->nd_cred,
903 if (!(nd->nd_flag & ND_NFSV4)) {
904 getret = nfsvno_getattr(vp, &nva, nd, p, 1, NULL);
906 nd->nd_repstat = getret;
908 if (nd->nd_repstat) {
912 if (nd->nd_flag & ND_NFSV3)
913 nfsrv_postopattr(nd, getret, &nva);
918 if (nd->nd_flag & ND_NFSV2) {
919 nfsrv_fillattr(nd, &nva);
920 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
922 if (nd->nd_flag & ND_NFSV3) {
923 nfsrv_postopattr(nd, getret, &nva);
924 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
925 *tl++ = txdr_unsigned(cnt);
927 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
931 *tl++ = newnfs_false;
933 *tl = txdr_unsigned(cnt);
935 nd->nd_mb->m_next = m3;
937 if ((m2->m_flags & M_EXTPG) != 0) {
938 nd->nd_flag |= ND_EXTPG;
939 nd->nd_bextpg = m2->m_epg_npgs - 1;
940 nd->nd_bpos = (char *)(void *)
941 PHYS_TO_DMAP(m2->m_epg_pa[nd->nd_bextpg]);
942 poff = (nd->nd_bextpg == 0) ? m2->m_epg_1st_off : 0;
943 nd->nd_bpos += poff + m2->m_epg_last_len;
944 nd->nd_bextpgsiz = PAGE_SIZE - m2->m_epg_last_len -
947 nd->nd_bpos = mtod(m2, char *) + m2->m_len;
955 NFSEXITCODE2(error, nd);
963 nfsrvd_write(struct nfsrv_descript *nd, __unused int isdgram,
964 vnode_t vp, struct nfsexstuff *exp)
967 struct nfsvattr nva, forat;
968 int aftat_ret = 1, retlen, len, error = 0, forat_ret = 1;
969 int gotproxystateid, stable = NFSWRITE_FILESYNC;
971 struct nfsstate st, *stp = &st;
972 struct nfslock lo, *lop = &lo;
973 nfsv4stateid_t stateid;
975 nfsattrbit_t attrbits;
976 struct thread *p = curthread;
978 if (nd->nd_repstat) {
979 nfsrv_wcc(nd, forat_ret, &forat, aftat_ret, &nva);
983 if (nd->nd_flag & ND_NFSV2) {
984 NFSM_DISSECT(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
985 off = (off_t)fxdr_unsigned(u_int32_t, *++tl);
987 retlen = len = fxdr_unsigned(int32_t, *tl);
988 } else if (nd->nd_flag & ND_NFSV3) {
989 NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
990 off = fxdr_hyper(tl);
992 stable = fxdr_unsigned(int, *tl++);
993 retlen = len = fxdr_unsigned(int32_t, *tl);
995 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID + 4 * NFSX_UNSIGNED);
996 stp->ls_flags = (NFSLCK_CHECK | NFSLCK_WRITEACCESS);
997 lop->lo_flags = NFSLCK_WRITE;
998 stp->ls_ownerlen = 0;
1000 stp->ls_uid = nd->nd_cred->cr_uid;
1001 stp->ls_stateid.seqid = fxdr_unsigned(u_int32_t, *tl++);
1002 clientid.lval[0] = stp->ls_stateid.other[0] = *tl++;
1003 clientid.lval[1] = stp->ls_stateid.other[1] = *tl++;
1004 if ((nd->nd_flag & ND_IMPLIEDCLID) != 0) {
1005 if ((nd->nd_flag & ND_NFSV41) != 0)
1006 clientid.qval = nd->nd_clientid.qval;
1007 else if (nd->nd_clientid.qval != clientid.qval)
1008 printf("EEK2 multiple clids\n");
1010 if ((nd->nd_flag & ND_NFSV41) != 0)
1011 printf("EEK! no clientid from session\n");
1012 nd->nd_flag |= ND_IMPLIEDCLID;
1013 nd->nd_clientid.qval = clientid.qval;
1015 stp->ls_stateid.other[2] = *tl++;
1017 * Don't allow the client to use a special stateid for a DS op.
1019 if ((nd->nd_flag & ND_DSSERVER) != 0 &&
1020 ((stp->ls_stateid.other[0] == 0x0 &&
1021 stp->ls_stateid.other[1] == 0x0 &&
1022 stp->ls_stateid.other[2] == 0x0) ||
1023 (stp->ls_stateid.other[0] == 0xffffffff &&
1024 stp->ls_stateid.other[1] == 0xffffffff &&
1025 stp->ls_stateid.other[2] == 0xffffffff) ||
1026 stp->ls_stateid.seqid != 0))
1027 nd->nd_repstat = NFSERR_BADSTATEID;
1028 /* However, allow the proxy stateid. */
1029 if (stp->ls_stateid.seqid == 0xffffffff &&
1030 stp->ls_stateid.other[0] == 0x55555555 &&
1031 stp->ls_stateid.other[1] == 0x55555555 &&
1032 stp->ls_stateid.other[2] == 0x55555555)
1033 gotproxystateid = 1;
1034 off = fxdr_hyper(tl);
1035 lop->lo_first = off;
1037 stable = fxdr_unsigned(int, *tl++);
1038 retlen = len = fxdr_unsigned(int32_t, *tl);
1039 lop->lo_end = off + len;
1041 * Paranoia, just in case it wraps around, which shouldn't
1042 * ever happen anyhow.
1044 if (lop->lo_end < lop->lo_first)
1045 lop->lo_end = NFS64BITSSET;
1048 if (retlen > nfs_srvmaxio || retlen < 0)
1049 nd->nd_repstat = EIO;
1050 if (vp->v_type != VREG && !nd->nd_repstat) {
1051 if (nd->nd_flag & ND_NFSV3)
1052 nd->nd_repstat = EINVAL;
1054 nd->nd_repstat = (vp->v_type == VDIR) ? EISDIR :
1057 NFSZERO_ATTRBIT(&attrbits);
1058 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_OWNER);
1059 forat_ret = nfsvno_getattr(vp, &forat, nd, p, 1, &attrbits);
1060 if (!nd->nd_repstat)
1061 nd->nd_repstat = forat_ret;
1062 if (!nd->nd_repstat &&
1063 (forat.na_uid != nd->nd_cred->cr_uid ||
1064 NFSVNO_EXSTRICTACCESS(exp)))
1065 nd->nd_repstat = nfsvno_accchk(vp, VWRITE,
1066 nd->nd_cred, exp, p,
1067 NFSACCCHK_ALLOWOWNER, NFSACCCHK_VPISLOCKED, NULL);
1069 * DS reads are marked by ND_DSSERVER or use the proxy special
1072 if (nd->nd_repstat == 0 && (nd->nd_flag & (ND_NFSV4 | ND_DSSERVER)) ==
1073 ND_NFSV4 && gotproxystateid == 0)
1074 nd->nd_repstat = nfsrv_lockctrl(vp, &stp, &lop, NULL, clientid,
1075 &stateid, exp, nd, p);
1076 if (nd->nd_repstat) {
1078 if (nd->nd_flag & ND_NFSV3)
1079 nfsrv_wcc(nd, forat_ret, &forat, aftat_ret, &nva);
1084 * For NFS Version 2, it is not obvious what a write of zero length
1085 * should do, but I might as well be consistent with Version 3,
1086 * which is to return ok so long as there are no permission problems.
1089 nd->nd_repstat = nfsvno_write(vp, off, retlen, &stable,
1090 nd->nd_md, nd->nd_dpos, nd->nd_cred, p);
1091 error = nfsm_advance(nd, NFSM_RNDUP(retlen), -1);
1095 if (nd->nd_flag & ND_NFSV4)
1098 aftat_ret = nfsvno_getattr(vp, &nva, nd, p, 1, NULL);
1100 if (!nd->nd_repstat)
1101 nd->nd_repstat = aftat_ret;
1102 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) {
1103 if (nd->nd_flag & ND_NFSV3)
1104 nfsrv_wcc(nd, forat_ret, &forat, aftat_ret, &nva);
1107 NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1108 *tl++ = txdr_unsigned(retlen);
1110 * If nfs_async is set, then pretend the write was FILESYNC.
1111 * Warning: Doing this violates RFC1813 and runs a risk
1112 * of data written by a client being lost when the server
1115 if (stable == NFSWRITE_UNSTABLE && nfs_async == 0)
1116 *tl++ = txdr_unsigned(stable);
1118 *tl++ = txdr_unsigned(NFSWRITE_FILESYNC);
1120 * Actually, there is no need to txdr these fields,
1121 * but it may make the values more human readable,
1122 * for debugging purposes.
1124 *tl++ = txdr_unsigned(nfsboottime.tv_sec);
1125 *tl = txdr_unsigned(nfsboottime.tv_usec);
1126 } else if (!nd->nd_repstat)
1127 nfsrv_fillattr(nd, &nva);
1130 NFSEXITCODE2(0, nd);
1134 NFSEXITCODE2(error, nd);
1139 * nfs create service (creates regular files for V2 and V3. Spec. files for V2.)
1140 * now does a truncate to 0 length via. setattr if it already exists
1141 * The core creation routine has been extracted out into nfsrv_creatsub(),
1142 * so it can also be used by nfsrv_open() for V4.
1145 nfsrvd_create(struct nfsrv_descript *nd, __unused int isdgram,
1146 vnode_t dp, struct nfsexstuff *exp)
1148 struct nfsvattr nva, dirfor, diraft;
1149 struct nfsv2_sattr *sp;
1150 struct nameidata named;
1152 int error = 0, tsize, dirfor_ret = 1, diraft_ret = 1;
1153 int how = NFSCREATE_UNCHECKED, exclusive_flag = 0;
1155 vnode_t vp = NULL, dirp = NULL;
1160 int32_t cverf[2], tverf[2] = { 0, 0 };
1161 struct thread *p = curthread;
1163 if (nd->nd_repstat) {
1164 nfsrv_wcc(nd, dirfor_ret, &dirfor, diraft_ret, &diraft);
1167 NFSNAMEICNDSET(&named.ni_cnd, nd->nd_cred, CREATE,
1168 LOCKPARENT | LOCKLEAF | NOCACHE);
1169 nfsvno_setpathbuf(&named, &bufp, &hashp);
1170 error = nfsrv_parsename(nd, bufp, hashp, &named.ni_pathlen);
1173 if (!nd->nd_repstat) {
1174 NFSVNO_ATTRINIT(&nva);
1175 if (nd->nd_flag & ND_NFSV2) {
1176 NFSM_DISSECT(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1177 vtyp = IFTOVT(fxdr_unsigned(u_int32_t, sp->sa_mode));
1180 NFSVNO_SETATTRVAL(&nva, type, vtyp);
1181 NFSVNO_SETATTRVAL(&nva, mode,
1182 nfstov_mode(sp->sa_mode));
1183 switch (nva.na_type) {
1185 tsize = fxdr_unsigned(int32_t, sp->sa_size);
1187 NFSVNO_SETATTRVAL(&nva, size,
1193 rdev = fxdr_unsigned(NFSDEV_T, sp->sa_size);
1199 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
1200 how = fxdr_unsigned(int, *tl);
1202 case NFSCREATE_GUARDED:
1203 case NFSCREATE_UNCHECKED:
1204 error = nfsrv_sattr(nd, NULL, &nva, NULL, NULL, p);
1208 case NFSCREATE_EXCLUSIVE:
1209 NFSM_DISSECT(tl, u_int32_t *, NFSX_VERF);
1215 NFSVNO_SETATTRVAL(&nva, type, VREG);
1218 if (nd->nd_repstat) {
1219 nfsvno_relpathbuf(&named);
1220 if (nd->nd_flag & ND_NFSV3) {
1221 dirfor_ret = nfsvno_getattr(dp, &dirfor, nd, p, 1,
1223 nfsrv_wcc(nd, dirfor_ret, &dirfor, diraft_ret,
1230 nd->nd_repstat = nfsvno_namei(nd, &named, dp, 1, exp, &dirp);
1232 if (nd->nd_flag & ND_NFSV2) {
1236 dirfor_ret = nfsvno_getattr(dirp, &dirfor, nd, p, 0,
1240 if (nd->nd_repstat) {
1241 if (nd->nd_flag & ND_NFSV3)
1242 nfsrv_wcc(nd, dirfor_ret, &dirfor, diraft_ret,
1249 if (!(nd->nd_flag & ND_NFSV2)) {
1251 case NFSCREATE_GUARDED:
1253 nd->nd_repstat = EEXIST;
1255 case NFSCREATE_UNCHECKED:
1257 case NFSCREATE_EXCLUSIVE:
1258 if (named.ni_vp == NULL)
1259 NFSVNO_SETATTRVAL(&nva, mode, 0);
1265 * Iff doesn't exist, create it
1266 * otherwise just truncate to 0 length
1267 * should I set the mode too ?
1269 nd->nd_repstat = nfsvno_createsub(nd, &named, &vp, &nva,
1270 &exclusive_flag, cverf, rdev, exp);
1272 if (!nd->nd_repstat) {
1273 nd->nd_repstat = nfsvno_getfh(vp, &fh, p);
1274 if (!nd->nd_repstat)
1275 nd->nd_repstat = nfsvno_getattr(vp, &nva, nd, p, 1,
1278 if (!nd->nd_repstat) {
1279 tverf[0] = nva.na_atime.tv_sec;
1280 tverf[1] = nva.na_atime.tv_nsec;
1283 if (nd->nd_flag & ND_NFSV2) {
1284 if (!nd->nd_repstat) {
1285 (void) nfsm_fhtom(nd, (u_int8_t *)&fh, 0, 0);
1286 nfsrv_fillattr(nd, &nva);
1289 if (exclusive_flag && !nd->nd_repstat && (cverf[0] != tverf[0]
1290 || cverf[1] != tverf[1]))
1291 nd->nd_repstat = EEXIST;
1292 diraft_ret = nfsvno_getattr(dirp, &diraft, nd, p, 0, NULL);
1294 if (!nd->nd_repstat) {
1295 (void) nfsm_fhtom(nd, (u_int8_t *)&fh, 0, 1);
1296 nfsrv_postopattr(nd, 0, &nva);
1298 nfsrv_wcc(nd, dirfor_ret, &dirfor, diraft_ret, &diraft);
1302 NFSEXITCODE2(0, nd);
1306 nfsvno_relpathbuf(&named);
1307 NFSEXITCODE2(error, nd);
1312 * nfs v3 mknod service (and v4 create)
1315 nfsrvd_mknod(struct nfsrv_descript *nd, __unused int isdgram,
1316 vnode_t dp, vnode_t *vpp, fhandle_t *fhp, struct nfsexstuff *exp)
1318 struct nfsvattr nva, dirfor, diraft;
1320 struct nameidata named;
1321 int error = 0, dirfor_ret = 1, diraft_ret = 1, pathlen;
1322 u_int32_t major, minor;
1323 enum vtype vtyp = VNON;
1324 nfstype nfs4type = NFNON;
1325 vnode_t vp, dirp = NULL;
1326 nfsattrbit_t attrbits;
1327 char *bufp = NULL, *pathcp = NULL;
1328 u_long *hashp, cnflags;
1329 NFSACL_T *aclp = NULL;
1330 struct thread *p = curthread;
1332 NFSVNO_ATTRINIT(&nva);
1333 cnflags = LOCKPARENT;
1334 if (nd->nd_repstat) {
1335 nfsrv_wcc(nd, dirfor_ret, &dirfor, diraft_ret, &diraft);
1338 #ifdef NFS4_ACL_EXTATTR_NAME
1339 aclp = acl_alloc(M_WAITOK);
1344 * For V4, the creation stuff is here, Yuck!
1346 if (nd->nd_flag & ND_NFSV4) {
1347 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
1348 vtyp = nfsv34tov_type(*tl);
1349 nfs4type = fxdr_unsigned(nfstype, *tl);
1352 error = nfsvno_getsymlink(nd, &nva, p, &pathcp,
1359 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1360 major = fxdr_unsigned(u_int32_t, *tl++);
1361 minor = fxdr_unsigned(u_int32_t, *tl);
1362 nva.na_rdev = NFSMAKEDEV(major, minor);
1368 cnflags = LOCKPARENT;
1371 nd->nd_repstat = NFSERR_BADTYPE;
1373 #ifdef NFS4_ACL_EXTATTR_NAME
1379 NFSNAMEICNDSET(&named.ni_cnd, nd->nd_cred, CREATE, cnflags | NOCACHE);
1380 nfsvno_setpathbuf(&named, &bufp, &hashp);
1381 error = nfsrv_parsename(nd, bufp, hashp, &named.ni_pathlen);
1384 if (!nd->nd_repstat) {
1385 if (nd->nd_flag & ND_NFSV3) {
1386 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
1387 vtyp = nfsv34tov_type(*tl);
1389 error = nfsrv_sattr(nd, NULL, &nva, &attrbits, aclp, p);
1393 if (!nd->nd_repstat && (nd->nd_flag & ND_NFSV3) &&
1394 (vtyp == VCHR || vtyp == VBLK)) {
1395 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1396 major = fxdr_unsigned(u_int32_t, *tl++);
1397 minor = fxdr_unsigned(u_int32_t, *tl);
1398 nva.na_rdev = NFSMAKEDEV(major, minor);
1402 dirfor_ret = nfsvno_getattr(dp, &dirfor, nd, p, 0, NULL);
1403 if (!nd->nd_repstat && (nd->nd_flag & ND_NFSV4)) {
1404 if (!dirfor_ret && NFSVNO_ISSETGID(&nva) &&
1405 dirfor.na_gid == nva.na_gid)
1406 NFSVNO_UNSET(&nva, gid);
1407 nd->nd_repstat = nfsrv_checkuidgid(nd, &nva);
1409 if (nd->nd_repstat) {
1411 #ifdef NFS4_ACL_EXTATTR_NAME
1414 nfsvno_relpathbuf(&named);
1416 free(pathcp, M_TEMP);
1417 if (nd->nd_flag & ND_NFSV3)
1418 nfsrv_wcc(nd, dirfor_ret, &dirfor, diraft_ret,
1424 * Yuck! For V4, mkdir and link are here and some V4 clients don't fill
1425 * in va_mode, so we'll have to set a default here.
1427 if (NFSVNO_NOTSETMODE(&nva)) {
1435 named.ni_cnd.cn_flags |= WILLBEDIR;
1436 nd->nd_repstat = nfsvno_namei(nd, &named, dp, 0, exp, &dirp);
1437 if (nd->nd_repstat) {
1439 if (nd->nd_flag & ND_NFSV3)
1440 dirfor_ret = nfsvno_getattr(dirp, &dirfor, nd,
1444 #ifdef NFS4_ACL_EXTATTR_NAME
1447 if (nd->nd_flag & ND_NFSV3)
1448 nfsrv_wcc(nd, dirfor_ret, &dirfor, diraft_ret,
1453 dirfor_ret = nfsvno_getattr(dirp, &dirfor, nd, p, 0, NULL);
1455 if ((nd->nd_flag & ND_NFSV4) && (vtyp == VDIR || vtyp == VLNK)) {
1457 nfsrvd_mkdirsub(nd, &named, &nva, fhp, vpp, dirp,
1458 &dirfor, &diraft, &diraft_ret, &attrbits, aclp, p,
1460 #ifdef NFS4_ACL_EXTATTR_NAME
1464 } else if (vtyp == VLNK) {
1465 nfsrvd_symlinksub(nd, &named, &nva, fhp, vpp, dirp,
1466 &dirfor, &diraft, &diraft_ret, &attrbits,
1467 aclp, p, exp, pathcp, pathlen);
1468 #ifdef NFS4_ACL_EXTATTR_NAME
1471 free(pathcp, M_TEMP);
1476 nd->nd_repstat = nfsvno_mknod(&named, &nva, nd->nd_cred, p);
1477 if (!nd->nd_repstat) {
1479 nfsrv_fixattr(nd, vp, &nva, aclp, p, &attrbits, exp);
1480 nd->nd_repstat = nfsvno_getfh(vp, fhp, p);
1481 if ((nd->nd_flag & ND_NFSV3) && !nd->nd_repstat)
1482 nd->nd_repstat = nfsvno_getattr(vp, &nva, nd, p, 1,
1484 if (vpp != NULL && nd->nd_repstat == 0) {
1491 diraft_ret = nfsvno_getattr(dirp, &diraft, nd, p, 0, NULL);
1493 if (!nd->nd_repstat) {
1494 if (nd->nd_flag & ND_NFSV3) {
1495 (void) nfsm_fhtom(nd, (u_int8_t *)fhp, 0, 1);
1496 nfsrv_postopattr(nd, 0, &nva);
1498 NFSM_BUILD(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
1499 *tl++ = newnfs_false;
1500 txdr_hyper(dirfor.na_filerev, tl);
1502 txdr_hyper(diraft.na_filerev, tl);
1503 (void) nfsrv_putattrbit(nd, &attrbits);
1506 if (nd->nd_flag & ND_NFSV3)
1507 nfsrv_wcc(nd, dirfor_ret, &dirfor, diraft_ret, &diraft);
1508 #ifdef NFS4_ACL_EXTATTR_NAME
1513 NFSEXITCODE2(0, nd);
1517 #ifdef NFS4_ACL_EXTATTR_NAME
1521 nfsvno_relpathbuf(&named);
1523 free(pathcp, M_TEMP);
1525 NFSEXITCODE2(error, nd);
1530 * nfs remove service
1533 nfsrvd_remove(struct nfsrv_descript *nd, __unused int isdgram,
1534 vnode_t dp, struct nfsexstuff *exp)
1536 struct nameidata named;
1538 int error = 0, dirfor_ret = 1, diraft_ret = 1;
1539 vnode_t dirp = NULL;
1540 struct nfsvattr dirfor, diraft;
1543 struct thread *p = curthread;
1545 if (nd->nd_repstat) {
1546 nfsrv_wcc(nd, dirfor_ret, &dirfor, diraft_ret, &diraft);
1549 NFSNAMEICNDSET(&named.ni_cnd, nd->nd_cred, DELETE,
1550 LOCKPARENT | LOCKLEAF);
1551 nfsvno_setpathbuf(&named, &bufp, &hashp);
1552 error = nfsrv_parsename(nd, bufp, hashp, &named.ni_pathlen);
1555 nfsvno_relpathbuf(&named);
1558 if (!nd->nd_repstat) {
1559 nd->nd_repstat = nfsvno_namei(nd, &named, dp, 1, exp, &dirp);
1562 nfsvno_relpathbuf(&named);
1565 if (!(nd->nd_flag & ND_NFSV2)) {
1566 dirfor_ret = nfsvno_getattr(dirp, &dirfor, nd, p, 0,
1573 if (!nd->nd_repstat) {
1574 if (nd->nd_flag & ND_NFSV4) {
1575 if (named.ni_vp->v_type == VDIR)
1576 nd->nd_repstat = nfsvno_rmdirsub(&named, 1,
1577 nd->nd_cred, p, exp);
1579 nd->nd_repstat = nfsvno_removesub(&named, 1,
1580 nd->nd_cred, p, exp);
1581 } else if (nd->nd_procnum == NFSPROC_RMDIR) {
1582 nd->nd_repstat = nfsvno_rmdirsub(&named, 0,
1583 nd->nd_cred, p, exp);
1585 nd->nd_repstat = nfsvno_removesub(&named, 0,
1586 nd->nd_cred, p, exp);
1589 if (!(nd->nd_flag & ND_NFSV2)) {
1591 diraft_ret = nfsvno_getattr(dirp, &diraft, nd, p, 0,
1595 if (nd->nd_flag & ND_NFSV3) {
1596 nfsrv_wcc(nd, dirfor_ret, &dirfor, diraft_ret,
1598 } else if (!nd->nd_repstat) {
1599 NFSM_BUILD(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
1600 *tl++ = newnfs_false;
1601 txdr_hyper(dirfor.na_filerev, tl);
1603 txdr_hyper(diraft.na_filerev, tl);
1608 NFSEXITCODE2(error, nd);
1613 * nfs rename service
1616 nfsrvd_rename(struct nfsrv_descript *nd, int isdgram,
1617 vnode_t dp, vnode_t todp, struct nfsexstuff *exp, struct nfsexstuff *toexp)
1620 int error = 0, fdirfor_ret = 1, fdiraft_ret = 1;
1621 int tdirfor_ret = 1, tdiraft_ret = 1;
1622 struct nameidata fromnd, tond;
1623 vnode_t fdirp = NULL, tdirp = NULL, tdp = NULL;
1624 struct nfsvattr fdirfor, fdiraft, tdirfor, tdiraft;
1625 struct nfsexstuff tnes;
1627 char *bufp, *tbufp = NULL;
1630 struct thread *p = curthread;
1632 if (nd->nd_repstat) {
1633 nfsrv_wcc(nd, fdirfor_ret, &fdirfor, fdiraft_ret, &fdiraft);
1634 nfsrv_wcc(nd, tdirfor_ret, &tdirfor, tdiraft_ret, &tdiraft);
1637 if (!(nd->nd_flag & ND_NFSV2))
1638 fdirfor_ret = nfsvno_getattr(dp, &fdirfor, nd, p, 1, NULL);
1639 tond.ni_cnd.cn_nameiop = 0;
1640 tond.ni_startdir = NULL;
1641 NFSNAMEICNDSET(&fromnd.ni_cnd, nd->nd_cred, DELETE, WANTPARENT);
1642 nfsvno_setpathbuf(&fromnd, &bufp, &hashp);
1643 error = nfsrv_parsename(nd, bufp, hashp, &fromnd.ni_pathlen);
1648 nfsvno_relpathbuf(&fromnd);
1652 * Unlock dp in this code section, so it is unlocked before
1653 * tdp gets locked. This avoids a potential LOR if tdp is the
1654 * parent directory of dp.
1656 if (nd->nd_flag & ND_NFSV4) {
1661 /* Might lock tdp. */
1662 tdirfor_ret = nfsvno_getattr(tdp, &tdirfor, nd, p, 0,
1665 tdirfor_ret = nfsvno_getattr(tdp, &tdirfor, nd, p, 1,
1670 tfh.nfsrvfh_len = 0;
1671 error = nfsrv_mtofh(nd, &tfh);
1673 error = nfsvno_getfh(dp, &fh, p);
1676 /* todp is always NULL except NFSv4 */
1677 nfsvno_relpathbuf(&fromnd);
1681 /* If this is the same file handle, just VREF() the vnode. */
1682 if (tfh.nfsrvfh_len == NFSX_MYFH &&
1683 !NFSBCMP(tfh.nfsrvfh_data, &fh, NFSX_MYFH)) {
1687 tdirfor_ret = nfsvno_getattr(tdp, &tdirfor, nd, p, 1,
1692 nd->nd_cred->cr_uid = nd->nd_saveduid;
1693 nfsd_fhtovp(nd, &tfh, LK_EXCLUSIVE, &tdp, &tnes, NULL,
1694 0, -1); /* Locks tdp. */
1696 tdirfor_ret = nfsvno_getattr(tdp, &tdirfor, nd,
1702 NFSNAMEICNDSET(&tond.ni_cnd, nd->nd_cred, RENAME, LOCKPARENT | LOCKLEAF | NOCACHE);
1703 nfsvno_setpathbuf(&tond, &tbufp, &hashp);
1704 if (!nd->nd_repstat) {
1705 error = nfsrv_parsename(nd, tbufp, hashp, &tond.ni_pathlen);
1710 nfsvno_relpathbuf(&fromnd);
1711 nfsvno_relpathbuf(&tond);
1715 if (nd->nd_repstat) {
1716 if (nd->nd_flag & ND_NFSV3) {
1717 nfsrv_wcc(nd, fdirfor_ret, &fdirfor, fdiraft_ret,
1719 nfsrv_wcc(nd, tdirfor_ret, &tdirfor, tdiraft_ret,
1725 nfsvno_relpathbuf(&fromnd);
1726 nfsvno_relpathbuf(&tond);
1731 * Done parsing, now down to business.
1733 nd->nd_repstat = nfsvno_namei(nd, &fromnd, dp, 0, exp, &fdirp);
1734 if (nd->nd_repstat) {
1735 if (nd->nd_flag & ND_NFSV3) {
1736 nfsrv_wcc(nd, fdirfor_ret, &fdirfor, fdiraft_ret,
1738 nfsrv_wcc(nd, tdirfor_ret, &tdirfor, tdiraft_ret,
1745 nfsvno_relpathbuf(&tond);
1748 if (fromnd.ni_vp->v_type == VDIR)
1749 tond.ni_cnd.cn_flags |= WILLBEDIR;
1750 nd->nd_repstat = nfsvno_namei(nd, &tond, tdp, 0, &tnes, &tdirp);
1751 nd->nd_repstat = nfsvno_rename(&fromnd, &tond, nd->nd_repstat,
1752 nd->nd_flag, nd->nd_cred, p);
1754 fdiraft_ret = nfsvno_getattr(fdirp, &fdiraft, nd, p, 0, NULL);
1756 tdiraft_ret = nfsvno_getattr(tdirp, &tdiraft, nd, p, 0, NULL);
1761 if (nd->nd_flag & ND_NFSV3) {
1762 nfsrv_wcc(nd, fdirfor_ret, &fdirfor, fdiraft_ret, &fdiraft);
1763 nfsrv_wcc(nd, tdirfor_ret, &tdirfor, tdiraft_ret, &tdiraft);
1764 } else if ((nd->nd_flag & ND_NFSV4) && !nd->nd_repstat) {
1765 NFSM_BUILD(tl, u_int32_t *, 10 * NFSX_UNSIGNED);
1766 *tl++ = newnfs_false;
1767 txdr_hyper(fdirfor.na_filerev, tl);
1769 txdr_hyper(fdiraft.na_filerev, tl);
1771 *tl++ = newnfs_false;
1772 txdr_hyper(tdirfor.na_filerev, tl);
1774 txdr_hyper(tdiraft.na_filerev, tl);
1778 NFSEXITCODE2(error, nd);
1786 nfsrvd_link(struct nfsrv_descript *nd, int isdgram,
1787 vnode_t vp, vnode_t tovp, struct nfsexstuff *exp, struct nfsexstuff *toexp)
1789 struct nameidata named;
1791 int error = 0, dirfor_ret = 1, diraft_ret = 1, getret = 1;
1792 vnode_t dirp = NULL, dp = NULL;
1793 struct nfsvattr dirfor, diraft, at;
1794 struct nfsexstuff tnes;
1798 struct thread *p = curthread;
1800 if (nd->nd_repstat) {
1801 nfsrv_postopattr(nd, getret, &at);
1802 nfsrv_wcc(nd, dirfor_ret, &dirfor, diraft_ret, &diraft);
1806 if (vp->v_type == VDIR) {
1807 if (nd->nd_flag & ND_NFSV4)
1808 nd->nd_repstat = NFSERR_ISDIR;
1810 nd->nd_repstat = NFSERR_INVAL;
1814 if (!nd->nd_repstat) {
1815 if (nd->nd_flag & ND_NFSV4) {
1819 error = nfsrv_mtofh(nd, &dfh);
1822 /* tovp is always NULL unless NFSv4 */
1825 nfsd_fhtovp(nd, &dfh, LK_EXCLUSIVE, &dp, &tnes, NULL,
1831 NFSNAMEICNDSET(&named.ni_cnd, nd->nd_cred, CREATE, LOCKPARENT | NOCACHE);
1832 if (!nd->nd_repstat) {
1833 nfsvno_setpathbuf(&named, &bufp, &hashp);
1834 error = nfsrv_parsename(nd, bufp, hashp, &named.ni_pathlen);
1839 nfsvno_relpathbuf(&named);
1842 if (!nd->nd_repstat) {
1843 nd->nd_repstat = nfsvno_namei(nd, &named, dp, 0, &tnes,
1848 nfsvno_relpathbuf(&named);
1852 if (nd->nd_flag & ND_NFSV2) {
1856 dirfor_ret = nfsvno_getattr(dirp, &dirfor, nd, p, 0,
1860 if (!nd->nd_repstat)
1861 nd->nd_repstat = nfsvno_link(&named, vp, nd->nd_cred, p, exp);
1862 if (nd->nd_flag & ND_NFSV3)
1863 getret = nfsvno_getattr(vp, &at, nd, p, 0, NULL);
1865 diraft_ret = nfsvno_getattr(dirp, &diraft, nd, p, 0, NULL);
1869 if (nd->nd_flag & ND_NFSV3) {
1870 nfsrv_postopattr(nd, getret, &at);
1871 nfsrv_wcc(nd, dirfor_ret, &dirfor, diraft_ret, &diraft);
1872 } else if ((nd->nd_flag & ND_NFSV4) && !nd->nd_repstat) {
1873 NFSM_BUILD(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
1874 *tl++ = newnfs_false;
1875 txdr_hyper(dirfor.na_filerev, tl);
1877 txdr_hyper(diraft.na_filerev, tl);
1881 NFSEXITCODE2(error, nd);
1886 * nfs symbolic link service
1889 nfsrvd_symlink(struct nfsrv_descript *nd, __unused int isdgram,
1890 vnode_t dp, vnode_t *vpp, fhandle_t *fhp, struct nfsexstuff *exp)
1892 struct nfsvattr nva, dirfor, diraft;
1893 struct nameidata named;
1894 int error = 0, dirfor_ret = 1, diraft_ret = 1, pathlen;
1895 vnode_t dirp = NULL;
1896 char *bufp, *pathcp = NULL;
1898 struct thread *p = curthread;
1900 if (nd->nd_repstat) {
1901 nfsrv_wcc(nd, dirfor_ret, &dirfor, diraft_ret, &diraft);
1906 NFSVNO_ATTRINIT(&nva);
1907 NFSNAMEICNDSET(&named.ni_cnd, nd->nd_cred, CREATE,
1908 LOCKPARENT | NOCACHE);
1909 nfsvno_setpathbuf(&named, &bufp, &hashp);
1910 error = nfsrv_parsename(nd, bufp, hashp, &named.ni_pathlen);
1911 if (!error && !nd->nd_repstat)
1912 error = nfsvno_getsymlink(nd, &nva, p, &pathcp, &pathlen);
1915 nfsvno_relpathbuf(&named);
1918 if (!nd->nd_repstat) {
1919 nd->nd_repstat = nfsvno_namei(nd, &named, dp, 0, exp, &dirp);
1922 nfsvno_relpathbuf(&named);
1924 if (dirp != NULL && !(nd->nd_flag & ND_NFSV3)) {
1930 * And call nfsrvd_symlinksub() to do the common code. It will
1931 * return EBADRPC upon a parsing error, 0 otherwise.
1933 if (!nd->nd_repstat) {
1935 dirfor_ret = nfsvno_getattr(dirp, &dirfor, nd, p, 0,
1937 nfsrvd_symlinksub(nd, &named, &nva, fhp, vpp, dirp,
1938 &dirfor, &diraft, &diraft_ret, NULL, NULL, p, exp,
1940 } else if (dirp != NULL) {
1941 dirfor_ret = nfsvno_getattr(dirp, &dirfor, nd, p, 0, NULL);
1945 free(pathcp, M_TEMP);
1947 if (nd->nd_flag & ND_NFSV3) {
1948 if (!nd->nd_repstat) {
1949 (void) nfsm_fhtom(nd, (u_int8_t *)fhp, 0, 1);
1950 nfsrv_postopattr(nd, 0, &nva);
1952 nfsrv_wcc(nd, dirfor_ret, &dirfor, diraft_ret, &diraft);
1956 NFSEXITCODE2(error, nd);
1961 * Common code for creating a symbolic link.
1964 nfsrvd_symlinksub(struct nfsrv_descript *nd, struct nameidata *ndp,
1965 struct nfsvattr *nvap, fhandle_t *fhp, vnode_t *vpp,
1966 vnode_t dirp, struct nfsvattr *dirforp, struct nfsvattr *diraftp,
1967 int *diraft_retp, nfsattrbit_t *attrbitp,
1968 NFSACL_T *aclp, NFSPROC_T *p, struct nfsexstuff *exp, char *pathcp,
1973 nd->nd_repstat = nfsvno_symlink(ndp, nvap, pathcp, pathlen,
1974 !(nd->nd_flag & ND_NFSV2), nd->nd_saveduid, nd->nd_cred, p, exp);
1975 if (!nd->nd_repstat && !(nd->nd_flag & ND_NFSV2)) {
1976 nfsrv_fixattr(nd, ndp->ni_vp, nvap, aclp, p, attrbitp, exp);
1977 if (nd->nd_flag & ND_NFSV3) {
1978 nd->nd_repstat = nfsvno_getfh(ndp->ni_vp, fhp, p);
1979 if (!nd->nd_repstat)
1980 nd->nd_repstat = nfsvno_getattr(ndp->ni_vp,
1981 nvap, nd, p, 1, NULL);
1983 if (vpp != NULL && nd->nd_repstat == 0) {
1984 NFSVOPUNLOCK(ndp->ni_vp);
1990 *diraft_retp = nfsvno_getattr(dirp, diraftp, nd, p, 0, NULL);
1993 if ((nd->nd_flag & ND_NFSV4) && !nd->nd_repstat) {
1994 NFSM_BUILD(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
1995 *tl++ = newnfs_false;
1996 txdr_hyper(dirforp->na_filerev, tl);
1998 txdr_hyper(diraftp->na_filerev, tl);
1999 (void) nfsrv_putattrbit(nd, attrbitp);
2002 NFSEXITCODE2(0, nd);
2009 nfsrvd_mkdir(struct nfsrv_descript *nd, __unused int isdgram,
2010 vnode_t dp, vnode_t *vpp, fhandle_t *fhp, struct nfsexstuff *exp)
2012 struct nfsvattr nva, dirfor, diraft;
2013 struct nameidata named;
2015 int error = 0, dirfor_ret = 1, diraft_ret = 1;
2016 vnode_t dirp = NULL;
2019 struct thread *p = curthread;
2021 if (nd->nd_repstat) {
2022 nfsrv_wcc(nd, dirfor_ret, &dirfor, diraft_ret, &diraft);
2025 NFSNAMEICNDSET(&named.ni_cnd, nd->nd_cred, CREATE, LOCKPARENT | NOCACHE);
2026 nfsvno_setpathbuf(&named, &bufp, &hashp);
2027 error = nfsrv_parsename(nd, bufp, hashp, &named.ni_pathlen);
2030 if (!nd->nd_repstat) {
2031 NFSVNO_ATTRINIT(&nva);
2032 if (nd->nd_flag & ND_NFSV3) {
2033 error = nfsrv_sattr(nd, NULL, &nva, NULL, NULL, p);
2037 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2038 nva.na_mode = nfstov_mode(*tl++);
2041 if (!nd->nd_repstat) {
2042 nd->nd_repstat = nfsvno_namei(nd, &named, dp, 0, exp, &dirp);
2045 nfsvno_relpathbuf(&named);
2047 if (dirp != NULL && !(nd->nd_flag & ND_NFSV3)) {
2051 if (nd->nd_repstat) {
2053 dirfor_ret = nfsvno_getattr(dirp, &dirfor, nd, p, 0,
2057 if (nd->nd_flag & ND_NFSV3)
2058 nfsrv_wcc(nd, dirfor_ret, &dirfor, diraft_ret,
2063 dirfor_ret = nfsvno_getattr(dirp, &dirfor, nd, p, 0, NULL);
2066 * Call nfsrvd_mkdirsub() for the code common to V4 as well.
2068 nfsrvd_mkdirsub(nd, &named, &nva, fhp, vpp, dirp, &dirfor, &diraft,
2069 &diraft_ret, NULL, NULL, p, exp);
2071 if (nd->nd_flag & ND_NFSV3) {
2072 if (!nd->nd_repstat) {
2073 (void) nfsm_fhtom(nd, (u_int8_t *)fhp, 0, 1);
2074 nfsrv_postopattr(nd, 0, &nva);
2076 nfsrv_wcc(nd, dirfor_ret, &dirfor, diraft_ret, &diraft);
2077 } else if (!nd->nd_repstat) {
2078 (void) nfsm_fhtom(nd, (u_int8_t *)fhp, 0, 0);
2079 nfsrv_fillattr(nd, &nva);
2083 NFSEXITCODE2(0, nd);
2087 nfsvno_relpathbuf(&named);
2088 NFSEXITCODE2(error, nd);
2093 * Code common to mkdir for V2,3 and 4.
2096 nfsrvd_mkdirsub(struct nfsrv_descript *nd, struct nameidata *ndp,
2097 struct nfsvattr *nvap, fhandle_t *fhp, vnode_t *vpp,
2098 vnode_t dirp, struct nfsvattr *dirforp, struct nfsvattr *diraftp,
2099 int *diraft_retp, nfsattrbit_t *attrbitp, NFSACL_T *aclp,
2100 NFSPROC_T *p, struct nfsexstuff *exp)
2105 NFSVNO_SETATTRVAL(nvap, type, VDIR);
2106 nd->nd_repstat = nfsvno_mkdir(ndp, nvap, nd->nd_saveduid,
2107 nd->nd_cred, p, exp);
2108 if (!nd->nd_repstat) {
2110 nfsrv_fixattr(nd, vp, nvap, aclp, p, attrbitp, exp);
2111 nd->nd_repstat = nfsvno_getfh(vp, fhp, p);
2112 if (!(nd->nd_flag & ND_NFSV4) && !nd->nd_repstat)
2113 nd->nd_repstat = nfsvno_getattr(vp, nvap, nd, p, 1,
2115 if (vpp && !nd->nd_repstat) {
2123 *diraft_retp = nfsvno_getattr(dirp, diraftp, nd, p, 0, NULL);
2126 if ((nd->nd_flag & ND_NFSV4) && !nd->nd_repstat) {
2127 NFSM_BUILD(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
2128 *tl++ = newnfs_false;
2129 txdr_hyper(dirforp->na_filerev, tl);
2131 txdr_hyper(diraftp->na_filerev, tl);
2132 (void) nfsrv_putattrbit(nd, attrbitp);
2135 NFSEXITCODE2(0, nd);
2139 * nfs commit service
2142 nfsrvd_commit(struct nfsrv_descript *nd, __unused int isdgram,
2143 vnode_t vp, __unused struct nfsexstuff *exp)
2145 struct nfsvattr bfor, aft;
2147 int error = 0, for_ret = 1, aft_ret = 1, cnt;
2149 struct thread *p = curthread;
2151 if (nd->nd_repstat) {
2152 nfsrv_wcc(nd, for_ret, &bfor, aft_ret, &aft);
2156 /* Return NFSERR_ISDIR in NFSv4 when commit on a directory. */
2157 if (vp->v_type != VREG) {
2158 if (nd->nd_flag & ND_NFSV3)
2159 error = NFSERR_NOTSUPP;
2161 error = (vp->v_type == VDIR) ? NFSERR_ISDIR : NFSERR_INVAL;
2164 NFSM_DISSECT(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2167 * XXX At this time VOP_FSYNC() does not accept offset and byte
2168 * count parameters, so these arguments are useless (someday maybe).
2170 off = fxdr_hyper(tl);
2172 cnt = fxdr_unsigned(int, *tl);
2173 if (nd->nd_flag & ND_NFSV3)
2174 for_ret = nfsvno_getattr(vp, &bfor, nd, p, 1, NULL);
2175 nd->nd_repstat = nfsvno_fsync(vp, off, cnt, nd->nd_cred, p);
2176 if (nd->nd_flag & ND_NFSV3) {
2177 aft_ret = nfsvno_getattr(vp, &aft, nd, p, 1, NULL);
2178 nfsrv_wcc(nd, for_ret, &bfor, aft_ret, &aft);
2181 if (!nd->nd_repstat) {
2182 NFSM_BUILD(tl, u_int32_t *, NFSX_VERF);
2183 *tl++ = txdr_unsigned(nfsboottime.tv_sec);
2184 *tl = txdr_unsigned(nfsboottime.tv_usec);
2188 NFSEXITCODE2(0, nd);
2192 NFSEXITCODE2(error, nd);
2197 * nfs statfs service
2200 nfsrvd_statfs(struct nfsrv_descript *nd, __unused int isdgram,
2201 vnode_t vp, __unused struct nfsexstuff *exp)
2208 struct thread *p = curthread;
2211 if (nd->nd_repstat) {
2212 nfsrv_postopattr(nd, getret, &at);
2215 sf = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
2216 nd->nd_repstat = nfsvno_statfs(vp, sf);
2217 getret = nfsvno_getattr(vp, &at, nd, p, 1, NULL);
2219 if (nd->nd_flag & ND_NFSV3)
2220 nfsrv_postopattr(nd, getret, &at);
2223 if (nd->nd_flag & ND_NFSV2) {
2224 NFSM_BUILD(tl, u_int32_t *, NFSX_V2STATFS);
2225 *tl++ = txdr_unsigned(NFS_V2MAXDATA);
2226 *tl++ = txdr_unsigned(sf->f_bsize);
2227 *tl++ = txdr_unsigned(sf->f_blocks);
2228 *tl++ = txdr_unsigned(sf->f_bfree);
2229 *tl = txdr_unsigned(sf->f_bavail);
2231 NFSM_BUILD(tl, u_int32_t *, NFSX_V3STATFS);
2232 tval = (u_quad_t)sf->f_blocks;
2233 tval *= (u_quad_t)sf->f_bsize;
2234 txdr_hyper(tval, tl); tl += 2;
2235 tval = (u_quad_t)sf->f_bfree;
2236 tval *= (u_quad_t)sf->f_bsize;
2237 txdr_hyper(tval, tl); tl += 2;
2238 tval = (u_quad_t)sf->f_bavail;
2239 tval *= (u_quad_t)sf->f_bsize;
2240 txdr_hyper(tval, tl); tl += 2;
2241 tval = (u_quad_t)sf->f_files;
2242 txdr_hyper(tval, tl); tl += 2;
2243 tval = (u_quad_t)sf->f_ffree;
2244 txdr_hyper(tval, tl); tl += 2;
2245 tval = (u_quad_t)sf->f_ffree;
2246 txdr_hyper(tval, tl); tl += 2;
2252 NFSEXITCODE2(0, nd);
2257 * nfs fsinfo service
2260 nfsrvd_fsinfo(struct nfsrv_descript *nd, int isdgram,
2261 vnode_t vp, __unused struct nfsexstuff *exp)
2264 struct nfsfsinfo fs;
2267 struct thread *p = curthread;
2269 if (nd->nd_repstat) {
2270 nfsrv_postopattr(nd, getret, &at);
2273 getret = nfsvno_getattr(vp, &at, nd, p, 1, NULL);
2274 nfsvno_getfs(&fs, isdgram);
2276 nfsrv_postopattr(nd, getret, &at);
2277 NFSM_BUILD(tl, u_int32_t *, NFSX_V3FSINFO);
2278 *tl++ = txdr_unsigned(fs.fs_rtmax);
2279 *tl++ = txdr_unsigned(fs.fs_rtpref);
2280 *tl++ = txdr_unsigned(fs.fs_rtmult);
2281 *tl++ = txdr_unsigned(fs.fs_wtmax);
2282 *tl++ = txdr_unsigned(fs.fs_wtpref);
2283 *tl++ = txdr_unsigned(fs.fs_wtmult);
2284 *tl++ = txdr_unsigned(fs.fs_dtpref);
2285 txdr_hyper(fs.fs_maxfilesize, tl);
2287 txdr_nfsv3time(&fs.fs_timedelta, tl);
2289 *tl = txdr_unsigned(fs.fs_properties);
2292 NFSEXITCODE2(0, nd);
2297 * nfs pathconf service
2300 nfsrvd_pathconf(struct nfsrv_descript *nd, __unused int isdgram,
2301 vnode_t vp, __unused struct nfsexstuff *exp)
2303 struct nfsv3_pathconf *pc;
2305 long linkmax, namemax, chownres, notrunc;
2307 struct thread *p = curthread;
2309 if (nd->nd_repstat) {
2310 nfsrv_postopattr(nd, getret, &at);
2313 nd->nd_repstat = nfsvno_pathconf(vp, _PC_LINK_MAX, &linkmax,
2315 if (!nd->nd_repstat)
2316 nd->nd_repstat = nfsvno_pathconf(vp, _PC_NAME_MAX, &namemax,
2318 if (!nd->nd_repstat)
2319 nd->nd_repstat=nfsvno_pathconf(vp, _PC_CHOWN_RESTRICTED,
2320 &chownres, nd->nd_cred, p);
2321 if (!nd->nd_repstat)
2322 nd->nd_repstat = nfsvno_pathconf(vp, _PC_NO_TRUNC, ¬runc,
2324 getret = nfsvno_getattr(vp, &at, nd, p, 1, NULL);
2326 nfsrv_postopattr(nd, getret, &at);
2327 if (!nd->nd_repstat) {
2328 NFSM_BUILD(pc, struct nfsv3_pathconf *, NFSX_V3PATHCONF);
2329 pc->pc_linkmax = txdr_unsigned(linkmax);
2330 pc->pc_namemax = txdr_unsigned(namemax);
2331 pc->pc_notrunc = txdr_unsigned(notrunc);
2332 pc->pc_chownrestricted = txdr_unsigned(chownres);
2335 * These should probably be supported by VOP_PATHCONF(), but
2336 * until msdosfs is exportable (why would you want to?), the
2337 * Unix defaults should be ok.
2339 pc->pc_caseinsensitive = newnfs_false;
2340 pc->pc_casepreserving = newnfs_true;
2344 NFSEXITCODE2(0, nd);
2349 * nfsv4 lock service
2352 nfsrvd_lock(struct nfsrv_descript *nd, __unused int isdgram,
2353 vnode_t vp, struct nfsexstuff *exp)
2357 struct nfsstate *stp = NULL;
2358 struct nfslock *lop;
2359 struct nfslockconflict cf;
2361 u_short flags = NFSLCK_LOCK, lflags;
2362 u_int64_t offset, len;
2363 nfsv4stateid_t stateid;
2365 struct thread *p = curthread;
2367 NFSM_DISSECT(tl, u_int32_t *, 7 * NFSX_UNSIGNED);
2368 i = fxdr_unsigned(int, *tl++);
2370 case NFSV4LOCKT_READW:
2371 flags |= NFSLCK_BLOCKING;
2372 case NFSV4LOCKT_READ:
2373 lflags = NFSLCK_READ;
2375 case NFSV4LOCKT_WRITEW:
2376 flags |= NFSLCK_BLOCKING;
2377 case NFSV4LOCKT_WRITE:
2378 lflags = NFSLCK_WRITE;
2381 nd->nd_repstat = NFSERR_BADXDR;
2384 if (*tl++ == newnfs_true)
2385 flags |= NFSLCK_RECLAIM;
2386 offset = fxdr_hyper(tl);
2388 len = fxdr_hyper(tl);
2390 if (*tl == newnfs_true)
2391 flags |= NFSLCK_OPENTOLOCK;
2392 if (flags & NFSLCK_OPENTOLOCK) {
2393 NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED + NFSX_STATEID);
2394 i = fxdr_unsigned(int, *(tl+4+(NFSX_STATEID / NFSX_UNSIGNED)));
2395 if (i <= 0 || i > NFSV4_OPAQUELIMIT) {
2396 nd->nd_repstat = NFSERR_BADXDR;
2399 stp = malloc(sizeof (struct nfsstate) + i,
2400 M_NFSDSTATE, M_WAITOK);
2401 stp->ls_ownerlen = i;
2402 stp->ls_op = nd->nd_rp;
2403 stp->ls_seq = fxdr_unsigned(int, *tl++);
2404 stp->ls_stateid.seqid = fxdr_unsigned(u_int32_t, *tl++);
2405 NFSBCOPY((caddr_t)tl, (caddr_t)stp->ls_stateid.other,
2407 tl += (NFSX_STATEIDOTHER / NFSX_UNSIGNED);
2410 * For the special stateid of other all 0s and seqid == 1, set
2411 * the stateid to the current stateid, if it is set.
2413 if ((nd->nd_flag & ND_NFSV41) != 0 &&
2414 stp->ls_stateid.seqid == 1 &&
2415 stp->ls_stateid.other[0] == 0 &&
2416 stp->ls_stateid.other[1] == 0 &&
2417 stp->ls_stateid.other[2] == 0) {
2418 if ((nd->nd_flag & ND_CURSTATEID) != 0) {
2419 stp->ls_stateid = nd->nd_curstateid;
2420 stp->ls_stateid.seqid = 0;
2422 nd->nd_repstat = NFSERR_BADSTATEID;
2427 stp->ls_opentolockseq = fxdr_unsigned(int, *tl++);
2428 clientid.lval[0] = *tl++;
2429 clientid.lval[1] = *tl++;
2430 if ((nd->nd_flag & ND_IMPLIEDCLID) != 0) {
2431 if ((nd->nd_flag & ND_NFSV41) != 0)
2432 clientid.qval = nd->nd_clientid.qval;
2433 else if (nd->nd_clientid.qval != clientid.qval)
2434 printf("EEK3 multiple clids\n");
2436 if ((nd->nd_flag & ND_NFSV41) != 0)
2437 printf("EEK! no clientid from session\n");
2438 nd->nd_flag |= ND_IMPLIEDCLID;
2439 nd->nd_clientid.qval = clientid.qval;
2441 error = nfsrv_mtostr(nd, stp->ls_owner, stp->ls_ownerlen);
2445 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID + NFSX_UNSIGNED);
2446 stp = malloc(sizeof (struct nfsstate),
2447 M_NFSDSTATE, M_WAITOK);
2448 stp->ls_ownerlen = 0;
2449 stp->ls_op = nd->nd_rp;
2450 stp->ls_stateid.seqid = fxdr_unsigned(u_int32_t, *tl++);
2451 NFSBCOPY((caddr_t)tl, (caddr_t)stp->ls_stateid.other,
2453 tl += (NFSX_STATEIDOTHER / NFSX_UNSIGNED);
2456 * For the special stateid of other all 0s and seqid == 1, set
2457 * the stateid to the current stateid, if it is set.
2459 if ((nd->nd_flag & ND_NFSV41) != 0 &&
2460 stp->ls_stateid.seqid == 1 &&
2461 stp->ls_stateid.other[0] == 0 &&
2462 stp->ls_stateid.other[1] == 0 &&
2463 stp->ls_stateid.other[2] == 0) {
2464 if ((nd->nd_flag & ND_CURSTATEID) != 0) {
2465 stp->ls_stateid = nd->nd_curstateid;
2466 stp->ls_stateid.seqid = 0;
2468 nd->nd_repstat = NFSERR_BADSTATEID;
2473 stp->ls_seq = fxdr_unsigned(int, *tl);
2474 clientid.lval[0] = stp->ls_stateid.other[0];
2475 clientid.lval[1] = stp->ls_stateid.other[1];
2476 if ((nd->nd_flag & ND_IMPLIEDCLID) != 0) {
2477 if ((nd->nd_flag & ND_NFSV41) != 0)
2478 clientid.qval = nd->nd_clientid.qval;
2479 else if (nd->nd_clientid.qval != clientid.qval)
2480 printf("EEK4 multiple clids\n");
2482 if ((nd->nd_flag & ND_NFSV41) != 0)
2483 printf("EEK! no clientid from session\n");
2484 nd->nd_flag |= ND_IMPLIEDCLID;
2485 nd->nd_clientid.qval = clientid.qval;
2488 lop = malloc(sizeof (struct nfslock),
2489 M_NFSDLOCK, M_WAITOK);
2490 lop->lo_first = offset;
2491 if (len == NFS64BITSSET) {
2492 lop->lo_end = NFS64BITSSET;
2494 lop->lo_end = offset + len;
2495 if (lop->lo_end <= lop->lo_first)
2496 nd->nd_repstat = NFSERR_INVAL;
2498 lop->lo_flags = lflags;
2499 stp->ls_flags = flags;
2500 stp->ls_uid = nd->nd_cred->cr_uid;
2503 * Do basic access checking.
2505 if (!nd->nd_repstat && vp->v_type != VREG) {
2506 if (vp->v_type == VDIR)
2507 nd->nd_repstat = NFSERR_ISDIR;
2509 nd->nd_repstat = NFSERR_INVAL;
2511 if (!nd->nd_repstat) {
2512 if (lflags & NFSLCK_WRITE) {
2513 nd->nd_repstat = nfsvno_accchk(vp, VWRITE,
2514 nd->nd_cred, exp, p, NFSACCCHK_ALLOWOWNER,
2515 NFSACCCHK_VPISLOCKED, NULL);
2517 nd->nd_repstat = nfsvno_accchk(vp, VREAD,
2518 nd->nd_cred, exp, p, NFSACCCHK_ALLOWOWNER,
2519 NFSACCCHK_VPISLOCKED, NULL);
2521 nd->nd_repstat = nfsvno_accchk(vp, VEXEC,
2522 nd->nd_cred, exp, p, NFSACCCHK_ALLOWOWNER,
2523 NFSACCCHK_VPISLOCKED, NULL);
2528 * We call nfsrv_lockctrl() even if nd_repstat set, so that the
2529 * seqid# gets updated. nfsrv_lockctrl() will return the value
2530 * of nd_repstat, if it gets that far.
2532 nd->nd_repstat = nfsrv_lockctrl(vp, &stp, &lop, &cf, clientid,
2533 &stateid, exp, nd, p);
2535 free(lop, M_NFSDLOCK);
2537 free(stp, M_NFSDSTATE);
2538 if (!nd->nd_repstat) {
2539 /* For NFSv4.1, set the Current StateID. */
2540 if ((nd->nd_flag & ND_NFSV41) != 0) {
2541 nd->nd_curstateid = stateid;
2542 nd->nd_flag |= ND_CURSTATEID;
2544 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID);
2545 *tl++ = txdr_unsigned(stateid.seqid);
2546 NFSBCOPY((caddr_t)stateid.other,(caddr_t)tl,NFSX_STATEIDOTHER);
2547 } else if (nd->nd_repstat == NFSERR_DENIED) {
2548 NFSM_BUILD(tl, u_int32_t *, 7 * NFSX_UNSIGNED);
2549 txdr_hyper(cf.cl_first, tl);
2551 if (cf.cl_end == NFS64BITSSET)
2554 len = cf.cl_end - cf.cl_first;
2555 txdr_hyper(len, tl);
2557 if (cf.cl_flags == NFSLCK_WRITE)
2558 *tl++ = txdr_unsigned(NFSV4LOCKT_WRITE);
2560 *tl++ = txdr_unsigned(NFSV4LOCKT_READ);
2561 *tl++ = stateid.other[0];
2562 *tl = stateid.other[1];
2563 (void) nfsm_strtom(nd, cf.cl_owner, cf.cl_ownerlen);
2566 NFSEXITCODE2(0, nd);
2571 free(stp, M_NFSDSTATE);
2572 NFSEXITCODE2(error, nd);
2577 * nfsv4 lock test service
2580 nfsrvd_lockt(struct nfsrv_descript *nd, __unused int isdgram,
2581 vnode_t vp, struct nfsexstuff *exp)
2585 struct nfsstate *stp = NULL;
2586 struct nfslock lo, *lop = &lo;
2587 struct nfslockconflict cf;
2589 nfsv4stateid_t stateid;
2592 struct thread *p = curthread;
2594 NFSM_DISSECT(tl, u_int32_t *, 8 * NFSX_UNSIGNED);
2595 i = fxdr_unsigned(int, *(tl + 7));
2596 if (i <= 0 || i > NFSV4_OPAQUELIMIT) {
2597 nd->nd_repstat = NFSERR_BADXDR;
2600 stp = malloc(sizeof (struct nfsstate) + i,
2601 M_NFSDSTATE, M_WAITOK);
2602 stp->ls_ownerlen = i;
2604 stp->ls_flags = NFSLCK_TEST;
2605 stp->ls_uid = nd->nd_cred->cr_uid;
2606 i = fxdr_unsigned(int, *tl++);
2608 case NFSV4LOCKT_READW:
2609 stp->ls_flags |= NFSLCK_BLOCKING;
2610 case NFSV4LOCKT_READ:
2611 lo.lo_flags = NFSLCK_READ;
2613 case NFSV4LOCKT_WRITEW:
2614 stp->ls_flags |= NFSLCK_BLOCKING;
2615 case NFSV4LOCKT_WRITE:
2616 lo.lo_flags = NFSLCK_WRITE;
2619 nd->nd_repstat = NFSERR_BADXDR;
2622 lo.lo_first = fxdr_hyper(tl);
2624 len = fxdr_hyper(tl);
2625 if (len == NFS64BITSSET) {
2626 lo.lo_end = NFS64BITSSET;
2628 lo.lo_end = lo.lo_first + len;
2629 if (lo.lo_end <= lo.lo_first)
2630 nd->nd_repstat = NFSERR_INVAL;
2633 clientid.lval[0] = *tl++;
2634 clientid.lval[1] = *tl;
2635 if ((nd->nd_flag & ND_IMPLIEDCLID) != 0) {
2636 if ((nd->nd_flag & ND_NFSV41) != 0)
2637 clientid.qval = nd->nd_clientid.qval;
2638 else if (nd->nd_clientid.qval != clientid.qval)
2639 printf("EEK5 multiple clids\n");
2641 if ((nd->nd_flag & ND_NFSV41) != 0)
2642 printf("EEK! no clientid from session\n");
2643 nd->nd_flag |= ND_IMPLIEDCLID;
2644 nd->nd_clientid.qval = clientid.qval;
2646 error = nfsrv_mtostr(nd, stp->ls_owner, stp->ls_ownerlen);
2649 if (!nd->nd_repstat && vp->v_type != VREG) {
2650 if (vp->v_type == VDIR)
2651 nd->nd_repstat = NFSERR_ISDIR;
2653 nd->nd_repstat = NFSERR_INVAL;
2655 if (!nd->nd_repstat)
2656 nd->nd_repstat = nfsrv_lockctrl(vp, &stp, &lop, &cf, clientid,
2657 &stateid, exp, nd, p);
2658 if (nd->nd_repstat) {
2659 if (nd->nd_repstat == NFSERR_DENIED) {
2660 NFSM_BUILD(tl, u_int32_t *, 7 * NFSX_UNSIGNED);
2661 txdr_hyper(cf.cl_first, tl);
2663 if (cf.cl_end == NFS64BITSSET)
2666 len = cf.cl_end - cf.cl_first;
2667 txdr_hyper(len, tl);
2669 if (cf.cl_flags == NFSLCK_WRITE)
2670 *tl++ = txdr_unsigned(NFSV4LOCKT_WRITE);
2672 *tl++ = txdr_unsigned(NFSV4LOCKT_READ);
2673 *tl++ = stp->ls_stateid.other[0];
2674 *tl = stp->ls_stateid.other[1];
2675 (void) nfsm_strtom(nd, cf.cl_owner, cf.cl_ownerlen);
2680 free(stp, M_NFSDSTATE);
2681 NFSEXITCODE2(0, nd);
2686 free(stp, M_NFSDSTATE);
2687 NFSEXITCODE2(error, nd);
2692 * nfsv4 unlock service
2695 nfsrvd_locku(struct nfsrv_descript *nd, __unused int isdgram,
2696 vnode_t vp, struct nfsexstuff *exp)
2700 struct nfsstate *stp;
2701 struct nfslock *lop;
2703 nfsv4stateid_t stateid;
2706 struct thread *p = curthread;
2708 NFSM_DISSECT(tl, u_int32_t *, 6 * NFSX_UNSIGNED + NFSX_STATEID);
2709 stp = malloc(sizeof (struct nfsstate),
2710 M_NFSDSTATE, M_WAITOK);
2711 lop = malloc(sizeof (struct nfslock),
2712 M_NFSDLOCK, M_WAITOK);
2713 stp->ls_flags = NFSLCK_UNLOCK;
2714 lop->lo_flags = NFSLCK_UNLOCK;
2715 stp->ls_op = nd->nd_rp;
2716 i = fxdr_unsigned(int, *tl++);
2718 case NFSV4LOCKT_READW:
2719 stp->ls_flags |= NFSLCK_BLOCKING;
2720 case NFSV4LOCKT_READ:
2722 case NFSV4LOCKT_WRITEW:
2723 stp->ls_flags |= NFSLCK_BLOCKING;
2724 case NFSV4LOCKT_WRITE:
2727 nd->nd_repstat = NFSERR_BADXDR;
2728 free(stp, M_NFSDSTATE);
2729 free(lop, M_NFSDLOCK);
2732 stp->ls_ownerlen = 0;
2733 stp->ls_uid = nd->nd_cred->cr_uid;
2734 stp->ls_seq = fxdr_unsigned(int, *tl++);
2735 stp->ls_stateid.seqid = fxdr_unsigned(u_int32_t, *tl++);
2736 NFSBCOPY((caddr_t)tl, (caddr_t)stp->ls_stateid.other,
2738 tl += (NFSX_STATEIDOTHER / NFSX_UNSIGNED);
2741 * For the special stateid of other all 0s and seqid == 1, set the
2742 * stateid to the current stateid, if it is set.
2744 if ((nd->nd_flag & ND_NFSV41) != 0 && stp->ls_stateid.seqid == 1 &&
2745 stp->ls_stateid.other[0] == 0 && stp->ls_stateid.other[1] == 0 &&
2746 stp->ls_stateid.other[2] == 0) {
2747 if ((nd->nd_flag & ND_CURSTATEID) != 0) {
2748 stp->ls_stateid = nd->nd_curstateid;
2749 stp->ls_stateid.seqid = 0;
2751 nd->nd_repstat = NFSERR_BADSTATEID;
2752 free(stp, M_NFSDSTATE);
2753 free(lop, M_NFSDLOCK);
2758 lop->lo_first = fxdr_hyper(tl);
2760 len = fxdr_hyper(tl);
2761 if (len == NFS64BITSSET) {
2762 lop->lo_end = NFS64BITSSET;
2764 lop->lo_end = lop->lo_first + len;
2765 if (lop->lo_end <= lop->lo_first)
2766 nd->nd_repstat = NFSERR_INVAL;
2768 clientid.lval[0] = stp->ls_stateid.other[0];
2769 clientid.lval[1] = stp->ls_stateid.other[1];
2770 if ((nd->nd_flag & ND_IMPLIEDCLID) != 0) {
2771 if ((nd->nd_flag & ND_NFSV41) != 0)
2772 clientid.qval = nd->nd_clientid.qval;
2773 else if (nd->nd_clientid.qval != clientid.qval)
2774 printf("EEK6 multiple clids\n");
2776 if ((nd->nd_flag & ND_NFSV41) != 0)
2777 printf("EEK! no clientid from session\n");
2778 nd->nd_flag |= ND_IMPLIEDCLID;
2779 nd->nd_clientid.qval = clientid.qval;
2781 if (!nd->nd_repstat && vp->v_type != VREG) {
2782 if (vp->v_type == VDIR)
2783 nd->nd_repstat = NFSERR_ISDIR;
2785 nd->nd_repstat = NFSERR_INVAL;
2788 * Call nfsrv_lockctrl() even if nd_repstat is set, so that the
2789 * seqid# gets incremented. nfsrv_lockctrl() will return the
2790 * value of nd_repstat, if it gets that far.
2792 nd->nd_repstat = nfsrv_lockctrl(vp, &stp, &lop, NULL, clientid,
2793 &stateid, exp, nd, p);
2795 free(stp, M_NFSDSTATE);
2797 free(lop, M_NFSDLOCK);
2798 if (!nd->nd_repstat) {
2799 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID);
2800 *tl++ = txdr_unsigned(stateid.seqid);
2801 NFSBCOPY((caddr_t)stateid.other,(caddr_t)tl,NFSX_STATEIDOTHER);
2805 NFSEXITCODE2(error, nd);
2810 * nfsv4 open service
2813 nfsrvd_open(struct nfsrv_descript *nd, __unused int isdgram,
2814 vnode_t dp, vnode_t *vpp, __unused fhandle_t *fhp, struct nfsexstuff *exp)
2818 struct nfsstate *stp = NULL;
2819 int error = 0, create, claim, exclusive_flag = 0, override;
2820 u_int32_t rflags = NFSV4OPEN_LOCKTYPEPOSIX, acemask;
2821 int how = NFSCREATE_UNCHECKED;
2822 int32_t cverf[2], tverf[2] = { 0, 0 };
2823 vnode_t vp = NULL, dirp = NULL;
2824 struct nfsvattr nva, dirfor, diraft;
2825 struct nameidata named;
2826 nfsv4stateid_t stateid, delegstateid;
2827 nfsattrbit_t attrbits;
2831 NFSACL_T *aclp = NULL;
2832 struct thread *p = curthread;
2835 #ifdef NFS4_ACL_EXTATTR_NAME
2836 aclp = acl_alloc(M_WAITOK);
2839 NFSZERO_ATTRBIT(&attrbits);
2841 named.ni_cnd.cn_nameiop = 0;
2842 NFSM_DISSECT(tl, u_int32_t *, 6 * NFSX_UNSIGNED);
2843 i = fxdr_unsigned(int, *(tl + 5));
2844 if (i <= 0 || i > NFSV4_OPAQUELIMIT) {
2845 nd->nd_repstat = NFSERR_BADXDR;
2848 stp = malloc(sizeof (struct nfsstate) + i,
2849 M_NFSDSTATE, M_WAITOK);
2850 stp->ls_ownerlen = i;
2851 stp->ls_op = nd->nd_rp;
2852 stp->ls_flags = NFSLCK_OPEN;
2853 stp->ls_uid = nd->nd_cred->cr_uid;
2854 stp->ls_seq = fxdr_unsigned(u_int32_t, *tl++);
2855 i = fxdr_unsigned(int, *tl++);
2857 if ((i & (NFSV4OPEN_WANTDELEGMASK | NFSV4OPEN_WANTSIGNALDELEG |
2858 NFSV4OPEN_WANTPUSHDELEG)) != 0 && (nd->nd_flag & ND_NFSV41) != 0) {
2860 /* For now, ignore these. */
2861 i &= ~(NFSV4OPEN_WANTPUSHDELEG | NFSV4OPEN_WANTSIGNALDELEG);
2862 switch (i & NFSV4OPEN_WANTDELEGMASK) {
2863 case NFSV4OPEN_WANTANYDELEG:
2864 stp->ls_flags |= (NFSLCK_WANTRDELEG |
2866 i &= ~NFSV4OPEN_WANTDELEGMASK;
2868 case NFSV4OPEN_WANTREADDELEG:
2869 stp->ls_flags |= NFSLCK_WANTRDELEG;
2870 i &= ~NFSV4OPEN_WANTDELEGMASK;
2872 case NFSV4OPEN_WANTWRITEDELEG:
2873 stp->ls_flags |= NFSLCK_WANTWDELEG;
2874 i &= ~NFSV4OPEN_WANTDELEGMASK;
2876 case NFSV4OPEN_WANTNODELEG:
2877 stp->ls_flags |= NFSLCK_WANTNODELEG;
2878 i &= ~NFSV4OPEN_WANTDELEGMASK;
2880 case NFSV4OPEN_WANTCANCEL:
2881 printf("NFSv4: ignore Open WantCancel\n");
2882 i &= ~NFSV4OPEN_WANTDELEGMASK;
2885 /* nd_repstat will be set to NFSERR_INVAL below. */
2890 case NFSV4OPEN_ACCESSREAD:
2891 stp->ls_flags |= NFSLCK_READACCESS;
2893 case NFSV4OPEN_ACCESSWRITE:
2894 stp->ls_flags |= NFSLCK_WRITEACCESS;
2896 case NFSV4OPEN_ACCESSBOTH:
2897 stp->ls_flags |= (NFSLCK_READACCESS | NFSLCK_WRITEACCESS);
2900 nd->nd_repstat = NFSERR_INVAL;
2902 i = fxdr_unsigned(int, *tl++);
2904 case NFSV4OPEN_DENYNONE:
2906 case NFSV4OPEN_DENYREAD:
2907 stp->ls_flags |= NFSLCK_READDENY;
2909 case NFSV4OPEN_DENYWRITE:
2910 stp->ls_flags |= NFSLCK_WRITEDENY;
2912 case NFSV4OPEN_DENYBOTH:
2913 stp->ls_flags |= (NFSLCK_READDENY | NFSLCK_WRITEDENY);
2916 nd->nd_repstat = NFSERR_INVAL;
2918 clientid.lval[0] = *tl++;
2919 clientid.lval[1] = *tl;
2920 if ((nd->nd_flag & ND_IMPLIEDCLID) != 0) {
2921 if ((nd->nd_flag & ND_NFSV41) != 0)
2922 clientid.qval = nd->nd_clientid.qval;
2923 else if (nd->nd_clientid.qval != clientid.qval)
2924 printf("EEK7 multiple clids\n");
2926 if ((nd->nd_flag & ND_NFSV41) != 0)
2927 printf("EEK! no clientid from session\n");
2928 nd->nd_flag |= ND_IMPLIEDCLID;
2929 nd->nd_clientid.qval = clientid.qval;
2931 error = nfsrv_mtostr(nd, stp->ls_owner, stp->ls_ownerlen);
2934 NFSVNO_ATTRINIT(&nva);
2935 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2936 create = fxdr_unsigned(int, *tl);
2937 if (!nd->nd_repstat)
2938 nd->nd_repstat = nfsvno_getattr(dp, &dirfor, nd, p, 0, NULL);
2939 if (create == NFSV4OPEN_CREATE) {
2942 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2943 how = fxdr_unsigned(int, *tl);
2945 case NFSCREATE_UNCHECKED:
2946 case NFSCREATE_GUARDED:
2947 error = nfsv4_sattr(nd, NULL, &nva, &attrbits, aclp, p);
2951 * If the na_gid being set is the same as that of
2952 * the directory it is going in, clear it, since
2953 * that is what will be set by default. This allows
2954 * a user that isn't in that group to do the create.
2956 if (!nd->nd_repstat && NFSVNO_ISSETGID(&nva) &&
2957 nva.na_gid == dirfor.na_gid)
2958 NFSVNO_UNSET(&nva, gid);
2959 if (!nd->nd_repstat)
2960 nd->nd_repstat = nfsrv_checkuidgid(nd, &nva);
2962 case NFSCREATE_EXCLUSIVE:
2963 NFSM_DISSECT(tl, u_int32_t *, NFSX_VERF);
2967 case NFSCREATE_EXCLUSIVE41:
2968 NFSM_DISSECT(tl, u_int32_t *, NFSX_VERF);
2971 error = nfsv4_sattr(nd, NULL, &nva, &attrbits, aclp, p);
2974 if (NFSISSET_ATTRBIT(&attrbits,
2975 NFSATTRBIT_TIMEACCESSSET))
2976 nd->nd_repstat = NFSERR_INVAL;
2978 * If the na_gid being set is the same as that of
2979 * the directory it is going in, clear it, since
2980 * that is what will be set by default. This allows
2981 * a user that isn't in that group to do the create.
2983 if (nd->nd_repstat == 0 && NFSVNO_ISSETGID(&nva) &&
2984 nva.na_gid == dirfor.na_gid)
2985 NFSVNO_UNSET(&nva, gid);
2986 if (nd->nd_repstat == 0)
2987 nd->nd_repstat = nfsrv_checkuidgid(nd, &nva);
2990 nd->nd_repstat = NFSERR_BADXDR;
2993 } else if (create != NFSV4OPEN_NOCREATE) {
2994 nd->nd_repstat = NFSERR_BADXDR;
2999 * Now, handle the claim, which usually includes looking up a
3000 * name in the directory referenced by dp. The exception is
3001 * NFSV4OPEN_CLAIMPREVIOUS.
3003 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3004 claim = fxdr_unsigned(int, *tl);
3005 if (claim == NFSV4OPEN_CLAIMDELEGATECUR || claim ==
3006 NFSV4OPEN_CLAIMDELEGATECURFH) {
3007 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID);
3008 stateid.seqid = fxdr_unsigned(u_int32_t, *tl++);
3009 NFSBCOPY((caddr_t)tl,(caddr_t)stateid.other,NFSX_STATEIDOTHER);
3010 stp->ls_flags |= NFSLCK_DELEGCUR;
3011 } else if (claim == NFSV4OPEN_CLAIMDELEGATEPREV || claim ==
3012 NFSV4OPEN_CLAIMDELEGATEPREVFH) {
3013 stp->ls_flags |= NFSLCK_DELEGPREV;
3015 if (claim == NFSV4OPEN_CLAIMNULL || claim == NFSV4OPEN_CLAIMDELEGATECUR
3016 || claim == NFSV4OPEN_CLAIMDELEGATEPREV) {
3017 if (!nd->nd_repstat && create == NFSV4OPEN_CREATE &&
3018 claim != NFSV4OPEN_CLAIMNULL)
3019 nd->nd_repstat = NFSERR_INVAL;
3020 if (nd->nd_repstat) {
3021 nd->nd_repstat = nfsrv_opencheck(clientid,
3022 &stateid, stp, NULL, nd, p, nd->nd_repstat);
3025 if (create == NFSV4OPEN_CREATE)
3026 NFSNAMEICNDSET(&named.ni_cnd, nd->nd_cred, CREATE,
3027 LOCKPARENT | LOCKLEAF | NOCACHE);
3029 NFSNAMEICNDSET(&named.ni_cnd, nd->nd_cred, LOOKUP,
3031 nfsvno_setpathbuf(&named, &bufp, &hashp);
3032 error = nfsrv_parsename(nd, bufp, hashp, &named.ni_pathlen);
3035 #ifdef NFS4_ACL_EXTATTR_NAME
3038 free(stp, M_NFSDSTATE);
3039 nfsvno_relpathbuf(&named);
3040 NFSEXITCODE2(error, nd);
3043 if (!nd->nd_repstat) {
3044 nd->nd_repstat = nfsvno_namei(nd, &named, dp, 0, exp,
3049 nfsvno_relpathbuf(&named);
3051 if (create == NFSV4OPEN_CREATE) {
3053 case NFSCREATE_UNCHECKED:
3054 if (done_namei && named.ni_vp != NULL) {
3056 * Clear the setable attribute bits, except
3057 * for Size, if it is being truncated.
3059 NFSZERO_ATTRBIT(&attrbits);
3060 if (NFSVNO_ISSETSIZE(&nva))
3061 NFSSETBIT_ATTRBIT(&attrbits,
3065 case NFSCREATE_GUARDED:
3066 if (done_namei && named.ni_vp != NULL &&
3067 nd->nd_repstat == 0)
3068 nd->nd_repstat = EEXIST;
3070 case NFSCREATE_EXCLUSIVE:
3072 if (done_namei && named.ni_vp == NULL)
3075 case NFSCREATE_EXCLUSIVE41:
3080 nfsvno_open(nd, &named, clientid, &stateid, stp,
3081 &exclusive_flag, &nva, cverf, create, aclp, &attrbits,
3082 nd->nd_cred, done_namei, exp, &vp);
3083 } else if (claim == NFSV4OPEN_CLAIMPREVIOUS || claim ==
3084 NFSV4OPEN_CLAIMFH || claim == NFSV4OPEN_CLAIMDELEGATECURFH ||
3085 claim == NFSV4OPEN_CLAIMDELEGATEPREVFH) {
3086 if (claim == NFSV4OPEN_CLAIMPREVIOUS) {
3087 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3088 i = fxdr_unsigned(int, *tl);
3090 case NFSV4OPEN_DELEGATEREAD:
3091 stp->ls_flags |= NFSLCK_DELEGREAD;
3093 case NFSV4OPEN_DELEGATEWRITE:
3094 stp->ls_flags |= NFSLCK_DELEGWRITE;
3095 case NFSV4OPEN_DELEGATENONE:
3098 nd->nd_repstat = NFSERR_BADXDR;
3101 stp->ls_flags |= NFSLCK_RECLAIM;
3103 if (nd->nd_repstat == 0 && create == NFSV4OPEN_CREATE)
3104 nd->nd_repstat = NFSERR_INVAL;
3107 NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY);
3108 if (!VN_IS_DOOMED(vp))
3109 nd->nd_repstat = nfsrv_opencheck(clientid, &stateid,
3110 stp, vp, nd, p, nd->nd_repstat);
3112 nd->nd_repstat = NFSERR_PERM;
3114 nd->nd_repstat = NFSERR_BADXDR;
3119 * Do basic access checking.
3121 if (!nd->nd_repstat && vp->v_type != VREG) {
3123 * The IETF working group decided that this is the correct
3124 * error return for all non-regular files.
3126 nd->nd_repstat = (vp->v_type == VDIR) ? NFSERR_ISDIR : NFSERR_SYMLINK;
3130 * If the Open is being done for a file that already exists, apply
3131 * normal permission checking including for the file owner, if
3132 * vfs.nfsd.v4openaccess is set.
3133 * Previously, the owner was always allowed to open the file to
3134 * be consistent with the NFS tradition of always allowing the
3135 * owner of the file to write to the file regardless of permissions.
3136 * It now appears that the Linux client expects the owner
3137 * permissions to be checked for opens that are not creating the
3138 * file. I believe the correct approach is to use the Access
3139 * operation's results to be consistent with NFSv3, but that is
3140 * not what the current Linux client appears to be doing.
3141 * Since both the Linux and OpenSolaris NFSv4 servers do this check,
3142 * I have enabled it by default. Since Linux does not apply this
3143 * check for claim_delegate_cur, this code does the same.
3144 * If this semantic change causes a problem, it can be disabled by
3145 * setting the sysctl vfs.nfsd.v4openaccess to 0 to re-enable the
3146 * previous semantics.
3148 if (nfsrv_openaccess && create == NFSV4OPEN_NOCREATE &&
3149 (stp->ls_flags & NFSLCK_DELEGCUR) == 0)
3150 override = NFSACCCHK_NOOVERRIDE;
3152 override = NFSACCCHK_ALLOWOWNER;
3153 if (!nd->nd_repstat && (stp->ls_flags & NFSLCK_WRITEACCESS))
3154 nd->nd_repstat = nfsvno_accchk(vp, VWRITE, nd->nd_cred,
3155 exp, p, override, NFSACCCHK_VPISLOCKED, NULL);
3156 if (!nd->nd_repstat && (stp->ls_flags & NFSLCK_READACCESS)) {
3157 nd->nd_repstat = nfsvno_accchk(vp, VREAD, nd->nd_cred,
3158 exp, p, override, NFSACCCHK_VPISLOCKED, NULL);
3160 nd->nd_repstat = nfsvno_accchk(vp, VEXEC,
3161 nd->nd_cred, exp, p, override,
3162 NFSACCCHK_VPISLOCKED, NULL);
3165 if (!nd->nd_repstat) {
3166 nd->nd_repstat = nfsvno_getattr(vp, &nva, nd, p, 1, NULL);
3167 if (!nd->nd_repstat) {
3168 tverf[0] = nva.na_atime.tv_sec;
3169 tverf[1] = nva.na_atime.tv_nsec;
3172 if (!nd->nd_repstat && exclusive_flag && (cverf[0] != tverf[0] ||
3173 cverf[1] != tverf[1]))
3174 nd->nd_repstat = EEXIST;
3176 * Do the open locking/delegation stuff.
3178 if (!nd->nd_repstat)
3179 nd->nd_repstat = nfsrv_openctrl(nd, vp, &stp, clientid, &stateid,
3180 &delegstateid, &rflags, exp, p, nva.na_filerev);
3183 * vp must be unlocked before the call to nfsvno_getattr(dirp,...)
3184 * below, to avoid a deadlock with the lookup in nfsvno_namei() above.
3185 * (ie: Leave the NFSVOPUNLOCK() about here.)
3190 free(stp, M_NFSDSTATE);
3191 if (!nd->nd_repstat && dirp)
3192 nd->nd_repstat = nfsvno_getattr(dirp, &diraft, nd, p, 0, NULL);
3193 if (!nd->nd_repstat) {
3194 /* For NFSv4.1, set the Current StateID. */
3195 if ((nd->nd_flag & ND_NFSV41) != 0) {
3196 nd->nd_curstateid = stateid;
3197 nd->nd_flag |= ND_CURSTATEID;
3199 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID + 6 * NFSX_UNSIGNED);
3200 *tl++ = txdr_unsigned(stateid.seqid);
3201 NFSBCOPY((caddr_t)stateid.other,(caddr_t)tl,NFSX_STATEIDOTHER);
3202 tl += (NFSX_STATEIDOTHER / NFSX_UNSIGNED);
3203 if (claim == NFSV4OPEN_CLAIMPREVIOUS) {
3204 *tl++ = newnfs_true;
3210 *tl++ = newnfs_false; /* Since dirp is not locked */
3211 txdr_hyper(dirfor.na_filerev, tl);
3213 txdr_hyper(diraft.na_filerev, tl);
3216 *tl = txdr_unsigned(rflags & NFSV4OPEN_RFLAGS);
3217 (void) nfsrv_putattrbit(nd, &attrbits);
3218 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
3219 if (rflags & NFSV4OPEN_READDELEGATE)
3220 *tl = txdr_unsigned(NFSV4OPEN_DELEGATEREAD);
3221 else if (rflags & NFSV4OPEN_WRITEDELEGATE)
3222 *tl = txdr_unsigned(NFSV4OPEN_DELEGATEWRITE);
3223 else if (retext != 0) {
3224 *tl = txdr_unsigned(NFSV4OPEN_DELEGATENONEEXT);
3225 if ((rflags & NFSV4OPEN_WDNOTWANTED) != 0) {
3226 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
3227 *tl = txdr_unsigned(NFSV4OPEN_NOTWANTED);
3228 } else if ((rflags & NFSV4OPEN_WDSUPPFTYPE) != 0) {
3229 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
3230 *tl = txdr_unsigned(NFSV4OPEN_NOTSUPPFTYPE);
3231 } else if ((rflags & NFSV4OPEN_WDCONTENTION) != 0) {
3232 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
3233 *tl++ = txdr_unsigned(NFSV4OPEN_CONTENTION);
3235 } else if ((rflags & NFSV4OPEN_WDRESOURCE) != 0) {
3236 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
3237 *tl++ = txdr_unsigned(NFSV4OPEN_RESOURCE);
3240 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
3241 *tl = txdr_unsigned(NFSV4OPEN_NOTWANTED);
3244 *tl = txdr_unsigned(NFSV4OPEN_DELEGATENONE);
3245 if (rflags & (NFSV4OPEN_READDELEGATE|NFSV4OPEN_WRITEDELEGATE)) {
3246 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID+NFSX_UNSIGNED);
3247 *tl++ = txdr_unsigned(delegstateid.seqid);
3248 NFSBCOPY((caddr_t)delegstateid.other, (caddr_t)tl,
3250 tl += (NFSX_STATEIDOTHER / NFSX_UNSIGNED);
3251 if (rflags & NFSV4OPEN_RECALL)
3255 if (rflags & NFSV4OPEN_WRITEDELEGATE) {
3256 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
3257 *tl++ = txdr_unsigned(NFSV4OPEN_LIMITSIZE);
3258 txdr_hyper(nva.na_size, tl);
3260 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
3261 *tl++ = txdr_unsigned(NFSV4ACE_ALLOWEDTYPE);
3262 *tl++ = txdr_unsigned(0x0);
3263 acemask = NFSV4ACE_ALLFILESMASK;
3264 if (nva.na_mode & S_IRUSR)
3265 acemask |= NFSV4ACE_READMASK;
3266 if (nva.na_mode & S_IWUSR)
3267 acemask |= NFSV4ACE_WRITEMASK;
3268 if (nva.na_mode & S_IXUSR)
3269 acemask |= NFSV4ACE_EXECUTEMASK;
3270 *tl = txdr_unsigned(acemask);
3271 (void) nfsm_strtom(nd, "OWNER@", 6);
3279 #ifdef NFS4_ACL_EXTATTR_NAME
3282 NFSEXITCODE2(0, nd);
3286 #ifdef NFS4_ACL_EXTATTR_NAME
3290 free(stp, M_NFSDSTATE);
3291 NFSEXITCODE2(error, nd);
3296 * nfsv4 close service
3299 nfsrvd_close(struct nfsrv_descript *nd, __unused int isdgram,
3300 vnode_t vp, __unused struct nfsexstuff *exp)
3303 struct nfsstate st, *stp = &st;
3304 int error = 0, writeacc;
3305 nfsv4stateid_t stateid;
3308 struct thread *p = curthread;
3310 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED + NFSX_STATEID);
3311 stp->ls_seq = fxdr_unsigned(u_int32_t, *tl++);
3312 stp->ls_ownerlen = 0;
3313 stp->ls_op = nd->nd_rp;
3314 stp->ls_uid = nd->nd_cred->cr_uid;
3315 stp->ls_stateid.seqid = fxdr_unsigned(u_int32_t, *tl++);
3316 NFSBCOPY((caddr_t)tl, (caddr_t)stp->ls_stateid.other,
3320 * For the special stateid of other all 0s and seqid == 1, set the
3321 * stateid to the current stateid, if it is set.
3323 if ((nd->nd_flag & ND_NFSV41) != 0 && stp->ls_stateid.seqid == 1 &&
3324 stp->ls_stateid.other[0] == 0 && stp->ls_stateid.other[1] == 0 &&
3325 stp->ls_stateid.other[2] == 0) {
3326 if ((nd->nd_flag & ND_CURSTATEID) != 0)
3327 stp->ls_stateid = nd->nd_curstateid;
3329 nd->nd_repstat = NFSERR_BADSTATEID;
3334 stp->ls_flags = NFSLCK_CLOSE;
3335 clientid.lval[0] = stp->ls_stateid.other[0];
3336 clientid.lval[1] = stp->ls_stateid.other[1];
3337 if ((nd->nd_flag & ND_IMPLIEDCLID) != 0) {
3338 if ((nd->nd_flag & ND_NFSV41) != 0)
3339 clientid.qval = nd->nd_clientid.qval;
3340 else if (nd->nd_clientid.qval != clientid.qval)
3341 printf("EEK8 multiple clids\n");
3343 if ((nd->nd_flag & ND_NFSV41) != 0)
3344 printf("EEK! no clientid from session\n");
3345 nd->nd_flag |= ND_IMPLIEDCLID;
3346 nd->nd_clientid.qval = clientid.qval;
3348 nd->nd_repstat = nfsrv_openupdate(vp, stp, clientid, &stateid, nd, p,
3350 /* For pNFS, update the attributes. */
3351 if (writeacc != 0 || nfsrv_pnfsatime != 0)
3352 nfsrv_updatemdsattr(vp, &na, p);
3354 if (!nd->nd_repstat) {
3356 * If the stateid that has been closed is the current stateid,
3359 if ((nd->nd_flag & ND_CURSTATEID) != 0 &&
3360 stateid.other[0] == nd->nd_curstateid.other[0] &&
3361 stateid.other[1] == nd->nd_curstateid.other[1] &&
3362 stateid.other[2] == nd->nd_curstateid.other[2])
3363 nd->nd_flag &= ~ND_CURSTATEID;
3364 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID);
3365 *tl++ = txdr_unsigned(stateid.seqid);
3366 NFSBCOPY((caddr_t)stateid.other,(caddr_t)tl,NFSX_STATEIDOTHER);
3368 NFSEXITCODE2(0, nd);
3372 NFSEXITCODE2(error, nd);
3377 * nfsv4 delegpurge service
3380 nfsrvd_delegpurge(struct nfsrv_descript *nd, __unused int isdgram,
3381 __unused vnode_t vp, __unused struct nfsexstuff *exp)
3386 struct thread *p = curthread;
3388 if ((nd->nd_repstat = nfsd_checkrootexp(nd)) != 0)
3390 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
3391 clientid.lval[0] = *tl++;
3392 clientid.lval[1] = *tl;
3393 if ((nd->nd_flag & ND_IMPLIEDCLID) != 0) {
3394 if ((nd->nd_flag & ND_NFSV41) != 0)
3395 clientid.qval = nd->nd_clientid.qval;
3396 else if (nd->nd_clientid.qval != clientid.qval)
3397 printf("EEK9 multiple clids\n");
3399 if ((nd->nd_flag & ND_NFSV41) != 0)
3400 printf("EEK! no clientid from session\n");
3401 nd->nd_flag |= ND_IMPLIEDCLID;
3402 nd->nd_clientid.qval = clientid.qval;
3404 nd->nd_repstat = nfsrv_delegupdate(nd, clientid, NULL, NULL,
3405 NFSV4OP_DELEGPURGE, nd->nd_cred, p, NULL);
3407 NFSEXITCODE2(error, nd);
3412 * nfsv4 delegreturn service
3415 nfsrvd_delegreturn(struct nfsrv_descript *nd, __unused int isdgram,
3416 vnode_t vp, __unused struct nfsexstuff *exp)
3419 int error = 0, writeacc;
3420 nfsv4stateid_t stateid;
3423 struct thread *p = curthread;
3425 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID);
3426 stateid.seqid = fxdr_unsigned(u_int32_t, *tl++);
3427 NFSBCOPY((caddr_t)tl, (caddr_t)stateid.other, NFSX_STATEIDOTHER);
3428 clientid.lval[0] = stateid.other[0];
3429 clientid.lval[1] = stateid.other[1];
3430 if ((nd->nd_flag & ND_IMPLIEDCLID) != 0) {
3431 if ((nd->nd_flag & ND_NFSV41) != 0)
3432 clientid.qval = nd->nd_clientid.qval;
3433 else if (nd->nd_clientid.qval != clientid.qval)
3434 printf("EEK10 multiple clids\n");
3436 if ((nd->nd_flag & ND_NFSV41) != 0)
3437 printf("EEK! no clientid from session\n");
3438 nd->nd_flag |= ND_IMPLIEDCLID;
3439 nd->nd_clientid.qval = clientid.qval;
3441 nd->nd_repstat = nfsrv_delegupdate(nd, clientid, &stateid, vp,
3442 NFSV4OP_DELEGRETURN, nd->nd_cred, p, &writeacc);
3443 /* For pNFS, update the attributes. */
3444 if (writeacc != 0 || nfsrv_pnfsatime != 0)
3445 nfsrv_updatemdsattr(vp, &na, p);
3448 NFSEXITCODE2(error, nd);
3453 * nfsv4 get file handle service
3456 nfsrvd_getfh(struct nfsrv_descript *nd, __unused int isdgram,
3457 vnode_t vp, __unused struct nfsexstuff *exp)
3460 struct thread *p = curthread;
3462 nd->nd_repstat = nfsvno_getfh(vp, &fh, p);
3464 if (!nd->nd_repstat)
3465 (void) nfsm_fhtom(nd, (u_int8_t *)&fh, 0, 0);
3466 NFSEXITCODE2(0, nd);
3471 * nfsv4 open confirm service
3474 nfsrvd_openconfirm(struct nfsrv_descript *nd, __unused int isdgram,
3475 vnode_t vp, __unused struct nfsexstuff *exp)
3478 struct nfsstate st, *stp = &st;
3480 nfsv4stateid_t stateid;
3482 struct thread *p = curthread;
3484 if ((nd->nd_flag & ND_NFSV41) != 0) {
3485 nd->nd_repstat = NFSERR_NOTSUPP;
3488 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID + NFSX_UNSIGNED);
3489 stp->ls_ownerlen = 0;
3490 stp->ls_op = nd->nd_rp;
3491 stp->ls_uid = nd->nd_cred->cr_uid;
3492 stp->ls_stateid.seqid = fxdr_unsigned(u_int32_t, *tl++);
3493 NFSBCOPY((caddr_t)tl, (caddr_t)stp->ls_stateid.other,
3495 tl += (NFSX_STATEIDOTHER / NFSX_UNSIGNED);
3496 stp->ls_seq = fxdr_unsigned(u_int32_t, *tl);
3497 stp->ls_flags = NFSLCK_CONFIRM;
3498 clientid.lval[0] = stp->ls_stateid.other[0];
3499 clientid.lval[1] = stp->ls_stateid.other[1];
3500 if ((nd->nd_flag & ND_IMPLIEDCLID) != 0) {
3501 if ((nd->nd_flag & ND_NFSV41) != 0)
3502 clientid.qval = nd->nd_clientid.qval;
3503 else if (nd->nd_clientid.qval != clientid.qval)
3504 printf("EEK11 multiple clids\n");
3506 if ((nd->nd_flag & ND_NFSV41) != 0)
3507 printf("EEK! no clientid from session\n");
3508 nd->nd_flag |= ND_IMPLIEDCLID;
3509 nd->nd_clientid.qval = clientid.qval;
3511 nd->nd_repstat = nfsrv_openupdate(vp, stp, clientid, &stateid, nd, p,
3513 if (!nd->nd_repstat) {
3514 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID);
3515 *tl++ = txdr_unsigned(stateid.seqid);
3516 NFSBCOPY((caddr_t)stateid.other,(caddr_t)tl,NFSX_STATEIDOTHER);
3520 NFSEXITCODE2(error, nd);
3525 * nfsv4 open downgrade service
3528 nfsrvd_opendowngrade(struct nfsrv_descript *nd, __unused int isdgram,
3529 vnode_t vp, __unused struct nfsexstuff *exp)
3533 struct nfsstate st, *stp = &st;
3535 nfsv4stateid_t stateid;
3537 struct thread *p = curthread;
3539 /* opendowngrade can only work on a file object.*/
3540 if (vp->v_type != VREG) {
3541 error = NFSERR_INVAL;
3544 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID + 3 * NFSX_UNSIGNED);
3545 stp->ls_ownerlen = 0;
3546 stp->ls_op = nd->nd_rp;
3547 stp->ls_uid = nd->nd_cred->cr_uid;
3548 stp->ls_stateid.seqid = fxdr_unsigned(u_int32_t, *tl++);
3549 NFSBCOPY((caddr_t)tl, (caddr_t)stp->ls_stateid.other,
3551 tl += (NFSX_STATEIDOTHER / NFSX_UNSIGNED);
3554 * For the special stateid of other all 0s and seqid == 1, set the
3555 * stateid to the current stateid, if it is set.
3557 if ((nd->nd_flag & ND_NFSV41) != 0 && stp->ls_stateid.seqid == 1 &&
3558 stp->ls_stateid.other[0] == 0 && stp->ls_stateid.other[1] == 0 &&
3559 stp->ls_stateid.other[2] == 0) {
3560 if ((nd->nd_flag & ND_CURSTATEID) != 0)
3561 stp->ls_stateid = nd->nd_curstateid;
3563 nd->nd_repstat = NFSERR_BADSTATEID;
3568 stp->ls_seq = fxdr_unsigned(u_int32_t, *tl++);
3569 i = fxdr_unsigned(int, *tl++);
3570 if ((nd->nd_flag & ND_NFSV41) != 0)
3571 i &= ~NFSV4OPEN_WANTDELEGMASK;
3573 case NFSV4OPEN_ACCESSREAD:
3574 stp->ls_flags = (NFSLCK_READACCESS | NFSLCK_DOWNGRADE);
3576 case NFSV4OPEN_ACCESSWRITE:
3577 stp->ls_flags = (NFSLCK_WRITEACCESS | NFSLCK_DOWNGRADE);
3579 case NFSV4OPEN_ACCESSBOTH:
3580 stp->ls_flags = (NFSLCK_READACCESS | NFSLCK_WRITEACCESS |
3584 nd->nd_repstat = NFSERR_INVAL;
3586 i = fxdr_unsigned(int, *tl);
3588 case NFSV4OPEN_DENYNONE:
3590 case NFSV4OPEN_DENYREAD:
3591 stp->ls_flags |= NFSLCK_READDENY;
3593 case NFSV4OPEN_DENYWRITE:
3594 stp->ls_flags |= NFSLCK_WRITEDENY;
3596 case NFSV4OPEN_DENYBOTH:
3597 stp->ls_flags |= (NFSLCK_READDENY | NFSLCK_WRITEDENY);
3600 nd->nd_repstat = NFSERR_INVAL;
3603 clientid.lval[0] = stp->ls_stateid.other[0];
3604 clientid.lval[1] = stp->ls_stateid.other[1];
3605 if ((nd->nd_flag & ND_IMPLIEDCLID) != 0) {
3606 if ((nd->nd_flag & ND_NFSV41) != 0)
3607 clientid.qval = nd->nd_clientid.qval;
3608 else if (nd->nd_clientid.qval != clientid.qval)
3609 printf("EEK12 multiple clids\n");
3611 if ((nd->nd_flag & ND_NFSV41) != 0)
3612 printf("EEK! no clientid from session\n");
3613 nd->nd_flag |= ND_IMPLIEDCLID;
3614 nd->nd_clientid.qval = clientid.qval;
3616 if (!nd->nd_repstat)
3617 nd->nd_repstat = nfsrv_openupdate(vp, stp, clientid, &stateid,
3619 if (!nd->nd_repstat) {
3620 /* For NFSv4.1, set the Current StateID. */
3621 if ((nd->nd_flag & ND_NFSV41) != 0) {
3622 nd->nd_curstateid = stateid;
3623 nd->nd_flag |= ND_CURSTATEID;
3625 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID);
3626 *tl++ = txdr_unsigned(stateid.seqid);
3627 NFSBCOPY((caddr_t)stateid.other,(caddr_t)tl,NFSX_STATEIDOTHER);
3631 NFSEXITCODE2(error, nd);
3636 * nfsv4 renew lease service
3639 nfsrvd_renew(struct nfsrv_descript *nd, __unused int isdgram,
3640 __unused vnode_t vp, __unused struct nfsexstuff *exp)
3645 struct thread *p = curthread;
3647 if ((nd->nd_flag & ND_NFSV41) != 0) {
3648 nd->nd_repstat = NFSERR_NOTSUPP;
3651 if ((nd->nd_repstat = nfsd_checkrootexp(nd)) != 0)
3653 NFSM_DISSECT(tl, u_int32_t *, NFSX_HYPER);
3654 clientid.lval[0] = *tl++;
3655 clientid.lval[1] = *tl;
3656 if ((nd->nd_flag & ND_IMPLIEDCLID) != 0) {
3657 if ((nd->nd_flag & ND_NFSV41) != 0)
3658 clientid.qval = nd->nd_clientid.qval;
3659 else if (nd->nd_clientid.qval != clientid.qval)
3660 printf("EEK13 multiple clids\n");
3662 if ((nd->nd_flag & ND_NFSV41) != 0)
3663 printf("EEK! no clientid from session\n");
3664 nd->nd_flag |= ND_IMPLIEDCLID;
3665 nd->nd_clientid.qval = clientid.qval;
3667 nd->nd_repstat = nfsrv_getclient(clientid, (CLOPS_RENEWOP|CLOPS_RENEW),
3668 NULL, NULL, (nfsquad_t)((u_quad_t)0), 0, nd, p);
3670 NFSEXITCODE2(error, nd);
3675 * nfsv4 security info service
3678 nfsrvd_secinfo(struct nfsrv_descript *nd, int isdgram,
3679 vnode_t dp, struct nfsexstuff *exp)
3683 struct nameidata named;
3684 vnode_t dirp = NULL, vp;
3686 struct nfsexstuff retnes;
3692 struct thread *p = curthread;
3695 * All this just to get the export flags for the name.
3697 NFSNAMEICNDSET(&named.ni_cnd, nd->nd_cred, LOOKUP,
3699 nfsvno_setpathbuf(&named, &bufp, &hashp);
3700 error = nfsrv_parsename(nd, bufp, hashp, &named.ni_pathlen);
3703 nfsvno_relpathbuf(&named);
3706 if (!nd->nd_repstat) {
3707 nd->nd_repstat = nfsvno_namei(nd, &named, dp, 1, exp, &dirp);
3710 nfsvno_relpathbuf(&named);
3716 nfsvno_relpathbuf(&named);
3717 fh.nfsrvfh_len = NFSX_MYFH;
3719 nd->nd_repstat = nfsvno_getfh(vp, (fhandle_t *)fh.nfsrvfh_data, p);
3721 savflag = nd->nd_flag;
3722 if (!nd->nd_repstat) {
3724 * Pretend the next op is Secinfo, so that no wrongsec
3725 * test will be done.
3727 nfsd_fhtovp(nd, &fh, LK_SHARED, &vp, &retnes, NULL, 0,
3732 nd->nd_flag = savflag;
3737 * Finally have the export flags for name, so we can create
3738 * the security info.
3741 NFSM_BUILD(sizp, u_int32_t *, NFSX_UNSIGNED);
3743 /* If nes_numsecflavor == 0, all are allowed. */
3744 if (retnes.nes_numsecflavor == 0) {
3745 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_UNSIGNED);
3746 *tl++ = txdr_unsigned(RPCAUTH_UNIX);
3747 *tl = txdr_unsigned(RPCAUTH_GSS);
3748 nfsm_strtom(nd, nfsgss_mechlist[KERBV_MECH].str,
3749 nfsgss_mechlist[KERBV_MECH].len);
3750 NFSM_BUILD(tl, uint32_t *, 3 * NFSX_UNSIGNED);
3751 *tl++ = txdr_unsigned(GSS_KERBV_QOP);
3752 *tl++ = txdr_unsigned(RPCAUTHGSS_SVCNONE);
3753 *tl = txdr_unsigned(RPCAUTH_GSS);
3754 nfsm_strtom(nd, nfsgss_mechlist[KERBV_MECH].str,
3755 nfsgss_mechlist[KERBV_MECH].len);
3756 NFSM_BUILD(tl, uint32_t *, 3 * NFSX_UNSIGNED);
3757 *tl++ = txdr_unsigned(GSS_KERBV_QOP);
3758 *tl++ = txdr_unsigned(RPCAUTHGSS_SVCINTEGRITY);
3759 *tl = txdr_unsigned(RPCAUTH_GSS);
3760 nfsm_strtom(nd, nfsgss_mechlist[KERBV_MECH].str,
3761 nfsgss_mechlist[KERBV_MECH].len);
3762 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_UNSIGNED);
3763 *tl++ = txdr_unsigned(GSS_KERBV_QOP);
3764 *tl = txdr_unsigned(RPCAUTHGSS_SVCPRIVACY);
3767 for (i = 0; i < retnes.nes_numsecflavor; i++) {
3768 if (retnes.nes_secflavors[i] == AUTH_SYS) {
3769 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
3770 *tl = txdr_unsigned(RPCAUTH_UNIX);
3772 } else if (retnes.nes_secflavors[i] == RPCSEC_GSS_KRB5) {
3773 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
3774 *tl++ = txdr_unsigned(RPCAUTH_GSS);
3775 (void) nfsm_strtom(nd, nfsgss_mechlist[KERBV_MECH].str,
3776 nfsgss_mechlist[KERBV_MECH].len);
3777 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
3778 *tl++ = txdr_unsigned(GSS_KERBV_QOP);
3779 *tl = txdr_unsigned(RPCAUTHGSS_SVCNONE);
3781 } else if (retnes.nes_secflavors[i] == RPCSEC_GSS_KRB5I) {
3782 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
3783 *tl++ = txdr_unsigned(RPCAUTH_GSS);
3784 (void) nfsm_strtom(nd, nfsgss_mechlist[KERBV_MECH].str,
3785 nfsgss_mechlist[KERBV_MECH].len);
3786 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
3787 *tl++ = txdr_unsigned(GSS_KERBV_QOP);
3788 *tl = txdr_unsigned(RPCAUTHGSS_SVCINTEGRITY);
3790 } else if (retnes.nes_secflavors[i] == RPCSEC_GSS_KRB5P) {
3791 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
3792 *tl++ = txdr_unsigned(RPCAUTH_GSS);
3793 (void) nfsm_strtom(nd, nfsgss_mechlist[KERBV_MECH].str,
3794 nfsgss_mechlist[KERBV_MECH].len);
3795 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
3796 *tl++ = txdr_unsigned(GSS_KERBV_QOP);
3797 *tl = txdr_unsigned(RPCAUTHGSS_SVCPRIVACY);
3801 *sizp = txdr_unsigned(len);
3804 NFSEXITCODE2(error, nd);
3809 * nfsv4 security info no name service
3812 nfsrvd_secinfononame(struct nfsrv_descript *nd, int isdgram,
3813 vnode_t dp, struct nfsexstuff *exp)
3815 uint32_t *tl, *sizp;
3816 struct nameidata named;
3817 vnode_t dirp = NULL, vp;
3819 struct nfsexstuff retnes;
3820 int error = 0, fhstyle, i, len;
3824 struct thread *p = curthread;
3826 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
3827 fhstyle = fxdr_unsigned(int, *tl);
3829 case NFSSECINFONONAME_PARENT:
3830 if (dp->v_type != VDIR) {
3832 nd->nd_repstat = NFSERR_NOTDIR;
3835 NFSNAMEICNDSET(&named.ni_cnd, nd->nd_cred, LOOKUP,
3837 nfsvno_setpathbuf(&named, &bufp, &hashp);
3838 error = nfsrv_parsename(nd, bufp, hashp, &named.ni_pathlen);
3841 nfsvno_relpathbuf(&named);
3844 if (nd->nd_repstat == 0)
3845 nd->nd_repstat = nfsvno_namei(nd, &named, dp, 1, exp, &dirp);
3850 nfsvno_relpathbuf(&named);
3853 case NFSSECINFONONAME_CURFH:
3857 nd->nd_repstat = NFSERR_INVAL;
3860 if (nd->nd_repstat != 0)
3862 fh.nfsrvfh_len = NFSX_MYFH;
3863 nd->nd_repstat = nfsvno_getfh(vp, (fhandle_t *)fh.nfsrvfh_data, p);
3865 savflag = nd->nd_flag;
3866 if (nd->nd_repstat == 0) {
3868 * Pretend the next op is Secinfo, so that no wrongsec
3869 * test will be done.
3871 nfsd_fhtovp(nd, &fh, LK_SHARED, &vp, &retnes, NULL, 0,
3876 nd->nd_flag = savflag;
3877 if (nd->nd_repstat != 0)
3881 * Finally have the export flags for fh/parent, so we can create
3882 * the security info.
3885 NFSM_BUILD(sizp, uint32_t *, NFSX_UNSIGNED);
3887 /* If nes_numsecflavor == 0, all are allowed. */
3888 if (retnes.nes_numsecflavor == 0) {
3889 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_UNSIGNED);
3890 *tl++ = txdr_unsigned(RPCAUTH_UNIX);
3891 *tl = txdr_unsigned(RPCAUTH_GSS);
3892 nfsm_strtom(nd, nfsgss_mechlist[KERBV_MECH].str,
3893 nfsgss_mechlist[KERBV_MECH].len);
3894 NFSM_BUILD(tl, uint32_t *, 3 * NFSX_UNSIGNED);
3895 *tl++ = txdr_unsigned(GSS_KERBV_QOP);
3896 *tl++ = txdr_unsigned(RPCAUTHGSS_SVCNONE);
3897 *tl = txdr_unsigned(RPCAUTH_GSS);
3898 nfsm_strtom(nd, nfsgss_mechlist[KERBV_MECH].str,
3899 nfsgss_mechlist[KERBV_MECH].len);
3900 NFSM_BUILD(tl, uint32_t *, 3 * NFSX_UNSIGNED);
3901 *tl++ = txdr_unsigned(GSS_KERBV_QOP);
3902 *tl++ = txdr_unsigned(RPCAUTHGSS_SVCINTEGRITY);
3903 *tl = txdr_unsigned(RPCAUTH_GSS);
3904 nfsm_strtom(nd, nfsgss_mechlist[KERBV_MECH].str,
3905 nfsgss_mechlist[KERBV_MECH].len);
3906 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_UNSIGNED);
3907 *tl++ = txdr_unsigned(GSS_KERBV_QOP);
3908 *tl = txdr_unsigned(RPCAUTHGSS_SVCPRIVACY);
3911 for (i = 0; i < retnes.nes_numsecflavor; i++) {
3912 if (retnes.nes_secflavors[i] == AUTH_SYS) {
3913 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
3914 *tl = txdr_unsigned(RPCAUTH_UNIX);
3916 } else if (retnes.nes_secflavors[i] == RPCSEC_GSS_KRB5) {
3917 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
3918 *tl = txdr_unsigned(RPCAUTH_GSS);
3919 nfsm_strtom(nd, nfsgss_mechlist[KERBV_MECH].str,
3920 nfsgss_mechlist[KERBV_MECH].len);
3921 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_UNSIGNED);
3922 *tl++ = txdr_unsigned(GSS_KERBV_QOP);
3923 *tl = txdr_unsigned(RPCAUTHGSS_SVCNONE);
3925 } else if (retnes.nes_secflavors[i] == RPCSEC_GSS_KRB5I) {
3926 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
3927 *tl = txdr_unsigned(RPCAUTH_GSS);
3928 nfsm_strtom(nd, nfsgss_mechlist[KERBV_MECH].str,
3929 nfsgss_mechlist[KERBV_MECH].len);
3930 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_UNSIGNED);
3931 *tl++ = txdr_unsigned(GSS_KERBV_QOP);
3932 *tl = txdr_unsigned(RPCAUTHGSS_SVCINTEGRITY);
3934 } else if (retnes.nes_secflavors[i] == RPCSEC_GSS_KRB5P) {
3935 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
3936 *tl = txdr_unsigned(RPCAUTH_GSS);
3937 nfsm_strtom(nd, nfsgss_mechlist[KERBV_MECH].str,
3938 nfsgss_mechlist[KERBV_MECH].len);
3939 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_UNSIGNED);
3940 *tl++ = txdr_unsigned(GSS_KERBV_QOP);
3941 *tl = txdr_unsigned(RPCAUTHGSS_SVCPRIVACY);
3945 *sizp = txdr_unsigned(len);
3948 NFSEXITCODE2(error, nd);
3953 * nfsv4 set client id service
3956 nfsrvd_setclientid(struct nfsrv_descript *nd, __unused int isdgram,
3957 __unused vnode_t vp, __unused struct nfsexstuff *exp)
3961 int error = 0, idlen;
3962 struct nfsclient *clp = NULL;
3964 struct sockaddr_in *rin;
3967 struct sockaddr_in6 *rin6;
3969 #if defined(INET) || defined(INET6)
3972 u_char *verf, *addrbuf;
3973 nfsquad_t clientid, confirm;
3974 struct thread *p = curthread;
3976 if ((nd->nd_flag & ND_NFSV41) != 0) {
3977 nd->nd_repstat = NFSERR_NOTSUPP;
3980 if ((nd->nd_repstat = nfsd_checkrootexp(nd)) != 0)
3982 NFSM_DISSECT(tl, u_int32_t *, NFSX_VERF + NFSX_UNSIGNED);
3983 verf = (u_char *)tl;
3984 tl += (NFSX_VERF / NFSX_UNSIGNED);
3985 i = fxdr_unsigned(int, *tl);
3986 if (i > NFSV4_OPAQUELIMIT || i <= 0) {
3987 nd->nd_repstat = NFSERR_BADXDR;
3991 if (nd->nd_flag & ND_GSS)
3992 i += nd->nd_princlen;
3993 clp = malloc(sizeof(struct nfsclient) + i, M_NFSDCLIENT, M_WAITOK |
3995 clp->lc_stateid = malloc(sizeof(struct nfsstatehead) *
3996 nfsrv_statehashsize, M_NFSDCLIENT, M_WAITOK);
3997 NFSINITSOCKMUTEX(&clp->lc_req.nr_mtx);
3998 /* Allocated large enough for an AF_INET or AF_INET6 socket. */
3999 clp->lc_req.nr_nam = malloc(sizeof(struct sockaddr_in6), M_SONAME,
4001 clp->lc_req.nr_cred = NULL;
4002 NFSBCOPY(verf, clp->lc_verf, NFSX_VERF);
4003 clp->lc_idlen = idlen;
4004 error = nfsrv_mtostr(nd, clp->lc_id, idlen);
4007 if (nd->nd_flag & ND_GSS) {
4008 clp->lc_flags = LCL_GSS;
4009 if (nd->nd_flag & ND_GSSINTEGRITY)
4010 clp->lc_flags |= LCL_GSSINTEGRITY;
4011 else if (nd->nd_flag & ND_GSSPRIVACY)
4012 clp->lc_flags |= LCL_GSSPRIVACY;
4016 if ((nd->nd_flag & ND_GSS) && nd->nd_princlen > 0) {
4017 clp->lc_flags |= LCL_NAME;
4018 clp->lc_namelen = nd->nd_princlen;
4019 clp->lc_name = &clp->lc_id[idlen];
4020 NFSBCOPY(nd->nd_principal, clp->lc_name, clp->lc_namelen);
4022 clp->lc_uid = nd->nd_cred->cr_uid;
4023 clp->lc_gid = nd->nd_cred->cr_gid;
4026 /* If the client is using TLS, do so for the callback connection. */
4027 if (nd->nd_flag & ND_TLS)
4028 clp->lc_flags |= LCL_TLSCB;
4030 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
4031 clp->lc_program = fxdr_unsigned(u_int32_t, *tl);
4032 error = nfsrv_getclientipaddr(nd, clp);
4035 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
4036 clp->lc_callback = fxdr_unsigned(u_int32_t, *tl);
4039 * nfsrv_setclient() does the actual work of adding it to the
4040 * client list. If there is no error, the structure has been
4041 * linked into the client list and clp should no longer be used
4042 * here. When an error is returned, it has not been linked in,
4043 * so it should be free'd.
4045 nd->nd_repstat = nfsrv_setclient(nd, &clp, &clientid, &confirm, p);
4046 if (nd->nd_repstat == NFSERR_CLIDINUSE) {
4048 * 8 is the maximum length of the port# string.
4050 addrbuf = malloc(INET6_ADDRSTRLEN + 8, M_TEMP, M_WAITOK);
4051 switch (clp->lc_req.nr_nam->sa_family) {
4054 if (clp->lc_flags & LCL_TCPCALLBACK)
4055 (void) nfsm_strtom(nd, "tcp", 3);
4057 (void) nfsm_strtom(nd, "udp", 3);
4058 rin = (struct sockaddr_in *)clp->lc_req.nr_nam;
4059 ucp = (u_char *)&rin->sin_addr.s_addr;
4060 ucp2 = (u_char *)&rin->sin_port;
4061 sprintf(addrbuf, "%d.%d.%d.%d.%d.%d", ucp[0] & 0xff,
4062 ucp[1] & 0xff, ucp[2] & 0xff, ucp[3] & 0xff,
4063 ucp2[0] & 0xff, ucp2[1] & 0xff);
4068 if (clp->lc_flags & LCL_TCPCALLBACK)
4069 (void) nfsm_strtom(nd, "tcp6", 4);
4071 (void) nfsm_strtom(nd, "udp6", 4);
4072 rin6 = (struct sockaddr_in6 *)clp->lc_req.nr_nam;
4073 ucp = inet_ntop(AF_INET6, &rin6->sin6_addr, addrbuf,
4079 ucp2 = (u_char *)&rin6->sin6_port;
4080 sprintf(&addrbuf[i], ".%d.%d", ucp2[0] & 0xff,
4085 (void) nfsm_strtom(nd, addrbuf, strlen(addrbuf));
4086 free(addrbuf, M_TEMP);
4089 free(clp->lc_req.nr_nam, M_SONAME);
4090 NFSFREEMUTEX(&clp->lc_req.nr_mtx);
4091 free(clp->lc_stateid, M_NFSDCLIENT);
4092 free(clp, M_NFSDCLIENT);
4094 if (!nd->nd_repstat) {
4095 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_HYPER);
4096 *tl++ = clientid.lval[0];
4097 *tl++ = clientid.lval[1];
4098 *tl++ = confirm.lval[0];
4099 *tl = confirm.lval[1];
4103 NFSEXITCODE2(0, nd);
4107 free(clp->lc_req.nr_nam, M_SONAME);
4108 NFSFREEMUTEX(&clp->lc_req.nr_mtx);
4109 free(clp->lc_stateid, M_NFSDCLIENT);
4110 free(clp, M_NFSDCLIENT);
4112 NFSEXITCODE2(error, nd);
4117 * nfsv4 set client id confirm service
4120 nfsrvd_setclientidcfrm(struct nfsrv_descript *nd,
4121 __unused int isdgram, __unused vnode_t vp,
4122 __unused struct nfsexstuff *exp)
4126 nfsquad_t clientid, confirm;
4127 struct thread *p = curthread;
4129 if ((nd->nd_flag & ND_NFSV41) != 0) {
4130 nd->nd_repstat = NFSERR_NOTSUPP;
4133 if ((nd->nd_repstat = nfsd_checkrootexp(nd)) != 0)
4135 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_HYPER);
4136 clientid.lval[0] = *tl++;
4137 clientid.lval[1] = *tl++;
4138 confirm.lval[0] = *tl++;
4139 confirm.lval[1] = *tl;
4142 * nfsrv_getclient() searches the client list for a match and
4143 * returns the appropriate NFSERR status.
4145 nd->nd_repstat = nfsrv_getclient(clientid, (CLOPS_CONFIRM|CLOPS_RENEW),
4146 NULL, NULL, confirm, 0, nd, p);
4148 NFSEXITCODE2(error, nd);
4153 * nfsv4 verify service
4156 nfsrvd_verify(struct nfsrv_descript *nd, int isdgram,
4157 vnode_t vp, __unused struct nfsexstuff *exp)
4159 int error = 0, ret, fhsize = NFSX_MYFH;
4160 struct nfsvattr nva;
4162 struct nfsfsinfo fs;
4164 struct thread *p = curthread;
4166 sf = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
4167 nd->nd_repstat = nfsvno_getattr(vp, &nva, nd, p, 1, NULL);
4168 if (!nd->nd_repstat)
4169 nd->nd_repstat = nfsvno_statfs(vp, sf);
4170 if (!nd->nd_repstat)
4171 nd->nd_repstat = nfsvno_getfh(vp, &fh, p);
4172 if (!nd->nd_repstat) {
4173 nfsvno_getfs(&fs, isdgram);
4174 error = nfsv4_loadattr(nd, vp, &nva, NULL, &fh, fhsize, NULL,
4175 sf, NULL, &fs, NULL, 1, &ret, NULL, NULL, p, nd->nd_cred);
4177 if (nd->nd_procnum == NFSV4OP_NVERIFY) {
4179 nd->nd_repstat = NFSERR_SAME;
4180 else if (ret != NFSERR_NOTSAME)
4181 nd->nd_repstat = ret;
4183 nd->nd_repstat = ret;
4188 NFSEXITCODE2(error, nd);
4196 nfsrvd_openattr(struct nfsrv_descript *nd, __unused int isdgram,
4197 vnode_t dp, __unused vnode_t *vpp, __unused fhandle_t *fhp,
4198 __unused struct nfsexstuff *exp)
4201 int error = 0, createdir __unused;
4203 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
4204 createdir = fxdr_unsigned(int, *tl);
4205 nd->nd_repstat = NFSERR_NOTSUPP;
4208 NFSEXITCODE2(error, nd);
4213 * nfsv4 release lock owner service
4216 nfsrvd_releaselckown(struct nfsrv_descript *nd, __unused int isdgram,
4217 __unused vnode_t vp, __unused struct nfsexstuff *exp)
4220 struct nfsstate *stp = NULL;
4223 struct thread *p = curthread;
4225 if ((nd->nd_flag & ND_NFSV41) != 0) {
4226 nd->nd_repstat = NFSERR_NOTSUPP;
4229 if ((nd->nd_repstat = nfsd_checkrootexp(nd)) != 0)
4231 NFSM_DISSECT(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
4232 len = fxdr_unsigned(int, *(tl + 2));
4233 if (len <= 0 || len > NFSV4_OPAQUELIMIT) {
4234 nd->nd_repstat = NFSERR_BADXDR;
4237 stp = malloc(sizeof (struct nfsstate) + len,
4238 M_NFSDSTATE, M_WAITOK);
4239 stp->ls_ownerlen = len;
4241 stp->ls_flags = NFSLCK_RELEASE;
4242 stp->ls_uid = nd->nd_cred->cr_uid;
4243 clientid.lval[0] = *tl++;
4244 clientid.lval[1] = *tl;
4245 if ((nd->nd_flag & ND_IMPLIEDCLID) != 0) {
4246 if ((nd->nd_flag & ND_NFSV41) != 0)
4247 clientid.qval = nd->nd_clientid.qval;
4248 else if (nd->nd_clientid.qval != clientid.qval)
4249 printf("EEK14 multiple clids\n");
4251 if ((nd->nd_flag & ND_NFSV41) != 0)
4252 printf("EEK! no clientid from session\n");
4253 nd->nd_flag |= ND_IMPLIEDCLID;
4254 nd->nd_clientid.qval = clientid.qval;
4256 error = nfsrv_mtostr(nd, stp->ls_owner, len);
4259 nd->nd_repstat = nfsrv_releaselckown(stp, clientid, p);
4260 free(stp, M_NFSDSTATE);
4262 NFSEXITCODE2(0, nd);
4266 free(stp, M_NFSDSTATE);
4267 NFSEXITCODE2(error, nd);
4272 * nfsv4 exchange_id service
4275 nfsrvd_exchangeid(struct nfsrv_descript *nd, __unused int isdgram,
4276 __unused vnode_t vp, __unused struct nfsexstuff *exp)
4279 int error = 0, i, idlen;
4280 struct nfsclient *clp = NULL;
4281 nfsquad_t clientid, confirm;
4283 uint32_t sp4type, v41flags;
4284 struct timespec verstime;
4286 struct sockaddr_in *sin, *rin;
4289 struct sockaddr_in6 *sin6, *rin6;
4291 struct thread *p = curthread;
4294 if ((nd->nd_repstat = nfsd_checkrootexp(nd)) != 0)
4296 NFSM_DISSECT(tl, u_int32_t *, NFSX_VERF + NFSX_UNSIGNED);
4297 verf = (uint8_t *)tl;
4298 tl += (NFSX_VERF / NFSX_UNSIGNED);
4299 i = fxdr_unsigned(int, *tl);
4300 if (i > NFSV4_OPAQUELIMIT || i <= 0) {
4301 nd->nd_repstat = NFSERR_BADXDR;
4305 if (nd->nd_flag & ND_GSS)
4306 i += nd->nd_princlen;
4307 clp = malloc(sizeof(struct nfsclient) + i, M_NFSDCLIENT, M_WAITOK |
4309 clp->lc_stateid = malloc(sizeof(struct nfsstatehead) *
4310 nfsrv_statehashsize, M_NFSDCLIENT, M_WAITOK);
4311 NFSINITSOCKMUTEX(&clp->lc_req.nr_mtx);
4312 /* Allocated large enough for an AF_INET or AF_INET6 socket. */
4313 clp->lc_req.nr_nam = malloc(sizeof(struct sockaddr_in6), M_SONAME,
4315 switch (nd->nd_nam->sa_family) {
4318 rin = (struct sockaddr_in *)clp->lc_req.nr_nam;
4319 sin = (struct sockaddr_in *)nd->nd_nam;
4320 rin->sin_family = AF_INET;
4321 rin->sin_len = sizeof(struct sockaddr_in);
4323 rin->sin_addr.s_addr = sin->sin_addr.s_addr;
4328 rin6 = (struct sockaddr_in6 *)clp->lc_req.nr_nam;
4329 sin6 = (struct sockaddr_in6 *)nd->nd_nam;
4330 rin6->sin6_family = AF_INET6;
4331 rin6->sin6_len = sizeof(struct sockaddr_in6);
4332 rin6->sin6_port = 0;
4333 rin6->sin6_addr = sin6->sin6_addr;
4337 clp->lc_req.nr_cred = NULL;
4338 NFSBCOPY(verf, clp->lc_verf, NFSX_VERF);
4339 clp->lc_idlen = idlen;
4340 error = nfsrv_mtostr(nd, clp->lc_id, idlen);
4343 if ((nd->nd_flag & ND_GSS) != 0) {
4344 clp->lc_flags = LCL_GSS | LCL_NFSV41;
4345 if ((nd->nd_flag & ND_GSSINTEGRITY) != 0)
4346 clp->lc_flags |= LCL_GSSINTEGRITY;
4347 else if ((nd->nd_flag & ND_GSSPRIVACY) != 0)
4348 clp->lc_flags |= LCL_GSSPRIVACY;
4350 clp->lc_flags = LCL_NFSV41;
4351 if ((nd->nd_flag & ND_NFSV42) != 0)
4352 clp->lc_flags |= LCL_NFSV42;
4353 if ((nd->nd_flag & ND_GSS) != 0 && nd->nd_princlen > 0) {
4354 clp->lc_flags |= LCL_NAME;
4355 clp->lc_namelen = nd->nd_princlen;
4356 clp->lc_name = &clp->lc_id[idlen];
4357 NFSBCOPY(nd->nd_principal, clp->lc_name, clp->lc_namelen);
4359 clp->lc_uid = nd->nd_cred->cr_uid;
4360 clp->lc_gid = nd->nd_cred->cr_gid;
4362 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
4363 v41flags = fxdr_unsigned(uint32_t, *tl++);
4364 if ((v41flags & ~(NFSV4EXCH_SUPPMOVEDREFER | NFSV4EXCH_SUPPMOVEDMIGR |
4365 NFSV4EXCH_BINDPRINCSTATEID | NFSV4EXCH_MASKPNFS |
4366 NFSV4EXCH_UPDCONFIRMEDRECA)) != 0) {
4367 nd->nd_repstat = NFSERR_INVAL;
4370 if ((v41flags & NFSV4EXCH_UPDCONFIRMEDRECA) != 0)
4371 confirm.lval[1] = 1;
4373 confirm.lval[1] = 0;
4374 if (nfsrv_devidcnt == 0)
4375 v41flags = NFSV4EXCH_USENONPNFS | NFSV4EXCH_USEPNFSDS;
4377 v41flags = NFSV4EXCH_USEPNFSMDS;
4378 sp4type = fxdr_unsigned(uint32_t, *tl);
4379 if (sp4type != NFSV4EXCH_SP4NONE) {
4380 nd->nd_repstat = NFSERR_NOTSUPP;
4385 * nfsrv_setclient() does the actual work of adding it to the
4386 * client list. If there is no error, the structure has been
4387 * linked into the client list and clp should no longer be used
4388 * here. When an error is returned, it has not been linked in,
4389 * so it should be free'd.
4391 nd->nd_repstat = nfsrv_setclient(nd, &clp, &clientid, &confirm, p);
4393 free(clp->lc_req.nr_nam, M_SONAME);
4394 NFSFREEMUTEX(&clp->lc_req.nr_mtx);
4395 free(clp->lc_stateid, M_NFSDCLIENT);
4396 free(clp, M_NFSDCLIENT);
4398 if (nd->nd_repstat == 0) {
4399 if (confirm.lval[1] != 0)
4400 v41flags |= NFSV4EXCH_CONFIRMEDR;
4401 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_HYPER + 3 * NFSX_UNSIGNED);
4402 *tl++ = clientid.lval[0]; /* ClientID */
4403 *tl++ = clientid.lval[1];
4404 *tl++ = txdr_unsigned(confirm.lval[0]); /* SequenceID */
4405 *tl++ = txdr_unsigned(v41flags); /* Exch flags */
4406 *tl++ = txdr_unsigned(NFSV4EXCH_SP4NONE); /* No SSV */
4407 txdr_hyper(nfsrv_owner_minor, tl); /* Owner Minor */
4408 if (nfsrv_owner_major[0] != 0)
4409 s = nfsrv_owner_major;
4411 s = nd->nd_cred->cr_prison->pr_hostuuid;
4412 nfsm_strtom(nd, s, strlen(s)); /* Owner Major */
4413 if (nfsrv_scope[0] != 0)
4416 s = nd->nd_cred->cr_prison->pr_hostuuid;
4417 nfsm_strtom(nd, s, strlen(s) ); /* Scope */
4418 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
4419 *tl = txdr_unsigned(1);
4420 (void)nfsm_strtom(nd, "freebsd.org", strlen("freebsd.org"));
4421 (void)nfsm_strtom(nd, version, strlen(version));
4422 NFSM_BUILD(tl, uint32_t *, NFSX_V4TIME);
4423 verstime.tv_sec = 1293840000; /* Jan 1, 2011 */
4424 verstime.tv_nsec = 0;
4425 txdr_nfsv4time(&verstime, tl);
4427 NFSEXITCODE2(0, nd);
4431 free(clp->lc_req.nr_nam, M_SONAME);
4432 NFSFREEMUTEX(&clp->lc_req.nr_mtx);
4433 free(clp->lc_stateid, M_NFSDCLIENT);
4434 free(clp, M_NFSDCLIENT);
4436 NFSEXITCODE2(error, nd);
4441 * nfsv4 create session service
4444 nfsrvd_createsession(struct nfsrv_descript *nd, __unused int isdgram,
4445 __unused vnode_t vp, __unused struct nfsexstuff *exp)
4449 nfsquad_t clientid, confirm;
4450 struct nfsdsession *sep = NULL;
4452 struct thread *p = curthread;
4453 static bool do_printf = true;
4455 if ((nd->nd_repstat = nfsd_checkrootexp(nd)) != 0)
4457 sep = (struct nfsdsession *)malloc(sizeof(struct nfsdsession),
4458 M_NFSDSESSION, M_WAITOK | M_ZERO);
4459 sep->sess_refcnt = 1;
4460 mtx_init(&sep->sess_cbsess.nfsess_mtx, "nfscbsession", NULL, MTX_DEF);
4461 NFSM_DISSECT(tl, uint32_t *, NFSX_HYPER + 2 * NFSX_UNSIGNED);
4462 clientid.lval[0] = *tl++;
4463 clientid.lval[1] = *tl++;
4464 confirm.lval[0] = fxdr_unsigned(uint32_t, *tl++);
4465 sep->sess_crflags = fxdr_unsigned(uint32_t, *tl);
4466 /* Persistent sessions and RDMA are not supported. */
4467 sep->sess_crflags &= NFSV4CRSESS_CONNBACKCHAN;
4469 /* Fore channel attributes. */
4470 NFSM_DISSECT(tl, uint32_t *, 7 * NFSX_UNSIGNED);
4471 tl++; /* Header pad always 0. */
4472 sep->sess_maxreq = fxdr_unsigned(uint32_t, *tl++);
4473 if (sep->sess_maxreq > sb_max_adj - NFS_MAXXDR) {
4474 sep->sess_maxreq = sb_max_adj - NFS_MAXXDR;
4476 printf("Consider increasing kern.ipc.maxsockbuf\n");
4479 sep->sess_maxresp = fxdr_unsigned(uint32_t, *tl++);
4480 if (sep->sess_maxresp > sb_max_adj - NFS_MAXXDR) {
4481 sep->sess_maxresp = sb_max_adj - NFS_MAXXDR;
4483 printf("Consider increasing kern.ipc.maxsockbuf\n");
4486 sep->sess_maxrespcached = fxdr_unsigned(uint32_t, *tl++);
4487 sep->sess_maxops = fxdr_unsigned(uint32_t, *tl++);
4488 sep->sess_maxslots = fxdr_unsigned(uint32_t, *tl++);
4489 if (sep->sess_maxslots > NFSV4_SLOTS)
4490 sep->sess_maxslots = NFSV4_SLOTS;
4491 rdmacnt = fxdr_unsigned(uint32_t, *tl);
4493 nd->nd_repstat = NFSERR_BADXDR;
4495 } else if (rdmacnt == 1)
4496 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
4498 /* Back channel attributes. */
4499 NFSM_DISSECT(tl, uint32_t *, 7 * NFSX_UNSIGNED);
4500 tl++; /* Header pad always 0. */
4501 sep->sess_cbmaxreq = fxdr_unsigned(uint32_t, *tl++);
4502 sep->sess_cbmaxresp = fxdr_unsigned(uint32_t, *tl++);
4503 sep->sess_cbmaxrespcached = fxdr_unsigned(uint32_t, *tl++);
4504 sep->sess_cbmaxops = fxdr_unsigned(uint32_t, *tl++);
4505 sep->sess_cbsess.nfsess_foreslots = fxdr_unsigned(uint32_t, *tl++);
4506 rdmacnt = fxdr_unsigned(uint32_t, *tl);
4508 nd->nd_repstat = NFSERR_BADXDR;
4510 } else if (rdmacnt == 1)
4511 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
4513 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
4514 sep->sess_cbprogram = fxdr_unsigned(uint32_t, *tl);
4517 * nfsrv_getclient() searches the client list for a match and
4518 * returns the appropriate NFSERR status.
4520 nd->nd_repstat = nfsrv_getclient(clientid, CLOPS_CONFIRM | CLOPS_RENEW,
4521 NULL, sep, confirm, sep->sess_cbprogram, nd, p);
4522 if (nd->nd_repstat == 0) {
4523 NFSM_BUILD(tl, uint32_t *, NFSX_V4SESSIONID);
4524 NFSBCOPY(sep->sess_sessionid, tl, NFSX_V4SESSIONID);
4525 NFSM_BUILD(tl, uint32_t *, 18 * NFSX_UNSIGNED);
4526 *tl++ = txdr_unsigned(confirm.lval[0]); /* sequenceid */
4527 *tl++ = txdr_unsigned(sep->sess_crflags);
4529 /* Fore channel attributes. */
4531 *tl++ = txdr_unsigned(sep->sess_maxreq);
4532 *tl++ = txdr_unsigned(sep->sess_maxresp);
4533 *tl++ = txdr_unsigned(sep->sess_maxrespcached);
4534 *tl++ = txdr_unsigned(sep->sess_maxops);
4535 *tl++ = txdr_unsigned(sep->sess_maxslots);
4536 *tl++ = txdr_unsigned(1);
4537 *tl++ = txdr_unsigned(0); /* No RDMA. */
4539 /* Back channel attributes. */
4541 *tl++ = txdr_unsigned(sep->sess_cbmaxreq);
4542 *tl++ = txdr_unsigned(sep->sess_cbmaxresp);
4543 *tl++ = txdr_unsigned(sep->sess_cbmaxrespcached);
4544 *tl++ = txdr_unsigned(sep->sess_cbmaxops);
4545 *tl++ = txdr_unsigned(sep->sess_cbsess.nfsess_foreslots);
4546 *tl++ = txdr_unsigned(1);
4547 *tl = txdr_unsigned(0); /* No RDMA. */
4550 if (nd->nd_repstat != 0 && sep != NULL)
4551 free(sep, M_NFSDSESSION);
4552 NFSEXITCODE2(error, nd);
4557 * nfsv4 sequence service
4560 nfsrvd_sequence(struct nfsrv_descript *nd, __unused int isdgram,
4561 __unused vnode_t vp, __unused struct nfsexstuff *exp)
4564 uint32_t highest_slotid, sequenceid, sflags, target_highest_slotid;
4565 int cache_this, error = 0;
4566 struct thread *p = curthread;
4568 if ((nd->nd_repstat = nfsd_checkrootexp(nd)) != 0)
4570 NFSM_DISSECT(tl, uint32_t *, NFSX_V4SESSIONID);
4571 NFSBCOPY(tl, nd->nd_sessionid, NFSX_V4SESSIONID);
4572 NFSM_DISSECT(tl, uint32_t *, 4 * NFSX_UNSIGNED);
4573 sequenceid = fxdr_unsigned(uint32_t, *tl++);
4574 nd->nd_slotid = fxdr_unsigned(uint32_t, *tl++);
4575 highest_slotid = fxdr_unsigned(uint32_t, *tl++);
4576 if (*tl == newnfs_true)
4580 nd->nd_repstat = nfsrv_checksequence(nd, sequenceid, &highest_slotid,
4581 &target_highest_slotid, cache_this, &sflags, p);
4582 if (nd->nd_repstat != NFSERR_BADSLOT)
4583 nd->nd_flag |= ND_HASSEQUENCE;
4584 if (nd->nd_repstat == 0) {
4585 NFSM_BUILD(tl, uint32_t *, NFSX_V4SESSIONID);
4586 NFSBCOPY(nd->nd_sessionid, tl, NFSX_V4SESSIONID);
4587 NFSM_BUILD(tl, uint32_t *, 5 * NFSX_UNSIGNED);
4588 *tl++ = txdr_unsigned(sequenceid);
4589 *tl++ = txdr_unsigned(nd->nd_slotid);
4590 *tl++ = txdr_unsigned(highest_slotid);
4591 *tl++ = txdr_unsigned(target_highest_slotid);
4592 *tl = txdr_unsigned(sflags);
4595 NFSEXITCODE2(error, nd);
4600 * nfsv4 reclaim complete service
4603 nfsrvd_reclaimcomplete(struct nfsrv_descript *nd, __unused int isdgram,
4604 __unused vnode_t vp, __unused struct nfsexstuff *exp)
4607 int error = 0, onefs;
4609 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
4611 * I believe that a ReclaimComplete with rca_one_fs == TRUE is only
4612 * to be used after a file system has been transferred to a different
4613 * file server. However, RFC5661 is somewhat vague w.r.t. this and
4614 * the ESXi 6.7 client does both a ReclaimComplete with rca_one_fs
4615 * == TRUE and one with ReclaimComplete with rca_one_fs == FALSE.
4616 * Therefore, just ignore the rca_one_fs == TRUE operation and return
4617 * NFS_OK without doing anything.
4620 if (*tl == newnfs_true)
4622 nd->nd_repstat = nfsrv_checkreclaimcomplete(nd, onefs);
4624 NFSEXITCODE2(error, nd);
4629 * nfsv4 destroy clientid service
4632 nfsrvd_destroyclientid(struct nfsrv_descript *nd, __unused int isdgram,
4633 __unused vnode_t vp, __unused struct nfsexstuff *exp)
4638 struct thread *p = curthread;
4640 if ((nd->nd_repstat = nfsd_checkrootexp(nd)) != 0)
4642 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
4643 clientid.lval[0] = *tl++;
4644 clientid.lval[1] = *tl;
4645 nd->nd_repstat = nfsrv_destroyclient(clientid, p);
4647 NFSEXITCODE2(error, nd);
4652 * nfsv4 bind connection to session service
4655 nfsrvd_bindconnsess(struct nfsrv_descript *nd, __unused int isdgram,
4656 __unused vnode_t vp, __unused struct nfsexstuff *exp)
4659 uint8_t sessid[NFSX_V4SESSIONID];
4660 int error = 0, foreaft;
4662 if ((nd->nd_repstat = nfsd_checkrootexp(nd)) != 0)
4664 NFSM_DISSECT(tl, uint32_t *, NFSX_V4SESSIONID + 2 * NFSX_UNSIGNED);
4665 NFSBCOPY(tl, sessid, NFSX_V4SESSIONID);
4666 tl += (NFSX_V4SESSIONID / NFSX_UNSIGNED);
4667 foreaft = fxdr_unsigned(int, *tl++);
4668 if (*tl == newnfs_true) {
4669 /* RDMA is not supported. */
4670 nd->nd_repstat = NFSERR_NOTSUPP;
4674 nd->nd_repstat = nfsrv_bindconnsess(nd, sessid, &foreaft);
4675 if (nd->nd_repstat == 0) {
4676 NFSM_BUILD(tl, uint32_t *, NFSX_V4SESSIONID + 2 *
4678 NFSBCOPY(sessid, tl, NFSX_V4SESSIONID);
4679 tl += (NFSX_V4SESSIONID / NFSX_UNSIGNED);
4680 *tl++ = txdr_unsigned(foreaft);
4684 NFSEXITCODE2(error, nd);
4689 * nfsv4 destroy session service
4692 nfsrvd_destroysession(struct nfsrv_descript *nd, __unused int isdgram,
4693 __unused vnode_t vp, __unused struct nfsexstuff *exp)
4695 uint8_t *cp, sessid[NFSX_V4SESSIONID];
4698 if ((nd->nd_repstat = nfsd_checkrootexp(nd)) != 0)
4700 NFSM_DISSECT(cp, uint8_t *, NFSX_V4SESSIONID);
4701 NFSBCOPY(cp, sessid, NFSX_V4SESSIONID);
4702 nd->nd_repstat = nfsrv_destroysession(nd, sessid);
4704 NFSEXITCODE2(error, nd);
4709 * nfsv4 free stateid service
4712 nfsrvd_freestateid(struct nfsrv_descript *nd, __unused int isdgram,
4713 __unused vnode_t vp, __unused struct nfsexstuff *exp)
4716 nfsv4stateid_t stateid;
4718 struct thread *p = curthread;
4720 NFSM_DISSECT(tl, uint32_t *, NFSX_STATEID);
4721 stateid.seqid = fxdr_unsigned(uint32_t, *tl++);
4722 NFSBCOPY(tl, stateid.other, NFSX_STATEIDOTHER);
4725 * For the special stateid of other all 0s and seqid == 1, set the
4726 * stateid to the current stateid, if it is set.
4728 if (stateid.seqid == 1 && stateid.other[0] == 0 &&
4729 stateid.other[1] == 0 && stateid.other[2] == 0) {
4730 if ((nd->nd_flag & ND_CURSTATEID) != 0) {
4731 stateid = nd->nd_curstateid;
4734 nd->nd_repstat = NFSERR_BADSTATEID;
4739 nd->nd_repstat = nfsrv_freestateid(nd, &stateid, p);
4741 /* If the current stateid has been free'd, unset it. */
4742 if (nd->nd_repstat == 0 && (nd->nd_flag & ND_CURSTATEID) != 0 &&
4743 stateid.other[0] == nd->nd_curstateid.other[0] &&
4744 stateid.other[1] == nd->nd_curstateid.other[1] &&
4745 stateid.other[2] == nd->nd_curstateid.other[2])
4746 nd->nd_flag &= ~ND_CURSTATEID;
4748 NFSEXITCODE2(error, nd);
4753 * nfsv4 layoutget service
4756 nfsrvd_layoutget(struct nfsrv_descript *nd, __unused int isdgram,
4757 vnode_t vp, struct nfsexstuff *exp)
4760 nfsv4stateid_t stateid;
4761 int error = 0, layoutlen, layouttype, iomode, maxcnt, retonclose;
4762 uint64_t offset, len, minlen;
4764 struct thread *p = curthread;
4766 NFSM_DISSECT(tl, uint32_t *, 4 * NFSX_UNSIGNED + 3 * NFSX_HYPER +
4768 tl++; /* Signal layout available. Ignore for now. */
4769 layouttype = fxdr_unsigned(int, *tl++);
4770 iomode = fxdr_unsigned(int, *tl++);
4771 offset = fxdr_hyper(tl); tl += 2;
4772 len = fxdr_hyper(tl); tl += 2;
4773 minlen = fxdr_hyper(tl); tl += 2;
4774 stateid.seqid = fxdr_unsigned(uint32_t, *tl++);
4775 NFSBCOPY(tl, stateid.other, NFSX_STATEIDOTHER);
4776 tl += (NFSX_STATEIDOTHER / NFSX_UNSIGNED);
4777 maxcnt = fxdr_unsigned(int, *tl);
4778 NFSD_DEBUG(4, "layoutget ltyp=%d iom=%d off=%ju len=%ju mlen=%ju\n",
4779 layouttype, iomode, (uintmax_t)offset, (uintmax_t)len,
4782 (minlen != UINT64_MAX && offset + minlen < offset) ||
4783 (len != UINT64_MAX && offset + len < offset)) {
4784 nd->nd_repstat = NFSERR_INVAL;
4789 * For the special stateid of other all 0s and seqid == 1, set the
4790 * stateid to the current stateid, if it is set.
4792 if (stateid.seqid == 1 && stateid.other[0] == 0 &&
4793 stateid.other[1] == 0 && stateid.other[2] == 0) {
4794 if ((nd->nd_flag & ND_CURSTATEID) != 0) {
4795 stateid = nd->nd_curstateid;
4798 nd->nd_repstat = NFSERR_BADSTATEID;
4804 if (layouttype == NFSLAYOUT_NFSV4_1_FILES && nfsrv_maxpnfsmirror == 1)
4805 layp = malloc(NFSX_V4FILELAYOUT, M_TEMP, M_WAITOK);
4806 else if (layouttype == NFSLAYOUT_FLEXFILE)
4807 layp = malloc(NFSX_V4FLEXLAYOUT(nfsrv_maxpnfsmirror), M_TEMP,
4810 nd->nd_repstat = NFSERR_UNKNLAYOUTTYPE;
4812 nd->nd_repstat = nfsrv_layoutget(nd, vp, exp, layouttype,
4813 &iomode, &offset, &len, minlen, &stateid, maxcnt,
4814 &retonclose, &layoutlen, layp, nd->nd_cred, p);
4815 NFSD_DEBUG(4, "nfsrv_layoutget stat=%u layoutlen=%d\n", nd->nd_repstat,
4817 if (nd->nd_repstat == 0) {
4818 /* For NFSv4.1, set the Current StateID. */
4819 if ((nd->nd_flag & ND_NFSV41) != 0) {
4820 nd->nd_curstateid = stateid;
4821 nd->nd_flag |= ND_CURSTATEID;
4823 NFSM_BUILD(tl, uint32_t *, 4 * NFSX_UNSIGNED + NFSX_STATEID +
4825 *tl++ = txdr_unsigned(retonclose);
4826 *tl++ = txdr_unsigned(stateid.seqid);
4827 NFSBCOPY(stateid.other, tl, NFSX_STATEIDOTHER);
4828 tl += (NFSX_STATEIDOTHER / NFSX_UNSIGNED);
4829 *tl++ = txdr_unsigned(1); /* Only returns one layout. */
4830 txdr_hyper(offset, tl); tl += 2;
4831 txdr_hyper(len, tl); tl += 2;
4832 *tl++ = txdr_unsigned(iomode);
4833 *tl = txdr_unsigned(layouttype);
4834 nfsm_strtom(nd, layp, layoutlen);
4835 } else if (nd->nd_repstat == NFSERR_LAYOUTTRYLATER) {
4836 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
4842 NFSEXITCODE2(error, nd);
4847 * nfsv4 layoutcommit service
4850 nfsrvd_layoutcommit(struct nfsrv_descript *nd, __unused int isdgram,
4851 vnode_t vp, struct nfsexstuff *exp)
4854 nfsv4stateid_t stateid;
4855 int error = 0, hasnewoff, hasnewmtime, layouttype, maxcnt, reclaim;
4857 uint64_t offset, len, newoff = 0, newsize;
4858 struct timespec newmtime;
4860 struct thread *p = curthread;
4863 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED + 2 * NFSX_HYPER +
4865 offset = fxdr_hyper(tl); tl += 2;
4866 len = fxdr_hyper(tl); tl += 2;
4867 reclaim = fxdr_unsigned(int, *tl++);
4868 stateid.seqid = fxdr_unsigned(uint32_t, *tl++);
4869 NFSBCOPY(tl, stateid.other, NFSX_STATEIDOTHER);
4870 tl += (NFSX_STATEIDOTHER / NFSX_UNSIGNED);
4872 * For the special stateid of other all 0s and seqid == 1, set the
4873 * stateid to the current stateid, if it is set.
4875 if (stateid.seqid == 1 && stateid.other[0] == 0 &&
4876 stateid.other[1] == 0 && stateid.other[2] == 0) {
4877 if ((nd->nd_flag & ND_CURSTATEID) != 0) {
4878 stateid = nd->nd_curstateid;
4881 nd->nd_repstat = NFSERR_BADSTATEID;
4886 hasnewoff = fxdr_unsigned(int, *tl);
4887 if (hasnewoff != 0) {
4888 NFSM_DISSECT(tl, uint32_t *, NFSX_HYPER + NFSX_UNSIGNED);
4889 newoff = fxdr_hyper(tl); tl += 2;
4891 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
4892 hasnewmtime = fxdr_unsigned(int, *tl);
4893 if (hasnewmtime != 0) {
4894 NFSM_DISSECT(tl, uint32_t *, NFSX_V4TIME + 2 * NFSX_UNSIGNED);
4895 fxdr_nfsv4time(tl, &newmtime);
4896 tl += (NFSX_V4TIME / NFSX_UNSIGNED);
4898 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
4899 layouttype = fxdr_unsigned(int, *tl++);
4900 maxcnt = fxdr_unsigned(int, *tl);
4902 layp = malloc(maxcnt + 1, M_TEMP, M_WAITOK);
4903 error = nfsrv_mtostr(nd, layp, maxcnt);
4907 nd->nd_repstat = nfsrv_layoutcommit(nd, vp, layouttype, hasnewoff,
4908 newoff, offset, len, hasnewmtime, &newmtime, reclaim, &stateid,
4909 maxcnt, layp, &hasnewsize, &newsize, nd->nd_cred, p);
4910 NFSD_DEBUG(4, "nfsrv_layoutcommit stat=%u\n", nd->nd_repstat);
4911 if (nd->nd_repstat == 0) {
4912 if (hasnewsize != 0) {
4913 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED + NFSX_HYPER);
4914 *tl++ = newnfs_true;
4915 txdr_hyper(newsize, tl);
4917 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
4924 NFSEXITCODE2(error, nd);
4929 * nfsv4 layoutreturn service
4932 nfsrvd_layoutreturn(struct nfsrv_descript *nd, __unused int isdgram,
4933 vnode_t vp, struct nfsexstuff *exp)
4935 uint32_t *tl, *layp;
4936 nfsv4stateid_t stateid;
4937 int error = 0, fnd, kind, layouttype, iomode, maxcnt, reclaim;
4938 uint64_t offset, len;
4939 struct thread *p = curthread;
4942 NFSM_DISSECT(tl, uint32_t *, 4 * NFSX_UNSIGNED);
4944 layouttype = fxdr_unsigned(int, *tl++);
4945 iomode = fxdr_unsigned(int, *tl++);
4946 kind = fxdr_unsigned(int, *tl);
4947 NFSD_DEBUG(4, "layoutreturn recl=%d ltyp=%d iom=%d kind=%d\n", reclaim,
4948 layouttype, iomode, kind);
4949 if (kind == NFSV4LAYOUTRET_FILE) {
4950 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_HYPER + NFSX_STATEID +
4952 offset = fxdr_hyper(tl); tl += 2;
4953 len = fxdr_hyper(tl); tl += 2;
4954 stateid.seqid = fxdr_unsigned(uint32_t, *tl++);
4955 NFSBCOPY(tl, stateid.other, NFSX_STATEIDOTHER);
4956 tl += (NFSX_STATEIDOTHER / NFSX_UNSIGNED);
4959 * For the special stateid of other all 0s and seqid == 1, set
4960 * the stateid to the current stateid, if it is set.
4962 if (stateid.seqid == 1 && stateid.other[0] == 0 &&
4963 stateid.other[1] == 0 && stateid.other[2] == 0) {
4964 if ((nd->nd_flag & ND_CURSTATEID) != 0) {
4965 stateid = nd->nd_curstateid;
4968 nd->nd_repstat = NFSERR_BADSTATEID;
4973 maxcnt = fxdr_unsigned(int, *tl);
4975 * There is no fixed upper bound defined in the RFCs,
4976 * but 128Kbytes should be more than sufficient.
4978 if (maxcnt < 0 || maxcnt > 131072)
4981 layp = malloc(maxcnt + 1, M_TEMP, M_WAITOK);
4982 error = nfsrv_mtostr(nd, (char *)layp, maxcnt);
4987 if (reclaim == newnfs_true) {
4988 nd->nd_repstat = NFSERR_INVAL;
4994 nd->nd_repstat = nfsrv_layoutreturn(nd, vp, layouttype, iomode,
4995 offset, len, reclaim, kind, &stateid, maxcnt, layp, &fnd,
4997 NFSD_DEBUG(4, "nfsrv_layoutreturn stat=%u fnd=%d\n", nd->nd_repstat,
4999 if (nd->nd_repstat == 0) {
5000 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
5003 NFSM_BUILD(tl, uint32_t *, NFSX_STATEID);
5004 *tl++ = txdr_unsigned(stateid.seqid);
5005 NFSBCOPY(stateid.other, tl, NFSX_STATEIDOTHER);
5012 NFSEXITCODE2(error, nd);
5017 * nfsv4 layout error service
5020 nfsrvd_layouterror(struct nfsrv_descript *nd, __unused int isdgram,
5021 vnode_t vp, struct nfsexstuff *exp)
5024 nfsv4stateid_t stateid;
5025 int cnt, error = 0, i, stat;
5027 char devid[NFSX_V4DEVICEID];
5028 uint64_t offset, len;
5030 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_HYPER + NFSX_STATEID +
5032 offset = fxdr_hyper(tl); tl += 2;
5033 len = fxdr_hyper(tl); tl += 2;
5034 stateid.seqid = fxdr_unsigned(uint32_t, *tl++);
5035 NFSBCOPY(tl, stateid.other, NFSX_STATEIDOTHER);
5036 tl += (NFSX_STATEIDOTHER / NFSX_UNSIGNED);
5037 cnt = fxdr_unsigned(int, *tl);
5038 NFSD_DEBUG(4, "layouterror off=%ju len=%ju cnt=%d\n", (uintmax_t)offset,
5039 (uintmax_t)len, cnt);
5041 * For the special stateid of other all 0s and seqid == 1, set
5042 * the stateid to the current stateid, if it is set.
5044 if (stateid.seqid == 1 && stateid.other[0] == 0 &&
5045 stateid.other[1] == 0 && stateid.other[2] == 0) {
5046 if ((nd->nd_flag & ND_CURSTATEID) != 0) {
5047 stateid = nd->nd_curstateid;
5050 nd->nd_repstat = NFSERR_BADSTATEID;
5056 * Ignore offset, len and stateid for now.
5058 for (i = 0; i < cnt; i++) {
5059 NFSM_DISSECT(tl, uint32_t *, NFSX_V4DEVICEID + 2 *
5061 NFSBCOPY(tl, devid, NFSX_V4DEVICEID);
5062 tl += (NFSX_V4DEVICEID / NFSX_UNSIGNED);
5063 stat = fxdr_unsigned(int, *tl++);
5064 opnum = fxdr_unsigned(int, *tl);
5065 NFSD_DEBUG(4, "nfsrvd_layouterr op=%d stat=%d\n", opnum, stat);
5067 * Except for NFSERR_ACCES, NFSERR_STALE and NFSERR_NOSPC
5068 * errors, disable the mirror.
5070 if (stat != NFSERR_ACCES && stat != NFSERR_STALE &&
5071 stat != NFSERR_NOSPC)
5072 nfsrv_delds(devid, curthread);
5074 /* For NFSERR_NOSPC, mark all deviceids and layouts. */
5075 if (stat == NFSERR_NOSPC)
5076 nfsrv_marknospc(devid, true);
5080 NFSEXITCODE2(error, nd);
5085 * nfsv4 layout stats service
5088 nfsrvd_layoutstats(struct nfsrv_descript *nd, __unused int isdgram,
5089 vnode_t vp, struct nfsexstuff *exp)
5092 nfsv4stateid_t stateid;
5094 int layouttype __unused;
5095 char devid[NFSX_V4DEVICEID] __unused;
5096 uint64_t offset __unused, len __unused, readcount __unused;
5097 uint64_t readbytes __unused, writecount __unused, writebytes __unused;
5099 NFSM_DISSECT(tl, uint32_t *, 6 * NFSX_HYPER + NFSX_STATEID +
5100 NFSX_V4DEVICEID + 2 * NFSX_UNSIGNED);
5101 offset = fxdr_hyper(tl); tl += 2;
5102 len = fxdr_hyper(tl); tl += 2;
5103 stateid.seqid = fxdr_unsigned(uint32_t, *tl++);
5104 NFSBCOPY(tl, stateid.other, NFSX_STATEIDOTHER);
5105 tl += (NFSX_STATEIDOTHER / NFSX_UNSIGNED);
5106 readcount = fxdr_hyper(tl); tl += 2;
5107 readbytes = fxdr_hyper(tl); tl += 2;
5108 writecount = fxdr_hyper(tl); tl += 2;
5109 writebytes = fxdr_hyper(tl); tl += 2;
5110 NFSBCOPY(tl, devid, NFSX_V4DEVICEID);
5111 tl += (NFSX_V4DEVICEID / NFSX_UNSIGNED);
5112 layouttype = fxdr_unsigned(int, *tl++);
5113 cnt = fxdr_unsigned(int, *tl);
5114 error = nfsm_advance(nd, NFSM_RNDUP(cnt), -1);
5117 NFSD_DEBUG(4, "layoutstats cnt=%d\n", cnt);
5119 * For the special stateid of other all 0s and seqid == 1, set
5120 * the stateid to the current stateid, if it is set.
5122 if (stateid.seqid == 1 && stateid.other[0] == 0 &&
5123 stateid.other[1] == 0 && stateid.other[2] == 0) {
5124 if ((nd->nd_flag & ND_CURSTATEID) != 0) {
5125 stateid = nd->nd_curstateid;
5128 nd->nd_repstat = NFSERR_BADSTATEID;
5134 * No use for the stats for now.
5138 NFSEXITCODE2(error, nd);
5143 * nfsv4 io_advise service
5146 nfsrvd_ioadvise(struct nfsrv_descript *nd, __unused int isdgram,
5147 vnode_t vp, struct nfsexstuff *exp)
5150 nfsv4stateid_t stateid;
5155 NFSM_DISSECT(tl, uint32_t *, NFSX_STATEID + 2 * NFSX_HYPER);
5156 stateid.seqid = fxdr_unsigned(uint32_t, *tl++);
5157 NFSBCOPY(tl, stateid.other, NFSX_STATEIDOTHER);
5158 tl += (NFSX_STATEIDOTHER / NFSX_UNSIGNED);
5159 offset = fxdr_hyper(tl); tl += 2;
5160 len = fxdr_hyper(tl);
5161 error = nfsrv_getattrbits(nd, &hints, NULL, NULL);
5165 * For the special stateid of other all 0s and seqid == 1, set
5166 * the stateid to the current stateid, if it is set.
5168 if (stateid.seqid == 1 && stateid.other[0] == 0 &&
5169 stateid.other[1] == 0 && stateid.other[2] == 0) {
5170 if ((nd->nd_flag & ND_CURSTATEID) != 0) {
5171 stateid = nd->nd_curstateid;
5174 nd->nd_repstat = NFSERR_BADSTATEID;
5180 nd->nd_repstat = NFSERR_INVAL;
5185 if (vp->v_type != VREG) {
5186 if (vp->v_type == VDIR)
5187 nd->nd_repstat = NFSERR_ISDIR;
5189 nd->nd_repstat = NFSERR_WRONGTYPE;
5194 * For now, we can only handle WILLNEED and DONTNEED and don't use
5197 if ((NFSISSET_ATTRBIT(&hints, NFSV4IOHINT_WILLNEED) &&
5198 !NFSISSET_ATTRBIT(&hints, NFSV4IOHINT_DONTNEED)) ||
5199 (NFSISSET_ATTRBIT(&hints, NFSV4IOHINT_DONTNEED) &&
5200 !NFSISSET_ATTRBIT(&hints, NFSV4IOHINT_WILLNEED))) {
5202 if (NFSISSET_ATTRBIT(&hints, NFSV4IOHINT_WILLNEED)) {
5203 ret = VOP_ADVISE(vp, offset, len, POSIX_FADV_WILLNEED);
5204 NFSZERO_ATTRBIT(&hints);
5206 NFSSETBIT_ATTRBIT(&hints, NFSV4IOHINT_WILLNEED);
5208 NFSSETBIT_ATTRBIT(&hints, NFSV4IOHINT_NORMAL);
5210 ret = VOP_ADVISE(vp, offset, len, POSIX_FADV_DONTNEED);
5211 NFSZERO_ATTRBIT(&hints);
5213 NFSSETBIT_ATTRBIT(&hints, NFSV4IOHINT_DONTNEED);
5215 NFSSETBIT_ATTRBIT(&hints, NFSV4IOHINT_NORMAL);
5219 NFSZERO_ATTRBIT(&hints);
5220 NFSSETBIT_ATTRBIT(&hints, NFSV4IOHINT_NORMAL);
5223 nfsrv_putattrbit(nd, &hints);
5224 NFSEXITCODE2(error, nd);
5228 NFSEXITCODE2(error, nd);
5233 * nfsv4 getdeviceinfo service
5236 nfsrvd_getdevinfo(struct nfsrv_descript *nd, __unused int isdgram,
5237 __unused vnode_t vp, __unused struct nfsexstuff *exp)
5239 uint32_t *tl, maxcnt, notify[NFSV4_NOTIFYBITMAP];
5240 int cnt, devaddrlen, error = 0, i, layouttype;
5241 char devid[NFSX_V4DEVICEID], *devaddr;
5244 NFSM_DISSECT(tl, uint32_t *, 3 * NFSX_UNSIGNED + NFSX_V4DEVICEID);
5245 NFSBCOPY(tl, devid, NFSX_V4DEVICEID);
5246 tl += (NFSX_V4DEVICEID / NFSX_UNSIGNED);
5247 layouttype = fxdr_unsigned(int, *tl++);
5248 maxcnt = fxdr_unsigned(uint32_t, *tl++);
5249 cnt = fxdr_unsigned(int, *tl);
5250 NFSD_DEBUG(4, "getdevinfo ltyp=%d maxcnt=%u bitcnt=%d\n", layouttype,
5252 if (cnt > NFSV4_NOTIFYBITMAP || cnt < 0) {
5253 nd->nd_repstat = NFSERR_INVAL;
5257 NFSM_DISSECT(tl, uint32_t *, cnt * NFSX_UNSIGNED);
5258 for (i = 0; i < cnt; i++)
5259 notify[i] = fxdr_unsigned(uint32_t, *tl++);
5261 for (i = cnt; i < NFSV4_NOTIFYBITMAP; i++)
5265 * Check that the device id is not stale. Device ids are recreated
5266 * each time the nfsd threads are restarted.
5268 NFSBCOPY(devid, &dev_time, sizeof(dev_time));
5269 if (dev_time != nfsdev_time) {
5270 nd->nd_repstat = NFSERR_NOENT;
5274 /* Look for the device id. */
5275 nd->nd_repstat = nfsrv_getdevinfo(devid, layouttype, &maxcnt,
5276 notify, &devaddrlen, &devaddr);
5277 NFSD_DEBUG(4, "nfsrv_getdevinfo stat=%u\n", nd->nd_repstat);
5278 if (nd->nd_repstat == 0) {
5279 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
5280 *tl = txdr_unsigned(layouttype);
5281 nfsm_strtom(nd, devaddr, devaddrlen);
5283 for (i = 0; i < NFSV4_NOTIFYBITMAP; i++) {
5287 NFSM_BUILD(tl, uint32_t *, (cnt + 1) * NFSX_UNSIGNED);
5288 *tl++ = txdr_unsigned(cnt);
5289 for (i = 0; i < cnt; i++)
5290 *tl++ = txdr_unsigned(notify[i]);
5291 } else if (nd->nd_repstat == NFSERR_TOOSMALL) {
5292 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
5293 *tl = txdr_unsigned(maxcnt);
5296 NFSEXITCODE2(error, nd);
5301 * nfsv4 test stateid service
5304 nfsrvd_teststateid(struct nfsrv_descript *nd, __unused int isdgram,
5305 __unused vnode_t vp, __unused struct nfsexstuff *exp)
5308 nfsv4stateid_t *stateidp = NULL, *tstateidp;
5309 int cnt, error = 0, i, ret;
5310 struct thread *p = curthread;
5312 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
5313 cnt = fxdr_unsigned(int, *tl);
5314 if (cnt <= 0 || cnt > 1024) {
5315 nd->nd_repstat = NFSERR_BADXDR;
5318 stateidp = mallocarray(cnt, sizeof(nfsv4stateid_t), M_TEMP, M_WAITOK);
5319 tstateidp = stateidp;
5320 for (i = 0; i < cnt; i++) {
5321 NFSM_DISSECT(tl, uint32_t *, NFSX_STATEID);
5322 tstateidp->seqid = fxdr_unsigned(uint32_t, *tl++);
5323 NFSBCOPY(tl, tstateidp->other, NFSX_STATEIDOTHER);
5326 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
5327 *tl = txdr_unsigned(cnt);
5328 tstateidp = stateidp;
5329 for (i = 0; i < cnt; i++) {
5330 ret = nfsrv_teststateid(nd, tstateidp, p);
5331 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
5332 *tl = txdr_unsigned(ret);
5336 free(stateidp, M_TEMP);
5337 NFSEXITCODE2(error, nd);
5342 * nfs allocate service
5345 nfsrvd_allocate(struct nfsrv_descript *nd, __unused int isdgram,
5346 vnode_t vp, struct nfsexstuff *exp)
5349 struct nfsvattr forat;
5350 int error = 0, forat_ret = 1, gotproxystateid;
5352 struct nfsstate st, *stp = &st;
5353 struct nfslock lo, *lop = &lo;
5354 nfsv4stateid_t stateid;
5356 nfsattrbit_t attrbits;
5358 if (!nfsrv_doallocate) {
5360 * If any exported file system, such as a ZFS one, cannot
5361 * do VOP_ALLOCATE(), this operation cannot be supported
5362 * for NFSv4.2. This cannot be done 'per filesystem', but
5363 * must be for the entire nfsd NFSv4.2 service.
5365 nd->nd_repstat = NFSERR_NOTSUPP;
5368 gotproxystateid = 0;
5369 NFSM_DISSECT(tl, uint32_t *, NFSX_STATEID + 2 * NFSX_HYPER);
5370 stp->ls_flags = (NFSLCK_CHECK | NFSLCK_WRITEACCESS);
5371 lop->lo_flags = NFSLCK_WRITE;
5372 stp->ls_ownerlen = 0;
5374 stp->ls_uid = nd->nd_cred->cr_uid;
5375 stp->ls_stateid.seqid = fxdr_unsigned(u_int32_t, *tl++);
5376 clientid.lval[0] = stp->ls_stateid.other[0] = *tl++;
5377 clientid.lval[1] = stp->ls_stateid.other[1] = *tl++;
5378 if ((nd->nd_flag & ND_IMPLIEDCLID) != 0) {
5379 if ((nd->nd_flag & ND_NFSV41) != 0)
5380 clientid.qval = nd->nd_clientid.qval;
5381 else if (nd->nd_clientid.qval != clientid.qval)
5382 printf("EEK2 multiple clids\n");
5384 if ((nd->nd_flag & ND_NFSV41) != 0)
5385 printf("EEK! no clientid from session\n");
5386 nd->nd_flag |= ND_IMPLIEDCLID;
5387 nd->nd_clientid.qval = clientid.qval;
5389 stp->ls_stateid.other[2] = *tl++;
5391 * Don't allow this to be done for a DS.
5393 if ((nd->nd_flag & ND_DSSERVER) != 0)
5394 nd->nd_repstat = NFSERR_NOTSUPP;
5395 /* However, allow the proxy stateid. */
5396 if (stp->ls_stateid.seqid == 0xffffffff &&
5397 stp->ls_stateid.other[0] == 0x55555555 &&
5398 stp->ls_stateid.other[1] == 0x55555555 &&
5399 stp->ls_stateid.other[2] == 0x55555555)
5400 gotproxystateid = 1;
5401 off = fxdr_hyper(tl); tl += 2;
5402 lop->lo_first = off;
5403 len = fxdr_hyper(tl);
5404 lop->lo_end = lop->lo_first + len;
5406 * Sanity check the offset and length.
5407 * off and len are off_t (signed int64_t) whereas
5408 * lo_first and lo_end are uint64_t and, as such,
5409 * if off >= 0 && len > 0, lo_end cannot overflow
5410 * unless off_t is changed to something other than
5411 * int64_t. Check lo_end < lo_first in case that
5412 * is someday the case.
5414 if (nd->nd_repstat == 0 && (len <= 0 || off < 0 || lop->lo_end >
5415 OFF_MAX || lop->lo_end < lop->lo_first))
5416 nd->nd_repstat = NFSERR_INVAL;
5418 if (nd->nd_repstat == 0 && vp->v_type != VREG)
5419 nd->nd_repstat = NFSERR_WRONGTYPE;
5420 NFSZERO_ATTRBIT(&attrbits);
5421 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_OWNER);
5422 forat_ret = nfsvno_getattr(vp, &forat, nd, curthread, 1, &attrbits);
5423 if (nd->nd_repstat == 0)
5424 nd->nd_repstat = forat_ret;
5425 if (nd->nd_repstat == 0 && (forat.na_uid != nd->nd_cred->cr_uid ||
5426 NFSVNO_EXSTRICTACCESS(exp)))
5427 nd->nd_repstat = nfsvno_accchk(vp, VWRITE, nd->nd_cred, exp,
5428 curthread, NFSACCCHK_ALLOWOWNER, NFSACCCHK_VPISLOCKED,
5430 if (nd->nd_repstat == 0 && gotproxystateid == 0)
5431 nd->nd_repstat = nfsrv_lockctrl(vp, &stp, &lop, NULL, clientid,
5432 &stateid, exp, nd, curthread);
5434 NFSD_DEBUG(4, "nfsrvd_allocate: off=%jd len=%jd stat=%d\n",
5435 (intmax_t)off, (intmax_t)len, nd->nd_repstat);
5436 if (nd->nd_repstat == 0)
5437 nd->nd_repstat = nfsvno_allocate(vp, off, len, nd->nd_cred,
5439 NFSD_DEBUG(4, "nfsrvd_allocate: aft nfsvno_allocate=%d\n",
5442 NFSEXITCODE2(0, nd);
5446 NFSEXITCODE2(error, nd);
5451 * nfs deallocate service
5454 nfsrvd_deallocate(struct nfsrv_descript *nd, __unused int isdgram,
5455 vnode_t vp, struct nfsexstuff *exp)
5458 struct nfsvattr forat;
5459 int error = 0, forat_ret = 1, gotproxystateid;
5461 struct nfsstate st, *stp = &st;
5462 struct nfslock lo, *lop = &lo;
5463 nfsv4stateid_t stateid;
5465 nfsattrbit_t attrbits;
5467 gotproxystateid = 0;
5468 NFSM_DISSECT(tl, uint32_t *, NFSX_STATEID + 2 * NFSX_HYPER);
5469 stp->ls_flags = (NFSLCK_CHECK | NFSLCK_WRITEACCESS);
5470 lop->lo_flags = NFSLCK_WRITE;
5471 stp->ls_ownerlen = 0;
5473 stp->ls_uid = nd->nd_cred->cr_uid;
5474 stp->ls_stateid.seqid = fxdr_unsigned(u_int32_t, *tl++);
5475 clientid.lval[0] = stp->ls_stateid.other[0] = *tl++;
5476 clientid.lval[1] = stp->ls_stateid.other[1] = *tl++;
5477 if ((nd->nd_flag & ND_IMPLIEDCLID) != 0) {
5478 if ((nd->nd_flag & ND_NFSV41) != 0)
5479 clientid.qval = nd->nd_clientid.qval;
5480 else if (nd->nd_clientid.qval != clientid.qval)
5481 printf("EEK2 multiple clids\n");
5483 if ((nd->nd_flag & ND_NFSV41) != 0)
5484 printf("EEK! no clientid from session\n");
5485 nd->nd_flag |= ND_IMPLIEDCLID;
5486 nd->nd_clientid.qval = clientid.qval;
5488 stp->ls_stateid.other[2] = *tl++;
5490 * Don't allow this to be done for a DS.
5492 if ((nd->nd_flag & ND_DSSERVER) != 0)
5493 nd->nd_repstat = NFSERR_NOTSUPP;
5494 /* However, allow the proxy stateid. */
5495 if (stp->ls_stateid.seqid == 0xffffffff &&
5496 stp->ls_stateid.other[0] == 0x55555555 &&
5497 stp->ls_stateid.other[1] == 0x55555555 &&
5498 stp->ls_stateid.other[2] == 0x55555555)
5499 gotproxystateid = 1;
5500 off = fxdr_hyper(tl); tl += 2;
5501 lop->lo_first = off;
5502 len = fxdr_hyper(tl);
5505 NFSD_DEBUG(4, "dealloc: off=%jd len=%jd\n", (intmax_t)off,
5507 lop->lo_end = lop->lo_first + len;
5509 * Sanity check the offset and length.
5510 * off and len are off_t (signed int64_t) whereas
5511 * lo_first and lo_end are uint64_t and, as such,
5512 * if off >= 0 && len > 0, lo_end cannot overflow
5513 * unless off_t is changed to something other than
5514 * int64_t. Check lo_end < lo_first in case that
5515 * is someday the case.
5516 * The error to return is not specified by RFC 7862 so I
5517 * made this compatible with the Linux knfsd.
5519 if (nd->nd_repstat == 0) {
5520 if (off < 0 || lop->lo_end > NFSRV_MAXFILESIZE)
5521 nd->nd_repstat = NFSERR_FBIG;
5522 else if (len == 0 || lop->lo_end < lop->lo_first)
5523 nd->nd_repstat = NFSERR_INVAL;
5526 if (nd->nd_repstat == 0 && vp->v_type != VREG)
5527 nd->nd_repstat = NFSERR_WRONGTYPE;
5528 NFSZERO_ATTRBIT(&attrbits);
5529 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_OWNER);
5530 forat_ret = nfsvno_getattr(vp, &forat, nd, curthread, 1, &attrbits);
5531 if (nd->nd_repstat == 0)
5532 nd->nd_repstat = forat_ret;
5533 if (nd->nd_repstat == 0 && (forat.na_uid != nd->nd_cred->cr_uid ||
5534 NFSVNO_EXSTRICTACCESS(exp)))
5535 nd->nd_repstat = nfsvno_accchk(vp, VWRITE, nd->nd_cred, exp,
5536 curthread, NFSACCCHK_ALLOWOWNER, NFSACCCHK_VPISLOCKED,
5538 if (nd->nd_repstat == 0 && gotproxystateid == 0)
5539 nd->nd_repstat = nfsrv_lockctrl(vp, &stp, &lop, NULL, clientid,
5540 &stateid, exp, nd, curthread);
5542 if (nd->nd_repstat == 0)
5543 nd->nd_repstat = nfsvno_deallocate(vp, off, len, nd->nd_cred,
5546 NFSD_DEBUG(4, "eo deallocate=%d\n", nd->nd_repstat);
5547 NFSEXITCODE2(0, nd);
5551 NFSEXITCODE2(error, nd);
5559 nfsrvd_copy_file_range(struct nfsrv_descript *nd, __unused int isdgram,
5560 vnode_t vp, vnode_t tovp, struct nfsexstuff *exp, struct nfsexstuff *toexp)
5564 int cnt, error = 0, ret;
5565 off_t inoff, outoff;
5568 struct nfsstate inst, outst, *instp = &inst, *outstp = &outst;
5569 struct nfslock inlo, outlo, *inlop = &inlo, *outlop = &outlo;
5571 nfsv4stateid_t stateid;
5572 nfsattrbit_t attrbits;
5573 void *rl_rcookie, *rl_wcookie;
5575 rl_rcookie = rl_wcookie = NULL;
5576 if (nfsrv_devidcnt > 0) {
5578 * For a pNFS server, reply NFSERR_NOTSUPP so that the client
5579 * will do the copy via I/O on the DS(s).
5581 nd->nd_repstat = NFSERR_NOTSUPP;
5585 /* Copying a byte range within the same file is not allowed. */
5586 nd->nd_repstat = NFSERR_INVAL;
5589 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_STATEID + 3 * NFSX_HYPER +
5591 instp->ls_flags = (NFSLCK_CHECK | NFSLCK_READACCESS);
5592 inlop->lo_flags = NFSLCK_READ;
5593 instp->ls_ownerlen = 0;
5594 instp->ls_op = NULL;
5595 instp->ls_uid = nd->nd_cred->cr_uid;
5596 instp->ls_stateid.seqid = fxdr_unsigned(uint32_t, *tl++);
5597 clientid.lval[0] = instp->ls_stateid.other[0] = *tl++;
5598 clientid.lval[1] = instp->ls_stateid.other[1] = *tl++;
5599 if ((nd->nd_flag & ND_IMPLIEDCLID) != 0)
5600 clientid.qval = nd->nd_clientid.qval;
5601 instp->ls_stateid.other[2] = *tl++;
5602 outstp->ls_flags = (NFSLCK_CHECK | NFSLCK_WRITEACCESS);
5603 outlop->lo_flags = NFSLCK_WRITE;
5604 outstp->ls_ownerlen = 0;
5605 outstp->ls_op = NULL;
5606 outstp->ls_uid = nd->nd_cred->cr_uid;
5607 outstp->ls_stateid.seqid = fxdr_unsigned(uint32_t, *tl++);
5608 outstp->ls_stateid.other[0] = *tl++;
5609 outstp->ls_stateid.other[1] = *tl++;
5610 outstp->ls_stateid.other[2] = *tl++;
5611 inoff = fxdr_hyper(tl); tl += 2;
5612 inlop->lo_first = inoff;
5613 outoff = fxdr_hyper(tl); tl += 2;
5614 outlop->lo_first = outoff;
5615 len = fxdr_hyper(tl); tl += 2;
5617 /* len == 0 means to EOF. */
5618 inlop->lo_end = OFF_MAX;
5619 outlop->lo_end = OFF_MAX;
5621 inlop->lo_end = inlop->lo_first + len;
5622 outlop->lo_end = outlop->lo_first + len;
5626 * At this time only consecutive, synchronous copy is supported,
5627 * so ca_consecutive and ca_synchronous can be ignored.
5631 cnt = fxdr_unsigned(int, *tl);
5632 if ((nd->nd_flag & ND_DSSERVER) != 0 || cnt != 0)
5633 nd->nd_repstat = NFSERR_NOTSUPP;
5634 if (nd->nd_repstat == 0 && (inoff > OFF_MAX || outoff > OFF_MAX ||
5635 inlop->lo_end > OFF_MAX || outlop->lo_end > OFF_MAX ||
5636 inlop->lo_end < inlop->lo_first || outlop->lo_end <
5638 nd->nd_repstat = NFSERR_INVAL;
5640 if (nd->nd_repstat == 0 && vp->v_type != VREG)
5641 nd->nd_repstat = NFSERR_WRONGTYPE;
5643 /* Check permissions for the input file. */
5644 NFSZERO_ATTRBIT(&attrbits);
5645 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_OWNER);
5646 ret = nfsvno_getattr(vp, &at, nd, curthread, 1, &attrbits);
5647 if (nd->nd_repstat == 0)
5648 nd->nd_repstat = ret;
5649 if (nd->nd_repstat == 0 && (at.na_uid != nd->nd_cred->cr_uid ||
5650 NFSVNO_EXSTRICTACCESS(exp)))
5651 nd->nd_repstat = nfsvno_accchk(vp, VREAD, nd->nd_cred, exp,
5652 curthread, NFSACCCHK_ALLOWOWNER, NFSACCCHK_VPISLOCKED,
5654 if (nd->nd_repstat == 0)
5655 nd->nd_repstat = nfsrv_lockctrl(vp, &instp, &inlop, NULL,
5656 clientid, &stateid, exp, nd, curthread);
5658 if (nd->nd_repstat != 0)
5661 error = NFSVOPLOCK(tovp, LK_SHARED);
5664 if (tovp->v_type != VREG)
5665 nd->nd_repstat = NFSERR_WRONGTYPE;
5667 /* For the output file, we only need the Owner attribute. */
5668 ret = nfsvno_getattr(tovp, &at, nd, curthread, 1, &attrbits);
5669 if (nd->nd_repstat == 0)
5670 nd->nd_repstat = ret;
5671 if (nd->nd_repstat == 0 && (at.na_uid != nd->nd_cred->cr_uid ||
5672 NFSVNO_EXSTRICTACCESS(exp)))
5673 nd->nd_repstat = nfsvno_accchk(tovp, VWRITE, nd->nd_cred, toexp,
5674 curthread, NFSACCCHK_ALLOWOWNER, NFSACCCHK_VPISLOCKED,
5676 if (nd->nd_repstat == 0)
5677 nd->nd_repstat = nfsrv_lockctrl(tovp, &outstp, &outlop, NULL,
5678 clientid, &stateid, toexp, nd, curthread);
5681 /* Range lock the byte ranges for both invp and outvp. */
5682 if (nd->nd_repstat == 0) {
5685 rl_wcookie = vn_rangelock_wlock(tovp, outoff,
5687 rl_rcookie = vn_rangelock_tryrlock(vp, inoff,
5690 rl_wcookie = vn_rangelock_wlock(tovp, outoff,
5692 rl_rcookie = vn_rangelock_tryrlock(vp, inoff,
5695 if (rl_rcookie != NULL)
5697 vn_rangelock_unlock(tovp, rl_wcookie);
5699 rl_rcookie = vn_rangelock_rlock(vp, inoff,
5702 rl_rcookie = vn_rangelock_rlock(vp, inoff,
5704 vn_rangelock_unlock(vp, rl_rcookie);
5707 error = NFSVOPLOCK(vp, LK_SHARED);
5709 ret = nfsvno_getattr(vp, &at, nd, curthread, 1, NULL);
5712 * Since invp is range locked, na_size should
5715 if (len == 0 && at.na_size > inoff) {
5717 * If len == 0, set it based on invp's
5718 * size. If offset is past EOF, just
5721 len = at.na_size - inoff;
5722 } else if (nfsrv_linux42server == 0 &&
5723 inoff + len > at.na_size) {
5725 * RFC-7862 says that NFSERR_INVAL must
5726 * be returned when inoff + len exceeds
5727 * the file size, however the NFSv4.2
5728 * Linux client likes to do this, so
5729 * only check if nfsrv_linux42server
5732 nd->nd_repstat = NFSERR_INVAL;
5736 if (ret != 0 && nd->nd_repstat == 0)
5737 nd->nd_repstat = ret;
5738 } else if (nd->nd_repstat == 0)
5739 nd->nd_repstat = error;
5743 if (nd->nd_repstat == 0) {
5744 nd->nd_repstat = vn_copy_file_range(vp, &inoff, tovp, &outoff,
5745 &xfer, COPY_FILE_RANGE_TIMEO1SEC, nd->nd_cred, nd->nd_cred,
5747 if (nd->nd_repstat == 0)
5751 /* Unlock the ranges. */
5752 if (rl_rcookie != NULL)
5753 vn_rangelock_unlock(vp, rl_rcookie);
5754 if (rl_wcookie != NULL)
5755 vn_rangelock_unlock(tovp, rl_wcookie);
5757 if (nd->nd_repstat == 0) {
5758 NFSM_BUILD(tl, uint32_t *, 4 * NFSX_UNSIGNED + NFSX_HYPER +
5760 *tl++ = txdr_unsigned(0); /* No callback ids. */
5761 txdr_hyper(len, tl); tl += 2;
5762 *tl++ = txdr_unsigned(NFSWRITE_UNSTABLE);
5763 *tl++ = txdr_unsigned(nfsboottime.tv_sec);
5764 *tl++ = txdr_unsigned(nfsboottime.tv_usec);
5765 *tl++ = newnfs_true;
5771 NFSEXITCODE2(error, nd);
5776 NFSEXITCODE2(error, nd);
5784 nfsrvd_seek(struct nfsrv_descript *nd, __unused int isdgram,
5785 vnode_t vp, struct nfsexstuff *exp)
5789 int content, error = 0;
5792 nfsattrbit_t attrbits;
5795 NFSM_DISSECT(tl, uint32_t *, NFSX_STATEID + NFSX_HYPER + NFSX_UNSIGNED);
5796 /* Ignore the stateid for now. */
5797 tl += (NFSX_STATEID / NFSX_UNSIGNED);
5798 off = fxdr_hyper(tl); tl += 2;
5799 content = fxdr_unsigned(int, *tl);
5800 if (content == NFSV4CONTENT_DATA)
5802 else if (content == NFSV4CONTENT_HOLE)
5805 nd->nd_repstat = NFSERR_BADXDR;
5806 if (nd->nd_repstat == 0 && vp->v_type == VDIR)
5807 nd->nd_repstat = NFSERR_ISDIR;
5808 if (nd->nd_repstat == 0 && vp->v_type != VREG)
5809 nd->nd_repstat = NFSERR_WRONGTYPE;
5810 if (nd->nd_repstat == 0 && off < 0)
5811 nd->nd_repstat = NFSERR_NXIO;
5812 if (nd->nd_repstat == 0) {
5813 /* Check permissions for the input file. */
5814 NFSZERO_ATTRBIT(&attrbits);
5815 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_OWNER);
5816 nd->nd_repstat = nfsvno_getattr(vp, &at, nd, curthread, 1,
5819 if (nd->nd_repstat == 0 && (at.na_uid != nd->nd_cred->cr_uid ||
5820 NFSVNO_EXSTRICTACCESS(exp)))
5821 nd->nd_repstat = nfsvno_accchk(vp, VREAD, nd->nd_cred, exp,
5822 curthread, NFSACCCHK_ALLOWOWNER, NFSACCCHK_VPISLOCKED,
5824 if (nd->nd_repstat != 0)
5827 /* nfsvno_seek() unlocks and vrele()s the vp. */
5828 nd->nd_repstat = nfsvno_seek(nd, vp, cmd, &off, content, &eof,
5829 nd->nd_cred, curthread);
5830 if (nd->nd_repstat == 0 && eof && content == NFSV4CONTENT_DATA &&
5831 nfsrv_linux42server != 0)
5832 nd->nd_repstat = NFSERR_NXIO;
5833 if (nd->nd_repstat == 0) {
5834 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED + NFSX_HYPER);
5836 *tl++ = newnfs_true;
5838 *tl++ = newnfs_false;
5839 txdr_hyper(off, tl);
5841 NFSEXITCODE2(error, nd);
5845 NFSEXITCODE2(error, nd);
5850 * nfs get extended attribute service
5853 nfsrvd_getxattr(struct nfsrv_descript *nd, __unused int isdgram,
5854 vnode_t vp, __unused struct nfsexstuff *exp)
5857 struct mbuf *mp = NULL, *mpend = NULL;
5860 struct thread *p = curthread;
5864 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
5865 len = fxdr_unsigned(int, *tl);
5867 nd->nd_repstat = NFSERR_BADXDR;
5870 if (len > EXTATTR_MAXNAMELEN) {
5871 nd->nd_repstat = NFSERR_NOXATTR;
5874 name = malloc(len + 1, M_TEMP, M_WAITOK);
5875 nd->nd_repstat = nfsrv_mtostr(nd, name, len);
5876 if (nd->nd_repstat == 0)
5877 nd->nd_repstat = nfsvno_getxattr(vp, name,
5878 nd->nd_maxresp, nd->nd_cred, nd->nd_flag,
5879 nd->nd_maxextsiz, p, &mp, &mpend, &len);
5880 if (nd->nd_repstat == ENOATTR)
5881 nd->nd_repstat = NFSERR_NOXATTR;
5882 else if (nd->nd_repstat == EOPNOTSUPP)
5883 nd->nd_repstat = NFSERR_NOTSUPP;
5884 if (nd->nd_repstat == 0) {
5885 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
5886 *tl = txdr_unsigned(len);
5888 nd->nd_mb->m_next = mp;
5890 if ((mpend->m_flags & M_EXTPG) != 0) {
5891 nd->nd_flag |= ND_EXTPG;
5892 nd->nd_bextpg = mpend->m_epg_npgs - 1;
5893 nd->nd_bpos = (char *)(void *)
5894 PHYS_TO_DMAP(mpend->m_epg_pa[nd->nd_bextpg]);
5895 off = (nd->nd_bextpg == 0) ?
5896 mpend->m_epg_1st_off : 0;
5897 nd->nd_bpos += off + mpend->m_epg_last_len;
5898 nd->nd_bextpgsiz = PAGE_SIZE -
5899 mpend->m_epg_last_len - off;
5901 nd->nd_bpos = mtod(mpend, char *) +
5908 if (nd->nd_repstat == 0)
5909 nd->nd_repstat = error;
5911 NFSEXITCODE2(0, nd);
5916 * nfs set extended attribute service
5919 nfsrvd_setxattr(struct nfsrv_descript *nd, __unused int isdgram,
5920 vnode_t vp, __unused struct nfsexstuff *exp)
5923 struct nfsvattr ova, nva;
5924 nfsattrbit_t attrbits;
5925 int error, len, opt;
5928 struct thread *p = curthread;
5932 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
5933 opt = fxdr_unsigned(int, *tl++);
5934 len = fxdr_unsigned(int, *tl);
5936 nd->nd_repstat = NFSERR_BADXDR;
5939 if (len > EXTATTR_MAXNAMELEN) {
5940 nd->nd_repstat = NFSERR_NOXATTR;
5943 name = malloc(len + 1, M_TEMP, M_WAITOK);
5944 error = nfsrv_mtostr(nd, name, len);
5947 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
5948 len = fxdr_unsigned(int, *tl);
5949 if (len < 0 || len > IOSIZE_MAX) {
5950 nd->nd_repstat = NFSERR_XATTR2BIG;
5954 case NFSV4SXATTR_CREATE:
5955 error = VOP_GETEXTATTR(vp, EXTATTR_NAMESPACE_USER, name, NULL,
5956 &siz, nd->nd_cred, p);
5957 if (error != ENOATTR)
5958 nd->nd_repstat = NFSERR_EXIST;
5961 case NFSV4SXATTR_REPLACE:
5962 error = VOP_GETEXTATTR(vp, EXTATTR_NAMESPACE_USER, name, NULL,
5963 &siz, nd->nd_cred, p);
5965 nd->nd_repstat = NFSERR_NOXATTR;
5967 case NFSV4SXATTR_EITHER:
5970 nd->nd_repstat = NFSERR_BADXDR;
5972 if (nd->nd_repstat != 0)
5975 /* Now, do the Set Extended attribute, with Change before and after. */
5976 NFSZERO_ATTRBIT(&attrbits);
5977 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_CHANGE);
5978 nd->nd_repstat = nfsvno_getattr(vp, &ova, nd, p, 1, &attrbits);
5979 if (nd->nd_repstat == 0) {
5980 nd->nd_repstat = nfsvno_setxattr(vp, name, len, nd->nd_md,
5981 nd->nd_dpos, nd->nd_cred, p);
5982 if (nd->nd_repstat == ENXIO)
5983 nd->nd_repstat = NFSERR_XATTR2BIG;
5985 if (nd->nd_repstat == 0 && len > 0)
5986 nd->nd_repstat = nfsm_advance(nd, NFSM_RNDUP(len), -1);
5987 if (nd->nd_repstat == 0)
5988 nd->nd_repstat = nfsvno_getattr(vp, &nva, nd, p, 1, &attrbits);
5989 if (nd->nd_repstat == 0) {
5990 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_HYPER + NFSX_UNSIGNED);
5991 *tl++ = newnfs_true;
5992 txdr_hyper(ova.na_filerev, tl); tl += 2;
5993 txdr_hyper(nva.na_filerev, tl);
5998 if (nd->nd_repstat == 0)
5999 nd->nd_repstat = error;
6001 NFSEXITCODE2(0, nd);
6006 * nfs remove extended attribute service
6009 nfsrvd_rmxattr(struct nfsrv_descript *nd, __unused int isdgram,
6010 vnode_t vp, __unused struct nfsexstuff *exp)
6013 struct nfsvattr ova, nva;
6014 nfsattrbit_t attrbits;
6017 struct thread *p = curthread;
6021 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
6022 len = fxdr_unsigned(int, *tl);
6024 nd->nd_repstat = NFSERR_BADXDR;
6027 if (len > EXTATTR_MAXNAMELEN) {
6028 nd->nd_repstat = NFSERR_NOXATTR;
6031 name = malloc(len + 1, M_TEMP, M_WAITOK);
6032 error = nfsrv_mtostr(nd, name, len);
6036 if ((nd->nd_flag & ND_IMPLIEDCLID) == 0) {
6037 printf("EEK! nfsrvd_rmxattr: no implied clientid\n");
6038 error = NFSERR_NOXATTR;
6042 * Now, do the Remove Extended attribute, with Change before and
6045 NFSZERO_ATTRBIT(&attrbits);
6046 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_CHANGE);
6047 nd->nd_repstat = nfsvno_getattr(vp, &ova, nd, p, 1, &attrbits);
6048 if (nd->nd_repstat == 0) {
6049 nd->nd_repstat = nfsvno_rmxattr(nd, vp, name, nd->nd_cred, p);
6050 if (nd->nd_repstat == ENOATTR)
6051 nd->nd_repstat = NFSERR_NOXATTR;
6053 if (nd->nd_repstat == 0)
6054 nd->nd_repstat = nfsvno_getattr(vp, &nva, nd, p, 1, &attrbits);
6055 if (nd->nd_repstat == 0) {
6056 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_HYPER + NFSX_UNSIGNED);
6057 *tl++ = newnfs_true;
6058 txdr_hyper(ova.na_filerev, tl); tl += 2;
6059 txdr_hyper(nva.na_filerev, tl);
6064 if (nd->nd_repstat == 0)
6065 nd->nd_repstat = error;
6067 NFSEXITCODE2(0, nd);
6072 * nfs list extended attribute service
6075 nfsrvd_listxattr(struct nfsrv_descript *nd, __unused int isdgram,
6076 vnode_t vp, __unused struct nfsexstuff *exp)
6078 uint32_t cnt, *tl, len, len2, i, pos, retlen;
6080 uint64_t cookie, cookie2;
6083 struct thread *p = curthread;
6087 NFSM_DISSECT(tl, uint32_t *, NFSX_HYPER + NFSX_UNSIGNED);
6089 * The cookie doesn't need to be in net byte order, but FreeBSD
6090 * does so to make it more readable in packet traces.
6092 cookie = fxdr_hyper(tl); tl += 2;
6093 len = fxdr_unsigned(uint32_t, *tl);
6094 if (len == 0 || cookie >= IOSIZE_MAX) {
6095 nd->nd_repstat = NFSERR_BADXDR;
6098 if (len > nd->nd_maxresp - NFS_MAXXDR)
6099 len = nd->nd_maxresp - NFS_MAXXDR;
6101 nd->nd_repstat = nfsvno_listxattr(vp, cookie, nd->nd_cred, p, &buf,
6103 if (nd->nd_repstat == EOPNOTSUPP)
6104 nd->nd_repstat = NFSERR_NOTSUPP;
6105 if (nd->nd_repstat == 0) {
6106 cookie2 = cookie + len;
6107 if (cookie2 < cookie)
6108 nd->nd_repstat = NFSERR_BADXDR;
6110 retlen = NFSX_HYPER + 2 * NFSX_UNSIGNED;
6111 if (nd->nd_repstat == 0 && len2 < retlen)
6112 nd->nd_repstat = NFSERR_TOOSMALL;
6113 if (nd->nd_repstat == 0) {
6114 /* Now copy the entries out. */
6116 /* The cookie was at eof. */
6117 NFSM_BUILD(tl, uint32_t *, NFSX_HYPER + 2 *
6119 txdr_hyper(cookie2, tl); tl += 2;
6120 *tl++ = txdr_unsigned(0);
6125 /* Sanity check the cookie. */
6126 for (pos = 0; pos < len; pos += (i + 1)) {
6131 if (pos != cookie) {
6132 nd->nd_repstat = NFSERR_INVAL;
6136 /* Loop around copying the entrie(s) out. */
6140 while (i < len && len2 >= retlen + NFSM_RNDUP(i) +
6143 NFSM_BUILD(tl, uint32_t *, NFSX_HYPER +
6145 txdr_hyper(cookie2, tl); tl += 2;
6147 retlen += nfsm_strtom(nd, &buf[pos + 1], i);
6154 * eof is set true/false by nfsvno_listxattr(), but if we
6155 * can't copy all entries returned by nfsvno_listxattr(),
6156 * we are not at eof.
6161 /* *tl is set above. */
6162 *tl = txdr_unsigned(cnt);
6163 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
6169 nd->nd_repstat = NFSERR_TOOSMALL;
6174 if (nd->nd_repstat == 0)
6175 nd->nd_repstat = error;
6177 NFSEXITCODE2(0, nd);
6182 * nfsv4 service not supported
6185 nfsrvd_notsupp(struct nfsrv_descript *nd, __unused int isdgram,
6186 __unused vnode_t vp, __unused struct nfsexstuff *exp)
6189 nd->nd_repstat = NFSERR_NOTSUPP;
6190 NFSEXITCODE2(0, nd);