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");
63 extern int nfs_numnfscbd;
64 extern struct timeval nfsboottime;
65 extern u_int32_t newnfs_false, newnfs_true;
66 extern nfstype nfsv34_type[9];
67 extern int nfsrv_useacl;
68 extern char nfsv4_callbackaddr[INET6_ADDRSTRLEN];
69 extern int nfscl_debuglevel;
70 extern int nfs_pnfsiothreads;
72 int nfstest_outofseq = 0;
73 int nfscl_assumeposixlocks = 1;
74 int nfscl_enablecallb = 0;
75 short nfsv4_cbport = NFSV4_CBPORT;
76 int nfstest_openallsetattr = 0;
77 #endif /* !APPLEKEXT */
79 #define DIRHDSIZ offsetof(struct dirent, d_name)
82 * nfscl_getsameserver() can return one of three values:
83 * NFSDSP_USETHISSESSION - Use this session for the DS.
84 * NFSDSP_SEQTHISSESSION - Use the nfsclds_sequence field of this dsp for new
86 * NFSDSP_NOTFOUND - No matching server was found.
89 NFSDSP_USETHISSESSION = 0,
90 NFSDSP_SEQTHISSESSION = 1,
95 * Do a write RPC on a DS data file, using this structure for the arguments,
96 * so that this function can be executed by a separate kernel process.
98 struct nfsclwritedsdorpc {
105 nfsv4stateid_t *stateidp;
118 static int nfsrpc_setattrrpc(vnode_t , struct vattr *, nfsv4stateid_t *,
119 struct ucred *, NFSPROC_T *, struct nfsvattr *, int *, void *);
120 static int nfsrpc_readrpc(vnode_t , struct uio *, struct ucred *,
121 nfsv4stateid_t *, NFSPROC_T *, struct nfsvattr *, int *, void *);
122 static int nfsrpc_writerpc(vnode_t , struct uio *, int *, int *,
123 struct ucred *, nfsv4stateid_t *, NFSPROC_T *, struct nfsvattr *, int *,
125 static int nfsrpc_createv23(vnode_t , char *, int, struct vattr *,
126 nfsquad_t, int, struct ucred *, NFSPROC_T *, struct nfsvattr *,
127 struct nfsvattr *, struct nfsfh **, int *, int *, void *);
128 static int nfsrpc_createv4(vnode_t , char *, int, struct vattr *,
129 nfsquad_t, int, struct nfsclowner *, struct nfscldeleg **, struct ucred *,
130 NFSPROC_T *, struct nfsvattr *, struct nfsvattr *, struct nfsfh **, int *,
131 int *, void *, int *);
132 static int nfsrpc_locku(struct nfsrv_descript *, struct nfsmount *,
133 struct nfscllockowner *, u_int64_t, u_int64_t,
134 u_int32_t, struct ucred *, NFSPROC_T *, int);
135 static int nfsrpc_setaclrpc(vnode_t, struct ucred *, NFSPROC_T *,
136 struct acl *, nfsv4stateid_t *, void *);
137 static int nfsrpc_getlayout(struct nfsmount *, vnode_t, struct nfsfh *, int,
138 uint32_t *, nfsv4stateid_t *, uint64_t, struct nfscllayout **,
139 struct ucred *, NFSPROC_T *);
140 static int nfsrpc_fillsa(struct nfsmount *, struct sockaddr_in *,
141 struct sockaddr_in6 *, sa_family_t, int, struct nfsclds **, NFSPROC_T *);
142 static void nfscl_initsessionslots(struct nfsclsession *);
143 static int nfscl_doflayoutio(vnode_t, struct uio *, int *, int *, int *,
144 nfsv4stateid_t *, int, struct nfscldevinfo *, struct nfscllayout *,
145 struct nfsclflayout *, uint64_t, uint64_t, int, struct ucred *,
147 static int nfscl_dofflayoutio(vnode_t, struct uio *, int *, int *, int *,
148 nfsv4stateid_t *, int, struct nfscldevinfo *, struct nfscllayout *,
149 struct nfsclflayout *, uint64_t, uint64_t, int, int, struct mbuf *,
150 struct nfsclwritedsdorpc *, struct ucred *, NFSPROC_T *);
151 static struct mbuf *nfsm_copym(struct mbuf *, int, int);
152 static int nfsrpc_readds(vnode_t, struct uio *, nfsv4stateid_t *, int *,
153 struct nfsclds *, uint64_t, int, struct nfsfh *, int, int, int,
154 struct ucred *, NFSPROC_T *);
155 static int nfsrpc_writeds(vnode_t, struct uio *, int *, int *,
156 nfsv4stateid_t *, struct nfsclds *, uint64_t, int,
157 struct nfsfh *, int, int, int, int, struct ucred *, NFSPROC_T *);
158 static int nfsio_writedsmir(vnode_t, int *, int *, nfsv4stateid_t *,
159 struct nfsclds *, uint64_t, int, struct nfsfh *, struct mbuf *, int, int,
160 struct nfsclwritedsdorpc *, struct ucred *, NFSPROC_T *);
161 static int nfsrpc_writedsmir(vnode_t, int *, int *, nfsv4stateid_t *,
162 struct nfsclds *, uint64_t, int, struct nfsfh *, struct mbuf *, int, int,
163 struct ucred *, NFSPROC_T *);
164 static enum nfsclds_state nfscl_getsameserver(struct nfsmount *,
165 struct nfsclds *, struct nfsclds **);
166 static int nfsio_commitds(vnode_t, uint64_t, int, struct nfsclds *,
167 struct nfsfh *, int, int, struct nfsclwritedsdorpc *, struct ucred *,
169 static int nfsrpc_commitds(vnode_t, uint64_t, int, struct nfsclds *,
170 struct nfsfh *, int, int, struct ucred *, NFSPROC_T *);
171 static void nfsrv_setuplayoutget(struct nfsrv_descript *, int, uint64_t,
172 uint64_t, uint64_t, nfsv4stateid_t *, int, int, int);
173 static int nfsrv_parseug(struct nfsrv_descript *, int, uid_t *, gid_t *,
175 static int nfsrv_parselayoutget(struct nfsrv_descript *, nfsv4stateid_t *,
176 int *, struct nfsclflayouthead *);
177 static int nfsrpc_getopenlayout(struct nfsmount *, vnode_t, u_int8_t *,
178 int, uint8_t *, int, uint32_t, struct nfsclopen *, uint8_t *, int,
179 struct nfscldeleg **, struct ucred *, NFSPROC_T *);
180 static int nfsrpc_getcreatelayout(vnode_t, char *, int, struct vattr *,
181 nfsquad_t, int, struct nfsclowner *, struct nfscldeleg **,
182 struct ucred *, NFSPROC_T *, struct nfsvattr *, struct nfsvattr *,
183 struct nfsfh **, int *, int *, void *, int *);
184 static int nfsrpc_openlayoutrpc(struct nfsmount *, vnode_t, u_int8_t *,
185 int, uint8_t *, int, uint32_t, struct nfsclopen *, uint8_t *, int,
186 struct nfscldeleg **, nfsv4stateid_t *, int, int, int, int *,
187 struct nfsclflayouthead *, int *, struct ucred *, NFSPROC_T *);
188 static int nfsrpc_createlayout(vnode_t, char *, int, struct vattr *,
189 nfsquad_t, int, struct nfsclowner *, struct nfscldeleg **,
190 struct ucred *, NFSPROC_T *, struct nfsvattr *, struct nfsvattr *,
191 struct nfsfh **, int *, int *, void *, int *, nfsv4stateid_t *,
192 int, int, int, int *, struct nfsclflayouthead *, int *);
193 static int nfsrpc_layoutget(struct nfsmount *, uint8_t *, int, int, uint64_t,
194 uint64_t, uint64_t, int, int, nfsv4stateid_t *, int *,
195 struct nfsclflayouthead *, struct ucred *, NFSPROC_T *, void *);
196 static int nfsrpc_layoutgetres(struct nfsmount *, vnode_t, uint8_t *,
197 int, nfsv4stateid_t *, int, uint32_t *, struct nfscllayout **,
198 struct nfsclflayouthead *, int, int, int *, struct ucred *, NFSPROC_T *);
200 int nfs_pnfsio(task_fn_t *, void *);
203 * nfs null call from vfs.
206 nfsrpc_null(vnode_t vp, struct ucred *cred, NFSPROC_T *p)
209 struct nfsrv_descript nfsd, *nd = &nfsd;
211 NFSCL_REQSTART(nd, NFSPROC_NULL, vp);
212 error = nfscl_request(nd, vp, p, cred, NULL);
213 if (nd->nd_repstat && !error)
214 error = nd->nd_repstat;
215 mbuf_freem(nd->nd_mrep);
221 * For nfs version 3 and 4, use the access rpc to check accessibility. If file
222 * modes are changed on the server, accesses might still fail later.
225 nfsrpc_access(vnode_t vp, int acmode, struct ucred *cred,
226 NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp)
229 u_int32_t mode, rmode;
232 mode = NFSACCESS_READ;
235 if (vnode_vtype(vp) == VDIR) {
237 mode |= (NFSACCESS_MODIFY | NFSACCESS_EXTEND |
240 mode |= NFSACCESS_LOOKUP;
243 mode |= (NFSACCESS_MODIFY | NFSACCESS_EXTEND);
245 mode |= NFSACCESS_EXECUTE;
249 * Now, just call nfsrpc_accessrpc() to do the actual RPC.
251 error = nfsrpc_accessrpc(vp, mode, cred, p, nap, attrflagp, &rmode,
255 * The NFS V3 spec does not clarify whether or not
256 * the returned access bits can be a superset of
257 * the ones requested, so...
259 if (!error && (rmode & mode) != mode)
265 * The actual rpc, separated out for Darwin.
268 nfsrpc_accessrpc(vnode_t vp, u_int32_t mode, struct ucred *cred,
269 NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, u_int32_t *rmodep,
273 u_int32_t supported, rmode;
275 struct nfsrv_descript nfsd, *nd = &nfsd;
276 nfsattrbit_t attrbits;
280 NFSCL_REQSTART(nd, NFSPROC_ACCESS, vp);
281 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
282 *tl = txdr_unsigned(mode);
283 if (nd->nd_flag & ND_NFSV4) {
285 * And do a Getattr op.
287 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
288 *tl = txdr_unsigned(NFSV4OP_GETATTR);
289 NFSGETATTR_ATTRBIT(&attrbits);
290 (void) nfsrv_putattrbit(nd, &attrbits);
292 error = nfscl_request(nd, vp, p, cred, stuff);
295 if (nd->nd_flag & ND_NFSV3) {
296 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
300 if (!nd->nd_repstat) {
301 if (nd->nd_flag & ND_NFSV4) {
302 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
303 supported = fxdr_unsigned(u_int32_t, *tl++);
305 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
307 rmode = fxdr_unsigned(u_int32_t, *tl);
308 if (nd->nd_flag & ND_NFSV4)
309 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
312 * It's not obvious what should be done about
313 * unsupported access modes. For now, be paranoid
314 * and clear the unsupported ones.
319 error = nd->nd_repstat;
321 mbuf_freem(nd->nd_mrep);
329 nfsrpc_open(vnode_t vp, int amode, struct ucred *cred, NFSPROC_T *p)
331 struct nfsclopen *op;
332 struct nfscldeleg *dp;
334 struct nfsnode *np = VTONFS(vp);
335 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
336 u_int32_t mode, clidrev;
337 int ret, newone, error, expireret = 0, retrycnt;
340 * For NFSv4, Open Ops are only done on Regular Files.
342 if (vnode_vtype(vp) != VREG)
346 mode |= NFSV4OPEN_ACCESSREAD;
348 mode |= NFSV4OPEN_ACCESSWRITE;
353 { char name[100]; int namel;
354 namel = (np->n_v4->n4_namelen < 100) ? np->n_v4->n4_namelen : 99;
355 bcopy(NFS4NODENAME(np->n_v4), name, namel);
357 printf("rpcopen p=0x%x name=%s",p->p_pid,name);
358 if (nfhp->nfh_len > 0) printf(" fh=0x%x\n",nfhp->nfh_fh[12]);
359 else printf(" fhl=0\n");
364 error = nfscl_open(vp, nfhp->nfh_fh, nfhp->nfh_len, mode, 1,
365 cred, p, NULL, &op, &newone, &ret, 1);
369 if (nmp->nm_clp != NULL)
370 clidrev = nmp->nm_clp->nfsc_clientidrev;
373 if (ret == NFSCLOPEN_DOOPEN) {
374 if (np->n_v4 != NULL) {
376 * For the first attempt, try and get a layout, if
377 * pNFS is enabled for the mount.
379 if (!NFSHASPNFS(nmp) || nfscl_enablecallb == 0 ||
380 nfs_numnfscbd == 0 ||
381 (np->n_flag & NNOLAYOUT) != 0 || retrycnt > 0)
382 error = nfsrpc_openrpc(nmp, vp,
384 np->n_v4->n4_fhlen, np->n_fhp->nfh_fh,
385 np->n_fhp->nfh_len, mode, op,
386 NFS4NODENAME(np->n_v4),
387 np->n_v4->n4_namelen,
388 &dp, 0, 0x0, cred, p, 0, 0);
390 error = nfsrpc_getopenlayout(nmp, vp,
392 np->n_v4->n4_fhlen, np->n_fhp->nfh_fh,
393 np->n_fhp->nfh_len, mode, op,
394 NFS4NODENAME(np->n_v4),
395 np->n_v4->n4_namelen, &dp, cred, p);
398 OSBitAndAtomic((int32_t)~NDELEGMOD, (UInt32 *)&np->n_flag);
401 np->n_flag &= ~NDELEGMOD;
403 * Invalidate the attribute cache, so that
404 * attributes that pre-date the issue of a
405 * delegation are not cached, since the
406 * cached attributes will remain valid while
407 * the delegation is held.
409 NFSINVALATTRCACHE(np);
412 (void) nfscl_deleg(nmp->nm_mountp,
413 op->nfso_own->nfsow_clp,
414 nfhp->nfh_fh, nfhp->nfh_len, cred, p, &dp);
419 newnfs_copyincred(cred, &op->nfso_cred);
420 } else if (ret == NFSCLOPEN_SETCRED)
422 * This is a new local open on a delegation. It needs
423 * to have credentials so that an open can be done
424 * against the server during recovery.
426 newnfs_copyincred(cred, &op->nfso_cred);
429 * nfso_opencnt is the count of how many VOP_OPEN()s have
430 * been done on this Open successfully and a VOP_CLOSE()
431 * is expected for each of these.
432 * If error is non-zero, don't increment it, since the Open
433 * hasn't succeeded yet.
437 nfscl_openrelease(nmp, op, error, newone);
438 if (error == NFSERR_GRACE || error == NFSERR_STALECLIENTID ||
439 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
440 error == NFSERR_BADSESSION) {
441 (void) nfs_catnap(PZERO, error, "nfs_open");
442 } else if ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID)
444 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
447 } while (error == NFSERR_GRACE || error == NFSERR_STALECLIENTID ||
448 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
449 error == NFSERR_BADSESSION ||
450 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
451 expireret == 0 && clidrev != 0 && retrycnt < 4));
452 if (error && retrycnt >= 4)
458 * the actual open rpc
461 nfsrpc_openrpc(struct nfsmount *nmp, vnode_t vp, u_int8_t *nfhp, int fhlen,
462 u_int8_t *newfhp, int newfhlen, u_int32_t mode, struct nfsclopen *op,
463 u_int8_t *name, int namelen, struct nfscldeleg **dpp,
464 int reclaim, u_int32_t delegtype, struct ucred *cred, NFSPROC_T *p,
465 int syscred, int recursed)
468 struct nfsrv_descript nfsd, *nd = &nfsd;
469 struct nfscldeleg *dp, *ndp = NULL;
470 struct nfsvattr nfsva;
471 u_int32_t rflags, deleg;
472 nfsattrbit_t attrbits;
473 int error, ret, acesize, limitby;
474 struct nfsclsession *tsep;
478 nfscl_reqstart(nd, NFSPROC_OPEN, nmp, nfhp, fhlen, NULL, NULL, 0, 0);
479 NFSM_BUILD(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
480 *tl++ = txdr_unsigned(op->nfso_own->nfsow_seqid);
481 *tl++ = txdr_unsigned(mode & NFSV4OPEN_ACCESSBOTH);
482 *tl++ = txdr_unsigned((mode >> NFSLCK_SHIFT) & NFSV4OPEN_DENYBOTH);
483 tsep = nfsmnt_mdssession(nmp);
484 *tl++ = tsep->nfsess_clientid.lval[0];
485 *tl = tsep->nfsess_clientid.lval[1];
486 (void) nfsm_strtom(nd, op->nfso_own->nfsow_owner, NFSV4CL_LOCKNAMELEN);
487 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
488 *tl++ = txdr_unsigned(NFSV4OPEN_NOCREATE);
490 *tl = txdr_unsigned(NFSV4OPEN_CLAIMPREVIOUS);
491 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
492 *tl = txdr_unsigned(delegtype);
495 *tl = txdr_unsigned(NFSV4OPEN_CLAIMDELEGATECUR);
496 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID);
497 if (NFSHASNFSV4N(nmp))
500 *tl++ = dp->nfsdl_stateid.seqid;
501 *tl++ = dp->nfsdl_stateid.other[0];
502 *tl++ = dp->nfsdl_stateid.other[1];
503 *tl = dp->nfsdl_stateid.other[2];
505 *tl = txdr_unsigned(NFSV4OPEN_CLAIMNULL);
507 (void) nfsm_strtom(nd, name, namelen);
509 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
510 *tl = txdr_unsigned(NFSV4OP_GETATTR);
511 NFSZERO_ATTRBIT(&attrbits);
512 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_CHANGE);
513 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEMODIFY);
514 (void) nfsrv_putattrbit(nd, &attrbits);
516 nd->nd_flag |= ND_USEGSSNAME;
517 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, vp, p, cred,
518 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
521 NFSCL_INCRSEQID(op->nfso_own->nfsow_seqid, nd);
522 if (!nd->nd_repstat) {
523 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
525 op->nfso_stateid.seqid = *tl++;
526 op->nfso_stateid.other[0] = *tl++;
527 op->nfso_stateid.other[1] = *tl++;
528 op->nfso_stateid.other[2] = *tl;
529 rflags = fxdr_unsigned(u_int32_t, *(tl + 6));
530 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
533 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
534 deleg = fxdr_unsigned(u_int32_t, *tl);
535 if (deleg == NFSV4OPEN_DELEGATEREAD ||
536 deleg == NFSV4OPEN_DELEGATEWRITE) {
537 if (!(op->nfso_own->nfsow_clp->nfsc_flags &
538 NFSCLFLAGS_FIRSTDELEG))
539 op->nfso_own->nfsow_clp->nfsc_flags |=
540 (NFSCLFLAGS_FIRSTDELEG | NFSCLFLAGS_GOTDELEG);
541 MALLOC(ndp, struct nfscldeleg *,
542 sizeof (struct nfscldeleg) + newfhlen,
543 M_NFSCLDELEG, M_WAITOK);
544 LIST_INIT(&ndp->nfsdl_owner);
545 LIST_INIT(&ndp->nfsdl_lock);
546 ndp->nfsdl_clp = op->nfso_own->nfsow_clp;
547 ndp->nfsdl_fhlen = newfhlen;
548 NFSBCOPY(newfhp, ndp->nfsdl_fh, newfhlen);
549 newnfs_copyincred(cred, &ndp->nfsdl_cred);
550 nfscl_lockinit(&ndp->nfsdl_rwlock);
551 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
553 ndp->nfsdl_stateid.seqid = *tl++;
554 ndp->nfsdl_stateid.other[0] = *tl++;
555 ndp->nfsdl_stateid.other[1] = *tl++;
556 ndp->nfsdl_stateid.other[2] = *tl++;
557 ret = fxdr_unsigned(int, *tl);
558 if (deleg == NFSV4OPEN_DELEGATEWRITE) {
559 ndp->nfsdl_flags = NFSCLDL_WRITE;
561 * Indicates how much the file can grow.
563 NFSM_DISSECT(tl, u_int32_t *,
565 limitby = fxdr_unsigned(int, *tl++);
567 case NFSV4OPEN_LIMITSIZE:
568 ndp->nfsdl_sizelimit = fxdr_hyper(tl);
570 case NFSV4OPEN_LIMITBLOCKS:
571 ndp->nfsdl_sizelimit =
572 fxdr_unsigned(u_int64_t, *tl++);
573 ndp->nfsdl_sizelimit *=
574 fxdr_unsigned(u_int64_t, *tl);
577 error = NFSERR_BADXDR;
581 ndp->nfsdl_flags = NFSCLDL_READ;
584 ndp->nfsdl_flags |= NFSCLDL_RECALL;
585 error = nfsrv_dissectace(nd, &ndp->nfsdl_ace, &ret,
589 } else if (deleg != NFSV4OPEN_DELEGATENONE) {
590 error = NFSERR_BADXDR;
593 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
594 error = nfsv4_loadattr(nd, NULL, &nfsva, NULL,
595 NULL, 0, NULL, NULL, NULL, NULL, NULL, 0,
596 NULL, NULL, NULL, p, cred);
600 ndp->nfsdl_change = nfsva.na_filerev;
601 ndp->nfsdl_modtime = nfsva.na_mtime;
602 ndp->nfsdl_flags |= NFSCLDL_MODTIMESET;
604 if (!reclaim && (rflags & NFSV4OPEN_RESULTCONFIRM)) {
606 ret = nfsrpc_openconfirm(vp, newfhp, newfhlen, op,
608 if (ret == NFSERR_DELAY)
609 (void) nfs_catnap(PZERO, ret, "nfs_open");
610 } while (ret == NFSERR_DELAY);
613 if ((rflags & NFSV4OPEN_LOCKTYPEPOSIX) ||
614 nfscl_assumeposixlocks)
615 op->nfso_posixlock = 1;
617 op->nfso_posixlock = 0;
620 * If the server is handing out delegations, but we didn't
621 * get one because an OpenConfirm was required, try the
622 * Open again, to get a delegation. This is a harmless no-op,
623 * from a server's point of view.
625 if (!reclaim && (rflags & NFSV4OPEN_RESULTCONFIRM) &&
626 (op->nfso_own->nfsow_clp->nfsc_flags & NFSCLFLAGS_GOTDELEG)
627 && !error && dp == NULL && ndp == NULL && !recursed) {
629 ret = nfsrpc_openrpc(nmp, vp, nfhp, fhlen, newfhp,
630 newfhlen, mode, op, name, namelen, &ndp, 0, 0x0,
631 cred, p, syscred, 1);
632 if (ret == NFSERR_DELAY)
633 (void) nfs_catnap(PZERO, ret, "nfs_open2");
634 } while (ret == NFSERR_DELAY);
637 FREE((caddr_t)ndp, M_NFSCLDELEG);
640 if (ret == NFSERR_STALECLIENTID ||
641 ret == NFSERR_STALEDONTRECOVER ||
642 ret == NFSERR_BADSESSION)
647 if (nd->nd_repstat != 0 && error == 0)
648 error = nd->nd_repstat;
649 if (error == NFSERR_STALECLIENTID)
650 nfscl_initiate_recovery(op->nfso_own->nfsow_clp);
654 else if (ndp != NULL)
655 FREE((caddr_t)ndp, M_NFSCLDELEG);
656 mbuf_freem(nd->nd_mrep);
664 nfsrpc_opendowngrade(vnode_t vp, u_int32_t mode, struct nfsclopen *op,
665 struct ucred *cred, NFSPROC_T *p)
668 struct nfsrv_descript nfsd, *nd = &nfsd;
671 NFSCL_REQSTART(nd, NFSPROC_OPENDOWNGRADE, vp);
672 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID + 3 * NFSX_UNSIGNED);
673 if (NFSHASNFSV4N(VFSTONFS(vnode_mount(vp))))
676 *tl++ = op->nfso_stateid.seqid;
677 *tl++ = op->nfso_stateid.other[0];
678 *tl++ = op->nfso_stateid.other[1];
679 *tl++ = op->nfso_stateid.other[2];
680 *tl++ = txdr_unsigned(op->nfso_own->nfsow_seqid);
681 *tl++ = txdr_unsigned(mode & NFSV4OPEN_ACCESSBOTH);
682 *tl = txdr_unsigned((mode >> NFSLCK_SHIFT) & NFSV4OPEN_DENYBOTH);
683 error = nfscl_request(nd, vp, p, cred, NULL);
686 NFSCL_INCRSEQID(op->nfso_own->nfsow_seqid, nd);
687 if (!nd->nd_repstat) {
688 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID);
689 op->nfso_stateid.seqid = *tl++;
690 op->nfso_stateid.other[0] = *tl++;
691 op->nfso_stateid.other[1] = *tl++;
692 op->nfso_stateid.other[2] = *tl;
694 if (nd->nd_repstat && error == 0)
695 error = nd->nd_repstat;
696 if (error == NFSERR_STALESTATEID)
697 nfscl_initiate_recovery(op->nfso_own->nfsow_clp);
699 mbuf_freem(nd->nd_mrep);
704 * V4 Close operation.
707 nfsrpc_close(vnode_t vp, int doclose, NFSPROC_T *p)
709 struct nfsclclient *clp;
712 if (vnode_vtype(vp) != VREG)
715 error = nfscl_doclose(vp, &clp, p);
717 error = nfscl_getclose(vp, &clp);
721 nfscl_clientrelease(clp);
729 nfsrpc_doclose(struct nfsmount *nmp, struct nfsclopen *op, NFSPROC_T *p)
731 struct nfsrv_descript nfsd, *nd = &nfsd;
732 struct nfscllockowner *lp, *nlp;
733 struct nfscllock *lop, *nlop;
735 u_int64_t off = 0, len = 0;
736 u_int32_t type = NFSV4LOCKT_READ;
737 int error, do_unlock, trycnt;
739 tcred = newnfs_getcred();
740 newnfs_copycred(&op->nfso_cred, tcred);
742 * (Theoretically this could be done in the same
743 * compound as the close, but having multiple
744 * sequenced Ops in the same compound might be
745 * too scary for some servers.)
747 if (op->nfso_posixlock) {
750 type = NFSV4LOCKT_READ;
754 * Since this function is only called from VOP_INACTIVE(), no
755 * other thread will be manipulating this Open. As such, the
756 * lock lists are not being changed by other threads, so it should
757 * be safe to do this without locking.
759 LIST_FOREACH(lp, &op->nfso_lock, nfsl_list) {
761 LIST_FOREACH_SAFE(lop, &lp->nfsl_lock, nfslo_list, nlop) {
762 if (op->nfso_posixlock == 0) {
763 off = lop->nfslo_first;
764 len = lop->nfslo_end - lop->nfslo_first;
765 if (lop->nfslo_type == F_WRLCK)
766 type = NFSV4LOCKT_WRITE;
768 type = NFSV4LOCKT_READ;
773 error = nfsrpc_locku(nd, nmp, lp, off,
774 len, type, tcred, p, 0);
775 if ((nd->nd_repstat == NFSERR_GRACE ||
776 nd->nd_repstat == NFSERR_DELAY) &&
778 (void) nfs_catnap(PZERO,
781 } while ((nd->nd_repstat == NFSERR_GRACE ||
782 nd->nd_repstat == NFSERR_DELAY) &&
783 error == 0 && trycnt++ < 5);
784 if (op->nfso_posixlock)
787 nfscl_freelock(lop, 0);
790 * Do a ReleaseLockOwner.
791 * The lock owner name nfsl_owner may be used by other opens for
792 * other files but the lock_owner4 name that nfsrpc_rellockown()
793 * puts on the wire has the file handle for this file appended
794 * to it, so it can be done now.
796 (void)nfsrpc_rellockown(nmp, lp, lp->nfsl_open->nfso_fh,
797 lp->nfsl_open->nfso_fhlen, tcred, p);
801 * There could be other Opens for different files on the same
802 * OpenOwner, so locking is required.
805 nfscl_lockexcl(&op->nfso_own->nfsow_rwlock, NFSCLSTATEMUTEXPTR);
808 error = nfscl_tryclose(op, tcred, nmp, p);
809 if (error == NFSERR_GRACE)
810 (void) nfs_catnap(PZERO, error, "nfs_close");
811 } while (error == NFSERR_GRACE);
813 nfscl_lockunlock(&op->nfso_own->nfsow_rwlock);
815 LIST_FOREACH_SAFE(lp, &op->nfso_lock, nfsl_list, nlp)
816 nfscl_freelockowner(lp, 0);
817 nfscl_freeopen(op, 0);
823 * The actual Close RPC.
826 nfsrpc_closerpc(struct nfsrv_descript *nd, struct nfsmount *nmp,
827 struct nfsclopen *op, struct ucred *cred, NFSPROC_T *p,
833 nfscl_reqstart(nd, NFSPROC_CLOSE, nmp, op->nfso_fh,
834 op->nfso_fhlen, NULL, NULL, 0, 0);
835 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED + NFSX_STATEID);
836 *tl++ = txdr_unsigned(op->nfso_own->nfsow_seqid);
837 if (NFSHASNFSV4N(nmp))
840 *tl++ = op->nfso_stateid.seqid;
841 *tl++ = op->nfso_stateid.other[0];
842 *tl++ = op->nfso_stateid.other[1];
843 *tl = op->nfso_stateid.other[2];
845 nd->nd_flag |= ND_USEGSSNAME;
846 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
847 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
850 NFSCL_INCRSEQID(op->nfso_own->nfsow_seqid, nd);
851 if (nd->nd_repstat == 0)
852 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID);
853 error = nd->nd_repstat;
854 if (error == NFSERR_STALESTATEID)
855 nfscl_initiate_recovery(op->nfso_own->nfsow_clp);
857 mbuf_freem(nd->nd_mrep);
862 * V4 Open Confirm RPC.
865 nfsrpc_openconfirm(vnode_t vp, u_int8_t *nfhp, int fhlen,
866 struct nfsclopen *op, struct ucred *cred, NFSPROC_T *p)
869 struct nfsrv_descript nfsd, *nd = &nfsd;
870 struct nfsmount *nmp;
873 nmp = VFSTONFS(vnode_mount(vp));
874 if (NFSHASNFSV4N(nmp))
875 return (0); /* No confirmation for NFSv4.1. */
876 nfscl_reqstart(nd, NFSPROC_OPENCONFIRM, nmp, nfhp, fhlen, NULL, NULL,
878 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED + NFSX_STATEID);
879 *tl++ = op->nfso_stateid.seqid;
880 *tl++ = op->nfso_stateid.other[0];
881 *tl++ = op->nfso_stateid.other[1];
882 *tl++ = op->nfso_stateid.other[2];
883 *tl = txdr_unsigned(op->nfso_own->nfsow_seqid);
884 error = nfscl_request(nd, vp, p, cred, NULL);
887 NFSCL_INCRSEQID(op->nfso_own->nfsow_seqid, nd);
888 if (!nd->nd_repstat) {
889 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID);
890 op->nfso_stateid.seqid = *tl++;
891 op->nfso_stateid.other[0] = *tl++;
892 op->nfso_stateid.other[1] = *tl++;
893 op->nfso_stateid.other[2] = *tl;
895 error = nd->nd_repstat;
896 if (error == NFSERR_STALESTATEID)
897 nfscl_initiate_recovery(op->nfso_own->nfsow_clp);
899 mbuf_freem(nd->nd_mrep);
904 * Do the setclientid and setclientid confirm RPCs. Called from nfs_statfs()
905 * when a mount has just occurred and when the server replies NFSERR_EXPIRED.
908 nfsrpc_setclient(struct nfsmount *nmp, struct nfsclclient *clp, int reclaim,
909 struct ucred *cred, NFSPROC_T *p)
912 struct nfsrv_descript nfsd;
913 struct nfsrv_descript *nd = &nfsd;
914 nfsattrbit_t attrbits;
915 u_int8_t *cp = NULL, *cp2, addr[INET6_ADDRSTRLEN + 9];
917 int error, isinet6 = 0, callblen;
920 static u_int32_t rev = 0;
923 struct nfsclsession *tsep;
925 if (nfsboottime.tv_sec == 0)
926 NFSSETBOOTTIME(nfsboottime);
927 clp->nfsc_rev = rev++;
928 if (NFSHASNFSV4N(nmp)) {
930 * Either there was no previous session or the
931 * previous session has failed, so...
932 * do an ExchangeID followed by the CreateSession.
934 error = nfsrpc_exchangeid(nmp, clp, &nmp->nm_sockreq,
935 NFSV4EXCH_USEPNFSMDS | NFSV4EXCH_USENONPNFS, &dsp, cred, p);
936 NFSCL_DEBUG(1, "aft exch=%d\n", error);
938 error = nfsrpc_createsession(nmp, &dsp->nfsclds_sess,
940 dsp->nfsclds_sess.nfsess_sequenceid, 1, cred, p);
944 * The old sessions cannot be safely free'd
945 * here, since they may still be used by
949 if (TAILQ_FIRST(&nmp->nm_sess) != NULL)
950 tsep = NFSMNT_MDSSESSION(nmp);
951 TAILQ_INSERT_HEAD(&nmp->nm_sess, dsp,
954 * Wake up RPCs waiting for a slot on the
955 * old session. These will then fail with
956 * NFSERR_BADSESSION and be retried with the
957 * new session by nfsv4_setsequence().
958 * Also wakeup() processes waiting for the
962 wakeup(&tsep->nfsess_slots);
963 wakeup(&nmp->nm_sess);
966 nfscl_freenfsclds(dsp);
967 NFSCL_DEBUG(1, "aft createsess=%d\n", error);
968 if (error == 0 && reclaim == 0) {
969 error = nfsrpc_reclaimcomplete(nmp, cred, p);
970 NFSCL_DEBUG(1, "aft reclaimcomp=%d\n", error);
971 if (error == NFSERR_COMPLETEALREADY ||
972 error == NFSERR_NOTSUPP)
973 /* Ignore this error. */
980 * Allocate a single session structure for NFSv4.0, because some of
981 * the fields are used by NFSv4.0 although it doesn't do a session.
983 dsp = malloc(sizeof(struct nfsclds), M_NFSCLDS, M_WAITOK | M_ZERO);
984 mtx_init(&dsp->nfsclds_mtx, "nfsds", NULL, MTX_DEF);
985 mtx_init(&dsp->nfsclds_sess.nfsess_mtx, "nfssession", NULL, MTX_DEF);
987 TAILQ_INSERT_HEAD(&nmp->nm_sess, dsp, nfsclds_list);
988 tsep = NFSMNT_MDSSESSION(nmp);
991 nfscl_reqstart(nd, NFSPROC_SETCLIENTID, nmp, NULL, 0, NULL, NULL, 0, 0);
992 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
993 *tl++ = txdr_unsigned(nfsboottime.tv_sec);
994 *tl = txdr_unsigned(clp->nfsc_rev);
995 (void) nfsm_strtom(nd, clp->nfsc_id, clp->nfsc_idlen);
998 * set up the callback address
1000 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1001 *tl = txdr_unsigned(NFS_CALLBCKPROG);
1002 callblen = strlen(nfsv4_callbackaddr);
1004 cp = nfscl_getmyip(nmp, &a6, &isinet6);
1005 if (nfscl_enablecallb && nfs_numnfscbd > 0 &&
1006 (callblen > 0 || cp != NULL)) {
1007 port = htons(nfsv4_cbport);
1008 cp2 = (u_int8_t *)&port;
1010 if ((callblen > 0 &&
1011 strchr(nfsv4_callbackaddr, ':')) || isinet6) {
1012 char ip6buf[INET6_ADDRSTRLEN], *ip6add;
1014 (void) nfsm_strtom(nd, "tcp6", 4);
1015 if (callblen == 0) {
1016 ip6_sprintf(ip6buf, (struct in6_addr *)cp);
1019 ip6add = nfsv4_callbackaddr;
1021 snprintf(addr, INET6_ADDRSTRLEN + 9, "%s.%d.%d",
1022 ip6add, cp2[0], cp2[1]);
1026 (void) nfsm_strtom(nd, "tcp", 3);
1028 snprintf(addr, INET6_ADDRSTRLEN + 9,
1029 "%d.%d.%d.%d.%d.%d", cp[0], cp[1],
1030 cp[2], cp[3], cp2[0], cp2[1]);
1032 snprintf(addr, INET6_ADDRSTRLEN + 9,
1033 "%s.%d.%d", nfsv4_callbackaddr,
1036 (void) nfsm_strtom(nd, addr, strlen(addr));
1038 (void) nfsm_strtom(nd, "tcp", 3);
1039 (void) nfsm_strtom(nd, "0.0.0.0.0.0", 11);
1041 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1042 *tl = txdr_unsigned(clp->nfsc_cbident);
1043 nd->nd_flag |= ND_USEGSSNAME;
1044 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
1045 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
1048 if (nd->nd_repstat == 0) {
1049 NFSM_DISSECT(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1050 tsep->nfsess_clientid.lval[0] = *tl++;
1051 tsep->nfsess_clientid.lval[1] = *tl++;
1052 confirm.lval[0] = *tl++;
1053 confirm.lval[1] = *tl;
1054 mbuf_freem(nd->nd_mrep);
1060 nfscl_reqstart(nd, NFSPROC_SETCLIENTIDCFRM, nmp, NULL, 0, NULL,
1062 NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1063 *tl++ = tsep->nfsess_clientid.lval[0];
1064 *tl++ = tsep->nfsess_clientid.lval[1];
1065 *tl++ = confirm.lval[0];
1066 *tl = confirm.lval[1];
1067 nd->nd_flag |= ND_USEGSSNAME;
1068 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p,
1069 cred, NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
1072 mbuf_freem(nd->nd_mrep);
1074 if (nd->nd_repstat == 0) {
1075 nfscl_reqstart(nd, NFSPROC_GETATTR, nmp, nmp->nm_fh,
1076 nmp->nm_fhsize, NULL, NULL, 0, 0);
1077 NFSZERO_ATTRBIT(&attrbits);
1078 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_LEASETIME);
1079 (void) nfsrv_putattrbit(nd, &attrbits);
1080 nd->nd_flag |= ND_USEGSSNAME;
1081 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p,
1082 cred, NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
1085 if (nd->nd_repstat == 0) {
1086 error = nfsv4_loadattr(nd, NULL, NULL, NULL, NULL, 0, NULL,
1087 NULL, NULL, NULL, NULL, 0, NULL, &lease, NULL, p, cred);
1090 clp->nfsc_renew = NFSCL_RENEW(lease);
1091 clp->nfsc_expire = NFSD_MONOSEC + clp->nfsc_renew;
1092 clp->nfsc_clientidrev++;
1093 if (clp->nfsc_clientidrev == 0)
1094 clp->nfsc_clientidrev++;
1098 error = nd->nd_repstat;
1100 mbuf_freem(nd->nd_mrep);
1108 nfsrpc_getattr(vnode_t vp, struct ucred *cred, NFSPROC_T *p,
1109 struct nfsvattr *nap, void *stuff)
1111 struct nfsrv_descript nfsd, *nd = &nfsd;
1113 nfsattrbit_t attrbits;
1115 NFSCL_REQSTART(nd, NFSPROC_GETATTR, vp);
1116 if (nd->nd_flag & ND_NFSV4) {
1117 NFSGETATTR_ATTRBIT(&attrbits);
1118 (void) nfsrv_putattrbit(nd, &attrbits);
1120 error = nfscl_request(nd, vp, p, cred, stuff);
1123 if (!nd->nd_repstat)
1124 error = nfsm_loadattr(nd, nap);
1126 error = nd->nd_repstat;
1127 mbuf_freem(nd->nd_mrep);
1132 * nfs getattr call with non-vnode arguemnts.
1135 nfsrpc_getattrnovp(struct nfsmount *nmp, u_int8_t *fhp, int fhlen, int syscred,
1136 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, u_int64_t *xidp,
1139 struct nfsrv_descript nfsd, *nd = &nfsd;
1140 int error, vers = NFS_VER2;
1141 nfsattrbit_t attrbits;
1143 nfscl_reqstart(nd, NFSPROC_GETATTR, nmp, fhp, fhlen, NULL, NULL, 0, 0);
1144 if (nd->nd_flag & ND_NFSV4) {
1146 NFSGETATTR_ATTRBIT(&attrbits);
1147 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_LEASETIME);
1148 (void) nfsrv_putattrbit(nd, &attrbits);
1149 } else if (nd->nd_flag & ND_NFSV3) {
1153 nd->nd_flag |= ND_USEGSSNAME;
1154 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
1155 NFS_PROG, vers, NULL, 1, xidp, NULL);
1158 if (nd->nd_repstat == 0) {
1159 if ((nd->nd_flag & ND_NFSV4) != 0)
1160 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0,
1161 NULL, NULL, NULL, NULL, NULL, 0, NULL, leasep, NULL,
1164 error = nfsm_loadattr(nd, nap);
1166 error = nd->nd_repstat;
1167 mbuf_freem(nd->nd_mrep);
1172 * Do an nfs setattr operation.
1175 nfsrpc_setattr(vnode_t vp, struct vattr *vap, NFSACL_T *aclp,
1176 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *rnap, int *attrflagp,
1179 int error, expireret = 0, openerr, retrycnt;
1180 u_int32_t clidrev = 0, mode;
1181 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
1183 nfsv4stateid_t stateid;
1186 if (nmp->nm_clp != NULL)
1187 clidrev = nmp->nm_clp->nfsc_clientidrev;
1188 if (vap != NULL && NFSATTRISSET(u_quad_t, vap, va_size))
1189 mode = NFSV4OPEN_ACCESSWRITE;
1191 mode = NFSV4OPEN_ACCESSREAD;
1196 if (NFSHASNFSV4(nmp)) {
1197 nfhp = VTONFS(vp)->n_fhp;
1198 error = nfscl_getstateid(vp, nfhp->nfh_fh,
1199 nfhp->nfh_len, mode, 0, cred, p, &stateid, &lckp);
1200 if (error && vnode_vtype(vp) == VREG &&
1201 (mode == NFSV4OPEN_ACCESSWRITE ||
1202 nfstest_openallsetattr)) {
1204 * No Open stateid, so try and open the file
1207 if (mode == NFSV4OPEN_ACCESSWRITE)
1208 openerr = nfsrpc_open(vp, FWRITE, cred,
1211 openerr = nfsrpc_open(vp, FREAD, cred,
1214 (void) nfscl_getstateid(vp,
1215 nfhp->nfh_fh, nfhp->nfh_len,
1216 mode, 0, cred, p, &stateid, &lckp);
1220 error = nfsrpc_setattrrpc(vp, vap, &stateid, cred, p,
1221 rnap, attrflagp, stuff);
1223 error = nfsrpc_setaclrpc(vp, cred, p, aclp, &stateid,
1225 if (error == NFSERR_OPENMODE && mode == NFSV4OPEN_ACCESSREAD) {
1227 nmp->nm_state |= NFSSTA_OPENMODE;
1230 if (error == NFSERR_STALESTATEID)
1231 nfscl_initiate_recovery(nmp->nm_clp);
1233 nfscl_lockderef(lckp);
1235 (void) nfsrpc_close(vp, 0, p);
1236 if (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
1237 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
1238 error == NFSERR_OLDSTATEID || error == NFSERR_BADSESSION) {
1239 (void) nfs_catnap(PZERO, error, "nfs_setattr");
1240 } else if ((error == NFSERR_EXPIRED ||
1241 error == NFSERR_BADSTATEID) && clidrev != 0) {
1242 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
1245 } while (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
1246 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
1247 error == NFSERR_BADSESSION ||
1248 (error == NFSERR_OLDSTATEID && retrycnt < 20) ||
1249 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
1250 expireret == 0 && clidrev != 0 && retrycnt < 4) ||
1251 (error == NFSERR_OPENMODE && mode == NFSV4OPEN_ACCESSREAD &&
1253 if (error && retrycnt >= 4)
1259 nfsrpc_setattrrpc(vnode_t vp, struct vattr *vap,
1260 nfsv4stateid_t *stateidp, struct ucred *cred, NFSPROC_T *p,
1261 struct nfsvattr *rnap, int *attrflagp, void *stuff)
1264 struct nfsrv_descript nfsd, *nd = &nfsd;
1266 nfsattrbit_t attrbits;
1269 NFSCL_REQSTART(nd, NFSPROC_SETATTR, vp);
1270 if (nd->nd_flag & ND_NFSV4)
1271 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID);
1272 vap->va_type = vnode_vtype(vp);
1273 nfscl_fillsattr(nd, vap, vp, NFSSATTR_FULL, 0);
1274 if (nd->nd_flag & ND_NFSV3) {
1275 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1277 } else if (nd->nd_flag & ND_NFSV4) {
1278 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1279 *tl = txdr_unsigned(NFSV4OP_GETATTR);
1280 NFSGETATTR_ATTRBIT(&attrbits);
1281 (void) nfsrv_putattrbit(nd, &attrbits);
1283 error = nfscl_request(nd, vp, p, cred, stuff);
1286 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4))
1287 error = nfscl_wcc_data(nd, vp, rnap, attrflagp, NULL, stuff);
1288 if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) == ND_NFSV4 && !error)
1289 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
1290 if (!(nd->nd_flag & ND_NFSV3) && !nd->nd_repstat && !error)
1291 error = nfscl_postop_attr(nd, rnap, attrflagp, stuff);
1292 mbuf_freem(nd->nd_mrep);
1293 if (nd->nd_repstat && !error)
1294 error = nd->nd_repstat;
1302 nfsrpc_lookup(vnode_t dvp, char *name, int len, struct ucred *cred,
1303 NFSPROC_T *p, struct nfsvattr *dnap, struct nfsvattr *nap,
1304 struct nfsfh **nfhpp, int *attrflagp, int *dattrflagp, void *stuff)
1307 struct nfsrv_descript nfsd, *nd = &nfsd;
1308 struct nfsmount *nmp;
1311 nfsattrbit_t attrbits;
1312 int error = 0, lookupp = 0;
1316 if (vnode_vtype(dvp) != VDIR)
1318 nmp = VFSTONFS(vnode_mount(dvp));
1319 if (len > NFS_MAXNAMLEN)
1320 return (ENAMETOOLONG);
1321 if (NFSHASNFSV4(nmp) && len == 1 &&
1324 * Just return the current dir's fh.
1327 MALLOC(nfhp, struct nfsfh *, sizeof (struct nfsfh) +
1328 np->n_fhp->nfh_len, M_NFSFH, M_WAITOK);
1329 nfhp->nfh_len = np->n_fhp->nfh_len;
1330 NFSBCOPY(np->n_fhp->nfh_fh, nfhp->nfh_fh, nfhp->nfh_len);
1334 if (NFSHASNFSV4(nmp) && len == 2 &&
1335 name[0] == '.' && name[1] == '.') {
1337 NFSCL_REQSTART(nd, NFSPROC_LOOKUPP, dvp);
1339 NFSCL_REQSTART(nd, NFSPROC_LOOKUP, dvp);
1340 (void) nfsm_strtom(nd, name, len);
1342 if (nd->nd_flag & ND_NFSV4) {
1343 NFSGETATTR_ATTRBIT(&attrbits);
1344 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1345 *tl++ = txdr_unsigned(NFSV4OP_GETFH);
1346 *tl = txdr_unsigned(NFSV4OP_GETATTR);
1347 (void) nfsrv_putattrbit(nd, &attrbits);
1349 error = nfscl_request(nd, dvp, p, cred, stuff);
1352 if (nd->nd_repstat) {
1354 * When an NFSv4 Lookupp returns ENOENT, it means that
1355 * the lookup is at the root of an fs, so return this dir.
1357 if (nd->nd_repstat == NFSERR_NOENT && lookupp) {
1359 MALLOC(nfhp, struct nfsfh *, sizeof (struct nfsfh) +
1360 np->n_fhp->nfh_len, M_NFSFH, M_WAITOK);
1361 nfhp->nfh_len = np->n_fhp->nfh_len;
1362 NFSBCOPY(np->n_fhp->nfh_fh, nfhp->nfh_fh, nfhp->nfh_len);
1364 mbuf_freem(nd->nd_mrep);
1367 if (nd->nd_flag & ND_NFSV3)
1368 error = nfscl_postop_attr(nd, dnap, dattrflagp, stuff);
1369 else if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) ==
1371 /* Load the directory attributes. */
1372 error = nfsm_loadattr(nd, dnap);
1378 if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) == ND_NFSV4) {
1379 /* Load the directory attributes. */
1380 error = nfsm_loadattr(nd, dnap);
1384 /* Skip over the Lookup and GetFH operation status values. */
1385 NFSM_DISSECT(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1387 error = nfsm_getfh(nd, nfhpp);
1391 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
1392 if ((nd->nd_flag & ND_NFSV3) && !error)
1393 error = nfscl_postop_attr(nd, dnap, dattrflagp, stuff);
1395 mbuf_freem(nd->nd_mrep);
1396 if (!error && nd->nd_repstat)
1397 error = nd->nd_repstat;
1402 * Do a readlink rpc.
1405 nfsrpc_readlink(vnode_t vp, struct uio *uiop, struct ucred *cred,
1406 NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, void *stuff)
1409 struct nfsrv_descript nfsd, *nd = &nfsd;
1410 struct nfsnode *np = VTONFS(vp);
1411 nfsattrbit_t attrbits;
1412 int error, len, cangetattr = 1;
1415 NFSCL_REQSTART(nd, NFSPROC_READLINK, vp);
1416 if (nd->nd_flag & ND_NFSV4) {
1418 * And do a Getattr op.
1420 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1421 *tl = txdr_unsigned(NFSV4OP_GETATTR);
1422 NFSGETATTR_ATTRBIT(&attrbits);
1423 (void) nfsrv_putattrbit(nd, &attrbits);
1425 error = nfscl_request(nd, vp, p, cred, stuff);
1428 if (nd->nd_flag & ND_NFSV3)
1429 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
1430 if (!nd->nd_repstat && !error) {
1431 NFSM_STRSIZ(len, NFS_MAXPATHLEN);
1433 * This seems weird to me, but must have been added to
1434 * FreeBSD for some reason. The only thing I can think of
1435 * is that there was/is some server that replies with
1436 * more link data than it should?
1438 if (len == NFS_MAXPATHLEN) {
1440 if (np->n_size > 0 && np->n_size < NFS_MAXPATHLEN) {
1446 error = nfsm_mbufuio(nd, uiop, len);
1447 if ((nd->nd_flag & ND_NFSV4) && !error && cangetattr)
1448 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
1450 if (nd->nd_repstat && !error)
1451 error = nd->nd_repstat;
1453 mbuf_freem(nd->nd_mrep);
1461 nfsrpc_read(vnode_t vp, struct uio *uiop, struct ucred *cred,
1462 NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, void *stuff)
1464 int error, expireret = 0, retrycnt;
1465 u_int32_t clidrev = 0;
1466 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
1467 struct nfsnode *np = VTONFS(vp);
1468 struct ucred *newcred;
1469 struct nfsfh *nfhp = NULL;
1470 nfsv4stateid_t stateid;
1473 if (nmp->nm_clp != NULL)
1474 clidrev = nmp->nm_clp->nfsc_clientidrev;
1476 if (NFSHASNFSV4(nmp)) {
1478 newcred = NFSNEWCRED(cred);
1483 if (NFSHASNFSV4(nmp))
1484 (void)nfscl_getstateid(vp, nfhp->nfh_fh, nfhp->nfh_len,
1485 NFSV4OPEN_ACCESSREAD, 0, newcred, p, &stateid,
1487 error = nfsrpc_readrpc(vp, uiop, newcred, &stateid, p, nap,
1489 if (error == NFSERR_OPENMODE) {
1491 nmp->nm_state |= NFSSTA_OPENMODE;
1494 if (error == NFSERR_STALESTATEID)
1495 nfscl_initiate_recovery(nmp->nm_clp);
1497 nfscl_lockderef(lckp);
1498 if (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
1499 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
1500 error == NFSERR_OLDSTATEID || error == NFSERR_BADSESSION) {
1501 (void) nfs_catnap(PZERO, error, "nfs_read");
1502 } else if ((error == NFSERR_EXPIRED ||
1503 error == NFSERR_BADSTATEID) && clidrev != 0) {
1504 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
1507 } while (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
1508 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
1509 error == NFSERR_BADSESSION ||
1510 (error == NFSERR_OLDSTATEID && retrycnt < 20) ||
1511 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
1512 expireret == 0 && clidrev != 0 && retrycnt < 4) ||
1513 (error == NFSERR_OPENMODE && retrycnt < 4));
1514 if (error && retrycnt >= 4)
1516 if (NFSHASNFSV4(nmp))
1517 NFSFREECRED(newcred);
1522 * The actual read RPC.
1525 nfsrpc_readrpc(vnode_t vp, struct uio *uiop, struct ucred *cred,
1526 nfsv4stateid_t *stateidp, NFSPROC_T *p, struct nfsvattr *nap,
1527 int *attrflagp, void *stuff)
1530 int error = 0, len, retlen, tsiz, eof = 0;
1531 struct nfsrv_descript nfsd;
1532 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
1533 struct nfsrv_descript *nd = &nfsd;
1538 tsiz = uio_uio_resid(uiop);
1539 tmp_off = uiop->uio_offset + tsiz;
1541 if (tmp_off > nmp->nm_maxfilesize || tmp_off < uiop->uio_offset) {
1545 rsize = nmp->nm_rsize;
1550 len = (tsiz > rsize) ? rsize : tsiz;
1551 NFSCL_REQSTART(nd, NFSPROC_READ, vp);
1552 if (nd->nd_flag & ND_NFSV4)
1553 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID);
1554 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED * 3);
1555 if (nd->nd_flag & ND_NFSV2) {
1556 *tl++ = txdr_unsigned(uiop->uio_offset);
1557 *tl++ = txdr_unsigned(len);
1560 txdr_hyper(uiop->uio_offset, tl);
1561 *(tl + 2) = txdr_unsigned(len);
1564 * Since I can't do a Getattr for NFSv4 for Write, there
1565 * doesn't seem any point in doing one here, either.
1566 * (See the comment in nfsrpc_writerpc() for more info.)
1568 error = nfscl_request(nd, vp, p, cred, stuff);
1571 if (nd->nd_flag & ND_NFSV3) {
1572 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
1573 } else if (!nd->nd_repstat && (nd->nd_flag & ND_NFSV2)) {
1574 error = nfsm_loadattr(nd, nap);
1578 if (nd->nd_repstat || error) {
1580 error = nd->nd_repstat;
1583 if (nd->nd_flag & ND_NFSV3) {
1584 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1585 eof = fxdr_unsigned(int, *(tl + 1));
1586 } else if (nd->nd_flag & ND_NFSV4) {
1587 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
1588 eof = fxdr_unsigned(int, *tl);
1590 NFSM_STRSIZ(retlen, len);
1591 error = nfsm_mbufuio(nd, uiop, retlen);
1594 mbuf_freem(nd->nd_mrep);
1597 if (!(nd->nd_flag & ND_NFSV2)) {
1598 if (eof || retlen == 0)
1600 } else if (retlen < len)
1605 if (nd->nd_mrep != NULL)
1606 mbuf_freem(nd->nd_mrep);
1611 * nfs write operation
1612 * When called_from_strategy != 0, it should return EIO for an error that
1613 * indicates recovery is in progress, so that the buffer will be left
1614 * dirty and be written back to the server later. If it loops around,
1615 * the recovery thread could get stuck waiting for the buffer and recovery
1616 * will then deadlock.
1619 nfsrpc_write(vnode_t vp, struct uio *uiop, int *iomode, int *must_commit,
1620 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp,
1621 void *stuff, int called_from_strategy)
1623 int error, expireret = 0, retrycnt, nostateid;
1624 u_int32_t clidrev = 0;
1625 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
1626 struct nfsnode *np = VTONFS(vp);
1627 struct ucred *newcred;
1628 struct nfsfh *nfhp = NULL;
1629 nfsv4stateid_t stateid;
1633 if (nmp->nm_clp != NULL)
1634 clidrev = nmp->nm_clp->nfsc_clientidrev;
1636 if (NFSHASNFSV4(nmp)) {
1637 newcred = NFSNEWCRED(cred);
1644 if (NFSHASNFSV4(nmp)) {
1645 (void)nfscl_getstateid(vp, nfhp->nfh_fh, nfhp->nfh_len,
1646 NFSV4OPEN_ACCESSWRITE, 0, newcred, p, &stateid,
1648 if (stateid.other[0] == 0 && stateid.other[1] == 0 &&
1649 stateid.other[2] == 0) {
1651 NFSCL_DEBUG(1, "stateid0 in write\n");
1656 * If there is no stateid for NFSv4, it means this is an
1657 * extraneous write after close. Basically a poorly
1658 * implemented buffer cache. Just don't do the write.
1663 error = nfsrpc_writerpc(vp, uiop, iomode, must_commit,
1664 newcred, &stateid, p, nap, attrflagp, stuff);
1665 if (error == NFSERR_STALESTATEID)
1666 nfscl_initiate_recovery(nmp->nm_clp);
1668 nfscl_lockderef(lckp);
1669 if (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
1670 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
1671 error == NFSERR_OLDSTATEID || error == NFSERR_BADSESSION) {
1672 (void) nfs_catnap(PZERO, error, "nfs_write");
1673 } else if ((error == NFSERR_EXPIRED ||
1674 error == NFSERR_BADSTATEID) && clidrev != 0) {
1675 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
1678 } while (error == NFSERR_GRACE || error == NFSERR_DELAY ||
1679 ((error == NFSERR_STALESTATEID || error == NFSERR_BADSESSION ||
1680 error == NFSERR_STALEDONTRECOVER) && called_from_strategy == 0) ||
1681 (error == NFSERR_OLDSTATEID && retrycnt < 20) ||
1682 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
1683 expireret == 0 && clidrev != 0 && retrycnt < 4));
1684 if (error != 0 && (retrycnt >= 4 ||
1685 ((error == NFSERR_STALESTATEID || error == NFSERR_BADSESSION ||
1686 error == NFSERR_STALEDONTRECOVER) && called_from_strategy != 0)))
1688 if (NFSHASNFSV4(nmp))
1689 NFSFREECRED(newcred);
1694 * The actual write RPC.
1697 nfsrpc_writerpc(vnode_t vp, struct uio *uiop, int *iomode,
1698 int *must_commit, struct ucred *cred, nfsv4stateid_t *stateidp,
1699 NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, void *stuff)
1702 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
1703 struct nfsnode *np = VTONFS(vp);
1704 int error = 0, len, tsiz, rlen, commit, committed = NFSWRITE_FILESYNC;
1705 int wccflag = 0, wsize;
1707 struct nfsrv_descript nfsd;
1708 struct nfsrv_descript *nd = &nfsd;
1709 nfsattrbit_t attrbits;
1712 KASSERT(uiop->uio_iovcnt == 1, ("nfs: writerpc iovcnt > 1"));
1714 tsiz = uio_uio_resid(uiop);
1715 tmp_off = uiop->uio_offset + tsiz;
1717 if (tmp_off > nmp->nm_maxfilesize || tmp_off < uiop->uio_offset) {
1721 wsize = nmp->nm_wsize;
1723 nd->nd_mrep = NULL; /* NFSv2 sometimes does a write with */
1724 nd->nd_repstat = 0; /* uio_resid == 0, so the while is not done */
1727 len = (tsiz > wsize) ? wsize : tsiz;
1728 NFSCL_REQSTART(nd, NFSPROC_WRITE, vp);
1729 if (nd->nd_flag & ND_NFSV4) {
1730 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID);
1731 NFSM_BUILD(tl, u_int32_t *, NFSX_HYPER+2*NFSX_UNSIGNED);
1732 txdr_hyper(uiop->uio_offset, tl);
1734 *tl++ = txdr_unsigned(*iomode);
1735 *tl = txdr_unsigned(len);
1736 } else if (nd->nd_flag & ND_NFSV3) {
1737 NFSM_BUILD(tl, u_int32_t *, NFSX_HYPER+3*NFSX_UNSIGNED);
1738 txdr_hyper(uiop->uio_offset, tl);
1740 *tl++ = txdr_unsigned(len);
1741 *tl++ = txdr_unsigned(*iomode);
1742 *tl = txdr_unsigned(len);
1746 NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1748 * Not sure why someone changed this, since the
1749 * RFC clearly states that "beginoffset" and
1750 * "totalcount" are ignored, but it wouldn't
1751 * surprise me if there's a busted server out there.
1753 /* Set both "begin" and "current" to non-garbage. */
1754 x = txdr_unsigned((u_int32_t)uiop->uio_offset);
1755 *tl++ = x; /* "begin offset" */
1756 *tl++ = x; /* "current offset" */
1757 x = txdr_unsigned(len);
1758 *tl++ = x; /* total to this offset */
1759 *tl = x; /* size of this write */
1762 nfsm_uiombuf(nd, uiop, len);
1764 * Although it is tempting to do a normal Getattr Op in the
1765 * NFSv4 compound, the result can be a nearly hung client
1766 * system if the Getattr asks for Owner and/or OwnerGroup.
1767 * It occurs when the client can't map either the Owner or
1768 * Owner_group name in the Getattr reply to a uid/gid. When
1769 * there is a cache miss, the kernel does an upcall to the
1770 * nfsuserd. Then, it can try and read the local /etc/passwd
1771 * or /etc/group file. It can then block in getnewbuf(),
1772 * waiting for dirty writes to be pushed to the NFS server.
1773 * The only reason this doesn't result in a complete
1774 * deadlock, is that the upcall times out and allows
1775 * the write to complete. However, progress is so slow
1776 * that it might just as well be deadlocked.
1777 * As such, we get the rest of the attributes, but not
1778 * Owner or Owner_group.
1779 * nb: nfscl_loadattrcache() needs to be told that these
1780 * partial attributes from a write rpc are being
1781 * passed in, via a argument flag.
1783 if (nd->nd_flag & ND_NFSV4) {
1784 NFSWRITEGETATTR_ATTRBIT(&attrbits);
1785 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1786 *tl = txdr_unsigned(NFSV4OP_GETATTR);
1787 (void) nfsrv_putattrbit(nd, &attrbits);
1789 error = nfscl_request(nd, vp, p, cred, stuff);
1792 if (nd->nd_repstat) {
1794 * In case the rpc gets retried, roll
1795 * the uio fileds changed by nfsm_uiombuf()
1798 uiop->uio_offset -= len;
1799 uio_uio_resid_add(uiop, len);
1800 uio_iov_base_add(uiop, -len);
1801 uio_iov_len_add(uiop, len);
1803 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) {
1804 error = nfscl_wcc_data(nd, vp, nap, attrflagp,
1809 if (!nd->nd_repstat) {
1810 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) {
1811 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED
1813 rlen = fxdr_unsigned(int, *tl++);
1817 } else if (rlen < len) {
1818 backup = len - rlen;
1819 uio_iov_base_add(uiop, -(backup));
1820 uio_iov_len_add(uiop, backup);
1821 uiop->uio_offset -= backup;
1822 uio_uio_resid_add(uiop, backup);
1825 commit = fxdr_unsigned(int, *tl++);
1828 * Return the lowest commitment level
1829 * obtained by any of the RPCs.
1831 if (committed == NFSWRITE_FILESYNC)
1833 else if (committed == NFSWRITE_DATASYNC &&
1834 commit == NFSWRITE_UNSTABLE)
1837 if (!NFSHASWRITEVERF(nmp)) {
1838 NFSBCOPY((caddr_t)tl,
1839 (caddr_t)&nmp->nm_verf[0],
1841 NFSSETWRITEVERF(nmp);
1842 } else if (NFSBCMP(tl, nmp->nm_verf,
1845 NFSBCOPY(tl, nmp->nm_verf, NFSX_VERF);
1849 if (nd->nd_flag & ND_NFSV4)
1850 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1851 if (nd->nd_flag & (ND_NFSV2 | ND_NFSV4)) {
1852 error = nfsm_loadattr(nd, nap);
1854 *attrflagp = NFS_LATTR_NOSHRINK;
1857 error = nd->nd_repstat;
1861 NFSWRITERPC_SETTIME(wccflag, np, nap, (nd->nd_flag & ND_NFSV4));
1862 mbuf_freem(nd->nd_mrep);
1867 if (nd->nd_mrep != NULL)
1868 mbuf_freem(nd->nd_mrep);
1869 *iomode = committed;
1870 if (nd->nd_repstat && !error)
1871 error = nd->nd_repstat;
1877 * For NFS v2 this is a kludge. Use a create rpc but with the IFMT bits of the
1878 * mode set to specify the file type and the size field for rdev.
1881 nfsrpc_mknod(vnode_t dvp, char *name, int namelen, struct vattr *vap,
1882 u_int32_t rdev, enum vtype vtyp, struct ucred *cred, NFSPROC_T *p,
1883 struct nfsvattr *dnap, struct nfsvattr *nnap, struct nfsfh **nfhpp,
1884 int *attrflagp, int *dattrflagp, void *dstuff)
1888 struct nfsrv_descript nfsd, *nd = &nfsd;
1889 nfsattrbit_t attrbits;
1894 if (namelen > NFS_MAXNAMLEN)
1895 return (ENAMETOOLONG);
1896 NFSCL_REQSTART(nd, NFSPROC_MKNOD, dvp);
1897 if (nd->nd_flag & ND_NFSV4) {
1898 if (vtyp == VBLK || vtyp == VCHR) {
1899 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
1900 *tl++ = vtonfsv34_type(vtyp);
1901 *tl++ = txdr_unsigned(NFSMAJOR(rdev));
1902 *tl = txdr_unsigned(NFSMINOR(rdev));
1904 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1905 *tl = vtonfsv34_type(vtyp);
1908 (void) nfsm_strtom(nd, name, namelen);
1909 if (nd->nd_flag & ND_NFSV3) {
1910 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1911 *tl = vtonfsv34_type(vtyp);
1913 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4))
1914 nfscl_fillsattr(nd, vap, dvp, 0, 0);
1915 if ((nd->nd_flag & ND_NFSV3) &&
1916 (vtyp == VCHR || vtyp == VBLK)) {
1917 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1918 *tl++ = txdr_unsigned(NFSMAJOR(rdev));
1919 *tl = txdr_unsigned(NFSMINOR(rdev));
1921 if (nd->nd_flag & ND_NFSV4) {
1922 NFSGETATTR_ATTRBIT(&attrbits);
1923 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1924 *tl++ = txdr_unsigned(NFSV4OP_GETFH);
1925 *tl = txdr_unsigned(NFSV4OP_GETATTR);
1926 (void) nfsrv_putattrbit(nd, &attrbits);
1928 if (nd->nd_flag & ND_NFSV2)
1929 nfscl_fillsattr(nd, vap, dvp, NFSSATTR_SIZERDEV, rdev);
1930 error = nfscl_request(nd, dvp, p, cred, dstuff);
1933 if (nd->nd_flag & ND_NFSV4)
1934 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
1935 if (!nd->nd_repstat) {
1936 if (nd->nd_flag & ND_NFSV4) {
1937 NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
1938 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
1942 error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp);
1946 if (nd->nd_flag & ND_NFSV3)
1947 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
1948 if (!error && nd->nd_repstat)
1949 error = nd->nd_repstat;
1951 mbuf_freem(nd->nd_mrep);
1956 * nfs file create call
1957 * Mostly just call the approriate routine. (I separated out v4, so that
1958 * error recovery wouldn't be as difficult.)
1961 nfsrpc_create(vnode_t dvp, char *name, int namelen, struct vattr *vap,
1962 nfsquad_t cverf, int fmode, struct ucred *cred, NFSPROC_T *p,
1963 struct nfsvattr *dnap, struct nfsvattr *nnap, struct nfsfh **nfhpp,
1964 int *attrflagp, int *dattrflagp, void *dstuff)
1966 int error = 0, newone, expireret = 0, retrycnt, unlocked;
1967 struct nfsclowner *owp;
1968 struct nfscldeleg *dp;
1969 struct nfsmount *nmp = VFSTONFS(vnode_mount(dvp));
1972 if (NFSHASNFSV4(nmp)) {
1976 error = nfscl_open(dvp, NULL, 0, (NFSV4OPEN_ACCESSWRITE |
1977 NFSV4OPEN_ACCESSREAD), 0, cred, p, &owp, NULL, &newone,
1981 if (nmp->nm_clp != NULL)
1982 clidrev = nmp->nm_clp->nfsc_clientidrev;
1985 if (!NFSHASPNFS(nmp) || nfscl_enablecallb == 0 ||
1986 nfs_numnfscbd == 0 || retrycnt > 0)
1987 error = nfsrpc_createv4(dvp, name, namelen, vap, cverf,
1988 fmode, owp, &dp, cred, p, dnap, nnap, nfhpp,
1989 attrflagp, dattrflagp, dstuff, &unlocked);
1991 error = nfsrpc_getcreatelayout(dvp, name, namelen, vap,
1992 cverf, fmode, owp, &dp, cred, p, dnap, nnap, nfhpp,
1993 attrflagp, dattrflagp, dstuff, &unlocked);
1995 * There is no need to invalidate cached attributes here,
1996 * since new post-delegation issue attributes are always
1997 * returned by nfsrpc_createv4() and these will update the
2001 (void) nfscl_deleg(nmp->nm_mountp, owp->nfsow_clp,
2002 (*nfhpp)->nfh_fh, (*nfhpp)->nfh_len, cred, p, &dp);
2003 nfscl_ownerrelease(nmp, owp, error, newone, unlocked);
2004 if (error == NFSERR_GRACE || error == NFSERR_STALECLIENTID ||
2005 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
2006 error == NFSERR_BADSESSION) {
2007 (void) nfs_catnap(PZERO, error, "nfs_open");
2008 } else if ((error == NFSERR_EXPIRED ||
2009 error == NFSERR_BADSTATEID) && clidrev != 0) {
2010 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
2013 } while (error == NFSERR_GRACE || error == NFSERR_STALECLIENTID ||
2014 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
2015 error == NFSERR_BADSESSION ||
2016 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
2017 expireret == 0 && clidrev != 0 && retrycnt < 4));
2018 if (error && retrycnt >= 4)
2021 error = nfsrpc_createv23(dvp, name, namelen, vap, cverf,
2022 fmode, cred, p, dnap, nnap, nfhpp, attrflagp, dattrflagp,
2029 * The create rpc for v2 and 3.
2032 nfsrpc_createv23(vnode_t dvp, char *name, int namelen, struct vattr *vap,
2033 nfsquad_t cverf, int fmode, struct ucred *cred, NFSPROC_T *p,
2034 struct nfsvattr *dnap, struct nfsvattr *nnap, struct nfsfh **nfhpp,
2035 int *attrflagp, int *dattrflagp, void *dstuff)
2039 struct nfsrv_descript nfsd, *nd = &nfsd;
2044 if (namelen > NFS_MAXNAMLEN)
2045 return (ENAMETOOLONG);
2046 NFSCL_REQSTART(nd, NFSPROC_CREATE, dvp);
2047 (void) nfsm_strtom(nd, name, namelen);
2048 if (nd->nd_flag & ND_NFSV3) {
2049 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2050 if (fmode & O_EXCL) {
2051 *tl = txdr_unsigned(NFSCREATE_EXCLUSIVE);
2052 NFSM_BUILD(tl, u_int32_t *, NFSX_VERF);
2053 *tl++ = cverf.lval[0];
2054 *tl = cverf.lval[1];
2056 *tl = txdr_unsigned(NFSCREATE_UNCHECKED);
2057 nfscl_fillsattr(nd, vap, dvp, 0, 0);
2060 nfscl_fillsattr(nd, vap, dvp, NFSSATTR_SIZE0, 0);
2062 error = nfscl_request(nd, dvp, p, cred, dstuff);
2065 if (nd->nd_repstat == 0) {
2066 error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp);
2070 if (nd->nd_flag & ND_NFSV3)
2071 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
2072 if (nd->nd_repstat != 0 && error == 0)
2073 error = nd->nd_repstat;
2075 mbuf_freem(nd->nd_mrep);
2080 nfsrpc_createv4(vnode_t dvp, char *name, int namelen, struct vattr *vap,
2081 nfsquad_t cverf, int fmode, struct nfsclowner *owp, struct nfscldeleg **dpp,
2082 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap,
2083 struct nfsvattr *nnap, struct nfsfh **nfhpp, int *attrflagp,
2084 int *dattrflagp, void *dstuff, int *unlockedp)
2087 int error = 0, deleg, newone, ret, acesize, limitby;
2088 struct nfsrv_descript nfsd, *nd = &nfsd;
2089 struct nfsclopen *op;
2090 struct nfscldeleg *dp = NULL;
2093 nfsattrbit_t attrbits;
2094 nfsv4stateid_t stateid;
2096 struct nfsmount *nmp;
2097 struct nfsclsession *tsep;
2099 nmp = VFSTONFS(dvp->v_mount);
2106 if (namelen > NFS_MAXNAMLEN)
2107 return (ENAMETOOLONG);
2108 NFSCL_REQSTART(nd, NFSPROC_CREATE, dvp);
2110 * For V4, this is actually an Open op.
2112 NFSM_BUILD(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
2113 *tl++ = txdr_unsigned(owp->nfsow_seqid);
2114 *tl++ = txdr_unsigned(NFSV4OPEN_ACCESSWRITE |
2115 NFSV4OPEN_ACCESSREAD);
2116 *tl++ = txdr_unsigned(NFSV4OPEN_DENYNONE);
2117 tsep = nfsmnt_mdssession(nmp);
2118 *tl++ = tsep->nfsess_clientid.lval[0];
2119 *tl = tsep->nfsess_clientid.lval[1];
2120 (void) nfsm_strtom(nd, owp->nfsow_owner, NFSV4CL_LOCKNAMELEN);
2121 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2122 *tl++ = txdr_unsigned(NFSV4OPEN_CREATE);
2123 if (fmode & O_EXCL) {
2124 if (NFSHASNFSV4N(nmp)) {
2125 if (NFSHASSESSPERSIST(nmp)) {
2126 /* Use GUARDED for persistent sessions. */
2127 *tl = txdr_unsigned(NFSCREATE_GUARDED);
2128 nfscl_fillsattr(nd, vap, dvp, 0, 0);
2130 /* Otherwise, use EXCLUSIVE4_1. */
2131 *tl = txdr_unsigned(NFSCREATE_EXCLUSIVE41);
2132 NFSM_BUILD(tl, u_int32_t *, NFSX_VERF);
2133 *tl++ = cverf.lval[0];
2134 *tl = cverf.lval[1];
2135 nfscl_fillsattr(nd, vap, dvp, 0, 0);
2139 *tl = txdr_unsigned(NFSCREATE_EXCLUSIVE);
2140 NFSM_BUILD(tl, u_int32_t *, NFSX_VERF);
2141 *tl++ = cverf.lval[0];
2142 *tl = cverf.lval[1];
2145 *tl = txdr_unsigned(NFSCREATE_UNCHECKED);
2146 nfscl_fillsattr(nd, vap, dvp, 0, 0);
2148 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2149 *tl = txdr_unsigned(NFSV4OPEN_CLAIMNULL);
2150 (void) nfsm_strtom(nd, name, namelen);
2151 /* Get the new file's handle and attributes. */
2152 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2153 *tl++ = txdr_unsigned(NFSV4OP_GETFH);
2154 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2155 NFSGETATTR_ATTRBIT(&attrbits);
2156 (void) nfsrv_putattrbit(nd, &attrbits);
2157 /* Get the directory's post-op attributes. */
2158 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2159 *tl = txdr_unsigned(NFSV4OP_PUTFH);
2160 (void) nfsm_fhtom(nd, np->n_fhp->nfh_fh, np->n_fhp->nfh_len, 0);
2161 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2162 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2163 (void) nfsrv_putattrbit(nd, &attrbits);
2164 error = nfscl_request(nd, dvp, p, cred, dstuff);
2167 NFSCL_INCRSEQID(owp->nfsow_seqid, nd);
2168 if (nd->nd_repstat == 0) {
2169 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
2171 stateid.seqid = *tl++;
2172 stateid.other[0] = *tl++;
2173 stateid.other[1] = *tl++;
2174 stateid.other[2] = *tl;
2175 rflags = fxdr_unsigned(u_int32_t, *(tl + 6));
2176 (void) nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
2177 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2178 deleg = fxdr_unsigned(int, *tl);
2179 if (deleg == NFSV4OPEN_DELEGATEREAD ||
2180 deleg == NFSV4OPEN_DELEGATEWRITE) {
2181 if (!(owp->nfsow_clp->nfsc_flags &
2182 NFSCLFLAGS_FIRSTDELEG))
2183 owp->nfsow_clp->nfsc_flags |=
2184 (NFSCLFLAGS_FIRSTDELEG | NFSCLFLAGS_GOTDELEG);
2185 MALLOC(dp, struct nfscldeleg *,
2186 sizeof (struct nfscldeleg) + NFSX_V4FHMAX,
2187 M_NFSCLDELEG, M_WAITOK);
2188 LIST_INIT(&dp->nfsdl_owner);
2189 LIST_INIT(&dp->nfsdl_lock);
2190 dp->nfsdl_clp = owp->nfsow_clp;
2191 newnfs_copyincred(cred, &dp->nfsdl_cred);
2192 nfscl_lockinit(&dp->nfsdl_rwlock);
2193 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
2195 dp->nfsdl_stateid.seqid = *tl++;
2196 dp->nfsdl_stateid.other[0] = *tl++;
2197 dp->nfsdl_stateid.other[1] = *tl++;
2198 dp->nfsdl_stateid.other[2] = *tl++;
2199 ret = fxdr_unsigned(int, *tl);
2200 if (deleg == NFSV4OPEN_DELEGATEWRITE) {
2201 dp->nfsdl_flags = NFSCLDL_WRITE;
2203 * Indicates how much the file can grow.
2205 NFSM_DISSECT(tl, u_int32_t *,
2207 limitby = fxdr_unsigned(int, *tl++);
2209 case NFSV4OPEN_LIMITSIZE:
2210 dp->nfsdl_sizelimit = fxdr_hyper(tl);
2212 case NFSV4OPEN_LIMITBLOCKS:
2213 dp->nfsdl_sizelimit =
2214 fxdr_unsigned(u_int64_t, *tl++);
2215 dp->nfsdl_sizelimit *=
2216 fxdr_unsigned(u_int64_t, *tl);
2219 error = NFSERR_BADXDR;
2223 dp->nfsdl_flags = NFSCLDL_READ;
2226 dp->nfsdl_flags |= NFSCLDL_RECALL;
2227 error = nfsrv_dissectace(nd, &dp->nfsdl_ace, &ret,
2231 } else if (deleg != NFSV4OPEN_DELEGATENONE) {
2232 error = NFSERR_BADXDR;
2235 error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp);
2238 /* Get rid of the PutFH and Getattr status values. */
2239 NFSM_DISSECT(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
2240 /* Load the directory attributes. */
2241 error = nfsm_loadattr(nd, dnap);
2245 if (dp != NULL && *attrflagp) {
2246 dp->nfsdl_change = nnap->na_filerev;
2247 dp->nfsdl_modtime = nnap->na_mtime;
2248 dp->nfsdl_flags |= NFSCLDL_MODTIMESET;
2251 * We can now complete the Open state.
2255 dp->nfsdl_fhlen = nfhp->nfh_len;
2256 NFSBCOPY(nfhp->nfh_fh, dp->nfsdl_fh, nfhp->nfh_len);
2259 * Get an Open structure that will be
2260 * attached to the OpenOwner, acquired already.
2262 error = nfscl_open(dvp, nfhp->nfh_fh, nfhp->nfh_len,
2263 (NFSV4OPEN_ACCESSWRITE | NFSV4OPEN_ACCESSREAD), 0,
2264 cred, p, NULL, &op, &newone, NULL, 0);
2267 op->nfso_stateid = stateid;
2268 newnfs_copyincred(cred, &op->nfso_cred);
2269 if ((rflags & NFSV4OPEN_RESULTCONFIRM)) {
2271 ret = nfsrpc_openconfirm(dvp, nfhp->nfh_fh,
2272 nfhp->nfh_len, op, cred, p);
2273 if (ret == NFSERR_DELAY)
2274 (void) nfs_catnap(PZERO, ret, "nfs_create");
2275 } while (ret == NFSERR_DELAY);
2280 * If the server is handing out delegations, but we didn't
2281 * get one because an OpenConfirm was required, try the
2282 * Open again, to get a delegation. This is a harmless no-op,
2283 * from a server's point of view.
2285 if ((rflags & NFSV4OPEN_RESULTCONFIRM) &&
2286 (owp->nfsow_clp->nfsc_flags & NFSCLFLAGS_GOTDELEG) &&
2287 !error && dp == NULL) {
2289 ret = nfsrpc_openrpc(VFSTONFS(vnode_mount(dvp)), dvp,
2290 np->n_fhp->nfh_fh, np->n_fhp->nfh_len,
2291 nfhp->nfh_fh, nfhp->nfh_len,
2292 (NFSV4OPEN_ACCESSWRITE | NFSV4OPEN_ACCESSREAD), op,
2293 name, namelen, &dp, 0, 0x0, cred, p, 0, 1);
2294 if (ret == NFSERR_DELAY)
2295 (void) nfs_catnap(PZERO, ret, "nfs_crt2");
2296 } while (ret == NFSERR_DELAY);
2299 FREE((caddr_t)dp, M_NFSCLDELEG);
2302 if (ret == NFSERR_STALECLIENTID ||
2303 ret == NFSERR_STALEDONTRECOVER ||
2304 ret == NFSERR_BADSESSION)
2308 nfscl_openrelease(nmp, op, error, newone);
2311 if (nd->nd_repstat != 0 && error == 0)
2312 error = nd->nd_repstat;
2313 if (error == NFSERR_STALECLIENTID)
2314 nfscl_initiate_recovery(owp->nfsow_clp);
2318 else if (dp != NULL)
2319 FREE((caddr_t)dp, M_NFSCLDELEG);
2320 mbuf_freem(nd->nd_mrep);
2328 nfsrpc_remove(vnode_t dvp, char *name, int namelen, vnode_t vp,
2329 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap, int *dattrflagp,
2333 struct nfsrv_descript nfsd, *nd = &nfsd;
2335 struct nfsmount *nmp;
2336 nfsv4stateid_t dstateid;
2337 int error, ret = 0, i;
2340 if (namelen > NFS_MAXNAMLEN)
2341 return (ENAMETOOLONG);
2342 nmp = VFSTONFS(vnode_mount(dvp));
2344 if (NFSHASNFSV4(nmp) && ret == 0) {
2345 ret = nfscl_removedeleg(vp, p, &dstateid);
2347 NFSCL_REQSTART(nd, NFSPROC_RETDELEGREMOVE, vp);
2348 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID +
2350 if (NFSHASNFSV4N(nmp))
2353 *tl++ = dstateid.seqid;
2354 *tl++ = dstateid.other[0];
2355 *tl++ = dstateid.other[1];
2356 *tl++ = dstateid.other[2];
2357 *tl = txdr_unsigned(NFSV4OP_PUTFH);
2359 (void) nfsm_fhtom(nd, np->n_fhp->nfh_fh,
2360 np->n_fhp->nfh_len, 0);
2361 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2362 *tl = txdr_unsigned(NFSV4OP_REMOVE);
2368 NFSCL_REQSTART(nd, NFSPROC_REMOVE, dvp);
2369 (void) nfsm_strtom(nd, name, namelen);
2370 error = nfscl_request(nd, dvp, p, cred, dstuff);
2373 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) {
2374 /* For NFSv4, parse out any Delereturn replies. */
2375 if (ret > 0 && nd->nd_repstat != 0 &&
2376 (nd->nd_flag & ND_NOMOREDATA)) {
2378 * If the Delegreturn failed, try again without
2379 * it. The server will Recall, as required.
2381 mbuf_freem(nd->nd_mrep);
2384 for (i = 0; i < (ret * 2); i++) {
2385 if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) ==
2387 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2389 nd->nd_flag |= ND_NOMOREDATA;
2392 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
2394 if (nd->nd_repstat && !error)
2395 error = nd->nd_repstat;
2397 mbuf_freem(nd->nd_mrep);
2402 * Do an nfs rename rpc.
2405 nfsrpc_rename(vnode_t fdvp, vnode_t fvp, char *fnameptr, int fnamelen,
2406 vnode_t tdvp, vnode_t tvp, char *tnameptr, int tnamelen, struct ucred *cred,
2407 NFSPROC_T *p, struct nfsvattr *fnap, struct nfsvattr *tnap,
2408 int *fattrflagp, int *tattrflagp, void *fstuff, void *tstuff)
2411 struct nfsrv_descript nfsd, *nd = &nfsd;
2412 struct nfsmount *nmp;
2414 nfsattrbit_t attrbits;
2415 nfsv4stateid_t fdstateid, tdstateid;
2416 int error = 0, ret = 0, gottd = 0, gotfd = 0, i;
2420 nmp = VFSTONFS(vnode_mount(fdvp));
2421 if (fnamelen > NFS_MAXNAMLEN || tnamelen > NFS_MAXNAMLEN)
2422 return (ENAMETOOLONG);
2424 if (NFSHASNFSV4(nmp) && ret == 0) {
2425 ret = nfscl_renamedeleg(fvp, &fdstateid, &gotfd, tvp,
2426 &tdstateid, &gottd, p);
2427 if (gotfd && gottd) {
2428 NFSCL_REQSTART(nd, NFSPROC_RETDELEGRENAME2, fvp);
2430 NFSCL_REQSTART(nd, NFSPROC_RETDELEGRENAME1, fvp);
2432 NFSCL_REQSTART(nd, NFSPROC_RETDELEGRENAME1, tvp);
2435 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID);
2436 if (NFSHASNFSV4N(nmp))
2439 *tl++ = fdstateid.seqid;
2440 *tl++ = fdstateid.other[0];
2441 *tl++ = fdstateid.other[1];
2442 *tl = fdstateid.other[2];
2444 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2445 *tl = txdr_unsigned(NFSV4OP_PUTFH);
2447 (void) nfsm_fhtom(nd, np->n_fhp->nfh_fh,
2448 np->n_fhp->nfh_len, 0);
2449 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2450 *tl = txdr_unsigned(NFSV4OP_DELEGRETURN);
2454 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID);
2455 if (NFSHASNFSV4N(nmp))
2458 *tl++ = tdstateid.seqid;
2459 *tl++ = tdstateid.other[0];
2460 *tl++ = tdstateid.other[1];
2461 *tl = tdstateid.other[2];
2464 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2465 *tl = txdr_unsigned(NFSV4OP_PUTFH);
2467 (void) nfsm_fhtom(nd, np->n_fhp->nfh_fh,
2468 np->n_fhp->nfh_len, 0);
2469 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2470 *tl = txdr_unsigned(NFSV4OP_SAVEFH);
2476 NFSCL_REQSTART(nd, NFSPROC_RENAME, fdvp);
2477 if (nd->nd_flag & ND_NFSV4) {
2478 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2479 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2480 NFSWCCATTR_ATTRBIT(&attrbits);
2481 (void) nfsrv_putattrbit(nd, &attrbits);
2482 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2483 *tl = txdr_unsigned(NFSV4OP_PUTFH);
2484 (void) nfsm_fhtom(nd, VTONFS(tdvp)->n_fhp->nfh_fh,
2485 VTONFS(tdvp)->n_fhp->nfh_len, 0);
2486 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2487 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2488 (void) nfsrv_putattrbit(nd, &attrbits);
2489 nd->nd_flag |= ND_V4WCCATTR;
2490 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2491 *tl = txdr_unsigned(NFSV4OP_RENAME);
2493 (void) nfsm_strtom(nd, fnameptr, fnamelen);
2494 if (!(nd->nd_flag & ND_NFSV4))
2495 (void) nfsm_fhtom(nd, VTONFS(tdvp)->n_fhp->nfh_fh,
2496 VTONFS(tdvp)->n_fhp->nfh_len, 0);
2497 (void) nfsm_strtom(nd, tnameptr, tnamelen);
2498 error = nfscl_request(nd, fdvp, p, cred, fstuff);
2501 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) {
2502 /* For NFSv4, parse out any Delereturn replies. */
2503 if (ret > 0 && nd->nd_repstat != 0 &&
2504 (nd->nd_flag & ND_NOMOREDATA)) {
2506 * If the Delegreturn failed, try again without
2507 * it. The server will Recall, as required.
2509 mbuf_freem(nd->nd_mrep);
2512 for (i = 0; i < (ret * 2); i++) {
2513 if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) ==
2515 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2517 if (i == 0 && ret > 1) {
2519 * If the Delegreturn failed, try again
2520 * without it. The server will Recall, as
2522 * If ret > 1, the first iteration of this
2523 * loop is the second DelegReturn result.
2525 mbuf_freem(nd->nd_mrep);
2528 nd->nd_flag |= ND_NOMOREDATA;
2533 /* Now, the first wcc attribute reply. */
2534 if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) == ND_NFSV4) {
2535 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2537 nd->nd_flag |= ND_NOMOREDATA;
2539 error = nfscl_wcc_data(nd, fdvp, fnap, fattrflagp, NULL,
2541 /* and the second wcc attribute reply. */
2542 if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) == ND_NFSV4 &&
2544 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2546 nd->nd_flag |= ND_NOMOREDATA;
2549 error = nfscl_wcc_data(nd, tdvp, tnap, tattrflagp,
2552 if (nd->nd_repstat && !error)
2553 error = nd->nd_repstat;
2555 mbuf_freem(nd->nd_mrep);
2560 * nfs hard link create rpc
2563 nfsrpc_link(vnode_t dvp, vnode_t vp, char *name, int namelen,
2564 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap,
2565 struct nfsvattr *nap, int *attrflagp, int *dattrflagp, void *dstuff)
2568 struct nfsrv_descript nfsd, *nd = &nfsd;
2569 nfsattrbit_t attrbits;
2574 if (namelen > NFS_MAXNAMLEN)
2575 return (ENAMETOOLONG);
2576 NFSCL_REQSTART(nd, NFSPROC_LINK, vp);
2577 if (nd->nd_flag & ND_NFSV4) {
2578 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2579 *tl = txdr_unsigned(NFSV4OP_PUTFH);
2581 (void) nfsm_fhtom(nd, VTONFS(dvp)->n_fhp->nfh_fh,
2582 VTONFS(dvp)->n_fhp->nfh_len, 0);
2583 if (nd->nd_flag & ND_NFSV4) {
2584 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2585 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2586 NFSWCCATTR_ATTRBIT(&attrbits);
2587 (void) nfsrv_putattrbit(nd, &attrbits);
2588 nd->nd_flag |= ND_V4WCCATTR;
2589 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2590 *tl = txdr_unsigned(NFSV4OP_LINK);
2592 (void) nfsm_strtom(nd, name, namelen);
2593 error = nfscl_request(nd, vp, p, cred, dstuff);
2596 if (nd->nd_flag & ND_NFSV3) {
2597 error = nfscl_postop_attr(nd, nap, attrflagp, dstuff);
2599 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp,
2601 } else if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) == ND_NFSV4) {
2603 * First, parse out the PutFH and Getattr result.
2605 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2607 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2609 nd->nd_flag |= ND_NOMOREDATA;
2611 * Get the pre-op attributes.
2613 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
2615 if (nd->nd_repstat && !error)
2616 error = nd->nd_repstat;
2618 mbuf_freem(nd->nd_mrep);
2623 * nfs symbolic link create rpc
2626 nfsrpc_symlink(vnode_t dvp, char *name, int namelen, char *target,
2627 struct vattr *vap, struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap,
2628 struct nfsvattr *nnap, struct nfsfh **nfhpp, int *attrflagp,
2629 int *dattrflagp, void *dstuff)
2632 struct nfsrv_descript nfsd, *nd = &nfsd;
2633 struct nfsmount *nmp;
2634 int slen, error = 0;
2639 nmp = VFSTONFS(vnode_mount(dvp));
2640 slen = strlen(target);
2641 if (slen > NFS_MAXPATHLEN || namelen > NFS_MAXNAMLEN)
2642 return (ENAMETOOLONG);
2643 NFSCL_REQSTART(nd, NFSPROC_SYMLINK, dvp);
2644 if (nd->nd_flag & ND_NFSV4) {
2645 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2646 *tl = txdr_unsigned(NFLNK);
2647 (void) nfsm_strtom(nd, target, slen);
2649 (void) nfsm_strtom(nd, name, namelen);
2650 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4))
2651 nfscl_fillsattr(nd, vap, dvp, 0, 0);
2652 if (!(nd->nd_flag & ND_NFSV4))
2653 (void) nfsm_strtom(nd, target, slen);
2654 if (nd->nd_flag & ND_NFSV2)
2655 nfscl_fillsattr(nd, vap, dvp, NFSSATTR_SIZENEG1, 0);
2656 error = nfscl_request(nd, dvp, p, cred, dstuff);
2659 if (nd->nd_flag & ND_NFSV4)
2660 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
2661 if ((nd->nd_flag & ND_NFSV3) && !error) {
2662 if (!nd->nd_repstat)
2663 error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp);
2665 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp,
2668 if (nd->nd_repstat && !error)
2669 error = nd->nd_repstat;
2670 mbuf_freem(nd->nd_mrep);
2672 * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry.
2673 * Only do this if vfs.nfs.ignore_eexist is set.
2674 * Never do this for NFSv4.1 or later minor versions, since sessions
2675 * should guarantee "exactly once" RPC semantics.
2677 if (error == EEXIST && nfsignore_eexist != 0 && (!NFSHASNFSV4(nmp) ||
2678 nmp->nm_minorvers == 0))
2687 nfsrpc_mkdir(vnode_t dvp, char *name, int namelen, struct vattr *vap,
2688 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap,
2689 struct nfsvattr *nnap, struct nfsfh **nfhpp, int *attrflagp,
2690 int *dattrflagp, void *dstuff)
2693 struct nfsrv_descript nfsd, *nd = &nfsd;
2694 nfsattrbit_t attrbits;
2697 struct nfsmount *nmp;
2702 nmp = VFSTONFS(vnode_mount(dvp));
2703 fhp = VTONFS(dvp)->n_fhp;
2704 if (namelen > NFS_MAXNAMLEN)
2705 return (ENAMETOOLONG);
2706 NFSCL_REQSTART(nd, NFSPROC_MKDIR, dvp);
2707 if (nd->nd_flag & ND_NFSV4) {
2708 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2709 *tl = txdr_unsigned(NFDIR);
2711 (void) nfsm_strtom(nd, name, namelen);
2712 nfscl_fillsattr(nd, vap, dvp, NFSSATTR_SIZENEG1, 0);
2713 if (nd->nd_flag & ND_NFSV4) {
2714 NFSGETATTR_ATTRBIT(&attrbits);
2715 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2716 *tl++ = txdr_unsigned(NFSV4OP_GETFH);
2717 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2718 (void) nfsrv_putattrbit(nd, &attrbits);
2719 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2720 *tl = txdr_unsigned(NFSV4OP_PUTFH);
2721 (void) nfsm_fhtom(nd, fhp->nfh_fh, fhp->nfh_len, 0);
2722 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2723 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2724 (void) nfsrv_putattrbit(nd, &attrbits);
2726 error = nfscl_request(nd, dvp, p, cred, dstuff);
2729 if (nd->nd_flag & ND_NFSV4)
2730 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
2731 if (!nd->nd_repstat && !error) {
2732 if (nd->nd_flag & ND_NFSV4) {
2733 NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
2734 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
2737 error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp);
2738 if (error == 0 && (nd->nd_flag & ND_NFSV4) != 0) {
2739 /* Get rid of the PutFH and Getattr status values. */
2740 NFSM_DISSECT(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
2741 /* Load the directory attributes. */
2742 error = nfsm_loadattr(nd, dnap);
2747 if ((nd->nd_flag & ND_NFSV3) && !error)
2748 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
2749 if (nd->nd_repstat && !error)
2750 error = nd->nd_repstat;
2752 mbuf_freem(nd->nd_mrep);
2754 * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry.
2755 * Only do this if vfs.nfs.ignore_eexist is set.
2756 * Never do this for NFSv4.1 or later minor versions, since sessions
2757 * should guarantee "exactly once" RPC semantics.
2759 if (error == EEXIST && nfsignore_eexist != 0 && (!NFSHASNFSV4(nmp) ||
2760 nmp->nm_minorvers == 0))
2766 * nfs remove directory call
2769 nfsrpc_rmdir(vnode_t dvp, char *name, int namelen, struct ucred *cred,
2770 NFSPROC_T *p, struct nfsvattr *dnap, int *dattrflagp, void *dstuff)
2772 struct nfsrv_descript nfsd, *nd = &nfsd;
2776 if (namelen > NFS_MAXNAMLEN)
2777 return (ENAMETOOLONG);
2778 NFSCL_REQSTART(nd, NFSPROC_RMDIR, dvp);
2779 (void) nfsm_strtom(nd, name, namelen);
2780 error = nfscl_request(nd, dvp, p, cred, dstuff);
2783 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4))
2784 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
2785 if (nd->nd_repstat && !error)
2786 error = nd->nd_repstat;
2787 mbuf_freem(nd->nd_mrep);
2789 * Kludge: Map ENOENT => 0 assuming that you have a reply to a retry.
2791 if (error == ENOENT)
2798 * Always returns with either uio_resid unchanged, if you are at the
2799 * end of the directory, or uio_resid == 0, with all DIRBLKSIZ chunks
2801 * I felt this would allow caching of directory blocks more easily
2802 * than returning a pertially filled block.
2803 * Directory offset cookies:
2804 * Oh my, what to do with them...
2805 * I can think of three ways to deal with them:
2806 * 1 - have the layer above these RPCs maintain a map between logical
2807 * directory byte offsets and the NFS directory offset cookies
2808 * 2 - pass the opaque directory offset cookies up into userland
2809 * and let the libc functions deal with them, via the system call
2810 * 3 - return them to userland in the "struct dirent", so future versions
2811 * of libc can use them and do whatever is necessary to make things work
2812 * above these rpc calls, in the meantime
2813 * For now, I do #3 by "hiding" the directory offset cookies after the
2814 * d_name field in struct dirent. This is space inside d_reclen that
2815 * will be ignored by anything that doesn't know about them.
2816 * The directory offset cookies are filled in as the last 8 bytes of
2817 * each directory entry, after d_name. Someday, the userland libc
2818 * functions may be able to use these. In the meantime, it satisfies
2819 * OpenBSD's requirements for cookies being returned.
2820 * If expects the directory offset cookie for the read to be in uio_offset
2821 * and returns the one for the next entry after this directory block in
2825 nfsrpc_readdir(vnode_t vp, struct uio *uiop, nfsuint64 *cookiep,
2826 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp,
2827 int *eofp, void *stuff)
2830 struct dirent *dp = NULL;
2832 nfsquad_t cookie, ncookie;
2833 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
2834 struct nfsnode *dnp = VTONFS(vp);
2835 struct nfsvattr nfsva;
2836 struct nfsrv_descript nfsd, *nd = &nfsd;
2837 int error = 0, tlen, more_dirs = 1, blksiz = 0, bigenough = 1;
2838 int reqsize, tryformoredirs = 1, readsize, eof = 0, gotmnton = 0;
2839 u_int64_t dotfileid, dotdotfileid = 0, fakefileno = UINT64_MAX;
2841 nfsattrbit_t attrbits, dattrbits;
2842 u_int32_t rderr, *tl2 = NULL;
2845 KASSERT(uiop->uio_iovcnt == 1 &&
2846 (uio_uio_resid(uiop) & (DIRBLKSIZ - 1)) == 0,
2847 ("nfs readdirrpc bad uio"));
2850 * There is no point in reading a lot more than uio_resid, however
2851 * adding one additional DIRBLKSIZ makes sense. Since uio_resid
2852 * and nm_readdirsize are both exact multiples of DIRBLKSIZ, this
2853 * will never make readsize > nm_readdirsize.
2855 readsize = nmp->nm_readdirsize;
2856 if (readsize > uio_uio_resid(uiop))
2857 readsize = uio_uio_resid(uiop) + DIRBLKSIZ;
2862 tresid = uio_uio_resid(uiop);
2863 cookie.lval[0] = cookiep->nfsuquad[0];
2864 cookie.lval[1] = cookiep->nfsuquad[1];
2868 * For NFSv4, first create the "." and ".." entries.
2870 if (NFSHASNFSV4(nmp)) {
2871 reqsize = 6 * NFSX_UNSIGNED;
2872 NFSGETATTR_ATTRBIT(&dattrbits);
2873 NFSZERO_ATTRBIT(&attrbits);
2874 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_FILEID);
2875 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TYPE);
2876 if (NFSISSET_ATTRBIT(&dnp->n_vattr.na_suppattr,
2877 NFSATTRBIT_MOUNTEDONFILEID)) {
2878 NFSSETBIT_ATTRBIT(&attrbits,
2879 NFSATTRBIT_MOUNTEDONFILEID);
2883 * Must fake it. Use the fileno, except when the
2884 * fsid is != to that of the directory. For that
2885 * case, generate a fake fileno that is not the same.
2887 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_FSID);
2892 * Joy, oh joy. For V4 we get to hand craft '.' and '..'.
2894 if (uiop->uio_offset == 0) {
2895 NFSCL_REQSTART(nd, NFSPROC_LOOKUPP, vp);
2896 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2897 *tl++ = txdr_unsigned(NFSV4OP_GETFH);
2898 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2899 (void) nfsrv_putattrbit(nd, &attrbits);
2900 error = nfscl_request(nd, vp, p, cred, stuff);
2903 dotfileid = 0; /* Fake out the compiler. */
2904 if ((nd->nd_flag & ND_NOMOREDATA) == 0) {
2905 error = nfsm_loadattr(nd, &nfsva);
2908 dotfileid = nfsva.na_fileid;
2910 if (nd->nd_repstat == 0) {
2911 NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
2912 len = fxdr_unsigned(int, *(tl + 4));
2913 if (len > 0 && len <= NFSX_V4FHMAX)
2914 error = nfsm_advance(nd, NFSM_RNDUP(len), -1);
2918 NFSM_DISSECT(tl, u_int32_t *, 2*NFSX_UNSIGNED);
2919 nfsva.na_mntonfileno = UINT64_MAX;
2920 error = nfsv4_loadattr(nd, NULL, &nfsva, NULL,
2921 NULL, 0, NULL, NULL, NULL, NULL, NULL, 0,
2922 NULL, NULL, NULL, p, cred);
2924 dotdotfileid = dotfileid;
2925 } else if (gotmnton) {
2926 if (nfsva.na_mntonfileno != UINT64_MAX)
2927 dotdotfileid = nfsva.na_mntonfileno;
2929 dotdotfileid = nfsva.na_fileid;
2930 } else if (nfsva.na_filesid[0] ==
2931 dnp->n_vattr.na_filesid[0] &&
2932 nfsva.na_filesid[1] ==
2933 dnp->n_vattr.na_filesid[1]) {
2934 dotdotfileid = nfsva.na_fileid;
2938 } while (fakefileno ==
2940 dotdotfileid = fakefileno;
2943 } else if (nd->nd_repstat == NFSERR_NOENT) {
2945 * Lookupp returns NFSERR_NOENT when we are
2946 * at the root, so just use the current dir.
2949 dotdotfileid = dotfileid;
2951 error = nd->nd_repstat;
2953 mbuf_freem(nd->nd_mrep);
2957 dp = (struct dirent *)uio_iov_base(uiop);
2959 dp->d_type = DT_DIR;
2960 dp->d_fileno = dotfileid;
2962 *((uint64_t *)dp->d_name) = 0; /* Zero pad it. */
2963 dp->d_name[0] = '.';
2964 dp->d_reclen = _GENERIC_DIRSIZ(dp) + NFSX_HYPER;
2966 * Just make these offset cookie 0.
2968 tl = (u_int32_t *)&dp->d_name[8];
2971 blksiz += dp->d_reclen;
2972 uio_uio_resid_add(uiop, -(dp->d_reclen));
2973 uiop->uio_offset += dp->d_reclen;
2974 uio_iov_base_add(uiop, dp->d_reclen);
2975 uio_iov_len_add(uiop, -(dp->d_reclen));
2976 dp = (struct dirent *)uio_iov_base(uiop);
2978 dp->d_type = DT_DIR;
2979 dp->d_fileno = dotdotfileid;
2981 *((uint64_t *)dp->d_name) = 0;
2982 dp->d_name[0] = '.';
2983 dp->d_name[1] = '.';
2984 dp->d_reclen = _GENERIC_DIRSIZ(dp) + NFSX_HYPER;
2986 * Just make these offset cookie 0.
2988 tl = (u_int32_t *)&dp->d_name[8];
2991 blksiz += dp->d_reclen;
2992 uio_uio_resid_add(uiop, -(dp->d_reclen));
2993 uiop->uio_offset += dp->d_reclen;
2994 uio_iov_base_add(uiop, dp->d_reclen);
2995 uio_iov_len_add(uiop, -(dp->d_reclen));
2997 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_RDATTRERROR);
2999 reqsize = 5 * NFSX_UNSIGNED;
3004 * Loop around doing readdir rpc's of size readsize.
3005 * The stopping criteria is EOF or buffer full.
3007 while (more_dirs && bigenough) {
3009 NFSCL_REQSTART(nd, NFSPROC_READDIR, vp);
3010 if (nd->nd_flag & ND_NFSV2) {
3011 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
3012 *tl++ = cookie.lval[1];
3013 *tl = txdr_unsigned(readsize);
3015 NFSM_BUILD(tl, u_int32_t *, reqsize);
3016 *tl++ = cookie.lval[0];
3017 *tl++ = cookie.lval[1];
3018 if (cookie.qval == 0) {
3023 *tl++ = dnp->n_cookieverf.nfsuquad[0];
3024 *tl++ = dnp->n_cookieverf.nfsuquad[1];
3027 if (nd->nd_flag & ND_NFSV4) {
3028 *tl++ = txdr_unsigned(readsize);
3029 *tl = txdr_unsigned(readsize);
3030 (void) nfsrv_putattrbit(nd, &attrbits);
3031 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
3032 *tl = txdr_unsigned(NFSV4OP_GETATTR);
3033 (void) nfsrv_putattrbit(nd, &dattrbits);
3035 *tl = txdr_unsigned(readsize);
3038 error = nfscl_request(nd, vp, p, cred, stuff);
3041 if (!(nd->nd_flag & ND_NFSV2)) {
3042 if (nd->nd_flag & ND_NFSV3)
3043 error = nfscl_postop_attr(nd, nap, attrflagp,
3045 if (!nd->nd_repstat && !error) {
3046 NFSM_DISSECT(tl, u_int32_t *, NFSX_HYPER);
3048 dnp->n_cookieverf.nfsuquad[0] = *tl++;
3049 dnp->n_cookieverf.nfsuquad[1] = *tl;
3053 if (nd->nd_repstat || error) {
3055 error = nd->nd_repstat;
3058 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3059 more_dirs = fxdr_unsigned(int, *tl);
3063 /* loop through the dir entries, doctoring them to 4bsd form */
3064 while (more_dirs && bigenough) {
3065 if (nd->nd_flag & ND_NFSV4) {
3066 NFSM_DISSECT(tl, u_int32_t *, 3*NFSX_UNSIGNED);
3067 ncookie.lval[0] = *tl++;
3068 ncookie.lval[1] = *tl++;
3069 len = fxdr_unsigned(int, *tl);
3070 } else if (nd->nd_flag & ND_NFSV3) {
3071 NFSM_DISSECT(tl, u_int32_t *, 3*NFSX_UNSIGNED);
3072 nfsva.na_fileid = fxdr_hyper(tl);
3074 len = fxdr_unsigned(int, *tl);
3076 NFSM_DISSECT(tl, u_int32_t *, 2*NFSX_UNSIGNED);
3077 nfsva.na_fileid = fxdr_unsigned(uint64_t,
3079 len = fxdr_unsigned(int, *tl);
3081 if (len <= 0 || len > NFS_MAXNAMLEN) {
3085 tlen = roundup2(len, 8);
3087 tlen += 8; /* To ensure null termination. */
3088 left = DIRBLKSIZ - blksiz;
3089 if (_GENERIC_DIRLEN(len) + NFSX_HYPER > left) {
3090 dp->d_reclen += left;
3091 uio_iov_base_add(uiop, left);
3092 uio_iov_len_add(uiop, -(left));
3093 uio_uio_resid_add(uiop, -(left));
3094 uiop->uio_offset += left;
3097 if (_GENERIC_DIRLEN(len) + NFSX_HYPER >
3098 uio_uio_resid(uiop))
3101 dp = (struct dirent *)uio_iov_base(uiop);
3104 dp->d_reclen = _GENERIC_DIRLEN(len) +
3106 dp->d_type = DT_UNKNOWN;
3107 blksiz += dp->d_reclen;
3108 if (blksiz == DIRBLKSIZ)
3110 uio_uio_resid_add(uiop, -(DIRHDSIZ));
3111 uiop->uio_offset += DIRHDSIZ;
3112 uio_iov_base_add(uiop, DIRHDSIZ);
3113 uio_iov_len_add(uiop, -(DIRHDSIZ));
3114 error = nfsm_mbufuio(nd, uiop, len);
3117 cp = uio_iov_base(uiop);
3119 *cp = '\0'; /* null terminate */
3120 cp += tlen; /* points to cookie storage */
3121 tl2 = (u_int32_t *)cp;
3122 uio_iov_base_add(uiop, (tlen + NFSX_HYPER));
3123 uio_iov_len_add(uiop, -(tlen + NFSX_HYPER));
3124 uio_uio_resid_add(uiop, -(tlen + NFSX_HYPER));
3125 uiop->uio_offset += (tlen + NFSX_HYPER);
3127 error = nfsm_advance(nd, NFSM_RNDUP(len), -1);
3131 if (nd->nd_flag & ND_NFSV4) {
3133 nfsva.na_mntonfileno = UINT64_MAX;
3134 error = nfsv4_loadattr(nd, NULL, &nfsva, NULL,
3135 NULL, 0, NULL, NULL, NULL, NULL, NULL, 0,
3136 NULL, NULL, &rderr, p, cred);
3139 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3140 } else if (nd->nd_flag & ND_NFSV3) {
3141 NFSM_DISSECT(tl, u_int32_t *, 3*NFSX_UNSIGNED);
3142 ncookie.lval[0] = *tl++;
3143 ncookie.lval[1] = *tl++;
3145 NFSM_DISSECT(tl, u_int32_t *, 2*NFSX_UNSIGNED);
3146 ncookie.lval[0] = 0;
3147 ncookie.lval[1] = *tl++;
3150 if (nd->nd_flag & ND_NFSV4) {
3155 if (nfsva.na_mntonfileno != UINT64_MAX)
3156 dp->d_fileno = nfsva.na_mntonfileno;
3158 dp->d_fileno = nfsva.na_fileid;
3159 } else if (nfsva.na_filesid[0] ==
3160 dnp->n_vattr.na_filesid[0] &&
3161 nfsva.na_filesid[1] ==
3162 dnp->n_vattr.na_filesid[1]) {
3163 dp->d_fileno = nfsva.na_fileid;
3167 } while (fakefileno ==
3169 dp->d_fileno = fakefileno;
3171 dp->d_type = vtonfs_dtype(nfsva.na_type);
3174 dp->d_fileno = nfsva.na_fileid;
3176 *tl2++ = cookiep->nfsuquad[0] = cookie.lval[0] =
3178 *tl2 = cookiep->nfsuquad[1] = cookie.lval[1] =
3181 more_dirs = fxdr_unsigned(int, *tl);
3184 * If at end of rpc data, get the eof boolean
3187 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3188 eof = fxdr_unsigned(int, *tl);
3191 if (nd->nd_flag & ND_NFSV4) {
3192 error = nfscl_postop_attr(nd, nap, attrflagp,
3198 mbuf_freem(nd->nd_mrep);
3202 * Fill last record, iff any, out to a multiple of DIRBLKSIZ
3203 * by increasing d_reclen for the last record.
3206 left = DIRBLKSIZ - blksiz;
3207 dp->d_reclen += left;
3208 uio_iov_base_add(uiop, left);
3209 uio_iov_len_add(uiop, -(left));
3210 uio_uio_resid_add(uiop, -(left));
3211 uiop->uio_offset += left;
3215 * If returning no data, assume end of file.
3216 * If not bigenough, return not end of file, since you aren't
3217 * returning all the data
3218 * Otherwise, return the eof flag from the server.
3221 if (tresid == ((size_t)(uio_uio_resid(uiop))))
3223 else if (!bigenough)
3230 * Add extra empty records to any remaining DIRBLKSIZ chunks.
3232 while (uio_uio_resid(uiop) > 0 && uio_uio_resid(uiop) != tresid) {
3233 dp = (struct dirent *)uio_iov_base(uiop);
3234 dp->d_type = DT_UNKNOWN;
3237 dp->d_name[0] = '\0';
3238 tl = (u_int32_t *)&dp->d_name[4];
3239 *tl++ = cookie.lval[0];
3240 *tl = cookie.lval[1];
3241 dp->d_reclen = DIRBLKSIZ;
3242 uio_iov_base_add(uiop, DIRBLKSIZ);
3243 uio_iov_len_add(uiop, -(DIRBLKSIZ));
3244 uio_uio_resid_add(uiop, -(DIRBLKSIZ));
3245 uiop->uio_offset += DIRBLKSIZ;
3249 if (nd->nd_mrep != NULL)
3250 mbuf_freem(nd->nd_mrep);
3256 * NFS V3 readdir plus RPC. Used in place of nfsrpc_readdir().
3257 * (Also used for NFS V4 when mount flag set.)
3258 * (ditto above w.r.t. multiple of DIRBLKSIZ, etc.)
3261 nfsrpc_readdirplus(vnode_t vp, struct uio *uiop, nfsuint64 *cookiep,
3262 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp,
3263 int *eofp, void *stuff)
3266 struct dirent *dp = NULL;
3268 vnode_t newvp = NULLVP;
3269 struct nfsrv_descript nfsd, *nd = &nfsd;
3270 struct nameidata nami, *ndp = &nami;
3271 struct componentname *cnp = &ndp->ni_cnd;
3272 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
3273 struct nfsnode *dnp = VTONFS(vp), *np;
3274 struct nfsvattr nfsva;
3276 nfsquad_t cookie, ncookie;
3277 int error = 0, tlen, more_dirs = 1, blksiz = 0, bigenough = 1;
3278 int attrflag, tryformoredirs = 1, eof = 0, gotmnton = 0;
3279 int isdotdot = 0, unlocknewvp = 0;
3280 u_int64_t dotfileid, dotdotfileid = 0, fakefileno = UINT64_MAX;
3281 u_int64_t fileno = 0;
3283 nfsattrbit_t attrbits, dattrbits;
3285 u_int32_t *tl2 = NULL, rderr;
3286 struct timespec dctime;
3288 KASSERT(uiop->uio_iovcnt == 1 &&
3289 (uio_uio_resid(uiop) & (DIRBLKSIZ - 1)) == 0,
3290 ("nfs readdirplusrpc bad uio"));
3291 timespecclear(&dctime);
3297 cookie.lval[0] = cookiep->nfsuquad[0];
3298 cookie.lval[1] = cookiep->nfsuquad[1];
3299 tresid = uio_uio_resid(uiop);
3302 * For NFSv4, first create the "." and ".." entries.
3304 if (NFSHASNFSV4(nmp)) {
3305 NFSGETATTR_ATTRBIT(&dattrbits);
3306 NFSZERO_ATTRBIT(&attrbits);
3307 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_FILEID);
3308 if (NFSISSET_ATTRBIT(&dnp->n_vattr.na_suppattr,
3309 NFSATTRBIT_MOUNTEDONFILEID)) {
3310 NFSSETBIT_ATTRBIT(&attrbits,
3311 NFSATTRBIT_MOUNTEDONFILEID);
3315 * Must fake it. Use the fileno, except when the
3316 * fsid is != to that of the directory. For that
3317 * case, generate a fake fileno that is not the same.
3319 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_FSID);
3324 * Joy, oh joy. For V4 we get to hand craft '.' and '..'.
3326 if (uiop->uio_offset == 0) {
3327 NFSCL_REQSTART(nd, NFSPROC_LOOKUPP, vp);
3328 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
3329 *tl++ = txdr_unsigned(NFSV4OP_GETFH);
3330 *tl = txdr_unsigned(NFSV4OP_GETATTR);
3331 (void) nfsrv_putattrbit(nd, &attrbits);
3332 error = nfscl_request(nd, vp, p, cred, stuff);
3335 dotfileid = 0; /* Fake out the compiler. */
3336 if ((nd->nd_flag & ND_NOMOREDATA) == 0) {
3337 error = nfsm_loadattr(nd, &nfsva);
3340 dctime = nfsva.na_ctime;
3341 dotfileid = nfsva.na_fileid;
3343 if (nd->nd_repstat == 0) {
3344 NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
3345 len = fxdr_unsigned(int, *(tl + 4));
3346 if (len > 0 && len <= NFSX_V4FHMAX)
3347 error = nfsm_advance(nd, NFSM_RNDUP(len), -1);
3351 NFSM_DISSECT(tl, u_int32_t *, 2*NFSX_UNSIGNED);
3352 nfsva.na_mntonfileno = UINT64_MAX;
3353 error = nfsv4_loadattr(nd, NULL, &nfsva, NULL,
3354 NULL, 0, NULL, NULL, NULL, NULL, NULL, 0,
3355 NULL, NULL, NULL, p, cred);
3357 dotdotfileid = dotfileid;
3358 } else if (gotmnton) {
3359 if (nfsva.na_mntonfileno != UINT64_MAX)
3360 dotdotfileid = nfsva.na_mntonfileno;
3362 dotdotfileid = nfsva.na_fileid;
3363 } else if (nfsva.na_filesid[0] ==
3364 dnp->n_vattr.na_filesid[0] &&
3365 nfsva.na_filesid[1] ==
3366 dnp->n_vattr.na_filesid[1]) {
3367 dotdotfileid = nfsva.na_fileid;
3371 } while (fakefileno ==
3373 dotdotfileid = fakefileno;
3376 } else if (nd->nd_repstat == NFSERR_NOENT) {
3378 * Lookupp returns NFSERR_NOENT when we are
3379 * at the root, so just use the current dir.
3382 dotdotfileid = dotfileid;
3384 error = nd->nd_repstat;
3386 mbuf_freem(nd->nd_mrep);
3390 dp = (struct dirent *)uio_iov_base(uiop);
3392 dp->d_type = DT_DIR;
3393 dp->d_fileno = dotfileid;
3395 *((uint64_t *)dp->d_name) = 0; /* Zero pad it. */
3396 dp->d_name[0] = '.';
3397 dp->d_reclen = _GENERIC_DIRSIZ(dp) + NFSX_HYPER;
3399 * Just make these offset cookie 0.
3401 tl = (u_int32_t *)&dp->d_name[8];
3404 blksiz += dp->d_reclen;
3405 uio_uio_resid_add(uiop, -(dp->d_reclen));
3406 uiop->uio_offset += dp->d_reclen;
3407 uio_iov_base_add(uiop, dp->d_reclen);
3408 uio_iov_len_add(uiop, -(dp->d_reclen));
3409 dp = (struct dirent *)uio_iov_base(uiop);
3411 dp->d_type = DT_DIR;
3412 dp->d_fileno = dotdotfileid;
3414 *((uint64_t *)dp->d_name) = 0;
3415 dp->d_name[0] = '.';
3416 dp->d_name[1] = '.';
3417 dp->d_reclen = _GENERIC_DIRSIZ(dp) + NFSX_HYPER;
3419 * Just make these offset cookie 0.
3421 tl = (u_int32_t *)&dp->d_name[8];
3424 blksiz += dp->d_reclen;
3425 uio_uio_resid_add(uiop, -(dp->d_reclen));
3426 uiop->uio_offset += dp->d_reclen;
3427 uio_iov_base_add(uiop, dp->d_reclen);
3428 uio_iov_len_add(uiop, -(dp->d_reclen));
3430 NFSREADDIRPLUS_ATTRBIT(&attrbits);
3432 NFSSETBIT_ATTRBIT(&attrbits,
3433 NFSATTRBIT_MOUNTEDONFILEID);
3437 * Loop around doing readdir rpc's of size nm_readdirsize.
3438 * The stopping criteria is EOF or buffer full.
3440 while (more_dirs && bigenough) {
3442 NFSCL_REQSTART(nd, NFSPROC_READDIRPLUS, vp);
3443 NFSM_BUILD(tl, u_int32_t *, 6 * NFSX_UNSIGNED);
3444 *tl++ = cookie.lval[0];
3445 *tl++ = cookie.lval[1];
3446 if (cookie.qval == 0) {
3451 *tl++ = dnp->n_cookieverf.nfsuquad[0];
3452 *tl++ = dnp->n_cookieverf.nfsuquad[1];
3455 *tl++ = txdr_unsigned(nmp->nm_readdirsize);
3456 *tl = txdr_unsigned(nmp->nm_readdirsize);
3457 if (nd->nd_flag & ND_NFSV4) {
3458 (void) nfsrv_putattrbit(nd, &attrbits);
3459 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
3460 *tl = txdr_unsigned(NFSV4OP_GETATTR);
3461 (void) nfsrv_putattrbit(nd, &dattrbits);
3463 error = nfscl_request(nd, vp, p, cred, stuff);
3466 if (nd->nd_flag & ND_NFSV3)
3467 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
3468 if (nd->nd_repstat || error) {
3470 error = nd->nd_repstat;
3473 if ((nd->nd_flag & ND_NFSV3) != 0 && *attrflagp != 0)
3474 dctime = nap->na_ctime;
3475 NFSM_DISSECT(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
3477 dnp->n_cookieverf.nfsuquad[0] = *tl++;
3478 dnp->n_cookieverf.nfsuquad[1] = *tl++;
3480 more_dirs = fxdr_unsigned(int, *tl);
3484 /* loop through the dir entries, doctoring them to 4bsd form */
3485 while (more_dirs && bigenough) {
3486 NFSM_DISSECT(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
3487 if (nd->nd_flag & ND_NFSV4) {
3488 ncookie.lval[0] = *tl++;
3489 ncookie.lval[1] = *tl++;
3491 fileno = fxdr_hyper(tl);
3494 len = fxdr_unsigned(int, *tl);
3495 if (len <= 0 || len > NFS_MAXNAMLEN) {
3499 tlen = roundup2(len, 8);
3501 tlen += 8; /* To ensure null termination. */
3502 left = DIRBLKSIZ - blksiz;
3503 if (_GENERIC_DIRLEN(len) + NFSX_HYPER > left) {
3504 dp->d_reclen += left;
3505 uio_iov_base_add(uiop, left);
3506 uio_iov_len_add(uiop, -(left));
3507 uio_uio_resid_add(uiop, -(left));
3508 uiop->uio_offset += left;
3511 if (_GENERIC_DIRLEN(len) + NFSX_HYPER >
3512 uio_uio_resid(uiop))
3515 dp = (struct dirent *)uio_iov_base(uiop);
3518 dp->d_reclen = _GENERIC_DIRLEN(len) +
3520 dp->d_type = DT_UNKNOWN;
3521 blksiz += dp->d_reclen;
3522 if (blksiz == DIRBLKSIZ)
3524 uio_uio_resid_add(uiop, -(DIRHDSIZ));
3525 uiop->uio_offset += DIRHDSIZ;
3526 uio_iov_base_add(uiop, DIRHDSIZ);
3527 uio_iov_len_add(uiop, -(DIRHDSIZ));
3528 cnp->cn_nameptr = uio_iov_base(uiop);
3529 cnp->cn_namelen = len;
3531 error = nfsm_mbufuio(nd, uiop, len);
3534 cp = uio_iov_base(uiop);
3537 cp += tlen; /* points to cookie storage */
3538 tl2 = (u_int32_t *)cp;
3539 if (len == 2 && cnp->cn_nameptr[0] == '.' &&
3540 cnp->cn_nameptr[1] == '.')
3544 uio_iov_base_add(uiop, (tlen + NFSX_HYPER));
3545 uio_iov_len_add(uiop, -(tlen + NFSX_HYPER));
3546 uio_uio_resid_add(uiop, -(tlen + NFSX_HYPER));
3547 uiop->uio_offset += (tlen + NFSX_HYPER);
3549 error = nfsm_advance(nd, NFSM_RNDUP(len), -1);
3554 if (nd->nd_flag & ND_NFSV3) {
3555 NFSM_DISSECT(tl, u_int32_t *, 3*NFSX_UNSIGNED);
3556 ncookie.lval[0] = *tl++;
3557 ncookie.lval[1] = *tl++;
3558 attrflag = fxdr_unsigned(int, *tl);
3560 error = nfsm_loadattr(nd, &nfsva);
3564 NFSM_DISSECT(tl,u_int32_t *,NFSX_UNSIGNED);
3566 error = nfsm_getfh(nd, &nfhp);
3570 if (!attrflag && nfhp != NULL) {
3571 FREE((caddr_t)nfhp, M_NFSFH);
3576 nfsva.na_mntonfileno = 0xffffffff;
3577 error = nfsv4_loadattr(nd, NULL, &nfsva, &nfhp,
3578 NULL, 0, NULL, NULL, NULL, NULL, NULL, 0,
3579 NULL, NULL, &rderr, p, cred);
3585 if (nd->nd_flag & ND_NFSV4) {
3588 } else if (gotmnton) {
3589 if (nfsva.na_mntonfileno != 0xffffffff)
3590 dp->d_fileno = nfsva.na_mntonfileno;
3592 dp->d_fileno = nfsva.na_fileid;
3593 } else if (nfsva.na_filesid[0] ==
3594 dnp->n_vattr.na_filesid[0] &&
3595 nfsva.na_filesid[1] ==
3596 dnp->n_vattr.na_filesid[1]) {
3597 dp->d_fileno = nfsva.na_fileid;
3601 } while (fakefileno ==
3603 dp->d_fileno = fakefileno;
3606 dp->d_fileno = fileno;
3608 *tl2++ = cookiep->nfsuquad[0] = cookie.lval[0] =
3610 *tl2 = cookiep->nfsuquad[1] = cookie.lval[1] =
3614 if (NFSRV_CMPFH(nfhp->nfh_fh, nfhp->nfh_len,
3615 dnp->n_fhp->nfh_fh, dnp->n_fhp->nfh_len)) {
3619 FREE((caddr_t)nfhp, M_NFSFH);
3621 } else if (isdotdot != 0) {
3623 * Skip doing a nfscl_nget() call for "..".
3624 * There's a race between acquiring the nfs
3625 * node here and lookups that look for the
3626 * directory being read (in the parent).
3627 * It would try to get a lock on ".." here,
3628 * owning the lock on the directory being
3629 * read. Lookup will hold the lock on ".."
3630 * and try to acquire the lock on the
3631 * directory being read.
3632 * If the directory is unlocked/relocked,
3633 * then there is a LOR with the buflock
3636 free(nfhp, M_NFSFH);
3638 error = nfscl_nget(vnode_mount(vp), vp,
3639 nfhp, cnp, p, &np, NULL, LK_EXCLUSIVE);
3646 if (newvp != NULLVP) {
3647 error = nfscl_loadattrcache(&newvp,
3648 &nfsva, NULL, NULL, 0, 0);
3657 vtonfs_dtype(np->n_vattr.na_type);
3659 NFSCNHASH(cnp, HASHINIT);
3660 if (cnp->cn_namelen <= NCHNAMLEN &&
3661 (newvp->v_type != VDIR ||
3662 dctime.tv_sec != 0)) {
3663 cache_enter_time(ndp->ni_dvp,
3666 newvp->v_type != VDIR ? NULL :
3676 } else if (nfhp != NULL) {
3677 FREE((caddr_t)nfhp, M_NFSFH);
3679 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3680 more_dirs = fxdr_unsigned(int, *tl);
3683 * If at end of rpc data, get the eof boolean
3686 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3687 eof = fxdr_unsigned(int, *tl);
3690 if (nd->nd_flag & ND_NFSV4) {
3691 error = nfscl_postop_attr(nd, nap, attrflagp,
3697 mbuf_freem(nd->nd_mrep);
3701 * Fill last record, iff any, out to a multiple of DIRBLKSIZ
3702 * by increasing d_reclen for the last record.
3705 left = DIRBLKSIZ - blksiz;
3706 dp->d_reclen += left;
3707 uio_iov_base_add(uiop, left);
3708 uio_iov_len_add(uiop, -(left));
3709 uio_uio_resid_add(uiop, -(left));
3710 uiop->uio_offset += left;
3714 * If returning no data, assume end of file.
3715 * If not bigenough, return not end of file, since you aren't
3716 * returning all the data
3717 * Otherwise, return the eof flag from the server.
3720 if (tresid == uio_uio_resid(uiop))
3722 else if (!bigenough)
3729 * Add extra empty records to any remaining DIRBLKSIZ chunks.
3731 while (uio_uio_resid(uiop) > 0 && uio_uio_resid(uiop) != tresid) {
3732 dp = (struct dirent *)uio_iov_base(uiop);
3733 dp->d_type = DT_UNKNOWN;
3736 dp->d_name[0] = '\0';
3737 tl = (u_int32_t *)&dp->d_name[4];
3738 *tl++ = cookie.lval[0];
3739 *tl = cookie.lval[1];
3740 dp->d_reclen = DIRBLKSIZ;
3741 uio_iov_base_add(uiop, DIRBLKSIZ);
3742 uio_iov_len_add(uiop, -(DIRBLKSIZ));
3743 uio_uio_resid_add(uiop, -(DIRBLKSIZ));
3744 uiop->uio_offset += DIRBLKSIZ;
3748 if (nd->nd_mrep != NULL)
3749 mbuf_freem(nd->nd_mrep);
3758 nfsrpc_commit(vnode_t vp, u_quad_t offset, int cnt, struct ucred *cred,
3759 NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, void *stuff)
3762 struct nfsrv_descript nfsd, *nd = &nfsd;
3763 nfsattrbit_t attrbits;
3765 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
3768 NFSCL_REQSTART(nd, NFSPROC_COMMIT, vp);
3769 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
3770 txdr_hyper(offset, tl);
3772 *tl = txdr_unsigned(cnt);
3773 if (nd->nd_flag & ND_NFSV4) {
3775 * And do a Getattr op.
3777 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
3778 *tl = txdr_unsigned(NFSV4OP_GETATTR);
3779 NFSGETATTR_ATTRBIT(&attrbits);
3780 (void) nfsrv_putattrbit(nd, &attrbits);
3782 error = nfscl_request(nd, vp, p, cred, stuff);
3785 error = nfscl_wcc_data(nd, vp, nap, attrflagp, NULL, stuff);
3786 if (!error && !nd->nd_repstat) {
3787 NFSM_DISSECT(tl, u_int32_t *, NFSX_VERF);
3789 if (NFSBCMP(nmp->nm_verf, tl, NFSX_VERF)) {
3790 NFSBCOPY(tl, nmp->nm_verf, NFSX_VERF);
3791 nd->nd_repstat = NFSERR_STALEWRITEVERF;
3794 if (nd->nd_flag & ND_NFSV4)
3795 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
3798 if (!error && nd->nd_repstat)
3799 error = nd->nd_repstat;
3800 mbuf_freem(nd->nd_mrep);
3805 * NFS byte range lock rpc.
3806 * (Mostly just calls one of the three lower level RPC routines.)
3809 nfsrpc_advlock(vnode_t vp, off_t size, int op, struct flock *fl,
3810 int reclaim, struct ucred *cred, NFSPROC_T *p, void *id, int flags)
3812 struct nfscllockowner *lp;
3813 struct nfsclclient *clp;
3815 struct nfsrv_descript nfsd, *nd = &nfsd;
3816 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
3819 u_int32_t clidrev = 0;
3820 int error = 0, newone = 0, expireret = 0, retrycnt, donelocally;
3824 * Convert the flock structure into a start and end and do POSIX
3827 switch (fl->l_whence) {
3831 * Caller is responsible for adding any necessary offset
3832 * when SEEK_CUR is used.
3834 start = fl->l_start;
3838 start = size + fl->l_start;
3839 off = size + fl->l_start;
3846 if (fl->l_len != 0) {
3847 end = start + fl->l_len - 1;
3858 if (op == F_GETLK) {
3859 error = nfscl_getcl(vnode_mount(vp), cred, p, 1, &clp);
3862 error = nfscl_lockt(vp, clp, off, len, fl, p, id, flags);
3864 clidrev = clp->nfsc_clientidrev;
3865 error = nfsrpc_lockt(nd, vp, clp, off, len, fl, cred,
3867 } else if (error == -1) {
3870 nfscl_clientrelease(clp);
3871 } else if (op == F_UNLCK && fl->l_type == F_UNLCK) {
3873 * We must loop around for all lockowner cases.
3876 error = nfscl_getcl(vnode_mount(vp), cred, p, 1, &clp);
3880 error = nfscl_relbytelock(vp, off, len, cred, p, callcnt,
3881 clp, id, flags, &lp, &dorpc);
3883 * If it returns a NULL lp, we're done.
3887 nfscl_clientrelease(clp);
3889 nfscl_releasealllocks(clp, vp, p, id, flags);
3892 if (nmp->nm_clp != NULL)
3893 clidrev = nmp->nm_clp->nfsc_clientidrev;
3897 * If the server doesn't support Posix lock semantics,
3898 * only allow locks on the entire file, since it won't
3899 * handle overlapping byte ranges.
3900 * There might still be a problem when a lock
3901 * upgrade/downgrade (read<->write) occurs, since the
3902 * server "might" expect an unlock first?
3904 if (dorpc && (lp->nfsl_open->nfso_posixlock ||
3905 (off == 0 && len == NFS64BITSSET))) {
3907 * Since the lock records will go away, we must
3908 * wait for grace and delay here.
3911 error = nfsrpc_locku(nd, nmp, lp, off, len,
3912 NFSV4LOCKT_READ, cred, p, 0);
3913 if ((nd->nd_repstat == NFSERR_GRACE ||
3914 nd->nd_repstat == NFSERR_DELAY) &&
3916 (void) nfs_catnap(PZERO, (int)nd->nd_repstat,
3918 } while ((nd->nd_repstat == NFSERR_GRACE ||
3919 nd->nd_repstat == NFSERR_DELAY) && error == 0);
3922 } while (error == 0 && nd->nd_repstat == 0);
3923 nfscl_releasealllocks(clp, vp, p, id, flags);
3924 } else if (op == F_SETLK) {
3925 error = nfscl_getbytelock(vp, off, len, fl->l_type, cred, p,
3926 NULL, 0, id, flags, NULL, NULL, &lp, &newone, &donelocally);
3927 if (error || donelocally) {
3930 if (nmp->nm_clp != NULL)
3931 clidrev = nmp->nm_clp->nfsc_clientidrev;
3934 nfhp = VTONFS(vp)->n_fhp;
3935 if (!lp->nfsl_open->nfso_posixlock &&
3936 (off != 0 || len != NFS64BITSSET)) {
3939 error = nfsrpc_lock(nd, nmp, vp, nfhp->nfh_fh,
3940 nfhp->nfh_len, lp, newone, reclaim, off,
3941 len, fl->l_type, cred, p, 0);
3944 error = nd->nd_repstat;
3945 nfscl_lockrelease(lp, error, newone);
3950 error = nd->nd_repstat;
3951 if (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
3952 error == NFSERR_STALEDONTRECOVER ||
3953 error == NFSERR_STALECLIENTID || error == NFSERR_DELAY ||
3954 error == NFSERR_BADSESSION) {
3955 (void) nfs_catnap(PZERO, error, "nfs_advlock");
3956 } else if ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID)
3958 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
3961 } while (error == NFSERR_GRACE ||
3962 error == NFSERR_STALECLIENTID || error == NFSERR_DELAY ||
3963 error == NFSERR_STALEDONTRECOVER || error == NFSERR_STALESTATEID ||
3964 error == NFSERR_BADSESSION ||
3965 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
3966 expireret == 0 && clidrev != 0 && retrycnt < 4));
3967 if (error && retrycnt >= 4)
3973 * The lower level routine for the LockT case.
3976 nfsrpc_lockt(struct nfsrv_descript *nd, vnode_t vp,
3977 struct nfsclclient *clp, u_int64_t off, u_int64_t len, struct flock *fl,
3978 struct ucred *cred, NFSPROC_T *p, void *id, int flags)
3981 int error, type, size;
3982 uint8_t own[NFSV4CL_LOCKNAMELEN + NFSX_V4FHMAX];
3984 struct nfsmount *nmp;
3985 struct nfsclsession *tsep;
3987 nmp = VFSTONFS(vp->v_mount);
3988 NFSCL_REQSTART(nd, NFSPROC_LOCKT, vp);
3989 NFSM_BUILD(tl, u_int32_t *, 7 * NFSX_UNSIGNED);
3990 if (fl->l_type == F_RDLCK)
3991 *tl++ = txdr_unsigned(NFSV4LOCKT_READ);
3993 *tl++ = txdr_unsigned(NFSV4LOCKT_WRITE);
3994 txdr_hyper(off, tl);
3996 txdr_hyper(len, tl);
3998 tsep = nfsmnt_mdssession(nmp);
3999 *tl++ = tsep->nfsess_clientid.lval[0];
4000 *tl = tsep->nfsess_clientid.lval[1];
4001 nfscl_filllockowner(id, own, flags);
4003 NFSBCOPY(np->n_fhp->nfh_fh, &own[NFSV4CL_LOCKNAMELEN],
4004 np->n_fhp->nfh_len);
4005 (void)nfsm_strtom(nd, own, NFSV4CL_LOCKNAMELEN + np->n_fhp->nfh_len);
4006 error = nfscl_request(nd, vp, p, cred, NULL);
4009 if (nd->nd_repstat == 0) {
4010 fl->l_type = F_UNLCK;
4011 } else if (nd->nd_repstat == NFSERR_DENIED) {
4013 fl->l_whence = SEEK_SET;
4014 NFSM_DISSECT(tl, u_int32_t *, 8 * NFSX_UNSIGNED);
4015 fl->l_start = fxdr_hyper(tl);
4017 len = fxdr_hyper(tl);
4019 if (len == NFS64BITSSET)
4023 type = fxdr_unsigned(int, *tl++);
4024 if (type == NFSV4LOCKT_WRITE)
4025 fl->l_type = F_WRLCK;
4027 fl->l_type = F_RDLCK;
4029 * XXX For now, I have no idea what to do with the
4030 * conflicting lock_owner, so I'll just set the pid == 0
4031 * and skip over the lock_owner.
4033 fl->l_pid = (pid_t)0;
4035 size = fxdr_unsigned(int, *tl);
4036 if (size < 0 || size > NFSV4_OPAQUELIMIT)
4039 error = nfsm_advance(nd, NFSM_RNDUP(size), -1);
4040 } else if (nd->nd_repstat == NFSERR_STALECLIENTID)
4041 nfscl_initiate_recovery(clp);
4043 mbuf_freem(nd->nd_mrep);
4048 * Lower level function that performs the LockU RPC.
4051 nfsrpc_locku(struct nfsrv_descript *nd, struct nfsmount *nmp,
4052 struct nfscllockowner *lp, u_int64_t off, u_int64_t len,
4053 u_int32_t type, struct ucred *cred, NFSPROC_T *p, int syscred)
4058 nfscl_reqstart(nd, NFSPROC_LOCKU, nmp, lp->nfsl_open->nfso_fh,
4059 lp->nfsl_open->nfso_fhlen, NULL, NULL, 0, 0);
4060 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID + 6 * NFSX_UNSIGNED);
4061 *tl++ = txdr_unsigned(type);
4062 *tl = txdr_unsigned(lp->nfsl_seqid);
4063 if (nfstest_outofseq &&
4064 (arc4random() % nfstest_outofseq) == 0)
4065 *tl = txdr_unsigned(lp->nfsl_seqid + 1);
4067 if (NFSHASNFSV4N(nmp))
4070 *tl++ = lp->nfsl_stateid.seqid;
4071 *tl++ = lp->nfsl_stateid.other[0];
4072 *tl++ = lp->nfsl_stateid.other[1];
4073 *tl++ = lp->nfsl_stateid.other[2];
4074 txdr_hyper(off, tl);
4076 txdr_hyper(len, tl);
4078 nd->nd_flag |= ND_USEGSSNAME;
4079 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4080 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4081 NFSCL_INCRSEQID(lp->nfsl_seqid, nd);
4084 if (nd->nd_repstat == 0) {
4085 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID);
4086 lp->nfsl_stateid.seqid = *tl++;
4087 lp->nfsl_stateid.other[0] = *tl++;
4088 lp->nfsl_stateid.other[1] = *tl++;
4089 lp->nfsl_stateid.other[2] = *tl;
4090 } else if (nd->nd_repstat == NFSERR_STALESTATEID)
4091 nfscl_initiate_recovery(lp->nfsl_open->nfso_own->nfsow_clp);
4093 mbuf_freem(nd->nd_mrep);
4098 * The actual Lock RPC.
4101 nfsrpc_lock(struct nfsrv_descript *nd, struct nfsmount *nmp, vnode_t vp,
4102 u_int8_t *nfhp, int fhlen, struct nfscllockowner *lp, int newone,
4103 int reclaim, u_int64_t off, u_int64_t len, short type, struct ucred *cred,
4104 NFSPROC_T *p, int syscred)
4108 uint8_t own[NFSV4CL_LOCKNAMELEN + NFSX_V4FHMAX];
4109 struct nfsclsession *tsep;
4111 nfscl_reqstart(nd, NFSPROC_LOCK, nmp, nfhp, fhlen, NULL, NULL, 0, 0);
4112 NFSM_BUILD(tl, u_int32_t *, 7 * NFSX_UNSIGNED);
4113 if (type == F_RDLCK)
4114 *tl++ = txdr_unsigned(NFSV4LOCKT_READ);
4116 *tl++ = txdr_unsigned(NFSV4LOCKT_WRITE);
4117 *tl++ = txdr_unsigned(reclaim);
4118 txdr_hyper(off, tl);
4120 txdr_hyper(len, tl);
4124 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID +
4125 2 * NFSX_UNSIGNED + NFSX_HYPER);
4126 *tl++ = txdr_unsigned(lp->nfsl_open->nfso_own->nfsow_seqid);
4127 if (NFSHASNFSV4N(nmp))
4130 *tl++ = lp->nfsl_open->nfso_stateid.seqid;
4131 *tl++ = lp->nfsl_open->nfso_stateid.other[0];
4132 *tl++ = lp->nfsl_open->nfso_stateid.other[1];
4133 *tl++ = lp->nfsl_open->nfso_stateid.other[2];
4134 *tl++ = txdr_unsigned(lp->nfsl_seqid);
4135 tsep = nfsmnt_mdssession(nmp);
4136 *tl++ = tsep->nfsess_clientid.lval[0];
4137 *tl = tsep->nfsess_clientid.lval[1];
4138 NFSBCOPY(lp->nfsl_owner, own, NFSV4CL_LOCKNAMELEN);
4139 NFSBCOPY(nfhp, &own[NFSV4CL_LOCKNAMELEN], fhlen);
4140 (void)nfsm_strtom(nd, own, NFSV4CL_LOCKNAMELEN + fhlen);
4143 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID + NFSX_UNSIGNED);
4144 if (NFSHASNFSV4N(nmp))
4147 *tl++ = lp->nfsl_stateid.seqid;
4148 *tl++ = lp->nfsl_stateid.other[0];
4149 *tl++ = lp->nfsl_stateid.other[1];
4150 *tl++ = lp->nfsl_stateid.other[2];
4151 *tl = txdr_unsigned(lp->nfsl_seqid);
4152 if (nfstest_outofseq &&
4153 (arc4random() % nfstest_outofseq) == 0)
4154 *tl = txdr_unsigned(lp->nfsl_seqid + 1);
4157 nd->nd_flag |= ND_USEGSSNAME;
4158 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, vp, p, cred,
4159 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4163 NFSCL_INCRSEQID(lp->nfsl_open->nfso_own->nfsow_seqid, nd);
4164 NFSCL_INCRSEQID(lp->nfsl_seqid, nd);
4165 if (nd->nd_repstat == 0) {
4166 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID);
4167 lp->nfsl_stateid.seqid = *tl++;
4168 lp->nfsl_stateid.other[0] = *tl++;
4169 lp->nfsl_stateid.other[1] = *tl++;
4170 lp->nfsl_stateid.other[2] = *tl;
4171 } else if (nd->nd_repstat == NFSERR_DENIED) {
4172 NFSM_DISSECT(tl, u_int32_t *, 8 * NFSX_UNSIGNED);
4173 size = fxdr_unsigned(int, *(tl + 7));
4174 if (size < 0 || size > NFSV4_OPAQUELIMIT)
4177 error = nfsm_advance(nd, NFSM_RNDUP(size), -1);
4178 } else if (nd->nd_repstat == NFSERR_STALESTATEID)
4179 nfscl_initiate_recovery(lp->nfsl_open->nfso_own->nfsow_clp);
4181 mbuf_freem(nd->nd_mrep);
4187 * (always called with the vp for the mount point)
4190 nfsrpc_statfs(vnode_t vp, struct nfsstatfs *sbp, struct nfsfsinfo *fsp,
4191 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp,
4194 u_int32_t *tl = NULL;
4195 struct nfsrv_descript nfsd, *nd = &nfsd;
4196 struct nfsmount *nmp;
4197 nfsattrbit_t attrbits;
4201 nmp = VFSTONFS(vnode_mount(vp));
4202 if (NFSHASNFSV4(nmp)) {
4204 * For V4, you actually do a getattr.
4206 NFSCL_REQSTART(nd, NFSPROC_GETATTR, vp);
4207 NFSSTATFS_GETATTRBIT(&attrbits);
4208 (void) nfsrv_putattrbit(nd, &attrbits);
4209 nd->nd_flag |= ND_USEGSSNAME;
4210 error = nfscl_request(nd, vp, p, cred, stuff);
4213 if (nd->nd_repstat == 0) {
4214 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0,
4215 NULL, NULL, sbp, fsp, NULL, 0, NULL, NULL, NULL, p,
4218 nmp->nm_fsid[0] = nap->na_filesid[0];
4219 nmp->nm_fsid[1] = nap->na_filesid[1];
4220 NFSSETHASSETFSID(nmp);
4224 error = nd->nd_repstat;
4229 NFSCL_REQSTART(nd, NFSPROC_FSSTAT, vp);
4230 error = nfscl_request(nd, vp, p, cred, stuff);
4233 if (nd->nd_flag & ND_NFSV3) {
4234 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
4238 if (nd->nd_repstat) {
4239 error = nd->nd_repstat;
4242 NFSM_DISSECT(tl, u_int32_t *,
4243 NFSX_STATFS(nd->nd_flag & ND_NFSV3));
4245 if (NFSHASNFSV3(nmp)) {
4246 sbp->sf_tbytes = fxdr_hyper(tl); tl += 2;
4247 sbp->sf_fbytes = fxdr_hyper(tl); tl += 2;
4248 sbp->sf_abytes = fxdr_hyper(tl); tl += 2;
4249 sbp->sf_tfiles = fxdr_hyper(tl); tl += 2;
4250 sbp->sf_ffiles = fxdr_hyper(tl); tl += 2;
4251 sbp->sf_afiles = fxdr_hyper(tl); tl += 2;
4252 sbp->sf_invarsec = fxdr_unsigned(u_int32_t, *tl);
4253 } else if (NFSHASNFSV4(nmp) == 0) {
4254 sbp->sf_tsize = fxdr_unsigned(u_int32_t, *tl++);
4255 sbp->sf_bsize = fxdr_unsigned(u_int32_t, *tl++);
4256 sbp->sf_blocks = fxdr_unsigned(u_int32_t, *tl++);
4257 sbp->sf_bfree = fxdr_unsigned(u_int32_t, *tl++);
4258 sbp->sf_bavail = fxdr_unsigned(u_int32_t, *tl);
4261 mbuf_freem(nd->nd_mrep);
4269 nfsrpc_pathconf(vnode_t vp, struct nfsv3_pathconf *pc,
4270 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp,
4273 struct nfsrv_descript nfsd, *nd = &nfsd;
4274 struct nfsmount *nmp;
4276 nfsattrbit_t attrbits;
4280 nmp = VFSTONFS(vnode_mount(vp));
4281 if (NFSHASNFSV4(nmp)) {
4283 * For V4, you actually do a getattr.
4285 NFSCL_REQSTART(nd, NFSPROC_GETATTR, vp);
4286 NFSPATHCONF_GETATTRBIT(&attrbits);
4287 (void) nfsrv_putattrbit(nd, &attrbits);
4288 nd->nd_flag |= ND_USEGSSNAME;
4289 error = nfscl_request(nd, vp, p, cred, stuff);
4292 if (nd->nd_repstat == 0) {
4293 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0,
4294 pc, NULL, NULL, NULL, NULL, 0, NULL, NULL, NULL, p,
4299 error = nd->nd_repstat;
4302 NFSCL_REQSTART(nd, NFSPROC_PATHCONF, vp);
4303 error = nfscl_request(nd, vp, p, cred, stuff);
4306 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
4307 if (nd->nd_repstat && !error)
4308 error = nd->nd_repstat;
4310 NFSM_DISSECT(tl, u_int32_t *, NFSX_V3PATHCONF);
4311 pc->pc_linkmax = fxdr_unsigned(u_int32_t, *tl++);
4312 pc->pc_namemax = fxdr_unsigned(u_int32_t, *tl++);
4313 pc->pc_notrunc = fxdr_unsigned(u_int32_t, *tl++);
4314 pc->pc_chownrestricted =
4315 fxdr_unsigned(u_int32_t, *tl++);
4316 pc->pc_caseinsensitive =
4317 fxdr_unsigned(u_int32_t, *tl++);
4318 pc->pc_casepreserving = fxdr_unsigned(u_int32_t, *tl);
4322 mbuf_freem(nd->nd_mrep);
4327 * nfs version 3 fsinfo rpc call
4330 nfsrpc_fsinfo(vnode_t vp, struct nfsfsinfo *fsp, struct ucred *cred,
4331 NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, void *stuff)
4334 struct nfsrv_descript nfsd, *nd = &nfsd;
4338 NFSCL_REQSTART(nd, NFSPROC_FSINFO, vp);
4339 error = nfscl_request(nd, vp, p, cred, stuff);
4342 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
4343 if (nd->nd_repstat && !error)
4344 error = nd->nd_repstat;
4346 NFSM_DISSECT(tl, u_int32_t *, NFSX_V3FSINFO);
4347 fsp->fs_rtmax = fxdr_unsigned(u_int32_t, *tl++);
4348 fsp->fs_rtpref = fxdr_unsigned(u_int32_t, *tl++);
4349 fsp->fs_rtmult = fxdr_unsigned(u_int32_t, *tl++);
4350 fsp->fs_wtmax = fxdr_unsigned(u_int32_t, *tl++);
4351 fsp->fs_wtpref = fxdr_unsigned(u_int32_t, *tl++);
4352 fsp->fs_wtmult = fxdr_unsigned(u_int32_t, *tl++);
4353 fsp->fs_dtpref = fxdr_unsigned(u_int32_t, *tl++);
4354 fsp->fs_maxfilesize = fxdr_hyper(tl);
4356 fxdr_nfsv3time(tl, &fsp->fs_timedelta);
4358 fsp->fs_properties = fxdr_unsigned(u_int32_t, *tl);
4361 mbuf_freem(nd->nd_mrep);
4366 * This function performs the Renew RPC.
4369 nfsrpc_renew(struct nfsclclient *clp, struct nfsclds *dsp, struct ucred *cred,
4373 struct nfsrv_descript nfsd;
4374 struct nfsrv_descript *nd = &nfsd;
4375 struct nfsmount *nmp;
4377 struct nfssockreq *nrp;
4378 struct nfsclsession *tsep;
4380 nmp = clp->nfsc_nmp;
4384 nfscl_reqstart(nd, NFSPROC_RENEW, nmp, NULL, 0, NULL, NULL, 0,
4387 nfscl_reqstart(nd, NFSPROC_RENEW, nmp, NULL, 0, NULL,
4388 &dsp->nfsclds_sess, 0, 0);
4389 if (!NFSHASNFSV4N(nmp)) {
4390 /* NFSv4.1 just uses a Sequence Op and not a Renew. */
4391 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
4392 tsep = nfsmnt_mdssession(nmp);
4393 *tl++ = tsep->nfsess_clientid.lval[0];
4394 *tl = tsep->nfsess_clientid.lval[1];
4398 nrp = dsp->nfsclds_sockp;
4400 /* If NULL, use the MDS socket. */
4401 nrp = &nmp->nm_sockreq;
4402 nd->nd_flag |= ND_USEGSSNAME;
4404 error = newnfs_request(nd, nmp, NULL, nrp, NULL, p, cred,
4405 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4407 error = newnfs_request(nd, nmp, NULL, nrp, NULL, p, cred,
4408 NFS_PROG, NFS_VER4, NULL, 1, NULL, &dsp->nfsclds_sess);
4411 error = nd->nd_repstat;
4412 mbuf_freem(nd->nd_mrep);
4417 * This function performs the Releaselockowner RPC.
4420 nfsrpc_rellockown(struct nfsmount *nmp, struct nfscllockowner *lp,
4421 uint8_t *fh, int fhlen, struct ucred *cred, NFSPROC_T *p)
4423 struct nfsrv_descript nfsd, *nd = &nfsd;
4426 uint8_t own[NFSV4CL_LOCKNAMELEN + NFSX_V4FHMAX];
4427 struct nfsclsession *tsep;
4429 if (NFSHASNFSV4N(nmp)) {
4430 /* For NFSv4.1, do a FreeStateID. */
4431 nfscl_reqstart(nd, NFSPROC_FREESTATEID, nmp, NULL, 0, NULL,
4433 nfsm_stateidtom(nd, &lp->nfsl_stateid, NFSSTATEID_PUTSTATEID);
4435 nfscl_reqstart(nd, NFSPROC_RELEASELCKOWN, nmp, NULL, 0, NULL,
4437 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
4438 tsep = nfsmnt_mdssession(nmp);
4439 *tl++ = tsep->nfsess_clientid.lval[0];
4440 *tl = tsep->nfsess_clientid.lval[1];
4441 NFSBCOPY(lp->nfsl_owner, own, NFSV4CL_LOCKNAMELEN);
4442 NFSBCOPY(fh, &own[NFSV4CL_LOCKNAMELEN], fhlen);
4443 (void)nfsm_strtom(nd, own, NFSV4CL_LOCKNAMELEN + fhlen);
4445 nd->nd_flag |= ND_USEGSSNAME;
4446 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4447 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4450 error = nd->nd_repstat;
4451 mbuf_freem(nd->nd_mrep);
4456 * This function performs the Compound to get the mount pt FH.
4459 nfsrpc_getdirpath(struct nfsmount *nmp, u_char *dirpath, struct ucred *cred,
4463 struct nfsrv_descript nfsd;
4464 struct nfsrv_descript *nd = &nfsd;
4466 int error, cnt, len, setnil;
4469 nfscl_reqstart(nd, NFSPROC_PUTROOTFH, nmp, NULL, 0, &opcntp, NULL, 0,
4478 while (*cp2 != '\0' && *cp2 != '/')
4485 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
4486 *tl = txdr_unsigned(NFSV4OP_LOOKUP);
4487 nfsm_strtom(nd, cp, strlen(cp));
4493 } while (*cp != '\0');
4494 if (NFSHASNFSV4N(nmp))
4495 /* Has a Sequence Op done by nfscl_reqstart(). */
4496 *opcntp = txdr_unsigned(3 + cnt);
4498 *opcntp = txdr_unsigned(2 + cnt);
4499 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
4500 *tl = txdr_unsigned(NFSV4OP_GETFH);
4501 nd->nd_flag |= ND_USEGSSNAME;
4502 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4503 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4506 if (nd->nd_repstat == 0) {
4507 NFSM_DISSECT(tl, u_int32_t *, (3 + 2 * cnt) * NFSX_UNSIGNED);
4508 tl += (2 + 2 * cnt);
4509 if ((len = fxdr_unsigned(int, *tl)) <= 0 ||
4511 nd->nd_repstat = NFSERR_BADXDR;
4513 nd->nd_repstat = nfsrv_mtostr(nd, nmp->nm_fh, len);
4514 if (nd->nd_repstat == 0)
4515 nmp->nm_fhsize = len;
4518 error = nd->nd_repstat;
4520 mbuf_freem(nd->nd_mrep);
4525 * This function performs the Delegreturn RPC.
4528 nfsrpc_delegreturn(struct nfscldeleg *dp, struct ucred *cred,
4529 struct nfsmount *nmp, NFSPROC_T *p, int syscred)
4532 struct nfsrv_descript nfsd;
4533 struct nfsrv_descript *nd = &nfsd;
4536 nfscl_reqstart(nd, NFSPROC_DELEGRETURN, nmp, dp->nfsdl_fh,
4537 dp->nfsdl_fhlen, NULL, NULL, 0, 0);
4538 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID);
4539 if (NFSHASNFSV4N(nmp))
4542 *tl++ = dp->nfsdl_stateid.seqid;
4543 *tl++ = dp->nfsdl_stateid.other[0];
4544 *tl++ = dp->nfsdl_stateid.other[1];
4545 *tl = dp->nfsdl_stateid.other[2];
4547 nd->nd_flag |= ND_USEGSSNAME;
4548 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4549 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4552 error = nd->nd_repstat;
4553 mbuf_freem(nd->nd_mrep);
4561 nfsrpc_getacl(vnode_t vp, struct ucred *cred, NFSPROC_T *p,
4562 struct acl *aclp, void *stuff)
4564 struct nfsrv_descript nfsd, *nd = &nfsd;
4566 nfsattrbit_t attrbits;
4567 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
4569 if (nfsrv_useacl == 0 || !NFSHASNFSV4(nmp))
4570 return (EOPNOTSUPP);
4571 NFSCL_REQSTART(nd, NFSPROC_GETACL, vp);
4572 NFSZERO_ATTRBIT(&attrbits);
4573 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_ACL);
4574 (void) nfsrv_putattrbit(nd, &attrbits);
4575 error = nfscl_request(nd, vp, p, cred, stuff);
4578 if (!nd->nd_repstat)
4579 error = nfsv4_loadattr(nd, vp, NULL, NULL, NULL, 0, NULL,
4580 NULL, NULL, NULL, aclp, 0, NULL, NULL, NULL, p, cred);
4582 error = nd->nd_repstat;
4583 mbuf_freem(nd->nd_mrep);
4591 nfsrpc_setacl(vnode_t vp, struct ucred *cred, NFSPROC_T *p,
4592 struct acl *aclp, void *stuff)
4595 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
4597 if (nfsrv_useacl == 0 || !NFSHASNFSV4(nmp))
4598 return (EOPNOTSUPP);
4599 error = nfsrpc_setattr(vp, NULL, aclp, cred, p, NULL, NULL, stuff);
4607 nfsrpc_setaclrpc(vnode_t vp, struct ucred *cred, NFSPROC_T *p,
4608 struct acl *aclp, nfsv4stateid_t *stateidp, void *stuff)
4610 struct nfsrv_descript nfsd, *nd = &nfsd;
4612 nfsattrbit_t attrbits;
4613 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
4615 if (!NFSHASNFSV4(nmp))
4616 return (EOPNOTSUPP);
4617 NFSCL_REQSTART(nd, NFSPROC_SETACL, vp);
4618 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID);
4619 NFSZERO_ATTRBIT(&attrbits);
4620 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_ACL);
4621 (void) nfsv4_fillattr(nd, vnode_mount(vp), vp, aclp, NULL, NULL, 0,
4622 &attrbits, NULL, NULL, 0, 0, 0, 0, (uint64_t)0);
4623 error = nfscl_request(nd, vp, p, cred, stuff);
4626 /* Don't care about the pre/postop attributes */
4627 mbuf_freem(nd->nd_mrep);
4628 return (nd->nd_repstat);
4632 * Do the NFSv4.1 Exchange ID.
4635 nfsrpc_exchangeid(struct nfsmount *nmp, struct nfsclclient *clp,
4636 struct nfssockreq *nrp, uint32_t exchflags, struct nfsclds **dspp,
4637 struct ucred *cred, NFSPROC_T *p)
4639 uint32_t *tl, v41flags;
4640 struct nfsrv_descript nfsd;
4641 struct nfsrv_descript *nd = &nfsd;
4642 struct nfsclds *dsp;
4643 struct timespec verstime;
4647 nfscl_reqstart(nd, NFSPROC_EXCHANGEID, nmp, NULL, 0, NULL, NULL, 0, 0);
4648 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_UNSIGNED);
4649 *tl++ = txdr_unsigned(nfsboottime.tv_sec); /* Client owner */
4650 *tl = txdr_unsigned(clp->nfsc_rev);
4651 (void) nfsm_strtom(nd, clp->nfsc_id, clp->nfsc_idlen);
4653 NFSM_BUILD(tl, uint32_t *, 3 * NFSX_UNSIGNED);
4654 *tl++ = txdr_unsigned(exchflags);
4655 *tl++ = txdr_unsigned(NFSV4EXCH_SP4NONE);
4657 /* Set the implementation id4 */
4658 *tl = txdr_unsigned(1);
4659 (void) nfsm_strtom(nd, "freebsd.org", strlen("freebsd.org"));
4660 (void) nfsm_strtom(nd, version, strlen(version));
4661 NFSM_BUILD(tl, uint32_t *, NFSX_V4TIME);
4662 verstime.tv_sec = 1293840000; /* Jan 1, 2011 */
4663 verstime.tv_nsec = 0;
4664 txdr_nfsv4time(&verstime, tl);
4665 nd->nd_flag |= ND_USEGSSNAME;
4666 error = newnfs_request(nd, nmp, NULL, nrp, NULL, p, cred,
4667 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4668 NFSCL_DEBUG(1, "exchangeid err=%d reps=%d\n", error,
4669 (int)nd->nd_repstat);
4672 if (nd->nd_repstat == 0) {
4673 NFSM_DISSECT(tl, uint32_t *, 6 * NFSX_UNSIGNED + NFSX_HYPER);
4674 len = fxdr_unsigned(int, *(tl + 7));
4675 if (len < 0 || len > NFSV4_OPAQUELIMIT) {
4676 error = NFSERR_BADXDR;
4679 dsp = malloc(sizeof(struct nfsclds) + len + 1, M_NFSCLDS,
4681 dsp->nfsclds_expire = NFSD_MONOSEC + clp->nfsc_renew;
4682 dsp->nfsclds_servownlen = len;
4683 dsp->nfsclds_sess.nfsess_clientid.lval[0] = *tl++;
4684 dsp->nfsclds_sess.nfsess_clientid.lval[1] = *tl++;
4685 dsp->nfsclds_sess.nfsess_sequenceid =
4686 fxdr_unsigned(uint32_t, *tl++);
4687 v41flags = fxdr_unsigned(uint32_t, *tl);
4688 if ((v41flags & NFSV4EXCH_USEPNFSMDS) != 0 &&
4689 NFSHASPNFSOPT(nmp)) {
4690 NFSCL_DEBUG(1, "set PNFS\n");
4692 nmp->nm_state |= NFSSTA_PNFS;
4694 dsp->nfsclds_flags |= NFSCLDS_MDS;
4696 if ((v41flags & NFSV4EXCH_USEPNFSDS) != 0)
4697 dsp->nfsclds_flags |= NFSCLDS_DS;
4699 nd->nd_repstat = nfsrv_mtostr(nd,
4700 dsp->nfsclds_serverown, len);
4701 if (nd->nd_repstat == 0) {
4702 mtx_init(&dsp->nfsclds_mtx, "nfsds", NULL, MTX_DEF);
4703 mtx_init(&dsp->nfsclds_sess.nfsess_mtx, "nfssession",
4705 nfscl_initsessionslots(&dsp->nfsclds_sess);
4708 free(dsp, M_NFSCLDS);
4710 error = nd->nd_repstat;
4712 mbuf_freem(nd->nd_mrep);
4717 * Do the NFSv4.1 Create Session.
4720 nfsrpc_createsession(struct nfsmount *nmp, struct nfsclsession *sep,
4721 struct nfssockreq *nrp, uint32_t sequenceid, int mds, struct ucred *cred,
4724 uint32_t crflags, maxval, *tl;
4725 struct nfsrv_descript nfsd;
4726 struct nfsrv_descript *nd = &nfsd;
4729 /* Make sure nm_rsize, nm_wsize is set. */
4730 if (nmp->nm_rsize > NFS_MAXBSIZE || nmp->nm_rsize == 0)
4731 nmp->nm_rsize = NFS_MAXBSIZE;
4732 if (nmp->nm_wsize > NFS_MAXBSIZE || nmp->nm_wsize == 0)
4733 nmp->nm_wsize = NFS_MAXBSIZE;
4734 nfscl_reqstart(nd, NFSPROC_CREATESESSION, nmp, NULL, 0, NULL, NULL, 0,
4736 NFSM_BUILD(tl, uint32_t *, 4 * NFSX_UNSIGNED);
4737 *tl++ = sep->nfsess_clientid.lval[0];
4738 *tl++ = sep->nfsess_clientid.lval[1];
4739 *tl++ = txdr_unsigned(sequenceid);
4740 crflags = (NFSMNT_RDONLY(nmp->nm_mountp) ? 0 : NFSV4CRSESS_PERSIST);
4741 if (nfscl_enablecallb != 0 && nfs_numnfscbd > 0 && mds != 0)
4742 crflags |= NFSV4CRSESS_CONNBACKCHAN;
4743 *tl = txdr_unsigned(crflags);
4745 /* Fill in fore channel attributes. */
4746 NFSM_BUILD(tl, uint32_t *, 7 * NFSX_UNSIGNED);
4747 *tl++ = 0; /* Header pad size */
4748 *tl++ = txdr_unsigned(nmp->nm_wsize + NFS_MAXXDR);/* Max request size */
4749 *tl++ = txdr_unsigned(nmp->nm_rsize + NFS_MAXXDR);/* Max reply size */
4750 *tl++ = txdr_unsigned(4096); /* Max response size cached */
4751 *tl++ = txdr_unsigned(20); /* Max operations */
4752 *tl++ = txdr_unsigned(64); /* Max slots */
4753 *tl = 0; /* No rdma ird */
4755 /* Fill in back channel attributes. */
4756 NFSM_BUILD(tl, uint32_t *, 7 * NFSX_UNSIGNED);
4757 *tl++ = 0; /* Header pad size */
4758 *tl++ = txdr_unsigned(10000); /* Max request size */
4759 *tl++ = txdr_unsigned(10000); /* Max response size */
4760 *tl++ = txdr_unsigned(4096); /* Max response size cached */
4761 *tl++ = txdr_unsigned(4); /* Max operations */
4762 *tl++ = txdr_unsigned(NFSV4_CBSLOTS); /* Max slots */
4763 *tl = 0; /* No rdma ird */
4765 NFSM_BUILD(tl, uint32_t *, 8 * NFSX_UNSIGNED);
4766 *tl++ = txdr_unsigned(NFS_CALLBCKPROG); /* Call back prog # */
4768 /* Allow AUTH_SYS callbacks as uid, gid == 0. */
4769 *tl++ = txdr_unsigned(1); /* Auth_sys only */
4770 *tl++ = txdr_unsigned(AUTH_SYS); /* AUTH_SYS type */
4771 *tl++ = txdr_unsigned(nfsboottime.tv_sec); /* time stamp */
4772 *tl++ = 0; /* Null machine name */
4773 *tl++ = 0; /* Uid == 0 */
4774 *tl++ = 0; /* Gid == 0 */
4775 *tl = 0; /* No additional gids */
4776 nd->nd_flag |= ND_USEGSSNAME;
4777 error = newnfs_request(nd, nmp, NULL, nrp, NULL, p, cred, NFS_PROG,
4778 NFS_VER4, NULL, 1, NULL, NULL);
4781 if (nd->nd_repstat == 0) {
4782 NFSM_DISSECT(tl, uint32_t *, NFSX_V4SESSIONID +
4784 bcopy(tl, sep->nfsess_sessionid, NFSX_V4SESSIONID);
4785 tl += NFSX_V4SESSIONID / NFSX_UNSIGNED;
4786 sep->nfsess_sequenceid = fxdr_unsigned(uint32_t, *tl++);
4787 crflags = fxdr_unsigned(uint32_t, *tl);
4788 if ((crflags & NFSV4CRSESS_PERSIST) != 0 && mds != 0) {
4790 nmp->nm_state |= NFSSTA_SESSPERSIST;
4794 /* Get the fore channel slot count. */
4795 NFSM_DISSECT(tl, uint32_t *, 7 * NFSX_UNSIGNED);
4796 tl++; /* Skip the header pad size. */
4798 /* Make sure nm_wsize is small enough. */
4799 maxval = fxdr_unsigned(uint32_t, *tl++);
4800 while (maxval < nmp->nm_wsize + NFS_MAXXDR) {
4801 if (nmp->nm_wsize > 8096)
4807 /* Make sure nm_rsize is small enough. */
4808 maxval = fxdr_unsigned(uint32_t, *tl++);
4809 while (maxval < nmp->nm_rsize + NFS_MAXXDR) {
4810 if (nmp->nm_rsize > 8096)
4816 sep->nfsess_maxcache = fxdr_unsigned(int, *tl++);
4818 sep->nfsess_foreslots = fxdr_unsigned(uint16_t, *tl++);
4819 NFSCL_DEBUG(4, "fore slots=%d\n", (int)sep->nfsess_foreslots);
4820 irdcnt = fxdr_unsigned(int, *tl);
4822 NFSM_DISSECT(tl, uint32_t *, irdcnt * NFSX_UNSIGNED);
4824 /* and the back channel slot count. */
4825 NFSM_DISSECT(tl, uint32_t *, 7 * NFSX_UNSIGNED);
4827 sep->nfsess_backslots = fxdr_unsigned(uint16_t, *tl);
4828 NFSCL_DEBUG(4, "back slots=%d\n", (int)sep->nfsess_backslots);
4830 error = nd->nd_repstat;
4832 mbuf_freem(nd->nd_mrep);
4837 * Do the NFSv4.1 Destroy Session.
4840 nfsrpc_destroysession(struct nfsmount *nmp, struct nfsclclient *clp,
4841 struct ucred *cred, NFSPROC_T *p)
4844 struct nfsrv_descript nfsd;
4845 struct nfsrv_descript *nd = &nfsd;
4847 struct nfsclsession *tsep;
4849 nfscl_reqstart(nd, NFSPROC_DESTROYSESSION, nmp, NULL, 0, NULL, NULL, 0,
4851 NFSM_BUILD(tl, uint32_t *, NFSX_V4SESSIONID);
4852 tsep = nfsmnt_mdssession(nmp);
4853 bcopy(tsep->nfsess_sessionid, tl, NFSX_V4SESSIONID);
4854 nd->nd_flag |= ND_USEGSSNAME;
4855 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4856 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4859 error = nd->nd_repstat;
4860 mbuf_freem(nd->nd_mrep);
4865 * Do the NFSv4.1 Destroy Client.
4868 nfsrpc_destroyclient(struct nfsmount *nmp, struct nfsclclient *clp,
4869 struct ucred *cred, NFSPROC_T *p)
4872 struct nfsrv_descript nfsd;
4873 struct nfsrv_descript *nd = &nfsd;
4875 struct nfsclsession *tsep;
4877 nfscl_reqstart(nd, NFSPROC_DESTROYCLIENT, nmp, NULL, 0, NULL, NULL, 0,
4879 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_UNSIGNED);
4880 tsep = nfsmnt_mdssession(nmp);
4881 *tl++ = tsep->nfsess_clientid.lval[0];
4882 *tl = tsep->nfsess_clientid.lval[1];
4883 nd->nd_flag |= ND_USEGSSNAME;
4884 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4885 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4888 error = nd->nd_repstat;
4889 mbuf_freem(nd->nd_mrep);
4894 * Do the NFSv4.1 LayoutGet.
4897 nfsrpc_layoutget(struct nfsmount *nmp, uint8_t *fhp, int fhlen, int iomode,
4898 uint64_t offset, uint64_t len, uint64_t minlen, int layouttype,
4899 int layoutlen, nfsv4stateid_t *stateidp, int *retonclosep,
4900 struct nfsclflayouthead *flhp, struct ucred *cred, NFSPROC_T *p,
4903 struct nfsrv_descript nfsd, *nd = &nfsd;
4906 nfscl_reqstart(nd, NFSPROC_LAYOUTGET, nmp, fhp, fhlen, NULL, NULL, 0,
4908 nfsrv_setuplayoutget(nd, iomode, offset, len, minlen, stateidp,
4909 layouttype, layoutlen, 0);
4910 nd->nd_flag |= ND_USEGSSNAME;
4911 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4912 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4913 NFSCL_DEBUG(4, "layget err=%d st=%d\n", error, nd->nd_repstat);
4916 if (nd->nd_repstat == 0)
4917 error = nfsrv_parselayoutget(nd, stateidp, retonclosep, flhp);
4918 if (error == 0 && nd->nd_repstat != 0)
4919 error = nd->nd_repstat;
4920 mbuf_freem(nd->nd_mrep);
4925 * Do the NFSv4.1 Get Device Info.
4928 nfsrpc_getdeviceinfo(struct nfsmount *nmp, uint8_t *deviceid, int layouttype,
4929 uint32_t *notifybitsp, struct nfscldevinfo **ndip, struct ucred *cred,
4932 uint32_t cnt, *tl, vers, minorvers;
4933 struct nfsrv_descript nfsd;
4934 struct nfsrv_descript *nd = &nfsd;
4935 struct sockaddr_in sin, ssin;
4936 struct sockaddr_in6 sin6, ssin6;
4937 struct nfsclds *dsp = NULL, **dspp, **gotdspp;
4938 struct nfscldevinfo *ndi;
4939 int addrcnt = 0, bitcnt, error, gotvers, i, isudp, j, stripecnt;
4940 uint8_t stripeindex;
4941 sa_family_t af, safilled;
4946 nfscl_reqstart(nd, NFSPROC_GETDEVICEINFO, nmp, NULL, 0, NULL, NULL, 0,
4948 NFSM_BUILD(tl, uint32_t *, NFSX_V4DEVICEID + 3 * NFSX_UNSIGNED);
4949 NFSBCOPY(deviceid, tl, NFSX_V4DEVICEID);
4950 tl += (NFSX_V4DEVICEID / NFSX_UNSIGNED);
4951 *tl++ = txdr_unsigned(layouttype);
4952 *tl++ = txdr_unsigned(100000);
4953 if (notifybitsp != NULL && *notifybitsp != 0) {
4954 *tl = txdr_unsigned(1); /* One word of bits. */
4955 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
4956 *tl = txdr_unsigned(*notifybitsp);
4958 *tl = txdr_unsigned(0);
4959 nd->nd_flag |= ND_USEGSSNAME;
4960 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4961 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4964 if (nd->nd_repstat == 0) {
4965 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
4966 if (layouttype != fxdr_unsigned(int, *tl))
4967 printf("EEK! devinfo layout type not same!\n");
4968 if (layouttype == NFSLAYOUT_NFSV4_1_FILES) {
4969 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
4970 stripecnt = fxdr_unsigned(int, *tl);
4971 NFSCL_DEBUG(4, "stripecnt=%d\n", stripecnt);
4972 if (stripecnt < 1 || stripecnt > 4096) {
4973 printf("pNFS File layout devinfo stripecnt %d:"
4974 " out of range\n", stripecnt);
4975 error = NFSERR_BADXDR;
4978 NFSM_DISSECT(tl, uint32_t *, (stripecnt + 1) *
4980 addrcnt = fxdr_unsigned(int, *(tl + stripecnt));
4981 NFSCL_DEBUG(4, "addrcnt=%d\n", addrcnt);
4982 if (addrcnt < 1 || addrcnt > 128) {
4983 printf("NFS devinfo addrcnt %d: out of range\n",
4985 error = NFSERR_BADXDR;
4990 * Now we know how many stripe indices and addresses, so
4991 * we can allocate the structure the correct size.
4993 i = (stripecnt * sizeof(uint8_t)) /
4994 sizeof(struct nfsclds *) + 1;
4995 NFSCL_DEBUG(4, "stripeindices=%d\n", i);
4996 ndi = malloc(sizeof(*ndi) + (addrcnt + i) *
4997 sizeof(struct nfsclds *), M_NFSDEVINFO, M_WAITOK |
4999 NFSBCOPY(deviceid, ndi->nfsdi_deviceid,
5001 ndi->nfsdi_refcnt = 0;
5002 ndi->nfsdi_flags = NFSDI_FILELAYOUT;
5003 ndi->nfsdi_stripecnt = stripecnt;
5004 ndi->nfsdi_addrcnt = addrcnt;
5005 /* Fill in the stripe indices. */
5006 for (i = 0; i < stripecnt; i++) {
5007 stripeindex = fxdr_unsigned(uint8_t, *tl++);
5008 NFSCL_DEBUG(4, "stripeind=%d\n", stripeindex);
5009 if (stripeindex >= addrcnt) {
5010 printf("pNFS File Layout devinfo"
5011 " stripeindex %d: too big\n",
5013 error = NFSERR_BADXDR;
5016 nfsfldi_setstripeindex(ndi, i, stripeindex);
5018 } else if (layouttype == NFSLAYOUT_FLEXFILE) {
5019 /* For Flex File, we only get one address list. */
5020 ndi = malloc(sizeof(*ndi) + sizeof(struct nfsclds *),
5021 M_NFSDEVINFO, M_WAITOK | M_ZERO);
5022 NFSBCOPY(deviceid, ndi->nfsdi_deviceid,
5024 ndi->nfsdi_refcnt = 0;
5025 ndi->nfsdi_flags = NFSDI_FLEXFILE;
5026 addrcnt = ndi->nfsdi_addrcnt = 1;
5029 /* Now, dissect the server address(es). */
5030 safilled = AF_UNSPEC;
5031 for (i = 0; i < addrcnt; i++) {
5032 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
5033 cnt = fxdr_unsigned(uint32_t, *tl);
5035 printf("NFS devinfo 0 len addrlist\n");
5036 error = NFSERR_BADXDR;
5039 dspp = nfsfldi_addr(ndi, i);
5040 safilled = AF_UNSPEC;
5041 for (j = 0; j < cnt; j++) {
5042 error = nfsv4_getipaddr(nd, &sin, &sin6, &af,
5044 if (error != 0 && error != EPERM) {
5045 error = NFSERR_BADXDR;
5048 if (error == 0 && isudp == 0) {
5051 * - Same address family.
5052 * Save the address and dspp, so that
5053 * the connection can be done after
5054 * parsing is complete.
5056 if (safilled == AF_UNSPEC ||
5057 (af == nmp->nm_nam->sa_family &&
5058 safilled != nmp->nm_nam->sa_family)
5071 gotvers = NFS_VER4; /* Always NFSv4 for File Layout. */
5072 /* For Flex File, we will take one of the versions to use. */
5073 if (layouttype == NFSLAYOUT_FLEXFILE) {
5074 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
5075 j = fxdr_unsigned(int, *tl);
5076 if (j < 1 || j > NFSDEV_MAXVERS) {
5077 printf("pNFS: too many versions\n");
5078 error = NFSERR_BADXDR;
5082 for (i = 0; i < j; i++) {
5083 NFSM_DISSECT(tl, uint32_t *, 5 * NFSX_UNSIGNED);
5084 vers = fxdr_unsigned(uint32_t, *tl++);
5085 minorvers = fxdr_unsigned(uint32_t, *tl++);
5086 if ((vers == NFS_VER4 && minorvers ==
5087 NFSV41_MINORVERSION) || (vers == NFS_VER3 &&
5090 /* We'll take this one. */
5091 ndi->nfsdi_versindex = i;
5092 ndi->nfsdi_vers = vers;
5093 ndi->nfsdi_minorvers = minorvers;
5094 ndi->nfsdi_rsize = fxdr_unsigned(
5096 ndi->nfsdi_wsize = fxdr_unsigned(
5098 if (*tl == newnfs_true)
5103 ~NFSDI_TIGHTCOUPLED;
5107 printf("pNFS: no NFSv3 or NFSv4.1\n");
5108 error = NFSERR_BADXDR;
5113 /* And the notify bits. */
5114 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
5115 bitcnt = fxdr_unsigned(int, *tl);
5117 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
5118 if (notifybitsp != NULL)
5120 fxdr_unsigned(uint32_t, *tl);
5122 if (safilled != AF_UNSPEC) {
5123 KASSERT(ndi != NULL, ("ndi is NULL"));
5129 * Now we can do a TCP connection for the correct
5130 * NFS version and IP address.
5132 error = nfsrpc_fillsa(nmp, &ssin, &ssin6, safilled,
5136 KASSERT(gotdspp != NULL, ("gotdspp is NULL"));
5140 if (nd->nd_repstat != 0 && error == 0)
5141 error = nd->nd_repstat;
5143 if (error != 0 && ndi != NULL)
5144 nfscl_freedevinfo(ndi);
5145 mbuf_freem(nd->nd_mrep);
5150 * Do the NFSv4.1 LayoutCommit.
5153 nfsrpc_layoutcommit(struct nfsmount *nmp, uint8_t *fh, int fhlen, int reclaim,
5154 uint64_t off, uint64_t len, uint64_t lastbyte, nfsv4stateid_t *stateidp,
5155 int layouttype, struct ucred *cred, NFSPROC_T *p, void *stuff)
5158 struct nfsrv_descript nfsd, *nd = &nfsd;
5161 nfscl_reqstart(nd, NFSPROC_LAYOUTCOMMIT, nmp, fh, fhlen, NULL, NULL,
5163 NFSM_BUILD(tl, uint32_t *, 5 * NFSX_UNSIGNED + 3 * NFSX_HYPER +
5165 txdr_hyper(off, tl);
5167 txdr_hyper(len, tl);
5170 *tl++ = newnfs_true;
5172 *tl++ = newnfs_false;
5173 *tl++ = txdr_unsigned(stateidp->seqid);
5174 *tl++ = stateidp->other[0];
5175 *tl++ = stateidp->other[1];
5176 *tl++ = stateidp->other[2];
5177 *tl++ = newnfs_true;
5180 else if (lastbyte >= (off + len))
5181 lastbyte = off + len - 1;
5182 txdr_hyper(lastbyte, tl);
5184 *tl++ = newnfs_false;
5185 *tl++ = txdr_unsigned(layouttype);
5186 /* All supported layouts are 0 length. */
5187 *tl = txdr_unsigned(0);
5188 nd->nd_flag |= ND_USEGSSNAME;
5189 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
5190 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
5193 error = nd->nd_repstat;
5194 mbuf_freem(nd->nd_mrep);
5199 * Do the NFSv4.1 LayoutReturn.
5202 nfsrpc_layoutreturn(struct nfsmount *nmp, uint8_t *fh, int fhlen, int reclaim,
5203 int layouttype, uint32_t iomode, int layoutreturn, uint64_t offset,
5204 uint64_t len, nfsv4stateid_t *stateidp, struct ucred *cred, NFSPROC_T *p,
5208 struct nfsrv_descript nfsd, *nd = &nfsd;
5211 nfscl_reqstart(nd, NFSPROC_LAYOUTRETURN, nmp, fh, fhlen, NULL, NULL,
5213 NFSM_BUILD(tl, uint32_t *, 4 * NFSX_UNSIGNED);
5215 *tl++ = newnfs_true;
5217 *tl++ = newnfs_false;
5218 *tl++ = txdr_unsigned(layouttype);
5219 *tl++ = txdr_unsigned(iomode);
5220 *tl = txdr_unsigned(layoutreturn);
5221 if (layoutreturn == NFSLAYOUTRETURN_FILE) {
5222 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_HYPER + NFSX_STATEID +
5224 txdr_hyper(offset, tl);
5226 txdr_hyper(len, tl);
5228 NFSCL_DEBUG(4, "layoutret stseq=%d\n", (int)stateidp->seqid);
5229 *tl++ = txdr_unsigned(stateidp->seqid);
5230 *tl++ = stateidp->other[0];
5231 *tl++ = stateidp->other[1];
5232 *tl++ = stateidp->other[2];
5233 if (layouttype == NFSLAYOUT_NFSV4_1_FILES)
5234 *tl = txdr_unsigned(0);
5235 else if (layouttype == NFSLAYOUT_FLEXFILE) {
5236 *tl = txdr_unsigned(2 * NFSX_UNSIGNED);
5237 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_UNSIGNED);
5238 /* No ioerrs or stats yet. */
5243 nd->nd_flag |= ND_USEGSSNAME;
5244 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
5245 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
5248 if (nd->nd_repstat == 0) {
5249 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
5251 NFSM_DISSECT(tl, uint32_t *, NFSX_STATEID);
5252 stateidp->seqid = fxdr_unsigned(uint32_t, *tl++);
5253 stateidp->other[0] = *tl++;
5254 stateidp->other[1] = *tl++;
5255 stateidp->other[2] = *tl;
5258 error = nd->nd_repstat;
5260 mbuf_freem(nd->nd_mrep);
5265 * Acquire a layout and devinfo, if possible. The caller must have acquired
5266 * a reference count on the nfsclclient structure before calling this.
5267 * Return the layout in lypp with a reference count on it, if successful.
5270 nfsrpc_getlayout(struct nfsmount *nmp, vnode_t vp, struct nfsfh *nfhp,
5271 int iomode, uint32_t *notifybitsp, nfsv4stateid_t *stateidp, uint64_t off,
5272 struct nfscllayout **lypp, struct ucred *cred, NFSPROC_T *p)
5274 struct nfscllayout *lyp;
5275 struct nfsclflayout *flp;
5276 struct nfsclflayouthead flh;
5277 int error = 0, islocked, layoutlen, layouttype, recalled, retonclose;
5278 nfsv4stateid_t stateid;
5279 struct nfsclsession *tsep;
5282 if (NFSHASFLEXFILE(nmp))
5283 layouttype = NFSLAYOUT_FLEXFILE;
5285 layouttype = NFSLAYOUT_NFSV4_1_FILES;
5287 * If lyp is returned non-NULL, there will be a refcnt (shared lock)
5288 * on it, iff flp != NULL or a lock (exclusive lock) on it iff
5291 lyp = nfscl_getlayout(nmp->nm_clp, nfhp->nfh_fh, nfhp->nfh_len,
5292 off, &flp, &recalled);
5294 if (lyp == NULL || flp == NULL) {
5298 tsep = nfsmnt_mdssession(nmp);
5299 layoutlen = tsep->nfsess_maxcache -
5300 (NFSX_STATEID + 3 * NFSX_UNSIGNED);
5303 stateid.other[0] = stateidp->other[0];
5304 stateid.other[1] = stateidp->other[1];
5305 stateid.other[2] = stateidp->other[2];
5306 error = nfsrpc_layoutget(nmp, nfhp->nfh_fh,
5307 nfhp->nfh_len, iomode, (uint64_t)0, UINT64_MAX,
5308 (uint64_t)0, layouttype, layoutlen, &stateid,
5309 &retonclose, &flh, cred, p, NULL);
5312 stateid.seqid = lyp->nfsly_stateid.seqid;
5313 stateid.other[0] = lyp->nfsly_stateid.other[0];
5314 stateid.other[1] = lyp->nfsly_stateid.other[1];
5315 stateid.other[2] = lyp->nfsly_stateid.other[2];
5316 error = nfsrpc_layoutget(nmp, nfhp->nfh_fh,
5317 nfhp->nfh_len, iomode, off, UINT64_MAX,
5318 (uint64_t)0, layouttype, layoutlen, &stateid,
5319 &retonclose, &flh, cred, p, NULL);
5321 error = nfsrpc_layoutgetres(nmp, vp, nfhp->nfh_fh,
5322 nfhp->nfh_len, &stateid, retonclose, notifybitsp, &lyp,
5323 &flh, layouttype, error, NULL, cred, p);
5326 else if (islocked != 0)
5327 nfscl_rellayout(lyp, 1);
5334 * Do a TCP connection plus exchange id and create session.
5335 * If successful, a "struct nfsclds" is linked into the list for the
5336 * mount point and a pointer to it is returned.
5339 nfsrpc_fillsa(struct nfsmount *nmp, struct sockaddr_in *sin,
5340 struct sockaddr_in6 *sin6, sa_family_t af, int vers, struct nfsclds **dspp,
5343 struct sockaddr_in *msad, *sad;
5344 struct sockaddr_in6 *msad6, *sad6;
5345 struct nfsclclient *clp;
5346 struct nfssockreq *nrp;
5347 struct nfsclds *dsp, *tdsp;
5349 enum nfsclds_state retv;
5350 uint32_t sequenceid;
5352 KASSERT(nmp->nm_sockreq.nr_cred != NULL,
5353 ("nfsrpc_fillsa: NULL nr_cred"));
5359 if (af == AF_INET) {
5362 * Check to see if we already have a session for this
5363 * address that is usable for a DS.
5364 * Note that the MDS's address is in a different place
5365 * than the sessions already acquired for DS's.
5367 msad = (struct sockaddr_in *)nmp->nm_sockreq.nr_nam;
5368 tdsp = TAILQ_FIRST(&nmp->nm_sess);
5369 while (tdsp != NULL) {
5370 if (msad != NULL && msad->sin_family == AF_INET &&
5371 sin->sin_addr.s_addr == msad->sin_addr.s_addr &&
5372 sin->sin_port == msad->sin_port &&
5373 (tdsp->nfsclds_flags & NFSCLDS_DS) != 0 &&
5374 tdsp->nfsclds_sess.nfsess_defunct == 0) {
5377 NFSCL_DEBUG(4, "fnd same addr\n");
5380 tdsp = TAILQ_NEXT(tdsp, nfsclds_list);
5381 if (tdsp != NULL && tdsp->nfsclds_sockp != NULL)
5382 msad = (struct sockaddr_in *)
5383 tdsp->nfsclds_sockp->nr_nam;
5389 /* No IP address match, so look for new/trunked one. */
5390 sad = malloc(sizeof(*sad), M_SONAME, M_WAITOK | M_ZERO);
5391 sad->sin_len = sizeof(*sad);
5392 sad->sin_family = AF_INET;
5393 sad->sin_port = sin->sin_port;
5394 sad->sin_addr.s_addr = sin->sin_addr.s_addr;
5395 nrp = malloc(sizeof(*nrp), M_NFSSOCKREQ, M_WAITOK | M_ZERO);
5396 nrp->nr_nam = (struct sockaddr *)sad;
5397 } else if (af == AF_INET6) {
5400 * Check to see if we already have a session for this
5401 * address that is usable for a DS.
5402 * Note that the MDS's address is in a different place
5403 * than the sessions already acquired for DS's.
5405 msad6 = (struct sockaddr_in6 *)nmp->nm_sockreq.nr_nam;
5406 tdsp = TAILQ_FIRST(&nmp->nm_sess);
5407 while (tdsp != NULL) {
5408 if (msad6 != NULL && msad6->sin6_family == AF_INET6 &&
5409 IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr,
5410 &msad6->sin6_addr) &&
5411 sin6->sin6_port == msad6->sin6_port &&
5412 (tdsp->nfsclds_flags & NFSCLDS_DS) != 0 &&
5413 tdsp->nfsclds_sess.nfsess_defunct == 0) {
5418 tdsp = TAILQ_NEXT(tdsp, nfsclds_list);
5419 if (tdsp != NULL && tdsp->nfsclds_sockp != NULL)
5420 msad6 = (struct sockaddr_in6 *)
5421 tdsp->nfsclds_sockp->nr_nam;
5427 /* No IP address match, so look for new/trunked one. */
5428 sad6 = malloc(sizeof(*sad6), M_SONAME, M_WAITOK | M_ZERO);
5429 sad6->sin6_len = sizeof(*sad6);
5430 sad6->sin6_family = AF_INET6;
5431 sad6->sin6_port = sin6->sin6_port;
5432 NFSBCOPY(&sin6->sin6_addr, &sad6->sin6_addr,
5433 sizeof(struct in6_addr));
5434 nrp = malloc(sizeof(*nrp), M_NFSSOCKREQ, M_WAITOK | M_ZERO);
5435 nrp->nr_nam = (struct sockaddr *)sad6;
5439 nrp->nr_sotype = SOCK_STREAM;
5440 mtx_init(&nrp->nr_mtx, "nfssock", NULL, MTX_DEF);
5441 nrp->nr_prog = NFS_PROG;
5442 nrp->nr_vers = vers;
5445 * Use the credentials that were used for the mount, which are
5446 * in nmp->nm_sockreq.nr_cred for newnfs_connect() etc.
5447 * Ref. counting the credentials with crhold() is probably not
5448 * necessary, since nm_sockreq.nr_cred won't be crfree()'d until
5449 * unmount, but I did it anyhow.
5451 nrp->nr_cred = crhold(nmp->nm_sockreq.nr_cred);
5452 error = newnfs_connect(nmp, nrp, NULL, p, 0);
5453 NFSCL_DEBUG(3, "DS connect=%d\n", error);
5456 /* Now, do the exchangeid and create session. */
5458 if (vers == NFS_VER4) {
5459 error = nfsrpc_exchangeid(nmp, clp, nrp,
5460 NFSV4EXCH_USEPNFSDS, &dsp, nrp->nr_cred, p);
5461 NFSCL_DEBUG(3, "DS exchangeid=%d\n", error);
5463 newnfs_disconnect(nrp);
5465 dsp = malloc(sizeof(struct nfsclds), M_NFSCLDS,
5467 dsp->nfsclds_flags |= NFSCLDS_DS;
5468 dsp->nfsclds_expire = INT32_MAX; /* No renews needed. */
5469 mtx_init(&dsp->nfsclds_mtx, "nfsds", NULL, MTX_DEF);
5470 mtx_init(&dsp->nfsclds_sess.nfsess_mtx, "nfssession",
5475 dsp->nfsclds_sockp = nrp;
5476 if (vers == NFS_VER4) {
5478 retv = nfscl_getsameserver(nmp, dsp, &tdsp);
5479 NFSCL_DEBUG(3, "getsame ret=%d\n", retv);
5480 if (retv == NFSDSP_USETHISSESSION) {
5483 * If there is already a session for this
5486 (void)newnfs_disconnect(nrp);
5487 nfscl_freenfsclds(dsp);
5491 if (retv == NFSDSP_SEQTHISSESSION)
5493 tdsp->nfsclds_sess.nfsess_sequenceid;
5496 dsp->nfsclds_sess.nfsess_sequenceid;
5498 error = nfsrpc_createsession(nmp, &dsp->nfsclds_sess,
5499 nrp, sequenceid, 0, nrp->nr_cred, p);
5500 NFSCL_DEBUG(3, "DS createsess=%d\n", error);
5503 NFSFREECRED(nrp->nr_cred);
5504 NFSFREEMUTEX(&nrp->nr_mtx);
5505 free(nrp->nr_nam, M_SONAME);
5506 free(nrp, M_NFSSOCKREQ);
5509 NFSCL_DEBUG(3, "add DS session\n");
5511 * Put it at the end of the list. That way the list
5512 * is ordered by when the entry was added. This matters
5513 * since the one done first is the one that should be
5514 * used for sequencid'ing any subsequent create sessions.
5517 TAILQ_INSERT_TAIL(&nmp->nm_sess, dsp, nfsclds_list);
5520 } else if (dsp != NULL) {
5521 newnfs_disconnect(nrp);
5522 nfscl_freenfsclds(dsp);
5528 * Do the NFSv4.1 Reclaim Complete.
5531 nfsrpc_reclaimcomplete(struct nfsmount *nmp, struct ucred *cred, NFSPROC_T *p)
5534 struct nfsrv_descript nfsd;
5535 struct nfsrv_descript *nd = &nfsd;
5538 nfscl_reqstart(nd, NFSPROC_RECLAIMCOMPL, nmp, NULL, 0, NULL, NULL, 0,
5540 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
5542 nd->nd_flag |= ND_USEGSSNAME;
5543 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
5544 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
5547 error = nd->nd_repstat;
5548 mbuf_freem(nd->nd_mrep);
5553 * Initialize the slot tables for a session.
5556 nfscl_initsessionslots(struct nfsclsession *sep)
5560 for (i = 0; i < NFSV4_CBSLOTS; i++) {
5561 if (sep->nfsess_cbslots[i].nfssl_reply != NULL)
5562 m_freem(sep->nfsess_cbslots[i].nfssl_reply);
5563 NFSBZERO(&sep->nfsess_cbslots[i], sizeof(struct nfsslot));
5565 for (i = 0; i < 64; i++)
5566 sep->nfsess_slotseq[i] = 0;
5567 sep->nfsess_slots = 0;
5571 * Called to try and do an I/O operation via an NFSv4.1 Data Server (DS).
5574 nfscl_doiods(vnode_t vp, struct uio *uiop, int *iomode, int *must_commit,
5575 uint32_t rwaccess, int docommit, struct ucred *cred, NFSPROC_T *p)
5577 struct nfsnode *np = VTONFS(vp);
5578 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
5579 struct nfscllayout *layp;
5580 struct nfscldevinfo *dip;
5581 struct nfsclflayout *rflp;
5583 struct nfsclwritedsdorpc *drpc, *tdrpc;
5584 nfsv4stateid_t stateid;
5585 struct ucred *newcred;
5586 uint64_t lastbyte, len, off, oresid, xfer;
5587 int eof, error, firstmirror, i, iolaymode, mirrorcnt, recalled, timo;
5595 if (!NFSHASPNFS(nmp) || nfscl_enablecallb == 0 || nfs_numnfscbd == 0 ||
5596 (np->n_flag & NNOLAYOUT) != 0)
5598 /* Now, get a reference cnt on the clientid for this mount. */
5599 if (nfscl_getref(nmp) == 0)
5602 /* Find an appropriate stateid. */
5603 newcred = NFSNEWCRED(cred);
5604 error = nfscl_getstateid(vp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len,
5605 rwaccess, 1, newcred, p, &stateid, &lckp);
5607 NFSFREECRED(newcred);
5611 /* Search for a layout for this file. */
5612 off = uiop->uio_offset;
5613 layp = nfscl_getlayout(nmp->nm_clp, np->n_fhp->nfh_fh,
5614 np->n_fhp->nfh_len, off, &rflp, &recalled);
5615 if (layp == NULL || rflp == NULL) {
5616 if (recalled != 0) {
5617 NFSFREECRED(newcred);
5622 nfscl_rellayout(layp, (rflp == NULL) ? 1 : 0);
5625 /* Try and get a Layout, if it is supported. */
5626 if (rwaccess == NFSV4OPEN_ACCESSWRITE ||
5627 (np->n_flag & NWRITEOPENED) != 0)
5628 iolaymode = NFSLAYOUTIOMODE_RW;
5630 iolaymode = NFSLAYOUTIOMODE_READ;
5631 error = nfsrpc_getlayout(nmp, vp, np->n_fhp, iolaymode,
5632 NULL, &stateid, off, &layp, newcred, p);
5635 np->n_flag |= NNOLAYOUT;
5638 nfscl_lockderef(lckp);
5639 NFSFREECRED(newcred);
5641 nfscl_rellayout(layp, 0);
5648 * Loop around finding a layout that works for the first part of
5649 * this I/O operation, and then call the function that actually
5653 len = (uint64_t)uiop->uio_resid;
5654 while (len > 0 && error == 0 && eof == 0) {
5655 off = uiop->uio_offset;
5656 error = nfscl_findlayoutforio(layp, off, rwaccess, &rflp);
5658 oresid = xfer = (uint64_t)uiop->uio_resid;
5659 if (xfer > (rflp->nfsfl_end - rflp->nfsfl_off))
5660 xfer = rflp->nfsfl_end - rflp->nfsfl_off;
5662 * For Flex File layout with mirrored DSs, select one
5663 * of them at random for reads. For writes and commits,
5670 if ((layp->nfsly_flags & NFSLY_FLEXFILE) != 0 &&
5671 (mirrorcnt = rflp->nfsfl_mirrorcnt) > 1) {
5672 if (rwaccess == NFSV4OPEN_ACCESSREAD) {
5673 firstmirror = arc4random() % mirrorcnt;
5674 mirrorcnt = firstmirror + 1;
5676 if (docommit == 0) {
5678 * Save values, so uiop can be
5679 * rolled back upon a write
5682 offs = uiop->uio_offset;
5683 resid = uiop->uio_resid;
5685 uiop->uio_iov->iov_base;
5686 iovlen = uiop->uio_iov->iov_len;
5687 m = nfsm_uiombuflist(uiop, len,
5690 tdrpc = drpc = malloc(sizeof(*drpc) *
5691 (mirrorcnt - 1), M_TEMP, M_WAITOK |
5695 for (i = firstmirror; i < mirrorcnt && error == 0; i++){
5696 if ((layp->nfsly_flags & NFSLY_FLEXFILE) != 0)
5697 dev = rflp->nfsfl_ffm[i].dev;
5699 dev = rflp->nfsfl_dev;
5700 dip = nfscl_getdevinfo(nmp->nm_clp, dev,
5703 if ((rflp->nfsfl_flags & NFSFL_FLEXFILE)
5705 error = nfscl_dofflayoutio(vp,
5706 uiop, iomode, must_commit,
5707 &eof, &stateid, rwaccess,
5708 dip, layp, rflp, off, xfer,
5709 i, docommit, m, tdrpc,
5712 error = nfscl_doflayoutio(vp,
5713 uiop, iomode, must_commit,
5714 &eof, &stateid, rwaccess,
5715 dip, layp, rflp, off, xfer,
5716 docommit, newcred, p);
5717 nfscl_reldevinfo(dip);
5725 timo = hz / 50; /* Wait for 20msec. */
5728 for (i = firstmirror; i < mirrorcnt - 1 &&
5729 tdrpc != NULL; i++, tdrpc++) {
5731 * For the unused drpc entries, both inprog and
5732 * err == 0, so this loop won't break.
5734 while (tdrpc->inprog != 0 && tdrpc->done == 0)
5735 tsleep(&tdrpc->tsk, PVFS, "clrpcio",
5737 if (error == 0 && tdrpc->err != 0)
5742 if (mirrorcnt > 1 && rwaccess ==
5743 NFSV4OPEN_ACCESSWRITE && docommit == 0) {
5745 layp->nfsly_flags |= NFSLY_WRITTEN;
5748 lastbyte = off + xfer - 1;
5750 if (lastbyte > layp->nfsly_lastbyte)
5751 layp->nfsly_lastbyte = lastbyte;
5753 } else if (error == NFSERR_OPENMODE &&
5754 rwaccess == NFSV4OPEN_ACCESSREAD) {
5756 nmp->nm_state |= NFSSTA_OPENMODE;
5761 len -= (oresid - (uint64_t)uiop->uio_resid);
5762 else if (mirrorcnt > 1 && rwaccess ==
5763 NFSV4OPEN_ACCESSWRITE && docommit == 0) {
5765 * In case the rpc gets retried, roll the
5766 * uio fields changed by nfsm_uiombuflist()
5769 uiop->uio_offset = offs;
5770 uiop->uio_resid = resid;
5771 uiop->uio_iov->iov_base = iovbase;
5772 uiop->uio_iov->iov_len = iovlen;
5777 nfscl_lockderef(lckp);
5778 NFSFREECRED(newcred);
5779 nfscl_rellayout(layp, 0);
5785 * Make a copy of the mbuf chain and add an mbuf for null padding, as required.
5787 static struct mbuf *
5788 nfsm_copym(struct mbuf *m, int off, int xfer)
5790 struct mbuf *m2, *m3, *m4;
5794 m2 = m_copym(m, off, xfer, M_WAITOK);
5795 rem = NFSM_RNDUP(xfer) - xfer;
5798 * The zero padding to a multiple of 4 bytes is required by
5799 * the XDR. So that the mbufs copied by reference aren't
5800 * modified, add an mbuf with the zero'd bytes to the list.
5801 * rem will be a maximum of 3, so one zero'd uint32_t is
5805 while (m3->m_next != NULL)
5808 tl = NFSMTOD(m4, uint32_t *);
5810 mbuf_setlen(m4, rem);
5811 mbuf_setnext(m3, m4);
5817 * Find a file layout that will handle the first bytes of the requested
5818 * range and return the information from it needed to the I/O operation.
5821 nfscl_findlayoutforio(struct nfscllayout *lyp, uint64_t off, uint32_t rwaccess,
5822 struct nfsclflayout **retflpp)
5824 struct nfsclflayout *flp, *nflp, *rflp;
5829 /* For reading, do the Read list first and then the Write list. */
5831 if (rw == NFSV4OPEN_ACCESSREAD)
5832 flp = LIST_FIRST(&lyp->nfsly_flayread);
5834 flp = LIST_FIRST(&lyp->nfsly_flayrw);
5835 while (flp != NULL) {
5836 nflp = LIST_NEXT(flp, nfsfl_list);
5837 if (flp->nfsfl_off > off)
5839 if (flp->nfsfl_end > off &&
5840 (rflp == NULL || rflp->nfsfl_end < flp->nfsfl_end))
5844 if (rw == NFSV4OPEN_ACCESSREAD)
5845 rw = NFSV4OPEN_ACCESSWRITE;
5850 /* This one covers the most bytes starting at off. */
5858 * Do I/O using an NFSv4.1 file layout.
5861 nfscl_doflayoutio(vnode_t vp, struct uio *uiop, int *iomode, int *must_commit,
5862 int *eofp, nfsv4stateid_t *stateidp, int rwflag, struct nfscldevinfo *dp,
5863 struct nfscllayout *lyp, struct nfsclflayout *flp, uint64_t off,
5864 uint64_t len, int docommit, struct ucred *cred, NFSPROC_T *p)
5866 uint64_t io_off, rel_off, stripe_unit_size, transfer, xfer;
5867 int commit_thru_mds, error, stripe_index, stripe_pos;
5870 struct nfsclds **dspp;
5873 rel_off = off - flp->nfsfl_patoff;
5874 stripe_unit_size = (flp->nfsfl_util >> 6) & 0x3ffffff;
5875 stripe_pos = (rel_off / stripe_unit_size + flp->nfsfl_stripe1) %
5876 dp->nfsdi_stripecnt;
5877 transfer = stripe_unit_size - (rel_off % stripe_unit_size);
5880 /* Loop around, doing I/O for each stripe unit. */
5881 while (len > 0 && error == 0) {
5882 stripe_index = nfsfldi_stripeindex(dp, stripe_pos);
5883 dspp = nfsfldi_addr(dp, stripe_index);
5884 if (len > transfer && docommit == 0)
5888 if ((flp->nfsfl_util & NFSFLAYUTIL_DENSE) != 0) {
5890 if (stripe_pos >= flp->nfsfl_fhcnt)
5892 fhp = flp->nfsfl_fh[stripe_pos];
5893 io_off = (rel_off / (stripe_unit_size *
5894 dp->nfsdi_stripecnt)) * stripe_unit_size +
5895 rel_off % stripe_unit_size;
5897 /* Sparse layout. */
5898 if (flp->nfsfl_fhcnt > 1) {
5899 if (stripe_index >= flp->nfsfl_fhcnt)
5901 fhp = flp->nfsfl_fh[stripe_index];
5902 } else if (flp->nfsfl_fhcnt == 1)
5903 fhp = flp->nfsfl_fh[0];
5908 if ((flp->nfsfl_util & NFSFLAYUTIL_COMMIT_THRU_MDS) != 0) {
5909 commit_thru_mds = 1;
5913 commit_thru_mds = 0;
5914 mtx_lock(&np->n_mtx);
5915 np->n_flag |= NDSCOMMIT;
5916 mtx_unlock(&np->n_mtx);
5918 if (docommit != 0) {
5920 error = nfsrpc_commitds(vp, io_off, xfer,
5921 *dspp, fhp, 0, 0, cred, p);
5924 * Set both eof and uio_resid = 0 to end any
5928 uiop->uio_resid = 0;
5930 mtx_lock(&np->n_mtx);
5931 np->n_flag &= ~NDSCOMMIT;
5932 mtx_unlock(&np->n_mtx);
5934 } else if (rwflag == NFSV4OPEN_ACCESSREAD)
5935 error = nfsrpc_readds(vp, uiop, stateidp, eofp, *dspp,
5936 io_off, xfer, fhp, 0, 0, 0, cred, p);
5938 error = nfsrpc_writeds(vp, uiop, iomode, must_commit,
5939 stateidp, *dspp, io_off, xfer, fhp, commit_thru_mds,
5943 lyp->nfsly_flags |= NFSLY_WRITTEN;
5948 transfer = stripe_unit_size;
5949 stripe_pos = (stripe_pos + 1) % dp->nfsdi_stripecnt;
5958 * Do I/O using an NFSv4.1 flex file layout.
5961 nfscl_dofflayoutio(vnode_t vp, struct uio *uiop, int *iomode, int *must_commit,
5962 int *eofp, nfsv4stateid_t *stateidp, int rwflag, struct nfscldevinfo *dp,
5963 struct nfscllayout *lyp, struct nfsclflayout *flp, uint64_t off,
5964 uint64_t len, int mirror, int docommit, struct mbuf *mp,
5965 struct nfsclwritedsdorpc *drpc, struct ucred *cred, NFSPROC_T *p)
5967 uint64_t transfer, xfer;
5971 struct nfsclds **dspp;
5972 struct ucred *tcred;
5978 NFSCL_DEBUG(4, "nfscl_dofflayoutio: off=%ju len=%ju\n", (uintmax_t)off,
5980 /* Loop around, doing I/O for each stripe unit. */
5981 while (len > 0 && error == 0) {
5982 dspp = nfsfldi_addr(dp, 0);
5983 fhp = flp->nfsfl_ffm[mirror].fh[dp->nfsdi_versindex];
5984 stateidp = &flp->nfsfl_ffm[mirror].st;
5985 NFSCL_DEBUG(4, "mirror=%d vind=%d fhlen=%d st.seqid=0x%x\n",
5986 mirror, dp->nfsdi_versindex, fhp->nfh_len, stateidp->seqid);
5987 if ((dp->nfsdi_flags & NFSDI_TIGHTCOUPLED) == 0) {
5988 tcred = NFSNEWCRED(cred);
5989 tcred->cr_uid = flp->nfsfl_ffm[mirror].user;
5990 tcred->cr_groups[0] = flp->nfsfl_ffm[mirror].group;
5991 tcred->cr_ngroups = 1;
5994 if (rwflag == NFSV4OPEN_ACCESSREAD)
5995 transfer = dp->nfsdi_rsize;
5997 transfer = dp->nfsdi_wsize;
5998 mtx_lock(&np->n_mtx);
5999 np->n_flag |= NDSCOMMIT;
6000 mtx_unlock(&np->n_mtx);
6001 if (len > transfer && docommit == 0)
6005 if (docommit != 0) {
6008 * Do last mirrored DS commit with this thread.
6010 if (mirror < flp->nfsfl_mirrorcnt - 1)
6011 error = nfsio_commitds(vp, off, xfer,
6012 *dspp, fhp, dp->nfsdi_vers,
6013 dp->nfsdi_minorvers, drpc, tcred,
6016 error = nfsrpc_commitds(vp, off, xfer,
6017 *dspp, fhp, dp->nfsdi_vers,
6018 dp->nfsdi_minorvers, tcred, p);
6020 NFSCL_DEBUG(4, "aft nfsio_commitds=%d\n", error);
6023 * Set both eof and uio_resid = 0 to end any
6027 uiop->uio_resid = 0;
6029 mtx_lock(&np->n_mtx);
6030 np->n_flag &= ~NDSCOMMIT;
6031 mtx_unlock(&np->n_mtx);
6033 } else if (rwflag == NFSV4OPEN_ACCESSREAD)
6034 error = nfsrpc_readds(vp, uiop, stateidp, eofp, *dspp,
6035 off, xfer, fhp, 1, dp->nfsdi_vers,
6036 dp->nfsdi_minorvers, tcred, p);
6038 if (flp->nfsfl_mirrorcnt == 1) {
6039 error = nfsrpc_writeds(vp, uiop, iomode,
6040 must_commit, stateidp, *dspp, off, xfer,
6041 fhp, 0, 1, dp->nfsdi_vers,
6042 dp->nfsdi_minorvers, tcred, p);
6045 lyp->nfsly_flags |= NFSLY_WRITTEN;
6049 m = nfsm_copym(mp, rel_off, xfer);
6050 NFSCL_DEBUG(4, "mcopy reloff=%d xfer=%jd\n",
6051 rel_off, (uintmax_t)xfer);
6053 * Do last write to a mirrored DS with this
6056 if (mirror < flp->nfsfl_mirrorcnt - 1)
6057 error = nfsio_writedsmir(vp, iomode,
6058 must_commit, stateidp, *dspp, off,
6059 xfer, fhp, m, dp->nfsdi_vers,
6060 dp->nfsdi_minorvers, drpc, tcred,
6063 error = nfsrpc_writedsmir(vp, iomode,
6064 must_commit, stateidp, *dspp, off,
6065 xfer, fhp, m, dp->nfsdi_vers,
6066 dp->nfsdi_minorvers, tcred, p);
6067 NFSCL_DEBUG(4, "nfsio_writedsmir=%d\n", error);
6070 NFSCL_DEBUG(4, "aft read/writeds=%d\n", error);
6076 if ((dp->nfsdi_flags & NFSDI_TIGHTCOUPLED) == 0)
6079 NFSCL_DEBUG(4, "eo nfscl_dofflayoutio=%d\n", error);
6084 * The actual read RPC done to a DS.
6087 nfsrpc_readds(vnode_t vp, struct uio *uiop, nfsv4stateid_t *stateidp, int *eofp,
6088 struct nfsclds *dsp, uint64_t io_off, int len, struct nfsfh *fhp, int flex,
6089 int vers, int minorvers, struct ucred *cred, NFSPROC_T *p)
6092 int attrflag, error, retlen;
6093 struct nfsrv_descript nfsd;
6094 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
6095 struct nfsrv_descript *nd = &nfsd;
6096 struct nfssockreq *nrp;
6100 if (vers == 0 || vers == NFS_VER4) {
6101 nfscl_reqstart(nd, NFSPROC_READDS, nmp, fhp->nfh_fh,
6102 fhp->nfh_len, NULL, &dsp->nfsclds_sess, vers, minorvers);
6104 NFSCL_DEBUG(4, "nfsrpc_readds: vers4 minvers=%d\n", minorvers);
6106 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID);
6108 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSEQIDZERO);
6110 nfscl_reqstart(nd, NFSPROC_READ, nmp, fhp->nfh_fh,
6111 fhp->nfh_len, NULL, &dsp->nfsclds_sess, vers, minorvers);
6112 NFSCL_DEBUG(4, "nfsrpc_readds: vers3\n");
6114 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED * 3);
6115 txdr_hyper(io_off, tl);
6116 *(tl + 2) = txdr_unsigned(len);
6117 nrp = dsp->nfsclds_sockp;
6118 NFSCL_DEBUG(4, "nfsrpc_readds: nrp=%p\n", nrp);
6120 /* If NULL, use the MDS socket. */
6121 nrp = &nmp->nm_sockreq;
6122 error = newnfs_request(nd, nmp, NULL, nrp, vp, p, cred,
6123 NFS_PROG, vers, NULL, 1, NULL, &dsp->nfsclds_sess);
6124 NFSCL_DEBUG(4, "nfsrpc_readds: stat=%d err=%d\n", nd->nd_repstat,
6128 if (vers == NFS_VER3) {
6129 error = nfscl_postop_attr(nd, &na, &attrflag, NULL);
6130 NFSCL_DEBUG(4, "nfsrpc_readds: postop=%d\n", error);
6134 if (nd->nd_repstat != 0) {
6135 error = nd->nd_repstat;
6138 if (vers == NFS_VER3) {
6139 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
6140 *eofp = fxdr_unsigned(int, *(tl + 1));
6142 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
6143 *eofp = fxdr_unsigned(int, *tl);
6145 NFSM_STRSIZ(retlen, len);
6146 NFSCL_DEBUG(4, "nfsrpc_readds: retlen=%d eof=%d\n", retlen, *eofp);
6147 error = nfsm_mbufuio(nd, uiop, retlen);
6149 if (nd->nd_mrep != NULL)
6150 mbuf_freem(nd->nd_mrep);
6155 * The actual write RPC done to a DS.
6158 nfsrpc_writeds(vnode_t vp, struct uio *uiop, int *iomode, int *must_commit,
6159 nfsv4stateid_t *stateidp, struct nfsclds *dsp, uint64_t io_off, int len,
6160 struct nfsfh *fhp, int commit_thru_mds, int flex, int vers, int minorvers,
6161 struct ucred *cred, NFSPROC_T *p)
6164 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
6165 int attrflag, error, rlen, commit, committed = NFSWRITE_FILESYNC;
6167 struct nfsrv_descript nfsd;
6168 struct nfsrv_descript *nd = &nfsd;
6169 struct nfssockreq *nrp;
6172 KASSERT(uiop->uio_iovcnt == 1, ("nfs: writerpc iovcnt > 1"));
6174 if (vers == 0 || vers == NFS_VER4) {
6175 nfscl_reqstart(nd, NFSPROC_WRITEDS, nmp, fhp->nfh_fh,
6176 fhp->nfh_len, NULL, &dsp->nfsclds_sess, vers, minorvers);
6177 NFSCL_DEBUG(4, "nfsrpc_writeds: vers4 minvers=%d\n", minorvers);
6180 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID);
6182 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSEQIDZERO);
6183 NFSM_BUILD(tl, uint32_t *, NFSX_HYPER + 2 * NFSX_UNSIGNED);
6185 nfscl_reqstart(nd, NFSPROC_WRITE, nmp, fhp->nfh_fh,
6186 fhp->nfh_len, NULL, &dsp->nfsclds_sess, vers, minorvers);
6187 NFSCL_DEBUG(4, "nfsrpc_writeds: vers3\n");
6188 NFSM_BUILD(tl, uint32_t *, NFSX_HYPER + 3 * NFSX_UNSIGNED);
6190 txdr_hyper(io_off, tl);
6192 if (vers == NFS_VER3)
6193 *tl++ = txdr_unsigned(len);
6194 *tl++ = txdr_unsigned(*iomode);
6195 *tl = txdr_unsigned(len);
6196 nfsm_uiombuf(nd, uiop, len);
6197 nrp = dsp->nfsclds_sockp;
6199 /* If NULL, use the MDS socket. */
6200 nrp = &nmp->nm_sockreq;
6201 error = newnfs_request(nd, nmp, NULL, nrp, vp, p, cred,
6202 NFS_PROG, vers, NULL, 1, NULL, &dsp->nfsclds_sess);
6203 NFSCL_DEBUG(4, "nfsrpc_writeds: err=%d stat=%d\n", error,
6207 if (nd->nd_repstat != 0) {
6209 * In case the rpc gets retried, roll
6210 * the uio fileds changed by nfsm_uiombuf()
6213 uiop->uio_offset -= len;
6214 uio_uio_resid_add(uiop, len);
6215 uio_iov_base_add(uiop, -len);
6216 uio_iov_len_add(uiop, len);
6217 error = nd->nd_repstat;
6219 if (vers == NFS_VER3) {
6220 error = nfscl_wcc_data(nd, vp, &na, &attrflag, NULL,
6222 NFSCL_DEBUG(4, "nfsrpc_writeds: wcc_data=%d\n", error);
6226 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED + NFSX_VERF);
6227 rlen = fxdr_unsigned(int, *tl++);
6228 NFSCL_DEBUG(4, "nfsrpc_writeds: len=%d rlen=%d\n", len, rlen);
6232 } else if (rlen < len) {
6233 backup = len - rlen;
6234 uio_iov_base_add(uiop, -(backup));
6235 uio_iov_len_add(uiop, backup);
6236 uiop->uio_offset -= backup;
6237 uio_uio_resid_add(uiop, backup);
6240 commit = fxdr_unsigned(int, *tl++);
6243 * Return the lowest commitment level
6244 * obtained by any of the RPCs.
6246 if (committed == NFSWRITE_FILESYNC)
6248 else if (committed == NFSWRITE_DATASYNC &&
6249 commit == NFSWRITE_UNSTABLE)
6251 if (commit_thru_mds != 0) {
6253 if (!NFSHASWRITEVERF(nmp)) {
6254 NFSBCOPY(tl, nmp->nm_verf, NFSX_VERF);
6255 NFSSETWRITEVERF(nmp);
6256 } else if (NFSBCMP(tl, nmp->nm_verf, NFSX_VERF)) {
6258 NFSBCOPY(tl, nmp->nm_verf, NFSX_VERF);
6263 if ((dsp->nfsclds_flags & NFSCLDS_HASWRITEVERF) == 0) {
6264 NFSBCOPY(tl, dsp->nfsclds_verf, NFSX_VERF);
6265 dsp->nfsclds_flags |= NFSCLDS_HASWRITEVERF;
6266 } else if (NFSBCMP(tl, dsp->nfsclds_verf, NFSX_VERF)) {
6268 NFSBCOPY(tl, dsp->nfsclds_verf, NFSX_VERF);
6274 if (nd->nd_mrep != NULL)
6275 mbuf_freem(nd->nd_mrep);
6276 *iomode = committed;
6277 if (nd->nd_repstat != 0 && error == 0)
6278 error = nd->nd_repstat;
6283 * The actual write RPC done to a DS.
6284 * This variant is called from a separate kernel process for mirrors.
6285 * Any short write is considered an IO error.
6288 nfsrpc_writedsmir(vnode_t vp, int *iomode, int *must_commit,
6289 nfsv4stateid_t *stateidp, struct nfsclds *dsp, uint64_t io_off, int len,
6290 struct nfsfh *fhp, struct mbuf *m, int vers, int minorvers,
6291 struct ucred *cred, NFSPROC_T *p)
6294 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
6295 int attrflag, error, commit, committed = NFSWRITE_FILESYNC, rlen;
6296 struct nfsrv_descript nfsd;
6297 struct nfsrv_descript *nd = &nfsd;
6298 struct nfssockreq *nrp;
6302 if (vers == 0 || vers == NFS_VER4) {
6303 nfscl_reqstart(nd, NFSPROC_WRITEDS, nmp, fhp->nfh_fh,
6304 fhp->nfh_len, NULL, &dsp->nfsclds_sess, vers, minorvers);
6306 NFSCL_DEBUG(4, "nfsrpc_writedsmir: vers4 minvers=%d\n",
6308 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID);
6309 NFSM_BUILD(tl, uint32_t *, NFSX_HYPER + 2 * NFSX_UNSIGNED);
6311 nfscl_reqstart(nd, NFSPROC_WRITE, nmp, fhp->nfh_fh,
6312 fhp->nfh_len, NULL, &dsp->nfsclds_sess, vers, minorvers);
6313 NFSCL_DEBUG(4, "nfsrpc_writedsmir: vers3\n");
6314 NFSM_BUILD(tl, uint32_t *, NFSX_HYPER + 3 * NFSX_UNSIGNED);
6316 txdr_hyper(io_off, tl);
6318 if (vers == NFS_VER3)
6319 *tl++ = txdr_unsigned(len);
6320 *tl++ = txdr_unsigned(*iomode);
6321 *tl = txdr_unsigned(len);
6323 /* Put data in mbuf chain. */
6324 nd->nd_mb->m_next = m;
6325 /* Set nd_mb and nd_bpos to end of data. */
6326 while (m->m_next != NULL)
6329 nd->nd_bpos = mtod(m, char *) + m->m_len;
6330 NFSCL_DEBUG(4, "nfsrpc_writedsmir: lastmb len=%d\n", m->m_len);
6332 nrp = dsp->nfsclds_sockp;
6334 /* If NULL, use the MDS socket. */
6335 nrp = &nmp->nm_sockreq;
6336 error = newnfs_request(nd, nmp, NULL, nrp, vp, p, cred,
6337 NFS_PROG, vers, NULL, 1, NULL, &dsp->nfsclds_sess);
6338 NFSCL_DEBUG(4, "nfsrpc_writedsmir: err=%d stat=%d\n", error,
6342 if (nd->nd_repstat != 0)
6343 error = nd->nd_repstat;
6345 if (vers == NFS_VER3) {
6346 error = nfscl_wcc_data(nd, vp, &na, &attrflag, NULL,
6348 NFSCL_DEBUG(4, "nfsrpc_writedsmir: wcc_data=%d\n",
6353 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED + NFSX_VERF);
6354 rlen = fxdr_unsigned(int, *tl++);
6355 NFSCL_DEBUG(4, "nfsrpc_writedsmir: len=%d rlen=%d\n", len,
6359 NFSCL_DEBUG(4, "nfsrpc_writedsmir: len=%d rlen=%d\n",
6363 commit = fxdr_unsigned(int, *tl++);
6366 * Return the lowest commitment level
6367 * obtained by any of the RPCs.
6369 if (committed == NFSWRITE_FILESYNC)
6371 else if (committed == NFSWRITE_DATASYNC &&
6372 commit == NFSWRITE_UNSTABLE)
6375 if ((dsp->nfsclds_flags & NFSCLDS_HASWRITEVERF) == 0) {
6376 NFSBCOPY(tl, dsp->nfsclds_verf, NFSX_VERF);
6377 dsp->nfsclds_flags |= NFSCLDS_HASWRITEVERF;
6378 } else if (NFSBCMP(tl, dsp->nfsclds_verf, NFSX_VERF)) {
6380 NFSBCOPY(tl, dsp->nfsclds_verf, NFSX_VERF);
6385 if (nd->nd_mrep != NULL)
6386 mbuf_freem(nd->nd_mrep);
6387 *iomode = committed;
6388 if (nd->nd_repstat != 0 && error == 0)
6389 error = nd->nd_repstat;
6394 * Start up the thread that will execute nfsrpc_writedsmir().
6397 start_writedsmir(void *arg, int pending)
6399 struct nfsclwritedsdorpc *drpc;
6401 drpc = (struct nfsclwritedsdorpc *)arg;
6402 drpc->err = nfsrpc_writedsmir(drpc->vp, &drpc->iomode,
6403 &drpc->must_commit, drpc->stateidp, drpc->dsp, drpc->off, drpc->len,
6404 drpc->fhp, drpc->m, drpc->vers, drpc->minorvers, drpc->cred,
6407 NFSCL_DEBUG(4, "start_writedsmir: err=%d\n", drpc->err);
6411 * Set up the write DS mirror call for the pNFS I/O thread.
6414 nfsio_writedsmir(vnode_t vp, int *iomode, int *must_commit,
6415 nfsv4stateid_t *stateidp, struct nfsclds *dsp, uint64_t off, int len,
6416 struct nfsfh *fhp, struct mbuf *m, int vers, int minorvers,
6417 struct nfsclwritedsdorpc *drpc, struct ucred *cred, NFSPROC_T *p)
6424 drpc->iomode = *iomode;
6425 drpc->must_commit = *must_commit;
6426 drpc->stateidp = stateidp;
6433 drpc->minorvers = minorvers;
6438 if (nfs_pnfsiothreads > 0) {
6439 ret = nfs_pnfsio(start_writedsmir, drpc);
6440 NFSCL_DEBUG(4, "nfsio_writedsmir: nfs_pnfsio=%d\n", ret);
6443 error = nfsrpc_writedsmir(vp, iomode, must_commit, stateidp,
6444 dsp, off, len, fhp, m, vers, minorvers, cred, p);
6445 NFSCL_DEBUG(4, "nfsio_writedsmir: error=%d\n", error);
6450 * Free up the nfsclds structure.
6453 nfscl_freenfsclds(struct nfsclds *dsp)
6459 if (dsp->nfsclds_sockp != NULL) {
6460 NFSFREECRED(dsp->nfsclds_sockp->nr_cred);
6461 NFSFREEMUTEX(&dsp->nfsclds_sockp->nr_mtx);
6462 free(dsp->nfsclds_sockp->nr_nam, M_SONAME);
6463 free(dsp->nfsclds_sockp, M_NFSSOCKREQ);
6465 NFSFREEMUTEX(&dsp->nfsclds_mtx);
6466 NFSFREEMUTEX(&dsp->nfsclds_sess.nfsess_mtx);
6467 for (i = 0; i < NFSV4_CBSLOTS; i++) {
6468 if (dsp->nfsclds_sess.nfsess_cbslots[i].nfssl_reply != NULL)
6470 dsp->nfsclds_sess.nfsess_cbslots[i].nfssl_reply);
6472 free(dsp, M_NFSCLDS);
6475 static enum nfsclds_state
6476 nfscl_getsameserver(struct nfsmount *nmp, struct nfsclds *newdsp,
6477 struct nfsclds **retdspp)
6479 struct nfsclds *dsp, *cur_dsp;
6482 * Search the list of nfsclds structures for one with the same
6486 TAILQ_FOREACH(dsp, &nmp->nm_sess, nfsclds_list) {
6487 if (dsp->nfsclds_servownlen == newdsp->nfsclds_servownlen &&
6488 dsp->nfsclds_servownlen != 0 &&
6489 !NFSBCMP(dsp->nfsclds_serverown, newdsp->nfsclds_serverown,
6490 dsp->nfsclds_servownlen) &&
6491 dsp->nfsclds_sess.nfsess_defunct == 0) {
6492 NFSCL_DEBUG(4, "fnd same fdsp=%p dsp=%p flg=0x%x\n",
6493 TAILQ_FIRST(&nmp->nm_sess), dsp,
6494 dsp->nfsclds_flags);
6495 /* Server major id matches. */
6496 if ((dsp->nfsclds_flags & NFSCLDS_DS) != 0) {
6498 return (NFSDSP_USETHISSESSION);
6502 * Note the first match, so it can be used for
6503 * sequence'ing new sessions.
6505 if (cur_dsp == NULL)
6509 if (cur_dsp != NULL) {
6511 return (NFSDSP_SEQTHISSESSION);
6513 return (NFSDSP_NOTFOUND);
6517 * NFS commit rpc to a NFSv4.1 DS.
6520 nfsrpc_commitds(vnode_t vp, uint64_t offset, int cnt, struct nfsclds *dsp,
6521 struct nfsfh *fhp, int vers, int minorvers, struct ucred *cred,
6525 struct nfsrv_descript nfsd, *nd = &nfsd;
6526 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
6527 struct nfssockreq *nrp;
6529 int attrflag, error;
6532 if (vers == 0 || vers == NFS_VER4) {
6533 nfscl_reqstart(nd, NFSPROC_COMMITDS, nmp, fhp->nfh_fh,
6534 fhp->nfh_len, NULL, &dsp->nfsclds_sess, vers, minorvers);
6537 nfscl_reqstart(nd, NFSPROC_COMMIT, nmp, fhp->nfh_fh,
6538 fhp->nfh_len, NULL, &dsp->nfsclds_sess, vers, minorvers);
6539 NFSCL_DEBUG(4, "nfsrpc_commitds: vers=%d minvers=%d\n", vers,
6541 NFSM_BUILD(tl, uint32_t *, NFSX_HYPER + NFSX_UNSIGNED);
6542 txdr_hyper(offset, tl);
6544 *tl = txdr_unsigned(cnt);
6545 nrp = dsp->nfsclds_sockp;
6547 /* If NULL, use the MDS socket. */
6548 nrp = &nmp->nm_sockreq;
6549 error = newnfs_request(nd, nmp, NULL, nrp, vp, p, cred,
6550 NFS_PROG, vers, NULL, 1, NULL, &dsp->nfsclds_sess);
6551 NFSCL_DEBUG(4, "nfsrpc_commitds: err=%d stat=%d\n", error,
6555 if (nd->nd_repstat == 0) {
6556 if (vers == NFS_VER3) {
6557 error = nfscl_wcc_data(nd, vp, &na, &attrflag, NULL,
6559 NFSCL_DEBUG(4, "nfsrpc_commitds: wccdata=%d\n", error);
6563 NFSM_DISSECT(tl, u_int32_t *, NFSX_VERF);
6565 if (NFSBCMP(tl, dsp->nfsclds_verf, NFSX_VERF)) {
6566 NFSBCOPY(tl, dsp->nfsclds_verf, NFSX_VERF);
6567 error = NFSERR_STALEWRITEVERF;
6572 if (error == 0 && nd->nd_repstat != 0)
6573 error = nd->nd_repstat;
6574 mbuf_freem(nd->nd_mrep);
6579 * Start up the thread that will execute nfsrpc_commitds().
6582 start_commitds(void *arg, int pending)
6584 struct nfsclwritedsdorpc *drpc;
6586 drpc = (struct nfsclwritedsdorpc *)arg;
6587 drpc->err = nfsrpc_commitds(drpc->vp, drpc->off, drpc->len,
6588 drpc->dsp, drpc->fhp, drpc->vers, drpc->minorvers, drpc->cred,
6591 NFSCL_DEBUG(4, "start_commitds: err=%d\n", drpc->err);
6595 * Set up the commit DS mirror call for the pNFS I/O thread.
6598 nfsio_commitds(vnode_t vp, uint64_t offset, int cnt, struct nfsclds *dsp,
6599 struct nfsfh *fhp, int vers, int minorvers,
6600 struct nfsclwritedsdorpc *drpc, struct ucred *cred, NFSPROC_T *p)
6612 drpc->minorvers = minorvers;
6617 if (nfs_pnfsiothreads > 0) {
6618 ret = nfs_pnfsio(start_commitds, drpc);
6619 NFSCL_DEBUG(4, "nfsio_commitds: nfs_pnfsio=%d\n", ret);
6622 error = nfsrpc_commitds(vp, offset, cnt, dsp, fhp, vers,
6623 minorvers, cred, p);
6624 NFSCL_DEBUG(4, "nfsio_commitds: error=%d\n", error);
6629 * Set up the XDR arguments for the LayoutGet operation.
6632 nfsrv_setuplayoutget(struct nfsrv_descript *nd, int iomode, uint64_t offset,
6633 uint64_t len, uint64_t minlen, nfsv4stateid_t *stateidp, int layouttype,
6634 int layoutlen, int usecurstateid)
6638 NFSM_BUILD(tl, uint32_t *, 4 * NFSX_UNSIGNED + 3 * NFSX_HYPER +
6640 *tl++ = newnfs_false; /* Don't signal availability. */
6641 *tl++ = txdr_unsigned(layouttype);
6642 *tl++ = txdr_unsigned(iomode);
6643 txdr_hyper(offset, tl);
6645 txdr_hyper(len, tl);
6647 txdr_hyper(minlen, tl);
6649 if (usecurstateid != 0) {
6650 /* Special stateid for Current stateid. */
6651 *tl++ = txdr_unsigned(1);
6656 *tl++ = txdr_unsigned(stateidp->seqid);
6657 NFSCL_DEBUG(4, "layget seq=%d\n", (int)stateidp->seqid);
6658 *tl++ = stateidp->other[0];
6659 *tl++ = stateidp->other[1];
6660 *tl++ = stateidp->other[2];
6662 *tl = txdr_unsigned(layoutlen);
6666 * Parse the reply for a successful LayoutGet operation.
6669 nfsrv_parselayoutget(struct nfsrv_descript *nd, nfsv4stateid_t *stateidp,
6670 int *retonclosep, struct nfsclflayouthead *flhp)
6673 struct nfsclflayout *flp, *prevflp, *tflp;
6674 int cnt, error, fhcnt, gotiomode, i, iomode, j, k, l, laytype, nfhlen;
6676 uint64_t retlen, off;
6682 NFSCL_DEBUG(4, "in nfsrv_parselayoutget\n");
6686 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED + NFSX_STATEID);
6691 stateidp->seqid = fxdr_unsigned(uint32_t, *tl++);
6692 NFSCL_DEBUG(4, "retoncls=%d stseq=%d\n", *retonclosep,
6693 (int)stateidp->seqid);
6694 stateidp->other[0] = *tl++;
6695 stateidp->other[1] = *tl++;
6696 stateidp->other[2] = *tl++;
6697 cnt = fxdr_unsigned(int, *tl);
6698 NFSCL_DEBUG(4, "layg cnt=%d\n", cnt);
6699 if (cnt <= 0 || cnt > 10000) {
6700 /* Don't accept more than 10000 layouts in reply. */
6701 error = NFSERR_BADXDR;
6704 for (i = 0; i < cnt; i++) {
6705 /* Dissect to the layout type. */
6706 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_HYPER +
6708 off = fxdr_hyper(tl); tl += 2;
6709 retlen = fxdr_hyper(tl); tl += 2;
6710 iomode = fxdr_unsigned(int, *tl++);
6711 laytype = fxdr_unsigned(int, *tl);
6712 NFSCL_DEBUG(4, "layt=%d off=%ju len=%ju iom=%d\n", laytype,
6713 (uintmax_t)off, (uintmax_t)retlen, iomode);
6714 /* Ignore length of layout body for now. */
6715 if (laytype == NFSLAYOUT_NFSV4_1_FILES) {
6716 /* Parse the File layout up to fhcnt. */
6717 NFSM_DISSECT(tl, uint32_t *, 3 * NFSX_UNSIGNED +
6718 NFSX_HYPER + NFSX_V4DEVICEID);
6719 fhcnt = fxdr_unsigned(int, *(tl + 4 +
6720 NFSX_V4DEVICEID / NFSX_UNSIGNED));
6721 NFSCL_DEBUG(4, "fhcnt=%d\n", fhcnt);
6722 if (fhcnt < 0 || fhcnt > 100) {
6723 /* Don't accept more than 100 file handles. */
6724 error = NFSERR_BADXDR;
6728 flp = malloc(sizeof(*flp) + fhcnt *
6729 sizeof(struct nfsfh *), M_NFSFLAYOUT,
6732 flp = malloc(sizeof(*flp), M_NFSFLAYOUT,
6734 flp->nfsfl_flags = NFSFL_FILE;
6735 flp->nfsfl_fhcnt = 0;
6736 flp->nfsfl_devp = NULL;
6737 flp->nfsfl_off = off;
6738 if (flp->nfsfl_off + retlen < flp->nfsfl_off)
6739 flp->nfsfl_end = UINT64_MAX - flp->nfsfl_off;
6741 flp->nfsfl_end = flp->nfsfl_off + retlen;
6742 flp->nfsfl_iomode = iomode;
6743 if (gotiomode == -1)
6744 gotiomode = flp->nfsfl_iomode;
6745 /* Ignore layout body length for now. */
6746 NFSBCOPY(tl, flp->nfsfl_dev, NFSX_V4DEVICEID);
6747 tl += (NFSX_V4DEVICEID / NFSX_UNSIGNED);
6748 flp->nfsfl_util = fxdr_unsigned(uint32_t, *tl++);
6749 NFSCL_DEBUG(4, "flutil=0x%x\n", flp->nfsfl_util);
6750 flp->nfsfl_stripe1 = fxdr_unsigned(uint32_t, *tl++);
6751 flp->nfsfl_patoff = fxdr_hyper(tl); tl += 2;
6752 NFSCL_DEBUG(4, "stripe1=%u poff=%ju\n",
6753 flp->nfsfl_stripe1, (uintmax_t)flp->nfsfl_patoff);
6754 for (j = 0; j < fhcnt; j++) {
6755 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
6756 nfhlen = fxdr_unsigned(int, *tl);
6757 if (nfhlen <= 0 || nfhlen > NFSX_V4FHMAX) {
6758 error = NFSERR_BADXDR;
6761 nfhp = malloc(sizeof(*nfhp) + nfhlen - 1,
6763 flp->nfsfl_fh[j] = nfhp;
6765 nfhp->nfh_len = nfhlen;
6766 NFSM_DISSECT(cp, uint8_t *, NFSM_RNDUP(nfhlen));
6767 NFSBCOPY(cp, nfhp->nfh_fh, nfhlen);
6769 } else if (laytype == NFSLAYOUT_FLEXFILE) {
6770 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED +
6772 mirrorcnt = fxdr_unsigned(int, *(tl + 2));
6773 NFSCL_DEBUG(4, "mirrorcnt=%d\n", mirrorcnt);
6774 if (mirrorcnt < 1 || mirrorcnt > NFSDEV_MAXMIRRORS) {
6775 error = NFSERR_BADXDR;
6778 flp = malloc(sizeof(*flp) + mirrorcnt *
6779 sizeof(struct nfsffm), M_NFSFLAYOUT, M_WAITOK);
6780 flp->nfsfl_flags = NFSFL_FLEXFILE;
6781 flp->nfsfl_mirrorcnt = mirrorcnt;
6782 flp->nfsfl_devp = NULL;
6783 flp->nfsfl_off = off;
6784 if (flp->nfsfl_off + retlen < flp->nfsfl_off)
6785 flp->nfsfl_end = UINT64_MAX - flp->nfsfl_off;
6787 flp->nfsfl_end = flp->nfsfl_off + retlen;
6788 flp->nfsfl_iomode = iomode;
6789 if (gotiomode == -1)
6790 gotiomode = flp->nfsfl_iomode;
6791 flp->nfsfl_stripeunit = fxdr_hyper(tl);
6792 NFSCL_DEBUG(4, "stripeunit=%ju\n",
6793 (uintmax_t)flp->nfsfl_stripeunit);
6794 for (j = 0; j < mirrorcnt; j++) {
6795 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
6796 k = fxdr_unsigned(int, *tl);
6797 if (k < 1 || k > 128) {
6798 error = NFSERR_BADXDR;
6801 NFSCL_DEBUG(4, "servercnt=%d\n", k);
6802 for (l = 0; l < k; l++) {
6803 NFSM_DISSECT(tl, uint32_t *,
6804 NFSX_V4DEVICEID + NFSX_STATEID +
6807 /* Just use the first server. */
6809 flp->nfsfl_ffm[j].dev,
6811 tl += (NFSX_V4DEVICEID /
6814 flp->nfsfl_ffm[j].st.seqid =
6816 flp->nfsfl_ffm[j].st.other[0] =
6818 flp->nfsfl_ffm[j].st.other[1] =
6820 flp->nfsfl_ffm[j].st.other[2] =
6822 NFSCL_DEBUG(4, "st.seqid=%u "
6823 "st.o0=0x%x st.o1=0x%x "
6825 flp->nfsfl_ffm[j].st.seqid,
6826 flp->nfsfl_ffm[j].st.other[0],
6827 flp->nfsfl_ffm[j].st.other[1],
6828 flp->nfsfl_ffm[j].st.other[2]);
6830 tl += ((NFSX_V4DEVICEID +
6834 fhcnt = fxdr_unsigned(int, *tl);
6835 NFSCL_DEBUG(4, "fhcnt=%d\n", fhcnt);
6837 fhcnt > NFSDEV_MAXVERS) {
6838 error = NFSERR_BADXDR;
6841 for (m = 0; m < fhcnt; m++) {
6842 NFSM_DISSECT(tl, uint32_t *,
6844 nfhlen = fxdr_unsigned(int,
6846 NFSCL_DEBUG(4, "nfhlen=%d\n",
6848 if (nfhlen <= 0 || nfhlen >
6850 error = NFSERR_BADXDR;
6853 NFSM_DISSECT(cp, uint8_t *,
6854 NFSM_RNDUP(nfhlen));
6856 flp->nfsfl_ffm[j].fhcnt
6860 nfhlen - 1, M_NFSFH,
6862 flp->nfsfl_ffm[j].fh[m]
6864 nfhp->nfh_len = nfhlen;
6872 /* Now, get the ffsd_user/ffds_group. */
6873 error = nfsrv_parseug(nd, 0, &user,
6875 NFSCL_DEBUG(4, "after parseu=%d\n",
6878 error = nfsrv_parseug(nd, 1,
6879 &user, &grp, curthread);
6880 NFSCL_DEBUG(4, "aft parseg=%d\n",
6884 NFSCL_DEBUG(4, "user=%d group=%d\n",
6887 flp->nfsfl_ffm[j].user = user;
6888 flp->nfsfl_ffm[j].group = grp;
6890 "usr=%d grp=%d\n", user,
6895 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
6896 flp->nfsfl_fflags = fxdr_unsigned(uint32_t, *tl++);
6897 flp->nfsfl_statshint = fxdr_unsigned(uint32_t, *tl);
6898 NFSCL_DEBUG(4, "fflags=0x%x statshint=%d\n",
6899 flp->nfsfl_fflags, flp->nfsfl_statshint);
6901 error = NFSERR_BADXDR;
6904 if (flp->nfsfl_iomode == gotiomode) {
6905 /* Keep the list in increasing offset order. */
6906 tflp = LIST_FIRST(flhp);
6908 while (tflp != NULL &&
6909 tflp->nfsfl_off < flp->nfsfl_off) {
6911 tflp = LIST_NEXT(tflp, nfsfl_list);
6913 if (prevflp == NULL)
6914 LIST_INSERT_HEAD(flhp, flp, nfsfl_list);
6916 LIST_INSERT_AFTER(prevflp, flp,
6918 NFSCL_DEBUG(4, "flp inserted\n");
6920 printf("nfscl_layoutget(): got wrong iomode\n");
6921 nfscl_freeflayout(flp);
6926 NFSCL_DEBUG(4, "eo nfsrv_parselayoutget=%d\n", error);
6927 if (error != 0 && flp != NULL)
6928 nfscl_freeflayout(flp);
6933 * Parse a user/group digit string.
6936 nfsrv_parseug(struct nfsrv_descript *nd, int dogrp, uid_t *uidp, gid_t *gidp,
6940 char *cp, *str, str0[NFSV4_SMALLSTR + 1];
6944 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
6945 len = fxdr_unsigned(uint32_t, *tl);
6946 if (len > NFSV4_OPAQUELIMIT) {
6947 error = NFSERR_BADXDR;
6950 NFSCL_DEBUG(4, "nfsrv_parseug: len=%d\n", len);
6953 *gidp = GID_NOGROUP;
6958 if (len > NFSV4_SMALLSTR)
6959 str = malloc(len + 1, M_TEMP, M_WAITOK);
6962 NFSM_DISSECT(cp, char *, NFSM_RNDUP(len));
6963 NFSBCOPY(cp, str, len);
6965 NFSCL_DEBUG(4, "nfsrv_parseug: str=%s\n", str);
6967 error = nfsv4_strtogid(nd, str, len, gidp, p);
6969 error = nfsv4_strtouid(nd, str, len, uidp, p);
6971 if (len > NFSV4_SMALLSTR)
6973 NFSCL_DEBUG(4, "eo nfsrv_parseug=%d\n", error);
6978 * Similar to nfsrpc_getlayout(), except that it uses nfsrpc_openlayget(),
6979 * so that it does both an Open and a Layoutget.
6982 nfsrpc_getopenlayout(struct nfsmount *nmp, vnode_t vp, u_int8_t *nfhp,
6983 int fhlen, uint8_t *newfhp, int newfhlen, uint32_t mode,
6984 struct nfsclopen *op, uint8_t *name, int namelen, struct nfscldeleg **dpp,
6985 struct ucred *cred, NFSPROC_T *p)
6987 struct nfscllayout *lyp;
6988 struct nfsclflayout *flp;
6989 struct nfsclflayouthead flh;
6990 int error, islocked, layoutlen, recalled, retonclose, usecurstateid;
6991 int layouttype, laystat;
6992 nfsv4stateid_t stateid;
6993 struct nfsclsession *tsep;
6996 if (NFSHASFLEXFILE(nmp))
6997 layouttype = NFSLAYOUT_FLEXFILE;
6999 layouttype = NFSLAYOUT_NFSV4_1_FILES;
7001 * If lyp is returned non-NULL, there will be a refcnt (shared lock)
7002 * on it, iff flp != NULL or a lock (exclusive lock) on it iff
7005 lyp = nfscl_getlayout(nmp->nm_clp, newfhp, newfhlen, 0, &flp,
7007 NFSCL_DEBUG(4, "nfsrpc_getopenlayout nfscl_getlayout lyp=%p\n", lyp);
7010 else if (flp != NULL)
7014 if ((lyp == NULL || flp == NULL) && recalled == 0) {
7016 tsep = nfsmnt_mdssession(nmp);
7017 layoutlen = tsep->nfsess_maxcache - (NFSX_STATEID +
7023 stateid.seqid = lyp->nfsly_stateid.seqid;
7024 stateid.other[0] = lyp->nfsly_stateid.other[0];
7025 stateid.other[1] = lyp->nfsly_stateid.other[1];
7026 stateid.other[2] = lyp->nfsly_stateid.other[2];
7028 error = nfsrpc_openlayoutrpc(nmp, vp, nfhp, fhlen,
7029 newfhp, newfhlen, mode, op, name, namelen,
7030 dpp, &stateid, usecurstateid, layouttype, layoutlen,
7031 &retonclose, &flh, &laystat, cred, p);
7032 NFSCL_DEBUG(4, "aft nfsrpc_openlayoutrpc laystat=%d err=%d\n",
7034 laystat = nfsrpc_layoutgetres(nmp, vp, newfhp, newfhlen,
7035 &stateid, retonclose, NULL, &lyp, &flh, layouttype, laystat,
7036 &islocked, cred, p);
7038 error = nfsrpc_openrpc(nmp, vp, nfhp, fhlen, newfhp, newfhlen,
7039 mode, op, name, namelen, dpp, 0, 0, cred, p, 0, 0);
7041 nfscl_rellayout(lyp, 1);
7042 else if (islocked == 1)
7043 nfscl_rellayout(lyp, 0);
7048 * This function does an Open+LayoutGet for an NFSv4.1 mount with pNFS
7049 * enabled, only for the CLAIM_NULL case. All other NFSv4 Opens are
7050 * handled by nfsrpc_openrpc().
7051 * For the case where op == NULL, dvp is the directory. When op != NULL, it
7055 nfsrpc_openlayoutrpc(struct nfsmount *nmp, vnode_t vp, u_int8_t *nfhp,
7056 int fhlen, uint8_t *newfhp, int newfhlen, uint32_t mode,
7057 struct nfsclopen *op, uint8_t *name, int namelen, struct nfscldeleg **dpp,
7058 nfsv4stateid_t *stateidp, int usecurstateid, int layouttype,
7059 int layoutlen, int *retonclosep, struct nfsclflayouthead *flhp,
7060 int *laystatp, struct ucred *cred, NFSPROC_T *p)
7063 struct nfsrv_descript nfsd, *nd = &nfsd;
7064 struct nfscldeleg *ndp = NULL;
7065 struct nfsvattr nfsva;
7066 struct nfsclsession *tsep;
7067 uint32_t rflags, deleg;
7068 nfsattrbit_t attrbits;
7069 int error, ret, acesize, limitby, iomode;
7073 nfscl_reqstart(nd, NFSPROC_OPENLAYGET, nmp, nfhp, fhlen, NULL, NULL,
7075 NFSM_BUILD(tl, uint32_t *, 5 * NFSX_UNSIGNED);
7076 *tl++ = txdr_unsigned(op->nfso_own->nfsow_seqid);
7077 *tl++ = txdr_unsigned(mode & NFSV4OPEN_ACCESSBOTH);
7078 *tl++ = txdr_unsigned((mode >> NFSLCK_SHIFT) & NFSV4OPEN_DENYBOTH);
7079 tsep = nfsmnt_mdssession(nmp);
7080 *tl++ = tsep->nfsess_clientid.lval[0];
7081 *tl = tsep->nfsess_clientid.lval[1];
7082 nfsm_strtom(nd, op->nfso_own->nfsow_owner, NFSV4CL_LOCKNAMELEN);
7083 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_UNSIGNED);
7084 *tl++ = txdr_unsigned(NFSV4OPEN_NOCREATE);
7085 *tl = txdr_unsigned(NFSV4OPEN_CLAIMNULL);
7086 nfsm_strtom(nd, name, namelen);
7087 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
7088 *tl = txdr_unsigned(NFSV4OP_GETATTR);
7089 NFSZERO_ATTRBIT(&attrbits);
7090 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_CHANGE);
7091 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEMODIFY);
7092 nfsrv_putattrbit(nd, &attrbits);
7093 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
7094 *tl = txdr_unsigned(NFSV4OP_LAYOUTGET);
7095 if ((mode & NFSV4OPEN_ACCESSWRITE) != 0)
7096 iomode = NFSLAYOUTIOMODE_RW;
7098 iomode = NFSLAYOUTIOMODE_READ;
7099 nfsrv_setuplayoutget(nd, iomode, 0, UINT64_MAX, 0, stateidp,
7100 layouttype, layoutlen, usecurstateid);
7101 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, vp, p, cred,
7102 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
7105 NFSCL_INCRSEQID(op->nfso_own->nfsow_seqid, nd);
7106 if (nd->nd_repstat != 0)
7107 *laystatp = nd->nd_repstat;
7108 if ((nd->nd_flag & ND_NOMOREDATA) == 0) {
7109 /* ND_NOMOREDATA will be set if the Open operation failed. */
7110 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
7112 op->nfso_stateid.seqid = *tl++;
7113 op->nfso_stateid.other[0] = *tl++;
7114 op->nfso_stateid.other[1] = *tl++;
7115 op->nfso_stateid.other[2] = *tl;
7116 rflags = fxdr_unsigned(u_int32_t, *(tl + 6));
7117 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
7120 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
7121 deleg = fxdr_unsigned(u_int32_t, *tl);
7122 if (deleg == NFSV4OPEN_DELEGATEREAD ||
7123 deleg == NFSV4OPEN_DELEGATEWRITE) {
7124 if (!(op->nfso_own->nfsow_clp->nfsc_flags &
7125 NFSCLFLAGS_FIRSTDELEG))
7126 op->nfso_own->nfsow_clp->nfsc_flags |=
7127 (NFSCLFLAGS_FIRSTDELEG | NFSCLFLAGS_GOTDELEG);
7128 ndp = malloc(sizeof(struct nfscldeleg) + newfhlen,
7129 M_NFSCLDELEG, M_WAITOK);
7130 LIST_INIT(&ndp->nfsdl_owner);
7131 LIST_INIT(&ndp->nfsdl_lock);
7132 ndp->nfsdl_clp = op->nfso_own->nfsow_clp;
7133 ndp->nfsdl_fhlen = newfhlen;
7134 NFSBCOPY(newfhp, ndp->nfsdl_fh, newfhlen);
7135 newnfs_copyincred(cred, &ndp->nfsdl_cred);
7136 nfscl_lockinit(&ndp->nfsdl_rwlock);
7137 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
7139 ndp->nfsdl_stateid.seqid = *tl++;
7140 ndp->nfsdl_stateid.other[0] = *tl++;
7141 ndp->nfsdl_stateid.other[1] = *tl++;
7142 ndp->nfsdl_stateid.other[2] = *tl++;
7143 ret = fxdr_unsigned(int, *tl);
7144 if (deleg == NFSV4OPEN_DELEGATEWRITE) {
7145 ndp->nfsdl_flags = NFSCLDL_WRITE;
7147 * Indicates how much the file can grow.
7149 NFSM_DISSECT(tl, u_int32_t *,
7151 limitby = fxdr_unsigned(int, *tl++);
7153 case NFSV4OPEN_LIMITSIZE:
7154 ndp->nfsdl_sizelimit = fxdr_hyper(tl);
7156 case NFSV4OPEN_LIMITBLOCKS:
7157 ndp->nfsdl_sizelimit =
7158 fxdr_unsigned(u_int64_t, *tl++);
7159 ndp->nfsdl_sizelimit *=
7160 fxdr_unsigned(u_int64_t, *tl);
7163 error = NFSERR_BADXDR;
7167 ndp->nfsdl_flags = NFSCLDL_READ;
7169 ndp->nfsdl_flags |= NFSCLDL_RECALL;
7170 error = nfsrv_dissectace(nd, &ndp->nfsdl_ace, &ret,
7174 } else if (deleg != NFSV4OPEN_DELEGATENONE) {
7175 error = NFSERR_BADXDR;
7178 if ((rflags & NFSV4OPEN_LOCKTYPEPOSIX) != 0 ||
7179 nfscl_assumeposixlocks)
7180 op->nfso_posixlock = 1;
7182 op->nfso_posixlock = 0;
7183 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
7184 /* If the 2nd element == NFS_OK, the Getattr succeeded. */
7186 error = nfsv4_loadattr(nd, NULL, &nfsva, NULL,
7187 NULL, 0, NULL, NULL, NULL, NULL, NULL, 0,
7188 NULL, NULL, NULL, p, cred);
7192 ndp->nfsdl_change = nfsva.na_filerev;
7193 ndp->nfsdl_modtime = nfsva.na_mtime;
7194 ndp->nfsdl_flags |= NFSCLDL_MODTIMESET;
7199 * At this point, the Open has succeeded, so set
7200 * nd_repstat = NFS_OK. If the Layoutget failed,
7201 * this function just won't return a layout.
7203 if (nd->nd_repstat == 0) {
7204 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
7205 *laystatp = fxdr_unsigned(int, *++tl);
7206 if (*laystatp == 0) {
7207 error = nfsrv_parselayoutget(nd,
7208 stateidp, retonclosep, flhp);
7213 nd->nd_repstat = 0; /* Return 0 for Open. */
7216 if (nd->nd_repstat != 0 && error == 0)
7217 error = nd->nd_repstat;
7219 free(ndp, M_NFSCLDELEG);
7220 mbuf_freem(nd->nd_mrep);
7225 * Similar nfsrpc_createv4(), but also does the LayoutGet operation.
7226 * Used only for mounts with pNFS enabled.
7229 nfsrpc_createlayout(vnode_t dvp, char *name, int namelen, struct vattr *vap,
7230 nfsquad_t cverf, int fmode, struct nfsclowner *owp, struct nfscldeleg **dpp,
7231 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap,
7232 struct nfsvattr *nnap, struct nfsfh **nfhpp, int *attrflagp,
7233 int *dattrflagp, void *dstuff, int *unlockedp, nfsv4stateid_t *stateidp,
7234 int usecurstateid, int layouttype, int layoutlen, int *retonclosep,
7235 struct nfsclflayouthead *flhp, int *laystatp)
7238 int error = 0, deleg, newone, ret, acesize, limitby;
7239 struct nfsrv_descript nfsd, *nd = &nfsd;
7240 struct nfsclopen *op;
7241 struct nfscldeleg *dp = NULL;
7244 struct nfsclsession *tsep;
7245 nfsattrbit_t attrbits;
7246 nfsv4stateid_t stateid;
7248 struct nfsmount *nmp;
7250 nmp = VFSTONFS(dvp->v_mount);
7258 if (namelen > NFS_MAXNAMLEN)
7259 return (ENAMETOOLONG);
7260 NFSCL_REQSTART(nd, NFSPROC_CREATELAYGET, dvp);
7262 * For V4, this is actually an Open op.
7264 NFSM_BUILD(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
7265 *tl++ = txdr_unsigned(owp->nfsow_seqid);
7266 *tl++ = txdr_unsigned(NFSV4OPEN_ACCESSWRITE |
7267 NFSV4OPEN_ACCESSREAD);
7268 *tl++ = txdr_unsigned(NFSV4OPEN_DENYNONE);
7269 tsep = nfsmnt_mdssession(nmp);
7270 *tl++ = tsep->nfsess_clientid.lval[0];
7271 *tl = tsep->nfsess_clientid.lval[1];
7272 nfsm_strtom(nd, owp->nfsow_owner, NFSV4CL_LOCKNAMELEN);
7273 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
7274 *tl++ = txdr_unsigned(NFSV4OPEN_CREATE);
7275 if ((fmode & O_EXCL) != 0) {
7276 if (NFSHASSESSPERSIST(nmp)) {
7277 /* Use GUARDED for persistent sessions. */
7278 *tl = txdr_unsigned(NFSCREATE_GUARDED);
7279 nfscl_fillsattr(nd, vap, dvp, 0, 0);
7281 /* Otherwise, use EXCLUSIVE4_1. */
7282 *tl = txdr_unsigned(NFSCREATE_EXCLUSIVE41);
7283 NFSM_BUILD(tl, u_int32_t *, NFSX_VERF);
7284 *tl++ = cverf.lval[0];
7285 *tl = cverf.lval[1];
7286 nfscl_fillsattr(nd, vap, dvp, 0, 0);
7289 *tl = txdr_unsigned(NFSCREATE_UNCHECKED);
7290 nfscl_fillsattr(nd, vap, dvp, 0, 0);
7292 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
7293 *tl = txdr_unsigned(NFSV4OPEN_CLAIMNULL);
7294 nfsm_strtom(nd, name, namelen);
7295 /* Get the new file's handle and attributes, plus save the FH. */
7296 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
7297 *tl++ = txdr_unsigned(NFSV4OP_SAVEFH);
7298 *tl++ = txdr_unsigned(NFSV4OP_GETFH);
7299 *tl = txdr_unsigned(NFSV4OP_GETATTR);
7300 NFSGETATTR_ATTRBIT(&attrbits);
7301 nfsrv_putattrbit(nd, &attrbits);
7302 /* Get the directory's post-op attributes. */
7303 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
7304 *tl = txdr_unsigned(NFSV4OP_PUTFH);
7305 nfsm_fhtom(nd, np->n_fhp->nfh_fh, np->n_fhp->nfh_len, 0);
7306 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
7307 *tl = txdr_unsigned(NFSV4OP_GETATTR);
7308 nfsrv_putattrbit(nd, &attrbits);
7309 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
7310 *tl++ = txdr_unsigned(NFSV4OP_RESTOREFH);
7311 *tl = txdr_unsigned(NFSV4OP_LAYOUTGET);
7312 nfsrv_setuplayoutget(nd, NFSLAYOUTIOMODE_RW, 0, UINT64_MAX, 0, stateidp,
7313 layouttype, layoutlen, usecurstateid);
7314 error = nfscl_request(nd, dvp, p, cred, dstuff);
7317 NFSCL_DEBUG(4, "nfsrpc_createlayout stat=%d err=%d\n", nd->nd_repstat,
7319 if (nd->nd_repstat != 0)
7320 *laystatp = nd->nd_repstat;
7321 NFSCL_INCRSEQID(owp->nfsow_seqid, nd);
7322 if ((nd->nd_flag & ND_NOMOREDATA) == 0) {
7323 NFSCL_DEBUG(4, "nfsrpc_createlayout open succeeded\n");
7324 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
7326 stateid.seqid = *tl++;
7327 stateid.other[0] = *tl++;
7328 stateid.other[1] = *tl++;
7329 stateid.other[2] = *tl;
7330 rflags = fxdr_unsigned(u_int32_t, *(tl + 6));
7331 nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
7332 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
7333 deleg = fxdr_unsigned(int, *tl);
7334 if (deleg == NFSV4OPEN_DELEGATEREAD ||
7335 deleg == NFSV4OPEN_DELEGATEWRITE) {
7336 if (!(owp->nfsow_clp->nfsc_flags &
7337 NFSCLFLAGS_FIRSTDELEG))
7338 owp->nfsow_clp->nfsc_flags |=
7339 (NFSCLFLAGS_FIRSTDELEG | NFSCLFLAGS_GOTDELEG);
7340 dp = malloc(sizeof(struct nfscldeleg) + NFSX_V4FHMAX,
7341 M_NFSCLDELEG, M_WAITOK);
7342 LIST_INIT(&dp->nfsdl_owner);
7343 LIST_INIT(&dp->nfsdl_lock);
7344 dp->nfsdl_clp = owp->nfsow_clp;
7345 newnfs_copyincred(cred, &dp->nfsdl_cred);
7346 nfscl_lockinit(&dp->nfsdl_rwlock);
7347 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
7349 dp->nfsdl_stateid.seqid = *tl++;
7350 dp->nfsdl_stateid.other[0] = *tl++;
7351 dp->nfsdl_stateid.other[1] = *tl++;
7352 dp->nfsdl_stateid.other[2] = *tl++;
7353 ret = fxdr_unsigned(int, *tl);
7354 if (deleg == NFSV4OPEN_DELEGATEWRITE) {
7355 dp->nfsdl_flags = NFSCLDL_WRITE;
7357 * Indicates how much the file can grow.
7359 NFSM_DISSECT(tl, u_int32_t *,
7361 limitby = fxdr_unsigned(int, *tl++);
7363 case NFSV4OPEN_LIMITSIZE:
7364 dp->nfsdl_sizelimit = fxdr_hyper(tl);
7366 case NFSV4OPEN_LIMITBLOCKS:
7367 dp->nfsdl_sizelimit =
7368 fxdr_unsigned(u_int64_t, *tl++);
7369 dp->nfsdl_sizelimit *=
7370 fxdr_unsigned(u_int64_t, *tl);
7373 error = NFSERR_BADXDR;
7377 dp->nfsdl_flags = NFSCLDL_READ;
7380 dp->nfsdl_flags |= NFSCLDL_RECALL;
7381 error = nfsrv_dissectace(nd, &dp->nfsdl_ace, &ret,
7385 } else if (deleg != NFSV4OPEN_DELEGATENONE) {
7386 error = NFSERR_BADXDR;
7390 /* Now, we should have the status for the SaveFH. */
7391 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
7393 NFSCL_DEBUG(4, "nfsrpc_createlayout SaveFH ok\n");
7395 * Now, process the GetFH and Getattr for the newly
7396 * created file. nfscl_mtofh() will set
7397 * ND_NOMOREDATA if these weren't successful.
7399 error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp);
7400 NFSCL_DEBUG(4, "aft nfscl_mtofh err=%d\n", error);
7404 nd->nd_flag |= ND_NOMOREDATA;
7405 /* Now we have the PutFH and Getattr for the directory. */
7406 if ((nd->nd_flag & ND_NOMOREDATA) == 0) {
7407 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
7409 nd->nd_flag |= ND_NOMOREDATA;
7411 NFSM_DISSECT(tl, uint32_t *, 2 *
7414 nd->nd_flag |= ND_NOMOREDATA;
7417 if ((nd->nd_flag & ND_NOMOREDATA) == 0) {
7418 /* Load the directory attributes. */
7419 error = nfsm_loadattr(nd, dnap);
7420 NFSCL_DEBUG(4, "aft nfsm_loadattr err=%d\n", error);
7424 if (dp != NULL && *attrflagp != 0) {
7425 dp->nfsdl_change = nnap->na_filerev;
7426 dp->nfsdl_modtime = nnap->na_mtime;
7427 dp->nfsdl_flags |= NFSCLDL_MODTIMESET;
7430 * We can now complete the Open state.
7434 dp->nfsdl_fhlen = nfhp->nfh_len;
7435 NFSBCOPY(nfhp->nfh_fh, dp->nfsdl_fh,
7439 * Get an Open structure that will be
7440 * attached to the OpenOwner, acquired already.
7442 error = nfscl_open(dvp, nfhp->nfh_fh, nfhp->nfh_len,
7443 (NFSV4OPEN_ACCESSWRITE | NFSV4OPEN_ACCESSREAD), 0,
7444 cred, p, NULL, &op, &newone, NULL, 0);
7447 op->nfso_stateid = stateid;
7448 newnfs_copyincred(cred, &op->nfso_cred);
7450 nfscl_openrelease(nmp, op, error, newone);
7453 /* Now, handle the RestoreFH and LayoutGet. */
7454 if (nd->nd_repstat == 0) {
7455 NFSM_DISSECT(tl, uint32_t *, 4 * NFSX_UNSIGNED);
7456 *laystatp = fxdr_unsigned(int, *(tl + 3));
7457 if (*laystatp == 0) {
7458 error = nfsrv_parselayoutget(nd,
7459 stateidp, retonclosep, flhp);
7463 NFSCL_DEBUG(4, "aft nfsrv_parselayout err=%d\n",
7469 if (nd->nd_repstat != 0 && error == 0)
7470 error = nd->nd_repstat;
7471 if (error == NFSERR_STALECLIENTID || error == NFSERR_BADSESSION)
7472 nfscl_initiate_recovery(owp->nfsow_clp);
7474 NFSCL_DEBUG(4, "eo nfsrpc_createlayout err=%d\n", error);
7478 free(dp, M_NFSCLDELEG);
7479 mbuf_freem(nd->nd_mrep);
7484 * Similar to nfsrpc_getopenlayout(), except that it used for the Create case.
7487 nfsrpc_getcreatelayout(vnode_t dvp, char *name, int namelen, struct vattr *vap,
7488 nfsquad_t cverf, int fmode, struct nfsclowner *owp, struct nfscldeleg **dpp,
7489 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap,
7490 struct nfsvattr *nnap, struct nfsfh **nfhpp, int *attrflagp,
7491 int *dattrflagp, void *dstuff, int *unlockedp)
7493 struct nfscllayout *lyp;
7494 struct nfsclflayouthead flh;
7496 struct nfsclsession *tsep;
7497 struct nfsmount *nmp;
7498 nfsv4stateid_t stateid;
7499 int error, layoutlen, layouttype, retonclose, laystat;
7502 nmp = VFSTONFS(dvp->v_mount);
7503 if (NFSHASFLEXFILE(nmp))
7504 layouttype = NFSLAYOUT_FLEXFILE;
7506 layouttype = NFSLAYOUT_NFSV4_1_FILES;
7508 tsep = nfsmnt_mdssession(nmp);
7509 layoutlen = tsep->nfsess_maxcache - (NFSX_STATEID + 3 * NFSX_UNSIGNED);
7510 error = nfsrpc_createlayout(dvp, name, namelen, vap, cverf, fmode,
7511 owp, dpp, cred, p, dnap, nnap, nfhpp, attrflagp, dattrflagp,
7512 dstuff, unlockedp, &stateid, 1, layouttype, layoutlen, &retonclose,
7514 NFSCL_DEBUG(4, "aft nfsrpc_createlayoutrpc laystat=%d err=%d\n",
7519 laystat = nfsrpc_layoutgetres(nmp, dvp, nfhp->nfh_fh,
7520 nfhp->nfh_len, &stateid, retonclose, NULL, &lyp, &flh,
7521 layouttype, laystat, NULL, cred, p);
7523 laystat = nfsrpc_layoutgetres(nmp, dvp, NULL, 0, &stateid,
7524 retonclose, NULL, &lyp, &flh, layouttype, laystat, NULL,
7527 nfscl_rellayout(lyp, 0);
7532 * Process the results of a layoutget() operation.
7535 nfsrpc_layoutgetres(struct nfsmount *nmp, vnode_t vp, uint8_t *newfhp,
7536 int newfhlen, nfsv4stateid_t *stateidp, int retonclose, uint32_t *notifybit,
7537 struct nfscllayout **lypp, struct nfsclflayouthead *flhp, int layouttype,
7538 int laystat, int *islockedp, struct ucred *cred, NFSPROC_T *p)
7540 struct nfsclflayout *tflp;
7541 struct nfscldevinfo *dip;
7544 if (laystat == NFSERR_UNKNLAYOUTTYPE) {
7546 if (!NFSHASFLEXFILE(nmp)) {
7547 /* Switch to using Flex File Layout. */
7548 nmp->nm_state |= NFSSTA_FLEXFILE;
7549 } else if (layouttype == NFSLAYOUT_FLEXFILE) {
7551 NFSCL_DEBUG(1, "disable PNFS\n");
7552 nmp->nm_state &= ~(NFSSTA_PNFS | NFSSTA_FLEXFILE);
7557 NFSCL_DEBUG(4, "nfsrpc_layoutgetres at FOREACH\n");
7558 LIST_FOREACH(tflp, flhp, nfsfl_list) {
7559 laystat = nfscl_adddevinfo(nmp, NULL, tflp);
7560 NFSCL_DEBUG(4, "aft adddev=%d\n", laystat);
7562 if (layouttype == NFSLAYOUT_FLEXFILE)
7563 dev = tflp->nfsfl_ffm[0].dev;
7565 dev = tflp->nfsfl_dev;
7566 laystat = nfsrpc_getdeviceinfo(nmp, dev,
7567 layouttype, notifybit, &dip, cred, p);
7568 NFSCL_DEBUG(4, "aft nfsrpc_gdi=%d\n",
7572 laystat = nfscl_adddevinfo(nmp, dip, tflp);
7574 printf("getlayout: cannot add\n");
7580 * nfscl_layout() always returns with the nfsly_lock
7581 * set to a refcnt (shared lock).
7582 * Passing in dvp is sufficient, since it is only used to
7583 * get the fsid for the file system.
7585 laystat = nfscl_layout(nmp, vp, newfhp, newfhlen, stateidp,
7586 layouttype, retonclose, flhp, lypp, cred, p);
7587 NFSCL_DEBUG(4, "nfsrpc_layoutgetres: aft nfscl_layout=%d\n",
7589 if (laystat == 0 && islockedp != NULL)