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 * nfs version 2, 3 and 4 server calls to vnode ops
39 * - these routines generally have 3 phases
40 * 1 - break down and validate rpc request in mbuf list
41 * 2 - do the vnode ops for the request, usually by calling a nfsvno_XXX()
42 * function in nfsd_port.c
43 * 3 - build the rpc reply in an mbuf list
44 * For nfsv4, these functions are called for each Op within the Compound RPC.
48 #include <fs/nfs/nfsport.h>
51 extern u_int32_t newnfs_false, newnfs_true;
52 extern enum vtype nv34tov_type[8];
53 extern struct timeval nfsboottime;
54 extern int nfs_rootfhset;
55 extern int nfsrv_enable_crossmntpt;
56 extern int nfsrv_statehashsize;
57 #endif /* !APPLEKEXT */
59 static int nfs_async = 0;
60 SYSCTL_DECL(_vfs_nfsd);
61 SYSCTL_INT(_vfs_nfsd, OID_AUTO, async, CTLFLAG_RW, &nfs_async, 0,
62 "Tell client that writes were synced even though they were not");
65 * This list defines the GSS mechanisms supported.
66 * (Don't ask me how you get these strings from the RFC stuff like
67 * iso(1), org(3)... but someone did it, so I don't need to know.)
69 static struct nfsgss_mechlist nfsgss_mechlist[] = {
70 { 9, "\052\206\110\206\367\022\001\002\002", 11 },
75 static void nfsrvd_symlinksub(struct nfsrv_descript *nd, struct nameidata *ndp,
76 struct nfsvattr *nvap, fhandle_t *fhp, vnode_t *vpp,
77 vnode_t dirp, struct nfsvattr *dirforp, struct nfsvattr *diraftp,
78 int *diraft_retp, nfsattrbit_t *attrbitp,
79 NFSACL_T *aclp, NFSPROC_T *p, struct nfsexstuff *exp, char *pathcp,
81 static void nfsrvd_mkdirsub(struct nfsrv_descript *nd, struct nameidata *ndp,
82 struct nfsvattr *nvap, fhandle_t *fhp, vnode_t *vpp,
83 vnode_t dirp, struct nfsvattr *dirforp, struct nfsvattr *diraftp,
84 int *diraft_retp, nfsattrbit_t *attrbitp, NFSACL_T *aclp,
85 NFSPROC_T *p, struct nfsexstuff *exp);
88 * nfs access service (not a part of NFS V2)
91 nfsrvd_access(struct nfsrv_descript *nd, __unused int isdgram,
92 vnode_t vp, NFSPROC_T *p, struct nfsexstuff *exp)
95 int getret, error = 0;
97 u_int32_t testmode, nfsmode, supported = 0;
100 if (nd->nd_repstat) {
101 nfsrv_postopattr(nd, 1, &nva);
104 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
105 nfsmode = fxdr_unsigned(u_int32_t, *tl);
106 if ((nd->nd_flag & ND_NFSV4) &&
107 (nfsmode & ~(NFSACCESS_READ | NFSACCESS_LOOKUP |
108 NFSACCESS_MODIFY | NFSACCESS_EXTEND | NFSACCESS_DELETE |
109 NFSACCESS_EXECUTE))) {
110 nd->nd_repstat = NFSERR_INVAL;
114 if (nfsmode & NFSACCESS_READ) {
115 supported |= NFSACCESS_READ;
116 if (nfsvno_accchk(vp, VREAD, nd->nd_cred, exp, p,
117 NFSACCCHK_NOOVERRIDE, NFSACCCHK_VPISLOCKED, &supported))
118 nfsmode &= ~NFSACCESS_READ;
120 if (nfsmode & NFSACCESS_MODIFY) {
121 supported |= NFSACCESS_MODIFY;
122 if (nfsvno_accchk(vp, VWRITE, nd->nd_cred, exp, p,
123 NFSACCCHK_NOOVERRIDE, NFSACCCHK_VPISLOCKED, &supported))
124 nfsmode &= ~NFSACCESS_MODIFY;
126 if (nfsmode & NFSACCESS_EXTEND) {
127 supported |= NFSACCESS_EXTEND;
128 if (nfsvno_accchk(vp, VWRITE | VAPPEND, nd->nd_cred, exp, p,
129 NFSACCCHK_NOOVERRIDE, NFSACCCHK_VPISLOCKED, &supported))
130 nfsmode &= ~NFSACCESS_EXTEND;
132 if (nfsmode & NFSACCESS_DELETE) {
133 supported |= NFSACCESS_DELETE;
134 if (vp->v_type == VDIR)
135 deletebit = VDELETE_CHILD;
138 if (nfsvno_accchk(vp, deletebit, nd->nd_cred, exp, p,
139 NFSACCCHK_NOOVERRIDE, NFSACCCHK_VPISLOCKED, &supported))
140 nfsmode &= ~NFSACCESS_DELETE;
142 if (vnode_vtype(vp) == VDIR)
143 testmode = NFSACCESS_LOOKUP;
145 testmode = NFSACCESS_EXECUTE;
146 if (nfsmode & testmode) {
147 supported |= (nfsmode & testmode);
148 if (nfsvno_accchk(vp, VEXEC, nd->nd_cred, exp, p,
149 NFSACCCHK_NOOVERRIDE, NFSACCCHK_VPISLOCKED, &supported))
150 nfsmode &= ~testmode;
152 nfsmode &= supported;
153 if (nd->nd_flag & ND_NFSV3) {
154 getret = nfsvno_getattr(vp, &nva, nd->nd_cred, p, 1);
155 nfsrv_postopattr(nd, getret, &nva);
158 if (nd->nd_flag & ND_NFSV4) {
159 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
160 *tl++ = txdr_unsigned(supported);
162 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
163 *tl = txdr_unsigned(nfsmode);
170 NFSEXITCODE2(error, nd);
175 * nfs getattr service
178 nfsrvd_getattr(struct nfsrv_descript *nd, int isdgram,
179 vnode_t vp, NFSPROC_T *p, __unused struct nfsexstuff *exp)
183 int at_root = 0, error = 0, supports_nfsv4acls;
184 struct nfsreferral *refp;
185 nfsattrbit_t attrbits, tmpbits;
187 struct vnode *tvp = NULL;
189 uint64_t mounted_on_fileno = 0;
194 if (nd->nd_flag & ND_NFSV4) {
195 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
202 * Check for a referral.
204 refp = nfsv4root_getreferral(vp, NULL, 0);
206 (void) nfsrv_putreferralattr(nd, &attrbits, refp, 1,
211 if (nd->nd_repstat == 0) {
213 NFSSET_ATTRBIT(&tmpbits, &attrbits);
214 if (NFSISSET_ATTRBIT(&tmpbits, NFSATTRBIT_ACL)) {
215 NFSCLRBIT_ATTRBIT(&tmpbits, NFSATTRBIT_ACL);
216 accmode |= VREAD_ACL;
218 if (NFSNONZERO_ATTRBIT(&tmpbits))
219 accmode |= VREAD_ATTRIBUTES;
221 nd->nd_repstat = nfsvno_accchk(vp, accmode,
222 nd->nd_cred, exp, p, NFSACCCHK_NOOVERRIDE,
223 NFSACCCHK_VPISLOCKED, NULL);
227 nd->nd_repstat = nfsvno_getattr(vp, &nva, nd->nd_cred, p, 1);
228 if (!nd->nd_repstat) {
229 if (nd->nd_flag & ND_NFSV4) {
230 if (NFSISSET_ATTRBIT(&attrbits, NFSATTRBIT_FILEHANDLE))
231 nd->nd_repstat = nfsvno_getfh(vp, &fh, p);
233 nd->nd_repstat = nfsrv_checkgetattr(nd, vp,
234 &nva, &attrbits, nd->nd_cred, p);
235 if (nd->nd_repstat == 0) {
236 supports_nfsv4acls = nfs_supportsnfsv4acls(vp);
238 if (nfsrv_enable_crossmntpt != 0 &&
239 vp->v_type == VDIR &&
240 (vp->v_vflag & VV_ROOT) != 0 &&
242 tvp = mp->mnt_vnodecovered;
250 if ((nd->nd_repstat =
251 NFSVOPLOCK(tvp, LK_SHARED)) == 0) {
252 nd->nd_repstat = VOP_GETATTR(
253 tvp, &va, nd->nd_cred);
257 if (nd->nd_repstat == 0)
258 mounted_on_fileno = (uint64_t)
263 if (nd->nd_repstat == 0)
264 nd->nd_repstat = vfs_busy(mp, 0);
266 if (nd->nd_repstat == 0) {
267 (void)nfsvno_fillattr(nd, mp, vp, &nva,
268 &fh, 0, &attrbits, nd->nd_cred, p,
269 isdgram, 1, supports_nfsv4acls,
270 at_root, mounted_on_fileno);
277 nfsrv_fillattr(nd, &nva);
285 NFSEXITCODE2(error, nd);
290 * nfs setattr service
293 nfsrvd_setattr(struct nfsrv_descript *nd, __unused int isdgram,
294 vnode_t vp, NFSPROC_T *p, struct nfsexstuff *exp)
296 struct nfsvattr nva, nva2;
298 int preat_ret = 1, postat_ret = 1, gcheck = 0, error = 0;
299 struct timespec guard = { 0, 0 };
300 nfsattrbit_t attrbits, retbits;
301 nfsv4stateid_t stateid;
302 NFSACL_T *aclp = NULL;
304 if (nd->nd_repstat) {
305 nfsrv_wcc(nd, preat_ret, &nva2, postat_ret, &nva);
308 #ifdef NFS4_ACL_EXTATTR_NAME
309 aclp = acl_alloc(M_WAITOK);
312 NFSVNO_ATTRINIT(&nva);
313 NFSZERO_ATTRBIT(&retbits);
314 if (nd->nd_flag & ND_NFSV4) {
315 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID);
316 stateid.seqid = fxdr_unsigned(u_int32_t, *tl++);
317 NFSBCOPY((caddr_t)tl,(caddr_t)stateid.other,NFSX_STATEIDOTHER);
319 error = nfsrv_sattr(nd, &nva, &attrbits, aclp, p);
322 preat_ret = nfsvno_getattr(vp, &nva2, nd->nd_cred, p, 1);
324 nd->nd_repstat = preat_ret;
325 if (nd->nd_flag & ND_NFSV3) {
326 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
327 gcheck = fxdr_unsigned(int, *tl);
329 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
330 fxdr_nfsv3time(tl, &guard);
332 if (!nd->nd_repstat && gcheck &&
333 (nva2.na_ctime.tv_sec != guard.tv_sec ||
334 nva2.na_ctime.tv_nsec != guard.tv_nsec))
335 nd->nd_repstat = NFSERR_NOT_SYNC;
336 if (nd->nd_repstat) {
338 #ifdef NFS4_ACL_EXTATTR_NAME
341 nfsrv_wcc(nd, preat_ret, &nva2, postat_ret, &nva);
344 } else if (!nd->nd_repstat && (nd->nd_flag & ND_NFSV4))
345 nd->nd_repstat = nfsrv_checkuidgid(nd, &nva);
348 * Now that we have all the fields, lets do it.
349 * If the size is being changed write access is required, otherwise
350 * just check for a read only file system.
352 if (!nd->nd_repstat) {
353 if (NFSVNO_NOTSETSIZE(&nva)) {
354 if (NFSVNO_EXRDONLY(exp) ||
355 (vfs_flags(vnode_mount(vp)) & MNT_RDONLY))
356 nd->nd_repstat = EROFS;
358 if (vnode_vtype(vp) != VREG)
359 nd->nd_repstat = EINVAL;
360 else if (nva2.na_uid != nd->nd_cred->cr_uid ||
361 NFSVNO_EXSTRICTACCESS(exp))
362 nd->nd_repstat = nfsvno_accchk(vp,
363 VWRITE, nd->nd_cred, exp, p,
364 NFSACCCHK_NOOVERRIDE,
365 NFSACCCHK_VPISLOCKED, NULL);
368 if (!nd->nd_repstat && (nd->nd_flag & ND_NFSV4))
369 nd->nd_repstat = nfsrv_checksetattr(vp, nd, &stateid,
370 &nva, &attrbits, exp, p);
372 if (!nd->nd_repstat && (nd->nd_flag & ND_NFSV4)) {
374 * For V4, try setting the attrbutes in sets, so that the
375 * reply bitmap will be correct for an error case.
377 if (NFSISSET_ATTRBIT(&attrbits, NFSATTRBIT_OWNER) ||
378 NFSISSET_ATTRBIT(&attrbits, NFSATTRBIT_OWNERGROUP)) {
379 NFSVNO_ATTRINIT(&nva2);
380 NFSVNO_SETATTRVAL(&nva2, uid, nva.na_uid);
381 NFSVNO_SETATTRVAL(&nva2, gid, nva.na_gid);
382 nd->nd_repstat = nfsvno_setattr(vp, &nva2, nd->nd_cred, p,
384 if (!nd->nd_repstat) {
385 if (NFSISSET_ATTRBIT(&attrbits, NFSATTRBIT_OWNER))
386 NFSSETBIT_ATTRBIT(&retbits, NFSATTRBIT_OWNER);
387 if (NFSISSET_ATTRBIT(&attrbits, NFSATTRBIT_OWNERGROUP))
388 NFSSETBIT_ATTRBIT(&retbits, NFSATTRBIT_OWNERGROUP);
391 if (!nd->nd_repstat &&
392 NFSISSET_ATTRBIT(&attrbits, NFSATTRBIT_SIZE)) {
393 NFSVNO_ATTRINIT(&nva2);
394 NFSVNO_SETATTRVAL(&nva2, size, nva.na_size);
395 nd->nd_repstat = nfsvno_setattr(vp, &nva2, nd->nd_cred, p,
398 NFSSETBIT_ATTRBIT(&retbits, NFSATTRBIT_SIZE);
400 if (!nd->nd_repstat &&
401 (NFSISSET_ATTRBIT(&attrbits, NFSATTRBIT_TIMEACCESSSET) ||
402 NFSISSET_ATTRBIT(&attrbits, NFSATTRBIT_TIMEMODIFYSET))) {
403 NFSVNO_ATTRINIT(&nva2);
404 NFSVNO_SETATTRVAL(&nva2, atime, nva.na_atime);
405 NFSVNO_SETATTRVAL(&nva2, mtime, nva.na_mtime);
406 if (nva.na_vaflags & VA_UTIMES_NULL) {
407 nva2.na_vaflags |= VA_UTIMES_NULL;
408 NFSVNO_SETACTIVE(&nva2, vaflags);
410 nd->nd_repstat = nfsvno_setattr(vp, &nva2, nd->nd_cred, p,
412 if (!nd->nd_repstat) {
413 if (NFSISSET_ATTRBIT(&attrbits, NFSATTRBIT_TIMEACCESSSET))
414 NFSSETBIT_ATTRBIT(&retbits, NFSATTRBIT_TIMEACCESSSET);
415 if (NFSISSET_ATTRBIT(&attrbits, NFSATTRBIT_TIMEMODIFYSET))
416 NFSSETBIT_ATTRBIT(&retbits, NFSATTRBIT_TIMEMODIFYSET);
419 if (!nd->nd_repstat &&
420 NFSISSET_ATTRBIT(&attrbits, NFSATTRBIT_MODE)) {
421 NFSVNO_ATTRINIT(&nva2);
422 NFSVNO_SETATTRVAL(&nva2, mode, nva.na_mode);
423 nd->nd_repstat = nfsvno_setattr(vp, &nva2, nd->nd_cred, p,
426 NFSSETBIT_ATTRBIT(&retbits, NFSATTRBIT_MODE);
429 #ifdef NFS4_ACL_EXTATTR_NAME
430 if (!nd->nd_repstat && aclp->acl_cnt > 0 &&
431 NFSISSET_ATTRBIT(&attrbits, NFSATTRBIT_ACL)) {
432 nd->nd_repstat = nfsrv_setacl(vp, aclp, nd->nd_cred, p);
434 NFSSETBIT_ATTRBIT(&retbits, NFSATTRBIT_ACL);
437 } else if (!nd->nd_repstat) {
438 nd->nd_repstat = nfsvno_setattr(vp, &nva, nd->nd_cred, p,
441 if (nd->nd_flag & (ND_NFSV2 | ND_NFSV3)) {
442 postat_ret = nfsvno_getattr(vp, &nva, nd->nd_cred, p, 1);
444 nd->nd_repstat = postat_ret;
447 #ifdef NFS4_ACL_EXTATTR_NAME
450 if (nd->nd_flag & ND_NFSV3)
451 nfsrv_wcc(nd, preat_ret, &nva2, postat_ret, &nva);
452 else if (nd->nd_flag & ND_NFSV4)
453 (void) nfsrv_putattrbit(nd, &retbits);
454 else if (!nd->nd_repstat)
455 nfsrv_fillattr(nd, &nva);
462 #ifdef NFS4_ACL_EXTATTR_NAME
465 if (nd->nd_flag & ND_NFSV4) {
467 * For all nd_repstat, the V4 reply includes a bitmap,
468 * even NFSERR_BADXDR, which is what this will end up
471 (void) nfsrv_putattrbit(nd, &retbits);
473 NFSEXITCODE2(error, nd);
479 * (Also performs lookup parent for v4)
482 nfsrvd_lookup(struct nfsrv_descript *nd, __unused int isdgram,
483 vnode_t dp, vnode_t *vpp, fhandle_t *fhp, NFSPROC_T *p,
484 struct nfsexstuff *exp)
486 struct nameidata named;
487 vnode_t vp, dirp = NULL;
488 int error = 0, dattr_ret = 1;
489 struct nfsvattr nva, dattr;
493 if (nd->nd_repstat) {
494 nfsrv_postopattr(nd, dattr_ret, &dattr);
499 * For some reason, if dp is a symlink, the error
500 * returned is supposed to be NFSERR_SYMLINK and not NFSERR_NOTDIR.
502 if (dp->v_type == VLNK && (nd->nd_flag & ND_NFSV4)) {
503 nd->nd_repstat = NFSERR_SYMLINK;
508 NFSNAMEICNDSET(&named.ni_cnd, nd->nd_cred, LOOKUP,
509 LOCKLEAF | SAVESTART);
510 nfsvno_setpathbuf(&named, &bufp, &hashp);
511 error = nfsrv_parsename(nd, bufp, hashp, &named.ni_pathlen);
514 nfsvno_relpathbuf(&named);
517 if (!nd->nd_repstat) {
518 nd->nd_repstat = nfsvno_namei(nd, &named, dp, 0, exp, p, &dirp);
521 nfsvno_relpathbuf(&named);
523 if (nd->nd_repstat) {
525 if (nd->nd_flag & ND_NFSV3)
526 dattr_ret = nfsvno_getattr(dirp, &dattr,
530 if (nd->nd_flag & ND_NFSV3)
531 nfsrv_postopattr(nd, dattr_ret, &dattr);
534 if (named.ni_startdir)
535 vrele(named.ni_startdir);
536 nfsvno_relpathbuf(&named);
538 if ((nd->nd_flag & ND_NFSV4) != 0 && !NFSVNO_EXPORTED(exp) &&
539 vp->v_type != VDIR && vp->v_type != VLNK)
541 * Only allow lookup of VDIR and VLNK for traversal of
542 * non-exported volumes during NFSv4 mounting.
544 nd->nd_repstat = ENOENT;
545 if (nd->nd_repstat == 0)
546 nd->nd_repstat = nfsvno_getfh(vp, fhp, p);
547 if (!(nd->nd_flag & ND_NFSV4) && !nd->nd_repstat)
548 nd->nd_repstat = nfsvno_getattr(vp, &nva, nd->nd_cred, p, 1);
549 if (vpp != NULL && nd->nd_repstat == 0)
554 if (nd->nd_flag & ND_NFSV3)
555 dattr_ret = nfsvno_getattr(dirp, &dattr, nd->nd_cred,
559 if (nd->nd_repstat) {
560 if (nd->nd_flag & ND_NFSV3)
561 nfsrv_postopattr(nd, dattr_ret, &dattr);
564 if (nd->nd_flag & ND_NFSV2) {
565 (void) nfsm_fhtom(nd, (u_int8_t *)fhp, 0, 0);
566 nfsrv_fillattr(nd, &nva);
567 } else if (nd->nd_flag & ND_NFSV3) {
568 (void) nfsm_fhtom(nd, (u_int8_t *)fhp, 0, 0);
569 nfsrv_postopattr(nd, 0, &nva);
570 nfsrv_postopattr(nd, dattr_ret, &dattr);
574 NFSEXITCODE2(error, nd);
579 * nfs readlink service
582 nfsrvd_readlink(struct nfsrv_descript *nd, __unused int isdgram,
583 vnode_t vp, NFSPROC_T *p, __unused struct nfsexstuff *exp)
586 mbuf_t mp = NULL, mpend = NULL;
590 if (nd->nd_repstat) {
591 nfsrv_postopattr(nd, getret, &nva);
594 if (vnode_vtype(vp) != VLNK) {
595 if (nd->nd_flag & ND_NFSV2)
596 nd->nd_repstat = ENXIO;
598 nd->nd_repstat = EINVAL;
601 nd->nd_repstat = nfsvno_readlink(vp, nd->nd_cred, p,
603 if (nd->nd_flag & ND_NFSV3)
604 getret = nfsvno_getattr(vp, &nva, nd->nd_cred, p, 1);
606 if (nd->nd_flag & ND_NFSV3)
607 nfsrv_postopattr(nd, getret, &nva);
610 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
611 *tl = txdr_unsigned(len);
612 mbuf_setnext(nd->nd_mb, mp);
614 nd->nd_bpos = NFSMTOD(mpend, caddr_t) + mbuf_len(mpend);
625 nfsrvd_read(struct nfsrv_descript *nd, __unused int isdgram,
626 vnode_t vp, NFSPROC_T *p, struct nfsexstuff *exp)
629 int error = 0, cnt, getret = 1, reqlen, eof = 0;
633 struct nfsstate st, *stp = &st;
634 struct nfslock lo, *lop = &lo;
635 nfsv4stateid_t stateid;
638 if (nd->nd_repstat) {
639 nfsrv_postopattr(nd, getret, &nva);
642 if (nd->nd_flag & ND_NFSV2) {
643 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
644 off = (off_t)fxdr_unsigned(u_int32_t, *tl++);
645 reqlen = fxdr_unsigned(int, *tl);
646 } else if (nd->nd_flag & ND_NFSV3) {
647 NFSM_DISSECT(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
648 off = fxdr_hyper(tl);
650 reqlen = fxdr_unsigned(int, *tl);
652 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID + 3*NFSX_UNSIGNED);
653 reqlen = fxdr_unsigned(int, *(tl + 6));
655 if (reqlen > NFS_SRVMAXDATA(nd)) {
656 reqlen = NFS_SRVMAXDATA(nd);
657 } else if (reqlen < 0) {
661 if (nd->nd_flag & ND_NFSV4) {
662 stp->ls_flags = (NFSLCK_CHECK | NFSLCK_READACCESS);
663 lop->lo_flags = NFSLCK_READ;
664 stp->ls_ownerlen = 0;
666 stp->ls_uid = nd->nd_cred->cr_uid;
667 stp->ls_stateid.seqid = fxdr_unsigned(u_int32_t, *tl++);
668 clientid.lval[0] = stp->ls_stateid.other[0] = *tl++;
669 clientid.lval[1] = stp->ls_stateid.other[1] = *tl++;
670 if ((nd->nd_flag & ND_IMPLIEDCLID) != 0) {
671 if ((nd->nd_flag & ND_NFSV41) != 0)
672 clientid.qval = nd->nd_clientid.qval;
673 else if (nd->nd_clientid.qval != clientid.qval)
674 printf("EEK1 multiple clids\n");
676 if ((nd->nd_flag & ND_NFSV41) != 0)
677 printf("EEK! no clientid from session\n");
678 nd->nd_flag |= ND_IMPLIEDCLID;
679 nd->nd_clientid.qval = clientid.qval;
681 stp->ls_stateid.other[2] = *tl++;
682 off = fxdr_hyper(tl);
685 lop->lo_end = off + reqlen;
687 * Paranoia, just in case it wraps around.
689 if (lop->lo_end < off)
690 lop->lo_end = NFS64BITSSET;
692 if (vnode_vtype(vp) != VREG) {
693 if (nd->nd_flag & ND_NFSV3)
694 nd->nd_repstat = EINVAL;
696 nd->nd_repstat = (vnode_vtype(vp) == VDIR) ? EISDIR :
699 getret = nfsvno_getattr(vp, &nva, nd->nd_cred, p, 1);
701 nd->nd_repstat = getret;
702 if (!nd->nd_repstat &&
703 (nva.na_uid != nd->nd_cred->cr_uid ||
704 NFSVNO_EXSTRICTACCESS(exp))) {
705 nd->nd_repstat = nfsvno_accchk(vp, VREAD,
707 NFSACCCHK_ALLOWOWNER, NFSACCCHK_VPISLOCKED, NULL);
709 nd->nd_repstat = nfsvno_accchk(vp, VEXEC,
710 nd->nd_cred, exp, p, NFSACCCHK_ALLOWOWNER,
711 NFSACCCHK_VPISLOCKED, NULL);
713 if ((nd->nd_flag & ND_NFSV4) && !nd->nd_repstat)
714 nd->nd_repstat = nfsrv_lockctrl(vp, &stp, &lop, NULL, clientid,
715 &stateid, exp, nd, p);
716 if (nd->nd_repstat) {
718 if (nd->nd_flag & ND_NFSV3)
719 nfsrv_postopattr(nd, getret, &nva);
722 if (off >= nva.na_size) {
725 } else if (reqlen == 0)
727 else if ((off + reqlen) >= nva.na_size) {
728 cnt = nva.na_size - off;
734 nd->nd_repstat = nfsvno_read(vp, off, cnt, nd->nd_cred, p,
736 if (!(nd->nd_flag & ND_NFSV4)) {
737 getret = nfsvno_getattr(vp, &nva, nd->nd_cred, p, 1);
739 nd->nd_repstat = getret;
741 if (nd->nd_repstat) {
745 if (nd->nd_flag & ND_NFSV3)
746 nfsrv_postopattr(nd, getret, &nva);
751 if (nd->nd_flag & ND_NFSV2) {
752 nfsrv_fillattr(nd, &nva);
753 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
755 if (nd->nd_flag & ND_NFSV3) {
756 nfsrv_postopattr(nd, getret, &nva);
757 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
758 *tl++ = txdr_unsigned(cnt);
760 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
764 *tl++ = newnfs_false;
766 *tl = txdr_unsigned(cnt);
768 mbuf_setnext(nd->nd_mb, m3);
770 nd->nd_bpos = NFSMTOD(m2, caddr_t) + mbuf_len(m2);
778 NFSEXITCODE2(error, nd);
786 nfsrvd_write(struct nfsrv_descript *nd, __unused int isdgram,
787 vnode_t vp, NFSPROC_T *p, struct nfsexstuff *exp)
792 struct nfsvattr nva, forat;
793 int aftat_ret = 1, retlen, len, error = 0, forat_ret = 1;
794 int stable = NFSWRITE_FILESYNC;
796 struct nfsstate st, *stp = &st;
797 struct nfslock lo, *lop = &lo;
798 nfsv4stateid_t stateid;
801 if (nd->nd_repstat) {
802 nfsrv_wcc(nd, forat_ret, &forat, aftat_ret, &nva);
805 if (nd->nd_flag & ND_NFSV2) {
806 NFSM_DISSECT(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
807 off = (off_t)fxdr_unsigned(u_int32_t, *++tl);
809 retlen = len = fxdr_unsigned(int32_t, *tl);
810 } else if (nd->nd_flag & ND_NFSV3) {
811 NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
812 off = fxdr_hyper(tl);
814 stable = fxdr_unsigned(int, *tl++);
815 retlen = len = fxdr_unsigned(int32_t, *tl);
817 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID + 4 * NFSX_UNSIGNED);
818 stp->ls_flags = (NFSLCK_CHECK | NFSLCK_WRITEACCESS);
819 lop->lo_flags = NFSLCK_WRITE;
820 stp->ls_ownerlen = 0;
822 stp->ls_uid = nd->nd_cred->cr_uid;
823 stp->ls_stateid.seqid = fxdr_unsigned(u_int32_t, *tl++);
824 clientid.lval[0] = stp->ls_stateid.other[0] = *tl++;
825 clientid.lval[1] = stp->ls_stateid.other[1] = *tl++;
826 if ((nd->nd_flag & ND_IMPLIEDCLID) != 0) {
827 if ((nd->nd_flag & ND_NFSV41) != 0)
828 clientid.qval = nd->nd_clientid.qval;
829 else if (nd->nd_clientid.qval != clientid.qval)
830 printf("EEK2 multiple clids\n");
832 if ((nd->nd_flag & ND_NFSV41) != 0)
833 printf("EEK! no clientid from session\n");
834 nd->nd_flag |= ND_IMPLIEDCLID;
835 nd->nd_clientid.qval = clientid.qval;
837 stp->ls_stateid.other[2] = *tl++;
838 off = fxdr_hyper(tl);
841 stable = fxdr_unsigned(int, *tl++);
842 retlen = len = fxdr_unsigned(int32_t, *tl);
843 lop->lo_end = off + len;
845 * Paranoia, just in case it wraps around, which shouldn't
846 * ever happen anyhow.
848 if (lop->lo_end < lop->lo_first)
849 lop->lo_end = NFS64BITSSET;
853 * Loop through the mbuf chain, counting how many mbufs are a
854 * part of this write operation, so the iovec size is known.
858 i = NFSMTOD(mp, caddr_t) + mbuf_len(mp) - nd->nd_dpos;
874 if (retlen > NFS_SRVMAXIO || retlen < 0)
875 nd->nd_repstat = EIO;
876 if (vnode_vtype(vp) != VREG && !nd->nd_repstat) {
877 if (nd->nd_flag & ND_NFSV3)
878 nd->nd_repstat = EINVAL;
880 nd->nd_repstat = (vnode_vtype(vp) == VDIR) ? EISDIR :
883 forat_ret = nfsvno_getattr(vp, &forat, nd->nd_cred, p, 1);
885 nd->nd_repstat = forat_ret;
886 if (!nd->nd_repstat &&
887 (forat.na_uid != nd->nd_cred->cr_uid ||
888 NFSVNO_EXSTRICTACCESS(exp)))
889 nd->nd_repstat = nfsvno_accchk(vp, VWRITE,
891 NFSACCCHK_ALLOWOWNER, NFSACCCHK_VPISLOCKED, NULL);
892 if ((nd->nd_flag & ND_NFSV4) && !nd->nd_repstat) {
893 nd->nd_repstat = nfsrv_lockctrl(vp, &stp, &lop, NULL, clientid,
894 &stateid, exp, nd, p);
896 if (nd->nd_repstat) {
898 if (nd->nd_flag & ND_NFSV3)
899 nfsrv_wcc(nd, forat_ret, &forat, aftat_ret, &nva);
904 * For NFS Version 2, it is not obvious what a write of zero length
905 * should do, but I might as well be consistent with Version 3,
906 * which is to return ok so long as there are no permission problems.
909 nd->nd_repstat = nfsvno_write(vp, off, retlen, cnt, stable,
910 nd->nd_md, nd->nd_dpos, nd->nd_cred, p);
911 error = nfsm_advance(nd, NFSM_RNDUP(retlen), -1);
913 panic("nfsrv_write mbuf");
915 if (nd->nd_flag & ND_NFSV4)
918 aftat_ret = nfsvno_getattr(vp, &nva, nd->nd_cred, p, 1);
921 nd->nd_repstat = aftat_ret;
922 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) {
923 if (nd->nd_flag & ND_NFSV3)
924 nfsrv_wcc(nd, forat_ret, &forat, aftat_ret, &nva);
927 NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
928 *tl++ = txdr_unsigned(retlen);
930 * If nfs_async is set, then pretend the write was FILESYNC.
931 * Warning: Doing this violates RFC1813 and runs a risk
932 * of data written by a client being lost when the server
935 if (stable == NFSWRITE_UNSTABLE && nfs_async == 0)
936 *tl++ = txdr_unsigned(stable);
938 *tl++ = txdr_unsigned(NFSWRITE_FILESYNC);
940 * Actually, there is no need to txdr these fields,
941 * but it may make the values more human readable,
942 * for debugging purposes.
944 *tl++ = txdr_unsigned(nfsboottime.tv_sec);
945 *tl = txdr_unsigned(nfsboottime.tv_usec);
946 } else if (!nd->nd_repstat)
947 nfsrv_fillattr(nd, &nva);
954 NFSEXITCODE2(error, nd);
959 * nfs create service (creates regular files for V2 and V3. Spec. files for V2.)
960 * now does a truncate to 0 length via. setattr if it already exists
961 * The core creation routine has been extracted out into nfsrv_creatsub(),
962 * so it can also be used by nfsrv_open() for V4.
965 nfsrvd_create(struct nfsrv_descript *nd, __unused int isdgram,
966 vnode_t dp, NFSPROC_T *p, struct nfsexstuff *exp)
968 struct nfsvattr nva, dirfor, diraft;
969 struct nfsv2_sattr *sp;
970 struct nameidata named;
972 int error = 0, tsize, dirfor_ret = 1, diraft_ret = 1;
973 int how = NFSCREATE_UNCHECKED, exclusive_flag = 0;
975 vnode_t vp = NULL, dirp = NULL;
980 int32_t cverf[2], tverf[2] = { 0, 0 };
982 if (nd->nd_repstat) {
983 nfsrv_wcc(nd, dirfor_ret, &dirfor, diraft_ret, &diraft);
986 NFSNAMEICNDSET(&named.ni_cnd, nd->nd_cred, CREATE,
987 LOCKPARENT | LOCKLEAF | SAVESTART | NOCACHE);
988 nfsvno_setpathbuf(&named, &bufp, &hashp);
989 error = nfsrv_parsename(nd, bufp, hashp, &named.ni_pathlen);
992 if (!nd->nd_repstat) {
993 NFSVNO_ATTRINIT(&nva);
994 if (nd->nd_flag & ND_NFSV2) {
995 NFSM_DISSECT(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
996 vtyp = IFTOVT(fxdr_unsigned(u_int32_t, sp->sa_mode));
999 NFSVNO_SETATTRVAL(&nva, type, vtyp);
1000 NFSVNO_SETATTRVAL(&nva, mode,
1001 nfstov_mode(sp->sa_mode));
1002 switch (nva.na_type) {
1004 tsize = fxdr_unsigned(int32_t, sp->sa_size);
1006 NFSVNO_SETATTRVAL(&nva, size,
1012 rdev = fxdr_unsigned(NFSDEV_T, sp->sa_size);
1018 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
1019 how = fxdr_unsigned(int, *tl);
1021 case NFSCREATE_GUARDED:
1022 case NFSCREATE_UNCHECKED:
1023 error = nfsrv_sattr(nd, &nva, NULL, NULL, p);
1027 case NFSCREATE_EXCLUSIVE:
1028 NFSM_DISSECT(tl, u_int32_t *, NFSX_VERF);
1034 NFSVNO_SETATTRVAL(&nva, type, VREG);
1037 if (nd->nd_repstat) {
1038 nfsvno_relpathbuf(&named);
1039 if (nd->nd_flag & ND_NFSV3) {
1040 dirfor_ret = nfsvno_getattr(dp, &dirfor, nd->nd_cred,
1042 nfsrv_wcc(nd, dirfor_ret, &dirfor, diraft_ret,
1049 nd->nd_repstat = nfsvno_namei(nd, &named, dp, 1, exp, p, &dirp);
1051 if (nd->nd_flag & ND_NFSV2) {
1055 dirfor_ret = nfsvno_getattr(dirp, &dirfor, nd->nd_cred,
1059 if (nd->nd_repstat) {
1060 if (nd->nd_flag & ND_NFSV3)
1061 nfsrv_wcc(nd, dirfor_ret, &dirfor, diraft_ret,
1068 if (!(nd->nd_flag & ND_NFSV2)) {
1070 case NFSCREATE_GUARDED:
1072 nd->nd_repstat = EEXIST;
1074 case NFSCREATE_UNCHECKED:
1076 case NFSCREATE_EXCLUSIVE:
1077 if (named.ni_vp == NULL)
1078 NFSVNO_SETATTRVAL(&nva, mode, 0);
1084 * Iff doesn't exist, create it
1085 * otherwise just truncate to 0 length
1086 * should I set the mode too ?
1088 nd->nd_repstat = nfsvno_createsub(nd, &named, &vp, &nva,
1089 &exclusive_flag, cverf, rdev, p, exp);
1091 if (!nd->nd_repstat) {
1092 nd->nd_repstat = nfsvno_getfh(vp, &fh, p);
1093 if (!nd->nd_repstat)
1094 nd->nd_repstat = nfsvno_getattr(vp, &nva, nd->nd_cred,
1097 if (!nd->nd_repstat) {
1098 tverf[0] = nva.na_atime.tv_sec;
1099 tverf[1] = nva.na_atime.tv_nsec;
1102 if (nd->nd_flag & ND_NFSV2) {
1103 if (!nd->nd_repstat) {
1104 (void) nfsm_fhtom(nd, (u_int8_t *)&fh, 0, 0);
1105 nfsrv_fillattr(nd, &nva);
1108 if (exclusive_flag && !nd->nd_repstat && (cverf[0] != tverf[0]
1109 || cverf[1] != tverf[1]))
1110 nd->nd_repstat = EEXIST;
1111 diraft_ret = nfsvno_getattr(dirp, &diraft, nd->nd_cred, p, 0);
1113 if (!nd->nd_repstat) {
1114 (void) nfsm_fhtom(nd, (u_int8_t *)&fh, 0, 1);
1115 nfsrv_postopattr(nd, 0, &nva);
1117 nfsrv_wcc(nd, dirfor_ret, &dirfor, diraft_ret, &diraft);
1121 NFSEXITCODE2(0, nd);
1125 nfsvno_relpathbuf(&named);
1126 NFSEXITCODE2(error, nd);
1131 * nfs v3 mknod service (and v4 create)
1134 nfsrvd_mknod(struct nfsrv_descript *nd, __unused int isdgram,
1135 vnode_t dp, vnode_t *vpp, fhandle_t *fhp, NFSPROC_T *p,
1136 struct nfsexstuff *exp)
1138 struct nfsvattr nva, dirfor, diraft;
1140 struct nameidata named;
1141 int error = 0, dirfor_ret = 1, diraft_ret = 1, pathlen;
1142 u_int32_t major, minor;
1143 enum vtype vtyp = VNON;
1144 nfstype nfs4type = NFNON;
1145 vnode_t vp, dirp = NULL;
1146 nfsattrbit_t attrbits;
1147 char *bufp = NULL, *pathcp = NULL;
1148 u_long *hashp, cnflags;
1149 NFSACL_T *aclp = NULL;
1151 NFSVNO_ATTRINIT(&nva);
1152 cnflags = (LOCKPARENT | SAVESTART);
1153 if (nd->nd_repstat) {
1154 nfsrv_wcc(nd, dirfor_ret, &dirfor, diraft_ret, &diraft);
1157 #ifdef NFS4_ACL_EXTATTR_NAME
1158 aclp = acl_alloc(M_WAITOK);
1163 * For V4, the creation stuff is here, Yuck!
1165 if (nd->nd_flag & ND_NFSV4) {
1166 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
1167 vtyp = nfsv34tov_type(*tl);
1168 nfs4type = fxdr_unsigned(nfstype, *tl);
1171 error = nfsvno_getsymlink(nd, &nva, p, &pathcp,
1178 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1179 major = fxdr_unsigned(u_int32_t, *tl++);
1180 minor = fxdr_unsigned(u_int32_t, *tl);
1181 nva.na_rdev = NFSMAKEDEV(major, minor);
1187 cnflags = (LOCKPARENT | SAVENAME);
1190 nd->nd_repstat = NFSERR_BADTYPE;
1192 #ifdef NFS4_ACL_EXTATTR_NAME
1198 NFSNAMEICNDSET(&named.ni_cnd, nd->nd_cred, CREATE, cnflags | NOCACHE);
1199 nfsvno_setpathbuf(&named, &bufp, &hashp);
1200 error = nfsrv_parsename(nd, bufp, hashp, &named.ni_pathlen);
1203 if (!nd->nd_repstat) {
1204 if (nd->nd_flag & ND_NFSV3) {
1205 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
1206 vtyp = nfsv34tov_type(*tl);
1208 error = nfsrv_sattr(nd, &nva, &attrbits, aclp, p);
1212 if (!nd->nd_repstat && (nd->nd_flag & ND_NFSV3) &&
1213 (vtyp == VCHR || vtyp == VBLK)) {
1214 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1215 major = fxdr_unsigned(u_int32_t, *tl++);
1216 minor = fxdr_unsigned(u_int32_t, *tl);
1217 nva.na_rdev = NFSMAKEDEV(major, minor);
1221 dirfor_ret = nfsvno_getattr(dp, &dirfor, nd->nd_cred, p, 0);
1222 if (!nd->nd_repstat && (nd->nd_flag & ND_NFSV4)) {
1223 if (!dirfor_ret && NFSVNO_ISSETGID(&nva) &&
1224 dirfor.na_gid == nva.na_gid)
1225 NFSVNO_UNSET(&nva, gid);
1226 nd->nd_repstat = nfsrv_checkuidgid(nd, &nva);
1228 if (nd->nd_repstat) {
1230 #ifdef NFS4_ACL_EXTATTR_NAME
1233 nfsvno_relpathbuf(&named);
1235 FREE(pathcp, M_TEMP);
1236 if (nd->nd_flag & ND_NFSV3)
1237 nfsrv_wcc(nd, dirfor_ret, &dirfor, diraft_ret,
1243 * Yuck! For V4, mkdir and link are here and some V4 clients don't fill
1244 * in va_mode, so we'll have to set a default here.
1246 if (NFSVNO_NOTSETMODE(&nva)) {
1254 named.ni_cnd.cn_flags |= WILLBEDIR;
1255 nd->nd_repstat = nfsvno_namei(nd, &named, dp, 0, exp, p, &dirp);
1256 if (nd->nd_repstat) {
1258 if (nd->nd_flag & ND_NFSV3)
1259 dirfor_ret = nfsvno_getattr(dirp, &dirfor,
1263 #ifdef NFS4_ACL_EXTATTR_NAME
1266 if (nd->nd_flag & ND_NFSV3)
1267 nfsrv_wcc(nd, dirfor_ret, &dirfor, diraft_ret,
1272 dirfor_ret = nfsvno_getattr(dirp, &dirfor, nd->nd_cred, p, 0);
1274 if ((nd->nd_flag & ND_NFSV4) && (vtyp == VDIR || vtyp == VLNK)) {
1276 nfsrvd_mkdirsub(nd, &named, &nva, fhp, vpp, dirp,
1277 &dirfor, &diraft, &diraft_ret, &attrbits, aclp, p,
1279 #ifdef NFS4_ACL_EXTATTR_NAME
1283 } else if (vtyp == VLNK) {
1284 nfsrvd_symlinksub(nd, &named, &nva, fhp, vpp, dirp,
1285 &dirfor, &diraft, &diraft_ret, &attrbits,
1286 aclp, p, exp, pathcp, pathlen);
1287 #ifdef NFS4_ACL_EXTATTR_NAME
1290 FREE(pathcp, M_TEMP);
1295 nd->nd_repstat = nfsvno_mknod(&named, &nva, nd->nd_cred, p);
1296 if (!nd->nd_repstat) {
1298 nfsrv_fixattr(nd, vp, &nva, aclp, p, &attrbits, exp);
1299 nd->nd_repstat = nfsvno_getfh(vp, fhp, p);
1300 if ((nd->nd_flag & ND_NFSV3) && !nd->nd_repstat)
1301 nd->nd_repstat = nfsvno_getattr(vp, &nva, nd->nd_cred,
1303 if (vpp != NULL && nd->nd_repstat == 0) {
1304 NFSVOPUNLOCK(vp, 0);
1310 diraft_ret = nfsvno_getattr(dirp, &diraft, nd->nd_cred, p, 0);
1312 if (!nd->nd_repstat) {
1313 if (nd->nd_flag & ND_NFSV3) {
1314 (void) nfsm_fhtom(nd, (u_int8_t *)fhp, 0, 1);
1315 nfsrv_postopattr(nd, 0, &nva);
1317 NFSM_BUILD(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
1318 *tl++ = newnfs_false;
1319 txdr_hyper(dirfor.na_filerev, tl);
1321 txdr_hyper(diraft.na_filerev, tl);
1322 (void) nfsrv_putattrbit(nd, &attrbits);
1325 if (nd->nd_flag & ND_NFSV3)
1326 nfsrv_wcc(nd, dirfor_ret, &dirfor, diraft_ret, &diraft);
1327 #ifdef NFS4_ACL_EXTATTR_NAME
1332 NFSEXITCODE2(0, nd);
1336 #ifdef NFS4_ACL_EXTATTR_NAME
1340 nfsvno_relpathbuf(&named);
1342 FREE(pathcp, M_TEMP);
1344 NFSEXITCODE2(error, nd);
1349 * nfs remove service
1352 nfsrvd_remove(struct nfsrv_descript *nd, __unused int isdgram,
1353 vnode_t dp, NFSPROC_T *p, struct nfsexstuff *exp)
1355 struct nameidata named;
1357 int error = 0, dirfor_ret = 1, diraft_ret = 1;
1358 vnode_t dirp = NULL;
1359 struct nfsvattr dirfor, diraft;
1363 if (nd->nd_repstat) {
1364 nfsrv_wcc(nd, dirfor_ret, &dirfor, diraft_ret, &diraft);
1367 NFSNAMEICNDSET(&named.ni_cnd, nd->nd_cred, DELETE,
1368 LOCKPARENT | LOCKLEAF);
1369 nfsvno_setpathbuf(&named, &bufp, &hashp);
1370 error = nfsrv_parsename(nd, bufp, hashp, &named.ni_pathlen);
1373 nfsvno_relpathbuf(&named);
1376 if (!nd->nd_repstat) {
1377 nd->nd_repstat = nfsvno_namei(nd, &named, dp, 1, exp, p, &dirp);
1380 nfsvno_relpathbuf(&named);
1383 if (!(nd->nd_flag & ND_NFSV2)) {
1384 dirfor_ret = nfsvno_getattr(dirp, &dirfor,
1391 if (!nd->nd_repstat) {
1392 if (nd->nd_flag & ND_NFSV4) {
1393 if (vnode_vtype(named.ni_vp) == VDIR)
1394 nd->nd_repstat = nfsvno_rmdirsub(&named, 1,
1395 nd->nd_cred, p, exp);
1397 nd->nd_repstat = nfsvno_removesub(&named, 1,
1398 nd->nd_cred, p, exp);
1399 } else if (nd->nd_procnum == NFSPROC_RMDIR) {
1400 nd->nd_repstat = nfsvno_rmdirsub(&named, 0,
1401 nd->nd_cred, p, exp);
1403 nd->nd_repstat = nfsvno_removesub(&named, 0,
1404 nd->nd_cred, p, exp);
1407 if (!(nd->nd_flag & ND_NFSV2)) {
1409 diraft_ret = nfsvno_getattr(dirp, &diraft, nd->nd_cred,
1413 if (nd->nd_flag & ND_NFSV3) {
1414 nfsrv_wcc(nd, dirfor_ret, &dirfor, diraft_ret,
1416 } else if (!nd->nd_repstat) {
1417 NFSM_BUILD(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
1418 *tl++ = newnfs_false;
1419 txdr_hyper(dirfor.na_filerev, tl);
1421 txdr_hyper(diraft.na_filerev, tl);
1426 NFSEXITCODE2(error, nd);
1431 * nfs rename service
1434 nfsrvd_rename(struct nfsrv_descript *nd, int isdgram,
1435 vnode_t dp, vnode_t todp, NFSPROC_T *p, struct nfsexstuff *exp,
1436 struct nfsexstuff *toexp)
1439 int error = 0, fdirfor_ret = 1, fdiraft_ret = 1;
1440 int tdirfor_ret = 1, tdiraft_ret = 1;
1441 struct nameidata fromnd, tond;
1442 vnode_t fdirp = NULL, tdirp = NULL, tdp = NULL;
1443 struct nfsvattr fdirfor, fdiraft, tdirfor, tdiraft;
1444 struct nfsexstuff tnes;
1446 char *bufp, *tbufp = NULL;
1450 if (nd->nd_repstat) {
1451 nfsrv_wcc(nd, fdirfor_ret, &fdirfor, fdiraft_ret, &fdiraft);
1452 nfsrv_wcc(nd, tdirfor_ret, &tdirfor, tdiraft_ret, &tdiraft);
1455 if (!(nd->nd_flag & ND_NFSV2))
1456 fdirfor_ret = nfsvno_getattr(dp, &fdirfor, nd->nd_cred, p, 1);
1457 tond.ni_cnd.cn_nameiop = 0;
1458 tond.ni_startdir = NULL;
1459 NFSNAMEICNDSET(&fromnd.ni_cnd, nd->nd_cred, DELETE, WANTPARENT | SAVESTART);
1460 nfsvno_setpathbuf(&fromnd, &bufp, &hashp);
1461 error = nfsrv_parsename(nd, bufp, hashp, &fromnd.ni_pathlen);
1466 nfsvno_relpathbuf(&fromnd);
1470 * Unlock dp in this code section, so it is unlocked before
1471 * tdp gets locked. This avoids a potential LOR if tdp is the
1472 * parent directory of dp.
1474 if (nd->nd_flag & ND_NFSV4) {
1478 NFSVOPUNLOCK(dp, 0);
1479 tdirfor_ret = nfsvno_getattr(tdp, &tdirfor, nd->nd_cred,
1480 p, 0); /* Might lock tdp. */
1482 tdirfor_ret = nfsvno_getattr(tdp, &tdirfor, nd->nd_cred,
1484 NFSVOPUNLOCK(dp, 0);
1487 tfh.nfsrvfh_len = 0;
1488 error = nfsrv_mtofh(nd, &tfh);
1490 error = nfsvno_getfh(dp, &fh, p);
1493 /* todp is always NULL except NFSv4 */
1494 nfsvno_relpathbuf(&fromnd);
1498 /* If this is the same file handle, just VREF() the vnode. */
1499 if (tfh.nfsrvfh_len == NFSX_MYFH &&
1500 !NFSBCMP(tfh.nfsrvfh_data, &fh, NFSX_MYFH)) {
1504 tdirfor_ret = nfsvno_getattr(tdp, &tdirfor, nd->nd_cred,
1506 NFSVOPUNLOCK(dp, 0);
1508 NFSVOPUNLOCK(dp, 0);
1509 nd->nd_cred->cr_uid = nd->nd_saveduid;
1510 nfsd_fhtovp(nd, &tfh, LK_EXCLUSIVE, &tdp, &tnes, NULL,
1511 0, p); /* Locks tdp. */
1513 tdirfor_ret = nfsvno_getattr(tdp, &tdirfor,
1515 NFSVOPUNLOCK(tdp, 0);
1519 NFSNAMEICNDSET(&tond.ni_cnd, nd->nd_cred, RENAME, LOCKPARENT | LOCKLEAF | NOCACHE | SAVESTART);
1520 nfsvno_setpathbuf(&tond, &tbufp, &hashp);
1521 if (!nd->nd_repstat) {
1522 error = nfsrv_parsename(nd, tbufp, hashp, &tond.ni_pathlen);
1527 nfsvno_relpathbuf(&fromnd);
1528 nfsvno_relpathbuf(&tond);
1532 if (nd->nd_repstat) {
1533 if (nd->nd_flag & ND_NFSV3) {
1534 nfsrv_wcc(nd, fdirfor_ret, &fdirfor, fdiraft_ret,
1536 nfsrv_wcc(nd, tdirfor_ret, &tdirfor, tdiraft_ret,
1542 nfsvno_relpathbuf(&fromnd);
1543 nfsvno_relpathbuf(&tond);
1548 * Done parsing, now down to business.
1550 nd->nd_repstat = nfsvno_namei(nd, &fromnd, dp, 0, exp, p, &fdirp);
1551 if (nd->nd_repstat) {
1552 if (nd->nd_flag & ND_NFSV3) {
1553 nfsrv_wcc(nd, fdirfor_ret, &fdirfor, fdiraft_ret,
1555 nfsrv_wcc(nd, tdirfor_ret, &tdirfor, tdiraft_ret,
1562 nfsvno_relpathbuf(&tond);
1565 if (vnode_vtype(fromnd.ni_vp) == VDIR)
1566 tond.ni_cnd.cn_flags |= WILLBEDIR;
1567 nd->nd_repstat = nfsvno_namei(nd, &tond, tdp, 0, &tnes, p, &tdirp);
1568 nd->nd_repstat = nfsvno_rename(&fromnd, &tond, nd->nd_repstat,
1569 nd->nd_flag, nd->nd_cred, p);
1571 fdiraft_ret = nfsvno_getattr(fdirp, &fdiraft, nd->nd_cred, p,
1574 tdiraft_ret = nfsvno_getattr(tdirp, &tdiraft, nd->nd_cred, p,
1580 if (nd->nd_flag & ND_NFSV3) {
1581 nfsrv_wcc(nd, fdirfor_ret, &fdirfor, fdiraft_ret, &fdiraft);
1582 nfsrv_wcc(nd, tdirfor_ret, &tdirfor, tdiraft_ret, &tdiraft);
1583 } else if ((nd->nd_flag & ND_NFSV4) && !nd->nd_repstat) {
1584 NFSM_BUILD(tl, u_int32_t *, 10 * NFSX_UNSIGNED);
1585 *tl++ = newnfs_false;
1586 txdr_hyper(fdirfor.na_filerev, tl);
1588 txdr_hyper(fdiraft.na_filerev, tl);
1590 *tl++ = newnfs_false;
1591 txdr_hyper(tdirfor.na_filerev, tl);
1593 txdr_hyper(tdiraft.na_filerev, tl);
1597 NFSEXITCODE2(error, nd);
1605 nfsrvd_link(struct nfsrv_descript *nd, int isdgram,
1606 vnode_t vp, vnode_t tovp, NFSPROC_T *p, struct nfsexstuff *exp,
1607 struct nfsexstuff *toexp)
1609 struct nameidata named;
1611 int error = 0, dirfor_ret = 1, diraft_ret = 1, getret = 1;
1612 vnode_t dirp = NULL, dp = NULL;
1613 struct nfsvattr dirfor, diraft, at;
1614 struct nfsexstuff tnes;
1619 if (nd->nd_repstat) {
1620 nfsrv_postopattr(nd, getret, &at);
1621 nfsrv_wcc(nd, dirfor_ret, &dirfor, diraft_ret, &diraft);
1624 NFSVOPUNLOCK(vp, 0);
1625 if (vnode_vtype(vp) == VDIR) {
1626 if (nd->nd_flag & ND_NFSV4)
1627 nd->nd_repstat = NFSERR_ISDIR;
1629 nd->nd_repstat = NFSERR_INVAL;
1633 if (!nd->nd_repstat) {
1634 if (nd->nd_flag & ND_NFSV4) {
1638 error = nfsrv_mtofh(nd, &dfh);
1641 /* tovp is always NULL unless NFSv4 */
1644 nfsd_fhtovp(nd, &dfh, LK_EXCLUSIVE, &dp, &tnes, NULL, 0,
1647 NFSVOPUNLOCK(dp, 0);
1650 NFSNAMEICNDSET(&named.ni_cnd, nd->nd_cred, CREATE,
1651 LOCKPARENT | SAVENAME | NOCACHE);
1652 if (!nd->nd_repstat) {
1653 nfsvno_setpathbuf(&named, &bufp, &hashp);
1654 error = nfsrv_parsename(nd, bufp, hashp, &named.ni_pathlen);
1659 nfsvno_relpathbuf(&named);
1662 if (!nd->nd_repstat) {
1663 nd->nd_repstat = nfsvno_namei(nd, &named, dp, 0, &tnes,
1668 nfsvno_relpathbuf(&named);
1672 if (nd->nd_flag & ND_NFSV2) {
1676 dirfor_ret = nfsvno_getattr(dirp, &dirfor,
1680 if (!nd->nd_repstat)
1681 nd->nd_repstat = nfsvno_link(&named, vp, nd->nd_cred, p, exp);
1682 if (nd->nd_flag & ND_NFSV3)
1683 getret = nfsvno_getattr(vp, &at, nd->nd_cred, p, 0);
1685 diraft_ret = nfsvno_getattr(dirp, &diraft, nd->nd_cred, p, 0);
1689 if (nd->nd_flag & ND_NFSV3) {
1690 nfsrv_postopattr(nd, getret, &at);
1691 nfsrv_wcc(nd, dirfor_ret, &dirfor, diraft_ret, &diraft);
1692 } else if ((nd->nd_flag & ND_NFSV4) && !nd->nd_repstat) {
1693 NFSM_BUILD(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
1694 *tl++ = newnfs_false;
1695 txdr_hyper(dirfor.na_filerev, tl);
1697 txdr_hyper(diraft.na_filerev, tl);
1701 NFSEXITCODE2(error, nd);
1706 * nfs symbolic link service
1709 nfsrvd_symlink(struct nfsrv_descript *nd, __unused int isdgram,
1710 vnode_t dp, vnode_t *vpp, fhandle_t *fhp, NFSPROC_T *p,
1711 struct nfsexstuff *exp)
1713 struct nfsvattr nva, dirfor, diraft;
1714 struct nameidata named;
1715 int error = 0, dirfor_ret = 1, diraft_ret = 1, pathlen;
1716 vnode_t dirp = NULL;
1717 char *bufp, *pathcp = NULL;
1720 if (nd->nd_repstat) {
1721 nfsrv_wcc(nd, dirfor_ret, &dirfor, diraft_ret, &diraft);
1726 NFSVNO_ATTRINIT(&nva);
1727 NFSNAMEICNDSET(&named.ni_cnd, nd->nd_cred, CREATE,
1728 LOCKPARENT | SAVESTART | NOCACHE);
1729 nfsvno_setpathbuf(&named, &bufp, &hashp);
1730 error = nfsrv_parsename(nd, bufp, hashp, &named.ni_pathlen);
1731 if (!error && !nd->nd_repstat)
1732 error = nfsvno_getsymlink(nd, &nva, p, &pathcp, &pathlen);
1735 nfsvno_relpathbuf(&named);
1738 if (!nd->nd_repstat) {
1739 nd->nd_repstat = nfsvno_namei(nd, &named, dp, 0, exp, p, &dirp);
1742 nfsvno_relpathbuf(&named);
1744 if (dirp != NULL && !(nd->nd_flag & ND_NFSV3)) {
1750 * And call nfsrvd_symlinksub() to do the common code. It will
1751 * return EBADRPC upon a parsing error, 0 otherwise.
1753 if (!nd->nd_repstat) {
1755 dirfor_ret = nfsvno_getattr(dirp, &dirfor, nd->nd_cred,
1757 nfsrvd_symlinksub(nd, &named, &nva, fhp, vpp, dirp,
1758 &dirfor, &diraft, &diraft_ret, NULL, NULL, p, exp,
1760 } else if (dirp != NULL) {
1761 dirfor_ret = nfsvno_getattr(dirp, &dirfor, nd->nd_cred, p, 0);
1765 FREE(pathcp, M_TEMP);
1767 if (nd->nd_flag & ND_NFSV3) {
1768 if (!nd->nd_repstat) {
1769 (void) nfsm_fhtom(nd, (u_int8_t *)fhp, 0, 1);
1770 nfsrv_postopattr(nd, 0, &nva);
1772 nfsrv_wcc(nd, dirfor_ret, &dirfor, diraft_ret, &diraft);
1776 NFSEXITCODE2(error, nd);
1781 * Common code for creating a symbolic link.
1784 nfsrvd_symlinksub(struct nfsrv_descript *nd, struct nameidata *ndp,
1785 struct nfsvattr *nvap, fhandle_t *fhp, vnode_t *vpp,
1786 vnode_t dirp, struct nfsvattr *dirforp, struct nfsvattr *diraftp,
1787 int *diraft_retp, nfsattrbit_t *attrbitp,
1788 NFSACL_T *aclp, NFSPROC_T *p, struct nfsexstuff *exp, char *pathcp,
1793 nd->nd_repstat = nfsvno_symlink(ndp, nvap, pathcp, pathlen,
1794 !(nd->nd_flag & ND_NFSV2), nd->nd_saveduid, nd->nd_cred, p, exp);
1795 if (!nd->nd_repstat && !(nd->nd_flag & ND_NFSV2)) {
1796 nfsrv_fixattr(nd, ndp->ni_vp, nvap, aclp, p, attrbitp, exp);
1797 if (nd->nd_flag & ND_NFSV3) {
1798 nd->nd_repstat = nfsvno_getfh(ndp->ni_vp, fhp, p);
1799 if (!nd->nd_repstat)
1800 nd->nd_repstat = nfsvno_getattr(ndp->ni_vp,
1801 nvap, nd->nd_cred, p, 1);
1803 if (vpp != NULL && nd->nd_repstat == 0) {
1804 NFSVOPUNLOCK(ndp->ni_vp, 0);
1810 *diraft_retp = nfsvno_getattr(dirp, diraftp, nd->nd_cred, p, 0);
1813 if ((nd->nd_flag & ND_NFSV4) && !nd->nd_repstat) {
1814 NFSM_BUILD(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
1815 *tl++ = newnfs_false;
1816 txdr_hyper(dirforp->na_filerev, tl);
1818 txdr_hyper(diraftp->na_filerev, tl);
1819 (void) nfsrv_putattrbit(nd, attrbitp);
1822 NFSEXITCODE2(0, nd);
1829 nfsrvd_mkdir(struct nfsrv_descript *nd, __unused int isdgram,
1830 vnode_t dp, vnode_t *vpp, fhandle_t *fhp, NFSPROC_T *p,
1831 struct nfsexstuff *exp)
1833 struct nfsvattr nva, dirfor, diraft;
1834 struct nameidata named;
1836 int error = 0, dirfor_ret = 1, diraft_ret = 1;
1837 vnode_t dirp = NULL;
1841 if (nd->nd_repstat) {
1842 nfsrv_wcc(nd, dirfor_ret, &dirfor, diraft_ret, &diraft);
1845 NFSNAMEICNDSET(&named.ni_cnd, nd->nd_cred, CREATE,
1846 LOCKPARENT | SAVENAME | NOCACHE);
1847 nfsvno_setpathbuf(&named, &bufp, &hashp);
1848 error = nfsrv_parsename(nd, bufp, hashp, &named.ni_pathlen);
1851 if (!nd->nd_repstat) {
1852 NFSVNO_ATTRINIT(&nva);
1853 if (nd->nd_flag & ND_NFSV3) {
1854 error = nfsrv_sattr(nd, &nva, NULL, NULL, p);
1858 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
1859 nva.na_mode = nfstov_mode(*tl++);
1862 if (!nd->nd_repstat) {
1863 nd->nd_repstat = nfsvno_namei(nd, &named, dp, 0, exp, p, &dirp);
1866 nfsvno_relpathbuf(&named);
1868 if (dirp != NULL && !(nd->nd_flag & ND_NFSV3)) {
1872 if (nd->nd_repstat) {
1874 dirfor_ret = nfsvno_getattr(dirp, &dirfor, nd->nd_cred,
1878 if (nd->nd_flag & ND_NFSV3)
1879 nfsrv_wcc(nd, dirfor_ret, &dirfor, diraft_ret,
1884 dirfor_ret = nfsvno_getattr(dirp, &dirfor, nd->nd_cred, p, 0);
1887 * Call nfsrvd_mkdirsub() for the code common to V4 as well.
1889 nfsrvd_mkdirsub(nd, &named, &nva, fhp, vpp, dirp, &dirfor, &diraft,
1890 &diraft_ret, NULL, NULL, p, exp);
1892 if (nd->nd_flag & ND_NFSV3) {
1893 if (!nd->nd_repstat) {
1894 (void) nfsm_fhtom(nd, (u_int8_t *)fhp, 0, 1);
1895 nfsrv_postopattr(nd, 0, &nva);
1897 nfsrv_wcc(nd, dirfor_ret, &dirfor, diraft_ret, &diraft);
1898 } else if (!nd->nd_repstat) {
1899 (void) nfsm_fhtom(nd, (u_int8_t *)fhp, 0, 0);
1900 nfsrv_fillattr(nd, &nva);
1904 NFSEXITCODE2(0, nd);
1908 nfsvno_relpathbuf(&named);
1909 NFSEXITCODE2(error, nd);
1914 * Code common to mkdir for V2,3 and 4.
1917 nfsrvd_mkdirsub(struct nfsrv_descript *nd, struct nameidata *ndp,
1918 struct nfsvattr *nvap, fhandle_t *fhp, vnode_t *vpp,
1919 vnode_t dirp, struct nfsvattr *dirforp, struct nfsvattr *diraftp,
1920 int *diraft_retp, nfsattrbit_t *attrbitp, NFSACL_T *aclp,
1921 NFSPROC_T *p, struct nfsexstuff *exp)
1926 NFSVNO_SETATTRVAL(nvap, type, VDIR);
1927 nd->nd_repstat = nfsvno_mkdir(ndp, nvap, nd->nd_saveduid,
1928 nd->nd_cred, p, exp);
1929 if (!nd->nd_repstat) {
1931 nfsrv_fixattr(nd, vp, nvap, aclp, p, attrbitp, exp);
1932 nd->nd_repstat = nfsvno_getfh(vp, fhp, p);
1933 if (!(nd->nd_flag & ND_NFSV4) && !nd->nd_repstat)
1934 nd->nd_repstat = nfsvno_getattr(vp, nvap, nd->nd_cred,
1936 if (vpp && !nd->nd_repstat) {
1937 NFSVOPUNLOCK(vp, 0);
1944 *diraft_retp = nfsvno_getattr(dirp, diraftp, nd->nd_cred, p, 0);
1947 if ((nd->nd_flag & ND_NFSV4) && !nd->nd_repstat) {
1948 NFSM_BUILD(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
1949 *tl++ = newnfs_false;
1950 txdr_hyper(dirforp->na_filerev, tl);
1952 txdr_hyper(diraftp->na_filerev, tl);
1953 (void) nfsrv_putattrbit(nd, attrbitp);
1956 NFSEXITCODE2(0, nd);
1960 * nfs commit service
1963 nfsrvd_commit(struct nfsrv_descript *nd, __unused int isdgram,
1964 vnode_t vp, NFSPROC_T *p, __unused struct nfsexstuff *exp)
1966 struct nfsvattr bfor, aft;
1968 int error = 0, for_ret = 1, aft_ret = 1, cnt;
1971 if (nd->nd_repstat) {
1972 nfsrv_wcc(nd, for_ret, &bfor, aft_ret, &aft);
1975 NFSM_DISSECT(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
1977 * XXX At this time VOP_FSYNC() does not accept offset and byte
1978 * count parameters, so these arguments are useless (someday maybe).
1980 off = fxdr_hyper(tl);
1982 cnt = fxdr_unsigned(int, *tl);
1983 if (nd->nd_flag & ND_NFSV3)
1984 for_ret = nfsvno_getattr(vp, &bfor, nd->nd_cred, p, 1);
1985 nd->nd_repstat = nfsvno_fsync(vp, off, cnt, nd->nd_cred, p);
1986 if (nd->nd_flag & ND_NFSV3) {
1987 aft_ret = nfsvno_getattr(vp, &aft, nd->nd_cred, p, 1);
1988 nfsrv_wcc(nd, for_ret, &bfor, aft_ret, &aft);
1991 if (!nd->nd_repstat) {
1992 NFSM_BUILD(tl, u_int32_t *, NFSX_VERF);
1993 *tl++ = txdr_unsigned(nfsboottime.tv_sec);
1994 *tl = txdr_unsigned(nfsboottime.tv_usec);
1998 NFSEXITCODE2(0, nd);
2002 NFSEXITCODE2(error, nd);
2007 * nfs statfs service
2010 nfsrvd_statfs(struct nfsrv_descript *nd, __unused int isdgram,
2011 vnode_t vp, NFSPROC_T *p, __unused struct nfsexstuff *exp)
2020 if (nd->nd_repstat) {
2021 nfsrv_postopattr(nd, getret, &at);
2025 nd->nd_repstat = nfsvno_statfs(vp, sf);
2026 getret = nfsvno_getattr(vp, &at, nd->nd_cred, p, 1);
2028 if (nd->nd_flag & ND_NFSV3)
2029 nfsrv_postopattr(nd, getret, &at);
2032 if (nd->nd_flag & ND_NFSV2) {
2033 NFSM_BUILD(tl, u_int32_t *, NFSX_V2STATFS);
2034 *tl++ = txdr_unsigned(NFS_V2MAXDATA);
2035 *tl++ = txdr_unsigned(sf->f_bsize);
2036 *tl++ = txdr_unsigned(sf->f_blocks);
2037 *tl++ = txdr_unsigned(sf->f_bfree);
2038 *tl = txdr_unsigned(sf->f_bavail);
2040 NFSM_BUILD(tl, u_int32_t *, NFSX_V3STATFS);
2041 tval = (u_quad_t)sf->f_blocks;
2042 tval *= (u_quad_t)sf->f_bsize;
2043 txdr_hyper(tval, tl); tl += 2;
2044 tval = (u_quad_t)sf->f_bfree;
2045 tval *= (u_quad_t)sf->f_bsize;
2046 txdr_hyper(tval, tl); tl += 2;
2047 tval = (u_quad_t)sf->f_bavail;
2048 tval *= (u_quad_t)sf->f_bsize;
2049 txdr_hyper(tval, tl); tl += 2;
2050 tval = (u_quad_t)sf->f_files;
2051 txdr_hyper(tval, tl); tl += 2;
2052 tval = (u_quad_t)sf->f_ffree;
2053 txdr_hyper(tval, tl); tl += 2;
2054 tval = (u_quad_t)sf->f_ffree;
2055 txdr_hyper(tval, tl); tl += 2;
2060 NFSEXITCODE2(0, nd);
2065 * nfs fsinfo service
2068 nfsrvd_fsinfo(struct nfsrv_descript *nd, int isdgram,
2069 vnode_t vp, NFSPROC_T *p, __unused struct nfsexstuff *exp)
2072 struct nfsfsinfo fs;
2076 if (nd->nd_repstat) {
2077 nfsrv_postopattr(nd, getret, &at);
2080 getret = nfsvno_getattr(vp, &at, nd->nd_cred, p, 1);
2081 nfsvno_getfs(&fs, isdgram);
2083 nfsrv_postopattr(nd, getret, &at);
2084 NFSM_BUILD(tl, u_int32_t *, NFSX_V3FSINFO);
2085 *tl++ = txdr_unsigned(fs.fs_rtmax);
2086 *tl++ = txdr_unsigned(fs.fs_rtpref);
2087 *tl++ = txdr_unsigned(fs.fs_rtmult);
2088 *tl++ = txdr_unsigned(fs.fs_wtmax);
2089 *tl++ = txdr_unsigned(fs.fs_wtpref);
2090 *tl++ = txdr_unsigned(fs.fs_wtmult);
2091 *tl++ = txdr_unsigned(fs.fs_dtpref);
2092 txdr_hyper(fs.fs_maxfilesize, tl);
2094 txdr_nfsv3time(&fs.fs_timedelta, tl);
2096 *tl = txdr_unsigned(fs.fs_properties);
2099 NFSEXITCODE2(0, nd);
2104 * nfs pathconf service
2107 nfsrvd_pathconf(struct nfsrv_descript *nd, __unused int isdgram,
2108 vnode_t vp, NFSPROC_T *p, __unused struct nfsexstuff *exp)
2110 struct nfsv3_pathconf *pc;
2112 register_t linkmax, namemax, chownres, notrunc;
2115 if (nd->nd_repstat) {
2116 nfsrv_postopattr(nd, getret, &at);
2119 nd->nd_repstat = nfsvno_pathconf(vp, _PC_LINK_MAX, &linkmax,
2121 if (!nd->nd_repstat)
2122 nd->nd_repstat = nfsvno_pathconf(vp, _PC_NAME_MAX, &namemax,
2124 if (!nd->nd_repstat)
2125 nd->nd_repstat=nfsvno_pathconf(vp, _PC_CHOWN_RESTRICTED,
2126 &chownres, nd->nd_cred, p);
2127 if (!nd->nd_repstat)
2128 nd->nd_repstat = nfsvno_pathconf(vp, _PC_NO_TRUNC, ¬runc,
2130 getret = nfsvno_getattr(vp, &at, nd->nd_cred, p, 1);
2132 nfsrv_postopattr(nd, getret, &at);
2133 if (!nd->nd_repstat) {
2134 NFSM_BUILD(pc, struct nfsv3_pathconf *, NFSX_V3PATHCONF);
2135 pc->pc_linkmax = txdr_unsigned(linkmax);
2136 pc->pc_namemax = txdr_unsigned(namemax);
2137 pc->pc_notrunc = txdr_unsigned(notrunc);
2138 pc->pc_chownrestricted = txdr_unsigned(chownres);
2141 * These should probably be supported by VOP_PATHCONF(), but
2142 * until msdosfs is exportable (why would you want to?), the
2143 * Unix defaults should be ok.
2145 pc->pc_caseinsensitive = newnfs_false;
2146 pc->pc_casepreserving = newnfs_true;
2150 NFSEXITCODE2(0, nd);
2155 * nfsv4 lock service
2158 nfsrvd_lock(struct nfsrv_descript *nd, __unused int isdgram,
2159 vnode_t vp, NFSPROC_T *p, struct nfsexstuff *exp)
2163 struct nfsstate *stp = NULL;
2164 struct nfslock *lop;
2165 struct nfslockconflict cf;
2167 u_short flags = NFSLCK_LOCK, lflags;
2168 u_int64_t offset, len;
2169 nfsv4stateid_t stateid;
2172 NFSM_DISSECT(tl, u_int32_t *, 7 * NFSX_UNSIGNED);
2173 i = fxdr_unsigned(int, *tl++);
2175 case NFSV4LOCKT_READW:
2176 flags |= NFSLCK_BLOCKING;
2177 case NFSV4LOCKT_READ:
2178 lflags = NFSLCK_READ;
2180 case NFSV4LOCKT_WRITEW:
2181 flags |= NFSLCK_BLOCKING;
2182 case NFSV4LOCKT_WRITE:
2183 lflags = NFSLCK_WRITE;
2186 nd->nd_repstat = NFSERR_BADXDR;
2189 if (*tl++ == newnfs_true)
2190 flags |= NFSLCK_RECLAIM;
2191 offset = fxdr_hyper(tl);
2193 len = fxdr_hyper(tl);
2195 if (*tl == newnfs_true)
2196 flags |= NFSLCK_OPENTOLOCK;
2197 if (flags & NFSLCK_OPENTOLOCK) {
2198 NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED + NFSX_STATEID);
2199 i = fxdr_unsigned(int, *(tl+4+(NFSX_STATEID / NFSX_UNSIGNED)));
2200 if (i <= 0 || i > NFSV4_OPAQUELIMIT) {
2201 nd->nd_repstat = NFSERR_BADXDR;
2204 MALLOC(stp, struct nfsstate *, sizeof (struct nfsstate) + i,
2205 M_NFSDSTATE, M_WAITOK);
2206 stp->ls_ownerlen = i;
2207 stp->ls_op = nd->nd_rp;
2208 stp->ls_seq = fxdr_unsigned(int, *tl++);
2209 stp->ls_stateid.seqid = fxdr_unsigned(u_int32_t, *tl++);
2210 NFSBCOPY((caddr_t)tl, (caddr_t)stp->ls_stateid.other,
2212 tl += (NFSX_STATEIDOTHER / NFSX_UNSIGNED);
2213 stp->ls_opentolockseq = fxdr_unsigned(int, *tl++);
2214 clientid.lval[0] = *tl++;
2215 clientid.lval[1] = *tl++;
2216 if ((nd->nd_flag & ND_IMPLIEDCLID) != 0) {
2217 if ((nd->nd_flag & ND_NFSV41) != 0)
2218 clientid.qval = nd->nd_clientid.qval;
2219 else if (nd->nd_clientid.qval != clientid.qval)
2220 printf("EEK3 multiple clids\n");
2222 if ((nd->nd_flag & ND_NFSV41) != 0)
2223 printf("EEK! no clientid from session\n");
2224 nd->nd_flag |= ND_IMPLIEDCLID;
2225 nd->nd_clientid.qval = clientid.qval;
2227 error = nfsrv_mtostr(nd, stp->ls_owner, stp->ls_ownerlen);
2231 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID + NFSX_UNSIGNED);
2232 MALLOC(stp, struct nfsstate *, sizeof (struct nfsstate),
2233 M_NFSDSTATE, M_WAITOK);
2234 stp->ls_ownerlen = 0;
2235 stp->ls_op = nd->nd_rp;
2236 stp->ls_stateid.seqid = fxdr_unsigned(u_int32_t, *tl++);
2237 NFSBCOPY((caddr_t)tl, (caddr_t)stp->ls_stateid.other,
2239 tl += (NFSX_STATEIDOTHER / NFSX_UNSIGNED);
2240 stp->ls_seq = fxdr_unsigned(int, *tl);
2241 clientid.lval[0] = stp->ls_stateid.other[0];
2242 clientid.lval[1] = stp->ls_stateid.other[1];
2243 if ((nd->nd_flag & ND_IMPLIEDCLID) != 0) {
2244 if ((nd->nd_flag & ND_NFSV41) != 0)
2245 clientid.qval = nd->nd_clientid.qval;
2246 else if (nd->nd_clientid.qval != clientid.qval)
2247 printf("EEK4 multiple clids\n");
2249 if ((nd->nd_flag & ND_NFSV41) != 0)
2250 printf("EEK! no clientid from session\n");
2251 nd->nd_flag |= ND_IMPLIEDCLID;
2252 nd->nd_clientid.qval = clientid.qval;
2255 MALLOC(lop, struct nfslock *, sizeof (struct nfslock),
2256 M_NFSDLOCK, M_WAITOK);
2257 lop->lo_first = offset;
2258 if (len == NFS64BITSSET) {
2259 lop->lo_end = NFS64BITSSET;
2261 lop->lo_end = offset + len;
2262 if (lop->lo_end <= lop->lo_first)
2263 nd->nd_repstat = NFSERR_INVAL;
2265 lop->lo_flags = lflags;
2266 stp->ls_flags = flags;
2267 stp->ls_uid = nd->nd_cred->cr_uid;
2270 * Do basic access checking.
2272 if (!nd->nd_repstat && vnode_vtype(vp) != VREG) {
2273 if (vnode_vtype(vp) == VDIR)
2274 nd->nd_repstat = NFSERR_ISDIR;
2276 nd->nd_repstat = NFSERR_INVAL;
2278 if (!nd->nd_repstat) {
2279 if (lflags & NFSLCK_WRITE) {
2280 nd->nd_repstat = nfsvno_accchk(vp, VWRITE,
2281 nd->nd_cred, exp, p, NFSACCCHK_ALLOWOWNER,
2282 NFSACCCHK_VPISLOCKED, NULL);
2284 nd->nd_repstat = nfsvno_accchk(vp, VREAD,
2285 nd->nd_cred, exp, p, NFSACCCHK_ALLOWOWNER,
2286 NFSACCCHK_VPISLOCKED, NULL);
2288 nd->nd_repstat = nfsvno_accchk(vp, VEXEC,
2289 nd->nd_cred, exp, p, NFSACCCHK_ALLOWOWNER,
2290 NFSACCCHK_VPISLOCKED, NULL);
2295 * We call nfsrv_lockctrl() even if nd_repstat set, so that the
2296 * seqid# gets updated. nfsrv_lockctrl() will return the value
2297 * of nd_repstat, if it gets that far.
2299 nd->nd_repstat = nfsrv_lockctrl(vp, &stp, &lop, &cf, clientid,
2300 &stateid, exp, nd, p);
2302 FREE((caddr_t)lop, M_NFSDLOCK);
2304 FREE((caddr_t)stp, M_NFSDSTATE);
2305 if (!nd->nd_repstat) {
2306 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID);
2307 *tl++ = txdr_unsigned(stateid.seqid);
2308 NFSBCOPY((caddr_t)stateid.other,(caddr_t)tl,NFSX_STATEIDOTHER);
2309 } else if (nd->nd_repstat == NFSERR_DENIED) {
2310 NFSM_BUILD(tl, u_int32_t *, 7 * NFSX_UNSIGNED);
2311 txdr_hyper(cf.cl_first, tl);
2313 if (cf.cl_end == NFS64BITSSET)
2316 len = cf.cl_end - cf.cl_first;
2317 txdr_hyper(len, tl);
2319 if (cf.cl_flags == NFSLCK_WRITE)
2320 *tl++ = txdr_unsigned(NFSV4LOCKT_WRITE);
2322 *tl++ = txdr_unsigned(NFSV4LOCKT_READ);
2323 *tl++ = stateid.other[0];
2324 *tl = stateid.other[1];
2325 (void) nfsm_strtom(nd, cf.cl_owner, cf.cl_ownerlen);
2328 NFSEXITCODE2(0, nd);
2333 free((caddr_t)stp, M_NFSDSTATE);
2334 NFSEXITCODE2(error, nd);
2339 * nfsv4 lock test service
2342 nfsrvd_lockt(struct nfsrv_descript *nd, __unused int isdgram,
2343 vnode_t vp, NFSPROC_T *p, struct nfsexstuff *exp)
2347 struct nfsstate *stp = NULL;
2348 struct nfslock lo, *lop = &lo;
2349 struct nfslockconflict cf;
2351 nfsv4stateid_t stateid;
2355 NFSM_DISSECT(tl, u_int32_t *, 8 * NFSX_UNSIGNED);
2356 i = fxdr_unsigned(int, *(tl + 7));
2357 if (i <= 0 || i > NFSV4_OPAQUELIMIT) {
2358 nd->nd_repstat = NFSERR_BADXDR;
2361 MALLOC(stp, struct nfsstate *, sizeof (struct nfsstate) + i,
2362 M_NFSDSTATE, M_WAITOK);
2363 stp->ls_ownerlen = i;
2365 stp->ls_flags = NFSLCK_TEST;
2366 stp->ls_uid = nd->nd_cred->cr_uid;
2367 i = fxdr_unsigned(int, *tl++);
2369 case NFSV4LOCKT_READW:
2370 stp->ls_flags |= NFSLCK_BLOCKING;
2371 case NFSV4LOCKT_READ:
2372 lo.lo_flags = NFSLCK_READ;
2374 case NFSV4LOCKT_WRITEW:
2375 stp->ls_flags |= NFSLCK_BLOCKING;
2376 case NFSV4LOCKT_WRITE:
2377 lo.lo_flags = NFSLCK_WRITE;
2380 nd->nd_repstat = NFSERR_BADXDR;
2383 lo.lo_first = fxdr_hyper(tl);
2385 len = fxdr_hyper(tl);
2386 if (len == NFS64BITSSET) {
2387 lo.lo_end = NFS64BITSSET;
2389 lo.lo_end = lo.lo_first + len;
2390 if (lo.lo_end <= lo.lo_first)
2391 nd->nd_repstat = NFSERR_INVAL;
2394 clientid.lval[0] = *tl++;
2395 clientid.lval[1] = *tl;
2396 if ((nd->nd_flag & ND_IMPLIEDCLID) != 0) {
2397 if ((nd->nd_flag & ND_NFSV41) != 0)
2398 clientid.qval = nd->nd_clientid.qval;
2399 else if (nd->nd_clientid.qval != clientid.qval)
2400 printf("EEK5 multiple clids\n");
2402 if ((nd->nd_flag & ND_NFSV41) != 0)
2403 printf("EEK! no clientid from session\n");
2404 nd->nd_flag |= ND_IMPLIEDCLID;
2405 nd->nd_clientid.qval = clientid.qval;
2407 error = nfsrv_mtostr(nd, stp->ls_owner, stp->ls_ownerlen);
2410 if (!nd->nd_repstat && vnode_vtype(vp) != VREG) {
2411 if (vnode_vtype(vp) == VDIR)
2412 nd->nd_repstat = NFSERR_ISDIR;
2414 nd->nd_repstat = NFSERR_INVAL;
2416 if (!nd->nd_repstat)
2417 nd->nd_repstat = nfsrv_lockctrl(vp, &stp, &lop, &cf, clientid,
2418 &stateid, exp, nd, p);
2420 FREE((caddr_t)stp, M_NFSDSTATE);
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);
2442 NFSEXITCODE2(0, nd);
2447 free((caddr_t)stp, M_NFSDSTATE);
2448 NFSEXITCODE2(error, nd);
2453 * nfsv4 unlock service
2456 nfsrvd_locku(struct nfsrv_descript *nd, __unused int isdgram,
2457 vnode_t vp, NFSPROC_T *p, struct nfsexstuff *exp)
2461 struct nfsstate *stp;
2462 struct nfslock *lop;
2464 nfsv4stateid_t stateid;
2468 NFSM_DISSECT(tl, u_int32_t *, 6 * NFSX_UNSIGNED + NFSX_STATEID);
2469 MALLOC(stp, struct nfsstate *, sizeof (struct nfsstate),
2470 M_NFSDSTATE, M_WAITOK);
2471 MALLOC(lop, struct nfslock *, sizeof (struct nfslock),
2472 M_NFSDLOCK, M_WAITOK);
2473 stp->ls_flags = NFSLCK_UNLOCK;
2474 lop->lo_flags = NFSLCK_UNLOCK;
2475 stp->ls_op = nd->nd_rp;
2476 i = fxdr_unsigned(int, *tl++);
2478 case NFSV4LOCKT_READW:
2479 stp->ls_flags |= NFSLCK_BLOCKING;
2480 case NFSV4LOCKT_READ:
2482 case NFSV4LOCKT_WRITEW:
2483 stp->ls_flags |= NFSLCK_BLOCKING;
2484 case NFSV4LOCKT_WRITE:
2487 nd->nd_repstat = NFSERR_BADXDR;
2488 free(stp, M_NFSDSTATE);
2489 free(lop, M_NFSDLOCK);
2492 stp->ls_ownerlen = 0;
2493 stp->ls_uid = nd->nd_cred->cr_uid;
2494 stp->ls_seq = fxdr_unsigned(int, *tl++);
2495 stp->ls_stateid.seqid = fxdr_unsigned(u_int32_t, *tl++);
2496 NFSBCOPY((caddr_t)tl, (caddr_t)stp->ls_stateid.other,
2498 tl += (NFSX_STATEIDOTHER / NFSX_UNSIGNED);
2499 lop->lo_first = fxdr_hyper(tl);
2501 len = fxdr_hyper(tl);
2502 if (len == NFS64BITSSET) {
2503 lop->lo_end = NFS64BITSSET;
2505 lop->lo_end = lop->lo_first + len;
2506 if (lop->lo_end <= lop->lo_first)
2507 nd->nd_repstat = NFSERR_INVAL;
2509 clientid.lval[0] = stp->ls_stateid.other[0];
2510 clientid.lval[1] = stp->ls_stateid.other[1];
2511 if ((nd->nd_flag & ND_IMPLIEDCLID) != 0) {
2512 if ((nd->nd_flag & ND_NFSV41) != 0)
2513 clientid.qval = nd->nd_clientid.qval;
2514 else if (nd->nd_clientid.qval != clientid.qval)
2515 printf("EEK6 multiple clids\n");
2517 if ((nd->nd_flag & ND_NFSV41) != 0)
2518 printf("EEK! no clientid from session\n");
2519 nd->nd_flag |= ND_IMPLIEDCLID;
2520 nd->nd_clientid.qval = clientid.qval;
2522 if (!nd->nd_repstat && vnode_vtype(vp) != VREG) {
2523 if (vnode_vtype(vp) == VDIR)
2524 nd->nd_repstat = NFSERR_ISDIR;
2526 nd->nd_repstat = NFSERR_INVAL;
2529 * Call nfsrv_lockctrl() even if nd_repstat is set, so that the
2530 * seqid# gets incremented. nfsrv_lockctrl() will return the
2531 * value of nd_repstat, if it gets that far.
2533 nd->nd_repstat = nfsrv_lockctrl(vp, &stp, &lop, NULL, clientid,
2534 &stateid, exp, nd, p);
2536 FREE((caddr_t)stp, M_NFSDSTATE);
2538 free((caddr_t)lop, M_NFSDLOCK);
2539 if (!nd->nd_repstat) {
2540 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID);
2541 *tl++ = txdr_unsigned(stateid.seqid);
2542 NFSBCOPY((caddr_t)stateid.other,(caddr_t)tl,NFSX_STATEIDOTHER);
2546 NFSEXITCODE2(error, nd);
2551 * nfsv4 open service
2554 nfsrvd_open(struct nfsrv_descript *nd, __unused int isdgram,
2555 vnode_t dp, vnode_t *vpp, __unused fhandle_t *fhp, NFSPROC_T *p,
2556 struct nfsexstuff *exp)
2560 struct nfsstate *stp = NULL;
2561 int error = 0, create, claim, exclusive_flag = 0;
2562 u_int32_t rflags = NFSV4OPEN_LOCKTYPEPOSIX, acemask;
2563 int how = NFSCREATE_UNCHECKED;
2564 int32_t cverf[2], tverf[2] = { 0, 0 };
2565 vnode_t vp = NULL, dirp = NULL;
2566 struct nfsvattr nva, dirfor, diraft;
2567 struct nameidata named;
2568 nfsv4stateid_t stateid, delegstateid;
2569 nfsattrbit_t attrbits;
2573 NFSACL_T *aclp = NULL;
2575 #ifdef NFS4_ACL_EXTATTR_NAME
2576 aclp = acl_alloc(M_WAITOK);
2579 NFSZERO_ATTRBIT(&attrbits);
2580 named.ni_startdir = NULL;
2581 named.ni_cnd.cn_nameiop = 0;
2582 NFSM_DISSECT(tl, u_int32_t *, 6 * NFSX_UNSIGNED);
2583 i = fxdr_unsigned(int, *(tl + 5));
2584 if (i <= 0 || i > NFSV4_OPAQUELIMIT) {
2585 nd->nd_repstat = NFSERR_BADXDR;
2588 MALLOC(stp, struct nfsstate *, sizeof (struct nfsstate) + i,
2589 M_NFSDSTATE, M_WAITOK);
2590 stp->ls_ownerlen = i;
2591 stp->ls_op = nd->nd_rp;
2592 stp->ls_flags = NFSLCK_OPEN;
2593 stp->ls_uid = nd->nd_cred->cr_uid;
2594 stp->ls_seq = fxdr_unsigned(u_int32_t, *tl++);
2595 i = fxdr_unsigned(int, *tl++);
2597 if ((i & (NFSV4OPEN_WANTDELEGMASK | NFSV4OPEN_WANTSIGNALDELEG |
2598 NFSV4OPEN_WANTPUSHDELEG)) != 0 && (nd->nd_flag & ND_NFSV41) != 0) {
2600 /* For now, ignore these. */
2601 i &= ~(NFSV4OPEN_WANTPUSHDELEG | NFSV4OPEN_WANTSIGNALDELEG);
2602 switch (i & NFSV4OPEN_WANTDELEGMASK) {
2603 case NFSV4OPEN_WANTANYDELEG:
2604 stp->ls_flags |= (NFSLCK_WANTRDELEG |
2606 i &= ~NFSV4OPEN_WANTDELEGMASK;
2608 case NFSV4OPEN_WANTREADDELEG:
2609 stp->ls_flags |= NFSLCK_WANTRDELEG;
2610 i &= ~NFSV4OPEN_WANTDELEGMASK;
2612 case NFSV4OPEN_WANTWRITEDELEG:
2613 stp->ls_flags |= NFSLCK_WANTWDELEG;
2614 i &= ~NFSV4OPEN_WANTDELEGMASK;
2616 case NFSV4OPEN_WANTNODELEG:
2617 stp->ls_flags |= NFSLCK_WANTNODELEG;
2618 i &= ~NFSV4OPEN_WANTDELEGMASK;
2620 case NFSV4OPEN_WANTCANCEL:
2621 printf("NFSv4: ignore Open WantCancel\n");
2622 i &= ~NFSV4OPEN_WANTDELEGMASK;
2625 /* nd_repstat will be set to NFSERR_INVAL below. */
2630 case NFSV4OPEN_ACCESSREAD:
2631 stp->ls_flags |= NFSLCK_READACCESS;
2633 case NFSV4OPEN_ACCESSWRITE:
2634 stp->ls_flags |= NFSLCK_WRITEACCESS;
2636 case NFSV4OPEN_ACCESSBOTH:
2637 stp->ls_flags |= (NFSLCK_READACCESS | NFSLCK_WRITEACCESS);
2640 nd->nd_repstat = NFSERR_INVAL;
2642 i = fxdr_unsigned(int, *tl++);
2644 case NFSV4OPEN_DENYNONE:
2646 case NFSV4OPEN_DENYREAD:
2647 stp->ls_flags |= NFSLCK_READDENY;
2649 case NFSV4OPEN_DENYWRITE:
2650 stp->ls_flags |= NFSLCK_WRITEDENY;
2652 case NFSV4OPEN_DENYBOTH:
2653 stp->ls_flags |= (NFSLCK_READDENY | NFSLCK_WRITEDENY);
2656 nd->nd_repstat = NFSERR_INVAL;
2658 clientid.lval[0] = *tl++;
2659 clientid.lval[1] = *tl;
2660 if ((nd->nd_flag & ND_IMPLIEDCLID) != 0) {
2661 if ((nd->nd_flag & ND_NFSV41) != 0)
2662 clientid.qval = nd->nd_clientid.qval;
2663 else if (nd->nd_clientid.qval != clientid.qval)
2664 printf("EEK7 multiple clids\n");
2666 if ((nd->nd_flag & ND_NFSV41) != 0)
2667 printf("EEK! no clientid from session\n");
2668 nd->nd_flag |= ND_IMPLIEDCLID;
2669 nd->nd_clientid.qval = clientid.qval;
2671 error = nfsrv_mtostr(nd, stp->ls_owner, stp->ls_ownerlen);
2674 NFSVNO_ATTRINIT(&nva);
2675 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2676 create = fxdr_unsigned(int, *tl);
2677 if (!nd->nd_repstat)
2678 nd->nd_repstat = nfsvno_getattr(dp, &dirfor, nd->nd_cred, p, 0);
2679 if (create == NFSV4OPEN_CREATE) {
2682 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2683 how = fxdr_unsigned(int, *tl);
2685 case NFSCREATE_UNCHECKED:
2686 case NFSCREATE_GUARDED:
2687 error = nfsv4_sattr(nd, &nva, &attrbits, aclp, p);
2691 * If the na_gid being set is the same as that of
2692 * the directory it is going in, clear it, since
2693 * that is what will be set by default. This allows
2694 * a user that isn't in that group to do the create.
2696 if (!nd->nd_repstat && NFSVNO_ISSETGID(&nva) &&
2697 nva.na_gid == dirfor.na_gid)
2698 NFSVNO_UNSET(&nva, gid);
2699 if (!nd->nd_repstat)
2700 nd->nd_repstat = nfsrv_checkuidgid(nd, &nva);
2702 case NFSCREATE_EXCLUSIVE:
2703 NFSM_DISSECT(tl, u_int32_t *, NFSX_VERF);
2707 case NFSCREATE_EXCLUSIVE41:
2708 NFSM_DISSECT(tl, u_int32_t *, NFSX_VERF);
2711 error = nfsv4_sattr(nd, &nva, &attrbits, aclp, p);
2714 if (NFSISSET_ATTRBIT(&attrbits,
2715 NFSATTRBIT_TIMEACCESSSET))
2716 nd->nd_repstat = NFSERR_INVAL;
2718 * If the na_gid being set is the same as that of
2719 * the directory it is going in, clear it, since
2720 * that is what will be set by default. This allows
2721 * a user that isn't in that group to do the create.
2723 if (nd->nd_repstat == 0 && NFSVNO_ISSETGID(&nva) &&
2724 nva.na_gid == dirfor.na_gid)
2725 NFSVNO_UNSET(&nva, gid);
2726 if (nd->nd_repstat == 0)
2727 nd->nd_repstat = nfsrv_checkuidgid(nd, &nva);
2730 nd->nd_repstat = NFSERR_BADXDR;
2733 } else if (create != NFSV4OPEN_NOCREATE) {
2734 nd->nd_repstat = NFSERR_BADXDR;
2739 * Now, handle the claim, which usually includes looking up a
2740 * name in the directory referenced by dp. The exception is
2741 * NFSV4OPEN_CLAIMPREVIOUS.
2743 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2744 claim = fxdr_unsigned(int, *tl);
2745 if (claim == NFSV4OPEN_CLAIMDELEGATECUR) {
2746 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID);
2747 stateid.seqid = fxdr_unsigned(u_int32_t, *tl++);
2748 NFSBCOPY((caddr_t)tl,(caddr_t)stateid.other,NFSX_STATEIDOTHER);
2749 stp->ls_flags |= NFSLCK_DELEGCUR;
2750 } else if (claim == NFSV4OPEN_CLAIMDELEGATEPREV) {
2751 stp->ls_flags |= NFSLCK_DELEGPREV;
2753 if (claim == NFSV4OPEN_CLAIMNULL || claim == NFSV4OPEN_CLAIMDELEGATECUR
2754 || claim == NFSV4OPEN_CLAIMDELEGATEPREV) {
2755 if (!nd->nd_repstat && create == NFSV4OPEN_CREATE &&
2756 claim != NFSV4OPEN_CLAIMNULL)
2757 nd->nd_repstat = NFSERR_INVAL;
2758 if (nd->nd_repstat) {
2759 nd->nd_repstat = nfsrv_opencheck(clientid,
2760 &stateid, stp, NULL, nd, p, nd->nd_repstat);
2763 if (create == NFSV4OPEN_CREATE)
2764 NFSNAMEICNDSET(&named.ni_cnd, nd->nd_cred, CREATE,
2765 LOCKPARENT | LOCKLEAF | SAVESTART | NOCACHE);
2767 NFSNAMEICNDSET(&named.ni_cnd, nd->nd_cred, LOOKUP,
2768 LOCKLEAF | SAVESTART);
2769 nfsvno_setpathbuf(&named, &bufp, &hashp);
2770 error = nfsrv_parsename(nd, bufp, hashp, &named.ni_pathlen);
2773 #ifdef NFS4_ACL_EXTATTR_NAME
2776 FREE((caddr_t)stp, M_NFSDSTATE);
2777 nfsvno_relpathbuf(&named);
2778 NFSEXITCODE2(error, nd);
2781 if (!nd->nd_repstat) {
2782 nd->nd_repstat = nfsvno_namei(nd, &named, dp, 0, exp,
2786 nfsvno_relpathbuf(&named);
2788 if (create == NFSV4OPEN_CREATE) {
2790 case NFSCREATE_UNCHECKED:
2793 * Clear the setable attribute bits, except
2794 * for Size, if it is being truncated.
2796 NFSZERO_ATTRBIT(&attrbits);
2797 if (NFSVNO_ISSETSIZE(&nva))
2798 NFSSETBIT_ATTRBIT(&attrbits,
2802 case NFSCREATE_GUARDED:
2803 if (named.ni_vp && !nd->nd_repstat)
2804 nd->nd_repstat = EEXIST;
2806 case NFSCREATE_EXCLUSIVE:
2811 case NFSCREATE_EXCLUSIVE41:
2816 nfsvno_open(nd, &named, clientid, &stateid, stp,
2817 &exclusive_flag, &nva, cverf, create, aclp, &attrbits,
2818 nd->nd_cred, p, exp, &vp);
2819 } else if (claim == NFSV4OPEN_CLAIMPREVIOUS || claim ==
2820 NFSV4OPEN_CLAIMFH) {
2821 if (claim == NFSV4OPEN_CLAIMPREVIOUS) {
2822 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2823 i = fxdr_unsigned(int, *tl);
2825 case NFSV4OPEN_DELEGATEREAD:
2826 stp->ls_flags |= NFSLCK_DELEGREAD;
2828 case NFSV4OPEN_DELEGATEWRITE:
2829 stp->ls_flags |= NFSLCK_DELEGWRITE;
2830 case NFSV4OPEN_DELEGATENONE:
2833 nd->nd_repstat = NFSERR_BADXDR;
2836 stp->ls_flags |= NFSLCK_RECLAIM;
2839 if (nd->nd_repstat == 0 && create == NFSV4OPEN_CREATE)
2840 nd->nd_repstat = NFSERR_INVAL;
2843 NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY);
2844 if ((vp->v_iflag & VI_DOOMED) == 0)
2845 nd->nd_repstat = nfsrv_opencheck(clientid, &stateid,
2846 stp, vp, nd, p, nd->nd_repstat);
2848 nd->nd_repstat = NFSERR_PERM;
2850 nd->nd_repstat = NFSERR_BADXDR;
2855 * Do basic access checking.
2857 if (!nd->nd_repstat && vnode_vtype(vp) != VREG) {
2859 * The IETF working group decided that this is the correct
2860 * error return for all non-regular files.
2862 nd->nd_repstat = NFSERR_SYMLINK;
2864 if (!nd->nd_repstat && (stp->ls_flags & NFSLCK_WRITEACCESS))
2865 nd->nd_repstat = nfsvno_accchk(vp, VWRITE, nd->nd_cred,
2866 exp, p, NFSACCCHK_ALLOWOWNER, NFSACCCHK_VPISLOCKED, NULL);
2867 if (!nd->nd_repstat && (stp->ls_flags & NFSLCK_READACCESS)) {
2868 nd->nd_repstat = nfsvno_accchk(vp, VREAD, nd->nd_cred,
2869 exp, p, NFSACCCHK_ALLOWOWNER, NFSACCCHK_VPISLOCKED, NULL);
2871 nd->nd_repstat = nfsvno_accchk(vp, VEXEC,
2872 nd->nd_cred, exp, p, NFSACCCHK_ALLOWOWNER,
2873 NFSACCCHK_VPISLOCKED, NULL);
2876 if (!nd->nd_repstat) {
2877 nd->nd_repstat = nfsvno_getattr(vp, &nva, nd->nd_cred, p, 1);
2878 if (!nd->nd_repstat) {
2879 tverf[0] = nva.na_atime.tv_sec;
2880 tverf[1] = nva.na_atime.tv_nsec;
2883 if (!nd->nd_repstat && exclusive_flag && (cverf[0] != tverf[0] ||
2884 cverf[1] != tverf[1]))
2885 nd->nd_repstat = EEXIST;
2887 * Do the open locking/delegation stuff.
2889 if (!nd->nd_repstat)
2890 nd->nd_repstat = nfsrv_openctrl(nd, vp, &stp, clientid, &stateid,
2891 &delegstateid, &rflags, exp, p, nva.na_filerev);
2894 * vp must be unlocked before the call to nfsvno_getattr(dirp,...)
2895 * below, to avoid a deadlock with the lookup in nfsvno_namei() above.
2896 * (ie: Leave the NFSVOPUNLOCK() about here.)
2899 NFSVOPUNLOCK(vp, 0);
2901 FREE((caddr_t)stp, M_NFSDSTATE);
2902 if (!nd->nd_repstat && dirp)
2903 nd->nd_repstat = nfsvno_getattr(dirp, &diraft, nd->nd_cred, p,
2905 if (!nd->nd_repstat) {
2906 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID + 6 * NFSX_UNSIGNED);
2907 *tl++ = txdr_unsigned(stateid.seqid);
2908 NFSBCOPY((caddr_t)stateid.other,(caddr_t)tl,NFSX_STATEIDOTHER);
2909 tl += (NFSX_STATEIDOTHER / NFSX_UNSIGNED);
2910 if (claim == NFSV4OPEN_CLAIMPREVIOUS) {
2911 *tl++ = newnfs_true;
2917 *tl++ = newnfs_false; /* Since dirp is not locked */
2918 txdr_hyper(dirfor.na_filerev, tl);
2920 txdr_hyper(diraft.na_filerev, tl);
2923 *tl = txdr_unsigned(rflags & NFSV4OPEN_RFLAGS);
2924 (void) nfsrv_putattrbit(nd, &attrbits);
2925 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2926 if (rflags & NFSV4OPEN_READDELEGATE)
2927 *tl = txdr_unsigned(NFSV4OPEN_DELEGATEREAD);
2928 else if (rflags & NFSV4OPEN_WRITEDELEGATE)
2929 *tl = txdr_unsigned(NFSV4OPEN_DELEGATEWRITE);
2930 else if (retext != 0) {
2931 *tl = txdr_unsigned(NFSV4OPEN_DELEGATENONEEXT);
2932 if ((rflags & NFSV4OPEN_WDCONTENTION) != 0) {
2933 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2934 *tl++ = txdr_unsigned(NFSV4OPEN_CONTENTION);
2936 } else if ((rflags & NFSV4OPEN_WDRESOURCE) != 0) {
2937 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2938 *tl++ = txdr_unsigned(NFSV4OPEN_RESOURCE);
2941 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2942 *tl = txdr_unsigned(NFSV4OPEN_NOTWANTED);
2945 *tl = txdr_unsigned(NFSV4OPEN_DELEGATENONE);
2946 if (rflags & (NFSV4OPEN_READDELEGATE|NFSV4OPEN_WRITEDELEGATE)) {
2947 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID+NFSX_UNSIGNED);
2948 *tl++ = txdr_unsigned(delegstateid.seqid);
2949 NFSBCOPY((caddr_t)delegstateid.other, (caddr_t)tl,
2951 tl += (NFSX_STATEIDOTHER / NFSX_UNSIGNED);
2952 if (rflags & NFSV4OPEN_RECALL)
2956 if (rflags & NFSV4OPEN_WRITEDELEGATE) {
2957 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2958 *tl++ = txdr_unsigned(NFSV4OPEN_LIMITSIZE);
2959 txdr_hyper(nva.na_size, tl);
2961 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2962 *tl++ = txdr_unsigned(NFSV4ACE_ALLOWEDTYPE);
2963 *tl++ = txdr_unsigned(0x0);
2964 acemask = NFSV4ACE_ALLFILESMASK;
2965 if (nva.na_mode & S_IRUSR)
2966 acemask |= NFSV4ACE_READMASK;
2967 if (nva.na_mode & S_IWUSR)
2968 acemask |= NFSV4ACE_WRITEMASK;
2969 if (nva.na_mode & S_IXUSR)
2970 acemask |= NFSV4ACE_EXECUTEMASK;
2971 *tl = txdr_unsigned(acemask);
2972 (void) nfsm_strtom(nd, "OWNER@", 6);
2980 #ifdef NFS4_ACL_EXTATTR_NAME
2983 NFSEXITCODE2(0, nd);
2987 #ifdef NFS4_ACL_EXTATTR_NAME
2991 FREE((caddr_t)stp, M_NFSDSTATE);
2992 NFSEXITCODE2(error, nd);
2997 * nfsv4 close service
3000 nfsrvd_close(struct nfsrv_descript *nd, __unused int isdgram,
3001 vnode_t vp, NFSPROC_T *p, __unused struct nfsexstuff *exp)
3004 struct nfsstate st, *stp = &st;
3006 nfsv4stateid_t stateid;
3009 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED + NFSX_STATEID);
3010 stp->ls_seq = fxdr_unsigned(u_int32_t, *tl++);
3011 stp->ls_ownerlen = 0;
3012 stp->ls_op = nd->nd_rp;
3013 stp->ls_uid = nd->nd_cred->cr_uid;
3014 stp->ls_stateid.seqid = fxdr_unsigned(u_int32_t, *tl++);
3015 NFSBCOPY((caddr_t)tl, (caddr_t)stp->ls_stateid.other,
3017 stp->ls_flags = NFSLCK_CLOSE;
3018 clientid.lval[0] = stp->ls_stateid.other[0];
3019 clientid.lval[1] = stp->ls_stateid.other[1];
3020 if ((nd->nd_flag & ND_IMPLIEDCLID) != 0) {
3021 if ((nd->nd_flag & ND_NFSV41) != 0)
3022 clientid.qval = nd->nd_clientid.qval;
3023 else if (nd->nd_clientid.qval != clientid.qval)
3024 printf("EEK8 multiple clids\n");
3026 if ((nd->nd_flag & ND_NFSV41) != 0)
3027 printf("EEK! no clientid from session\n");
3028 nd->nd_flag |= ND_IMPLIEDCLID;
3029 nd->nd_clientid.qval = clientid.qval;
3031 nd->nd_repstat = nfsrv_openupdate(vp, stp, clientid, &stateid, nd, p);
3033 if (!nd->nd_repstat) {
3034 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID);
3035 *tl++ = txdr_unsigned(stateid.seqid);
3036 NFSBCOPY((caddr_t)stateid.other,(caddr_t)tl,NFSX_STATEIDOTHER);
3038 NFSEXITCODE2(0, nd);
3042 NFSEXITCODE2(error, nd);
3047 * nfsv4 delegpurge service
3050 nfsrvd_delegpurge(struct nfsrv_descript *nd, __unused int isdgram,
3051 __unused vnode_t vp, __unused NFSPROC_T *p, __unused struct nfsexstuff *exp)
3057 if (nfs_rootfhset == 0 || nfsd_checkrootexp(nd) != 0) {
3058 nd->nd_repstat = NFSERR_WRONGSEC;
3061 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
3062 clientid.lval[0] = *tl++;
3063 clientid.lval[1] = *tl;
3064 if ((nd->nd_flag & ND_IMPLIEDCLID) != 0) {
3065 if ((nd->nd_flag & ND_NFSV41) != 0)
3066 clientid.qval = nd->nd_clientid.qval;
3067 else if (nd->nd_clientid.qval != clientid.qval)
3068 printf("EEK9 multiple clids\n");
3070 if ((nd->nd_flag & ND_NFSV41) != 0)
3071 printf("EEK! no clientid from session\n");
3072 nd->nd_flag |= ND_IMPLIEDCLID;
3073 nd->nd_clientid.qval = clientid.qval;
3075 nd->nd_repstat = nfsrv_delegupdate(nd, clientid, NULL, NULL,
3076 NFSV4OP_DELEGPURGE, nd->nd_cred, p);
3078 NFSEXITCODE2(error, nd);
3083 * nfsv4 delegreturn service
3086 nfsrvd_delegreturn(struct nfsrv_descript *nd, __unused int isdgram,
3087 vnode_t vp, NFSPROC_T *p, __unused struct nfsexstuff *exp)
3091 nfsv4stateid_t stateid;
3094 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID);
3095 stateid.seqid = fxdr_unsigned(u_int32_t, *tl++);
3096 NFSBCOPY((caddr_t)tl, (caddr_t)stateid.other, NFSX_STATEIDOTHER);
3097 clientid.lval[0] = stateid.other[0];
3098 clientid.lval[1] = stateid.other[1];
3099 if ((nd->nd_flag & ND_IMPLIEDCLID) != 0) {
3100 if ((nd->nd_flag & ND_NFSV41) != 0)
3101 clientid.qval = nd->nd_clientid.qval;
3102 else if (nd->nd_clientid.qval != clientid.qval)
3103 printf("EEK10 multiple clids\n");
3105 if ((nd->nd_flag & ND_NFSV41) != 0)
3106 printf("EEK! no clientid from session\n");
3107 nd->nd_flag |= ND_IMPLIEDCLID;
3108 nd->nd_clientid.qval = clientid.qval;
3110 nd->nd_repstat = nfsrv_delegupdate(nd, clientid, &stateid, vp,
3111 NFSV4OP_DELEGRETURN, nd->nd_cred, p);
3114 NFSEXITCODE2(error, nd);
3119 * nfsv4 get file handle service
3122 nfsrvd_getfh(struct nfsrv_descript *nd, __unused int isdgram,
3123 vnode_t vp, NFSPROC_T *p, __unused struct nfsexstuff *exp)
3127 nd->nd_repstat = nfsvno_getfh(vp, &fh, p);
3129 if (!nd->nd_repstat)
3130 (void) nfsm_fhtom(nd, (u_int8_t *)&fh, 0, 0);
3131 NFSEXITCODE2(0, nd);
3136 * nfsv4 open confirm service
3139 nfsrvd_openconfirm(struct nfsrv_descript *nd, __unused int isdgram,
3140 vnode_t vp, NFSPROC_T *p, __unused struct nfsexstuff *exp)
3143 struct nfsstate st, *stp = &st;
3145 nfsv4stateid_t stateid;
3148 if ((nd->nd_flag & ND_NFSV41) != 0) {
3149 nd->nd_repstat = NFSERR_NOTSUPP;
3152 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID + NFSX_UNSIGNED);
3153 stp->ls_ownerlen = 0;
3154 stp->ls_op = nd->nd_rp;
3155 stp->ls_uid = nd->nd_cred->cr_uid;
3156 stp->ls_stateid.seqid = fxdr_unsigned(u_int32_t, *tl++);
3157 NFSBCOPY((caddr_t)tl, (caddr_t)stp->ls_stateid.other,
3159 tl += (NFSX_STATEIDOTHER / NFSX_UNSIGNED);
3160 stp->ls_seq = fxdr_unsigned(u_int32_t, *tl);
3161 stp->ls_flags = NFSLCK_CONFIRM;
3162 clientid.lval[0] = stp->ls_stateid.other[0];
3163 clientid.lval[1] = stp->ls_stateid.other[1];
3164 if ((nd->nd_flag & ND_IMPLIEDCLID) != 0) {
3165 if ((nd->nd_flag & ND_NFSV41) != 0)
3166 clientid.qval = nd->nd_clientid.qval;
3167 else if (nd->nd_clientid.qval != clientid.qval)
3168 printf("EEK11 multiple clids\n");
3170 if ((nd->nd_flag & ND_NFSV41) != 0)
3171 printf("EEK! no clientid from session\n");
3172 nd->nd_flag |= ND_IMPLIEDCLID;
3173 nd->nd_clientid.qval = clientid.qval;
3175 nd->nd_repstat = nfsrv_openupdate(vp, stp, clientid, &stateid, nd, p);
3176 if (!nd->nd_repstat) {
3177 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID);
3178 *tl++ = txdr_unsigned(stateid.seqid);
3179 NFSBCOPY((caddr_t)stateid.other,(caddr_t)tl,NFSX_STATEIDOTHER);
3183 NFSEXITCODE2(error, nd);
3188 * nfsv4 open downgrade service
3191 nfsrvd_opendowngrade(struct nfsrv_descript *nd, __unused int isdgram,
3192 vnode_t vp, NFSPROC_T *p, __unused struct nfsexstuff *exp)
3196 struct nfsstate st, *stp = &st;
3198 nfsv4stateid_t stateid;
3201 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID + 3 * NFSX_UNSIGNED);
3202 stp->ls_ownerlen = 0;
3203 stp->ls_op = nd->nd_rp;
3204 stp->ls_uid = nd->nd_cred->cr_uid;
3205 stp->ls_stateid.seqid = fxdr_unsigned(u_int32_t, *tl++);
3206 NFSBCOPY((caddr_t)tl, (caddr_t)stp->ls_stateid.other,
3208 tl += (NFSX_STATEIDOTHER / NFSX_UNSIGNED);
3209 stp->ls_seq = fxdr_unsigned(u_int32_t, *tl++);
3210 i = fxdr_unsigned(int, *tl++);
3212 case NFSV4OPEN_ACCESSREAD:
3213 stp->ls_flags = (NFSLCK_READACCESS | NFSLCK_DOWNGRADE);
3215 case NFSV4OPEN_ACCESSWRITE:
3216 stp->ls_flags = (NFSLCK_WRITEACCESS | NFSLCK_DOWNGRADE);
3218 case NFSV4OPEN_ACCESSBOTH:
3219 stp->ls_flags = (NFSLCK_READACCESS | NFSLCK_WRITEACCESS |
3223 nd->nd_repstat = NFSERR_BADXDR;
3225 i = fxdr_unsigned(int, *tl);
3227 case NFSV4OPEN_DENYNONE:
3229 case NFSV4OPEN_DENYREAD:
3230 stp->ls_flags |= NFSLCK_READDENY;
3232 case NFSV4OPEN_DENYWRITE:
3233 stp->ls_flags |= NFSLCK_WRITEDENY;
3235 case NFSV4OPEN_DENYBOTH:
3236 stp->ls_flags |= (NFSLCK_READDENY | NFSLCK_WRITEDENY);
3239 nd->nd_repstat = NFSERR_BADXDR;
3242 clientid.lval[0] = stp->ls_stateid.other[0];
3243 clientid.lval[1] = stp->ls_stateid.other[1];
3244 if ((nd->nd_flag & ND_IMPLIEDCLID) != 0) {
3245 if ((nd->nd_flag & ND_NFSV41) != 0)
3246 clientid.qval = nd->nd_clientid.qval;
3247 else if (nd->nd_clientid.qval != clientid.qval)
3248 printf("EEK12 multiple clids\n");
3250 if ((nd->nd_flag & ND_NFSV41) != 0)
3251 printf("EEK! no clientid from session\n");
3252 nd->nd_flag |= ND_IMPLIEDCLID;
3253 nd->nd_clientid.qval = clientid.qval;
3255 if (!nd->nd_repstat)
3256 nd->nd_repstat = nfsrv_openupdate(vp, stp, clientid, &stateid,
3258 if (!nd->nd_repstat) {
3259 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID);
3260 *tl++ = txdr_unsigned(stateid.seqid);
3261 NFSBCOPY((caddr_t)stateid.other,(caddr_t)tl,NFSX_STATEIDOTHER);
3265 NFSEXITCODE2(error, nd);
3270 * nfsv4 renew lease service
3273 nfsrvd_renew(struct nfsrv_descript *nd, __unused int isdgram,
3274 __unused vnode_t vp, NFSPROC_T *p, __unused struct nfsexstuff *exp)
3280 if ((nd->nd_flag & ND_NFSV41) != 0) {
3281 nd->nd_repstat = NFSERR_NOTSUPP;
3284 if (nfs_rootfhset == 0 || nfsd_checkrootexp(nd) != 0) {
3285 nd->nd_repstat = NFSERR_WRONGSEC;
3288 NFSM_DISSECT(tl, u_int32_t *, NFSX_HYPER);
3289 clientid.lval[0] = *tl++;
3290 clientid.lval[1] = *tl;
3291 if ((nd->nd_flag & ND_IMPLIEDCLID) != 0) {
3292 if ((nd->nd_flag & ND_NFSV41) != 0)
3293 clientid.qval = nd->nd_clientid.qval;
3294 else if (nd->nd_clientid.qval != clientid.qval)
3295 printf("EEK13 multiple clids\n");
3297 if ((nd->nd_flag & ND_NFSV41) != 0)
3298 printf("EEK! no clientid from session\n");
3299 nd->nd_flag |= ND_IMPLIEDCLID;
3300 nd->nd_clientid.qval = clientid.qval;
3302 nd->nd_repstat = nfsrv_getclient(clientid, (CLOPS_RENEWOP|CLOPS_RENEW),
3303 NULL, NULL, (nfsquad_t)((u_quad_t)0), 0, nd, p);
3305 NFSEXITCODE2(error, nd);
3310 * nfsv4 security info service
3313 nfsrvd_secinfo(struct nfsrv_descript *nd, int isdgram,
3314 vnode_t dp, NFSPROC_T *p, struct nfsexstuff *exp)
3318 struct nameidata named;
3319 vnode_t dirp = NULL, vp;
3321 struct nfsexstuff retnes;
3323 int error = 0, savflag, i;
3328 * All this just to get the export flags for the name.
3330 NFSNAMEICNDSET(&named.ni_cnd, nd->nd_cred, LOOKUP,
3331 LOCKLEAF | SAVESTART);
3332 nfsvno_setpathbuf(&named, &bufp, &hashp);
3333 error = nfsrv_parsename(nd, bufp, hashp, &named.ni_pathlen);
3336 nfsvno_relpathbuf(&named);
3339 if (!nd->nd_repstat) {
3340 nd->nd_repstat = nfsvno_namei(nd, &named, dp, 1, exp, p, &dirp);
3343 nfsvno_relpathbuf(&named);
3349 vrele(named.ni_startdir);
3350 nfsvno_relpathbuf(&named);
3351 fh.nfsrvfh_len = NFSX_MYFH;
3353 nd->nd_repstat = nfsvno_getfh(vp, (fhandle_t *)fh.nfsrvfh_data, p);
3355 savflag = nd->nd_flag;
3356 if (!nd->nd_repstat) {
3357 nfsd_fhtovp(nd, &fh, LK_SHARED, &vp, &retnes, NULL, 0, p);
3361 nd->nd_flag = savflag;
3366 * Finally have the export flags for name, so we can create
3367 * the security info.
3370 NFSM_BUILD(sizp, u_int32_t *, NFSX_UNSIGNED);
3371 for (i = 0; i < retnes.nes_numsecflavor; i++) {
3372 if (retnes.nes_secflavors[i] == AUTH_SYS) {
3373 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
3374 *tl = txdr_unsigned(RPCAUTH_UNIX);
3376 } else if (retnes.nes_secflavors[i] == RPCSEC_GSS_KRB5) {
3377 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
3378 *tl++ = txdr_unsigned(RPCAUTH_GSS);
3379 (void) nfsm_strtom(nd, nfsgss_mechlist[KERBV_MECH].str,
3380 nfsgss_mechlist[KERBV_MECH].len);
3381 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
3382 *tl++ = txdr_unsigned(GSS_KERBV_QOP);
3383 *tl = txdr_unsigned(RPCAUTHGSS_SVCNONE);
3385 } else if (retnes.nes_secflavors[i] == RPCSEC_GSS_KRB5I) {
3386 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
3387 *tl++ = txdr_unsigned(RPCAUTH_GSS);
3388 (void) nfsm_strtom(nd, nfsgss_mechlist[KERBV_MECH].str,
3389 nfsgss_mechlist[KERBV_MECH].len);
3390 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
3391 *tl++ = txdr_unsigned(GSS_KERBV_QOP);
3392 *tl = txdr_unsigned(RPCAUTHGSS_SVCINTEGRITY);
3394 } else if (retnes.nes_secflavors[i] == RPCSEC_GSS_KRB5P) {
3395 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
3396 *tl++ = txdr_unsigned(RPCAUTH_GSS);
3397 (void) nfsm_strtom(nd, nfsgss_mechlist[KERBV_MECH].str,
3398 nfsgss_mechlist[KERBV_MECH].len);
3399 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
3400 *tl++ = txdr_unsigned(GSS_KERBV_QOP);
3401 *tl = txdr_unsigned(RPCAUTHGSS_SVCPRIVACY);
3405 *sizp = txdr_unsigned(len);
3408 NFSEXITCODE2(error, nd);
3413 * nfsv4 set client id service
3416 nfsrvd_setclientid(struct nfsrv_descript *nd, __unused int isdgram,
3417 __unused vnode_t vp, NFSPROC_T *p, __unused struct nfsexstuff *exp)
3421 int error = 0, idlen;
3422 struct nfsclient *clp = NULL;
3423 struct sockaddr_in *rad;
3424 u_char *verf, *ucp, *ucp2, addrbuf[24];
3425 nfsquad_t clientid, confirm;
3427 if ((nd->nd_flag & ND_NFSV41) != 0) {
3428 nd->nd_repstat = NFSERR_NOTSUPP;
3431 if (nfs_rootfhset == 0 || nfsd_checkrootexp(nd) != 0) {
3432 nd->nd_repstat = NFSERR_WRONGSEC;
3435 NFSM_DISSECT(tl, u_int32_t *, NFSX_VERF + NFSX_UNSIGNED);
3436 verf = (u_char *)tl;
3437 tl += (NFSX_VERF / NFSX_UNSIGNED);
3438 i = fxdr_unsigned(int, *tl);
3439 if (i > NFSV4_OPAQUELIMIT || i <= 0) {
3440 nd->nd_repstat = NFSERR_BADXDR;
3444 if (nd->nd_flag & ND_GSS)
3445 i += nd->nd_princlen;
3446 clp = malloc(sizeof(struct nfsclient) + i, M_NFSDCLIENT, M_WAITOK |
3448 clp->lc_stateid = malloc(sizeof(struct nfsstatehead) *
3449 nfsrv_statehashsize, M_NFSDCLIENT, M_WAITOK);
3450 NFSINITSOCKMUTEX(&clp->lc_req.nr_mtx);
3451 NFSSOCKADDRALLOC(clp->lc_req.nr_nam);
3452 NFSSOCKADDRSIZE(clp->lc_req.nr_nam, sizeof (struct sockaddr_in));
3453 clp->lc_req.nr_cred = NULL;
3454 NFSBCOPY(verf, clp->lc_verf, NFSX_VERF);
3455 clp->lc_idlen = idlen;
3456 error = nfsrv_mtostr(nd, clp->lc_id, idlen);
3459 if (nd->nd_flag & ND_GSS) {
3460 clp->lc_flags = LCL_GSS;
3461 if (nd->nd_flag & ND_GSSINTEGRITY)
3462 clp->lc_flags |= LCL_GSSINTEGRITY;
3463 else if (nd->nd_flag & ND_GSSPRIVACY)
3464 clp->lc_flags |= LCL_GSSPRIVACY;
3468 if ((nd->nd_flag & ND_GSS) && nd->nd_princlen > 0) {
3469 clp->lc_flags |= LCL_NAME;
3470 clp->lc_namelen = nd->nd_princlen;
3471 clp->lc_name = &clp->lc_id[idlen];
3472 NFSBCOPY(nd->nd_principal, clp->lc_name, clp->lc_namelen);
3474 clp->lc_uid = nd->nd_cred->cr_uid;
3475 clp->lc_gid = nd->nd_cred->cr_gid;
3477 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3478 clp->lc_program = fxdr_unsigned(u_int32_t, *tl);
3479 error = nfsrv_getclientipaddr(nd, clp);
3482 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3483 clp->lc_callback = fxdr_unsigned(u_int32_t, *tl);
3486 * nfsrv_setclient() does the actual work of adding it to the
3487 * client list. If there is no error, the structure has been
3488 * linked into the client list and clp should no longer be used
3489 * here. When an error is returned, it has not been linked in,
3490 * so it should be free'd.
3492 nd->nd_repstat = nfsrv_setclient(nd, &clp, &clientid, &confirm, p);
3493 if (nd->nd_repstat == NFSERR_CLIDINUSE) {
3494 if (clp->lc_flags & LCL_TCPCALLBACK)
3495 (void) nfsm_strtom(nd, "tcp", 3);
3497 (void) nfsm_strtom(nd, "udp", 3);
3498 rad = NFSSOCKADDR(clp->lc_req.nr_nam, struct sockaddr_in *);
3499 ucp = (u_char *)&rad->sin_addr.s_addr;
3500 ucp2 = (u_char *)&rad->sin_port;
3501 sprintf(addrbuf, "%d.%d.%d.%d.%d.%d", ucp[0] & 0xff,
3502 ucp[1] & 0xff, ucp[2] & 0xff, ucp[3] & 0xff,
3503 ucp2[0] & 0xff, ucp2[1] & 0xff);
3504 (void) nfsm_strtom(nd, addrbuf, strlen(addrbuf));
3507 NFSSOCKADDRFREE(clp->lc_req.nr_nam);
3508 NFSFREEMUTEX(&clp->lc_req.nr_mtx);
3509 free(clp->lc_stateid, M_NFSDCLIENT);
3510 free(clp, M_NFSDCLIENT);
3512 if (!nd->nd_repstat) {
3513 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_HYPER);
3514 *tl++ = clientid.lval[0];
3515 *tl++ = clientid.lval[1];
3516 *tl++ = confirm.lval[0];
3517 *tl = confirm.lval[1];
3521 NFSEXITCODE2(0, nd);
3525 NFSSOCKADDRFREE(clp->lc_req.nr_nam);
3526 NFSFREEMUTEX(&clp->lc_req.nr_mtx);
3527 free(clp->lc_stateid, M_NFSDCLIENT);
3528 free(clp, M_NFSDCLIENT);
3530 NFSEXITCODE2(error, nd);
3535 * nfsv4 set client id confirm service
3538 nfsrvd_setclientidcfrm(struct nfsrv_descript *nd,
3539 __unused int isdgram, __unused vnode_t vp, NFSPROC_T *p,
3540 __unused struct nfsexstuff *exp)
3544 nfsquad_t clientid, confirm;
3546 if ((nd->nd_flag & ND_NFSV41) != 0) {
3547 nd->nd_repstat = NFSERR_NOTSUPP;
3550 if (nfs_rootfhset == 0 || nfsd_checkrootexp(nd) != 0) {
3551 nd->nd_repstat = NFSERR_WRONGSEC;
3554 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_HYPER);
3555 clientid.lval[0] = *tl++;
3556 clientid.lval[1] = *tl++;
3557 confirm.lval[0] = *tl++;
3558 confirm.lval[1] = *tl;
3561 * nfsrv_getclient() searches the client list for a match and
3562 * returns the appropriate NFSERR status.
3564 nd->nd_repstat = nfsrv_getclient(clientid, (CLOPS_CONFIRM|CLOPS_RENEW),
3565 NULL, NULL, confirm, 0, nd, p);
3567 NFSEXITCODE2(error, nd);
3572 * nfsv4 verify service
3575 nfsrvd_verify(struct nfsrv_descript *nd, int isdgram,
3576 vnode_t vp, NFSPROC_T *p, __unused struct nfsexstuff *exp)
3578 int error = 0, ret, fhsize = NFSX_MYFH;
3579 struct nfsvattr nva;
3581 struct nfsfsinfo fs;
3584 nd->nd_repstat = nfsvno_getattr(vp, &nva, nd->nd_cred, p, 1);
3585 if (!nd->nd_repstat)
3586 nd->nd_repstat = nfsvno_statfs(vp, &sf);
3587 if (!nd->nd_repstat)
3588 nd->nd_repstat = nfsvno_getfh(vp, &fh, p);
3589 if (!nd->nd_repstat) {
3590 nfsvno_getfs(&fs, isdgram);
3591 error = nfsv4_loadattr(nd, vp, &nva, NULL, &fh, fhsize, NULL,
3592 &sf, NULL, &fs, NULL, 1, &ret, NULL, NULL, p, nd->nd_cred);
3594 if (nd->nd_procnum == NFSV4OP_NVERIFY) {
3596 nd->nd_repstat = NFSERR_SAME;
3597 else if (ret != NFSERR_NOTSAME)
3598 nd->nd_repstat = ret;
3600 nd->nd_repstat = ret;
3604 NFSEXITCODE2(error, nd);
3612 nfsrvd_openattr(struct nfsrv_descript *nd, __unused int isdgram,
3613 vnode_t dp, __unused vnode_t *vpp, __unused fhandle_t *fhp,
3614 __unused NFSPROC_T *p, __unused struct nfsexstuff *exp)
3617 int error = 0, createdir;
3619 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3620 createdir = fxdr_unsigned(int, *tl);
3621 nd->nd_repstat = NFSERR_NOTSUPP;
3624 NFSEXITCODE2(error, nd);
3629 * nfsv4 release lock owner service
3632 nfsrvd_releaselckown(struct nfsrv_descript *nd, __unused int isdgram,
3633 __unused vnode_t vp, NFSPROC_T *p, __unused struct nfsexstuff *exp)
3636 struct nfsstate *stp = NULL;
3640 if ((nd->nd_flag & ND_NFSV41) != 0) {
3641 nd->nd_repstat = NFSERR_NOTSUPP;
3644 if (nfs_rootfhset == 0 || nfsd_checkrootexp(nd) != 0) {
3645 nd->nd_repstat = NFSERR_WRONGSEC;
3648 NFSM_DISSECT(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
3649 len = fxdr_unsigned(int, *(tl + 2));
3650 if (len <= 0 || len > NFSV4_OPAQUELIMIT) {
3651 nd->nd_repstat = NFSERR_BADXDR;
3654 MALLOC(stp, struct nfsstate *, sizeof (struct nfsstate) + len,
3655 M_NFSDSTATE, M_WAITOK);
3656 stp->ls_ownerlen = len;
3658 stp->ls_flags = NFSLCK_RELEASE;
3659 stp->ls_uid = nd->nd_cred->cr_uid;
3660 clientid.lval[0] = *tl++;
3661 clientid.lval[1] = *tl;
3662 if ((nd->nd_flag & ND_IMPLIEDCLID) != 0) {
3663 if ((nd->nd_flag & ND_NFSV41) != 0)
3664 clientid.qval = nd->nd_clientid.qval;
3665 else if (nd->nd_clientid.qval != clientid.qval)
3666 printf("EEK14 multiple clids\n");
3668 if ((nd->nd_flag & ND_NFSV41) != 0)
3669 printf("EEK! no clientid from session\n");
3670 nd->nd_flag |= ND_IMPLIEDCLID;
3671 nd->nd_clientid.qval = clientid.qval;
3673 error = nfsrv_mtostr(nd, stp->ls_owner, len);
3676 nd->nd_repstat = nfsrv_releaselckown(stp, clientid, p);
3677 FREE((caddr_t)stp, M_NFSDSTATE);
3679 NFSEXITCODE2(0, nd);
3683 free((caddr_t)stp, M_NFSDSTATE);
3684 NFSEXITCODE2(error, nd);
3689 * nfsv4 exchange_id service
3692 nfsrvd_exchangeid(struct nfsrv_descript *nd, __unused int isdgram,
3693 __unused vnode_t vp, NFSPROC_T *p, __unused struct nfsexstuff *exp)
3696 int error = 0, i, idlen;
3697 struct nfsclient *clp = NULL;
3698 nfsquad_t clientid, confirm;
3700 uint32_t sp4type, v41flags;
3701 uint64_t owner_minor;
3702 struct timespec verstime;
3704 if (nfs_rootfhset == 0 || nfsd_checkrootexp(nd) != 0) {
3705 nd->nd_repstat = NFSERR_WRONGSEC;
3708 NFSM_DISSECT(tl, u_int32_t *, NFSX_VERF + NFSX_UNSIGNED);
3709 verf = (uint8_t *)tl;
3710 tl += (NFSX_VERF / NFSX_UNSIGNED);
3711 i = fxdr_unsigned(int, *tl);
3712 if (i > NFSV4_OPAQUELIMIT || i <= 0) {
3713 nd->nd_repstat = NFSERR_BADXDR;
3717 if (nd->nd_flag & ND_GSS)
3718 i += nd->nd_princlen;
3719 clp = malloc(sizeof(struct nfsclient) + i, M_NFSDCLIENT, M_WAITOK |
3721 clp->lc_stateid = malloc(sizeof(struct nfsstatehead) *
3722 nfsrv_statehashsize, M_NFSDCLIENT, M_WAITOK);
3723 NFSINITSOCKMUTEX(&clp->lc_req.nr_mtx);
3724 NFSSOCKADDRALLOC(clp->lc_req.nr_nam);
3725 NFSSOCKADDRSIZE(clp->lc_req.nr_nam, sizeof (struct sockaddr_in));
3726 clp->lc_req.nr_cred = NULL;
3727 NFSBCOPY(verf, clp->lc_verf, NFSX_VERF);
3728 clp->lc_idlen = idlen;
3729 error = nfsrv_mtostr(nd, clp->lc_id, idlen);
3732 if ((nd->nd_flag & ND_GSS) != 0) {
3733 clp->lc_flags = LCL_GSS | LCL_NFSV41;
3734 if ((nd->nd_flag & ND_GSSINTEGRITY) != 0)
3735 clp->lc_flags |= LCL_GSSINTEGRITY;
3736 else if ((nd->nd_flag & ND_GSSPRIVACY) != 0)
3737 clp->lc_flags |= LCL_GSSPRIVACY;
3739 clp->lc_flags = LCL_NFSV41;
3740 if ((nd->nd_flag & ND_GSS) != 0 && nd->nd_princlen > 0) {
3741 clp->lc_flags |= LCL_NAME;
3742 clp->lc_namelen = nd->nd_princlen;
3743 clp->lc_name = &clp->lc_id[idlen];
3744 NFSBCOPY(nd->nd_principal, clp->lc_name, clp->lc_namelen);
3746 clp->lc_uid = nd->nd_cred->cr_uid;
3747 clp->lc_gid = nd->nd_cred->cr_gid;
3749 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
3750 v41flags = fxdr_unsigned(uint32_t, *tl++);
3751 if ((v41flags & ~(NFSV4EXCH_SUPPMOVEDREFER | NFSV4EXCH_SUPPMOVEDMIGR |
3752 NFSV4EXCH_BINDPRINCSTATEID | NFSV4EXCH_MASKPNFS |
3753 NFSV4EXCH_UPDCONFIRMEDRECA)) != 0) {
3754 nd->nd_repstat = NFSERR_INVAL;
3757 if ((v41flags & NFSV4EXCH_UPDCONFIRMEDRECA) != 0)
3758 confirm.lval[1] = 1;
3760 confirm.lval[1] = 0;
3761 v41flags = NFSV4EXCH_USENONPNFS;
3762 sp4type = fxdr_unsigned(uint32_t, *tl);
3763 if (sp4type != NFSV4EXCH_SP4NONE) {
3764 nd->nd_repstat = NFSERR_NOTSUPP;
3769 * nfsrv_setclient() does the actual work of adding it to the
3770 * client list. If there is no error, the structure has been
3771 * linked into the client list and clp should no longer be used
3772 * here. When an error is returned, it has not been linked in,
3773 * so it should be free'd.
3775 nd->nd_repstat = nfsrv_setclient(nd, &clp, &clientid, &confirm, p);
3777 NFSSOCKADDRFREE(clp->lc_req.nr_nam);
3778 NFSFREEMUTEX(&clp->lc_req.nr_mtx);
3779 free(clp->lc_stateid, M_NFSDCLIENT);
3780 free(clp, M_NFSDCLIENT);
3782 if (nd->nd_repstat == 0) {
3783 if (confirm.lval[1] != 0)
3784 v41flags |= NFSV4EXCH_CONFIRMEDR;
3785 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_HYPER + 3 * NFSX_UNSIGNED);
3786 *tl++ = clientid.lval[0]; /* ClientID */
3787 *tl++ = clientid.lval[1];
3788 *tl++ = txdr_unsigned(confirm.lval[0]); /* SequenceID */
3789 *tl++ = txdr_unsigned(v41flags); /* Exch flags */
3790 *tl++ = txdr_unsigned(NFSV4EXCH_SP4NONE); /* No SSV */
3791 owner_minor = 0; /* Owner */
3792 txdr_hyper(owner_minor, tl); /* Minor */
3793 (void)nfsm_strtom(nd, nd->nd_cred->cr_prison->pr_hostuuid,
3794 strlen(nd->nd_cred->cr_prison->pr_hostuuid)); /* Major */
3795 NFSM_BUILD(tl, uint32_t *, 3 * NFSX_UNSIGNED);
3796 *tl++ = txdr_unsigned(NFSX_UNSIGNED);
3797 *tl++ = time_uptime; /* Make scope a unique value. */
3798 *tl = txdr_unsigned(1);
3799 (void)nfsm_strtom(nd, "freebsd.org", strlen("freebsd.org"));
3800 (void)nfsm_strtom(nd, version, strlen(version));
3801 NFSM_BUILD(tl, uint32_t *, NFSX_V4TIME);
3802 verstime.tv_sec = 1293840000; /* Jan 1, 2011 */
3803 verstime.tv_nsec = 0;
3804 txdr_nfsv4time(&verstime, tl);
3806 NFSEXITCODE2(0, nd);
3810 NFSSOCKADDRFREE(clp->lc_req.nr_nam);
3811 NFSFREEMUTEX(&clp->lc_req.nr_mtx);
3812 free(clp->lc_stateid, M_NFSDCLIENT);
3813 free(clp, M_NFSDCLIENT);
3815 NFSEXITCODE2(error, nd);
3820 * nfsv4 create session service
3823 nfsrvd_createsession(struct nfsrv_descript *nd, __unused int isdgram,
3824 __unused vnode_t vp, NFSPROC_T *p, __unused struct nfsexstuff *exp)
3828 nfsquad_t clientid, confirm;
3829 struct nfsdsession *sep = NULL;
3832 if (nfs_rootfhset == 0 || nfsd_checkrootexp(nd) != 0) {
3833 nd->nd_repstat = NFSERR_WRONGSEC;
3836 sep = (struct nfsdsession *)malloc(sizeof(struct nfsdsession),
3837 M_NFSDSESSION, M_WAITOK | M_ZERO);
3838 sep->sess_refcnt = 1;
3839 mtx_init(&sep->sess_cbsess.nfsess_mtx, "nfscbsession", NULL, MTX_DEF);
3840 NFSM_DISSECT(tl, uint32_t *, NFSX_HYPER + 2 * NFSX_UNSIGNED);
3841 clientid.lval[0] = *tl++;
3842 clientid.lval[1] = *tl++;
3843 confirm.lval[0] = fxdr_unsigned(uint32_t, *tl++);
3844 sep->sess_crflags = fxdr_unsigned(uint32_t, *tl);
3845 /* Persistent sessions and RDMA are not supported. */
3846 sep->sess_crflags &= NFSV4CRSESS_CONNBACKCHAN;
3848 /* Fore channel attributes. */
3849 NFSM_DISSECT(tl, uint32_t *, 7 * NFSX_UNSIGNED);
3850 tl++; /* Header pad always 0. */
3851 sep->sess_maxreq = fxdr_unsigned(uint32_t, *tl++);
3852 sep->sess_maxresp = fxdr_unsigned(uint32_t, *tl++);
3853 sep->sess_maxrespcached = fxdr_unsigned(uint32_t, *tl++);
3854 sep->sess_maxops = fxdr_unsigned(uint32_t, *tl++);
3855 sep->sess_maxslots = fxdr_unsigned(uint32_t, *tl++);
3856 if (sep->sess_maxslots > NFSV4_SLOTS)
3857 sep->sess_maxslots = NFSV4_SLOTS;
3858 rdmacnt = fxdr_unsigned(uint32_t, *tl);
3860 nd->nd_repstat = NFSERR_BADXDR;
3862 } else if (rdmacnt == 1)
3863 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
3865 /* Back channel attributes. */
3866 NFSM_DISSECT(tl, uint32_t *, 7 * NFSX_UNSIGNED);
3867 tl++; /* Header pad always 0. */
3868 sep->sess_cbmaxreq = fxdr_unsigned(uint32_t, *tl++);
3869 sep->sess_cbmaxresp = fxdr_unsigned(uint32_t, *tl++);
3870 sep->sess_cbmaxrespcached = fxdr_unsigned(uint32_t, *tl++);
3871 sep->sess_cbmaxops = fxdr_unsigned(uint32_t, *tl++);
3872 sep->sess_cbsess.nfsess_foreslots = fxdr_unsigned(uint32_t, *tl++);
3873 rdmacnt = fxdr_unsigned(uint32_t, *tl);
3875 nd->nd_repstat = NFSERR_BADXDR;
3877 } else if (rdmacnt == 1)
3878 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
3880 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
3881 sep->sess_cbprogram = fxdr_unsigned(uint32_t, *tl);
3884 * nfsrv_getclient() searches the client list for a match and
3885 * returns the appropriate NFSERR status.
3887 nd->nd_repstat = nfsrv_getclient(clientid, CLOPS_CONFIRM | CLOPS_RENEW,
3888 NULL, sep, confirm, sep->sess_cbprogram, nd, p);
3889 if (nd->nd_repstat == 0) {
3890 NFSM_BUILD(tl, uint32_t *, NFSX_V4SESSIONID);
3891 NFSBCOPY(sep->sess_sessionid, tl, NFSX_V4SESSIONID);
3892 NFSM_BUILD(tl, uint32_t *, 18 * NFSX_UNSIGNED);
3893 *tl++ = txdr_unsigned(confirm.lval[0]); /* sequenceid */
3894 *tl++ = txdr_unsigned(sep->sess_crflags);
3896 /* Fore channel attributes. */
3898 *tl++ = txdr_unsigned(sep->sess_maxreq);
3899 *tl++ = txdr_unsigned(sep->sess_maxresp);
3900 *tl++ = txdr_unsigned(sep->sess_maxrespcached);
3901 *tl++ = txdr_unsigned(sep->sess_maxops);
3902 *tl++ = txdr_unsigned(sep->sess_maxslots);
3903 *tl++ = txdr_unsigned(1);
3904 *tl++ = txdr_unsigned(0); /* No RDMA. */
3906 /* Back channel attributes. */
3908 *tl++ = txdr_unsigned(sep->sess_cbmaxreq);
3909 *tl++ = txdr_unsigned(sep->sess_cbmaxresp);
3910 *tl++ = txdr_unsigned(sep->sess_cbmaxrespcached);
3911 *tl++ = txdr_unsigned(sep->sess_cbmaxops);
3912 *tl++ = txdr_unsigned(sep->sess_cbsess.nfsess_foreslots);
3913 *tl++ = txdr_unsigned(1);
3914 *tl = txdr_unsigned(0); /* No RDMA. */
3917 if (nd->nd_repstat != 0 && sep != NULL)
3918 free(sep, M_NFSDSESSION);
3919 NFSEXITCODE2(error, nd);
3924 * nfsv4 sequence service
3927 nfsrvd_sequence(struct nfsrv_descript *nd, __unused int isdgram,
3928 __unused vnode_t vp, __unused NFSPROC_T *p, __unused struct nfsexstuff *exp)
3931 uint32_t highest_slotid, sequenceid, sflags, target_highest_slotid;
3932 int cache_this, error = 0;
3934 if (nfs_rootfhset == 0 || nfsd_checkrootexp(nd) != 0) {
3935 nd->nd_repstat = NFSERR_WRONGSEC;
3938 NFSM_DISSECT(tl, uint32_t *, NFSX_V4SESSIONID);
3939 NFSBCOPY(tl, nd->nd_sessionid, NFSX_V4SESSIONID);
3940 NFSM_DISSECT(tl, uint32_t *, 4 * NFSX_UNSIGNED);
3941 sequenceid = fxdr_unsigned(uint32_t, *tl++);
3942 nd->nd_slotid = fxdr_unsigned(uint32_t, *tl++);
3943 highest_slotid = fxdr_unsigned(uint32_t, *tl++);
3944 if (*tl == newnfs_true)
3948 nd->nd_flag |= ND_HASSEQUENCE;
3949 nd->nd_repstat = nfsrv_checksequence(nd, sequenceid, &highest_slotid,
3950 &target_highest_slotid, cache_this, &sflags, p);
3951 if (nd->nd_repstat == 0) {
3952 NFSM_BUILD(tl, uint32_t *, NFSX_V4SESSIONID);
3953 NFSBCOPY(nd->nd_sessionid, tl, NFSX_V4SESSIONID);
3954 NFSM_BUILD(tl, uint32_t *, 5 * NFSX_UNSIGNED);
3955 *tl++ = txdr_unsigned(sequenceid);
3956 *tl++ = txdr_unsigned(nd->nd_slotid);
3957 *tl++ = txdr_unsigned(highest_slotid);
3958 *tl++ = txdr_unsigned(target_highest_slotid);
3959 *tl = txdr_unsigned(sflags);
3962 NFSEXITCODE2(error, nd);
3967 * nfsv4 reclaim complete service
3970 nfsrvd_reclaimcomplete(struct nfsrv_descript *nd, __unused int isdgram,
3971 __unused vnode_t vp, __unused NFSPROC_T *p, __unused struct nfsexstuff *exp)
3976 if (nfs_rootfhset == 0 || nfsd_checkrootexp(nd) != 0) {
3977 nd->nd_repstat = NFSERR_WRONGSEC;
3980 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
3981 if (*tl == newnfs_true)
3982 nd->nd_repstat = NFSERR_NOTSUPP;
3984 nd->nd_repstat = nfsrv_checkreclaimcomplete(nd);
3986 NFSEXITCODE2(error, nd);
3991 * nfsv4 destroy clientid service
3994 nfsrvd_destroyclientid(struct nfsrv_descript *nd, __unused int isdgram,
3995 __unused vnode_t vp, NFSPROC_T *p, __unused struct nfsexstuff *exp)
4001 if (nfs_rootfhset == 0 || nfsd_checkrootexp(nd) != 0) {
4002 nd->nd_repstat = NFSERR_WRONGSEC;
4005 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
4006 clientid.lval[0] = *tl++;
4007 clientid.lval[1] = *tl;
4008 nd->nd_repstat = nfsrv_destroyclient(clientid, p);
4010 NFSEXITCODE2(error, nd);
4015 * nfsv4 destroy session service
4018 nfsrvd_destroysession(struct nfsrv_descript *nd, __unused int isdgram,
4019 __unused vnode_t vp, __unused NFSPROC_T *p, __unused struct nfsexstuff *exp)
4021 uint8_t *cp, sessid[NFSX_V4SESSIONID];
4024 if (nfs_rootfhset == 0 || nfsd_checkrootexp(nd) != 0) {
4025 nd->nd_repstat = NFSERR_WRONGSEC;
4028 NFSM_DISSECT(cp, uint8_t *, NFSX_V4SESSIONID);
4029 NFSBCOPY(cp, sessid, NFSX_V4SESSIONID);
4030 nd->nd_repstat = nfsrv_destroysession(nd, sessid);
4032 NFSEXITCODE2(error, nd);
4037 * nfsv4 free stateid service
4040 nfsrvd_freestateid(struct nfsrv_descript *nd, __unused int isdgram,
4041 __unused vnode_t vp, NFSPROC_T *p, __unused struct nfsexstuff *exp)
4044 nfsv4stateid_t stateid;
4047 if (nfs_rootfhset == 0 || nfsd_checkrootexp(nd) != 0) {
4048 nd->nd_repstat = NFSERR_WRONGSEC;
4051 NFSM_DISSECT(tl, uint32_t *, NFSX_STATEID);
4052 stateid.seqid = fxdr_unsigned(uint32_t, *tl++);
4053 NFSBCOPY(tl, stateid.other, NFSX_STATEIDOTHER);
4054 nd->nd_repstat = nfsrv_freestateid(nd, &stateid, p);
4056 NFSEXITCODE2(error, nd);
4061 * nfsv4 service not supported
4064 nfsrvd_notsupp(struct nfsrv_descript *nd, __unused int isdgram,
4065 __unused vnode_t vp, __unused NFSPROC_T *p, __unused struct nfsexstuff *exp)
4068 nd->nd_repstat = NFSERR_NOTSUPP;
4069 NFSEXITCODE2(0, nd);