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);
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);
2419 if (nd->nd_repstat) {
2420 if (nd->nd_repstat == NFSERR_DENIED) {
2421 NFSM_BUILD(tl, u_int32_t *, 7 * NFSX_UNSIGNED);
2422 txdr_hyper(cf.cl_first, tl);
2424 if (cf.cl_end == NFS64BITSSET)
2427 len = cf.cl_end - cf.cl_first;
2428 txdr_hyper(len, tl);
2430 if (cf.cl_flags == NFSLCK_WRITE)
2431 *tl++ = txdr_unsigned(NFSV4LOCKT_WRITE);
2433 *tl++ = txdr_unsigned(NFSV4LOCKT_READ);
2434 *tl++ = stp->ls_stateid.other[0];
2435 *tl = stp->ls_stateid.other[1];
2436 (void) nfsm_strtom(nd, cf.cl_owner, cf.cl_ownerlen);
2441 FREE((caddr_t)stp, M_NFSDSTATE);
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_WDNOTWANTED) != 0) {
2933 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2934 *tl = txdr_unsigned(NFSV4OPEN_NOTWANTED);
2935 } else if ((rflags & NFSV4OPEN_WDSUPPFTYPE) != 0) {
2936 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2937 *tl = txdr_unsigned(NFSV4OPEN_NOTSUPPFTYPE);
2938 } else if ((rflags & NFSV4OPEN_WDCONTENTION) != 0) {
2939 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2940 *tl++ = txdr_unsigned(NFSV4OPEN_CONTENTION);
2942 } else if ((rflags & NFSV4OPEN_WDRESOURCE) != 0) {
2943 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2944 *tl++ = txdr_unsigned(NFSV4OPEN_RESOURCE);
2947 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2948 *tl = txdr_unsigned(NFSV4OPEN_NOTWANTED);
2951 *tl = txdr_unsigned(NFSV4OPEN_DELEGATENONE);
2952 if (rflags & (NFSV4OPEN_READDELEGATE|NFSV4OPEN_WRITEDELEGATE)) {
2953 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID+NFSX_UNSIGNED);
2954 *tl++ = txdr_unsigned(delegstateid.seqid);
2955 NFSBCOPY((caddr_t)delegstateid.other, (caddr_t)tl,
2957 tl += (NFSX_STATEIDOTHER / NFSX_UNSIGNED);
2958 if (rflags & NFSV4OPEN_RECALL)
2962 if (rflags & NFSV4OPEN_WRITEDELEGATE) {
2963 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2964 *tl++ = txdr_unsigned(NFSV4OPEN_LIMITSIZE);
2965 txdr_hyper(nva.na_size, tl);
2967 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2968 *tl++ = txdr_unsigned(NFSV4ACE_ALLOWEDTYPE);
2969 *tl++ = txdr_unsigned(0x0);
2970 acemask = NFSV4ACE_ALLFILESMASK;
2971 if (nva.na_mode & S_IRUSR)
2972 acemask |= NFSV4ACE_READMASK;
2973 if (nva.na_mode & S_IWUSR)
2974 acemask |= NFSV4ACE_WRITEMASK;
2975 if (nva.na_mode & S_IXUSR)
2976 acemask |= NFSV4ACE_EXECUTEMASK;
2977 *tl = txdr_unsigned(acemask);
2978 (void) nfsm_strtom(nd, "OWNER@", 6);
2986 #ifdef NFS4_ACL_EXTATTR_NAME
2989 NFSEXITCODE2(0, nd);
2993 #ifdef NFS4_ACL_EXTATTR_NAME
2997 FREE((caddr_t)stp, M_NFSDSTATE);
2998 NFSEXITCODE2(error, nd);
3003 * nfsv4 close service
3006 nfsrvd_close(struct nfsrv_descript *nd, __unused int isdgram,
3007 vnode_t vp, NFSPROC_T *p, __unused struct nfsexstuff *exp)
3010 struct nfsstate st, *stp = &st;
3012 nfsv4stateid_t stateid;
3015 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED + NFSX_STATEID);
3016 stp->ls_seq = fxdr_unsigned(u_int32_t, *tl++);
3017 stp->ls_ownerlen = 0;
3018 stp->ls_op = nd->nd_rp;
3019 stp->ls_uid = nd->nd_cred->cr_uid;
3020 stp->ls_stateid.seqid = fxdr_unsigned(u_int32_t, *tl++);
3021 NFSBCOPY((caddr_t)tl, (caddr_t)stp->ls_stateid.other,
3023 stp->ls_flags = NFSLCK_CLOSE;
3024 clientid.lval[0] = stp->ls_stateid.other[0];
3025 clientid.lval[1] = stp->ls_stateid.other[1];
3026 if ((nd->nd_flag & ND_IMPLIEDCLID) != 0) {
3027 if ((nd->nd_flag & ND_NFSV41) != 0)
3028 clientid.qval = nd->nd_clientid.qval;
3029 else if (nd->nd_clientid.qval != clientid.qval)
3030 printf("EEK8 multiple clids\n");
3032 if ((nd->nd_flag & ND_NFSV41) != 0)
3033 printf("EEK! no clientid from session\n");
3034 nd->nd_flag |= ND_IMPLIEDCLID;
3035 nd->nd_clientid.qval = clientid.qval;
3037 nd->nd_repstat = nfsrv_openupdate(vp, stp, clientid, &stateid, nd, p);
3039 if (!nd->nd_repstat) {
3040 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID);
3041 *tl++ = txdr_unsigned(stateid.seqid);
3042 NFSBCOPY((caddr_t)stateid.other,(caddr_t)tl,NFSX_STATEIDOTHER);
3044 NFSEXITCODE2(0, nd);
3048 NFSEXITCODE2(error, nd);
3053 * nfsv4 delegpurge service
3056 nfsrvd_delegpurge(struct nfsrv_descript *nd, __unused int isdgram,
3057 __unused vnode_t vp, __unused NFSPROC_T *p, __unused struct nfsexstuff *exp)
3063 if (nfs_rootfhset == 0 || nfsd_checkrootexp(nd) != 0) {
3064 nd->nd_repstat = NFSERR_WRONGSEC;
3067 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
3068 clientid.lval[0] = *tl++;
3069 clientid.lval[1] = *tl;
3070 if ((nd->nd_flag & ND_IMPLIEDCLID) != 0) {
3071 if ((nd->nd_flag & ND_NFSV41) != 0)
3072 clientid.qval = nd->nd_clientid.qval;
3073 else if (nd->nd_clientid.qval != clientid.qval)
3074 printf("EEK9 multiple clids\n");
3076 if ((nd->nd_flag & ND_NFSV41) != 0)
3077 printf("EEK! no clientid from session\n");
3078 nd->nd_flag |= ND_IMPLIEDCLID;
3079 nd->nd_clientid.qval = clientid.qval;
3081 nd->nd_repstat = nfsrv_delegupdate(nd, clientid, NULL, NULL,
3082 NFSV4OP_DELEGPURGE, nd->nd_cred, p);
3084 NFSEXITCODE2(error, nd);
3089 * nfsv4 delegreturn service
3092 nfsrvd_delegreturn(struct nfsrv_descript *nd, __unused int isdgram,
3093 vnode_t vp, NFSPROC_T *p, __unused struct nfsexstuff *exp)
3097 nfsv4stateid_t stateid;
3100 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID);
3101 stateid.seqid = fxdr_unsigned(u_int32_t, *tl++);
3102 NFSBCOPY((caddr_t)tl, (caddr_t)stateid.other, NFSX_STATEIDOTHER);
3103 clientid.lval[0] = stateid.other[0];
3104 clientid.lval[1] = stateid.other[1];
3105 if ((nd->nd_flag & ND_IMPLIEDCLID) != 0) {
3106 if ((nd->nd_flag & ND_NFSV41) != 0)
3107 clientid.qval = nd->nd_clientid.qval;
3108 else if (nd->nd_clientid.qval != clientid.qval)
3109 printf("EEK10 multiple clids\n");
3111 if ((nd->nd_flag & ND_NFSV41) != 0)
3112 printf("EEK! no clientid from session\n");
3113 nd->nd_flag |= ND_IMPLIEDCLID;
3114 nd->nd_clientid.qval = clientid.qval;
3116 nd->nd_repstat = nfsrv_delegupdate(nd, clientid, &stateid, vp,
3117 NFSV4OP_DELEGRETURN, nd->nd_cred, p);
3120 NFSEXITCODE2(error, nd);
3125 * nfsv4 get file handle service
3128 nfsrvd_getfh(struct nfsrv_descript *nd, __unused int isdgram,
3129 vnode_t vp, NFSPROC_T *p, __unused struct nfsexstuff *exp)
3133 nd->nd_repstat = nfsvno_getfh(vp, &fh, p);
3135 if (!nd->nd_repstat)
3136 (void) nfsm_fhtom(nd, (u_int8_t *)&fh, 0, 0);
3137 NFSEXITCODE2(0, nd);
3142 * nfsv4 open confirm service
3145 nfsrvd_openconfirm(struct nfsrv_descript *nd, __unused int isdgram,
3146 vnode_t vp, NFSPROC_T *p, __unused struct nfsexstuff *exp)
3149 struct nfsstate st, *stp = &st;
3151 nfsv4stateid_t stateid;
3154 if ((nd->nd_flag & ND_NFSV41) != 0) {
3155 nd->nd_repstat = NFSERR_NOTSUPP;
3158 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID + NFSX_UNSIGNED);
3159 stp->ls_ownerlen = 0;
3160 stp->ls_op = nd->nd_rp;
3161 stp->ls_uid = nd->nd_cred->cr_uid;
3162 stp->ls_stateid.seqid = fxdr_unsigned(u_int32_t, *tl++);
3163 NFSBCOPY((caddr_t)tl, (caddr_t)stp->ls_stateid.other,
3165 tl += (NFSX_STATEIDOTHER / NFSX_UNSIGNED);
3166 stp->ls_seq = fxdr_unsigned(u_int32_t, *tl);
3167 stp->ls_flags = NFSLCK_CONFIRM;
3168 clientid.lval[0] = stp->ls_stateid.other[0];
3169 clientid.lval[1] = stp->ls_stateid.other[1];
3170 if ((nd->nd_flag & ND_IMPLIEDCLID) != 0) {
3171 if ((nd->nd_flag & ND_NFSV41) != 0)
3172 clientid.qval = nd->nd_clientid.qval;
3173 else if (nd->nd_clientid.qval != clientid.qval)
3174 printf("EEK11 multiple clids\n");
3176 if ((nd->nd_flag & ND_NFSV41) != 0)
3177 printf("EEK! no clientid from session\n");
3178 nd->nd_flag |= ND_IMPLIEDCLID;
3179 nd->nd_clientid.qval = clientid.qval;
3181 nd->nd_repstat = nfsrv_openupdate(vp, stp, clientid, &stateid, nd, p);
3182 if (!nd->nd_repstat) {
3183 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID);
3184 *tl++ = txdr_unsigned(stateid.seqid);
3185 NFSBCOPY((caddr_t)stateid.other,(caddr_t)tl,NFSX_STATEIDOTHER);
3189 NFSEXITCODE2(error, nd);
3194 * nfsv4 open downgrade service
3197 nfsrvd_opendowngrade(struct nfsrv_descript *nd, __unused int isdgram,
3198 vnode_t vp, NFSPROC_T *p, __unused struct nfsexstuff *exp)
3202 struct nfsstate st, *stp = &st;
3204 nfsv4stateid_t stateid;
3207 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID + 3 * NFSX_UNSIGNED);
3208 stp->ls_ownerlen = 0;
3209 stp->ls_op = nd->nd_rp;
3210 stp->ls_uid = nd->nd_cred->cr_uid;
3211 stp->ls_stateid.seqid = fxdr_unsigned(u_int32_t, *tl++);
3212 NFSBCOPY((caddr_t)tl, (caddr_t)stp->ls_stateid.other,
3214 tl += (NFSX_STATEIDOTHER / NFSX_UNSIGNED);
3215 stp->ls_seq = fxdr_unsigned(u_int32_t, *tl++);
3216 i = fxdr_unsigned(int, *tl++);
3218 case NFSV4OPEN_ACCESSREAD:
3219 stp->ls_flags = (NFSLCK_READACCESS | NFSLCK_DOWNGRADE);
3221 case NFSV4OPEN_ACCESSWRITE:
3222 stp->ls_flags = (NFSLCK_WRITEACCESS | NFSLCK_DOWNGRADE);
3224 case NFSV4OPEN_ACCESSBOTH:
3225 stp->ls_flags = (NFSLCK_READACCESS | NFSLCK_WRITEACCESS |
3229 nd->nd_repstat = NFSERR_BADXDR;
3231 i = fxdr_unsigned(int, *tl);
3233 case NFSV4OPEN_DENYNONE:
3235 case NFSV4OPEN_DENYREAD:
3236 stp->ls_flags |= NFSLCK_READDENY;
3238 case NFSV4OPEN_DENYWRITE:
3239 stp->ls_flags |= NFSLCK_WRITEDENY;
3241 case NFSV4OPEN_DENYBOTH:
3242 stp->ls_flags |= (NFSLCK_READDENY | NFSLCK_WRITEDENY);
3245 nd->nd_repstat = NFSERR_BADXDR;
3248 clientid.lval[0] = stp->ls_stateid.other[0];
3249 clientid.lval[1] = stp->ls_stateid.other[1];
3250 if ((nd->nd_flag & ND_IMPLIEDCLID) != 0) {
3251 if ((nd->nd_flag & ND_NFSV41) != 0)
3252 clientid.qval = nd->nd_clientid.qval;
3253 else if (nd->nd_clientid.qval != clientid.qval)
3254 printf("EEK12 multiple clids\n");
3256 if ((nd->nd_flag & ND_NFSV41) != 0)
3257 printf("EEK! no clientid from session\n");
3258 nd->nd_flag |= ND_IMPLIEDCLID;
3259 nd->nd_clientid.qval = clientid.qval;
3261 if (!nd->nd_repstat)
3262 nd->nd_repstat = nfsrv_openupdate(vp, stp, clientid, &stateid,
3264 if (!nd->nd_repstat) {
3265 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID);
3266 *tl++ = txdr_unsigned(stateid.seqid);
3267 NFSBCOPY((caddr_t)stateid.other,(caddr_t)tl,NFSX_STATEIDOTHER);
3271 NFSEXITCODE2(error, nd);
3276 * nfsv4 renew lease service
3279 nfsrvd_renew(struct nfsrv_descript *nd, __unused int isdgram,
3280 __unused vnode_t vp, NFSPROC_T *p, __unused struct nfsexstuff *exp)
3286 if ((nd->nd_flag & ND_NFSV41) != 0) {
3287 nd->nd_repstat = NFSERR_NOTSUPP;
3290 if (nfs_rootfhset == 0 || nfsd_checkrootexp(nd) != 0) {
3291 nd->nd_repstat = NFSERR_WRONGSEC;
3294 NFSM_DISSECT(tl, u_int32_t *, NFSX_HYPER);
3295 clientid.lval[0] = *tl++;
3296 clientid.lval[1] = *tl;
3297 if ((nd->nd_flag & ND_IMPLIEDCLID) != 0) {
3298 if ((nd->nd_flag & ND_NFSV41) != 0)
3299 clientid.qval = nd->nd_clientid.qval;
3300 else if (nd->nd_clientid.qval != clientid.qval)
3301 printf("EEK13 multiple clids\n");
3303 if ((nd->nd_flag & ND_NFSV41) != 0)
3304 printf("EEK! no clientid from session\n");
3305 nd->nd_flag |= ND_IMPLIEDCLID;
3306 nd->nd_clientid.qval = clientid.qval;
3308 nd->nd_repstat = nfsrv_getclient(clientid, (CLOPS_RENEWOP|CLOPS_RENEW),
3309 NULL, NULL, (nfsquad_t)((u_quad_t)0), 0, nd, p);
3311 NFSEXITCODE2(error, nd);
3316 * nfsv4 security info service
3319 nfsrvd_secinfo(struct nfsrv_descript *nd, int isdgram,
3320 vnode_t dp, NFSPROC_T *p, struct nfsexstuff *exp)
3324 struct nameidata named;
3325 vnode_t dirp = NULL, vp;
3327 struct nfsexstuff retnes;
3329 int error = 0, savflag, i;
3334 * All this just to get the export flags for the name.
3336 NFSNAMEICNDSET(&named.ni_cnd, nd->nd_cred, LOOKUP,
3337 LOCKLEAF | SAVESTART);
3338 nfsvno_setpathbuf(&named, &bufp, &hashp);
3339 error = nfsrv_parsename(nd, bufp, hashp, &named.ni_pathlen);
3342 nfsvno_relpathbuf(&named);
3345 if (!nd->nd_repstat) {
3346 nd->nd_repstat = nfsvno_namei(nd, &named, dp, 1, exp, p, &dirp);
3349 nfsvno_relpathbuf(&named);
3355 vrele(named.ni_startdir);
3356 nfsvno_relpathbuf(&named);
3357 fh.nfsrvfh_len = NFSX_MYFH;
3359 nd->nd_repstat = nfsvno_getfh(vp, (fhandle_t *)fh.nfsrvfh_data, p);
3361 savflag = nd->nd_flag;
3362 if (!nd->nd_repstat) {
3363 nfsd_fhtovp(nd, &fh, LK_SHARED, &vp, &retnes, NULL, 0, p);
3367 nd->nd_flag = savflag;
3372 * Finally have the export flags for name, so we can create
3373 * the security info.
3376 NFSM_BUILD(sizp, u_int32_t *, NFSX_UNSIGNED);
3377 for (i = 0; i < retnes.nes_numsecflavor; i++) {
3378 if (retnes.nes_secflavors[i] == AUTH_SYS) {
3379 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
3380 *tl = txdr_unsigned(RPCAUTH_UNIX);
3382 } else if (retnes.nes_secflavors[i] == RPCSEC_GSS_KRB5) {
3383 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
3384 *tl++ = txdr_unsigned(RPCAUTH_GSS);
3385 (void) nfsm_strtom(nd, nfsgss_mechlist[KERBV_MECH].str,
3386 nfsgss_mechlist[KERBV_MECH].len);
3387 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
3388 *tl++ = txdr_unsigned(GSS_KERBV_QOP);
3389 *tl = txdr_unsigned(RPCAUTHGSS_SVCNONE);
3391 } else if (retnes.nes_secflavors[i] == RPCSEC_GSS_KRB5I) {
3392 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
3393 *tl++ = txdr_unsigned(RPCAUTH_GSS);
3394 (void) nfsm_strtom(nd, nfsgss_mechlist[KERBV_MECH].str,
3395 nfsgss_mechlist[KERBV_MECH].len);
3396 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
3397 *tl++ = txdr_unsigned(GSS_KERBV_QOP);
3398 *tl = txdr_unsigned(RPCAUTHGSS_SVCINTEGRITY);
3400 } else if (retnes.nes_secflavors[i] == RPCSEC_GSS_KRB5P) {
3401 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
3402 *tl++ = txdr_unsigned(RPCAUTH_GSS);
3403 (void) nfsm_strtom(nd, nfsgss_mechlist[KERBV_MECH].str,
3404 nfsgss_mechlist[KERBV_MECH].len);
3405 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
3406 *tl++ = txdr_unsigned(GSS_KERBV_QOP);
3407 *tl = txdr_unsigned(RPCAUTHGSS_SVCPRIVACY);
3411 *sizp = txdr_unsigned(len);
3414 NFSEXITCODE2(error, nd);
3419 * nfsv4 set client id service
3422 nfsrvd_setclientid(struct nfsrv_descript *nd, __unused int isdgram,
3423 __unused vnode_t vp, NFSPROC_T *p, __unused struct nfsexstuff *exp)
3427 int error = 0, idlen;
3428 struct nfsclient *clp = NULL;
3429 struct sockaddr_in *rad;
3430 u_char *verf, *ucp, *ucp2, addrbuf[24];
3431 nfsquad_t clientid, confirm;
3433 if ((nd->nd_flag & ND_NFSV41) != 0) {
3434 nd->nd_repstat = NFSERR_NOTSUPP;
3437 if (nfs_rootfhset == 0 || nfsd_checkrootexp(nd) != 0) {
3438 nd->nd_repstat = NFSERR_WRONGSEC;
3441 NFSM_DISSECT(tl, u_int32_t *, NFSX_VERF + NFSX_UNSIGNED);
3442 verf = (u_char *)tl;
3443 tl += (NFSX_VERF / NFSX_UNSIGNED);
3444 i = fxdr_unsigned(int, *tl);
3445 if (i > NFSV4_OPAQUELIMIT || i <= 0) {
3446 nd->nd_repstat = NFSERR_BADXDR;
3450 if (nd->nd_flag & ND_GSS)
3451 i += nd->nd_princlen;
3452 clp = malloc(sizeof(struct nfsclient) + i, M_NFSDCLIENT, M_WAITOK |
3454 clp->lc_stateid = malloc(sizeof(struct nfsstatehead) *
3455 nfsrv_statehashsize, M_NFSDCLIENT, M_WAITOK);
3456 NFSINITSOCKMUTEX(&clp->lc_req.nr_mtx);
3457 NFSSOCKADDRALLOC(clp->lc_req.nr_nam);
3458 NFSSOCKADDRSIZE(clp->lc_req.nr_nam, sizeof (struct sockaddr_in));
3459 clp->lc_req.nr_cred = NULL;
3460 NFSBCOPY(verf, clp->lc_verf, NFSX_VERF);
3461 clp->lc_idlen = idlen;
3462 error = nfsrv_mtostr(nd, clp->lc_id, idlen);
3465 if (nd->nd_flag & ND_GSS) {
3466 clp->lc_flags = LCL_GSS;
3467 if (nd->nd_flag & ND_GSSINTEGRITY)
3468 clp->lc_flags |= LCL_GSSINTEGRITY;
3469 else if (nd->nd_flag & ND_GSSPRIVACY)
3470 clp->lc_flags |= LCL_GSSPRIVACY;
3474 if ((nd->nd_flag & ND_GSS) && nd->nd_princlen > 0) {
3475 clp->lc_flags |= LCL_NAME;
3476 clp->lc_namelen = nd->nd_princlen;
3477 clp->lc_name = &clp->lc_id[idlen];
3478 NFSBCOPY(nd->nd_principal, clp->lc_name, clp->lc_namelen);
3480 clp->lc_uid = nd->nd_cred->cr_uid;
3481 clp->lc_gid = nd->nd_cred->cr_gid;
3483 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3484 clp->lc_program = fxdr_unsigned(u_int32_t, *tl);
3485 error = nfsrv_getclientipaddr(nd, clp);
3488 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3489 clp->lc_callback = fxdr_unsigned(u_int32_t, *tl);
3492 * nfsrv_setclient() does the actual work of adding it to the
3493 * client list. If there is no error, the structure has been
3494 * linked into the client list and clp should no longer be used
3495 * here. When an error is returned, it has not been linked in,
3496 * so it should be free'd.
3498 nd->nd_repstat = nfsrv_setclient(nd, &clp, &clientid, &confirm, p);
3499 if (nd->nd_repstat == NFSERR_CLIDINUSE) {
3500 if (clp->lc_flags & LCL_TCPCALLBACK)
3501 (void) nfsm_strtom(nd, "tcp", 3);
3503 (void) nfsm_strtom(nd, "udp", 3);
3504 rad = NFSSOCKADDR(clp->lc_req.nr_nam, struct sockaddr_in *);
3505 ucp = (u_char *)&rad->sin_addr.s_addr;
3506 ucp2 = (u_char *)&rad->sin_port;
3507 sprintf(addrbuf, "%d.%d.%d.%d.%d.%d", ucp[0] & 0xff,
3508 ucp[1] & 0xff, ucp[2] & 0xff, ucp[3] & 0xff,
3509 ucp2[0] & 0xff, ucp2[1] & 0xff);
3510 (void) nfsm_strtom(nd, addrbuf, strlen(addrbuf));
3513 NFSSOCKADDRFREE(clp->lc_req.nr_nam);
3514 NFSFREEMUTEX(&clp->lc_req.nr_mtx);
3515 free(clp->lc_stateid, M_NFSDCLIENT);
3516 free(clp, M_NFSDCLIENT);
3518 if (!nd->nd_repstat) {
3519 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_HYPER);
3520 *tl++ = clientid.lval[0];
3521 *tl++ = clientid.lval[1];
3522 *tl++ = confirm.lval[0];
3523 *tl = confirm.lval[1];
3527 NFSEXITCODE2(0, nd);
3531 NFSSOCKADDRFREE(clp->lc_req.nr_nam);
3532 NFSFREEMUTEX(&clp->lc_req.nr_mtx);
3533 free(clp->lc_stateid, M_NFSDCLIENT);
3534 free(clp, M_NFSDCLIENT);
3536 NFSEXITCODE2(error, nd);
3541 * nfsv4 set client id confirm service
3544 nfsrvd_setclientidcfrm(struct nfsrv_descript *nd,
3545 __unused int isdgram, __unused vnode_t vp, NFSPROC_T *p,
3546 __unused struct nfsexstuff *exp)
3550 nfsquad_t clientid, confirm;
3552 if ((nd->nd_flag & ND_NFSV41) != 0) {
3553 nd->nd_repstat = NFSERR_NOTSUPP;
3556 if (nfs_rootfhset == 0 || nfsd_checkrootexp(nd) != 0) {
3557 nd->nd_repstat = NFSERR_WRONGSEC;
3560 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_HYPER);
3561 clientid.lval[0] = *tl++;
3562 clientid.lval[1] = *tl++;
3563 confirm.lval[0] = *tl++;
3564 confirm.lval[1] = *tl;
3567 * nfsrv_getclient() searches the client list for a match and
3568 * returns the appropriate NFSERR status.
3570 nd->nd_repstat = nfsrv_getclient(clientid, (CLOPS_CONFIRM|CLOPS_RENEW),
3571 NULL, NULL, confirm, 0, nd, p);
3573 NFSEXITCODE2(error, nd);
3578 * nfsv4 verify service
3581 nfsrvd_verify(struct nfsrv_descript *nd, int isdgram,
3582 vnode_t vp, NFSPROC_T *p, __unused struct nfsexstuff *exp)
3584 int error = 0, ret, fhsize = NFSX_MYFH;
3585 struct nfsvattr nva;
3587 struct nfsfsinfo fs;
3590 nd->nd_repstat = nfsvno_getattr(vp, &nva, nd->nd_cred, p, 1);
3591 if (!nd->nd_repstat)
3592 nd->nd_repstat = nfsvno_statfs(vp, &sf);
3593 if (!nd->nd_repstat)
3594 nd->nd_repstat = nfsvno_getfh(vp, &fh, p);
3595 if (!nd->nd_repstat) {
3596 nfsvno_getfs(&fs, isdgram);
3597 error = nfsv4_loadattr(nd, vp, &nva, NULL, &fh, fhsize, NULL,
3598 &sf, NULL, &fs, NULL, 1, &ret, NULL, NULL, p, nd->nd_cred);
3600 if (nd->nd_procnum == NFSV4OP_NVERIFY) {
3602 nd->nd_repstat = NFSERR_SAME;
3603 else if (ret != NFSERR_NOTSAME)
3604 nd->nd_repstat = ret;
3606 nd->nd_repstat = ret;
3610 NFSEXITCODE2(error, nd);
3618 nfsrvd_openattr(struct nfsrv_descript *nd, __unused int isdgram,
3619 vnode_t dp, __unused vnode_t *vpp, __unused fhandle_t *fhp,
3620 __unused NFSPROC_T *p, __unused struct nfsexstuff *exp)
3623 int error = 0, createdir;
3625 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3626 createdir = fxdr_unsigned(int, *tl);
3627 nd->nd_repstat = NFSERR_NOTSUPP;
3630 NFSEXITCODE2(error, nd);
3635 * nfsv4 release lock owner service
3638 nfsrvd_releaselckown(struct nfsrv_descript *nd, __unused int isdgram,
3639 __unused vnode_t vp, NFSPROC_T *p, __unused struct nfsexstuff *exp)
3642 struct nfsstate *stp = NULL;
3646 if ((nd->nd_flag & ND_NFSV41) != 0) {
3647 nd->nd_repstat = NFSERR_NOTSUPP;
3650 if (nfs_rootfhset == 0 || nfsd_checkrootexp(nd) != 0) {
3651 nd->nd_repstat = NFSERR_WRONGSEC;
3654 NFSM_DISSECT(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
3655 len = fxdr_unsigned(int, *(tl + 2));
3656 if (len <= 0 || len > NFSV4_OPAQUELIMIT) {
3657 nd->nd_repstat = NFSERR_BADXDR;
3660 MALLOC(stp, struct nfsstate *, sizeof (struct nfsstate) + len,
3661 M_NFSDSTATE, M_WAITOK);
3662 stp->ls_ownerlen = len;
3664 stp->ls_flags = NFSLCK_RELEASE;
3665 stp->ls_uid = nd->nd_cred->cr_uid;
3666 clientid.lval[0] = *tl++;
3667 clientid.lval[1] = *tl;
3668 if ((nd->nd_flag & ND_IMPLIEDCLID) != 0) {
3669 if ((nd->nd_flag & ND_NFSV41) != 0)
3670 clientid.qval = nd->nd_clientid.qval;
3671 else if (nd->nd_clientid.qval != clientid.qval)
3672 printf("EEK14 multiple clids\n");
3674 if ((nd->nd_flag & ND_NFSV41) != 0)
3675 printf("EEK! no clientid from session\n");
3676 nd->nd_flag |= ND_IMPLIEDCLID;
3677 nd->nd_clientid.qval = clientid.qval;
3679 error = nfsrv_mtostr(nd, stp->ls_owner, len);
3682 nd->nd_repstat = nfsrv_releaselckown(stp, clientid, p);
3683 FREE((caddr_t)stp, M_NFSDSTATE);
3685 NFSEXITCODE2(0, nd);
3689 free((caddr_t)stp, M_NFSDSTATE);
3690 NFSEXITCODE2(error, nd);
3695 * nfsv4 exchange_id service
3698 nfsrvd_exchangeid(struct nfsrv_descript *nd, __unused int isdgram,
3699 __unused vnode_t vp, NFSPROC_T *p, __unused struct nfsexstuff *exp)
3702 int error = 0, i, idlen;
3703 struct nfsclient *clp = NULL;
3704 nfsquad_t clientid, confirm;
3706 uint32_t sp4type, v41flags;
3707 uint64_t owner_minor;
3708 struct timespec verstime;
3709 struct sockaddr_in *sad, *rad;
3711 if (nfs_rootfhset == 0 || nfsd_checkrootexp(nd) != 0) {
3712 nd->nd_repstat = NFSERR_WRONGSEC;
3715 NFSM_DISSECT(tl, u_int32_t *, NFSX_VERF + NFSX_UNSIGNED);
3716 verf = (uint8_t *)tl;
3717 tl += (NFSX_VERF / NFSX_UNSIGNED);
3718 i = fxdr_unsigned(int, *tl);
3719 if (i > NFSV4_OPAQUELIMIT || i <= 0) {
3720 nd->nd_repstat = NFSERR_BADXDR;
3724 if (nd->nd_flag & ND_GSS)
3725 i += nd->nd_princlen;
3726 clp = malloc(sizeof(struct nfsclient) + i, M_NFSDCLIENT, M_WAITOK |
3728 clp->lc_stateid = malloc(sizeof(struct nfsstatehead) *
3729 nfsrv_statehashsize, M_NFSDCLIENT, M_WAITOK);
3730 NFSINITSOCKMUTEX(&clp->lc_req.nr_mtx);
3731 NFSSOCKADDRALLOC(clp->lc_req.nr_nam);
3732 NFSSOCKADDRSIZE(clp->lc_req.nr_nam, sizeof (struct sockaddr_in));
3733 sad = NFSSOCKADDR(nd->nd_nam, struct sockaddr_in *);
3734 rad = NFSSOCKADDR(clp->lc_req.nr_nam, struct sockaddr_in *);
3735 rad->sin_family = AF_INET;
3736 rad->sin_addr.s_addr = 0;
3738 if (sad->sin_family == AF_INET)
3739 rad->sin_addr.s_addr = sad->sin_addr.s_addr;
3740 clp->lc_req.nr_cred = NULL;
3741 NFSBCOPY(verf, clp->lc_verf, NFSX_VERF);
3742 clp->lc_idlen = idlen;
3743 error = nfsrv_mtostr(nd, clp->lc_id, idlen);
3746 if ((nd->nd_flag & ND_GSS) != 0) {
3747 clp->lc_flags = LCL_GSS | LCL_NFSV41;
3748 if ((nd->nd_flag & ND_GSSINTEGRITY) != 0)
3749 clp->lc_flags |= LCL_GSSINTEGRITY;
3750 else if ((nd->nd_flag & ND_GSSPRIVACY) != 0)
3751 clp->lc_flags |= LCL_GSSPRIVACY;
3753 clp->lc_flags = LCL_NFSV41;
3754 if ((nd->nd_flag & ND_GSS) != 0 && nd->nd_princlen > 0) {
3755 clp->lc_flags |= LCL_NAME;
3756 clp->lc_namelen = nd->nd_princlen;
3757 clp->lc_name = &clp->lc_id[idlen];
3758 NFSBCOPY(nd->nd_principal, clp->lc_name, clp->lc_namelen);
3760 clp->lc_uid = nd->nd_cred->cr_uid;
3761 clp->lc_gid = nd->nd_cred->cr_gid;
3763 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
3764 v41flags = fxdr_unsigned(uint32_t, *tl++);
3765 if ((v41flags & ~(NFSV4EXCH_SUPPMOVEDREFER | NFSV4EXCH_SUPPMOVEDMIGR |
3766 NFSV4EXCH_BINDPRINCSTATEID | NFSV4EXCH_MASKPNFS |
3767 NFSV4EXCH_UPDCONFIRMEDRECA)) != 0) {
3768 nd->nd_repstat = NFSERR_INVAL;
3771 if ((v41flags & NFSV4EXCH_UPDCONFIRMEDRECA) != 0)
3772 confirm.lval[1] = 1;
3774 confirm.lval[1] = 0;
3775 v41flags = NFSV4EXCH_USENONPNFS;
3776 sp4type = fxdr_unsigned(uint32_t, *tl);
3777 if (sp4type != NFSV4EXCH_SP4NONE) {
3778 nd->nd_repstat = NFSERR_NOTSUPP;
3783 * nfsrv_setclient() does the actual work of adding it to the
3784 * client list. If there is no error, the structure has been
3785 * linked into the client list and clp should no longer be used
3786 * here. When an error is returned, it has not been linked in,
3787 * so it should be free'd.
3789 nd->nd_repstat = nfsrv_setclient(nd, &clp, &clientid, &confirm, p);
3791 NFSSOCKADDRFREE(clp->lc_req.nr_nam);
3792 NFSFREEMUTEX(&clp->lc_req.nr_mtx);
3793 free(clp->lc_stateid, M_NFSDCLIENT);
3794 free(clp, M_NFSDCLIENT);
3796 if (nd->nd_repstat == 0) {
3797 if (confirm.lval[1] != 0)
3798 v41flags |= NFSV4EXCH_CONFIRMEDR;
3799 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_HYPER + 3 * NFSX_UNSIGNED);
3800 *tl++ = clientid.lval[0]; /* ClientID */
3801 *tl++ = clientid.lval[1];
3802 *tl++ = txdr_unsigned(confirm.lval[0]); /* SequenceID */
3803 *tl++ = txdr_unsigned(v41flags); /* Exch flags */
3804 *tl++ = txdr_unsigned(NFSV4EXCH_SP4NONE); /* No SSV */
3805 owner_minor = 0; /* Owner */
3806 txdr_hyper(owner_minor, tl); /* Minor */
3807 (void)nfsm_strtom(nd, nd->nd_cred->cr_prison->pr_hostuuid,
3808 strlen(nd->nd_cred->cr_prison->pr_hostuuid)); /* Major */
3809 (void)nfsm_strtom(nd, nd->nd_cred->cr_prison->pr_hostuuid,
3810 strlen(nd->nd_cred->cr_prison->pr_hostuuid)); /* Scope */
3811 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
3812 *tl = txdr_unsigned(1);
3813 (void)nfsm_strtom(nd, "freebsd.org", strlen("freebsd.org"));
3814 (void)nfsm_strtom(nd, version, strlen(version));
3815 NFSM_BUILD(tl, uint32_t *, NFSX_V4TIME);
3816 verstime.tv_sec = 1293840000; /* Jan 1, 2011 */
3817 verstime.tv_nsec = 0;
3818 txdr_nfsv4time(&verstime, tl);
3820 NFSEXITCODE2(0, nd);
3824 NFSSOCKADDRFREE(clp->lc_req.nr_nam);
3825 NFSFREEMUTEX(&clp->lc_req.nr_mtx);
3826 free(clp->lc_stateid, M_NFSDCLIENT);
3827 free(clp, M_NFSDCLIENT);
3829 NFSEXITCODE2(error, nd);
3834 * nfsv4 create session service
3837 nfsrvd_createsession(struct nfsrv_descript *nd, __unused int isdgram,
3838 __unused vnode_t vp, NFSPROC_T *p, __unused struct nfsexstuff *exp)
3842 nfsquad_t clientid, confirm;
3843 struct nfsdsession *sep = NULL;
3846 if (nfs_rootfhset == 0 || nfsd_checkrootexp(nd) != 0) {
3847 nd->nd_repstat = NFSERR_WRONGSEC;
3850 sep = (struct nfsdsession *)malloc(sizeof(struct nfsdsession),
3851 M_NFSDSESSION, M_WAITOK | M_ZERO);
3852 sep->sess_refcnt = 1;
3853 mtx_init(&sep->sess_cbsess.nfsess_mtx, "nfscbsession", NULL, MTX_DEF);
3854 NFSM_DISSECT(tl, uint32_t *, NFSX_HYPER + 2 * NFSX_UNSIGNED);
3855 clientid.lval[0] = *tl++;
3856 clientid.lval[1] = *tl++;
3857 confirm.lval[0] = fxdr_unsigned(uint32_t, *tl++);
3858 sep->sess_crflags = fxdr_unsigned(uint32_t, *tl);
3859 /* Persistent sessions and RDMA are not supported. */
3860 sep->sess_crflags &= NFSV4CRSESS_CONNBACKCHAN;
3862 /* Fore channel attributes. */
3863 NFSM_DISSECT(tl, uint32_t *, 7 * NFSX_UNSIGNED);
3864 tl++; /* Header pad always 0. */
3865 sep->sess_maxreq = fxdr_unsigned(uint32_t, *tl++);
3866 sep->sess_maxresp = fxdr_unsigned(uint32_t, *tl++);
3867 sep->sess_maxrespcached = fxdr_unsigned(uint32_t, *tl++);
3868 sep->sess_maxops = fxdr_unsigned(uint32_t, *tl++);
3869 sep->sess_maxslots = fxdr_unsigned(uint32_t, *tl++);
3870 if (sep->sess_maxslots > NFSV4_SLOTS)
3871 sep->sess_maxslots = NFSV4_SLOTS;
3872 rdmacnt = fxdr_unsigned(uint32_t, *tl);
3874 nd->nd_repstat = NFSERR_BADXDR;
3876 } else if (rdmacnt == 1)
3877 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
3879 /* Back channel attributes. */
3880 NFSM_DISSECT(tl, uint32_t *, 7 * NFSX_UNSIGNED);
3881 tl++; /* Header pad always 0. */
3882 sep->sess_cbmaxreq = fxdr_unsigned(uint32_t, *tl++);
3883 sep->sess_cbmaxresp = fxdr_unsigned(uint32_t, *tl++);
3884 sep->sess_cbmaxrespcached = fxdr_unsigned(uint32_t, *tl++);
3885 sep->sess_cbmaxops = fxdr_unsigned(uint32_t, *tl++);
3886 sep->sess_cbsess.nfsess_foreslots = fxdr_unsigned(uint32_t, *tl++);
3887 rdmacnt = fxdr_unsigned(uint32_t, *tl);
3889 nd->nd_repstat = NFSERR_BADXDR;
3891 } else if (rdmacnt == 1)
3892 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
3894 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
3895 sep->sess_cbprogram = fxdr_unsigned(uint32_t, *tl);
3898 * nfsrv_getclient() searches the client list for a match and
3899 * returns the appropriate NFSERR status.
3901 nd->nd_repstat = nfsrv_getclient(clientid, CLOPS_CONFIRM | CLOPS_RENEW,
3902 NULL, sep, confirm, sep->sess_cbprogram, nd, p);
3903 if (nd->nd_repstat == 0) {
3904 NFSM_BUILD(tl, uint32_t *, NFSX_V4SESSIONID);
3905 NFSBCOPY(sep->sess_sessionid, tl, NFSX_V4SESSIONID);
3906 NFSM_BUILD(tl, uint32_t *, 18 * NFSX_UNSIGNED);
3907 *tl++ = txdr_unsigned(confirm.lval[0]); /* sequenceid */
3908 *tl++ = txdr_unsigned(sep->sess_crflags);
3910 /* Fore channel attributes. */
3912 *tl++ = txdr_unsigned(sep->sess_maxreq);
3913 *tl++ = txdr_unsigned(sep->sess_maxresp);
3914 *tl++ = txdr_unsigned(sep->sess_maxrespcached);
3915 *tl++ = txdr_unsigned(sep->sess_maxops);
3916 *tl++ = txdr_unsigned(sep->sess_maxslots);
3917 *tl++ = txdr_unsigned(1);
3918 *tl++ = txdr_unsigned(0); /* No RDMA. */
3920 /* Back channel attributes. */
3922 *tl++ = txdr_unsigned(sep->sess_cbmaxreq);
3923 *tl++ = txdr_unsigned(sep->sess_cbmaxresp);
3924 *tl++ = txdr_unsigned(sep->sess_cbmaxrespcached);
3925 *tl++ = txdr_unsigned(sep->sess_cbmaxops);
3926 *tl++ = txdr_unsigned(sep->sess_cbsess.nfsess_foreslots);
3927 *tl++ = txdr_unsigned(1);
3928 *tl = txdr_unsigned(0); /* No RDMA. */
3931 if (nd->nd_repstat != 0 && sep != NULL)
3932 free(sep, M_NFSDSESSION);
3933 NFSEXITCODE2(error, nd);
3938 * nfsv4 sequence service
3941 nfsrvd_sequence(struct nfsrv_descript *nd, __unused int isdgram,
3942 __unused vnode_t vp, __unused NFSPROC_T *p, __unused struct nfsexstuff *exp)
3945 uint32_t highest_slotid, sequenceid, sflags, target_highest_slotid;
3946 int cache_this, error = 0;
3948 if (nfs_rootfhset == 0 || nfsd_checkrootexp(nd) != 0) {
3949 nd->nd_repstat = NFSERR_WRONGSEC;
3952 NFSM_DISSECT(tl, uint32_t *, NFSX_V4SESSIONID);
3953 NFSBCOPY(tl, nd->nd_sessionid, NFSX_V4SESSIONID);
3954 NFSM_DISSECT(tl, uint32_t *, 4 * NFSX_UNSIGNED);
3955 sequenceid = fxdr_unsigned(uint32_t, *tl++);
3956 nd->nd_slotid = fxdr_unsigned(uint32_t, *tl++);
3957 highest_slotid = fxdr_unsigned(uint32_t, *tl++);
3958 if (*tl == newnfs_true)
3962 nd->nd_flag |= ND_HASSEQUENCE;
3963 nd->nd_repstat = nfsrv_checksequence(nd, sequenceid, &highest_slotid,
3964 &target_highest_slotid, cache_this, &sflags, p);
3965 if (nd->nd_repstat == 0) {
3966 NFSM_BUILD(tl, uint32_t *, NFSX_V4SESSIONID);
3967 NFSBCOPY(nd->nd_sessionid, tl, NFSX_V4SESSIONID);
3968 NFSM_BUILD(tl, uint32_t *, 5 * NFSX_UNSIGNED);
3969 *tl++ = txdr_unsigned(sequenceid);
3970 *tl++ = txdr_unsigned(nd->nd_slotid);
3971 *tl++ = txdr_unsigned(highest_slotid);
3972 *tl++ = txdr_unsigned(target_highest_slotid);
3973 *tl = txdr_unsigned(sflags);
3976 NFSEXITCODE2(error, nd);
3981 * nfsv4 reclaim complete service
3984 nfsrvd_reclaimcomplete(struct nfsrv_descript *nd, __unused int isdgram,
3985 __unused vnode_t vp, __unused NFSPROC_T *p, __unused struct nfsexstuff *exp)
3990 if (nfs_rootfhset == 0 || nfsd_checkrootexp(nd) != 0) {
3991 nd->nd_repstat = NFSERR_WRONGSEC;
3994 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
3995 if (*tl == newnfs_true)
3996 nd->nd_repstat = NFSERR_NOTSUPP;
3998 nd->nd_repstat = nfsrv_checkreclaimcomplete(nd);
4000 NFSEXITCODE2(error, nd);
4005 * nfsv4 destroy clientid service
4008 nfsrvd_destroyclientid(struct nfsrv_descript *nd, __unused int isdgram,
4009 __unused vnode_t vp, NFSPROC_T *p, __unused struct nfsexstuff *exp)
4015 if (nfs_rootfhset == 0 || nfsd_checkrootexp(nd) != 0) {
4016 nd->nd_repstat = NFSERR_WRONGSEC;
4019 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
4020 clientid.lval[0] = *tl++;
4021 clientid.lval[1] = *tl;
4022 nd->nd_repstat = nfsrv_destroyclient(clientid, p);
4024 NFSEXITCODE2(error, nd);
4029 * nfsv4 bind connection to session service
4032 nfsrvd_bindconnsess(struct nfsrv_descript *nd, __unused int isdgram,
4033 __unused vnode_t vp, NFSPROC_T *p, __unused struct nfsexstuff *exp)
4036 uint8_t sessid[NFSX_V4SESSIONID];
4037 int error = 0, foreaft;
4039 if (nfs_rootfhset == 0 || nfsd_checkrootexp(nd) != 0) {
4040 nd->nd_repstat = NFSERR_WRONGSEC;
4043 NFSM_DISSECT(tl, uint32_t *, NFSX_V4SESSIONID + 2 * NFSX_UNSIGNED);
4044 NFSBCOPY(tl, sessid, NFSX_V4SESSIONID);
4045 tl += (NFSX_V4SESSIONID / NFSX_UNSIGNED);
4046 foreaft = fxdr_unsigned(int, *tl++);
4047 if (*tl == newnfs_true) {
4048 /* RDMA is not supported. */
4049 nd->nd_repstat = NFSERR_NOTSUPP;
4053 nd->nd_repstat = nfsrv_bindconnsess(nd, sessid, &foreaft);
4054 if (nd->nd_repstat == 0) {
4055 NFSM_BUILD(tl, uint32_t *, NFSX_V4SESSIONID + 2 *
4057 NFSBCOPY(sessid, tl, NFSX_V4SESSIONID);
4058 tl += (NFSX_V4SESSIONID / NFSX_UNSIGNED);
4059 *tl++ = txdr_unsigned(foreaft);
4063 NFSEXITCODE2(error, nd);
4068 * nfsv4 destroy session service
4071 nfsrvd_destroysession(struct nfsrv_descript *nd, __unused int isdgram,
4072 __unused vnode_t vp, __unused NFSPROC_T *p, __unused struct nfsexstuff *exp)
4074 uint8_t *cp, sessid[NFSX_V4SESSIONID];
4077 if (nfs_rootfhset == 0 || nfsd_checkrootexp(nd) != 0) {
4078 nd->nd_repstat = NFSERR_WRONGSEC;
4081 NFSM_DISSECT(cp, uint8_t *, NFSX_V4SESSIONID);
4082 NFSBCOPY(cp, sessid, NFSX_V4SESSIONID);
4083 nd->nd_repstat = nfsrv_destroysession(nd, sessid);
4085 NFSEXITCODE2(error, nd);
4090 * nfsv4 free stateid service
4093 nfsrvd_freestateid(struct nfsrv_descript *nd, __unused int isdgram,
4094 __unused vnode_t vp, NFSPROC_T *p, __unused struct nfsexstuff *exp)
4097 nfsv4stateid_t stateid;
4100 if (nfs_rootfhset == 0 || nfsd_checkrootexp(nd) != 0) {
4101 nd->nd_repstat = NFSERR_WRONGSEC;
4104 NFSM_DISSECT(tl, uint32_t *, NFSX_STATEID);
4105 stateid.seqid = fxdr_unsigned(uint32_t, *tl++);
4106 NFSBCOPY(tl, stateid.other, NFSX_STATEIDOTHER);
4107 nd->nd_repstat = nfsrv_freestateid(nd, &stateid, p);
4109 NFSEXITCODE2(error, nd);
4114 * nfsv4 test stateid service
4117 nfsrvd_teststateid(struct nfsrv_descript *nd, __unused int isdgram,
4118 __unused vnode_t vp, NFSPROC_T *p, __unused struct nfsexstuff *exp)
4121 nfsv4stateid_t *stateidp = NULL, *tstateidp;
4122 int cnt, error = 0, i, ret;
4124 if (nfs_rootfhset == 0 || nfsd_checkrootexp(nd) != 0) {
4125 nd->nd_repstat = NFSERR_WRONGSEC;
4128 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
4129 cnt = fxdr_unsigned(int, *tl);
4130 if (cnt <= 0 || cnt > 1024) {
4131 nd->nd_repstat = NFSERR_BADXDR;
4134 stateidp = mallocarray(cnt, sizeof(nfsv4stateid_t), M_TEMP, M_WAITOK);
4135 tstateidp = stateidp;
4136 for (i = 0; i < cnt; i++) {
4137 NFSM_DISSECT(tl, uint32_t *, NFSX_STATEID);
4138 tstateidp->seqid = fxdr_unsigned(uint32_t, *tl++);
4139 NFSBCOPY(tl, tstateidp->other, NFSX_STATEIDOTHER);
4142 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
4143 *tl = txdr_unsigned(cnt);
4144 tstateidp = stateidp;
4145 for (i = 0; i < cnt; i++) {
4146 ret = nfsrv_teststateid(nd, tstateidp, p);
4147 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
4148 *tl = txdr_unsigned(ret);
4152 free(stateidp, M_TEMP);
4153 NFSEXITCODE2(error, nd);
4158 * nfsv4 service not supported
4161 nfsrvd_notsupp(struct nfsrv_descript *nd, __unused int isdgram,
4162 __unused vnode_t vp, __unused NFSPROC_T *p, __unused struct nfsexstuff *exp)
4165 nd->nd_repstat = NFSERR_NOTSUPP;
4166 NFSEXITCODE2(0, nd);