2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Rick Macklem at The University of Guelph.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
38 #include "opt_inet6.h"
40 * nfs version 2, 3 and 4 server calls to vnode ops
41 * - these routines generally have 3 phases
42 * 1 - break down and validate rpc request in mbuf list
43 * 2 - do the vnode ops for the request, usually by calling a nfsvno_XXX()
44 * function in nfsd_port.c
45 * 3 - build the rpc reply in an mbuf list
46 * For nfsv4, these functions are called for each Op within the Compound RPC.
50 #include <fs/nfs/nfsport.h>
53 extern u_int32_t newnfs_false, newnfs_true;
54 extern enum vtype nv34tov_type[8];
55 extern struct timeval nfsboottime;
56 extern int nfs_rootfhset;
57 extern int nfsrv_enable_crossmntpt;
58 extern int nfsrv_statehashsize;
59 #endif /* !APPLEKEXT */
61 static int nfs_async = 0;
62 SYSCTL_DECL(_vfs_nfsd);
63 SYSCTL_INT(_vfs_nfsd, OID_AUTO, async, CTLFLAG_RW, &nfs_async, 0,
64 "Tell client that writes were synced even though they were not");
67 * This list defines the GSS mechanisms supported.
68 * (Don't ask me how you get these strings from the RFC stuff like
69 * iso(1), org(3)... but someone did it, so I don't need to know.)
71 static struct nfsgss_mechlist nfsgss_mechlist[] = {
72 { 9, "\052\206\110\206\367\022\001\002\002", 11 },
77 static void nfsrvd_symlinksub(struct nfsrv_descript *nd, struct nameidata *ndp,
78 struct nfsvattr *nvap, fhandle_t *fhp, vnode_t *vpp,
79 vnode_t dirp, struct nfsvattr *dirforp, struct nfsvattr *diraftp,
80 int *diraft_retp, nfsattrbit_t *attrbitp,
81 NFSACL_T *aclp, NFSPROC_T *p, struct nfsexstuff *exp, char *pathcp,
83 static void nfsrvd_mkdirsub(struct nfsrv_descript *nd, struct nameidata *ndp,
84 struct nfsvattr *nvap, fhandle_t *fhp, vnode_t *vpp,
85 vnode_t dirp, struct nfsvattr *dirforp, struct nfsvattr *diraftp,
86 int *diraft_retp, nfsattrbit_t *attrbitp, NFSACL_T *aclp,
87 NFSPROC_T *p, struct nfsexstuff *exp);
90 * nfs access service (not a part of NFS V2)
93 nfsrvd_access(struct nfsrv_descript *nd, __unused int isdgram,
94 vnode_t vp, NFSPROC_T *p, struct nfsexstuff *exp)
97 int getret, error = 0;
99 u_int32_t testmode, nfsmode, supported = 0;
102 if (nd->nd_repstat) {
103 nfsrv_postopattr(nd, 1, &nva);
106 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
107 nfsmode = fxdr_unsigned(u_int32_t, *tl);
108 if ((nd->nd_flag & ND_NFSV4) &&
109 (nfsmode & ~(NFSACCESS_READ | NFSACCESS_LOOKUP |
110 NFSACCESS_MODIFY | NFSACCESS_EXTEND | NFSACCESS_DELETE |
111 NFSACCESS_EXECUTE))) {
112 nd->nd_repstat = NFSERR_INVAL;
116 if (nfsmode & NFSACCESS_READ) {
117 supported |= NFSACCESS_READ;
118 if (nfsvno_accchk(vp, VREAD, nd->nd_cred, exp, p,
119 NFSACCCHK_NOOVERRIDE, NFSACCCHK_VPISLOCKED, &supported))
120 nfsmode &= ~NFSACCESS_READ;
122 if (nfsmode & NFSACCESS_MODIFY) {
123 supported |= NFSACCESS_MODIFY;
124 if (nfsvno_accchk(vp, VWRITE, nd->nd_cred, exp, p,
125 NFSACCCHK_NOOVERRIDE, NFSACCCHK_VPISLOCKED, &supported))
126 nfsmode &= ~NFSACCESS_MODIFY;
128 if (nfsmode & NFSACCESS_EXTEND) {
129 supported |= NFSACCESS_EXTEND;
130 if (nfsvno_accchk(vp, VWRITE | VAPPEND, nd->nd_cred, exp, p,
131 NFSACCCHK_NOOVERRIDE, NFSACCCHK_VPISLOCKED, &supported))
132 nfsmode &= ~NFSACCESS_EXTEND;
134 if (nfsmode & NFSACCESS_DELETE) {
135 supported |= NFSACCESS_DELETE;
136 if (vp->v_type == VDIR)
137 deletebit = VDELETE_CHILD;
140 if (nfsvno_accchk(vp, deletebit, nd->nd_cred, exp, p,
141 NFSACCCHK_NOOVERRIDE, NFSACCCHK_VPISLOCKED, &supported))
142 nfsmode &= ~NFSACCESS_DELETE;
144 if (vnode_vtype(vp) == VDIR)
145 testmode = NFSACCESS_LOOKUP;
147 testmode = NFSACCESS_EXECUTE;
148 if (nfsmode & testmode) {
149 supported |= (nfsmode & testmode);
150 if (nfsvno_accchk(vp, VEXEC, nd->nd_cred, exp, p,
151 NFSACCCHK_NOOVERRIDE, NFSACCCHK_VPISLOCKED, &supported))
152 nfsmode &= ~testmode;
154 nfsmode &= supported;
155 if (nd->nd_flag & ND_NFSV3) {
156 getret = nfsvno_getattr(vp, &nva, nd->nd_cred, p, 1);
157 nfsrv_postopattr(nd, getret, &nva);
160 if (nd->nd_flag & ND_NFSV4) {
161 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
162 *tl++ = txdr_unsigned(supported);
164 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
165 *tl = txdr_unsigned(nfsmode);
172 NFSEXITCODE2(error, nd);
177 * nfs getattr service
180 nfsrvd_getattr(struct nfsrv_descript *nd, int isdgram,
181 vnode_t vp, NFSPROC_T *p, __unused struct nfsexstuff *exp)
185 int at_root = 0, error = 0, supports_nfsv4acls;
186 struct nfsreferral *refp;
187 nfsattrbit_t attrbits, tmpbits;
189 struct vnode *tvp = NULL;
191 uint64_t mounted_on_fileno = 0;
196 if (nd->nd_flag & ND_NFSV4) {
197 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
204 * Check for a referral.
206 refp = nfsv4root_getreferral(vp, NULL, 0);
208 (void) nfsrv_putreferralattr(nd, &attrbits, refp, 1,
213 if (nd->nd_repstat == 0) {
215 NFSSET_ATTRBIT(&tmpbits, &attrbits);
216 if (NFSISSET_ATTRBIT(&tmpbits, NFSATTRBIT_ACL)) {
217 NFSCLRBIT_ATTRBIT(&tmpbits, NFSATTRBIT_ACL);
218 accmode |= VREAD_ACL;
220 if (NFSNONZERO_ATTRBIT(&tmpbits))
221 accmode |= VREAD_ATTRIBUTES;
223 nd->nd_repstat = nfsvno_accchk(vp, accmode,
224 nd->nd_cred, exp, p, NFSACCCHK_NOOVERRIDE,
225 NFSACCCHK_VPISLOCKED, NULL);
229 nd->nd_repstat = nfsvno_getattr(vp, &nva, nd->nd_cred, p, 1);
230 if (!nd->nd_repstat) {
231 if (nd->nd_flag & ND_NFSV4) {
232 if (NFSISSET_ATTRBIT(&attrbits, NFSATTRBIT_FILEHANDLE))
233 nd->nd_repstat = nfsvno_getfh(vp, &fh, p);
235 nd->nd_repstat = nfsrv_checkgetattr(nd, vp,
236 &nva, &attrbits, nd->nd_cred, p);
237 if (nd->nd_repstat == 0) {
238 supports_nfsv4acls = nfs_supportsnfsv4acls(vp);
240 if (nfsrv_enable_crossmntpt != 0 &&
241 vp->v_type == VDIR &&
242 (vp->v_vflag & VV_ROOT) != 0 &&
244 tvp = mp->mnt_vnodecovered;
252 if ((nd->nd_repstat =
253 NFSVOPLOCK(tvp, LK_SHARED)) == 0) {
254 nd->nd_repstat = VOP_GETATTR(
255 tvp, &va, nd->nd_cred);
259 if (nd->nd_repstat == 0)
260 mounted_on_fileno = (uint64_t)
265 if (nd->nd_repstat == 0)
266 nd->nd_repstat = vfs_busy(mp, 0);
268 if (nd->nd_repstat == 0) {
269 (void)nfsvno_fillattr(nd, mp, vp, &nva,
270 &fh, 0, &attrbits, nd->nd_cred, p,
271 isdgram, 1, supports_nfsv4acls,
272 at_root, mounted_on_fileno);
279 nfsrv_fillattr(nd, &nva);
287 NFSEXITCODE2(error, nd);
292 * nfs setattr service
295 nfsrvd_setattr(struct nfsrv_descript *nd, __unused int isdgram,
296 vnode_t vp, NFSPROC_T *p, struct nfsexstuff *exp)
298 struct nfsvattr nva, nva2;
300 int preat_ret = 1, postat_ret = 1, gcheck = 0, error = 0;
301 struct timespec guard = { 0, 0 };
302 nfsattrbit_t attrbits, retbits;
303 nfsv4stateid_t stateid;
304 NFSACL_T *aclp = NULL;
306 if (nd->nd_repstat) {
307 nfsrv_wcc(nd, preat_ret, &nva2, postat_ret, &nva);
310 #ifdef NFS4_ACL_EXTATTR_NAME
311 aclp = acl_alloc(M_WAITOK);
314 NFSVNO_ATTRINIT(&nva);
315 NFSZERO_ATTRBIT(&retbits);
316 if (nd->nd_flag & ND_NFSV4) {
317 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID);
318 stateid.seqid = fxdr_unsigned(u_int32_t, *tl++);
319 NFSBCOPY((caddr_t)tl,(caddr_t)stateid.other,NFSX_STATEIDOTHER);
321 error = nfsrv_sattr(nd, &nva, &attrbits, aclp, p);
324 preat_ret = nfsvno_getattr(vp, &nva2, nd->nd_cred, p, 1);
326 nd->nd_repstat = preat_ret;
327 if (nd->nd_flag & ND_NFSV3) {
328 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
329 gcheck = fxdr_unsigned(int, *tl);
331 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
332 fxdr_nfsv3time(tl, &guard);
334 if (!nd->nd_repstat && gcheck &&
335 (nva2.na_ctime.tv_sec != guard.tv_sec ||
336 nva2.na_ctime.tv_nsec != guard.tv_nsec))
337 nd->nd_repstat = NFSERR_NOT_SYNC;
338 if (nd->nd_repstat) {
340 #ifdef NFS4_ACL_EXTATTR_NAME
343 nfsrv_wcc(nd, preat_ret, &nva2, postat_ret, &nva);
346 } else if (!nd->nd_repstat && (nd->nd_flag & ND_NFSV4))
347 nd->nd_repstat = nfsrv_checkuidgid(nd, &nva);
350 * Now that we have all the fields, lets do it.
351 * If the size is being changed write access is required, otherwise
352 * just check for a read only file system.
354 if (!nd->nd_repstat) {
355 if (NFSVNO_NOTSETSIZE(&nva)) {
356 if (NFSVNO_EXRDONLY(exp) ||
357 (vfs_flags(vnode_mount(vp)) & MNT_RDONLY))
358 nd->nd_repstat = EROFS;
360 if (vnode_vtype(vp) != VREG)
361 nd->nd_repstat = EINVAL;
362 else if (nva2.na_uid != nd->nd_cred->cr_uid ||
363 NFSVNO_EXSTRICTACCESS(exp))
364 nd->nd_repstat = nfsvno_accchk(vp,
365 VWRITE, nd->nd_cred, exp, p,
366 NFSACCCHK_NOOVERRIDE,
367 NFSACCCHK_VPISLOCKED, NULL);
370 if (!nd->nd_repstat && (nd->nd_flag & ND_NFSV4))
371 nd->nd_repstat = nfsrv_checksetattr(vp, nd, &stateid,
372 &nva, &attrbits, exp, p);
374 if (!nd->nd_repstat && (nd->nd_flag & ND_NFSV4)) {
376 * For V4, try setting the attrbutes in sets, so that the
377 * reply bitmap will be correct for an error case.
379 if (NFSISSET_ATTRBIT(&attrbits, NFSATTRBIT_OWNER) ||
380 NFSISSET_ATTRBIT(&attrbits, NFSATTRBIT_OWNERGROUP)) {
381 NFSVNO_ATTRINIT(&nva2);
382 NFSVNO_SETATTRVAL(&nva2, uid, nva.na_uid);
383 NFSVNO_SETATTRVAL(&nva2, gid, nva.na_gid);
384 nd->nd_repstat = nfsvno_setattr(vp, &nva2, nd->nd_cred, p,
386 if (!nd->nd_repstat) {
387 if (NFSISSET_ATTRBIT(&attrbits, NFSATTRBIT_OWNER))
388 NFSSETBIT_ATTRBIT(&retbits, NFSATTRBIT_OWNER);
389 if (NFSISSET_ATTRBIT(&attrbits, NFSATTRBIT_OWNERGROUP))
390 NFSSETBIT_ATTRBIT(&retbits, NFSATTRBIT_OWNERGROUP);
393 if (!nd->nd_repstat &&
394 NFSISSET_ATTRBIT(&attrbits, NFSATTRBIT_SIZE)) {
395 NFSVNO_ATTRINIT(&nva2);
396 NFSVNO_SETATTRVAL(&nva2, size, nva.na_size);
397 nd->nd_repstat = nfsvno_setattr(vp, &nva2, nd->nd_cred, p,
400 NFSSETBIT_ATTRBIT(&retbits, NFSATTRBIT_SIZE);
402 if (!nd->nd_repstat &&
403 (NFSISSET_ATTRBIT(&attrbits, NFSATTRBIT_TIMEACCESSSET) ||
404 NFSISSET_ATTRBIT(&attrbits, NFSATTRBIT_TIMEMODIFYSET))) {
405 NFSVNO_ATTRINIT(&nva2);
406 NFSVNO_SETATTRVAL(&nva2, atime, nva.na_atime);
407 NFSVNO_SETATTRVAL(&nva2, mtime, nva.na_mtime);
408 if (nva.na_vaflags & VA_UTIMES_NULL) {
409 nva2.na_vaflags |= VA_UTIMES_NULL;
410 NFSVNO_SETACTIVE(&nva2, vaflags);
412 nd->nd_repstat = nfsvno_setattr(vp, &nva2, nd->nd_cred, p,
414 if (!nd->nd_repstat) {
415 if (NFSISSET_ATTRBIT(&attrbits, NFSATTRBIT_TIMEACCESSSET))
416 NFSSETBIT_ATTRBIT(&retbits, NFSATTRBIT_TIMEACCESSSET);
417 if (NFSISSET_ATTRBIT(&attrbits, NFSATTRBIT_TIMEMODIFYSET))
418 NFSSETBIT_ATTRBIT(&retbits, NFSATTRBIT_TIMEMODIFYSET);
421 if (!nd->nd_repstat &&
422 NFSISSET_ATTRBIT(&attrbits, NFSATTRBIT_MODE)) {
423 NFSVNO_ATTRINIT(&nva2);
424 NFSVNO_SETATTRVAL(&nva2, mode, nva.na_mode);
425 nd->nd_repstat = nfsvno_setattr(vp, &nva2, nd->nd_cred, p,
428 NFSSETBIT_ATTRBIT(&retbits, NFSATTRBIT_MODE);
431 #ifdef NFS4_ACL_EXTATTR_NAME
432 if (!nd->nd_repstat && aclp->acl_cnt > 0 &&
433 NFSISSET_ATTRBIT(&attrbits, NFSATTRBIT_ACL)) {
434 nd->nd_repstat = nfsrv_setacl(vp, aclp, nd->nd_cred, p);
436 NFSSETBIT_ATTRBIT(&retbits, NFSATTRBIT_ACL);
439 } else if (!nd->nd_repstat) {
440 nd->nd_repstat = nfsvno_setattr(vp, &nva, nd->nd_cred, p,
443 if (nd->nd_flag & (ND_NFSV2 | ND_NFSV3)) {
444 postat_ret = nfsvno_getattr(vp, &nva, nd->nd_cred, p, 1);
446 nd->nd_repstat = postat_ret;
449 #ifdef NFS4_ACL_EXTATTR_NAME
452 if (nd->nd_flag & ND_NFSV3)
453 nfsrv_wcc(nd, preat_ret, &nva2, postat_ret, &nva);
454 else if (nd->nd_flag & ND_NFSV4)
455 (void) nfsrv_putattrbit(nd, &retbits);
456 else if (!nd->nd_repstat)
457 nfsrv_fillattr(nd, &nva);
464 #ifdef NFS4_ACL_EXTATTR_NAME
467 if (nd->nd_flag & ND_NFSV4) {
469 * For all nd_repstat, the V4 reply includes a bitmap,
470 * even NFSERR_BADXDR, which is what this will end up
473 (void) nfsrv_putattrbit(nd, &retbits);
475 NFSEXITCODE2(error, nd);
481 * (Also performs lookup parent for v4)
484 nfsrvd_lookup(struct nfsrv_descript *nd, __unused int isdgram,
485 vnode_t dp, vnode_t *vpp, fhandle_t *fhp, NFSPROC_T *p,
486 struct nfsexstuff *exp)
488 struct nameidata named;
489 vnode_t vp, dirp = NULL;
490 int error = 0, dattr_ret = 1;
491 struct nfsvattr nva, dattr;
495 if (nd->nd_repstat) {
496 nfsrv_postopattr(nd, dattr_ret, &dattr);
501 * For some reason, if dp is a symlink, the error
502 * returned is supposed to be NFSERR_SYMLINK and not NFSERR_NOTDIR.
504 if (dp->v_type == VLNK && (nd->nd_flag & ND_NFSV4)) {
505 nd->nd_repstat = NFSERR_SYMLINK;
510 NFSNAMEICNDSET(&named.ni_cnd, nd->nd_cred, LOOKUP,
511 LOCKLEAF | SAVESTART);
512 nfsvno_setpathbuf(&named, &bufp, &hashp);
513 error = nfsrv_parsename(nd, bufp, hashp, &named.ni_pathlen);
516 nfsvno_relpathbuf(&named);
519 if (!nd->nd_repstat) {
520 nd->nd_repstat = nfsvno_namei(nd, &named, dp, 0, exp, p, &dirp);
523 nfsvno_relpathbuf(&named);
525 if (nd->nd_repstat) {
527 if (nd->nd_flag & ND_NFSV3)
528 dattr_ret = nfsvno_getattr(dirp, &dattr,
532 if (nd->nd_flag & ND_NFSV3)
533 nfsrv_postopattr(nd, dattr_ret, &dattr);
536 if (named.ni_startdir)
537 vrele(named.ni_startdir);
538 nfsvno_relpathbuf(&named);
540 if ((nd->nd_flag & ND_NFSV4) != 0 && !NFSVNO_EXPORTED(exp) &&
541 vp->v_type != VDIR && vp->v_type != VLNK)
543 * Only allow lookup of VDIR and VLNK for traversal of
544 * non-exported volumes during NFSv4 mounting.
546 nd->nd_repstat = ENOENT;
547 if (nd->nd_repstat == 0)
548 nd->nd_repstat = nfsvno_getfh(vp, fhp, p);
549 if (!(nd->nd_flag & ND_NFSV4) && !nd->nd_repstat)
550 nd->nd_repstat = nfsvno_getattr(vp, &nva, nd->nd_cred, p, 1);
551 if (vpp != NULL && nd->nd_repstat == 0)
556 if (nd->nd_flag & ND_NFSV3)
557 dattr_ret = nfsvno_getattr(dirp, &dattr, nd->nd_cred,
561 if (nd->nd_repstat) {
562 if (nd->nd_flag & ND_NFSV3)
563 nfsrv_postopattr(nd, dattr_ret, &dattr);
566 if (nd->nd_flag & ND_NFSV2) {
567 (void) nfsm_fhtom(nd, (u_int8_t *)fhp, 0, 0);
568 nfsrv_fillattr(nd, &nva);
569 } else if (nd->nd_flag & ND_NFSV3) {
570 (void) nfsm_fhtom(nd, (u_int8_t *)fhp, 0, 0);
571 nfsrv_postopattr(nd, 0, &nva);
572 nfsrv_postopattr(nd, dattr_ret, &dattr);
576 NFSEXITCODE2(error, nd);
581 * nfs readlink service
584 nfsrvd_readlink(struct nfsrv_descript *nd, __unused int isdgram,
585 vnode_t vp, NFSPROC_T *p, __unused struct nfsexstuff *exp)
588 mbuf_t mp = NULL, mpend = NULL;
592 if (nd->nd_repstat) {
593 nfsrv_postopattr(nd, getret, &nva);
596 if (vnode_vtype(vp) != VLNK) {
597 if (nd->nd_flag & ND_NFSV2)
598 nd->nd_repstat = ENXIO;
600 nd->nd_repstat = EINVAL;
603 nd->nd_repstat = nfsvno_readlink(vp, nd->nd_cred, p,
605 if (nd->nd_flag & ND_NFSV3)
606 getret = nfsvno_getattr(vp, &nva, nd->nd_cred, p, 1);
608 if (nd->nd_flag & ND_NFSV3)
609 nfsrv_postopattr(nd, getret, &nva);
612 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
613 *tl = txdr_unsigned(len);
614 mbuf_setnext(nd->nd_mb, mp);
616 nd->nd_bpos = NFSMTOD(mpend, caddr_t) + mbuf_len(mpend);
627 nfsrvd_read(struct nfsrv_descript *nd, __unused int isdgram,
628 vnode_t vp, NFSPROC_T *p, struct nfsexstuff *exp)
631 int error = 0, cnt, getret = 1, reqlen, eof = 0;
635 struct nfsstate st, *stp = &st;
636 struct nfslock lo, *lop = &lo;
637 nfsv4stateid_t stateid;
640 if (nd->nd_repstat) {
641 nfsrv_postopattr(nd, getret, &nva);
644 if (nd->nd_flag & ND_NFSV2) {
645 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
646 off = (off_t)fxdr_unsigned(u_int32_t, *tl++);
647 reqlen = fxdr_unsigned(int, *tl);
648 } else if (nd->nd_flag & ND_NFSV3) {
649 NFSM_DISSECT(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
650 off = fxdr_hyper(tl);
652 reqlen = fxdr_unsigned(int, *tl);
654 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID + 3*NFSX_UNSIGNED);
655 reqlen = fxdr_unsigned(int, *(tl + 6));
657 if (reqlen > NFS_SRVMAXDATA(nd)) {
658 reqlen = NFS_SRVMAXDATA(nd);
659 } else if (reqlen < 0) {
663 if (nd->nd_flag & ND_NFSV4) {
664 stp->ls_flags = (NFSLCK_CHECK | NFSLCK_READACCESS);
665 lop->lo_flags = NFSLCK_READ;
666 stp->ls_ownerlen = 0;
668 stp->ls_uid = nd->nd_cred->cr_uid;
669 stp->ls_stateid.seqid = fxdr_unsigned(u_int32_t, *tl++);
670 clientid.lval[0] = stp->ls_stateid.other[0] = *tl++;
671 clientid.lval[1] = stp->ls_stateid.other[1] = *tl++;
672 if ((nd->nd_flag & ND_IMPLIEDCLID) != 0) {
673 if ((nd->nd_flag & ND_NFSV41) != 0)
674 clientid.qval = nd->nd_clientid.qval;
675 else if (nd->nd_clientid.qval != clientid.qval)
676 printf("EEK1 multiple clids\n");
678 if ((nd->nd_flag & ND_NFSV41) != 0)
679 printf("EEK! no clientid from session\n");
680 nd->nd_flag |= ND_IMPLIEDCLID;
681 nd->nd_clientid.qval = clientid.qval;
683 stp->ls_stateid.other[2] = *tl++;
684 off = fxdr_hyper(tl);
687 lop->lo_end = off + reqlen;
689 * Paranoia, just in case it wraps around.
691 if (lop->lo_end < off)
692 lop->lo_end = NFS64BITSSET;
694 if (vnode_vtype(vp) != VREG) {
695 if (nd->nd_flag & ND_NFSV3)
696 nd->nd_repstat = EINVAL;
698 nd->nd_repstat = (vnode_vtype(vp) == VDIR) ? EISDIR :
701 getret = nfsvno_getattr(vp, &nva, nd->nd_cred, p, 1);
703 nd->nd_repstat = getret;
704 if (!nd->nd_repstat &&
705 (nva.na_uid != nd->nd_cred->cr_uid ||
706 NFSVNO_EXSTRICTACCESS(exp))) {
707 nd->nd_repstat = nfsvno_accchk(vp, VREAD,
709 NFSACCCHK_ALLOWOWNER, NFSACCCHK_VPISLOCKED, NULL);
711 nd->nd_repstat = nfsvno_accchk(vp, VEXEC,
712 nd->nd_cred, exp, p, NFSACCCHK_ALLOWOWNER,
713 NFSACCCHK_VPISLOCKED, NULL);
715 if ((nd->nd_flag & ND_NFSV4) && !nd->nd_repstat)
716 nd->nd_repstat = nfsrv_lockctrl(vp, &stp, &lop, NULL, clientid,
717 &stateid, exp, nd, p);
718 if (nd->nd_repstat) {
720 if (nd->nd_flag & ND_NFSV3)
721 nfsrv_postopattr(nd, getret, &nva);
724 if (off >= nva.na_size) {
727 } else if (reqlen == 0)
729 else if ((off + reqlen) >= nva.na_size) {
730 cnt = nva.na_size - off;
736 nd->nd_repstat = nfsvno_read(vp, off, cnt, nd->nd_cred, p,
738 if (!(nd->nd_flag & ND_NFSV4)) {
739 getret = nfsvno_getattr(vp, &nva, nd->nd_cred, p, 1);
741 nd->nd_repstat = getret;
743 if (nd->nd_repstat) {
747 if (nd->nd_flag & ND_NFSV3)
748 nfsrv_postopattr(nd, getret, &nva);
753 if (nd->nd_flag & ND_NFSV2) {
754 nfsrv_fillattr(nd, &nva);
755 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
757 if (nd->nd_flag & ND_NFSV3) {
758 nfsrv_postopattr(nd, getret, &nva);
759 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
760 *tl++ = txdr_unsigned(cnt);
762 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
766 *tl++ = newnfs_false;
768 *tl = txdr_unsigned(cnt);
770 mbuf_setnext(nd->nd_mb, m3);
772 nd->nd_bpos = NFSMTOD(m2, caddr_t) + mbuf_len(m2);
780 NFSEXITCODE2(error, nd);
788 nfsrvd_write(struct nfsrv_descript *nd, __unused int isdgram,
789 vnode_t vp, NFSPROC_T *p, struct nfsexstuff *exp)
794 struct nfsvattr nva, forat;
795 int aftat_ret = 1, retlen, len, error = 0, forat_ret = 1;
796 int stable = NFSWRITE_FILESYNC;
798 struct nfsstate st, *stp = &st;
799 struct nfslock lo, *lop = &lo;
800 nfsv4stateid_t stateid;
803 if (nd->nd_repstat) {
804 nfsrv_wcc(nd, forat_ret, &forat, aftat_ret, &nva);
807 if (nd->nd_flag & ND_NFSV2) {
808 NFSM_DISSECT(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
809 off = (off_t)fxdr_unsigned(u_int32_t, *++tl);
811 retlen = len = fxdr_unsigned(int32_t, *tl);
812 } else if (nd->nd_flag & ND_NFSV3) {
813 NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
814 off = fxdr_hyper(tl);
816 stable = fxdr_unsigned(int, *tl++);
817 retlen = len = fxdr_unsigned(int32_t, *tl);
819 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID + 4 * NFSX_UNSIGNED);
820 stp->ls_flags = (NFSLCK_CHECK | NFSLCK_WRITEACCESS);
821 lop->lo_flags = NFSLCK_WRITE;
822 stp->ls_ownerlen = 0;
824 stp->ls_uid = nd->nd_cred->cr_uid;
825 stp->ls_stateid.seqid = fxdr_unsigned(u_int32_t, *tl++);
826 clientid.lval[0] = stp->ls_stateid.other[0] = *tl++;
827 clientid.lval[1] = stp->ls_stateid.other[1] = *tl++;
828 if ((nd->nd_flag & ND_IMPLIEDCLID) != 0) {
829 if ((nd->nd_flag & ND_NFSV41) != 0)
830 clientid.qval = nd->nd_clientid.qval;
831 else if (nd->nd_clientid.qval != clientid.qval)
832 printf("EEK2 multiple clids\n");
834 if ((nd->nd_flag & ND_NFSV41) != 0)
835 printf("EEK! no clientid from session\n");
836 nd->nd_flag |= ND_IMPLIEDCLID;
837 nd->nd_clientid.qval = clientid.qval;
839 stp->ls_stateid.other[2] = *tl++;
840 off = fxdr_hyper(tl);
843 stable = fxdr_unsigned(int, *tl++);
844 retlen = len = fxdr_unsigned(int32_t, *tl);
845 lop->lo_end = off + len;
847 * Paranoia, just in case it wraps around, which shouldn't
848 * ever happen anyhow.
850 if (lop->lo_end < lop->lo_first)
851 lop->lo_end = NFS64BITSSET;
855 * Loop through the mbuf chain, counting how many mbufs are a
856 * part of this write operation, so the iovec size is known.
860 i = NFSMTOD(mp, caddr_t) + mbuf_len(mp) - nd->nd_dpos;
876 if (retlen > NFS_SRVMAXIO || retlen < 0)
877 nd->nd_repstat = EIO;
878 if (vnode_vtype(vp) != VREG && !nd->nd_repstat) {
879 if (nd->nd_flag & ND_NFSV3)
880 nd->nd_repstat = EINVAL;
882 nd->nd_repstat = (vnode_vtype(vp) == VDIR) ? EISDIR :
885 forat_ret = nfsvno_getattr(vp, &forat, nd->nd_cred, p, 1);
887 nd->nd_repstat = forat_ret;
888 if (!nd->nd_repstat &&
889 (forat.na_uid != nd->nd_cred->cr_uid ||
890 NFSVNO_EXSTRICTACCESS(exp)))
891 nd->nd_repstat = nfsvno_accchk(vp, VWRITE,
893 NFSACCCHK_ALLOWOWNER, NFSACCCHK_VPISLOCKED, NULL);
894 if ((nd->nd_flag & ND_NFSV4) && !nd->nd_repstat) {
895 nd->nd_repstat = nfsrv_lockctrl(vp, &stp, &lop, NULL, clientid,
896 &stateid, exp, nd, p);
898 if (nd->nd_repstat) {
900 if (nd->nd_flag & ND_NFSV3)
901 nfsrv_wcc(nd, forat_ret, &forat, aftat_ret, &nva);
906 * For NFS Version 2, it is not obvious what a write of zero length
907 * should do, but I might as well be consistent with Version 3,
908 * which is to return ok so long as there are no permission problems.
911 nd->nd_repstat = nfsvno_write(vp, off, retlen, cnt, stable,
912 nd->nd_md, nd->nd_dpos, nd->nd_cred, p);
913 error = nfsm_advance(nd, NFSM_RNDUP(retlen), -1);
917 if (nd->nd_flag & ND_NFSV4)
920 aftat_ret = nfsvno_getattr(vp, &nva, nd->nd_cred, p, 1);
923 nd->nd_repstat = aftat_ret;
924 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) {
925 if (nd->nd_flag & ND_NFSV3)
926 nfsrv_wcc(nd, forat_ret, &forat, aftat_ret, &nva);
929 NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
930 *tl++ = txdr_unsigned(retlen);
932 * If nfs_async is set, then pretend the write was FILESYNC.
933 * Warning: Doing this violates RFC1813 and runs a risk
934 * of data written by a client being lost when the server
937 if (stable == NFSWRITE_UNSTABLE && nfs_async == 0)
938 *tl++ = txdr_unsigned(stable);
940 *tl++ = txdr_unsigned(NFSWRITE_FILESYNC);
942 * Actually, there is no need to txdr these fields,
943 * but it may make the values more human readable,
944 * for debugging purposes.
946 *tl++ = txdr_unsigned(nfsboottime.tv_sec);
947 *tl = txdr_unsigned(nfsboottime.tv_usec);
948 } else if (!nd->nd_repstat)
949 nfsrv_fillattr(nd, &nva);
956 NFSEXITCODE2(error, nd);
961 * nfs create service (creates regular files for V2 and V3. Spec. files for V2.)
962 * now does a truncate to 0 length via. setattr if it already exists
963 * The core creation routine has been extracted out into nfsrv_creatsub(),
964 * so it can also be used by nfsrv_open() for V4.
967 nfsrvd_create(struct nfsrv_descript *nd, __unused int isdgram,
968 vnode_t dp, NFSPROC_T *p, struct nfsexstuff *exp)
970 struct nfsvattr nva, dirfor, diraft;
971 struct nfsv2_sattr *sp;
972 struct nameidata named;
974 int error = 0, tsize, dirfor_ret = 1, diraft_ret = 1;
975 int how = NFSCREATE_UNCHECKED, exclusive_flag = 0;
977 vnode_t vp = NULL, dirp = NULL;
982 int32_t cverf[2], tverf[2] = { 0, 0 };
984 if (nd->nd_repstat) {
985 nfsrv_wcc(nd, dirfor_ret, &dirfor, diraft_ret, &diraft);
988 NFSNAMEICNDSET(&named.ni_cnd, nd->nd_cred, CREATE,
989 LOCKPARENT | LOCKLEAF | SAVESTART | NOCACHE);
990 nfsvno_setpathbuf(&named, &bufp, &hashp);
991 error = nfsrv_parsename(nd, bufp, hashp, &named.ni_pathlen);
994 if (!nd->nd_repstat) {
995 NFSVNO_ATTRINIT(&nva);
996 if (nd->nd_flag & ND_NFSV2) {
997 NFSM_DISSECT(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
998 vtyp = IFTOVT(fxdr_unsigned(u_int32_t, sp->sa_mode));
1001 NFSVNO_SETATTRVAL(&nva, type, vtyp);
1002 NFSVNO_SETATTRVAL(&nva, mode,
1003 nfstov_mode(sp->sa_mode));
1004 switch (nva.na_type) {
1006 tsize = fxdr_unsigned(int32_t, sp->sa_size);
1008 NFSVNO_SETATTRVAL(&nva, size,
1014 rdev = fxdr_unsigned(NFSDEV_T, sp->sa_size);
1020 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
1021 how = fxdr_unsigned(int, *tl);
1023 case NFSCREATE_GUARDED:
1024 case NFSCREATE_UNCHECKED:
1025 error = nfsrv_sattr(nd, &nva, NULL, NULL, p);
1029 case NFSCREATE_EXCLUSIVE:
1030 NFSM_DISSECT(tl, u_int32_t *, NFSX_VERF);
1036 NFSVNO_SETATTRVAL(&nva, type, VREG);
1039 if (nd->nd_repstat) {
1040 nfsvno_relpathbuf(&named);
1041 if (nd->nd_flag & ND_NFSV3) {
1042 dirfor_ret = nfsvno_getattr(dp, &dirfor, nd->nd_cred,
1044 nfsrv_wcc(nd, dirfor_ret, &dirfor, diraft_ret,
1051 nd->nd_repstat = nfsvno_namei(nd, &named, dp, 1, exp, p, &dirp);
1053 if (nd->nd_flag & ND_NFSV2) {
1057 dirfor_ret = nfsvno_getattr(dirp, &dirfor, nd->nd_cred,
1061 if (nd->nd_repstat) {
1062 if (nd->nd_flag & ND_NFSV3)
1063 nfsrv_wcc(nd, dirfor_ret, &dirfor, diraft_ret,
1070 if (!(nd->nd_flag & ND_NFSV2)) {
1072 case NFSCREATE_GUARDED:
1074 nd->nd_repstat = EEXIST;
1076 case NFSCREATE_UNCHECKED:
1078 case NFSCREATE_EXCLUSIVE:
1079 if (named.ni_vp == NULL)
1080 NFSVNO_SETATTRVAL(&nva, mode, 0);
1086 * Iff doesn't exist, create it
1087 * otherwise just truncate to 0 length
1088 * should I set the mode too ?
1090 nd->nd_repstat = nfsvno_createsub(nd, &named, &vp, &nva,
1091 &exclusive_flag, cverf, rdev, p, exp);
1093 if (!nd->nd_repstat) {
1094 nd->nd_repstat = nfsvno_getfh(vp, &fh, p);
1095 if (!nd->nd_repstat)
1096 nd->nd_repstat = nfsvno_getattr(vp, &nva, nd->nd_cred,
1099 if (!nd->nd_repstat) {
1100 tverf[0] = nva.na_atime.tv_sec;
1101 tverf[1] = nva.na_atime.tv_nsec;
1104 if (nd->nd_flag & ND_NFSV2) {
1105 if (!nd->nd_repstat) {
1106 (void) nfsm_fhtom(nd, (u_int8_t *)&fh, 0, 0);
1107 nfsrv_fillattr(nd, &nva);
1110 if (exclusive_flag && !nd->nd_repstat && (cverf[0] != tverf[0]
1111 || cverf[1] != tverf[1]))
1112 nd->nd_repstat = EEXIST;
1113 diraft_ret = nfsvno_getattr(dirp, &diraft, nd->nd_cred, p, 0);
1115 if (!nd->nd_repstat) {
1116 (void) nfsm_fhtom(nd, (u_int8_t *)&fh, 0, 1);
1117 nfsrv_postopattr(nd, 0, &nva);
1119 nfsrv_wcc(nd, dirfor_ret, &dirfor, diraft_ret, &diraft);
1123 NFSEXITCODE2(0, nd);
1127 nfsvno_relpathbuf(&named);
1128 NFSEXITCODE2(error, nd);
1133 * nfs v3 mknod service (and v4 create)
1136 nfsrvd_mknod(struct nfsrv_descript *nd, __unused int isdgram,
1137 vnode_t dp, vnode_t *vpp, fhandle_t *fhp, NFSPROC_T *p,
1138 struct nfsexstuff *exp)
1140 struct nfsvattr nva, dirfor, diraft;
1142 struct nameidata named;
1143 int error = 0, dirfor_ret = 1, diraft_ret = 1, pathlen;
1144 u_int32_t major, minor;
1145 enum vtype vtyp = VNON;
1146 nfstype nfs4type = NFNON;
1147 vnode_t vp, dirp = NULL;
1148 nfsattrbit_t attrbits;
1149 char *bufp = NULL, *pathcp = NULL;
1150 u_long *hashp, cnflags;
1151 NFSACL_T *aclp = NULL;
1153 NFSVNO_ATTRINIT(&nva);
1154 cnflags = (LOCKPARENT | SAVESTART);
1155 if (nd->nd_repstat) {
1156 nfsrv_wcc(nd, dirfor_ret, &dirfor, diraft_ret, &diraft);
1159 #ifdef NFS4_ACL_EXTATTR_NAME
1160 aclp = acl_alloc(M_WAITOK);
1165 * For V4, the creation stuff is here, Yuck!
1167 if (nd->nd_flag & ND_NFSV4) {
1168 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
1169 vtyp = nfsv34tov_type(*tl);
1170 nfs4type = fxdr_unsigned(nfstype, *tl);
1173 error = nfsvno_getsymlink(nd, &nva, p, &pathcp,
1180 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1181 major = fxdr_unsigned(u_int32_t, *tl++);
1182 minor = fxdr_unsigned(u_int32_t, *tl);
1183 nva.na_rdev = NFSMAKEDEV(major, minor);
1189 cnflags = (LOCKPARENT | SAVENAME);
1192 nd->nd_repstat = NFSERR_BADTYPE;
1194 #ifdef NFS4_ACL_EXTATTR_NAME
1200 NFSNAMEICNDSET(&named.ni_cnd, nd->nd_cred, CREATE, cnflags | NOCACHE);
1201 nfsvno_setpathbuf(&named, &bufp, &hashp);
1202 error = nfsrv_parsename(nd, bufp, hashp, &named.ni_pathlen);
1205 if (!nd->nd_repstat) {
1206 if (nd->nd_flag & ND_NFSV3) {
1207 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
1208 vtyp = nfsv34tov_type(*tl);
1210 error = nfsrv_sattr(nd, &nva, &attrbits, aclp, p);
1214 if (!nd->nd_repstat && (nd->nd_flag & ND_NFSV3) &&
1215 (vtyp == VCHR || vtyp == VBLK)) {
1216 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1217 major = fxdr_unsigned(u_int32_t, *tl++);
1218 minor = fxdr_unsigned(u_int32_t, *tl);
1219 nva.na_rdev = NFSMAKEDEV(major, minor);
1223 dirfor_ret = nfsvno_getattr(dp, &dirfor, nd->nd_cred, p, 0);
1224 if (!nd->nd_repstat && (nd->nd_flag & ND_NFSV4)) {
1225 if (!dirfor_ret && NFSVNO_ISSETGID(&nva) &&
1226 dirfor.na_gid == nva.na_gid)
1227 NFSVNO_UNSET(&nva, gid);
1228 nd->nd_repstat = nfsrv_checkuidgid(nd, &nva);
1230 if (nd->nd_repstat) {
1232 #ifdef NFS4_ACL_EXTATTR_NAME
1235 nfsvno_relpathbuf(&named);
1237 FREE(pathcp, M_TEMP);
1238 if (nd->nd_flag & ND_NFSV3)
1239 nfsrv_wcc(nd, dirfor_ret, &dirfor, diraft_ret,
1245 * Yuck! For V4, mkdir and link are here and some V4 clients don't fill
1246 * in va_mode, so we'll have to set a default here.
1248 if (NFSVNO_NOTSETMODE(&nva)) {
1256 named.ni_cnd.cn_flags |= WILLBEDIR;
1257 nd->nd_repstat = nfsvno_namei(nd, &named, dp, 0, exp, p, &dirp);
1258 if (nd->nd_repstat) {
1260 if (nd->nd_flag & ND_NFSV3)
1261 dirfor_ret = nfsvno_getattr(dirp, &dirfor,
1265 #ifdef NFS4_ACL_EXTATTR_NAME
1268 if (nd->nd_flag & ND_NFSV3)
1269 nfsrv_wcc(nd, dirfor_ret, &dirfor, diraft_ret,
1274 dirfor_ret = nfsvno_getattr(dirp, &dirfor, nd->nd_cred, p, 0);
1276 if ((nd->nd_flag & ND_NFSV4) && (vtyp == VDIR || vtyp == VLNK)) {
1278 nfsrvd_mkdirsub(nd, &named, &nva, fhp, vpp, dirp,
1279 &dirfor, &diraft, &diraft_ret, &attrbits, aclp, p,
1281 #ifdef NFS4_ACL_EXTATTR_NAME
1285 } else if (vtyp == VLNK) {
1286 nfsrvd_symlinksub(nd, &named, &nva, fhp, vpp, dirp,
1287 &dirfor, &diraft, &diraft_ret, &attrbits,
1288 aclp, p, exp, pathcp, pathlen);
1289 #ifdef NFS4_ACL_EXTATTR_NAME
1292 FREE(pathcp, M_TEMP);
1297 nd->nd_repstat = nfsvno_mknod(&named, &nva, nd->nd_cred, p);
1298 if (!nd->nd_repstat) {
1300 nfsrv_fixattr(nd, vp, &nva, aclp, p, &attrbits, exp);
1301 nd->nd_repstat = nfsvno_getfh(vp, fhp, p);
1302 if ((nd->nd_flag & ND_NFSV3) && !nd->nd_repstat)
1303 nd->nd_repstat = nfsvno_getattr(vp, &nva, nd->nd_cred,
1305 if (vpp != NULL && nd->nd_repstat == 0) {
1306 NFSVOPUNLOCK(vp, 0);
1312 diraft_ret = nfsvno_getattr(dirp, &diraft, nd->nd_cred, p, 0);
1314 if (!nd->nd_repstat) {
1315 if (nd->nd_flag & ND_NFSV3) {
1316 (void) nfsm_fhtom(nd, (u_int8_t *)fhp, 0, 1);
1317 nfsrv_postopattr(nd, 0, &nva);
1319 NFSM_BUILD(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
1320 *tl++ = newnfs_false;
1321 txdr_hyper(dirfor.na_filerev, tl);
1323 txdr_hyper(diraft.na_filerev, tl);
1324 (void) nfsrv_putattrbit(nd, &attrbits);
1327 if (nd->nd_flag & ND_NFSV3)
1328 nfsrv_wcc(nd, dirfor_ret, &dirfor, diraft_ret, &diraft);
1329 #ifdef NFS4_ACL_EXTATTR_NAME
1334 NFSEXITCODE2(0, nd);
1338 #ifdef NFS4_ACL_EXTATTR_NAME
1342 nfsvno_relpathbuf(&named);
1344 FREE(pathcp, M_TEMP);
1346 NFSEXITCODE2(error, nd);
1351 * nfs remove service
1354 nfsrvd_remove(struct nfsrv_descript *nd, __unused int isdgram,
1355 vnode_t dp, NFSPROC_T *p, struct nfsexstuff *exp)
1357 struct nameidata named;
1359 int error = 0, dirfor_ret = 1, diraft_ret = 1;
1360 vnode_t dirp = NULL;
1361 struct nfsvattr dirfor, diraft;
1365 if (nd->nd_repstat) {
1366 nfsrv_wcc(nd, dirfor_ret, &dirfor, diraft_ret, &diraft);
1369 NFSNAMEICNDSET(&named.ni_cnd, nd->nd_cred, DELETE,
1370 LOCKPARENT | LOCKLEAF);
1371 nfsvno_setpathbuf(&named, &bufp, &hashp);
1372 error = nfsrv_parsename(nd, bufp, hashp, &named.ni_pathlen);
1375 nfsvno_relpathbuf(&named);
1378 if (!nd->nd_repstat) {
1379 nd->nd_repstat = nfsvno_namei(nd, &named, dp, 1, exp, p, &dirp);
1382 nfsvno_relpathbuf(&named);
1385 if (!(nd->nd_flag & ND_NFSV2)) {
1386 dirfor_ret = nfsvno_getattr(dirp, &dirfor,
1393 if (!nd->nd_repstat) {
1394 if (nd->nd_flag & ND_NFSV4) {
1395 if (vnode_vtype(named.ni_vp) == VDIR)
1396 nd->nd_repstat = nfsvno_rmdirsub(&named, 1,
1397 nd->nd_cred, p, exp);
1399 nd->nd_repstat = nfsvno_removesub(&named, 1,
1400 nd->nd_cred, p, exp);
1401 } else if (nd->nd_procnum == NFSPROC_RMDIR) {
1402 nd->nd_repstat = nfsvno_rmdirsub(&named, 0,
1403 nd->nd_cred, p, exp);
1405 nd->nd_repstat = nfsvno_removesub(&named, 0,
1406 nd->nd_cred, p, exp);
1409 if (!(nd->nd_flag & ND_NFSV2)) {
1411 diraft_ret = nfsvno_getattr(dirp, &diraft, nd->nd_cred,
1415 if (nd->nd_flag & ND_NFSV3) {
1416 nfsrv_wcc(nd, dirfor_ret, &dirfor, diraft_ret,
1418 } else if (!nd->nd_repstat) {
1419 NFSM_BUILD(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
1420 *tl++ = newnfs_false;
1421 txdr_hyper(dirfor.na_filerev, tl);
1423 txdr_hyper(diraft.na_filerev, tl);
1428 NFSEXITCODE2(error, nd);
1433 * nfs rename service
1436 nfsrvd_rename(struct nfsrv_descript *nd, int isdgram,
1437 vnode_t dp, vnode_t todp, NFSPROC_T *p, struct nfsexstuff *exp,
1438 struct nfsexstuff *toexp)
1441 int error = 0, fdirfor_ret = 1, fdiraft_ret = 1;
1442 int tdirfor_ret = 1, tdiraft_ret = 1;
1443 struct nameidata fromnd, tond;
1444 vnode_t fdirp = NULL, tdirp = NULL, tdp = NULL;
1445 struct nfsvattr fdirfor, fdiraft, tdirfor, tdiraft;
1446 struct nfsexstuff tnes;
1448 char *bufp, *tbufp = NULL;
1452 if (nd->nd_repstat) {
1453 nfsrv_wcc(nd, fdirfor_ret, &fdirfor, fdiraft_ret, &fdiraft);
1454 nfsrv_wcc(nd, tdirfor_ret, &tdirfor, tdiraft_ret, &tdiraft);
1457 if (!(nd->nd_flag & ND_NFSV2))
1458 fdirfor_ret = nfsvno_getattr(dp, &fdirfor, nd->nd_cred, p, 1);
1459 tond.ni_cnd.cn_nameiop = 0;
1460 tond.ni_startdir = NULL;
1461 NFSNAMEICNDSET(&fromnd.ni_cnd, nd->nd_cred, DELETE, WANTPARENT | SAVESTART);
1462 nfsvno_setpathbuf(&fromnd, &bufp, &hashp);
1463 error = nfsrv_parsename(nd, bufp, hashp, &fromnd.ni_pathlen);
1468 nfsvno_relpathbuf(&fromnd);
1472 * Unlock dp in this code section, so it is unlocked before
1473 * tdp gets locked. This avoids a potential LOR if tdp is the
1474 * parent directory of dp.
1476 if (nd->nd_flag & ND_NFSV4) {
1480 NFSVOPUNLOCK(dp, 0);
1481 tdirfor_ret = nfsvno_getattr(tdp, &tdirfor, nd->nd_cred,
1482 p, 0); /* Might lock tdp. */
1484 tdirfor_ret = nfsvno_getattr(tdp, &tdirfor, nd->nd_cred,
1486 NFSVOPUNLOCK(dp, 0);
1489 tfh.nfsrvfh_len = 0;
1490 error = nfsrv_mtofh(nd, &tfh);
1492 error = nfsvno_getfh(dp, &fh, p);
1495 /* todp is always NULL except NFSv4 */
1496 nfsvno_relpathbuf(&fromnd);
1500 /* If this is the same file handle, just VREF() the vnode. */
1501 if (tfh.nfsrvfh_len == NFSX_MYFH &&
1502 !NFSBCMP(tfh.nfsrvfh_data, &fh, NFSX_MYFH)) {
1506 tdirfor_ret = nfsvno_getattr(tdp, &tdirfor, nd->nd_cred,
1508 NFSVOPUNLOCK(dp, 0);
1510 NFSVOPUNLOCK(dp, 0);
1511 nd->nd_cred->cr_uid = nd->nd_saveduid;
1512 nfsd_fhtovp(nd, &tfh, LK_EXCLUSIVE, &tdp, &tnes, NULL,
1513 0, p); /* Locks tdp. */
1515 tdirfor_ret = nfsvno_getattr(tdp, &tdirfor,
1517 NFSVOPUNLOCK(tdp, 0);
1521 NFSNAMEICNDSET(&tond.ni_cnd, nd->nd_cred, RENAME, LOCKPARENT | LOCKLEAF | NOCACHE | SAVESTART);
1522 nfsvno_setpathbuf(&tond, &tbufp, &hashp);
1523 if (!nd->nd_repstat) {
1524 error = nfsrv_parsename(nd, tbufp, hashp, &tond.ni_pathlen);
1529 nfsvno_relpathbuf(&fromnd);
1530 nfsvno_relpathbuf(&tond);
1534 if (nd->nd_repstat) {
1535 if (nd->nd_flag & ND_NFSV3) {
1536 nfsrv_wcc(nd, fdirfor_ret, &fdirfor, fdiraft_ret,
1538 nfsrv_wcc(nd, tdirfor_ret, &tdirfor, tdiraft_ret,
1544 nfsvno_relpathbuf(&fromnd);
1545 nfsvno_relpathbuf(&tond);
1550 * Done parsing, now down to business.
1552 nd->nd_repstat = nfsvno_namei(nd, &fromnd, dp, 0, exp, p, &fdirp);
1553 if (nd->nd_repstat) {
1554 if (nd->nd_flag & ND_NFSV3) {
1555 nfsrv_wcc(nd, fdirfor_ret, &fdirfor, fdiraft_ret,
1557 nfsrv_wcc(nd, tdirfor_ret, &tdirfor, tdiraft_ret,
1564 nfsvno_relpathbuf(&tond);
1567 if (vnode_vtype(fromnd.ni_vp) == VDIR)
1568 tond.ni_cnd.cn_flags |= WILLBEDIR;
1569 nd->nd_repstat = nfsvno_namei(nd, &tond, tdp, 0, &tnes, p, &tdirp);
1570 nd->nd_repstat = nfsvno_rename(&fromnd, &tond, nd->nd_repstat,
1571 nd->nd_flag, nd->nd_cred, p);
1573 fdiraft_ret = nfsvno_getattr(fdirp, &fdiraft, nd->nd_cred, p,
1576 tdiraft_ret = nfsvno_getattr(tdirp, &tdiraft, nd->nd_cred, p,
1582 if (nd->nd_flag & ND_NFSV3) {
1583 nfsrv_wcc(nd, fdirfor_ret, &fdirfor, fdiraft_ret, &fdiraft);
1584 nfsrv_wcc(nd, tdirfor_ret, &tdirfor, tdiraft_ret, &tdiraft);
1585 } else if ((nd->nd_flag & ND_NFSV4) && !nd->nd_repstat) {
1586 NFSM_BUILD(tl, u_int32_t *, 10 * NFSX_UNSIGNED);
1587 *tl++ = newnfs_false;
1588 txdr_hyper(fdirfor.na_filerev, tl);
1590 txdr_hyper(fdiraft.na_filerev, tl);
1592 *tl++ = newnfs_false;
1593 txdr_hyper(tdirfor.na_filerev, tl);
1595 txdr_hyper(tdiraft.na_filerev, tl);
1599 NFSEXITCODE2(error, nd);
1607 nfsrvd_link(struct nfsrv_descript *nd, int isdgram,
1608 vnode_t vp, vnode_t tovp, NFSPROC_T *p, struct nfsexstuff *exp,
1609 struct nfsexstuff *toexp)
1611 struct nameidata named;
1613 int error = 0, dirfor_ret = 1, diraft_ret = 1, getret = 1;
1614 vnode_t dirp = NULL, dp = NULL;
1615 struct nfsvattr dirfor, diraft, at;
1616 struct nfsexstuff tnes;
1621 if (nd->nd_repstat) {
1622 nfsrv_postopattr(nd, getret, &at);
1623 nfsrv_wcc(nd, dirfor_ret, &dirfor, diraft_ret, &diraft);
1626 NFSVOPUNLOCK(vp, 0);
1627 if (vnode_vtype(vp) == VDIR) {
1628 if (nd->nd_flag & ND_NFSV4)
1629 nd->nd_repstat = NFSERR_ISDIR;
1631 nd->nd_repstat = NFSERR_INVAL;
1635 if (!nd->nd_repstat) {
1636 if (nd->nd_flag & ND_NFSV4) {
1640 error = nfsrv_mtofh(nd, &dfh);
1643 /* tovp is always NULL unless NFSv4 */
1646 nfsd_fhtovp(nd, &dfh, LK_EXCLUSIVE, &dp, &tnes, NULL, 0,
1649 NFSVOPUNLOCK(dp, 0);
1652 NFSNAMEICNDSET(&named.ni_cnd, nd->nd_cred, CREATE,
1653 LOCKPARENT | SAVENAME | NOCACHE);
1654 if (!nd->nd_repstat) {
1655 nfsvno_setpathbuf(&named, &bufp, &hashp);
1656 error = nfsrv_parsename(nd, bufp, hashp, &named.ni_pathlen);
1661 nfsvno_relpathbuf(&named);
1664 if (!nd->nd_repstat) {
1665 nd->nd_repstat = nfsvno_namei(nd, &named, dp, 0, &tnes,
1670 nfsvno_relpathbuf(&named);
1674 if (nd->nd_flag & ND_NFSV2) {
1678 dirfor_ret = nfsvno_getattr(dirp, &dirfor,
1682 if (!nd->nd_repstat)
1683 nd->nd_repstat = nfsvno_link(&named, vp, nd->nd_cred, p, exp);
1684 if (nd->nd_flag & ND_NFSV3)
1685 getret = nfsvno_getattr(vp, &at, nd->nd_cred, p, 0);
1687 diraft_ret = nfsvno_getattr(dirp, &diraft, nd->nd_cred, p, 0);
1691 if (nd->nd_flag & ND_NFSV3) {
1692 nfsrv_postopattr(nd, getret, &at);
1693 nfsrv_wcc(nd, dirfor_ret, &dirfor, diraft_ret, &diraft);
1694 } else if ((nd->nd_flag & ND_NFSV4) && !nd->nd_repstat) {
1695 NFSM_BUILD(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
1696 *tl++ = newnfs_false;
1697 txdr_hyper(dirfor.na_filerev, tl);
1699 txdr_hyper(diraft.na_filerev, tl);
1703 NFSEXITCODE2(error, nd);
1708 * nfs symbolic link service
1711 nfsrvd_symlink(struct nfsrv_descript *nd, __unused int isdgram,
1712 vnode_t dp, vnode_t *vpp, fhandle_t *fhp, NFSPROC_T *p,
1713 struct nfsexstuff *exp)
1715 struct nfsvattr nva, dirfor, diraft;
1716 struct nameidata named;
1717 int error = 0, dirfor_ret = 1, diraft_ret = 1, pathlen;
1718 vnode_t dirp = NULL;
1719 char *bufp, *pathcp = NULL;
1722 if (nd->nd_repstat) {
1723 nfsrv_wcc(nd, dirfor_ret, &dirfor, diraft_ret, &diraft);
1728 NFSVNO_ATTRINIT(&nva);
1729 NFSNAMEICNDSET(&named.ni_cnd, nd->nd_cred, CREATE,
1730 LOCKPARENT | SAVESTART | NOCACHE);
1731 nfsvno_setpathbuf(&named, &bufp, &hashp);
1732 error = nfsrv_parsename(nd, bufp, hashp, &named.ni_pathlen);
1733 if (!error && !nd->nd_repstat)
1734 error = nfsvno_getsymlink(nd, &nva, p, &pathcp, &pathlen);
1737 nfsvno_relpathbuf(&named);
1740 if (!nd->nd_repstat) {
1741 nd->nd_repstat = nfsvno_namei(nd, &named, dp, 0, exp, p, &dirp);
1744 nfsvno_relpathbuf(&named);
1746 if (dirp != NULL && !(nd->nd_flag & ND_NFSV3)) {
1752 * And call nfsrvd_symlinksub() to do the common code. It will
1753 * return EBADRPC upon a parsing error, 0 otherwise.
1755 if (!nd->nd_repstat) {
1757 dirfor_ret = nfsvno_getattr(dirp, &dirfor, nd->nd_cred,
1759 nfsrvd_symlinksub(nd, &named, &nva, fhp, vpp, dirp,
1760 &dirfor, &diraft, &diraft_ret, NULL, NULL, p, exp,
1762 } else if (dirp != NULL) {
1763 dirfor_ret = nfsvno_getattr(dirp, &dirfor, nd->nd_cred, p, 0);
1767 FREE(pathcp, M_TEMP);
1769 if (nd->nd_flag & ND_NFSV3) {
1770 if (!nd->nd_repstat) {
1771 (void) nfsm_fhtom(nd, (u_int8_t *)fhp, 0, 1);
1772 nfsrv_postopattr(nd, 0, &nva);
1774 nfsrv_wcc(nd, dirfor_ret, &dirfor, diraft_ret, &diraft);
1778 NFSEXITCODE2(error, nd);
1783 * Common code for creating a symbolic link.
1786 nfsrvd_symlinksub(struct nfsrv_descript *nd, struct nameidata *ndp,
1787 struct nfsvattr *nvap, fhandle_t *fhp, vnode_t *vpp,
1788 vnode_t dirp, struct nfsvattr *dirforp, struct nfsvattr *diraftp,
1789 int *diraft_retp, nfsattrbit_t *attrbitp,
1790 NFSACL_T *aclp, NFSPROC_T *p, struct nfsexstuff *exp, char *pathcp,
1795 nd->nd_repstat = nfsvno_symlink(ndp, nvap, pathcp, pathlen,
1796 !(nd->nd_flag & ND_NFSV2), nd->nd_saveduid, nd->nd_cred, p, exp);
1797 if (!nd->nd_repstat && !(nd->nd_flag & ND_NFSV2)) {
1798 nfsrv_fixattr(nd, ndp->ni_vp, nvap, aclp, p, attrbitp, exp);
1799 if (nd->nd_flag & ND_NFSV3) {
1800 nd->nd_repstat = nfsvno_getfh(ndp->ni_vp, fhp, p);
1801 if (!nd->nd_repstat)
1802 nd->nd_repstat = nfsvno_getattr(ndp->ni_vp,
1803 nvap, nd->nd_cred, p, 1);
1805 if (vpp != NULL && nd->nd_repstat == 0) {
1806 NFSVOPUNLOCK(ndp->ni_vp, 0);
1812 *diraft_retp = nfsvno_getattr(dirp, diraftp, nd->nd_cred, p, 0);
1815 if ((nd->nd_flag & ND_NFSV4) && !nd->nd_repstat) {
1816 NFSM_BUILD(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
1817 *tl++ = newnfs_false;
1818 txdr_hyper(dirforp->na_filerev, tl);
1820 txdr_hyper(diraftp->na_filerev, tl);
1821 (void) nfsrv_putattrbit(nd, attrbitp);
1824 NFSEXITCODE2(0, nd);
1831 nfsrvd_mkdir(struct nfsrv_descript *nd, __unused int isdgram,
1832 vnode_t dp, vnode_t *vpp, fhandle_t *fhp, NFSPROC_T *p,
1833 struct nfsexstuff *exp)
1835 struct nfsvattr nva, dirfor, diraft;
1836 struct nameidata named;
1838 int error = 0, dirfor_ret = 1, diraft_ret = 1;
1839 vnode_t dirp = NULL;
1843 if (nd->nd_repstat) {
1844 nfsrv_wcc(nd, dirfor_ret, &dirfor, diraft_ret, &diraft);
1847 NFSNAMEICNDSET(&named.ni_cnd, nd->nd_cred, CREATE,
1848 LOCKPARENT | SAVENAME | NOCACHE);
1849 nfsvno_setpathbuf(&named, &bufp, &hashp);
1850 error = nfsrv_parsename(nd, bufp, hashp, &named.ni_pathlen);
1853 if (!nd->nd_repstat) {
1854 NFSVNO_ATTRINIT(&nva);
1855 if (nd->nd_flag & ND_NFSV3) {
1856 error = nfsrv_sattr(nd, &nva, NULL, NULL, p);
1860 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
1861 nva.na_mode = nfstov_mode(*tl++);
1864 if (!nd->nd_repstat) {
1865 nd->nd_repstat = nfsvno_namei(nd, &named, dp, 0, exp, p, &dirp);
1868 nfsvno_relpathbuf(&named);
1870 if (dirp != NULL && !(nd->nd_flag & ND_NFSV3)) {
1874 if (nd->nd_repstat) {
1876 dirfor_ret = nfsvno_getattr(dirp, &dirfor, nd->nd_cred,
1880 if (nd->nd_flag & ND_NFSV3)
1881 nfsrv_wcc(nd, dirfor_ret, &dirfor, diraft_ret,
1886 dirfor_ret = nfsvno_getattr(dirp, &dirfor, nd->nd_cred, p, 0);
1889 * Call nfsrvd_mkdirsub() for the code common to V4 as well.
1891 nfsrvd_mkdirsub(nd, &named, &nva, fhp, vpp, dirp, &dirfor, &diraft,
1892 &diraft_ret, NULL, NULL, p, exp);
1894 if (nd->nd_flag & ND_NFSV3) {
1895 if (!nd->nd_repstat) {
1896 (void) nfsm_fhtom(nd, (u_int8_t *)fhp, 0, 1);
1897 nfsrv_postopattr(nd, 0, &nva);
1899 nfsrv_wcc(nd, dirfor_ret, &dirfor, diraft_ret, &diraft);
1900 } else if (!nd->nd_repstat) {
1901 (void) nfsm_fhtom(nd, (u_int8_t *)fhp, 0, 0);
1902 nfsrv_fillattr(nd, &nva);
1906 NFSEXITCODE2(0, nd);
1910 nfsvno_relpathbuf(&named);
1911 NFSEXITCODE2(error, nd);
1916 * Code common to mkdir for V2,3 and 4.
1919 nfsrvd_mkdirsub(struct nfsrv_descript *nd, struct nameidata *ndp,
1920 struct nfsvattr *nvap, fhandle_t *fhp, vnode_t *vpp,
1921 vnode_t dirp, struct nfsvattr *dirforp, struct nfsvattr *diraftp,
1922 int *diraft_retp, nfsattrbit_t *attrbitp, NFSACL_T *aclp,
1923 NFSPROC_T *p, struct nfsexstuff *exp)
1928 NFSVNO_SETATTRVAL(nvap, type, VDIR);
1929 nd->nd_repstat = nfsvno_mkdir(ndp, nvap, nd->nd_saveduid,
1930 nd->nd_cred, p, exp);
1931 if (!nd->nd_repstat) {
1933 nfsrv_fixattr(nd, vp, nvap, aclp, p, attrbitp, exp);
1934 nd->nd_repstat = nfsvno_getfh(vp, fhp, p);
1935 if (!(nd->nd_flag & ND_NFSV4) && !nd->nd_repstat)
1936 nd->nd_repstat = nfsvno_getattr(vp, nvap, nd->nd_cred,
1938 if (vpp && !nd->nd_repstat) {
1939 NFSVOPUNLOCK(vp, 0);
1946 *diraft_retp = nfsvno_getattr(dirp, diraftp, nd->nd_cred, p, 0);
1949 if ((nd->nd_flag & ND_NFSV4) && !nd->nd_repstat) {
1950 NFSM_BUILD(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
1951 *tl++ = newnfs_false;
1952 txdr_hyper(dirforp->na_filerev, tl);
1954 txdr_hyper(diraftp->na_filerev, tl);
1955 (void) nfsrv_putattrbit(nd, attrbitp);
1958 NFSEXITCODE2(0, nd);
1962 * nfs commit service
1965 nfsrvd_commit(struct nfsrv_descript *nd, __unused int isdgram,
1966 vnode_t vp, NFSPROC_T *p, __unused struct nfsexstuff *exp)
1968 struct nfsvattr bfor, aft;
1970 int error = 0, for_ret = 1, aft_ret = 1, cnt;
1973 if (nd->nd_repstat) {
1974 nfsrv_wcc(nd, for_ret, &bfor, aft_ret, &aft);
1977 NFSM_DISSECT(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
1979 * XXX At this time VOP_FSYNC() does not accept offset and byte
1980 * count parameters, so these arguments are useless (someday maybe).
1982 off = fxdr_hyper(tl);
1984 cnt = fxdr_unsigned(int, *tl);
1985 if (nd->nd_flag & ND_NFSV3)
1986 for_ret = nfsvno_getattr(vp, &bfor, nd->nd_cred, p, 1);
1987 nd->nd_repstat = nfsvno_fsync(vp, off, cnt, nd->nd_cred, p);
1988 if (nd->nd_flag & ND_NFSV3) {
1989 aft_ret = nfsvno_getattr(vp, &aft, nd->nd_cred, p, 1);
1990 nfsrv_wcc(nd, for_ret, &bfor, aft_ret, &aft);
1993 if (!nd->nd_repstat) {
1994 NFSM_BUILD(tl, u_int32_t *, NFSX_VERF);
1995 *tl++ = txdr_unsigned(nfsboottime.tv_sec);
1996 *tl = txdr_unsigned(nfsboottime.tv_usec);
2000 NFSEXITCODE2(0, nd);
2004 NFSEXITCODE2(error, nd);
2009 * nfs statfs service
2012 nfsrvd_statfs(struct nfsrv_descript *nd, __unused int isdgram,
2013 vnode_t vp, NFSPROC_T *p, __unused struct nfsexstuff *exp)
2022 if (nd->nd_repstat) {
2023 nfsrv_postopattr(nd, getret, &at);
2027 nd->nd_repstat = nfsvno_statfs(vp, sf);
2028 getret = nfsvno_getattr(vp, &at, nd->nd_cred, p, 1);
2030 if (nd->nd_flag & ND_NFSV3)
2031 nfsrv_postopattr(nd, getret, &at);
2034 if (nd->nd_flag & ND_NFSV2) {
2035 NFSM_BUILD(tl, u_int32_t *, NFSX_V2STATFS);
2036 *tl++ = txdr_unsigned(NFS_V2MAXDATA);
2037 *tl++ = txdr_unsigned(sf->f_bsize);
2038 *tl++ = txdr_unsigned(sf->f_blocks);
2039 *tl++ = txdr_unsigned(sf->f_bfree);
2040 *tl = txdr_unsigned(sf->f_bavail);
2042 NFSM_BUILD(tl, u_int32_t *, NFSX_V3STATFS);
2043 tval = (u_quad_t)sf->f_blocks;
2044 tval *= (u_quad_t)sf->f_bsize;
2045 txdr_hyper(tval, tl); tl += 2;
2046 tval = (u_quad_t)sf->f_bfree;
2047 tval *= (u_quad_t)sf->f_bsize;
2048 txdr_hyper(tval, tl); tl += 2;
2049 tval = (u_quad_t)sf->f_bavail;
2050 tval *= (u_quad_t)sf->f_bsize;
2051 txdr_hyper(tval, tl); tl += 2;
2052 tval = (u_quad_t)sf->f_files;
2053 txdr_hyper(tval, tl); tl += 2;
2054 tval = (u_quad_t)sf->f_ffree;
2055 txdr_hyper(tval, tl); tl += 2;
2056 tval = (u_quad_t)sf->f_ffree;
2057 txdr_hyper(tval, tl); tl += 2;
2062 NFSEXITCODE2(0, nd);
2067 * nfs fsinfo service
2070 nfsrvd_fsinfo(struct nfsrv_descript *nd, int isdgram,
2071 vnode_t vp, NFSPROC_T *p, __unused struct nfsexstuff *exp)
2074 struct nfsfsinfo fs;
2078 if (nd->nd_repstat) {
2079 nfsrv_postopattr(nd, getret, &at);
2082 getret = nfsvno_getattr(vp, &at, nd->nd_cred, p, 1);
2083 nfsvno_getfs(&fs, isdgram);
2085 nfsrv_postopattr(nd, getret, &at);
2086 NFSM_BUILD(tl, u_int32_t *, NFSX_V3FSINFO);
2087 *tl++ = txdr_unsigned(fs.fs_rtmax);
2088 *tl++ = txdr_unsigned(fs.fs_rtpref);
2089 *tl++ = txdr_unsigned(fs.fs_rtmult);
2090 *tl++ = txdr_unsigned(fs.fs_wtmax);
2091 *tl++ = txdr_unsigned(fs.fs_wtpref);
2092 *tl++ = txdr_unsigned(fs.fs_wtmult);
2093 *tl++ = txdr_unsigned(fs.fs_dtpref);
2094 txdr_hyper(fs.fs_maxfilesize, tl);
2096 txdr_nfsv3time(&fs.fs_timedelta, tl);
2098 *tl = txdr_unsigned(fs.fs_properties);
2101 NFSEXITCODE2(0, nd);
2106 * nfs pathconf service
2109 nfsrvd_pathconf(struct nfsrv_descript *nd, __unused int isdgram,
2110 vnode_t vp, NFSPROC_T *p, __unused struct nfsexstuff *exp)
2112 struct nfsv3_pathconf *pc;
2114 register_t linkmax, namemax, chownres, notrunc;
2117 if (nd->nd_repstat) {
2118 nfsrv_postopattr(nd, getret, &at);
2121 nd->nd_repstat = nfsvno_pathconf(vp, _PC_LINK_MAX, &linkmax,
2123 if (!nd->nd_repstat)
2124 nd->nd_repstat = nfsvno_pathconf(vp, _PC_NAME_MAX, &namemax,
2126 if (!nd->nd_repstat)
2127 nd->nd_repstat=nfsvno_pathconf(vp, _PC_CHOWN_RESTRICTED,
2128 &chownres, nd->nd_cred, p);
2129 if (!nd->nd_repstat)
2130 nd->nd_repstat = nfsvno_pathconf(vp, _PC_NO_TRUNC, ¬runc,
2132 getret = nfsvno_getattr(vp, &at, nd->nd_cred, p, 1);
2134 nfsrv_postopattr(nd, getret, &at);
2135 if (!nd->nd_repstat) {
2136 NFSM_BUILD(pc, struct nfsv3_pathconf *, NFSX_V3PATHCONF);
2137 pc->pc_linkmax = txdr_unsigned(linkmax);
2138 pc->pc_namemax = txdr_unsigned(namemax);
2139 pc->pc_notrunc = txdr_unsigned(notrunc);
2140 pc->pc_chownrestricted = txdr_unsigned(chownres);
2143 * These should probably be supported by VOP_PATHCONF(), but
2144 * until msdosfs is exportable (why would you want to?), the
2145 * Unix defaults should be ok.
2147 pc->pc_caseinsensitive = newnfs_false;
2148 pc->pc_casepreserving = newnfs_true;
2152 NFSEXITCODE2(0, nd);
2157 * nfsv4 lock service
2160 nfsrvd_lock(struct nfsrv_descript *nd, __unused int isdgram,
2161 vnode_t vp, NFSPROC_T *p, struct nfsexstuff *exp)
2165 struct nfsstate *stp = NULL;
2166 struct nfslock *lop;
2167 struct nfslockconflict cf;
2169 u_short flags = NFSLCK_LOCK, lflags;
2170 u_int64_t offset, len;
2171 nfsv4stateid_t stateid;
2174 NFSM_DISSECT(tl, u_int32_t *, 7 * NFSX_UNSIGNED);
2175 i = fxdr_unsigned(int, *tl++);
2177 case NFSV4LOCKT_READW:
2178 flags |= NFSLCK_BLOCKING;
2179 case NFSV4LOCKT_READ:
2180 lflags = NFSLCK_READ;
2182 case NFSV4LOCKT_WRITEW:
2183 flags |= NFSLCK_BLOCKING;
2184 case NFSV4LOCKT_WRITE:
2185 lflags = NFSLCK_WRITE;
2188 nd->nd_repstat = NFSERR_BADXDR;
2191 if (*tl++ == newnfs_true)
2192 flags |= NFSLCK_RECLAIM;
2193 offset = fxdr_hyper(tl);
2195 len = fxdr_hyper(tl);
2197 if (*tl == newnfs_true)
2198 flags |= NFSLCK_OPENTOLOCK;
2199 if (flags & NFSLCK_OPENTOLOCK) {
2200 NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED + NFSX_STATEID);
2201 i = fxdr_unsigned(int, *(tl+4+(NFSX_STATEID / NFSX_UNSIGNED)));
2202 if (i <= 0 || i > NFSV4_OPAQUELIMIT) {
2203 nd->nd_repstat = NFSERR_BADXDR;
2206 MALLOC(stp, struct nfsstate *, sizeof (struct nfsstate) + i,
2207 M_NFSDSTATE, M_WAITOK);
2208 stp->ls_ownerlen = i;
2209 stp->ls_op = nd->nd_rp;
2210 stp->ls_seq = fxdr_unsigned(int, *tl++);
2211 stp->ls_stateid.seqid = fxdr_unsigned(u_int32_t, *tl++);
2212 NFSBCOPY((caddr_t)tl, (caddr_t)stp->ls_stateid.other,
2214 tl += (NFSX_STATEIDOTHER / NFSX_UNSIGNED);
2215 stp->ls_opentolockseq = fxdr_unsigned(int, *tl++);
2216 clientid.lval[0] = *tl++;
2217 clientid.lval[1] = *tl++;
2218 if ((nd->nd_flag & ND_IMPLIEDCLID) != 0) {
2219 if ((nd->nd_flag & ND_NFSV41) != 0)
2220 clientid.qval = nd->nd_clientid.qval;
2221 else if (nd->nd_clientid.qval != clientid.qval)
2222 printf("EEK3 multiple clids\n");
2224 if ((nd->nd_flag & ND_NFSV41) != 0)
2225 printf("EEK! no clientid from session\n");
2226 nd->nd_flag |= ND_IMPLIEDCLID;
2227 nd->nd_clientid.qval = clientid.qval;
2229 error = nfsrv_mtostr(nd, stp->ls_owner, stp->ls_ownerlen);
2233 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID + NFSX_UNSIGNED);
2234 MALLOC(stp, struct nfsstate *, sizeof (struct nfsstate),
2235 M_NFSDSTATE, M_WAITOK);
2236 stp->ls_ownerlen = 0;
2237 stp->ls_op = nd->nd_rp;
2238 stp->ls_stateid.seqid = fxdr_unsigned(u_int32_t, *tl++);
2239 NFSBCOPY((caddr_t)tl, (caddr_t)stp->ls_stateid.other,
2241 tl += (NFSX_STATEIDOTHER / NFSX_UNSIGNED);
2242 stp->ls_seq = fxdr_unsigned(int, *tl);
2243 clientid.lval[0] = stp->ls_stateid.other[0];
2244 clientid.lval[1] = stp->ls_stateid.other[1];
2245 if ((nd->nd_flag & ND_IMPLIEDCLID) != 0) {
2246 if ((nd->nd_flag & ND_NFSV41) != 0)
2247 clientid.qval = nd->nd_clientid.qval;
2248 else if (nd->nd_clientid.qval != clientid.qval)
2249 printf("EEK4 multiple clids\n");
2251 if ((nd->nd_flag & ND_NFSV41) != 0)
2252 printf("EEK! no clientid from session\n");
2253 nd->nd_flag |= ND_IMPLIEDCLID;
2254 nd->nd_clientid.qval = clientid.qval;
2257 MALLOC(lop, struct nfslock *, sizeof (struct nfslock),
2258 M_NFSDLOCK, M_WAITOK);
2259 lop->lo_first = offset;
2260 if (len == NFS64BITSSET) {
2261 lop->lo_end = NFS64BITSSET;
2263 lop->lo_end = offset + len;
2264 if (lop->lo_end <= lop->lo_first)
2265 nd->nd_repstat = NFSERR_INVAL;
2267 lop->lo_flags = lflags;
2268 stp->ls_flags = flags;
2269 stp->ls_uid = nd->nd_cred->cr_uid;
2272 * Do basic access checking.
2274 if (!nd->nd_repstat && vnode_vtype(vp) != VREG) {
2275 if (vnode_vtype(vp) == VDIR)
2276 nd->nd_repstat = NFSERR_ISDIR;
2278 nd->nd_repstat = NFSERR_INVAL;
2280 if (!nd->nd_repstat) {
2281 if (lflags & NFSLCK_WRITE) {
2282 nd->nd_repstat = nfsvno_accchk(vp, VWRITE,
2283 nd->nd_cred, exp, p, NFSACCCHK_ALLOWOWNER,
2284 NFSACCCHK_VPISLOCKED, NULL);
2286 nd->nd_repstat = nfsvno_accchk(vp, VREAD,
2287 nd->nd_cred, exp, p, NFSACCCHK_ALLOWOWNER,
2288 NFSACCCHK_VPISLOCKED, NULL);
2290 nd->nd_repstat = nfsvno_accchk(vp, VEXEC,
2291 nd->nd_cred, exp, p, NFSACCCHK_ALLOWOWNER,
2292 NFSACCCHK_VPISLOCKED, NULL);
2297 * We call nfsrv_lockctrl() even if nd_repstat set, so that the
2298 * seqid# gets updated. nfsrv_lockctrl() will return the value
2299 * of nd_repstat, if it gets that far.
2301 nd->nd_repstat = nfsrv_lockctrl(vp, &stp, &lop, &cf, clientid,
2302 &stateid, exp, nd, p);
2304 FREE((caddr_t)lop, M_NFSDLOCK);
2306 FREE((caddr_t)stp, M_NFSDSTATE);
2307 if (!nd->nd_repstat) {
2308 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID);
2309 *tl++ = txdr_unsigned(stateid.seqid);
2310 NFSBCOPY((caddr_t)stateid.other,(caddr_t)tl,NFSX_STATEIDOTHER);
2311 } else if (nd->nd_repstat == NFSERR_DENIED) {
2312 NFSM_BUILD(tl, u_int32_t *, 7 * NFSX_UNSIGNED);
2313 txdr_hyper(cf.cl_first, tl);
2315 if (cf.cl_end == NFS64BITSSET)
2318 len = cf.cl_end - cf.cl_first;
2319 txdr_hyper(len, tl);
2321 if (cf.cl_flags == NFSLCK_WRITE)
2322 *tl++ = txdr_unsigned(NFSV4LOCKT_WRITE);
2324 *tl++ = txdr_unsigned(NFSV4LOCKT_READ);
2325 *tl++ = stateid.other[0];
2326 *tl = stateid.other[1];
2327 (void) nfsm_strtom(nd, cf.cl_owner, cf.cl_ownerlen);
2330 NFSEXITCODE2(0, nd);
2335 free((caddr_t)stp, M_NFSDSTATE);
2336 NFSEXITCODE2(error, nd);
2341 * nfsv4 lock test service
2344 nfsrvd_lockt(struct nfsrv_descript *nd, __unused int isdgram,
2345 vnode_t vp, NFSPROC_T *p, struct nfsexstuff *exp)
2349 struct nfsstate *stp = NULL;
2350 struct nfslock lo, *lop = &lo;
2351 struct nfslockconflict cf;
2353 nfsv4stateid_t stateid;
2357 NFSM_DISSECT(tl, u_int32_t *, 8 * NFSX_UNSIGNED);
2358 i = fxdr_unsigned(int, *(tl + 7));
2359 if (i <= 0 || i > NFSV4_OPAQUELIMIT) {
2360 nd->nd_repstat = NFSERR_BADXDR;
2363 MALLOC(stp, struct nfsstate *, sizeof (struct nfsstate) + i,
2364 M_NFSDSTATE, M_WAITOK);
2365 stp->ls_ownerlen = i;
2367 stp->ls_flags = NFSLCK_TEST;
2368 stp->ls_uid = nd->nd_cred->cr_uid;
2369 i = fxdr_unsigned(int, *tl++);
2371 case NFSV4LOCKT_READW:
2372 stp->ls_flags |= NFSLCK_BLOCKING;
2373 case NFSV4LOCKT_READ:
2374 lo.lo_flags = NFSLCK_READ;
2376 case NFSV4LOCKT_WRITEW:
2377 stp->ls_flags |= NFSLCK_BLOCKING;
2378 case NFSV4LOCKT_WRITE:
2379 lo.lo_flags = NFSLCK_WRITE;
2382 nd->nd_repstat = NFSERR_BADXDR;
2385 lo.lo_first = fxdr_hyper(tl);
2387 len = fxdr_hyper(tl);
2388 if (len == NFS64BITSSET) {
2389 lo.lo_end = NFS64BITSSET;
2391 lo.lo_end = lo.lo_first + len;
2392 if (lo.lo_end <= lo.lo_first)
2393 nd->nd_repstat = NFSERR_INVAL;
2396 clientid.lval[0] = *tl++;
2397 clientid.lval[1] = *tl;
2398 if ((nd->nd_flag & ND_IMPLIEDCLID) != 0) {
2399 if ((nd->nd_flag & ND_NFSV41) != 0)
2400 clientid.qval = nd->nd_clientid.qval;
2401 else if (nd->nd_clientid.qval != clientid.qval)
2402 printf("EEK5 multiple clids\n");
2404 if ((nd->nd_flag & ND_NFSV41) != 0)
2405 printf("EEK! no clientid from session\n");
2406 nd->nd_flag |= ND_IMPLIEDCLID;
2407 nd->nd_clientid.qval = clientid.qval;
2409 error = nfsrv_mtostr(nd, stp->ls_owner, stp->ls_ownerlen);
2412 if (!nd->nd_repstat && vnode_vtype(vp) != VREG) {
2413 if (vnode_vtype(vp) == VDIR)
2414 nd->nd_repstat = NFSERR_ISDIR;
2416 nd->nd_repstat = NFSERR_INVAL;
2418 if (!nd->nd_repstat)
2419 nd->nd_repstat = nfsrv_lockctrl(vp, &stp, &lop, &cf, clientid,
2420 &stateid, exp, nd, p);
2421 if (nd->nd_repstat) {
2422 if (nd->nd_repstat == NFSERR_DENIED) {
2423 NFSM_BUILD(tl, u_int32_t *, 7 * NFSX_UNSIGNED);
2424 txdr_hyper(cf.cl_first, tl);
2426 if (cf.cl_end == NFS64BITSSET)
2429 len = cf.cl_end - cf.cl_first;
2430 txdr_hyper(len, tl);
2432 if (cf.cl_flags == NFSLCK_WRITE)
2433 *tl++ = txdr_unsigned(NFSV4LOCKT_WRITE);
2435 *tl++ = txdr_unsigned(NFSV4LOCKT_READ);
2436 *tl++ = stp->ls_stateid.other[0];
2437 *tl = stp->ls_stateid.other[1];
2438 (void) nfsm_strtom(nd, cf.cl_owner, cf.cl_ownerlen);
2443 FREE((caddr_t)stp, M_NFSDSTATE);
2444 NFSEXITCODE2(0, nd);
2449 free((caddr_t)stp, M_NFSDSTATE);
2450 NFSEXITCODE2(error, nd);
2455 * nfsv4 unlock service
2458 nfsrvd_locku(struct nfsrv_descript *nd, __unused int isdgram,
2459 vnode_t vp, NFSPROC_T *p, struct nfsexstuff *exp)
2463 struct nfsstate *stp;
2464 struct nfslock *lop;
2466 nfsv4stateid_t stateid;
2470 NFSM_DISSECT(tl, u_int32_t *, 6 * NFSX_UNSIGNED + NFSX_STATEID);
2471 MALLOC(stp, struct nfsstate *, sizeof (struct nfsstate),
2472 M_NFSDSTATE, M_WAITOK);
2473 MALLOC(lop, struct nfslock *, sizeof (struct nfslock),
2474 M_NFSDLOCK, M_WAITOK);
2475 stp->ls_flags = NFSLCK_UNLOCK;
2476 lop->lo_flags = NFSLCK_UNLOCK;
2477 stp->ls_op = nd->nd_rp;
2478 i = fxdr_unsigned(int, *tl++);
2480 case NFSV4LOCKT_READW:
2481 stp->ls_flags |= NFSLCK_BLOCKING;
2482 case NFSV4LOCKT_READ:
2484 case NFSV4LOCKT_WRITEW:
2485 stp->ls_flags |= NFSLCK_BLOCKING;
2486 case NFSV4LOCKT_WRITE:
2489 nd->nd_repstat = NFSERR_BADXDR;
2490 free(stp, M_NFSDSTATE);
2491 free(lop, M_NFSDLOCK);
2494 stp->ls_ownerlen = 0;
2495 stp->ls_uid = nd->nd_cred->cr_uid;
2496 stp->ls_seq = fxdr_unsigned(int, *tl++);
2497 stp->ls_stateid.seqid = fxdr_unsigned(u_int32_t, *tl++);
2498 NFSBCOPY((caddr_t)tl, (caddr_t)stp->ls_stateid.other,
2500 tl += (NFSX_STATEIDOTHER / NFSX_UNSIGNED);
2501 lop->lo_first = fxdr_hyper(tl);
2503 len = fxdr_hyper(tl);
2504 if (len == NFS64BITSSET) {
2505 lop->lo_end = NFS64BITSSET;
2507 lop->lo_end = lop->lo_first + len;
2508 if (lop->lo_end <= lop->lo_first)
2509 nd->nd_repstat = NFSERR_INVAL;
2511 clientid.lval[0] = stp->ls_stateid.other[0];
2512 clientid.lval[1] = stp->ls_stateid.other[1];
2513 if ((nd->nd_flag & ND_IMPLIEDCLID) != 0) {
2514 if ((nd->nd_flag & ND_NFSV41) != 0)
2515 clientid.qval = nd->nd_clientid.qval;
2516 else if (nd->nd_clientid.qval != clientid.qval)
2517 printf("EEK6 multiple clids\n");
2519 if ((nd->nd_flag & ND_NFSV41) != 0)
2520 printf("EEK! no clientid from session\n");
2521 nd->nd_flag |= ND_IMPLIEDCLID;
2522 nd->nd_clientid.qval = clientid.qval;
2524 if (!nd->nd_repstat && vnode_vtype(vp) != VREG) {
2525 if (vnode_vtype(vp) == VDIR)
2526 nd->nd_repstat = NFSERR_ISDIR;
2528 nd->nd_repstat = NFSERR_INVAL;
2531 * Call nfsrv_lockctrl() even if nd_repstat is set, so that the
2532 * seqid# gets incremented. nfsrv_lockctrl() will return the
2533 * value of nd_repstat, if it gets that far.
2535 nd->nd_repstat = nfsrv_lockctrl(vp, &stp, &lop, NULL, clientid,
2536 &stateid, exp, nd, p);
2538 FREE((caddr_t)stp, M_NFSDSTATE);
2540 free((caddr_t)lop, M_NFSDLOCK);
2541 if (!nd->nd_repstat) {
2542 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID);
2543 *tl++ = txdr_unsigned(stateid.seqid);
2544 NFSBCOPY((caddr_t)stateid.other,(caddr_t)tl,NFSX_STATEIDOTHER);
2548 NFSEXITCODE2(error, nd);
2553 * nfsv4 open service
2556 nfsrvd_open(struct nfsrv_descript *nd, __unused int isdgram,
2557 vnode_t dp, vnode_t *vpp, __unused fhandle_t *fhp, NFSPROC_T *p,
2558 struct nfsexstuff *exp)
2562 struct nfsstate *stp = NULL;
2563 int error = 0, create, claim, exclusive_flag = 0;
2564 u_int32_t rflags = NFSV4OPEN_LOCKTYPEPOSIX, acemask;
2565 int how = NFSCREATE_UNCHECKED;
2566 int32_t cverf[2], tverf[2] = { 0, 0 };
2567 vnode_t vp = NULL, dirp = NULL;
2568 struct nfsvattr nva, dirfor, diraft;
2569 struct nameidata named;
2570 nfsv4stateid_t stateid, delegstateid;
2571 nfsattrbit_t attrbits;
2575 NFSACL_T *aclp = NULL;
2577 #ifdef NFS4_ACL_EXTATTR_NAME
2578 aclp = acl_alloc(M_WAITOK);
2581 NFSZERO_ATTRBIT(&attrbits);
2582 named.ni_startdir = NULL;
2583 named.ni_cnd.cn_nameiop = 0;
2584 NFSM_DISSECT(tl, u_int32_t *, 6 * NFSX_UNSIGNED);
2585 i = fxdr_unsigned(int, *(tl + 5));
2586 if (i <= 0 || i > NFSV4_OPAQUELIMIT) {
2587 nd->nd_repstat = NFSERR_BADXDR;
2590 MALLOC(stp, struct nfsstate *, sizeof (struct nfsstate) + i,
2591 M_NFSDSTATE, M_WAITOK);
2592 stp->ls_ownerlen = i;
2593 stp->ls_op = nd->nd_rp;
2594 stp->ls_flags = NFSLCK_OPEN;
2595 stp->ls_uid = nd->nd_cred->cr_uid;
2596 stp->ls_seq = fxdr_unsigned(u_int32_t, *tl++);
2597 i = fxdr_unsigned(int, *tl++);
2599 if ((i & (NFSV4OPEN_WANTDELEGMASK | NFSV4OPEN_WANTSIGNALDELEG |
2600 NFSV4OPEN_WANTPUSHDELEG)) != 0 && (nd->nd_flag & ND_NFSV41) != 0) {
2602 /* For now, ignore these. */
2603 i &= ~(NFSV4OPEN_WANTPUSHDELEG | NFSV4OPEN_WANTSIGNALDELEG);
2604 switch (i & NFSV4OPEN_WANTDELEGMASK) {
2605 case NFSV4OPEN_WANTANYDELEG:
2606 stp->ls_flags |= (NFSLCK_WANTRDELEG |
2608 i &= ~NFSV4OPEN_WANTDELEGMASK;
2610 case NFSV4OPEN_WANTREADDELEG:
2611 stp->ls_flags |= NFSLCK_WANTRDELEG;
2612 i &= ~NFSV4OPEN_WANTDELEGMASK;
2614 case NFSV4OPEN_WANTWRITEDELEG:
2615 stp->ls_flags |= NFSLCK_WANTWDELEG;
2616 i &= ~NFSV4OPEN_WANTDELEGMASK;
2618 case NFSV4OPEN_WANTNODELEG:
2619 stp->ls_flags |= NFSLCK_WANTNODELEG;
2620 i &= ~NFSV4OPEN_WANTDELEGMASK;
2622 case NFSV4OPEN_WANTCANCEL:
2623 printf("NFSv4: ignore Open WantCancel\n");
2624 i &= ~NFSV4OPEN_WANTDELEGMASK;
2627 /* nd_repstat will be set to NFSERR_INVAL below. */
2632 case NFSV4OPEN_ACCESSREAD:
2633 stp->ls_flags |= NFSLCK_READACCESS;
2635 case NFSV4OPEN_ACCESSWRITE:
2636 stp->ls_flags |= NFSLCK_WRITEACCESS;
2638 case NFSV4OPEN_ACCESSBOTH:
2639 stp->ls_flags |= (NFSLCK_READACCESS | NFSLCK_WRITEACCESS);
2642 nd->nd_repstat = NFSERR_INVAL;
2644 i = fxdr_unsigned(int, *tl++);
2646 case NFSV4OPEN_DENYNONE:
2648 case NFSV4OPEN_DENYREAD:
2649 stp->ls_flags |= NFSLCK_READDENY;
2651 case NFSV4OPEN_DENYWRITE:
2652 stp->ls_flags |= NFSLCK_WRITEDENY;
2654 case NFSV4OPEN_DENYBOTH:
2655 stp->ls_flags |= (NFSLCK_READDENY | NFSLCK_WRITEDENY);
2658 nd->nd_repstat = NFSERR_INVAL;
2660 clientid.lval[0] = *tl++;
2661 clientid.lval[1] = *tl;
2662 if ((nd->nd_flag & ND_IMPLIEDCLID) != 0) {
2663 if ((nd->nd_flag & ND_NFSV41) != 0)
2664 clientid.qval = nd->nd_clientid.qval;
2665 else if (nd->nd_clientid.qval != clientid.qval)
2666 printf("EEK7 multiple clids\n");
2668 if ((nd->nd_flag & ND_NFSV41) != 0)
2669 printf("EEK! no clientid from session\n");
2670 nd->nd_flag |= ND_IMPLIEDCLID;
2671 nd->nd_clientid.qval = clientid.qval;
2673 error = nfsrv_mtostr(nd, stp->ls_owner, stp->ls_ownerlen);
2676 NFSVNO_ATTRINIT(&nva);
2677 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2678 create = fxdr_unsigned(int, *tl);
2679 if (!nd->nd_repstat)
2680 nd->nd_repstat = nfsvno_getattr(dp, &dirfor, nd->nd_cred, p, 0);
2681 if (create == NFSV4OPEN_CREATE) {
2684 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2685 how = fxdr_unsigned(int, *tl);
2687 case NFSCREATE_UNCHECKED:
2688 case NFSCREATE_GUARDED:
2689 error = nfsv4_sattr(nd, &nva, &attrbits, aclp, p);
2693 * If the na_gid being set is the same as that of
2694 * the directory it is going in, clear it, since
2695 * that is what will be set by default. This allows
2696 * a user that isn't in that group to do the create.
2698 if (!nd->nd_repstat && NFSVNO_ISSETGID(&nva) &&
2699 nva.na_gid == dirfor.na_gid)
2700 NFSVNO_UNSET(&nva, gid);
2701 if (!nd->nd_repstat)
2702 nd->nd_repstat = nfsrv_checkuidgid(nd, &nva);
2704 case NFSCREATE_EXCLUSIVE:
2705 NFSM_DISSECT(tl, u_int32_t *, NFSX_VERF);
2709 case NFSCREATE_EXCLUSIVE41:
2710 NFSM_DISSECT(tl, u_int32_t *, NFSX_VERF);
2713 error = nfsv4_sattr(nd, &nva, &attrbits, aclp, p);
2716 if (NFSISSET_ATTRBIT(&attrbits,
2717 NFSATTRBIT_TIMEACCESSSET))
2718 nd->nd_repstat = NFSERR_INVAL;
2720 * If the na_gid being set is the same as that of
2721 * the directory it is going in, clear it, since
2722 * that is what will be set by default. This allows
2723 * a user that isn't in that group to do the create.
2725 if (nd->nd_repstat == 0 && NFSVNO_ISSETGID(&nva) &&
2726 nva.na_gid == dirfor.na_gid)
2727 NFSVNO_UNSET(&nva, gid);
2728 if (nd->nd_repstat == 0)
2729 nd->nd_repstat = nfsrv_checkuidgid(nd, &nva);
2732 nd->nd_repstat = NFSERR_BADXDR;
2735 } else if (create != NFSV4OPEN_NOCREATE) {
2736 nd->nd_repstat = NFSERR_BADXDR;
2741 * Now, handle the claim, which usually includes looking up a
2742 * name in the directory referenced by dp. The exception is
2743 * NFSV4OPEN_CLAIMPREVIOUS.
2745 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2746 claim = fxdr_unsigned(int, *tl);
2747 if (claim == NFSV4OPEN_CLAIMDELEGATECUR) {
2748 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID);
2749 stateid.seqid = fxdr_unsigned(u_int32_t, *tl++);
2750 NFSBCOPY((caddr_t)tl,(caddr_t)stateid.other,NFSX_STATEIDOTHER);
2751 stp->ls_flags |= NFSLCK_DELEGCUR;
2752 } else if (claim == NFSV4OPEN_CLAIMDELEGATEPREV) {
2753 stp->ls_flags |= NFSLCK_DELEGPREV;
2755 if (claim == NFSV4OPEN_CLAIMNULL || claim == NFSV4OPEN_CLAIMDELEGATECUR
2756 || claim == NFSV4OPEN_CLAIMDELEGATEPREV) {
2757 if (!nd->nd_repstat && create == NFSV4OPEN_CREATE &&
2758 claim != NFSV4OPEN_CLAIMNULL)
2759 nd->nd_repstat = NFSERR_INVAL;
2760 if (nd->nd_repstat) {
2761 nd->nd_repstat = nfsrv_opencheck(clientid,
2762 &stateid, stp, NULL, nd, p, nd->nd_repstat);
2765 if (create == NFSV4OPEN_CREATE)
2766 NFSNAMEICNDSET(&named.ni_cnd, nd->nd_cred, CREATE,
2767 LOCKPARENT | LOCKLEAF | SAVESTART | NOCACHE);
2769 NFSNAMEICNDSET(&named.ni_cnd, nd->nd_cred, LOOKUP,
2770 LOCKLEAF | SAVESTART);
2771 nfsvno_setpathbuf(&named, &bufp, &hashp);
2772 error = nfsrv_parsename(nd, bufp, hashp, &named.ni_pathlen);
2775 #ifdef NFS4_ACL_EXTATTR_NAME
2778 FREE((caddr_t)stp, M_NFSDSTATE);
2779 nfsvno_relpathbuf(&named);
2780 NFSEXITCODE2(error, nd);
2783 if (!nd->nd_repstat) {
2784 nd->nd_repstat = nfsvno_namei(nd, &named, dp, 0, exp,
2788 nfsvno_relpathbuf(&named);
2790 if (create == NFSV4OPEN_CREATE) {
2792 case NFSCREATE_UNCHECKED:
2795 * Clear the setable attribute bits, except
2796 * for Size, if it is being truncated.
2798 NFSZERO_ATTRBIT(&attrbits);
2799 if (NFSVNO_ISSETSIZE(&nva))
2800 NFSSETBIT_ATTRBIT(&attrbits,
2804 case NFSCREATE_GUARDED:
2805 if (named.ni_vp && !nd->nd_repstat)
2806 nd->nd_repstat = EEXIST;
2808 case NFSCREATE_EXCLUSIVE:
2813 case NFSCREATE_EXCLUSIVE41:
2818 nfsvno_open(nd, &named, clientid, &stateid, stp,
2819 &exclusive_flag, &nva, cverf, create, aclp, &attrbits,
2820 nd->nd_cred, p, exp, &vp);
2821 } else if (claim == NFSV4OPEN_CLAIMPREVIOUS || claim ==
2822 NFSV4OPEN_CLAIMFH) {
2823 if (claim == NFSV4OPEN_CLAIMPREVIOUS) {
2824 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2825 i = fxdr_unsigned(int, *tl);
2827 case NFSV4OPEN_DELEGATEREAD:
2828 stp->ls_flags |= NFSLCK_DELEGREAD;
2830 case NFSV4OPEN_DELEGATEWRITE:
2831 stp->ls_flags |= NFSLCK_DELEGWRITE;
2832 case NFSV4OPEN_DELEGATENONE:
2835 nd->nd_repstat = NFSERR_BADXDR;
2838 stp->ls_flags |= NFSLCK_RECLAIM;
2841 if (nd->nd_repstat == 0 && create == NFSV4OPEN_CREATE)
2842 nd->nd_repstat = NFSERR_INVAL;
2845 NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY);
2846 if ((vp->v_iflag & VI_DOOMED) == 0)
2847 nd->nd_repstat = nfsrv_opencheck(clientid, &stateid,
2848 stp, vp, nd, p, nd->nd_repstat);
2850 nd->nd_repstat = NFSERR_PERM;
2852 nd->nd_repstat = NFSERR_BADXDR;
2857 * Do basic access checking.
2859 if (!nd->nd_repstat && vnode_vtype(vp) != VREG) {
2861 * The IETF working group decided that this is the correct
2862 * error return for all non-regular files.
2864 nd->nd_repstat = NFSERR_SYMLINK;
2866 if (!nd->nd_repstat && (stp->ls_flags & NFSLCK_WRITEACCESS))
2867 nd->nd_repstat = nfsvno_accchk(vp, VWRITE, nd->nd_cred,
2868 exp, p, NFSACCCHK_ALLOWOWNER, NFSACCCHK_VPISLOCKED, NULL);
2869 if (!nd->nd_repstat && (stp->ls_flags & NFSLCK_READACCESS)) {
2870 nd->nd_repstat = nfsvno_accchk(vp, VREAD, nd->nd_cred,
2871 exp, p, NFSACCCHK_ALLOWOWNER, NFSACCCHK_VPISLOCKED, NULL);
2873 nd->nd_repstat = nfsvno_accchk(vp, VEXEC,
2874 nd->nd_cred, exp, p, NFSACCCHK_ALLOWOWNER,
2875 NFSACCCHK_VPISLOCKED, NULL);
2878 if (!nd->nd_repstat) {
2879 nd->nd_repstat = nfsvno_getattr(vp, &nva, nd->nd_cred, p, 1);
2880 if (!nd->nd_repstat) {
2881 tverf[0] = nva.na_atime.tv_sec;
2882 tverf[1] = nva.na_atime.tv_nsec;
2885 if (!nd->nd_repstat && exclusive_flag && (cverf[0] != tverf[0] ||
2886 cverf[1] != tverf[1]))
2887 nd->nd_repstat = EEXIST;
2889 * Do the open locking/delegation stuff.
2891 if (!nd->nd_repstat)
2892 nd->nd_repstat = nfsrv_openctrl(nd, vp, &stp, clientid, &stateid,
2893 &delegstateid, &rflags, exp, p, nva.na_filerev);
2896 * vp must be unlocked before the call to nfsvno_getattr(dirp,...)
2897 * below, to avoid a deadlock with the lookup in nfsvno_namei() above.
2898 * (ie: Leave the NFSVOPUNLOCK() about here.)
2901 NFSVOPUNLOCK(vp, 0);
2903 FREE((caddr_t)stp, M_NFSDSTATE);
2904 if (!nd->nd_repstat && dirp)
2905 nd->nd_repstat = nfsvno_getattr(dirp, &diraft, nd->nd_cred, p,
2907 if (!nd->nd_repstat) {
2908 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID + 6 * NFSX_UNSIGNED);
2909 *tl++ = txdr_unsigned(stateid.seqid);
2910 NFSBCOPY((caddr_t)stateid.other,(caddr_t)tl,NFSX_STATEIDOTHER);
2911 tl += (NFSX_STATEIDOTHER / NFSX_UNSIGNED);
2912 if (claim == NFSV4OPEN_CLAIMPREVIOUS) {
2913 *tl++ = newnfs_true;
2919 *tl++ = newnfs_false; /* Since dirp is not locked */
2920 txdr_hyper(dirfor.na_filerev, tl);
2922 txdr_hyper(diraft.na_filerev, tl);
2925 *tl = txdr_unsigned(rflags & NFSV4OPEN_RFLAGS);
2926 (void) nfsrv_putattrbit(nd, &attrbits);
2927 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2928 if (rflags & NFSV4OPEN_READDELEGATE)
2929 *tl = txdr_unsigned(NFSV4OPEN_DELEGATEREAD);
2930 else if (rflags & NFSV4OPEN_WRITEDELEGATE)
2931 *tl = txdr_unsigned(NFSV4OPEN_DELEGATEWRITE);
2932 else if (retext != 0) {
2933 *tl = txdr_unsigned(NFSV4OPEN_DELEGATENONEEXT);
2934 if ((rflags & NFSV4OPEN_WDNOTWANTED) != 0) {
2935 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2936 *tl = txdr_unsigned(NFSV4OPEN_NOTWANTED);
2937 } else if ((rflags & NFSV4OPEN_WDSUPPFTYPE) != 0) {
2938 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2939 *tl = txdr_unsigned(NFSV4OPEN_NOTSUPPFTYPE);
2940 } else if ((rflags & NFSV4OPEN_WDCONTENTION) != 0) {
2941 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2942 *tl++ = txdr_unsigned(NFSV4OPEN_CONTENTION);
2944 } else if ((rflags & NFSV4OPEN_WDRESOURCE) != 0) {
2945 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2946 *tl++ = txdr_unsigned(NFSV4OPEN_RESOURCE);
2949 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2950 *tl = txdr_unsigned(NFSV4OPEN_NOTWANTED);
2953 *tl = txdr_unsigned(NFSV4OPEN_DELEGATENONE);
2954 if (rflags & (NFSV4OPEN_READDELEGATE|NFSV4OPEN_WRITEDELEGATE)) {
2955 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID+NFSX_UNSIGNED);
2956 *tl++ = txdr_unsigned(delegstateid.seqid);
2957 NFSBCOPY((caddr_t)delegstateid.other, (caddr_t)tl,
2959 tl += (NFSX_STATEIDOTHER / NFSX_UNSIGNED);
2960 if (rflags & NFSV4OPEN_RECALL)
2964 if (rflags & NFSV4OPEN_WRITEDELEGATE) {
2965 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2966 *tl++ = txdr_unsigned(NFSV4OPEN_LIMITSIZE);
2967 txdr_hyper(nva.na_size, tl);
2969 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2970 *tl++ = txdr_unsigned(NFSV4ACE_ALLOWEDTYPE);
2971 *tl++ = txdr_unsigned(0x0);
2972 acemask = NFSV4ACE_ALLFILESMASK;
2973 if (nva.na_mode & S_IRUSR)
2974 acemask |= NFSV4ACE_READMASK;
2975 if (nva.na_mode & S_IWUSR)
2976 acemask |= NFSV4ACE_WRITEMASK;
2977 if (nva.na_mode & S_IXUSR)
2978 acemask |= NFSV4ACE_EXECUTEMASK;
2979 *tl = txdr_unsigned(acemask);
2980 (void) nfsm_strtom(nd, "OWNER@", 6);
2988 #ifdef NFS4_ACL_EXTATTR_NAME
2991 NFSEXITCODE2(0, nd);
2995 #ifdef NFS4_ACL_EXTATTR_NAME
2999 FREE((caddr_t)stp, M_NFSDSTATE);
3000 NFSEXITCODE2(error, nd);
3005 * nfsv4 close service
3008 nfsrvd_close(struct nfsrv_descript *nd, __unused int isdgram,
3009 vnode_t vp, NFSPROC_T *p, __unused struct nfsexstuff *exp)
3012 struct nfsstate st, *stp = &st;
3014 nfsv4stateid_t stateid;
3017 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED + NFSX_STATEID);
3018 stp->ls_seq = fxdr_unsigned(u_int32_t, *tl++);
3019 stp->ls_ownerlen = 0;
3020 stp->ls_op = nd->nd_rp;
3021 stp->ls_uid = nd->nd_cred->cr_uid;
3022 stp->ls_stateid.seqid = fxdr_unsigned(u_int32_t, *tl++);
3023 NFSBCOPY((caddr_t)tl, (caddr_t)stp->ls_stateid.other,
3025 stp->ls_flags = NFSLCK_CLOSE;
3026 clientid.lval[0] = stp->ls_stateid.other[0];
3027 clientid.lval[1] = stp->ls_stateid.other[1];
3028 if ((nd->nd_flag & ND_IMPLIEDCLID) != 0) {
3029 if ((nd->nd_flag & ND_NFSV41) != 0)
3030 clientid.qval = nd->nd_clientid.qval;
3031 else if (nd->nd_clientid.qval != clientid.qval)
3032 printf("EEK8 multiple clids\n");
3034 if ((nd->nd_flag & ND_NFSV41) != 0)
3035 printf("EEK! no clientid from session\n");
3036 nd->nd_flag |= ND_IMPLIEDCLID;
3037 nd->nd_clientid.qval = clientid.qval;
3039 nd->nd_repstat = nfsrv_openupdate(vp, stp, clientid, &stateid, nd, p);
3041 if (!nd->nd_repstat) {
3042 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID);
3043 *tl++ = txdr_unsigned(stateid.seqid);
3044 NFSBCOPY((caddr_t)stateid.other,(caddr_t)tl,NFSX_STATEIDOTHER);
3046 NFSEXITCODE2(0, nd);
3050 NFSEXITCODE2(error, nd);
3055 * nfsv4 delegpurge service
3058 nfsrvd_delegpurge(struct nfsrv_descript *nd, __unused int isdgram,
3059 __unused vnode_t vp, __unused NFSPROC_T *p, __unused struct nfsexstuff *exp)
3065 if (nfs_rootfhset == 0 || nfsd_checkrootexp(nd) != 0) {
3066 nd->nd_repstat = NFSERR_WRONGSEC;
3069 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
3070 clientid.lval[0] = *tl++;
3071 clientid.lval[1] = *tl;
3072 if ((nd->nd_flag & ND_IMPLIEDCLID) != 0) {
3073 if ((nd->nd_flag & ND_NFSV41) != 0)
3074 clientid.qval = nd->nd_clientid.qval;
3075 else if (nd->nd_clientid.qval != clientid.qval)
3076 printf("EEK9 multiple clids\n");
3078 if ((nd->nd_flag & ND_NFSV41) != 0)
3079 printf("EEK! no clientid from session\n");
3080 nd->nd_flag |= ND_IMPLIEDCLID;
3081 nd->nd_clientid.qval = clientid.qval;
3083 nd->nd_repstat = nfsrv_delegupdate(nd, clientid, NULL, NULL,
3084 NFSV4OP_DELEGPURGE, nd->nd_cred, p);
3086 NFSEXITCODE2(error, nd);
3091 * nfsv4 delegreturn service
3094 nfsrvd_delegreturn(struct nfsrv_descript *nd, __unused int isdgram,
3095 vnode_t vp, NFSPROC_T *p, __unused struct nfsexstuff *exp)
3099 nfsv4stateid_t stateid;
3102 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID);
3103 stateid.seqid = fxdr_unsigned(u_int32_t, *tl++);
3104 NFSBCOPY((caddr_t)tl, (caddr_t)stateid.other, NFSX_STATEIDOTHER);
3105 clientid.lval[0] = stateid.other[0];
3106 clientid.lval[1] = stateid.other[1];
3107 if ((nd->nd_flag & ND_IMPLIEDCLID) != 0) {
3108 if ((nd->nd_flag & ND_NFSV41) != 0)
3109 clientid.qval = nd->nd_clientid.qval;
3110 else if (nd->nd_clientid.qval != clientid.qval)
3111 printf("EEK10 multiple clids\n");
3113 if ((nd->nd_flag & ND_NFSV41) != 0)
3114 printf("EEK! no clientid from session\n");
3115 nd->nd_flag |= ND_IMPLIEDCLID;
3116 nd->nd_clientid.qval = clientid.qval;
3118 nd->nd_repstat = nfsrv_delegupdate(nd, clientid, &stateid, vp,
3119 NFSV4OP_DELEGRETURN, nd->nd_cred, p);
3122 NFSEXITCODE2(error, nd);
3127 * nfsv4 get file handle service
3130 nfsrvd_getfh(struct nfsrv_descript *nd, __unused int isdgram,
3131 vnode_t vp, NFSPROC_T *p, __unused struct nfsexstuff *exp)
3135 nd->nd_repstat = nfsvno_getfh(vp, &fh, p);
3137 if (!nd->nd_repstat)
3138 (void) nfsm_fhtom(nd, (u_int8_t *)&fh, 0, 0);
3139 NFSEXITCODE2(0, nd);
3144 * nfsv4 open confirm service
3147 nfsrvd_openconfirm(struct nfsrv_descript *nd, __unused int isdgram,
3148 vnode_t vp, NFSPROC_T *p, __unused struct nfsexstuff *exp)
3151 struct nfsstate st, *stp = &st;
3153 nfsv4stateid_t stateid;
3156 if ((nd->nd_flag & ND_NFSV41) != 0) {
3157 nd->nd_repstat = NFSERR_NOTSUPP;
3160 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID + NFSX_UNSIGNED);
3161 stp->ls_ownerlen = 0;
3162 stp->ls_op = nd->nd_rp;
3163 stp->ls_uid = nd->nd_cred->cr_uid;
3164 stp->ls_stateid.seqid = fxdr_unsigned(u_int32_t, *tl++);
3165 NFSBCOPY((caddr_t)tl, (caddr_t)stp->ls_stateid.other,
3167 tl += (NFSX_STATEIDOTHER / NFSX_UNSIGNED);
3168 stp->ls_seq = fxdr_unsigned(u_int32_t, *tl);
3169 stp->ls_flags = NFSLCK_CONFIRM;
3170 clientid.lval[0] = stp->ls_stateid.other[0];
3171 clientid.lval[1] = stp->ls_stateid.other[1];
3172 if ((nd->nd_flag & ND_IMPLIEDCLID) != 0) {
3173 if ((nd->nd_flag & ND_NFSV41) != 0)
3174 clientid.qval = nd->nd_clientid.qval;
3175 else if (nd->nd_clientid.qval != clientid.qval)
3176 printf("EEK11 multiple clids\n");
3178 if ((nd->nd_flag & ND_NFSV41) != 0)
3179 printf("EEK! no clientid from session\n");
3180 nd->nd_flag |= ND_IMPLIEDCLID;
3181 nd->nd_clientid.qval = clientid.qval;
3183 nd->nd_repstat = nfsrv_openupdate(vp, stp, clientid, &stateid, nd, p);
3184 if (!nd->nd_repstat) {
3185 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID);
3186 *tl++ = txdr_unsigned(stateid.seqid);
3187 NFSBCOPY((caddr_t)stateid.other,(caddr_t)tl,NFSX_STATEIDOTHER);
3191 NFSEXITCODE2(error, nd);
3196 * nfsv4 open downgrade service
3199 nfsrvd_opendowngrade(struct nfsrv_descript *nd, __unused int isdgram,
3200 vnode_t vp, NFSPROC_T *p, __unused struct nfsexstuff *exp)
3204 struct nfsstate st, *stp = &st;
3206 nfsv4stateid_t stateid;
3209 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID + 3 * NFSX_UNSIGNED);
3210 stp->ls_ownerlen = 0;
3211 stp->ls_op = nd->nd_rp;
3212 stp->ls_uid = nd->nd_cred->cr_uid;
3213 stp->ls_stateid.seqid = fxdr_unsigned(u_int32_t, *tl++);
3214 NFSBCOPY((caddr_t)tl, (caddr_t)stp->ls_stateid.other,
3216 tl += (NFSX_STATEIDOTHER / NFSX_UNSIGNED);
3217 stp->ls_seq = fxdr_unsigned(u_int32_t, *tl++);
3218 i = fxdr_unsigned(int, *tl++);
3220 case NFSV4OPEN_ACCESSREAD:
3221 stp->ls_flags = (NFSLCK_READACCESS | NFSLCK_DOWNGRADE);
3223 case NFSV4OPEN_ACCESSWRITE:
3224 stp->ls_flags = (NFSLCK_WRITEACCESS | NFSLCK_DOWNGRADE);
3226 case NFSV4OPEN_ACCESSBOTH:
3227 stp->ls_flags = (NFSLCK_READACCESS | NFSLCK_WRITEACCESS |
3231 nd->nd_repstat = NFSERR_BADXDR;
3233 i = fxdr_unsigned(int, *tl);
3235 case NFSV4OPEN_DENYNONE:
3237 case NFSV4OPEN_DENYREAD:
3238 stp->ls_flags |= NFSLCK_READDENY;
3240 case NFSV4OPEN_DENYWRITE:
3241 stp->ls_flags |= NFSLCK_WRITEDENY;
3243 case NFSV4OPEN_DENYBOTH:
3244 stp->ls_flags |= (NFSLCK_READDENY | NFSLCK_WRITEDENY);
3247 nd->nd_repstat = NFSERR_BADXDR;
3250 clientid.lval[0] = stp->ls_stateid.other[0];
3251 clientid.lval[1] = stp->ls_stateid.other[1];
3252 if ((nd->nd_flag & ND_IMPLIEDCLID) != 0) {
3253 if ((nd->nd_flag & ND_NFSV41) != 0)
3254 clientid.qval = nd->nd_clientid.qval;
3255 else if (nd->nd_clientid.qval != clientid.qval)
3256 printf("EEK12 multiple clids\n");
3258 if ((nd->nd_flag & ND_NFSV41) != 0)
3259 printf("EEK! no clientid from session\n");
3260 nd->nd_flag |= ND_IMPLIEDCLID;
3261 nd->nd_clientid.qval = clientid.qval;
3263 if (!nd->nd_repstat)
3264 nd->nd_repstat = nfsrv_openupdate(vp, stp, clientid, &stateid,
3266 if (!nd->nd_repstat) {
3267 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID);
3268 *tl++ = txdr_unsigned(stateid.seqid);
3269 NFSBCOPY((caddr_t)stateid.other,(caddr_t)tl,NFSX_STATEIDOTHER);
3273 NFSEXITCODE2(error, nd);
3278 * nfsv4 renew lease service
3281 nfsrvd_renew(struct nfsrv_descript *nd, __unused int isdgram,
3282 __unused vnode_t vp, NFSPROC_T *p, __unused struct nfsexstuff *exp)
3288 if ((nd->nd_flag & ND_NFSV41) != 0) {
3289 nd->nd_repstat = NFSERR_NOTSUPP;
3292 if (nfs_rootfhset == 0 || nfsd_checkrootexp(nd) != 0) {
3293 nd->nd_repstat = NFSERR_WRONGSEC;
3296 NFSM_DISSECT(tl, u_int32_t *, NFSX_HYPER);
3297 clientid.lval[0] = *tl++;
3298 clientid.lval[1] = *tl;
3299 if ((nd->nd_flag & ND_IMPLIEDCLID) != 0) {
3300 if ((nd->nd_flag & ND_NFSV41) != 0)
3301 clientid.qval = nd->nd_clientid.qval;
3302 else if (nd->nd_clientid.qval != clientid.qval)
3303 printf("EEK13 multiple clids\n");
3305 if ((nd->nd_flag & ND_NFSV41) != 0)
3306 printf("EEK! no clientid from session\n");
3307 nd->nd_flag |= ND_IMPLIEDCLID;
3308 nd->nd_clientid.qval = clientid.qval;
3310 nd->nd_repstat = nfsrv_getclient(clientid, (CLOPS_RENEWOP|CLOPS_RENEW),
3311 NULL, NULL, (nfsquad_t)((u_quad_t)0), 0, nd, p);
3313 NFSEXITCODE2(error, nd);
3318 * nfsv4 security info service
3321 nfsrvd_secinfo(struct nfsrv_descript *nd, int isdgram,
3322 vnode_t dp, NFSPROC_T *p, struct nfsexstuff *exp)
3326 struct nameidata named;
3327 vnode_t dirp = NULL, vp;
3329 struct nfsexstuff retnes;
3331 int error = 0, savflag, i;
3336 * All this just to get the export flags for the name.
3338 NFSNAMEICNDSET(&named.ni_cnd, nd->nd_cred, LOOKUP,
3339 LOCKLEAF | SAVESTART);
3340 nfsvno_setpathbuf(&named, &bufp, &hashp);
3341 error = nfsrv_parsename(nd, bufp, hashp, &named.ni_pathlen);
3344 nfsvno_relpathbuf(&named);
3347 if (!nd->nd_repstat) {
3348 nd->nd_repstat = nfsvno_namei(nd, &named, dp, 1, exp, p, &dirp);
3351 nfsvno_relpathbuf(&named);
3357 vrele(named.ni_startdir);
3358 nfsvno_relpathbuf(&named);
3359 fh.nfsrvfh_len = NFSX_MYFH;
3361 nd->nd_repstat = nfsvno_getfh(vp, (fhandle_t *)fh.nfsrvfh_data, p);
3363 savflag = nd->nd_flag;
3364 if (!nd->nd_repstat) {
3365 nfsd_fhtovp(nd, &fh, LK_SHARED, &vp, &retnes, NULL, 0, p);
3369 nd->nd_flag = savflag;
3374 * Finally have the export flags for name, so we can create
3375 * the security info.
3378 NFSM_BUILD(sizp, u_int32_t *, NFSX_UNSIGNED);
3379 for (i = 0; i < retnes.nes_numsecflavor; i++) {
3380 if (retnes.nes_secflavors[i] == AUTH_SYS) {
3381 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
3382 *tl = txdr_unsigned(RPCAUTH_UNIX);
3384 } else if (retnes.nes_secflavors[i] == RPCSEC_GSS_KRB5) {
3385 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
3386 *tl++ = txdr_unsigned(RPCAUTH_GSS);
3387 (void) nfsm_strtom(nd, nfsgss_mechlist[KERBV_MECH].str,
3388 nfsgss_mechlist[KERBV_MECH].len);
3389 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
3390 *tl++ = txdr_unsigned(GSS_KERBV_QOP);
3391 *tl = txdr_unsigned(RPCAUTHGSS_SVCNONE);
3393 } else if (retnes.nes_secflavors[i] == RPCSEC_GSS_KRB5I) {
3394 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
3395 *tl++ = txdr_unsigned(RPCAUTH_GSS);
3396 (void) nfsm_strtom(nd, nfsgss_mechlist[KERBV_MECH].str,
3397 nfsgss_mechlist[KERBV_MECH].len);
3398 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
3399 *tl++ = txdr_unsigned(GSS_KERBV_QOP);
3400 *tl = txdr_unsigned(RPCAUTHGSS_SVCINTEGRITY);
3402 } else if (retnes.nes_secflavors[i] == RPCSEC_GSS_KRB5P) {
3403 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
3404 *tl++ = txdr_unsigned(RPCAUTH_GSS);
3405 (void) nfsm_strtom(nd, nfsgss_mechlist[KERBV_MECH].str,
3406 nfsgss_mechlist[KERBV_MECH].len);
3407 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
3408 *tl++ = txdr_unsigned(GSS_KERBV_QOP);
3409 *tl = txdr_unsigned(RPCAUTHGSS_SVCPRIVACY);
3413 *sizp = txdr_unsigned(len);
3416 NFSEXITCODE2(error, nd);
3421 * nfsv4 set client id service
3424 nfsrvd_setclientid(struct nfsrv_descript *nd, __unused int isdgram,
3425 __unused vnode_t vp, NFSPROC_T *p, __unused struct nfsexstuff *exp)
3429 int error = 0, idlen;
3430 struct nfsclient *clp = NULL;
3432 struct sockaddr_in *rin;
3435 struct sockaddr_in6 *rin6;
3437 #if defined(INET) || defined(INET6)
3440 u_char *verf, *addrbuf;
3441 nfsquad_t clientid, confirm;
3443 if ((nd->nd_flag & ND_NFSV41) != 0) {
3444 nd->nd_repstat = NFSERR_NOTSUPP;
3447 if (nfs_rootfhset == 0 || nfsd_checkrootexp(nd) != 0) {
3448 nd->nd_repstat = NFSERR_WRONGSEC;
3451 NFSM_DISSECT(tl, u_int32_t *, NFSX_VERF + NFSX_UNSIGNED);
3452 verf = (u_char *)tl;
3453 tl += (NFSX_VERF / NFSX_UNSIGNED);
3454 i = fxdr_unsigned(int, *tl);
3455 if (i > NFSV4_OPAQUELIMIT || i <= 0) {
3456 nd->nd_repstat = NFSERR_BADXDR;
3460 if (nd->nd_flag & ND_GSS)
3461 i += nd->nd_princlen;
3462 clp = malloc(sizeof(struct nfsclient) + i, M_NFSDCLIENT, M_WAITOK |
3464 clp->lc_stateid = malloc(sizeof(struct nfsstatehead) *
3465 nfsrv_statehashsize, M_NFSDCLIENT, M_WAITOK);
3466 NFSINITSOCKMUTEX(&clp->lc_req.nr_mtx);
3467 /* Allocated large enough for an AF_INET or AF_INET6 socket. */
3468 clp->lc_req.nr_nam = malloc(sizeof(struct sockaddr_in6), M_SONAME,
3470 clp->lc_req.nr_cred = NULL;
3471 NFSBCOPY(verf, clp->lc_verf, NFSX_VERF);
3472 clp->lc_idlen = idlen;
3473 error = nfsrv_mtostr(nd, clp->lc_id, idlen);
3476 if (nd->nd_flag & ND_GSS) {
3477 clp->lc_flags = LCL_GSS;
3478 if (nd->nd_flag & ND_GSSINTEGRITY)
3479 clp->lc_flags |= LCL_GSSINTEGRITY;
3480 else if (nd->nd_flag & ND_GSSPRIVACY)
3481 clp->lc_flags |= LCL_GSSPRIVACY;
3485 if ((nd->nd_flag & ND_GSS) && nd->nd_princlen > 0) {
3486 clp->lc_flags |= LCL_NAME;
3487 clp->lc_namelen = nd->nd_princlen;
3488 clp->lc_name = &clp->lc_id[idlen];
3489 NFSBCOPY(nd->nd_principal, clp->lc_name, clp->lc_namelen);
3491 clp->lc_uid = nd->nd_cred->cr_uid;
3492 clp->lc_gid = nd->nd_cred->cr_gid;
3494 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3495 clp->lc_program = fxdr_unsigned(u_int32_t, *tl);
3496 error = nfsrv_getclientipaddr(nd, clp);
3499 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3500 clp->lc_callback = fxdr_unsigned(u_int32_t, *tl);
3503 * nfsrv_setclient() does the actual work of adding it to the
3504 * client list. If there is no error, the structure has been
3505 * linked into the client list and clp should no longer be used
3506 * here. When an error is returned, it has not been linked in,
3507 * so it should be free'd.
3509 nd->nd_repstat = nfsrv_setclient(nd, &clp, &clientid, &confirm, p);
3510 if (nd->nd_repstat == NFSERR_CLIDINUSE) {
3512 * 8 is the maximum length of the port# string.
3514 addrbuf = malloc(INET6_ADDRSTRLEN + 8, M_TEMP, M_WAITOK);
3515 switch (clp->lc_req.nr_nam->sa_family) {
3518 if (clp->lc_flags & LCL_TCPCALLBACK)
3519 (void) nfsm_strtom(nd, "tcp", 3);
3521 (void) nfsm_strtom(nd, "udp", 3);
3522 rin = (struct sockaddr_in *)clp->lc_req.nr_nam;
3523 ucp = (u_char *)&rin->sin_addr.s_addr;
3524 ucp2 = (u_char *)&rin->sin_port;
3525 sprintf(addrbuf, "%d.%d.%d.%d.%d.%d", ucp[0] & 0xff,
3526 ucp[1] & 0xff, ucp[2] & 0xff, ucp[3] & 0xff,
3527 ucp2[0] & 0xff, ucp2[1] & 0xff);
3532 if (clp->lc_flags & LCL_TCPCALLBACK)
3533 (void) nfsm_strtom(nd, "tcp6", 4);
3535 (void) nfsm_strtom(nd, "udp6", 4);
3536 rin6 = (struct sockaddr_in6 *)clp->lc_req.nr_nam;
3537 ucp = inet_ntop(AF_INET6, &rin6->sin6_addr, addrbuf,
3543 ucp2 = (u_char *)&rin6->sin6_port;
3544 sprintf(&addrbuf[i], ".%d.%d", ucp2[0] & 0xff,
3549 (void) nfsm_strtom(nd, addrbuf, strlen(addrbuf));
3550 free(addrbuf, M_TEMP);
3553 NFSSOCKADDRFREE(clp->lc_req.nr_nam);
3554 NFSFREEMUTEX(&clp->lc_req.nr_mtx);
3555 free(clp->lc_stateid, M_NFSDCLIENT);
3556 free(clp, M_NFSDCLIENT);
3558 if (!nd->nd_repstat) {
3559 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_HYPER);
3560 *tl++ = clientid.lval[0];
3561 *tl++ = clientid.lval[1];
3562 *tl++ = confirm.lval[0];
3563 *tl = confirm.lval[1];
3567 NFSEXITCODE2(0, nd);
3571 NFSSOCKADDRFREE(clp->lc_req.nr_nam);
3572 NFSFREEMUTEX(&clp->lc_req.nr_mtx);
3573 free(clp->lc_stateid, M_NFSDCLIENT);
3574 free(clp, M_NFSDCLIENT);
3576 NFSEXITCODE2(error, nd);
3581 * nfsv4 set client id confirm service
3584 nfsrvd_setclientidcfrm(struct nfsrv_descript *nd,
3585 __unused int isdgram, __unused vnode_t vp, NFSPROC_T *p,
3586 __unused struct nfsexstuff *exp)
3590 nfsquad_t clientid, confirm;
3592 if ((nd->nd_flag & ND_NFSV41) != 0) {
3593 nd->nd_repstat = NFSERR_NOTSUPP;
3596 if (nfs_rootfhset == 0 || nfsd_checkrootexp(nd) != 0) {
3597 nd->nd_repstat = NFSERR_WRONGSEC;
3600 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_HYPER);
3601 clientid.lval[0] = *tl++;
3602 clientid.lval[1] = *tl++;
3603 confirm.lval[0] = *tl++;
3604 confirm.lval[1] = *tl;
3607 * nfsrv_getclient() searches the client list for a match and
3608 * returns the appropriate NFSERR status.
3610 nd->nd_repstat = nfsrv_getclient(clientid, (CLOPS_CONFIRM|CLOPS_RENEW),
3611 NULL, NULL, confirm, 0, nd, p);
3613 NFSEXITCODE2(error, nd);
3618 * nfsv4 verify service
3621 nfsrvd_verify(struct nfsrv_descript *nd, int isdgram,
3622 vnode_t vp, NFSPROC_T *p, __unused struct nfsexstuff *exp)
3624 int error = 0, ret, fhsize = NFSX_MYFH;
3625 struct nfsvattr nva;
3627 struct nfsfsinfo fs;
3630 nd->nd_repstat = nfsvno_getattr(vp, &nva, nd->nd_cred, p, 1);
3631 if (!nd->nd_repstat)
3632 nd->nd_repstat = nfsvno_statfs(vp, &sf);
3633 if (!nd->nd_repstat)
3634 nd->nd_repstat = nfsvno_getfh(vp, &fh, p);
3635 if (!nd->nd_repstat) {
3636 nfsvno_getfs(&fs, isdgram);
3637 error = nfsv4_loadattr(nd, vp, &nva, NULL, &fh, fhsize, NULL,
3638 &sf, NULL, &fs, NULL, 1, &ret, NULL, NULL, p, nd->nd_cred);
3640 if (nd->nd_procnum == NFSV4OP_NVERIFY) {
3642 nd->nd_repstat = NFSERR_SAME;
3643 else if (ret != NFSERR_NOTSAME)
3644 nd->nd_repstat = ret;
3646 nd->nd_repstat = ret;
3650 NFSEXITCODE2(error, nd);
3658 nfsrvd_openattr(struct nfsrv_descript *nd, __unused int isdgram,
3659 vnode_t dp, __unused vnode_t *vpp, __unused fhandle_t *fhp,
3660 __unused NFSPROC_T *p, __unused struct nfsexstuff *exp)
3663 int error = 0, createdir;
3665 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3666 createdir = fxdr_unsigned(int, *tl);
3667 nd->nd_repstat = NFSERR_NOTSUPP;
3670 NFSEXITCODE2(error, nd);
3675 * nfsv4 release lock owner service
3678 nfsrvd_releaselckown(struct nfsrv_descript *nd, __unused int isdgram,
3679 __unused vnode_t vp, NFSPROC_T *p, __unused struct nfsexstuff *exp)
3682 struct nfsstate *stp = NULL;
3686 if ((nd->nd_flag & ND_NFSV41) != 0) {
3687 nd->nd_repstat = NFSERR_NOTSUPP;
3690 if (nfs_rootfhset == 0 || nfsd_checkrootexp(nd) != 0) {
3691 nd->nd_repstat = NFSERR_WRONGSEC;
3694 NFSM_DISSECT(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
3695 len = fxdr_unsigned(int, *(tl + 2));
3696 if (len <= 0 || len > NFSV4_OPAQUELIMIT) {
3697 nd->nd_repstat = NFSERR_BADXDR;
3700 MALLOC(stp, struct nfsstate *, sizeof (struct nfsstate) + len,
3701 M_NFSDSTATE, M_WAITOK);
3702 stp->ls_ownerlen = len;
3704 stp->ls_flags = NFSLCK_RELEASE;
3705 stp->ls_uid = nd->nd_cred->cr_uid;
3706 clientid.lval[0] = *tl++;
3707 clientid.lval[1] = *tl;
3708 if ((nd->nd_flag & ND_IMPLIEDCLID) != 0) {
3709 if ((nd->nd_flag & ND_NFSV41) != 0)
3710 clientid.qval = nd->nd_clientid.qval;
3711 else if (nd->nd_clientid.qval != clientid.qval)
3712 printf("EEK14 multiple clids\n");
3714 if ((nd->nd_flag & ND_NFSV41) != 0)
3715 printf("EEK! no clientid from session\n");
3716 nd->nd_flag |= ND_IMPLIEDCLID;
3717 nd->nd_clientid.qval = clientid.qval;
3719 error = nfsrv_mtostr(nd, stp->ls_owner, len);
3722 nd->nd_repstat = nfsrv_releaselckown(stp, clientid, p);
3723 FREE((caddr_t)stp, M_NFSDSTATE);
3725 NFSEXITCODE2(0, nd);
3729 free((caddr_t)stp, M_NFSDSTATE);
3730 NFSEXITCODE2(error, nd);
3735 * nfsv4 exchange_id service
3738 nfsrvd_exchangeid(struct nfsrv_descript *nd, __unused int isdgram,
3739 __unused vnode_t vp, NFSPROC_T *p, __unused struct nfsexstuff *exp)
3742 int error = 0, i, idlen;
3743 struct nfsclient *clp = NULL;
3744 nfsquad_t clientid, confirm;
3746 uint32_t sp4type, v41flags;
3747 uint64_t owner_minor;
3748 struct timespec verstime;
3750 struct sockaddr_in *sin, *rin;
3753 struct sockaddr_in6 *sin6, *rin6;
3756 if (nfs_rootfhset == 0 || nfsd_checkrootexp(nd) != 0) {
3757 nd->nd_repstat = NFSERR_WRONGSEC;
3760 NFSM_DISSECT(tl, u_int32_t *, NFSX_VERF + NFSX_UNSIGNED);
3761 verf = (uint8_t *)tl;
3762 tl += (NFSX_VERF / NFSX_UNSIGNED);
3763 i = fxdr_unsigned(int, *tl);
3764 if (i > NFSV4_OPAQUELIMIT || i <= 0) {
3765 nd->nd_repstat = NFSERR_BADXDR;
3769 if (nd->nd_flag & ND_GSS)
3770 i += nd->nd_princlen;
3771 clp = malloc(sizeof(struct nfsclient) + i, M_NFSDCLIENT, M_WAITOK |
3773 clp->lc_stateid = malloc(sizeof(struct nfsstatehead) *
3774 nfsrv_statehashsize, M_NFSDCLIENT, M_WAITOK);
3775 NFSINITSOCKMUTEX(&clp->lc_req.nr_mtx);
3776 /* Allocated large enough for an AF_INET or AF_INET6 socket. */
3777 clp->lc_req.nr_nam = malloc(sizeof(struct sockaddr_in6), M_SONAME,
3779 switch (nd->nd_nam->sa_family) {
3782 rin = (struct sockaddr_in *)clp->lc_req.nr_nam;
3783 sin = (struct sockaddr_in *)nd->nd_nam;
3784 rin->sin_family = AF_INET;
3785 rin->sin_len = sizeof(struct sockaddr_in);
3787 rin->sin_addr.s_addr = sin->sin_addr.s_addr;
3792 rin6 = (struct sockaddr_in6 *)clp->lc_req.nr_nam;
3793 sin6 = (struct sockaddr_in6 *)nd->nd_nam;
3794 rin6->sin6_family = AF_INET6;
3795 rin6->sin6_len = sizeof(struct sockaddr_in6);
3796 rin6->sin6_port = 0;
3797 rin6->sin6_addr = sin6->sin6_addr;
3801 clp->lc_req.nr_cred = NULL;
3802 NFSBCOPY(verf, clp->lc_verf, NFSX_VERF);
3803 clp->lc_idlen = idlen;
3804 error = nfsrv_mtostr(nd, clp->lc_id, idlen);
3807 if ((nd->nd_flag & ND_GSS) != 0) {
3808 clp->lc_flags = LCL_GSS | LCL_NFSV41;
3809 if ((nd->nd_flag & ND_GSSINTEGRITY) != 0)
3810 clp->lc_flags |= LCL_GSSINTEGRITY;
3811 else if ((nd->nd_flag & ND_GSSPRIVACY) != 0)
3812 clp->lc_flags |= LCL_GSSPRIVACY;
3814 clp->lc_flags = LCL_NFSV41;
3815 if ((nd->nd_flag & ND_GSS) != 0 && nd->nd_princlen > 0) {
3816 clp->lc_flags |= LCL_NAME;
3817 clp->lc_namelen = nd->nd_princlen;
3818 clp->lc_name = &clp->lc_id[idlen];
3819 NFSBCOPY(nd->nd_principal, clp->lc_name, clp->lc_namelen);
3821 clp->lc_uid = nd->nd_cred->cr_uid;
3822 clp->lc_gid = nd->nd_cred->cr_gid;
3824 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
3825 v41flags = fxdr_unsigned(uint32_t, *tl++);
3826 if ((v41flags & ~(NFSV4EXCH_SUPPMOVEDREFER | NFSV4EXCH_SUPPMOVEDMIGR |
3827 NFSV4EXCH_BINDPRINCSTATEID | NFSV4EXCH_MASKPNFS |
3828 NFSV4EXCH_UPDCONFIRMEDRECA)) != 0) {
3829 nd->nd_repstat = NFSERR_INVAL;
3832 if ((v41flags & NFSV4EXCH_UPDCONFIRMEDRECA) != 0)
3833 confirm.lval[1] = 1;
3835 confirm.lval[1] = 0;
3836 v41flags = NFSV4EXCH_USENONPNFS;
3837 sp4type = fxdr_unsigned(uint32_t, *tl);
3838 if (sp4type != NFSV4EXCH_SP4NONE) {
3839 nd->nd_repstat = NFSERR_NOTSUPP;
3844 * nfsrv_setclient() does the actual work of adding it to the
3845 * client list. If there is no error, the structure has been
3846 * linked into the client list and clp should no longer be used
3847 * here. When an error is returned, it has not been linked in,
3848 * so it should be free'd.
3850 nd->nd_repstat = nfsrv_setclient(nd, &clp, &clientid, &confirm, p);
3852 NFSSOCKADDRFREE(clp->lc_req.nr_nam);
3853 NFSFREEMUTEX(&clp->lc_req.nr_mtx);
3854 free(clp->lc_stateid, M_NFSDCLIENT);
3855 free(clp, M_NFSDCLIENT);
3857 if (nd->nd_repstat == 0) {
3858 if (confirm.lval[1] != 0)
3859 v41flags |= NFSV4EXCH_CONFIRMEDR;
3860 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_HYPER + 3 * NFSX_UNSIGNED);
3861 *tl++ = clientid.lval[0]; /* ClientID */
3862 *tl++ = clientid.lval[1];
3863 *tl++ = txdr_unsigned(confirm.lval[0]); /* SequenceID */
3864 *tl++ = txdr_unsigned(v41flags); /* Exch flags */
3865 *tl++ = txdr_unsigned(NFSV4EXCH_SP4NONE); /* No SSV */
3866 owner_minor = 0; /* Owner */
3867 txdr_hyper(owner_minor, tl); /* Minor */
3868 (void)nfsm_strtom(nd, nd->nd_cred->cr_prison->pr_hostuuid,
3869 strlen(nd->nd_cred->cr_prison->pr_hostuuid)); /* Major */
3870 (void)nfsm_strtom(nd, nd->nd_cred->cr_prison->pr_hostuuid,
3871 strlen(nd->nd_cred->cr_prison->pr_hostuuid)); /* Scope */
3872 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
3873 *tl = txdr_unsigned(1);
3874 (void)nfsm_strtom(nd, "freebsd.org", strlen("freebsd.org"));
3875 (void)nfsm_strtom(nd, version, strlen(version));
3876 NFSM_BUILD(tl, uint32_t *, NFSX_V4TIME);
3877 verstime.tv_sec = 1293840000; /* Jan 1, 2011 */
3878 verstime.tv_nsec = 0;
3879 txdr_nfsv4time(&verstime, tl);
3881 NFSEXITCODE2(0, nd);
3885 NFSSOCKADDRFREE(clp->lc_req.nr_nam);
3886 NFSFREEMUTEX(&clp->lc_req.nr_mtx);
3887 free(clp->lc_stateid, M_NFSDCLIENT);
3888 free(clp, M_NFSDCLIENT);
3890 NFSEXITCODE2(error, nd);
3895 * nfsv4 create session service
3898 nfsrvd_createsession(struct nfsrv_descript *nd, __unused int isdgram,
3899 __unused vnode_t vp, NFSPROC_T *p, __unused struct nfsexstuff *exp)
3903 nfsquad_t clientid, confirm;
3904 struct nfsdsession *sep = NULL;
3907 if (nfs_rootfhset == 0 || nfsd_checkrootexp(nd) != 0) {
3908 nd->nd_repstat = NFSERR_WRONGSEC;
3911 sep = (struct nfsdsession *)malloc(sizeof(struct nfsdsession),
3912 M_NFSDSESSION, M_WAITOK | M_ZERO);
3913 sep->sess_refcnt = 1;
3914 mtx_init(&sep->sess_cbsess.nfsess_mtx, "nfscbsession", NULL, MTX_DEF);
3915 NFSM_DISSECT(tl, uint32_t *, NFSX_HYPER + 2 * NFSX_UNSIGNED);
3916 clientid.lval[0] = *tl++;
3917 clientid.lval[1] = *tl++;
3918 confirm.lval[0] = fxdr_unsigned(uint32_t, *tl++);
3919 sep->sess_crflags = fxdr_unsigned(uint32_t, *tl);
3920 /* Persistent sessions and RDMA are not supported. */
3921 sep->sess_crflags &= NFSV4CRSESS_CONNBACKCHAN;
3923 /* Fore channel attributes. */
3924 NFSM_DISSECT(tl, uint32_t *, 7 * NFSX_UNSIGNED);
3925 tl++; /* Header pad always 0. */
3926 sep->sess_maxreq = fxdr_unsigned(uint32_t, *tl++);
3927 sep->sess_maxresp = fxdr_unsigned(uint32_t, *tl++);
3928 sep->sess_maxrespcached = fxdr_unsigned(uint32_t, *tl++);
3929 sep->sess_maxops = fxdr_unsigned(uint32_t, *tl++);
3930 sep->sess_maxslots = fxdr_unsigned(uint32_t, *tl++);
3931 if (sep->sess_maxslots > NFSV4_SLOTS)
3932 sep->sess_maxslots = NFSV4_SLOTS;
3933 rdmacnt = fxdr_unsigned(uint32_t, *tl);
3935 nd->nd_repstat = NFSERR_BADXDR;
3937 } else if (rdmacnt == 1)
3938 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
3940 /* Back channel attributes. */
3941 NFSM_DISSECT(tl, uint32_t *, 7 * NFSX_UNSIGNED);
3942 tl++; /* Header pad always 0. */
3943 sep->sess_cbmaxreq = fxdr_unsigned(uint32_t, *tl++);
3944 sep->sess_cbmaxresp = fxdr_unsigned(uint32_t, *tl++);
3945 sep->sess_cbmaxrespcached = fxdr_unsigned(uint32_t, *tl++);
3946 sep->sess_cbmaxops = fxdr_unsigned(uint32_t, *tl++);
3947 sep->sess_cbsess.nfsess_foreslots = fxdr_unsigned(uint32_t, *tl++);
3948 rdmacnt = fxdr_unsigned(uint32_t, *tl);
3950 nd->nd_repstat = NFSERR_BADXDR;
3952 } else if (rdmacnt == 1)
3953 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
3955 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
3956 sep->sess_cbprogram = fxdr_unsigned(uint32_t, *tl);
3959 * nfsrv_getclient() searches the client list for a match and
3960 * returns the appropriate NFSERR status.
3962 nd->nd_repstat = nfsrv_getclient(clientid, CLOPS_CONFIRM | CLOPS_RENEW,
3963 NULL, sep, confirm, sep->sess_cbprogram, nd, p);
3964 if (nd->nd_repstat == 0) {
3965 NFSM_BUILD(tl, uint32_t *, NFSX_V4SESSIONID);
3966 NFSBCOPY(sep->sess_sessionid, tl, NFSX_V4SESSIONID);
3967 NFSM_BUILD(tl, uint32_t *, 18 * NFSX_UNSIGNED);
3968 *tl++ = txdr_unsigned(confirm.lval[0]); /* sequenceid */
3969 *tl++ = txdr_unsigned(sep->sess_crflags);
3971 /* Fore channel attributes. */
3973 *tl++ = txdr_unsigned(sep->sess_maxreq);
3974 *tl++ = txdr_unsigned(sep->sess_maxresp);
3975 *tl++ = txdr_unsigned(sep->sess_maxrespcached);
3976 *tl++ = txdr_unsigned(sep->sess_maxops);
3977 *tl++ = txdr_unsigned(sep->sess_maxslots);
3978 *tl++ = txdr_unsigned(1);
3979 *tl++ = txdr_unsigned(0); /* No RDMA. */
3981 /* Back channel attributes. */
3983 *tl++ = txdr_unsigned(sep->sess_cbmaxreq);
3984 *tl++ = txdr_unsigned(sep->sess_cbmaxresp);
3985 *tl++ = txdr_unsigned(sep->sess_cbmaxrespcached);
3986 *tl++ = txdr_unsigned(sep->sess_cbmaxops);
3987 *tl++ = txdr_unsigned(sep->sess_cbsess.nfsess_foreslots);
3988 *tl++ = txdr_unsigned(1);
3989 *tl = txdr_unsigned(0); /* No RDMA. */
3992 if (nd->nd_repstat != 0 && sep != NULL)
3993 free(sep, M_NFSDSESSION);
3994 NFSEXITCODE2(error, nd);
3999 * nfsv4 sequence service
4002 nfsrvd_sequence(struct nfsrv_descript *nd, __unused int isdgram,
4003 __unused vnode_t vp, __unused NFSPROC_T *p, __unused struct nfsexstuff *exp)
4006 uint32_t highest_slotid, sequenceid, sflags, target_highest_slotid;
4007 int cache_this, error = 0;
4009 if (nfs_rootfhset == 0 || nfsd_checkrootexp(nd) != 0) {
4010 nd->nd_repstat = NFSERR_WRONGSEC;
4013 NFSM_DISSECT(tl, uint32_t *, NFSX_V4SESSIONID);
4014 NFSBCOPY(tl, nd->nd_sessionid, NFSX_V4SESSIONID);
4015 NFSM_DISSECT(tl, uint32_t *, 4 * NFSX_UNSIGNED);
4016 sequenceid = fxdr_unsigned(uint32_t, *tl++);
4017 nd->nd_slotid = fxdr_unsigned(uint32_t, *tl++);
4018 highest_slotid = fxdr_unsigned(uint32_t, *tl++);
4019 if (*tl == newnfs_true)
4023 nd->nd_flag |= ND_HASSEQUENCE;
4024 nd->nd_repstat = nfsrv_checksequence(nd, sequenceid, &highest_slotid,
4025 &target_highest_slotid, cache_this, &sflags, p);
4026 if (nd->nd_repstat == 0) {
4027 NFSM_BUILD(tl, uint32_t *, NFSX_V4SESSIONID);
4028 NFSBCOPY(nd->nd_sessionid, tl, NFSX_V4SESSIONID);
4029 NFSM_BUILD(tl, uint32_t *, 5 * NFSX_UNSIGNED);
4030 *tl++ = txdr_unsigned(sequenceid);
4031 *tl++ = txdr_unsigned(nd->nd_slotid);
4032 *tl++ = txdr_unsigned(highest_slotid);
4033 *tl++ = txdr_unsigned(target_highest_slotid);
4034 *tl = txdr_unsigned(sflags);
4037 NFSEXITCODE2(error, nd);
4042 * nfsv4 reclaim complete service
4045 nfsrvd_reclaimcomplete(struct nfsrv_descript *nd, __unused int isdgram,
4046 __unused vnode_t vp, __unused NFSPROC_T *p, __unused struct nfsexstuff *exp)
4049 int error = 0, onefs;
4051 if (nfs_rootfhset == 0 || nfsd_checkrootexp(nd) != 0) {
4052 nd->nd_repstat = NFSERR_WRONGSEC;
4055 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
4057 * I believe that a ReclaimComplete with rca_one_fs == TRUE is only
4058 * to be used after a file system has been transferred to a different
4059 * file server. However, RFC5661 is somewhat vague w.r.t. this and
4060 * the ESXi 6.7 client does both a ReclaimComplete with rca_one_fs
4061 * == TRUE and one with ReclaimComplete with rca_one_fs == FALSE.
4062 * Therefore, just ignore the rca_one_fs == TRUE operation and return
4063 * NFS_OK without doing anything.
4066 if (*tl == newnfs_true)
4068 nd->nd_repstat = nfsrv_checkreclaimcomplete(nd, onefs);
4070 NFSEXITCODE2(error, nd);
4075 * nfsv4 destroy clientid service
4078 nfsrvd_destroyclientid(struct nfsrv_descript *nd, __unused int isdgram,
4079 __unused vnode_t vp, NFSPROC_T *p, __unused struct nfsexstuff *exp)
4085 if (nfs_rootfhset == 0 || nfsd_checkrootexp(nd) != 0) {
4086 nd->nd_repstat = NFSERR_WRONGSEC;
4089 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
4090 clientid.lval[0] = *tl++;
4091 clientid.lval[1] = *tl;
4092 nd->nd_repstat = nfsrv_destroyclient(clientid, p);
4094 NFSEXITCODE2(error, nd);
4099 * nfsv4 bind connection to session service
4102 nfsrvd_bindconnsess(struct nfsrv_descript *nd, __unused int isdgram,
4103 __unused vnode_t vp, NFSPROC_T *p, __unused struct nfsexstuff *exp)
4106 uint8_t sessid[NFSX_V4SESSIONID];
4107 int error = 0, foreaft;
4109 if (nfs_rootfhset == 0 || nfsd_checkrootexp(nd) != 0) {
4110 nd->nd_repstat = NFSERR_WRONGSEC;
4113 NFSM_DISSECT(tl, uint32_t *, NFSX_V4SESSIONID + 2 * NFSX_UNSIGNED);
4114 NFSBCOPY(tl, sessid, NFSX_V4SESSIONID);
4115 tl += (NFSX_V4SESSIONID / NFSX_UNSIGNED);
4116 foreaft = fxdr_unsigned(int, *tl++);
4117 if (*tl == newnfs_true) {
4118 /* RDMA is not supported. */
4119 nd->nd_repstat = NFSERR_NOTSUPP;
4123 nd->nd_repstat = nfsrv_bindconnsess(nd, sessid, &foreaft);
4124 if (nd->nd_repstat == 0) {
4125 NFSM_BUILD(tl, uint32_t *, NFSX_V4SESSIONID + 2 *
4127 NFSBCOPY(sessid, tl, NFSX_V4SESSIONID);
4128 tl += (NFSX_V4SESSIONID / NFSX_UNSIGNED);
4129 *tl++ = txdr_unsigned(foreaft);
4133 NFSEXITCODE2(error, nd);
4138 * nfsv4 destroy session service
4141 nfsrvd_destroysession(struct nfsrv_descript *nd, __unused int isdgram,
4142 __unused vnode_t vp, __unused NFSPROC_T *p, __unused struct nfsexstuff *exp)
4144 uint8_t *cp, sessid[NFSX_V4SESSIONID];
4147 if (nfs_rootfhset == 0 || nfsd_checkrootexp(nd) != 0) {
4148 nd->nd_repstat = NFSERR_WRONGSEC;
4151 NFSM_DISSECT(cp, uint8_t *, NFSX_V4SESSIONID);
4152 NFSBCOPY(cp, sessid, NFSX_V4SESSIONID);
4153 nd->nd_repstat = nfsrv_destroysession(nd, sessid);
4155 NFSEXITCODE2(error, nd);
4160 * nfsv4 free stateid service
4163 nfsrvd_freestateid(struct nfsrv_descript *nd, __unused int isdgram,
4164 __unused vnode_t vp, NFSPROC_T *p, __unused struct nfsexstuff *exp)
4167 nfsv4stateid_t stateid;
4170 if (nfs_rootfhset == 0 || nfsd_checkrootexp(nd) != 0) {
4171 nd->nd_repstat = NFSERR_WRONGSEC;
4174 NFSM_DISSECT(tl, uint32_t *, NFSX_STATEID);
4175 stateid.seqid = fxdr_unsigned(uint32_t, *tl++);
4176 NFSBCOPY(tl, stateid.other, NFSX_STATEIDOTHER);
4177 nd->nd_repstat = nfsrv_freestateid(nd, &stateid, p);
4179 NFSEXITCODE2(error, nd);
4184 * nfsv4 test stateid service
4187 nfsrvd_teststateid(struct nfsrv_descript *nd, __unused int isdgram,
4188 __unused vnode_t vp, NFSPROC_T *p, __unused struct nfsexstuff *exp)
4191 nfsv4stateid_t *stateidp = NULL, *tstateidp;
4192 int cnt, error = 0, i, ret;
4194 if (nfs_rootfhset == 0 || nfsd_checkrootexp(nd) != 0) {
4195 nd->nd_repstat = NFSERR_WRONGSEC;
4198 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
4199 cnt = fxdr_unsigned(int, *tl);
4200 if (cnt <= 0 || cnt > 1024) {
4201 nd->nd_repstat = NFSERR_BADXDR;
4204 stateidp = mallocarray(cnt, sizeof(nfsv4stateid_t), M_TEMP, M_WAITOK);
4205 tstateidp = stateidp;
4206 for (i = 0; i < cnt; i++) {
4207 NFSM_DISSECT(tl, uint32_t *, NFSX_STATEID);
4208 tstateidp->seqid = fxdr_unsigned(uint32_t, *tl++);
4209 NFSBCOPY(tl, tstateidp->other, NFSX_STATEIDOTHER);
4212 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
4213 *tl = txdr_unsigned(cnt);
4214 tstateidp = stateidp;
4215 for (i = 0; i < cnt; i++) {
4216 ret = nfsrv_teststateid(nd, tstateidp, p);
4217 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
4218 *tl = txdr_unsigned(ret);
4222 free(stateidp, M_TEMP);
4223 NFSEXITCODE2(error, nd);
4228 * nfsv4 service not supported
4231 nfsrvd_notsupp(struct nfsrv_descript *nd, __unused int isdgram,
4232 __unused vnode_t vp, __unused NFSPROC_T *p, __unused struct nfsexstuff *exp)
4235 nd->nd_repstat = NFSERR_NOTSUPP;
4236 NFSEXITCODE2(0, nd);