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 ndp->ni_dvp != ndp->ni_vp &&
3672 (newvp->v_type != VDIR ||
3673 dctime.tv_sec != 0)) {
3674 cache_enter_time(ndp->ni_dvp,
3677 newvp->v_type != VDIR ? NULL :
3687 } else if (nfhp != NULL) {
3688 free(nfhp, M_NFSFH);
3690 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3691 more_dirs = fxdr_unsigned(int, *tl);
3694 * If at end of rpc data, get the eof boolean
3697 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3698 eof = fxdr_unsigned(int, *tl);
3701 if (nd->nd_flag & ND_NFSV4) {
3702 error = nfscl_postop_attr(nd, nap, attrflagp,
3708 mbuf_freem(nd->nd_mrep);
3712 * Fill last record, iff any, out to a multiple of DIRBLKSIZ
3713 * by increasing d_reclen for the last record.
3716 left = DIRBLKSIZ - blksiz;
3717 NFSBZERO(uio_iov_base(uiop), left);
3718 dp->d_reclen += left;
3719 uio_iov_base_add(uiop, left);
3720 uio_iov_len_add(uiop, -(left));
3721 uio_uio_resid_add(uiop, -(left));
3722 uiop->uio_offset += left;
3726 * If returning no data, assume end of file.
3727 * If not bigenough, return not end of file, since you aren't
3728 * returning all the data
3729 * Otherwise, return the eof flag from the server.
3732 if (tresid == uio_uio_resid(uiop))
3734 else if (!bigenough)
3741 * Add extra empty records to any remaining DIRBLKSIZ chunks.
3743 while (uio_uio_resid(uiop) > 0 && uio_uio_resid(uiop) != tresid) {
3744 dp = (struct dirent *)uio_iov_base(uiop);
3745 NFSBZERO(dp, DIRBLKSIZ);
3746 dp->d_type = DT_UNKNOWN;
3747 tl = (u_int32_t *)&dp->d_name[4];
3748 *tl++ = cookie.lval[0];
3749 *tl = cookie.lval[1];
3750 dp->d_reclen = DIRBLKSIZ;
3751 uio_iov_base_add(uiop, DIRBLKSIZ);
3752 uio_iov_len_add(uiop, -(DIRBLKSIZ));
3753 uio_uio_resid_add(uiop, -(DIRBLKSIZ));
3754 uiop->uio_offset += DIRBLKSIZ;
3758 if (nd->nd_mrep != NULL)
3759 mbuf_freem(nd->nd_mrep);
3768 nfsrpc_commit(vnode_t vp, u_quad_t offset, int cnt, struct ucred *cred,
3769 NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, void *stuff)
3772 struct nfsrv_descript nfsd, *nd = &nfsd;
3773 nfsattrbit_t attrbits;
3775 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
3778 NFSCL_REQSTART(nd, NFSPROC_COMMIT, vp);
3779 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
3780 txdr_hyper(offset, tl);
3782 *tl = txdr_unsigned(cnt);
3783 if (nd->nd_flag & ND_NFSV4) {
3785 * And do a Getattr op.
3787 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
3788 *tl = txdr_unsigned(NFSV4OP_GETATTR);
3789 NFSGETATTR_ATTRBIT(&attrbits);
3790 (void) nfsrv_putattrbit(nd, &attrbits);
3792 error = nfscl_request(nd, vp, p, cred, stuff);
3795 error = nfscl_wcc_data(nd, vp, nap, attrflagp, NULL, stuff);
3796 if (!error && !nd->nd_repstat) {
3797 NFSM_DISSECT(tl, u_int32_t *, NFSX_VERF);
3799 if (NFSBCMP(nmp->nm_verf, tl, NFSX_VERF)) {
3800 NFSBCOPY(tl, nmp->nm_verf, NFSX_VERF);
3801 nd->nd_repstat = NFSERR_STALEWRITEVERF;
3804 if (nd->nd_flag & ND_NFSV4)
3805 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
3808 if (!error && nd->nd_repstat)
3809 error = nd->nd_repstat;
3810 mbuf_freem(nd->nd_mrep);
3815 * NFS byte range lock rpc.
3816 * (Mostly just calls one of the three lower level RPC routines.)
3819 nfsrpc_advlock(vnode_t vp, off_t size, int op, struct flock *fl,
3820 int reclaim, struct ucred *cred, NFSPROC_T *p, void *id, int flags)
3822 struct nfscllockowner *lp;
3823 struct nfsclclient *clp;
3825 struct nfsrv_descript nfsd, *nd = &nfsd;
3826 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
3829 u_int32_t clidrev = 0;
3830 int error = 0, newone = 0, expireret = 0, retrycnt, donelocally;
3834 * Convert the flock structure into a start and end and do POSIX
3837 switch (fl->l_whence) {
3841 * Caller is responsible for adding any necessary offset
3842 * when SEEK_CUR is used.
3844 start = fl->l_start;
3848 start = size + fl->l_start;
3849 off = size + fl->l_start;
3856 if (fl->l_len != 0) {
3857 end = start + fl->l_len - 1;
3868 if (op == F_GETLK) {
3869 error = nfscl_getcl(vnode_mount(vp), cred, p, 1, &clp);
3872 error = nfscl_lockt(vp, clp, off, len, fl, p, id, flags);
3874 clidrev = clp->nfsc_clientidrev;
3875 error = nfsrpc_lockt(nd, vp, clp, off, len, fl, cred,
3877 } else if (error == -1) {
3880 nfscl_clientrelease(clp);
3881 } else if (op == F_UNLCK && fl->l_type == F_UNLCK) {
3883 * We must loop around for all lockowner cases.
3886 error = nfscl_getcl(vnode_mount(vp), cred, p, 1, &clp);
3890 error = nfscl_relbytelock(vp, off, len, cred, p, callcnt,
3891 clp, id, flags, &lp, &dorpc);
3893 * If it returns a NULL lp, we're done.
3897 nfscl_clientrelease(clp);
3899 nfscl_releasealllocks(clp, vp, p, id, flags);
3902 if (nmp->nm_clp != NULL)
3903 clidrev = nmp->nm_clp->nfsc_clientidrev;
3907 * If the server doesn't support Posix lock semantics,
3908 * only allow locks on the entire file, since it won't
3909 * handle overlapping byte ranges.
3910 * There might still be a problem when a lock
3911 * upgrade/downgrade (read<->write) occurs, since the
3912 * server "might" expect an unlock first?
3914 if (dorpc && (lp->nfsl_open->nfso_posixlock ||
3915 (off == 0 && len == NFS64BITSSET))) {
3917 * Since the lock records will go away, we must
3918 * wait for grace and delay here.
3921 error = nfsrpc_locku(nd, nmp, lp, off, len,
3922 NFSV4LOCKT_READ, cred, p, 0);
3923 if ((nd->nd_repstat == NFSERR_GRACE ||
3924 nd->nd_repstat == NFSERR_DELAY) &&
3926 (void) nfs_catnap(PZERO, (int)nd->nd_repstat,
3928 } while ((nd->nd_repstat == NFSERR_GRACE ||
3929 nd->nd_repstat == NFSERR_DELAY) && error == 0);
3932 } while (error == 0 && nd->nd_repstat == 0);
3933 nfscl_releasealllocks(clp, vp, p, id, flags);
3934 } else if (op == F_SETLK) {
3935 error = nfscl_getbytelock(vp, off, len, fl->l_type, cred, p,
3936 NULL, 0, id, flags, NULL, NULL, &lp, &newone, &donelocally);
3937 if (error || donelocally) {
3940 if (nmp->nm_clp != NULL)
3941 clidrev = nmp->nm_clp->nfsc_clientidrev;
3944 nfhp = VTONFS(vp)->n_fhp;
3945 if (!lp->nfsl_open->nfso_posixlock &&
3946 (off != 0 || len != NFS64BITSSET)) {
3949 error = nfsrpc_lock(nd, nmp, vp, nfhp->nfh_fh,
3950 nfhp->nfh_len, lp, newone, reclaim, off,
3951 len, fl->l_type, cred, p, 0);
3954 error = nd->nd_repstat;
3955 nfscl_lockrelease(lp, error, newone);
3960 error = nd->nd_repstat;
3961 if (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
3962 error == NFSERR_STALEDONTRECOVER ||
3963 error == NFSERR_STALECLIENTID || error == NFSERR_DELAY ||
3964 error == NFSERR_BADSESSION) {
3965 (void) nfs_catnap(PZERO, error, "nfs_advlock");
3966 } else if ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID)
3968 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
3971 } while (error == NFSERR_GRACE ||
3972 error == NFSERR_STALECLIENTID || error == NFSERR_DELAY ||
3973 error == NFSERR_STALEDONTRECOVER || error == NFSERR_STALESTATEID ||
3974 error == NFSERR_BADSESSION ||
3975 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
3976 expireret == 0 && clidrev != 0 && retrycnt < 4));
3977 if (error && retrycnt >= 4)
3983 * The lower level routine for the LockT case.
3986 nfsrpc_lockt(struct nfsrv_descript *nd, vnode_t vp,
3987 struct nfsclclient *clp, u_int64_t off, u_int64_t len, struct flock *fl,
3988 struct ucred *cred, NFSPROC_T *p, void *id, int flags)
3991 int error, type, size;
3992 uint8_t own[NFSV4CL_LOCKNAMELEN + NFSX_V4FHMAX];
3994 struct nfsmount *nmp;
3995 struct nfsclsession *tsep;
3997 nmp = VFSTONFS(vp->v_mount);
3998 NFSCL_REQSTART(nd, NFSPROC_LOCKT, vp);
3999 NFSM_BUILD(tl, u_int32_t *, 7 * NFSX_UNSIGNED);
4000 if (fl->l_type == F_RDLCK)
4001 *tl++ = txdr_unsigned(NFSV4LOCKT_READ);
4003 *tl++ = txdr_unsigned(NFSV4LOCKT_WRITE);
4004 txdr_hyper(off, tl);
4006 txdr_hyper(len, tl);
4008 tsep = nfsmnt_mdssession(nmp);
4009 *tl++ = tsep->nfsess_clientid.lval[0];
4010 *tl = tsep->nfsess_clientid.lval[1];
4011 nfscl_filllockowner(id, own, flags);
4013 NFSBCOPY(np->n_fhp->nfh_fh, &own[NFSV4CL_LOCKNAMELEN],
4014 np->n_fhp->nfh_len);
4015 (void)nfsm_strtom(nd, own, NFSV4CL_LOCKNAMELEN + np->n_fhp->nfh_len);
4016 error = nfscl_request(nd, vp, p, cred, NULL);
4019 if (nd->nd_repstat == 0) {
4020 fl->l_type = F_UNLCK;
4021 } else if (nd->nd_repstat == NFSERR_DENIED) {
4023 fl->l_whence = SEEK_SET;
4024 NFSM_DISSECT(tl, u_int32_t *, 8 * NFSX_UNSIGNED);
4025 fl->l_start = fxdr_hyper(tl);
4027 len = fxdr_hyper(tl);
4029 if (len == NFS64BITSSET)
4033 type = fxdr_unsigned(int, *tl++);
4034 if (type == NFSV4LOCKT_WRITE)
4035 fl->l_type = F_WRLCK;
4037 fl->l_type = F_RDLCK;
4039 * XXX For now, I have no idea what to do with the
4040 * conflicting lock_owner, so I'll just set the pid == 0
4041 * and skip over the lock_owner.
4043 fl->l_pid = (pid_t)0;
4045 size = fxdr_unsigned(int, *tl);
4046 if (size < 0 || size > NFSV4_OPAQUELIMIT)
4049 error = nfsm_advance(nd, NFSM_RNDUP(size), -1);
4050 } else if (nd->nd_repstat == NFSERR_STALECLIENTID)
4051 nfscl_initiate_recovery(clp);
4053 mbuf_freem(nd->nd_mrep);
4058 * Lower level function that performs the LockU RPC.
4061 nfsrpc_locku(struct nfsrv_descript *nd, struct nfsmount *nmp,
4062 struct nfscllockowner *lp, u_int64_t off, u_int64_t len,
4063 u_int32_t type, struct ucred *cred, NFSPROC_T *p, int syscred)
4068 nfscl_reqstart(nd, NFSPROC_LOCKU, nmp, lp->nfsl_open->nfso_fh,
4069 lp->nfsl_open->nfso_fhlen, NULL, NULL, 0, 0);
4070 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID + 6 * NFSX_UNSIGNED);
4071 *tl++ = txdr_unsigned(type);
4072 *tl = txdr_unsigned(lp->nfsl_seqid);
4073 if (nfstest_outofseq &&
4074 (arc4random() % nfstest_outofseq) == 0)
4075 *tl = txdr_unsigned(lp->nfsl_seqid + 1);
4077 if (NFSHASNFSV4N(nmp))
4080 *tl++ = lp->nfsl_stateid.seqid;
4081 *tl++ = lp->nfsl_stateid.other[0];
4082 *tl++ = lp->nfsl_stateid.other[1];
4083 *tl++ = lp->nfsl_stateid.other[2];
4084 txdr_hyper(off, tl);
4086 txdr_hyper(len, tl);
4088 nd->nd_flag |= ND_USEGSSNAME;
4089 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4090 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4091 NFSCL_INCRSEQID(lp->nfsl_seqid, nd);
4094 if (nd->nd_repstat == 0) {
4095 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID);
4096 lp->nfsl_stateid.seqid = *tl++;
4097 lp->nfsl_stateid.other[0] = *tl++;
4098 lp->nfsl_stateid.other[1] = *tl++;
4099 lp->nfsl_stateid.other[2] = *tl;
4100 } else if (nd->nd_repstat == NFSERR_STALESTATEID)
4101 nfscl_initiate_recovery(lp->nfsl_open->nfso_own->nfsow_clp);
4103 mbuf_freem(nd->nd_mrep);
4108 * The actual Lock RPC.
4111 nfsrpc_lock(struct nfsrv_descript *nd, struct nfsmount *nmp, vnode_t vp,
4112 u_int8_t *nfhp, int fhlen, struct nfscllockowner *lp, int newone,
4113 int reclaim, u_int64_t off, u_int64_t len, short type, struct ucred *cred,
4114 NFSPROC_T *p, int syscred)
4118 uint8_t own[NFSV4CL_LOCKNAMELEN + NFSX_V4FHMAX];
4119 struct nfsclsession *tsep;
4121 nfscl_reqstart(nd, NFSPROC_LOCK, nmp, nfhp, fhlen, NULL, NULL, 0, 0);
4122 NFSM_BUILD(tl, u_int32_t *, 7 * NFSX_UNSIGNED);
4123 if (type == F_RDLCK)
4124 *tl++ = txdr_unsigned(NFSV4LOCKT_READ);
4126 *tl++ = txdr_unsigned(NFSV4LOCKT_WRITE);
4127 *tl++ = txdr_unsigned(reclaim);
4128 txdr_hyper(off, tl);
4130 txdr_hyper(len, tl);
4134 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID +
4135 2 * NFSX_UNSIGNED + NFSX_HYPER);
4136 *tl++ = txdr_unsigned(lp->nfsl_open->nfso_own->nfsow_seqid);
4137 if (NFSHASNFSV4N(nmp))
4140 *tl++ = lp->nfsl_open->nfso_stateid.seqid;
4141 *tl++ = lp->nfsl_open->nfso_stateid.other[0];
4142 *tl++ = lp->nfsl_open->nfso_stateid.other[1];
4143 *tl++ = lp->nfsl_open->nfso_stateid.other[2];
4144 *tl++ = txdr_unsigned(lp->nfsl_seqid);
4145 tsep = nfsmnt_mdssession(nmp);
4146 *tl++ = tsep->nfsess_clientid.lval[0];
4147 *tl = tsep->nfsess_clientid.lval[1];
4148 NFSBCOPY(lp->nfsl_owner, own, NFSV4CL_LOCKNAMELEN);
4149 NFSBCOPY(nfhp, &own[NFSV4CL_LOCKNAMELEN], fhlen);
4150 (void)nfsm_strtom(nd, own, NFSV4CL_LOCKNAMELEN + fhlen);
4153 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID + NFSX_UNSIGNED);
4154 if (NFSHASNFSV4N(nmp))
4157 *tl++ = lp->nfsl_stateid.seqid;
4158 *tl++ = lp->nfsl_stateid.other[0];
4159 *tl++ = lp->nfsl_stateid.other[1];
4160 *tl++ = lp->nfsl_stateid.other[2];
4161 *tl = txdr_unsigned(lp->nfsl_seqid);
4162 if (nfstest_outofseq &&
4163 (arc4random() % nfstest_outofseq) == 0)
4164 *tl = txdr_unsigned(lp->nfsl_seqid + 1);
4167 nd->nd_flag |= ND_USEGSSNAME;
4168 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, vp, p, cred,
4169 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4173 NFSCL_INCRSEQID(lp->nfsl_open->nfso_own->nfsow_seqid, nd);
4174 NFSCL_INCRSEQID(lp->nfsl_seqid, nd);
4175 if (nd->nd_repstat == 0) {
4176 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID);
4177 lp->nfsl_stateid.seqid = *tl++;
4178 lp->nfsl_stateid.other[0] = *tl++;
4179 lp->nfsl_stateid.other[1] = *tl++;
4180 lp->nfsl_stateid.other[2] = *tl;
4181 } else if (nd->nd_repstat == NFSERR_DENIED) {
4182 NFSM_DISSECT(tl, u_int32_t *, 8 * NFSX_UNSIGNED);
4183 size = fxdr_unsigned(int, *(tl + 7));
4184 if (size < 0 || size > NFSV4_OPAQUELIMIT)
4187 error = nfsm_advance(nd, NFSM_RNDUP(size), -1);
4188 } else if (nd->nd_repstat == NFSERR_STALESTATEID)
4189 nfscl_initiate_recovery(lp->nfsl_open->nfso_own->nfsow_clp);
4191 mbuf_freem(nd->nd_mrep);
4197 * (always called with the vp for the mount point)
4200 nfsrpc_statfs(vnode_t vp, struct nfsstatfs *sbp, struct nfsfsinfo *fsp,
4201 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp,
4204 u_int32_t *tl = NULL;
4205 struct nfsrv_descript nfsd, *nd = &nfsd;
4206 struct nfsmount *nmp;
4207 nfsattrbit_t attrbits;
4211 nmp = VFSTONFS(vnode_mount(vp));
4212 if (NFSHASNFSV4(nmp)) {
4214 * For V4, you actually do a getattr.
4216 NFSCL_REQSTART(nd, NFSPROC_GETATTR, vp);
4217 NFSSTATFS_GETATTRBIT(&attrbits);
4218 (void) nfsrv_putattrbit(nd, &attrbits);
4219 nd->nd_flag |= ND_USEGSSNAME;
4220 error = nfscl_request(nd, vp, p, cred, stuff);
4223 if (nd->nd_repstat == 0) {
4224 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0,
4225 NULL, NULL, sbp, fsp, NULL, 0, NULL, NULL, NULL, p,
4228 nmp->nm_fsid[0] = nap->na_filesid[0];
4229 nmp->nm_fsid[1] = nap->na_filesid[1];
4230 NFSSETHASSETFSID(nmp);
4234 error = nd->nd_repstat;
4239 NFSCL_REQSTART(nd, NFSPROC_FSSTAT, vp);
4240 error = nfscl_request(nd, vp, p, cred, stuff);
4243 if (nd->nd_flag & ND_NFSV3) {
4244 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
4248 if (nd->nd_repstat) {
4249 error = nd->nd_repstat;
4252 NFSM_DISSECT(tl, u_int32_t *,
4253 NFSX_STATFS(nd->nd_flag & ND_NFSV3));
4255 if (NFSHASNFSV3(nmp)) {
4256 sbp->sf_tbytes = fxdr_hyper(tl); tl += 2;
4257 sbp->sf_fbytes = fxdr_hyper(tl); tl += 2;
4258 sbp->sf_abytes = fxdr_hyper(tl); tl += 2;
4259 sbp->sf_tfiles = fxdr_hyper(tl); tl += 2;
4260 sbp->sf_ffiles = fxdr_hyper(tl); tl += 2;
4261 sbp->sf_afiles = fxdr_hyper(tl); tl += 2;
4262 sbp->sf_invarsec = fxdr_unsigned(u_int32_t, *tl);
4263 } else if (NFSHASNFSV4(nmp) == 0) {
4264 sbp->sf_tsize = fxdr_unsigned(u_int32_t, *tl++);
4265 sbp->sf_bsize = fxdr_unsigned(u_int32_t, *tl++);
4266 sbp->sf_blocks = fxdr_unsigned(u_int32_t, *tl++);
4267 sbp->sf_bfree = fxdr_unsigned(u_int32_t, *tl++);
4268 sbp->sf_bavail = fxdr_unsigned(u_int32_t, *tl);
4271 mbuf_freem(nd->nd_mrep);
4279 nfsrpc_pathconf(vnode_t vp, struct nfsv3_pathconf *pc,
4280 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp,
4283 struct nfsrv_descript nfsd, *nd = &nfsd;
4284 struct nfsmount *nmp;
4286 nfsattrbit_t attrbits;
4290 nmp = VFSTONFS(vnode_mount(vp));
4291 if (NFSHASNFSV4(nmp)) {
4293 * For V4, you actually do a getattr.
4295 NFSCL_REQSTART(nd, NFSPROC_GETATTR, vp);
4296 NFSPATHCONF_GETATTRBIT(&attrbits);
4297 (void) nfsrv_putattrbit(nd, &attrbits);
4298 nd->nd_flag |= ND_USEGSSNAME;
4299 error = nfscl_request(nd, vp, p, cred, stuff);
4302 if (nd->nd_repstat == 0) {
4303 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0,
4304 pc, NULL, NULL, NULL, NULL, 0, NULL, NULL, NULL, p,
4309 error = nd->nd_repstat;
4312 NFSCL_REQSTART(nd, NFSPROC_PATHCONF, vp);
4313 error = nfscl_request(nd, vp, p, cred, stuff);
4316 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
4317 if (nd->nd_repstat && !error)
4318 error = nd->nd_repstat;
4320 NFSM_DISSECT(tl, u_int32_t *, NFSX_V3PATHCONF);
4321 pc->pc_linkmax = fxdr_unsigned(u_int32_t, *tl++);
4322 pc->pc_namemax = fxdr_unsigned(u_int32_t, *tl++);
4323 pc->pc_notrunc = fxdr_unsigned(u_int32_t, *tl++);
4324 pc->pc_chownrestricted =
4325 fxdr_unsigned(u_int32_t, *tl++);
4326 pc->pc_caseinsensitive =
4327 fxdr_unsigned(u_int32_t, *tl++);
4328 pc->pc_casepreserving = fxdr_unsigned(u_int32_t, *tl);
4332 mbuf_freem(nd->nd_mrep);
4337 * nfs version 3 fsinfo rpc call
4340 nfsrpc_fsinfo(vnode_t vp, struct nfsfsinfo *fsp, struct ucred *cred,
4341 NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, void *stuff)
4344 struct nfsrv_descript nfsd, *nd = &nfsd;
4348 NFSCL_REQSTART(nd, NFSPROC_FSINFO, vp);
4349 error = nfscl_request(nd, vp, p, cred, stuff);
4352 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
4353 if (nd->nd_repstat && !error)
4354 error = nd->nd_repstat;
4356 NFSM_DISSECT(tl, u_int32_t *, NFSX_V3FSINFO);
4357 fsp->fs_rtmax = fxdr_unsigned(u_int32_t, *tl++);
4358 fsp->fs_rtpref = fxdr_unsigned(u_int32_t, *tl++);
4359 fsp->fs_rtmult = fxdr_unsigned(u_int32_t, *tl++);
4360 fsp->fs_wtmax = fxdr_unsigned(u_int32_t, *tl++);
4361 fsp->fs_wtpref = fxdr_unsigned(u_int32_t, *tl++);
4362 fsp->fs_wtmult = fxdr_unsigned(u_int32_t, *tl++);
4363 fsp->fs_dtpref = fxdr_unsigned(u_int32_t, *tl++);
4364 fsp->fs_maxfilesize = fxdr_hyper(tl);
4366 fxdr_nfsv3time(tl, &fsp->fs_timedelta);
4368 fsp->fs_properties = fxdr_unsigned(u_int32_t, *tl);
4371 mbuf_freem(nd->nd_mrep);
4376 * This function performs the Renew RPC.
4379 nfsrpc_renew(struct nfsclclient *clp, struct nfsclds *dsp, struct ucred *cred,
4383 struct nfsrv_descript nfsd;
4384 struct nfsrv_descript *nd = &nfsd;
4385 struct nfsmount *nmp;
4387 struct nfssockreq *nrp;
4388 struct nfsclsession *tsep;
4390 nmp = clp->nfsc_nmp;
4394 nfscl_reqstart(nd, NFSPROC_RENEW, nmp, NULL, 0, NULL, NULL, 0,
4397 nfscl_reqstart(nd, NFSPROC_RENEW, nmp, NULL, 0, NULL,
4398 &dsp->nfsclds_sess, 0, 0);
4399 if (!NFSHASNFSV4N(nmp)) {
4400 /* NFSv4.1 just uses a Sequence Op and not a Renew. */
4401 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
4402 tsep = nfsmnt_mdssession(nmp);
4403 *tl++ = tsep->nfsess_clientid.lval[0];
4404 *tl = tsep->nfsess_clientid.lval[1];
4408 nrp = dsp->nfsclds_sockp;
4410 /* If NULL, use the MDS socket. */
4411 nrp = &nmp->nm_sockreq;
4412 nd->nd_flag |= ND_USEGSSNAME;
4414 error = newnfs_request(nd, nmp, NULL, nrp, NULL, p, cred,
4415 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4417 error = newnfs_request(nd, nmp, NULL, nrp, NULL, p, cred,
4418 NFS_PROG, NFS_VER4, NULL, 1, NULL, &dsp->nfsclds_sess);
4420 nfscl_cancelreqs(dsp);
4424 error = nd->nd_repstat;
4425 mbuf_freem(nd->nd_mrep);
4430 * This function performs the Releaselockowner RPC.
4433 nfsrpc_rellockown(struct nfsmount *nmp, struct nfscllockowner *lp,
4434 uint8_t *fh, int fhlen, struct ucred *cred, NFSPROC_T *p)
4436 struct nfsrv_descript nfsd, *nd = &nfsd;
4439 uint8_t own[NFSV4CL_LOCKNAMELEN + NFSX_V4FHMAX];
4440 struct nfsclsession *tsep;
4442 if (NFSHASNFSV4N(nmp)) {
4443 /* For NFSv4.1, do a FreeStateID. */
4444 nfscl_reqstart(nd, NFSPROC_FREESTATEID, nmp, NULL, 0, NULL,
4446 nfsm_stateidtom(nd, &lp->nfsl_stateid, NFSSTATEID_PUTSTATEID);
4448 nfscl_reqstart(nd, NFSPROC_RELEASELCKOWN, nmp, NULL, 0, NULL,
4450 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
4451 tsep = nfsmnt_mdssession(nmp);
4452 *tl++ = tsep->nfsess_clientid.lval[0];
4453 *tl = tsep->nfsess_clientid.lval[1];
4454 NFSBCOPY(lp->nfsl_owner, own, NFSV4CL_LOCKNAMELEN);
4455 NFSBCOPY(fh, &own[NFSV4CL_LOCKNAMELEN], fhlen);
4456 (void)nfsm_strtom(nd, own, NFSV4CL_LOCKNAMELEN + fhlen);
4458 nd->nd_flag |= ND_USEGSSNAME;
4459 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4460 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4463 error = nd->nd_repstat;
4464 mbuf_freem(nd->nd_mrep);
4469 * This function performs the Compound to get the mount pt FH.
4472 nfsrpc_getdirpath(struct nfsmount *nmp, u_char *dirpath, struct ucred *cred,
4476 struct nfsrv_descript nfsd;
4477 struct nfsrv_descript *nd = &nfsd;
4479 int error, cnt, len, setnil;
4482 nfscl_reqstart(nd, NFSPROC_PUTROOTFH, nmp, NULL, 0, &opcntp, NULL, 0,
4491 while (*cp2 != '\0' && *cp2 != '/')
4498 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
4499 *tl = txdr_unsigned(NFSV4OP_LOOKUP);
4500 nfsm_strtom(nd, cp, strlen(cp));
4506 } while (*cp != '\0');
4507 if (NFSHASNFSV4N(nmp))
4508 /* Has a Sequence Op done by nfscl_reqstart(). */
4509 *opcntp = txdr_unsigned(3 + cnt);
4511 *opcntp = txdr_unsigned(2 + cnt);
4512 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
4513 *tl = txdr_unsigned(NFSV4OP_GETFH);
4514 nd->nd_flag |= ND_USEGSSNAME;
4515 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4516 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4519 if (nd->nd_repstat == 0) {
4520 NFSM_DISSECT(tl, u_int32_t *, (3 + 2 * cnt) * NFSX_UNSIGNED);
4521 tl += (2 + 2 * cnt);
4522 if ((len = fxdr_unsigned(int, *tl)) <= 0 ||
4524 nd->nd_repstat = NFSERR_BADXDR;
4526 nd->nd_repstat = nfsrv_mtostr(nd, nmp->nm_fh, len);
4527 if (nd->nd_repstat == 0)
4528 nmp->nm_fhsize = len;
4531 error = nd->nd_repstat;
4533 mbuf_freem(nd->nd_mrep);
4538 * This function performs the Delegreturn RPC.
4541 nfsrpc_delegreturn(struct nfscldeleg *dp, struct ucred *cred,
4542 struct nfsmount *nmp, NFSPROC_T *p, int syscred)
4545 struct nfsrv_descript nfsd;
4546 struct nfsrv_descript *nd = &nfsd;
4549 nfscl_reqstart(nd, NFSPROC_DELEGRETURN, nmp, dp->nfsdl_fh,
4550 dp->nfsdl_fhlen, NULL, NULL, 0, 0);
4551 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID);
4552 if (NFSHASNFSV4N(nmp))
4555 *tl++ = dp->nfsdl_stateid.seqid;
4556 *tl++ = dp->nfsdl_stateid.other[0];
4557 *tl++ = dp->nfsdl_stateid.other[1];
4558 *tl = dp->nfsdl_stateid.other[2];
4560 nd->nd_flag |= ND_USEGSSNAME;
4561 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4562 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4565 error = nd->nd_repstat;
4566 mbuf_freem(nd->nd_mrep);
4574 nfsrpc_getacl(vnode_t vp, struct ucred *cred, NFSPROC_T *p,
4575 struct acl *aclp, void *stuff)
4577 struct nfsrv_descript nfsd, *nd = &nfsd;
4579 nfsattrbit_t attrbits;
4580 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
4582 if (nfsrv_useacl == 0 || !NFSHASNFSV4(nmp))
4583 return (EOPNOTSUPP);
4584 NFSCL_REQSTART(nd, NFSPROC_GETACL, vp);
4585 NFSZERO_ATTRBIT(&attrbits);
4586 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_ACL);
4587 (void) nfsrv_putattrbit(nd, &attrbits);
4588 error = nfscl_request(nd, vp, p, cred, stuff);
4591 if (!nd->nd_repstat)
4592 error = nfsv4_loadattr(nd, vp, NULL, NULL, NULL, 0, NULL,
4593 NULL, NULL, NULL, aclp, 0, NULL, NULL, NULL, p, cred);
4595 error = nd->nd_repstat;
4596 mbuf_freem(nd->nd_mrep);
4604 nfsrpc_setacl(vnode_t vp, struct ucred *cred, NFSPROC_T *p,
4605 struct acl *aclp, void *stuff)
4608 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
4610 if (nfsrv_useacl == 0 || !NFSHASNFSV4(nmp))
4611 return (EOPNOTSUPP);
4612 error = nfsrpc_setattr(vp, NULL, aclp, cred, p, NULL, NULL, stuff);
4620 nfsrpc_setaclrpc(vnode_t vp, struct ucred *cred, NFSPROC_T *p,
4621 struct acl *aclp, nfsv4stateid_t *stateidp, void *stuff)
4623 struct nfsrv_descript nfsd, *nd = &nfsd;
4625 nfsattrbit_t attrbits;
4626 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
4628 if (!NFSHASNFSV4(nmp))
4629 return (EOPNOTSUPP);
4630 NFSCL_REQSTART(nd, NFSPROC_SETACL, vp);
4631 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID);
4632 NFSZERO_ATTRBIT(&attrbits);
4633 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_ACL);
4634 (void) nfsv4_fillattr(nd, vnode_mount(vp), vp, aclp, NULL, NULL, 0,
4635 &attrbits, NULL, NULL, 0, 0, 0, 0, (uint64_t)0, NULL);
4636 error = nfscl_request(nd, vp, p, cred, stuff);
4639 /* Don't care about the pre/postop attributes */
4640 mbuf_freem(nd->nd_mrep);
4641 return (nd->nd_repstat);
4645 * Do the NFSv4.1 Exchange ID.
4648 nfsrpc_exchangeid(struct nfsmount *nmp, struct nfsclclient *clp,
4649 struct nfssockreq *nrp, uint32_t exchflags, struct nfsclds **dspp,
4650 struct ucred *cred, NFSPROC_T *p)
4652 uint32_t *tl, v41flags;
4653 struct nfsrv_descript nfsd;
4654 struct nfsrv_descript *nd = &nfsd;
4655 struct nfsclds *dsp;
4656 struct timespec verstime;
4660 nfscl_reqstart(nd, NFSPROC_EXCHANGEID, nmp, NULL, 0, NULL, NULL, 0, 0);
4661 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_UNSIGNED);
4662 *tl++ = txdr_unsigned(nfsboottime.tv_sec); /* Client owner */
4663 *tl = txdr_unsigned(clp->nfsc_rev);
4664 (void) nfsm_strtom(nd, clp->nfsc_id, clp->nfsc_idlen);
4666 NFSM_BUILD(tl, uint32_t *, 3 * NFSX_UNSIGNED);
4667 *tl++ = txdr_unsigned(exchflags);
4668 *tl++ = txdr_unsigned(NFSV4EXCH_SP4NONE);
4670 /* Set the implementation id4 */
4671 *tl = txdr_unsigned(1);
4672 (void) nfsm_strtom(nd, "freebsd.org", strlen("freebsd.org"));
4673 (void) nfsm_strtom(nd, version, strlen(version));
4674 NFSM_BUILD(tl, uint32_t *, NFSX_V4TIME);
4675 verstime.tv_sec = 1293840000; /* Jan 1, 2011 */
4676 verstime.tv_nsec = 0;
4677 txdr_nfsv4time(&verstime, tl);
4678 nd->nd_flag |= ND_USEGSSNAME;
4679 error = newnfs_request(nd, nmp, NULL, nrp, NULL, p, cred,
4680 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4681 NFSCL_DEBUG(1, "exchangeid err=%d reps=%d\n", error,
4682 (int)nd->nd_repstat);
4685 if (nd->nd_repstat == 0) {
4686 NFSM_DISSECT(tl, uint32_t *, 6 * NFSX_UNSIGNED + NFSX_HYPER);
4687 len = fxdr_unsigned(int, *(tl + 7));
4688 if (len < 0 || len > NFSV4_OPAQUELIMIT) {
4689 error = NFSERR_BADXDR;
4692 dsp = malloc(sizeof(struct nfsclds) + len + 1, M_NFSCLDS,
4694 dsp->nfsclds_expire = NFSD_MONOSEC + clp->nfsc_renew;
4695 dsp->nfsclds_servownlen = len;
4696 dsp->nfsclds_sess.nfsess_clientid.lval[0] = *tl++;
4697 dsp->nfsclds_sess.nfsess_clientid.lval[1] = *tl++;
4698 dsp->nfsclds_sess.nfsess_sequenceid =
4699 fxdr_unsigned(uint32_t, *tl++);
4700 v41flags = fxdr_unsigned(uint32_t, *tl);
4701 if ((v41flags & NFSV4EXCH_USEPNFSMDS) != 0 &&
4702 NFSHASPNFSOPT(nmp)) {
4703 NFSCL_DEBUG(1, "set PNFS\n");
4705 nmp->nm_state |= NFSSTA_PNFS;
4707 dsp->nfsclds_flags |= NFSCLDS_MDS;
4709 if ((v41flags & NFSV4EXCH_USEPNFSDS) != 0)
4710 dsp->nfsclds_flags |= NFSCLDS_DS;
4712 nd->nd_repstat = nfsrv_mtostr(nd,
4713 dsp->nfsclds_serverown, len);
4714 if (nd->nd_repstat == 0) {
4715 mtx_init(&dsp->nfsclds_mtx, "nfsds", NULL, MTX_DEF);
4716 mtx_init(&dsp->nfsclds_sess.nfsess_mtx, "nfssession",
4718 nfscl_initsessionslots(&dsp->nfsclds_sess);
4721 free(dsp, M_NFSCLDS);
4723 error = nd->nd_repstat;
4725 mbuf_freem(nd->nd_mrep);
4730 * Do the NFSv4.1 Create Session.
4733 nfsrpc_createsession(struct nfsmount *nmp, struct nfsclsession *sep,
4734 struct nfssockreq *nrp, uint32_t sequenceid, int mds, struct ucred *cred,
4737 uint32_t crflags, maxval, *tl;
4738 struct nfsrv_descript nfsd;
4739 struct nfsrv_descript *nd = &nfsd;
4742 /* Make sure nm_rsize, nm_wsize is set. */
4743 if (nmp->nm_rsize > NFS_MAXBSIZE || nmp->nm_rsize == 0)
4744 nmp->nm_rsize = NFS_MAXBSIZE;
4745 if (nmp->nm_wsize > NFS_MAXBSIZE || nmp->nm_wsize == 0)
4746 nmp->nm_wsize = NFS_MAXBSIZE;
4747 nfscl_reqstart(nd, NFSPROC_CREATESESSION, nmp, NULL, 0, NULL, NULL, 0,
4749 NFSM_BUILD(tl, uint32_t *, 4 * NFSX_UNSIGNED);
4750 *tl++ = sep->nfsess_clientid.lval[0];
4751 *tl++ = sep->nfsess_clientid.lval[1];
4752 *tl++ = txdr_unsigned(sequenceid);
4753 crflags = (NFSMNT_RDONLY(nmp->nm_mountp) ? 0 : NFSV4CRSESS_PERSIST);
4754 if (nfscl_enablecallb != 0 && nfs_numnfscbd > 0 && mds != 0)
4755 crflags |= NFSV4CRSESS_CONNBACKCHAN;
4756 *tl = txdr_unsigned(crflags);
4758 /* Fill in fore channel attributes. */
4759 NFSM_BUILD(tl, uint32_t *, 7 * NFSX_UNSIGNED);
4760 *tl++ = 0; /* Header pad size */
4761 *tl++ = txdr_unsigned(nmp->nm_wsize + NFS_MAXXDR);/* Max request size */
4762 *tl++ = txdr_unsigned(nmp->nm_rsize + NFS_MAXXDR);/* Max reply size */
4763 *tl++ = txdr_unsigned(4096); /* Max response size cached */
4764 *tl++ = txdr_unsigned(20); /* Max operations */
4765 *tl++ = txdr_unsigned(64); /* Max slots */
4766 *tl = 0; /* No rdma ird */
4768 /* Fill in back channel attributes. */
4769 NFSM_BUILD(tl, uint32_t *, 7 * NFSX_UNSIGNED);
4770 *tl++ = 0; /* Header pad size */
4771 *tl++ = txdr_unsigned(10000); /* Max request size */
4772 *tl++ = txdr_unsigned(10000); /* Max response size */
4773 *tl++ = txdr_unsigned(4096); /* Max response size cached */
4774 *tl++ = txdr_unsigned(4); /* Max operations */
4775 *tl++ = txdr_unsigned(NFSV4_CBSLOTS); /* Max slots */
4776 *tl = 0; /* No rdma ird */
4778 NFSM_BUILD(tl, uint32_t *, 8 * NFSX_UNSIGNED);
4779 *tl++ = txdr_unsigned(NFS_CALLBCKPROG); /* Call back prog # */
4781 /* Allow AUTH_SYS callbacks as uid, gid == 0. */
4782 *tl++ = txdr_unsigned(1); /* Auth_sys only */
4783 *tl++ = txdr_unsigned(AUTH_SYS); /* AUTH_SYS type */
4784 *tl++ = txdr_unsigned(nfsboottime.tv_sec); /* time stamp */
4785 *tl++ = 0; /* Null machine name */
4786 *tl++ = 0; /* Uid == 0 */
4787 *tl++ = 0; /* Gid == 0 */
4788 *tl = 0; /* No additional gids */
4789 nd->nd_flag |= ND_USEGSSNAME;
4790 error = newnfs_request(nd, nmp, NULL, nrp, NULL, p, cred, NFS_PROG,
4791 NFS_VER4, NULL, 1, NULL, NULL);
4794 if (nd->nd_repstat == 0) {
4795 NFSM_DISSECT(tl, uint32_t *, NFSX_V4SESSIONID +
4797 bcopy(tl, sep->nfsess_sessionid, NFSX_V4SESSIONID);
4798 tl += NFSX_V4SESSIONID / NFSX_UNSIGNED;
4799 sep->nfsess_sequenceid = fxdr_unsigned(uint32_t, *tl++);
4800 crflags = fxdr_unsigned(uint32_t, *tl);
4801 if ((crflags & NFSV4CRSESS_PERSIST) != 0 && mds != 0) {
4803 nmp->nm_state |= NFSSTA_SESSPERSIST;
4807 /* Get the fore channel slot count. */
4808 NFSM_DISSECT(tl, uint32_t *, 7 * NFSX_UNSIGNED);
4809 tl++; /* Skip the header pad size. */
4811 /* Make sure nm_wsize is small enough. */
4812 maxval = fxdr_unsigned(uint32_t, *tl++);
4813 while (maxval < nmp->nm_wsize + NFS_MAXXDR) {
4814 if (nmp->nm_wsize > 8096)
4820 /* Make sure nm_rsize is small enough. */
4821 maxval = fxdr_unsigned(uint32_t, *tl++);
4822 while (maxval < nmp->nm_rsize + NFS_MAXXDR) {
4823 if (nmp->nm_rsize > 8096)
4829 sep->nfsess_maxcache = fxdr_unsigned(int, *tl++);
4831 sep->nfsess_foreslots = fxdr_unsigned(uint16_t, *tl++);
4832 NFSCL_DEBUG(4, "fore slots=%d\n", (int)sep->nfsess_foreslots);
4833 irdcnt = fxdr_unsigned(int, *tl);
4835 NFSM_DISSECT(tl, uint32_t *, irdcnt * NFSX_UNSIGNED);
4837 /* and the back channel slot count. */
4838 NFSM_DISSECT(tl, uint32_t *, 7 * NFSX_UNSIGNED);
4840 sep->nfsess_backslots = fxdr_unsigned(uint16_t, *tl);
4841 NFSCL_DEBUG(4, "back slots=%d\n", (int)sep->nfsess_backslots);
4843 error = nd->nd_repstat;
4845 mbuf_freem(nd->nd_mrep);
4850 * Do the NFSv4.1 Destroy Session.
4853 nfsrpc_destroysession(struct nfsmount *nmp, struct nfsclclient *clp,
4854 struct ucred *cred, NFSPROC_T *p)
4857 struct nfsrv_descript nfsd;
4858 struct nfsrv_descript *nd = &nfsd;
4860 struct nfsclsession *tsep;
4862 nfscl_reqstart(nd, NFSPROC_DESTROYSESSION, nmp, NULL, 0, NULL, NULL, 0,
4864 NFSM_BUILD(tl, uint32_t *, NFSX_V4SESSIONID);
4865 tsep = nfsmnt_mdssession(nmp);
4866 bcopy(tsep->nfsess_sessionid, tl, NFSX_V4SESSIONID);
4867 nd->nd_flag |= ND_USEGSSNAME;
4868 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4869 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4872 error = nd->nd_repstat;
4873 mbuf_freem(nd->nd_mrep);
4878 * Do the NFSv4.1 Destroy Client.
4881 nfsrpc_destroyclient(struct nfsmount *nmp, struct nfsclclient *clp,
4882 struct ucred *cred, NFSPROC_T *p)
4885 struct nfsrv_descript nfsd;
4886 struct nfsrv_descript *nd = &nfsd;
4888 struct nfsclsession *tsep;
4890 nfscl_reqstart(nd, NFSPROC_DESTROYCLIENT, nmp, NULL, 0, NULL, NULL, 0,
4892 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_UNSIGNED);
4893 tsep = nfsmnt_mdssession(nmp);
4894 *tl++ = tsep->nfsess_clientid.lval[0];
4895 *tl = tsep->nfsess_clientid.lval[1];
4896 nd->nd_flag |= ND_USEGSSNAME;
4897 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4898 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4901 error = nd->nd_repstat;
4902 mbuf_freem(nd->nd_mrep);
4907 * Do the NFSv4.1 LayoutGet.
4910 nfsrpc_layoutget(struct nfsmount *nmp, uint8_t *fhp, int fhlen, int iomode,
4911 uint64_t offset, uint64_t len, uint64_t minlen, int layouttype,
4912 int layoutlen, nfsv4stateid_t *stateidp, int *retonclosep,
4913 struct nfsclflayouthead *flhp, struct ucred *cred, NFSPROC_T *p,
4916 struct nfsrv_descript nfsd, *nd = &nfsd;
4919 nfscl_reqstart(nd, NFSPROC_LAYOUTGET, nmp, fhp, fhlen, NULL, NULL, 0,
4921 nfsrv_setuplayoutget(nd, iomode, offset, len, minlen, stateidp,
4922 layouttype, layoutlen, 0);
4923 nd->nd_flag |= ND_USEGSSNAME;
4924 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4925 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4926 NFSCL_DEBUG(4, "layget err=%d st=%d\n", error, nd->nd_repstat);
4929 if (nd->nd_repstat == 0)
4930 error = nfsrv_parselayoutget(nd, stateidp, retonclosep, flhp);
4931 if (error == 0 && nd->nd_repstat != 0)
4932 error = nd->nd_repstat;
4933 mbuf_freem(nd->nd_mrep);
4938 * Do the NFSv4.1 Get Device Info.
4941 nfsrpc_getdeviceinfo(struct nfsmount *nmp, uint8_t *deviceid, int layouttype,
4942 uint32_t *notifybitsp, struct nfscldevinfo **ndip, struct ucred *cred,
4945 uint32_t cnt, *tl, vers, minorvers;
4946 struct nfsrv_descript nfsd;
4947 struct nfsrv_descript *nd = &nfsd;
4948 struct sockaddr_in sin, ssin;
4949 struct sockaddr_in6 sin6, ssin6;
4950 struct nfsclds *dsp = NULL, **dspp, **gotdspp;
4951 struct nfscldevinfo *ndi;
4952 int addrcnt = 0, bitcnt, error, gotvers, i, isudp, j, stripecnt;
4953 uint8_t stripeindex;
4954 sa_family_t af, safilled;
4959 nfscl_reqstart(nd, NFSPROC_GETDEVICEINFO, nmp, NULL, 0, NULL, NULL, 0,
4961 NFSM_BUILD(tl, uint32_t *, NFSX_V4DEVICEID + 3 * NFSX_UNSIGNED);
4962 NFSBCOPY(deviceid, tl, NFSX_V4DEVICEID);
4963 tl += (NFSX_V4DEVICEID / NFSX_UNSIGNED);
4964 *tl++ = txdr_unsigned(layouttype);
4965 *tl++ = txdr_unsigned(100000);
4966 if (notifybitsp != NULL && *notifybitsp != 0) {
4967 *tl = txdr_unsigned(1); /* One word of bits. */
4968 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
4969 *tl = txdr_unsigned(*notifybitsp);
4971 *tl = txdr_unsigned(0);
4972 nd->nd_flag |= ND_USEGSSNAME;
4973 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4974 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4977 if (nd->nd_repstat == 0) {
4978 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
4979 if (layouttype != fxdr_unsigned(int, *tl))
4980 printf("EEK! devinfo layout type not same!\n");
4981 if (layouttype == NFSLAYOUT_NFSV4_1_FILES) {
4982 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
4983 stripecnt = fxdr_unsigned(int, *tl);
4984 NFSCL_DEBUG(4, "stripecnt=%d\n", stripecnt);
4985 if (stripecnt < 1 || stripecnt > 4096) {
4986 printf("pNFS File layout devinfo stripecnt %d:"
4987 " out of range\n", stripecnt);
4988 error = NFSERR_BADXDR;
4991 NFSM_DISSECT(tl, uint32_t *, (stripecnt + 1) *
4993 addrcnt = fxdr_unsigned(int, *(tl + stripecnt));
4994 NFSCL_DEBUG(4, "addrcnt=%d\n", addrcnt);
4995 if (addrcnt < 1 || addrcnt > 128) {
4996 printf("NFS devinfo addrcnt %d: out of range\n",
4998 error = NFSERR_BADXDR;
5003 * Now we know how many stripe indices and addresses, so
5004 * we can allocate the structure the correct size.
5006 i = (stripecnt * sizeof(uint8_t)) /
5007 sizeof(struct nfsclds *) + 1;
5008 NFSCL_DEBUG(4, "stripeindices=%d\n", i);
5009 ndi = malloc(sizeof(*ndi) + (addrcnt + i) *
5010 sizeof(struct nfsclds *), M_NFSDEVINFO, M_WAITOK |
5012 NFSBCOPY(deviceid, ndi->nfsdi_deviceid,
5014 ndi->nfsdi_refcnt = 0;
5015 ndi->nfsdi_flags = NFSDI_FILELAYOUT;
5016 ndi->nfsdi_stripecnt = stripecnt;
5017 ndi->nfsdi_addrcnt = addrcnt;
5018 /* Fill in the stripe indices. */
5019 for (i = 0; i < stripecnt; i++) {
5020 stripeindex = fxdr_unsigned(uint8_t, *tl++);
5021 NFSCL_DEBUG(4, "stripeind=%d\n", stripeindex);
5022 if (stripeindex >= addrcnt) {
5023 printf("pNFS File Layout devinfo"
5024 " stripeindex %d: too big\n",
5026 error = NFSERR_BADXDR;
5029 nfsfldi_setstripeindex(ndi, i, stripeindex);
5031 } else if (layouttype == NFSLAYOUT_FLEXFILE) {
5032 /* For Flex File, we only get one address list. */
5033 ndi = malloc(sizeof(*ndi) + sizeof(struct nfsclds *),
5034 M_NFSDEVINFO, M_WAITOK | M_ZERO);
5035 NFSBCOPY(deviceid, ndi->nfsdi_deviceid,
5037 ndi->nfsdi_refcnt = 0;
5038 ndi->nfsdi_flags = NFSDI_FLEXFILE;
5039 addrcnt = ndi->nfsdi_addrcnt = 1;
5042 /* Now, dissect the server address(es). */
5043 safilled = AF_UNSPEC;
5044 for (i = 0; i < addrcnt; i++) {
5045 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
5046 cnt = fxdr_unsigned(uint32_t, *tl);
5048 printf("NFS devinfo 0 len addrlist\n");
5049 error = NFSERR_BADXDR;
5052 dspp = nfsfldi_addr(ndi, i);
5053 safilled = AF_UNSPEC;
5054 for (j = 0; j < cnt; j++) {
5055 error = nfsv4_getipaddr(nd, &sin, &sin6, &af,
5057 if (error != 0 && error != EPERM) {
5058 error = NFSERR_BADXDR;
5061 if (error == 0 && isudp == 0) {
5064 * - Same address family.
5065 * Save the address and dspp, so that
5066 * the connection can be done after
5067 * parsing is complete.
5069 if (safilled == AF_UNSPEC ||
5070 (af == nmp->nm_nam->sa_family &&
5071 safilled != nmp->nm_nam->sa_family)
5084 gotvers = NFS_VER4; /* Always NFSv4 for File Layout. */
5085 /* For Flex File, we will take one of the versions to use. */
5086 if (layouttype == NFSLAYOUT_FLEXFILE) {
5087 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
5088 j = fxdr_unsigned(int, *tl);
5089 if (j < 1 || j > NFSDEV_MAXVERS) {
5090 printf("pNFS: too many versions\n");
5091 error = NFSERR_BADXDR;
5095 for (i = 0; i < j; i++) {
5096 NFSM_DISSECT(tl, uint32_t *, 5 * NFSX_UNSIGNED);
5097 vers = fxdr_unsigned(uint32_t, *tl++);
5098 minorvers = fxdr_unsigned(uint32_t, *tl++);
5099 if ((vers == NFS_VER4 && minorvers ==
5100 NFSV41_MINORVERSION) || (vers == NFS_VER3 &&
5103 /* We'll take this one. */
5104 ndi->nfsdi_versindex = i;
5105 ndi->nfsdi_vers = vers;
5106 ndi->nfsdi_minorvers = minorvers;
5107 ndi->nfsdi_rsize = fxdr_unsigned(
5109 ndi->nfsdi_wsize = fxdr_unsigned(
5111 if (*tl == newnfs_true)
5116 ~NFSDI_TIGHTCOUPLED;
5120 printf("pNFS: no NFSv3 or NFSv4.1\n");
5121 error = NFSERR_BADXDR;
5126 /* And the notify bits. */
5127 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
5128 bitcnt = fxdr_unsigned(int, *tl);
5130 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
5131 if (notifybitsp != NULL)
5133 fxdr_unsigned(uint32_t, *tl);
5135 if (safilled != AF_UNSPEC) {
5136 KASSERT(ndi != NULL, ("ndi is NULL"));
5142 * Now we can do a TCP connection for the correct
5143 * NFS version and IP address.
5145 error = nfsrpc_fillsa(nmp, &ssin, &ssin6, safilled,
5149 KASSERT(gotdspp != NULL, ("gotdspp is NULL"));
5153 if (nd->nd_repstat != 0 && error == 0)
5154 error = nd->nd_repstat;
5156 if (error != 0 && ndi != NULL)
5157 nfscl_freedevinfo(ndi);
5158 mbuf_freem(nd->nd_mrep);
5163 * Do the NFSv4.1 LayoutCommit.
5166 nfsrpc_layoutcommit(struct nfsmount *nmp, uint8_t *fh, int fhlen, int reclaim,
5167 uint64_t off, uint64_t len, uint64_t lastbyte, nfsv4stateid_t *stateidp,
5168 int layouttype, struct ucred *cred, NFSPROC_T *p, void *stuff)
5171 struct nfsrv_descript nfsd, *nd = &nfsd;
5174 nfscl_reqstart(nd, NFSPROC_LAYOUTCOMMIT, nmp, fh, fhlen, NULL, NULL,
5176 NFSM_BUILD(tl, uint32_t *, 5 * NFSX_UNSIGNED + 3 * NFSX_HYPER +
5178 txdr_hyper(off, tl);
5180 txdr_hyper(len, tl);
5183 *tl++ = newnfs_true;
5185 *tl++ = newnfs_false;
5186 *tl++ = txdr_unsigned(stateidp->seqid);
5187 *tl++ = stateidp->other[0];
5188 *tl++ = stateidp->other[1];
5189 *tl++ = stateidp->other[2];
5190 *tl++ = newnfs_true;
5193 else if (lastbyte >= (off + len))
5194 lastbyte = off + len - 1;
5195 txdr_hyper(lastbyte, tl);
5197 *tl++ = newnfs_false;
5198 *tl++ = txdr_unsigned(layouttype);
5199 /* All supported layouts are 0 length. */
5200 *tl = txdr_unsigned(0);
5201 nd->nd_flag |= ND_USEGSSNAME;
5202 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
5203 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
5206 error = nd->nd_repstat;
5207 mbuf_freem(nd->nd_mrep);
5212 * Do the NFSv4.1 LayoutReturn.
5215 nfsrpc_layoutreturn(struct nfsmount *nmp, uint8_t *fh, int fhlen, int reclaim,
5216 int layouttype, uint32_t iomode, int layoutreturn, uint64_t offset,
5217 uint64_t len, nfsv4stateid_t *stateidp, struct ucred *cred, NFSPROC_T *p,
5218 uint32_t stat, uint32_t op, char *devid)
5221 struct nfsrv_descript nfsd, *nd = &nfsd;
5225 nfscl_reqstart(nd, NFSPROC_LAYOUTRETURN, nmp, fh, fhlen, NULL, NULL,
5227 NFSM_BUILD(tl, uint32_t *, 4 * NFSX_UNSIGNED);
5229 *tl++ = newnfs_true;
5231 *tl++ = newnfs_false;
5232 *tl++ = txdr_unsigned(layouttype);
5233 *tl++ = txdr_unsigned(iomode);
5234 *tl = txdr_unsigned(layoutreturn);
5235 if (layoutreturn == NFSLAYOUTRETURN_FILE) {
5236 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_HYPER + NFSX_STATEID +
5238 txdr_hyper(offset, tl);
5240 txdr_hyper(len, tl);
5242 NFSCL_DEBUG(4, "layoutret stseq=%d\n", (int)stateidp->seqid);
5243 *tl++ = txdr_unsigned(stateidp->seqid);
5244 *tl++ = stateidp->other[0];
5245 *tl++ = stateidp->other[1];
5246 *tl++ = stateidp->other[2];
5247 if (layouttype == NFSLAYOUT_NFSV4_1_FILES)
5248 *tl = txdr_unsigned(0);
5249 else if (layouttype == NFSLAYOUT_FLEXFILE) {
5251 *tl = txdr_unsigned(2 * NFSX_HYPER +
5252 NFSX_STATEID + NFSX_V4DEVICEID + 5 *
5254 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_HYPER +
5255 NFSX_STATEID + NFSX_V4DEVICEID + 5 *
5257 *tl++ = txdr_unsigned(1); /* One error. */
5258 tu64 = 0; /* Offset. */
5259 txdr_hyper(tu64, tl); tl += 2;
5260 tu64 = UINT64_MAX; /* Length. */
5261 txdr_hyper(tu64, tl); tl += 2;
5262 NFSBCOPY(stateidp, tl, NFSX_STATEID);
5263 tl += (NFSX_STATEID / NFSX_UNSIGNED);
5264 *tl++ = txdr_unsigned(1); /* One error. */
5265 NFSBCOPY(devid, tl, NFSX_V4DEVICEID);
5266 tl += (NFSX_V4DEVICEID / NFSX_UNSIGNED);
5267 *tl++ = txdr_unsigned(stat);
5268 *tl++ = txdr_unsigned(op);
5270 *tl = txdr_unsigned(2 * NFSX_UNSIGNED);
5271 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_UNSIGNED);
5275 *tl = 0; /* No stats yet. */
5278 nd->nd_flag |= ND_USEGSSNAME;
5279 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
5280 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
5283 if (nd->nd_repstat == 0) {
5284 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
5286 NFSM_DISSECT(tl, uint32_t *, NFSX_STATEID);
5287 stateidp->seqid = fxdr_unsigned(uint32_t, *tl++);
5288 stateidp->other[0] = *tl++;
5289 stateidp->other[1] = *tl++;
5290 stateidp->other[2] = *tl;
5293 error = nd->nd_repstat;
5295 mbuf_freem(nd->nd_mrep);
5300 * Acquire a layout and devinfo, if possible. The caller must have acquired
5301 * a reference count on the nfsclclient structure before calling this.
5302 * Return the layout in lypp with a reference count on it, if successful.
5305 nfsrpc_getlayout(struct nfsmount *nmp, vnode_t vp, struct nfsfh *nfhp,
5306 int iomode, uint32_t *notifybitsp, nfsv4stateid_t *stateidp, uint64_t off,
5307 struct nfscllayout **lypp, struct ucred *cred, NFSPROC_T *p)
5309 struct nfscllayout *lyp;
5310 struct nfsclflayout *flp;
5311 struct nfsclflayouthead flh;
5312 int error = 0, islocked, layoutlen, layouttype, recalled, retonclose;
5313 nfsv4stateid_t stateid;
5314 struct nfsclsession *tsep;
5317 if (NFSHASFLEXFILE(nmp))
5318 layouttype = NFSLAYOUT_FLEXFILE;
5320 layouttype = NFSLAYOUT_NFSV4_1_FILES;
5322 * If lyp is returned non-NULL, there will be a refcnt (shared lock)
5323 * on it, iff flp != NULL or a lock (exclusive lock) on it iff
5326 lyp = nfscl_getlayout(nmp->nm_clp, nfhp->nfh_fh, nfhp->nfh_len,
5327 off, &flp, &recalled);
5329 if (lyp == NULL || flp == NULL) {
5333 tsep = nfsmnt_mdssession(nmp);
5334 layoutlen = tsep->nfsess_maxcache -
5335 (NFSX_STATEID + 3 * NFSX_UNSIGNED);
5338 stateid.other[0] = stateidp->other[0];
5339 stateid.other[1] = stateidp->other[1];
5340 stateid.other[2] = stateidp->other[2];
5341 error = nfsrpc_layoutget(nmp, nfhp->nfh_fh,
5342 nfhp->nfh_len, iomode, (uint64_t)0, UINT64_MAX,
5343 (uint64_t)0, layouttype, layoutlen, &stateid,
5344 &retonclose, &flh, cred, p, NULL);
5347 stateid.seqid = lyp->nfsly_stateid.seqid;
5348 stateid.other[0] = lyp->nfsly_stateid.other[0];
5349 stateid.other[1] = lyp->nfsly_stateid.other[1];
5350 stateid.other[2] = lyp->nfsly_stateid.other[2];
5351 error = nfsrpc_layoutget(nmp, nfhp->nfh_fh,
5352 nfhp->nfh_len, iomode, off, UINT64_MAX,
5353 (uint64_t)0, layouttype, layoutlen, &stateid,
5354 &retonclose, &flh, cred, p, NULL);
5356 error = nfsrpc_layoutgetres(nmp, vp, nfhp->nfh_fh,
5357 nfhp->nfh_len, &stateid, retonclose, notifybitsp, &lyp,
5358 &flh, layouttype, error, NULL, cred, p);
5361 else if (islocked != 0)
5362 nfscl_rellayout(lyp, 1);
5369 * Do a TCP connection plus exchange id and create session.
5370 * If successful, a "struct nfsclds" is linked into the list for the
5371 * mount point and a pointer to it is returned.
5374 nfsrpc_fillsa(struct nfsmount *nmp, struct sockaddr_in *sin,
5375 struct sockaddr_in6 *sin6, sa_family_t af, int vers, struct nfsclds **dspp,
5378 struct sockaddr_in *msad, *sad;
5379 struct sockaddr_in6 *msad6, *sad6;
5380 struct nfsclclient *clp;
5381 struct nfssockreq *nrp;
5382 struct nfsclds *dsp, *tdsp;
5384 enum nfsclds_state retv;
5385 uint32_t sequenceid;
5387 KASSERT(nmp->nm_sockreq.nr_cred != NULL,
5388 ("nfsrpc_fillsa: NULL nr_cred"));
5394 if (af == AF_INET) {
5397 * Check to see if we already have a session for this
5398 * address that is usable for a DS.
5399 * Note that the MDS's address is in a different place
5400 * than the sessions already acquired for DS's.
5402 msad = (struct sockaddr_in *)nmp->nm_sockreq.nr_nam;
5403 tdsp = TAILQ_FIRST(&nmp->nm_sess);
5404 while (tdsp != NULL) {
5405 if (msad != NULL && msad->sin_family == AF_INET &&
5406 sin->sin_addr.s_addr == msad->sin_addr.s_addr &&
5407 sin->sin_port == msad->sin_port &&
5408 (tdsp->nfsclds_flags & NFSCLDS_DS) != 0 &&
5409 tdsp->nfsclds_sess.nfsess_defunct == 0) {
5412 NFSCL_DEBUG(4, "fnd same addr\n");
5415 tdsp = TAILQ_NEXT(tdsp, nfsclds_list);
5416 if (tdsp != NULL && tdsp->nfsclds_sockp != NULL)
5417 msad = (struct sockaddr_in *)
5418 tdsp->nfsclds_sockp->nr_nam;
5424 /* No IP address match, so look for new/trunked one. */
5425 sad = malloc(sizeof(*sad), M_SONAME, M_WAITOK | M_ZERO);
5426 sad->sin_len = sizeof(*sad);
5427 sad->sin_family = AF_INET;
5428 sad->sin_port = sin->sin_port;
5429 sad->sin_addr.s_addr = sin->sin_addr.s_addr;
5430 nrp = malloc(sizeof(*nrp), M_NFSSOCKREQ, M_WAITOK | M_ZERO);
5431 nrp->nr_nam = (struct sockaddr *)sad;
5432 } else if (af == AF_INET6) {
5435 * Check to see if we already have a session for this
5436 * address that is usable for a DS.
5437 * Note that the MDS's address is in a different place
5438 * than the sessions already acquired for DS's.
5440 msad6 = (struct sockaddr_in6 *)nmp->nm_sockreq.nr_nam;
5441 tdsp = TAILQ_FIRST(&nmp->nm_sess);
5442 while (tdsp != NULL) {
5443 if (msad6 != NULL && msad6->sin6_family == AF_INET6 &&
5444 IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr,
5445 &msad6->sin6_addr) &&
5446 sin6->sin6_port == msad6->sin6_port &&
5447 (tdsp->nfsclds_flags & NFSCLDS_DS) != 0 &&
5448 tdsp->nfsclds_sess.nfsess_defunct == 0) {
5453 tdsp = TAILQ_NEXT(tdsp, nfsclds_list);
5454 if (tdsp != NULL && tdsp->nfsclds_sockp != NULL)
5455 msad6 = (struct sockaddr_in6 *)
5456 tdsp->nfsclds_sockp->nr_nam;
5462 /* No IP address match, so look for new/trunked one. */
5463 sad6 = malloc(sizeof(*sad6), M_SONAME, M_WAITOK | M_ZERO);
5464 sad6->sin6_len = sizeof(*sad6);
5465 sad6->sin6_family = AF_INET6;
5466 sad6->sin6_port = sin6->sin6_port;
5467 NFSBCOPY(&sin6->sin6_addr, &sad6->sin6_addr,
5468 sizeof(struct in6_addr));
5469 nrp = malloc(sizeof(*nrp), M_NFSSOCKREQ, M_WAITOK | M_ZERO);
5470 nrp->nr_nam = (struct sockaddr *)sad6;
5474 nrp->nr_sotype = SOCK_STREAM;
5475 mtx_init(&nrp->nr_mtx, "nfssock", NULL, MTX_DEF);
5476 nrp->nr_prog = NFS_PROG;
5477 nrp->nr_vers = vers;
5480 * Use the credentials that were used for the mount, which are
5481 * in nmp->nm_sockreq.nr_cred for newnfs_connect() etc.
5482 * Ref. counting the credentials with crhold() is probably not
5483 * necessary, since nm_sockreq.nr_cred won't be crfree()'d until
5484 * unmount, but I did it anyhow.
5486 nrp->nr_cred = crhold(nmp->nm_sockreq.nr_cred);
5487 error = newnfs_connect(nmp, nrp, NULL, p, 0);
5488 NFSCL_DEBUG(3, "DS connect=%d\n", error);
5491 /* Now, do the exchangeid and create session. */
5493 if (vers == NFS_VER4) {
5494 error = nfsrpc_exchangeid(nmp, clp, nrp,
5495 NFSV4EXCH_USEPNFSDS, &dsp, nrp->nr_cred, p);
5496 NFSCL_DEBUG(3, "DS exchangeid=%d\n", error);
5498 newnfs_disconnect(nrp);
5500 dsp = malloc(sizeof(struct nfsclds), M_NFSCLDS,
5502 dsp->nfsclds_flags |= NFSCLDS_DS;
5503 dsp->nfsclds_expire = INT32_MAX; /* No renews needed. */
5504 mtx_init(&dsp->nfsclds_mtx, "nfsds", NULL, MTX_DEF);
5505 mtx_init(&dsp->nfsclds_sess.nfsess_mtx, "nfssession",
5510 dsp->nfsclds_sockp = nrp;
5511 if (vers == NFS_VER4) {
5513 retv = nfscl_getsameserver(nmp, dsp, &tdsp,
5515 NFSCL_DEBUG(3, "getsame ret=%d\n", retv);
5516 if (retv == NFSDSP_USETHISSESSION &&
5517 nfscl_dssameconn != 0) {
5519 tdsp->nfsclds_flags |= NFSCLDS_SAMECONN;
5523 * If there is already a session for this
5526 (void)newnfs_disconnect(nrp);
5527 nfscl_freenfsclds(dsp);
5531 if (retv == NFSDSP_NOTFOUND)
5533 dsp->nfsclds_sess.nfsess_sequenceid;
5535 error = nfsrpc_createsession(nmp, &dsp->nfsclds_sess,
5536 nrp, sequenceid, 0, nrp->nr_cred, p);
5537 NFSCL_DEBUG(3, "DS createsess=%d\n", error);
5540 NFSFREECRED(nrp->nr_cred);
5541 NFSFREEMUTEX(&nrp->nr_mtx);
5542 free(nrp->nr_nam, M_SONAME);
5543 free(nrp, M_NFSSOCKREQ);
5546 NFSCL_DEBUG(3, "add DS session\n");
5548 * Put it at the end of the list. That way the list
5549 * is ordered by when the entry was added. This matters
5550 * since the one done first is the one that should be
5551 * used for sequencid'ing any subsequent create sessions.
5554 TAILQ_INSERT_TAIL(&nmp->nm_sess, dsp, nfsclds_list);
5557 } else if (dsp != NULL) {
5558 newnfs_disconnect(nrp);
5559 nfscl_freenfsclds(dsp);
5565 * Do the NFSv4.1 Reclaim Complete.
5568 nfsrpc_reclaimcomplete(struct nfsmount *nmp, struct ucred *cred, NFSPROC_T *p)
5571 struct nfsrv_descript nfsd;
5572 struct nfsrv_descript *nd = &nfsd;
5575 nfscl_reqstart(nd, NFSPROC_RECLAIMCOMPL, nmp, NULL, 0, NULL, NULL, 0,
5577 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
5579 nd->nd_flag |= ND_USEGSSNAME;
5580 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
5581 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
5584 error = nd->nd_repstat;
5585 mbuf_freem(nd->nd_mrep);
5590 * Initialize the slot tables for a session.
5593 nfscl_initsessionslots(struct nfsclsession *sep)
5597 for (i = 0; i < NFSV4_CBSLOTS; i++) {
5598 if (sep->nfsess_cbslots[i].nfssl_reply != NULL)
5599 m_freem(sep->nfsess_cbslots[i].nfssl_reply);
5600 NFSBZERO(&sep->nfsess_cbslots[i], sizeof(struct nfsslot));
5602 for (i = 0; i < 64; i++)
5603 sep->nfsess_slotseq[i] = 0;
5604 sep->nfsess_slots = 0;
5608 * Called to try and do an I/O operation via an NFSv4.1 Data Server (DS).
5611 nfscl_doiods(vnode_t vp, struct uio *uiop, int *iomode, int *must_commit,
5612 uint32_t rwaccess, int docommit, struct ucred *cred, NFSPROC_T *p)
5614 struct nfsnode *np = VTONFS(vp);
5615 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
5616 struct nfscllayout *layp;
5617 struct nfscldevinfo *dip;
5618 struct nfsclflayout *rflp;
5620 struct nfsclwritedsdorpc *drpc, *tdrpc;
5621 nfsv4stateid_t stateid;
5622 struct ucred *newcred;
5623 uint64_t lastbyte, len, off, oresid, xfer;
5624 int eof, error, firstmirror, i, iolaymode, mirrorcnt, recalled, timo;
5627 void *iovbase = NULL;
5632 if (!NFSHASPNFS(nmp) || nfscl_enablecallb == 0 || nfs_numnfscbd == 0 ||
5633 (np->n_flag & NNOLAYOUT) != 0)
5635 /* Now, get a reference cnt on the clientid for this mount. */
5636 if (nfscl_getref(nmp) == 0)
5639 /* Find an appropriate stateid. */
5640 newcred = NFSNEWCRED(cred);
5641 error = nfscl_getstateid(vp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len,
5642 rwaccess, 1, newcred, p, &stateid, &lckp);
5644 NFSFREECRED(newcred);
5648 /* Search for a layout for this file. */
5649 off = uiop->uio_offset;
5650 layp = nfscl_getlayout(nmp->nm_clp, np->n_fhp->nfh_fh,
5651 np->n_fhp->nfh_len, off, &rflp, &recalled);
5652 if (layp == NULL || rflp == NULL) {
5653 if (recalled != 0) {
5654 NFSFREECRED(newcred);
5659 nfscl_rellayout(layp, (rflp == NULL) ? 1 : 0);
5662 /* Try and get a Layout, if it is supported. */
5663 if (rwaccess == NFSV4OPEN_ACCESSWRITE ||
5664 (np->n_flag & NWRITEOPENED) != 0)
5665 iolaymode = NFSLAYOUTIOMODE_RW;
5667 iolaymode = NFSLAYOUTIOMODE_READ;
5668 error = nfsrpc_getlayout(nmp, vp, np->n_fhp, iolaymode,
5669 NULL, &stateid, off, &layp, newcred, p);
5672 np->n_flag |= NNOLAYOUT;
5675 nfscl_lockderef(lckp);
5676 NFSFREECRED(newcred);
5678 nfscl_rellayout(layp, 0);
5685 * Loop around finding a layout that works for the first part of
5686 * this I/O operation, and then call the function that actually
5690 len = (uint64_t)uiop->uio_resid;
5691 while (len > 0 && error == 0 && eof == 0) {
5692 off = uiop->uio_offset;
5693 error = nfscl_findlayoutforio(layp, off, rwaccess, &rflp);
5695 oresid = xfer = (uint64_t)uiop->uio_resid;
5696 if (xfer > (rflp->nfsfl_end - rflp->nfsfl_off))
5697 xfer = rflp->nfsfl_end - rflp->nfsfl_off;
5699 * For Flex File layout with mirrored DSs, select one
5700 * of them at random for reads. For writes and commits,
5704 tdrpc = drpc = NULL;
5707 if ((layp->nfsly_flags & NFSLY_FLEXFILE) != 0 &&
5708 (mirrorcnt = rflp->nfsfl_mirrorcnt) > 1) {
5709 if (rwaccess == NFSV4OPEN_ACCESSREAD) {
5710 firstmirror = arc4random() % mirrorcnt;
5711 mirrorcnt = firstmirror + 1;
5713 if (docommit == 0) {
5715 * Save values, so uiop can be
5716 * rolled back upon a write
5719 offs = uiop->uio_offset;
5720 resid = uiop->uio_resid;
5722 uiop->uio_iov->iov_base;
5723 iovlen = uiop->uio_iov->iov_len;
5724 m = nfsm_uiombuflist(uiop, len,
5727 tdrpc = drpc = malloc(sizeof(*drpc) *
5728 (mirrorcnt - 1), M_TEMP, M_WAITOK |
5732 for (i = firstmirror; i < mirrorcnt && error == 0; i++){
5733 if ((layp->nfsly_flags & NFSLY_FLEXFILE) != 0) {
5734 dev = rflp->nfsfl_ffm[i].dev;
5735 dip = nfscl_getdevinfo(nmp->nm_clp, dev,
5736 rflp->nfsfl_ffm[i].devp);
5738 dev = rflp->nfsfl_dev;
5739 dip = nfscl_getdevinfo(nmp->nm_clp, dev,
5743 if ((rflp->nfsfl_flags & NFSFL_FLEXFILE)
5745 error = nfscl_dofflayoutio(vp,
5746 uiop, iomode, must_commit,
5747 &eof, &stateid, rwaccess,
5748 dip, layp, rflp, off, xfer,
5749 i, docommit, m, tdrpc,
5752 error = nfscl_doflayoutio(vp,
5753 uiop, iomode, must_commit,
5754 &eof, &stateid, rwaccess,
5755 dip, layp, rflp, off, xfer,
5756 docommit, newcred, p);
5757 nfscl_reldevinfo(dip);
5765 timo = hz / 50; /* Wait for 20msec. */
5768 for (i = firstmirror; i < mirrorcnt - 1 &&
5769 tdrpc != NULL; i++, tdrpc++) {
5771 * For the unused drpc entries, both inprog and
5772 * err == 0, so this loop won't break.
5774 while (tdrpc->inprog != 0 && tdrpc->done == 0)
5775 tsleep(&tdrpc->tsk, PVFS, "clrpcio",
5777 if (error == 0 && tdrpc->err != 0)
5782 if (mirrorcnt > 1 && rwaccess ==
5783 NFSV4OPEN_ACCESSWRITE && docommit == 0) {
5785 layp->nfsly_flags |= NFSLY_WRITTEN;
5788 lastbyte = off + xfer - 1;
5790 if (lastbyte > layp->nfsly_lastbyte)
5791 layp->nfsly_lastbyte = lastbyte;
5793 } else if (error == NFSERR_OPENMODE &&
5794 rwaccess == NFSV4OPEN_ACCESSREAD) {
5796 nmp->nm_state |= NFSSTA_OPENMODE;
5801 len -= (oresid - (uint64_t)uiop->uio_resid);
5802 else if (mirrorcnt > 1 && rwaccess ==
5803 NFSV4OPEN_ACCESSWRITE && docommit == 0) {
5805 * In case the rpc gets retried, roll the
5806 * uio fields changed by nfsm_uiombuflist()
5809 uiop->uio_offset = offs;
5810 uiop->uio_resid = resid;
5811 uiop->uio_iov->iov_base = iovbase;
5812 uiop->uio_iov->iov_len = iovlen;
5817 nfscl_lockderef(lckp);
5818 NFSFREECRED(newcred);
5819 nfscl_rellayout(layp, 0);
5825 * Make a copy of the mbuf chain and add an mbuf for null padding, as required.
5827 static struct mbuf *
5828 nfsm_copym(struct mbuf *m, int off, int xfer)
5830 struct mbuf *m2, *m3, *m4;
5834 m2 = m_copym(m, off, xfer, M_WAITOK);
5835 rem = NFSM_RNDUP(xfer) - xfer;
5838 * The zero padding to a multiple of 4 bytes is required by
5839 * the XDR. So that the mbufs copied by reference aren't
5840 * modified, add an mbuf with the zero'd bytes to the list.
5841 * rem will be a maximum of 3, so one zero'd uint32_t is
5845 while (m3->m_next != NULL)
5848 tl = NFSMTOD(m4, uint32_t *);
5850 mbuf_setlen(m4, rem);
5851 mbuf_setnext(m3, m4);
5857 * Find a file layout that will handle the first bytes of the requested
5858 * range and return the information from it needed to the I/O operation.
5861 nfscl_findlayoutforio(struct nfscllayout *lyp, uint64_t off, uint32_t rwaccess,
5862 struct nfsclflayout **retflpp)
5864 struct nfsclflayout *flp, *nflp, *rflp;
5869 /* For reading, do the Read list first and then the Write list. */
5871 if (rw == NFSV4OPEN_ACCESSREAD)
5872 flp = LIST_FIRST(&lyp->nfsly_flayread);
5874 flp = LIST_FIRST(&lyp->nfsly_flayrw);
5875 while (flp != NULL) {
5876 nflp = LIST_NEXT(flp, nfsfl_list);
5877 if (flp->nfsfl_off > off)
5879 if (flp->nfsfl_end > off &&
5880 (rflp == NULL || rflp->nfsfl_end < flp->nfsfl_end))
5884 if (rw == NFSV4OPEN_ACCESSREAD)
5885 rw = NFSV4OPEN_ACCESSWRITE;
5890 /* This one covers the most bytes starting at off. */
5898 * Do I/O using an NFSv4.1 file layout.
5901 nfscl_doflayoutio(vnode_t vp, struct uio *uiop, int *iomode, int *must_commit,
5902 int *eofp, nfsv4stateid_t *stateidp, int rwflag, struct nfscldevinfo *dp,
5903 struct nfscllayout *lyp, struct nfsclflayout *flp, uint64_t off,
5904 uint64_t len, int docommit, struct ucred *cred, NFSPROC_T *p)
5906 uint64_t io_off, rel_off, stripe_unit_size, transfer, xfer;
5907 int commit_thru_mds, error, stripe_index, stripe_pos;
5910 struct nfsclds **dspp;
5913 rel_off = off - flp->nfsfl_patoff;
5914 stripe_unit_size = flp->nfsfl_util & NFSFLAYUTIL_STRIPE_MASK;
5915 stripe_pos = (rel_off / stripe_unit_size + flp->nfsfl_stripe1) %
5916 dp->nfsdi_stripecnt;
5917 transfer = stripe_unit_size - (rel_off % stripe_unit_size);
5920 /* Loop around, doing I/O for each stripe unit. */
5921 while (len > 0 && error == 0) {
5922 stripe_index = nfsfldi_stripeindex(dp, stripe_pos);
5923 dspp = nfsfldi_addr(dp, stripe_index);
5924 if (len > transfer && docommit == 0)
5928 if ((flp->nfsfl_util & NFSFLAYUTIL_DENSE) != 0) {
5930 if (stripe_pos >= flp->nfsfl_fhcnt)
5932 fhp = flp->nfsfl_fh[stripe_pos];
5933 io_off = (rel_off / (stripe_unit_size *
5934 dp->nfsdi_stripecnt)) * stripe_unit_size +
5935 rel_off % stripe_unit_size;
5937 /* Sparse layout. */
5938 if (flp->nfsfl_fhcnt > 1) {
5939 if (stripe_index >= flp->nfsfl_fhcnt)
5941 fhp = flp->nfsfl_fh[stripe_index];
5942 } else if (flp->nfsfl_fhcnt == 1)
5943 fhp = flp->nfsfl_fh[0];
5948 if ((flp->nfsfl_util & NFSFLAYUTIL_COMMIT_THRU_MDS) != 0) {
5949 commit_thru_mds = 1;
5953 commit_thru_mds = 0;
5955 np->n_flag |= NDSCOMMIT;
5958 if (docommit != 0) {
5960 error = nfsrpc_commitds(vp, io_off, xfer,
5961 *dspp, fhp, 0, 0, cred, p);
5964 * Set both eof and uio_resid = 0 to end any
5968 uiop->uio_resid = 0;
5971 np->n_flag &= ~NDSCOMMIT;
5974 } else if (rwflag == NFSV4OPEN_ACCESSREAD)
5975 error = nfsrpc_readds(vp, uiop, stateidp, eofp, *dspp,
5976 io_off, xfer, fhp, 0, 0, 0, cred, p);
5978 error = nfsrpc_writeds(vp, uiop, iomode, must_commit,
5979 stateidp, *dspp, io_off, xfer, fhp, commit_thru_mds,
5983 lyp->nfsly_flags |= NFSLY_WRITTEN;
5988 transfer = stripe_unit_size;
5989 stripe_pos = (stripe_pos + 1) % dp->nfsdi_stripecnt;
5998 * Do I/O using an NFSv4.1 flex file layout.
6001 nfscl_dofflayoutio(vnode_t vp, struct uio *uiop, int *iomode, int *must_commit,
6002 int *eofp, nfsv4stateid_t *stateidp, int rwflag, struct nfscldevinfo *dp,
6003 struct nfscllayout *lyp, struct nfsclflayout *flp, uint64_t off,
6004 uint64_t len, int mirror, int docommit, struct mbuf *mp,
6005 struct nfsclwritedsdorpc *drpc, struct ucred *cred, NFSPROC_T *p)
6007 uint64_t transfer, xfer;
6011 struct nfsclds **dspp;
6012 struct ucred *tcred;
6018 NFSCL_DEBUG(4, "nfscl_dofflayoutio: off=%ju len=%ju\n", (uintmax_t)off,
6020 /* Loop around, doing I/O for each stripe unit. */
6021 while (len > 0 && error == 0) {
6022 dspp = nfsfldi_addr(dp, 0);
6023 fhp = flp->nfsfl_ffm[mirror].fh[dp->nfsdi_versindex];
6024 stateidp = &flp->nfsfl_ffm[mirror].st;
6025 NFSCL_DEBUG(4, "mirror=%d vind=%d fhlen=%d st.seqid=0x%x\n",
6026 mirror, dp->nfsdi_versindex, fhp->nfh_len, stateidp->seqid);
6027 if ((dp->nfsdi_flags & NFSDI_TIGHTCOUPLED) == 0) {
6028 tcred = NFSNEWCRED(cred);
6029 tcred->cr_uid = flp->nfsfl_ffm[mirror].user;
6030 tcred->cr_groups[0] = flp->nfsfl_ffm[mirror].group;
6031 tcred->cr_ngroups = 1;
6034 if (rwflag == NFSV4OPEN_ACCESSREAD)
6035 transfer = dp->nfsdi_rsize;
6037 transfer = dp->nfsdi_wsize;
6039 np->n_flag |= NDSCOMMIT;
6041 if (len > transfer && docommit == 0)
6045 if (docommit != 0) {
6048 * Do last mirrored DS commit with this thread.
6050 if (mirror < flp->nfsfl_mirrorcnt - 1)
6051 error = nfsio_commitds(vp, off, xfer,
6052 *dspp, fhp, dp->nfsdi_vers,
6053 dp->nfsdi_minorvers, drpc, tcred,
6056 error = nfsrpc_commitds(vp, off, xfer,
6057 *dspp, fhp, dp->nfsdi_vers,
6058 dp->nfsdi_minorvers, tcred, p);
6059 NFSCL_DEBUG(4, "commitds=%d\n", error);
6060 if (error != 0 && error != EACCES && error !=
6063 "DS layreterr for commit\n");
6064 nfscl_dserr(NFSV4OP_COMMIT, error, dp,
6068 NFSCL_DEBUG(4, "aft nfsio_commitds=%d\n", error);
6071 * Set both eof and uio_resid = 0 to end any
6075 uiop->uio_resid = 0;
6078 np->n_flag &= ~NDSCOMMIT;
6081 } else if (rwflag == NFSV4OPEN_ACCESSREAD) {
6082 error = nfsrpc_readds(vp, uiop, stateidp, eofp, *dspp,
6083 off, xfer, fhp, 1, dp->nfsdi_vers,
6084 dp->nfsdi_minorvers, tcred, p);
6085 NFSCL_DEBUG(4, "readds=%d\n", error);
6086 if (error != 0 && error != EACCES && error != ESTALE) {
6087 NFSCL_DEBUG(4, "DS layreterr for read\n");
6088 nfscl_dserr(NFSV4OP_READ, error, dp, lyp,
6092 if (flp->nfsfl_mirrorcnt == 1) {
6093 error = nfsrpc_writeds(vp, uiop, iomode,
6094 must_commit, stateidp, *dspp, off, xfer,
6095 fhp, 0, 1, dp->nfsdi_vers,
6096 dp->nfsdi_minorvers, tcred, p);
6099 lyp->nfsly_flags |= NFSLY_WRITTEN;
6103 m = nfsm_copym(mp, rel_off, xfer);
6104 NFSCL_DEBUG(4, "mcopy reloff=%d xfer=%jd\n",
6105 rel_off, (uintmax_t)xfer);
6107 * Do the writes after the first loop iteration
6108 * and the write for the last mirror via this
6110 * This loop only iterates for small values
6111 * of nfsdi_wsize, which may never occur in
6112 * practice. However, the drpc is completely
6113 * used by the first iteration and, as such,
6114 * cannot be used after that.
6116 if (mirror < flp->nfsfl_mirrorcnt - 1 &&
6118 error = nfsio_writedsmir(vp, iomode,
6119 must_commit, stateidp, *dspp, off,
6120 xfer, fhp, m, dp->nfsdi_vers,
6121 dp->nfsdi_minorvers, drpc, tcred,
6124 error = nfsrpc_writedsmir(vp, iomode,
6125 must_commit, stateidp, *dspp, off,
6126 xfer, fhp, m, dp->nfsdi_vers,
6127 dp->nfsdi_minorvers, tcred, p);
6128 NFSCL_DEBUG(4, "nfsio_writedsmir=%d\n", error);
6129 if (error != 0 && error != EACCES && error !=
6132 "DS layreterr for write\n");
6133 nfscl_dserr(NFSV4OP_WRITE, error, dp,
6138 NFSCL_DEBUG(4, "aft read/writeds=%d\n", error);
6144 if ((dp->nfsdi_flags & NFSDI_TIGHTCOUPLED) == 0)
6147 NFSCL_DEBUG(4, "eo nfscl_dofflayoutio=%d\n", error);
6152 * The actual read RPC done to a DS.
6155 nfsrpc_readds(vnode_t vp, struct uio *uiop, nfsv4stateid_t *stateidp, int *eofp,
6156 struct nfsclds *dsp, uint64_t io_off, int len, struct nfsfh *fhp, int flex,
6157 int vers, int minorvers, struct ucred *cred, NFSPROC_T *p)
6160 int attrflag, error, retlen;
6161 struct nfsrv_descript nfsd;
6162 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
6163 struct nfsrv_descript *nd = &nfsd;
6164 struct nfssockreq *nrp;
6168 if (vers == 0 || vers == NFS_VER4) {
6169 nfscl_reqstart(nd, NFSPROC_READDS, nmp, fhp->nfh_fh,
6170 fhp->nfh_len, NULL, &dsp->nfsclds_sess, vers, minorvers);
6172 NFSCL_DEBUG(4, "nfsrpc_readds: vers4 minvers=%d\n", minorvers);
6174 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID);
6176 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSEQIDZERO);
6178 nfscl_reqstart(nd, NFSPROC_READ, nmp, fhp->nfh_fh,
6179 fhp->nfh_len, NULL, &dsp->nfsclds_sess, vers, minorvers);
6180 NFSCL_DEBUG(4, "nfsrpc_readds: vers3\n");
6182 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED * 3);
6183 txdr_hyper(io_off, tl);
6184 *(tl + 2) = txdr_unsigned(len);
6185 nrp = dsp->nfsclds_sockp;
6186 NFSCL_DEBUG(4, "nfsrpc_readds: nrp=%p\n", nrp);
6188 /* If NULL, use the MDS socket. */
6189 nrp = &nmp->nm_sockreq;
6190 error = newnfs_request(nd, nmp, NULL, nrp, vp, p, cred,
6191 NFS_PROG, vers, NULL, 1, NULL, &dsp->nfsclds_sess);
6192 NFSCL_DEBUG(4, "nfsrpc_readds: stat=%d err=%d\n", nd->nd_repstat,
6196 if (vers == NFS_VER3) {
6197 error = nfscl_postop_attr(nd, &na, &attrflag, NULL);
6198 NFSCL_DEBUG(4, "nfsrpc_readds: postop=%d\n", error);
6202 if (nd->nd_repstat != 0) {
6203 error = nd->nd_repstat;
6206 if (vers == NFS_VER3) {
6207 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
6208 *eofp = fxdr_unsigned(int, *(tl + 1));
6210 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
6211 *eofp = fxdr_unsigned(int, *tl);
6213 NFSM_STRSIZ(retlen, len);
6214 NFSCL_DEBUG(4, "nfsrpc_readds: retlen=%d eof=%d\n", retlen, *eofp);
6215 error = nfsm_mbufuio(nd, uiop, retlen);
6217 if (nd->nd_mrep != NULL)
6218 mbuf_freem(nd->nd_mrep);
6223 * The actual write RPC done to a DS.
6226 nfsrpc_writeds(vnode_t vp, struct uio *uiop, int *iomode, int *must_commit,
6227 nfsv4stateid_t *stateidp, struct nfsclds *dsp, uint64_t io_off, int len,
6228 struct nfsfh *fhp, int commit_thru_mds, int flex, int vers, int minorvers,
6229 struct ucred *cred, NFSPROC_T *p)
6232 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
6233 int attrflag, error, rlen, commit, committed = NFSWRITE_FILESYNC;
6235 struct nfsrv_descript nfsd;
6236 struct nfsrv_descript *nd = &nfsd;
6237 struct nfssockreq *nrp;
6240 KASSERT(uiop->uio_iovcnt == 1, ("nfs: writerpc iovcnt > 1"));
6242 if (vers == 0 || vers == NFS_VER4) {
6243 nfscl_reqstart(nd, NFSPROC_WRITEDS, nmp, fhp->nfh_fh,
6244 fhp->nfh_len, NULL, &dsp->nfsclds_sess, vers, minorvers);
6245 NFSCL_DEBUG(4, "nfsrpc_writeds: vers4 minvers=%d\n", minorvers);
6248 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID);
6250 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSEQIDZERO);
6251 NFSM_BUILD(tl, uint32_t *, NFSX_HYPER + 2 * NFSX_UNSIGNED);
6253 nfscl_reqstart(nd, NFSPROC_WRITE, nmp, fhp->nfh_fh,
6254 fhp->nfh_len, NULL, &dsp->nfsclds_sess, vers, minorvers);
6255 NFSCL_DEBUG(4, "nfsrpc_writeds: vers3\n");
6256 NFSM_BUILD(tl, uint32_t *, NFSX_HYPER + 3 * NFSX_UNSIGNED);
6258 txdr_hyper(io_off, tl);
6260 if (vers == NFS_VER3)
6261 *tl++ = txdr_unsigned(len);
6262 *tl++ = txdr_unsigned(*iomode);
6263 *tl = txdr_unsigned(len);
6264 nfsm_uiombuf(nd, uiop, len);
6265 nrp = dsp->nfsclds_sockp;
6267 /* If NULL, use the MDS socket. */
6268 nrp = &nmp->nm_sockreq;
6269 error = newnfs_request(nd, nmp, NULL, nrp, vp, p, cred,
6270 NFS_PROG, vers, NULL, 1, NULL, &dsp->nfsclds_sess);
6271 NFSCL_DEBUG(4, "nfsrpc_writeds: err=%d stat=%d\n", error,
6275 if (nd->nd_repstat != 0) {
6277 * In case the rpc gets retried, roll
6278 * the uio fileds changed by nfsm_uiombuf()
6281 uiop->uio_offset -= len;
6282 uio_uio_resid_add(uiop, len);
6283 uio_iov_base_add(uiop, -len);
6284 uio_iov_len_add(uiop, len);
6285 error = nd->nd_repstat;
6287 if (vers == NFS_VER3) {
6288 error = nfscl_wcc_data(nd, vp, &na, &attrflag, NULL,
6290 NFSCL_DEBUG(4, "nfsrpc_writeds: wcc_data=%d\n", error);
6294 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED + NFSX_VERF);
6295 rlen = fxdr_unsigned(int, *tl++);
6296 NFSCL_DEBUG(4, "nfsrpc_writeds: len=%d rlen=%d\n", len, rlen);
6300 } else if (rlen < len) {
6301 backup = len - rlen;
6302 uio_iov_base_add(uiop, -(backup));
6303 uio_iov_len_add(uiop, backup);
6304 uiop->uio_offset -= backup;
6305 uio_uio_resid_add(uiop, backup);
6308 commit = fxdr_unsigned(int, *tl++);
6311 * Return the lowest commitment level
6312 * obtained by any of the RPCs.
6314 if (committed == NFSWRITE_FILESYNC)
6316 else if (committed == NFSWRITE_DATASYNC &&
6317 commit == NFSWRITE_UNSTABLE)
6319 if (commit_thru_mds != 0) {
6321 if (!NFSHASWRITEVERF(nmp)) {
6322 NFSBCOPY(tl, nmp->nm_verf, NFSX_VERF);
6323 NFSSETWRITEVERF(nmp);
6324 } else if (NFSBCMP(tl, nmp->nm_verf, NFSX_VERF)) {
6326 NFSBCOPY(tl, nmp->nm_verf, NFSX_VERF);
6331 if ((dsp->nfsclds_flags & NFSCLDS_HASWRITEVERF) == 0) {
6332 NFSBCOPY(tl, dsp->nfsclds_verf, NFSX_VERF);
6333 dsp->nfsclds_flags |= NFSCLDS_HASWRITEVERF;
6334 } else if (NFSBCMP(tl, dsp->nfsclds_verf, NFSX_VERF)) {
6336 NFSBCOPY(tl, dsp->nfsclds_verf, NFSX_VERF);
6342 if (nd->nd_mrep != NULL)
6343 mbuf_freem(nd->nd_mrep);
6344 *iomode = committed;
6345 if (nd->nd_repstat != 0 && error == 0)
6346 error = nd->nd_repstat;
6351 * The actual write RPC done to a DS.
6352 * This variant is called from a separate kernel process for mirrors.
6353 * Any short write is considered an IO error.
6356 nfsrpc_writedsmir(vnode_t vp, int *iomode, int *must_commit,
6357 nfsv4stateid_t *stateidp, struct nfsclds *dsp, uint64_t io_off, int len,
6358 struct nfsfh *fhp, struct mbuf *m, int vers, int minorvers,
6359 struct ucred *cred, NFSPROC_T *p)
6362 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
6363 int attrflag, error, commit, committed = NFSWRITE_FILESYNC, rlen;
6364 struct nfsrv_descript nfsd;
6365 struct nfsrv_descript *nd = &nfsd;
6366 struct nfssockreq *nrp;
6370 if (vers == 0 || vers == NFS_VER4) {
6371 nfscl_reqstart(nd, NFSPROC_WRITEDS, nmp, fhp->nfh_fh,
6372 fhp->nfh_len, NULL, &dsp->nfsclds_sess, vers, minorvers);
6374 NFSCL_DEBUG(4, "nfsrpc_writedsmir: vers4 minvers=%d\n",
6376 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID);
6377 NFSM_BUILD(tl, uint32_t *, NFSX_HYPER + 2 * NFSX_UNSIGNED);
6379 nfscl_reqstart(nd, NFSPROC_WRITE, nmp, fhp->nfh_fh,
6380 fhp->nfh_len, NULL, &dsp->nfsclds_sess, vers, minorvers);
6381 NFSCL_DEBUG(4, "nfsrpc_writedsmir: vers3\n");
6382 NFSM_BUILD(tl, uint32_t *, NFSX_HYPER + 3 * NFSX_UNSIGNED);
6384 txdr_hyper(io_off, tl);
6386 if (vers == NFS_VER3)
6387 *tl++ = txdr_unsigned(len);
6388 *tl++ = txdr_unsigned(*iomode);
6389 *tl = txdr_unsigned(len);
6391 /* Put data in mbuf chain. */
6392 nd->nd_mb->m_next = m;
6393 /* Set nd_mb and nd_bpos to end of data. */
6394 while (m->m_next != NULL)
6397 nd->nd_bpos = mtod(m, char *) + m->m_len;
6398 NFSCL_DEBUG(4, "nfsrpc_writedsmir: lastmb len=%d\n", m->m_len);
6400 nrp = dsp->nfsclds_sockp;
6402 /* If NULL, use the MDS socket. */
6403 nrp = &nmp->nm_sockreq;
6404 error = newnfs_request(nd, nmp, NULL, nrp, vp, p, cred,
6405 NFS_PROG, vers, NULL, 1, NULL, &dsp->nfsclds_sess);
6406 NFSCL_DEBUG(4, "nfsrpc_writedsmir: err=%d stat=%d\n", error,
6410 if (nd->nd_repstat != 0)
6411 error = nd->nd_repstat;
6413 if (vers == NFS_VER3) {
6414 error = nfscl_wcc_data(nd, vp, &na, &attrflag, NULL,
6416 NFSCL_DEBUG(4, "nfsrpc_writedsmir: wcc_data=%d\n",
6421 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED + NFSX_VERF);
6422 rlen = fxdr_unsigned(int, *tl++);
6423 NFSCL_DEBUG(4, "nfsrpc_writedsmir: len=%d rlen=%d\n", len,
6427 NFSCL_DEBUG(4, "nfsrpc_writedsmir: len=%d rlen=%d\n",
6431 commit = fxdr_unsigned(int, *tl++);
6434 * Return the lowest commitment level
6435 * obtained by any of the RPCs.
6437 if (committed == NFSWRITE_FILESYNC)
6439 else if (committed == NFSWRITE_DATASYNC &&
6440 commit == NFSWRITE_UNSTABLE)
6443 if ((dsp->nfsclds_flags & NFSCLDS_HASWRITEVERF) == 0) {
6444 NFSBCOPY(tl, dsp->nfsclds_verf, NFSX_VERF);
6445 dsp->nfsclds_flags |= NFSCLDS_HASWRITEVERF;
6446 } else if (NFSBCMP(tl, dsp->nfsclds_verf, NFSX_VERF)) {
6448 NFSBCOPY(tl, dsp->nfsclds_verf, NFSX_VERF);
6453 if (nd->nd_mrep != NULL)
6454 mbuf_freem(nd->nd_mrep);
6455 *iomode = committed;
6456 if (nd->nd_repstat != 0 && error == 0)
6457 error = nd->nd_repstat;
6462 * Start up the thread that will execute nfsrpc_writedsmir().
6465 start_writedsmir(void *arg, int pending)
6467 struct nfsclwritedsdorpc *drpc;
6469 drpc = (struct nfsclwritedsdorpc *)arg;
6470 drpc->err = nfsrpc_writedsmir(drpc->vp, &drpc->iomode,
6471 &drpc->must_commit, drpc->stateidp, drpc->dsp, drpc->off, drpc->len,
6472 drpc->fhp, drpc->m, drpc->vers, drpc->minorvers, drpc->cred,
6475 NFSCL_DEBUG(4, "start_writedsmir: err=%d\n", drpc->err);
6479 * Set up the write DS mirror call for the pNFS I/O thread.
6482 nfsio_writedsmir(vnode_t vp, int *iomode, int *must_commit,
6483 nfsv4stateid_t *stateidp, struct nfsclds *dsp, uint64_t off, int len,
6484 struct nfsfh *fhp, struct mbuf *m, int vers, int minorvers,
6485 struct nfsclwritedsdorpc *drpc, struct ucred *cred, NFSPROC_T *p)
6492 drpc->iomode = *iomode;
6493 drpc->must_commit = *must_commit;
6494 drpc->stateidp = stateidp;
6501 drpc->minorvers = minorvers;
6506 if (nfs_pnfsiothreads != 0) {
6507 ret = nfs_pnfsio(start_writedsmir, drpc);
6508 NFSCL_DEBUG(4, "nfsio_writedsmir: nfs_pnfsio=%d\n", ret);
6511 error = nfsrpc_writedsmir(vp, iomode, must_commit, stateidp,
6512 dsp, off, len, fhp, m, vers, minorvers, cred, p);
6513 NFSCL_DEBUG(4, "nfsio_writedsmir: error=%d\n", error);
6518 * Free up the nfsclds structure.
6521 nfscl_freenfsclds(struct nfsclds *dsp)
6527 if (dsp->nfsclds_sockp != NULL) {
6528 NFSFREECRED(dsp->nfsclds_sockp->nr_cred);
6529 NFSFREEMUTEX(&dsp->nfsclds_sockp->nr_mtx);
6530 free(dsp->nfsclds_sockp->nr_nam, M_SONAME);
6531 free(dsp->nfsclds_sockp, M_NFSSOCKREQ);
6533 NFSFREEMUTEX(&dsp->nfsclds_mtx);
6534 NFSFREEMUTEX(&dsp->nfsclds_sess.nfsess_mtx);
6535 for (i = 0; i < NFSV4_CBSLOTS; i++) {
6536 if (dsp->nfsclds_sess.nfsess_cbslots[i].nfssl_reply != NULL)
6538 dsp->nfsclds_sess.nfsess_cbslots[i].nfssl_reply);
6540 free(dsp, M_NFSCLDS);
6543 static enum nfsclds_state
6544 nfscl_getsameserver(struct nfsmount *nmp, struct nfsclds *newdsp,
6545 struct nfsclds **retdspp, uint32_t *sequencep)
6547 struct nfsclds *dsp;
6551 * Search the list of nfsclds structures for one with the same
6555 TAILQ_FOREACH(dsp, &nmp->nm_sess, nfsclds_list) {
6556 if (dsp->nfsclds_servownlen == newdsp->nfsclds_servownlen &&
6557 dsp->nfsclds_servownlen != 0 &&
6558 !NFSBCMP(dsp->nfsclds_serverown, newdsp->nfsclds_serverown,
6559 dsp->nfsclds_servownlen) &&
6560 dsp->nfsclds_sess.nfsess_defunct == 0) {
6561 NFSCL_DEBUG(4, "fnd same fdsp=%p dsp=%p flg=0x%x\n",
6562 TAILQ_FIRST(&nmp->nm_sess), dsp,
6563 dsp->nfsclds_flags);
6565 /* Get sequenceid# from first entry. */
6567 dsp->nfsclds_sess.nfsess_sequenceid;
6570 /* Server major id matches. */
6571 if ((dsp->nfsclds_flags & NFSCLDS_DS) != 0) {
6573 return (NFSDSP_USETHISSESSION);
6579 return (NFSDSP_SEQTHISSESSION);
6580 return (NFSDSP_NOTFOUND);
6584 * NFS commit rpc to a NFSv4.1 DS.
6587 nfsrpc_commitds(vnode_t vp, uint64_t offset, int cnt, struct nfsclds *dsp,
6588 struct nfsfh *fhp, int vers, int minorvers, struct ucred *cred,
6592 struct nfsrv_descript nfsd, *nd = &nfsd;
6593 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
6594 struct nfssockreq *nrp;
6596 int attrflag, error;
6599 if (vers == 0 || vers == NFS_VER4) {
6600 nfscl_reqstart(nd, NFSPROC_COMMITDS, nmp, fhp->nfh_fh,
6601 fhp->nfh_len, NULL, &dsp->nfsclds_sess, vers, minorvers);
6604 nfscl_reqstart(nd, NFSPROC_COMMIT, nmp, fhp->nfh_fh,
6605 fhp->nfh_len, NULL, &dsp->nfsclds_sess, vers, minorvers);
6606 NFSCL_DEBUG(4, "nfsrpc_commitds: vers=%d minvers=%d\n", vers,
6608 NFSM_BUILD(tl, uint32_t *, NFSX_HYPER + NFSX_UNSIGNED);
6609 txdr_hyper(offset, tl);
6611 *tl = txdr_unsigned(cnt);
6612 nrp = dsp->nfsclds_sockp;
6614 /* If NULL, use the MDS socket. */
6615 nrp = &nmp->nm_sockreq;
6616 error = newnfs_request(nd, nmp, NULL, nrp, vp, p, cred,
6617 NFS_PROG, vers, NULL, 1, NULL, &dsp->nfsclds_sess);
6618 NFSCL_DEBUG(4, "nfsrpc_commitds: err=%d stat=%d\n", error,
6622 if (nd->nd_repstat == 0) {
6623 if (vers == NFS_VER3) {
6624 error = nfscl_wcc_data(nd, vp, &na, &attrflag, NULL,
6626 NFSCL_DEBUG(4, "nfsrpc_commitds: wccdata=%d\n", error);
6630 NFSM_DISSECT(tl, u_int32_t *, NFSX_VERF);
6632 if (NFSBCMP(tl, dsp->nfsclds_verf, NFSX_VERF)) {
6633 NFSBCOPY(tl, dsp->nfsclds_verf, NFSX_VERF);
6634 error = NFSERR_STALEWRITEVERF;
6639 if (error == 0 && nd->nd_repstat != 0)
6640 error = nd->nd_repstat;
6641 mbuf_freem(nd->nd_mrep);
6646 * Start up the thread that will execute nfsrpc_commitds().
6649 start_commitds(void *arg, int pending)
6651 struct nfsclwritedsdorpc *drpc;
6653 drpc = (struct nfsclwritedsdorpc *)arg;
6654 drpc->err = nfsrpc_commitds(drpc->vp, drpc->off, drpc->len,
6655 drpc->dsp, drpc->fhp, drpc->vers, drpc->minorvers, drpc->cred,
6658 NFSCL_DEBUG(4, "start_commitds: err=%d\n", drpc->err);
6662 * Set up the commit DS mirror call for the pNFS I/O thread.
6665 nfsio_commitds(vnode_t vp, uint64_t offset, int cnt, struct nfsclds *dsp,
6666 struct nfsfh *fhp, int vers, int minorvers,
6667 struct nfsclwritedsdorpc *drpc, struct ucred *cred, NFSPROC_T *p)
6679 drpc->minorvers = minorvers;
6684 if (nfs_pnfsiothreads != 0) {
6685 ret = nfs_pnfsio(start_commitds, drpc);
6686 NFSCL_DEBUG(4, "nfsio_commitds: nfs_pnfsio=%d\n", ret);
6689 error = nfsrpc_commitds(vp, offset, cnt, dsp, fhp, vers,
6690 minorvers, cred, p);
6691 NFSCL_DEBUG(4, "nfsio_commitds: error=%d\n", error);
6696 * Set up the XDR arguments for the LayoutGet operation.
6699 nfsrv_setuplayoutget(struct nfsrv_descript *nd, int iomode, uint64_t offset,
6700 uint64_t len, uint64_t minlen, nfsv4stateid_t *stateidp, int layouttype,
6701 int layoutlen, int usecurstateid)
6705 NFSM_BUILD(tl, uint32_t *, 4 * NFSX_UNSIGNED + 3 * NFSX_HYPER +
6707 *tl++ = newnfs_false; /* Don't signal availability. */
6708 *tl++ = txdr_unsigned(layouttype);
6709 *tl++ = txdr_unsigned(iomode);
6710 txdr_hyper(offset, tl);
6712 txdr_hyper(len, tl);
6714 txdr_hyper(minlen, tl);
6716 if (usecurstateid != 0) {
6717 /* Special stateid for Current stateid. */
6718 *tl++ = txdr_unsigned(1);
6723 *tl++ = txdr_unsigned(stateidp->seqid);
6724 NFSCL_DEBUG(4, "layget seq=%d\n", (int)stateidp->seqid);
6725 *tl++ = stateidp->other[0];
6726 *tl++ = stateidp->other[1];
6727 *tl++ = stateidp->other[2];
6729 *tl = txdr_unsigned(layoutlen);
6733 * Parse the reply for a successful LayoutGet operation.
6736 nfsrv_parselayoutget(struct nfsrv_descript *nd, nfsv4stateid_t *stateidp,
6737 int *retonclosep, struct nfsclflayouthead *flhp)
6740 struct nfsclflayout *flp, *prevflp, *tflp;
6741 int cnt, error, fhcnt, gotiomode, i, iomode, j, k, l, laytype, nfhlen;
6743 uint64_t retlen, off;
6749 NFSCL_DEBUG(4, "in nfsrv_parselayoutget\n");
6753 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED + NFSX_STATEID);
6758 stateidp->seqid = fxdr_unsigned(uint32_t, *tl++);
6759 NFSCL_DEBUG(4, "retoncls=%d stseq=%d\n", *retonclosep,
6760 (int)stateidp->seqid);
6761 stateidp->other[0] = *tl++;
6762 stateidp->other[1] = *tl++;
6763 stateidp->other[2] = *tl++;
6764 cnt = fxdr_unsigned(int, *tl);
6765 NFSCL_DEBUG(4, "layg cnt=%d\n", cnt);
6766 if (cnt <= 0 || cnt > 10000) {
6767 /* Don't accept more than 10000 layouts in reply. */
6768 error = NFSERR_BADXDR;
6771 for (i = 0; i < cnt; i++) {
6772 /* Dissect to the layout type. */
6773 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_HYPER +
6775 off = fxdr_hyper(tl); tl += 2;
6776 retlen = fxdr_hyper(tl); tl += 2;
6777 iomode = fxdr_unsigned(int, *tl++);
6778 laytype = fxdr_unsigned(int, *tl);
6779 NFSCL_DEBUG(4, "layt=%d off=%ju len=%ju iom=%d\n", laytype,
6780 (uintmax_t)off, (uintmax_t)retlen, iomode);
6781 /* Ignore length of layout body for now. */
6782 if (laytype == NFSLAYOUT_NFSV4_1_FILES) {
6783 /* Parse the File layout up to fhcnt. */
6784 NFSM_DISSECT(tl, uint32_t *, 3 * NFSX_UNSIGNED +
6785 NFSX_HYPER + NFSX_V4DEVICEID);
6786 fhcnt = fxdr_unsigned(int, *(tl + 4 +
6787 NFSX_V4DEVICEID / NFSX_UNSIGNED));
6788 NFSCL_DEBUG(4, "fhcnt=%d\n", fhcnt);
6789 if (fhcnt < 0 || fhcnt > 100) {
6790 /* Don't accept more than 100 file handles. */
6791 error = NFSERR_BADXDR;
6795 flp = malloc(sizeof(*flp) + fhcnt *
6796 sizeof(struct nfsfh *), M_NFSFLAYOUT,
6799 flp = malloc(sizeof(*flp), M_NFSFLAYOUT,
6801 flp->nfsfl_flags = NFSFL_FILE;
6802 flp->nfsfl_fhcnt = 0;
6803 flp->nfsfl_devp = NULL;
6804 flp->nfsfl_off = off;
6805 if (flp->nfsfl_off + retlen < flp->nfsfl_off)
6806 flp->nfsfl_end = UINT64_MAX - flp->nfsfl_off;
6808 flp->nfsfl_end = flp->nfsfl_off + retlen;
6809 flp->nfsfl_iomode = iomode;
6810 if (gotiomode == -1)
6811 gotiomode = flp->nfsfl_iomode;
6812 /* Ignore layout body length for now. */
6813 NFSBCOPY(tl, flp->nfsfl_dev, NFSX_V4DEVICEID);
6814 tl += (NFSX_V4DEVICEID / NFSX_UNSIGNED);
6815 flp->nfsfl_util = fxdr_unsigned(uint32_t, *tl++);
6816 NFSCL_DEBUG(4, "flutil=0x%x\n", flp->nfsfl_util);
6817 flp->nfsfl_stripe1 = fxdr_unsigned(uint32_t, *tl++);
6818 flp->nfsfl_patoff = fxdr_hyper(tl); tl += 2;
6819 NFSCL_DEBUG(4, "stripe1=%u poff=%ju\n",
6820 flp->nfsfl_stripe1, (uintmax_t)flp->nfsfl_patoff);
6821 for (j = 0; j < fhcnt; j++) {
6822 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
6823 nfhlen = fxdr_unsigned(int, *tl);
6824 if (nfhlen <= 0 || nfhlen > NFSX_V4FHMAX) {
6825 error = NFSERR_BADXDR;
6828 nfhp = malloc(sizeof(*nfhp) + nfhlen - 1,
6830 flp->nfsfl_fh[j] = nfhp;
6832 nfhp->nfh_len = nfhlen;
6833 NFSM_DISSECT(cp, uint8_t *, NFSM_RNDUP(nfhlen));
6834 NFSBCOPY(cp, nfhp->nfh_fh, nfhlen);
6836 } else if (laytype == NFSLAYOUT_FLEXFILE) {
6837 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED +
6839 mirrorcnt = fxdr_unsigned(int, *(tl + 2));
6840 NFSCL_DEBUG(4, "mirrorcnt=%d\n", mirrorcnt);
6841 if (mirrorcnt < 1 || mirrorcnt > NFSDEV_MAXMIRRORS) {
6842 error = NFSERR_BADXDR;
6845 flp = malloc(sizeof(*flp) + mirrorcnt *
6846 sizeof(struct nfsffm), M_NFSFLAYOUT, M_WAITOK);
6847 flp->nfsfl_flags = NFSFL_FLEXFILE;
6848 flp->nfsfl_mirrorcnt = mirrorcnt;
6849 for (j = 0; j < mirrorcnt; j++)
6850 flp->nfsfl_ffm[j].devp = NULL;
6851 flp->nfsfl_off = off;
6852 if (flp->nfsfl_off + retlen < flp->nfsfl_off)
6853 flp->nfsfl_end = UINT64_MAX - flp->nfsfl_off;
6855 flp->nfsfl_end = flp->nfsfl_off + retlen;
6856 flp->nfsfl_iomode = iomode;
6857 if (gotiomode == -1)
6858 gotiomode = flp->nfsfl_iomode;
6859 flp->nfsfl_stripeunit = fxdr_hyper(tl);
6860 NFSCL_DEBUG(4, "stripeunit=%ju\n",
6861 (uintmax_t)flp->nfsfl_stripeunit);
6862 for (j = 0; j < mirrorcnt; j++) {
6863 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
6864 k = fxdr_unsigned(int, *tl);
6865 if (k < 1 || k > 128) {
6866 error = NFSERR_BADXDR;
6869 NFSCL_DEBUG(4, "servercnt=%d\n", k);
6870 for (l = 0; l < k; l++) {
6871 NFSM_DISSECT(tl, uint32_t *,
6872 NFSX_V4DEVICEID + NFSX_STATEID +
6875 /* Just use the first server. */
6877 flp->nfsfl_ffm[j].dev,
6879 tl += (NFSX_V4DEVICEID /
6882 flp->nfsfl_ffm[j].st.seqid =
6884 flp->nfsfl_ffm[j].st.other[0] =
6886 flp->nfsfl_ffm[j].st.other[1] =
6888 flp->nfsfl_ffm[j].st.other[2] =
6890 NFSCL_DEBUG(4, "st.seqid=%u "
6891 "st.o0=0x%x st.o1=0x%x "
6893 flp->nfsfl_ffm[j].st.seqid,
6894 flp->nfsfl_ffm[j].st.other[0],
6895 flp->nfsfl_ffm[j].st.other[1],
6896 flp->nfsfl_ffm[j].st.other[2]);
6898 tl += ((NFSX_V4DEVICEID +
6902 fhcnt = fxdr_unsigned(int, *tl);
6903 NFSCL_DEBUG(4, "fhcnt=%d\n", fhcnt);
6905 fhcnt > NFSDEV_MAXVERS) {
6906 error = NFSERR_BADXDR;
6909 for (m = 0; m < fhcnt; m++) {
6910 NFSM_DISSECT(tl, uint32_t *,
6912 nfhlen = fxdr_unsigned(int,
6914 NFSCL_DEBUG(4, "nfhlen=%d\n",
6916 if (nfhlen <= 0 || nfhlen >
6918 error = NFSERR_BADXDR;
6921 NFSM_DISSECT(cp, uint8_t *,
6922 NFSM_RNDUP(nfhlen));
6924 flp->nfsfl_ffm[j].fhcnt
6928 nfhlen - 1, M_NFSFH,
6930 flp->nfsfl_ffm[j].fh[m]
6932 nfhp->nfh_len = nfhlen;
6940 /* Now, get the ffsd_user/ffds_group. */
6941 error = nfsrv_parseug(nd, 0, &user,
6943 NFSCL_DEBUG(4, "after parseu=%d\n",
6946 error = nfsrv_parseug(nd, 1,
6947 &user, &grp, curthread);
6948 NFSCL_DEBUG(4, "aft parseg=%d\n",
6952 NFSCL_DEBUG(4, "user=%d group=%d\n",
6955 flp->nfsfl_ffm[j].user = user;
6956 flp->nfsfl_ffm[j].group = grp;
6958 "usr=%d grp=%d\n", user,
6963 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
6964 flp->nfsfl_fflags = fxdr_unsigned(uint32_t, *tl++);
6965 flp->nfsfl_statshint = fxdr_unsigned(uint32_t, *tl);
6966 NFSCL_DEBUG(4, "fflags=0x%x statshint=%d\n",
6967 flp->nfsfl_fflags, flp->nfsfl_statshint);
6969 error = NFSERR_BADXDR;
6972 if (flp->nfsfl_iomode == gotiomode) {
6973 /* Keep the list in increasing offset order. */
6974 tflp = LIST_FIRST(flhp);
6976 while (tflp != NULL &&
6977 tflp->nfsfl_off < flp->nfsfl_off) {
6979 tflp = LIST_NEXT(tflp, nfsfl_list);
6981 if (prevflp == NULL)
6982 LIST_INSERT_HEAD(flhp, flp, nfsfl_list);
6984 LIST_INSERT_AFTER(prevflp, flp,
6986 NFSCL_DEBUG(4, "flp inserted\n");
6988 printf("nfscl_layoutget(): got wrong iomode\n");
6989 nfscl_freeflayout(flp);
6994 NFSCL_DEBUG(4, "eo nfsrv_parselayoutget=%d\n", error);
6995 if (error != 0 && flp != NULL)
6996 nfscl_freeflayout(flp);
7001 * Parse a user/group digit string.
7004 nfsrv_parseug(struct nfsrv_descript *nd, int dogrp, uid_t *uidp, gid_t *gidp,
7008 char *cp, *str, str0[NFSV4_SMALLSTR + 1];
7012 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
7013 len = fxdr_unsigned(uint32_t, *tl);
7015 if (len > NFSV4_OPAQUELIMIT) {
7016 error = NFSERR_BADXDR;
7019 NFSCL_DEBUG(4, "nfsrv_parseug: len=%d\n", len);
7022 *gidp = GID_NOGROUP;
7027 if (len > NFSV4_SMALLSTR)
7028 str = malloc(len + 1, M_TEMP, M_WAITOK);
7031 NFSM_DISSECT(cp, char *, NFSM_RNDUP(len));
7032 NFSBCOPY(cp, str, len);
7034 NFSCL_DEBUG(4, "nfsrv_parseug: str=%s\n", str);
7036 error = nfsv4_strtogid(nd, str, len, gidp, p);
7038 error = nfsv4_strtouid(nd, str, len, uidp, p);
7040 if (len > NFSV4_SMALLSTR)
7042 NFSCL_DEBUG(4, "eo nfsrv_parseug=%d\n", error);
7047 * Similar to nfsrpc_getlayout(), except that it uses nfsrpc_openlayget(),
7048 * so that it does both an Open and a Layoutget.
7051 nfsrpc_getopenlayout(struct nfsmount *nmp, vnode_t vp, u_int8_t *nfhp,
7052 int fhlen, uint8_t *newfhp, int newfhlen, uint32_t mode,
7053 struct nfsclopen *op, uint8_t *name, int namelen, struct nfscldeleg **dpp,
7054 struct ucred *cred, NFSPROC_T *p)
7056 struct nfscllayout *lyp;
7057 struct nfsclflayout *flp;
7058 struct nfsclflayouthead flh;
7059 int error, islocked, layoutlen, recalled, retonclose, usecurstateid;
7060 int layouttype, laystat;
7061 nfsv4stateid_t stateid;
7062 struct nfsclsession *tsep;
7065 if (NFSHASFLEXFILE(nmp))
7066 layouttype = NFSLAYOUT_FLEXFILE;
7068 layouttype = NFSLAYOUT_NFSV4_1_FILES;
7070 * If lyp is returned non-NULL, there will be a refcnt (shared lock)
7071 * on it, iff flp != NULL or a lock (exclusive lock) on it iff
7074 lyp = nfscl_getlayout(nmp->nm_clp, newfhp, newfhlen, 0, &flp,
7076 NFSCL_DEBUG(4, "nfsrpc_getopenlayout nfscl_getlayout lyp=%p\n", lyp);
7079 else if (flp != NULL)
7083 if ((lyp == NULL || flp == NULL) && recalled == 0) {
7085 tsep = nfsmnt_mdssession(nmp);
7086 layoutlen = tsep->nfsess_maxcache - (NFSX_STATEID +
7092 stateid.seqid = lyp->nfsly_stateid.seqid;
7093 stateid.other[0] = lyp->nfsly_stateid.other[0];
7094 stateid.other[1] = lyp->nfsly_stateid.other[1];
7095 stateid.other[2] = lyp->nfsly_stateid.other[2];
7097 error = nfsrpc_openlayoutrpc(nmp, vp, nfhp, fhlen,
7098 newfhp, newfhlen, mode, op, name, namelen,
7099 dpp, &stateid, usecurstateid, layouttype, layoutlen,
7100 &retonclose, &flh, &laystat, cred, p);
7101 NFSCL_DEBUG(4, "aft nfsrpc_openlayoutrpc laystat=%d err=%d\n",
7103 laystat = nfsrpc_layoutgetres(nmp, vp, newfhp, newfhlen,
7104 &stateid, retonclose, NULL, &lyp, &flh, layouttype, laystat,
7105 &islocked, cred, p);
7107 error = nfsrpc_openrpc(nmp, vp, nfhp, fhlen, newfhp, newfhlen,
7108 mode, op, name, namelen, dpp, 0, 0, cred, p, 0, 0);
7110 nfscl_rellayout(lyp, 1);
7111 else if (islocked == 1)
7112 nfscl_rellayout(lyp, 0);
7117 * This function does an Open+LayoutGet for an NFSv4.1 mount with pNFS
7118 * enabled, only for the CLAIM_NULL case. All other NFSv4 Opens are
7119 * handled by nfsrpc_openrpc().
7120 * For the case where op == NULL, dvp is the directory. When op != NULL, it
7124 nfsrpc_openlayoutrpc(struct nfsmount *nmp, vnode_t vp, u_int8_t *nfhp,
7125 int fhlen, uint8_t *newfhp, int newfhlen, uint32_t mode,
7126 struct nfsclopen *op, uint8_t *name, int namelen, struct nfscldeleg **dpp,
7127 nfsv4stateid_t *stateidp, int usecurstateid, int layouttype,
7128 int layoutlen, int *retonclosep, struct nfsclflayouthead *flhp,
7129 int *laystatp, struct ucred *cred, NFSPROC_T *p)
7132 struct nfsrv_descript nfsd, *nd = &nfsd;
7133 struct nfscldeleg *ndp = NULL;
7134 struct nfsvattr nfsva;
7135 struct nfsclsession *tsep;
7136 uint32_t rflags, deleg;
7137 nfsattrbit_t attrbits;
7138 int error, ret, acesize, limitby, iomode;
7142 nfscl_reqstart(nd, NFSPROC_OPENLAYGET, nmp, nfhp, fhlen, NULL, NULL,
7144 NFSM_BUILD(tl, uint32_t *, 5 * NFSX_UNSIGNED);
7145 *tl++ = txdr_unsigned(op->nfso_own->nfsow_seqid);
7146 *tl++ = txdr_unsigned(mode & NFSV4OPEN_ACCESSBOTH);
7147 *tl++ = txdr_unsigned((mode >> NFSLCK_SHIFT) & NFSV4OPEN_DENYBOTH);
7148 tsep = nfsmnt_mdssession(nmp);
7149 *tl++ = tsep->nfsess_clientid.lval[0];
7150 *tl = tsep->nfsess_clientid.lval[1];
7151 nfsm_strtom(nd, op->nfso_own->nfsow_owner, NFSV4CL_LOCKNAMELEN);
7152 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_UNSIGNED);
7153 *tl++ = txdr_unsigned(NFSV4OPEN_NOCREATE);
7154 *tl = txdr_unsigned(NFSV4OPEN_CLAIMNULL);
7155 nfsm_strtom(nd, name, namelen);
7156 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
7157 *tl = txdr_unsigned(NFSV4OP_GETATTR);
7158 NFSZERO_ATTRBIT(&attrbits);
7159 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_CHANGE);
7160 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEMODIFY);
7161 nfsrv_putattrbit(nd, &attrbits);
7162 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
7163 *tl = txdr_unsigned(NFSV4OP_LAYOUTGET);
7164 if ((mode & NFSV4OPEN_ACCESSWRITE) != 0)
7165 iomode = NFSLAYOUTIOMODE_RW;
7167 iomode = NFSLAYOUTIOMODE_READ;
7168 nfsrv_setuplayoutget(nd, iomode, 0, UINT64_MAX, 0, stateidp,
7169 layouttype, layoutlen, usecurstateid);
7170 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, vp, p, cred,
7171 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
7174 NFSCL_INCRSEQID(op->nfso_own->nfsow_seqid, nd);
7175 if (nd->nd_repstat != 0)
7176 *laystatp = nd->nd_repstat;
7177 if ((nd->nd_flag & ND_NOMOREDATA) == 0) {
7178 /* ND_NOMOREDATA will be set if the Open operation failed. */
7179 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
7181 op->nfso_stateid.seqid = *tl++;
7182 op->nfso_stateid.other[0] = *tl++;
7183 op->nfso_stateid.other[1] = *tl++;
7184 op->nfso_stateid.other[2] = *tl;
7185 rflags = fxdr_unsigned(u_int32_t, *(tl + 6));
7186 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
7189 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
7190 deleg = fxdr_unsigned(u_int32_t, *tl);
7191 if (deleg == NFSV4OPEN_DELEGATEREAD ||
7192 deleg == NFSV4OPEN_DELEGATEWRITE) {
7193 if (!(op->nfso_own->nfsow_clp->nfsc_flags &
7194 NFSCLFLAGS_FIRSTDELEG))
7195 op->nfso_own->nfsow_clp->nfsc_flags |=
7196 (NFSCLFLAGS_FIRSTDELEG | NFSCLFLAGS_GOTDELEG);
7197 ndp = malloc(sizeof(struct nfscldeleg) + newfhlen,
7198 M_NFSCLDELEG, M_WAITOK);
7199 LIST_INIT(&ndp->nfsdl_owner);
7200 LIST_INIT(&ndp->nfsdl_lock);
7201 ndp->nfsdl_clp = op->nfso_own->nfsow_clp;
7202 ndp->nfsdl_fhlen = newfhlen;
7203 NFSBCOPY(newfhp, ndp->nfsdl_fh, newfhlen);
7204 newnfs_copyincred(cred, &ndp->nfsdl_cred);
7205 nfscl_lockinit(&ndp->nfsdl_rwlock);
7206 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
7208 ndp->nfsdl_stateid.seqid = *tl++;
7209 ndp->nfsdl_stateid.other[0] = *tl++;
7210 ndp->nfsdl_stateid.other[1] = *tl++;
7211 ndp->nfsdl_stateid.other[2] = *tl++;
7212 ret = fxdr_unsigned(int, *tl);
7213 if (deleg == NFSV4OPEN_DELEGATEWRITE) {
7214 ndp->nfsdl_flags = NFSCLDL_WRITE;
7216 * Indicates how much the file can grow.
7218 NFSM_DISSECT(tl, u_int32_t *,
7220 limitby = fxdr_unsigned(int, *tl++);
7222 case NFSV4OPEN_LIMITSIZE:
7223 ndp->nfsdl_sizelimit = fxdr_hyper(tl);
7225 case NFSV4OPEN_LIMITBLOCKS:
7226 ndp->nfsdl_sizelimit =
7227 fxdr_unsigned(u_int64_t, *tl++);
7228 ndp->nfsdl_sizelimit *=
7229 fxdr_unsigned(u_int64_t, *tl);
7232 error = NFSERR_BADXDR;
7236 ndp->nfsdl_flags = NFSCLDL_READ;
7238 ndp->nfsdl_flags |= NFSCLDL_RECALL;
7239 error = nfsrv_dissectace(nd, &ndp->nfsdl_ace, &ret,
7243 } else if (deleg != NFSV4OPEN_DELEGATENONE) {
7244 error = NFSERR_BADXDR;
7247 if ((rflags & NFSV4OPEN_LOCKTYPEPOSIX) != 0 ||
7248 nfscl_assumeposixlocks)
7249 op->nfso_posixlock = 1;
7251 op->nfso_posixlock = 0;
7252 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
7253 /* If the 2nd element == NFS_OK, the Getattr succeeded. */
7255 error = nfsv4_loadattr(nd, NULL, &nfsva, NULL,
7256 NULL, 0, NULL, NULL, NULL, NULL, NULL, 0,
7257 NULL, NULL, NULL, p, cred);
7261 ndp->nfsdl_change = nfsva.na_filerev;
7262 ndp->nfsdl_modtime = nfsva.na_mtime;
7263 ndp->nfsdl_flags |= NFSCLDL_MODTIMESET;
7268 * At this point, the Open has succeeded, so set
7269 * nd_repstat = NFS_OK. If the Layoutget failed,
7270 * this function just won't return a layout.
7272 if (nd->nd_repstat == 0) {
7273 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
7274 *laystatp = fxdr_unsigned(int, *++tl);
7275 if (*laystatp == 0) {
7276 error = nfsrv_parselayoutget(nd,
7277 stateidp, retonclosep, flhp);
7282 nd->nd_repstat = 0; /* Return 0 for Open. */
7285 if (nd->nd_repstat != 0 && error == 0)
7286 error = nd->nd_repstat;
7288 free(ndp, M_NFSCLDELEG);
7289 mbuf_freem(nd->nd_mrep);
7294 * Similar nfsrpc_createv4(), but also does the LayoutGet operation.
7295 * Used only for mounts with pNFS enabled.
7298 nfsrpc_createlayout(vnode_t dvp, char *name, int namelen, struct vattr *vap,
7299 nfsquad_t cverf, int fmode, struct nfsclowner *owp, struct nfscldeleg **dpp,
7300 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap,
7301 struct nfsvattr *nnap, struct nfsfh **nfhpp, int *attrflagp,
7302 int *dattrflagp, void *dstuff, int *unlockedp, nfsv4stateid_t *stateidp,
7303 int usecurstateid, int layouttype, int layoutlen, int *retonclosep,
7304 struct nfsclflayouthead *flhp, int *laystatp)
7307 int error = 0, deleg, newone, ret, acesize, limitby;
7308 struct nfsrv_descript nfsd, *nd = &nfsd;
7309 struct nfsclopen *op;
7310 struct nfscldeleg *dp = NULL;
7313 struct nfsclsession *tsep;
7314 nfsattrbit_t attrbits;
7315 nfsv4stateid_t stateid;
7316 struct nfsmount *nmp;
7318 nmp = VFSTONFS(dvp->v_mount);
7326 if (namelen > NFS_MAXNAMLEN)
7327 return (ENAMETOOLONG);
7328 NFSCL_REQSTART(nd, NFSPROC_CREATELAYGET, dvp);
7330 * For V4, this is actually an Open op.
7332 NFSM_BUILD(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
7333 *tl++ = txdr_unsigned(owp->nfsow_seqid);
7334 *tl++ = txdr_unsigned(NFSV4OPEN_ACCESSWRITE |
7335 NFSV4OPEN_ACCESSREAD);
7336 *tl++ = txdr_unsigned(NFSV4OPEN_DENYNONE);
7337 tsep = nfsmnt_mdssession(nmp);
7338 *tl++ = tsep->nfsess_clientid.lval[0];
7339 *tl = tsep->nfsess_clientid.lval[1];
7340 nfsm_strtom(nd, owp->nfsow_owner, NFSV4CL_LOCKNAMELEN);
7341 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
7342 *tl++ = txdr_unsigned(NFSV4OPEN_CREATE);
7343 if ((fmode & O_EXCL) != 0) {
7344 if (NFSHASSESSPERSIST(nmp)) {
7345 /* Use GUARDED for persistent sessions. */
7346 *tl = txdr_unsigned(NFSCREATE_GUARDED);
7347 nfscl_fillsattr(nd, vap, dvp, 0, 0);
7349 /* Otherwise, use EXCLUSIVE4_1. */
7350 *tl = txdr_unsigned(NFSCREATE_EXCLUSIVE41);
7351 NFSM_BUILD(tl, u_int32_t *, NFSX_VERF);
7352 *tl++ = cverf.lval[0];
7353 *tl = cverf.lval[1];
7354 nfscl_fillsattr(nd, vap, dvp, 0, 0);
7357 *tl = txdr_unsigned(NFSCREATE_UNCHECKED);
7358 nfscl_fillsattr(nd, vap, dvp, 0, 0);
7360 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
7361 *tl = txdr_unsigned(NFSV4OPEN_CLAIMNULL);
7362 nfsm_strtom(nd, name, namelen);
7363 /* Get the new file's handle and attributes, plus save the FH. */
7364 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
7365 *tl++ = txdr_unsigned(NFSV4OP_SAVEFH);
7366 *tl++ = txdr_unsigned(NFSV4OP_GETFH);
7367 *tl = txdr_unsigned(NFSV4OP_GETATTR);
7368 NFSGETATTR_ATTRBIT(&attrbits);
7369 nfsrv_putattrbit(nd, &attrbits);
7370 /* Get the directory's post-op attributes. */
7371 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
7372 *tl = txdr_unsigned(NFSV4OP_PUTFH);
7373 nfsm_fhtom(nd, np->n_fhp->nfh_fh, np->n_fhp->nfh_len, 0);
7374 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
7375 *tl = txdr_unsigned(NFSV4OP_GETATTR);
7376 nfsrv_putattrbit(nd, &attrbits);
7377 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
7378 *tl++ = txdr_unsigned(NFSV4OP_RESTOREFH);
7379 *tl = txdr_unsigned(NFSV4OP_LAYOUTGET);
7380 nfsrv_setuplayoutget(nd, NFSLAYOUTIOMODE_RW, 0, UINT64_MAX, 0, stateidp,
7381 layouttype, layoutlen, usecurstateid);
7382 error = nfscl_request(nd, dvp, p, cred, dstuff);
7385 NFSCL_DEBUG(4, "nfsrpc_createlayout stat=%d err=%d\n", nd->nd_repstat,
7387 if (nd->nd_repstat != 0)
7388 *laystatp = nd->nd_repstat;
7389 NFSCL_INCRSEQID(owp->nfsow_seqid, nd);
7390 if ((nd->nd_flag & ND_NOMOREDATA) == 0) {
7391 NFSCL_DEBUG(4, "nfsrpc_createlayout open succeeded\n");
7392 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
7394 stateid.seqid = *tl++;
7395 stateid.other[0] = *tl++;
7396 stateid.other[1] = *tl++;
7397 stateid.other[2] = *tl;
7398 nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
7399 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
7400 deleg = fxdr_unsigned(int, *tl);
7401 if (deleg == NFSV4OPEN_DELEGATEREAD ||
7402 deleg == NFSV4OPEN_DELEGATEWRITE) {
7403 if (!(owp->nfsow_clp->nfsc_flags &
7404 NFSCLFLAGS_FIRSTDELEG))
7405 owp->nfsow_clp->nfsc_flags |=
7406 (NFSCLFLAGS_FIRSTDELEG | NFSCLFLAGS_GOTDELEG);
7407 dp = malloc(sizeof(struct nfscldeleg) + NFSX_V4FHMAX,
7408 M_NFSCLDELEG, M_WAITOK);
7409 LIST_INIT(&dp->nfsdl_owner);
7410 LIST_INIT(&dp->nfsdl_lock);
7411 dp->nfsdl_clp = owp->nfsow_clp;
7412 newnfs_copyincred(cred, &dp->nfsdl_cred);
7413 nfscl_lockinit(&dp->nfsdl_rwlock);
7414 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
7416 dp->nfsdl_stateid.seqid = *tl++;
7417 dp->nfsdl_stateid.other[0] = *tl++;
7418 dp->nfsdl_stateid.other[1] = *tl++;
7419 dp->nfsdl_stateid.other[2] = *tl++;
7420 ret = fxdr_unsigned(int, *tl);
7421 if (deleg == NFSV4OPEN_DELEGATEWRITE) {
7422 dp->nfsdl_flags = NFSCLDL_WRITE;
7424 * Indicates how much the file can grow.
7426 NFSM_DISSECT(tl, u_int32_t *,
7428 limitby = fxdr_unsigned(int, *tl++);
7430 case NFSV4OPEN_LIMITSIZE:
7431 dp->nfsdl_sizelimit = fxdr_hyper(tl);
7433 case NFSV4OPEN_LIMITBLOCKS:
7434 dp->nfsdl_sizelimit =
7435 fxdr_unsigned(u_int64_t, *tl++);
7436 dp->nfsdl_sizelimit *=
7437 fxdr_unsigned(u_int64_t, *tl);
7440 error = NFSERR_BADXDR;
7444 dp->nfsdl_flags = NFSCLDL_READ;
7447 dp->nfsdl_flags |= NFSCLDL_RECALL;
7448 error = nfsrv_dissectace(nd, &dp->nfsdl_ace, &ret,
7452 } else if (deleg != NFSV4OPEN_DELEGATENONE) {
7453 error = NFSERR_BADXDR;
7457 /* Now, we should have the status for the SaveFH. */
7458 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
7460 NFSCL_DEBUG(4, "nfsrpc_createlayout SaveFH ok\n");
7462 * Now, process the GetFH and Getattr for the newly
7463 * created file. nfscl_mtofh() will set
7464 * ND_NOMOREDATA if these weren't successful.
7466 error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp);
7467 NFSCL_DEBUG(4, "aft nfscl_mtofh err=%d\n", error);
7471 nd->nd_flag |= ND_NOMOREDATA;
7472 /* Now we have the PutFH and Getattr for the directory. */
7473 if ((nd->nd_flag & ND_NOMOREDATA) == 0) {
7474 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
7476 nd->nd_flag |= ND_NOMOREDATA;
7478 NFSM_DISSECT(tl, uint32_t *, 2 *
7481 nd->nd_flag |= ND_NOMOREDATA;
7484 if ((nd->nd_flag & ND_NOMOREDATA) == 0) {
7485 /* Load the directory attributes. */
7486 error = nfsm_loadattr(nd, dnap);
7487 NFSCL_DEBUG(4, "aft nfsm_loadattr err=%d\n", error);
7491 if (dp != NULL && *attrflagp != 0) {
7492 dp->nfsdl_change = nnap->na_filerev;
7493 dp->nfsdl_modtime = nnap->na_mtime;
7494 dp->nfsdl_flags |= NFSCLDL_MODTIMESET;
7497 * We can now complete the Open state.
7501 dp->nfsdl_fhlen = nfhp->nfh_len;
7502 NFSBCOPY(nfhp->nfh_fh, dp->nfsdl_fh,
7506 * Get an Open structure that will be
7507 * attached to the OpenOwner, acquired already.
7509 error = nfscl_open(dvp, nfhp->nfh_fh, nfhp->nfh_len,
7510 (NFSV4OPEN_ACCESSWRITE | NFSV4OPEN_ACCESSREAD), 0,
7511 cred, p, NULL, &op, &newone, NULL, 0);
7514 op->nfso_stateid = stateid;
7515 newnfs_copyincred(cred, &op->nfso_cred);
7517 nfscl_openrelease(nmp, op, error, newone);
7520 /* Now, handle the RestoreFH and LayoutGet. */
7521 if (nd->nd_repstat == 0) {
7522 NFSM_DISSECT(tl, uint32_t *, 4 * NFSX_UNSIGNED);
7523 *laystatp = fxdr_unsigned(int, *(tl + 3));
7524 if (*laystatp == 0) {
7525 error = nfsrv_parselayoutget(nd,
7526 stateidp, retonclosep, flhp);
7530 NFSCL_DEBUG(4, "aft nfsrv_parselayout err=%d\n",
7536 if (nd->nd_repstat != 0 && error == 0)
7537 error = nd->nd_repstat;
7538 if (error == NFSERR_STALECLIENTID || error == NFSERR_BADSESSION)
7539 nfscl_initiate_recovery(owp->nfsow_clp);
7541 NFSCL_DEBUG(4, "eo nfsrpc_createlayout err=%d\n", error);
7545 free(dp, M_NFSCLDELEG);
7546 mbuf_freem(nd->nd_mrep);
7551 * Similar to nfsrpc_getopenlayout(), except that it used for the Create case.
7554 nfsrpc_getcreatelayout(vnode_t dvp, char *name, int namelen, struct vattr *vap,
7555 nfsquad_t cverf, int fmode, struct nfsclowner *owp, struct nfscldeleg **dpp,
7556 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap,
7557 struct nfsvattr *nnap, struct nfsfh **nfhpp, int *attrflagp,
7558 int *dattrflagp, void *dstuff, int *unlockedp)
7560 struct nfscllayout *lyp;
7561 struct nfsclflayouthead flh;
7563 struct nfsclsession *tsep;
7564 struct nfsmount *nmp;
7565 nfsv4stateid_t stateid;
7566 int error, layoutlen, layouttype, retonclose, laystat;
7569 nmp = VFSTONFS(dvp->v_mount);
7570 if (NFSHASFLEXFILE(nmp))
7571 layouttype = NFSLAYOUT_FLEXFILE;
7573 layouttype = NFSLAYOUT_NFSV4_1_FILES;
7575 tsep = nfsmnt_mdssession(nmp);
7576 layoutlen = tsep->nfsess_maxcache - (NFSX_STATEID + 3 * NFSX_UNSIGNED);
7577 error = nfsrpc_createlayout(dvp, name, namelen, vap, cverf, fmode,
7578 owp, dpp, cred, p, dnap, nnap, nfhpp, attrflagp, dattrflagp,
7579 dstuff, unlockedp, &stateid, 1, layouttype, layoutlen, &retonclose,
7581 NFSCL_DEBUG(4, "aft nfsrpc_createlayoutrpc laystat=%d err=%d\n",
7586 laystat = nfsrpc_layoutgetres(nmp, dvp, nfhp->nfh_fh,
7587 nfhp->nfh_len, &stateid, retonclose, NULL, &lyp, &flh,
7588 layouttype, laystat, NULL, cred, p);
7590 laystat = nfsrpc_layoutgetres(nmp, dvp, NULL, 0, &stateid,
7591 retonclose, NULL, &lyp, &flh, layouttype, laystat, NULL,
7594 nfscl_rellayout(lyp, 0);
7599 * Process the results of a layoutget() operation.
7602 nfsrpc_layoutgetres(struct nfsmount *nmp, vnode_t vp, uint8_t *newfhp,
7603 int newfhlen, nfsv4stateid_t *stateidp, int retonclose, uint32_t *notifybit,
7604 struct nfscllayout **lypp, struct nfsclflayouthead *flhp, int layouttype,
7605 int laystat, int *islockedp, struct ucred *cred, NFSPROC_T *p)
7607 struct nfsclflayout *tflp;
7608 struct nfscldevinfo *dip;
7612 if (laystat == NFSERR_UNKNLAYOUTTYPE) {
7614 if (!NFSHASFLEXFILE(nmp)) {
7615 /* Switch to using Flex File Layout. */
7616 nmp->nm_state |= NFSSTA_FLEXFILE;
7617 } else if (layouttype == NFSLAYOUT_FLEXFILE) {
7619 NFSCL_DEBUG(1, "disable PNFS\n");
7620 nmp->nm_state &= ~(NFSSTA_PNFS | NFSSTA_FLEXFILE);
7625 NFSCL_DEBUG(4, "nfsrpc_layoutgetres at FOREACH\n");
7626 LIST_FOREACH(tflp, flhp, nfsfl_list) {
7627 if (layouttype == NFSLAYOUT_FLEXFILE)
7628 mirrorcnt = tflp->nfsfl_mirrorcnt;
7631 for (i = 0; i < mirrorcnt; i++) {
7632 laystat = nfscl_adddevinfo(nmp, NULL, i, tflp);
7633 NFSCL_DEBUG(4, "aft adddev=%d\n", laystat);
7635 if (layouttype == NFSLAYOUT_FLEXFILE)
7636 dev = tflp->nfsfl_ffm[i].dev;
7638 dev = tflp->nfsfl_dev;
7639 laystat = nfsrpc_getdeviceinfo(nmp, dev,
7640 layouttype, notifybit, &dip, cred,
7642 NFSCL_DEBUG(4, "aft nfsrpc_gdi=%d\n",
7646 laystat = nfscl_adddevinfo(nmp, dip, i,
7649 printf("nfsrpc_layoutgetresout"
7658 * nfscl_layout() always returns with the nfsly_lock
7659 * set to a refcnt (shared lock).
7660 * Passing in dvp is sufficient, since it is only used to
7661 * get the fsid for the file system.
7663 laystat = nfscl_layout(nmp, vp, newfhp, newfhlen, stateidp,
7664 layouttype, retonclose, flhp, lypp, cred, p);
7665 NFSCL_DEBUG(4, "nfsrpc_layoutgetres: aft nfscl_layout=%d\n",
7667 if (laystat == 0 && islockedp != NULL)