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 * Rpc op calls, generally called from the vnode op calls or through the
39 * buffer cache, for NFS v2, 3 and 4.
40 * These do not normally make any changes to vnode arguments or use
41 * structures that might change between the VFS variants. The returned
42 * arguments are all at the end, after the NFSPROC_T *p one.
46 #include "opt_inet6.h"
48 #include <fs/nfs/nfsport.h>
53 extern int nfs_numnfscbd;
54 extern struct timeval nfsboottime;
55 extern u_int32_t newnfs_false, newnfs_true;
56 extern nfstype nfsv34_type[9];
57 extern int nfsrv_useacl;
58 extern char nfsv4_callbackaddr[INET6_ADDRSTRLEN];
59 extern int nfscl_debuglevel;
61 int nfstest_outofseq = 0;
62 int nfscl_assumeposixlocks = 1;
63 int nfscl_enablecallb = 0;
64 short nfsv4_cbport = NFSV4_CBPORT;
65 int nfstest_openallsetattr = 0;
66 #endif /* !APPLEKEXT */
68 #define DIRHDSIZ (sizeof (struct dirent) - (MAXNAMLEN + 1))
71 * nfscl_getsameserver() can return one of three values:
72 * NFSDSP_USETHISSESSION - Use this session for the DS.
73 * NFSDSP_SEQTHISSESSION - Use the nfsclds_sequence field of this dsp for new
75 * NFSDSP_NOTFOUND - No matching server was found.
78 NFSDSP_USETHISSESSION = 0,
79 NFSDSP_SEQTHISSESSION = 1,
83 static int nfsrpc_setattrrpc(vnode_t , struct vattr *, nfsv4stateid_t *,
84 struct ucred *, NFSPROC_T *, struct nfsvattr *, int *, void *);
85 static int nfsrpc_readrpc(vnode_t , struct uio *, struct ucred *,
86 nfsv4stateid_t *, NFSPROC_T *, struct nfsvattr *, int *, void *);
87 static int nfsrpc_writerpc(vnode_t , struct uio *, int *, int *,
88 struct ucred *, nfsv4stateid_t *, NFSPROC_T *, struct nfsvattr *, int *,
90 static int nfsrpc_createv23(vnode_t , char *, int, struct vattr *,
91 nfsquad_t, int, struct ucred *, NFSPROC_T *, struct nfsvattr *,
92 struct nfsvattr *, struct nfsfh **, int *, int *, void *);
93 static int nfsrpc_createv4(vnode_t , char *, int, struct vattr *,
94 nfsquad_t, int, struct nfsclowner *, struct nfscldeleg **, struct ucred *,
95 NFSPROC_T *, struct nfsvattr *, struct nfsvattr *, struct nfsfh **, int *,
96 int *, void *, int *);
97 static int nfsrpc_locku(struct nfsrv_descript *, struct nfsmount *,
98 struct nfscllockowner *, u_int64_t, u_int64_t,
99 u_int32_t, struct ucred *, NFSPROC_T *, int);
100 static int nfsrpc_setaclrpc(vnode_t, struct ucred *, NFSPROC_T *,
101 struct acl *, nfsv4stateid_t *, void *);
102 static int nfsrpc_getlayout(struct nfsmount *, vnode_t, struct nfsfh *, int,
103 uint32_t *, nfsv4stateid_t *, uint64_t, struct nfscllayout **,
104 struct ucred *, NFSPROC_T *);
105 static int nfsrpc_fillsa(struct nfsmount *, struct sockaddr_storage *,
106 struct nfsclds **, NFSPROC_T *);
107 static void nfscl_initsessionslots(struct nfsclsession *);
108 static int nfscl_doflayoutio(vnode_t, struct uio *, int *, int *, int *,
109 nfsv4stateid_t *, int, struct nfscldevinfo *, struct nfscllayout *,
110 struct nfsclflayout *, uint64_t, uint64_t, struct ucred *, NFSPROC_T *);
111 static int nfsrpc_readds(vnode_t, struct uio *, nfsv4stateid_t *, int *,
112 struct nfsclds *, uint64_t, int, struct nfsfh *, struct ucred *,
114 static int nfsrpc_writeds(vnode_t, struct uio *, int *, int *,
115 nfsv4stateid_t *, struct nfsclds *, uint64_t, int,
116 struct nfsfh *, int, struct ucred *, NFSPROC_T *);
117 static enum nfsclds_state nfscl_getsameserver(struct nfsmount *,
118 struct nfsclds *, struct nfsclds **);
120 static int nfsrpc_commitds(vnode_t, uint64_t, int, struct nfsclds *,
121 struct nfsfh *, struct ucred *, NFSPROC_T *, void *);
125 * nfs null call from vfs.
128 nfsrpc_null(vnode_t vp, struct ucred *cred, NFSPROC_T *p)
131 struct nfsrv_descript nfsd, *nd = &nfsd;
133 NFSCL_REQSTART(nd, NFSPROC_NULL, vp);
134 error = nfscl_request(nd, vp, p, cred, NULL);
135 if (nd->nd_repstat && !error)
136 error = nd->nd_repstat;
137 mbuf_freem(nd->nd_mrep);
143 * For nfs version 3 and 4, use the access rpc to check accessibility. If file
144 * modes are changed on the server, accesses might still fail later.
147 nfsrpc_access(vnode_t vp, int acmode, struct ucred *cred,
148 NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp)
151 u_int32_t mode, rmode;
154 mode = NFSACCESS_READ;
157 if (vnode_vtype(vp) == VDIR) {
159 mode |= (NFSACCESS_MODIFY | NFSACCESS_EXTEND |
162 mode |= NFSACCESS_LOOKUP;
165 mode |= (NFSACCESS_MODIFY | NFSACCESS_EXTEND);
167 mode |= NFSACCESS_EXECUTE;
171 * Now, just call nfsrpc_accessrpc() to do the actual RPC.
173 error = nfsrpc_accessrpc(vp, mode, cred, p, nap, attrflagp, &rmode,
177 * The NFS V3 spec does not clarify whether or not
178 * the returned access bits can be a superset of
179 * the ones requested, so...
181 if (!error && (rmode & mode) != mode)
187 * The actual rpc, separated out for Darwin.
190 nfsrpc_accessrpc(vnode_t vp, u_int32_t mode, struct ucred *cred,
191 NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, u_int32_t *rmodep,
195 u_int32_t supported, rmode;
197 struct nfsrv_descript nfsd, *nd = &nfsd;
198 nfsattrbit_t attrbits;
202 NFSCL_REQSTART(nd, NFSPROC_ACCESS, vp);
203 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
204 *tl = txdr_unsigned(mode);
205 if (nd->nd_flag & ND_NFSV4) {
207 * And do a Getattr op.
209 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
210 *tl = txdr_unsigned(NFSV4OP_GETATTR);
211 NFSGETATTR_ATTRBIT(&attrbits);
212 (void) nfsrv_putattrbit(nd, &attrbits);
214 error = nfscl_request(nd, vp, p, cred, stuff);
217 if (nd->nd_flag & ND_NFSV3) {
218 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
222 if (!nd->nd_repstat) {
223 if (nd->nd_flag & ND_NFSV4) {
224 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
225 supported = fxdr_unsigned(u_int32_t, *tl++);
227 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
229 rmode = fxdr_unsigned(u_int32_t, *tl);
230 if (nd->nd_flag & ND_NFSV4)
231 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
234 * It's not obvious what should be done about
235 * unsupported access modes. For now, be paranoid
236 * and clear the unsupported ones.
241 error = nd->nd_repstat;
243 mbuf_freem(nd->nd_mrep);
251 nfsrpc_open(vnode_t vp, int amode, struct ucred *cred, NFSPROC_T *p)
253 struct nfsclopen *op;
254 struct nfscldeleg *dp;
256 struct nfsnode *np = VTONFS(vp);
257 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
258 u_int32_t mode, clidrev;
259 int ret, newone, error, expireret = 0, retrycnt;
262 * For NFSv4, Open Ops are only done on Regular Files.
264 if (vnode_vtype(vp) != VREG)
268 mode |= NFSV4OPEN_ACCESSREAD;
270 mode |= NFSV4OPEN_ACCESSWRITE;
275 { char name[100]; int namel;
276 namel = (np->n_v4->n4_namelen < 100) ? np->n_v4->n4_namelen : 99;
277 bcopy(NFS4NODENAME(np->n_v4), name, namel);
279 printf("rpcopen p=0x%x name=%s",p->p_pid,name);
280 if (nfhp->nfh_len > 0) printf(" fh=0x%x\n",nfhp->nfh_fh[12]);
281 else printf(" fhl=0\n");
286 error = nfscl_open(vp, nfhp->nfh_fh, nfhp->nfh_len, mode, 1,
287 cred, p, NULL, &op, &newone, &ret, 1);
291 if (nmp->nm_clp != NULL)
292 clidrev = nmp->nm_clp->nfsc_clientidrev;
295 if (ret == NFSCLOPEN_DOOPEN) {
296 if (np->n_v4 != NULL) {
297 error = nfsrpc_openrpc(nmp, vp, np->n_v4->n4_data,
298 np->n_v4->n4_fhlen, np->n_fhp->nfh_fh,
299 np->n_fhp->nfh_len, mode, op,
300 NFS4NODENAME(np->n_v4), np->n_v4->n4_namelen, &dp,
301 0, 0x0, cred, p, 0, 0);
304 OSBitAndAtomic((int32_t)~NDELEGMOD, (UInt32 *)&np->n_flag);
307 np->n_flag &= ~NDELEGMOD;
309 * Invalidate the attribute cache, so that
310 * attributes that pre-date the issue of a
311 * delegation are not cached, since the
312 * cached attributes will remain valid while
313 * the delegation is held.
315 NFSINVALATTRCACHE(np);
318 (void) nfscl_deleg(nmp->nm_mountp,
319 op->nfso_own->nfsow_clp,
320 nfhp->nfh_fh, nfhp->nfh_len, cred, p, &dp);
325 newnfs_copyincred(cred, &op->nfso_cred);
326 } else if (ret == NFSCLOPEN_SETCRED)
328 * This is a new local open on a delegation. It needs
329 * to have credentials so that an open can be done
330 * against the server during recovery.
332 newnfs_copyincred(cred, &op->nfso_cred);
335 * nfso_opencnt is the count of how many VOP_OPEN()s have
336 * been done on this Open successfully and a VOP_CLOSE()
337 * is expected for each of these.
338 * If error is non-zero, don't increment it, since the Open
339 * hasn't succeeded yet.
343 nfscl_openrelease(op, error, newone);
344 if (error == NFSERR_GRACE || error == NFSERR_STALECLIENTID ||
345 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
346 error == NFSERR_BADSESSION) {
347 (void) nfs_catnap(PZERO, error, "nfs_open");
348 } else if ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID)
350 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
353 } while (error == NFSERR_GRACE || error == NFSERR_STALECLIENTID ||
354 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
355 error == NFSERR_BADSESSION ||
356 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
357 expireret == 0 && clidrev != 0 && retrycnt < 4));
358 if (error && retrycnt >= 4)
364 * the actual open rpc
367 nfsrpc_openrpc(struct nfsmount *nmp, vnode_t vp, u_int8_t *nfhp, int fhlen,
368 u_int8_t *newfhp, int newfhlen, u_int32_t mode, struct nfsclopen *op,
369 u_int8_t *name, int namelen, struct nfscldeleg **dpp,
370 int reclaim, u_int32_t delegtype, struct ucred *cred, NFSPROC_T *p,
371 int syscred, int recursed)
374 struct nfsrv_descript nfsd, *nd = &nfsd;
375 struct nfscldeleg *dp, *ndp = NULL;
376 struct nfsvattr nfsva;
377 u_int32_t rflags, deleg;
378 nfsattrbit_t attrbits;
379 int error, ret, acesize, limitby;
383 nfscl_reqstart(nd, NFSPROC_OPEN, nmp, nfhp, fhlen, NULL, NULL);
384 NFSM_BUILD(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
385 *tl++ = txdr_unsigned(op->nfso_own->nfsow_seqid);
386 *tl++ = txdr_unsigned(mode & NFSV4OPEN_ACCESSBOTH);
387 *tl++ = txdr_unsigned((mode >> NFSLCK_SHIFT) & NFSV4OPEN_DENYBOTH);
388 *tl++ = NFSMNT_MDSSESSION(nmp)->nfsess_clientid.lval[0];
389 *tl = NFSMNT_MDSSESSION(nmp)->nfsess_clientid.lval[1];
390 (void) nfsm_strtom(nd, op->nfso_own->nfsow_owner, NFSV4CL_LOCKNAMELEN);
391 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
392 *tl++ = txdr_unsigned(NFSV4OPEN_NOCREATE);
394 *tl = txdr_unsigned(NFSV4OPEN_CLAIMPREVIOUS);
395 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
396 *tl = txdr_unsigned(delegtype);
399 *tl = txdr_unsigned(NFSV4OPEN_CLAIMDELEGATECUR);
400 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID);
401 if (NFSHASNFSV4N(nmp))
404 *tl++ = dp->nfsdl_stateid.seqid;
405 *tl++ = dp->nfsdl_stateid.other[0];
406 *tl++ = dp->nfsdl_stateid.other[1];
407 *tl = dp->nfsdl_stateid.other[2];
409 *tl = txdr_unsigned(NFSV4OPEN_CLAIMNULL);
411 (void) nfsm_strtom(nd, name, namelen);
413 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
414 *tl = txdr_unsigned(NFSV4OP_GETATTR);
415 NFSZERO_ATTRBIT(&attrbits);
416 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_CHANGE);
417 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEMODIFY);
418 (void) nfsrv_putattrbit(nd, &attrbits);
420 nd->nd_flag |= ND_USEGSSNAME;
421 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, vp, p, cred,
422 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
425 NFSCL_INCRSEQID(op->nfso_own->nfsow_seqid, nd);
426 if (!nd->nd_repstat) {
427 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
429 op->nfso_stateid.seqid = *tl++;
430 op->nfso_stateid.other[0] = *tl++;
431 op->nfso_stateid.other[1] = *tl++;
432 op->nfso_stateid.other[2] = *tl;
433 rflags = fxdr_unsigned(u_int32_t, *(tl + 6));
434 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
437 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
438 deleg = fxdr_unsigned(u_int32_t, *tl);
439 if (deleg == NFSV4OPEN_DELEGATEREAD ||
440 deleg == NFSV4OPEN_DELEGATEWRITE) {
441 if (!(op->nfso_own->nfsow_clp->nfsc_flags &
442 NFSCLFLAGS_FIRSTDELEG))
443 op->nfso_own->nfsow_clp->nfsc_flags |=
444 (NFSCLFLAGS_FIRSTDELEG | NFSCLFLAGS_GOTDELEG);
445 MALLOC(ndp, struct nfscldeleg *,
446 sizeof (struct nfscldeleg) + newfhlen,
447 M_NFSCLDELEG, M_WAITOK);
448 LIST_INIT(&ndp->nfsdl_owner);
449 LIST_INIT(&ndp->nfsdl_lock);
450 ndp->nfsdl_clp = op->nfso_own->nfsow_clp;
451 ndp->nfsdl_fhlen = newfhlen;
452 NFSBCOPY(newfhp, ndp->nfsdl_fh, newfhlen);
453 newnfs_copyincred(cred, &ndp->nfsdl_cred);
454 nfscl_lockinit(&ndp->nfsdl_rwlock);
455 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
457 ndp->nfsdl_stateid.seqid = *tl++;
458 ndp->nfsdl_stateid.other[0] = *tl++;
459 ndp->nfsdl_stateid.other[1] = *tl++;
460 ndp->nfsdl_stateid.other[2] = *tl++;
461 ret = fxdr_unsigned(int, *tl);
462 if (deleg == NFSV4OPEN_DELEGATEWRITE) {
463 ndp->nfsdl_flags = NFSCLDL_WRITE;
465 * Indicates how much the file can grow.
467 NFSM_DISSECT(tl, u_int32_t *,
469 limitby = fxdr_unsigned(int, *tl++);
471 case NFSV4OPEN_LIMITSIZE:
472 ndp->nfsdl_sizelimit = fxdr_hyper(tl);
474 case NFSV4OPEN_LIMITBLOCKS:
475 ndp->nfsdl_sizelimit =
476 fxdr_unsigned(u_int64_t, *tl++);
477 ndp->nfsdl_sizelimit *=
478 fxdr_unsigned(u_int64_t, *tl);
481 error = NFSERR_BADXDR;
485 ndp->nfsdl_flags = NFSCLDL_READ;
488 ndp->nfsdl_flags |= NFSCLDL_RECALL;
489 error = nfsrv_dissectace(nd, &ndp->nfsdl_ace, &ret,
493 } else if (deleg != NFSV4OPEN_DELEGATENONE) {
494 error = NFSERR_BADXDR;
497 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
498 error = nfsv4_loadattr(nd, NULL, &nfsva, NULL,
499 NULL, 0, NULL, NULL, NULL, NULL, NULL, 0,
500 NULL, NULL, NULL, p, cred);
504 ndp->nfsdl_change = nfsva.na_filerev;
505 ndp->nfsdl_modtime = nfsva.na_mtime;
506 ndp->nfsdl_flags |= NFSCLDL_MODTIMESET;
508 if (!reclaim && (rflags & NFSV4OPEN_RESULTCONFIRM)) {
510 ret = nfsrpc_openconfirm(vp, newfhp, newfhlen, op,
512 if (ret == NFSERR_DELAY)
513 (void) nfs_catnap(PZERO, ret, "nfs_open");
514 } while (ret == NFSERR_DELAY);
517 if ((rflags & NFSV4OPEN_LOCKTYPEPOSIX) ||
518 nfscl_assumeposixlocks)
519 op->nfso_posixlock = 1;
521 op->nfso_posixlock = 0;
524 * If the server is handing out delegations, but we didn't
525 * get one because an OpenConfirm was required, try the
526 * Open again, to get a delegation. This is a harmless no-op,
527 * from a server's point of view.
529 if (!reclaim && (rflags & NFSV4OPEN_RESULTCONFIRM) &&
530 (op->nfso_own->nfsow_clp->nfsc_flags & NFSCLFLAGS_GOTDELEG)
531 && !error && dp == NULL && ndp == NULL && !recursed) {
533 ret = nfsrpc_openrpc(nmp, vp, nfhp, fhlen, newfhp,
534 newfhlen, mode, op, name, namelen, &ndp, 0, 0x0,
535 cred, p, syscred, 1);
536 if (ret == NFSERR_DELAY)
537 (void) nfs_catnap(PZERO, ret, "nfs_open2");
538 } while (ret == NFSERR_DELAY);
541 FREE((caddr_t)ndp, M_NFSCLDELEG);
544 if (ret == NFSERR_STALECLIENTID ||
545 ret == NFSERR_STALEDONTRECOVER ||
546 ret == NFSERR_BADSESSION)
551 if (nd->nd_repstat != 0 && error == 0)
552 error = nd->nd_repstat;
553 if (error == NFSERR_STALECLIENTID || error == NFSERR_BADSESSION)
554 nfscl_initiate_recovery(op->nfso_own->nfsow_clp);
558 else if (ndp != NULL)
559 FREE((caddr_t)ndp, M_NFSCLDELEG);
560 mbuf_freem(nd->nd_mrep);
568 nfsrpc_opendowngrade(vnode_t vp, u_int32_t mode, struct nfsclopen *op,
569 struct ucred *cred, NFSPROC_T *p)
572 struct nfsrv_descript nfsd, *nd = &nfsd;
575 NFSCL_REQSTART(nd, NFSPROC_OPENDOWNGRADE, vp);
576 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID + 3 * NFSX_UNSIGNED);
577 if (NFSHASNFSV4N(VFSTONFS(vnode_mount(vp))))
580 *tl++ = op->nfso_stateid.seqid;
581 *tl++ = op->nfso_stateid.other[0];
582 *tl++ = op->nfso_stateid.other[1];
583 *tl++ = op->nfso_stateid.other[2];
584 *tl++ = txdr_unsigned(op->nfso_own->nfsow_seqid);
585 *tl++ = txdr_unsigned(mode & NFSV4OPEN_ACCESSBOTH);
586 *tl = txdr_unsigned((mode >> NFSLCK_SHIFT) & NFSV4OPEN_DENYBOTH);
587 error = nfscl_request(nd, vp, p, cred, NULL);
590 NFSCL_INCRSEQID(op->nfso_own->nfsow_seqid, nd);
591 if (!nd->nd_repstat) {
592 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID);
593 op->nfso_stateid.seqid = *tl++;
594 op->nfso_stateid.other[0] = *tl++;
595 op->nfso_stateid.other[1] = *tl++;
596 op->nfso_stateid.other[2] = *tl;
598 if (nd->nd_repstat && error == 0)
599 error = nd->nd_repstat;
600 if (error == NFSERR_STALESTATEID || error == NFSERR_BADSESSION)
601 nfscl_initiate_recovery(op->nfso_own->nfsow_clp);
603 mbuf_freem(nd->nd_mrep);
608 * V4 Close operation.
611 nfsrpc_close(vnode_t vp, int doclose, NFSPROC_T *p)
613 struct nfsclclient *clp;
616 if (vnode_vtype(vp) != VREG)
619 error = nfscl_doclose(vp, &clp, p);
621 error = nfscl_getclose(vp, &clp);
625 nfscl_clientrelease(clp);
633 nfsrpc_doclose(struct nfsmount *nmp, struct nfsclopen *op, NFSPROC_T *p)
635 struct nfsrv_descript nfsd, *nd = &nfsd;
636 struct nfscllockowner *lp, *nlp;
637 struct nfscllock *lop, *nlop;
639 u_int64_t off = 0, len = 0;
640 u_int32_t type = NFSV4LOCKT_READ;
641 int error, do_unlock, trycnt;
643 tcred = newnfs_getcred();
644 newnfs_copycred(&op->nfso_cred, tcred);
646 * (Theoretically this could be done in the same
647 * compound as the close, but having multiple
648 * sequenced Ops in the same compound might be
649 * too scary for some servers.)
651 if (op->nfso_posixlock) {
654 type = NFSV4LOCKT_READ;
658 * Since this function is only called from VOP_INACTIVE(), no
659 * other thread will be manipulating this Open. As such, the
660 * lock lists are not being changed by other threads, so it should
661 * be safe to do this without locking.
663 LIST_FOREACH(lp, &op->nfso_lock, nfsl_list) {
665 LIST_FOREACH_SAFE(lop, &lp->nfsl_lock, nfslo_list, nlop) {
666 if (op->nfso_posixlock == 0) {
667 off = lop->nfslo_first;
668 len = lop->nfslo_end - lop->nfslo_first;
669 if (lop->nfslo_type == F_WRLCK)
670 type = NFSV4LOCKT_WRITE;
672 type = NFSV4LOCKT_READ;
677 error = nfsrpc_locku(nd, nmp, lp, off,
678 len, type, tcred, p, 0);
679 if ((nd->nd_repstat == NFSERR_GRACE ||
680 nd->nd_repstat == NFSERR_DELAY) &&
682 (void) nfs_catnap(PZERO,
685 } while ((nd->nd_repstat == NFSERR_GRACE ||
686 nd->nd_repstat == NFSERR_DELAY) &&
687 error == 0 && trycnt++ < 5);
688 if (op->nfso_posixlock)
691 nfscl_freelock(lop, 0);
694 * Do a ReleaseLockOwner.
695 * The lock owner name nfsl_owner may be used by other opens for
696 * other files but the lock_owner4 name that nfsrpc_rellockown()
697 * puts on the wire has the file handle for this file appended
698 * to it, so it can be done now.
700 (void)nfsrpc_rellockown(nmp, lp, lp->nfsl_open->nfso_fh,
701 lp->nfsl_open->nfso_fhlen, tcred, p);
705 * There could be other Opens for different files on the same
706 * OpenOwner, so locking is required.
709 nfscl_lockexcl(&op->nfso_own->nfsow_rwlock, NFSCLSTATEMUTEXPTR);
712 error = nfscl_tryclose(op, tcred, nmp, p);
713 if (error == NFSERR_GRACE)
714 (void) nfs_catnap(PZERO, error, "nfs_close");
715 } while (error == NFSERR_GRACE);
717 nfscl_lockunlock(&op->nfso_own->nfsow_rwlock);
719 LIST_FOREACH_SAFE(lp, &op->nfso_lock, nfsl_list, nlp)
720 nfscl_freelockowner(lp, 0);
721 nfscl_freeopen(op, 0);
727 * The actual Close RPC.
730 nfsrpc_closerpc(struct nfsrv_descript *nd, struct nfsmount *nmp,
731 struct nfsclopen *op, struct ucred *cred, NFSPROC_T *p,
737 nfscl_reqstart(nd, NFSPROC_CLOSE, nmp, op->nfso_fh,
738 op->nfso_fhlen, NULL, NULL);
739 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED + NFSX_STATEID);
740 *tl++ = txdr_unsigned(op->nfso_own->nfsow_seqid);
741 if (NFSHASNFSV4N(nmp))
744 *tl++ = op->nfso_stateid.seqid;
745 *tl++ = op->nfso_stateid.other[0];
746 *tl++ = op->nfso_stateid.other[1];
747 *tl = op->nfso_stateid.other[2];
749 nd->nd_flag |= ND_USEGSSNAME;
750 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
751 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
754 NFSCL_INCRSEQID(op->nfso_own->nfsow_seqid, nd);
755 if (nd->nd_repstat == 0)
756 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID);
757 error = nd->nd_repstat;
758 if (error == NFSERR_STALESTATEID || error == NFSERR_BADSESSION)
759 nfscl_initiate_recovery(op->nfso_own->nfsow_clp);
761 mbuf_freem(nd->nd_mrep);
766 * V4 Open Confirm RPC.
769 nfsrpc_openconfirm(vnode_t vp, u_int8_t *nfhp, int fhlen,
770 struct nfsclopen *op, struct ucred *cred, NFSPROC_T *p)
773 struct nfsrv_descript nfsd, *nd = &nfsd;
774 struct nfsmount *nmp;
777 nmp = VFSTONFS(vnode_mount(vp));
778 if (NFSHASNFSV4N(nmp))
779 return (0); /* No confirmation for NFSv4.1. */
780 nfscl_reqstart(nd, NFSPROC_OPENCONFIRM, nmp, nfhp, fhlen, NULL, NULL);
781 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED + NFSX_STATEID);
782 *tl++ = op->nfso_stateid.seqid;
783 *tl++ = op->nfso_stateid.other[0];
784 *tl++ = op->nfso_stateid.other[1];
785 *tl++ = op->nfso_stateid.other[2];
786 *tl = txdr_unsigned(op->nfso_own->nfsow_seqid);
787 error = nfscl_request(nd, vp, p, cred, NULL);
790 NFSCL_INCRSEQID(op->nfso_own->nfsow_seqid, nd);
791 if (!nd->nd_repstat) {
792 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID);
793 op->nfso_stateid.seqid = *tl++;
794 op->nfso_stateid.other[0] = *tl++;
795 op->nfso_stateid.other[1] = *tl++;
796 op->nfso_stateid.other[2] = *tl;
798 error = nd->nd_repstat;
799 if (error == NFSERR_STALESTATEID || error == NFSERR_BADSESSION)
800 nfscl_initiate_recovery(op->nfso_own->nfsow_clp);
802 mbuf_freem(nd->nd_mrep);
807 * Do the setclientid and setclientid confirm RPCs. Called from nfs_statfs()
808 * when a mount has just occurred and when the server replies NFSERR_EXPIRED.
811 nfsrpc_setclient(struct nfsmount *nmp, struct nfsclclient *clp, int reclaim,
812 struct ucred *cred, NFSPROC_T *p)
815 struct nfsrv_descript nfsd;
816 struct nfsrv_descript *nd = &nfsd;
817 nfsattrbit_t attrbits;
818 u_int8_t *cp = NULL, *cp2, addr[INET6_ADDRSTRLEN + 9];
820 int error, isinet6 = 0, callblen;
823 static u_int32_t rev = 0;
824 struct nfsclds *dsp, *ndsp, *tdsp;
826 if (nfsboottime.tv_sec == 0)
827 NFSSETBOOTTIME(nfsboottime);
828 clp->nfsc_rev = rev++;
829 if (NFSHASNFSV4N(nmp)) {
830 error = nfsrpc_exchangeid(nmp, clp, &nmp->nm_sockreq,
831 NFSV4EXCH_USEPNFSMDS | NFSV4EXCH_USENONPNFS, &dsp, cred, p);
832 NFSCL_DEBUG(1, "aft exch=%d\n", error);
834 error = nfsrpc_createsession(nmp, &dsp->nfsclds_sess,
836 dsp->nfsclds_sess.nfsess_sequenceid, 1, cred, p);
839 TAILQ_FOREACH_SAFE(tdsp, &nmp->nm_sess,
841 nfscl_freenfsclds(tdsp);
842 TAILQ_INIT(&nmp->nm_sess);
843 TAILQ_INSERT_HEAD(&nmp->nm_sess, dsp,
847 nfscl_freenfsclds(dsp);
848 NFSCL_DEBUG(1, "aft createsess=%d\n", error);
850 if (error == 0 && reclaim == 0) {
851 error = nfsrpc_reclaimcomplete(nmp, cred, p);
852 NFSCL_DEBUG(1, "aft reclaimcomp=%d\n", error);
853 if (error == NFSERR_COMPLETEALREADY ||
854 error == NFSERR_NOTSUPP)
855 /* Ignore this error. */
862 * Allocate a single session structure for NFSv4.0, because some of
863 * the fields are used by NFSv4.0 although it doesn't do a session.
865 dsp = malloc(sizeof(struct nfsclds), M_NFSCLDS, M_WAITOK | M_ZERO);
866 mtx_init(&dsp->nfsclds_mtx, "nfsds", NULL, MTX_DEF);
867 mtx_init(&dsp->nfsclds_sess.nfsess_mtx, "nfssession", NULL, MTX_DEF);
869 TAILQ_INSERT_HEAD(&nmp->nm_sess, dsp, nfsclds_list);
872 nfscl_reqstart(nd, NFSPROC_SETCLIENTID, nmp, NULL, 0, NULL, NULL);
873 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
874 *tl++ = txdr_unsigned(nfsboottime.tv_sec);
875 *tl = txdr_unsigned(clp->nfsc_rev);
876 (void) nfsm_strtom(nd, clp->nfsc_id, clp->nfsc_idlen);
879 * set up the callback address
881 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
882 *tl = txdr_unsigned(NFS_CALLBCKPROG);
883 callblen = strlen(nfsv4_callbackaddr);
885 cp = nfscl_getmyip(nmp, &isinet6);
886 if (nfscl_enablecallb && nfs_numnfscbd > 0 &&
887 (callblen > 0 || cp != NULL)) {
888 port = htons(nfsv4_cbport);
889 cp2 = (u_int8_t *)&port;
892 strchr(nfsv4_callbackaddr, ':')) || isinet6) {
893 char ip6buf[INET6_ADDRSTRLEN], *ip6add;
895 (void) nfsm_strtom(nd, "tcp6", 4);
897 ip6_sprintf(ip6buf, (struct in6_addr *)cp);
900 ip6add = nfsv4_callbackaddr;
902 snprintf(addr, INET6_ADDRSTRLEN + 9, "%s.%d.%d",
903 ip6add, cp2[0], cp2[1]);
907 (void) nfsm_strtom(nd, "tcp", 3);
909 snprintf(addr, INET6_ADDRSTRLEN + 9,
910 "%d.%d.%d.%d.%d.%d", cp[0], cp[1],
911 cp[2], cp[3], cp2[0], cp2[1]);
913 snprintf(addr, INET6_ADDRSTRLEN + 9,
914 "%s.%d.%d", nfsv4_callbackaddr,
917 (void) nfsm_strtom(nd, addr, strlen(addr));
919 (void) nfsm_strtom(nd, "tcp", 3);
920 (void) nfsm_strtom(nd, "0.0.0.0.0.0", 11);
922 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
923 *tl = txdr_unsigned(clp->nfsc_cbident);
924 nd->nd_flag |= ND_USEGSSNAME;
925 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
926 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
929 if (nd->nd_repstat == 0) {
930 NFSM_DISSECT(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
931 NFSMNT_MDSSESSION(nmp)->nfsess_clientid.lval[0] = *tl++;
932 NFSMNT_MDSSESSION(nmp)->nfsess_clientid.lval[1] = *tl++;
933 confirm.lval[0] = *tl++;
934 confirm.lval[1] = *tl;
935 mbuf_freem(nd->nd_mrep);
941 nfscl_reqstart(nd, NFSPROC_SETCLIENTIDCFRM, nmp, NULL, 0, NULL,
943 NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
944 *tl++ = NFSMNT_MDSSESSION(nmp)->nfsess_clientid.lval[0];
945 *tl++ = NFSMNT_MDSSESSION(nmp)->nfsess_clientid.lval[1];
946 *tl++ = confirm.lval[0];
947 *tl = confirm.lval[1];
948 nd->nd_flag |= ND_USEGSSNAME;
949 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p,
950 cred, NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
953 mbuf_freem(nd->nd_mrep);
955 if (nd->nd_repstat == 0) {
956 nfscl_reqstart(nd, NFSPROC_GETATTR, nmp, nmp->nm_fh,
957 nmp->nm_fhsize, NULL, NULL);
958 NFSZERO_ATTRBIT(&attrbits);
959 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_LEASETIME);
960 (void) nfsrv_putattrbit(nd, &attrbits);
961 nd->nd_flag |= ND_USEGSSNAME;
962 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p,
963 cred, NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
966 if (nd->nd_repstat == 0) {
967 error = nfsv4_loadattr(nd, NULL, NULL, NULL, NULL, 0, NULL,
968 NULL, NULL, NULL, NULL, 0, NULL, &lease, NULL, p, cred);
971 clp->nfsc_renew = NFSCL_RENEW(lease);
972 clp->nfsc_expire = NFSD_MONOSEC + clp->nfsc_renew;
973 clp->nfsc_clientidrev++;
974 if (clp->nfsc_clientidrev == 0)
975 clp->nfsc_clientidrev++;
979 error = nd->nd_repstat;
981 mbuf_freem(nd->nd_mrep);
989 nfsrpc_getattr(vnode_t vp, struct ucred *cred, NFSPROC_T *p,
990 struct nfsvattr *nap, void *stuff)
992 struct nfsrv_descript nfsd, *nd = &nfsd;
994 nfsattrbit_t attrbits;
996 NFSCL_REQSTART(nd, NFSPROC_GETATTR, vp);
997 if (nd->nd_flag & ND_NFSV4) {
998 NFSGETATTR_ATTRBIT(&attrbits);
999 (void) nfsrv_putattrbit(nd, &attrbits);
1001 error = nfscl_request(nd, vp, p, cred, stuff);
1004 if (!nd->nd_repstat)
1005 error = nfsm_loadattr(nd, nap);
1007 error = nd->nd_repstat;
1008 mbuf_freem(nd->nd_mrep);
1013 * nfs getattr call with non-vnode arguemnts.
1016 nfsrpc_getattrnovp(struct nfsmount *nmp, u_int8_t *fhp, int fhlen, int syscred,
1017 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, u_int64_t *xidp,
1020 struct nfsrv_descript nfsd, *nd = &nfsd;
1021 int error, vers = NFS_VER2;
1022 nfsattrbit_t attrbits;
1024 nfscl_reqstart(nd, NFSPROC_GETATTR, nmp, fhp, fhlen, NULL, NULL);
1025 if (nd->nd_flag & ND_NFSV4) {
1027 NFSGETATTR_ATTRBIT(&attrbits);
1028 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_LEASETIME);
1029 (void) nfsrv_putattrbit(nd, &attrbits);
1030 } else if (nd->nd_flag & ND_NFSV3) {
1034 nd->nd_flag |= ND_USEGSSNAME;
1035 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
1036 NFS_PROG, vers, NULL, 1, xidp, NULL);
1039 if (nd->nd_repstat == 0) {
1040 if ((nd->nd_flag & ND_NFSV4) != 0)
1041 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0,
1042 NULL, NULL, NULL, NULL, NULL, 0, NULL, leasep, NULL,
1045 error = nfsm_loadattr(nd, nap);
1047 error = nd->nd_repstat;
1048 mbuf_freem(nd->nd_mrep);
1053 * Do an nfs setattr operation.
1056 nfsrpc_setattr(vnode_t vp, struct vattr *vap, NFSACL_T *aclp,
1057 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *rnap, int *attrflagp,
1060 int error, expireret = 0, openerr, retrycnt;
1061 u_int32_t clidrev = 0, mode;
1062 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
1064 nfsv4stateid_t stateid;
1067 if (nmp->nm_clp != NULL)
1068 clidrev = nmp->nm_clp->nfsc_clientidrev;
1069 if (vap != NULL && NFSATTRISSET(u_quad_t, vap, va_size))
1070 mode = NFSV4OPEN_ACCESSWRITE;
1072 mode = NFSV4OPEN_ACCESSREAD;
1077 if (NFSHASNFSV4(nmp)) {
1078 nfhp = VTONFS(vp)->n_fhp;
1079 error = nfscl_getstateid(vp, nfhp->nfh_fh,
1080 nfhp->nfh_len, mode, 0, cred, p, &stateid, &lckp);
1081 if (error && vnode_vtype(vp) == VREG &&
1082 (mode == NFSV4OPEN_ACCESSWRITE ||
1083 nfstest_openallsetattr)) {
1085 * No Open stateid, so try and open the file
1088 if (mode == NFSV4OPEN_ACCESSWRITE)
1089 openerr = nfsrpc_open(vp, FWRITE, cred,
1092 openerr = nfsrpc_open(vp, FREAD, cred,
1095 (void) nfscl_getstateid(vp,
1096 nfhp->nfh_fh, nfhp->nfh_len,
1097 mode, 0, cred, p, &stateid, &lckp);
1101 error = nfsrpc_setattrrpc(vp, vap, &stateid, cred, p,
1102 rnap, attrflagp, stuff);
1104 error = nfsrpc_setaclrpc(vp, cred, p, aclp, &stateid,
1106 if (error == NFSERR_STALESTATEID || error == NFSERR_BADSESSION)
1107 nfscl_initiate_recovery(nmp->nm_clp);
1109 nfscl_lockderef(lckp);
1111 (void) nfsrpc_close(vp, 0, p);
1112 if (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
1113 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
1114 error == NFSERR_OLDSTATEID || error == NFSERR_BADSESSION) {
1115 (void) nfs_catnap(PZERO, error, "nfs_setattr");
1116 } else if ((error == NFSERR_EXPIRED ||
1117 error == NFSERR_BADSTATEID) && clidrev != 0) {
1118 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
1121 } while (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
1122 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
1123 error == NFSERR_BADSESSION ||
1124 (error == NFSERR_OLDSTATEID && retrycnt < 20) ||
1125 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
1126 expireret == 0 && clidrev != 0 && retrycnt < 4));
1127 if (error && retrycnt >= 4)
1133 nfsrpc_setattrrpc(vnode_t vp, struct vattr *vap,
1134 nfsv4stateid_t *stateidp, struct ucred *cred, NFSPROC_T *p,
1135 struct nfsvattr *rnap, int *attrflagp, void *stuff)
1138 struct nfsrv_descript nfsd, *nd = &nfsd;
1140 nfsattrbit_t attrbits;
1143 NFSCL_REQSTART(nd, NFSPROC_SETATTR, vp);
1144 if (nd->nd_flag & ND_NFSV4)
1145 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID);
1146 vap->va_type = vnode_vtype(vp);
1147 nfscl_fillsattr(nd, vap, vp, NFSSATTR_FULL, 0);
1148 if (nd->nd_flag & ND_NFSV3) {
1149 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1151 } else if (nd->nd_flag & ND_NFSV4) {
1152 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1153 *tl = txdr_unsigned(NFSV4OP_GETATTR);
1154 NFSGETATTR_ATTRBIT(&attrbits);
1155 (void) nfsrv_putattrbit(nd, &attrbits);
1157 error = nfscl_request(nd, vp, p, cred, stuff);
1160 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4))
1161 error = nfscl_wcc_data(nd, vp, rnap, attrflagp, NULL, stuff);
1162 if ((nd->nd_flag & ND_NFSV4) && !error)
1163 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
1164 if (!(nd->nd_flag & ND_NFSV3) && !nd->nd_repstat && !error)
1165 error = nfscl_postop_attr(nd, rnap, attrflagp, stuff);
1166 mbuf_freem(nd->nd_mrep);
1167 if (nd->nd_repstat && !error)
1168 error = nd->nd_repstat;
1176 nfsrpc_lookup(vnode_t dvp, char *name, int len, struct ucred *cred,
1177 NFSPROC_T *p, struct nfsvattr *dnap, struct nfsvattr *nap,
1178 struct nfsfh **nfhpp, int *attrflagp, int *dattrflagp, void *stuff)
1181 struct nfsrv_descript nfsd, *nd = &nfsd;
1182 struct nfsmount *nmp;
1185 nfsattrbit_t attrbits;
1186 int error = 0, lookupp = 0;
1190 if (vnode_vtype(dvp) != VDIR)
1192 nmp = VFSTONFS(vnode_mount(dvp));
1193 if (len > NFS_MAXNAMLEN)
1194 return (ENAMETOOLONG);
1195 if (NFSHASNFSV4(nmp) && len == 1 &&
1198 * Just return the current dir's fh.
1201 MALLOC(nfhp, struct nfsfh *, sizeof (struct nfsfh) +
1202 np->n_fhp->nfh_len, M_NFSFH, M_WAITOK);
1203 nfhp->nfh_len = np->n_fhp->nfh_len;
1204 NFSBCOPY(np->n_fhp->nfh_fh, nfhp->nfh_fh, nfhp->nfh_len);
1208 if (NFSHASNFSV4(nmp) && len == 2 &&
1209 name[0] == '.' && name[1] == '.') {
1211 NFSCL_REQSTART(nd, NFSPROC_LOOKUPP, dvp);
1213 NFSCL_REQSTART(nd, NFSPROC_LOOKUP, dvp);
1214 (void) nfsm_strtom(nd, name, len);
1216 if (nd->nd_flag & ND_NFSV4) {
1217 NFSGETATTR_ATTRBIT(&attrbits);
1218 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1219 *tl++ = txdr_unsigned(NFSV4OP_GETFH);
1220 *tl = txdr_unsigned(NFSV4OP_GETATTR);
1221 (void) nfsrv_putattrbit(nd, &attrbits);
1223 error = nfscl_request(nd, dvp, p, cred, stuff);
1226 if (nd->nd_repstat) {
1228 * When an NFSv4 Lookupp returns ENOENT, it means that
1229 * the lookup is at the root of an fs, so return this dir.
1231 if (nd->nd_repstat == NFSERR_NOENT && lookupp) {
1233 MALLOC(nfhp, struct nfsfh *, sizeof (struct nfsfh) +
1234 np->n_fhp->nfh_len, M_NFSFH, M_WAITOK);
1235 nfhp->nfh_len = np->n_fhp->nfh_len;
1236 NFSBCOPY(np->n_fhp->nfh_fh, nfhp->nfh_fh, nfhp->nfh_len);
1238 mbuf_freem(nd->nd_mrep);
1241 if (nd->nd_flag & ND_NFSV3)
1242 error = nfscl_postop_attr(nd, dnap, dattrflagp, stuff);
1243 else if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) ==
1245 /* Load the directory attributes. */
1246 error = nfsm_loadattr(nd, dnap);
1252 if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) == ND_NFSV4) {
1253 /* Load the directory attributes. */
1254 error = nfsm_loadattr(nd, dnap);
1258 /* Skip over the Lookup and GetFH operation status values. */
1259 NFSM_DISSECT(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1261 error = nfsm_getfh(nd, nfhpp);
1265 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
1266 if ((nd->nd_flag & ND_NFSV3) && !error)
1267 error = nfscl_postop_attr(nd, dnap, dattrflagp, stuff);
1269 mbuf_freem(nd->nd_mrep);
1270 if (!error && nd->nd_repstat)
1271 error = nd->nd_repstat;
1276 * Do a readlink rpc.
1279 nfsrpc_readlink(vnode_t vp, struct uio *uiop, struct ucred *cred,
1280 NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, void *stuff)
1283 struct nfsrv_descript nfsd, *nd = &nfsd;
1284 struct nfsnode *np = VTONFS(vp);
1285 nfsattrbit_t attrbits;
1286 int error, len, cangetattr = 1;
1289 NFSCL_REQSTART(nd, NFSPROC_READLINK, vp);
1290 if (nd->nd_flag & ND_NFSV4) {
1292 * And do a Getattr op.
1294 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1295 *tl = txdr_unsigned(NFSV4OP_GETATTR);
1296 NFSGETATTR_ATTRBIT(&attrbits);
1297 (void) nfsrv_putattrbit(nd, &attrbits);
1299 error = nfscl_request(nd, vp, p, cred, stuff);
1302 if (nd->nd_flag & ND_NFSV3)
1303 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
1304 if (!nd->nd_repstat && !error) {
1305 NFSM_STRSIZ(len, NFS_MAXPATHLEN);
1307 * This seems weird to me, but must have been added to
1308 * FreeBSD for some reason. The only thing I can think of
1309 * is that there was/is some server that replies with
1310 * more link data than it should?
1312 if (len == NFS_MAXPATHLEN) {
1314 if (np->n_size > 0 && np->n_size < NFS_MAXPATHLEN) {
1320 error = nfsm_mbufuio(nd, uiop, len);
1321 if ((nd->nd_flag & ND_NFSV4) && !error && cangetattr)
1322 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
1324 if (nd->nd_repstat && !error)
1325 error = nd->nd_repstat;
1327 mbuf_freem(nd->nd_mrep);
1335 nfsrpc_read(vnode_t vp, struct uio *uiop, struct ucred *cred,
1336 NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, void *stuff)
1338 int error, expireret = 0, retrycnt;
1339 u_int32_t clidrev = 0;
1340 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
1341 struct nfsnode *np = VTONFS(vp);
1342 struct ucred *newcred;
1343 struct nfsfh *nfhp = NULL;
1344 nfsv4stateid_t stateid;
1347 if (nmp->nm_clp != NULL)
1348 clidrev = nmp->nm_clp->nfsc_clientidrev;
1350 if (NFSHASNFSV4(nmp)) {
1352 newcred = NFSNEWCRED(cred);
1357 if (NFSHASNFSV4(nmp))
1358 (void)nfscl_getstateid(vp, nfhp->nfh_fh, nfhp->nfh_len,
1359 NFSV4OPEN_ACCESSREAD, 0, newcred, p, &stateid,
1361 error = nfsrpc_readrpc(vp, uiop, newcred, &stateid, p, nap,
1363 if (error == NFSERR_STALESTATEID || error == NFSERR_BADSESSION)
1364 nfscl_initiate_recovery(nmp->nm_clp);
1366 nfscl_lockderef(lckp);
1367 if (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
1368 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
1369 error == NFSERR_OLDSTATEID || error == NFSERR_BADSESSION) {
1370 (void) nfs_catnap(PZERO, error, "nfs_read");
1371 } else if ((error == NFSERR_EXPIRED ||
1372 error == NFSERR_BADSTATEID) && clidrev != 0) {
1373 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
1376 } while (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
1377 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
1378 error == NFSERR_BADSESSION ||
1379 (error == NFSERR_OLDSTATEID && retrycnt < 20) ||
1380 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
1381 expireret == 0 && clidrev != 0 && retrycnt < 4));
1382 if (error && retrycnt >= 4)
1384 if (NFSHASNFSV4(nmp))
1385 NFSFREECRED(newcred);
1390 * The actual read RPC.
1393 nfsrpc_readrpc(vnode_t vp, struct uio *uiop, struct ucred *cred,
1394 nfsv4stateid_t *stateidp, NFSPROC_T *p, struct nfsvattr *nap,
1395 int *attrflagp, void *stuff)
1398 int error = 0, len, retlen, tsiz, eof = 0;
1399 struct nfsrv_descript nfsd;
1400 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
1401 struct nfsrv_descript *nd = &nfsd;
1406 tsiz = uio_uio_resid(uiop);
1407 tmp_off = uiop->uio_offset + tsiz;
1409 if (tmp_off > nmp->nm_maxfilesize || tmp_off < uiop->uio_offset) {
1413 rsize = nmp->nm_rsize;
1418 len = (tsiz > rsize) ? rsize : tsiz;
1419 NFSCL_REQSTART(nd, NFSPROC_READ, vp);
1420 if (nd->nd_flag & ND_NFSV4)
1421 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID);
1422 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED * 3);
1423 if (nd->nd_flag & ND_NFSV2) {
1424 *tl++ = txdr_unsigned(uiop->uio_offset);
1425 *tl++ = txdr_unsigned(len);
1428 txdr_hyper(uiop->uio_offset, tl);
1429 *(tl + 2) = txdr_unsigned(len);
1432 * Since I can't do a Getattr for NFSv4 for Write, there
1433 * doesn't seem any point in doing one here, either.
1434 * (See the comment in nfsrpc_writerpc() for more info.)
1436 error = nfscl_request(nd, vp, p, cred, stuff);
1439 if (nd->nd_flag & ND_NFSV3) {
1440 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
1441 } else if (!nd->nd_repstat && (nd->nd_flag & ND_NFSV2)) {
1442 error = nfsm_loadattr(nd, nap);
1446 if (nd->nd_repstat || error) {
1448 error = nd->nd_repstat;
1451 if (nd->nd_flag & ND_NFSV3) {
1452 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1453 eof = fxdr_unsigned(int, *(tl + 1));
1454 } else if (nd->nd_flag & ND_NFSV4) {
1455 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
1456 eof = fxdr_unsigned(int, *tl);
1458 NFSM_STRSIZ(retlen, len);
1459 error = nfsm_mbufuio(nd, uiop, retlen);
1462 mbuf_freem(nd->nd_mrep);
1465 if (!(nd->nd_flag & ND_NFSV2)) {
1466 if (eof || retlen == 0)
1468 } else if (retlen < len)
1473 if (nd->nd_mrep != NULL)
1474 mbuf_freem(nd->nd_mrep);
1479 * nfs write operation
1480 * When called_from_strategy != 0, it should return EIO for an error that
1481 * indicates recovery is in progress, so that the buffer will be left
1482 * dirty and be written back to the server later. If it loops around,
1483 * the recovery thread could get stuck waiting for the buffer and recovery
1484 * will then deadlock.
1487 nfsrpc_write(vnode_t vp, struct uio *uiop, int *iomode, int *must_commit,
1488 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp,
1489 void *stuff, int called_from_strategy)
1491 int error, expireret = 0, retrycnt, nostateid;
1492 u_int32_t clidrev = 0;
1493 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
1494 struct nfsnode *np = VTONFS(vp);
1495 struct ucred *newcred;
1496 struct nfsfh *nfhp = NULL;
1497 nfsv4stateid_t stateid;
1501 if (nmp->nm_clp != NULL)
1502 clidrev = nmp->nm_clp->nfsc_clientidrev;
1504 if (NFSHASNFSV4(nmp)) {
1505 newcred = NFSNEWCRED(cred);
1512 if (NFSHASNFSV4(nmp)) {
1513 (void)nfscl_getstateid(vp, nfhp->nfh_fh, nfhp->nfh_len,
1514 NFSV4OPEN_ACCESSWRITE, 0, newcred, p, &stateid,
1516 if (stateid.other[0] == 0 && stateid.other[1] == 0 &&
1517 stateid.other[2] == 0) {
1519 NFSCL_DEBUG(1, "stateid0 in write\n");
1524 * If there is no stateid for NFSv4, it means this is an
1525 * extraneous write after close. Basically a poorly
1526 * implemented buffer cache. Just don't do the write.
1531 error = nfsrpc_writerpc(vp, uiop, iomode, must_commit,
1532 newcred, &stateid, p, nap, attrflagp, stuff);
1533 if (error == NFSERR_STALESTATEID || error == NFSERR_BADSESSION)
1534 nfscl_initiate_recovery(nmp->nm_clp);
1536 nfscl_lockderef(lckp);
1537 if (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
1538 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
1539 error == NFSERR_OLDSTATEID || error == NFSERR_BADSESSION) {
1540 (void) nfs_catnap(PZERO, error, "nfs_write");
1541 } else if ((error == NFSERR_EXPIRED ||
1542 error == NFSERR_BADSTATEID) && clidrev != 0) {
1543 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
1546 } while (error == NFSERR_GRACE || error == NFSERR_DELAY ||
1547 ((error == NFSERR_STALESTATEID || error == NFSERR_BADSESSION ||
1548 error == NFSERR_STALEDONTRECOVER) && called_from_strategy == 0) ||
1549 (error == NFSERR_OLDSTATEID && retrycnt < 20) ||
1550 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
1551 expireret == 0 && clidrev != 0 && retrycnt < 4));
1552 if (error != 0 && (retrycnt >= 4 ||
1553 ((error == NFSERR_STALESTATEID || error == NFSERR_BADSESSION ||
1554 error == NFSERR_STALEDONTRECOVER) && called_from_strategy != 0)))
1556 if (NFSHASNFSV4(nmp))
1557 NFSFREECRED(newcred);
1562 * The actual write RPC.
1565 nfsrpc_writerpc(vnode_t vp, struct uio *uiop, int *iomode,
1566 int *must_commit, struct ucred *cred, nfsv4stateid_t *stateidp,
1567 NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, void *stuff)
1570 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
1571 struct nfsnode *np = VTONFS(vp);
1572 int error = 0, len, tsiz, rlen, commit, committed = NFSWRITE_FILESYNC;
1573 int wccflag = 0, wsize;
1575 struct nfsrv_descript nfsd;
1576 struct nfsrv_descript *nd = &nfsd;
1577 nfsattrbit_t attrbits;
1580 KASSERT(uiop->uio_iovcnt == 1, ("nfs: writerpc iovcnt > 1"));
1582 tsiz = uio_uio_resid(uiop);
1583 tmp_off = uiop->uio_offset + tsiz;
1585 if (tmp_off > nmp->nm_maxfilesize || tmp_off < uiop->uio_offset) {
1589 wsize = nmp->nm_wsize;
1591 nd->nd_mrep = NULL; /* NFSv2 sometimes does a write with */
1592 nd->nd_repstat = 0; /* uio_resid == 0, so the while is not done */
1595 len = (tsiz > wsize) ? wsize : tsiz;
1596 NFSCL_REQSTART(nd, NFSPROC_WRITE, vp);
1597 if (nd->nd_flag & ND_NFSV4) {
1598 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID);
1599 NFSM_BUILD(tl, u_int32_t *, NFSX_HYPER+2*NFSX_UNSIGNED);
1600 txdr_hyper(uiop->uio_offset, tl);
1602 *tl++ = txdr_unsigned(*iomode);
1603 *tl = txdr_unsigned(len);
1604 } else if (nd->nd_flag & ND_NFSV3) {
1605 NFSM_BUILD(tl, u_int32_t *, NFSX_HYPER+3*NFSX_UNSIGNED);
1606 txdr_hyper(uiop->uio_offset, tl);
1608 *tl++ = txdr_unsigned(len);
1609 *tl++ = txdr_unsigned(*iomode);
1610 *tl = txdr_unsigned(len);
1614 NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1616 * Not sure why someone changed this, since the
1617 * RFC clearly states that "beginoffset" and
1618 * "totalcount" are ignored, but it wouldn't
1619 * surprise me if there's a busted server out there.
1621 /* Set both "begin" and "current" to non-garbage. */
1622 x = txdr_unsigned((u_int32_t)uiop->uio_offset);
1623 *tl++ = x; /* "begin offset" */
1624 *tl++ = x; /* "current offset" */
1625 x = txdr_unsigned(len);
1626 *tl++ = x; /* total to this offset */
1627 *tl = x; /* size of this write */
1630 nfsm_uiombuf(nd, uiop, len);
1632 * Although it is tempting to do a normal Getattr Op in the
1633 * NFSv4 compound, the result can be a nearly hung client
1634 * system if the Getattr asks for Owner and/or OwnerGroup.
1635 * It occurs when the client can't map either the Owner or
1636 * Owner_group name in the Getattr reply to a uid/gid. When
1637 * there is a cache miss, the kernel does an upcall to the
1638 * nfsuserd. Then, it can try and read the local /etc/passwd
1639 * or /etc/group file. It can then block in getnewbuf(),
1640 * waiting for dirty writes to be pushed to the NFS server.
1641 * The only reason this doesn't result in a complete
1642 * deadlock, is that the upcall times out and allows
1643 * the write to complete. However, progress is so slow
1644 * that it might just as well be deadlocked.
1645 * As such, we get the rest of the attributes, but not
1646 * Owner or Owner_group.
1647 * nb: nfscl_loadattrcache() needs to be told that these
1648 * partial attributes from a write rpc are being
1649 * passed in, via a argument flag.
1651 if (nd->nd_flag & ND_NFSV4) {
1652 NFSWRITEGETATTR_ATTRBIT(&attrbits);
1653 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1654 *tl = txdr_unsigned(NFSV4OP_GETATTR);
1655 (void) nfsrv_putattrbit(nd, &attrbits);
1657 error = nfscl_request(nd, vp, p, cred, stuff);
1660 if (nd->nd_repstat) {
1662 * In case the rpc gets retried, roll
1663 * the uio fileds changed by nfsm_uiombuf()
1666 uiop->uio_offset -= len;
1667 uio_uio_resid_add(uiop, len);
1668 uio_iov_base_add(uiop, -len);
1669 uio_iov_len_add(uiop, len);
1671 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) {
1672 error = nfscl_wcc_data(nd, vp, nap, attrflagp,
1677 if (!nd->nd_repstat) {
1678 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) {
1679 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED
1681 rlen = fxdr_unsigned(int, *tl++);
1685 } else if (rlen < len) {
1686 backup = len - rlen;
1687 uio_iov_base_add(uiop, -(backup));
1688 uio_iov_len_add(uiop, backup);
1689 uiop->uio_offset -= backup;
1690 uio_uio_resid_add(uiop, backup);
1693 commit = fxdr_unsigned(int, *tl++);
1696 * Return the lowest committment level
1697 * obtained by any of the RPCs.
1699 if (committed == NFSWRITE_FILESYNC)
1701 else if (committed == NFSWRITE_DATASYNC &&
1702 commit == NFSWRITE_UNSTABLE)
1705 if (!NFSHASWRITEVERF(nmp)) {
1706 NFSBCOPY((caddr_t)tl,
1707 (caddr_t)&nmp->nm_verf[0],
1709 NFSSETWRITEVERF(nmp);
1710 } else if (NFSBCMP(tl, nmp->nm_verf,
1713 NFSBCOPY(tl, nmp->nm_verf, NFSX_VERF);
1717 if (nd->nd_flag & ND_NFSV4)
1718 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1719 if (nd->nd_flag & (ND_NFSV2 | ND_NFSV4)) {
1720 error = nfsm_loadattr(nd, nap);
1722 *attrflagp = NFS_LATTR_NOSHRINK;
1725 error = nd->nd_repstat;
1729 NFSWRITERPC_SETTIME(wccflag, np, (nd->nd_flag & ND_NFSV4));
1730 mbuf_freem(nd->nd_mrep);
1735 if (nd->nd_mrep != NULL)
1736 mbuf_freem(nd->nd_mrep);
1737 *iomode = committed;
1738 if (nd->nd_repstat && !error)
1739 error = nd->nd_repstat;
1745 * For NFS v2 this is a kludge. Use a create rpc but with the IFMT bits of the
1746 * mode set to specify the file type and the size field for rdev.
1749 nfsrpc_mknod(vnode_t dvp, char *name, int namelen, struct vattr *vap,
1750 u_int32_t rdev, enum vtype vtyp, struct ucred *cred, NFSPROC_T *p,
1751 struct nfsvattr *dnap, struct nfsvattr *nnap, struct nfsfh **nfhpp,
1752 int *attrflagp, int *dattrflagp, void *dstuff)
1756 struct nfsrv_descript nfsd, *nd = &nfsd;
1757 nfsattrbit_t attrbits;
1762 if (namelen > NFS_MAXNAMLEN)
1763 return (ENAMETOOLONG);
1764 NFSCL_REQSTART(nd, NFSPROC_MKNOD, dvp);
1765 if (nd->nd_flag & ND_NFSV4) {
1766 if (vtyp == VBLK || vtyp == VCHR) {
1767 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
1768 *tl++ = vtonfsv34_type(vtyp);
1769 *tl++ = txdr_unsigned(NFSMAJOR(rdev));
1770 *tl = txdr_unsigned(NFSMINOR(rdev));
1772 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1773 *tl = vtonfsv34_type(vtyp);
1776 (void) nfsm_strtom(nd, name, namelen);
1777 if (nd->nd_flag & ND_NFSV3) {
1778 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1779 *tl = vtonfsv34_type(vtyp);
1781 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4))
1782 nfscl_fillsattr(nd, vap, dvp, 0, 0);
1783 if ((nd->nd_flag & ND_NFSV3) &&
1784 (vtyp == VCHR || vtyp == VBLK)) {
1785 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1786 *tl++ = txdr_unsigned(NFSMAJOR(rdev));
1787 *tl = txdr_unsigned(NFSMINOR(rdev));
1789 if (nd->nd_flag & ND_NFSV4) {
1790 NFSGETATTR_ATTRBIT(&attrbits);
1791 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1792 *tl++ = txdr_unsigned(NFSV4OP_GETFH);
1793 *tl = txdr_unsigned(NFSV4OP_GETATTR);
1794 (void) nfsrv_putattrbit(nd, &attrbits);
1796 if (nd->nd_flag & ND_NFSV2)
1797 nfscl_fillsattr(nd, vap, dvp, NFSSATTR_SIZERDEV, rdev);
1798 error = nfscl_request(nd, dvp, p, cred, dstuff);
1801 if (nd->nd_flag & ND_NFSV4)
1802 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
1803 if (!nd->nd_repstat) {
1804 if (nd->nd_flag & ND_NFSV4) {
1805 NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
1806 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
1810 error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp);
1814 if (nd->nd_flag & ND_NFSV3)
1815 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
1816 if (!error && nd->nd_repstat)
1817 error = nd->nd_repstat;
1819 mbuf_freem(nd->nd_mrep);
1824 * nfs file create call
1825 * Mostly just call the approriate routine. (I separated out v4, so that
1826 * error recovery wouldn't be as difficult.)
1829 nfsrpc_create(vnode_t dvp, char *name, int namelen, struct vattr *vap,
1830 nfsquad_t cverf, int fmode, struct ucred *cred, NFSPROC_T *p,
1831 struct nfsvattr *dnap, struct nfsvattr *nnap, struct nfsfh **nfhpp,
1832 int *attrflagp, int *dattrflagp, void *dstuff)
1834 int error = 0, newone, expireret = 0, retrycnt, unlocked;
1835 struct nfsclowner *owp;
1836 struct nfscldeleg *dp;
1837 struct nfsmount *nmp = VFSTONFS(vnode_mount(dvp));
1840 if (NFSHASNFSV4(nmp)) {
1844 error = nfscl_open(dvp, NULL, 0, (NFSV4OPEN_ACCESSWRITE |
1845 NFSV4OPEN_ACCESSREAD), 0, cred, p, &owp, NULL, &newone,
1849 if (nmp->nm_clp != NULL)
1850 clidrev = nmp->nm_clp->nfsc_clientidrev;
1853 error = nfsrpc_createv4(dvp, name, namelen, vap, cverf, fmode,
1854 owp, &dp, cred, p, dnap, nnap, nfhpp, attrflagp, dattrflagp,
1857 * There is no need to invalidate cached attributes here,
1858 * since new post-delegation issue attributes are always
1859 * returned by nfsrpc_createv4() and these will update the
1863 (void) nfscl_deleg(nmp->nm_mountp, owp->nfsow_clp,
1864 (*nfhpp)->nfh_fh, (*nfhpp)->nfh_len, cred, p, &dp);
1865 nfscl_ownerrelease(owp, error, newone, unlocked);
1866 if (error == NFSERR_GRACE || error == NFSERR_STALECLIENTID ||
1867 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
1868 error == NFSERR_BADSESSION) {
1869 (void) nfs_catnap(PZERO, error, "nfs_open");
1870 } else if ((error == NFSERR_EXPIRED ||
1871 error == NFSERR_BADSTATEID) && clidrev != 0) {
1872 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
1875 } while (error == NFSERR_GRACE || error == NFSERR_STALECLIENTID ||
1876 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
1877 error == NFSERR_BADSESSION ||
1878 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
1879 expireret == 0 && clidrev != 0 && retrycnt < 4));
1880 if (error && retrycnt >= 4)
1883 error = nfsrpc_createv23(dvp, name, namelen, vap, cverf,
1884 fmode, cred, p, dnap, nnap, nfhpp, attrflagp, dattrflagp,
1891 * The create rpc for v2 and 3.
1894 nfsrpc_createv23(vnode_t dvp, char *name, int namelen, struct vattr *vap,
1895 nfsquad_t cverf, int fmode, struct ucred *cred, NFSPROC_T *p,
1896 struct nfsvattr *dnap, struct nfsvattr *nnap, struct nfsfh **nfhpp,
1897 int *attrflagp, int *dattrflagp, void *dstuff)
1901 struct nfsrv_descript nfsd, *nd = &nfsd;
1906 if (namelen > NFS_MAXNAMLEN)
1907 return (ENAMETOOLONG);
1908 NFSCL_REQSTART(nd, NFSPROC_CREATE, dvp);
1909 (void) nfsm_strtom(nd, name, namelen);
1910 if (nd->nd_flag & ND_NFSV3) {
1911 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1912 if (fmode & O_EXCL) {
1913 *tl = txdr_unsigned(NFSCREATE_EXCLUSIVE);
1914 NFSM_BUILD(tl, u_int32_t *, NFSX_VERF);
1915 *tl++ = cverf.lval[0];
1916 *tl = cverf.lval[1];
1918 *tl = txdr_unsigned(NFSCREATE_UNCHECKED);
1919 nfscl_fillsattr(nd, vap, dvp, 0, 0);
1922 nfscl_fillsattr(nd, vap, dvp, NFSSATTR_SIZE0, 0);
1924 error = nfscl_request(nd, dvp, p, cred, dstuff);
1927 if (nd->nd_repstat == 0) {
1928 error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp);
1932 if (nd->nd_flag & ND_NFSV3)
1933 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
1934 if (nd->nd_repstat != 0 && error == 0)
1935 error = nd->nd_repstat;
1937 mbuf_freem(nd->nd_mrep);
1942 nfsrpc_createv4(vnode_t dvp, char *name, int namelen, struct vattr *vap,
1943 nfsquad_t cverf, int fmode, struct nfsclowner *owp, struct nfscldeleg **dpp,
1944 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap,
1945 struct nfsvattr *nnap, struct nfsfh **nfhpp, int *attrflagp,
1946 int *dattrflagp, void *dstuff, int *unlockedp)
1949 int error = 0, deleg, newone, ret, acesize, limitby;
1950 struct nfsrv_descript nfsd, *nd = &nfsd;
1951 struct nfsclopen *op;
1952 struct nfscldeleg *dp = NULL;
1955 nfsattrbit_t attrbits;
1956 nfsv4stateid_t stateid;
1958 struct nfsmount *nmp;
1960 nmp = VFSTONFS(dvp->v_mount);
1967 if (namelen > NFS_MAXNAMLEN)
1968 return (ENAMETOOLONG);
1969 NFSCL_REQSTART(nd, NFSPROC_CREATE, dvp);
1971 * For V4, this is actually an Open op.
1973 NFSM_BUILD(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
1974 *tl++ = txdr_unsigned(owp->nfsow_seqid);
1975 *tl++ = txdr_unsigned(NFSV4OPEN_ACCESSWRITE |
1976 NFSV4OPEN_ACCESSREAD);
1977 *tl++ = txdr_unsigned(NFSV4OPEN_DENYNONE);
1978 *tl++ = NFSMNT_MDSSESSION(nmp)->nfsess_clientid.lval[0];
1979 *tl = NFSMNT_MDSSESSION(nmp)->nfsess_clientid.lval[1];
1980 (void) nfsm_strtom(nd, owp->nfsow_owner, NFSV4CL_LOCKNAMELEN);
1981 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1982 *tl++ = txdr_unsigned(NFSV4OPEN_CREATE);
1983 if (fmode & O_EXCL) {
1984 if (NFSHASNFSV4N(nmp)) {
1985 if (NFSHASSESSPERSIST(nmp)) {
1986 /* Use GUARDED for persistent sessions. */
1987 *tl = txdr_unsigned(NFSCREATE_GUARDED);
1988 nfscl_fillsattr(nd, vap, dvp, 0, 0);
1990 /* Otherwise, use EXCLUSIVE4_1. */
1991 *tl = txdr_unsigned(NFSCREATE_EXCLUSIVE41);
1992 NFSM_BUILD(tl, u_int32_t *, NFSX_VERF);
1993 *tl++ = cverf.lval[0];
1994 *tl = cverf.lval[1];
1995 nfscl_fillsattr(nd, vap, dvp, 0, 0);
1999 *tl = txdr_unsigned(NFSCREATE_EXCLUSIVE);
2000 NFSM_BUILD(tl, u_int32_t *, NFSX_VERF);
2001 *tl++ = cverf.lval[0];
2002 *tl = cverf.lval[1];
2005 *tl = txdr_unsigned(NFSCREATE_UNCHECKED);
2006 nfscl_fillsattr(nd, vap, dvp, 0, 0);
2008 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2009 *tl = txdr_unsigned(NFSV4OPEN_CLAIMNULL);
2010 (void) nfsm_strtom(nd, name, namelen);
2011 /* Get the new file's handle and attributes. */
2012 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2013 *tl++ = txdr_unsigned(NFSV4OP_GETFH);
2014 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2015 NFSGETATTR_ATTRBIT(&attrbits);
2016 (void) nfsrv_putattrbit(nd, &attrbits);
2017 /* Get the directory's post-op attributes. */
2018 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2019 *tl = txdr_unsigned(NFSV4OP_PUTFH);
2020 (void) nfsm_fhtom(nd, np->n_fhp->nfh_fh, np->n_fhp->nfh_len, 0);
2021 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2022 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2023 (void) nfsrv_putattrbit(nd, &attrbits);
2024 error = nfscl_request(nd, dvp, p, cred, dstuff);
2027 NFSCL_INCRSEQID(owp->nfsow_seqid, nd);
2028 if (nd->nd_repstat == 0) {
2029 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
2031 stateid.seqid = *tl++;
2032 stateid.other[0] = *tl++;
2033 stateid.other[1] = *tl++;
2034 stateid.other[2] = *tl;
2035 rflags = fxdr_unsigned(u_int32_t, *(tl + 6));
2036 (void) nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
2037 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2038 deleg = fxdr_unsigned(int, *tl);
2039 if (deleg == NFSV4OPEN_DELEGATEREAD ||
2040 deleg == NFSV4OPEN_DELEGATEWRITE) {
2041 if (!(owp->nfsow_clp->nfsc_flags &
2042 NFSCLFLAGS_FIRSTDELEG))
2043 owp->nfsow_clp->nfsc_flags |=
2044 (NFSCLFLAGS_FIRSTDELEG | NFSCLFLAGS_GOTDELEG);
2045 MALLOC(dp, struct nfscldeleg *,
2046 sizeof (struct nfscldeleg) + NFSX_V4FHMAX,
2047 M_NFSCLDELEG, M_WAITOK);
2048 LIST_INIT(&dp->nfsdl_owner);
2049 LIST_INIT(&dp->nfsdl_lock);
2050 dp->nfsdl_clp = owp->nfsow_clp;
2051 newnfs_copyincred(cred, &dp->nfsdl_cred);
2052 nfscl_lockinit(&dp->nfsdl_rwlock);
2053 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
2055 dp->nfsdl_stateid.seqid = *tl++;
2056 dp->nfsdl_stateid.other[0] = *tl++;
2057 dp->nfsdl_stateid.other[1] = *tl++;
2058 dp->nfsdl_stateid.other[2] = *tl++;
2059 ret = fxdr_unsigned(int, *tl);
2060 if (deleg == NFSV4OPEN_DELEGATEWRITE) {
2061 dp->nfsdl_flags = NFSCLDL_WRITE;
2063 * Indicates how much the file can grow.
2065 NFSM_DISSECT(tl, u_int32_t *,
2067 limitby = fxdr_unsigned(int, *tl++);
2069 case NFSV4OPEN_LIMITSIZE:
2070 dp->nfsdl_sizelimit = fxdr_hyper(tl);
2072 case NFSV4OPEN_LIMITBLOCKS:
2073 dp->nfsdl_sizelimit =
2074 fxdr_unsigned(u_int64_t, *tl++);
2075 dp->nfsdl_sizelimit *=
2076 fxdr_unsigned(u_int64_t, *tl);
2079 error = NFSERR_BADXDR;
2083 dp->nfsdl_flags = NFSCLDL_READ;
2086 dp->nfsdl_flags |= NFSCLDL_RECALL;
2087 error = nfsrv_dissectace(nd, &dp->nfsdl_ace, &ret,
2091 } else if (deleg != NFSV4OPEN_DELEGATENONE) {
2092 error = NFSERR_BADXDR;
2095 error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp);
2098 /* Get rid of the PutFH and Getattr status values. */
2099 NFSM_DISSECT(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
2100 /* Load the directory attributes. */
2101 error = nfsm_loadattr(nd, dnap);
2105 if (dp != NULL && *attrflagp) {
2106 dp->nfsdl_change = nnap->na_filerev;
2107 dp->nfsdl_modtime = nnap->na_mtime;
2108 dp->nfsdl_flags |= NFSCLDL_MODTIMESET;
2111 * We can now complete the Open state.
2115 dp->nfsdl_fhlen = nfhp->nfh_len;
2116 NFSBCOPY(nfhp->nfh_fh, dp->nfsdl_fh, nfhp->nfh_len);
2119 * Get an Open structure that will be
2120 * attached to the OpenOwner, acquired already.
2122 error = nfscl_open(dvp, nfhp->nfh_fh, nfhp->nfh_len,
2123 (NFSV4OPEN_ACCESSWRITE | NFSV4OPEN_ACCESSREAD), 0,
2124 cred, p, NULL, &op, &newone, NULL, 0);
2127 op->nfso_stateid = stateid;
2128 newnfs_copyincred(cred, &op->nfso_cred);
2129 if ((rflags & NFSV4OPEN_RESULTCONFIRM)) {
2131 ret = nfsrpc_openconfirm(dvp, nfhp->nfh_fh,
2132 nfhp->nfh_len, op, cred, p);
2133 if (ret == NFSERR_DELAY)
2134 (void) nfs_catnap(PZERO, ret, "nfs_create");
2135 } while (ret == NFSERR_DELAY);
2140 * If the server is handing out delegations, but we didn't
2141 * get one because an OpenConfirm was required, try the
2142 * Open again, to get a delegation. This is a harmless no-op,
2143 * from a server's point of view.
2145 if ((rflags & NFSV4OPEN_RESULTCONFIRM) &&
2146 (owp->nfsow_clp->nfsc_flags & NFSCLFLAGS_GOTDELEG) &&
2147 !error && dp == NULL) {
2149 ret = nfsrpc_openrpc(VFSTONFS(vnode_mount(dvp)), dvp,
2150 np->n_fhp->nfh_fh, np->n_fhp->nfh_len,
2151 nfhp->nfh_fh, nfhp->nfh_len,
2152 (NFSV4OPEN_ACCESSWRITE | NFSV4OPEN_ACCESSREAD), op,
2153 name, namelen, &dp, 0, 0x0, cred, p, 0, 1);
2154 if (ret == NFSERR_DELAY)
2155 (void) nfs_catnap(PZERO, ret, "nfs_crt2");
2156 } while (ret == NFSERR_DELAY);
2159 FREE((caddr_t)dp, M_NFSCLDELEG);
2162 if (ret == NFSERR_STALECLIENTID ||
2163 ret == NFSERR_STALEDONTRECOVER ||
2164 ret == NFSERR_BADSESSION)
2168 nfscl_openrelease(op, error, newone);
2171 if (nd->nd_repstat != 0 && error == 0)
2172 error = nd->nd_repstat;
2173 if (error == NFSERR_STALECLIENTID || error == NFSERR_BADSESSION)
2174 nfscl_initiate_recovery(owp->nfsow_clp);
2178 else if (dp != NULL)
2179 FREE((caddr_t)dp, M_NFSCLDELEG);
2180 mbuf_freem(nd->nd_mrep);
2188 nfsrpc_remove(vnode_t dvp, char *name, int namelen, vnode_t vp,
2189 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap, int *dattrflagp,
2193 struct nfsrv_descript nfsd, *nd = &nfsd;
2195 struct nfsmount *nmp;
2196 nfsv4stateid_t dstateid;
2197 int error, ret = 0, i;
2200 if (namelen > NFS_MAXNAMLEN)
2201 return (ENAMETOOLONG);
2202 nmp = VFSTONFS(vnode_mount(dvp));
2204 if (NFSHASNFSV4(nmp) && ret == 0) {
2205 ret = nfscl_removedeleg(vp, p, &dstateid);
2207 NFSCL_REQSTART(nd, NFSPROC_RETDELEGREMOVE, vp);
2208 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID +
2210 if (NFSHASNFSV4N(nmp))
2213 *tl++ = dstateid.seqid;
2214 *tl++ = dstateid.other[0];
2215 *tl++ = dstateid.other[1];
2216 *tl++ = dstateid.other[2];
2217 *tl = txdr_unsigned(NFSV4OP_PUTFH);
2219 (void) nfsm_fhtom(nd, np->n_fhp->nfh_fh,
2220 np->n_fhp->nfh_len, 0);
2221 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2222 *tl = txdr_unsigned(NFSV4OP_REMOVE);
2228 NFSCL_REQSTART(nd, NFSPROC_REMOVE, dvp);
2229 (void) nfsm_strtom(nd, name, namelen);
2230 error = nfscl_request(nd, dvp, p, cred, dstuff);
2233 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) {
2234 /* For NFSv4, parse out any Delereturn replies. */
2235 if (ret > 0 && nd->nd_repstat != 0 &&
2236 (nd->nd_flag & ND_NOMOREDATA)) {
2238 * If the Delegreturn failed, try again without
2239 * it. The server will Recall, as required.
2241 mbuf_freem(nd->nd_mrep);
2244 for (i = 0; i < (ret * 2); i++) {
2245 if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) ==
2247 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2249 nd->nd_flag |= ND_NOMOREDATA;
2252 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
2254 if (nd->nd_repstat && !error)
2255 error = nd->nd_repstat;
2257 mbuf_freem(nd->nd_mrep);
2262 * Do an nfs rename rpc.
2265 nfsrpc_rename(vnode_t fdvp, vnode_t fvp, char *fnameptr, int fnamelen,
2266 vnode_t tdvp, vnode_t tvp, char *tnameptr, int tnamelen, struct ucred *cred,
2267 NFSPROC_T *p, struct nfsvattr *fnap, struct nfsvattr *tnap,
2268 int *fattrflagp, int *tattrflagp, void *fstuff, void *tstuff)
2271 struct nfsrv_descript nfsd, *nd = &nfsd;
2272 struct nfsmount *nmp;
2274 nfsattrbit_t attrbits;
2275 nfsv4stateid_t fdstateid, tdstateid;
2276 int error = 0, ret = 0, gottd = 0, gotfd = 0, i;
2280 nmp = VFSTONFS(vnode_mount(fdvp));
2281 if (fnamelen > NFS_MAXNAMLEN || tnamelen > NFS_MAXNAMLEN)
2282 return (ENAMETOOLONG);
2284 if (NFSHASNFSV4(nmp) && ret == 0) {
2285 ret = nfscl_renamedeleg(fvp, &fdstateid, &gotfd, tvp,
2286 &tdstateid, &gottd, p);
2287 if (gotfd && gottd) {
2288 NFSCL_REQSTART(nd, NFSPROC_RETDELEGRENAME2, fvp);
2290 NFSCL_REQSTART(nd, NFSPROC_RETDELEGRENAME1, fvp);
2292 NFSCL_REQSTART(nd, NFSPROC_RETDELEGRENAME1, tvp);
2295 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID);
2296 if (NFSHASNFSV4N(nmp))
2299 *tl++ = fdstateid.seqid;
2300 *tl++ = fdstateid.other[0];
2301 *tl++ = fdstateid.other[1];
2302 *tl = fdstateid.other[2];
2304 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2305 *tl = txdr_unsigned(NFSV4OP_PUTFH);
2307 (void) nfsm_fhtom(nd, np->n_fhp->nfh_fh,
2308 np->n_fhp->nfh_len, 0);
2309 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2310 *tl = txdr_unsigned(NFSV4OP_DELEGRETURN);
2314 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID);
2315 if (NFSHASNFSV4N(nmp))
2318 *tl++ = tdstateid.seqid;
2319 *tl++ = tdstateid.other[0];
2320 *tl++ = tdstateid.other[1];
2321 *tl = tdstateid.other[2];
2324 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2325 *tl = txdr_unsigned(NFSV4OP_PUTFH);
2327 (void) nfsm_fhtom(nd, np->n_fhp->nfh_fh,
2328 np->n_fhp->nfh_len, 0);
2329 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2330 *tl = txdr_unsigned(NFSV4OP_SAVEFH);
2336 NFSCL_REQSTART(nd, NFSPROC_RENAME, fdvp);
2337 if (nd->nd_flag & ND_NFSV4) {
2338 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2339 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2340 NFSWCCATTR_ATTRBIT(&attrbits);
2341 (void) nfsrv_putattrbit(nd, &attrbits);
2342 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2343 *tl = txdr_unsigned(NFSV4OP_PUTFH);
2344 (void) nfsm_fhtom(nd, VTONFS(tdvp)->n_fhp->nfh_fh,
2345 VTONFS(tdvp)->n_fhp->nfh_len, 0);
2346 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2347 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2348 (void) nfsrv_putattrbit(nd, &attrbits);
2349 nd->nd_flag |= ND_V4WCCATTR;
2350 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2351 *tl = txdr_unsigned(NFSV4OP_RENAME);
2353 (void) nfsm_strtom(nd, fnameptr, fnamelen);
2354 if (!(nd->nd_flag & ND_NFSV4))
2355 (void) nfsm_fhtom(nd, VTONFS(tdvp)->n_fhp->nfh_fh,
2356 VTONFS(tdvp)->n_fhp->nfh_len, 0);
2357 (void) nfsm_strtom(nd, tnameptr, tnamelen);
2358 error = nfscl_request(nd, fdvp, p, cred, fstuff);
2361 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) {
2362 /* For NFSv4, parse out any Delereturn replies. */
2363 if (ret > 0 && nd->nd_repstat != 0 &&
2364 (nd->nd_flag & ND_NOMOREDATA)) {
2366 * If the Delegreturn failed, try again without
2367 * it. The server will Recall, as required.
2369 mbuf_freem(nd->nd_mrep);
2372 for (i = 0; i < (ret * 2); i++) {
2373 if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) ==
2375 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2377 if (i == 0 && ret > 1) {
2379 * If the Delegreturn failed, try again
2380 * without it. The server will Recall, as
2382 * If ret > 1, the first iteration of this
2383 * loop is the second DelegReturn result.
2385 mbuf_freem(nd->nd_mrep);
2388 nd->nd_flag |= ND_NOMOREDATA;
2393 /* Now, the first wcc attribute reply. */
2394 if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) == ND_NFSV4) {
2395 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2397 nd->nd_flag |= ND_NOMOREDATA;
2399 error = nfscl_wcc_data(nd, fdvp, fnap, fattrflagp, NULL,
2401 /* and the second wcc attribute reply. */
2402 if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) == ND_NFSV4 &&
2404 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2406 nd->nd_flag |= ND_NOMOREDATA;
2409 error = nfscl_wcc_data(nd, tdvp, tnap, tattrflagp,
2412 if (nd->nd_repstat && !error)
2413 error = nd->nd_repstat;
2415 mbuf_freem(nd->nd_mrep);
2420 * nfs hard link create rpc
2423 nfsrpc_link(vnode_t dvp, vnode_t vp, char *name, int namelen,
2424 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap,
2425 struct nfsvattr *nap, int *attrflagp, int *dattrflagp, void *dstuff)
2428 struct nfsrv_descript nfsd, *nd = &nfsd;
2429 nfsattrbit_t attrbits;
2434 if (namelen > NFS_MAXNAMLEN)
2435 return (ENAMETOOLONG);
2436 NFSCL_REQSTART(nd, NFSPROC_LINK, vp);
2437 if (nd->nd_flag & ND_NFSV4) {
2438 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2439 *tl = txdr_unsigned(NFSV4OP_PUTFH);
2441 (void) nfsm_fhtom(nd, VTONFS(dvp)->n_fhp->nfh_fh,
2442 VTONFS(dvp)->n_fhp->nfh_len, 0);
2443 if (nd->nd_flag & ND_NFSV4) {
2444 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2445 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2446 NFSWCCATTR_ATTRBIT(&attrbits);
2447 (void) nfsrv_putattrbit(nd, &attrbits);
2448 nd->nd_flag |= ND_V4WCCATTR;
2449 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2450 *tl = txdr_unsigned(NFSV4OP_LINK);
2452 (void) nfsm_strtom(nd, name, namelen);
2453 error = nfscl_request(nd, vp, p, cred, dstuff);
2456 if (nd->nd_flag & ND_NFSV3) {
2457 error = nfscl_postop_attr(nd, nap, attrflagp, dstuff);
2459 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp,
2461 } else if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) == ND_NFSV4) {
2463 * First, parse out the PutFH and Getattr result.
2465 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2467 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2469 nd->nd_flag |= ND_NOMOREDATA;
2471 * Get the pre-op attributes.
2473 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
2475 if (nd->nd_repstat && !error)
2476 error = nd->nd_repstat;
2478 mbuf_freem(nd->nd_mrep);
2483 * nfs symbolic link create rpc
2486 nfsrpc_symlink(vnode_t dvp, char *name, int namelen, char *target,
2487 struct vattr *vap, struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap,
2488 struct nfsvattr *nnap, struct nfsfh **nfhpp, int *attrflagp,
2489 int *dattrflagp, void *dstuff)
2492 struct nfsrv_descript nfsd, *nd = &nfsd;
2493 struct nfsmount *nmp;
2494 int slen, error = 0;
2499 nmp = VFSTONFS(vnode_mount(dvp));
2500 slen = strlen(target);
2501 if (slen > NFS_MAXPATHLEN || namelen > NFS_MAXNAMLEN)
2502 return (ENAMETOOLONG);
2503 NFSCL_REQSTART(nd, NFSPROC_SYMLINK, dvp);
2504 if (nd->nd_flag & ND_NFSV4) {
2505 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2506 *tl = txdr_unsigned(NFLNK);
2507 (void) nfsm_strtom(nd, target, slen);
2509 (void) nfsm_strtom(nd, name, namelen);
2510 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4))
2511 nfscl_fillsattr(nd, vap, dvp, 0, 0);
2512 if (!(nd->nd_flag & ND_NFSV4))
2513 (void) nfsm_strtom(nd, target, slen);
2514 if (nd->nd_flag & ND_NFSV2)
2515 nfscl_fillsattr(nd, vap, dvp, NFSSATTR_SIZENEG1, 0);
2516 error = nfscl_request(nd, dvp, p, cred, dstuff);
2519 if (nd->nd_flag & ND_NFSV4)
2520 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
2521 if ((nd->nd_flag & ND_NFSV3) && !error) {
2522 if (!nd->nd_repstat)
2523 error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp);
2525 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp,
2528 if (nd->nd_repstat && !error)
2529 error = nd->nd_repstat;
2530 mbuf_freem(nd->nd_mrep);
2532 * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry.
2534 if (error == EEXIST)
2543 nfsrpc_mkdir(vnode_t dvp, char *name, int namelen, struct vattr *vap,
2544 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap,
2545 struct nfsvattr *nnap, struct nfsfh **nfhpp, int *attrflagp,
2546 int *dattrflagp, void *dstuff)
2549 struct nfsrv_descript nfsd, *nd = &nfsd;
2550 nfsattrbit_t attrbits;
2557 fhp = VTONFS(dvp)->n_fhp;
2558 if (namelen > NFS_MAXNAMLEN)
2559 return (ENAMETOOLONG);
2560 NFSCL_REQSTART(nd, NFSPROC_MKDIR, dvp);
2561 if (nd->nd_flag & ND_NFSV4) {
2562 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2563 *tl = txdr_unsigned(NFDIR);
2565 (void) nfsm_strtom(nd, name, namelen);
2566 nfscl_fillsattr(nd, vap, dvp, NFSSATTR_SIZENEG1, 0);
2567 if (nd->nd_flag & ND_NFSV4) {
2568 NFSGETATTR_ATTRBIT(&attrbits);
2569 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2570 *tl++ = txdr_unsigned(NFSV4OP_GETFH);
2571 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2572 (void) nfsrv_putattrbit(nd, &attrbits);
2573 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2574 *tl = txdr_unsigned(NFSV4OP_PUTFH);
2575 (void) nfsm_fhtom(nd, fhp->nfh_fh, fhp->nfh_len, 0);
2576 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2577 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2578 (void) nfsrv_putattrbit(nd, &attrbits);
2580 error = nfscl_request(nd, dvp, p, cred, dstuff);
2583 if (nd->nd_flag & ND_NFSV4)
2584 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
2585 if (!nd->nd_repstat && !error) {
2586 if (nd->nd_flag & ND_NFSV4) {
2587 NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
2588 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
2591 error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp);
2592 if (error == 0 && (nd->nd_flag & ND_NFSV4) != 0) {
2593 /* Get rid of the PutFH and Getattr status values. */
2594 NFSM_DISSECT(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
2595 /* Load the directory attributes. */
2596 error = nfsm_loadattr(nd, dnap);
2601 if ((nd->nd_flag & ND_NFSV3) && !error)
2602 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
2603 if (nd->nd_repstat && !error)
2604 error = nd->nd_repstat;
2606 mbuf_freem(nd->nd_mrep);
2608 * Kludge: Map EEXIST => 0 assuming that you have a reply to a retry.
2610 if (error == EEXIST)
2616 * nfs remove directory call
2619 nfsrpc_rmdir(vnode_t dvp, char *name, int namelen, struct ucred *cred,
2620 NFSPROC_T *p, struct nfsvattr *dnap, int *dattrflagp, void *dstuff)
2622 struct nfsrv_descript nfsd, *nd = &nfsd;
2626 if (namelen > NFS_MAXNAMLEN)
2627 return (ENAMETOOLONG);
2628 NFSCL_REQSTART(nd, NFSPROC_RMDIR, dvp);
2629 (void) nfsm_strtom(nd, name, namelen);
2630 error = nfscl_request(nd, dvp, p, cred, dstuff);
2633 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4))
2634 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
2635 if (nd->nd_repstat && !error)
2636 error = nd->nd_repstat;
2637 mbuf_freem(nd->nd_mrep);
2639 * Kludge: Map ENOENT => 0 assuming that you have a reply to a retry.
2641 if (error == ENOENT)
2648 * Always returns with either uio_resid unchanged, if you are at the
2649 * end of the directory, or uio_resid == 0, with all DIRBLKSIZ chunks
2651 * I felt this would allow caching of directory blocks more easily
2652 * than returning a pertially filled block.
2653 * Directory offset cookies:
2654 * Oh my, what to do with them...
2655 * I can think of three ways to deal with them:
2656 * 1 - have the layer above these RPCs maintain a map between logical
2657 * directory byte offsets and the NFS directory offset cookies
2658 * 2 - pass the opaque directory offset cookies up into userland
2659 * and let the libc functions deal with them, via the system call
2660 * 3 - return them to userland in the "struct dirent", so future versions
2661 * of libc can use them and do whatever is necessary to amke things work
2662 * above these rpc calls, in the meantime
2663 * For now, I do #3 by "hiding" the directory offset cookies after the
2664 * d_name field in struct dirent. This is space inside d_reclen that
2665 * will be ignored by anything that doesn't know about them.
2666 * The directory offset cookies are filled in as the last 8 bytes of
2667 * each directory entry, after d_name. Someday, the userland libc
2668 * functions may be able to use these. In the meantime, it satisfies
2669 * OpenBSD's requirements for cookies being returned.
2670 * If expects the directory offset cookie for the read to be in uio_offset
2671 * and returns the one for the next entry after this directory block in
2675 nfsrpc_readdir(vnode_t vp, struct uio *uiop, nfsuint64 *cookiep,
2676 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp,
2677 int *eofp, void *stuff)
2680 struct dirent *dp = NULL;
2682 nfsquad_t cookie, ncookie;
2683 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
2684 struct nfsnode *dnp = VTONFS(vp);
2685 struct nfsvattr nfsva;
2686 struct nfsrv_descript nfsd, *nd = &nfsd;
2687 int error = 0, tlen, more_dirs = 1, blksiz = 0, bigenough = 1;
2688 int reqsize, tryformoredirs = 1, readsize, eof = 0, gotmnton = 0;
2689 long dotfileid, dotdotfileid = 0;
2690 u_int32_t fakefileno = 0xffffffff, rderr;
2692 nfsattrbit_t attrbits, dattrbits;
2693 u_int32_t *tl2 = NULL;
2696 KASSERT(uiop->uio_iovcnt == 1 &&
2697 (uio_uio_resid(uiop) & (DIRBLKSIZ - 1)) == 0,
2698 ("nfs readdirrpc bad uio"));
2701 * There is no point in reading a lot more than uio_resid, however
2702 * adding one additional DIRBLKSIZ makes sense. Since uio_resid
2703 * and nm_readdirsize are both exact multiples of DIRBLKSIZ, this
2704 * will never make readsize > nm_readdirsize.
2706 readsize = nmp->nm_readdirsize;
2707 if (readsize > uio_uio_resid(uiop))
2708 readsize = uio_uio_resid(uiop) + DIRBLKSIZ;
2713 tresid = uio_uio_resid(uiop);
2714 cookie.lval[0] = cookiep->nfsuquad[0];
2715 cookie.lval[1] = cookiep->nfsuquad[1];
2719 * For NFSv4, first create the "." and ".." entries.
2721 if (NFSHASNFSV4(nmp)) {
2722 reqsize = 6 * NFSX_UNSIGNED;
2723 NFSGETATTR_ATTRBIT(&dattrbits);
2724 NFSZERO_ATTRBIT(&attrbits);
2725 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_FILEID);
2726 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TYPE);
2727 if (NFSISSET_ATTRBIT(&dnp->n_vattr.na_suppattr,
2728 NFSATTRBIT_MOUNTEDONFILEID)) {
2729 NFSSETBIT_ATTRBIT(&attrbits,
2730 NFSATTRBIT_MOUNTEDONFILEID);
2734 * Must fake it. Use the fileno, except when the
2735 * fsid is != to that of the directory. For that
2736 * case, generate a fake fileno that is not the same.
2738 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_FSID);
2743 * Joy, oh joy. For V4 we get to hand craft '.' and '..'.
2745 if (uiop->uio_offset == 0) {
2746 NFSCL_REQSTART(nd, NFSPROC_LOOKUPP, vp);
2747 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2748 *tl++ = txdr_unsigned(NFSV4OP_GETFH);
2749 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2750 (void) nfsrv_putattrbit(nd, &attrbits);
2751 error = nfscl_request(nd, vp, p, cred, stuff);
2754 dotfileid = 0; /* Fake out the compiler. */
2755 if ((nd->nd_flag & ND_NOMOREDATA) == 0) {
2756 error = nfsm_loadattr(nd, &nfsva);
2759 dotfileid = nfsva.na_fileid;
2761 if (nd->nd_repstat == 0) {
2762 NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
2763 len = fxdr_unsigned(int, *(tl + 4));
2764 if (len > 0 && len <= NFSX_V4FHMAX)
2765 error = nfsm_advance(nd, NFSM_RNDUP(len), -1);
2769 NFSM_DISSECT(tl, u_int32_t *, 2*NFSX_UNSIGNED);
2770 nfsva.na_mntonfileno = 0xffffffff;
2771 error = nfsv4_loadattr(nd, NULL, &nfsva, NULL,
2772 NULL, 0, NULL, NULL, NULL, NULL, NULL, 0,
2773 NULL, NULL, NULL, p, cred);
2775 dotdotfileid = dotfileid;
2776 } else if (gotmnton) {
2777 if (nfsva.na_mntonfileno != 0xffffffff)
2778 dotdotfileid = nfsva.na_mntonfileno;
2780 dotdotfileid = nfsva.na_fileid;
2781 } else if (nfsva.na_filesid[0] ==
2782 dnp->n_vattr.na_filesid[0] &&
2783 nfsva.na_filesid[1] ==
2784 dnp->n_vattr.na_filesid[1]) {
2785 dotdotfileid = nfsva.na_fileid;
2789 } while (fakefileno ==
2791 dotdotfileid = fakefileno;
2794 } else if (nd->nd_repstat == NFSERR_NOENT) {
2796 * Lookupp returns NFSERR_NOENT when we are
2797 * at the root, so just use the current dir.
2800 dotdotfileid = dotfileid;
2802 error = nd->nd_repstat;
2804 mbuf_freem(nd->nd_mrep);
2808 dp = (struct dirent *) CAST_DOWN(caddr_t, uio_iov_base(uiop));
2809 dp->d_type = DT_DIR;
2810 dp->d_fileno = dotfileid;
2812 dp->d_name[0] = '.';
2813 dp->d_name[1] = '\0';
2814 dp->d_reclen = DIRENT_SIZE(dp) + NFSX_HYPER;
2816 * Just make these offset cookie 0.
2818 tl = (u_int32_t *)&dp->d_name[4];
2821 blksiz += dp->d_reclen;
2822 uio_uio_resid_add(uiop, -(dp->d_reclen));
2823 uiop->uio_offset += dp->d_reclen;
2824 uio_iov_base_add(uiop, dp->d_reclen);
2825 uio_iov_len_add(uiop, -(dp->d_reclen));
2826 dp = (struct dirent *) CAST_DOWN(caddr_t, uio_iov_base(uiop));
2827 dp->d_type = DT_DIR;
2828 dp->d_fileno = dotdotfileid;
2830 dp->d_name[0] = '.';
2831 dp->d_name[1] = '.';
2832 dp->d_name[2] = '\0';
2833 dp->d_reclen = DIRENT_SIZE(dp) + NFSX_HYPER;
2835 * Just make these offset cookie 0.
2837 tl = (u_int32_t *)&dp->d_name[4];
2840 blksiz += dp->d_reclen;
2841 uio_uio_resid_add(uiop, -(dp->d_reclen));
2842 uiop->uio_offset += dp->d_reclen;
2843 uio_iov_base_add(uiop, dp->d_reclen);
2844 uio_iov_len_add(uiop, -(dp->d_reclen));
2846 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_RDATTRERROR);
2848 reqsize = 5 * NFSX_UNSIGNED;
2853 * Loop around doing readdir rpc's of size readsize.
2854 * The stopping criteria is EOF or buffer full.
2856 while (more_dirs && bigenough) {
2858 NFSCL_REQSTART(nd, NFSPROC_READDIR, vp);
2859 if (nd->nd_flag & ND_NFSV2) {
2860 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2861 *tl++ = cookie.lval[1];
2862 *tl = txdr_unsigned(readsize);
2864 NFSM_BUILD(tl, u_int32_t *, reqsize);
2865 *tl++ = cookie.lval[0];
2866 *tl++ = cookie.lval[1];
2867 if (cookie.qval == 0) {
2872 *tl++ = dnp->n_cookieverf.nfsuquad[0];
2873 *tl++ = dnp->n_cookieverf.nfsuquad[1];
2876 if (nd->nd_flag & ND_NFSV4) {
2877 *tl++ = txdr_unsigned(readsize);
2878 *tl = txdr_unsigned(readsize);
2879 (void) nfsrv_putattrbit(nd, &attrbits);
2880 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2881 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2882 (void) nfsrv_putattrbit(nd, &dattrbits);
2884 *tl = txdr_unsigned(readsize);
2887 error = nfscl_request(nd, vp, p, cred, stuff);
2890 if (!(nd->nd_flag & ND_NFSV2)) {
2891 if (nd->nd_flag & ND_NFSV3)
2892 error = nfscl_postop_attr(nd, nap, attrflagp,
2894 if (!nd->nd_repstat && !error) {
2895 NFSM_DISSECT(tl, u_int32_t *, NFSX_HYPER);
2897 dnp->n_cookieverf.nfsuquad[0] = *tl++;
2898 dnp->n_cookieverf.nfsuquad[1] = *tl;
2902 if (nd->nd_repstat || error) {
2904 error = nd->nd_repstat;
2907 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2908 more_dirs = fxdr_unsigned(int, *tl);
2912 /* loop thru the dir entries, doctoring them to 4bsd form */
2913 while (more_dirs && bigenough) {
2914 if (nd->nd_flag & ND_NFSV4) {
2915 NFSM_DISSECT(tl, u_int32_t *, 3*NFSX_UNSIGNED);
2916 ncookie.lval[0] = *tl++;
2917 ncookie.lval[1] = *tl++;
2918 len = fxdr_unsigned(int, *tl);
2919 } else if (nd->nd_flag & ND_NFSV3) {
2920 NFSM_DISSECT(tl, u_int32_t *, 3*NFSX_UNSIGNED);
2921 nfsva.na_fileid = fxdr_hyper(tl);
2923 len = fxdr_unsigned(int, *tl);
2925 NFSM_DISSECT(tl, u_int32_t *, 2*NFSX_UNSIGNED);
2927 fxdr_unsigned(long, *tl++);
2928 len = fxdr_unsigned(int, *tl);
2930 if (len <= 0 || len > NFS_MAXNAMLEN) {
2934 tlen = NFSM_RNDUP(len);
2936 tlen += 4; /* To ensure null termination */
2937 left = DIRBLKSIZ - blksiz;
2938 if ((int)(tlen + DIRHDSIZ + NFSX_HYPER) > left) {
2939 dp->d_reclen += left;
2940 uio_iov_base_add(uiop, left);
2941 uio_iov_len_add(uiop, -(left));
2942 uio_uio_resid_add(uiop, -(left));
2943 uiop->uio_offset += left;
2946 if ((int)(tlen + DIRHDSIZ + NFSX_HYPER) > uio_uio_resid(uiop))
2949 dp = (struct dirent *) CAST_DOWN(caddr_t, uio_iov_base(uiop));
2951 dp->d_reclen = tlen + DIRHDSIZ + NFSX_HYPER;
2952 dp->d_type = DT_UNKNOWN;
2953 blksiz += dp->d_reclen;
2954 if (blksiz == DIRBLKSIZ)
2956 uio_uio_resid_add(uiop, -(DIRHDSIZ));
2957 uiop->uio_offset += DIRHDSIZ;
2958 uio_iov_base_add(uiop, DIRHDSIZ);
2959 uio_iov_len_add(uiop, -(DIRHDSIZ));
2960 error = nfsm_mbufuio(nd, uiop, len);
2963 cp = CAST_DOWN(caddr_t, uio_iov_base(uiop));
2965 *cp = '\0'; /* null terminate */
2966 cp += tlen; /* points to cookie storage */
2967 tl2 = (u_int32_t *)cp;
2968 uio_iov_base_add(uiop, (tlen + NFSX_HYPER));
2969 uio_iov_len_add(uiop, -(tlen + NFSX_HYPER));
2970 uio_uio_resid_add(uiop, -(tlen + NFSX_HYPER));
2971 uiop->uio_offset += (tlen + NFSX_HYPER);
2973 error = nfsm_advance(nd, NFSM_RNDUP(len), -1);
2977 if (nd->nd_flag & ND_NFSV4) {
2979 nfsva.na_mntonfileno = 0xffffffff;
2980 error = nfsv4_loadattr(nd, NULL, &nfsva, NULL,
2981 NULL, 0, NULL, NULL, NULL, NULL, NULL, 0,
2982 NULL, NULL, &rderr, p, cred);
2985 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2986 } else if (nd->nd_flag & ND_NFSV3) {
2987 NFSM_DISSECT(tl, u_int32_t *, 3*NFSX_UNSIGNED);
2988 ncookie.lval[0] = *tl++;
2989 ncookie.lval[1] = *tl++;
2991 NFSM_DISSECT(tl, u_int32_t *, 2*NFSX_UNSIGNED);
2992 ncookie.lval[0] = 0;
2993 ncookie.lval[1] = *tl++;
2996 if (nd->nd_flag & ND_NFSV4) {
3001 if (nfsva.na_mntonfileno != 0xffffffff)
3002 dp->d_fileno = nfsva.na_mntonfileno;
3004 dp->d_fileno = nfsva.na_fileid;
3005 } else if (nfsva.na_filesid[0] ==
3006 dnp->n_vattr.na_filesid[0] &&
3007 nfsva.na_filesid[1] ==
3008 dnp->n_vattr.na_filesid[1]) {
3009 dp->d_fileno = nfsva.na_fileid;
3013 } while (fakefileno ==
3015 dp->d_fileno = fakefileno;
3017 dp->d_type = vtonfs_dtype(nfsva.na_type);
3020 dp->d_fileno = nfsva.na_fileid;
3022 *tl2++ = cookiep->nfsuquad[0] = cookie.lval[0] =
3024 *tl2 = cookiep->nfsuquad[1] = cookie.lval[1] =
3027 more_dirs = fxdr_unsigned(int, *tl);
3030 * If at end of rpc data, get the eof boolean
3033 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3034 eof = fxdr_unsigned(int, *tl);
3037 if (nd->nd_flag & ND_NFSV4) {
3038 error = nfscl_postop_attr(nd, nap, attrflagp,
3044 mbuf_freem(nd->nd_mrep);
3048 * Fill last record, iff any, out to a multiple of DIRBLKSIZ
3049 * by increasing d_reclen for the last record.
3052 left = DIRBLKSIZ - blksiz;
3053 dp->d_reclen += left;
3054 uio_iov_base_add(uiop, left);
3055 uio_iov_len_add(uiop, -(left));
3056 uio_uio_resid_add(uiop, -(left));
3057 uiop->uio_offset += left;
3061 * If returning no data, assume end of file.
3062 * If not bigenough, return not end of file, since you aren't
3063 * returning all the data
3064 * Otherwise, return the eof flag from the server.
3067 if (tresid == ((size_t)(uio_uio_resid(uiop))))
3069 else if (!bigenough)
3076 if (nd->nd_mrep != NULL)
3077 mbuf_freem(nd->nd_mrep);
3083 * NFS V3 readdir plus RPC. Used in place of nfsrpc_readdir().
3084 * (Also used for NFS V4 when mount flag set.)
3085 * (ditto above w.r.t. multiple of DIRBLKSIZ, etc.)
3088 nfsrpc_readdirplus(vnode_t vp, struct uio *uiop, nfsuint64 *cookiep,
3089 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp,
3090 int *eofp, void *stuff)
3093 struct dirent *dp = NULL;
3095 vnode_t newvp = NULLVP;
3096 struct nfsrv_descript nfsd, *nd = &nfsd;
3097 struct nameidata nami, *ndp = &nami;
3098 struct componentname *cnp = &ndp->ni_cnd;
3099 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
3100 struct nfsnode *dnp = VTONFS(vp), *np;
3101 struct nfsvattr nfsva;
3103 nfsquad_t cookie, ncookie;
3104 int error = 0, tlen, more_dirs = 1, blksiz = 0, bigenough = 1;
3105 int attrflag, tryformoredirs = 1, eof = 0, gotmnton = 0;
3106 int isdotdot = 0, unlocknewvp = 0;
3107 long dotfileid, dotdotfileid = 0, fileno = 0;
3109 nfsattrbit_t attrbits, dattrbits;
3111 u_int32_t *tl2 = NULL, fakefileno = 0xffffffff, rderr;
3112 struct timespec dctime;
3114 KASSERT(uiop->uio_iovcnt == 1 &&
3115 (uio_uio_resid(uiop) & (DIRBLKSIZ - 1)) == 0,
3116 ("nfs readdirplusrpc bad uio"));
3117 timespecclear(&dctime);
3123 cookie.lval[0] = cookiep->nfsuquad[0];
3124 cookie.lval[1] = cookiep->nfsuquad[1];
3125 tresid = uio_uio_resid(uiop);
3128 * For NFSv4, first create the "." and ".." entries.
3130 if (NFSHASNFSV4(nmp)) {
3131 NFSGETATTR_ATTRBIT(&dattrbits);
3132 NFSZERO_ATTRBIT(&attrbits);
3133 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_FILEID);
3134 if (NFSISSET_ATTRBIT(&dnp->n_vattr.na_suppattr,
3135 NFSATTRBIT_MOUNTEDONFILEID)) {
3136 NFSSETBIT_ATTRBIT(&attrbits,
3137 NFSATTRBIT_MOUNTEDONFILEID);
3141 * Must fake it. Use the fileno, except when the
3142 * fsid is != to that of the directory. For that
3143 * case, generate a fake fileno that is not the same.
3145 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_FSID);
3150 * Joy, oh joy. For V4 we get to hand craft '.' and '..'.
3152 if (uiop->uio_offset == 0) {
3153 NFSCL_REQSTART(nd, NFSPROC_LOOKUPP, vp);
3154 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
3155 *tl++ = txdr_unsigned(NFSV4OP_GETFH);
3156 *tl = txdr_unsigned(NFSV4OP_GETATTR);
3157 (void) nfsrv_putattrbit(nd, &attrbits);
3158 error = nfscl_request(nd, vp, p, cred, stuff);
3161 dotfileid = 0; /* Fake out the compiler. */
3162 if ((nd->nd_flag & ND_NOMOREDATA) == 0) {
3163 error = nfsm_loadattr(nd, &nfsva);
3166 dctime = nfsva.na_ctime;
3167 dotfileid = nfsva.na_fileid;
3169 if (nd->nd_repstat == 0) {
3170 NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
3171 len = fxdr_unsigned(int, *(tl + 4));
3172 if (len > 0 && len <= NFSX_V4FHMAX)
3173 error = nfsm_advance(nd, NFSM_RNDUP(len), -1);
3177 NFSM_DISSECT(tl, u_int32_t *, 2*NFSX_UNSIGNED);
3178 nfsva.na_mntonfileno = 0xffffffff;
3179 error = nfsv4_loadattr(nd, NULL, &nfsva, NULL,
3180 NULL, 0, NULL, NULL, NULL, NULL, NULL, 0,
3181 NULL, NULL, NULL, p, cred);
3183 dotdotfileid = dotfileid;
3184 } else if (gotmnton) {
3185 if (nfsva.na_mntonfileno != 0xffffffff)
3186 dotdotfileid = nfsva.na_mntonfileno;
3188 dotdotfileid = nfsva.na_fileid;
3189 } else if (nfsva.na_filesid[0] ==
3190 dnp->n_vattr.na_filesid[0] &&
3191 nfsva.na_filesid[1] ==
3192 dnp->n_vattr.na_filesid[1]) {
3193 dotdotfileid = nfsva.na_fileid;
3197 } while (fakefileno ==
3199 dotdotfileid = fakefileno;
3202 } else if (nd->nd_repstat == NFSERR_NOENT) {
3204 * Lookupp returns NFSERR_NOENT when we are
3205 * at the root, so just use the current dir.
3208 dotdotfileid = dotfileid;
3210 error = nd->nd_repstat;
3212 mbuf_freem(nd->nd_mrep);
3216 dp = (struct dirent *)uio_iov_base(uiop);
3217 dp->d_type = DT_DIR;
3218 dp->d_fileno = dotfileid;
3220 dp->d_name[0] = '.';
3221 dp->d_name[1] = '\0';
3222 dp->d_reclen = DIRENT_SIZE(dp) + NFSX_HYPER;
3224 * Just make these offset cookie 0.
3226 tl = (u_int32_t *)&dp->d_name[4];
3229 blksiz += dp->d_reclen;
3230 uio_uio_resid_add(uiop, -(dp->d_reclen));
3231 uiop->uio_offset += dp->d_reclen;
3232 uio_iov_base_add(uiop, dp->d_reclen);
3233 uio_iov_len_add(uiop, -(dp->d_reclen));
3234 dp = (struct dirent *)uio_iov_base(uiop);
3235 dp->d_type = DT_DIR;
3236 dp->d_fileno = dotdotfileid;
3238 dp->d_name[0] = '.';
3239 dp->d_name[1] = '.';
3240 dp->d_name[2] = '\0';
3241 dp->d_reclen = DIRENT_SIZE(dp) + NFSX_HYPER;
3243 * Just make these offset cookie 0.
3245 tl = (u_int32_t *)&dp->d_name[4];
3248 blksiz += dp->d_reclen;
3249 uio_uio_resid_add(uiop, -(dp->d_reclen));
3250 uiop->uio_offset += dp->d_reclen;
3251 uio_iov_base_add(uiop, dp->d_reclen);
3252 uio_iov_len_add(uiop, -(dp->d_reclen));
3254 NFSREADDIRPLUS_ATTRBIT(&attrbits);
3256 NFSSETBIT_ATTRBIT(&attrbits,
3257 NFSATTRBIT_MOUNTEDONFILEID);
3261 * Loop around doing readdir rpc's of size nm_readdirsize.
3262 * The stopping criteria is EOF or buffer full.
3264 while (more_dirs && bigenough) {
3266 NFSCL_REQSTART(nd, NFSPROC_READDIRPLUS, vp);
3267 NFSM_BUILD(tl, u_int32_t *, 6 * NFSX_UNSIGNED);
3268 *tl++ = cookie.lval[0];
3269 *tl++ = cookie.lval[1];
3270 if (cookie.qval == 0) {
3275 *tl++ = dnp->n_cookieverf.nfsuquad[0];
3276 *tl++ = dnp->n_cookieverf.nfsuquad[1];
3279 *tl++ = txdr_unsigned(nmp->nm_readdirsize);
3280 *tl = txdr_unsigned(nmp->nm_readdirsize);
3281 if (nd->nd_flag & ND_NFSV4) {
3282 (void) nfsrv_putattrbit(nd, &attrbits);
3283 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
3284 *tl = txdr_unsigned(NFSV4OP_GETATTR);
3285 (void) nfsrv_putattrbit(nd, &dattrbits);
3287 error = nfscl_request(nd, vp, p, cred, stuff);
3290 if (nd->nd_flag & ND_NFSV3)
3291 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
3292 if (nd->nd_repstat || error) {
3294 error = nd->nd_repstat;
3297 if ((nd->nd_flag & ND_NFSV3) != 0 && *attrflagp != 0)
3298 dctime = nap->na_ctime;
3299 NFSM_DISSECT(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
3301 dnp->n_cookieverf.nfsuquad[0] = *tl++;
3302 dnp->n_cookieverf.nfsuquad[1] = *tl++;
3304 more_dirs = fxdr_unsigned(int, *tl);
3308 /* loop thru the dir entries, doctoring them to 4bsd form */
3309 while (more_dirs && bigenough) {
3310 NFSM_DISSECT(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
3311 if (nd->nd_flag & ND_NFSV4) {
3312 ncookie.lval[0] = *tl++;
3313 ncookie.lval[1] = *tl++;
3315 fileno = fxdr_unsigned(long, *++tl);
3318 len = fxdr_unsigned(int, *tl);
3319 if (len <= 0 || len > NFS_MAXNAMLEN) {
3323 tlen = NFSM_RNDUP(len);
3325 tlen += 4; /* To ensure null termination */
3326 left = DIRBLKSIZ - blksiz;
3327 if ((tlen + DIRHDSIZ + NFSX_HYPER) > left) {
3328 dp->d_reclen += left;
3329 uio_iov_base_add(uiop, left);
3330 uio_iov_len_add(uiop, -(left));
3331 uio_uio_resid_add(uiop, -(left));
3332 uiop->uio_offset += left;
3335 if ((tlen + DIRHDSIZ + NFSX_HYPER) > uio_uio_resid(uiop))
3338 dp = (struct dirent *)uio_iov_base(uiop);
3340 dp->d_reclen = tlen + DIRHDSIZ + NFSX_HYPER;
3341 dp->d_type = DT_UNKNOWN;
3342 blksiz += dp->d_reclen;
3343 if (blksiz == DIRBLKSIZ)
3345 uio_uio_resid_add(uiop, -(DIRHDSIZ));
3346 uiop->uio_offset += DIRHDSIZ;
3347 uio_iov_base_add(uiop, DIRHDSIZ);
3348 uio_iov_len_add(uiop, -(DIRHDSIZ));
3349 cnp->cn_nameptr = uio_iov_base(uiop);
3350 cnp->cn_namelen = len;
3352 error = nfsm_mbufuio(nd, uiop, len);
3355 cp = uio_iov_base(uiop);
3358 cp += tlen; /* points to cookie storage */
3359 tl2 = (u_int32_t *)cp;
3360 if (len == 2 && cnp->cn_nameptr[0] == '.' &&
3361 cnp->cn_nameptr[1] == '.')
3365 uio_iov_base_add(uiop, (tlen + NFSX_HYPER));
3366 uio_iov_len_add(uiop, -(tlen + NFSX_HYPER));
3367 uio_uio_resid_add(uiop, -(tlen + NFSX_HYPER));
3368 uiop->uio_offset += (tlen + NFSX_HYPER);
3370 error = nfsm_advance(nd, NFSM_RNDUP(len), -1);
3375 if (nd->nd_flag & ND_NFSV3) {
3376 NFSM_DISSECT(tl, u_int32_t *, 3*NFSX_UNSIGNED);
3377 ncookie.lval[0] = *tl++;
3378 ncookie.lval[1] = *tl++;
3379 attrflag = fxdr_unsigned(int, *tl);
3381 error = nfsm_loadattr(nd, &nfsva);
3385 NFSM_DISSECT(tl,u_int32_t *,NFSX_UNSIGNED);
3387 error = nfsm_getfh(nd, &nfhp);
3391 if (!attrflag && nfhp != NULL) {
3392 FREE((caddr_t)nfhp, M_NFSFH);
3397 nfsva.na_mntonfileno = 0xffffffff;
3398 error = nfsv4_loadattr(nd, NULL, &nfsva, &nfhp,
3399 NULL, 0, NULL, NULL, NULL, NULL, NULL, 0,
3400 NULL, NULL, &rderr, p, cred);
3406 if (nd->nd_flag & ND_NFSV4) {
3409 } else if (gotmnton) {
3410 if (nfsva.na_mntonfileno != 0xffffffff)
3411 dp->d_fileno = nfsva.na_mntonfileno;
3413 dp->d_fileno = nfsva.na_fileid;
3414 } else if (nfsva.na_filesid[0] ==
3415 dnp->n_vattr.na_filesid[0] &&
3416 nfsva.na_filesid[1] ==
3417 dnp->n_vattr.na_filesid[1]) {
3418 dp->d_fileno = nfsva.na_fileid;
3422 } while (fakefileno ==
3424 dp->d_fileno = fakefileno;
3427 dp->d_fileno = fileno;
3429 *tl2++ = cookiep->nfsuquad[0] = cookie.lval[0] =
3431 *tl2 = cookiep->nfsuquad[1] = cookie.lval[1] =
3435 if (NFSRV_CMPFH(nfhp->nfh_fh, nfhp->nfh_len,
3436 dnp->n_fhp->nfh_fh, dnp->n_fhp->nfh_len)) {
3440 FREE((caddr_t)nfhp, M_NFSFH);
3442 } else if (isdotdot != 0) {
3444 * Skip doing a nfscl_nget() call for "..".
3445 * There's a race between acquiring the nfs
3446 * node here and lookups that look for the
3447 * directory being read (in the parent).
3448 * It would try to get a lock on ".." here,
3449 * owning the lock on the directory being
3450 * read. Lookup will hold the lock on ".."
3451 * and try to acquire the lock on the
3452 * directory being read.
3453 * If the directory is unlocked/relocked,
3454 * then there is a LOR with the buflock
3457 free(nfhp, M_NFSFH);
3459 error = nfscl_nget(vnode_mount(vp), vp,
3460 nfhp, cnp, p, &np, NULL, LK_EXCLUSIVE);
3467 if (newvp != NULLVP) {
3468 error = nfscl_loadattrcache(&newvp,
3469 &nfsva, NULL, NULL, 0, 0);
3478 vtonfs_dtype(np->n_vattr.na_type);
3480 NFSCNHASH(cnp, HASHINIT);
3481 if (cnp->cn_namelen <= NCHNAMLEN &&
3482 (newvp->v_type != VDIR ||
3483 dctime.tv_sec != 0)) {
3484 cache_enter_time(ndp->ni_dvp,
3487 newvp->v_type != VDIR ? NULL :
3497 } else if (nfhp != NULL) {
3498 FREE((caddr_t)nfhp, M_NFSFH);
3500 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3501 more_dirs = fxdr_unsigned(int, *tl);
3504 * If at end of rpc data, get the eof boolean
3507 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3508 eof = fxdr_unsigned(int, *tl);
3511 if (nd->nd_flag & ND_NFSV4) {
3512 error = nfscl_postop_attr(nd, nap, attrflagp,
3518 mbuf_freem(nd->nd_mrep);
3522 * Fill last record, iff any, out to a multiple of DIRBLKSIZ
3523 * by increasing d_reclen for the last record.
3526 left = DIRBLKSIZ - blksiz;
3527 dp->d_reclen += left;
3528 uio_iov_base_add(uiop, left);
3529 uio_iov_len_add(uiop, -(left));
3530 uio_uio_resid_add(uiop, -(left));
3531 uiop->uio_offset += left;
3535 * If returning no data, assume end of file.
3536 * If not bigenough, return not end of file, since you aren't
3537 * returning all the data
3538 * Otherwise, return the eof flag from the server.
3541 if (tresid == uio_uio_resid(uiop))
3543 else if (!bigenough)
3550 if (nd->nd_mrep != NULL)
3551 mbuf_freem(nd->nd_mrep);
3560 nfsrpc_commit(vnode_t vp, u_quad_t offset, int cnt, struct ucred *cred,
3561 NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, void *stuff)
3564 struct nfsrv_descript nfsd, *nd = &nfsd;
3565 nfsattrbit_t attrbits;
3567 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
3570 NFSCL_REQSTART(nd, NFSPROC_COMMIT, vp);
3571 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
3572 txdr_hyper(offset, tl);
3574 *tl = txdr_unsigned(cnt);
3575 if (nd->nd_flag & ND_NFSV4) {
3577 * And do a Getattr op.
3579 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
3580 *tl = txdr_unsigned(NFSV4OP_GETATTR);
3581 NFSGETATTR_ATTRBIT(&attrbits);
3582 (void) nfsrv_putattrbit(nd, &attrbits);
3584 error = nfscl_request(nd, vp, p, cred, stuff);
3587 error = nfscl_wcc_data(nd, vp, nap, attrflagp, NULL, stuff);
3588 if (!error && !nd->nd_repstat) {
3589 NFSM_DISSECT(tl, u_int32_t *, NFSX_VERF);
3591 if (NFSBCMP(nmp->nm_verf, tl, NFSX_VERF)) {
3592 NFSBCOPY(tl, nmp->nm_verf, NFSX_VERF);
3593 nd->nd_repstat = NFSERR_STALEWRITEVERF;
3596 if (nd->nd_flag & ND_NFSV4)
3597 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
3600 if (!error && nd->nd_repstat)
3601 error = nd->nd_repstat;
3602 mbuf_freem(nd->nd_mrep);
3607 * NFS byte range lock rpc.
3608 * (Mostly just calls one of the three lower level RPC routines.)
3611 nfsrpc_advlock(vnode_t vp, off_t size, int op, struct flock *fl,
3612 int reclaim, struct ucred *cred, NFSPROC_T *p, void *id, int flags)
3614 struct nfscllockowner *lp;
3615 struct nfsclclient *clp;
3617 struct nfsrv_descript nfsd, *nd = &nfsd;
3618 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
3621 u_int32_t clidrev = 0;
3622 int error = 0, newone = 0, expireret = 0, retrycnt, donelocally;
3626 * Convert the flock structure into a start and end and do POSIX
3629 switch (fl->l_whence) {
3633 * Caller is responsible for adding any necessary offset
3634 * when SEEK_CUR is used.
3636 start = fl->l_start;
3640 start = size + fl->l_start;
3641 off = size + fl->l_start;
3648 if (fl->l_len != 0) {
3649 end = start + fl->l_len - 1;
3660 if (op == F_GETLK) {
3661 error = nfscl_getcl(vnode_mount(vp), cred, p, 1, &clp);
3664 error = nfscl_lockt(vp, clp, off, len, fl, p, id, flags);
3666 clidrev = clp->nfsc_clientidrev;
3667 error = nfsrpc_lockt(nd, vp, clp, off, len, fl, cred,
3669 } else if (error == -1) {
3672 nfscl_clientrelease(clp);
3673 } else if (op == F_UNLCK && fl->l_type == F_UNLCK) {
3675 * We must loop around for all lockowner cases.
3678 error = nfscl_getcl(vnode_mount(vp), cred, p, 1, &clp);
3682 error = nfscl_relbytelock(vp, off, len, cred, p, callcnt,
3683 clp, id, flags, &lp, &dorpc);
3685 * If it returns a NULL lp, we're done.
3689 nfscl_clientrelease(clp);
3691 nfscl_releasealllocks(clp, vp, p, id, flags);
3694 if (nmp->nm_clp != NULL)
3695 clidrev = nmp->nm_clp->nfsc_clientidrev;
3699 * If the server doesn't support Posix lock semantics,
3700 * only allow locks on the entire file, since it won't
3701 * handle overlapping byte ranges.
3702 * There might still be a problem when a lock
3703 * upgrade/downgrade (read<->write) occurs, since the
3704 * server "might" expect an unlock first?
3706 if (dorpc && (lp->nfsl_open->nfso_posixlock ||
3707 (off == 0 && len == NFS64BITSSET))) {
3709 * Since the lock records will go away, we must
3710 * wait for grace and delay here.
3713 error = nfsrpc_locku(nd, nmp, lp, off, len,
3714 NFSV4LOCKT_READ, cred, p, 0);
3715 if ((nd->nd_repstat == NFSERR_GRACE ||
3716 nd->nd_repstat == NFSERR_DELAY) &&
3718 (void) nfs_catnap(PZERO, (int)nd->nd_repstat,
3720 } while ((nd->nd_repstat == NFSERR_GRACE ||
3721 nd->nd_repstat == NFSERR_DELAY) && error == 0);
3724 } while (error == 0 && nd->nd_repstat == 0);
3725 nfscl_releasealllocks(clp, vp, p, id, flags);
3726 } else if (op == F_SETLK) {
3727 error = nfscl_getbytelock(vp, off, len, fl->l_type, cred, p,
3728 NULL, 0, id, flags, NULL, NULL, &lp, &newone, &donelocally);
3729 if (error || donelocally) {
3732 if (nmp->nm_clp != NULL)
3733 clidrev = nmp->nm_clp->nfsc_clientidrev;
3736 nfhp = VTONFS(vp)->n_fhp;
3737 if (!lp->nfsl_open->nfso_posixlock &&
3738 (off != 0 || len != NFS64BITSSET)) {
3741 error = nfsrpc_lock(nd, nmp, vp, nfhp->nfh_fh,
3742 nfhp->nfh_len, lp, newone, reclaim, off,
3743 len, fl->l_type, cred, p, 0);
3746 error = nd->nd_repstat;
3747 nfscl_lockrelease(lp, error, newone);
3752 error = nd->nd_repstat;
3753 if (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
3754 error == NFSERR_STALEDONTRECOVER ||
3755 error == NFSERR_STALECLIENTID || error == NFSERR_DELAY ||
3756 error == NFSERR_BADSESSION) {
3757 (void) nfs_catnap(PZERO, error, "nfs_advlock");
3758 } else if ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID)
3760 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
3763 } while (error == NFSERR_GRACE ||
3764 error == NFSERR_STALECLIENTID || error == NFSERR_DELAY ||
3765 error == NFSERR_STALEDONTRECOVER || error == NFSERR_STALESTATEID ||
3766 error == NFSERR_BADSESSION ||
3767 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
3768 expireret == 0 && clidrev != 0 && retrycnt < 4));
3769 if (error && retrycnt >= 4)
3775 * The lower level routine for the LockT case.
3778 nfsrpc_lockt(struct nfsrv_descript *nd, vnode_t vp,
3779 struct nfsclclient *clp, u_int64_t off, u_int64_t len, struct flock *fl,
3780 struct ucred *cred, NFSPROC_T *p, void *id, int flags)
3783 int error, type, size;
3784 uint8_t own[NFSV4CL_LOCKNAMELEN + NFSX_V4FHMAX];
3786 struct nfsmount *nmp;
3788 nmp = VFSTONFS(vp->v_mount);
3789 NFSCL_REQSTART(nd, NFSPROC_LOCKT, vp);
3790 NFSM_BUILD(tl, u_int32_t *, 7 * NFSX_UNSIGNED);
3791 if (fl->l_type == F_RDLCK)
3792 *tl++ = txdr_unsigned(NFSV4LOCKT_READ);
3794 *tl++ = txdr_unsigned(NFSV4LOCKT_WRITE);
3795 txdr_hyper(off, tl);
3797 txdr_hyper(len, tl);
3799 *tl++ = NFSMNT_MDSSESSION(nmp)->nfsess_clientid.lval[0];
3800 *tl = NFSMNT_MDSSESSION(nmp)->nfsess_clientid.lval[1];
3801 nfscl_filllockowner(id, own, flags);
3803 NFSBCOPY(np->n_fhp->nfh_fh, &own[NFSV4CL_LOCKNAMELEN],
3804 np->n_fhp->nfh_len);
3805 (void)nfsm_strtom(nd, own, NFSV4CL_LOCKNAMELEN + np->n_fhp->nfh_len);
3806 error = nfscl_request(nd, vp, p, cred, NULL);
3809 if (nd->nd_repstat == 0) {
3810 fl->l_type = F_UNLCK;
3811 } else if (nd->nd_repstat == NFSERR_DENIED) {
3813 fl->l_whence = SEEK_SET;
3814 NFSM_DISSECT(tl, u_int32_t *, 8 * NFSX_UNSIGNED);
3815 fl->l_start = fxdr_hyper(tl);
3817 len = fxdr_hyper(tl);
3819 if (len == NFS64BITSSET)
3823 type = fxdr_unsigned(int, *tl++);
3824 if (type == NFSV4LOCKT_WRITE)
3825 fl->l_type = F_WRLCK;
3827 fl->l_type = F_RDLCK;
3829 * XXX For now, I have no idea what to do with the
3830 * conflicting lock_owner, so I'll just set the pid == 0
3831 * and skip over the lock_owner.
3833 fl->l_pid = (pid_t)0;
3835 size = fxdr_unsigned(int, *tl);
3836 if (size < 0 || size > NFSV4_OPAQUELIMIT)
3839 error = nfsm_advance(nd, NFSM_RNDUP(size), -1);
3840 } else if (nd->nd_repstat == NFSERR_STALECLIENTID ||
3841 nd->nd_repstat == NFSERR_BADSESSION)
3842 nfscl_initiate_recovery(clp);
3844 mbuf_freem(nd->nd_mrep);
3849 * Lower level function that performs the LockU RPC.
3852 nfsrpc_locku(struct nfsrv_descript *nd, struct nfsmount *nmp,
3853 struct nfscllockowner *lp, u_int64_t off, u_int64_t len,
3854 u_int32_t type, struct ucred *cred, NFSPROC_T *p, int syscred)
3859 nfscl_reqstart(nd, NFSPROC_LOCKU, nmp, lp->nfsl_open->nfso_fh,
3860 lp->nfsl_open->nfso_fhlen, NULL, NULL);
3861 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID + 6 * NFSX_UNSIGNED);
3862 *tl++ = txdr_unsigned(type);
3863 *tl = txdr_unsigned(lp->nfsl_seqid);
3864 if (nfstest_outofseq &&
3865 (arc4random() % nfstest_outofseq) == 0)
3866 *tl = txdr_unsigned(lp->nfsl_seqid + 1);
3868 if (NFSHASNFSV4N(nmp))
3871 *tl++ = lp->nfsl_stateid.seqid;
3872 *tl++ = lp->nfsl_stateid.other[0];
3873 *tl++ = lp->nfsl_stateid.other[1];
3874 *tl++ = lp->nfsl_stateid.other[2];
3875 txdr_hyper(off, tl);
3877 txdr_hyper(len, tl);
3879 nd->nd_flag |= ND_USEGSSNAME;
3880 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
3881 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
3882 NFSCL_INCRSEQID(lp->nfsl_seqid, nd);
3885 if (nd->nd_repstat == 0) {
3886 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID);
3887 lp->nfsl_stateid.seqid = *tl++;
3888 lp->nfsl_stateid.other[0] = *tl++;
3889 lp->nfsl_stateid.other[1] = *tl++;
3890 lp->nfsl_stateid.other[2] = *tl;
3891 } else if (nd->nd_repstat == NFSERR_STALESTATEID ||
3892 nd->nd_repstat == NFSERR_BADSESSION)
3893 nfscl_initiate_recovery(lp->nfsl_open->nfso_own->nfsow_clp);
3895 mbuf_freem(nd->nd_mrep);
3900 * The actual Lock RPC.
3903 nfsrpc_lock(struct nfsrv_descript *nd, struct nfsmount *nmp, vnode_t vp,
3904 u_int8_t *nfhp, int fhlen, struct nfscllockowner *lp, int newone,
3905 int reclaim, u_int64_t off, u_int64_t len, short type, struct ucred *cred,
3906 NFSPROC_T *p, int syscred)
3910 uint8_t own[NFSV4CL_LOCKNAMELEN + NFSX_V4FHMAX];
3912 nfscl_reqstart(nd, NFSPROC_LOCK, nmp, nfhp, fhlen, NULL, NULL);
3913 NFSM_BUILD(tl, u_int32_t *, 7 * NFSX_UNSIGNED);
3914 if (type == F_RDLCK)
3915 *tl++ = txdr_unsigned(NFSV4LOCKT_READ);
3917 *tl++ = txdr_unsigned(NFSV4LOCKT_WRITE);
3918 *tl++ = txdr_unsigned(reclaim);
3919 txdr_hyper(off, tl);
3921 txdr_hyper(len, tl);
3925 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID +
3926 2 * NFSX_UNSIGNED + NFSX_HYPER);
3927 *tl++ = txdr_unsigned(lp->nfsl_open->nfso_own->nfsow_seqid);
3928 if (NFSHASNFSV4N(nmp))
3931 *tl++ = lp->nfsl_open->nfso_stateid.seqid;
3932 *tl++ = lp->nfsl_open->nfso_stateid.other[0];
3933 *tl++ = lp->nfsl_open->nfso_stateid.other[1];
3934 *tl++ = lp->nfsl_open->nfso_stateid.other[2];
3935 *tl++ = txdr_unsigned(lp->nfsl_seqid);
3936 *tl++ = NFSMNT_MDSSESSION(nmp)->nfsess_clientid.lval[0];
3937 *tl = NFSMNT_MDSSESSION(nmp)->nfsess_clientid.lval[1];
3938 NFSBCOPY(lp->nfsl_owner, own, NFSV4CL_LOCKNAMELEN);
3939 NFSBCOPY(nfhp, &own[NFSV4CL_LOCKNAMELEN], fhlen);
3940 (void)nfsm_strtom(nd, own, NFSV4CL_LOCKNAMELEN + fhlen);
3943 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID + NFSX_UNSIGNED);
3944 if (NFSHASNFSV4N(nmp))
3947 *tl++ = lp->nfsl_stateid.seqid;
3948 *tl++ = lp->nfsl_stateid.other[0];
3949 *tl++ = lp->nfsl_stateid.other[1];
3950 *tl++ = lp->nfsl_stateid.other[2];
3951 *tl = txdr_unsigned(lp->nfsl_seqid);
3952 if (nfstest_outofseq &&
3953 (arc4random() % nfstest_outofseq) == 0)
3954 *tl = txdr_unsigned(lp->nfsl_seqid + 1);
3957 nd->nd_flag |= ND_USEGSSNAME;
3958 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, vp, p, cred,
3959 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
3963 NFSCL_INCRSEQID(lp->nfsl_open->nfso_own->nfsow_seqid, nd);
3964 NFSCL_INCRSEQID(lp->nfsl_seqid, nd);
3965 if (nd->nd_repstat == 0) {
3966 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID);
3967 lp->nfsl_stateid.seqid = *tl++;
3968 lp->nfsl_stateid.other[0] = *tl++;
3969 lp->nfsl_stateid.other[1] = *tl++;
3970 lp->nfsl_stateid.other[2] = *tl;
3971 } else if (nd->nd_repstat == NFSERR_DENIED) {
3972 NFSM_DISSECT(tl, u_int32_t *, 8 * NFSX_UNSIGNED);
3973 size = fxdr_unsigned(int, *(tl + 7));
3974 if (size < 0 || size > NFSV4_OPAQUELIMIT)
3977 error = nfsm_advance(nd, NFSM_RNDUP(size), -1);
3978 } else if (nd->nd_repstat == NFSERR_STALESTATEID ||
3979 nd->nd_repstat == NFSERR_BADSESSION)
3980 nfscl_initiate_recovery(lp->nfsl_open->nfso_own->nfsow_clp);
3982 mbuf_freem(nd->nd_mrep);
3988 * (always called with the vp for the mount point)
3991 nfsrpc_statfs(vnode_t vp, struct nfsstatfs *sbp, struct nfsfsinfo *fsp,
3992 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp,
3995 u_int32_t *tl = NULL;
3996 struct nfsrv_descript nfsd, *nd = &nfsd;
3997 struct nfsmount *nmp;
3998 nfsattrbit_t attrbits;
4002 nmp = VFSTONFS(vnode_mount(vp));
4003 if (NFSHASNFSV4(nmp)) {
4005 * For V4, you actually do a getattr.
4007 NFSCL_REQSTART(nd, NFSPROC_GETATTR, vp);
4008 NFSSTATFS_GETATTRBIT(&attrbits);
4009 (void) nfsrv_putattrbit(nd, &attrbits);
4010 nd->nd_flag |= ND_USEGSSNAME;
4011 error = nfscl_request(nd, vp, p, cred, stuff);
4014 if (nd->nd_repstat == 0) {
4015 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0,
4016 NULL, NULL, sbp, fsp, NULL, 0, NULL, NULL, NULL, p,
4019 nmp->nm_fsid[0] = nap->na_filesid[0];
4020 nmp->nm_fsid[1] = nap->na_filesid[1];
4021 NFSSETHASSETFSID(nmp);
4025 error = nd->nd_repstat;
4030 NFSCL_REQSTART(nd, NFSPROC_FSSTAT, vp);
4031 error = nfscl_request(nd, vp, p, cred, stuff);
4034 if (nd->nd_flag & ND_NFSV3) {
4035 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
4039 if (nd->nd_repstat) {
4040 error = nd->nd_repstat;
4043 NFSM_DISSECT(tl, u_int32_t *,
4044 NFSX_STATFS(nd->nd_flag & ND_NFSV3));
4046 if (NFSHASNFSV3(nmp)) {
4047 sbp->sf_tbytes = fxdr_hyper(tl); tl += 2;
4048 sbp->sf_fbytes = fxdr_hyper(tl); tl += 2;
4049 sbp->sf_abytes = fxdr_hyper(tl); tl += 2;
4050 sbp->sf_tfiles = fxdr_hyper(tl); tl += 2;
4051 sbp->sf_ffiles = fxdr_hyper(tl); tl += 2;
4052 sbp->sf_afiles = fxdr_hyper(tl); tl += 2;
4053 sbp->sf_invarsec = fxdr_unsigned(u_int32_t, *tl);
4054 } else if (NFSHASNFSV4(nmp) == 0) {
4055 sbp->sf_tsize = fxdr_unsigned(u_int32_t, *tl++);
4056 sbp->sf_bsize = fxdr_unsigned(u_int32_t, *tl++);
4057 sbp->sf_blocks = fxdr_unsigned(u_int32_t, *tl++);
4058 sbp->sf_bfree = fxdr_unsigned(u_int32_t, *tl++);
4059 sbp->sf_bavail = fxdr_unsigned(u_int32_t, *tl);
4062 mbuf_freem(nd->nd_mrep);
4070 nfsrpc_pathconf(vnode_t vp, struct nfsv3_pathconf *pc,
4071 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp,
4074 struct nfsrv_descript nfsd, *nd = &nfsd;
4075 struct nfsmount *nmp;
4077 nfsattrbit_t attrbits;
4081 nmp = VFSTONFS(vnode_mount(vp));
4082 if (NFSHASNFSV4(nmp)) {
4084 * For V4, you actually do a getattr.
4086 NFSCL_REQSTART(nd, NFSPROC_GETATTR, vp);
4087 NFSPATHCONF_GETATTRBIT(&attrbits);
4088 (void) nfsrv_putattrbit(nd, &attrbits);
4089 nd->nd_flag |= ND_USEGSSNAME;
4090 error = nfscl_request(nd, vp, p, cred, stuff);
4093 if (nd->nd_repstat == 0) {
4094 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0,
4095 pc, NULL, NULL, NULL, NULL, 0, NULL, NULL, NULL, p,
4100 error = nd->nd_repstat;
4103 NFSCL_REQSTART(nd, NFSPROC_PATHCONF, vp);
4104 error = nfscl_request(nd, vp, p, cred, stuff);
4107 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
4108 if (nd->nd_repstat && !error)
4109 error = nd->nd_repstat;
4111 NFSM_DISSECT(tl, u_int32_t *, NFSX_V3PATHCONF);
4112 pc->pc_linkmax = fxdr_unsigned(u_int32_t, *tl++);
4113 pc->pc_namemax = fxdr_unsigned(u_int32_t, *tl++);
4114 pc->pc_notrunc = fxdr_unsigned(u_int32_t, *tl++);
4115 pc->pc_chownrestricted =
4116 fxdr_unsigned(u_int32_t, *tl++);
4117 pc->pc_caseinsensitive =
4118 fxdr_unsigned(u_int32_t, *tl++);
4119 pc->pc_casepreserving = fxdr_unsigned(u_int32_t, *tl);
4123 mbuf_freem(nd->nd_mrep);
4128 * nfs version 3 fsinfo rpc call
4131 nfsrpc_fsinfo(vnode_t vp, struct nfsfsinfo *fsp, struct ucred *cred,
4132 NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, void *stuff)
4135 struct nfsrv_descript nfsd, *nd = &nfsd;
4139 NFSCL_REQSTART(nd, NFSPROC_FSINFO, vp);
4140 error = nfscl_request(nd, vp, p, cred, stuff);
4143 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
4144 if (nd->nd_repstat && !error)
4145 error = nd->nd_repstat;
4147 NFSM_DISSECT(tl, u_int32_t *, NFSX_V3FSINFO);
4148 fsp->fs_rtmax = fxdr_unsigned(u_int32_t, *tl++);
4149 fsp->fs_rtpref = fxdr_unsigned(u_int32_t, *tl++);
4150 fsp->fs_rtmult = fxdr_unsigned(u_int32_t, *tl++);
4151 fsp->fs_wtmax = fxdr_unsigned(u_int32_t, *tl++);
4152 fsp->fs_wtpref = fxdr_unsigned(u_int32_t, *tl++);
4153 fsp->fs_wtmult = fxdr_unsigned(u_int32_t, *tl++);
4154 fsp->fs_dtpref = fxdr_unsigned(u_int32_t, *tl++);
4155 fsp->fs_maxfilesize = fxdr_hyper(tl);
4157 fxdr_nfsv3time(tl, &fsp->fs_timedelta);
4159 fsp->fs_properties = fxdr_unsigned(u_int32_t, *tl);
4162 mbuf_freem(nd->nd_mrep);
4167 * This function performs the Renew RPC.
4170 nfsrpc_renew(struct nfsclclient *clp, struct nfsclds *dsp, struct ucred *cred,
4174 struct nfsrv_descript nfsd;
4175 struct nfsrv_descript *nd = &nfsd;
4176 struct nfsmount *nmp;
4178 struct nfssockreq *nrp;
4180 nmp = clp->nfsc_nmp;
4183 nfscl_reqstart(nd, NFSPROC_RENEW, nmp, NULL, 0, NULL,
4184 &dsp->nfsclds_sess);
4185 if (!NFSHASNFSV4N(nmp)) {
4186 /* NFSv4.1 just uses a Sequence Op and not a Renew. */
4187 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
4188 *tl++ = NFSMNT_MDSSESSION(nmp)->nfsess_clientid.lval[0];
4189 *tl = NFSMNT_MDSSESSION(nmp)->nfsess_clientid.lval[1];
4191 nrp = dsp->nfsclds_sockp;
4193 /* If NULL, use the MDS socket. */
4194 nrp = &nmp->nm_sockreq;
4195 nd->nd_flag |= ND_USEGSSNAME;
4196 error = newnfs_request(nd, nmp, NULL, nrp, NULL, p, cred,
4197 NFS_PROG, NFS_VER4, NULL, 1, NULL, &dsp->nfsclds_sess);
4200 error = nd->nd_repstat;
4201 mbuf_freem(nd->nd_mrep);
4206 * This function performs the Releaselockowner RPC.
4209 nfsrpc_rellockown(struct nfsmount *nmp, struct nfscllockowner *lp,
4210 uint8_t *fh, int fhlen, struct ucred *cred, NFSPROC_T *p)
4212 struct nfsrv_descript nfsd, *nd = &nfsd;
4215 uint8_t own[NFSV4CL_LOCKNAMELEN + NFSX_V4FHMAX];
4217 if (NFSHASNFSV4N(nmp)) {
4218 /* For NFSv4.1, do a FreeStateID. */
4219 nfscl_reqstart(nd, NFSPROC_FREESTATEID, nmp, NULL, 0, NULL,
4221 nfsm_stateidtom(nd, &lp->nfsl_stateid, NFSSTATEID_PUTSTATEID);
4223 nfscl_reqstart(nd, NFSPROC_RELEASELCKOWN, nmp, NULL, 0, NULL,
4225 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
4226 *tl++ = NFSMNT_MDSSESSION(nmp)->nfsess_clientid.lval[0];
4227 *tl = NFSMNT_MDSSESSION(nmp)->nfsess_clientid.lval[1];
4228 NFSBCOPY(lp->nfsl_owner, own, NFSV4CL_LOCKNAMELEN);
4229 NFSBCOPY(fh, &own[NFSV4CL_LOCKNAMELEN], fhlen);
4230 (void)nfsm_strtom(nd, own, NFSV4CL_LOCKNAMELEN + fhlen);
4232 nd->nd_flag |= ND_USEGSSNAME;
4233 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4234 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4237 error = nd->nd_repstat;
4238 mbuf_freem(nd->nd_mrep);
4243 * This function performs the Compound to get the mount pt FH.
4246 nfsrpc_getdirpath(struct nfsmount *nmp, u_char *dirpath, struct ucred *cred,
4250 struct nfsrv_descript nfsd;
4251 struct nfsrv_descript *nd = &nfsd;
4253 int error, cnt, len, setnil;
4256 nfscl_reqstart(nd, NFSPROC_PUTROOTFH, nmp, NULL, 0, &opcntp, NULL);
4264 while (*cp2 != '\0' && *cp2 != '/')
4271 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
4272 *tl = txdr_unsigned(NFSV4OP_LOOKUP);
4273 nfsm_strtom(nd, cp, strlen(cp));
4279 } while (*cp != '\0');
4280 if (NFSHASNFSV4N(nmp))
4281 /* Has a Sequence Op done by nfscl_reqstart(). */
4282 *opcntp = txdr_unsigned(3 + cnt);
4284 *opcntp = txdr_unsigned(2 + cnt);
4285 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
4286 *tl = txdr_unsigned(NFSV4OP_GETFH);
4287 nd->nd_flag |= ND_USEGSSNAME;
4288 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4289 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4292 if (nd->nd_repstat == 0) {
4293 NFSM_DISSECT(tl, u_int32_t *, (3 + 2 * cnt) * NFSX_UNSIGNED);
4294 tl += (2 + 2 * cnt);
4295 if ((len = fxdr_unsigned(int, *tl)) <= 0 ||
4297 nd->nd_repstat = NFSERR_BADXDR;
4299 nd->nd_repstat = nfsrv_mtostr(nd, nmp->nm_fh, len);
4300 if (nd->nd_repstat == 0)
4301 nmp->nm_fhsize = len;
4304 error = nd->nd_repstat;
4306 mbuf_freem(nd->nd_mrep);
4311 * This function performs the Delegreturn RPC.
4314 nfsrpc_delegreturn(struct nfscldeleg *dp, struct ucred *cred,
4315 struct nfsmount *nmp, NFSPROC_T *p, int syscred)
4318 struct nfsrv_descript nfsd;
4319 struct nfsrv_descript *nd = &nfsd;
4322 nfscl_reqstart(nd, NFSPROC_DELEGRETURN, nmp, dp->nfsdl_fh,
4323 dp->nfsdl_fhlen, NULL, NULL);
4324 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID);
4325 if (NFSHASNFSV4N(nmp))
4328 *tl++ = dp->nfsdl_stateid.seqid;
4329 *tl++ = dp->nfsdl_stateid.other[0];
4330 *tl++ = dp->nfsdl_stateid.other[1];
4331 *tl = dp->nfsdl_stateid.other[2];
4333 nd->nd_flag |= ND_USEGSSNAME;
4334 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4335 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4338 error = nd->nd_repstat;
4339 mbuf_freem(nd->nd_mrep);
4347 nfsrpc_getacl(vnode_t vp, struct ucred *cred, NFSPROC_T *p,
4348 struct acl *aclp, void *stuff)
4350 struct nfsrv_descript nfsd, *nd = &nfsd;
4352 nfsattrbit_t attrbits;
4353 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
4355 if (nfsrv_useacl == 0 || !NFSHASNFSV4(nmp))
4356 return (EOPNOTSUPP);
4357 NFSCL_REQSTART(nd, NFSPROC_GETACL, vp);
4358 NFSZERO_ATTRBIT(&attrbits);
4359 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_ACL);
4360 (void) nfsrv_putattrbit(nd, &attrbits);
4361 error = nfscl_request(nd, vp, p, cred, stuff);
4364 if (!nd->nd_repstat)
4365 error = nfsv4_loadattr(nd, vp, NULL, NULL, NULL, 0, NULL,
4366 NULL, NULL, NULL, aclp, 0, NULL, NULL, NULL, p, cred);
4368 error = nd->nd_repstat;
4369 mbuf_freem(nd->nd_mrep);
4377 nfsrpc_setacl(vnode_t vp, struct ucred *cred, NFSPROC_T *p,
4378 struct acl *aclp, void *stuff)
4381 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
4383 if (nfsrv_useacl == 0 || !NFSHASNFSV4(nmp))
4384 return (EOPNOTSUPP);
4385 error = nfsrpc_setattr(vp, NULL, aclp, cred, p, NULL, NULL, stuff);
4393 nfsrpc_setaclrpc(vnode_t vp, struct ucred *cred, NFSPROC_T *p,
4394 struct acl *aclp, nfsv4stateid_t *stateidp, void *stuff)
4396 struct nfsrv_descript nfsd, *nd = &nfsd;
4398 nfsattrbit_t attrbits;
4399 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
4401 if (!NFSHASNFSV4(nmp))
4402 return (EOPNOTSUPP);
4403 NFSCL_REQSTART(nd, NFSPROC_SETACL, vp);
4404 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID);
4405 NFSZERO_ATTRBIT(&attrbits);
4406 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_ACL);
4407 (void) nfsv4_fillattr(nd, vnode_mount(vp), vp, aclp, NULL, NULL, 0,
4408 &attrbits, NULL, NULL, 0, 0, 0, 0, (uint64_t)0);
4409 error = nfscl_request(nd, vp, p, cred, stuff);
4412 /* Don't care about the pre/postop attributes */
4413 mbuf_freem(nd->nd_mrep);
4414 return (nd->nd_repstat);
4418 * Do the NFSv4.1 Exchange ID.
4421 nfsrpc_exchangeid(struct nfsmount *nmp, struct nfsclclient *clp,
4422 struct nfssockreq *nrp, uint32_t exchflags, struct nfsclds **dspp,
4423 struct ucred *cred, NFSPROC_T *p)
4425 uint32_t *tl, v41flags;
4426 struct nfsrv_descript nfsd;
4427 struct nfsrv_descript *nd = &nfsd;
4428 struct nfsclds *dsp;
4429 struct timespec verstime;
4433 nfscl_reqstart(nd, NFSPROC_EXCHANGEID, nmp, NULL, 0, NULL, NULL);
4434 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_UNSIGNED);
4435 *tl++ = txdr_unsigned(nfsboottime.tv_sec); /* Client owner */
4436 *tl = txdr_unsigned(clp->nfsc_rev);
4437 (void) nfsm_strtom(nd, clp->nfsc_id, clp->nfsc_idlen);
4439 NFSM_BUILD(tl, uint32_t *, 3 * NFSX_UNSIGNED);
4440 *tl++ = txdr_unsigned(exchflags);
4441 *tl++ = txdr_unsigned(NFSV4EXCH_SP4NONE);
4443 /* Set the implementation id4 */
4444 *tl = txdr_unsigned(1);
4445 (void) nfsm_strtom(nd, "freebsd.org", strlen("freebsd.org"));
4446 (void) nfsm_strtom(nd, version, strlen(version));
4447 NFSM_BUILD(tl, uint32_t *, NFSX_V4TIME);
4448 verstime.tv_sec = 1293840000; /* Jan 1, 2011 */
4449 verstime.tv_nsec = 0;
4450 txdr_nfsv4time(&verstime, tl);
4451 nd->nd_flag |= ND_USEGSSNAME;
4452 error = newnfs_request(nd, nmp, NULL, nrp, NULL, p, cred,
4453 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4454 NFSCL_DEBUG(1, "exchangeid err=%d reps=%d\n", error,
4455 (int)nd->nd_repstat);
4458 if (nd->nd_repstat == 0) {
4459 NFSM_DISSECT(tl, uint32_t *, 6 * NFSX_UNSIGNED + NFSX_HYPER);
4460 len = fxdr_unsigned(int, *(tl + 7));
4461 if (len < 0 || len > NFSV4_OPAQUELIMIT) {
4462 error = NFSERR_BADXDR;
4465 dsp = malloc(sizeof(struct nfsclds) + len, M_NFSCLDS,
4467 dsp->nfsclds_expire = NFSD_MONOSEC + clp->nfsc_renew;
4468 dsp->nfsclds_servownlen = len;
4469 dsp->nfsclds_sess.nfsess_clientid.lval[0] = *tl++;
4470 dsp->nfsclds_sess.nfsess_clientid.lval[1] = *tl++;
4471 dsp->nfsclds_sess.nfsess_sequenceid =
4472 fxdr_unsigned(uint32_t, *tl++);
4473 v41flags = fxdr_unsigned(uint32_t, *tl);
4474 if ((v41flags & NFSV4EXCH_USEPNFSMDS) != 0 &&
4475 NFSHASPNFSOPT(nmp)) {
4476 NFSCL_DEBUG(1, "set PNFS\n");
4478 nmp->nm_state |= NFSSTA_PNFS;
4480 dsp->nfsclds_flags |= NFSCLDS_MDS;
4482 if ((v41flags & NFSV4EXCH_USEPNFSDS) != 0)
4483 dsp->nfsclds_flags |= NFSCLDS_DS;
4485 nd->nd_repstat = nfsrv_mtostr(nd,
4486 dsp->nfsclds_serverown, len);
4487 if (nd->nd_repstat == 0) {
4488 mtx_init(&dsp->nfsclds_mtx, "nfsds", NULL, MTX_DEF);
4489 mtx_init(&dsp->nfsclds_sess.nfsess_mtx, "nfssession",
4491 nfscl_initsessionslots(&dsp->nfsclds_sess);
4494 free(dsp, M_NFSCLDS);
4496 error = nd->nd_repstat;
4498 mbuf_freem(nd->nd_mrep);
4503 * Do the NFSv4.1 Create Session.
4506 nfsrpc_createsession(struct nfsmount *nmp, struct nfsclsession *sep,
4507 struct nfssockreq *nrp, uint32_t sequenceid, int mds, struct ucred *cred,
4510 uint32_t crflags, *tl;
4511 struct nfsrv_descript nfsd;
4512 struct nfsrv_descript *nd = &nfsd;
4515 nfscl_reqstart(nd, NFSPROC_CREATESESSION, nmp, NULL, 0, NULL, NULL);
4516 NFSM_BUILD(tl, uint32_t *, 4 * NFSX_UNSIGNED);
4517 *tl++ = sep->nfsess_clientid.lval[0];
4518 *tl++ = sep->nfsess_clientid.lval[1];
4519 *tl++ = txdr_unsigned(sequenceid);
4520 crflags = (NFSMNT_RDONLY(nmp->nm_mountp) ? 0 : NFSV4CRSESS_PERSIST);
4521 if (nfscl_enablecallb != 0 && nfs_numnfscbd > 0)
4522 crflags |= NFSV4CRSESS_CONNBACKCHAN;
4523 *tl = txdr_unsigned(crflags);
4525 /* Fill in fore channel attributes. */
4526 NFSM_BUILD(tl, uint32_t *, 7 * NFSX_UNSIGNED);
4527 *tl++ = 0; /* Header pad size */
4528 *tl++ = txdr_unsigned(100000); /* Max request size */
4529 *tl++ = txdr_unsigned(100000); /* Max response size */
4530 *tl++ = txdr_unsigned(4096); /* Max response size cached */
4531 *tl++ = txdr_unsigned(20); /* Max operations */
4532 *tl++ = txdr_unsigned(64); /* Max slots */
4533 *tl = 0; /* No rdma ird */
4535 /* Fill in back channel attributes. */
4536 NFSM_BUILD(tl, uint32_t *, 7 * NFSX_UNSIGNED);
4537 *tl++ = 0; /* Header pad size */
4538 *tl++ = txdr_unsigned(10000); /* Max request size */
4539 *tl++ = txdr_unsigned(10000); /* Max response size */
4540 *tl++ = txdr_unsigned(4096); /* Max response size cached */
4541 *tl++ = txdr_unsigned(4); /* Max operations */
4542 *tl++ = txdr_unsigned(NFSV4_CBSLOTS); /* Max slots */
4543 *tl = 0; /* No rdma ird */
4545 NFSM_BUILD(tl, uint32_t *, 8 * NFSX_UNSIGNED);
4546 *tl++ = txdr_unsigned(NFS_CALLBCKPROG); /* Call back prog # */
4548 /* Allow AUTH_SYS callbacks as uid, gid == 0. */
4549 *tl++ = txdr_unsigned(1); /* Auth_sys only */
4550 *tl++ = txdr_unsigned(AUTH_SYS); /* AUTH_SYS type */
4551 *tl++ = txdr_unsigned(nfsboottime.tv_sec); /* time stamp */
4552 *tl++ = 0; /* Null machine name */
4553 *tl++ = 0; /* Uid == 0 */
4554 *tl++ = 0; /* Gid == 0 */
4555 *tl = 0; /* No additional gids */
4556 nd->nd_flag |= ND_USEGSSNAME;
4557 error = newnfs_request(nd, nmp, NULL, nrp, NULL, p, cred, NFS_PROG,
4558 NFS_VER4, NULL, 1, NULL, NULL);
4561 if (nd->nd_repstat == 0) {
4562 NFSM_DISSECT(tl, uint32_t *, NFSX_V4SESSIONID +
4564 bcopy(tl, sep->nfsess_sessionid, NFSX_V4SESSIONID);
4565 tl += NFSX_V4SESSIONID / NFSX_UNSIGNED;
4566 sep->nfsess_sequenceid = fxdr_unsigned(uint32_t, *tl++);
4567 crflags = fxdr_unsigned(uint32_t, *tl);
4568 if ((crflags & NFSV4CRSESS_PERSIST) != 0 && mds != 0) {
4570 nmp->nm_state |= NFSSTA_SESSPERSIST;
4574 /* Get the fore channel slot count. */
4575 NFSM_DISSECT(tl, uint32_t *, 7 * NFSX_UNSIGNED);
4576 tl += 3; /* Skip the other counts. */
4577 sep->nfsess_maxcache = fxdr_unsigned(int, *tl++);
4579 sep->nfsess_foreslots = fxdr_unsigned(uint16_t, *tl++);
4580 NFSCL_DEBUG(4, "fore slots=%d\n", (int)sep->nfsess_foreslots);
4581 irdcnt = fxdr_unsigned(int, *tl);
4583 NFSM_DISSECT(tl, uint32_t *, irdcnt * NFSX_UNSIGNED);
4585 /* and the back channel slot count. */
4586 NFSM_DISSECT(tl, uint32_t *, 7 * NFSX_UNSIGNED);
4588 sep->nfsess_backslots = fxdr_unsigned(uint16_t, *tl);
4589 NFSCL_DEBUG(4, "back slots=%d\n", (int)sep->nfsess_backslots);
4591 error = nd->nd_repstat;
4593 mbuf_freem(nd->nd_mrep);
4598 * Do the NFSv4.1 Destroy Session.
4601 nfsrpc_destroysession(struct nfsmount *nmp, struct nfsclclient *clp,
4602 struct ucred *cred, NFSPROC_T *p)
4605 struct nfsrv_descript nfsd;
4606 struct nfsrv_descript *nd = &nfsd;
4609 nfscl_reqstart(nd, NFSPROC_DESTROYSESSION, nmp, NULL, 0, NULL, NULL);
4610 NFSM_BUILD(tl, uint32_t *, NFSX_V4SESSIONID);
4611 bcopy(NFSMNT_MDSSESSION(nmp)->nfsess_sessionid, tl, NFSX_V4SESSIONID);
4612 nd->nd_flag |= ND_USEGSSNAME;
4613 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4614 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4617 error = nd->nd_repstat;
4618 mbuf_freem(nd->nd_mrep);
4623 * Do the NFSv4.1 Destroy Client.
4626 nfsrpc_destroyclient(struct nfsmount *nmp, struct nfsclclient *clp,
4627 struct ucred *cred, NFSPROC_T *p)
4630 struct nfsrv_descript nfsd;
4631 struct nfsrv_descript *nd = &nfsd;
4634 nfscl_reqstart(nd, NFSPROC_DESTROYCLIENT, nmp, NULL, 0, NULL, NULL);
4635 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_UNSIGNED);
4636 *tl++ = NFSMNT_MDSSESSION(nmp)->nfsess_clientid.lval[0];
4637 *tl = NFSMNT_MDSSESSION(nmp)->nfsess_clientid.lval[1];
4638 nd->nd_flag |= ND_USEGSSNAME;
4639 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4640 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4643 error = nd->nd_repstat;
4644 mbuf_freem(nd->nd_mrep);
4649 * Do the NFSv4.1 LayoutGet.
4652 nfsrpc_layoutget(struct nfsmount *nmp, uint8_t *fhp, int fhlen, int iomode,
4653 uint64_t offset, uint64_t len, uint64_t minlen, int layoutlen,
4654 nfsv4stateid_t *stateidp, int *retonclosep, struct nfsclflayouthead *flhp,
4655 struct ucred *cred, NFSPROC_T *p, void *stuff)
4658 struct nfsrv_descript nfsd, *nd = &nfsd;
4660 struct nfsclflayout *flp, *prevflp, *tflp;
4661 int cnt, error, gotiomode, fhcnt, nfhlen, i, j;
4667 nfscl_reqstart(nd, NFSPROC_LAYOUTGET, nmp, fhp, fhlen, NULL, NULL);
4668 NFSM_BUILD(tl, uint32_t *, 4 * NFSX_UNSIGNED + 3 * NFSX_HYPER +
4670 *tl++ = newnfs_false; /* Don't signal availability. */
4671 *tl++ = txdr_unsigned(NFSLAYOUT_NFSV4_1_FILES);
4672 *tl++ = txdr_unsigned(iomode);
4673 txdr_hyper(offset, tl);
4675 txdr_hyper(len, tl);
4677 txdr_hyper(minlen, tl);
4679 *tl++ = txdr_unsigned(stateidp->seqid);
4680 NFSCL_DEBUG(4, "layget seq=%d\n", (int)stateidp->seqid);
4681 *tl++ = stateidp->other[0];
4682 *tl++ = stateidp->other[1];
4683 *tl++ = stateidp->other[2];
4684 *tl = txdr_unsigned(layoutlen);
4685 nd->nd_flag |= ND_USEGSSNAME;
4686 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4687 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4690 if (nd->nd_repstat == 0) {
4691 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED + NFSX_STATEID);
4696 stateidp->seqid = fxdr_unsigned(uint32_t, *tl++);
4697 NFSCL_DEBUG(4, "retoncls=%d stseq=%d\n", *retonclosep,
4698 (int)stateidp->seqid);
4699 stateidp->other[0] = *tl++;
4700 stateidp->other[1] = *tl++;
4701 stateidp->other[2] = *tl++;
4702 cnt = fxdr_unsigned(int, *tl);
4703 NFSCL_DEBUG(4, "layg cnt=%d\n", cnt);
4704 if (cnt <= 0 || cnt > 10000) {
4705 /* Don't accept more than 10000 layouts in reply. */
4706 error = NFSERR_BADXDR;
4709 for (i = 0; i < cnt; i++) {
4710 /* Dissect all the way to the file handle cnt. */
4711 NFSM_DISSECT(tl, uint32_t *, 3 * NFSX_HYPER +
4712 6 * NFSX_UNSIGNED + NFSX_V4DEVICEID);
4713 fhcnt = fxdr_unsigned(int, *(tl + 11 +
4714 NFSX_V4DEVICEID / NFSX_UNSIGNED));
4715 NFSCL_DEBUG(4, "fhcnt=%d\n", fhcnt);
4716 if (fhcnt < 0 || fhcnt > 100) {
4717 /* Don't accept more than 100 file handles. */
4718 error = NFSERR_BADXDR;
4722 flp = malloc(sizeof(*flp) + (fhcnt - 1) *
4723 sizeof(struct nfsfh *),
4724 M_NFSFLAYOUT, M_WAITOK);
4726 flp = malloc(sizeof(*flp),
4727 M_NFSFLAYOUT, M_WAITOK);
4728 flp->nfsfl_flags = 0;
4729 flp->nfsfl_fhcnt = 0;
4730 flp->nfsfl_devp = NULL;
4731 flp->nfsfl_off = fxdr_hyper(tl); tl += 2;
4732 retlen = fxdr_hyper(tl); tl += 2;
4733 if (flp->nfsfl_off + retlen < flp->nfsfl_off)
4734 flp->nfsfl_end = UINT64_MAX - flp->nfsfl_off;
4736 flp->nfsfl_end = flp->nfsfl_off + retlen;
4737 flp->nfsfl_iomode = fxdr_unsigned(int, *tl++);
4738 if (gotiomode == -1)
4739 gotiomode = flp->nfsfl_iomode;
4740 NFSCL_DEBUG(4, "layg reqiom=%d retiom=%d\n", iomode,
4741 (int)flp->nfsfl_iomode);
4742 if (fxdr_unsigned(int, *tl++) !=
4743 NFSLAYOUT_NFSV4_1_FILES) {
4744 printf("NFSv4.1: got non-files layout\n");
4745 error = NFSERR_BADXDR;
4748 NFSBCOPY(++tl, flp->nfsfl_dev, NFSX_V4DEVICEID);
4749 tl += (NFSX_V4DEVICEID / NFSX_UNSIGNED);
4750 flp->nfsfl_util = fxdr_unsigned(uint32_t, *tl++);
4751 NFSCL_DEBUG(4, "flutil=0x%x\n", flp->nfsfl_util);
4752 flp->nfsfl_stripe1 = fxdr_unsigned(uint32_t, *tl++);
4753 flp->nfsfl_patoff = fxdr_hyper(tl); tl += 2;
4754 if (fxdr_unsigned(int, *tl) != fhcnt) {
4755 printf("EEK! bad fhcnt\n");
4756 error = NFSERR_BADXDR;
4759 for (j = 0; j < fhcnt; j++) {
4760 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
4761 nfhlen = fxdr_unsigned(int, *tl);
4762 if (nfhlen <= 0 || nfhlen > NFSX_V4FHMAX) {
4763 error = NFSERR_BADXDR;
4766 nfhp = malloc(sizeof(*nfhp) + nfhlen - 1,
4768 flp->nfsfl_fh[j] = nfhp;
4770 nfhp->nfh_len = nfhlen;
4771 NFSM_DISSECT(cp, uint8_t *, NFSM_RNDUP(nfhlen));
4772 NFSBCOPY(cp, nfhp->nfh_fh, nfhlen);
4774 if (flp->nfsfl_iomode == gotiomode) {
4775 /* Keep the list in increasing offset order. */
4776 tflp = LIST_FIRST(flhp);
4778 while (tflp != NULL &&
4779 tflp->nfsfl_off < flp->nfsfl_off) {
4781 tflp = LIST_NEXT(tflp, nfsfl_list);
4783 if (prevflp == NULL)
4784 LIST_INSERT_HEAD(flhp, flp, nfsfl_list);
4786 LIST_INSERT_AFTER(prevflp, flp,
4789 printf("nfscl_layoutget(): got wrong iomode\n");
4790 nfscl_freeflayout(flp);
4795 if (nd->nd_repstat != 0 && error == 0)
4796 error = nd->nd_repstat;
4798 if (error != 0 && flp != NULL)
4799 nfscl_freeflayout(flp);
4800 mbuf_freem(nd->nd_mrep);
4805 * Do the NFSv4.1 Get Device Info.
4808 nfsrpc_getdeviceinfo(struct nfsmount *nmp, uint8_t *deviceid, int layouttype,
4809 uint32_t *notifybitsp, struct nfscldevinfo **ndip, struct ucred *cred,
4813 struct nfsrv_descript nfsd;
4814 struct nfsrv_descript *nd = &nfsd;
4815 struct sockaddr_storage ss;
4816 struct nfsclds *dsp = NULL, **dspp;
4817 struct nfscldevinfo *ndi;
4818 int addrcnt, bitcnt, error, i, isudp, j, pos, safilled, stripecnt;
4819 uint8_t stripeindex;
4823 nfscl_reqstart(nd, NFSPROC_GETDEVICEINFO, nmp, NULL, 0, NULL, NULL);
4824 NFSM_BUILD(tl, uint32_t *, NFSX_V4DEVICEID + 3 * NFSX_UNSIGNED);
4825 NFSBCOPY(deviceid, tl, NFSX_V4DEVICEID);
4826 tl += (NFSX_V4DEVICEID / NFSX_UNSIGNED);
4827 *tl++ = txdr_unsigned(layouttype);
4828 *tl++ = txdr_unsigned(100000);
4829 if (notifybitsp != NULL && *notifybitsp != 0) {
4830 *tl = txdr_unsigned(1); /* One word of bits. */
4831 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
4832 *tl = txdr_unsigned(*notifybitsp);
4834 *tl = txdr_unsigned(0);
4835 nd->nd_flag |= ND_USEGSSNAME;
4836 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4837 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4840 if (nd->nd_repstat == 0) {
4841 NFSM_DISSECT(tl, uint32_t *, 3 * NFSX_UNSIGNED);
4842 if (layouttype != fxdr_unsigned(int, *tl++))
4843 printf("EEK! devinfo layout type not same!\n");
4844 stripecnt = fxdr_unsigned(int, *++tl);
4845 NFSCL_DEBUG(4, "stripecnt=%d\n", stripecnt);
4846 if (stripecnt < 1 || stripecnt > 4096) {
4847 printf("NFS devinfo stripecnt %d: out of range\n",
4849 error = NFSERR_BADXDR;
4852 NFSM_DISSECT(tl, uint32_t *, (stripecnt + 1) * NFSX_UNSIGNED);
4853 addrcnt = fxdr_unsigned(int, *(tl + stripecnt));
4854 NFSCL_DEBUG(4, "addrcnt=%d\n", addrcnt);
4855 if (addrcnt < 1 || addrcnt > 128) {
4856 printf("NFS devinfo addrcnt %d: out of range\n",
4858 error = NFSERR_BADXDR;
4863 * Now we know how many stripe indices and addresses, so
4864 * we can allocate the structure the correct size.
4866 i = (stripecnt * sizeof(uint8_t)) / sizeof(struct nfsclds *)
4868 NFSCL_DEBUG(4, "stripeindices=%d\n", i);
4869 ndi = malloc(sizeof(*ndi) + (addrcnt + i) *
4870 sizeof(struct nfsclds *), M_NFSDEVINFO, M_WAITOK | M_ZERO);
4871 NFSBCOPY(deviceid, ndi->nfsdi_deviceid, NFSX_V4DEVICEID);
4872 ndi->nfsdi_refcnt = 0;
4873 ndi->nfsdi_stripecnt = stripecnt;
4874 ndi->nfsdi_addrcnt = addrcnt;
4875 /* Fill in the stripe indices. */
4876 for (i = 0; i < stripecnt; i++) {
4877 stripeindex = fxdr_unsigned(uint8_t, *tl++);
4878 NFSCL_DEBUG(4, "stripeind=%d\n", stripeindex);
4879 if (stripeindex >= addrcnt) {
4880 printf("NFS devinfo stripeindex %d: too big\n",
4882 error = NFSERR_BADXDR;
4885 nfsfldi_setstripeindex(ndi, i, stripeindex);
4888 /* Now, dissect the server address(es). */
4890 for (i = 0; i < addrcnt; i++) {
4891 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
4892 cnt = fxdr_unsigned(uint32_t, *tl);
4894 printf("NFS devinfo 0 len addrlist\n");
4895 error = NFSERR_BADXDR;
4898 dspp = nfsfldi_addr(ndi, i);
4899 pos = arc4random() % cnt; /* Choose one. */
4901 for (j = 0; j < cnt; j++) {
4902 error = nfsv4_getipaddr(nd, &ss, &isudp);
4903 if (error != 0 && error != EPERM) {
4904 error = NFSERR_BADXDR;
4907 if (error == 0 && isudp == 0) {
4910 * - use "pos" entry if it is of the
4911 * same af_family or none of them
4912 * is of the same af_family
4914 * - use the first one of the same
4917 if ((safilled == 0 && ss.ss_family ==
4918 nmp->nm_nam->sa_family) ||
4920 (safilled == 0 || ss.ss_family ==
4921 nmp->nm_nam->sa_family)) ||
4922 (safilled == 1 && ss.ss_family ==
4923 nmp->nm_nam->sa_family)) {
4924 error = nfsrpc_fillsa(nmp, &ss,
4929 nmp->nm_nam->sa_family)
4941 /* And the notify bits. */
4942 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
4943 if (safilled != 0) {
4944 bitcnt = fxdr_unsigned(int, *tl);
4946 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
4947 if (notifybitsp != NULL)
4949 fxdr_unsigned(uint32_t, *tl);
4955 if (nd->nd_repstat != 0)
4956 error = nd->nd_repstat;
4958 if (error != 0 && ndi != NULL)
4959 nfscl_freedevinfo(ndi);
4960 mbuf_freem(nd->nd_mrep);
4965 * Do the NFSv4.1 LayoutCommit.
4968 nfsrpc_layoutcommit(struct nfsmount *nmp, uint8_t *fh, int fhlen, int reclaim,
4969 uint64_t off, uint64_t len, uint64_t lastbyte, nfsv4stateid_t *stateidp,
4970 int layouttype, int layoutupdatecnt, uint8_t *layp, struct ucred *cred,
4971 NFSPROC_T *p, void *stuff)
4974 struct nfsrv_descript nfsd, *nd = &nfsd;
4975 int error, outcnt, i;
4978 nfscl_reqstart(nd, NFSPROC_LAYOUTCOMMIT, nmp, fh, fhlen, NULL, NULL);
4979 NFSM_BUILD(tl, uint32_t *, 5 * NFSX_UNSIGNED + 3 * NFSX_HYPER +
4981 txdr_hyper(off, tl);
4983 txdr_hyper(len, tl);
4986 *tl++ = newnfs_true;
4988 *tl++ = newnfs_false;
4989 *tl++ = txdr_unsigned(stateidp->seqid);
4990 *tl++ = stateidp->other[0];
4991 *tl++ = stateidp->other[1];
4992 *tl++ = stateidp->other[2];
4993 *tl++ = newnfs_true;
4996 else if (lastbyte >= (off + len))
4997 lastbyte = off + len - 1;
4998 txdr_hyper(lastbyte, tl);
5000 *tl++ = newnfs_false;
5001 *tl++ = txdr_unsigned(layouttype);
5002 *tl = txdr_unsigned(layoutupdatecnt);
5003 if (layoutupdatecnt > 0) {
5004 KASSERT(layouttype != NFSLAYOUT_NFSV4_1_FILES,
5005 ("Must be nil for Files Layout"));
5006 outcnt = NFSM_RNDUP(layoutupdatecnt);
5007 NFSM_BUILD(cp, uint8_t *, outcnt);
5008 NFSBCOPY(layp, cp, layoutupdatecnt);
5009 cp += layoutupdatecnt;
5010 for (i = 0; i < (outcnt - layoutupdatecnt); i++)
5013 nd->nd_flag |= ND_USEGSSNAME;
5014 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
5015 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
5018 error = nd->nd_repstat;
5019 mbuf_freem(nd->nd_mrep);
5024 * Do the NFSv4.1 LayoutReturn.
5027 nfsrpc_layoutreturn(struct nfsmount *nmp, uint8_t *fh, int fhlen, int reclaim,
5028 int layouttype, uint32_t iomode, int layoutreturn, uint64_t offset,
5029 uint64_t len, nfsv4stateid_t *stateidp, int layoutcnt, uint32_t *layp,
5030 struct ucred *cred, NFSPROC_T *p, void *stuff)
5033 struct nfsrv_descript nfsd, *nd = &nfsd;
5034 int error, outcnt, i;
5037 nfscl_reqstart(nd, NFSPROC_LAYOUTRETURN, nmp, fh, fhlen, NULL, NULL);
5038 NFSM_BUILD(tl, uint32_t *, 4 * NFSX_UNSIGNED);
5040 *tl++ = newnfs_true;
5042 *tl++ = newnfs_false;
5043 *tl++ = txdr_unsigned(layouttype);
5044 *tl++ = txdr_unsigned(iomode);
5045 *tl = txdr_unsigned(layoutreturn);
5046 if (layoutreturn == NFSLAYOUTRETURN_FILE) {
5047 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_HYPER + NFSX_STATEID +
5049 txdr_hyper(offset, tl);
5051 txdr_hyper(len, tl);
5053 NFSCL_DEBUG(4, "layoutret stseq=%d\n", (int)stateidp->seqid);
5054 *tl++ = txdr_unsigned(stateidp->seqid);
5055 *tl++ = stateidp->other[0];
5056 *tl++ = stateidp->other[1];
5057 *tl++ = stateidp->other[2];
5058 *tl = txdr_unsigned(layoutcnt);
5059 if (layoutcnt > 0) {
5060 outcnt = NFSM_RNDUP(layoutcnt);
5061 NFSM_BUILD(cp, uint8_t *, outcnt);
5062 NFSBCOPY(layp, cp, layoutcnt);
5064 for (i = 0; i < (outcnt - layoutcnt); i++)
5068 nd->nd_flag |= ND_USEGSSNAME;
5069 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
5070 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
5073 if (nd->nd_repstat == 0) {
5074 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
5076 NFSM_DISSECT(tl, uint32_t *, NFSX_STATEID);
5077 stateidp->seqid = fxdr_unsigned(uint32_t, *tl++);
5078 stateidp->other[0] = *tl++;
5079 stateidp->other[1] = *tl++;
5080 stateidp->other[2] = *tl;
5083 error = nd->nd_repstat;
5085 mbuf_freem(nd->nd_mrep);
5090 * Acquire a layout and devinfo, if possible. The caller must have acquired
5091 * a reference count on the nfsclclient structure before calling this.
5092 * Return the layout in lypp with a reference count on it, if successful.
5095 nfsrpc_getlayout(struct nfsmount *nmp, vnode_t vp, struct nfsfh *nfhp,
5096 int iomode, uint32_t *notifybitsp, nfsv4stateid_t *stateidp, uint64_t off,
5097 struct nfscllayout **lypp, struct ucred *cred, NFSPROC_T *p)
5099 struct nfscllayout *lyp;
5100 struct nfsclflayout *flp, *tflp;
5101 struct nfscldevinfo *dip;
5102 struct nfsclflayouthead flh;
5103 int error = 0, islocked, layoutlen, recalled, retonclose;
5104 nfsv4stateid_t stateid;
5108 * If lyp is returned non-NULL, there will be a refcnt (shared lock)
5109 * on it, iff flp != NULL or a lock (exclusive lock) on it iff
5112 lyp = nfscl_getlayout(nmp->nm_clp, nfhp->nfh_fh, nfhp->nfh_len,
5113 off, &flp, &recalled);
5115 if (lyp == NULL || flp == NULL) {
5119 layoutlen = NFSMNT_MDSSESSION(nmp)->nfsess_maxcache -
5120 (NFSX_STATEID + 3 * NFSX_UNSIGNED);
5123 stateid.other[0] = stateidp->other[0];
5124 stateid.other[1] = stateidp->other[1];
5125 stateid.other[2] = stateidp->other[2];
5126 error = nfsrpc_layoutget(nmp, nfhp->nfh_fh,
5127 nfhp->nfh_len, iomode, (uint64_t)0, INT64_MAX,
5128 (uint64_t)0, layoutlen, &stateid, &retonclose,
5129 &flh, cred, p, NULL);
5132 stateid.seqid = lyp->nfsly_stateid.seqid;
5133 stateid.other[0] = lyp->nfsly_stateid.other[0];
5134 stateid.other[1] = lyp->nfsly_stateid.other[1];
5135 stateid.other[2] = lyp->nfsly_stateid.other[2];
5136 error = nfsrpc_layoutget(nmp, nfhp->nfh_fh,
5137 nfhp->nfh_len, iomode, off, INT64_MAX,
5138 (uint64_t)0, layoutlen, &stateid, &retonclose,
5139 &flh, cred, p, NULL);
5142 LIST_FOREACH(tflp, &flh, nfsfl_list) {
5143 error = nfscl_adddevinfo(nmp, NULL, tflp);
5145 error = nfsrpc_getdeviceinfo(nmp,
5147 NFSLAYOUT_NFSV4_1_FILES,
5148 notifybitsp, &dip, cred, p);
5151 error = nfscl_adddevinfo(nmp, dip,
5155 "getlayout: cannot add\n");
5160 * nfscl_layout() always returns with the nfsly_lock
5161 * set to a refcnt (shared lock).
5163 error = nfscl_layout(nmp, vp, nfhp->nfh_fh,
5164 nfhp->nfh_len, &stateid, retonclose, &flh, &lyp,
5168 } else if (islocked != 0)
5169 nfsv4_unlock(&lyp->nfsly_lock, 0);
5176 * Do a TCP connection plus exchange id and create session.
5177 * If successful, a "struct nfsclds" is linked into the list for the
5178 * mount point and a pointer to it is returned.
5181 nfsrpc_fillsa(struct nfsmount *nmp, struct sockaddr_storage *ssp,
5182 struct nfsclds **dspp, NFSPROC_T *p)
5184 struct sockaddr_in *msad, *sad, *ssd;
5185 struct sockaddr_in6 *msad6, *sad6, *ssd6;
5186 struct nfsclclient *clp;
5187 struct nfssockreq *nrp;
5188 struct nfsclds *dsp, *tdsp;
5190 enum nfsclds_state retv;
5191 uint32_t sequenceid;
5193 KASSERT(nmp->nm_sockreq.nr_cred != NULL,
5194 ("nfsrpc_fillsa: NULL nr_cred"));
5200 if (ssp->ss_family == AF_INET) {
5201 ssd = (struct sockaddr_in *)ssp;
5205 * Check to see if we already have a session for this
5206 * address that is usable for a DS.
5207 * Note that the MDS's address is in a different place
5208 * than the sessions already acquired for DS's.
5210 msad = (struct sockaddr_in *)nmp->nm_sockreq.nr_nam;
5211 tdsp = TAILQ_FIRST(&nmp->nm_sess);
5212 while (tdsp != NULL) {
5213 if (msad != NULL && msad->sin_family == AF_INET &&
5214 ssd->sin_addr.s_addr == msad->sin_addr.s_addr &&
5215 ssd->sin_port == msad->sin_port &&
5216 (tdsp->nfsclds_flags & NFSCLDS_DS) != 0) {
5219 NFSCL_DEBUG(4, "fnd same addr\n");
5222 tdsp = TAILQ_NEXT(tdsp, nfsclds_list);
5223 if (tdsp != NULL && tdsp->nfsclds_sockp != NULL)
5224 msad = (struct sockaddr_in *)
5225 tdsp->nfsclds_sockp->nr_nam;
5231 /* No IP address match, so look for new/trunked one. */
5232 sad = malloc(sizeof(*sad), M_SONAME, M_WAITOK | M_ZERO);
5233 sad->sin_len = sizeof(*sad);
5234 sad->sin_family = AF_INET;
5235 sad->sin_port = ssd->sin_port;
5236 sad->sin_addr.s_addr = ssd->sin_addr.s_addr;
5237 nrp = malloc(sizeof(*nrp), M_NFSSOCKREQ, M_WAITOK | M_ZERO);
5238 nrp->nr_nam = (struct sockaddr *)sad;
5239 } else if (ssp->ss_family == AF_INET6) {
5240 ssd6 = (struct sockaddr_in6 *)ssp;
5244 * Check to see if we already have a session for this
5245 * address that is usable for a DS.
5246 * Note that the MDS's address is in a different place
5247 * than the sessions already acquired for DS's.
5249 msad6 = (struct sockaddr_in6 *)nmp->nm_sockreq.nr_nam;
5250 tdsp = TAILQ_FIRST(&nmp->nm_sess);
5251 while (tdsp != NULL) {
5252 if (msad6 != NULL && msad6->sin6_family == AF_INET6 &&
5253 IN6_ARE_ADDR_EQUAL(&ssd6->sin6_addr,
5254 &msad6->sin6_addr) &&
5255 ssd6->sin6_port == msad6->sin6_port &&
5256 (tdsp->nfsclds_flags & NFSCLDS_DS) != 0) {
5261 tdsp = TAILQ_NEXT(tdsp, nfsclds_list);
5262 if (tdsp != NULL && tdsp->nfsclds_sockp != NULL)
5263 msad6 = (struct sockaddr_in6 *)
5264 tdsp->nfsclds_sockp->nr_nam;
5270 /* No IP address match, so look for new/trunked one. */
5271 sad6 = malloc(sizeof(*sad6), M_SONAME, M_WAITOK | M_ZERO);
5272 sad6->sin6_len = sizeof(*sad6);
5273 sad6->sin6_family = AF_INET6;
5274 sad6->sin6_port = ssd6->sin6_port;
5275 NFSBCOPY(&ssd6->sin6_addr, &sad6->sin6_addr,
5276 sizeof(struct in6_addr));
5277 nrp = malloc(sizeof(*nrp), M_NFSSOCKREQ, M_WAITOK | M_ZERO);
5278 nrp->nr_nam = (struct sockaddr *)sad6;
5282 nrp->nr_sotype = SOCK_STREAM;
5283 mtx_init(&nrp->nr_mtx, "nfssock", NULL, MTX_DEF);
5284 nrp->nr_prog = NFS_PROG;
5285 nrp->nr_vers = NFS_VER4;
5288 * Use the credentials that were used for the mount, which are
5289 * in nmp->nm_sockreq.nr_cred for newnfs_connect() etc.
5290 * Ref. counting the credentials with crhold() is probably not
5291 * necessary, since nm_sockreq.nr_cred won't be crfree()'d until
5292 * unmount, but I did it anyhow.
5294 nrp->nr_cred = crhold(nmp->nm_sockreq.nr_cred);
5295 error = newnfs_connect(nmp, nrp, NULL, p, 0);
5296 NFSCL_DEBUG(3, "DS connect=%d\n", error);
5298 /* Now, do the exchangeid and create session. */
5300 error = nfsrpc_exchangeid(nmp, clp, nrp, NFSV4EXCH_USEPNFSDS,
5301 &dsp, nrp->nr_cred, p);
5302 NFSCL_DEBUG(3, "DS exchangeid=%d\n", error);
5304 dsp->nfsclds_sockp = nrp;
5306 retv = nfscl_getsameserver(nmp, dsp, &tdsp);
5307 NFSCL_DEBUG(3, "getsame ret=%d\n", retv);
5308 if (retv == NFSDSP_USETHISSESSION) {
5311 * If there is already a session for this server,
5314 (void)newnfs_disconnect(nrp);
5315 nfscl_freenfsclds(dsp);
5319 if (retv == NFSDSP_SEQTHISSESSION)
5320 sequenceid = tdsp->nfsclds_sess.nfsess_sequenceid;
5322 sequenceid = dsp->nfsclds_sess.nfsess_sequenceid;
5324 error = nfsrpc_createsession(nmp, &dsp->nfsclds_sess,
5325 nrp, sequenceid, 0, nrp->nr_cred, p);
5326 NFSCL_DEBUG(3, "DS createsess=%d\n", error);
5328 NFSFREECRED(nrp->nr_cred);
5329 NFSFREEMUTEX(&nrp->nr_mtx);
5330 free(nrp->nr_nam, M_SONAME);
5331 free(nrp, M_NFSSOCKREQ);
5334 NFSCL_DEBUG(3, "add DS session\n");
5336 * Put it at the end of the list. That way the list
5337 * is ordered by when the entry was added. This matters
5338 * since the one done first is the one that should be
5339 * used for sequencid'ing any subsequent create sessions.
5342 TAILQ_INSERT_TAIL(&nmp->nm_sess, dsp, nfsclds_list);
5345 } else if (dsp != NULL)
5346 nfscl_freenfsclds(dsp);
5351 * Do the NFSv4.1 Reclaim Complete.
5354 nfsrpc_reclaimcomplete(struct nfsmount *nmp, struct ucred *cred, NFSPROC_T *p)
5357 struct nfsrv_descript nfsd;
5358 struct nfsrv_descript *nd = &nfsd;
5361 nfscl_reqstart(nd, NFSPROC_RECLAIMCOMPL, nmp, NULL, 0, NULL, NULL);
5362 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
5364 nd->nd_flag |= ND_USEGSSNAME;
5365 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
5366 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
5369 error = nd->nd_repstat;
5370 mbuf_freem(nd->nd_mrep);
5375 * Initialize the slot tables for a session.
5378 nfscl_initsessionslots(struct nfsclsession *sep)
5382 for (i = 0; i < NFSV4_CBSLOTS; i++) {
5383 if (sep->nfsess_cbslots[i].nfssl_reply != NULL)
5384 m_freem(sep->nfsess_cbslots[i].nfssl_reply);
5385 NFSBZERO(&sep->nfsess_cbslots[i], sizeof(struct nfsslot));
5387 for (i = 0; i < 64; i++)
5388 sep->nfsess_slotseq[i] = 0;
5389 sep->nfsess_slots = 0;
5393 * Called to try and do an I/O operation via an NFSv4.1 Data Server (DS).
5396 nfscl_doiods(vnode_t vp, struct uio *uiop, int *iomode, int *must_commit,
5397 uint32_t rwaccess, struct ucred *cred, NFSPROC_T *p)
5399 struct nfsnode *np = VTONFS(vp);
5400 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
5401 struct nfscllayout *layp;
5402 struct nfscldevinfo *dip;
5403 struct nfsclflayout *rflp;
5404 nfsv4stateid_t stateid;
5405 struct ucred *newcred;
5406 uint64_t lastbyte, len, off, oresid, xfer;
5407 int eof, error, iolaymode, recalled;
5410 if (!NFSHASPNFS(nmp) || nfscl_enablecallb == 0 || nfs_numnfscbd == 0 ||
5411 (np->n_flag & NNOLAYOUT) != 0)
5413 /* Now, get a reference cnt on the clientid for this mount. */
5414 if (nfscl_getref(nmp) == 0)
5417 /* Find an appropriate stateid. */
5418 newcred = NFSNEWCRED(cred);
5419 error = nfscl_getstateid(vp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len,
5420 rwaccess, 1, newcred, p, &stateid, &lckp);
5422 NFSFREECRED(newcred);
5426 /* Search for a layout for this file. */
5427 off = uiop->uio_offset;
5428 layp = nfscl_getlayout(nmp->nm_clp, np->n_fhp->nfh_fh,
5429 np->n_fhp->nfh_len, off, &rflp, &recalled);
5430 if (layp == NULL || rflp == NULL) {
5431 if (recalled != 0) {
5432 NFSFREECRED(newcred);
5437 nfscl_rellayout(layp, (rflp == NULL) ? 1 : 0);
5440 /* Try and get a Layout, if it is supported. */
5441 if (rwaccess == NFSV4OPEN_ACCESSWRITE ||
5442 (np->n_flag & NWRITEOPENED) != 0)
5443 iolaymode = NFSLAYOUTIOMODE_RW;
5445 iolaymode = NFSLAYOUTIOMODE_READ;
5446 error = nfsrpc_getlayout(nmp, vp, np->n_fhp, iolaymode,
5447 NULL, &stateid, off, &layp, newcred, p);
5450 np->n_flag |= NNOLAYOUT;
5453 nfscl_lockderef(lckp);
5454 NFSFREECRED(newcred);
5456 nfscl_rellayout(layp, 0);
5463 * Loop around finding a layout that works for the first part of
5464 * this I/O operation, and then call the function that actually
5468 len = (uint64_t)uiop->uio_resid;
5469 while (len > 0 && error == 0 && eof == 0) {
5470 off = uiop->uio_offset;
5471 error = nfscl_findlayoutforio(layp, off, rwaccess, &rflp);
5473 oresid = xfer = (uint64_t)uiop->uio_resid;
5474 if (xfer > (rflp->nfsfl_end - rflp->nfsfl_off))
5475 xfer = rflp->nfsfl_end - rflp->nfsfl_off;
5476 dip = nfscl_getdevinfo(nmp->nm_clp, rflp->nfsfl_dev,
5479 error = nfscl_doflayoutio(vp, uiop, iomode,
5480 must_commit, &eof, &stateid, rwaccess, dip,
5481 layp, rflp, off, xfer, newcred, p);
5482 nfscl_reldevinfo(dip);
5483 lastbyte = off + xfer - 1;
5486 if (lastbyte > layp->nfsly_lastbyte)
5487 layp->nfsly_lastbyte = lastbyte;
5493 len -= (oresid - (uint64_t)uiop->uio_resid);
5497 nfscl_lockderef(lckp);
5498 NFSFREECRED(newcred);
5499 nfscl_rellayout(layp, 0);
5505 * Find a file layout that will handle the first bytes of the requested
5506 * range and return the information from it needed to to the I/O operation.
5509 nfscl_findlayoutforio(struct nfscllayout *lyp, uint64_t off, uint32_t rwaccess,
5510 struct nfsclflayout **retflpp)
5512 struct nfsclflayout *flp, *nflp, *rflp;
5517 /* For reading, do the Read list first and then the Write list. */
5519 if (rw == NFSV4OPEN_ACCESSREAD)
5520 flp = LIST_FIRST(&lyp->nfsly_flayread);
5522 flp = LIST_FIRST(&lyp->nfsly_flayrw);
5523 while (flp != NULL) {
5524 nflp = LIST_NEXT(flp, nfsfl_list);
5525 if (flp->nfsfl_off > off)
5527 if (flp->nfsfl_end > off &&
5528 (rflp == NULL || rflp->nfsfl_end < flp->nfsfl_end))
5532 if (rw == NFSV4OPEN_ACCESSREAD)
5533 rw = NFSV4OPEN_ACCESSWRITE;
5538 /* This one covers the most bytes starting at off. */
5546 * Do I/O using an NFSv4.1 file layout.
5549 nfscl_doflayoutio(vnode_t vp, struct uio *uiop, int *iomode, int *must_commit,
5550 int *eofp, nfsv4stateid_t *stateidp, int rwflag, struct nfscldevinfo *dp,
5551 struct nfscllayout *lyp, struct nfsclflayout *flp, uint64_t off,
5552 uint64_t len, struct ucred *cred, NFSPROC_T *p)
5554 uint64_t io_off, rel_off, stripe_unit_size, transfer, xfer;
5555 int commit_thru_mds, error = 0, stripe_index, stripe_pos;
5558 struct nfsclds **dspp;
5561 rel_off = off - flp->nfsfl_patoff;
5562 stripe_unit_size = (flp->nfsfl_util >> 6) & 0x3ffffff;
5563 stripe_pos = (rel_off / stripe_unit_size + flp->nfsfl_stripe1) %
5564 dp->nfsdi_stripecnt;
5565 transfer = stripe_unit_size - (rel_off % stripe_unit_size);
5567 /* Loop around, doing I/O for each stripe unit. */
5568 while (len > 0 && error == 0) {
5569 stripe_index = nfsfldi_stripeindex(dp, stripe_pos);
5570 dspp = nfsfldi_addr(dp, stripe_index);
5575 if ((flp->nfsfl_util & NFSFLAYUTIL_DENSE) != 0) {
5577 if (stripe_pos >= flp->nfsfl_fhcnt)
5579 fhp = flp->nfsfl_fh[stripe_pos];
5580 io_off = (rel_off / (stripe_unit_size *
5581 dp->nfsdi_stripecnt)) * stripe_unit_size +
5582 rel_off % stripe_unit_size;
5584 /* Sparse layout. */
5585 if (flp->nfsfl_fhcnt > 1) {
5586 if (stripe_index >= flp->nfsfl_fhcnt)
5588 fhp = flp->nfsfl_fh[stripe_index];
5589 } else if (flp->nfsfl_fhcnt == 1)
5590 fhp = flp->nfsfl_fh[0];
5595 if ((flp->nfsfl_util & NFSFLAYUTIL_COMMIT_THRU_MDS) != 0)
5596 commit_thru_mds = 1;
5598 commit_thru_mds = 0;
5599 if (rwflag == FREAD)
5600 error = nfsrpc_readds(vp, uiop, stateidp, eofp, *dspp,
5601 io_off, xfer, fhp, cred, p);
5603 error = nfsrpc_writeds(vp, uiop, iomode, must_commit,
5604 stateidp, *dspp, io_off, xfer, fhp, commit_thru_mds,
5608 lyp->nfsly_flags |= NFSLY_WRITTEN;
5613 transfer = stripe_unit_size;
5614 stripe_pos = (stripe_pos + 1) % dp->nfsdi_stripecnt;
5623 * The actual read RPC done to a DS.
5626 nfsrpc_readds(vnode_t vp, struct uio *uiop, nfsv4stateid_t *stateidp, int *eofp,
5627 struct nfsclds *dsp, uint64_t io_off, int len, struct nfsfh *fhp,
5628 struct ucred *cred, NFSPROC_T *p)
5632 struct nfsrv_descript nfsd;
5633 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
5634 struct nfsrv_descript *nd = &nfsd;
5635 struct nfssockreq *nrp;
5638 nfscl_reqstart(nd, NFSPROC_READDS, nmp, fhp->nfh_fh, fhp->nfh_len,
5639 NULL, &dsp->nfsclds_sess);
5640 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSEQIDZERO);
5641 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED * 3);
5642 txdr_hyper(io_off, tl);
5643 *(tl + 2) = txdr_unsigned(len);
5644 nrp = dsp->nfsclds_sockp;
5646 /* If NULL, use the MDS socket. */
5647 nrp = &nmp->nm_sockreq;
5648 error = newnfs_request(nd, nmp, NULL, nrp, vp, p, cred,
5649 NFS_PROG, NFS_VER4, NULL, 1, NULL, &dsp->nfsclds_sess);
5652 if (nd->nd_repstat != 0) {
5653 error = nd->nd_repstat;
5656 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
5657 *eofp = fxdr_unsigned(int, *tl);
5658 NFSM_STRSIZ(retlen, len);
5659 error = nfsm_mbufuio(nd, uiop, retlen);
5661 if (nd->nd_mrep != NULL)
5662 mbuf_freem(nd->nd_mrep);
5667 * The actual write RPC done to a DS.
5670 nfsrpc_writeds(vnode_t vp, struct uio *uiop, int *iomode, int *must_commit,
5671 nfsv4stateid_t *stateidp, struct nfsclds *dsp, uint64_t io_off, int len,
5672 struct nfsfh *fhp, int commit_thru_mds, struct ucred *cred, NFSPROC_T *p)
5675 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
5676 int error, rlen, commit, committed = NFSWRITE_FILESYNC;
5678 struct nfsrv_descript nfsd;
5679 struct nfsrv_descript *nd = &nfsd;
5680 struct nfssockreq *nrp;
5682 KASSERT(uiop->uio_iovcnt == 1, ("nfs: writerpc iovcnt > 1"));
5684 nfscl_reqstart(nd, NFSPROC_WRITEDS, nmp, fhp->nfh_fh, fhp->nfh_len,
5685 NULL, &dsp->nfsclds_sess);
5686 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSEQIDZERO);
5687 NFSM_BUILD(tl, uint32_t *, NFSX_HYPER + 2 * NFSX_UNSIGNED);
5688 txdr_hyper(io_off, tl);
5690 *tl++ = txdr_unsigned(*iomode);
5691 *tl = txdr_unsigned(len);
5692 nfsm_uiombuf(nd, uiop, len);
5693 nrp = dsp->nfsclds_sockp;
5695 /* If NULL, use the MDS socket. */
5696 nrp = &nmp->nm_sockreq;
5697 error = newnfs_request(nd, nmp, NULL, nrp, vp, p, cred,
5698 NFS_PROG, NFS_VER4, NULL, 1, NULL, &dsp->nfsclds_sess);
5701 if (nd->nd_repstat != 0) {
5703 * In case the rpc gets retried, roll
5704 * the uio fileds changed by nfsm_uiombuf()
5707 uiop->uio_offset -= len;
5708 uio_uio_resid_add(uiop, len);
5709 uio_iov_base_add(uiop, -len);
5710 uio_iov_len_add(uiop, len);
5711 error = nd->nd_repstat;
5713 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED + NFSX_VERF);
5714 rlen = fxdr_unsigned(int, *tl++);
5718 } else if (rlen < len) {
5719 backup = len - rlen;
5720 uio_iov_base_add(uiop, -(backup));
5721 uio_iov_len_add(uiop, backup);
5722 uiop->uio_offset -= backup;
5723 uio_uio_resid_add(uiop, backup);
5726 commit = fxdr_unsigned(int, *tl++);
5729 * Return the lowest committment level
5730 * obtained by any of the RPCs.
5732 if (committed == NFSWRITE_FILESYNC)
5734 else if (committed == NFSWRITE_DATASYNC &&
5735 commit == NFSWRITE_UNSTABLE)
5737 if (commit_thru_mds != 0) {
5739 if (!NFSHASWRITEVERF(nmp)) {
5740 NFSBCOPY(tl, nmp->nm_verf, NFSX_VERF);
5741 NFSSETWRITEVERF(nmp);
5742 } else if (NFSBCMP(tl, nmp->nm_verf, NFSX_VERF)) {
5744 NFSBCOPY(tl, nmp->nm_verf, NFSX_VERF);
5749 if ((dsp->nfsclds_flags & NFSCLDS_HASWRITEVERF) == 0) {
5750 NFSBCOPY(tl, dsp->nfsclds_verf, NFSX_VERF);
5751 dsp->nfsclds_flags |= NFSCLDS_HASWRITEVERF;
5752 } else if (NFSBCMP(tl, dsp->nfsclds_verf, NFSX_VERF)) {
5754 NFSBCOPY(tl, dsp->nfsclds_verf, NFSX_VERF);
5760 if (nd->nd_mrep != NULL)
5761 mbuf_freem(nd->nd_mrep);
5762 *iomode = committed;
5763 if (nd->nd_repstat != 0 && error == 0)
5764 error = nd->nd_repstat;
5769 * Free up the nfsclds structure.
5772 nfscl_freenfsclds(struct nfsclds *dsp)
5778 if (dsp->nfsclds_sockp != NULL) {
5779 NFSFREECRED(dsp->nfsclds_sockp->nr_cred);
5780 NFSFREEMUTEX(&dsp->nfsclds_sockp->nr_mtx);
5781 free(dsp->nfsclds_sockp->nr_nam, M_SONAME);
5782 free(dsp->nfsclds_sockp, M_NFSSOCKREQ);
5784 NFSFREEMUTEX(&dsp->nfsclds_mtx);
5785 NFSFREEMUTEX(&dsp->nfsclds_sess.nfsess_mtx);
5786 for (i = 0; i < NFSV4_CBSLOTS; i++) {
5787 if (dsp->nfsclds_sess.nfsess_cbslots[i].nfssl_reply != NULL)
5789 dsp->nfsclds_sess.nfsess_cbslots[i].nfssl_reply);
5791 free(dsp, M_NFSCLDS);
5794 static enum nfsclds_state
5795 nfscl_getsameserver(struct nfsmount *nmp, struct nfsclds *newdsp,
5796 struct nfsclds **retdspp)
5798 struct nfsclds *dsp, *cur_dsp;
5801 * Search the list of nfsclds structures for one with the same
5805 TAILQ_FOREACH(dsp, &nmp->nm_sess, nfsclds_list) {
5806 if (dsp->nfsclds_servownlen == newdsp->nfsclds_servownlen &&
5807 dsp->nfsclds_servownlen != 0 &&
5808 !NFSBCMP(dsp->nfsclds_serverown, newdsp->nfsclds_serverown,
5809 dsp->nfsclds_servownlen)) {
5810 NFSCL_DEBUG(4, "fnd same fdsp=%p dsp=%p flg=0x%x\n",
5811 TAILQ_FIRST(&nmp->nm_sess), dsp,
5812 dsp->nfsclds_flags);
5813 /* Server major id matches. */
5814 if ((dsp->nfsclds_flags & NFSCLDS_DS) != 0) {
5816 return (NFSDSP_USETHISSESSION);
5820 * Note the first match, so it can be used for
5821 * sequence'ing new sessions.
5823 if (cur_dsp == NULL)
5827 if (cur_dsp != NULL) {
5829 return (NFSDSP_SEQTHISSESSION);
5831 return (NFSDSP_NOTFOUND);
5836 * NFS commit rpc to a DS.
5839 nfsrpc_commitds(vnode_t vp, uint64_t offset, int cnt, struct nfsclds *dsp,
5840 struct nfsfh *fhp, struct ucred *cred, NFSPROC_T *p, void *stuff)
5843 struct nfsrv_descript nfsd, *nd = &nfsd;
5844 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
5845 struct nfssockreq *nrp;
5848 nfscl_reqstart(nd, NFSPROC_COMMITDS, nmp, fhp->nfh_fh, fhp->nfh_len,
5849 NULL, &dsp->nfsclds_sess);
5850 NFSM_BUILD(tl, uint32_t *, NFSX_HYPER + NFSX_UNSIGNED);
5851 txdr_hyper(offset, tl);
5853 *tl = txdr_unsigned(cnt);
5854 nrp = dsp->nfsclds_sockp;
5856 /* If NULL, use the MDS socket. */
5857 nrp = &nmp->nm_sockreq;
5858 error = newnfs_request(nd, nmp, NULL, nrp, vp, p, cred,
5859 NFS_PROG, NFS_VER4, NULL, 1, NULL, &dsp->nfsclds_sess);
5862 if (nd->nd_repstat == 0) {
5863 NFSM_DISSECT(tl, u_int32_t *, NFSX_VERF);
5865 if (NFSBCMP(tl, dsp->nfsclds_verf, NFSX_VERF)) {
5866 NFSBCOPY(tl, dsp->nfsclds_verf, NFSX_VERF);
5867 error = NFSERR_STALEWRITEVERF;
5872 if (error == 0 && nd->nd_repstat != 0)
5873 error = nd->nd_repstat;
5874 mbuf_freem(nd->nd_mrep);