2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Rick Macklem at The University of Guelph.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
40 * Rpc op calls, generally called from the vnode op calls or through the
41 * buffer cache, for NFS v2, 3 and 4.
42 * These do not normally make any changes to vnode arguments or use
43 * structures that might change between the VFS variants. The returned
44 * arguments are all at the end, after the NFSPROC_T *p one.
47 #include "opt_inet6.h"
49 #include <fs/nfs/nfsport.h>
50 #include <fs/nfsclient/nfs.h>
51 #include <sys/extattr.h>
52 #include <sys/sysctl.h>
53 #include <sys/taskqueue.h>
55 SYSCTL_DECL(_vfs_nfs);
57 static int nfsignore_eexist = 0;
58 SYSCTL_INT(_vfs_nfs, OID_AUTO, ignore_eexist, CTLFLAG_RW,
59 &nfsignore_eexist, 0, "NFS ignore EEXIST replies for mkdir/symlink");
61 static int nfscl_dssameconn = 0;
62 SYSCTL_INT(_vfs_nfs, OID_AUTO, dssameconn, CTLFLAG_RW,
63 &nfscl_dssameconn, 0, "Use same TCP connection to multiple DSs");
68 extern int nfs_numnfscbd;
69 extern struct timeval nfsboottime;
70 extern u_int32_t newnfs_false, newnfs_true;
71 extern nfstype nfsv34_type[9];
72 extern int nfsrv_useacl;
73 extern char nfsv4_callbackaddr[INET6_ADDRSTRLEN];
74 extern int nfscl_debuglevel;
75 extern int nfs_pnfsiothreads;
76 extern u_long sb_max_adj;
77 extern int nfs_maxcopyrange;
79 int nfstest_outofseq = 0;
80 int nfscl_assumeposixlocks = 1;
81 int nfscl_enablecallb = 0;
82 short nfsv4_cbport = NFSV4_CBPORT;
83 int nfstest_openallsetattr = 0;
85 #define DIRHDSIZ offsetof(struct dirent, d_name)
88 * nfscl_getsameserver() can return one of three values:
89 * NFSDSP_USETHISSESSION - Use this session for the DS.
90 * NFSDSP_SEQTHISSESSION - Use the nfsclds_sequence field of this dsp for new
92 * NFSDSP_NOTFOUND - No matching server was found.
95 NFSDSP_USETHISSESSION = 0,
96 NFSDSP_SEQTHISSESSION = 1,
101 * Do a write RPC on a DS data file, using this structure for the arguments,
102 * so that this function can be executed by a separate kernel process.
104 struct nfsclwritedsdorpc {
111 nfsv4stateid_t *stateidp;
127 static int nfsrpc_setattrrpc(vnode_t , struct vattr *, nfsv4stateid_t *,
128 struct ucred *, NFSPROC_T *, struct nfsvattr *, int *, void *);
129 static int nfsrpc_readrpc(vnode_t , struct uio *, struct ucred *,
130 nfsv4stateid_t *, NFSPROC_T *, struct nfsvattr *, int *, void *);
131 static int nfsrpc_writerpc(vnode_t , struct uio *, int *, int *,
132 struct ucred *, nfsv4stateid_t *, NFSPROC_T *, struct nfsvattr *, int *,
134 static int nfsrpc_createv23(vnode_t , char *, int, struct vattr *,
135 nfsquad_t, int, struct ucred *, NFSPROC_T *, struct nfsvattr *,
136 struct nfsvattr *, struct nfsfh **, int *, int *, void *);
137 static int nfsrpc_createv4(vnode_t , char *, int, struct vattr *,
138 nfsquad_t, int, struct nfsclowner *, struct nfscldeleg **, struct ucred *,
139 NFSPROC_T *, struct nfsvattr *, struct nfsvattr *, struct nfsfh **, int *,
140 int *, void *, int *);
141 static int nfsrpc_locku(struct nfsrv_descript *, struct nfsmount *,
142 struct nfscllockowner *, u_int64_t, u_int64_t,
143 u_int32_t, struct ucred *, NFSPROC_T *, int);
144 static int nfsrpc_setaclrpc(vnode_t, struct ucred *, NFSPROC_T *,
145 struct acl *, nfsv4stateid_t *, void *);
146 static int nfsrpc_getlayout(struct nfsmount *, vnode_t, struct nfsfh *, int,
147 uint32_t *, nfsv4stateid_t *, uint64_t, struct nfscllayout **,
148 struct ucred *, NFSPROC_T *);
149 static int nfsrpc_fillsa(struct nfsmount *, struct sockaddr_in *,
150 struct sockaddr_in6 *, sa_family_t, int, int, struct nfsclds **,
152 static void nfscl_initsessionslots(struct nfsclsession *);
153 static int nfscl_doflayoutio(vnode_t, struct uio *, int *, int *, int *,
154 nfsv4stateid_t *, int, struct nfscldevinfo *, struct nfscllayout *,
155 struct nfsclflayout *, uint64_t, uint64_t, int, struct ucred *,
157 static int nfscl_dofflayoutio(vnode_t, struct uio *, int *, int *, int *,
158 nfsv4stateid_t *, int, struct nfscldevinfo *, struct nfscllayout *,
159 struct nfsclflayout *, uint64_t, uint64_t, int, int, struct mbuf *,
160 struct nfsclwritedsdorpc *, struct ucred *, NFSPROC_T *);
161 static struct mbuf *nfsm_copym(struct mbuf *, int, int);
162 static int nfsrpc_readds(vnode_t, struct uio *, nfsv4stateid_t *, int *,
163 struct nfsclds *, uint64_t, int, struct nfsfh *, int, int, int,
164 struct ucred *, NFSPROC_T *);
165 static int nfsrpc_writeds(vnode_t, struct uio *, int *, int *,
166 nfsv4stateid_t *, struct nfsclds *, uint64_t, int,
167 struct nfsfh *, int, int, int, int, struct ucred *, NFSPROC_T *);
168 static int nfsio_writedsmir(vnode_t, int *, int *, nfsv4stateid_t *,
169 struct nfsclds *, uint64_t, int, struct nfsfh *, struct mbuf *, int, int,
170 struct nfsclwritedsdorpc *, struct ucred *, NFSPROC_T *);
171 static int nfsrpc_writedsmir(vnode_t, int *, int *, nfsv4stateid_t *,
172 struct nfsclds *, uint64_t, int, struct nfsfh *, struct mbuf *, int, int,
173 struct ucred *, NFSPROC_T *);
174 static enum nfsclds_state nfscl_getsameserver(struct nfsmount *,
175 struct nfsclds *, struct nfsclds **, uint32_t *);
176 static int nfsio_commitds(vnode_t, uint64_t, int, struct nfsclds *,
177 struct nfsfh *, int, int, struct nfsclwritedsdorpc *, struct ucred *,
179 static int nfsrpc_commitds(vnode_t, uint64_t, int, struct nfsclds *,
180 struct nfsfh *, int, int, struct ucred *, NFSPROC_T *);
182 static int nfsio_adviseds(vnode_t, uint64_t, int, int, struct nfsclds *,
183 struct nfsfh *, int, int, struct nfsclwritedsdorpc *, struct ucred *,
185 static int nfsrpc_adviseds(vnode_t, uint64_t, int, int, struct nfsclds *,
186 struct nfsfh *, int, int, struct ucred *, NFSPROC_T *);
188 static int nfsrpc_allocaterpc(vnode_t, off_t, off_t, nfsv4stateid_t *,
189 struct nfsvattr *, int *, struct ucred *, NFSPROC_T *, void *);
190 static void nfsrv_setuplayoutget(struct nfsrv_descript *, int, uint64_t,
191 uint64_t, uint64_t, nfsv4stateid_t *, int, int, int);
192 static int nfsrv_parseug(struct nfsrv_descript *, int, uid_t *, gid_t *,
194 static int nfsrv_parselayoutget(struct nfsmount *, struct nfsrv_descript *,
195 nfsv4stateid_t *, int *, struct nfsclflayouthead *);
196 static int nfsrpc_getopenlayout(struct nfsmount *, vnode_t, u_int8_t *,
197 int, uint8_t *, int, uint32_t, struct nfsclopen *, uint8_t *, int,
198 struct nfscldeleg **, struct ucred *, NFSPROC_T *);
199 static int nfsrpc_getcreatelayout(vnode_t, char *, int, struct vattr *,
200 nfsquad_t, int, struct nfsclowner *, struct nfscldeleg **,
201 struct ucred *, NFSPROC_T *, struct nfsvattr *, struct nfsvattr *,
202 struct nfsfh **, int *, int *, void *, int *);
203 static int nfsrpc_openlayoutrpc(struct nfsmount *, vnode_t, u_int8_t *,
204 int, uint8_t *, int, uint32_t, struct nfsclopen *, uint8_t *, int,
205 struct nfscldeleg **, nfsv4stateid_t *, int, int, int, int *,
206 struct nfsclflayouthead *, int *, struct ucred *, NFSPROC_T *);
207 static int nfsrpc_createlayout(vnode_t, char *, int, struct vattr *,
208 nfsquad_t, int, struct nfsclowner *, struct nfscldeleg **,
209 struct ucred *, NFSPROC_T *, struct nfsvattr *, struct nfsvattr *,
210 struct nfsfh **, int *, int *, void *, int *, nfsv4stateid_t *,
211 int, int, int, int *, struct nfsclflayouthead *, int *);
212 static int nfsrpc_layoutget(struct nfsmount *, uint8_t *, int, int, uint64_t,
213 uint64_t, uint64_t, int, int, nfsv4stateid_t *, int *,
214 struct nfsclflayouthead *, struct ucred *, NFSPROC_T *, void *);
215 static int nfsrpc_layoutgetres(struct nfsmount *, vnode_t, uint8_t *,
216 int, nfsv4stateid_t *, int, uint32_t *, struct nfscllayout **,
217 struct nfsclflayouthead *, int, int, int *, struct ucred *, NFSPROC_T *);
218 static int nfsrpc_copyrpc(vnode_t, off_t, vnode_t, off_t, size_t *,
219 nfsv4stateid_t *, nfsv4stateid_t *, struct nfsvattr *, int *,
220 struct nfsvattr *, int *, bool, int *, struct ucred *, NFSPROC_T *);
221 static int nfsrpc_seekrpc(vnode_t, off_t *, nfsv4stateid_t *, bool *,
222 int, struct nfsvattr *, int *, struct ucred *);
224 int nfs_pnfsio(task_fn_t *, void *);
227 * nfs null call from vfs.
230 nfsrpc_null(vnode_t vp, struct ucred *cred, NFSPROC_T *p)
233 struct nfsrv_descript nfsd, *nd = &nfsd;
235 NFSCL_REQSTART(nd, NFSPROC_NULL, vp);
236 error = nfscl_request(nd, vp, p, cred, NULL);
237 if (nd->nd_repstat && !error)
238 error = nd->nd_repstat;
239 m_freem(nd->nd_mrep);
245 * For nfs version 3 and 4, use the access rpc to check accessibility. If file
246 * modes are changed on the server, accesses might still fail later.
249 nfsrpc_access(vnode_t vp, int acmode, struct ucred *cred,
250 NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp)
253 u_int32_t mode, rmode;
256 mode = NFSACCESS_READ;
259 if (vnode_vtype(vp) == VDIR) {
261 mode |= (NFSACCESS_MODIFY | NFSACCESS_EXTEND |
264 mode |= NFSACCESS_LOOKUP;
267 mode |= (NFSACCESS_MODIFY | NFSACCESS_EXTEND);
269 mode |= NFSACCESS_EXECUTE;
273 * Now, just call nfsrpc_accessrpc() to do the actual RPC.
275 error = nfsrpc_accessrpc(vp, mode, cred, p, nap, attrflagp, &rmode,
279 * The NFS V3 spec does not clarify whether or not
280 * the returned access bits can be a superset of
281 * the ones requested, so...
283 if (!error && (rmode & mode) != mode)
289 * The actual rpc, separated out for Darwin.
292 nfsrpc_accessrpc(vnode_t vp, u_int32_t mode, struct ucred *cred,
293 NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, u_int32_t *rmodep,
297 u_int32_t supported, rmode;
299 struct nfsrv_descript nfsd, *nd = &nfsd;
300 nfsattrbit_t attrbits;
304 NFSCL_REQSTART(nd, NFSPROC_ACCESS, vp);
305 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
306 *tl = txdr_unsigned(mode);
307 if (nd->nd_flag & ND_NFSV4) {
309 * And do a Getattr op.
311 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
312 *tl = txdr_unsigned(NFSV4OP_GETATTR);
313 NFSGETATTR_ATTRBIT(&attrbits);
314 (void) nfsrv_putattrbit(nd, &attrbits);
316 error = nfscl_request(nd, vp, p, cred, stuff);
319 if (nd->nd_flag & ND_NFSV3) {
320 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
324 if (!nd->nd_repstat) {
325 if (nd->nd_flag & ND_NFSV4) {
326 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
327 supported = fxdr_unsigned(u_int32_t, *tl++);
329 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
331 rmode = fxdr_unsigned(u_int32_t, *tl);
332 if (nd->nd_flag & ND_NFSV4)
333 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
336 * It's not obvious what should be done about
337 * unsupported access modes. For now, be paranoid
338 * and clear the unsupported ones.
343 error = nd->nd_repstat;
345 m_freem(nd->nd_mrep);
353 nfsrpc_open(vnode_t vp, int amode, struct ucred *cred, NFSPROC_T *p)
355 struct nfsclopen *op;
356 struct nfscldeleg *dp;
358 struct nfsnode *np = VTONFS(vp);
359 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
360 u_int32_t mode, clidrev;
361 int ret, newone, error, expireret = 0, retrycnt;
364 * For NFSv4, Open Ops are only done on Regular Files.
366 if (vnode_vtype(vp) != VREG)
370 mode |= NFSV4OPEN_ACCESSREAD;
372 mode |= NFSV4OPEN_ACCESSWRITE;
377 { char name[100]; int namel;
378 namel = (np->n_v4->n4_namelen < 100) ? np->n_v4->n4_namelen : 99;
379 bcopy(NFS4NODENAME(np->n_v4), name, namel);
381 printf("rpcopen p=0x%x name=%s",p->p_pid,name);
382 if (nfhp->nfh_len > 0) printf(" fh=0x%x\n",nfhp->nfh_fh[12]);
383 else printf(" fhl=0\n");
388 error = nfscl_open(vp, nfhp->nfh_fh, nfhp->nfh_len, mode, 1,
389 cred, p, NULL, &op, &newone, &ret, 1);
393 if (nmp->nm_clp != NULL)
394 clidrev = nmp->nm_clp->nfsc_clientidrev;
397 if (ret == NFSCLOPEN_DOOPEN) {
398 if (np->n_v4 != NULL) {
400 * For the first attempt, try and get a layout, if
401 * pNFS is enabled for the mount.
403 if (!NFSHASPNFS(nmp) || nfscl_enablecallb == 0 ||
404 nfs_numnfscbd == 0 ||
405 (np->n_flag & NNOLAYOUT) != 0 || retrycnt > 0)
406 error = nfsrpc_openrpc(nmp, vp,
408 np->n_v4->n4_fhlen, np->n_fhp->nfh_fh,
409 np->n_fhp->nfh_len, mode, op,
410 NFS4NODENAME(np->n_v4),
411 np->n_v4->n4_namelen,
412 &dp, 0, 0x0, cred, p, 0, 0);
414 error = nfsrpc_getopenlayout(nmp, vp,
416 np->n_v4->n4_fhlen, np->n_fhp->nfh_fh,
417 np->n_fhp->nfh_len, mode, op,
418 NFS4NODENAME(np->n_v4),
419 np->n_v4->n4_namelen, &dp, cred, p);
422 OSBitAndAtomic((int32_t)~NDELEGMOD, (UInt32 *)&np->n_flag);
425 np->n_flag &= ~NDELEGMOD;
427 * Invalidate the attribute cache, so that
428 * attributes that pre-date the issue of a
429 * delegation are not cached, since the
430 * cached attributes will remain valid while
431 * the delegation is held.
433 NFSINVALATTRCACHE(np);
436 (void) nfscl_deleg(nmp->nm_mountp,
437 op->nfso_own->nfsow_clp,
438 nfhp->nfh_fh, nfhp->nfh_len, cred, p, &dp);
443 newnfs_copyincred(cred, &op->nfso_cred);
444 } else if (ret == NFSCLOPEN_SETCRED)
446 * This is a new local open on a delegation. It needs
447 * to have credentials so that an open can be done
448 * against the server during recovery.
450 newnfs_copyincred(cred, &op->nfso_cred);
453 * nfso_opencnt is the count of how many VOP_OPEN()s have
454 * been done on this Open successfully and a VOP_CLOSE()
455 * is expected for each of these.
456 * If error is non-zero, don't increment it, since the Open
457 * hasn't succeeded yet.
461 nfscl_openrelease(nmp, op, error, newone);
462 if (error == NFSERR_GRACE || error == NFSERR_STALECLIENTID ||
463 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
464 error == NFSERR_BADSESSION) {
465 (void) nfs_catnap(PZERO, error, "nfs_open");
466 } else if ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID)
468 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
471 } while (error == NFSERR_GRACE || error == NFSERR_STALECLIENTID ||
472 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
473 error == NFSERR_BADSESSION ||
474 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
475 expireret == 0 && clidrev != 0 && retrycnt < 4));
476 if (error && retrycnt >= 4)
482 * the actual open rpc
485 nfsrpc_openrpc(struct nfsmount *nmp, vnode_t vp, u_int8_t *nfhp, int fhlen,
486 u_int8_t *newfhp, int newfhlen, u_int32_t mode, struct nfsclopen *op,
487 u_int8_t *name, int namelen, struct nfscldeleg **dpp,
488 int reclaim, u_int32_t delegtype, struct ucred *cred, NFSPROC_T *p,
489 int syscred, int recursed)
492 struct nfsrv_descript nfsd, *nd = &nfsd;
493 struct nfscldeleg *dp, *ndp = NULL;
494 struct nfsvattr nfsva;
495 u_int32_t rflags, deleg;
496 nfsattrbit_t attrbits;
497 int error, ret, acesize, limitby;
498 struct nfsclsession *tsep;
502 nfscl_reqstart(nd, NFSPROC_OPEN, nmp, nfhp, fhlen, NULL, NULL, 0, 0,
504 NFSM_BUILD(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
505 *tl++ = txdr_unsigned(op->nfso_own->nfsow_seqid);
506 *tl++ = txdr_unsigned(mode & NFSV4OPEN_ACCESSBOTH);
507 *tl++ = txdr_unsigned((mode >> NFSLCK_SHIFT) & NFSV4OPEN_DENYBOTH);
508 tsep = nfsmnt_mdssession(nmp);
509 *tl++ = tsep->nfsess_clientid.lval[0];
510 *tl = tsep->nfsess_clientid.lval[1];
511 (void) nfsm_strtom(nd, op->nfso_own->nfsow_owner, NFSV4CL_LOCKNAMELEN);
512 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
513 *tl++ = txdr_unsigned(NFSV4OPEN_NOCREATE);
515 *tl = txdr_unsigned(NFSV4OPEN_CLAIMPREVIOUS);
516 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
517 *tl = txdr_unsigned(delegtype);
520 *tl = txdr_unsigned(NFSV4OPEN_CLAIMDELEGATECUR);
521 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID);
522 if (NFSHASNFSV4N(nmp))
525 *tl++ = dp->nfsdl_stateid.seqid;
526 *tl++ = dp->nfsdl_stateid.other[0];
527 *tl++ = dp->nfsdl_stateid.other[1];
528 *tl = dp->nfsdl_stateid.other[2];
530 *tl = txdr_unsigned(NFSV4OPEN_CLAIMNULL);
532 (void) nfsm_strtom(nd, name, namelen);
534 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
535 *tl = txdr_unsigned(NFSV4OP_GETATTR);
536 NFSZERO_ATTRBIT(&attrbits);
537 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_CHANGE);
538 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEMODIFY);
539 (void) nfsrv_putattrbit(nd, &attrbits);
541 nd->nd_flag |= ND_USEGSSNAME;
542 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, vp, p, cred,
543 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
546 NFSCL_INCRSEQID(op->nfso_own->nfsow_seqid, nd);
547 if (!nd->nd_repstat) {
548 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
550 op->nfso_stateid.seqid = *tl++;
551 op->nfso_stateid.other[0] = *tl++;
552 op->nfso_stateid.other[1] = *tl++;
553 op->nfso_stateid.other[2] = *tl;
554 rflags = fxdr_unsigned(u_int32_t, *(tl + 6));
555 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
558 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
559 deleg = fxdr_unsigned(u_int32_t, *tl);
560 if (deleg == NFSV4OPEN_DELEGATEREAD ||
561 deleg == NFSV4OPEN_DELEGATEWRITE) {
562 if (!(op->nfso_own->nfsow_clp->nfsc_flags &
563 NFSCLFLAGS_FIRSTDELEG))
564 op->nfso_own->nfsow_clp->nfsc_flags |=
565 (NFSCLFLAGS_FIRSTDELEG | NFSCLFLAGS_GOTDELEG);
567 sizeof (struct nfscldeleg) + newfhlen,
568 M_NFSCLDELEG, M_WAITOK);
569 LIST_INIT(&ndp->nfsdl_owner);
570 LIST_INIT(&ndp->nfsdl_lock);
571 ndp->nfsdl_clp = op->nfso_own->nfsow_clp;
572 ndp->nfsdl_fhlen = newfhlen;
573 NFSBCOPY(newfhp, ndp->nfsdl_fh, newfhlen);
574 newnfs_copyincred(cred, &ndp->nfsdl_cred);
575 nfscl_lockinit(&ndp->nfsdl_rwlock);
576 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
578 ndp->nfsdl_stateid.seqid = *tl++;
579 ndp->nfsdl_stateid.other[0] = *tl++;
580 ndp->nfsdl_stateid.other[1] = *tl++;
581 ndp->nfsdl_stateid.other[2] = *tl++;
582 ret = fxdr_unsigned(int, *tl);
583 if (deleg == NFSV4OPEN_DELEGATEWRITE) {
584 ndp->nfsdl_flags = NFSCLDL_WRITE;
586 * Indicates how much the file can grow.
588 NFSM_DISSECT(tl, u_int32_t *,
590 limitby = fxdr_unsigned(int, *tl++);
592 case NFSV4OPEN_LIMITSIZE:
593 ndp->nfsdl_sizelimit = fxdr_hyper(tl);
595 case NFSV4OPEN_LIMITBLOCKS:
596 ndp->nfsdl_sizelimit =
597 fxdr_unsigned(u_int64_t, *tl++);
598 ndp->nfsdl_sizelimit *=
599 fxdr_unsigned(u_int64_t, *tl);
602 error = NFSERR_BADXDR;
606 ndp->nfsdl_flags = NFSCLDL_READ;
609 ndp->nfsdl_flags |= NFSCLDL_RECALL;
610 error = nfsrv_dissectace(nd, &ndp->nfsdl_ace, &ret,
614 } else if (deleg != NFSV4OPEN_DELEGATENONE) {
615 error = NFSERR_BADXDR;
618 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
619 error = nfsv4_loadattr(nd, NULL, &nfsva, NULL,
620 NULL, 0, NULL, NULL, NULL, NULL, NULL, 0,
621 NULL, NULL, NULL, p, cred);
625 ndp->nfsdl_change = nfsva.na_filerev;
626 ndp->nfsdl_modtime = nfsva.na_mtime;
627 ndp->nfsdl_flags |= NFSCLDL_MODTIMESET;
629 if (!reclaim && (rflags & NFSV4OPEN_RESULTCONFIRM)) {
631 ret = nfsrpc_openconfirm(vp, newfhp, newfhlen, op,
633 if (ret == NFSERR_DELAY)
634 (void) nfs_catnap(PZERO, ret, "nfs_open");
635 } while (ret == NFSERR_DELAY);
638 if ((rflags & NFSV4OPEN_LOCKTYPEPOSIX) ||
639 nfscl_assumeposixlocks)
640 op->nfso_posixlock = 1;
642 op->nfso_posixlock = 0;
645 * If the server is handing out delegations, but we didn't
646 * get one because an OpenConfirm was required, try the
647 * Open again, to get a delegation. This is a harmless no-op,
648 * from a server's point of view.
650 if (!reclaim && (rflags & NFSV4OPEN_RESULTCONFIRM) &&
651 (op->nfso_own->nfsow_clp->nfsc_flags & NFSCLFLAGS_GOTDELEG)
652 && !error && dp == NULL && ndp == NULL && !recursed) {
654 ret = nfsrpc_openrpc(nmp, vp, nfhp, fhlen, newfhp,
655 newfhlen, mode, op, name, namelen, &ndp, 0, 0x0,
656 cred, p, syscred, 1);
657 if (ret == NFSERR_DELAY)
658 (void) nfs_catnap(PZERO, ret, "nfs_open2");
659 } while (ret == NFSERR_DELAY);
662 free(ndp, M_NFSCLDELEG);
665 if (ret == NFSERR_STALECLIENTID ||
666 ret == NFSERR_STALEDONTRECOVER ||
667 ret == NFSERR_BADSESSION)
672 if (nd->nd_repstat != 0 && error == 0)
673 error = nd->nd_repstat;
674 if (error == NFSERR_STALECLIENTID)
675 nfscl_initiate_recovery(op->nfso_own->nfsow_clp);
679 else if (ndp != NULL)
680 free(ndp, M_NFSCLDELEG);
681 m_freem(nd->nd_mrep);
689 nfsrpc_opendowngrade(vnode_t vp, u_int32_t mode, struct nfsclopen *op,
690 struct ucred *cred, NFSPROC_T *p)
693 struct nfsrv_descript nfsd, *nd = &nfsd;
696 NFSCL_REQSTART(nd, NFSPROC_OPENDOWNGRADE, vp);
697 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID + 3 * NFSX_UNSIGNED);
698 if (NFSHASNFSV4N(VFSTONFS(vp->v_mount)))
701 *tl++ = op->nfso_stateid.seqid;
702 *tl++ = op->nfso_stateid.other[0];
703 *tl++ = op->nfso_stateid.other[1];
704 *tl++ = op->nfso_stateid.other[2];
705 *tl++ = txdr_unsigned(op->nfso_own->nfsow_seqid);
706 *tl++ = txdr_unsigned(mode & NFSV4OPEN_ACCESSBOTH);
707 *tl = txdr_unsigned((mode >> NFSLCK_SHIFT) & NFSV4OPEN_DENYBOTH);
708 error = nfscl_request(nd, vp, p, cred, NULL);
711 NFSCL_INCRSEQID(op->nfso_own->nfsow_seqid, nd);
712 if (!nd->nd_repstat) {
713 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID);
714 op->nfso_stateid.seqid = *tl++;
715 op->nfso_stateid.other[0] = *tl++;
716 op->nfso_stateid.other[1] = *tl++;
717 op->nfso_stateid.other[2] = *tl;
719 if (nd->nd_repstat && error == 0)
720 error = nd->nd_repstat;
721 if (error == NFSERR_STALESTATEID)
722 nfscl_initiate_recovery(op->nfso_own->nfsow_clp);
724 m_freem(nd->nd_mrep);
729 * V4 Close operation.
732 nfsrpc_close(vnode_t vp, int doclose, NFSPROC_T *p)
734 struct nfsclclient *clp;
737 if (vnode_vtype(vp) != VREG)
740 error = nfscl_doclose(vp, &clp, p);
742 error = nfscl_getclose(vp, &clp);
746 nfscl_clientrelease(clp);
754 nfsrpc_doclose(struct nfsmount *nmp, struct nfsclopen *op, NFSPROC_T *p)
756 struct nfsrv_descript nfsd, *nd = &nfsd;
757 struct nfscllockowner *lp, *nlp;
758 struct nfscllock *lop, *nlop;
760 u_int64_t off = 0, len = 0;
761 u_int32_t type = NFSV4LOCKT_READ;
762 int error, do_unlock, trycnt;
764 tcred = newnfs_getcred();
765 newnfs_copycred(&op->nfso_cred, tcred);
767 * (Theoretically this could be done in the same
768 * compound as the close, but having multiple
769 * sequenced Ops in the same compound might be
770 * too scary for some servers.)
772 if (op->nfso_posixlock) {
775 type = NFSV4LOCKT_READ;
779 * Since this function is only called from VOP_INACTIVE(), no
780 * other thread will be manipulating this Open. As such, the
781 * lock lists are not being changed by other threads, so it should
782 * be safe to do this without locking.
784 LIST_FOREACH(lp, &op->nfso_lock, nfsl_list) {
786 LIST_FOREACH_SAFE(lop, &lp->nfsl_lock, nfslo_list, nlop) {
787 if (op->nfso_posixlock == 0) {
788 off = lop->nfslo_first;
789 len = lop->nfslo_end - lop->nfslo_first;
790 if (lop->nfslo_type == F_WRLCK)
791 type = NFSV4LOCKT_WRITE;
793 type = NFSV4LOCKT_READ;
798 error = nfsrpc_locku(nd, nmp, lp, off,
799 len, type, tcred, p, 0);
800 if ((nd->nd_repstat == NFSERR_GRACE ||
801 nd->nd_repstat == NFSERR_DELAY) &&
803 (void) nfs_catnap(PZERO,
806 } while ((nd->nd_repstat == NFSERR_GRACE ||
807 nd->nd_repstat == NFSERR_DELAY) &&
808 error == 0 && trycnt++ < 5);
809 if (op->nfso_posixlock)
812 nfscl_freelock(lop, 0);
815 * Do a ReleaseLockOwner.
816 * The lock owner name nfsl_owner may be used by other opens for
817 * other files but the lock_owner4 name that nfsrpc_rellockown()
818 * puts on the wire has the file handle for this file appended
819 * to it, so it can be done now.
821 (void)nfsrpc_rellockown(nmp, lp, lp->nfsl_open->nfso_fh,
822 lp->nfsl_open->nfso_fhlen, tcred, p);
826 * There could be other Opens for different files on the same
827 * OpenOwner, so locking is required.
830 nfscl_lockexcl(&op->nfso_own->nfsow_rwlock, NFSCLSTATEMUTEXPTR);
833 error = nfscl_tryclose(op, tcred, nmp, p);
834 if (error == NFSERR_GRACE)
835 (void) nfs_catnap(PZERO, error, "nfs_close");
836 } while (error == NFSERR_GRACE);
838 nfscl_lockunlock(&op->nfso_own->nfsow_rwlock);
840 LIST_FOREACH_SAFE(lp, &op->nfso_lock, nfsl_list, nlp)
841 nfscl_freelockowner(lp, 0);
842 nfscl_freeopen(op, 0);
848 * The actual Close RPC.
851 nfsrpc_closerpc(struct nfsrv_descript *nd, struct nfsmount *nmp,
852 struct nfsclopen *op, struct ucred *cred, NFSPROC_T *p,
858 nfscl_reqstart(nd, NFSPROC_CLOSE, nmp, op->nfso_fh,
859 op->nfso_fhlen, NULL, NULL, 0, 0, false);
860 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED + NFSX_STATEID);
861 *tl++ = txdr_unsigned(op->nfso_own->nfsow_seqid);
862 if (NFSHASNFSV4N(nmp))
865 *tl++ = op->nfso_stateid.seqid;
866 *tl++ = op->nfso_stateid.other[0];
867 *tl++ = op->nfso_stateid.other[1];
868 *tl = op->nfso_stateid.other[2];
870 nd->nd_flag |= ND_USEGSSNAME;
871 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
872 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
875 NFSCL_INCRSEQID(op->nfso_own->nfsow_seqid, nd);
876 if (nd->nd_repstat == 0)
877 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID);
878 error = nd->nd_repstat;
879 if (error == NFSERR_STALESTATEID)
880 nfscl_initiate_recovery(op->nfso_own->nfsow_clp);
882 m_freem(nd->nd_mrep);
887 * V4 Open Confirm RPC.
890 nfsrpc_openconfirm(vnode_t vp, u_int8_t *nfhp, int fhlen,
891 struct nfsclopen *op, struct ucred *cred, NFSPROC_T *p)
894 struct nfsrv_descript nfsd, *nd = &nfsd;
895 struct nfsmount *nmp;
898 nmp = VFSTONFS(vp->v_mount);
899 if (NFSHASNFSV4N(nmp))
900 return (0); /* No confirmation for NFSv4.1. */
901 nfscl_reqstart(nd, NFSPROC_OPENCONFIRM, nmp, nfhp, fhlen, NULL, NULL,
903 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED + NFSX_STATEID);
904 *tl++ = op->nfso_stateid.seqid;
905 *tl++ = op->nfso_stateid.other[0];
906 *tl++ = op->nfso_stateid.other[1];
907 *tl++ = op->nfso_stateid.other[2];
908 *tl = txdr_unsigned(op->nfso_own->nfsow_seqid);
909 error = nfscl_request(nd, vp, p, cred, NULL);
912 NFSCL_INCRSEQID(op->nfso_own->nfsow_seqid, nd);
913 if (!nd->nd_repstat) {
914 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID);
915 op->nfso_stateid.seqid = *tl++;
916 op->nfso_stateid.other[0] = *tl++;
917 op->nfso_stateid.other[1] = *tl++;
918 op->nfso_stateid.other[2] = *tl;
920 error = nd->nd_repstat;
921 if (error == NFSERR_STALESTATEID)
922 nfscl_initiate_recovery(op->nfso_own->nfsow_clp);
924 m_freem(nd->nd_mrep);
929 * Do the setclientid and setclientid confirm RPCs. Called from nfs_statfs()
930 * when a mount has just occurred and when the server replies NFSERR_EXPIRED.
933 nfsrpc_setclient(struct nfsmount *nmp, struct nfsclclient *clp, int reclaim,
934 bool *retokp, struct ucred *cred, NFSPROC_T *p)
937 struct nfsrv_descript nfsd;
938 struct nfsrv_descript *nd = &nfsd;
939 nfsattrbit_t attrbits;
940 u_int8_t *cp = NULL, *cp2, addr[INET6_ADDRSTRLEN + 9];
942 int error, isinet6 = 0, callblen;
945 static u_int32_t rev = 0;
946 struct nfsclds *dsp, *odsp;
948 struct nfsclsession *tsep;
950 if (nfsboottime.tv_sec == 0)
951 NFSSETBOOTTIME(nfsboottime);
952 if (NFSHASNFSV4N(nmp)) {
953 error = NFSERR_BADSESSION;
955 if (retokp != NULL) {
957 odsp = TAILQ_FIRST(&nmp->nm_sess);
962 * When a session already exists, first try a
963 * CreateSession with the extant ClientID.
965 dsp = malloc(sizeof(struct nfsclds) +
966 odsp->nfsclds_servownlen + 1, M_NFSCLDS,
968 dsp->nfsclds_expire = NFSD_MONOSEC + clp->nfsc_renew;
969 dsp->nfsclds_servownlen = odsp->nfsclds_servownlen;
970 dsp->nfsclds_sess.nfsess_clientid =
971 odsp->nfsclds_sess.nfsess_clientid;
972 dsp->nfsclds_sess.nfsess_sequenceid =
973 odsp->nfsclds_sess.nfsess_sequenceid;
974 dsp->nfsclds_flags = odsp->nfsclds_flags;
975 if (dsp->nfsclds_servownlen > 0)
976 memcpy(dsp->nfsclds_serverown,
977 odsp->nfsclds_serverown,
978 dsp->nfsclds_servownlen + 1);
979 mtx_init(&dsp->nfsclds_mtx, "nfsds", NULL, MTX_DEF);
980 mtx_init(&dsp->nfsclds_sess.nfsess_mtx, "nfssession",
982 nfscl_initsessionslots(&dsp->nfsclds_sess);
983 error = nfsrpc_createsession(nmp, &dsp->nfsclds_sess,
984 &nmp->nm_sockreq, NULL,
985 dsp->nfsclds_sess.nfsess_sequenceid, 1, cred, p);
986 NFSCL_DEBUG(1, "create session for extant "
987 "ClientID=%d\n", error);
989 nfscl_freenfsclds(dsp);
992 * If *retokp is true, return any error other
993 * than NFSERR_STALECLIENTID,
994 * NFSERR_BADSESSION or NFSERR_STALEDONTRECOVER
995 * so that nfscl_recover() will not loop.
1001 } else if (retokp != NULL && *retokp)
1005 * Either there was no previous session or the
1006 * CreateSession attempt failed, so...
1007 * do an ExchangeID followed by the CreateSession.
1009 clp->nfsc_rev = rev++;
1010 error = nfsrpc_exchangeid(nmp, clp, &nmp->nm_sockreq, 0,
1011 NFSV4EXCH_USEPNFSMDS | NFSV4EXCH_USENONPNFS, &dsp,
1013 NFSCL_DEBUG(1, "aft exch=%d\n", error);
1015 error = nfsrpc_createsession(nmp,
1016 &dsp->nfsclds_sess, &nmp->nm_sockreq, NULL,
1017 dsp->nfsclds_sess.nfsess_sequenceid, 1,
1019 NFSCL_DEBUG(1, "aft createsess=%d\n", error);
1024 * The old sessions cannot be safely free'd
1025 * here, since they may still be used by
1029 if (TAILQ_FIRST(&nmp->nm_sess) != NULL)
1030 tsep = NFSMNT_MDSSESSION(nmp);
1031 TAILQ_INSERT_HEAD(&nmp->nm_sess, dsp,
1034 * Wake up RPCs waiting for a slot on the
1035 * old session. These will then fail with
1036 * NFSERR_BADSESSION and be retried with the
1037 * new session by nfsv4_setsequence().
1038 * Also wakeup() processes waiting for the
1042 wakeup(&tsep->nfsess_slots);
1043 wakeup(&nmp->nm_sess);
1045 } else if (dsp != NULL)
1046 nfscl_freenfsclds(dsp);
1047 if (error == 0 && reclaim == 0) {
1048 error = nfsrpc_reclaimcomplete(nmp, cred, p);
1049 NFSCL_DEBUG(1, "aft reclaimcomp=%d\n", error);
1050 if (error == NFSERR_COMPLETEALREADY ||
1051 error == NFSERR_NOTSUPP)
1052 /* Ignore this error. */
1056 } else if (retokp != NULL && *retokp)
1058 clp->nfsc_rev = rev++;
1061 * Allocate a single session structure for NFSv4.0, because some of
1062 * the fields are used by NFSv4.0 although it doesn't do a session.
1064 dsp = malloc(sizeof(struct nfsclds), M_NFSCLDS, M_WAITOK | M_ZERO);
1065 mtx_init(&dsp->nfsclds_mtx, "nfsds", NULL, MTX_DEF);
1066 mtx_init(&dsp->nfsclds_sess.nfsess_mtx, "nfssession", NULL, MTX_DEF);
1068 TAILQ_INSERT_HEAD(&nmp->nm_sess, dsp, nfsclds_list);
1069 tsep = NFSMNT_MDSSESSION(nmp);
1072 nfscl_reqstart(nd, NFSPROC_SETCLIENTID, nmp, NULL, 0, NULL, NULL, 0, 0,
1074 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1075 *tl++ = txdr_unsigned(nfsboottime.tv_sec);
1076 *tl = txdr_unsigned(clp->nfsc_rev);
1077 (void) nfsm_strtom(nd, clp->nfsc_id, clp->nfsc_idlen);
1080 * set up the callback address
1082 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1083 *tl = txdr_unsigned(NFS_CALLBCKPROG);
1084 callblen = strlen(nfsv4_callbackaddr);
1086 cp = nfscl_getmyip(nmp, &a6, &isinet6);
1087 if (nfscl_enablecallb && nfs_numnfscbd > 0 &&
1088 (callblen > 0 || cp != NULL)) {
1089 port = htons(nfsv4_cbport);
1090 cp2 = (u_int8_t *)&port;
1092 if ((callblen > 0 &&
1093 strchr(nfsv4_callbackaddr, ':')) || isinet6) {
1094 char ip6buf[INET6_ADDRSTRLEN], *ip6add;
1096 (void) nfsm_strtom(nd, "tcp6", 4);
1097 if (callblen == 0) {
1098 ip6_sprintf(ip6buf, (struct in6_addr *)cp);
1101 ip6add = nfsv4_callbackaddr;
1103 snprintf(addr, INET6_ADDRSTRLEN + 9, "%s.%d.%d",
1104 ip6add, cp2[0], cp2[1]);
1108 (void) nfsm_strtom(nd, "tcp", 3);
1110 snprintf(addr, INET6_ADDRSTRLEN + 9,
1111 "%d.%d.%d.%d.%d.%d", cp[0], cp[1],
1112 cp[2], cp[3], cp2[0], cp2[1]);
1114 snprintf(addr, INET6_ADDRSTRLEN + 9,
1115 "%s.%d.%d", nfsv4_callbackaddr,
1118 (void) nfsm_strtom(nd, addr, strlen(addr));
1120 (void) nfsm_strtom(nd, "tcp", 3);
1121 (void) nfsm_strtom(nd, "0.0.0.0.0.0", 11);
1123 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1124 *tl = txdr_unsigned(clp->nfsc_cbident);
1125 nd->nd_flag |= ND_USEGSSNAME;
1126 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
1127 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
1130 if (nd->nd_repstat == 0) {
1131 NFSM_DISSECT(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1132 tsep->nfsess_clientid.lval[0] = *tl++;
1133 tsep->nfsess_clientid.lval[1] = *tl++;
1134 confirm.lval[0] = *tl++;
1135 confirm.lval[1] = *tl;
1136 m_freem(nd->nd_mrep);
1142 nfscl_reqstart(nd, NFSPROC_SETCLIENTIDCFRM, nmp, NULL, 0, NULL,
1144 NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1145 *tl++ = tsep->nfsess_clientid.lval[0];
1146 *tl++ = tsep->nfsess_clientid.lval[1];
1147 *tl++ = confirm.lval[0];
1148 *tl = confirm.lval[1];
1149 nd->nd_flag |= ND_USEGSSNAME;
1150 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p,
1151 cred, NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
1154 m_freem(nd->nd_mrep);
1156 if (nd->nd_repstat == 0) {
1157 nfscl_reqstart(nd, NFSPROC_GETATTR, nmp, nmp->nm_fh,
1158 nmp->nm_fhsize, NULL, NULL, 0, 0, false);
1159 NFSZERO_ATTRBIT(&attrbits);
1160 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_LEASETIME);
1161 (void) nfsrv_putattrbit(nd, &attrbits);
1162 nd->nd_flag |= ND_USEGSSNAME;
1163 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p,
1164 cred, NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
1167 if (nd->nd_repstat == 0) {
1168 error = nfsv4_loadattr(nd, NULL, NULL, NULL, NULL, 0, NULL,
1169 NULL, NULL, NULL, NULL, 0, NULL, &lease, NULL, p, cred);
1172 clp->nfsc_renew = NFSCL_RENEW(lease);
1173 clp->nfsc_expire = NFSD_MONOSEC + clp->nfsc_renew;
1174 clp->nfsc_clientidrev++;
1175 if (clp->nfsc_clientidrev == 0)
1176 clp->nfsc_clientidrev++;
1180 error = nd->nd_repstat;
1182 m_freem(nd->nd_mrep);
1190 nfsrpc_getattr(vnode_t vp, struct ucred *cred, NFSPROC_T *p,
1191 struct nfsvattr *nap, void *stuff)
1193 struct nfsrv_descript nfsd, *nd = &nfsd;
1195 nfsattrbit_t attrbits;
1197 NFSCL_REQSTART(nd, NFSPROC_GETATTR, vp);
1198 if (nd->nd_flag & ND_NFSV4) {
1199 NFSGETATTR_ATTRBIT(&attrbits);
1200 (void) nfsrv_putattrbit(nd, &attrbits);
1202 error = nfscl_request(nd, vp, p, cred, stuff);
1205 if (!nd->nd_repstat)
1206 error = nfsm_loadattr(nd, nap);
1208 error = nd->nd_repstat;
1209 m_freem(nd->nd_mrep);
1214 * nfs getattr call with non-vnode arguemnts.
1217 nfsrpc_getattrnovp(struct nfsmount *nmp, u_int8_t *fhp, int fhlen, int syscred,
1218 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, u_int64_t *xidp,
1221 struct nfsrv_descript nfsd, *nd = &nfsd;
1222 int error, vers = NFS_VER2;
1223 nfsattrbit_t attrbits;
1225 nfscl_reqstart(nd, NFSPROC_GETATTR, nmp, fhp, fhlen, NULL, NULL, 0, 0,
1227 if (nd->nd_flag & ND_NFSV4) {
1229 NFSGETATTR_ATTRBIT(&attrbits);
1230 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_LEASETIME);
1231 (void) nfsrv_putattrbit(nd, &attrbits);
1232 } else if (nd->nd_flag & ND_NFSV3) {
1236 nd->nd_flag |= ND_USEGSSNAME;
1237 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
1238 NFS_PROG, vers, NULL, 1, xidp, NULL);
1241 if (nd->nd_repstat == 0) {
1242 if ((nd->nd_flag & ND_NFSV4) != 0)
1243 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0,
1244 NULL, NULL, NULL, NULL, NULL, 0, NULL, leasep, NULL,
1247 error = nfsm_loadattr(nd, nap);
1249 error = nd->nd_repstat;
1250 m_freem(nd->nd_mrep);
1255 * Do an nfs setattr operation.
1258 nfsrpc_setattr(vnode_t vp, struct vattr *vap, NFSACL_T *aclp,
1259 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *rnap, int *attrflagp,
1262 int error, expireret = 0, openerr, retrycnt;
1263 u_int32_t clidrev = 0, mode;
1264 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1266 nfsv4stateid_t stateid;
1269 if (nmp->nm_clp != NULL)
1270 clidrev = nmp->nm_clp->nfsc_clientidrev;
1271 if (vap != NULL && NFSATTRISSET(u_quad_t, vap, va_size))
1272 mode = NFSV4OPEN_ACCESSWRITE;
1274 mode = NFSV4OPEN_ACCESSREAD;
1279 if (NFSHASNFSV4(nmp)) {
1280 nfhp = VTONFS(vp)->n_fhp;
1281 error = nfscl_getstateid(vp, nfhp->nfh_fh,
1282 nfhp->nfh_len, mode, 0, cred, p, &stateid, &lckp);
1283 if (error && vnode_vtype(vp) == VREG &&
1284 (mode == NFSV4OPEN_ACCESSWRITE ||
1285 nfstest_openallsetattr)) {
1287 * No Open stateid, so try and open the file
1290 if (mode == NFSV4OPEN_ACCESSWRITE)
1291 openerr = nfsrpc_open(vp, FWRITE, cred,
1294 openerr = nfsrpc_open(vp, FREAD, cred,
1297 (void) nfscl_getstateid(vp,
1298 nfhp->nfh_fh, nfhp->nfh_len,
1299 mode, 0, cred, p, &stateid, &lckp);
1303 error = nfsrpc_setattrrpc(vp, vap, &stateid, cred, p,
1304 rnap, attrflagp, stuff);
1306 error = nfsrpc_setaclrpc(vp, cred, p, aclp, &stateid,
1308 if (error == NFSERR_OPENMODE && mode == NFSV4OPEN_ACCESSREAD) {
1310 nmp->nm_state |= NFSSTA_OPENMODE;
1313 if (error == NFSERR_STALESTATEID)
1314 nfscl_initiate_recovery(nmp->nm_clp);
1316 nfscl_lockderef(lckp);
1318 (void) nfsrpc_close(vp, 0, p);
1319 if (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
1320 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
1321 error == NFSERR_OLDSTATEID || error == NFSERR_BADSESSION) {
1322 (void) nfs_catnap(PZERO, error, "nfs_setattr");
1323 } else if ((error == NFSERR_EXPIRED ||
1324 error == NFSERR_BADSTATEID) && clidrev != 0) {
1325 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
1328 } while (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
1329 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
1330 error == NFSERR_BADSESSION ||
1331 (error == NFSERR_OLDSTATEID && retrycnt < 20) ||
1332 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
1333 expireret == 0 && clidrev != 0 && retrycnt < 4) ||
1334 (error == NFSERR_OPENMODE && mode == NFSV4OPEN_ACCESSREAD &&
1336 if (error && retrycnt >= 4)
1342 nfsrpc_setattrrpc(vnode_t vp, struct vattr *vap,
1343 nfsv4stateid_t *stateidp, struct ucred *cred, NFSPROC_T *p,
1344 struct nfsvattr *rnap, int *attrflagp, void *stuff)
1347 struct nfsrv_descript nfsd, *nd = &nfsd;
1349 nfsattrbit_t attrbits;
1352 NFSCL_REQSTART(nd, NFSPROC_SETATTR, vp);
1353 if (nd->nd_flag & ND_NFSV4)
1354 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID);
1355 vap->va_type = vnode_vtype(vp);
1356 nfscl_fillsattr(nd, vap, vp, NFSSATTR_FULL, 0);
1357 if (nd->nd_flag & ND_NFSV3) {
1358 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1360 } else if (nd->nd_flag & ND_NFSV4) {
1361 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1362 *tl = txdr_unsigned(NFSV4OP_GETATTR);
1363 NFSGETATTR_ATTRBIT(&attrbits);
1364 (void) nfsrv_putattrbit(nd, &attrbits);
1366 error = nfscl_request(nd, vp, p, cred, stuff);
1369 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4))
1370 error = nfscl_wcc_data(nd, vp, rnap, attrflagp, NULL, stuff);
1371 if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) == ND_NFSV4 && !error)
1372 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
1373 if (!(nd->nd_flag & ND_NFSV3) && !nd->nd_repstat && !error)
1374 error = nfscl_postop_attr(nd, rnap, attrflagp, stuff);
1375 m_freem(nd->nd_mrep);
1376 if (nd->nd_repstat && !error)
1377 error = nd->nd_repstat;
1385 nfsrpc_lookup(vnode_t dvp, char *name, int len, struct ucred *cred,
1386 NFSPROC_T *p, struct nfsvattr *dnap, struct nfsvattr *nap,
1387 struct nfsfh **nfhpp, int *attrflagp, int *dattrflagp, void *stuff)
1390 struct nfsrv_descript nfsd, *nd = &nfsd;
1391 struct nfsmount *nmp;
1394 nfsattrbit_t attrbits;
1395 int error = 0, lookupp = 0;
1399 if (vnode_vtype(dvp) != VDIR)
1401 nmp = VFSTONFS(dvp->v_mount);
1402 if (len > NFS_MAXNAMLEN)
1403 return (ENAMETOOLONG);
1404 if (NFSHASNFSV4(nmp) && len == 1 &&
1407 * Just return the current dir's fh.
1410 nfhp = malloc(sizeof (struct nfsfh) +
1411 np->n_fhp->nfh_len, M_NFSFH, M_WAITOK);
1412 nfhp->nfh_len = np->n_fhp->nfh_len;
1413 NFSBCOPY(np->n_fhp->nfh_fh, nfhp->nfh_fh, nfhp->nfh_len);
1417 if (NFSHASNFSV4(nmp) && len == 2 &&
1418 name[0] == '.' && name[1] == '.') {
1420 NFSCL_REQSTART(nd, NFSPROC_LOOKUPP, dvp);
1422 NFSCL_REQSTART(nd, NFSPROC_LOOKUP, dvp);
1423 (void) nfsm_strtom(nd, name, len);
1425 if (nd->nd_flag & ND_NFSV4) {
1426 NFSGETATTR_ATTRBIT(&attrbits);
1427 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1428 *tl++ = txdr_unsigned(NFSV4OP_GETFH);
1429 *tl = txdr_unsigned(NFSV4OP_GETATTR);
1430 (void) nfsrv_putattrbit(nd, &attrbits);
1432 error = nfscl_request(nd, dvp, p, cred, stuff);
1435 if (nd->nd_repstat) {
1437 * When an NFSv4 Lookupp returns ENOENT, it means that
1438 * the lookup is at the root of an fs, so return this dir.
1440 if (nd->nd_repstat == NFSERR_NOENT && lookupp) {
1442 nfhp = malloc(sizeof (struct nfsfh) +
1443 np->n_fhp->nfh_len, M_NFSFH, M_WAITOK);
1444 nfhp->nfh_len = np->n_fhp->nfh_len;
1445 NFSBCOPY(np->n_fhp->nfh_fh, nfhp->nfh_fh, nfhp->nfh_len);
1447 m_freem(nd->nd_mrep);
1450 if (nd->nd_flag & ND_NFSV3)
1451 error = nfscl_postop_attr(nd, dnap, dattrflagp, stuff);
1452 else if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) ==
1454 /* Load the directory attributes. */
1455 error = nfsm_loadattr(nd, dnap);
1461 if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) == ND_NFSV4) {
1462 /* Load the directory attributes. */
1463 error = nfsm_loadattr(nd, dnap);
1467 /* Skip over the Lookup and GetFH operation status values. */
1468 NFSM_DISSECT(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1470 error = nfsm_getfh(nd, nfhpp);
1474 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
1475 if ((nd->nd_flag & ND_NFSV3) && !error)
1476 error = nfscl_postop_attr(nd, dnap, dattrflagp, stuff);
1478 m_freem(nd->nd_mrep);
1479 if (!error && nd->nd_repstat)
1480 error = nd->nd_repstat;
1485 * Do a readlink rpc.
1488 nfsrpc_readlink(vnode_t vp, struct uio *uiop, struct ucred *cred,
1489 NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, void *stuff)
1492 struct nfsrv_descript nfsd, *nd = &nfsd;
1493 struct nfsnode *np = VTONFS(vp);
1494 nfsattrbit_t attrbits;
1495 int error, len, cangetattr = 1;
1498 NFSCL_REQSTART(nd, NFSPROC_READLINK, vp);
1499 if (nd->nd_flag & ND_NFSV4) {
1501 * And do a Getattr op.
1503 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1504 *tl = txdr_unsigned(NFSV4OP_GETATTR);
1505 NFSGETATTR_ATTRBIT(&attrbits);
1506 (void) nfsrv_putattrbit(nd, &attrbits);
1508 error = nfscl_request(nd, vp, p, cred, stuff);
1511 if (nd->nd_flag & ND_NFSV3)
1512 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
1513 if (!nd->nd_repstat && !error) {
1514 NFSM_STRSIZ(len, NFS_MAXPATHLEN);
1516 * This seems weird to me, but must have been added to
1517 * FreeBSD for some reason. The only thing I can think of
1518 * is that there was/is some server that replies with
1519 * more link data than it should?
1521 if (len == NFS_MAXPATHLEN) {
1523 if (np->n_size > 0 && np->n_size < NFS_MAXPATHLEN) {
1529 error = nfsm_mbufuio(nd, uiop, len);
1530 if ((nd->nd_flag & ND_NFSV4) && !error && cangetattr)
1531 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
1533 if (nd->nd_repstat && !error)
1534 error = nd->nd_repstat;
1536 m_freem(nd->nd_mrep);
1544 nfsrpc_read(vnode_t vp, struct uio *uiop, struct ucred *cred,
1545 NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, void *stuff)
1547 int error, expireret = 0, retrycnt;
1548 u_int32_t clidrev = 0;
1549 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1550 struct nfsnode *np = VTONFS(vp);
1551 struct ucred *newcred;
1552 struct nfsfh *nfhp = NULL;
1553 nfsv4stateid_t stateid;
1556 if (nmp->nm_clp != NULL)
1557 clidrev = nmp->nm_clp->nfsc_clientidrev;
1559 if (NFSHASNFSV4(nmp)) {
1561 newcred = NFSNEWCRED(cred);
1566 if (NFSHASNFSV4(nmp))
1567 (void)nfscl_getstateid(vp, nfhp->nfh_fh, nfhp->nfh_len,
1568 NFSV4OPEN_ACCESSREAD, 0, newcred, p, &stateid,
1570 error = nfsrpc_readrpc(vp, uiop, newcred, &stateid, p, nap,
1572 if (error == NFSERR_OPENMODE) {
1574 nmp->nm_state |= NFSSTA_OPENMODE;
1577 if (error == NFSERR_STALESTATEID)
1578 nfscl_initiate_recovery(nmp->nm_clp);
1580 nfscl_lockderef(lckp);
1581 if (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
1582 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
1583 error == NFSERR_OLDSTATEID || error == NFSERR_BADSESSION) {
1584 (void) nfs_catnap(PZERO, error, "nfs_read");
1585 } else if ((error == NFSERR_EXPIRED ||
1586 error == NFSERR_BADSTATEID) && clidrev != 0) {
1587 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
1590 } while (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
1591 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
1592 error == NFSERR_BADSESSION ||
1593 (error == NFSERR_OLDSTATEID && retrycnt < 20) ||
1594 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
1595 expireret == 0 && clidrev != 0 && retrycnt < 4) ||
1596 (error == NFSERR_OPENMODE && retrycnt < 4));
1597 if (error && retrycnt >= 4)
1599 if (NFSHASNFSV4(nmp))
1600 NFSFREECRED(newcred);
1605 * The actual read RPC.
1608 nfsrpc_readrpc(vnode_t vp, struct uio *uiop, struct ucred *cred,
1609 nfsv4stateid_t *stateidp, NFSPROC_T *p, struct nfsvattr *nap,
1610 int *attrflagp, void *stuff)
1613 int error = 0, len, retlen, tsiz, eof = 0;
1614 struct nfsrv_descript nfsd;
1615 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1616 struct nfsrv_descript *nd = &nfsd;
1621 tsiz = uiop->uio_resid;
1622 tmp_off = uiop->uio_offset + tsiz;
1624 if (tmp_off > nmp->nm_maxfilesize || tmp_off < uiop->uio_offset) {
1628 rsize = nmp->nm_rsize;
1633 len = (tsiz > rsize) ? rsize : tsiz;
1634 NFSCL_REQSTART(nd, NFSPROC_READ, vp);
1635 if (nd->nd_flag & ND_NFSV4)
1636 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID);
1637 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED * 3);
1638 if (nd->nd_flag & ND_NFSV2) {
1639 *tl++ = txdr_unsigned(uiop->uio_offset);
1640 *tl++ = txdr_unsigned(len);
1643 txdr_hyper(uiop->uio_offset, tl);
1644 *(tl + 2) = txdr_unsigned(len);
1647 * Since I can't do a Getattr for NFSv4 for Write, there
1648 * doesn't seem any point in doing one here, either.
1649 * (See the comment in nfsrpc_writerpc() for more info.)
1651 error = nfscl_request(nd, vp, p, cred, stuff);
1654 if (nd->nd_flag & ND_NFSV3) {
1655 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
1656 } else if (!nd->nd_repstat && (nd->nd_flag & ND_NFSV2)) {
1657 error = nfsm_loadattr(nd, nap);
1661 if (nd->nd_repstat || error) {
1663 error = nd->nd_repstat;
1666 if (nd->nd_flag & ND_NFSV3) {
1667 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1668 eof = fxdr_unsigned(int, *(tl + 1));
1669 } else if (nd->nd_flag & ND_NFSV4) {
1670 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
1671 eof = fxdr_unsigned(int, *tl);
1673 NFSM_STRSIZ(retlen, len);
1674 error = nfsm_mbufuio(nd, uiop, retlen);
1677 m_freem(nd->nd_mrep);
1680 if (!(nd->nd_flag & ND_NFSV2)) {
1681 if (eof || retlen == 0)
1683 } else if (retlen < len)
1688 if (nd->nd_mrep != NULL)
1689 m_freem(nd->nd_mrep);
1694 * nfs write operation
1695 * When called_from_strategy != 0, it should return EIO for an error that
1696 * indicates recovery is in progress, so that the buffer will be left
1697 * dirty and be written back to the server later. If it loops around,
1698 * the recovery thread could get stuck waiting for the buffer and recovery
1699 * will then deadlock.
1702 nfsrpc_write(vnode_t vp, struct uio *uiop, int *iomode, int *must_commit,
1703 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp,
1704 void *stuff, int called_from_strategy)
1706 int error, expireret = 0, retrycnt, nostateid;
1707 u_int32_t clidrev = 0;
1708 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1709 struct nfsnode *np = VTONFS(vp);
1710 struct ucred *newcred;
1711 struct nfsfh *nfhp = NULL;
1712 nfsv4stateid_t stateid;
1716 if (nmp->nm_clp != NULL)
1717 clidrev = nmp->nm_clp->nfsc_clientidrev;
1719 if (NFSHASNFSV4(nmp)) {
1720 newcred = NFSNEWCRED(cred);
1727 if (NFSHASNFSV4(nmp)) {
1728 (void)nfscl_getstateid(vp, nfhp->nfh_fh, nfhp->nfh_len,
1729 NFSV4OPEN_ACCESSWRITE, 0, newcred, p, &stateid,
1731 if (stateid.other[0] == 0 && stateid.other[1] == 0 &&
1732 stateid.other[2] == 0) {
1734 NFSCL_DEBUG(1, "stateid0 in write\n");
1739 * If there is no stateid for NFSv4, it means this is an
1740 * extraneous write after close. Basically a poorly
1741 * implemented buffer cache. Just don't do the write.
1746 error = nfsrpc_writerpc(vp, uiop, iomode, must_commit,
1747 newcred, &stateid, p, nap, attrflagp, stuff);
1748 if (error == NFSERR_STALESTATEID)
1749 nfscl_initiate_recovery(nmp->nm_clp);
1751 nfscl_lockderef(lckp);
1752 if (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
1753 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
1754 error == NFSERR_OLDSTATEID || error == NFSERR_BADSESSION) {
1755 (void) nfs_catnap(PZERO, error, "nfs_write");
1756 } else if ((error == NFSERR_EXPIRED ||
1757 error == NFSERR_BADSTATEID) && clidrev != 0) {
1758 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
1761 } while (error == NFSERR_GRACE || error == NFSERR_DELAY ||
1762 ((error == NFSERR_STALESTATEID || error == NFSERR_BADSESSION ||
1763 error == NFSERR_STALEDONTRECOVER) && called_from_strategy == 0) ||
1764 (error == NFSERR_OLDSTATEID && retrycnt < 20) ||
1765 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
1766 expireret == 0 && clidrev != 0 && retrycnt < 4));
1767 if (error != 0 && (retrycnt >= 4 ||
1768 ((error == NFSERR_STALESTATEID || error == NFSERR_BADSESSION ||
1769 error == NFSERR_STALEDONTRECOVER) && called_from_strategy != 0)))
1771 if (NFSHASNFSV4(nmp))
1772 NFSFREECRED(newcred);
1777 * The actual write RPC.
1780 nfsrpc_writerpc(vnode_t vp, struct uio *uiop, int *iomode,
1781 int *must_commit, struct ucred *cred, nfsv4stateid_t *stateidp,
1782 NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, void *stuff)
1785 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1786 struct nfsnode *np = VTONFS(vp);
1787 int error = 0, len, tsiz, rlen, commit, committed = NFSWRITE_FILESYNC;
1788 int wccflag = 0, wsize;
1790 struct nfsrv_descript nfsd;
1791 struct nfsrv_descript *nd = &nfsd;
1792 nfsattrbit_t attrbits;
1795 KASSERT(uiop->uio_iovcnt == 1, ("nfs: writerpc iovcnt > 1"));
1797 tsiz = uiop->uio_resid;
1798 tmp_off = uiop->uio_offset + tsiz;
1800 if (tmp_off > nmp->nm_maxfilesize || tmp_off < uiop->uio_offset) {
1804 wsize = nmp->nm_wsize;
1806 nd->nd_mrep = NULL; /* NFSv2 sometimes does a write with */
1807 nd->nd_repstat = 0; /* uio_resid == 0, so the while is not done */
1810 len = (tsiz > wsize) ? wsize : tsiz;
1811 NFSCL_REQSTART(nd, NFSPROC_WRITE, vp);
1812 if (nd->nd_flag & ND_NFSV4) {
1813 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID);
1814 NFSM_BUILD(tl, u_int32_t *, NFSX_HYPER+2*NFSX_UNSIGNED);
1815 txdr_hyper(uiop->uio_offset, tl);
1817 *tl++ = txdr_unsigned(*iomode);
1818 *tl = txdr_unsigned(len);
1819 } else if (nd->nd_flag & ND_NFSV3) {
1820 NFSM_BUILD(tl, u_int32_t *, NFSX_HYPER+3*NFSX_UNSIGNED);
1821 txdr_hyper(uiop->uio_offset, tl);
1823 *tl++ = txdr_unsigned(len);
1824 *tl++ = txdr_unsigned(*iomode);
1825 *tl = txdr_unsigned(len);
1829 NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1831 * Not sure why someone changed this, since the
1832 * RFC clearly states that "beginoffset" and
1833 * "totalcount" are ignored, but it wouldn't
1834 * surprise me if there's a busted server out there.
1836 /* Set both "begin" and "current" to non-garbage. */
1837 x = txdr_unsigned((u_int32_t)uiop->uio_offset);
1838 *tl++ = x; /* "begin offset" */
1839 *tl++ = x; /* "current offset" */
1840 x = txdr_unsigned(len);
1841 *tl++ = x; /* total to this offset */
1842 *tl = x; /* size of this write */
1845 nfsm_uiombuf(nd, uiop, len);
1847 * Although it is tempting to do a normal Getattr Op in the
1848 * NFSv4 compound, the result can be a nearly hung client
1849 * system if the Getattr asks for Owner and/or OwnerGroup.
1850 * It occurs when the client can't map either the Owner or
1851 * Owner_group name in the Getattr reply to a uid/gid. When
1852 * there is a cache miss, the kernel does an upcall to the
1853 * nfsuserd. Then, it can try and read the local /etc/passwd
1854 * or /etc/group file. It can then block in getnewbuf(),
1855 * waiting for dirty writes to be pushed to the NFS server.
1856 * The only reason this doesn't result in a complete
1857 * deadlock, is that the upcall times out and allows
1858 * the write to complete. However, progress is so slow
1859 * that it might just as well be deadlocked.
1860 * As such, we get the rest of the attributes, but not
1861 * Owner or Owner_group.
1862 * nb: nfscl_loadattrcache() needs to be told that these
1863 * partial attributes from a write rpc are being
1864 * passed in, via a argument flag.
1866 if (nd->nd_flag & ND_NFSV4) {
1867 NFSWRITEGETATTR_ATTRBIT(&attrbits);
1868 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1869 *tl = txdr_unsigned(NFSV4OP_GETATTR);
1870 (void) nfsrv_putattrbit(nd, &attrbits);
1872 error = nfscl_request(nd, vp, p, cred, stuff);
1875 if (nd->nd_repstat) {
1877 * In case the rpc gets retried, roll
1878 * the uio fileds changed by nfsm_uiombuf()
1881 uiop->uio_offset -= len;
1882 uiop->uio_resid += len;
1883 uiop->uio_iov->iov_base =
1884 (char *)uiop->uio_iov->iov_base - len;
1885 uiop->uio_iov->iov_len += len;
1887 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) {
1888 error = nfscl_wcc_data(nd, vp, nap, attrflagp,
1893 if (!nd->nd_repstat) {
1894 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) {
1895 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED
1897 rlen = fxdr_unsigned(int, *tl++);
1901 } else if (rlen < len) {
1902 backup = len - rlen;
1903 uiop->uio_iov->iov_base =
1904 (char *)uiop->uio_iov->iov_base -
1906 uiop->uio_iov->iov_len += backup;
1907 uiop->uio_offset -= backup;
1908 uiop->uio_resid += backup;
1911 commit = fxdr_unsigned(int, *tl++);
1914 * Return the lowest commitment level
1915 * obtained by any of the RPCs.
1917 if (committed == NFSWRITE_FILESYNC)
1919 else if (committed == NFSWRITE_DATASYNC &&
1920 commit == NFSWRITE_UNSTABLE)
1923 if (!NFSHASWRITEVERF(nmp)) {
1924 NFSBCOPY((caddr_t)tl,
1925 (caddr_t)&nmp->nm_verf[0],
1927 NFSSETWRITEVERF(nmp);
1928 } else if (NFSBCMP(tl, nmp->nm_verf,
1931 NFSBCOPY(tl, nmp->nm_verf, NFSX_VERF);
1935 if (nd->nd_flag & ND_NFSV4)
1936 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1937 if (nd->nd_flag & (ND_NFSV2 | ND_NFSV4)) {
1938 error = nfsm_loadattr(nd, nap);
1940 *attrflagp = NFS_LATTR_NOSHRINK;
1943 error = nd->nd_repstat;
1947 NFSWRITERPC_SETTIME(wccflag, np, nap, (nd->nd_flag & ND_NFSV4));
1948 m_freem(nd->nd_mrep);
1953 if (nd->nd_mrep != NULL)
1954 m_freem(nd->nd_mrep);
1955 *iomode = committed;
1956 if (nd->nd_repstat && !error)
1957 error = nd->nd_repstat;
1963 * For NFS v2 this is a kludge. Use a create rpc but with the IFMT bits of the
1964 * mode set to specify the file type and the size field for rdev.
1967 nfsrpc_mknod(vnode_t dvp, char *name, int namelen, struct vattr *vap,
1968 u_int32_t rdev, enum vtype vtyp, struct ucred *cred, NFSPROC_T *p,
1969 struct nfsvattr *dnap, struct nfsvattr *nnap, struct nfsfh **nfhpp,
1970 int *attrflagp, int *dattrflagp, void *dstuff)
1974 struct nfsrv_descript nfsd, *nd = &nfsd;
1975 nfsattrbit_t attrbits;
1980 if (namelen > NFS_MAXNAMLEN)
1981 return (ENAMETOOLONG);
1982 NFSCL_REQSTART(nd, NFSPROC_MKNOD, dvp);
1983 if (nd->nd_flag & ND_NFSV4) {
1984 if (vtyp == VBLK || vtyp == VCHR) {
1985 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
1986 *tl++ = vtonfsv34_type(vtyp);
1987 *tl++ = txdr_unsigned(NFSMAJOR(rdev));
1988 *tl = txdr_unsigned(NFSMINOR(rdev));
1990 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1991 *tl = vtonfsv34_type(vtyp);
1994 (void) nfsm_strtom(nd, name, namelen);
1995 if (nd->nd_flag & ND_NFSV3) {
1996 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1997 *tl = vtonfsv34_type(vtyp);
1999 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4))
2000 nfscl_fillsattr(nd, vap, dvp, 0, 0);
2001 if ((nd->nd_flag & ND_NFSV3) &&
2002 (vtyp == VCHR || vtyp == VBLK)) {
2003 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2004 *tl++ = txdr_unsigned(NFSMAJOR(rdev));
2005 *tl = txdr_unsigned(NFSMINOR(rdev));
2007 if (nd->nd_flag & ND_NFSV4) {
2008 NFSGETATTR_ATTRBIT(&attrbits);
2009 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2010 *tl++ = txdr_unsigned(NFSV4OP_GETFH);
2011 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2012 (void) nfsrv_putattrbit(nd, &attrbits);
2014 if (nd->nd_flag & ND_NFSV2)
2015 nfscl_fillsattr(nd, vap, dvp, NFSSATTR_SIZERDEV, rdev);
2016 error = nfscl_request(nd, dvp, p, cred, dstuff);
2019 if (nd->nd_flag & ND_NFSV4)
2020 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
2021 if (!nd->nd_repstat) {
2022 if (nd->nd_flag & ND_NFSV4) {
2023 NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
2024 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
2028 error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp);
2032 if (nd->nd_flag & ND_NFSV3)
2033 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
2034 if (!error && nd->nd_repstat)
2035 error = nd->nd_repstat;
2037 m_freem(nd->nd_mrep);
2042 * nfs file create call
2043 * Mostly just call the approriate routine. (I separated out v4, so that
2044 * error recovery wouldn't be as difficult.)
2047 nfsrpc_create(vnode_t dvp, char *name, int namelen, struct vattr *vap,
2048 nfsquad_t cverf, int fmode, struct ucred *cred, NFSPROC_T *p,
2049 struct nfsvattr *dnap, struct nfsvattr *nnap, struct nfsfh **nfhpp,
2050 int *attrflagp, int *dattrflagp, void *dstuff)
2052 int error = 0, newone, expireret = 0, retrycnt, unlocked;
2053 struct nfsclowner *owp;
2054 struct nfscldeleg *dp;
2055 struct nfsmount *nmp = VFSTONFS(dvp->v_mount);
2058 if (NFSHASNFSV4(nmp)) {
2062 error = nfscl_open(dvp, NULL, 0, (NFSV4OPEN_ACCESSWRITE |
2063 NFSV4OPEN_ACCESSREAD), 0, cred, p, &owp, NULL, &newone,
2067 if (nmp->nm_clp != NULL)
2068 clidrev = nmp->nm_clp->nfsc_clientidrev;
2071 if (!NFSHASPNFS(nmp) || nfscl_enablecallb == 0 ||
2072 nfs_numnfscbd == 0 || retrycnt > 0)
2073 error = nfsrpc_createv4(dvp, name, namelen, vap, cverf,
2074 fmode, owp, &dp, cred, p, dnap, nnap, nfhpp,
2075 attrflagp, dattrflagp, dstuff, &unlocked);
2077 error = nfsrpc_getcreatelayout(dvp, name, namelen, vap,
2078 cverf, fmode, owp, &dp, cred, p, dnap, nnap, nfhpp,
2079 attrflagp, dattrflagp, dstuff, &unlocked);
2081 * There is no need to invalidate cached attributes here,
2082 * since new post-delegation issue attributes are always
2083 * returned by nfsrpc_createv4() and these will update the
2087 (void) nfscl_deleg(nmp->nm_mountp, owp->nfsow_clp,
2088 (*nfhpp)->nfh_fh, (*nfhpp)->nfh_len, cred, p, &dp);
2089 nfscl_ownerrelease(nmp, owp, error, newone, unlocked);
2090 if (error == NFSERR_GRACE || error == NFSERR_STALECLIENTID ||
2091 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
2092 error == NFSERR_BADSESSION) {
2093 (void) nfs_catnap(PZERO, error, "nfs_open");
2094 } else if ((error == NFSERR_EXPIRED ||
2095 error == NFSERR_BADSTATEID) && clidrev != 0) {
2096 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
2099 } while (error == NFSERR_GRACE || error == NFSERR_STALECLIENTID ||
2100 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
2101 error == NFSERR_BADSESSION ||
2102 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
2103 expireret == 0 && clidrev != 0 && retrycnt < 4));
2104 if (error && retrycnt >= 4)
2107 error = nfsrpc_createv23(dvp, name, namelen, vap, cverf,
2108 fmode, cred, p, dnap, nnap, nfhpp, attrflagp, dattrflagp,
2115 * The create rpc for v2 and 3.
2118 nfsrpc_createv23(vnode_t dvp, char *name, int namelen, struct vattr *vap,
2119 nfsquad_t cverf, int fmode, struct ucred *cred, NFSPROC_T *p,
2120 struct nfsvattr *dnap, struct nfsvattr *nnap, struct nfsfh **nfhpp,
2121 int *attrflagp, int *dattrflagp, void *dstuff)
2125 struct nfsrv_descript nfsd, *nd = &nfsd;
2130 if (namelen > NFS_MAXNAMLEN)
2131 return (ENAMETOOLONG);
2132 NFSCL_REQSTART(nd, NFSPROC_CREATE, dvp);
2133 (void) nfsm_strtom(nd, name, namelen);
2134 if (nd->nd_flag & ND_NFSV3) {
2135 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2136 if (fmode & O_EXCL) {
2137 *tl = txdr_unsigned(NFSCREATE_EXCLUSIVE);
2138 NFSM_BUILD(tl, u_int32_t *, NFSX_VERF);
2139 *tl++ = cverf.lval[0];
2140 *tl = cverf.lval[1];
2142 *tl = txdr_unsigned(NFSCREATE_UNCHECKED);
2143 nfscl_fillsattr(nd, vap, dvp, 0, 0);
2146 nfscl_fillsattr(nd, vap, dvp, NFSSATTR_SIZE0, 0);
2148 error = nfscl_request(nd, dvp, p, cred, dstuff);
2151 if (nd->nd_repstat == 0) {
2152 error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp);
2156 if (nd->nd_flag & ND_NFSV3)
2157 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
2158 if (nd->nd_repstat != 0 && error == 0)
2159 error = nd->nd_repstat;
2161 m_freem(nd->nd_mrep);
2166 nfsrpc_createv4(vnode_t dvp, char *name, int namelen, struct vattr *vap,
2167 nfsquad_t cverf, int fmode, struct nfsclowner *owp, struct nfscldeleg **dpp,
2168 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap,
2169 struct nfsvattr *nnap, struct nfsfh **nfhpp, int *attrflagp,
2170 int *dattrflagp, void *dstuff, int *unlockedp)
2173 int error = 0, deleg, newone, ret, acesize, limitby;
2174 struct nfsrv_descript nfsd, *nd = &nfsd;
2175 struct nfsclopen *op;
2176 struct nfscldeleg *dp = NULL;
2179 nfsattrbit_t attrbits;
2180 nfsv4stateid_t stateid;
2182 struct nfsmount *nmp;
2183 struct nfsclsession *tsep;
2185 nmp = VFSTONFS(dvp->v_mount);
2192 if (namelen > NFS_MAXNAMLEN)
2193 return (ENAMETOOLONG);
2194 NFSCL_REQSTART(nd, NFSPROC_CREATE, dvp);
2196 * For V4, this is actually an Open op.
2198 NFSM_BUILD(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
2199 *tl++ = txdr_unsigned(owp->nfsow_seqid);
2200 *tl++ = txdr_unsigned(NFSV4OPEN_ACCESSWRITE |
2201 NFSV4OPEN_ACCESSREAD);
2202 *tl++ = txdr_unsigned(NFSV4OPEN_DENYNONE);
2203 tsep = nfsmnt_mdssession(nmp);
2204 *tl++ = tsep->nfsess_clientid.lval[0];
2205 *tl = tsep->nfsess_clientid.lval[1];
2206 (void) nfsm_strtom(nd, owp->nfsow_owner, NFSV4CL_LOCKNAMELEN);
2207 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2208 *tl++ = txdr_unsigned(NFSV4OPEN_CREATE);
2209 if (fmode & O_EXCL) {
2210 if (NFSHASNFSV4N(nmp)) {
2211 if (NFSHASSESSPERSIST(nmp)) {
2212 /* Use GUARDED for persistent sessions. */
2213 *tl = txdr_unsigned(NFSCREATE_GUARDED);
2214 nfscl_fillsattr(nd, vap, dvp, 0, 0);
2216 /* Otherwise, use EXCLUSIVE4_1. */
2217 *tl = txdr_unsigned(NFSCREATE_EXCLUSIVE41);
2218 NFSM_BUILD(tl, u_int32_t *, NFSX_VERF);
2219 *tl++ = cverf.lval[0];
2220 *tl = cverf.lval[1];
2221 nfscl_fillsattr(nd, vap, dvp, 0, 0);
2225 *tl = txdr_unsigned(NFSCREATE_EXCLUSIVE);
2226 NFSM_BUILD(tl, u_int32_t *, NFSX_VERF);
2227 *tl++ = cverf.lval[0];
2228 *tl = cverf.lval[1];
2231 *tl = txdr_unsigned(NFSCREATE_UNCHECKED);
2232 nfscl_fillsattr(nd, vap, dvp, 0, 0);
2234 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2235 *tl = txdr_unsigned(NFSV4OPEN_CLAIMNULL);
2236 (void) nfsm_strtom(nd, name, namelen);
2237 /* Get the new file's handle and attributes. */
2238 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2239 *tl++ = txdr_unsigned(NFSV4OP_GETFH);
2240 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2241 NFSGETATTR_ATTRBIT(&attrbits);
2242 (void) nfsrv_putattrbit(nd, &attrbits);
2243 /* Get the directory's post-op attributes. */
2244 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2245 *tl = txdr_unsigned(NFSV4OP_PUTFH);
2246 (void) nfsm_fhtom(nd, np->n_fhp->nfh_fh, np->n_fhp->nfh_len, 0);
2247 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2248 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2249 (void) nfsrv_putattrbit(nd, &attrbits);
2250 error = nfscl_request(nd, dvp, p, cred, dstuff);
2253 NFSCL_INCRSEQID(owp->nfsow_seqid, nd);
2254 if (nd->nd_repstat == 0) {
2255 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
2257 stateid.seqid = *tl++;
2258 stateid.other[0] = *tl++;
2259 stateid.other[1] = *tl++;
2260 stateid.other[2] = *tl;
2261 rflags = fxdr_unsigned(u_int32_t, *(tl + 6));
2262 (void) nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
2263 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2264 deleg = fxdr_unsigned(int, *tl);
2265 if (deleg == NFSV4OPEN_DELEGATEREAD ||
2266 deleg == NFSV4OPEN_DELEGATEWRITE) {
2267 if (!(owp->nfsow_clp->nfsc_flags &
2268 NFSCLFLAGS_FIRSTDELEG))
2269 owp->nfsow_clp->nfsc_flags |=
2270 (NFSCLFLAGS_FIRSTDELEG | NFSCLFLAGS_GOTDELEG);
2272 sizeof (struct nfscldeleg) + NFSX_V4FHMAX,
2273 M_NFSCLDELEG, M_WAITOK);
2274 LIST_INIT(&dp->nfsdl_owner);
2275 LIST_INIT(&dp->nfsdl_lock);
2276 dp->nfsdl_clp = owp->nfsow_clp;
2277 newnfs_copyincred(cred, &dp->nfsdl_cred);
2278 nfscl_lockinit(&dp->nfsdl_rwlock);
2279 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
2281 dp->nfsdl_stateid.seqid = *tl++;
2282 dp->nfsdl_stateid.other[0] = *tl++;
2283 dp->nfsdl_stateid.other[1] = *tl++;
2284 dp->nfsdl_stateid.other[2] = *tl++;
2285 ret = fxdr_unsigned(int, *tl);
2286 if (deleg == NFSV4OPEN_DELEGATEWRITE) {
2287 dp->nfsdl_flags = NFSCLDL_WRITE;
2289 * Indicates how much the file can grow.
2291 NFSM_DISSECT(tl, u_int32_t *,
2293 limitby = fxdr_unsigned(int, *tl++);
2295 case NFSV4OPEN_LIMITSIZE:
2296 dp->nfsdl_sizelimit = fxdr_hyper(tl);
2298 case NFSV4OPEN_LIMITBLOCKS:
2299 dp->nfsdl_sizelimit =
2300 fxdr_unsigned(u_int64_t, *tl++);
2301 dp->nfsdl_sizelimit *=
2302 fxdr_unsigned(u_int64_t, *tl);
2305 error = NFSERR_BADXDR;
2309 dp->nfsdl_flags = NFSCLDL_READ;
2312 dp->nfsdl_flags |= NFSCLDL_RECALL;
2313 error = nfsrv_dissectace(nd, &dp->nfsdl_ace, &ret,
2317 } else if (deleg != NFSV4OPEN_DELEGATENONE) {
2318 error = NFSERR_BADXDR;
2321 error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp);
2324 /* Get rid of the PutFH and Getattr status values. */
2325 NFSM_DISSECT(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
2326 /* Load the directory attributes. */
2327 error = nfsm_loadattr(nd, dnap);
2331 if (dp != NULL && *attrflagp) {
2332 dp->nfsdl_change = nnap->na_filerev;
2333 dp->nfsdl_modtime = nnap->na_mtime;
2334 dp->nfsdl_flags |= NFSCLDL_MODTIMESET;
2337 * We can now complete the Open state.
2341 dp->nfsdl_fhlen = nfhp->nfh_len;
2342 NFSBCOPY(nfhp->nfh_fh, dp->nfsdl_fh, nfhp->nfh_len);
2345 * Get an Open structure that will be
2346 * attached to the OpenOwner, acquired already.
2348 error = nfscl_open(dvp, nfhp->nfh_fh, nfhp->nfh_len,
2349 (NFSV4OPEN_ACCESSWRITE | NFSV4OPEN_ACCESSREAD), 0,
2350 cred, p, NULL, &op, &newone, NULL, 0);
2353 op->nfso_stateid = stateid;
2354 newnfs_copyincred(cred, &op->nfso_cred);
2355 if ((rflags & NFSV4OPEN_RESULTCONFIRM)) {
2357 ret = nfsrpc_openconfirm(dvp, nfhp->nfh_fh,
2358 nfhp->nfh_len, op, cred, p);
2359 if (ret == NFSERR_DELAY)
2360 (void) nfs_catnap(PZERO, ret, "nfs_create");
2361 } while (ret == NFSERR_DELAY);
2366 * If the server is handing out delegations, but we didn't
2367 * get one because an OpenConfirm was required, try the
2368 * Open again, to get a delegation. This is a harmless no-op,
2369 * from a server's point of view.
2371 if ((rflags & NFSV4OPEN_RESULTCONFIRM) &&
2372 (owp->nfsow_clp->nfsc_flags & NFSCLFLAGS_GOTDELEG) &&
2373 !error && dp == NULL) {
2375 ret = nfsrpc_openrpc(VFSTONFS(dvp->v_mount), dvp,
2376 np->n_fhp->nfh_fh, np->n_fhp->nfh_len,
2377 nfhp->nfh_fh, nfhp->nfh_len,
2378 (NFSV4OPEN_ACCESSWRITE | NFSV4OPEN_ACCESSREAD), op,
2379 name, namelen, &dp, 0, 0x0, cred, p, 0, 1);
2380 if (ret == NFSERR_DELAY)
2381 (void) nfs_catnap(PZERO, ret, "nfs_crt2");
2382 } while (ret == NFSERR_DELAY);
2385 free(dp, M_NFSCLDELEG);
2388 if (ret == NFSERR_STALECLIENTID ||
2389 ret == NFSERR_STALEDONTRECOVER ||
2390 ret == NFSERR_BADSESSION)
2394 nfscl_openrelease(nmp, op, error, newone);
2397 if (nd->nd_repstat != 0 && error == 0)
2398 error = nd->nd_repstat;
2399 if (error == NFSERR_STALECLIENTID)
2400 nfscl_initiate_recovery(owp->nfsow_clp);
2404 else if (dp != NULL)
2405 free(dp, M_NFSCLDELEG);
2406 m_freem(nd->nd_mrep);
2414 nfsrpc_remove(vnode_t dvp, char *name, int namelen, vnode_t vp,
2415 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap, int *dattrflagp,
2419 struct nfsrv_descript nfsd, *nd = &nfsd;
2421 struct nfsmount *nmp;
2422 nfsv4stateid_t dstateid;
2423 int error, ret = 0, i;
2426 if (namelen > NFS_MAXNAMLEN)
2427 return (ENAMETOOLONG);
2428 nmp = VFSTONFS(dvp->v_mount);
2430 if (NFSHASNFSV4(nmp) && ret == 0) {
2431 ret = nfscl_removedeleg(vp, p, &dstateid);
2433 NFSCL_REQSTART(nd, NFSPROC_RETDELEGREMOVE, vp);
2434 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID +
2436 if (NFSHASNFSV4N(nmp))
2439 *tl++ = dstateid.seqid;
2440 *tl++ = dstateid.other[0];
2441 *tl++ = dstateid.other[1];
2442 *tl++ = dstateid.other[2];
2443 *tl = txdr_unsigned(NFSV4OP_PUTFH);
2445 (void) nfsm_fhtom(nd, np->n_fhp->nfh_fh,
2446 np->n_fhp->nfh_len, 0);
2447 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2448 *tl = txdr_unsigned(NFSV4OP_REMOVE);
2454 NFSCL_REQSTART(nd, NFSPROC_REMOVE, dvp);
2455 (void) nfsm_strtom(nd, name, namelen);
2456 error = nfscl_request(nd, dvp, p, cred, dstuff);
2459 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) {
2460 /* For NFSv4, parse out any Delereturn replies. */
2461 if (ret > 0 && nd->nd_repstat != 0 &&
2462 (nd->nd_flag & ND_NOMOREDATA)) {
2464 * If the Delegreturn failed, try again without
2465 * it. The server will Recall, as required.
2467 m_freem(nd->nd_mrep);
2470 for (i = 0; i < (ret * 2); i++) {
2471 if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) ==
2473 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2475 nd->nd_flag |= ND_NOMOREDATA;
2478 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
2480 if (nd->nd_repstat && !error)
2481 error = nd->nd_repstat;
2483 m_freem(nd->nd_mrep);
2488 * Do an nfs rename rpc.
2491 nfsrpc_rename(vnode_t fdvp, vnode_t fvp, char *fnameptr, int fnamelen,
2492 vnode_t tdvp, vnode_t tvp, char *tnameptr, int tnamelen, struct ucred *cred,
2493 NFSPROC_T *p, struct nfsvattr *fnap, struct nfsvattr *tnap,
2494 int *fattrflagp, int *tattrflagp, void *fstuff, void *tstuff)
2497 struct nfsrv_descript nfsd, *nd = &nfsd;
2498 struct nfsmount *nmp;
2500 nfsattrbit_t attrbits;
2501 nfsv4stateid_t fdstateid, tdstateid;
2502 int error = 0, ret = 0, gottd = 0, gotfd = 0, i;
2506 nmp = VFSTONFS(fdvp->v_mount);
2507 if (fnamelen > NFS_MAXNAMLEN || tnamelen > NFS_MAXNAMLEN)
2508 return (ENAMETOOLONG);
2510 if (NFSHASNFSV4(nmp) && ret == 0) {
2511 ret = nfscl_renamedeleg(fvp, &fdstateid, &gotfd, tvp,
2512 &tdstateid, &gottd, p);
2513 if (gotfd && gottd) {
2514 NFSCL_REQSTART(nd, NFSPROC_RETDELEGRENAME2, fvp);
2516 NFSCL_REQSTART(nd, NFSPROC_RETDELEGRENAME1, fvp);
2518 NFSCL_REQSTART(nd, NFSPROC_RETDELEGRENAME1, tvp);
2521 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID);
2522 if (NFSHASNFSV4N(nmp))
2525 *tl++ = fdstateid.seqid;
2526 *tl++ = fdstateid.other[0];
2527 *tl++ = fdstateid.other[1];
2528 *tl = fdstateid.other[2];
2530 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2531 *tl = txdr_unsigned(NFSV4OP_PUTFH);
2533 (void) nfsm_fhtom(nd, np->n_fhp->nfh_fh,
2534 np->n_fhp->nfh_len, 0);
2535 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2536 *tl = txdr_unsigned(NFSV4OP_DELEGRETURN);
2540 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID);
2541 if (NFSHASNFSV4N(nmp))
2544 *tl++ = tdstateid.seqid;
2545 *tl++ = tdstateid.other[0];
2546 *tl++ = tdstateid.other[1];
2547 *tl = tdstateid.other[2];
2550 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2551 *tl = txdr_unsigned(NFSV4OP_PUTFH);
2553 (void) nfsm_fhtom(nd, np->n_fhp->nfh_fh,
2554 np->n_fhp->nfh_len, 0);
2555 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2556 *tl = txdr_unsigned(NFSV4OP_SAVEFH);
2562 NFSCL_REQSTART(nd, NFSPROC_RENAME, fdvp);
2563 if (nd->nd_flag & ND_NFSV4) {
2564 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2565 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2566 NFSWCCATTR_ATTRBIT(&attrbits);
2567 (void) nfsrv_putattrbit(nd, &attrbits);
2568 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2569 *tl = txdr_unsigned(NFSV4OP_PUTFH);
2570 (void) nfsm_fhtom(nd, VTONFS(tdvp)->n_fhp->nfh_fh,
2571 VTONFS(tdvp)->n_fhp->nfh_len, 0);
2572 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2573 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2574 (void) nfsrv_putattrbit(nd, &attrbits);
2575 nd->nd_flag |= ND_V4WCCATTR;
2576 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2577 *tl = txdr_unsigned(NFSV4OP_RENAME);
2579 (void) nfsm_strtom(nd, fnameptr, fnamelen);
2580 if (!(nd->nd_flag & ND_NFSV4))
2581 (void) nfsm_fhtom(nd, VTONFS(tdvp)->n_fhp->nfh_fh,
2582 VTONFS(tdvp)->n_fhp->nfh_len, 0);
2583 (void) nfsm_strtom(nd, tnameptr, tnamelen);
2584 error = nfscl_request(nd, fdvp, p, cred, fstuff);
2587 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) {
2588 /* For NFSv4, parse out any Delereturn replies. */
2589 if (ret > 0 && nd->nd_repstat != 0 &&
2590 (nd->nd_flag & ND_NOMOREDATA)) {
2592 * If the Delegreturn failed, try again without
2593 * it. The server will Recall, as required.
2595 m_freem(nd->nd_mrep);
2598 for (i = 0; i < (ret * 2); i++) {
2599 if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) ==
2601 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2603 if (i == 0 && ret > 1) {
2605 * If the Delegreturn failed, try again
2606 * without it. The server will Recall, as
2608 * If ret > 1, the first iteration of this
2609 * loop is the second DelegReturn result.
2611 m_freem(nd->nd_mrep);
2614 nd->nd_flag |= ND_NOMOREDATA;
2619 /* Now, the first wcc attribute reply. */
2620 if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) == ND_NFSV4) {
2621 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2623 nd->nd_flag |= ND_NOMOREDATA;
2625 error = nfscl_wcc_data(nd, fdvp, fnap, fattrflagp, NULL,
2627 /* and the second wcc attribute reply. */
2628 if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) == ND_NFSV4 &&
2630 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2632 nd->nd_flag |= ND_NOMOREDATA;
2635 error = nfscl_wcc_data(nd, tdvp, tnap, tattrflagp,
2638 if (nd->nd_repstat && !error)
2639 error = nd->nd_repstat;
2641 m_freem(nd->nd_mrep);
2646 * nfs hard link create rpc
2649 nfsrpc_link(vnode_t dvp, vnode_t vp, char *name, int namelen,
2650 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap,
2651 struct nfsvattr *nap, int *attrflagp, int *dattrflagp, void *dstuff)
2654 struct nfsrv_descript nfsd, *nd = &nfsd;
2655 nfsattrbit_t attrbits;
2660 if (namelen > NFS_MAXNAMLEN)
2661 return (ENAMETOOLONG);
2662 NFSCL_REQSTART(nd, NFSPROC_LINK, vp);
2663 if (nd->nd_flag & ND_NFSV4) {
2664 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2665 *tl = txdr_unsigned(NFSV4OP_PUTFH);
2667 (void) nfsm_fhtom(nd, VTONFS(dvp)->n_fhp->nfh_fh,
2668 VTONFS(dvp)->n_fhp->nfh_len, 0);
2669 if (nd->nd_flag & ND_NFSV4) {
2670 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2671 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2672 NFSWCCATTR_ATTRBIT(&attrbits);
2673 (void) nfsrv_putattrbit(nd, &attrbits);
2674 nd->nd_flag |= ND_V4WCCATTR;
2675 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2676 *tl = txdr_unsigned(NFSV4OP_LINK);
2678 (void) nfsm_strtom(nd, name, namelen);
2679 error = nfscl_request(nd, vp, p, cred, dstuff);
2682 if (nd->nd_flag & ND_NFSV3) {
2683 error = nfscl_postop_attr(nd, nap, attrflagp, dstuff);
2685 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp,
2687 } else if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) == ND_NFSV4) {
2689 * First, parse out the PutFH and Getattr result.
2691 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2693 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2695 nd->nd_flag |= ND_NOMOREDATA;
2697 * Get the pre-op attributes.
2699 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
2701 if (nd->nd_repstat && !error)
2702 error = nd->nd_repstat;
2704 m_freem(nd->nd_mrep);
2709 * nfs symbolic link create rpc
2712 nfsrpc_symlink(vnode_t dvp, char *name, int namelen, const char *target,
2713 struct vattr *vap, struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap,
2714 struct nfsvattr *nnap, struct nfsfh **nfhpp, int *attrflagp,
2715 int *dattrflagp, void *dstuff)
2718 struct nfsrv_descript nfsd, *nd = &nfsd;
2719 struct nfsmount *nmp;
2720 int slen, error = 0;
2725 nmp = VFSTONFS(dvp->v_mount);
2726 slen = strlen(target);
2727 if (slen > NFS_MAXPATHLEN || namelen > NFS_MAXNAMLEN)
2728 return (ENAMETOOLONG);
2729 NFSCL_REQSTART(nd, NFSPROC_SYMLINK, dvp);
2730 if (nd->nd_flag & ND_NFSV4) {
2731 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2732 *tl = txdr_unsigned(NFLNK);
2733 (void) nfsm_strtom(nd, target, slen);
2735 (void) nfsm_strtom(nd, name, namelen);
2736 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4))
2737 nfscl_fillsattr(nd, vap, dvp, 0, 0);
2738 if (!(nd->nd_flag & ND_NFSV4))
2739 (void) nfsm_strtom(nd, target, slen);
2740 if (nd->nd_flag & ND_NFSV2)
2741 nfscl_fillsattr(nd, vap, dvp, NFSSATTR_SIZENEG1, 0);
2742 error = nfscl_request(nd, dvp, p, cred, dstuff);
2745 if (nd->nd_flag & ND_NFSV4)
2746 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
2747 if ((nd->nd_flag & ND_NFSV3) && !error) {
2748 if (!nd->nd_repstat)
2749 error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp);
2751 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp,
2754 if (nd->nd_repstat && !error)
2755 error = nd->nd_repstat;
2756 m_freem(nd->nd_mrep);
2758 * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry.
2759 * Only do this if vfs.nfs.ignore_eexist is set.
2760 * Never do this for NFSv4.1 or later minor versions, since sessions
2761 * should guarantee "exactly once" RPC semantics.
2763 if (error == EEXIST && nfsignore_eexist != 0 && (!NFSHASNFSV4(nmp) ||
2764 nmp->nm_minorvers == 0))
2773 nfsrpc_mkdir(vnode_t dvp, char *name, int namelen, struct vattr *vap,
2774 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap,
2775 struct nfsvattr *nnap, struct nfsfh **nfhpp, int *attrflagp,
2776 int *dattrflagp, void *dstuff)
2779 struct nfsrv_descript nfsd, *nd = &nfsd;
2780 nfsattrbit_t attrbits;
2783 struct nfsmount *nmp;
2788 nmp = VFSTONFS(dvp->v_mount);
2789 fhp = VTONFS(dvp)->n_fhp;
2790 if (namelen > NFS_MAXNAMLEN)
2791 return (ENAMETOOLONG);
2792 NFSCL_REQSTART(nd, NFSPROC_MKDIR, dvp);
2793 if (nd->nd_flag & ND_NFSV4) {
2794 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2795 *tl = txdr_unsigned(NFDIR);
2797 (void) nfsm_strtom(nd, name, namelen);
2798 nfscl_fillsattr(nd, vap, dvp, NFSSATTR_SIZENEG1, 0);
2799 if (nd->nd_flag & ND_NFSV4) {
2800 NFSGETATTR_ATTRBIT(&attrbits);
2801 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2802 *tl++ = txdr_unsigned(NFSV4OP_GETFH);
2803 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2804 (void) nfsrv_putattrbit(nd, &attrbits);
2805 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2806 *tl = txdr_unsigned(NFSV4OP_PUTFH);
2807 (void) nfsm_fhtom(nd, fhp->nfh_fh, fhp->nfh_len, 0);
2808 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2809 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2810 (void) nfsrv_putattrbit(nd, &attrbits);
2812 error = nfscl_request(nd, dvp, p, cred, dstuff);
2815 if (nd->nd_flag & ND_NFSV4)
2816 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
2817 if (!nd->nd_repstat && !error) {
2818 if (nd->nd_flag & ND_NFSV4) {
2819 NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
2820 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
2823 error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp);
2824 if (error == 0 && (nd->nd_flag & ND_NFSV4) != 0) {
2825 /* Get rid of the PutFH and Getattr status values. */
2826 NFSM_DISSECT(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
2827 /* Load the directory attributes. */
2828 error = nfsm_loadattr(nd, dnap);
2833 if ((nd->nd_flag & ND_NFSV3) && !error)
2834 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
2835 if (nd->nd_repstat && !error)
2836 error = nd->nd_repstat;
2838 m_freem(nd->nd_mrep);
2840 * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry.
2841 * Only do this if vfs.nfs.ignore_eexist is set.
2842 * Never do this for NFSv4.1 or later minor versions, since sessions
2843 * should guarantee "exactly once" RPC semantics.
2845 if (error == EEXIST && nfsignore_eexist != 0 && (!NFSHASNFSV4(nmp) ||
2846 nmp->nm_minorvers == 0))
2852 * nfs remove directory call
2855 nfsrpc_rmdir(vnode_t dvp, char *name, int namelen, struct ucred *cred,
2856 NFSPROC_T *p, struct nfsvattr *dnap, int *dattrflagp, void *dstuff)
2858 struct nfsrv_descript nfsd, *nd = &nfsd;
2862 if (namelen > NFS_MAXNAMLEN)
2863 return (ENAMETOOLONG);
2864 NFSCL_REQSTART(nd, NFSPROC_RMDIR, dvp);
2865 (void) nfsm_strtom(nd, name, namelen);
2866 error = nfscl_request(nd, dvp, p, cred, dstuff);
2869 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4))
2870 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
2871 if (nd->nd_repstat && !error)
2872 error = nd->nd_repstat;
2873 m_freem(nd->nd_mrep);
2875 * Kludge: Map ENOENT => 0 assuming that you have a reply to a retry.
2877 if (error == ENOENT)
2884 * Always returns with either uio_resid unchanged, if you are at the
2885 * end of the directory, or uio_resid == 0, with all DIRBLKSIZ chunks
2887 * I felt this would allow caching of directory blocks more easily
2888 * than returning a pertially filled block.
2889 * Directory offset cookies:
2890 * Oh my, what to do with them...
2891 * I can think of three ways to deal with them:
2892 * 1 - have the layer above these RPCs maintain a map between logical
2893 * directory byte offsets and the NFS directory offset cookies
2894 * 2 - pass the opaque directory offset cookies up into userland
2895 * and let the libc functions deal with them, via the system call
2896 * 3 - return them to userland in the "struct dirent", so future versions
2897 * of libc can use them and do whatever is necessary to make things work
2898 * above these rpc calls, in the meantime
2899 * For now, I do #3 by "hiding" the directory offset cookies after the
2900 * d_name field in struct dirent. This is space inside d_reclen that
2901 * will be ignored by anything that doesn't know about them.
2902 * The directory offset cookies are filled in as the last 8 bytes of
2903 * each directory entry, after d_name. Someday, the userland libc
2904 * functions may be able to use these. In the meantime, it satisfies
2905 * OpenBSD's requirements for cookies being returned.
2906 * If expects the directory offset cookie for the read to be in uio_offset
2907 * and returns the one for the next entry after this directory block in
2911 nfsrpc_readdir(vnode_t vp, struct uio *uiop, nfsuint64 *cookiep,
2912 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp,
2913 int *eofp, void *stuff)
2916 struct dirent *dp = NULL;
2918 nfsquad_t cookie, ncookie;
2919 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2920 struct nfsnode *dnp = VTONFS(vp);
2921 struct nfsvattr nfsva;
2922 struct nfsrv_descript nfsd, *nd = &nfsd;
2923 int error = 0, tlen, more_dirs = 1, blksiz = 0, bigenough = 1;
2924 int reqsize, tryformoredirs = 1, readsize, eof = 0, gotmnton = 0;
2925 u_int64_t dotfileid, dotdotfileid = 0, fakefileno = UINT64_MAX;
2927 nfsattrbit_t attrbits, dattrbits;
2928 u_int32_t rderr, *tl2 = NULL;
2931 KASSERT(uiop->uio_iovcnt == 1 &&
2932 (uiop->uio_resid & (DIRBLKSIZ - 1)) == 0,
2933 ("nfs readdirrpc bad uio"));
2934 ncookie.lval[0] = ncookie.lval[1] = 0;
2936 * There is no point in reading a lot more than uio_resid, however
2937 * adding one additional DIRBLKSIZ makes sense. Since uio_resid
2938 * and nm_readdirsize are both exact multiples of DIRBLKSIZ, this
2939 * will never make readsize > nm_readdirsize.
2941 readsize = nmp->nm_readdirsize;
2942 if (readsize > uiop->uio_resid)
2943 readsize = uiop->uio_resid + DIRBLKSIZ;
2948 tresid = uiop->uio_resid;
2949 cookie.lval[0] = cookiep->nfsuquad[0];
2950 cookie.lval[1] = cookiep->nfsuquad[1];
2954 * For NFSv4, first create the "." and ".." entries.
2956 if (NFSHASNFSV4(nmp)) {
2957 reqsize = 6 * NFSX_UNSIGNED;
2958 NFSGETATTR_ATTRBIT(&dattrbits);
2959 NFSZERO_ATTRBIT(&attrbits);
2960 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_FILEID);
2961 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TYPE);
2962 if (NFSISSET_ATTRBIT(&dnp->n_vattr.na_suppattr,
2963 NFSATTRBIT_MOUNTEDONFILEID)) {
2964 NFSSETBIT_ATTRBIT(&attrbits,
2965 NFSATTRBIT_MOUNTEDONFILEID);
2969 * Must fake it. Use the fileno, except when the
2970 * fsid is != to that of the directory. For that
2971 * case, generate a fake fileno that is not the same.
2973 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_FSID);
2978 * Joy, oh joy. For V4 we get to hand craft '.' and '..'.
2980 if (uiop->uio_offset == 0) {
2981 NFSCL_REQSTART(nd, NFSPROC_LOOKUPP, vp);
2982 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2983 *tl++ = txdr_unsigned(NFSV4OP_GETFH);
2984 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2985 (void) nfsrv_putattrbit(nd, &attrbits);
2986 error = nfscl_request(nd, vp, p, cred, stuff);
2989 dotfileid = 0; /* Fake out the compiler. */
2990 if ((nd->nd_flag & ND_NOMOREDATA) == 0) {
2991 error = nfsm_loadattr(nd, &nfsva);
2994 dotfileid = nfsva.na_fileid;
2996 if (nd->nd_repstat == 0) {
2997 NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
2998 len = fxdr_unsigned(int, *(tl + 4));
2999 if (len > 0 && len <= NFSX_V4FHMAX)
3000 error = nfsm_advance(nd, NFSM_RNDUP(len), -1);
3004 NFSM_DISSECT(tl, u_int32_t *, 2*NFSX_UNSIGNED);
3005 nfsva.na_mntonfileno = UINT64_MAX;
3006 error = nfsv4_loadattr(nd, NULL, &nfsva, NULL,
3007 NULL, 0, NULL, NULL, NULL, NULL, NULL, 0,
3008 NULL, NULL, NULL, p, cred);
3010 dotdotfileid = dotfileid;
3011 } else if (gotmnton) {
3012 if (nfsva.na_mntonfileno != UINT64_MAX)
3013 dotdotfileid = nfsva.na_mntonfileno;
3015 dotdotfileid = nfsva.na_fileid;
3016 } else if (nfsva.na_filesid[0] ==
3017 dnp->n_vattr.na_filesid[0] &&
3018 nfsva.na_filesid[1] ==
3019 dnp->n_vattr.na_filesid[1]) {
3020 dotdotfileid = nfsva.na_fileid;
3024 } while (fakefileno ==
3026 dotdotfileid = fakefileno;
3029 } else if (nd->nd_repstat == NFSERR_NOENT) {
3031 * Lookupp returns NFSERR_NOENT when we are
3032 * at the root, so just use the current dir.
3035 dotdotfileid = dotfileid;
3037 error = nd->nd_repstat;
3039 m_freem(nd->nd_mrep);
3043 dp = (struct dirent *)uiop->uio_iov->iov_base;
3044 dp->d_pad0 = dp->d_pad1 = 0;
3046 dp->d_type = DT_DIR;
3047 dp->d_fileno = dotfileid;
3049 *((uint64_t *)dp->d_name) = 0; /* Zero pad it. */
3050 dp->d_name[0] = '.';
3051 dp->d_reclen = _GENERIC_DIRSIZ(dp) + NFSX_HYPER;
3053 * Just make these offset cookie 0.
3055 tl = (u_int32_t *)&dp->d_name[8];
3058 blksiz += dp->d_reclen;
3059 uiop->uio_resid -= dp->d_reclen;
3060 uiop->uio_offset += dp->d_reclen;
3061 uiop->uio_iov->iov_base =
3062 (char *)uiop->uio_iov->iov_base + dp->d_reclen;
3063 uiop->uio_iov->iov_len -= dp->d_reclen;
3064 dp = (struct dirent *)uiop->uio_iov->iov_base;
3065 dp->d_pad0 = dp->d_pad1 = 0;
3067 dp->d_type = DT_DIR;
3068 dp->d_fileno = dotdotfileid;
3070 *((uint64_t *)dp->d_name) = 0;
3071 dp->d_name[0] = '.';
3072 dp->d_name[1] = '.';
3073 dp->d_reclen = _GENERIC_DIRSIZ(dp) + NFSX_HYPER;
3075 * Just make these offset cookie 0.
3077 tl = (u_int32_t *)&dp->d_name[8];
3080 blksiz += dp->d_reclen;
3081 uiop->uio_resid -= dp->d_reclen;
3082 uiop->uio_offset += dp->d_reclen;
3083 uiop->uio_iov->iov_base =
3084 (char *)uiop->uio_iov->iov_base + dp->d_reclen;
3085 uiop->uio_iov->iov_len -= dp->d_reclen;
3087 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_RDATTRERROR);
3089 reqsize = 5 * NFSX_UNSIGNED;
3094 * Loop around doing readdir rpc's of size readsize.
3095 * The stopping criteria is EOF or buffer full.
3097 while (more_dirs && bigenough) {
3099 NFSCL_REQSTART(nd, NFSPROC_READDIR, vp);
3100 if (nd->nd_flag & ND_NFSV2) {
3101 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
3102 *tl++ = cookie.lval[1];
3103 *tl = txdr_unsigned(readsize);
3105 NFSM_BUILD(tl, u_int32_t *, reqsize);
3106 *tl++ = cookie.lval[0];
3107 *tl++ = cookie.lval[1];
3108 if (cookie.qval == 0) {
3113 *tl++ = dnp->n_cookieverf.nfsuquad[0];
3114 *tl++ = dnp->n_cookieverf.nfsuquad[1];
3117 if (nd->nd_flag & ND_NFSV4) {
3118 *tl++ = txdr_unsigned(readsize);
3119 *tl = txdr_unsigned(readsize);
3120 (void) nfsrv_putattrbit(nd, &attrbits);
3121 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
3122 *tl = txdr_unsigned(NFSV4OP_GETATTR);
3123 (void) nfsrv_putattrbit(nd, &dattrbits);
3125 *tl = txdr_unsigned(readsize);
3128 error = nfscl_request(nd, vp, p, cred, stuff);
3131 if (!(nd->nd_flag & ND_NFSV2)) {
3132 if (nd->nd_flag & ND_NFSV3)
3133 error = nfscl_postop_attr(nd, nap, attrflagp,
3135 if (!nd->nd_repstat && !error) {
3136 NFSM_DISSECT(tl, u_int32_t *, NFSX_HYPER);
3138 dnp->n_cookieverf.nfsuquad[0] = *tl++;
3139 dnp->n_cookieverf.nfsuquad[1] = *tl;
3143 if (nd->nd_repstat || error) {
3145 error = nd->nd_repstat;
3148 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3149 more_dirs = fxdr_unsigned(int, *tl);
3153 /* loop through the dir entries, doctoring them to 4bsd form */
3154 while (more_dirs && bigenough) {
3155 if (nd->nd_flag & ND_NFSV4) {
3156 NFSM_DISSECT(tl, u_int32_t *, 3*NFSX_UNSIGNED);
3157 ncookie.lval[0] = *tl++;
3158 ncookie.lval[1] = *tl++;
3159 len = fxdr_unsigned(int, *tl);
3160 } else if (nd->nd_flag & ND_NFSV3) {
3161 NFSM_DISSECT(tl, u_int32_t *, 3*NFSX_UNSIGNED);
3162 nfsva.na_fileid = fxdr_hyper(tl);
3164 len = fxdr_unsigned(int, *tl);
3166 NFSM_DISSECT(tl, u_int32_t *, 2*NFSX_UNSIGNED);
3167 nfsva.na_fileid = fxdr_unsigned(uint64_t,
3169 len = fxdr_unsigned(int, *tl);
3171 if (len <= 0 || len > NFS_MAXNAMLEN) {
3175 tlen = roundup2(len, 8);
3177 tlen += 8; /* To ensure null termination. */
3178 left = DIRBLKSIZ - blksiz;
3179 if (_GENERIC_DIRLEN(len) + NFSX_HYPER > left) {
3180 NFSBZERO(uiop->uio_iov->iov_base, left);
3181 dp->d_reclen += left;
3182 uiop->uio_iov->iov_base =
3183 (char *)uiop->uio_iov->iov_base + left;
3184 uiop->uio_iov->iov_len -= left;
3185 uiop->uio_resid -= left;
3186 uiop->uio_offset += left;
3189 if (_GENERIC_DIRLEN(len) + NFSX_HYPER >
3193 dp = (struct dirent *)uiop->uio_iov->iov_base;
3194 dp->d_pad0 = dp->d_pad1 = 0;
3197 dp->d_reclen = _GENERIC_DIRLEN(len) +
3199 dp->d_type = DT_UNKNOWN;
3200 blksiz += dp->d_reclen;
3201 if (blksiz == DIRBLKSIZ)
3203 uiop->uio_resid -= DIRHDSIZ;
3204 uiop->uio_offset += DIRHDSIZ;
3205 uiop->uio_iov->iov_base =
3206 (char *)uiop->uio_iov->iov_base + DIRHDSIZ;
3207 uiop->uio_iov->iov_len -= DIRHDSIZ;
3208 error = nfsm_mbufuio(nd, uiop, len);
3211 cp = uiop->uio_iov->iov_base;
3214 cp += tlen; /* points to cookie storage */
3215 tl2 = (u_int32_t *)cp;
3216 uiop->uio_iov->iov_base =
3217 (char *)uiop->uio_iov->iov_base + tlen +
3219 uiop->uio_iov->iov_len -= tlen + NFSX_HYPER;
3220 uiop->uio_resid -= tlen + NFSX_HYPER;
3221 uiop->uio_offset += (tlen + NFSX_HYPER);
3223 error = nfsm_advance(nd, NFSM_RNDUP(len), -1);
3227 if (nd->nd_flag & ND_NFSV4) {
3229 nfsva.na_mntonfileno = UINT64_MAX;
3230 error = nfsv4_loadattr(nd, NULL, &nfsva, NULL,
3231 NULL, 0, NULL, NULL, NULL, NULL, NULL, 0,
3232 NULL, NULL, &rderr, p, cred);
3235 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3236 } else if (nd->nd_flag & ND_NFSV3) {
3237 NFSM_DISSECT(tl, u_int32_t *, 3*NFSX_UNSIGNED);
3238 ncookie.lval[0] = *tl++;
3239 ncookie.lval[1] = *tl++;
3241 NFSM_DISSECT(tl, u_int32_t *, 2*NFSX_UNSIGNED);
3242 ncookie.lval[0] = 0;
3243 ncookie.lval[1] = *tl++;
3246 if (nd->nd_flag & ND_NFSV4) {
3251 if (nfsva.na_mntonfileno != UINT64_MAX)
3252 dp->d_fileno = nfsva.na_mntonfileno;
3254 dp->d_fileno = nfsva.na_fileid;
3255 } else if (nfsva.na_filesid[0] ==
3256 dnp->n_vattr.na_filesid[0] &&
3257 nfsva.na_filesid[1] ==
3258 dnp->n_vattr.na_filesid[1]) {
3259 dp->d_fileno = nfsva.na_fileid;
3263 } while (fakefileno ==
3265 dp->d_fileno = fakefileno;
3267 dp->d_type = vtonfs_dtype(nfsva.na_type);
3270 dp->d_fileno = nfsva.na_fileid;
3272 *tl2++ = cookiep->nfsuquad[0] = cookie.lval[0] =
3274 *tl2 = cookiep->nfsuquad[1] = cookie.lval[1] =
3277 more_dirs = fxdr_unsigned(int, *tl);
3280 * If at end of rpc data, get the eof boolean
3283 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3284 eof = fxdr_unsigned(int, *tl);
3287 if (nd->nd_flag & ND_NFSV4) {
3288 error = nfscl_postop_attr(nd, nap, attrflagp,
3294 m_freem(nd->nd_mrep);
3298 * Fill last record, iff any, out to a multiple of DIRBLKSIZ
3299 * by increasing d_reclen for the last record.
3302 left = DIRBLKSIZ - blksiz;
3303 NFSBZERO(uiop->uio_iov->iov_base, left);
3304 dp->d_reclen += left;
3305 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base +
3307 uiop->uio_iov->iov_len -= left;
3308 uiop->uio_resid -= left;
3309 uiop->uio_offset += left;
3313 * If returning no data, assume end of file.
3314 * If not bigenough, return not end of file, since you aren't
3315 * returning all the data
3316 * Otherwise, return the eof flag from the server.
3319 if (tresid == ((size_t)(uiop->uio_resid)))
3321 else if (!bigenough)
3328 * Add extra empty records to any remaining DIRBLKSIZ chunks.
3330 while (uiop->uio_resid > 0 && uiop->uio_resid != tresid) {
3331 dp = (struct dirent *)uiop->uio_iov->iov_base;
3332 NFSBZERO(dp, DIRBLKSIZ);
3333 dp->d_type = DT_UNKNOWN;
3334 tl = (u_int32_t *)&dp->d_name[4];
3335 *tl++ = cookie.lval[0];
3336 *tl = cookie.lval[1];
3337 dp->d_reclen = DIRBLKSIZ;
3338 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base +
3340 uiop->uio_iov->iov_len -= DIRBLKSIZ;
3341 uiop->uio_resid -= DIRBLKSIZ;
3342 uiop->uio_offset += DIRBLKSIZ;
3346 if (nd->nd_mrep != NULL)
3347 m_freem(nd->nd_mrep);
3353 * NFS V3 readdir plus RPC. Used in place of nfsrpc_readdir().
3354 * (Also used for NFS V4 when mount flag set.)
3355 * (ditto above w.r.t. multiple of DIRBLKSIZ, etc.)
3358 nfsrpc_readdirplus(vnode_t vp, struct uio *uiop, nfsuint64 *cookiep,
3359 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp,
3360 int *eofp, void *stuff)
3363 struct dirent *dp = NULL;
3365 vnode_t newvp = NULLVP;
3366 struct nfsrv_descript nfsd, *nd = &nfsd;
3367 struct nameidata nami, *ndp = &nami;
3368 struct componentname *cnp = &ndp->ni_cnd;
3369 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
3370 struct nfsnode *dnp = VTONFS(vp), *np;
3371 struct nfsvattr nfsva;
3373 nfsquad_t cookie, ncookie;
3374 int error = 0, tlen, more_dirs = 1, blksiz = 0, bigenough = 1;
3375 int attrflag, tryformoredirs = 1, eof = 0, gotmnton = 0;
3376 int isdotdot = 0, unlocknewvp = 0;
3377 u_int64_t dotfileid, dotdotfileid = 0, fakefileno = UINT64_MAX;
3378 u_int64_t fileno = 0;
3380 nfsattrbit_t attrbits, dattrbits;
3382 u_int32_t *tl2 = NULL, rderr;
3383 struct timespec dctime;
3385 KASSERT(uiop->uio_iovcnt == 1 &&
3386 (uiop->uio_resid & (DIRBLKSIZ - 1)) == 0,
3387 ("nfs readdirplusrpc bad uio"));
3388 ncookie.lval[0] = ncookie.lval[1] = 0;
3389 timespecclear(&dctime);
3395 cookie.lval[0] = cookiep->nfsuquad[0];
3396 cookie.lval[1] = cookiep->nfsuquad[1];
3397 tresid = uiop->uio_resid;
3400 * For NFSv4, first create the "." and ".." entries.
3402 if (NFSHASNFSV4(nmp)) {
3403 NFSGETATTR_ATTRBIT(&dattrbits);
3404 NFSZERO_ATTRBIT(&attrbits);
3405 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_FILEID);
3406 if (NFSISSET_ATTRBIT(&dnp->n_vattr.na_suppattr,
3407 NFSATTRBIT_MOUNTEDONFILEID)) {
3408 NFSSETBIT_ATTRBIT(&attrbits,
3409 NFSATTRBIT_MOUNTEDONFILEID);
3413 * Must fake it. Use the fileno, except when the
3414 * fsid is != to that of the directory. For that
3415 * case, generate a fake fileno that is not the same.
3417 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_FSID);
3422 * Joy, oh joy. For V4 we get to hand craft '.' and '..'.
3424 if (uiop->uio_offset == 0) {
3425 NFSCL_REQSTART(nd, NFSPROC_LOOKUPP, vp);
3426 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
3427 *tl++ = txdr_unsigned(NFSV4OP_GETFH);
3428 *tl = txdr_unsigned(NFSV4OP_GETATTR);
3429 (void) nfsrv_putattrbit(nd, &attrbits);
3430 error = nfscl_request(nd, vp, p, cred, stuff);
3433 dotfileid = 0; /* Fake out the compiler. */
3434 if ((nd->nd_flag & ND_NOMOREDATA) == 0) {
3435 error = nfsm_loadattr(nd, &nfsva);
3438 dctime = nfsva.na_ctime;
3439 dotfileid = nfsva.na_fileid;
3441 if (nd->nd_repstat == 0) {
3442 NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
3443 len = fxdr_unsigned(int, *(tl + 4));
3444 if (len > 0 && len <= NFSX_V4FHMAX)
3445 error = nfsm_advance(nd, NFSM_RNDUP(len), -1);
3449 NFSM_DISSECT(tl, u_int32_t *, 2*NFSX_UNSIGNED);
3450 nfsva.na_mntonfileno = UINT64_MAX;
3451 error = nfsv4_loadattr(nd, NULL, &nfsva, NULL,
3452 NULL, 0, NULL, NULL, NULL, NULL, NULL, 0,
3453 NULL, NULL, NULL, p, cred);
3455 dotdotfileid = dotfileid;
3456 } else if (gotmnton) {
3457 if (nfsva.na_mntonfileno != UINT64_MAX)
3458 dotdotfileid = nfsva.na_mntonfileno;
3460 dotdotfileid = nfsva.na_fileid;
3461 } else if (nfsva.na_filesid[0] ==
3462 dnp->n_vattr.na_filesid[0] &&
3463 nfsva.na_filesid[1] ==
3464 dnp->n_vattr.na_filesid[1]) {
3465 dotdotfileid = nfsva.na_fileid;
3469 } while (fakefileno ==
3471 dotdotfileid = fakefileno;
3474 } else if (nd->nd_repstat == NFSERR_NOENT) {
3476 * Lookupp returns NFSERR_NOENT when we are
3477 * at the root, so just use the current dir.
3480 dotdotfileid = dotfileid;
3482 error = nd->nd_repstat;
3484 m_freem(nd->nd_mrep);
3488 dp = (struct dirent *)uiop->uio_iov->iov_base;
3489 dp->d_pad0 = dp->d_pad1 = 0;
3491 dp->d_type = DT_DIR;
3492 dp->d_fileno = dotfileid;
3494 *((uint64_t *)dp->d_name) = 0; /* Zero pad it. */
3495 dp->d_name[0] = '.';
3496 dp->d_reclen = _GENERIC_DIRSIZ(dp) + NFSX_HYPER;
3498 * Just make these offset cookie 0.
3500 tl = (u_int32_t *)&dp->d_name[8];
3503 blksiz += dp->d_reclen;
3504 uiop->uio_resid -= dp->d_reclen;
3505 uiop->uio_offset += dp->d_reclen;
3506 uiop->uio_iov->iov_base =
3507 (char *)uiop->uio_iov->iov_base + dp->d_reclen;
3508 uiop->uio_iov->iov_len -= dp->d_reclen;
3509 dp = (struct dirent *)uiop->uio_iov->iov_base;
3510 dp->d_pad0 = dp->d_pad1 = 0;
3512 dp->d_type = DT_DIR;
3513 dp->d_fileno = dotdotfileid;
3515 *((uint64_t *)dp->d_name) = 0;
3516 dp->d_name[0] = '.';
3517 dp->d_name[1] = '.';
3518 dp->d_reclen = _GENERIC_DIRSIZ(dp) + NFSX_HYPER;
3520 * Just make these offset cookie 0.
3522 tl = (u_int32_t *)&dp->d_name[8];
3525 blksiz += dp->d_reclen;
3526 uiop->uio_resid -= dp->d_reclen;
3527 uiop->uio_offset += dp->d_reclen;
3528 uiop->uio_iov->iov_base =
3529 (char *)uiop->uio_iov->iov_base + dp->d_reclen;
3530 uiop->uio_iov->iov_len -= dp->d_reclen;
3532 NFSREADDIRPLUS_ATTRBIT(&attrbits);
3534 NFSSETBIT_ATTRBIT(&attrbits,
3535 NFSATTRBIT_MOUNTEDONFILEID);
3539 * Loop around doing readdir rpc's of size nm_readdirsize.
3540 * The stopping criteria is EOF or buffer full.
3542 while (more_dirs && bigenough) {
3544 NFSCL_REQSTART(nd, NFSPROC_READDIRPLUS, vp);
3545 NFSM_BUILD(tl, u_int32_t *, 6 * NFSX_UNSIGNED);
3546 *tl++ = cookie.lval[0];
3547 *tl++ = cookie.lval[1];
3548 if (cookie.qval == 0) {
3553 *tl++ = dnp->n_cookieverf.nfsuquad[0];
3554 *tl++ = dnp->n_cookieverf.nfsuquad[1];
3557 *tl++ = txdr_unsigned(nmp->nm_readdirsize);
3558 *tl = txdr_unsigned(nmp->nm_readdirsize);
3559 if (nd->nd_flag & ND_NFSV4) {
3560 (void) nfsrv_putattrbit(nd, &attrbits);
3561 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
3562 *tl = txdr_unsigned(NFSV4OP_GETATTR);
3563 (void) nfsrv_putattrbit(nd, &dattrbits);
3565 error = nfscl_request(nd, vp, p, cred, stuff);
3568 if (nd->nd_flag & ND_NFSV3)
3569 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
3570 if (nd->nd_repstat || error) {
3572 error = nd->nd_repstat;
3575 if ((nd->nd_flag & ND_NFSV3) != 0 && *attrflagp != 0)
3576 dctime = nap->na_ctime;
3577 NFSM_DISSECT(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
3579 dnp->n_cookieverf.nfsuquad[0] = *tl++;
3580 dnp->n_cookieverf.nfsuquad[1] = *tl++;
3582 more_dirs = fxdr_unsigned(int, *tl);
3586 /* loop through the dir entries, doctoring them to 4bsd form */
3587 while (more_dirs && bigenough) {
3588 NFSM_DISSECT(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
3589 if (nd->nd_flag & ND_NFSV4) {
3590 ncookie.lval[0] = *tl++;
3591 ncookie.lval[1] = *tl++;
3593 fileno = fxdr_hyper(tl);
3596 len = fxdr_unsigned(int, *tl);
3597 if (len <= 0 || len > NFS_MAXNAMLEN) {
3601 tlen = roundup2(len, 8);
3603 tlen += 8; /* To ensure null termination. */
3604 left = DIRBLKSIZ - blksiz;
3605 if (_GENERIC_DIRLEN(len) + NFSX_HYPER > left) {
3606 NFSBZERO(uiop->uio_iov->iov_base, left);
3607 dp->d_reclen += left;
3608 uiop->uio_iov->iov_base =
3609 (char *)uiop->uio_iov->iov_base + left;
3610 uiop->uio_iov->iov_len -= left;
3611 uiop->uio_resid -= left;
3612 uiop->uio_offset += left;
3615 if (_GENERIC_DIRLEN(len) + NFSX_HYPER >
3619 dp = (struct dirent *)uiop->uio_iov->iov_base;
3620 dp->d_pad0 = dp->d_pad1 = 0;
3623 dp->d_reclen = _GENERIC_DIRLEN(len) +
3625 dp->d_type = DT_UNKNOWN;
3626 blksiz += dp->d_reclen;
3627 if (blksiz == DIRBLKSIZ)
3629 uiop->uio_resid -= DIRHDSIZ;
3630 uiop->uio_offset += DIRHDSIZ;
3631 uiop->uio_iov->iov_base =
3632 (char *)uiop->uio_iov->iov_base + DIRHDSIZ;
3633 uiop->uio_iov->iov_len -= DIRHDSIZ;
3634 cnp->cn_nameptr = uiop->uio_iov->iov_base;
3635 cnp->cn_namelen = len;
3637 error = nfsm_mbufuio(nd, uiop, len);
3640 cp = uiop->uio_iov->iov_base;
3643 cp += tlen; /* points to cookie storage */
3644 tl2 = (u_int32_t *)cp;
3645 if (len == 2 && cnp->cn_nameptr[0] == '.' &&
3646 cnp->cn_nameptr[1] == '.')
3650 uiop->uio_iov->iov_base =
3651 (char *)uiop->uio_iov->iov_base + tlen +
3653 uiop->uio_iov->iov_len -= tlen + NFSX_HYPER;
3654 uiop->uio_resid -= tlen + NFSX_HYPER;
3655 uiop->uio_offset += (tlen + NFSX_HYPER);
3657 error = nfsm_advance(nd, NFSM_RNDUP(len), -1);
3662 if (nd->nd_flag & ND_NFSV3) {
3663 NFSM_DISSECT(tl, u_int32_t *, 3*NFSX_UNSIGNED);
3664 ncookie.lval[0] = *tl++;
3665 ncookie.lval[1] = *tl++;
3666 attrflag = fxdr_unsigned(int, *tl);
3668 error = nfsm_loadattr(nd, &nfsva);
3672 NFSM_DISSECT(tl,u_int32_t *,NFSX_UNSIGNED);
3674 error = nfsm_getfh(nd, &nfhp);
3678 if (!attrflag && nfhp != NULL) {
3679 free(nfhp, M_NFSFH);
3684 nfsva.na_mntonfileno = 0xffffffff;
3685 error = nfsv4_loadattr(nd, NULL, &nfsva, &nfhp,
3686 NULL, 0, NULL, NULL, NULL, NULL, NULL, 0,
3687 NULL, NULL, &rderr, p, cred);
3693 if (nd->nd_flag & ND_NFSV4) {
3696 } else if (gotmnton) {
3697 if (nfsva.na_mntonfileno != 0xffffffff)
3698 dp->d_fileno = nfsva.na_mntonfileno;
3700 dp->d_fileno = nfsva.na_fileid;
3701 } else if (nfsva.na_filesid[0] ==
3702 dnp->n_vattr.na_filesid[0] &&
3703 nfsva.na_filesid[1] ==
3704 dnp->n_vattr.na_filesid[1]) {
3705 dp->d_fileno = nfsva.na_fileid;
3709 } while (fakefileno ==
3711 dp->d_fileno = fakefileno;
3714 dp->d_fileno = fileno;
3716 *tl2++ = cookiep->nfsuquad[0] = cookie.lval[0] =
3718 *tl2 = cookiep->nfsuquad[1] = cookie.lval[1] =
3722 if (NFSRV_CMPFH(nfhp->nfh_fh, nfhp->nfh_len,
3723 dnp->n_fhp->nfh_fh, dnp->n_fhp->nfh_len)) {
3727 free(nfhp, M_NFSFH);
3729 } else if (isdotdot != 0) {
3731 * Skip doing a nfscl_nget() call for "..".
3732 * There's a race between acquiring the nfs
3733 * node here and lookups that look for the
3734 * directory being read (in the parent).
3735 * It would try to get a lock on ".." here,
3736 * owning the lock on the directory being
3737 * read. Lookup will hold the lock on ".."
3738 * and try to acquire the lock on the
3739 * directory being read.
3740 * If the directory is unlocked/relocked,
3741 * then there is a LOR with the buflock
3744 free(nfhp, M_NFSFH);
3746 error = nfscl_nget(vp->v_mount, vp,
3747 nfhp, cnp, p, &np, NULL, LK_EXCLUSIVE);
3754 if (newvp != NULLVP) {
3755 error = nfscl_loadattrcache(&newvp,
3756 &nfsva, NULL, NULL, 0, 0);
3765 vtonfs_dtype(np->n_vattr.na_type);
3767 NFSCNHASH(cnp, HASHINIT);
3768 if (cnp->cn_namelen <= NCHNAMLEN &&
3769 (newvp->v_type != VDIR ||
3770 dctime.tv_sec != 0)) {
3771 cache_enter_time(ndp->ni_dvp,
3774 newvp->v_type != VDIR ? NULL :
3784 } else if (nfhp != NULL) {
3785 free(nfhp, M_NFSFH);
3787 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3788 more_dirs = fxdr_unsigned(int, *tl);
3791 * If at end of rpc data, get the eof boolean
3794 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3795 eof = fxdr_unsigned(int, *tl);
3798 if (nd->nd_flag & ND_NFSV4) {
3799 error = nfscl_postop_attr(nd, nap, attrflagp,
3805 m_freem(nd->nd_mrep);
3809 * Fill last record, iff any, out to a multiple of DIRBLKSIZ
3810 * by increasing d_reclen for the last record.
3813 left = DIRBLKSIZ - blksiz;
3814 NFSBZERO(uiop->uio_iov->iov_base, left);
3815 dp->d_reclen += left;
3816 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base +
3818 uiop->uio_iov->iov_len -= left;
3819 uiop->uio_resid -= left;
3820 uiop->uio_offset += left;
3824 * If returning no data, assume end of file.
3825 * If not bigenough, return not end of file, since you aren't
3826 * returning all the data
3827 * Otherwise, return the eof flag from the server.
3830 if (tresid == uiop->uio_resid)
3832 else if (!bigenough)
3839 * Add extra empty records to any remaining DIRBLKSIZ chunks.
3841 while (uiop->uio_resid > 0 && uiop->uio_resid != tresid) {
3842 dp = (struct dirent *)uiop->uio_iov->iov_base;
3843 NFSBZERO(dp, DIRBLKSIZ);
3844 dp->d_type = DT_UNKNOWN;
3845 tl = (u_int32_t *)&dp->d_name[4];
3846 *tl++ = cookie.lval[0];
3847 *tl = cookie.lval[1];
3848 dp->d_reclen = DIRBLKSIZ;
3849 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base +
3851 uiop->uio_iov->iov_len -= DIRBLKSIZ;
3852 uiop->uio_resid -= DIRBLKSIZ;
3853 uiop->uio_offset += DIRBLKSIZ;
3857 if (nd->nd_mrep != NULL)
3858 m_freem(nd->nd_mrep);
3867 nfsrpc_commit(vnode_t vp, u_quad_t offset, int cnt, struct ucred *cred,
3868 NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, void *stuff)
3871 struct nfsrv_descript nfsd, *nd = &nfsd;
3872 nfsattrbit_t attrbits;
3874 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
3877 NFSCL_REQSTART(nd, NFSPROC_COMMIT, vp);
3878 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
3879 txdr_hyper(offset, tl);
3881 *tl = txdr_unsigned(cnt);
3882 if (nd->nd_flag & ND_NFSV4) {
3884 * And do a Getattr op.
3886 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
3887 *tl = txdr_unsigned(NFSV4OP_GETATTR);
3888 NFSGETATTR_ATTRBIT(&attrbits);
3889 (void) nfsrv_putattrbit(nd, &attrbits);
3891 error = nfscl_request(nd, vp, p, cred, stuff);
3894 error = nfscl_wcc_data(nd, vp, nap, attrflagp, NULL, stuff);
3895 if (!error && !nd->nd_repstat) {
3896 NFSM_DISSECT(tl, u_int32_t *, NFSX_VERF);
3898 if (NFSBCMP(nmp->nm_verf, tl, NFSX_VERF)) {
3899 NFSBCOPY(tl, nmp->nm_verf, NFSX_VERF);
3900 nd->nd_repstat = NFSERR_STALEWRITEVERF;
3903 if (nd->nd_flag & ND_NFSV4)
3904 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
3907 if (!error && nd->nd_repstat)
3908 error = nd->nd_repstat;
3909 m_freem(nd->nd_mrep);
3914 * NFS byte range lock rpc.
3915 * (Mostly just calls one of the three lower level RPC routines.)
3918 nfsrpc_advlock(vnode_t vp, off_t size, int op, struct flock *fl,
3919 int reclaim, struct ucred *cred, NFSPROC_T *p, void *id, int flags)
3921 struct nfscllockowner *lp;
3922 struct nfsclclient *clp;
3924 struct nfsrv_descript nfsd, *nd = &nfsd;
3925 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
3928 u_int32_t clidrev = 0;
3929 int error = 0, newone = 0, expireret = 0, retrycnt, donelocally;
3933 * Convert the flock structure into a start and end and do POSIX
3936 switch (fl->l_whence) {
3940 * Caller is responsible for adding any necessary offset
3941 * when SEEK_CUR is used.
3943 start = fl->l_start;
3947 start = size + fl->l_start;
3948 off = size + fl->l_start;
3955 if (fl->l_len != 0) {
3956 end = start + fl->l_len - 1;
3967 if (op == F_GETLK) {
3968 error = nfscl_getcl(vp->v_mount, cred, p, 1, &clp);
3971 error = nfscl_lockt(vp, clp, off, len, fl, p, id, flags);
3973 clidrev = clp->nfsc_clientidrev;
3974 error = nfsrpc_lockt(nd, vp, clp, off, len, fl, cred,
3976 } else if (error == -1) {
3979 nfscl_clientrelease(clp);
3980 } else if (op == F_UNLCK && fl->l_type == F_UNLCK) {
3982 * We must loop around for all lockowner cases.
3985 error = nfscl_getcl(vp->v_mount, cred, p, 1, &clp);
3989 error = nfscl_relbytelock(vp, off, len, cred, p, callcnt,
3990 clp, id, flags, &lp, &dorpc);
3992 * If it returns a NULL lp, we're done.
3996 nfscl_clientrelease(clp);
3998 nfscl_releasealllocks(clp, vp, p, id, flags);
4001 if (nmp->nm_clp != NULL)
4002 clidrev = nmp->nm_clp->nfsc_clientidrev;
4006 * If the server doesn't support Posix lock semantics,
4007 * only allow locks on the entire file, since it won't
4008 * handle overlapping byte ranges.
4009 * There might still be a problem when a lock
4010 * upgrade/downgrade (read<->write) occurs, since the
4011 * server "might" expect an unlock first?
4013 if (dorpc && (lp->nfsl_open->nfso_posixlock ||
4014 (off == 0 && len == NFS64BITSSET))) {
4016 * Since the lock records will go away, we must
4017 * wait for grace and delay here.
4020 error = nfsrpc_locku(nd, nmp, lp, off, len,
4021 NFSV4LOCKT_READ, cred, p, 0);
4022 if ((nd->nd_repstat == NFSERR_GRACE ||
4023 nd->nd_repstat == NFSERR_DELAY) &&
4025 (void) nfs_catnap(PZERO, (int)nd->nd_repstat,
4027 } while ((nd->nd_repstat == NFSERR_GRACE ||
4028 nd->nd_repstat == NFSERR_DELAY) && error == 0);
4031 } while (error == 0 && nd->nd_repstat == 0);
4032 nfscl_releasealllocks(clp, vp, p, id, flags);
4033 } else if (op == F_SETLK) {
4034 error = nfscl_getbytelock(vp, off, len, fl->l_type, cred, p,
4035 NULL, 0, id, flags, NULL, NULL, &lp, &newone, &donelocally);
4036 if (error || donelocally) {
4039 if (nmp->nm_clp != NULL)
4040 clidrev = nmp->nm_clp->nfsc_clientidrev;
4043 nfhp = VTONFS(vp)->n_fhp;
4044 if (!lp->nfsl_open->nfso_posixlock &&
4045 (off != 0 || len != NFS64BITSSET)) {
4048 error = nfsrpc_lock(nd, nmp, vp, nfhp->nfh_fh,
4049 nfhp->nfh_len, lp, newone, reclaim, off,
4050 len, fl->l_type, cred, p, 0);
4053 error = nd->nd_repstat;
4054 nfscl_lockrelease(lp, error, newone);
4059 error = nd->nd_repstat;
4060 if (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
4061 error == NFSERR_STALEDONTRECOVER ||
4062 error == NFSERR_STALECLIENTID || error == NFSERR_DELAY ||
4063 error == NFSERR_BADSESSION) {
4064 (void) nfs_catnap(PZERO, error, "nfs_advlock");
4065 } else if ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID)
4067 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
4070 } while (error == NFSERR_GRACE ||
4071 error == NFSERR_STALECLIENTID || error == NFSERR_DELAY ||
4072 error == NFSERR_STALEDONTRECOVER || error == NFSERR_STALESTATEID ||
4073 error == NFSERR_BADSESSION ||
4074 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
4075 expireret == 0 && clidrev != 0 && retrycnt < 4));
4076 if (error && retrycnt >= 4)
4082 * The lower level routine for the LockT case.
4085 nfsrpc_lockt(struct nfsrv_descript *nd, vnode_t vp,
4086 struct nfsclclient *clp, u_int64_t off, u_int64_t len, struct flock *fl,
4087 struct ucred *cred, NFSPROC_T *p, void *id, int flags)
4090 int error, type, size;
4091 uint8_t own[NFSV4CL_LOCKNAMELEN + NFSX_V4FHMAX];
4093 struct nfsmount *nmp;
4094 struct nfsclsession *tsep;
4096 nmp = VFSTONFS(vp->v_mount);
4097 NFSCL_REQSTART(nd, NFSPROC_LOCKT, vp);
4098 NFSM_BUILD(tl, u_int32_t *, 7 * NFSX_UNSIGNED);
4099 if (fl->l_type == F_RDLCK)
4100 *tl++ = txdr_unsigned(NFSV4LOCKT_READ);
4102 *tl++ = txdr_unsigned(NFSV4LOCKT_WRITE);
4103 txdr_hyper(off, tl);
4105 txdr_hyper(len, tl);
4107 tsep = nfsmnt_mdssession(nmp);
4108 *tl++ = tsep->nfsess_clientid.lval[0];
4109 *tl = tsep->nfsess_clientid.lval[1];
4110 nfscl_filllockowner(id, own, flags);
4112 NFSBCOPY(np->n_fhp->nfh_fh, &own[NFSV4CL_LOCKNAMELEN],
4113 np->n_fhp->nfh_len);
4114 (void)nfsm_strtom(nd, own, NFSV4CL_LOCKNAMELEN + np->n_fhp->nfh_len);
4115 error = nfscl_request(nd, vp, p, cred, NULL);
4118 if (nd->nd_repstat == 0) {
4119 fl->l_type = F_UNLCK;
4120 } else if (nd->nd_repstat == NFSERR_DENIED) {
4122 fl->l_whence = SEEK_SET;
4123 NFSM_DISSECT(tl, u_int32_t *, 8 * NFSX_UNSIGNED);
4124 fl->l_start = fxdr_hyper(tl);
4126 len = fxdr_hyper(tl);
4128 if (len == NFS64BITSSET)
4132 type = fxdr_unsigned(int, *tl++);
4133 if (type == NFSV4LOCKT_WRITE)
4134 fl->l_type = F_WRLCK;
4136 fl->l_type = F_RDLCK;
4138 * XXX For now, I have no idea what to do with the
4139 * conflicting lock_owner, so I'll just set the pid == 0
4140 * and skip over the lock_owner.
4142 fl->l_pid = (pid_t)0;
4144 size = fxdr_unsigned(int, *tl);
4145 if (size < 0 || size > NFSV4_OPAQUELIMIT)
4148 error = nfsm_advance(nd, NFSM_RNDUP(size), -1);
4149 } else if (nd->nd_repstat == NFSERR_STALECLIENTID)
4150 nfscl_initiate_recovery(clp);
4152 m_freem(nd->nd_mrep);
4157 * Lower level function that performs the LockU RPC.
4160 nfsrpc_locku(struct nfsrv_descript *nd, struct nfsmount *nmp,
4161 struct nfscllockowner *lp, u_int64_t off, u_int64_t len,
4162 u_int32_t type, struct ucred *cred, NFSPROC_T *p, int syscred)
4167 nfscl_reqstart(nd, NFSPROC_LOCKU, nmp, lp->nfsl_open->nfso_fh,
4168 lp->nfsl_open->nfso_fhlen, NULL, NULL, 0, 0, false);
4169 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID + 6 * NFSX_UNSIGNED);
4170 *tl++ = txdr_unsigned(type);
4171 *tl = txdr_unsigned(lp->nfsl_seqid);
4172 if (nfstest_outofseq &&
4173 (arc4random() % nfstest_outofseq) == 0)
4174 *tl = txdr_unsigned(lp->nfsl_seqid + 1);
4176 if (NFSHASNFSV4N(nmp))
4179 *tl++ = lp->nfsl_stateid.seqid;
4180 *tl++ = lp->nfsl_stateid.other[0];
4181 *tl++ = lp->nfsl_stateid.other[1];
4182 *tl++ = lp->nfsl_stateid.other[2];
4183 txdr_hyper(off, tl);
4185 txdr_hyper(len, tl);
4187 nd->nd_flag |= ND_USEGSSNAME;
4188 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4189 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4190 NFSCL_INCRSEQID(lp->nfsl_seqid, nd);
4193 if (nd->nd_repstat == 0) {
4194 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID);
4195 lp->nfsl_stateid.seqid = *tl++;
4196 lp->nfsl_stateid.other[0] = *tl++;
4197 lp->nfsl_stateid.other[1] = *tl++;
4198 lp->nfsl_stateid.other[2] = *tl;
4199 } else if (nd->nd_repstat == NFSERR_STALESTATEID)
4200 nfscl_initiate_recovery(lp->nfsl_open->nfso_own->nfsow_clp);
4202 m_freem(nd->nd_mrep);
4207 * The actual Lock RPC.
4210 nfsrpc_lock(struct nfsrv_descript *nd, struct nfsmount *nmp, vnode_t vp,
4211 u_int8_t *nfhp, int fhlen, struct nfscllockowner *lp, int newone,
4212 int reclaim, u_int64_t off, u_int64_t len, short type, struct ucred *cred,
4213 NFSPROC_T *p, int syscred)
4217 uint8_t own[NFSV4CL_LOCKNAMELEN + NFSX_V4FHMAX];
4218 struct nfsclsession *tsep;
4220 nfscl_reqstart(nd, NFSPROC_LOCK, nmp, nfhp, fhlen, NULL, NULL, 0, 0,
4222 NFSM_BUILD(tl, u_int32_t *, 7 * NFSX_UNSIGNED);
4223 if (type == F_RDLCK)
4224 *tl++ = txdr_unsigned(NFSV4LOCKT_READ);
4226 *tl++ = txdr_unsigned(NFSV4LOCKT_WRITE);
4227 *tl++ = txdr_unsigned(reclaim);
4228 txdr_hyper(off, tl);
4230 txdr_hyper(len, tl);
4234 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID +
4235 2 * NFSX_UNSIGNED + NFSX_HYPER);
4236 *tl++ = txdr_unsigned(lp->nfsl_open->nfso_own->nfsow_seqid);
4237 if (NFSHASNFSV4N(nmp))
4240 *tl++ = lp->nfsl_open->nfso_stateid.seqid;
4241 *tl++ = lp->nfsl_open->nfso_stateid.other[0];
4242 *tl++ = lp->nfsl_open->nfso_stateid.other[1];
4243 *tl++ = lp->nfsl_open->nfso_stateid.other[2];
4244 *tl++ = txdr_unsigned(lp->nfsl_seqid);
4245 tsep = nfsmnt_mdssession(nmp);
4246 *tl++ = tsep->nfsess_clientid.lval[0];
4247 *tl = tsep->nfsess_clientid.lval[1];
4248 NFSBCOPY(lp->nfsl_owner, own, NFSV4CL_LOCKNAMELEN);
4249 NFSBCOPY(nfhp, &own[NFSV4CL_LOCKNAMELEN], fhlen);
4250 (void)nfsm_strtom(nd, own, NFSV4CL_LOCKNAMELEN + fhlen);
4253 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID + NFSX_UNSIGNED);
4254 if (NFSHASNFSV4N(nmp))
4257 *tl++ = lp->nfsl_stateid.seqid;
4258 *tl++ = lp->nfsl_stateid.other[0];
4259 *tl++ = lp->nfsl_stateid.other[1];
4260 *tl++ = lp->nfsl_stateid.other[2];
4261 *tl = txdr_unsigned(lp->nfsl_seqid);
4262 if (nfstest_outofseq &&
4263 (arc4random() % nfstest_outofseq) == 0)
4264 *tl = txdr_unsigned(lp->nfsl_seqid + 1);
4267 nd->nd_flag |= ND_USEGSSNAME;
4268 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, vp, p, cred,
4269 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4273 NFSCL_INCRSEQID(lp->nfsl_open->nfso_own->nfsow_seqid, nd);
4274 NFSCL_INCRSEQID(lp->nfsl_seqid, nd);
4275 if (nd->nd_repstat == 0) {
4276 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID);
4277 lp->nfsl_stateid.seqid = *tl++;
4278 lp->nfsl_stateid.other[0] = *tl++;
4279 lp->nfsl_stateid.other[1] = *tl++;
4280 lp->nfsl_stateid.other[2] = *tl;
4281 } else if (nd->nd_repstat == NFSERR_DENIED) {
4282 NFSM_DISSECT(tl, u_int32_t *, 8 * NFSX_UNSIGNED);
4283 size = fxdr_unsigned(int, *(tl + 7));
4284 if (size < 0 || size > NFSV4_OPAQUELIMIT)
4287 error = nfsm_advance(nd, NFSM_RNDUP(size), -1);
4288 } else if (nd->nd_repstat == NFSERR_STALESTATEID)
4289 nfscl_initiate_recovery(lp->nfsl_open->nfso_own->nfsow_clp);
4291 m_freem(nd->nd_mrep);
4297 * (always called with the vp for the mount point)
4300 nfsrpc_statfs(vnode_t vp, struct nfsstatfs *sbp, struct nfsfsinfo *fsp,
4301 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp,
4304 u_int32_t *tl = NULL;
4305 struct nfsrv_descript nfsd, *nd = &nfsd;
4306 struct nfsmount *nmp;
4307 nfsattrbit_t attrbits;
4311 nmp = VFSTONFS(vp->v_mount);
4312 if (NFSHASNFSV4(nmp)) {
4314 * For V4, you actually do a getattr.
4316 NFSCL_REQSTART(nd, NFSPROC_GETATTR, vp);
4317 NFSSTATFS_GETATTRBIT(&attrbits);
4318 (void) nfsrv_putattrbit(nd, &attrbits);
4319 nd->nd_flag |= ND_USEGSSNAME;
4320 error = nfscl_request(nd, vp, p, cred, stuff);
4323 if (nd->nd_repstat == 0) {
4324 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0,
4325 NULL, NULL, sbp, fsp, NULL, 0, NULL, NULL, NULL, p,
4328 nmp->nm_fsid[0] = nap->na_filesid[0];
4329 nmp->nm_fsid[1] = nap->na_filesid[1];
4330 NFSSETHASSETFSID(nmp);
4334 error = nd->nd_repstat;
4339 NFSCL_REQSTART(nd, NFSPROC_FSSTAT, vp);
4340 error = nfscl_request(nd, vp, p, cred, stuff);
4343 if (nd->nd_flag & ND_NFSV3) {
4344 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
4348 if (nd->nd_repstat) {
4349 error = nd->nd_repstat;
4352 NFSM_DISSECT(tl, u_int32_t *,
4353 NFSX_STATFS(nd->nd_flag & ND_NFSV3));
4355 if (NFSHASNFSV3(nmp)) {
4356 sbp->sf_tbytes = fxdr_hyper(tl); tl += 2;
4357 sbp->sf_fbytes = fxdr_hyper(tl); tl += 2;
4358 sbp->sf_abytes = fxdr_hyper(tl); tl += 2;
4359 sbp->sf_tfiles = fxdr_hyper(tl); tl += 2;
4360 sbp->sf_ffiles = fxdr_hyper(tl); tl += 2;
4361 sbp->sf_afiles = fxdr_hyper(tl); tl += 2;
4362 sbp->sf_invarsec = fxdr_unsigned(u_int32_t, *tl);
4363 } else if (NFSHASNFSV4(nmp) == 0) {
4364 sbp->sf_tsize = fxdr_unsigned(u_int32_t, *tl++);
4365 sbp->sf_bsize = fxdr_unsigned(u_int32_t, *tl++);
4366 sbp->sf_blocks = fxdr_unsigned(u_int32_t, *tl++);
4367 sbp->sf_bfree = fxdr_unsigned(u_int32_t, *tl++);
4368 sbp->sf_bavail = fxdr_unsigned(u_int32_t, *tl);
4371 m_freem(nd->nd_mrep);
4379 nfsrpc_pathconf(vnode_t vp, struct nfsv3_pathconf *pc,
4380 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp,
4383 struct nfsrv_descript nfsd, *nd = &nfsd;
4384 struct nfsmount *nmp;
4386 nfsattrbit_t attrbits;
4390 nmp = VFSTONFS(vp->v_mount);
4391 if (NFSHASNFSV4(nmp)) {
4393 * For V4, you actually do a getattr.
4395 NFSCL_REQSTART(nd, NFSPROC_GETATTR, vp);
4396 NFSPATHCONF_GETATTRBIT(&attrbits);
4397 (void) nfsrv_putattrbit(nd, &attrbits);
4398 nd->nd_flag |= ND_USEGSSNAME;
4399 error = nfscl_request(nd, vp, p, cred, stuff);
4402 if (nd->nd_repstat == 0) {
4403 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0,
4404 pc, NULL, NULL, NULL, NULL, 0, NULL, NULL, NULL, p,
4409 error = nd->nd_repstat;
4412 NFSCL_REQSTART(nd, NFSPROC_PATHCONF, vp);
4413 error = nfscl_request(nd, vp, p, cred, stuff);
4416 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
4417 if (nd->nd_repstat && !error)
4418 error = nd->nd_repstat;
4420 NFSM_DISSECT(tl, u_int32_t *, NFSX_V3PATHCONF);
4421 pc->pc_linkmax = fxdr_unsigned(u_int32_t, *tl++);
4422 pc->pc_namemax = fxdr_unsigned(u_int32_t, *tl++);
4423 pc->pc_notrunc = fxdr_unsigned(u_int32_t, *tl++);
4424 pc->pc_chownrestricted =
4425 fxdr_unsigned(u_int32_t, *tl++);
4426 pc->pc_caseinsensitive =
4427 fxdr_unsigned(u_int32_t, *tl++);
4428 pc->pc_casepreserving = fxdr_unsigned(u_int32_t, *tl);
4432 m_freem(nd->nd_mrep);
4437 * nfs version 3 fsinfo rpc call
4440 nfsrpc_fsinfo(vnode_t vp, struct nfsfsinfo *fsp, struct ucred *cred,
4441 NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, void *stuff)
4444 struct nfsrv_descript nfsd, *nd = &nfsd;
4448 NFSCL_REQSTART(nd, NFSPROC_FSINFO, vp);
4449 error = nfscl_request(nd, vp, p, cred, stuff);
4452 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
4453 if (nd->nd_repstat && !error)
4454 error = nd->nd_repstat;
4456 NFSM_DISSECT(tl, u_int32_t *, NFSX_V3FSINFO);
4457 fsp->fs_rtmax = fxdr_unsigned(u_int32_t, *tl++);
4458 fsp->fs_rtpref = fxdr_unsigned(u_int32_t, *tl++);
4459 fsp->fs_rtmult = fxdr_unsigned(u_int32_t, *tl++);
4460 fsp->fs_wtmax = fxdr_unsigned(u_int32_t, *tl++);
4461 fsp->fs_wtpref = fxdr_unsigned(u_int32_t, *tl++);
4462 fsp->fs_wtmult = fxdr_unsigned(u_int32_t, *tl++);
4463 fsp->fs_dtpref = fxdr_unsigned(u_int32_t, *tl++);
4464 fsp->fs_maxfilesize = fxdr_hyper(tl);
4466 fxdr_nfsv3time(tl, &fsp->fs_timedelta);
4468 fsp->fs_properties = fxdr_unsigned(u_int32_t, *tl);
4471 m_freem(nd->nd_mrep);
4476 * This function performs the Renew RPC.
4479 nfsrpc_renew(struct nfsclclient *clp, struct nfsclds *dsp, struct ucred *cred,
4483 struct nfsrv_descript nfsd;
4484 struct nfsrv_descript *nd = &nfsd;
4485 struct nfsmount *nmp;
4487 struct nfssockreq *nrp;
4488 struct nfsclsession *tsep;
4490 nmp = clp->nfsc_nmp;
4494 nfscl_reqstart(nd, NFSPROC_RENEW, nmp, NULL, 0, NULL, NULL, 0,
4497 nfscl_reqstart(nd, NFSPROC_RENEW, nmp, NULL, 0, NULL,
4498 &dsp->nfsclds_sess, 0, 0, false);
4499 if (!NFSHASNFSV4N(nmp)) {
4500 /* NFSv4.1 just uses a Sequence Op and not a Renew. */
4501 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
4502 tsep = nfsmnt_mdssession(nmp);
4503 *tl++ = tsep->nfsess_clientid.lval[0];
4504 *tl = tsep->nfsess_clientid.lval[1];
4508 nrp = dsp->nfsclds_sockp;
4510 /* If NULL, use the MDS socket. */
4511 nrp = &nmp->nm_sockreq;
4512 nd->nd_flag |= ND_USEGSSNAME;
4514 error = newnfs_request(nd, nmp, NULL, nrp, NULL, p, cred,
4515 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4517 error = newnfs_request(nd, nmp, NULL, nrp, NULL, p, cred,
4518 NFS_PROG, NFS_VER4, NULL, 1, NULL, &dsp->nfsclds_sess);
4520 nfscl_cancelreqs(dsp);
4524 error = nd->nd_repstat;
4525 m_freem(nd->nd_mrep);
4530 * This function performs the Releaselockowner RPC.
4533 nfsrpc_rellockown(struct nfsmount *nmp, struct nfscllockowner *lp,
4534 uint8_t *fh, int fhlen, struct ucred *cred, NFSPROC_T *p)
4536 struct nfsrv_descript nfsd, *nd = &nfsd;
4539 uint8_t own[NFSV4CL_LOCKNAMELEN + NFSX_V4FHMAX];
4540 struct nfsclsession *tsep;
4542 if (NFSHASNFSV4N(nmp)) {
4543 /* For NFSv4.1, do a FreeStateID. */
4544 nfscl_reqstart(nd, NFSPROC_FREESTATEID, nmp, NULL, 0, NULL,
4546 nfsm_stateidtom(nd, &lp->nfsl_stateid, NFSSTATEID_PUTSTATEID);
4548 nfscl_reqstart(nd, NFSPROC_RELEASELCKOWN, nmp, NULL, 0, NULL,
4550 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
4551 tsep = nfsmnt_mdssession(nmp);
4552 *tl++ = tsep->nfsess_clientid.lval[0];
4553 *tl = tsep->nfsess_clientid.lval[1];
4554 NFSBCOPY(lp->nfsl_owner, own, NFSV4CL_LOCKNAMELEN);
4555 NFSBCOPY(fh, &own[NFSV4CL_LOCKNAMELEN], fhlen);
4556 (void)nfsm_strtom(nd, own, NFSV4CL_LOCKNAMELEN + fhlen);
4558 nd->nd_flag |= ND_USEGSSNAME;
4559 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4560 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4563 error = nd->nd_repstat;
4564 m_freem(nd->nd_mrep);
4569 * This function performs the Compound to get the mount pt FH.
4572 nfsrpc_getdirpath(struct nfsmount *nmp, u_char *dirpath, struct ucred *cred,
4576 struct nfsrv_descript nfsd;
4577 struct nfsrv_descript *nd = &nfsd;
4579 int error, cnt, len, setnil;
4582 nfscl_reqstart(nd, NFSPROC_PUTROOTFH, nmp, NULL, 0, &opcntp, NULL, 0,
4591 while (*cp2 != '\0' && *cp2 != '/')
4598 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
4599 *tl = txdr_unsigned(NFSV4OP_LOOKUP);
4600 nfsm_strtom(nd, cp, strlen(cp));
4606 } while (*cp != '\0');
4607 if (NFSHASNFSV4N(nmp))
4608 /* Has a Sequence Op done by nfscl_reqstart(). */
4609 *opcntp = txdr_unsigned(3 + cnt);
4611 *opcntp = txdr_unsigned(2 + cnt);
4612 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
4613 *tl = txdr_unsigned(NFSV4OP_GETFH);
4614 nd->nd_flag |= ND_USEGSSNAME;
4615 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4616 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4619 if (nd->nd_repstat == 0) {
4620 NFSM_DISSECT(tl, u_int32_t *, (3 + 2 * cnt) * NFSX_UNSIGNED);
4621 tl += (2 + 2 * cnt);
4622 if ((len = fxdr_unsigned(int, *tl)) <= 0 ||
4624 nd->nd_repstat = NFSERR_BADXDR;
4626 nd->nd_repstat = nfsrv_mtostr(nd, nmp->nm_fh, len);
4627 if (nd->nd_repstat == 0)
4628 nmp->nm_fhsize = len;
4631 error = nd->nd_repstat;
4633 m_freem(nd->nd_mrep);
4638 * This function performs the Delegreturn RPC.
4641 nfsrpc_delegreturn(struct nfscldeleg *dp, struct ucred *cred,
4642 struct nfsmount *nmp, NFSPROC_T *p, int syscred)
4645 struct nfsrv_descript nfsd;
4646 struct nfsrv_descript *nd = &nfsd;
4649 nfscl_reqstart(nd, NFSPROC_DELEGRETURN, nmp, dp->nfsdl_fh,
4650 dp->nfsdl_fhlen, NULL, NULL, 0, 0, false);
4651 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID);
4652 if (NFSHASNFSV4N(nmp))
4655 *tl++ = dp->nfsdl_stateid.seqid;
4656 *tl++ = dp->nfsdl_stateid.other[0];
4657 *tl++ = dp->nfsdl_stateid.other[1];
4658 *tl = dp->nfsdl_stateid.other[2];
4660 nd->nd_flag |= ND_USEGSSNAME;
4661 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4662 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4665 error = nd->nd_repstat;
4666 m_freem(nd->nd_mrep);
4674 nfsrpc_getacl(vnode_t vp, struct ucred *cred, NFSPROC_T *p,
4675 struct acl *aclp, void *stuff)
4677 struct nfsrv_descript nfsd, *nd = &nfsd;
4679 nfsattrbit_t attrbits;
4680 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
4682 if (nfsrv_useacl == 0 || !NFSHASNFSV4(nmp))
4683 return (EOPNOTSUPP);
4684 NFSCL_REQSTART(nd, NFSPROC_GETACL, vp);
4685 NFSZERO_ATTRBIT(&attrbits);
4686 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_ACL);
4687 (void) nfsrv_putattrbit(nd, &attrbits);
4688 error = nfscl_request(nd, vp, p, cred, stuff);
4691 if (!nd->nd_repstat)
4692 error = nfsv4_loadattr(nd, vp, NULL, NULL, NULL, 0, NULL,
4693 NULL, NULL, NULL, aclp, 0, NULL, NULL, NULL, p, cred);
4695 error = nd->nd_repstat;
4696 m_freem(nd->nd_mrep);
4704 nfsrpc_setacl(vnode_t vp, struct ucred *cred, NFSPROC_T *p,
4705 struct acl *aclp, void *stuff)
4708 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
4710 if (nfsrv_useacl == 0 || !NFSHASNFSV4(nmp))
4711 return (EOPNOTSUPP);
4712 error = nfsrpc_setattr(vp, NULL, aclp, cred, p, NULL, NULL, stuff);
4720 nfsrpc_setaclrpc(vnode_t vp, struct ucred *cred, NFSPROC_T *p,
4721 struct acl *aclp, nfsv4stateid_t *stateidp, void *stuff)
4723 struct nfsrv_descript nfsd, *nd = &nfsd;
4725 nfsattrbit_t attrbits;
4726 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
4728 if (!NFSHASNFSV4(nmp))
4729 return (EOPNOTSUPP);
4730 NFSCL_REQSTART(nd, NFSPROC_SETACL, vp);
4731 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID);
4732 NFSZERO_ATTRBIT(&attrbits);
4733 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_ACL);
4734 (void) nfsv4_fillattr(nd, vp->v_mount, vp, aclp, NULL, NULL, 0,
4735 &attrbits, NULL, NULL, 0, 0, 0, 0, (uint64_t)0, NULL);
4736 error = nfscl_request(nd, vp, p, cred, stuff);
4739 /* Don't care about the pre/postop attributes */
4740 m_freem(nd->nd_mrep);
4741 return (nd->nd_repstat);
4745 * Do the NFSv4.1 Exchange ID.
4748 nfsrpc_exchangeid(struct nfsmount *nmp, struct nfsclclient *clp,
4749 struct nfssockreq *nrp, int minorvers, uint32_t exchflags,
4750 struct nfsclds **dspp, struct ucred *cred, NFSPROC_T *p)
4752 uint32_t *tl, v41flags;
4753 struct nfsrv_descript nfsd;
4754 struct nfsrv_descript *nd = &nfsd;
4755 struct nfsclds *dsp;
4756 struct timespec verstime;
4761 minorvers = nmp->nm_minorvers;
4762 nfscl_reqstart(nd, NFSPROC_EXCHANGEID, nmp, NULL, 0, NULL, NULL,
4763 NFS_VER4, minorvers, false);
4764 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_UNSIGNED);
4765 *tl++ = txdr_unsigned(nfsboottime.tv_sec); /* Client owner */
4766 *tl = txdr_unsigned(clp->nfsc_rev);
4767 (void) nfsm_strtom(nd, clp->nfsc_id, clp->nfsc_idlen);
4769 NFSM_BUILD(tl, uint32_t *, 3 * NFSX_UNSIGNED);
4770 *tl++ = txdr_unsigned(exchflags);
4771 *tl++ = txdr_unsigned(NFSV4EXCH_SP4NONE);
4773 /* Set the implementation id4 */
4774 *tl = txdr_unsigned(1);
4775 (void) nfsm_strtom(nd, "freebsd.org", strlen("freebsd.org"));
4776 (void) nfsm_strtom(nd, version, strlen(version));
4777 NFSM_BUILD(tl, uint32_t *, NFSX_V4TIME);
4778 verstime.tv_sec = 1293840000; /* Jan 1, 2011 */
4779 verstime.tv_nsec = 0;
4780 txdr_nfsv4time(&verstime, tl);
4781 nd->nd_flag |= ND_USEGSSNAME;
4782 error = newnfs_request(nd, nmp, NULL, nrp, NULL, p, cred,
4783 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4784 NFSCL_DEBUG(1, "exchangeid err=%d reps=%d\n", error,
4785 (int)nd->nd_repstat);
4788 if (nd->nd_repstat == 0) {
4789 NFSM_DISSECT(tl, uint32_t *, 6 * NFSX_UNSIGNED + NFSX_HYPER);
4790 len = fxdr_unsigned(int, *(tl + 7));
4791 if (len < 0 || len > NFSV4_OPAQUELIMIT) {
4792 error = NFSERR_BADXDR;
4795 dsp = malloc(sizeof(struct nfsclds) + len + 1, M_NFSCLDS,
4797 dsp->nfsclds_expire = NFSD_MONOSEC + clp->nfsc_renew;
4798 dsp->nfsclds_servownlen = len;
4799 dsp->nfsclds_sess.nfsess_clientid.lval[0] = *tl++;
4800 dsp->nfsclds_sess.nfsess_clientid.lval[1] = *tl++;
4801 dsp->nfsclds_sess.nfsess_sequenceid =
4802 fxdr_unsigned(uint32_t, *tl++);
4803 v41flags = fxdr_unsigned(uint32_t, *tl);
4804 if ((v41flags & NFSV4EXCH_USEPNFSMDS) != 0 &&
4805 NFSHASPNFSOPT(nmp)) {
4806 NFSCL_DEBUG(1, "set PNFS\n");
4808 nmp->nm_state |= NFSSTA_PNFS;
4810 dsp->nfsclds_flags |= NFSCLDS_MDS;
4812 if ((v41flags & NFSV4EXCH_USEPNFSDS) != 0)
4813 dsp->nfsclds_flags |= NFSCLDS_DS;
4814 if (minorvers == NFSV42_MINORVERSION)
4815 dsp->nfsclds_flags |= NFSCLDS_MINORV2;
4817 nd->nd_repstat = nfsrv_mtostr(nd,
4818 dsp->nfsclds_serverown, len);
4819 if (nd->nd_repstat == 0) {
4820 mtx_init(&dsp->nfsclds_mtx, "nfsds", NULL, MTX_DEF);
4821 mtx_init(&dsp->nfsclds_sess.nfsess_mtx, "nfssession",
4823 nfscl_initsessionslots(&dsp->nfsclds_sess);
4826 free(dsp, M_NFSCLDS);
4828 error = nd->nd_repstat;
4830 m_freem(nd->nd_mrep);
4835 * Do the NFSv4.1 Create Session.
4838 nfsrpc_createsession(struct nfsmount *nmp, struct nfsclsession *sep,
4839 struct nfssockreq *nrp, struct nfsclds *dsp, uint32_t sequenceid, int mds,
4840 struct ucred *cred, NFSPROC_T *p)
4842 uint32_t crflags, maxval, *tl;
4843 struct nfsrv_descript nfsd;
4844 struct nfsrv_descript *nd = &nfsd;
4845 int error, irdcnt, minorvers;
4847 /* Make sure nm_rsize, nm_wsize is set. */
4848 if (nmp->nm_rsize > NFS_MAXBSIZE || nmp->nm_rsize == 0)
4849 nmp->nm_rsize = NFS_MAXBSIZE;
4850 if (nmp->nm_wsize > NFS_MAXBSIZE || nmp->nm_wsize == 0)
4851 nmp->nm_wsize = NFS_MAXBSIZE;
4853 minorvers = nmp->nm_minorvers;
4854 else if ((dsp->nfsclds_flags & NFSCLDS_MINORV2) != 0)
4855 minorvers = NFSV42_MINORVERSION;
4857 minorvers = NFSV41_MINORVERSION;
4858 nfscl_reqstart(nd, NFSPROC_CREATESESSION, nmp, NULL, 0, NULL, NULL,
4859 NFS_VER4, minorvers, false);
4860 NFSM_BUILD(tl, uint32_t *, 4 * NFSX_UNSIGNED);
4861 *tl++ = sep->nfsess_clientid.lval[0];
4862 *tl++ = sep->nfsess_clientid.lval[1];
4863 *tl++ = txdr_unsigned(sequenceid);
4864 crflags = (NFSMNT_RDONLY(nmp->nm_mountp) ? 0 : NFSV4CRSESS_PERSIST);
4865 if (nfscl_enablecallb != 0 && nfs_numnfscbd > 0 && mds != 0)
4866 crflags |= NFSV4CRSESS_CONNBACKCHAN;
4867 *tl = txdr_unsigned(crflags);
4869 /* Fill in fore channel attributes. */
4870 NFSM_BUILD(tl, uint32_t *, 7 * NFSX_UNSIGNED);
4871 *tl++ = 0; /* Header pad size */
4872 if ((nd->nd_flag & ND_NFSV42) != 0 && mds != 0 && sb_max_adj >=
4873 nmp->nm_wsize && sb_max_adj >= nmp->nm_rsize) {
4875 * NFSv4.2 Extended Attribute operations may want to do
4876 * requests/replies that are larger than nm_rsize/nm_wsize.
4878 *tl++ = txdr_unsigned(sb_max_adj - NFS_MAXXDR);
4879 *tl++ = txdr_unsigned(sb_max_adj - NFS_MAXXDR);
4881 *tl++ = txdr_unsigned(nmp->nm_wsize + NFS_MAXXDR);
4882 *tl++ = txdr_unsigned(nmp->nm_rsize + NFS_MAXXDR);
4884 *tl++ = txdr_unsigned(4096); /* Max response size cached */
4885 *tl++ = txdr_unsigned(20); /* Max operations */
4886 *tl++ = txdr_unsigned(64); /* Max slots */
4887 *tl = 0; /* No rdma ird */
4889 /* Fill in back channel attributes. */
4890 NFSM_BUILD(tl, uint32_t *, 7 * NFSX_UNSIGNED);
4891 *tl++ = 0; /* Header pad size */
4892 *tl++ = txdr_unsigned(10000); /* Max request size */
4893 *tl++ = txdr_unsigned(10000); /* Max response size */
4894 *tl++ = txdr_unsigned(4096); /* Max response size cached */
4895 *tl++ = txdr_unsigned(4); /* Max operations */
4896 *tl++ = txdr_unsigned(NFSV4_CBSLOTS); /* Max slots */
4897 *tl = 0; /* No rdma ird */
4899 NFSM_BUILD(tl, uint32_t *, 8 * NFSX_UNSIGNED);
4900 *tl++ = txdr_unsigned(NFS_CALLBCKPROG); /* Call back prog # */
4902 /* Allow AUTH_SYS callbacks as uid, gid == 0. */
4903 *tl++ = txdr_unsigned(1); /* Auth_sys only */
4904 *tl++ = txdr_unsigned(AUTH_SYS); /* AUTH_SYS type */
4905 *tl++ = txdr_unsigned(nfsboottime.tv_sec); /* time stamp */
4906 *tl++ = 0; /* Null machine name */
4907 *tl++ = 0; /* Uid == 0 */
4908 *tl++ = 0; /* Gid == 0 */
4909 *tl = 0; /* No additional gids */
4910 nd->nd_flag |= ND_USEGSSNAME;
4911 error = newnfs_request(nd, nmp, NULL, nrp, NULL, p, cred, NFS_PROG,
4912 NFS_VER4, NULL, 1, NULL, NULL);
4915 if (nd->nd_repstat == 0) {
4916 NFSM_DISSECT(tl, uint32_t *, NFSX_V4SESSIONID +
4918 bcopy(tl, sep->nfsess_sessionid, NFSX_V4SESSIONID);
4919 tl += NFSX_V4SESSIONID / NFSX_UNSIGNED;
4920 sep->nfsess_sequenceid = fxdr_unsigned(uint32_t, *tl++);
4921 crflags = fxdr_unsigned(uint32_t, *tl);
4922 if ((crflags & NFSV4CRSESS_PERSIST) != 0 && mds != 0) {
4924 nmp->nm_state |= NFSSTA_SESSPERSIST;
4928 /* Get the fore channel slot count. */
4929 NFSM_DISSECT(tl, uint32_t *, 7 * NFSX_UNSIGNED);
4930 tl++; /* Skip the header pad size. */
4932 /* Make sure nm_wsize is small enough. */
4933 maxval = fxdr_unsigned(uint32_t, *tl++);
4934 while (maxval < nmp->nm_wsize + NFS_MAXXDR) {
4935 if (nmp->nm_wsize > 8096)
4940 sep->nfsess_maxreq = maxval;
4942 /* Make sure nm_rsize is small enough. */
4943 maxval = fxdr_unsigned(uint32_t, *tl++);
4944 while (maxval < nmp->nm_rsize + NFS_MAXXDR) {
4945 if (nmp->nm_rsize > 8096)
4950 sep->nfsess_maxresp = maxval;
4952 sep->nfsess_maxcache = fxdr_unsigned(int, *tl++);
4954 sep->nfsess_foreslots = fxdr_unsigned(uint16_t, *tl++);
4955 NFSCL_DEBUG(4, "fore slots=%d\n", (int)sep->nfsess_foreslots);
4956 irdcnt = fxdr_unsigned(int, *tl);
4958 NFSM_DISSECT(tl, uint32_t *, irdcnt * NFSX_UNSIGNED);
4960 /* and the back channel slot count. */
4961 NFSM_DISSECT(tl, uint32_t *, 7 * NFSX_UNSIGNED);
4963 sep->nfsess_backslots = fxdr_unsigned(uint16_t, *tl);
4964 NFSCL_DEBUG(4, "back slots=%d\n", (int)sep->nfsess_backslots);
4966 error = nd->nd_repstat;
4968 m_freem(nd->nd_mrep);
4973 * Do the NFSv4.1 Destroy Session.
4976 nfsrpc_destroysession(struct nfsmount *nmp, struct nfsclclient *clp,
4977 struct ucred *cred, NFSPROC_T *p)
4980 struct nfsrv_descript nfsd;
4981 struct nfsrv_descript *nd = &nfsd;
4983 struct nfsclsession *tsep;
4985 nfscl_reqstart(nd, NFSPROC_DESTROYSESSION, nmp, NULL, 0, NULL, NULL, 0,
4987 NFSM_BUILD(tl, uint32_t *, NFSX_V4SESSIONID);
4988 tsep = nfsmnt_mdssession(nmp);
4989 bcopy(tsep->nfsess_sessionid, tl, NFSX_V4SESSIONID);
4990 nd->nd_flag |= ND_USEGSSNAME;
4991 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4992 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4995 error = nd->nd_repstat;
4996 m_freem(nd->nd_mrep);
5001 * Do the NFSv4.1 Destroy Client.
5004 nfsrpc_destroyclient(struct nfsmount *nmp, struct nfsclclient *clp,
5005 struct ucred *cred, NFSPROC_T *p)
5008 struct nfsrv_descript nfsd;
5009 struct nfsrv_descript *nd = &nfsd;
5011 struct nfsclsession *tsep;
5013 nfscl_reqstart(nd, NFSPROC_DESTROYCLIENT, nmp, NULL, 0, NULL, NULL, 0,
5015 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_UNSIGNED);
5016 tsep = nfsmnt_mdssession(nmp);
5017 *tl++ = tsep->nfsess_clientid.lval[0];
5018 *tl = tsep->nfsess_clientid.lval[1];
5019 nd->nd_flag |= ND_USEGSSNAME;
5020 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
5021 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
5024 error = nd->nd_repstat;
5025 m_freem(nd->nd_mrep);
5030 * Do the NFSv4.1 LayoutGet.
5033 nfsrpc_layoutget(struct nfsmount *nmp, uint8_t *fhp, int fhlen, int iomode,
5034 uint64_t offset, uint64_t len, uint64_t minlen, int layouttype,
5035 int layoutlen, nfsv4stateid_t *stateidp, int *retonclosep,
5036 struct nfsclflayouthead *flhp, struct ucred *cred, NFSPROC_T *p,
5039 struct nfsrv_descript nfsd, *nd = &nfsd;
5042 nfscl_reqstart(nd, NFSPROC_LAYOUTGET, nmp, fhp, fhlen, NULL, NULL, 0,
5044 nfsrv_setuplayoutget(nd, iomode, offset, len, minlen, stateidp,
5045 layouttype, layoutlen, 0);
5046 nd->nd_flag |= ND_USEGSSNAME;
5047 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
5048 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
5049 NFSCL_DEBUG(4, "layget err=%d st=%d\n", error, nd->nd_repstat);
5052 if (nd->nd_repstat == 0)
5053 error = nfsrv_parselayoutget(nmp, nd, stateidp, retonclosep,
5055 if (error == 0 && nd->nd_repstat != 0)
5056 error = nd->nd_repstat;
5057 m_freem(nd->nd_mrep);
5062 * Do the NFSv4.1 Get Device Info.
5065 nfsrpc_getdeviceinfo(struct nfsmount *nmp, uint8_t *deviceid, int layouttype,
5066 uint32_t *notifybitsp, struct nfscldevinfo **ndip, struct ucred *cred,
5069 uint32_t cnt, *tl, vers, minorvers;
5070 struct nfsrv_descript nfsd;
5071 struct nfsrv_descript *nd = &nfsd;
5072 struct sockaddr_in sin, ssin;
5073 struct sockaddr_in6 sin6, ssin6;
5074 struct nfsclds *dsp = NULL, **dspp, **gotdspp;
5075 struct nfscldevinfo *ndi;
5076 int addrcnt = 0, bitcnt, error, gotminor, gotvers, i, isudp, j;
5078 uint8_t stripeindex;
5079 sa_family_t af, safilled;
5081 ssin.sin_port = 0; /* To shut up compiler. */
5082 ssin.sin_addr.s_addr = 0; /* ditto */
5086 nfscl_reqstart(nd, NFSPROC_GETDEVICEINFO, nmp, NULL, 0, NULL, NULL, 0,
5088 NFSM_BUILD(tl, uint32_t *, NFSX_V4DEVICEID + 3 * NFSX_UNSIGNED);
5089 NFSBCOPY(deviceid, tl, NFSX_V4DEVICEID);
5090 tl += (NFSX_V4DEVICEID / NFSX_UNSIGNED);
5091 *tl++ = txdr_unsigned(layouttype);
5092 *tl++ = txdr_unsigned(100000);
5093 if (notifybitsp != NULL && *notifybitsp != 0) {
5094 *tl = txdr_unsigned(1); /* One word of bits. */
5095 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
5096 *tl = txdr_unsigned(*notifybitsp);
5098 *tl = txdr_unsigned(0);
5099 nd->nd_flag |= ND_USEGSSNAME;
5100 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
5101 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
5104 if (nd->nd_repstat == 0) {
5105 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
5106 if (layouttype != fxdr_unsigned(int, *tl))
5107 printf("EEK! devinfo layout type not same!\n");
5108 if (layouttype == NFSLAYOUT_NFSV4_1_FILES) {
5109 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
5110 stripecnt = fxdr_unsigned(int, *tl);
5111 NFSCL_DEBUG(4, "stripecnt=%d\n", stripecnt);
5112 if (stripecnt < 1 || stripecnt > 4096) {
5113 printf("pNFS File layout devinfo stripecnt %d:"
5114 " out of range\n", stripecnt);
5115 error = NFSERR_BADXDR;
5118 NFSM_DISSECT(tl, uint32_t *, (stripecnt + 1) *
5120 addrcnt = fxdr_unsigned(int, *(tl + stripecnt));
5121 NFSCL_DEBUG(4, "addrcnt=%d\n", addrcnt);
5122 if (addrcnt < 1 || addrcnt > 128) {
5123 printf("NFS devinfo addrcnt %d: out of range\n",
5125 error = NFSERR_BADXDR;
5130 * Now we know how many stripe indices and addresses, so
5131 * we can allocate the structure the correct size.
5133 i = (stripecnt * sizeof(uint8_t)) /
5134 sizeof(struct nfsclds *) + 1;
5135 NFSCL_DEBUG(4, "stripeindices=%d\n", i);
5136 ndi = malloc(sizeof(*ndi) + (addrcnt + i) *
5137 sizeof(struct nfsclds *), M_NFSDEVINFO, M_WAITOK |
5139 NFSBCOPY(deviceid, ndi->nfsdi_deviceid,
5141 ndi->nfsdi_refcnt = 0;
5142 ndi->nfsdi_flags = NFSDI_FILELAYOUT;
5143 ndi->nfsdi_stripecnt = stripecnt;
5144 ndi->nfsdi_addrcnt = addrcnt;
5145 /* Fill in the stripe indices. */
5146 for (i = 0; i < stripecnt; i++) {
5147 stripeindex = fxdr_unsigned(uint8_t, *tl++);
5148 NFSCL_DEBUG(4, "stripeind=%d\n", stripeindex);
5149 if (stripeindex >= addrcnt) {
5150 printf("pNFS File Layout devinfo"
5151 " stripeindex %d: too big\n",
5153 error = NFSERR_BADXDR;
5156 nfsfldi_setstripeindex(ndi, i, stripeindex);
5158 } else if (layouttype == NFSLAYOUT_FLEXFILE) {
5159 /* For Flex File, we only get one address list. */
5160 ndi = malloc(sizeof(*ndi) + sizeof(struct nfsclds *),
5161 M_NFSDEVINFO, M_WAITOK | M_ZERO);
5162 NFSBCOPY(deviceid, ndi->nfsdi_deviceid,
5164 ndi->nfsdi_refcnt = 0;
5165 ndi->nfsdi_flags = NFSDI_FLEXFILE;
5166 addrcnt = ndi->nfsdi_addrcnt = 1;
5169 /* Now, dissect the server address(es). */
5170 safilled = AF_UNSPEC;
5171 for (i = 0; i < addrcnt; i++) {
5172 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
5173 cnt = fxdr_unsigned(uint32_t, *tl);
5175 printf("NFS devinfo 0 len addrlist\n");
5176 error = NFSERR_BADXDR;
5179 dspp = nfsfldi_addr(ndi, i);
5180 safilled = AF_UNSPEC;
5181 for (j = 0; j < cnt; j++) {
5182 error = nfsv4_getipaddr(nd, &sin, &sin6, &af,
5184 if (error != 0 && error != EPERM) {
5185 error = NFSERR_BADXDR;
5188 if (error == 0 && isudp == 0) {
5191 * - Same address family.
5192 * Save the address and dspp, so that
5193 * the connection can be done after
5194 * parsing is complete.
5196 if (safilled == AF_UNSPEC ||
5197 (af == nmp->nm_nam->sa_family &&
5198 safilled != nmp->nm_nam->sa_family)
5211 gotvers = NFS_VER4; /* Default NFSv4.1 for File Layout. */
5212 gotminor = NFSV41_MINORVERSION;
5213 /* For Flex File, we will take one of the versions to use. */
5214 if (layouttype == NFSLAYOUT_FLEXFILE) {
5215 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
5216 j = fxdr_unsigned(int, *tl);
5217 if (j < 1 || j > NFSDEV_MAXVERS) {
5218 printf("pNFS: too many versions\n");
5219 error = NFSERR_BADXDR;
5224 for (i = 0; i < j; i++) {
5225 NFSM_DISSECT(tl, uint32_t *, 5 * NFSX_UNSIGNED);
5226 vers = fxdr_unsigned(uint32_t, *tl++);
5227 minorvers = fxdr_unsigned(uint32_t, *tl++);
5228 if (vers == NFS_VER3)
5230 if ((vers == NFS_VER4 && ((minorvers ==
5231 NFSV41_MINORVERSION && gotminor == 0) ||
5232 minorvers == NFSV42_MINORVERSION)) ||
5233 (vers == NFS_VER3 && gotvers == 0)) {
5235 gotminor = minorvers;
5236 /* We'll take this one. */
5237 ndi->nfsdi_versindex = i;
5238 ndi->nfsdi_vers = vers;
5239 ndi->nfsdi_minorvers = minorvers;
5240 ndi->nfsdi_rsize = fxdr_unsigned(
5242 ndi->nfsdi_wsize = fxdr_unsigned(
5244 if (*tl == newnfs_true)
5249 ~NFSDI_TIGHTCOUPLED;
5253 printf("pNFS: no NFSv3, NFSv4.1 or NFSv4.2\n");
5254 error = NFSERR_BADXDR;
5259 /* And the notify bits. */
5260 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
5261 bitcnt = fxdr_unsigned(int, *tl);
5263 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
5264 if (notifybitsp != NULL)
5266 fxdr_unsigned(uint32_t, *tl);
5268 if (safilled != AF_UNSPEC) {
5269 KASSERT(ndi != NULL, ("ndi is NULL"));
5275 * Now we can do a TCP connection for the correct
5276 * NFS version and IP address.
5278 error = nfsrpc_fillsa(nmp, &ssin, &ssin6, safilled,
5279 gotvers, gotminor, &dsp, p);
5282 KASSERT(gotdspp != NULL, ("gotdspp is NULL"));
5286 if (nd->nd_repstat != 0 && error == 0)
5287 error = nd->nd_repstat;
5289 if (error != 0 && ndi != NULL)
5290 nfscl_freedevinfo(ndi);
5291 m_freem(nd->nd_mrep);
5296 * Do the NFSv4.1 LayoutCommit.
5299 nfsrpc_layoutcommit(struct nfsmount *nmp, uint8_t *fh, int fhlen, int reclaim,
5300 uint64_t off, uint64_t len, uint64_t lastbyte, nfsv4stateid_t *stateidp,
5301 int layouttype, struct ucred *cred, NFSPROC_T *p, void *stuff)
5304 struct nfsrv_descript nfsd, *nd = &nfsd;
5307 nfscl_reqstart(nd, NFSPROC_LAYOUTCOMMIT, nmp, fh, fhlen, NULL, NULL,
5309 NFSM_BUILD(tl, uint32_t *, 5 * NFSX_UNSIGNED + 3 * NFSX_HYPER +
5311 txdr_hyper(off, tl);
5313 txdr_hyper(len, tl);
5316 *tl++ = newnfs_true;
5318 *tl++ = newnfs_false;
5319 *tl++ = txdr_unsigned(stateidp->seqid);
5320 *tl++ = stateidp->other[0];
5321 *tl++ = stateidp->other[1];
5322 *tl++ = stateidp->other[2];
5323 *tl++ = newnfs_true;
5326 else if (lastbyte >= (off + len))
5327 lastbyte = off + len - 1;
5328 txdr_hyper(lastbyte, tl);
5330 *tl++ = newnfs_false;
5331 *tl++ = txdr_unsigned(layouttype);
5332 /* All supported layouts are 0 length. */
5333 *tl = txdr_unsigned(0);
5334 nd->nd_flag |= ND_USEGSSNAME;
5335 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
5336 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
5339 error = nd->nd_repstat;
5340 m_freem(nd->nd_mrep);
5345 * Do the NFSv4.1 LayoutReturn.
5348 nfsrpc_layoutreturn(struct nfsmount *nmp, uint8_t *fh, int fhlen, int reclaim,
5349 int layouttype, uint32_t iomode, int layoutreturn, uint64_t offset,
5350 uint64_t len, nfsv4stateid_t *stateidp, struct ucred *cred, NFSPROC_T *p,
5351 uint32_t stat, uint32_t op, char *devid)
5354 struct nfsrv_descript nfsd, *nd = &nfsd;
5358 nfscl_reqstart(nd, NFSPROC_LAYOUTRETURN, nmp, fh, fhlen, NULL, NULL,
5360 NFSM_BUILD(tl, uint32_t *, 4 * NFSX_UNSIGNED);
5362 *tl++ = newnfs_true;
5364 *tl++ = newnfs_false;
5365 *tl++ = txdr_unsigned(layouttype);
5366 *tl++ = txdr_unsigned(iomode);
5367 *tl = txdr_unsigned(layoutreturn);
5368 if (layoutreturn == NFSLAYOUTRETURN_FILE) {
5369 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_HYPER + NFSX_STATEID +
5371 txdr_hyper(offset, tl);
5373 txdr_hyper(len, tl);
5375 NFSCL_DEBUG(4, "layoutret stseq=%d\n", (int)stateidp->seqid);
5376 *tl++ = txdr_unsigned(stateidp->seqid);
5377 *tl++ = stateidp->other[0];
5378 *tl++ = stateidp->other[1];
5379 *tl++ = stateidp->other[2];
5380 if (layouttype == NFSLAYOUT_NFSV4_1_FILES)
5381 *tl = txdr_unsigned(0);
5382 else if (layouttype == NFSLAYOUT_FLEXFILE) {
5384 *tl = txdr_unsigned(2 * NFSX_HYPER +
5385 NFSX_STATEID + NFSX_V4DEVICEID + 5 *
5387 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_HYPER +
5388 NFSX_STATEID + NFSX_V4DEVICEID + 5 *
5390 *tl++ = txdr_unsigned(1); /* One error. */
5391 tu64 = 0; /* Offset. */
5392 txdr_hyper(tu64, tl); tl += 2;
5393 tu64 = UINT64_MAX; /* Length. */
5394 txdr_hyper(tu64, tl); tl += 2;
5395 NFSBCOPY(stateidp, tl, NFSX_STATEID);
5396 tl += (NFSX_STATEID / NFSX_UNSIGNED);
5397 *tl++ = txdr_unsigned(1); /* One error. */
5398 NFSBCOPY(devid, tl, NFSX_V4DEVICEID);
5399 tl += (NFSX_V4DEVICEID / NFSX_UNSIGNED);
5400 *tl++ = txdr_unsigned(stat);
5401 *tl++ = txdr_unsigned(op);
5403 *tl = txdr_unsigned(2 * NFSX_UNSIGNED);
5404 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_UNSIGNED);
5408 *tl = 0; /* No stats yet. */
5411 nd->nd_flag |= ND_USEGSSNAME;
5412 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
5413 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
5416 if (nd->nd_repstat == 0) {
5417 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
5419 NFSM_DISSECT(tl, uint32_t *, NFSX_STATEID);
5420 stateidp->seqid = fxdr_unsigned(uint32_t, *tl++);
5421 stateidp->other[0] = *tl++;
5422 stateidp->other[1] = *tl++;
5423 stateidp->other[2] = *tl;
5426 error = nd->nd_repstat;
5428 m_freem(nd->nd_mrep);
5433 * Acquire a layout and devinfo, if possible. The caller must have acquired
5434 * a reference count on the nfsclclient structure before calling this.
5435 * Return the layout in lypp with a reference count on it, if successful.
5438 nfsrpc_getlayout(struct nfsmount *nmp, vnode_t vp, struct nfsfh *nfhp,
5439 int iomode, uint32_t *notifybitsp, nfsv4stateid_t *stateidp, uint64_t off,
5440 struct nfscllayout **lypp, struct ucred *cred, NFSPROC_T *p)
5442 struct nfscllayout *lyp;
5443 struct nfsclflayout *flp;
5444 struct nfsclflayouthead flh;
5445 int error = 0, islocked, layoutlen, layouttype, recalled, retonclose;
5446 nfsv4stateid_t stateid;
5447 struct nfsclsession *tsep;
5450 if (NFSHASFLEXFILE(nmp))
5451 layouttype = NFSLAYOUT_FLEXFILE;
5453 layouttype = NFSLAYOUT_NFSV4_1_FILES;
5455 * If lyp is returned non-NULL, there will be a refcnt (shared lock)
5456 * on it, iff flp != NULL or a lock (exclusive lock) on it iff
5459 lyp = nfscl_getlayout(nmp->nm_clp, nfhp->nfh_fh, nfhp->nfh_len,
5460 off, &flp, &recalled);
5462 if (lyp == NULL || flp == NULL) {
5466 tsep = nfsmnt_mdssession(nmp);
5467 layoutlen = tsep->nfsess_maxcache -
5468 (NFSX_STATEID + 3 * NFSX_UNSIGNED);
5471 stateid.other[0] = stateidp->other[0];
5472 stateid.other[1] = stateidp->other[1];
5473 stateid.other[2] = stateidp->other[2];
5474 error = nfsrpc_layoutget(nmp, nfhp->nfh_fh,
5475 nfhp->nfh_len, iomode, (uint64_t)0, UINT64_MAX,
5476 (uint64_t)0, layouttype, layoutlen, &stateid,
5477 &retonclose, &flh, cred, p, NULL);
5480 stateid.seqid = lyp->nfsly_stateid.seqid;
5481 stateid.other[0] = lyp->nfsly_stateid.other[0];
5482 stateid.other[1] = lyp->nfsly_stateid.other[1];
5483 stateid.other[2] = lyp->nfsly_stateid.other[2];
5484 error = nfsrpc_layoutget(nmp, nfhp->nfh_fh,
5485 nfhp->nfh_len, iomode, off, UINT64_MAX,
5486 (uint64_t)0, layouttype, layoutlen, &stateid,
5487 &retonclose, &flh, cred, p, NULL);
5489 error = nfsrpc_layoutgetres(nmp, vp, nfhp->nfh_fh,
5490 nfhp->nfh_len, &stateid, retonclose, notifybitsp, &lyp,
5491 &flh, layouttype, error, NULL, cred, p);
5494 else if (islocked != 0)
5495 nfscl_rellayout(lyp, 1);
5502 * Do a TCP connection plus exchange id and create session.
5503 * If successful, a "struct nfsclds" is linked into the list for the
5504 * mount point and a pointer to it is returned.
5507 nfsrpc_fillsa(struct nfsmount *nmp, struct sockaddr_in *sin,
5508 struct sockaddr_in6 *sin6, sa_family_t af, int vers, int minorvers,
5509 struct nfsclds **dspp, NFSPROC_T *p)
5511 struct sockaddr_in *msad, *sad;
5512 struct sockaddr_in6 *msad6, *sad6;
5513 struct nfsclclient *clp;
5514 struct nfssockreq *nrp;
5515 struct nfsclds *dsp, *tdsp;
5516 int error, firsttry;
5517 enum nfsclds_state retv;
5518 uint32_t sequenceid = 0;
5520 KASSERT(nmp->nm_sockreq.nr_cred != NULL,
5521 ("nfsrpc_fillsa: NULL nr_cred"));
5527 if (af == AF_INET) {
5530 * Check to see if we already have a session for this
5531 * address that is usable for a DS.
5532 * Note that the MDS's address is in a different place
5533 * than the sessions already acquired for DS's.
5535 msad = (struct sockaddr_in *)nmp->nm_sockreq.nr_nam;
5536 tdsp = TAILQ_FIRST(&nmp->nm_sess);
5537 while (tdsp != NULL) {
5538 if (msad != NULL && msad->sin_family == AF_INET &&
5539 sin->sin_addr.s_addr == msad->sin_addr.s_addr &&
5540 sin->sin_port == msad->sin_port &&
5541 (tdsp->nfsclds_flags & NFSCLDS_DS) != 0 &&
5542 tdsp->nfsclds_sess.nfsess_defunct == 0) {
5545 NFSCL_DEBUG(4, "fnd same addr\n");
5548 tdsp = TAILQ_NEXT(tdsp, nfsclds_list);
5549 if (tdsp != NULL && tdsp->nfsclds_sockp != NULL)
5550 msad = (struct sockaddr_in *)
5551 tdsp->nfsclds_sockp->nr_nam;
5557 /* No IP address match, so look for new/trunked one. */
5558 sad = malloc(sizeof(*sad), M_SONAME, M_WAITOK | M_ZERO);
5559 sad->sin_len = sizeof(*sad);
5560 sad->sin_family = AF_INET;
5561 sad->sin_port = sin->sin_port;
5562 sad->sin_addr.s_addr = sin->sin_addr.s_addr;
5563 nrp = malloc(sizeof(*nrp), M_NFSSOCKREQ, M_WAITOK | M_ZERO);
5564 nrp->nr_nam = (struct sockaddr *)sad;
5565 } else if (af == AF_INET6) {
5568 * Check to see if we already have a session for this
5569 * address that is usable for a DS.
5570 * Note that the MDS's address is in a different place
5571 * than the sessions already acquired for DS's.
5573 msad6 = (struct sockaddr_in6 *)nmp->nm_sockreq.nr_nam;
5574 tdsp = TAILQ_FIRST(&nmp->nm_sess);
5575 while (tdsp != NULL) {
5576 if (msad6 != NULL && msad6->sin6_family == AF_INET6 &&
5577 IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr,
5578 &msad6->sin6_addr) &&
5579 sin6->sin6_port == msad6->sin6_port &&
5580 (tdsp->nfsclds_flags & NFSCLDS_DS) != 0 &&
5581 tdsp->nfsclds_sess.nfsess_defunct == 0) {
5586 tdsp = TAILQ_NEXT(tdsp, nfsclds_list);
5587 if (tdsp != NULL && tdsp->nfsclds_sockp != NULL)
5588 msad6 = (struct sockaddr_in6 *)
5589 tdsp->nfsclds_sockp->nr_nam;
5595 /* No IP address match, so look for new/trunked one. */
5596 sad6 = malloc(sizeof(*sad6), M_SONAME, M_WAITOK | M_ZERO);
5597 sad6->sin6_len = sizeof(*sad6);
5598 sad6->sin6_family = AF_INET6;
5599 sad6->sin6_port = sin6->sin6_port;
5600 NFSBCOPY(&sin6->sin6_addr, &sad6->sin6_addr,
5601 sizeof(struct in6_addr));
5602 nrp = malloc(sizeof(*nrp), M_NFSSOCKREQ, M_WAITOK | M_ZERO);
5603 nrp->nr_nam = (struct sockaddr *)sad6;
5607 nrp->nr_sotype = SOCK_STREAM;
5608 mtx_init(&nrp->nr_mtx, "nfssock", NULL, MTX_DEF);
5609 nrp->nr_prog = NFS_PROG;
5610 nrp->nr_vers = vers;
5613 * Use the credentials that were used for the mount, which are
5614 * in nmp->nm_sockreq.nr_cred for newnfs_connect() etc.
5615 * Ref. counting the credentials with crhold() is probably not
5616 * necessary, since nm_sockreq.nr_cred won't be crfree()'d until
5617 * unmount, but I did it anyhow.
5619 nrp->nr_cred = crhold(nmp->nm_sockreq.nr_cred);
5620 error = newnfs_connect(nmp, nrp, NULL, p, 0);
5621 NFSCL_DEBUG(3, "DS connect=%d\n", error);
5624 /* Now, do the exchangeid and create session. */
5626 if (vers == NFS_VER4) {
5629 error = nfsrpc_exchangeid(nmp, clp, nrp,
5630 minorvers, NFSV4EXCH_USEPNFSDS, &dsp,
5632 NFSCL_DEBUG(3, "DS exchangeid=%d\n", error);
5633 if (error == NFSERR_MINORVERMISMATCH)
5634 minorvers = NFSV42_MINORVERSION;
5635 } while (error == NFSERR_MINORVERMISMATCH &&
5638 newnfs_disconnect(nrp);
5640 dsp = malloc(sizeof(struct nfsclds), M_NFSCLDS,
5642 dsp->nfsclds_flags |= NFSCLDS_DS;
5643 dsp->nfsclds_expire = INT32_MAX; /* No renews needed. */
5644 mtx_init(&dsp->nfsclds_mtx, "nfsds", NULL, MTX_DEF);
5645 mtx_init(&dsp->nfsclds_sess.nfsess_mtx, "nfssession",
5650 dsp->nfsclds_sockp = nrp;
5651 if (vers == NFS_VER4) {
5653 retv = nfscl_getsameserver(nmp, dsp, &tdsp,
5655 NFSCL_DEBUG(3, "getsame ret=%d\n", retv);
5656 if (retv == NFSDSP_USETHISSESSION &&
5657 nfscl_dssameconn != 0) {
5659 tdsp->nfsclds_flags |= NFSCLDS_SAMECONN;
5663 * If there is already a session for this
5666 (void)newnfs_disconnect(nrp);
5667 nfscl_freenfsclds(dsp);
5671 if (retv == NFSDSP_NOTFOUND)
5673 dsp->nfsclds_sess.nfsess_sequenceid;
5675 error = nfsrpc_createsession(nmp, &dsp->nfsclds_sess,
5676 nrp, dsp, sequenceid, 0, nrp->nr_cred, p);
5677 NFSCL_DEBUG(3, "DS createsess=%d\n", error);
5680 NFSFREECRED(nrp->nr_cred);
5681 NFSFREEMUTEX(&nrp->nr_mtx);
5682 free(nrp->nr_nam, M_SONAME);
5683 free(nrp, M_NFSSOCKREQ);
5686 NFSCL_DEBUG(3, "add DS session\n");
5688 * Put it at the end of the list. That way the list
5689 * is ordered by when the entry was added. This matters
5690 * since the one done first is the one that should be
5691 * used for sequencid'ing any subsequent create sessions.
5694 TAILQ_INSERT_TAIL(&nmp->nm_sess, dsp, nfsclds_list);
5697 } else if (dsp != NULL) {
5698 newnfs_disconnect(nrp);
5699 nfscl_freenfsclds(dsp);
5705 * Do the NFSv4.1 Reclaim Complete.
5708 nfsrpc_reclaimcomplete(struct nfsmount *nmp, struct ucred *cred, NFSPROC_T *p)
5711 struct nfsrv_descript nfsd;
5712 struct nfsrv_descript *nd = &nfsd;
5715 nfscl_reqstart(nd, NFSPROC_RECLAIMCOMPL, nmp, NULL, 0, NULL, NULL, 0,
5717 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
5719 nd->nd_flag |= ND_USEGSSNAME;
5720 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
5721 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
5724 error = nd->nd_repstat;
5725 m_freem(nd->nd_mrep);
5730 * Initialize the slot tables for a session.
5733 nfscl_initsessionslots(struct nfsclsession *sep)
5737 for (i = 0; i < NFSV4_CBSLOTS; i++) {
5738 if (sep->nfsess_cbslots[i].nfssl_reply != NULL)
5739 m_freem(sep->nfsess_cbslots[i].nfssl_reply);
5740 NFSBZERO(&sep->nfsess_cbslots[i], sizeof(struct nfsslot));
5742 for (i = 0; i < 64; i++)
5743 sep->nfsess_slotseq[i] = 0;
5744 sep->nfsess_slots = 0;
5748 * Called to try and do an I/O operation via an NFSv4.1 Data Server (DS).
5751 nfscl_doiods(vnode_t vp, struct uio *uiop, int *iomode, int *must_commit,
5752 uint32_t rwaccess, int docommit, struct ucred *cred, NFSPROC_T *p)
5754 struct nfsnode *np = VTONFS(vp);
5755 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
5756 struct nfscllayout *layp;
5757 struct nfscldevinfo *dip;
5758 struct nfsclflayout *rflp;
5760 struct nfsclwritedsdorpc *drpc, *tdrpc;
5761 nfsv4stateid_t stateid;
5762 struct ucred *newcred;
5763 uint64_t lastbyte, len, off, oresid, xfer;
5764 int eof, error, firstmirror, i, iolaymode, mirrorcnt, recalled, timo;
5767 void *iovbase = NULL;
5772 if (!NFSHASPNFS(nmp) || nfscl_enablecallb == 0 || nfs_numnfscbd == 0 ||
5773 (np->n_flag & NNOLAYOUT) != 0)
5775 /* Now, get a reference cnt on the clientid for this mount. */
5776 if (nfscl_getref(nmp) == 0)
5779 /* Find an appropriate stateid. */
5780 newcred = NFSNEWCRED(cred);
5781 error = nfscl_getstateid(vp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len,
5782 rwaccess, 1, newcred, p, &stateid, &lckp);
5784 NFSFREECRED(newcred);
5788 /* Search for a layout for this file. */
5789 off = uiop->uio_offset;
5790 layp = nfscl_getlayout(nmp->nm_clp, np->n_fhp->nfh_fh,
5791 np->n_fhp->nfh_len, off, &rflp, &recalled);
5792 if (layp == NULL || rflp == NULL) {
5793 if (recalled != 0) {
5794 NFSFREECRED(newcred);
5799 nfscl_rellayout(layp, (rflp == NULL) ? 1 : 0);
5802 /* Try and get a Layout, if it is supported. */
5803 if (rwaccess == NFSV4OPEN_ACCESSWRITE ||
5804 (np->n_flag & NWRITEOPENED) != 0)
5805 iolaymode = NFSLAYOUTIOMODE_RW;
5807 iolaymode = NFSLAYOUTIOMODE_READ;
5808 error = nfsrpc_getlayout(nmp, vp, np->n_fhp, iolaymode,
5809 NULL, &stateid, off, &layp, newcred, p);
5812 np->n_flag |= NNOLAYOUT;
5815 nfscl_lockderef(lckp);
5816 NFSFREECRED(newcred);
5818 nfscl_rellayout(layp, 0);
5825 * Loop around finding a layout that works for the first part of
5826 * this I/O operation, and then call the function that actually
5830 len = (uint64_t)uiop->uio_resid;
5831 while (len > 0 && error == 0 && eof == 0) {
5832 off = uiop->uio_offset;
5833 error = nfscl_findlayoutforio(layp, off, rwaccess, &rflp);
5835 oresid = xfer = (uint64_t)uiop->uio_resid;
5836 if (xfer > (rflp->nfsfl_end - rflp->nfsfl_off))
5837 xfer = rflp->nfsfl_end - rflp->nfsfl_off;
5839 * For Flex File layout with mirrored DSs, select one
5840 * of them at random for reads. For writes and commits,
5844 tdrpc = drpc = NULL;
5847 if ((layp->nfsly_flags & NFSLY_FLEXFILE) != 0 &&
5848 (mirrorcnt = rflp->nfsfl_mirrorcnt) > 1) {
5849 if (rwaccess == NFSV4OPEN_ACCESSREAD) {
5850 firstmirror = arc4random() % mirrorcnt;
5851 mirrorcnt = firstmirror + 1;
5853 if (docommit == 0) {
5855 * Save values, so uiop can be
5856 * rolled back upon a write
5859 offs = uiop->uio_offset;
5860 resid = uiop->uio_resid;
5862 uiop->uio_iov->iov_base;
5863 iovlen = uiop->uio_iov->iov_len;
5864 m = nfsm_uiombuflist(uiop, len,
5867 tdrpc = drpc = malloc(sizeof(*drpc) *
5868 (mirrorcnt - 1), M_TEMP, M_WAITOK |
5872 for (i = firstmirror; i < mirrorcnt && error == 0; i++){
5873 if ((layp->nfsly_flags & NFSLY_FLEXFILE) != 0) {
5874 dev = rflp->nfsfl_ffm[i].dev;
5875 dip = nfscl_getdevinfo(nmp->nm_clp, dev,
5876 rflp->nfsfl_ffm[i].devp);
5878 dev = rflp->nfsfl_dev;
5879 dip = nfscl_getdevinfo(nmp->nm_clp, dev,
5883 if ((rflp->nfsfl_flags & NFSFL_FLEXFILE)
5885 error = nfscl_dofflayoutio(vp,
5886 uiop, iomode, must_commit,
5887 &eof, &stateid, rwaccess,
5888 dip, layp, rflp, off, xfer,
5889 i, docommit, m, tdrpc,
5892 error = nfscl_doflayoutio(vp,
5893 uiop, iomode, must_commit,
5894 &eof, &stateid, rwaccess,
5895 dip, layp, rflp, off, xfer,
5896 docommit, newcred, p);
5897 nfscl_reldevinfo(dip);
5905 timo = hz / 50; /* Wait for 20msec. */
5908 for (i = firstmirror; i < mirrorcnt - 1 &&
5909 tdrpc != NULL; i++, tdrpc++) {
5911 * For the unused drpc entries, both inprog and
5912 * err == 0, so this loop won't break.
5914 while (tdrpc->inprog != 0 && tdrpc->done == 0)
5915 tsleep(&tdrpc->tsk, PVFS, "clrpcio",
5917 if (error == 0 && tdrpc->err != 0)
5922 if (mirrorcnt > 1 && rwaccess ==
5923 NFSV4OPEN_ACCESSWRITE && docommit == 0) {
5925 layp->nfsly_flags |= NFSLY_WRITTEN;
5928 lastbyte = off + xfer - 1;
5930 if (lastbyte > layp->nfsly_lastbyte)
5931 layp->nfsly_lastbyte = lastbyte;
5933 } else if (error == NFSERR_OPENMODE &&
5934 rwaccess == NFSV4OPEN_ACCESSREAD) {
5936 nmp->nm_state |= NFSSTA_OPENMODE;
5941 len -= (oresid - (uint64_t)uiop->uio_resid);
5942 else if (mirrorcnt > 1 && rwaccess ==
5943 NFSV4OPEN_ACCESSWRITE && docommit == 0) {
5945 * In case the rpc gets retried, roll the
5946 * uio fields changed by nfsm_uiombuflist()
5949 uiop->uio_offset = offs;
5950 uiop->uio_resid = resid;
5951 uiop->uio_iov->iov_base = iovbase;
5952 uiop->uio_iov->iov_len = iovlen;
5957 nfscl_lockderef(lckp);
5958 NFSFREECRED(newcred);
5959 nfscl_rellayout(layp, 0);
5965 * Make a copy of the mbuf chain and add an mbuf for null padding, as required.
5967 static struct mbuf *
5968 nfsm_copym(struct mbuf *m, int off, int xfer)
5970 struct mbuf *m2, *m3, *m4;
5974 m2 = m_copym(m, off, xfer, M_WAITOK);
5975 rem = NFSM_RNDUP(xfer) - xfer;
5978 * The zero padding to a multiple of 4 bytes is required by
5979 * the XDR. So that the mbufs copied by reference aren't
5980 * modified, add an mbuf with the zero'd bytes to the list.
5981 * rem will be a maximum of 3, so one zero'd uint32_t is
5985 while (m3->m_next != NULL)
5988 tl = mtod(m4, uint32_t *);
5997 * Find a file layout that will handle the first bytes of the requested
5998 * range and return the information from it needed to the I/O operation.
6001 nfscl_findlayoutforio(struct nfscllayout *lyp, uint64_t off, uint32_t rwaccess,
6002 struct nfsclflayout **retflpp)
6004 struct nfsclflayout *flp, *nflp, *rflp;
6009 /* For reading, do the Read list first and then the Write list. */
6011 if (rw == NFSV4OPEN_ACCESSREAD)
6012 flp = LIST_FIRST(&lyp->nfsly_flayread);
6014 flp = LIST_FIRST(&lyp->nfsly_flayrw);
6015 while (flp != NULL) {
6016 nflp = LIST_NEXT(flp, nfsfl_list);
6017 if (flp->nfsfl_off > off)
6019 if (flp->nfsfl_end > off &&
6020 (rflp == NULL || rflp->nfsfl_end < flp->nfsfl_end))
6024 if (rw == NFSV4OPEN_ACCESSREAD)
6025 rw = NFSV4OPEN_ACCESSWRITE;
6030 /* This one covers the most bytes starting at off. */
6038 * Do I/O using an NFSv4.1 or NFSv4.2 file layout.
6041 nfscl_doflayoutio(vnode_t vp, struct uio *uiop, int *iomode, int *must_commit,
6042 int *eofp, nfsv4stateid_t *stateidp, int rwflag, struct nfscldevinfo *dp,
6043 struct nfscllayout *lyp, struct nfsclflayout *flp, uint64_t off,
6044 uint64_t len, int docommit, struct ucred *cred, NFSPROC_T *p)
6046 uint64_t io_off, rel_off, stripe_unit_size, transfer, xfer;
6047 int commit_thru_mds, error, stripe_index, stripe_pos, minorvers;
6050 struct nfsclds **dspp;
6053 rel_off = off - flp->nfsfl_patoff;
6054 stripe_unit_size = (flp->nfsfl_util >> 6) & 0x3ffffff;
6055 stripe_pos = (rel_off / stripe_unit_size + flp->nfsfl_stripe1) %
6056 dp->nfsdi_stripecnt;
6057 transfer = stripe_unit_size - (rel_off % stripe_unit_size);
6060 /* Loop around, doing I/O for each stripe unit. */
6061 while (len > 0 && error == 0) {
6062 stripe_index = nfsfldi_stripeindex(dp, stripe_pos);
6063 dspp = nfsfldi_addr(dp, stripe_index);
6064 if (((*dspp)->nfsclds_flags & NFSCLDS_MINORV2) != 0)
6065 minorvers = NFSV42_MINORVERSION;
6067 minorvers = NFSV41_MINORVERSION;
6068 if (len > transfer && docommit == 0)
6072 if ((flp->nfsfl_util & NFSFLAYUTIL_DENSE) != 0) {
6074 if (stripe_pos >= flp->nfsfl_fhcnt)
6076 fhp = flp->nfsfl_fh[stripe_pos];
6077 io_off = (rel_off / (stripe_unit_size *
6078 dp->nfsdi_stripecnt)) * stripe_unit_size +
6079 rel_off % stripe_unit_size;
6081 /* Sparse layout. */
6082 if (flp->nfsfl_fhcnt > 1) {
6083 if (stripe_index >= flp->nfsfl_fhcnt)
6085 fhp = flp->nfsfl_fh[stripe_index];
6086 } else if (flp->nfsfl_fhcnt == 1)
6087 fhp = flp->nfsfl_fh[0];
6092 if ((flp->nfsfl_util & NFSFLAYUTIL_COMMIT_THRU_MDS) != 0) {
6093 commit_thru_mds = 1;
6097 commit_thru_mds = 0;
6099 np->n_flag |= NDSCOMMIT;
6102 if (docommit != 0) {
6104 error = nfsrpc_commitds(vp, io_off, xfer,
6105 *dspp, fhp, NFS_VER4, minorvers, cred, p);
6108 * Set both eof and uio_resid = 0 to end any
6112 uiop->uio_resid = 0;
6115 np->n_flag &= ~NDSCOMMIT;
6118 } else if (rwflag == NFSV4OPEN_ACCESSREAD)
6119 error = nfsrpc_readds(vp, uiop, stateidp, eofp, *dspp,
6120 io_off, xfer, fhp, 0, NFS_VER4, minorvers, cred, p);
6122 error = nfsrpc_writeds(vp, uiop, iomode, must_commit,
6123 stateidp, *dspp, io_off, xfer, fhp, commit_thru_mds,
6124 0, NFS_VER4, minorvers, cred, p);
6127 lyp->nfsly_flags |= NFSLY_WRITTEN;
6132 transfer = stripe_unit_size;
6133 stripe_pos = (stripe_pos + 1) % dp->nfsdi_stripecnt;
6142 * Do I/O using an NFSv4.1 flex file layout.
6145 nfscl_dofflayoutio(vnode_t vp, struct uio *uiop, int *iomode, int *must_commit,
6146 int *eofp, nfsv4stateid_t *stateidp, int rwflag, struct nfscldevinfo *dp,
6147 struct nfscllayout *lyp, struct nfsclflayout *flp, uint64_t off,
6148 uint64_t len, int mirror, int docommit, struct mbuf *mp,
6149 struct nfsclwritedsdorpc *drpc, struct ucred *cred, NFSPROC_T *p)
6151 uint64_t transfer, xfer;
6155 struct nfsclds **dspp;
6156 struct ucred *tcred;
6162 NFSCL_DEBUG(4, "nfscl_dofflayoutio: off=%ju len=%ju\n", (uintmax_t)off,
6164 /* Loop around, doing I/O for each stripe unit. */
6165 while (len > 0 && error == 0) {
6166 dspp = nfsfldi_addr(dp, 0);
6167 fhp = flp->nfsfl_ffm[mirror].fh[dp->nfsdi_versindex];
6168 stateidp = &flp->nfsfl_ffm[mirror].st;
6169 NFSCL_DEBUG(4, "mirror=%d vind=%d fhlen=%d st.seqid=0x%x\n",
6170 mirror, dp->nfsdi_versindex, fhp->nfh_len, stateidp->seqid);
6171 if ((dp->nfsdi_flags & NFSDI_TIGHTCOUPLED) == 0) {
6172 tcred = NFSNEWCRED(cred);
6173 tcred->cr_uid = flp->nfsfl_ffm[mirror].user;
6174 tcred->cr_groups[0] = flp->nfsfl_ffm[mirror].group;
6175 tcred->cr_ngroups = 1;
6178 if (rwflag == NFSV4OPEN_ACCESSREAD)
6179 transfer = dp->nfsdi_rsize;
6181 transfer = dp->nfsdi_wsize;
6183 np->n_flag |= NDSCOMMIT;
6185 if (len > transfer && docommit == 0)
6189 if (docommit != 0) {
6192 * Do last mirrored DS commit with this thread.
6194 if (mirror < flp->nfsfl_mirrorcnt - 1)
6195 error = nfsio_commitds(vp, off, xfer,
6196 *dspp, fhp, dp->nfsdi_vers,
6197 dp->nfsdi_minorvers, drpc, tcred,
6200 error = nfsrpc_commitds(vp, off, xfer,
6201 *dspp, fhp, dp->nfsdi_vers,
6202 dp->nfsdi_minorvers, tcred, p);
6203 NFSCL_DEBUG(4, "commitds=%d\n", error);
6204 if (error != 0 && error != EACCES && error !=
6207 "DS layreterr for commit\n");
6208 nfscl_dserr(NFSV4OP_COMMIT, error, dp,
6212 NFSCL_DEBUG(4, "aft nfsio_commitds=%d\n", error);
6215 * Set both eof and uio_resid = 0 to end any
6219 uiop->uio_resid = 0;
6222 np->n_flag &= ~NDSCOMMIT;
6225 } else if (rwflag == NFSV4OPEN_ACCESSREAD) {
6226 error = nfsrpc_readds(vp, uiop, stateidp, eofp, *dspp,
6227 off, xfer, fhp, 1, dp->nfsdi_vers,
6228 dp->nfsdi_minorvers, tcred, p);
6229 NFSCL_DEBUG(4, "readds=%d\n", error);
6230 if (error != 0 && error != EACCES && error != ESTALE) {
6231 NFSCL_DEBUG(4, "DS layreterr for read\n");
6232 nfscl_dserr(NFSV4OP_READ, error, dp, lyp,
6236 if (flp->nfsfl_mirrorcnt == 1) {
6237 error = nfsrpc_writeds(vp, uiop, iomode,
6238 must_commit, stateidp, *dspp, off, xfer,
6239 fhp, 0, 1, dp->nfsdi_vers,
6240 dp->nfsdi_minorvers, tcred, p);
6243 lyp->nfsly_flags |= NFSLY_WRITTEN;
6247 m = nfsm_copym(mp, rel_off, xfer);
6248 NFSCL_DEBUG(4, "mcopy reloff=%d xfer=%jd\n",
6249 rel_off, (uintmax_t)xfer);
6251 * Do last write to a mirrored DS with this
6254 if (mirror < flp->nfsfl_mirrorcnt - 1)
6255 error = nfsio_writedsmir(vp, iomode,
6256 must_commit, stateidp, *dspp, off,
6257 xfer, fhp, m, dp->nfsdi_vers,
6258 dp->nfsdi_minorvers, drpc, tcred,
6261 error = nfsrpc_writedsmir(vp, iomode,
6262 must_commit, stateidp, *dspp, off,
6263 xfer, fhp, m, dp->nfsdi_vers,
6264 dp->nfsdi_minorvers, tcred, p);
6265 NFSCL_DEBUG(4, "nfsio_writedsmir=%d\n", error);
6266 if (error != 0 && error != EACCES && error !=
6269 "DS layreterr for write\n");
6270 nfscl_dserr(NFSV4OP_WRITE, error, dp,
6275 NFSCL_DEBUG(4, "aft read/writeds=%d\n", error);
6281 if ((dp->nfsdi_flags & NFSDI_TIGHTCOUPLED) == 0)
6284 NFSCL_DEBUG(4, "eo nfscl_dofflayoutio=%d\n", error);
6289 * The actual read RPC done to a DS.
6292 nfsrpc_readds(vnode_t vp, struct uio *uiop, nfsv4stateid_t *stateidp, int *eofp,
6293 struct nfsclds *dsp, uint64_t io_off, int len, struct nfsfh *fhp, int flex,
6294 int vers, int minorvers, struct ucred *cred, NFSPROC_T *p)
6297 int attrflag, error, retlen;
6298 struct nfsrv_descript nfsd;
6299 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
6300 struct nfsrv_descript *nd = &nfsd;
6301 struct nfssockreq *nrp;
6305 if (vers == 0 || vers == NFS_VER4) {
6306 nfscl_reqstart(nd, NFSPROC_READDS, nmp, fhp->nfh_fh,
6307 fhp->nfh_len, NULL, &dsp->nfsclds_sess, vers, minorvers,
6310 NFSCL_DEBUG(4, "nfsrpc_readds: vers4 minvers=%d\n", minorvers);
6312 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID);
6314 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSEQIDZERO);
6316 nfscl_reqstart(nd, NFSPROC_READ, nmp, fhp->nfh_fh,
6317 fhp->nfh_len, NULL, &dsp->nfsclds_sess, vers, minorvers,
6319 NFSCL_DEBUG(4, "nfsrpc_readds: vers3\n");
6321 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED * 3);
6322 txdr_hyper(io_off, tl);
6323 *(tl + 2) = txdr_unsigned(len);
6324 nrp = dsp->nfsclds_sockp;
6325 NFSCL_DEBUG(4, "nfsrpc_readds: nrp=%p\n", nrp);
6327 /* If NULL, use the MDS socket. */
6328 nrp = &nmp->nm_sockreq;
6329 error = newnfs_request(nd, nmp, NULL, nrp, vp, p, cred,
6330 NFS_PROG, vers, NULL, 1, NULL, &dsp->nfsclds_sess);
6331 NFSCL_DEBUG(4, "nfsrpc_readds: stat=%d err=%d\n", nd->nd_repstat,
6335 if (vers == NFS_VER3) {
6336 error = nfscl_postop_attr(nd, &na, &attrflag, NULL);
6337 NFSCL_DEBUG(4, "nfsrpc_readds: postop=%d\n", error);
6341 if (nd->nd_repstat != 0) {
6342 error = nd->nd_repstat;
6345 if (vers == NFS_VER3) {
6346 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
6347 *eofp = fxdr_unsigned(int, *(tl + 1));
6349 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
6350 *eofp = fxdr_unsigned(int, *tl);
6352 NFSM_STRSIZ(retlen, len);
6353 NFSCL_DEBUG(4, "nfsrpc_readds: retlen=%d eof=%d\n", retlen, *eofp);
6354 error = nfsm_mbufuio(nd, uiop, retlen);
6356 if (nd->nd_mrep != NULL)
6357 m_freem(nd->nd_mrep);
6362 * The actual write RPC done to a DS.
6365 nfsrpc_writeds(vnode_t vp, struct uio *uiop, int *iomode, int *must_commit,
6366 nfsv4stateid_t *stateidp, struct nfsclds *dsp, uint64_t io_off, int len,
6367 struct nfsfh *fhp, int commit_thru_mds, int flex, int vers, int minorvers,
6368 struct ucred *cred, NFSPROC_T *p)
6371 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
6372 int attrflag, error, rlen, commit, committed = NFSWRITE_FILESYNC;
6374 struct nfsrv_descript nfsd;
6375 struct nfsrv_descript *nd = &nfsd;
6376 struct nfssockreq *nrp;
6379 KASSERT(uiop->uio_iovcnt == 1, ("nfs: writerpc iovcnt > 1"));
6381 if (vers == 0 || vers == NFS_VER4) {
6382 nfscl_reqstart(nd, NFSPROC_WRITEDS, nmp, fhp->nfh_fh,
6383 fhp->nfh_len, NULL, &dsp->nfsclds_sess, vers, minorvers,
6385 NFSCL_DEBUG(4, "nfsrpc_writeds: vers4 minvers=%d\n", minorvers);
6388 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID);
6390 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSEQIDZERO);
6391 NFSM_BUILD(tl, uint32_t *, NFSX_HYPER + 2 * NFSX_UNSIGNED);
6393 nfscl_reqstart(nd, NFSPROC_WRITE, nmp, fhp->nfh_fh,
6394 fhp->nfh_len, NULL, &dsp->nfsclds_sess, vers, minorvers,
6396 NFSCL_DEBUG(4, "nfsrpc_writeds: vers3\n");
6397 NFSM_BUILD(tl, uint32_t *, NFSX_HYPER + 3 * NFSX_UNSIGNED);
6399 txdr_hyper(io_off, tl);
6401 if (vers == NFS_VER3)
6402 *tl++ = txdr_unsigned(len);
6403 *tl++ = txdr_unsigned(*iomode);
6404 *tl = txdr_unsigned(len);
6405 nfsm_uiombuf(nd, uiop, len);
6406 nrp = dsp->nfsclds_sockp;
6408 /* If NULL, use the MDS socket. */
6409 nrp = &nmp->nm_sockreq;
6410 error = newnfs_request(nd, nmp, NULL, nrp, vp, p, cred,
6411 NFS_PROG, vers, NULL, 1, NULL, &dsp->nfsclds_sess);
6412 NFSCL_DEBUG(4, "nfsrpc_writeds: err=%d stat=%d\n", error,
6416 if (nd->nd_repstat != 0) {
6418 * In case the rpc gets retried, roll
6419 * the uio fileds changed by nfsm_uiombuf()
6422 uiop->uio_offset -= len;
6423 uiop->uio_resid += len;
6424 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base - len;
6425 uiop->uio_iov->iov_len += len;
6426 error = nd->nd_repstat;
6428 if (vers == NFS_VER3) {
6429 error = nfscl_wcc_data(nd, vp, &na, &attrflag, NULL,
6431 NFSCL_DEBUG(4, "nfsrpc_writeds: wcc_data=%d\n", error);
6435 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED + NFSX_VERF);
6436 rlen = fxdr_unsigned(int, *tl++);
6437 NFSCL_DEBUG(4, "nfsrpc_writeds: len=%d rlen=%d\n", len, rlen);
6441 } else if (rlen < len) {
6442 backup = len - rlen;
6443 uiop->uio_iov->iov_base =
6444 (char *)uiop->uio_iov->iov_base - backup;
6445 uiop->uio_iov->iov_len += backup;
6446 uiop->uio_offset -= backup;
6447 uiop->uio_resid += backup;
6450 commit = fxdr_unsigned(int, *tl++);
6453 * Return the lowest commitment level
6454 * obtained by any of the RPCs.
6456 if (committed == NFSWRITE_FILESYNC)
6458 else if (committed == NFSWRITE_DATASYNC &&
6459 commit == NFSWRITE_UNSTABLE)
6461 if (commit_thru_mds != 0) {
6463 if (!NFSHASWRITEVERF(nmp)) {
6464 NFSBCOPY(tl, nmp->nm_verf, NFSX_VERF);
6465 NFSSETWRITEVERF(nmp);
6466 } else if (NFSBCMP(tl, nmp->nm_verf, NFSX_VERF)) {
6468 NFSBCOPY(tl, nmp->nm_verf, NFSX_VERF);
6473 if ((dsp->nfsclds_flags & NFSCLDS_HASWRITEVERF) == 0) {
6474 NFSBCOPY(tl, dsp->nfsclds_verf, NFSX_VERF);
6475 dsp->nfsclds_flags |= NFSCLDS_HASWRITEVERF;
6476 } else if (NFSBCMP(tl, dsp->nfsclds_verf, NFSX_VERF)) {
6478 NFSBCOPY(tl, dsp->nfsclds_verf, NFSX_VERF);
6484 if (nd->nd_mrep != NULL)
6485 m_freem(nd->nd_mrep);
6486 *iomode = committed;
6487 if (nd->nd_repstat != 0 && error == 0)
6488 error = nd->nd_repstat;
6493 * The actual write RPC done to a DS.
6494 * This variant is called from a separate kernel process for mirrors.
6495 * Any short write is considered an IO error.
6498 nfsrpc_writedsmir(vnode_t vp, int *iomode, int *must_commit,
6499 nfsv4stateid_t *stateidp, struct nfsclds *dsp, uint64_t io_off, int len,
6500 struct nfsfh *fhp, struct mbuf *m, int vers, int minorvers,
6501 struct ucred *cred, NFSPROC_T *p)
6504 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
6505 int attrflag, error, commit, committed = NFSWRITE_FILESYNC, rlen;
6506 struct nfsrv_descript nfsd;
6507 struct nfsrv_descript *nd = &nfsd;
6508 struct nfssockreq *nrp;
6512 if (vers == 0 || vers == NFS_VER4) {
6513 nfscl_reqstart(nd, NFSPROC_WRITEDS, nmp, fhp->nfh_fh,
6514 fhp->nfh_len, NULL, &dsp->nfsclds_sess, vers, minorvers,
6517 NFSCL_DEBUG(4, "nfsrpc_writedsmir: vers4 minvers=%d\n",
6519 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID);
6520 NFSM_BUILD(tl, uint32_t *, NFSX_HYPER + 2 * NFSX_UNSIGNED);
6522 nfscl_reqstart(nd, NFSPROC_WRITE, nmp, fhp->nfh_fh,
6523 fhp->nfh_len, NULL, &dsp->nfsclds_sess, vers, minorvers,
6525 NFSCL_DEBUG(4, "nfsrpc_writedsmir: vers3\n");
6526 NFSM_BUILD(tl, uint32_t *, NFSX_HYPER + 3 * NFSX_UNSIGNED);
6528 txdr_hyper(io_off, tl);
6530 if (vers == NFS_VER3)
6531 *tl++ = txdr_unsigned(len);
6532 *tl++ = txdr_unsigned(*iomode);
6533 *tl = txdr_unsigned(len);
6535 /* Put data in mbuf chain. */
6536 nd->nd_mb->m_next = m;
6537 /* Set nd_mb and nd_bpos to end of data. */
6538 while (m->m_next != NULL)
6541 nd->nd_bpos = mtod(m, char *) + m->m_len;
6542 NFSCL_DEBUG(4, "nfsrpc_writedsmir: lastmb len=%d\n", m->m_len);
6544 nrp = dsp->nfsclds_sockp;
6546 /* If NULL, use the MDS socket. */
6547 nrp = &nmp->nm_sockreq;
6548 error = newnfs_request(nd, nmp, NULL, nrp, vp, p, cred,
6549 NFS_PROG, vers, NULL, 1, NULL, &dsp->nfsclds_sess);
6550 NFSCL_DEBUG(4, "nfsrpc_writedsmir: err=%d stat=%d\n", error,
6554 if (nd->nd_repstat != 0)
6555 error = nd->nd_repstat;
6557 if (vers == NFS_VER3) {
6558 error = nfscl_wcc_data(nd, vp, &na, &attrflag, NULL,
6560 NFSCL_DEBUG(4, "nfsrpc_writedsmir: wcc_data=%d\n",
6565 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED + NFSX_VERF);
6566 rlen = fxdr_unsigned(int, *tl++);
6567 NFSCL_DEBUG(4, "nfsrpc_writedsmir: len=%d rlen=%d\n", len,
6571 NFSCL_DEBUG(4, "nfsrpc_writedsmir: len=%d rlen=%d\n",
6575 commit = fxdr_unsigned(int, *tl++);
6578 * Return the lowest commitment level
6579 * obtained by any of the RPCs.
6581 if (committed == NFSWRITE_FILESYNC)
6583 else if (committed == NFSWRITE_DATASYNC &&
6584 commit == NFSWRITE_UNSTABLE)
6587 if ((dsp->nfsclds_flags & NFSCLDS_HASWRITEVERF) == 0) {
6588 NFSBCOPY(tl, dsp->nfsclds_verf, NFSX_VERF);
6589 dsp->nfsclds_flags |= NFSCLDS_HASWRITEVERF;
6590 } else if (NFSBCMP(tl, dsp->nfsclds_verf, NFSX_VERF)) {
6592 NFSBCOPY(tl, dsp->nfsclds_verf, NFSX_VERF);
6597 if (nd->nd_mrep != NULL)
6598 m_freem(nd->nd_mrep);
6599 *iomode = committed;
6600 if (nd->nd_repstat != 0 && error == 0)
6601 error = nd->nd_repstat;
6606 * Start up the thread that will execute nfsrpc_writedsmir().
6609 start_writedsmir(void *arg, int pending)
6611 struct nfsclwritedsdorpc *drpc;
6613 drpc = (struct nfsclwritedsdorpc *)arg;
6614 drpc->err = nfsrpc_writedsmir(drpc->vp, &drpc->iomode,
6615 &drpc->must_commit, drpc->stateidp, drpc->dsp, drpc->off, drpc->len,
6616 drpc->fhp, drpc->m, drpc->vers, drpc->minorvers, drpc->cred,
6619 NFSCL_DEBUG(4, "start_writedsmir: err=%d\n", drpc->err);
6623 * Set up the write DS mirror call for the pNFS I/O thread.
6626 nfsio_writedsmir(vnode_t vp, int *iomode, int *must_commit,
6627 nfsv4stateid_t *stateidp, struct nfsclds *dsp, uint64_t off, int len,
6628 struct nfsfh *fhp, struct mbuf *m, int vers, int minorvers,
6629 struct nfsclwritedsdorpc *drpc, struct ucred *cred, NFSPROC_T *p)
6636 drpc->iomode = *iomode;
6637 drpc->must_commit = *must_commit;
6638 drpc->stateidp = stateidp;
6645 drpc->minorvers = minorvers;
6650 if (nfs_pnfsiothreads != 0) {
6651 ret = nfs_pnfsio(start_writedsmir, drpc);
6652 NFSCL_DEBUG(4, "nfsio_writedsmir: nfs_pnfsio=%d\n", ret);
6655 error = nfsrpc_writedsmir(vp, iomode, must_commit, stateidp,
6656 dsp, off, len, fhp, m, vers, minorvers, cred, p);
6657 NFSCL_DEBUG(4, "nfsio_writedsmir: error=%d\n", error);
6662 * Free up the nfsclds structure.
6665 nfscl_freenfsclds(struct nfsclds *dsp)
6671 if (dsp->nfsclds_sockp != NULL) {
6672 NFSFREECRED(dsp->nfsclds_sockp->nr_cred);
6673 NFSFREEMUTEX(&dsp->nfsclds_sockp->nr_mtx);
6674 free(dsp->nfsclds_sockp->nr_nam, M_SONAME);
6675 free(dsp->nfsclds_sockp, M_NFSSOCKREQ);
6677 NFSFREEMUTEX(&dsp->nfsclds_mtx);
6678 NFSFREEMUTEX(&dsp->nfsclds_sess.nfsess_mtx);
6679 for (i = 0; i < NFSV4_CBSLOTS; i++) {
6680 if (dsp->nfsclds_sess.nfsess_cbslots[i].nfssl_reply != NULL)
6682 dsp->nfsclds_sess.nfsess_cbslots[i].nfssl_reply);
6684 free(dsp, M_NFSCLDS);
6687 static enum nfsclds_state
6688 nfscl_getsameserver(struct nfsmount *nmp, struct nfsclds *newdsp,
6689 struct nfsclds **retdspp, uint32_t *sequencep)
6691 struct nfsclds *dsp;
6695 * Search the list of nfsclds structures for one with the same
6699 TAILQ_FOREACH(dsp, &nmp->nm_sess, nfsclds_list) {
6700 if (dsp->nfsclds_servownlen == newdsp->nfsclds_servownlen &&
6701 dsp->nfsclds_servownlen != 0 &&
6702 !NFSBCMP(dsp->nfsclds_serverown, newdsp->nfsclds_serverown,
6703 dsp->nfsclds_servownlen) &&
6704 dsp->nfsclds_sess.nfsess_defunct == 0) {
6705 NFSCL_DEBUG(4, "fnd same fdsp=%p dsp=%p flg=0x%x\n",
6706 TAILQ_FIRST(&nmp->nm_sess), dsp,
6707 dsp->nfsclds_flags);
6709 /* Get sequenceid# from first entry. */
6711 dsp->nfsclds_sess.nfsess_sequenceid;
6714 /* Server major id matches. */
6715 if ((dsp->nfsclds_flags & NFSCLDS_DS) != 0) {
6717 return (NFSDSP_USETHISSESSION);
6723 return (NFSDSP_SEQTHISSESSION);
6724 return (NFSDSP_NOTFOUND);
6728 * NFS commit rpc to a NFSv4.1 DS.
6731 nfsrpc_commitds(vnode_t vp, uint64_t offset, int cnt, struct nfsclds *dsp,
6732 struct nfsfh *fhp, int vers, int minorvers, struct ucred *cred,
6736 struct nfsrv_descript nfsd, *nd = &nfsd;
6737 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
6738 struct nfssockreq *nrp;
6740 int attrflag, error;
6743 if (vers == 0 || vers == NFS_VER4) {
6744 nfscl_reqstart(nd, NFSPROC_COMMITDS, nmp, fhp->nfh_fh,
6745 fhp->nfh_len, NULL, &dsp->nfsclds_sess, vers, minorvers,
6749 nfscl_reqstart(nd, NFSPROC_COMMIT, nmp, fhp->nfh_fh,
6750 fhp->nfh_len, NULL, &dsp->nfsclds_sess, vers, minorvers,
6752 NFSCL_DEBUG(4, "nfsrpc_commitds: vers=%d minvers=%d\n", vers,
6754 NFSM_BUILD(tl, uint32_t *, NFSX_HYPER + NFSX_UNSIGNED);
6755 txdr_hyper(offset, tl);
6757 *tl = txdr_unsigned(cnt);
6758 nrp = dsp->nfsclds_sockp;
6760 /* If NULL, use the MDS socket. */
6761 nrp = &nmp->nm_sockreq;
6762 error = newnfs_request(nd, nmp, NULL, nrp, vp, p, cred,
6763 NFS_PROG, vers, NULL, 1, NULL, &dsp->nfsclds_sess);
6764 NFSCL_DEBUG(4, "nfsrpc_commitds: err=%d stat=%d\n", error,
6768 if (nd->nd_repstat == 0) {
6769 if (vers == NFS_VER3) {
6770 error = nfscl_wcc_data(nd, vp, &na, &attrflag, NULL,
6772 NFSCL_DEBUG(4, "nfsrpc_commitds: wccdata=%d\n", error);
6776 NFSM_DISSECT(tl, u_int32_t *, NFSX_VERF);
6778 if (NFSBCMP(tl, dsp->nfsclds_verf, NFSX_VERF)) {
6779 NFSBCOPY(tl, dsp->nfsclds_verf, NFSX_VERF);
6780 error = NFSERR_STALEWRITEVERF;
6785 if (error == 0 && nd->nd_repstat != 0)
6786 error = nd->nd_repstat;
6787 m_freem(nd->nd_mrep);
6792 * Start up the thread that will execute nfsrpc_commitds().
6795 start_commitds(void *arg, int pending)
6797 struct nfsclwritedsdorpc *drpc;
6799 drpc = (struct nfsclwritedsdorpc *)arg;
6800 drpc->err = nfsrpc_commitds(drpc->vp, drpc->off, drpc->len,
6801 drpc->dsp, drpc->fhp, drpc->vers, drpc->minorvers, drpc->cred,
6804 NFSCL_DEBUG(4, "start_commitds: err=%d\n", drpc->err);
6808 * Set up the commit DS mirror call for the pNFS I/O thread.
6811 nfsio_commitds(vnode_t vp, uint64_t offset, int cnt, struct nfsclds *dsp,
6812 struct nfsfh *fhp, int vers, int minorvers,
6813 struct nfsclwritedsdorpc *drpc, struct ucred *cred, NFSPROC_T *p)
6825 drpc->minorvers = minorvers;
6830 if (nfs_pnfsiothreads != 0) {
6831 ret = nfs_pnfsio(start_commitds, drpc);
6832 NFSCL_DEBUG(4, "nfsio_commitds: nfs_pnfsio=%d\n", ret);
6835 error = nfsrpc_commitds(vp, offset, cnt, dsp, fhp, vers,
6836 minorvers, cred, p);
6837 NFSCL_DEBUG(4, "nfsio_commitds: error=%d\n", error);
6845 nfsrpc_advise(vnode_t vp, off_t offset, uint64_t cnt, int advise,
6846 struct ucred *cred, NFSPROC_T *p)
6849 struct nfsrv_descript nfsd, *nd = &nfsd;
6853 NFSZERO_ATTRBIT(&hints);
6854 if (advise == POSIX_FADV_WILLNEED)
6855 NFSSETBIT_ATTRBIT(&hints, NFSV4IOHINT_WILLNEED);
6856 else if (advise == POSIX_FADV_DONTNEED)
6857 NFSSETBIT_ATTRBIT(&hints, NFSV4IOHINT_DONTNEED);
6860 NFSCL_REQSTART(nd, NFSPROC_IOADVISE, vp);
6861 nfsm_stateidtom(nd, NULL, NFSSTATEID_PUTALLZERO);
6862 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_HYPER);
6863 txdr_hyper(offset, tl);
6865 txdr_hyper(cnt, tl);
6866 nfsrv_putattrbit(nd, &hints);
6867 error = nfscl_request(nd, vp, p, cred, NULL);
6870 if (nd->nd_repstat != 0)
6871 error = nd->nd_repstat;
6872 m_freem(nd->nd_mrep);
6878 * NFS advise rpc to a NFSv4.2 DS.
6881 nfsrpc_adviseds(vnode_t vp, uint64_t offset, int cnt, int advise,
6882 struct nfsclds *dsp, struct nfsfh *fhp, int vers, int minorvers,
6883 struct ucred *cred, NFSPROC_T *p)
6886 struct nfsrv_descript nfsd, *nd = &nfsd;
6887 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
6888 struct nfssockreq *nrp;
6892 /* For NFS DSs prior to NFSv4.2, just return OK. */
6893 if (vers == NFS_VER3 || minorversion < NFSV42_MINORVERSION)
6895 NFSZERO_ATTRBIT(&hints);
6896 if (advise == POSIX_FADV_WILLNEED)
6897 NFSSETBIT_ATTRBIT(&hints, NFSV4IOHINT_WILLNEED);
6898 else if (advise == POSIX_FADV_DONTNEED)
6899 NFSSETBIT_ATTRBIT(&hints, NFSV4IOHINT_DONTNEED);
6903 nfscl_reqstart(nd, NFSPROC_IOADVISEDS, nmp, fhp->nfh_fh,
6904 fhp->nfh_len, NULL, &dsp->nfsclds_sess, vers, minorvers, false);
6906 NFSCL_DEBUG(4, "nfsrpc_adviseds: vers=%d minvers=%d\n", vers,
6908 nfsm_stateidtom(nd, NULL, NFSSTATEID_PUTALLZERO);
6909 NFSM_BUILD(tl, uint32_t *, NFSX_HYPER + NFSX_UNSIGNED);
6910 txdr_hyper(offset, tl);
6912 *tl = txdr_unsigned(cnt);
6913 nfsrv_putattrbit(nd, &hints);
6914 nrp = dsp->nfsclds_sockp;
6916 /* If NULL, use the MDS socket. */
6917 nrp = &nmp->nm_sockreq;
6918 error = newnfs_request(nd, nmp, NULL, nrp, vp, p, cred,
6919 NFS_PROG, vers, NULL, 1, NULL, &dsp->nfsclds_sess);
6920 NFSCL_DEBUG(4, "nfsrpc_adviseds: err=%d stat=%d\n", error,
6924 if (nd->nd_repstat != 0)
6925 error = nd->nd_repstat;
6926 m_freem(nd->nd_mrep);
6931 * Start up the thread that will execute nfsrpc_commitds().
6934 start_adviseds(void *arg, int pending)
6936 struct nfsclwritedsdorpc *drpc;
6938 drpc = (struct nfsclwritedsdorpc *)arg;
6939 drpc->err = nfsrpc_adviseds(drpc->vp, drpc->off, drpc->len,
6940 drpc->advise, drpc->dsp, drpc->fhp, drpc->vers, drpc->minorvers,
6941 drpc->cred, drpc->p);
6943 NFSCL_DEBUG(4, "start_adviseds: err=%d\n", drpc->err);
6947 * Set up the commit DS mirror call for the pNFS I/O thread.
6950 nfsio_adviseds(vnode_t vp, uint64_t offset, int cnt, int advise,
6951 struct nfsclds *dsp, struct nfsfh *fhp, int vers, int minorvers,
6952 struct nfsclwritedsdorpc *drpc, struct ucred *cred, NFSPROC_T *p)
6961 drpc->advise = advise;
6965 drpc->minorvers = minorvers;
6970 if (nfs_pnfsiothreads != 0) {
6971 ret = nfs_pnfsio(start_adviseds, drpc);
6972 NFSCL_DEBUG(4, "nfsio_adviseds: nfs_pnfsio=%d\n", ret);
6975 error = nfsrpc_adviseds(vp, offset, cnt, advise, dsp, fhp, vers,
6976 minorvers, cred, p);
6977 NFSCL_DEBUG(4, "nfsio_adviseds: error=%d\n", error);
6983 * Do the Allocate operation, retrying for recovery.
6986 nfsrpc_allocate(vnode_t vp, off_t off, off_t len, struct nfsvattr *nap,
6987 int *attrflagp, struct ucred *cred, NFSPROC_T *p, void *stuff)
6989 int error, expireret = 0, retrycnt, nostateid;
6990 uint32_t clidrev = 0;
6991 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
6992 struct nfsfh *nfhp = NULL;
6993 nfsv4stateid_t stateid;
7001 tmp_off = off + len;
7003 if (tmp_off > nmp->nm_maxfilesize || tmp_off < off) {
7007 if (nmp->nm_clp != NULL)
7008 clidrev = nmp->nm_clp->nfsc_clientidrev;
7010 nfhp = VTONFS(vp)->n_fhp;
7015 nfscl_getstateid(vp, nfhp->nfh_fh, nfhp->nfh_len,
7016 NFSV4OPEN_ACCESSWRITE, 0, cred, p, &stateid, &lckp);
7017 if (stateid.other[0] == 0 && stateid.other[1] == 0 &&
7018 stateid.other[2] == 0) {
7020 NFSCL_DEBUG(1, "stateid0 in allocate\n");
7024 * Not finding a stateid should probably never happen,
7025 * but just return an error for this case.
7030 error = nfsrpc_allocaterpc(vp, off, len, &stateid,
7031 nap, attrflagp, cred, p, stuff);
7032 if (error == NFSERR_STALESTATEID)
7033 nfscl_initiate_recovery(nmp->nm_clp);
7035 nfscl_lockderef(lckp);
7036 if (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
7037 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
7038 error == NFSERR_OLDSTATEID || error == NFSERR_BADSESSION) {
7039 (void) nfs_catnap(PZERO, error, "nfs_allocate");
7040 } else if ((error == NFSERR_EXPIRED ||
7041 error == NFSERR_BADSTATEID) && clidrev != 0) {
7042 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
7045 } while (error == NFSERR_GRACE || error == NFSERR_DELAY ||
7046 error == NFSERR_STALESTATEID || error == NFSERR_BADSESSION ||
7047 error == NFSERR_STALEDONTRECOVER ||
7048 (error == NFSERR_OLDSTATEID && retrycnt < 20) ||
7049 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
7050 expireret == 0 && clidrev != 0 && retrycnt < 4));
7051 if (error != 0 && retrycnt >= 4)
7060 nfsrpc_allocaterpc(vnode_t vp, off_t off, off_t len, nfsv4stateid_t *stateidp,
7061 struct nfsvattr *nap, int *attrflagp, struct ucred *cred, NFSPROC_T *p,
7066 struct nfsrv_descript nfsd;
7067 struct nfsrv_descript *nd = &nfsd;
7068 nfsattrbit_t attrbits;
7071 NFSCL_REQSTART(nd, NFSPROC_ALLOCATE, vp);
7072 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID);
7073 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_HYPER + NFSX_UNSIGNED);
7074 txdr_hyper(off, tl); tl += 2;
7075 txdr_hyper(len, tl); tl += 2;
7076 *tl = txdr_unsigned(NFSV4OP_GETATTR);
7077 NFSGETATTR_ATTRBIT(&attrbits);
7078 nfsrv_putattrbit(nd, &attrbits);
7079 error = nfscl_request(nd, vp, p, cred, stuff);
7082 if (nd->nd_repstat == 0) {
7083 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
7084 error = nfsm_loadattr(nd, nap);
7086 *attrflagp = NFS_LATTR_NOSHRINK;
7088 error = nd->nd_repstat;
7090 m_freem(nd->nd_mrep);
7095 * Set up the XDR arguments for the LayoutGet operation.
7098 nfsrv_setuplayoutget(struct nfsrv_descript *nd, int iomode, uint64_t offset,
7099 uint64_t len, uint64_t minlen, nfsv4stateid_t *stateidp, int layouttype,
7100 int layoutlen, int usecurstateid)
7104 NFSM_BUILD(tl, uint32_t *, 4 * NFSX_UNSIGNED + 3 * NFSX_HYPER +
7106 *tl++ = newnfs_false; /* Don't signal availability. */
7107 *tl++ = txdr_unsigned(layouttype);
7108 *tl++ = txdr_unsigned(iomode);
7109 txdr_hyper(offset, tl);
7111 txdr_hyper(len, tl);
7113 txdr_hyper(minlen, tl);
7115 if (usecurstateid != 0) {
7116 /* Special stateid for Current stateid. */
7117 *tl++ = txdr_unsigned(1);
7122 *tl++ = txdr_unsigned(stateidp->seqid);
7123 NFSCL_DEBUG(4, "layget seq=%d\n", (int)stateidp->seqid);
7124 *tl++ = stateidp->other[0];
7125 *tl++ = stateidp->other[1];
7126 *tl++ = stateidp->other[2];
7128 *tl = txdr_unsigned(layoutlen);
7132 * Parse the reply for a successful LayoutGet operation.
7135 nfsrv_parselayoutget(struct nfsmount *nmp, struct nfsrv_descript *nd,
7136 nfsv4stateid_t *stateidp, int *retonclosep, struct nfsclflayouthead *flhp)
7139 struct nfsclflayout *flp, *prevflp, *tflp;
7140 int cnt, error, fhcnt, gotiomode, i, iomode, j, k, l, laytype, nfhlen;
7142 uint64_t retlen, off;
7148 NFSCL_DEBUG(4, "in nfsrv_parselayoutget\n");
7152 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED + NFSX_STATEID);
7157 stateidp->seqid = fxdr_unsigned(uint32_t, *tl++);
7158 NFSCL_DEBUG(4, "retoncls=%d stseq=%d\n", *retonclosep,
7159 (int)stateidp->seqid);
7160 stateidp->other[0] = *tl++;
7161 stateidp->other[1] = *tl++;
7162 stateidp->other[2] = *tl++;
7163 cnt = fxdr_unsigned(int, *tl);
7164 NFSCL_DEBUG(4, "layg cnt=%d\n", cnt);
7165 if (cnt <= 0 || cnt > 10000) {
7166 /* Don't accept more than 10000 layouts in reply. */
7167 error = NFSERR_BADXDR;
7170 for (i = 0; i < cnt; i++) {
7171 /* Dissect to the layout type. */
7172 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_HYPER +
7174 off = fxdr_hyper(tl); tl += 2;
7175 retlen = fxdr_hyper(tl); tl += 2;
7176 iomode = fxdr_unsigned(int, *tl++);
7177 laytype = fxdr_unsigned(int, *tl);
7178 NFSCL_DEBUG(4, "layt=%d off=%ju len=%ju iom=%d\n", laytype,
7179 (uintmax_t)off, (uintmax_t)retlen, iomode);
7180 /* Ignore length of layout body for now. */
7181 if (laytype == NFSLAYOUT_NFSV4_1_FILES) {
7182 /* Parse the File layout up to fhcnt. */
7183 NFSM_DISSECT(tl, uint32_t *, 3 * NFSX_UNSIGNED +
7184 NFSX_HYPER + NFSX_V4DEVICEID);
7185 fhcnt = fxdr_unsigned(int, *(tl + 4 +
7186 NFSX_V4DEVICEID / NFSX_UNSIGNED));
7187 NFSCL_DEBUG(4, "fhcnt=%d\n", fhcnt);
7188 if (fhcnt < 0 || fhcnt > 100) {
7189 /* Don't accept more than 100 file handles. */
7190 error = NFSERR_BADXDR;
7194 flp = malloc(sizeof(*flp) + fhcnt *
7195 sizeof(struct nfsfh *), M_NFSFLAYOUT,
7198 flp = malloc(sizeof(*flp), M_NFSFLAYOUT,
7200 flp->nfsfl_flags = NFSFL_FILE;
7201 flp->nfsfl_fhcnt = 0;
7202 flp->nfsfl_devp = NULL;
7203 flp->nfsfl_off = off;
7204 if (flp->nfsfl_off + retlen < flp->nfsfl_off)
7205 flp->nfsfl_end = UINT64_MAX - flp->nfsfl_off;
7207 flp->nfsfl_end = flp->nfsfl_off + retlen;
7208 flp->nfsfl_iomode = iomode;
7209 if (gotiomode == -1)
7210 gotiomode = flp->nfsfl_iomode;
7211 /* Ignore layout body length for now. */
7212 NFSBCOPY(tl, flp->nfsfl_dev, NFSX_V4DEVICEID);
7213 tl += (NFSX_V4DEVICEID / NFSX_UNSIGNED);
7214 flp->nfsfl_util = fxdr_unsigned(uint32_t, *tl++);
7215 NFSCL_DEBUG(4, "flutil=0x%x\n", flp->nfsfl_util);
7216 mtx_lock(&nmp->nm_mtx);
7217 if (nmp->nm_minorvers > 1 && (flp->nfsfl_util &
7218 NFSFLAYUTIL_IOADVISE_THRU_MDS) != 0)
7219 nmp->nm_privflag |= NFSMNTP_IOADVISETHRUMDS;
7220 mtx_unlock(&nmp->nm_mtx);
7221 flp->nfsfl_stripe1 = fxdr_unsigned(uint32_t, *tl++);
7222 flp->nfsfl_patoff = fxdr_hyper(tl); tl += 2;
7223 NFSCL_DEBUG(4, "stripe1=%u poff=%ju\n",
7224 flp->nfsfl_stripe1, (uintmax_t)flp->nfsfl_patoff);
7225 for (j = 0; j < fhcnt; j++) {
7226 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
7227 nfhlen = fxdr_unsigned(int, *tl);
7228 if (nfhlen <= 0 || nfhlen > NFSX_V4FHMAX) {
7229 error = NFSERR_BADXDR;
7232 nfhp = malloc(sizeof(*nfhp) + nfhlen - 1,
7234 flp->nfsfl_fh[j] = nfhp;
7236 nfhp->nfh_len = nfhlen;
7237 NFSM_DISSECT(cp, uint8_t *, NFSM_RNDUP(nfhlen));
7238 NFSBCOPY(cp, nfhp->nfh_fh, nfhlen);
7240 } else if (laytype == NFSLAYOUT_FLEXFILE) {
7241 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED +
7243 mirrorcnt = fxdr_unsigned(int, *(tl + 2));
7244 NFSCL_DEBUG(4, "mirrorcnt=%d\n", mirrorcnt);
7245 if (mirrorcnt < 1 || mirrorcnt > NFSDEV_MAXMIRRORS) {
7246 error = NFSERR_BADXDR;
7249 flp = malloc(sizeof(*flp) + mirrorcnt *
7250 sizeof(struct nfsffm), M_NFSFLAYOUT, M_WAITOK);
7251 flp->nfsfl_flags = NFSFL_FLEXFILE;
7252 flp->nfsfl_mirrorcnt = mirrorcnt;
7253 for (j = 0; j < mirrorcnt; j++)
7254 flp->nfsfl_ffm[j].devp = NULL;
7255 flp->nfsfl_off = off;
7256 if (flp->nfsfl_off + retlen < flp->nfsfl_off)
7257 flp->nfsfl_end = UINT64_MAX - flp->nfsfl_off;
7259 flp->nfsfl_end = flp->nfsfl_off + retlen;
7260 flp->nfsfl_iomode = iomode;
7261 if (gotiomode == -1)
7262 gotiomode = flp->nfsfl_iomode;
7263 flp->nfsfl_stripeunit = fxdr_hyper(tl);
7264 NFSCL_DEBUG(4, "stripeunit=%ju\n",
7265 (uintmax_t)flp->nfsfl_stripeunit);
7266 for (j = 0; j < mirrorcnt; j++) {
7267 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
7268 k = fxdr_unsigned(int, *tl);
7269 if (k < 1 || k > 128) {
7270 error = NFSERR_BADXDR;
7273 NFSCL_DEBUG(4, "servercnt=%d\n", k);
7274 for (l = 0; l < k; l++) {
7275 NFSM_DISSECT(tl, uint32_t *,
7276 NFSX_V4DEVICEID + NFSX_STATEID +
7279 /* Just use the first server. */
7281 flp->nfsfl_ffm[j].dev,
7283 tl += (NFSX_V4DEVICEID /
7286 flp->nfsfl_ffm[j].st.seqid =
7288 flp->nfsfl_ffm[j].st.other[0] =
7290 flp->nfsfl_ffm[j].st.other[1] =
7292 flp->nfsfl_ffm[j].st.other[2] =
7294 NFSCL_DEBUG(4, "st.seqid=%u "
7295 "st.o0=0x%x st.o1=0x%x "
7297 flp->nfsfl_ffm[j].st.seqid,
7298 flp->nfsfl_ffm[j].st.other[0],
7299 flp->nfsfl_ffm[j].st.other[1],
7300 flp->nfsfl_ffm[j].st.other[2]);
7302 tl += ((NFSX_V4DEVICEID +
7306 fhcnt = fxdr_unsigned(int, *tl);
7307 NFSCL_DEBUG(4, "fhcnt=%d\n", fhcnt);
7309 fhcnt > NFSDEV_MAXVERS) {
7310 error = NFSERR_BADXDR;
7313 for (m = 0; m < fhcnt; m++) {
7314 NFSM_DISSECT(tl, uint32_t *,
7316 nfhlen = fxdr_unsigned(int,
7318 NFSCL_DEBUG(4, "nfhlen=%d\n",
7320 if (nfhlen <= 0 || nfhlen >
7322 error = NFSERR_BADXDR;
7325 NFSM_DISSECT(cp, uint8_t *,
7326 NFSM_RNDUP(nfhlen));
7328 flp->nfsfl_ffm[j].fhcnt
7332 nfhlen - 1, M_NFSFH,
7334 flp->nfsfl_ffm[j].fh[m]
7336 nfhp->nfh_len = nfhlen;
7344 /* Now, get the ffsd_user/ffds_group. */
7345 error = nfsrv_parseug(nd, 0, &user,
7347 NFSCL_DEBUG(4, "after parseu=%d\n",
7350 error = nfsrv_parseug(nd, 1,
7351 &user, &grp, curthread);
7352 NFSCL_DEBUG(4, "aft parseg=%d\n",
7356 NFSCL_DEBUG(4, "user=%d group=%d\n",
7359 flp->nfsfl_ffm[j].user = user;
7360 flp->nfsfl_ffm[j].group = grp;
7362 "usr=%d grp=%d\n", user,
7367 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
7368 flp->nfsfl_fflags = fxdr_unsigned(uint32_t, *tl++);
7371 * At this time, there is no flag.
7372 * NFSFLEXFLAG_IOADVISE_THRU_MDS might need to be
7373 * added, or it may never exist?
7375 mtx_lock(&nmp->nm_mtx);
7376 if (nmp->nm_minorvers > 1 && (flp->nfsfl_fflags &
7377 NFSFLEXFLAG_IOADVISE_THRU_MDS) != 0)
7378 nmp->nm_privflag |= NFSMNTP_IOADVISETHRUMDS;
7379 mtx_unlock(&nmp->nm_mtx);
7381 flp->nfsfl_statshint = fxdr_unsigned(uint32_t, *tl);
7382 NFSCL_DEBUG(4, "fflags=0x%x statshint=%d\n",
7383 flp->nfsfl_fflags, flp->nfsfl_statshint);
7385 error = NFSERR_BADXDR;
7388 if (flp->nfsfl_iomode == gotiomode) {
7389 /* Keep the list in increasing offset order. */
7390 tflp = LIST_FIRST(flhp);
7392 while (tflp != NULL &&
7393 tflp->nfsfl_off < flp->nfsfl_off) {
7395 tflp = LIST_NEXT(tflp, nfsfl_list);
7397 if (prevflp == NULL)
7398 LIST_INSERT_HEAD(flhp, flp, nfsfl_list);
7400 LIST_INSERT_AFTER(prevflp, flp,
7402 NFSCL_DEBUG(4, "flp inserted\n");
7404 printf("nfscl_layoutget(): got wrong iomode\n");
7405 nfscl_freeflayout(flp);
7410 NFSCL_DEBUG(4, "eo nfsrv_parselayoutget=%d\n", error);
7411 if (error != 0 && flp != NULL)
7412 nfscl_freeflayout(flp);
7417 * Parse a user/group digit string.
7420 nfsrv_parseug(struct nfsrv_descript *nd, int dogrp, uid_t *uidp, gid_t *gidp,
7424 char *cp, *str, str0[NFSV4_SMALLSTR + 1];
7428 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
7429 len = fxdr_unsigned(uint32_t, *tl);
7431 if (len > NFSV4_OPAQUELIMIT) {
7432 error = NFSERR_BADXDR;
7435 NFSCL_DEBUG(4, "nfsrv_parseug: len=%d\n", len);
7438 *gidp = GID_NOGROUP;
7443 if (len > NFSV4_SMALLSTR)
7444 str = malloc(len + 1, M_TEMP, M_WAITOK);
7447 NFSM_DISSECT(cp, char *, NFSM_RNDUP(len));
7448 NFSBCOPY(cp, str, len);
7450 NFSCL_DEBUG(4, "nfsrv_parseug: str=%s\n", str);
7452 error = nfsv4_strtogid(nd, str, len, gidp);
7454 error = nfsv4_strtouid(nd, str, len, uidp);
7456 if (len > NFSV4_SMALLSTR)
7458 NFSCL_DEBUG(4, "eo nfsrv_parseug=%d\n", error);
7463 * Similar to nfsrpc_getlayout(), except that it uses nfsrpc_openlayget(),
7464 * so that it does both an Open and a Layoutget.
7467 nfsrpc_getopenlayout(struct nfsmount *nmp, vnode_t vp, u_int8_t *nfhp,
7468 int fhlen, uint8_t *newfhp, int newfhlen, uint32_t mode,
7469 struct nfsclopen *op, uint8_t *name, int namelen, struct nfscldeleg **dpp,
7470 struct ucred *cred, NFSPROC_T *p)
7472 struct nfscllayout *lyp;
7473 struct nfsclflayout *flp;
7474 struct nfsclflayouthead flh;
7475 int error, islocked, layoutlen, recalled, retonclose, usecurstateid;
7476 int layouttype, laystat;
7477 nfsv4stateid_t stateid;
7478 struct nfsclsession *tsep;
7481 if (NFSHASFLEXFILE(nmp))
7482 layouttype = NFSLAYOUT_FLEXFILE;
7484 layouttype = NFSLAYOUT_NFSV4_1_FILES;
7486 * If lyp is returned non-NULL, there will be a refcnt (shared lock)
7487 * on it, iff flp != NULL or a lock (exclusive lock) on it iff
7490 lyp = nfscl_getlayout(nmp->nm_clp, newfhp, newfhlen, 0, &flp,
7492 NFSCL_DEBUG(4, "nfsrpc_getopenlayout nfscl_getlayout lyp=%p\n", lyp);
7495 else if (flp != NULL)
7499 if ((lyp == NULL || flp == NULL) && recalled == 0) {
7501 tsep = nfsmnt_mdssession(nmp);
7502 layoutlen = tsep->nfsess_maxcache - (NFSX_STATEID +
7508 stateid.seqid = lyp->nfsly_stateid.seqid;
7509 stateid.other[0] = lyp->nfsly_stateid.other[0];
7510 stateid.other[1] = lyp->nfsly_stateid.other[1];
7511 stateid.other[2] = lyp->nfsly_stateid.other[2];
7513 error = nfsrpc_openlayoutrpc(nmp, vp, nfhp, fhlen,
7514 newfhp, newfhlen, mode, op, name, namelen,
7515 dpp, &stateid, usecurstateid, layouttype, layoutlen,
7516 &retonclose, &flh, &laystat, cred, p);
7517 NFSCL_DEBUG(4, "aft nfsrpc_openlayoutrpc laystat=%d err=%d\n",
7519 laystat = nfsrpc_layoutgetres(nmp, vp, newfhp, newfhlen,
7520 &stateid, retonclose, NULL, &lyp, &flh, layouttype, laystat,
7521 &islocked, cred, p);
7523 error = nfsrpc_openrpc(nmp, vp, nfhp, fhlen, newfhp, newfhlen,
7524 mode, op, name, namelen, dpp, 0, 0, cred, p, 0, 0);
7526 nfscl_rellayout(lyp, 1);
7527 else if (islocked == 1)
7528 nfscl_rellayout(lyp, 0);
7533 * This function does an Open+LayoutGet for an NFSv4.1 mount with pNFS
7534 * enabled, only for the CLAIM_NULL case. All other NFSv4 Opens are
7535 * handled by nfsrpc_openrpc().
7536 * For the case where op == NULL, dvp is the directory. When op != NULL, it
7540 nfsrpc_openlayoutrpc(struct nfsmount *nmp, vnode_t vp, u_int8_t *nfhp,
7541 int fhlen, uint8_t *newfhp, int newfhlen, uint32_t mode,
7542 struct nfsclopen *op, uint8_t *name, int namelen, struct nfscldeleg **dpp,
7543 nfsv4stateid_t *stateidp, int usecurstateid, int layouttype,
7544 int layoutlen, int *retonclosep, struct nfsclflayouthead *flhp,
7545 int *laystatp, struct ucred *cred, NFSPROC_T *p)
7548 struct nfsrv_descript nfsd, *nd = &nfsd;
7549 struct nfscldeleg *ndp = NULL;
7550 struct nfsvattr nfsva;
7551 struct nfsclsession *tsep;
7552 uint32_t rflags, deleg;
7553 nfsattrbit_t attrbits;
7554 int error, ret, acesize, limitby, iomode;
7558 nfscl_reqstart(nd, NFSPROC_OPENLAYGET, nmp, nfhp, fhlen, NULL, NULL,
7560 NFSM_BUILD(tl, uint32_t *, 5 * NFSX_UNSIGNED);
7561 *tl++ = txdr_unsigned(op->nfso_own->nfsow_seqid);
7562 *tl++ = txdr_unsigned(mode & NFSV4OPEN_ACCESSBOTH);
7563 *tl++ = txdr_unsigned((mode >> NFSLCK_SHIFT) & NFSV4OPEN_DENYBOTH);
7564 tsep = nfsmnt_mdssession(nmp);
7565 *tl++ = tsep->nfsess_clientid.lval[0];
7566 *tl = tsep->nfsess_clientid.lval[1];
7567 nfsm_strtom(nd, op->nfso_own->nfsow_owner, NFSV4CL_LOCKNAMELEN);
7568 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_UNSIGNED);
7569 *tl++ = txdr_unsigned(NFSV4OPEN_NOCREATE);
7570 *tl = txdr_unsigned(NFSV4OPEN_CLAIMNULL);
7571 nfsm_strtom(nd, name, namelen);
7572 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
7573 *tl = txdr_unsigned(NFSV4OP_GETATTR);
7574 NFSZERO_ATTRBIT(&attrbits);
7575 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_CHANGE);
7576 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEMODIFY);
7577 nfsrv_putattrbit(nd, &attrbits);
7578 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
7579 *tl = txdr_unsigned(NFSV4OP_LAYOUTGET);
7580 if ((mode & NFSV4OPEN_ACCESSWRITE) != 0)
7581 iomode = NFSLAYOUTIOMODE_RW;
7583 iomode = NFSLAYOUTIOMODE_READ;
7584 nfsrv_setuplayoutget(nd, iomode, 0, UINT64_MAX, 0, stateidp,
7585 layouttype, layoutlen, usecurstateid);
7586 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, vp, p, cred,
7587 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
7590 NFSCL_INCRSEQID(op->nfso_own->nfsow_seqid, nd);
7591 if (nd->nd_repstat != 0)
7592 *laystatp = nd->nd_repstat;
7593 if ((nd->nd_flag & ND_NOMOREDATA) == 0) {
7594 /* ND_NOMOREDATA will be set if the Open operation failed. */
7595 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
7597 op->nfso_stateid.seqid = *tl++;
7598 op->nfso_stateid.other[0] = *tl++;
7599 op->nfso_stateid.other[1] = *tl++;
7600 op->nfso_stateid.other[2] = *tl;
7601 rflags = fxdr_unsigned(u_int32_t, *(tl + 6));
7602 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
7605 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
7606 deleg = fxdr_unsigned(u_int32_t, *tl);
7607 if (deleg == NFSV4OPEN_DELEGATEREAD ||
7608 deleg == NFSV4OPEN_DELEGATEWRITE) {
7609 if (!(op->nfso_own->nfsow_clp->nfsc_flags &
7610 NFSCLFLAGS_FIRSTDELEG))
7611 op->nfso_own->nfsow_clp->nfsc_flags |=
7612 (NFSCLFLAGS_FIRSTDELEG | NFSCLFLAGS_GOTDELEG);
7613 ndp = malloc(sizeof(struct nfscldeleg) + newfhlen,
7614 M_NFSCLDELEG, M_WAITOK);
7615 LIST_INIT(&ndp->nfsdl_owner);
7616 LIST_INIT(&ndp->nfsdl_lock);
7617 ndp->nfsdl_clp = op->nfso_own->nfsow_clp;
7618 ndp->nfsdl_fhlen = newfhlen;
7619 NFSBCOPY(newfhp, ndp->nfsdl_fh, newfhlen);
7620 newnfs_copyincred(cred, &ndp->nfsdl_cred);
7621 nfscl_lockinit(&ndp->nfsdl_rwlock);
7622 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
7624 ndp->nfsdl_stateid.seqid = *tl++;
7625 ndp->nfsdl_stateid.other[0] = *tl++;
7626 ndp->nfsdl_stateid.other[1] = *tl++;
7627 ndp->nfsdl_stateid.other[2] = *tl++;
7628 ret = fxdr_unsigned(int, *tl);
7629 if (deleg == NFSV4OPEN_DELEGATEWRITE) {
7630 ndp->nfsdl_flags = NFSCLDL_WRITE;
7632 * Indicates how much the file can grow.
7634 NFSM_DISSECT(tl, u_int32_t *,
7636 limitby = fxdr_unsigned(int, *tl++);
7638 case NFSV4OPEN_LIMITSIZE:
7639 ndp->nfsdl_sizelimit = fxdr_hyper(tl);
7641 case NFSV4OPEN_LIMITBLOCKS:
7642 ndp->nfsdl_sizelimit =
7643 fxdr_unsigned(u_int64_t, *tl++);
7644 ndp->nfsdl_sizelimit *=
7645 fxdr_unsigned(u_int64_t, *tl);
7648 error = NFSERR_BADXDR;
7652 ndp->nfsdl_flags = NFSCLDL_READ;
7654 ndp->nfsdl_flags |= NFSCLDL_RECALL;
7655 error = nfsrv_dissectace(nd, &ndp->nfsdl_ace, &ret,
7659 } else if (deleg != NFSV4OPEN_DELEGATENONE) {
7660 error = NFSERR_BADXDR;
7663 if ((rflags & NFSV4OPEN_LOCKTYPEPOSIX) != 0 ||
7664 nfscl_assumeposixlocks)
7665 op->nfso_posixlock = 1;
7667 op->nfso_posixlock = 0;
7668 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
7669 /* If the 2nd element == NFS_OK, the Getattr succeeded. */
7671 error = nfsv4_loadattr(nd, NULL, &nfsva, NULL,
7672 NULL, 0, NULL, NULL, NULL, NULL, NULL, 0,
7673 NULL, NULL, NULL, p, cred);
7677 ndp->nfsdl_change = nfsva.na_filerev;
7678 ndp->nfsdl_modtime = nfsva.na_mtime;
7679 ndp->nfsdl_flags |= NFSCLDL_MODTIMESET;
7684 * At this point, the Open has succeeded, so set
7685 * nd_repstat = NFS_OK. If the Layoutget failed,
7686 * this function just won't return a layout.
7688 if (nd->nd_repstat == 0) {
7689 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
7690 *laystatp = fxdr_unsigned(int, *++tl);
7691 if (*laystatp == 0) {
7692 error = nfsrv_parselayoutget(nmp, nd,
7693 stateidp, retonclosep, flhp);
7698 nd->nd_repstat = 0; /* Return 0 for Open. */
7701 if (nd->nd_repstat != 0 && error == 0)
7702 error = nd->nd_repstat;
7704 free(ndp, M_NFSCLDELEG);
7705 m_freem(nd->nd_mrep);
7710 * Similar nfsrpc_createv4(), but also does the LayoutGet operation.
7711 * Used only for mounts with pNFS enabled.
7714 nfsrpc_createlayout(vnode_t dvp, char *name, int namelen, struct vattr *vap,
7715 nfsquad_t cverf, int fmode, struct nfsclowner *owp, struct nfscldeleg **dpp,
7716 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap,
7717 struct nfsvattr *nnap, struct nfsfh **nfhpp, int *attrflagp,
7718 int *dattrflagp, void *dstuff, int *unlockedp, nfsv4stateid_t *stateidp,
7719 int usecurstateid, int layouttype, int layoutlen, int *retonclosep,
7720 struct nfsclflayouthead *flhp, int *laystatp)
7723 int error = 0, deleg, newone, ret, acesize, limitby;
7724 struct nfsrv_descript nfsd, *nd = &nfsd;
7725 struct nfsclopen *op;
7726 struct nfscldeleg *dp = NULL;
7729 struct nfsclsession *tsep;
7730 nfsattrbit_t attrbits;
7731 nfsv4stateid_t stateid;
7732 struct nfsmount *nmp;
7734 nmp = VFSTONFS(dvp->v_mount);
7742 if (namelen > NFS_MAXNAMLEN)
7743 return (ENAMETOOLONG);
7744 NFSCL_REQSTART(nd, NFSPROC_CREATELAYGET, dvp);
7746 * For V4, this is actually an Open op.
7748 NFSM_BUILD(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
7749 *tl++ = txdr_unsigned(owp->nfsow_seqid);
7750 *tl++ = txdr_unsigned(NFSV4OPEN_ACCESSWRITE |
7751 NFSV4OPEN_ACCESSREAD);
7752 *tl++ = txdr_unsigned(NFSV4OPEN_DENYNONE);
7753 tsep = nfsmnt_mdssession(nmp);
7754 *tl++ = tsep->nfsess_clientid.lval[0];
7755 *tl = tsep->nfsess_clientid.lval[1];
7756 nfsm_strtom(nd, owp->nfsow_owner, NFSV4CL_LOCKNAMELEN);
7757 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
7758 *tl++ = txdr_unsigned(NFSV4OPEN_CREATE);
7759 if ((fmode & O_EXCL) != 0) {
7760 if (NFSHASSESSPERSIST(nmp)) {
7761 /* Use GUARDED for persistent sessions. */
7762 *tl = txdr_unsigned(NFSCREATE_GUARDED);
7763 nfscl_fillsattr(nd, vap, dvp, 0, 0);
7765 /* Otherwise, use EXCLUSIVE4_1. */
7766 *tl = txdr_unsigned(NFSCREATE_EXCLUSIVE41);
7767 NFSM_BUILD(tl, u_int32_t *, NFSX_VERF);
7768 *tl++ = cverf.lval[0];
7769 *tl = cverf.lval[1];
7770 nfscl_fillsattr(nd, vap, dvp, 0, 0);
7773 *tl = txdr_unsigned(NFSCREATE_UNCHECKED);
7774 nfscl_fillsattr(nd, vap, dvp, 0, 0);
7776 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
7777 *tl = txdr_unsigned(NFSV4OPEN_CLAIMNULL);
7778 nfsm_strtom(nd, name, namelen);
7779 /* Get the new file's handle and attributes, plus save the FH. */
7780 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
7781 *tl++ = txdr_unsigned(NFSV4OP_SAVEFH);
7782 *tl++ = txdr_unsigned(NFSV4OP_GETFH);
7783 *tl = txdr_unsigned(NFSV4OP_GETATTR);
7784 NFSGETATTR_ATTRBIT(&attrbits);
7785 nfsrv_putattrbit(nd, &attrbits);
7786 /* Get the directory's post-op attributes. */
7787 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
7788 *tl = txdr_unsigned(NFSV4OP_PUTFH);
7789 nfsm_fhtom(nd, np->n_fhp->nfh_fh, np->n_fhp->nfh_len, 0);
7790 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
7791 *tl = txdr_unsigned(NFSV4OP_GETATTR);
7792 nfsrv_putattrbit(nd, &attrbits);
7793 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
7794 *tl++ = txdr_unsigned(NFSV4OP_RESTOREFH);
7795 *tl = txdr_unsigned(NFSV4OP_LAYOUTGET);
7796 nfsrv_setuplayoutget(nd, NFSLAYOUTIOMODE_RW, 0, UINT64_MAX, 0, stateidp,
7797 layouttype, layoutlen, usecurstateid);
7798 error = nfscl_request(nd, dvp, p, cred, dstuff);
7801 NFSCL_DEBUG(4, "nfsrpc_createlayout stat=%d err=%d\n", nd->nd_repstat,
7803 if (nd->nd_repstat != 0)
7804 *laystatp = nd->nd_repstat;
7805 NFSCL_INCRSEQID(owp->nfsow_seqid, nd);
7806 if ((nd->nd_flag & ND_NOMOREDATA) == 0) {
7807 NFSCL_DEBUG(4, "nfsrpc_createlayout open succeeded\n");
7808 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
7810 stateid.seqid = *tl++;
7811 stateid.other[0] = *tl++;
7812 stateid.other[1] = *tl++;
7813 stateid.other[2] = *tl;
7814 nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
7815 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
7816 deleg = fxdr_unsigned(int, *tl);
7817 if (deleg == NFSV4OPEN_DELEGATEREAD ||
7818 deleg == NFSV4OPEN_DELEGATEWRITE) {
7819 if (!(owp->nfsow_clp->nfsc_flags &
7820 NFSCLFLAGS_FIRSTDELEG))
7821 owp->nfsow_clp->nfsc_flags |=
7822 (NFSCLFLAGS_FIRSTDELEG | NFSCLFLAGS_GOTDELEG);
7823 dp = malloc(sizeof(struct nfscldeleg) + NFSX_V4FHMAX,
7824 M_NFSCLDELEG, M_WAITOK);
7825 LIST_INIT(&dp->nfsdl_owner);
7826 LIST_INIT(&dp->nfsdl_lock);
7827 dp->nfsdl_clp = owp->nfsow_clp;
7828 newnfs_copyincred(cred, &dp->nfsdl_cred);
7829 nfscl_lockinit(&dp->nfsdl_rwlock);
7830 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
7832 dp->nfsdl_stateid.seqid = *tl++;
7833 dp->nfsdl_stateid.other[0] = *tl++;
7834 dp->nfsdl_stateid.other[1] = *tl++;
7835 dp->nfsdl_stateid.other[2] = *tl++;
7836 ret = fxdr_unsigned(int, *tl);
7837 if (deleg == NFSV4OPEN_DELEGATEWRITE) {
7838 dp->nfsdl_flags = NFSCLDL_WRITE;
7840 * Indicates how much the file can grow.
7842 NFSM_DISSECT(tl, u_int32_t *,
7844 limitby = fxdr_unsigned(int, *tl++);
7846 case NFSV4OPEN_LIMITSIZE:
7847 dp->nfsdl_sizelimit = fxdr_hyper(tl);
7849 case NFSV4OPEN_LIMITBLOCKS:
7850 dp->nfsdl_sizelimit =
7851 fxdr_unsigned(u_int64_t, *tl++);
7852 dp->nfsdl_sizelimit *=
7853 fxdr_unsigned(u_int64_t, *tl);
7856 error = NFSERR_BADXDR;
7860 dp->nfsdl_flags = NFSCLDL_READ;
7863 dp->nfsdl_flags |= NFSCLDL_RECALL;
7864 error = nfsrv_dissectace(nd, &dp->nfsdl_ace, &ret,
7868 } else if (deleg != NFSV4OPEN_DELEGATENONE) {
7869 error = NFSERR_BADXDR;
7873 /* Now, we should have the status for the SaveFH. */
7874 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
7876 NFSCL_DEBUG(4, "nfsrpc_createlayout SaveFH ok\n");
7878 * Now, process the GetFH and Getattr for the newly
7879 * created file. nfscl_mtofh() will set
7880 * ND_NOMOREDATA if these weren't successful.
7882 error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp);
7883 NFSCL_DEBUG(4, "aft nfscl_mtofh err=%d\n", error);
7887 nd->nd_flag |= ND_NOMOREDATA;
7888 /* Now we have the PutFH and Getattr for the directory. */
7889 if ((nd->nd_flag & ND_NOMOREDATA) == 0) {
7890 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
7892 nd->nd_flag |= ND_NOMOREDATA;
7894 NFSM_DISSECT(tl, uint32_t *, 2 *
7897 nd->nd_flag |= ND_NOMOREDATA;
7900 if ((nd->nd_flag & ND_NOMOREDATA) == 0) {
7901 /* Load the directory attributes. */
7902 error = nfsm_loadattr(nd, dnap);
7903 NFSCL_DEBUG(4, "aft nfsm_loadattr err=%d\n", error);
7907 if (dp != NULL && *attrflagp != 0) {
7908 dp->nfsdl_change = nnap->na_filerev;
7909 dp->nfsdl_modtime = nnap->na_mtime;
7910 dp->nfsdl_flags |= NFSCLDL_MODTIMESET;
7913 * We can now complete the Open state.
7917 dp->nfsdl_fhlen = nfhp->nfh_len;
7918 NFSBCOPY(nfhp->nfh_fh, dp->nfsdl_fh,
7922 * Get an Open structure that will be
7923 * attached to the OpenOwner, acquired already.
7925 error = nfscl_open(dvp, nfhp->nfh_fh, nfhp->nfh_len,
7926 (NFSV4OPEN_ACCESSWRITE | NFSV4OPEN_ACCESSREAD), 0,
7927 cred, p, NULL, &op, &newone, NULL, 0);
7930 op->nfso_stateid = stateid;
7931 newnfs_copyincred(cred, &op->nfso_cred);
7933 nfscl_openrelease(nmp, op, error, newone);
7936 /* Now, handle the RestoreFH and LayoutGet. */
7937 if (nd->nd_repstat == 0) {
7938 NFSM_DISSECT(tl, uint32_t *, 4 * NFSX_UNSIGNED);
7939 *laystatp = fxdr_unsigned(int, *(tl + 3));
7940 if (*laystatp == 0) {
7941 error = nfsrv_parselayoutget(nmp, nd,
7942 stateidp, retonclosep, flhp);
7946 NFSCL_DEBUG(4, "aft nfsrv_parselayout err=%d\n",
7952 if (nd->nd_repstat != 0 && error == 0)
7953 error = nd->nd_repstat;
7954 if (error == NFSERR_STALECLIENTID || error == NFSERR_BADSESSION)
7955 nfscl_initiate_recovery(owp->nfsow_clp);
7957 NFSCL_DEBUG(4, "eo nfsrpc_createlayout err=%d\n", error);
7961 free(dp, M_NFSCLDELEG);
7962 m_freem(nd->nd_mrep);
7967 * Similar to nfsrpc_getopenlayout(), except that it used for the Create case.
7970 nfsrpc_getcreatelayout(vnode_t dvp, char *name, int namelen, struct vattr *vap,
7971 nfsquad_t cverf, int fmode, struct nfsclowner *owp, struct nfscldeleg **dpp,
7972 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap,
7973 struct nfsvattr *nnap, struct nfsfh **nfhpp, int *attrflagp,
7974 int *dattrflagp, void *dstuff, int *unlockedp)
7976 struct nfscllayout *lyp;
7977 struct nfsclflayouthead flh;
7979 struct nfsclsession *tsep;
7980 struct nfsmount *nmp;
7981 nfsv4stateid_t stateid;
7982 int error, layoutlen, layouttype, retonclose, laystat;
7985 nmp = VFSTONFS(dvp->v_mount);
7986 if (NFSHASFLEXFILE(nmp))
7987 layouttype = NFSLAYOUT_FLEXFILE;
7989 layouttype = NFSLAYOUT_NFSV4_1_FILES;
7991 tsep = nfsmnt_mdssession(nmp);
7992 layoutlen = tsep->nfsess_maxcache - (NFSX_STATEID + 3 * NFSX_UNSIGNED);
7993 error = nfsrpc_createlayout(dvp, name, namelen, vap, cverf, fmode,
7994 owp, dpp, cred, p, dnap, nnap, nfhpp, attrflagp, dattrflagp,
7995 dstuff, unlockedp, &stateid, 1, layouttype, layoutlen, &retonclose,
7997 NFSCL_DEBUG(4, "aft nfsrpc_createlayoutrpc laystat=%d err=%d\n",
8002 laystat = nfsrpc_layoutgetres(nmp, dvp, nfhp->nfh_fh,
8003 nfhp->nfh_len, &stateid, retonclose, NULL, &lyp, &flh,
8004 layouttype, laystat, NULL, cred, p);
8006 laystat = nfsrpc_layoutgetres(nmp, dvp, NULL, 0, &stateid,
8007 retonclose, NULL, &lyp, &flh, layouttype, laystat, NULL,
8010 nfscl_rellayout(lyp, 0);
8015 * Process the results of a layoutget() operation.
8018 nfsrpc_layoutgetres(struct nfsmount *nmp, vnode_t vp, uint8_t *newfhp,
8019 int newfhlen, nfsv4stateid_t *stateidp, int retonclose, uint32_t *notifybit,
8020 struct nfscllayout **lypp, struct nfsclflayouthead *flhp, int layouttype,
8021 int laystat, int *islockedp, struct ucred *cred, NFSPROC_T *p)
8023 struct nfsclflayout *tflp;
8024 struct nfscldevinfo *dip;
8028 if (laystat == NFSERR_UNKNLAYOUTTYPE) {
8030 if (!NFSHASFLEXFILE(nmp)) {
8031 /* Switch to using Flex File Layout. */
8032 nmp->nm_state |= NFSSTA_FLEXFILE;
8033 } else if (layouttype == NFSLAYOUT_FLEXFILE) {
8035 NFSCL_DEBUG(1, "disable PNFS\n");
8036 nmp->nm_state &= ~(NFSSTA_PNFS | NFSSTA_FLEXFILE);
8041 NFSCL_DEBUG(4, "nfsrpc_layoutgetres at FOREACH\n");
8042 LIST_FOREACH(tflp, flhp, nfsfl_list) {
8043 if (layouttype == NFSLAYOUT_FLEXFILE)
8044 mirrorcnt = tflp->nfsfl_mirrorcnt;
8047 for (i = 0; i < mirrorcnt; i++) {
8048 laystat = nfscl_adddevinfo(nmp, NULL, i, tflp);
8049 NFSCL_DEBUG(4, "aft adddev=%d\n", laystat);
8051 if (layouttype == NFSLAYOUT_FLEXFILE)
8052 dev = tflp->nfsfl_ffm[i].dev;
8054 dev = tflp->nfsfl_dev;
8055 laystat = nfsrpc_getdeviceinfo(nmp, dev,
8056 layouttype, notifybit, &dip, cred,
8058 NFSCL_DEBUG(4, "aft nfsrpc_gdi=%d\n",
8062 laystat = nfscl_adddevinfo(nmp, dip, i,
8065 printf("nfsrpc_layoutgetresout"
8074 * nfscl_layout() always returns with the nfsly_lock
8075 * set to a refcnt (shared lock).
8076 * Passing in dvp is sufficient, since it is only used to
8077 * get the fsid for the file system.
8079 laystat = nfscl_layout(nmp, vp, newfhp, newfhlen, stateidp,
8080 layouttype, retonclose, flhp, lypp, cred, p);
8081 NFSCL_DEBUG(4, "nfsrpc_layoutgetres: aft nfscl_layout=%d\n",
8083 if (laystat == 0 && islockedp != NULL)
8090 * nfs copy_file_range operation.
8093 nfsrpc_copy_file_range(vnode_t invp, off_t *inoffp, vnode_t outvp,
8094 off_t *outoffp, size_t *lenp, unsigned int flags, int *inattrflagp,
8095 struct nfsvattr *innap, int *outattrflagp, struct nfsvattr *outnap,
8096 struct ucred *cred, bool consecutive, bool *must_commitp)
8098 int commit, error, expireret = 0, retrycnt;
8099 u_int32_t clidrev = 0;
8100 struct nfsmount *nmp = VFSTONFS(invp->v_mount);
8101 struct nfsfh *innfhp = NULL, *outnfhp = NULL;
8102 nfsv4stateid_t instateid, outstateid;
8103 void *inlckp, *outlckp;
8105 if (nmp->nm_clp != NULL)
8106 clidrev = nmp->nm_clp->nfsc_clientidrev;
8107 innfhp = VTONFS(invp)->n_fhp;
8108 outnfhp = VTONFS(outvp)->n_fhp;
8111 /* Get both stateids. */
8113 nfscl_getstateid(invp, innfhp->nfh_fh, innfhp->nfh_len,
8114 NFSV4OPEN_ACCESSREAD, 0, NULL, curthread, &instateid,
8117 nfscl_getstateid(outvp, outnfhp->nfh_fh, outnfhp->nfh_len,
8118 NFSV4OPEN_ACCESSWRITE, 0, NULL, curthread, &outstateid,
8121 error = nfsrpc_copyrpc(invp, *inoffp, outvp, *outoffp, lenp,
8122 &instateid, &outstateid, innap, inattrflagp, outnap,
8123 outattrflagp, consecutive, &commit, cred, curthread);
8125 if (commit != NFSWRITE_FILESYNC)
8126 *must_commitp = true;
8129 } else if (error == NFSERR_STALESTATEID)
8130 nfscl_initiate_recovery(nmp->nm_clp);
8132 nfscl_lockderef(inlckp);
8133 if (outlckp != NULL)
8134 nfscl_lockderef(outlckp);
8135 if (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
8136 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
8137 error == NFSERR_OLDSTATEID || error == NFSERR_BADSESSION) {
8138 (void) nfs_catnap(PZERO, error, "nfs_cfr");
8139 } else if ((error == NFSERR_EXPIRED ||
8140 error == NFSERR_BADSTATEID) && clidrev != 0) {
8141 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev,
8145 } while (error == NFSERR_GRACE || error == NFSERR_DELAY ||
8146 error == NFSERR_STALESTATEID || error == NFSERR_BADSESSION ||
8147 error == NFSERR_STALEDONTRECOVER ||
8148 (error == NFSERR_OLDSTATEID && retrycnt < 20) ||
8149 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
8150 expireret == 0 && clidrev != 0 && retrycnt < 4));
8151 if (error != 0 && (retrycnt >= 4 ||
8152 error == NFSERR_STALESTATEID || error == NFSERR_BADSESSION ||
8153 error == NFSERR_STALEDONTRECOVER))
8162 nfsrpc_copyrpc(vnode_t invp, off_t inoff, vnode_t outvp, off_t outoff,
8163 size_t *lenp, nfsv4stateid_t *instateidp, nfsv4stateid_t *outstateidp,
8164 struct nfsvattr *innap, int *inattrflagp, struct nfsvattr *outnap,
8165 int *outattrflagp, bool consecutive, int *commitp, struct ucred *cred,
8170 struct nfsrv_descript nfsd;
8171 struct nfsrv_descript *nd = &nfsd;
8172 struct nfsmount *nmp;
8173 nfsattrbit_t attrbits;
8176 nmp = VFSTONFS(outvp->v_mount);
8177 *inattrflagp = *outattrflagp = 0;
8178 *commitp = NFSWRITE_UNSTABLE;
8181 if (len > nfs_maxcopyrange)
8182 len = nfs_maxcopyrange;
8183 NFSCL_REQSTART(nd, NFSPROC_COPY, invp);
8184 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
8185 *tl = txdr_unsigned(NFSV4OP_GETATTR);
8186 NFSGETATTR_ATTRBIT(&attrbits);
8187 nfsrv_putattrbit(nd, &attrbits);
8188 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
8189 *tl = txdr_unsigned(NFSV4OP_PUTFH);
8190 nfsm_fhtom(nd, VTONFS(outvp)->n_fhp->nfh_fh,
8191 VTONFS(outvp)->n_fhp->nfh_len, 0);
8192 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
8193 *tl = txdr_unsigned(NFSV4OP_COPY);
8194 nfsm_stateidtom(nd, instateidp, NFSSTATEID_PUTSTATEID);
8195 nfsm_stateidtom(nd, outstateidp, NFSSTATEID_PUTSTATEID);
8196 NFSM_BUILD(tl, uint32_t *, 3 * NFSX_HYPER + 4 * NFSX_UNSIGNED);
8197 txdr_hyper(inoff, tl); tl += 2;
8198 txdr_hyper(outoff, tl); tl += 2;
8199 txdr_hyper(len, tl); tl += 2;
8201 *tl++ = newnfs_true;
8203 *tl++ = newnfs_false;
8204 *tl++ = newnfs_true;
8206 *tl = txdr_unsigned(NFSV4OP_GETATTR);
8207 NFSWRITEGETATTR_ATTRBIT(&attrbits);
8208 nfsrv_putattrbit(nd, &attrbits);
8209 error = nfscl_request(nd, invp, p, cred, NULL);
8212 if ((nd->nd_flag & ND_NOMOREDATA) == 0) {
8213 /* Get the input file's attributes. */
8214 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
8215 if (*(tl + 1) == 0) {
8216 error = nfsm_loadattr(nd, innap);
8221 nd->nd_flag |= ND_NOMOREDATA;
8223 /* Skip over return stat for PutFH. */
8224 if ((nd->nd_flag & ND_NOMOREDATA) == 0) {
8225 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
8227 nd->nd_flag |= ND_NOMOREDATA;
8229 /* Skip over return stat for Copy. */
8230 if ((nd->nd_flag & ND_NOMOREDATA) == 0)
8231 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
8232 if (nd->nd_repstat == 0) {
8233 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
8235 /* There should be no callback ids. */
8236 error = NFSERR_BADXDR;
8239 NFSM_DISSECT(tl, uint32_t *, NFSX_HYPER + 3 * NFSX_UNSIGNED +
8241 len = fxdr_hyper(tl); tl += 2;
8242 *commitp = fxdr_unsigned(int, *tl++);
8244 if (!NFSHASWRITEVERF(nmp)) {
8245 NFSBCOPY(tl, nmp->nm_verf, NFSX_VERF);
8246 NFSSETWRITEVERF(nmp);
8247 } else if (NFSBCMP(tl, nmp->nm_verf, NFSX_VERF)) {
8248 NFSBCOPY(tl, nmp->nm_verf, NFSX_VERF);
8249 nd->nd_repstat = NFSERR_STALEWRITEVERF;
8252 tl += (NFSX_VERF / NFSX_UNSIGNED);
8253 if (nd->nd_repstat == 0 && *++tl != newnfs_true)
8254 /* Must be a synchronous copy. */
8255 nd->nd_repstat = NFSERR_NOTSUPP;
8256 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
8257 error = nfsm_loadattr(nd, outnap);
8259 *outattrflagp = NFS_LATTR_NOSHRINK;
8260 if (nd->nd_repstat == 0)
8262 } else if (nd->nd_repstat == NFSERR_OFFLOADNOREQS) {
8264 * For the case where consecutive is not supported, but
8265 * synchronous is supported, we can try consecutive == false
8266 * by returning this error. Otherwise, return NFSERR_NOTSUPP,
8267 * since Copy cannot be done.
8269 if ((nd->nd_flag & ND_NOMOREDATA) == 0) {
8270 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
8271 if (!consecutive || *++tl == newnfs_false)
8272 nd->nd_repstat = NFSERR_NOTSUPP;
8274 nd->nd_repstat = NFSERR_BADXDR;
8277 error = nd->nd_repstat;
8279 m_freem(nd->nd_mrep);
8287 nfsrpc_seek(vnode_t vp, off_t *offp, bool *eofp, int content,
8288 struct ucred *cred, struct nfsvattr *nap, int *attrflagp)
8290 int error, expireret = 0, retrycnt;
8291 u_int32_t clidrev = 0;
8292 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
8293 struct nfsnode *np = VTONFS(vp);
8294 struct nfsfh *nfhp = NULL;
8295 nfsv4stateid_t stateid;
8298 if (nmp->nm_clp != NULL)
8299 clidrev = nmp->nm_clp->nfsc_clientidrev;
8304 nfscl_getstateid(vp, nfhp->nfh_fh, nfhp->nfh_len,
8305 NFSV4OPEN_ACCESSREAD, 0, cred, curthread, &stateid, &lckp);
8306 error = nfsrpc_seekrpc(vp, offp, &stateid, eofp, content,
8307 nap, attrflagp, cred);
8308 if (error == NFSERR_STALESTATEID)
8309 nfscl_initiate_recovery(nmp->nm_clp);
8311 nfscl_lockderef(lckp);
8312 if (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
8313 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
8314 error == NFSERR_OLDSTATEID || error == NFSERR_BADSESSION) {
8315 (void) nfs_catnap(PZERO, error, "nfs_seek");
8316 } else if ((error == NFSERR_EXPIRED ||
8317 error == NFSERR_BADSTATEID) && clidrev != 0) {
8318 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev,
8322 } while (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
8323 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
8324 error == NFSERR_BADSESSION ||
8325 (error == NFSERR_OLDSTATEID && retrycnt < 20) ||
8326 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
8327 expireret == 0 && clidrev != 0 && retrycnt < 4) ||
8328 (error == NFSERR_OPENMODE && retrycnt < 4));
8329 if (error && retrycnt >= 4)
8338 nfsrpc_seekrpc(vnode_t vp, off_t *offp, nfsv4stateid_t *stateidp, bool *eofp,
8339 int content, struct nfsvattr *nap, int *attrflagp, struct ucred *cred)
8343 struct nfsrv_descript nfsd;
8344 struct nfsrv_descript *nd = &nfsd;
8345 nfsattrbit_t attrbits;
8348 NFSCL_REQSTART(nd, NFSPROC_SEEK, vp);
8349 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID);
8350 NFSM_BUILD(tl, uint32_t *, NFSX_HYPER + 2 * NFSX_UNSIGNED);
8351 txdr_hyper(*offp, tl); tl += 2;
8352 *tl++ = txdr_unsigned(content);
8353 *tl = txdr_unsigned(NFSV4OP_GETATTR);
8354 NFSGETATTR_ATTRBIT(&attrbits);
8355 nfsrv_putattrbit(nd, &attrbits);
8356 error = nfscl_request(nd, vp, curthread, cred, NULL);
8359 if (nd->nd_repstat == 0) {
8360 NFSM_DISSECT(tl, uint32_t *, 3 * NFSX_UNSIGNED + NFSX_HYPER);
8361 if (*tl++ == newnfs_true)
8365 *offp = fxdr_hyper(tl);
8366 /* Just skip over Getattr op status. */
8367 error = nfsm_loadattr(nd, nap);
8371 error = nd->nd_repstat;
8373 m_freem(nd->nd_mrep);
8378 * The getextattr RPC.
8381 nfsrpc_getextattr(vnode_t vp, const char *name, struct uio *uiop, ssize_t *lenp,
8382 struct nfsvattr *nap, int *attrflagp, struct ucred *cred, NFSPROC_T *p)
8386 struct nfsrv_descript nfsd;
8387 struct nfsrv_descript *nd = &nfsd;
8388 nfsattrbit_t attrbits;
8392 NFSCL_REQSTART(nd, NFSPROC_GETEXTATTR, vp);
8393 nfsm_strtom(nd, name, strlen(name));
8394 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
8395 *tl = txdr_unsigned(NFSV4OP_GETATTR);
8396 NFSGETATTR_ATTRBIT(&attrbits);
8397 nfsrv_putattrbit(nd, &attrbits);
8398 error = nfscl_request(nd, vp, p, cred, NULL);
8401 if (nd->nd_repstat == 0) {
8402 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
8403 len = fxdr_unsigned(uint32_t, *tl);
8404 /* Sanity check lengths. */
8405 if (uiop != NULL && len > 0 && len <= IOSIZE_MAX &&
8406 uiop->uio_resid <= UINT32_MAX) {
8407 len2 = uiop->uio_resid;
8409 error = nfsm_mbufuio(nd, uiop, len);
8411 error = nfsm_mbufuio(nd, uiop, len2);
8414 * nfsm_mbufuio() advances to a multiple
8415 * of 4, so round up len2 as well. Then
8416 * we need to advance over the rest of
8417 * the data, rounding up the remaining
8420 len2 = NFSM_RNDUP(len2);
8421 len2 = NFSM_RNDUP(len - len2);
8423 error = nfsm_advance(nd, len2,
8427 } else if (uiop == NULL && len > 0) {
8428 /* Just wants the length and not the data. */
8429 error = nfsm_advance(nd, NFSM_RNDUP(len), -1);
8435 /* Just skip over Getattr op status. */
8436 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
8437 error = nfsm_loadattr(nd, nap);
8442 error = nd->nd_repstat;
8444 m_freem(nd->nd_mrep);
8449 * The setextattr RPC.
8452 nfsrpc_setextattr(vnode_t vp, const char *name, struct uio *uiop,
8453 struct nfsvattr *nap, int *attrflagp, struct ucred *cred, NFSPROC_T *p)
8457 struct nfsrv_descript nfsd;
8458 struct nfsrv_descript *nd = &nfsd;
8459 nfsattrbit_t attrbits;
8462 NFSCL_REQSTART(nd, NFSPROC_SETEXTATTR, vp);
8463 if (uiop->uio_resid > nd->nd_maxreq) {
8464 /* nd_maxreq is set by NFSCL_REQSTART(). */
8465 m_freem(nd->nd_mreq);
8468 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
8469 *tl = txdr_unsigned(NFSV4SXATTR_EITHER);
8470 nfsm_strtom(nd, name, strlen(name));
8471 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
8472 *tl = txdr_unsigned(uiop->uio_resid);
8473 nfsm_uiombuf(nd, uiop, uiop->uio_resid);
8474 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
8475 *tl = txdr_unsigned(NFSV4OP_GETATTR);
8476 NFSGETATTR_ATTRBIT(&attrbits);
8477 nfsrv_putattrbit(nd, &attrbits);
8478 error = nfscl_request(nd, vp, p, cred, NULL);
8481 if (nd->nd_repstat == 0) {
8482 /* Just skip over the reply and Getattr op status. */
8483 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_HYPER + 3 *
8485 error = nfsm_loadattr(nd, nap);
8490 error = nd->nd_repstat;
8492 m_freem(nd->nd_mrep);
8497 * The removeextattr RPC.
8500 nfsrpc_rmextattr(vnode_t vp, const char *name, struct nfsvattr *nap,
8501 int *attrflagp, struct ucred *cred, NFSPROC_T *p)
8505 struct nfsrv_descript nfsd;
8506 struct nfsrv_descript *nd = &nfsd;
8507 nfsattrbit_t attrbits;
8510 NFSCL_REQSTART(nd, NFSPROC_RMEXTATTR, vp);
8511 nfsm_strtom(nd, name, strlen(name));
8512 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
8513 *tl = txdr_unsigned(NFSV4OP_GETATTR);
8514 NFSGETATTR_ATTRBIT(&attrbits);
8515 nfsrv_putattrbit(nd, &attrbits);
8516 error = nfscl_request(nd, vp, p, cred, NULL);
8519 if (nd->nd_repstat == 0) {
8520 /* Just skip over the reply and Getattr op status. */
8521 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_HYPER + 3 *
8523 error = nfsm_loadattr(nd, nap);
8528 error = nd->nd_repstat;
8530 m_freem(nd->nd_mrep);
8535 * The listextattr RPC.
8538 nfsrpc_listextattr(vnode_t vp, uint64_t *cookiep, struct uio *uiop,
8539 size_t *lenp, bool *eofp, struct nfsvattr *nap, int *attrflagp,
8540 struct ucred *cred, NFSPROC_T *p)
8543 int cnt, error, i, len;
8544 struct nfsrv_descript nfsd;
8545 struct nfsrv_descript *nd = &nfsd;
8546 nfsattrbit_t attrbits;
8550 NFSCL_REQSTART(nd, NFSPROC_LISTEXTATTR, vp);
8551 NFSM_BUILD(tl, uint32_t *, NFSX_HYPER + 2 * NFSX_UNSIGNED);
8552 txdr_hyper(*cookiep, tl); tl += 2;
8553 *tl++ = txdr_unsigned(*lenp);
8554 *tl = txdr_unsigned(NFSV4OP_GETATTR);
8555 NFSGETATTR_ATTRBIT(&attrbits);
8556 nfsrv_putattrbit(nd, &attrbits);
8557 error = nfscl_request(nd, vp, p, cred, NULL);
8562 if (nd->nd_repstat == 0) {
8563 NFSM_DISSECT(tl, uint32_t *, NFSX_HYPER + NFSX_UNSIGNED);
8564 *cookiep = fxdr_hyper(tl); tl += 2;
8565 cnt = fxdr_unsigned(int, *tl);
8570 for (i = 0; i < cnt; i++) {
8571 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
8572 len = fxdr_unsigned(int, *tl);
8573 if (len <= 0 || len > EXTATTR_MAXNAMELEN) {
8578 error = nfsm_advance(nd, NFSM_RNDUP(len), -1);
8579 else if (uiop->uio_resid >= len + 1) {
8581 error = uiomove(&c, sizeof(c), uiop);
8583 error = nfsm_mbufuio(nd, uiop, len);
8585 error = nfsm_advance(nd, NFSM_RNDUP(len), -1);
8592 /* Get the eof and skip over the Getattr op status. */
8593 NFSM_DISSECT(tl, uint32_t *, 3 * NFSX_UNSIGNED);
8595 * *eofp is set false above, because it wasn't able to copy
8598 if (*eofp && *tl == 0)
8600 error = nfsm_loadattr(nd, nap);
8605 error = nd->nd_repstat;
8607 m_freem(nd->nd_mrep);