2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Rick Macklem at The University of Guelph.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
40 * Rpc op calls, generally called from the vnode op calls or through the
41 * buffer cache, for NFS v2, 3 and 4.
42 * These do not normally make any changes to vnode arguments or use
43 * structures that might change between the VFS variants. The returned
44 * arguments are all at the end, after the NFSPROC_T *p one.
47 #include "opt_inet6.h"
49 #include <fs/nfs/nfsport.h>
50 #include <sys/sysctl.h>
51 #include <sys/taskqueue.h>
53 SYSCTL_DECL(_vfs_nfs);
55 static int nfsignore_eexist = 0;
56 SYSCTL_INT(_vfs_nfs, OID_AUTO, ignore_eexist, CTLFLAG_RW,
57 &nfsignore_eexist, 0, "NFS ignore EEXIST replies for mkdir/symlink");
59 static int nfscl_dssameconn = 0;
60 SYSCTL_INT(_vfs_nfs, OID_AUTO, dssameconn, CTLFLAG_RW,
61 &nfscl_dssameconn, 0, "Use same TCP connection to multiple DSs");
66 extern int nfs_numnfscbd;
67 extern struct timeval nfsboottime;
68 extern u_int32_t newnfs_false, newnfs_true;
69 extern nfstype nfsv34_type[9];
70 extern int nfsrv_useacl;
71 extern char nfsv4_callbackaddr[INET6_ADDRSTRLEN];
72 extern int nfscl_debuglevel;
73 extern int nfs_pnfsiothreads;
75 int nfstest_outofseq = 0;
76 int nfscl_assumeposixlocks = 1;
77 int nfscl_enablecallb = 0;
78 short nfsv4_cbport = NFSV4_CBPORT;
79 int nfstest_openallsetattr = 0;
81 #define DIRHDSIZ offsetof(struct dirent, d_name)
84 * nfscl_getsameserver() can return one of three values:
85 * NFSDSP_USETHISSESSION - Use this session for the DS.
86 * NFSDSP_SEQTHISSESSION - Use the nfsclds_sequence field of this dsp for new
88 * NFSDSP_NOTFOUND - No matching server was found.
91 NFSDSP_USETHISSESSION = 0,
92 NFSDSP_SEQTHISSESSION = 1,
97 * Do a write RPC on a DS data file, using this structure for the arguments,
98 * so that this function can be executed by a separate kernel process.
100 struct nfsclwritedsdorpc {
107 nfsv4stateid_t *stateidp;
120 static int nfsrpc_setattrrpc(vnode_t , struct vattr *, nfsv4stateid_t *,
121 struct ucred *, NFSPROC_T *, struct nfsvattr *, int *, void *);
122 static int nfsrpc_readrpc(vnode_t , struct uio *, struct ucred *,
123 nfsv4stateid_t *, NFSPROC_T *, struct nfsvattr *, int *, void *);
124 static int nfsrpc_writerpc(vnode_t , struct uio *, int *, int *,
125 struct ucred *, nfsv4stateid_t *, NFSPROC_T *, struct nfsvattr *, int *,
127 static int nfsrpc_createv23(vnode_t , char *, int, struct vattr *,
128 nfsquad_t, int, struct ucred *, NFSPROC_T *, struct nfsvattr *,
129 struct nfsvattr *, struct nfsfh **, int *, int *, void *);
130 static int nfsrpc_createv4(vnode_t , char *, int, struct vattr *,
131 nfsquad_t, int, struct nfsclowner *, struct nfscldeleg **, struct ucred *,
132 NFSPROC_T *, struct nfsvattr *, struct nfsvattr *, struct nfsfh **, int *,
133 int *, void *, int *);
134 static int nfsrpc_locku(struct nfsrv_descript *, struct nfsmount *,
135 struct nfscllockowner *, u_int64_t, u_int64_t,
136 u_int32_t, struct ucred *, NFSPROC_T *, int);
137 static int nfsrpc_setaclrpc(vnode_t, struct ucred *, NFSPROC_T *,
138 struct acl *, nfsv4stateid_t *, void *);
139 static int nfsrpc_getlayout(struct nfsmount *, vnode_t, struct nfsfh *, int,
140 uint32_t *, nfsv4stateid_t *, uint64_t, struct nfscllayout **,
141 struct ucred *, NFSPROC_T *);
142 static int nfsrpc_fillsa(struct nfsmount *, struct sockaddr_in *,
143 struct sockaddr_in6 *, sa_family_t, int, struct nfsclds **, NFSPROC_T *);
144 static void nfscl_initsessionslots(struct nfsclsession *);
145 static int nfscl_doflayoutio(vnode_t, struct uio *, int *, int *, int *,
146 nfsv4stateid_t *, int, struct nfscldevinfo *, struct nfscllayout *,
147 struct nfsclflayout *, uint64_t, uint64_t, int, struct ucred *,
149 static int nfscl_dofflayoutio(vnode_t, struct uio *, int *, int *, int *,
150 nfsv4stateid_t *, int, struct nfscldevinfo *, struct nfscllayout *,
151 struct nfsclflayout *, uint64_t, uint64_t, int, int, struct mbuf *,
152 struct nfsclwritedsdorpc *, struct ucred *, NFSPROC_T *);
153 static struct mbuf *nfsm_copym(struct mbuf *, int, int);
154 static int nfsrpc_readds(vnode_t, struct uio *, nfsv4stateid_t *, int *,
155 struct nfsclds *, uint64_t, int, struct nfsfh *, int, int, int,
156 struct ucred *, NFSPROC_T *);
157 static int nfsrpc_writeds(vnode_t, struct uio *, int *, int *,
158 nfsv4stateid_t *, struct nfsclds *, uint64_t, int,
159 struct nfsfh *, int, int, int, int, struct ucred *, NFSPROC_T *);
160 static int nfsio_writedsmir(vnode_t, int *, int *, nfsv4stateid_t *,
161 struct nfsclds *, uint64_t, int, struct nfsfh *, struct mbuf *, int, int,
162 struct nfsclwritedsdorpc *, struct ucred *, NFSPROC_T *);
163 static int nfsrpc_writedsmir(vnode_t, int *, int *, nfsv4stateid_t *,
164 struct nfsclds *, uint64_t, int, struct nfsfh *, struct mbuf *, int, int,
165 struct ucred *, NFSPROC_T *);
166 static enum nfsclds_state nfscl_getsameserver(struct nfsmount *,
167 struct nfsclds *, struct nfsclds **, uint32_t *);
168 static int nfsio_commitds(vnode_t, uint64_t, int, struct nfsclds *,
169 struct nfsfh *, int, int, struct nfsclwritedsdorpc *, struct ucred *,
171 static int nfsrpc_commitds(vnode_t, uint64_t, int, struct nfsclds *,
172 struct nfsfh *, int, int, struct ucred *, NFSPROC_T *);
173 static void nfsrv_setuplayoutget(struct nfsrv_descript *, int, uint64_t,
174 uint64_t, uint64_t, nfsv4stateid_t *, int, int, int);
175 static int nfsrv_parseug(struct nfsrv_descript *, int, uid_t *, gid_t *,
177 static int nfsrv_parselayoutget(struct nfsrv_descript *, nfsv4stateid_t *,
178 int *, struct nfsclflayouthead *);
179 static int nfsrpc_getopenlayout(struct nfsmount *, vnode_t, u_int8_t *,
180 int, uint8_t *, int, uint32_t, struct nfsclopen *, uint8_t *, int,
181 struct nfscldeleg **, struct ucred *, NFSPROC_T *);
182 static int nfsrpc_getcreatelayout(vnode_t, char *, int, struct vattr *,
183 nfsquad_t, int, struct nfsclowner *, struct nfscldeleg **,
184 struct ucred *, NFSPROC_T *, struct nfsvattr *, struct nfsvattr *,
185 struct nfsfh **, int *, int *, void *, int *);
186 static int nfsrpc_openlayoutrpc(struct nfsmount *, vnode_t, u_int8_t *,
187 int, uint8_t *, int, uint32_t, struct nfsclopen *, uint8_t *, int,
188 struct nfscldeleg **, nfsv4stateid_t *, int, int, int, int *,
189 struct nfsclflayouthead *, int *, struct ucred *, NFSPROC_T *);
190 static int nfsrpc_createlayout(vnode_t, char *, int, struct vattr *,
191 nfsquad_t, int, struct nfsclowner *, struct nfscldeleg **,
192 struct ucred *, NFSPROC_T *, struct nfsvattr *, struct nfsvattr *,
193 struct nfsfh **, int *, int *, void *, int *, nfsv4stateid_t *,
194 int, int, int, int *, struct nfsclflayouthead *, int *);
195 static int nfsrpc_layoutget(struct nfsmount *, uint8_t *, int, int, uint64_t,
196 uint64_t, uint64_t, int, int, nfsv4stateid_t *, int *,
197 struct nfsclflayouthead *, struct ucred *, NFSPROC_T *, void *);
198 static int nfsrpc_layoutgetres(struct nfsmount *, vnode_t, uint8_t *,
199 int, nfsv4stateid_t *, int, uint32_t *, struct nfscllayout **,
200 struct nfsclflayouthead *, int, int, int *, struct ucred *, NFSPROC_T *);
202 int nfs_pnfsio(task_fn_t *, void *);
205 * nfs null call from vfs.
208 nfsrpc_null(vnode_t vp, struct ucred *cred, NFSPROC_T *p)
211 struct nfsrv_descript nfsd, *nd = &nfsd;
213 NFSCL_REQSTART(nd, NFSPROC_NULL, vp);
214 error = nfscl_request(nd, vp, p, cred, NULL);
215 if (nd->nd_repstat && !error)
216 error = nd->nd_repstat;
217 mbuf_freem(nd->nd_mrep);
223 * For nfs version 3 and 4, use the access rpc to check accessibility. If file
224 * modes are changed on the server, accesses might still fail later.
227 nfsrpc_access(vnode_t vp, int acmode, struct ucred *cred,
228 NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp)
231 u_int32_t mode, rmode;
234 mode = NFSACCESS_READ;
237 if (vnode_vtype(vp) == VDIR) {
239 mode |= (NFSACCESS_MODIFY | NFSACCESS_EXTEND |
242 mode |= NFSACCESS_LOOKUP;
245 mode |= (NFSACCESS_MODIFY | NFSACCESS_EXTEND);
247 mode |= NFSACCESS_EXECUTE;
251 * Now, just call nfsrpc_accessrpc() to do the actual RPC.
253 error = nfsrpc_accessrpc(vp, mode, cred, p, nap, attrflagp, &rmode,
257 * The NFS V3 spec does not clarify whether or not
258 * the returned access bits can be a superset of
259 * the ones requested, so...
261 if (!error && (rmode & mode) != mode)
267 * The actual rpc, separated out for Darwin.
270 nfsrpc_accessrpc(vnode_t vp, u_int32_t mode, struct ucred *cred,
271 NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, u_int32_t *rmodep,
275 u_int32_t supported, rmode;
277 struct nfsrv_descript nfsd, *nd = &nfsd;
278 nfsattrbit_t attrbits;
282 NFSCL_REQSTART(nd, NFSPROC_ACCESS, vp);
283 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
284 *tl = txdr_unsigned(mode);
285 if (nd->nd_flag & ND_NFSV4) {
287 * And do a Getattr op.
289 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
290 *tl = txdr_unsigned(NFSV4OP_GETATTR);
291 NFSGETATTR_ATTRBIT(&attrbits);
292 (void) nfsrv_putattrbit(nd, &attrbits);
294 error = nfscl_request(nd, vp, p, cred, stuff);
297 if (nd->nd_flag & ND_NFSV3) {
298 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
302 if (!nd->nd_repstat) {
303 if (nd->nd_flag & ND_NFSV4) {
304 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
305 supported = fxdr_unsigned(u_int32_t, *tl++);
307 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
309 rmode = fxdr_unsigned(u_int32_t, *tl);
310 if (nd->nd_flag & ND_NFSV4)
311 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
314 * It's not obvious what should be done about
315 * unsupported access modes. For now, be paranoid
316 * and clear the unsupported ones.
321 error = nd->nd_repstat;
323 mbuf_freem(nd->nd_mrep);
331 nfsrpc_open(vnode_t vp, int amode, struct ucred *cred, NFSPROC_T *p)
333 struct nfsclopen *op;
334 struct nfscldeleg *dp;
336 struct nfsnode *np = VTONFS(vp);
337 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
338 u_int32_t mode, clidrev;
339 int ret, newone, error, expireret = 0, retrycnt;
342 * For NFSv4, Open Ops are only done on Regular Files.
344 if (vnode_vtype(vp) != VREG)
348 mode |= NFSV4OPEN_ACCESSREAD;
350 mode |= NFSV4OPEN_ACCESSWRITE;
355 { char name[100]; int namel;
356 namel = (np->n_v4->n4_namelen < 100) ? np->n_v4->n4_namelen : 99;
357 bcopy(NFS4NODENAME(np->n_v4), name, namel);
359 printf("rpcopen p=0x%x name=%s",p->p_pid,name);
360 if (nfhp->nfh_len > 0) printf(" fh=0x%x\n",nfhp->nfh_fh[12]);
361 else printf(" fhl=0\n");
366 error = nfscl_open(vp, nfhp->nfh_fh, nfhp->nfh_len, mode, 1,
367 cred, p, NULL, &op, &newone, &ret, 1);
371 if (nmp->nm_clp != NULL)
372 clidrev = nmp->nm_clp->nfsc_clientidrev;
375 if (ret == NFSCLOPEN_DOOPEN) {
376 if (np->n_v4 != NULL) {
378 * For the first attempt, try and get a layout, if
379 * pNFS is enabled for the mount.
381 if (!NFSHASPNFS(nmp) || nfscl_enablecallb == 0 ||
382 nfs_numnfscbd == 0 ||
383 (np->n_flag & NNOLAYOUT) != 0 || retrycnt > 0)
384 error = nfsrpc_openrpc(nmp, vp,
386 np->n_v4->n4_fhlen, np->n_fhp->nfh_fh,
387 np->n_fhp->nfh_len, mode, op,
388 NFS4NODENAME(np->n_v4),
389 np->n_v4->n4_namelen,
390 &dp, 0, 0x0, cred, p, 0, 0);
392 error = nfsrpc_getopenlayout(nmp, vp,
394 np->n_v4->n4_fhlen, np->n_fhp->nfh_fh,
395 np->n_fhp->nfh_len, mode, op,
396 NFS4NODENAME(np->n_v4),
397 np->n_v4->n4_namelen, &dp, cred, p);
400 OSBitAndAtomic((int32_t)~NDELEGMOD, (UInt32 *)&np->n_flag);
403 np->n_flag &= ~NDELEGMOD;
405 * Invalidate the attribute cache, so that
406 * attributes that pre-date the issue of a
407 * delegation are not cached, since the
408 * cached attributes will remain valid while
409 * the delegation is held.
411 NFSINVALATTRCACHE(np);
414 (void) nfscl_deleg(nmp->nm_mountp,
415 op->nfso_own->nfsow_clp,
416 nfhp->nfh_fh, nfhp->nfh_len, cred, p, &dp);
421 newnfs_copyincred(cred, &op->nfso_cred);
422 } else if (ret == NFSCLOPEN_SETCRED)
424 * This is a new local open on a delegation. It needs
425 * to have credentials so that an open can be done
426 * against the server during recovery.
428 newnfs_copyincred(cred, &op->nfso_cred);
431 * nfso_opencnt is the count of how many VOP_OPEN()s have
432 * been done on this Open successfully and a VOP_CLOSE()
433 * is expected for each of these.
434 * If error is non-zero, don't increment it, since the Open
435 * hasn't succeeded yet.
439 nfscl_openrelease(nmp, op, error, newone);
440 if (error == NFSERR_GRACE || error == NFSERR_STALECLIENTID ||
441 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
442 error == NFSERR_BADSESSION) {
443 (void) nfs_catnap(PZERO, error, "nfs_open");
444 } else if ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID)
446 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
449 } while (error == NFSERR_GRACE || error == NFSERR_STALECLIENTID ||
450 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
451 error == NFSERR_BADSESSION ||
452 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
453 expireret == 0 && clidrev != 0 && retrycnt < 4));
454 if (error && retrycnt >= 4)
460 * the actual open rpc
463 nfsrpc_openrpc(struct nfsmount *nmp, vnode_t vp, u_int8_t *nfhp, int fhlen,
464 u_int8_t *newfhp, int newfhlen, u_int32_t mode, struct nfsclopen *op,
465 u_int8_t *name, int namelen, struct nfscldeleg **dpp,
466 int reclaim, u_int32_t delegtype, struct ucred *cred, NFSPROC_T *p,
467 int syscred, int recursed)
470 struct nfsrv_descript nfsd, *nd = &nfsd;
471 struct nfscldeleg *dp, *ndp = NULL;
472 struct nfsvattr nfsva;
473 u_int32_t rflags, deleg;
474 nfsattrbit_t attrbits;
475 int error, ret, acesize, limitby;
476 struct nfsclsession *tsep;
480 nfscl_reqstart(nd, NFSPROC_OPEN, nmp, nfhp, fhlen, NULL, NULL, 0, 0);
481 NFSM_BUILD(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
482 *tl++ = txdr_unsigned(op->nfso_own->nfsow_seqid);
483 *tl++ = txdr_unsigned(mode & NFSV4OPEN_ACCESSBOTH);
484 *tl++ = txdr_unsigned((mode >> NFSLCK_SHIFT) & NFSV4OPEN_DENYBOTH);
485 tsep = nfsmnt_mdssession(nmp);
486 *tl++ = tsep->nfsess_clientid.lval[0];
487 *tl = tsep->nfsess_clientid.lval[1];
488 (void) nfsm_strtom(nd, op->nfso_own->nfsow_owner, NFSV4CL_LOCKNAMELEN);
489 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
490 *tl++ = txdr_unsigned(NFSV4OPEN_NOCREATE);
492 *tl = txdr_unsigned(NFSV4OPEN_CLAIMPREVIOUS);
493 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
494 *tl = txdr_unsigned(delegtype);
497 *tl = txdr_unsigned(NFSV4OPEN_CLAIMDELEGATECUR);
498 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID);
499 if (NFSHASNFSV4N(nmp))
502 *tl++ = dp->nfsdl_stateid.seqid;
503 *tl++ = dp->nfsdl_stateid.other[0];
504 *tl++ = dp->nfsdl_stateid.other[1];
505 *tl = dp->nfsdl_stateid.other[2];
507 *tl = txdr_unsigned(NFSV4OPEN_CLAIMNULL);
509 (void) nfsm_strtom(nd, name, namelen);
511 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
512 *tl = txdr_unsigned(NFSV4OP_GETATTR);
513 NFSZERO_ATTRBIT(&attrbits);
514 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_CHANGE);
515 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEMODIFY);
516 (void) nfsrv_putattrbit(nd, &attrbits);
518 nd->nd_flag |= ND_USEGSSNAME;
519 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, vp, p, cred,
520 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
523 NFSCL_INCRSEQID(op->nfso_own->nfsow_seqid, nd);
524 if (!nd->nd_repstat) {
525 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
527 op->nfso_stateid.seqid = *tl++;
528 op->nfso_stateid.other[0] = *tl++;
529 op->nfso_stateid.other[1] = *tl++;
530 op->nfso_stateid.other[2] = *tl;
531 rflags = fxdr_unsigned(u_int32_t, *(tl + 6));
532 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
535 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
536 deleg = fxdr_unsigned(u_int32_t, *tl);
537 if (deleg == NFSV4OPEN_DELEGATEREAD ||
538 deleg == NFSV4OPEN_DELEGATEWRITE) {
539 if (!(op->nfso_own->nfsow_clp->nfsc_flags &
540 NFSCLFLAGS_FIRSTDELEG))
541 op->nfso_own->nfsow_clp->nfsc_flags |=
542 (NFSCLFLAGS_FIRSTDELEG | NFSCLFLAGS_GOTDELEG);
544 sizeof (struct nfscldeleg) + newfhlen,
545 M_NFSCLDELEG, M_WAITOK);
546 LIST_INIT(&ndp->nfsdl_owner);
547 LIST_INIT(&ndp->nfsdl_lock);
548 ndp->nfsdl_clp = op->nfso_own->nfsow_clp;
549 ndp->nfsdl_fhlen = newfhlen;
550 NFSBCOPY(newfhp, ndp->nfsdl_fh, newfhlen);
551 newnfs_copyincred(cred, &ndp->nfsdl_cred);
552 nfscl_lockinit(&ndp->nfsdl_rwlock);
553 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
555 ndp->nfsdl_stateid.seqid = *tl++;
556 ndp->nfsdl_stateid.other[0] = *tl++;
557 ndp->nfsdl_stateid.other[1] = *tl++;
558 ndp->nfsdl_stateid.other[2] = *tl++;
559 ret = fxdr_unsigned(int, *tl);
560 if (deleg == NFSV4OPEN_DELEGATEWRITE) {
561 ndp->nfsdl_flags = NFSCLDL_WRITE;
563 * Indicates how much the file can grow.
565 NFSM_DISSECT(tl, u_int32_t *,
567 limitby = fxdr_unsigned(int, *tl++);
569 case NFSV4OPEN_LIMITSIZE:
570 ndp->nfsdl_sizelimit = fxdr_hyper(tl);
572 case NFSV4OPEN_LIMITBLOCKS:
573 ndp->nfsdl_sizelimit =
574 fxdr_unsigned(u_int64_t, *tl++);
575 ndp->nfsdl_sizelimit *=
576 fxdr_unsigned(u_int64_t, *tl);
579 error = NFSERR_BADXDR;
583 ndp->nfsdl_flags = NFSCLDL_READ;
586 ndp->nfsdl_flags |= NFSCLDL_RECALL;
587 error = nfsrv_dissectace(nd, &ndp->nfsdl_ace, &ret,
591 } else if (deleg != NFSV4OPEN_DELEGATENONE) {
592 error = NFSERR_BADXDR;
595 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
596 error = nfsv4_loadattr(nd, NULL, &nfsva, NULL,
597 NULL, 0, NULL, NULL, NULL, NULL, NULL, 0,
598 NULL, NULL, NULL, p, cred);
602 ndp->nfsdl_change = nfsva.na_filerev;
603 ndp->nfsdl_modtime = nfsva.na_mtime;
604 ndp->nfsdl_flags |= NFSCLDL_MODTIMESET;
606 if (!reclaim && (rflags & NFSV4OPEN_RESULTCONFIRM)) {
608 ret = nfsrpc_openconfirm(vp, newfhp, newfhlen, op,
610 if (ret == NFSERR_DELAY)
611 (void) nfs_catnap(PZERO, ret, "nfs_open");
612 } while (ret == NFSERR_DELAY);
615 if ((rflags & NFSV4OPEN_LOCKTYPEPOSIX) ||
616 nfscl_assumeposixlocks)
617 op->nfso_posixlock = 1;
619 op->nfso_posixlock = 0;
622 * If the server is handing out delegations, but we didn't
623 * get one because an OpenConfirm was required, try the
624 * Open again, to get a delegation. This is a harmless no-op,
625 * from a server's point of view.
627 if (!reclaim && (rflags & NFSV4OPEN_RESULTCONFIRM) &&
628 (op->nfso_own->nfsow_clp->nfsc_flags & NFSCLFLAGS_GOTDELEG)
629 && !error && dp == NULL && ndp == NULL && !recursed) {
631 ret = nfsrpc_openrpc(nmp, vp, nfhp, fhlen, newfhp,
632 newfhlen, mode, op, name, namelen, &ndp, 0, 0x0,
633 cred, p, syscred, 1);
634 if (ret == NFSERR_DELAY)
635 (void) nfs_catnap(PZERO, ret, "nfs_open2");
636 } while (ret == NFSERR_DELAY);
639 free(ndp, M_NFSCLDELEG);
642 if (ret == NFSERR_STALECLIENTID ||
643 ret == NFSERR_STALEDONTRECOVER ||
644 ret == NFSERR_BADSESSION)
649 if (nd->nd_repstat != 0 && error == 0)
650 error = nd->nd_repstat;
651 if (error == NFSERR_STALECLIENTID)
652 nfscl_initiate_recovery(op->nfso_own->nfsow_clp);
656 else if (ndp != NULL)
657 free(ndp, M_NFSCLDELEG);
658 mbuf_freem(nd->nd_mrep);
666 nfsrpc_opendowngrade(vnode_t vp, u_int32_t mode, struct nfsclopen *op,
667 struct ucred *cred, NFSPROC_T *p)
670 struct nfsrv_descript nfsd, *nd = &nfsd;
673 NFSCL_REQSTART(nd, NFSPROC_OPENDOWNGRADE, vp);
674 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID + 3 * NFSX_UNSIGNED);
675 if (NFSHASNFSV4N(VFSTONFS(vnode_mount(vp))))
678 *tl++ = op->nfso_stateid.seqid;
679 *tl++ = op->nfso_stateid.other[0];
680 *tl++ = op->nfso_stateid.other[1];
681 *tl++ = op->nfso_stateid.other[2];
682 *tl++ = txdr_unsigned(op->nfso_own->nfsow_seqid);
683 *tl++ = txdr_unsigned(mode & NFSV4OPEN_ACCESSBOTH);
684 *tl = txdr_unsigned((mode >> NFSLCK_SHIFT) & NFSV4OPEN_DENYBOTH);
685 error = nfscl_request(nd, vp, p, cred, NULL);
688 NFSCL_INCRSEQID(op->nfso_own->nfsow_seqid, nd);
689 if (!nd->nd_repstat) {
690 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID);
691 op->nfso_stateid.seqid = *tl++;
692 op->nfso_stateid.other[0] = *tl++;
693 op->nfso_stateid.other[1] = *tl++;
694 op->nfso_stateid.other[2] = *tl;
696 if (nd->nd_repstat && error == 0)
697 error = nd->nd_repstat;
698 if (error == NFSERR_STALESTATEID)
699 nfscl_initiate_recovery(op->nfso_own->nfsow_clp);
701 mbuf_freem(nd->nd_mrep);
706 * V4 Close operation.
709 nfsrpc_close(vnode_t vp, int doclose, NFSPROC_T *p)
711 struct nfsclclient *clp;
714 if (vnode_vtype(vp) != VREG)
717 error = nfscl_doclose(vp, &clp, p);
719 error = nfscl_getclose(vp, &clp);
723 nfscl_clientrelease(clp);
731 nfsrpc_doclose(struct nfsmount *nmp, struct nfsclopen *op, NFSPROC_T *p)
733 struct nfsrv_descript nfsd, *nd = &nfsd;
734 struct nfscllockowner *lp, *nlp;
735 struct nfscllock *lop, *nlop;
737 u_int64_t off = 0, len = 0;
738 u_int32_t type = NFSV4LOCKT_READ;
739 int error, do_unlock, trycnt;
741 tcred = newnfs_getcred();
742 newnfs_copycred(&op->nfso_cred, tcred);
744 * (Theoretically this could be done in the same
745 * compound as the close, but having multiple
746 * sequenced Ops in the same compound might be
747 * too scary for some servers.)
749 if (op->nfso_posixlock) {
752 type = NFSV4LOCKT_READ;
756 * Since this function is only called from VOP_INACTIVE(), no
757 * other thread will be manipulating this Open. As such, the
758 * lock lists are not being changed by other threads, so it should
759 * be safe to do this without locking.
761 LIST_FOREACH(lp, &op->nfso_lock, nfsl_list) {
763 LIST_FOREACH_SAFE(lop, &lp->nfsl_lock, nfslo_list, nlop) {
764 if (op->nfso_posixlock == 0) {
765 off = lop->nfslo_first;
766 len = lop->nfslo_end - lop->nfslo_first;
767 if (lop->nfslo_type == F_WRLCK)
768 type = NFSV4LOCKT_WRITE;
770 type = NFSV4LOCKT_READ;
775 error = nfsrpc_locku(nd, nmp, lp, off,
776 len, type, tcred, p, 0);
777 if ((nd->nd_repstat == NFSERR_GRACE ||
778 nd->nd_repstat == NFSERR_DELAY) &&
780 (void) nfs_catnap(PZERO,
783 } while ((nd->nd_repstat == NFSERR_GRACE ||
784 nd->nd_repstat == NFSERR_DELAY) &&
785 error == 0 && trycnt++ < 5);
786 if (op->nfso_posixlock)
789 nfscl_freelock(lop, 0);
792 * Do a ReleaseLockOwner.
793 * The lock owner name nfsl_owner may be used by other opens for
794 * other files but the lock_owner4 name that nfsrpc_rellockown()
795 * puts on the wire has the file handle for this file appended
796 * to it, so it can be done now.
798 (void)nfsrpc_rellockown(nmp, lp, lp->nfsl_open->nfso_fh,
799 lp->nfsl_open->nfso_fhlen, tcred, p);
803 * There could be other Opens for different files on the same
804 * OpenOwner, so locking is required.
807 nfscl_lockexcl(&op->nfso_own->nfsow_rwlock, NFSCLSTATEMUTEXPTR);
810 error = nfscl_tryclose(op, tcred, nmp, p);
811 if (error == NFSERR_GRACE)
812 (void) nfs_catnap(PZERO, error, "nfs_close");
813 } while (error == NFSERR_GRACE);
815 nfscl_lockunlock(&op->nfso_own->nfsow_rwlock);
817 LIST_FOREACH_SAFE(lp, &op->nfso_lock, nfsl_list, nlp)
818 nfscl_freelockowner(lp, 0);
819 nfscl_freeopen(op, 0);
825 * The actual Close RPC.
828 nfsrpc_closerpc(struct nfsrv_descript *nd, struct nfsmount *nmp,
829 struct nfsclopen *op, struct ucred *cred, NFSPROC_T *p,
835 nfscl_reqstart(nd, NFSPROC_CLOSE, nmp, op->nfso_fh,
836 op->nfso_fhlen, NULL, NULL, 0, 0);
837 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED + NFSX_STATEID);
838 *tl++ = txdr_unsigned(op->nfso_own->nfsow_seqid);
839 if (NFSHASNFSV4N(nmp))
842 *tl++ = op->nfso_stateid.seqid;
843 *tl++ = op->nfso_stateid.other[0];
844 *tl++ = op->nfso_stateid.other[1];
845 *tl = op->nfso_stateid.other[2];
847 nd->nd_flag |= ND_USEGSSNAME;
848 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
849 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
852 NFSCL_INCRSEQID(op->nfso_own->nfsow_seqid, nd);
853 if (nd->nd_repstat == 0)
854 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID);
855 error = nd->nd_repstat;
856 if (error == NFSERR_STALESTATEID)
857 nfscl_initiate_recovery(op->nfso_own->nfsow_clp);
859 mbuf_freem(nd->nd_mrep);
864 * V4 Open Confirm RPC.
867 nfsrpc_openconfirm(vnode_t vp, u_int8_t *nfhp, int fhlen,
868 struct nfsclopen *op, struct ucred *cred, NFSPROC_T *p)
871 struct nfsrv_descript nfsd, *nd = &nfsd;
872 struct nfsmount *nmp;
875 nmp = VFSTONFS(vnode_mount(vp));
876 if (NFSHASNFSV4N(nmp))
877 return (0); /* No confirmation for NFSv4.1. */
878 nfscl_reqstart(nd, NFSPROC_OPENCONFIRM, nmp, nfhp, fhlen, NULL, NULL,
880 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED + NFSX_STATEID);
881 *tl++ = op->nfso_stateid.seqid;
882 *tl++ = op->nfso_stateid.other[0];
883 *tl++ = op->nfso_stateid.other[1];
884 *tl++ = op->nfso_stateid.other[2];
885 *tl = txdr_unsigned(op->nfso_own->nfsow_seqid);
886 error = nfscl_request(nd, vp, p, cred, NULL);
889 NFSCL_INCRSEQID(op->nfso_own->nfsow_seqid, nd);
890 if (!nd->nd_repstat) {
891 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID);
892 op->nfso_stateid.seqid = *tl++;
893 op->nfso_stateid.other[0] = *tl++;
894 op->nfso_stateid.other[1] = *tl++;
895 op->nfso_stateid.other[2] = *tl;
897 error = nd->nd_repstat;
898 if (error == NFSERR_STALESTATEID)
899 nfscl_initiate_recovery(op->nfso_own->nfsow_clp);
901 mbuf_freem(nd->nd_mrep);
906 * Do the setclientid and setclientid confirm RPCs. Called from nfs_statfs()
907 * when a mount has just occurred and when the server replies NFSERR_EXPIRED.
910 nfsrpc_setclient(struct nfsmount *nmp, struct nfsclclient *clp, int reclaim,
911 struct ucred *cred, NFSPROC_T *p)
914 struct nfsrv_descript nfsd;
915 struct nfsrv_descript *nd = &nfsd;
916 nfsattrbit_t attrbits;
917 u_int8_t *cp = NULL, *cp2, addr[INET6_ADDRSTRLEN + 9];
919 int error, isinet6 = 0, callblen;
922 static u_int32_t rev = 0;
925 struct nfsclsession *tsep;
927 if (nfsboottime.tv_sec == 0)
928 NFSSETBOOTTIME(nfsboottime);
929 clp->nfsc_rev = rev++;
930 if (NFSHASNFSV4N(nmp)) {
932 * Either there was no previous session or the
933 * previous session has failed, so...
934 * do an ExchangeID followed by the CreateSession.
936 error = nfsrpc_exchangeid(nmp, clp, &nmp->nm_sockreq,
937 NFSV4EXCH_USEPNFSMDS | NFSV4EXCH_USENONPNFS, &dsp, cred, p);
938 NFSCL_DEBUG(1, "aft exch=%d\n", error);
940 error = nfsrpc_createsession(nmp, &dsp->nfsclds_sess,
942 dsp->nfsclds_sess.nfsess_sequenceid, 1, cred, p);
946 * The old sessions cannot be safely free'd
947 * here, since they may still be used by
951 if (TAILQ_FIRST(&nmp->nm_sess) != NULL)
952 tsep = NFSMNT_MDSSESSION(nmp);
953 TAILQ_INSERT_HEAD(&nmp->nm_sess, dsp,
956 * Wake up RPCs waiting for a slot on the
957 * old session. These will then fail with
958 * NFSERR_BADSESSION and be retried with the
959 * new session by nfsv4_setsequence().
960 * Also wakeup() processes waiting for the
964 wakeup(&tsep->nfsess_slots);
965 wakeup(&nmp->nm_sess);
968 nfscl_freenfsclds(dsp);
969 NFSCL_DEBUG(1, "aft createsess=%d\n", error);
970 if (error == 0 && reclaim == 0) {
971 error = nfsrpc_reclaimcomplete(nmp, cred, p);
972 NFSCL_DEBUG(1, "aft reclaimcomp=%d\n", error);
973 if (error == NFSERR_COMPLETEALREADY ||
974 error == NFSERR_NOTSUPP)
975 /* Ignore this error. */
982 * Allocate a single session structure for NFSv4.0, because some of
983 * the fields are used by NFSv4.0 although it doesn't do a session.
985 dsp = malloc(sizeof(struct nfsclds), M_NFSCLDS, M_WAITOK | M_ZERO);
986 mtx_init(&dsp->nfsclds_mtx, "nfsds", NULL, MTX_DEF);
987 mtx_init(&dsp->nfsclds_sess.nfsess_mtx, "nfssession", NULL, MTX_DEF);
989 TAILQ_INSERT_HEAD(&nmp->nm_sess, dsp, nfsclds_list);
990 tsep = NFSMNT_MDSSESSION(nmp);
993 nfscl_reqstart(nd, NFSPROC_SETCLIENTID, nmp, NULL, 0, NULL, NULL, 0, 0);
994 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
995 *tl++ = txdr_unsigned(nfsboottime.tv_sec);
996 *tl = txdr_unsigned(clp->nfsc_rev);
997 (void) nfsm_strtom(nd, clp->nfsc_id, clp->nfsc_idlen);
1000 * set up the callback address
1002 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1003 *tl = txdr_unsigned(NFS_CALLBCKPROG);
1004 callblen = strlen(nfsv4_callbackaddr);
1006 cp = nfscl_getmyip(nmp, &a6, &isinet6);
1007 if (nfscl_enablecallb && nfs_numnfscbd > 0 &&
1008 (callblen > 0 || cp != NULL)) {
1009 port = htons(nfsv4_cbport);
1010 cp2 = (u_int8_t *)&port;
1012 if ((callblen > 0 &&
1013 strchr(nfsv4_callbackaddr, ':')) || isinet6) {
1014 char ip6buf[INET6_ADDRSTRLEN], *ip6add;
1016 (void) nfsm_strtom(nd, "tcp6", 4);
1017 if (callblen == 0) {
1018 ip6_sprintf(ip6buf, (struct in6_addr *)cp);
1021 ip6add = nfsv4_callbackaddr;
1023 snprintf(addr, INET6_ADDRSTRLEN + 9, "%s.%d.%d",
1024 ip6add, cp2[0], cp2[1]);
1028 (void) nfsm_strtom(nd, "tcp", 3);
1030 snprintf(addr, INET6_ADDRSTRLEN + 9,
1031 "%d.%d.%d.%d.%d.%d", cp[0], cp[1],
1032 cp[2], cp[3], cp2[0], cp2[1]);
1034 snprintf(addr, INET6_ADDRSTRLEN + 9,
1035 "%s.%d.%d", nfsv4_callbackaddr,
1038 (void) nfsm_strtom(nd, addr, strlen(addr));
1040 (void) nfsm_strtom(nd, "tcp", 3);
1041 (void) nfsm_strtom(nd, "0.0.0.0.0.0", 11);
1043 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1044 *tl = txdr_unsigned(clp->nfsc_cbident);
1045 nd->nd_flag |= ND_USEGSSNAME;
1046 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
1047 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
1050 if (nd->nd_repstat == 0) {
1051 NFSM_DISSECT(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1052 tsep->nfsess_clientid.lval[0] = *tl++;
1053 tsep->nfsess_clientid.lval[1] = *tl++;
1054 confirm.lval[0] = *tl++;
1055 confirm.lval[1] = *tl;
1056 mbuf_freem(nd->nd_mrep);
1062 nfscl_reqstart(nd, NFSPROC_SETCLIENTIDCFRM, nmp, NULL, 0, NULL,
1064 NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1065 *tl++ = tsep->nfsess_clientid.lval[0];
1066 *tl++ = tsep->nfsess_clientid.lval[1];
1067 *tl++ = confirm.lval[0];
1068 *tl = confirm.lval[1];
1069 nd->nd_flag |= ND_USEGSSNAME;
1070 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p,
1071 cred, NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
1074 mbuf_freem(nd->nd_mrep);
1076 if (nd->nd_repstat == 0) {
1077 nfscl_reqstart(nd, NFSPROC_GETATTR, nmp, nmp->nm_fh,
1078 nmp->nm_fhsize, NULL, NULL, 0, 0);
1079 NFSZERO_ATTRBIT(&attrbits);
1080 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_LEASETIME);
1081 (void) nfsrv_putattrbit(nd, &attrbits);
1082 nd->nd_flag |= ND_USEGSSNAME;
1083 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p,
1084 cred, NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
1087 if (nd->nd_repstat == 0) {
1088 error = nfsv4_loadattr(nd, NULL, NULL, NULL, NULL, 0, NULL,
1089 NULL, NULL, NULL, NULL, 0, NULL, &lease, NULL, p, cred);
1092 clp->nfsc_renew = NFSCL_RENEW(lease);
1093 clp->nfsc_expire = NFSD_MONOSEC + clp->nfsc_renew;
1094 clp->nfsc_clientidrev++;
1095 if (clp->nfsc_clientidrev == 0)
1096 clp->nfsc_clientidrev++;
1100 error = nd->nd_repstat;
1102 mbuf_freem(nd->nd_mrep);
1110 nfsrpc_getattr(vnode_t vp, struct ucred *cred, NFSPROC_T *p,
1111 struct nfsvattr *nap, void *stuff)
1113 struct nfsrv_descript nfsd, *nd = &nfsd;
1115 nfsattrbit_t attrbits;
1117 NFSCL_REQSTART(nd, NFSPROC_GETATTR, vp);
1118 if (nd->nd_flag & ND_NFSV4) {
1119 NFSGETATTR_ATTRBIT(&attrbits);
1120 (void) nfsrv_putattrbit(nd, &attrbits);
1122 error = nfscl_request(nd, vp, p, cred, stuff);
1125 if (!nd->nd_repstat)
1126 error = nfsm_loadattr(nd, nap);
1128 error = nd->nd_repstat;
1129 mbuf_freem(nd->nd_mrep);
1134 * nfs getattr call with non-vnode arguemnts.
1137 nfsrpc_getattrnovp(struct nfsmount *nmp, u_int8_t *fhp, int fhlen, int syscred,
1138 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, u_int64_t *xidp,
1141 struct nfsrv_descript nfsd, *nd = &nfsd;
1142 int error, vers = NFS_VER2;
1143 nfsattrbit_t attrbits;
1145 nfscl_reqstart(nd, NFSPROC_GETATTR, nmp, fhp, fhlen, NULL, NULL, 0, 0);
1146 if (nd->nd_flag & ND_NFSV4) {
1148 NFSGETATTR_ATTRBIT(&attrbits);
1149 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_LEASETIME);
1150 (void) nfsrv_putattrbit(nd, &attrbits);
1151 } else if (nd->nd_flag & ND_NFSV3) {
1155 nd->nd_flag |= ND_USEGSSNAME;
1156 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
1157 NFS_PROG, vers, NULL, 1, xidp, NULL);
1160 if (nd->nd_repstat == 0) {
1161 if ((nd->nd_flag & ND_NFSV4) != 0)
1162 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0,
1163 NULL, NULL, NULL, NULL, NULL, 0, NULL, leasep, NULL,
1166 error = nfsm_loadattr(nd, nap);
1168 error = nd->nd_repstat;
1169 mbuf_freem(nd->nd_mrep);
1174 * Do an nfs setattr operation.
1177 nfsrpc_setattr(vnode_t vp, struct vattr *vap, NFSACL_T *aclp,
1178 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *rnap, int *attrflagp,
1181 int error, expireret = 0, openerr, retrycnt;
1182 u_int32_t clidrev = 0, mode;
1183 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
1185 nfsv4stateid_t stateid;
1188 if (nmp->nm_clp != NULL)
1189 clidrev = nmp->nm_clp->nfsc_clientidrev;
1190 if (vap != NULL && NFSATTRISSET(u_quad_t, vap, va_size))
1191 mode = NFSV4OPEN_ACCESSWRITE;
1193 mode = NFSV4OPEN_ACCESSREAD;
1198 if (NFSHASNFSV4(nmp)) {
1199 nfhp = VTONFS(vp)->n_fhp;
1200 error = nfscl_getstateid(vp, nfhp->nfh_fh,
1201 nfhp->nfh_len, mode, 0, cred, p, &stateid, &lckp);
1202 if (error && vnode_vtype(vp) == VREG &&
1203 (mode == NFSV4OPEN_ACCESSWRITE ||
1204 nfstest_openallsetattr)) {
1206 * No Open stateid, so try and open the file
1209 if (mode == NFSV4OPEN_ACCESSWRITE)
1210 openerr = nfsrpc_open(vp, FWRITE, cred,
1213 openerr = nfsrpc_open(vp, FREAD, cred,
1216 (void) nfscl_getstateid(vp,
1217 nfhp->nfh_fh, nfhp->nfh_len,
1218 mode, 0, cred, p, &stateid, &lckp);
1222 error = nfsrpc_setattrrpc(vp, vap, &stateid, cred, p,
1223 rnap, attrflagp, stuff);
1225 error = nfsrpc_setaclrpc(vp, cred, p, aclp, &stateid,
1227 if (error == NFSERR_OPENMODE && mode == NFSV4OPEN_ACCESSREAD) {
1229 nmp->nm_state |= NFSSTA_OPENMODE;
1232 if (error == NFSERR_STALESTATEID)
1233 nfscl_initiate_recovery(nmp->nm_clp);
1235 nfscl_lockderef(lckp);
1237 (void) nfsrpc_close(vp, 0, p);
1238 if (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
1239 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
1240 error == NFSERR_OLDSTATEID || error == NFSERR_BADSESSION) {
1241 (void) nfs_catnap(PZERO, error, "nfs_setattr");
1242 } else if ((error == NFSERR_EXPIRED ||
1243 error == NFSERR_BADSTATEID) && clidrev != 0) {
1244 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
1247 } while (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
1248 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
1249 error == NFSERR_BADSESSION ||
1250 (error == NFSERR_OLDSTATEID && retrycnt < 20) ||
1251 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
1252 expireret == 0 && clidrev != 0 && retrycnt < 4) ||
1253 (error == NFSERR_OPENMODE && mode == NFSV4OPEN_ACCESSREAD &&
1255 if (error && retrycnt >= 4)
1261 nfsrpc_setattrrpc(vnode_t vp, struct vattr *vap,
1262 nfsv4stateid_t *stateidp, struct ucred *cred, NFSPROC_T *p,
1263 struct nfsvattr *rnap, int *attrflagp, void *stuff)
1266 struct nfsrv_descript nfsd, *nd = &nfsd;
1268 nfsattrbit_t attrbits;
1271 NFSCL_REQSTART(nd, NFSPROC_SETATTR, vp);
1272 if (nd->nd_flag & ND_NFSV4)
1273 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID);
1274 vap->va_type = vnode_vtype(vp);
1275 nfscl_fillsattr(nd, vap, vp, NFSSATTR_FULL, 0);
1276 if (nd->nd_flag & ND_NFSV3) {
1277 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1279 } else if (nd->nd_flag & ND_NFSV4) {
1280 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1281 *tl = txdr_unsigned(NFSV4OP_GETATTR);
1282 NFSGETATTR_ATTRBIT(&attrbits);
1283 (void) nfsrv_putattrbit(nd, &attrbits);
1285 error = nfscl_request(nd, vp, p, cred, stuff);
1288 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4))
1289 error = nfscl_wcc_data(nd, vp, rnap, attrflagp, NULL, stuff);
1290 if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) == ND_NFSV4 && !error)
1291 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
1292 if (!(nd->nd_flag & ND_NFSV3) && !nd->nd_repstat && !error)
1293 error = nfscl_postop_attr(nd, rnap, attrflagp, stuff);
1294 mbuf_freem(nd->nd_mrep);
1295 if (nd->nd_repstat && !error)
1296 error = nd->nd_repstat;
1304 nfsrpc_lookup(vnode_t dvp, char *name, int len, struct ucred *cred,
1305 NFSPROC_T *p, struct nfsvattr *dnap, struct nfsvattr *nap,
1306 struct nfsfh **nfhpp, int *attrflagp, int *dattrflagp, void *stuff)
1309 struct nfsrv_descript nfsd, *nd = &nfsd;
1310 struct nfsmount *nmp;
1313 nfsattrbit_t attrbits;
1314 int error = 0, lookupp = 0;
1318 if (vnode_vtype(dvp) != VDIR)
1320 nmp = VFSTONFS(vnode_mount(dvp));
1321 if (len > NFS_MAXNAMLEN)
1322 return (ENAMETOOLONG);
1323 if (NFSHASNFSV4(nmp) && len == 1 &&
1326 * Just return the current dir's fh.
1329 nfhp = malloc(sizeof (struct nfsfh) +
1330 np->n_fhp->nfh_len, M_NFSFH, M_WAITOK);
1331 nfhp->nfh_len = np->n_fhp->nfh_len;
1332 NFSBCOPY(np->n_fhp->nfh_fh, nfhp->nfh_fh, nfhp->nfh_len);
1336 if (NFSHASNFSV4(nmp) && len == 2 &&
1337 name[0] == '.' && name[1] == '.') {
1339 NFSCL_REQSTART(nd, NFSPROC_LOOKUPP, dvp);
1341 NFSCL_REQSTART(nd, NFSPROC_LOOKUP, dvp);
1342 (void) nfsm_strtom(nd, name, len);
1344 if (nd->nd_flag & ND_NFSV4) {
1345 NFSGETATTR_ATTRBIT(&attrbits);
1346 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1347 *tl++ = txdr_unsigned(NFSV4OP_GETFH);
1348 *tl = txdr_unsigned(NFSV4OP_GETATTR);
1349 (void) nfsrv_putattrbit(nd, &attrbits);
1351 error = nfscl_request(nd, dvp, p, cred, stuff);
1354 if (nd->nd_repstat) {
1356 * When an NFSv4 Lookupp returns ENOENT, it means that
1357 * the lookup is at the root of an fs, so return this dir.
1359 if (nd->nd_repstat == NFSERR_NOENT && lookupp) {
1361 nfhp = malloc(sizeof (struct nfsfh) +
1362 np->n_fhp->nfh_len, M_NFSFH, M_WAITOK);
1363 nfhp->nfh_len = np->n_fhp->nfh_len;
1364 NFSBCOPY(np->n_fhp->nfh_fh, nfhp->nfh_fh, nfhp->nfh_len);
1366 mbuf_freem(nd->nd_mrep);
1369 if (nd->nd_flag & ND_NFSV3)
1370 error = nfscl_postop_attr(nd, dnap, dattrflagp, stuff);
1371 else if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) ==
1373 /* Load the directory attributes. */
1374 error = nfsm_loadattr(nd, dnap);
1380 if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) == ND_NFSV4) {
1381 /* Load the directory attributes. */
1382 error = nfsm_loadattr(nd, dnap);
1386 /* Skip over the Lookup and GetFH operation status values. */
1387 NFSM_DISSECT(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1389 error = nfsm_getfh(nd, nfhpp);
1393 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
1394 if ((nd->nd_flag & ND_NFSV3) && !error)
1395 error = nfscl_postop_attr(nd, dnap, dattrflagp, stuff);
1397 mbuf_freem(nd->nd_mrep);
1398 if (!error && nd->nd_repstat)
1399 error = nd->nd_repstat;
1404 * Do a readlink rpc.
1407 nfsrpc_readlink(vnode_t vp, struct uio *uiop, struct ucred *cred,
1408 NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, void *stuff)
1411 struct nfsrv_descript nfsd, *nd = &nfsd;
1412 struct nfsnode *np = VTONFS(vp);
1413 nfsattrbit_t attrbits;
1414 int error, len, cangetattr = 1;
1417 NFSCL_REQSTART(nd, NFSPROC_READLINK, vp);
1418 if (nd->nd_flag & ND_NFSV4) {
1420 * And do a Getattr op.
1422 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1423 *tl = txdr_unsigned(NFSV4OP_GETATTR);
1424 NFSGETATTR_ATTRBIT(&attrbits);
1425 (void) nfsrv_putattrbit(nd, &attrbits);
1427 error = nfscl_request(nd, vp, p, cred, stuff);
1430 if (nd->nd_flag & ND_NFSV3)
1431 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
1432 if (!nd->nd_repstat && !error) {
1433 NFSM_STRSIZ(len, NFS_MAXPATHLEN);
1435 * This seems weird to me, but must have been added to
1436 * FreeBSD for some reason. The only thing I can think of
1437 * is that there was/is some server that replies with
1438 * more link data than it should?
1440 if (len == NFS_MAXPATHLEN) {
1442 if (np->n_size > 0 && np->n_size < NFS_MAXPATHLEN) {
1448 error = nfsm_mbufuio(nd, uiop, len);
1449 if ((nd->nd_flag & ND_NFSV4) && !error && cangetattr)
1450 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
1452 if (nd->nd_repstat && !error)
1453 error = nd->nd_repstat;
1455 mbuf_freem(nd->nd_mrep);
1463 nfsrpc_read(vnode_t vp, struct uio *uiop, struct ucred *cred,
1464 NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, void *stuff)
1466 int error, expireret = 0, retrycnt;
1467 u_int32_t clidrev = 0;
1468 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
1469 struct nfsnode *np = VTONFS(vp);
1470 struct ucred *newcred;
1471 struct nfsfh *nfhp = NULL;
1472 nfsv4stateid_t stateid;
1475 if (nmp->nm_clp != NULL)
1476 clidrev = nmp->nm_clp->nfsc_clientidrev;
1478 if (NFSHASNFSV4(nmp)) {
1480 newcred = NFSNEWCRED(cred);
1485 if (NFSHASNFSV4(nmp))
1486 (void)nfscl_getstateid(vp, nfhp->nfh_fh, nfhp->nfh_len,
1487 NFSV4OPEN_ACCESSREAD, 0, newcred, p, &stateid,
1489 error = nfsrpc_readrpc(vp, uiop, newcred, &stateid, p, nap,
1491 if (error == NFSERR_OPENMODE) {
1493 nmp->nm_state |= NFSSTA_OPENMODE;
1496 if (error == NFSERR_STALESTATEID)
1497 nfscl_initiate_recovery(nmp->nm_clp);
1499 nfscl_lockderef(lckp);
1500 if (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
1501 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
1502 error == NFSERR_OLDSTATEID || error == NFSERR_BADSESSION) {
1503 (void) nfs_catnap(PZERO, error, "nfs_read");
1504 } else if ((error == NFSERR_EXPIRED ||
1505 error == NFSERR_BADSTATEID) && clidrev != 0) {
1506 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
1509 } while (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
1510 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
1511 error == NFSERR_BADSESSION ||
1512 (error == NFSERR_OLDSTATEID && retrycnt < 20) ||
1513 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
1514 expireret == 0 && clidrev != 0 && retrycnt < 4) ||
1515 (error == NFSERR_OPENMODE && retrycnt < 4));
1516 if (error && retrycnt >= 4)
1518 if (NFSHASNFSV4(nmp))
1519 NFSFREECRED(newcred);
1524 * The actual read RPC.
1527 nfsrpc_readrpc(vnode_t vp, struct uio *uiop, struct ucred *cred,
1528 nfsv4stateid_t *stateidp, NFSPROC_T *p, struct nfsvattr *nap,
1529 int *attrflagp, void *stuff)
1532 int error = 0, len, retlen, tsiz, eof = 0;
1533 struct nfsrv_descript nfsd;
1534 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
1535 struct nfsrv_descript *nd = &nfsd;
1540 tsiz = uio_uio_resid(uiop);
1541 tmp_off = uiop->uio_offset + tsiz;
1543 if (tmp_off > nmp->nm_maxfilesize || tmp_off < uiop->uio_offset) {
1547 rsize = nmp->nm_rsize;
1552 len = (tsiz > rsize) ? rsize : tsiz;
1553 NFSCL_REQSTART(nd, NFSPROC_READ, vp);
1554 if (nd->nd_flag & ND_NFSV4)
1555 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID);
1556 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED * 3);
1557 if (nd->nd_flag & ND_NFSV2) {
1558 *tl++ = txdr_unsigned(uiop->uio_offset);
1559 *tl++ = txdr_unsigned(len);
1562 txdr_hyper(uiop->uio_offset, tl);
1563 *(tl + 2) = txdr_unsigned(len);
1566 * Since I can't do a Getattr for NFSv4 for Write, there
1567 * doesn't seem any point in doing one here, either.
1568 * (See the comment in nfsrpc_writerpc() for more info.)
1570 error = nfscl_request(nd, vp, p, cred, stuff);
1573 if (nd->nd_flag & ND_NFSV3) {
1574 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
1575 } else if (!nd->nd_repstat && (nd->nd_flag & ND_NFSV2)) {
1576 error = nfsm_loadattr(nd, nap);
1580 if (nd->nd_repstat || error) {
1582 error = nd->nd_repstat;
1585 if (nd->nd_flag & ND_NFSV3) {
1586 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1587 eof = fxdr_unsigned(int, *(tl + 1));
1588 } else if (nd->nd_flag & ND_NFSV4) {
1589 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
1590 eof = fxdr_unsigned(int, *tl);
1592 NFSM_STRSIZ(retlen, len);
1593 error = nfsm_mbufuio(nd, uiop, retlen);
1596 mbuf_freem(nd->nd_mrep);
1599 if (!(nd->nd_flag & ND_NFSV2)) {
1600 if (eof || retlen == 0)
1602 } else if (retlen < len)
1607 if (nd->nd_mrep != NULL)
1608 mbuf_freem(nd->nd_mrep);
1613 * nfs write operation
1614 * When called_from_strategy != 0, it should return EIO for an error that
1615 * indicates recovery is in progress, so that the buffer will be left
1616 * dirty and be written back to the server later. If it loops around,
1617 * the recovery thread could get stuck waiting for the buffer and recovery
1618 * will then deadlock.
1621 nfsrpc_write(vnode_t vp, struct uio *uiop, int *iomode, int *must_commit,
1622 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp,
1623 void *stuff, int called_from_strategy)
1625 int error, expireret = 0, retrycnt, nostateid;
1626 u_int32_t clidrev = 0;
1627 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
1628 struct nfsnode *np = VTONFS(vp);
1629 struct ucred *newcred;
1630 struct nfsfh *nfhp = NULL;
1631 nfsv4stateid_t stateid;
1635 if (nmp->nm_clp != NULL)
1636 clidrev = nmp->nm_clp->nfsc_clientidrev;
1638 if (NFSHASNFSV4(nmp)) {
1639 newcred = NFSNEWCRED(cred);
1646 if (NFSHASNFSV4(nmp)) {
1647 (void)nfscl_getstateid(vp, nfhp->nfh_fh, nfhp->nfh_len,
1648 NFSV4OPEN_ACCESSWRITE, 0, newcred, p, &stateid,
1650 if (stateid.other[0] == 0 && stateid.other[1] == 0 &&
1651 stateid.other[2] == 0) {
1653 NFSCL_DEBUG(1, "stateid0 in write\n");
1658 * If there is no stateid for NFSv4, it means this is an
1659 * extraneous write after close. Basically a poorly
1660 * implemented buffer cache. Just don't do the write.
1665 error = nfsrpc_writerpc(vp, uiop, iomode, must_commit,
1666 newcred, &stateid, p, nap, attrflagp, stuff);
1667 if (error == NFSERR_STALESTATEID)
1668 nfscl_initiate_recovery(nmp->nm_clp);
1670 nfscl_lockderef(lckp);
1671 if (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
1672 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
1673 error == NFSERR_OLDSTATEID || error == NFSERR_BADSESSION) {
1674 (void) nfs_catnap(PZERO, error, "nfs_write");
1675 } else if ((error == NFSERR_EXPIRED ||
1676 error == NFSERR_BADSTATEID) && clidrev != 0) {
1677 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
1680 } while (error == NFSERR_GRACE || error == NFSERR_DELAY ||
1681 ((error == NFSERR_STALESTATEID || error == NFSERR_BADSESSION ||
1682 error == NFSERR_STALEDONTRECOVER) && called_from_strategy == 0) ||
1683 (error == NFSERR_OLDSTATEID && retrycnt < 20) ||
1684 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
1685 expireret == 0 && clidrev != 0 && retrycnt < 4));
1686 if (error != 0 && (retrycnt >= 4 ||
1687 ((error == NFSERR_STALESTATEID || error == NFSERR_BADSESSION ||
1688 error == NFSERR_STALEDONTRECOVER) && called_from_strategy != 0)))
1690 if (NFSHASNFSV4(nmp))
1691 NFSFREECRED(newcred);
1696 * The actual write RPC.
1699 nfsrpc_writerpc(vnode_t vp, struct uio *uiop, int *iomode,
1700 int *must_commit, struct ucred *cred, nfsv4stateid_t *stateidp,
1701 NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, void *stuff)
1704 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
1705 struct nfsnode *np = VTONFS(vp);
1706 int error = 0, len, tsiz, rlen, commit, committed = NFSWRITE_FILESYNC;
1707 int wccflag = 0, wsize;
1709 struct nfsrv_descript nfsd;
1710 struct nfsrv_descript *nd = &nfsd;
1711 nfsattrbit_t attrbits;
1714 KASSERT(uiop->uio_iovcnt == 1, ("nfs: writerpc iovcnt > 1"));
1716 tsiz = uio_uio_resid(uiop);
1717 tmp_off = uiop->uio_offset + tsiz;
1719 if (tmp_off > nmp->nm_maxfilesize || tmp_off < uiop->uio_offset) {
1723 wsize = nmp->nm_wsize;
1725 nd->nd_mrep = NULL; /* NFSv2 sometimes does a write with */
1726 nd->nd_repstat = 0; /* uio_resid == 0, so the while is not done */
1729 len = (tsiz > wsize) ? wsize : tsiz;
1730 NFSCL_REQSTART(nd, NFSPROC_WRITE, vp);
1731 if (nd->nd_flag & ND_NFSV4) {
1732 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID);
1733 NFSM_BUILD(tl, u_int32_t *, NFSX_HYPER+2*NFSX_UNSIGNED);
1734 txdr_hyper(uiop->uio_offset, tl);
1736 *tl++ = txdr_unsigned(*iomode);
1737 *tl = txdr_unsigned(len);
1738 } else if (nd->nd_flag & ND_NFSV3) {
1739 NFSM_BUILD(tl, u_int32_t *, NFSX_HYPER+3*NFSX_UNSIGNED);
1740 txdr_hyper(uiop->uio_offset, tl);
1742 *tl++ = txdr_unsigned(len);
1743 *tl++ = txdr_unsigned(*iomode);
1744 *tl = txdr_unsigned(len);
1748 NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1750 * Not sure why someone changed this, since the
1751 * RFC clearly states that "beginoffset" and
1752 * "totalcount" are ignored, but it wouldn't
1753 * surprise me if there's a busted server out there.
1755 /* Set both "begin" and "current" to non-garbage. */
1756 x = txdr_unsigned((u_int32_t)uiop->uio_offset);
1757 *tl++ = x; /* "begin offset" */
1758 *tl++ = x; /* "current offset" */
1759 x = txdr_unsigned(len);
1760 *tl++ = x; /* total to this offset */
1761 *tl = x; /* size of this write */
1764 nfsm_uiombuf(nd, uiop, len);
1766 * Although it is tempting to do a normal Getattr Op in the
1767 * NFSv4 compound, the result can be a nearly hung client
1768 * system if the Getattr asks for Owner and/or OwnerGroup.
1769 * It occurs when the client can't map either the Owner or
1770 * Owner_group name in the Getattr reply to a uid/gid. When
1771 * there is a cache miss, the kernel does an upcall to the
1772 * nfsuserd. Then, it can try and read the local /etc/passwd
1773 * or /etc/group file. It can then block in getnewbuf(),
1774 * waiting for dirty writes to be pushed to the NFS server.
1775 * The only reason this doesn't result in a complete
1776 * deadlock, is that the upcall times out and allows
1777 * the write to complete. However, progress is so slow
1778 * that it might just as well be deadlocked.
1779 * As such, we get the rest of the attributes, but not
1780 * Owner or Owner_group.
1781 * nb: nfscl_loadattrcache() needs to be told that these
1782 * partial attributes from a write rpc are being
1783 * passed in, via a argument flag.
1785 if (nd->nd_flag & ND_NFSV4) {
1786 NFSWRITEGETATTR_ATTRBIT(&attrbits);
1787 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1788 *tl = txdr_unsigned(NFSV4OP_GETATTR);
1789 (void) nfsrv_putattrbit(nd, &attrbits);
1791 error = nfscl_request(nd, vp, p, cred, stuff);
1794 if (nd->nd_repstat) {
1796 * In case the rpc gets retried, roll
1797 * the uio fileds changed by nfsm_uiombuf()
1800 uiop->uio_offset -= len;
1801 uio_uio_resid_add(uiop, len);
1802 uio_iov_base_add(uiop, -len);
1803 uio_iov_len_add(uiop, len);
1805 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) {
1806 error = nfscl_wcc_data(nd, vp, nap, attrflagp,
1811 if (!nd->nd_repstat) {
1812 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) {
1813 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED
1815 rlen = fxdr_unsigned(int, *tl++);
1819 } else if (rlen < len) {
1820 backup = len - rlen;
1821 uio_iov_base_add(uiop, -(backup));
1822 uio_iov_len_add(uiop, backup);
1823 uiop->uio_offset -= backup;
1824 uio_uio_resid_add(uiop, backup);
1827 commit = fxdr_unsigned(int, *tl++);
1830 * Return the lowest commitment level
1831 * obtained by any of the RPCs.
1833 if (committed == NFSWRITE_FILESYNC)
1835 else if (committed == NFSWRITE_DATASYNC &&
1836 commit == NFSWRITE_UNSTABLE)
1839 if (!NFSHASWRITEVERF(nmp)) {
1840 NFSBCOPY((caddr_t)tl,
1841 (caddr_t)&nmp->nm_verf[0],
1843 NFSSETWRITEVERF(nmp);
1844 } else if (NFSBCMP(tl, nmp->nm_verf,
1847 NFSBCOPY(tl, nmp->nm_verf, NFSX_VERF);
1851 if (nd->nd_flag & ND_NFSV4)
1852 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1853 if (nd->nd_flag & (ND_NFSV2 | ND_NFSV4)) {
1854 error = nfsm_loadattr(nd, nap);
1856 *attrflagp = NFS_LATTR_NOSHRINK;
1859 error = nd->nd_repstat;
1863 NFSWRITERPC_SETTIME(wccflag, np, nap, (nd->nd_flag & ND_NFSV4));
1864 mbuf_freem(nd->nd_mrep);
1869 if (nd->nd_mrep != NULL)
1870 mbuf_freem(nd->nd_mrep);
1871 *iomode = committed;
1872 if (nd->nd_repstat && !error)
1873 error = nd->nd_repstat;
1879 * For NFS v2 this is a kludge. Use a create rpc but with the IFMT bits of the
1880 * mode set to specify the file type and the size field for rdev.
1883 nfsrpc_mknod(vnode_t dvp, char *name, int namelen, struct vattr *vap,
1884 u_int32_t rdev, enum vtype vtyp, struct ucred *cred, NFSPROC_T *p,
1885 struct nfsvattr *dnap, struct nfsvattr *nnap, struct nfsfh **nfhpp,
1886 int *attrflagp, int *dattrflagp, void *dstuff)
1890 struct nfsrv_descript nfsd, *nd = &nfsd;
1891 nfsattrbit_t attrbits;
1896 if (namelen > NFS_MAXNAMLEN)
1897 return (ENAMETOOLONG);
1898 NFSCL_REQSTART(nd, NFSPROC_MKNOD, dvp);
1899 if (nd->nd_flag & ND_NFSV4) {
1900 if (vtyp == VBLK || vtyp == VCHR) {
1901 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
1902 *tl++ = vtonfsv34_type(vtyp);
1903 *tl++ = txdr_unsigned(NFSMAJOR(rdev));
1904 *tl = txdr_unsigned(NFSMINOR(rdev));
1906 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1907 *tl = vtonfsv34_type(vtyp);
1910 (void) nfsm_strtom(nd, name, namelen);
1911 if (nd->nd_flag & ND_NFSV3) {
1912 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1913 *tl = vtonfsv34_type(vtyp);
1915 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4))
1916 nfscl_fillsattr(nd, vap, dvp, 0, 0);
1917 if ((nd->nd_flag & ND_NFSV3) &&
1918 (vtyp == VCHR || vtyp == VBLK)) {
1919 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1920 *tl++ = txdr_unsigned(NFSMAJOR(rdev));
1921 *tl = txdr_unsigned(NFSMINOR(rdev));
1923 if (nd->nd_flag & ND_NFSV4) {
1924 NFSGETATTR_ATTRBIT(&attrbits);
1925 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1926 *tl++ = txdr_unsigned(NFSV4OP_GETFH);
1927 *tl = txdr_unsigned(NFSV4OP_GETATTR);
1928 (void) nfsrv_putattrbit(nd, &attrbits);
1930 if (nd->nd_flag & ND_NFSV2)
1931 nfscl_fillsattr(nd, vap, dvp, NFSSATTR_SIZERDEV, rdev);
1932 error = nfscl_request(nd, dvp, p, cred, dstuff);
1935 if (nd->nd_flag & ND_NFSV4)
1936 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
1937 if (!nd->nd_repstat) {
1938 if (nd->nd_flag & ND_NFSV4) {
1939 NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
1940 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
1944 error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp);
1948 if (nd->nd_flag & ND_NFSV3)
1949 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
1950 if (!error && nd->nd_repstat)
1951 error = nd->nd_repstat;
1953 mbuf_freem(nd->nd_mrep);
1958 * nfs file create call
1959 * Mostly just call the approriate routine. (I separated out v4, so that
1960 * error recovery wouldn't be as difficult.)
1963 nfsrpc_create(vnode_t dvp, char *name, int namelen, struct vattr *vap,
1964 nfsquad_t cverf, int fmode, struct ucred *cred, NFSPROC_T *p,
1965 struct nfsvattr *dnap, struct nfsvattr *nnap, struct nfsfh **nfhpp,
1966 int *attrflagp, int *dattrflagp, void *dstuff)
1968 int error = 0, newone, expireret = 0, retrycnt, unlocked;
1969 struct nfsclowner *owp;
1970 struct nfscldeleg *dp;
1971 struct nfsmount *nmp = VFSTONFS(vnode_mount(dvp));
1974 if (NFSHASNFSV4(nmp)) {
1978 error = nfscl_open(dvp, NULL, 0, (NFSV4OPEN_ACCESSWRITE |
1979 NFSV4OPEN_ACCESSREAD), 0, cred, p, &owp, NULL, &newone,
1983 if (nmp->nm_clp != NULL)
1984 clidrev = nmp->nm_clp->nfsc_clientidrev;
1987 if (!NFSHASPNFS(nmp) || nfscl_enablecallb == 0 ||
1988 nfs_numnfscbd == 0 || retrycnt > 0)
1989 error = nfsrpc_createv4(dvp, name, namelen, vap, cverf,
1990 fmode, owp, &dp, cred, p, dnap, nnap, nfhpp,
1991 attrflagp, dattrflagp, dstuff, &unlocked);
1993 error = nfsrpc_getcreatelayout(dvp, name, namelen, vap,
1994 cverf, fmode, owp, &dp, cred, p, dnap, nnap, nfhpp,
1995 attrflagp, dattrflagp, dstuff, &unlocked);
1997 * There is no need to invalidate cached attributes here,
1998 * since new post-delegation issue attributes are always
1999 * returned by nfsrpc_createv4() and these will update the
2003 (void) nfscl_deleg(nmp->nm_mountp, owp->nfsow_clp,
2004 (*nfhpp)->nfh_fh, (*nfhpp)->nfh_len, cred, p, &dp);
2005 nfscl_ownerrelease(nmp, owp, error, newone, unlocked);
2006 if (error == NFSERR_GRACE || error == NFSERR_STALECLIENTID ||
2007 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
2008 error == NFSERR_BADSESSION) {
2009 (void) nfs_catnap(PZERO, error, "nfs_open");
2010 } else if ((error == NFSERR_EXPIRED ||
2011 error == NFSERR_BADSTATEID) && clidrev != 0) {
2012 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
2015 } while (error == NFSERR_GRACE || error == NFSERR_STALECLIENTID ||
2016 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
2017 error == NFSERR_BADSESSION ||
2018 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
2019 expireret == 0 && clidrev != 0 && retrycnt < 4));
2020 if (error && retrycnt >= 4)
2023 error = nfsrpc_createv23(dvp, name, namelen, vap, cverf,
2024 fmode, cred, p, dnap, nnap, nfhpp, attrflagp, dattrflagp,
2031 * The create rpc for v2 and 3.
2034 nfsrpc_createv23(vnode_t dvp, char *name, int namelen, struct vattr *vap,
2035 nfsquad_t cverf, int fmode, struct ucred *cred, NFSPROC_T *p,
2036 struct nfsvattr *dnap, struct nfsvattr *nnap, struct nfsfh **nfhpp,
2037 int *attrflagp, int *dattrflagp, void *dstuff)
2041 struct nfsrv_descript nfsd, *nd = &nfsd;
2046 if (namelen > NFS_MAXNAMLEN)
2047 return (ENAMETOOLONG);
2048 NFSCL_REQSTART(nd, NFSPROC_CREATE, dvp);
2049 (void) nfsm_strtom(nd, name, namelen);
2050 if (nd->nd_flag & ND_NFSV3) {
2051 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2052 if (fmode & O_EXCL) {
2053 *tl = txdr_unsigned(NFSCREATE_EXCLUSIVE);
2054 NFSM_BUILD(tl, u_int32_t *, NFSX_VERF);
2055 *tl++ = cverf.lval[0];
2056 *tl = cverf.lval[1];
2058 *tl = txdr_unsigned(NFSCREATE_UNCHECKED);
2059 nfscl_fillsattr(nd, vap, dvp, 0, 0);
2062 nfscl_fillsattr(nd, vap, dvp, NFSSATTR_SIZE0, 0);
2064 error = nfscl_request(nd, dvp, p, cred, dstuff);
2067 if (nd->nd_repstat == 0) {
2068 error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp);
2072 if (nd->nd_flag & ND_NFSV3)
2073 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
2074 if (nd->nd_repstat != 0 && error == 0)
2075 error = nd->nd_repstat;
2077 mbuf_freem(nd->nd_mrep);
2082 nfsrpc_createv4(vnode_t dvp, char *name, int namelen, struct vattr *vap,
2083 nfsquad_t cverf, int fmode, struct nfsclowner *owp, struct nfscldeleg **dpp,
2084 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap,
2085 struct nfsvattr *nnap, struct nfsfh **nfhpp, int *attrflagp,
2086 int *dattrflagp, void *dstuff, int *unlockedp)
2089 int error = 0, deleg, newone, ret, acesize, limitby;
2090 struct nfsrv_descript nfsd, *nd = &nfsd;
2091 struct nfsclopen *op;
2092 struct nfscldeleg *dp = NULL;
2095 nfsattrbit_t attrbits;
2096 nfsv4stateid_t stateid;
2098 struct nfsmount *nmp;
2099 struct nfsclsession *tsep;
2101 nmp = VFSTONFS(dvp->v_mount);
2108 if (namelen > NFS_MAXNAMLEN)
2109 return (ENAMETOOLONG);
2110 NFSCL_REQSTART(nd, NFSPROC_CREATE, dvp);
2112 * For V4, this is actually an Open op.
2114 NFSM_BUILD(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
2115 *tl++ = txdr_unsigned(owp->nfsow_seqid);
2116 *tl++ = txdr_unsigned(NFSV4OPEN_ACCESSWRITE |
2117 NFSV4OPEN_ACCESSREAD);
2118 *tl++ = txdr_unsigned(NFSV4OPEN_DENYNONE);
2119 tsep = nfsmnt_mdssession(nmp);
2120 *tl++ = tsep->nfsess_clientid.lval[0];
2121 *tl = tsep->nfsess_clientid.lval[1];
2122 (void) nfsm_strtom(nd, owp->nfsow_owner, NFSV4CL_LOCKNAMELEN);
2123 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2124 *tl++ = txdr_unsigned(NFSV4OPEN_CREATE);
2125 if (fmode & O_EXCL) {
2126 if (NFSHASNFSV4N(nmp)) {
2127 if (NFSHASSESSPERSIST(nmp)) {
2128 /* Use GUARDED for persistent sessions. */
2129 *tl = txdr_unsigned(NFSCREATE_GUARDED);
2130 nfscl_fillsattr(nd, vap, dvp, 0, 0);
2132 /* Otherwise, use EXCLUSIVE4_1. */
2133 *tl = txdr_unsigned(NFSCREATE_EXCLUSIVE41);
2134 NFSM_BUILD(tl, u_int32_t *, NFSX_VERF);
2135 *tl++ = cverf.lval[0];
2136 *tl = cverf.lval[1];
2137 nfscl_fillsattr(nd, vap, dvp, 0, 0);
2141 *tl = txdr_unsigned(NFSCREATE_EXCLUSIVE);
2142 NFSM_BUILD(tl, u_int32_t *, NFSX_VERF);
2143 *tl++ = cverf.lval[0];
2144 *tl = cverf.lval[1];
2147 *tl = txdr_unsigned(NFSCREATE_UNCHECKED);
2148 nfscl_fillsattr(nd, vap, dvp, 0, 0);
2150 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2151 *tl = txdr_unsigned(NFSV4OPEN_CLAIMNULL);
2152 (void) nfsm_strtom(nd, name, namelen);
2153 /* Get the new file's handle and attributes. */
2154 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2155 *tl++ = txdr_unsigned(NFSV4OP_GETFH);
2156 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2157 NFSGETATTR_ATTRBIT(&attrbits);
2158 (void) nfsrv_putattrbit(nd, &attrbits);
2159 /* Get the directory's post-op attributes. */
2160 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2161 *tl = txdr_unsigned(NFSV4OP_PUTFH);
2162 (void) nfsm_fhtom(nd, np->n_fhp->nfh_fh, np->n_fhp->nfh_len, 0);
2163 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2164 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2165 (void) nfsrv_putattrbit(nd, &attrbits);
2166 error = nfscl_request(nd, dvp, p, cred, dstuff);
2169 NFSCL_INCRSEQID(owp->nfsow_seqid, nd);
2170 if (nd->nd_repstat == 0) {
2171 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
2173 stateid.seqid = *tl++;
2174 stateid.other[0] = *tl++;
2175 stateid.other[1] = *tl++;
2176 stateid.other[2] = *tl;
2177 rflags = fxdr_unsigned(u_int32_t, *(tl + 6));
2178 (void) nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
2179 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2180 deleg = fxdr_unsigned(int, *tl);
2181 if (deleg == NFSV4OPEN_DELEGATEREAD ||
2182 deleg == NFSV4OPEN_DELEGATEWRITE) {
2183 if (!(owp->nfsow_clp->nfsc_flags &
2184 NFSCLFLAGS_FIRSTDELEG))
2185 owp->nfsow_clp->nfsc_flags |=
2186 (NFSCLFLAGS_FIRSTDELEG | NFSCLFLAGS_GOTDELEG);
2188 sizeof (struct nfscldeleg) + NFSX_V4FHMAX,
2189 M_NFSCLDELEG, M_WAITOK);
2190 LIST_INIT(&dp->nfsdl_owner);
2191 LIST_INIT(&dp->nfsdl_lock);
2192 dp->nfsdl_clp = owp->nfsow_clp;
2193 newnfs_copyincred(cred, &dp->nfsdl_cred);
2194 nfscl_lockinit(&dp->nfsdl_rwlock);
2195 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
2197 dp->nfsdl_stateid.seqid = *tl++;
2198 dp->nfsdl_stateid.other[0] = *tl++;
2199 dp->nfsdl_stateid.other[1] = *tl++;
2200 dp->nfsdl_stateid.other[2] = *tl++;
2201 ret = fxdr_unsigned(int, *tl);
2202 if (deleg == NFSV4OPEN_DELEGATEWRITE) {
2203 dp->nfsdl_flags = NFSCLDL_WRITE;
2205 * Indicates how much the file can grow.
2207 NFSM_DISSECT(tl, u_int32_t *,
2209 limitby = fxdr_unsigned(int, *tl++);
2211 case NFSV4OPEN_LIMITSIZE:
2212 dp->nfsdl_sizelimit = fxdr_hyper(tl);
2214 case NFSV4OPEN_LIMITBLOCKS:
2215 dp->nfsdl_sizelimit =
2216 fxdr_unsigned(u_int64_t, *tl++);
2217 dp->nfsdl_sizelimit *=
2218 fxdr_unsigned(u_int64_t, *tl);
2221 error = NFSERR_BADXDR;
2225 dp->nfsdl_flags = NFSCLDL_READ;
2228 dp->nfsdl_flags |= NFSCLDL_RECALL;
2229 error = nfsrv_dissectace(nd, &dp->nfsdl_ace, &ret,
2233 } else if (deleg != NFSV4OPEN_DELEGATENONE) {
2234 error = NFSERR_BADXDR;
2237 error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp);
2240 /* Get rid of the PutFH and Getattr status values. */
2241 NFSM_DISSECT(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
2242 /* Load the directory attributes. */
2243 error = nfsm_loadattr(nd, dnap);
2247 if (dp != NULL && *attrflagp) {
2248 dp->nfsdl_change = nnap->na_filerev;
2249 dp->nfsdl_modtime = nnap->na_mtime;
2250 dp->nfsdl_flags |= NFSCLDL_MODTIMESET;
2253 * We can now complete the Open state.
2257 dp->nfsdl_fhlen = nfhp->nfh_len;
2258 NFSBCOPY(nfhp->nfh_fh, dp->nfsdl_fh, nfhp->nfh_len);
2261 * Get an Open structure that will be
2262 * attached to the OpenOwner, acquired already.
2264 error = nfscl_open(dvp, nfhp->nfh_fh, nfhp->nfh_len,
2265 (NFSV4OPEN_ACCESSWRITE | NFSV4OPEN_ACCESSREAD), 0,
2266 cred, p, NULL, &op, &newone, NULL, 0);
2269 op->nfso_stateid = stateid;
2270 newnfs_copyincred(cred, &op->nfso_cred);
2271 if ((rflags & NFSV4OPEN_RESULTCONFIRM)) {
2273 ret = nfsrpc_openconfirm(dvp, nfhp->nfh_fh,
2274 nfhp->nfh_len, op, cred, p);
2275 if (ret == NFSERR_DELAY)
2276 (void) nfs_catnap(PZERO, ret, "nfs_create");
2277 } while (ret == NFSERR_DELAY);
2282 * If the server is handing out delegations, but we didn't
2283 * get one because an OpenConfirm was required, try the
2284 * Open again, to get a delegation. This is a harmless no-op,
2285 * from a server's point of view.
2287 if ((rflags & NFSV4OPEN_RESULTCONFIRM) &&
2288 (owp->nfsow_clp->nfsc_flags & NFSCLFLAGS_GOTDELEG) &&
2289 !error && dp == NULL) {
2291 ret = nfsrpc_openrpc(VFSTONFS(vnode_mount(dvp)), dvp,
2292 np->n_fhp->nfh_fh, np->n_fhp->nfh_len,
2293 nfhp->nfh_fh, nfhp->nfh_len,
2294 (NFSV4OPEN_ACCESSWRITE | NFSV4OPEN_ACCESSREAD), op,
2295 name, namelen, &dp, 0, 0x0, cred, p, 0, 1);
2296 if (ret == NFSERR_DELAY)
2297 (void) nfs_catnap(PZERO, ret, "nfs_crt2");
2298 } while (ret == NFSERR_DELAY);
2301 free(dp, M_NFSCLDELEG);
2304 if (ret == NFSERR_STALECLIENTID ||
2305 ret == NFSERR_STALEDONTRECOVER ||
2306 ret == NFSERR_BADSESSION)
2310 nfscl_openrelease(nmp, op, error, newone);
2313 if (nd->nd_repstat != 0 && error == 0)
2314 error = nd->nd_repstat;
2315 if (error == NFSERR_STALECLIENTID)
2316 nfscl_initiate_recovery(owp->nfsow_clp);
2320 else if (dp != NULL)
2321 free(dp, M_NFSCLDELEG);
2322 mbuf_freem(nd->nd_mrep);
2330 nfsrpc_remove(vnode_t dvp, char *name, int namelen, vnode_t vp,
2331 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap, int *dattrflagp,
2335 struct nfsrv_descript nfsd, *nd = &nfsd;
2337 struct nfsmount *nmp;
2338 nfsv4stateid_t dstateid;
2339 int error, ret = 0, i;
2342 if (namelen > NFS_MAXNAMLEN)
2343 return (ENAMETOOLONG);
2344 nmp = VFSTONFS(vnode_mount(dvp));
2346 if (NFSHASNFSV4(nmp) && ret == 0) {
2347 ret = nfscl_removedeleg(vp, p, &dstateid);
2349 NFSCL_REQSTART(nd, NFSPROC_RETDELEGREMOVE, vp);
2350 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID +
2352 if (NFSHASNFSV4N(nmp))
2355 *tl++ = dstateid.seqid;
2356 *tl++ = dstateid.other[0];
2357 *tl++ = dstateid.other[1];
2358 *tl++ = dstateid.other[2];
2359 *tl = txdr_unsigned(NFSV4OP_PUTFH);
2361 (void) nfsm_fhtom(nd, np->n_fhp->nfh_fh,
2362 np->n_fhp->nfh_len, 0);
2363 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2364 *tl = txdr_unsigned(NFSV4OP_REMOVE);
2370 NFSCL_REQSTART(nd, NFSPROC_REMOVE, dvp);
2371 (void) nfsm_strtom(nd, name, namelen);
2372 error = nfscl_request(nd, dvp, p, cred, dstuff);
2375 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) {
2376 /* For NFSv4, parse out any Delereturn replies. */
2377 if (ret > 0 && nd->nd_repstat != 0 &&
2378 (nd->nd_flag & ND_NOMOREDATA)) {
2380 * If the Delegreturn failed, try again without
2381 * it. The server will Recall, as required.
2383 mbuf_freem(nd->nd_mrep);
2386 for (i = 0; i < (ret * 2); i++) {
2387 if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) ==
2389 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2391 nd->nd_flag |= ND_NOMOREDATA;
2394 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
2396 if (nd->nd_repstat && !error)
2397 error = nd->nd_repstat;
2399 mbuf_freem(nd->nd_mrep);
2404 * Do an nfs rename rpc.
2407 nfsrpc_rename(vnode_t fdvp, vnode_t fvp, char *fnameptr, int fnamelen,
2408 vnode_t tdvp, vnode_t tvp, char *tnameptr, int tnamelen, struct ucred *cred,
2409 NFSPROC_T *p, struct nfsvattr *fnap, struct nfsvattr *tnap,
2410 int *fattrflagp, int *tattrflagp, void *fstuff, void *tstuff)
2413 struct nfsrv_descript nfsd, *nd = &nfsd;
2414 struct nfsmount *nmp;
2416 nfsattrbit_t attrbits;
2417 nfsv4stateid_t fdstateid, tdstateid;
2418 int error = 0, ret = 0, gottd = 0, gotfd = 0, i;
2422 nmp = VFSTONFS(vnode_mount(fdvp));
2423 if (fnamelen > NFS_MAXNAMLEN || tnamelen > NFS_MAXNAMLEN)
2424 return (ENAMETOOLONG);
2426 if (NFSHASNFSV4(nmp) && ret == 0) {
2427 ret = nfscl_renamedeleg(fvp, &fdstateid, &gotfd, tvp,
2428 &tdstateid, &gottd, p);
2429 if (gotfd && gottd) {
2430 NFSCL_REQSTART(nd, NFSPROC_RETDELEGRENAME2, fvp);
2432 NFSCL_REQSTART(nd, NFSPROC_RETDELEGRENAME1, fvp);
2434 NFSCL_REQSTART(nd, NFSPROC_RETDELEGRENAME1, tvp);
2437 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID);
2438 if (NFSHASNFSV4N(nmp))
2441 *tl++ = fdstateid.seqid;
2442 *tl++ = fdstateid.other[0];
2443 *tl++ = fdstateid.other[1];
2444 *tl = fdstateid.other[2];
2446 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2447 *tl = txdr_unsigned(NFSV4OP_PUTFH);
2449 (void) nfsm_fhtom(nd, np->n_fhp->nfh_fh,
2450 np->n_fhp->nfh_len, 0);
2451 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2452 *tl = txdr_unsigned(NFSV4OP_DELEGRETURN);
2456 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID);
2457 if (NFSHASNFSV4N(nmp))
2460 *tl++ = tdstateid.seqid;
2461 *tl++ = tdstateid.other[0];
2462 *tl++ = tdstateid.other[1];
2463 *tl = tdstateid.other[2];
2466 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2467 *tl = txdr_unsigned(NFSV4OP_PUTFH);
2469 (void) nfsm_fhtom(nd, np->n_fhp->nfh_fh,
2470 np->n_fhp->nfh_len, 0);
2471 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2472 *tl = txdr_unsigned(NFSV4OP_SAVEFH);
2478 NFSCL_REQSTART(nd, NFSPROC_RENAME, fdvp);
2479 if (nd->nd_flag & ND_NFSV4) {
2480 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2481 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2482 NFSWCCATTR_ATTRBIT(&attrbits);
2483 (void) nfsrv_putattrbit(nd, &attrbits);
2484 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2485 *tl = txdr_unsigned(NFSV4OP_PUTFH);
2486 (void) nfsm_fhtom(nd, VTONFS(tdvp)->n_fhp->nfh_fh,
2487 VTONFS(tdvp)->n_fhp->nfh_len, 0);
2488 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2489 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2490 (void) nfsrv_putattrbit(nd, &attrbits);
2491 nd->nd_flag |= ND_V4WCCATTR;
2492 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2493 *tl = txdr_unsigned(NFSV4OP_RENAME);
2495 (void) nfsm_strtom(nd, fnameptr, fnamelen);
2496 if (!(nd->nd_flag & ND_NFSV4))
2497 (void) nfsm_fhtom(nd, VTONFS(tdvp)->n_fhp->nfh_fh,
2498 VTONFS(tdvp)->n_fhp->nfh_len, 0);
2499 (void) nfsm_strtom(nd, tnameptr, tnamelen);
2500 error = nfscl_request(nd, fdvp, p, cred, fstuff);
2503 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) {
2504 /* For NFSv4, parse out any Delereturn replies. */
2505 if (ret > 0 && nd->nd_repstat != 0 &&
2506 (nd->nd_flag & ND_NOMOREDATA)) {
2508 * If the Delegreturn failed, try again without
2509 * it. The server will Recall, as required.
2511 mbuf_freem(nd->nd_mrep);
2514 for (i = 0; i < (ret * 2); i++) {
2515 if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) ==
2517 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2519 if (i == 0 && ret > 1) {
2521 * If the Delegreturn failed, try again
2522 * without it. The server will Recall, as
2524 * If ret > 1, the first iteration of this
2525 * loop is the second DelegReturn result.
2527 mbuf_freem(nd->nd_mrep);
2530 nd->nd_flag |= ND_NOMOREDATA;
2535 /* Now, the first wcc attribute reply. */
2536 if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) == ND_NFSV4) {
2537 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2539 nd->nd_flag |= ND_NOMOREDATA;
2541 error = nfscl_wcc_data(nd, fdvp, fnap, fattrflagp, NULL,
2543 /* and the second wcc attribute reply. */
2544 if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) == ND_NFSV4 &&
2546 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2548 nd->nd_flag |= ND_NOMOREDATA;
2551 error = nfscl_wcc_data(nd, tdvp, tnap, tattrflagp,
2554 if (nd->nd_repstat && !error)
2555 error = nd->nd_repstat;
2557 mbuf_freem(nd->nd_mrep);
2562 * nfs hard link create rpc
2565 nfsrpc_link(vnode_t dvp, vnode_t vp, char *name, int namelen,
2566 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap,
2567 struct nfsvattr *nap, int *attrflagp, int *dattrflagp, void *dstuff)
2570 struct nfsrv_descript nfsd, *nd = &nfsd;
2571 nfsattrbit_t attrbits;
2576 if (namelen > NFS_MAXNAMLEN)
2577 return (ENAMETOOLONG);
2578 NFSCL_REQSTART(nd, NFSPROC_LINK, vp);
2579 if (nd->nd_flag & ND_NFSV4) {
2580 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2581 *tl = txdr_unsigned(NFSV4OP_PUTFH);
2583 (void) nfsm_fhtom(nd, VTONFS(dvp)->n_fhp->nfh_fh,
2584 VTONFS(dvp)->n_fhp->nfh_len, 0);
2585 if (nd->nd_flag & ND_NFSV4) {
2586 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2587 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2588 NFSWCCATTR_ATTRBIT(&attrbits);
2589 (void) nfsrv_putattrbit(nd, &attrbits);
2590 nd->nd_flag |= ND_V4WCCATTR;
2591 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2592 *tl = txdr_unsigned(NFSV4OP_LINK);
2594 (void) nfsm_strtom(nd, name, namelen);
2595 error = nfscl_request(nd, vp, p, cred, dstuff);
2598 if (nd->nd_flag & ND_NFSV3) {
2599 error = nfscl_postop_attr(nd, nap, attrflagp, dstuff);
2601 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp,
2603 } else if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) == ND_NFSV4) {
2605 * First, parse out the PutFH and Getattr result.
2607 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2609 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2611 nd->nd_flag |= ND_NOMOREDATA;
2613 * Get the pre-op attributes.
2615 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
2617 if (nd->nd_repstat && !error)
2618 error = nd->nd_repstat;
2620 mbuf_freem(nd->nd_mrep);
2625 * nfs symbolic link create rpc
2628 nfsrpc_symlink(vnode_t dvp, char *name, int namelen, char *target,
2629 struct vattr *vap, struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap,
2630 struct nfsvattr *nnap, struct nfsfh **nfhpp, int *attrflagp,
2631 int *dattrflagp, void *dstuff)
2634 struct nfsrv_descript nfsd, *nd = &nfsd;
2635 struct nfsmount *nmp;
2636 int slen, error = 0;
2641 nmp = VFSTONFS(vnode_mount(dvp));
2642 slen = strlen(target);
2643 if (slen > NFS_MAXPATHLEN || namelen > NFS_MAXNAMLEN)
2644 return (ENAMETOOLONG);
2645 NFSCL_REQSTART(nd, NFSPROC_SYMLINK, dvp);
2646 if (nd->nd_flag & ND_NFSV4) {
2647 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2648 *tl = txdr_unsigned(NFLNK);
2649 (void) nfsm_strtom(nd, target, slen);
2651 (void) nfsm_strtom(nd, name, namelen);
2652 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4))
2653 nfscl_fillsattr(nd, vap, dvp, 0, 0);
2654 if (!(nd->nd_flag & ND_NFSV4))
2655 (void) nfsm_strtom(nd, target, slen);
2656 if (nd->nd_flag & ND_NFSV2)
2657 nfscl_fillsattr(nd, vap, dvp, NFSSATTR_SIZENEG1, 0);
2658 error = nfscl_request(nd, dvp, p, cred, dstuff);
2661 if (nd->nd_flag & ND_NFSV4)
2662 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
2663 if ((nd->nd_flag & ND_NFSV3) && !error) {
2664 if (!nd->nd_repstat)
2665 error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp);
2667 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp,
2670 if (nd->nd_repstat && !error)
2671 error = nd->nd_repstat;
2672 mbuf_freem(nd->nd_mrep);
2674 * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry.
2675 * Only do this if vfs.nfs.ignore_eexist is set.
2676 * Never do this for NFSv4.1 or later minor versions, since sessions
2677 * should guarantee "exactly once" RPC semantics.
2679 if (error == EEXIST && nfsignore_eexist != 0 && (!NFSHASNFSV4(nmp) ||
2680 nmp->nm_minorvers == 0))
2689 nfsrpc_mkdir(vnode_t dvp, char *name, int namelen, struct vattr *vap,
2690 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap,
2691 struct nfsvattr *nnap, struct nfsfh **nfhpp, int *attrflagp,
2692 int *dattrflagp, void *dstuff)
2695 struct nfsrv_descript nfsd, *nd = &nfsd;
2696 nfsattrbit_t attrbits;
2699 struct nfsmount *nmp;
2704 nmp = VFSTONFS(vnode_mount(dvp));
2705 fhp = VTONFS(dvp)->n_fhp;
2706 if (namelen > NFS_MAXNAMLEN)
2707 return (ENAMETOOLONG);
2708 NFSCL_REQSTART(nd, NFSPROC_MKDIR, dvp);
2709 if (nd->nd_flag & ND_NFSV4) {
2710 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2711 *tl = txdr_unsigned(NFDIR);
2713 (void) nfsm_strtom(nd, name, namelen);
2714 nfscl_fillsattr(nd, vap, dvp, NFSSATTR_SIZENEG1, 0);
2715 if (nd->nd_flag & ND_NFSV4) {
2716 NFSGETATTR_ATTRBIT(&attrbits);
2717 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2718 *tl++ = txdr_unsigned(NFSV4OP_GETFH);
2719 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2720 (void) nfsrv_putattrbit(nd, &attrbits);
2721 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2722 *tl = txdr_unsigned(NFSV4OP_PUTFH);
2723 (void) nfsm_fhtom(nd, fhp->nfh_fh, fhp->nfh_len, 0);
2724 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2725 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2726 (void) nfsrv_putattrbit(nd, &attrbits);
2728 error = nfscl_request(nd, dvp, p, cred, dstuff);
2731 if (nd->nd_flag & ND_NFSV4)
2732 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
2733 if (!nd->nd_repstat && !error) {
2734 if (nd->nd_flag & ND_NFSV4) {
2735 NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
2736 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
2739 error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp);
2740 if (error == 0 && (nd->nd_flag & ND_NFSV4) != 0) {
2741 /* Get rid of the PutFH and Getattr status values. */
2742 NFSM_DISSECT(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
2743 /* Load the directory attributes. */
2744 error = nfsm_loadattr(nd, dnap);
2749 if ((nd->nd_flag & ND_NFSV3) && !error)
2750 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
2751 if (nd->nd_repstat && !error)
2752 error = nd->nd_repstat;
2754 mbuf_freem(nd->nd_mrep);
2756 * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry.
2757 * Only do this if vfs.nfs.ignore_eexist is set.
2758 * Never do this for NFSv4.1 or later minor versions, since sessions
2759 * should guarantee "exactly once" RPC semantics.
2761 if (error == EEXIST && nfsignore_eexist != 0 && (!NFSHASNFSV4(nmp) ||
2762 nmp->nm_minorvers == 0))
2768 * nfs remove directory call
2771 nfsrpc_rmdir(vnode_t dvp, char *name, int namelen, struct ucred *cred,
2772 NFSPROC_T *p, struct nfsvattr *dnap, int *dattrflagp, void *dstuff)
2774 struct nfsrv_descript nfsd, *nd = &nfsd;
2778 if (namelen > NFS_MAXNAMLEN)
2779 return (ENAMETOOLONG);
2780 NFSCL_REQSTART(nd, NFSPROC_RMDIR, dvp);
2781 (void) nfsm_strtom(nd, name, namelen);
2782 error = nfscl_request(nd, dvp, p, cred, dstuff);
2785 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4))
2786 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
2787 if (nd->nd_repstat && !error)
2788 error = nd->nd_repstat;
2789 mbuf_freem(nd->nd_mrep);
2791 * Kludge: Map ENOENT => 0 assuming that you have a reply to a retry.
2793 if (error == ENOENT)
2800 * Always returns with either uio_resid unchanged, if you are at the
2801 * end of the directory, or uio_resid == 0, with all DIRBLKSIZ chunks
2803 * I felt this would allow caching of directory blocks more easily
2804 * than returning a pertially filled block.
2805 * Directory offset cookies:
2806 * Oh my, what to do with them...
2807 * I can think of three ways to deal with them:
2808 * 1 - have the layer above these RPCs maintain a map between logical
2809 * directory byte offsets and the NFS directory offset cookies
2810 * 2 - pass the opaque directory offset cookies up into userland
2811 * and let the libc functions deal with them, via the system call
2812 * 3 - return them to userland in the "struct dirent", so future versions
2813 * of libc can use them and do whatever is necessary to make things work
2814 * above these rpc calls, in the meantime
2815 * For now, I do #3 by "hiding" the directory offset cookies after the
2816 * d_name field in struct dirent. This is space inside d_reclen that
2817 * will be ignored by anything that doesn't know about them.
2818 * The directory offset cookies are filled in as the last 8 bytes of
2819 * each directory entry, after d_name. Someday, the userland libc
2820 * functions may be able to use these. In the meantime, it satisfies
2821 * OpenBSD's requirements for cookies being returned.
2822 * If expects the directory offset cookie for the read to be in uio_offset
2823 * and returns the one for the next entry after this directory block in
2827 nfsrpc_readdir(vnode_t vp, struct uio *uiop, nfsuint64 *cookiep,
2828 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp,
2829 int *eofp, void *stuff)
2832 struct dirent *dp = NULL;
2834 nfsquad_t cookie, ncookie;
2835 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
2836 struct nfsnode *dnp = VTONFS(vp);
2837 struct nfsvattr nfsva;
2838 struct nfsrv_descript nfsd, *nd = &nfsd;
2839 int error = 0, tlen, more_dirs = 1, blksiz = 0, bigenough = 1;
2840 int reqsize, tryformoredirs = 1, readsize, eof = 0, gotmnton = 0;
2841 u_int64_t dotfileid, dotdotfileid = 0, fakefileno = UINT64_MAX;
2843 nfsattrbit_t attrbits, dattrbits;
2844 u_int32_t rderr, *tl2 = NULL;
2847 KASSERT(uiop->uio_iovcnt == 1 &&
2848 (uio_uio_resid(uiop) & (DIRBLKSIZ - 1)) == 0,
2849 ("nfs readdirrpc bad uio"));
2850 ncookie.lval[0] = ncookie.lval[1] = 0;
2852 * There is no point in reading a lot more than uio_resid, however
2853 * adding one additional DIRBLKSIZ makes sense. Since uio_resid
2854 * and nm_readdirsize are both exact multiples of DIRBLKSIZ, this
2855 * will never make readsize > nm_readdirsize.
2857 readsize = nmp->nm_readdirsize;
2858 if (readsize > uio_uio_resid(uiop))
2859 readsize = uio_uio_resid(uiop) + DIRBLKSIZ;
2864 tresid = uio_uio_resid(uiop);
2865 cookie.lval[0] = cookiep->nfsuquad[0];
2866 cookie.lval[1] = cookiep->nfsuquad[1];
2870 * For NFSv4, first create the "." and ".." entries.
2872 if (NFSHASNFSV4(nmp)) {
2873 reqsize = 6 * NFSX_UNSIGNED;
2874 NFSGETATTR_ATTRBIT(&dattrbits);
2875 NFSZERO_ATTRBIT(&attrbits);
2876 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_FILEID);
2877 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TYPE);
2878 if (NFSISSET_ATTRBIT(&dnp->n_vattr.na_suppattr,
2879 NFSATTRBIT_MOUNTEDONFILEID)) {
2880 NFSSETBIT_ATTRBIT(&attrbits,
2881 NFSATTRBIT_MOUNTEDONFILEID);
2885 * Must fake it. Use the fileno, except when the
2886 * fsid is != to that of the directory. For that
2887 * case, generate a fake fileno that is not the same.
2889 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_FSID);
2894 * Joy, oh joy. For V4 we get to hand craft '.' and '..'.
2896 if (uiop->uio_offset == 0) {
2897 NFSCL_REQSTART(nd, NFSPROC_LOOKUPP, vp);
2898 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2899 *tl++ = txdr_unsigned(NFSV4OP_GETFH);
2900 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2901 (void) nfsrv_putattrbit(nd, &attrbits);
2902 error = nfscl_request(nd, vp, p, cred, stuff);
2905 dotfileid = 0; /* Fake out the compiler. */
2906 if ((nd->nd_flag & ND_NOMOREDATA) == 0) {
2907 error = nfsm_loadattr(nd, &nfsva);
2910 dotfileid = nfsva.na_fileid;
2912 if (nd->nd_repstat == 0) {
2913 NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
2914 len = fxdr_unsigned(int, *(tl + 4));
2915 if (len > 0 && len <= NFSX_V4FHMAX)
2916 error = nfsm_advance(nd, NFSM_RNDUP(len), -1);
2920 NFSM_DISSECT(tl, u_int32_t *, 2*NFSX_UNSIGNED);
2921 nfsva.na_mntonfileno = UINT64_MAX;
2922 error = nfsv4_loadattr(nd, NULL, &nfsva, NULL,
2923 NULL, 0, NULL, NULL, NULL, NULL, NULL, 0,
2924 NULL, NULL, NULL, p, cred);
2926 dotdotfileid = dotfileid;
2927 } else if (gotmnton) {
2928 if (nfsva.na_mntonfileno != UINT64_MAX)
2929 dotdotfileid = nfsva.na_mntonfileno;
2931 dotdotfileid = nfsva.na_fileid;
2932 } else if (nfsva.na_filesid[0] ==
2933 dnp->n_vattr.na_filesid[0] &&
2934 nfsva.na_filesid[1] ==
2935 dnp->n_vattr.na_filesid[1]) {
2936 dotdotfileid = nfsva.na_fileid;
2940 } while (fakefileno ==
2942 dotdotfileid = fakefileno;
2945 } else if (nd->nd_repstat == NFSERR_NOENT) {
2947 * Lookupp returns NFSERR_NOENT when we are
2948 * at the root, so just use the current dir.
2951 dotdotfileid = dotfileid;
2953 error = nd->nd_repstat;
2955 mbuf_freem(nd->nd_mrep);
2959 dp = (struct dirent *)uio_iov_base(uiop);
2960 dp->d_pad0 = dp->d_pad1 = 0;
2962 dp->d_type = DT_DIR;
2963 dp->d_fileno = dotfileid;
2965 *((uint64_t *)dp->d_name) = 0; /* Zero pad it. */
2966 dp->d_name[0] = '.';
2967 dp->d_reclen = _GENERIC_DIRSIZ(dp) + NFSX_HYPER;
2969 * Just make these offset cookie 0.
2971 tl = (u_int32_t *)&dp->d_name[8];
2974 blksiz += dp->d_reclen;
2975 uio_uio_resid_add(uiop, -(dp->d_reclen));
2976 uiop->uio_offset += dp->d_reclen;
2977 uio_iov_base_add(uiop, dp->d_reclen);
2978 uio_iov_len_add(uiop, -(dp->d_reclen));
2979 dp = (struct dirent *)uio_iov_base(uiop);
2980 dp->d_pad0 = dp->d_pad1 = 0;
2982 dp->d_type = DT_DIR;
2983 dp->d_fileno = dotdotfileid;
2985 *((uint64_t *)dp->d_name) = 0;
2986 dp->d_name[0] = '.';
2987 dp->d_name[1] = '.';
2988 dp->d_reclen = _GENERIC_DIRSIZ(dp) + NFSX_HYPER;
2990 * Just make these offset cookie 0.
2992 tl = (u_int32_t *)&dp->d_name[8];
2995 blksiz += dp->d_reclen;
2996 uio_uio_resid_add(uiop, -(dp->d_reclen));
2997 uiop->uio_offset += dp->d_reclen;
2998 uio_iov_base_add(uiop, dp->d_reclen);
2999 uio_iov_len_add(uiop, -(dp->d_reclen));
3001 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_RDATTRERROR);
3003 reqsize = 5 * NFSX_UNSIGNED;
3008 * Loop around doing readdir rpc's of size readsize.
3009 * The stopping criteria is EOF or buffer full.
3011 while (more_dirs && bigenough) {
3013 NFSCL_REQSTART(nd, NFSPROC_READDIR, vp);
3014 if (nd->nd_flag & ND_NFSV2) {
3015 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
3016 *tl++ = cookie.lval[1];
3017 *tl = txdr_unsigned(readsize);
3019 NFSM_BUILD(tl, u_int32_t *, reqsize);
3020 *tl++ = cookie.lval[0];
3021 *tl++ = cookie.lval[1];
3022 if (cookie.qval == 0) {
3027 *tl++ = dnp->n_cookieverf.nfsuquad[0];
3028 *tl++ = dnp->n_cookieverf.nfsuquad[1];
3031 if (nd->nd_flag & ND_NFSV4) {
3032 *tl++ = txdr_unsigned(readsize);
3033 *tl = txdr_unsigned(readsize);
3034 (void) nfsrv_putattrbit(nd, &attrbits);
3035 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
3036 *tl = txdr_unsigned(NFSV4OP_GETATTR);
3037 (void) nfsrv_putattrbit(nd, &dattrbits);
3039 *tl = txdr_unsigned(readsize);
3042 error = nfscl_request(nd, vp, p, cred, stuff);
3045 if (!(nd->nd_flag & ND_NFSV2)) {
3046 if (nd->nd_flag & ND_NFSV3)
3047 error = nfscl_postop_attr(nd, nap, attrflagp,
3049 if (!nd->nd_repstat && !error) {
3050 NFSM_DISSECT(tl, u_int32_t *, NFSX_HYPER);
3052 dnp->n_cookieverf.nfsuquad[0] = *tl++;
3053 dnp->n_cookieverf.nfsuquad[1] = *tl;
3057 if (nd->nd_repstat || error) {
3059 error = nd->nd_repstat;
3062 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3063 more_dirs = fxdr_unsigned(int, *tl);
3067 /* loop through the dir entries, doctoring them to 4bsd form */
3068 while (more_dirs && bigenough) {
3069 if (nd->nd_flag & ND_NFSV4) {
3070 NFSM_DISSECT(tl, u_int32_t *, 3*NFSX_UNSIGNED);
3071 ncookie.lval[0] = *tl++;
3072 ncookie.lval[1] = *tl++;
3073 len = fxdr_unsigned(int, *tl);
3074 } else if (nd->nd_flag & ND_NFSV3) {
3075 NFSM_DISSECT(tl, u_int32_t *, 3*NFSX_UNSIGNED);
3076 nfsva.na_fileid = fxdr_hyper(tl);
3078 len = fxdr_unsigned(int, *tl);
3080 NFSM_DISSECT(tl, u_int32_t *, 2*NFSX_UNSIGNED);
3081 nfsva.na_fileid = fxdr_unsigned(uint64_t,
3083 len = fxdr_unsigned(int, *tl);
3085 if (len <= 0 || len > NFS_MAXNAMLEN) {
3089 tlen = roundup2(len, 8);
3091 tlen += 8; /* To ensure null termination. */
3092 left = DIRBLKSIZ - blksiz;
3093 if (_GENERIC_DIRLEN(len) + NFSX_HYPER > left) {
3094 NFSBZERO(uio_iov_base(uiop), left);
3095 dp->d_reclen += left;
3096 uio_iov_base_add(uiop, left);
3097 uio_iov_len_add(uiop, -(left));
3098 uio_uio_resid_add(uiop, -(left));
3099 uiop->uio_offset += left;
3102 if (_GENERIC_DIRLEN(len) + NFSX_HYPER >
3103 uio_uio_resid(uiop))
3106 dp = (struct dirent *)uio_iov_base(uiop);
3107 dp->d_pad0 = dp->d_pad1 = 0;
3110 dp->d_reclen = _GENERIC_DIRLEN(len) +
3112 dp->d_type = DT_UNKNOWN;
3113 blksiz += dp->d_reclen;
3114 if (blksiz == DIRBLKSIZ)
3116 uio_uio_resid_add(uiop, -(DIRHDSIZ));
3117 uiop->uio_offset += DIRHDSIZ;
3118 uio_iov_base_add(uiop, DIRHDSIZ);
3119 uio_iov_len_add(uiop, -(DIRHDSIZ));
3120 error = nfsm_mbufuio(nd, uiop, len);
3123 cp = uio_iov_base(uiop);
3126 cp += tlen; /* points to cookie storage */
3127 tl2 = (u_int32_t *)cp;
3128 uio_iov_base_add(uiop, (tlen + NFSX_HYPER));
3129 uio_iov_len_add(uiop, -(tlen + NFSX_HYPER));
3130 uio_uio_resid_add(uiop, -(tlen + NFSX_HYPER));
3131 uiop->uio_offset += (tlen + NFSX_HYPER);
3133 error = nfsm_advance(nd, NFSM_RNDUP(len), -1);
3137 if (nd->nd_flag & ND_NFSV4) {
3139 nfsva.na_mntonfileno = UINT64_MAX;
3140 error = nfsv4_loadattr(nd, NULL, &nfsva, NULL,
3141 NULL, 0, NULL, NULL, NULL, NULL, NULL, 0,
3142 NULL, NULL, &rderr, p, cred);
3145 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3146 } else if (nd->nd_flag & ND_NFSV3) {
3147 NFSM_DISSECT(tl, u_int32_t *, 3*NFSX_UNSIGNED);
3148 ncookie.lval[0] = *tl++;
3149 ncookie.lval[1] = *tl++;
3151 NFSM_DISSECT(tl, u_int32_t *, 2*NFSX_UNSIGNED);
3152 ncookie.lval[0] = 0;
3153 ncookie.lval[1] = *tl++;
3156 if (nd->nd_flag & ND_NFSV4) {
3161 if (nfsva.na_mntonfileno != UINT64_MAX)
3162 dp->d_fileno = nfsva.na_mntonfileno;
3164 dp->d_fileno = nfsva.na_fileid;
3165 } else if (nfsva.na_filesid[0] ==
3166 dnp->n_vattr.na_filesid[0] &&
3167 nfsva.na_filesid[1] ==
3168 dnp->n_vattr.na_filesid[1]) {
3169 dp->d_fileno = nfsva.na_fileid;
3173 } while (fakefileno ==
3175 dp->d_fileno = fakefileno;
3177 dp->d_type = vtonfs_dtype(nfsva.na_type);
3180 dp->d_fileno = nfsva.na_fileid;
3182 *tl2++ = cookiep->nfsuquad[0] = cookie.lval[0] =
3184 *tl2 = cookiep->nfsuquad[1] = cookie.lval[1] =
3187 more_dirs = fxdr_unsigned(int, *tl);
3190 * If at end of rpc data, get the eof boolean
3193 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3194 eof = fxdr_unsigned(int, *tl);
3197 if (nd->nd_flag & ND_NFSV4) {
3198 error = nfscl_postop_attr(nd, nap, attrflagp,
3204 mbuf_freem(nd->nd_mrep);
3208 * Fill last record, iff any, out to a multiple of DIRBLKSIZ
3209 * by increasing d_reclen for the last record.
3212 left = DIRBLKSIZ - blksiz;
3213 NFSBZERO(uio_iov_base(uiop), left);
3214 dp->d_reclen += left;
3215 uio_iov_base_add(uiop, left);
3216 uio_iov_len_add(uiop, -(left));
3217 uio_uio_resid_add(uiop, -(left));
3218 uiop->uio_offset += left;
3222 * If returning no data, assume end of file.
3223 * If not bigenough, return not end of file, since you aren't
3224 * returning all the data
3225 * Otherwise, return the eof flag from the server.
3228 if (tresid == ((size_t)(uio_uio_resid(uiop))))
3230 else if (!bigenough)
3237 * Add extra empty records to any remaining DIRBLKSIZ chunks.
3239 while (uio_uio_resid(uiop) > 0 && uio_uio_resid(uiop) != tresid) {
3240 dp = (struct dirent *)uio_iov_base(uiop);
3241 NFSBZERO(dp, DIRBLKSIZ);
3242 dp->d_type = DT_UNKNOWN;
3243 tl = (u_int32_t *)&dp->d_name[4];
3244 *tl++ = cookie.lval[0];
3245 *tl = cookie.lval[1];
3246 dp->d_reclen = DIRBLKSIZ;
3247 uio_iov_base_add(uiop, DIRBLKSIZ);
3248 uio_iov_len_add(uiop, -(DIRBLKSIZ));
3249 uio_uio_resid_add(uiop, -(DIRBLKSIZ));
3250 uiop->uio_offset += DIRBLKSIZ;
3254 if (nd->nd_mrep != NULL)
3255 mbuf_freem(nd->nd_mrep);
3261 * NFS V3 readdir plus RPC. Used in place of nfsrpc_readdir().
3262 * (Also used for NFS V4 when mount flag set.)
3263 * (ditto above w.r.t. multiple of DIRBLKSIZ, etc.)
3266 nfsrpc_readdirplus(vnode_t vp, struct uio *uiop, nfsuint64 *cookiep,
3267 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp,
3268 int *eofp, void *stuff)
3271 struct dirent *dp = NULL;
3273 vnode_t newvp = NULLVP;
3274 struct nfsrv_descript nfsd, *nd = &nfsd;
3275 struct nameidata nami, *ndp = &nami;
3276 struct componentname *cnp = &ndp->ni_cnd;
3277 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
3278 struct nfsnode *dnp = VTONFS(vp), *np;
3279 struct nfsvattr nfsva;
3281 nfsquad_t cookie, ncookie;
3282 int error = 0, tlen, more_dirs = 1, blksiz = 0, bigenough = 1;
3283 int attrflag, tryformoredirs = 1, eof = 0, gotmnton = 0;
3284 int isdotdot = 0, unlocknewvp = 0;
3285 u_int64_t dotfileid, dotdotfileid = 0, fakefileno = UINT64_MAX;
3286 u_int64_t fileno = 0;
3288 nfsattrbit_t attrbits, dattrbits;
3290 u_int32_t *tl2 = NULL, rderr;
3291 struct timespec dctime;
3293 KASSERT(uiop->uio_iovcnt == 1 &&
3294 (uio_uio_resid(uiop) & (DIRBLKSIZ - 1)) == 0,
3295 ("nfs readdirplusrpc bad uio"));
3296 ncookie.lval[0] = ncookie.lval[1] = 0;
3297 timespecclear(&dctime);
3303 cookie.lval[0] = cookiep->nfsuquad[0];
3304 cookie.lval[1] = cookiep->nfsuquad[1];
3305 tresid = uio_uio_resid(uiop);
3308 * For NFSv4, first create the "." and ".." entries.
3310 if (NFSHASNFSV4(nmp)) {
3311 NFSGETATTR_ATTRBIT(&dattrbits);
3312 NFSZERO_ATTRBIT(&attrbits);
3313 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_FILEID);
3314 if (NFSISSET_ATTRBIT(&dnp->n_vattr.na_suppattr,
3315 NFSATTRBIT_MOUNTEDONFILEID)) {
3316 NFSSETBIT_ATTRBIT(&attrbits,
3317 NFSATTRBIT_MOUNTEDONFILEID);
3321 * Must fake it. Use the fileno, except when the
3322 * fsid is != to that of the directory. For that
3323 * case, generate a fake fileno that is not the same.
3325 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_FSID);
3330 * Joy, oh joy. For V4 we get to hand craft '.' and '..'.
3332 if (uiop->uio_offset == 0) {
3333 NFSCL_REQSTART(nd, NFSPROC_LOOKUPP, vp);
3334 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
3335 *tl++ = txdr_unsigned(NFSV4OP_GETFH);
3336 *tl = txdr_unsigned(NFSV4OP_GETATTR);
3337 (void) nfsrv_putattrbit(nd, &attrbits);
3338 error = nfscl_request(nd, vp, p, cred, stuff);
3341 dotfileid = 0; /* Fake out the compiler. */
3342 if ((nd->nd_flag & ND_NOMOREDATA) == 0) {
3343 error = nfsm_loadattr(nd, &nfsva);
3346 dctime = nfsva.na_ctime;
3347 dotfileid = nfsva.na_fileid;
3349 if (nd->nd_repstat == 0) {
3350 NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
3351 len = fxdr_unsigned(int, *(tl + 4));
3352 if (len > 0 && len <= NFSX_V4FHMAX)
3353 error = nfsm_advance(nd, NFSM_RNDUP(len), -1);
3357 NFSM_DISSECT(tl, u_int32_t *, 2*NFSX_UNSIGNED);
3358 nfsva.na_mntonfileno = UINT64_MAX;
3359 error = nfsv4_loadattr(nd, NULL, &nfsva, NULL,
3360 NULL, 0, NULL, NULL, NULL, NULL, NULL, 0,
3361 NULL, NULL, NULL, p, cred);
3363 dotdotfileid = dotfileid;
3364 } else if (gotmnton) {
3365 if (nfsva.na_mntonfileno != UINT64_MAX)
3366 dotdotfileid = nfsva.na_mntonfileno;
3368 dotdotfileid = nfsva.na_fileid;
3369 } else if (nfsva.na_filesid[0] ==
3370 dnp->n_vattr.na_filesid[0] &&
3371 nfsva.na_filesid[1] ==
3372 dnp->n_vattr.na_filesid[1]) {
3373 dotdotfileid = nfsva.na_fileid;
3377 } while (fakefileno ==
3379 dotdotfileid = fakefileno;
3382 } else if (nd->nd_repstat == NFSERR_NOENT) {
3384 * Lookupp returns NFSERR_NOENT when we are
3385 * at the root, so just use the current dir.
3388 dotdotfileid = dotfileid;
3390 error = nd->nd_repstat;
3392 mbuf_freem(nd->nd_mrep);
3396 dp = (struct dirent *)uio_iov_base(uiop);
3397 dp->d_pad0 = dp->d_pad1 = 0;
3399 dp->d_type = DT_DIR;
3400 dp->d_fileno = dotfileid;
3402 *((uint64_t *)dp->d_name) = 0; /* Zero pad it. */
3403 dp->d_name[0] = '.';
3404 dp->d_reclen = _GENERIC_DIRSIZ(dp) + NFSX_HYPER;
3406 * Just make these offset cookie 0.
3408 tl = (u_int32_t *)&dp->d_name[8];
3411 blksiz += dp->d_reclen;
3412 uio_uio_resid_add(uiop, -(dp->d_reclen));
3413 uiop->uio_offset += dp->d_reclen;
3414 uio_iov_base_add(uiop, dp->d_reclen);
3415 uio_iov_len_add(uiop, -(dp->d_reclen));
3416 dp = (struct dirent *)uio_iov_base(uiop);
3417 dp->d_pad0 = dp->d_pad1 = 0;
3419 dp->d_type = DT_DIR;
3420 dp->d_fileno = dotdotfileid;
3422 *((uint64_t *)dp->d_name) = 0;
3423 dp->d_name[0] = '.';
3424 dp->d_name[1] = '.';
3425 dp->d_reclen = _GENERIC_DIRSIZ(dp) + NFSX_HYPER;
3427 * Just make these offset cookie 0.
3429 tl = (u_int32_t *)&dp->d_name[8];
3432 blksiz += dp->d_reclen;
3433 uio_uio_resid_add(uiop, -(dp->d_reclen));
3434 uiop->uio_offset += dp->d_reclen;
3435 uio_iov_base_add(uiop, dp->d_reclen);
3436 uio_iov_len_add(uiop, -(dp->d_reclen));
3438 NFSREADDIRPLUS_ATTRBIT(&attrbits);
3440 NFSSETBIT_ATTRBIT(&attrbits,
3441 NFSATTRBIT_MOUNTEDONFILEID);
3445 * Loop around doing readdir rpc's of size nm_readdirsize.
3446 * The stopping criteria is EOF or buffer full.
3448 while (more_dirs && bigenough) {
3450 NFSCL_REQSTART(nd, NFSPROC_READDIRPLUS, vp);
3451 NFSM_BUILD(tl, u_int32_t *, 6 * NFSX_UNSIGNED);
3452 *tl++ = cookie.lval[0];
3453 *tl++ = cookie.lval[1];
3454 if (cookie.qval == 0) {
3459 *tl++ = dnp->n_cookieverf.nfsuquad[0];
3460 *tl++ = dnp->n_cookieverf.nfsuquad[1];
3463 *tl++ = txdr_unsigned(nmp->nm_readdirsize);
3464 *tl = txdr_unsigned(nmp->nm_readdirsize);
3465 if (nd->nd_flag & ND_NFSV4) {
3466 (void) nfsrv_putattrbit(nd, &attrbits);
3467 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
3468 *tl = txdr_unsigned(NFSV4OP_GETATTR);
3469 (void) nfsrv_putattrbit(nd, &dattrbits);
3471 error = nfscl_request(nd, vp, p, cred, stuff);
3474 if (nd->nd_flag & ND_NFSV3)
3475 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
3476 if (nd->nd_repstat || error) {
3478 error = nd->nd_repstat;
3481 if ((nd->nd_flag & ND_NFSV3) != 0 && *attrflagp != 0)
3482 dctime = nap->na_ctime;
3483 NFSM_DISSECT(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
3485 dnp->n_cookieverf.nfsuquad[0] = *tl++;
3486 dnp->n_cookieverf.nfsuquad[1] = *tl++;
3488 more_dirs = fxdr_unsigned(int, *tl);
3492 /* loop through the dir entries, doctoring them to 4bsd form */
3493 while (more_dirs && bigenough) {
3494 NFSM_DISSECT(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
3495 if (nd->nd_flag & ND_NFSV4) {
3496 ncookie.lval[0] = *tl++;
3497 ncookie.lval[1] = *tl++;
3499 fileno = fxdr_hyper(tl);
3502 len = fxdr_unsigned(int, *tl);
3503 if (len <= 0 || len > NFS_MAXNAMLEN) {
3507 tlen = roundup2(len, 8);
3509 tlen += 8; /* To ensure null termination. */
3510 left = DIRBLKSIZ - blksiz;
3511 if (_GENERIC_DIRLEN(len) + NFSX_HYPER > left) {
3512 NFSBZERO(uio_iov_base(uiop), left);
3513 dp->d_reclen += left;
3514 uio_iov_base_add(uiop, left);
3515 uio_iov_len_add(uiop, -(left));
3516 uio_uio_resid_add(uiop, -(left));
3517 uiop->uio_offset += left;
3520 if (_GENERIC_DIRLEN(len) + NFSX_HYPER >
3521 uio_uio_resid(uiop))
3524 dp = (struct dirent *)uio_iov_base(uiop);
3525 dp->d_pad0 = dp->d_pad1 = 0;
3528 dp->d_reclen = _GENERIC_DIRLEN(len) +
3530 dp->d_type = DT_UNKNOWN;
3531 blksiz += dp->d_reclen;
3532 if (blksiz == DIRBLKSIZ)
3534 uio_uio_resid_add(uiop, -(DIRHDSIZ));
3535 uiop->uio_offset += DIRHDSIZ;
3536 uio_iov_base_add(uiop, DIRHDSIZ);
3537 uio_iov_len_add(uiop, -(DIRHDSIZ));
3538 cnp->cn_nameptr = uio_iov_base(uiop);
3539 cnp->cn_namelen = len;
3541 error = nfsm_mbufuio(nd, uiop, len);
3544 cp = uio_iov_base(uiop);
3547 cp += tlen; /* points to cookie storage */
3548 tl2 = (u_int32_t *)cp;
3549 if (len == 2 && cnp->cn_nameptr[0] == '.' &&
3550 cnp->cn_nameptr[1] == '.')
3554 uio_iov_base_add(uiop, (tlen + NFSX_HYPER));
3555 uio_iov_len_add(uiop, -(tlen + NFSX_HYPER));
3556 uio_uio_resid_add(uiop, -(tlen + NFSX_HYPER));
3557 uiop->uio_offset += (tlen + NFSX_HYPER);
3559 error = nfsm_advance(nd, NFSM_RNDUP(len), -1);
3564 if (nd->nd_flag & ND_NFSV3) {
3565 NFSM_DISSECT(tl, u_int32_t *, 3*NFSX_UNSIGNED);
3566 ncookie.lval[0] = *tl++;
3567 ncookie.lval[1] = *tl++;
3568 attrflag = fxdr_unsigned(int, *tl);
3570 error = nfsm_loadattr(nd, &nfsva);
3574 NFSM_DISSECT(tl,u_int32_t *,NFSX_UNSIGNED);
3576 error = nfsm_getfh(nd, &nfhp);
3580 if (!attrflag && nfhp != NULL) {
3581 free(nfhp, M_NFSFH);
3586 nfsva.na_mntonfileno = 0xffffffff;
3587 error = nfsv4_loadattr(nd, NULL, &nfsva, &nfhp,
3588 NULL, 0, NULL, NULL, NULL, NULL, NULL, 0,
3589 NULL, NULL, &rderr, p, cred);
3595 if (nd->nd_flag & ND_NFSV4) {
3598 } else if (gotmnton) {
3599 if (nfsva.na_mntonfileno != 0xffffffff)
3600 dp->d_fileno = nfsva.na_mntonfileno;
3602 dp->d_fileno = nfsva.na_fileid;
3603 } else if (nfsva.na_filesid[0] ==
3604 dnp->n_vattr.na_filesid[0] &&
3605 nfsva.na_filesid[1] ==
3606 dnp->n_vattr.na_filesid[1]) {
3607 dp->d_fileno = nfsva.na_fileid;
3611 } while (fakefileno ==
3613 dp->d_fileno = fakefileno;
3616 dp->d_fileno = fileno;
3618 *tl2++ = cookiep->nfsuquad[0] = cookie.lval[0] =
3620 *tl2 = cookiep->nfsuquad[1] = cookie.lval[1] =
3624 if (NFSRV_CMPFH(nfhp->nfh_fh, nfhp->nfh_len,
3625 dnp->n_fhp->nfh_fh, dnp->n_fhp->nfh_len)) {
3629 free(nfhp, M_NFSFH);
3631 } else if (isdotdot != 0) {
3633 * Skip doing a nfscl_nget() call for "..".
3634 * There's a race between acquiring the nfs
3635 * node here and lookups that look for the
3636 * directory being read (in the parent).
3637 * It would try to get a lock on ".." here,
3638 * owning the lock on the directory being
3639 * read. Lookup will hold the lock on ".."
3640 * and try to acquire the lock on the
3641 * directory being read.
3642 * If the directory is unlocked/relocked,
3643 * then there is a LOR with the buflock
3646 free(nfhp, M_NFSFH);
3648 error = nfscl_nget(vnode_mount(vp), vp,
3649 nfhp, cnp, p, &np, NULL, LK_EXCLUSIVE);
3656 if (newvp != NULLVP) {
3657 error = nfscl_loadattrcache(&newvp,
3658 &nfsva, NULL, NULL, 0, 0);
3667 vtonfs_dtype(np->n_vattr.na_type);
3669 NFSCNHASH(cnp, HASHINIT);
3670 if (cnp->cn_namelen <= NCHNAMLEN &&
3671 (newvp->v_type != VDIR ||
3672 dctime.tv_sec != 0)) {
3673 cache_enter_time(ndp->ni_dvp,
3676 newvp->v_type != VDIR ? NULL :
3686 } else if (nfhp != NULL) {
3687 free(nfhp, M_NFSFH);
3689 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3690 more_dirs = fxdr_unsigned(int, *tl);
3693 * If at end of rpc data, get the eof boolean
3696 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3697 eof = fxdr_unsigned(int, *tl);
3700 if (nd->nd_flag & ND_NFSV4) {
3701 error = nfscl_postop_attr(nd, nap, attrflagp,
3707 mbuf_freem(nd->nd_mrep);
3711 * Fill last record, iff any, out to a multiple of DIRBLKSIZ
3712 * by increasing d_reclen for the last record.
3715 left = DIRBLKSIZ - blksiz;
3716 NFSBZERO(uio_iov_base(uiop), left);
3717 dp->d_reclen += left;
3718 uio_iov_base_add(uiop, left);
3719 uio_iov_len_add(uiop, -(left));
3720 uio_uio_resid_add(uiop, -(left));
3721 uiop->uio_offset += left;
3725 * If returning no data, assume end of file.
3726 * If not bigenough, return not end of file, since you aren't
3727 * returning all the data
3728 * Otherwise, return the eof flag from the server.
3731 if (tresid == uio_uio_resid(uiop))
3733 else if (!bigenough)
3740 * Add extra empty records to any remaining DIRBLKSIZ chunks.
3742 while (uio_uio_resid(uiop) > 0 && uio_uio_resid(uiop) != tresid) {
3743 dp = (struct dirent *)uio_iov_base(uiop);
3744 NFSBZERO(dp, DIRBLKSIZ);
3745 dp->d_type = DT_UNKNOWN;
3746 tl = (u_int32_t *)&dp->d_name[4];
3747 *tl++ = cookie.lval[0];
3748 *tl = cookie.lval[1];
3749 dp->d_reclen = DIRBLKSIZ;
3750 uio_iov_base_add(uiop, DIRBLKSIZ);
3751 uio_iov_len_add(uiop, -(DIRBLKSIZ));
3752 uio_uio_resid_add(uiop, -(DIRBLKSIZ));
3753 uiop->uio_offset += DIRBLKSIZ;
3757 if (nd->nd_mrep != NULL)
3758 mbuf_freem(nd->nd_mrep);
3767 nfsrpc_commit(vnode_t vp, u_quad_t offset, int cnt, struct ucred *cred,
3768 NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, void *stuff)
3771 struct nfsrv_descript nfsd, *nd = &nfsd;
3772 nfsattrbit_t attrbits;
3774 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
3777 NFSCL_REQSTART(nd, NFSPROC_COMMIT, vp);
3778 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
3779 txdr_hyper(offset, tl);
3781 *tl = txdr_unsigned(cnt);
3782 if (nd->nd_flag & ND_NFSV4) {
3784 * And do a Getattr op.
3786 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
3787 *tl = txdr_unsigned(NFSV4OP_GETATTR);
3788 NFSGETATTR_ATTRBIT(&attrbits);
3789 (void) nfsrv_putattrbit(nd, &attrbits);
3791 error = nfscl_request(nd, vp, p, cred, stuff);
3794 error = nfscl_wcc_data(nd, vp, nap, attrflagp, NULL, stuff);
3795 if (!error && !nd->nd_repstat) {
3796 NFSM_DISSECT(tl, u_int32_t *, NFSX_VERF);
3798 if (NFSBCMP(nmp->nm_verf, tl, NFSX_VERF)) {
3799 NFSBCOPY(tl, nmp->nm_verf, NFSX_VERF);
3800 nd->nd_repstat = NFSERR_STALEWRITEVERF;
3803 if (nd->nd_flag & ND_NFSV4)
3804 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
3807 if (!error && nd->nd_repstat)
3808 error = nd->nd_repstat;
3809 mbuf_freem(nd->nd_mrep);
3814 * NFS byte range lock rpc.
3815 * (Mostly just calls one of the three lower level RPC routines.)
3818 nfsrpc_advlock(vnode_t vp, off_t size, int op, struct flock *fl,
3819 int reclaim, struct ucred *cred, NFSPROC_T *p, void *id, int flags)
3821 struct nfscllockowner *lp;
3822 struct nfsclclient *clp;
3824 struct nfsrv_descript nfsd, *nd = &nfsd;
3825 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
3828 u_int32_t clidrev = 0;
3829 int error = 0, newone = 0, expireret = 0, retrycnt, donelocally;
3833 * Convert the flock structure into a start and end and do POSIX
3836 switch (fl->l_whence) {
3840 * Caller is responsible for adding any necessary offset
3841 * when SEEK_CUR is used.
3843 start = fl->l_start;
3847 start = size + fl->l_start;
3848 off = size + fl->l_start;
3855 if (fl->l_len != 0) {
3856 end = start + fl->l_len - 1;
3867 if (op == F_GETLK) {
3868 error = nfscl_getcl(vnode_mount(vp), cred, p, 1, &clp);
3871 error = nfscl_lockt(vp, clp, off, len, fl, p, id, flags);
3873 clidrev = clp->nfsc_clientidrev;
3874 error = nfsrpc_lockt(nd, vp, clp, off, len, fl, cred,
3876 } else if (error == -1) {
3879 nfscl_clientrelease(clp);
3880 } else if (op == F_UNLCK && fl->l_type == F_UNLCK) {
3882 * We must loop around for all lockowner cases.
3885 error = nfscl_getcl(vnode_mount(vp), cred, p, 1, &clp);
3889 error = nfscl_relbytelock(vp, off, len, cred, p, callcnt,
3890 clp, id, flags, &lp, &dorpc);
3892 * If it returns a NULL lp, we're done.
3896 nfscl_clientrelease(clp);
3898 nfscl_releasealllocks(clp, vp, p, id, flags);
3901 if (nmp->nm_clp != NULL)
3902 clidrev = nmp->nm_clp->nfsc_clientidrev;
3906 * If the server doesn't support Posix lock semantics,
3907 * only allow locks on the entire file, since it won't
3908 * handle overlapping byte ranges.
3909 * There might still be a problem when a lock
3910 * upgrade/downgrade (read<->write) occurs, since the
3911 * server "might" expect an unlock first?
3913 if (dorpc && (lp->nfsl_open->nfso_posixlock ||
3914 (off == 0 && len == NFS64BITSSET))) {
3916 * Since the lock records will go away, we must
3917 * wait for grace and delay here.
3920 error = nfsrpc_locku(nd, nmp, lp, off, len,
3921 NFSV4LOCKT_READ, cred, p, 0);
3922 if ((nd->nd_repstat == NFSERR_GRACE ||
3923 nd->nd_repstat == NFSERR_DELAY) &&
3925 (void) nfs_catnap(PZERO, (int)nd->nd_repstat,
3927 } while ((nd->nd_repstat == NFSERR_GRACE ||
3928 nd->nd_repstat == NFSERR_DELAY) && error == 0);
3931 } while (error == 0 && nd->nd_repstat == 0);
3932 nfscl_releasealllocks(clp, vp, p, id, flags);
3933 } else if (op == F_SETLK) {
3934 error = nfscl_getbytelock(vp, off, len, fl->l_type, cred, p,
3935 NULL, 0, id, flags, NULL, NULL, &lp, &newone, &donelocally);
3936 if (error || donelocally) {
3939 if (nmp->nm_clp != NULL)
3940 clidrev = nmp->nm_clp->nfsc_clientidrev;
3943 nfhp = VTONFS(vp)->n_fhp;
3944 if (!lp->nfsl_open->nfso_posixlock &&
3945 (off != 0 || len != NFS64BITSSET)) {
3948 error = nfsrpc_lock(nd, nmp, vp, nfhp->nfh_fh,
3949 nfhp->nfh_len, lp, newone, reclaim, off,
3950 len, fl->l_type, cred, p, 0);
3953 error = nd->nd_repstat;
3954 nfscl_lockrelease(lp, error, newone);
3959 error = nd->nd_repstat;
3960 if (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
3961 error == NFSERR_STALEDONTRECOVER ||
3962 error == NFSERR_STALECLIENTID || error == NFSERR_DELAY ||
3963 error == NFSERR_BADSESSION) {
3964 (void) nfs_catnap(PZERO, error, "nfs_advlock");
3965 } else if ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID)
3967 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
3970 } while (error == NFSERR_GRACE ||
3971 error == NFSERR_STALECLIENTID || error == NFSERR_DELAY ||
3972 error == NFSERR_STALEDONTRECOVER || error == NFSERR_STALESTATEID ||
3973 error == NFSERR_BADSESSION ||
3974 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
3975 expireret == 0 && clidrev != 0 && retrycnt < 4));
3976 if (error && retrycnt >= 4)
3982 * The lower level routine for the LockT case.
3985 nfsrpc_lockt(struct nfsrv_descript *nd, vnode_t vp,
3986 struct nfsclclient *clp, u_int64_t off, u_int64_t len, struct flock *fl,
3987 struct ucred *cred, NFSPROC_T *p, void *id, int flags)
3990 int error, type, size;
3991 uint8_t own[NFSV4CL_LOCKNAMELEN + NFSX_V4FHMAX];
3993 struct nfsmount *nmp;
3994 struct nfsclsession *tsep;
3996 nmp = VFSTONFS(vp->v_mount);
3997 NFSCL_REQSTART(nd, NFSPROC_LOCKT, vp);
3998 NFSM_BUILD(tl, u_int32_t *, 7 * NFSX_UNSIGNED);
3999 if (fl->l_type == F_RDLCK)
4000 *tl++ = txdr_unsigned(NFSV4LOCKT_READ);
4002 *tl++ = txdr_unsigned(NFSV4LOCKT_WRITE);
4003 txdr_hyper(off, tl);
4005 txdr_hyper(len, tl);
4007 tsep = nfsmnt_mdssession(nmp);
4008 *tl++ = tsep->nfsess_clientid.lval[0];
4009 *tl = tsep->nfsess_clientid.lval[1];
4010 nfscl_filllockowner(id, own, flags);
4012 NFSBCOPY(np->n_fhp->nfh_fh, &own[NFSV4CL_LOCKNAMELEN],
4013 np->n_fhp->nfh_len);
4014 (void)nfsm_strtom(nd, own, NFSV4CL_LOCKNAMELEN + np->n_fhp->nfh_len);
4015 error = nfscl_request(nd, vp, p, cred, NULL);
4018 if (nd->nd_repstat == 0) {
4019 fl->l_type = F_UNLCK;
4020 } else if (nd->nd_repstat == NFSERR_DENIED) {
4022 fl->l_whence = SEEK_SET;
4023 NFSM_DISSECT(tl, u_int32_t *, 8 * NFSX_UNSIGNED);
4024 fl->l_start = fxdr_hyper(tl);
4026 len = fxdr_hyper(tl);
4028 if (len == NFS64BITSSET)
4032 type = fxdr_unsigned(int, *tl++);
4033 if (type == NFSV4LOCKT_WRITE)
4034 fl->l_type = F_WRLCK;
4036 fl->l_type = F_RDLCK;
4038 * XXX For now, I have no idea what to do with the
4039 * conflicting lock_owner, so I'll just set the pid == 0
4040 * and skip over the lock_owner.
4042 fl->l_pid = (pid_t)0;
4044 size = fxdr_unsigned(int, *tl);
4045 if (size < 0 || size > NFSV4_OPAQUELIMIT)
4048 error = nfsm_advance(nd, NFSM_RNDUP(size), -1);
4049 } else if (nd->nd_repstat == NFSERR_STALECLIENTID)
4050 nfscl_initiate_recovery(clp);
4052 mbuf_freem(nd->nd_mrep);
4057 * Lower level function that performs the LockU RPC.
4060 nfsrpc_locku(struct nfsrv_descript *nd, struct nfsmount *nmp,
4061 struct nfscllockowner *lp, u_int64_t off, u_int64_t len,
4062 u_int32_t type, struct ucred *cred, NFSPROC_T *p, int syscred)
4067 nfscl_reqstart(nd, NFSPROC_LOCKU, nmp, lp->nfsl_open->nfso_fh,
4068 lp->nfsl_open->nfso_fhlen, NULL, NULL, 0, 0);
4069 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID + 6 * NFSX_UNSIGNED);
4070 *tl++ = txdr_unsigned(type);
4071 *tl = txdr_unsigned(lp->nfsl_seqid);
4072 if (nfstest_outofseq &&
4073 (arc4random() % nfstest_outofseq) == 0)
4074 *tl = txdr_unsigned(lp->nfsl_seqid + 1);
4076 if (NFSHASNFSV4N(nmp))
4079 *tl++ = lp->nfsl_stateid.seqid;
4080 *tl++ = lp->nfsl_stateid.other[0];
4081 *tl++ = lp->nfsl_stateid.other[1];
4082 *tl++ = lp->nfsl_stateid.other[2];
4083 txdr_hyper(off, tl);
4085 txdr_hyper(len, tl);
4087 nd->nd_flag |= ND_USEGSSNAME;
4088 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4089 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4090 NFSCL_INCRSEQID(lp->nfsl_seqid, nd);
4093 if (nd->nd_repstat == 0) {
4094 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID);
4095 lp->nfsl_stateid.seqid = *tl++;
4096 lp->nfsl_stateid.other[0] = *tl++;
4097 lp->nfsl_stateid.other[1] = *tl++;
4098 lp->nfsl_stateid.other[2] = *tl;
4099 } else if (nd->nd_repstat == NFSERR_STALESTATEID)
4100 nfscl_initiate_recovery(lp->nfsl_open->nfso_own->nfsow_clp);
4102 mbuf_freem(nd->nd_mrep);
4107 * The actual Lock RPC.
4110 nfsrpc_lock(struct nfsrv_descript *nd, struct nfsmount *nmp, vnode_t vp,
4111 u_int8_t *nfhp, int fhlen, struct nfscllockowner *lp, int newone,
4112 int reclaim, u_int64_t off, u_int64_t len, short type, struct ucred *cred,
4113 NFSPROC_T *p, int syscred)
4117 uint8_t own[NFSV4CL_LOCKNAMELEN + NFSX_V4FHMAX];
4118 struct nfsclsession *tsep;
4120 nfscl_reqstart(nd, NFSPROC_LOCK, nmp, nfhp, fhlen, NULL, NULL, 0, 0);
4121 NFSM_BUILD(tl, u_int32_t *, 7 * NFSX_UNSIGNED);
4122 if (type == F_RDLCK)
4123 *tl++ = txdr_unsigned(NFSV4LOCKT_READ);
4125 *tl++ = txdr_unsigned(NFSV4LOCKT_WRITE);
4126 *tl++ = txdr_unsigned(reclaim);
4127 txdr_hyper(off, tl);
4129 txdr_hyper(len, tl);
4133 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID +
4134 2 * NFSX_UNSIGNED + NFSX_HYPER);
4135 *tl++ = txdr_unsigned(lp->nfsl_open->nfso_own->nfsow_seqid);
4136 if (NFSHASNFSV4N(nmp))
4139 *tl++ = lp->nfsl_open->nfso_stateid.seqid;
4140 *tl++ = lp->nfsl_open->nfso_stateid.other[0];
4141 *tl++ = lp->nfsl_open->nfso_stateid.other[1];
4142 *tl++ = lp->nfsl_open->nfso_stateid.other[2];
4143 *tl++ = txdr_unsigned(lp->nfsl_seqid);
4144 tsep = nfsmnt_mdssession(nmp);
4145 *tl++ = tsep->nfsess_clientid.lval[0];
4146 *tl = tsep->nfsess_clientid.lval[1];
4147 NFSBCOPY(lp->nfsl_owner, own, NFSV4CL_LOCKNAMELEN);
4148 NFSBCOPY(nfhp, &own[NFSV4CL_LOCKNAMELEN], fhlen);
4149 (void)nfsm_strtom(nd, own, NFSV4CL_LOCKNAMELEN + fhlen);
4152 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID + NFSX_UNSIGNED);
4153 if (NFSHASNFSV4N(nmp))
4156 *tl++ = lp->nfsl_stateid.seqid;
4157 *tl++ = lp->nfsl_stateid.other[0];
4158 *tl++ = lp->nfsl_stateid.other[1];
4159 *tl++ = lp->nfsl_stateid.other[2];
4160 *tl = txdr_unsigned(lp->nfsl_seqid);
4161 if (nfstest_outofseq &&
4162 (arc4random() % nfstest_outofseq) == 0)
4163 *tl = txdr_unsigned(lp->nfsl_seqid + 1);
4166 nd->nd_flag |= ND_USEGSSNAME;
4167 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, vp, p, cred,
4168 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4172 NFSCL_INCRSEQID(lp->nfsl_open->nfso_own->nfsow_seqid, nd);
4173 NFSCL_INCRSEQID(lp->nfsl_seqid, nd);
4174 if (nd->nd_repstat == 0) {
4175 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID);
4176 lp->nfsl_stateid.seqid = *tl++;
4177 lp->nfsl_stateid.other[0] = *tl++;
4178 lp->nfsl_stateid.other[1] = *tl++;
4179 lp->nfsl_stateid.other[2] = *tl;
4180 } else if (nd->nd_repstat == NFSERR_DENIED) {
4181 NFSM_DISSECT(tl, u_int32_t *, 8 * NFSX_UNSIGNED);
4182 size = fxdr_unsigned(int, *(tl + 7));
4183 if (size < 0 || size > NFSV4_OPAQUELIMIT)
4186 error = nfsm_advance(nd, NFSM_RNDUP(size), -1);
4187 } else if (nd->nd_repstat == NFSERR_STALESTATEID)
4188 nfscl_initiate_recovery(lp->nfsl_open->nfso_own->nfsow_clp);
4190 mbuf_freem(nd->nd_mrep);
4196 * (always called with the vp for the mount point)
4199 nfsrpc_statfs(vnode_t vp, struct nfsstatfs *sbp, struct nfsfsinfo *fsp,
4200 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp,
4203 u_int32_t *tl = NULL;
4204 struct nfsrv_descript nfsd, *nd = &nfsd;
4205 struct nfsmount *nmp;
4206 nfsattrbit_t attrbits;
4210 nmp = VFSTONFS(vnode_mount(vp));
4211 if (NFSHASNFSV4(nmp)) {
4213 * For V4, you actually do a getattr.
4215 NFSCL_REQSTART(nd, NFSPROC_GETATTR, vp);
4216 NFSSTATFS_GETATTRBIT(&attrbits);
4217 (void) nfsrv_putattrbit(nd, &attrbits);
4218 nd->nd_flag |= ND_USEGSSNAME;
4219 error = nfscl_request(nd, vp, p, cred, stuff);
4222 if (nd->nd_repstat == 0) {
4223 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0,
4224 NULL, NULL, sbp, fsp, NULL, 0, NULL, NULL, NULL, p,
4227 nmp->nm_fsid[0] = nap->na_filesid[0];
4228 nmp->nm_fsid[1] = nap->na_filesid[1];
4229 NFSSETHASSETFSID(nmp);
4233 error = nd->nd_repstat;
4238 NFSCL_REQSTART(nd, NFSPROC_FSSTAT, vp);
4239 error = nfscl_request(nd, vp, p, cred, stuff);
4242 if (nd->nd_flag & ND_NFSV3) {
4243 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
4247 if (nd->nd_repstat) {
4248 error = nd->nd_repstat;
4251 NFSM_DISSECT(tl, u_int32_t *,
4252 NFSX_STATFS(nd->nd_flag & ND_NFSV3));
4254 if (NFSHASNFSV3(nmp)) {
4255 sbp->sf_tbytes = fxdr_hyper(tl); tl += 2;
4256 sbp->sf_fbytes = fxdr_hyper(tl); tl += 2;
4257 sbp->sf_abytes = fxdr_hyper(tl); tl += 2;
4258 sbp->sf_tfiles = fxdr_hyper(tl); tl += 2;
4259 sbp->sf_ffiles = fxdr_hyper(tl); tl += 2;
4260 sbp->sf_afiles = fxdr_hyper(tl); tl += 2;
4261 sbp->sf_invarsec = fxdr_unsigned(u_int32_t, *tl);
4262 } else if (NFSHASNFSV4(nmp) == 0) {
4263 sbp->sf_tsize = fxdr_unsigned(u_int32_t, *tl++);
4264 sbp->sf_bsize = fxdr_unsigned(u_int32_t, *tl++);
4265 sbp->sf_blocks = fxdr_unsigned(u_int32_t, *tl++);
4266 sbp->sf_bfree = fxdr_unsigned(u_int32_t, *tl++);
4267 sbp->sf_bavail = fxdr_unsigned(u_int32_t, *tl);
4270 mbuf_freem(nd->nd_mrep);
4278 nfsrpc_pathconf(vnode_t vp, struct nfsv3_pathconf *pc,
4279 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp,
4282 struct nfsrv_descript nfsd, *nd = &nfsd;
4283 struct nfsmount *nmp;
4285 nfsattrbit_t attrbits;
4289 nmp = VFSTONFS(vnode_mount(vp));
4290 if (NFSHASNFSV4(nmp)) {
4292 * For V4, you actually do a getattr.
4294 NFSCL_REQSTART(nd, NFSPROC_GETATTR, vp);
4295 NFSPATHCONF_GETATTRBIT(&attrbits);
4296 (void) nfsrv_putattrbit(nd, &attrbits);
4297 nd->nd_flag |= ND_USEGSSNAME;
4298 error = nfscl_request(nd, vp, p, cred, stuff);
4301 if (nd->nd_repstat == 0) {
4302 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0,
4303 pc, NULL, NULL, NULL, NULL, 0, NULL, NULL, NULL, p,
4308 error = nd->nd_repstat;
4311 NFSCL_REQSTART(nd, NFSPROC_PATHCONF, vp);
4312 error = nfscl_request(nd, vp, p, cred, stuff);
4315 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
4316 if (nd->nd_repstat && !error)
4317 error = nd->nd_repstat;
4319 NFSM_DISSECT(tl, u_int32_t *, NFSX_V3PATHCONF);
4320 pc->pc_linkmax = fxdr_unsigned(u_int32_t, *tl++);
4321 pc->pc_namemax = fxdr_unsigned(u_int32_t, *tl++);
4322 pc->pc_notrunc = fxdr_unsigned(u_int32_t, *tl++);
4323 pc->pc_chownrestricted =
4324 fxdr_unsigned(u_int32_t, *tl++);
4325 pc->pc_caseinsensitive =
4326 fxdr_unsigned(u_int32_t, *tl++);
4327 pc->pc_casepreserving = fxdr_unsigned(u_int32_t, *tl);
4331 mbuf_freem(nd->nd_mrep);
4336 * nfs version 3 fsinfo rpc call
4339 nfsrpc_fsinfo(vnode_t vp, struct nfsfsinfo *fsp, struct ucred *cred,
4340 NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, void *stuff)
4343 struct nfsrv_descript nfsd, *nd = &nfsd;
4347 NFSCL_REQSTART(nd, NFSPROC_FSINFO, vp);
4348 error = nfscl_request(nd, vp, p, cred, stuff);
4351 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
4352 if (nd->nd_repstat && !error)
4353 error = nd->nd_repstat;
4355 NFSM_DISSECT(tl, u_int32_t *, NFSX_V3FSINFO);
4356 fsp->fs_rtmax = fxdr_unsigned(u_int32_t, *tl++);
4357 fsp->fs_rtpref = fxdr_unsigned(u_int32_t, *tl++);
4358 fsp->fs_rtmult = fxdr_unsigned(u_int32_t, *tl++);
4359 fsp->fs_wtmax = fxdr_unsigned(u_int32_t, *tl++);
4360 fsp->fs_wtpref = fxdr_unsigned(u_int32_t, *tl++);
4361 fsp->fs_wtmult = fxdr_unsigned(u_int32_t, *tl++);
4362 fsp->fs_dtpref = fxdr_unsigned(u_int32_t, *tl++);
4363 fsp->fs_maxfilesize = fxdr_hyper(tl);
4365 fxdr_nfsv3time(tl, &fsp->fs_timedelta);
4367 fsp->fs_properties = fxdr_unsigned(u_int32_t, *tl);
4370 mbuf_freem(nd->nd_mrep);
4375 * This function performs the Renew RPC.
4378 nfsrpc_renew(struct nfsclclient *clp, struct nfsclds *dsp, struct ucred *cred,
4382 struct nfsrv_descript nfsd;
4383 struct nfsrv_descript *nd = &nfsd;
4384 struct nfsmount *nmp;
4386 struct nfssockreq *nrp;
4387 struct nfsclsession *tsep;
4389 nmp = clp->nfsc_nmp;
4393 nfscl_reqstart(nd, NFSPROC_RENEW, nmp, NULL, 0, NULL, NULL, 0,
4396 nfscl_reqstart(nd, NFSPROC_RENEW, nmp, NULL, 0, NULL,
4397 &dsp->nfsclds_sess, 0, 0);
4398 if (!NFSHASNFSV4N(nmp)) {
4399 /* NFSv4.1 just uses a Sequence Op and not a Renew. */
4400 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
4401 tsep = nfsmnt_mdssession(nmp);
4402 *tl++ = tsep->nfsess_clientid.lval[0];
4403 *tl = tsep->nfsess_clientid.lval[1];
4407 nrp = dsp->nfsclds_sockp;
4409 /* If NULL, use the MDS socket. */
4410 nrp = &nmp->nm_sockreq;
4411 nd->nd_flag |= ND_USEGSSNAME;
4413 error = newnfs_request(nd, nmp, NULL, nrp, NULL, p, cred,
4414 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4416 error = newnfs_request(nd, nmp, NULL, nrp, NULL, p, cred,
4417 NFS_PROG, NFS_VER4, NULL, 1, NULL, &dsp->nfsclds_sess);
4419 nfscl_cancelreqs(dsp);
4423 error = nd->nd_repstat;
4424 mbuf_freem(nd->nd_mrep);
4429 * This function performs the Releaselockowner RPC.
4432 nfsrpc_rellockown(struct nfsmount *nmp, struct nfscllockowner *lp,
4433 uint8_t *fh, int fhlen, struct ucred *cred, NFSPROC_T *p)
4435 struct nfsrv_descript nfsd, *nd = &nfsd;
4438 uint8_t own[NFSV4CL_LOCKNAMELEN + NFSX_V4FHMAX];
4439 struct nfsclsession *tsep;
4441 if (NFSHASNFSV4N(nmp)) {
4442 /* For NFSv4.1, do a FreeStateID. */
4443 nfscl_reqstart(nd, NFSPROC_FREESTATEID, nmp, NULL, 0, NULL,
4445 nfsm_stateidtom(nd, &lp->nfsl_stateid, NFSSTATEID_PUTSTATEID);
4447 nfscl_reqstart(nd, NFSPROC_RELEASELCKOWN, nmp, NULL, 0, NULL,
4449 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
4450 tsep = nfsmnt_mdssession(nmp);
4451 *tl++ = tsep->nfsess_clientid.lval[0];
4452 *tl = tsep->nfsess_clientid.lval[1];
4453 NFSBCOPY(lp->nfsl_owner, own, NFSV4CL_LOCKNAMELEN);
4454 NFSBCOPY(fh, &own[NFSV4CL_LOCKNAMELEN], fhlen);
4455 (void)nfsm_strtom(nd, own, NFSV4CL_LOCKNAMELEN + fhlen);
4457 nd->nd_flag |= ND_USEGSSNAME;
4458 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4459 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4462 error = nd->nd_repstat;
4463 mbuf_freem(nd->nd_mrep);
4468 * This function performs the Compound to get the mount pt FH.
4471 nfsrpc_getdirpath(struct nfsmount *nmp, u_char *dirpath, struct ucred *cred,
4475 struct nfsrv_descript nfsd;
4476 struct nfsrv_descript *nd = &nfsd;
4478 int error, cnt, len, setnil;
4481 nfscl_reqstart(nd, NFSPROC_PUTROOTFH, nmp, NULL, 0, &opcntp, NULL, 0,
4490 while (*cp2 != '\0' && *cp2 != '/')
4497 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
4498 *tl = txdr_unsigned(NFSV4OP_LOOKUP);
4499 nfsm_strtom(nd, cp, strlen(cp));
4505 } while (*cp != '\0');
4506 if (NFSHASNFSV4N(nmp))
4507 /* Has a Sequence Op done by nfscl_reqstart(). */
4508 *opcntp = txdr_unsigned(3 + cnt);
4510 *opcntp = txdr_unsigned(2 + cnt);
4511 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
4512 *tl = txdr_unsigned(NFSV4OP_GETFH);
4513 nd->nd_flag |= ND_USEGSSNAME;
4514 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4515 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4518 if (nd->nd_repstat == 0) {
4519 NFSM_DISSECT(tl, u_int32_t *, (3 + 2 * cnt) * NFSX_UNSIGNED);
4520 tl += (2 + 2 * cnt);
4521 if ((len = fxdr_unsigned(int, *tl)) <= 0 ||
4523 nd->nd_repstat = NFSERR_BADXDR;
4525 nd->nd_repstat = nfsrv_mtostr(nd, nmp->nm_fh, len);
4526 if (nd->nd_repstat == 0)
4527 nmp->nm_fhsize = len;
4530 error = nd->nd_repstat;
4532 mbuf_freem(nd->nd_mrep);
4537 * This function performs the Delegreturn RPC.
4540 nfsrpc_delegreturn(struct nfscldeleg *dp, struct ucred *cred,
4541 struct nfsmount *nmp, NFSPROC_T *p, int syscred)
4544 struct nfsrv_descript nfsd;
4545 struct nfsrv_descript *nd = &nfsd;
4548 nfscl_reqstart(nd, NFSPROC_DELEGRETURN, nmp, dp->nfsdl_fh,
4549 dp->nfsdl_fhlen, NULL, NULL, 0, 0);
4550 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID);
4551 if (NFSHASNFSV4N(nmp))
4554 *tl++ = dp->nfsdl_stateid.seqid;
4555 *tl++ = dp->nfsdl_stateid.other[0];
4556 *tl++ = dp->nfsdl_stateid.other[1];
4557 *tl = dp->nfsdl_stateid.other[2];
4559 nd->nd_flag |= ND_USEGSSNAME;
4560 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4561 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4564 error = nd->nd_repstat;
4565 mbuf_freem(nd->nd_mrep);
4573 nfsrpc_getacl(vnode_t vp, struct ucred *cred, NFSPROC_T *p,
4574 struct acl *aclp, void *stuff)
4576 struct nfsrv_descript nfsd, *nd = &nfsd;
4578 nfsattrbit_t attrbits;
4579 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
4581 if (nfsrv_useacl == 0 || !NFSHASNFSV4(nmp))
4582 return (EOPNOTSUPP);
4583 NFSCL_REQSTART(nd, NFSPROC_GETACL, vp);
4584 NFSZERO_ATTRBIT(&attrbits);
4585 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_ACL);
4586 (void) nfsrv_putattrbit(nd, &attrbits);
4587 error = nfscl_request(nd, vp, p, cred, stuff);
4590 if (!nd->nd_repstat)
4591 error = nfsv4_loadattr(nd, vp, NULL, NULL, NULL, 0, NULL,
4592 NULL, NULL, NULL, aclp, 0, NULL, NULL, NULL, p, cred);
4594 error = nd->nd_repstat;
4595 mbuf_freem(nd->nd_mrep);
4603 nfsrpc_setacl(vnode_t vp, struct ucred *cred, NFSPROC_T *p,
4604 struct acl *aclp, void *stuff)
4607 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
4609 if (nfsrv_useacl == 0 || !NFSHASNFSV4(nmp))
4610 return (EOPNOTSUPP);
4611 error = nfsrpc_setattr(vp, NULL, aclp, cred, p, NULL, NULL, stuff);
4619 nfsrpc_setaclrpc(vnode_t vp, struct ucred *cred, NFSPROC_T *p,
4620 struct acl *aclp, nfsv4stateid_t *stateidp, void *stuff)
4622 struct nfsrv_descript nfsd, *nd = &nfsd;
4624 nfsattrbit_t attrbits;
4625 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
4627 if (!NFSHASNFSV4(nmp))
4628 return (EOPNOTSUPP);
4629 NFSCL_REQSTART(nd, NFSPROC_SETACL, vp);
4630 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID);
4631 NFSZERO_ATTRBIT(&attrbits);
4632 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_ACL);
4633 (void) nfsv4_fillattr(nd, vnode_mount(vp), vp, aclp, NULL, NULL, 0,
4634 &attrbits, NULL, NULL, 0, 0, 0, 0, (uint64_t)0, NULL);
4635 error = nfscl_request(nd, vp, p, cred, stuff);
4638 /* Don't care about the pre/postop attributes */
4639 mbuf_freem(nd->nd_mrep);
4640 return (nd->nd_repstat);
4644 * Do the NFSv4.1 Exchange ID.
4647 nfsrpc_exchangeid(struct nfsmount *nmp, struct nfsclclient *clp,
4648 struct nfssockreq *nrp, uint32_t exchflags, struct nfsclds **dspp,
4649 struct ucred *cred, NFSPROC_T *p)
4651 uint32_t *tl, v41flags;
4652 struct nfsrv_descript nfsd;
4653 struct nfsrv_descript *nd = &nfsd;
4654 struct nfsclds *dsp;
4655 struct timespec verstime;
4659 nfscl_reqstart(nd, NFSPROC_EXCHANGEID, nmp, NULL, 0, NULL, NULL, 0, 0);
4660 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_UNSIGNED);
4661 *tl++ = txdr_unsigned(nfsboottime.tv_sec); /* Client owner */
4662 *tl = txdr_unsigned(clp->nfsc_rev);
4663 (void) nfsm_strtom(nd, clp->nfsc_id, clp->nfsc_idlen);
4665 NFSM_BUILD(tl, uint32_t *, 3 * NFSX_UNSIGNED);
4666 *tl++ = txdr_unsigned(exchflags);
4667 *tl++ = txdr_unsigned(NFSV4EXCH_SP4NONE);
4669 /* Set the implementation id4 */
4670 *tl = txdr_unsigned(1);
4671 (void) nfsm_strtom(nd, "freebsd.org", strlen("freebsd.org"));
4672 (void) nfsm_strtom(nd, version, strlen(version));
4673 NFSM_BUILD(tl, uint32_t *, NFSX_V4TIME);
4674 verstime.tv_sec = 1293840000; /* Jan 1, 2011 */
4675 verstime.tv_nsec = 0;
4676 txdr_nfsv4time(&verstime, tl);
4677 nd->nd_flag |= ND_USEGSSNAME;
4678 error = newnfs_request(nd, nmp, NULL, nrp, NULL, p, cred,
4679 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4680 NFSCL_DEBUG(1, "exchangeid err=%d reps=%d\n", error,
4681 (int)nd->nd_repstat);
4684 if (nd->nd_repstat == 0) {
4685 NFSM_DISSECT(tl, uint32_t *, 6 * NFSX_UNSIGNED + NFSX_HYPER);
4686 len = fxdr_unsigned(int, *(tl + 7));
4687 if (len < 0 || len > NFSV4_OPAQUELIMIT) {
4688 error = NFSERR_BADXDR;
4691 dsp = malloc(sizeof(struct nfsclds) + len + 1, M_NFSCLDS,
4693 dsp->nfsclds_expire = NFSD_MONOSEC + clp->nfsc_renew;
4694 dsp->nfsclds_servownlen = len;
4695 dsp->nfsclds_sess.nfsess_clientid.lval[0] = *tl++;
4696 dsp->nfsclds_sess.nfsess_clientid.lval[1] = *tl++;
4697 dsp->nfsclds_sess.nfsess_sequenceid =
4698 fxdr_unsigned(uint32_t, *tl++);
4699 v41flags = fxdr_unsigned(uint32_t, *tl);
4700 if ((v41flags & NFSV4EXCH_USEPNFSMDS) != 0 &&
4701 NFSHASPNFSOPT(nmp)) {
4702 NFSCL_DEBUG(1, "set PNFS\n");
4704 nmp->nm_state |= NFSSTA_PNFS;
4706 dsp->nfsclds_flags |= NFSCLDS_MDS;
4708 if ((v41flags & NFSV4EXCH_USEPNFSDS) != 0)
4709 dsp->nfsclds_flags |= NFSCLDS_DS;
4711 nd->nd_repstat = nfsrv_mtostr(nd,
4712 dsp->nfsclds_serverown, len);
4713 if (nd->nd_repstat == 0) {
4714 mtx_init(&dsp->nfsclds_mtx, "nfsds", NULL, MTX_DEF);
4715 mtx_init(&dsp->nfsclds_sess.nfsess_mtx, "nfssession",
4717 nfscl_initsessionslots(&dsp->nfsclds_sess);
4720 free(dsp, M_NFSCLDS);
4722 error = nd->nd_repstat;
4724 mbuf_freem(nd->nd_mrep);
4729 * Do the NFSv4.1 Create Session.
4732 nfsrpc_createsession(struct nfsmount *nmp, struct nfsclsession *sep,
4733 struct nfssockreq *nrp, uint32_t sequenceid, int mds, struct ucred *cred,
4736 uint32_t crflags, maxval, *tl;
4737 struct nfsrv_descript nfsd;
4738 struct nfsrv_descript *nd = &nfsd;
4741 /* Make sure nm_rsize, nm_wsize is set. */
4742 if (nmp->nm_rsize > NFS_MAXBSIZE || nmp->nm_rsize == 0)
4743 nmp->nm_rsize = NFS_MAXBSIZE;
4744 if (nmp->nm_wsize > NFS_MAXBSIZE || nmp->nm_wsize == 0)
4745 nmp->nm_wsize = NFS_MAXBSIZE;
4746 nfscl_reqstart(nd, NFSPROC_CREATESESSION, nmp, NULL, 0, NULL, NULL, 0,
4748 NFSM_BUILD(tl, uint32_t *, 4 * NFSX_UNSIGNED);
4749 *tl++ = sep->nfsess_clientid.lval[0];
4750 *tl++ = sep->nfsess_clientid.lval[1];
4751 *tl++ = txdr_unsigned(sequenceid);
4752 crflags = (NFSMNT_RDONLY(nmp->nm_mountp) ? 0 : NFSV4CRSESS_PERSIST);
4753 if (nfscl_enablecallb != 0 && nfs_numnfscbd > 0 && mds != 0)
4754 crflags |= NFSV4CRSESS_CONNBACKCHAN;
4755 *tl = txdr_unsigned(crflags);
4757 /* Fill in fore channel attributes. */
4758 NFSM_BUILD(tl, uint32_t *, 7 * NFSX_UNSIGNED);
4759 *tl++ = 0; /* Header pad size */
4760 *tl++ = txdr_unsigned(nmp->nm_wsize + NFS_MAXXDR);/* Max request size */
4761 *tl++ = txdr_unsigned(nmp->nm_rsize + NFS_MAXXDR);/* Max reply size */
4762 *tl++ = txdr_unsigned(4096); /* Max response size cached */
4763 *tl++ = txdr_unsigned(20); /* Max operations */
4764 *tl++ = txdr_unsigned(64); /* Max slots */
4765 *tl = 0; /* No rdma ird */
4767 /* Fill in back channel attributes. */
4768 NFSM_BUILD(tl, uint32_t *, 7 * NFSX_UNSIGNED);
4769 *tl++ = 0; /* Header pad size */
4770 *tl++ = txdr_unsigned(10000); /* Max request size */
4771 *tl++ = txdr_unsigned(10000); /* Max response size */
4772 *tl++ = txdr_unsigned(4096); /* Max response size cached */
4773 *tl++ = txdr_unsigned(4); /* Max operations */
4774 *tl++ = txdr_unsigned(NFSV4_CBSLOTS); /* Max slots */
4775 *tl = 0; /* No rdma ird */
4777 NFSM_BUILD(tl, uint32_t *, 8 * NFSX_UNSIGNED);
4778 *tl++ = txdr_unsigned(NFS_CALLBCKPROG); /* Call back prog # */
4780 /* Allow AUTH_SYS callbacks as uid, gid == 0. */
4781 *tl++ = txdr_unsigned(1); /* Auth_sys only */
4782 *tl++ = txdr_unsigned(AUTH_SYS); /* AUTH_SYS type */
4783 *tl++ = txdr_unsigned(nfsboottime.tv_sec); /* time stamp */
4784 *tl++ = 0; /* Null machine name */
4785 *tl++ = 0; /* Uid == 0 */
4786 *tl++ = 0; /* Gid == 0 */
4787 *tl = 0; /* No additional gids */
4788 nd->nd_flag |= ND_USEGSSNAME;
4789 error = newnfs_request(nd, nmp, NULL, nrp, NULL, p, cred, NFS_PROG,
4790 NFS_VER4, NULL, 1, NULL, NULL);
4793 if (nd->nd_repstat == 0) {
4794 NFSM_DISSECT(tl, uint32_t *, NFSX_V4SESSIONID +
4796 bcopy(tl, sep->nfsess_sessionid, NFSX_V4SESSIONID);
4797 tl += NFSX_V4SESSIONID / NFSX_UNSIGNED;
4798 sep->nfsess_sequenceid = fxdr_unsigned(uint32_t, *tl++);
4799 crflags = fxdr_unsigned(uint32_t, *tl);
4800 if ((crflags & NFSV4CRSESS_PERSIST) != 0 && mds != 0) {
4802 nmp->nm_state |= NFSSTA_SESSPERSIST;
4806 /* Get the fore channel slot count. */
4807 NFSM_DISSECT(tl, uint32_t *, 7 * NFSX_UNSIGNED);
4808 tl++; /* Skip the header pad size. */
4810 /* Make sure nm_wsize is small enough. */
4811 maxval = fxdr_unsigned(uint32_t, *tl++);
4812 while (maxval < nmp->nm_wsize + NFS_MAXXDR) {
4813 if (nmp->nm_wsize > 8096)
4819 /* Make sure nm_rsize is small enough. */
4820 maxval = fxdr_unsigned(uint32_t, *tl++);
4821 while (maxval < nmp->nm_rsize + NFS_MAXXDR) {
4822 if (nmp->nm_rsize > 8096)
4828 sep->nfsess_maxcache = fxdr_unsigned(int, *tl++);
4830 sep->nfsess_foreslots = fxdr_unsigned(uint16_t, *tl++);
4831 NFSCL_DEBUG(4, "fore slots=%d\n", (int)sep->nfsess_foreslots);
4832 irdcnt = fxdr_unsigned(int, *tl);
4834 NFSM_DISSECT(tl, uint32_t *, irdcnt * NFSX_UNSIGNED);
4836 /* and the back channel slot count. */
4837 NFSM_DISSECT(tl, uint32_t *, 7 * NFSX_UNSIGNED);
4839 sep->nfsess_backslots = fxdr_unsigned(uint16_t, *tl);
4840 NFSCL_DEBUG(4, "back slots=%d\n", (int)sep->nfsess_backslots);
4842 error = nd->nd_repstat;
4844 mbuf_freem(nd->nd_mrep);
4849 * Do the NFSv4.1 Destroy Session.
4852 nfsrpc_destroysession(struct nfsmount *nmp, struct nfsclclient *clp,
4853 struct ucred *cred, NFSPROC_T *p)
4856 struct nfsrv_descript nfsd;
4857 struct nfsrv_descript *nd = &nfsd;
4859 struct nfsclsession *tsep;
4861 nfscl_reqstart(nd, NFSPROC_DESTROYSESSION, nmp, NULL, 0, NULL, NULL, 0,
4863 NFSM_BUILD(tl, uint32_t *, NFSX_V4SESSIONID);
4864 tsep = nfsmnt_mdssession(nmp);
4865 bcopy(tsep->nfsess_sessionid, tl, NFSX_V4SESSIONID);
4866 nd->nd_flag |= ND_USEGSSNAME;
4867 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4868 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4871 error = nd->nd_repstat;
4872 mbuf_freem(nd->nd_mrep);
4877 * Do the NFSv4.1 Destroy Client.
4880 nfsrpc_destroyclient(struct nfsmount *nmp, struct nfsclclient *clp,
4881 struct ucred *cred, NFSPROC_T *p)
4884 struct nfsrv_descript nfsd;
4885 struct nfsrv_descript *nd = &nfsd;
4887 struct nfsclsession *tsep;
4889 nfscl_reqstart(nd, NFSPROC_DESTROYCLIENT, nmp, NULL, 0, NULL, NULL, 0,
4891 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_UNSIGNED);
4892 tsep = nfsmnt_mdssession(nmp);
4893 *tl++ = tsep->nfsess_clientid.lval[0];
4894 *tl = tsep->nfsess_clientid.lval[1];
4895 nd->nd_flag |= ND_USEGSSNAME;
4896 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4897 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4900 error = nd->nd_repstat;
4901 mbuf_freem(nd->nd_mrep);
4906 * Do the NFSv4.1 LayoutGet.
4909 nfsrpc_layoutget(struct nfsmount *nmp, uint8_t *fhp, int fhlen, int iomode,
4910 uint64_t offset, uint64_t len, uint64_t minlen, int layouttype,
4911 int layoutlen, nfsv4stateid_t *stateidp, int *retonclosep,
4912 struct nfsclflayouthead *flhp, struct ucred *cred, NFSPROC_T *p,
4915 struct nfsrv_descript nfsd, *nd = &nfsd;
4918 nfscl_reqstart(nd, NFSPROC_LAYOUTGET, nmp, fhp, fhlen, NULL, NULL, 0,
4920 nfsrv_setuplayoutget(nd, iomode, offset, len, minlen, stateidp,
4921 layouttype, layoutlen, 0);
4922 nd->nd_flag |= ND_USEGSSNAME;
4923 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4924 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4925 NFSCL_DEBUG(4, "layget err=%d st=%d\n", error, nd->nd_repstat);
4928 if (nd->nd_repstat == 0)
4929 error = nfsrv_parselayoutget(nd, stateidp, retonclosep, flhp);
4930 if (error == 0 && nd->nd_repstat != 0)
4931 error = nd->nd_repstat;
4932 mbuf_freem(nd->nd_mrep);
4937 * Do the NFSv4.1 Get Device Info.
4940 nfsrpc_getdeviceinfo(struct nfsmount *nmp, uint8_t *deviceid, int layouttype,
4941 uint32_t *notifybitsp, struct nfscldevinfo **ndip, struct ucred *cred,
4944 uint32_t cnt, *tl, vers, minorvers;
4945 struct nfsrv_descript nfsd;
4946 struct nfsrv_descript *nd = &nfsd;
4947 struct sockaddr_in sin, ssin;
4948 struct sockaddr_in6 sin6, ssin6;
4949 struct nfsclds *dsp = NULL, **dspp, **gotdspp;
4950 struct nfscldevinfo *ndi;
4951 int addrcnt = 0, bitcnt, error, gotvers, i, isudp, j, stripecnt;
4952 uint8_t stripeindex;
4953 sa_family_t af, safilled;
4958 nfscl_reqstart(nd, NFSPROC_GETDEVICEINFO, nmp, NULL, 0, NULL, NULL, 0,
4960 NFSM_BUILD(tl, uint32_t *, NFSX_V4DEVICEID + 3 * NFSX_UNSIGNED);
4961 NFSBCOPY(deviceid, tl, NFSX_V4DEVICEID);
4962 tl += (NFSX_V4DEVICEID / NFSX_UNSIGNED);
4963 *tl++ = txdr_unsigned(layouttype);
4964 *tl++ = txdr_unsigned(100000);
4965 if (notifybitsp != NULL && *notifybitsp != 0) {
4966 *tl = txdr_unsigned(1); /* One word of bits. */
4967 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
4968 *tl = txdr_unsigned(*notifybitsp);
4970 *tl = txdr_unsigned(0);
4971 nd->nd_flag |= ND_USEGSSNAME;
4972 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4973 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4976 if (nd->nd_repstat == 0) {
4977 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
4978 if (layouttype != fxdr_unsigned(int, *tl))
4979 printf("EEK! devinfo layout type not same!\n");
4980 if (layouttype == NFSLAYOUT_NFSV4_1_FILES) {
4981 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
4982 stripecnt = fxdr_unsigned(int, *tl);
4983 NFSCL_DEBUG(4, "stripecnt=%d\n", stripecnt);
4984 if (stripecnt < 1 || stripecnt > 4096) {
4985 printf("pNFS File layout devinfo stripecnt %d:"
4986 " out of range\n", stripecnt);
4987 error = NFSERR_BADXDR;
4990 NFSM_DISSECT(tl, uint32_t *, (stripecnt + 1) *
4992 addrcnt = fxdr_unsigned(int, *(tl + stripecnt));
4993 NFSCL_DEBUG(4, "addrcnt=%d\n", addrcnt);
4994 if (addrcnt < 1 || addrcnt > 128) {
4995 printf("NFS devinfo addrcnt %d: out of range\n",
4997 error = NFSERR_BADXDR;
5002 * Now we know how many stripe indices and addresses, so
5003 * we can allocate the structure the correct size.
5005 i = (stripecnt * sizeof(uint8_t)) /
5006 sizeof(struct nfsclds *) + 1;
5007 NFSCL_DEBUG(4, "stripeindices=%d\n", i);
5008 ndi = malloc(sizeof(*ndi) + (addrcnt + i) *
5009 sizeof(struct nfsclds *), M_NFSDEVINFO, M_WAITOK |
5011 NFSBCOPY(deviceid, ndi->nfsdi_deviceid,
5013 ndi->nfsdi_refcnt = 0;
5014 ndi->nfsdi_flags = NFSDI_FILELAYOUT;
5015 ndi->nfsdi_stripecnt = stripecnt;
5016 ndi->nfsdi_addrcnt = addrcnt;
5017 /* Fill in the stripe indices. */
5018 for (i = 0; i < stripecnt; i++) {
5019 stripeindex = fxdr_unsigned(uint8_t, *tl++);
5020 NFSCL_DEBUG(4, "stripeind=%d\n", stripeindex);
5021 if (stripeindex >= addrcnt) {
5022 printf("pNFS File Layout devinfo"
5023 " stripeindex %d: too big\n",
5025 error = NFSERR_BADXDR;
5028 nfsfldi_setstripeindex(ndi, i, stripeindex);
5030 } else if (layouttype == NFSLAYOUT_FLEXFILE) {
5031 /* For Flex File, we only get one address list. */
5032 ndi = malloc(sizeof(*ndi) + sizeof(struct nfsclds *),
5033 M_NFSDEVINFO, M_WAITOK | M_ZERO);
5034 NFSBCOPY(deviceid, ndi->nfsdi_deviceid,
5036 ndi->nfsdi_refcnt = 0;
5037 ndi->nfsdi_flags = NFSDI_FLEXFILE;
5038 addrcnt = ndi->nfsdi_addrcnt = 1;
5041 /* Now, dissect the server address(es). */
5042 safilled = AF_UNSPEC;
5043 for (i = 0; i < addrcnt; i++) {
5044 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
5045 cnt = fxdr_unsigned(uint32_t, *tl);
5047 printf("NFS devinfo 0 len addrlist\n");
5048 error = NFSERR_BADXDR;
5051 dspp = nfsfldi_addr(ndi, i);
5052 safilled = AF_UNSPEC;
5053 for (j = 0; j < cnt; j++) {
5054 error = nfsv4_getipaddr(nd, &sin, &sin6, &af,
5056 if (error != 0 && error != EPERM) {
5057 error = NFSERR_BADXDR;
5060 if (error == 0 && isudp == 0) {
5063 * - Same address family.
5064 * Save the address and dspp, so that
5065 * the connection can be done after
5066 * parsing is complete.
5068 if (safilled == AF_UNSPEC ||
5069 (af == nmp->nm_nam->sa_family &&
5070 safilled != nmp->nm_nam->sa_family)
5083 gotvers = NFS_VER4; /* Always NFSv4 for File Layout. */
5084 /* For Flex File, we will take one of the versions to use. */
5085 if (layouttype == NFSLAYOUT_FLEXFILE) {
5086 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
5087 j = fxdr_unsigned(int, *tl);
5088 if (j < 1 || j > NFSDEV_MAXVERS) {
5089 printf("pNFS: too many versions\n");
5090 error = NFSERR_BADXDR;
5094 for (i = 0; i < j; i++) {
5095 NFSM_DISSECT(tl, uint32_t *, 5 * NFSX_UNSIGNED);
5096 vers = fxdr_unsigned(uint32_t, *tl++);
5097 minorvers = fxdr_unsigned(uint32_t, *tl++);
5098 if ((vers == NFS_VER4 && minorvers ==
5099 NFSV41_MINORVERSION) || (vers == NFS_VER3 &&
5102 /* We'll take this one. */
5103 ndi->nfsdi_versindex = i;
5104 ndi->nfsdi_vers = vers;
5105 ndi->nfsdi_minorvers = minorvers;
5106 ndi->nfsdi_rsize = fxdr_unsigned(
5108 ndi->nfsdi_wsize = fxdr_unsigned(
5110 if (*tl == newnfs_true)
5115 ~NFSDI_TIGHTCOUPLED;
5119 printf("pNFS: no NFSv3 or NFSv4.1\n");
5120 error = NFSERR_BADXDR;
5125 /* And the notify bits. */
5126 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
5127 bitcnt = fxdr_unsigned(int, *tl);
5129 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
5130 if (notifybitsp != NULL)
5132 fxdr_unsigned(uint32_t, *tl);
5134 if (safilled != AF_UNSPEC) {
5135 KASSERT(ndi != NULL, ("ndi is NULL"));
5141 * Now we can do a TCP connection for the correct
5142 * NFS version and IP address.
5144 error = nfsrpc_fillsa(nmp, &ssin, &ssin6, safilled,
5148 KASSERT(gotdspp != NULL, ("gotdspp is NULL"));
5152 if (nd->nd_repstat != 0 && error == 0)
5153 error = nd->nd_repstat;
5155 if (error != 0 && ndi != NULL)
5156 nfscl_freedevinfo(ndi);
5157 mbuf_freem(nd->nd_mrep);
5162 * Do the NFSv4.1 LayoutCommit.
5165 nfsrpc_layoutcommit(struct nfsmount *nmp, uint8_t *fh, int fhlen, int reclaim,
5166 uint64_t off, uint64_t len, uint64_t lastbyte, nfsv4stateid_t *stateidp,
5167 int layouttype, struct ucred *cred, NFSPROC_T *p, void *stuff)
5170 struct nfsrv_descript nfsd, *nd = &nfsd;
5173 nfscl_reqstart(nd, NFSPROC_LAYOUTCOMMIT, nmp, fh, fhlen, NULL, NULL,
5175 NFSM_BUILD(tl, uint32_t *, 5 * NFSX_UNSIGNED + 3 * NFSX_HYPER +
5177 txdr_hyper(off, tl);
5179 txdr_hyper(len, tl);
5182 *tl++ = newnfs_true;
5184 *tl++ = newnfs_false;
5185 *tl++ = txdr_unsigned(stateidp->seqid);
5186 *tl++ = stateidp->other[0];
5187 *tl++ = stateidp->other[1];
5188 *tl++ = stateidp->other[2];
5189 *tl++ = newnfs_true;
5192 else if (lastbyte >= (off + len))
5193 lastbyte = off + len - 1;
5194 txdr_hyper(lastbyte, tl);
5196 *tl++ = newnfs_false;
5197 *tl++ = txdr_unsigned(layouttype);
5198 /* All supported layouts are 0 length. */
5199 *tl = txdr_unsigned(0);
5200 nd->nd_flag |= ND_USEGSSNAME;
5201 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
5202 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
5205 error = nd->nd_repstat;
5206 mbuf_freem(nd->nd_mrep);
5211 * Do the NFSv4.1 LayoutReturn.
5214 nfsrpc_layoutreturn(struct nfsmount *nmp, uint8_t *fh, int fhlen, int reclaim,
5215 int layouttype, uint32_t iomode, int layoutreturn, uint64_t offset,
5216 uint64_t len, nfsv4stateid_t *stateidp, struct ucred *cred, NFSPROC_T *p,
5217 uint32_t stat, uint32_t op, char *devid)
5220 struct nfsrv_descript nfsd, *nd = &nfsd;
5224 nfscl_reqstart(nd, NFSPROC_LAYOUTRETURN, nmp, fh, fhlen, NULL, NULL,
5226 NFSM_BUILD(tl, uint32_t *, 4 * NFSX_UNSIGNED);
5228 *tl++ = newnfs_true;
5230 *tl++ = newnfs_false;
5231 *tl++ = txdr_unsigned(layouttype);
5232 *tl++ = txdr_unsigned(iomode);
5233 *tl = txdr_unsigned(layoutreturn);
5234 if (layoutreturn == NFSLAYOUTRETURN_FILE) {
5235 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_HYPER + NFSX_STATEID +
5237 txdr_hyper(offset, tl);
5239 txdr_hyper(len, tl);
5241 NFSCL_DEBUG(4, "layoutret stseq=%d\n", (int)stateidp->seqid);
5242 *tl++ = txdr_unsigned(stateidp->seqid);
5243 *tl++ = stateidp->other[0];
5244 *tl++ = stateidp->other[1];
5245 *tl++ = stateidp->other[2];
5246 if (layouttype == NFSLAYOUT_NFSV4_1_FILES)
5247 *tl = txdr_unsigned(0);
5248 else if (layouttype == NFSLAYOUT_FLEXFILE) {
5250 *tl = txdr_unsigned(2 * NFSX_HYPER +
5251 NFSX_STATEID + NFSX_V4DEVICEID + 5 *
5253 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_HYPER +
5254 NFSX_STATEID + NFSX_V4DEVICEID + 5 *
5256 *tl++ = txdr_unsigned(1); /* One error. */
5257 tu64 = 0; /* Offset. */
5258 txdr_hyper(tu64, tl); tl += 2;
5259 tu64 = UINT64_MAX; /* Length. */
5260 txdr_hyper(tu64, tl); tl += 2;
5261 NFSBCOPY(stateidp, tl, NFSX_STATEID);
5262 tl += (NFSX_STATEID / NFSX_UNSIGNED);
5263 *tl++ = txdr_unsigned(1); /* One error. */
5264 NFSBCOPY(devid, tl, NFSX_V4DEVICEID);
5265 tl += (NFSX_V4DEVICEID / NFSX_UNSIGNED);
5266 *tl++ = txdr_unsigned(stat);
5267 *tl++ = txdr_unsigned(op);
5269 *tl = txdr_unsigned(2 * NFSX_UNSIGNED);
5270 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_UNSIGNED);
5274 *tl = 0; /* No stats yet. */
5277 nd->nd_flag |= ND_USEGSSNAME;
5278 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
5279 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
5282 if (nd->nd_repstat == 0) {
5283 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
5285 NFSM_DISSECT(tl, uint32_t *, NFSX_STATEID);
5286 stateidp->seqid = fxdr_unsigned(uint32_t, *tl++);
5287 stateidp->other[0] = *tl++;
5288 stateidp->other[1] = *tl++;
5289 stateidp->other[2] = *tl;
5292 error = nd->nd_repstat;
5294 mbuf_freem(nd->nd_mrep);
5299 * Acquire a layout and devinfo, if possible. The caller must have acquired
5300 * a reference count on the nfsclclient structure before calling this.
5301 * Return the layout in lypp with a reference count on it, if successful.
5304 nfsrpc_getlayout(struct nfsmount *nmp, vnode_t vp, struct nfsfh *nfhp,
5305 int iomode, uint32_t *notifybitsp, nfsv4stateid_t *stateidp, uint64_t off,
5306 struct nfscllayout **lypp, struct ucred *cred, NFSPROC_T *p)
5308 struct nfscllayout *lyp;
5309 struct nfsclflayout *flp;
5310 struct nfsclflayouthead flh;
5311 int error = 0, islocked, layoutlen, layouttype, recalled, retonclose;
5312 nfsv4stateid_t stateid;
5313 struct nfsclsession *tsep;
5316 if (NFSHASFLEXFILE(nmp))
5317 layouttype = NFSLAYOUT_FLEXFILE;
5319 layouttype = NFSLAYOUT_NFSV4_1_FILES;
5321 * If lyp is returned non-NULL, there will be a refcnt (shared lock)
5322 * on it, iff flp != NULL or a lock (exclusive lock) on it iff
5325 lyp = nfscl_getlayout(nmp->nm_clp, nfhp->nfh_fh, nfhp->nfh_len,
5326 off, &flp, &recalled);
5328 if (lyp == NULL || flp == NULL) {
5332 tsep = nfsmnt_mdssession(nmp);
5333 layoutlen = tsep->nfsess_maxcache -
5334 (NFSX_STATEID + 3 * NFSX_UNSIGNED);
5337 stateid.other[0] = stateidp->other[0];
5338 stateid.other[1] = stateidp->other[1];
5339 stateid.other[2] = stateidp->other[2];
5340 error = nfsrpc_layoutget(nmp, nfhp->nfh_fh,
5341 nfhp->nfh_len, iomode, (uint64_t)0, UINT64_MAX,
5342 (uint64_t)0, layouttype, layoutlen, &stateid,
5343 &retonclose, &flh, cred, p, NULL);
5346 stateid.seqid = lyp->nfsly_stateid.seqid;
5347 stateid.other[0] = lyp->nfsly_stateid.other[0];
5348 stateid.other[1] = lyp->nfsly_stateid.other[1];
5349 stateid.other[2] = lyp->nfsly_stateid.other[2];
5350 error = nfsrpc_layoutget(nmp, nfhp->nfh_fh,
5351 nfhp->nfh_len, iomode, off, UINT64_MAX,
5352 (uint64_t)0, layouttype, layoutlen, &stateid,
5353 &retonclose, &flh, cred, p, NULL);
5355 error = nfsrpc_layoutgetres(nmp, vp, nfhp->nfh_fh,
5356 nfhp->nfh_len, &stateid, retonclose, notifybitsp, &lyp,
5357 &flh, layouttype, error, NULL, cred, p);
5360 else if (islocked != 0)
5361 nfscl_rellayout(lyp, 1);
5368 * Do a TCP connection plus exchange id and create session.
5369 * If successful, a "struct nfsclds" is linked into the list for the
5370 * mount point and a pointer to it is returned.
5373 nfsrpc_fillsa(struct nfsmount *nmp, struct sockaddr_in *sin,
5374 struct sockaddr_in6 *sin6, sa_family_t af, int vers, struct nfsclds **dspp,
5377 struct sockaddr_in *msad, *sad;
5378 struct sockaddr_in6 *msad6, *sad6;
5379 struct nfsclclient *clp;
5380 struct nfssockreq *nrp;
5381 struct nfsclds *dsp, *tdsp;
5383 enum nfsclds_state retv;
5384 uint32_t sequenceid;
5386 KASSERT(nmp->nm_sockreq.nr_cred != NULL,
5387 ("nfsrpc_fillsa: NULL nr_cred"));
5393 if (af == AF_INET) {
5396 * Check to see if we already have a session for this
5397 * address that is usable for a DS.
5398 * Note that the MDS's address is in a different place
5399 * than the sessions already acquired for DS's.
5401 msad = (struct sockaddr_in *)nmp->nm_sockreq.nr_nam;
5402 tdsp = TAILQ_FIRST(&nmp->nm_sess);
5403 while (tdsp != NULL) {
5404 if (msad != NULL && msad->sin_family == AF_INET &&
5405 sin->sin_addr.s_addr == msad->sin_addr.s_addr &&
5406 sin->sin_port == msad->sin_port &&
5407 (tdsp->nfsclds_flags & NFSCLDS_DS) != 0 &&
5408 tdsp->nfsclds_sess.nfsess_defunct == 0) {
5411 NFSCL_DEBUG(4, "fnd same addr\n");
5414 tdsp = TAILQ_NEXT(tdsp, nfsclds_list);
5415 if (tdsp != NULL && tdsp->nfsclds_sockp != NULL)
5416 msad = (struct sockaddr_in *)
5417 tdsp->nfsclds_sockp->nr_nam;
5423 /* No IP address match, so look for new/trunked one. */
5424 sad = malloc(sizeof(*sad), M_SONAME, M_WAITOK | M_ZERO);
5425 sad->sin_len = sizeof(*sad);
5426 sad->sin_family = AF_INET;
5427 sad->sin_port = sin->sin_port;
5428 sad->sin_addr.s_addr = sin->sin_addr.s_addr;
5429 nrp = malloc(sizeof(*nrp), M_NFSSOCKREQ, M_WAITOK | M_ZERO);
5430 nrp->nr_nam = (struct sockaddr *)sad;
5431 } else if (af == AF_INET6) {
5434 * Check to see if we already have a session for this
5435 * address that is usable for a DS.
5436 * Note that the MDS's address is in a different place
5437 * than the sessions already acquired for DS's.
5439 msad6 = (struct sockaddr_in6 *)nmp->nm_sockreq.nr_nam;
5440 tdsp = TAILQ_FIRST(&nmp->nm_sess);
5441 while (tdsp != NULL) {
5442 if (msad6 != NULL && msad6->sin6_family == AF_INET6 &&
5443 IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr,
5444 &msad6->sin6_addr) &&
5445 sin6->sin6_port == msad6->sin6_port &&
5446 (tdsp->nfsclds_flags & NFSCLDS_DS) != 0 &&
5447 tdsp->nfsclds_sess.nfsess_defunct == 0) {
5452 tdsp = TAILQ_NEXT(tdsp, nfsclds_list);
5453 if (tdsp != NULL && tdsp->nfsclds_sockp != NULL)
5454 msad6 = (struct sockaddr_in6 *)
5455 tdsp->nfsclds_sockp->nr_nam;
5461 /* No IP address match, so look for new/trunked one. */
5462 sad6 = malloc(sizeof(*sad6), M_SONAME, M_WAITOK | M_ZERO);
5463 sad6->sin6_len = sizeof(*sad6);
5464 sad6->sin6_family = AF_INET6;
5465 sad6->sin6_port = sin6->sin6_port;
5466 NFSBCOPY(&sin6->sin6_addr, &sad6->sin6_addr,
5467 sizeof(struct in6_addr));
5468 nrp = malloc(sizeof(*nrp), M_NFSSOCKREQ, M_WAITOK | M_ZERO);
5469 nrp->nr_nam = (struct sockaddr *)sad6;
5473 nrp->nr_sotype = SOCK_STREAM;
5474 mtx_init(&nrp->nr_mtx, "nfssock", NULL, MTX_DEF);
5475 nrp->nr_prog = NFS_PROG;
5476 nrp->nr_vers = vers;
5479 * Use the credentials that were used for the mount, which are
5480 * in nmp->nm_sockreq.nr_cred for newnfs_connect() etc.
5481 * Ref. counting the credentials with crhold() is probably not
5482 * necessary, since nm_sockreq.nr_cred won't be crfree()'d until
5483 * unmount, but I did it anyhow.
5485 nrp->nr_cred = crhold(nmp->nm_sockreq.nr_cred);
5486 error = newnfs_connect(nmp, nrp, NULL, p, 0);
5487 NFSCL_DEBUG(3, "DS connect=%d\n", error);
5490 /* Now, do the exchangeid and create session. */
5492 if (vers == NFS_VER4) {
5493 error = nfsrpc_exchangeid(nmp, clp, nrp,
5494 NFSV4EXCH_USEPNFSDS, &dsp, nrp->nr_cred, p);
5495 NFSCL_DEBUG(3, "DS exchangeid=%d\n", error);
5497 newnfs_disconnect(nrp);
5499 dsp = malloc(sizeof(struct nfsclds), M_NFSCLDS,
5501 dsp->nfsclds_flags |= NFSCLDS_DS;
5502 dsp->nfsclds_expire = INT32_MAX; /* No renews needed. */
5503 mtx_init(&dsp->nfsclds_mtx, "nfsds", NULL, MTX_DEF);
5504 mtx_init(&dsp->nfsclds_sess.nfsess_mtx, "nfssession",
5509 dsp->nfsclds_sockp = nrp;
5510 if (vers == NFS_VER4) {
5512 retv = nfscl_getsameserver(nmp, dsp, &tdsp,
5514 NFSCL_DEBUG(3, "getsame ret=%d\n", retv);
5515 if (retv == NFSDSP_USETHISSESSION &&
5516 nfscl_dssameconn != 0) {
5518 tdsp->nfsclds_flags |= NFSCLDS_SAMECONN;
5522 * If there is already a session for this
5525 (void)newnfs_disconnect(nrp);
5526 nfscl_freenfsclds(dsp);
5530 if (retv == NFSDSP_NOTFOUND)
5532 dsp->nfsclds_sess.nfsess_sequenceid;
5534 error = nfsrpc_createsession(nmp, &dsp->nfsclds_sess,
5535 nrp, sequenceid, 0, nrp->nr_cred, p);
5536 NFSCL_DEBUG(3, "DS createsess=%d\n", error);
5539 NFSFREECRED(nrp->nr_cred);
5540 NFSFREEMUTEX(&nrp->nr_mtx);
5541 free(nrp->nr_nam, M_SONAME);
5542 free(nrp, M_NFSSOCKREQ);
5545 NFSCL_DEBUG(3, "add DS session\n");
5547 * Put it at the end of the list. That way the list
5548 * is ordered by when the entry was added. This matters
5549 * since the one done first is the one that should be
5550 * used for sequencid'ing any subsequent create sessions.
5553 TAILQ_INSERT_TAIL(&nmp->nm_sess, dsp, nfsclds_list);
5556 } else if (dsp != NULL) {
5557 newnfs_disconnect(nrp);
5558 nfscl_freenfsclds(dsp);
5564 * Do the NFSv4.1 Reclaim Complete.
5567 nfsrpc_reclaimcomplete(struct nfsmount *nmp, struct ucred *cred, NFSPROC_T *p)
5570 struct nfsrv_descript nfsd;
5571 struct nfsrv_descript *nd = &nfsd;
5574 nfscl_reqstart(nd, NFSPROC_RECLAIMCOMPL, nmp, NULL, 0, NULL, NULL, 0,
5576 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
5578 nd->nd_flag |= ND_USEGSSNAME;
5579 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
5580 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
5583 error = nd->nd_repstat;
5584 mbuf_freem(nd->nd_mrep);
5589 * Initialize the slot tables for a session.
5592 nfscl_initsessionslots(struct nfsclsession *sep)
5596 for (i = 0; i < NFSV4_CBSLOTS; i++) {
5597 if (sep->nfsess_cbslots[i].nfssl_reply != NULL)
5598 m_freem(sep->nfsess_cbslots[i].nfssl_reply);
5599 NFSBZERO(&sep->nfsess_cbslots[i], sizeof(struct nfsslot));
5601 for (i = 0; i < 64; i++)
5602 sep->nfsess_slotseq[i] = 0;
5603 sep->nfsess_slots = 0;
5607 * Called to try and do an I/O operation via an NFSv4.1 Data Server (DS).
5610 nfscl_doiods(vnode_t vp, struct uio *uiop, int *iomode, int *must_commit,
5611 uint32_t rwaccess, int docommit, struct ucred *cred, NFSPROC_T *p)
5613 struct nfsnode *np = VTONFS(vp);
5614 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
5615 struct nfscllayout *layp;
5616 struct nfscldevinfo *dip;
5617 struct nfsclflayout *rflp;
5619 struct nfsclwritedsdorpc *drpc, *tdrpc;
5620 nfsv4stateid_t stateid;
5621 struct ucred *newcred;
5622 uint64_t lastbyte, len, off, oresid, xfer;
5623 int eof, error, firstmirror, i, iolaymode, mirrorcnt, recalled, timo;
5626 void *iovbase = NULL;
5631 if (!NFSHASPNFS(nmp) || nfscl_enablecallb == 0 || nfs_numnfscbd == 0 ||
5632 (np->n_flag & NNOLAYOUT) != 0)
5634 /* Now, get a reference cnt on the clientid for this mount. */
5635 if (nfscl_getref(nmp) == 0)
5638 /* Find an appropriate stateid. */
5639 newcred = NFSNEWCRED(cred);
5640 error = nfscl_getstateid(vp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len,
5641 rwaccess, 1, newcred, p, &stateid, &lckp);
5643 NFSFREECRED(newcred);
5647 /* Search for a layout for this file. */
5648 off = uiop->uio_offset;
5649 layp = nfscl_getlayout(nmp->nm_clp, np->n_fhp->nfh_fh,
5650 np->n_fhp->nfh_len, off, &rflp, &recalled);
5651 if (layp == NULL || rflp == NULL) {
5652 if (recalled != 0) {
5653 NFSFREECRED(newcred);
5658 nfscl_rellayout(layp, (rflp == NULL) ? 1 : 0);
5661 /* Try and get a Layout, if it is supported. */
5662 if (rwaccess == NFSV4OPEN_ACCESSWRITE ||
5663 (np->n_flag & NWRITEOPENED) != 0)
5664 iolaymode = NFSLAYOUTIOMODE_RW;
5666 iolaymode = NFSLAYOUTIOMODE_READ;
5667 error = nfsrpc_getlayout(nmp, vp, np->n_fhp, iolaymode,
5668 NULL, &stateid, off, &layp, newcred, p);
5671 np->n_flag |= NNOLAYOUT;
5674 nfscl_lockderef(lckp);
5675 NFSFREECRED(newcred);
5677 nfscl_rellayout(layp, 0);
5684 * Loop around finding a layout that works for the first part of
5685 * this I/O operation, and then call the function that actually
5689 len = (uint64_t)uiop->uio_resid;
5690 while (len > 0 && error == 0 && eof == 0) {
5691 off = uiop->uio_offset;
5692 error = nfscl_findlayoutforio(layp, off, rwaccess, &rflp);
5694 oresid = xfer = (uint64_t)uiop->uio_resid;
5695 if (xfer > (rflp->nfsfl_end - rflp->nfsfl_off))
5696 xfer = rflp->nfsfl_end - rflp->nfsfl_off;
5698 * For Flex File layout with mirrored DSs, select one
5699 * of them at random for reads. For writes and commits,
5703 tdrpc = drpc = NULL;
5706 if ((layp->nfsly_flags & NFSLY_FLEXFILE) != 0 &&
5707 (mirrorcnt = rflp->nfsfl_mirrorcnt) > 1) {
5708 if (rwaccess == NFSV4OPEN_ACCESSREAD) {
5709 firstmirror = arc4random() % mirrorcnt;
5710 mirrorcnt = firstmirror + 1;
5712 if (docommit == 0) {
5714 * Save values, so uiop can be
5715 * rolled back upon a write
5718 offs = uiop->uio_offset;
5719 resid = uiop->uio_resid;
5721 uiop->uio_iov->iov_base;
5722 iovlen = uiop->uio_iov->iov_len;
5723 m = nfsm_uiombuflist(uiop, len,
5726 tdrpc = drpc = malloc(sizeof(*drpc) *
5727 (mirrorcnt - 1), M_TEMP, M_WAITOK |
5731 for (i = firstmirror; i < mirrorcnt && error == 0; i++){
5732 if ((layp->nfsly_flags & NFSLY_FLEXFILE) != 0) {
5733 dev = rflp->nfsfl_ffm[i].dev;
5734 dip = nfscl_getdevinfo(nmp->nm_clp, dev,
5735 rflp->nfsfl_ffm[i].devp);
5737 dev = rflp->nfsfl_dev;
5738 dip = nfscl_getdevinfo(nmp->nm_clp, dev,
5742 if ((rflp->nfsfl_flags & NFSFL_FLEXFILE)
5744 error = nfscl_dofflayoutio(vp,
5745 uiop, iomode, must_commit,
5746 &eof, &stateid, rwaccess,
5747 dip, layp, rflp, off, xfer,
5748 i, docommit, m, tdrpc,
5751 error = nfscl_doflayoutio(vp,
5752 uiop, iomode, must_commit,
5753 &eof, &stateid, rwaccess,
5754 dip, layp, rflp, off, xfer,
5755 docommit, newcred, p);
5756 nfscl_reldevinfo(dip);
5764 timo = hz / 50; /* Wait for 20msec. */
5767 for (i = firstmirror; i < mirrorcnt - 1 &&
5768 tdrpc != NULL; i++, tdrpc++) {
5770 * For the unused drpc entries, both inprog and
5771 * err == 0, so this loop won't break.
5773 while (tdrpc->inprog != 0 && tdrpc->done == 0)
5774 tsleep(&tdrpc->tsk, PVFS, "clrpcio",
5776 if (error == 0 && tdrpc->err != 0)
5781 if (mirrorcnt > 1 && rwaccess ==
5782 NFSV4OPEN_ACCESSWRITE && docommit == 0) {
5784 layp->nfsly_flags |= NFSLY_WRITTEN;
5787 lastbyte = off + xfer - 1;
5789 if (lastbyte > layp->nfsly_lastbyte)
5790 layp->nfsly_lastbyte = lastbyte;
5792 } else if (error == NFSERR_OPENMODE &&
5793 rwaccess == NFSV4OPEN_ACCESSREAD) {
5795 nmp->nm_state |= NFSSTA_OPENMODE;
5800 len -= (oresid - (uint64_t)uiop->uio_resid);
5801 else if (mirrorcnt > 1 && rwaccess ==
5802 NFSV4OPEN_ACCESSWRITE && docommit == 0) {
5804 * In case the rpc gets retried, roll the
5805 * uio fields changed by nfsm_uiombuflist()
5808 uiop->uio_offset = offs;
5809 uiop->uio_resid = resid;
5810 uiop->uio_iov->iov_base = iovbase;
5811 uiop->uio_iov->iov_len = iovlen;
5816 nfscl_lockderef(lckp);
5817 NFSFREECRED(newcred);
5818 nfscl_rellayout(layp, 0);
5824 * Make a copy of the mbuf chain and add an mbuf for null padding, as required.
5826 static struct mbuf *
5827 nfsm_copym(struct mbuf *m, int off, int xfer)
5829 struct mbuf *m2, *m3, *m4;
5833 m2 = m_copym(m, off, xfer, M_WAITOK);
5834 rem = NFSM_RNDUP(xfer) - xfer;
5837 * The zero padding to a multiple of 4 bytes is required by
5838 * the XDR. So that the mbufs copied by reference aren't
5839 * modified, add an mbuf with the zero'd bytes to the list.
5840 * rem will be a maximum of 3, so one zero'd uint32_t is
5844 while (m3->m_next != NULL)
5847 tl = NFSMTOD(m4, uint32_t *);
5849 mbuf_setlen(m4, rem);
5850 mbuf_setnext(m3, m4);
5856 * Find a file layout that will handle the first bytes of the requested
5857 * range and return the information from it needed to the I/O operation.
5860 nfscl_findlayoutforio(struct nfscllayout *lyp, uint64_t off, uint32_t rwaccess,
5861 struct nfsclflayout **retflpp)
5863 struct nfsclflayout *flp, *nflp, *rflp;
5868 /* For reading, do the Read list first and then the Write list. */
5870 if (rw == NFSV4OPEN_ACCESSREAD)
5871 flp = LIST_FIRST(&lyp->nfsly_flayread);
5873 flp = LIST_FIRST(&lyp->nfsly_flayrw);
5874 while (flp != NULL) {
5875 nflp = LIST_NEXT(flp, nfsfl_list);
5876 if (flp->nfsfl_off > off)
5878 if (flp->nfsfl_end > off &&
5879 (rflp == NULL || rflp->nfsfl_end < flp->nfsfl_end))
5883 if (rw == NFSV4OPEN_ACCESSREAD)
5884 rw = NFSV4OPEN_ACCESSWRITE;
5889 /* This one covers the most bytes starting at off. */
5897 * Do I/O using an NFSv4.1 file layout.
5900 nfscl_doflayoutio(vnode_t vp, struct uio *uiop, int *iomode, int *must_commit,
5901 int *eofp, nfsv4stateid_t *stateidp, int rwflag, struct nfscldevinfo *dp,
5902 struct nfscllayout *lyp, struct nfsclflayout *flp, uint64_t off,
5903 uint64_t len, int docommit, struct ucred *cred, NFSPROC_T *p)
5905 uint64_t io_off, rel_off, stripe_unit_size, transfer, xfer;
5906 int commit_thru_mds, error, stripe_index, stripe_pos;
5909 struct nfsclds **dspp;
5912 rel_off = off - flp->nfsfl_patoff;
5913 stripe_unit_size = (flp->nfsfl_util >> 6) & 0x3ffffff;
5914 stripe_pos = (rel_off / stripe_unit_size + flp->nfsfl_stripe1) %
5915 dp->nfsdi_stripecnt;
5916 transfer = stripe_unit_size - (rel_off % stripe_unit_size);
5919 /* Loop around, doing I/O for each stripe unit. */
5920 while (len > 0 && error == 0) {
5921 stripe_index = nfsfldi_stripeindex(dp, stripe_pos);
5922 dspp = nfsfldi_addr(dp, stripe_index);
5923 if (len > transfer && docommit == 0)
5927 if ((flp->nfsfl_util & NFSFLAYUTIL_DENSE) != 0) {
5929 if (stripe_pos >= flp->nfsfl_fhcnt)
5931 fhp = flp->nfsfl_fh[stripe_pos];
5932 io_off = (rel_off / (stripe_unit_size *
5933 dp->nfsdi_stripecnt)) * stripe_unit_size +
5934 rel_off % stripe_unit_size;
5936 /* Sparse layout. */
5937 if (flp->nfsfl_fhcnt > 1) {
5938 if (stripe_index >= flp->nfsfl_fhcnt)
5940 fhp = flp->nfsfl_fh[stripe_index];
5941 } else if (flp->nfsfl_fhcnt == 1)
5942 fhp = flp->nfsfl_fh[0];
5947 if ((flp->nfsfl_util & NFSFLAYUTIL_COMMIT_THRU_MDS) != 0) {
5948 commit_thru_mds = 1;
5952 commit_thru_mds = 0;
5954 np->n_flag |= NDSCOMMIT;
5957 if (docommit != 0) {
5959 error = nfsrpc_commitds(vp, io_off, xfer,
5960 *dspp, fhp, 0, 0, cred, p);
5963 * Set both eof and uio_resid = 0 to end any
5967 uiop->uio_resid = 0;
5970 np->n_flag &= ~NDSCOMMIT;
5973 } else if (rwflag == NFSV4OPEN_ACCESSREAD)
5974 error = nfsrpc_readds(vp, uiop, stateidp, eofp, *dspp,
5975 io_off, xfer, fhp, 0, 0, 0, cred, p);
5977 error = nfsrpc_writeds(vp, uiop, iomode, must_commit,
5978 stateidp, *dspp, io_off, xfer, fhp, commit_thru_mds,
5982 lyp->nfsly_flags |= NFSLY_WRITTEN;
5987 transfer = stripe_unit_size;
5988 stripe_pos = (stripe_pos + 1) % dp->nfsdi_stripecnt;
5997 * Do I/O using an NFSv4.1 flex file layout.
6000 nfscl_dofflayoutio(vnode_t vp, struct uio *uiop, int *iomode, int *must_commit,
6001 int *eofp, nfsv4stateid_t *stateidp, int rwflag, struct nfscldevinfo *dp,
6002 struct nfscllayout *lyp, struct nfsclflayout *flp, uint64_t off,
6003 uint64_t len, int mirror, int docommit, struct mbuf *mp,
6004 struct nfsclwritedsdorpc *drpc, struct ucred *cred, NFSPROC_T *p)
6006 uint64_t transfer, xfer;
6010 struct nfsclds **dspp;
6011 struct ucred *tcred;
6017 NFSCL_DEBUG(4, "nfscl_dofflayoutio: off=%ju len=%ju\n", (uintmax_t)off,
6019 /* Loop around, doing I/O for each stripe unit. */
6020 while (len > 0 && error == 0) {
6021 dspp = nfsfldi_addr(dp, 0);
6022 fhp = flp->nfsfl_ffm[mirror].fh[dp->nfsdi_versindex];
6023 stateidp = &flp->nfsfl_ffm[mirror].st;
6024 NFSCL_DEBUG(4, "mirror=%d vind=%d fhlen=%d st.seqid=0x%x\n",
6025 mirror, dp->nfsdi_versindex, fhp->nfh_len, stateidp->seqid);
6026 if ((dp->nfsdi_flags & NFSDI_TIGHTCOUPLED) == 0) {
6027 tcred = NFSNEWCRED(cred);
6028 tcred->cr_uid = flp->nfsfl_ffm[mirror].user;
6029 tcred->cr_groups[0] = flp->nfsfl_ffm[mirror].group;
6030 tcred->cr_ngroups = 1;
6033 if (rwflag == NFSV4OPEN_ACCESSREAD)
6034 transfer = dp->nfsdi_rsize;
6036 transfer = dp->nfsdi_wsize;
6038 np->n_flag |= NDSCOMMIT;
6040 if (len > transfer && docommit == 0)
6044 if (docommit != 0) {
6047 * Do last mirrored DS commit with this thread.
6049 if (mirror < flp->nfsfl_mirrorcnt - 1)
6050 error = nfsio_commitds(vp, off, xfer,
6051 *dspp, fhp, dp->nfsdi_vers,
6052 dp->nfsdi_minorvers, drpc, tcred,
6055 error = nfsrpc_commitds(vp, off, xfer,
6056 *dspp, fhp, dp->nfsdi_vers,
6057 dp->nfsdi_minorvers, tcred, p);
6058 NFSCL_DEBUG(4, "commitds=%d\n", error);
6059 if (error != 0 && error != EACCES && error !=
6062 "DS layreterr for commit\n");
6063 nfscl_dserr(NFSV4OP_COMMIT, error, dp,
6067 NFSCL_DEBUG(4, "aft nfsio_commitds=%d\n", error);
6070 * Set both eof and uio_resid = 0 to end any
6074 uiop->uio_resid = 0;
6077 np->n_flag &= ~NDSCOMMIT;
6080 } else if (rwflag == NFSV4OPEN_ACCESSREAD) {
6081 error = nfsrpc_readds(vp, uiop, stateidp, eofp, *dspp,
6082 off, xfer, fhp, 1, dp->nfsdi_vers,
6083 dp->nfsdi_minorvers, tcred, p);
6084 NFSCL_DEBUG(4, "readds=%d\n", error);
6085 if (error != 0 && error != EACCES && error != ESTALE) {
6086 NFSCL_DEBUG(4, "DS layreterr for read\n");
6087 nfscl_dserr(NFSV4OP_READ, error, dp, lyp,
6091 if (flp->nfsfl_mirrorcnt == 1) {
6092 error = nfsrpc_writeds(vp, uiop, iomode,
6093 must_commit, stateidp, *dspp, off, xfer,
6094 fhp, 0, 1, dp->nfsdi_vers,
6095 dp->nfsdi_minorvers, tcred, p);
6098 lyp->nfsly_flags |= NFSLY_WRITTEN;
6102 m = nfsm_copym(mp, rel_off, xfer);
6103 NFSCL_DEBUG(4, "mcopy reloff=%d xfer=%jd\n",
6104 rel_off, (uintmax_t)xfer);
6106 * Do the writes after the first loop iteration
6107 * and the write for the last mirror via this
6109 * This loop only iterates for small values
6110 * of nfsdi_wsize, which may never occur in
6111 * practice. However, the drpc is completely
6112 * used by the first iteration and, as such,
6113 * cannot be used after that.
6115 if (mirror < flp->nfsfl_mirrorcnt - 1 &&
6117 error = nfsio_writedsmir(vp, iomode,
6118 must_commit, stateidp, *dspp, off,
6119 xfer, fhp, m, dp->nfsdi_vers,
6120 dp->nfsdi_minorvers, drpc, tcred,
6123 error = nfsrpc_writedsmir(vp, iomode,
6124 must_commit, stateidp, *dspp, off,
6125 xfer, fhp, m, dp->nfsdi_vers,
6126 dp->nfsdi_minorvers, tcred, p);
6127 NFSCL_DEBUG(4, "nfsio_writedsmir=%d\n", error);
6128 if (error != 0 && error != EACCES && error !=
6131 "DS layreterr for write\n");
6132 nfscl_dserr(NFSV4OP_WRITE, error, dp,
6137 NFSCL_DEBUG(4, "aft read/writeds=%d\n", error);
6143 if ((dp->nfsdi_flags & NFSDI_TIGHTCOUPLED) == 0)
6146 NFSCL_DEBUG(4, "eo nfscl_dofflayoutio=%d\n", error);
6151 * The actual read RPC done to a DS.
6154 nfsrpc_readds(vnode_t vp, struct uio *uiop, nfsv4stateid_t *stateidp, int *eofp,
6155 struct nfsclds *dsp, uint64_t io_off, int len, struct nfsfh *fhp, int flex,
6156 int vers, int minorvers, struct ucred *cred, NFSPROC_T *p)
6159 int attrflag, error, retlen;
6160 struct nfsrv_descript nfsd;
6161 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
6162 struct nfsrv_descript *nd = &nfsd;
6163 struct nfssockreq *nrp;
6167 if (vers == 0 || vers == NFS_VER4) {
6168 nfscl_reqstart(nd, NFSPROC_READDS, nmp, fhp->nfh_fh,
6169 fhp->nfh_len, NULL, &dsp->nfsclds_sess, vers, minorvers);
6171 NFSCL_DEBUG(4, "nfsrpc_readds: vers4 minvers=%d\n", minorvers);
6173 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID);
6175 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSEQIDZERO);
6177 nfscl_reqstart(nd, NFSPROC_READ, nmp, fhp->nfh_fh,
6178 fhp->nfh_len, NULL, &dsp->nfsclds_sess, vers, minorvers);
6179 NFSCL_DEBUG(4, "nfsrpc_readds: vers3\n");
6181 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED * 3);
6182 txdr_hyper(io_off, tl);
6183 *(tl + 2) = txdr_unsigned(len);
6184 nrp = dsp->nfsclds_sockp;
6185 NFSCL_DEBUG(4, "nfsrpc_readds: nrp=%p\n", nrp);
6187 /* If NULL, use the MDS socket. */
6188 nrp = &nmp->nm_sockreq;
6189 error = newnfs_request(nd, nmp, NULL, nrp, vp, p, cred,
6190 NFS_PROG, vers, NULL, 1, NULL, &dsp->nfsclds_sess);
6191 NFSCL_DEBUG(4, "nfsrpc_readds: stat=%d err=%d\n", nd->nd_repstat,
6195 if (vers == NFS_VER3) {
6196 error = nfscl_postop_attr(nd, &na, &attrflag, NULL);
6197 NFSCL_DEBUG(4, "nfsrpc_readds: postop=%d\n", error);
6201 if (nd->nd_repstat != 0) {
6202 error = nd->nd_repstat;
6205 if (vers == NFS_VER3) {
6206 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
6207 *eofp = fxdr_unsigned(int, *(tl + 1));
6209 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
6210 *eofp = fxdr_unsigned(int, *tl);
6212 NFSM_STRSIZ(retlen, len);
6213 NFSCL_DEBUG(4, "nfsrpc_readds: retlen=%d eof=%d\n", retlen, *eofp);
6214 error = nfsm_mbufuio(nd, uiop, retlen);
6216 if (nd->nd_mrep != NULL)
6217 mbuf_freem(nd->nd_mrep);
6222 * The actual write RPC done to a DS.
6225 nfsrpc_writeds(vnode_t vp, struct uio *uiop, int *iomode, int *must_commit,
6226 nfsv4stateid_t *stateidp, struct nfsclds *dsp, uint64_t io_off, int len,
6227 struct nfsfh *fhp, int commit_thru_mds, int flex, int vers, int minorvers,
6228 struct ucred *cred, NFSPROC_T *p)
6231 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
6232 int attrflag, error, rlen, commit, committed = NFSWRITE_FILESYNC;
6234 struct nfsrv_descript nfsd;
6235 struct nfsrv_descript *nd = &nfsd;
6236 struct nfssockreq *nrp;
6239 KASSERT(uiop->uio_iovcnt == 1, ("nfs: writerpc iovcnt > 1"));
6241 if (vers == 0 || vers == NFS_VER4) {
6242 nfscl_reqstart(nd, NFSPROC_WRITEDS, nmp, fhp->nfh_fh,
6243 fhp->nfh_len, NULL, &dsp->nfsclds_sess, vers, minorvers);
6244 NFSCL_DEBUG(4, "nfsrpc_writeds: vers4 minvers=%d\n", minorvers);
6247 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID);
6249 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSEQIDZERO);
6250 NFSM_BUILD(tl, uint32_t *, NFSX_HYPER + 2 * NFSX_UNSIGNED);
6252 nfscl_reqstart(nd, NFSPROC_WRITE, nmp, fhp->nfh_fh,
6253 fhp->nfh_len, NULL, &dsp->nfsclds_sess, vers, minorvers);
6254 NFSCL_DEBUG(4, "nfsrpc_writeds: vers3\n");
6255 NFSM_BUILD(tl, uint32_t *, NFSX_HYPER + 3 * NFSX_UNSIGNED);
6257 txdr_hyper(io_off, tl);
6259 if (vers == NFS_VER3)
6260 *tl++ = txdr_unsigned(len);
6261 *tl++ = txdr_unsigned(*iomode);
6262 *tl = txdr_unsigned(len);
6263 nfsm_uiombuf(nd, uiop, len);
6264 nrp = dsp->nfsclds_sockp;
6266 /* If NULL, use the MDS socket. */
6267 nrp = &nmp->nm_sockreq;
6268 error = newnfs_request(nd, nmp, NULL, nrp, vp, p, cred,
6269 NFS_PROG, vers, NULL, 1, NULL, &dsp->nfsclds_sess);
6270 NFSCL_DEBUG(4, "nfsrpc_writeds: err=%d stat=%d\n", error,
6274 if (nd->nd_repstat != 0) {
6276 * In case the rpc gets retried, roll
6277 * the uio fileds changed by nfsm_uiombuf()
6280 uiop->uio_offset -= len;
6281 uio_uio_resid_add(uiop, len);
6282 uio_iov_base_add(uiop, -len);
6283 uio_iov_len_add(uiop, len);
6284 error = nd->nd_repstat;
6286 if (vers == NFS_VER3) {
6287 error = nfscl_wcc_data(nd, vp, &na, &attrflag, NULL,
6289 NFSCL_DEBUG(4, "nfsrpc_writeds: wcc_data=%d\n", error);
6293 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED + NFSX_VERF);
6294 rlen = fxdr_unsigned(int, *tl++);
6295 NFSCL_DEBUG(4, "nfsrpc_writeds: len=%d rlen=%d\n", len, rlen);
6299 } else if (rlen < len) {
6300 backup = len - rlen;
6301 uio_iov_base_add(uiop, -(backup));
6302 uio_iov_len_add(uiop, backup);
6303 uiop->uio_offset -= backup;
6304 uio_uio_resid_add(uiop, backup);
6307 commit = fxdr_unsigned(int, *tl++);
6310 * Return the lowest commitment level
6311 * obtained by any of the RPCs.
6313 if (committed == NFSWRITE_FILESYNC)
6315 else if (committed == NFSWRITE_DATASYNC &&
6316 commit == NFSWRITE_UNSTABLE)
6318 if (commit_thru_mds != 0) {
6320 if (!NFSHASWRITEVERF(nmp)) {
6321 NFSBCOPY(tl, nmp->nm_verf, NFSX_VERF);
6322 NFSSETWRITEVERF(nmp);
6323 } else if (NFSBCMP(tl, nmp->nm_verf, NFSX_VERF)) {
6325 NFSBCOPY(tl, nmp->nm_verf, NFSX_VERF);
6330 if ((dsp->nfsclds_flags & NFSCLDS_HASWRITEVERF) == 0) {
6331 NFSBCOPY(tl, dsp->nfsclds_verf, NFSX_VERF);
6332 dsp->nfsclds_flags |= NFSCLDS_HASWRITEVERF;
6333 } else if (NFSBCMP(tl, dsp->nfsclds_verf, NFSX_VERF)) {
6335 NFSBCOPY(tl, dsp->nfsclds_verf, NFSX_VERF);
6341 if (nd->nd_mrep != NULL)
6342 mbuf_freem(nd->nd_mrep);
6343 *iomode = committed;
6344 if (nd->nd_repstat != 0 && error == 0)
6345 error = nd->nd_repstat;
6350 * The actual write RPC done to a DS.
6351 * This variant is called from a separate kernel process for mirrors.
6352 * Any short write is considered an IO error.
6355 nfsrpc_writedsmir(vnode_t vp, int *iomode, int *must_commit,
6356 nfsv4stateid_t *stateidp, struct nfsclds *dsp, uint64_t io_off, int len,
6357 struct nfsfh *fhp, struct mbuf *m, int vers, int minorvers,
6358 struct ucred *cred, NFSPROC_T *p)
6361 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
6362 int attrflag, error, commit, committed = NFSWRITE_FILESYNC, rlen;
6363 struct nfsrv_descript nfsd;
6364 struct nfsrv_descript *nd = &nfsd;
6365 struct nfssockreq *nrp;
6369 if (vers == 0 || vers == NFS_VER4) {
6370 nfscl_reqstart(nd, NFSPROC_WRITEDS, nmp, fhp->nfh_fh,
6371 fhp->nfh_len, NULL, &dsp->nfsclds_sess, vers, minorvers);
6373 NFSCL_DEBUG(4, "nfsrpc_writedsmir: vers4 minvers=%d\n",
6375 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID);
6376 NFSM_BUILD(tl, uint32_t *, NFSX_HYPER + 2 * NFSX_UNSIGNED);
6378 nfscl_reqstart(nd, NFSPROC_WRITE, nmp, fhp->nfh_fh,
6379 fhp->nfh_len, NULL, &dsp->nfsclds_sess, vers, minorvers);
6380 NFSCL_DEBUG(4, "nfsrpc_writedsmir: vers3\n");
6381 NFSM_BUILD(tl, uint32_t *, NFSX_HYPER + 3 * NFSX_UNSIGNED);
6383 txdr_hyper(io_off, tl);
6385 if (vers == NFS_VER3)
6386 *tl++ = txdr_unsigned(len);
6387 *tl++ = txdr_unsigned(*iomode);
6388 *tl = txdr_unsigned(len);
6390 /* Put data in mbuf chain. */
6391 nd->nd_mb->m_next = m;
6392 /* Set nd_mb and nd_bpos to end of data. */
6393 while (m->m_next != NULL)
6396 nd->nd_bpos = mtod(m, char *) + m->m_len;
6397 NFSCL_DEBUG(4, "nfsrpc_writedsmir: lastmb len=%d\n", m->m_len);
6399 nrp = dsp->nfsclds_sockp;
6401 /* If NULL, use the MDS socket. */
6402 nrp = &nmp->nm_sockreq;
6403 error = newnfs_request(nd, nmp, NULL, nrp, vp, p, cred,
6404 NFS_PROG, vers, NULL, 1, NULL, &dsp->nfsclds_sess);
6405 NFSCL_DEBUG(4, "nfsrpc_writedsmir: err=%d stat=%d\n", error,
6409 if (nd->nd_repstat != 0)
6410 error = nd->nd_repstat;
6412 if (vers == NFS_VER3) {
6413 error = nfscl_wcc_data(nd, vp, &na, &attrflag, NULL,
6415 NFSCL_DEBUG(4, "nfsrpc_writedsmir: wcc_data=%d\n",
6420 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED + NFSX_VERF);
6421 rlen = fxdr_unsigned(int, *tl++);
6422 NFSCL_DEBUG(4, "nfsrpc_writedsmir: len=%d rlen=%d\n", len,
6426 NFSCL_DEBUG(4, "nfsrpc_writedsmir: len=%d rlen=%d\n",
6430 commit = fxdr_unsigned(int, *tl++);
6433 * Return the lowest commitment level
6434 * obtained by any of the RPCs.
6436 if (committed == NFSWRITE_FILESYNC)
6438 else if (committed == NFSWRITE_DATASYNC &&
6439 commit == NFSWRITE_UNSTABLE)
6442 if ((dsp->nfsclds_flags & NFSCLDS_HASWRITEVERF) == 0) {
6443 NFSBCOPY(tl, dsp->nfsclds_verf, NFSX_VERF);
6444 dsp->nfsclds_flags |= NFSCLDS_HASWRITEVERF;
6445 } else if (NFSBCMP(tl, dsp->nfsclds_verf, NFSX_VERF)) {
6447 NFSBCOPY(tl, dsp->nfsclds_verf, NFSX_VERF);
6452 if (nd->nd_mrep != NULL)
6453 mbuf_freem(nd->nd_mrep);
6454 *iomode = committed;
6455 if (nd->nd_repstat != 0 && error == 0)
6456 error = nd->nd_repstat;
6461 * Start up the thread that will execute nfsrpc_writedsmir().
6464 start_writedsmir(void *arg, int pending)
6466 struct nfsclwritedsdorpc *drpc;
6468 drpc = (struct nfsclwritedsdorpc *)arg;
6469 drpc->err = nfsrpc_writedsmir(drpc->vp, &drpc->iomode,
6470 &drpc->must_commit, drpc->stateidp, drpc->dsp, drpc->off, drpc->len,
6471 drpc->fhp, drpc->m, drpc->vers, drpc->minorvers, drpc->cred,
6474 NFSCL_DEBUG(4, "start_writedsmir: err=%d\n", drpc->err);
6478 * Set up the write DS mirror call for the pNFS I/O thread.
6481 nfsio_writedsmir(vnode_t vp, int *iomode, int *must_commit,
6482 nfsv4stateid_t *stateidp, struct nfsclds *dsp, uint64_t off, int len,
6483 struct nfsfh *fhp, struct mbuf *m, int vers, int minorvers,
6484 struct nfsclwritedsdorpc *drpc, struct ucred *cred, NFSPROC_T *p)
6491 drpc->iomode = *iomode;
6492 drpc->must_commit = *must_commit;
6493 drpc->stateidp = stateidp;
6500 drpc->minorvers = minorvers;
6505 if (nfs_pnfsiothreads != 0) {
6506 ret = nfs_pnfsio(start_writedsmir, drpc);
6507 NFSCL_DEBUG(4, "nfsio_writedsmir: nfs_pnfsio=%d\n", ret);
6510 error = nfsrpc_writedsmir(vp, iomode, must_commit, stateidp,
6511 dsp, off, len, fhp, m, vers, minorvers, cred, p);
6512 NFSCL_DEBUG(4, "nfsio_writedsmir: error=%d\n", error);
6517 * Free up the nfsclds structure.
6520 nfscl_freenfsclds(struct nfsclds *dsp)
6526 if (dsp->nfsclds_sockp != NULL) {
6527 NFSFREECRED(dsp->nfsclds_sockp->nr_cred);
6528 NFSFREEMUTEX(&dsp->nfsclds_sockp->nr_mtx);
6529 free(dsp->nfsclds_sockp->nr_nam, M_SONAME);
6530 free(dsp->nfsclds_sockp, M_NFSSOCKREQ);
6532 NFSFREEMUTEX(&dsp->nfsclds_mtx);
6533 NFSFREEMUTEX(&dsp->nfsclds_sess.nfsess_mtx);
6534 for (i = 0; i < NFSV4_CBSLOTS; i++) {
6535 if (dsp->nfsclds_sess.nfsess_cbslots[i].nfssl_reply != NULL)
6537 dsp->nfsclds_sess.nfsess_cbslots[i].nfssl_reply);
6539 free(dsp, M_NFSCLDS);
6542 static enum nfsclds_state
6543 nfscl_getsameserver(struct nfsmount *nmp, struct nfsclds *newdsp,
6544 struct nfsclds **retdspp, uint32_t *sequencep)
6546 struct nfsclds *dsp;
6550 * Search the list of nfsclds structures for one with the same
6554 TAILQ_FOREACH(dsp, &nmp->nm_sess, nfsclds_list) {
6555 if (dsp->nfsclds_servownlen == newdsp->nfsclds_servownlen &&
6556 dsp->nfsclds_servownlen != 0 &&
6557 !NFSBCMP(dsp->nfsclds_serverown, newdsp->nfsclds_serverown,
6558 dsp->nfsclds_servownlen) &&
6559 dsp->nfsclds_sess.nfsess_defunct == 0) {
6560 NFSCL_DEBUG(4, "fnd same fdsp=%p dsp=%p flg=0x%x\n",
6561 TAILQ_FIRST(&nmp->nm_sess), dsp,
6562 dsp->nfsclds_flags);
6564 /* Get sequenceid# from first entry. */
6566 dsp->nfsclds_sess.nfsess_sequenceid;
6569 /* Server major id matches. */
6570 if ((dsp->nfsclds_flags & NFSCLDS_DS) != 0) {
6572 return (NFSDSP_USETHISSESSION);
6578 return (NFSDSP_SEQTHISSESSION);
6579 return (NFSDSP_NOTFOUND);
6583 * NFS commit rpc to a NFSv4.1 DS.
6586 nfsrpc_commitds(vnode_t vp, uint64_t offset, int cnt, struct nfsclds *dsp,
6587 struct nfsfh *fhp, int vers, int minorvers, struct ucred *cred,
6591 struct nfsrv_descript nfsd, *nd = &nfsd;
6592 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
6593 struct nfssockreq *nrp;
6595 int attrflag, error;
6598 if (vers == 0 || vers == NFS_VER4) {
6599 nfscl_reqstart(nd, NFSPROC_COMMITDS, nmp, fhp->nfh_fh,
6600 fhp->nfh_len, NULL, &dsp->nfsclds_sess, vers, minorvers);
6603 nfscl_reqstart(nd, NFSPROC_COMMIT, nmp, fhp->nfh_fh,
6604 fhp->nfh_len, NULL, &dsp->nfsclds_sess, vers, minorvers);
6605 NFSCL_DEBUG(4, "nfsrpc_commitds: vers=%d minvers=%d\n", vers,
6607 NFSM_BUILD(tl, uint32_t *, NFSX_HYPER + NFSX_UNSIGNED);
6608 txdr_hyper(offset, tl);
6610 *tl = txdr_unsigned(cnt);
6611 nrp = dsp->nfsclds_sockp;
6613 /* If NULL, use the MDS socket. */
6614 nrp = &nmp->nm_sockreq;
6615 error = newnfs_request(nd, nmp, NULL, nrp, vp, p, cred,
6616 NFS_PROG, vers, NULL, 1, NULL, &dsp->nfsclds_sess);
6617 NFSCL_DEBUG(4, "nfsrpc_commitds: err=%d stat=%d\n", error,
6621 if (nd->nd_repstat == 0) {
6622 if (vers == NFS_VER3) {
6623 error = nfscl_wcc_data(nd, vp, &na, &attrflag, NULL,
6625 NFSCL_DEBUG(4, "nfsrpc_commitds: wccdata=%d\n", error);
6629 NFSM_DISSECT(tl, u_int32_t *, NFSX_VERF);
6631 if (NFSBCMP(tl, dsp->nfsclds_verf, NFSX_VERF)) {
6632 NFSBCOPY(tl, dsp->nfsclds_verf, NFSX_VERF);
6633 error = NFSERR_STALEWRITEVERF;
6638 if (error == 0 && nd->nd_repstat != 0)
6639 error = nd->nd_repstat;
6640 mbuf_freem(nd->nd_mrep);
6645 * Start up the thread that will execute nfsrpc_commitds().
6648 start_commitds(void *arg, int pending)
6650 struct nfsclwritedsdorpc *drpc;
6652 drpc = (struct nfsclwritedsdorpc *)arg;
6653 drpc->err = nfsrpc_commitds(drpc->vp, drpc->off, drpc->len,
6654 drpc->dsp, drpc->fhp, drpc->vers, drpc->minorvers, drpc->cred,
6657 NFSCL_DEBUG(4, "start_commitds: err=%d\n", drpc->err);
6661 * Set up the commit DS mirror call for the pNFS I/O thread.
6664 nfsio_commitds(vnode_t vp, uint64_t offset, int cnt, struct nfsclds *dsp,
6665 struct nfsfh *fhp, int vers, int minorvers,
6666 struct nfsclwritedsdorpc *drpc, struct ucred *cred, NFSPROC_T *p)
6678 drpc->minorvers = minorvers;
6683 if (nfs_pnfsiothreads != 0) {
6684 ret = nfs_pnfsio(start_commitds, drpc);
6685 NFSCL_DEBUG(4, "nfsio_commitds: nfs_pnfsio=%d\n", ret);
6688 error = nfsrpc_commitds(vp, offset, cnt, dsp, fhp, vers,
6689 minorvers, cred, p);
6690 NFSCL_DEBUG(4, "nfsio_commitds: error=%d\n", error);
6695 * Set up the XDR arguments for the LayoutGet operation.
6698 nfsrv_setuplayoutget(struct nfsrv_descript *nd, int iomode, uint64_t offset,
6699 uint64_t len, uint64_t minlen, nfsv4stateid_t *stateidp, int layouttype,
6700 int layoutlen, int usecurstateid)
6704 NFSM_BUILD(tl, uint32_t *, 4 * NFSX_UNSIGNED + 3 * NFSX_HYPER +
6706 *tl++ = newnfs_false; /* Don't signal availability. */
6707 *tl++ = txdr_unsigned(layouttype);
6708 *tl++ = txdr_unsigned(iomode);
6709 txdr_hyper(offset, tl);
6711 txdr_hyper(len, tl);
6713 txdr_hyper(minlen, tl);
6715 if (usecurstateid != 0) {
6716 /* Special stateid for Current stateid. */
6717 *tl++ = txdr_unsigned(1);
6722 *tl++ = txdr_unsigned(stateidp->seqid);
6723 NFSCL_DEBUG(4, "layget seq=%d\n", (int)stateidp->seqid);
6724 *tl++ = stateidp->other[0];
6725 *tl++ = stateidp->other[1];
6726 *tl++ = stateidp->other[2];
6728 *tl = txdr_unsigned(layoutlen);
6732 * Parse the reply for a successful LayoutGet operation.
6735 nfsrv_parselayoutget(struct nfsrv_descript *nd, nfsv4stateid_t *stateidp,
6736 int *retonclosep, struct nfsclflayouthead *flhp)
6739 struct nfsclflayout *flp, *prevflp, *tflp;
6740 int cnt, error, fhcnt, gotiomode, i, iomode, j, k, l, laytype, nfhlen;
6742 uint64_t retlen, off;
6748 NFSCL_DEBUG(4, "in nfsrv_parselayoutget\n");
6752 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED + NFSX_STATEID);
6757 stateidp->seqid = fxdr_unsigned(uint32_t, *tl++);
6758 NFSCL_DEBUG(4, "retoncls=%d stseq=%d\n", *retonclosep,
6759 (int)stateidp->seqid);
6760 stateidp->other[0] = *tl++;
6761 stateidp->other[1] = *tl++;
6762 stateidp->other[2] = *tl++;
6763 cnt = fxdr_unsigned(int, *tl);
6764 NFSCL_DEBUG(4, "layg cnt=%d\n", cnt);
6765 if (cnt <= 0 || cnt > 10000) {
6766 /* Don't accept more than 10000 layouts in reply. */
6767 error = NFSERR_BADXDR;
6770 for (i = 0; i < cnt; i++) {
6771 /* Dissect to the layout type. */
6772 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_HYPER +
6774 off = fxdr_hyper(tl); tl += 2;
6775 retlen = fxdr_hyper(tl); tl += 2;
6776 iomode = fxdr_unsigned(int, *tl++);
6777 laytype = fxdr_unsigned(int, *tl);
6778 NFSCL_DEBUG(4, "layt=%d off=%ju len=%ju iom=%d\n", laytype,
6779 (uintmax_t)off, (uintmax_t)retlen, iomode);
6780 /* Ignore length of layout body for now. */
6781 if (laytype == NFSLAYOUT_NFSV4_1_FILES) {
6782 /* Parse the File layout up to fhcnt. */
6783 NFSM_DISSECT(tl, uint32_t *, 3 * NFSX_UNSIGNED +
6784 NFSX_HYPER + NFSX_V4DEVICEID);
6785 fhcnt = fxdr_unsigned(int, *(tl + 4 +
6786 NFSX_V4DEVICEID / NFSX_UNSIGNED));
6787 NFSCL_DEBUG(4, "fhcnt=%d\n", fhcnt);
6788 if (fhcnt < 0 || fhcnt > 100) {
6789 /* Don't accept more than 100 file handles. */
6790 error = NFSERR_BADXDR;
6794 flp = malloc(sizeof(*flp) + fhcnt *
6795 sizeof(struct nfsfh *), M_NFSFLAYOUT,
6798 flp = malloc(sizeof(*flp), M_NFSFLAYOUT,
6800 flp->nfsfl_flags = NFSFL_FILE;
6801 flp->nfsfl_fhcnt = 0;
6802 flp->nfsfl_devp = NULL;
6803 flp->nfsfl_off = off;
6804 if (flp->nfsfl_off + retlen < flp->nfsfl_off)
6805 flp->nfsfl_end = UINT64_MAX - flp->nfsfl_off;
6807 flp->nfsfl_end = flp->nfsfl_off + retlen;
6808 flp->nfsfl_iomode = iomode;
6809 if (gotiomode == -1)
6810 gotiomode = flp->nfsfl_iomode;
6811 /* Ignore layout body length for now. */
6812 NFSBCOPY(tl, flp->nfsfl_dev, NFSX_V4DEVICEID);
6813 tl += (NFSX_V4DEVICEID / NFSX_UNSIGNED);
6814 flp->nfsfl_util = fxdr_unsigned(uint32_t, *tl++);
6815 NFSCL_DEBUG(4, "flutil=0x%x\n", flp->nfsfl_util);
6816 flp->nfsfl_stripe1 = fxdr_unsigned(uint32_t, *tl++);
6817 flp->nfsfl_patoff = fxdr_hyper(tl); tl += 2;
6818 NFSCL_DEBUG(4, "stripe1=%u poff=%ju\n",
6819 flp->nfsfl_stripe1, (uintmax_t)flp->nfsfl_patoff);
6820 for (j = 0; j < fhcnt; j++) {
6821 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
6822 nfhlen = fxdr_unsigned(int, *tl);
6823 if (nfhlen <= 0 || nfhlen > NFSX_V4FHMAX) {
6824 error = NFSERR_BADXDR;
6827 nfhp = malloc(sizeof(*nfhp) + nfhlen - 1,
6829 flp->nfsfl_fh[j] = nfhp;
6831 nfhp->nfh_len = nfhlen;
6832 NFSM_DISSECT(cp, uint8_t *, NFSM_RNDUP(nfhlen));
6833 NFSBCOPY(cp, nfhp->nfh_fh, nfhlen);
6835 } else if (laytype == NFSLAYOUT_FLEXFILE) {
6836 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED +
6838 mirrorcnt = fxdr_unsigned(int, *(tl + 2));
6839 NFSCL_DEBUG(4, "mirrorcnt=%d\n", mirrorcnt);
6840 if (mirrorcnt < 1 || mirrorcnt > NFSDEV_MAXMIRRORS) {
6841 error = NFSERR_BADXDR;
6844 flp = malloc(sizeof(*flp) + mirrorcnt *
6845 sizeof(struct nfsffm), M_NFSFLAYOUT, M_WAITOK);
6846 flp->nfsfl_flags = NFSFL_FLEXFILE;
6847 flp->nfsfl_mirrorcnt = mirrorcnt;
6848 for (j = 0; j < mirrorcnt; j++)
6849 flp->nfsfl_ffm[j].devp = NULL;
6850 flp->nfsfl_off = off;
6851 if (flp->nfsfl_off + retlen < flp->nfsfl_off)
6852 flp->nfsfl_end = UINT64_MAX - flp->nfsfl_off;
6854 flp->nfsfl_end = flp->nfsfl_off + retlen;
6855 flp->nfsfl_iomode = iomode;
6856 if (gotiomode == -1)
6857 gotiomode = flp->nfsfl_iomode;
6858 flp->nfsfl_stripeunit = fxdr_hyper(tl);
6859 NFSCL_DEBUG(4, "stripeunit=%ju\n",
6860 (uintmax_t)flp->nfsfl_stripeunit);
6861 for (j = 0; j < mirrorcnt; j++) {
6862 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
6863 k = fxdr_unsigned(int, *tl);
6864 if (k < 1 || k > 128) {
6865 error = NFSERR_BADXDR;
6868 NFSCL_DEBUG(4, "servercnt=%d\n", k);
6869 for (l = 0; l < k; l++) {
6870 NFSM_DISSECT(tl, uint32_t *,
6871 NFSX_V4DEVICEID + NFSX_STATEID +
6874 /* Just use the first server. */
6876 flp->nfsfl_ffm[j].dev,
6878 tl += (NFSX_V4DEVICEID /
6881 flp->nfsfl_ffm[j].st.seqid =
6883 flp->nfsfl_ffm[j].st.other[0] =
6885 flp->nfsfl_ffm[j].st.other[1] =
6887 flp->nfsfl_ffm[j].st.other[2] =
6889 NFSCL_DEBUG(4, "st.seqid=%u "
6890 "st.o0=0x%x st.o1=0x%x "
6892 flp->nfsfl_ffm[j].st.seqid,
6893 flp->nfsfl_ffm[j].st.other[0],
6894 flp->nfsfl_ffm[j].st.other[1],
6895 flp->nfsfl_ffm[j].st.other[2]);
6897 tl += ((NFSX_V4DEVICEID +
6901 fhcnt = fxdr_unsigned(int, *tl);
6902 NFSCL_DEBUG(4, "fhcnt=%d\n", fhcnt);
6904 fhcnt > NFSDEV_MAXVERS) {
6905 error = NFSERR_BADXDR;
6908 for (m = 0; m < fhcnt; m++) {
6909 NFSM_DISSECT(tl, uint32_t *,
6911 nfhlen = fxdr_unsigned(int,
6913 NFSCL_DEBUG(4, "nfhlen=%d\n",
6915 if (nfhlen <= 0 || nfhlen >
6917 error = NFSERR_BADXDR;
6920 NFSM_DISSECT(cp, uint8_t *,
6921 NFSM_RNDUP(nfhlen));
6923 flp->nfsfl_ffm[j].fhcnt
6927 nfhlen - 1, M_NFSFH,
6929 flp->nfsfl_ffm[j].fh[m]
6931 nfhp->nfh_len = nfhlen;
6939 /* Now, get the ffsd_user/ffds_group. */
6940 error = nfsrv_parseug(nd, 0, &user,
6942 NFSCL_DEBUG(4, "after parseu=%d\n",
6945 error = nfsrv_parseug(nd, 1,
6946 &user, &grp, curthread);
6947 NFSCL_DEBUG(4, "aft parseg=%d\n",
6951 NFSCL_DEBUG(4, "user=%d group=%d\n",
6954 flp->nfsfl_ffm[j].user = user;
6955 flp->nfsfl_ffm[j].group = grp;
6957 "usr=%d grp=%d\n", user,
6962 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
6963 flp->nfsfl_fflags = fxdr_unsigned(uint32_t, *tl++);
6964 flp->nfsfl_statshint = fxdr_unsigned(uint32_t, *tl);
6965 NFSCL_DEBUG(4, "fflags=0x%x statshint=%d\n",
6966 flp->nfsfl_fflags, flp->nfsfl_statshint);
6968 error = NFSERR_BADXDR;
6971 if (flp->nfsfl_iomode == gotiomode) {
6972 /* Keep the list in increasing offset order. */
6973 tflp = LIST_FIRST(flhp);
6975 while (tflp != NULL &&
6976 tflp->nfsfl_off < flp->nfsfl_off) {
6978 tflp = LIST_NEXT(tflp, nfsfl_list);
6980 if (prevflp == NULL)
6981 LIST_INSERT_HEAD(flhp, flp, nfsfl_list);
6983 LIST_INSERT_AFTER(prevflp, flp,
6985 NFSCL_DEBUG(4, "flp inserted\n");
6987 printf("nfscl_layoutget(): got wrong iomode\n");
6988 nfscl_freeflayout(flp);
6993 NFSCL_DEBUG(4, "eo nfsrv_parselayoutget=%d\n", error);
6994 if (error != 0 && flp != NULL)
6995 nfscl_freeflayout(flp);
7000 * Parse a user/group digit string.
7003 nfsrv_parseug(struct nfsrv_descript *nd, int dogrp, uid_t *uidp, gid_t *gidp,
7007 char *cp, *str, str0[NFSV4_SMALLSTR + 1];
7011 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
7012 len = fxdr_unsigned(uint32_t, *tl);
7014 if (len > NFSV4_OPAQUELIMIT) {
7015 error = NFSERR_BADXDR;
7018 NFSCL_DEBUG(4, "nfsrv_parseug: len=%d\n", len);
7021 *gidp = GID_NOGROUP;
7026 if (len > NFSV4_SMALLSTR)
7027 str = malloc(len + 1, M_TEMP, M_WAITOK);
7030 NFSM_DISSECT(cp, char *, NFSM_RNDUP(len));
7031 NFSBCOPY(cp, str, len);
7033 NFSCL_DEBUG(4, "nfsrv_parseug: str=%s\n", str);
7035 error = nfsv4_strtogid(nd, str, len, gidp, p);
7037 error = nfsv4_strtouid(nd, str, len, uidp, p);
7039 if (len > NFSV4_SMALLSTR)
7041 NFSCL_DEBUG(4, "eo nfsrv_parseug=%d\n", error);
7046 * Similar to nfsrpc_getlayout(), except that it uses nfsrpc_openlayget(),
7047 * so that it does both an Open and a Layoutget.
7050 nfsrpc_getopenlayout(struct nfsmount *nmp, vnode_t vp, u_int8_t *nfhp,
7051 int fhlen, uint8_t *newfhp, int newfhlen, uint32_t mode,
7052 struct nfsclopen *op, uint8_t *name, int namelen, struct nfscldeleg **dpp,
7053 struct ucred *cred, NFSPROC_T *p)
7055 struct nfscllayout *lyp;
7056 struct nfsclflayout *flp;
7057 struct nfsclflayouthead flh;
7058 int error, islocked, layoutlen, recalled, retonclose, usecurstateid;
7059 int layouttype, laystat;
7060 nfsv4stateid_t stateid;
7061 struct nfsclsession *tsep;
7064 if (NFSHASFLEXFILE(nmp))
7065 layouttype = NFSLAYOUT_FLEXFILE;
7067 layouttype = NFSLAYOUT_NFSV4_1_FILES;
7069 * If lyp is returned non-NULL, there will be a refcnt (shared lock)
7070 * on it, iff flp != NULL or a lock (exclusive lock) on it iff
7073 lyp = nfscl_getlayout(nmp->nm_clp, newfhp, newfhlen, 0, &flp,
7075 NFSCL_DEBUG(4, "nfsrpc_getopenlayout nfscl_getlayout lyp=%p\n", lyp);
7078 else if (flp != NULL)
7082 if ((lyp == NULL || flp == NULL) && recalled == 0) {
7084 tsep = nfsmnt_mdssession(nmp);
7085 layoutlen = tsep->nfsess_maxcache - (NFSX_STATEID +
7091 stateid.seqid = lyp->nfsly_stateid.seqid;
7092 stateid.other[0] = lyp->nfsly_stateid.other[0];
7093 stateid.other[1] = lyp->nfsly_stateid.other[1];
7094 stateid.other[2] = lyp->nfsly_stateid.other[2];
7096 error = nfsrpc_openlayoutrpc(nmp, vp, nfhp, fhlen,
7097 newfhp, newfhlen, mode, op, name, namelen,
7098 dpp, &stateid, usecurstateid, layouttype, layoutlen,
7099 &retonclose, &flh, &laystat, cred, p);
7100 NFSCL_DEBUG(4, "aft nfsrpc_openlayoutrpc laystat=%d err=%d\n",
7102 laystat = nfsrpc_layoutgetres(nmp, vp, newfhp, newfhlen,
7103 &stateid, retonclose, NULL, &lyp, &flh, layouttype, laystat,
7104 &islocked, cred, p);
7106 error = nfsrpc_openrpc(nmp, vp, nfhp, fhlen, newfhp, newfhlen,
7107 mode, op, name, namelen, dpp, 0, 0, cred, p, 0, 0);
7109 nfscl_rellayout(lyp, 1);
7110 else if (islocked == 1)
7111 nfscl_rellayout(lyp, 0);
7116 * This function does an Open+LayoutGet for an NFSv4.1 mount with pNFS
7117 * enabled, only for the CLAIM_NULL case. All other NFSv4 Opens are
7118 * handled by nfsrpc_openrpc().
7119 * For the case where op == NULL, dvp is the directory. When op != NULL, it
7123 nfsrpc_openlayoutrpc(struct nfsmount *nmp, vnode_t vp, u_int8_t *nfhp,
7124 int fhlen, uint8_t *newfhp, int newfhlen, uint32_t mode,
7125 struct nfsclopen *op, uint8_t *name, int namelen, struct nfscldeleg **dpp,
7126 nfsv4stateid_t *stateidp, int usecurstateid, int layouttype,
7127 int layoutlen, int *retonclosep, struct nfsclflayouthead *flhp,
7128 int *laystatp, struct ucred *cred, NFSPROC_T *p)
7131 struct nfsrv_descript nfsd, *nd = &nfsd;
7132 struct nfscldeleg *ndp = NULL;
7133 struct nfsvattr nfsva;
7134 struct nfsclsession *tsep;
7135 uint32_t rflags, deleg;
7136 nfsattrbit_t attrbits;
7137 int error, ret, acesize, limitby, iomode;
7141 nfscl_reqstart(nd, NFSPROC_OPENLAYGET, nmp, nfhp, fhlen, NULL, NULL,
7143 NFSM_BUILD(tl, uint32_t *, 5 * NFSX_UNSIGNED);
7144 *tl++ = txdr_unsigned(op->nfso_own->nfsow_seqid);
7145 *tl++ = txdr_unsigned(mode & NFSV4OPEN_ACCESSBOTH);
7146 *tl++ = txdr_unsigned((mode >> NFSLCK_SHIFT) & NFSV4OPEN_DENYBOTH);
7147 tsep = nfsmnt_mdssession(nmp);
7148 *tl++ = tsep->nfsess_clientid.lval[0];
7149 *tl = tsep->nfsess_clientid.lval[1];
7150 nfsm_strtom(nd, op->nfso_own->nfsow_owner, NFSV4CL_LOCKNAMELEN);
7151 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_UNSIGNED);
7152 *tl++ = txdr_unsigned(NFSV4OPEN_NOCREATE);
7153 *tl = txdr_unsigned(NFSV4OPEN_CLAIMNULL);
7154 nfsm_strtom(nd, name, namelen);
7155 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
7156 *tl = txdr_unsigned(NFSV4OP_GETATTR);
7157 NFSZERO_ATTRBIT(&attrbits);
7158 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_CHANGE);
7159 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEMODIFY);
7160 nfsrv_putattrbit(nd, &attrbits);
7161 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
7162 *tl = txdr_unsigned(NFSV4OP_LAYOUTGET);
7163 if ((mode & NFSV4OPEN_ACCESSWRITE) != 0)
7164 iomode = NFSLAYOUTIOMODE_RW;
7166 iomode = NFSLAYOUTIOMODE_READ;
7167 nfsrv_setuplayoutget(nd, iomode, 0, UINT64_MAX, 0, stateidp,
7168 layouttype, layoutlen, usecurstateid);
7169 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, vp, p, cred,
7170 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
7173 NFSCL_INCRSEQID(op->nfso_own->nfsow_seqid, nd);
7174 if (nd->nd_repstat != 0)
7175 *laystatp = nd->nd_repstat;
7176 if ((nd->nd_flag & ND_NOMOREDATA) == 0) {
7177 /* ND_NOMOREDATA will be set if the Open operation failed. */
7178 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
7180 op->nfso_stateid.seqid = *tl++;
7181 op->nfso_stateid.other[0] = *tl++;
7182 op->nfso_stateid.other[1] = *tl++;
7183 op->nfso_stateid.other[2] = *tl;
7184 rflags = fxdr_unsigned(u_int32_t, *(tl + 6));
7185 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
7188 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
7189 deleg = fxdr_unsigned(u_int32_t, *tl);
7190 if (deleg == NFSV4OPEN_DELEGATEREAD ||
7191 deleg == NFSV4OPEN_DELEGATEWRITE) {
7192 if (!(op->nfso_own->nfsow_clp->nfsc_flags &
7193 NFSCLFLAGS_FIRSTDELEG))
7194 op->nfso_own->nfsow_clp->nfsc_flags |=
7195 (NFSCLFLAGS_FIRSTDELEG | NFSCLFLAGS_GOTDELEG);
7196 ndp = malloc(sizeof(struct nfscldeleg) + newfhlen,
7197 M_NFSCLDELEG, M_WAITOK);
7198 LIST_INIT(&ndp->nfsdl_owner);
7199 LIST_INIT(&ndp->nfsdl_lock);
7200 ndp->nfsdl_clp = op->nfso_own->nfsow_clp;
7201 ndp->nfsdl_fhlen = newfhlen;
7202 NFSBCOPY(newfhp, ndp->nfsdl_fh, newfhlen);
7203 newnfs_copyincred(cred, &ndp->nfsdl_cred);
7204 nfscl_lockinit(&ndp->nfsdl_rwlock);
7205 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
7207 ndp->nfsdl_stateid.seqid = *tl++;
7208 ndp->nfsdl_stateid.other[0] = *tl++;
7209 ndp->nfsdl_stateid.other[1] = *tl++;
7210 ndp->nfsdl_stateid.other[2] = *tl++;
7211 ret = fxdr_unsigned(int, *tl);
7212 if (deleg == NFSV4OPEN_DELEGATEWRITE) {
7213 ndp->nfsdl_flags = NFSCLDL_WRITE;
7215 * Indicates how much the file can grow.
7217 NFSM_DISSECT(tl, u_int32_t *,
7219 limitby = fxdr_unsigned(int, *tl++);
7221 case NFSV4OPEN_LIMITSIZE:
7222 ndp->nfsdl_sizelimit = fxdr_hyper(tl);
7224 case NFSV4OPEN_LIMITBLOCKS:
7225 ndp->nfsdl_sizelimit =
7226 fxdr_unsigned(u_int64_t, *tl++);
7227 ndp->nfsdl_sizelimit *=
7228 fxdr_unsigned(u_int64_t, *tl);
7231 error = NFSERR_BADXDR;
7235 ndp->nfsdl_flags = NFSCLDL_READ;
7237 ndp->nfsdl_flags |= NFSCLDL_RECALL;
7238 error = nfsrv_dissectace(nd, &ndp->nfsdl_ace, &ret,
7242 } else if (deleg != NFSV4OPEN_DELEGATENONE) {
7243 error = NFSERR_BADXDR;
7246 if ((rflags & NFSV4OPEN_LOCKTYPEPOSIX) != 0 ||
7247 nfscl_assumeposixlocks)
7248 op->nfso_posixlock = 1;
7250 op->nfso_posixlock = 0;
7251 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
7252 /* If the 2nd element == NFS_OK, the Getattr succeeded. */
7254 error = nfsv4_loadattr(nd, NULL, &nfsva, NULL,
7255 NULL, 0, NULL, NULL, NULL, NULL, NULL, 0,
7256 NULL, NULL, NULL, p, cred);
7260 ndp->nfsdl_change = nfsva.na_filerev;
7261 ndp->nfsdl_modtime = nfsva.na_mtime;
7262 ndp->nfsdl_flags |= NFSCLDL_MODTIMESET;
7267 * At this point, the Open has succeeded, so set
7268 * nd_repstat = NFS_OK. If the Layoutget failed,
7269 * this function just won't return a layout.
7271 if (nd->nd_repstat == 0) {
7272 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
7273 *laystatp = fxdr_unsigned(int, *++tl);
7274 if (*laystatp == 0) {
7275 error = nfsrv_parselayoutget(nd,
7276 stateidp, retonclosep, flhp);
7281 nd->nd_repstat = 0; /* Return 0 for Open. */
7284 if (nd->nd_repstat != 0 && error == 0)
7285 error = nd->nd_repstat;
7287 free(ndp, M_NFSCLDELEG);
7288 mbuf_freem(nd->nd_mrep);
7293 * Similar nfsrpc_createv4(), but also does the LayoutGet operation.
7294 * Used only for mounts with pNFS enabled.
7297 nfsrpc_createlayout(vnode_t dvp, char *name, int namelen, struct vattr *vap,
7298 nfsquad_t cverf, int fmode, struct nfsclowner *owp, struct nfscldeleg **dpp,
7299 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap,
7300 struct nfsvattr *nnap, struct nfsfh **nfhpp, int *attrflagp,
7301 int *dattrflagp, void *dstuff, int *unlockedp, nfsv4stateid_t *stateidp,
7302 int usecurstateid, int layouttype, int layoutlen, int *retonclosep,
7303 struct nfsclflayouthead *flhp, int *laystatp)
7306 int error = 0, deleg, newone, ret, acesize, limitby;
7307 struct nfsrv_descript nfsd, *nd = &nfsd;
7308 struct nfsclopen *op;
7309 struct nfscldeleg *dp = NULL;
7312 struct nfsclsession *tsep;
7313 nfsattrbit_t attrbits;
7314 nfsv4stateid_t stateid;
7315 struct nfsmount *nmp;
7317 nmp = VFSTONFS(dvp->v_mount);
7325 if (namelen > NFS_MAXNAMLEN)
7326 return (ENAMETOOLONG);
7327 NFSCL_REQSTART(nd, NFSPROC_CREATELAYGET, dvp);
7329 * For V4, this is actually an Open op.
7331 NFSM_BUILD(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
7332 *tl++ = txdr_unsigned(owp->nfsow_seqid);
7333 *tl++ = txdr_unsigned(NFSV4OPEN_ACCESSWRITE |
7334 NFSV4OPEN_ACCESSREAD);
7335 *tl++ = txdr_unsigned(NFSV4OPEN_DENYNONE);
7336 tsep = nfsmnt_mdssession(nmp);
7337 *tl++ = tsep->nfsess_clientid.lval[0];
7338 *tl = tsep->nfsess_clientid.lval[1];
7339 nfsm_strtom(nd, owp->nfsow_owner, NFSV4CL_LOCKNAMELEN);
7340 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
7341 *tl++ = txdr_unsigned(NFSV4OPEN_CREATE);
7342 if ((fmode & O_EXCL) != 0) {
7343 if (NFSHASSESSPERSIST(nmp)) {
7344 /* Use GUARDED for persistent sessions. */
7345 *tl = txdr_unsigned(NFSCREATE_GUARDED);
7346 nfscl_fillsattr(nd, vap, dvp, 0, 0);
7348 /* Otherwise, use EXCLUSIVE4_1. */
7349 *tl = txdr_unsigned(NFSCREATE_EXCLUSIVE41);
7350 NFSM_BUILD(tl, u_int32_t *, NFSX_VERF);
7351 *tl++ = cverf.lval[0];
7352 *tl = cverf.lval[1];
7353 nfscl_fillsattr(nd, vap, dvp, 0, 0);
7356 *tl = txdr_unsigned(NFSCREATE_UNCHECKED);
7357 nfscl_fillsattr(nd, vap, dvp, 0, 0);
7359 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
7360 *tl = txdr_unsigned(NFSV4OPEN_CLAIMNULL);
7361 nfsm_strtom(nd, name, namelen);
7362 /* Get the new file's handle and attributes, plus save the FH. */
7363 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
7364 *tl++ = txdr_unsigned(NFSV4OP_SAVEFH);
7365 *tl++ = txdr_unsigned(NFSV4OP_GETFH);
7366 *tl = txdr_unsigned(NFSV4OP_GETATTR);
7367 NFSGETATTR_ATTRBIT(&attrbits);
7368 nfsrv_putattrbit(nd, &attrbits);
7369 /* Get the directory's post-op attributes. */
7370 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
7371 *tl = txdr_unsigned(NFSV4OP_PUTFH);
7372 nfsm_fhtom(nd, np->n_fhp->nfh_fh, np->n_fhp->nfh_len, 0);
7373 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
7374 *tl = txdr_unsigned(NFSV4OP_GETATTR);
7375 nfsrv_putattrbit(nd, &attrbits);
7376 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
7377 *tl++ = txdr_unsigned(NFSV4OP_RESTOREFH);
7378 *tl = txdr_unsigned(NFSV4OP_LAYOUTGET);
7379 nfsrv_setuplayoutget(nd, NFSLAYOUTIOMODE_RW, 0, UINT64_MAX, 0, stateidp,
7380 layouttype, layoutlen, usecurstateid);
7381 error = nfscl_request(nd, dvp, p, cred, dstuff);
7384 NFSCL_DEBUG(4, "nfsrpc_createlayout stat=%d err=%d\n", nd->nd_repstat,
7386 if (nd->nd_repstat != 0)
7387 *laystatp = nd->nd_repstat;
7388 NFSCL_INCRSEQID(owp->nfsow_seqid, nd);
7389 if ((nd->nd_flag & ND_NOMOREDATA) == 0) {
7390 NFSCL_DEBUG(4, "nfsrpc_createlayout open succeeded\n");
7391 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
7393 stateid.seqid = *tl++;
7394 stateid.other[0] = *tl++;
7395 stateid.other[1] = *tl++;
7396 stateid.other[2] = *tl;
7397 nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
7398 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
7399 deleg = fxdr_unsigned(int, *tl);
7400 if (deleg == NFSV4OPEN_DELEGATEREAD ||
7401 deleg == NFSV4OPEN_DELEGATEWRITE) {
7402 if (!(owp->nfsow_clp->nfsc_flags &
7403 NFSCLFLAGS_FIRSTDELEG))
7404 owp->nfsow_clp->nfsc_flags |=
7405 (NFSCLFLAGS_FIRSTDELEG | NFSCLFLAGS_GOTDELEG);
7406 dp = malloc(sizeof(struct nfscldeleg) + NFSX_V4FHMAX,
7407 M_NFSCLDELEG, M_WAITOK);
7408 LIST_INIT(&dp->nfsdl_owner);
7409 LIST_INIT(&dp->nfsdl_lock);
7410 dp->nfsdl_clp = owp->nfsow_clp;
7411 newnfs_copyincred(cred, &dp->nfsdl_cred);
7412 nfscl_lockinit(&dp->nfsdl_rwlock);
7413 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
7415 dp->nfsdl_stateid.seqid = *tl++;
7416 dp->nfsdl_stateid.other[0] = *tl++;
7417 dp->nfsdl_stateid.other[1] = *tl++;
7418 dp->nfsdl_stateid.other[2] = *tl++;
7419 ret = fxdr_unsigned(int, *tl);
7420 if (deleg == NFSV4OPEN_DELEGATEWRITE) {
7421 dp->nfsdl_flags = NFSCLDL_WRITE;
7423 * Indicates how much the file can grow.
7425 NFSM_DISSECT(tl, u_int32_t *,
7427 limitby = fxdr_unsigned(int, *tl++);
7429 case NFSV4OPEN_LIMITSIZE:
7430 dp->nfsdl_sizelimit = fxdr_hyper(tl);
7432 case NFSV4OPEN_LIMITBLOCKS:
7433 dp->nfsdl_sizelimit =
7434 fxdr_unsigned(u_int64_t, *tl++);
7435 dp->nfsdl_sizelimit *=
7436 fxdr_unsigned(u_int64_t, *tl);
7439 error = NFSERR_BADXDR;
7443 dp->nfsdl_flags = NFSCLDL_READ;
7446 dp->nfsdl_flags |= NFSCLDL_RECALL;
7447 error = nfsrv_dissectace(nd, &dp->nfsdl_ace, &ret,
7451 } else if (deleg != NFSV4OPEN_DELEGATENONE) {
7452 error = NFSERR_BADXDR;
7456 /* Now, we should have the status for the SaveFH. */
7457 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
7459 NFSCL_DEBUG(4, "nfsrpc_createlayout SaveFH ok\n");
7461 * Now, process the GetFH and Getattr for the newly
7462 * created file. nfscl_mtofh() will set
7463 * ND_NOMOREDATA if these weren't successful.
7465 error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp);
7466 NFSCL_DEBUG(4, "aft nfscl_mtofh err=%d\n", error);
7470 nd->nd_flag |= ND_NOMOREDATA;
7471 /* Now we have the PutFH and Getattr for the directory. */
7472 if ((nd->nd_flag & ND_NOMOREDATA) == 0) {
7473 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
7475 nd->nd_flag |= ND_NOMOREDATA;
7477 NFSM_DISSECT(tl, uint32_t *, 2 *
7480 nd->nd_flag |= ND_NOMOREDATA;
7483 if ((nd->nd_flag & ND_NOMOREDATA) == 0) {
7484 /* Load the directory attributes. */
7485 error = nfsm_loadattr(nd, dnap);
7486 NFSCL_DEBUG(4, "aft nfsm_loadattr err=%d\n", error);
7490 if (dp != NULL && *attrflagp != 0) {
7491 dp->nfsdl_change = nnap->na_filerev;
7492 dp->nfsdl_modtime = nnap->na_mtime;
7493 dp->nfsdl_flags |= NFSCLDL_MODTIMESET;
7496 * We can now complete the Open state.
7500 dp->nfsdl_fhlen = nfhp->nfh_len;
7501 NFSBCOPY(nfhp->nfh_fh, dp->nfsdl_fh,
7505 * Get an Open structure that will be
7506 * attached to the OpenOwner, acquired already.
7508 error = nfscl_open(dvp, nfhp->nfh_fh, nfhp->nfh_len,
7509 (NFSV4OPEN_ACCESSWRITE | NFSV4OPEN_ACCESSREAD), 0,
7510 cred, p, NULL, &op, &newone, NULL, 0);
7513 op->nfso_stateid = stateid;
7514 newnfs_copyincred(cred, &op->nfso_cred);
7516 nfscl_openrelease(nmp, op, error, newone);
7519 /* Now, handle the RestoreFH and LayoutGet. */
7520 if (nd->nd_repstat == 0) {
7521 NFSM_DISSECT(tl, uint32_t *, 4 * NFSX_UNSIGNED);
7522 *laystatp = fxdr_unsigned(int, *(tl + 3));
7523 if (*laystatp == 0) {
7524 error = nfsrv_parselayoutget(nd,
7525 stateidp, retonclosep, flhp);
7529 NFSCL_DEBUG(4, "aft nfsrv_parselayout err=%d\n",
7535 if (nd->nd_repstat != 0 && error == 0)
7536 error = nd->nd_repstat;
7537 if (error == NFSERR_STALECLIENTID || error == NFSERR_BADSESSION)
7538 nfscl_initiate_recovery(owp->nfsow_clp);
7540 NFSCL_DEBUG(4, "eo nfsrpc_createlayout err=%d\n", error);
7544 free(dp, M_NFSCLDELEG);
7545 mbuf_freem(nd->nd_mrep);
7550 * Similar to nfsrpc_getopenlayout(), except that it used for the Create case.
7553 nfsrpc_getcreatelayout(vnode_t dvp, char *name, int namelen, struct vattr *vap,
7554 nfsquad_t cverf, int fmode, struct nfsclowner *owp, struct nfscldeleg **dpp,
7555 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap,
7556 struct nfsvattr *nnap, struct nfsfh **nfhpp, int *attrflagp,
7557 int *dattrflagp, void *dstuff, int *unlockedp)
7559 struct nfscllayout *lyp;
7560 struct nfsclflayouthead flh;
7562 struct nfsclsession *tsep;
7563 struct nfsmount *nmp;
7564 nfsv4stateid_t stateid;
7565 int error, layoutlen, layouttype, retonclose, laystat;
7568 nmp = VFSTONFS(dvp->v_mount);
7569 if (NFSHASFLEXFILE(nmp))
7570 layouttype = NFSLAYOUT_FLEXFILE;
7572 layouttype = NFSLAYOUT_NFSV4_1_FILES;
7574 tsep = nfsmnt_mdssession(nmp);
7575 layoutlen = tsep->nfsess_maxcache - (NFSX_STATEID + 3 * NFSX_UNSIGNED);
7576 error = nfsrpc_createlayout(dvp, name, namelen, vap, cverf, fmode,
7577 owp, dpp, cred, p, dnap, nnap, nfhpp, attrflagp, dattrflagp,
7578 dstuff, unlockedp, &stateid, 1, layouttype, layoutlen, &retonclose,
7580 NFSCL_DEBUG(4, "aft nfsrpc_createlayoutrpc laystat=%d err=%d\n",
7585 laystat = nfsrpc_layoutgetres(nmp, dvp, nfhp->nfh_fh,
7586 nfhp->nfh_len, &stateid, retonclose, NULL, &lyp, &flh,
7587 layouttype, laystat, NULL, cred, p);
7589 laystat = nfsrpc_layoutgetres(nmp, dvp, NULL, 0, &stateid,
7590 retonclose, NULL, &lyp, &flh, layouttype, laystat, NULL,
7593 nfscl_rellayout(lyp, 0);
7598 * Process the results of a layoutget() operation.
7601 nfsrpc_layoutgetres(struct nfsmount *nmp, vnode_t vp, uint8_t *newfhp,
7602 int newfhlen, nfsv4stateid_t *stateidp, int retonclose, uint32_t *notifybit,
7603 struct nfscllayout **lypp, struct nfsclflayouthead *flhp, int layouttype,
7604 int laystat, int *islockedp, struct ucred *cred, NFSPROC_T *p)
7606 struct nfsclflayout *tflp;
7607 struct nfscldevinfo *dip;
7611 if (laystat == NFSERR_UNKNLAYOUTTYPE) {
7613 if (!NFSHASFLEXFILE(nmp)) {
7614 /* Switch to using Flex File Layout. */
7615 nmp->nm_state |= NFSSTA_FLEXFILE;
7616 } else if (layouttype == NFSLAYOUT_FLEXFILE) {
7618 NFSCL_DEBUG(1, "disable PNFS\n");
7619 nmp->nm_state &= ~(NFSSTA_PNFS | NFSSTA_FLEXFILE);
7624 NFSCL_DEBUG(4, "nfsrpc_layoutgetres at FOREACH\n");
7625 LIST_FOREACH(tflp, flhp, nfsfl_list) {
7626 if (layouttype == NFSLAYOUT_FLEXFILE)
7627 mirrorcnt = tflp->nfsfl_mirrorcnt;
7630 for (i = 0; i < mirrorcnt; i++) {
7631 laystat = nfscl_adddevinfo(nmp, NULL, i, tflp);
7632 NFSCL_DEBUG(4, "aft adddev=%d\n", laystat);
7634 if (layouttype == NFSLAYOUT_FLEXFILE)
7635 dev = tflp->nfsfl_ffm[i].dev;
7637 dev = tflp->nfsfl_dev;
7638 laystat = nfsrpc_getdeviceinfo(nmp, dev,
7639 layouttype, notifybit, &dip, cred,
7641 NFSCL_DEBUG(4, "aft nfsrpc_gdi=%d\n",
7645 laystat = nfscl_adddevinfo(nmp, dip, i,
7648 printf("nfsrpc_layoutgetresout"
7657 * nfscl_layout() always returns with the nfsly_lock
7658 * set to a refcnt (shared lock).
7659 * Passing in dvp is sufficient, since it is only used to
7660 * get the fsid for the file system.
7662 laystat = nfscl_layout(nmp, vp, newfhp, newfhlen, stateidp,
7663 layouttype, retonclose, flhp, lypp, cred, p);
7664 NFSCL_DEBUG(4, "nfsrpc_layoutgetres: aft nfscl_layout=%d\n",
7666 if (laystat == 0 && islockedp != NULL)