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.
48 #include "opt_inet6.h"
50 #include <fs/nfs/nfsport.h>
51 #include <sys/sysctl.h>
52 #include <sys/taskqueue.h>
54 SYSCTL_DECL(_vfs_nfs);
56 static int nfsignore_eexist = 0;
57 SYSCTL_INT(_vfs_nfs, OID_AUTO, ignore_eexist, CTLFLAG_RW,
58 &nfsignore_eexist, 0, "NFS ignore EEXIST replies for mkdir/symlink");
60 static int nfscl_dssameconn = 0;
61 SYSCTL_INT(_vfs_nfs, OID_AUTO, dssameconn, CTLFLAG_RW,
62 &nfscl_dssameconn, 0, "Use same TCP connection to multiple DSs");
67 extern int nfs_numnfscbd;
68 extern struct timeval nfsboottime;
69 extern u_int32_t newnfs_false, newnfs_true;
70 extern nfstype nfsv34_type[9];
71 extern int nfsrv_useacl;
72 extern char nfsv4_callbackaddr[INET6_ADDRSTRLEN];
73 extern int nfscl_debuglevel;
74 extern int nfs_pnfsiothreads;
76 int nfstest_outofseq = 0;
77 int nfscl_assumeposixlocks = 1;
78 int nfscl_enablecallb = 0;
79 short nfsv4_cbport = NFSV4_CBPORT;
80 int nfstest_openallsetattr = 0;
81 #endif /* !APPLEKEXT */
83 #define DIRHDSIZ offsetof(struct dirent, d_name)
86 * nfscl_getsameserver() can return one of three values:
87 * NFSDSP_USETHISSESSION - Use this session for the DS.
88 * NFSDSP_SEQTHISSESSION - Use the nfsclds_sequence field of this dsp for new
90 * NFSDSP_NOTFOUND - No matching server was found.
93 NFSDSP_USETHISSESSION = 0,
94 NFSDSP_SEQTHISSESSION = 1,
99 * Do a write RPC on a DS data file, using this structure for the arguments,
100 * so that this function can be executed by a separate kernel process.
102 struct nfsclwritedsdorpc {
109 nfsv4stateid_t *stateidp;
122 static int nfsrpc_setattrrpc(vnode_t , struct vattr *, nfsv4stateid_t *,
123 struct ucred *, NFSPROC_T *, struct nfsvattr *, int *, void *);
124 static int nfsrpc_readrpc(vnode_t , struct uio *, struct ucred *,
125 nfsv4stateid_t *, NFSPROC_T *, struct nfsvattr *, int *, void *);
126 static int nfsrpc_writerpc(vnode_t , struct uio *, int *, int *,
127 struct ucred *, nfsv4stateid_t *, NFSPROC_T *, struct nfsvattr *, int *,
129 static int nfsrpc_createv23(vnode_t , char *, int, struct vattr *,
130 nfsquad_t, int, struct ucred *, NFSPROC_T *, struct nfsvattr *,
131 struct nfsvattr *, struct nfsfh **, int *, int *, void *);
132 static int nfsrpc_createv4(vnode_t , char *, int, struct vattr *,
133 nfsquad_t, int, struct nfsclowner *, struct nfscldeleg **, struct ucred *,
134 NFSPROC_T *, struct nfsvattr *, struct nfsvattr *, struct nfsfh **, int *,
135 int *, void *, int *);
136 static int nfsrpc_locku(struct nfsrv_descript *, struct nfsmount *,
137 struct nfscllockowner *, u_int64_t, u_int64_t,
138 u_int32_t, struct ucred *, NFSPROC_T *, int);
139 static int nfsrpc_setaclrpc(vnode_t, struct ucred *, NFSPROC_T *,
140 struct acl *, nfsv4stateid_t *, void *);
141 static int nfsrpc_getlayout(struct nfsmount *, vnode_t, struct nfsfh *, int,
142 uint32_t *, nfsv4stateid_t *, uint64_t, struct nfscllayout **,
143 struct ucred *, NFSPROC_T *);
144 static int nfsrpc_fillsa(struct nfsmount *, struct sockaddr_in *,
145 struct sockaddr_in6 *, sa_family_t, int, struct nfsclds **, NFSPROC_T *);
146 static void nfscl_initsessionslots(struct nfsclsession *);
147 static int nfscl_doflayoutio(vnode_t, struct uio *, int *, int *, int *,
148 nfsv4stateid_t *, int, struct nfscldevinfo *, struct nfscllayout *,
149 struct nfsclflayout *, uint64_t, uint64_t, int, struct ucred *,
151 static int nfscl_dofflayoutio(vnode_t, struct uio *, int *, int *, int *,
152 nfsv4stateid_t *, int, struct nfscldevinfo *, struct nfscllayout *,
153 struct nfsclflayout *, uint64_t, uint64_t, int, int, struct mbuf *,
154 struct nfsclwritedsdorpc *, struct ucred *, NFSPROC_T *);
155 static struct mbuf *nfsm_copym(struct mbuf *, int, int);
156 static int nfsrpc_readds(vnode_t, struct uio *, nfsv4stateid_t *, int *,
157 struct nfsclds *, uint64_t, int, struct nfsfh *, int, int, int,
158 struct ucred *, NFSPROC_T *);
159 static int nfsrpc_writeds(vnode_t, struct uio *, int *, int *,
160 nfsv4stateid_t *, struct nfsclds *, uint64_t, int,
161 struct nfsfh *, int, int, int, int, struct ucred *, NFSPROC_T *);
162 static int nfsio_writedsmir(vnode_t, int *, int *, nfsv4stateid_t *,
163 struct nfsclds *, uint64_t, int, struct nfsfh *, struct mbuf *, int, int,
164 struct nfsclwritedsdorpc *, struct ucred *, NFSPROC_T *);
165 static int nfsrpc_writedsmir(vnode_t, int *, int *, nfsv4stateid_t *,
166 struct nfsclds *, uint64_t, int, struct nfsfh *, struct mbuf *, int, int,
167 struct ucred *, NFSPROC_T *);
168 static enum nfsclds_state nfscl_getsameserver(struct nfsmount *,
169 struct nfsclds *, struct nfsclds **, uint32_t *);
170 static int nfsio_commitds(vnode_t, uint64_t, int, struct nfsclds *,
171 struct nfsfh *, int, int, struct nfsclwritedsdorpc *, struct ucred *,
173 static int nfsrpc_commitds(vnode_t, uint64_t, int, struct nfsclds *,
174 struct nfsfh *, int, int, struct ucred *, NFSPROC_T *);
175 static void nfsrv_setuplayoutget(struct nfsrv_descript *, int, uint64_t,
176 uint64_t, uint64_t, nfsv4stateid_t *, int, int, int);
177 static int nfsrv_parseug(struct nfsrv_descript *, int, uid_t *, gid_t *,
179 static int nfsrv_parselayoutget(struct nfsrv_descript *, nfsv4stateid_t *,
180 int *, struct nfsclflayouthead *);
181 static int nfsrpc_getopenlayout(struct nfsmount *, vnode_t, u_int8_t *,
182 int, uint8_t *, int, uint32_t, struct nfsclopen *, uint8_t *, int,
183 struct nfscldeleg **, struct ucred *, NFSPROC_T *);
184 static int nfsrpc_getcreatelayout(vnode_t, char *, int, struct vattr *,
185 nfsquad_t, int, struct nfsclowner *, struct nfscldeleg **,
186 struct ucred *, NFSPROC_T *, struct nfsvattr *, struct nfsvattr *,
187 struct nfsfh **, int *, int *, void *, int *);
188 static int nfsrpc_openlayoutrpc(struct nfsmount *, vnode_t, u_int8_t *,
189 int, uint8_t *, int, uint32_t, struct nfsclopen *, uint8_t *, int,
190 struct nfscldeleg **, nfsv4stateid_t *, int, int, int, int *,
191 struct nfsclflayouthead *, int *, struct ucred *, NFSPROC_T *);
192 static int nfsrpc_createlayout(vnode_t, char *, int, struct vattr *,
193 nfsquad_t, int, struct nfsclowner *, struct nfscldeleg **,
194 struct ucred *, NFSPROC_T *, struct nfsvattr *, struct nfsvattr *,
195 struct nfsfh **, int *, int *, void *, int *, nfsv4stateid_t *,
196 int, int, int, int *, struct nfsclflayouthead *, int *);
197 static int nfsrpc_layoutget(struct nfsmount *, uint8_t *, int, int, uint64_t,
198 uint64_t, uint64_t, int, int, nfsv4stateid_t *, int *,
199 struct nfsclflayouthead *, struct ucred *, NFSPROC_T *, void *);
200 static int nfsrpc_layoutgetres(struct nfsmount *, vnode_t, uint8_t *,
201 int, nfsv4stateid_t *, int, uint32_t *, struct nfscllayout **,
202 struct nfsclflayouthead *, int, int, int *, struct ucred *, NFSPROC_T *);
204 int nfs_pnfsio(task_fn_t *, void *);
207 * nfs null call from vfs.
210 nfsrpc_null(vnode_t vp, struct ucred *cred, NFSPROC_T *p)
213 struct nfsrv_descript nfsd, *nd = &nfsd;
215 NFSCL_REQSTART(nd, NFSPROC_NULL, vp);
216 error = nfscl_request(nd, vp, p, cred, NULL);
217 if (nd->nd_repstat && !error)
218 error = nd->nd_repstat;
219 mbuf_freem(nd->nd_mrep);
225 * For nfs version 3 and 4, use the access rpc to check accessibility. If file
226 * modes are changed on the server, accesses might still fail later.
229 nfsrpc_access(vnode_t vp, int acmode, struct ucred *cred,
230 NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp)
233 u_int32_t mode, rmode;
236 mode = NFSACCESS_READ;
239 if (vnode_vtype(vp) == VDIR) {
241 mode |= (NFSACCESS_MODIFY | NFSACCESS_EXTEND |
244 mode |= NFSACCESS_LOOKUP;
247 mode |= (NFSACCESS_MODIFY | NFSACCESS_EXTEND);
249 mode |= NFSACCESS_EXECUTE;
253 * Now, just call nfsrpc_accessrpc() to do the actual RPC.
255 error = nfsrpc_accessrpc(vp, mode, cred, p, nap, attrflagp, &rmode,
259 * The NFS V3 spec does not clarify whether or not
260 * the returned access bits can be a superset of
261 * the ones requested, so...
263 if (!error && (rmode & mode) != mode)
269 * The actual rpc, separated out for Darwin.
272 nfsrpc_accessrpc(vnode_t vp, u_int32_t mode, struct ucred *cred,
273 NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, u_int32_t *rmodep,
277 u_int32_t supported, rmode;
279 struct nfsrv_descript nfsd, *nd = &nfsd;
280 nfsattrbit_t attrbits;
284 NFSCL_REQSTART(nd, NFSPROC_ACCESS, vp);
285 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
286 *tl = txdr_unsigned(mode);
287 if (nd->nd_flag & ND_NFSV4) {
289 * And do a Getattr op.
291 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
292 *tl = txdr_unsigned(NFSV4OP_GETATTR);
293 NFSGETATTR_ATTRBIT(&attrbits);
294 (void) nfsrv_putattrbit(nd, &attrbits);
296 error = nfscl_request(nd, vp, p, cred, stuff);
299 if (nd->nd_flag & ND_NFSV3) {
300 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
304 if (!nd->nd_repstat) {
305 if (nd->nd_flag & ND_NFSV4) {
306 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
307 supported = fxdr_unsigned(u_int32_t, *tl++);
309 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
311 rmode = fxdr_unsigned(u_int32_t, *tl);
312 if (nd->nd_flag & ND_NFSV4)
313 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
316 * It's not obvious what should be done about
317 * unsupported access modes. For now, be paranoid
318 * and clear the unsupported ones.
323 error = nd->nd_repstat;
325 mbuf_freem(nd->nd_mrep);
333 nfsrpc_open(vnode_t vp, int amode, struct ucred *cred, NFSPROC_T *p)
335 struct nfsclopen *op;
336 struct nfscldeleg *dp;
338 struct nfsnode *np = VTONFS(vp);
339 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
340 u_int32_t mode, clidrev;
341 int ret, newone, error, expireret = 0, retrycnt;
344 * For NFSv4, Open Ops are only done on Regular Files.
346 if (vnode_vtype(vp) != VREG)
350 mode |= NFSV4OPEN_ACCESSREAD;
352 mode |= NFSV4OPEN_ACCESSWRITE;
357 { char name[100]; int namel;
358 namel = (np->n_v4->n4_namelen < 100) ? np->n_v4->n4_namelen : 99;
359 bcopy(NFS4NODENAME(np->n_v4), name, namel);
361 printf("rpcopen p=0x%x name=%s",p->p_pid,name);
362 if (nfhp->nfh_len > 0) printf(" fh=0x%x\n",nfhp->nfh_fh[12]);
363 else printf(" fhl=0\n");
368 error = nfscl_open(vp, nfhp->nfh_fh, nfhp->nfh_len, mode, 1,
369 cred, p, NULL, &op, &newone, &ret, 1);
373 if (nmp->nm_clp != NULL)
374 clidrev = nmp->nm_clp->nfsc_clientidrev;
377 if (ret == NFSCLOPEN_DOOPEN) {
378 if (np->n_v4 != NULL) {
380 * For the first attempt, try and get a layout, if
381 * pNFS is enabled for the mount.
383 if (!NFSHASPNFS(nmp) || nfscl_enablecallb == 0 ||
384 nfs_numnfscbd == 0 ||
385 (np->n_flag & NNOLAYOUT) != 0 || retrycnt > 0)
386 error = nfsrpc_openrpc(nmp, vp,
388 np->n_v4->n4_fhlen, np->n_fhp->nfh_fh,
389 np->n_fhp->nfh_len, mode, op,
390 NFS4NODENAME(np->n_v4),
391 np->n_v4->n4_namelen,
392 &dp, 0, 0x0, cred, p, 0, 0);
394 error = nfsrpc_getopenlayout(nmp, vp,
396 np->n_v4->n4_fhlen, np->n_fhp->nfh_fh,
397 np->n_fhp->nfh_len, mode, op,
398 NFS4NODENAME(np->n_v4),
399 np->n_v4->n4_namelen, &dp, cred, p);
402 OSBitAndAtomic((int32_t)~NDELEGMOD, (UInt32 *)&np->n_flag);
405 np->n_flag &= ~NDELEGMOD;
407 * Invalidate the attribute cache, so that
408 * attributes that pre-date the issue of a
409 * delegation are not cached, since the
410 * cached attributes will remain valid while
411 * the delegation is held.
413 NFSINVALATTRCACHE(np);
416 (void) nfscl_deleg(nmp->nm_mountp,
417 op->nfso_own->nfsow_clp,
418 nfhp->nfh_fh, nfhp->nfh_len, cred, p, &dp);
423 newnfs_copyincred(cred, &op->nfso_cred);
424 } else if (ret == NFSCLOPEN_SETCRED)
426 * This is a new local open on a delegation. It needs
427 * to have credentials so that an open can be done
428 * against the server during recovery.
430 newnfs_copyincred(cred, &op->nfso_cred);
433 * nfso_opencnt is the count of how many VOP_OPEN()s have
434 * been done on this Open successfully and a VOP_CLOSE()
435 * is expected for each of these.
436 * If error is non-zero, don't increment it, since the Open
437 * hasn't succeeded yet.
441 nfscl_openrelease(nmp, op, error, newone);
442 if (error == NFSERR_GRACE || error == NFSERR_STALECLIENTID ||
443 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
444 error == NFSERR_BADSESSION) {
445 (void) nfs_catnap(PZERO, error, "nfs_open");
446 } else if ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID)
448 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
451 } while (error == NFSERR_GRACE || error == NFSERR_STALECLIENTID ||
452 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
453 error == NFSERR_BADSESSION ||
454 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
455 expireret == 0 && clidrev != 0 && retrycnt < 4));
456 if (error && retrycnt >= 4)
462 * the actual open rpc
465 nfsrpc_openrpc(struct nfsmount *nmp, vnode_t vp, u_int8_t *nfhp, int fhlen,
466 u_int8_t *newfhp, int newfhlen, u_int32_t mode, struct nfsclopen *op,
467 u_int8_t *name, int namelen, struct nfscldeleg **dpp,
468 int reclaim, u_int32_t delegtype, struct ucred *cred, NFSPROC_T *p,
469 int syscred, int recursed)
472 struct nfsrv_descript nfsd, *nd = &nfsd;
473 struct nfscldeleg *dp, *ndp = NULL;
474 struct nfsvattr nfsva;
475 u_int32_t rflags, deleg;
476 nfsattrbit_t attrbits;
477 int error, ret, acesize, limitby;
478 struct nfsclsession *tsep;
482 nfscl_reqstart(nd, NFSPROC_OPEN, nmp, nfhp, fhlen, NULL, NULL, 0, 0);
483 NFSM_BUILD(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
484 *tl++ = txdr_unsigned(op->nfso_own->nfsow_seqid);
485 *tl++ = txdr_unsigned(mode & NFSV4OPEN_ACCESSBOTH);
486 *tl++ = txdr_unsigned((mode >> NFSLCK_SHIFT) & NFSV4OPEN_DENYBOTH);
487 tsep = nfsmnt_mdssession(nmp);
488 *tl++ = tsep->nfsess_clientid.lval[0];
489 *tl = tsep->nfsess_clientid.lval[1];
490 (void) nfsm_strtom(nd, op->nfso_own->nfsow_owner, NFSV4CL_LOCKNAMELEN);
491 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
492 *tl++ = txdr_unsigned(NFSV4OPEN_NOCREATE);
494 *tl = txdr_unsigned(NFSV4OPEN_CLAIMPREVIOUS);
495 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
496 *tl = txdr_unsigned(delegtype);
499 *tl = txdr_unsigned(NFSV4OPEN_CLAIMDELEGATECUR);
500 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID);
501 if (NFSHASNFSV4N(nmp))
504 *tl++ = dp->nfsdl_stateid.seqid;
505 *tl++ = dp->nfsdl_stateid.other[0];
506 *tl++ = dp->nfsdl_stateid.other[1];
507 *tl = dp->nfsdl_stateid.other[2];
509 *tl = txdr_unsigned(NFSV4OPEN_CLAIMNULL);
511 (void) nfsm_strtom(nd, name, namelen);
513 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
514 *tl = txdr_unsigned(NFSV4OP_GETATTR);
515 NFSZERO_ATTRBIT(&attrbits);
516 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_CHANGE);
517 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEMODIFY);
518 (void) nfsrv_putattrbit(nd, &attrbits);
520 nd->nd_flag |= ND_USEGSSNAME;
521 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, vp, p, cred,
522 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
525 NFSCL_INCRSEQID(op->nfso_own->nfsow_seqid, nd);
526 if (!nd->nd_repstat) {
527 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
529 op->nfso_stateid.seqid = *tl++;
530 op->nfso_stateid.other[0] = *tl++;
531 op->nfso_stateid.other[1] = *tl++;
532 op->nfso_stateid.other[2] = *tl;
533 rflags = fxdr_unsigned(u_int32_t, *(tl + 6));
534 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
537 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
538 deleg = fxdr_unsigned(u_int32_t, *tl);
539 if (deleg == NFSV4OPEN_DELEGATEREAD ||
540 deleg == NFSV4OPEN_DELEGATEWRITE) {
541 if (!(op->nfso_own->nfsow_clp->nfsc_flags &
542 NFSCLFLAGS_FIRSTDELEG))
543 op->nfso_own->nfsow_clp->nfsc_flags |=
544 (NFSCLFLAGS_FIRSTDELEG | NFSCLFLAGS_GOTDELEG);
546 sizeof (struct nfscldeleg) + newfhlen,
547 M_NFSCLDELEG, M_WAITOK);
548 LIST_INIT(&ndp->nfsdl_owner);
549 LIST_INIT(&ndp->nfsdl_lock);
550 ndp->nfsdl_clp = op->nfso_own->nfsow_clp;
551 ndp->nfsdl_fhlen = newfhlen;
552 NFSBCOPY(newfhp, ndp->nfsdl_fh, newfhlen);
553 newnfs_copyincred(cred, &ndp->nfsdl_cred);
554 nfscl_lockinit(&ndp->nfsdl_rwlock);
555 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
557 ndp->nfsdl_stateid.seqid = *tl++;
558 ndp->nfsdl_stateid.other[0] = *tl++;
559 ndp->nfsdl_stateid.other[1] = *tl++;
560 ndp->nfsdl_stateid.other[2] = *tl++;
561 ret = fxdr_unsigned(int, *tl);
562 if (deleg == NFSV4OPEN_DELEGATEWRITE) {
563 ndp->nfsdl_flags = NFSCLDL_WRITE;
565 * Indicates how much the file can grow.
567 NFSM_DISSECT(tl, u_int32_t *,
569 limitby = fxdr_unsigned(int, *tl++);
571 case NFSV4OPEN_LIMITSIZE:
572 ndp->nfsdl_sizelimit = fxdr_hyper(tl);
574 case NFSV4OPEN_LIMITBLOCKS:
575 ndp->nfsdl_sizelimit =
576 fxdr_unsigned(u_int64_t, *tl++);
577 ndp->nfsdl_sizelimit *=
578 fxdr_unsigned(u_int64_t, *tl);
581 error = NFSERR_BADXDR;
585 ndp->nfsdl_flags = NFSCLDL_READ;
588 ndp->nfsdl_flags |= NFSCLDL_RECALL;
589 error = nfsrv_dissectace(nd, &ndp->nfsdl_ace, &ret,
593 } else if (deleg != NFSV4OPEN_DELEGATENONE) {
594 error = NFSERR_BADXDR;
597 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
598 error = nfsv4_loadattr(nd, NULL, &nfsva, NULL,
599 NULL, 0, NULL, NULL, NULL, NULL, NULL, 0,
600 NULL, NULL, NULL, p, cred);
604 ndp->nfsdl_change = nfsva.na_filerev;
605 ndp->nfsdl_modtime = nfsva.na_mtime;
606 ndp->nfsdl_flags |= NFSCLDL_MODTIMESET;
608 if (!reclaim && (rflags & NFSV4OPEN_RESULTCONFIRM)) {
610 ret = nfsrpc_openconfirm(vp, newfhp, newfhlen, op,
612 if (ret == NFSERR_DELAY)
613 (void) nfs_catnap(PZERO, ret, "nfs_open");
614 } while (ret == NFSERR_DELAY);
617 if ((rflags & NFSV4OPEN_LOCKTYPEPOSIX) ||
618 nfscl_assumeposixlocks)
619 op->nfso_posixlock = 1;
621 op->nfso_posixlock = 0;
624 * If the server is handing out delegations, but we didn't
625 * get one because an OpenConfirm was required, try the
626 * Open again, to get a delegation. This is a harmless no-op,
627 * from a server's point of view.
629 if (!reclaim && (rflags & NFSV4OPEN_RESULTCONFIRM) &&
630 (op->nfso_own->nfsow_clp->nfsc_flags & NFSCLFLAGS_GOTDELEG)
631 && !error && dp == NULL && ndp == NULL && !recursed) {
633 ret = nfsrpc_openrpc(nmp, vp, nfhp, fhlen, newfhp,
634 newfhlen, mode, op, name, namelen, &ndp, 0, 0x0,
635 cred, p, syscred, 1);
636 if (ret == NFSERR_DELAY)
637 (void) nfs_catnap(PZERO, ret, "nfs_open2");
638 } while (ret == NFSERR_DELAY);
641 free(ndp, M_NFSCLDELEG);
644 if (ret == NFSERR_STALECLIENTID ||
645 ret == NFSERR_STALEDONTRECOVER ||
646 ret == NFSERR_BADSESSION)
651 if (nd->nd_repstat != 0 && error == 0)
652 error = nd->nd_repstat;
653 if (error == NFSERR_STALECLIENTID)
654 nfscl_initiate_recovery(op->nfso_own->nfsow_clp);
658 else if (ndp != NULL)
659 free(ndp, M_NFSCLDELEG);
660 mbuf_freem(nd->nd_mrep);
668 nfsrpc_opendowngrade(vnode_t vp, u_int32_t mode, struct nfsclopen *op,
669 struct ucred *cred, NFSPROC_T *p)
672 struct nfsrv_descript nfsd, *nd = &nfsd;
675 NFSCL_REQSTART(nd, NFSPROC_OPENDOWNGRADE, vp);
676 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID + 3 * NFSX_UNSIGNED);
677 if (NFSHASNFSV4N(VFSTONFS(vnode_mount(vp))))
680 *tl++ = op->nfso_stateid.seqid;
681 *tl++ = op->nfso_stateid.other[0];
682 *tl++ = op->nfso_stateid.other[1];
683 *tl++ = op->nfso_stateid.other[2];
684 *tl++ = txdr_unsigned(op->nfso_own->nfsow_seqid);
685 *tl++ = txdr_unsigned(mode & NFSV4OPEN_ACCESSBOTH);
686 *tl = txdr_unsigned((mode >> NFSLCK_SHIFT) & NFSV4OPEN_DENYBOTH);
687 error = nfscl_request(nd, vp, p, cred, NULL);
690 NFSCL_INCRSEQID(op->nfso_own->nfsow_seqid, nd);
691 if (!nd->nd_repstat) {
692 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID);
693 op->nfso_stateid.seqid = *tl++;
694 op->nfso_stateid.other[0] = *tl++;
695 op->nfso_stateid.other[1] = *tl++;
696 op->nfso_stateid.other[2] = *tl;
698 if (nd->nd_repstat && error == 0)
699 error = nd->nd_repstat;
700 if (error == NFSERR_STALESTATEID)
701 nfscl_initiate_recovery(op->nfso_own->nfsow_clp);
703 mbuf_freem(nd->nd_mrep);
708 * V4 Close operation.
711 nfsrpc_close(vnode_t vp, int doclose, NFSPROC_T *p)
713 struct nfsclclient *clp;
716 if (vnode_vtype(vp) != VREG)
719 error = nfscl_doclose(vp, &clp, p);
721 error = nfscl_getclose(vp, &clp);
725 nfscl_clientrelease(clp);
733 nfsrpc_doclose(struct nfsmount *nmp, struct nfsclopen *op, NFSPROC_T *p)
735 struct nfsrv_descript nfsd, *nd = &nfsd;
736 struct nfscllockowner *lp, *nlp;
737 struct nfscllock *lop, *nlop;
739 u_int64_t off = 0, len = 0;
740 u_int32_t type = NFSV4LOCKT_READ;
741 int error, do_unlock, trycnt;
743 tcred = newnfs_getcred();
744 newnfs_copycred(&op->nfso_cred, tcred);
746 * (Theoretically this could be done in the same
747 * compound as the close, but having multiple
748 * sequenced Ops in the same compound might be
749 * too scary for some servers.)
751 if (op->nfso_posixlock) {
754 type = NFSV4LOCKT_READ;
758 * Since this function is only called from VOP_INACTIVE(), no
759 * other thread will be manipulating this Open. As such, the
760 * lock lists are not being changed by other threads, so it should
761 * be safe to do this without locking.
763 LIST_FOREACH(lp, &op->nfso_lock, nfsl_list) {
765 LIST_FOREACH_SAFE(lop, &lp->nfsl_lock, nfslo_list, nlop) {
766 if (op->nfso_posixlock == 0) {
767 off = lop->nfslo_first;
768 len = lop->nfslo_end - lop->nfslo_first;
769 if (lop->nfslo_type == F_WRLCK)
770 type = NFSV4LOCKT_WRITE;
772 type = NFSV4LOCKT_READ;
777 error = nfsrpc_locku(nd, nmp, lp, off,
778 len, type, tcred, p, 0);
779 if ((nd->nd_repstat == NFSERR_GRACE ||
780 nd->nd_repstat == NFSERR_DELAY) &&
782 (void) nfs_catnap(PZERO,
785 } while ((nd->nd_repstat == NFSERR_GRACE ||
786 nd->nd_repstat == NFSERR_DELAY) &&
787 error == 0 && trycnt++ < 5);
788 if (op->nfso_posixlock)
791 nfscl_freelock(lop, 0);
794 * Do a ReleaseLockOwner.
795 * The lock owner name nfsl_owner may be used by other opens for
796 * other files but the lock_owner4 name that nfsrpc_rellockown()
797 * puts on the wire has the file handle for this file appended
798 * to it, so it can be done now.
800 (void)nfsrpc_rellockown(nmp, lp, lp->nfsl_open->nfso_fh,
801 lp->nfsl_open->nfso_fhlen, tcred, p);
805 * There could be other Opens for different files on the same
806 * OpenOwner, so locking is required.
809 nfscl_lockexcl(&op->nfso_own->nfsow_rwlock, NFSCLSTATEMUTEXPTR);
812 error = nfscl_tryclose(op, tcred, nmp, p);
813 if (error == NFSERR_GRACE)
814 (void) nfs_catnap(PZERO, error, "nfs_close");
815 } while (error == NFSERR_GRACE);
817 nfscl_lockunlock(&op->nfso_own->nfsow_rwlock);
819 LIST_FOREACH_SAFE(lp, &op->nfso_lock, nfsl_list, nlp)
820 nfscl_freelockowner(lp, 0);
821 nfscl_freeopen(op, 0);
827 * The actual Close RPC.
830 nfsrpc_closerpc(struct nfsrv_descript *nd, struct nfsmount *nmp,
831 struct nfsclopen *op, struct ucred *cred, NFSPROC_T *p,
837 nfscl_reqstart(nd, NFSPROC_CLOSE, nmp, op->nfso_fh,
838 op->nfso_fhlen, NULL, NULL, 0, 0);
839 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED + NFSX_STATEID);
840 *tl++ = txdr_unsigned(op->nfso_own->nfsow_seqid);
841 if (NFSHASNFSV4N(nmp))
844 *tl++ = op->nfso_stateid.seqid;
845 *tl++ = op->nfso_stateid.other[0];
846 *tl++ = op->nfso_stateid.other[1];
847 *tl = op->nfso_stateid.other[2];
849 nd->nd_flag |= ND_USEGSSNAME;
850 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
851 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
854 NFSCL_INCRSEQID(op->nfso_own->nfsow_seqid, nd);
855 if (nd->nd_repstat == 0)
856 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID);
857 error = nd->nd_repstat;
858 if (error == NFSERR_STALESTATEID)
859 nfscl_initiate_recovery(op->nfso_own->nfsow_clp);
861 mbuf_freem(nd->nd_mrep);
866 * V4 Open Confirm RPC.
869 nfsrpc_openconfirm(vnode_t vp, u_int8_t *nfhp, int fhlen,
870 struct nfsclopen *op, struct ucred *cred, NFSPROC_T *p)
873 struct nfsrv_descript nfsd, *nd = &nfsd;
874 struct nfsmount *nmp;
877 nmp = VFSTONFS(vnode_mount(vp));
878 if (NFSHASNFSV4N(nmp))
879 return (0); /* No confirmation for NFSv4.1. */
880 nfscl_reqstart(nd, NFSPROC_OPENCONFIRM, nmp, nfhp, fhlen, NULL, NULL,
882 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED + NFSX_STATEID);
883 *tl++ = op->nfso_stateid.seqid;
884 *tl++ = op->nfso_stateid.other[0];
885 *tl++ = op->nfso_stateid.other[1];
886 *tl++ = op->nfso_stateid.other[2];
887 *tl = txdr_unsigned(op->nfso_own->nfsow_seqid);
888 error = nfscl_request(nd, vp, p, cred, NULL);
891 NFSCL_INCRSEQID(op->nfso_own->nfsow_seqid, nd);
892 if (!nd->nd_repstat) {
893 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID);
894 op->nfso_stateid.seqid = *tl++;
895 op->nfso_stateid.other[0] = *tl++;
896 op->nfso_stateid.other[1] = *tl++;
897 op->nfso_stateid.other[2] = *tl;
899 error = nd->nd_repstat;
900 if (error == NFSERR_STALESTATEID)
901 nfscl_initiate_recovery(op->nfso_own->nfsow_clp);
903 mbuf_freem(nd->nd_mrep);
908 * Do the setclientid and setclientid confirm RPCs. Called from nfs_statfs()
909 * when a mount has just occurred and when the server replies NFSERR_EXPIRED.
912 nfsrpc_setclient(struct nfsmount *nmp, struct nfsclclient *clp, int reclaim,
913 struct ucred *cred, NFSPROC_T *p)
916 struct nfsrv_descript nfsd;
917 struct nfsrv_descript *nd = &nfsd;
918 nfsattrbit_t attrbits;
919 u_int8_t *cp = NULL, *cp2, addr[INET6_ADDRSTRLEN + 9];
921 int error, isinet6 = 0, callblen;
924 static u_int32_t rev = 0;
927 struct nfsclsession *tsep;
929 if (nfsboottime.tv_sec == 0)
930 NFSSETBOOTTIME(nfsboottime);
931 clp->nfsc_rev = rev++;
932 if (NFSHASNFSV4N(nmp)) {
934 * Either there was no previous session or the
935 * previous session has failed, so...
936 * do an ExchangeID followed by the CreateSession.
938 error = nfsrpc_exchangeid(nmp, clp, &nmp->nm_sockreq,
939 NFSV4EXCH_USEPNFSMDS | NFSV4EXCH_USENONPNFS, &dsp, cred, p);
940 NFSCL_DEBUG(1, "aft exch=%d\n", error);
942 error = nfsrpc_createsession(nmp, &dsp->nfsclds_sess,
944 dsp->nfsclds_sess.nfsess_sequenceid, 1, cred, p);
948 * The old sessions cannot be safely free'd
949 * here, since they may still be used by
953 if (TAILQ_FIRST(&nmp->nm_sess) != NULL)
954 tsep = NFSMNT_MDSSESSION(nmp);
955 TAILQ_INSERT_HEAD(&nmp->nm_sess, dsp,
958 * Wake up RPCs waiting for a slot on the
959 * old session. These will then fail with
960 * NFSERR_BADSESSION and be retried with the
961 * new session by nfsv4_setsequence().
962 * Also wakeup() processes waiting for the
966 wakeup(&tsep->nfsess_slots);
967 wakeup(&nmp->nm_sess);
970 nfscl_freenfsclds(dsp);
971 NFSCL_DEBUG(1, "aft createsess=%d\n", error);
972 if (error == 0 && reclaim == 0) {
973 error = nfsrpc_reclaimcomplete(nmp, cred, p);
974 NFSCL_DEBUG(1, "aft reclaimcomp=%d\n", error);
975 if (error == NFSERR_COMPLETEALREADY ||
976 error == NFSERR_NOTSUPP)
977 /* Ignore this error. */
984 * Allocate a single session structure for NFSv4.0, because some of
985 * the fields are used by NFSv4.0 although it doesn't do a session.
987 dsp = malloc(sizeof(struct nfsclds), M_NFSCLDS, M_WAITOK | M_ZERO);
988 mtx_init(&dsp->nfsclds_mtx, "nfsds", NULL, MTX_DEF);
989 mtx_init(&dsp->nfsclds_sess.nfsess_mtx, "nfssession", NULL, MTX_DEF);
991 TAILQ_INSERT_HEAD(&nmp->nm_sess, dsp, nfsclds_list);
992 tsep = NFSMNT_MDSSESSION(nmp);
995 nfscl_reqstart(nd, NFSPROC_SETCLIENTID, nmp, NULL, 0, NULL, NULL, 0, 0);
996 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
997 *tl++ = txdr_unsigned(nfsboottime.tv_sec);
998 *tl = txdr_unsigned(clp->nfsc_rev);
999 (void) nfsm_strtom(nd, clp->nfsc_id, clp->nfsc_idlen);
1002 * set up the callback address
1004 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1005 *tl = txdr_unsigned(NFS_CALLBCKPROG);
1006 callblen = strlen(nfsv4_callbackaddr);
1008 cp = nfscl_getmyip(nmp, &a6, &isinet6);
1009 if (nfscl_enablecallb && nfs_numnfscbd > 0 &&
1010 (callblen > 0 || cp != NULL)) {
1011 port = htons(nfsv4_cbport);
1012 cp2 = (u_int8_t *)&port;
1014 if ((callblen > 0 &&
1015 strchr(nfsv4_callbackaddr, ':')) || isinet6) {
1016 char ip6buf[INET6_ADDRSTRLEN], *ip6add;
1018 (void) nfsm_strtom(nd, "tcp6", 4);
1019 if (callblen == 0) {
1020 ip6_sprintf(ip6buf, (struct in6_addr *)cp);
1023 ip6add = nfsv4_callbackaddr;
1025 snprintf(addr, INET6_ADDRSTRLEN + 9, "%s.%d.%d",
1026 ip6add, cp2[0], cp2[1]);
1030 (void) nfsm_strtom(nd, "tcp", 3);
1032 snprintf(addr, INET6_ADDRSTRLEN + 9,
1033 "%d.%d.%d.%d.%d.%d", cp[0], cp[1],
1034 cp[2], cp[3], cp2[0], cp2[1]);
1036 snprintf(addr, INET6_ADDRSTRLEN + 9,
1037 "%s.%d.%d", nfsv4_callbackaddr,
1040 (void) nfsm_strtom(nd, addr, strlen(addr));
1042 (void) nfsm_strtom(nd, "tcp", 3);
1043 (void) nfsm_strtom(nd, "0.0.0.0.0.0", 11);
1045 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1046 *tl = txdr_unsigned(clp->nfsc_cbident);
1047 nd->nd_flag |= ND_USEGSSNAME;
1048 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
1049 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
1052 if (nd->nd_repstat == 0) {
1053 NFSM_DISSECT(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1054 tsep->nfsess_clientid.lval[0] = *tl++;
1055 tsep->nfsess_clientid.lval[1] = *tl++;
1056 confirm.lval[0] = *tl++;
1057 confirm.lval[1] = *tl;
1058 mbuf_freem(nd->nd_mrep);
1064 nfscl_reqstart(nd, NFSPROC_SETCLIENTIDCFRM, nmp, NULL, 0, NULL,
1066 NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1067 *tl++ = tsep->nfsess_clientid.lval[0];
1068 *tl++ = tsep->nfsess_clientid.lval[1];
1069 *tl++ = confirm.lval[0];
1070 *tl = confirm.lval[1];
1071 nd->nd_flag |= ND_USEGSSNAME;
1072 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p,
1073 cred, NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
1076 mbuf_freem(nd->nd_mrep);
1078 if (nd->nd_repstat == 0) {
1079 nfscl_reqstart(nd, NFSPROC_GETATTR, nmp, nmp->nm_fh,
1080 nmp->nm_fhsize, NULL, NULL, 0, 0);
1081 NFSZERO_ATTRBIT(&attrbits);
1082 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_LEASETIME);
1083 (void) nfsrv_putattrbit(nd, &attrbits);
1084 nd->nd_flag |= ND_USEGSSNAME;
1085 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p,
1086 cred, NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
1089 if (nd->nd_repstat == 0) {
1090 error = nfsv4_loadattr(nd, NULL, NULL, NULL, NULL, 0, NULL,
1091 NULL, NULL, NULL, NULL, 0, NULL, &lease, NULL, p, cred);
1094 clp->nfsc_renew = NFSCL_RENEW(lease);
1095 clp->nfsc_expire = NFSD_MONOSEC + clp->nfsc_renew;
1096 clp->nfsc_clientidrev++;
1097 if (clp->nfsc_clientidrev == 0)
1098 clp->nfsc_clientidrev++;
1102 error = nd->nd_repstat;
1104 mbuf_freem(nd->nd_mrep);
1112 nfsrpc_getattr(vnode_t vp, struct ucred *cred, NFSPROC_T *p,
1113 struct nfsvattr *nap, void *stuff)
1115 struct nfsrv_descript nfsd, *nd = &nfsd;
1117 nfsattrbit_t attrbits;
1119 NFSCL_REQSTART(nd, NFSPROC_GETATTR, vp);
1120 if (nd->nd_flag & ND_NFSV4) {
1121 NFSGETATTR_ATTRBIT(&attrbits);
1122 (void) nfsrv_putattrbit(nd, &attrbits);
1124 error = nfscl_request(nd, vp, p, cred, stuff);
1127 if (!nd->nd_repstat)
1128 error = nfsm_loadattr(nd, nap);
1130 error = nd->nd_repstat;
1131 mbuf_freem(nd->nd_mrep);
1136 * nfs getattr call with non-vnode arguemnts.
1139 nfsrpc_getattrnovp(struct nfsmount *nmp, u_int8_t *fhp, int fhlen, int syscred,
1140 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, u_int64_t *xidp,
1143 struct nfsrv_descript nfsd, *nd = &nfsd;
1144 int error, vers = NFS_VER2;
1145 nfsattrbit_t attrbits;
1147 nfscl_reqstart(nd, NFSPROC_GETATTR, nmp, fhp, fhlen, NULL, NULL, 0, 0);
1148 if (nd->nd_flag & ND_NFSV4) {
1150 NFSGETATTR_ATTRBIT(&attrbits);
1151 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_LEASETIME);
1152 (void) nfsrv_putattrbit(nd, &attrbits);
1153 } else if (nd->nd_flag & ND_NFSV3) {
1157 nd->nd_flag |= ND_USEGSSNAME;
1158 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
1159 NFS_PROG, vers, NULL, 1, xidp, NULL);
1162 if (nd->nd_repstat == 0) {
1163 if ((nd->nd_flag & ND_NFSV4) != 0)
1164 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0,
1165 NULL, NULL, NULL, NULL, NULL, 0, NULL, leasep, NULL,
1168 error = nfsm_loadattr(nd, nap);
1170 error = nd->nd_repstat;
1171 mbuf_freem(nd->nd_mrep);
1176 * Do an nfs setattr operation.
1179 nfsrpc_setattr(vnode_t vp, struct vattr *vap, NFSACL_T *aclp,
1180 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *rnap, int *attrflagp,
1183 int error, expireret = 0, openerr, retrycnt;
1184 u_int32_t clidrev = 0, mode;
1185 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
1187 nfsv4stateid_t stateid;
1190 if (nmp->nm_clp != NULL)
1191 clidrev = nmp->nm_clp->nfsc_clientidrev;
1192 if (vap != NULL && NFSATTRISSET(u_quad_t, vap, va_size))
1193 mode = NFSV4OPEN_ACCESSWRITE;
1195 mode = NFSV4OPEN_ACCESSREAD;
1200 if (NFSHASNFSV4(nmp)) {
1201 nfhp = VTONFS(vp)->n_fhp;
1202 error = nfscl_getstateid(vp, nfhp->nfh_fh,
1203 nfhp->nfh_len, mode, 0, cred, p, &stateid, &lckp);
1204 if (error && vnode_vtype(vp) == VREG &&
1205 (mode == NFSV4OPEN_ACCESSWRITE ||
1206 nfstest_openallsetattr)) {
1208 * No Open stateid, so try and open the file
1211 if (mode == NFSV4OPEN_ACCESSWRITE)
1212 openerr = nfsrpc_open(vp, FWRITE, cred,
1215 openerr = nfsrpc_open(vp, FREAD, cred,
1218 (void) nfscl_getstateid(vp,
1219 nfhp->nfh_fh, nfhp->nfh_len,
1220 mode, 0, cred, p, &stateid, &lckp);
1224 error = nfsrpc_setattrrpc(vp, vap, &stateid, cred, p,
1225 rnap, attrflagp, stuff);
1227 error = nfsrpc_setaclrpc(vp, cred, p, aclp, &stateid,
1229 if (error == NFSERR_OPENMODE && mode == NFSV4OPEN_ACCESSREAD) {
1231 nmp->nm_state |= NFSSTA_OPENMODE;
1234 if (error == NFSERR_STALESTATEID)
1235 nfscl_initiate_recovery(nmp->nm_clp);
1237 nfscl_lockderef(lckp);
1239 (void) nfsrpc_close(vp, 0, p);
1240 if (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
1241 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
1242 error == NFSERR_OLDSTATEID || error == NFSERR_BADSESSION) {
1243 (void) nfs_catnap(PZERO, error, "nfs_setattr");
1244 } else if ((error == NFSERR_EXPIRED ||
1245 error == NFSERR_BADSTATEID) && clidrev != 0) {
1246 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
1249 } while (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
1250 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
1251 error == NFSERR_BADSESSION ||
1252 (error == NFSERR_OLDSTATEID && retrycnt < 20) ||
1253 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
1254 expireret == 0 && clidrev != 0 && retrycnt < 4) ||
1255 (error == NFSERR_OPENMODE && mode == NFSV4OPEN_ACCESSREAD &&
1257 if (error && retrycnt >= 4)
1263 nfsrpc_setattrrpc(vnode_t vp, struct vattr *vap,
1264 nfsv4stateid_t *stateidp, struct ucred *cred, NFSPROC_T *p,
1265 struct nfsvattr *rnap, int *attrflagp, void *stuff)
1268 struct nfsrv_descript nfsd, *nd = &nfsd;
1270 nfsattrbit_t attrbits;
1273 NFSCL_REQSTART(nd, NFSPROC_SETATTR, vp);
1274 if (nd->nd_flag & ND_NFSV4)
1275 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID);
1276 vap->va_type = vnode_vtype(vp);
1277 nfscl_fillsattr(nd, vap, vp, NFSSATTR_FULL, 0);
1278 if (nd->nd_flag & ND_NFSV3) {
1279 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1281 } else if (nd->nd_flag & ND_NFSV4) {
1282 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1283 *tl = txdr_unsigned(NFSV4OP_GETATTR);
1284 NFSGETATTR_ATTRBIT(&attrbits);
1285 (void) nfsrv_putattrbit(nd, &attrbits);
1287 error = nfscl_request(nd, vp, p, cred, stuff);
1290 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4))
1291 error = nfscl_wcc_data(nd, vp, rnap, attrflagp, NULL, stuff);
1292 if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) == ND_NFSV4 && !error)
1293 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
1294 if (!(nd->nd_flag & ND_NFSV3) && !nd->nd_repstat && !error)
1295 error = nfscl_postop_attr(nd, rnap, attrflagp, stuff);
1296 mbuf_freem(nd->nd_mrep);
1297 if (nd->nd_repstat && !error)
1298 error = nd->nd_repstat;
1306 nfsrpc_lookup(vnode_t dvp, char *name, int len, struct ucred *cred,
1307 NFSPROC_T *p, struct nfsvattr *dnap, struct nfsvattr *nap,
1308 struct nfsfh **nfhpp, int *attrflagp, int *dattrflagp, void *stuff)
1311 struct nfsrv_descript nfsd, *nd = &nfsd;
1312 struct nfsmount *nmp;
1315 nfsattrbit_t attrbits;
1316 int error = 0, lookupp = 0;
1320 if (vnode_vtype(dvp) != VDIR)
1322 nmp = VFSTONFS(vnode_mount(dvp));
1323 if (len > NFS_MAXNAMLEN)
1324 return (ENAMETOOLONG);
1325 if (NFSHASNFSV4(nmp) && len == 1 &&
1328 * Just return the current dir's fh.
1331 nfhp = malloc(sizeof (struct nfsfh) +
1332 np->n_fhp->nfh_len, M_NFSFH, M_WAITOK);
1333 nfhp->nfh_len = np->n_fhp->nfh_len;
1334 NFSBCOPY(np->n_fhp->nfh_fh, nfhp->nfh_fh, nfhp->nfh_len);
1338 if (NFSHASNFSV4(nmp) && len == 2 &&
1339 name[0] == '.' && name[1] == '.') {
1341 NFSCL_REQSTART(nd, NFSPROC_LOOKUPP, dvp);
1343 NFSCL_REQSTART(nd, NFSPROC_LOOKUP, dvp);
1344 (void) nfsm_strtom(nd, name, len);
1346 if (nd->nd_flag & ND_NFSV4) {
1347 NFSGETATTR_ATTRBIT(&attrbits);
1348 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1349 *tl++ = txdr_unsigned(NFSV4OP_GETFH);
1350 *tl = txdr_unsigned(NFSV4OP_GETATTR);
1351 (void) nfsrv_putattrbit(nd, &attrbits);
1353 error = nfscl_request(nd, dvp, p, cred, stuff);
1356 if (nd->nd_repstat) {
1358 * When an NFSv4 Lookupp returns ENOENT, it means that
1359 * the lookup is at the root of an fs, so return this dir.
1361 if (nd->nd_repstat == NFSERR_NOENT && lookupp) {
1363 nfhp = malloc(sizeof (struct nfsfh) +
1364 np->n_fhp->nfh_len, M_NFSFH, M_WAITOK);
1365 nfhp->nfh_len = np->n_fhp->nfh_len;
1366 NFSBCOPY(np->n_fhp->nfh_fh, nfhp->nfh_fh, nfhp->nfh_len);
1368 mbuf_freem(nd->nd_mrep);
1371 if (nd->nd_flag & ND_NFSV3)
1372 error = nfscl_postop_attr(nd, dnap, dattrflagp, stuff);
1373 else if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) ==
1375 /* Load the directory attributes. */
1376 error = nfsm_loadattr(nd, dnap);
1382 if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) == ND_NFSV4) {
1383 /* Load the directory attributes. */
1384 error = nfsm_loadattr(nd, dnap);
1388 /* Skip over the Lookup and GetFH operation status values. */
1389 NFSM_DISSECT(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1391 error = nfsm_getfh(nd, nfhpp);
1395 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
1396 if ((nd->nd_flag & ND_NFSV3) && !error)
1397 error = nfscl_postop_attr(nd, dnap, dattrflagp, stuff);
1399 mbuf_freem(nd->nd_mrep);
1400 if (!error && nd->nd_repstat)
1401 error = nd->nd_repstat;
1406 * Do a readlink rpc.
1409 nfsrpc_readlink(vnode_t vp, struct uio *uiop, struct ucred *cred,
1410 NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, void *stuff)
1413 struct nfsrv_descript nfsd, *nd = &nfsd;
1414 struct nfsnode *np = VTONFS(vp);
1415 nfsattrbit_t attrbits;
1416 int error, len, cangetattr = 1;
1419 NFSCL_REQSTART(nd, NFSPROC_READLINK, vp);
1420 if (nd->nd_flag & ND_NFSV4) {
1422 * And do a Getattr op.
1424 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1425 *tl = txdr_unsigned(NFSV4OP_GETATTR);
1426 NFSGETATTR_ATTRBIT(&attrbits);
1427 (void) nfsrv_putattrbit(nd, &attrbits);
1429 error = nfscl_request(nd, vp, p, cred, stuff);
1432 if (nd->nd_flag & ND_NFSV3)
1433 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
1434 if (!nd->nd_repstat && !error) {
1435 NFSM_STRSIZ(len, NFS_MAXPATHLEN);
1437 * This seems weird to me, but must have been added to
1438 * FreeBSD for some reason. The only thing I can think of
1439 * is that there was/is some server that replies with
1440 * more link data than it should?
1442 if (len == NFS_MAXPATHLEN) {
1444 if (np->n_size > 0 && np->n_size < NFS_MAXPATHLEN) {
1450 error = nfsm_mbufuio(nd, uiop, len);
1451 if ((nd->nd_flag & ND_NFSV4) && !error && cangetattr)
1452 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
1454 if (nd->nd_repstat && !error)
1455 error = nd->nd_repstat;
1457 mbuf_freem(nd->nd_mrep);
1465 nfsrpc_read(vnode_t vp, struct uio *uiop, struct ucred *cred,
1466 NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, void *stuff)
1468 int error, expireret = 0, retrycnt;
1469 u_int32_t clidrev = 0;
1470 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
1471 struct nfsnode *np = VTONFS(vp);
1472 struct ucred *newcred;
1473 struct nfsfh *nfhp = NULL;
1474 nfsv4stateid_t stateid;
1477 if (nmp->nm_clp != NULL)
1478 clidrev = nmp->nm_clp->nfsc_clientidrev;
1480 if (NFSHASNFSV4(nmp)) {
1482 newcred = NFSNEWCRED(cred);
1487 if (NFSHASNFSV4(nmp))
1488 (void)nfscl_getstateid(vp, nfhp->nfh_fh, nfhp->nfh_len,
1489 NFSV4OPEN_ACCESSREAD, 0, newcred, p, &stateid,
1491 error = nfsrpc_readrpc(vp, uiop, newcred, &stateid, p, nap,
1493 if (error == NFSERR_OPENMODE) {
1495 nmp->nm_state |= NFSSTA_OPENMODE;
1498 if (error == NFSERR_STALESTATEID)
1499 nfscl_initiate_recovery(nmp->nm_clp);
1501 nfscl_lockderef(lckp);
1502 if (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
1503 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
1504 error == NFSERR_OLDSTATEID || error == NFSERR_BADSESSION) {
1505 (void) nfs_catnap(PZERO, error, "nfs_read");
1506 } else if ((error == NFSERR_EXPIRED ||
1507 error == NFSERR_BADSTATEID) && clidrev != 0) {
1508 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
1511 } while (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
1512 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
1513 error == NFSERR_BADSESSION ||
1514 (error == NFSERR_OLDSTATEID && retrycnt < 20) ||
1515 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
1516 expireret == 0 && clidrev != 0 && retrycnt < 4) ||
1517 (error == NFSERR_OPENMODE && retrycnt < 4));
1518 if (error && retrycnt >= 4)
1520 if (NFSHASNFSV4(nmp))
1521 NFSFREECRED(newcred);
1526 * The actual read RPC.
1529 nfsrpc_readrpc(vnode_t vp, struct uio *uiop, struct ucred *cred,
1530 nfsv4stateid_t *stateidp, NFSPROC_T *p, struct nfsvattr *nap,
1531 int *attrflagp, void *stuff)
1534 int error = 0, len, retlen, tsiz, eof = 0;
1535 struct nfsrv_descript nfsd;
1536 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
1537 struct nfsrv_descript *nd = &nfsd;
1542 tsiz = uio_uio_resid(uiop);
1543 tmp_off = uiop->uio_offset + tsiz;
1545 if (tmp_off > nmp->nm_maxfilesize || tmp_off < uiop->uio_offset) {
1549 rsize = nmp->nm_rsize;
1554 len = (tsiz > rsize) ? rsize : tsiz;
1555 NFSCL_REQSTART(nd, NFSPROC_READ, vp);
1556 if (nd->nd_flag & ND_NFSV4)
1557 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID);
1558 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED * 3);
1559 if (nd->nd_flag & ND_NFSV2) {
1560 *tl++ = txdr_unsigned(uiop->uio_offset);
1561 *tl++ = txdr_unsigned(len);
1564 txdr_hyper(uiop->uio_offset, tl);
1565 *(tl + 2) = txdr_unsigned(len);
1568 * Since I can't do a Getattr for NFSv4 for Write, there
1569 * doesn't seem any point in doing one here, either.
1570 * (See the comment in nfsrpc_writerpc() for more info.)
1572 error = nfscl_request(nd, vp, p, cred, stuff);
1575 if (nd->nd_flag & ND_NFSV3) {
1576 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
1577 } else if (!nd->nd_repstat && (nd->nd_flag & ND_NFSV2)) {
1578 error = nfsm_loadattr(nd, nap);
1582 if (nd->nd_repstat || error) {
1584 error = nd->nd_repstat;
1587 if (nd->nd_flag & ND_NFSV3) {
1588 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1589 eof = fxdr_unsigned(int, *(tl + 1));
1590 } else if (nd->nd_flag & ND_NFSV4) {
1591 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
1592 eof = fxdr_unsigned(int, *tl);
1594 NFSM_STRSIZ(retlen, len);
1595 error = nfsm_mbufuio(nd, uiop, retlen);
1598 mbuf_freem(nd->nd_mrep);
1601 if (!(nd->nd_flag & ND_NFSV2)) {
1602 if (eof || retlen == 0)
1604 } else if (retlen < len)
1609 if (nd->nd_mrep != NULL)
1610 mbuf_freem(nd->nd_mrep);
1615 * nfs write operation
1616 * When called_from_strategy != 0, it should return EIO for an error that
1617 * indicates recovery is in progress, so that the buffer will be left
1618 * dirty and be written back to the server later. If it loops around,
1619 * the recovery thread could get stuck waiting for the buffer and recovery
1620 * will then deadlock.
1623 nfsrpc_write(vnode_t vp, struct uio *uiop, int *iomode, int *must_commit,
1624 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp,
1625 void *stuff, int called_from_strategy)
1627 int error, expireret = 0, retrycnt, nostateid;
1628 u_int32_t clidrev = 0;
1629 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
1630 struct nfsnode *np = VTONFS(vp);
1631 struct ucred *newcred;
1632 struct nfsfh *nfhp = NULL;
1633 nfsv4stateid_t stateid;
1637 if (nmp->nm_clp != NULL)
1638 clidrev = nmp->nm_clp->nfsc_clientidrev;
1640 if (NFSHASNFSV4(nmp)) {
1641 newcred = NFSNEWCRED(cred);
1648 if (NFSHASNFSV4(nmp)) {
1649 (void)nfscl_getstateid(vp, nfhp->nfh_fh, nfhp->nfh_len,
1650 NFSV4OPEN_ACCESSWRITE, 0, newcred, p, &stateid,
1652 if (stateid.other[0] == 0 && stateid.other[1] == 0 &&
1653 stateid.other[2] == 0) {
1655 NFSCL_DEBUG(1, "stateid0 in write\n");
1660 * If there is no stateid for NFSv4, it means this is an
1661 * extraneous write after close. Basically a poorly
1662 * implemented buffer cache. Just don't do the write.
1667 error = nfsrpc_writerpc(vp, uiop, iomode, must_commit,
1668 newcred, &stateid, p, nap, attrflagp, stuff);
1669 if (error == NFSERR_STALESTATEID)
1670 nfscl_initiate_recovery(nmp->nm_clp);
1672 nfscl_lockderef(lckp);
1673 if (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
1674 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
1675 error == NFSERR_OLDSTATEID || error == NFSERR_BADSESSION) {
1676 (void) nfs_catnap(PZERO, error, "nfs_write");
1677 } else if ((error == NFSERR_EXPIRED ||
1678 error == NFSERR_BADSTATEID) && clidrev != 0) {
1679 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
1682 } while (error == NFSERR_GRACE || error == NFSERR_DELAY ||
1683 ((error == NFSERR_STALESTATEID || error == NFSERR_BADSESSION ||
1684 error == NFSERR_STALEDONTRECOVER) && called_from_strategy == 0) ||
1685 (error == NFSERR_OLDSTATEID && retrycnt < 20) ||
1686 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
1687 expireret == 0 && clidrev != 0 && retrycnt < 4));
1688 if (error != 0 && (retrycnt >= 4 ||
1689 ((error == NFSERR_STALESTATEID || error == NFSERR_BADSESSION ||
1690 error == NFSERR_STALEDONTRECOVER) && called_from_strategy != 0)))
1692 if (NFSHASNFSV4(nmp))
1693 NFSFREECRED(newcred);
1698 * The actual write RPC.
1701 nfsrpc_writerpc(vnode_t vp, struct uio *uiop, int *iomode,
1702 int *must_commit, struct ucred *cred, nfsv4stateid_t *stateidp,
1703 NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, void *stuff)
1706 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
1707 struct nfsnode *np = VTONFS(vp);
1708 int error = 0, len, tsiz, rlen, commit, committed = NFSWRITE_FILESYNC;
1709 int wccflag = 0, wsize;
1711 struct nfsrv_descript nfsd;
1712 struct nfsrv_descript *nd = &nfsd;
1713 nfsattrbit_t attrbits;
1716 KASSERT(uiop->uio_iovcnt == 1, ("nfs: writerpc iovcnt > 1"));
1718 tsiz = uio_uio_resid(uiop);
1719 tmp_off = uiop->uio_offset + tsiz;
1721 if (tmp_off > nmp->nm_maxfilesize || tmp_off < uiop->uio_offset) {
1725 wsize = nmp->nm_wsize;
1727 nd->nd_mrep = NULL; /* NFSv2 sometimes does a write with */
1728 nd->nd_repstat = 0; /* uio_resid == 0, so the while is not done */
1731 len = (tsiz > wsize) ? wsize : tsiz;
1732 NFSCL_REQSTART(nd, NFSPROC_WRITE, vp);
1733 if (nd->nd_flag & ND_NFSV4) {
1734 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID);
1735 NFSM_BUILD(tl, u_int32_t *, NFSX_HYPER+2*NFSX_UNSIGNED);
1736 txdr_hyper(uiop->uio_offset, tl);
1738 *tl++ = txdr_unsigned(*iomode);
1739 *tl = txdr_unsigned(len);
1740 } else if (nd->nd_flag & ND_NFSV3) {
1741 NFSM_BUILD(tl, u_int32_t *, NFSX_HYPER+3*NFSX_UNSIGNED);
1742 txdr_hyper(uiop->uio_offset, tl);
1744 *tl++ = txdr_unsigned(len);
1745 *tl++ = txdr_unsigned(*iomode);
1746 *tl = txdr_unsigned(len);
1750 NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1752 * Not sure why someone changed this, since the
1753 * RFC clearly states that "beginoffset" and
1754 * "totalcount" are ignored, but it wouldn't
1755 * surprise me if there's a busted server out there.
1757 /* Set both "begin" and "current" to non-garbage. */
1758 x = txdr_unsigned((u_int32_t)uiop->uio_offset);
1759 *tl++ = x; /* "begin offset" */
1760 *tl++ = x; /* "current offset" */
1761 x = txdr_unsigned(len);
1762 *tl++ = x; /* total to this offset */
1763 *tl = x; /* size of this write */
1766 nfsm_uiombuf(nd, uiop, len);
1768 * Although it is tempting to do a normal Getattr Op in the
1769 * NFSv4 compound, the result can be a nearly hung client
1770 * system if the Getattr asks for Owner and/or OwnerGroup.
1771 * It occurs when the client can't map either the Owner or
1772 * Owner_group name in the Getattr reply to a uid/gid. When
1773 * there is a cache miss, the kernel does an upcall to the
1774 * nfsuserd. Then, it can try and read the local /etc/passwd
1775 * or /etc/group file. It can then block in getnewbuf(),
1776 * waiting for dirty writes to be pushed to the NFS server.
1777 * The only reason this doesn't result in a complete
1778 * deadlock, is that the upcall times out and allows
1779 * the write to complete. However, progress is so slow
1780 * that it might just as well be deadlocked.
1781 * As such, we get the rest of the attributes, but not
1782 * Owner or Owner_group.
1783 * nb: nfscl_loadattrcache() needs to be told that these
1784 * partial attributes from a write rpc are being
1785 * passed in, via a argument flag.
1787 if (nd->nd_flag & ND_NFSV4) {
1788 NFSWRITEGETATTR_ATTRBIT(&attrbits);
1789 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1790 *tl = txdr_unsigned(NFSV4OP_GETATTR);
1791 (void) nfsrv_putattrbit(nd, &attrbits);
1793 error = nfscl_request(nd, vp, p, cred, stuff);
1796 if (nd->nd_repstat) {
1798 * In case the rpc gets retried, roll
1799 * the uio fileds changed by nfsm_uiombuf()
1802 uiop->uio_offset -= len;
1803 uio_uio_resid_add(uiop, len);
1804 uio_iov_base_add(uiop, -len);
1805 uio_iov_len_add(uiop, len);
1807 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) {
1808 error = nfscl_wcc_data(nd, vp, nap, attrflagp,
1813 if (!nd->nd_repstat) {
1814 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) {
1815 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED
1817 rlen = fxdr_unsigned(int, *tl++);
1821 } else if (rlen < len) {
1822 backup = len - rlen;
1823 uio_iov_base_add(uiop, -(backup));
1824 uio_iov_len_add(uiop, backup);
1825 uiop->uio_offset -= backup;
1826 uio_uio_resid_add(uiop, backup);
1829 commit = fxdr_unsigned(int, *tl++);
1832 * Return the lowest commitment level
1833 * obtained by any of the RPCs.
1835 if (committed == NFSWRITE_FILESYNC)
1837 else if (committed == NFSWRITE_DATASYNC &&
1838 commit == NFSWRITE_UNSTABLE)
1841 if (!NFSHASWRITEVERF(nmp)) {
1842 NFSBCOPY((caddr_t)tl,
1843 (caddr_t)&nmp->nm_verf[0],
1845 NFSSETWRITEVERF(nmp);
1846 } else if (NFSBCMP(tl, nmp->nm_verf,
1849 NFSBCOPY(tl, nmp->nm_verf, NFSX_VERF);
1853 if (nd->nd_flag & ND_NFSV4)
1854 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1855 if (nd->nd_flag & (ND_NFSV2 | ND_NFSV4)) {
1856 error = nfsm_loadattr(nd, nap);
1858 *attrflagp = NFS_LATTR_NOSHRINK;
1861 error = nd->nd_repstat;
1865 NFSWRITERPC_SETTIME(wccflag, np, nap, (nd->nd_flag & ND_NFSV4));
1866 mbuf_freem(nd->nd_mrep);
1871 if (nd->nd_mrep != NULL)
1872 mbuf_freem(nd->nd_mrep);
1873 *iomode = committed;
1874 if (nd->nd_repstat && !error)
1875 error = nd->nd_repstat;
1881 * For NFS v2 this is a kludge. Use a create rpc but with the IFMT bits of the
1882 * mode set to specify the file type and the size field for rdev.
1885 nfsrpc_mknod(vnode_t dvp, char *name, int namelen, struct vattr *vap,
1886 u_int32_t rdev, enum vtype vtyp, struct ucred *cred, NFSPROC_T *p,
1887 struct nfsvattr *dnap, struct nfsvattr *nnap, struct nfsfh **nfhpp,
1888 int *attrflagp, int *dattrflagp, void *dstuff)
1892 struct nfsrv_descript nfsd, *nd = &nfsd;
1893 nfsattrbit_t attrbits;
1898 if (namelen > NFS_MAXNAMLEN)
1899 return (ENAMETOOLONG);
1900 NFSCL_REQSTART(nd, NFSPROC_MKNOD, dvp);
1901 if (nd->nd_flag & ND_NFSV4) {
1902 if (vtyp == VBLK || vtyp == VCHR) {
1903 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
1904 *tl++ = vtonfsv34_type(vtyp);
1905 *tl++ = txdr_unsigned(NFSMAJOR(rdev));
1906 *tl = txdr_unsigned(NFSMINOR(rdev));
1908 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1909 *tl = vtonfsv34_type(vtyp);
1912 (void) nfsm_strtom(nd, name, namelen);
1913 if (nd->nd_flag & ND_NFSV3) {
1914 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1915 *tl = vtonfsv34_type(vtyp);
1917 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4))
1918 nfscl_fillsattr(nd, vap, dvp, 0, 0);
1919 if ((nd->nd_flag & ND_NFSV3) &&
1920 (vtyp == VCHR || vtyp == VBLK)) {
1921 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1922 *tl++ = txdr_unsigned(NFSMAJOR(rdev));
1923 *tl = txdr_unsigned(NFSMINOR(rdev));
1925 if (nd->nd_flag & ND_NFSV4) {
1926 NFSGETATTR_ATTRBIT(&attrbits);
1927 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1928 *tl++ = txdr_unsigned(NFSV4OP_GETFH);
1929 *tl = txdr_unsigned(NFSV4OP_GETATTR);
1930 (void) nfsrv_putattrbit(nd, &attrbits);
1932 if (nd->nd_flag & ND_NFSV2)
1933 nfscl_fillsattr(nd, vap, dvp, NFSSATTR_SIZERDEV, rdev);
1934 error = nfscl_request(nd, dvp, p, cred, dstuff);
1937 if (nd->nd_flag & ND_NFSV4)
1938 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
1939 if (!nd->nd_repstat) {
1940 if (nd->nd_flag & ND_NFSV4) {
1941 NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
1942 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
1946 error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp);
1950 if (nd->nd_flag & ND_NFSV3)
1951 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
1952 if (!error && nd->nd_repstat)
1953 error = nd->nd_repstat;
1955 mbuf_freem(nd->nd_mrep);
1960 * nfs file create call
1961 * Mostly just call the approriate routine. (I separated out v4, so that
1962 * error recovery wouldn't be as difficult.)
1965 nfsrpc_create(vnode_t dvp, char *name, int namelen, struct vattr *vap,
1966 nfsquad_t cverf, int fmode, struct ucred *cred, NFSPROC_T *p,
1967 struct nfsvattr *dnap, struct nfsvattr *nnap, struct nfsfh **nfhpp,
1968 int *attrflagp, int *dattrflagp, void *dstuff)
1970 int error = 0, newone, expireret = 0, retrycnt, unlocked;
1971 struct nfsclowner *owp;
1972 struct nfscldeleg *dp;
1973 struct nfsmount *nmp = VFSTONFS(vnode_mount(dvp));
1976 if (NFSHASNFSV4(nmp)) {
1980 error = nfscl_open(dvp, NULL, 0, (NFSV4OPEN_ACCESSWRITE |
1981 NFSV4OPEN_ACCESSREAD), 0, cred, p, &owp, NULL, &newone,
1985 if (nmp->nm_clp != NULL)
1986 clidrev = nmp->nm_clp->nfsc_clientidrev;
1989 if (!NFSHASPNFS(nmp) || nfscl_enablecallb == 0 ||
1990 nfs_numnfscbd == 0 || retrycnt > 0)
1991 error = nfsrpc_createv4(dvp, name, namelen, vap, cverf,
1992 fmode, owp, &dp, cred, p, dnap, nnap, nfhpp,
1993 attrflagp, dattrflagp, dstuff, &unlocked);
1995 error = nfsrpc_getcreatelayout(dvp, name, namelen, vap,
1996 cverf, fmode, owp, &dp, cred, p, dnap, nnap, nfhpp,
1997 attrflagp, dattrflagp, dstuff, &unlocked);
1999 * There is no need to invalidate cached attributes here,
2000 * since new post-delegation issue attributes are always
2001 * returned by nfsrpc_createv4() and these will update the
2005 (void) nfscl_deleg(nmp->nm_mountp, owp->nfsow_clp,
2006 (*nfhpp)->nfh_fh, (*nfhpp)->nfh_len, cred, p, &dp);
2007 nfscl_ownerrelease(nmp, owp, error, newone, unlocked);
2008 if (error == NFSERR_GRACE || error == NFSERR_STALECLIENTID ||
2009 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
2010 error == NFSERR_BADSESSION) {
2011 (void) nfs_catnap(PZERO, error, "nfs_open");
2012 } else if ((error == NFSERR_EXPIRED ||
2013 error == NFSERR_BADSTATEID) && clidrev != 0) {
2014 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
2017 } while (error == NFSERR_GRACE || error == NFSERR_STALECLIENTID ||
2018 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
2019 error == NFSERR_BADSESSION ||
2020 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
2021 expireret == 0 && clidrev != 0 && retrycnt < 4));
2022 if (error && retrycnt >= 4)
2025 error = nfsrpc_createv23(dvp, name, namelen, vap, cverf,
2026 fmode, cred, p, dnap, nnap, nfhpp, attrflagp, dattrflagp,
2033 * The create rpc for v2 and 3.
2036 nfsrpc_createv23(vnode_t dvp, char *name, int namelen, struct vattr *vap,
2037 nfsquad_t cverf, int fmode, struct ucred *cred, NFSPROC_T *p,
2038 struct nfsvattr *dnap, struct nfsvattr *nnap, struct nfsfh **nfhpp,
2039 int *attrflagp, int *dattrflagp, void *dstuff)
2043 struct nfsrv_descript nfsd, *nd = &nfsd;
2048 if (namelen > NFS_MAXNAMLEN)
2049 return (ENAMETOOLONG);
2050 NFSCL_REQSTART(nd, NFSPROC_CREATE, dvp);
2051 (void) nfsm_strtom(nd, name, namelen);
2052 if (nd->nd_flag & ND_NFSV3) {
2053 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2054 if (fmode & O_EXCL) {
2055 *tl = txdr_unsigned(NFSCREATE_EXCLUSIVE);
2056 NFSM_BUILD(tl, u_int32_t *, NFSX_VERF);
2057 *tl++ = cverf.lval[0];
2058 *tl = cverf.lval[1];
2060 *tl = txdr_unsigned(NFSCREATE_UNCHECKED);
2061 nfscl_fillsattr(nd, vap, dvp, 0, 0);
2064 nfscl_fillsattr(nd, vap, dvp, NFSSATTR_SIZE0, 0);
2066 error = nfscl_request(nd, dvp, p, cred, dstuff);
2069 if (nd->nd_repstat == 0) {
2070 error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp);
2074 if (nd->nd_flag & ND_NFSV3)
2075 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
2076 if (nd->nd_repstat != 0 && error == 0)
2077 error = nd->nd_repstat;
2079 mbuf_freem(nd->nd_mrep);
2084 nfsrpc_createv4(vnode_t dvp, char *name, int namelen, struct vattr *vap,
2085 nfsquad_t cverf, int fmode, struct nfsclowner *owp, struct nfscldeleg **dpp,
2086 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap,
2087 struct nfsvattr *nnap, struct nfsfh **nfhpp, int *attrflagp,
2088 int *dattrflagp, void *dstuff, int *unlockedp)
2091 int error = 0, deleg, newone, ret, acesize, limitby;
2092 struct nfsrv_descript nfsd, *nd = &nfsd;
2093 struct nfsclopen *op;
2094 struct nfscldeleg *dp = NULL;
2097 nfsattrbit_t attrbits;
2098 nfsv4stateid_t stateid;
2100 struct nfsmount *nmp;
2101 struct nfsclsession *tsep;
2103 nmp = VFSTONFS(dvp->v_mount);
2110 if (namelen > NFS_MAXNAMLEN)
2111 return (ENAMETOOLONG);
2112 NFSCL_REQSTART(nd, NFSPROC_CREATE, dvp);
2114 * For V4, this is actually an Open op.
2116 NFSM_BUILD(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
2117 *tl++ = txdr_unsigned(owp->nfsow_seqid);
2118 *tl++ = txdr_unsigned(NFSV4OPEN_ACCESSWRITE |
2119 NFSV4OPEN_ACCESSREAD);
2120 *tl++ = txdr_unsigned(NFSV4OPEN_DENYNONE);
2121 tsep = nfsmnt_mdssession(nmp);
2122 *tl++ = tsep->nfsess_clientid.lval[0];
2123 *tl = tsep->nfsess_clientid.lval[1];
2124 (void) nfsm_strtom(nd, owp->nfsow_owner, NFSV4CL_LOCKNAMELEN);
2125 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2126 *tl++ = txdr_unsigned(NFSV4OPEN_CREATE);
2127 if (fmode & O_EXCL) {
2128 if (NFSHASNFSV4N(nmp)) {
2129 if (NFSHASSESSPERSIST(nmp)) {
2130 /* Use GUARDED for persistent sessions. */
2131 *tl = txdr_unsigned(NFSCREATE_GUARDED);
2132 nfscl_fillsattr(nd, vap, dvp, 0, 0);
2134 /* Otherwise, use EXCLUSIVE4_1. */
2135 *tl = txdr_unsigned(NFSCREATE_EXCLUSIVE41);
2136 NFSM_BUILD(tl, u_int32_t *, NFSX_VERF);
2137 *tl++ = cverf.lval[0];
2138 *tl = cverf.lval[1];
2139 nfscl_fillsattr(nd, vap, dvp, 0, 0);
2143 *tl = txdr_unsigned(NFSCREATE_EXCLUSIVE);
2144 NFSM_BUILD(tl, u_int32_t *, NFSX_VERF);
2145 *tl++ = cverf.lval[0];
2146 *tl = cverf.lval[1];
2149 *tl = txdr_unsigned(NFSCREATE_UNCHECKED);
2150 nfscl_fillsattr(nd, vap, dvp, 0, 0);
2152 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2153 *tl = txdr_unsigned(NFSV4OPEN_CLAIMNULL);
2154 (void) nfsm_strtom(nd, name, namelen);
2155 /* Get the new file's handle and attributes. */
2156 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2157 *tl++ = txdr_unsigned(NFSV4OP_GETFH);
2158 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2159 NFSGETATTR_ATTRBIT(&attrbits);
2160 (void) nfsrv_putattrbit(nd, &attrbits);
2161 /* Get the directory's post-op attributes. */
2162 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2163 *tl = txdr_unsigned(NFSV4OP_PUTFH);
2164 (void) nfsm_fhtom(nd, np->n_fhp->nfh_fh, np->n_fhp->nfh_len, 0);
2165 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2166 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2167 (void) nfsrv_putattrbit(nd, &attrbits);
2168 error = nfscl_request(nd, dvp, p, cred, dstuff);
2171 NFSCL_INCRSEQID(owp->nfsow_seqid, nd);
2172 if (nd->nd_repstat == 0) {
2173 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
2175 stateid.seqid = *tl++;
2176 stateid.other[0] = *tl++;
2177 stateid.other[1] = *tl++;
2178 stateid.other[2] = *tl;
2179 rflags = fxdr_unsigned(u_int32_t, *(tl + 6));
2180 (void) nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
2181 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2182 deleg = fxdr_unsigned(int, *tl);
2183 if (deleg == NFSV4OPEN_DELEGATEREAD ||
2184 deleg == NFSV4OPEN_DELEGATEWRITE) {
2185 if (!(owp->nfsow_clp->nfsc_flags &
2186 NFSCLFLAGS_FIRSTDELEG))
2187 owp->nfsow_clp->nfsc_flags |=
2188 (NFSCLFLAGS_FIRSTDELEG | NFSCLFLAGS_GOTDELEG);
2190 sizeof (struct nfscldeleg) + NFSX_V4FHMAX,
2191 M_NFSCLDELEG, M_WAITOK);
2192 LIST_INIT(&dp->nfsdl_owner);
2193 LIST_INIT(&dp->nfsdl_lock);
2194 dp->nfsdl_clp = owp->nfsow_clp;
2195 newnfs_copyincred(cred, &dp->nfsdl_cred);
2196 nfscl_lockinit(&dp->nfsdl_rwlock);
2197 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
2199 dp->nfsdl_stateid.seqid = *tl++;
2200 dp->nfsdl_stateid.other[0] = *tl++;
2201 dp->nfsdl_stateid.other[1] = *tl++;
2202 dp->nfsdl_stateid.other[2] = *tl++;
2203 ret = fxdr_unsigned(int, *tl);
2204 if (deleg == NFSV4OPEN_DELEGATEWRITE) {
2205 dp->nfsdl_flags = NFSCLDL_WRITE;
2207 * Indicates how much the file can grow.
2209 NFSM_DISSECT(tl, u_int32_t *,
2211 limitby = fxdr_unsigned(int, *tl++);
2213 case NFSV4OPEN_LIMITSIZE:
2214 dp->nfsdl_sizelimit = fxdr_hyper(tl);
2216 case NFSV4OPEN_LIMITBLOCKS:
2217 dp->nfsdl_sizelimit =
2218 fxdr_unsigned(u_int64_t, *tl++);
2219 dp->nfsdl_sizelimit *=
2220 fxdr_unsigned(u_int64_t, *tl);
2223 error = NFSERR_BADXDR;
2227 dp->nfsdl_flags = NFSCLDL_READ;
2230 dp->nfsdl_flags |= NFSCLDL_RECALL;
2231 error = nfsrv_dissectace(nd, &dp->nfsdl_ace, &ret,
2235 } else if (deleg != NFSV4OPEN_DELEGATENONE) {
2236 error = NFSERR_BADXDR;
2239 error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp);
2242 /* Get rid of the PutFH and Getattr status values. */
2243 NFSM_DISSECT(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
2244 /* Load the directory attributes. */
2245 error = nfsm_loadattr(nd, dnap);
2249 if (dp != NULL && *attrflagp) {
2250 dp->nfsdl_change = nnap->na_filerev;
2251 dp->nfsdl_modtime = nnap->na_mtime;
2252 dp->nfsdl_flags |= NFSCLDL_MODTIMESET;
2255 * We can now complete the Open state.
2259 dp->nfsdl_fhlen = nfhp->nfh_len;
2260 NFSBCOPY(nfhp->nfh_fh, dp->nfsdl_fh, nfhp->nfh_len);
2263 * Get an Open structure that will be
2264 * attached to the OpenOwner, acquired already.
2266 error = nfscl_open(dvp, nfhp->nfh_fh, nfhp->nfh_len,
2267 (NFSV4OPEN_ACCESSWRITE | NFSV4OPEN_ACCESSREAD), 0,
2268 cred, p, NULL, &op, &newone, NULL, 0);
2271 op->nfso_stateid = stateid;
2272 newnfs_copyincred(cred, &op->nfso_cred);
2273 if ((rflags & NFSV4OPEN_RESULTCONFIRM)) {
2275 ret = nfsrpc_openconfirm(dvp, nfhp->nfh_fh,
2276 nfhp->nfh_len, op, cred, p);
2277 if (ret == NFSERR_DELAY)
2278 (void) nfs_catnap(PZERO, ret, "nfs_create");
2279 } while (ret == NFSERR_DELAY);
2284 * If the server is handing out delegations, but we didn't
2285 * get one because an OpenConfirm was required, try the
2286 * Open again, to get a delegation. This is a harmless no-op,
2287 * from a server's point of view.
2289 if ((rflags & NFSV4OPEN_RESULTCONFIRM) &&
2290 (owp->nfsow_clp->nfsc_flags & NFSCLFLAGS_GOTDELEG) &&
2291 !error && dp == NULL) {
2293 ret = nfsrpc_openrpc(VFSTONFS(vnode_mount(dvp)), dvp,
2294 np->n_fhp->nfh_fh, np->n_fhp->nfh_len,
2295 nfhp->nfh_fh, nfhp->nfh_len,
2296 (NFSV4OPEN_ACCESSWRITE | NFSV4OPEN_ACCESSREAD), op,
2297 name, namelen, &dp, 0, 0x0, cred, p, 0, 1);
2298 if (ret == NFSERR_DELAY)
2299 (void) nfs_catnap(PZERO, ret, "nfs_crt2");
2300 } while (ret == NFSERR_DELAY);
2303 free(dp, M_NFSCLDELEG);
2306 if (ret == NFSERR_STALECLIENTID ||
2307 ret == NFSERR_STALEDONTRECOVER ||
2308 ret == NFSERR_BADSESSION)
2312 nfscl_openrelease(nmp, op, error, newone);
2315 if (nd->nd_repstat != 0 && error == 0)
2316 error = nd->nd_repstat;
2317 if (error == NFSERR_STALECLIENTID)
2318 nfscl_initiate_recovery(owp->nfsow_clp);
2322 else if (dp != NULL)
2323 free(dp, M_NFSCLDELEG);
2324 mbuf_freem(nd->nd_mrep);
2332 nfsrpc_remove(vnode_t dvp, char *name, int namelen, vnode_t vp,
2333 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap, int *dattrflagp,
2337 struct nfsrv_descript nfsd, *nd = &nfsd;
2339 struct nfsmount *nmp;
2340 nfsv4stateid_t dstateid;
2341 int error, ret = 0, i;
2344 if (namelen > NFS_MAXNAMLEN)
2345 return (ENAMETOOLONG);
2346 nmp = VFSTONFS(vnode_mount(dvp));
2348 if (NFSHASNFSV4(nmp) && ret == 0) {
2349 ret = nfscl_removedeleg(vp, p, &dstateid);
2351 NFSCL_REQSTART(nd, NFSPROC_RETDELEGREMOVE, vp);
2352 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID +
2354 if (NFSHASNFSV4N(nmp))
2357 *tl++ = dstateid.seqid;
2358 *tl++ = dstateid.other[0];
2359 *tl++ = dstateid.other[1];
2360 *tl++ = dstateid.other[2];
2361 *tl = txdr_unsigned(NFSV4OP_PUTFH);
2363 (void) nfsm_fhtom(nd, np->n_fhp->nfh_fh,
2364 np->n_fhp->nfh_len, 0);
2365 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2366 *tl = txdr_unsigned(NFSV4OP_REMOVE);
2372 NFSCL_REQSTART(nd, NFSPROC_REMOVE, dvp);
2373 (void) nfsm_strtom(nd, name, namelen);
2374 error = nfscl_request(nd, dvp, p, cred, dstuff);
2377 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) {
2378 /* For NFSv4, parse out any Delereturn replies. */
2379 if (ret > 0 && nd->nd_repstat != 0 &&
2380 (nd->nd_flag & ND_NOMOREDATA)) {
2382 * If the Delegreturn failed, try again without
2383 * it. The server will Recall, as required.
2385 mbuf_freem(nd->nd_mrep);
2388 for (i = 0; i < (ret * 2); i++) {
2389 if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) ==
2391 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2393 nd->nd_flag |= ND_NOMOREDATA;
2396 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
2398 if (nd->nd_repstat && !error)
2399 error = nd->nd_repstat;
2401 mbuf_freem(nd->nd_mrep);
2406 * Do an nfs rename rpc.
2409 nfsrpc_rename(vnode_t fdvp, vnode_t fvp, char *fnameptr, int fnamelen,
2410 vnode_t tdvp, vnode_t tvp, char *tnameptr, int tnamelen, struct ucred *cred,
2411 NFSPROC_T *p, struct nfsvattr *fnap, struct nfsvattr *tnap,
2412 int *fattrflagp, int *tattrflagp, void *fstuff, void *tstuff)
2415 struct nfsrv_descript nfsd, *nd = &nfsd;
2416 struct nfsmount *nmp;
2418 nfsattrbit_t attrbits;
2419 nfsv4stateid_t fdstateid, tdstateid;
2420 int error = 0, ret = 0, gottd = 0, gotfd = 0, i;
2424 nmp = VFSTONFS(vnode_mount(fdvp));
2425 if (fnamelen > NFS_MAXNAMLEN || tnamelen > NFS_MAXNAMLEN)
2426 return (ENAMETOOLONG);
2428 if (NFSHASNFSV4(nmp) && ret == 0) {
2429 ret = nfscl_renamedeleg(fvp, &fdstateid, &gotfd, tvp,
2430 &tdstateid, &gottd, p);
2431 if (gotfd && gottd) {
2432 NFSCL_REQSTART(nd, NFSPROC_RETDELEGRENAME2, fvp);
2434 NFSCL_REQSTART(nd, NFSPROC_RETDELEGRENAME1, fvp);
2436 NFSCL_REQSTART(nd, NFSPROC_RETDELEGRENAME1, tvp);
2439 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID);
2440 if (NFSHASNFSV4N(nmp))
2443 *tl++ = fdstateid.seqid;
2444 *tl++ = fdstateid.other[0];
2445 *tl++ = fdstateid.other[1];
2446 *tl = fdstateid.other[2];
2448 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2449 *tl = txdr_unsigned(NFSV4OP_PUTFH);
2451 (void) nfsm_fhtom(nd, np->n_fhp->nfh_fh,
2452 np->n_fhp->nfh_len, 0);
2453 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2454 *tl = txdr_unsigned(NFSV4OP_DELEGRETURN);
2458 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID);
2459 if (NFSHASNFSV4N(nmp))
2462 *tl++ = tdstateid.seqid;
2463 *tl++ = tdstateid.other[0];
2464 *tl++ = tdstateid.other[1];
2465 *tl = tdstateid.other[2];
2468 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2469 *tl = txdr_unsigned(NFSV4OP_PUTFH);
2471 (void) nfsm_fhtom(nd, np->n_fhp->nfh_fh,
2472 np->n_fhp->nfh_len, 0);
2473 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2474 *tl = txdr_unsigned(NFSV4OP_SAVEFH);
2480 NFSCL_REQSTART(nd, NFSPROC_RENAME, fdvp);
2481 if (nd->nd_flag & ND_NFSV4) {
2482 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2483 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2484 NFSWCCATTR_ATTRBIT(&attrbits);
2485 (void) nfsrv_putattrbit(nd, &attrbits);
2486 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2487 *tl = txdr_unsigned(NFSV4OP_PUTFH);
2488 (void) nfsm_fhtom(nd, VTONFS(tdvp)->n_fhp->nfh_fh,
2489 VTONFS(tdvp)->n_fhp->nfh_len, 0);
2490 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2491 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2492 (void) nfsrv_putattrbit(nd, &attrbits);
2493 nd->nd_flag |= ND_V4WCCATTR;
2494 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2495 *tl = txdr_unsigned(NFSV4OP_RENAME);
2497 (void) nfsm_strtom(nd, fnameptr, fnamelen);
2498 if (!(nd->nd_flag & ND_NFSV4))
2499 (void) nfsm_fhtom(nd, VTONFS(tdvp)->n_fhp->nfh_fh,
2500 VTONFS(tdvp)->n_fhp->nfh_len, 0);
2501 (void) nfsm_strtom(nd, tnameptr, tnamelen);
2502 error = nfscl_request(nd, fdvp, p, cred, fstuff);
2505 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) {
2506 /* For NFSv4, parse out any Delereturn replies. */
2507 if (ret > 0 && nd->nd_repstat != 0 &&
2508 (nd->nd_flag & ND_NOMOREDATA)) {
2510 * If the Delegreturn failed, try again without
2511 * it. The server will Recall, as required.
2513 mbuf_freem(nd->nd_mrep);
2516 for (i = 0; i < (ret * 2); i++) {
2517 if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) ==
2519 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2521 if (i == 0 && ret > 1) {
2523 * If the Delegreturn failed, try again
2524 * without it. The server will Recall, as
2526 * If ret > 1, the first iteration of this
2527 * loop is the second DelegReturn result.
2529 mbuf_freem(nd->nd_mrep);
2532 nd->nd_flag |= ND_NOMOREDATA;
2537 /* Now, the first wcc attribute reply. */
2538 if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) == ND_NFSV4) {
2539 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2541 nd->nd_flag |= ND_NOMOREDATA;
2543 error = nfscl_wcc_data(nd, fdvp, fnap, fattrflagp, NULL,
2545 /* and the second wcc attribute reply. */
2546 if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) == ND_NFSV4 &&
2548 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2550 nd->nd_flag |= ND_NOMOREDATA;
2553 error = nfscl_wcc_data(nd, tdvp, tnap, tattrflagp,
2556 if (nd->nd_repstat && !error)
2557 error = nd->nd_repstat;
2559 mbuf_freem(nd->nd_mrep);
2564 * nfs hard link create rpc
2567 nfsrpc_link(vnode_t dvp, vnode_t vp, char *name, int namelen,
2568 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap,
2569 struct nfsvattr *nap, int *attrflagp, int *dattrflagp, void *dstuff)
2572 struct nfsrv_descript nfsd, *nd = &nfsd;
2573 nfsattrbit_t attrbits;
2578 if (namelen > NFS_MAXNAMLEN)
2579 return (ENAMETOOLONG);
2580 NFSCL_REQSTART(nd, NFSPROC_LINK, vp);
2581 if (nd->nd_flag & ND_NFSV4) {
2582 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2583 *tl = txdr_unsigned(NFSV4OP_PUTFH);
2585 (void) nfsm_fhtom(nd, VTONFS(dvp)->n_fhp->nfh_fh,
2586 VTONFS(dvp)->n_fhp->nfh_len, 0);
2587 if (nd->nd_flag & ND_NFSV4) {
2588 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2589 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2590 NFSWCCATTR_ATTRBIT(&attrbits);
2591 (void) nfsrv_putattrbit(nd, &attrbits);
2592 nd->nd_flag |= ND_V4WCCATTR;
2593 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2594 *tl = txdr_unsigned(NFSV4OP_LINK);
2596 (void) nfsm_strtom(nd, name, namelen);
2597 error = nfscl_request(nd, vp, p, cred, dstuff);
2600 if (nd->nd_flag & ND_NFSV3) {
2601 error = nfscl_postop_attr(nd, nap, attrflagp, dstuff);
2603 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp,
2605 } else if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) == ND_NFSV4) {
2607 * First, parse out the PutFH and Getattr result.
2609 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2611 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2613 nd->nd_flag |= ND_NOMOREDATA;
2615 * Get the pre-op attributes.
2617 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
2619 if (nd->nd_repstat && !error)
2620 error = nd->nd_repstat;
2622 mbuf_freem(nd->nd_mrep);
2627 * nfs symbolic link create rpc
2630 nfsrpc_symlink(vnode_t dvp, char *name, int namelen, char *target,
2631 struct vattr *vap, struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap,
2632 struct nfsvattr *nnap, struct nfsfh **nfhpp, int *attrflagp,
2633 int *dattrflagp, void *dstuff)
2636 struct nfsrv_descript nfsd, *nd = &nfsd;
2637 struct nfsmount *nmp;
2638 int slen, error = 0;
2643 nmp = VFSTONFS(vnode_mount(dvp));
2644 slen = strlen(target);
2645 if (slen > NFS_MAXPATHLEN || namelen > NFS_MAXNAMLEN)
2646 return (ENAMETOOLONG);
2647 NFSCL_REQSTART(nd, NFSPROC_SYMLINK, dvp);
2648 if (nd->nd_flag & ND_NFSV4) {
2649 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2650 *tl = txdr_unsigned(NFLNK);
2651 (void) nfsm_strtom(nd, target, slen);
2653 (void) nfsm_strtom(nd, name, namelen);
2654 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4))
2655 nfscl_fillsattr(nd, vap, dvp, 0, 0);
2656 if (!(nd->nd_flag & ND_NFSV4))
2657 (void) nfsm_strtom(nd, target, slen);
2658 if (nd->nd_flag & ND_NFSV2)
2659 nfscl_fillsattr(nd, vap, dvp, NFSSATTR_SIZENEG1, 0);
2660 error = nfscl_request(nd, dvp, p, cred, dstuff);
2663 if (nd->nd_flag & ND_NFSV4)
2664 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
2665 if ((nd->nd_flag & ND_NFSV3) && !error) {
2666 if (!nd->nd_repstat)
2667 error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp);
2669 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp,
2672 if (nd->nd_repstat && !error)
2673 error = nd->nd_repstat;
2674 mbuf_freem(nd->nd_mrep);
2676 * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry.
2677 * Only do this if vfs.nfs.ignore_eexist is set.
2678 * Never do this for NFSv4.1 or later minor versions, since sessions
2679 * should guarantee "exactly once" RPC semantics.
2681 if (error == EEXIST && nfsignore_eexist != 0 && (!NFSHASNFSV4(nmp) ||
2682 nmp->nm_minorvers == 0))
2691 nfsrpc_mkdir(vnode_t dvp, char *name, int namelen, struct vattr *vap,
2692 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap,
2693 struct nfsvattr *nnap, struct nfsfh **nfhpp, int *attrflagp,
2694 int *dattrflagp, void *dstuff)
2697 struct nfsrv_descript nfsd, *nd = &nfsd;
2698 nfsattrbit_t attrbits;
2701 struct nfsmount *nmp;
2706 nmp = VFSTONFS(vnode_mount(dvp));
2707 fhp = VTONFS(dvp)->n_fhp;
2708 if (namelen > NFS_MAXNAMLEN)
2709 return (ENAMETOOLONG);
2710 NFSCL_REQSTART(nd, NFSPROC_MKDIR, dvp);
2711 if (nd->nd_flag & ND_NFSV4) {
2712 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2713 *tl = txdr_unsigned(NFDIR);
2715 (void) nfsm_strtom(nd, name, namelen);
2716 nfscl_fillsattr(nd, vap, dvp, NFSSATTR_SIZENEG1, 0);
2717 if (nd->nd_flag & ND_NFSV4) {
2718 NFSGETATTR_ATTRBIT(&attrbits);
2719 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2720 *tl++ = txdr_unsigned(NFSV4OP_GETFH);
2721 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2722 (void) nfsrv_putattrbit(nd, &attrbits);
2723 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2724 *tl = txdr_unsigned(NFSV4OP_PUTFH);
2725 (void) nfsm_fhtom(nd, fhp->nfh_fh, fhp->nfh_len, 0);
2726 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2727 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2728 (void) nfsrv_putattrbit(nd, &attrbits);
2730 error = nfscl_request(nd, dvp, p, cred, dstuff);
2733 if (nd->nd_flag & ND_NFSV4)
2734 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
2735 if (!nd->nd_repstat && !error) {
2736 if (nd->nd_flag & ND_NFSV4) {
2737 NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
2738 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
2741 error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp);
2742 if (error == 0 && (nd->nd_flag & ND_NFSV4) != 0) {
2743 /* Get rid of the PutFH and Getattr status values. */
2744 NFSM_DISSECT(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
2745 /* Load the directory attributes. */
2746 error = nfsm_loadattr(nd, dnap);
2751 if ((nd->nd_flag & ND_NFSV3) && !error)
2752 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
2753 if (nd->nd_repstat && !error)
2754 error = nd->nd_repstat;
2756 mbuf_freem(nd->nd_mrep);
2758 * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry.
2759 * Only do this if vfs.nfs.ignore_eexist is set.
2760 * Never do this for NFSv4.1 or later minor versions, since sessions
2761 * should guarantee "exactly once" RPC semantics.
2763 if (error == EEXIST && nfsignore_eexist != 0 && (!NFSHASNFSV4(nmp) ||
2764 nmp->nm_minorvers == 0))
2770 * nfs remove directory call
2773 nfsrpc_rmdir(vnode_t dvp, char *name, int namelen, struct ucred *cred,
2774 NFSPROC_T *p, struct nfsvattr *dnap, int *dattrflagp, void *dstuff)
2776 struct nfsrv_descript nfsd, *nd = &nfsd;
2780 if (namelen > NFS_MAXNAMLEN)
2781 return (ENAMETOOLONG);
2782 NFSCL_REQSTART(nd, NFSPROC_RMDIR, dvp);
2783 (void) nfsm_strtom(nd, name, namelen);
2784 error = nfscl_request(nd, dvp, p, cred, dstuff);
2787 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4))
2788 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
2789 if (nd->nd_repstat && !error)
2790 error = nd->nd_repstat;
2791 mbuf_freem(nd->nd_mrep);
2793 * Kludge: Map ENOENT => 0 assuming that you have a reply to a retry.
2795 if (error == ENOENT)
2802 * Always returns with either uio_resid unchanged, if you are at the
2803 * end of the directory, or uio_resid == 0, with all DIRBLKSIZ chunks
2805 * I felt this would allow caching of directory blocks more easily
2806 * than returning a pertially filled block.
2807 * Directory offset cookies:
2808 * Oh my, what to do with them...
2809 * I can think of three ways to deal with them:
2810 * 1 - have the layer above these RPCs maintain a map between logical
2811 * directory byte offsets and the NFS directory offset cookies
2812 * 2 - pass the opaque directory offset cookies up into userland
2813 * and let the libc functions deal with them, via the system call
2814 * 3 - return them to userland in the "struct dirent", so future versions
2815 * of libc can use them and do whatever is necessary to make things work
2816 * above these rpc calls, in the meantime
2817 * For now, I do #3 by "hiding" the directory offset cookies after the
2818 * d_name field in struct dirent. This is space inside d_reclen that
2819 * will be ignored by anything that doesn't know about them.
2820 * The directory offset cookies are filled in as the last 8 bytes of
2821 * each directory entry, after d_name. Someday, the userland libc
2822 * functions may be able to use these. In the meantime, it satisfies
2823 * OpenBSD's requirements for cookies being returned.
2824 * If expects the directory offset cookie for the read to be in uio_offset
2825 * and returns the one for the next entry after this directory block in
2829 nfsrpc_readdir(vnode_t vp, struct uio *uiop, nfsuint64 *cookiep,
2830 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp,
2831 int *eofp, void *stuff)
2834 struct dirent *dp = NULL;
2836 nfsquad_t cookie, ncookie;
2837 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
2838 struct nfsnode *dnp = VTONFS(vp);
2839 struct nfsvattr nfsva;
2840 struct nfsrv_descript nfsd, *nd = &nfsd;
2841 int error = 0, tlen, more_dirs = 1, blksiz = 0, bigenough = 1;
2842 int reqsize, tryformoredirs = 1, readsize, eof = 0, gotmnton = 0;
2843 u_int64_t dotfileid, dotdotfileid = 0, fakefileno = UINT64_MAX;
2845 nfsattrbit_t attrbits, dattrbits;
2846 u_int32_t rderr, *tl2 = NULL;
2849 KASSERT(uiop->uio_iovcnt == 1 &&
2850 (uio_uio_resid(uiop) & (DIRBLKSIZ - 1)) == 0,
2851 ("nfs readdirrpc bad uio"));
2852 ncookie.lval[0] = ncookie.lval[1] = 0;
2854 * There is no point in reading a lot more than uio_resid, however
2855 * adding one additional DIRBLKSIZ makes sense. Since uio_resid
2856 * and nm_readdirsize are both exact multiples of DIRBLKSIZ, this
2857 * will never make readsize > nm_readdirsize.
2859 readsize = nmp->nm_readdirsize;
2860 if (readsize > uio_uio_resid(uiop))
2861 readsize = uio_uio_resid(uiop) + DIRBLKSIZ;
2866 tresid = uio_uio_resid(uiop);
2867 cookie.lval[0] = cookiep->nfsuquad[0];
2868 cookie.lval[1] = cookiep->nfsuquad[1];
2872 * For NFSv4, first create the "." and ".." entries.
2874 if (NFSHASNFSV4(nmp)) {
2875 reqsize = 6 * NFSX_UNSIGNED;
2876 NFSGETATTR_ATTRBIT(&dattrbits);
2877 NFSZERO_ATTRBIT(&attrbits);
2878 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_FILEID);
2879 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TYPE);
2880 if (NFSISSET_ATTRBIT(&dnp->n_vattr.na_suppattr,
2881 NFSATTRBIT_MOUNTEDONFILEID)) {
2882 NFSSETBIT_ATTRBIT(&attrbits,
2883 NFSATTRBIT_MOUNTEDONFILEID);
2887 * Must fake it. Use the fileno, except when the
2888 * fsid is != to that of the directory. For that
2889 * case, generate a fake fileno that is not the same.
2891 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_FSID);
2896 * Joy, oh joy. For V4 we get to hand craft '.' and '..'.
2898 if (uiop->uio_offset == 0) {
2899 NFSCL_REQSTART(nd, NFSPROC_LOOKUPP, vp);
2900 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2901 *tl++ = txdr_unsigned(NFSV4OP_GETFH);
2902 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2903 (void) nfsrv_putattrbit(nd, &attrbits);
2904 error = nfscl_request(nd, vp, p, cred, stuff);
2907 dotfileid = 0; /* Fake out the compiler. */
2908 if ((nd->nd_flag & ND_NOMOREDATA) == 0) {
2909 error = nfsm_loadattr(nd, &nfsva);
2912 dotfileid = nfsva.na_fileid;
2914 if (nd->nd_repstat == 0) {
2915 NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
2916 len = fxdr_unsigned(int, *(tl + 4));
2917 if (len > 0 && len <= NFSX_V4FHMAX)
2918 error = nfsm_advance(nd, NFSM_RNDUP(len), -1);
2922 NFSM_DISSECT(tl, u_int32_t *, 2*NFSX_UNSIGNED);
2923 nfsva.na_mntonfileno = UINT64_MAX;
2924 error = nfsv4_loadattr(nd, NULL, &nfsva, NULL,
2925 NULL, 0, NULL, NULL, NULL, NULL, NULL, 0,
2926 NULL, NULL, NULL, p, cred);
2928 dotdotfileid = dotfileid;
2929 } else if (gotmnton) {
2930 if (nfsva.na_mntonfileno != UINT64_MAX)
2931 dotdotfileid = nfsva.na_mntonfileno;
2933 dotdotfileid = nfsva.na_fileid;
2934 } else if (nfsva.na_filesid[0] ==
2935 dnp->n_vattr.na_filesid[0] &&
2936 nfsva.na_filesid[1] ==
2937 dnp->n_vattr.na_filesid[1]) {
2938 dotdotfileid = nfsva.na_fileid;
2942 } while (fakefileno ==
2944 dotdotfileid = fakefileno;
2947 } else if (nd->nd_repstat == NFSERR_NOENT) {
2949 * Lookupp returns NFSERR_NOENT when we are
2950 * at the root, so just use the current dir.
2953 dotdotfileid = dotfileid;
2955 error = nd->nd_repstat;
2957 mbuf_freem(nd->nd_mrep);
2961 dp = (struct dirent *)uio_iov_base(uiop);
2963 dp->d_type = DT_DIR;
2964 dp->d_fileno = dotfileid;
2966 *((uint64_t *)dp->d_name) = 0; /* Zero pad it. */
2967 dp->d_name[0] = '.';
2968 dp->d_reclen = _GENERIC_DIRSIZ(dp) + NFSX_HYPER;
2970 * Just make these offset cookie 0.
2972 tl = (u_int32_t *)&dp->d_name[8];
2975 blksiz += dp->d_reclen;
2976 uio_uio_resid_add(uiop, -(dp->d_reclen));
2977 uiop->uio_offset += dp->d_reclen;
2978 uio_iov_base_add(uiop, dp->d_reclen);
2979 uio_iov_len_add(uiop, -(dp->d_reclen));
2980 dp = (struct dirent *)uio_iov_base(uiop);
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 dp->d_reclen += left;
3095 uio_iov_base_add(uiop, left);
3096 uio_iov_len_add(uiop, -(left));
3097 uio_uio_resid_add(uiop, -(left));
3098 uiop->uio_offset += left;
3101 if (_GENERIC_DIRLEN(len) + NFSX_HYPER >
3102 uio_uio_resid(uiop))
3105 dp = (struct dirent *)uio_iov_base(uiop);
3108 dp->d_reclen = _GENERIC_DIRLEN(len) +
3110 dp->d_type = DT_UNKNOWN;
3111 blksiz += dp->d_reclen;
3112 if (blksiz == DIRBLKSIZ)
3114 uio_uio_resid_add(uiop, -(DIRHDSIZ));
3115 uiop->uio_offset += DIRHDSIZ;
3116 uio_iov_base_add(uiop, DIRHDSIZ);
3117 uio_iov_len_add(uiop, -(DIRHDSIZ));
3118 error = nfsm_mbufuio(nd, uiop, len);
3121 cp = uio_iov_base(uiop);
3123 *cp = '\0'; /* null terminate */
3124 cp += tlen; /* points to cookie storage */
3125 tl2 = (u_int32_t *)cp;
3126 uio_iov_base_add(uiop, (tlen + NFSX_HYPER));
3127 uio_iov_len_add(uiop, -(tlen + NFSX_HYPER));
3128 uio_uio_resid_add(uiop, -(tlen + NFSX_HYPER));
3129 uiop->uio_offset += (tlen + NFSX_HYPER);
3131 error = nfsm_advance(nd, NFSM_RNDUP(len), -1);
3135 if (nd->nd_flag & ND_NFSV4) {
3137 nfsva.na_mntonfileno = UINT64_MAX;
3138 error = nfsv4_loadattr(nd, NULL, &nfsva, NULL,
3139 NULL, 0, NULL, NULL, NULL, NULL, NULL, 0,
3140 NULL, NULL, &rderr, p, cred);
3143 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3144 } else if (nd->nd_flag & ND_NFSV3) {
3145 NFSM_DISSECT(tl, u_int32_t *, 3*NFSX_UNSIGNED);
3146 ncookie.lval[0] = *tl++;
3147 ncookie.lval[1] = *tl++;
3149 NFSM_DISSECT(tl, u_int32_t *, 2*NFSX_UNSIGNED);
3150 ncookie.lval[0] = 0;
3151 ncookie.lval[1] = *tl++;
3154 if (nd->nd_flag & ND_NFSV4) {
3159 if (nfsva.na_mntonfileno != UINT64_MAX)
3160 dp->d_fileno = nfsva.na_mntonfileno;
3162 dp->d_fileno = nfsva.na_fileid;
3163 } else if (nfsva.na_filesid[0] ==
3164 dnp->n_vattr.na_filesid[0] &&
3165 nfsva.na_filesid[1] ==
3166 dnp->n_vattr.na_filesid[1]) {
3167 dp->d_fileno = nfsva.na_fileid;
3171 } while (fakefileno ==
3173 dp->d_fileno = fakefileno;
3175 dp->d_type = vtonfs_dtype(nfsva.na_type);
3178 dp->d_fileno = nfsva.na_fileid;
3180 *tl2++ = cookiep->nfsuquad[0] = cookie.lval[0] =
3182 *tl2 = cookiep->nfsuquad[1] = cookie.lval[1] =
3185 more_dirs = fxdr_unsigned(int, *tl);
3188 * If at end of rpc data, get the eof boolean
3191 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3192 eof = fxdr_unsigned(int, *tl);
3195 if (nd->nd_flag & ND_NFSV4) {
3196 error = nfscl_postop_attr(nd, nap, attrflagp,
3202 mbuf_freem(nd->nd_mrep);
3206 * Fill last record, iff any, out to a multiple of DIRBLKSIZ
3207 * by increasing d_reclen for the last record.
3210 left = DIRBLKSIZ - blksiz;
3211 dp->d_reclen += left;
3212 uio_iov_base_add(uiop, left);
3213 uio_iov_len_add(uiop, -(left));
3214 uio_uio_resid_add(uiop, -(left));
3215 uiop->uio_offset += left;
3219 * If returning no data, assume end of file.
3220 * If not bigenough, return not end of file, since you aren't
3221 * returning all the data
3222 * Otherwise, return the eof flag from the server.
3225 if (tresid == ((size_t)(uio_uio_resid(uiop))))
3227 else if (!bigenough)
3234 * Add extra empty records to any remaining DIRBLKSIZ chunks.
3236 while (uio_uio_resid(uiop) > 0 && uio_uio_resid(uiop) != tresid) {
3237 dp = (struct dirent *)uio_iov_base(uiop);
3238 dp->d_type = DT_UNKNOWN;
3241 dp->d_name[0] = '\0';
3242 tl = (u_int32_t *)&dp->d_name[4];
3243 *tl++ = cookie.lval[0];
3244 *tl = cookie.lval[1];
3245 dp->d_reclen = DIRBLKSIZ;
3246 uio_iov_base_add(uiop, DIRBLKSIZ);
3247 uio_iov_len_add(uiop, -(DIRBLKSIZ));
3248 uio_uio_resid_add(uiop, -(DIRBLKSIZ));
3249 uiop->uio_offset += DIRBLKSIZ;
3253 if (nd->nd_mrep != NULL)
3254 mbuf_freem(nd->nd_mrep);
3260 * NFS V3 readdir plus RPC. Used in place of nfsrpc_readdir().
3261 * (Also used for NFS V4 when mount flag set.)
3262 * (ditto above w.r.t. multiple of DIRBLKSIZ, etc.)
3265 nfsrpc_readdirplus(vnode_t vp, struct uio *uiop, nfsuint64 *cookiep,
3266 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp,
3267 int *eofp, void *stuff)
3270 struct dirent *dp = NULL;
3272 vnode_t newvp = NULLVP;
3273 struct nfsrv_descript nfsd, *nd = &nfsd;
3274 struct nameidata nami, *ndp = &nami;
3275 struct componentname *cnp = &ndp->ni_cnd;
3276 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
3277 struct nfsnode *dnp = VTONFS(vp), *np;
3278 struct nfsvattr nfsva;
3280 nfsquad_t cookie, ncookie;
3281 int error = 0, tlen, more_dirs = 1, blksiz = 0, bigenough = 1;
3282 int attrflag, tryformoredirs = 1, eof = 0, gotmnton = 0;
3283 int isdotdot = 0, unlocknewvp = 0;
3284 u_int64_t dotfileid, dotdotfileid = 0, fakefileno = UINT64_MAX;
3285 u_int64_t fileno = 0;
3287 nfsattrbit_t attrbits, dattrbits;
3289 u_int32_t *tl2 = NULL, rderr;
3290 struct timespec dctime;
3292 KASSERT(uiop->uio_iovcnt == 1 &&
3293 (uio_uio_resid(uiop) & (DIRBLKSIZ - 1)) == 0,
3294 ("nfs readdirplusrpc bad uio"));
3295 ncookie.lval[0] = ncookie.lval[1] = 0;
3296 timespecclear(&dctime);
3302 cookie.lval[0] = cookiep->nfsuquad[0];
3303 cookie.lval[1] = cookiep->nfsuquad[1];
3304 tresid = uio_uio_resid(uiop);
3307 * For NFSv4, first create the "." and ".." entries.
3309 if (NFSHASNFSV4(nmp)) {
3310 NFSGETATTR_ATTRBIT(&dattrbits);
3311 NFSZERO_ATTRBIT(&attrbits);
3312 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_FILEID);
3313 if (NFSISSET_ATTRBIT(&dnp->n_vattr.na_suppattr,
3314 NFSATTRBIT_MOUNTEDONFILEID)) {
3315 NFSSETBIT_ATTRBIT(&attrbits,
3316 NFSATTRBIT_MOUNTEDONFILEID);
3320 * Must fake it. Use the fileno, except when the
3321 * fsid is != to that of the directory. For that
3322 * case, generate a fake fileno that is not the same.
3324 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_FSID);
3329 * Joy, oh joy. For V4 we get to hand craft '.' and '..'.
3331 if (uiop->uio_offset == 0) {
3332 NFSCL_REQSTART(nd, NFSPROC_LOOKUPP, vp);
3333 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
3334 *tl++ = txdr_unsigned(NFSV4OP_GETFH);
3335 *tl = txdr_unsigned(NFSV4OP_GETATTR);
3336 (void) nfsrv_putattrbit(nd, &attrbits);
3337 error = nfscl_request(nd, vp, p, cred, stuff);
3340 dotfileid = 0; /* Fake out the compiler. */
3341 if ((nd->nd_flag & ND_NOMOREDATA) == 0) {
3342 error = nfsm_loadattr(nd, &nfsva);
3345 dctime = nfsva.na_ctime;
3346 dotfileid = nfsva.na_fileid;
3348 if (nd->nd_repstat == 0) {
3349 NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
3350 len = fxdr_unsigned(int, *(tl + 4));
3351 if (len > 0 && len <= NFSX_V4FHMAX)
3352 error = nfsm_advance(nd, NFSM_RNDUP(len), -1);
3356 NFSM_DISSECT(tl, u_int32_t *, 2*NFSX_UNSIGNED);
3357 nfsva.na_mntonfileno = UINT64_MAX;
3358 error = nfsv4_loadattr(nd, NULL, &nfsva, NULL,
3359 NULL, 0, NULL, NULL, NULL, NULL, NULL, 0,
3360 NULL, NULL, NULL, p, cred);
3362 dotdotfileid = dotfileid;
3363 } else if (gotmnton) {
3364 if (nfsva.na_mntonfileno != UINT64_MAX)
3365 dotdotfileid = nfsva.na_mntonfileno;
3367 dotdotfileid = nfsva.na_fileid;
3368 } else if (nfsva.na_filesid[0] ==
3369 dnp->n_vattr.na_filesid[0] &&
3370 nfsva.na_filesid[1] ==
3371 dnp->n_vattr.na_filesid[1]) {
3372 dotdotfileid = nfsva.na_fileid;
3376 } while (fakefileno ==
3378 dotdotfileid = fakefileno;
3381 } else if (nd->nd_repstat == NFSERR_NOENT) {
3383 * Lookupp returns NFSERR_NOENT when we are
3384 * at the root, so just use the current dir.
3387 dotdotfileid = dotfileid;
3389 error = nd->nd_repstat;
3391 mbuf_freem(nd->nd_mrep);
3395 dp = (struct dirent *)uio_iov_base(uiop);
3397 dp->d_type = DT_DIR;
3398 dp->d_fileno = dotfileid;
3400 *((uint64_t *)dp->d_name) = 0; /* Zero pad it. */
3401 dp->d_name[0] = '.';
3402 dp->d_reclen = _GENERIC_DIRSIZ(dp) + NFSX_HYPER;
3404 * Just make these offset cookie 0.
3406 tl = (u_int32_t *)&dp->d_name[8];
3409 blksiz += dp->d_reclen;
3410 uio_uio_resid_add(uiop, -(dp->d_reclen));
3411 uiop->uio_offset += dp->d_reclen;
3412 uio_iov_base_add(uiop, dp->d_reclen);
3413 uio_iov_len_add(uiop, -(dp->d_reclen));
3414 dp = (struct dirent *)uio_iov_base(uiop);
3416 dp->d_type = DT_DIR;
3417 dp->d_fileno = dotdotfileid;
3419 *((uint64_t *)dp->d_name) = 0;
3420 dp->d_name[0] = '.';
3421 dp->d_name[1] = '.';
3422 dp->d_reclen = _GENERIC_DIRSIZ(dp) + NFSX_HYPER;
3424 * Just make these offset cookie 0.
3426 tl = (u_int32_t *)&dp->d_name[8];
3429 blksiz += dp->d_reclen;
3430 uio_uio_resid_add(uiop, -(dp->d_reclen));
3431 uiop->uio_offset += dp->d_reclen;
3432 uio_iov_base_add(uiop, dp->d_reclen);
3433 uio_iov_len_add(uiop, -(dp->d_reclen));
3435 NFSREADDIRPLUS_ATTRBIT(&attrbits);
3437 NFSSETBIT_ATTRBIT(&attrbits,
3438 NFSATTRBIT_MOUNTEDONFILEID);
3442 * Loop around doing readdir rpc's of size nm_readdirsize.
3443 * The stopping criteria is EOF or buffer full.
3445 while (more_dirs && bigenough) {
3447 NFSCL_REQSTART(nd, NFSPROC_READDIRPLUS, vp);
3448 NFSM_BUILD(tl, u_int32_t *, 6 * NFSX_UNSIGNED);
3449 *tl++ = cookie.lval[0];
3450 *tl++ = cookie.lval[1];
3451 if (cookie.qval == 0) {
3456 *tl++ = dnp->n_cookieverf.nfsuquad[0];
3457 *tl++ = dnp->n_cookieverf.nfsuquad[1];
3460 *tl++ = txdr_unsigned(nmp->nm_readdirsize);
3461 *tl = txdr_unsigned(nmp->nm_readdirsize);
3462 if (nd->nd_flag & ND_NFSV4) {
3463 (void) nfsrv_putattrbit(nd, &attrbits);
3464 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
3465 *tl = txdr_unsigned(NFSV4OP_GETATTR);
3466 (void) nfsrv_putattrbit(nd, &dattrbits);
3468 error = nfscl_request(nd, vp, p, cred, stuff);
3471 if (nd->nd_flag & ND_NFSV3)
3472 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
3473 if (nd->nd_repstat || error) {
3475 error = nd->nd_repstat;
3478 if ((nd->nd_flag & ND_NFSV3) != 0 && *attrflagp != 0)
3479 dctime = nap->na_ctime;
3480 NFSM_DISSECT(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
3482 dnp->n_cookieverf.nfsuquad[0] = *tl++;
3483 dnp->n_cookieverf.nfsuquad[1] = *tl++;
3485 more_dirs = fxdr_unsigned(int, *tl);
3489 /* loop through the dir entries, doctoring them to 4bsd form */
3490 while (more_dirs && bigenough) {
3491 NFSM_DISSECT(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
3492 if (nd->nd_flag & ND_NFSV4) {
3493 ncookie.lval[0] = *tl++;
3494 ncookie.lval[1] = *tl++;
3496 fileno = fxdr_hyper(tl);
3499 len = fxdr_unsigned(int, *tl);
3500 if (len <= 0 || len > NFS_MAXNAMLEN) {
3504 tlen = roundup2(len, 8);
3506 tlen += 8; /* To ensure null termination. */
3507 left = DIRBLKSIZ - blksiz;
3508 if (_GENERIC_DIRLEN(len) + NFSX_HYPER > left) {
3509 dp->d_reclen += left;
3510 uio_iov_base_add(uiop, left);
3511 uio_iov_len_add(uiop, -(left));
3512 uio_uio_resid_add(uiop, -(left));
3513 uiop->uio_offset += left;
3516 if (_GENERIC_DIRLEN(len) + NFSX_HYPER >
3517 uio_uio_resid(uiop))
3520 dp = (struct dirent *)uio_iov_base(uiop);
3523 dp->d_reclen = _GENERIC_DIRLEN(len) +
3525 dp->d_type = DT_UNKNOWN;
3526 blksiz += dp->d_reclen;
3527 if (blksiz == DIRBLKSIZ)
3529 uio_uio_resid_add(uiop, -(DIRHDSIZ));
3530 uiop->uio_offset += DIRHDSIZ;
3531 uio_iov_base_add(uiop, DIRHDSIZ);
3532 uio_iov_len_add(uiop, -(DIRHDSIZ));
3533 cnp->cn_nameptr = uio_iov_base(uiop);
3534 cnp->cn_namelen = len;
3536 error = nfsm_mbufuio(nd, uiop, len);
3539 cp = uio_iov_base(uiop);
3542 cp += tlen; /* points to cookie storage */
3543 tl2 = (u_int32_t *)cp;
3544 if (len == 2 && cnp->cn_nameptr[0] == '.' &&
3545 cnp->cn_nameptr[1] == '.')
3549 uio_iov_base_add(uiop, (tlen + NFSX_HYPER));
3550 uio_iov_len_add(uiop, -(tlen + NFSX_HYPER));
3551 uio_uio_resid_add(uiop, -(tlen + NFSX_HYPER));
3552 uiop->uio_offset += (tlen + NFSX_HYPER);
3554 error = nfsm_advance(nd, NFSM_RNDUP(len), -1);
3559 if (nd->nd_flag & ND_NFSV3) {
3560 NFSM_DISSECT(tl, u_int32_t *, 3*NFSX_UNSIGNED);
3561 ncookie.lval[0] = *tl++;
3562 ncookie.lval[1] = *tl++;
3563 attrflag = fxdr_unsigned(int, *tl);
3565 error = nfsm_loadattr(nd, &nfsva);
3569 NFSM_DISSECT(tl,u_int32_t *,NFSX_UNSIGNED);
3571 error = nfsm_getfh(nd, &nfhp);
3575 if (!attrflag && nfhp != NULL) {
3576 free(nfhp, M_NFSFH);
3581 nfsva.na_mntonfileno = 0xffffffff;
3582 error = nfsv4_loadattr(nd, NULL, &nfsva, &nfhp,
3583 NULL, 0, NULL, NULL, NULL, NULL, NULL, 0,
3584 NULL, NULL, &rderr, p, cred);
3590 if (nd->nd_flag & ND_NFSV4) {
3593 } else if (gotmnton) {
3594 if (nfsva.na_mntonfileno != 0xffffffff)
3595 dp->d_fileno = nfsva.na_mntonfileno;
3597 dp->d_fileno = nfsva.na_fileid;
3598 } else if (nfsva.na_filesid[0] ==
3599 dnp->n_vattr.na_filesid[0] &&
3600 nfsva.na_filesid[1] ==
3601 dnp->n_vattr.na_filesid[1]) {
3602 dp->d_fileno = nfsva.na_fileid;
3606 } while (fakefileno ==
3608 dp->d_fileno = fakefileno;
3611 dp->d_fileno = fileno;
3613 *tl2++ = cookiep->nfsuquad[0] = cookie.lval[0] =
3615 *tl2 = cookiep->nfsuquad[1] = cookie.lval[1] =
3619 if (NFSRV_CMPFH(nfhp->nfh_fh, nfhp->nfh_len,
3620 dnp->n_fhp->nfh_fh, dnp->n_fhp->nfh_len)) {
3624 free(nfhp, M_NFSFH);
3626 } else if (isdotdot != 0) {
3628 * Skip doing a nfscl_nget() call for "..".
3629 * There's a race between acquiring the nfs
3630 * node here and lookups that look for the
3631 * directory being read (in the parent).
3632 * It would try to get a lock on ".." here,
3633 * owning the lock on the directory being
3634 * read. Lookup will hold the lock on ".."
3635 * and try to acquire the lock on the
3636 * directory being read.
3637 * If the directory is unlocked/relocked,
3638 * then there is a LOR with the buflock
3641 free(nfhp, M_NFSFH);
3643 error = nfscl_nget(vnode_mount(vp), vp,
3644 nfhp, cnp, p, &np, NULL, LK_EXCLUSIVE);
3651 if (newvp != NULLVP) {
3652 error = nfscl_loadattrcache(&newvp,
3653 &nfsva, NULL, NULL, 0, 0);
3662 vtonfs_dtype(np->n_vattr.na_type);
3664 NFSCNHASH(cnp, HASHINIT);
3665 if (cnp->cn_namelen <= NCHNAMLEN &&
3666 (newvp->v_type != VDIR ||
3667 dctime.tv_sec != 0)) {
3668 cache_enter_time(ndp->ni_dvp,
3671 newvp->v_type != VDIR ? NULL :
3681 } else if (nfhp != NULL) {
3682 free(nfhp, M_NFSFH);
3684 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3685 more_dirs = fxdr_unsigned(int, *tl);
3688 * If at end of rpc data, get the eof boolean
3691 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3692 eof = fxdr_unsigned(int, *tl);
3695 if (nd->nd_flag & ND_NFSV4) {
3696 error = nfscl_postop_attr(nd, nap, attrflagp,
3702 mbuf_freem(nd->nd_mrep);
3706 * Fill last record, iff any, out to a multiple of DIRBLKSIZ
3707 * by increasing d_reclen for the last record.
3710 left = DIRBLKSIZ - blksiz;
3711 dp->d_reclen += left;
3712 uio_iov_base_add(uiop, left);
3713 uio_iov_len_add(uiop, -(left));
3714 uio_uio_resid_add(uiop, -(left));
3715 uiop->uio_offset += left;
3719 * If returning no data, assume end of file.
3720 * If not bigenough, return not end of file, since you aren't
3721 * returning all the data
3722 * Otherwise, return the eof flag from the server.
3725 if (tresid == uio_uio_resid(uiop))
3727 else if (!bigenough)
3734 * Add extra empty records to any remaining DIRBLKSIZ chunks.
3736 while (uio_uio_resid(uiop) > 0 && uio_uio_resid(uiop) != tresid) {
3737 dp = (struct dirent *)uio_iov_base(uiop);
3738 dp->d_type = DT_UNKNOWN;
3741 dp->d_name[0] = '\0';
3742 tl = (u_int32_t *)&dp->d_name[4];
3743 *tl++ = cookie.lval[0];
3744 *tl = cookie.lval[1];
3745 dp->d_reclen = DIRBLKSIZ;
3746 uio_iov_base_add(uiop, DIRBLKSIZ);
3747 uio_iov_len_add(uiop, -(DIRBLKSIZ));
3748 uio_uio_resid_add(uiop, -(DIRBLKSIZ));
3749 uiop->uio_offset += DIRBLKSIZ;
3753 if (nd->nd_mrep != NULL)
3754 mbuf_freem(nd->nd_mrep);
3763 nfsrpc_commit(vnode_t vp, u_quad_t offset, int cnt, struct ucred *cred,
3764 NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, void *stuff)
3767 struct nfsrv_descript nfsd, *nd = &nfsd;
3768 nfsattrbit_t attrbits;
3770 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
3773 NFSCL_REQSTART(nd, NFSPROC_COMMIT, vp);
3774 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
3775 txdr_hyper(offset, tl);
3777 *tl = txdr_unsigned(cnt);
3778 if (nd->nd_flag & ND_NFSV4) {
3780 * And do a Getattr op.
3782 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
3783 *tl = txdr_unsigned(NFSV4OP_GETATTR);
3784 NFSGETATTR_ATTRBIT(&attrbits);
3785 (void) nfsrv_putattrbit(nd, &attrbits);
3787 error = nfscl_request(nd, vp, p, cred, stuff);
3790 error = nfscl_wcc_data(nd, vp, nap, attrflagp, NULL, stuff);
3791 if (!error && !nd->nd_repstat) {
3792 NFSM_DISSECT(tl, u_int32_t *, NFSX_VERF);
3794 if (NFSBCMP(nmp->nm_verf, tl, NFSX_VERF)) {
3795 NFSBCOPY(tl, nmp->nm_verf, NFSX_VERF);
3796 nd->nd_repstat = NFSERR_STALEWRITEVERF;
3799 if (nd->nd_flag & ND_NFSV4)
3800 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
3803 if (!error && nd->nd_repstat)
3804 error = nd->nd_repstat;
3805 mbuf_freem(nd->nd_mrep);
3810 * NFS byte range lock rpc.
3811 * (Mostly just calls one of the three lower level RPC routines.)
3814 nfsrpc_advlock(vnode_t vp, off_t size, int op, struct flock *fl,
3815 int reclaim, struct ucred *cred, NFSPROC_T *p, void *id, int flags)
3817 struct nfscllockowner *lp;
3818 struct nfsclclient *clp;
3820 struct nfsrv_descript nfsd, *nd = &nfsd;
3821 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
3824 u_int32_t clidrev = 0;
3825 int error = 0, newone = 0, expireret = 0, retrycnt, donelocally;
3829 * Convert the flock structure into a start and end and do POSIX
3832 switch (fl->l_whence) {
3836 * Caller is responsible for adding any necessary offset
3837 * when SEEK_CUR is used.
3839 start = fl->l_start;
3843 start = size + fl->l_start;
3844 off = size + fl->l_start;
3851 if (fl->l_len != 0) {
3852 end = start + fl->l_len - 1;
3863 if (op == F_GETLK) {
3864 error = nfscl_getcl(vnode_mount(vp), cred, p, 1, &clp);
3867 error = nfscl_lockt(vp, clp, off, len, fl, p, id, flags);
3869 clidrev = clp->nfsc_clientidrev;
3870 error = nfsrpc_lockt(nd, vp, clp, off, len, fl, cred,
3872 } else if (error == -1) {
3875 nfscl_clientrelease(clp);
3876 } else if (op == F_UNLCK && fl->l_type == F_UNLCK) {
3878 * We must loop around for all lockowner cases.
3881 error = nfscl_getcl(vnode_mount(vp), cred, p, 1, &clp);
3885 error = nfscl_relbytelock(vp, off, len, cred, p, callcnt,
3886 clp, id, flags, &lp, &dorpc);
3888 * If it returns a NULL lp, we're done.
3892 nfscl_clientrelease(clp);
3894 nfscl_releasealllocks(clp, vp, p, id, flags);
3897 if (nmp->nm_clp != NULL)
3898 clidrev = nmp->nm_clp->nfsc_clientidrev;
3902 * If the server doesn't support Posix lock semantics,
3903 * only allow locks on the entire file, since it won't
3904 * handle overlapping byte ranges.
3905 * There might still be a problem when a lock
3906 * upgrade/downgrade (read<->write) occurs, since the
3907 * server "might" expect an unlock first?
3909 if (dorpc && (lp->nfsl_open->nfso_posixlock ||
3910 (off == 0 && len == NFS64BITSSET))) {
3912 * Since the lock records will go away, we must
3913 * wait for grace and delay here.
3916 error = nfsrpc_locku(nd, nmp, lp, off, len,
3917 NFSV4LOCKT_READ, cred, p, 0);
3918 if ((nd->nd_repstat == NFSERR_GRACE ||
3919 nd->nd_repstat == NFSERR_DELAY) &&
3921 (void) nfs_catnap(PZERO, (int)nd->nd_repstat,
3923 } while ((nd->nd_repstat == NFSERR_GRACE ||
3924 nd->nd_repstat == NFSERR_DELAY) && error == 0);
3927 } while (error == 0 && nd->nd_repstat == 0);
3928 nfscl_releasealllocks(clp, vp, p, id, flags);
3929 } else if (op == F_SETLK) {
3930 error = nfscl_getbytelock(vp, off, len, fl->l_type, cred, p,
3931 NULL, 0, id, flags, NULL, NULL, &lp, &newone, &donelocally);
3932 if (error || donelocally) {
3935 if (nmp->nm_clp != NULL)
3936 clidrev = nmp->nm_clp->nfsc_clientidrev;
3939 nfhp = VTONFS(vp)->n_fhp;
3940 if (!lp->nfsl_open->nfso_posixlock &&
3941 (off != 0 || len != NFS64BITSSET)) {
3944 error = nfsrpc_lock(nd, nmp, vp, nfhp->nfh_fh,
3945 nfhp->nfh_len, lp, newone, reclaim, off,
3946 len, fl->l_type, cred, p, 0);
3949 error = nd->nd_repstat;
3950 nfscl_lockrelease(lp, error, newone);
3955 error = nd->nd_repstat;
3956 if (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
3957 error == NFSERR_STALEDONTRECOVER ||
3958 error == NFSERR_STALECLIENTID || error == NFSERR_DELAY ||
3959 error == NFSERR_BADSESSION) {
3960 (void) nfs_catnap(PZERO, error, "nfs_advlock");
3961 } else if ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID)
3963 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
3966 } while (error == NFSERR_GRACE ||
3967 error == NFSERR_STALECLIENTID || error == NFSERR_DELAY ||
3968 error == NFSERR_STALEDONTRECOVER || error == NFSERR_STALESTATEID ||
3969 error == NFSERR_BADSESSION ||
3970 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
3971 expireret == 0 && clidrev != 0 && retrycnt < 4));
3972 if (error && retrycnt >= 4)
3978 * The lower level routine for the LockT case.
3981 nfsrpc_lockt(struct nfsrv_descript *nd, vnode_t vp,
3982 struct nfsclclient *clp, u_int64_t off, u_int64_t len, struct flock *fl,
3983 struct ucred *cred, NFSPROC_T *p, void *id, int flags)
3986 int error, type, size;
3987 uint8_t own[NFSV4CL_LOCKNAMELEN + NFSX_V4FHMAX];
3989 struct nfsmount *nmp;
3990 struct nfsclsession *tsep;
3992 nmp = VFSTONFS(vp->v_mount);
3993 NFSCL_REQSTART(nd, NFSPROC_LOCKT, vp);
3994 NFSM_BUILD(tl, u_int32_t *, 7 * NFSX_UNSIGNED);
3995 if (fl->l_type == F_RDLCK)
3996 *tl++ = txdr_unsigned(NFSV4LOCKT_READ);
3998 *tl++ = txdr_unsigned(NFSV4LOCKT_WRITE);
3999 txdr_hyper(off, tl);
4001 txdr_hyper(len, tl);
4003 tsep = nfsmnt_mdssession(nmp);
4004 *tl++ = tsep->nfsess_clientid.lval[0];
4005 *tl = tsep->nfsess_clientid.lval[1];
4006 nfscl_filllockowner(id, own, flags);
4008 NFSBCOPY(np->n_fhp->nfh_fh, &own[NFSV4CL_LOCKNAMELEN],
4009 np->n_fhp->nfh_len);
4010 (void)nfsm_strtom(nd, own, NFSV4CL_LOCKNAMELEN + np->n_fhp->nfh_len);
4011 error = nfscl_request(nd, vp, p, cred, NULL);
4014 if (nd->nd_repstat == 0) {
4015 fl->l_type = F_UNLCK;
4016 } else if (nd->nd_repstat == NFSERR_DENIED) {
4018 fl->l_whence = SEEK_SET;
4019 NFSM_DISSECT(tl, u_int32_t *, 8 * NFSX_UNSIGNED);
4020 fl->l_start = fxdr_hyper(tl);
4022 len = fxdr_hyper(tl);
4024 if (len == NFS64BITSSET)
4028 type = fxdr_unsigned(int, *tl++);
4029 if (type == NFSV4LOCKT_WRITE)
4030 fl->l_type = F_WRLCK;
4032 fl->l_type = F_RDLCK;
4034 * XXX For now, I have no idea what to do with the
4035 * conflicting lock_owner, so I'll just set the pid == 0
4036 * and skip over the lock_owner.
4038 fl->l_pid = (pid_t)0;
4040 size = fxdr_unsigned(int, *tl);
4041 if (size < 0 || size > NFSV4_OPAQUELIMIT)
4044 error = nfsm_advance(nd, NFSM_RNDUP(size), -1);
4045 } else if (nd->nd_repstat == NFSERR_STALECLIENTID)
4046 nfscl_initiate_recovery(clp);
4048 mbuf_freem(nd->nd_mrep);
4053 * Lower level function that performs the LockU RPC.
4056 nfsrpc_locku(struct nfsrv_descript *nd, struct nfsmount *nmp,
4057 struct nfscllockowner *lp, u_int64_t off, u_int64_t len,
4058 u_int32_t type, struct ucred *cred, NFSPROC_T *p, int syscred)
4063 nfscl_reqstart(nd, NFSPROC_LOCKU, nmp, lp->nfsl_open->nfso_fh,
4064 lp->nfsl_open->nfso_fhlen, NULL, NULL, 0, 0);
4065 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID + 6 * NFSX_UNSIGNED);
4066 *tl++ = txdr_unsigned(type);
4067 *tl = txdr_unsigned(lp->nfsl_seqid);
4068 if (nfstest_outofseq &&
4069 (arc4random() % nfstest_outofseq) == 0)
4070 *tl = txdr_unsigned(lp->nfsl_seqid + 1);
4072 if (NFSHASNFSV4N(nmp))
4075 *tl++ = lp->nfsl_stateid.seqid;
4076 *tl++ = lp->nfsl_stateid.other[0];
4077 *tl++ = lp->nfsl_stateid.other[1];
4078 *tl++ = lp->nfsl_stateid.other[2];
4079 txdr_hyper(off, tl);
4081 txdr_hyper(len, tl);
4083 nd->nd_flag |= ND_USEGSSNAME;
4084 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4085 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4086 NFSCL_INCRSEQID(lp->nfsl_seqid, nd);
4089 if (nd->nd_repstat == 0) {
4090 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID);
4091 lp->nfsl_stateid.seqid = *tl++;
4092 lp->nfsl_stateid.other[0] = *tl++;
4093 lp->nfsl_stateid.other[1] = *tl++;
4094 lp->nfsl_stateid.other[2] = *tl;
4095 } else if (nd->nd_repstat == NFSERR_STALESTATEID)
4096 nfscl_initiate_recovery(lp->nfsl_open->nfso_own->nfsow_clp);
4098 mbuf_freem(nd->nd_mrep);
4103 * The actual Lock RPC.
4106 nfsrpc_lock(struct nfsrv_descript *nd, struct nfsmount *nmp, vnode_t vp,
4107 u_int8_t *nfhp, int fhlen, struct nfscllockowner *lp, int newone,
4108 int reclaim, u_int64_t off, u_int64_t len, short type, struct ucred *cred,
4109 NFSPROC_T *p, int syscred)
4113 uint8_t own[NFSV4CL_LOCKNAMELEN + NFSX_V4FHMAX];
4114 struct nfsclsession *tsep;
4116 nfscl_reqstart(nd, NFSPROC_LOCK, nmp, nfhp, fhlen, NULL, NULL, 0, 0);
4117 NFSM_BUILD(tl, u_int32_t *, 7 * NFSX_UNSIGNED);
4118 if (type == F_RDLCK)
4119 *tl++ = txdr_unsigned(NFSV4LOCKT_READ);
4121 *tl++ = txdr_unsigned(NFSV4LOCKT_WRITE);
4122 *tl++ = txdr_unsigned(reclaim);
4123 txdr_hyper(off, tl);
4125 txdr_hyper(len, tl);
4129 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID +
4130 2 * NFSX_UNSIGNED + NFSX_HYPER);
4131 *tl++ = txdr_unsigned(lp->nfsl_open->nfso_own->nfsow_seqid);
4132 if (NFSHASNFSV4N(nmp))
4135 *tl++ = lp->nfsl_open->nfso_stateid.seqid;
4136 *tl++ = lp->nfsl_open->nfso_stateid.other[0];
4137 *tl++ = lp->nfsl_open->nfso_stateid.other[1];
4138 *tl++ = lp->nfsl_open->nfso_stateid.other[2];
4139 *tl++ = txdr_unsigned(lp->nfsl_seqid);
4140 tsep = nfsmnt_mdssession(nmp);
4141 *tl++ = tsep->nfsess_clientid.lval[0];
4142 *tl = tsep->nfsess_clientid.lval[1];
4143 NFSBCOPY(lp->nfsl_owner, own, NFSV4CL_LOCKNAMELEN);
4144 NFSBCOPY(nfhp, &own[NFSV4CL_LOCKNAMELEN], fhlen);
4145 (void)nfsm_strtom(nd, own, NFSV4CL_LOCKNAMELEN + fhlen);
4148 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID + NFSX_UNSIGNED);
4149 if (NFSHASNFSV4N(nmp))
4152 *tl++ = lp->nfsl_stateid.seqid;
4153 *tl++ = lp->nfsl_stateid.other[0];
4154 *tl++ = lp->nfsl_stateid.other[1];
4155 *tl++ = lp->nfsl_stateid.other[2];
4156 *tl = txdr_unsigned(lp->nfsl_seqid);
4157 if (nfstest_outofseq &&
4158 (arc4random() % nfstest_outofseq) == 0)
4159 *tl = txdr_unsigned(lp->nfsl_seqid + 1);
4162 nd->nd_flag |= ND_USEGSSNAME;
4163 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, vp, p, cred,
4164 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4168 NFSCL_INCRSEQID(lp->nfsl_open->nfso_own->nfsow_seqid, nd);
4169 NFSCL_INCRSEQID(lp->nfsl_seqid, nd);
4170 if (nd->nd_repstat == 0) {
4171 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID);
4172 lp->nfsl_stateid.seqid = *tl++;
4173 lp->nfsl_stateid.other[0] = *tl++;
4174 lp->nfsl_stateid.other[1] = *tl++;
4175 lp->nfsl_stateid.other[2] = *tl;
4176 } else if (nd->nd_repstat == NFSERR_DENIED) {
4177 NFSM_DISSECT(tl, u_int32_t *, 8 * NFSX_UNSIGNED);
4178 size = fxdr_unsigned(int, *(tl + 7));
4179 if (size < 0 || size > NFSV4_OPAQUELIMIT)
4182 error = nfsm_advance(nd, NFSM_RNDUP(size), -1);
4183 } else if (nd->nd_repstat == NFSERR_STALESTATEID)
4184 nfscl_initiate_recovery(lp->nfsl_open->nfso_own->nfsow_clp);
4186 mbuf_freem(nd->nd_mrep);
4192 * (always called with the vp for the mount point)
4195 nfsrpc_statfs(vnode_t vp, struct nfsstatfs *sbp, struct nfsfsinfo *fsp,
4196 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp,
4199 u_int32_t *tl = NULL;
4200 struct nfsrv_descript nfsd, *nd = &nfsd;
4201 struct nfsmount *nmp;
4202 nfsattrbit_t attrbits;
4206 nmp = VFSTONFS(vnode_mount(vp));
4207 if (NFSHASNFSV4(nmp)) {
4209 * For V4, you actually do a getattr.
4211 NFSCL_REQSTART(nd, NFSPROC_GETATTR, vp);
4212 NFSSTATFS_GETATTRBIT(&attrbits);
4213 (void) nfsrv_putattrbit(nd, &attrbits);
4214 nd->nd_flag |= ND_USEGSSNAME;
4215 error = nfscl_request(nd, vp, p, cred, stuff);
4218 if (nd->nd_repstat == 0) {
4219 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0,
4220 NULL, NULL, sbp, fsp, NULL, 0, NULL, NULL, NULL, p,
4223 nmp->nm_fsid[0] = nap->na_filesid[0];
4224 nmp->nm_fsid[1] = nap->na_filesid[1];
4225 NFSSETHASSETFSID(nmp);
4229 error = nd->nd_repstat;
4234 NFSCL_REQSTART(nd, NFSPROC_FSSTAT, vp);
4235 error = nfscl_request(nd, vp, p, cred, stuff);
4238 if (nd->nd_flag & ND_NFSV3) {
4239 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
4243 if (nd->nd_repstat) {
4244 error = nd->nd_repstat;
4247 NFSM_DISSECT(tl, u_int32_t *,
4248 NFSX_STATFS(nd->nd_flag & ND_NFSV3));
4250 if (NFSHASNFSV3(nmp)) {
4251 sbp->sf_tbytes = fxdr_hyper(tl); tl += 2;
4252 sbp->sf_fbytes = fxdr_hyper(tl); tl += 2;
4253 sbp->sf_abytes = fxdr_hyper(tl); tl += 2;
4254 sbp->sf_tfiles = fxdr_hyper(tl); tl += 2;
4255 sbp->sf_ffiles = fxdr_hyper(tl); tl += 2;
4256 sbp->sf_afiles = fxdr_hyper(tl); tl += 2;
4257 sbp->sf_invarsec = fxdr_unsigned(u_int32_t, *tl);
4258 } else if (NFSHASNFSV4(nmp) == 0) {
4259 sbp->sf_tsize = fxdr_unsigned(u_int32_t, *tl++);
4260 sbp->sf_bsize = fxdr_unsigned(u_int32_t, *tl++);
4261 sbp->sf_blocks = fxdr_unsigned(u_int32_t, *tl++);
4262 sbp->sf_bfree = fxdr_unsigned(u_int32_t, *tl++);
4263 sbp->sf_bavail = fxdr_unsigned(u_int32_t, *tl);
4266 mbuf_freem(nd->nd_mrep);
4274 nfsrpc_pathconf(vnode_t vp, struct nfsv3_pathconf *pc,
4275 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp,
4278 struct nfsrv_descript nfsd, *nd = &nfsd;
4279 struct nfsmount *nmp;
4281 nfsattrbit_t attrbits;
4285 nmp = VFSTONFS(vnode_mount(vp));
4286 if (NFSHASNFSV4(nmp)) {
4288 * For V4, you actually do a getattr.
4290 NFSCL_REQSTART(nd, NFSPROC_GETATTR, vp);
4291 NFSPATHCONF_GETATTRBIT(&attrbits);
4292 (void) nfsrv_putattrbit(nd, &attrbits);
4293 nd->nd_flag |= ND_USEGSSNAME;
4294 error = nfscl_request(nd, vp, p, cred, stuff);
4297 if (nd->nd_repstat == 0) {
4298 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0,
4299 pc, NULL, NULL, NULL, NULL, 0, NULL, NULL, NULL, p,
4304 error = nd->nd_repstat;
4307 NFSCL_REQSTART(nd, NFSPROC_PATHCONF, vp);
4308 error = nfscl_request(nd, vp, p, cred, stuff);
4311 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
4312 if (nd->nd_repstat && !error)
4313 error = nd->nd_repstat;
4315 NFSM_DISSECT(tl, u_int32_t *, NFSX_V3PATHCONF);
4316 pc->pc_linkmax = fxdr_unsigned(u_int32_t, *tl++);
4317 pc->pc_namemax = fxdr_unsigned(u_int32_t, *tl++);
4318 pc->pc_notrunc = fxdr_unsigned(u_int32_t, *tl++);
4319 pc->pc_chownrestricted =
4320 fxdr_unsigned(u_int32_t, *tl++);
4321 pc->pc_caseinsensitive =
4322 fxdr_unsigned(u_int32_t, *tl++);
4323 pc->pc_casepreserving = fxdr_unsigned(u_int32_t, *tl);
4327 mbuf_freem(nd->nd_mrep);
4332 * nfs version 3 fsinfo rpc call
4335 nfsrpc_fsinfo(vnode_t vp, struct nfsfsinfo *fsp, struct ucred *cred,
4336 NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, void *stuff)
4339 struct nfsrv_descript nfsd, *nd = &nfsd;
4343 NFSCL_REQSTART(nd, NFSPROC_FSINFO, vp);
4344 error = nfscl_request(nd, vp, p, cred, stuff);
4347 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
4348 if (nd->nd_repstat && !error)
4349 error = nd->nd_repstat;
4351 NFSM_DISSECT(tl, u_int32_t *, NFSX_V3FSINFO);
4352 fsp->fs_rtmax = fxdr_unsigned(u_int32_t, *tl++);
4353 fsp->fs_rtpref = fxdr_unsigned(u_int32_t, *tl++);
4354 fsp->fs_rtmult = fxdr_unsigned(u_int32_t, *tl++);
4355 fsp->fs_wtmax = fxdr_unsigned(u_int32_t, *tl++);
4356 fsp->fs_wtpref = fxdr_unsigned(u_int32_t, *tl++);
4357 fsp->fs_wtmult = fxdr_unsigned(u_int32_t, *tl++);
4358 fsp->fs_dtpref = fxdr_unsigned(u_int32_t, *tl++);
4359 fsp->fs_maxfilesize = fxdr_hyper(tl);
4361 fxdr_nfsv3time(tl, &fsp->fs_timedelta);
4363 fsp->fs_properties = fxdr_unsigned(u_int32_t, *tl);
4366 mbuf_freem(nd->nd_mrep);
4371 * This function performs the Renew RPC.
4374 nfsrpc_renew(struct nfsclclient *clp, struct nfsclds *dsp, struct ucred *cred,
4378 struct nfsrv_descript nfsd;
4379 struct nfsrv_descript *nd = &nfsd;
4380 struct nfsmount *nmp;
4382 struct nfssockreq *nrp;
4383 struct nfsclsession *tsep;
4385 nmp = clp->nfsc_nmp;
4389 nfscl_reqstart(nd, NFSPROC_RENEW, nmp, NULL, 0, NULL, NULL, 0,
4392 nfscl_reqstart(nd, NFSPROC_RENEW, nmp, NULL, 0, NULL,
4393 &dsp->nfsclds_sess, 0, 0);
4394 if (!NFSHASNFSV4N(nmp)) {
4395 /* NFSv4.1 just uses a Sequence Op and not a Renew. */
4396 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
4397 tsep = nfsmnt_mdssession(nmp);
4398 *tl++ = tsep->nfsess_clientid.lval[0];
4399 *tl = tsep->nfsess_clientid.lval[1];
4403 nrp = dsp->nfsclds_sockp;
4405 /* If NULL, use the MDS socket. */
4406 nrp = &nmp->nm_sockreq;
4407 nd->nd_flag |= ND_USEGSSNAME;
4409 error = newnfs_request(nd, nmp, NULL, nrp, NULL, p, cred,
4410 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4412 error = newnfs_request(nd, nmp, NULL, nrp, NULL, p, cred,
4413 NFS_PROG, NFS_VER4, NULL, 1, NULL, &dsp->nfsclds_sess);
4416 error = nd->nd_repstat;
4417 mbuf_freem(nd->nd_mrep);
4422 * This function performs the Releaselockowner RPC.
4425 nfsrpc_rellockown(struct nfsmount *nmp, struct nfscllockowner *lp,
4426 uint8_t *fh, int fhlen, struct ucred *cred, NFSPROC_T *p)
4428 struct nfsrv_descript nfsd, *nd = &nfsd;
4431 uint8_t own[NFSV4CL_LOCKNAMELEN + NFSX_V4FHMAX];
4432 struct nfsclsession *tsep;
4434 if (NFSHASNFSV4N(nmp)) {
4435 /* For NFSv4.1, do a FreeStateID. */
4436 nfscl_reqstart(nd, NFSPROC_FREESTATEID, nmp, NULL, 0, NULL,
4438 nfsm_stateidtom(nd, &lp->nfsl_stateid, NFSSTATEID_PUTSTATEID);
4440 nfscl_reqstart(nd, NFSPROC_RELEASELCKOWN, nmp, NULL, 0, NULL,
4442 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
4443 tsep = nfsmnt_mdssession(nmp);
4444 *tl++ = tsep->nfsess_clientid.lval[0];
4445 *tl = tsep->nfsess_clientid.lval[1];
4446 NFSBCOPY(lp->nfsl_owner, own, NFSV4CL_LOCKNAMELEN);
4447 NFSBCOPY(fh, &own[NFSV4CL_LOCKNAMELEN], fhlen);
4448 (void)nfsm_strtom(nd, own, NFSV4CL_LOCKNAMELEN + fhlen);
4450 nd->nd_flag |= ND_USEGSSNAME;
4451 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4452 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4455 error = nd->nd_repstat;
4456 mbuf_freem(nd->nd_mrep);
4461 * This function performs the Compound to get the mount pt FH.
4464 nfsrpc_getdirpath(struct nfsmount *nmp, u_char *dirpath, struct ucred *cred,
4468 struct nfsrv_descript nfsd;
4469 struct nfsrv_descript *nd = &nfsd;
4471 int error, cnt, len, setnil;
4474 nfscl_reqstart(nd, NFSPROC_PUTROOTFH, nmp, NULL, 0, &opcntp, NULL, 0,
4483 while (*cp2 != '\0' && *cp2 != '/')
4490 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
4491 *tl = txdr_unsigned(NFSV4OP_LOOKUP);
4492 nfsm_strtom(nd, cp, strlen(cp));
4498 } while (*cp != '\0');
4499 if (NFSHASNFSV4N(nmp))
4500 /* Has a Sequence Op done by nfscl_reqstart(). */
4501 *opcntp = txdr_unsigned(3 + cnt);
4503 *opcntp = txdr_unsigned(2 + cnt);
4504 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
4505 *tl = txdr_unsigned(NFSV4OP_GETFH);
4506 nd->nd_flag |= ND_USEGSSNAME;
4507 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4508 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4511 if (nd->nd_repstat == 0) {
4512 NFSM_DISSECT(tl, u_int32_t *, (3 + 2 * cnt) * NFSX_UNSIGNED);
4513 tl += (2 + 2 * cnt);
4514 if ((len = fxdr_unsigned(int, *tl)) <= 0 ||
4516 nd->nd_repstat = NFSERR_BADXDR;
4518 nd->nd_repstat = nfsrv_mtostr(nd, nmp->nm_fh, len);
4519 if (nd->nd_repstat == 0)
4520 nmp->nm_fhsize = len;
4523 error = nd->nd_repstat;
4525 mbuf_freem(nd->nd_mrep);
4530 * This function performs the Delegreturn RPC.
4533 nfsrpc_delegreturn(struct nfscldeleg *dp, struct ucred *cred,
4534 struct nfsmount *nmp, NFSPROC_T *p, int syscred)
4537 struct nfsrv_descript nfsd;
4538 struct nfsrv_descript *nd = &nfsd;
4541 nfscl_reqstart(nd, NFSPROC_DELEGRETURN, nmp, dp->nfsdl_fh,
4542 dp->nfsdl_fhlen, NULL, NULL, 0, 0);
4543 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID);
4544 if (NFSHASNFSV4N(nmp))
4547 *tl++ = dp->nfsdl_stateid.seqid;
4548 *tl++ = dp->nfsdl_stateid.other[0];
4549 *tl++ = dp->nfsdl_stateid.other[1];
4550 *tl = dp->nfsdl_stateid.other[2];
4552 nd->nd_flag |= ND_USEGSSNAME;
4553 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4554 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4557 error = nd->nd_repstat;
4558 mbuf_freem(nd->nd_mrep);
4566 nfsrpc_getacl(vnode_t vp, struct ucred *cred, NFSPROC_T *p,
4567 struct acl *aclp, void *stuff)
4569 struct nfsrv_descript nfsd, *nd = &nfsd;
4571 nfsattrbit_t attrbits;
4572 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
4574 if (nfsrv_useacl == 0 || !NFSHASNFSV4(nmp))
4575 return (EOPNOTSUPP);
4576 NFSCL_REQSTART(nd, NFSPROC_GETACL, vp);
4577 NFSZERO_ATTRBIT(&attrbits);
4578 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_ACL);
4579 (void) nfsrv_putattrbit(nd, &attrbits);
4580 error = nfscl_request(nd, vp, p, cred, stuff);
4583 if (!nd->nd_repstat)
4584 error = nfsv4_loadattr(nd, vp, NULL, NULL, NULL, 0, NULL,
4585 NULL, NULL, NULL, aclp, 0, NULL, NULL, NULL, p, cred);
4587 error = nd->nd_repstat;
4588 mbuf_freem(nd->nd_mrep);
4596 nfsrpc_setacl(vnode_t vp, struct ucred *cred, NFSPROC_T *p,
4597 struct acl *aclp, void *stuff)
4600 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
4602 if (nfsrv_useacl == 0 || !NFSHASNFSV4(nmp))
4603 return (EOPNOTSUPP);
4604 error = nfsrpc_setattr(vp, NULL, aclp, cred, p, NULL, NULL, stuff);
4612 nfsrpc_setaclrpc(vnode_t vp, struct ucred *cred, NFSPROC_T *p,
4613 struct acl *aclp, nfsv4stateid_t *stateidp, void *stuff)
4615 struct nfsrv_descript nfsd, *nd = &nfsd;
4617 nfsattrbit_t attrbits;
4618 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
4620 if (!NFSHASNFSV4(nmp))
4621 return (EOPNOTSUPP);
4622 NFSCL_REQSTART(nd, NFSPROC_SETACL, vp);
4623 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID);
4624 NFSZERO_ATTRBIT(&attrbits);
4625 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_ACL);
4626 (void) nfsv4_fillattr(nd, vnode_mount(vp), vp, aclp, NULL, NULL, 0,
4627 &attrbits, NULL, NULL, 0, 0, 0, 0, (uint64_t)0, NULL);
4628 error = nfscl_request(nd, vp, p, cred, stuff);
4631 /* Don't care about the pre/postop attributes */
4632 mbuf_freem(nd->nd_mrep);
4633 return (nd->nd_repstat);
4637 * Do the NFSv4.1 Exchange ID.
4640 nfsrpc_exchangeid(struct nfsmount *nmp, struct nfsclclient *clp,
4641 struct nfssockreq *nrp, uint32_t exchflags, struct nfsclds **dspp,
4642 struct ucred *cred, NFSPROC_T *p)
4644 uint32_t *tl, v41flags;
4645 struct nfsrv_descript nfsd;
4646 struct nfsrv_descript *nd = &nfsd;
4647 struct nfsclds *dsp;
4648 struct timespec verstime;
4652 nfscl_reqstart(nd, NFSPROC_EXCHANGEID, nmp, NULL, 0, NULL, NULL, 0, 0);
4653 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_UNSIGNED);
4654 *tl++ = txdr_unsigned(nfsboottime.tv_sec); /* Client owner */
4655 *tl = txdr_unsigned(clp->nfsc_rev);
4656 (void) nfsm_strtom(nd, clp->nfsc_id, clp->nfsc_idlen);
4658 NFSM_BUILD(tl, uint32_t *, 3 * NFSX_UNSIGNED);
4659 *tl++ = txdr_unsigned(exchflags);
4660 *tl++ = txdr_unsigned(NFSV4EXCH_SP4NONE);
4662 /* Set the implementation id4 */
4663 *tl = txdr_unsigned(1);
4664 (void) nfsm_strtom(nd, "freebsd.org", strlen("freebsd.org"));
4665 (void) nfsm_strtom(nd, version, strlen(version));
4666 NFSM_BUILD(tl, uint32_t *, NFSX_V4TIME);
4667 verstime.tv_sec = 1293840000; /* Jan 1, 2011 */
4668 verstime.tv_nsec = 0;
4669 txdr_nfsv4time(&verstime, tl);
4670 nd->nd_flag |= ND_USEGSSNAME;
4671 error = newnfs_request(nd, nmp, NULL, nrp, NULL, p, cred,
4672 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4673 NFSCL_DEBUG(1, "exchangeid err=%d reps=%d\n", error,
4674 (int)nd->nd_repstat);
4677 if (nd->nd_repstat == 0) {
4678 NFSM_DISSECT(tl, uint32_t *, 6 * NFSX_UNSIGNED + NFSX_HYPER);
4679 len = fxdr_unsigned(int, *(tl + 7));
4680 if (len < 0 || len > NFSV4_OPAQUELIMIT) {
4681 error = NFSERR_BADXDR;
4684 dsp = malloc(sizeof(struct nfsclds) + len + 1, M_NFSCLDS,
4686 dsp->nfsclds_expire = NFSD_MONOSEC + clp->nfsc_renew;
4687 dsp->nfsclds_servownlen = len;
4688 dsp->nfsclds_sess.nfsess_clientid.lval[0] = *tl++;
4689 dsp->nfsclds_sess.nfsess_clientid.lval[1] = *tl++;
4690 dsp->nfsclds_sess.nfsess_sequenceid =
4691 fxdr_unsigned(uint32_t, *tl++);
4692 v41flags = fxdr_unsigned(uint32_t, *tl);
4693 if ((v41flags & NFSV4EXCH_USEPNFSMDS) != 0 &&
4694 NFSHASPNFSOPT(nmp)) {
4695 NFSCL_DEBUG(1, "set PNFS\n");
4697 nmp->nm_state |= NFSSTA_PNFS;
4699 dsp->nfsclds_flags |= NFSCLDS_MDS;
4701 if ((v41flags & NFSV4EXCH_USEPNFSDS) != 0)
4702 dsp->nfsclds_flags |= NFSCLDS_DS;
4704 nd->nd_repstat = nfsrv_mtostr(nd,
4705 dsp->nfsclds_serverown, len);
4706 if (nd->nd_repstat == 0) {
4707 mtx_init(&dsp->nfsclds_mtx, "nfsds", NULL, MTX_DEF);
4708 mtx_init(&dsp->nfsclds_sess.nfsess_mtx, "nfssession",
4710 nfscl_initsessionslots(&dsp->nfsclds_sess);
4713 free(dsp, M_NFSCLDS);
4715 error = nd->nd_repstat;
4717 mbuf_freem(nd->nd_mrep);
4722 * Do the NFSv4.1 Create Session.
4725 nfsrpc_createsession(struct nfsmount *nmp, struct nfsclsession *sep,
4726 struct nfssockreq *nrp, uint32_t sequenceid, int mds, struct ucred *cred,
4729 uint32_t crflags, maxval, *tl;
4730 struct nfsrv_descript nfsd;
4731 struct nfsrv_descript *nd = &nfsd;
4734 /* Make sure nm_rsize, nm_wsize is set. */
4735 if (nmp->nm_rsize > NFS_MAXBSIZE || nmp->nm_rsize == 0)
4736 nmp->nm_rsize = NFS_MAXBSIZE;
4737 if (nmp->nm_wsize > NFS_MAXBSIZE || nmp->nm_wsize == 0)
4738 nmp->nm_wsize = NFS_MAXBSIZE;
4739 nfscl_reqstart(nd, NFSPROC_CREATESESSION, nmp, NULL, 0, NULL, NULL, 0,
4741 NFSM_BUILD(tl, uint32_t *, 4 * NFSX_UNSIGNED);
4742 *tl++ = sep->nfsess_clientid.lval[0];
4743 *tl++ = sep->nfsess_clientid.lval[1];
4744 *tl++ = txdr_unsigned(sequenceid);
4745 crflags = (NFSMNT_RDONLY(nmp->nm_mountp) ? 0 : NFSV4CRSESS_PERSIST);
4746 if (nfscl_enablecallb != 0 && nfs_numnfscbd > 0 && mds != 0)
4747 crflags |= NFSV4CRSESS_CONNBACKCHAN;
4748 *tl = txdr_unsigned(crflags);
4750 /* Fill in fore channel attributes. */
4751 NFSM_BUILD(tl, uint32_t *, 7 * NFSX_UNSIGNED);
4752 *tl++ = 0; /* Header pad size */
4753 *tl++ = txdr_unsigned(nmp->nm_wsize + NFS_MAXXDR);/* Max request size */
4754 *tl++ = txdr_unsigned(nmp->nm_rsize + NFS_MAXXDR);/* Max reply size */
4755 *tl++ = txdr_unsigned(4096); /* Max response size cached */
4756 *tl++ = txdr_unsigned(20); /* Max operations */
4757 *tl++ = txdr_unsigned(64); /* Max slots */
4758 *tl = 0; /* No rdma ird */
4760 /* Fill in back channel attributes. */
4761 NFSM_BUILD(tl, uint32_t *, 7 * NFSX_UNSIGNED);
4762 *tl++ = 0; /* Header pad size */
4763 *tl++ = txdr_unsigned(10000); /* Max request size */
4764 *tl++ = txdr_unsigned(10000); /* Max response size */
4765 *tl++ = txdr_unsigned(4096); /* Max response size cached */
4766 *tl++ = txdr_unsigned(4); /* Max operations */
4767 *tl++ = txdr_unsigned(NFSV4_CBSLOTS); /* Max slots */
4768 *tl = 0; /* No rdma ird */
4770 NFSM_BUILD(tl, uint32_t *, 8 * NFSX_UNSIGNED);
4771 *tl++ = txdr_unsigned(NFS_CALLBCKPROG); /* Call back prog # */
4773 /* Allow AUTH_SYS callbacks as uid, gid == 0. */
4774 *tl++ = txdr_unsigned(1); /* Auth_sys only */
4775 *tl++ = txdr_unsigned(AUTH_SYS); /* AUTH_SYS type */
4776 *tl++ = txdr_unsigned(nfsboottime.tv_sec); /* time stamp */
4777 *tl++ = 0; /* Null machine name */
4778 *tl++ = 0; /* Uid == 0 */
4779 *tl++ = 0; /* Gid == 0 */
4780 *tl = 0; /* No additional gids */
4781 nd->nd_flag |= ND_USEGSSNAME;
4782 error = newnfs_request(nd, nmp, NULL, nrp, NULL, p, cred, NFS_PROG,
4783 NFS_VER4, NULL, 1, NULL, NULL);
4786 if (nd->nd_repstat == 0) {
4787 NFSM_DISSECT(tl, uint32_t *, NFSX_V4SESSIONID +
4789 bcopy(tl, sep->nfsess_sessionid, NFSX_V4SESSIONID);
4790 tl += NFSX_V4SESSIONID / NFSX_UNSIGNED;
4791 sep->nfsess_sequenceid = fxdr_unsigned(uint32_t, *tl++);
4792 crflags = fxdr_unsigned(uint32_t, *tl);
4793 if ((crflags & NFSV4CRSESS_PERSIST) != 0 && mds != 0) {
4795 nmp->nm_state |= NFSSTA_SESSPERSIST;
4799 /* Get the fore channel slot count. */
4800 NFSM_DISSECT(tl, uint32_t *, 7 * NFSX_UNSIGNED);
4801 tl++; /* Skip the header pad size. */
4803 /* Make sure nm_wsize is small enough. */
4804 maxval = fxdr_unsigned(uint32_t, *tl++);
4805 while (maxval < nmp->nm_wsize + NFS_MAXXDR) {
4806 if (nmp->nm_wsize > 8096)
4812 /* Make sure nm_rsize is small enough. */
4813 maxval = fxdr_unsigned(uint32_t, *tl++);
4814 while (maxval < nmp->nm_rsize + NFS_MAXXDR) {
4815 if (nmp->nm_rsize > 8096)
4821 sep->nfsess_maxcache = fxdr_unsigned(int, *tl++);
4823 sep->nfsess_foreslots = fxdr_unsigned(uint16_t, *tl++);
4824 NFSCL_DEBUG(4, "fore slots=%d\n", (int)sep->nfsess_foreslots);
4825 irdcnt = fxdr_unsigned(int, *tl);
4827 NFSM_DISSECT(tl, uint32_t *, irdcnt * NFSX_UNSIGNED);
4829 /* and the back channel slot count. */
4830 NFSM_DISSECT(tl, uint32_t *, 7 * NFSX_UNSIGNED);
4832 sep->nfsess_backslots = fxdr_unsigned(uint16_t, *tl);
4833 NFSCL_DEBUG(4, "back slots=%d\n", (int)sep->nfsess_backslots);
4835 error = nd->nd_repstat;
4837 mbuf_freem(nd->nd_mrep);
4842 * Do the NFSv4.1 Destroy Session.
4845 nfsrpc_destroysession(struct nfsmount *nmp, struct nfsclclient *clp,
4846 struct ucred *cred, NFSPROC_T *p)
4849 struct nfsrv_descript nfsd;
4850 struct nfsrv_descript *nd = &nfsd;
4852 struct nfsclsession *tsep;
4854 nfscl_reqstart(nd, NFSPROC_DESTROYSESSION, nmp, NULL, 0, NULL, NULL, 0,
4856 NFSM_BUILD(tl, uint32_t *, NFSX_V4SESSIONID);
4857 tsep = nfsmnt_mdssession(nmp);
4858 bcopy(tsep->nfsess_sessionid, tl, NFSX_V4SESSIONID);
4859 nd->nd_flag |= ND_USEGSSNAME;
4860 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4861 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4864 error = nd->nd_repstat;
4865 mbuf_freem(nd->nd_mrep);
4870 * Do the NFSv4.1 Destroy Client.
4873 nfsrpc_destroyclient(struct nfsmount *nmp, struct nfsclclient *clp,
4874 struct ucred *cred, NFSPROC_T *p)
4877 struct nfsrv_descript nfsd;
4878 struct nfsrv_descript *nd = &nfsd;
4880 struct nfsclsession *tsep;
4882 nfscl_reqstart(nd, NFSPROC_DESTROYCLIENT, nmp, NULL, 0, NULL, NULL, 0,
4884 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_UNSIGNED);
4885 tsep = nfsmnt_mdssession(nmp);
4886 *tl++ = tsep->nfsess_clientid.lval[0];
4887 *tl = tsep->nfsess_clientid.lval[1];
4888 nd->nd_flag |= ND_USEGSSNAME;
4889 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4890 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4893 error = nd->nd_repstat;
4894 mbuf_freem(nd->nd_mrep);
4899 * Do the NFSv4.1 LayoutGet.
4902 nfsrpc_layoutget(struct nfsmount *nmp, uint8_t *fhp, int fhlen, int iomode,
4903 uint64_t offset, uint64_t len, uint64_t minlen, int layouttype,
4904 int layoutlen, nfsv4stateid_t *stateidp, int *retonclosep,
4905 struct nfsclflayouthead *flhp, struct ucred *cred, NFSPROC_T *p,
4908 struct nfsrv_descript nfsd, *nd = &nfsd;
4911 nfscl_reqstart(nd, NFSPROC_LAYOUTGET, nmp, fhp, fhlen, NULL, NULL, 0,
4913 nfsrv_setuplayoutget(nd, iomode, offset, len, minlen, stateidp,
4914 layouttype, layoutlen, 0);
4915 nd->nd_flag |= ND_USEGSSNAME;
4916 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4917 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4918 NFSCL_DEBUG(4, "layget err=%d st=%d\n", error, nd->nd_repstat);
4921 if (nd->nd_repstat == 0)
4922 error = nfsrv_parselayoutget(nd, stateidp, retonclosep, flhp);
4923 if (error == 0 && nd->nd_repstat != 0)
4924 error = nd->nd_repstat;
4925 mbuf_freem(nd->nd_mrep);
4930 * Do the NFSv4.1 Get Device Info.
4933 nfsrpc_getdeviceinfo(struct nfsmount *nmp, uint8_t *deviceid, int layouttype,
4934 uint32_t *notifybitsp, struct nfscldevinfo **ndip, struct ucred *cred,
4937 uint32_t cnt, *tl, vers, minorvers;
4938 struct nfsrv_descript nfsd;
4939 struct nfsrv_descript *nd = &nfsd;
4940 struct sockaddr_in sin, ssin;
4941 struct sockaddr_in6 sin6, ssin6;
4942 struct nfsclds *dsp = NULL, **dspp, **gotdspp;
4943 struct nfscldevinfo *ndi;
4944 int addrcnt = 0, bitcnt, error, gotvers, i, isudp, j, stripecnt;
4945 uint8_t stripeindex;
4946 sa_family_t af, safilled;
4951 nfscl_reqstart(nd, NFSPROC_GETDEVICEINFO, nmp, NULL, 0, NULL, NULL, 0,
4953 NFSM_BUILD(tl, uint32_t *, NFSX_V4DEVICEID + 3 * NFSX_UNSIGNED);
4954 NFSBCOPY(deviceid, tl, NFSX_V4DEVICEID);
4955 tl += (NFSX_V4DEVICEID / NFSX_UNSIGNED);
4956 *tl++ = txdr_unsigned(layouttype);
4957 *tl++ = txdr_unsigned(100000);
4958 if (notifybitsp != NULL && *notifybitsp != 0) {
4959 *tl = txdr_unsigned(1); /* One word of bits. */
4960 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
4961 *tl = txdr_unsigned(*notifybitsp);
4963 *tl = txdr_unsigned(0);
4964 nd->nd_flag |= ND_USEGSSNAME;
4965 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4966 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4969 if (nd->nd_repstat == 0) {
4970 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
4971 if (layouttype != fxdr_unsigned(int, *tl))
4972 printf("EEK! devinfo layout type not same!\n");
4973 if (layouttype == NFSLAYOUT_NFSV4_1_FILES) {
4974 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
4975 stripecnt = fxdr_unsigned(int, *tl);
4976 NFSCL_DEBUG(4, "stripecnt=%d\n", stripecnt);
4977 if (stripecnt < 1 || stripecnt > 4096) {
4978 printf("pNFS File layout devinfo stripecnt %d:"
4979 " out of range\n", stripecnt);
4980 error = NFSERR_BADXDR;
4983 NFSM_DISSECT(tl, uint32_t *, (stripecnt + 1) *
4985 addrcnt = fxdr_unsigned(int, *(tl + stripecnt));
4986 NFSCL_DEBUG(4, "addrcnt=%d\n", addrcnt);
4987 if (addrcnt < 1 || addrcnt > 128) {
4988 printf("NFS devinfo addrcnt %d: out of range\n",
4990 error = NFSERR_BADXDR;
4995 * Now we know how many stripe indices and addresses, so
4996 * we can allocate the structure the correct size.
4998 i = (stripecnt * sizeof(uint8_t)) /
4999 sizeof(struct nfsclds *) + 1;
5000 NFSCL_DEBUG(4, "stripeindices=%d\n", i);
5001 ndi = malloc(sizeof(*ndi) + (addrcnt + i) *
5002 sizeof(struct nfsclds *), M_NFSDEVINFO, M_WAITOK |
5004 NFSBCOPY(deviceid, ndi->nfsdi_deviceid,
5006 ndi->nfsdi_refcnt = 0;
5007 ndi->nfsdi_flags = NFSDI_FILELAYOUT;
5008 ndi->nfsdi_stripecnt = stripecnt;
5009 ndi->nfsdi_addrcnt = addrcnt;
5010 /* Fill in the stripe indices. */
5011 for (i = 0; i < stripecnt; i++) {
5012 stripeindex = fxdr_unsigned(uint8_t, *tl++);
5013 NFSCL_DEBUG(4, "stripeind=%d\n", stripeindex);
5014 if (stripeindex >= addrcnt) {
5015 printf("pNFS File Layout devinfo"
5016 " stripeindex %d: too big\n",
5018 error = NFSERR_BADXDR;
5021 nfsfldi_setstripeindex(ndi, i, stripeindex);
5023 } else if (layouttype == NFSLAYOUT_FLEXFILE) {
5024 /* For Flex File, we only get one address list. */
5025 ndi = malloc(sizeof(*ndi) + sizeof(struct nfsclds *),
5026 M_NFSDEVINFO, M_WAITOK | M_ZERO);
5027 NFSBCOPY(deviceid, ndi->nfsdi_deviceid,
5029 ndi->nfsdi_refcnt = 0;
5030 ndi->nfsdi_flags = NFSDI_FLEXFILE;
5031 addrcnt = ndi->nfsdi_addrcnt = 1;
5034 /* Now, dissect the server address(es). */
5035 safilled = AF_UNSPEC;
5036 for (i = 0; i < addrcnt; i++) {
5037 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
5038 cnt = fxdr_unsigned(uint32_t, *tl);
5040 printf("NFS devinfo 0 len addrlist\n");
5041 error = NFSERR_BADXDR;
5044 dspp = nfsfldi_addr(ndi, i);
5045 safilled = AF_UNSPEC;
5046 for (j = 0; j < cnt; j++) {
5047 error = nfsv4_getipaddr(nd, &sin, &sin6, &af,
5049 if (error != 0 && error != EPERM) {
5050 error = NFSERR_BADXDR;
5053 if (error == 0 && isudp == 0) {
5056 * - Same address family.
5057 * Save the address and dspp, so that
5058 * the connection can be done after
5059 * parsing is complete.
5061 if (safilled == AF_UNSPEC ||
5062 (af == nmp->nm_nam->sa_family &&
5063 safilled != nmp->nm_nam->sa_family)
5076 gotvers = NFS_VER4; /* Always NFSv4 for File Layout. */
5077 /* For Flex File, we will take one of the versions to use. */
5078 if (layouttype == NFSLAYOUT_FLEXFILE) {
5079 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
5080 j = fxdr_unsigned(int, *tl);
5081 if (j < 1 || j > NFSDEV_MAXVERS) {
5082 printf("pNFS: too many versions\n");
5083 error = NFSERR_BADXDR;
5087 for (i = 0; i < j; i++) {
5088 NFSM_DISSECT(tl, uint32_t *, 5 * NFSX_UNSIGNED);
5089 vers = fxdr_unsigned(uint32_t, *tl++);
5090 minorvers = fxdr_unsigned(uint32_t, *tl++);
5091 if ((vers == NFS_VER4 && minorvers ==
5092 NFSV41_MINORVERSION) || (vers == NFS_VER3 &&
5095 /* We'll take this one. */
5096 ndi->nfsdi_versindex = i;
5097 ndi->nfsdi_vers = vers;
5098 ndi->nfsdi_minorvers = minorvers;
5099 ndi->nfsdi_rsize = fxdr_unsigned(
5101 ndi->nfsdi_wsize = fxdr_unsigned(
5103 if (*tl == newnfs_true)
5108 ~NFSDI_TIGHTCOUPLED;
5112 printf("pNFS: no NFSv3 or NFSv4.1\n");
5113 error = NFSERR_BADXDR;
5118 /* And the notify bits. */
5119 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
5120 bitcnt = fxdr_unsigned(int, *tl);
5122 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
5123 if (notifybitsp != NULL)
5125 fxdr_unsigned(uint32_t, *tl);
5127 if (safilled != AF_UNSPEC) {
5128 KASSERT(ndi != NULL, ("ndi is NULL"));
5134 * Now we can do a TCP connection for the correct
5135 * NFS version and IP address.
5137 error = nfsrpc_fillsa(nmp, &ssin, &ssin6, safilled,
5141 KASSERT(gotdspp != NULL, ("gotdspp is NULL"));
5145 if (nd->nd_repstat != 0 && error == 0)
5146 error = nd->nd_repstat;
5148 if (error != 0 && ndi != NULL)
5149 nfscl_freedevinfo(ndi);
5150 mbuf_freem(nd->nd_mrep);
5155 * Do the NFSv4.1 LayoutCommit.
5158 nfsrpc_layoutcommit(struct nfsmount *nmp, uint8_t *fh, int fhlen, int reclaim,
5159 uint64_t off, uint64_t len, uint64_t lastbyte, nfsv4stateid_t *stateidp,
5160 int layouttype, struct ucred *cred, NFSPROC_T *p, void *stuff)
5163 struct nfsrv_descript nfsd, *nd = &nfsd;
5166 nfscl_reqstart(nd, NFSPROC_LAYOUTCOMMIT, nmp, fh, fhlen, NULL, NULL,
5168 NFSM_BUILD(tl, uint32_t *, 5 * NFSX_UNSIGNED + 3 * NFSX_HYPER +
5170 txdr_hyper(off, tl);
5172 txdr_hyper(len, tl);
5175 *tl++ = newnfs_true;
5177 *tl++ = newnfs_false;
5178 *tl++ = txdr_unsigned(stateidp->seqid);
5179 *tl++ = stateidp->other[0];
5180 *tl++ = stateidp->other[1];
5181 *tl++ = stateidp->other[2];
5182 *tl++ = newnfs_true;
5185 else if (lastbyte >= (off + len))
5186 lastbyte = off + len - 1;
5187 txdr_hyper(lastbyte, tl);
5189 *tl++ = newnfs_false;
5190 *tl++ = txdr_unsigned(layouttype);
5191 /* All supported layouts are 0 length. */
5192 *tl = txdr_unsigned(0);
5193 nd->nd_flag |= ND_USEGSSNAME;
5194 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
5195 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
5198 error = nd->nd_repstat;
5199 mbuf_freem(nd->nd_mrep);
5204 * Do the NFSv4.1 LayoutReturn.
5207 nfsrpc_layoutreturn(struct nfsmount *nmp, uint8_t *fh, int fhlen, int reclaim,
5208 int layouttype, uint32_t iomode, int layoutreturn, uint64_t offset,
5209 uint64_t len, nfsv4stateid_t *stateidp, struct ucred *cred, NFSPROC_T *p,
5210 uint32_t stat, uint32_t op, char *devid)
5213 struct nfsrv_descript nfsd, *nd = &nfsd;
5217 nfscl_reqstart(nd, NFSPROC_LAYOUTRETURN, nmp, fh, fhlen, NULL, NULL,
5219 NFSM_BUILD(tl, uint32_t *, 4 * NFSX_UNSIGNED);
5221 *tl++ = newnfs_true;
5223 *tl++ = newnfs_false;
5224 *tl++ = txdr_unsigned(layouttype);
5225 *tl++ = txdr_unsigned(iomode);
5226 *tl = txdr_unsigned(layoutreturn);
5227 if (layoutreturn == NFSLAYOUTRETURN_FILE) {
5228 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_HYPER + NFSX_STATEID +
5230 txdr_hyper(offset, tl);
5232 txdr_hyper(len, tl);
5234 NFSCL_DEBUG(4, "layoutret stseq=%d\n", (int)stateidp->seqid);
5235 *tl++ = txdr_unsigned(stateidp->seqid);
5236 *tl++ = stateidp->other[0];
5237 *tl++ = stateidp->other[1];
5238 *tl++ = stateidp->other[2];
5239 if (layouttype == NFSLAYOUT_NFSV4_1_FILES)
5240 *tl = txdr_unsigned(0);
5241 else if (layouttype == NFSLAYOUT_FLEXFILE) {
5243 *tl = txdr_unsigned(2 * NFSX_HYPER +
5244 NFSX_STATEID + NFSX_V4DEVICEID + 5 *
5246 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_HYPER +
5247 NFSX_STATEID + NFSX_V4DEVICEID + 5 *
5249 *tl++ = txdr_unsigned(1); /* One error. */
5250 tu64 = 0; /* Offset. */
5251 txdr_hyper(tu64, tl); tl += 2;
5252 tu64 = UINT64_MAX; /* Length. */
5253 txdr_hyper(tu64, tl); tl += 2;
5254 NFSBCOPY(stateidp, tl, NFSX_STATEID);
5255 tl += (NFSX_STATEID / NFSX_UNSIGNED);
5256 *tl++ = txdr_unsigned(1); /* One error. */
5257 NFSBCOPY(devid, tl, NFSX_V4DEVICEID);
5258 tl += (NFSX_V4DEVICEID / NFSX_UNSIGNED);
5259 *tl++ = txdr_unsigned(stat);
5260 *tl++ = txdr_unsigned(op);
5262 *tl = txdr_unsigned(2 * NFSX_UNSIGNED);
5263 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_UNSIGNED);
5267 *tl = 0; /* No stats yet. */
5270 nd->nd_flag |= ND_USEGSSNAME;
5271 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
5272 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
5275 if (nd->nd_repstat == 0) {
5276 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
5278 NFSM_DISSECT(tl, uint32_t *, NFSX_STATEID);
5279 stateidp->seqid = fxdr_unsigned(uint32_t, *tl++);
5280 stateidp->other[0] = *tl++;
5281 stateidp->other[1] = *tl++;
5282 stateidp->other[2] = *tl;
5285 error = nd->nd_repstat;
5287 mbuf_freem(nd->nd_mrep);
5292 * Acquire a layout and devinfo, if possible. The caller must have acquired
5293 * a reference count on the nfsclclient structure before calling this.
5294 * Return the layout in lypp with a reference count on it, if successful.
5297 nfsrpc_getlayout(struct nfsmount *nmp, vnode_t vp, struct nfsfh *nfhp,
5298 int iomode, uint32_t *notifybitsp, nfsv4stateid_t *stateidp, uint64_t off,
5299 struct nfscllayout **lypp, struct ucred *cred, NFSPROC_T *p)
5301 struct nfscllayout *lyp;
5302 struct nfsclflayout *flp;
5303 struct nfsclflayouthead flh;
5304 int error = 0, islocked, layoutlen, layouttype, recalled, retonclose;
5305 nfsv4stateid_t stateid;
5306 struct nfsclsession *tsep;
5309 if (NFSHASFLEXFILE(nmp))
5310 layouttype = NFSLAYOUT_FLEXFILE;
5312 layouttype = NFSLAYOUT_NFSV4_1_FILES;
5314 * If lyp is returned non-NULL, there will be a refcnt (shared lock)
5315 * on it, iff flp != NULL or a lock (exclusive lock) on it iff
5318 lyp = nfscl_getlayout(nmp->nm_clp, nfhp->nfh_fh, nfhp->nfh_len,
5319 off, &flp, &recalled);
5321 if (lyp == NULL || flp == NULL) {
5325 tsep = nfsmnt_mdssession(nmp);
5326 layoutlen = tsep->nfsess_maxcache -
5327 (NFSX_STATEID + 3 * NFSX_UNSIGNED);
5330 stateid.other[0] = stateidp->other[0];
5331 stateid.other[1] = stateidp->other[1];
5332 stateid.other[2] = stateidp->other[2];
5333 error = nfsrpc_layoutget(nmp, nfhp->nfh_fh,
5334 nfhp->nfh_len, iomode, (uint64_t)0, UINT64_MAX,
5335 (uint64_t)0, layouttype, layoutlen, &stateid,
5336 &retonclose, &flh, cred, p, NULL);
5339 stateid.seqid = lyp->nfsly_stateid.seqid;
5340 stateid.other[0] = lyp->nfsly_stateid.other[0];
5341 stateid.other[1] = lyp->nfsly_stateid.other[1];
5342 stateid.other[2] = lyp->nfsly_stateid.other[2];
5343 error = nfsrpc_layoutget(nmp, nfhp->nfh_fh,
5344 nfhp->nfh_len, iomode, off, UINT64_MAX,
5345 (uint64_t)0, layouttype, layoutlen, &stateid,
5346 &retonclose, &flh, cred, p, NULL);
5348 error = nfsrpc_layoutgetres(nmp, vp, nfhp->nfh_fh,
5349 nfhp->nfh_len, &stateid, retonclose, notifybitsp, &lyp,
5350 &flh, layouttype, error, NULL, cred, p);
5353 else if (islocked != 0)
5354 nfscl_rellayout(lyp, 1);
5361 * Do a TCP connection plus exchange id and create session.
5362 * If successful, a "struct nfsclds" is linked into the list for the
5363 * mount point and a pointer to it is returned.
5366 nfsrpc_fillsa(struct nfsmount *nmp, struct sockaddr_in *sin,
5367 struct sockaddr_in6 *sin6, sa_family_t af, int vers, struct nfsclds **dspp,
5370 struct sockaddr_in *msad, *sad;
5371 struct sockaddr_in6 *msad6, *sad6;
5372 struct nfsclclient *clp;
5373 struct nfssockreq *nrp;
5374 struct nfsclds *dsp, *tdsp;
5376 enum nfsclds_state retv;
5377 uint32_t sequenceid;
5379 KASSERT(nmp->nm_sockreq.nr_cred != NULL,
5380 ("nfsrpc_fillsa: NULL nr_cred"));
5386 if (af == AF_INET) {
5389 * Check to see if we already have a session for this
5390 * address that is usable for a DS.
5391 * Note that the MDS's address is in a different place
5392 * than the sessions already acquired for DS's.
5394 msad = (struct sockaddr_in *)nmp->nm_sockreq.nr_nam;
5395 tdsp = TAILQ_FIRST(&nmp->nm_sess);
5396 while (tdsp != NULL) {
5397 if (msad != NULL && msad->sin_family == AF_INET &&
5398 sin->sin_addr.s_addr == msad->sin_addr.s_addr &&
5399 sin->sin_port == msad->sin_port &&
5400 (tdsp->nfsclds_flags & NFSCLDS_DS) != 0 &&
5401 tdsp->nfsclds_sess.nfsess_defunct == 0) {
5404 NFSCL_DEBUG(4, "fnd same addr\n");
5407 tdsp = TAILQ_NEXT(tdsp, nfsclds_list);
5408 if (tdsp != NULL && tdsp->nfsclds_sockp != NULL)
5409 msad = (struct sockaddr_in *)
5410 tdsp->nfsclds_sockp->nr_nam;
5416 /* No IP address match, so look for new/trunked one. */
5417 sad = malloc(sizeof(*sad), M_SONAME, M_WAITOK | M_ZERO);
5418 sad->sin_len = sizeof(*sad);
5419 sad->sin_family = AF_INET;
5420 sad->sin_port = sin->sin_port;
5421 sad->sin_addr.s_addr = sin->sin_addr.s_addr;
5422 nrp = malloc(sizeof(*nrp), M_NFSSOCKREQ, M_WAITOK | M_ZERO);
5423 nrp->nr_nam = (struct sockaddr *)sad;
5424 } else if (af == AF_INET6) {
5427 * Check to see if we already have a session for this
5428 * address that is usable for a DS.
5429 * Note that the MDS's address is in a different place
5430 * than the sessions already acquired for DS's.
5432 msad6 = (struct sockaddr_in6 *)nmp->nm_sockreq.nr_nam;
5433 tdsp = TAILQ_FIRST(&nmp->nm_sess);
5434 while (tdsp != NULL) {
5435 if (msad6 != NULL && msad6->sin6_family == AF_INET6 &&
5436 IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr,
5437 &msad6->sin6_addr) &&
5438 sin6->sin6_port == msad6->sin6_port &&
5439 (tdsp->nfsclds_flags & NFSCLDS_DS) != 0 &&
5440 tdsp->nfsclds_sess.nfsess_defunct == 0) {
5445 tdsp = TAILQ_NEXT(tdsp, nfsclds_list);
5446 if (tdsp != NULL && tdsp->nfsclds_sockp != NULL)
5447 msad6 = (struct sockaddr_in6 *)
5448 tdsp->nfsclds_sockp->nr_nam;
5454 /* No IP address match, so look for new/trunked one. */
5455 sad6 = malloc(sizeof(*sad6), M_SONAME, M_WAITOK | M_ZERO);
5456 sad6->sin6_len = sizeof(*sad6);
5457 sad6->sin6_family = AF_INET6;
5458 sad6->sin6_port = sin6->sin6_port;
5459 NFSBCOPY(&sin6->sin6_addr, &sad6->sin6_addr,
5460 sizeof(struct in6_addr));
5461 nrp = malloc(sizeof(*nrp), M_NFSSOCKREQ, M_WAITOK | M_ZERO);
5462 nrp->nr_nam = (struct sockaddr *)sad6;
5466 nrp->nr_sotype = SOCK_STREAM;
5467 mtx_init(&nrp->nr_mtx, "nfssock", NULL, MTX_DEF);
5468 nrp->nr_prog = NFS_PROG;
5469 nrp->nr_vers = vers;
5472 * Use the credentials that were used for the mount, which are
5473 * in nmp->nm_sockreq.nr_cred for newnfs_connect() etc.
5474 * Ref. counting the credentials with crhold() is probably not
5475 * necessary, since nm_sockreq.nr_cred won't be crfree()'d until
5476 * unmount, but I did it anyhow.
5478 nrp->nr_cred = crhold(nmp->nm_sockreq.nr_cred);
5479 error = newnfs_connect(nmp, nrp, NULL, p, 0);
5480 NFSCL_DEBUG(3, "DS connect=%d\n", error);
5483 /* Now, do the exchangeid and create session. */
5485 if (vers == NFS_VER4) {
5486 error = nfsrpc_exchangeid(nmp, clp, nrp,
5487 NFSV4EXCH_USEPNFSDS, &dsp, nrp->nr_cred, p);
5488 NFSCL_DEBUG(3, "DS exchangeid=%d\n", error);
5490 newnfs_disconnect(nrp);
5492 dsp = malloc(sizeof(struct nfsclds), M_NFSCLDS,
5494 dsp->nfsclds_flags |= NFSCLDS_DS;
5495 dsp->nfsclds_expire = INT32_MAX; /* No renews needed. */
5496 mtx_init(&dsp->nfsclds_mtx, "nfsds", NULL, MTX_DEF);
5497 mtx_init(&dsp->nfsclds_sess.nfsess_mtx, "nfssession",
5502 dsp->nfsclds_sockp = nrp;
5503 if (vers == NFS_VER4) {
5505 retv = nfscl_getsameserver(nmp, dsp, &tdsp,
5507 NFSCL_DEBUG(3, "getsame ret=%d\n", retv);
5508 if (retv == NFSDSP_USETHISSESSION &&
5509 nfscl_dssameconn != 0) {
5511 tdsp->nfsclds_flags |= NFSCLDS_SAMECONN;
5515 * If there is already a session for this
5518 (void)newnfs_disconnect(nrp);
5519 nfscl_freenfsclds(dsp);
5523 if (retv == NFSDSP_NOTFOUND)
5525 dsp->nfsclds_sess.nfsess_sequenceid;
5527 error = nfsrpc_createsession(nmp, &dsp->nfsclds_sess,
5528 nrp, sequenceid, 0, nrp->nr_cred, p);
5529 NFSCL_DEBUG(3, "DS createsess=%d\n", error);
5532 NFSFREECRED(nrp->nr_cred);
5533 NFSFREEMUTEX(&nrp->nr_mtx);
5534 free(nrp->nr_nam, M_SONAME);
5535 free(nrp, M_NFSSOCKREQ);
5538 NFSCL_DEBUG(3, "add DS session\n");
5540 * Put it at the end of the list. That way the list
5541 * is ordered by when the entry was added. This matters
5542 * since the one done first is the one that should be
5543 * used for sequencid'ing any subsequent create sessions.
5546 TAILQ_INSERT_TAIL(&nmp->nm_sess, dsp, nfsclds_list);
5549 } else if (dsp != NULL) {
5550 newnfs_disconnect(nrp);
5551 nfscl_freenfsclds(dsp);
5557 * Do the NFSv4.1 Reclaim Complete.
5560 nfsrpc_reclaimcomplete(struct nfsmount *nmp, struct ucred *cred, NFSPROC_T *p)
5563 struct nfsrv_descript nfsd;
5564 struct nfsrv_descript *nd = &nfsd;
5567 nfscl_reqstart(nd, NFSPROC_RECLAIMCOMPL, nmp, NULL, 0, NULL, NULL, 0,
5569 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
5571 nd->nd_flag |= ND_USEGSSNAME;
5572 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
5573 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
5576 error = nd->nd_repstat;
5577 mbuf_freem(nd->nd_mrep);
5582 * Initialize the slot tables for a session.
5585 nfscl_initsessionslots(struct nfsclsession *sep)
5589 for (i = 0; i < NFSV4_CBSLOTS; i++) {
5590 if (sep->nfsess_cbslots[i].nfssl_reply != NULL)
5591 m_freem(sep->nfsess_cbslots[i].nfssl_reply);
5592 NFSBZERO(&sep->nfsess_cbslots[i], sizeof(struct nfsslot));
5594 for (i = 0; i < 64; i++)
5595 sep->nfsess_slotseq[i] = 0;
5596 sep->nfsess_slots = 0;
5600 * Called to try and do an I/O operation via an NFSv4.1 Data Server (DS).
5603 nfscl_doiods(vnode_t vp, struct uio *uiop, int *iomode, int *must_commit,
5604 uint32_t rwaccess, int docommit, struct ucred *cred, NFSPROC_T *p)
5606 struct nfsnode *np = VTONFS(vp);
5607 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
5608 struct nfscllayout *layp;
5609 struct nfscldevinfo *dip;
5610 struct nfsclflayout *rflp;
5612 struct nfsclwritedsdorpc *drpc, *tdrpc;
5613 nfsv4stateid_t stateid;
5614 struct ucred *newcred;
5615 uint64_t lastbyte, len, off, oresid, xfer;
5616 int eof, error, firstmirror, i, iolaymode, mirrorcnt, recalled, timo;
5624 if (!NFSHASPNFS(nmp) || nfscl_enablecallb == 0 || nfs_numnfscbd == 0 ||
5625 (np->n_flag & NNOLAYOUT) != 0)
5627 /* Now, get a reference cnt on the clientid for this mount. */
5628 if (nfscl_getref(nmp) == 0)
5631 /* Find an appropriate stateid. */
5632 newcred = NFSNEWCRED(cred);
5633 error = nfscl_getstateid(vp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len,
5634 rwaccess, 1, newcred, p, &stateid, &lckp);
5636 NFSFREECRED(newcred);
5640 /* Search for a layout for this file. */
5641 off = uiop->uio_offset;
5642 layp = nfscl_getlayout(nmp->nm_clp, np->n_fhp->nfh_fh,
5643 np->n_fhp->nfh_len, off, &rflp, &recalled);
5644 if (layp == NULL || rflp == NULL) {
5645 if (recalled != 0) {
5646 NFSFREECRED(newcred);
5651 nfscl_rellayout(layp, (rflp == NULL) ? 1 : 0);
5654 /* Try and get a Layout, if it is supported. */
5655 if (rwaccess == NFSV4OPEN_ACCESSWRITE ||
5656 (np->n_flag & NWRITEOPENED) != 0)
5657 iolaymode = NFSLAYOUTIOMODE_RW;
5659 iolaymode = NFSLAYOUTIOMODE_READ;
5660 error = nfsrpc_getlayout(nmp, vp, np->n_fhp, iolaymode,
5661 NULL, &stateid, off, &layp, newcred, p);
5664 np->n_flag |= NNOLAYOUT;
5667 nfscl_lockderef(lckp);
5668 NFSFREECRED(newcred);
5670 nfscl_rellayout(layp, 0);
5677 * Loop around finding a layout that works for the first part of
5678 * this I/O operation, and then call the function that actually
5682 len = (uint64_t)uiop->uio_resid;
5683 while (len > 0 && error == 0 && eof == 0) {
5684 off = uiop->uio_offset;
5685 error = nfscl_findlayoutforio(layp, off, rwaccess, &rflp);
5687 oresid = xfer = (uint64_t)uiop->uio_resid;
5688 if (xfer > (rflp->nfsfl_end - rflp->nfsfl_off))
5689 xfer = rflp->nfsfl_end - rflp->nfsfl_off;
5691 * For Flex File layout with mirrored DSs, select one
5692 * of them at random for reads. For writes and commits,
5699 if ((layp->nfsly_flags & NFSLY_FLEXFILE) != 0 &&
5700 (mirrorcnt = rflp->nfsfl_mirrorcnt) > 1) {
5701 if (rwaccess == NFSV4OPEN_ACCESSREAD) {
5702 firstmirror = arc4random() % mirrorcnt;
5703 mirrorcnt = firstmirror + 1;
5705 if (docommit == 0) {
5707 * Save values, so uiop can be
5708 * rolled back upon a write
5711 offs = uiop->uio_offset;
5712 resid = uiop->uio_resid;
5714 uiop->uio_iov->iov_base;
5715 iovlen = uiop->uio_iov->iov_len;
5716 m = nfsm_uiombuflist(uiop, len,
5719 tdrpc = drpc = malloc(sizeof(*drpc) *
5720 (mirrorcnt - 1), M_TEMP, M_WAITOK |
5724 for (i = firstmirror; i < mirrorcnt && error == 0; i++){
5725 if ((layp->nfsly_flags & NFSLY_FLEXFILE) != 0)
5726 dev = rflp->nfsfl_ffm[i].dev;
5728 dev = rflp->nfsfl_dev;
5729 dip = nfscl_getdevinfo(nmp->nm_clp, dev,
5732 if ((rflp->nfsfl_flags & NFSFL_FLEXFILE)
5734 error = nfscl_dofflayoutio(vp,
5735 uiop, iomode, must_commit,
5736 &eof, &stateid, rwaccess,
5737 dip, layp, rflp, off, xfer,
5738 i, docommit, m, tdrpc,
5741 error = nfscl_doflayoutio(vp,
5742 uiop, iomode, must_commit,
5743 &eof, &stateid, rwaccess,
5744 dip, layp, rflp, off, xfer,
5745 docommit, newcred, p);
5746 nfscl_reldevinfo(dip);
5754 timo = hz / 50; /* Wait for 20msec. */
5757 for (i = firstmirror; i < mirrorcnt - 1 &&
5758 tdrpc != NULL; i++, tdrpc++) {
5760 * For the unused drpc entries, both inprog and
5761 * err == 0, so this loop won't break.
5763 while (tdrpc->inprog != 0 && tdrpc->done == 0)
5764 tsleep(&tdrpc->tsk, PVFS, "clrpcio",
5766 if (error == 0 && tdrpc->err != 0)
5771 if (mirrorcnt > 1 && rwaccess ==
5772 NFSV4OPEN_ACCESSWRITE && docommit == 0) {
5774 layp->nfsly_flags |= NFSLY_WRITTEN;
5777 lastbyte = off + xfer - 1;
5779 if (lastbyte > layp->nfsly_lastbyte)
5780 layp->nfsly_lastbyte = lastbyte;
5782 } else if (error == NFSERR_OPENMODE &&
5783 rwaccess == NFSV4OPEN_ACCESSREAD) {
5785 nmp->nm_state |= NFSSTA_OPENMODE;
5790 len -= (oresid - (uint64_t)uiop->uio_resid);
5791 else if (mirrorcnt > 1 && rwaccess ==
5792 NFSV4OPEN_ACCESSWRITE && docommit == 0) {
5794 * In case the rpc gets retried, roll the
5795 * uio fields changed by nfsm_uiombuflist()
5798 uiop->uio_offset = offs;
5799 uiop->uio_resid = resid;
5800 uiop->uio_iov->iov_base = iovbase;
5801 uiop->uio_iov->iov_len = iovlen;
5806 nfscl_lockderef(lckp);
5807 NFSFREECRED(newcred);
5808 nfscl_rellayout(layp, 0);
5814 * Make a copy of the mbuf chain and add an mbuf for null padding, as required.
5816 static struct mbuf *
5817 nfsm_copym(struct mbuf *m, int off, int xfer)
5819 struct mbuf *m2, *m3, *m4;
5823 m2 = m_copym(m, off, xfer, M_WAITOK);
5824 rem = NFSM_RNDUP(xfer) - xfer;
5827 * The zero padding to a multiple of 4 bytes is required by
5828 * the XDR. So that the mbufs copied by reference aren't
5829 * modified, add an mbuf with the zero'd bytes to the list.
5830 * rem will be a maximum of 3, so one zero'd uint32_t is
5834 while (m3->m_next != NULL)
5837 tl = NFSMTOD(m4, uint32_t *);
5839 mbuf_setlen(m4, rem);
5840 mbuf_setnext(m3, m4);
5846 * Find a file layout that will handle the first bytes of the requested
5847 * range and return the information from it needed to the I/O operation.
5850 nfscl_findlayoutforio(struct nfscllayout *lyp, uint64_t off, uint32_t rwaccess,
5851 struct nfsclflayout **retflpp)
5853 struct nfsclflayout *flp, *nflp, *rflp;
5858 /* For reading, do the Read list first and then the Write list. */
5860 if (rw == NFSV4OPEN_ACCESSREAD)
5861 flp = LIST_FIRST(&lyp->nfsly_flayread);
5863 flp = LIST_FIRST(&lyp->nfsly_flayrw);
5864 while (flp != NULL) {
5865 nflp = LIST_NEXT(flp, nfsfl_list);
5866 if (flp->nfsfl_off > off)
5868 if (flp->nfsfl_end > off &&
5869 (rflp == NULL || rflp->nfsfl_end < flp->nfsfl_end))
5873 if (rw == NFSV4OPEN_ACCESSREAD)
5874 rw = NFSV4OPEN_ACCESSWRITE;
5879 /* This one covers the most bytes starting at off. */
5887 * Do I/O using an NFSv4.1 file layout.
5890 nfscl_doflayoutio(vnode_t vp, struct uio *uiop, int *iomode, int *must_commit,
5891 int *eofp, nfsv4stateid_t *stateidp, int rwflag, struct nfscldevinfo *dp,
5892 struct nfscllayout *lyp, struct nfsclflayout *flp, uint64_t off,
5893 uint64_t len, int docommit, struct ucred *cred, NFSPROC_T *p)
5895 uint64_t io_off, rel_off, stripe_unit_size, transfer, xfer;
5896 int commit_thru_mds, error, stripe_index, stripe_pos;
5899 struct nfsclds **dspp;
5902 rel_off = off - flp->nfsfl_patoff;
5903 stripe_unit_size = (flp->nfsfl_util >> 6) & 0x3ffffff;
5904 stripe_pos = (rel_off / stripe_unit_size + flp->nfsfl_stripe1) %
5905 dp->nfsdi_stripecnt;
5906 transfer = stripe_unit_size - (rel_off % stripe_unit_size);
5909 /* Loop around, doing I/O for each stripe unit. */
5910 while (len > 0 && error == 0) {
5911 stripe_index = nfsfldi_stripeindex(dp, stripe_pos);
5912 dspp = nfsfldi_addr(dp, stripe_index);
5913 if (len > transfer && docommit == 0)
5917 if ((flp->nfsfl_util & NFSFLAYUTIL_DENSE) != 0) {
5919 if (stripe_pos >= flp->nfsfl_fhcnt)
5921 fhp = flp->nfsfl_fh[stripe_pos];
5922 io_off = (rel_off / (stripe_unit_size *
5923 dp->nfsdi_stripecnt)) * stripe_unit_size +
5924 rel_off % stripe_unit_size;
5926 /* Sparse layout. */
5927 if (flp->nfsfl_fhcnt > 1) {
5928 if (stripe_index >= flp->nfsfl_fhcnt)
5930 fhp = flp->nfsfl_fh[stripe_index];
5931 } else if (flp->nfsfl_fhcnt == 1)
5932 fhp = flp->nfsfl_fh[0];
5937 if ((flp->nfsfl_util & NFSFLAYUTIL_COMMIT_THRU_MDS) != 0) {
5938 commit_thru_mds = 1;
5942 commit_thru_mds = 0;
5943 mtx_lock(&np->n_mtx);
5944 np->n_flag |= NDSCOMMIT;
5945 mtx_unlock(&np->n_mtx);
5947 if (docommit != 0) {
5949 error = nfsrpc_commitds(vp, io_off, xfer,
5950 *dspp, fhp, 0, 0, cred, p);
5953 * Set both eof and uio_resid = 0 to end any
5957 uiop->uio_resid = 0;
5959 mtx_lock(&np->n_mtx);
5960 np->n_flag &= ~NDSCOMMIT;
5961 mtx_unlock(&np->n_mtx);
5963 } else if (rwflag == NFSV4OPEN_ACCESSREAD)
5964 error = nfsrpc_readds(vp, uiop, stateidp, eofp, *dspp,
5965 io_off, xfer, fhp, 0, 0, 0, cred, p);
5967 error = nfsrpc_writeds(vp, uiop, iomode, must_commit,
5968 stateidp, *dspp, io_off, xfer, fhp, commit_thru_mds,
5972 lyp->nfsly_flags |= NFSLY_WRITTEN;
5977 transfer = stripe_unit_size;
5978 stripe_pos = (stripe_pos + 1) % dp->nfsdi_stripecnt;
5987 * Do I/O using an NFSv4.1 flex file layout.
5990 nfscl_dofflayoutio(vnode_t vp, struct uio *uiop, int *iomode, int *must_commit,
5991 int *eofp, nfsv4stateid_t *stateidp, int rwflag, struct nfscldevinfo *dp,
5992 struct nfscllayout *lyp, struct nfsclflayout *flp, uint64_t off,
5993 uint64_t len, int mirror, int docommit, struct mbuf *mp,
5994 struct nfsclwritedsdorpc *drpc, struct ucred *cred, NFSPROC_T *p)
5996 uint64_t transfer, xfer;
6000 struct nfsclds **dspp;
6001 struct ucred *tcred;
6007 NFSCL_DEBUG(4, "nfscl_dofflayoutio: off=%ju len=%ju\n", (uintmax_t)off,
6009 /* Loop around, doing I/O for each stripe unit. */
6010 while (len > 0 && error == 0) {
6011 dspp = nfsfldi_addr(dp, 0);
6012 fhp = flp->nfsfl_ffm[mirror].fh[dp->nfsdi_versindex];
6013 stateidp = &flp->nfsfl_ffm[mirror].st;
6014 NFSCL_DEBUG(4, "mirror=%d vind=%d fhlen=%d st.seqid=0x%x\n",
6015 mirror, dp->nfsdi_versindex, fhp->nfh_len, stateidp->seqid);
6016 if ((dp->nfsdi_flags & NFSDI_TIGHTCOUPLED) == 0) {
6017 tcred = NFSNEWCRED(cred);
6018 tcred->cr_uid = flp->nfsfl_ffm[mirror].user;
6019 tcred->cr_groups[0] = flp->nfsfl_ffm[mirror].group;
6020 tcred->cr_ngroups = 1;
6023 if (rwflag == NFSV4OPEN_ACCESSREAD)
6024 transfer = dp->nfsdi_rsize;
6026 transfer = dp->nfsdi_wsize;
6027 mtx_lock(&np->n_mtx);
6028 np->n_flag |= NDSCOMMIT;
6029 mtx_unlock(&np->n_mtx);
6030 if (len > transfer && docommit == 0)
6034 if (docommit != 0) {
6037 * Do last mirrored DS commit with this thread.
6039 if (mirror < flp->nfsfl_mirrorcnt - 1)
6040 error = nfsio_commitds(vp, off, xfer,
6041 *dspp, fhp, dp->nfsdi_vers,
6042 dp->nfsdi_minorvers, drpc, tcred,
6045 error = nfsrpc_commitds(vp, off, xfer,
6046 *dspp, fhp, dp->nfsdi_vers,
6047 dp->nfsdi_minorvers, tcred, p);
6048 NFSCL_DEBUG(4, "commitds=%d\n", error);
6049 if (error != 0 && error != EACCES) {
6051 "DS layreterr for commit\n");
6052 nfscl_dserr(NFSV4OP_COMMIT, error, dp,
6056 NFSCL_DEBUG(4, "aft nfsio_commitds=%d\n", error);
6059 * Set both eof and uio_resid = 0 to end any
6063 uiop->uio_resid = 0;
6065 mtx_lock(&np->n_mtx);
6066 np->n_flag &= ~NDSCOMMIT;
6067 mtx_unlock(&np->n_mtx);
6069 } else if (rwflag == NFSV4OPEN_ACCESSREAD) {
6070 error = nfsrpc_readds(vp, uiop, stateidp, eofp, *dspp,
6071 off, xfer, fhp, 1, dp->nfsdi_vers,
6072 dp->nfsdi_minorvers, tcred, p);
6073 NFSCL_DEBUG(4, "readds=%d\n", error);
6074 if (error != 0 && error != EACCES) {
6075 NFSCL_DEBUG(4, "DS layreterr for read\n");
6076 nfscl_dserr(NFSV4OP_READ, error, dp, lyp,
6080 if (flp->nfsfl_mirrorcnt == 1) {
6081 error = nfsrpc_writeds(vp, uiop, iomode,
6082 must_commit, stateidp, *dspp, off, xfer,
6083 fhp, 0, 1, dp->nfsdi_vers,
6084 dp->nfsdi_minorvers, tcred, p);
6087 lyp->nfsly_flags |= NFSLY_WRITTEN;
6091 m = nfsm_copym(mp, rel_off, xfer);
6092 NFSCL_DEBUG(4, "mcopy reloff=%d xfer=%jd\n",
6093 rel_off, (uintmax_t)xfer);
6095 * Do last write to a mirrored DS with this
6098 if (mirror < flp->nfsfl_mirrorcnt - 1)
6099 error = nfsio_writedsmir(vp, iomode,
6100 must_commit, stateidp, *dspp, off,
6101 xfer, fhp, m, dp->nfsdi_vers,
6102 dp->nfsdi_minorvers, drpc, tcred,
6105 error = nfsrpc_writedsmir(vp, iomode,
6106 must_commit, stateidp, *dspp, off,
6107 xfer, fhp, m, dp->nfsdi_vers,
6108 dp->nfsdi_minorvers, tcred, p);
6109 NFSCL_DEBUG(4, "nfsio_writedsmir=%d\n", error);
6110 if (error != 0 && error != EACCES) {
6112 "DS layreterr for write\n");
6113 nfscl_dserr(NFSV4OP_WRITE, error, dp,
6118 NFSCL_DEBUG(4, "aft read/writeds=%d\n", error);
6124 if ((dp->nfsdi_flags & NFSDI_TIGHTCOUPLED) == 0)
6127 NFSCL_DEBUG(4, "eo nfscl_dofflayoutio=%d\n", error);
6132 * The actual read RPC done to a DS.
6135 nfsrpc_readds(vnode_t vp, struct uio *uiop, nfsv4stateid_t *stateidp, int *eofp,
6136 struct nfsclds *dsp, uint64_t io_off, int len, struct nfsfh *fhp, int flex,
6137 int vers, int minorvers, struct ucred *cred, NFSPROC_T *p)
6140 int attrflag, error, retlen;
6141 struct nfsrv_descript nfsd;
6142 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
6143 struct nfsrv_descript *nd = &nfsd;
6144 struct nfssockreq *nrp;
6148 if (vers == 0 || vers == NFS_VER4) {
6149 nfscl_reqstart(nd, NFSPROC_READDS, nmp, fhp->nfh_fh,
6150 fhp->nfh_len, NULL, &dsp->nfsclds_sess, vers, minorvers);
6152 NFSCL_DEBUG(4, "nfsrpc_readds: vers4 minvers=%d\n", minorvers);
6154 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID);
6156 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSEQIDZERO);
6158 nfscl_reqstart(nd, NFSPROC_READ, nmp, fhp->nfh_fh,
6159 fhp->nfh_len, NULL, &dsp->nfsclds_sess, vers, minorvers);
6160 NFSCL_DEBUG(4, "nfsrpc_readds: vers3\n");
6162 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED * 3);
6163 txdr_hyper(io_off, tl);
6164 *(tl + 2) = txdr_unsigned(len);
6165 nrp = dsp->nfsclds_sockp;
6166 NFSCL_DEBUG(4, "nfsrpc_readds: nrp=%p\n", nrp);
6168 /* If NULL, use the MDS socket. */
6169 nrp = &nmp->nm_sockreq;
6170 error = newnfs_request(nd, nmp, NULL, nrp, vp, p, cred,
6171 NFS_PROG, vers, NULL, 1, NULL, &dsp->nfsclds_sess);
6172 NFSCL_DEBUG(4, "nfsrpc_readds: stat=%d err=%d\n", nd->nd_repstat,
6176 if (vers == NFS_VER3) {
6177 error = nfscl_postop_attr(nd, &na, &attrflag, NULL);
6178 NFSCL_DEBUG(4, "nfsrpc_readds: postop=%d\n", error);
6182 if (nd->nd_repstat != 0) {
6183 error = nd->nd_repstat;
6186 if (vers == NFS_VER3) {
6187 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
6188 *eofp = fxdr_unsigned(int, *(tl + 1));
6190 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
6191 *eofp = fxdr_unsigned(int, *tl);
6193 NFSM_STRSIZ(retlen, len);
6194 NFSCL_DEBUG(4, "nfsrpc_readds: retlen=%d eof=%d\n", retlen, *eofp);
6195 error = nfsm_mbufuio(nd, uiop, retlen);
6197 if (nd->nd_mrep != NULL)
6198 mbuf_freem(nd->nd_mrep);
6203 * The actual write RPC done to a DS.
6206 nfsrpc_writeds(vnode_t vp, struct uio *uiop, int *iomode, int *must_commit,
6207 nfsv4stateid_t *stateidp, struct nfsclds *dsp, uint64_t io_off, int len,
6208 struct nfsfh *fhp, int commit_thru_mds, int flex, int vers, int minorvers,
6209 struct ucred *cred, NFSPROC_T *p)
6212 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
6213 int attrflag, error, rlen, commit, committed = NFSWRITE_FILESYNC;
6215 struct nfsrv_descript nfsd;
6216 struct nfsrv_descript *nd = &nfsd;
6217 struct nfssockreq *nrp;
6220 KASSERT(uiop->uio_iovcnt == 1, ("nfs: writerpc iovcnt > 1"));
6222 if (vers == 0 || vers == NFS_VER4) {
6223 nfscl_reqstart(nd, NFSPROC_WRITEDS, nmp, fhp->nfh_fh,
6224 fhp->nfh_len, NULL, &dsp->nfsclds_sess, vers, minorvers);
6225 NFSCL_DEBUG(4, "nfsrpc_writeds: vers4 minvers=%d\n", minorvers);
6228 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID);
6230 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSEQIDZERO);
6231 NFSM_BUILD(tl, uint32_t *, NFSX_HYPER + 2 * NFSX_UNSIGNED);
6233 nfscl_reqstart(nd, NFSPROC_WRITE, nmp, fhp->nfh_fh,
6234 fhp->nfh_len, NULL, &dsp->nfsclds_sess, vers, minorvers);
6235 NFSCL_DEBUG(4, "nfsrpc_writeds: vers3\n");
6236 NFSM_BUILD(tl, uint32_t *, NFSX_HYPER + 3 * NFSX_UNSIGNED);
6238 txdr_hyper(io_off, tl);
6240 if (vers == NFS_VER3)
6241 *tl++ = txdr_unsigned(len);
6242 *tl++ = txdr_unsigned(*iomode);
6243 *tl = txdr_unsigned(len);
6244 nfsm_uiombuf(nd, uiop, len);
6245 nrp = dsp->nfsclds_sockp;
6247 /* If NULL, use the MDS socket. */
6248 nrp = &nmp->nm_sockreq;
6249 error = newnfs_request(nd, nmp, NULL, nrp, vp, p, cred,
6250 NFS_PROG, vers, NULL, 1, NULL, &dsp->nfsclds_sess);
6251 NFSCL_DEBUG(4, "nfsrpc_writeds: err=%d stat=%d\n", error,
6255 if (nd->nd_repstat != 0) {
6257 * In case the rpc gets retried, roll
6258 * the uio fileds changed by nfsm_uiombuf()
6261 uiop->uio_offset -= len;
6262 uio_uio_resid_add(uiop, len);
6263 uio_iov_base_add(uiop, -len);
6264 uio_iov_len_add(uiop, len);
6265 error = nd->nd_repstat;
6267 if (vers == NFS_VER3) {
6268 error = nfscl_wcc_data(nd, vp, &na, &attrflag, NULL,
6270 NFSCL_DEBUG(4, "nfsrpc_writeds: wcc_data=%d\n", error);
6274 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED + NFSX_VERF);
6275 rlen = fxdr_unsigned(int, *tl++);
6276 NFSCL_DEBUG(4, "nfsrpc_writeds: len=%d rlen=%d\n", len, rlen);
6280 } else if (rlen < len) {
6281 backup = len - rlen;
6282 uio_iov_base_add(uiop, -(backup));
6283 uio_iov_len_add(uiop, backup);
6284 uiop->uio_offset -= backup;
6285 uio_uio_resid_add(uiop, backup);
6288 commit = fxdr_unsigned(int, *tl++);
6291 * Return the lowest commitment level
6292 * obtained by any of the RPCs.
6294 if (committed == NFSWRITE_FILESYNC)
6296 else if (committed == NFSWRITE_DATASYNC &&
6297 commit == NFSWRITE_UNSTABLE)
6299 if (commit_thru_mds != 0) {
6301 if (!NFSHASWRITEVERF(nmp)) {
6302 NFSBCOPY(tl, nmp->nm_verf, NFSX_VERF);
6303 NFSSETWRITEVERF(nmp);
6304 } else if (NFSBCMP(tl, nmp->nm_verf, NFSX_VERF)) {
6306 NFSBCOPY(tl, nmp->nm_verf, NFSX_VERF);
6311 if ((dsp->nfsclds_flags & NFSCLDS_HASWRITEVERF) == 0) {
6312 NFSBCOPY(tl, dsp->nfsclds_verf, NFSX_VERF);
6313 dsp->nfsclds_flags |= NFSCLDS_HASWRITEVERF;
6314 } else if (NFSBCMP(tl, dsp->nfsclds_verf, NFSX_VERF)) {
6316 NFSBCOPY(tl, dsp->nfsclds_verf, NFSX_VERF);
6322 if (nd->nd_mrep != NULL)
6323 mbuf_freem(nd->nd_mrep);
6324 *iomode = committed;
6325 if (nd->nd_repstat != 0 && error == 0)
6326 error = nd->nd_repstat;
6331 * The actual write RPC done to a DS.
6332 * This variant is called from a separate kernel process for mirrors.
6333 * Any short write is considered an IO error.
6336 nfsrpc_writedsmir(vnode_t vp, int *iomode, int *must_commit,
6337 nfsv4stateid_t *stateidp, struct nfsclds *dsp, uint64_t io_off, int len,
6338 struct nfsfh *fhp, struct mbuf *m, int vers, int minorvers,
6339 struct ucred *cred, NFSPROC_T *p)
6342 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
6343 int attrflag, error, commit, committed = NFSWRITE_FILESYNC, rlen;
6344 struct nfsrv_descript nfsd;
6345 struct nfsrv_descript *nd = &nfsd;
6346 struct nfssockreq *nrp;
6350 if (vers == 0 || vers == NFS_VER4) {
6351 nfscl_reqstart(nd, NFSPROC_WRITEDS, nmp, fhp->nfh_fh,
6352 fhp->nfh_len, NULL, &dsp->nfsclds_sess, vers, minorvers);
6354 NFSCL_DEBUG(4, "nfsrpc_writedsmir: vers4 minvers=%d\n",
6356 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID);
6357 NFSM_BUILD(tl, uint32_t *, NFSX_HYPER + 2 * NFSX_UNSIGNED);
6359 nfscl_reqstart(nd, NFSPROC_WRITE, nmp, fhp->nfh_fh,
6360 fhp->nfh_len, NULL, &dsp->nfsclds_sess, vers, minorvers);
6361 NFSCL_DEBUG(4, "nfsrpc_writedsmir: vers3\n");
6362 NFSM_BUILD(tl, uint32_t *, NFSX_HYPER + 3 * NFSX_UNSIGNED);
6364 txdr_hyper(io_off, tl);
6366 if (vers == NFS_VER3)
6367 *tl++ = txdr_unsigned(len);
6368 *tl++ = txdr_unsigned(*iomode);
6369 *tl = txdr_unsigned(len);
6371 /* Put data in mbuf chain. */
6372 nd->nd_mb->m_next = m;
6373 /* Set nd_mb and nd_bpos to end of data. */
6374 while (m->m_next != NULL)
6377 nd->nd_bpos = mtod(m, char *) + m->m_len;
6378 NFSCL_DEBUG(4, "nfsrpc_writedsmir: lastmb len=%d\n", m->m_len);
6380 nrp = dsp->nfsclds_sockp;
6382 /* If NULL, use the MDS socket. */
6383 nrp = &nmp->nm_sockreq;
6384 error = newnfs_request(nd, nmp, NULL, nrp, vp, p, cred,
6385 NFS_PROG, vers, NULL, 1, NULL, &dsp->nfsclds_sess);
6386 NFSCL_DEBUG(4, "nfsrpc_writedsmir: err=%d stat=%d\n", error,
6390 if (nd->nd_repstat != 0)
6391 error = nd->nd_repstat;
6393 if (vers == NFS_VER3) {
6394 error = nfscl_wcc_data(nd, vp, &na, &attrflag, NULL,
6396 NFSCL_DEBUG(4, "nfsrpc_writedsmir: wcc_data=%d\n",
6401 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED + NFSX_VERF);
6402 rlen = fxdr_unsigned(int, *tl++);
6403 NFSCL_DEBUG(4, "nfsrpc_writedsmir: len=%d rlen=%d\n", len,
6407 NFSCL_DEBUG(4, "nfsrpc_writedsmir: len=%d rlen=%d\n",
6411 commit = fxdr_unsigned(int, *tl++);
6414 * Return the lowest commitment level
6415 * obtained by any of the RPCs.
6417 if (committed == NFSWRITE_FILESYNC)
6419 else if (committed == NFSWRITE_DATASYNC &&
6420 commit == NFSWRITE_UNSTABLE)
6423 if ((dsp->nfsclds_flags & NFSCLDS_HASWRITEVERF) == 0) {
6424 NFSBCOPY(tl, dsp->nfsclds_verf, NFSX_VERF);
6425 dsp->nfsclds_flags |= NFSCLDS_HASWRITEVERF;
6426 } else if (NFSBCMP(tl, dsp->nfsclds_verf, NFSX_VERF)) {
6428 NFSBCOPY(tl, dsp->nfsclds_verf, NFSX_VERF);
6433 if (nd->nd_mrep != NULL)
6434 mbuf_freem(nd->nd_mrep);
6435 *iomode = committed;
6436 if (nd->nd_repstat != 0 && error == 0)
6437 error = nd->nd_repstat;
6442 * Start up the thread that will execute nfsrpc_writedsmir().
6445 start_writedsmir(void *arg, int pending)
6447 struct nfsclwritedsdorpc *drpc;
6449 drpc = (struct nfsclwritedsdorpc *)arg;
6450 drpc->err = nfsrpc_writedsmir(drpc->vp, &drpc->iomode,
6451 &drpc->must_commit, drpc->stateidp, drpc->dsp, drpc->off, drpc->len,
6452 drpc->fhp, drpc->m, drpc->vers, drpc->minorvers, drpc->cred,
6455 NFSCL_DEBUG(4, "start_writedsmir: err=%d\n", drpc->err);
6459 * Set up the write DS mirror call for the pNFS I/O thread.
6462 nfsio_writedsmir(vnode_t vp, int *iomode, int *must_commit,
6463 nfsv4stateid_t *stateidp, struct nfsclds *dsp, uint64_t off, int len,
6464 struct nfsfh *fhp, struct mbuf *m, int vers, int minorvers,
6465 struct nfsclwritedsdorpc *drpc, struct ucred *cred, NFSPROC_T *p)
6472 drpc->iomode = *iomode;
6473 drpc->must_commit = *must_commit;
6474 drpc->stateidp = stateidp;
6481 drpc->minorvers = minorvers;
6486 if (nfs_pnfsiothreads != 0) {
6487 ret = nfs_pnfsio(start_writedsmir, drpc);
6488 NFSCL_DEBUG(4, "nfsio_writedsmir: nfs_pnfsio=%d\n", ret);
6491 error = nfsrpc_writedsmir(vp, iomode, must_commit, stateidp,
6492 dsp, off, len, fhp, m, vers, minorvers, cred, p);
6493 NFSCL_DEBUG(4, "nfsio_writedsmir: error=%d\n", error);
6498 * Free up the nfsclds structure.
6501 nfscl_freenfsclds(struct nfsclds *dsp)
6507 if (dsp->nfsclds_sockp != NULL) {
6508 NFSFREECRED(dsp->nfsclds_sockp->nr_cred);
6509 NFSFREEMUTEX(&dsp->nfsclds_sockp->nr_mtx);
6510 free(dsp->nfsclds_sockp->nr_nam, M_SONAME);
6511 free(dsp->nfsclds_sockp, M_NFSSOCKREQ);
6513 NFSFREEMUTEX(&dsp->nfsclds_mtx);
6514 NFSFREEMUTEX(&dsp->nfsclds_sess.nfsess_mtx);
6515 for (i = 0; i < NFSV4_CBSLOTS; i++) {
6516 if (dsp->nfsclds_sess.nfsess_cbslots[i].nfssl_reply != NULL)
6518 dsp->nfsclds_sess.nfsess_cbslots[i].nfssl_reply);
6520 free(dsp, M_NFSCLDS);
6523 static enum nfsclds_state
6524 nfscl_getsameserver(struct nfsmount *nmp, struct nfsclds *newdsp,
6525 struct nfsclds **retdspp, uint32_t *sequencep)
6527 struct nfsclds *dsp;
6531 * Search the list of nfsclds structures for one with the same
6535 TAILQ_FOREACH(dsp, &nmp->nm_sess, nfsclds_list) {
6536 if (dsp->nfsclds_servownlen == newdsp->nfsclds_servownlen &&
6537 dsp->nfsclds_servownlen != 0 &&
6538 !NFSBCMP(dsp->nfsclds_serverown, newdsp->nfsclds_serverown,
6539 dsp->nfsclds_servownlen) &&
6540 dsp->nfsclds_sess.nfsess_defunct == 0) {
6541 NFSCL_DEBUG(4, "fnd same fdsp=%p dsp=%p flg=0x%x\n",
6542 TAILQ_FIRST(&nmp->nm_sess), dsp,
6543 dsp->nfsclds_flags);
6545 /* Get sequenceid# from first entry. */
6547 dsp->nfsclds_sess.nfsess_sequenceid;
6550 /* Server major id matches. */
6551 if ((dsp->nfsclds_flags & NFSCLDS_DS) != 0) {
6553 return (NFSDSP_USETHISSESSION);
6559 return (NFSDSP_SEQTHISSESSION);
6560 return (NFSDSP_NOTFOUND);
6564 * NFS commit rpc to a NFSv4.1 DS.
6567 nfsrpc_commitds(vnode_t vp, uint64_t offset, int cnt, struct nfsclds *dsp,
6568 struct nfsfh *fhp, int vers, int minorvers, struct ucred *cred,
6572 struct nfsrv_descript nfsd, *nd = &nfsd;
6573 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
6574 struct nfssockreq *nrp;
6576 int attrflag, error;
6579 if (vers == 0 || vers == NFS_VER4) {
6580 nfscl_reqstart(nd, NFSPROC_COMMITDS, nmp, fhp->nfh_fh,
6581 fhp->nfh_len, NULL, &dsp->nfsclds_sess, vers, minorvers);
6584 nfscl_reqstart(nd, NFSPROC_COMMIT, nmp, fhp->nfh_fh,
6585 fhp->nfh_len, NULL, &dsp->nfsclds_sess, vers, minorvers);
6586 NFSCL_DEBUG(4, "nfsrpc_commitds: vers=%d minvers=%d\n", vers,
6588 NFSM_BUILD(tl, uint32_t *, NFSX_HYPER + NFSX_UNSIGNED);
6589 txdr_hyper(offset, tl);
6591 *tl = txdr_unsigned(cnt);
6592 nrp = dsp->nfsclds_sockp;
6594 /* If NULL, use the MDS socket. */
6595 nrp = &nmp->nm_sockreq;
6596 error = newnfs_request(nd, nmp, NULL, nrp, vp, p, cred,
6597 NFS_PROG, vers, NULL, 1, NULL, &dsp->nfsclds_sess);
6598 NFSCL_DEBUG(4, "nfsrpc_commitds: err=%d stat=%d\n", error,
6602 if (nd->nd_repstat == 0) {
6603 if (vers == NFS_VER3) {
6604 error = nfscl_wcc_data(nd, vp, &na, &attrflag, NULL,
6606 NFSCL_DEBUG(4, "nfsrpc_commitds: wccdata=%d\n", error);
6610 NFSM_DISSECT(tl, u_int32_t *, NFSX_VERF);
6612 if (NFSBCMP(tl, dsp->nfsclds_verf, NFSX_VERF)) {
6613 NFSBCOPY(tl, dsp->nfsclds_verf, NFSX_VERF);
6614 error = NFSERR_STALEWRITEVERF;
6619 if (error == 0 && nd->nd_repstat != 0)
6620 error = nd->nd_repstat;
6621 mbuf_freem(nd->nd_mrep);
6626 * Start up the thread that will execute nfsrpc_commitds().
6629 start_commitds(void *arg, int pending)
6631 struct nfsclwritedsdorpc *drpc;
6633 drpc = (struct nfsclwritedsdorpc *)arg;
6634 drpc->err = nfsrpc_commitds(drpc->vp, drpc->off, drpc->len,
6635 drpc->dsp, drpc->fhp, drpc->vers, drpc->minorvers, drpc->cred,
6638 NFSCL_DEBUG(4, "start_commitds: err=%d\n", drpc->err);
6642 * Set up the commit DS mirror call for the pNFS I/O thread.
6645 nfsio_commitds(vnode_t vp, uint64_t offset, int cnt, struct nfsclds *dsp,
6646 struct nfsfh *fhp, int vers, int minorvers,
6647 struct nfsclwritedsdorpc *drpc, struct ucred *cred, NFSPROC_T *p)
6659 drpc->minorvers = minorvers;
6664 if (nfs_pnfsiothreads != 0) {
6665 ret = nfs_pnfsio(start_commitds, drpc);
6666 NFSCL_DEBUG(4, "nfsio_commitds: nfs_pnfsio=%d\n", ret);
6669 error = nfsrpc_commitds(vp, offset, cnt, dsp, fhp, vers,
6670 minorvers, cred, p);
6671 NFSCL_DEBUG(4, "nfsio_commitds: error=%d\n", error);
6676 * Set up the XDR arguments for the LayoutGet operation.
6679 nfsrv_setuplayoutget(struct nfsrv_descript *nd, int iomode, uint64_t offset,
6680 uint64_t len, uint64_t minlen, nfsv4stateid_t *stateidp, int layouttype,
6681 int layoutlen, int usecurstateid)
6685 NFSM_BUILD(tl, uint32_t *, 4 * NFSX_UNSIGNED + 3 * NFSX_HYPER +
6687 *tl++ = newnfs_false; /* Don't signal availability. */
6688 *tl++ = txdr_unsigned(layouttype);
6689 *tl++ = txdr_unsigned(iomode);
6690 txdr_hyper(offset, tl);
6692 txdr_hyper(len, tl);
6694 txdr_hyper(minlen, tl);
6696 if (usecurstateid != 0) {
6697 /* Special stateid for Current stateid. */
6698 *tl++ = txdr_unsigned(1);
6703 *tl++ = txdr_unsigned(stateidp->seqid);
6704 NFSCL_DEBUG(4, "layget seq=%d\n", (int)stateidp->seqid);
6705 *tl++ = stateidp->other[0];
6706 *tl++ = stateidp->other[1];
6707 *tl++ = stateidp->other[2];
6709 *tl = txdr_unsigned(layoutlen);
6713 * Parse the reply for a successful LayoutGet operation.
6716 nfsrv_parselayoutget(struct nfsrv_descript *nd, nfsv4stateid_t *stateidp,
6717 int *retonclosep, struct nfsclflayouthead *flhp)
6720 struct nfsclflayout *flp, *prevflp, *tflp;
6721 int cnt, error, fhcnt, gotiomode, i, iomode, j, k, l, laytype, nfhlen;
6723 uint64_t retlen, off;
6729 NFSCL_DEBUG(4, "in nfsrv_parselayoutget\n");
6733 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED + NFSX_STATEID);
6738 stateidp->seqid = fxdr_unsigned(uint32_t, *tl++);
6739 NFSCL_DEBUG(4, "retoncls=%d stseq=%d\n", *retonclosep,
6740 (int)stateidp->seqid);
6741 stateidp->other[0] = *tl++;
6742 stateidp->other[1] = *tl++;
6743 stateidp->other[2] = *tl++;
6744 cnt = fxdr_unsigned(int, *tl);
6745 NFSCL_DEBUG(4, "layg cnt=%d\n", cnt);
6746 if (cnt <= 0 || cnt > 10000) {
6747 /* Don't accept more than 10000 layouts in reply. */
6748 error = NFSERR_BADXDR;
6751 for (i = 0; i < cnt; i++) {
6752 /* Dissect to the layout type. */
6753 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_HYPER +
6755 off = fxdr_hyper(tl); tl += 2;
6756 retlen = fxdr_hyper(tl); tl += 2;
6757 iomode = fxdr_unsigned(int, *tl++);
6758 laytype = fxdr_unsigned(int, *tl);
6759 NFSCL_DEBUG(4, "layt=%d off=%ju len=%ju iom=%d\n", laytype,
6760 (uintmax_t)off, (uintmax_t)retlen, iomode);
6761 /* Ignore length of layout body for now. */
6762 if (laytype == NFSLAYOUT_NFSV4_1_FILES) {
6763 /* Parse the File layout up to fhcnt. */
6764 NFSM_DISSECT(tl, uint32_t *, 3 * NFSX_UNSIGNED +
6765 NFSX_HYPER + NFSX_V4DEVICEID);
6766 fhcnt = fxdr_unsigned(int, *(tl + 4 +
6767 NFSX_V4DEVICEID / NFSX_UNSIGNED));
6768 NFSCL_DEBUG(4, "fhcnt=%d\n", fhcnt);
6769 if (fhcnt < 0 || fhcnt > 100) {
6770 /* Don't accept more than 100 file handles. */
6771 error = NFSERR_BADXDR;
6775 flp = malloc(sizeof(*flp) + fhcnt *
6776 sizeof(struct nfsfh *), M_NFSFLAYOUT,
6779 flp = malloc(sizeof(*flp), M_NFSFLAYOUT,
6781 flp->nfsfl_flags = NFSFL_FILE;
6782 flp->nfsfl_fhcnt = 0;
6783 flp->nfsfl_devp = NULL;
6784 flp->nfsfl_off = off;
6785 if (flp->nfsfl_off + retlen < flp->nfsfl_off)
6786 flp->nfsfl_end = UINT64_MAX - flp->nfsfl_off;
6788 flp->nfsfl_end = flp->nfsfl_off + retlen;
6789 flp->nfsfl_iomode = iomode;
6790 if (gotiomode == -1)
6791 gotiomode = flp->nfsfl_iomode;
6792 /* Ignore layout body length for now. */
6793 NFSBCOPY(tl, flp->nfsfl_dev, NFSX_V4DEVICEID);
6794 tl += (NFSX_V4DEVICEID / NFSX_UNSIGNED);
6795 flp->nfsfl_util = fxdr_unsigned(uint32_t, *tl++);
6796 NFSCL_DEBUG(4, "flutil=0x%x\n", flp->nfsfl_util);
6797 flp->nfsfl_stripe1 = fxdr_unsigned(uint32_t, *tl++);
6798 flp->nfsfl_patoff = fxdr_hyper(tl); tl += 2;
6799 NFSCL_DEBUG(4, "stripe1=%u poff=%ju\n",
6800 flp->nfsfl_stripe1, (uintmax_t)flp->nfsfl_patoff);
6801 for (j = 0; j < fhcnt; j++) {
6802 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
6803 nfhlen = fxdr_unsigned(int, *tl);
6804 if (nfhlen <= 0 || nfhlen > NFSX_V4FHMAX) {
6805 error = NFSERR_BADXDR;
6808 nfhp = malloc(sizeof(*nfhp) + nfhlen - 1,
6810 flp->nfsfl_fh[j] = nfhp;
6812 nfhp->nfh_len = nfhlen;
6813 NFSM_DISSECT(cp, uint8_t *, NFSM_RNDUP(nfhlen));
6814 NFSBCOPY(cp, nfhp->nfh_fh, nfhlen);
6816 } else if (laytype == NFSLAYOUT_FLEXFILE) {
6817 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED +
6819 mirrorcnt = fxdr_unsigned(int, *(tl + 2));
6820 NFSCL_DEBUG(4, "mirrorcnt=%d\n", mirrorcnt);
6821 if (mirrorcnt < 1 || mirrorcnt > NFSDEV_MAXMIRRORS) {
6822 error = NFSERR_BADXDR;
6825 flp = malloc(sizeof(*flp) + mirrorcnt *
6826 sizeof(struct nfsffm), M_NFSFLAYOUT, M_WAITOK);
6827 flp->nfsfl_flags = NFSFL_FLEXFILE;
6828 flp->nfsfl_mirrorcnt = mirrorcnt;
6829 flp->nfsfl_devp = NULL;
6830 flp->nfsfl_off = off;
6831 if (flp->nfsfl_off + retlen < flp->nfsfl_off)
6832 flp->nfsfl_end = UINT64_MAX - flp->nfsfl_off;
6834 flp->nfsfl_end = flp->nfsfl_off + retlen;
6835 flp->nfsfl_iomode = iomode;
6836 if (gotiomode == -1)
6837 gotiomode = flp->nfsfl_iomode;
6838 flp->nfsfl_stripeunit = fxdr_hyper(tl);
6839 NFSCL_DEBUG(4, "stripeunit=%ju\n",
6840 (uintmax_t)flp->nfsfl_stripeunit);
6841 for (j = 0; j < mirrorcnt; j++) {
6842 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
6843 k = fxdr_unsigned(int, *tl);
6844 if (k < 1 || k > 128) {
6845 error = NFSERR_BADXDR;
6848 NFSCL_DEBUG(4, "servercnt=%d\n", k);
6849 for (l = 0; l < k; l++) {
6850 NFSM_DISSECT(tl, uint32_t *,
6851 NFSX_V4DEVICEID + NFSX_STATEID +
6854 /* Just use the first server. */
6856 flp->nfsfl_ffm[j].dev,
6858 tl += (NFSX_V4DEVICEID /
6861 flp->nfsfl_ffm[j].st.seqid =
6863 flp->nfsfl_ffm[j].st.other[0] =
6865 flp->nfsfl_ffm[j].st.other[1] =
6867 flp->nfsfl_ffm[j].st.other[2] =
6869 NFSCL_DEBUG(4, "st.seqid=%u "
6870 "st.o0=0x%x st.o1=0x%x "
6872 flp->nfsfl_ffm[j].st.seqid,
6873 flp->nfsfl_ffm[j].st.other[0],
6874 flp->nfsfl_ffm[j].st.other[1],
6875 flp->nfsfl_ffm[j].st.other[2]);
6877 tl += ((NFSX_V4DEVICEID +
6881 fhcnt = fxdr_unsigned(int, *tl);
6882 NFSCL_DEBUG(4, "fhcnt=%d\n", fhcnt);
6884 fhcnt > NFSDEV_MAXVERS) {
6885 error = NFSERR_BADXDR;
6888 for (m = 0; m < fhcnt; m++) {
6889 NFSM_DISSECT(tl, uint32_t *,
6891 nfhlen = fxdr_unsigned(int,
6893 NFSCL_DEBUG(4, "nfhlen=%d\n",
6895 if (nfhlen <= 0 || nfhlen >
6897 error = NFSERR_BADXDR;
6900 NFSM_DISSECT(cp, uint8_t *,
6901 NFSM_RNDUP(nfhlen));
6903 flp->nfsfl_ffm[j].fhcnt
6907 nfhlen - 1, M_NFSFH,
6909 flp->nfsfl_ffm[j].fh[m]
6911 nfhp->nfh_len = nfhlen;
6919 /* Now, get the ffsd_user/ffds_group. */
6920 error = nfsrv_parseug(nd, 0, &user,
6922 NFSCL_DEBUG(4, "after parseu=%d\n",
6925 error = nfsrv_parseug(nd, 1,
6926 &user, &grp, curthread);
6927 NFSCL_DEBUG(4, "aft parseg=%d\n",
6931 NFSCL_DEBUG(4, "user=%d group=%d\n",
6934 flp->nfsfl_ffm[j].user = user;
6935 flp->nfsfl_ffm[j].group = grp;
6937 "usr=%d grp=%d\n", user,
6942 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
6943 flp->nfsfl_fflags = fxdr_unsigned(uint32_t, *tl++);
6944 flp->nfsfl_statshint = fxdr_unsigned(uint32_t, *tl);
6945 NFSCL_DEBUG(4, "fflags=0x%x statshint=%d\n",
6946 flp->nfsfl_fflags, flp->nfsfl_statshint);
6948 error = NFSERR_BADXDR;
6951 if (flp->nfsfl_iomode == gotiomode) {
6952 /* Keep the list in increasing offset order. */
6953 tflp = LIST_FIRST(flhp);
6955 while (tflp != NULL &&
6956 tflp->nfsfl_off < flp->nfsfl_off) {
6958 tflp = LIST_NEXT(tflp, nfsfl_list);
6960 if (prevflp == NULL)
6961 LIST_INSERT_HEAD(flhp, flp, nfsfl_list);
6963 LIST_INSERT_AFTER(prevflp, flp,
6965 NFSCL_DEBUG(4, "flp inserted\n");
6967 printf("nfscl_layoutget(): got wrong iomode\n");
6968 nfscl_freeflayout(flp);
6973 NFSCL_DEBUG(4, "eo nfsrv_parselayoutget=%d\n", error);
6974 if (error != 0 && flp != NULL)
6975 nfscl_freeflayout(flp);
6980 * Parse a user/group digit string.
6983 nfsrv_parseug(struct nfsrv_descript *nd, int dogrp, uid_t *uidp, gid_t *gidp,
6987 char *cp, *str, str0[NFSV4_SMALLSTR + 1];
6991 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
6992 len = fxdr_unsigned(uint32_t, *tl);
6994 if (len > NFSV4_OPAQUELIMIT) {
6995 error = NFSERR_BADXDR;
6998 NFSCL_DEBUG(4, "nfsrv_parseug: len=%d\n", len);
7001 *gidp = GID_NOGROUP;
7006 if (len > NFSV4_SMALLSTR)
7007 str = malloc(len + 1, M_TEMP, M_WAITOK);
7010 NFSM_DISSECT(cp, char *, NFSM_RNDUP(len));
7011 NFSBCOPY(cp, str, len);
7013 NFSCL_DEBUG(4, "nfsrv_parseug: str=%s\n", str);
7015 error = nfsv4_strtogid(nd, str, len, gidp, p);
7017 error = nfsv4_strtouid(nd, str, len, uidp, p);
7019 if (len > NFSV4_SMALLSTR)
7021 NFSCL_DEBUG(4, "eo nfsrv_parseug=%d\n", error);
7026 * Similar to nfsrpc_getlayout(), except that it uses nfsrpc_openlayget(),
7027 * so that it does both an Open and a Layoutget.
7030 nfsrpc_getopenlayout(struct nfsmount *nmp, vnode_t vp, u_int8_t *nfhp,
7031 int fhlen, uint8_t *newfhp, int newfhlen, uint32_t mode,
7032 struct nfsclopen *op, uint8_t *name, int namelen, struct nfscldeleg **dpp,
7033 struct ucred *cred, NFSPROC_T *p)
7035 struct nfscllayout *lyp;
7036 struct nfsclflayout *flp;
7037 struct nfsclflayouthead flh;
7038 int error, islocked, layoutlen, recalled, retonclose, usecurstateid;
7039 int layouttype, laystat;
7040 nfsv4stateid_t stateid;
7041 struct nfsclsession *tsep;
7044 if (NFSHASFLEXFILE(nmp))
7045 layouttype = NFSLAYOUT_FLEXFILE;
7047 layouttype = NFSLAYOUT_NFSV4_1_FILES;
7049 * If lyp is returned non-NULL, there will be a refcnt (shared lock)
7050 * on it, iff flp != NULL or a lock (exclusive lock) on it iff
7053 lyp = nfscl_getlayout(nmp->nm_clp, newfhp, newfhlen, 0, &flp,
7055 NFSCL_DEBUG(4, "nfsrpc_getopenlayout nfscl_getlayout lyp=%p\n", lyp);
7058 else if (flp != NULL)
7062 if ((lyp == NULL || flp == NULL) && recalled == 0) {
7064 tsep = nfsmnt_mdssession(nmp);
7065 layoutlen = tsep->nfsess_maxcache - (NFSX_STATEID +
7071 stateid.seqid = lyp->nfsly_stateid.seqid;
7072 stateid.other[0] = lyp->nfsly_stateid.other[0];
7073 stateid.other[1] = lyp->nfsly_stateid.other[1];
7074 stateid.other[2] = lyp->nfsly_stateid.other[2];
7076 error = nfsrpc_openlayoutrpc(nmp, vp, nfhp, fhlen,
7077 newfhp, newfhlen, mode, op, name, namelen,
7078 dpp, &stateid, usecurstateid, layouttype, layoutlen,
7079 &retonclose, &flh, &laystat, cred, p);
7080 NFSCL_DEBUG(4, "aft nfsrpc_openlayoutrpc laystat=%d err=%d\n",
7082 laystat = nfsrpc_layoutgetres(nmp, vp, newfhp, newfhlen,
7083 &stateid, retonclose, NULL, &lyp, &flh, layouttype, laystat,
7084 &islocked, cred, p);
7086 error = nfsrpc_openrpc(nmp, vp, nfhp, fhlen, newfhp, newfhlen,
7087 mode, op, name, namelen, dpp, 0, 0, cred, p, 0, 0);
7089 nfscl_rellayout(lyp, 1);
7090 else if (islocked == 1)
7091 nfscl_rellayout(lyp, 0);
7096 * This function does an Open+LayoutGet for an NFSv4.1 mount with pNFS
7097 * enabled, only for the CLAIM_NULL case. All other NFSv4 Opens are
7098 * handled by nfsrpc_openrpc().
7099 * For the case where op == NULL, dvp is the directory. When op != NULL, it
7103 nfsrpc_openlayoutrpc(struct nfsmount *nmp, vnode_t vp, u_int8_t *nfhp,
7104 int fhlen, uint8_t *newfhp, int newfhlen, uint32_t mode,
7105 struct nfsclopen *op, uint8_t *name, int namelen, struct nfscldeleg **dpp,
7106 nfsv4stateid_t *stateidp, int usecurstateid, int layouttype,
7107 int layoutlen, int *retonclosep, struct nfsclflayouthead *flhp,
7108 int *laystatp, struct ucred *cred, NFSPROC_T *p)
7111 struct nfsrv_descript nfsd, *nd = &nfsd;
7112 struct nfscldeleg *ndp = NULL;
7113 struct nfsvattr nfsva;
7114 struct nfsclsession *tsep;
7115 uint32_t rflags, deleg;
7116 nfsattrbit_t attrbits;
7117 int error, ret, acesize, limitby, iomode;
7121 nfscl_reqstart(nd, NFSPROC_OPENLAYGET, nmp, nfhp, fhlen, NULL, NULL,
7123 NFSM_BUILD(tl, uint32_t *, 5 * NFSX_UNSIGNED);
7124 *tl++ = txdr_unsigned(op->nfso_own->nfsow_seqid);
7125 *tl++ = txdr_unsigned(mode & NFSV4OPEN_ACCESSBOTH);
7126 *tl++ = txdr_unsigned((mode >> NFSLCK_SHIFT) & NFSV4OPEN_DENYBOTH);
7127 tsep = nfsmnt_mdssession(nmp);
7128 *tl++ = tsep->nfsess_clientid.lval[0];
7129 *tl = tsep->nfsess_clientid.lval[1];
7130 nfsm_strtom(nd, op->nfso_own->nfsow_owner, NFSV4CL_LOCKNAMELEN);
7131 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_UNSIGNED);
7132 *tl++ = txdr_unsigned(NFSV4OPEN_NOCREATE);
7133 *tl = txdr_unsigned(NFSV4OPEN_CLAIMNULL);
7134 nfsm_strtom(nd, name, namelen);
7135 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
7136 *tl = txdr_unsigned(NFSV4OP_GETATTR);
7137 NFSZERO_ATTRBIT(&attrbits);
7138 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_CHANGE);
7139 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEMODIFY);
7140 nfsrv_putattrbit(nd, &attrbits);
7141 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
7142 *tl = txdr_unsigned(NFSV4OP_LAYOUTGET);
7143 if ((mode & NFSV4OPEN_ACCESSWRITE) != 0)
7144 iomode = NFSLAYOUTIOMODE_RW;
7146 iomode = NFSLAYOUTIOMODE_READ;
7147 nfsrv_setuplayoutget(nd, iomode, 0, UINT64_MAX, 0, stateidp,
7148 layouttype, layoutlen, usecurstateid);
7149 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, vp, p, cred,
7150 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
7153 NFSCL_INCRSEQID(op->nfso_own->nfsow_seqid, nd);
7154 if (nd->nd_repstat != 0)
7155 *laystatp = nd->nd_repstat;
7156 if ((nd->nd_flag & ND_NOMOREDATA) == 0) {
7157 /* ND_NOMOREDATA will be set if the Open operation failed. */
7158 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
7160 op->nfso_stateid.seqid = *tl++;
7161 op->nfso_stateid.other[0] = *tl++;
7162 op->nfso_stateid.other[1] = *tl++;
7163 op->nfso_stateid.other[2] = *tl;
7164 rflags = fxdr_unsigned(u_int32_t, *(tl + 6));
7165 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
7168 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
7169 deleg = fxdr_unsigned(u_int32_t, *tl);
7170 if (deleg == NFSV4OPEN_DELEGATEREAD ||
7171 deleg == NFSV4OPEN_DELEGATEWRITE) {
7172 if (!(op->nfso_own->nfsow_clp->nfsc_flags &
7173 NFSCLFLAGS_FIRSTDELEG))
7174 op->nfso_own->nfsow_clp->nfsc_flags |=
7175 (NFSCLFLAGS_FIRSTDELEG | NFSCLFLAGS_GOTDELEG);
7176 ndp = malloc(sizeof(struct nfscldeleg) + newfhlen,
7177 M_NFSCLDELEG, M_WAITOK);
7178 LIST_INIT(&ndp->nfsdl_owner);
7179 LIST_INIT(&ndp->nfsdl_lock);
7180 ndp->nfsdl_clp = op->nfso_own->nfsow_clp;
7181 ndp->nfsdl_fhlen = newfhlen;
7182 NFSBCOPY(newfhp, ndp->nfsdl_fh, newfhlen);
7183 newnfs_copyincred(cred, &ndp->nfsdl_cred);
7184 nfscl_lockinit(&ndp->nfsdl_rwlock);
7185 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
7187 ndp->nfsdl_stateid.seqid = *tl++;
7188 ndp->nfsdl_stateid.other[0] = *tl++;
7189 ndp->nfsdl_stateid.other[1] = *tl++;
7190 ndp->nfsdl_stateid.other[2] = *tl++;
7191 ret = fxdr_unsigned(int, *tl);
7192 if (deleg == NFSV4OPEN_DELEGATEWRITE) {
7193 ndp->nfsdl_flags = NFSCLDL_WRITE;
7195 * Indicates how much the file can grow.
7197 NFSM_DISSECT(tl, u_int32_t *,
7199 limitby = fxdr_unsigned(int, *tl++);
7201 case NFSV4OPEN_LIMITSIZE:
7202 ndp->nfsdl_sizelimit = fxdr_hyper(tl);
7204 case NFSV4OPEN_LIMITBLOCKS:
7205 ndp->nfsdl_sizelimit =
7206 fxdr_unsigned(u_int64_t, *tl++);
7207 ndp->nfsdl_sizelimit *=
7208 fxdr_unsigned(u_int64_t, *tl);
7211 error = NFSERR_BADXDR;
7215 ndp->nfsdl_flags = NFSCLDL_READ;
7217 ndp->nfsdl_flags |= NFSCLDL_RECALL;
7218 error = nfsrv_dissectace(nd, &ndp->nfsdl_ace, &ret,
7222 } else if (deleg != NFSV4OPEN_DELEGATENONE) {
7223 error = NFSERR_BADXDR;
7226 if ((rflags & NFSV4OPEN_LOCKTYPEPOSIX) != 0 ||
7227 nfscl_assumeposixlocks)
7228 op->nfso_posixlock = 1;
7230 op->nfso_posixlock = 0;
7231 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
7232 /* If the 2nd element == NFS_OK, the Getattr succeeded. */
7234 error = nfsv4_loadattr(nd, NULL, &nfsva, NULL,
7235 NULL, 0, NULL, NULL, NULL, NULL, NULL, 0,
7236 NULL, NULL, NULL, p, cred);
7240 ndp->nfsdl_change = nfsva.na_filerev;
7241 ndp->nfsdl_modtime = nfsva.na_mtime;
7242 ndp->nfsdl_flags |= NFSCLDL_MODTIMESET;
7247 * At this point, the Open has succeeded, so set
7248 * nd_repstat = NFS_OK. If the Layoutget failed,
7249 * this function just won't return a layout.
7251 if (nd->nd_repstat == 0) {
7252 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
7253 *laystatp = fxdr_unsigned(int, *++tl);
7254 if (*laystatp == 0) {
7255 error = nfsrv_parselayoutget(nd,
7256 stateidp, retonclosep, flhp);
7261 nd->nd_repstat = 0; /* Return 0 for Open. */
7264 if (nd->nd_repstat != 0 && error == 0)
7265 error = nd->nd_repstat;
7267 free(ndp, M_NFSCLDELEG);
7268 mbuf_freem(nd->nd_mrep);
7273 * Similar nfsrpc_createv4(), but also does the LayoutGet operation.
7274 * Used only for mounts with pNFS enabled.
7277 nfsrpc_createlayout(vnode_t dvp, char *name, int namelen, struct vattr *vap,
7278 nfsquad_t cverf, int fmode, struct nfsclowner *owp, struct nfscldeleg **dpp,
7279 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap,
7280 struct nfsvattr *nnap, struct nfsfh **nfhpp, int *attrflagp,
7281 int *dattrflagp, void *dstuff, int *unlockedp, nfsv4stateid_t *stateidp,
7282 int usecurstateid, int layouttype, int layoutlen, int *retonclosep,
7283 struct nfsclflayouthead *flhp, int *laystatp)
7286 int error = 0, deleg, newone, ret, acesize, limitby;
7287 struct nfsrv_descript nfsd, *nd = &nfsd;
7288 struct nfsclopen *op;
7289 struct nfscldeleg *dp = NULL;
7292 struct nfsclsession *tsep;
7293 nfsattrbit_t attrbits;
7294 nfsv4stateid_t stateid;
7295 struct nfsmount *nmp;
7297 nmp = VFSTONFS(dvp->v_mount);
7305 if (namelen > NFS_MAXNAMLEN)
7306 return (ENAMETOOLONG);
7307 NFSCL_REQSTART(nd, NFSPROC_CREATELAYGET, dvp);
7309 * For V4, this is actually an Open op.
7311 NFSM_BUILD(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
7312 *tl++ = txdr_unsigned(owp->nfsow_seqid);
7313 *tl++ = txdr_unsigned(NFSV4OPEN_ACCESSWRITE |
7314 NFSV4OPEN_ACCESSREAD);
7315 *tl++ = txdr_unsigned(NFSV4OPEN_DENYNONE);
7316 tsep = nfsmnt_mdssession(nmp);
7317 *tl++ = tsep->nfsess_clientid.lval[0];
7318 *tl = tsep->nfsess_clientid.lval[1];
7319 nfsm_strtom(nd, owp->nfsow_owner, NFSV4CL_LOCKNAMELEN);
7320 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
7321 *tl++ = txdr_unsigned(NFSV4OPEN_CREATE);
7322 if ((fmode & O_EXCL) != 0) {
7323 if (NFSHASSESSPERSIST(nmp)) {
7324 /* Use GUARDED for persistent sessions. */
7325 *tl = txdr_unsigned(NFSCREATE_GUARDED);
7326 nfscl_fillsattr(nd, vap, dvp, 0, 0);
7328 /* Otherwise, use EXCLUSIVE4_1. */
7329 *tl = txdr_unsigned(NFSCREATE_EXCLUSIVE41);
7330 NFSM_BUILD(tl, u_int32_t *, NFSX_VERF);
7331 *tl++ = cverf.lval[0];
7332 *tl = cverf.lval[1];
7333 nfscl_fillsattr(nd, vap, dvp, 0, 0);
7336 *tl = txdr_unsigned(NFSCREATE_UNCHECKED);
7337 nfscl_fillsattr(nd, vap, dvp, 0, 0);
7339 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
7340 *tl = txdr_unsigned(NFSV4OPEN_CLAIMNULL);
7341 nfsm_strtom(nd, name, namelen);
7342 /* Get the new file's handle and attributes, plus save the FH. */
7343 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
7344 *tl++ = txdr_unsigned(NFSV4OP_SAVEFH);
7345 *tl++ = txdr_unsigned(NFSV4OP_GETFH);
7346 *tl = txdr_unsigned(NFSV4OP_GETATTR);
7347 NFSGETATTR_ATTRBIT(&attrbits);
7348 nfsrv_putattrbit(nd, &attrbits);
7349 /* Get the directory's post-op attributes. */
7350 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
7351 *tl = txdr_unsigned(NFSV4OP_PUTFH);
7352 nfsm_fhtom(nd, np->n_fhp->nfh_fh, np->n_fhp->nfh_len, 0);
7353 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
7354 *tl = txdr_unsigned(NFSV4OP_GETATTR);
7355 nfsrv_putattrbit(nd, &attrbits);
7356 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
7357 *tl++ = txdr_unsigned(NFSV4OP_RESTOREFH);
7358 *tl = txdr_unsigned(NFSV4OP_LAYOUTGET);
7359 nfsrv_setuplayoutget(nd, NFSLAYOUTIOMODE_RW, 0, UINT64_MAX, 0, stateidp,
7360 layouttype, layoutlen, usecurstateid);
7361 error = nfscl_request(nd, dvp, p, cred, dstuff);
7364 NFSCL_DEBUG(4, "nfsrpc_createlayout stat=%d err=%d\n", nd->nd_repstat,
7366 if (nd->nd_repstat != 0)
7367 *laystatp = nd->nd_repstat;
7368 NFSCL_INCRSEQID(owp->nfsow_seqid, nd);
7369 if ((nd->nd_flag & ND_NOMOREDATA) == 0) {
7370 NFSCL_DEBUG(4, "nfsrpc_createlayout open succeeded\n");
7371 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
7373 stateid.seqid = *tl++;
7374 stateid.other[0] = *tl++;
7375 stateid.other[1] = *tl++;
7376 stateid.other[2] = *tl;
7377 nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
7378 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
7379 deleg = fxdr_unsigned(int, *tl);
7380 if (deleg == NFSV4OPEN_DELEGATEREAD ||
7381 deleg == NFSV4OPEN_DELEGATEWRITE) {
7382 if (!(owp->nfsow_clp->nfsc_flags &
7383 NFSCLFLAGS_FIRSTDELEG))
7384 owp->nfsow_clp->nfsc_flags |=
7385 (NFSCLFLAGS_FIRSTDELEG | NFSCLFLAGS_GOTDELEG);
7386 dp = malloc(sizeof(struct nfscldeleg) + NFSX_V4FHMAX,
7387 M_NFSCLDELEG, M_WAITOK);
7388 LIST_INIT(&dp->nfsdl_owner);
7389 LIST_INIT(&dp->nfsdl_lock);
7390 dp->nfsdl_clp = owp->nfsow_clp;
7391 newnfs_copyincred(cred, &dp->nfsdl_cred);
7392 nfscl_lockinit(&dp->nfsdl_rwlock);
7393 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
7395 dp->nfsdl_stateid.seqid = *tl++;
7396 dp->nfsdl_stateid.other[0] = *tl++;
7397 dp->nfsdl_stateid.other[1] = *tl++;
7398 dp->nfsdl_stateid.other[2] = *tl++;
7399 ret = fxdr_unsigned(int, *tl);
7400 if (deleg == NFSV4OPEN_DELEGATEWRITE) {
7401 dp->nfsdl_flags = NFSCLDL_WRITE;
7403 * Indicates how much the file can grow.
7405 NFSM_DISSECT(tl, u_int32_t *,
7407 limitby = fxdr_unsigned(int, *tl++);
7409 case NFSV4OPEN_LIMITSIZE:
7410 dp->nfsdl_sizelimit = fxdr_hyper(tl);
7412 case NFSV4OPEN_LIMITBLOCKS:
7413 dp->nfsdl_sizelimit =
7414 fxdr_unsigned(u_int64_t, *tl++);
7415 dp->nfsdl_sizelimit *=
7416 fxdr_unsigned(u_int64_t, *tl);
7419 error = NFSERR_BADXDR;
7423 dp->nfsdl_flags = NFSCLDL_READ;
7426 dp->nfsdl_flags |= NFSCLDL_RECALL;
7427 error = nfsrv_dissectace(nd, &dp->nfsdl_ace, &ret,
7431 } else if (deleg != NFSV4OPEN_DELEGATENONE) {
7432 error = NFSERR_BADXDR;
7436 /* Now, we should have the status for the SaveFH. */
7437 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
7439 NFSCL_DEBUG(4, "nfsrpc_createlayout SaveFH ok\n");
7441 * Now, process the GetFH and Getattr for the newly
7442 * created file. nfscl_mtofh() will set
7443 * ND_NOMOREDATA if these weren't successful.
7445 error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp);
7446 NFSCL_DEBUG(4, "aft nfscl_mtofh err=%d\n", error);
7450 nd->nd_flag |= ND_NOMOREDATA;
7451 /* Now we have the PutFH and Getattr for the directory. */
7452 if ((nd->nd_flag & ND_NOMOREDATA) == 0) {
7453 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
7455 nd->nd_flag |= ND_NOMOREDATA;
7457 NFSM_DISSECT(tl, uint32_t *, 2 *
7460 nd->nd_flag |= ND_NOMOREDATA;
7463 if ((nd->nd_flag & ND_NOMOREDATA) == 0) {
7464 /* Load the directory attributes. */
7465 error = nfsm_loadattr(nd, dnap);
7466 NFSCL_DEBUG(4, "aft nfsm_loadattr err=%d\n", error);
7470 if (dp != NULL && *attrflagp != 0) {
7471 dp->nfsdl_change = nnap->na_filerev;
7472 dp->nfsdl_modtime = nnap->na_mtime;
7473 dp->nfsdl_flags |= NFSCLDL_MODTIMESET;
7476 * We can now complete the Open state.
7480 dp->nfsdl_fhlen = nfhp->nfh_len;
7481 NFSBCOPY(nfhp->nfh_fh, dp->nfsdl_fh,
7485 * Get an Open structure that will be
7486 * attached to the OpenOwner, acquired already.
7488 error = nfscl_open(dvp, nfhp->nfh_fh, nfhp->nfh_len,
7489 (NFSV4OPEN_ACCESSWRITE | NFSV4OPEN_ACCESSREAD), 0,
7490 cred, p, NULL, &op, &newone, NULL, 0);
7493 op->nfso_stateid = stateid;
7494 newnfs_copyincred(cred, &op->nfso_cred);
7496 nfscl_openrelease(nmp, op, error, newone);
7499 /* Now, handle the RestoreFH and LayoutGet. */
7500 if (nd->nd_repstat == 0) {
7501 NFSM_DISSECT(tl, uint32_t *, 4 * NFSX_UNSIGNED);
7502 *laystatp = fxdr_unsigned(int, *(tl + 3));
7503 if (*laystatp == 0) {
7504 error = nfsrv_parselayoutget(nd,
7505 stateidp, retonclosep, flhp);
7509 NFSCL_DEBUG(4, "aft nfsrv_parselayout err=%d\n",
7515 if (nd->nd_repstat != 0 && error == 0)
7516 error = nd->nd_repstat;
7517 if (error == NFSERR_STALECLIENTID || error == NFSERR_BADSESSION)
7518 nfscl_initiate_recovery(owp->nfsow_clp);
7520 NFSCL_DEBUG(4, "eo nfsrpc_createlayout err=%d\n", error);
7524 free(dp, M_NFSCLDELEG);
7525 mbuf_freem(nd->nd_mrep);
7530 * Similar to nfsrpc_getopenlayout(), except that it used for the Create case.
7533 nfsrpc_getcreatelayout(vnode_t dvp, char *name, int namelen, struct vattr *vap,
7534 nfsquad_t cverf, int fmode, struct nfsclowner *owp, struct nfscldeleg **dpp,
7535 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap,
7536 struct nfsvattr *nnap, struct nfsfh **nfhpp, int *attrflagp,
7537 int *dattrflagp, void *dstuff, int *unlockedp)
7539 struct nfscllayout *lyp;
7540 struct nfsclflayouthead flh;
7542 struct nfsclsession *tsep;
7543 struct nfsmount *nmp;
7544 nfsv4stateid_t stateid;
7545 int error, layoutlen, layouttype, retonclose, laystat;
7548 nmp = VFSTONFS(dvp->v_mount);
7549 if (NFSHASFLEXFILE(nmp))
7550 layouttype = NFSLAYOUT_FLEXFILE;
7552 layouttype = NFSLAYOUT_NFSV4_1_FILES;
7554 tsep = nfsmnt_mdssession(nmp);
7555 layoutlen = tsep->nfsess_maxcache - (NFSX_STATEID + 3 * NFSX_UNSIGNED);
7556 error = nfsrpc_createlayout(dvp, name, namelen, vap, cverf, fmode,
7557 owp, dpp, cred, p, dnap, nnap, nfhpp, attrflagp, dattrflagp,
7558 dstuff, unlockedp, &stateid, 1, layouttype, layoutlen, &retonclose,
7560 NFSCL_DEBUG(4, "aft nfsrpc_createlayoutrpc laystat=%d err=%d\n",
7565 laystat = nfsrpc_layoutgetres(nmp, dvp, nfhp->nfh_fh,
7566 nfhp->nfh_len, &stateid, retonclose, NULL, &lyp, &flh,
7567 layouttype, laystat, NULL, cred, p);
7569 laystat = nfsrpc_layoutgetres(nmp, dvp, NULL, 0, &stateid,
7570 retonclose, NULL, &lyp, &flh, layouttype, laystat, NULL,
7573 nfscl_rellayout(lyp, 0);
7578 * Process the results of a layoutget() operation.
7581 nfsrpc_layoutgetres(struct nfsmount *nmp, vnode_t vp, uint8_t *newfhp,
7582 int newfhlen, nfsv4stateid_t *stateidp, int retonclose, uint32_t *notifybit,
7583 struct nfscllayout **lypp, struct nfsclflayouthead *flhp, int layouttype,
7584 int laystat, int *islockedp, struct ucred *cred, NFSPROC_T *p)
7586 struct nfsclflayout *tflp;
7587 struct nfscldevinfo *dip;
7590 if (laystat == NFSERR_UNKNLAYOUTTYPE) {
7592 if (!NFSHASFLEXFILE(nmp)) {
7593 /* Switch to using Flex File Layout. */
7594 nmp->nm_state |= NFSSTA_FLEXFILE;
7595 } else if (layouttype == NFSLAYOUT_FLEXFILE) {
7597 NFSCL_DEBUG(1, "disable PNFS\n");
7598 nmp->nm_state &= ~(NFSSTA_PNFS | NFSSTA_FLEXFILE);
7603 NFSCL_DEBUG(4, "nfsrpc_layoutgetres at FOREACH\n");
7604 LIST_FOREACH(tflp, flhp, nfsfl_list) {
7605 laystat = nfscl_adddevinfo(nmp, NULL, tflp);
7606 NFSCL_DEBUG(4, "aft adddev=%d\n", laystat);
7608 if (layouttype == NFSLAYOUT_FLEXFILE)
7609 dev = tflp->nfsfl_ffm[0].dev;
7611 dev = tflp->nfsfl_dev;
7612 laystat = nfsrpc_getdeviceinfo(nmp, dev,
7613 layouttype, notifybit, &dip, cred, p);
7614 NFSCL_DEBUG(4, "aft nfsrpc_gdi=%d\n",
7618 laystat = nfscl_adddevinfo(nmp, dip, tflp);
7620 printf("getlayout: cannot add\n");
7626 * nfscl_layout() always returns with the nfsly_lock
7627 * set to a refcnt (shared lock).
7628 * Passing in dvp is sufficient, since it is only used to
7629 * get the fsid for the file system.
7631 laystat = nfscl_layout(nmp, vp, newfhp, newfhlen, stateidp,
7632 layouttype, retonclose, flhp, lypp, cred, p);
7633 NFSCL_DEBUG(4, "nfsrpc_layoutgetres: aft nfscl_layout=%d\n",
7635 if (laystat == 0 && islockedp != NULL)