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);
542 if (ret == NFSERR_STALECLIENTID ||
543 ret == NFSERR_STALEDONTRECOVER ||
544 ret == NFSERR_BADSESSION)
549 if (nd->nd_repstat != 0 && error == 0)
550 error = nd->nd_repstat;
551 if (error == NFSERR_STALECLIENTID || error == NFSERR_BADSESSION)
552 nfscl_initiate_recovery(op->nfso_own->nfsow_clp);
556 else if (ndp != NULL)
557 FREE((caddr_t)ndp, M_NFSCLDELEG);
558 mbuf_freem(nd->nd_mrep);
566 nfsrpc_opendowngrade(vnode_t vp, u_int32_t mode, struct nfsclopen *op,
567 struct ucred *cred, NFSPROC_T *p)
570 struct nfsrv_descript nfsd, *nd = &nfsd;
573 NFSCL_REQSTART(nd, NFSPROC_OPENDOWNGRADE, vp);
574 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID + 3 * NFSX_UNSIGNED);
575 if (NFSHASNFSV4N(VFSTONFS(vnode_mount(vp))))
578 *tl++ = op->nfso_stateid.seqid;
579 *tl++ = op->nfso_stateid.other[0];
580 *tl++ = op->nfso_stateid.other[1];
581 *tl++ = op->nfso_stateid.other[2];
582 *tl++ = txdr_unsigned(op->nfso_own->nfsow_seqid);
583 *tl++ = txdr_unsigned(mode & NFSV4OPEN_ACCESSBOTH);
584 *tl = txdr_unsigned((mode >> NFSLCK_SHIFT) & NFSV4OPEN_DENYBOTH);
585 error = nfscl_request(nd, vp, p, cred, NULL);
588 NFSCL_INCRSEQID(op->nfso_own->nfsow_seqid, nd);
589 if (!nd->nd_repstat) {
590 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID);
591 op->nfso_stateid.seqid = *tl++;
592 op->nfso_stateid.other[0] = *tl++;
593 op->nfso_stateid.other[1] = *tl++;
594 op->nfso_stateid.other[2] = *tl;
596 if (nd->nd_repstat && error == 0)
597 error = nd->nd_repstat;
598 if (error == NFSERR_STALESTATEID || error == NFSERR_BADSESSION)
599 nfscl_initiate_recovery(op->nfso_own->nfsow_clp);
601 mbuf_freem(nd->nd_mrep);
606 * V4 Close operation.
609 nfsrpc_close(vnode_t vp, int doclose, NFSPROC_T *p)
611 struct nfsclclient *clp;
614 if (vnode_vtype(vp) != VREG)
617 error = nfscl_doclose(vp, &clp, p);
619 error = nfscl_getclose(vp, &clp);
623 nfscl_clientrelease(clp);
631 nfsrpc_doclose(struct nfsmount *nmp, struct nfsclopen *op, NFSPROC_T *p)
633 struct nfsrv_descript nfsd, *nd = &nfsd;
634 struct nfscllockowner *lp, *nlp;
635 struct nfscllock *lop, *nlop;
637 u_int64_t off = 0, len = 0;
638 u_int32_t type = NFSV4LOCKT_READ;
639 int error, do_unlock, trycnt;
641 tcred = newnfs_getcred();
642 newnfs_copycred(&op->nfso_cred, tcred);
644 * (Theoretically this could be done in the same
645 * compound as the close, but having multiple
646 * sequenced Ops in the same compound might be
647 * too scary for some servers.)
649 if (op->nfso_posixlock) {
652 type = NFSV4LOCKT_READ;
656 * Since this function is only called from VOP_INACTIVE(), no
657 * other thread will be manipulating this Open. As such, the
658 * lock lists are not being changed by other threads, so it should
659 * be safe to do this without locking.
661 LIST_FOREACH(lp, &op->nfso_lock, nfsl_list) {
663 LIST_FOREACH_SAFE(lop, &lp->nfsl_lock, nfslo_list, nlop) {
664 if (op->nfso_posixlock == 0) {
665 off = lop->nfslo_first;
666 len = lop->nfslo_end - lop->nfslo_first;
667 if (lop->nfslo_type == F_WRLCK)
668 type = NFSV4LOCKT_WRITE;
670 type = NFSV4LOCKT_READ;
675 error = nfsrpc_locku(nd, nmp, lp, off,
676 len, type, tcred, p, 0);
677 if ((nd->nd_repstat == NFSERR_GRACE ||
678 nd->nd_repstat == NFSERR_DELAY) &&
680 (void) nfs_catnap(PZERO,
683 } while ((nd->nd_repstat == NFSERR_GRACE ||
684 nd->nd_repstat == NFSERR_DELAY) &&
685 error == 0 && trycnt++ < 5);
686 if (op->nfso_posixlock)
689 nfscl_freelock(lop, 0);
692 * Do a ReleaseLockOwner.
693 * The lock owner name nfsl_owner may be used by other opens for
694 * other files but the lock_owner4 name that nfsrpc_rellockown()
695 * puts on the wire has the file handle for this file appended
696 * to it, so it can be done now.
698 (void)nfsrpc_rellockown(nmp, lp, lp->nfsl_open->nfso_fh,
699 lp->nfsl_open->nfso_fhlen, tcred, p);
703 * There could be other Opens for different files on the same
704 * OpenOwner, so locking is required.
707 nfscl_lockexcl(&op->nfso_own->nfsow_rwlock, NFSCLSTATEMUTEXPTR);
710 error = nfscl_tryclose(op, tcred, nmp, p);
711 if (error == NFSERR_GRACE)
712 (void) nfs_catnap(PZERO, error, "nfs_close");
713 } while (error == NFSERR_GRACE);
715 nfscl_lockunlock(&op->nfso_own->nfsow_rwlock);
717 LIST_FOREACH_SAFE(lp, &op->nfso_lock, nfsl_list, nlp)
718 nfscl_freelockowner(lp, 0);
719 nfscl_freeopen(op, 0);
725 * The actual Close RPC.
728 nfsrpc_closerpc(struct nfsrv_descript *nd, struct nfsmount *nmp,
729 struct nfsclopen *op, struct ucred *cred, NFSPROC_T *p,
735 nfscl_reqstart(nd, NFSPROC_CLOSE, nmp, op->nfso_fh,
736 op->nfso_fhlen, NULL, NULL);
737 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED + NFSX_STATEID);
738 *tl++ = txdr_unsigned(op->nfso_own->nfsow_seqid);
739 if (NFSHASNFSV4N(nmp))
742 *tl++ = op->nfso_stateid.seqid;
743 *tl++ = op->nfso_stateid.other[0];
744 *tl++ = op->nfso_stateid.other[1];
745 *tl = op->nfso_stateid.other[2];
747 nd->nd_flag |= ND_USEGSSNAME;
748 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
749 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
752 NFSCL_INCRSEQID(op->nfso_own->nfsow_seqid, nd);
753 if (nd->nd_repstat == 0)
754 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID);
755 error = nd->nd_repstat;
756 if (error == NFSERR_STALESTATEID || error == NFSERR_BADSESSION)
757 nfscl_initiate_recovery(op->nfso_own->nfsow_clp);
759 mbuf_freem(nd->nd_mrep);
764 * V4 Open Confirm RPC.
767 nfsrpc_openconfirm(vnode_t vp, u_int8_t *nfhp, int fhlen,
768 struct nfsclopen *op, struct ucred *cred, NFSPROC_T *p)
771 struct nfsrv_descript nfsd, *nd = &nfsd;
772 struct nfsmount *nmp;
775 nmp = VFSTONFS(vnode_mount(vp));
776 if (NFSHASNFSV4N(nmp))
777 return (0); /* No confirmation for NFSv4.1. */
778 nfscl_reqstart(nd, NFSPROC_OPENCONFIRM, nmp, nfhp, fhlen, NULL, NULL);
779 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED + NFSX_STATEID);
780 *tl++ = op->nfso_stateid.seqid;
781 *tl++ = op->nfso_stateid.other[0];
782 *tl++ = op->nfso_stateid.other[1];
783 *tl++ = op->nfso_stateid.other[2];
784 *tl = txdr_unsigned(op->nfso_own->nfsow_seqid);
785 error = nfscl_request(nd, vp, p, cred, NULL);
788 NFSCL_INCRSEQID(op->nfso_own->nfsow_seqid, nd);
789 if (!nd->nd_repstat) {
790 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID);
791 op->nfso_stateid.seqid = *tl++;
792 op->nfso_stateid.other[0] = *tl++;
793 op->nfso_stateid.other[1] = *tl++;
794 op->nfso_stateid.other[2] = *tl;
796 error = nd->nd_repstat;
797 if (error == NFSERR_STALESTATEID || error == NFSERR_BADSESSION)
798 nfscl_initiate_recovery(op->nfso_own->nfsow_clp);
800 mbuf_freem(nd->nd_mrep);
805 * Do the setclientid and setclientid confirm RPCs. Called from nfs_statfs()
806 * when a mount has just occurred and when the server replies NFSERR_EXPIRED.
809 nfsrpc_setclient(struct nfsmount *nmp, struct nfsclclient *clp, int reclaim,
810 struct ucred *cred, NFSPROC_T *p)
813 struct nfsrv_descript nfsd;
814 struct nfsrv_descript *nd = &nfsd;
815 nfsattrbit_t attrbits;
816 u_int8_t *cp = NULL, *cp2, addr[INET6_ADDRSTRLEN + 9];
818 int error, isinet6 = 0, callblen;
821 static u_int32_t rev = 0;
822 struct nfsclds *dsp, *ndsp, *tdsp;
824 if (nfsboottime.tv_sec == 0)
825 NFSSETBOOTTIME(nfsboottime);
826 clp->nfsc_rev = rev++;
827 if (NFSHASNFSV4N(nmp)) {
828 error = nfsrpc_exchangeid(nmp, clp, &nmp->nm_sockreq,
829 NFSV4EXCH_USEPNFSMDS | NFSV4EXCH_USENONPNFS, &dsp, cred, p);
830 NFSCL_DEBUG(1, "aft exch=%d\n", error);
832 error = nfsrpc_createsession(nmp, &dsp->nfsclds_sess,
834 dsp->nfsclds_sess.nfsess_sequenceid, 1, cred, p);
837 TAILQ_FOREACH_SAFE(tdsp, &nmp->nm_sess,
839 nfscl_freenfsclds(tdsp);
840 TAILQ_INIT(&nmp->nm_sess);
841 TAILQ_INSERT_HEAD(&nmp->nm_sess, dsp,
845 nfscl_freenfsclds(dsp);
846 NFSCL_DEBUG(1, "aft createsess=%d\n", error);
848 if (error == 0 && reclaim == 0) {
849 error = nfsrpc_reclaimcomplete(nmp, cred, p);
850 NFSCL_DEBUG(1, "aft reclaimcomp=%d\n", error);
851 if (error == NFSERR_COMPLETEALREADY ||
852 error == NFSERR_NOTSUPP)
853 /* Ignore this error. */
860 * Allocate a single session structure for NFSv4.0, because some of
861 * the fields are used by NFSv4.0 although it doesn't do a session.
863 dsp = malloc(sizeof(struct nfsclds), M_NFSCLDS, M_WAITOK | M_ZERO);
864 mtx_init(&dsp->nfsclds_mtx, "nfsds", NULL, MTX_DEF);
865 mtx_init(&dsp->nfsclds_sess.nfsess_mtx, "nfssession", NULL, MTX_DEF);
867 TAILQ_INSERT_HEAD(&nmp->nm_sess, dsp, nfsclds_list);
870 nfscl_reqstart(nd, NFSPROC_SETCLIENTID, nmp, NULL, 0, NULL, NULL);
871 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
872 *tl++ = txdr_unsigned(nfsboottime.tv_sec);
873 *tl = txdr_unsigned(clp->nfsc_rev);
874 (void) nfsm_strtom(nd, clp->nfsc_id, clp->nfsc_idlen);
877 * set up the callback address
879 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
880 *tl = txdr_unsigned(NFS_CALLBCKPROG);
881 callblen = strlen(nfsv4_callbackaddr);
883 cp = nfscl_getmyip(nmp, &isinet6);
884 if (nfscl_enablecallb && nfs_numnfscbd > 0 &&
885 (callblen > 0 || cp != NULL)) {
886 port = htons(nfsv4_cbport);
887 cp2 = (u_int8_t *)&port;
890 strchr(nfsv4_callbackaddr, ':')) || isinet6) {
891 char ip6buf[INET6_ADDRSTRLEN], *ip6add;
893 (void) nfsm_strtom(nd, "tcp6", 4);
895 ip6_sprintf(ip6buf, (struct in6_addr *)cp);
898 ip6add = nfsv4_callbackaddr;
900 snprintf(addr, INET6_ADDRSTRLEN + 9, "%s.%d.%d",
901 ip6add, cp2[0], cp2[1]);
905 (void) nfsm_strtom(nd, "tcp", 3);
907 snprintf(addr, INET6_ADDRSTRLEN + 9,
908 "%d.%d.%d.%d.%d.%d", cp[0], cp[1],
909 cp[2], cp[3], cp2[0], cp2[1]);
911 snprintf(addr, INET6_ADDRSTRLEN + 9,
912 "%s.%d.%d", nfsv4_callbackaddr,
915 (void) nfsm_strtom(nd, addr, strlen(addr));
917 (void) nfsm_strtom(nd, "tcp", 3);
918 (void) nfsm_strtom(nd, "0.0.0.0.0.0", 11);
920 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
921 *tl = txdr_unsigned(clp->nfsc_cbident);
922 nd->nd_flag |= ND_USEGSSNAME;
923 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
924 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
927 if (nd->nd_repstat == 0) {
928 NFSM_DISSECT(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
929 NFSMNT_MDSSESSION(nmp)->nfsess_clientid.lval[0] = *tl++;
930 NFSMNT_MDSSESSION(nmp)->nfsess_clientid.lval[1] = *tl++;
931 confirm.lval[0] = *tl++;
932 confirm.lval[1] = *tl;
933 mbuf_freem(nd->nd_mrep);
939 nfscl_reqstart(nd, NFSPROC_SETCLIENTIDCFRM, nmp, NULL, 0, NULL,
941 NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
942 *tl++ = NFSMNT_MDSSESSION(nmp)->nfsess_clientid.lval[0];
943 *tl++ = NFSMNT_MDSSESSION(nmp)->nfsess_clientid.lval[1];
944 *tl++ = confirm.lval[0];
945 *tl = confirm.lval[1];
946 nd->nd_flag |= ND_USEGSSNAME;
947 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p,
948 cred, NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
951 mbuf_freem(nd->nd_mrep);
953 if (nd->nd_repstat == 0) {
954 nfscl_reqstart(nd, NFSPROC_GETATTR, nmp, nmp->nm_fh,
955 nmp->nm_fhsize, NULL, NULL);
956 NFSZERO_ATTRBIT(&attrbits);
957 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_LEASETIME);
958 (void) nfsrv_putattrbit(nd, &attrbits);
959 nd->nd_flag |= ND_USEGSSNAME;
960 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p,
961 cred, NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
964 if (nd->nd_repstat == 0) {
965 error = nfsv4_loadattr(nd, NULL, NULL, NULL, NULL, 0, NULL,
966 NULL, NULL, NULL, NULL, 0, NULL, &lease, NULL, p, cred);
969 clp->nfsc_renew = NFSCL_RENEW(lease);
970 clp->nfsc_expire = NFSD_MONOSEC + clp->nfsc_renew;
971 clp->nfsc_clientidrev++;
972 if (clp->nfsc_clientidrev == 0)
973 clp->nfsc_clientidrev++;
977 error = nd->nd_repstat;
979 mbuf_freem(nd->nd_mrep);
987 nfsrpc_getattr(vnode_t vp, struct ucred *cred, NFSPROC_T *p,
988 struct nfsvattr *nap, void *stuff)
990 struct nfsrv_descript nfsd, *nd = &nfsd;
992 nfsattrbit_t attrbits;
994 NFSCL_REQSTART(nd, NFSPROC_GETATTR, vp);
995 if (nd->nd_flag & ND_NFSV4) {
996 NFSGETATTR_ATTRBIT(&attrbits);
997 (void) nfsrv_putattrbit(nd, &attrbits);
999 error = nfscl_request(nd, vp, p, cred, stuff);
1002 if (!nd->nd_repstat)
1003 error = nfsm_loadattr(nd, nap);
1005 error = nd->nd_repstat;
1006 mbuf_freem(nd->nd_mrep);
1011 * nfs getattr call with non-vnode arguemnts.
1014 nfsrpc_getattrnovp(struct nfsmount *nmp, u_int8_t *fhp, int fhlen, int syscred,
1015 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, u_int64_t *xidp,
1018 struct nfsrv_descript nfsd, *nd = &nfsd;
1019 int error, vers = NFS_VER2;
1020 nfsattrbit_t attrbits;
1022 nfscl_reqstart(nd, NFSPROC_GETATTR, nmp, fhp, fhlen, NULL, NULL);
1023 if (nd->nd_flag & ND_NFSV4) {
1025 NFSGETATTR_ATTRBIT(&attrbits);
1026 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_LEASETIME);
1027 (void) nfsrv_putattrbit(nd, &attrbits);
1028 } else if (nd->nd_flag & ND_NFSV3) {
1032 nd->nd_flag |= ND_USEGSSNAME;
1033 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
1034 NFS_PROG, vers, NULL, 1, xidp, NULL);
1037 if (nd->nd_repstat == 0) {
1038 if ((nd->nd_flag & ND_NFSV4) != 0)
1039 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0,
1040 NULL, NULL, NULL, NULL, NULL, 0, NULL, leasep, NULL,
1043 error = nfsm_loadattr(nd, nap);
1045 error = nd->nd_repstat;
1046 mbuf_freem(nd->nd_mrep);
1051 * Do an nfs setattr operation.
1054 nfsrpc_setattr(vnode_t vp, struct vattr *vap, NFSACL_T *aclp,
1055 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *rnap, int *attrflagp,
1058 int error, expireret = 0, openerr, retrycnt;
1059 u_int32_t clidrev = 0, mode;
1060 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
1062 nfsv4stateid_t stateid;
1065 if (nmp->nm_clp != NULL)
1066 clidrev = nmp->nm_clp->nfsc_clientidrev;
1067 if (vap != NULL && NFSATTRISSET(u_quad_t, vap, va_size))
1068 mode = NFSV4OPEN_ACCESSWRITE;
1070 mode = NFSV4OPEN_ACCESSREAD;
1075 if (NFSHASNFSV4(nmp)) {
1076 nfhp = VTONFS(vp)->n_fhp;
1077 error = nfscl_getstateid(vp, nfhp->nfh_fh,
1078 nfhp->nfh_len, mode, 0, cred, p, &stateid, &lckp);
1079 if (error && vnode_vtype(vp) == VREG &&
1080 (mode == NFSV4OPEN_ACCESSWRITE ||
1081 nfstest_openallsetattr)) {
1083 * No Open stateid, so try and open the file
1086 if (mode == NFSV4OPEN_ACCESSWRITE)
1087 openerr = nfsrpc_open(vp, FWRITE, cred,
1090 openerr = nfsrpc_open(vp, FREAD, cred,
1093 (void) nfscl_getstateid(vp,
1094 nfhp->nfh_fh, nfhp->nfh_len,
1095 mode, 0, cred, p, &stateid, &lckp);
1099 error = nfsrpc_setattrrpc(vp, vap, &stateid, cred, p,
1100 rnap, attrflagp, stuff);
1102 error = nfsrpc_setaclrpc(vp, cred, p, aclp, &stateid,
1104 if (error == NFSERR_STALESTATEID || error == NFSERR_BADSESSION)
1105 nfscl_initiate_recovery(nmp->nm_clp);
1107 nfscl_lockderef(lckp);
1109 (void) nfsrpc_close(vp, 0, p);
1110 if (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
1111 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
1112 error == NFSERR_OLDSTATEID || error == NFSERR_BADSESSION) {
1113 (void) nfs_catnap(PZERO, error, "nfs_setattr");
1114 } else if ((error == NFSERR_EXPIRED ||
1115 error == NFSERR_BADSTATEID) && clidrev != 0) {
1116 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
1119 } while (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
1120 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
1121 error == NFSERR_BADSESSION ||
1122 (error == NFSERR_OLDSTATEID && retrycnt < 20) ||
1123 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
1124 expireret == 0 && clidrev != 0 && retrycnt < 4));
1125 if (error && retrycnt >= 4)
1131 nfsrpc_setattrrpc(vnode_t vp, struct vattr *vap,
1132 nfsv4stateid_t *stateidp, struct ucred *cred, NFSPROC_T *p,
1133 struct nfsvattr *rnap, int *attrflagp, void *stuff)
1136 struct nfsrv_descript nfsd, *nd = &nfsd;
1138 nfsattrbit_t attrbits;
1141 NFSCL_REQSTART(nd, NFSPROC_SETATTR, vp);
1142 if (nd->nd_flag & ND_NFSV4)
1143 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID);
1144 vap->va_type = vnode_vtype(vp);
1145 nfscl_fillsattr(nd, vap, vp, NFSSATTR_FULL, 0);
1146 if (nd->nd_flag & ND_NFSV3) {
1147 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1149 } else if (nd->nd_flag & ND_NFSV4) {
1150 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1151 *tl = txdr_unsigned(NFSV4OP_GETATTR);
1152 NFSGETATTR_ATTRBIT(&attrbits);
1153 (void) nfsrv_putattrbit(nd, &attrbits);
1155 error = nfscl_request(nd, vp, p, cred, stuff);
1158 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4))
1159 error = nfscl_wcc_data(nd, vp, rnap, attrflagp, NULL, stuff);
1160 if ((nd->nd_flag & ND_NFSV4) && !error)
1161 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
1162 if (!(nd->nd_flag & ND_NFSV3) && !nd->nd_repstat && !error)
1163 error = nfscl_postop_attr(nd, rnap, attrflagp, stuff);
1164 mbuf_freem(nd->nd_mrep);
1165 if (nd->nd_repstat && !error)
1166 error = nd->nd_repstat;
1174 nfsrpc_lookup(vnode_t dvp, char *name, int len, struct ucred *cred,
1175 NFSPROC_T *p, struct nfsvattr *dnap, struct nfsvattr *nap,
1176 struct nfsfh **nfhpp, int *attrflagp, int *dattrflagp, void *stuff)
1179 struct nfsrv_descript nfsd, *nd = &nfsd;
1180 struct nfsmount *nmp;
1183 nfsattrbit_t attrbits;
1184 int error = 0, lookupp = 0;
1188 if (vnode_vtype(dvp) != VDIR)
1190 nmp = VFSTONFS(vnode_mount(dvp));
1191 if (len > NFS_MAXNAMLEN)
1192 return (ENAMETOOLONG);
1193 if (NFSHASNFSV4(nmp) && len == 1 &&
1196 * Just return the current dir's fh.
1199 MALLOC(nfhp, struct nfsfh *, sizeof (struct nfsfh) +
1200 np->n_fhp->nfh_len, M_NFSFH, M_WAITOK);
1201 nfhp->nfh_len = np->n_fhp->nfh_len;
1202 NFSBCOPY(np->n_fhp->nfh_fh, nfhp->nfh_fh, nfhp->nfh_len);
1206 if (NFSHASNFSV4(nmp) && len == 2 &&
1207 name[0] == '.' && name[1] == '.') {
1209 NFSCL_REQSTART(nd, NFSPROC_LOOKUPP, dvp);
1211 NFSCL_REQSTART(nd, NFSPROC_LOOKUP, dvp);
1212 (void) nfsm_strtom(nd, name, len);
1214 if (nd->nd_flag & ND_NFSV4) {
1215 NFSGETATTR_ATTRBIT(&attrbits);
1216 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1217 *tl++ = txdr_unsigned(NFSV4OP_GETFH);
1218 *tl = txdr_unsigned(NFSV4OP_GETATTR);
1219 (void) nfsrv_putattrbit(nd, &attrbits);
1221 error = nfscl_request(nd, dvp, p, cred, stuff);
1224 if (nd->nd_repstat) {
1226 * When an NFSv4 Lookupp returns ENOENT, it means that
1227 * the lookup is at the root of an fs, so return this dir.
1229 if (nd->nd_repstat == NFSERR_NOENT && lookupp) {
1231 MALLOC(nfhp, struct nfsfh *, sizeof (struct nfsfh) +
1232 np->n_fhp->nfh_len, M_NFSFH, M_WAITOK);
1233 nfhp->nfh_len = np->n_fhp->nfh_len;
1234 NFSBCOPY(np->n_fhp->nfh_fh, nfhp->nfh_fh, nfhp->nfh_len);
1236 mbuf_freem(nd->nd_mrep);
1239 if (nd->nd_flag & ND_NFSV3)
1240 error = nfscl_postop_attr(nd, dnap, dattrflagp, stuff);
1241 else if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) ==
1243 /* Load the directory attributes. */
1244 error = nfsm_loadattr(nd, dnap);
1250 if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) == ND_NFSV4) {
1251 /* Load the directory attributes. */
1252 error = nfsm_loadattr(nd, dnap);
1256 /* Skip over the Lookup and GetFH operation status values. */
1257 NFSM_DISSECT(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1259 error = nfsm_getfh(nd, nfhpp);
1263 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
1264 if ((nd->nd_flag & ND_NFSV3) && !error)
1265 error = nfscl_postop_attr(nd, dnap, dattrflagp, stuff);
1267 mbuf_freem(nd->nd_mrep);
1268 if (!error && nd->nd_repstat)
1269 error = nd->nd_repstat;
1274 * Do a readlink rpc.
1277 nfsrpc_readlink(vnode_t vp, struct uio *uiop, struct ucred *cred,
1278 NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, void *stuff)
1281 struct nfsrv_descript nfsd, *nd = &nfsd;
1282 struct nfsnode *np = VTONFS(vp);
1283 nfsattrbit_t attrbits;
1284 int error, len, cangetattr = 1;
1287 NFSCL_REQSTART(nd, NFSPROC_READLINK, vp);
1288 if (nd->nd_flag & ND_NFSV4) {
1290 * And do a Getattr op.
1292 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1293 *tl = txdr_unsigned(NFSV4OP_GETATTR);
1294 NFSGETATTR_ATTRBIT(&attrbits);
1295 (void) nfsrv_putattrbit(nd, &attrbits);
1297 error = nfscl_request(nd, vp, p, cred, stuff);
1300 if (nd->nd_flag & ND_NFSV3)
1301 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
1302 if (!nd->nd_repstat && !error) {
1303 NFSM_STRSIZ(len, NFS_MAXPATHLEN);
1305 * This seems weird to me, but must have been added to
1306 * FreeBSD for some reason. The only thing I can think of
1307 * is that there was/is some server that replies with
1308 * more link data than it should?
1310 if (len == NFS_MAXPATHLEN) {
1312 if (np->n_size > 0 && np->n_size < NFS_MAXPATHLEN) {
1318 error = nfsm_mbufuio(nd, uiop, len);
1319 if ((nd->nd_flag & ND_NFSV4) && !error && cangetattr)
1320 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
1322 if (nd->nd_repstat && !error)
1323 error = nd->nd_repstat;
1325 mbuf_freem(nd->nd_mrep);
1333 nfsrpc_read(vnode_t vp, struct uio *uiop, struct ucred *cred,
1334 NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, void *stuff)
1336 int error, expireret = 0, retrycnt;
1337 u_int32_t clidrev = 0;
1338 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
1339 struct nfsnode *np = VTONFS(vp);
1340 struct ucred *newcred;
1341 struct nfsfh *nfhp = NULL;
1342 nfsv4stateid_t stateid;
1345 if (nmp->nm_clp != NULL)
1346 clidrev = nmp->nm_clp->nfsc_clientidrev;
1348 if (NFSHASNFSV4(nmp)) {
1350 newcred = NFSNEWCRED(cred);
1355 if (NFSHASNFSV4(nmp))
1356 (void)nfscl_getstateid(vp, nfhp->nfh_fh, nfhp->nfh_len,
1357 NFSV4OPEN_ACCESSREAD, 0, newcred, p, &stateid,
1359 error = nfsrpc_readrpc(vp, uiop, newcred, &stateid, p, nap,
1361 if (error == NFSERR_STALESTATEID || error == NFSERR_BADSESSION)
1362 nfscl_initiate_recovery(nmp->nm_clp);
1364 nfscl_lockderef(lckp);
1365 if (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
1366 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
1367 error == NFSERR_OLDSTATEID || error == NFSERR_BADSESSION) {
1368 (void) nfs_catnap(PZERO, error, "nfs_read");
1369 } else if ((error == NFSERR_EXPIRED ||
1370 error == NFSERR_BADSTATEID) && clidrev != 0) {
1371 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
1374 } while (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
1375 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
1376 error == NFSERR_BADSESSION ||
1377 (error == NFSERR_OLDSTATEID && retrycnt < 20) ||
1378 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
1379 expireret == 0 && clidrev != 0 && retrycnt < 4));
1380 if (error && retrycnt >= 4)
1382 if (NFSHASNFSV4(nmp))
1383 NFSFREECRED(newcred);
1388 * The actual read RPC.
1391 nfsrpc_readrpc(vnode_t vp, struct uio *uiop, struct ucred *cred,
1392 nfsv4stateid_t *stateidp, NFSPROC_T *p, struct nfsvattr *nap,
1393 int *attrflagp, void *stuff)
1396 int error = 0, len, retlen, tsiz, eof = 0;
1397 struct nfsrv_descript nfsd;
1398 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
1399 struct nfsrv_descript *nd = &nfsd;
1404 tsiz = uio_uio_resid(uiop);
1405 tmp_off = uiop->uio_offset + tsiz;
1407 if (tmp_off > nmp->nm_maxfilesize || tmp_off < uiop->uio_offset) {
1411 rsize = nmp->nm_rsize;
1416 len = (tsiz > rsize) ? rsize : tsiz;
1417 NFSCL_REQSTART(nd, NFSPROC_READ, vp);
1418 if (nd->nd_flag & ND_NFSV4)
1419 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID);
1420 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED * 3);
1421 if (nd->nd_flag & ND_NFSV2) {
1422 *tl++ = txdr_unsigned(uiop->uio_offset);
1423 *tl++ = txdr_unsigned(len);
1426 txdr_hyper(uiop->uio_offset, tl);
1427 *(tl + 2) = txdr_unsigned(len);
1430 * Since I can't do a Getattr for NFSv4 for Write, there
1431 * doesn't seem any point in doing one here, either.
1432 * (See the comment in nfsrpc_writerpc() for more info.)
1434 error = nfscl_request(nd, vp, p, cred, stuff);
1437 if (nd->nd_flag & ND_NFSV3) {
1438 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
1439 } else if (!nd->nd_repstat && (nd->nd_flag & ND_NFSV2)) {
1440 error = nfsm_loadattr(nd, nap);
1444 if (nd->nd_repstat || error) {
1446 error = nd->nd_repstat;
1449 if (nd->nd_flag & ND_NFSV3) {
1450 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1451 eof = fxdr_unsigned(int, *(tl + 1));
1452 } else if (nd->nd_flag & ND_NFSV4) {
1453 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
1454 eof = fxdr_unsigned(int, *tl);
1456 NFSM_STRSIZ(retlen, len);
1457 error = nfsm_mbufuio(nd, uiop, retlen);
1460 mbuf_freem(nd->nd_mrep);
1463 if (!(nd->nd_flag & ND_NFSV2)) {
1464 if (eof || retlen == 0)
1466 } else if (retlen < len)
1471 if (nd->nd_mrep != NULL)
1472 mbuf_freem(nd->nd_mrep);
1477 * nfs write operation
1478 * When called_from_strategy != 0, it should return EIO for an error that
1479 * indicates recovery is in progress, so that the buffer will be left
1480 * dirty and be written back to the server later. If it loops around,
1481 * the recovery thread could get stuck waiting for the buffer and recovery
1482 * will then deadlock.
1485 nfsrpc_write(vnode_t vp, struct uio *uiop, int *iomode, int *must_commit,
1486 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp,
1487 void *stuff, int called_from_strategy)
1489 int error, expireret = 0, retrycnt, nostateid;
1490 u_int32_t clidrev = 0;
1491 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
1492 struct nfsnode *np = VTONFS(vp);
1493 struct ucred *newcred;
1494 struct nfsfh *nfhp = NULL;
1495 nfsv4stateid_t stateid;
1499 if (nmp->nm_clp != NULL)
1500 clidrev = nmp->nm_clp->nfsc_clientidrev;
1502 if (NFSHASNFSV4(nmp)) {
1503 newcred = NFSNEWCRED(cred);
1510 if (NFSHASNFSV4(nmp)) {
1511 (void)nfscl_getstateid(vp, nfhp->nfh_fh, nfhp->nfh_len,
1512 NFSV4OPEN_ACCESSWRITE, 0, newcred, p, &stateid,
1514 if (stateid.other[0] == 0 && stateid.other[1] == 0 &&
1515 stateid.other[2] == 0) {
1517 NFSCL_DEBUG(1, "stateid0 in write\n");
1522 * If there is no stateid for NFSv4, it means this is an
1523 * extraneous write after close. Basically a poorly
1524 * implemented buffer cache. Just don't do the write.
1529 error = nfsrpc_writerpc(vp, uiop, iomode, must_commit,
1530 newcred, &stateid, p, nap, attrflagp, stuff);
1531 if (error == NFSERR_STALESTATEID || error == NFSERR_BADSESSION)
1532 nfscl_initiate_recovery(nmp->nm_clp);
1534 nfscl_lockderef(lckp);
1535 if (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
1536 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
1537 error == NFSERR_OLDSTATEID || error == NFSERR_BADSESSION) {
1538 (void) nfs_catnap(PZERO, error, "nfs_write");
1539 } else if ((error == NFSERR_EXPIRED ||
1540 error == NFSERR_BADSTATEID) && clidrev != 0) {
1541 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
1544 } while (error == NFSERR_GRACE || error == NFSERR_DELAY ||
1545 ((error == NFSERR_STALESTATEID || error == NFSERR_BADSESSION ||
1546 error == NFSERR_STALEDONTRECOVER) && called_from_strategy == 0) ||
1547 (error == NFSERR_OLDSTATEID && retrycnt < 20) ||
1548 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
1549 expireret == 0 && clidrev != 0 && retrycnt < 4));
1550 if (error != 0 && (retrycnt >= 4 ||
1551 ((error == NFSERR_STALESTATEID || error == NFSERR_BADSESSION ||
1552 error == NFSERR_STALEDONTRECOVER) && called_from_strategy != 0)))
1554 if (NFSHASNFSV4(nmp))
1555 NFSFREECRED(newcred);
1560 * The actual write RPC.
1563 nfsrpc_writerpc(vnode_t vp, struct uio *uiop, int *iomode,
1564 int *must_commit, struct ucred *cred, nfsv4stateid_t *stateidp,
1565 NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, void *stuff)
1568 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
1569 struct nfsnode *np = VTONFS(vp);
1570 int error = 0, len, tsiz, rlen, commit, committed = NFSWRITE_FILESYNC;
1571 int wccflag = 0, wsize;
1573 struct nfsrv_descript nfsd;
1574 struct nfsrv_descript *nd = &nfsd;
1575 nfsattrbit_t attrbits;
1578 KASSERT(uiop->uio_iovcnt == 1, ("nfs: writerpc iovcnt > 1"));
1580 tsiz = uio_uio_resid(uiop);
1581 tmp_off = uiop->uio_offset + tsiz;
1583 if (tmp_off > nmp->nm_maxfilesize || tmp_off < uiop->uio_offset) {
1587 wsize = nmp->nm_wsize;
1589 nd->nd_mrep = NULL; /* NFSv2 sometimes does a write with */
1590 nd->nd_repstat = 0; /* uio_resid == 0, so the while is not done */
1593 len = (tsiz > wsize) ? wsize : tsiz;
1594 NFSCL_REQSTART(nd, NFSPROC_WRITE, vp);
1595 if (nd->nd_flag & ND_NFSV4) {
1596 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID);
1597 NFSM_BUILD(tl, u_int32_t *, NFSX_HYPER+2*NFSX_UNSIGNED);
1598 txdr_hyper(uiop->uio_offset, tl);
1600 *tl++ = txdr_unsigned(*iomode);
1601 *tl = txdr_unsigned(len);
1602 } else if (nd->nd_flag & ND_NFSV3) {
1603 NFSM_BUILD(tl, u_int32_t *, NFSX_HYPER+3*NFSX_UNSIGNED);
1604 txdr_hyper(uiop->uio_offset, tl);
1606 *tl++ = txdr_unsigned(len);
1607 *tl++ = txdr_unsigned(*iomode);
1608 *tl = txdr_unsigned(len);
1612 NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1614 * Not sure why someone changed this, since the
1615 * RFC clearly states that "beginoffset" and
1616 * "totalcount" are ignored, but it wouldn't
1617 * surprise me if there's a busted server out there.
1619 /* Set both "begin" and "current" to non-garbage. */
1620 x = txdr_unsigned((u_int32_t)uiop->uio_offset);
1621 *tl++ = x; /* "begin offset" */
1622 *tl++ = x; /* "current offset" */
1623 x = txdr_unsigned(len);
1624 *tl++ = x; /* total to this offset */
1625 *tl = x; /* size of this write */
1628 nfsm_uiombuf(nd, uiop, len);
1630 * Although it is tempting to do a normal Getattr Op in the
1631 * NFSv4 compound, the result can be a nearly hung client
1632 * system if the Getattr asks for Owner and/or OwnerGroup.
1633 * It occurs when the client can't map either the Owner or
1634 * Owner_group name in the Getattr reply to a uid/gid. When
1635 * there is a cache miss, the kernel does an upcall to the
1636 * nfsuserd. Then, it can try and read the local /etc/passwd
1637 * or /etc/group file. It can then block in getnewbuf(),
1638 * waiting for dirty writes to be pushed to the NFS server.
1639 * The only reason this doesn't result in a complete
1640 * deadlock, is that the upcall times out and allows
1641 * the write to complete. However, progress is so slow
1642 * that it might just as well be deadlocked.
1643 * As such, we get the rest of the attributes, but not
1644 * Owner or Owner_group.
1645 * nb: nfscl_loadattrcache() needs to be told that these
1646 * partial attributes from a write rpc are being
1647 * passed in, via a argument flag.
1649 if (nd->nd_flag & ND_NFSV4) {
1650 NFSWRITEGETATTR_ATTRBIT(&attrbits);
1651 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1652 *tl = txdr_unsigned(NFSV4OP_GETATTR);
1653 (void) nfsrv_putattrbit(nd, &attrbits);
1655 error = nfscl_request(nd, vp, p, cred, stuff);
1658 if (nd->nd_repstat) {
1660 * In case the rpc gets retried, roll
1661 * the uio fileds changed by nfsm_uiombuf()
1664 uiop->uio_offset -= len;
1665 uio_uio_resid_add(uiop, len);
1666 uio_iov_base_add(uiop, -len);
1667 uio_iov_len_add(uiop, len);
1669 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) {
1670 error = nfscl_wcc_data(nd, vp, nap, attrflagp,
1675 if (!nd->nd_repstat) {
1676 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) {
1677 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED
1679 rlen = fxdr_unsigned(int, *tl++);
1683 } else if (rlen < len) {
1684 backup = len - rlen;
1685 uio_iov_base_add(uiop, -(backup));
1686 uio_iov_len_add(uiop, backup);
1687 uiop->uio_offset -= backup;
1688 uio_uio_resid_add(uiop, backup);
1691 commit = fxdr_unsigned(int, *tl++);
1694 * Return the lowest committment level
1695 * obtained by any of the RPCs.
1697 if (committed == NFSWRITE_FILESYNC)
1699 else if (committed == NFSWRITE_DATASYNC &&
1700 commit == NFSWRITE_UNSTABLE)
1703 if (!NFSHASWRITEVERF(nmp)) {
1704 NFSBCOPY((caddr_t)tl,
1705 (caddr_t)&nmp->nm_verf[0],
1707 NFSSETWRITEVERF(nmp);
1708 } else if (NFSBCMP(tl, nmp->nm_verf,
1711 NFSBCOPY(tl, nmp->nm_verf, NFSX_VERF);
1715 if (nd->nd_flag & ND_NFSV4)
1716 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1717 if (nd->nd_flag & (ND_NFSV2 | ND_NFSV4)) {
1718 error = nfsm_loadattr(nd, nap);
1720 *attrflagp = NFS_LATTR_NOSHRINK;
1723 error = nd->nd_repstat;
1727 NFSWRITERPC_SETTIME(wccflag, np, (nd->nd_flag & ND_NFSV4));
1728 mbuf_freem(nd->nd_mrep);
1733 if (nd->nd_mrep != NULL)
1734 mbuf_freem(nd->nd_mrep);
1735 *iomode = committed;
1736 if (nd->nd_repstat && !error)
1737 error = nd->nd_repstat;
1743 * For NFS v2 this is a kludge. Use a create rpc but with the IFMT bits of the
1744 * mode set to specify the file type and the size field for rdev.
1747 nfsrpc_mknod(vnode_t dvp, char *name, int namelen, struct vattr *vap,
1748 u_int32_t rdev, enum vtype vtyp, struct ucred *cred, NFSPROC_T *p,
1749 struct nfsvattr *dnap, struct nfsvattr *nnap, struct nfsfh **nfhpp,
1750 int *attrflagp, int *dattrflagp, void *dstuff)
1754 struct nfsrv_descript nfsd, *nd = &nfsd;
1755 nfsattrbit_t attrbits;
1760 if (namelen > NFS_MAXNAMLEN)
1761 return (ENAMETOOLONG);
1762 NFSCL_REQSTART(nd, NFSPROC_MKNOD, dvp);
1763 if (nd->nd_flag & ND_NFSV4) {
1764 if (vtyp == VBLK || vtyp == VCHR) {
1765 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
1766 *tl++ = vtonfsv34_type(vtyp);
1767 *tl++ = txdr_unsigned(NFSMAJOR(rdev));
1768 *tl = txdr_unsigned(NFSMINOR(rdev));
1770 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1771 *tl = vtonfsv34_type(vtyp);
1774 (void) nfsm_strtom(nd, name, namelen);
1775 if (nd->nd_flag & ND_NFSV3) {
1776 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1777 *tl = vtonfsv34_type(vtyp);
1779 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4))
1780 nfscl_fillsattr(nd, vap, dvp, 0, 0);
1781 if ((nd->nd_flag & ND_NFSV3) &&
1782 (vtyp == VCHR || vtyp == VBLK)) {
1783 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1784 *tl++ = txdr_unsigned(NFSMAJOR(rdev));
1785 *tl = txdr_unsigned(NFSMINOR(rdev));
1787 if (nd->nd_flag & ND_NFSV4) {
1788 NFSGETATTR_ATTRBIT(&attrbits);
1789 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1790 *tl++ = txdr_unsigned(NFSV4OP_GETFH);
1791 *tl = txdr_unsigned(NFSV4OP_GETATTR);
1792 (void) nfsrv_putattrbit(nd, &attrbits);
1794 if (nd->nd_flag & ND_NFSV2)
1795 nfscl_fillsattr(nd, vap, dvp, NFSSATTR_SIZERDEV, rdev);
1796 error = nfscl_request(nd, dvp, p, cred, dstuff);
1799 if (nd->nd_flag & ND_NFSV4)
1800 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
1801 if (!nd->nd_repstat) {
1802 if (nd->nd_flag & ND_NFSV4) {
1803 NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
1804 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
1808 error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp);
1812 if (nd->nd_flag & ND_NFSV3)
1813 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
1814 if (!error && nd->nd_repstat)
1815 error = nd->nd_repstat;
1817 mbuf_freem(nd->nd_mrep);
1822 * nfs file create call
1823 * Mostly just call the approriate routine. (I separated out v4, so that
1824 * error recovery wouldn't be as difficult.)
1827 nfsrpc_create(vnode_t dvp, char *name, int namelen, struct vattr *vap,
1828 nfsquad_t cverf, int fmode, struct ucred *cred, NFSPROC_T *p,
1829 struct nfsvattr *dnap, struct nfsvattr *nnap, struct nfsfh **nfhpp,
1830 int *attrflagp, int *dattrflagp, void *dstuff)
1832 int error = 0, newone, expireret = 0, retrycnt, unlocked;
1833 struct nfsclowner *owp;
1834 struct nfscldeleg *dp;
1835 struct nfsmount *nmp = VFSTONFS(vnode_mount(dvp));
1838 if (NFSHASNFSV4(nmp)) {
1842 error = nfscl_open(dvp, NULL, 0, (NFSV4OPEN_ACCESSWRITE |
1843 NFSV4OPEN_ACCESSREAD), 0, cred, p, &owp, NULL, &newone,
1847 if (nmp->nm_clp != NULL)
1848 clidrev = nmp->nm_clp->nfsc_clientidrev;
1851 error = nfsrpc_createv4(dvp, name, namelen, vap, cverf, fmode,
1852 owp, &dp, cred, p, dnap, nnap, nfhpp, attrflagp, dattrflagp,
1855 * There is no need to invalidate cached attributes here,
1856 * since new post-delegation issue attributes are always
1857 * returned by nfsrpc_createv4() and these will update the
1861 (void) nfscl_deleg(nmp->nm_mountp, owp->nfsow_clp,
1862 (*nfhpp)->nfh_fh, (*nfhpp)->nfh_len, cred, p, &dp);
1863 nfscl_ownerrelease(owp, error, newone, unlocked);
1864 if (error == NFSERR_GRACE || error == NFSERR_STALECLIENTID ||
1865 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
1866 error == NFSERR_BADSESSION) {
1867 (void) nfs_catnap(PZERO, error, "nfs_open");
1868 } else if ((error == NFSERR_EXPIRED ||
1869 error == NFSERR_BADSTATEID) && clidrev != 0) {
1870 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
1873 } while (error == NFSERR_GRACE || error == NFSERR_STALECLIENTID ||
1874 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
1875 error == NFSERR_BADSESSION ||
1876 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
1877 expireret == 0 && clidrev != 0 && retrycnt < 4));
1878 if (error && retrycnt >= 4)
1881 error = nfsrpc_createv23(dvp, name, namelen, vap, cverf,
1882 fmode, cred, p, dnap, nnap, nfhpp, attrflagp, dattrflagp,
1889 * The create rpc for v2 and 3.
1892 nfsrpc_createv23(vnode_t dvp, char *name, int namelen, struct vattr *vap,
1893 nfsquad_t cverf, int fmode, struct ucred *cred, NFSPROC_T *p,
1894 struct nfsvattr *dnap, struct nfsvattr *nnap, struct nfsfh **nfhpp,
1895 int *attrflagp, int *dattrflagp, void *dstuff)
1899 struct nfsrv_descript nfsd, *nd = &nfsd;
1904 if (namelen > NFS_MAXNAMLEN)
1905 return (ENAMETOOLONG);
1906 NFSCL_REQSTART(nd, NFSPROC_CREATE, dvp);
1907 (void) nfsm_strtom(nd, name, namelen);
1908 if (nd->nd_flag & ND_NFSV3) {
1909 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1910 if (fmode & O_EXCL) {
1911 *tl = txdr_unsigned(NFSCREATE_EXCLUSIVE);
1912 NFSM_BUILD(tl, u_int32_t *, NFSX_VERF);
1913 *tl++ = cverf.lval[0];
1914 *tl = cverf.lval[1];
1916 *tl = txdr_unsigned(NFSCREATE_UNCHECKED);
1917 nfscl_fillsattr(nd, vap, dvp, 0, 0);
1920 nfscl_fillsattr(nd, vap, dvp, NFSSATTR_SIZE0, 0);
1922 error = nfscl_request(nd, dvp, p, cred, dstuff);
1925 if (nd->nd_repstat == 0) {
1926 error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp);
1930 if (nd->nd_flag & ND_NFSV3)
1931 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
1932 if (nd->nd_repstat != 0 && error == 0)
1933 error = nd->nd_repstat;
1935 mbuf_freem(nd->nd_mrep);
1940 nfsrpc_createv4(vnode_t dvp, char *name, int namelen, struct vattr *vap,
1941 nfsquad_t cverf, int fmode, struct nfsclowner *owp, struct nfscldeleg **dpp,
1942 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap,
1943 struct nfsvattr *nnap, struct nfsfh **nfhpp, int *attrflagp,
1944 int *dattrflagp, void *dstuff, int *unlockedp)
1947 int error = 0, deleg, newone, ret, acesize, limitby;
1948 struct nfsrv_descript nfsd, *nd = &nfsd;
1949 struct nfsclopen *op;
1950 struct nfscldeleg *dp = NULL;
1953 nfsattrbit_t attrbits;
1954 nfsv4stateid_t stateid;
1956 struct nfsmount *nmp;
1958 nmp = VFSTONFS(dvp->v_mount);
1965 if (namelen > NFS_MAXNAMLEN)
1966 return (ENAMETOOLONG);
1967 NFSCL_REQSTART(nd, NFSPROC_CREATE, dvp);
1969 * For V4, this is actually an Open op.
1971 NFSM_BUILD(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
1972 *tl++ = txdr_unsigned(owp->nfsow_seqid);
1973 *tl++ = txdr_unsigned(NFSV4OPEN_ACCESSWRITE |
1974 NFSV4OPEN_ACCESSREAD);
1975 *tl++ = txdr_unsigned(NFSV4OPEN_DENYNONE);
1976 *tl++ = NFSMNT_MDSSESSION(nmp)->nfsess_clientid.lval[0];
1977 *tl = NFSMNT_MDSSESSION(nmp)->nfsess_clientid.lval[1];
1978 (void) nfsm_strtom(nd, owp->nfsow_owner, NFSV4CL_LOCKNAMELEN);
1979 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1980 *tl++ = txdr_unsigned(NFSV4OPEN_CREATE);
1981 if (fmode & O_EXCL) {
1982 if (NFSHASNFSV4N(nmp)) {
1983 if (NFSHASSESSPERSIST(nmp)) {
1984 /* Use GUARDED for persistent sessions. */
1985 *tl = txdr_unsigned(NFSCREATE_GUARDED);
1986 nfscl_fillsattr(nd, vap, dvp, 0, 0);
1988 /* Otherwise, use EXCLUSIVE4_1. */
1989 *tl = txdr_unsigned(NFSCREATE_EXCLUSIVE41);
1990 NFSM_BUILD(tl, u_int32_t *, NFSX_VERF);
1991 *tl++ = cverf.lval[0];
1992 *tl = cverf.lval[1];
1993 nfscl_fillsattr(nd, vap, dvp, 0, 0);
1997 *tl = txdr_unsigned(NFSCREATE_EXCLUSIVE);
1998 NFSM_BUILD(tl, u_int32_t *, NFSX_VERF);
1999 *tl++ = cverf.lval[0];
2000 *tl = cverf.lval[1];
2003 *tl = txdr_unsigned(NFSCREATE_UNCHECKED);
2004 nfscl_fillsattr(nd, vap, dvp, 0, 0);
2006 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2007 *tl = txdr_unsigned(NFSV4OPEN_CLAIMNULL);
2008 (void) nfsm_strtom(nd, name, namelen);
2009 /* Get the new file's handle and attributes. */
2010 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2011 *tl++ = txdr_unsigned(NFSV4OP_GETFH);
2012 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2013 NFSGETATTR_ATTRBIT(&attrbits);
2014 (void) nfsrv_putattrbit(nd, &attrbits);
2015 /* Get the directory's post-op attributes. */
2016 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2017 *tl = txdr_unsigned(NFSV4OP_PUTFH);
2018 (void) nfsm_fhtom(nd, np->n_fhp->nfh_fh, np->n_fhp->nfh_len, 0);
2019 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2020 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2021 (void) nfsrv_putattrbit(nd, &attrbits);
2022 error = nfscl_request(nd, dvp, p, cred, dstuff);
2025 NFSCL_INCRSEQID(owp->nfsow_seqid, nd);
2026 if (nd->nd_repstat == 0) {
2027 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
2029 stateid.seqid = *tl++;
2030 stateid.other[0] = *tl++;
2031 stateid.other[1] = *tl++;
2032 stateid.other[2] = *tl;
2033 rflags = fxdr_unsigned(u_int32_t, *(tl + 6));
2034 (void) nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
2035 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2036 deleg = fxdr_unsigned(int, *tl);
2037 if (deleg == NFSV4OPEN_DELEGATEREAD ||
2038 deleg == NFSV4OPEN_DELEGATEWRITE) {
2039 if (!(owp->nfsow_clp->nfsc_flags &
2040 NFSCLFLAGS_FIRSTDELEG))
2041 owp->nfsow_clp->nfsc_flags |=
2042 (NFSCLFLAGS_FIRSTDELEG | NFSCLFLAGS_GOTDELEG);
2043 MALLOC(dp, struct nfscldeleg *,
2044 sizeof (struct nfscldeleg) + NFSX_V4FHMAX,
2045 M_NFSCLDELEG, M_WAITOK);
2046 LIST_INIT(&dp->nfsdl_owner);
2047 LIST_INIT(&dp->nfsdl_lock);
2048 dp->nfsdl_clp = owp->nfsow_clp;
2049 newnfs_copyincred(cred, &dp->nfsdl_cred);
2050 nfscl_lockinit(&dp->nfsdl_rwlock);
2051 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
2053 dp->nfsdl_stateid.seqid = *tl++;
2054 dp->nfsdl_stateid.other[0] = *tl++;
2055 dp->nfsdl_stateid.other[1] = *tl++;
2056 dp->nfsdl_stateid.other[2] = *tl++;
2057 ret = fxdr_unsigned(int, *tl);
2058 if (deleg == NFSV4OPEN_DELEGATEWRITE) {
2059 dp->nfsdl_flags = NFSCLDL_WRITE;
2061 * Indicates how much the file can grow.
2063 NFSM_DISSECT(tl, u_int32_t *,
2065 limitby = fxdr_unsigned(int, *tl++);
2067 case NFSV4OPEN_LIMITSIZE:
2068 dp->nfsdl_sizelimit = fxdr_hyper(tl);
2070 case NFSV4OPEN_LIMITBLOCKS:
2071 dp->nfsdl_sizelimit =
2072 fxdr_unsigned(u_int64_t, *tl++);
2073 dp->nfsdl_sizelimit *=
2074 fxdr_unsigned(u_int64_t, *tl);
2077 error = NFSERR_BADXDR;
2081 dp->nfsdl_flags = NFSCLDL_READ;
2084 dp->nfsdl_flags |= NFSCLDL_RECALL;
2085 error = nfsrv_dissectace(nd, &dp->nfsdl_ace, &ret,
2089 } else if (deleg != NFSV4OPEN_DELEGATENONE) {
2090 error = NFSERR_BADXDR;
2093 error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp);
2096 /* Get rid of the PutFH and Getattr status values. */
2097 NFSM_DISSECT(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
2098 /* Load the directory attributes. */
2099 error = nfsm_loadattr(nd, dnap);
2103 if (dp != NULL && *attrflagp) {
2104 dp->nfsdl_change = nnap->na_filerev;
2105 dp->nfsdl_modtime = nnap->na_mtime;
2106 dp->nfsdl_flags |= NFSCLDL_MODTIMESET;
2109 * We can now complete the Open state.
2113 dp->nfsdl_fhlen = nfhp->nfh_len;
2114 NFSBCOPY(nfhp->nfh_fh, dp->nfsdl_fh, nfhp->nfh_len);
2117 * Get an Open structure that will be
2118 * attached to the OpenOwner, acquired already.
2120 error = nfscl_open(dvp, nfhp->nfh_fh, nfhp->nfh_len,
2121 (NFSV4OPEN_ACCESSWRITE | NFSV4OPEN_ACCESSREAD), 0,
2122 cred, p, NULL, &op, &newone, NULL, 0);
2125 op->nfso_stateid = stateid;
2126 newnfs_copyincred(cred, &op->nfso_cred);
2127 if ((rflags & NFSV4OPEN_RESULTCONFIRM)) {
2129 ret = nfsrpc_openconfirm(dvp, nfhp->nfh_fh,
2130 nfhp->nfh_len, op, cred, p);
2131 if (ret == NFSERR_DELAY)
2132 (void) nfs_catnap(PZERO, ret, "nfs_create");
2133 } while (ret == NFSERR_DELAY);
2138 * If the server is handing out delegations, but we didn't
2139 * get one because an OpenConfirm was required, try the
2140 * Open again, to get a delegation. This is a harmless no-op,
2141 * from a server's point of view.
2143 if ((rflags & NFSV4OPEN_RESULTCONFIRM) &&
2144 (owp->nfsow_clp->nfsc_flags & NFSCLFLAGS_GOTDELEG) &&
2145 !error && dp == NULL) {
2147 ret = nfsrpc_openrpc(VFSTONFS(vnode_mount(dvp)), dvp,
2148 np->n_fhp->nfh_fh, np->n_fhp->nfh_len,
2149 nfhp->nfh_fh, nfhp->nfh_len,
2150 (NFSV4OPEN_ACCESSWRITE | NFSV4OPEN_ACCESSREAD), op,
2151 name, namelen, &dp, 0, 0x0, cred, p, 0, 1);
2152 if (ret == NFSERR_DELAY)
2153 (void) nfs_catnap(PZERO, ret, "nfs_crt2");
2154 } while (ret == NFSERR_DELAY);
2157 FREE((caddr_t)dp, M_NFSCLDELEG);
2158 if (ret == NFSERR_STALECLIENTID ||
2159 ret == NFSERR_STALEDONTRECOVER ||
2160 ret == NFSERR_BADSESSION)
2164 nfscl_openrelease(op, error, newone);
2167 if (nd->nd_repstat != 0 && error == 0)
2168 error = nd->nd_repstat;
2169 if (error == NFSERR_STALECLIENTID || error == NFSERR_BADSESSION)
2170 nfscl_initiate_recovery(owp->nfsow_clp);
2174 else if (dp != NULL)
2175 FREE((caddr_t)dp, M_NFSCLDELEG);
2176 mbuf_freem(nd->nd_mrep);
2184 nfsrpc_remove(vnode_t dvp, char *name, int namelen, vnode_t vp,
2185 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap, int *dattrflagp,
2189 struct nfsrv_descript nfsd, *nd = &nfsd;
2191 struct nfsmount *nmp;
2192 nfsv4stateid_t dstateid;
2193 int error, ret = 0, i;
2196 if (namelen > NFS_MAXNAMLEN)
2197 return (ENAMETOOLONG);
2198 nmp = VFSTONFS(vnode_mount(dvp));
2200 if (NFSHASNFSV4(nmp) && ret == 0) {
2201 ret = nfscl_removedeleg(vp, p, &dstateid);
2203 NFSCL_REQSTART(nd, NFSPROC_RETDELEGREMOVE, vp);
2204 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID +
2206 if (NFSHASNFSV4N(nmp))
2209 *tl++ = dstateid.seqid;
2210 *tl++ = dstateid.other[0];
2211 *tl++ = dstateid.other[1];
2212 *tl++ = dstateid.other[2];
2213 *tl = txdr_unsigned(NFSV4OP_PUTFH);
2215 (void) nfsm_fhtom(nd, np->n_fhp->nfh_fh,
2216 np->n_fhp->nfh_len, 0);
2217 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2218 *tl = txdr_unsigned(NFSV4OP_REMOVE);
2224 NFSCL_REQSTART(nd, NFSPROC_REMOVE, dvp);
2225 (void) nfsm_strtom(nd, name, namelen);
2226 error = nfscl_request(nd, dvp, p, cred, dstuff);
2229 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) {
2230 /* For NFSv4, parse out any Delereturn replies. */
2231 if (ret > 0 && nd->nd_repstat != 0 &&
2232 (nd->nd_flag & ND_NOMOREDATA)) {
2234 * If the Delegreturn failed, try again without
2235 * it. The server will Recall, as required.
2237 mbuf_freem(nd->nd_mrep);
2240 for (i = 0; i < (ret * 2); i++) {
2241 if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) ==
2243 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2245 nd->nd_flag |= ND_NOMOREDATA;
2248 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
2250 if (nd->nd_repstat && !error)
2251 error = nd->nd_repstat;
2253 mbuf_freem(nd->nd_mrep);
2258 * Do an nfs rename rpc.
2261 nfsrpc_rename(vnode_t fdvp, vnode_t fvp, char *fnameptr, int fnamelen,
2262 vnode_t tdvp, vnode_t tvp, char *tnameptr, int tnamelen, struct ucred *cred,
2263 NFSPROC_T *p, struct nfsvattr *fnap, struct nfsvattr *tnap,
2264 int *fattrflagp, int *tattrflagp, void *fstuff, void *tstuff)
2267 struct nfsrv_descript nfsd, *nd = &nfsd;
2268 struct nfsmount *nmp;
2270 nfsattrbit_t attrbits;
2271 nfsv4stateid_t fdstateid, tdstateid;
2272 int error = 0, ret = 0, gottd = 0, gotfd = 0, i;
2276 nmp = VFSTONFS(vnode_mount(fdvp));
2277 if (fnamelen > NFS_MAXNAMLEN || tnamelen > NFS_MAXNAMLEN)
2278 return (ENAMETOOLONG);
2280 if (NFSHASNFSV4(nmp) && ret == 0) {
2281 ret = nfscl_renamedeleg(fvp, &fdstateid, &gotfd, tvp,
2282 &tdstateid, &gottd, p);
2283 if (gotfd && gottd) {
2284 NFSCL_REQSTART(nd, NFSPROC_RETDELEGRENAME2, fvp);
2286 NFSCL_REQSTART(nd, NFSPROC_RETDELEGRENAME1, fvp);
2288 NFSCL_REQSTART(nd, NFSPROC_RETDELEGRENAME1, tvp);
2291 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID);
2292 if (NFSHASNFSV4N(nmp))
2295 *tl++ = fdstateid.seqid;
2296 *tl++ = fdstateid.other[0];
2297 *tl++ = fdstateid.other[1];
2298 *tl = fdstateid.other[2];
2300 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2301 *tl = txdr_unsigned(NFSV4OP_PUTFH);
2303 (void) nfsm_fhtom(nd, np->n_fhp->nfh_fh,
2304 np->n_fhp->nfh_len, 0);
2305 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2306 *tl = txdr_unsigned(NFSV4OP_DELEGRETURN);
2310 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID);
2311 if (NFSHASNFSV4N(nmp))
2314 *tl++ = tdstateid.seqid;
2315 *tl++ = tdstateid.other[0];
2316 *tl++ = tdstateid.other[1];
2317 *tl = tdstateid.other[2];
2320 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2321 *tl = txdr_unsigned(NFSV4OP_PUTFH);
2323 (void) nfsm_fhtom(nd, np->n_fhp->nfh_fh,
2324 np->n_fhp->nfh_len, 0);
2325 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2326 *tl = txdr_unsigned(NFSV4OP_SAVEFH);
2332 NFSCL_REQSTART(nd, NFSPROC_RENAME, fdvp);
2333 if (nd->nd_flag & ND_NFSV4) {
2334 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2335 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2336 NFSWCCATTR_ATTRBIT(&attrbits);
2337 (void) nfsrv_putattrbit(nd, &attrbits);
2338 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2339 *tl = txdr_unsigned(NFSV4OP_PUTFH);
2340 (void) nfsm_fhtom(nd, VTONFS(tdvp)->n_fhp->nfh_fh,
2341 VTONFS(tdvp)->n_fhp->nfh_len, 0);
2342 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2343 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2344 (void) nfsrv_putattrbit(nd, &attrbits);
2345 nd->nd_flag |= ND_V4WCCATTR;
2346 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2347 *tl = txdr_unsigned(NFSV4OP_RENAME);
2349 (void) nfsm_strtom(nd, fnameptr, fnamelen);
2350 if (!(nd->nd_flag & ND_NFSV4))
2351 (void) nfsm_fhtom(nd, VTONFS(tdvp)->n_fhp->nfh_fh,
2352 VTONFS(tdvp)->n_fhp->nfh_len, 0);
2353 (void) nfsm_strtom(nd, tnameptr, tnamelen);
2354 error = nfscl_request(nd, fdvp, p, cred, fstuff);
2357 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) {
2358 /* For NFSv4, parse out any Delereturn replies. */
2359 if (ret > 0 && nd->nd_repstat != 0 &&
2360 (nd->nd_flag & ND_NOMOREDATA)) {
2362 * If the Delegreturn failed, try again without
2363 * it. The server will Recall, as required.
2365 mbuf_freem(nd->nd_mrep);
2368 for (i = 0; i < (ret * 2); i++) {
2369 if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) ==
2371 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2373 if (i == 0 && ret > 1) {
2375 * If the Delegreturn failed, try again
2376 * without it. The server will Recall, as
2378 * If ret > 1, the first iteration of this
2379 * loop is the second DelegReturn result.
2381 mbuf_freem(nd->nd_mrep);
2384 nd->nd_flag |= ND_NOMOREDATA;
2389 /* Now, the first wcc attribute reply. */
2390 if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) == ND_NFSV4) {
2391 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2393 nd->nd_flag |= ND_NOMOREDATA;
2395 error = nfscl_wcc_data(nd, fdvp, fnap, fattrflagp, NULL,
2397 /* and the second wcc attribute reply. */
2398 if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) == ND_NFSV4 &&
2400 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2402 nd->nd_flag |= ND_NOMOREDATA;
2405 error = nfscl_wcc_data(nd, tdvp, tnap, tattrflagp,
2408 if (nd->nd_repstat && !error)
2409 error = nd->nd_repstat;
2411 mbuf_freem(nd->nd_mrep);
2416 * nfs hard link create rpc
2419 nfsrpc_link(vnode_t dvp, vnode_t vp, char *name, int namelen,
2420 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap,
2421 struct nfsvattr *nap, int *attrflagp, int *dattrflagp, void *dstuff)
2424 struct nfsrv_descript nfsd, *nd = &nfsd;
2425 nfsattrbit_t attrbits;
2430 if (namelen > NFS_MAXNAMLEN)
2431 return (ENAMETOOLONG);
2432 NFSCL_REQSTART(nd, NFSPROC_LINK, vp);
2433 if (nd->nd_flag & ND_NFSV4) {
2434 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2435 *tl = txdr_unsigned(NFSV4OP_PUTFH);
2437 (void) nfsm_fhtom(nd, VTONFS(dvp)->n_fhp->nfh_fh,
2438 VTONFS(dvp)->n_fhp->nfh_len, 0);
2439 if (nd->nd_flag & ND_NFSV4) {
2440 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2441 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2442 NFSWCCATTR_ATTRBIT(&attrbits);
2443 (void) nfsrv_putattrbit(nd, &attrbits);
2444 nd->nd_flag |= ND_V4WCCATTR;
2445 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2446 *tl = txdr_unsigned(NFSV4OP_LINK);
2448 (void) nfsm_strtom(nd, name, namelen);
2449 error = nfscl_request(nd, vp, p, cred, dstuff);
2452 if (nd->nd_flag & ND_NFSV3) {
2453 error = nfscl_postop_attr(nd, nap, attrflagp, dstuff);
2455 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp,
2457 } else if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) == ND_NFSV4) {
2459 * First, parse out the PutFH and Getattr result.
2461 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2463 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2465 nd->nd_flag |= ND_NOMOREDATA;
2467 * Get the pre-op attributes.
2469 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
2471 if (nd->nd_repstat && !error)
2472 error = nd->nd_repstat;
2474 mbuf_freem(nd->nd_mrep);
2479 * nfs symbolic link create rpc
2482 nfsrpc_symlink(vnode_t dvp, char *name, int namelen, char *target,
2483 struct vattr *vap, struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap,
2484 struct nfsvattr *nnap, struct nfsfh **nfhpp, int *attrflagp,
2485 int *dattrflagp, void *dstuff)
2488 struct nfsrv_descript nfsd, *nd = &nfsd;
2489 struct nfsmount *nmp;
2490 int slen, error = 0;
2495 nmp = VFSTONFS(vnode_mount(dvp));
2496 slen = strlen(target);
2497 if (slen > NFS_MAXPATHLEN || namelen > NFS_MAXNAMLEN)
2498 return (ENAMETOOLONG);
2499 NFSCL_REQSTART(nd, NFSPROC_SYMLINK, dvp);
2500 if (nd->nd_flag & ND_NFSV4) {
2501 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2502 *tl = txdr_unsigned(NFLNK);
2503 (void) nfsm_strtom(nd, target, slen);
2505 (void) nfsm_strtom(nd, name, namelen);
2506 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4))
2507 nfscl_fillsattr(nd, vap, dvp, 0, 0);
2508 if (!(nd->nd_flag & ND_NFSV4))
2509 (void) nfsm_strtom(nd, target, slen);
2510 if (nd->nd_flag & ND_NFSV2)
2511 nfscl_fillsattr(nd, vap, dvp, NFSSATTR_SIZENEG1, 0);
2512 error = nfscl_request(nd, dvp, p, cred, dstuff);
2515 if (nd->nd_flag & ND_NFSV4)
2516 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
2517 if ((nd->nd_flag & ND_NFSV3) && !error) {
2518 if (!nd->nd_repstat)
2519 error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp);
2521 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp,
2524 if (nd->nd_repstat && !error)
2525 error = nd->nd_repstat;
2526 mbuf_freem(nd->nd_mrep);
2528 * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry.
2530 if (error == EEXIST)
2539 nfsrpc_mkdir(vnode_t dvp, char *name, int namelen, struct vattr *vap,
2540 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap,
2541 struct nfsvattr *nnap, struct nfsfh **nfhpp, int *attrflagp,
2542 int *dattrflagp, void *dstuff)
2545 struct nfsrv_descript nfsd, *nd = &nfsd;
2546 nfsattrbit_t attrbits;
2553 fhp = VTONFS(dvp)->n_fhp;
2554 if (namelen > NFS_MAXNAMLEN)
2555 return (ENAMETOOLONG);
2556 NFSCL_REQSTART(nd, NFSPROC_MKDIR, dvp);
2557 if (nd->nd_flag & ND_NFSV4) {
2558 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2559 *tl = txdr_unsigned(NFDIR);
2561 (void) nfsm_strtom(nd, name, namelen);
2562 nfscl_fillsattr(nd, vap, dvp, NFSSATTR_SIZENEG1, 0);
2563 if (nd->nd_flag & ND_NFSV4) {
2564 NFSGETATTR_ATTRBIT(&attrbits);
2565 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2566 *tl++ = txdr_unsigned(NFSV4OP_GETFH);
2567 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2568 (void) nfsrv_putattrbit(nd, &attrbits);
2569 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2570 *tl = txdr_unsigned(NFSV4OP_PUTFH);
2571 (void) nfsm_fhtom(nd, fhp->nfh_fh, fhp->nfh_len, 0);
2572 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2573 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2574 (void) nfsrv_putattrbit(nd, &attrbits);
2576 error = nfscl_request(nd, dvp, p, cred, dstuff);
2579 if (nd->nd_flag & ND_NFSV4)
2580 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
2581 if (!nd->nd_repstat && !error) {
2582 if (nd->nd_flag & ND_NFSV4) {
2583 NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
2584 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
2587 error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp);
2588 if (error == 0 && (nd->nd_flag & ND_NFSV4) != 0) {
2589 /* Get rid of the PutFH and Getattr status values. */
2590 NFSM_DISSECT(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
2591 /* Load the directory attributes. */
2592 error = nfsm_loadattr(nd, dnap);
2597 if ((nd->nd_flag & ND_NFSV3) && !error)
2598 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
2599 if (nd->nd_repstat && !error)
2600 error = nd->nd_repstat;
2602 mbuf_freem(nd->nd_mrep);
2604 * Kludge: Map EEXIST => 0 assuming that you have a reply to a retry.
2606 if (error == EEXIST)
2612 * nfs remove directory call
2615 nfsrpc_rmdir(vnode_t dvp, char *name, int namelen, struct ucred *cred,
2616 NFSPROC_T *p, struct nfsvattr *dnap, int *dattrflagp, void *dstuff)
2618 struct nfsrv_descript nfsd, *nd = &nfsd;
2622 if (namelen > NFS_MAXNAMLEN)
2623 return (ENAMETOOLONG);
2624 NFSCL_REQSTART(nd, NFSPROC_RMDIR, dvp);
2625 (void) nfsm_strtom(nd, name, namelen);
2626 error = nfscl_request(nd, dvp, p, cred, dstuff);
2629 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4))
2630 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
2631 if (nd->nd_repstat && !error)
2632 error = nd->nd_repstat;
2633 mbuf_freem(nd->nd_mrep);
2635 * Kludge: Map ENOENT => 0 assuming that you have a reply to a retry.
2637 if (error == ENOENT)
2644 * Always returns with either uio_resid unchanged, if you are at the
2645 * end of the directory, or uio_resid == 0, with all DIRBLKSIZ chunks
2647 * I felt this would allow caching of directory blocks more easily
2648 * than returning a pertially filled block.
2649 * Directory offset cookies:
2650 * Oh my, what to do with them...
2651 * I can think of three ways to deal with them:
2652 * 1 - have the layer above these RPCs maintain a map between logical
2653 * directory byte offsets and the NFS directory offset cookies
2654 * 2 - pass the opaque directory offset cookies up into userland
2655 * and let the libc functions deal with them, via the system call
2656 * 3 - return them to userland in the "struct dirent", so future versions
2657 * of libc can use them and do whatever is necessary to amke things work
2658 * above these rpc calls, in the meantime
2659 * For now, I do #3 by "hiding" the directory offset cookies after the
2660 * d_name field in struct dirent. This is space inside d_reclen that
2661 * will be ignored by anything that doesn't know about them.
2662 * The directory offset cookies are filled in as the last 8 bytes of
2663 * each directory entry, after d_name. Someday, the userland libc
2664 * functions may be able to use these. In the meantime, it satisfies
2665 * OpenBSD's requirements for cookies being returned.
2666 * If expects the directory offset cookie for the read to be in uio_offset
2667 * and returns the one for the next entry after this directory block in
2671 nfsrpc_readdir(vnode_t vp, struct uio *uiop, nfsuint64 *cookiep,
2672 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp,
2673 int *eofp, void *stuff)
2676 struct dirent *dp = NULL;
2678 nfsquad_t cookie, ncookie;
2679 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
2680 struct nfsnode *dnp = VTONFS(vp);
2681 struct nfsvattr nfsva;
2682 struct nfsrv_descript nfsd, *nd = &nfsd;
2683 int error = 0, tlen, more_dirs = 1, blksiz = 0, bigenough = 1;
2684 int reqsize, tryformoredirs = 1, readsize, eof = 0, gotmnton = 0;
2685 long dotfileid, dotdotfileid = 0;
2686 u_int32_t fakefileno = 0xffffffff, rderr;
2688 nfsattrbit_t attrbits, dattrbits;
2689 u_int32_t *tl2 = NULL;
2692 KASSERT(uiop->uio_iovcnt == 1 &&
2693 (uio_uio_resid(uiop) & (DIRBLKSIZ - 1)) == 0,
2694 ("nfs readdirrpc bad uio"));
2697 * There is no point in reading a lot more than uio_resid, however
2698 * adding one additional DIRBLKSIZ makes sense. Since uio_resid
2699 * and nm_readdirsize are both exact multiples of DIRBLKSIZ, this
2700 * will never make readsize > nm_readdirsize.
2702 readsize = nmp->nm_readdirsize;
2703 if (readsize > uio_uio_resid(uiop))
2704 readsize = uio_uio_resid(uiop) + DIRBLKSIZ;
2709 tresid = uio_uio_resid(uiop);
2710 cookie.lval[0] = cookiep->nfsuquad[0];
2711 cookie.lval[1] = cookiep->nfsuquad[1];
2715 * For NFSv4, first create the "." and ".." entries.
2717 if (NFSHASNFSV4(nmp)) {
2718 reqsize = 6 * NFSX_UNSIGNED;
2719 NFSGETATTR_ATTRBIT(&dattrbits);
2720 NFSZERO_ATTRBIT(&attrbits);
2721 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_FILEID);
2722 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TYPE);
2723 if (NFSISSET_ATTRBIT(&dnp->n_vattr.na_suppattr,
2724 NFSATTRBIT_MOUNTEDONFILEID)) {
2725 NFSSETBIT_ATTRBIT(&attrbits,
2726 NFSATTRBIT_MOUNTEDONFILEID);
2730 * Must fake it. Use the fileno, except when the
2731 * fsid is != to that of the directory. For that
2732 * case, generate a fake fileno that is not the same.
2734 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_FSID);
2739 * Joy, oh joy. For V4 we get to hand craft '.' and '..'.
2741 if (uiop->uio_offset == 0) {
2742 NFSCL_REQSTART(nd, NFSPROC_LOOKUPP, vp);
2743 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2744 *tl++ = txdr_unsigned(NFSV4OP_GETFH);
2745 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2746 (void) nfsrv_putattrbit(nd, &attrbits);
2747 error = nfscl_request(nd, vp, p, cred, stuff);
2750 dotfileid = 0; /* Fake out the compiler. */
2751 if ((nd->nd_flag & ND_NOMOREDATA) == 0) {
2752 error = nfsm_loadattr(nd, &nfsva);
2755 dotfileid = nfsva.na_fileid;
2757 if (nd->nd_repstat == 0) {
2758 NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
2759 len = fxdr_unsigned(int, *(tl + 4));
2760 if (len > 0 && len <= NFSX_V4FHMAX)
2761 error = nfsm_advance(nd, NFSM_RNDUP(len), -1);
2765 NFSM_DISSECT(tl, u_int32_t *, 2*NFSX_UNSIGNED);
2766 nfsva.na_mntonfileno = 0xffffffff;
2767 error = nfsv4_loadattr(nd, NULL, &nfsva, NULL,
2768 NULL, 0, NULL, NULL, NULL, NULL, NULL, 0,
2769 NULL, NULL, NULL, p, cred);
2771 dotdotfileid = dotfileid;
2772 } else if (gotmnton) {
2773 if (nfsva.na_mntonfileno != 0xffffffff)
2774 dotdotfileid = nfsva.na_mntonfileno;
2776 dotdotfileid = nfsva.na_fileid;
2777 } else if (nfsva.na_filesid[0] ==
2778 dnp->n_vattr.na_filesid[0] &&
2779 nfsva.na_filesid[1] ==
2780 dnp->n_vattr.na_filesid[1]) {
2781 dotdotfileid = nfsva.na_fileid;
2785 } while (fakefileno ==
2787 dotdotfileid = fakefileno;
2790 } else if (nd->nd_repstat == NFSERR_NOENT) {
2792 * Lookupp returns NFSERR_NOENT when we are
2793 * at the root, so just use the current dir.
2796 dotdotfileid = dotfileid;
2798 error = nd->nd_repstat;
2800 mbuf_freem(nd->nd_mrep);
2804 dp = (struct dirent *) CAST_DOWN(caddr_t, uio_iov_base(uiop));
2805 dp->d_type = DT_DIR;
2806 dp->d_fileno = dotfileid;
2808 dp->d_name[0] = '.';
2809 dp->d_name[1] = '\0';
2810 dp->d_reclen = DIRENT_SIZE(dp) + NFSX_HYPER;
2812 * Just make these offset cookie 0.
2814 tl = (u_int32_t *)&dp->d_name[4];
2817 blksiz += dp->d_reclen;
2818 uio_uio_resid_add(uiop, -(dp->d_reclen));
2819 uiop->uio_offset += dp->d_reclen;
2820 uio_iov_base_add(uiop, dp->d_reclen);
2821 uio_iov_len_add(uiop, -(dp->d_reclen));
2822 dp = (struct dirent *) CAST_DOWN(caddr_t, uio_iov_base(uiop));
2823 dp->d_type = DT_DIR;
2824 dp->d_fileno = dotdotfileid;
2826 dp->d_name[0] = '.';
2827 dp->d_name[1] = '.';
2828 dp->d_name[2] = '\0';
2829 dp->d_reclen = DIRENT_SIZE(dp) + NFSX_HYPER;
2831 * Just make these offset cookie 0.
2833 tl = (u_int32_t *)&dp->d_name[4];
2836 blksiz += dp->d_reclen;
2837 uio_uio_resid_add(uiop, -(dp->d_reclen));
2838 uiop->uio_offset += dp->d_reclen;
2839 uio_iov_base_add(uiop, dp->d_reclen);
2840 uio_iov_len_add(uiop, -(dp->d_reclen));
2842 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_RDATTRERROR);
2844 reqsize = 5 * NFSX_UNSIGNED;
2849 * Loop around doing readdir rpc's of size readsize.
2850 * The stopping criteria is EOF or buffer full.
2852 while (more_dirs && bigenough) {
2854 NFSCL_REQSTART(nd, NFSPROC_READDIR, vp);
2855 if (nd->nd_flag & ND_NFSV2) {
2856 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2857 *tl++ = cookie.lval[1];
2858 *tl = txdr_unsigned(readsize);
2860 NFSM_BUILD(tl, u_int32_t *, reqsize);
2861 *tl++ = cookie.lval[0];
2862 *tl++ = cookie.lval[1];
2863 if (cookie.qval == 0) {
2868 *tl++ = dnp->n_cookieverf.nfsuquad[0];
2869 *tl++ = dnp->n_cookieverf.nfsuquad[1];
2872 if (nd->nd_flag & ND_NFSV4) {
2873 *tl++ = txdr_unsigned(readsize);
2874 *tl = txdr_unsigned(readsize);
2875 (void) nfsrv_putattrbit(nd, &attrbits);
2876 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2877 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2878 (void) nfsrv_putattrbit(nd, &dattrbits);
2880 *tl = txdr_unsigned(readsize);
2883 error = nfscl_request(nd, vp, p, cred, stuff);
2886 if (!(nd->nd_flag & ND_NFSV2)) {
2887 if (nd->nd_flag & ND_NFSV3)
2888 error = nfscl_postop_attr(nd, nap, attrflagp,
2890 if (!nd->nd_repstat && !error) {
2891 NFSM_DISSECT(tl, u_int32_t *, NFSX_HYPER);
2893 dnp->n_cookieverf.nfsuquad[0] = *tl++;
2894 dnp->n_cookieverf.nfsuquad[1] = *tl;
2898 if (nd->nd_repstat || error) {
2900 error = nd->nd_repstat;
2903 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2904 more_dirs = fxdr_unsigned(int, *tl);
2908 /* loop thru the dir entries, doctoring them to 4bsd form */
2909 while (more_dirs && bigenough) {
2910 if (nd->nd_flag & ND_NFSV4) {
2911 NFSM_DISSECT(tl, u_int32_t *, 3*NFSX_UNSIGNED);
2912 ncookie.lval[0] = *tl++;
2913 ncookie.lval[1] = *tl++;
2914 len = fxdr_unsigned(int, *tl);
2915 } else if (nd->nd_flag & ND_NFSV3) {
2916 NFSM_DISSECT(tl, u_int32_t *, 3*NFSX_UNSIGNED);
2917 nfsva.na_fileid = fxdr_hyper(tl);
2919 len = fxdr_unsigned(int, *tl);
2921 NFSM_DISSECT(tl, u_int32_t *, 2*NFSX_UNSIGNED);
2923 fxdr_unsigned(long, *tl++);
2924 len = fxdr_unsigned(int, *tl);
2926 if (len <= 0 || len > NFS_MAXNAMLEN) {
2930 tlen = NFSM_RNDUP(len);
2932 tlen += 4; /* To ensure null termination */
2933 left = DIRBLKSIZ - blksiz;
2934 if ((int)(tlen + DIRHDSIZ + NFSX_HYPER) > left) {
2935 dp->d_reclen += left;
2936 uio_iov_base_add(uiop, left);
2937 uio_iov_len_add(uiop, -(left));
2938 uio_uio_resid_add(uiop, -(left));
2939 uiop->uio_offset += left;
2942 if ((int)(tlen + DIRHDSIZ + NFSX_HYPER) > uio_uio_resid(uiop))
2945 dp = (struct dirent *) CAST_DOWN(caddr_t, uio_iov_base(uiop));
2947 dp->d_reclen = tlen + DIRHDSIZ + NFSX_HYPER;
2948 dp->d_type = DT_UNKNOWN;
2949 blksiz += dp->d_reclen;
2950 if (blksiz == DIRBLKSIZ)
2952 uio_uio_resid_add(uiop, -(DIRHDSIZ));
2953 uiop->uio_offset += DIRHDSIZ;
2954 uio_iov_base_add(uiop, DIRHDSIZ);
2955 uio_iov_len_add(uiop, -(DIRHDSIZ));
2956 error = nfsm_mbufuio(nd, uiop, len);
2959 cp = CAST_DOWN(caddr_t, uio_iov_base(uiop));
2961 *cp = '\0'; /* null terminate */
2962 cp += tlen; /* points to cookie storage */
2963 tl2 = (u_int32_t *)cp;
2964 uio_iov_base_add(uiop, (tlen + NFSX_HYPER));
2965 uio_iov_len_add(uiop, -(tlen + NFSX_HYPER));
2966 uio_uio_resid_add(uiop, -(tlen + NFSX_HYPER));
2967 uiop->uio_offset += (tlen + NFSX_HYPER);
2969 error = nfsm_advance(nd, NFSM_RNDUP(len), -1);
2973 if (nd->nd_flag & ND_NFSV4) {
2975 nfsva.na_mntonfileno = 0xffffffff;
2976 error = nfsv4_loadattr(nd, NULL, &nfsva, NULL,
2977 NULL, 0, NULL, NULL, NULL, NULL, NULL, 0,
2978 NULL, NULL, &rderr, p, cred);
2981 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2982 } else if (nd->nd_flag & ND_NFSV3) {
2983 NFSM_DISSECT(tl, u_int32_t *, 3*NFSX_UNSIGNED);
2984 ncookie.lval[0] = *tl++;
2985 ncookie.lval[1] = *tl++;
2987 NFSM_DISSECT(tl, u_int32_t *, 2*NFSX_UNSIGNED);
2988 ncookie.lval[0] = 0;
2989 ncookie.lval[1] = *tl++;
2992 if (nd->nd_flag & ND_NFSV4) {
2997 if (nfsva.na_mntonfileno != 0xffffffff)
2998 dp->d_fileno = nfsva.na_mntonfileno;
3000 dp->d_fileno = nfsva.na_fileid;
3001 } else if (nfsva.na_filesid[0] ==
3002 dnp->n_vattr.na_filesid[0] &&
3003 nfsva.na_filesid[1] ==
3004 dnp->n_vattr.na_filesid[1]) {
3005 dp->d_fileno = nfsva.na_fileid;
3009 } while (fakefileno ==
3011 dp->d_fileno = fakefileno;
3013 dp->d_type = vtonfs_dtype(nfsva.na_type);
3016 dp->d_fileno = nfsva.na_fileid;
3018 *tl2++ = cookiep->nfsuquad[0] = cookie.lval[0] =
3020 *tl2 = cookiep->nfsuquad[1] = cookie.lval[1] =
3023 more_dirs = fxdr_unsigned(int, *tl);
3026 * If at end of rpc data, get the eof boolean
3029 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3030 eof = fxdr_unsigned(int, *tl);
3033 if (nd->nd_flag & ND_NFSV4) {
3034 error = nfscl_postop_attr(nd, nap, attrflagp,
3040 mbuf_freem(nd->nd_mrep);
3044 * Fill last record, iff any, out to a multiple of DIRBLKSIZ
3045 * by increasing d_reclen for the last record.
3048 left = DIRBLKSIZ - blksiz;
3049 dp->d_reclen += left;
3050 uio_iov_base_add(uiop, left);
3051 uio_iov_len_add(uiop, -(left));
3052 uio_uio_resid_add(uiop, -(left));
3053 uiop->uio_offset += left;
3057 * If returning no data, assume end of file.
3058 * If not bigenough, return not end of file, since you aren't
3059 * returning all the data
3060 * Otherwise, return the eof flag from the server.
3063 if (tresid == ((size_t)(uio_uio_resid(uiop))))
3065 else if (!bigenough)
3072 * Add extra empty records to any remaining DIRBLKSIZ chunks.
3074 while (uio_uio_resid(uiop) > 0 && ((size_t)(uio_uio_resid(uiop))) != tresid) {
3075 dp = (struct dirent *) CAST_DOWN(caddr_t, uio_iov_base(uiop));
3076 dp->d_type = DT_UNKNOWN;
3079 dp->d_name[0] = '\0';
3080 tl = (u_int32_t *)&dp->d_name[4];
3081 *tl++ = cookie.lval[0];
3082 *tl = cookie.lval[1];
3083 dp->d_reclen = DIRBLKSIZ;
3084 uio_iov_base_add(uiop, DIRBLKSIZ);
3085 uio_iov_len_add(uiop, -(DIRBLKSIZ));
3086 uio_uio_resid_add(uiop, -(DIRBLKSIZ));
3087 uiop->uio_offset += DIRBLKSIZ;
3091 if (nd->nd_mrep != NULL)
3092 mbuf_freem(nd->nd_mrep);
3098 * NFS V3 readdir plus RPC. Used in place of nfsrpc_readdir().
3099 * (Also used for NFS V4 when mount flag set.)
3100 * (ditto above w.r.t. multiple of DIRBLKSIZ, etc.)
3103 nfsrpc_readdirplus(vnode_t vp, struct uio *uiop, nfsuint64 *cookiep,
3104 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp,
3105 int *eofp, void *stuff)
3108 struct dirent *dp = NULL;
3110 vnode_t newvp = NULLVP;
3111 struct nfsrv_descript nfsd, *nd = &nfsd;
3112 struct nameidata nami, *ndp = &nami;
3113 struct componentname *cnp = &ndp->ni_cnd;
3114 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
3115 struct nfsnode *dnp = VTONFS(vp), *np;
3116 struct nfsvattr nfsva;
3118 nfsquad_t cookie, ncookie;
3119 int error = 0, tlen, more_dirs = 1, blksiz = 0, bigenough = 1;
3120 int attrflag, tryformoredirs = 1, eof = 0, gotmnton = 0;
3121 int isdotdot = 0, unlocknewvp = 0;
3122 long dotfileid, dotdotfileid = 0, fileno = 0;
3124 nfsattrbit_t attrbits, dattrbits;
3126 u_int32_t *tl2 = NULL, fakefileno = 0xffffffff, rderr;
3127 struct timespec dctime;
3129 KASSERT(uiop->uio_iovcnt == 1 &&
3130 (uio_uio_resid(uiop) & (DIRBLKSIZ - 1)) == 0,
3131 ("nfs readdirplusrpc bad uio"));
3132 timespecclear(&dctime);
3138 cookie.lval[0] = cookiep->nfsuquad[0];
3139 cookie.lval[1] = cookiep->nfsuquad[1];
3140 tresid = uio_uio_resid(uiop);
3143 * For NFSv4, first create the "." and ".." entries.
3145 if (NFSHASNFSV4(nmp)) {
3146 NFSGETATTR_ATTRBIT(&dattrbits);
3147 NFSZERO_ATTRBIT(&attrbits);
3148 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_FILEID);
3149 if (NFSISSET_ATTRBIT(&dnp->n_vattr.na_suppattr,
3150 NFSATTRBIT_MOUNTEDONFILEID)) {
3151 NFSSETBIT_ATTRBIT(&attrbits,
3152 NFSATTRBIT_MOUNTEDONFILEID);
3156 * Must fake it. Use the fileno, except when the
3157 * fsid is != to that of the directory. For that
3158 * case, generate a fake fileno that is not the same.
3160 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_FSID);
3165 * Joy, oh joy. For V4 we get to hand craft '.' and '..'.
3167 if (uiop->uio_offset == 0) {
3168 NFSCL_REQSTART(nd, NFSPROC_LOOKUPP, vp);
3169 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
3170 *tl++ = txdr_unsigned(NFSV4OP_GETFH);
3171 *tl = txdr_unsigned(NFSV4OP_GETATTR);
3172 (void) nfsrv_putattrbit(nd, &attrbits);
3173 error = nfscl_request(nd, vp, p, cred, stuff);
3176 dotfileid = 0; /* Fake out the compiler. */
3177 if ((nd->nd_flag & ND_NOMOREDATA) == 0) {
3178 error = nfsm_loadattr(nd, &nfsva);
3181 dctime = nfsva.na_ctime;
3182 dotfileid = nfsva.na_fileid;
3184 if (nd->nd_repstat == 0) {
3185 NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
3186 len = fxdr_unsigned(int, *(tl + 4));
3187 if (len > 0 && len <= NFSX_V4FHMAX)
3188 error = nfsm_advance(nd, NFSM_RNDUP(len), -1);
3192 NFSM_DISSECT(tl, u_int32_t *, 2*NFSX_UNSIGNED);
3193 nfsva.na_mntonfileno = 0xffffffff;
3194 error = nfsv4_loadattr(nd, NULL, &nfsva, NULL,
3195 NULL, 0, NULL, NULL, NULL, NULL, NULL, 0,
3196 NULL, NULL, NULL, p, cred);
3198 dotdotfileid = dotfileid;
3199 } else if (gotmnton) {
3200 if (nfsva.na_mntonfileno != 0xffffffff)
3201 dotdotfileid = nfsva.na_mntonfileno;
3203 dotdotfileid = nfsva.na_fileid;
3204 } else if (nfsva.na_filesid[0] ==
3205 dnp->n_vattr.na_filesid[0] &&
3206 nfsva.na_filesid[1] ==
3207 dnp->n_vattr.na_filesid[1]) {
3208 dotdotfileid = nfsva.na_fileid;
3212 } while (fakefileno ==
3214 dotdotfileid = fakefileno;
3217 } else if (nd->nd_repstat == NFSERR_NOENT) {
3219 * Lookupp returns NFSERR_NOENT when we are
3220 * at the root, so just use the current dir.
3223 dotdotfileid = dotfileid;
3225 error = nd->nd_repstat;
3227 mbuf_freem(nd->nd_mrep);
3231 dp = (struct dirent *)uio_iov_base(uiop);
3232 dp->d_type = DT_DIR;
3233 dp->d_fileno = dotfileid;
3235 dp->d_name[0] = '.';
3236 dp->d_name[1] = '\0';
3237 dp->d_reclen = DIRENT_SIZE(dp) + NFSX_HYPER;
3239 * Just make these offset cookie 0.
3241 tl = (u_int32_t *)&dp->d_name[4];
3244 blksiz += dp->d_reclen;
3245 uio_uio_resid_add(uiop, -(dp->d_reclen));
3246 uiop->uio_offset += dp->d_reclen;
3247 uio_iov_base_add(uiop, dp->d_reclen);
3248 uio_iov_len_add(uiop, -(dp->d_reclen));
3249 dp = (struct dirent *)uio_iov_base(uiop);
3250 dp->d_type = DT_DIR;
3251 dp->d_fileno = dotdotfileid;
3253 dp->d_name[0] = '.';
3254 dp->d_name[1] = '.';
3255 dp->d_name[2] = '\0';
3256 dp->d_reclen = DIRENT_SIZE(dp) + NFSX_HYPER;
3258 * Just make these offset cookie 0.
3260 tl = (u_int32_t *)&dp->d_name[4];
3263 blksiz += dp->d_reclen;
3264 uio_uio_resid_add(uiop, -(dp->d_reclen));
3265 uiop->uio_offset += dp->d_reclen;
3266 uio_iov_base_add(uiop, dp->d_reclen);
3267 uio_iov_len_add(uiop, -(dp->d_reclen));
3269 NFSREADDIRPLUS_ATTRBIT(&attrbits);
3271 NFSSETBIT_ATTRBIT(&attrbits,
3272 NFSATTRBIT_MOUNTEDONFILEID);
3276 * Loop around doing readdir rpc's of size nm_readdirsize.
3277 * The stopping criteria is EOF or buffer full.
3279 while (more_dirs && bigenough) {
3281 NFSCL_REQSTART(nd, NFSPROC_READDIRPLUS, vp);
3282 NFSM_BUILD(tl, u_int32_t *, 6 * NFSX_UNSIGNED);
3283 *tl++ = cookie.lval[0];
3284 *tl++ = cookie.lval[1];
3285 if (cookie.qval == 0) {
3290 *tl++ = dnp->n_cookieverf.nfsuquad[0];
3291 *tl++ = dnp->n_cookieverf.nfsuquad[1];
3294 *tl++ = txdr_unsigned(nmp->nm_readdirsize);
3295 *tl = txdr_unsigned(nmp->nm_readdirsize);
3296 if (nd->nd_flag & ND_NFSV4) {
3297 (void) nfsrv_putattrbit(nd, &attrbits);
3298 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
3299 *tl = txdr_unsigned(NFSV4OP_GETATTR);
3300 (void) nfsrv_putattrbit(nd, &dattrbits);
3302 error = nfscl_request(nd, vp, p, cred, stuff);
3305 if (nd->nd_flag & ND_NFSV3)
3306 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
3307 if (nd->nd_repstat || error) {
3309 error = nd->nd_repstat;
3312 if ((nd->nd_flag & ND_NFSV3) != 0 && *attrflagp != 0)
3313 dctime = nap->na_ctime;
3314 NFSM_DISSECT(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
3316 dnp->n_cookieverf.nfsuquad[0] = *tl++;
3317 dnp->n_cookieverf.nfsuquad[1] = *tl++;
3319 more_dirs = fxdr_unsigned(int, *tl);
3323 /* loop thru the dir entries, doctoring them to 4bsd form */
3324 while (more_dirs && bigenough) {
3325 NFSM_DISSECT(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
3326 if (nd->nd_flag & ND_NFSV4) {
3327 ncookie.lval[0] = *tl++;
3328 ncookie.lval[1] = *tl++;
3330 fileno = fxdr_unsigned(long, *++tl);
3333 len = fxdr_unsigned(int, *tl);
3334 if (len <= 0 || len > NFS_MAXNAMLEN) {
3338 tlen = NFSM_RNDUP(len);
3340 tlen += 4; /* To ensure null termination */
3341 left = DIRBLKSIZ - blksiz;
3342 if ((tlen + DIRHDSIZ + NFSX_HYPER) > left) {
3343 dp->d_reclen += left;
3344 uio_iov_base_add(uiop, left);
3345 uio_iov_len_add(uiop, -(left));
3346 uio_uio_resid_add(uiop, -(left));
3347 uiop->uio_offset += left;
3350 if ((tlen + DIRHDSIZ + NFSX_HYPER) > uio_uio_resid(uiop))
3353 dp = (struct dirent *)uio_iov_base(uiop);
3355 dp->d_reclen = tlen + DIRHDSIZ + NFSX_HYPER;
3356 dp->d_type = DT_UNKNOWN;
3357 blksiz += dp->d_reclen;
3358 if (blksiz == DIRBLKSIZ)
3360 uio_uio_resid_add(uiop, -(DIRHDSIZ));
3361 uiop->uio_offset += DIRHDSIZ;
3362 uio_iov_base_add(uiop, DIRHDSIZ);
3363 uio_iov_len_add(uiop, -(DIRHDSIZ));
3364 cnp->cn_nameptr = uio_iov_base(uiop);
3365 cnp->cn_namelen = len;
3367 error = nfsm_mbufuio(nd, uiop, len);
3370 cp = uio_iov_base(uiop);
3373 cp += tlen; /* points to cookie storage */
3374 tl2 = (u_int32_t *)cp;
3375 if (len == 2 && cnp->cn_nameptr[0] == '.' &&
3376 cnp->cn_nameptr[1] == '.')
3380 uio_iov_base_add(uiop, (tlen + NFSX_HYPER));
3381 uio_iov_len_add(uiop, -(tlen + NFSX_HYPER));
3382 uio_uio_resid_add(uiop, -(tlen + NFSX_HYPER));
3383 uiop->uio_offset += (tlen + NFSX_HYPER);
3385 error = nfsm_advance(nd, NFSM_RNDUP(len), -1);
3390 if (nd->nd_flag & ND_NFSV3) {
3391 NFSM_DISSECT(tl, u_int32_t *, 3*NFSX_UNSIGNED);
3392 ncookie.lval[0] = *tl++;
3393 ncookie.lval[1] = *tl++;
3394 attrflag = fxdr_unsigned(int, *tl);
3396 error = nfsm_loadattr(nd, &nfsva);
3400 NFSM_DISSECT(tl,u_int32_t *,NFSX_UNSIGNED);
3402 error = nfsm_getfh(nd, &nfhp);
3406 if (!attrflag && nfhp != NULL) {
3407 FREE((caddr_t)nfhp, M_NFSFH);
3412 nfsva.na_mntonfileno = 0xffffffff;
3413 error = nfsv4_loadattr(nd, NULL, &nfsva, &nfhp,
3414 NULL, 0, NULL, NULL, NULL, NULL, NULL, 0,
3415 NULL, NULL, &rderr, p, cred);
3421 if (nd->nd_flag & ND_NFSV4) {
3424 } else if (gotmnton) {
3425 if (nfsva.na_mntonfileno != 0xffffffff)
3426 dp->d_fileno = nfsva.na_mntonfileno;
3428 dp->d_fileno = nfsva.na_fileid;
3429 } else if (nfsva.na_filesid[0] ==
3430 dnp->n_vattr.na_filesid[0] &&
3431 nfsva.na_filesid[1] ==
3432 dnp->n_vattr.na_filesid[1]) {
3433 dp->d_fileno = nfsva.na_fileid;
3437 } while (fakefileno ==
3439 dp->d_fileno = fakefileno;
3442 dp->d_fileno = fileno;
3444 *tl2++ = cookiep->nfsuquad[0] = cookie.lval[0] =
3446 *tl2 = cookiep->nfsuquad[1] = cookie.lval[1] =
3450 if (NFSRV_CMPFH(nfhp->nfh_fh, nfhp->nfh_len,
3451 dnp->n_fhp->nfh_fh, dnp->n_fhp->nfh_len)) {
3455 FREE((caddr_t)nfhp, M_NFSFH);
3457 } else if (isdotdot != 0) {
3459 * Skip doing a nfscl_nget() call for "..".
3460 * There's a race between acquiring the nfs
3461 * node here and lookups that look for the
3462 * directory being read (in the parent).
3463 * It would try to get a lock on ".." here,
3464 * owning the lock on the directory being
3465 * read. Lookup will hold the lock on ".."
3466 * and try to acquire the lock on the
3467 * directory being read.
3468 * If the directory is unlocked/relocked,
3469 * then there is a LOR with the buflock
3472 free(nfhp, M_NFSFH);
3474 error = nfscl_nget(vnode_mount(vp), vp,
3475 nfhp, cnp, p, &np, NULL, LK_EXCLUSIVE);
3482 if (newvp != NULLVP) {
3483 error = nfscl_loadattrcache(&newvp,
3484 &nfsva, NULL, NULL, 0, 0);
3493 vtonfs_dtype(np->n_vattr.na_type);
3495 NFSCNHASH(cnp, HASHINIT);
3496 if (cnp->cn_namelen <= NCHNAMLEN &&
3497 (newvp->v_type != VDIR ||
3498 dctime.tv_sec != 0)) {
3499 cache_enter_time(ndp->ni_dvp,
3502 newvp->v_type != VDIR ? NULL :
3512 } else if (nfhp != NULL) {
3513 FREE((caddr_t)nfhp, M_NFSFH);
3515 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3516 more_dirs = fxdr_unsigned(int, *tl);
3519 * If at end of rpc data, get the eof boolean
3522 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3523 eof = fxdr_unsigned(int, *tl);
3526 if (nd->nd_flag & ND_NFSV4) {
3527 error = nfscl_postop_attr(nd, nap, attrflagp,
3533 mbuf_freem(nd->nd_mrep);
3537 * Fill last record, iff any, out to a multiple of DIRBLKSIZ
3538 * by increasing d_reclen for the last record.
3541 left = DIRBLKSIZ - blksiz;
3542 dp->d_reclen += left;
3543 uio_iov_base_add(uiop, left);
3544 uio_iov_len_add(uiop, -(left));
3545 uio_uio_resid_add(uiop, -(left));
3546 uiop->uio_offset += left;
3550 * If returning no data, assume end of file.
3551 * If not bigenough, return not end of file, since you aren't
3552 * returning all the data
3553 * Otherwise, return the eof flag from the server.
3556 if (tresid == uio_uio_resid(uiop))
3558 else if (!bigenough)
3565 * Add extra empty records to any remaining DIRBLKSIZ chunks.
3567 while (uio_uio_resid(uiop) > 0 && uio_uio_resid(uiop) != tresid) {
3568 dp = (struct dirent *)uio_iov_base(uiop);
3569 dp->d_type = DT_UNKNOWN;
3572 dp->d_name[0] = '\0';
3573 tl = (u_int32_t *)&dp->d_name[4];
3574 *tl++ = cookie.lval[0];
3575 *tl = cookie.lval[1];
3576 dp->d_reclen = DIRBLKSIZ;
3577 uio_iov_base_add(uiop, DIRBLKSIZ);
3578 uio_iov_len_add(uiop, -(DIRBLKSIZ));
3579 uio_uio_resid_add(uiop, -(DIRBLKSIZ));
3580 uiop->uio_offset += DIRBLKSIZ;
3584 if (nd->nd_mrep != NULL)
3585 mbuf_freem(nd->nd_mrep);
3594 nfsrpc_commit(vnode_t vp, u_quad_t offset, int cnt, struct ucred *cred,
3595 NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, void *stuff)
3598 struct nfsrv_descript nfsd, *nd = &nfsd;
3599 nfsattrbit_t attrbits;
3601 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
3604 NFSCL_REQSTART(nd, NFSPROC_COMMIT, vp);
3605 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
3606 txdr_hyper(offset, tl);
3608 *tl = txdr_unsigned(cnt);
3609 if (nd->nd_flag & ND_NFSV4) {
3611 * And do a Getattr op.
3613 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
3614 *tl = txdr_unsigned(NFSV4OP_GETATTR);
3615 NFSGETATTR_ATTRBIT(&attrbits);
3616 (void) nfsrv_putattrbit(nd, &attrbits);
3618 error = nfscl_request(nd, vp, p, cred, stuff);
3621 error = nfscl_wcc_data(nd, vp, nap, attrflagp, NULL, stuff);
3622 if (!error && !nd->nd_repstat) {
3623 NFSM_DISSECT(tl, u_int32_t *, NFSX_VERF);
3625 if (NFSBCMP(nmp->nm_verf, tl, NFSX_VERF)) {
3626 NFSBCOPY(tl, nmp->nm_verf, NFSX_VERF);
3627 nd->nd_repstat = NFSERR_STALEWRITEVERF;
3630 if (nd->nd_flag & ND_NFSV4)
3631 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
3634 if (!error && nd->nd_repstat)
3635 error = nd->nd_repstat;
3636 mbuf_freem(nd->nd_mrep);
3641 * NFS byte range lock rpc.
3642 * (Mostly just calls one of the three lower level RPC routines.)
3645 nfsrpc_advlock(vnode_t vp, off_t size, int op, struct flock *fl,
3646 int reclaim, struct ucred *cred, NFSPROC_T *p, void *id, int flags)
3648 struct nfscllockowner *lp;
3649 struct nfsclclient *clp;
3651 struct nfsrv_descript nfsd, *nd = &nfsd;
3652 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
3655 u_int32_t clidrev = 0;
3656 int error = 0, newone = 0, expireret = 0, retrycnt, donelocally;
3660 * Convert the flock structure into a start and end and do POSIX
3663 switch (fl->l_whence) {
3667 * Caller is responsible for adding any necessary offset
3668 * when SEEK_CUR is used.
3670 start = fl->l_start;
3674 start = size + fl->l_start;
3675 off = size + fl->l_start;
3682 if (fl->l_len != 0) {
3683 end = start + fl->l_len - 1;
3694 if (op == F_GETLK) {
3695 error = nfscl_getcl(vnode_mount(vp), cred, p, 1, &clp);
3698 error = nfscl_lockt(vp, clp, off, len, fl, p, id, flags);
3700 clidrev = clp->nfsc_clientidrev;
3701 error = nfsrpc_lockt(nd, vp, clp, off, len, fl, cred,
3703 } else if (error == -1) {
3706 nfscl_clientrelease(clp);
3707 } else if (op == F_UNLCK && fl->l_type == F_UNLCK) {
3709 * We must loop around for all lockowner cases.
3712 error = nfscl_getcl(vnode_mount(vp), cred, p, 1, &clp);
3716 error = nfscl_relbytelock(vp, off, len, cred, p, callcnt,
3717 clp, id, flags, &lp, &dorpc);
3719 * If it returns a NULL lp, we're done.
3723 nfscl_clientrelease(clp);
3725 nfscl_releasealllocks(clp, vp, p, id, flags);
3728 if (nmp->nm_clp != NULL)
3729 clidrev = nmp->nm_clp->nfsc_clientidrev;
3733 * If the server doesn't support Posix lock semantics,
3734 * only allow locks on the entire file, since it won't
3735 * handle overlapping byte ranges.
3736 * There might still be a problem when a lock
3737 * upgrade/downgrade (read<->write) occurs, since the
3738 * server "might" expect an unlock first?
3740 if (dorpc && (lp->nfsl_open->nfso_posixlock ||
3741 (off == 0 && len == NFS64BITSSET))) {
3743 * Since the lock records will go away, we must
3744 * wait for grace and delay here.
3747 error = nfsrpc_locku(nd, nmp, lp, off, len,
3748 NFSV4LOCKT_READ, cred, p, 0);
3749 if ((nd->nd_repstat == NFSERR_GRACE ||
3750 nd->nd_repstat == NFSERR_DELAY) &&
3752 (void) nfs_catnap(PZERO, (int)nd->nd_repstat,
3754 } while ((nd->nd_repstat == NFSERR_GRACE ||
3755 nd->nd_repstat == NFSERR_DELAY) && error == 0);
3758 } while (error == 0 && nd->nd_repstat == 0);
3759 nfscl_releasealllocks(clp, vp, p, id, flags);
3760 } else if (op == F_SETLK) {
3761 error = nfscl_getbytelock(vp, off, len, fl->l_type, cred, p,
3762 NULL, 0, id, flags, NULL, NULL, &lp, &newone, &donelocally);
3763 if (error || donelocally) {
3766 if (nmp->nm_clp != NULL)
3767 clidrev = nmp->nm_clp->nfsc_clientidrev;
3770 nfhp = VTONFS(vp)->n_fhp;
3771 if (!lp->nfsl_open->nfso_posixlock &&
3772 (off != 0 || len != NFS64BITSSET)) {
3775 error = nfsrpc_lock(nd, nmp, vp, nfhp->nfh_fh,
3776 nfhp->nfh_len, lp, newone, reclaim, off,
3777 len, fl->l_type, cred, p, 0);
3780 error = nd->nd_repstat;
3781 nfscl_lockrelease(lp, error, newone);
3786 error = nd->nd_repstat;
3787 if (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
3788 error == NFSERR_STALEDONTRECOVER ||
3789 error == NFSERR_STALECLIENTID || error == NFSERR_DELAY ||
3790 error == NFSERR_BADSESSION) {
3791 (void) nfs_catnap(PZERO, error, "nfs_advlock");
3792 } else if ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID)
3794 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
3797 } while (error == NFSERR_GRACE ||
3798 error == NFSERR_STALECLIENTID || error == NFSERR_DELAY ||
3799 error == NFSERR_STALEDONTRECOVER || error == NFSERR_STALESTATEID ||
3800 error == NFSERR_BADSESSION ||
3801 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
3802 expireret == 0 && clidrev != 0 && retrycnt < 4));
3803 if (error && retrycnt >= 4)
3809 * The lower level routine for the LockT case.
3812 nfsrpc_lockt(struct nfsrv_descript *nd, vnode_t vp,
3813 struct nfsclclient *clp, u_int64_t off, u_int64_t len, struct flock *fl,
3814 struct ucred *cred, NFSPROC_T *p, void *id, int flags)
3817 int error, type, size;
3818 uint8_t own[NFSV4CL_LOCKNAMELEN + NFSX_V4FHMAX];
3820 struct nfsmount *nmp;
3822 nmp = VFSTONFS(vp->v_mount);
3823 NFSCL_REQSTART(nd, NFSPROC_LOCKT, vp);
3824 NFSM_BUILD(tl, u_int32_t *, 7 * NFSX_UNSIGNED);
3825 if (fl->l_type == F_RDLCK)
3826 *tl++ = txdr_unsigned(NFSV4LOCKT_READ);
3828 *tl++ = txdr_unsigned(NFSV4LOCKT_WRITE);
3829 txdr_hyper(off, tl);
3831 txdr_hyper(len, tl);
3833 *tl++ = NFSMNT_MDSSESSION(nmp)->nfsess_clientid.lval[0];
3834 *tl = NFSMNT_MDSSESSION(nmp)->nfsess_clientid.lval[1];
3835 nfscl_filllockowner(id, own, flags);
3837 NFSBCOPY(np->n_fhp->nfh_fh, &own[NFSV4CL_LOCKNAMELEN],
3838 np->n_fhp->nfh_len);
3839 (void)nfsm_strtom(nd, own, NFSV4CL_LOCKNAMELEN + np->n_fhp->nfh_len);
3840 error = nfscl_request(nd, vp, p, cred, NULL);
3843 if (nd->nd_repstat == 0) {
3844 fl->l_type = F_UNLCK;
3845 } else if (nd->nd_repstat == NFSERR_DENIED) {
3847 fl->l_whence = SEEK_SET;
3848 NFSM_DISSECT(tl, u_int32_t *, 8 * NFSX_UNSIGNED);
3849 fl->l_start = fxdr_hyper(tl);
3851 len = fxdr_hyper(tl);
3853 if (len == NFS64BITSSET)
3857 type = fxdr_unsigned(int, *tl++);
3858 if (type == NFSV4LOCKT_WRITE)
3859 fl->l_type = F_WRLCK;
3861 fl->l_type = F_RDLCK;
3863 * XXX For now, I have no idea what to do with the
3864 * conflicting lock_owner, so I'll just set the pid == 0
3865 * and skip over the lock_owner.
3867 fl->l_pid = (pid_t)0;
3869 size = fxdr_unsigned(int, *tl);
3870 if (size < 0 || size > NFSV4_OPAQUELIMIT)
3873 error = nfsm_advance(nd, NFSM_RNDUP(size), -1);
3874 } else if (nd->nd_repstat == NFSERR_STALECLIENTID ||
3875 nd->nd_repstat == NFSERR_BADSESSION)
3876 nfscl_initiate_recovery(clp);
3878 mbuf_freem(nd->nd_mrep);
3883 * Lower level function that performs the LockU RPC.
3886 nfsrpc_locku(struct nfsrv_descript *nd, struct nfsmount *nmp,
3887 struct nfscllockowner *lp, u_int64_t off, u_int64_t len,
3888 u_int32_t type, struct ucred *cred, NFSPROC_T *p, int syscred)
3893 nfscl_reqstart(nd, NFSPROC_LOCKU, nmp, lp->nfsl_open->nfso_fh,
3894 lp->nfsl_open->nfso_fhlen, NULL, NULL);
3895 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID + 6 * NFSX_UNSIGNED);
3896 *tl++ = txdr_unsigned(type);
3897 *tl = txdr_unsigned(lp->nfsl_seqid);
3898 if (nfstest_outofseq &&
3899 (arc4random() % nfstest_outofseq) == 0)
3900 *tl = txdr_unsigned(lp->nfsl_seqid + 1);
3902 if (NFSHASNFSV4N(nmp))
3905 *tl++ = lp->nfsl_stateid.seqid;
3906 *tl++ = lp->nfsl_stateid.other[0];
3907 *tl++ = lp->nfsl_stateid.other[1];
3908 *tl++ = lp->nfsl_stateid.other[2];
3909 txdr_hyper(off, tl);
3911 txdr_hyper(len, tl);
3913 nd->nd_flag |= ND_USEGSSNAME;
3914 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
3915 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
3916 NFSCL_INCRSEQID(lp->nfsl_seqid, nd);
3919 if (nd->nd_repstat == 0) {
3920 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID);
3921 lp->nfsl_stateid.seqid = *tl++;
3922 lp->nfsl_stateid.other[0] = *tl++;
3923 lp->nfsl_stateid.other[1] = *tl++;
3924 lp->nfsl_stateid.other[2] = *tl;
3925 } else if (nd->nd_repstat == NFSERR_STALESTATEID ||
3926 nd->nd_repstat == NFSERR_BADSESSION)
3927 nfscl_initiate_recovery(lp->nfsl_open->nfso_own->nfsow_clp);
3929 mbuf_freem(nd->nd_mrep);
3934 * The actual Lock RPC.
3937 nfsrpc_lock(struct nfsrv_descript *nd, struct nfsmount *nmp, vnode_t vp,
3938 u_int8_t *nfhp, int fhlen, struct nfscllockowner *lp, int newone,
3939 int reclaim, u_int64_t off, u_int64_t len, short type, struct ucred *cred,
3940 NFSPROC_T *p, int syscred)
3944 uint8_t own[NFSV4CL_LOCKNAMELEN + NFSX_V4FHMAX];
3946 nfscl_reqstart(nd, NFSPROC_LOCK, nmp, nfhp, fhlen, NULL, NULL);
3947 NFSM_BUILD(tl, u_int32_t *, 7 * NFSX_UNSIGNED);
3948 if (type == F_RDLCK)
3949 *tl++ = txdr_unsigned(NFSV4LOCKT_READ);
3951 *tl++ = txdr_unsigned(NFSV4LOCKT_WRITE);
3952 *tl++ = txdr_unsigned(reclaim);
3953 txdr_hyper(off, tl);
3955 txdr_hyper(len, tl);
3959 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID +
3960 2 * NFSX_UNSIGNED + NFSX_HYPER);
3961 *tl++ = txdr_unsigned(lp->nfsl_open->nfso_own->nfsow_seqid);
3962 if (NFSHASNFSV4N(nmp))
3965 *tl++ = lp->nfsl_open->nfso_stateid.seqid;
3966 *tl++ = lp->nfsl_open->nfso_stateid.other[0];
3967 *tl++ = lp->nfsl_open->nfso_stateid.other[1];
3968 *tl++ = lp->nfsl_open->nfso_stateid.other[2];
3969 *tl++ = txdr_unsigned(lp->nfsl_seqid);
3970 *tl++ = NFSMNT_MDSSESSION(nmp)->nfsess_clientid.lval[0];
3971 *tl = NFSMNT_MDSSESSION(nmp)->nfsess_clientid.lval[1];
3972 NFSBCOPY(lp->nfsl_owner, own, NFSV4CL_LOCKNAMELEN);
3973 NFSBCOPY(nfhp, &own[NFSV4CL_LOCKNAMELEN], fhlen);
3974 (void)nfsm_strtom(nd, own, NFSV4CL_LOCKNAMELEN + fhlen);
3977 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID + NFSX_UNSIGNED);
3978 if (NFSHASNFSV4N(nmp))
3981 *tl++ = lp->nfsl_stateid.seqid;
3982 *tl++ = lp->nfsl_stateid.other[0];
3983 *tl++ = lp->nfsl_stateid.other[1];
3984 *tl++ = lp->nfsl_stateid.other[2];
3985 *tl = txdr_unsigned(lp->nfsl_seqid);
3986 if (nfstest_outofseq &&
3987 (arc4random() % nfstest_outofseq) == 0)
3988 *tl = txdr_unsigned(lp->nfsl_seqid + 1);
3991 nd->nd_flag |= ND_USEGSSNAME;
3992 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, vp, p, cred,
3993 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
3997 NFSCL_INCRSEQID(lp->nfsl_open->nfso_own->nfsow_seqid, nd);
3998 NFSCL_INCRSEQID(lp->nfsl_seqid, nd);
3999 if (nd->nd_repstat == 0) {
4000 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID);
4001 lp->nfsl_stateid.seqid = *tl++;
4002 lp->nfsl_stateid.other[0] = *tl++;
4003 lp->nfsl_stateid.other[1] = *tl++;
4004 lp->nfsl_stateid.other[2] = *tl;
4005 } else if (nd->nd_repstat == NFSERR_DENIED) {
4006 NFSM_DISSECT(tl, u_int32_t *, 8 * NFSX_UNSIGNED);
4007 size = fxdr_unsigned(int, *(tl + 7));
4008 if (size < 0 || size > NFSV4_OPAQUELIMIT)
4011 error = nfsm_advance(nd, NFSM_RNDUP(size), -1);
4012 } else if (nd->nd_repstat == NFSERR_STALESTATEID ||
4013 nd->nd_repstat == NFSERR_BADSESSION)
4014 nfscl_initiate_recovery(lp->nfsl_open->nfso_own->nfsow_clp);
4016 mbuf_freem(nd->nd_mrep);
4022 * (always called with the vp for the mount point)
4025 nfsrpc_statfs(vnode_t vp, struct nfsstatfs *sbp, struct nfsfsinfo *fsp,
4026 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp,
4029 u_int32_t *tl = NULL;
4030 struct nfsrv_descript nfsd, *nd = &nfsd;
4031 struct nfsmount *nmp;
4032 nfsattrbit_t attrbits;
4036 nmp = VFSTONFS(vnode_mount(vp));
4037 if (NFSHASNFSV4(nmp)) {
4039 * For V4, you actually do a getattr.
4041 NFSCL_REQSTART(nd, NFSPROC_GETATTR, vp);
4042 NFSSTATFS_GETATTRBIT(&attrbits);
4043 (void) nfsrv_putattrbit(nd, &attrbits);
4044 nd->nd_flag |= ND_USEGSSNAME;
4045 error = nfscl_request(nd, vp, p, cred, stuff);
4048 if (nd->nd_repstat == 0) {
4049 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0,
4050 NULL, NULL, sbp, fsp, NULL, 0, NULL, NULL, NULL, p,
4053 nmp->nm_fsid[0] = nap->na_filesid[0];
4054 nmp->nm_fsid[1] = nap->na_filesid[1];
4055 NFSSETHASSETFSID(nmp);
4059 error = nd->nd_repstat;
4064 NFSCL_REQSTART(nd, NFSPROC_FSSTAT, vp);
4065 error = nfscl_request(nd, vp, p, cred, stuff);
4068 if (nd->nd_flag & ND_NFSV3) {
4069 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
4073 if (nd->nd_repstat) {
4074 error = nd->nd_repstat;
4077 NFSM_DISSECT(tl, u_int32_t *,
4078 NFSX_STATFS(nd->nd_flag & ND_NFSV3));
4080 if (NFSHASNFSV3(nmp)) {
4081 sbp->sf_tbytes = fxdr_hyper(tl); tl += 2;
4082 sbp->sf_fbytes = fxdr_hyper(tl); tl += 2;
4083 sbp->sf_abytes = fxdr_hyper(tl); tl += 2;
4084 sbp->sf_tfiles = fxdr_hyper(tl); tl += 2;
4085 sbp->sf_ffiles = fxdr_hyper(tl); tl += 2;
4086 sbp->sf_afiles = fxdr_hyper(tl); tl += 2;
4087 sbp->sf_invarsec = fxdr_unsigned(u_int32_t, *tl);
4088 } else if (NFSHASNFSV4(nmp) == 0) {
4089 sbp->sf_tsize = fxdr_unsigned(u_int32_t, *tl++);
4090 sbp->sf_bsize = fxdr_unsigned(u_int32_t, *tl++);
4091 sbp->sf_blocks = fxdr_unsigned(u_int32_t, *tl++);
4092 sbp->sf_bfree = fxdr_unsigned(u_int32_t, *tl++);
4093 sbp->sf_bavail = fxdr_unsigned(u_int32_t, *tl);
4096 mbuf_freem(nd->nd_mrep);
4104 nfsrpc_pathconf(vnode_t vp, struct nfsv3_pathconf *pc,
4105 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp,
4108 struct nfsrv_descript nfsd, *nd = &nfsd;
4109 struct nfsmount *nmp;
4111 nfsattrbit_t attrbits;
4115 nmp = VFSTONFS(vnode_mount(vp));
4116 if (NFSHASNFSV4(nmp)) {
4118 * For V4, you actually do a getattr.
4120 NFSCL_REQSTART(nd, NFSPROC_GETATTR, vp);
4121 NFSPATHCONF_GETATTRBIT(&attrbits);
4122 (void) nfsrv_putattrbit(nd, &attrbits);
4123 nd->nd_flag |= ND_USEGSSNAME;
4124 error = nfscl_request(nd, vp, p, cred, stuff);
4127 if (nd->nd_repstat == 0) {
4128 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0,
4129 pc, NULL, NULL, NULL, NULL, 0, NULL, NULL, NULL, p,
4134 error = nd->nd_repstat;
4137 NFSCL_REQSTART(nd, NFSPROC_PATHCONF, vp);
4138 error = nfscl_request(nd, vp, p, cred, stuff);
4141 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
4142 if (nd->nd_repstat && !error)
4143 error = nd->nd_repstat;
4145 NFSM_DISSECT(tl, u_int32_t *, NFSX_V3PATHCONF);
4146 pc->pc_linkmax = fxdr_unsigned(u_int32_t, *tl++);
4147 pc->pc_namemax = fxdr_unsigned(u_int32_t, *tl++);
4148 pc->pc_notrunc = fxdr_unsigned(u_int32_t, *tl++);
4149 pc->pc_chownrestricted =
4150 fxdr_unsigned(u_int32_t, *tl++);
4151 pc->pc_caseinsensitive =
4152 fxdr_unsigned(u_int32_t, *tl++);
4153 pc->pc_casepreserving = fxdr_unsigned(u_int32_t, *tl);
4157 mbuf_freem(nd->nd_mrep);
4162 * nfs version 3 fsinfo rpc call
4165 nfsrpc_fsinfo(vnode_t vp, struct nfsfsinfo *fsp, struct ucred *cred,
4166 NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, void *stuff)
4169 struct nfsrv_descript nfsd, *nd = &nfsd;
4173 NFSCL_REQSTART(nd, NFSPROC_FSINFO, vp);
4174 error = nfscl_request(nd, vp, p, cred, stuff);
4177 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
4178 if (nd->nd_repstat && !error)
4179 error = nd->nd_repstat;
4181 NFSM_DISSECT(tl, u_int32_t *, NFSX_V3FSINFO);
4182 fsp->fs_rtmax = fxdr_unsigned(u_int32_t, *tl++);
4183 fsp->fs_rtpref = fxdr_unsigned(u_int32_t, *tl++);
4184 fsp->fs_rtmult = fxdr_unsigned(u_int32_t, *tl++);
4185 fsp->fs_wtmax = fxdr_unsigned(u_int32_t, *tl++);
4186 fsp->fs_wtpref = fxdr_unsigned(u_int32_t, *tl++);
4187 fsp->fs_wtmult = fxdr_unsigned(u_int32_t, *tl++);
4188 fsp->fs_dtpref = fxdr_unsigned(u_int32_t, *tl++);
4189 fsp->fs_maxfilesize = fxdr_hyper(tl);
4191 fxdr_nfsv3time(tl, &fsp->fs_timedelta);
4193 fsp->fs_properties = fxdr_unsigned(u_int32_t, *tl);
4196 mbuf_freem(nd->nd_mrep);
4201 * This function performs the Renew RPC.
4204 nfsrpc_renew(struct nfsclclient *clp, struct nfsclds *dsp, struct ucred *cred,
4208 struct nfsrv_descript nfsd;
4209 struct nfsrv_descript *nd = &nfsd;
4210 struct nfsmount *nmp;
4212 struct nfssockreq *nrp;
4214 nmp = clp->nfsc_nmp;
4217 nfscl_reqstart(nd, NFSPROC_RENEW, nmp, NULL, 0, NULL,
4218 &dsp->nfsclds_sess);
4219 if (!NFSHASNFSV4N(nmp)) {
4220 /* NFSv4.1 just uses a Sequence Op and not a Renew. */
4221 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
4222 *tl++ = NFSMNT_MDSSESSION(nmp)->nfsess_clientid.lval[0];
4223 *tl = NFSMNT_MDSSESSION(nmp)->nfsess_clientid.lval[1];
4225 nrp = dsp->nfsclds_sockp;
4227 /* If NULL, use the MDS socket. */
4228 nrp = &nmp->nm_sockreq;
4229 nd->nd_flag |= ND_USEGSSNAME;
4230 error = newnfs_request(nd, nmp, NULL, nrp, NULL, p, cred,
4231 NFS_PROG, NFS_VER4, NULL, 1, NULL, &dsp->nfsclds_sess);
4234 error = nd->nd_repstat;
4235 mbuf_freem(nd->nd_mrep);
4240 * This function performs the Releaselockowner RPC.
4243 nfsrpc_rellockown(struct nfsmount *nmp, struct nfscllockowner *lp,
4244 uint8_t *fh, int fhlen, struct ucred *cred, NFSPROC_T *p)
4246 struct nfsrv_descript nfsd, *nd = &nfsd;
4249 uint8_t own[NFSV4CL_LOCKNAMELEN + NFSX_V4FHMAX];
4251 if (NFSHASNFSV4N(nmp)) {
4252 /* For NFSv4.1, do a FreeStateID. */
4253 nfscl_reqstart(nd, NFSPROC_FREESTATEID, nmp, NULL, 0, NULL,
4255 nfsm_stateidtom(nd, &lp->nfsl_stateid, NFSSTATEID_PUTSTATEID);
4257 nfscl_reqstart(nd, NFSPROC_RELEASELCKOWN, nmp, NULL, 0, NULL,
4259 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
4260 *tl++ = NFSMNT_MDSSESSION(nmp)->nfsess_clientid.lval[0];
4261 *tl = NFSMNT_MDSSESSION(nmp)->nfsess_clientid.lval[1];
4262 NFSBCOPY(lp->nfsl_owner, own, NFSV4CL_LOCKNAMELEN);
4263 NFSBCOPY(fh, &own[NFSV4CL_LOCKNAMELEN], fhlen);
4264 (void)nfsm_strtom(nd, own, NFSV4CL_LOCKNAMELEN + fhlen);
4266 nd->nd_flag |= ND_USEGSSNAME;
4267 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4268 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4271 error = nd->nd_repstat;
4272 mbuf_freem(nd->nd_mrep);
4277 * This function performs the Compound to get the mount pt FH.
4280 nfsrpc_getdirpath(struct nfsmount *nmp, u_char *dirpath, struct ucred *cred,
4284 struct nfsrv_descript nfsd;
4285 struct nfsrv_descript *nd = &nfsd;
4287 int error, cnt, len, setnil;
4290 nfscl_reqstart(nd, NFSPROC_PUTROOTFH, nmp, NULL, 0, &opcntp, NULL);
4298 while (*cp2 != '\0' && *cp2 != '/')
4305 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
4306 *tl = txdr_unsigned(NFSV4OP_LOOKUP);
4307 nfsm_strtom(nd, cp, strlen(cp));
4313 } while (*cp != '\0');
4314 if (NFSHASNFSV4N(nmp))
4315 /* Has a Sequence Op done by nfscl_reqstart(). */
4316 *opcntp = txdr_unsigned(3 + cnt);
4318 *opcntp = txdr_unsigned(2 + cnt);
4319 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
4320 *tl = txdr_unsigned(NFSV4OP_GETFH);
4321 nd->nd_flag |= ND_USEGSSNAME;
4322 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4323 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4326 if (nd->nd_repstat == 0) {
4327 NFSM_DISSECT(tl, u_int32_t *, (3 + 2 * cnt) * NFSX_UNSIGNED);
4328 tl += (2 + 2 * cnt);
4329 if ((len = fxdr_unsigned(int, *tl)) <= 0 ||
4331 nd->nd_repstat = NFSERR_BADXDR;
4333 nd->nd_repstat = nfsrv_mtostr(nd, nmp->nm_fh, len);
4334 if (nd->nd_repstat == 0)
4335 nmp->nm_fhsize = len;
4338 error = nd->nd_repstat;
4340 mbuf_freem(nd->nd_mrep);
4345 * This function performs the Delegreturn RPC.
4348 nfsrpc_delegreturn(struct nfscldeleg *dp, struct ucred *cred,
4349 struct nfsmount *nmp, NFSPROC_T *p, int syscred)
4352 struct nfsrv_descript nfsd;
4353 struct nfsrv_descript *nd = &nfsd;
4356 nfscl_reqstart(nd, NFSPROC_DELEGRETURN, nmp, dp->nfsdl_fh,
4357 dp->nfsdl_fhlen, NULL, NULL);
4358 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID);
4359 if (NFSHASNFSV4N(nmp))
4362 *tl++ = dp->nfsdl_stateid.seqid;
4363 *tl++ = dp->nfsdl_stateid.other[0];
4364 *tl++ = dp->nfsdl_stateid.other[1];
4365 *tl = dp->nfsdl_stateid.other[2];
4367 nd->nd_flag |= ND_USEGSSNAME;
4368 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4369 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4372 error = nd->nd_repstat;
4373 mbuf_freem(nd->nd_mrep);
4381 nfsrpc_getacl(vnode_t vp, struct ucred *cred, NFSPROC_T *p,
4382 struct acl *aclp, void *stuff)
4384 struct nfsrv_descript nfsd, *nd = &nfsd;
4386 nfsattrbit_t attrbits;
4387 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
4389 if (nfsrv_useacl == 0 || !NFSHASNFSV4(nmp))
4390 return (EOPNOTSUPP);
4391 NFSCL_REQSTART(nd, NFSPROC_GETACL, vp);
4392 NFSZERO_ATTRBIT(&attrbits);
4393 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_ACL);
4394 (void) nfsrv_putattrbit(nd, &attrbits);
4395 error = nfscl_request(nd, vp, p, cred, stuff);
4398 if (!nd->nd_repstat)
4399 error = nfsv4_loadattr(nd, vp, NULL, NULL, NULL, 0, NULL,
4400 NULL, NULL, NULL, aclp, 0, NULL, NULL, NULL, p, cred);
4402 error = nd->nd_repstat;
4403 mbuf_freem(nd->nd_mrep);
4411 nfsrpc_setacl(vnode_t vp, struct ucred *cred, NFSPROC_T *p,
4412 struct acl *aclp, void *stuff)
4415 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
4417 if (nfsrv_useacl == 0 || !NFSHASNFSV4(nmp))
4418 return (EOPNOTSUPP);
4419 error = nfsrpc_setattr(vp, NULL, aclp, cred, p, NULL, NULL, stuff);
4427 nfsrpc_setaclrpc(vnode_t vp, struct ucred *cred, NFSPROC_T *p,
4428 struct acl *aclp, nfsv4stateid_t *stateidp, void *stuff)
4430 struct nfsrv_descript nfsd, *nd = &nfsd;
4432 nfsattrbit_t attrbits;
4433 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
4435 if (!NFSHASNFSV4(nmp))
4436 return (EOPNOTSUPP);
4437 NFSCL_REQSTART(nd, NFSPROC_SETACL, vp);
4438 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID);
4439 NFSZERO_ATTRBIT(&attrbits);
4440 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_ACL);
4441 (void) nfsv4_fillattr(nd, vnode_mount(vp), vp, aclp, NULL, NULL, 0,
4442 &attrbits, NULL, NULL, 0, 0, 0, 0, (uint64_t)0);
4443 error = nfscl_request(nd, vp, p, cred, stuff);
4446 /* Don't care about the pre/postop attributes */
4447 mbuf_freem(nd->nd_mrep);
4448 return (nd->nd_repstat);
4452 * Do the NFSv4.1 Exchange ID.
4455 nfsrpc_exchangeid(struct nfsmount *nmp, struct nfsclclient *clp,
4456 struct nfssockreq *nrp, uint32_t exchflags, struct nfsclds **dspp,
4457 struct ucred *cred, NFSPROC_T *p)
4459 uint32_t *tl, v41flags;
4460 struct nfsrv_descript nfsd;
4461 struct nfsrv_descript *nd = &nfsd;
4462 struct nfsclds *dsp;
4463 struct timespec verstime;
4467 nfscl_reqstart(nd, NFSPROC_EXCHANGEID, nmp, NULL, 0, NULL, NULL);
4468 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_UNSIGNED);
4469 *tl++ = txdr_unsigned(nfsboottime.tv_sec); /* Client owner */
4470 *tl = txdr_unsigned(clp->nfsc_rev);
4471 (void) nfsm_strtom(nd, clp->nfsc_id, clp->nfsc_idlen);
4473 NFSM_BUILD(tl, uint32_t *, 3 * NFSX_UNSIGNED);
4474 *tl++ = txdr_unsigned(exchflags);
4475 *tl++ = txdr_unsigned(NFSV4EXCH_SP4NONE);
4477 /* Set the implementation id4 */
4478 *tl = txdr_unsigned(1);
4479 (void) nfsm_strtom(nd, "freebsd.org", strlen("freebsd.org"));
4480 (void) nfsm_strtom(nd, version, strlen(version));
4481 NFSM_BUILD(tl, uint32_t *, NFSX_V4TIME);
4482 verstime.tv_sec = 1293840000; /* Jan 1, 2011 */
4483 verstime.tv_nsec = 0;
4484 txdr_nfsv4time(&verstime, tl);
4485 nd->nd_flag |= ND_USEGSSNAME;
4486 error = newnfs_request(nd, nmp, NULL, nrp, NULL, p, cred,
4487 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4488 NFSCL_DEBUG(1, "exchangeid err=%d reps=%d\n", error,
4489 (int)nd->nd_repstat);
4492 if (nd->nd_repstat == 0) {
4493 NFSM_DISSECT(tl, uint32_t *, 6 * NFSX_UNSIGNED + NFSX_HYPER);
4494 len = fxdr_unsigned(int, *(tl + 7));
4495 if (len < 0 || len > NFSV4_OPAQUELIMIT) {
4496 error = NFSERR_BADXDR;
4499 dsp = malloc(sizeof(struct nfsclds) + len, M_NFSCLDS,
4501 dsp->nfsclds_expire = NFSD_MONOSEC + clp->nfsc_renew;
4502 dsp->nfsclds_servownlen = len;
4503 dsp->nfsclds_sess.nfsess_clientid.lval[0] = *tl++;
4504 dsp->nfsclds_sess.nfsess_clientid.lval[1] = *tl++;
4505 dsp->nfsclds_sess.nfsess_sequenceid =
4506 fxdr_unsigned(uint32_t, *tl++);
4507 v41flags = fxdr_unsigned(uint32_t, *tl);
4508 if ((v41flags & NFSV4EXCH_USEPNFSMDS) != 0 &&
4509 NFSHASPNFSOPT(nmp)) {
4510 NFSCL_DEBUG(1, "set PNFS\n");
4512 nmp->nm_state |= NFSSTA_PNFS;
4514 dsp->nfsclds_flags |= NFSCLDS_MDS;
4516 if ((v41flags & NFSV4EXCH_USEPNFSDS) != 0)
4517 dsp->nfsclds_flags |= NFSCLDS_DS;
4519 nd->nd_repstat = nfsrv_mtostr(nd,
4520 dsp->nfsclds_serverown, len);
4521 if (nd->nd_repstat == 0) {
4522 mtx_init(&dsp->nfsclds_mtx, "nfsds", NULL, MTX_DEF);
4523 mtx_init(&dsp->nfsclds_sess.nfsess_mtx, "nfssession",
4525 nfscl_initsessionslots(&dsp->nfsclds_sess);
4528 free(dsp, M_NFSCLDS);
4530 error = nd->nd_repstat;
4532 mbuf_freem(nd->nd_mrep);
4537 * Do the NFSv4.1 Create Session.
4540 nfsrpc_createsession(struct nfsmount *nmp, struct nfsclsession *sep,
4541 struct nfssockreq *nrp, uint32_t sequenceid, int mds, struct ucred *cred,
4544 uint32_t crflags, *tl;
4545 struct nfsrv_descript nfsd;
4546 struct nfsrv_descript *nd = &nfsd;
4549 nfscl_reqstart(nd, NFSPROC_CREATESESSION, nmp, NULL, 0, NULL, NULL);
4550 NFSM_BUILD(tl, uint32_t *, 4 * NFSX_UNSIGNED);
4551 *tl++ = sep->nfsess_clientid.lval[0];
4552 *tl++ = sep->nfsess_clientid.lval[1];
4553 *tl++ = txdr_unsigned(sequenceid);
4554 crflags = (NFSMNT_RDONLY(nmp->nm_mountp) ? 0 : NFSV4CRSESS_PERSIST);
4555 if (nfscl_enablecallb != 0 && nfs_numnfscbd > 0)
4556 crflags |= NFSV4CRSESS_CONNBACKCHAN;
4557 *tl = txdr_unsigned(crflags);
4559 /* Fill in fore channel attributes. */
4560 NFSM_BUILD(tl, uint32_t *, 7 * NFSX_UNSIGNED);
4561 *tl++ = 0; /* Header pad size */
4562 *tl++ = txdr_unsigned(100000); /* Max request size */
4563 *tl++ = txdr_unsigned(100000); /* Max response size */
4564 *tl++ = txdr_unsigned(4096); /* Max response size cached */
4565 *tl++ = txdr_unsigned(20); /* Max operations */
4566 *tl++ = txdr_unsigned(64); /* Max slots */
4567 *tl = 0; /* No rdma ird */
4569 /* Fill in back channel attributes. */
4570 NFSM_BUILD(tl, uint32_t *, 7 * NFSX_UNSIGNED);
4571 *tl++ = 0; /* Header pad size */
4572 *tl++ = txdr_unsigned(10000); /* Max request size */
4573 *tl++ = txdr_unsigned(10000); /* Max response size */
4574 *tl++ = txdr_unsigned(4096); /* Max response size cached */
4575 *tl++ = txdr_unsigned(4); /* Max operations */
4576 *tl++ = txdr_unsigned(NFSV4_CBSLOTS); /* Max slots */
4577 *tl = 0; /* No rdma ird */
4579 NFSM_BUILD(tl, uint32_t *, 8 * NFSX_UNSIGNED);
4580 *tl++ = txdr_unsigned(NFS_CALLBCKPROG); /* Call back prog # */
4582 /* Allow AUTH_SYS callbacks as uid, gid == 0. */
4583 *tl++ = txdr_unsigned(1); /* Auth_sys only */
4584 *tl++ = txdr_unsigned(AUTH_SYS); /* AUTH_SYS type */
4585 *tl++ = txdr_unsigned(nfsboottime.tv_sec); /* time stamp */
4586 *tl++ = 0; /* Null machine name */
4587 *tl++ = 0; /* Uid == 0 */
4588 *tl++ = 0; /* Gid == 0 */
4589 *tl = 0; /* No additional gids */
4590 nd->nd_flag |= ND_USEGSSNAME;
4591 error = newnfs_request(nd, nmp, NULL, nrp, NULL, p, cred, NFS_PROG,
4592 NFS_VER4, NULL, 1, NULL, NULL);
4595 if (nd->nd_repstat == 0) {
4596 NFSM_DISSECT(tl, uint32_t *, NFSX_V4SESSIONID +
4598 bcopy(tl, sep->nfsess_sessionid, NFSX_V4SESSIONID);
4599 tl += NFSX_V4SESSIONID / NFSX_UNSIGNED;
4600 sep->nfsess_sequenceid = fxdr_unsigned(uint32_t, *tl++);
4601 crflags = fxdr_unsigned(uint32_t, *tl);
4602 if ((crflags & NFSV4CRSESS_PERSIST) != 0 && mds != 0) {
4604 nmp->nm_state |= NFSSTA_SESSPERSIST;
4608 /* Get the fore channel slot count. */
4609 NFSM_DISSECT(tl, uint32_t *, 7 * NFSX_UNSIGNED);
4610 tl += 3; /* Skip the other counts. */
4611 sep->nfsess_maxcache = fxdr_unsigned(int, *tl++);
4613 sep->nfsess_foreslots = fxdr_unsigned(uint16_t, *tl++);
4614 NFSCL_DEBUG(4, "fore slots=%d\n", (int)sep->nfsess_foreslots);
4615 irdcnt = fxdr_unsigned(int, *tl);
4617 NFSM_DISSECT(tl, uint32_t *, irdcnt * NFSX_UNSIGNED);
4619 /* and the back channel slot count. */
4620 NFSM_DISSECT(tl, uint32_t *, 7 * NFSX_UNSIGNED);
4622 sep->nfsess_backslots = fxdr_unsigned(uint16_t, *tl);
4623 NFSCL_DEBUG(4, "back slots=%d\n", (int)sep->nfsess_backslots);
4625 error = nd->nd_repstat;
4627 mbuf_freem(nd->nd_mrep);
4632 * Do the NFSv4.1 Destroy Session.
4635 nfsrpc_destroysession(struct nfsmount *nmp, struct nfsclclient *clp,
4636 struct ucred *cred, NFSPROC_T *p)
4639 struct nfsrv_descript nfsd;
4640 struct nfsrv_descript *nd = &nfsd;
4643 nfscl_reqstart(nd, NFSPROC_DESTROYSESSION, nmp, NULL, 0, NULL, NULL);
4644 NFSM_BUILD(tl, uint32_t *, NFSX_V4SESSIONID);
4645 bcopy(NFSMNT_MDSSESSION(nmp)->nfsess_sessionid, tl, NFSX_V4SESSIONID);
4646 nd->nd_flag |= ND_USEGSSNAME;
4647 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4648 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4651 error = nd->nd_repstat;
4652 mbuf_freem(nd->nd_mrep);
4657 * Do the NFSv4.1 Destroy Client.
4660 nfsrpc_destroyclient(struct nfsmount *nmp, struct nfsclclient *clp,
4661 struct ucred *cred, NFSPROC_T *p)
4664 struct nfsrv_descript nfsd;
4665 struct nfsrv_descript *nd = &nfsd;
4668 nfscl_reqstart(nd, NFSPROC_DESTROYCLIENT, nmp, NULL, 0, NULL, NULL);
4669 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_UNSIGNED);
4670 *tl++ = NFSMNT_MDSSESSION(nmp)->nfsess_clientid.lval[0];
4671 *tl = NFSMNT_MDSSESSION(nmp)->nfsess_clientid.lval[1];
4672 nd->nd_flag |= ND_USEGSSNAME;
4673 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4674 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4677 error = nd->nd_repstat;
4678 mbuf_freem(nd->nd_mrep);
4683 * Do the NFSv4.1 LayoutGet.
4686 nfsrpc_layoutget(struct nfsmount *nmp, uint8_t *fhp, int fhlen, int iomode,
4687 uint64_t offset, uint64_t len, uint64_t minlen, int layoutlen,
4688 nfsv4stateid_t *stateidp, int *retonclosep, struct nfsclflayouthead *flhp,
4689 struct ucred *cred, NFSPROC_T *p, void *stuff)
4692 struct nfsrv_descript nfsd, *nd = &nfsd;
4694 struct nfsclflayout *flp, *prevflp, *tflp;
4695 int cnt, error, gotiomode, fhcnt, nfhlen, i, j;
4701 nfscl_reqstart(nd, NFSPROC_LAYOUTGET, nmp, fhp, fhlen, NULL, NULL);
4702 NFSM_BUILD(tl, uint32_t *, 4 * NFSX_UNSIGNED + 3 * NFSX_HYPER +
4704 *tl++ = newnfs_false; /* Don't signal availability. */
4705 *tl++ = txdr_unsigned(NFSLAYOUT_NFSV4_1_FILES);
4706 *tl++ = txdr_unsigned(iomode);
4707 txdr_hyper(offset, tl);
4709 txdr_hyper(len, tl);
4711 txdr_hyper(minlen, tl);
4713 *tl++ = txdr_unsigned(stateidp->seqid);
4714 NFSCL_DEBUG(4, "layget seq=%d\n", (int)stateidp->seqid);
4715 *tl++ = stateidp->other[0];
4716 *tl++ = stateidp->other[1];
4717 *tl++ = stateidp->other[2];
4718 *tl = txdr_unsigned(layoutlen);
4719 nd->nd_flag |= ND_USEGSSNAME;
4720 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4721 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4724 if (nd->nd_repstat == 0) {
4725 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED + NFSX_STATEID);
4730 stateidp->seqid = fxdr_unsigned(uint32_t, *tl++);
4731 NFSCL_DEBUG(4, "retoncls=%d stseq=%d\n", *retonclosep,
4732 (int)stateidp->seqid);
4733 stateidp->other[0] = *tl++;
4734 stateidp->other[1] = *tl++;
4735 stateidp->other[2] = *tl++;
4736 cnt = fxdr_unsigned(int, *tl);
4737 NFSCL_DEBUG(4, "layg cnt=%d\n", cnt);
4738 if (cnt <= 0 || cnt > 10000) {
4739 /* Don't accept more than 10000 layouts in reply. */
4740 error = NFSERR_BADXDR;
4743 for (i = 0; i < cnt; i++) {
4744 /* Dissect all the way to the file handle cnt. */
4745 NFSM_DISSECT(tl, uint32_t *, 3 * NFSX_HYPER +
4746 6 * NFSX_UNSIGNED + NFSX_V4DEVICEID);
4747 fhcnt = fxdr_unsigned(int, *(tl + 11 +
4748 NFSX_V4DEVICEID / NFSX_UNSIGNED));
4749 NFSCL_DEBUG(4, "fhcnt=%d\n", fhcnt);
4750 if (fhcnt < 0 || fhcnt > 100) {
4751 /* Don't accept more than 100 file handles. */
4752 error = NFSERR_BADXDR;
4756 flp = malloc(sizeof(*flp) + (fhcnt - 1) *
4757 sizeof(struct nfsfh *),
4758 M_NFSFLAYOUT, M_WAITOK);
4760 flp = malloc(sizeof(*flp),
4761 M_NFSFLAYOUT, M_WAITOK);
4762 flp->nfsfl_flags = 0;
4763 flp->nfsfl_fhcnt = 0;
4764 flp->nfsfl_devp = NULL;
4765 flp->nfsfl_off = fxdr_hyper(tl); tl += 2;
4766 retlen = fxdr_hyper(tl); tl += 2;
4767 if (flp->nfsfl_off + retlen < flp->nfsfl_off)
4768 flp->nfsfl_end = UINT64_MAX - flp->nfsfl_off;
4770 flp->nfsfl_end = flp->nfsfl_off + retlen;
4771 flp->nfsfl_iomode = fxdr_unsigned(int, *tl++);
4772 if (gotiomode == -1)
4773 gotiomode = flp->nfsfl_iomode;
4774 NFSCL_DEBUG(4, "layg reqiom=%d retiom=%d\n", iomode,
4775 (int)flp->nfsfl_iomode);
4776 if (fxdr_unsigned(int, *tl++) !=
4777 NFSLAYOUT_NFSV4_1_FILES) {
4778 printf("NFSv4.1: got non-files layout\n");
4779 error = NFSERR_BADXDR;
4782 NFSBCOPY(++tl, flp->nfsfl_dev, NFSX_V4DEVICEID);
4783 tl += (NFSX_V4DEVICEID / NFSX_UNSIGNED);
4784 flp->nfsfl_util = fxdr_unsigned(uint32_t, *tl++);
4785 NFSCL_DEBUG(4, "flutil=0x%x\n", flp->nfsfl_util);
4786 flp->nfsfl_stripe1 = fxdr_unsigned(uint32_t, *tl++);
4787 flp->nfsfl_patoff = fxdr_hyper(tl); tl += 2;
4788 if (fxdr_unsigned(int, *tl) != fhcnt) {
4789 printf("EEK! bad fhcnt\n");
4790 error = NFSERR_BADXDR;
4793 for (j = 0; j < fhcnt; j++) {
4794 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
4795 nfhlen = fxdr_unsigned(int, *tl);
4796 if (nfhlen <= 0 || nfhlen > NFSX_V4FHMAX) {
4797 error = NFSERR_BADXDR;
4800 nfhp = malloc(sizeof(*nfhp) + nfhlen - 1,
4802 flp->nfsfl_fh[j] = nfhp;
4804 nfhp->nfh_len = nfhlen;
4805 NFSM_DISSECT(cp, uint8_t *, NFSM_RNDUP(nfhlen));
4806 NFSBCOPY(cp, nfhp->nfh_fh, nfhlen);
4808 if (flp->nfsfl_iomode == gotiomode) {
4809 /* Keep the list in increasing offset order. */
4810 tflp = LIST_FIRST(flhp);
4812 while (tflp != NULL &&
4813 tflp->nfsfl_off < flp->nfsfl_off) {
4815 tflp = LIST_NEXT(tflp, nfsfl_list);
4817 if (prevflp == NULL)
4818 LIST_INSERT_HEAD(flhp, flp, nfsfl_list);
4820 LIST_INSERT_AFTER(prevflp, flp,
4823 printf("nfscl_layoutget(): got wrong iomode\n");
4824 nfscl_freeflayout(flp);
4829 if (nd->nd_repstat != 0 && error == 0)
4830 error = nd->nd_repstat;
4832 if (error != 0 && flp != NULL)
4833 nfscl_freeflayout(flp);
4834 mbuf_freem(nd->nd_mrep);
4839 * Do the NFSv4.1 Get Device Info.
4842 nfsrpc_getdeviceinfo(struct nfsmount *nmp, uint8_t *deviceid, int layouttype,
4843 uint32_t *notifybitsp, struct nfscldevinfo **ndip, struct ucred *cred,
4847 struct nfsrv_descript nfsd;
4848 struct nfsrv_descript *nd = &nfsd;
4849 struct sockaddr_storage ss;
4850 struct nfsclds *dsp = NULL, **dspp;
4851 struct nfscldevinfo *ndi;
4852 int addrcnt, bitcnt, error, i, isudp, j, pos, safilled, stripecnt;
4853 uint8_t stripeindex;
4857 nfscl_reqstart(nd, NFSPROC_GETDEVICEINFO, nmp, NULL, 0, NULL, NULL);
4858 NFSM_BUILD(tl, uint32_t *, NFSX_V4DEVICEID + 3 * NFSX_UNSIGNED);
4859 NFSBCOPY(deviceid, tl, NFSX_V4DEVICEID);
4860 tl += (NFSX_V4DEVICEID / NFSX_UNSIGNED);
4861 *tl++ = txdr_unsigned(layouttype);
4862 *tl++ = txdr_unsigned(100000);
4863 if (notifybitsp != NULL && *notifybitsp != 0) {
4864 *tl = txdr_unsigned(1); /* One word of bits. */
4865 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
4866 *tl = txdr_unsigned(*notifybitsp);
4868 *tl = txdr_unsigned(0);
4869 nd->nd_flag |= ND_USEGSSNAME;
4870 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4871 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4874 if (nd->nd_repstat == 0) {
4875 NFSM_DISSECT(tl, uint32_t *, 3 * NFSX_UNSIGNED);
4876 if (layouttype != fxdr_unsigned(int, *tl++))
4877 printf("EEK! devinfo layout type not same!\n");
4878 stripecnt = fxdr_unsigned(int, *++tl);
4879 NFSCL_DEBUG(4, "stripecnt=%d\n", stripecnt);
4880 if (stripecnt < 1 || stripecnt > 4096) {
4881 printf("NFS devinfo stripecnt %d: out of range\n",
4883 error = NFSERR_BADXDR;
4886 NFSM_DISSECT(tl, uint32_t *, (stripecnt + 1) * NFSX_UNSIGNED);
4887 addrcnt = fxdr_unsigned(int, *(tl + stripecnt));
4888 NFSCL_DEBUG(4, "addrcnt=%d\n", addrcnt);
4889 if (addrcnt < 1 || addrcnt > 128) {
4890 printf("NFS devinfo addrcnt %d: out of range\n",
4892 error = NFSERR_BADXDR;
4897 * Now we know how many stripe indices and addresses, so
4898 * we can allocate the structure the correct size.
4900 i = (stripecnt * sizeof(uint8_t)) / sizeof(struct nfsclds *)
4902 NFSCL_DEBUG(4, "stripeindices=%d\n", i);
4903 ndi = malloc(sizeof(*ndi) + (addrcnt + i) *
4904 sizeof(struct nfsclds *), M_NFSDEVINFO, M_WAITOK | M_ZERO);
4905 NFSBCOPY(deviceid, ndi->nfsdi_deviceid, NFSX_V4DEVICEID);
4906 ndi->nfsdi_refcnt = 0;
4907 ndi->nfsdi_stripecnt = stripecnt;
4908 ndi->nfsdi_addrcnt = addrcnt;
4909 /* Fill in the stripe indices. */
4910 for (i = 0; i < stripecnt; i++) {
4911 stripeindex = fxdr_unsigned(uint8_t, *tl++);
4912 NFSCL_DEBUG(4, "stripeind=%d\n", stripeindex);
4913 if (stripeindex >= addrcnt) {
4914 printf("NFS devinfo stripeindex %d: too big\n",
4916 error = NFSERR_BADXDR;
4919 nfsfldi_setstripeindex(ndi, i, stripeindex);
4922 /* Now, dissect the server address(es). */
4924 for (i = 0; i < addrcnt; i++) {
4925 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
4926 cnt = fxdr_unsigned(uint32_t, *tl);
4928 printf("NFS devinfo 0 len addrlist\n");
4929 error = NFSERR_BADXDR;
4932 dspp = nfsfldi_addr(ndi, i);
4933 pos = arc4random() % cnt; /* Choose one. */
4935 for (j = 0; j < cnt; j++) {
4936 error = nfsv4_getipaddr(nd, &ss, &isudp);
4937 if (error != 0 && error != EPERM) {
4938 error = NFSERR_BADXDR;
4941 if (error == 0 && isudp == 0) {
4944 * - use "pos" entry if it is of the
4945 * same af_family or none of them
4946 * is of the same af_family
4948 * - use the first one of the same
4951 if ((safilled == 0 && ss.ss_family ==
4952 nmp->nm_nam->sa_family) ||
4954 (safilled == 0 || ss.ss_family ==
4955 nmp->nm_nam->sa_family)) ||
4956 (safilled == 1 && ss.ss_family ==
4957 nmp->nm_nam->sa_family)) {
4958 error = nfsrpc_fillsa(nmp, &ss,
4963 nmp->nm_nam->sa_family)
4975 /* And the notify bits. */
4976 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
4977 if (safilled != 0) {
4978 bitcnt = fxdr_unsigned(int, *tl);
4980 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
4981 if (notifybitsp != NULL)
4983 fxdr_unsigned(uint32_t, *tl);
4989 if (nd->nd_repstat != 0)
4990 error = nd->nd_repstat;
4992 if (error != 0 && ndi != NULL)
4993 nfscl_freedevinfo(ndi);
4994 mbuf_freem(nd->nd_mrep);
4999 * Do the NFSv4.1 LayoutCommit.
5002 nfsrpc_layoutcommit(struct nfsmount *nmp, uint8_t *fh, int fhlen, int reclaim,
5003 uint64_t off, uint64_t len, uint64_t lastbyte, nfsv4stateid_t *stateidp,
5004 int layouttype, int layoutupdatecnt, uint8_t *layp, struct ucred *cred,
5005 NFSPROC_T *p, void *stuff)
5008 struct nfsrv_descript nfsd, *nd = &nfsd;
5009 int error, outcnt, i;
5012 nfscl_reqstart(nd, NFSPROC_LAYOUTCOMMIT, nmp, fh, fhlen, NULL, NULL);
5013 NFSM_BUILD(tl, uint32_t *, 5 * NFSX_UNSIGNED + 3 * NFSX_HYPER +
5015 txdr_hyper(off, tl);
5017 txdr_hyper(len, tl);
5020 *tl++ = newnfs_true;
5022 *tl++ = newnfs_false;
5023 *tl++ = txdr_unsigned(stateidp->seqid);
5024 *tl++ = stateidp->other[0];
5025 *tl++ = stateidp->other[1];
5026 *tl++ = stateidp->other[2];
5027 *tl++ = newnfs_true;
5030 else if (lastbyte >= (off + len))
5031 lastbyte = off + len - 1;
5032 txdr_hyper(lastbyte, tl);
5034 *tl++ = newnfs_false;
5035 *tl++ = txdr_unsigned(layouttype);
5036 *tl = txdr_unsigned(layoutupdatecnt);
5037 if (layoutupdatecnt > 0) {
5038 KASSERT(layouttype != NFSLAYOUT_NFSV4_1_FILES,
5039 ("Must be nil for Files Layout"));
5040 outcnt = NFSM_RNDUP(layoutupdatecnt);
5041 NFSM_BUILD(cp, uint8_t *, outcnt);
5042 NFSBCOPY(layp, cp, layoutupdatecnt);
5043 cp += layoutupdatecnt;
5044 for (i = 0; i < (outcnt - layoutupdatecnt); i++)
5047 nd->nd_flag |= ND_USEGSSNAME;
5048 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
5049 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
5052 error = nd->nd_repstat;
5053 mbuf_freem(nd->nd_mrep);
5058 * Do the NFSv4.1 LayoutReturn.
5061 nfsrpc_layoutreturn(struct nfsmount *nmp, uint8_t *fh, int fhlen, int reclaim,
5062 int layouttype, uint32_t iomode, int layoutreturn, uint64_t offset,
5063 uint64_t len, nfsv4stateid_t *stateidp, int layoutcnt, uint32_t *layp,
5064 struct ucred *cred, NFSPROC_T *p, void *stuff)
5067 struct nfsrv_descript nfsd, *nd = &nfsd;
5068 int error, outcnt, i;
5071 nfscl_reqstart(nd, NFSPROC_LAYOUTRETURN, nmp, fh, fhlen, NULL, NULL);
5072 NFSM_BUILD(tl, uint32_t *, 4 * NFSX_UNSIGNED);
5074 *tl++ = newnfs_true;
5076 *tl++ = newnfs_false;
5077 *tl++ = txdr_unsigned(layouttype);
5078 *tl++ = txdr_unsigned(iomode);
5079 *tl = txdr_unsigned(layoutreturn);
5080 if (layoutreturn == NFSLAYOUTRETURN_FILE) {
5081 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_HYPER + NFSX_STATEID +
5083 txdr_hyper(offset, tl);
5085 txdr_hyper(len, tl);
5087 NFSCL_DEBUG(4, "layoutret stseq=%d\n", (int)stateidp->seqid);
5088 *tl++ = txdr_unsigned(stateidp->seqid);
5089 *tl++ = stateidp->other[0];
5090 *tl++ = stateidp->other[1];
5091 *tl++ = stateidp->other[2];
5092 *tl = txdr_unsigned(layoutcnt);
5093 if (layoutcnt > 0) {
5094 outcnt = NFSM_RNDUP(layoutcnt);
5095 NFSM_BUILD(cp, uint8_t *, outcnt);
5096 NFSBCOPY(layp, cp, layoutcnt);
5098 for (i = 0; i < (outcnt - layoutcnt); i++)
5102 nd->nd_flag |= ND_USEGSSNAME;
5103 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
5104 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
5107 if (nd->nd_repstat == 0) {
5108 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
5110 NFSM_DISSECT(tl, uint32_t *, NFSX_STATEID);
5111 stateidp->seqid = fxdr_unsigned(uint32_t, *tl++);
5112 stateidp->other[0] = *tl++;
5113 stateidp->other[1] = *tl++;
5114 stateidp->other[2] = *tl;
5117 error = nd->nd_repstat;
5119 mbuf_freem(nd->nd_mrep);
5124 * Acquire a layout and devinfo, if possible. The caller must have acquired
5125 * a reference count on the nfsclclient structure before calling this.
5126 * Return the layout in lypp with a reference count on it, if successful.
5129 nfsrpc_getlayout(struct nfsmount *nmp, vnode_t vp, struct nfsfh *nfhp,
5130 int iomode, uint32_t *notifybitsp, nfsv4stateid_t *stateidp, uint64_t off,
5131 struct nfscllayout **lypp, struct ucred *cred, NFSPROC_T *p)
5133 struct nfscllayout *lyp;
5134 struct nfsclflayout *flp, *tflp;
5135 struct nfscldevinfo *dip;
5136 struct nfsclflayouthead flh;
5137 int error = 0, islocked, layoutlen, recalled, retonclose;
5138 nfsv4stateid_t stateid;
5142 * If lyp is returned non-NULL, there will be a refcnt (shared lock)
5143 * on it, iff flp != NULL or a lock (exclusive lock) on it iff
5146 lyp = nfscl_getlayout(nmp->nm_clp, nfhp->nfh_fh, nfhp->nfh_len,
5147 off, &flp, &recalled);
5149 if (lyp == NULL || flp == NULL) {
5153 layoutlen = NFSMNT_MDSSESSION(nmp)->nfsess_maxcache -
5154 (NFSX_STATEID + 3 * NFSX_UNSIGNED);
5157 stateid.other[0] = stateidp->other[0];
5158 stateid.other[1] = stateidp->other[1];
5159 stateid.other[2] = stateidp->other[2];
5160 error = nfsrpc_layoutget(nmp, nfhp->nfh_fh,
5161 nfhp->nfh_len, iomode, (uint64_t)0, INT64_MAX,
5162 (uint64_t)0, layoutlen, &stateid, &retonclose,
5163 &flh, cred, p, NULL);
5166 stateid.seqid = lyp->nfsly_stateid.seqid;
5167 stateid.other[0] = lyp->nfsly_stateid.other[0];
5168 stateid.other[1] = lyp->nfsly_stateid.other[1];
5169 stateid.other[2] = lyp->nfsly_stateid.other[2];
5170 error = nfsrpc_layoutget(nmp, nfhp->nfh_fh,
5171 nfhp->nfh_len, iomode, off, INT64_MAX,
5172 (uint64_t)0, layoutlen, &stateid, &retonclose,
5173 &flh, cred, p, NULL);
5176 LIST_FOREACH(tflp, &flh, nfsfl_list) {
5177 error = nfscl_adddevinfo(nmp, NULL, tflp);
5179 error = nfsrpc_getdeviceinfo(nmp,
5181 NFSLAYOUT_NFSV4_1_FILES,
5182 notifybitsp, &dip, cred, p);
5185 error = nfscl_adddevinfo(nmp, dip,
5189 "getlayout: cannot add\n");
5194 * nfscl_layout() always returns with the nfsly_lock
5195 * set to a refcnt (shared lock).
5197 error = nfscl_layout(nmp, vp, nfhp->nfh_fh,
5198 nfhp->nfh_len, &stateid, retonclose, &flh, &lyp,
5202 } else if (islocked != 0)
5203 nfsv4_unlock(&lyp->nfsly_lock, 0);
5210 * Do a TCP connection plus exchange id and create session.
5211 * If successful, a "struct nfsclds" is linked into the list for the
5212 * mount point and a pointer to it is returned.
5215 nfsrpc_fillsa(struct nfsmount *nmp, struct sockaddr_storage *ssp,
5216 struct nfsclds **dspp, NFSPROC_T *p)
5218 struct sockaddr_in *msad, *sad, *ssd;
5219 struct sockaddr_in6 *msad6, *sad6, *ssd6;
5220 struct nfsclclient *clp;
5221 struct nfssockreq *nrp;
5222 struct nfsclds *dsp, *tdsp;
5224 enum nfsclds_state retv;
5225 uint32_t sequenceid;
5227 KASSERT(nmp->nm_sockreq.nr_cred != NULL,
5228 ("nfsrpc_fillsa: NULL nr_cred"));
5234 if (ssp->ss_family == AF_INET) {
5235 ssd = (struct sockaddr_in *)ssp;
5239 * Check to see if we already have a session for this
5240 * address that is usable for a DS.
5241 * Note that the MDS's address is in a different place
5242 * than the sessions already acquired for DS's.
5244 msad = (struct sockaddr_in *)nmp->nm_sockreq.nr_nam;
5245 tdsp = TAILQ_FIRST(&nmp->nm_sess);
5246 while (tdsp != NULL) {
5247 if (msad != NULL && msad->sin_family == AF_INET &&
5248 ssd->sin_addr.s_addr == msad->sin_addr.s_addr &&
5249 ssd->sin_port == msad->sin_port &&
5250 (tdsp->nfsclds_flags & NFSCLDS_DS) != 0) {
5253 NFSCL_DEBUG(4, "fnd same addr\n");
5256 tdsp = TAILQ_NEXT(tdsp, nfsclds_list);
5257 if (tdsp != NULL && tdsp->nfsclds_sockp != NULL)
5258 msad = (struct sockaddr_in *)
5259 tdsp->nfsclds_sockp->nr_nam;
5265 /* No IP address match, so look for new/trunked one. */
5266 sad = malloc(sizeof(*sad), M_SONAME, M_WAITOK | M_ZERO);
5267 sad->sin_len = sizeof(*sad);
5268 sad->sin_family = AF_INET;
5269 sad->sin_port = ssd->sin_port;
5270 sad->sin_addr.s_addr = ssd->sin_addr.s_addr;
5271 nrp = malloc(sizeof(*nrp), M_NFSSOCKREQ, M_WAITOK | M_ZERO);
5272 nrp->nr_nam = (struct sockaddr *)sad;
5273 } else if (ssp->ss_family == AF_INET6) {
5274 ssd6 = (struct sockaddr_in6 *)ssp;
5278 * Check to see if we already have a session for this
5279 * address that is usable for a DS.
5280 * Note that the MDS's address is in a different place
5281 * than the sessions already acquired for DS's.
5283 msad6 = (struct sockaddr_in6 *)nmp->nm_sockreq.nr_nam;
5284 tdsp = TAILQ_FIRST(&nmp->nm_sess);
5285 while (tdsp != NULL) {
5286 if (msad6 != NULL && msad6->sin6_family == AF_INET6 &&
5287 IN6_ARE_ADDR_EQUAL(&ssd6->sin6_addr,
5288 &msad6->sin6_addr) &&
5289 ssd6->sin6_port == msad6->sin6_port &&
5290 (tdsp->nfsclds_flags & NFSCLDS_DS) != 0) {
5295 tdsp = TAILQ_NEXT(tdsp, nfsclds_list);
5296 if (tdsp != NULL && tdsp->nfsclds_sockp != NULL)
5297 msad6 = (struct sockaddr_in6 *)
5298 tdsp->nfsclds_sockp->nr_nam;
5304 /* No IP address match, so look for new/trunked one. */
5305 sad6 = malloc(sizeof(*sad6), M_SONAME, M_WAITOK | M_ZERO);
5306 sad6->sin6_len = sizeof(*sad6);
5307 sad6->sin6_family = AF_INET6;
5308 sad6->sin6_port = ssd6->sin6_port;
5309 NFSBCOPY(&ssd6->sin6_addr, &sad6->sin6_addr,
5310 sizeof(struct in6_addr));
5311 nrp = malloc(sizeof(*nrp), M_NFSSOCKREQ, M_WAITOK | M_ZERO);
5312 nrp->nr_nam = (struct sockaddr *)sad6;
5316 nrp->nr_sotype = SOCK_STREAM;
5317 mtx_init(&nrp->nr_mtx, "nfssock", NULL, MTX_DEF);
5318 nrp->nr_prog = NFS_PROG;
5319 nrp->nr_vers = NFS_VER4;
5322 * Use the credentials that were used for the mount, which are
5323 * in nmp->nm_sockreq.nr_cred for newnfs_connect() etc.
5324 * Ref. counting the credentials with crhold() is probably not
5325 * necessary, since nm_sockreq.nr_cred won't be crfree()'d until
5326 * unmount, but I did it anyhow.
5328 nrp->nr_cred = crhold(nmp->nm_sockreq.nr_cred);
5329 error = newnfs_connect(nmp, nrp, NULL, p, 0);
5330 NFSCL_DEBUG(3, "DS connect=%d\n", error);
5332 /* Now, do the exchangeid and create session. */
5334 error = nfsrpc_exchangeid(nmp, clp, nrp, NFSV4EXCH_USEPNFSDS,
5335 &dsp, nrp->nr_cred, p);
5336 NFSCL_DEBUG(3, "DS exchangeid=%d\n", error);
5338 dsp->nfsclds_sockp = nrp;
5340 retv = nfscl_getsameserver(nmp, dsp, &tdsp);
5341 NFSCL_DEBUG(3, "getsame ret=%d\n", retv);
5342 if (retv == NFSDSP_USETHISSESSION) {
5345 * If there is already a session for this server,
5348 (void)newnfs_disconnect(nrp);
5349 nfscl_freenfsclds(dsp);
5353 if (retv == NFSDSP_SEQTHISSESSION)
5354 sequenceid = tdsp->nfsclds_sess.nfsess_sequenceid;
5356 sequenceid = dsp->nfsclds_sess.nfsess_sequenceid;
5358 error = nfsrpc_createsession(nmp, &dsp->nfsclds_sess,
5359 nrp, sequenceid, 0, nrp->nr_cred, p);
5360 NFSCL_DEBUG(3, "DS createsess=%d\n", error);
5362 NFSFREECRED(nrp->nr_cred);
5363 NFSFREEMUTEX(&nrp->nr_mtx);
5364 free(nrp->nr_nam, M_SONAME);
5365 free(nrp, M_NFSSOCKREQ);
5368 NFSCL_DEBUG(3, "add DS session\n");
5370 * Put it at the end of the list. That way the list
5371 * is ordered by when the entry was added. This matters
5372 * since the one done first is the one that should be
5373 * used for sequencid'ing any subsequent create sessions.
5376 TAILQ_INSERT_TAIL(&nmp->nm_sess, dsp, nfsclds_list);
5379 } else if (dsp != NULL)
5380 nfscl_freenfsclds(dsp);
5385 * Do the NFSv4.1 Reclaim Complete.
5388 nfsrpc_reclaimcomplete(struct nfsmount *nmp, struct ucred *cred, NFSPROC_T *p)
5391 struct nfsrv_descript nfsd;
5392 struct nfsrv_descript *nd = &nfsd;
5395 nfscl_reqstart(nd, NFSPROC_RECLAIMCOMPL, nmp, NULL, 0, NULL, NULL);
5396 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
5398 nd->nd_flag |= ND_USEGSSNAME;
5399 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
5400 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
5403 error = nd->nd_repstat;
5404 mbuf_freem(nd->nd_mrep);
5409 * Initialize the slot tables for a session.
5412 nfscl_initsessionslots(struct nfsclsession *sep)
5416 for (i = 0; i < NFSV4_CBSLOTS; i++) {
5417 if (sep->nfsess_cbslots[i].nfssl_reply != NULL)
5418 m_freem(sep->nfsess_cbslots[i].nfssl_reply);
5419 NFSBZERO(&sep->nfsess_cbslots[i], sizeof(struct nfsslot));
5421 for (i = 0; i < 64; i++)
5422 sep->nfsess_slotseq[i] = 0;
5423 sep->nfsess_slots = 0;
5427 * Called to try and do an I/O operation via an NFSv4.1 Data Server (DS).
5430 nfscl_doiods(vnode_t vp, struct uio *uiop, int *iomode, int *must_commit,
5431 uint32_t rwaccess, struct ucred *cred, NFSPROC_T *p)
5433 struct nfsnode *np = VTONFS(vp);
5434 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
5435 struct nfscllayout *layp;
5436 struct nfscldevinfo *dip;
5437 struct nfsclflayout *rflp;
5438 nfsv4stateid_t stateid;
5439 struct ucred *newcred;
5440 uint64_t lastbyte, len, off, oresid, xfer;
5441 int eof, error, iolaymode, recalled;
5444 if (!NFSHASPNFS(nmp) || nfscl_enablecallb == 0 || nfs_numnfscbd == 0 ||
5445 (np->n_flag & NNOLAYOUT) != 0)
5447 /* Now, get a reference cnt on the clientid for this mount. */
5448 if (nfscl_getref(nmp) == 0)
5451 /* Find an appropriate stateid. */
5452 newcred = NFSNEWCRED(cred);
5453 error = nfscl_getstateid(vp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len,
5454 rwaccess, 1, newcred, p, &stateid, &lckp);
5456 NFSFREECRED(newcred);
5460 /* Search for a layout for this file. */
5461 off = uiop->uio_offset;
5462 layp = nfscl_getlayout(nmp->nm_clp, np->n_fhp->nfh_fh,
5463 np->n_fhp->nfh_len, off, &rflp, &recalled);
5464 if (layp == NULL || rflp == NULL) {
5465 if (recalled != 0) {
5466 NFSFREECRED(newcred);
5471 nfscl_rellayout(layp, (rflp == NULL) ? 1 : 0);
5474 /* Try and get a Layout, if it is supported. */
5475 if (rwaccess == NFSV4OPEN_ACCESSWRITE ||
5476 (np->n_flag & NWRITEOPENED) != 0)
5477 iolaymode = NFSLAYOUTIOMODE_RW;
5479 iolaymode = NFSLAYOUTIOMODE_READ;
5480 error = nfsrpc_getlayout(nmp, vp, np->n_fhp, iolaymode,
5481 NULL, &stateid, off, &layp, newcred, p);
5484 np->n_flag |= NNOLAYOUT;
5487 nfscl_lockderef(lckp);
5488 NFSFREECRED(newcred);
5490 nfscl_rellayout(layp, 0);
5497 * Loop around finding a layout that works for the first part of
5498 * this I/O operation, and then call the function that actually
5502 len = (uint64_t)uiop->uio_resid;
5503 while (len > 0 && error == 0 && eof == 0) {
5504 off = uiop->uio_offset;
5505 error = nfscl_findlayoutforio(layp, off, rwaccess, &rflp);
5507 oresid = xfer = (uint64_t)uiop->uio_resid;
5508 if (xfer > (rflp->nfsfl_end - rflp->nfsfl_off))
5509 xfer = rflp->nfsfl_end - rflp->nfsfl_off;
5510 dip = nfscl_getdevinfo(nmp->nm_clp, rflp->nfsfl_dev,
5513 error = nfscl_doflayoutio(vp, uiop, iomode,
5514 must_commit, &eof, &stateid, rwaccess, dip,
5515 layp, rflp, off, xfer, newcred, p);
5516 nfscl_reldevinfo(dip);
5517 lastbyte = off + xfer - 1;
5520 if (lastbyte > layp->nfsly_lastbyte)
5521 layp->nfsly_lastbyte = lastbyte;
5527 len -= (oresid - (uint64_t)uiop->uio_resid);
5531 nfscl_lockderef(lckp);
5532 NFSFREECRED(newcred);
5533 nfscl_rellayout(layp, 0);
5539 * Find a file layout that will handle the first bytes of the requested
5540 * range and return the information from it needed to to the I/O operation.
5543 nfscl_findlayoutforio(struct nfscllayout *lyp, uint64_t off, uint32_t rwaccess,
5544 struct nfsclflayout **retflpp)
5546 struct nfsclflayout *flp, *nflp, *rflp;
5551 /* For reading, do the Read list first and then the Write list. */
5553 if (rw == NFSV4OPEN_ACCESSREAD)
5554 flp = LIST_FIRST(&lyp->nfsly_flayread);
5556 flp = LIST_FIRST(&lyp->nfsly_flayrw);
5557 while (flp != NULL) {
5558 nflp = LIST_NEXT(flp, nfsfl_list);
5559 if (flp->nfsfl_off > off)
5561 if (flp->nfsfl_end > off &&
5562 (rflp == NULL || rflp->nfsfl_end < flp->nfsfl_end))
5566 if (rw == NFSV4OPEN_ACCESSREAD)
5567 rw = NFSV4OPEN_ACCESSWRITE;
5572 /* This one covers the most bytes starting at off. */
5580 * Do I/O using an NFSv4.1 file layout.
5583 nfscl_doflayoutio(vnode_t vp, struct uio *uiop, int *iomode, int *must_commit,
5584 int *eofp, nfsv4stateid_t *stateidp, int rwflag, struct nfscldevinfo *dp,
5585 struct nfscllayout *lyp, struct nfsclflayout *flp, uint64_t off,
5586 uint64_t len, struct ucred *cred, NFSPROC_T *p)
5588 uint64_t io_off, rel_off, stripe_unit_size, transfer, xfer;
5589 int commit_thru_mds, error = 0, stripe_index, stripe_pos;
5592 struct nfsclds **dspp;
5595 rel_off = off - flp->nfsfl_patoff;
5596 stripe_unit_size = (flp->nfsfl_util >> 6) & 0x3ffffff;
5597 stripe_pos = (rel_off / stripe_unit_size + flp->nfsfl_stripe1) %
5598 dp->nfsdi_stripecnt;
5599 transfer = stripe_unit_size - (rel_off % stripe_unit_size);
5601 /* Loop around, doing I/O for each stripe unit. */
5602 while (len > 0 && error == 0) {
5603 stripe_index = nfsfldi_stripeindex(dp, stripe_pos);
5604 dspp = nfsfldi_addr(dp, stripe_index);
5609 if ((flp->nfsfl_util & NFSFLAYUTIL_DENSE) != 0) {
5611 if (stripe_pos >= flp->nfsfl_fhcnt)
5613 fhp = flp->nfsfl_fh[stripe_pos];
5614 io_off = (rel_off / (stripe_unit_size *
5615 dp->nfsdi_stripecnt)) * stripe_unit_size +
5616 rel_off % stripe_unit_size;
5618 /* Sparse layout. */
5619 if (flp->nfsfl_fhcnt > 1) {
5620 if (stripe_index >= flp->nfsfl_fhcnt)
5622 fhp = flp->nfsfl_fh[stripe_index];
5623 } else if (flp->nfsfl_fhcnt == 1)
5624 fhp = flp->nfsfl_fh[0];
5629 if ((flp->nfsfl_util & NFSFLAYUTIL_COMMIT_THRU_MDS) != 0)
5630 commit_thru_mds = 1;
5632 commit_thru_mds = 0;
5633 if (rwflag == FREAD)
5634 error = nfsrpc_readds(vp, uiop, stateidp, eofp, *dspp,
5635 io_off, xfer, fhp, cred, p);
5637 error = nfsrpc_writeds(vp, uiop, iomode, must_commit,
5638 stateidp, *dspp, io_off, xfer, fhp, commit_thru_mds,
5642 lyp->nfsly_flags |= NFSLY_WRITTEN;
5647 transfer = stripe_unit_size;
5648 stripe_pos = (stripe_pos + 1) % dp->nfsdi_stripecnt;
5657 * The actual read RPC done to a DS.
5660 nfsrpc_readds(vnode_t vp, struct uio *uiop, nfsv4stateid_t *stateidp, int *eofp,
5661 struct nfsclds *dsp, uint64_t io_off, int len, struct nfsfh *fhp,
5662 struct ucred *cred, NFSPROC_T *p)
5666 struct nfsrv_descript nfsd;
5667 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
5668 struct nfsrv_descript *nd = &nfsd;
5669 struct nfssockreq *nrp;
5672 nfscl_reqstart(nd, NFSPROC_READDS, nmp, fhp->nfh_fh, fhp->nfh_len,
5673 NULL, &dsp->nfsclds_sess);
5674 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSEQIDZERO);
5675 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED * 3);
5676 txdr_hyper(io_off, tl);
5677 *(tl + 2) = txdr_unsigned(len);
5678 nrp = dsp->nfsclds_sockp;
5680 /* If NULL, use the MDS socket. */
5681 nrp = &nmp->nm_sockreq;
5682 error = newnfs_request(nd, nmp, NULL, nrp, vp, p, cred,
5683 NFS_PROG, NFS_VER4, NULL, 1, NULL, &dsp->nfsclds_sess);
5686 if (nd->nd_repstat != 0) {
5687 error = nd->nd_repstat;
5690 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
5691 *eofp = fxdr_unsigned(int, *tl);
5692 NFSM_STRSIZ(retlen, len);
5693 error = nfsm_mbufuio(nd, uiop, retlen);
5695 if (nd->nd_mrep != NULL)
5696 mbuf_freem(nd->nd_mrep);
5701 * The actual write RPC done to a DS.
5704 nfsrpc_writeds(vnode_t vp, struct uio *uiop, int *iomode, int *must_commit,
5705 nfsv4stateid_t *stateidp, struct nfsclds *dsp, uint64_t io_off, int len,
5706 struct nfsfh *fhp, int commit_thru_mds, struct ucred *cred, NFSPROC_T *p)
5709 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
5710 int error, rlen, commit, committed = NFSWRITE_FILESYNC;
5712 struct nfsrv_descript nfsd;
5713 struct nfsrv_descript *nd = &nfsd;
5714 struct nfssockreq *nrp;
5716 KASSERT(uiop->uio_iovcnt == 1, ("nfs: writerpc iovcnt > 1"));
5718 nfscl_reqstart(nd, NFSPROC_WRITEDS, nmp, fhp->nfh_fh, fhp->nfh_len,
5719 NULL, &dsp->nfsclds_sess);
5720 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSEQIDZERO);
5721 NFSM_BUILD(tl, uint32_t *, NFSX_HYPER + 2 * NFSX_UNSIGNED);
5722 txdr_hyper(io_off, tl);
5724 *tl++ = txdr_unsigned(*iomode);
5725 *tl = txdr_unsigned(len);
5726 nfsm_uiombuf(nd, uiop, len);
5727 nrp = dsp->nfsclds_sockp;
5729 /* If NULL, use the MDS socket. */
5730 nrp = &nmp->nm_sockreq;
5731 error = newnfs_request(nd, nmp, NULL, nrp, vp, p, cred,
5732 NFS_PROG, NFS_VER4, NULL, 1, NULL, &dsp->nfsclds_sess);
5735 if (nd->nd_repstat != 0) {
5737 * In case the rpc gets retried, roll
5738 * the uio fileds changed by nfsm_uiombuf()
5741 uiop->uio_offset -= len;
5742 uio_uio_resid_add(uiop, len);
5743 uio_iov_base_add(uiop, -len);
5744 uio_iov_len_add(uiop, len);
5745 error = nd->nd_repstat;
5747 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED + NFSX_VERF);
5748 rlen = fxdr_unsigned(int, *tl++);
5752 } else if (rlen < len) {
5753 backup = len - rlen;
5754 uio_iov_base_add(uiop, -(backup));
5755 uio_iov_len_add(uiop, backup);
5756 uiop->uio_offset -= backup;
5757 uio_uio_resid_add(uiop, backup);
5760 commit = fxdr_unsigned(int, *tl++);
5763 * Return the lowest committment level
5764 * obtained by any of the RPCs.
5766 if (committed == NFSWRITE_FILESYNC)
5768 else if (committed == NFSWRITE_DATASYNC &&
5769 commit == NFSWRITE_UNSTABLE)
5771 if (commit_thru_mds != 0) {
5773 if (!NFSHASWRITEVERF(nmp)) {
5774 NFSBCOPY(tl, nmp->nm_verf, NFSX_VERF);
5775 NFSSETWRITEVERF(nmp);
5776 } else if (NFSBCMP(tl, nmp->nm_verf, NFSX_VERF)) {
5778 NFSBCOPY(tl, nmp->nm_verf, NFSX_VERF);
5783 if ((dsp->nfsclds_flags & NFSCLDS_HASWRITEVERF) == 0) {
5784 NFSBCOPY(tl, dsp->nfsclds_verf, NFSX_VERF);
5785 dsp->nfsclds_flags |= NFSCLDS_HASWRITEVERF;
5786 } else if (NFSBCMP(tl, dsp->nfsclds_verf, NFSX_VERF)) {
5788 NFSBCOPY(tl, dsp->nfsclds_verf, NFSX_VERF);
5794 if (nd->nd_mrep != NULL)
5795 mbuf_freem(nd->nd_mrep);
5796 *iomode = committed;
5797 if (nd->nd_repstat != 0 && error == 0)
5798 error = nd->nd_repstat;
5803 * Free up the nfsclds structure.
5806 nfscl_freenfsclds(struct nfsclds *dsp)
5812 if (dsp->nfsclds_sockp != NULL) {
5813 NFSFREECRED(dsp->nfsclds_sockp->nr_cred);
5814 NFSFREEMUTEX(&dsp->nfsclds_sockp->nr_mtx);
5815 free(dsp->nfsclds_sockp->nr_nam, M_SONAME);
5816 free(dsp->nfsclds_sockp, M_NFSSOCKREQ);
5818 NFSFREEMUTEX(&dsp->nfsclds_mtx);
5819 NFSFREEMUTEX(&dsp->nfsclds_sess.nfsess_mtx);
5820 for (i = 0; i < NFSV4_CBSLOTS; i++) {
5821 if (dsp->nfsclds_sess.nfsess_cbslots[i].nfssl_reply != NULL)
5823 dsp->nfsclds_sess.nfsess_cbslots[i].nfssl_reply);
5825 free(dsp, M_NFSCLDS);
5828 static enum nfsclds_state
5829 nfscl_getsameserver(struct nfsmount *nmp, struct nfsclds *newdsp,
5830 struct nfsclds **retdspp)
5832 struct nfsclds *dsp, *cur_dsp;
5835 * Search the list of nfsclds structures for one with the same
5839 TAILQ_FOREACH(dsp, &nmp->nm_sess, nfsclds_list) {
5840 if (dsp->nfsclds_servownlen == newdsp->nfsclds_servownlen &&
5841 dsp->nfsclds_servownlen != 0 &&
5842 !NFSBCMP(dsp->nfsclds_serverown, newdsp->nfsclds_serverown,
5843 dsp->nfsclds_servownlen)) {
5844 NFSCL_DEBUG(4, "fnd same fdsp=%p dsp=%p flg=0x%x\n",
5845 TAILQ_FIRST(&nmp->nm_sess), dsp,
5846 dsp->nfsclds_flags);
5847 /* Server major id matches. */
5848 if ((dsp->nfsclds_flags & NFSCLDS_DS) != 0) {
5850 return (NFSDSP_USETHISSESSION);
5854 * Note the first match, so it can be used for
5855 * sequence'ing new sessions.
5857 if (cur_dsp == NULL)
5861 if (cur_dsp != NULL) {
5863 return (NFSDSP_SEQTHISSESSION);
5865 return (NFSDSP_NOTFOUND);
5870 * NFS commit rpc to a DS.
5873 nfsrpc_commitds(vnode_t vp, uint64_t offset, int cnt, struct nfsclds *dsp,
5874 struct nfsfh *fhp, struct ucred *cred, NFSPROC_T *p, void *stuff)
5877 struct nfsrv_descript nfsd, *nd = &nfsd;
5878 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
5879 struct nfssockreq *nrp;
5882 nfscl_reqstart(nd, NFSPROC_COMMITDS, nmp, fhp->nfh_fh, fhp->nfh_len,
5883 NULL, &dsp->nfsclds_sess);
5884 NFSM_BUILD(tl, uint32_t *, NFSX_HYPER + NFSX_UNSIGNED);
5885 txdr_hyper(offset, tl);
5887 *tl = txdr_unsigned(cnt);
5888 nrp = dsp->nfsclds_sockp;
5890 /* If NULL, use the MDS socket. */
5891 nrp = &nmp->nm_sockreq;
5892 error = newnfs_request(nd, nmp, NULL, nrp, vp, p, cred,
5893 NFS_PROG, NFS_VER4, NULL, 1, NULL, &dsp->nfsclds_sess);
5896 if (nd->nd_repstat == 0) {
5897 NFSM_DISSECT(tl, u_int32_t *, NFSX_VERF);
5899 if (NFSBCMP(tl, dsp->nfsclds_verf, NFSX_VERF)) {
5900 NFSBCOPY(tl, dsp->nfsclds_verf, NFSX_VERF);
5901 error = NFSERR_STALEWRITEVERF;
5906 if (error == 0 && nd->nd_repstat != 0)
5907 error = nd->nd_repstat;
5908 mbuf_freem(nd->nd_mrep);