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(vnode_mount(vp));
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);
503 NFSM_BUILD(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
504 *tl++ = txdr_unsigned(op->nfso_own->nfsow_seqid);
505 *tl++ = txdr_unsigned(mode & NFSV4OPEN_ACCESSBOTH);
506 *tl++ = txdr_unsigned((mode >> NFSLCK_SHIFT) & NFSV4OPEN_DENYBOTH);
507 tsep = nfsmnt_mdssession(nmp);
508 *tl++ = tsep->nfsess_clientid.lval[0];
509 *tl = tsep->nfsess_clientid.lval[1];
510 (void) nfsm_strtom(nd, op->nfso_own->nfsow_owner, NFSV4CL_LOCKNAMELEN);
511 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
512 *tl++ = txdr_unsigned(NFSV4OPEN_NOCREATE);
514 *tl = txdr_unsigned(NFSV4OPEN_CLAIMPREVIOUS);
515 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
516 *tl = txdr_unsigned(delegtype);
519 *tl = txdr_unsigned(NFSV4OPEN_CLAIMDELEGATECUR);
520 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID);
521 if (NFSHASNFSV4N(nmp))
524 *tl++ = dp->nfsdl_stateid.seqid;
525 *tl++ = dp->nfsdl_stateid.other[0];
526 *tl++ = dp->nfsdl_stateid.other[1];
527 *tl = dp->nfsdl_stateid.other[2];
529 *tl = txdr_unsigned(NFSV4OPEN_CLAIMNULL);
531 (void) nfsm_strtom(nd, name, namelen);
533 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
534 *tl = txdr_unsigned(NFSV4OP_GETATTR);
535 NFSZERO_ATTRBIT(&attrbits);
536 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_CHANGE);
537 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEMODIFY);
538 (void) nfsrv_putattrbit(nd, &attrbits);
540 nd->nd_flag |= ND_USEGSSNAME;
541 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, vp, p, cred,
542 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
545 NFSCL_INCRSEQID(op->nfso_own->nfsow_seqid, nd);
546 if (!nd->nd_repstat) {
547 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
549 op->nfso_stateid.seqid = *tl++;
550 op->nfso_stateid.other[0] = *tl++;
551 op->nfso_stateid.other[1] = *tl++;
552 op->nfso_stateid.other[2] = *tl;
553 rflags = fxdr_unsigned(u_int32_t, *(tl + 6));
554 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
557 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
558 deleg = fxdr_unsigned(u_int32_t, *tl);
559 if (deleg == NFSV4OPEN_DELEGATEREAD ||
560 deleg == NFSV4OPEN_DELEGATEWRITE) {
561 if (!(op->nfso_own->nfsow_clp->nfsc_flags &
562 NFSCLFLAGS_FIRSTDELEG))
563 op->nfso_own->nfsow_clp->nfsc_flags |=
564 (NFSCLFLAGS_FIRSTDELEG | NFSCLFLAGS_GOTDELEG);
566 sizeof (struct nfscldeleg) + newfhlen,
567 M_NFSCLDELEG, M_WAITOK);
568 LIST_INIT(&ndp->nfsdl_owner);
569 LIST_INIT(&ndp->nfsdl_lock);
570 ndp->nfsdl_clp = op->nfso_own->nfsow_clp;
571 ndp->nfsdl_fhlen = newfhlen;
572 NFSBCOPY(newfhp, ndp->nfsdl_fh, newfhlen);
573 newnfs_copyincred(cred, &ndp->nfsdl_cred);
574 nfscl_lockinit(&ndp->nfsdl_rwlock);
575 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
577 ndp->nfsdl_stateid.seqid = *tl++;
578 ndp->nfsdl_stateid.other[0] = *tl++;
579 ndp->nfsdl_stateid.other[1] = *tl++;
580 ndp->nfsdl_stateid.other[2] = *tl++;
581 ret = fxdr_unsigned(int, *tl);
582 if (deleg == NFSV4OPEN_DELEGATEWRITE) {
583 ndp->nfsdl_flags = NFSCLDL_WRITE;
585 * Indicates how much the file can grow.
587 NFSM_DISSECT(tl, u_int32_t *,
589 limitby = fxdr_unsigned(int, *tl++);
591 case NFSV4OPEN_LIMITSIZE:
592 ndp->nfsdl_sizelimit = fxdr_hyper(tl);
594 case NFSV4OPEN_LIMITBLOCKS:
595 ndp->nfsdl_sizelimit =
596 fxdr_unsigned(u_int64_t, *tl++);
597 ndp->nfsdl_sizelimit *=
598 fxdr_unsigned(u_int64_t, *tl);
601 error = NFSERR_BADXDR;
605 ndp->nfsdl_flags = NFSCLDL_READ;
608 ndp->nfsdl_flags |= NFSCLDL_RECALL;
609 error = nfsrv_dissectace(nd, &ndp->nfsdl_ace, &ret,
613 } else if (deleg != NFSV4OPEN_DELEGATENONE) {
614 error = NFSERR_BADXDR;
617 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
618 error = nfsv4_loadattr(nd, NULL, &nfsva, NULL,
619 NULL, 0, NULL, NULL, NULL, NULL, NULL, 0,
620 NULL, NULL, NULL, p, cred);
624 ndp->nfsdl_change = nfsva.na_filerev;
625 ndp->nfsdl_modtime = nfsva.na_mtime;
626 ndp->nfsdl_flags |= NFSCLDL_MODTIMESET;
628 if (!reclaim && (rflags & NFSV4OPEN_RESULTCONFIRM)) {
630 ret = nfsrpc_openconfirm(vp, newfhp, newfhlen, op,
632 if (ret == NFSERR_DELAY)
633 (void) nfs_catnap(PZERO, ret, "nfs_open");
634 } while (ret == NFSERR_DELAY);
637 if ((rflags & NFSV4OPEN_LOCKTYPEPOSIX) ||
638 nfscl_assumeposixlocks)
639 op->nfso_posixlock = 1;
641 op->nfso_posixlock = 0;
644 * If the server is handing out delegations, but we didn't
645 * get one because an OpenConfirm was required, try the
646 * Open again, to get a delegation. This is a harmless no-op,
647 * from a server's point of view.
649 if (!reclaim && (rflags & NFSV4OPEN_RESULTCONFIRM) &&
650 (op->nfso_own->nfsow_clp->nfsc_flags & NFSCLFLAGS_GOTDELEG)
651 && !error && dp == NULL && ndp == NULL && !recursed) {
653 ret = nfsrpc_openrpc(nmp, vp, nfhp, fhlen, newfhp,
654 newfhlen, mode, op, name, namelen, &ndp, 0, 0x0,
655 cred, p, syscred, 1);
656 if (ret == NFSERR_DELAY)
657 (void) nfs_catnap(PZERO, ret, "nfs_open2");
658 } while (ret == NFSERR_DELAY);
661 free(ndp, M_NFSCLDELEG);
664 if (ret == NFSERR_STALECLIENTID ||
665 ret == NFSERR_STALEDONTRECOVER ||
666 ret == NFSERR_BADSESSION)
671 if (nd->nd_repstat != 0 && error == 0)
672 error = nd->nd_repstat;
673 if (error == NFSERR_STALECLIENTID)
674 nfscl_initiate_recovery(op->nfso_own->nfsow_clp);
678 else if (ndp != NULL)
679 free(ndp, M_NFSCLDELEG);
680 m_freem(nd->nd_mrep);
688 nfsrpc_opendowngrade(vnode_t vp, u_int32_t mode, struct nfsclopen *op,
689 struct ucred *cred, NFSPROC_T *p)
692 struct nfsrv_descript nfsd, *nd = &nfsd;
695 NFSCL_REQSTART(nd, NFSPROC_OPENDOWNGRADE, vp);
696 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID + 3 * NFSX_UNSIGNED);
697 if (NFSHASNFSV4N(VFSTONFS(vnode_mount(vp))))
700 *tl++ = op->nfso_stateid.seqid;
701 *tl++ = op->nfso_stateid.other[0];
702 *tl++ = op->nfso_stateid.other[1];
703 *tl++ = op->nfso_stateid.other[2];
704 *tl++ = txdr_unsigned(op->nfso_own->nfsow_seqid);
705 *tl++ = txdr_unsigned(mode & NFSV4OPEN_ACCESSBOTH);
706 *tl = txdr_unsigned((mode >> NFSLCK_SHIFT) & NFSV4OPEN_DENYBOTH);
707 error = nfscl_request(nd, vp, p, cred, NULL);
710 NFSCL_INCRSEQID(op->nfso_own->nfsow_seqid, nd);
711 if (!nd->nd_repstat) {
712 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID);
713 op->nfso_stateid.seqid = *tl++;
714 op->nfso_stateid.other[0] = *tl++;
715 op->nfso_stateid.other[1] = *tl++;
716 op->nfso_stateid.other[2] = *tl;
718 if (nd->nd_repstat && error == 0)
719 error = nd->nd_repstat;
720 if (error == NFSERR_STALESTATEID)
721 nfscl_initiate_recovery(op->nfso_own->nfsow_clp);
723 m_freem(nd->nd_mrep);
728 * V4 Close operation.
731 nfsrpc_close(vnode_t vp, int doclose, NFSPROC_T *p)
733 struct nfsclclient *clp;
736 if (vnode_vtype(vp) != VREG)
739 error = nfscl_doclose(vp, &clp, p);
741 error = nfscl_getclose(vp, &clp);
745 nfscl_clientrelease(clp);
753 nfsrpc_doclose(struct nfsmount *nmp, struct nfsclopen *op, NFSPROC_T *p)
755 struct nfsrv_descript nfsd, *nd = &nfsd;
756 struct nfscllockowner *lp, *nlp;
757 struct nfscllock *lop, *nlop;
759 u_int64_t off = 0, len = 0;
760 u_int32_t type = NFSV4LOCKT_READ;
761 int error, do_unlock, trycnt;
763 tcred = newnfs_getcred();
764 newnfs_copycred(&op->nfso_cred, tcred);
766 * (Theoretically this could be done in the same
767 * compound as the close, but having multiple
768 * sequenced Ops in the same compound might be
769 * too scary for some servers.)
771 if (op->nfso_posixlock) {
774 type = NFSV4LOCKT_READ;
778 * Since this function is only called from VOP_INACTIVE(), no
779 * other thread will be manipulating this Open. As such, the
780 * lock lists are not being changed by other threads, so it should
781 * be safe to do this without locking.
783 LIST_FOREACH(lp, &op->nfso_lock, nfsl_list) {
785 LIST_FOREACH_SAFE(lop, &lp->nfsl_lock, nfslo_list, nlop) {
786 if (op->nfso_posixlock == 0) {
787 off = lop->nfslo_first;
788 len = lop->nfslo_end - lop->nfslo_first;
789 if (lop->nfslo_type == F_WRLCK)
790 type = NFSV4LOCKT_WRITE;
792 type = NFSV4LOCKT_READ;
797 error = nfsrpc_locku(nd, nmp, lp, off,
798 len, type, tcred, p, 0);
799 if ((nd->nd_repstat == NFSERR_GRACE ||
800 nd->nd_repstat == NFSERR_DELAY) &&
802 (void) nfs_catnap(PZERO,
805 } while ((nd->nd_repstat == NFSERR_GRACE ||
806 nd->nd_repstat == NFSERR_DELAY) &&
807 error == 0 && trycnt++ < 5);
808 if (op->nfso_posixlock)
811 nfscl_freelock(lop, 0);
814 * Do a ReleaseLockOwner.
815 * The lock owner name nfsl_owner may be used by other opens for
816 * other files but the lock_owner4 name that nfsrpc_rellockown()
817 * puts on the wire has the file handle for this file appended
818 * to it, so it can be done now.
820 (void)nfsrpc_rellockown(nmp, lp, lp->nfsl_open->nfso_fh,
821 lp->nfsl_open->nfso_fhlen, tcred, p);
825 * There could be other Opens for different files on the same
826 * OpenOwner, so locking is required.
829 nfscl_lockexcl(&op->nfso_own->nfsow_rwlock, NFSCLSTATEMUTEXPTR);
832 error = nfscl_tryclose(op, tcred, nmp, p);
833 if (error == NFSERR_GRACE)
834 (void) nfs_catnap(PZERO, error, "nfs_close");
835 } while (error == NFSERR_GRACE);
837 nfscl_lockunlock(&op->nfso_own->nfsow_rwlock);
839 LIST_FOREACH_SAFE(lp, &op->nfso_lock, nfsl_list, nlp)
840 nfscl_freelockowner(lp, 0);
841 nfscl_freeopen(op, 0);
847 * The actual Close RPC.
850 nfsrpc_closerpc(struct nfsrv_descript *nd, struct nfsmount *nmp,
851 struct nfsclopen *op, struct ucred *cred, NFSPROC_T *p,
857 nfscl_reqstart(nd, NFSPROC_CLOSE, nmp, op->nfso_fh,
858 op->nfso_fhlen, NULL, NULL, 0, 0);
859 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED + NFSX_STATEID);
860 *tl++ = txdr_unsigned(op->nfso_own->nfsow_seqid);
861 if (NFSHASNFSV4N(nmp))
864 *tl++ = op->nfso_stateid.seqid;
865 *tl++ = op->nfso_stateid.other[0];
866 *tl++ = op->nfso_stateid.other[1];
867 *tl = op->nfso_stateid.other[2];
869 nd->nd_flag |= ND_USEGSSNAME;
870 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
871 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
874 NFSCL_INCRSEQID(op->nfso_own->nfsow_seqid, nd);
875 if (nd->nd_repstat == 0)
876 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID);
877 error = nd->nd_repstat;
878 if (error == NFSERR_STALESTATEID)
879 nfscl_initiate_recovery(op->nfso_own->nfsow_clp);
881 m_freem(nd->nd_mrep);
886 * V4 Open Confirm RPC.
889 nfsrpc_openconfirm(vnode_t vp, u_int8_t *nfhp, int fhlen,
890 struct nfsclopen *op, struct ucred *cred, NFSPROC_T *p)
893 struct nfsrv_descript nfsd, *nd = &nfsd;
894 struct nfsmount *nmp;
897 nmp = VFSTONFS(vnode_mount(vp));
898 if (NFSHASNFSV4N(nmp))
899 return (0); /* No confirmation for NFSv4.1. */
900 nfscl_reqstart(nd, NFSPROC_OPENCONFIRM, nmp, nfhp, fhlen, NULL, NULL,
902 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED + NFSX_STATEID);
903 *tl++ = op->nfso_stateid.seqid;
904 *tl++ = op->nfso_stateid.other[0];
905 *tl++ = op->nfso_stateid.other[1];
906 *tl++ = op->nfso_stateid.other[2];
907 *tl = txdr_unsigned(op->nfso_own->nfsow_seqid);
908 error = nfscl_request(nd, vp, p, cred, NULL);
911 NFSCL_INCRSEQID(op->nfso_own->nfsow_seqid, nd);
912 if (!nd->nd_repstat) {
913 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID);
914 op->nfso_stateid.seqid = *tl++;
915 op->nfso_stateid.other[0] = *tl++;
916 op->nfso_stateid.other[1] = *tl++;
917 op->nfso_stateid.other[2] = *tl;
919 error = nd->nd_repstat;
920 if (error == NFSERR_STALESTATEID)
921 nfscl_initiate_recovery(op->nfso_own->nfsow_clp);
923 m_freem(nd->nd_mrep);
928 * Do the setclientid and setclientid confirm RPCs. Called from nfs_statfs()
929 * when a mount has just occurred and when the server replies NFSERR_EXPIRED.
932 nfsrpc_setclient(struct nfsmount *nmp, struct nfsclclient *clp, int reclaim,
933 bool *retokp, struct ucred *cred, NFSPROC_T *p)
936 struct nfsrv_descript nfsd;
937 struct nfsrv_descript *nd = &nfsd;
938 nfsattrbit_t attrbits;
939 u_int8_t *cp = NULL, *cp2, addr[INET6_ADDRSTRLEN + 9];
941 int error, isinet6 = 0, callblen;
944 static u_int32_t rev = 0;
945 struct nfsclds *dsp, *odsp;
947 struct nfsclsession *tsep;
949 if (nfsboottime.tv_sec == 0)
950 NFSSETBOOTTIME(nfsboottime);
951 if (NFSHASNFSV4N(nmp)) {
952 error = NFSERR_BADSESSION;
954 if (retokp != NULL) {
956 odsp = TAILQ_FIRST(&nmp->nm_sess);
961 * When a session already exists, first try a
962 * CreateSession with the extant ClientID.
964 dsp = malloc(sizeof(struct nfsclds) +
965 odsp->nfsclds_servownlen + 1, M_NFSCLDS,
967 dsp->nfsclds_expire = NFSD_MONOSEC + clp->nfsc_renew;
968 dsp->nfsclds_servownlen = odsp->nfsclds_servownlen;
969 dsp->nfsclds_sess.nfsess_clientid =
970 odsp->nfsclds_sess.nfsess_clientid;
971 dsp->nfsclds_sess.nfsess_sequenceid =
972 odsp->nfsclds_sess.nfsess_sequenceid;
973 dsp->nfsclds_flags = odsp->nfsclds_flags;
974 if (dsp->nfsclds_servownlen > 0)
975 memcpy(dsp->nfsclds_serverown,
976 odsp->nfsclds_serverown,
977 dsp->nfsclds_servownlen + 1);
978 mtx_init(&dsp->nfsclds_mtx, "nfsds", NULL, MTX_DEF);
979 mtx_init(&dsp->nfsclds_sess.nfsess_mtx, "nfssession",
981 nfscl_initsessionslots(&dsp->nfsclds_sess);
982 error = nfsrpc_createsession(nmp, &dsp->nfsclds_sess,
983 &nmp->nm_sockreq, NULL,
984 dsp->nfsclds_sess.nfsess_sequenceid, 1, cred, p);
985 NFSCL_DEBUG(1, "create session for extant "
986 "ClientID=%d\n", error);
988 nfscl_freenfsclds(dsp);
991 * If *retokp is true, return any error other
992 * than NFSERR_STALECLIENTID,
993 * NFSERR_BADSESSION or NFSERR_STALEDONTRECOVER
994 * so that nfscl_recover() will not loop.
1000 } else if (retokp != NULL && *retokp)
1004 * Either there was no previous session or the
1005 * CreateSession attempt failed, so...
1006 * do an ExchangeID followed by the CreateSession.
1008 clp->nfsc_rev = rev++;
1009 error = nfsrpc_exchangeid(nmp, clp, &nmp->nm_sockreq, 0,
1010 NFSV4EXCH_USEPNFSMDS | NFSV4EXCH_USENONPNFS, &dsp,
1012 NFSCL_DEBUG(1, "aft exch=%d\n", error);
1014 error = nfsrpc_createsession(nmp,
1015 &dsp->nfsclds_sess, &nmp->nm_sockreq, NULL,
1016 dsp->nfsclds_sess.nfsess_sequenceid, 1,
1018 NFSCL_DEBUG(1, "aft createsess=%d\n", error);
1023 * The old sessions cannot be safely free'd
1024 * here, since they may still be used by
1028 if (TAILQ_FIRST(&nmp->nm_sess) != NULL)
1029 tsep = NFSMNT_MDSSESSION(nmp);
1030 TAILQ_INSERT_HEAD(&nmp->nm_sess, dsp,
1033 * Wake up RPCs waiting for a slot on the
1034 * old session. These will then fail with
1035 * NFSERR_BADSESSION and be retried with the
1036 * new session by nfsv4_setsequence().
1037 * Also wakeup() processes waiting for the
1041 wakeup(&tsep->nfsess_slots);
1042 wakeup(&nmp->nm_sess);
1044 } else if (dsp != NULL)
1045 nfscl_freenfsclds(dsp);
1046 if (error == 0 && reclaim == 0) {
1047 error = nfsrpc_reclaimcomplete(nmp, cred, p);
1048 NFSCL_DEBUG(1, "aft reclaimcomp=%d\n", error);
1049 if (error == NFSERR_COMPLETEALREADY ||
1050 error == NFSERR_NOTSUPP)
1051 /* Ignore this error. */
1055 } else if (retokp != NULL && *retokp)
1057 clp->nfsc_rev = rev++;
1060 * Allocate a single session structure for NFSv4.0, because some of
1061 * the fields are used by NFSv4.0 although it doesn't do a session.
1063 dsp = malloc(sizeof(struct nfsclds), M_NFSCLDS, M_WAITOK | M_ZERO);
1064 mtx_init(&dsp->nfsclds_mtx, "nfsds", NULL, MTX_DEF);
1065 mtx_init(&dsp->nfsclds_sess.nfsess_mtx, "nfssession", NULL, MTX_DEF);
1067 TAILQ_INSERT_HEAD(&nmp->nm_sess, dsp, nfsclds_list);
1068 tsep = NFSMNT_MDSSESSION(nmp);
1071 nfscl_reqstart(nd, NFSPROC_SETCLIENTID, nmp, NULL, 0, NULL, NULL, 0, 0);
1072 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1073 *tl++ = txdr_unsigned(nfsboottime.tv_sec);
1074 *tl = txdr_unsigned(clp->nfsc_rev);
1075 (void) nfsm_strtom(nd, clp->nfsc_id, clp->nfsc_idlen);
1078 * set up the callback address
1080 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1081 *tl = txdr_unsigned(NFS_CALLBCKPROG);
1082 callblen = strlen(nfsv4_callbackaddr);
1084 cp = nfscl_getmyip(nmp, &a6, &isinet6);
1085 if (nfscl_enablecallb && nfs_numnfscbd > 0 &&
1086 (callblen > 0 || cp != NULL)) {
1087 port = htons(nfsv4_cbport);
1088 cp2 = (u_int8_t *)&port;
1090 if ((callblen > 0 &&
1091 strchr(nfsv4_callbackaddr, ':')) || isinet6) {
1092 char ip6buf[INET6_ADDRSTRLEN], *ip6add;
1094 (void) nfsm_strtom(nd, "tcp6", 4);
1095 if (callblen == 0) {
1096 ip6_sprintf(ip6buf, (struct in6_addr *)cp);
1099 ip6add = nfsv4_callbackaddr;
1101 snprintf(addr, INET6_ADDRSTRLEN + 9, "%s.%d.%d",
1102 ip6add, cp2[0], cp2[1]);
1106 (void) nfsm_strtom(nd, "tcp", 3);
1108 snprintf(addr, INET6_ADDRSTRLEN + 9,
1109 "%d.%d.%d.%d.%d.%d", cp[0], cp[1],
1110 cp[2], cp[3], cp2[0], cp2[1]);
1112 snprintf(addr, INET6_ADDRSTRLEN + 9,
1113 "%s.%d.%d", nfsv4_callbackaddr,
1116 (void) nfsm_strtom(nd, addr, strlen(addr));
1118 (void) nfsm_strtom(nd, "tcp", 3);
1119 (void) nfsm_strtom(nd, "0.0.0.0.0.0", 11);
1121 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1122 *tl = txdr_unsigned(clp->nfsc_cbident);
1123 nd->nd_flag |= ND_USEGSSNAME;
1124 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
1125 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
1128 if (nd->nd_repstat == 0) {
1129 NFSM_DISSECT(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1130 tsep->nfsess_clientid.lval[0] = *tl++;
1131 tsep->nfsess_clientid.lval[1] = *tl++;
1132 confirm.lval[0] = *tl++;
1133 confirm.lval[1] = *tl;
1134 m_freem(nd->nd_mrep);
1140 nfscl_reqstart(nd, NFSPROC_SETCLIENTIDCFRM, nmp, NULL, 0, NULL,
1142 NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1143 *tl++ = tsep->nfsess_clientid.lval[0];
1144 *tl++ = tsep->nfsess_clientid.lval[1];
1145 *tl++ = confirm.lval[0];
1146 *tl = confirm.lval[1];
1147 nd->nd_flag |= ND_USEGSSNAME;
1148 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p,
1149 cred, NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
1152 m_freem(nd->nd_mrep);
1154 if (nd->nd_repstat == 0) {
1155 nfscl_reqstart(nd, NFSPROC_GETATTR, nmp, nmp->nm_fh,
1156 nmp->nm_fhsize, NULL, NULL, 0, 0);
1157 NFSZERO_ATTRBIT(&attrbits);
1158 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_LEASETIME);
1159 (void) nfsrv_putattrbit(nd, &attrbits);
1160 nd->nd_flag |= ND_USEGSSNAME;
1161 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p,
1162 cred, NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
1165 if (nd->nd_repstat == 0) {
1166 error = nfsv4_loadattr(nd, NULL, NULL, NULL, NULL, 0, NULL,
1167 NULL, NULL, NULL, NULL, 0, NULL, &lease, NULL, p, cred);
1170 clp->nfsc_renew = NFSCL_RENEW(lease);
1171 clp->nfsc_expire = NFSD_MONOSEC + clp->nfsc_renew;
1172 clp->nfsc_clientidrev++;
1173 if (clp->nfsc_clientidrev == 0)
1174 clp->nfsc_clientidrev++;
1178 error = nd->nd_repstat;
1180 m_freem(nd->nd_mrep);
1188 nfsrpc_getattr(vnode_t vp, struct ucred *cred, NFSPROC_T *p,
1189 struct nfsvattr *nap, void *stuff)
1191 struct nfsrv_descript nfsd, *nd = &nfsd;
1193 nfsattrbit_t attrbits;
1195 NFSCL_REQSTART(nd, NFSPROC_GETATTR, vp);
1196 if (nd->nd_flag & ND_NFSV4) {
1197 NFSGETATTR_ATTRBIT(&attrbits);
1198 (void) nfsrv_putattrbit(nd, &attrbits);
1200 error = nfscl_request(nd, vp, p, cred, stuff);
1203 if (!nd->nd_repstat)
1204 error = nfsm_loadattr(nd, nap);
1206 error = nd->nd_repstat;
1207 m_freem(nd->nd_mrep);
1212 * nfs getattr call with non-vnode arguemnts.
1215 nfsrpc_getattrnovp(struct nfsmount *nmp, u_int8_t *fhp, int fhlen, int syscred,
1216 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, u_int64_t *xidp,
1219 struct nfsrv_descript nfsd, *nd = &nfsd;
1220 int error, vers = NFS_VER2;
1221 nfsattrbit_t attrbits;
1223 nfscl_reqstart(nd, NFSPROC_GETATTR, nmp, fhp, fhlen, NULL, NULL, 0, 0);
1224 if (nd->nd_flag & ND_NFSV4) {
1226 NFSGETATTR_ATTRBIT(&attrbits);
1227 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_LEASETIME);
1228 (void) nfsrv_putattrbit(nd, &attrbits);
1229 } else if (nd->nd_flag & ND_NFSV3) {
1233 nd->nd_flag |= ND_USEGSSNAME;
1234 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
1235 NFS_PROG, vers, NULL, 1, xidp, NULL);
1238 if (nd->nd_repstat == 0) {
1239 if ((nd->nd_flag & ND_NFSV4) != 0)
1240 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0,
1241 NULL, NULL, NULL, NULL, NULL, 0, NULL, leasep, NULL,
1244 error = nfsm_loadattr(nd, nap);
1246 error = nd->nd_repstat;
1247 m_freem(nd->nd_mrep);
1252 * Do an nfs setattr operation.
1255 nfsrpc_setattr(vnode_t vp, struct vattr *vap, NFSACL_T *aclp,
1256 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *rnap, int *attrflagp,
1259 int error, expireret = 0, openerr, retrycnt;
1260 u_int32_t clidrev = 0, mode;
1261 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
1263 nfsv4stateid_t stateid;
1266 if (nmp->nm_clp != NULL)
1267 clidrev = nmp->nm_clp->nfsc_clientidrev;
1268 if (vap != NULL && NFSATTRISSET(u_quad_t, vap, va_size))
1269 mode = NFSV4OPEN_ACCESSWRITE;
1271 mode = NFSV4OPEN_ACCESSREAD;
1276 if (NFSHASNFSV4(nmp)) {
1277 nfhp = VTONFS(vp)->n_fhp;
1278 error = nfscl_getstateid(vp, nfhp->nfh_fh,
1279 nfhp->nfh_len, mode, 0, cred, p, &stateid, &lckp);
1280 if (error && vnode_vtype(vp) == VREG &&
1281 (mode == NFSV4OPEN_ACCESSWRITE ||
1282 nfstest_openallsetattr)) {
1284 * No Open stateid, so try and open the file
1287 if (mode == NFSV4OPEN_ACCESSWRITE)
1288 openerr = nfsrpc_open(vp, FWRITE, cred,
1291 openerr = nfsrpc_open(vp, FREAD, cred,
1294 (void) nfscl_getstateid(vp,
1295 nfhp->nfh_fh, nfhp->nfh_len,
1296 mode, 0, cred, p, &stateid, &lckp);
1300 error = nfsrpc_setattrrpc(vp, vap, &stateid, cred, p,
1301 rnap, attrflagp, stuff);
1303 error = nfsrpc_setaclrpc(vp, cred, p, aclp, &stateid,
1305 if (error == NFSERR_OPENMODE && mode == NFSV4OPEN_ACCESSREAD) {
1307 nmp->nm_state |= NFSSTA_OPENMODE;
1310 if (error == NFSERR_STALESTATEID)
1311 nfscl_initiate_recovery(nmp->nm_clp);
1313 nfscl_lockderef(lckp);
1315 (void) nfsrpc_close(vp, 0, p);
1316 if (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
1317 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
1318 error == NFSERR_OLDSTATEID || error == NFSERR_BADSESSION) {
1319 (void) nfs_catnap(PZERO, error, "nfs_setattr");
1320 } else if ((error == NFSERR_EXPIRED ||
1321 error == NFSERR_BADSTATEID) && clidrev != 0) {
1322 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
1325 } while (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
1326 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
1327 error == NFSERR_BADSESSION ||
1328 (error == NFSERR_OLDSTATEID && retrycnt < 20) ||
1329 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
1330 expireret == 0 && clidrev != 0 && retrycnt < 4) ||
1331 (error == NFSERR_OPENMODE && mode == NFSV4OPEN_ACCESSREAD &&
1333 if (error && retrycnt >= 4)
1339 nfsrpc_setattrrpc(vnode_t vp, struct vattr *vap,
1340 nfsv4stateid_t *stateidp, struct ucred *cred, NFSPROC_T *p,
1341 struct nfsvattr *rnap, int *attrflagp, void *stuff)
1344 struct nfsrv_descript nfsd, *nd = &nfsd;
1346 nfsattrbit_t attrbits;
1349 NFSCL_REQSTART(nd, NFSPROC_SETATTR, vp);
1350 if (nd->nd_flag & ND_NFSV4)
1351 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID);
1352 vap->va_type = vnode_vtype(vp);
1353 nfscl_fillsattr(nd, vap, vp, NFSSATTR_FULL, 0);
1354 if (nd->nd_flag & ND_NFSV3) {
1355 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1357 } else if (nd->nd_flag & ND_NFSV4) {
1358 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1359 *tl = txdr_unsigned(NFSV4OP_GETATTR);
1360 NFSGETATTR_ATTRBIT(&attrbits);
1361 (void) nfsrv_putattrbit(nd, &attrbits);
1363 error = nfscl_request(nd, vp, p, cred, stuff);
1366 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4))
1367 error = nfscl_wcc_data(nd, vp, rnap, attrflagp, NULL, stuff);
1368 if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) == ND_NFSV4 && !error)
1369 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
1370 if (!(nd->nd_flag & ND_NFSV3) && !nd->nd_repstat && !error)
1371 error = nfscl_postop_attr(nd, rnap, attrflagp, stuff);
1372 m_freem(nd->nd_mrep);
1373 if (nd->nd_repstat && !error)
1374 error = nd->nd_repstat;
1382 nfsrpc_lookup(vnode_t dvp, char *name, int len, struct ucred *cred,
1383 NFSPROC_T *p, struct nfsvattr *dnap, struct nfsvattr *nap,
1384 struct nfsfh **nfhpp, int *attrflagp, int *dattrflagp, void *stuff)
1387 struct nfsrv_descript nfsd, *nd = &nfsd;
1388 struct nfsmount *nmp;
1391 nfsattrbit_t attrbits;
1392 int error = 0, lookupp = 0;
1396 if (vnode_vtype(dvp) != VDIR)
1398 nmp = VFSTONFS(vnode_mount(dvp));
1399 if (len > NFS_MAXNAMLEN)
1400 return (ENAMETOOLONG);
1401 if (NFSHASNFSV4(nmp) && len == 1 &&
1404 * Just return the current dir's fh.
1407 nfhp = malloc(sizeof (struct nfsfh) +
1408 np->n_fhp->nfh_len, M_NFSFH, M_WAITOK);
1409 nfhp->nfh_len = np->n_fhp->nfh_len;
1410 NFSBCOPY(np->n_fhp->nfh_fh, nfhp->nfh_fh, nfhp->nfh_len);
1414 if (NFSHASNFSV4(nmp) && len == 2 &&
1415 name[0] == '.' && name[1] == '.') {
1417 NFSCL_REQSTART(nd, NFSPROC_LOOKUPP, dvp);
1419 NFSCL_REQSTART(nd, NFSPROC_LOOKUP, dvp);
1420 (void) nfsm_strtom(nd, name, len);
1422 if (nd->nd_flag & ND_NFSV4) {
1423 NFSGETATTR_ATTRBIT(&attrbits);
1424 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1425 *tl++ = txdr_unsigned(NFSV4OP_GETFH);
1426 *tl = txdr_unsigned(NFSV4OP_GETATTR);
1427 (void) nfsrv_putattrbit(nd, &attrbits);
1429 error = nfscl_request(nd, dvp, p, cred, stuff);
1432 if (nd->nd_repstat) {
1434 * When an NFSv4 Lookupp returns ENOENT, it means that
1435 * the lookup is at the root of an fs, so return this dir.
1437 if (nd->nd_repstat == NFSERR_NOENT && lookupp) {
1439 nfhp = malloc(sizeof (struct nfsfh) +
1440 np->n_fhp->nfh_len, M_NFSFH, M_WAITOK);
1441 nfhp->nfh_len = np->n_fhp->nfh_len;
1442 NFSBCOPY(np->n_fhp->nfh_fh, nfhp->nfh_fh, nfhp->nfh_len);
1444 m_freem(nd->nd_mrep);
1447 if (nd->nd_flag & ND_NFSV3)
1448 error = nfscl_postop_attr(nd, dnap, dattrflagp, stuff);
1449 else if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) ==
1451 /* Load the directory attributes. */
1452 error = nfsm_loadattr(nd, dnap);
1458 if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) == ND_NFSV4) {
1459 /* Load the directory attributes. */
1460 error = nfsm_loadattr(nd, dnap);
1464 /* Skip over the Lookup and GetFH operation status values. */
1465 NFSM_DISSECT(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1467 error = nfsm_getfh(nd, nfhpp);
1471 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
1472 if ((nd->nd_flag & ND_NFSV3) && !error)
1473 error = nfscl_postop_attr(nd, dnap, dattrflagp, stuff);
1475 m_freem(nd->nd_mrep);
1476 if (!error && nd->nd_repstat)
1477 error = nd->nd_repstat;
1482 * Do a readlink rpc.
1485 nfsrpc_readlink(vnode_t vp, struct uio *uiop, struct ucred *cred,
1486 NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, void *stuff)
1489 struct nfsrv_descript nfsd, *nd = &nfsd;
1490 struct nfsnode *np = VTONFS(vp);
1491 nfsattrbit_t attrbits;
1492 int error, len, cangetattr = 1;
1495 NFSCL_REQSTART(nd, NFSPROC_READLINK, vp);
1496 if (nd->nd_flag & ND_NFSV4) {
1498 * And do a Getattr op.
1500 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1501 *tl = txdr_unsigned(NFSV4OP_GETATTR);
1502 NFSGETATTR_ATTRBIT(&attrbits);
1503 (void) nfsrv_putattrbit(nd, &attrbits);
1505 error = nfscl_request(nd, vp, p, cred, stuff);
1508 if (nd->nd_flag & ND_NFSV3)
1509 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
1510 if (!nd->nd_repstat && !error) {
1511 NFSM_STRSIZ(len, NFS_MAXPATHLEN);
1513 * This seems weird to me, but must have been added to
1514 * FreeBSD for some reason. The only thing I can think of
1515 * is that there was/is some server that replies with
1516 * more link data than it should?
1518 if (len == NFS_MAXPATHLEN) {
1520 if (np->n_size > 0 && np->n_size < NFS_MAXPATHLEN) {
1526 error = nfsm_mbufuio(nd, uiop, len);
1527 if ((nd->nd_flag & ND_NFSV4) && !error && cangetattr)
1528 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
1530 if (nd->nd_repstat && !error)
1531 error = nd->nd_repstat;
1533 m_freem(nd->nd_mrep);
1541 nfsrpc_read(vnode_t vp, struct uio *uiop, struct ucred *cred,
1542 NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, void *stuff)
1544 int error, expireret = 0, retrycnt;
1545 u_int32_t clidrev = 0;
1546 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
1547 struct nfsnode *np = VTONFS(vp);
1548 struct ucred *newcred;
1549 struct nfsfh *nfhp = NULL;
1550 nfsv4stateid_t stateid;
1553 if (nmp->nm_clp != NULL)
1554 clidrev = nmp->nm_clp->nfsc_clientidrev;
1556 if (NFSHASNFSV4(nmp)) {
1558 newcred = NFSNEWCRED(cred);
1563 if (NFSHASNFSV4(nmp))
1564 (void)nfscl_getstateid(vp, nfhp->nfh_fh, nfhp->nfh_len,
1565 NFSV4OPEN_ACCESSREAD, 0, newcred, p, &stateid,
1567 error = nfsrpc_readrpc(vp, uiop, newcred, &stateid, p, nap,
1569 if (error == NFSERR_OPENMODE) {
1571 nmp->nm_state |= NFSSTA_OPENMODE;
1574 if (error == NFSERR_STALESTATEID)
1575 nfscl_initiate_recovery(nmp->nm_clp);
1577 nfscl_lockderef(lckp);
1578 if (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
1579 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
1580 error == NFSERR_OLDSTATEID || error == NFSERR_BADSESSION) {
1581 (void) nfs_catnap(PZERO, error, "nfs_read");
1582 } else if ((error == NFSERR_EXPIRED ||
1583 error == NFSERR_BADSTATEID) && clidrev != 0) {
1584 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
1587 } while (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
1588 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
1589 error == NFSERR_BADSESSION ||
1590 (error == NFSERR_OLDSTATEID && retrycnt < 20) ||
1591 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
1592 expireret == 0 && clidrev != 0 && retrycnt < 4) ||
1593 (error == NFSERR_OPENMODE && retrycnt < 4));
1594 if (error && retrycnt >= 4)
1596 if (NFSHASNFSV4(nmp))
1597 NFSFREECRED(newcred);
1602 * The actual read RPC.
1605 nfsrpc_readrpc(vnode_t vp, struct uio *uiop, struct ucred *cred,
1606 nfsv4stateid_t *stateidp, NFSPROC_T *p, struct nfsvattr *nap,
1607 int *attrflagp, void *stuff)
1610 int error = 0, len, retlen, tsiz, eof = 0;
1611 struct nfsrv_descript nfsd;
1612 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
1613 struct nfsrv_descript *nd = &nfsd;
1618 tsiz = uiop->uio_resid;
1619 tmp_off = uiop->uio_offset + tsiz;
1621 if (tmp_off > nmp->nm_maxfilesize || tmp_off < uiop->uio_offset) {
1625 rsize = nmp->nm_rsize;
1630 len = (tsiz > rsize) ? rsize : tsiz;
1631 NFSCL_REQSTART(nd, NFSPROC_READ, vp);
1632 if (nd->nd_flag & ND_NFSV4)
1633 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID);
1634 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED * 3);
1635 if (nd->nd_flag & ND_NFSV2) {
1636 *tl++ = txdr_unsigned(uiop->uio_offset);
1637 *tl++ = txdr_unsigned(len);
1640 txdr_hyper(uiop->uio_offset, tl);
1641 *(tl + 2) = txdr_unsigned(len);
1644 * Since I can't do a Getattr for NFSv4 for Write, there
1645 * doesn't seem any point in doing one here, either.
1646 * (See the comment in nfsrpc_writerpc() for more info.)
1648 error = nfscl_request(nd, vp, p, cred, stuff);
1651 if (nd->nd_flag & ND_NFSV3) {
1652 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
1653 } else if (!nd->nd_repstat && (nd->nd_flag & ND_NFSV2)) {
1654 error = nfsm_loadattr(nd, nap);
1658 if (nd->nd_repstat || error) {
1660 error = nd->nd_repstat;
1663 if (nd->nd_flag & ND_NFSV3) {
1664 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1665 eof = fxdr_unsigned(int, *(tl + 1));
1666 } else if (nd->nd_flag & ND_NFSV4) {
1667 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
1668 eof = fxdr_unsigned(int, *tl);
1670 NFSM_STRSIZ(retlen, len);
1671 error = nfsm_mbufuio(nd, uiop, retlen);
1674 m_freem(nd->nd_mrep);
1677 if (!(nd->nd_flag & ND_NFSV2)) {
1678 if (eof || retlen == 0)
1680 } else if (retlen < len)
1685 if (nd->nd_mrep != NULL)
1686 m_freem(nd->nd_mrep);
1691 * nfs write operation
1692 * When called_from_strategy != 0, it should return EIO for an error that
1693 * indicates recovery is in progress, so that the buffer will be left
1694 * dirty and be written back to the server later. If it loops around,
1695 * the recovery thread could get stuck waiting for the buffer and recovery
1696 * will then deadlock.
1699 nfsrpc_write(vnode_t vp, struct uio *uiop, int *iomode, int *must_commit,
1700 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp,
1701 void *stuff, int called_from_strategy)
1703 int error, expireret = 0, retrycnt, nostateid;
1704 u_int32_t clidrev = 0;
1705 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
1706 struct nfsnode *np = VTONFS(vp);
1707 struct ucred *newcred;
1708 struct nfsfh *nfhp = NULL;
1709 nfsv4stateid_t stateid;
1713 if (nmp->nm_clp != NULL)
1714 clidrev = nmp->nm_clp->nfsc_clientidrev;
1716 if (NFSHASNFSV4(nmp)) {
1717 newcred = NFSNEWCRED(cred);
1724 if (NFSHASNFSV4(nmp)) {
1725 (void)nfscl_getstateid(vp, nfhp->nfh_fh, nfhp->nfh_len,
1726 NFSV4OPEN_ACCESSWRITE, 0, newcred, p, &stateid,
1728 if (stateid.other[0] == 0 && stateid.other[1] == 0 &&
1729 stateid.other[2] == 0) {
1731 NFSCL_DEBUG(1, "stateid0 in write\n");
1736 * If there is no stateid for NFSv4, it means this is an
1737 * extraneous write after close. Basically a poorly
1738 * implemented buffer cache. Just don't do the write.
1743 error = nfsrpc_writerpc(vp, uiop, iomode, must_commit,
1744 newcred, &stateid, p, nap, attrflagp, stuff);
1745 if (error == NFSERR_STALESTATEID)
1746 nfscl_initiate_recovery(nmp->nm_clp);
1748 nfscl_lockderef(lckp);
1749 if (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
1750 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
1751 error == NFSERR_OLDSTATEID || error == NFSERR_BADSESSION) {
1752 (void) nfs_catnap(PZERO, error, "nfs_write");
1753 } else if ((error == NFSERR_EXPIRED ||
1754 error == NFSERR_BADSTATEID) && clidrev != 0) {
1755 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
1758 } while (error == NFSERR_GRACE || error == NFSERR_DELAY ||
1759 ((error == NFSERR_STALESTATEID || error == NFSERR_BADSESSION ||
1760 error == NFSERR_STALEDONTRECOVER) && called_from_strategy == 0) ||
1761 (error == NFSERR_OLDSTATEID && retrycnt < 20) ||
1762 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
1763 expireret == 0 && clidrev != 0 && retrycnt < 4));
1764 if (error != 0 && (retrycnt >= 4 ||
1765 ((error == NFSERR_STALESTATEID || error == NFSERR_BADSESSION ||
1766 error == NFSERR_STALEDONTRECOVER) && called_from_strategy != 0)))
1768 if (NFSHASNFSV4(nmp))
1769 NFSFREECRED(newcred);
1774 * The actual write RPC.
1777 nfsrpc_writerpc(vnode_t vp, struct uio *uiop, int *iomode,
1778 int *must_commit, struct ucred *cred, nfsv4stateid_t *stateidp,
1779 NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, void *stuff)
1782 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
1783 struct nfsnode *np = VTONFS(vp);
1784 int error = 0, len, tsiz, rlen, commit, committed = NFSWRITE_FILESYNC;
1785 int wccflag = 0, wsize;
1787 struct nfsrv_descript nfsd;
1788 struct nfsrv_descript *nd = &nfsd;
1789 nfsattrbit_t attrbits;
1792 KASSERT(uiop->uio_iovcnt == 1, ("nfs: writerpc iovcnt > 1"));
1794 tsiz = uiop->uio_resid;
1795 tmp_off = uiop->uio_offset + tsiz;
1797 if (tmp_off > nmp->nm_maxfilesize || tmp_off < uiop->uio_offset) {
1801 wsize = nmp->nm_wsize;
1803 nd->nd_mrep = NULL; /* NFSv2 sometimes does a write with */
1804 nd->nd_repstat = 0; /* uio_resid == 0, so the while is not done */
1807 len = (tsiz > wsize) ? wsize : tsiz;
1808 NFSCL_REQSTART(nd, NFSPROC_WRITE, vp);
1809 if (nd->nd_flag & ND_NFSV4) {
1810 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID);
1811 NFSM_BUILD(tl, u_int32_t *, NFSX_HYPER+2*NFSX_UNSIGNED);
1812 txdr_hyper(uiop->uio_offset, tl);
1814 *tl++ = txdr_unsigned(*iomode);
1815 *tl = txdr_unsigned(len);
1816 } else if (nd->nd_flag & ND_NFSV3) {
1817 NFSM_BUILD(tl, u_int32_t *, NFSX_HYPER+3*NFSX_UNSIGNED);
1818 txdr_hyper(uiop->uio_offset, tl);
1820 *tl++ = txdr_unsigned(len);
1821 *tl++ = txdr_unsigned(*iomode);
1822 *tl = txdr_unsigned(len);
1826 NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1828 * Not sure why someone changed this, since the
1829 * RFC clearly states that "beginoffset" and
1830 * "totalcount" are ignored, but it wouldn't
1831 * surprise me if there's a busted server out there.
1833 /* Set both "begin" and "current" to non-garbage. */
1834 x = txdr_unsigned((u_int32_t)uiop->uio_offset);
1835 *tl++ = x; /* "begin offset" */
1836 *tl++ = x; /* "current offset" */
1837 x = txdr_unsigned(len);
1838 *tl++ = x; /* total to this offset */
1839 *tl = x; /* size of this write */
1842 nfsm_uiombuf(nd, uiop, len);
1844 * Although it is tempting to do a normal Getattr Op in the
1845 * NFSv4 compound, the result can be a nearly hung client
1846 * system if the Getattr asks for Owner and/or OwnerGroup.
1847 * It occurs when the client can't map either the Owner or
1848 * Owner_group name in the Getattr reply to a uid/gid. When
1849 * there is a cache miss, the kernel does an upcall to the
1850 * nfsuserd. Then, it can try and read the local /etc/passwd
1851 * or /etc/group file. It can then block in getnewbuf(),
1852 * waiting for dirty writes to be pushed to the NFS server.
1853 * The only reason this doesn't result in a complete
1854 * deadlock, is that the upcall times out and allows
1855 * the write to complete. However, progress is so slow
1856 * that it might just as well be deadlocked.
1857 * As such, we get the rest of the attributes, but not
1858 * Owner or Owner_group.
1859 * nb: nfscl_loadattrcache() needs to be told that these
1860 * partial attributes from a write rpc are being
1861 * passed in, via a argument flag.
1863 if (nd->nd_flag & ND_NFSV4) {
1864 NFSWRITEGETATTR_ATTRBIT(&attrbits);
1865 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1866 *tl = txdr_unsigned(NFSV4OP_GETATTR);
1867 (void) nfsrv_putattrbit(nd, &attrbits);
1869 error = nfscl_request(nd, vp, p, cred, stuff);
1872 if (nd->nd_repstat) {
1874 * In case the rpc gets retried, roll
1875 * the uio fileds changed by nfsm_uiombuf()
1878 uiop->uio_offset -= len;
1879 uiop->uio_resid += len;
1880 uiop->uio_iov->iov_base =
1881 (char *)uiop->uio_iov->iov_base - len;
1882 uiop->uio_iov->iov_len += len;
1884 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) {
1885 error = nfscl_wcc_data(nd, vp, nap, attrflagp,
1890 if (!nd->nd_repstat) {
1891 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) {
1892 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED
1894 rlen = fxdr_unsigned(int, *tl++);
1898 } else if (rlen < len) {
1899 backup = len - rlen;
1900 uiop->uio_iov->iov_base =
1901 (char *)uiop->uio_iov->iov_base -
1903 uiop->uio_iov->iov_len += backup;
1904 uiop->uio_offset -= backup;
1905 uiop->uio_resid += backup;
1908 commit = fxdr_unsigned(int, *tl++);
1911 * Return the lowest commitment level
1912 * obtained by any of the RPCs.
1914 if (committed == NFSWRITE_FILESYNC)
1916 else if (committed == NFSWRITE_DATASYNC &&
1917 commit == NFSWRITE_UNSTABLE)
1920 if (!NFSHASWRITEVERF(nmp)) {
1921 NFSBCOPY((caddr_t)tl,
1922 (caddr_t)&nmp->nm_verf[0],
1924 NFSSETWRITEVERF(nmp);
1925 } else if (NFSBCMP(tl, nmp->nm_verf,
1928 NFSBCOPY(tl, nmp->nm_verf, NFSX_VERF);
1932 if (nd->nd_flag & ND_NFSV4)
1933 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1934 if (nd->nd_flag & (ND_NFSV2 | ND_NFSV4)) {
1935 error = nfsm_loadattr(nd, nap);
1937 *attrflagp = NFS_LATTR_NOSHRINK;
1940 error = nd->nd_repstat;
1944 NFSWRITERPC_SETTIME(wccflag, np, nap, (nd->nd_flag & ND_NFSV4));
1945 m_freem(nd->nd_mrep);
1950 if (nd->nd_mrep != NULL)
1951 m_freem(nd->nd_mrep);
1952 *iomode = committed;
1953 if (nd->nd_repstat && !error)
1954 error = nd->nd_repstat;
1960 * For NFS v2 this is a kludge. Use a create rpc but with the IFMT bits of the
1961 * mode set to specify the file type and the size field for rdev.
1964 nfsrpc_mknod(vnode_t dvp, char *name, int namelen, struct vattr *vap,
1965 u_int32_t rdev, enum vtype vtyp, struct ucred *cred, NFSPROC_T *p,
1966 struct nfsvattr *dnap, struct nfsvattr *nnap, struct nfsfh **nfhpp,
1967 int *attrflagp, int *dattrflagp, void *dstuff)
1971 struct nfsrv_descript nfsd, *nd = &nfsd;
1972 nfsattrbit_t attrbits;
1977 if (namelen > NFS_MAXNAMLEN)
1978 return (ENAMETOOLONG);
1979 NFSCL_REQSTART(nd, NFSPROC_MKNOD, dvp);
1980 if (nd->nd_flag & ND_NFSV4) {
1981 if (vtyp == VBLK || vtyp == VCHR) {
1982 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
1983 *tl++ = vtonfsv34_type(vtyp);
1984 *tl++ = txdr_unsigned(NFSMAJOR(rdev));
1985 *tl = txdr_unsigned(NFSMINOR(rdev));
1987 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1988 *tl = vtonfsv34_type(vtyp);
1991 (void) nfsm_strtom(nd, name, namelen);
1992 if (nd->nd_flag & ND_NFSV3) {
1993 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1994 *tl = vtonfsv34_type(vtyp);
1996 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4))
1997 nfscl_fillsattr(nd, vap, dvp, 0, 0);
1998 if ((nd->nd_flag & ND_NFSV3) &&
1999 (vtyp == VCHR || vtyp == VBLK)) {
2000 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2001 *tl++ = txdr_unsigned(NFSMAJOR(rdev));
2002 *tl = txdr_unsigned(NFSMINOR(rdev));
2004 if (nd->nd_flag & ND_NFSV4) {
2005 NFSGETATTR_ATTRBIT(&attrbits);
2006 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2007 *tl++ = txdr_unsigned(NFSV4OP_GETFH);
2008 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2009 (void) nfsrv_putattrbit(nd, &attrbits);
2011 if (nd->nd_flag & ND_NFSV2)
2012 nfscl_fillsattr(nd, vap, dvp, NFSSATTR_SIZERDEV, rdev);
2013 error = nfscl_request(nd, dvp, p, cred, dstuff);
2016 if (nd->nd_flag & ND_NFSV4)
2017 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
2018 if (!nd->nd_repstat) {
2019 if (nd->nd_flag & ND_NFSV4) {
2020 NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
2021 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
2025 error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp);
2029 if (nd->nd_flag & ND_NFSV3)
2030 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
2031 if (!error && nd->nd_repstat)
2032 error = nd->nd_repstat;
2034 m_freem(nd->nd_mrep);
2039 * nfs file create call
2040 * Mostly just call the approriate routine. (I separated out v4, so that
2041 * error recovery wouldn't be as difficult.)
2044 nfsrpc_create(vnode_t dvp, char *name, int namelen, struct vattr *vap,
2045 nfsquad_t cverf, int fmode, struct ucred *cred, NFSPROC_T *p,
2046 struct nfsvattr *dnap, struct nfsvattr *nnap, struct nfsfh **nfhpp,
2047 int *attrflagp, int *dattrflagp, void *dstuff)
2049 int error = 0, newone, expireret = 0, retrycnt, unlocked;
2050 struct nfsclowner *owp;
2051 struct nfscldeleg *dp;
2052 struct nfsmount *nmp = VFSTONFS(vnode_mount(dvp));
2055 if (NFSHASNFSV4(nmp)) {
2059 error = nfscl_open(dvp, NULL, 0, (NFSV4OPEN_ACCESSWRITE |
2060 NFSV4OPEN_ACCESSREAD), 0, cred, p, &owp, NULL, &newone,
2064 if (nmp->nm_clp != NULL)
2065 clidrev = nmp->nm_clp->nfsc_clientidrev;
2068 if (!NFSHASPNFS(nmp) || nfscl_enablecallb == 0 ||
2069 nfs_numnfscbd == 0 || retrycnt > 0)
2070 error = nfsrpc_createv4(dvp, name, namelen, vap, cverf,
2071 fmode, owp, &dp, cred, p, dnap, nnap, nfhpp,
2072 attrflagp, dattrflagp, dstuff, &unlocked);
2074 error = nfsrpc_getcreatelayout(dvp, name, namelen, vap,
2075 cverf, fmode, owp, &dp, cred, p, dnap, nnap, nfhpp,
2076 attrflagp, dattrflagp, dstuff, &unlocked);
2078 * There is no need to invalidate cached attributes here,
2079 * since new post-delegation issue attributes are always
2080 * returned by nfsrpc_createv4() and these will update the
2084 (void) nfscl_deleg(nmp->nm_mountp, owp->nfsow_clp,
2085 (*nfhpp)->nfh_fh, (*nfhpp)->nfh_len, cred, p, &dp);
2086 nfscl_ownerrelease(nmp, owp, error, newone, unlocked);
2087 if (error == NFSERR_GRACE || error == NFSERR_STALECLIENTID ||
2088 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
2089 error == NFSERR_BADSESSION) {
2090 (void) nfs_catnap(PZERO, error, "nfs_open");
2091 } else if ((error == NFSERR_EXPIRED ||
2092 error == NFSERR_BADSTATEID) && clidrev != 0) {
2093 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
2096 } while (error == NFSERR_GRACE || error == NFSERR_STALECLIENTID ||
2097 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
2098 error == NFSERR_BADSESSION ||
2099 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
2100 expireret == 0 && clidrev != 0 && retrycnt < 4));
2101 if (error && retrycnt >= 4)
2104 error = nfsrpc_createv23(dvp, name, namelen, vap, cverf,
2105 fmode, cred, p, dnap, nnap, nfhpp, attrflagp, dattrflagp,
2112 * The create rpc for v2 and 3.
2115 nfsrpc_createv23(vnode_t dvp, char *name, int namelen, struct vattr *vap,
2116 nfsquad_t cverf, int fmode, struct ucred *cred, NFSPROC_T *p,
2117 struct nfsvattr *dnap, struct nfsvattr *nnap, struct nfsfh **nfhpp,
2118 int *attrflagp, int *dattrflagp, void *dstuff)
2122 struct nfsrv_descript nfsd, *nd = &nfsd;
2127 if (namelen > NFS_MAXNAMLEN)
2128 return (ENAMETOOLONG);
2129 NFSCL_REQSTART(nd, NFSPROC_CREATE, dvp);
2130 (void) nfsm_strtom(nd, name, namelen);
2131 if (nd->nd_flag & ND_NFSV3) {
2132 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2133 if (fmode & O_EXCL) {
2134 *tl = txdr_unsigned(NFSCREATE_EXCLUSIVE);
2135 NFSM_BUILD(tl, u_int32_t *, NFSX_VERF);
2136 *tl++ = cverf.lval[0];
2137 *tl = cverf.lval[1];
2139 *tl = txdr_unsigned(NFSCREATE_UNCHECKED);
2140 nfscl_fillsattr(nd, vap, dvp, 0, 0);
2143 nfscl_fillsattr(nd, vap, dvp, NFSSATTR_SIZE0, 0);
2145 error = nfscl_request(nd, dvp, p, cred, dstuff);
2148 if (nd->nd_repstat == 0) {
2149 error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp);
2153 if (nd->nd_flag & ND_NFSV3)
2154 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
2155 if (nd->nd_repstat != 0 && error == 0)
2156 error = nd->nd_repstat;
2158 m_freem(nd->nd_mrep);
2163 nfsrpc_createv4(vnode_t dvp, char *name, int namelen, struct vattr *vap,
2164 nfsquad_t cverf, int fmode, struct nfsclowner *owp, struct nfscldeleg **dpp,
2165 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap,
2166 struct nfsvattr *nnap, struct nfsfh **nfhpp, int *attrflagp,
2167 int *dattrflagp, void *dstuff, int *unlockedp)
2170 int error = 0, deleg, newone, ret, acesize, limitby;
2171 struct nfsrv_descript nfsd, *nd = &nfsd;
2172 struct nfsclopen *op;
2173 struct nfscldeleg *dp = NULL;
2176 nfsattrbit_t attrbits;
2177 nfsv4stateid_t stateid;
2179 struct nfsmount *nmp;
2180 struct nfsclsession *tsep;
2182 nmp = VFSTONFS(dvp->v_mount);
2189 if (namelen > NFS_MAXNAMLEN)
2190 return (ENAMETOOLONG);
2191 NFSCL_REQSTART(nd, NFSPROC_CREATE, dvp);
2193 * For V4, this is actually an Open op.
2195 NFSM_BUILD(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
2196 *tl++ = txdr_unsigned(owp->nfsow_seqid);
2197 *tl++ = txdr_unsigned(NFSV4OPEN_ACCESSWRITE |
2198 NFSV4OPEN_ACCESSREAD);
2199 *tl++ = txdr_unsigned(NFSV4OPEN_DENYNONE);
2200 tsep = nfsmnt_mdssession(nmp);
2201 *tl++ = tsep->nfsess_clientid.lval[0];
2202 *tl = tsep->nfsess_clientid.lval[1];
2203 (void) nfsm_strtom(nd, owp->nfsow_owner, NFSV4CL_LOCKNAMELEN);
2204 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2205 *tl++ = txdr_unsigned(NFSV4OPEN_CREATE);
2206 if (fmode & O_EXCL) {
2207 if (NFSHASNFSV4N(nmp)) {
2208 if (NFSHASSESSPERSIST(nmp)) {
2209 /* Use GUARDED for persistent sessions. */
2210 *tl = txdr_unsigned(NFSCREATE_GUARDED);
2211 nfscl_fillsattr(nd, vap, dvp, 0, 0);
2213 /* Otherwise, use EXCLUSIVE4_1. */
2214 *tl = txdr_unsigned(NFSCREATE_EXCLUSIVE41);
2215 NFSM_BUILD(tl, u_int32_t *, NFSX_VERF);
2216 *tl++ = cverf.lval[0];
2217 *tl = cverf.lval[1];
2218 nfscl_fillsattr(nd, vap, dvp, 0, 0);
2222 *tl = txdr_unsigned(NFSCREATE_EXCLUSIVE);
2223 NFSM_BUILD(tl, u_int32_t *, NFSX_VERF);
2224 *tl++ = cverf.lval[0];
2225 *tl = cverf.lval[1];
2228 *tl = txdr_unsigned(NFSCREATE_UNCHECKED);
2229 nfscl_fillsattr(nd, vap, dvp, 0, 0);
2231 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2232 *tl = txdr_unsigned(NFSV4OPEN_CLAIMNULL);
2233 (void) nfsm_strtom(nd, name, namelen);
2234 /* Get the new file's handle and attributes. */
2235 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2236 *tl++ = txdr_unsigned(NFSV4OP_GETFH);
2237 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2238 NFSGETATTR_ATTRBIT(&attrbits);
2239 (void) nfsrv_putattrbit(nd, &attrbits);
2240 /* Get the directory's post-op attributes. */
2241 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2242 *tl = txdr_unsigned(NFSV4OP_PUTFH);
2243 (void) nfsm_fhtom(nd, np->n_fhp->nfh_fh, np->n_fhp->nfh_len, 0);
2244 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2245 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2246 (void) nfsrv_putattrbit(nd, &attrbits);
2247 error = nfscl_request(nd, dvp, p, cred, dstuff);
2250 NFSCL_INCRSEQID(owp->nfsow_seqid, nd);
2251 if (nd->nd_repstat == 0) {
2252 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
2254 stateid.seqid = *tl++;
2255 stateid.other[0] = *tl++;
2256 stateid.other[1] = *tl++;
2257 stateid.other[2] = *tl;
2258 rflags = fxdr_unsigned(u_int32_t, *(tl + 6));
2259 (void) nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
2260 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2261 deleg = fxdr_unsigned(int, *tl);
2262 if (deleg == NFSV4OPEN_DELEGATEREAD ||
2263 deleg == NFSV4OPEN_DELEGATEWRITE) {
2264 if (!(owp->nfsow_clp->nfsc_flags &
2265 NFSCLFLAGS_FIRSTDELEG))
2266 owp->nfsow_clp->nfsc_flags |=
2267 (NFSCLFLAGS_FIRSTDELEG | NFSCLFLAGS_GOTDELEG);
2269 sizeof (struct nfscldeleg) + NFSX_V4FHMAX,
2270 M_NFSCLDELEG, M_WAITOK);
2271 LIST_INIT(&dp->nfsdl_owner);
2272 LIST_INIT(&dp->nfsdl_lock);
2273 dp->nfsdl_clp = owp->nfsow_clp;
2274 newnfs_copyincred(cred, &dp->nfsdl_cred);
2275 nfscl_lockinit(&dp->nfsdl_rwlock);
2276 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
2278 dp->nfsdl_stateid.seqid = *tl++;
2279 dp->nfsdl_stateid.other[0] = *tl++;
2280 dp->nfsdl_stateid.other[1] = *tl++;
2281 dp->nfsdl_stateid.other[2] = *tl++;
2282 ret = fxdr_unsigned(int, *tl);
2283 if (deleg == NFSV4OPEN_DELEGATEWRITE) {
2284 dp->nfsdl_flags = NFSCLDL_WRITE;
2286 * Indicates how much the file can grow.
2288 NFSM_DISSECT(tl, u_int32_t *,
2290 limitby = fxdr_unsigned(int, *tl++);
2292 case NFSV4OPEN_LIMITSIZE:
2293 dp->nfsdl_sizelimit = fxdr_hyper(tl);
2295 case NFSV4OPEN_LIMITBLOCKS:
2296 dp->nfsdl_sizelimit =
2297 fxdr_unsigned(u_int64_t, *tl++);
2298 dp->nfsdl_sizelimit *=
2299 fxdr_unsigned(u_int64_t, *tl);
2302 error = NFSERR_BADXDR;
2306 dp->nfsdl_flags = NFSCLDL_READ;
2309 dp->nfsdl_flags |= NFSCLDL_RECALL;
2310 error = nfsrv_dissectace(nd, &dp->nfsdl_ace, &ret,
2314 } else if (deleg != NFSV4OPEN_DELEGATENONE) {
2315 error = NFSERR_BADXDR;
2318 error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp);
2321 /* Get rid of the PutFH and Getattr status values. */
2322 NFSM_DISSECT(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
2323 /* Load the directory attributes. */
2324 error = nfsm_loadattr(nd, dnap);
2328 if (dp != NULL && *attrflagp) {
2329 dp->nfsdl_change = nnap->na_filerev;
2330 dp->nfsdl_modtime = nnap->na_mtime;
2331 dp->nfsdl_flags |= NFSCLDL_MODTIMESET;
2334 * We can now complete the Open state.
2338 dp->nfsdl_fhlen = nfhp->nfh_len;
2339 NFSBCOPY(nfhp->nfh_fh, dp->nfsdl_fh, nfhp->nfh_len);
2342 * Get an Open structure that will be
2343 * attached to the OpenOwner, acquired already.
2345 error = nfscl_open(dvp, nfhp->nfh_fh, nfhp->nfh_len,
2346 (NFSV4OPEN_ACCESSWRITE | NFSV4OPEN_ACCESSREAD), 0,
2347 cred, p, NULL, &op, &newone, NULL, 0);
2350 op->nfso_stateid = stateid;
2351 newnfs_copyincred(cred, &op->nfso_cred);
2352 if ((rflags & NFSV4OPEN_RESULTCONFIRM)) {
2354 ret = nfsrpc_openconfirm(dvp, nfhp->nfh_fh,
2355 nfhp->nfh_len, op, cred, p);
2356 if (ret == NFSERR_DELAY)
2357 (void) nfs_catnap(PZERO, ret, "nfs_create");
2358 } while (ret == NFSERR_DELAY);
2363 * If the server is handing out delegations, but we didn't
2364 * get one because an OpenConfirm was required, try the
2365 * Open again, to get a delegation. This is a harmless no-op,
2366 * from a server's point of view.
2368 if ((rflags & NFSV4OPEN_RESULTCONFIRM) &&
2369 (owp->nfsow_clp->nfsc_flags & NFSCLFLAGS_GOTDELEG) &&
2370 !error && dp == NULL) {
2372 ret = nfsrpc_openrpc(VFSTONFS(vnode_mount(dvp)), dvp,
2373 np->n_fhp->nfh_fh, np->n_fhp->nfh_len,
2374 nfhp->nfh_fh, nfhp->nfh_len,
2375 (NFSV4OPEN_ACCESSWRITE | NFSV4OPEN_ACCESSREAD), op,
2376 name, namelen, &dp, 0, 0x0, cred, p, 0, 1);
2377 if (ret == NFSERR_DELAY)
2378 (void) nfs_catnap(PZERO, ret, "nfs_crt2");
2379 } while (ret == NFSERR_DELAY);
2382 free(dp, M_NFSCLDELEG);
2385 if (ret == NFSERR_STALECLIENTID ||
2386 ret == NFSERR_STALEDONTRECOVER ||
2387 ret == NFSERR_BADSESSION)
2391 nfscl_openrelease(nmp, op, error, newone);
2394 if (nd->nd_repstat != 0 && error == 0)
2395 error = nd->nd_repstat;
2396 if (error == NFSERR_STALECLIENTID)
2397 nfscl_initiate_recovery(owp->nfsow_clp);
2401 else if (dp != NULL)
2402 free(dp, M_NFSCLDELEG);
2403 m_freem(nd->nd_mrep);
2411 nfsrpc_remove(vnode_t dvp, char *name, int namelen, vnode_t vp,
2412 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap, int *dattrflagp,
2416 struct nfsrv_descript nfsd, *nd = &nfsd;
2418 struct nfsmount *nmp;
2419 nfsv4stateid_t dstateid;
2420 int error, ret = 0, i;
2423 if (namelen > NFS_MAXNAMLEN)
2424 return (ENAMETOOLONG);
2425 nmp = VFSTONFS(vnode_mount(dvp));
2427 if (NFSHASNFSV4(nmp) && ret == 0) {
2428 ret = nfscl_removedeleg(vp, p, &dstateid);
2430 NFSCL_REQSTART(nd, NFSPROC_RETDELEGREMOVE, vp);
2431 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID +
2433 if (NFSHASNFSV4N(nmp))
2436 *tl++ = dstateid.seqid;
2437 *tl++ = dstateid.other[0];
2438 *tl++ = dstateid.other[1];
2439 *tl++ = dstateid.other[2];
2440 *tl = txdr_unsigned(NFSV4OP_PUTFH);
2442 (void) nfsm_fhtom(nd, np->n_fhp->nfh_fh,
2443 np->n_fhp->nfh_len, 0);
2444 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2445 *tl = txdr_unsigned(NFSV4OP_REMOVE);
2451 NFSCL_REQSTART(nd, NFSPROC_REMOVE, dvp);
2452 (void) nfsm_strtom(nd, name, namelen);
2453 error = nfscl_request(nd, dvp, p, cred, dstuff);
2456 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) {
2457 /* For NFSv4, parse out any Delereturn replies. */
2458 if (ret > 0 && nd->nd_repstat != 0 &&
2459 (nd->nd_flag & ND_NOMOREDATA)) {
2461 * If the Delegreturn failed, try again without
2462 * it. The server will Recall, as required.
2464 m_freem(nd->nd_mrep);
2467 for (i = 0; i < (ret * 2); i++) {
2468 if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) ==
2470 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2472 nd->nd_flag |= ND_NOMOREDATA;
2475 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
2477 if (nd->nd_repstat && !error)
2478 error = nd->nd_repstat;
2480 m_freem(nd->nd_mrep);
2485 * Do an nfs rename rpc.
2488 nfsrpc_rename(vnode_t fdvp, vnode_t fvp, char *fnameptr, int fnamelen,
2489 vnode_t tdvp, vnode_t tvp, char *tnameptr, int tnamelen, struct ucred *cred,
2490 NFSPROC_T *p, struct nfsvattr *fnap, struct nfsvattr *tnap,
2491 int *fattrflagp, int *tattrflagp, void *fstuff, void *tstuff)
2494 struct nfsrv_descript nfsd, *nd = &nfsd;
2495 struct nfsmount *nmp;
2497 nfsattrbit_t attrbits;
2498 nfsv4stateid_t fdstateid, tdstateid;
2499 int error = 0, ret = 0, gottd = 0, gotfd = 0, i;
2503 nmp = VFSTONFS(vnode_mount(fdvp));
2504 if (fnamelen > NFS_MAXNAMLEN || tnamelen > NFS_MAXNAMLEN)
2505 return (ENAMETOOLONG);
2507 if (NFSHASNFSV4(nmp) && ret == 0) {
2508 ret = nfscl_renamedeleg(fvp, &fdstateid, &gotfd, tvp,
2509 &tdstateid, &gottd, p);
2510 if (gotfd && gottd) {
2511 NFSCL_REQSTART(nd, NFSPROC_RETDELEGRENAME2, fvp);
2513 NFSCL_REQSTART(nd, NFSPROC_RETDELEGRENAME1, fvp);
2515 NFSCL_REQSTART(nd, NFSPROC_RETDELEGRENAME1, tvp);
2518 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID);
2519 if (NFSHASNFSV4N(nmp))
2522 *tl++ = fdstateid.seqid;
2523 *tl++ = fdstateid.other[0];
2524 *tl++ = fdstateid.other[1];
2525 *tl = fdstateid.other[2];
2527 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2528 *tl = txdr_unsigned(NFSV4OP_PUTFH);
2530 (void) nfsm_fhtom(nd, np->n_fhp->nfh_fh,
2531 np->n_fhp->nfh_len, 0);
2532 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2533 *tl = txdr_unsigned(NFSV4OP_DELEGRETURN);
2537 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID);
2538 if (NFSHASNFSV4N(nmp))
2541 *tl++ = tdstateid.seqid;
2542 *tl++ = tdstateid.other[0];
2543 *tl++ = tdstateid.other[1];
2544 *tl = tdstateid.other[2];
2547 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2548 *tl = txdr_unsigned(NFSV4OP_PUTFH);
2550 (void) nfsm_fhtom(nd, np->n_fhp->nfh_fh,
2551 np->n_fhp->nfh_len, 0);
2552 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2553 *tl = txdr_unsigned(NFSV4OP_SAVEFH);
2559 NFSCL_REQSTART(nd, NFSPROC_RENAME, fdvp);
2560 if (nd->nd_flag & ND_NFSV4) {
2561 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2562 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2563 NFSWCCATTR_ATTRBIT(&attrbits);
2564 (void) nfsrv_putattrbit(nd, &attrbits);
2565 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2566 *tl = txdr_unsigned(NFSV4OP_PUTFH);
2567 (void) nfsm_fhtom(nd, VTONFS(tdvp)->n_fhp->nfh_fh,
2568 VTONFS(tdvp)->n_fhp->nfh_len, 0);
2569 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2570 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2571 (void) nfsrv_putattrbit(nd, &attrbits);
2572 nd->nd_flag |= ND_V4WCCATTR;
2573 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2574 *tl = txdr_unsigned(NFSV4OP_RENAME);
2576 (void) nfsm_strtom(nd, fnameptr, fnamelen);
2577 if (!(nd->nd_flag & ND_NFSV4))
2578 (void) nfsm_fhtom(nd, VTONFS(tdvp)->n_fhp->nfh_fh,
2579 VTONFS(tdvp)->n_fhp->nfh_len, 0);
2580 (void) nfsm_strtom(nd, tnameptr, tnamelen);
2581 error = nfscl_request(nd, fdvp, p, cred, fstuff);
2584 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) {
2585 /* For NFSv4, parse out any Delereturn replies. */
2586 if (ret > 0 && nd->nd_repstat != 0 &&
2587 (nd->nd_flag & ND_NOMOREDATA)) {
2589 * If the Delegreturn failed, try again without
2590 * it. The server will Recall, as required.
2592 m_freem(nd->nd_mrep);
2595 for (i = 0; i < (ret * 2); i++) {
2596 if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) ==
2598 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2600 if (i == 0 && ret > 1) {
2602 * If the Delegreturn failed, try again
2603 * without it. The server will Recall, as
2605 * If ret > 1, the first iteration of this
2606 * loop is the second DelegReturn result.
2608 m_freem(nd->nd_mrep);
2611 nd->nd_flag |= ND_NOMOREDATA;
2616 /* Now, the first wcc attribute reply. */
2617 if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) == ND_NFSV4) {
2618 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2620 nd->nd_flag |= ND_NOMOREDATA;
2622 error = nfscl_wcc_data(nd, fdvp, fnap, fattrflagp, NULL,
2624 /* and the second wcc attribute reply. */
2625 if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) == ND_NFSV4 &&
2627 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2629 nd->nd_flag |= ND_NOMOREDATA;
2632 error = nfscl_wcc_data(nd, tdvp, tnap, tattrflagp,
2635 if (nd->nd_repstat && !error)
2636 error = nd->nd_repstat;
2638 m_freem(nd->nd_mrep);
2643 * nfs hard link create rpc
2646 nfsrpc_link(vnode_t dvp, vnode_t vp, char *name, int namelen,
2647 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap,
2648 struct nfsvattr *nap, int *attrflagp, int *dattrflagp, void *dstuff)
2651 struct nfsrv_descript nfsd, *nd = &nfsd;
2652 nfsattrbit_t attrbits;
2657 if (namelen > NFS_MAXNAMLEN)
2658 return (ENAMETOOLONG);
2659 NFSCL_REQSTART(nd, NFSPROC_LINK, vp);
2660 if (nd->nd_flag & ND_NFSV4) {
2661 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2662 *tl = txdr_unsigned(NFSV4OP_PUTFH);
2664 (void) nfsm_fhtom(nd, VTONFS(dvp)->n_fhp->nfh_fh,
2665 VTONFS(dvp)->n_fhp->nfh_len, 0);
2666 if (nd->nd_flag & ND_NFSV4) {
2667 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2668 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2669 NFSWCCATTR_ATTRBIT(&attrbits);
2670 (void) nfsrv_putattrbit(nd, &attrbits);
2671 nd->nd_flag |= ND_V4WCCATTR;
2672 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2673 *tl = txdr_unsigned(NFSV4OP_LINK);
2675 (void) nfsm_strtom(nd, name, namelen);
2676 error = nfscl_request(nd, vp, p, cred, dstuff);
2679 if (nd->nd_flag & ND_NFSV3) {
2680 error = nfscl_postop_attr(nd, nap, attrflagp, dstuff);
2682 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp,
2684 } else if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) == ND_NFSV4) {
2686 * First, parse out the PutFH and Getattr result.
2688 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2690 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2692 nd->nd_flag |= ND_NOMOREDATA;
2694 * Get the pre-op attributes.
2696 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
2698 if (nd->nd_repstat && !error)
2699 error = nd->nd_repstat;
2701 m_freem(nd->nd_mrep);
2706 * nfs symbolic link create rpc
2709 nfsrpc_symlink(vnode_t dvp, char *name, int namelen, const char *target,
2710 struct vattr *vap, struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap,
2711 struct nfsvattr *nnap, struct nfsfh **nfhpp, int *attrflagp,
2712 int *dattrflagp, void *dstuff)
2715 struct nfsrv_descript nfsd, *nd = &nfsd;
2716 struct nfsmount *nmp;
2717 int slen, error = 0;
2722 nmp = VFSTONFS(vnode_mount(dvp));
2723 slen = strlen(target);
2724 if (slen > NFS_MAXPATHLEN || namelen > NFS_MAXNAMLEN)
2725 return (ENAMETOOLONG);
2726 NFSCL_REQSTART(nd, NFSPROC_SYMLINK, dvp);
2727 if (nd->nd_flag & ND_NFSV4) {
2728 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2729 *tl = txdr_unsigned(NFLNK);
2730 (void) nfsm_strtom(nd, target, slen);
2732 (void) nfsm_strtom(nd, name, namelen);
2733 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4))
2734 nfscl_fillsattr(nd, vap, dvp, 0, 0);
2735 if (!(nd->nd_flag & ND_NFSV4))
2736 (void) nfsm_strtom(nd, target, slen);
2737 if (nd->nd_flag & ND_NFSV2)
2738 nfscl_fillsattr(nd, vap, dvp, NFSSATTR_SIZENEG1, 0);
2739 error = nfscl_request(nd, dvp, p, cred, dstuff);
2742 if (nd->nd_flag & ND_NFSV4)
2743 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
2744 if ((nd->nd_flag & ND_NFSV3) && !error) {
2745 if (!nd->nd_repstat)
2746 error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp);
2748 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp,
2751 if (nd->nd_repstat && !error)
2752 error = nd->nd_repstat;
2753 m_freem(nd->nd_mrep);
2755 * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry.
2756 * Only do this if vfs.nfs.ignore_eexist is set.
2757 * Never do this for NFSv4.1 or later minor versions, since sessions
2758 * should guarantee "exactly once" RPC semantics.
2760 if (error == EEXIST && nfsignore_eexist != 0 && (!NFSHASNFSV4(nmp) ||
2761 nmp->nm_minorvers == 0))
2770 nfsrpc_mkdir(vnode_t dvp, char *name, int namelen, struct vattr *vap,
2771 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap,
2772 struct nfsvattr *nnap, struct nfsfh **nfhpp, int *attrflagp,
2773 int *dattrflagp, void *dstuff)
2776 struct nfsrv_descript nfsd, *nd = &nfsd;
2777 nfsattrbit_t attrbits;
2780 struct nfsmount *nmp;
2785 nmp = VFSTONFS(vnode_mount(dvp));
2786 fhp = VTONFS(dvp)->n_fhp;
2787 if (namelen > NFS_MAXNAMLEN)
2788 return (ENAMETOOLONG);
2789 NFSCL_REQSTART(nd, NFSPROC_MKDIR, dvp);
2790 if (nd->nd_flag & ND_NFSV4) {
2791 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2792 *tl = txdr_unsigned(NFDIR);
2794 (void) nfsm_strtom(nd, name, namelen);
2795 nfscl_fillsattr(nd, vap, dvp, NFSSATTR_SIZENEG1, 0);
2796 if (nd->nd_flag & ND_NFSV4) {
2797 NFSGETATTR_ATTRBIT(&attrbits);
2798 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2799 *tl++ = txdr_unsigned(NFSV4OP_GETFH);
2800 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2801 (void) nfsrv_putattrbit(nd, &attrbits);
2802 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2803 *tl = txdr_unsigned(NFSV4OP_PUTFH);
2804 (void) nfsm_fhtom(nd, fhp->nfh_fh, fhp->nfh_len, 0);
2805 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2806 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2807 (void) nfsrv_putattrbit(nd, &attrbits);
2809 error = nfscl_request(nd, dvp, p, cred, dstuff);
2812 if (nd->nd_flag & ND_NFSV4)
2813 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
2814 if (!nd->nd_repstat && !error) {
2815 if (nd->nd_flag & ND_NFSV4) {
2816 NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
2817 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
2820 error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp);
2821 if (error == 0 && (nd->nd_flag & ND_NFSV4) != 0) {
2822 /* Get rid of the PutFH and Getattr status values. */
2823 NFSM_DISSECT(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
2824 /* Load the directory attributes. */
2825 error = nfsm_loadattr(nd, dnap);
2830 if ((nd->nd_flag & ND_NFSV3) && !error)
2831 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
2832 if (nd->nd_repstat && !error)
2833 error = nd->nd_repstat;
2835 m_freem(nd->nd_mrep);
2837 * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry.
2838 * Only do this if vfs.nfs.ignore_eexist is set.
2839 * Never do this for NFSv4.1 or later minor versions, since sessions
2840 * should guarantee "exactly once" RPC semantics.
2842 if (error == EEXIST && nfsignore_eexist != 0 && (!NFSHASNFSV4(nmp) ||
2843 nmp->nm_minorvers == 0))
2849 * nfs remove directory call
2852 nfsrpc_rmdir(vnode_t dvp, char *name, int namelen, struct ucred *cred,
2853 NFSPROC_T *p, struct nfsvattr *dnap, int *dattrflagp, void *dstuff)
2855 struct nfsrv_descript nfsd, *nd = &nfsd;
2859 if (namelen > NFS_MAXNAMLEN)
2860 return (ENAMETOOLONG);
2861 NFSCL_REQSTART(nd, NFSPROC_RMDIR, dvp);
2862 (void) nfsm_strtom(nd, name, namelen);
2863 error = nfscl_request(nd, dvp, p, cred, dstuff);
2866 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4))
2867 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
2868 if (nd->nd_repstat && !error)
2869 error = nd->nd_repstat;
2870 m_freem(nd->nd_mrep);
2872 * Kludge: Map ENOENT => 0 assuming that you have a reply to a retry.
2874 if (error == ENOENT)
2881 * Always returns with either uio_resid unchanged, if you are at the
2882 * end of the directory, or uio_resid == 0, with all DIRBLKSIZ chunks
2884 * I felt this would allow caching of directory blocks more easily
2885 * than returning a pertially filled block.
2886 * Directory offset cookies:
2887 * Oh my, what to do with them...
2888 * I can think of three ways to deal with them:
2889 * 1 - have the layer above these RPCs maintain a map between logical
2890 * directory byte offsets and the NFS directory offset cookies
2891 * 2 - pass the opaque directory offset cookies up into userland
2892 * and let the libc functions deal with them, via the system call
2893 * 3 - return them to userland in the "struct dirent", so future versions
2894 * of libc can use them and do whatever is necessary to make things work
2895 * above these rpc calls, in the meantime
2896 * For now, I do #3 by "hiding" the directory offset cookies after the
2897 * d_name field in struct dirent. This is space inside d_reclen that
2898 * will be ignored by anything that doesn't know about them.
2899 * The directory offset cookies are filled in as the last 8 bytes of
2900 * each directory entry, after d_name. Someday, the userland libc
2901 * functions may be able to use these. In the meantime, it satisfies
2902 * OpenBSD's requirements for cookies being returned.
2903 * If expects the directory offset cookie for the read to be in uio_offset
2904 * and returns the one for the next entry after this directory block in
2908 nfsrpc_readdir(vnode_t vp, struct uio *uiop, nfsuint64 *cookiep,
2909 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp,
2910 int *eofp, void *stuff)
2913 struct dirent *dp = NULL;
2915 nfsquad_t cookie, ncookie;
2916 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
2917 struct nfsnode *dnp = VTONFS(vp);
2918 struct nfsvattr nfsva;
2919 struct nfsrv_descript nfsd, *nd = &nfsd;
2920 int error = 0, tlen, more_dirs = 1, blksiz = 0, bigenough = 1;
2921 int reqsize, tryformoredirs = 1, readsize, eof = 0, gotmnton = 0;
2922 u_int64_t dotfileid, dotdotfileid = 0, fakefileno = UINT64_MAX;
2924 nfsattrbit_t attrbits, dattrbits;
2925 u_int32_t rderr, *tl2 = NULL;
2928 KASSERT(uiop->uio_iovcnt == 1 &&
2929 (uiop->uio_resid & (DIRBLKSIZ - 1)) == 0,
2930 ("nfs readdirrpc bad uio"));
2931 ncookie.lval[0] = ncookie.lval[1] = 0;
2933 * There is no point in reading a lot more than uio_resid, however
2934 * adding one additional DIRBLKSIZ makes sense. Since uio_resid
2935 * and nm_readdirsize are both exact multiples of DIRBLKSIZ, this
2936 * will never make readsize > nm_readdirsize.
2938 readsize = nmp->nm_readdirsize;
2939 if (readsize > uiop->uio_resid)
2940 readsize = uiop->uio_resid + DIRBLKSIZ;
2945 tresid = uiop->uio_resid;
2946 cookie.lval[0] = cookiep->nfsuquad[0];
2947 cookie.lval[1] = cookiep->nfsuquad[1];
2951 * For NFSv4, first create the "." and ".." entries.
2953 if (NFSHASNFSV4(nmp)) {
2954 reqsize = 6 * NFSX_UNSIGNED;
2955 NFSGETATTR_ATTRBIT(&dattrbits);
2956 NFSZERO_ATTRBIT(&attrbits);
2957 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_FILEID);
2958 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TYPE);
2959 if (NFSISSET_ATTRBIT(&dnp->n_vattr.na_suppattr,
2960 NFSATTRBIT_MOUNTEDONFILEID)) {
2961 NFSSETBIT_ATTRBIT(&attrbits,
2962 NFSATTRBIT_MOUNTEDONFILEID);
2966 * Must fake it. Use the fileno, except when the
2967 * fsid is != to that of the directory. For that
2968 * case, generate a fake fileno that is not the same.
2970 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_FSID);
2975 * Joy, oh joy. For V4 we get to hand craft '.' and '..'.
2977 if (uiop->uio_offset == 0) {
2978 NFSCL_REQSTART(nd, NFSPROC_LOOKUPP, vp);
2979 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2980 *tl++ = txdr_unsigned(NFSV4OP_GETFH);
2981 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2982 (void) nfsrv_putattrbit(nd, &attrbits);
2983 error = nfscl_request(nd, vp, p, cred, stuff);
2986 dotfileid = 0; /* Fake out the compiler. */
2987 if ((nd->nd_flag & ND_NOMOREDATA) == 0) {
2988 error = nfsm_loadattr(nd, &nfsva);
2991 dotfileid = nfsva.na_fileid;
2993 if (nd->nd_repstat == 0) {
2994 NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
2995 len = fxdr_unsigned(int, *(tl + 4));
2996 if (len > 0 && len <= NFSX_V4FHMAX)
2997 error = nfsm_advance(nd, NFSM_RNDUP(len), -1);
3001 NFSM_DISSECT(tl, u_int32_t *, 2*NFSX_UNSIGNED);
3002 nfsva.na_mntonfileno = UINT64_MAX;
3003 error = nfsv4_loadattr(nd, NULL, &nfsva, NULL,
3004 NULL, 0, NULL, NULL, NULL, NULL, NULL, 0,
3005 NULL, NULL, NULL, p, cred);
3007 dotdotfileid = dotfileid;
3008 } else if (gotmnton) {
3009 if (nfsva.na_mntonfileno != UINT64_MAX)
3010 dotdotfileid = nfsva.na_mntonfileno;
3012 dotdotfileid = nfsva.na_fileid;
3013 } else if (nfsva.na_filesid[0] ==
3014 dnp->n_vattr.na_filesid[0] &&
3015 nfsva.na_filesid[1] ==
3016 dnp->n_vattr.na_filesid[1]) {
3017 dotdotfileid = nfsva.na_fileid;
3021 } while (fakefileno ==
3023 dotdotfileid = fakefileno;
3026 } else if (nd->nd_repstat == NFSERR_NOENT) {
3028 * Lookupp returns NFSERR_NOENT when we are
3029 * at the root, so just use the current dir.
3032 dotdotfileid = dotfileid;
3034 error = nd->nd_repstat;
3036 m_freem(nd->nd_mrep);
3040 dp = (struct dirent *)uiop->uio_iov->iov_base;
3041 dp->d_pad0 = dp->d_pad1 = 0;
3043 dp->d_type = DT_DIR;
3044 dp->d_fileno = dotfileid;
3046 *((uint64_t *)dp->d_name) = 0; /* Zero pad it. */
3047 dp->d_name[0] = '.';
3048 dp->d_reclen = _GENERIC_DIRSIZ(dp) + NFSX_HYPER;
3050 * Just make these offset cookie 0.
3052 tl = (u_int32_t *)&dp->d_name[8];
3055 blksiz += dp->d_reclen;
3056 uiop->uio_resid -= dp->d_reclen;
3057 uiop->uio_offset += dp->d_reclen;
3058 uiop->uio_iov->iov_base =
3059 (char *)uiop->uio_iov->iov_base + dp->d_reclen;
3060 uiop->uio_iov->iov_len -= dp->d_reclen;
3061 dp = (struct dirent *)uiop->uio_iov->iov_base;
3062 dp->d_pad0 = dp->d_pad1 = 0;
3064 dp->d_type = DT_DIR;
3065 dp->d_fileno = dotdotfileid;
3067 *((uint64_t *)dp->d_name) = 0;
3068 dp->d_name[0] = '.';
3069 dp->d_name[1] = '.';
3070 dp->d_reclen = _GENERIC_DIRSIZ(dp) + NFSX_HYPER;
3072 * Just make these offset cookie 0.
3074 tl = (u_int32_t *)&dp->d_name[8];
3077 blksiz += dp->d_reclen;
3078 uiop->uio_resid -= dp->d_reclen;
3079 uiop->uio_offset += dp->d_reclen;
3080 uiop->uio_iov->iov_base =
3081 (char *)uiop->uio_iov->iov_base + dp->d_reclen;
3082 uiop->uio_iov->iov_len -= dp->d_reclen;
3084 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_RDATTRERROR);
3086 reqsize = 5 * NFSX_UNSIGNED;
3091 * Loop around doing readdir rpc's of size readsize.
3092 * The stopping criteria is EOF or buffer full.
3094 while (more_dirs && bigenough) {
3096 NFSCL_REQSTART(nd, NFSPROC_READDIR, vp);
3097 if (nd->nd_flag & ND_NFSV2) {
3098 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
3099 *tl++ = cookie.lval[1];
3100 *tl = txdr_unsigned(readsize);
3102 NFSM_BUILD(tl, u_int32_t *, reqsize);
3103 *tl++ = cookie.lval[0];
3104 *tl++ = cookie.lval[1];
3105 if (cookie.qval == 0) {
3110 *tl++ = dnp->n_cookieverf.nfsuquad[0];
3111 *tl++ = dnp->n_cookieverf.nfsuquad[1];
3114 if (nd->nd_flag & ND_NFSV4) {
3115 *tl++ = txdr_unsigned(readsize);
3116 *tl = txdr_unsigned(readsize);
3117 (void) nfsrv_putattrbit(nd, &attrbits);
3118 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
3119 *tl = txdr_unsigned(NFSV4OP_GETATTR);
3120 (void) nfsrv_putattrbit(nd, &dattrbits);
3122 *tl = txdr_unsigned(readsize);
3125 error = nfscl_request(nd, vp, p, cred, stuff);
3128 if (!(nd->nd_flag & ND_NFSV2)) {
3129 if (nd->nd_flag & ND_NFSV3)
3130 error = nfscl_postop_attr(nd, nap, attrflagp,
3132 if (!nd->nd_repstat && !error) {
3133 NFSM_DISSECT(tl, u_int32_t *, NFSX_HYPER);
3135 dnp->n_cookieverf.nfsuquad[0] = *tl++;
3136 dnp->n_cookieverf.nfsuquad[1] = *tl;
3140 if (nd->nd_repstat || error) {
3142 error = nd->nd_repstat;
3145 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3146 more_dirs = fxdr_unsigned(int, *tl);
3150 /* loop through the dir entries, doctoring them to 4bsd form */
3151 while (more_dirs && bigenough) {
3152 if (nd->nd_flag & ND_NFSV4) {
3153 NFSM_DISSECT(tl, u_int32_t *, 3*NFSX_UNSIGNED);
3154 ncookie.lval[0] = *tl++;
3155 ncookie.lval[1] = *tl++;
3156 len = fxdr_unsigned(int, *tl);
3157 } else if (nd->nd_flag & ND_NFSV3) {
3158 NFSM_DISSECT(tl, u_int32_t *, 3*NFSX_UNSIGNED);
3159 nfsva.na_fileid = fxdr_hyper(tl);
3161 len = fxdr_unsigned(int, *tl);
3163 NFSM_DISSECT(tl, u_int32_t *, 2*NFSX_UNSIGNED);
3164 nfsva.na_fileid = fxdr_unsigned(uint64_t,
3166 len = fxdr_unsigned(int, *tl);
3168 if (len <= 0 || len > NFS_MAXNAMLEN) {
3172 tlen = roundup2(len, 8);
3174 tlen += 8; /* To ensure null termination. */
3175 left = DIRBLKSIZ - blksiz;
3176 if (_GENERIC_DIRLEN(len) + NFSX_HYPER > left) {
3177 NFSBZERO(uiop->uio_iov->iov_base, left);
3178 dp->d_reclen += left;
3179 uiop->uio_iov->iov_base =
3180 (char *)uiop->uio_iov->iov_base + left;
3181 uiop->uio_iov->iov_len -= left;
3182 uiop->uio_resid -= left;
3183 uiop->uio_offset += left;
3186 if (_GENERIC_DIRLEN(len) + NFSX_HYPER >
3190 dp = (struct dirent *)uiop->uio_iov->iov_base;
3191 dp->d_pad0 = dp->d_pad1 = 0;
3194 dp->d_reclen = _GENERIC_DIRLEN(len) +
3196 dp->d_type = DT_UNKNOWN;
3197 blksiz += dp->d_reclen;
3198 if (blksiz == DIRBLKSIZ)
3200 uiop->uio_resid -= DIRHDSIZ;
3201 uiop->uio_offset += DIRHDSIZ;
3202 uiop->uio_iov->iov_base =
3203 (char *)uiop->uio_iov->iov_base + DIRHDSIZ;
3204 uiop->uio_iov->iov_len -= DIRHDSIZ;
3205 error = nfsm_mbufuio(nd, uiop, len);
3208 cp = uiop->uio_iov->iov_base;
3211 cp += tlen; /* points to cookie storage */
3212 tl2 = (u_int32_t *)cp;
3213 uiop->uio_iov->iov_base =
3214 (char *)uiop->uio_iov->iov_base + tlen +
3216 uiop->uio_iov->iov_len -= tlen + NFSX_HYPER;
3217 uiop->uio_resid -= tlen + NFSX_HYPER;
3218 uiop->uio_offset += (tlen + NFSX_HYPER);
3220 error = nfsm_advance(nd, NFSM_RNDUP(len), -1);
3224 if (nd->nd_flag & ND_NFSV4) {
3226 nfsva.na_mntonfileno = UINT64_MAX;
3227 error = nfsv4_loadattr(nd, NULL, &nfsva, NULL,
3228 NULL, 0, NULL, NULL, NULL, NULL, NULL, 0,
3229 NULL, NULL, &rderr, p, cred);
3232 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3233 } else if (nd->nd_flag & ND_NFSV3) {
3234 NFSM_DISSECT(tl, u_int32_t *, 3*NFSX_UNSIGNED);
3235 ncookie.lval[0] = *tl++;
3236 ncookie.lval[1] = *tl++;
3238 NFSM_DISSECT(tl, u_int32_t *, 2*NFSX_UNSIGNED);
3239 ncookie.lval[0] = 0;
3240 ncookie.lval[1] = *tl++;
3243 if (nd->nd_flag & ND_NFSV4) {
3248 if (nfsva.na_mntonfileno != UINT64_MAX)
3249 dp->d_fileno = nfsva.na_mntonfileno;
3251 dp->d_fileno = nfsva.na_fileid;
3252 } else if (nfsva.na_filesid[0] ==
3253 dnp->n_vattr.na_filesid[0] &&
3254 nfsva.na_filesid[1] ==
3255 dnp->n_vattr.na_filesid[1]) {
3256 dp->d_fileno = nfsva.na_fileid;
3260 } while (fakefileno ==
3262 dp->d_fileno = fakefileno;
3264 dp->d_type = vtonfs_dtype(nfsva.na_type);
3267 dp->d_fileno = nfsva.na_fileid;
3269 *tl2++ = cookiep->nfsuquad[0] = cookie.lval[0] =
3271 *tl2 = cookiep->nfsuquad[1] = cookie.lval[1] =
3274 more_dirs = fxdr_unsigned(int, *tl);
3277 * If at end of rpc data, get the eof boolean
3280 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3281 eof = fxdr_unsigned(int, *tl);
3284 if (nd->nd_flag & ND_NFSV4) {
3285 error = nfscl_postop_attr(nd, nap, attrflagp,
3291 m_freem(nd->nd_mrep);
3295 * Fill last record, iff any, out to a multiple of DIRBLKSIZ
3296 * by increasing d_reclen for the last record.
3299 left = DIRBLKSIZ - blksiz;
3300 NFSBZERO(uiop->uio_iov->iov_base, left);
3301 dp->d_reclen += left;
3302 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base +
3304 uiop->uio_iov->iov_len -= left;
3305 uiop->uio_resid -= left;
3306 uiop->uio_offset += left;
3310 * If returning no data, assume end of file.
3311 * If not bigenough, return not end of file, since you aren't
3312 * returning all the data
3313 * Otherwise, return the eof flag from the server.
3316 if (tresid == ((size_t)(uiop->uio_resid)))
3318 else if (!bigenough)
3325 * Add extra empty records to any remaining DIRBLKSIZ chunks.
3327 while (uiop->uio_resid > 0 && uiop->uio_resid != tresid) {
3328 dp = (struct dirent *)uiop->uio_iov->iov_base;
3329 NFSBZERO(dp, DIRBLKSIZ);
3330 dp->d_type = DT_UNKNOWN;
3331 tl = (u_int32_t *)&dp->d_name[4];
3332 *tl++ = cookie.lval[0];
3333 *tl = cookie.lval[1];
3334 dp->d_reclen = DIRBLKSIZ;
3335 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base +
3337 uiop->uio_iov->iov_len -= DIRBLKSIZ;
3338 uiop->uio_resid -= DIRBLKSIZ;
3339 uiop->uio_offset += DIRBLKSIZ;
3343 if (nd->nd_mrep != NULL)
3344 m_freem(nd->nd_mrep);
3350 * NFS V3 readdir plus RPC. Used in place of nfsrpc_readdir().
3351 * (Also used for NFS V4 when mount flag set.)
3352 * (ditto above w.r.t. multiple of DIRBLKSIZ, etc.)
3355 nfsrpc_readdirplus(vnode_t vp, struct uio *uiop, nfsuint64 *cookiep,
3356 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp,
3357 int *eofp, void *stuff)
3360 struct dirent *dp = NULL;
3362 vnode_t newvp = NULLVP;
3363 struct nfsrv_descript nfsd, *nd = &nfsd;
3364 struct nameidata nami, *ndp = &nami;
3365 struct componentname *cnp = &ndp->ni_cnd;
3366 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
3367 struct nfsnode *dnp = VTONFS(vp), *np;
3368 struct nfsvattr nfsva;
3370 nfsquad_t cookie, ncookie;
3371 int error = 0, tlen, more_dirs = 1, blksiz = 0, bigenough = 1;
3372 int attrflag, tryformoredirs = 1, eof = 0, gotmnton = 0;
3373 int isdotdot = 0, unlocknewvp = 0;
3374 u_int64_t dotfileid, dotdotfileid = 0, fakefileno = UINT64_MAX;
3375 u_int64_t fileno = 0;
3377 nfsattrbit_t attrbits, dattrbits;
3379 u_int32_t *tl2 = NULL, rderr;
3380 struct timespec dctime;
3382 KASSERT(uiop->uio_iovcnt == 1 &&
3383 (uiop->uio_resid & (DIRBLKSIZ - 1)) == 0,
3384 ("nfs readdirplusrpc bad uio"));
3385 ncookie.lval[0] = ncookie.lval[1] = 0;
3386 timespecclear(&dctime);
3392 cookie.lval[0] = cookiep->nfsuquad[0];
3393 cookie.lval[1] = cookiep->nfsuquad[1];
3394 tresid = uiop->uio_resid;
3397 * For NFSv4, first create the "." and ".." entries.
3399 if (NFSHASNFSV4(nmp)) {
3400 NFSGETATTR_ATTRBIT(&dattrbits);
3401 NFSZERO_ATTRBIT(&attrbits);
3402 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_FILEID);
3403 if (NFSISSET_ATTRBIT(&dnp->n_vattr.na_suppattr,
3404 NFSATTRBIT_MOUNTEDONFILEID)) {
3405 NFSSETBIT_ATTRBIT(&attrbits,
3406 NFSATTRBIT_MOUNTEDONFILEID);
3410 * Must fake it. Use the fileno, except when the
3411 * fsid is != to that of the directory. For that
3412 * case, generate a fake fileno that is not the same.
3414 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_FSID);
3419 * Joy, oh joy. For V4 we get to hand craft '.' and '..'.
3421 if (uiop->uio_offset == 0) {
3422 NFSCL_REQSTART(nd, NFSPROC_LOOKUPP, vp);
3423 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
3424 *tl++ = txdr_unsigned(NFSV4OP_GETFH);
3425 *tl = txdr_unsigned(NFSV4OP_GETATTR);
3426 (void) nfsrv_putattrbit(nd, &attrbits);
3427 error = nfscl_request(nd, vp, p, cred, stuff);
3430 dotfileid = 0; /* Fake out the compiler. */
3431 if ((nd->nd_flag & ND_NOMOREDATA) == 0) {
3432 error = nfsm_loadattr(nd, &nfsva);
3435 dctime = nfsva.na_ctime;
3436 dotfileid = nfsva.na_fileid;
3438 if (nd->nd_repstat == 0) {
3439 NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
3440 len = fxdr_unsigned(int, *(tl + 4));
3441 if (len > 0 && len <= NFSX_V4FHMAX)
3442 error = nfsm_advance(nd, NFSM_RNDUP(len), -1);
3446 NFSM_DISSECT(tl, u_int32_t *, 2*NFSX_UNSIGNED);
3447 nfsva.na_mntonfileno = UINT64_MAX;
3448 error = nfsv4_loadattr(nd, NULL, &nfsva, NULL,
3449 NULL, 0, NULL, NULL, NULL, NULL, NULL, 0,
3450 NULL, NULL, NULL, p, cred);
3452 dotdotfileid = dotfileid;
3453 } else if (gotmnton) {
3454 if (nfsva.na_mntonfileno != UINT64_MAX)
3455 dotdotfileid = nfsva.na_mntonfileno;
3457 dotdotfileid = nfsva.na_fileid;
3458 } else if (nfsva.na_filesid[0] ==
3459 dnp->n_vattr.na_filesid[0] &&
3460 nfsva.na_filesid[1] ==
3461 dnp->n_vattr.na_filesid[1]) {
3462 dotdotfileid = nfsva.na_fileid;
3466 } while (fakefileno ==
3468 dotdotfileid = fakefileno;
3471 } else if (nd->nd_repstat == NFSERR_NOENT) {
3473 * Lookupp returns NFSERR_NOENT when we are
3474 * at the root, so just use the current dir.
3477 dotdotfileid = dotfileid;
3479 error = nd->nd_repstat;
3481 m_freem(nd->nd_mrep);
3485 dp = (struct dirent *)uiop->uio_iov->iov_base;
3486 dp->d_pad0 = dp->d_pad1 = 0;
3488 dp->d_type = DT_DIR;
3489 dp->d_fileno = dotfileid;
3491 *((uint64_t *)dp->d_name) = 0; /* Zero pad it. */
3492 dp->d_name[0] = '.';
3493 dp->d_reclen = _GENERIC_DIRSIZ(dp) + NFSX_HYPER;
3495 * Just make these offset cookie 0.
3497 tl = (u_int32_t *)&dp->d_name[8];
3500 blksiz += dp->d_reclen;
3501 uiop->uio_resid -= dp->d_reclen;
3502 uiop->uio_offset += dp->d_reclen;
3503 uiop->uio_iov->iov_base =
3504 (char *)uiop->uio_iov->iov_base + dp->d_reclen;
3505 uiop->uio_iov->iov_len -= dp->d_reclen;
3506 dp = (struct dirent *)uiop->uio_iov->iov_base;
3507 dp->d_pad0 = dp->d_pad1 = 0;
3509 dp->d_type = DT_DIR;
3510 dp->d_fileno = dotdotfileid;
3512 *((uint64_t *)dp->d_name) = 0;
3513 dp->d_name[0] = '.';
3514 dp->d_name[1] = '.';
3515 dp->d_reclen = _GENERIC_DIRSIZ(dp) + NFSX_HYPER;
3517 * Just make these offset cookie 0.
3519 tl = (u_int32_t *)&dp->d_name[8];
3522 blksiz += dp->d_reclen;
3523 uiop->uio_resid -= dp->d_reclen;
3524 uiop->uio_offset += dp->d_reclen;
3525 uiop->uio_iov->iov_base =
3526 (char *)uiop->uio_iov->iov_base + dp->d_reclen;
3527 uiop->uio_iov->iov_len -= dp->d_reclen;
3529 NFSREADDIRPLUS_ATTRBIT(&attrbits);
3531 NFSSETBIT_ATTRBIT(&attrbits,
3532 NFSATTRBIT_MOUNTEDONFILEID);
3536 * Loop around doing readdir rpc's of size nm_readdirsize.
3537 * The stopping criteria is EOF or buffer full.
3539 while (more_dirs && bigenough) {
3541 NFSCL_REQSTART(nd, NFSPROC_READDIRPLUS, vp);
3542 NFSM_BUILD(tl, u_int32_t *, 6 * NFSX_UNSIGNED);
3543 *tl++ = cookie.lval[0];
3544 *tl++ = cookie.lval[1];
3545 if (cookie.qval == 0) {
3550 *tl++ = dnp->n_cookieverf.nfsuquad[0];
3551 *tl++ = dnp->n_cookieverf.nfsuquad[1];
3554 *tl++ = txdr_unsigned(nmp->nm_readdirsize);
3555 *tl = txdr_unsigned(nmp->nm_readdirsize);
3556 if (nd->nd_flag & ND_NFSV4) {
3557 (void) nfsrv_putattrbit(nd, &attrbits);
3558 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
3559 *tl = txdr_unsigned(NFSV4OP_GETATTR);
3560 (void) nfsrv_putattrbit(nd, &dattrbits);
3562 error = nfscl_request(nd, vp, p, cred, stuff);
3565 if (nd->nd_flag & ND_NFSV3)
3566 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
3567 if (nd->nd_repstat || error) {
3569 error = nd->nd_repstat;
3572 if ((nd->nd_flag & ND_NFSV3) != 0 && *attrflagp != 0)
3573 dctime = nap->na_ctime;
3574 NFSM_DISSECT(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
3576 dnp->n_cookieverf.nfsuquad[0] = *tl++;
3577 dnp->n_cookieverf.nfsuquad[1] = *tl++;
3579 more_dirs = fxdr_unsigned(int, *tl);
3583 /* loop through the dir entries, doctoring them to 4bsd form */
3584 while (more_dirs && bigenough) {
3585 NFSM_DISSECT(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
3586 if (nd->nd_flag & ND_NFSV4) {
3587 ncookie.lval[0] = *tl++;
3588 ncookie.lval[1] = *tl++;
3590 fileno = fxdr_hyper(tl);
3593 len = fxdr_unsigned(int, *tl);
3594 if (len <= 0 || len > NFS_MAXNAMLEN) {
3598 tlen = roundup2(len, 8);
3600 tlen += 8; /* To ensure null termination. */
3601 left = DIRBLKSIZ - blksiz;
3602 if (_GENERIC_DIRLEN(len) + NFSX_HYPER > left) {
3603 NFSBZERO(uiop->uio_iov->iov_base, left);
3604 dp->d_reclen += left;
3605 uiop->uio_iov->iov_base =
3606 (char *)uiop->uio_iov->iov_base + left;
3607 uiop->uio_iov->iov_len -= left;
3608 uiop->uio_resid -= left;
3609 uiop->uio_offset += left;
3612 if (_GENERIC_DIRLEN(len) + NFSX_HYPER >
3616 dp = (struct dirent *)uiop->uio_iov->iov_base;
3617 dp->d_pad0 = dp->d_pad1 = 0;
3620 dp->d_reclen = _GENERIC_DIRLEN(len) +
3622 dp->d_type = DT_UNKNOWN;
3623 blksiz += dp->d_reclen;
3624 if (blksiz == DIRBLKSIZ)
3626 uiop->uio_resid -= DIRHDSIZ;
3627 uiop->uio_offset += DIRHDSIZ;
3628 uiop->uio_iov->iov_base =
3629 (char *)uiop->uio_iov->iov_base + DIRHDSIZ;
3630 uiop->uio_iov->iov_len -= DIRHDSIZ;
3631 cnp->cn_nameptr = uiop->uio_iov->iov_base;
3632 cnp->cn_namelen = len;
3634 error = nfsm_mbufuio(nd, uiop, len);
3637 cp = uiop->uio_iov->iov_base;
3640 cp += tlen; /* points to cookie storage */
3641 tl2 = (u_int32_t *)cp;
3642 if (len == 2 && cnp->cn_nameptr[0] == '.' &&
3643 cnp->cn_nameptr[1] == '.')
3647 uiop->uio_iov->iov_base =
3648 (char *)uiop->uio_iov->iov_base + tlen +
3650 uiop->uio_iov->iov_len -= tlen + NFSX_HYPER;
3651 uiop->uio_resid -= tlen + NFSX_HYPER;
3652 uiop->uio_offset += (tlen + NFSX_HYPER);
3654 error = nfsm_advance(nd, NFSM_RNDUP(len), -1);
3659 if (nd->nd_flag & ND_NFSV3) {
3660 NFSM_DISSECT(tl, u_int32_t *, 3*NFSX_UNSIGNED);
3661 ncookie.lval[0] = *tl++;
3662 ncookie.lval[1] = *tl++;
3663 attrflag = fxdr_unsigned(int, *tl);
3665 error = nfsm_loadattr(nd, &nfsva);
3669 NFSM_DISSECT(tl,u_int32_t *,NFSX_UNSIGNED);
3671 error = nfsm_getfh(nd, &nfhp);
3675 if (!attrflag && nfhp != NULL) {
3676 free(nfhp, M_NFSFH);
3681 nfsva.na_mntonfileno = 0xffffffff;
3682 error = nfsv4_loadattr(nd, NULL, &nfsva, &nfhp,
3683 NULL, 0, NULL, NULL, NULL, NULL, NULL, 0,
3684 NULL, NULL, &rderr, p, cred);
3690 if (nd->nd_flag & ND_NFSV4) {
3693 } else if (gotmnton) {
3694 if (nfsva.na_mntonfileno != 0xffffffff)
3695 dp->d_fileno = nfsva.na_mntonfileno;
3697 dp->d_fileno = nfsva.na_fileid;
3698 } else if (nfsva.na_filesid[0] ==
3699 dnp->n_vattr.na_filesid[0] &&
3700 nfsva.na_filesid[1] ==
3701 dnp->n_vattr.na_filesid[1]) {
3702 dp->d_fileno = nfsva.na_fileid;
3706 } while (fakefileno ==
3708 dp->d_fileno = fakefileno;
3711 dp->d_fileno = fileno;
3713 *tl2++ = cookiep->nfsuquad[0] = cookie.lval[0] =
3715 *tl2 = cookiep->nfsuquad[1] = cookie.lval[1] =
3719 if (NFSRV_CMPFH(nfhp->nfh_fh, nfhp->nfh_len,
3720 dnp->n_fhp->nfh_fh, dnp->n_fhp->nfh_len)) {
3724 free(nfhp, M_NFSFH);
3726 } else if (isdotdot != 0) {
3728 * Skip doing a nfscl_nget() call for "..".
3729 * There's a race between acquiring the nfs
3730 * node here and lookups that look for the
3731 * directory being read (in the parent).
3732 * It would try to get a lock on ".." here,
3733 * owning the lock on the directory being
3734 * read. Lookup will hold the lock on ".."
3735 * and try to acquire the lock on the
3736 * directory being read.
3737 * If the directory is unlocked/relocked,
3738 * then there is a LOR with the buflock
3741 free(nfhp, M_NFSFH);
3743 error = nfscl_nget(vnode_mount(vp), vp,
3744 nfhp, cnp, p, &np, NULL, LK_EXCLUSIVE);
3751 if (newvp != NULLVP) {
3752 error = nfscl_loadattrcache(&newvp,
3753 &nfsva, NULL, NULL, 0, 0);
3762 vtonfs_dtype(np->n_vattr.na_type);
3764 NFSCNHASH(cnp, HASHINIT);
3765 if (cnp->cn_namelen <= NCHNAMLEN &&
3766 (newvp->v_type != VDIR ||
3767 dctime.tv_sec != 0)) {
3768 cache_enter_time(ndp->ni_dvp,
3771 newvp->v_type != VDIR ? NULL :
3781 } else if (nfhp != NULL) {
3782 free(nfhp, M_NFSFH);
3784 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3785 more_dirs = fxdr_unsigned(int, *tl);
3788 * If at end of rpc data, get the eof boolean
3791 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3792 eof = fxdr_unsigned(int, *tl);
3795 if (nd->nd_flag & ND_NFSV4) {
3796 error = nfscl_postop_attr(nd, nap, attrflagp,
3802 m_freem(nd->nd_mrep);
3806 * Fill last record, iff any, out to a multiple of DIRBLKSIZ
3807 * by increasing d_reclen for the last record.
3810 left = DIRBLKSIZ - blksiz;
3811 NFSBZERO(uiop->uio_iov->iov_base, left);
3812 dp->d_reclen += left;
3813 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base +
3815 uiop->uio_iov->iov_len -= left;
3816 uiop->uio_resid -= left;
3817 uiop->uio_offset += left;
3821 * If returning no data, assume end of file.
3822 * If not bigenough, return not end of file, since you aren't
3823 * returning all the data
3824 * Otherwise, return the eof flag from the server.
3827 if (tresid == uiop->uio_resid)
3829 else if (!bigenough)
3836 * Add extra empty records to any remaining DIRBLKSIZ chunks.
3838 while (uiop->uio_resid > 0 && uiop->uio_resid != tresid) {
3839 dp = (struct dirent *)uiop->uio_iov->iov_base;
3840 NFSBZERO(dp, DIRBLKSIZ);
3841 dp->d_type = DT_UNKNOWN;
3842 tl = (u_int32_t *)&dp->d_name[4];
3843 *tl++ = cookie.lval[0];
3844 *tl = cookie.lval[1];
3845 dp->d_reclen = DIRBLKSIZ;
3846 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base +
3848 uiop->uio_iov->iov_len -= DIRBLKSIZ;
3849 uiop->uio_resid -= DIRBLKSIZ;
3850 uiop->uio_offset += DIRBLKSIZ;
3854 if (nd->nd_mrep != NULL)
3855 m_freem(nd->nd_mrep);
3864 nfsrpc_commit(vnode_t vp, u_quad_t offset, int cnt, struct ucred *cred,
3865 NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, void *stuff)
3868 struct nfsrv_descript nfsd, *nd = &nfsd;
3869 nfsattrbit_t attrbits;
3871 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
3874 NFSCL_REQSTART(nd, NFSPROC_COMMIT, vp);
3875 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
3876 txdr_hyper(offset, tl);
3878 *tl = txdr_unsigned(cnt);
3879 if (nd->nd_flag & ND_NFSV4) {
3881 * And do a Getattr op.
3883 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
3884 *tl = txdr_unsigned(NFSV4OP_GETATTR);
3885 NFSGETATTR_ATTRBIT(&attrbits);
3886 (void) nfsrv_putattrbit(nd, &attrbits);
3888 error = nfscl_request(nd, vp, p, cred, stuff);
3891 error = nfscl_wcc_data(nd, vp, nap, attrflagp, NULL, stuff);
3892 if (!error && !nd->nd_repstat) {
3893 NFSM_DISSECT(tl, u_int32_t *, NFSX_VERF);
3895 if (NFSBCMP(nmp->nm_verf, tl, NFSX_VERF)) {
3896 NFSBCOPY(tl, nmp->nm_verf, NFSX_VERF);
3897 nd->nd_repstat = NFSERR_STALEWRITEVERF;
3900 if (nd->nd_flag & ND_NFSV4)
3901 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
3904 if (!error && nd->nd_repstat)
3905 error = nd->nd_repstat;
3906 m_freem(nd->nd_mrep);
3911 * NFS byte range lock rpc.
3912 * (Mostly just calls one of the three lower level RPC routines.)
3915 nfsrpc_advlock(vnode_t vp, off_t size, int op, struct flock *fl,
3916 int reclaim, struct ucred *cred, NFSPROC_T *p, void *id, int flags)
3918 struct nfscllockowner *lp;
3919 struct nfsclclient *clp;
3921 struct nfsrv_descript nfsd, *nd = &nfsd;
3922 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
3925 u_int32_t clidrev = 0;
3926 int error = 0, newone = 0, expireret = 0, retrycnt, donelocally;
3930 * Convert the flock structure into a start and end and do POSIX
3933 switch (fl->l_whence) {
3937 * Caller is responsible for adding any necessary offset
3938 * when SEEK_CUR is used.
3940 start = fl->l_start;
3944 start = size + fl->l_start;
3945 off = size + fl->l_start;
3952 if (fl->l_len != 0) {
3953 end = start + fl->l_len - 1;
3964 if (op == F_GETLK) {
3965 error = nfscl_getcl(vnode_mount(vp), cred, p, 1, &clp);
3968 error = nfscl_lockt(vp, clp, off, len, fl, p, id, flags);
3970 clidrev = clp->nfsc_clientidrev;
3971 error = nfsrpc_lockt(nd, vp, clp, off, len, fl, cred,
3973 } else if (error == -1) {
3976 nfscl_clientrelease(clp);
3977 } else if (op == F_UNLCK && fl->l_type == F_UNLCK) {
3979 * We must loop around for all lockowner cases.
3982 error = nfscl_getcl(vnode_mount(vp), cred, p, 1, &clp);
3986 error = nfscl_relbytelock(vp, off, len, cred, p, callcnt,
3987 clp, id, flags, &lp, &dorpc);
3989 * If it returns a NULL lp, we're done.
3993 nfscl_clientrelease(clp);
3995 nfscl_releasealllocks(clp, vp, p, id, flags);
3998 if (nmp->nm_clp != NULL)
3999 clidrev = nmp->nm_clp->nfsc_clientidrev;
4003 * If the server doesn't support Posix lock semantics,
4004 * only allow locks on the entire file, since it won't
4005 * handle overlapping byte ranges.
4006 * There might still be a problem when a lock
4007 * upgrade/downgrade (read<->write) occurs, since the
4008 * server "might" expect an unlock first?
4010 if (dorpc && (lp->nfsl_open->nfso_posixlock ||
4011 (off == 0 && len == NFS64BITSSET))) {
4013 * Since the lock records will go away, we must
4014 * wait for grace and delay here.
4017 error = nfsrpc_locku(nd, nmp, lp, off, len,
4018 NFSV4LOCKT_READ, cred, p, 0);
4019 if ((nd->nd_repstat == NFSERR_GRACE ||
4020 nd->nd_repstat == NFSERR_DELAY) &&
4022 (void) nfs_catnap(PZERO, (int)nd->nd_repstat,
4024 } while ((nd->nd_repstat == NFSERR_GRACE ||
4025 nd->nd_repstat == NFSERR_DELAY) && error == 0);
4028 } while (error == 0 && nd->nd_repstat == 0);
4029 nfscl_releasealllocks(clp, vp, p, id, flags);
4030 } else if (op == F_SETLK) {
4031 error = nfscl_getbytelock(vp, off, len, fl->l_type, cred, p,
4032 NULL, 0, id, flags, NULL, NULL, &lp, &newone, &donelocally);
4033 if (error || donelocally) {
4036 if (nmp->nm_clp != NULL)
4037 clidrev = nmp->nm_clp->nfsc_clientidrev;
4040 nfhp = VTONFS(vp)->n_fhp;
4041 if (!lp->nfsl_open->nfso_posixlock &&
4042 (off != 0 || len != NFS64BITSSET)) {
4045 error = nfsrpc_lock(nd, nmp, vp, nfhp->nfh_fh,
4046 nfhp->nfh_len, lp, newone, reclaim, off,
4047 len, fl->l_type, cred, p, 0);
4050 error = nd->nd_repstat;
4051 nfscl_lockrelease(lp, error, newone);
4056 error = nd->nd_repstat;
4057 if (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
4058 error == NFSERR_STALEDONTRECOVER ||
4059 error == NFSERR_STALECLIENTID || error == NFSERR_DELAY ||
4060 error == NFSERR_BADSESSION) {
4061 (void) nfs_catnap(PZERO, error, "nfs_advlock");
4062 } else if ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID)
4064 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
4067 } while (error == NFSERR_GRACE ||
4068 error == NFSERR_STALECLIENTID || error == NFSERR_DELAY ||
4069 error == NFSERR_STALEDONTRECOVER || error == NFSERR_STALESTATEID ||
4070 error == NFSERR_BADSESSION ||
4071 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
4072 expireret == 0 && clidrev != 0 && retrycnt < 4));
4073 if (error && retrycnt >= 4)
4079 * The lower level routine for the LockT case.
4082 nfsrpc_lockt(struct nfsrv_descript *nd, vnode_t vp,
4083 struct nfsclclient *clp, u_int64_t off, u_int64_t len, struct flock *fl,
4084 struct ucred *cred, NFSPROC_T *p, void *id, int flags)
4087 int error, type, size;
4088 uint8_t own[NFSV4CL_LOCKNAMELEN + NFSX_V4FHMAX];
4090 struct nfsmount *nmp;
4091 struct nfsclsession *tsep;
4093 nmp = VFSTONFS(vp->v_mount);
4094 NFSCL_REQSTART(nd, NFSPROC_LOCKT, vp);
4095 NFSM_BUILD(tl, u_int32_t *, 7 * NFSX_UNSIGNED);
4096 if (fl->l_type == F_RDLCK)
4097 *tl++ = txdr_unsigned(NFSV4LOCKT_READ);
4099 *tl++ = txdr_unsigned(NFSV4LOCKT_WRITE);
4100 txdr_hyper(off, tl);
4102 txdr_hyper(len, tl);
4104 tsep = nfsmnt_mdssession(nmp);
4105 *tl++ = tsep->nfsess_clientid.lval[0];
4106 *tl = tsep->nfsess_clientid.lval[1];
4107 nfscl_filllockowner(id, own, flags);
4109 NFSBCOPY(np->n_fhp->nfh_fh, &own[NFSV4CL_LOCKNAMELEN],
4110 np->n_fhp->nfh_len);
4111 (void)nfsm_strtom(nd, own, NFSV4CL_LOCKNAMELEN + np->n_fhp->nfh_len);
4112 error = nfscl_request(nd, vp, p, cred, NULL);
4115 if (nd->nd_repstat == 0) {
4116 fl->l_type = F_UNLCK;
4117 } else if (nd->nd_repstat == NFSERR_DENIED) {
4119 fl->l_whence = SEEK_SET;
4120 NFSM_DISSECT(tl, u_int32_t *, 8 * NFSX_UNSIGNED);
4121 fl->l_start = fxdr_hyper(tl);
4123 len = fxdr_hyper(tl);
4125 if (len == NFS64BITSSET)
4129 type = fxdr_unsigned(int, *tl++);
4130 if (type == NFSV4LOCKT_WRITE)
4131 fl->l_type = F_WRLCK;
4133 fl->l_type = F_RDLCK;
4135 * XXX For now, I have no idea what to do with the
4136 * conflicting lock_owner, so I'll just set the pid == 0
4137 * and skip over the lock_owner.
4139 fl->l_pid = (pid_t)0;
4141 size = fxdr_unsigned(int, *tl);
4142 if (size < 0 || size > NFSV4_OPAQUELIMIT)
4145 error = nfsm_advance(nd, NFSM_RNDUP(size), -1);
4146 } else if (nd->nd_repstat == NFSERR_STALECLIENTID)
4147 nfscl_initiate_recovery(clp);
4149 m_freem(nd->nd_mrep);
4154 * Lower level function that performs the LockU RPC.
4157 nfsrpc_locku(struct nfsrv_descript *nd, struct nfsmount *nmp,
4158 struct nfscllockowner *lp, u_int64_t off, u_int64_t len,
4159 u_int32_t type, struct ucred *cred, NFSPROC_T *p, int syscred)
4164 nfscl_reqstart(nd, NFSPROC_LOCKU, nmp, lp->nfsl_open->nfso_fh,
4165 lp->nfsl_open->nfso_fhlen, NULL, NULL, 0, 0);
4166 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID + 6 * NFSX_UNSIGNED);
4167 *tl++ = txdr_unsigned(type);
4168 *tl = txdr_unsigned(lp->nfsl_seqid);
4169 if (nfstest_outofseq &&
4170 (arc4random() % nfstest_outofseq) == 0)
4171 *tl = txdr_unsigned(lp->nfsl_seqid + 1);
4173 if (NFSHASNFSV4N(nmp))
4176 *tl++ = lp->nfsl_stateid.seqid;
4177 *tl++ = lp->nfsl_stateid.other[0];
4178 *tl++ = lp->nfsl_stateid.other[1];
4179 *tl++ = lp->nfsl_stateid.other[2];
4180 txdr_hyper(off, tl);
4182 txdr_hyper(len, tl);
4184 nd->nd_flag |= ND_USEGSSNAME;
4185 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4186 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4187 NFSCL_INCRSEQID(lp->nfsl_seqid, nd);
4190 if (nd->nd_repstat == 0) {
4191 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID);
4192 lp->nfsl_stateid.seqid = *tl++;
4193 lp->nfsl_stateid.other[0] = *tl++;
4194 lp->nfsl_stateid.other[1] = *tl++;
4195 lp->nfsl_stateid.other[2] = *tl;
4196 } else if (nd->nd_repstat == NFSERR_STALESTATEID)
4197 nfscl_initiate_recovery(lp->nfsl_open->nfso_own->nfsow_clp);
4199 m_freem(nd->nd_mrep);
4204 * The actual Lock RPC.
4207 nfsrpc_lock(struct nfsrv_descript *nd, struct nfsmount *nmp, vnode_t vp,
4208 u_int8_t *nfhp, int fhlen, struct nfscllockowner *lp, int newone,
4209 int reclaim, u_int64_t off, u_int64_t len, short type, struct ucred *cred,
4210 NFSPROC_T *p, int syscred)
4214 uint8_t own[NFSV4CL_LOCKNAMELEN + NFSX_V4FHMAX];
4215 struct nfsclsession *tsep;
4217 nfscl_reqstart(nd, NFSPROC_LOCK, nmp, nfhp, fhlen, NULL, NULL, 0, 0);
4218 NFSM_BUILD(tl, u_int32_t *, 7 * NFSX_UNSIGNED);
4219 if (type == F_RDLCK)
4220 *tl++ = txdr_unsigned(NFSV4LOCKT_READ);
4222 *tl++ = txdr_unsigned(NFSV4LOCKT_WRITE);
4223 *tl++ = txdr_unsigned(reclaim);
4224 txdr_hyper(off, tl);
4226 txdr_hyper(len, tl);
4230 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID +
4231 2 * NFSX_UNSIGNED + NFSX_HYPER);
4232 *tl++ = txdr_unsigned(lp->nfsl_open->nfso_own->nfsow_seqid);
4233 if (NFSHASNFSV4N(nmp))
4236 *tl++ = lp->nfsl_open->nfso_stateid.seqid;
4237 *tl++ = lp->nfsl_open->nfso_stateid.other[0];
4238 *tl++ = lp->nfsl_open->nfso_stateid.other[1];
4239 *tl++ = lp->nfsl_open->nfso_stateid.other[2];
4240 *tl++ = txdr_unsigned(lp->nfsl_seqid);
4241 tsep = nfsmnt_mdssession(nmp);
4242 *tl++ = tsep->nfsess_clientid.lval[0];
4243 *tl = tsep->nfsess_clientid.lval[1];
4244 NFSBCOPY(lp->nfsl_owner, own, NFSV4CL_LOCKNAMELEN);
4245 NFSBCOPY(nfhp, &own[NFSV4CL_LOCKNAMELEN], fhlen);
4246 (void)nfsm_strtom(nd, own, NFSV4CL_LOCKNAMELEN + fhlen);
4249 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID + NFSX_UNSIGNED);
4250 if (NFSHASNFSV4N(nmp))
4253 *tl++ = lp->nfsl_stateid.seqid;
4254 *tl++ = lp->nfsl_stateid.other[0];
4255 *tl++ = lp->nfsl_stateid.other[1];
4256 *tl++ = lp->nfsl_stateid.other[2];
4257 *tl = txdr_unsigned(lp->nfsl_seqid);
4258 if (nfstest_outofseq &&
4259 (arc4random() % nfstest_outofseq) == 0)
4260 *tl = txdr_unsigned(lp->nfsl_seqid + 1);
4263 nd->nd_flag |= ND_USEGSSNAME;
4264 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, vp, p, cred,
4265 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4269 NFSCL_INCRSEQID(lp->nfsl_open->nfso_own->nfsow_seqid, nd);
4270 NFSCL_INCRSEQID(lp->nfsl_seqid, nd);
4271 if (nd->nd_repstat == 0) {
4272 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID);
4273 lp->nfsl_stateid.seqid = *tl++;
4274 lp->nfsl_stateid.other[0] = *tl++;
4275 lp->nfsl_stateid.other[1] = *tl++;
4276 lp->nfsl_stateid.other[2] = *tl;
4277 } else if (nd->nd_repstat == NFSERR_DENIED) {
4278 NFSM_DISSECT(tl, u_int32_t *, 8 * NFSX_UNSIGNED);
4279 size = fxdr_unsigned(int, *(tl + 7));
4280 if (size < 0 || size > NFSV4_OPAQUELIMIT)
4283 error = nfsm_advance(nd, NFSM_RNDUP(size), -1);
4284 } else if (nd->nd_repstat == NFSERR_STALESTATEID)
4285 nfscl_initiate_recovery(lp->nfsl_open->nfso_own->nfsow_clp);
4287 m_freem(nd->nd_mrep);
4293 * (always called with the vp for the mount point)
4296 nfsrpc_statfs(vnode_t vp, struct nfsstatfs *sbp, struct nfsfsinfo *fsp,
4297 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp,
4300 u_int32_t *tl = NULL;
4301 struct nfsrv_descript nfsd, *nd = &nfsd;
4302 struct nfsmount *nmp;
4303 nfsattrbit_t attrbits;
4307 nmp = VFSTONFS(vnode_mount(vp));
4308 if (NFSHASNFSV4(nmp)) {
4310 * For V4, you actually do a getattr.
4312 NFSCL_REQSTART(nd, NFSPROC_GETATTR, vp);
4313 NFSSTATFS_GETATTRBIT(&attrbits);
4314 (void) nfsrv_putattrbit(nd, &attrbits);
4315 nd->nd_flag |= ND_USEGSSNAME;
4316 error = nfscl_request(nd, vp, p, cred, stuff);
4319 if (nd->nd_repstat == 0) {
4320 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0,
4321 NULL, NULL, sbp, fsp, NULL, 0, NULL, NULL, NULL, p,
4324 nmp->nm_fsid[0] = nap->na_filesid[0];
4325 nmp->nm_fsid[1] = nap->na_filesid[1];
4326 NFSSETHASSETFSID(nmp);
4330 error = nd->nd_repstat;
4335 NFSCL_REQSTART(nd, NFSPROC_FSSTAT, vp);
4336 error = nfscl_request(nd, vp, p, cred, stuff);
4339 if (nd->nd_flag & ND_NFSV3) {
4340 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
4344 if (nd->nd_repstat) {
4345 error = nd->nd_repstat;
4348 NFSM_DISSECT(tl, u_int32_t *,
4349 NFSX_STATFS(nd->nd_flag & ND_NFSV3));
4351 if (NFSHASNFSV3(nmp)) {
4352 sbp->sf_tbytes = fxdr_hyper(tl); tl += 2;
4353 sbp->sf_fbytes = fxdr_hyper(tl); tl += 2;
4354 sbp->sf_abytes = fxdr_hyper(tl); tl += 2;
4355 sbp->sf_tfiles = fxdr_hyper(tl); tl += 2;
4356 sbp->sf_ffiles = fxdr_hyper(tl); tl += 2;
4357 sbp->sf_afiles = fxdr_hyper(tl); tl += 2;
4358 sbp->sf_invarsec = fxdr_unsigned(u_int32_t, *tl);
4359 } else if (NFSHASNFSV4(nmp) == 0) {
4360 sbp->sf_tsize = fxdr_unsigned(u_int32_t, *tl++);
4361 sbp->sf_bsize = fxdr_unsigned(u_int32_t, *tl++);
4362 sbp->sf_blocks = fxdr_unsigned(u_int32_t, *tl++);
4363 sbp->sf_bfree = fxdr_unsigned(u_int32_t, *tl++);
4364 sbp->sf_bavail = fxdr_unsigned(u_int32_t, *tl);
4367 m_freem(nd->nd_mrep);
4375 nfsrpc_pathconf(vnode_t vp, struct nfsv3_pathconf *pc,
4376 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp,
4379 struct nfsrv_descript nfsd, *nd = &nfsd;
4380 struct nfsmount *nmp;
4382 nfsattrbit_t attrbits;
4386 nmp = VFSTONFS(vnode_mount(vp));
4387 if (NFSHASNFSV4(nmp)) {
4389 * For V4, you actually do a getattr.
4391 NFSCL_REQSTART(nd, NFSPROC_GETATTR, vp);
4392 NFSPATHCONF_GETATTRBIT(&attrbits);
4393 (void) nfsrv_putattrbit(nd, &attrbits);
4394 nd->nd_flag |= ND_USEGSSNAME;
4395 error = nfscl_request(nd, vp, p, cred, stuff);
4398 if (nd->nd_repstat == 0) {
4399 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0,
4400 pc, NULL, NULL, NULL, NULL, 0, NULL, NULL, NULL, p,
4405 error = nd->nd_repstat;
4408 NFSCL_REQSTART(nd, NFSPROC_PATHCONF, vp);
4409 error = nfscl_request(nd, vp, p, cred, stuff);
4412 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
4413 if (nd->nd_repstat && !error)
4414 error = nd->nd_repstat;
4416 NFSM_DISSECT(tl, u_int32_t *, NFSX_V3PATHCONF);
4417 pc->pc_linkmax = fxdr_unsigned(u_int32_t, *tl++);
4418 pc->pc_namemax = fxdr_unsigned(u_int32_t, *tl++);
4419 pc->pc_notrunc = fxdr_unsigned(u_int32_t, *tl++);
4420 pc->pc_chownrestricted =
4421 fxdr_unsigned(u_int32_t, *tl++);
4422 pc->pc_caseinsensitive =
4423 fxdr_unsigned(u_int32_t, *tl++);
4424 pc->pc_casepreserving = fxdr_unsigned(u_int32_t, *tl);
4428 m_freem(nd->nd_mrep);
4433 * nfs version 3 fsinfo rpc call
4436 nfsrpc_fsinfo(vnode_t vp, struct nfsfsinfo *fsp, struct ucred *cred,
4437 NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, void *stuff)
4440 struct nfsrv_descript nfsd, *nd = &nfsd;
4444 NFSCL_REQSTART(nd, NFSPROC_FSINFO, vp);
4445 error = nfscl_request(nd, vp, p, cred, stuff);
4448 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
4449 if (nd->nd_repstat && !error)
4450 error = nd->nd_repstat;
4452 NFSM_DISSECT(tl, u_int32_t *, NFSX_V3FSINFO);
4453 fsp->fs_rtmax = fxdr_unsigned(u_int32_t, *tl++);
4454 fsp->fs_rtpref = fxdr_unsigned(u_int32_t, *tl++);
4455 fsp->fs_rtmult = fxdr_unsigned(u_int32_t, *tl++);
4456 fsp->fs_wtmax = fxdr_unsigned(u_int32_t, *tl++);
4457 fsp->fs_wtpref = fxdr_unsigned(u_int32_t, *tl++);
4458 fsp->fs_wtmult = fxdr_unsigned(u_int32_t, *tl++);
4459 fsp->fs_dtpref = fxdr_unsigned(u_int32_t, *tl++);
4460 fsp->fs_maxfilesize = fxdr_hyper(tl);
4462 fxdr_nfsv3time(tl, &fsp->fs_timedelta);
4464 fsp->fs_properties = fxdr_unsigned(u_int32_t, *tl);
4467 m_freem(nd->nd_mrep);
4472 * This function performs the Renew RPC.
4475 nfsrpc_renew(struct nfsclclient *clp, struct nfsclds *dsp, struct ucred *cred,
4479 struct nfsrv_descript nfsd;
4480 struct nfsrv_descript *nd = &nfsd;
4481 struct nfsmount *nmp;
4483 struct nfssockreq *nrp;
4484 struct nfsclsession *tsep;
4486 nmp = clp->nfsc_nmp;
4490 nfscl_reqstart(nd, NFSPROC_RENEW, nmp, NULL, 0, NULL, NULL, 0,
4493 nfscl_reqstart(nd, NFSPROC_RENEW, nmp, NULL, 0, NULL,
4494 &dsp->nfsclds_sess, 0, 0);
4495 if (!NFSHASNFSV4N(nmp)) {
4496 /* NFSv4.1 just uses a Sequence Op and not a Renew. */
4497 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
4498 tsep = nfsmnt_mdssession(nmp);
4499 *tl++ = tsep->nfsess_clientid.lval[0];
4500 *tl = tsep->nfsess_clientid.lval[1];
4504 nrp = dsp->nfsclds_sockp;
4506 /* If NULL, use the MDS socket. */
4507 nrp = &nmp->nm_sockreq;
4508 nd->nd_flag |= ND_USEGSSNAME;
4510 error = newnfs_request(nd, nmp, NULL, nrp, NULL, p, cred,
4511 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4513 error = newnfs_request(nd, nmp, NULL, nrp, NULL, p, cred,
4514 NFS_PROG, NFS_VER4, NULL, 1, NULL, &dsp->nfsclds_sess);
4516 nfscl_cancelreqs(dsp);
4520 error = nd->nd_repstat;
4521 m_freem(nd->nd_mrep);
4526 * This function performs the Releaselockowner RPC.
4529 nfsrpc_rellockown(struct nfsmount *nmp, struct nfscllockowner *lp,
4530 uint8_t *fh, int fhlen, struct ucred *cred, NFSPROC_T *p)
4532 struct nfsrv_descript nfsd, *nd = &nfsd;
4535 uint8_t own[NFSV4CL_LOCKNAMELEN + NFSX_V4FHMAX];
4536 struct nfsclsession *tsep;
4538 if (NFSHASNFSV4N(nmp)) {
4539 /* For NFSv4.1, do a FreeStateID. */
4540 nfscl_reqstart(nd, NFSPROC_FREESTATEID, nmp, NULL, 0, NULL,
4542 nfsm_stateidtom(nd, &lp->nfsl_stateid, NFSSTATEID_PUTSTATEID);
4544 nfscl_reqstart(nd, NFSPROC_RELEASELCKOWN, nmp, NULL, 0, NULL,
4546 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
4547 tsep = nfsmnt_mdssession(nmp);
4548 *tl++ = tsep->nfsess_clientid.lval[0];
4549 *tl = tsep->nfsess_clientid.lval[1];
4550 NFSBCOPY(lp->nfsl_owner, own, NFSV4CL_LOCKNAMELEN);
4551 NFSBCOPY(fh, &own[NFSV4CL_LOCKNAMELEN], fhlen);
4552 (void)nfsm_strtom(nd, own, NFSV4CL_LOCKNAMELEN + fhlen);
4554 nd->nd_flag |= ND_USEGSSNAME;
4555 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4556 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4559 error = nd->nd_repstat;
4560 m_freem(nd->nd_mrep);
4565 * This function performs the Compound to get the mount pt FH.
4568 nfsrpc_getdirpath(struct nfsmount *nmp, u_char *dirpath, struct ucred *cred,
4572 struct nfsrv_descript nfsd;
4573 struct nfsrv_descript *nd = &nfsd;
4575 int error, cnt, len, setnil;
4578 nfscl_reqstart(nd, NFSPROC_PUTROOTFH, nmp, NULL, 0, &opcntp, NULL, 0,
4587 while (*cp2 != '\0' && *cp2 != '/')
4594 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
4595 *tl = txdr_unsigned(NFSV4OP_LOOKUP);
4596 nfsm_strtom(nd, cp, strlen(cp));
4602 } while (*cp != '\0');
4603 if (NFSHASNFSV4N(nmp))
4604 /* Has a Sequence Op done by nfscl_reqstart(). */
4605 *opcntp = txdr_unsigned(3 + cnt);
4607 *opcntp = txdr_unsigned(2 + cnt);
4608 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
4609 *tl = txdr_unsigned(NFSV4OP_GETFH);
4610 nd->nd_flag |= ND_USEGSSNAME;
4611 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4612 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4615 if (nd->nd_repstat == 0) {
4616 NFSM_DISSECT(tl, u_int32_t *, (3 + 2 * cnt) * NFSX_UNSIGNED);
4617 tl += (2 + 2 * cnt);
4618 if ((len = fxdr_unsigned(int, *tl)) <= 0 ||
4620 nd->nd_repstat = NFSERR_BADXDR;
4622 nd->nd_repstat = nfsrv_mtostr(nd, nmp->nm_fh, len);
4623 if (nd->nd_repstat == 0)
4624 nmp->nm_fhsize = len;
4627 error = nd->nd_repstat;
4629 m_freem(nd->nd_mrep);
4634 * This function performs the Delegreturn RPC.
4637 nfsrpc_delegreturn(struct nfscldeleg *dp, struct ucred *cred,
4638 struct nfsmount *nmp, NFSPROC_T *p, int syscred)
4641 struct nfsrv_descript nfsd;
4642 struct nfsrv_descript *nd = &nfsd;
4645 nfscl_reqstart(nd, NFSPROC_DELEGRETURN, nmp, dp->nfsdl_fh,
4646 dp->nfsdl_fhlen, NULL, NULL, 0, 0);
4647 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID);
4648 if (NFSHASNFSV4N(nmp))
4651 *tl++ = dp->nfsdl_stateid.seqid;
4652 *tl++ = dp->nfsdl_stateid.other[0];
4653 *tl++ = dp->nfsdl_stateid.other[1];
4654 *tl = dp->nfsdl_stateid.other[2];
4656 nd->nd_flag |= ND_USEGSSNAME;
4657 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4658 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4661 error = nd->nd_repstat;
4662 m_freem(nd->nd_mrep);
4670 nfsrpc_getacl(vnode_t vp, struct ucred *cred, NFSPROC_T *p,
4671 struct acl *aclp, void *stuff)
4673 struct nfsrv_descript nfsd, *nd = &nfsd;
4675 nfsattrbit_t attrbits;
4676 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
4678 if (nfsrv_useacl == 0 || !NFSHASNFSV4(nmp))
4679 return (EOPNOTSUPP);
4680 NFSCL_REQSTART(nd, NFSPROC_GETACL, vp);
4681 NFSZERO_ATTRBIT(&attrbits);
4682 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_ACL);
4683 (void) nfsrv_putattrbit(nd, &attrbits);
4684 error = nfscl_request(nd, vp, p, cred, stuff);
4687 if (!nd->nd_repstat)
4688 error = nfsv4_loadattr(nd, vp, NULL, NULL, NULL, 0, NULL,
4689 NULL, NULL, NULL, aclp, 0, NULL, NULL, NULL, p, cred);
4691 error = nd->nd_repstat;
4692 m_freem(nd->nd_mrep);
4700 nfsrpc_setacl(vnode_t vp, struct ucred *cred, NFSPROC_T *p,
4701 struct acl *aclp, void *stuff)
4704 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
4706 if (nfsrv_useacl == 0 || !NFSHASNFSV4(nmp))
4707 return (EOPNOTSUPP);
4708 error = nfsrpc_setattr(vp, NULL, aclp, cred, p, NULL, NULL, stuff);
4716 nfsrpc_setaclrpc(vnode_t vp, struct ucred *cred, NFSPROC_T *p,
4717 struct acl *aclp, nfsv4stateid_t *stateidp, void *stuff)
4719 struct nfsrv_descript nfsd, *nd = &nfsd;
4721 nfsattrbit_t attrbits;
4722 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
4724 if (!NFSHASNFSV4(nmp))
4725 return (EOPNOTSUPP);
4726 NFSCL_REQSTART(nd, NFSPROC_SETACL, vp);
4727 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID);
4728 NFSZERO_ATTRBIT(&attrbits);
4729 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_ACL);
4730 (void) nfsv4_fillattr(nd, vnode_mount(vp), vp, aclp, NULL, NULL, 0,
4731 &attrbits, NULL, NULL, 0, 0, 0, 0, (uint64_t)0, NULL);
4732 error = nfscl_request(nd, vp, p, cred, stuff);
4735 /* Don't care about the pre/postop attributes */
4736 m_freem(nd->nd_mrep);
4737 return (nd->nd_repstat);
4741 * Do the NFSv4.1 Exchange ID.
4744 nfsrpc_exchangeid(struct nfsmount *nmp, struct nfsclclient *clp,
4745 struct nfssockreq *nrp, int minorvers, uint32_t exchflags,
4746 struct nfsclds **dspp, struct ucred *cred, NFSPROC_T *p)
4748 uint32_t *tl, v41flags;
4749 struct nfsrv_descript nfsd;
4750 struct nfsrv_descript *nd = &nfsd;
4751 struct nfsclds *dsp;
4752 struct timespec verstime;
4757 minorvers = nmp->nm_minorvers;
4758 nfscl_reqstart(nd, NFSPROC_EXCHANGEID, nmp, NULL, 0, NULL, NULL,
4759 NFS_VER4, minorvers);
4760 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_UNSIGNED);
4761 *tl++ = txdr_unsigned(nfsboottime.tv_sec); /* Client owner */
4762 *tl = txdr_unsigned(clp->nfsc_rev);
4763 (void) nfsm_strtom(nd, clp->nfsc_id, clp->nfsc_idlen);
4765 NFSM_BUILD(tl, uint32_t *, 3 * NFSX_UNSIGNED);
4766 *tl++ = txdr_unsigned(exchflags);
4767 *tl++ = txdr_unsigned(NFSV4EXCH_SP4NONE);
4769 /* Set the implementation id4 */
4770 *tl = txdr_unsigned(1);
4771 (void) nfsm_strtom(nd, "freebsd.org", strlen("freebsd.org"));
4772 (void) nfsm_strtom(nd, version, strlen(version));
4773 NFSM_BUILD(tl, uint32_t *, NFSX_V4TIME);
4774 verstime.tv_sec = 1293840000; /* Jan 1, 2011 */
4775 verstime.tv_nsec = 0;
4776 txdr_nfsv4time(&verstime, tl);
4777 nd->nd_flag |= ND_USEGSSNAME;
4778 error = newnfs_request(nd, nmp, NULL, nrp, NULL, p, cred,
4779 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4780 NFSCL_DEBUG(1, "exchangeid err=%d reps=%d\n", error,
4781 (int)nd->nd_repstat);
4784 if (nd->nd_repstat == 0) {
4785 NFSM_DISSECT(tl, uint32_t *, 6 * NFSX_UNSIGNED + NFSX_HYPER);
4786 len = fxdr_unsigned(int, *(tl + 7));
4787 if (len < 0 || len > NFSV4_OPAQUELIMIT) {
4788 error = NFSERR_BADXDR;
4791 dsp = malloc(sizeof(struct nfsclds) + len + 1, M_NFSCLDS,
4793 dsp->nfsclds_expire = NFSD_MONOSEC + clp->nfsc_renew;
4794 dsp->nfsclds_servownlen = len;
4795 dsp->nfsclds_sess.nfsess_clientid.lval[0] = *tl++;
4796 dsp->nfsclds_sess.nfsess_clientid.lval[1] = *tl++;
4797 dsp->nfsclds_sess.nfsess_sequenceid =
4798 fxdr_unsigned(uint32_t, *tl++);
4799 v41flags = fxdr_unsigned(uint32_t, *tl);
4800 if ((v41flags & NFSV4EXCH_USEPNFSMDS) != 0 &&
4801 NFSHASPNFSOPT(nmp)) {
4802 NFSCL_DEBUG(1, "set PNFS\n");
4804 nmp->nm_state |= NFSSTA_PNFS;
4806 dsp->nfsclds_flags |= NFSCLDS_MDS;
4808 if ((v41flags & NFSV4EXCH_USEPNFSDS) != 0)
4809 dsp->nfsclds_flags |= NFSCLDS_DS;
4810 if (minorvers == NFSV42_MINORVERSION)
4811 dsp->nfsclds_flags |= NFSCLDS_MINORV2;
4813 nd->nd_repstat = nfsrv_mtostr(nd,
4814 dsp->nfsclds_serverown, len);
4815 if (nd->nd_repstat == 0) {
4816 mtx_init(&dsp->nfsclds_mtx, "nfsds", NULL, MTX_DEF);
4817 mtx_init(&dsp->nfsclds_sess.nfsess_mtx, "nfssession",
4819 nfscl_initsessionslots(&dsp->nfsclds_sess);
4822 free(dsp, M_NFSCLDS);
4824 error = nd->nd_repstat;
4826 m_freem(nd->nd_mrep);
4831 * Do the NFSv4.1 Create Session.
4834 nfsrpc_createsession(struct nfsmount *nmp, struct nfsclsession *sep,
4835 struct nfssockreq *nrp, struct nfsclds *dsp, uint32_t sequenceid, int mds,
4836 struct ucred *cred, NFSPROC_T *p)
4838 uint32_t crflags, maxval, *tl;
4839 struct nfsrv_descript nfsd;
4840 struct nfsrv_descript *nd = &nfsd;
4841 int error, irdcnt, minorvers;
4843 /* Make sure nm_rsize, nm_wsize is set. */
4844 if (nmp->nm_rsize > NFS_MAXBSIZE || nmp->nm_rsize == 0)
4845 nmp->nm_rsize = NFS_MAXBSIZE;
4846 if (nmp->nm_wsize > NFS_MAXBSIZE || nmp->nm_wsize == 0)
4847 nmp->nm_wsize = NFS_MAXBSIZE;
4849 minorvers = nmp->nm_minorvers;
4850 else if ((dsp->nfsclds_flags & NFSCLDS_MINORV2) != 0)
4851 minorvers = NFSV42_MINORVERSION;
4853 minorvers = NFSV41_MINORVERSION;
4854 nfscl_reqstart(nd, NFSPROC_CREATESESSION, nmp, NULL, 0, NULL, NULL,
4855 NFS_VER4, minorvers);
4856 NFSM_BUILD(tl, uint32_t *, 4 * NFSX_UNSIGNED);
4857 *tl++ = sep->nfsess_clientid.lval[0];
4858 *tl++ = sep->nfsess_clientid.lval[1];
4859 *tl++ = txdr_unsigned(sequenceid);
4860 crflags = (NFSMNT_RDONLY(nmp->nm_mountp) ? 0 : NFSV4CRSESS_PERSIST);
4861 if (nfscl_enablecallb != 0 && nfs_numnfscbd > 0 && mds != 0)
4862 crflags |= NFSV4CRSESS_CONNBACKCHAN;
4863 *tl = txdr_unsigned(crflags);
4865 /* Fill in fore channel attributes. */
4866 NFSM_BUILD(tl, uint32_t *, 7 * NFSX_UNSIGNED);
4867 *tl++ = 0; /* Header pad size */
4868 if ((nd->nd_flag & ND_NFSV42) != 0 && mds != 0 && sb_max_adj >=
4869 nmp->nm_wsize && sb_max_adj >= nmp->nm_rsize) {
4871 * NFSv4.2 Extended Attribute operations may want to do
4872 * requests/replies that are larger than nm_rsize/nm_wsize.
4874 *tl++ = txdr_unsigned(sb_max_adj - NFS_MAXXDR);
4875 *tl++ = txdr_unsigned(sb_max_adj - NFS_MAXXDR);
4877 *tl++ = txdr_unsigned(nmp->nm_wsize + NFS_MAXXDR);
4878 *tl++ = txdr_unsigned(nmp->nm_rsize + NFS_MAXXDR);
4880 *tl++ = txdr_unsigned(4096); /* Max response size cached */
4881 *tl++ = txdr_unsigned(20); /* Max operations */
4882 *tl++ = txdr_unsigned(64); /* Max slots */
4883 *tl = 0; /* No rdma ird */
4885 /* Fill in back channel attributes. */
4886 NFSM_BUILD(tl, uint32_t *, 7 * NFSX_UNSIGNED);
4887 *tl++ = 0; /* Header pad size */
4888 *tl++ = txdr_unsigned(10000); /* Max request size */
4889 *tl++ = txdr_unsigned(10000); /* Max response size */
4890 *tl++ = txdr_unsigned(4096); /* Max response size cached */
4891 *tl++ = txdr_unsigned(4); /* Max operations */
4892 *tl++ = txdr_unsigned(NFSV4_CBSLOTS); /* Max slots */
4893 *tl = 0; /* No rdma ird */
4895 NFSM_BUILD(tl, uint32_t *, 8 * NFSX_UNSIGNED);
4896 *tl++ = txdr_unsigned(NFS_CALLBCKPROG); /* Call back prog # */
4898 /* Allow AUTH_SYS callbacks as uid, gid == 0. */
4899 *tl++ = txdr_unsigned(1); /* Auth_sys only */
4900 *tl++ = txdr_unsigned(AUTH_SYS); /* AUTH_SYS type */
4901 *tl++ = txdr_unsigned(nfsboottime.tv_sec); /* time stamp */
4902 *tl++ = 0; /* Null machine name */
4903 *tl++ = 0; /* Uid == 0 */
4904 *tl++ = 0; /* Gid == 0 */
4905 *tl = 0; /* No additional gids */
4906 nd->nd_flag |= ND_USEGSSNAME;
4907 error = newnfs_request(nd, nmp, NULL, nrp, NULL, p, cred, NFS_PROG,
4908 NFS_VER4, NULL, 1, NULL, NULL);
4911 if (nd->nd_repstat == 0) {
4912 NFSM_DISSECT(tl, uint32_t *, NFSX_V4SESSIONID +
4914 bcopy(tl, sep->nfsess_sessionid, NFSX_V4SESSIONID);
4915 tl += NFSX_V4SESSIONID / NFSX_UNSIGNED;
4916 sep->nfsess_sequenceid = fxdr_unsigned(uint32_t, *tl++);
4917 crflags = fxdr_unsigned(uint32_t, *tl);
4918 if ((crflags & NFSV4CRSESS_PERSIST) != 0 && mds != 0) {
4920 nmp->nm_state |= NFSSTA_SESSPERSIST;
4924 /* Get the fore channel slot count. */
4925 NFSM_DISSECT(tl, uint32_t *, 7 * NFSX_UNSIGNED);
4926 tl++; /* Skip the header pad size. */
4928 /* Make sure nm_wsize is small enough. */
4929 maxval = fxdr_unsigned(uint32_t, *tl++);
4930 while (maxval < nmp->nm_wsize + NFS_MAXXDR) {
4931 if (nmp->nm_wsize > 8096)
4936 sep->nfsess_maxreq = maxval;
4938 /* Make sure nm_rsize is small enough. */
4939 maxval = fxdr_unsigned(uint32_t, *tl++);
4940 while (maxval < nmp->nm_rsize + NFS_MAXXDR) {
4941 if (nmp->nm_rsize > 8096)
4946 sep->nfsess_maxresp = maxval;
4948 sep->nfsess_maxcache = fxdr_unsigned(int, *tl++);
4950 sep->nfsess_foreslots = fxdr_unsigned(uint16_t, *tl++);
4951 NFSCL_DEBUG(4, "fore slots=%d\n", (int)sep->nfsess_foreslots);
4952 irdcnt = fxdr_unsigned(int, *tl);
4954 NFSM_DISSECT(tl, uint32_t *, irdcnt * NFSX_UNSIGNED);
4956 /* and the back channel slot count. */
4957 NFSM_DISSECT(tl, uint32_t *, 7 * NFSX_UNSIGNED);
4959 sep->nfsess_backslots = fxdr_unsigned(uint16_t, *tl);
4960 NFSCL_DEBUG(4, "back slots=%d\n", (int)sep->nfsess_backslots);
4962 error = nd->nd_repstat;
4964 m_freem(nd->nd_mrep);
4969 * Do the NFSv4.1 Destroy Session.
4972 nfsrpc_destroysession(struct nfsmount *nmp, struct nfsclclient *clp,
4973 struct ucred *cred, NFSPROC_T *p)
4976 struct nfsrv_descript nfsd;
4977 struct nfsrv_descript *nd = &nfsd;
4979 struct nfsclsession *tsep;
4981 nfscl_reqstart(nd, NFSPROC_DESTROYSESSION, nmp, NULL, 0, NULL, NULL, 0,
4983 NFSM_BUILD(tl, uint32_t *, NFSX_V4SESSIONID);
4984 tsep = nfsmnt_mdssession(nmp);
4985 bcopy(tsep->nfsess_sessionid, tl, NFSX_V4SESSIONID);
4986 nd->nd_flag |= ND_USEGSSNAME;
4987 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4988 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4991 error = nd->nd_repstat;
4992 m_freem(nd->nd_mrep);
4997 * Do the NFSv4.1 Destroy Client.
5000 nfsrpc_destroyclient(struct nfsmount *nmp, struct nfsclclient *clp,
5001 struct ucred *cred, NFSPROC_T *p)
5004 struct nfsrv_descript nfsd;
5005 struct nfsrv_descript *nd = &nfsd;
5007 struct nfsclsession *tsep;
5009 nfscl_reqstart(nd, NFSPROC_DESTROYCLIENT, nmp, NULL, 0, NULL, NULL, 0,
5011 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_UNSIGNED);
5012 tsep = nfsmnt_mdssession(nmp);
5013 *tl++ = tsep->nfsess_clientid.lval[0];
5014 *tl = tsep->nfsess_clientid.lval[1];
5015 nd->nd_flag |= ND_USEGSSNAME;
5016 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
5017 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
5020 error = nd->nd_repstat;
5021 m_freem(nd->nd_mrep);
5026 * Do the NFSv4.1 LayoutGet.
5029 nfsrpc_layoutget(struct nfsmount *nmp, uint8_t *fhp, int fhlen, int iomode,
5030 uint64_t offset, uint64_t len, uint64_t minlen, int layouttype,
5031 int layoutlen, nfsv4stateid_t *stateidp, int *retonclosep,
5032 struct nfsclflayouthead *flhp, struct ucred *cred, NFSPROC_T *p,
5035 struct nfsrv_descript nfsd, *nd = &nfsd;
5038 nfscl_reqstart(nd, NFSPROC_LAYOUTGET, nmp, fhp, fhlen, NULL, NULL, 0,
5040 nfsrv_setuplayoutget(nd, iomode, offset, len, minlen, stateidp,
5041 layouttype, layoutlen, 0);
5042 nd->nd_flag |= ND_USEGSSNAME;
5043 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
5044 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
5045 NFSCL_DEBUG(4, "layget err=%d st=%d\n", error, nd->nd_repstat);
5048 if (nd->nd_repstat == 0)
5049 error = nfsrv_parselayoutget(nmp, nd, stateidp, retonclosep,
5051 if (error == 0 && nd->nd_repstat != 0)
5052 error = nd->nd_repstat;
5053 m_freem(nd->nd_mrep);
5058 * Do the NFSv4.1 Get Device Info.
5061 nfsrpc_getdeviceinfo(struct nfsmount *nmp, uint8_t *deviceid, int layouttype,
5062 uint32_t *notifybitsp, struct nfscldevinfo **ndip, struct ucred *cred,
5065 uint32_t cnt, *tl, vers, minorvers;
5066 struct nfsrv_descript nfsd;
5067 struct nfsrv_descript *nd = &nfsd;
5068 struct sockaddr_in sin, ssin;
5069 struct sockaddr_in6 sin6, ssin6;
5070 struct nfsclds *dsp = NULL, **dspp, **gotdspp;
5071 struct nfscldevinfo *ndi;
5072 int addrcnt = 0, bitcnt, error, gotminor, gotvers, i, isudp, j;
5074 uint8_t stripeindex;
5075 sa_family_t af, safilled;
5077 ssin.sin_port = 0; /* To shut up compiler. */
5078 ssin.sin_addr.s_addr = 0; /* ditto */
5082 nfscl_reqstart(nd, NFSPROC_GETDEVICEINFO, nmp, NULL, 0, NULL, NULL, 0,
5084 NFSM_BUILD(tl, uint32_t *, NFSX_V4DEVICEID + 3 * NFSX_UNSIGNED);
5085 NFSBCOPY(deviceid, tl, NFSX_V4DEVICEID);
5086 tl += (NFSX_V4DEVICEID / NFSX_UNSIGNED);
5087 *tl++ = txdr_unsigned(layouttype);
5088 *tl++ = txdr_unsigned(100000);
5089 if (notifybitsp != NULL && *notifybitsp != 0) {
5090 *tl = txdr_unsigned(1); /* One word of bits. */
5091 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
5092 *tl = txdr_unsigned(*notifybitsp);
5094 *tl = txdr_unsigned(0);
5095 nd->nd_flag |= ND_USEGSSNAME;
5096 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
5097 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
5100 if (nd->nd_repstat == 0) {
5101 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
5102 if (layouttype != fxdr_unsigned(int, *tl))
5103 printf("EEK! devinfo layout type not same!\n");
5104 if (layouttype == NFSLAYOUT_NFSV4_1_FILES) {
5105 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
5106 stripecnt = fxdr_unsigned(int, *tl);
5107 NFSCL_DEBUG(4, "stripecnt=%d\n", stripecnt);
5108 if (stripecnt < 1 || stripecnt > 4096) {
5109 printf("pNFS File layout devinfo stripecnt %d:"
5110 " out of range\n", stripecnt);
5111 error = NFSERR_BADXDR;
5114 NFSM_DISSECT(tl, uint32_t *, (stripecnt + 1) *
5116 addrcnt = fxdr_unsigned(int, *(tl + stripecnt));
5117 NFSCL_DEBUG(4, "addrcnt=%d\n", addrcnt);
5118 if (addrcnt < 1 || addrcnt > 128) {
5119 printf("NFS devinfo addrcnt %d: out of range\n",
5121 error = NFSERR_BADXDR;
5126 * Now we know how many stripe indices and addresses, so
5127 * we can allocate the structure the correct size.
5129 i = (stripecnt * sizeof(uint8_t)) /
5130 sizeof(struct nfsclds *) + 1;
5131 NFSCL_DEBUG(4, "stripeindices=%d\n", i);
5132 ndi = malloc(sizeof(*ndi) + (addrcnt + i) *
5133 sizeof(struct nfsclds *), M_NFSDEVINFO, M_WAITOK |
5135 NFSBCOPY(deviceid, ndi->nfsdi_deviceid,
5137 ndi->nfsdi_refcnt = 0;
5138 ndi->nfsdi_flags = NFSDI_FILELAYOUT;
5139 ndi->nfsdi_stripecnt = stripecnt;
5140 ndi->nfsdi_addrcnt = addrcnt;
5141 /* Fill in the stripe indices. */
5142 for (i = 0; i < stripecnt; i++) {
5143 stripeindex = fxdr_unsigned(uint8_t, *tl++);
5144 NFSCL_DEBUG(4, "stripeind=%d\n", stripeindex);
5145 if (stripeindex >= addrcnt) {
5146 printf("pNFS File Layout devinfo"
5147 " stripeindex %d: too big\n",
5149 error = NFSERR_BADXDR;
5152 nfsfldi_setstripeindex(ndi, i, stripeindex);
5154 } else if (layouttype == NFSLAYOUT_FLEXFILE) {
5155 /* For Flex File, we only get one address list. */
5156 ndi = malloc(sizeof(*ndi) + sizeof(struct nfsclds *),
5157 M_NFSDEVINFO, M_WAITOK | M_ZERO);
5158 NFSBCOPY(deviceid, ndi->nfsdi_deviceid,
5160 ndi->nfsdi_refcnt = 0;
5161 ndi->nfsdi_flags = NFSDI_FLEXFILE;
5162 addrcnt = ndi->nfsdi_addrcnt = 1;
5165 /* Now, dissect the server address(es). */
5166 safilled = AF_UNSPEC;
5167 for (i = 0; i < addrcnt; i++) {
5168 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
5169 cnt = fxdr_unsigned(uint32_t, *tl);
5171 printf("NFS devinfo 0 len addrlist\n");
5172 error = NFSERR_BADXDR;
5175 dspp = nfsfldi_addr(ndi, i);
5176 safilled = AF_UNSPEC;
5177 for (j = 0; j < cnt; j++) {
5178 error = nfsv4_getipaddr(nd, &sin, &sin6, &af,
5180 if (error != 0 && error != EPERM) {
5181 error = NFSERR_BADXDR;
5184 if (error == 0 && isudp == 0) {
5187 * - Same address family.
5188 * Save the address and dspp, so that
5189 * the connection can be done after
5190 * parsing is complete.
5192 if (safilled == AF_UNSPEC ||
5193 (af == nmp->nm_nam->sa_family &&
5194 safilled != nmp->nm_nam->sa_family)
5207 gotvers = NFS_VER4; /* Default NFSv4.1 for File Layout. */
5208 gotminor = NFSV41_MINORVERSION;
5209 /* For Flex File, we will take one of the versions to use. */
5210 if (layouttype == NFSLAYOUT_FLEXFILE) {
5211 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
5212 j = fxdr_unsigned(int, *tl);
5213 if (j < 1 || j > NFSDEV_MAXVERS) {
5214 printf("pNFS: too many versions\n");
5215 error = NFSERR_BADXDR;
5220 for (i = 0; i < j; i++) {
5221 NFSM_DISSECT(tl, uint32_t *, 5 * NFSX_UNSIGNED);
5222 vers = fxdr_unsigned(uint32_t, *tl++);
5223 minorvers = fxdr_unsigned(uint32_t, *tl++);
5224 if (vers == NFS_VER3)
5226 if ((vers == NFS_VER4 && ((minorvers ==
5227 NFSV41_MINORVERSION && gotminor == 0) ||
5228 minorvers == NFSV42_MINORVERSION)) ||
5229 (vers == NFS_VER3 && gotvers == 0)) {
5231 gotminor = minorvers;
5232 /* We'll take this one. */
5233 ndi->nfsdi_versindex = i;
5234 ndi->nfsdi_vers = vers;
5235 ndi->nfsdi_minorvers = minorvers;
5236 ndi->nfsdi_rsize = fxdr_unsigned(
5238 ndi->nfsdi_wsize = fxdr_unsigned(
5240 if (*tl == newnfs_true)
5245 ~NFSDI_TIGHTCOUPLED;
5249 printf("pNFS: no NFSv3, NFSv4.1 or NFSv4.2\n");
5250 error = NFSERR_BADXDR;
5255 /* And the notify bits. */
5256 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
5257 bitcnt = fxdr_unsigned(int, *tl);
5259 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
5260 if (notifybitsp != NULL)
5262 fxdr_unsigned(uint32_t, *tl);
5264 if (safilled != AF_UNSPEC) {
5265 KASSERT(ndi != NULL, ("ndi is NULL"));
5271 * Now we can do a TCP connection for the correct
5272 * NFS version and IP address.
5274 error = nfsrpc_fillsa(nmp, &ssin, &ssin6, safilled,
5275 gotvers, gotminor, &dsp, p);
5278 KASSERT(gotdspp != NULL, ("gotdspp is NULL"));
5282 if (nd->nd_repstat != 0 && error == 0)
5283 error = nd->nd_repstat;
5285 if (error != 0 && ndi != NULL)
5286 nfscl_freedevinfo(ndi);
5287 m_freem(nd->nd_mrep);
5292 * Do the NFSv4.1 LayoutCommit.
5295 nfsrpc_layoutcommit(struct nfsmount *nmp, uint8_t *fh, int fhlen, int reclaim,
5296 uint64_t off, uint64_t len, uint64_t lastbyte, nfsv4stateid_t *stateidp,
5297 int layouttype, struct ucred *cred, NFSPROC_T *p, void *stuff)
5300 struct nfsrv_descript nfsd, *nd = &nfsd;
5303 nfscl_reqstart(nd, NFSPROC_LAYOUTCOMMIT, nmp, fh, fhlen, NULL, NULL,
5305 NFSM_BUILD(tl, uint32_t *, 5 * NFSX_UNSIGNED + 3 * NFSX_HYPER +
5307 txdr_hyper(off, tl);
5309 txdr_hyper(len, tl);
5312 *tl++ = newnfs_true;
5314 *tl++ = newnfs_false;
5315 *tl++ = txdr_unsigned(stateidp->seqid);
5316 *tl++ = stateidp->other[0];
5317 *tl++ = stateidp->other[1];
5318 *tl++ = stateidp->other[2];
5319 *tl++ = newnfs_true;
5322 else if (lastbyte >= (off + len))
5323 lastbyte = off + len - 1;
5324 txdr_hyper(lastbyte, tl);
5326 *tl++ = newnfs_false;
5327 *tl++ = txdr_unsigned(layouttype);
5328 /* All supported layouts are 0 length. */
5329 *tl = txdr_unsigned(0);
5330 nd->nd_flag |= ND_USEGSSNAME;
5331 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
5332 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
5335 error = nd->nd_repstat;
5336 m_freem(nd->nd_mrep);
5341 * Do the NFSv4.1 LayoutReturn.
5344 nfsrpc_layoutreturn(struct nfsmount *nmp, uint8_t *fh, int fhlen, int reclaim,
5345 int layouttype, uint32_t iomode, int layoutreturn, uint64_t offset,
5346 uint64_t len, nfsv4stateid_t *stateidp, struct ucred *cred, NFSPROC_T *p,
5347 uint32_t stat, uint32_t op, char *devid)
5350 struct nfsrv_descript nfsd, *nd = &nfsd;
5354 nfscl_reqstart(nd, NFSPROC_LAYOUTRETURN, nmp, fh, fhlen, NULL, NULL,
5356 NFSM_BUILD(tl, uint32_t *, 4 * NFSX_UNSIGNED);
5358 *tl++ = newnfs_true;
5360 *tl++ = newnfs_false;
5361 *tl++ = txdr_unsigned(layouttype);
5362 *tl++ = txdr_unsigned(iomode);
5363 *tl = txdr_unsigned(layoutreturn);
5364 if (layoutreturn == NFSLAYOUTRETURN_FILE) {
5365 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_HYPER + NFSX_STATEID +
5367 txdr_hyper(offset, tl);
5369 txdr_hyper(len, tl);
5371 NFSCL_DEBUG(4, "layoutret stseq=%d\n", (int)stateidp->seqid);
5372 *tl++ = txdr_unsigned(stateidp->seqid);
5373 *tl++ = stateidp->other[0];
5374 *tl++ = stateidp->other[1];
5375 *tl++ = stateidp->other[2];
5376 if (layouttype == NFSLAYOUT_NFSV4_1_FILES)
5377 *tl = txdr_unsigned(0);
5378 else if (layouttype == NFSLAYOUT_FLEXFILE) {
5380 *tl = txdr_unsigned(2 * NFSX_HYPER +
5381 NFSX_STATEID + NFSX_V4DEVICEID + 5 *
5383 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_HYPER +
5384 NFSX_STATEID + NFSX_V4DEVICEID + 5 *
5386 *tl++ = txdr_unsigned(1); /* One error. */
5387 tu64 = 0; /* Offset. */
5388 txdr_hyper(tu64, tl); tl += 2;
5389 tu64 = UINT64_MAX; /* Length. */
5390 txdr_hyper(tu64, tl); tl += 2;
5391 NFSBCOPY(stateidp, tl, NFSX_STATEID);
5392 tl += (NFSX_STATEID / NFSX_UNSIGNED);
5393 *tl++ = txdr_unsigned(1); /* One error. */
5394 NFSBCOPY(devid, tl, NFSX_V4DEVICEID);
5395 tl += (NFSX_V4DEVICEID / NFSX_UNSIGNED);
5396 *tl++ = txdr_unsigned(stat);
5397 *tl++ = txdr_unsigned(op);
5399 *tl = txdr_unsigned(2 * NFSX_UNSIGNED);
5400 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_UNSIGNED);
5404 *tl = 0; /* No stats yet. */
5407 nd->nd_flag |= ND_USEGSSNAME;
5408 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
5409 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
5412 if (nd->nd_repstat == 0) {
5413 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
5415 NFSM_DISSECT(tl, uint32_t *, NFSX_STATEID);
5416 stateidp->seqid = fxdr_unsigned(uint32_t, *tl++);
5417 stateidp->other[0] = *tl++;
5418 stateidp->other[1] = *tl++;
5419 stateidp->other[2] = *tl;
5422 error = nd->nd_repstat;
5424 m_freem(nd->nd_mrep);
5429 * Acquire a layout and devinfo, if possible. The caller must have acquired
5430 * a reference count on the nfsclclient structure before calling this.
5431 * Return the layout in lypp with a reference count on it, if successful.
5434 nfsrpc_getlayout(struct nfsmount *nmp, vnode_t vp, struct nfsfh *nfhp,
5435 int iomode, uint32_t *notifybitsp, nfsv4stateid_t *stateidp, uint64_t off,
5436 struct nfscllayout **lypp, struct ucred *cred, NFSPROC_T *p)
5438 struct nfscllayout *lyp;
5439 struct nfsclflayout *flp;
5440 struct nfsclflayouthead flh;
5441 int error = 0, islocked, layoutlen, layouttype, recalled, retonclose;
5442 nfsv4stateid_t stateid;
5443 struct nfsclsession *tsep;
5446 if (NFSHASFLEXFILE(nmp))
5447 layouttype = NFSLAYOUT_FLEXFILE;
5449 layouttype = NFSLAYOUT_NFSV4_1_FILES;
5451 * If lyp is returned non-NULL, there will be a refcnt (shared lock)
5452 * on it, iff flp != NULL or a lock (exclusive lock) on it iff
5455 lyp = nfscl_getlayout(nmp->nm_clp, nfhp->nfh_fh, nfhp->nfh_len,
5456 off, &flp, &recalled);
5458 if (lyp == NULL || flp == NULL) {
5462 tsep = nfsmnt_mdssession(nmp);
5463 layoutlen = tsep->nfsess_maxcache -
5464 (NFSX_STATEID + 3 * NFSX_UNSIGNED);
5467 stateid.other[0] = stateidp->other[0];
5468 stateid.other[1] = stateidp->other[1];
5469 stateid.other[2] = stateidp->other[2];
5470 error = nfsrpc_layoutget(nmp, nfhp->nfh_fh,
5471 nfhp->nfh_len, iomode, (uint64_t)0, UINT64_MAX,
5472 (uint64_t)0, layouttype, layoutlen, &stateid,
5473 &retonclose, &flh, cred, p, NULL);
5476 stateid.seqid = lyp->nfsly_stateid.seqid;
5477 stateid.other[0] = lyp->nfsly_stateid.other[0];
5478 stateid.other[1] = lyp->nfsly_stateid.other[1];
5479 stateid.other[2] = lyp->nfsly_stateid.other[2];
5480 error = nfsrpc_layoutget(nmp, nfhp->nfh_fh,
5481 nfhp->nfh_len, iomode, off, UINT64_MAX,
5482 (uint64_t)0, layouttype, layoutlen, &stateid,
5483 &retonclose, &flh, cred, p, NULL);
5485 error = nfsrpc_layoutgetres(nmp, vp, nfhp->nfh_fh,
5486 nfhp->nfh_len, &stateid, retonclose, notifybitsp, &lyp,
5487 &flh, layouttype, error, NULL, cred, p);
5490 else if (islocked != 0)
5491 nfscl_rellayout(lyp, 1);
5498 * Do a TCP connection plus exchange id and create session.
5499 * If successful, a "struct nfsclds" is linked into the list for the
5500 * mount point and a pointer to it is returned.
5503 nfsrpc_fillsa(struct nfsmount *nmp, struct sockaddr_in *sin,
5504 struct sockaddr_in6 *sin6, sa_family_t af, int vers, int minorvers,
5505 struct nfsclds **dspp, NFSPROC_T *p)
5507 struct sockaddr_in *msad, *sad;
5508 struct sockaddr_in6 *msad6, *sad6;
5509 struct nfsclclient *clp;
5510 struct nfssockreq *nrp;
5511 struct nfsclds *dsp, *tdsp;
5512 int error, firsttry;
5513 enum nfsclds_state retv;
5514 uint32_t sequenceid = 0;
5516 KASSERT(nmp->nm_sockreq.nr_cred != NULL,
5517 ("nfsrpc_fillsa: NULL nr_cred"));
5523 if (af == AF_INET) {
5526 * Check to see if we already have a session for this
5527 * address that is usable for a DS.
5528 * Note that the MDS's address is in a different place
5529 * than the sessions already acquired for DS's.
5531 msad = (struct sockaddr_in *)nmp->nm_sockreq.nr_nam;
5532 tdsp = TAILQ_FIRST(&nmp->nm_sess);
5533 while (tdsp != NULL) {
5534 if (msad != NULL && msad->sin_family == AF_INET &&
5535 sin->sin_addr.s_addr == msad->sin_addr.s_addr &&
5536 sin->sin_port == msad->sin_port &&
5537 (tdsp->nfsclds_flags & NFSCLDS_DS) != 0 &&
5538 tdsp->nfsclds_sess.nfsess_defunct == 0) {
5541 NFSCL_DEBUG(4, "fnd same addr\n");
5544 tdsp = TAILQ_NEXT(tdsp, nfsclds_list);
5545 if (tdsp != NULL && tdsp->nfsclds_sockp != NULL)
5546 msad = (struct sockaddr_in *)
5547 tdsp->nfsclds_sockp->nr_nam;
5553 /* No IP address match, so look for new/trunked one. */
5554 sad = malloc(sizeof(*sad), M_SONAME, M_WAITOK | M_ZERO);
5555 sad->sin_len = sizeof(*sad);
5556 sad->sin_family = AF_INET;
5557 sad->sin_port = sin->sin_port;
5558 sad->sin_addr.s_addr = sin->sin_addr.s_addr;
5559 nrp = malloc(sizeof(*nrp), M_NFSSOCKREQ, M_WAITOK | M_ZERO);
5560 nrp->nr_nam = (struct sockaddr *)sad;
5561 } else if (af == AF_INET6) {
5564 * Check to see if we already have a session for this
5565 * address that is usable for a DS.
5566 * Note that the MDS's address is in a different place
5567 * than the sessions already acquired for DS's.
5569 msad6 = (struct sockaddr_in6 *)nmp->nm_sockreq.nr_nam;
5570 tdsp = TAILQ_FIRST(&nmp->nm_sess);
5571 while (tdsp != NULL) {
5572 if (msad6 != NULL && msad6->sin6_family == AF_INET6 &&
5573 IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr,
5574 &msad6->sin6_addr) &&
5575 sin6->sin6_port == msad6->sin6_port &&
5576 (tdsp->nfsclds_flags & NFSCLDS_DS) != 0 &&
5577 tdsp->nfsclds_sess.nfsess_defunct == 0) {
5582 tdsp = TAILQ_NEXT(tdsp, nfsclds_list);
5583 if (tdsp != NULL && tdsp->nfsclds_sockp != NULL)
5584 msad6 = (struct sockaddr_in6 *)
5585 tdsp->nfsclds_sockp->nr_nam;
5591 /* No IP address match, so look for new/trunked one. */
5592 sad6 = malloc(sizeof(*sad6), M_SONAME, M_WAITOK | M_ZERO);
5593 sad6->sin6_len = sizeof(*sad6);
5594 sad6->sin6_family = AF_INET6;
5595 sad6->sin6_port = sin6->sin6_port;
5596 NFSBCOPY(&sin6->sin6_addr, &sad6->sin6_addr,
5597 sizeof(struct in6_addr));
5598 nrp = malloc(sizeof(*nrp), M_NFSSOCKREQ, M_WAITOK | M_ZERO);
5599 nrp->nr_nam = (struct sockaddr *)sad6;
5603 nrp->nr_sotype = SOCK_STREAM;
5604 mtx_init(&nrp->nr_mtx, "nfssock", NULL, MTX_DEF);
5605 nrp->nr_prog = NFS_PROG;
5606 nrp->nr_vers = vers;
5609 * Use the credentials that were used for the mount, which are
5610 * in nmp->nm_sockreq.nr_cred for newnfs_connect() etc.
5611 * Ref. counting the credentials with crhold() is probably not
5612 * necessary, since nm_sockreq.nr_cred won't be crfree()'d until
5613 * unmount, but I did it anyhow.
5615 nrp->nr_cred = crhold(nmp->nm_sockreq.nr_cred);
5616 error = newnfs_connect(nmp, nrp, NULL, p, 0);
5617 NFSCL_DEBUG(3, "DS connect=%d\n", error);
5620 /* Now, do the exchangeid and create session. */
5622 if (vers == NFS_VER4) {
5625 error = nfsrpc_exchangeid(nmp, clp, nrp,
5626 minorvers, NFSV4EXCH_USEPNFSDS, &dsp,
5628 NFSCL_DEBUG(3, "DS exchangeid=%d\n", error);
5629 if (error == NFSERR_MINORVERMISMATCH)
5630 minorvers = NFSV42_MINORVERSION;
5631 } while (error == NFSERR_MINORVERMISMATCH &&
5634 newnfs_disconnect(nrp);
5636 dsp = malloc(sizeof(struct nfsclds), M_NFSCLDS,
5638 dsp->nfsclds_flags |= NFSCLDS_DS;
5639 dsp->nfsclds_expire = INT32_MAX; /* No renews needed. */
5640 mtx_init(&dsp->nfsclds_mtx, "nfsds", NULL, MTX_DEF);
5641 mtx_init(&dsp->nfsclds_sess.nfsess_mtx, "nfssession",
5646 dsp->nfsclds_sockp = nrp;
5647 if (vers == NFS_VER4) {
5649 retv = nfscl_getsameserver(nmp, dsp, &tdsp,
5651 NFSCL_DEBUG(3, "getsame ret=%d\n", retv);
5652 if (retv == NFSDSP_USETHISSESSION &&
5653 nfscl_dssameconn != 0) {
5655 tdsp->nfsclds_flags |= NFSCLDS_SAMECONN;
5659 * If there is already a session for this
5662 (void)newnfs_disconnect(nrp);
5663 nfscl_freenfsclds(dsp);
5667 if (retv == NFSDSP_NOTFOUND)
5669 dsp->nfsclds_sess.nfsess_sequenceid;
5671 error = nfsrpc_createsession(nmp, &dsp->nfsclds_sess,
5672 nrp, dsp, sequenceid, 0, nrp->nr_cred, p);
5673 NFSCL_DEBUG(3, "DS createsess=%d\n", error);
5676 NFSFREECRED(nrp->nr_cred);
5677 NFSFREEMUTEX(&nrp->nr_mtx);
5678 free(nrp->nr_nam, M_SONAME);
5679 free(nrp, M_NFSSOCKREQ);
5682 NFSCL_DEBUG(3, "add DS session\n");
5684 * Put it at the end of the list. That way the list
5685 * is ordered by when the entry was added. This matters
5686 * since the one done first is the one that should be
5687 * used for sequencid'ing any subsequent create sessions.
5690 TAILQ_INSERT_TAIL(&nmp->nm_sess, dsp, nfsclds_list);
5693 } else if (dsp != NULL) {
5694 newnfs_disconnect(nrp);
5695 nfscl_freenfsclds(dsp);
5701 * Do the NFSv4.1 Reclaim Complete.
5704 nfsrpc_reclaimcomplete(struct nfsmount *nmp, struct ucred *cred, NFSPROC_T *p)
5707 struct nfsrv_descript nfsd;
5708 struct nfsrv_descript *nd = &nfsd;
5711 nfscl_reqstart(nd, NFSPROC_RECLAIMCOMPL, nmp, NULL, 0, NULL, NULL, 0,
5713 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
5715 nd->nd_flag |= ND_USEGSSNAME;
5716 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
5717 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
5720 error = nd->nd_repstat;
5721 m_freem(nd->nd_mrep);
5726 * Initialize the slot tables for a session.
5729 nfscl_initsessionslots(struct nfsclsession *sep)
5733 for (i = 0; i < NFSV4_CBSLOTS; i++) {
5734 if (sep->nfsess_cbslots[i].nfssl_reply != NULL)
5735 m_freem(sep->nfsess_cbslots[i].nfssl_reply);
5736 NFSBZERO(&sep->nfsess_cbslots[i], sizeof(struct nfsslot));
5738 for (i = 0; i < 64; i++)
5739 sep->nfsess_slotseq[i] = 0;
5740 sep->nfsess_slots = 0;
5744 * Called to try and do an I/O operation via an NFSv4.1 Data Server (DS).
5747 nfscl_doiods(vnode_t vp, struct uio *uiop, int *iomode, int *must_commit,
5748 uint32_t rwaccess, int docommit, struct ucred *cred, NFSPROC_T *p)
5750 struct nfsnode *np = VTONFS(vp);
5751 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
5752 struct nfscllayout *layp;
5753 struct nfscldevinfo *dip;
5754 struct nfsclflayout *rflp;
5756 struct nfsclwritedsdorpc *drpc, *tdrpc;
5757 nfsv4stateid_t stateid;
5758 struct ucred *newcred;
5759 uint64_t lastbyte, len, off, oresid, xfer;
5760 int eof, error, firstmirror, i, iolaymode, mirrorcnt, recalled, timo;
5763 void *iovbase = NULL;
5768 if (!NFSHASPNFS(nmp) || nfscl_enablecallb == 0 || nfs_numnfscbd == 0 ||
5769 (np->n_flag & NNOLAYOUT) != 0)
5771 /* Now, get a reference cnt on the clientid for this mount. */
5772 if (nfscl_getref(nmp) == 0)
5775 /* Find an appropriate stateid. */
5776 newcred = NFSNEWCRED(cred);
5777 error = nfscl_getstateid(vp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len,
5778 rwaccess, 1, newcred, p, &stateid, &lckp);
5780 NFSFREECRED(newcred);
5784 /* Search for a layout for this file. */
5785 off = uiop->uio_offset;
5786 layp = nfscl_getlayout(nmp->nm_clp, np->n_fhp->nfh_fh,
5787 np->n_fhp->nfh_len, off, &rflp, &recalled);
5788 if (layp == NULL || rflp == NULL) {
5789 if (recalled != 0) {
5790 NFSFREECRED(newcred);
5795 nfscl_rellayout(layp, (rflp == NULL) ? 1 : 0);
5798 /* Try and get a Layout, if it is supported. */
5799 if (rwaccess == NFSV4OPEN_ACCESSWRITE ||
5800 (np->n_flag & NWRITEOPENED) != 0)
5801 iolaymode = NFSLAYOUTIOMODE_RW;
5803 iolaymode = NFSLAYOUTIOMODE_READ;
5804 error = nfsrpc_getlayout(nmp, vp, np->n_fhp, iolaymode,
5805 NULL, &stateid, off, &layp, newcred, p);
5808 np->n_flag |= NNOLAYOUT;
5811 nfscl_lockderef(lckp);
5812 NFSFREECRED(newcred);
5814 nfscl_rellayout(layp, 0);
5821 * Loop around finding a layout that works for the first part of
5822 * this I/O operation, and then call the function that actually
5826 len = (uint64_t)uiop->uio_resid;
5827 while (len > 0 && error == 0 && eof == 0) {
5828 off = uiop->uio_offset;
5829 error = nfscl_findlayoutforio(layp, off, rwaccess, &rflp);
5831 oresid = xfer = (uint64_t)uiop->uio_resid;
5832 if (xfer > (rflp->nfsfl_end - rflp->nfsfl_off))
5833 xfer = rflp->nfsfl_end - rflp->nfsfl_off;
5835 * For Flex File layout with mirrored DSs, select one
5836 * of them at random for reads. For writes and commits,
5840 tdrpc = drpc = NULL;
5843 if ((layp->nfsly_flags & NFSLY_FLEXFILE) != 0 &&
5844 (mirrorcnt = rflp->nfsfl_mirrorcnt) > 1) {
5845 if (rwaccess == NFSV4OPEN_ACCESSREAD) {
5846 firstmirror = arc4random() % mirrorcnt;
5847 mirrorcnt = firstmirror + 1;
5849 if (docommit == 0) {
5851 * Save values, so uiop can be
5852 * rolled back upon a write
5855 offs = uiop->uio_offset;
5856 resid = uiop->uio_resid;
5858 uiop->uio_iov->iov_base;
5859 iovlen = uiop->uio_iov->iov_len;
5860 m = nfsm_uiombuflist(uiop, len,
5863 tdrpc = drpc = malloc(sizeof(*drpc) *
5864 (mirrorcnt - 1), M_TEMP, M_WAITOK |
5868 for (i = firstmirror; i < mirrorcnt && error == 0; i++){
5869 if ((layp->nfsly_flags & NFSLY_FLEXFILE) != 0) {
5870 dev = rflp->nfsfl_ffm[i].dev;
5871 dip = nfscl_getdevinfo(nmp->nm_clp, dev,
5872 rflp->nfsfl_ffm[i].devp);
5874 dev = rflp->nfsfl_dev;
5875 dip = nfscl_getdevinfo(nmp->nm_clp, dev,
5879 if ((rflp->nfsfl_flags & NFSFL_FLEXFILE)
5881 error = nfscl_dofflayoutio(vp,
5882 uiop, iomode, must_commit,
5883 &eof, &stateid, rwaccess,
5884 dip, layp, rflp, off, xfer,
5885 i, docommit, m, tdrpc,
5888 error = nfscl_doflayoutio(vp,
5889 uiop, iomode, must_commit,
5890 &eof, &stateid, rwaccess,
5891 dip, layp, rflp, off, xfer,
5892 docommit, newcred, p);
5893 nfscl_reldevinfo(dip);
5901 timo = hz / 50; /* Wait for 20msec. */
5904 for (i = firstmirror; i < mirrorcnt - 1 &&
5905 tdrpc != NULL; i++, tdrpc++) {
5907 * For the unused drpc entries, both inprog and
5908 * err == 0, so this loop won't break.
5910 while (tdrpc->inprog != 0 && tdrpc->done == 0)
5911 tsleep(&tdrpc->tsk, PVFS, "clrpcio",
5913 if (error == 0 && tdrpc->err != 0)
5918 if (mirrorcnt > 1 && rwaccess ==
5919 NFSV4OPEN_ACCESSWRITE && docommit == 0) {
5921 layp->nfsly_flags |= NFSLY_WRITTEN;
5924 lastbyte = off + xfer - 1;
5926 if (lastbyte > layp->nfsly_lastbyte)
5927 layp->nfsly_lastbyte = lastbyte;
5929 } else if (error == NFSERR_OPENMODE &&
5930 rwaccess == NFSV4OPEN_ACCESSREAD) {
5932 nmp->nm_state |= NFSSTA_OPENMODE;
5937 len -= (oresid - (uint64_t)uiop->uio_resid);
5938 else if (mirrorcnt > 1 && rwaccess ==
5939 NFSV4OPEN_ACCESSWRITE && docommit == 0) {
5941 * In case the rpc gets retried, roll the
5942 * uio fields changed by nfsm_uiombuflist()
5945 uiop->uio_offset = offs;
5946 uiop->uio_resid = resid;
5947 uiop->uio_iov->iov_base = iovbase;
5948 uiop->uio_iov->iov_len = iovlen;
5953 nfscl_lockderef(lckp);
5954 NFSFREECRED(newcred);
5955 nfscl_rellayout(layp, 0);
5961 * Make a copy of the mbuf chain and add an mbuf for null padding, as required.
5963 static struct mbuf *
5964 nfsm_copym(struct mbuf *m, int off, int xfer)
5966 struct mbuf *m2, *m3, *m4;
5970 m2 = m_copym(m, off, xfer, M_WAITOK);
5971 rem = NFSM_RNDUP(xfer) - xfer;
5974 * The zero padding to a multiple of 4 bytes is required by
5975 * the XDR. So that the mbufs copied by reference aren't
5976 * modified, add an mbuf with the zero'd bytes to the list.
5977 * rem will be a maximum of 3, so one zero'd uint32_t is
5981 while (m3->m_next != NULL)
5984 tl = mtod(m4, uint32_t *);
5993 * Find a file layout that will handle the first bytes of the requested
5994 * range and return the information from it needed to the I/O operation.
5997 nfscl_findlayoutforio(struct nfscllayout *lyp, uint64_t off, uint32_t rwaccess,
5998 struct nfsclflayout **retflpp)
6000 struct nfsclflayout *flp, *nflp, *rflp;
6005 /* For reading, do the Read list first and then the Write list. */
6007 if (rw == NFSV4OPEN_ACCESSREAD)
6008 flp = LIST_FIRST(&lyp->nfsly_flayread);
6010 flp = LIST_FIRST(&lyp->nfsly_flayrw);
6011 while (flp != NULL) {
6012 nflp = LIST_NEXT(flp, nfsfl_list);
6013 if (flp->nfsfl_off > off)
6015 if (flp->nfsfl_end > off &&
6016 (rflp == NULL || rflp->nfsfl_end < flp->nfsfl_end))
6020 if (rw == NFSV4OPEN_ACCESSREAD)
6021 rw = NFSV4OPEN_ACCESSWRITE;
6026 /* This one covers the most bytes starting at off. */
6034 * Do I/O using an NFSv4.1 or NFSv4.2 file layout.
6037 nfscl_doflayoutio(vnode_t vp, struct uio *uiop, int *iomode, int *must_commit,
6038 int *eofp, nfsv4stateid_t *stateidp, int rwflag, struct nfscldevinfo *dp,
6039 struct nfscllayout *lyp, struct nfsclflayout *flp, uint64_t off,
6040 uint64_t len, int docommit, struct ucred *cred, NFSPROC_T *p)
6042 uint64_t io_off, rel_off, stripe_unit_size, transfer, xfer;
6043 int commit_thru_mds, error, stripe_index, stripe_pos, minorvers;
6046 struct nfsclds **dspp;
6049 rel_off = off - flp->nfsfl_patoff;
6050 stripe_unit_size = (flp->nfsfl_util >> 6) & 0x3ffffff;
6051 stripe_pos = (rel_off / stripe_unit_size + flp->nfsfl_stripe1) %
6052 dp->nfsdi_stripecnt;
6053 transfer = stripe_unit_size - (rel_off % stripe_unit_size);
6056 /* Loop around, doing I/O for each stripe unit. */
6057 while (len > 0 && error == 0) {
6058 stripe_index = nfsfldi_stripeindex(dp, stripe_pos);
6059 dspp = nfsfldi_addr(dp, stripe_index);
6060 if (((*dspp)->nfsclds_flags & NFSCLDS_MINORV2) != 0)
6061 minorvers = NFSV42_MINORVERSION;
6063 minorvers = NFSV41_MINORVERSION;
6064 if (len > transfer && docommit == 0)
6068 if ((flp->nfsfl_util & NFSFLAYUTIL_DENSE) != 0) {
6070 if (stripe_pos >= flp->nfsfl_fhcnt)
6072 fhp = flp->nfsfl_fh[stripe_pos];
6073 io_off = (rel_off / (stripe_unit_size *
6074 dp->nfsdi_stripecnt)) * stripe_unit_size +
6075 rel_off % stripe_unit_size;
6077 /* Sparse layout. */
6078 if (flp->nfsfl_fhcnt > 1) {
6079 if (stripe_index >= flp->nfsfl_fhcnt)
6081 fhp = flp->nfsfl_fh[stripe_index];
6082 } else if (flp->nfsfl_fhcnt == 1)
6083 fhp = flp->nfsfl_fh[0];
6088 if ((flp->nfsfl_util & NFSFLAYUTIL_COMMIT_THRU_MDS) != 0) {
6089 commit_thru_mds = 1;
6093 commit_thru_mds = 0;
6095 np->n_flag |= NDSCOMMIT;
6098 if (docommit != 0) {
6100 error = nfsrpc_commitds(vp, io_off, xfer,
6101 *dspp, fhp, NFS_VER4, minorvers, cred, p);
6104 * Set both eof and uio_resid = 0 to end any
6108 uiop->uio_resid = 0;
6111 np->n_flag &= ~NDSCOMMIT;
6114 } else if (rwflag == NFSV4OPEN_ACCESSREAD)
6115 error = nfsrpc_readds(vp, uiop, stateidp, eofp, *dspp,
6116 io_off, xfer, fhp, 0, NFS_VER4, minorvers, cred, p);
6118 error = nfsrpc_writeds(vp, uiop, iomode, must_commit,
6119 stateidp, *dspp, io_off, xfer, fhp, commit_thru_mds,
6120 0, NFS_VER4, minorvers, cred, p);
6123 lyp->nfsly_flags |= NFSLY_WRITTEN;
6128 transfer = stripe_unit_size;
6129 stripe_pos = (stripe_pos + 1) % dp->nfsdi_stripecnt;
6138 * Do I/O using an NFSv4.1 flex file layout.
6141 nfscl_dofflayoutio(vnode_t vp, struct uio *uiop, int *iomode, int *must_commit,
6142 int *eofp, nfsv4stateid_t *stateidp, int rwflag, struct nfscldevinfo *dp,
6143 struct nfscllayout *lyp, struct nfsclflayout *flp, uint64_t off,
6144 uint64_t len, int mirror, int docommit, struct mbuf *mp,
6145 struct nfsclwritedsdorpc *drpc, struct ucred *cred, NFSPROC_T *p)
6147 uint64_t transfer, xfer;
6151 struct nfsclds **dspp;
6152 struct ucred *tcred;
6158 NFSCL_DEBUG(4, "nfscl_dofflayoutio: off=%ju len=%ju\n", (uintmax_t)off,
6160 /* Loop around, doing I/O for each stripe unit. */
6161 while (len > 0 && error == 0) {
6162 dspp = nfsfldi_addr(dp, 0);
6163 fhp = flp->nfsfl_ffm[mirror].fh[dp->nfsdi_versindex];
6164 stateidp = &flp->nfsfl_ffm[mirror].st;
6165 NFSCL_DEBUG(4, "mirror=%d vind=%d fhlen=%d st.seqid=0x%x\n",
6166 mirror, dp->nfsdi_versindex, fhp->nfh_len, stateidp->seqid);
6167 if ((dp->nfsdi_flags & NFSDI_TIGHTCOUPLED) == 0) {
6168 tcred = NFSNEWCRED(cred);
6169 tcred->cr_uid = flp->nfsfl_ffm[mirror].user;
6170 tcred->cr_groups[0] = flp->nfsfl_ffm[mirror].group;
6171 tcred->cr_ngroups = 1;
6174 if (rwflag == NFSV4OPEN_ACCESSREAD)
6175 transfer = dp->nfsdi_rsize;
6177 transfer = dp->nfsdi_wsize;
6179 np->n_flag |= NDSCOMMIT;
6181 if (len > transfer && docommit == 0)
6185 if (docommit != 0) {
6188 * Do last mirrored DS commit with this thread.
6190 if (mirror < flp->nfsfl_mirrorcnt - 1)
6191 error = nfsio_commitds(vp, off, xfer,
6192 *dspp, fhp, dp->nfsdi_vers,
6193 dp->nfsdi_minorvers, drpc, tcred,
6196 error = nfsrpc_commitds(vp, off, xfer,
6197 *dspp, fhp, dp->nfsdi_vers,
6198 dp->nfsdi_minorvers, tcred, p);
6199 NFSCL_DEBUG(4, "commitds=%d\n", error);
6200 if (error != 0 && error != EACCES && error !=
6203 "DS layreterr for commit\n");
6204 nfscl_dserr(NFSV4OP_COMMIT, error, dp,
6208 NFSCL_DEBUG(4, "aft nfsio_commitds=%d\n", error);
6211 * Set both eof and uio_resid = 0 to end any
6215 uiop->uio_resid = 0;
6218 np->n_flag &= ~NDSCOMMIT;
6221 } else if (rwflag == NFSV4OPEN_ACCESSREAD) {
6222 error = nfsrpc_readds(vp, uiop, stateidp, eofp, *dspp,
6223 off, xfer, fhp, 1, dp->nfsdi_vers,
6224 dp->nfsdi_minorvers, tcred, p);
6225 NFSCL_DEBUG(4, "readds=%d\n", error);
6226 if (error != 0 && error != EACCES && error != ESTALE) {
6227 NFSCL_DEBUG(4, "DS layreterr for read\n");
6228 nfscl_dserr(NFSV4OP_READ, error, dp, lyp,
6232 if (flp->nfsfl_mirrorcnt == 1) {
6233 error = nfsrpc_writeds(vp, uiop, iomode,
6234 must_commit, stateidp, *dspp, off, xfer,
6235 fhp, 0, 1, dp->nfsdi_vers,
6236 dp->nfsdi_minorvers, tcred, p);
6239 lyp->nfsly_flags |= NFSLY_WRITTEN;
6243 m = nfsm_copym(mp, rel_off, xfer);
6244 NFSCL_DEBUG(4, "mcopy reloff=%d xfer=%jd\n",
6245 rel_off, (uintmax_t)xfer);
6247 * Do last write to a mirrored DS with this
6250 if (mirror < flp->nfsfl_mirrorcnt - 1)
6251 error = nfsio_writedsmir(vp, iomode,
6252 must_commit, stateidp, *dspp, off,
6253 xfer, fhp, m, dp->nfsdi_vers,
6254 dp->nfsdi_minorvers, drpc, tcred,
6257 error = nfsrpc_writedsmir(vp, iomode,
6258 must_commit, stateidp, *dspp, off,
6259 xfer, fhp, m, dp->nfsdi_vers,
6260 dp->nfsdi_minorvers, tcred, p);
6261 NFSCL_DEBUG(4, "nfsio_writedsmir=%d\n", error);
6262 if (error != 0 && error != EACCES && error !=
6265 "DS layreterr for write\n");
6266 nfscl_dserr(NFSV4OP_WRITE, error, dp,
6271 NFSCL_DEBUG(4, "aft read/writeds=%d\n", error);
6277 if ((dp->nfsdi_flags & NFSDI_TIGHTCOUPLED) == 0)
6280 NFSCL_DEBUG(4, "eo nfscl_dofflayoutio=%d\n", error);
6285 * The actual read RPC done to a DS.
6288 nfsrpc_readds(vnode_t vp, struct uio *uiop, nfsv4stateid_t *stateidp, int *eofp,
6289 struct nfsclds *dsp, uint64_t io_off, int len, struct nfsfh *fhp, int flex,
6290 int vers, int minorvers, struct ucred *cred, NFSPROC_T *p)
6293 int attrflag, error, retlen;
6294 struct nfsrv_descript nfsd;
6295 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
6296 struct nfsrv_descript *nd = &nfsd;
6297 struct nfssockreq *nrp;
6301 if (vers == 0 || vers == NFS_VER4) {
6302 nfscl_reqstart(nd, NFSPROC_READDS, nmp, fhp->nfh_fh,
6303 fhp->nfh_len, NULL, &dsp->nfsclds_sess, vers, minorvers);
6305 NFSCL_DEBUG(4, "nfsrpc_readds: vers4 minvers=%d\n", minorvers);
6307 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID);
6309 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSEQIDZERO);
6311 nfscl_reqstart(nd, NFSPROC_READ, nmp, fhp->nfh_fh,
6312 fhp->nfh_len, NULL, &dsp->nfsclds_sess, vers, minorvers);
6313 NFSCL_DEBUG(4, "nfsrpc_readds: vers3\n");
6315 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED * 3);
6316 txdr_hyper(io_off, tl);
6317 *(tl + 2) = txdr_unsigned(len);
6318 nrp = dsp->nfsclds_sockp;
6319 NFSCL_DEBUG(4, "nfsrpc_readds: nrp=%p\n", nrp);
6321 /* If NULL, use the MDS socket. */
6322 nrp = &nmp->nm_sockreq;
6323 error = newnfs_request(nd, nmp, NULL, nrp, vp, p, cred,
6324 NFS_PROG, vers, NULL, 1, NULL, &dsp->nfsclds_sess);
6325 NFSCL_DEBUG(4, "nfsrpc_readds: stat=%d err=%d\n", nd->nd_repstat,
6329 if (vers == NFS_VER3) {
6330 error = nfscl_postop_attr(nd, &na, &attrflag, NULL);
6331 NFSCL_DEBUG(4, "nfsrpc_readds: postop=%d\n", error);
6335 if (nd->nd_repstat != 0) {
6336 error = nd->nd_repstat;
6339 if (vers == NFS_VER3) {
6340 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
6341 *eofp = fxdr_unsigned(int, *(tl + 1));
6343 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
6344 *eofp = fxdr_unsigned(int, *tl);
6346 NFSM_STRSIZ(retlen, len);
6347 NFSCL_DEBUG(4, "nfsrpc_readds: retlen=%d eof=%d\n", retlen, *eofp);
6348 error = nfsm_mbufuio(nd, uiop, retlen);
6350 if (nd->nd_mrep != NULL)
6351 m_freem(nd->nd_mrep);
6356 * The actual write RPC done to a DS.
6359 nfsrpc_writeds(vnode_t vp, struct uio *uiop, int *iomode, int *must_commit,
6360 nfsv4stateid_t *stateidp, struct nfsclds *dsp, uint64_t io_off, int len,
6361 struct nfsfh *fhp, int commit_thru_mds, int flex, int vers, int minorvers,
6362 struct ucred *cred, NFSPROC_T *p)
6365 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
6366 int attrflag, error, rlen, commit, committed = NFSWRITE_FILESYNC;
6368 struct nfsrv_descript nfsd;
6369 struct nfsrv_descript *nd = &nfsd;
6370 struct nfssockreq *nrp;
6373 KASSERT(uiop->uio_iovcnt == 1, ("nfs: writerpc iovcnt > 1"));
6375 if (vers == 0 || vers == NFS_VER4) {
6376 nfscl_reqstart(nd, NFSPROC_WRITEDS, nmp, fhp->nfh_fh,
6377 fhp->nfh_len, NULL, &dsp->nfsclds_sess, vers, minorvers);
6378 NFSCL_DEBUG(4, "nfsrpc_writeds: vers4 minvers=%d\n", minorvers);
6381 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID);
6383 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSEQIDZERO);
6384 NFSM_BUILD(tl, uint32_t *, NFSX_HYPER + 2 * NFSX_UNSIGNED);
6386 nfscl_reqstart(nd, NFSPROC_WRITE, nmp, fhp->nfh_fh,
6387 fhp->nfh_len, NULL, &dsp->nfsclds_sess, vers, minorvers);
6388 NFSCL_DEBUG(4, "nfsrpc_writeds: vers3\n");
6389 NFSM_BUILD(tl, uint32_t *, NFSX_HYPER + 3 * NFSX_UNSIGNED);
6391 txdr_hyper(io_off, tl);
6393 if (vers == NFS_VER3)
6394 *tl++ = txdr_unsigned(len);
6395 *tl++ = txdr_unsigned(*iomode);
6396 *tl = txdr_unsigned(len);
6397 nfsm_uiombuf(nd, uiop, len);
6398 nrp = dsp->nfsclds_sockp;
6400 /* If NULL, use the MDS socket. */
6401 nrp = &nmp->nm_sockreq;
6402 error = newnfs_request(nd, nmp, NULL, nrp, vp, p, cred,
6403 NFS_PROG, vers, NULL, 1, NULL, &dsp->nfsclds_sess);
6404 NFSCL_DEBUG(4, "nfsrpc_writeds: err=%d stat=%d\n", error,
6408 if (nd->nd_repstat != 0) {
6410 * In case the rpc gets retried, roll
6411 * the uio fileds changed by nfsm_uiombuf()
6414 uiop->uio_offset -= len;
6415 uiop->uio_resid += len;
6416 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base - len;
6417 uiop->uio_iov->iov_len += len;
6418 error = nd->nd_repstat;
6420 if (vers == NFS_VER3) {
6421 error = nfscl_wcc_data(nd, vp, &na, &attrflag, NULL,
6423 NFSCL_DEBUG(4, "nfsrpc_writeds: wcc_data=%d\n", error);
6427 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED + NFSX_VERF);
6428 rlen = fxdr_unsigned(int, *tl++);
6429 NFSCL_DEBUG(4, "nfsrpc_writeds: len=%d rlen=%d\n", len, rlen);
6433 } else if (rlen < len) {
6434 backup = len - rlen;
6435 uiop->uio_iov->iov_base =
6436 (char *)uiop->uio_iov->iov_base - backup;
6437 uiop->uio_iov->iov_len += backup;
6438 uiop->uio_offset -= backup;
6439 uiop->uio_resid += backup;
6442 commit = fxdr_unsigned(int, *tl++);
6445 * Return the lowest commitment level
6446 * obtained by any of the RPCs.
6448 if (committed == NFSWRITE_FILESYNC)
6450 else if (committed == NFSWRITE_DATASYNC &&
6451 commit == NFSWRITE_UNSTABLE)
6453 if (commit_thru_mds != 0) {
6455 if (!NFSHASWRITEVERF(nmp)) {
6456 NFSBCOPY(tl, nmp->nm_verf, NFSX_VERF);
6457 NFSSETWRITEVERF(nmp);
6458 } else if (NFSBCMP(tl, nmp->nm_verf, NFSX_VERF)) {
6460 NFSBCOPY(tl, nmp->nm_verf, NFSX_VERF);
6465 if ((dsp->nfsclds_flags & NFSCLDS_HASWRITEVERF) == 0) {
6466 NFSBCOPY(tl, dsp->nfsclds_verf, NFSX_VERF);
6467 dsp->nfsclds_flags |= NFSCLDS_HASWRITEVERF;
6468 } else if (NFSBCMP(tl, dsp->nfsclds_verf, NFSX_VERF)) {
6470 NFSBCOPY(tl, dsp->nfsclds_verf, NFSX_VERF);
6476 if (nd->nd_mrep != NULL)
6477 m_freem(nd->nd_mrep);
6478 *iomode = committed;
6479 if (nd->nd_repstat != 0 && error == 0)
6480 error = nd->nd_repstat;
6485 * The actual write RPC done to a DS.
6486 * This variant is called from a separate kernel process for mirrors.
6487 * Any short write is considered an IO error.
6490 nfsrpc_writedsmir(vnode_t vp, int *iomode, int *must_commit,
6491 nfsv4stateid_t *stateidp, struct nfsclds *dsp, uint64_t io_off, int len,
6492 struct nfsfh *fhp, struct mbuf *m, int vers, int minorvers,
6493 struct ucred *cred, NFSPROC_T *p)
6496 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
6497 int attrflag, error, commit, committed = NFSWRITE_FILESYNC, rlen;
6498 struct nfsrv_descript nfsd;
6499 struct nfsrv_descript *nd = &nfsd;
6500 struct nfssockreq *nrp;
6504 if (vers == 0 || vers == NFS_VER4) {
6505 nfscl_reqstart(nd, NFSPROC_WRITEDS, nmp, fhp->nfh_fh,
6506 fhp->nfh_len, NULL, &dsp->nfsclds_sess, vers, minorvers);
6508 NFSCL_DEBUG(4, "nfsrpc_writedsmir: vers4 minvers=%d\n",
6510 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID);
6511 NFSM_BUILD(tl, uint32_t *, NFSX_HYPER + 2 * NFSX_UNSIGNED);
6513 nfscl_reqstart(nd, NFSPROC_WRITE, nmp, fhp->nfh_fh,
6514 fhp->nfh_len, NULL, &dsp->nfsclds_sess, vers, minorvers);
6515 NFSCL_DEBUG(4, "nfsrpc_writedsmir: vers3\n");
6516 NFSM_BUILD(tl, uint32_t *, NFSX_HYPER + 3 * NFSX_UNSIGNED);
6518 txdr_hyper(io_off, tl);
6520 if (vers == NFS_VER3)
6521 *tl++ = txdr_unsigned(len);
6522 *tl++ = txdr_unsigned(*iomode);
6523 *tl = txdr_unsigned(len);
6525 /* Put data in mbuf chain. */
6526 nd->nd_mb->m_next = m;
6527 /* Set nd_mb and nd_bpos to end of data. */
6528 while (m->m_next != NULL)
6531 nd->nd_bpos = mtod(m, char *) + m->m_len;
6532 NFSCL_DEBUG(4, "nfsrpc_writedsmir: lastmb len=%d\n", m->m_len);
6534 nrp = dsp->nfsclds_sockp;
6536 /* If NULL, use the MDS socket. */
6537 nrp = &nmp->nm_sockreq;
6538 error = newnfs_request(nd, nmp, NULL, nrp, vp, p, cred,
6539 NFS_PROG, vers, NULL, 1, NULL, &dsp->nfsclds_sess);
6540 NFSCL_DEBUG(4, "nfsrpc_writedsmir: err=%d stat=%d\n", error,
6544 if (nd->nd_repstat != 0)
6545 error = nd->nd_repstat;
6547 if (vers == NFS_VER3) {
6548 error = nfscl_wcc_data(nd, vp, &na, &attrflag, NULL,
6550 NFSCL_DEBUG(4, "nfsrpc_writedsmir: wcc_data=%d\n",
6555 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED + NFSX_VERF);
6556 rlen = fxdr_unsigned(int, *tl++);
6557 NFSCL_DEBUG(4, "nfsrpc_writedsmir: len=%d rlen=%d\n", len,
6561 NFSCL_DEBUG(4, "nfsrpc_writedsmir: len=%d rlen=%d\n",
6565 commit = fxdr_unsigned(int, *tl++);
6568 * Return the lowest commitment level
6569 * obtained by any of the RPCs.
6571 if (committed == NFSWRITE_FILESYNC)
6573 else if (committed == NFSWRITE_DATASYNC &&
6574 commit == NFSWRITE_UNSTABLE)
6577 if ((dsp->nfsclds_flags & NFSCLDS_HASWRITEVERF) == 0) {
6578 NFSBCOPY(tl, dsp->nfsclds_verf, NFSX_VERF);
6579 dsp->nfsclds_flags |= NFSCLDS_HASWRITEVERF;
6580 } else if (NFSBCMP(tl, dsp->nfsclds_verf, NFSX_VERF)) {
6582 NFSBCOPY(tl, dsp->nfsclds_verf, NFSX_VERF);
6587 if (nd->nd_mrep != NULL)
6588 m_freem(nd->nd_mrep);
6589 *iomode = committed;
6590 if (nd->nd_repstat != 0 && error == 0)
6591 error = nd->nd_repstat;
6596 * Start up the thread that will execute nfsrpc_writedsmir().
6599 start_writedsmir(void *arg, int pending)
6601 struct nfsclwritedsdorpc *drpc;
6603 drpc = (struct nfsclwritedsdorpc *)arg;
6604 drpc->err = nfsrpc_writedsmir(drpc->vp, &drpc->iomode,
6605 &drpc->must_commit, drpc->stateidp, drpc->dsp, drpc->off, drpc->len,
6606 drpc->fhp, drpc->m, drpc->vers, drpc->minorvers, drpc->cred,
6609 NFSCL_DEBUG(4, "start_writedsmir: err=%d\n", drpc->err);
6613 * Set up the write DS mirror call for the pNFS I/O thread.
6616 nfsio_writedsmir(vnode_t vp, int *iomode, int *must_commit,
6617 nfsv4stateid_t *stateidp, struct nfsclds *dsp, uint64_t off, int len,
6618 struct nfsfh *fhp, struct mbuf *m, int vers, int minorvers,
6619 struct nfsclwritedsdorpc *drpc, struct ucred *cred, NFSPROC_T *p)
6626 drpc->iomode = *iomode;
6627 drpc->must_commit = *must_commit;
6628 drpc->stateidp = stateidp;
6635 drpc->minorvers = minorvers;
6640 if (nfs_pnfsiothreads != 0) {
6641 ret = nfs_pnfsio(start_writedsmir, drpc);
6642 NFSCL_DEBUG(4, "nfsio_writedsmir: nfs_pnfsio=%d\n", ret);
6645 error = nfsrpc_writedsmir(vp, iomode, must_commit, stateidp,
6646 dsp, off, len, fhp, m, vers, minorvers, cred, p);
6647 NFSCL_DEBUG(4, "nfsio_writedsmir: error=%d\n", error);
6652 * Free up the nfsclds structure.
6655 nfscl_freenfsclds(struct nfsclds *dsp)
6661 if (dsp->nfsclds_sockp != NULL) {
6662 NFSFREECRED(dsp->nfsclds_sockp->nr_cred);
6663 NFSFREEMUTEX(&dsp->nfsclds_sockp->nr_mtx);
6664 free(dsp->nfsclds_sockp->nr_nam, M_SONAME);
6665 free(dsp->nfsclds_sockp, M_NFSSOCKREQ);
6667 NFSFREEMUTEX(&dsp->nfsclds_mtx);
6668 NFSFREEMUTEX(&dsp->nfsclds_sess.nfsess_mtx);
6669 for (i = 0; i < NFSV4_CBSLOTS; i++) {
6670 if (dsp->nfsclds_sess.nfsess_cbslots[i].nfssl_reply != NULL)
6672 dsp->nfsclds_sess.nfsess_cbslots[i].nfssl_reply);
6674 free(dsp, M_NFSCLDS);
6677 static enum nfsclds_state
6678 nfscl_getsameserver(struct nfsmount *nmp, struct nfsclds *newdsp,
6679 struct nfsclds **retdspp, uint32_t *sequencep)
6681 struct nfsclds *dsp;
6685 * Search the list of nfsclds structures for one with the same
6689 TAILQ_FOREACH(dsp, &nmp->nm_sess, nfsclds_list) {
6690 if (dsp->nfsclds_servownlen == newdsp->nfsclds_servownlen &&
6691 dsp->nfsclds_servownlen != 0 &&
6692 !NFSBCMP(dsp->nfsclds_serverown, newdsp->nfsclds_serverown,
6693 dsp->nfsclds_servownlen) &&
6694 dsp->nfsclds_sess.nfsess_defunct == 0) {
6695 NFSCL_DEBUG(4, "fnd same fdsp=%p dsp=%p flg=0x%x\n",
6696 TAILQ_FIRST(&nmp->nm_sess), dsp,
6697 dsp->nfsclds_flags);
6699 /* Get sequenceid# from first entry. */
6701 dsp->nfsclds_sess.nfsess_sequenceid;
6704 /* Server major id matches. */
6705 if ((dsp->nfsclds_flags & NFSCLDS_DS) != 0) {
6707 return (NFSDSP_USETHISSESSION);
6713 return (NFSDSP_SEQTHISSESSION);
6714 return (NFSDSP_NOTFOUND);
6718 * NFS commit rpc to a NFSv4.1 DS.
6721 nfsrpc_commitds(vnode_t vp, uint64_t offset, int cnt, struct nfsclds *dsp,
6722 struct nfsfh *fhp, int vers, int minorvers, struct ucred *cred,
6726 struct nfsrv_descript nfsd, *nd = &nfsd;
6727 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
6728 struct nfssockreq *nrp;
6730 int attrflag, error;
6733 if (vers == 0 || vers == NFS_VER4) {
6734 nfscl_reqstart(nd, NFSPROC_COMMITDS, nmp, fhp->nfh_fh,
6735 fhp->nfh_len, NULL, &dsp->nfsclds_sess, vers, minorvers);
6738 nfscl_reqstart(nd, NFSPROC_COMMIT, nmp, fhp->nfh_fh,
6739 fhp->nfh_len, NULL, &dsp->nfsclds_sess, vers, minorvers);
6740 NFSCL_DEBUG(4, "nfsrpc_commitds: vers=%d minvers=%d\n", vers,
6742 NFSM_BUILD(tl, uint32_t *, NFSX_HYPER + NFSX_UNSIGNED);
6743 txdr_hyper(offset, tl);
6745 *tl = txdr_unsigned(cnt);
6746 nrp = dsp->nfsclds_sockp;
6748 /* If NULL, use the MDS socket. */
6749 nrp = &nmp->nm_sockreq;
6750 error = newnfs_request(nd, nmp, NULL, nrp, vp, p, cred,
6751 NFS_PROG, vers, NULL, 1, NULL, &dsp->nfsclds_sess);
6752 NFSCL_DEBUG(4, "nfsrpc_commitds: err=%d stat=%d\n", error,
6756 if (nd->nd_repstat == 0) {
6757 if (vers == NFS_VER3) {
6758 error = nfscl_wcc_data(nd, vp, &na, &attrflag, NULL,
6760 NFSCL_DEBUG(4, "nfsrpc_commitds: wccdata=%d\n", error);
6764 NFSM_DISSECT(tl, u_int32_t *, NFSX_VERF);
6766 if (NFSBCMP(tl, dsp->nfsclds_verf, NFSX_VERF)) {
6767 NFSBCOPY(tl, dsp->nfsclds_verf, NFSX_VERF);
6768 error = NFSERR_STALEWRITEVERF;
6773 if (error == 0 && nd->nd_repstat != 0)
6774 error = nd->nd_repstat;
6775 m_freem(nd->nd_mrep);
6780 * Start up the thread that will execute nfsrpc_commitds().
6783 start_commitds(void *arg, int pending)
6785 struct nfsclwritedsdorpc *drpc;
6787 drpc = (struct nfsclwritedsdorpc *)arg;
6788 drpc->err = nfsrpc_commitds(drpc->vp, drpc->off, drpc->len,
6789 drpc->dsp, drpc->fhp, drpc->vers, drpc->minorvers, drpc->cred,
6792 NFSCL_DEBUG(4, "start_commitds: err=%d\n", drpc->err);
6796 * Set up the commit DS mirror call for the pNFS I/O thread.
6799 nfsio_commitds(vnode_t vp, uint64_t offset, int cnt, struct nfsclds *dsp,
6800 struct nfsfh *fhp, int vers, int minorvers,
6801 struct nfsclwritedsdorpc *drpc, struct ucred *cred, NFSPROC_T *p)
6813 drpc->minorvers = minorvers;
6818 if (nfs_pnfsiothreads != 0) {
6819 ret = nfs_pnfsio(start_commitds, drpc);
6820 NFSCL_DEBUG(4, "nfsio_commitds: nfs_pnfsio=%d\n", ret);
6823 error = nfsrpc_commitds(vp, offset, cnt, dsp, fhp, vers,
6824 minorvers, cred, p);
6825 NFSCL_DEBUG(4, "nfsio_commitds: error=%d\n", error);
6833 nfsrpc_advise(vnode_t vp, off_t offset, uint64_t cnt, int advise,
6834 struct ucred *cred, NFSPROC_T *p)
6837 struct nfsrv_descript nfsd, *nd = &nfsd;
6841 NFSZERO_ATTRBIT(&hints);
6842 if (advise == POSIX_FADV_WILLNEED)
6843 NFSSETBIT_ATTRBIT(&hints, NFSV4IOHINT_WILLNEED);
6844 else if (advise == POSIX_FADV_DONTNEED)
6845 NFSSETBIT_ATTRBIT(&hints, NFSV4IOHINT_DONTNEED);
6848 NFSCL_REQSTART(nd, NFSPROC_IOADVISE, vp);
6849 nfsm_stateidtom(nd, NULL, NFSSTATEID_PUTALLZERO);
6850 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_HYPER);
6851 txdr_hyper(offset, tl);
6853 txdr_hyper(cnt, tl);
6854 nfsrv_putattrbit(nd, &hints);
6855 error = nfscl_request(nd, vp, p, cred, NULL);
6858 if (nd->nd_repstat != 0)
6859 error = nd->nd_repstat;
6860 m_freem(nd->nd_mrep);
6866 * NFS advise rpc to a NFSv4.2 DS.
6869 nfsrpc_adviseds(vnode_t vp, uint64_t offset, int cnt, int advise,
6870 struct nfsclds *dsp, struct nfsfh *fhp, int vers, int minorvers,
6871 struct ucred *cred, NFSPROC_T *p)
6874 struct nfsrv_descript nfsd, *nd = &nfsd;
6875 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
6876 struct nfssockreq *nrp;
6880 /* For NFS DSs prior to NFSv4.2, just return OK. */
6881 if (vers == NFS_VER3 || minorversion < NFSV42_MINORVERSION)
6883 NFSZERO_ATTRBIT(&hints);
6884 if (advise == POSIX_FADV_WILLNEED)
6885 NFSSETBIT_ATTRBIT(&hints, NFSV4IOHINT_WILLNEED);
6886 else if (advise == POSIX_FADV_DONTNEED)
6887 NFSSETBIT_ATTRBIT(&hints, NFSV4IOHINT_DONTNEED);
6891 nfscl_reqstart(nd, NFSPROC_IOADVISEDS, nmp, fhp->nfh_fh,
6892 fhp->nfh_len, NULL, &dsp->nfsclds_sess, vers, minorvers);
6894 NFSCL_DEBUG(4, "nfsrpc_adviseds: vers=%d minvers=%d\n", vers,
6896 nfsm_stateidtom(nd, NULL, NFSSTATEID_PUTALLZERO);
6897 NFSM_BUILD(tl, uint32_t *, NFSX_HYPER + NFSX_UNSIGNED);
6898 txdr_hyper(offset, tl);
6900 *tl = txdr_unsigned(cnt);
6901 nfsrv_putattrbit(nd, &hints);
6902 nrp = dsp->nfsclds_sockp;
6904 /* If NULL, use the MDS socket. */
6905 nrp = &nmp->nm_sockreq;
6906 error = newnfs_request(nd, nmp, NULL, nrp, vp, p, cred,
6907 NFS_PROG, vers, NULL, 1, NULL, &dsp->nfsclds_sess);
6908 NFSCL_DEBUG(4, "nfsrpc_adviseds: err=%d stat=%d\n", error,
6912 if (nd->nd_repstat != 0)
6913 error = nd->nd_repstat;
6914 m_freem(nd->nd_mrep);
6919 * Start up the thread that will execute nfsrpc_commitds().
6922 start_adviseds(void *arg, int pending)
6924 struct nfsclwritedsdorpc *drpc;
6926 drpc = (struct nfsclwritedsdorpc *)arg;
6927 drpc->err = nfsrpc_adviseds(drpc->vp, drpc->off, drpc->len,
6928 drpc->advise, drpc->dsp, drpc->fhp, drpc->vers, drpc->minorvers,
6929 drpc->cred, drpc->p);
6931 NFSCL_DEBUG(4, "start_adviseds: err=%d\n", drpc->err);
6935 * Set up the commit DS mirror call for the pNFS I/O thread.
6938 nfsio_adviseds(vnode_t vp, uint64_t offset, int cnt, int advise,
6939 struct nfsclds *dsp, struct nfsfh *fhp, int vers, int minorvers,
6940 struct nfsclwritedsdorpc *drpc, struct ucred *cred, NFSPROC_T *p)
6949 drpc->advise = advise;
6953 drpc->minorvers = minorvers;
6958 if (nfs_pnfsiothreads != 0) {
6959 ret = nfs_pnfsio(start_adviseds, drpc);
6960 NFSCL_DEBUG(4, "nfsio_adviseds: nfs_pnfsio=%d\n", ret);
6963 error = nfsrpc_adviseds(vp, offset, cnt, advise, dsp, fhp, vers,
6964 minorvers, cred, p);
6965 NFSCL_DEBUG(4, "nfsio_adviseds: error=%d\n", error);
6971 * Do the Allocate operation, retrying for recovery.
6974 nfsrpc_allocate(vnode_t vp, off_t off, off_t len, struct nfsvattr *nap,
6975 int *attrflagp, struct ucred *cred, NFSPROC_T *p, void *stuff)
6977 int error, expireret = 0, retrycnt, nostateid;
6978 uint32_t clidrev = 0;
6979 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
6980 struct nfsfh *nfhp = NULL;
6981 nfsv4stateid_t stateid;
6989 tmp_off = off + len;
6991 if (tmp_off > nmp->nm_maxfilesize || tmp_off < off) {
6995 if (nmp->nm_clp != NULL)
6996 clidrev = nmp->nm_clp->nfsc_clientidrev;
6998 nfhp = VTONFS(vp)->n_fhp;
7003 nfscl_getstateid(vp, nfhp->nfh_fh, nfhp->nfh_len,
7004 NFSV4OPEN_ACCESSWRITE, 0, cred, p, &stateid, &lckp);
7005 if (stateid.other[0] == 0 && stateid.other[1] == 0 &&
7006 stateid.other[2] == 0) {
7008 NFSCL_DEBUG(1, "stateid0 in allocate\n");
7012 * Not finding a stateid should probably never happen,
7013 * but just return an error for this case.
7018 error = nfsrpc_allocaterpc(vp, off, len, &stateid,
7019 nap, attrflagp, cred, p, stuff);
7020 if (error == NFSERR_STALESTATEID)
7021 nfscl_initiate_recovery(nmp->nm_clp);
7023 nfscl_lockderef(lckp);
7024 if (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
7025 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
7026 error == NFSERR_OLDSTATEID || error == NFSERR_BADSESSION) {
7027 (void) nfs_catnap(PZERO, error, "nfs_allocate");
7028 } else if ((error == NFSERR_EXPIRED ||
7029 error == NFSERR_BADSTATEID) && clidrev != 0) {
7030 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
7033 } while (error == NFSERR_GRACE || error == NFSERR_DELAY ||
7034 error == NFSERR_STALESTATEID || error == NFSERR_BADSESSION ||
7035 error == NFSERR_STALEDONTRECOVER ||
7036 (error == NFSERR_OLDSTATEID && retrycnt < 20) ||
7037 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
7038 expireret == 0 && clidrev != 0 && retrycnt < 4));
7039 if (error != 0 && retrycnt >= 4)
7048 nfsrpc_allocaterpc(vnode_t vp, off_t off, off_t len, nfsv4stateid_t *stateidp,
7049 struct nfsvattr *nap, int *attrflagp, struct ucred *cred, NFSPROC_T *p,
7054 struct nfsrv_descript nfsd;
7055 struct nfsrv_descript *nd = &nfsd;
7056 nfsattrbit_t attrbits;
7059 NFSCL_REQSTART(nd, NFSPROC_ALLOCATE, vp);
7060 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID);
7061 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_HYPER + NFSX_UNSIGNED);
7062 txdr_hyper(off, tl); tl += 2;
7063 txdr_hyper(len, tl); tl += 2;
7064 *tl = txdr_unsigned(NFSV4OP_GETATTR);
7065 NFSGETATTR_ATTRBIT(&attrbits);
7066 nfsrv_putattrbit(nd, &attrbits);
7067 error = nfscl_request(nd, vp, p, cred, stuff);
7070 if (nd->nd_repstat == 0) {
7071 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
7072 error = nfsm_loadattr(nd, nap);
7074 *attrflagp = NFS_LATTR_NOSHRINK;
7076 error = nd->nd_repstat;
7078 m_freem(nd->nd_mrep);
7083 * Set up the XDR arguments for the LayoutGet operation.
7086 nfsrv_setuplayoutget(struct nfsrv_descript *nd, int iomode, uint64_t offset,
7087 uint64_t len, uint64_t minlen, nfsv4stateid_t *stateidp, int layouttype,
7088 int layoutlen, int usecurstateid)
7092 NFSM_BUILD(tl, uint32_t *, 4 * NFSX_UNSIGNED + 3 * NFSX_HYPER +
7094 *tl++ = newnfs_false; /* Don't signal availability. */
7095 *tl++ = txdr_unsigned(layouttype);
7096 *tl++ = txdr_unsigned(iomode);
7097 txdr_hyper(offset, tl);
7099 txdr_hyper(len, tl);
7101 txdr_hyper(minlen, tl);
7103 if (usecurstateid != 0) {
7104 /* Special stateid for Current stateid. */
7105 *tl++ = txdr_unsigned(1);
7110 *tl++ = txdr_unsigned(stateidp->seqid);
7111 NFSCL_DEBUG(4, "layget seq=%d\n", (int)stateidp->seqid);
7112 *tl++ = stateidp->other[0];
7113 *tl++ = stateidp->other[1];
7114 *tl++ = stateidp->other[2];
7116 *tl = txdr_unsigned(layoutlen);
7120 * Parse the reply for a successful LayoutGet operation.
7123 nfsrv_parselayoutget(struct nfsmount *nmp, struct nfsrv_descript *nd,
7124 nfsv4stateid_t *stateidp, int *retonclosep, struct nfsclflayouthead *flhp)
7127 struct nfsclflayout *flp, *prevflp, *tflp;
7128 int cnt, error, fhcnt, gotiomode, i, iomode, j, k, l, laytype, nfhlen;
7130 uint64_t retlen, off;
7136 NFSCL_DEBUG(4, "in nfsrv_parselayoutget\n");
7140 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED + NFSX_STATEID);
7145 stateidp->seqid = fxdr_unsigned(uint32_t, *tl++);
7146 NFSCL_DEBUG(4, "retoncls=%d stseq=%d\n", *retonclosep,
7147 (int)stateidp->seqid);
7148 stateidp->other[0] = *tl++;
7149 stateidp->other[1] = *tl++;
7150 stateidp->other[2] = *tl++;
7151 cnt = fxdr_unsigned(int, *tl);
7152 NFSCL_DEBUG(4, "layg cnt=%d\n", cnt);
7153 if (cnt <= 0 || cnt > 10000) {
7154 /* Don't accept more than 10000 layouts in reply. */
7155 error = NFSERR_BADXDR;
7158 for (i = 0; i < cnt; i++) {
7159 /* Dissect to the layout type. */
7160 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_HYPER +
7162 off = fxdr_hyper(tl); tl += 2;
7163 retlen = fxdr_hyper(tl); tl += 2;
7164 iomode = fxdr_unsigned(int, *tl++);
7165 laytype = fxdr_unsigned(int, *tl);
7166 NFSCL_DEBUG(4, "layt=%d off=%ju len=%ju iom=%d\n", laytype,
7167 (uintmax_t)off, (uintmax_t)retlen, iomode);
7168 /* Ignore length of layout body for now. */
7169 if (laytype == NFSLAYOUT_NFSV4_1_FILES) {
7170 /* Parse the File layout up to fhcnt. */
7171 NFSM_DISSECT(tl, uint32_t *, 3 * NFSX_UNSIGNED +
7172 NFSX_HYPER + NFSX_V4DEVICEID);
7173 fhcnt = fxdr_unsigned(int, *(tl + 4 +
7174 NFSX_V4DEVICEID / NFSX_UNSIGNED));
7175 NFSCL_DEBUG(4, "fhcnt=%d\n", fhcnt);
7176 if (fhcnt < 0 || fhcnt > 100) {
7177 /* Don't accept more than 100 file handles. */
7178 error = NFSERR_BADXDR;
7182 flp = malloc(sizeof(*flp) + fhcnt *
7183 sizeof(struct nfsfh *), M_NFSFLAYOUT,
7186 flp = malloc(sizeof(*flp), M_NFSFLAYOUT,
7188 flp->nfsfl_flags = NFSFL_FILE;
7189 flp->nfsfl_fhcnt = 0;
7190 flp->nfsfl_devp = NULL;
7191 flp->nfsfl_off = off;
7192 if (flp->nfsfl_off + retlen < flp->nfsfl_off)
7193 flp->nfsfl_end = UINT64_MAX - flp->nfsfl_off;
7195 flp->nfsfl_end = flp->nfsfl_off + retlen;
7196 flp->nfsfl_iomode = iomode;
7197 if (gotiomode == -1)
7198 gotiomode = flp->nfsfl_iomode;
7199 /* Ignore layout body length for now. */
7200 NFSBCOPY(tl, flp->nfsfl_dev, NFSX_V4DEVICEID);
7201 tl += (NFSX_V4DEVICEID / NFSX_UNSIGNED);
7202 flp->nfsfl_util = fxdr_unsigned(uint32_t, *tl++);
7203 NFSCL_DEBUG(4, "flutil=0x%x\n", flp->nfsfl_util);
7204 mtx_lock(&nmp->nm_mtx);
7205 if (nmp->nm_minorvers > 1 && (flp->nfsfl_util &
7206 NFSFLAYUTIL_IOADVISE_THRU_MDS) != 0)
7207 nmp->nm_privflag |= NFSMNTP_IOADVISETHRUMDS;
7208 mtx_unlock(&nmp->nm_mtx);
7209 flp->nfsfl_stripe1 = fxdr_unsigned(uint32_t, *tl++);
7210 flp->nfsfl_patoff = fxdr_hyper(tl); tl += 2;
7211 NFSCL_DEBUG(4, "stripe1=%u poff=%ju\n",
7212 flp->nfsfl_stripe1, (uintmax_t)flp->nfsfl_patoff);
7213 for (j = 0; j < fhcnt; j++) {
7214 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
7215 nfhlen = fxdr_unsigned(int, *tl);
7216 if (nfhlen <= 0 || nfhlen > NFSX_V4FHMAX) {
7217 error = NFSERR_BADXDR;
7220 nfhp = malloc(sizeof(*nfhp) + nfhlen - 1,
7222 flp->nfsfl_fh[j] = nfhp;
7224 nfhp->nfh_len = nfhlen;
7225 NFSM_DISSECT(cp, uint8_t *, NFSM_RNDUP(nfhlen));
7226 NFSBCOPY(cp, nfhp->nfh_fh, nfhlen);
7228 } else if (laytype == NFSLAYOUT_FLEXFILE) {
7229 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED +
7231 mirrorcnt = fxdr_unsigned(int, *(tl + 2));
7232 NFSCL_DEBUG(4, "mirrorcnt=%d\n", mirrorcnt);
7233 if (mirrorcnt < 1 || mirrorcnt > NFSDEV_MAXMIRRORS) {
7234 error = NFSERR_BADXDR;
7237 flp = malloc(sizeof(*flp) + mirrorcnt *
7238 sizeof(struct nfsffm), M_NFSFLAYOUT, M_WAITOK);
7239 flp->nfsfl_flags = NFSFL_FLEXFILE;
7240 flp->nfsfl_mirrorcnt = mirrorcnt;
7241 for (j = 0; j < mirrorcnt; j++)
7242 flp->nfsfl_ffm[j].devp = NULL;
7243 flp->nfsfl_off = off;
7244 if (flp->nfsfl_off + retlen < flp->nfsfl_off)
7245 flp->nfsfl_end = UINT64_MAX - flp->nfsfl_off;
7247 flp->nfsfl_end = flp->nfsfl_off + retlen;
7248 flp->nfsfl_iomode = iomode;
7249 if (gotiomode == -1)
7250 gotiomode = flp->nfsfl_iomode;
7251 flp->nfsfl_stripeunit = fxdr_hyper(tl);
7252 NFSCL_DEBUG(4, "stripeunit=%ju\n",
7253 (uintmax_t)flp->nfsfl_stripeunit);
7254 for (j = 0; j < mirrorcnt; j++) {
7255 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
7256 k = fxdr_unsigned(int, *tl);
7257 if (k < 1 || k > 128) {
7258 error = NFSERR_BADXDR;
7261 NFSCL_DEBUG(4, "servercnt=%d\n", k);
7262 for (l = 0; l < k; l++) {
7263 NFSM_DISSECT(tl, uint32_t *,
7264 NFSX_V4DEVICEID + NFSX_STATEID +
7267 /* Just use the first server. */
7269 flp->nfsfl_ffm[j].dev,
7271 tl += (NFSX_V4DEVICEID /
7274 flp->nfsfl_ffm[j].st.seqid =
7276 flp->nfsfl_ffm[j].st.other[0] =
7278 flp->nfsfl_ffm[j].st.other[1] =
7280 flp->nfsfl_ffm[j].st.other[2] =
7282 NFSCL_DEBUG(4, "st.seqid=%u "
7283 "st.o0=0x%x st.o1=0x%x "
7285 flp->nfsfl_ffm[j].st.seqid,
7286 flp->nfsfl_ffm[j].st.other[0],
7287 flp->nfsfl_ffm[j].st.other[1],
7288 flp->nfsfl_ffm[j].st.other[2]);
7290 tl += ((NFSX_V4DEVICEID +
7294 fhcnt = fxdr_unsigned(int, *tl);
7295 NFSCL_DEBUG(4, "fhcnt=%d\n", fhcnt);
7297 fhcnt > NFSDEV_MAXVERS) {
7298 error = NFSERR_BADXDR;
7301 for (m = 0; m < fhcnt; m++) {
7302 NFSM_DISSECT(tl, uint32_t *,
7304 nfhlen = fxdr_unsigned(int,
7306 NFSCL_DEBUG(4, "nfhlen=%d\n",
7308 if (nfhlen <= 0 || nfhlen >
7310 error = NFSERR_BADXDR;
7313 NFSM_DISSECT(cp, uint8_t *,
7314 NFSM_RNDUP(nfhlen));
7316 flp->nfsfl_ffm[j].fhcnt
7320 nfhlen - 1, M_NFSFH,
7322 flp->nfsfl_ffm[j].fh[m]
7324 nfhp->nfh_len = nfhlen;
7332 /* Now, get the ffsd_user/ffds_group. */
7333 error = nfsrv_parseug(nd, 0, &user,
7335 NFSCL_DEBUG(4, "after parseu=%d\n",
7338 error = nfsrv_parseug(nd, 1,
7339 &user, &grp, curthread);
7340 NFSCL_DEBUG(4, "aft parseg=%d\n",
7344 NFSCL_DEBUG(4, "user=%d group=%d\n",
7347 flp->nfsfl_ffm[j].user = user;
7348 flp->nfsfl_ffm[j].group = grp;
7350 "usr=%d grp=%d\n", user,
7355 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
7356 flp->nfsfl_fflags = fxdr_unsigned(uint32_t, *tl++);
7359 * At this time, there is no flag.
7360 * NFSFLEXFLAG_IOADVISE_THRU_MDS might need to be
7361 * added, or it may never exist?
7363 mtx_lock(&nmp->nm_mtx);
7364 if (nmp->nm_minorvers > 1 && (flp->nfsfl_fflags &
7365 NFSFLEXFLAG_IOADVISE_THRU_MDS) != 0)
7366 nmp->nm_privflag |= NFSMNTP_IOADVISETHRUMDS;
7367 mtx_unlock(&nmp->nm_mtx);
7369 flp->nfsfl_statshint = fxdr_unsigned(uint32_t, *tl);
7370 NFSCL_DEBUG(4, "fflags=0x%x statshint=%d\n",
7371 flp->nfsfl_fflags, flp->nfsfl_statshint);
7373 error = NFSERR_BADXDR;
7376 if (flp->nfsfl_iomode == gotiomode) {
7377 /* Keep the list in increasing offset order. */
7378 tflp = LIST_FIRST(flhp);
7380 while (tflp != NULL &&
7381 tflp->nfsfl_off < flp->nfsfl_off) {
7383 tflp = LIST_NEXT(tflp, nfsfl_list);
7385 if (prevflp == NULL)
7386 LIST_INSERT_HEAD(flhp, flp, nfsfl_list);
7388 LIST_INSERT_AFTER(prevflp, flp,
7390 NFSCL_DEBUG(4, "flp inserted\n");
7392 printf("nfscl_layoutget(): got wrong iomode\n");
7393 nfscl_freeflayout(flp);
7398 NFSCL_DEBUG(4, "eo nfsrv_parselayoutget=%d\n", error);
7399 if (error != 0 && flp != NULL)
7400 nfscl_freeflayout(flp);
7405 * Parse a user/group digit string.
7408 nfsrv_parseug(struct nfsrv_descript *nd, int dogrp, uid_t *uidp, gid_t *gidp,
7412 char *cp, *str, str0[NFSV4_SMALLSTR + 1];
7416 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
7417 len = fxdr_unsigned(uint32_t, *tl);
7419 if (len > NFSV4_OPAQUELIMIT) {
7420 error = NFSERR_BADXDR;
7423 NFSCL_DEBUG(4, "nfsrv_parseug: len=%d\n", len);
7426 *gidp = GID_NOGROUP;
7431 if (len > NFSV4_SMALLSTR)
7432 str = malloc(len + 1, M_TEMP, M_WAITOK);
7435 NFSM_DISSECT(cp, char *, NFSM_RNDUP(len));
7436 NFSBCOPY(cp, str, len);
7438 NFSCL_DEBUG(4, "nfsrv_parseug: str=%s\n", str);
7440 error = nfsv4_strtogid(nd, str, len, gidp);
7442 error = nfsv4_strtouid(nd, str, len, uidp);
7444 if (len > NFSV4_SMALLSTR)
7446 NFSCL_DEBUG(4, "eo nfsrv_parseug=%d\n", error);
7451 * Similar to nfsrpc_getlayout(), except that it uses nfsrpc_openlayget(),
7452 * so that it does both an Open and a Layoutget.
7455 nfsrpc_getopenlayout(struct nfsmount *nmp, vnode_t vp, u_int8_t *nfhp,
7456 int fhlen, uint8_t *newfhp, int newfhlen, uint32_t mode,
7457 struct nfsclopen *op, uint8_t *name, int namelen, struct nfscldeleg **dpp,
7458 struct ucred *cred, NFSPROC_T *p)
7460 struct nfscllayout *lyp;
7461 struct nfsclflayout *flp;
7462 struct nfsclflayouthead flh;
7463 int error, islocked, layoutlen, recalled, retonclose, usecurstateid;
7464 int layouttype, laystat;
7465 nfsv4stateid_t stateid;
7466 struct nfsclsession *tsep;
7469 if (NFSHASFLEXFILE(nmp))
7470 layouttype = NFSLAYOUT_FLEXFILE;
7472 layouttype = NFSLAYOUT_NFSV4_1_FILES;
7474 * If lyp is returned non-NULL, there will be a refcnt (shared lock)
7475 * on it, iff flp != NULL or a lock (exclusive lock) on it iff
7478 lyp = nfscl_getlayout(nmp->nm_clp, newfhp, newfhlen, 0, &flp,
7480 NFSCL_DEBUG(4, "nfsrpc_getopenlayout nfscl_getlayout lyp=%p\n", lyp);
7483 else if (flp != NULL)
7487 if ((lyp == NULL || flp == NULL) && recalled == 0) {
7489 tsep = nfsmnt_mdssession(nmp);
7490 layoutlen = tsep->nfsess_maxcache - (NFSX_STATEID +
7496 stateid.seqid = lyp->nfsly_stateid.seqid;
7497 stateid.other[0] = lyp->nfsly_stateid.other[0];
7498 stateid.other[1] = lyp->nfsly_stateid.other[1];
7499 stateid.other[2] = lyp->nfsly_stateid.other[2];
7501 error = nfsrpc_openlayoutrpc(nmp, vp, nfhp, fhlen,
7502 newfhp, newfhlen, mode, op, name, namelen,
7503 dpp, &stateid, usecurstateid, layouttype, layoutlen,
7504 &retonclose, &flh, &laystat, cred, p);
7505 NFSCL_DEBUG(4, "aft nfsrpc_openlayoutrpc laystat=%d err=%d\n",
7507 laystat = nfsrpc_layoutgetres(nmp, vp, newfhp, newfhlen,
7508 &stateid, retonclose, NULL, &lyp, &flh, layouttype, laystat,
7509 &islocked, cred, p);
7511 error = nfsrpc_openrpc(nmp, vp, nfhp, fhlen, newfhp, newfhlen,
7512 mode, op, name, namelen, dpp, 0, 0, cred, p, 0, 0);
7514 nfscl_rellayout(lyp, 1);
7515 else if (islocked == 1)
7516 nfscl_rellayout(lyp, 0);
7521 * This function does an Open+LayoutGet for an NFSv4.1 mount with pNFS
7522 * enabled, only for the CLAIM_NULL case. All other NFSv4 Opens are
7523 * handled by nfsrpc_openrpc().
7524 * For the case where op == NULL, dvp is the directory. When op != NULL, it
7528 nfsrpc_openlayoutrpc(struct nfsmount *nmp, vnode_t vp, u_int8_t *nfhp,
7529 int fhlen, uint8_t *newfhp, int newfhlen, uint32_t mode,
7530 struct nfsclopen *op, uint8_t *name, int namelen, struct nfscldeleg **dpp,
7531 nfsv4stateid_t *stateidp, int usecurstateid, int layouttype,
7532 int layoutlen, int *retonclosep, struct nfsclflayouthead *flhp,
7533 int *laystatp, struct ucred *cred, NFSPROC_T *p)
7536 struct nfsrv_descript nfsd, *nd = &nfsd;
7537 struct nfscldeleg *ndp = NULL;
7538 struct nfsvattr nfsva;
7539 struct nfsclsession *tsep;
7540 uint32_t rflags, deleg;
7541 nfsattrbit_t attrbits;
7542 int error, ret, acesize, limitby, iomode;
7546 nfscl_reqstart(nd, NFSPROC_OPENLAYGET, nmp, nfhp, fhlen, NULL, NULL,
7548 NFSM_BUILD(tl, uint32_t *, 5 * NFSX_UNSIGNED);
7549 *tl++ = txdr_unsigned(op->nfso_own->nfsow_seqid);
7550 *tl++ = txdr_unsigned(mode & NFSV4OPEN_ACCESSBOTH);
7551 *tl++ = txdr_unsigned((mode >> NFSLCK_SHIFT) & NFSV4OPEN_DENYBOTH);
7552 tsep = nfsmnt_mdssession(nmp);
7553 *tl++ = tsep->nfsess_clientid.lval[0];
7554 *tl = tsep->nfsess_clientid.lval[1];
7555 nfsm_strtom(nd, op->nfso_own->nfsow_owner, NFSV4CL_LOCKNAMELEN);
7556 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_UNSIGNED);
7557 *tl++ = txdr_unsigned(NFSV4OPEN_NOCREATE);
7558 *tl = txdr_unsigned(NFSV4OPEN_CLAIMNULL);
7559 nfsm_strtom(nd, name, namelen);
7560 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
7561 *tl = txdr_unsigned(NFSV4OP_GETATTR);
7562 NFSZERO_ATTRBIT(&attrbits);
7563 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_CHANGE);
7564 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEMODIFY);
7565 nfsrv_putattrbit(nd, &attrbits);
7566 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
7567 *tl = txdr_unsigned(NFSV4OP_LAYOUTGET);
7568 if ((mode & NFSV4OPEN_ACCESSWRITE) != 0)
7569 iomode = NFSLAYOUTIOMODE_RW;
7571 iomode = NFSLAYOUTIOMODE_READ;
7572 nfsrv_setuplayoutget(nd, iomode, 0, UINT64_MAX, 0, stateidp,
7573 layouttype, layoutlen, usecurstateid);
7574 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, vp, p, cred,
7575 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
7578 NFSCL_INCRSEQID(op->nfso_own->nfsow_seqid, nd);
7579 if (nd->nd_repstat != 0)
7580 *laystatp = nd->nd_repstat;
7581 if ((nd->nd_flag & ND_NOMOREDATA) == 0) {
7582 /* ND_NOMOREDATA will be set if the Open operation failed. */
7583 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
7585 op->nfso_stateid.seqid = *tl++;
7586 op->nfso_stateid.other[0] = *tl++;
7587 op->nfso_stateid.other[1] = *tl++;
7588 op->nfso_stateid.other[2] = *tl;
7589 rflags = fxdr_unsigned(u_int32_t, *(tl + 6));
7590 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
7593 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
7594 deleg = fxdr_unsigned(u_int32_t, *tl);
7595 if (deleg == NFSV4OPEN_DELEGATEREAD ||
7596 deleg == NFSV4OPEN_DELEGATEWRITE) {
7597 if (!(op->nfso_own->nfsow_clp->nfsc_flags &
7598 NFSCLFLAGS_FIRSTDELEG))
7599 op->nfso_own->nfsow_clp->nfsc_flags |=
7600 (NFSCLFLAGS_FIRSTDELEG | NFSCLFLAGS_GOTDELEG);
7601 ndp = malloc(sizeof(struct nfscldeleg) + newfhlen,
7602 M_NFSCLDELEG, M_WAITOK);
7603 LIST_INIT(&ndp->nfsdl_owner);
7604 LIST_INIT(&ndp->nfsdl_lock);
7605 ndp->nfsdl_clp = op->nfso_own->nfsow_clp;
7606 ndp->nfsdl_fhlen = newfhlen;
7607 NFSBCOPY(newfhp, ndp->nfsdl_fh, newfhlen);
7608 newnfs_copyincred(cred, &ndp->nfsdl_cred);
7609 nfscl_lockinit(&ndp->nfsdl_rwlock);
7610 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
7612 ndp->nfsdl_stateid.seqid = *tl++;
7613 ndp->nfsdl_stateid.other[0] = *tl++;
7614 ndp->nfsdl_stateid.other[1] = *tl++;
7615 ndp->nfsdl_stateid.other[2] = *tl++;
7616 ret = fxdr_unsigned(int, *tl);
7617 if (deleg == NFSV4OPEN_DELEGATEWRITE) {
7618 ndp->nfsdl_flags = NFSCLDL_WRITE;
7620 * Indicates how much the file can grow.
7622 NFSM_DISSECT(tl, u_int32_t *,
7624 limitby = fxdr_unsigned(int, *tl++);
7626 case NFSV4OPEN_LIMITSIZE:
7627 ndp->nfsdl_sizelimit = fxdr_hyper(tl);
7629 case NFSV4OPEN_LIMITBLOCKS:
7630 ndp->nfsdl_sizelimit =
7631 fxdr_unsigned(u_int64_t, *tl++);
7632 ndp->nfsdl_sizelimit *=
7633 fxdr_unsigned(u_int64_t, *tl);
7636 error = NFSERR_BADXDR;
7640 ndp->nfsdl_flags = NFSCLDL_READ;
7642 ndp->nfsdl_flags |= NFSCLDL_RECALL;
7643 error = nfsrv_dissectace(nd, &ndp->nfsdl_ace, &ret,
7647 } else if (deleg != NFSV4OPEN_DELEGATENONE) {
7648 error = NFSERR_BADXDR;
7651 if ((rflags & NFSV4OPEN_LOCKTYPEPOSIX) != 0 ||
7652 nfscl_assumeposixlocks)
7653 op->nfso_posixlock = 1;
7655 op->nfso_posixlock = 0;
7656 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
7657 /* If the 2nd element == NFS_OK, the Getattr succeeded. */
7659 error = nfsv4_loadattr(nd, NULL, &nfsva, NULL,
7660 NULL, 0, NULL, NULL, NULL, NULL, NULL, 0,
7661 NULL, NULL, NULL, p, cred);
7665 ndp->nfsdl_change = nfsva.na_filerev;
7666 ndp->nfsdl_modtime = nfsva.na_mtime;
7667 ndp->nfsdl_flags |= NFSCLDL_MODTIMESET;
7672 * At this point, the Open has succeeded, so set
7673 * nd_repstat = NFS_OK. If the Layoutget failed,
7674 * this function just won't return a layout.
7676 if (nd->nd_repstat == 0) {
7677 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
7678 *laystatp = fxdr_unsigned(int, *++tl);
7679 if (*laystatp == 0) {
7680 error = nfsrv_parselayoutget(nmp, nd,
7681 stateidp, retonclosep, flhp);
7686 nd->nd_repstat = 0; /* Return 0 for Open. */
7689 if (nd->nd_repstat != 0 && error == 0)
7690 error = nd->nd_repstat;
7692 free(ndp, M_NFSCLDELEG);
7693 m_freem(nd->nd_mrep);
7698 * Similar nfsrpc_createv4(), but also does the LayoutGet operation.
7699 * Used only for mounts with pNFS enabled.
7702 nfsrpc_createlayout(vnode_t dvp, char *name, int namelen, struct vattr *vap,
7703 nfsquad_t cverf, int fmode, struct nfsclowner *owp, struct nfscldeleg **dpp,
7704 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap,
7705 struct nfsvattr *nnap, struct nfsfh **nfhpp, int *attrflagp,
7706 int *dattrflagp, void *dstuff, int *unlockedp, nfsv4stateid_t *stateidp,
7707 int usecurstateid, int layouttype, int layoutlen, int *retonclosep,
7708 struct nfsclflayouthead *flhp, int *laystatp)
7711 int error = 0, deleg, newone, ret, acesize, limitby;
7712 struct nfsrv_descript nfsd, *nd = &nfsd;
7713 struct nfsclopen *op;
7714 struct nfscldeleg *dp = NULL;
7717 struct nfsclsession *tsep;
7718 nfsattrbit_t attrbits;
7719 nfsv4stateid_t stateid;
7720 struct nfsmount *nmp;
7722 nmp = VFSTONFS(dvp->v_mount);
7730 if (namelen > NFS_MAXNAMLEN)
7731 return (ENAMETOOLONG);
7732 NFSCL_REQSTART(nd, NFSPROC_CREATELAYGET, dvp);
7734 * For V4, this is actually an Open op.
7736 NFSM_BUILD(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
7737 *tl++ = txdr_unsigned(owp->nfsow_seqid);
7738 *tl++ = txdr_unsigned(NFSV4OPEN_ACCESSWRITE |
7739 NFSV4OPEN_ACCESSREAD);
7740 *tl++ = txdr_unsigned(NFSV4OPEN_DENYNONE);
7741 tsep = nfsmnt_mdssession(nmp);
7742 *tl++ = tsep->nfsess_clientid.lval[0];
7743 *tl = tsep->nfsess_clientid.lval[1];
7744 nfsm_strtom(nd, owp->nfsow_owner, NFSV4CL_LOCKNAMELEN);
7745 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
7746 *tl++ = txdr_unsigned(NFSV4OPEN_CREATE);
7747 if ((fmode & O_EXCL) != 0) {
7748 if (NFSHASSESSPERSIST(nmp)) {
7749 /* Use GUARDED for persistent sessions. */
7750 *tl = txdr_unsigned(NFSCREATE_GUARDED);
7751 nfscl_fillsattr(nd, vap, dvp, 0, 0);
7753 /* Otherwise, use EXCLUSIVE4_1. */
7754 *tl = txdr_unsigned(NFSCREATE_EXCLUSIVE41);
7755 NFSM_BUILD(tl, u_int32_t *, NFSX_VERF);
7756 *tl++ = cverf.lval[0];
7757 *tl = cverf.lval[1];
7758 nfscl_fillsattr(nd, vap, dvp, 0, 0);
7761 *tl = txdr_unsigned(NFSCREATE_UNCHECKED);
7762 nfscl_fillsattr(nd, vap, dvp, 0, 0);
7764 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
7765 *tl = txdr_unsigned(NFSV4OPEN_CLAIMNULL);
7766 nfsm_strtom(nd, name, namelen);
7767 /* Get the new file's handle and attributes, plus save the FH. */
7768 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
7769 *tl++ = txdr_unsigned(NFSV4OP_SAVEFH);
7770 *tl++ = txdr_unsigned(NFSV4OP_GETFH);
7771 *tl = txdr_unsigned(NFSV4OP_GETATTR);
7772 NFSGETATTR_ATTRBIT(&attrbits);
7773 nfsrv_putattrbit(nd, &attrbits);
7774 /* Get the directory's post-op attributes. */
7775 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
7776 *tl = txdr_unsigned(NFSV4OP_PUTFH);
7777 nfsm_fhtom(nd, np->n_fhp->nfh_fh, np->n_fhp->nfh_len, 0);
7778 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
7779 *tl = txdr_unsigned(NFSV4OP_GETATTR);
7780 nfsrv_putattrbit(nd, &attrbits);
7781 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
7782 *tl++ = txdr_unsigned(NFSV4OP_RESTOREFH);
7783 *tl = txdr_unsigned(NFSV4OP_LAYOUTGET);
7784 nfsrv_setuplayoutget(nd, NFSLAYOUTIOMODE_RW, 0, UINT64_MAX, 0, stateidp,
7785 layouttype, layoutlen, usecurstateid);
7786 error = nfscl_request(nd, dvp, p, cred, dstuff);
7789 NFSCL_DEBUG(4, "nfsrpc_createlayout stat=%d err=%d\n", nd->nd_repstat,
7791 if (nd->nd_repstat != 0)
7792 *laystatp = nd->nd_repstat;
7793 NFSCL_INCRSEQID(owp->nfsow_seqid, nd);
7794 if ((nd->nd_flag & ND_NOMOREDATA) == 0) {
7795 NFSCL_DEBUG(4, "nfsrpc_createlayout open succeeded\n");
7796 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
7798 stateid.seqid = *tl++;
7799 stateid.other[0] = *tl++;
7800 stateid.other[1] = *tl++;
7801 stateid.other[2] = *tl;
7802 nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
7803 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
7804 deleg = fxdr_unsigned(int, *tl);
7805 if (deleg == NFSV4OPEN_DELEGATEREAD ||
7806 deleg == NFSV4OPEN_DELEGATEWRITE) {
7807 if (!(owp->nfsow_clp->nfsc_flags &
7808 NFSCLFLAGS_FIRSTDELEG))
7809 owp->nfsow_clp->nfsc_flags |=
7810 (NFSCLFLAGS_FIRSTDELEG | NFSCLFLAGS_GOTDELEG);
7811 dp = malloc(sizeof(struct nfscldeleg) + NFSX_V4FHMAX,
7812 M_NFSCLDELEG, M_WAITOK);
7813 LIST_INIT(&dp->nfsdl_owner);
7814 LIST_INIT(&dp->nfsdl_lock);
7815 dp->nfsdl_clp = owp->nfsow_clp;
7816 newnfs_copyincred(cred, &dp->nfsdl_cred);
7817 nfscl_lockinit(&dp->nfsdl_rwlock);
7818 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
7820 dp->nfsdl_stateid.seqid = *tl++;
7821 dp->nfsdl_stateid.other[0] = *tl++;
7822 dp->nfsdl_stateid.other[1] = *tl++;
7823 dp->nfsdl_stateid.other[2] = *tl++;
7824 ret = fxdr_unsigned(int, *tl);
7825 if (deleg == NFSV4OPEN_DELEGATEWRITE) {
7826 dp->nfsdl_flags = NFSCLDL_WRITE;
7828 * Indicates how much the file can grow.
7830 NFSM_DISSECT(tl, u_int32_t *,
7832 limitby = fxdr_unsigned(int, *tl++);
7834 case NFSV4OPEN_LIMITSIZE:
7835 dp->nfsdl_sizelimit = fxdr_hyper(tl);
7837 case NFSV4OPEN_LIMITBLOCKS:
7838 dp->nfsdl_sizelimit =
7839 fxdr_unsigned(u_int64_t, *tl++);
7840 dp->nfsdl_sizelimit *=
7841 fxdr_unsigned(u_int64_t, *tl);
7844 error = NFSERR_BADXDR;
7848 dp->nfsdl_flags = NFSCLDL_READ;
7851 dp->nfsdl_flags |= NFSCLDL_RECALL;
7852 error = nfsrv_dissectace(nd, &dp->nfsdl_ace, &ret,
7856 } else if (deleg != NFSV4OPEN_DELEGATENONE) {
7857 error = NFSERR_BADXDR;
7861 /* Now, we should have the status for the SaveFH. */
7862 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
7864 NFSCL_DEBUG(4, "nfsrpc_createlayout SaveFH ok\n");
7866 * Now, process the GetFH and Getattr for the newly
7867 * created file. nfscl_mtofh() will set
7868 * ND_NOMOREDATA if these weren't successful.
7870 error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp);
7871 NFSCL_DEBUG(4, "aft nfscl_mtofh err=%d\n", error);
7875 nd->nd_flag |= ND_NOMOREDATA;
7876 /* Now we have the PutFH and Getattr for the directory. */
7877 if ((nd->nd_flag & ND_NOMOREDATA) == 0) {
7878 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
7880 nd->nd_flag |= ND_NOMOREDATA;
7882 NFSM_DISSECT(tl, uint32_t *, 2 *
7885 nd->nd_flag |= ND_NOMOREDATA;
7888 if ((nd->nd_flag & ND_NOMOREDATA) == 0) {
7889 /* Load the directory attributes. */
7890 error = nfsm_loadattr(nd, dnap);
7891 NFSCL_DEBUG(4, "aft nfsm_loadattr err=%d\n", error);
7895 if (dp != NULL && *attrflagp != 0) {
7896 dp->nfsdl_change = nnap->na_filerev;
7897 dp->nfsdl_modtime = nnap->na_mtime;
7898 dp->nfsdl_flags |= NFSCLDL_MODTIMESET;
7901 * We can now complete the Open state.
7905 dp->nfsdl_fhlen = nfhp->nfh_len;
7906 NFSBCOPY(nfhp->nfh_fh, dp->nfsdl_fh,
7910 * Get an Open structure that will be
7911 * attached to the OpenOwner, acquired already.
7913 error = nfscl_open(dvp, nfhp->nfh_fh, nfhp->nfh_len,
7914 (NFSV4OPEN_ACCESSWRITE | NFSV4OPEN_ACCESSREAD), 0,
7915 cred, p, NULL, &op, &newone, NULL, 0);
7918 op->nfso_stateid = stateid;
7919 newnfs_copyincred(cred, &op->nfso_cred);
7921 nfscl_openrelease(nmp, op, error, newone);
7924 /* Now, handle the RestoreFH and LayoutGet. */
7925 if (nd->nd_repstat == 0) {
7926 NFSM_DISSECT(tl, uint32_t *, 4 * NFSX_UNSIGNED);
7927 *laystatp = fxdr_unsigned(int, *(tl + 3));
7928 if (*laystatp == 0) {
7929 error = nfsrv_parselayoutget(nmp, nd,
7930 stateidp, retonclosep, flhp);
7934 NFSCL_DEBUG(4, "aft nfsrv_parselayout err=%d\n",
7940 if (nd->nd_repstat != 0 && error == 0)
7941 error = nd->nd_repstat;
7942 if (error == NFSERR_STALECLIENTID || error == NFSERR_BADSESSION)
7943 nfscl_initiate_recovery(owp->nfsow_clp);
7945 NFSCL_DEBUG(4, "eo nfsrpc_createlayout err=%d\n", error);
7949 free(dp, M_NFSCLDELEG);
7950 m_freem(nd->nd_mrep);
7955 * Similar to nfsrpc_getopenlayout(), except that it used for the Create case.
7958 nfsrpc_getcreatelayout(vnode_t dvp, char *name, int namelen, struct vattr *vap,
7959 nfsquad_t cverf, int fmode, struct nfsclowner *owp, struct nfscldeleg **dpp,
7960 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap,
7961 struct nfsvattr *nnap, struct nfsfh **nfhpp, int *attrflagp,
7962 int *dattrflagp, void *dstuff, int *unlockedp)
7964 struct nfscllayout *lyp;
7965 struct nfsclflayouthead flh;
7967 struct nfsclsession *tsep;
7968 struct nfsmount *nmp;
7969 nfsv4stateid_t stateid;
7970 int error, layoutlen, layouttype, retonclose, laystat;
7973 nmp = VFSTONFS(dvp->v_mount);
7974 if (NFSHASFLEXFILE(nmp))
7975 layouttype = NFSLAYOUT_FLEXFILE;
7977 layouttype = NFSLAYOUT_NFSV4_1_FILES;
7979 tsep = nfsmnt_mdssession(nmp);
7980 layoutlen = tsep->nfsess_maxcache - (NFSX_STATEID + 3 * NFSX_UNSIGNED);
7981 error = nfsrpc_createlayout(dvp, name, namelen, vap, cverf, fmode,
7982 owp, dpp, cred, p, dnap, nnap, nfhpp, attrflagp, dattrflagp,
7983 dstuff, unlockedp, &stateid, 1, layouttype, layoutlen, &retonclose,
7985 NFSCL_DEBUG(4, "aft nfsrpc_createlayoutrpc laystat=%d err=%d\n",
7990 laystat = nfsrpc_layoutgetres(nmp, dvp, nfhp->nfh_fh,
7991 nfhp->nfh_len, &stateid, retonclose, NULL, &lyp, &flh,
7992 layouttype, laystat, NULL, cred, p);
7994 laystat = nfsrpc_layoutgetres(nmp, dvp, NULL, 0, &stateid,
7995 retonclose, NULL, &lyp, &flh, layouttype, laystat, NULL,
7998 nfscl_rellayout(lyp, 0);
8003 * Process the results of a layoutget() operation.
8006 nfsrpc_layoutgetres(struct nfsmount *nmp, vnode_t vp, uint8_t *newfhp,
8007 int newfhlen, nfsv4stateid_t *stateidp, int retonclose, uint32_t *notifybit,
8008 struct nfscllayout **lypp, struct nfsclflayouthead *flhp, int layouttype,
8009 int laystat, int *islockedp, struct ucred *cred, NFSPROC_T *p)
8011 struct nfsclflayout *tflp;
8012 struct nfscldevinfo *dip;
8016 if (laystat == NFSERR_UNKNLAYOUTTYPE) {
8018 if (!NFSHASFLEXFILE(nmp)) {
8019 /* Switch to using Flex File Layout. */
8020 nmp->nm_state |= NFSSTA_FLEXFILE;
8021 } else if (layouttype == NFSLAYOUT_FLEXFILE) {
8023 NFSCL_DEBUG(1, "disable PNFS\n");
8024 nmp->nm_state &= ~(NFSSTA_PNFS | NFSSTA_FLEXFILE);
8029 NFSCL_DEBUG(4, "nfsrpc_layoutgetres at FOREACH\n");
8030 LIST_FOREACH(tflp, flhp, nfsfl_list) {
8031 if (layouttype == NFSLAYOUT_FLEXFILE)
8032 mirrorcnt = tflp->nfsfl_mirrorcnt;
8035 for (i = 0; i < mirrorcnt; i++) {
8036 laystat = nfscl_adddevinfo(nmp, NULL, i, tflp);
8037 NFSCL_DEBUG(4, "aft adddev=%d\n", laystat);
8039 if (layouttype == NFSLAYOUT_FLEXFILE)
8040 dev = tflp->nfsfl_ffm[i].dev;
8042 dev = tflp->nfsfl_dev;
8043 laystat = nfsrpc_getdeviceinfo(nmp, dev,
8044 layouttype, notifybit, &dip, cred,
8046 NFSCL_DEBUG(4, "aft nfsrpc_gdi=%d\n",
8050 laystat = nfscl_adddevinfo(nmp, dip, i,
8053 printf("nfsrpc_layoutgetresout"
8062 * nfscl_layout() always returns with the nfsly_lock
8063 * set to a refcnt (shared lock).
8064 * Passing in dvp is sufficient, since it is only used to
8065 * get the fsid for the file system.
8067 laystat = nfscl_layout(nmp, vp, newfhp, newfhlen, stateidp,
8068 layouttype, retonclose, flhp, lypp, cred, p);
8069 NFSCL_DEBUG(4, "nfsrpc_layoutgetres: aft nfscl_layout=%d\n",
8071 if (laystat == 0 && islockedp != NULL)
8078 * nfs copy_file_range operation.
8081 nfsrpc_copy_file_range(vnode_t invp, off_t *inoffp, vnode_t outvp,
8082 off_t *outoffp, size_t *lenp, unsigned int flags, int *inattrflagp,
8083 struct nfsvattr *innap, int *outattrflagp, struct nfsvattr *outnap,
8084 struct ucred *cred, bool consecutive, bool *must_commitp)
8086 int commit, error, expireret = 0, retrycnt;
8087 u_int32_t clidrev = 0;
8088 struct nfsmount *nmp = VFSTONFS(vnode_mount(invp));
8089 struct nfsfh *innfhp = NULL, *outnfhp = NULL;
8090 nfsv4stateid_t instateid, outstateid;
8091 void *inlckp, *outlckp;
8093 if (nmp->nm_clp != NULL)
8094 clidrev = nmp->nm_clp->nfsc_clientidrev;
8095 innfhp = VTONFS(invp)->n_fhp;
8096 outnfhp = VTONFS(outvp)->n_fhp;
8099 /* Get both stateids. */
8101 nfscl_getstateid(invp, innfhp->nfh_fh, innfhp->nfh_len,
8102 NFSV4OPEN_ACCESSREAD, 0, NULL, curthread, &instateid,
8105 nfscl_getstateid(outvp, outnfhp->nfh_fh, outnfhp->nfh_len,
8106 NFSV4OPEN_ACCESSWRITE, 0, NULL, curthread, &outstateid,
8109 error = nfsrpc_copyrpc(invp, *inoffp, outvp, *outoffp, lenp,
8110 &instateid, &outstateid, innap, inattrflagp, outnap,
8111 outattrflagp, consecutive, &commit, cred, curthread);
8113 if (commit != NFSWRITE_FILESYNC)
8114 *must_commitp = true;
8117 } else if (error == NFSERR_STALESTATEID)
8118 nfscl_initiate_recovery(nmp->nm_clp);
8120 nfscl_lockderef(inlckp);
8121 if (outlckp != NULL)
8122 nfscl_lockderef(outlckp);
8123 if (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
8124 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
8125 error == NFSERR_OLDSTATEID || error == NFSERR_BADSESSION) {
8126 (void) nfs_catnap(PZERO, error, "nfs_cfr");
8127 } else if ((error == NFSERR_EXPIRED ||
8128 error == NFSERR_BADSTATEID) && clidrev != 0) {
8129 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev,
8133 } while (error == NFSERR_GRACE || error == NFSERR_DELAY ||
8134 error == NFSERR_STALESTATEID || error == NFSERR_BADSESSION ||
8135 error == NFSERR_STALEDONTRECOVER ||
8136 (error == NFSERR_OLDSTATEID && retrycnt < 20) ||
8137 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
8138 expireret == 0 && clidrev != 0 && retrycnt < 4));
8139 if (error != 0 && (retrycnt >= 4 ||
8140 error == NFSERR_STALESTATEID || error == NFSERR_BADSESSION ||
8141 error == NFSERR_STALEDONTRECOVER))
8150 nfsrpc_copyrpc(vnode_t invp, off_t inoff, vnode_t outvp, off_t outoff,
8151 size_t *lenp, nfsv4stateid_t *instateidp, nfsv4stateid_t *outstateidp,
8152 struct nfsvattr *innap, int *inattrflagp, struct nfsvattr *outnap,
8153 int *outattrflagp, bool consecutive, int *commitp, struct ucred *cred,
8158 struct nfsrv_descript nfsd;
8159 struct nfsrv_descript *nd = &nfsd;
8160 struct nfsmount *nmp;
8161 nfsattrbit_t attrbits;
8164 nmp = VFSTONFS(outvp->v_mount);
8165 *inattrflagp = *outattrflagp = 0;
8166 *commitp = NFSWRITE_UNSTABLE;
8169 if (len > nfs_maxcopyrange)
8170 len = nfs_maxcopyrange;
8171 NFSCL_REQSTART(nd, NFSPROC_COPY, invp);
8172 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
8173 *tl = txdr_unsigned(NFSV4OP_GETATTR);
8174 NFSGETATTR_ATTRBIT(&attrbits);
8175 nfsrv_putattrbit(nd, &attrbits);
8176 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
8177 *tl = txdr_unsigned(NFSV4OP_PUTFH);
8178 nfsm_fhtom(nd, VTONFS(outvp)->n_fhp->nfh_fh,
8179 VTONFS(outvp)->n_fhp->nfh_len, 0);
8180 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
8181 *tl = txdr_unsigned(NFSV4OP_COPY);
8182 nfsm_stateidtom(nd, instateidp, NFSSTATEID_PUTSTATEID);
8183 nfsm_stateidtom(nd, outstateidp, NFSSTATEID_PUTSTATEID);
8184 NFSM_BUILD(tl, uint32_t *, 3 * NFSX_HYPER + 4 * NFSX_UNSIGNED);
8185 txdr_hyper(inoff, tl); tl += 2;
8186 txdr_hyper(outoff, tl); tl += 2;
8187 txdr_hyper(len, tl); tl += 2;
8189 *tl++ = newnfs_true;
8191 *tl++ = newnfs_false;
8192 *tl++ = newnfs_true;
8194 *tl = txdr_unsigned(NFSV4OP_GETATTR);
8195 NFSWRITEGETATTR_ATTRBIT(&attrbits);
8196 nfsrv_putattrbit(nd, &attrbits);
8197 error = nfscl_request(nd, invp, p, cred, NULL);
8200 if ((nd->nd_flag & ND_NOMOREDATA) == 0) {
8201 /* Get the input file's attributes. */
8202 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
8203 if (*(tl + 1) == 0) {
8204 error = nfsm_loadattr(nd, innap);
8209 nd->nd_flag |= ND_NOMOREDATA;
8211 /* Skip over return stat for PutFH. */
8212 if ((nd->nd_flag & ND_NOMOREDATA) == 0) {
8213 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
8215 nd->nd_flag |= ND_NOMOREDATA;
8217 /* Skip over return stat for Copy. */
8218 if ((nd->nd_flag & ND_NOMOREDATA) == 0)
8219 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
8220 if (nd->nd_repstat == 0) {
8221 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
8223 /* There should be no callback ids. */
8224 error = NFSERR_BADXDR;
8227 NFSM_DISSECT(tl, uint32_t *, NFSX_HYPER + 3 * NFSX_UNSIGNED +
8229 len = fxdr_hyper(tl); tl += 2;
8230 *commitp = fxdr_unsigned(int, *tl++);
8232 if (!NFSHASWRITEVERF(nmp)) {
8233 NFSBCOPY(tl, nmp->nm_verf, NFSX_VERF);
8234 NFSSETWRITEVERF(nmp);
8235 } else if (NFSBCMP(tl, nmp->nm_verf, NFSX_VERF)) {
8236 NFSBCOPY(tl, nmp->nm_verf, NFSX_VERF);
8237 nd->nd_repstat = NFSERR_STALEWRITEVERF;
8240 tl += (NFSX_VERF / NFSX_UNSIGNED);
8241 if (nd->nd_repstat == 0 && *++tl != newnfs_true)
8242 /* Must be a synchronous copy. */
8243 nd->nd_repstat = NFSERR_NOTSUPP;
8244 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
8245 error = nfsm_loadattr(nd, outnap);
8247 *outattrflagp = NFS_LATTR_NOSHRINK;
8248 if (nd->nd_repstat == 0)
8250 } else if (nd->nd_repstat == NFSERR_OFFLOADNOREQS) {
8252 * For the case where consecutive is not supported, but
8253 * synchronous is supported, we can try consecutive == false
8254 * by returning this error. Otherwise, return NFSERR_NOTSUPP,
8255 * since Copy cannot be done.
8257 if ((nd->nd_flag & ND_NOMOREDATA) == 0) {
8258 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
8259 if (!consecutive || *++tl == newnfs_false)
8260 nd->nd_repstat = NFSERR_NOTSUPP;
8262 nd->nd_repstat = NFSERR_BADXDR;
8265 error = nd->nd_repstat;
8267 m_freem(nd->nd_mrep);
8275 nfsrpc_seek(vnode_t vp, off_t *offp, bool *eofp, int content,
8276 struct ucred *cred, struct nfsvattr *nap, int *attrflagp)
8278 int error, expireret = 0, retrycnt;
8279 u_int32_t clidrev = 0;
8280 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
8281 struct nfsnode *np = VTONFS(vp);
8282 struct nfsfh *nfhp = NULL;
8283 nfsv4stateid_t stateid;
8286 if (nmp->nm_clp != NULL)
8287 clidrev = nmp->nm_clp->nfsc_clientidrev;
8292 nfscl_getstateid(vp, nfhp->nfh_fh, nfhp->nfh_len,
8293 NFSV4OPEN_ACCESSREAD, 0, cred, curthread, &stateid, &lckp);
8294 error = nfsrpc_seekrpc(vp, offp, &stateid, eofp, content,
8295 nap, attrflagp, cred);
8296 if (error == NFSERR_STALESTATEID)
8297 nfscl_initiate_recovery(nmp->nm_clp);
8299 nfscl_lockderef(lckp);
8300 if (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
8301 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
8302 error == NFSERR_OLDSTATEID || error == NFSERR_BADSESSION) {
8303 (void) nfs_catnap(PZERO, error, "nfs_seek");
8304 } else if ((error == NFSERR_EXPIRED ||
8305 error == NFSERR_BADSTATEID) && clidrev != 0) {
8306 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev,
8310 } while (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
8311 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
8312 error == NFSERR_BADSESSION ||
8313 (error == NFSERR_OLDSTATEID && retrycnt < 20) ||
8314 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
8315 expireret == 0 && clidrev != 0 && retrycnt < 4) ||
8316 (error == NFSERR_OPENMODE && retrycnt < 4));
8317 if (error && retrycnt >= 4)
8326 nfsrpc_seekrpc(vnode_t vp, off_t *offp, nfsv4stateid_t *stateidp, bool *eofp,
8327 int content, struct nfsvattr *nap, int *attrflagp, struct ucred *cred)
8331 struct nfsrv_descript nfsd;
8332 struct nfsrv_descript *nd = &nfsd;
8333 nfsattrbit_t attrbits;
8336 NFSCL_REQSTART(nd, NFSPROC_SEEK, vp);
8337 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID);
8338 NFSM_BUILD(tl, uint32_t *, NFSX_HYPER + 2 * NFSX_UNSIGNED);
8339 txdr_hyper(*offp, tl); tl += 2;
8340 *tl++ = txdr_unsigned(content);
8341 *tl = txdr_unsigned(NFSV4OP_GETATTR);
8342 NFSGETATTR_ATTRBIT(&attrbits);
8343 nfsrv_putattrbit(nd, &attrbits);
8344 error = nfscl_request(nd, vp, curthread, cred, NULL);
8347 if (nd->nd_repstat == 0) {
8348 NFSM_DISSECT(tl, uint32_t *, 3 * NFSX_UNSIGNED + NFSX_HYPER);
8349 if (*tl++ == newnfs_true)
8353 *offp = fxdr_hyper(tl);
8354 /* Just skip over Getattr op status. */
8355 error = nfsm_loadattr(nd, nap);
8359 error = nd->nd_repstat;
8361 m_freem(nd->nd_mrep);
8366 * The getextattr RPC.
8369 nfsrpc_getextattr(vnode_t vp, const char *name, struct uio *uiop, ssize_t *lenp,
8370 struct nfsvattr *nap, int *attrflagp, struct ucred *cred, NFSPROC_T *p)
8374 struct nfsrv_descript nfsd;
8375 struct nfsrv_descript *nd = &nfsd;
8376 nfsattrbit_t attrbits;
8380 NFSCL_REQSTART(nd, NFSPROC_GETEXTATTR, vp);
8381 nfsm_strtom(nd, name, strlen(name));
8382 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
8383 *tl = txdr_unsigned(NFSV4OP_GETATTR);
8384 NFSGETATTR_ATTRBIT(&attrbits);
8385 nfsrv_putattrbit(nd, &attrbits);
8386 error = nfscl_request(nd, vp, p, cred, NULL);
8389 if (nd->nd_repstat == 0) {
8390 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
8391 len = fxdr_unsigned(uint32_t, *tl);
8392 /* Sanity check lengths. */
8393 if (uiop != NULL && len > 0 && len <= IOSIZE_MAX &&
8394 uiop->uio_resid <= UINT32_MAX) {
8395 len2 = uiop->uio_resid;
8397 error = nfsm_mbufuio(nd, uiop, len);
8399 error = nfsm_mbufuio(nd, uiop, len2);
8402 * nfsm_mbufuio() advances to a multiple
8403 * of 4, so round up len2 as well. Then
8404 * we need to advance over the rest of
8405 * the data, rounding up the remaining
8408 len2 = NFSM_RNDUP(len2);
8409 len2 = NFSM_RNDUP(len - len2);
8411 error = nfsm_advance(nd, len2,
8415 } else if (uiop == NULL && len > 0) {
8416 /* Just wants the length and not the data. */
8417 error = nfsm_advance(nd, NFSM_RNDUP(len), -1);
8423 /* Just skip over Getattr op status. */
8424 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
8425 error = nfsm_loadattr(nd, nap);
8430 error = nd->nd_repstat;
8432 m_freem(nd->nd_mrep);
8437 * The setextattr RPC.
8440 nfsrpc_setextattr(vnode_t vp, const char *name, struct uio *uiop,
8441 struct nfsvattr *nap, int *attrflagp, struct ucred *cred, NFSPROC_T *p)
8445 struct nfsrv_descript nfsd;
8446 struct nfsrv_descript *nd = &nfsd;
8447 nfsattrbit_t attrbits;
8450 NFSCL_REQSTART(nd, NFSPROC_SETEXTATTR, vp);
8451 if (uiop->uio_resid > nd->nd_maxreq) {
8452 /* nd_maxreq is set by NFSCL_REQSTART(). */
8453 m_freem(nd->nd_mreq);
8456 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
8457 *tl = txdr_unsigned(NFSV4SXATTR_EITHER);
8458 nfsm_strtom(nd, name, strlen(name));
8459 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
8460 *tl = txdr_unsigned(uiop->uio_resid);
8461 nfsm_uiombuf(nd, uiop, uiop->uio_resid);
8462 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
8463 *tl = txdr_unsigned(NFSV4OP_GETATTR);
8464 NFSGETATTR_ATTRBIT(&attrbits);
8465 nfsrv_putattrbit(nd, &attrbits);
8466 error = nfscl_request(nd, vp, p, cred, NULL);
8469 if (nd->nd_repstat == 0) {
8470 /* Just skip over the reply and Getattr op status. */
8471 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_HYPER + 3 *
8473 error = nfsm_loadattr(nd, nap);
8478 error = nd->nd_repstat;
8480 m_freem(nd->nd_mrep);
8485 * The removeextattr RPC.
8488 nfsrpc_rmextattr(vnode_t vp, const char *name, struct nfsvattr *nap,
8489 int *attrflagp, struct ucred *cred, NFSPROC_T *p)
8493 struct nfsrv_descript nfsd;
8494 struct nfsrv_descript *nd = &nfsd;
8495 nfsattrbit_t attrbits;
8498 NFSCL_REQSTART(nd, NFSPROC_RMEXTATTR, vp);
8499 nfsm_strtom(nd, name, strlen(name));
8500 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
8501 *tl = txdr_unsigned(NFSV4OP_GETATTR);
8502 NFSGETATTR_ATTRBIT(&attrbits);
8503 nfsrv_putattrbit(nd, &attrbits);
8504 error = nfscl_request(nd, vp, p, cred, NULL);
8507 if (nd->nd_repstat == 0) {
8508 /* Just skip over the reply and Getattr op status. */
8509 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_HYPER + 3 *
8511 error = nfsm_loadattr(nd, nap);
8516 error = nd->nd_repstat;
8518 m_freem(nd->nd_mrep);
8523 * The listextattr RPC.
8526 nfsrpc_listextattr(vnode_t vp, uint64_t *cookiep, struct uio *uiop,
8527 size_t *lenp, bool *eofp, struct nfsvattr *nap, int *attrflagp,
8528 struct ucred *cred, NFSPROC_T *p)
8531 int cnt, error, i, len;
8532 struct nfsrv_descript nfsd;
8533 struct nfsrv_descript *nd = &nfsd;
8534 nfsattrbit_t attrbits;
8538 NFSCL_REQSTART(nd, NFSPROC_LISTEXTATTR, vp);
8539 NFSM_BUILD(tl, uint32_t *, NFSX_HYPER + 2 * NFSX_UNSIGNED);
8540 txdr_hyper(*cookiep, tl); tl += 2;
8541 *tl++ = txdr_unsigned(*lenp);
8542 *tl = txdr_unsigned(NFSV4OP_GETATTR);
8543 NFSGETATTR_ATTRBIT(&attrbits);
8544 nfsrv_putattrbit(nd, &attrbits);
8545 error = nfscl_request(nd, vp, p, cred, NULL);
8550 if (nd->nd_repstat == 0) {
8551 NFSM_DISSECT(tl, uint32_t *, NFSX_HYPER + NFSX_UNSIGNED);
8552 *cookiep = fxdr_hyper(tl); tl += 2;
8553 cnt = fxdr_unsigned(int, *tl);
8558 for (i = 0; i < cnt; i++) {
8559 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
8560 len = fxdr_unsigned(int, *tl);
8561 if (len <= 0 || len > EXTATTR_MAXNAMELEN) {
8566 error = nfsm_advance(nd, NFSM_RNDUP(len), -1);
8567 else if (uiop->uio_resid >= len + 1) {
8569 error = uiomove(&c, sizeof(c), uiop);
8571 error = nfsm_mbufuio(nd, uiop, len);
8573 error = nfsm_advance(nd, NFSM_RNDUP(len), -1);
8580 /* Get the eof and skip over the Getattr op status. */
8581 NFSM_DISSECT(tl, uint32_t *, 3 * NFSX_UNSIGNED);
8583 * *eofp is set false above, because it wasn't able to copy
8586 if (*eofp && *tl == 0)
8588 error = nfsm_loadattr(nd, nap);
8593 error = nd->nd_repstat;
8595 m_freem(nd->nd_mrep);