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>
38 * Rpc op calls, generally called from the vnode op calls or through the
39 * buffer cache, for NFS v2, 3 and 4.
40 * These do not normally make any changes to vnode arguments or use
41 * structures that might change between the VFS variants. The returned
42 * arguments are all at the end, after the NFSPROC_T *p one.
45 #include "opt_inet6.h"
47 #include <fs/nfs/nfsport.h>
48 #include <fs/nfsclient/nfs.h>
49 #include <sys/extattr.h>
50 #include <sys/sysctl.h>
51 #include <sys/taskqueue.h>
53 SYSCTL_DECL(_vfs_nfs);
55 static int nfsignore_eexist = 0;
56 SYSCTL_INT(_vfs_nfs, OID_AUTO, ignore_eexist, CTLFLAG_RW,
57 &nfsignore_eexist, 0, "NFS ignore EEXIST replies for mkdir/symlink");
59 static int nfscl_dssameconn = 0;
60 SYSCTL_INT(_vfs_nfs, OID_AUTO, dssameconn, CTLFLAG_RW,
61 &nfscl_dssameconn, 0, "Use same TCP connection to multiple DSs");
63 static uint64_t nfs_maxcopyrange = SSIZE_MAX;
64 SYSCTL_U64(_vfs_nfs, OID_AUTO, maxcopyrange, CTLFLAG_RW,
65 &nfs_maxcopyrange, 0, "Max size of a Copy so RPC times reasonable");
70 extern struct nfsstatsv1 nfsstatsv1;
71 extern int nfs_numnfscbd;
72 extern struct timeval nfsboottime;
73 extern u_int32_t newnfs_false, newnfs_true;
74 extern nfstype nfsv34_type[9];
75 extern int nfsrv_useacl;
76 extern char nfsv4_callbackaddr[INET6_ADDRSTRLEN];
77 extern int nfscl_debuglevel;
78 extern int nfs_pnfsiothreads;
79 extern u_long sb_max_adj;
81 int nfstest_outofseq = 0;
82 int nfscl_assumeposixlocks = 1;
83 int nfscl_enablecallb = 0;
84 short nfsv4_cbport = NFSV4_CBPORT;
85 int nfstest_openallsetattr = 0;
87 #define DIRHDSIZ offsetof(struct dirent, d_name)
90 * nfscl_getsameserver() can return one of three values:
91 * NFSDSP_USETHISSESSION - Use this session for the DS.
92 * NFSDSP_SEQTHISSESSION - Use the nfsclds_sequence field of this dsp for new
94 * NFSDSP_NOTFOUND - No matching server was found.
97 NFSDSP_USETHISSESSION = 0,
98 NFSDSP_SEQTHISSESSION = 1,
103 * Do a write RPC on a DS data file, using this structure for the arguments,
104 * so that this function can be executed by a separate kernel process.
106 struct nfsclwritedsdorpc {
113 nfsv4stateid_t *stateidp;
129 static int nfsrpc_setattrrpc(vnode_t , struct vattr *, nfsv4stateid_t *,
130 struct ucred *, NFSPROC_T *, struct nfsvattr *, int *, void *);
131 static int nfsrpc_readrpc(vnode_t , struct uio *, struct ucred *,
132 nfsv4stateid_t *, NFSPROC_T *, struct nfsvattr *, int *, void *);
133 static int nfsrpc_writerpc(vnode_t , struct uio *, int *, int *,
134 struct ucred *, nfsv4stateid_t *, NFSPROC_T *, struct nfsvattr *, int *,
136 static int nfsrpc_createv23(vnode_t , char *, int, struct vattr *,
137 nfsquad_t, int, struct ucred *, NFSPROC_T *, struct nfsvattr *,
138 struct nfsvattr *, struct nfsfh **, int *, int *, void *);
139 static int nfsrpc_createv4(vnode_t , char *, int, struct vattr *,
140 nfsquad_t, int, struct nfsclowner *, struct nfscldeleg **, struct ucred *,
141 NFSPROC_T *, struct nfsvattr *, struct nfsvattr *, struct nfsfh **, int *,
142 int *, void *, int *);
143 static int nfsrpc_locku(struct nfsrv_descript *, struct nfsmount *,
144 struct nfscllockowner *, u_int64_t, u_int64_t,
145 u_int32_t, struct ucred *, NFSPROC_T *, int);
146 static int nfsrpc_setaclrpc(vnode_t, struct ucred *, NFSPROC_T *,
147 struct acl *, nfsv4stateid_t *, void *);
148 static int nfsrpc_layouterror(struct nfsmount *, uint8_t *, int, uint64_t,
149 uint64_t, nfsv4stateid_t *, struct ucred *, NFSPROC_T *, uint32_t,
151 static int nfsrpc_getlayout(struct nfsmount *, vnode_t, struct nfsfh *, int,
152 uint32_t, uint32_t *, nfsv4stateid_t *, uint64_t, struct nfscllayout **,
153 struct ucred *, NFSPROC_T *);
154 static int nfsrpc_fillsa(struct nfsmount *, struct sockaddr_in *,
155 struct sockaddr_in6 *, sa_family_t, int, int, struct nfsclds **,
157 static void nfscl_initsessionslots(struct nfsclsession *);
158 static int nfscl_doflayoutio(vnode_t, struct uio *, int *, int *, int *,
159 nfsv4stateid_t *, int, struct nfscldevinfo *, struct nfscllayout *,
160 struct nfsclflayout *, uint64_t, uint64_t, int, struct ucred *,
162 static int nfscl_dofflayoutio(vnode_t, struct uio *, int *, int *, int *,
163 nfsv4stateid_t *, int, struct nfscldevinfo *, struct nfscllayout *,
164 struct nfsclflayout *, uint64_t, uint64_t, int, int, struct mbuf *,
165 struct nfsclwritedsdorpc *, struct ucred *, NFSPROC_T *);
166 static int nfsrpc_readds(vnode_t, struct uio *, nfsv4stateid_t *, int *,
167 struct nfsclds *, uint64_t, int, struct nfsfh *, int, int, int,
168 struct ucred *, NFSPROC_T *);
169 static int nfsrpc_writeds(vnode_t, struct uio *, int *, int *,
170 nfsv4stateid_t *, struct nfsclds *, uint64_t, int,
171 struct nfsfh *, int, int, int, int, struct ucred *, NFSPROC_T *);
172 static int nfsio_writedsmir(vnode_t, int *, int *, nfsv4stateid_t *,
173 struct nfsclds *, uint64_t, int, struct nfsfh *, struct mbuf *, int, int,
174 struct nfsclwritedsdorpc *, struct ucred *, NFSPROC_T *);
175 static int nfsrpc_writedsmir(vnode_t, int *, int *, nfsv4stateid_t *,
176 struct nfsclds *, uint64_t, int, struct nfsfh *, struct mbuf *, int, int,
177 struct ucred *, NFSPROC_T *);
178 static enum nfsclds_state nfscl_getsameserver(struct nfsmount *,
179 struct nfsclds *, struct nfsclds **, uint32_t *);
180 static int nfsio_commitds(vnode_t, uint64_t, int, struct nfsclds *,
181 struct nfsfh *, int, int, struct nfsclwritedsdorpc *, struct ucred *,
183 static int nfsrpc_commitds(vnode_t, uint64_t, int, struct nfsclds *,
184 struct nfsfh *, int, int, struct ucred *, NFSPROC_T *);
186 static int nfsio_adviseds(vnode_t, uint64_t, int, int, struct nfsclds *,
187 struct nfsfh *, int, int, struct nfsclwritedsdorpc *, struct ucred *,
189 static int nfsrpc_adviseds(vnode_t, uint64_t, int, int, struct nfsclds *,
190 struct nfsfh *, int, int, struct ucred *, NFSPROC_T *);
192 static int nfsrpc_allocaterpc(vnode_t, off_t, off_t, nfsv4stateid_t *,
193 struct nfsvattr *, int *, struct ucred *, NFSPROC_T *, void *);
194 static void nfsrv_setuplayoutget(struct nfsrv_descript *, int, uint64_t,
195 uint64_t, uint64_t, nfsv4stateid_t *, int, int, int);
196 static int nfsrv_parseug(struct nfsrv_descript *, int, uid_t *, gid_t *,
198 static int nfsrv_parselayoutget(struct nfsmount *, struct nfsrv_descript *,
199 nfsv4stateid_t *, int *, struct nfsclflayouthead *);
200 static int nfsrpc_getopenlayout(struct nfsmount *, vnode_t, u_int8_t *,
201 int, uint8_t *, int, uint32_t, struct nfsclopen *, uint8_t *, int,
202 struct nfscldeleg **, struct ucred *, NFSPROC_T *);
203 static int nfsrpc_getcreatelayout(vnode_t, char *, int, struct vattr *,
204 nfsquad_t, int, struct nfsclowner *, struct nfscldeleg **,
205 struct ucred *, NFSPROC_T *, struct nfsvattr *, struct nfsvattr *,
206 struct nfsfh **, int *, int *, void *, int *);
207 static int nfsrpc_openlayoutrpc(struct nfsmount *, vnode_t, u_int8_t *,
208 int, uint8_t *, int, uint32_t, struct nfsclopen *, uint8_t *, int,
209 struct nfscldeleg **, nfsv4stateid_t *, int, int, int, int *,
210 struct nfsclflayouthead *, int *, struct ucred *, NFSPROC_T *);
211 static int nfsrpc_createlayout(vnode_t, char *, int, struct vattr *,
212 nfsquad_t, int, struct nfsclowner *, struct nfscldeleg **,
213 struct ucred *, NFSPROC_T *, struct nfsvattr *, struct nfsvattr *,
214 struct nfsfh **, int *, int *, void *, int *, nfsv4stateid_t *,
215 int, int, int, int *, struct nfsclflayouthead *, int *);
216 static int nfsrpc_layoutget(struct nfsmount *, uint8_t *, int, int, uint64_t,
217 uint64_t, uint64_t, int, int, nfsv4stateid_t *, int *,
218 struct nfsclflayouthead *, struct ucred *, NFSPROC_T *, void *);
219 static int nfsrpc_layoutgetres(struct nfsmount *, vnode_t, uint8_t *,
220 int, nfsv4stateid_t *, int, uint32_t *, struct nfscllayout **,
221 struct nfsclflayouthead *, int, int, int *, struct ucred *, NFSPROC_T *);
222 static int nfsrpc_copyrpc(vnode_t, off_t, vnode_t, off_t, size_t *,
223 nfsv4stateid_t *, nfsv4stateid_t *, struct nfsvattr *, int *,
224 struct nfsvattr *, int *, bool, int *, struct ucred *, NFSPROC_T *);
225 static int nfsrpc_seekrpc(vnode_t, off_t *, nfsv4stateid_t *, bool *,
226 int, struct nfsvattr *, int *, struct ucred *);
227 static struct mbuf *nfsm_split(struct mbuf *, uint64_t);
228 static void nfscl_statfs(struct vnode *, struct ucred *, NFSPROC_T *);
230 int nfs_pnfsio(task_fn_t *, void *);
233 * nfs null call from vfs.
236 nfsrpc_null(vnode_t vp, struct ucred *cred, NFSPROC_T *p)
239 struct nfsrv_descript nfsd, *nd = &nfsd;
241 NFSCL_REQSTART(nd, NFSPROC_NULL, vp, NULL);
242 error = nfscl_request(nd, vp, p, cred, NULL);
243 if (nd->nd_repstat && !error)
244 error = nd->nd_repstat;
245 m_freem(nd->nd_mrep);
251 * For nfs version 3 and 4, use the access rpc to check accessibility. If file
252 * modes are changed on the server, accesses might still fail later.
255 nfsrpc_access(vnode_t vp, int acmode, struct ucred *cred,
256 NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp)
259 u_int32_t mode, rmode;
262 mode = NFSACCESS_READ;
265 if (vnode_vtype(vp) == VDIR) {
267 mode |= (NFSACCESS_MODIFY | NFSACCESS_EXTEND |
270 mode |= NFSACCESS_LOOKUP;
273 mode |= (NFSACCESS_MODIFY | NFSACCESS_EXTEND);
275 mode |= NFSACCESS_EXECUTE;
279 * Now, just call nfsrpc_accessrpc() to do the actual RPC.
281 error = nfsrpc_accessrpc(vp, mode, cred, p, nap, attrflagp, &rmode,
285 * The NFS V3 spec does not clarify whether or not
286 * the returned access bits can be a superset of
287 * the ones requested, so...
289 if (!error && (rmode & mode) != mode)
295 * The actual rpc, separated out for Darwin.
298 nfsrpc_accessrpc(vnode_t vp, u_int32_t mode, struct ucred *cred,
299 NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, u_int32_t *rmodep,
303 u_int32_t supported, rmode;
305 struct nfsrv_descript nfsd, *nd = &nfsd;
306 nfsattrbit_t attrbits;
307 struct nfsmount *nmp;
312 nmp = VFSTONFS(vp->v_mount);
314 if ((nmp->nm_privflag & NFSMNTP_FAKEROOTFH) != 0 &&
315 nmp->nm_fhsize == 0) {
316 /* Attempt to get the actual root file handle. */
317 error = nfsrpc_getdirpath(nmp, NFSMNT_DIRPATH(nmp), cred, p);
320 if (np->n_fhp->nfh_len == NFSX_FHMAX + 1)
321 nfscl_statfs(vp, cred, p);
323 NFSCL_REQSTART(nd, NFSPROC_ACCESS, vp, cred);
324 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
325 *tl = txdr_unsigned(mode);
326 if (nd->nd_flag & ND_NFSV4) {
328 * And do a Getattr op.
330 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
331 *tl = txdr_unsigned(NFSV4OP_GETATTR);
332 NFSGETATTR_ATTRBIT(&attrbits);
333 (void) nfsrv_putattrbit(nd, &attrbits);
335 error = nfscl_request(nd, vp, p, cred, stuff);
338 if (nd->nd_flag & ND_NFSV3) {
339 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
343 if (!nd->nd_repstat) {
344 if (nd->nd_flag & ND_NFSV4) {
345 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
346 supported = fxdr_unsigned(u_int32_t, *tl++);
348 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
350 rmode = fxdr_unsigned(u_int32_t, *tl);
351 if (nd->nd_flag & ND_NFSV4)
352 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
355 * It's not obvious what should be done about
356 * unsupported access modes. For now, be paranoid
357 * and clear the unsupported ones.
362 error = nd->nd_repstat;
364 m_freem(nd->nd_mrep);
372 nfsrpc_open(vnode_t vp, int amode, struct ucred *cred, NFSPROC_T *p)
374 struct nfsclopen *op;
375 struct nfscldeleg *dp;
377 struct nfsnode *np = VTONFS(vp);
378 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
379 u_int32_t mode, clidrev;
380 int ret, newone, error, expireret = 0, retrycnt;
383 * For NFSv4, Open Ops are only done on Regular Files.
385 if (vnode_vtype(vp) != VREG)
389 mode |= NFSV4OPEN_ACCESSREAD;
391 mode |= NFSV4OPEN_ACCESSWRITE;
396 { char name[100]; int namel;
397 namel = (np->n_v4->n4_namelen < 100) ? np->n_v4->n4_namelen : 99;
398 bcopy(NFS4NODENAME(np->n_v4), name, namel);
400 printf("rpcopen p=0x%x name=%s",p->p_pid,name);
401 if (nfhp->nfh_len > 0) printf(" fh=0x%x\n",nfhp->nfh_fh[12]);
402 else printf(" fhl=0\n");
407 error = nfscl_open(vp, nfhp->nfh_fh, nfhp->nfh_len, mode, 1,
408 cred, p, NULL, &op, &newone, &ret, 1, true);
412 if (nmp->nm_clp != NULL)
413 clidrev = nmp->nm_clp->nfsc_clientidrev;
416 if (ret == NFSCLOPEN_DOOPEN) {
417 if (np->n_v4 != NULL) {
419 * For the first attempt, try and get a layout, if
420 * pNFS is enabled for the mount.
422 if (!NFSHASPNFS(nmp) || nfscl_enablecallb == 0 ||
423 nfs_numnfscbd == 0 ||
424 (np->n_flag & NNOLAYOUT) != 0 || retrycnt > 0)
425 error = nfsrpc_openrpc(nmp, vp,
427 np->n_v4->n4_fhlen, np->n_fhp->nfh_fh,
428 np->n_fhp->nfh_len, mode, op,
429 NFS4NODENAME(np->n_v4),
430 np->n_v4->n4_namelen,
431 &dp, 0, 0x0, cred, p, 0, 0);
433 error = nfsrpc_getopenlayout(nmp, vp,
435 np->n_v4->n4_fhlen, np->n_fhp->nfh_fh,
436 np->n_fhp->nfh_len, mode, op,
437 NFS4NODENAME(np->n_v4),
438 np->n_v4->n4_namelen, &dp, cred, p);
441 OSBitAndAtomic((int32_t)~NDELEGMOD, (UInt32 *)&np->n_flag);
444 np->n_flag &= ~NDELEGMOD;
446 * Invalidate the attribute cache, so that
447 * attributes that pre-date the issue of a
448 * delegation are not cached, since the
449 * cached attributes will remain valid while
450 * the delegation is held.
452 NFSINVALATTRCACHE(np);
455 (void) nfscl_deleg(nmp->nm_mountp,
456 op->nfso_own->nfsow_clp,
457 nfhp->nfh_fh, nfhp->nfh_len, cred, p, &dp);
462 newnfs_copyincred(cred, &op->nfso_cred);
463 } else if (ret == NFSCLOPEN_SETCRED)
465 * This is a new local open on a delegation. It needs
466 * to have credentials so that an open can be done
467 * against the server during recovery.
469 newnfs_copyincred(cred, &op->nfso_cred);
472 * nfso_opencnt is the count of how many VOP_OPEN()s have
473 * been done on this Open successfully and a VOP_CLOSE()
474 * is expected for each of these.
475 * If error is non-zero, don't increment it, since the Open
476 * hasn't succeeded yet.
480 if (NFSHASNFSV4N(nmp) && NFSHASONEOPENOWN(nmp)) {
482 np->n_openstateid = op;
486 nfscl_openrelease(nmp, op, error, newone);
487 if (error == NFSERR_GRACE || error == NFSERR_STALECLIENTID ||
488 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
489 error == NFSERR_BADSESSION) {
490 (void) nfs_catnap(PZERO, error, "nfs_open");
491 } else if ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID)
493 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
496 } while (error == NFSERR_GRACE || error == NFSERR_STALECLIENTID ||
497 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
498 error == NFSERR_BADSESSION ||
499 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
500 expireret == 0 && clidrev != 0 && retrycnt < 4));
501 if (error && retrycnt >= 4)
507 * the actual open rpc
510 nfsrpc_openrpc(struct nfsmount *nmp, vnode_t vp, u_int8_t *nfhp, int fhlen,
511 u_int8_t *newfhp, int newfhlen, u_int32_t mode, struct nfsclopen *op,
512 u_int8_t *name, int namelen, struct nfscldeleg **dpp,
513 int reclaim, u_int32_t delegtype, struct ucred *cred, NFSPROC_T *p,
514 int syscred, int recursed)
517 struct nfsrv_descript nfsd, *nd = &nfsd;
518 struct nfscldeleg *dp, *ndp = NULL;
519 struct nfsvattr nfsva;
520 u_int32_t rflags, deleg;
521 nfsattrbit_t attrbits;
522 int error, ret, acesize, limitby;
523 struct nfsclsession *tsep;
527 nfscl_reqstart(nd, NFSPROC_OPEN, nmp, nfhp, fhlen, NULL, NULL, 0, 0,
529 NFSM_BUILD(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
530 *tl++ = txdr_unsigned(op->nfso_own->nfsow_seqid);
531 *tl++ = txdr_unsigned(mode & NFSV4OPEN_ACCESSBOTH);
532 *tl++ = txdr_unsigned((mode >> NFSLCK_SHIFT) & NFSV4OPEN_DENYBOTH);
533 tsep = nfsmnt_mdssession(nmp);
534 *tl++ = tsep->nfsess_clientid.lval[0];
535 *tl = tsep->nfsess_clientid.lval[1];
536 (void) nfsm_strtom(nd, op->nfso_own->nfsow_owner, NFSV4CL_LOCKNAMELEN);
537 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
538 *tl++ = txdr_unsigned(NFSV4OPEN_NOCREATE);
540 *tl = txdr_unsigned(NFSV4OPEN_CLAIMPREVIOUS);
541 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
542 *tl = txdr_unsigned(delegtype);
545 *tl = txdr_unsigned(NFSV4OPEN_CLAIMDELEGATECUR);
546 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID);
547 if (NFSHASNFSV4N(nmp))
550 *tl++ = dp->nfsdl_stateid.seqid;
551 *tl++ = dp->nfsdl_stateid.other[0];
552 *tl++ = dp->nfsdl_stateid.other[1];
553 *tl = dp->nfsdl_stateid.other[2];
555 *tl = txdr_unsigned(NFSV4OPEN_CLAIMNULL);
557 (void) nfsm_strtom(nd, name, namelen);
559 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
560 *tl = txdr_unsigned(NFSV4OP_GETATTR);
561 NFSZERO_ATTRBIT(&attrbits);
562 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_CHANGE);
563 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEMODIFY);
564 (void) nfsrv_putattrbit(nd, &attrbits);
566 nd->nd_flag |= ND_USEGSSNAME;
567 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, vp, p, cred,
568 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
571 NFSCL_INCRSEQID(op->nfso_own->nfsow_seqid, nd);
572 if (nd->nd_repstat == 0 || (nd->nd_repstat == NFSERR_DELAY &&
573 reclaim != 0 && (nd->nd_flag & ND_NOMOREDATA) == 0)) {
574 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
576 op->nfso_stateid.seqid = *tl++;
577 op->nfso_stateid.other[0] = *tl++;
578 op->nfso_stateid.other[1] = *tl++;
579 op->nfso_stateid.other[2] = *tl;
580 rflags = fxdr_unsigned(u_int32_t, *(tl + 6));
581 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
584 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
585 deleg = fxdr_unsigned(u_int32_t, *tl);
586 if (deleg == NFSV4OPEN_DELEGATEREAD ||
587 deleg == NFSV4OPEN_DELEGATEWRITE) {
588 if (!(op->nfso_own->nfsow_clp->nfsc_flags &
589 NFSCLFLAGS_FIRSTDELEG))
590 op->nfso_own->nfsow_clp->nfsc_flags |=
591 (NFSCLFLAGS_FIRSTDELEG | NFSCLFLAGS_GOTDELEG);
593 sizeof (struct nfscldeleg) + newfhlen,
594 M_NFSCLDELEG, M_WAITOK);
595 LIST_INIT(&ndp->nfsdl_owner);
596 LIST_INIT(&ndp->nfsdl_lock);
597 ndp->nfsdl_clp = op->nfso_own->nfsow_clp;
598 ndp->nfsdl_fhlen = newfhlen;
599 NFSBCOPY(newfhp, ndp->nfsdl_fh, newfhlen);
600 newnfs_copyincred(cred, &ndp->nfsdl_cred);
601 nfscl_lockinit(&ndp->nfsdl_rwlock);
602 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
604 ndp->nfsdl_stateid.seqid = *tl++;
605 ndp->nfsdl_stateid.other[0] = *tl++;
606 ndp->nfsdl_stateid.other[1] = *tl++;
607 ndp->nfsdl_stateid.other[2] = *tl++;
608 ret = fxdr_unsigned(int, *tl);
609 if (deleg == NFSV4OPEN_DELEGATEWRITE) {
610 ndp->nfsdl_flags = NFSCLDL_WRITE;
612 * Indicates how much the file can grow.
614 NFSM_DISSECT(tl, u_int32_t *,
616 limitby = fxdr_unsigned(int, *tl++);
618 case NFSV4OPEN_LIMITSIZE:
619 ndp->nfsdl_sizelimit = fxdr_hyper(tl);
621 case NFSV4OPEN_LIMITBLOCKS:
622 ndp->nfsdl_sizelimit =
623 fxdr_unsigned(u_int64_t, *tl++);
624 ndp->nfsdl_sizelimit *=
625 fxdr_unsigned(u_int64_t, *tl);
628 error = NFSERR_BADXDR;
632 ndp->nfsdl_flags = NFSCLDL_READ;
635 ndp->nfsdl_flags |= NFSCLDL_RECALL;
636 error = nfsrv_dissectace(nd, &ndp->nfsdl_ace, false,
640 } else if (deleg != NFSV4OPEN_DELEGATENONE) {
641 error = NFSERR_BADXDR;
644 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
645 /* If the 2nd element == NFS_OK, the Getattr succeeded. */
647 KASSERT(nd->nd_repstat == 0,
648 ("nfsrpc_openrpc: Getattr repstat"));
649 error = nfsv4_loadattr(nd, NULL, &nfsva, NULL,
650 NULL, 0, NULL, NULL, NULL, NULL, NULL, 0,
651 NULL, NULL, NULL, p, cred);
656 if (reclaim != 0 && dp != NULL) {
657 ndp->nfsdl_change = dp->nfsdl_change;
658 ndp->nfsdl_modtime = dp->nfsdl_modtime;
659 ndp->nfsdl_flags |= NFSCLDL_MODTIMESET;
660 } else if (nd->nd_repstat == 0) {
661 ndp->nfsdl_change = nfsva.na_filerev;
662 ndp->nfsdl_modtime = nfsva.na_mtime;
663 ndp->nfsdl_flags |= NFSCLDL_MODTIMESET;
665 ndp->nfsdl_flags |= NFSCLDL_RECALL;
668 if (!reclaim && (rflags & NFSV4OPEN_RESULTCONFIRM)) {
670 ret = nfsrpc_openconfirm(vp, newfhp, newfhlen, op,
672 if (ret == NFSERR_DELAY)
673 (void) nfs_catnap(PZERO, ret, "nfs_open");
674 } while (ret == NFSERR_DELAY);
677 if ((rflags & NFSV4OPEN_LOCKTYPEPOSIX) ||
678 nfscl_assumeposixlocks)
679 op->nfso_posixlock = 1;
681 op->nfso_posixlock = 0;
684 * If the server is handing out delegations, but we didn't
685 * get one because an OpenConfirm was required, try the
686 * Open again, to get a delegation. This is a harmless no-op,
687 * from a server's point of view.
689 if (!reclaim && (rflags & NFSV4OPEN_RESULTCONFIRM) &&
690 (op->nfso_own->nfsow_clp->nfsc_flags & NFSCLFLAGS_GOTDELEG)
691 && !error && dp == NULL && ndp == NULL && !recursed) {
693 ret = nfsrpc_openrpc(nmp, vp, nfhp, fhlen, newfhp,
694 newfhlen, mode, op, name, namelen, &ndp, 0, 0x0,
695 cred, p, syscred, 1);
696 if (ret == NFSERR_DELAY)
697 (void) nfs_catnap(PZERO, ret, "nfs_open2");
698 } while (ret == NFSERR_DELAY);
701 free(ndp, M_NFSCLDELEG);
704 if (ret == NFSERR_STALECLIENTID ||
705 ret == NFSERR_STALEDONTRECOVER ||
706 ret == NFSERR_BADSESSION)
711 if (nd->nd_repstat != 0 && error == 0)
712 error = nd->nd_repstat;
713 if (error == NFSERR_STALECLIENTID)
714 nfscl_initiate_recovery(op->nfso_own->nfsow_clp);
718 else if (ndp != NULL)
719 free(ndp, M_NFSCLDELEG);
720 m_freem(nd->nd_mrep);
728 nfsrpc_opendowngrade(vnode_t vp, u_int32_t mode, struct nfsclopen *op,
729 struct ucred *cred, NFSPROC_T *p)
732 struct nfsrv_descript nfsd, *nd = &nfsd;
735 NFSCL_REQSTART(nd, NFSPROC_OPENDOWNGRADE, vp, cred);
736 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID + 3 * NFSX_UNSIGNED);
737 if (NFSHASNFSV4N(VFSTONFS(vp->v_mount)))
740 *tl++ = op->nfso_stateid.seqid;
741 *tl++ = op->nfso_stateid.other[0];
742 *tl++ = op->nfso_stateid.other[1];
743 *tl++ = op->nfso_stateid.other[2];
744 *tl++ = txdr_unsigned(op->nfso_own->nfsow_seqid);
745 *tl++ = txdr_unsigned(mode & NFSV4OPEN_ACCESSBOTH);
746 *tl = txdr_unsigned((mode >> NFSLCK_SHIFT) & NFSV4OPEN_DENYBOTH);
747 error = nfscl_request(nd, vp, p, cred, NULL);
750 NFSCL_INCRSEQID(op->nfso_own->nfsow_seqid, nd);
751 if (!nd->nd_repstat) {
752 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID);
753 op->nfso_stateid.seqid = *tl++;
754 op->nfso_stateid.other[0] = *tl++;
755 op->nfso_stateid.other[1] = *tl++;
756 op->nfso_stateid.other[2] = *tl;
758 if (nd->nd_repstat && error == 0)
759 error = nd->nd_repstat;
760 if (error == NFSERR_STALESTATEID)
761 nfscl_initiate_recovery(op->nfso_own->nfsow_clp);
763 m_freem(nd->nd_mrep);
768 * V4 Close operation.
771 nfsrpc_close(vnode_t vp, int doclose, NFSPROC_T *p)
773 struct nfsclclient *clp;
776 if (vnode_vtype(vp) != VREG)
779 error = nfscl_doclose(vp, &clp, p);
781 error = nfscl_getclose(vp, &clp);
783 nfscl_clientrelease(clp);
792 nfsrpc_doclose(struct nfsmount *nmp, struct nfsclopen *op, NFSPROC_T *p,
793 bool loop_on_delayed, bool freeop)
795 struct nfsrv_descript nfsd, *nd = &nfsd;
796 struct nfscllockowner *lp, *nlp;
797 struct nfscllock *lop, *nlop;
799 u_int64_t off = 0, len = 0;
800 u_int32_t type = NFSV4LOCKT_READ;
801 int error, do_unlock, trycnt;
803 tcred = newnfs_getcred();
804 newnfs_copycred(&op->nfso_cred, tcred);
806 * (Theoretically this could be done in the same
807 * compound as the close, but having multiple
808 * sequenced Ops in the same compound might be
809 * too scary for some servers.)
811 if (op->nfso_posixlock) {
814 type = NFSV4LOCKT_READ;
818 * Since this function is only called from VOP_INACTIVE(), no
819 * other thread will be manipulating this Open. As such, the
820 * lock lists are not being changed by other threads, so it should
821 * be safe to do this without locking.
823 LIST_FOREACH(lp, &op->nfso_lock, nfsl_list) {
825 LIST_FOREACH_SAFE(lop, &lp->nfsl_lock, nfslo_list, nlop) {
826 if (op->nfso_posixlock == 0) {
827 off = lop->nfslo_first;
828 len = lop->nfslo_end - lop->nfslo_first;
829 if (lop->nfslo_type == F_WRLCK)
830 type = NFSV4LOCKT_WRITE;
832 type = NFSV4LOCKT_READ;
837 error = nfsrpc_locku(nd, nmp, lp, off,
838 len, type, tcred, p, 0);
839 if ((nd->nd_repstat == NFSERR_GRACE ||
840 nd->nd_repstat == NFSERR_DELAY) &&
842 (void) nfs_catnap(PZERO,
845 } while ((nd->nd_repstat == NFSERR_GRACE ||
846 nd->nd_repstat == NFSERR_DELAY) &&
847 error == 0 && trycnt++ < 5);
848 if (op->nfso_posixlock)
851 nfscl_freelock(lop, 0);
854 * Do a ReleaseLockOwner.
855 * The lock owner name nfsl_owner may be used by other opens for
856 * other files but the lock_owner4 name that nfsrpc_rellockown()
857 * puts on the wire has the file handle for this file appended
858 * to it, so it can be done now.
860 (void)nfsrpc_rellockown(nmp, lp, lp->nfsl_open->nfso_fh,
861 lp->nfsl_open->nfso_fhlen, tcred, p);
865 * There could be other Opens for different files on the same
866 * OpenOwner, so locking is required.
869 nfscl_lockexcl(&op->nfso_own->nfsow_rwlock, NFSCLSTATEMUTEXPTR);
872 error = nfscl_tryclose(op, tcred, nmp, p, loop_on_delayed);
873 if (error == NFSERR_GRACE)
874 (void) nfs_catnap(PZERO, error, "nfs_close");
875 } while (error == NFSERR_GRACE);
877 nfscl_lockunlock(&op->nfso_own->nfsow_rwlock);
879 LIST_FOREACH_SAFE(lp, &op->nfso_lock, nfsl_list, nlp)
880 nfscl_freelockowner(lp, 0);
881 if (freeop && error != NFSERR_DELAY)
882 nfscl_freeopen(op, 0, true);
889 * The actual Close RPC.
892 nfsrpc_closerpc(struct nfsrv_descript *nd, struct nfsmount *nmp,
893 struct nfsclopen *op, struct ucred *cred, NFSPROC_T *p,
899 nfscl_reqstart(nd, NFSPROC_CLOSE, nmp, op->nfso_fh,
900 op->nfso_fhlen, NULL, NULL, 0, 0, cred);
901 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED + NFSX_STATEID);
902 if (NFSHASNFSV4N(nmp)) {
906 *tl++ = txdr_unsigned(op->nfso_own->nfsow_seqid);
907 *tl++ = op->nfso_stateid.seqid;
909 *tl++ = op->nfso_stateid.other[0];
910 *tl++ = op->nfso_stateid.other[1];
911 *tl = op->nfso_stateid.other[2];
913 nd->nd_flag |= ND_USEGSSNAME;
914 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
915 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
918 if (!NFSHASNFSV4N(nmp))
919 NFSCL_INCRSEQID(op->nfso_own->nfsow_seqid, nd);
920 if (nd->nd_repstat == 0)
921 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID);
922 error = nd->nd_repstat;
923 if (!NFSHASNFSV4N(nmp) && error == NFSERR_STALESTATEID)
924 nfscl_initiate_recovery(op->nfso_own->nfsow_clp);
926 m_freem(nd->nd_mrep);
931 * V4 Open Confirm RPC.
934 nfsrpc_openconfirm(vnode_t vp, u_int8_t *nfhp, int fhlen,
935 struct nfsclopen *op, struct ucred *cred, NFSPROC_T *p)
938 struct nfsrv_descript nfsd, *nd = &nfsd;
939 struct nfsmount *nmp;
942 nmp = VFSTONFS(vp->v_mount);
943 if (NFSHASNFSV4N(nmp))
944 return (0); /* No confirmation for NFSv4.1. */
945 nfscl_reqstart(nd, NFSPROC_OPENCONFIRM, nmp, nfhp, fhlen, NULL, NULL,
947 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED + NFSX_STATEID);
948 *tl++ = op->nfso_stateid.seqid;
949 *tl++ = op->nfso_stateid.other[0];
950 *tl++ = op->nfso_stateid.other[1];
951 *tl++ = op->nfso_stateid.other[2];
952 *tl = txdr_unsigned(op->nfso_own->nfsow_seqid);
953 error = nfscl_request(nd, vp, p, cred, NULL);
956 NFSCL_INCRSEQID(op->nfso_own->nfsow_seqid, nd);
957 if (!nd->nd_repstat) {
958 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID);
959 op->nfso_stateid.seqid = *tl++;
960 op->nfso_stateid.other[0] = *tl++;
961 op->nfso_stateid.other[1] = *tl++;
962 op->nfso_stateid.other[2] = *tl;
964 error = nd->nd_repstat;
965 if (error == NFSERR_STALESTATEID)
966 nfscl_initiate_recovery(op->nfso_own->nfsow_clp);
968 m_freem(nd->nd_mrep);
973 * Do the setclientid and setclientid confirm RPCs. Called from nfs_statfs()
974 * when a mount has just occurred and when the server replies NFSERR_EXPIRED.
977 nfsrpc_setclient(struct nfsmount *nmp, struct nfsclclient *clp, int reclaim,
978 bool *retokp, struct ucred *cred, NFSPROC_T *p)
981 struct nfsrv_descript nfsd;
982 struct nfsrv_descript *nd = &nfsd;
983 u_int8_t *cp = NULL, *cp2, addr[INET6_ADDRSTRLEN + 9];
985 int error, isinet6 = 0, callblen;
987 static u_int32_t rev = 0;
988 struct nfsclds *dsp, *odsp;
990 struct nfsclsession *tsep;
991 struct rpc_reconupcall recon;
992 struct nfscl_reconarg *rcp;
994 if (nfsboottime.tv_sec == 0)
995 NFSSETBOOTTIME(nfsboottime);
996 if (NFSHASNFSV4N(nmp)) {
997 error = NFSERR_BADSESSION;
999 if (retokp != NULL) {
1001 odsp = TAILQ_FIRST(&nmp->nm_sess);
1006 * When a session already exists, first try a
1007 * CreateSession with the extant ClientID.
1009 dsp = malloc(sizeof(struct nfsclds) +
1010 odsp->nfsclds_servownlen + 1, M_NFSCLDS,
1012 dsp->nfsclds_expire = NFSD_MONOSEC + clp->nfsc_renew;
1013 dsp->nfsclds_servownlen = odsp->nfsclds_servownlen;
1014 dsp->nfsclds_sess.nfsess_clientid =
1015 odsp->nfsclds_sess.nfsess_clientid;
1016 dsp->nfsclds_sess.nfsess_sequenceid =
1017 odsp->nfsclds_sess.nfsess_sequenceid + 1;
1018 dsp->nfsclds_flags = odsp->nfsclds_flags;
1019 if (dsp->nfsclds_servownlen > 0)
1020 memcpy(dsp->nfsclds_serverown,
1021 odsp->nfsclds_serverown,
1022 dsp->nfsclds_servownlen + 1);
1023 mtx_init(&dsp->nfsclds_mtx, "nfsds", NULL, MTX_DEF);
1024 mtx_init(&dsp->nfsclds_sess.nfsess_mtx, "nfssession",
1026 nfscl_initsessionslots(&dsp->nfsclds_sess);
1027 error = nfsrpc_createsession(nmp, &dsp->nfsclds_sess,
1028 &nmp->nm_sockreq, NULL,
1029 dsp->nfsclds_sess.nfsess_sequenceid, 1, cred, p);
1030 NFSCL_DEBUG(1, "create session for extant "
1031 "ClientID=%d\n", error);
1033 nfscl_freenfsclds(dsp);
1036 * If *retokp is true, return any error other
1037 * than NFSERR_STALECLIENTID,
1038 * NFSERR_BADSESSION or NFSERR_STALEDONTRECOVER
1039 * so that nfscl_recover() will not loop.
1045 } else if (retokp != NULL && *retokp)
1049 * Either there was no previous session or the
1050 * CreateSession attempt failed, so...
1051 * do an ExchangeID followed by the CreateSession.
1053 clp->nfsc_rev = rev++;
1054 error = nfsrpc_exchangeid(nmp, clp, &nmp->nm_sockreq, 0,
1055 NFSV4EXCH_USEPNFSMDS | NFSV4EXCH_USENONPNFS, &dsp,
1057 NFSCL_DEBUG(1, "aft exch=%d\n", error);
1059 error = nfsrpc_createsession(nmp,
1060 &dsp->nfsclds_sess, &nmp->nm_sockreq, NULL,
1061 dsp->nfsclds_sess.nfsess_sequenceid, 1,
1063 NFSCL_DEBUG(1, "aft createsess=%d\n", error);
1067 * If the session supports a backchannel, set up
1068 * the BindConnectionToSession call in the krpc
1069 * so that it is done on a reconnection.
1071 if (nfscl_enablecallb != 0 && nfs_numnfscbd > 0) {
1072 rcp = mem_alloc(sizeof(*rcp));
1073 rcp->minorvers = nmp->nm_minorvers;
1074 memcpy(rcp->sessionid,
1075 dsp->nfsclds_sess.nfsess_sessionid,
1077 recon.call = nfsrpc_bindconnsess;
1079 CLNT_CONTROL(nmp->nm_client, CLSET_RECONUPCALL,
1085 * The old sessions cannot be safely free'd
1086 * here, since they may still be used by
1090 if (TAILQ_FIRST(&nmp->nm_sess) != NULL) {
1092 * Mark the old session defunct. Needed
1093 * when called from nfscl_hasexpired().
1095 tsep = NFSMNT_MDSSESSION(nmp);
1096 tsep->nfsess_defunct = 1;
1098 TAILQ_INSERT_HEAD(&nmp->nm_sess, dsp,
1101 * Wake up RPCs waiting for a slot on the
1102 * old session. These will then fail with
1103 * NFSERR_BADSESSION and be retried with the
1104 * new session by nfsv4_setsequence().
1105 * Also wakeup() processes waiting for the
1109 wakeup(&tsep->nfsess_slots);
1110 wakeup(&nmp->nm_sess);
1112 } else if (dsp != NULL)
1113 nfscl_freenfsclds(dsp);
1114 if (error == 0 && reclaim == 0) {
1115 error = nfsrpc_reclaimcomplete(nmp, cred, p);
1116 NFSCL_DEBUG(1, "aft reclaimcomp=%d\n", error);
1117 if (error == NFSERR_COMPLETEALREADY ||
1118 error == NFSERR_NOTSUPP)
1119 /* Ignore this error. */
1123 } else if (retokp != NULL && *retokp)
1125 clp->nfsc_rev = rev++;
1128 * Allocate a single session structure for NFSv4.0, because some of
1129 * the fields are used by NFSv4.0 although it doesn't do a session.
1131 dsp = malloc(sizeof(struct nfsclds), M_NFSCLDS, M_WAITOK | M_ZERO);
1132 mtx_init(&dsp->nfsclds_mtx, "nfsds", NULL, MTX_DEF);
1133 mtx_init(&dsp->nfsclds_sess.nfsess_mtx, "nfssession", NULL, MTX_DEF);
1135 TAILQ_INSERT_HEAD(&nmp->nm_sess, dsp, nfsclds_list);
1136 tsep = NFSMNT_MDSSESSION(nmp);
1139 nfscl_reqstart(nd, NFSPROC_SETCLIENTID, nmp, NULL, 0, NULL, NULL, 0, 0,
1141 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1142 *tl++ = txdr_unsigned(nfsboottime.tv_sec);
1143 *tl = txdr_unsigned(clp->nfsc_rev);
1144 (void) nfsm_strtom(nd, clp->nfsc_id, clp->nfsc_idlen);
1147 * set up the callback address
1149 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1150 *tl = txdr_unsigned(NFS_CALLBCKPROG);
1151 callblen = strlen(nfsv4_callbackaddr);
1153 cp = nfscl_getmyip(nmp, &a6, &isinet6);
1154 if (nfscl_enablecallb && nfs_numnfscbd > 0 &&
1155 (callblen > 0 || cp != NULL)) {
1156 port = htons(nfsv4_cbport);
1157 cp2 = (u_int8_t *)&port;
1159 if ((callblen > 0 &&
1160 strchr(nfsv4_callbackaddr, ':')) || isinet6) {
1161 char ip6buf[INET6_ADDRSTRLEN], *ip6add;
1163 (void) nfsm_strtom(nd, "tcp6", 4);
1164 if (callblen == 0) {
1165 ip6_sprintf(ip6buf, (struct in6_addr *)cp);
1168 ip6add = nfsv4_callbackaddr;
1170 snprintf(addr, INET6_ADDRSTRLEN + 9, "%s.%d.%d",
1171 ip6add, cp2[0], cp2[1]);
1175 (void) nfsm_strtom(nd, "tcp", 3);
1177 snprintf(addr, INET6_ADDRSTRLEN + 9,
1178 "%d.%d.%d.%d.%d.%d", cp[0], cp[1],
1179 cp[2], cp[3], cp2[0], cp2[1]);
1181 snprintf(addr, INET6_ADDRSTRLEN + 9,
1182 "%s.%d.%d", nfsv4_callbackaddr,
1185 (void) nfsm_strtom(nd, addr, strlen(addr));
1187 (void) nfsm_strtom(nd, "tcp", 3);
1188 (void) nfsm_strtom(nd, "0.0.0.0.0.0", 11);
1190 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1191 *tl = txdr_unsigned(clp->nfsc_cbident);
1192 nd->nd_flag |= ND_USEGSSNAME;
1193 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
1194 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
1197 if (nd->nd_repstat == 0) {
1198 NFSM_DISSECT(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1199 tsep->nfsess_clientid.lval[0] = *tl++;
1200 tsep->nfsess_clientid.lval[1] = *tl++;
1201 confirm.lval[0] = *tl++;
1202 confirm.lval[1] = *tl;
1203 m_freem(nd->nd_mrep);
1209 nfscl_reqstart(nd, NFSPROC_SETCLIENTIDCFRM, nmp, NULL, 0, NULL,
1211 NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1212 *tl++ = tsep->nfsess_clientid.lval[0];
1213 *tl++ = tsep->nfsess_clientid.lval[1];
1214 *tl++ = confirm.lval[0];
1215 *tl = confirm.lval[1];
1216 nd->nd_flag |= ND_USEGSSNAME;
1217 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p,
1218 cred, NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
1221 m_freem(nd->nd_mrep);
1224 error = nd->nd_repstat;
1226 m_freem(nd->nd_mrep);
1234 nfsrpc_getattr(vnode_t vp, struct ucred *cred, NFSPROC_T *p,
1235 struct nfsvattr *nap, void *stuff)
1237 struct nfsrv_descript nfsd, *nd = &nfsd;
1239 nfsattrbit_t attrbits;
1241 struct nfsmount *nmp;
1243 nmp = VFSTONFS(vp->v_mount);
1245 if ((nmp->nm_privflag & NFSMNTP_FAKEROOTFH) != 0 &&
1246 nmp->nm_fhsize == 0) {
1247 /* Attempt to get the actual root file handle. */
1248 error = nfsrpc_getdirpath(nmp, NFSMNT_DIRPATH(nmp), cred, p);
1251 if (np->n_fhp->nfh_len == NFSX_FHMAX + 1)
1252 nfscl_statfs(vp, cred, p);
1254 NFSCL_REQSTART(nd, NFSPROC_GETATTR, vp, cred);
1255 if (nd->nd_flag & ND_NFSV4) {
1256 NFSGETATTR_ATTRBIT(&attrbits);
1257 (void) nfsrv_putattrbit(nd, &attrbits);
1259 error = nfscl_request(nd, vp, p, cred, stuff);
1262 if (!nd->nd_repstat)
1263 error = nfsm_loadattr(nd, nap);
1265 error = nd->nd_repstat;
1266 m_freem(nd->nd_mrep);
1271 * nfs getattr call with non-vnode arguments.
1274 nfsrpc_getattrnovp(struct nfsmount *nmp, u_int8_t *fhp, int fhlen, int syscred,
1275 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, u_int64_t *xidp,
1278 struct nfsrv_descript nfsd, *nd = &nfsd;
1279 int error, vers = NFS_VER2;
1280 nfsattrbit_t attrbits;
1282 nfscl_reqstart(nd, NFSPROC_GETATTR, nmp, fhp, fhlen, NULL, NULL, 0, 0,
1284 if (nd->nd_flag & ND_NFSV4) {
1286 NFSGETATTR_ATTRBIT(&attrbits);
1287 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_LEASETIME);
1288 (void) nfsrv_putattrbit(nd, &attrbits);
1289 } else if (nd->nd_flag & ND_NFSV3) {
1293 nd->nd_flag |= ND_USEGSSNAME;
1294 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
1295 NFS_PROG, vers, NULL, 1, xidp, NULL);
1298 if (nd->nd_repstat == 0) {
1299 if ((nd->nd_flag & ND_NFSV4) != 0)
1300 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0,
1301 NULL, NULL, NULL, NULL, NULL, 0, NULL, leasep, NULL,
1304 error = nfsm_loadattr(nd, nap);
1306 error = nd->nd_repstat;
1307 m_freem(nd->nd_mrep);
1312 * Do an nfs setattr operation.
1315 nfsrpc_setattr(vnode_t vp, struct vattr *vap, NFSACL_T *aclp,
1316 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *rnap, int *attrflagp,
1319 int error, expireret = 0, openerr, retrycnt;
1320 u_int32_t clidrev = 0, mode;
1321 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1323 nfsv4stateid_t stateid;
1326 if (nmp->nm_clp != NULL)
1327 clidrev = nmp->nm_clp->nfsc_clientidrev;
1328 if (vap != NULL && NFSATTRISSET(u_quad_t, vap, va_size))
1329 mode = NFSV4OPEN_ACCESSWRITE;
1331 mode = NFSV4OPEN_ACCESSREAD;
1336 if (NFSHASNFSV4(nmp)) {
1337 nfhp = VTONFS(vp)->n_fhp;
1338 error = nfscl_getstateid(vp, nfhp->nfh_fh,
1339 nfhp->nfh_len, mode, 0, cred, p, &stateid, &lckp);
1340 if (error && vnode_vtype(vp) == VREG &&
1341 (mode == NFSV4OPEN_ACCESSWRITE ||
1342 nfstest_openallsetattr)) {
1344 * No Open stateid, so try and open the file
1347 if (mode == NFSV4OPEN_ACCESSWRITE)
1348 openerr = nfsrpc_open(vp, FWRITE, cred,
1351 openerr = nfsrpc_open(vp, FREAD, cred,
1354 (void) nfscl_getstateid(vp,
1355 nfhp->nfh_fh, nfhp->nfh_len,
1356 mode, 0, cred, p, &stateid, &lckp);
1360 error = nfsrpc_setattrrpc(vp, vap, &stateid, cred, p,
1361 rnap, attrflagp, stuff);
1363 error = nfsrpc_setaclrpc(vp, cred, p, aclp, &stateid,
1365 if (error == NFSERR_OPENMODE && mode == NFSV4OPEN_ACCESSREAD) {
1367 nmp->nm_state |= NFSSTA_OPENMODE;
1370 if (error == NFSERR_STALESTATEID)
1371 nfscl_initiate_recovery(nmp->nm_clp);
1373 nfscl_lockderef(lckp);
1375 (void) nfsrpc_close(vp, 0, p);
1376 if (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
1377 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
1378 error == NFSERR_OLDSTATEID || error == NFSERR_BADSESSION) {
1379 (void) nfs_catnap(PZERO, error, "nfs_setattr");
1380 } else if ((error == NFSERR_EXPIRED ||
1381 ((!NFSHASINT(nmp) || !NFSHASNFSV4N(nmp)) &&
1382 error == NFSERR_BADSTATEID)) && clidrev != 0) {
1383 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
1384 } else if (error == NFSERR_BADSTATEID && NFSHASINT(nmp) &&
1385 NFSHASNFSV4N(nmp)) {
1389 } while (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
1390 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
1391 error == NFSERR_BADSESSION ||
1392 (error == NFSERR_OLDSTATEID && retrycnt < 20) ||
1393 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
1394 expireret == 0 && clidrev != 0 && retrycnt < 4) ||
1395 (error == NFSERR_OPENMODE && mode == NFSV4OPEN_ACCESSREAD &&
1397 if (error && retrycnt >= 4)
1403 nfsrpc_setattrrpc(vnode_t vp, struct vattr *vap,
1404 nfsv4stateid_t *stateidp, struct ucred *cred, NFSPROC_T *p,
1405 struct nfsvattr *rnap, int *attrflagp, void *stuff)
1408 struct nfsrv_descript nfsd, *nd = &nfsd;
1410 nfsattrbit_t attrbits;
1413 NFSCL_REQSTART(nd, NFSPROC_SETATTR, vp, cred);
1414 if (nd->nd_flag & ND_NFSV4)
1415 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID);
1416 vap->va_type = vnode_vtype(vp);
1417 nfscl_fillsattr(nd, vap, vp, NFSSATTR_FULL, 0);
1418 if (nd->nd_flag & ND_NFSV3) {
1419 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1421 } else if (nd->nd_flag & ND_NFSV4) {
1422 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1423 *tl = txdr_unsigned(NFSV4OP_GETATTR);
1424 NFSGETATTR_ATTRBIT(&attrbits);
1425 (void) nfsrv_putattrbit(nd, &attrbits);
1427 error = nfscl_request(nd, vp, p, cred, stuff);
1430 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4))
1431 error = nfscl_wcc_data(nd, vp, rnap, attrflagp, NULL, NULL);
1432 if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) == ND_NFSV4 && !error)
1433 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
1434 if (!(nd->nd_flag & ND_NFSV3) && !nd->nd_repstat && !error)
1435 error = nfscl_postop_attr(nd, rnap, attrflagp, stuff);
1436 m_freem(nd->nd_mrep);
1437 if (nd->nd_repstat && !error)
1438 error = nd->nd_repstat;
1446 nfsrpc_lookup(vnode_t dvp, char *name, int len, struct ucred *cred,
1447 NFSPROC_T *p, struct nfsvattr *dnap, struct nfsvattr *nap,
1448 struct nfsfh **nfhpp, int *attrflagp, int *dattrflagp, void *stuff)
1451 struct nfsrv_descript nfsd, *nd = &nfsd;
1452 struct nfsmount *nmp;
1455 nfsattrbit_t attrbits;
1456 int error = 0, lookupp = 0;
1460 if (vnode_vtype(dvp) != VDIR)
1462 nmp = VFSTONFS(dvp->v_mount);
1463 if (len > NFS_MAXNAMLEN)
1464 return (ENAMETOOLONG);
1465 if (NFSHASNFSV4(nmp) && len == 1 &&
1468 * Just return the current dir's fh.
1471 nfhp = malloc(sizeof (struct nfsfh) +
1472 np->n_fhp->nfh_len, M_NFSFH, M_WAITOK);
1473 nfhp->nfh_len = np->n_fhp->nfh_len;
1474 NFSBCOPY(np->n_fhp->nfh_fh, nfhp->nfh_fh, nfhp->nfh_len);
1478 if (NFSHASNFSV4(nmp) && len == 2 &&
1479 name[0] == '.' && name[1] == '.') {
1481 NFSCL_REQSTART(nd, NFSPROC_LOOKUPP, dvp, cred);
1483 NFSCL_REQSTART(nd, NFSPROC_LOOKUP, dvp, cred);
1484 (void) nfsm_strtom(nd, name, len);
1486 if (nd->nd_flag & ND_NFSV4) {
1487 NFSGETATTR_ATTRBIT(&attrbits);
1488 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1489 *tl++ = txdr_unsigned(NFSV4OP_GETFH);
1490 *tl = txdr_unsigned(NFSV4OP_GETATTR);
1491 (void) nfsrv_putattrbit(nd, &attrbits);
1493 error = nfscl_request(nd, dvp, p, cred, stuff);
1496 if (nd->nd_repstat) {
1498 * When an NFSv4 Lookupp returns ENOENT, it means that
1499 * the lookup is at the root of an fs, so return this dir.
1501 if (nd->nd_repstat == NFSERR_NOENT && lookupp) {
1503 nfhp = malloc(sizeof (struct nfsfh) +
1504 np->n_fhp->nfh_len, M_NFSFH, M_WAITOK);
1505 nfhp->nfh_len = np->n_fhp->nfh_len;
1506 NFSBCOPY(np->n_fhp->nfh_fh, nfhp->nfh_fh, nfhp->nfh_len);
1508 m_freem(nd->nd_mrep);
1511 if (nd->nd_flag & ND_NFSV3)
1512 error = nfscl_postop_attr(nd, dnap, dattrflagp, stuff);
1513 else if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) ==
1515 /* Load the directory attributes. */
1516 error = nfsm_loadattr(nd, dnap);
1522 if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) == ND_NFSV4) {
1523 /* Load the directory attributes. */
1524 error = nfsm_loadattr(nd, dnap);
1528 /* Skip over the Lookup and GetFH operation status values. */
1529 NFSM_DISSECT(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1531 error = nfsm_getfh(nd, nfhpp);
1535 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
1536 if ((nd->nd_flag & ND_NFSV3) && !error)
1537 error = nfscl_postop_attr(nd, dnap, dattrflagp, stuff);
1539 m_freem(nd->nd_mrep);
1540 if (!error && nd->nd_repstat)
1541 error = nd->nd_repstat;
1546 * Do a readlink rpc.
1549 nfsrpc_readlink(vnode_t vp, struct uio *uiop, struct ucred *cred,
1550 NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, void *stuff)
1553 struct nfsrv_descript nfsd, *nd = &nfsd;
1554 struct nfsnode *np = VTONFS(vp);
1555 nfsattrbit_t attrbits;
1556 int error, len, cangetattr = 1;
1559 NFSCL_REQSTART(nd, NFSPROC_READLINK, vp, cred);
1560 if (nd->nd_flag & ND_NFSV4) {
1562 * And do a Getattr op.
1564 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1565 *tl = txdr_unsigned(NFSV4OP_GETATTR);
1566 NFSGETATTR_ATTRBIT(&attrbits);
1567 (void) nfsrv_putattrbit(nd, &attrbits);
1569 error = nfscl_request(nd, vp, p, cred, stuff);
1572 if (nd->nd_flag & ND_NFSV3)
1573 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
1574 if (!nd->nd_repstat && !error) {
1575 NFSM_STRSIZ(len, NFS_MAXPATHLEN);
1577 * This seems weird to me, but must have been added to
1578 * FreeBSD for some reason. The only thing I can think of
1579 * is that there was/is some server that replies with
1580 * more link data than it should?
1582 if (len == NFS_MAXPATHLEN) {
1584 if (np->n_size > 0 && np->n_size < NFS_MAXPATHLEN) {
1590 error = nfsm_mbufuio(nd, uiop, len);
1591 if ((nd->nd_flag & ND_NFSV4) && !error && cangetattr)
1592 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
1594 if (nd->nd_repstat && !error)
1595 error = nd->nd_repstat;
1597 m_freem(nd->nd_mrep);
1605 nfsrpc_read(vnode_t vp, struct uio *uiop, struct ucred *cred,
1606 NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, void *stuff)
1608 int error, expireret = 0, retrycnt;
1609 u_int32_t clidrev = 0;
1610 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1611 struct nfsnode *np = VTONFS(vp);
1612 struct ucred *newcred;
1613 struct nfsfh *nfhp = NULL;
1614 nfsv4stateid_t stateid;
1617 if (nmp->nm_clp != NULL)
1618 clidrev = nmp->nm_clp->nfsc_clientidrev;
1620 if (NFSHASNFSV4(nmp)) {
1622 newcred = NFSNEWCRED(cred);
1627 if (NFSHASNFSV4(nmp))
1628 (void)nfscl_getstateid(vp, nfhp->nfh_fh, nfhp->nfh_len,
1629 NFSV4OPEN_ACCESSREAD, 0, newcred, p, &stateid,
1631 error = nfsrpc_readrpc(vp, uiop, newcred, &stateid, p, nap,
1633 if (error == NFSERR_OPENMODE) {
1635 nmp->nm_state |= NFSSTA_OPENMODE;
1638 if (error == NFSERR_STALESTATEID)
1639 nfscl_initiate_recovery(nmp->nm_clp);
1641 nfscl_lockderef(lckp);
1642 if (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
1643 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
1644 error == NFSERR_OLDSTATEID || error == NFSERR_BADSESSION) {
1645 (void) nfs_catnap(PZERO, error, "nfs_read");
1646 } else if ((error == NFSERR_EXPIRED ||
1647 ((!NFSHASINT(nmp) || !NFSHASNFSV4N(nmp)) &&
1648 error == NFSERR_BADSTATEID)) && clidrev != 0) {
1649 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
1650 } else if (error == NFSERR_BADSTATEID && NFSHASINT(nmp) &&
1651 NFSHASNFSV4N(nmp)) {
1655 } while (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
1656 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
1657 error == NFSERR_BADSESSION ||
1658 (error == NFSERR_OLDSTATEID && retrycnt < 20) ||
1659 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
1660 expireret == 0 && clidrev != 0 && retrycnt < 4) ||
1661 (error == NFSERR_OPENMODE && retrycnt < 4));
1662 if (error && retrycnt >= 4)
1664 if (NFSHASNFSV4(nmp))
1665 NFSFREECRED(newcred);
1670 * The actual read RPC.
1673 nfsrpc_readrpc(vnode_t vp, struct uio *uiop, struct ucred *cred,
1674 nfsv4stateid_t *stateidp, NFSPROC_T *p, struct nfsvattr *nap,
1675 int *attrflagp, void *stuff)
1678 int error = 0, len, retlen, tsiz, eof = 0;
1679 struct nfsrv_descript nfsd;
1680 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1681 struct nfsrv_descript *nd = &nfsd;
1686 tsiz = uiop->uio_resid;
1687 tmp_off = uiop->uio_offset + tsiz;
1689 if (tmp_off > nmp->nm_maxfilesize || tmp_off < uiop->uio_offset) {
1693 rsize = nmp->nm_rsize;
1698 len = (tsiz > rsize) ? rsize : tsiz;
1699 NFSCL_REQSTART(nd, NFSPROC_READ, vp, cred);
1700 if (nd->nd_flag & ND_NFSV4)
1701 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID);
1702 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED * 3);
1703 if (nd->nd_flag & ND_NFSV2) {
1704 *tl++ = txdr_unsigned(uiop->uio_offset);
1705 *tl++ = txdr_unsigned(len);
1708 txdr_hyper(uiop->uio_offset, tl);
1709 *(tl + 2) = txdr_unsigned(len);
1712 * Since I can't do a Getattr for NFSv4 for Write, there
1713 * doesn't seem any point in doing one here, either.
1714 * (See the comment in nfsrpc_writerpc() for more info.)
1716 error = nfscl_request(nd, vp, p, cred, stuff);
1719 if (nd->nd_flag & ND_NFSV3) {
1720 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
1721 } else if (!nd->nd_repstat && (nd->nd_flag & ND_NFSV2)) {
1722 error = nfsm_loadattr(nd, nap);
1726 if (nd->nd_repstat || error) {
1728 error = nd->nd_repstat;
1731 if (nd->nd_flag & ND_NFSV3) {
1732 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1733 eof = fxdr_unsigned(int, *(tl + 1));
1734 } else if (nd->nd_flag & ND_NFSV4) {
1735 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
1736 eof = fxdr_unsigned(int, *tl);
1738 NFSM_STRSIZ(retlen, len);
1739 error = nfsm_mbufuio(nd, uiop, retlen);
1742 m_freem(nd->nd_mrep);
1745 if (!(nd->nd_flag & ND_NFSV2)) {
1746 if (eof || retlen == 0)
1748 } else if (retlen < len)
1753 if (nd->nd_mrep != NULL)
1754 m_freem(nd->nd_mrep);
1759 * nfs write operation
1760 * When called_from_strategy != 0, it should return EIO for an error that
1761 * indicates recovery is in progress, so that the buffer will be left
1762 * dirty and be written back to the server later. If it loops around,
1763 * the recovery thread could get stuck waiting for the buffer and recovery
1764 * will then deadlock.
1767 nfsrpc_write(vnode_t vp, struct uio *uiop, int *iomode, int *must_commit,
1768 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp,
1769 int called_from_strategy, int ioflag)
1771 int error, expireret = 0, retrycnt, nostateid;
1772 u_int32_t clidrev = 0;
1773 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1774 struct nfsnode *np = VTONFS(vp);
1775 struct ucred *newcred;
1776 struct nfsfh *nfhp = NULL;
1777 nfsv4stateid_t stateid;
1780 KASSERT(*must_commit >= 0 && *must_commit <= 2,
1781 ("nfsrpc_write: must_commit out of range=%d", *must_commit));
1782 if (nmp->nm_clp != NULL)
1783 clidrev = nmp->nm_clp->nfsc_clientidrev;
1785 if (NFSHASNFSV4(nmp)) {
1786 newcred = NFSNEWCRED(cred);
1793 if (NFSHASNFSV4(nmp)) {
1794 (void)nfscl_getstateid(vp, nfhp->nfh_fh, nfhp->nfh_len,
1795 NFSV4OPEN_ACCESSWRITE, 0, newcred, p, &stateid,
1797 if (stateid.other[0] == 0 && stateid.other[1] == 0 &&
1798 stateid.other[2] == 0) {
1800 NFSCL_DEBUG(1, "stateid0 in write\n");
1805 * If there is no stateid for NFSv4, it means this is an
1806 * extraneous write after close. Basically a poorly
1807 * implemented buffer cache. Just don't do the write.
1812 error = nfsrpc_writerpc(vp, uiop, iomode, must_commit,
1813 newcred, &stateid, p, nap, attrflagp, ioflag);
1814 if (error == NFSERR_STALESTATEID)
1815 nfscl_initiate_recovery(nmp->nm_clp);
1817 nfscl_lockderef(lckp);
1818 if (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
1819 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
1820 error == NFSERR_OLDSTATEID || error == NFSERR_BADSESSION) {
1821 (void) nfs_catnap(PZERO, error, "nfs_write");
1822 } else if ((error == NFSERR_EXPIRED ||
1823 ((!NFSHASINT(nmp) || !NFSHASNFSV4N(nmp)) &&
1824 error == NFSERR_BADSTATEID)) && clidrev != 0) {
1825 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
1826 } else if (error == NFSERR_BADSTATEID && NFSHASINT(nmp) &&
1827 NFSHASNFSV4N(nmp)) {
1831 } while (error == NFSERR_GRACE || error == NFSERR_DELAY ||
1832 ((error == NFSERR_STALESTATEID || error == NFSERR_BADSESSION ||
1833 error == NFSERR_STALEDONTRECOVER) && called_from_strategy == 0) ||
1834 (error == NFSERR_OLDSTATEID && retrycnt < 20) ||
1835 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
1836 expireret == 0 && clidrev != 0 && retrycnt < 4));
1837 if (error != 0 && (retrycnt >= 4 ||
1838 ((error == NFSERR_STALESTATEID || error == NFSERR_BADSESSION ||
1839 error == NFSERR_STALEDONTRECOVER) && called_from_strategy != 0)))
1841 if (NFSHASNFSV4(nmp))
1842 NFSFREECRED(newcred);
1847 * The actual write RPC.
1850 nfsrpc_writerpc(vnode_t vp, struct uio *uiop, int *iomode,
1851 int *must_commit, struct ucred *cred, nfsv4stateid_t *stateidp,
1852 NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, int ioflag)
1855 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1856 struct nfsnode *np = VTONFS(vp);
1857 int error = 0, len, rlen, commit, committed = NFSWRITE_FILESYNC;
1860 struct nfsrv_descript *nd;
1861 nfsattrbit_t attrbits;
1863 ssize_t tsiz, wsize;
1866 KASSERT(uiop->uio_iovcnt == 1, ("nfs: writerpc iovcnt > 1"));
1868 tsiz = uiop->uio_resid;
1869 tmp_off = uiop->uio_offset + tsiz;
1871 if (tmp_off > nmp->nm_maxfilesize || tmp_off < uiop->uio_offset) {
1875 wsize = nmp->nm_wsize;
1877 if ((ioflag & IO_APPEND) != 0 && NFSHASNFSV4(nmp) && !NFSHASPNFS(nmp))
1880 nd = malloc(sizeof(*nd), M_TEMP, M_WAITOK);
1881 nd->nd_mrep = NULL; /* NFSv2 sometimes does a write with */
1882 nd->nd_repstat = 0; /* uio_resid == 0, so the while is not done */
1885 len = (tsiz > wsize) ? wsize : tsiz;
1887 NFSCL_REQSTART(nd, NFSPROC_APPENDWRITE, vp, cred);
1889 NFSCL_REQSTART(nd, NFSPROC_WRITE, vp, cred);
1890 if (nd->nd_flag & ND_NFSV4) {
1892 NFSZERO_ATTRBIT(&attrbits);
1893 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_SIZE);
1894 nfsrv_putattrbit(nd, &attrbits);
1895 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_UNSIGNED +
1897 *tl++ = txdr_unsigned(NFSX_HYPER);
1898 txdr_hyper(uiop->uio_offset, tl); tl += 2;
1899 *tl = txdr_unsigned(NFSV4OP_WRITE);
1901 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID);
1902 NFSM_BUILD(tl, u_int32_t *, NFSX_HYPER+2*NFSX_UNSIGNED);
1903 txdr_hyper(uiop->uio_offset, tl);
1905 *tl++ = txdr_unsigned(*iomode);
1906 *tl = txdr_unsigned(len);
1907 } else if (nd->nd_flag & ND_NFSV3) {
1908 NFSM_BUILD(tl, u_int32_t *, NFSX_HYPER+3*NFSX_UNSIGNED);
1909 txdr_hyper(uiop->uio_offset, tl);
1911 *tl++ = txdr_unsigned(len);
1912 *tl++ = txdr_unsigned(*iomode);
1913 *tl = txdr_unsigned(len);
1917 NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1919 * Not sure why someone changed this, since the
1920 * RFC clearly states that "beginoffset" and
1921 * "totalcount" are ignored, but it wouldn't
1922 * surprise me if there's a busted server out there.
1924 /* Set both "begin" and "current" to non-garbage. */
1925 x = txdr_unsigned((u_int32_t)uiop->uio_offset);
1926 *tl++ = x; /* "begin offset" */
1927 *tl++ = x; /* "current offset" */
1928 x = txdr_unsigned(len);
1929 *tl++ = x; /* total to this offset */
1930 *tl = x; /* size of this write */
1932 error = nfsm_uiombuf(nd, uiop, len);
1934 m_freem(nd->nd_mreq);
1939 * Although it is tempting to do a normal Getattr Op in the
1940 * NFSv4 compound, the result can be a nearly hung client
1941 * system if the Getattr asks for Owner and/or OwnerGroup.
1942 * It occurs when the client can't map either the Owner or
1943 * Owner_group name in the Getattr reply to a uid/gid. When
1944 * there is a cache miss, the kernel does an upcall to the
1945 * nfsuserd. Then, it can try and read the local /etc/passwd
1946 * or /etc/group file. It can then block in getnewbuf(),
1947 * waiting for dirty writes to be pushed to the NFS server.
1948 * The only reason this doesn't result in a complete
1949 * deadlock, is that the upcall times out and allows
1950 * the write to complete. However, progress is so slow
1951 * that it might just as well be deadlocked.
1952 * As such, we get the rest of the attributes, but not
1953 * Owner or Owner_group.
1954 * nb: nfscl_loadattrcache() needs to be told that these
1955 * partial attributes from a write rpc are being
1956 * passed in, via a argument flag.
1958 if (nd->nd_flag & ND_NFSV4) {
1959 NFSWRITEGETATTR_ATTRBIT(&attrbits);
1960 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1961 *tl = txdr_unsigned(NFSV4OP_GETATTR);
1962 (void) nfsrv_putattrbit(nd, &attrbits);
1964 error = nfscl_request(nd, vp, p, cred, NULL);
1969 if (nd->nd_repstat) {
1971 * In case the rpc gets retried, roll
1972 * the uio fileds changed by nfsm_uiombuf()
1975 uiop->uio_offset -= len;
1976 uiop->uio_resid += len;
1977 uiop->uio_iov->iov_base =
1978 (char *)uiop->uio_iov->iov_base - len;
1979 uiop->uio_iov->iov_len += len;
1981 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) {
1982 error = nfscl_wcc_data(nd, vp, nap, attrflagp,
1983 &wccflag, &tmp_off);
1987 if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) ==
1988 (ND_NFSV4 | ND_NOMOREDATA) &&
1989 nd->nd_repstat == NFSERR_NOTSAME && do_append) {
1991 * Verify of the file's size failed, so redo the
1992 * write using the file's size as returned in
1993 * the wcc attributes.
1995 if (tmp_off + tsiz <= nmp->nm_maxfilesize) {
1997 uiop->uio_offset = tmp_off;
1998 m_freem(nd->nd_mrep);
2002 nd->nd_repstat = EFBIG;
2004 if (!nd->nd_repstat) {
2006 /* Strip off the Write reply status. */
2008 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
2010 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) {
2011 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED
2013 rlen = fxdr_unsigned(int, *tl++);
2017 } else if (rlen < len) {
2018 backup = len - rlen;
2019 uiop->uio_iov->iov_base =
2020 (char *)uiop->uio_iov->iov_base -
2022 uiop->uio_iov->iov_len += backup;
2023 uiop->uio_offset -= backup;
2024 uiop->uio_resid += backup;
2027 commit = fxdr_unsigned(int, *tl++);
2030 * Return the lowest commitment level
2031 * obtained by any of the RPCs.
2033 if (committed == NFSWRITE_FILESYNC)
2035 else if (committed == NFSWRITE_DATASYNC &&
2036 commit == NFSWRITE_UNSTABLE)
2039 if (!NFSHASWRITEVERF(nmp)) {
2040 NFSBCOPY((caddr_t)tl,
2041 (caddr_t)&nmp->nm_verf[0],
2043 NFSSETWRITEVERF(nmp);
2044 } else if (NFSBCMP(tl, nmp->nm_verf,
2045 NFSX_VERF) && *must_commit != 2) {
2047 NFSBCOPY(tl, nmp->nm_verf, NFSX_VERF);
2051 if (nd->nd_flag & ND_NFSV4)
2052 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2053 if (nd->nd_flag & (ND_NFSV2 | ND_NFSV4)) {
2054 error = nfsm_loadattr(nd, nap);
2056 *attrflagp = NFS_LATTR_NOSHRINK;
2059 error = nd->nd_repstat;
2063 NFSWRITERPC_SETTIME(wccflag, np, nap, (nd->nd_flag & ND_NFSV4));
2064 m_freem(nd->nd_mrep);
2069 if (nd->nd_mrep != NULL)
2070 m_freem(nd->nd_mrep);
2071 *iomode = committed;
2072 if (nd->nd_repstat && !error)
2073 error = nd->nd_repstat;
2080 * For NFS v2 this is a kludge. Use a create rpc but with the IFMT bits of the
2081 * mode set to specify the file type and the size field for rdev.
2084 nfsrpc_mknod(vnode_t dvp, char *name, int namelen, struct vattr *vap,
2085 u_int32_t rdev, enum vtype vtyp, struct ucred *cred, NFSPROC_T *p,
2086 struct nfsvattr *dnap, struct nfsvattr *nnap, struct nfsfh **nfhpp,
2087 int *attrflagp, int *dattrflagp, void *dstuff)
2091 struct nfsrv_descript nfsd, *nd = &nfsd;
2092 nfsattrbit_t attrbits;
2097 if (namelen > NFS_MAXNAMLEN)
2098 return (ENAMETOOLONG);
2099 NFSCL_REQSTART(nd, NFSPROC_MKNOD, dvp, cred);
2100 if (nd->nd_flag & ND_NFSV4) {
2101 if (vtyp == VBLK || vtyp == VCHR) {
2102 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2103 *tl++ = vtonfsv34_type(vtyp);
2104 *tl++ = txdr_unsigned(NFSMAJOR(rdev));
2105 *tl = txdr_unsigned(NFSMINOR(rdev));
2107 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2108 *tl = vtonfsv34_type(vtyp);
2111 (void) nfsm_strtom(nd, name, namelen);
2112 if (nd->nd_flag & ND_NFSV3) {
2113 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2114 *tl = vtonfsv34_type(vtyp);
2116 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4))
2117 nfscl_fillsattr(nd, vap, dvp, 0, 0);
2118 if ((nd->nd_flag & ND_NFSV3) &&
2119 (vtyp == VCHR || vtyp == VBLK)) {
2120 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2121 *tl++ = txdr_unsigned(NFSMAJOR(rdev));
2122 *tl = txdr_unsigned(NFSMINOR(rdev));
2124 if (nd->nd_flag & ND_NFSV4) {
2125 NFSGETATTR_ATTRBIT(&attrbits);
2126 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2127 *tl++ = txdr_unsigned(NFSV4OP_GETFH);
2128 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2129 (void) nfsrv_putattrbit(nd, &attrbits);
2131 if (nd->nd_flag & ND_NFSV2)
2132 nfscl_fillsattr(nd, vap, dvp, NFSSATTR_SIZERDEV, rdev);
2133 error = nfscl_request(nd, dvp, p, cred, dstuff);
2136 if (nd->nd_flag & ND_NFSV4)
2137 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, NULL);
2138 if (!nd->nd_repstat) {
2139 if (nd->nd_flag & ND_NFSV4) {
2140 NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
2141 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
2145 error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp);
2149 if (nd->nd_flag & ND_NFSV3)
2150 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, NULL);
2151 if (!error && nd->nd_repstat)
2152 error = nd->nd_repstat;
2154 m_freem(nd->nd_mrep);
2159 * nfs file create call
2160 * Mostly just call the approriate routine. (I separated out v4, so that
2161 * error recovery wouldn't be as difficult.)
2164 nfsrpc_create(vnode_t dvp, char *name, int namelen, struct vattr *vap,
2165 nfsquad_t cverf, int fmode, struct ucred *cred, NFSPROC_T *p,
2166 struct nfsvattr *dnap, struct nfsvattr *nnap, struct nfsfh **nfhpp,
2167 int *attrflagp, int *dattrflagp, void *dstuff)
2169 int error = 0, newone, expireret = 0, retrycnt, unlocked;
2170 struct nfsclowner *owp;
2171 struct nfscldeleg *dp;
2172 struct nfsmount *nmp = VFSTONFS(dvp->v_mount);
2175 if (NFSHASNFSV4(nmp)) {
2179 error = nfscl_open(dvp, NULL, 0, (NFSV4OPEN_ACCESSWRITE |
2180 NFSV4OPEN_ACCESSREAD), 0, cred, p, &owp, NULL, &newone,
2184 if (nmp->nm_clp != NULL)
2185 clidrev = nmp->nm_clp->nfsc_clientidrev;
2188 if (!NFSHASPNFS(nmp) || nfscl_enablecallb == 0 ||
2189 nfs_numnfscbd == 0 || retrycnt > 0)
2190 error = nfsrpc_createv4(dvp, name, namelen, vap, cverf,
2191 fmode, owp, &dp, cred, p, dnap, nnap, nfhpp,
2192 attrflagp, dattrflagp, dstuff, &unlocked);
2194 error = nfsrpc_getcreatelayout(dvp, name, namelen, vap,
2195 cverf, fmode, owp, &dp, cred, p, dnap, nnap, nfhpp,
2196 attrflagp, dattrflagp, dstuff, &unlocked);
2198 * There is no need to invalidate cached attributes here,
2199 * since new post-delegation issue attributes are always
2200 * returned by nfsrpc_createv4() and these will update the
2204 (void) nfscl_deleg(nmp->nm_mountp, owp->nfsow_clp,
2205 (*nfhpp)->nfh_fh, (*nfhpp)->nfh_len, cred, p, &dp);
2206 nfscl_ownerrelease(nmp, owp, error, newone, unlocked);
2207 if (error == NFSERR_GRACE || error == NFSERR_STALECLIENTID ||
2208 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
2209 error == NFSERR_BADSESSION) {
2210 (void) nfs_catnap(PZERO, error, "nfs_open");
2211 } else if ((error == NFSERR_EXPIRED ||
2212 error == NFSERR_BADSTATEID) && clidrev != 0) {
2213 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
2216 } while (error == NFSERR_GRACE || error == NFSERR_STALECLIENTID ||
2217 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
2218 error == NFSERR_BADSESSION ||
2219 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
2220 expireret == 0 && clidrev != 0 && retrycnt < 4));
2221 if (error && retrycnt >= 4)
2224 error = nfsrpc_createv23(dvp, name, namelen, vap, cverf,
2225 fmode, cred, p, dnap, nnap, nfhpp, attrflagp, dattrflagp,
2232 * The create rpc for v2 and 3.
2235 nfsrpc_createv23(vnode_t dvp, char *name, int namelen, struct vattr *vap,
2236 nfsquad_t cverf, int fmode, struct ucred *cred, NFSPROC_T *p,
2237 struct nfsvattr *dnap, struct nfsvattr *nnap, struct nfsfh **nfhpp,
2238 int *attrflagp, int *dattrflagp, void *dstuff)
2242 struct nfsrv_descript nfsd, *nd = &nfsd;
2247 if (namelen > NFS_MAXNAMLEN)
2248 return (ENAMETOOLONG);
2249 NFSCL_REQSTART(nd, NFSPROC_CREATE, dvp, cred);
2250 (void) nfsm_strtom(nd, name, namelen);
2251 if (nd->nd_flag & ND_NFSV3) {
2252 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2253 if (fmode & O_EXCL) {
2254 *tl = txdr_unsigned(NFSCREATE_EXCLUSIVE);
2255 NFSM_BUILD(tl, u_int32_t *, NFSX_VERF);
2256 *tl++ = cverf.lval[0];
2257 *tl = cverf.lval[1];
2259 *tl = txdr_unsigned(NFSCREATE_UNCHECKED);
2260 nfscl_fillsattr(nd, vap, dvp, 0, 0);
2263 nfscl_fillsattr(nd, vap, dvp, NFSSATTR_SIZE0, 0);
2265 error = nfscl_request(nd, dvp, p, cred, dstuff);
2268 if (nd->nd_repstat == 0) {
2269 error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp);
2273 if (nd->nd_flag & ND_NFSV3)
2274 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, NULL);
2275 if (nd->nd_repstat != 0 && error == 0)
2276 error = nd->nd_repstat;
2278 m_freem(nd->nd_mrep);
2283 nfsrpc_createv4(vnode_t dvp, char *name, int namelen, struct vattr *vap,
2284 nfsquad_t cverf, int fmode, struct nfsclowner *owp, struct nfscldeleg **dpp,
2285 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap,
2286 struct nfsvattr *nnap, struct nfsfh **nfhpp, int *attrflagp,
2287 int *dattrflagp, void *dstuff, int *unlockedp)
2290 int error = 0, deleg, newone, ret, acesize, limitby;
2291 struct nfsrv_descript nfsd, *nd = &nfsd;
2292 struct nfsclopen *op;
2293 struct nfscldeleg *dp = NULL;
2296 nfsattrbit_t attrbits;
2297 nfsv4stateid_t stateid;
2299 struct nfsmount *nmp;
2300 struct nfsclsession *tsep;
2302 nmp = VFSTONFS(dvp->v_mount);
2309 if (namelen > NFS_MAXNAMLEN)
2310 return (ENAMETOOLONG);
2311 NFSCL_REQSTART(nd, NFSPROC_CREATE, dvp, cred);
2313 * For V4, this is actually an Open op.
2315 NFSM_BUILD(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
2316 *tl++ = txdr_unsigned(owp->nfsow_seqid);
2317 *tl++ = txdr_unsigned(NFSV4OPEN_ACCESSWRITE |
2318 NFSV4OPEN_ACCESSREAD);
2319 *tl++ = txdr_unsigned(NFSV4OPEN_DENYNONE);
2320 tsep = nfsmnt_mdssession(nmp);
2321 *tl++ = tsep->nfsess_clientid.lval[0];
2322 *tl = tsep->nfsess_clientid.lval[1];
2323 (void) nfsm_strtom(nd, owp->nfsow_owner, NFSV4CL_LOCKNAMELEN);
2324 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2325 *tl++ = txdr_unsigned(NFSV4OPEN_CREATE);
2326 if (fmode & O_EXCL) {
2327 if (NFSHASNFSV4N(nmp)) {
2328 if (NFSHASSESSPERSIST(nmp)) {
2329 /* Use GUARDED for persistent sessions. */
2330 *tl = txdr_unsigned(NFSCREATE_GUARDED);
2331 nfscl_fillsattr(nd, vap, dvp, 0, 0);
2333 /* Otherwise, use EXCLUSIVE4_1. */
2334 *tl = txdr_unsigned(NFSCREATE_EXCLUSIVE41);
2335 NFSM_BUILD(tl, u_int32_t *, NFSX_VERF);
2336 *tl++ = cverf.lval[0];
2337 *tl = cverf.lval[1];
2338 nfscl_fillsattr(nd, vap, dvp, 0, 0);
2342 *tl = txdr_unsigned(NFSCREATE_EXCLUSIVE);
2343 NFSM_BUILD(tl, u_int32_t *, NFSX_VERF);
2344 *tl++ = cverf.lval[0];
2345 *tl = cverf.lval[1];
2348 *tl = txdr_unsigned(NFSCREATE_UNCHECKED);
2349 nfscl_fillsattr(nd, vap, dvp, 0, 0);
2351 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2352 *tl = txdr_unsigned(NFSV4OPEN_CLAIMNULL);
2353 (void) nfsm_strtom(nd, name, namelen);
2354 /* Get the new file's handle and attributes. */
2355 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2356 *tl++ = txdr_unsigned(NFSV4OP_GETFH);
2357 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2358 NFSGETATTR_ATTRBIT(&attrbits);
2359 (void) nfsrv_putattrbit(nd, &attrbits);
2360 /* Get the directory's post-op attributes. */
2361 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2362 *tl = txdr_unsigned(NFSV4OP_PUTFH);
2363 (void)nfsm_fhtom(nmp, nd, np->n_fhp->nfh_fh, np->n_fhp->nfh_len, 0);
2364 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2365 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2366 (void) nfsrv_putattrbit(nd, &attrbits);
2367 error = nfscl_request(nd, dvp, p, cred, dstuff);
2370 NFSCL_INCRSEQID(owp->nfsow_seqid, nd);
2371 if (nd->nd_repstat == 0) {
2372 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
2374 stateid.seqid = *tl++;
2375 stateid.other[0] = *tl++;
2376 stateid.other[1] = *tl++;
2377 stateid.other[2] = *tl;
2378 rflags = fxdr_unsigned(u_int32_t, *(tl + 6));
2379 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
2382 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2383 deleg = fxdr_unsigned(int, *tl);
2384 if (deleg == NFSV4OPEN_DELEGATEREAD ||
2385 deleg == NFSV4OPEN_DELEGATEWRITE) {
2386 if (!(owp->nfsow_clp->nfsc_flags &
2387 NFSCLFLAGS_FIRSTDELEG))
2388 owp->nfsow_clp->nfsc_flags |=
2389 (NFSCLFLAGS_FIRSTDELEG | NFSCLFLAGS_GOTDELEG);
2391 sizeof (struct nfscldeleg) + NFSX_V4FHMAX,
2392 M_NFSCLDELEG, M_WAITOK);
2393 LIST_INIT(&dp->nfsdl_owner);
2394 LIST_INIT(&dp->nfsdl_lock);
2395 dp->nfsdl_clp = owp->nfsow_clp;
2396 newnfs_copyincred(cred, &dp->nfsdl_cred);
2397 nfscl_lockinit(&dp->nfsdl_rwlock);
2398 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
2400 dp->nfsdl_stateid.seqid = *tl++;
2401 dp->nfsdl_stateid.other[0] = *tl++;
2402 dp->nfsdl_stateid.other[1] = *tl++;
2403 dp->nfsdl_stateid.other[2] = *tl++;
2404 ret = fxdr_unsigned(int, *tl);
2405 if (deleg == NFSV4OPEN_DELEGATEWRITE) {
2406 dp->nfsdl_flags = NFSCLDL_WRITE;
2408 * Indicates how much the file can grow.
2410 NFSM_DISSECT(tl, u_int32_t *,
2412 limitby = fxdr_unsigned(int, *tl++);
2414 case NFSV4OPEN_LIMITSIZE:
2415 dp->nfsdl_sizelimit = fxdr_hyper(tl);
2417 case NFSV4OPEN_LIMITBLOCKS:
2418 dp->nfsdl_sizelimit =
2419 fxdr_unsigned(u_int64_t, *tl++);
2420 dp->nfsdl_sizelimit *=
2421 fxdr_unsigned(u_int64_t, *tl);
2424 error = NFSERR_BADXDR;
2428 dp->nfsdl_flags = NFSCLDL_READ;
2431 dp->nfsdl_flags |= NFSCLDL_RECALL;
2432 error = nfsrv_dissectace(nd, &dp->nfsdl_ace, false,
2436 } else if (deleg != NFSV4OPEN_DELEGATENONE) {
2437 error = NFSERR_BADXDR;
2440 error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp);
2443 /* Get rid of the PutFH and Getattr status values. */
2444 NFSM_DISSECT(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
2445 /* Load the directory attributes. */
2446 error = nfsm_loadattr(nd, dnap);
2450 if (dp != NULL && *attrflagp) {
2451 dp->nfsdl_change = nnap->na_filerev;
2452 dp->nfsdl_modtime = nnap->na_mtime;
2453 dp->nfsdl_flags |= NFSCLDL_MODTIMESET;
2456 * We can now complete the Open state.
2460 dp->nfsdl_fhlen = nfhp->nfh_len;
2461 NFSBCOPY(nfhp->nfh_fh, dp->nfsdl_fh, nfhp->nfh_len);
2464 * Get an Open structure that will be
2465 * attached to the OpenOwner, acquired already.
2467 error = nfscl_open(dvp, nfhp->nfh_fh, nfhp->nfh_len,
2468 (NFSV4OPEN_ACCESSWRITE | NFSV4OPEN_ACCESSREAD), 0,
2469 cred, p, NULL, &op, &newone, NULL, 0, false);
2472 op->nfso_stateid = stateid;
2473 newnfs_copyincred(cred, &op->nfso_cred);
2474 if ((rflags & NFSV4OPEN_RESULTCONFIRM)) {
2476 ret = nfsrpc_openconfirm(dvp, nfhp->nfh_fh,
2477 nfhp->nfh_len, op, cred, p);
2478 if (ret == NFSERR_DELAY)
2479 (void) nfs_catnap(PZERO, ret, "nfs_create");
2480 } while (ret == NFSERR_DELAY);
2485 * If the server is handing out delegations, but we didn't
2486 * get one because an OpenConfirm was required, try the
2487 * Open again, to get a delegation. This is a harmless no-op,
2488 * from a server's point of view.
2490 if ((rflags & NFSV4OPEN_RESULTCONFIRM) &&
2491 (owp->nfsow_clp->nfsc_flags & NFSCLFLAGS_GOTDELEG) &&
2492 !error && dp == NULL) {
2494 ret = nfsrpc_openrpc(VFSTONFS(dvp->v_mount), dvp,
2495 np->n_fhp->nfh_fh, np->n_fhp->nfh_len,
2496 nfhp->nfh_fh, nfhp->nfh_len,
2497 (NFSV4OPEN_ACCESSWRITE | NFSV4OPEN_ACCESSREAD), op,
2498 name, namelen, &dp, 0, 0x0, cred, p, 0, 1);
2499 if (ret == NFSERR_DELAY)
2500 (void) nfs_catnap(PZERO, ret, "nfs_crt2");
2501 } while (ret == NFSERR_DELAY);
2504 free(dp, M_NFSCLDELEG);
2507 if (ret == NFSERR_STALECLIENTID ||
2508 ret == NFSERR_STALEDONTRECOVER ||
2509 ret == NFSERR_BADSESSION)
2513 nfscl_openrelease(nmp, op, error, newone);
2516 if (nd->nd_repstat != 0 && error == 0)
2517 error = nd->nd_repstat;
2518 if (error == NFSERR_STALECLIENTID)
2519 nfscl_initiate_recovery(owp->nfsow_clp);
2523 else if (dp != NULL)
2524 free(dp, M_NFSCLDELEG);
2525 m_freem(nd->nd_mrep);
2533 nfsrpc_remove(vnode_t dvp, char *name, int namelen, vnode_t vp,
2534 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap, int *dattrflagp,
2538 struct nfsrv_descript nfsd, *nd = &nfsd;
2540 struct nfsmount *nmp;
2541 nfsv4stateid_t dstateid;
2542 int error, ret = 0, i;
2545 if (namelen > NFS_MAXNAMLEN)
2546 return (ENAMETOOLONG);
2547 nmp = VFSTONFS(dvp->v_mount);
2549 if (NFSHASNFSV4(nmp) && ret == 0) {
2550 ret = nfscl_removedeleg(vp, p, &dstateid);
2552 NFSCL_REQSTART(nd, NFSPROC_RETDELEGREMOVE, vp, cred);
2553 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID +
2555 if (NFSHASNFSV4N(nmp))
2558 *tl++ = dstateid.seqid;
2559 *tl++ = dstateid.other[0];
2560 *tl++ = dstateid.other[1];
2561 *tl++ = dstateid.other[2];
2562 *tl = txdr_unsigned(NFSV4OP_PUTFH);
2564 (void)nfsm_fhtom(nmp, nd, np->n_fhp->nfh_fh,
2565 np->n_fhp->nfh_len, 0);
2566 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2567 *tl = txdr_unsigned(NFSV4OP_REMOVE);
2573 NFSCL_REQSTART(nd, NFSPROC_REMOVE, dvp, cred);
2574 (void) nfsm_strtom(nd, name, namelen);
2575 error = nfscl_request(nd, dvp, p, cred, dstuff);
2578 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) {
2579 /* For NFSv4, parse out any Delereturn replies. */
2580 if (ret > 0 && nd->nd_repstat != 0 &&
2581 (nd->nd_flag & ND_NOMOREDATA)) {
2583 * If the Delegreturn failed, try again without
2584 * it. The server will Recall, as required.
2586 m_freem(nd->nd_mrep);
2589 for (i = 0; i < (ret * 2); i++) {
2590 if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) ==
2592 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2594 nd->nd_flag |= ND_NOMOREDATA;
2597 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, NULL);
2599 if (nd->nd_repstat && !error)
2600 error = nd->nd_repstat;
2602 m_freem(nd->nd_mrep);
2607 * Do an nfs rename rpc.
2610 nfsrpc_rename(vnode_t fdvp, vnode_t fvp, char *fnameptr, int fnamelen,
2611 vnode_t tdvp, vnode_t tvp, char *tnameptr, int tnamelen, struct ucred *cred,
2612 NFSPROC_T *p, struct nfsvattr *fnap, struct nfsvattr *tnap,
2613 int *fattrflagp, int *tattrflagp, void *fstuff, void *tstuff)
2616 struct nfsrv_descript nfsd, *nd = &nfsd;
2617 struct nfsmount *nmp;
2619 nfsattrbit_t attrbits;
2620 nfsv4stateid_t fdstateid, tdstateid;
2621 int error = 0, ret = 0, gottd = 0, gotfd = 0, i;
2625 nmp = VFSTONFS(fdvp->v_mount);
2626 if (fnamelen > NFS_MAXNAMLEN || tnamelen > NFS_MAXNAMLEN)
2627 return (ENAMETOOLONG);
2629 if (NFSHASNFSV4(nmp) && ret == 0) {
2630 ret = nfscl_renamedeleg(fvp, &fdstateid, &gotfd, tvp,
2631 &tdstateid, &gottd, p);
2632 if (gotfd && gottd) {
2633 NFSCL_REQSTART(nd, NFSPROC_RETDELEGRENAME2, fvp, cred);
2635 NFSCL_REQSTART(nd, NFSPROC_RETDELEGRENAME1, fvp, cred);
2637 NFSCL_REQSTART(nd, NFSPROC_RETDELEGRENAME1, tvp, cred);
2640 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID);
2641 if (NFSHASNFSV4N(nmp))
2644 *tl++ = fdstateid.seqid;
2645 *tl++ = fdstateid.other[0];
2646 *tl++ = fdstateid.other[1];
2647 *tl = fdstateid.other[2];
2649 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2650 *tl = txdr_unsigned(NFSV4OP_PUTFH);
2652 (void)nfsm_fhtom(nmp, nd, np->n_fhp->nfh_fh,
2653 np->n_fhp->nfh_len, 0);
2654 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2655 *tl = txdr_unsigned(NFSV4OP_DELEGRETURN);
2659 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID);
2660 if (NFSHASNFSV4N(nmp))
2663 *tl++ = tdstateid.seqid;
2664 *tl++ = tdstateid.other[0];
2665 *tl++ = tdstateid.other[1];
2666 *tl = tdstateid.other[2];
2669 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2670 *tl = txdr_unsigned(NFSV4OP_PUTFH);
2672 (void)nfsm_fhtom(nmp, nd, np->n_fhp->nfh_fh,
2673 np->n_fhp->nfh_len, 0);
2674 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2675 *tl = txdr_unsigned(NFSV4OP_SAVEFH);
2681 NFSCL_REQSTART(nd, NFSPROC_RENAME, fdvp, cred);
2682 if (nd->nd_flag & ND_NFSV4) {
2683 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2684 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2685 NFSWCCATTR_ATTRBIT(&attrbits);
2686 (void) nfsrv_putattrbit(nd, &attrbits);
2687 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2688 *tl = txdr_unsigned(NFSV4OP_PUTFH);
2689 (void)nfsm_fhtom(nmp, nd, VTONFS(tdvp)->n_fhp->nfh_fh,
2690 VTONFS(tdvp)->n_fhp->nfh_len, 0);
2691 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2692 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2693 (void) nfsrv_putattrbit(nd, &attrbits);
2694 nd->nd_flag |= ND_V4WCCATTR;
2695 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2696 *tl = txdr_unsigned(NFSV4OP_RENAME);
2698 (void) nfsm_strtom(nd, fnameptr, fnamelen);
2699 if (!(nd->nd_flag & ND_NFSV4))
2700 (void)nfsm_fhtom(nmp, nd, VTONFS(tdvp)->n_fhp->nfh_fh,
2701 VTONFS(tdvp)->n_fhp->nfh_len, 0);
2702 (void) nfsm_strtom(nd, tnameptr, tnamelen);
2703 error = nfscl_request(nd, fdvp, p, cred, fstuff);
2706 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) {
2707 /* For NFSv4, parse out any Delereturn replies. */
2708 if (ret > 0 && nd->nd_repstat != 0 &&
2709 (nd->nd_flag & ND_NOMOREDATA)) {
2711 * If the Delegreturn failed, try again without
2712 * it. The server will Recall, as required.
2714 m_freem(nd->nd_mrep);
2717 for (i = 0; i < (ret * 2); i++) {
2718 if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) ==
2720 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2722 if (i == 0 && ret > 1) {
2724 * If the Delegreturn failed, try again
2725 * without it. The server will Recall, as
2727 * If ret > 1, the first iteration of this
2728 * loop is the second DelegReturn result.
2730 m_freem(nd->nd_mrep);
2733 nd->nd_flag |= ND_NOMOREDATA;
2738 /* Now, the first wcc attribute reply. */
2739 if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) == ND_NFSV4) {
2740 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2742 nd->nd_flag |= ND_NOMOREDATA;
2744 error = nfscl_wcc_data(nd, fdvp, fnap, fattrflagp, NULL, NULL);
2745 /* and the second wcc attribute reply. */
2746 if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) == ND_NFSV4 &&
2748 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2750 nd->nd_flag |= ND_NOMOREDATA;
2753 error = nfscl_wcc_data(nd, tdvp, tnap, tattrflagp,
2756 if (nd->nd_repstat && !error)
2757 error = nd->nd_repstat;
2759 m_freem(nd->nd_mrep);
2764 * nfs hard link create rpc
2767 nfsrpc_link(vnode_t dvp, vnode_t vp, char *name, int namelen,
2768 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap,
2769 struct nfsvattr *nap, int *attrflagp, int *dattrflagp, void *dstuff)
2772 struct nfsrv_descript nfsd, *nd = &nfsd;
2773 nfsattrbit_t attrbits;
2778 if (namelen > NFS_MAXNAMLEN)
2779 return (ENAMETOOLONG);
2780 NFSCL_REQSTART(nd, NFSPROC_LINK, vp, cred);
2781 if (nd->nd_flag & ND_NFSV4) {
2782 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2783 *tl = txdr_unsigned(NFSV4OP_PUTFH);
2785 (void)nfsm_fhtom(VFSTONFS(dvp->v_mount), nd, VTONFS(dvp)->n_fhp->nfh_fh,
2786 VTONFS(dvp)->n_fhp->nfh_len, 0);
2787 if (nd->nd_flag & ND_NFSV4) {
2788 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2789 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2790 NFSWCCATTR_ATTRBIT(&attrbits);
2791 (void) nfsrv_putattrbit(nd, &attrbits);
2792 nd->nd_flag |= ND_V4WCCATTR;
2793 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2794 *tl = txdr_unsigned(NFSV4OP_LINK);
2796 (void) nfsm_strtom(nd, name, namelen);
2797 error = nfscl_request(nd, vp, p, cred, dstuff);
2800 if (nd->nd_flag & ND_NFSV3) {
2801 error = nfscl_postop_attr(nd, nap, attrflagp, dstuff);
2803 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp,
2805 } else if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) == ND_NFSV4) {
2807 * First, parse out the PutFH and Getattr result.
2809 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2811 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2813 nd->nd_flag |= ND_NOMOREDATA;
2815 * Get the pre-op attributes.
2817 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, NULL);
2819 if (nd->nd_repstat && !error)
2820 error = nd->nd_repstat;
2822 m_freem(nd->nd_mrep);
2827 * nfs symbolic link create rpc
2830 nfsrpc_symlink(vnode_t dvp, char *name, int namelen, const char *target,
2831 struct vattr *vap, struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap,
2832 struct nfsvattr *nnap, struct nfsfh **nfhpp, int *attrflagp,
2833 int *dattrflagp, void *dstuff)
2836 struct nfsrv_descript nfsd, *nd = &nfsd;
2837 struct nfsmount *nmp;
2838 int slen, error = 0;
2843 nmp = VFSTONFS(dvp->v_mount);
2844 slen = strlen(target);
2845 if (slen > NFS_MAXPATHLEN || namelen > NFS_MAXNAMLEN)
2846 return (ENAMETOOLONG);
2847 NFSCL_REQSTART(nd, NFSPROC_SYMLINK, dvp, cred);
2848 if (nd->nd_flag & ND_NFSV4) {
2849 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2850 *tl = txdr_unsigned(NFLNK);
2851 (void) nfsm_strtom(nd, target, slen);
2853 (void) nfsm_strtom(nd, name, namelen);
2854 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4))
2855 nfscl_fillsattr(nd, vap, dvp, 0, 0);
2856 if (!(nd->nd_flag & ND_NFSV4))
2857 (void) nfsm_strtom(nd, target, slen);
2858 if (nd->nd_flag & ND_NFSV2)
2859 nfscl_fillsattr(nd, vap, dvp, NFSSATTR_SIZENEG1, 0);
2860 error = nfscl_request(nd, dvp, p, cred, dstuff);
2863 if (nd->nd_flag & ND_NFSV4)
2864 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, NULL);
2865 if ((nd->nd_flag & ND_NFSV3) && !error) {
2866 if (!nd->nd_repstat)
2867 error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp);
2869 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp,
2872 if (nd->nd_repstat && !error)
2873 error = nd->nd_repstat;
2874 m_freem(nd->nd_mrep);
2876 * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry.
2877 * Only do this if vfs.nfs.ignore_eexist is set.
2878 * Never do this for NFSv4.1 or later minor versions, since sessions
2879 * should guarantee "exactly once" RPC semantics.
2881 if (error == EEXIST && nfsignore_eexist != 0 && (!NFSHASNFSV4(nmp) ||
2882 nmp->nm_minorvers == 0))
2891 nfsrpc_mkdir(vnode_t dvp, char *name, int namelen, struct vattr *vap,
2892 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap,
2893 struct nfsvattr *nnap, struct nfsfh **nfhpp, int *attrflagp,
2894 int *dattrflagp, void *dstuff)
2897 struct nfsrv_descript nfsd, *nd = &nfsd;
2898 nfsattrbit_t attrbits;
2901 struct nfsmount *nmp;
2906 nmp = VFSTONFS(dvp->v_mount);
2907 fhp = VTONFS(dvp)->n_fhp;
2908 if (namelen > NFS_MAXNAMLEN)
2909 return (ENAMETOOLONG);
2910 NFSCL_REQSTART(nd, NFSPROC_MKDIR, dvp, cred);
2911 if (nd->nd_flag & ND_NFSV4) {
2912 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2913 *tl = txdr_unsigned(NFDIR);
2915 (void) nfsm_strtom(nd, name, namelen);
2916 nfscl_fillsattr(nd, vap, dvp, NFSSATTR_SIZENEG1, 0);
2917 if (nd->nd_flag & ND_NFSV4) {
2918 NFSGETATTR_ATTRBIT(&attrbits);
2919 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2920 *tl++ = txdr_unsigned(NFSV4OP_GETFH);
2921 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2922 (void) nfsrv_putattrbit(nd, &attrbits);
2923 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2924 *tl = txdr_unsigned(NFSV4OP_PUTFH);
2925 (void)nfsm_fhtom(nmp, nd, fhp->nfh_fh, fhp->nfh_len, 0);
2926 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2927 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2928 (void) nfsrv_putattrbit(nd, &attrbits);
2930 error = nfscl_request(nd, dvp, p, cred, dstuff);
2933 if (nd->nd_flag & ND_NFSV4)
2934 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, NULL);
2935 if (!nd->nd_repstat && !error) {
2936 if (nd->nd_flag & ND_NFSV4) {
2937 NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
2938 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
2941 error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp);
2942 if (error == 0 && (nd->nd_flag & ND_NFSV4) != 0) {
2943 /* Get rid of the PutFH and Getattr status values. */
2944 NFSM_DISSECT(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
2945 /* Load the directory attributes. */
2946 error = nfsm_loadattr(nd, dnap);
2951 if ((nd->nd_flag & ND_NFSV3) && !error)
2952 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, NULL);
2953 if (nd->nd_repstat && !error)
2954 error = nd->nd_repstat;
2956 m_freem(nd->nd_mrep);
2958 * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry.
2959 * Only do this if vfs.nfs.ignore_eexist is set.
2960 * Never do this for NFSv4.1 or later minor versions, since sessions
2961 * should guarantee "exactly once" RPC semantics.
2963 if (error == EEXIST && nfsignore_eexist != 0 && (!NFSHASNFSV4(nmp) ||
2964 nmp->nm_minorvers == 0))
2970 * nfs remove directory call
2973 nfsrpc_rmdir(vnode_t dvp, char *name, int namelen, struct ucred *cred,
2974 NFSPROC_T *p, struct nfsvattr *dnap, int *dattrflagp, void *dstuff)
2976 struct nfsrv_descript nfsd, *nd = &nfsd;
2980 if (namelen > NFS_MAXNAMLEN)
2981 return (ENAMETOOLONG);
2982 NFSCL_REQSTART(nd, NFSPROC_RMDIR, dvp, cred);
2983 (void) nfsm_strtom(nd, name, namelen);
2984 error = nfscl_request(nd, dvp, p, cred, dstuff);
2987 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4))
2988 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, NULL);
2989 if (nd->nd_repstat && !error)
2990 error = nd->nd_repstat;
2991 m_freem(nd->nd_mrep);
2993 * Kludge: Map ENOENT => 0 assuming that you have a reply to a retry.
2995 if (error == ENOENT)
3002 * Always returns with either uio_resid unchanged, if you are at the
3003 * end of the directory, or uio_resid == 0, with all DIRBLKSIZ chunks
3005 * I felt this would allow caching of directory blocks more easily
3006 * than returning a pertially filled block.
3007 * Directory offset cookies:
3008 * Oh my, what to do with them...
3009 * I can think of three ways to deal with them:
3010 * 1 - have the layer above these RPCs maintain a map between logical
3011 * directory byte offsets and the NFS directory offset cookies
3012 * 2 - pass the opaque directory offset cookies up into userland
3013 * and let the libc functions deal with them, via the system call
3014 * 3 - return them to userland in the "struct dirent", so future versions
3015 * of libc can use them and do whatever is necessary to make things work
3016 * above these rpc calls, in the meantime
3017 * For now, I do #3 by "hiding" the directory offset cookies after the
3018 * d_name field in struct dirent. This is space inside d_reclen that
3019 * will be ignored by anything that doesn't know about them.
3020 * The directory offset cookies are filled in as the last 8 bytes of
3021 * each directory entry, after d_name. Someday, the userland libc
3022 * functions may be able to use these. In the meantime, it satisfies
3023 * OpenBSD's requirements for cookies being returned.
3024 * If expects the directory offset cookie for the read to be in uio_offset
3025 * and returns the one for the next entry after this directory block in
3029 nfsrpc_readdir(vnode_t vp, struct uio *uiop, nfsuint64 *cookiep,
3030 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp,
3031 int *eofp, void *stuff)
3034 struct dirent *dp = NULL;
3036 nfsquad_t cookie, ncookie;
3037 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
3038 struct nfsnode *dnp = VTONFS(vp);
3039 struct nfsvattr nfsva;
3040 struct nfsrv_descript nfsd, *nd = &nfsd;
3041 int error = 0, tlen, more_dirs = 1, blksiz = 0, bigenough = 1;
3042 int reqsize, tryformoredirs = 1, readsize, eof = 0, gotmnton = 0;
3043 u_int64_t dotfileid, dotdotfileid = 0, fakefileno = UINT64_MAX;
3045 nfsattrbit_t attrbits, dattrbits;
3046 u_int32_t rderr, *tl2 = NULL;
3049 KASSERT(uiop->uio_iovcnt == 1 &&
3050 (uiop->uio_resid & (DIRBLKSIZ - 1)) == 0,
3051 ("nfs readdirrpc bad uio"));
3052 ncookie.lval[0] = ncookie.lval[1] = 0;
3054 * There is no point in reading a lot more than uio_resid, however
3055 * adding one additional DIRBLKSIZ makes sense. Since uio_resid
3056 * and nm_readdirsize are both exact multiples of DIRBLKSIZ, this
3057 * will never make readsize > nm_readdirsize.
3059 readsize = nmp->nm_readdirsize;
3060 if (readsize > uiop->uio_resid)
3061 readsize = uiop->uio_resid + DIRBLKSIZ;
3066 tresid = uiop->uio_resid;
3067 cookie.lval[0] = cookiep->nfsuquad[0];
3068 cookie.lval[1] = cookiep->nfsuquad[1];
3072 * For NFSv4, first create the "." and ".." entries.
3074 if (NFSHASNFSV4(nmp)) {
3075 reqsize = 6 * NFSX_UNSIGNED;
3076 NFSGETATTR_ATTRBIT(&dattrbits);
3077 NFSZERO_ATTRBIT(&attrbits);
3078 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_FILEID);
3079 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TYPE);
3080 if (NFSISSET_ATTRBIT(&dnp->n_vattr.na_suppattr,
3081 NFSATTRBIT_MOUNTEDONFILEID)) {
3082 NFSSETBIT_ATTRBIT(&attrbits,
3083 NFSATTRBIT_MOUNTEDONFILEID);
3087 * Must fake it. Use the fileno, except when the
3088 * fsid is != to that of the directory. For that
3089 * case, generate a fake fileno that is not the same.
3091 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_FSID);
3096 * Joy, oh joy. For V4 we get to hand craft '.' and '..'.
3098 if (uiop->uio_offset == 0) {
3099 NFSCL_REQSTART(nd, NFSPROC_LOOKUPP, vp, cred);
3100 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
3101 *tl++ = txdr_unsigned(NFSV4OP_GETFH);
3102 *tl = txdr_unsigned(NFSV4OP_GETATTR);
3103 (void) nfsrv_putattrbit(nd, &attrbits);
3104 error = nfscl_request(nd, vp, p, cred, stuff);
3107 dotfileid = 0; /* Fake out the compiler. */
3108 if ((nd->nd_flag & ND_NOMOREDATA) == 0) {
3109 error = nfsm_loadattr(nd, &nfsva);
3112 dotfileid = nfsva.na_fileid;
3114 if (nd->nd_repstat == 0) {
3115 NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
3116 len = fxdr_unsigned(int, *(tl + 4));
3117 if (len > 0 && len <= NFSX_V4FHMAX)
3118 error = nfsm_advance(nd, NFSM_RNDUP(len), -1);
3122 NFSM_DISSECT(tl, u_int32_t *, 2*NFSX_UNSIGNED);
3123 nfsva.na_mntonfileno = UINT64_MAX;
3124 error = nfsv4_loadattr(nd, NULL, &nfsva, NULL,
3125 NULL, 0, NULL, NULL, NULL, NULL, NULL, 0,
3126 NULL, NULL, NULL, p, cred);
3128 dotdotfileid = dotfileid;
3129 } else if (gotmnton) {
3130 if (nfsva.na_mntonfileno != UINT64_MAX)
3131 dotdotfileid = nfsva.na_mntonfileno;
3133 dotdotfileid = nfsva.na_fileid;
3134 } else if (nfsva.na_filesid[0] ==
3135 dnp->n_vattr.na_filesid[0] &&
3136 nfsva.na_filesid[1] ==
3137 dnp->n_vattr.na_filesid[1]) {
3138 dotdotfileid = nfsva.na_fileid;
3142 } while (fakefileno ==
3144 dotdotfileid = fakefileno;
3147 } else if (nd->nd_repstat == NFSERR_NOENT) {
3149 * Lookupp returns NFSERR_NOENT when we are
3150 * at the root, so just use the current dir.
3153 dotdotfileid = dotfileid;
3155 error = nd->nd_repstat;
3157 m_freem(nd->nd_mrep);
3161 dp = (struct dirent *)uiop->uio_iov->iov_base;
3162 dp->d_pad0 = dp->d_pad1 = 0;
3164 dp->d_type = DT_DIR;
3165 dp->d_fileno = dotfileid;
3167 *((uint64_t *)dp->d_name) = 0; /* Zero pad it. */
3168 dp->d_name[0] = '.';
3169 dp->d_reclen = _GENERIC_DIRSIZ(dp) + NFSX_HYPER;
3171 * Just make these offset cookie 0.
3173 tl = (u_int32_t *)&dp->d_name[8];
3176 blksiz += dp->d_reclen;
3177 uiop->uio_resid -= dp->d_reclen;
3178 uiop->uio_offset += dp->d_reclen;
3179 uiop->uio_iov->iov_base =
3180 (char *)uiop->uio_iov->iov_base + dp->d_reclen;
3181 uiop->uio_iov->iov_len -= dp->d_reclen;
3182 dp = (struct dirent *)uiop->uio_iov->iov_base;
3183 dp->d_pad0 = dp->d_pad1 = 0;
3185 dp->d_type = DT_DIR;
3186 dp->d_fileno = dotdotfileid;
3188 *((uint64_t *)dp->d_name) = 0;
3189 dp->d_name[0] = '.';
3190 dp->d_name[1] = '.';
3191 dp->d_reclen = _GENERIC_DIRSIZ(dp) + NFSX_HYPER;
3193 * Just make these offset cookie 0.
3195 tl = (u_int32_t *)&dp->d_name[8];
3198 blksiz += dp->d_reclen;
3199 uiop->uio_resid -= dp->d_reclen;
3200 uiop->uio_offset += dp->d_reclen;
3201 uiop->uio_iov->iov_base =
3202 (char *)uiop->uio_iov->iov_base + dp->d_reclen;
3203 uiop->uio_iov->iov_len -= dp->d_reclen;
3205 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_RDATTRERROR);
3207 reqsize = 5 * NFSX_UNSIGNED;
3211 * Loop around doing readdir rpc's of size readsize.
3212 * The stopping criteria is EOF or buffer full.
3214 while (more_dirs && bigenough) {
3216 NFSCL_REQSTART(nd, NFSPROC_READDIR, vp, cred);
3217 if (nd->nd_flag & ND_NFSV2) {
3218 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
3219 *tl++ = cookie.lval[1];
3220 *tl = txdr_unsigned(readsize);
3222 NFSM_BUILD(tl, u_int32_t *, reqsize);
3223 *tl++ = cookie.lval[0];
3224 *tl++ = cookie.lval[1];
3225 if (cookie.qval == 0) {
3230 *tl++ = dnp->n_cookieverf.nfsuquad[0];
3231 *tl++ = dnp->n_cookieverf.nfsuquad[1];
3234 if (nd->nd_flag & ND_NFSV4) {
3235 *tl++ = txdr_unsigned(readsize);
3236 *tl = txdr_unsigned(readsize);
3237 (void) nfsrv_putattrbit(nd, &attrbits);
3238 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
3239 *tl = txdr_unsigned(NFSV4OP_GETATTR);
3240 (void) nfsrv_putattrbit(nd, &dattrbits);
3242 *tl = txdr_unsigned(readsize);
3245 error = nfscl_request(nd, vp, p, cred, stuff);
3248 if (!(nd->nd_flag & ND_NFSV2)) {
3249 if (nd->nd_flag & ND_NFSV3)
3250 error = nfscl_postop_attr(nd, nap, attrflagp,
3252 if (!nd->nd_repstat && !error) {
3253 NFSM_DISSECT(tl, u_int32_t *, NFSX_HYPER);
3255 dnp->n_cookieverf.nfsuquad[0] = *tl++;
3256 dnp->n_cookieverf.nfsuquad[1] = *tl;
3260 if (nd->nd_repstat || error) {
3262 error = nd->nd_repstat;
3265 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3266 more_dirs = fxdr_unsigned(int, *tl);
3270 /* loop through the dir entries, doctoring them to 4bsd form */
3271 while (more_dirs && bigenough) {
3272 if (nd->nd_flag & ND_NFSV4) {
3273 NFSM_DISSECT(tl, u_int32_t *, 3*NFSX_UNSIGNED);
3274 ncookie.lval[0] = *tl++;
3275 ncookie.lval[1] = *tl++;
3276 len = fxdr_unsigned(int, *tl);
3277 } else if (nd->nd_flag & ND_NFSV3) {
3278 NFSM_DISSECT(tl, u_int32_t *, 3*NFSX_UNSIGNED);
3279 nfsva.na_fileid = fxdr_hyper(tl);
3281 len = fxdr_unsigned(int, *tl);
3283 NFSM_DISSECT(tl, u_int32_t *, 2*NFSX_UNSIGNED);
3284 nfsva.na_fileid = fxdr_unsigned(uint64_t,
3286 len = fxdr_unsigned(int, *tl);
3288 if (len <= 0 || len > NFS_MAXNAMLEN) {
3292 tlen = roundup2(len, 8);
3294 tlen += 8; /* To ensure null termination. */
3295 left = DIRBLKSIZ - blksiz;
3296 if (_GENERIC_DIRLEN(len) + NFSX_HYPER > left) {
3297 NFSBZERO(uiop->uio_iov->iov_base, left);
3298 dp->d_reclen += left;
3299 uiop->uio_iov->iov_base =
3300 (char *)uiop->uio_iov->iov_base + left;
3301 uiop->uio_iov->iov_len -= left;
3302 uiop->uio_resid -= left;
3303 uiop->uio_offset += left;
3306 if (_GENERIC_DIRLEN(len) + NFSX_HYPER >
3310 dp = (struct dirent *)uiop->uio_iov->iov_base;
3311 dp->d_pad0 = dp->d_pad1 = 0;
3314 dp->d_reclen = _GENERIC_DIRLEN(len) +
3316 dp->d_type = DT_UNKNOWN;
3317 blksiz += dp->d_reclen;
3318 if (blksiz == DIRBLKSIZ)
3320 uiop->uio_resid -= DIRHDSIZ;
3321 uiop->uio_offset += DIRHDSIZ;
3322 uiop->uio_iov->iov_base =
3323 (char *)uiop->uio_iov->iov_base + DIRHDSIZ;
3324 uiop->uio_iov->iov_len -= DIRHDSIZ;
3325 error = nfsm_mbufuio(nd, uiop, len);
3328 cp = uiop->uio_iov->iov_base;
3331 cp += tlen; /* points to cookie storage */
3332 tl2 = (u_int32_t *)cp;
3333 uiop->uio_iov->iov_base =
3334 (char *)uiop->uio_iov->iov_base + tlen +
3336 uiop->uio_iov->iov_len -= tlen + NFSX_HYPER;
3337 uiop->uio_resid -= tlen + NFSX_HYPER;
3338 uiop->uio_offset += (tlen + NFSX_HYPER);
3340 error = nfsm_advance(nd, NFSM_RNDUP(len), -1);
3344 if (nd->nd_flag & ND_NFSV4) {
3346 nfsva.na_mntonfileno = UINT64_MAX;
3347 error = nfsv4_loadattr(nd, NULL, &nfsva, NULL,
3348 NULL, 0, NULL, NULL, NULL, NULL, NULL, 0,
3349 NULL, NULL, &rderr, p, cred);
3352 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3353 } else if (nd->nd_flag & ND_NFSV3) {
3354 NFSM_DISSECT(tl, u_int32_t *, 3*NFSX_UNSIGNED);
3355 ncookie.lval[0] = *tl++;
3356 ncookie.lval[1] = *tl++;
3358 NFSM_DISSECT(tl, u_int32_t *, 2*NFSX_UNSIGNED);
3359 ncookie.lval[0] = 0;
3360 ncookie.lval[1] = *tl++;
3363 if (nd->nd_flag & ND_NFSV4) {
3368 if (nfsva.na_mntonfileno != UINT64_MAX)
3369 dp->d_fileno = nfsva.na_mntonfileno;
3371 dp->d_fileno = nfsva.na_fileid;
3372 } else if (nfsva.na_filesid[0] ==
3373 dnp->n_vattr.na_filesid[0] &&
3374 nfsva.na_filesid[1] ==
3375 dnp->n_vattr.na_filesid[1]) {
3376 dp->d_fileno = nfsva.na_fileid;
3380 } while (fakefileno ==
3382 dp->d_fileno = fakefileno;
3384 dp->d_type = vtonfs_dtype(nfsva.na_type);
3387 dp->d_fileno = nfsva.na_fileid;
3389 *tl2++ = cookiep->nfsuquad[0] = cookie.lval[0] =
3391 *tl2 = cookiep->nfsuquad[1] = cookie.lval[1] =
3394 more_dirs = fxdr_unsigned(int, *tl);
3397 * If at end of rpc data, get the eof boolean
3400 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3401 eof = fxdr_unsigned(int, *tl);
3404 if (nd->nd_flag & ND_NFSV4) {
3405 error = nfscl_postop_attr(nd, nap, attrflagp,
3411 m_freem(nd->nd_mrep);
3415 * Fill last record, iff any, out to a multiple of DIRBLKSIZ
3416 * by increasing d_reclen for the last record.
3419 left = DIRBLKSIZ - blksiz;
3420 NFSBZERO(uiop->uio_iov->iov_base, left);
3421 dp->d_reclen += left;
3422 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base +
3424 uiop->uio_iov->iov_len -= left;
3425 uiop->uio_resid -= left;
3426 uiop->uio_offset += left;
3430 * If returning no data, assume end of file.
3431 * If not bigenough, return not end of file, since you aren't
3432 * returning all the data
3433 * Otherwise, return the eof flag from the server.
3436 if (tresid == ((size_t)(uiop->uio_resid)))
3438 else if (!bigenough)
3445 * Add extra empty records to any remaining DIRBLKSIZ chunks.
3447 while (uiop->uio_resid > 0 && uiop->uio_resid != tresid) {
3448 dp = (struct dirent *)uiop->uio_iov->iov_base;
3449 NFSBZERO(dp, DIRBLKSIZ);
3450 dp->d_type = DT_UNKNOWN;
3451 tl = (u_int32_t *)&dp->d_name[4];
3452 *tl++ = cookie.lval[0];
3453 *tl = cookie.lval[1];
3454 dp->d_reclen = DIRBLKSIZ;
3455 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base +
3457 uiop->uio_iov->iov_len -= DIRBLKSIZ;
3458 uiop->uio_resid -= DIRBLKSIZ;
3459 uiop->uio_offset += DIRBLKSIZ;
3463 if (nd->nd_mrep != NULL)
3464 m_freem(nd->nd_mrep);
3470 * NFS V3 readdir plus RPC. Used in place of nfsrpc_readdir().
3471 * (Also used for NFS V4 when mount flag set.)
3472 * (ditto above w.r.t. multiple of DIRBLKSIZ, etc.)
3475 nfsrpc_readdirplus(vnode_t vp, struct uio *uiop, nfsuint64 *cookiep,
3476 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp,
3477 int *eofp, void *stuff)
3480 struct dirent *dp = NULL;
3482 vnode_t newvp = NULLVP;
3483 struct nfsrv_descript nfsd, *nd = &nfsd;
3484 struct nameidata nami, *ndp = &nami;
3485 struct componentname *cnp = &ndp->ni_cnd;
3486 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
3487 struct nfsnode *dnp = VTONFS(vp), *np;
3488 struct nfsvattr nfsva;
3490 nfsquad_t cookie, ncookie;
3491 int error = 0, tlen, more_dirs = 1, blksiz = 0, bigenough = 1;
3492 int attrflag, tryformoredirs = 1, eof = 0, gotmnton = 0;
3493 int isdotdot = 0, unlocknewvp = 0;
3494 u_int64_t dotfileid, dotdotfileid = 0, fakefileno = UINT64_MAX;
3495 u_int64_t fileno = 0;
3497 nfsattrbit_t attrbits, dattrbits;
3499 u_int32_t *tl2 = NULL, rderr;
3500 struct timespec dctime, ts;
3503 KASSERT(uiop->uio_iovcnt == 1 &&
3504 (uiop->uio_resid & (DIRBLKSIZ - 1)) == 0,
3505 ("nfs readdirplusrpc bad uio"));
3506 ncookie.lval[0] = ncookie.lval[1] = 0;
3507 timespecclear(&dctime);
3513 cookie.lval[0] = cookiep->nfsuquad[0];
3514 cookie.lval[1] = cookiep->nfsuquad[1];
3515 tresid = uiop->uio_resid;
3518 * For NFSv4, first create the "." and ".." entries.
3520 if (NFSHASNFSV4(nmp)) {
3521 NFSGETATTR_ATTRBIT(&dattrbits);
3522 NFSZERO_ATTRBIT(&attrbits);
3523 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_FILEID);
3524 if (NFSISSET_ATTRBIT(&dnp->n_vattr.na_suppattr,
3525 NFSATTRBIT_MOUNTEDONFILEID)) {
3526 NFSSETBIT_ATTRBIT(&attrbits,
3527 NFSATTRBIT_MOUNTEDONFILEID);
3531 * Must fake it. Use the fileno, except when the
3532 * fsid is != to that of the directory. For that
3533 * case, generate a fake fileno that is not the same.
3535 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_FSID);
3540 * Joy, oh joy. For V4 we get to hand craft '.' and '..'.
3542 if (uiop->uio_offset == 0) {
3543 NFSCL_REQSTART(nd, NFSPROC_LOOKUPP, vp, cred);
3544 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
3545 *tl++ = txdr_unsigned(NFSV4OP_GETFH);
3546 *tl = txdr_unsigned(NFSV4OP_GETATTR);
3547 (void) nfsrv_putattrbit(nd, &attrbits);
3548 error = nfscl_request(nd, vp, p, cred, stuff);
3551 dotfileid = 0; /* Fake out the compiler. */
3552 if ((nd->nd_flag & ND_NOMOREDATA) == 0) {
3553 error = nfsm_loadattr(nd, &nfsva);
3556 dctime = nfsva.na_ctime;
3557 dotfileid = nfsva.na_fileid;
3559 if (nd->nd_repstat == 0) {
3560 NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
3561 len = fxdr_unsigned(int, *(tl + 4));
3562 if (len > 0 && len <= NFSX_V4FHMAX)
3563 error = nfsm_advance(nd, NFSM_RNDUP(len), -1);
3567 NFSM_DISSECT(tl, u_int32_t *, 2*NFSX_UNSIGNED);
3568 nfsva.na_mntonfileno = UINT64_MAX;
3569 error = nfsv4_loadattr(nd, NULL, &nfsva, NULL,
3570 NULL, 0, NULL, NULL, NULL, NULL, NULL, 0,
3571 NULL, NULL, NULL, p, cred);
3573 dotdotfileid = dotfileid;
3574 } else if (gotmnton) {
3575 if (nfsva.na_mntonfileno != UINT64_MAX)
3576 dotdotfileid = nfsva.na_mntonfileno;
3578 dotdotfileid = nfsva.na_fileid;
3579 } else if (nfsva.na_filesid[0] ==
3580 dnp->n_vattr.na_filesid[0] &&
3581 nfsva.na_filesid[1] ==
3582 dnp->n_vattr.na_filesid[1]) {
3583 dotdotfileid = nfsva.na_fileid;
3587 } while (fakefileno ==
3589 dotdotfileid = fakefileno;
3592 } else if (nd->nd_repstat == NFSERR_NOENT) {
3594 * Lookupp returns NFSERR_NOENT when we are
3595 * at the root, so just use the current dir.
3598 dotdotfileid = dotfileid;
3600 error = nd->nd_repstat;
3602 m_freem(nd->nd_mrep);
3606 dp = (struct dirent *)uiop->uio_iov->iov_base;
3607 dp->d_pad0 = dp->d_pad1 = 0;
3609 dp->d_type = DT_DIR;
3610 dp->d_fileno = dotfileid;
3612 *((uint64_t *)dp->d_name) = 0; /* Zero pad it. */
3613 dp->d_name[0] = '.';
3614 dp->d_reclen = _GENERIC_DIRSIZ(dp) + NFSX_HYPER;
3616 * Just make these offset cookie 0.
3618 tl = (u_int32_t *)&dp->d_name[8];
3621 blksiz += dp->d_reclen;
3622 uiop->uio_resid -= dp->d_reclen;
3623 uiop->uio_offset += dp->d_reclen;
3624 uiop->uio_iov->iov_base =
3625 (char *)uiop->uio_iov->iov_base + dp->d_reclen;
3626 uiop->uio_iov->iov_len -= dp->d_reclen;
3627 dp = (struct dirent *)uiop->uio_iov->iov_base;
3628 dp->d_pad0 = dp->d_pad1 = 0;
3630 dp->d_type = DT_DIR;
3631 dp->d_fileno = dotdotfileid;
3633 *((uint64_t *)dp->d_name) = 0;
3634 dp->d_name[0] = '.';
3635 dp->d_name[1] = '.';
3636 dp->d_reclen = _GENERIC_DIRSIZ(dp) + NFSX_HYPER;
3638 * Just make these offset cookie 0.
3640 tl = (u_int32_t *)&dp->d_name[8];
3643 blksiz += dp->d_reclen;
3644 uiop->uio_resid -= dp->d_reclen;
3645 uiop->uio_offset += dp->d_reclen;
3646 uiop->uio_iov->iov_base =
3647 (char *)uiop->uio_iov->iov_base + dp->d_reclen;
3648 uiop->uio_iov->iov_len -= dp->d_reclen;
3650 NFSREADDIRPLUS_ATTRBIT(&attrbits);
3652 NFSSETBIT_ATTRBIT(&attrbits,
3653 NFSATTRBIT_MOUNTEDONFILEID);
3654 if (!NFSISSET_ATTRBIT(&dnp->n_vattr.na_suppattr,
3655 NFSATTRBIT_TIMECREATE))
3656 NFSCLRBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMECREATE);
3660 * Loop around doing readdir rpc's of size nm_readdirsize.
3661 * The stopping criteria is EOF or buffer full.
3663 while (more_dirs && bigenough) {
3665 NFSCL_REQSTART(nd, NFSPROC_READDIRPLUS, vp, cred);
3666 NFSM_BUILD(tl, u_int32_t *, 6 * NFSX_UNSIGNED);
3667 *tl++ = cookie.lval[0];
3668 *tl++ = cookie.lval[1];
3669 if (cookie.qval == 0) {
3674 *tl++ = dnp->n_cookieverf.nfsuquad[0];
3675 *tl++ = dnp->n_cookieverf.nfsuquad[1];
3678 *tl++ = txdr_unsigned(nmp->nm_readdirsize);
3679 *tl = txdr_unsigned(nmp->nm_readdirsize);
3680 if (nd->nd_flag & ND_NFSV4) {
3681 (void) nfsrv_putattrbit(nd, &attrbits);
3682 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
3683 *tl = txdr_unsigned(NFSV4OP_GETATTR);
3684 (void) nfsrv_putattrbit(nd, &dattrbits);
3687 error = nfscl_request(nd, vp, p, cred, stuff);
3690 if (nd->nd_flag & ND_NFSV3)
3691 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
3692 if (nd->nd_repstat || error) {
3694 error = nd->nd_repstat;
3697 if ((nd->nd_flag & ND_NFSV3) != 0 && *attrflagp != 0)
3698 dctime = nap->na_ctime;
3699 NFSM_DISSECT(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
3701 dnp->n_cookieverf.nfsuquad[0] = *tl++;
3702 dnp->n_cookieverf.nfsuquad[1] = *tl++;
3704 more_dirs = fxdr_unsigned(int, *tl);
3708 /* loop through the dir entries, doctoring them to 4bsd form */
3709 while (more_dirs && bigenough) {
3710 NFSM_DISSECT(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
3711 if (nd->nd_flag & ND_NFSV4) {
3712 ncookie.lval[0] = *tl++;
3713 ncookie.lval[1] = *tl++;
3715 fileno = fxdr_hyper(tl);
3718 len = fxdr_unsigned(int, *tl);
3719 if (len <= 0 || len > NFS_MAXNAMLEN) {
3723 tlen = roundup2(len, 8);
3725 tlen += 8; /* To ensure null termination. */
3726 left = DIRBLKSIZ - blksiz;
3727 if (_GENERIC_DIRLEN(len) + NFSX_HYPER > left) {
3728 NFSBZERO(uiop->uio_iov->iov_base, left);
3729 dp->d_reclen += left;
3730 uiop->uio_iov->iov_base =
3731 (char *)uiop->uio_iov->iov_base + left;
3732 uiop->uio_iov->iov_len -= left;
3733 uiop->uio_resid -= left;
3734 uiop->uio_offset += left;
3737 if (_GENERIC_DIRLEN(len) + NFSX_HYPER >
3741 dp = (struct dirent *)uiop->uio_iov->iov_base;
3742 dp->d_pad0 = dp->d_pad1 = 0;
3745 dp->d_reclen = _GENERIC_DIRLEN(len) +
3747 dp->d_type = DT_UNKNOWN;
3748 blksiz += dp->d_reclen;
3749 if (blksiz == DIRBLKSIZ)
3751 uiop->uio_resid -= DIRHDSIZ;
3752 uiop->uio_offset += DIRHDSIZ;
3753 uiop->uio_iov->iov_base =
3754 (char *)uiop->uio_iov->iov_base + DIRHDSIZ;
3755 uiop->uio_iov->iov_len -= DIRHDSIZ;
3756 cnp->cn_nameptr = uiop->uio_iov->iov_base;
3757 cnp->cn_namelen = len;
3759 error = nfsm_mbufuio(nd, uiop, len);
3762 cp = uiop->uio_iov->iov_base;
3765 cp += tlen; /* points to cookie storage */
3766 tl2 = (u_int32_t *)cp;
3767 if (len == 2 && cnp->cn_nameptr[0] == '.' &&
3768 cnp->cn_nameptr[1] == '.')
3772 uiop->uio_iov->iov_base =
3773 (char *)uiop->uio_iov->iov_base + tlen +
3775 uiop->uio_iov->iov_len -= tlen + NFSX_HYPER;
3776 uiop->uio_resid -= tlen + NFSX_HYPER;
3777 uiop->uio_offset += (tlen + NFSX_HYPER);
3779 error = nfsm_advance(nd, NFSM_RNDUP(len), -1);
3784 if (nd->nd_flag & ND_NFSV3) {
3785 NFSM_DISSECT(tl, u_int32_t *, 3*NFSX_UNSIGNED);
3786 ncookie.lval[0] = *tl++;
3787 ncookie.lval[1] = *tl++;
3788 attrflag = fxdr_unsigned(int, *tl);
3790 error = nfsm_loadattr(nd, &nfsva);
3794 NFSM_DISSECT(tl,u_int32_t *,NFSX_UNSIGNED);
3796 error = nfsm_getfh(nd, &nfhp);
3800 if (!attrflag && nfhp != NULL) {
3801 free(nfhp, M_NFSFH);
3806 nfsva.na_mntonfileno = 0xffffffff;
3807 error = nfsv4_loadattr(nd, NULL, &nfsva, &nfhp,
3808 NULL, 0, NULL, NULL, NULL, NULL, NULL, 0,
3809 NULL, NULL, &rderr, p, cred);
3815 if (nd->nd_flag & ND_NFSV4) {
3818 } else if (gotmnton) {
3819 if (nfsva.na_mntonfileno != 0xffffffff)
3820 dp->d_fileno = nfsva.na_mntonfileno;
3822 dp->d_fileno = nfsva.na_fileid;
3823 } else if (nfsva.na_filesid[0] ==
3824 dnp->n_vattr.na_filesid[0] &&
3825 nfsva.na_filesid[1] ==
3826 dnp->n_vattr.na_filesid[1]) {
3827 dp->d_fileno = nfsva.na_fileid;
3831 } while (fakefileno ==
3833 dp->d_fileno = fakefileno;
3836 dp->d_fileno = fileno;
3838 *tl2++ = cookiep->nfsuquad[0] = cookie.lval[0] =
3840 *tl2 = cookiep->nfsuquad[1] = cookie.lval[1] =
3845 if (NFSRV_CMPFH(nfhp->nfh_fh, nfhp->nfh_len,
3846 dnp->n_fhp->nfh_fh, dnp->n_fhp->nfh_len)) {
3850 free(nfhp, M_NFSFH);
3852 } else if (isdotdot != 0) {
3854 * Skip doing a nfscl_nget() call for "..".
3855 * There's a race between acquiring the nfs
3856 * node here and lookups that look for the
3857 * directory being read (in the parent).
3858 * It would try to get a lock on ".." here,
3859 * owning the lock on the directory being
3860 * read. Lookup will hold the lock on ".."
3861 * and try to acquire the lock on the
3862 * directory being read.
3863 * If the directory is unlocked/relocked,
3864 * then there is a LOR with the buflock
3867 free(nfhp, M_NFSFH);
3869 error = nfscl_nget(vp->v_mount, vp,
3870 nfhp, cnp, p, &np, NULL, LK_EXCLUSIVE);
3875 * If n_localmodtime >= time before RPC,
3876 * then a file modification operation,
3877 * such as VOP_SETATTR() of size, has
3878 * occurred while the Lookup RPC and
3879 * acquisition of the vnode happened. As
3880 * such, the attributes might be stale,
3881 * with possibly an incorrect size.
3885 &np->n_localmodtime) &&
3886 timespeccmp(&np->n_localmodtime,
3888 NFSCL_DEBUG(4, "nfsrpc_readdirplus:"
3889 " localmod stale attributes\n");
3896 if (newvp != NULLVP) {
3898 error = nfscl_loadattrcache(&newvp,
3899 &nfsva, NULL, NULL, 0, 0);
3908 vtonfs_dtype(np->n_vattr.na_type);
3910 NFSCNHASH(cnp, HASHINIT);
3911 if (cnp->cn_namelen <= NCHNAMLEN &&
3912 ndp->ni_dvp != ndp->ni_vp &&
3913 (newvp->v_type != VDIR ||
3914 dctime.tv_sec != 0)) {
3915 cache_enter_time_flags(ndp->ni_dvp,
3918 newvp->v_type != VDIR ? NULL :
3919 &dctime, VFS_CACHE_DROPOLD);
3928 } else if (nfhp != NULL) {
3929 free(nfhp, M_NFSFH);
3931 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3932 more_dirs = fxdr_unsigned(int, *tl);
3935 * If at end of rpc data, get the eof boolean
3938 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3939 eof = fxdr_unsigned(int, *tl);
3942 if (nd->nd_flag & ND_NFSV4) {
3943 error = nfscl_postop_attr(nd, nap, attrflagp,
3949 m_freem(nd->nd_mrep);
3953 * Fill last record, iff any, out to a multiple of DIRBLKSIZ
3954 * by increasing d_reclen for the last record.
3957 left = DIRBLKSIZ - blksiz;
3958 NFSBZERO(uiop->uio_iov->iov_base, left);
3959 dp->d_reclen += left;
3960 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base +
3962 uiop->uio_iov->iov_len -= left;
3963 uiop->uio_resid -= left;
3964 uiop->uio_offset += left;
3968 * If returning no data, assume end of file.
3969 * If not bigenough, return not end of file, since you aren't
3970 * returning all the data
3971 * Otherwise, return the eof flag from the server.
3974 if (tresid == uiop->uio_resid)
3976 else if (!bigenough)
3983 * Add extra empty records to any remaining DIRBLKSIZ chunks.
3985 while (uiop->uio_resid > 0 && uiop->uio_resid != tresid) {
3986 dp = (struct dirent *)uiop->uio_iov->iov_base;
3987 NFSBZERO(dp, DIRBLKSIZ);
3988 dp->d_type = DT_UNKNOWN;
3989 tl = (u_int32_t *)&dp->d_name[4];
3990 *tl++ = cookie.lval[0];
3991 *tl = cookie.lval[1];
3992 dp->d_reclen = DIRBLKSIZ;
3993 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base +
3995 uiop->uio_iov->iov_len -= DIRBLKSIZ;
3996 uiop->uio_resid -= DIRBLKSIZ;
3997 uiop->uio_offset += DIRBLKSIZ;
4001 if (nd->nd_mrep != NULL)
4002 m_freem(nd->nd_mrep);
4011 nfsrpc_commit(vnode_t vp, u_quad_t offset, int cnt, struct ucred *cred,
4012 NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, void *stuff)
4015 struct nfsrv_descript nfsd, *nd = &nfsd;
4016 nfsattrbit_t attrbits;
4018 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
4021 NFSCL_REQSTART(nd, NFSPROC_COMMIT, vp, cred);
4022 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
4023 txdr_hyper(offset, tl);
4025 *tl = txdr_unsigned(cnt);
4026 if (nd->nd_flag & ND_NFSV4) {
4028 * And do a Getattr op.
4030 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
4031 *tl = txdr_unsigned(NFSV4OP_GETATTR);
4032 NFSGETATTR_ATTRBIT(&attrbits);
4033 (void) nfsrv_putattrbit(nd, &attrbits);
4035 error = nfscl_request(nd, vp, p, cred, stuff);
4038 error = nfscl_wcc_data(nd, vp, nap, attrflagp, NULL, NULL);
4039 if (!error && !nd->nd_repstat) {
4040 NFSM_DISSECT(tl, u_int32_t *, NFSX_VERF);
4042 if (NFSBCMP(nmp->nm_verf, tl, NFSX_VERF)) {
4043 NFSBCOPY(tl, nmp->nm_verf, NFSX_VERF);
4044 nd->nd_repstat = NFSERR_STALEWRITEVERF;
4047 if (nd->nd_flag & ND_NFSV4)
4048 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
4051 if (!error && nd->nd_repstat)
4052 error = nd->nd_repstat;
4053 m_freem(nd->nd_mrep);
4058 * NFS byte range lock rpc.
4059 * (Mostly just calls one of the three lower level RPC routines.)
4062 nfsrpc_advlock(vnode_t vp, off_t size, int op, struct flock *fl,
4063 int reclaim, struct ucred *cred, NFSPROC_T *p, void *id, int flags)
4065 struct nfscllockowner *lp;
4066 struct nfsclclient *clp;
4068 struct nfsrv_descript nfsd, *nd = &nfsd;
4069 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
4072 u_int32_t clidrev = 0;
4073 int error = 0, newone = 0, expireret = 0, retrycnt, donelocally;
4077 * Convert the flock structure into a start and end and do POSIX
4080 switch (fl->l_whence) {
4084 * Caller is responsible for adding any necessary offset
4085 * when SEEK_CUR is used.
4087 start = fl->l_start;
4091 start = size + fl->l_start;
4092 off = size + fl->l_start;
4099 if (fl->l_len != 0) {
4100 end = start + fl->l_len - 1;
4111 if (op == F_GETLK) {
4112 error = nfscl_getcl(vp->v_mount, cred, p, false, true, &clp);
4115 error = nfscl_lockt(vp, clp, off, len, fl, p, id, flags);
4117 clidrev = clp->nfsc_clientidrev;
4118 error = nfsrpc_lockt(nd, vp, clp, off, len, fl, cred,
4120 } else if (error == -1) {
4123 nfscl_clientrelease(clp);
4124 } else if (op == F_UNLCK && fl->l_type == F_UNLCK) {
4126 * We must loop around for all lockowner cases.
4129 error = nfscl_getcl(vp->v_mount, cred, p, false, true, &clp);
4133 error = nfscl_relbytelock(vp, off, len, cred, p, callcnt,
4134 clp, id, flags, &lp, &dorpc);
4136 * If it returns a NULL lp, we're done.
4140 nfscl_clientrelease(clp);
4142 nfscl_releasealllocks(clp, vp, p, id, flags);
4145 if (nmp->nm_clp != NULL)
4146 clidrev = nmp->nm_clp->nfsc_clientidrev;
4150 * If the server doesn't support Posix lock semantics,
4151 * only allow locks on the entire file, since it won't
4152 * handle overlapping byte ranges.
4153 * There might still be a problem when a lock
4154 * upgrade/downgrade (read<->write) occurs, since the
4155 * server "might" expect an unlock first?
4157 if (dorpc && (lp->nfsl_open->nfso_posixlock ||
4158 (off == 0 && len == NFS64BITSSET))) {
4160 * Since the lock records will go away, we must
4161 * wait for grace and delay here.
4164 error = nfsrpc_locku(nd, nmp, lp, off, len,
4165 NFSV4LOCKT_READ, cred, p, 0);
4166 if ((nd->nd_repstat == NFSERR_GRACE ||
4167 nd->nd_repstat == NFSERR_DELAY) &&
4169 (void) nfs_catnap(PZERO, (int)nd->nd_repstat,
4171 } while ((nd->nd_repstat == NFSERR_GRACE ||
4172 nd->nd_repstat == NFSERR_DELAY) && error == 0);
4175 } while (error == 0 && nd->nd_repstat == 0);
4176 nfscl_releasealllocks(clp, vp, p, id, flags);
4177 } else if (op == F_SETLK) {
4178 error = nfscl_getbytelock(vp, off, len, fl->l_type, cred, p,
4179 NULL, 0, id, flags, NULL, NULL, &lp, &newone, &donelocally);
4180 if (error || donelocally) {
4183 if (nmp->nm_clp != NULL)
4184 clidrev = nmp->nm_clp->nfsc_clientidrev;
4187 nfhp = VTONFS(vp)->n_fhp;
4188 if (!lp->nfsl_open->nfso_posixlock &&
4189 (off != 0 || len != NFS64BITSSET)) {
4192 error = nfsrpc_lock(nd, nmp, vp, nfhp->nfh_fh,
4193 nfhp->nfh_len, lp, newone, reclaim, off,
4194 len, fl->l_type, cred, p, 0);
4197 error = nd->nd_repstat;
4198 nfscl_lockrelease(lp, error, newone);
4203 error = nd->nd_repstat;
4204 if (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
4205 error == NFSERR_STALEDONTRECOVER ||
4206 error == NFSERR_STALECLIENTID || error == NFSERR_DELAY ||
4207 error == NFSERR_BADSESSION) {
4208 (void) nfs_catnap(PZERO, error, "nfs_advlock");
4209 } else if ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID)
4211 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
4214 } while (error == NFSERR_GRACE ||
4215 error == NFSERR_STALECLIENTID || error == NFSERR_DELAY ||
4216 error == NFSERR_STALEDONTRECOVER || error == NFSERR_STALESTATEID ||
4217 error == NFSERR_BADSESSION ||
4218 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
4219 expireret == 0 && clidrev != 0 && retrycnt < 4));
4220 if (error && retrycnt >= 4)
4226 * The lower level routine for the LockT case.
4229 nfsrpc_lockt(struct nfsrv_descript *nd, vnode_t vp,
4230 struct nfsclclient *clp, u_int64_t off, u_int64_t len, struct flock *fl,
4231 struct ucred *cred, NFSPROC_T *p, void *id, int flags)
4234 int error, type, size;
4235 uint8_t own[NFSV4CL_LOCKNAMELEN + NFSX_V4FHMAX];
4237 struct nfsmount *nmp;
4238 struct nfsclsession *tsep;
4240 nmp = VFSTONFS(vp->v_mount);
4241 NFSCL_REQSTART(nd, NFSPROC_LOCKT, vp, cred);
4242 NFSM_BUILD(tl, u_int32_t *, 7 * NFSX_UNSIGNED);
4243 if (fl->l_type == F_RDLCK)
4244 *tl++ = txdr_unsigned(NFSV4LOCKT_READ);
4246 *tl++ = txdr_unsigned(NFSV4LOCKT_WRITE);
4247 txdr_hyper(off, tl);
4249 txdr_hyper(len, tl);
4251 tsep = nfsmnt_mdssession(nmp);
4252 *tl++ = tsep->nfsess_clientid.lval[0];
4253 *tl = tsep->nfsess_clientid.lval[1];
4254 nfscl_filllockowner(id, own, flags);
4256 NFSBCOPY(np->n_fhp->nfh_fh, &own[NFSV4CL_LOCKNAMELEN],
4257 np->n_fhp->nfh_len);
4258 (void)nfsm_strtom(nd, own, NFSV4CL_LOCKNAMELEN + np->n_fhp->nfh_len);
4259 error = nfscl_request(nd, vp, p, cred, NULL);
4262 if (nd->nd_repstat == 0) {
4263 fl->l_type = F_UNLCK;
4264 } else if (nd->nd_repstat == NFSERR_DENIED) {
4266 fl->l_whence = SEEK_SET;
4267 NFSM_DISSECT(tl, u_int32_t *, 8 * NFSX_UNSIGNED);
4268 fl->l_start = fxdr_hyper(tl);
4270 len = fxdr_hyper(tl);
4272 if (len == NFS64BITSSET)
4276 type = fxdr_unsigned(int, *tl++);
4277 if (type == NFSV4LOCKT_WRITE)
4278 fl->l_type = F_WRLCK;
4280 fl->l_type = F_RDLCK;
4282 * XXX For now, I have no idea what to do with the
4283 * conflicting lock_owner, so I'll just set the pid == 0
4284 * and skip over the lock_owner.
4286 fl->l_pid = (pid_t)0;
4288 size = fxdr_unsigned(int, *tl);
4289 if (size < 0 || size > NFSV4_OPAQUELIMIT)
4292 error = nfsm_advance(nd, NFSM_RNDUP(size), -1);
4293 } else if (nd->nd_repstat == NFSERR_STALECLIENTID)
4294 nfscl_initiate_recovery(clp);
4296 m_freem(nd->nd_mrep);
4301 * Lower level function that performs the LockU RPC.
4304 nfsrpc_locku(struct nfsrv_descript *nd, struct nfsmount *nmp,
4305 struct nfscllockowner *lp, u_int64_t off, u_int64_t len,
4306 u_int32_t type, struct ucred *cred, NFSPROC_T *p, int syscred)
4311 nfscl_reqstart(nd, NFSPROC_LOCKU, nmp, lp->nfsl_open->nfso_fh,
4312 lp->nfsl_open->nfso_fhlen, NULL, NULL, 0, 0, cred);
4313 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID + 6 * NFSX_UNSIGNED);
4314 *tl++ = txdr_unsigned(type);
4315 *tl = txdr_unsigned(lp->nfsl_seqid);
4316 if (nfstest_outofseq &&
4317 (arc4random() % nfstest_outofseq) == 0)
4318 *tl = txdr_unsigned(lp->nfsl_seqid + 1);
4320 if (NFSHASNFSV4N(nmp))
4323 *tl++ = lp->nfsl_stateid.seqid;
4324 *tl++ = lp->nfsl_stateid.other[0];
4325 *tl++ = lp->nfsl_stateid.other[1];
4326 *tl++ = lp->nfsl_stateid.other[2];
4327 txdr_hyper(off, tl);
4329 txdr_hyper(len, tl);
4331 nd->nd_flag |= ND_USEGSSNAME;
4332 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4333 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4334 NFSCL_INCRSEQID(lp->nfsl_seqid, nd);
4337 if (nd->nd_repstat == 0) {
4338 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID);
4339 lp->nfsl_stateid.seqid = *tl++;
4340 lp->nfsl_stateid.other[0] = *tl++;
4341 lp->nfsl_stateid.other[1] = *tl++;
4342 lp->nfsl_stateid.other[2] = *tl;
4343 } else if (nd->nd_repstat == NFSERR_STALESTATEID)
4344 nfscl_initiate_recovery(lp->nfsl_open->nfso_own->nfsow_clp);
4346 m_freem(nd->nd_mrep);
4351 * The actual Lock RPC.
4354 nfsrpc_lock(struct nfsrv_descript *nd, struct nfsmount *nmp, vnode_t vp,
4355 u_int8_t *nfhp, int fhlen, struct nfscllockowner *lp, int newone,
4356 int reclaim, u_int64_t off, u_int64_t len, short type, struct ucred *cred,
4357 NFSPROC_T *p, int syscred)
4361 uint8_t own[NFSV4CL_LOCKNAMELEN + NFSX_V4FHMAX];
4362 struct nfsclsession *tsep;
4364 nfscl_reqstart(nd, NFSPROC_LOCK, nmp, nfhp, fhlen, NULL, NULL, 0, 0,
4366 NFSM_BUILD(tl, u_int32_t *, 7 * NFSX_UNSIGNED);
4367 if (type == F_RDLCK)
4368 *tl++ = txdr_unsigned(NFSV4LOCKT_READ);
4370 *tl++ = txdr_unsigned(NFSV4LOCKT_WRITE);
4371 *tl++ = txdr_unsigned(reclaim);
4372 txdr_hyper(off, tl);
4374 txdr_hyper(len, tl);
4378 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID +
4379 2 * NFSX_UNSIGNED + NFSX_HYPER);
4380 *tl++ = txdr_unsigned(lp->nfsl_open->nfso_own->nfsow_seqid);
4381 if (NFSHASNFSV4N(nmp))
4384 *tl++ = lp->nfsl_open->nfso_stateid.seqid;
4385 *tl++ = lp->nfsl_open->nfso_stateid.other[0];
4386 *tl++ = lp->nfsl_open->nfso_stateid.other[1];
4387 *tl++ = lp->nfsl_open->nfso_stateid.other[2];
4388 *tl++ = txdr_unsigned(lp->nfsl_seqid);
4389 tsep = nfsmnt_mdssession(nmp);
4390 *tl++ = tsep->nfsess_clientid.lval[0];
4391 *tl = tsep->nfsess_clientid.lval[1];
4392 NFSBCOPY(lp->nfsl_owner, own, NFSV4CL_LOCKNAMELEN);
4393 NFSBCOPY(nfhp, &own[NFSV4CL_LOCKNAMELEN], fhlen);
4394 (void)nfsm_strtom(nd, own, NFSV4CL_LOCKNAMELEN + fhlen);
4397 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID + NFSX_UNSIGNED);
4398 if (NFSHASNFSV4N(nmp))
4401 *tl++ = lp->nfsl_stateid.seqid;
4402 *tl++ = lp->nfsl_stateid.other[0];
4403 *tl++ = lp->nfsl_stateid.other[1];
4404 *tl++ = lp->nfsl_stateid.other[2];
4405 *tl = txdr_unsigned(lp->nfsl_seqid);
4406 if (nfstest_outofseq &&
4407 (arc4random() % nfstest_outofseq) == 0)
4408 *tl = txdr_unsigned(lp->nfsl_seqid + 1);
4411 nd->nd_flag |= ND_USEGSSNAME;
4412 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, vp, p, cred,
4413 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4417 NFSCL_INCRSEQID(lp->nfsl_open->nfso_own->nfsow_seqid, nd);
4418 NFSCL_INCRSEQID(lp->nfsl_seqid, nd);
4419 if (nd->nd_repstat == 0) {
4420 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID);
4421 lp->nfsl_stateid.seqid = *tl++;
4422 lp->nfsl_stateid.other[0] = *tl++;
4423 lp->nfsl_stateid.other[1] = *tl++;
4424 lp->nfsl_stateid.other[2] = *tl;
4425 } else if (nd->nd_repstat == NFSERR_DENIED) {
4426 NFSM_DISSECT(tl, u_int32_t *, 8 * NFSX_UNSIGNED);
4427 size = fxdr_unsigned(int, *(tl + 7));
4428 if (size < 0 || size > NFSV4_OPAQUELIMIT)
4431 error = nfsm_advance(nd, NFSM_RNDUP(size), -1);
4432 } else if (nd->nd_repstat == NFSERR_STALESTATEID)
4433 nfscl_initiate_recovery(lp->nfsl_open->nfso_own->nfsow_clp);
4435 m_freem(nd->nd_mrep);
4441 * (always called with the vp for the mount point)
4444 nfsrpc_statfs(vnode_t vp, struct nfsstatfs *sbp, struct nfsfsinfo *fsp,
4445 uint32_t *leasep, struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap,
4446 int *attrflagp, void *stuff)
4448 u_int32_t *tl = NULL;
4449 struct nfsrv_descript nfsd, *nd = &nfsd;
4450 struct nfsmount *nmp;
4451 nfsattrbit_t attrbits;
4455 nmp = VFSTONFS(vp->v_mount);
4456 if (NFSHASNFSV4(nmp)) {
4458 * For V4, you actually do a getattr.
4460 NFSCL_REQSTART(nd, NFSPROC_GETATTR, vp, cred);
4462 NFSROOTFS_GETATTRBIT(&attrbits);
4464 NFSSTATFS_GETATTRBIT(&attrbits);
4465 (void) nfsrv_putattrbit(nd, &attrbits);
4466 nd->nd_flag |= ND_USEGSSNAME;
4467 error = nfscl_request(nd, vp, p, cred, stuff);
4470 if (nd->nd_repstat == 0) {
4471 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0,
4472 NULL, NULL, sbp, fsp, NULL, 0, NULL, leasep, NULL,
4475 nmp->nm_fsid[0] = nap->na_filesid[0];
4476 nmp->nm_fsid[1] = nap->na_filesid[1];
4477 NFSSETHASSETFSID(nmp);
4481 error = nd->nd_repstat;
4486 NFSCL_REQSTART(nd, NFSPROC_FSSTAT, vp, NULL);
4487 error = nfscl_request(nd, vp, p, cred, stuff);
4490 if (nd->nd_flag & ND_NFSV3) {
4491 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
4495 if (nd->nd_repstat) {
4496 error = nd->nd_repstat;
4499 NFSM_DISSECT(tl, u_int32_t *,
4500 NFSX_STATFS(nd->nd_flag & ND_NFSV3));
4502 if (NFSHASNFSV3(nmp)) {
4503 sbp->sf_tbytes = fxdr_hyper(tl); tl += 2;
4504 sbp->sf_fbytes = fxdr_hyper(tl); tl += 2;
4505 sbp->sf_abytes = fxdr_hyper(tl); tl += 2;
4506 sbp->sf_tfiles = fxdr_hyper(tl); tl += 2;
4507 sbp->sf_ffiles = fxdr_hyper(tl); tl += 2;
4508 sbp->sf_afiles = fxdr_hyper(tl); tl += 2;
4509 sbp->sf_invarsec = fxdr_unsigned(u_int32_t, *tl);
4510 } else if (NFSHASNFSV4(nmp) == 0) {
4511 sbp->sf_tsize = fxdr_unsigned(u_int32_t, *tl++);
4512 sbp->sf_bsize = fxdr_unsigned(u_int32_t, *tl++);
4513 sbp->sf_blocks = fxdr_unsigned(u_int32_t, *tl++);
4514 sbp->sf_bfree = fxdr_unsigned(u_int32_t, *tl++);
4515 sbp->sf_bavail = fxdr_unsigned(u_int32_t, *tl);
4518 m_freem(nd->nd_mrep);
4526 nfsrpc_pathconf(vnode_t vp, struct nfsv3_pathconf *pc,
4527 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp,
4530 struct nfsrv_descript nfsd, *nd = &nfsd;
4531 struct nfsmount *nmp;
4533 nfsattrbit_t attrbits;
4538 nmp = VFSTONFS(vp->v_mount);
4539 if (NFSHASNFSV4(nmp)) {
4541 if ((nmp->nm_privflag & NFSMNTP_FAKEROOTFH) != 0 &&
4542 nmp->nm_fhsize == 0) {
4543 /* Attempt to get the actual root file handle. */
4544 error = nfsrpc_getdirpath(nmp, NFSMNT_DIRPATH(nmp),
4548 if (np->n_fhp->nfh_len == NFSX_FHMAX + 1)
4549 nfscl_statfs(vp, cred, p);
4552 * For V4, you actually do a getattr.
4554 NFSCL_REQSTART(nd, NFSPROC_GETATTR, vp, cred);
4555 NFSPATHCONF_GETATTRBIT(&attrbits);
4556 (void) nfsrv_putattrbit(nd, &attrbits);
4557 nd->nd_flag |= ND_USEGSSNAME;
4558 error = nfscl_request(nd, vp, p, cred, stuff);
4561 if (nd->nd_repstat == 0) {
4562 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0,
4563 pc, NULL, NULL, NULL, NULL, 0, NULL, NULL, NULL, p,
4568 error = nd->nd_repstat;
4571 NFSCL_REQSTART(nd, NFSPROC_PATHCONF, vp, NULL);
4572 error = nfscl_request(nd, vp, p, cred, stuff);
4575 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
4576 if (nd->nd_repstat && !error)
4577 error = nd->nd_repstat;
4579 NFSM_DISSECT(tl, u_int32_t *, NFSX_V3PATHCONF);
4580 pc->pc_linkmax = fxdr_unsigned(u_int32_t, *tl++);
4581 pc->pc_namemax = fxdr_unsigned(u_int32_t, *tl++);
4582 pc->pc_notrunc = fxdr_unsigned(u_int32_t, *tl++);
4583 pc->pc_chownrestricted =
4584 fxdr_unsigned(u_int32_t, *tl++);
4585 pc->pc_caseinsensitive =
4586 fxdr_unsigned(u_int32_t, *tl++);
4587 pc->pc_casepreserving = fxdr_unsigned(u_int32_t, *tl);
4591 m_freem(nd->nd_mrep);
4596 * nfs version 3 fsinfo rpc call
4599 nfsrpc_fsinfo(vnode_t vp, struct nfsfsinfo *fsp, struct ucred *cred,
4600 NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, void *stuff)
4603 struct nfsrv_descript nfsd, *nd = &nfsd;
4607 NFSCL_REQSTART(nd, NFSPROC_FSINFO, vp, NULL);
4608 error = nfscl_request(nd, vp, p, cred, stuff);
4611 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
4612 if (nd->nd_repstat && !error)
4613 error = nd->nd_repstat;
4615 NFSM_DISSECT(tl, u_int32_t *, NFSX_V3FSINFO);
4616 fsp->fs_rtmax = fxdr_unsigned(u_int32_t, *tl++);
4617 fsp->fs_rtpref = fxdr_unsigned(u_int32_t, *tl++);
4618 fsp->fs_rtmult = fxdr_unsigned(u_int32_t, *tl++);
4619 fsp->fs_wtmax = fxdr_unsigned(u_int32_t, *tl++);
4620 fsp->fs_wtpref = fxdr_unsigned(u_int32_t, *tl++);
4621 fsp->fs_wtmult = fxdr_unsigned(u_int32_t, *tl++);
4622 fsp->fs_dtpref = fxdr_unsigned(u_int32_t, *tl++);
4623 fsp->fs_maxfilesize = fxdr_hyper(tl);
4625 fxdr_nfsv3time(tl, &fsp->fs_timedelta);
4627 fsp->fs_properties = fxdr_unsigned(u_int32_t, *tl);
4630 m_freem(nd->nd_mrep);
4635 * This function performs the Renew RPC.
4638 nfsrpc_renew(struct nfsclclient *clp, struct nfsclds *dsp, struct ucred *cred,
4642 struct nfsrv_descript nfsd;
4643 struct nfsrv_descript *nd = &nfsd;
4644 struct nfsmount *nmp;
4646 struct nfssockreq *nrp;
4647 struct nfsclsession *tsep;
4649 nmp = clp->nfsc_nmp;
4653 nfscl_reqstart(nd, NFSPROC_RENEW, nmp, NULL, 0, NULL, NULL, 0,
4656 nfscl_reqstart(nd, NFSPROC_RENEW, nmp, NULL, 0, NULL,
4657 &dsp->nfsclds_sess, 0, 0, NULL);
4658 if (!NFSHASNFSV4N(nmp)) {
4659 /* NFSv4.1 just uses a Sequence Op and not a Renew. */
4660 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
4661 tsep = nfsmnt_mdssession(nmp);
4662 *tl++ = tsep->nfsess_clientid.lval[0];
4663 *tl = tsep->nfsess_clientid.lval[1];
4667 nrp = dsp->nfsclds_sockp;
4669 /* If NULL, use the MDS socket. */
4670 nrp = &nmp->nm_sockreq;
4671 nd->nd_flag |= ND_USEGSSNAME;
4673 error = newnfs_request(nd, nmp, NULL, nrp, NULL, p, cred,
4674 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4676 error = newnfs_request(nd, nmp, NULL, nrp, NULL, p, cred,
4677 NFS_PROG, NFS_VER4, NULL, 1, NULL, &dsp->nfsclds_sess);
4679 nfscl_cancelreqs(dsp);
4683 error = nd->nd_repstat;
4684 m_freem(nd->nd_mrep);
4689 * This function performs the Releaselockowner RPC.
4692 nfsrpc_rellockown(struct nfsmount *nmp, struct nfscllockowner *lp,
4693 uint8_t *fh, int fhlen, struct ucred *cred, NFSPROC_T *p)
4695 struct nfsrv_descript nfsd, *nd = &nfsd;
4698 uint8_t own[NFSV4CL_LOCKNAMELEN + NFSX_V4FHMAX];
4699 struct nfsclsession *tsep;
4701 if (NFSHASNFSV4N(nmp)) {
4702 /* For NFSv4.1, do a FreeStateID. */
4703 nfscl_reqstart(nd, NFSPROC_FREESTATEID, nmp, NULL, 0, NULL,
4705 nfsm_stateidtom(nd, &lp->nfsl_stateid, NFSSTATEID_PUTSTATEID);
4707 nfscl_reqstart(nd, NFSPROC_RELEASELCKOWN, nmp, NULL, 0, NULL,
4709 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
4710 tsep = nfsmnt_mdssession(nmp);
4711 *tl++ = tsep->nfsess_clientid.lval[0];
4712 *tl = tsep->nfsess_clientid.lval[1];
4713 NFSBCOPY(lp->nfsl_owner, own, NFSV4CL_LOCKNAMELEN);
4714 NFSBCOPY(fh, &own[NFSV4CL_LOCKNAMELEN], fhlen);
4715 (void)nfsm_strtom(nd, own, NFSV4CL_LOCKNAMELEN + fhlen);
4717 nd->nd_flag |= ND_USEGSSNAME;
4718 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4719 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4722 error = nd->nd_repstat;
4723 m_freem(nd->nd_mrep);
4728 * This function performs the Compound to get the mount pt FH.
4731 nfsrpc_getdirpath(struct nfsmount *nmp, u_char *dirpath, struct ucred *cred,
4735 struct nfsrv_descript nfsd;
4736 struct nfsrv_descript *nd = &nfsd;
4737 u_char *cp, *cp2, *fhp;
4738 int error, cnt, len, setnil;
4741 nfscl_reqstart(nd, NFSPROC_PUTROOTFH, nmp, NULL, 0, &opcntp, NULL, 0,
4750 while (*cp2 != '\0' && *cp2 != '/')
4757 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
4758 *tl = txdr_unsigned(NFSV4OP_LOOKUP);
4759 nfsm_strtom(nd, cp, strlen(cp));
4765 } while (*cp != '\0');
4766 if (NFSHASNFSV4N(nmp))
4767 /* Has a Sequence Op done by nfscl_reqstart(). */
4768 *opcntp = txdr_unsigned(3 + cnt);
4770 *opcntp = txdr_unsigned(2 + cnt);
4771 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
4772 *tl = txdr_unsigned(NFSV4OP_GETFH);
4773 nd->nd_flag |= ND_USEGSSNAME;
4774 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4775 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4778 if (nd->nd_repstat == 0) {
4779 NFSM_DISSECT(tl, u_int32_t *, (3 + 2 * cnt) * NFSX_UNSIGNED);
4780 tl += (2 + 2 * cnt);
4781 if ((len = fxdr_unsigned(int, *tl)) <= 0 ||
4783 nd->nd_repstat = NFSERR_BADXDR;
4785 fhp = malloc(len + 1, M_TEMP, M_WAITOK);
4786 nd->nd_repstat = nfsrv_mtostr(nd, fhp, len);
4787 if (nd->nd_repstat == 0) {
4789 if (nmp->nm_fhsize == 0) {
4790 NFSBCOPY(fhp, nmp->nm_fh, len);
4791 nmp->nm_fhsize = len;
4798 error = nd->nd_repstat;
4800 m_freem(nd->nd_mrep);
4805 * This function performs the Delegreturn RPC.
4808 nfsrpc_delegreturn(struct nfscldeleg *dp, struct ucred *cred,
4809 struct nfsmount *nmp, NFSPROC_T *p, int syscred)
4812 struct nfsrv_descript nfsd;
4813 struct nfsrv_descript *nd = &nfsd;
4816 nfscl_reqstart(nd, NFSPROC_DELEGRETURN, nmp, dp->nfsdl_fh,
4817 dp->nfsdl_fhlen, NULL, NULL, 0, 0, cred);
4818 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID);
4819 if (NFSHASNFSV4N(nmp))
4822 *tl++ = dp->nfsdl_stateid.seqid;
4823 *tl++ = dp->nfsdl_stateid.other[0];
4824 *tl++ = dp->nfsdl_stateid.other[1];
4825 *tl = dp->nfsdl_stateid.other[2];
4827 nd->nd_flag |= ND_USEGSSNAME;
4828 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4829 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4832 error = nd->nd_repstat;
4833 m_freem(nd->nd_mrep);
4841 nfsrpc_getacl(vnode_t vp, struct ucred *cred, NFSPROC_T *p,
4842 struct acl *aclp, void *stuff)
4844 struct nfsrv_descript nfsd, *nd = &nfsd;
4846 nfsattrbit_t attrbits;
4847 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
4849 if (nfsrv_useacl == 0 || !NFSHASNFSV4(nmp))
4850 return (EOPNOTSUPP);
4851 NFSCL_REQSTART(nd, NFSPROC_GETACL, vp, cred);
4852 NFSZERO_ATTRBIT(&attrbits);
4853 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_ACL);
4854 (void) nfsrv_putattrbit(nd, &attrbits);
4855 error = nfscl_request(nd, vp, p, cred, stuff);
4858 if (!nd->nd_repstat)
4859 error = nfsv4_loadattr(nd, vp, NULL, NULL, NULL, 0, NULL,
4860 NULL, NULL, NULL, aclp, 0, NULL, NULL, NULL, p, cred);
4862 error = nd->nd_repstat;
4863 m_freem(nd->nd_mrep);
4871 nfsrpc_setacl(vnode_t vp, struct ucred *cred, NFSPROC_T *p,
4872 struct acl *aclp, void *stuff)
4875 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
4877 if (nfsrv_useacl == 0 || !NFSHASNFSV4(nmp))
4878 return (EOPNOTSUPP);
4879 error = nfsrpc_setattr(vp, NULL, aclp, cred, p, NULL, NULL, stuff);
4887 nfsrpc_setaclrpc(vnode_t vp, struct ucred *cred, NFSPROC_T *p,
4888 struct acl *aclp, nfsv4stateid_t *stateidp, void *stuff)
4890 struct nfsrv_descript nfsd, *nd = &nfsd;
4892 nfsattrbit_t attrbits;
4893 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
4895 if (!NFSHASNFSV4(nmp))
4896 return (EOPNOTSUPP);
4897 NFSCL_REQSTART(nd, NFSPROC_SETACL, vp, cred);
4898 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID);
4899 NFSZERO_ATTRBIT(&attrbits);
4900 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_ACL);
4901 (void) nfsv4_fillattr(nd, vp->v_mount, vp, aclp, NULL, NULL, 0,
4902 &attrbits, NULL, NULL, 0, 0, 0, 0, (uint64_t)0, NULL);
4903 error = nfscl_request(nd, vp, p, cred, stuff);
4906 /* Don't care about the pre/postop attributes */
4907 m_freem(nd->nd_mrep);
4908 return (nd->nd_repstat);
4912 * Do the NFSv4.1 Exchange ID.
4915 nfsrpc_exchangeid(struct nfsmount *nmp, struct nfsclclient *clp,
4916 struct nfssockreq *nrp, int minorvers, uint32_t exchflags,
4917 struct nfsclds **dspp, struct ucred *cred, NFSPROC_T *p)
4919 uint32_t *tl, v41flags;
4920 struct nfsrv_descript nfsd;
4921 struct nfsrv_descript *nd = &nfsd;
4922 struct nfsclds *dsp;
4923 struct timespec verstime;
4928 minorvers = nmp->nm_minorvers;
4929 nfscl_reqstart(nd, NFSPROC_EXCHANGEID, nmp, NULL, 0, NULL, NULL,
4930 NFS_VER4, minorvers, NULL);
4931 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_UNSIGNED);
4932 *tl++ = txdr_unsigned(nfsboottime.tv_sec); /* Client owner */
4933 *tl = txdr_unsigned(clp->nfsc_rev);
4934 (void) nfsm_strtom(nd, clp->nfsc_id, clp->nfsc_idlen);
4936 NFSM_BUILD(tl, uint32_t *, 3 * NFSX_UNSIGNED);
4937 *tl++ = txdr_unsigned(exchflags);
4938 *tl++ = txdr_unsigned(NFSV4EXCH_SP4NONE);
4940 /* Set the implementation id4 */
4941 *tl = txdr_unsigned(1);
4942 (void) nfsm_strtom(nd, "freebsd.org", strlen("freebsd.org"));
4943 (void) nfsm_strtom(nd, version, strlen(version));
4944 NFSM_BUILD(tl, uint32_t *, NFSX_V4TIME);
4945 verstime.tv_sec = 1293840000; /* Jan 1, 2011 */
4946 verstime.tv_nsec = 0;
4947 txdr_nfsv4time(&verstime, tl);
4948 nd->nd_flag |= ND_USEGSSNAME;
4949 error = newnfs_request(nd, nmp, NULL, nrp, NULL, p, cred,
4950 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4951 NFSCL_DEBUG(1, "exchangeid err=%d reps=%d\n", error,
4952 (int)nd->nd_repstat);
4955 if (nd->nd_repstat == 0) {
4956 NFSM_DISSECT(tl, uint32_t *, 6 * NFSX_UNSIGNED + NFSX_HYPER);
4957 len = fxdr_unsigned(int, *(tl + 7));
4958 if (len < 0 || len > NFSV4_OPAQUELIMIT) {
4959 error = NFSERR_BADXDR;
4962 dsp = malloc(sizeof(struct nfsclds) + len + 1, M_NFSCLDS,
4964 dsp->nfsclds_expire = NFSD_MONOSEC + clp->nfsc_renew;
4965 dsp->nfsclds_servownlen = len;
4966 dsp->nfsclds_sess.nfsess_clientid.lval[0] = *tl++;
4967 dsp->nfsclds_sess.nfsess_clientid.lval[1] = *tl++;
4968 dsp->nfsclds_sess.nfsess_sequenceid =
4969 fxdr_unsigned(uint32_t, *tl++);
4970 v41flags = fxdr_unsigned(uint32_t, *tl);
4971 if ((v41flags & NFSV4EXCH_USEPNFSMDS) != 0 &&
4972 NFSHASPNFSOPT(nmp)) {
4973 NFSCL_DEBUG(1, "set PNFS\n");
4975 nmp->nm_state |= NFSSTA_PNFS;
4977 dsp->nfsclds_flags |= NFSCLDS_MDS;
4979 if ((v41flags & NFSV4EXCH_USEPNFSDS) != 0)
4980 dsp->nfsclds_flags |= NFSCLDS_DS;
4981 if (minorvers == NFSV42_MINORVERSION)
4982 dsp->nfsclds_flags |= NFSCLDS_MINORV2;
4984 nd->nd_repstat = nfsrv_mtostr(nd,
4985 dsp->nfsclds_serverown, len);
4986 if (nd->nd_repstat == 0) {
4987 mtx_init(&dsp->nfsclds_mtx, "nfsds", NULL, MTX_DEF);
4988 mtx_init(&dsp->nfsclds_sess.nfsess_mtx, "nfssession",
4990 nfscl_initsessionslots(&dsp->nfsclds_sess);
4993 free(dsp, M_NFSCLDS);
4995 error = nd->nd_repstat;
4997 m_freem(nd->nd_mrep);
5002 * Do the NFSv4.1 Create Session.
5005 nfsrpc_createsession(struct nfsmount *nmp, struct nfsclsession *sep,
5006 struct nfssockreq *nrp, struct nfsclds *dsp, uint32_t sequenceid, int mds,
5007 struct ucred *cred, NFSPROC_T *p)
5009 uint32_t crflags, maxval, *tl;
5010 struct nfsrv_descript nfsd;
5011 struct nfsrv_descript *nd = &nfsd;
5012 int error, irdcnt, minorvers;
5014 /* Make sure nm_rsize, nm_wsize is set. */
5015 if (nmp->nm_rsize > NFS_MAXBSIZE || nmp->nm_rsize == 0)
5016 nmp->nm_rsize = NFS_MAXBSIZE;
5017 if (nmp->nm_wsize > NFS_MAXBSIZE || nmp->nm_wsize == 0)
5018 nmp->nm_wsize = NFS_MAXBSIZE;
5020 minorvers = nmp->nm_minorvers;
5021 else if ((dsp->nfsclds_flags & NFSCLDS_MINORV2) != 0)
5022 minorvers = NFSV42_MINORVERSION;
5024 minorvers = NFSV41_MINORVERSION;
5025 nfscl_reqstart(nd, NFSPROC_CREATESESSION, nmp, NULL, 0, NULL, NULL,
5026 NFS_VER4, minorvers, NULL);
5027 NFSM_BUILD(tl, uint32_t *, 4 * NFSX_UNSIGNED);
5028 *tl++ = sep->nfsess_clientid.lval[0];
5029 *tl++ = sep->nfsess_clientid.lval[1];
5030 *tl++ = txdr_unsigned(sequenceid);
5031 crflags = (NFSMNT_RDONLY(nmp->nm_mountp) ? 0 : NFSV4CRSESS_PERSIST);
5032 if (nfscl_enablecallb != 0 && nfs_numnfscbd > 0 && mds != 0)
5033 crflags |= NFSV4CRSESS_CONNBACKCHAN;
5034 *tl = txdr_unsigned(crflags);
5036 /* Fill in fore channel attributes. */
5037 NFSM_BUILD(tl, uint32_t *, 7 * NFSX_UNSIGNED);
5038 *tl++ = 0; /* Header pad size */
5039 if ((nd->nd_flag & ND_NFSV42) != 0 && mds != 0 && sb_max_adj >=
5040 nmp->nm_wsize && sb_max_adj >= nmp->nm_rsize) {
5042 * NFSv4.2 Extended Attribute operations may want to do
5043 * requests/replies that are larger than nm_rsize/nm_wsize.
5045 *tl++ = txdr_unsigned(sb_max_adj - NFS_MAXXDR);
5046 *tl++ = txdr_unsigned(sb_max_adj - NFS_MAXXDR);
5048 *tl++ = txdr_unsigned(nmp->nm_wsize + NFS_MAXXDR);
5049 *tl++ = txdr_unsigned(nmp->nm_rsize + NFS_MAXXDR);
5051 *tl++ = txdr_unsigned(4096); /* Max response size cached */
5052 *tl++ = txdr_unsigned(20); /* Max operations */
5053 *tl++ = txdr_unsigned(64); /* Max slots */
5054 *tl = 0; /* No rdma ird */
5056 /* Fill in back channel attributes. */
5057 NFSM_BUILD(tl, uint32_t *, 7 * NFSX_UNSIGNED);
5058 *tl++ = 0; /* Header pad size */
5059 *tl++ = txdr_unsigned(10000); /* Max request size */
5060 *tl++ = txdr_unsigned(10000); /* Max response size */
5061 *tl++ = txdr_unsigned(4096); /* Max response size cached */
5062 *tl++ = txdr_unsigned(4); /* Max operations */
5063 *tl++ = txdr_unsigned(NFSV4_CBSLOTS); /* Max slots */
5064 *tl = 0; /* No rdma ird */
5066 NFSM_BUILD(tl, uint32_t *, 8 * NFSX_UNSIGNED);
5067 *tl++ = txdr_unsigned(NFS_CALLBCKPROG); /* Call back prog # */
5069 /* Allow AUTH_SYS callbacks as uid, gid == 0. */
5070 *tl++ = txdr_unsigned(1); /* Auth_sys only */
5071 *tl++ = txdr_unsigned(AUTH_SYS); /* AUTH_SYS type */
5072 *tl++ = txdr_unsigned(nfsboottime.tv_sec); /* time stamp */
5073 *tl++ = 0; /* Null machine name */
5074 *tl++ = 0; /* Uid == 0 */
5075 *tl++ = 0; /* Gid == 0 */
5076 *tl = 0; /* No additional gids */
5077 nd->nd_flag |= ND_USEGSSNAME;
5078 error = newnfs_request(nd, nmp, NULL, nrp, NULL, p, cred, NFS_PROG,
5079 NFS_VER4, NULL, 1, NULL, NULL);
5082 if (nd->nd_repstat == 0) {
5083 NFSM_DISSECT(tl, uint32_t *, NFSX_V4SESSIONID +
5085 bcopy(tl, sep->nfsess_sessionid, NFSX_V4SESSIONID);
5086 tl += NFSX_V4SESSIONID / NFSX_UNSIGNED;
5087 sep->nfsess_sequenceid = fxdr_unsigned(uint32_t, *tl++);
5088 crflags = fxdr_unsigned(uint32_t, *tl);
5089 if ((crflags & NFSV4CRSESS_PERSIST) != 0 && mds != 0) {
5091 nmp->nm_state |= NFSSTA_SESSPERSIST;
5095 /* Get the fore channel slot count. */
5096 NFSM_DISSECT(tl, uint32_t *, 7 * NFSX_UNSIGNED);
5097 tl++; /* Skip the header pad size. */
5099 /* Make sure nm_wsize is small enough. */
5100 maxval = fxdr_unsigned(uint32_t, *tl++);
5101 while (maxval < nmp->nm_wsize + NFS_MAXXDR) {
5102 if (nmp->nm_wsize > 8096)
5107 sep->nfsess_maxreq = maxval;
5109 /* Make sure nm_rsize is small enough. */
5110 maxval = fxdr_unsigned(uint32_t, *tl++);
5111 while (maxval < nmp->nm_rsize + NFS_MAXXDR) {
5112 if (nmp->nm_rsize > 8096)
5117 sep->nfsess_maxresp = maxval;
5119 sep->nfsess_maxcache = fxdr_unsigned(int, *tl++);
5121 sep->nfsess_foreslots = fxdr_unsigned(uint16_t, *tl++);
5122 NFSCL_DEBUG(4, "fore slots=%d\n", (int)sep->nfsess_foreslots);
5123 irdcnt = fxdr_unsigned(int, *tl);
5124 if (irdcnt < 0 || irdcnt > 1) {
5125 error = NFSERR_BADXDR;
5129 NFSM_DISSECT(tl, uint32_t *, irdcnt * NFSX_UNSIGNED);
5131 /* and the back channel slot count. */
5132 NFSM_DISSECT(tl, uint32_t *, 7 * NFSX_UNSIGNED);
5134 sep->nfsess_backslots = fxdr_unsigned(uint16_t, *tl);
5135 NFSCL_DEBUG(4, "back slots=%d\n", (int)sep->nfsess_backslots);
5137 error = nd->nd_repstat;
5139 m_freem(nd->nd_mrep);
5144 * Do the NFSv4.1 Destroy Client.
5147 nfsrpc_destroyclient(struct nfsmount *nmp, struct nfsclclient *clp,
5148 struct ucred *cred, NFSPROC_T *p)
5151 struct nfsrv_descript nfsd;
5152 struct nfsrv_descript *nd = &nfsd;
5154 struct nfsclsession *tsep;
5156 nfscl_reqstart(nd, NFSPROC_DESTROYCLIENT, nmp, NULL, 0, NULL, NULL, 0,
5158 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_UNSIGNED);
5159 tsep = nfsmnt_mdssession(nmp);
5160 *tl++ = tsep->nfsess_clientid.lval[0];
5161 *tl = tsep->nfsess_clientid.lval[1];
5162 nd->nd_flag |= ND_USEGSSNAME;
5163 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
5164 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
5167 error = nd->nd_repstat;
5168 m_freem(nd->nd_mrep);
5173 * Do the NFSv4.1 LayoutGet.
5176 nfsrpc_layoutget(struct nfsmount *nmp, uint8_t *fhp, int fhlen, int iomode,
5177 uint64_t offset, uint64_t len, uint64_t minlen, int layouttype,
5178 int layoutlen, nfsv4stateid_t *stateidp, int *retonclosep,
5179 struct nfsclflayouthead *flhp, struct ucred *cred, NFSPROC_T *p,
5182 struct nfsrv_descript nfsd, *nd = &nfsd;
5185 nfscl_reqstart(nd, NFSPROC_LAYOUTGET, nmp, fhp, fhlen, NULL, NULL, 0,
5187 nfsrv_setuplayoutget(nd, iomode, offset, len, minlen, stateidp,
5188 layouttype, layoutlen, 0);
5189 nd->nd_flag |= ND_USEGSSNAME;
5190 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
5191 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
5192 NFSCL_DEBUG(4, "layget err=%d st=%d\n", error, nd->nd_repstat);
5195 if (nd->nd_repstat == 0)
5196 error = nfsrv_parselayoutget(nmp, nd, stateidp, retonclosep,
5198 if (error == 0 && nd->nd_repstat != 0)
5199 error = nd->nd_repstat;
5200 m_freem(nd->nd_mrep);
5205 * Do the NFSv4.1 Get Device Info.
5208 nfsrpc_getdeviceinfo(struct nfsmount *nmp, uint8_t *deviceid, int layouttype,
5209 uint32_t *notifybitsp, struct nfscldevinfo **ndip, struct ucred *cred,
5212 uint32_t cnt, *tl, vers, minorvers;
5213 struct nfsrv_descript nfsd;
5214 struct nfsrv_descript *nd = &nfsd;
5215 struct sockaddr_in sin, ssin;
5216 struct sockaddr_in6 sin6, ssin6;
5217 struct nfsclds *dsp = NULL, **dspp, **gotdspp;
5218 struct nfscldevinfo *ndi;
5219 int addrcnt = 0, bitcnt, error, gotminor, gotvers, i, isudp, j;
5221 uint8_t stripeindex;
5222 sa_family_t af, safilled;
5224 ssin.sin_port = 0; /* To shut up compiler. */
5225 ssin.sin_addr.s_addr = 0; /* ditto */
5229 nfscl_reqstart(nd, NFSPROC_GETDEVICEINFO, nmp, NULL, 0, NULL, NULL, 0,
5231 NFSM_BUILD(tl, uint32_t *, NFSX_V4DEVICEID + 3 * NFSX_UNSIGNED);
5232 NFSBCOPY(deviceid, tl, NFSX_V4DEVICEID);
5233 tl += (NFSX_V4DEVICEID / NFSX_UNSIGNED);
5234 *tl++ = txdr_unsigned(layouttype);
5235 *tl++ = txdr_unsigned(100000);
5236 if (notifybitsp != NULL && *notifybitsp != 0) {
5237 *tl = txdr_unsigned(1); /* One word of bits. */
5238 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
5239 *tl = txdr_unsigned(*notifybitsp);
5241 *tl = txdr_unsigned(0);
5242 nd->nd_flag |= ND_USEGSSNAME;
5243 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
5244 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
5247 if (nd->nd_repstat == 0) {
5248 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
5249 if (layouttype != fxdr_unsigned(int, *tl))
5250 printf("EEK! devinfo layout type not same!\n");
5251 if (layouttype == NFSLAYOUT_NFSV4_1_FILES) {
5252 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
5253 stripecnt = fxdr_unsigned(int, *tl);
5254 NFSCL_DEBUG(4, "stripecnt=%d\n", stripecnt);
5255 if (stripecnt < 1 || stripecnt > 4096) {
5256 printf("pNFS File layout devinfo stripecnt %d:"
5257 " out of range\n", stripecnt);
5258 error = NFSERR_BADXDR;
5261 NFSM_DISSECT(tl, uint32_t *, (stripecnt + 1) *
5263 addrcnt = fxdr_unsigned(int, *(tl + stripecnt));
5264 NFSCL_DEBUG(4, "addrcnt=%d\n", addrcnt);
5265 if (addrcnt < 1 || addrcnt > 128) {
5266 printf("NFS devinfo addrcnt %d: out of range\n",
5268 error = NFSERR_BADXDR;
5273 * Now we know how many stripe indices and addresses, so
5274 * we can allocate the structure the correct size.
5276 i = (stripecnt * sizeof(uint8_t)) /
5277 sizeof(struct nfsclds *) + 1;
5278 NFSCL_DEBUG(4, "stripeindices=%d\n", i);
5279 ndi = malloc(sizeof(*ndi) + (addrcnt + i) *
5280 sizeof(struct nfsclds *), M_NFSDEVINFO, M_WAITOK |
5282 NFSBCOPY(deviceid, ndi->nfsdi_deviceid,
5284 ndi->nfsdi_refcnt = 0;
5285 ndi->nfsdi_flags = NFSDI_FILELAYOUT;
5286 ndi->nfsdi_stripecnt = stripecnt;
5287 ndi->nfsdi_addrcnt = addrcnt;
5288 /* Fill in the stripe indices. */
5289 for (i = 0; i < stripecnt; i++) {
5290 stripeindex = fxdr_unsigned(uint8_t, *tl++);
5291 NFSCL_DEBUG(4, "stripeind=%d\n", stripeindex);
5292 if (stripeindex >= addrcnt) {
5293 printf("pNFS File Layout devinfo"
5294 " stripeindex %d: too big\n",
5296 error = NFSERR_BADXDR;
5299 nfsfldi_setstripeindex(ndi, i, stripeindex);
5301 } else if (layouttype == NFSLAYOUT_FLEXFILE) {
5302 /* For Flex File, we only get one address list. */
5303 ndi = malloc(sizeof(*ndi) + sizeof(struct nfsclds *),
5304 M_NFSDEVINFO, M_WAITOK | M_ZERO);
5305 NFSBCOPY(deviceid, ndi->nfsdi_deviceid,
5307 ndi->nfsdi_refcnt = 0;
5308 ndi->nfsdi_flags = NFSDI_FLEXFILE;
5309 addrcnt = ndi->nfsdi_addrcnt = 1;
5312 /* Now, dissect the server address(es). */
5313 safilled = AF_UNSPEC;
5314 for (i = 0; i < addrcnt; i++) {
5315 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
5316 cnt = fxdr_unsigned(uint32_t, *tl);
5318 printf("NFS devinfo 0 len addrlist\n");
5319 error = NFSERR_BADXDR;
5322 dspp = nfsfldi_addr(ndi, i);
5323 safilled = AF_UNSPEC;
5324 for (j = 0; j < cnt; j++) {
5325 error = nfsv4_getipaddr(nd, &sin, &sin6, &af,
5327 if (error != 0 && error != EPERM) {
5328 error = NFSERR_BADXDR;
5331 if (error == 0 && isudp == 0) {
5334 * - Same address family.
5335 * Save the address and dspp, so that
5336 * the connection can be done after
5337 * parsing is complete.
5339 if (safilled == AF_UNSPEC ||
5340 (af == nmp->nm_nam->sa_family &&
5341 safilled != nmp->nm_nam->sa_family)
5354 gotvers = NFS_VER4; /* Default NFSv4.1 for File Layout. */
5355 gotminor = NFSV41_MINORVERSION;
5356 /* For Flex File, we will take one of the versions to use. */
5357 if (layouttype == NFSLAYOUT_FLEXFILE) {
5358 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
5359 j = fxdr_unsigned(int, *tl);
5360 if (j < 1 || j > NFSDEV_MAXVERS) {
5361 printf("pNFS: too many versions\n");
5362 error = NFSERR_BADXDR;
5367 for (i = 0; i < j; i++) {
5368 NFSM_DISSECT(tl, uint32_t *, 5 * NFSX_UNSIGNED);
5369 vers = fxdr_unsigned(uint32_t, *tl++);
5370 minorvers = fxdr_unsigned(uint32_t, *tl++);
5371 if (vers == NFS_VER3)
5373 if ((vers == NFS_VER4 && ((minorvers ==
5374 NFSV41_MINORVERSION && gotminor == 0) ||
5375 minorvers == NFSV42_MINORVERSION)) ||
5376 (vers == NFS_VER3 && gotvers == 0)) {
5378 gotminor = minorvers;
5379 /* We'll take this one. */
5380 ndi->nfsdi_versindex = i;
5381 ndi->nfsdi_vers = vers;
5382 ndi->nfsdi_minorvers = minorvers;
5383 ndi->nfsdi_rsize = fxdr_unsigned(
5385 ndi->nfsdi_wsize = fxdr_unsigned(
5387 if (*tl == newnfs_true)
5392 ~NFSDI_TIGHTCOUPLED;
5396 printf("pNFS: no NFSv3, NFSv4.1 or NFSv4.2\n");
5397 error = NFSERR_BADXDR;
5402 /* And the notify bits. */
5403 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
5404 bitcnt = fxdr_unsigned(int, *tl);
5406 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
5407 if (notifybitsp != NULL)
5409 fxdr_unsigned(uint32_t, *tl);
5411 if (safilled != AF_UNSPEC) {
5412 KASSERT(ndi != NULL, ("ndi is NULL"));
5418 * Now we can do a TCP connection for the correct
5419 * NFS version and IP address.
5421 error = nfsrpc_fillsa(nmp, &ssin, &ssin6, safilled,
5422 gotvers, gotminor, &dsp, p);
5425 KASSERT(gotdspp != NULL, ("gotdspp is NULL"));
5429 if (nd->nd_repstat != 0 && error == 0)
5430 error = nd->nd_repstat;
5432 if (error != 0 && ndi != NULL)
5433 nfscl_freedevinfo(ndi);
5434 m_freem(nd->nd_mrep);
5439 * Do the NFSv4.1 LayoutCommit.
5442 nfsrpc_layoutcommit(struct nfsmount *nmp, uint8_t *fh, int fhlen, int reclaim,
5443 uint64_t off, uint64_t len, uint64_t lastbyte, nfsv4stateid_t *stateidp,
5444 int layouttype, struct ucred *cred, NFSPROC_T *p, void *stuff)
5447 struct nfsrv_descript nfsd, *nd = &nfsd;
5450 nfscl_reqstart(nd, NFSPROC_LAYOUTCOMMIT, nmp, fh, fhlen, NULL, NULL,
5452 NFSM_BUILD(tl, uint32_t *, 5 * NFSX_UNSIGNED + 3 * NFSX_HYPER +
5454 txdr_hyper(off, tl);
5456 txdr_hyper(len, tl);
5459 *tl++ = newnfs_true;
5461 *tl++ = newnfs_false;
5462 *tl++ = txdr_unsigned(stateidp->seqid);
5463 *tl++ = stateidp->other[0];
5464 *tl++ = stateidp->other[1];
5465 *tl++ = stateidp->other[2];
5466 *tl++ = newnfs_true;
5469 else if (lastbyte >= (off + len))
5470 lastbyte = off + len - 1;
5471 txdr_hyper(lastbyte, tl);
5473 *tl++ = newnfs_false;
5474 *tl++ = txdr_unsigned(layouttype);
5475 /* All supported layouts are 0 length. */
5476 *tl = txdr_unsigned(0);
5477 nd->nd_flag |= ND_USEGSSNAME;
5478 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
5479 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
5482 error = nd->nd_repstat;
5483 m_freem(nd->nd_mrep);
5488 * Do the NFSv4.1 LayoutReturn.
5491 nfsrpc_layoutreturn(struct nfsmount *nmp, uint8_t *fh, int fhlen, int reclaim,
5492 int layouttype, uint32_t iomode, int layoutreturn, uint64_t offset,
5493 uint64_t len, nfsv4stateid_t *stateidp, struct ucred *cred, NFSPROC_T *p,
5494 uint32_t stat, uint32_t op, char *devid)
5497 struct nfsrv_descript nfsd, *nd = &nfsd;
5501 nfscl_reqstart(nd, NFSPROC_LAYOUTRETURN, nmp, fh, fhlen, NULL, NULL,
5503 NFSM_BUILD(tl, uint32_t *, 4 * NFSX_UNSIGNED);
5505 *tl++ = newnfs_true;
5507 *tl++ = newnfs_false;
5508 *tl++ = txdr_unsigned(layouttype);
5509 *tl++ = txdr_unsigned(iomode);
5510 *tl = txdr_unsigned(layoutreturn);
5511 if (layoutreturn == NFSLAYOUTRETURN_FILE) {
5512 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_HYPER + NFSX_STATEID +
5514 txdr_hyper(offset, tl);
5516 txdr_hyper(len, tl);
5518 NFSCL_DEBUG(4, "layoutret stseq=%d\n", (int)stateidp->seqid);
5519 *tl++ = txdr_unsigned(stateidp->seqid);
5520 *tl++ = stateidp->other[0];
5521 *tl++ = stateidp->other[1];
5522 *tl++ = stateidp->other[2];
5523 if (layouttype == NFSLAYOUT_NFSV4_1_FILES)
5524 *tl = txdr_unsigned(0);
5525 else if (layouttype == NFSLAYOUT_FLEXFILE) {
5527 *tl = txdr_unsigned(2 * NFSX_HYPER +
5528 NFSX_STATEID + NFSX_V4DEVICEID + 5 *
5530 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_HYPER +
5531 NFSX_STATEID + NFSX_V4DEVICEID + 5 *
5533 *tl++ = txdr_unsigned(1); /* One error. */
5534 tu64 = 0; /* Offset. */
5535 txdr_hyper(tu64, tl); tl += 2;
5536 tu64 = UINT64_MAX; /* Length. */
5537 txdr_hyper(tu64, tl); tl += 2;
5538 NFSBCOPY(stateidp, tl, NFSX_STATEID);
5539 tl += (NFSX_STATEID / NFSX_UNSIGNED);
5540 *tl++ = txdr_unsigned(1); /* One error. */
5541 NFSBCOPY(devid, tl, NFSX_V4DEVICEID);
5542 tl += (NFSX_V4DEVICEID / NFSX_UNSIGNED);
5543 *tl++ = txdr_unsigned(stat);
5544 *tl++ = txdr_unsigned(op);
5546 *tl = txdr_unsigned(2 * NFSX_UNSIGNED);
5547 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_UNSIGNED);
5551 *tl = 0; /* No stats yet. */
5554 nd->nd_flag |= ND_USEGSSNAME;
5555 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
5556 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
5559 if (nd->nd_repstat == 0) {
5560 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
5562 NFSM_DISSECT(tl, uint32_t *, NFSX_STATEID);
5563 stateidp->seqid = fxdr_unsigned(uint32_t, *tl++);
5564 stateidp->other[0] = *tl++;
5565 stateidp->other[1] = *tl++;
5566 stateidp->other[2] = *tl;
5569 error = nd->nd_repstat;
5571 m_freem(nd->nd_mrep);
5576 * Do the NFSv4.2 LayoutError.
5579 nfsrpc_layouterror(struct nfsmount *nmp, uint8_t *fh, int fhlen, uint64_t offset,
5580 uint64_t len, nfsv4stateid_t *stateidp, struct ucred *cred, NFSPROC_T *p,
5581 uint32_t stat, uint32_t op, char *devid)
5584 struct nfsrv_descript nfsd, *nd = &nfsd;
5587 nfscl_reqstart(nd, NFSPROC_LAYOUTERROR, nmp, fh, fhlen, NULL, NULL,
5589 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_HYPER + NFSX_STATEID +
5590 NFSX_V4DEVICEID + 3 * NFSX_UNSIGNED);
5591 txdr_hyper(offset, tl); tl += 2;
5592 txdr_hyper(len, tl); tl += 2;
5593 *tl++ = txdr_unsigned(stateidp->seqid);
5594 *tl++ = stateidp->other[0];
5595 *tl++ = stateidp->other[1];
5596 *tl++ = stateidp->other[2];
5597 *tl++ = txdr_unsigned(1);
5598 NFSBCOPY(devid, tl, NFSX_V4DEVICEID);
5599 tl += (NFSX_V4DEVICEID / NFSX_UNSIGNED);
5600 *tl++ = txdr_unsigned(stat);
5601 *tl = txdr_unsigned(op);
5602 nd->nd_flag |= ND_USEGSSNAME;
5603 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
5604 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
5607 if (nd->nd_repstat != 0)
5608 error = nd->nd_repstat;
5609 m_freem(nd->nd_mrep);
5614 * Acquire a layout and devinfo, if possible. The caller must have acquired
5615 * a reference count on the nfsclclient structure before calling this.
5616 * Return the layout in lypp with a reference count on it, if successful.
5619 nfsrpc_getlayout(struct nfsmount *nmp, vnode_t vp, struct nfsfh *nfhp,
5620 int iomode, uint32_t rw, uint32_t *notifybitsp, nfsv4stateid_t *stateidp,
5621 uint64_t off, struct nfscllayout **lypp, struct ucred *cred, NFSPROC_T *p)
5623 struct nfscllayout *lyp;
5624 struct nfsclflayout *flp;
5625 struct nfsclflayouthead flh;
5626 int error = 0, islocked, layoutlen, layouttype, recalled, retonclose;
5627 nfsv4stateid_t stateid;
5628 struct nfsclsession *tsep;
5631 if (NFSHASFLEXFILE(nmp))
5632 layouttype = NFSLAYOUT_FLEXFILE;
5634 layouttype = NFSLAYOUT_NFSV4_1_FILES;
5636 * If lyp is returned non-NULL, there will be a refcnt (shared lock)
5637 * on it, iff flp != NULL or a lock (exclusive lock) on it iff
5640 lyp = nfscl_getlayout(nmp->nm_clp, nfhp->nfh_fh, nfhp->nfh_len,
5641 off, rw, &flp, &recalled);
5643 if (lyp == NULL || flp == NULL) {
5647 tsep = nfsmnt_mdssession(nmp);
5648 layoutlen = tsep->nfsess_maxcache -
5649 (NFSX_STATEID + 3 * NFSX_UNSIGNED);
5652 stateid.other[0] = stateidp->other[0];
5653 stateid.other[1] = stateidp->other[1];
5654 stateid.other[2] = stateidp->other[2];
5655 error = nfsrpc_layoutget(nmp, nfhp->nfh_fh,
5656 nfhp->nfh_len, iomode, (uint64_t)0, UINT64_MAX,
5657 (uint64_t)0, layouttype, layoutlen, &stateid,
5658 &retonclose, &flh, cred, p, NULL);
5661 stateid.seqid = lyp->nfsly_stateid.seqid;
5662 stateid.other[0] = lyp->nfsly_stateid.other[0];
5663 stateid.other[1] = lyp->nfsly_stateid.other[1];
5664 stateid.other[2] = lyp->nfsly_stateid.other[2];
5665 error = nfsrpc_layoutget(nmp, nfhp->nfh_fh,
5666 nfhp->nfh_len, iomode, off, UINT64_MAX,
5667 (uint64_t)0, layouttype, layoutlen, &stateid,
5668 &retonclose, &flh, cred, p, NULL);
5670 error = nfsrpc_layoutgetres(nmp, vp, nfhp->nfh_fh,
5671 nfhp->nfh_len, &stateid, retonclose, notifybitsp, &lyp,
5672 &flh, layouttype, error, NULL, cred, p);
5675 else if (islocked != 0)
5676 nfscl_rellayout(lyp, 1);
5683 * Do a TCP connection plus exchange id and create session.
5684 * If successful, a "struct nfsclds" is linked into the list for the
5685 * mount point and a pointer to it is returned.
5688 nfsrpc_fillsa(struct nfsmount *nmp, struct sockaddr_in *sin,
5689 struct sockaddr_in6 *sin6, sa_family_t af, int vers, int minorvers,
5690 struct nfsclds **dspp, NFSPROC_T *p)
5692 struct sockaddr_in *msad, *sad;
5693 struct sockaddr_in6 *msad6, *sad6;
5694 struct nfsclclient *clp;
5695 struct nfssockreq *nrp;
5696 struct nfsclds *dsp, *tdsp;
5697 int error, firsttry;
5698 enum nfsclds_state retv;
5699 uint32_t sequenceid = 0;
5701 KASSERT(nmp->nm_sockreq.nr_cred != NULL,
5702 ("nfsrpc_fillsa: NULL nr_cred"));
5708 if (af == AF_INET) {
5711 * Check to see if we already have a session for this
5712 * address that is usable for a DS.
5713 * Note that the MDS's address is in a different place
5714 * than the sessions already acquired for DS's.
5716 msad = (struct sockaddr_in *)nmp->nm_sockreq.nr_nam;
5717 tdsp = TAILQ_FIRST(&nmp->nm_sess);
5718 while (tdsp != NULL) {
5719 if (msad != NULL && msad->sin_family == AF_INET &&
5720 sin->sin_addr.s_addr == msad->sin_addr.s_addr &&
5721 sin->sin_port == msad->sin_port &&
5722 (tdsp->nfsclds_flags & NFSCLDS_DS) != 0 &&
5723 tdsp->nfsclds_sess.nfsess_defunct == 0) {
5726 NFSCL_DEBUG(4, "fnd same addr\n");
5729 tdsp = TAILQ_NEXT(tdsp, nfsclds_list);
5730 if (tdsp != NULL && tdsp->nfsclds_sockp != NULL)
5731 msad = (struct sockaddr_in *)
5732 tdsp->nfsclds_sockp->nr_nam;
5738 /* No IP address match, so look for new/trunked one. */
5739 sad = malloc(sizeof(*sad), M_SONAME, M_WAITOK | M_ZERO);
5740 sad->sin_len = sizeof(*sad);
5741 sad->sin_family = AF_INET;
5742 sad->sin_port = sin->sin_port;
5743 sad->sin_addr.s_addr = sin->sin_addr.s_addr;
5744 nrp = malloc(sizeof(*nrp), M_NFSSOCKREQ, M_WAITOK | M_ZERO);
5745 nrp->nr_nam = (struct sockaddr *)sad;
5746 } else if (af == AF_INET6) {
5749 * Check to see if we already have a session for this
5750 * address that is usable for a DS.
5751 * Note that the MDS's address is in a different place
5752 * than the sessions already acquired for DS's.
5754 msad6 = (struct sockaddr_in6 *)nmp->nm_sockreq.nr_nam;
5755 tdsp = TAILQ_FIRST(&nmp->nm_sess);
5756 while (tdsp != NULL) {
5757 if (msad6 != NULL && msad6->sin6_family == AF_INET6 &&
5758 IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr,
5759 &msad6->sin6_addr) &&
5760 sin6->sin6_port == msad6->sin6_port &&
5761 (tdsp->nfsclds_flags & NFSCLDS_DS) != 0 &&
5762 tdsp->nfsclds_sess.nfsess_defunct == 0) {
5767 tdsp = TAILQ_NEXT(tdsp, nfsclds_list);
5768 if (tdsp != NULL && tdsp->nfsclds_sockp != NULL)
5769 msad6 = (struct sockaddr_in6 *)
5770 tdsp->nfsclds_sockp->nr_nam;
5776 /* No IP address match, so look for new/trunked one. */
5777 sad6 = malloc(sizeof(*sad6), M_SONAME, M_WAITOK | M_ZERO);
5778 sad6->sin6_len = sizeof(*sad6);
5779 sad6->sin6_family = AF_INET6;
5780 sad6->sin6_port = sin6->sin6_port;
5781 NFSBCOPY(&sin6->sin6_addr, &sad6->sin6_addr,
5782 sizeof(struct in6_addr));
5783 nrp = malloc(sizeof(*nrp), M_NFSSOCKREQ, M_WAITOK | M_ZERO);
5784 nrp->nr_nam = (struct sockaddr *)sad6;
5788 nrp->nr_sotype = SOCK_STREAM;
5789 mtx_init(&nrp->nr_mtx, "nfssock", NULL, MTX_DEF);
5790 nrp->nr_prog = NFS_PROG;
5791 nrp->nr_vers = vers;
5794 * Use the credentials that were used for the mount, which are
5795 * in nmp->nm_sockreq.nr_cred for newnfs_connect() etc.
5796 * Ref. counting the credentials with crhold() is probably not
5797 * necessary, since nm_sockreq.nr_cred won't be crfree()'d until
5798 * unmount, but I did it anyhow.
5800 nrp->nr_cred = crhold(nmp->nm_sockreq.nr_cred);
5801 error = newnfs_connect(nmp, nrp, NULL, p, 0, false, &nrp->nr_client);
5802 NFSCL_DEBUG(3, "DS connect=%d\n", error);
5805 /* Now, do the exchangeid and create session. */
5807 if (vers == NFS_VER4) {
5810 error = nfsrpc_exchangeid(nmp, clp, nrp,
5811 minorvers, NFSV4EXCH_USEPNFSDS, &dsp,
5813 NFSCL_DEBUG(3, "DS exchangeid=%d\n", error);
5814 if (error == NFSERR_MINORVERMISMATCH)
5815 minorvers = NFSV42_MINORVERSION;
5816 } while (error == NFSERR_MINORVERMISMATCH &&
5819 newnfs_disconnect(NULL, nrp);
5821 dsp = malloc(sizeof(struct nfsclds), M_NFSCLDS,
5823 dsp->nfsclds_flags |= NFSCLDS_DS;
5824 dsp->nfsclds_expire = INT32_MAX; /* No renews needed. */
5825 mtx_init(&dsp->nfsclds_mtx, "nfsds", NULL, MTX_DEF);
5826 mtx_init(&dsp->nfsclds_sess.nfsess_mtx, "nfssession",
5831 dsp->nfsclds_sockp = nrp;
5832 if (vers == NFS_VER4) {
5834 retv = nfscl_getsameserver(nmp, dsp, &tdsp,
5836 NFSCL_DEBUG(3, "getsame ret=%d\n", retv);
5837 if (retv == NFSDSP_USETHISSESSION &&
5838 nfscl_dssameconn != 0) {
5840 tdsp->nfsclds_flags |= NFSCLDS_SAMECONN;
5844 * If there is already a session for this
5847 newnfs_disconnect(NULL, nrp);
5848 nfscl_freenfsclds(dsp);
5852 if (retv == NFSDSP_NOTFOUND)
5854 dsp->nfsclds_sess.nfsess_sequenceid;
5856 error = nfsrpc_createsession(nmp, &dsp->nfsclds_sess,
5857 nrp, dsp, sequenceid, 0, nrp->nr_cred, p);
5858 NFSCL_DEBUG(3, "DS createsess=%d\n", error);
5861 NFSFREECRED(nrp->nr_cred);
5862 NFSFREEMUTEX(&nrp->nr_mtx);
5863 free(nrp->nr_nam, M_SONAME);
5864 free(nrp, M_NFSSOCKREQ);
5867 NFSCL_DEBUG(3, "add DS session\n");
5869 * Put it at the end of the list. That way the list
5870 * is ordered by when the entry was added. This matters
5871 * since the one done first is the one that should be
5872 * used for sequencid'ing any subsequent create sessions.
5875 TAILQ_INSERT_TAIL(&nmp->nm_sess, dsp, nfsclds_list);
5878 } else if (dsp != NULL) {
5879 newnfs_disconnect(NULL, nrp);
5880 nfscl_freenfsclds(dsp);
5886 * Do the NFSv4.1 Reclaim Complete.
5889 nfsrpc_reclaimcomplete(struct nfsmount *nmp, struct ucred *cred, NFSPROC_T *p)
5892 struct nfsrv_descript nfsd;
5893 struct nfsrv_descript *nd = &nfsd;
5896 nfscl_reqstart(nd, NFSPROC_RECLAIMCOMPL, nmp, NULL, 0, NULL, NULL, 0,
5898 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
5900 nd->nd_flag |= ND_USEGSSNAME;
5901 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
5902 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
5905 error = nd->nd_repstat;
5906 m_freem(nd->nd_mrep);
5911 * Initialize the slot tables for a session.
5914 nfscl_initsessionslots(struct nfsclsession *sep)
5918 for (i = 0; i < NFSV4_CBSLOTS; i++) {
5919 if (sep->nfsess_cbslots[i].nfssl_reply != NULL)
5920 m_freem(sep->nfsess_cbslots[i].nfssl_reply);
5921 NFSBZERO(&sep->nfsess_cbslots[i], sizeof(struct nfsslot));
5923 for (i = 0; i < 64; i++)
5924 sep->nfsess_slotseq[i] = 0;
5925 sep->nfsess_slots = 0;
5926 sep->nfsess_badslots = 0;
5930 * Called to try and do an I/O operation via an NFSv4.1 Data Server (DS).
5933 nfscl_doiods(vnode_t vp, struct uio *uiop, int *iomode, int *must_commit,
5934 uint32_t rwaccess, int docommit, struct ucred *cred, NFSPROC_T *p)
5936 struct nfsnode *np = VTONFS(vp);
5937 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
5938 struct nfscllayout *layp;
5939 struct nfscldevinfo *dip;
5940 struct nfsclflayout *rflp;
5941 struct mbuf *m, *m2;
5942 struct nfsclwritedsdorpc *drpc, *tdrpc;
5943 nfsv4stateid_t stateid;
5944 struct ucred *newcred;
5945 uint64_t lastbyte, len, off, oresid, xfer;
5946 int eof, error, firstmirror, i, iolaymode, mirrorcnt, recalled, timo;
5949 void *iovbase = NULL;
5955 if (!NFSHASPNFS(nmp) || nfscl_enablecallb == 0 || nfs_numnfscbd == 0 ||
5956 (np->n_flag & NNOLAYOUT) != 0)
5958 /* Now, get a reference cnt on the clientid for this mount. */
5959 if (nfscl_getref(nmp) == 0)
5962 /* Find an appropriate stateid. */
5963 newcred = NFSNEWCRED(cred);
5964 error = nfscl_getstateid(vp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len,
5965 rwaccess, 1, newcred, p, &stateid, &lckp);
5967 NFSFREECRED(newcred);
5971 /* Search for a layout for this file. */
5972 off = uiop->uio_offset;
5973 layp = nfscl_getlayout(nmp->nm_clp, np->n_fhp->nfh_fh,
5974 np->n_fhp->nfh_len, off, rwaccess, &rflp, &recalled);
5975 if (layp == NULL || rflp == NULL) {
5976 if (recalled != 0) {
5977 NFSFREECRED(newcred);
5979 nfscl_lockderef(lckp);
5984 nfscl_rellayout(layp, (rflp == NULL) ? 1 : 0);
5987 /* Try and get a Layout, if it is supported. */
5988 if (rwaccess == NFSV4OPEN_ACCESSWRITE ||
5989 (np->n_flag & NWRITEOPENED) != 0)
5990 iolaymode = NFSLAYOUTIOMODE_RW;
5992 iolaymode = NFSLAYOUTIOMODE_READ;
5993 error = nfsrpc_getlayout(nmp, vp, np->n_fhp, iolaymode,
5994 rwaccess, NULL, &stateid, off, &layp, newcred, p);
5997 np->n_flag |= NNOLAYOUT;
6000 nfscl_lockderef(lckp);
6001 NFSFREECRED(newcred);
6003 nfscl_rellayout(layp, 0);
6010 * Loop around finding a layout that works for the first part of
6011 * this I/O operation, and then call the function that actually
6015 len = (uint64_t)uiop->uio_resid;
6016 while (len > 0 && error == 0 && eof == 0) {
6017 off = uiop->uio_offset;
6018 error = nfscl_findlayoutforio(layp, off, rwaccess, &rflp);
6020 oresid = xfer = (uint64_t)uiop->uio_resid;
6021 if (xfer > (rflp->nfsfl_end - rflp->nfsfl_off))
6022 xfer = rflp->nfsfl_end - rflp->nfsfl_off;
6024 * For Flex File layout with mirrored DSs, select one
6025 * of them at random for reads. For writes and commits,
6029 tdrpc = drpc = NULL;
6032 if ((layp->nfsly_flags & NFSLY_FLEXFILE) != 0 &&
6033 (mirrorcnt = rflp->nfsfl_mirrorcnt) > 1) {
6034 if (rwaccess == NFSV4OPEN_ACCESSREAD) {
6035 firstmirror = arc4random() % mirrorcnt;
6036 mirrorcnt = firstmirror + 1;
6038 if (docommit == 0) {
6040 * Save values, so uiop can be
6041 * rolled back upon a write
6044 offs = uiop->uio_offset;
6045 resid = uiop->uio_resid;
6047 uiop->uio_iov->iov_base;
6048 iovlen = uiop->uio_iov->iov_len;
6049 m = nfsm_uiombuflist(uiop, len,
6056 tdrpc = drpc = malloc(sizeof(*drpc) *
6057 (mirrorcnt - 1), M_TEMP, M_WAITOK |
6061 for (i = firstmirror; i < mirrorcnt && error == 0; i++){
6063 if (m != NULL && i < mirrorcnt - 1)
6064 m2 = m_copym(m, 0, M_COPYALL, M_WAITOK);
6069 if ((layp->nfsly_flags & NFSLY_FLEXFILE) != 0) {
6070 dev = rflp->nfsfl_ffm[i].dev;
6071 dip = nfscl_getdevinfo(nmp->nm_clp, dev,
6072 rflp->nfsfl_ffm[i].devp);
6074 dev = rflp->nfsfl_dev;
6075 dip = nfscl_getdevinfo(nmp->nm_clp, dev,
6079 if ((rflp->nfsfl_flags & NFSFL_FLEXFILE)
6081 error = nfscl_dofflayoutio(vp,
6082 uiop, iomode, must_commit,
6083 &eof, &stateid, rwaccess,
6084 dip, layp, rflp, off, xfer,
6085 i, docommit, m2, tdrpc,
6088 error = nfscl_doflayoutio(vp,
6089 uiop, iomode, must_commit,
6090 &eof, &stateid, rwaccess,
6091 dip, layp, rflp, off, xfer,
6092 docommit, newcred, p);
6093 nfscl_reldevinfo(dip);
6104 timo = hz / 50; /* Wait for 20msec. */
6107 for (i = firstmirror; i < mirrorcnt - 1 &&
6108 tdrpc != NULL; i++, tdrpc++) {
6110 * For the unused drpc entries, both inprog and
6111 * err == 0, so this loop won't break.
6113 while (tdrpc->inprog != 0 && tdrpc->done == 0)
6114 tsleep(&tdrpc->tsk, PVFS, "clrpcio",
6116 if (error == 0 && tdrpc->err != 0)
6118 if (rwaccess != NFSV4OPEN_ACCESSREAD &&
6119 docommit == 0 && *must_commit == 0 &&
6120 tdrpc->must_commit == 1)
6125 if (mirrorcnt > 1 && rwaccess ==
6126 NFSV4OPEN_ACCESSWRITE && docommit == 0) {
6128 layp->nfsly_flags |= NFSLY_WRITTEN;
6131 lastbyte = off + xfer - 1;
6133 if (lastbyte > layp->nfsly_lastbyte)
6134 layp->nfsly_lastbyte = lastbyte;
6136 } else if (error == NFSERR_OPENMODE &&
6137 rwaccess == NFSV4OPEN_ACCESSREAD) {
6139 nmp->nm_state |= NFSSTA_OPENMODE;
6141 } else if ((error == NFSERR_NOSPC ||
6142 error == NFSERR_IO || error == NFSERR_NXIO) &&
6143 nmp->nm_minorvers == NFSV42_MINORVERSION) {
6145 op = NFSV4OP_COMMIT;
6146 else if (rwaccess == NFSV4OPEN_ACCESSREAD)
6150 nfsrpc_layouterror(nmp, np->n_fhp->nfh_fh,
6151 np->n_fhp->nfh_len, off, xfer,
6152 &layp->nfsly_stateid, newcred, p, error, op,
6153 dip->nfsdi_deviceid);
6158 len -= (oresid - (uint64_t)uiop->uio_resid);
6159 else if (mirrorcnt > 1 && rwaccess ==
6160 NFSV4OPEN_ACCESSWRITE && docommit == 0) {
6162 * In case the rpc gets retried, roll the
6163 * uio fields changed by nfsm_uiombuflist()
6166 uiop->uio_offset = offs;
6167 uiop->uio_resid = resid;
6168 uiop->uio_iov->iov_base = iovbase;
6169 uiop->uio_iov->iov_len = iovlen;
6174 nfscl_lockderef(lckp);
6175 NFSFREECRED(newcred);
6176 nfscl_rellayout(layp, 0);
6182 * Find a file layout that will handle the first bytes of the requested
6183 * range and return the information from it needed to the I/O operation.
6186 nfscl_findlayoutforio(struct nfscllayout *lyp, uint64_t off, uint32_t rwaccess,
6187 struct nfsclflayout **retflpp)
6189 struct nfsclflayout *flp, *nflp, *rflp;
6194 /* For reading, do the Read list first and then the Write list. */
6196 if (rw == NFSV4OPEN_ACCESSREAD)
6197 flp = LIST_FIRST(&lyp->nfsly_flayread);
6199 flp = LIST_FIRST(&lyp->nfsly_flayrw);
6200 while (flp != NULL) {
6201 nflp = LIST_NEXT(flp, nfsfl_list);
6202 if (flp->nfsfl_off > off)
6204 if (flp->nfsfl_end > off &&
6205 (rflp == NULL || rflp->nfsfl_end < flp->nfsfl_end))
6209 if (rw == NFSV4OPEN_ACCESSREAD)
6210 rw = NFSV4OPEN_ACCESSWRITE;
6215 /* This one covers the most bytes starting at off. */
6223 * Do I/O using an NFSv4.1 or NFSv4.2 file layout.
6226 nfscl_doflayoutio(vnode_t vp, struct uio *uiop, int *iomode, int *must_commit,
6227 int *eofp, nfsv4stateid_t *stateidp, int rwflag, struct nfscldevinfo *dp,
6228 struct nfscllayout *lyp, struct nfsclflayout *flp, uint64_t off,
6229 uint64_t len, int docommit, struct ucred *cred, NFSPROC_T *p)
6231 uint64_t io_off, rel_off, stripe_unit_size, transfer, xfer;
6232 int commit_thru_mds, error, stripe_index, stripe_pos, minorvers;
6235 struct nfsclds **dspp;
6238 rel_off = off - flp->nfsfl_patoff;
6239 stripe_unit_size = flp->nfsfl_util & NFSFLAYUTIL_STRIPE_MASK;
6240 stripe_pos = (rel_off / stripe_unit_size + flp->nfsfl_stripe1) %
6241 dp->nfsdi_stripecnt;
6242 transfer = stripe_unit_size - (rel_off % stripe_unit_size);
6245 /* Loop around, doing I/O for each stripe unit. */
6246 while (len > 0 && error == 0) {
6247 stripe_index = nfsfldi_stripeindex(dp, stripe_pos);
6248 dspp = nfsfldi_addr(dp, stripe_index);
6249 if (((*dspp)->nfsclds_flags & NFSCLDS_MINORV2) != 0)
6250 minorvers = NFSV42_MINORVERSION;
6252 minorvers = NFSV41_MINORVERSION;
6253 if (len > transfer && docommit == 0)
6257 if ((flp->nfsfl_util & NFSFLAYUTIL_DENSE) != 0) {
6259 if (stripe_pos >= flp->nfsfl_fhcnt)
6261 fhp = flp->nfsfl_fh[stripe_pos];
6262 io_off = (rel_off / (stripe_unit_size *
6263 dp->nfsdi_stripecnt)) * stripe_unit_size +
6264 rel_off % stripe_unit_size;
6266 /* Sparse layout. */
6267 if (flp->nfsfl_fhcnt > 1) {
6268 if (stripe_index >= flp->nfsfl_fhcnt)
6270 fhp = flp->nfsfl_fh[stripe_index];
6271 } else if (flp->nfsfl_fhcnt == 1)
6272 fhp = flp->nfsfl_fh[0];
6277 if ((flp->nfsfl_util & NFSFLAYUTIL_COMMIT_THRU_MDS) != 0) {
6278 commit_thru_mds = 1;
6282 commit_thru_mds = 0;
6284 np->n_flag |= NDSCOMMIT;
6287 if (docommit != 0) {
6289 error = nfsrpc_commitds(vp, io_off, xfer,
6290 *dspp, fhp, NFS_VER4, minorvers, cred, p);
6293 * Set both eof and uio_resid = 0 to end any
6297 uiop->uio_resid = 0;
6300 np->n_flag &= ~NDSCOMMIT;
6303 } else if (rwflag == NFSV4OPEN_ACCESSREAD)
6304 error = nfsrpc_readds(vp, uiop, stateidp, eofp, *dspp,
6305 io_off, xfer, fhp, 0, NFS_VER4, minorvers, cred, p);
6307 error = nfsrpc_writeds(vp, uiop, iomode, must_commit,
6308 stateidp, *dspp, io_off, xfer, fhp, commit_thru_mds,
6309 0, NFS_VER4, minorvers, cred, p);
6312 lyp->nfsly_flags |= NFSLY_WRITTEN;
6317 transfer = stripe_unit_size;
6318 stripe_pos = (stripe_pos + 1) % dp->nfsdi_stripecnt;
6327 * Do I/O using an NFSv4.1 flex file layout.
6330 nfscl_dofflayoutio(vnode_t vp, struct uio *uiop, int *iomode, int *must_commit,
6331 int *eofp, nfsv4stateid_t *stateidp, int rwflag, struct nfscldevinfo *dp,
6332 struct nfscllayout *lyp, struct nfsclflayout *flp, uint64_t off,
6333 uint64_t len, int mirror, int docommit, struct mbuf *mp,
6334 struct nfsclwritedsdorpc *drpc, struct ucred *cred, NFSPROC_T *p)
6340 struct nfsclds **dspp;
6341 struct ucred *tcred;
6342 struct mbuf *m, *m2;
6347 NFSCL_DEBUG(4, "nfscl_dofflayoutio: off=%ju len=%ju\n", (uintmax_t)off,
6349 /* Loop around, doing I/O for each stripe unit. */
6350 while (len > 0 && error == 0) {
6351 dspp = nfsfldi_addr(dp, 0);
6352 fhp = flp->nfsfl_ffm[mirror].fh[dp->nfsdi_versindex];
6353 stateidp = &flp->nfsfl_ffm[mirror].st;
6354 NFSCL_DEBUG(4, "mirror=%d vind=%d fhlen=%d st.seqid=0x%x\n",
6355 mirror, dp->nfsdi_versindex, fhp->nfh_len, stateidp->seqid);
6356 if ((dp->nfsdi_flags & NFSDI_TIGHTCOUPLED) == 0) {
6357 tcred = NFSNEWCRED(cred);
6358 tcred->cr_uid = flp->nfsfl_ffm[mirror].user;
6359 tcred->cr_groups[0] = flp->nfsfl_ffm[mirror].group;
6360 tcred->cr_ngroups = 1;
6363 if (rwflag == NFSV4OPEN_ACCESSREAD)
6364 copylen = dp->nfsdi_rsize;
6366 copylen = dp->nfsdi_wsize;
6367 if (len > copylen && mp != NULL) {
6369 * When a mirrored configuration needs to do
6370 * multiple writes to each mirror, all writes
6371 * except the last one must be a multiple of
6372 * 4 bytes. This is required so that the XDR
6373 * does not need padding.
6374 * If possible, clip the size to an exact
6375 * multiple of the mbuf length, so that the
6376 * split will be on an mbuf boundary.
6378 copylen &= 0xfffffffc;
6379 if (copylen > mp->m_len)
6380 copylen = copylen / mp->m_len *
6385 np->n_flag |= NDSCOMMIT;
6387 if (len > copylen && docommit == 0)
6391 if (docommit != 0) {
6394 * Do last mirrored DS commit with this thread.
6396 if (mirror < flp->nfsfl_mirrorcnt - 1)
6397 error = nfsio_commitds(vp, off, xfer,
6398 *dspp, fhp, dp->nfsdi_vers,
6399 dp->nfsdi_minorvers, drpc, tcred,
6402 error = nfsrpc_commitds(vp, off, xfer,
6403 *dspp, fhp, dp->nfsdi_vers,
6404 dp->nfsdi_minorvers, tcred, p);
6405 NFSCL_DEBUG(4, "commitds=%d\n", error);
6406 if (error != 0 && error != EACCES && error !=
6409 "DS layreterr for commit\n");
6410 nfscl_dserr(NFSV4OP_COMMIT, error, dp,
6414 NFSCL_DEBUG(4, "aft nfsio_commitds=%d\n", error);
6417 * Set both eof and uio_resid = 0 to end any
6421 uiop->uio_resid = 0;
6424 np->n_flag &= ~NDSCOMMIT;
6427 } else if (rwflag == NFSV4OPEN_ACCESSREAD) {
6428 error = nfsrpc_readds(vp, uiop, stateidp, eofp, *dspp,
6429 off, xfer, fhp, 1, dp->nfsdi_vers,
6430 dp->nfsdi_minorvers, tcred, p);
6431 NFSCL_DEBUG(4, "readds=%d\n", error);
6432 if (error != 0 && error != EACCES && error != ESTALE) {
6433 NFSCL_DEBUG(4, "DS layreterr for read\n");
6434 nfscl_dserr(NFSV4OP_READ, error, dp, lyp,
6438 if (flp->nfsfl_mirrorcnt == 1) {
6439 error = nfsrpc_writeds(vp, uiop, iomode,
6440 must_commit, stateidp, *dspp, off, xfer,
6441 fhp, 0, 1, dp->nfsdi_vers,
6442 dp->nfsdi_minorvers, tcred, p);
6445 lyp->nfsly_flags |= NFSLY_WRITTEN;
6451 /* The mbuf list must be split. */
6452 m2 = nfsm_split(mp, xfer);
6460 NFSCL_DEBUG(4, "mcopy len=%jd xfer=%jd\n",
6461 (uintmax_t)len, (uintmax_t)xfer);
6463 * Do last write to a mirrored DS with this
6467 if (mirror < flp->nfsfl_mirrorcnt - 1)
6468 error = nfsio_writedsmir(vp,
6469 iomode, must_commit,
6470 stateidp, *dspp, off,
6473 dp->nfsdi_minorvers, drpc,
6476 error = nfsrpc_writedsmir(vp,
6477 iomode, must_commit,
6478 stateidp, *dspp, off,
6481 dp->nfsdi_minorvers, tcred,
6484 NFSCL_DEBUG(4, "nfsio_writedsmir=%d\n", error);
6485 if (error != 0 && error != EACCES && error !=
6488 "DS layreterr for write\n");
6489 nfscl_dserr(NFSV4OP_WRITE, error, dp,
6494 NFSCL_DEBUG(4, "aft read/writeds=%d\n", error);
6499 if ((dp->nfsdi_flags & NFSDI_TIGHTCOUPLED) == 0)
6502 NFSCL_DEBUG(4, "eo nfscl_dofflayoutio=%d\n", error);
6507 * The actual read RPC done to a DS.
6510 nfsrpc_readds(vnode_t vp, struct uio *uiop, nfsv4stateid_t *stateidp, int *eofp,
6511 struct nfsclds *dsp, uint64_t io_off, int len, struct nfsfh *fhp, int flex,
6512 int vers, int minorvers, struct ucred *cred, NFSPROC_T *p)
6515 int attrflag, error, retlen;
6516 struct nfsrv_descript nfsd;
6517 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
6518 struct nfsrv_descript *nd = &nfsd;
6519 struct nfssockreq *nrp;
6523 if (vers == 0 || vers == NFS_VER4) {
6524 nfscl_reqstart(nd, NFSPROC_READDS, nmp, fhp->nfh_fh,
6525 fhp->nfh_len, NULL, &dsp->nfsclds_sess, vers, minorvers,
6528 NFSCL_DEBUG(4, "nfsrpc_readds: vers4 minvers=%d\n", minorvers);
6530 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID);
6532 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSEQIDZERO);
6534 nfscl_reqstart(nd, NFSPROC_READ, nmp, fhp->nfh_fh,
6535 fhp->nfh_len, NULL, &dsp->nfsclds_sess, vers, minorvers,
6537 NFSDECRGLOBAL(nfsstatsv1.rpccnt[NFSPROC_READ]);
6538 NFSINCRGLOBAL(nfsstatsv1.rpccnt[NFSPROC_READDS]);
6539 NFSCL_DEBUG(4, "nfsrpc_readds: vers3\n");
6541 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED * 3);
6542 txdr_hyper(io_off, tl);
6543 *(tl + 2) = txdr_unsigned(len);
6544 nrp = dsp->nfsclds_sockp;
6545 NFSCL_DEBUG(4, "nfsrpc_readds: nrp=%p\n", nrp);
6547 /* If NULL, use the MDS socket. */
6548 nrp = &nmp->nm_sockreq;
6549 error = newnfs_request(nd, nmp, NULL, nrp, vp, p, cred,
6550 NFS_PROG, vers, NULL, 1, NULL, &dsp->nfsclds_sess);
6551 NFSCL_DEBUG(4, "nfsrpc_readds: stat=%d err=%d\n", nd->nd_repstat,
6555 if (vers == NFS_VER3) {
6556 error = nfscl_postop_attr(nd, &na, &attrflag, NULL);
6557 NFSCL_DEBUG(4, "nfsrpc_readds: postop=%d\n", error);
6561 if (nd->nd_repstat != 0) {
6562 error = nd->nd_repstat;
6565 if (vers == NFS_VER3) {
6566 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
6567 *eofp = fxdr_unsigned(int, *(tl + 1));
6569 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
6570 *eofp = fxdr_unsigned(int, *tl);
6572 NFSM_STRSIZ(retlen, len);
6573 NFSCL_DEBUG(4, "nfsrpc_readds: retlen=%d eof=%d\n", retlen, *eofp);
6574 error = nfsm_mbufuio(nd, uiop, retlen);
6576 if (nd->nd_mrep != NULL)
6577 m_freem(nd->nd_mrep);
6582 * The actual write RPC done to a DS.
6585 nfsrpc_writeds(vnode_t vp, struct uio *uiop, int *iomode, int *must_commit,
6586 nfsv4stateid_t *stateidp, struct nfsclds *dsp, uint64_t io_off, int len,
6587 struct nfsfh *fhp, int commit_thru_mds, int flex, int vers, int minorvers,
6588 struct ucred *cred, NFSPROC_T *p)
6591 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
6592 int attrflag, error, rlen, commit, committed = NFSWRITE_FILESYNC;
6594 struct nfsrv_descript nfsd;
6595 struct nfsrv_descript *nd = &nfsd;
6596 struct nfssockreq *nrp;
6599 KASSERT(uiop->uio_iovcnt == 1, ("nfs: writerpc iovcnt > 1"));
6601 if (vers == 0 || vers == NFS_VER4) {
6602 nfscl_reqstart(nd, NFSPROC_WRITEDS, nmp, fhp->nfh_fh,
6603 fhp->nfh_len, NULL, &dsp->nfsclds_sess, vers, minorvers,
6605 NFSCL_DEBUG(4, "nfsrpc_writeds: vers4 minvers=%d\n", minorvers);
6608 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID);
6610 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSEQIDZERO);
6611 NFSM_BUILD(tl, uint32_t *, NFSX_HYPER + 2 * NFSX_UNSIGNED);
6613 nfscl_reqstart(nd, NFSPROC_WRITE, nmp, fhp->nfh_fh,
6614 fhp->nfh_len, NULL, &dsp->nfsclds_sess, vers, minorvers,
6616 NFSDECRGLOBAL(nfsstatsv1.rpccnt[NFSPROC_WRITE]);
6617 NFSINCRGLOBAL(nfsstatsv1.rpccnt[NFSPROC_WRITEDS]);
6618 NFSCL_DEBUG(4, "nfsrpc_writeds: vers3\n");
6619 NFSM_BUILD(tl, uint32_t *, NFSX_HYPER + 3 * NFSX_UNSIGNED);
6621 txdr_hyper(io_off, tl);
6623 if (vers == NFS_VER3)
6624 *tl++ = txdr_unsigned(len);
6625 *tl++ = txdr_unsigned(*iomode);
6626 *tl = txdr_unsigned(len);
6627 error = nfsm_uiombuf(nd, uiop, len);
6629 m_freem(nd->nd_mreq);
6632 nrp = dsp->nfsclds_sockp;
6634 /* If NULL, use the MDS socket. */
6635 nrp = &nmp->nm_sockreq;
6636 error = newnfs_request(nd, nmp, NULL, nrp, vp, p, cred,
6637 NFS_PROG, vers, NULL, 1, NULL, &dsp->nfsclds_sess);
6638 NFSCL_DEBUG(4, "nfsrpc_writeds: err=%d stat=%d\n", error,
6642 if (nd->nd_repstat != 0) {
6644 * In case the rpc gets retried, roll
6645 * the uio fileds changed by nfsm_uiombuf()
6648 uiop->uio_offset -= len;
6649 uiop->uio_resid += len;
6650 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base - len;
6651 uiop->uio_iov->iov_len += len;
6652 error = nd->nd_repstat;
6654 if (vers == NFS_VER3) {
6655 error = nfscl_wcc_data(nd, vp, &na, &attrflag, NULL,
6657 NFSCL_DEBUG(4, "nfsrpc_writeds: wcc_data=%d\n", error);
6661 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED + NFSX_VERF);
6662 rlen = fxdr_unsigned(int, *tl++);
6663 NFSCL_DEBUG(4, "nfsrpc_writeds: len=%d rlen=%d\n", len, rlen);
6667 } else if (rlen < len) {
6668 backup = len - rlen;
6669 uiop->uio_iov->iov_base =
6670 (char *)uiop->uio_iov->iov_base - backup;
6671 uiop->uio_iov->iov_len += backup;
6672 uiop->uio_offset -= backup;
6673 uiop->uio_resid += backup;
6676 commit = fxdr_unsigned(int, *tl++);
6679 * Return the lowest commitment level
6680 * obtained by any of the RPCs.
6682 if (committed == NFSWRITE_FILESYNC)
6684 else if (committed == NFSWRITE_DATASYNC &&
6685 commit == NFSWRITE_UNSTABLE)
6687 if (commit_thru_mds != 0) {
6689 if (!NFSHASWRITEVERF(nmp)) {
6690 NFSBCOPY(tl, nmp->nm_verf, NFSX_VERF);
6691 NFSSETWRITEVERF(nmp);
6692 } else if (NFSBCMP(tl, nmp->nm_verf, NFSX_VERF) &&
6693 *must_commit != 2) {
6695 NFSBCOPY(tl, nmp->nm_verf, NFSX_VERF);
6700 if ((dsp->nfsclds_flags & NFSCLDS_HASWRITEVERF) == 0) {
6701 NFSBCOPY(tl, dsp->nfsclds_verf, NFSX_VERF);
6702 dsp->nfsclds_flags |= NFSCLDS_HASWRITEVERF;
6703 } else if (NFSBCMP(tl, dsp->nfsclds_verf, NFSX_VERF) &&
6704 *must_commit != 2) {
6706 NFSBCOPY(tl, dsp->nfsclds_verf, NFSX_VERF);
6712 if (nd->nd_mrep != NULL)
6713 m_freem(nd->nd_mrep);
6714 *iomode = committed;
6715 if (nd->nd_repstat != 0 && error == 0)
6716 error = nd->nd_repstat;
6721 * The actual write RPC done to a DS.
6722 * This variant is called from a separate kernel process for mirrors.
6723 * Any short write is considered an IO error.
6726 nfsrpc_writedsmir(vnode_t vp, int *iomode, int *must_commit,
6727 nfsv4stateid_t *stateidp, struct nfsclds *dsp, uint64_t io_off, int len,
6728 struct nfsfh *fhp, struct mbuf *m, int vers, int minorvers,
6729 struct ucred *cred, NFSPROC_T *p)
6732 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
6733 int attrflag, error, commit, committed = NFSWRITE_FILESYNC, rlen;
6734 struct nfsrv_descript nfsd;
6735 struct nfsrv_descript *nd = &nfsd;
6736 struct nfssockreq *nrp;
6740 if (vers == 0 || vers == NFS_VER4) {
6741 nfscl_reqstart(nd, NFSPROC_WRITEDS, nmp, fhp->nfh_fh,
6742 fhp->nfh_len, NULL, &dsp->nfsclds_sess, vers, minorvers,
6745 NFSCL_DEBUG(4, "nfsrpc_writedsmir: vers4 minvers=%d\n",
6747 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID);
6748 NFSM_BUILD(tl, uint32_t *, NFSX_HYPER + 2 * NFSX_UNSIGNED);
6750 nfscl_reqstart(nd, NFSPROC_WRITE, nmp, fhp->nfh_fh,
6751 fhp->nfh_len, NULL, &dsp->nfsclds_sess, vers, minorvers,
6753 NFSDECRGLOBAL(nfsstatsv1.rpccnt[NFSPROC_WRITE]);
6754 NFSINCRGLOBAL(nfsstatsv1.rpccnt[NFSPROC_WRITEDS]);
6755 NFSCL_DEBUG(4, "nfsrpc_writedsmir: vers3\n");
6756 NFSM_BUILD(tl, uint32_t *, NFSX_HYPER + 3 * NFSX_UNSIGNED);
6758 txdr_hyper(io_off, tl);
6760 if (vers == NFS_VER3)
6761 *tl++ = txdr_unsigned(len);
6762 *tl++ = txdr_unsigned(*iomode);
6763 *tl = txdr_unsigned(len);
6765 /* Put data in mbuf chain. */
6766 nd->nd_mb->m_next = m;
6768 nrp = dsp->nfsclds_sockp;
6770 /* If NULL, use the MDS socket. */
6771 nrp = &nmp->nm_sockreq;
6772 error = newnfs_request(nd, nmp, NULL, nrp, vp, p, cred,
6773 NFS_PROG, vers, NULL, 1, NULL, &dsp->nfsclds_sess);
6774 NFSCL_DEBUG(4, "nfsrpc_writedsmir: err=%d stat=%d\n", error,
6778 if (nd->nd_repstat != 0)
6779 error = nd->nd_repstat;
6781 if (vers == NFS_VER3) {
6782 error = nfscl_wcc_data(nd, vp, &na, &attrflag, NULL,
6784 NFSCL_DEBUG(4, "nfsrpc_writedsmir: wcc_data=%d\n",
6789 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED + NFSX_VERF);
6790 rlen = fxdr_unsigned(int, *tl++);
6791 NFSCL_DEBUG(4, "nfsrpc_writedsmir: len=%d rlen=%d\n", len,
6795 NFSCL_DEBUG(4, "nfsrpc_writedsmir: len=%d rlen=%d\n",
6799 commit = fxdr_unsigned(int, *tl++);
6802 * Return the lowest commitment level
6803 * obtained by any of the RPCs.
6805 if (committed == NFSWRITE_FILESYNC)
6807 else if (committed == NFSWRITE_DATASYNC &&
6808 commit == NFSWRITE_UNSTABLE)
6811 if ((dsp->nfsclds_flags & NFSCLDS_HASWRITEVERF) == 0) {
6812 NFSBCOPY(tl, dsp->nfsclds_verf, NFSX_VERF);
6813 dsp->nfsclds_flags |= NFSCLDS_HASWRITEVERF;
6814 } else if (NFSBCMP(tl, dsp->nfsclds_verf, NFSX_VERF) &&
6815 *must_commit != 2) {
6817 NFSBCOPY(tl, dsp->nfsclds_verf, NFSX_VERF);
6822 if (nd->nd_mrep != NULL)
6823 m_freem(nd->nd_mrep);
6824 *iomode = committed;
6825 if (nd->nd_repstat != 0 && error == 0)
6826 error = nd->nd_repstat;
6831 * Start up the thread that will execute nfsrpc_writedsmir().
6834 start_writedsmir(void *arg, int pending)
6836 struct nfsclwritedsdorpc *drpc;
6838 drpc = (struct nfsclwritedsdorpc *)arg;
6839 drpc->err = nfsrpc_writedsmir(drpc->vp, &drpc->iomode,
6840 &drpc->must_commit, drpc->stateidp, drpc->dsp, drpc->off, drpc->len,
6841 drpc->fhp, drpc->m, drpc->vers, drpc->minorvers, drpc->cred,
6845 NFSCL_DEBUG(4, "start_writedsmir: err=%d\n", drpc->err);
6849 * Set up the write DS mirror call for the pNFS I/O thread.
6852 nfsio_writedsmir(vnode_t vp, int *iomode, int *must_commit,
6853 nfsv4stateid_t *stateidp, struct nfsclds *dsp, uint64_t off, int len,
6854 struct nfsfh *fhp, struct mbuf *m, int vers, int minorvers,
6855 struct nfsclwritedsdorpc *drpc, struct ucred *cred, NFSPROC_T *p)
6862 drpc->iomode = *iomode;
6863 drpc->must_commit = *must_commit;
6864 drpc->stateidp = stateidp;
6871 drpc->minorvers = minorvers;
6872 drpc->cred = crhold(cred);
6876 if (nfs_pnfsiothreads != 0) {
6877 ret = nfs_pnfsio(start_writedsmir, drpc);
6878 NFSCL_DEBUG(4, "nfsio_writedsmir: nfs_pnfsio=%d\n", ret);
6881 error = nfsrpc_writedsmir(vp, iomode, &drpc->must_commit,
6882 stateidp, dsp, off, len, fhp, m, vers, minorvers, cred, p);
6885 NFSCL_DEBUG(4, "nfsio_writedsmir: error=%d\n", error);
6890 * Free up the nfsclds structure.
6893 nfscl_freenfsclds(struct nfsclds *dsp)
6899 if (dsp->nfsclds_sockp != NULL) {
6900 NFSFREECRED(dsp->nfsclds_sockp->nr_cred);
6901 NFSFREEMUTEX(&dsp->nfsclds_sockp->nr_mtx);
6902 free(dsp->nfsclds_sockp->nr_nam, M_SONAME);
6903 free(dsp->nfsclds_sockp, M_NFSSOCKREQ);
6905 NFSFREEMUTEX(&dsp->nfsclds_mtx);
6906 NFSFREEMUTEX(&dsp->nfsclds_sess.nfsess_mtx);
6907 for (i = 0; i < NFSV4_CBSLOTS; i++) {
6908 if (dsp->nfsclds_sess.nfsess_cbslots[i].nfssl_reply != NULL)
6910 dsp->nfsclds_sess.nfsess_cbslots[i].nfssl_reply);
6912 free(dsp, M_NFSCLDS);
6915 static enum nfsclds_state
6916 nfscl_getsameserver(struct nfsmount *nmp, struct nfsclds *newdsp,
6917 struct nfsclds **retdspp, uint32_t *sequencep)
6919 struct nfsclds *dsp;
6923 * Search the list of nfsclds structures for one with the same
6927 TAILQ_FOREACH(dsp, &nmp->nm_sess, nfsclds_list) {
6928 if (dsp->nfsclds_servownlen == newdsp->nfsclds_servownlen &&
6929 dsp->nfsclds_servownlen != 0 &&
6930 !NFSBCMP(dsp->nfsclds_serverown, newdsp->nfsclds_serverown,
6931 dsp->nfsclds_servownlen) &&
6932 dsp->nfsclds_sess.nfsess_defunct == 0) {
6933 NFSCL_DEBUG(4, "fnd same fdsp=%p dsp=%p flg=0x%x\n",
6934 TAILQ_FIRST(&nmp->nm_sess), dsp,
6935 dsp->nfsclds_flags);
6937 /* Get sequenceid# from first entry. */
6939 dsp->nfsclds_sess.nfsess_sequenceid;
6942 /* Server major id matches. */
6943 if ((dsp->nfsclds_flags & NFSCLDS_DS) != 0) {
6945 return (NFSDSP_USETHISSESSION);
6950 return (NFSDSP_SEQTHISSESSION);
6951 return (NFSDSP_NOTFOUND);
6955 * NFS commit rpc to a NFSv4.1 DS.
6958 nfsrpc_commitds(vnode_t vp, uint64_t offset, int cnt, struct nfsclds *dsp,
6959 struct nfsfh *fhp, int vers, int minorvers, struct ucred *cred,
6963 struct nfsrv_descript nfsd, *nd = &nfsd;
6964 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
6965 struct nfssockreq *nrp;
6967 int attrflag, error;
6970 if (vers == 0 || vers == NFS_VER4) {
6971 nfscl_reqstart(nd, NFSPROC_COMMITDS, nmp, fhp->nfh_fh,
6972 fhp->nfh_len, NULL, &dsp->nfsclds_sess, vers, minorvers,
6976 nfscl_reqstart(nd, NFSPROC_COMMIT, nmp, fhp->nfh_fh,
6977 fhp->nfh_len, NULL, &dsp->nfsclds_sess, vers, minorvers,
6979 NFSDECRGLOBAL(nfsstatsv1.rpccnt[NFSPROC_COMMIT]);
6980 NFSINCRGLOBAL(nfsstatsv1.rpccnt[NFSPROC_COMMITDS]);
6982 NFSCL_DEBUG(4, "nfsrpc_commitds: vers=%d minvers=%d\n", vers,
6984 NFSM_BUILD(tl, uint32_t *, NFSX_HYPER + NFSX_UNSIGNED);
6985 txdr_hyper(offset, tl);
6987 *tl = txdr_unsigned(cnt);
6988 nrp = dsp->nfsclds_sockp;
6990 /* If NULL, use the MDS socket. */
6991 nrp = &nmp->nm_sockreq;
6992 error = newnfs_request(nd, nmp, NULL, nrp, vp, p, cred,
6993 NFS_PROG, vers, NULL, 1, NULL, &dsp->nfsclds_sess);
6994 NFSCL_DEBUG(4, "nfsrpc_commitds: err=%d stat=%d\n", error,
6998 if (nd->nd_repstat == 0) {
6999 if (vers == NFS_VER3) {
7000 error = nfscl_wcc_data(nd, vp, &na, &attrflag, NULL,
7002 NFSCL_DEBUG(4, "nfsrpc_commitds: wccdata=%d\n", error);
7006 NFSM_DISSECT(tl, u_int32_t *, NFSX_VERF);
7008 if (NFSBCMP(tl, dsp->nfsclds_verf, NFSX_VERF)) {
7009 NFSBCOPY(tl, dsp->nfsclds_verf, NFSX_VERF);
7010 error = NFSERR_STALEWRITEVERF;
7015 if (error == 0 && nd->nd_repstat != 0)
7016 error = nd->nd_repstat;
7017 m_freem(nd->nd_mrep);
7022 * Start up the thread that will execute nfsrpc_commitds().
7025 start_commitds(void *arg, int pending)
7027 struct nfsclwritedsdorpc *drpc;
7029 drpc = (struct nfsclwritedsdorpc *)arg;
7030 drpc->err = nfsrpc_commitds(drpc->vp, drpc->off, drpc->len,
7031 drpc->dsp, drpc->fhp, drpc->vers, drpc->minorvers, drpc->cred,
7035 NFSCL_DEBUG(4, "start_commitds: err=%d\n", drpc->err);
7039 * Set up the commit DS mirror call for the pNFS I/O thread.
7042 nfsio_commitds(vnode_t vp, uint64_t offset, int cnt, struct nfsclds *dsp,
7043 struct nfsfh *fhp, int vers, int minorvers,
7044 struct nfsclwritedsdorpc *drpc, struct ucred *cred, NFSPROC_T *p)
7056 drpc->minorvers = minorvers;
7057 drpc->cred = crhold(cred);
7061 if (nfs_pnfsiothreads != 0) {
7062 ret = nfs_pnfsio(start_commitds, drpc);
7063 NFSCL_DEBUG(4, "nfsio_commitds: nfs_pnfsio=%d\n", ret);
7066 error = nfsrpc_commitds(vp, offset, cnt, dsp, fhp, vers,
7067 minorvers, cred, p);
7070 NFSCL_DEBUG(4, "nfsio_commitds: error=%d\n", error);
7078 nfsrpc_advise(vnode_t vp, off_t offset, uint64_t cnt, int advise,
7079 struct ucred *cred, NFSPROC_T *p)
7082 struct nfsrv_descript nfsd, *nd = &nfsd;
7086 NFSZERO_ATTRBIT(&hints);
7087 if (advise == POSIX_FADV_WILLNEED)
7088 NFSSETBIT_ATTRBIT(&hints, NFSV4IOHINT_WILLNEED);
7089 else if (advise == POSIX_FADV_DONTNEED)
7090 NFSSETBIT_ATTRBIT(&hints, NFSV4IOHINT_DONTNEED);
7093 NFSCL_REQSTART(nd, NFSPROC_IOADVISE, vp, cred);
7094 nfsm_stateidtom(nd, NULL, NFSSTATEID_PUTALLZERO);
7095 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_HYPER);
7096 txdr_hyper(offset, tl);
7098 txdr_hyper(cnt, tl);
7099 nfsrv_putattrbit(nd, &hints);
7100 error = nfscl_request(nd, vp, p, cred, NULL);
7103 if (nd->nd_repstat != 0)
7104 error = nd->nd_repstat;
7105 m_freem(nd->nd_mrep);
7111 * NFS advise rpc to a NFSv4.2 DS.
7114 nfsrpc_adviseds(vnode_t vp, uint64_t offset, int cnt, int advise,
7115 struct nfsclds *dsp, struct nfsfh *fhp, int vers, int minorvers,
7116 struct ucred *cred, NFSPROC_T *p)
7119 struct nfsrv_descript nfsd, *nd = &nfsd;
7120 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
7121 struct nfssockreq *nrp;
7125 /* For NFS DSs prior to NFSv4.2, just return OK. */
7126 if (vers == NFS_VER3 || minorversion < NFSV42_MINORVERSION)
7128 NFSZERO_ATTRBIT(&hints);
7129 if (advise == POSIX_FADV_WILLNEED)
7130 NFSSETBIT_ATTRBIT(&hints, NFSV4IOHINT_WILLNEED);
7131 else if (advise == POSIX_FADV_DONTNEED)
7132 NFSSETBIT_ATTRBIT(&hints, NFSV4IOHINT_DONTNEED);
7136 nfscl_reqstart(nd, NFSPROC_IOADVISEDS, nmp, fhp->nfh_fh,
7137 fhp->nfh_len, NULL, &dsp->nfsclds_sess, vers, minorvers, NULL);
7139 NFSCL_DEBUG(4, "nfsrpc_adviseds: vers=%d minvers=%d\n", vers,
7141 nfsm_stateidtom(nd, NULL, NFSSTATEID_PUTALLZERO);
7142 NFSM_BUILD(tl, uint32_t *, NFSX_HYPER + NFSX_UNSIGNED);
7143 txdr_hyper(offset, tl);
7145 *tl = txdr_unsigned(cnt);
7146 nfsrv_putattrbit(nd, &hints);
7147 nrp = dsp->nfsclds_sockp;
7149 /* If NULL, use the MDS socket. */
7150 nrp = &nmp->nm_sockreq;
7151 error = newnfs_request(nd, nmp, NULL, nrp, vp, p, cred,
7152 NFS_PROG, vers, NULL, 1, NULL, &dsp->nfsclds_sess);
7153 NFSCL_DEBUG(4, "nfsrpc_adviseds: err=%d stat=%d\n", error,
7157 if (nd->nd_repstat != 0)
7158 error = nd->nd_repstat;
7159 m_freem(nd->nd_mrep);
7164 * Start up the thread that will execute nfsrpc_commitds().
7167 start_adviseds(void *arg, int pending)
7169 struct nfsclwritedsdorpc *drpc;
7171 drpc = (struct nfsclwritedsdorpc *)arg;
7172 drpc->err = nfsrpc_adviseds(drpc->vp, drpc->off, drpc->len,
7173 drpc->advise, drpc->dsp, drpc->fhp, drpc->vers, drpc->minorvers,
7174 drpc->cred, drpc->p);
7177 NFSCL_DEBUG(4, "start_adviseds: err=%d\n", drpc->err);
7181 * Set up the advise DS mirror call for the pNFS I/O thread.
7184 nfsio_adviseds(vnode_t vp, uint64_t offset, int cnt, int advise,
7185 struct nfsclds *dsp, struct nfsfh *fhp, int vers, int minorvers,
7186 struct nfsclwritedsdorpc *drpc, struct ucred *cred, NFSPROC_T *p)
7195 drpc->advise = advise;
7199 drpc->minorvers = minorvers;
7200 drpc->cred = crhold(cred);
7204 if (nfs_pnfsiothreads != 0) {
7205 ret = nfs_pnfsio(start_adviseds, drpc);
7206 NFSCL_DEBUG(4, "nfsio_adviseds: nfs_pnfsio=%d\n", ret);
7209 error = nfsrpc_adviseds(vp, offset, cnt, advise, dsp, fhp, vers,
7210 minorvers, cred, p);
7213 NFSCL_DEBUG(4, "nfsio_adviseds: error=%d\n", error);
7219 * Do the Allocate operation, retrying for recovery.
7222 nfsrpc_allocate(vnode_t vp, off_t off, off_t len, struct nfsvattr *nap,
7223 int *attrflagp, struct ucred *cred, NFSPROC_T *p, void *stuff)
7225 int error, expireret = 0, retrycnt, nostateid;
7226 uint32_t clidrev = 0;
7227 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
7228 struct nfsfh *nfhp = NULL;
7229 nfsv4stateid_t stateid;
7237 tmp_off = off + len;
7239 if (tmp_off > nmp->nm_maxfilesize || tmp_off < off) {
7243 if (nmp->nm_clp != NULL)
7244 clidrev = nmp->nm_clp->nfsc_clientidrev;
7246 nfhp = VTONFS(vp)->n_fhp;
7251 nfscl_getstateid(vp, nfhp->nfh_fh, nfhp->nfh_len,
7252 NFSV4OPEN_ACCESSWRITE, 0, cred, p, &stateid, &lckp);
7253 if (stateid.other[0] == 0 && stateid.other[1] == 0 &&
7254 stateid.other[2] == 0) {
7256 NFSCL_DEBUG(1, "stateid0 in allocate\n");
7260 * Not finding a stateid should probably never happen,
7261 * but just return an error for this case.
7266 error = nfsrpc_allocaterpc(vp, off, len, &stateid,
7267 nap, attrflagp, cred, p, stuff);
7268 if (error == NFSERR_STALESTATEID)
7269 nfscl_initiate_recovery(nmp->nm_clp);
7271 nfscl_lockderef(lckp);
7272 if (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
7273 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
7274 error == NFSERR_OLDSTATEID || error == NFSERR_BADSESSION) {
7275 (void) nfs_catnap(PZERO, error, "nfs_allocate");
7276 } else if ((error == NFSERR_EXPIRED || (!NFSHASINT(nmp) &&
7277 error == NFSERR_BADSTATEID)) && clidrev != 0) {
7278 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
7279 } else if (error == NFSERR_BADSTATEID && NFSHASINT(nmp)) {
7283 } while (error == NFSERR_GRACE || error == NFSERR_DELAY ||
7284 error == NFSERR_STALESTATEID || error == NFSERR_BADSESSION ||
7285 error == NFSERR_STALEDONTRECOVER ||
7286 (error == NFSERR_OLDSTATEID && retrycnt < 20) ||
7287 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
7288 expireret == 0 && clidrev != 0 && retrycnt < 4));
7289 if (error != 0 && retrycnt >= 4)
7298 nfsrpc_allocaterpc(vnode_t vp, off_t off, off_t len, nfsv4stateid_t *stateidp,
7299 struct nfsvattr *nap, int *attrflagp, struct ucred *cred, NFSPROC_T *p,
7304 struct nfsrv_descript nfsd;
7305 struct nfsrv_descript *nd = &nfsd;
7306 nfsattrbit_t attrbits;
7309 NFSCL_REQSTART(nd, NFSPROC_ALLOCATE, vp, cred);
7310 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID);
7311 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_HYPER + NFSX_UNSIGNED);
7312 txdr_hyper(off, tl); tl += 2;
7313 txdr_hyper(len, tl); tl += 2;
7314 *tl = txdr_unsigned(NFSV4OP_GETATTR);
7315 NFSGETATTR_ATTRBIT(&attrbits);
7316 nfsrv_putattrbit(nd, &attrbits);
7317 error = nfscl_request(nd, vp, p, cred, stuff);
7320 if (nd->nd_repstat == 0) {
7321 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
7322 error = nfsm_loadattr(nd, nap);
7324 *attrflagp = NFS_LATTR_NOSHRINK;
7326 error = nd->nd_repstat;
7328 m_freem(nd->nd_mrep);
7333 * Set up the XDR arguments for the LayoutGet operation.
7336 nfsrv_setuplayoutget(struct nfsrv_descript *nd, int iomode, uint64_t offset,
7337 uint64_t len, uint64_t minlen, nfsv4stateid_t *stateidp, int layouttype,
7338 int layoutlen, int usecurstateid)
7342 NFSM_BUILD(tl, uint32_t *, 4 * NFSX_UNSIGNED + 3 * NFSX_HYPER +
7344 *tl++ = newnfs_false; /* Don't signal availability. */
7345 *tl++ = txdr_unsigned(layouttype);
7346 *tl++ = txdr_unsigned(iomode);
7347 txdr_hyper(offset, tl);
7349 txdr_hyper(len, tl);
7351 txdr_hyper(minlen, tl);
7353 if (usecurstateid != 0) {
7354 /* Special stateid for Current stateid. */
7355 *tl++ = txdr_unsigned(1);
7360 *tl++ = txdr_unsigned(stateidp->seqid);
7361 NFSCL_DEBUG(4, "layget seq=%d\n", (int)stateidp->seqid);
7362 *tl++ = stateidp->other[0];
7363 *tl++ = stateidp->other[1];
7364 *tl++ = stateidp->other[2];
7366 *tl = txdr_unsigned(layoutlen);
7370 * Parse the reply for a successful LayoutGet operation.
7373 nfsrv_parselayoutget(struct nfsmount *nmp, struct nfsrv_descript *nd,
7374 nfsv4stateid_t *stateidp, int *retonclosep, struct nfsclflayouthead *flhp)
7377 struct nfsclflayout *flp, *prevflp, *tflp;
7378 int cnt, error, fhcnt, gotiomode, i, iomode, j, k, l, laytype, nfhlen;
7380 uint64_t retlen, off;
7386 NFSCL_DEBUG(4, "in nfsrv_parselayoutget\n");
7390 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED + NFSX_STATEID);
7395 stateidp->seqid = fxdr_unsigned(uint32_t, *tl++);
7396 NFSCL_DEBUG(4, "retoncls=%d stseq=%d\n", *retonclosep,
7397 (int)stateidp->seqid);
7398 stateidp->other[0] = *tl++;
7399 stateidp->other[1] = *tl++;
7400 stateidp->other[2] = *tl++;
7401 cnt = fxdr_unsigned(int, *tl);
7402 NFSCL_DEBUG(4, "layg cnt=%d\n", cnt);
7403 if (cnt <= 0 || cnt > 10000) {
7404 /* Don't accept more than 10000 layouts in reply. */
7405 error = NFSERR_BADXDR;
7408 for (i = 0; i < cnt; i++) {
7409 /* Dissect to the layout type. */
7410 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_HYPER +
7412 off = fxdr_hyper(tl); tl += 2;
7413 retlen = fxdr_hyper(tl); tl += 2;
7414 iomode = fxdr_unsigned(int, *tl++);
7415 laytype = fxdr_unsigned(int, *tl);
7416 NFSCL_DEBUG(4, "layt=%d off=%ju len=%ju iom=%d\n", laytype,
7417 (uintmax_t)off, (uintmax_t)retlen, iomode);
7418 /* Ignore length of layout body for now. */
7419 if (laytype == NFSLAYOUT_NFSV4_1_FILES) {
7420 /* Parse the File layout up to fhcnt. */
7421 NFSM_DISSECT(tl, uint32_t *, 3 * NFSX_UNSIGNED +
7422 NFSX_HYPER + NFSX_V4DEVICEID);
7423 fhcnt = fxdr_unsigned(int, *(tl + 4 +
7424 NFSX_V4DEVICEID / NFSX_UNSIGNED));
7425 NFSCL_DEBUG(4, "fhcnt=%d\n", fhcnt);
7426 if (fhcnt < 0 || fhcnt > 100) {
7427 /* Don't accept more than 100 file handles. */
7428 error = NFSERR_BADXDR;
7432 flp = malloc(sizeof(*flp) + fhcnt *
7433 sizeof(struct nfsfh *), M_NFSFLAYOUT,
7436 flp = malloc(sizeof(*flp), M_NFSFLAYOUT,
7438 flp->nfsfl_flags = NFSFL_FILE;
7439 flp->nfsfl_fhcnt = 0;
7440 flp->nfsfl_devp = NULL;
7441 flp->nfsfl_off = off;
7442 if (flp->nfsfl_off + retlen < flp->nfsfl_off)
7443 flp->nfsfl_end = UINT64_MAX - flp->nfsfl_off;
7445 flp->nfsfl_end = flp->nfsfl_off + retlen;
7446 flp->nfsfl_iomode = iomode;
7447 if (gotiomode == -1)
7448 gotiomode = flp->nfsfl_iomode;
7449 /* Ignore layout body length for now. */
7450 NFSBCOPY(tl, flp->nfsfl_dev, NFSX_V4DEVICEID);
7451 tl += (NFSX_V4DEVICEID / NFSX_UNSIGNED);
7452 flp->nfsfl_util = fxdr_unsigned(uint32_t, *tl++);
7453 NFSCL_DEBUG(4, "flutil=0x%x\n", flp->nfsfl_util);
7454 mtx_lock(&nmp->nm_mtx);
7455 if (nmp->nm_minorvers > 1 && (flp->nfsfl_util &
7456 NFSFLAYUTIL_IOADVISE_THRU_MDS) != 0)
7457 nmp->nm_privflag |= NFSMNTP_IOADVISETHRUMDS;
7458 mtx_unlock(&nmp->nm_mtx);
7459 flp->nfsfl_stripe1 = fxdr_unsigned(uint32_t, *tl++);
7460 flp->nfsfl_patoff = fxdr_hyper(tl); tl += 2;
7461 NFSCL_DEBUG(4, "stripe1=%u poff=%ju\n",
7462 flp->nfsfl_stripe1, (uintmax_t)flp->nfsfl_patoff);
7463 for (j = 0; j < fhcnt; j++) {
7464 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
7465 nfhlen = fxdr_unsigned(int, *tl);
7466 if (nfhlen <= 0 || nfhlen > NFSX_V4FHMAX) {
7467 error = NFSERR_BADXDR;
7470 nfhp = malloc(sizeof(*nfhp) + nfhlen - 1,
7472 flp->nfsfl_fh[j] = nfhp;
7474 nfhp->nfh_len = nfhlen;
7475 NFSM_DISSECT(cp, uint8_t *, NFSM_RNDUP(nfhlen));
7476 NFSBCOPY(cp, nfhp->nfh_fh, nfhlen);
7478 } else if (laytype == NFSLAYOUT_FLEXFILE) {
7479 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED +
7481 mirrorcnt = fxdr_unsigned(int, *(tl + 2));
7482 NFSCL_DEBUG(4, "mirrorcnt=%d\n", mirrorcnt);
7483 if (mirrorcnt < 1 || mirrorcnt > NFSDEV_MAXMIRRORS) {
7484 error = NFSERR_BADXDR;
7487 flp = malloc(sizeof(*flp) + mirrorcnt *
7488 sizeof(struct nfsffm), M_NFSFLAYOUT, M_WAITOK);
7489 flp->nfsfl_flags = NFSFL_FLEXFILE;
7490 flp->nfsfl_mirrorcnt = mirrorcnt;
7491 for (j = 0; j < mirrorcnt; j++)
7492 flp->nfsfl_ffm[j].devp = NULL;
7493 flp->nfsfl_off = off;
7494 if (flp->nfsfl_off + retlen < flp->nfsfl_off)
7495 flp->nfsfl_end = UINT64_MAX - flp->nfsfl_off;
7497 flp->nfsfl_end = flp->nfsfl_off + retlen;
7498 flp->nfsfl_iomode = iomode;
7499 if (gotiomode == -1)
7500 gotiomode = flp->nfsfl_iomode;
7501 flp->nfsfl_stripeunit = fxdr_hyper(tl);
7502 NFSCL_DEBUG(4, "stripeunit=%ju\n",
7503 (uintmax_t)flp->nfsfl_stripeunit);
7504 for (j = 0; j < mirrorcnt; j++) {
7505 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
7506 k = fxdr_unsigned(int, *tl);
7507 if (k < 1 || k > 128) {
7508 error = NFSERR_BADXDR;
7511 NFSCL_DEBUG(4, "servercnt=%d\n", k);
7512 for (l = 0; l < k; l++) {
7513 NFSM_DISSECT(tl, uint32_t *,
7514 NFSX_V4DEVICEID + NFSX_STATEID +
7517 /* Just use the first server. */
7519 flp->nfsfl_ffm[j].dev,
7521 tl += (NFSX_V4DEVICEID /
7524 flp->nfsfl_ffm[j].st.seqid =
7526 flp->nfsfl_ffm[j].st.other[0] =
7528 flp->nfsfl_ffm[j].st.other[1] =
7530 flp->nfsfl_ffm[j].st.other[2] =
7532 NFSCL_DEBUG(4, "st.seqid=%u "
7533 "st.o0=0x%x st.o1=0x%x "
7535 flp->nfsfl_ffm[j].st.seqid,
7536 flp->nfsfl_ffm[j].st.other[0],
7537 flp->nfsfl_ffm[j].st.other[1],
7538 flp->nfsfl_ffm[j].st.other[2]);
7540 tl += ((NFSX_V4DEVICEID +
7544 fhcnt = fxdr_unsigned(int, *tl);
7545 NFSCL_DEBUG(4, "fhcnt=%d\n", fhcnt);
7547 fhcnt > NFSDEV_MAXVERS) {
7548 error = NFSERR_BADXDR;
7551 for (m = 0; m < fhcnt; m++) {
7552 NFSM_DISSECT(tl, uint32_t *,
7554 nfhlen = fxdr_unsigned(int,
7556 NFSCL_DEBUG(4, "nfhlen=%d\n",
7558 if (nfhlen <= 0 || nfhlen >
7560 error = NFSERR_BADXDR;
7563 NFSM_DISSECT(cp, uint8_t *,
7564 NFSM_RNDUP(nfhlen));
7566 flp->nfsfl_ffm[j].fhcnt
7570 nfhlen - 1, M_NFSFH,
7572 flp->nfsfl_ffm[j].fh[m]
7574 nfhp->nfh_len = nfhlen;
7582 /* Now, get the ffsd_user/ffds_group. */
7583 error = nfsrv_parseug(nd, 0, &user,
7585 NFSCL_DEBUG(4, "after parseu=%d\n",
7588 error = nfsrv_parseug(nd, 1,
7589 &user, &grp, curthread);
7590 NFSCL_DEBUG(4, "aft parseg=%d\n",
7594 NFSCL_DEBUG(4, "user=%d group=%d\n",
7597 flp->nfsfl_ffm[j].user = user;
7598 flp->nfsfl_ffm[j].group = grp;
7600 "usr=%d grp=%d\n", user,
7605 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
7606 flp->nfsfl_fflags = fxdr_unsigned(uint32_t, *tl++);
7609 * At this time, there is no flag.
7610 * NFSFLEXFLAG_IOADVISE_THRU_MDS might need to be
7611 * added, or it may never exist?
7613 mtx_lock(&nmp->nm_mtx);
7614 if (nmp->nm_minorvers > 1 && (flp->nfsfl_fflags &
7615 NFSFLEXFLAG_IOADVISE_THRU_MDS) != 0)
7616 nmp->nm_privflag |= NFSMNTP_IOADVISETHRUMDS;
7617 mtx_unlock(&nmp->nm_mtx);
7619 flp->nfsfl_statshint = fxdr_unsigned(uint32_t, *tl);
7620 NFSCL_DEBUG(4, "fflags=0x%x statshint=%d\n",
7621 flp->nfsfl_fflags, flp->nfsfl_statshint);
7623 error = NFSERR_BADXDR;
7626 if (flp->nfsfl_iomode == gotiomode) {
7627 /* Keep the list in increasing offset order. */
7628 tflp = LIST_FIRST(flhp);
7630 while (tflp != NULL &&
7631 tflp->nfsfl_off < flp->nfsfl_off) {
7633 tflp = LIST_NEXT(tflp, nfsfl_list);
7635 if (prevflp == NULL)
7636 LIST_INSERT_HEAD(flhp, flp, nfsfl_list);
7638 LIST_INSERT_AFTER(prevflp, flp,
7640 NFSCL_DEBUG(4, "flp inserted\n");
7642 printf("nfscl_layoutget(): got wrong iomode\n");
7643 nfscl_freeflayout(flp);
7648 NFSCL_DEBUG(4, "eo nfsrv_parselayoutget=%d\n", error);
7649 if (error != 0 && flp != NULL)
7650 nfscl_freeflayout(flp);
7655 * Parse a user/group digit string.
7658 nfsrv_parseug(struct nfsrv_descript *nd, int dogrp, uid_t *uidp, gid_t *gidp,
7662 char *cp, *str, str0[NFSV4_SMALLSTR + 1];
7666 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
7667 len = fxdr_unsigned(uint32_t, *tl);
7669 if (len > NFSV4_OPAQUELIMIT) {
7670 error = NFSERR_BADXDR;
7673 NFSCL_DEBUG(4, "nfsrv_parseug: len=%d\n", len);
7676 *gidp = GID_NOGROUP;
7681 if (len > NFSV4_SMALLSTR)
7682 str = malloc(len + 1, M_TEMP, M_WAITOK);
7685 NFSM_DISSECT(cp, char *, NFSM_RNDUP(len));
7686 NFSBCOPY(cp, str, len);
7688 NFSCL_DEBUG(4, "nfsrv_parseug: str=%s\n", str);
7690 error = nfsv4_strtogid(nd, str, len, gidp);
7692 error = nfsv4_strtouid(nd, str, len, uidp);
7694 if (len > NFSV4_SMALLSTR)
7696 NFSCL_DEBUG(4, "eo nfsrv_parseug=%d\n", error);
7701 * Similar to nfsrpc_getlayout(), except that it uses nfsrpc_openlayget(),
7702 * so that it does both an Open and a Layoutget.
7705 nfsrpc_getopenlayout(struct nfsmount *nmp, vnode_t vp, u_int8_t *nfhp,
7706 int fhlen, uint8_t *newfhp, int newfhlen, uint32_t mode,
7707 struct nfsclopen *op, uint8_t *name, int namelen, struct nfscldeleg **dpp,
7708 struct ucred *cred, NFSPROC_T *p)
7710 struct nfscllayout *lyp;
7711 struct nfsclflayout *flp;
7712 struct nfsclflayouthead flh;
7713 int error, islocked, layoutlen, recalled, retonclose, usecurstateid;
7714 int layouttype, laystat;
7715 nfsv4stateid_t stateid;
7716 struct nfsclsession *tsep;
7719 if (NFSHASFLEXFILE(nmp))
7720 layouttype = NFSLAYOUT_FLEXFILE;
7722 layouttype = NFSLAYOUT_NFSV4_1_FILES;
7724 * If lyp is returned non-NULL, there will be a refcnt (shared lock)
7725 * on it, iff flp != NULL or a lock (exclusive lock) on it iff
7728 lyp = nfscl_getlayout(nmp->nm_clp, newfhp, newfhlen, 0, mode, &flp,
7730 NFSCL_DEBUG(4, "nfsrpc_getopenlayout nfscl_getlayout lyp=%p\n", lyp);
7733 else if (flp != NULL)
7737 if ((lyp == NULL || flp == NULL) && recalled == 0) {
7739 tsep = nfsmnt_mdssession(nmp);
7740 layoutlen = tsep->nfsess_maxcache - (NFSX_STATEID +
7746 stateid.seqid = lyp->nfsly_stateid.seqid;
7747 stateid.other[0] = lyp->nfsly_stateid.other[0];
7748 stateid.other[1] = lyp->nfsly_stateid.other[1];
7749 stateid.other[2] = lyp->nfsly_stateid.other[2];
7751 error = nfsrpc_openlayoutrpc(nmp, vp, nfhp, fhlen,
7752 newfhp, newfhlen, mode, op, name, namelen,
7753 dpp, &stateid, usecurstateid, layouttype, layoutlen,
7754 &retonclose, &flh, &laystat, cred, p);
7755 NFSCL_DEBUG(4, "aft nfsrpc_openlayoutrpc laystat=%d err=%d\n",
7757 laystat = nfsrpc_layoutgetres(nmp, vp, newfhp, newfhlen,
7758 &stateid, retonclose, NULL, &lyp, &flh, layouttype, laystat,
7759 &islocked, cred, p);
7761 error = nfsrpc_openrpc(nmp, vp, nfhp, fhlen, newfhp, newfhlen,
7762 mode, op, name, namelen, dpp, 0, 0, cred, p, 0, 0);
7764 nfscl_rellayout(lyp, 1);
7765 else if (islocked == 1)
7766 nfscl_rellayout(lyp, 0);
7771 * This function does an Open+LayoutGet for an NFSv4.1 mount with pNFS
7772 * enabled, only for the CLAIM_NULL case. All other NFSv4 Opens are
7773 * handled by nfsrpc_openrpc().
7774 * For the case where op == NULL, dvp is the directory. When op != NULL, it
7778 nfsrpc_openlayoutrpc(struct nfsmount *nmp, vnode_t vp, u_int8_t *nfhp,
7779 int fhlen, uint8_t *newfhp, int newfhlen, uint32_t mode,
7780 struct nfsclopen *op, uint8_t *name, int namelen, struct nfscldeleg **dpp,
7781 nfsv4stateid_t *stateidp, int usecurstateid, int layouttype,
7782 int layoutlen, int *retonclosep, struct nfsclflayouthead *flhp,
7783 int *laystatp, struct ucred *cred, NFSPROC_T *p)
7786 struct nfsrv_descript nfsd, *nd = &nfsd;
7787 struct nfscldeleg *ndp = NULL;
7788 struct nfsvattr nfsva;
7789 struct nfsclsession *tsep;
7790 uint32_t rflags, deleg;
7791 nfsattrbit_t attrbits;
7792 int error, ret, acesize, limitby, iomode;
7796 nfscl_reqstart(nd, NFSPROC_OPENLAYGET, nmp, nfhp, fhlen, NULL, NULL,
7798 NFSM_BUILD(tl, uint32_t *, 5 * NFSX_UNSIGNED);
7799 *tl++ = txdr_unsigned(op->nfso_own->nfsow_seqid);
7800 *tl++ = txdr_unsigned(mode & NFSV4OPEN_ACCESSBOTH);
7801 *tl++ = txdr_unsigned((mode >> NFSLCK_SHIFT) & NFSV4OPEN_DENYBOTH);
7802 tsep = nfsmnt_mdssession(nmp);
7803 *tl++ = tsep->nfsess_clientid.lval[0];
7804 *tl = tsep->nfsess_clientid.lval[1];
7805 nfsm_strtom(nd, op->nfso_own->nfsow_owner, NFSV4CL_LOCKNAMELEN);
7806 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_UNSIGNED);
7807 *tl++ = txdr_unsigned(NFSV4OPEN_NOCREATE);
7808 *tl = txdr_unsigned(NFSV4OPEN_CLAIMNULL);
7809 nfsm_strtom(nd, name, namelen);
7810 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
7811 *tl = txdr_unsigned(NFSV4OP_GETATTR);
7812 NFSZERO_ATTRBIT(&attrbits);
7813 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_CHANGE);
7814 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEMODIFY);
7815 nfsrv_putattrbit(nd, &attrbits);
7816 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
7817 *tl = txdr_unsigned(NFSV4OP_LAYOUTGET);
7818 if ((mode & NFSV4OPEN_ACCESSWRITE) != 0)
7819 iomode = NFSLAYOUTIOMODE_RW;
7821 iomode = NFSLAYOUTIOMODE_READ;
7822 nfsrv_setuplayoutget(nd, iomode, 0, UINT64_MAX, 0, stateidp,
7823 layouttype, layoutlen, usecurstateid);
7824 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, vp, p, cred,
7825 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
7828 NFSCL_INCRSEQID(op->nfso_own->nfsow_seqid, nd);
7829 if (nd->nd_repstat != 0)
7830 *laystatp = nd->nd_repstat;
7831 if ((nd->nd_flag & ND_NOMOREDATA) == 0) {
7832 /* ND_NOMOREDATA will be set if the Open operation failed. */
7833 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
7835 op->nfso_stateid.seqid = *tl++;
7836 op->nfso_stateid.other[0] = *tl++;
7837 op->nfso_stateid.other[1] = *tl++;
7838 op->nfso_stateid.other[2] = *tl;
7839 rflags = fxdr_unsigned(u_int32_t, *(tl + 6));
7840 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
7843 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
7844 deleg = fxdr_unsigned(u_int32_t, *tl);
7845 if (deleg == NFSV4OPEN_DELEGATEREAD ||
7846 deleg == NFSV4OPEN_DELEGATEWRITE) {
7847 if (!(op->nfso_own->nfsow_clp->nfsc_flags &
7848 NFSCLFLAGS_FIRSTDELEG))
7849 op->nfso_own->nfsow_clp->nfsc_flags |=
7850 (NFSCLFLAGS_FIRSTDELEG | NFSCLFLAGS_GOTDELEG);
7851 ndp = malloc(sizeof(struct nfscldeleg) + newfhlen,
7852 M_NFSCLDELEG, M_WAITOK);
7853 LIST_INIT(&ndp->nfsdl_owner);
7854 LIST_INIT(&ndp->nfsdl_lock);
7855 ndp->nfsdl_clp = op->nfso_own->nfsow_clp;
7856 ndp->nfsdl_fhlen = newfhlen;
7857 NFSBCOPY(newfhp, ndp->nfsdl_fh, newfhlen);
7858 newnfs_copyincred(cred, &ndp->nfsdl_cred);
7859 nfscl_lockinit(&ndp->nfsdl_rwlock);
7860 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
7862 ndp->nfsdl_stateid.seqid = *tl++;
7863 ndp->nfsdl_stateid.other[0] = *tl++;
7864 ndp->nfsdl_stateid.other[1] = *tl++;
7865 ndp->nfsdl_stateid.other[2] = *tl++;
7866 ret = fxdr_unsigned(int, *tl);
7867 if (deleg == NFSV4OPEN_DELEGATEWRITE) {
7868 ndp->nfsdl_flags = NFSCLDL_WRITE;
7870 * Indicates how much the file can grow.
7872 NFSM_DISSECT(tl, u_int32_t *,
7874 limitby = fxdr_unsigned(int, *tl++);
7876 case NFSV4OPEN_LIMITSIZE:
7877 ndp->nfsdl_sizelimit = fxdr_hyper(tl);
7879 case NFSV4OPEN_LIMITBLOCKS:
7880 ndp->nfsdl_sizelimit =
7881 fxdr_unsigned(u_int64_t, *tl++);
7882 ndp->nfsdl_sizelimit *=
7883 fxdr_unsigned(u_int64_t, *tl);
7886 error = NFSERR_BADXDR;
7890 ndp->nfsdl_flags = NFSCLDL_READ;
7892 ndp->nfsdl_flags |= NFSCLDL_RECALL;
7893 error = nfsrv_dissectace(nd, &ndp->nfsdl_ace, false,
7897 } else if (deleg != NFSV4OPEN_DELEGATENONE) {
7898 error = NFSERR_BADXDR;
7901 if ((rflags & NFSV4OPEN_LOCKTYPEPOSIX) != 0 ||
7902 nfscl_assumeposixlocks)
7903 op->nfso_posixlock = 1;
7905 op->nfso_posixlock = 0;
7906 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
7907 /* If the 2nd element == NFS_OK, the Getattr succeeded. */
7909 error = nfsv4_loadattr(nd, NULL, &nfsva, NULL,
7910 NULL, 0, NULL, NULL, NULL, NULL, NULL, 0,
7911 NULL, NULL, NULL, p, cred);
7915 ndp->nfsdl_change = nfsva.na_filerev;
7916 ndp->nfsdl_modtime = nfsva.na_mtime;
7917 ndp->nfsdl_flags |= NFSCLDL_MODTIMESET;
7922 * At this point, the Open has succeeded, so set
7923 * nd_repstat = NFS_OK. If the Layoutget failed,
7924 * this function just won't return a layout.
7926 if (nd->nd_repstat == 0) {
7927 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
7928 *laystatp = fxdr_unsigned(int, *++tl);
7929 if (*laystatp == 0) {
7930 error = nfsrv_parselayoutget(nmp, nd,
7931 stateidp, retonclosep, flhp);
7936 nd->nd_repstat = 0; /* Return 0 for Open. */
7939 if (nd->nd_repstat != 0 && error == 0)
7940 error = nd->nd_repstat;
7942 free(ndp, M_NFSCLDELEG);
7943 m_freem(nd->nd_mrep);
7948 * Similar nfsrpc_createv4(), but also does the LayoutGet operation.
7949 * Used only for mounts with pNFS enabled.
7952 nfsrpc_createlayout(vnode_t dvp, char *name, int namelen, struct vattr *vap,
7953 nfsquad_t cverf, int fmode, struct nfsclowner *owp, struct nfscldeleg **dpp,
7954 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap,
7955 struct nfsvattr *nnap, struct nfsfh **nfhpp, int *attrflagp,
7956 int *dattrflagp, void *dstuff, int *unlockedp, nfsv4stateid_t *stateidp,
7957 int usecurstateid, int layouttype, int layoutlen, int *retonclosep,
7958 struct nfsclflayouthead *flhp, int *laystatp)
7961 int error = 0, deleg, newone, ret, acesize, limitby;
7962 struct nfsrv_descript nfsd, *nd = &nfsd;
7963 struct nfsclopen *op;
7964 struct nfscldeleg *dp = NULL;
7967 struct nfsclsession *tsep;
7968 nfsattrbit_t attrbits;
7969 nfsv4stateid_t stateid;
7970 struct nfsmount *nmp;
7972 nmp = VFSTONFS(dvp->v_mount);
7980 if (namelen > NFS_MAXNAMLEN)
7981 return (ENAMETOOLONG);
7982 NFSCL_REQSTART(nd, NFSPROC_CREATELAYGET, dvp, cred);
7984 * For V4, this is actually an Open op.
7986 NFSM_BUILD(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
7987 *tl++ = txdr_unsigned(owp->nfsow_seqid);
7988 *tl++ = txdr_unsigned(NFSV4OPEN_ACCESSWRITE |
7989 NFSV4OPEN_ACCESSREAD);
7990 *tl++ = txdr_unsigned(NFSV4OPEN_DENYNONE);
7991 tsep = nfsmnt_mdssession(nmp);
7992 *tl++ = tsep->nfsess_clientid.lval[0];
7993 *tl = tsep->nfsess_clientid.lval[1];
7994 nfsm_strtom(nd, owp->nfsow_owner, NFSV4CL_LOCKNAMELEN);
7995 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
7996 *tl++ = txdr_unsigned(NFSV4OPEN_CREATE);
7997 if ((fmode & O_EXCL) != 0) {
7998 if (NFSHASSESSPERSIST(nmp)) {
7999 /* Use GUARDED for persistent sessions. */
8000 *tl = txdr_unsigned(NFSCREATE_GUARDED);
8001 nfscl_fillsattr(nd, vap, dvp, 0, 0);
8003 /* Otherwise, use EXCLUSIVE4_1. */
8004 *tl = txdr_unsigned(NFSCREATE_EXCLUSIVE41);
8005 NFSM_BUILD(tl, u_int32_t *, NFSX_VERF);
8006 *tl++ = cverf.lval[0];
8007 *tl = cverf.lval[1];
8008 nfscl_fillsattr(nd, vap, dvp, 0, 0);
8011 *tl = txdr_unsigned(NFSCREATE_UNCHECKED);
8012 nfscl_fillsattr(nd, vap, dvp, 0, 0);
8014 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
8015 *tl = txdr_unsigned(NFSV4OPEN_CLAIMNULL);
8016 nfsm_strtom(nd, name, namelen);
8017 /* Get the new file's handle and attributes, plus save the FH. */
8018 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
8019 *tl++ = txdr_unsigned(NFSV4OP_SAVEFH);
8020 *tl++ = txdr_unsigned(NFSV4OP_GETFH);
8021 *tl = txdr_unsigned(NFSV4OP_GETATTR);
8022 NFSGETATTR_ATTRBIT(&attrbits);
8023 nfsrv_putattrbit(nd, &attrbits);
8024 /* Get the directory's post-op attributes. */
8025 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
8026 *tl = txdr_unsigned(NFSV4OP_PUTFH);
8027 (void)nfsm_fhtom(nmp, nd, np->n_fhp->nfh_fh, np->n_fhp->nfh_len, 0);
8028 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
8029 *tl = txdr_unsigned(NFSV4OP_GETATTR);
8030 nfsrv_putattrbit(nd, &attrbits);
8031 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
8032 *tl++ = txdr_unsigned(NFSV4OP_RESTOREFH);
8033 *tl = txdr_unsigned(NFSV4OP_LAYOUTGET);
8034 nfsrv_setuplayoutget(nd, NFSLAYOUTIOMODE_RW, 0, UINT64_MAX, 0, stateidp,
8035 layouttype, layoutlen, usecurstateid);
8036 error = nfscl_request(nd, dvp, p, cred, dstuff);
8039 NFSCL_DEBUG(4, "nfsrpc_createlayout stat=%d err=%d\n", nd->nd_repstat,
8041 if (nd->nd_repstat != 0)
8042 *laystatp = nd->nd_repstat;
8043 NFSCL_INCRSEQID(owp->nfsow_seqid, nd);
8044 if ((nd->nd_flag & ND_NOMOREDATA) == 0) {
8045 NFSCL_DEBUG(4, "nfsrpc_createlayout open succeeded\n");
8046 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
8048 stateid.seqid = *tl++;
8049 stateid.other[0] = *tl++;
8050 stateid.other[1] = *tl++;
8051 stateid.other[2] = *tl;
8052 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
8055 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
8056 deleg = fxdr_unsigned(int, *tl);
8057 if (deleg == NFSV4OPEN_DELEGATEREAD ||
8058 deleg == NFSV4OPEN_DELEGATEWRITE) {
8059 if (!(owp->nfsow_clp->nfsc_flags &
8060 NFSCLFLAGS_FIRSTDELEG))
8061 owp->nfsow_clp->nfsc_flags |=
8062 (NFSCLFLAGS_FIRSTDELEG | NFSCLFLAGS_GOTDELEG);
8063 dp = malloc(sizeof(struct nfscldeleg) + NFSX_V4FHMAX,
8064 M_NFSCLDELEG, M_WAITOK);
8065 LIST_INIT(&dp->nfsdl_owner);
8066 LIST_INIT(&dp->nfsdl_lock);
8067 dp->nfsdl_clp = owp->nfsow_clp;
8068 newnfs_copyincred(cred, &dp->nfsdl_cred);
8069 nfscl_lockinit(&dp->nfsdl_rwlock);
8070 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
8072 dp->nfsdl_stateid.seqid = *tl++;
8073 dp->nfsdl_stateid.other[0] = *tl++;
8074 dp->nfsdl_stateid.other[1] = *tl++;
8075 dp->nfsdl_stateid.other[2] = *tl++;
8076 ret = fxdr_unsigned(int, *tl);
8077 if (deleg == NFSV4OPEN_DELEGATEWRITE) {
8078 dp->nfsdl_flags = NFSCLDL_WRITE;
8080 * Indicates how much the file can grow.
8082 NFSM_DISSECT(tl, u_int32_t *,
8084 limitby = fxdr_unsigned(int, *tl++);
8086 case NFSV4OPEN_LIMITSIZE:
8087 dp->nfsdl_sizelimit = fxdr_hyper(tl);
8089 case NFSV4OPEN_LIMITBLOCKS:
8090 dp->nfsdl_sizelimit =
8091 fxdr_unsigned(u_int64_t, *tl++);
8092 dp->nfsdl_sizelimit *=
8093 fxdr_unsigned(u_int64_t, *tl);
8096 error = NFSERR_BADXDR;
8100 dp->nfsdl_flags = NFSCLDL_READ;
8103 dp->nfsdl_flags |= NFSCLDL_RECALL;
8104 error = nfsrv_dissectace(nd, &dp->nfsdl_ace, false,
8108 } else if (deleg != NFSV4OPEN_DELEGATENONE) {
8109 error = NFSERR_BADXDR;
8113 /* Now, we should have the status for the SaveFH. */
8114 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
8116 NFSCL_DEBUG(4, "nfsrpc_createlayout SaveFH ok\n");
8118 * Now, process the GetFH and Getattr for the newly
8119 * created file. nfscl_mtofh() will set
8120 * ND_NOMOREDATA if these weren't successful.
8122 error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp);
8123 NFSCL_DEBUG(4, "aft nfscl_mtofh err=%d\n", error);
8127 nd->nd_flag |= ND_NOMOREDATA;
8128 /* Now we have the PutFH and Getattr for the directory. */
8129 if ((nd->nd_flag & ND_NOMOREDATA) == 0) {
8130 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
8132 nd->nd_flag |= ND_NOMOREDATA;
8134 NFSM_DISSECT(tl, uint32_t *, 2 *
8137 nd->nd_flag |= ND_NOMOREDATA;
8140 if ((nd->nd_flag & ND_NOMOREDATA) == 0) {
8141 /* Load the directory attributes. */
8142 error = nfsm_loadattr(nd, dnap);
8143 NFSCL_DEBUG(4, "aft nfsm_loadattr err=%d\n", error);
8147 if (dp != NULL && *attrflagp != 0) {
8148 dp->nfsdl_change = nnap->na_filerev;
8149 dp->nfsdl_modtime = nnap->na_mtime;
8150 dp->nfsdl_flags |= NFSCLDL_MODTIMESET;
8153 * We can now complete the Open state.
8157 dp->nfsdl_fhlen = nfhp->nfh_len;
8158 NFSBCOPY(nfhp->nfh_fh, dp->nfsdl_fh,
8162 * Get an Open structure that will be
8163 * attached to the OpenOwner, acquired already.
8165 error = nfscl_open(dvp, nfhp->nfh_fh, nfhp->nfh_len,
8166 (NFSV4OPEN_ACCESSWRITE | NFSV4OPEN_ACCESSREAD), 0,
8167 cred, p, NULL, &op, &newone, NULL, 0, false);
8170 op->nfso_stateid = stateid;
8171 newnfs_copyincred(cred, &op->nfso_cred);
8173 nfscl_openrelease(nmp, op, error, newone);
8176 /* Now, handle the RestoreFH and LayoutGet. */
8177 if (nd->nd_repstat == 0) {
8178 NFSM_DISSECT(tl, uint32_t *, 4 * NFSX_UNSIGNED);
8179 *laystatp = fxdr_unsigned(int, *(tl + 3));
8180 if (*laystatp == 0) {
8181 error = nfsrv_parselayoutget(nmp, nd,
8182 stateidp, retonclosep, flhp);
8186 NFSCL_DEBUG(4, "aft nfsrv_parselayout err=%d\n",
8192 if (nd->nd_repstat != 0 && error == 0)
8193 error = nd->nd_repstat;
8194 if (error == NFSERR_STALECLIENTID)
8195 nfscl_initiate_recovery(owp->nfsow_clp);
8197 NFSCL_DEBUG(4, "eo nfsrpc_createlayout err=%d\n", error);
8201 free(dp, M_NFSCLDELEG);
8202 m_freem(nd->nd_mrep);
8207 * Similar to nfsrpc_getopenlayout(), except that it used for the Create case.
8210 nfsrpc_getcreatelayout(vnode_t dvp, char *name, int namelen, struct vattr *vap,
8211 nfsquad_t cverf, int fmode, struct nfsclowner *owp, struct nfscldeleg **dpp,
8212 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap,
8213 struct nfsvattr *nnap, struct nfsfh **nfhpp, int *attrflagp,
8214 int *dattrflagp, void *dstuff, int *unlockedp)
8216 struct nfscllayout *lyp;
8217 struct nfsclflayouthead flh;
8219 struct nfsclsession *tsep;
8220 struct nfsmount *nmp;
8221 nfsv4stateid_t stateid;
8222 int error, layoutlen, layouttype, retonclose, laystat;
8225 nmp = VFSTONFS(dvp->v_mount);
8226 if (NFSHASFLEXFILE(nmp))
8227 layouttype = NFSLAYOUT_FLEXFILE;
8229 layouttype = NFSLAYOUT_NFSV4_1_FILES;
8231 tsep = nfsmnt_mdssession(nmp);
8232 layoutlen = tsep->nfsess_maxcache - (NFSX_STATEID + 3 * NFSX_UNSIGNED);
8233 error = nfsrpc_createlayout(dvp, name, namelen, vap, cverf, fmode,
8234 owp, dpp, cred, p, dnap, nnap, nfhpp, attrflagp, dattrflagp,
8235 dstuff, unlockedp, &stateid, 1, layouttype, layoutlen, &retonclose,
8237 NFSCL_DEBUG(4, "aft nfsrpc_createlayoutrpc laystat=%d err=%d\n",
8242 laystat = nfsrpc_layoutgetres(nmp, dvp, nfhp->nfh_fh,
8243 nfhp->nfh_len, &stateid, retonclose, NULL, &lyp, &flh,
8244 layouttype, laystat, NULL, cred, p);
8246 laystat = nfsrpc_layoutgetres(nmp, dvp, NULL, 0, &stateid,
8247 retonclose, NULL, &lyp, &flh, layouttype, laystat, NULL,
8250 nfscl_rellayout(lyp, 0);
8255 * Process the results of a layoutget() operation.
8258 nfsrpc_layoutgetres(struct nfsmount *nmp, vnode_t vp, uint8_t *newfhp,
8259 int newfhlen, nfsv4stateid_t *stateidp, int retonclose, uint32_t *notifybit,
8260 struct nfscllayout **lypp, struct nfsclflayouthead *flhp, int layouttype,
8261 int laystat, int *islockedp, struct ucred *cred, NFSPROC_T *p)
8263 struct nfsclflayout *tflp;
8264 struct nfscldevinfo *dip;
8268 if (laystat == NFSERR_UNKNLAYOUTTYPE) {
8270 if (!NFSHASFLEXFILE(nmp)) {
8271 /* Switch to using Flex File Layout. */
8272 nmp->nm_state |= NFSSTA_FLEXFILE;
8273 } else if (layouttype == NFSLAYOUT_FLEXFILE) {
8275 NFSCL_DEBUG(1, "disable PNFS\n");
8276 nmp->nm_state &= ~(NFSSTA_PNFS | NFSSTA_FLEXFILE);
8281 NFSCL_DEBUG(4, "nfsrpc_layoutgetres at FOREACH\n");
8282 LIST_FOREACH(tflp, flhp, nfsfl_list) {
8283 if (layouttype == NFSLAYOUT_FLEXFILE)
8284 mirrorcnt = tflp->nfsfl_mirrorcnt;
8287 for (i = 0; i < mirrorcnt; i++) {
8288 laystat = nfscl_adddevinfo(nmp, NULL, i, tflp);
8289 NFSCL_DEBUG(4, "aft adddev=%d\n", laystat);
8291 if (layouttype == NFSLAYOUT_FLEXFILE)
8292 dev = tflp->nfsfl_ffm[i].dev;
8294 dev = tflp->nfsfl_dev;
8295 laystat = nfsrpc_getdeviceinfo(nmp, dev,
8296 layouttype, notifybit, &dip, cred,
8298 NFSCL_DEBUG(4, "aft nfsrpc_gdi=%d\n",
8302 laystat = nfscl_adddevinfo(nmp, dip, i,
8305 printf("nfsrpc_layoutgetresout"
8314 * nfscl_layout() always returns with the nfsly_lock
8315 * set to a refcnt (shared lock).
8316 * Passing in dvp is sufficient, since it is only used to
8317 * get the fsid for the file system.
8319 laystat = nfscl_layout(nmp, vp, newfhp, newfhlen, stateidp,
8320 layouttype, retonclose, flhp, lypp, cred, p);
8321 NFSCL_DEBUG(4, "nfsrpc_layoutgetres: aft nfscl_layout=%d\n",
8323 if (laystat == 0 && islockedp != NULL)
8330 * nfs copy_file_range operation.
8333 nfsrpc_copy_file_range(vnode_t invp, off_t *inoffp, vnode_t outvp,
8334 off_t *outoffp, size_t *lenp, unsigned int flags, int *inattrflagp,
8335 struct nfsvattr *innap, int *outattrflagp, struct nfsvattr *outnap,
8336 struct ucred *cred, bool consecutive, bool *must_commitp)
8338 int commit, error, expireret = 0, retrycnt;
8339 u_int32_t clidrev = 0;
8340 struct nfsmount *nmp = VFSTONFS(invp->v_mount);
8341 struct nfsfh *innfhp = NULL, *outnfhp = NULL;
8342 nfsv4stateid_t instateid, outstateid;
8343 void *inlckp, *outlckp;
8345 if (nmp->nm_clp != NULL)
8346 clidrev = nmp->nm_clp->nfsc_clientidrev;
8347 innfhp = VTONFS(invp)->n_fhp;
8348 outnfhp = VTONFS(outvp)->n_fhp;
8351 /* Get both stateids. */
8353 nfscl_getstateid(invp, innfhp->nfh_fh, innfhp->nfh_len,
8354 NFSV4OPEN_ACCESSREAD, 0, NULL, curthread, &instateid,
8357 nfscl_getstateid(outvp, outnfhp->nfh_fh, outnfhp->nfh_len,
8358 NFSV4OPEN_ACCESSWRITE, 0, NULL, curthread, &outstateid,
8361 error = nfsrpc_copyrpc(invp, *inoffp, outvp, *outoffp, lenp,
8362 &instateid, &outstateid, innap, inattrflagp, outnap,
8363 outattrflagp, consecutive, &commit, cred, curthread);
8365 if (commit != NFSWRITE_FILESYNC)
8366 *must_commitp = true;
8369 } else if (error == NFSERR_STALESTATEID)
8370 nfscl_initiate_recovery(nmp->nm_clp);
8372 nfscl_lockderef(inlckp);
8373 if (outlckp != NULL)
8374 nfscl_lockderef(outlckp);
8375 if (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
8376 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
8377 error == NFSERR_OLDSTATEID || error == NFSERR_BADSESSION) {
8378 (void) nfs_catnap(PZERO, error, "nfs_cfr");
8379 } else if ((error == NFSERR_EXPIRED || (!NFSHASINT(nmp) &&
8380 error == NFSERR_BADSTATEID)) && clidrev != 0) {
8381 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev,
8383 } else if (error == NFSERR_BADSTATEID && NFSHASINT(nmp)) {
8387 } while (error == NFSERR_GRACE || error == NFSERR_DELAY ||
8388 error == NFSERR_STALESTATEID || error == NFSERR_BADSESSION ||
8389 error == NFSERR_STALEDONTRECOVER ||
8390 (error == NFSERR_OLDSTATEID && retrycnt < 20) ||
8391 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
8392 expireret == 0 && clidrev != 0 && retrycnt < 4));
8393 if (error != 0 && (retrycnt >= 4 ||
8394 error == NFSERR_STALESTATEID || error == NFSERR_BADSESSION ||
8395 error == NFSERR_STALEDONTRECOVER))
8404 nfsrpc_copyrpc(vnode_t invp, off_t inoff, vnode_t outvp, off_t outoff,
8405 size_t *lenp, nfsv4stateid_t *instateidp, nfsv4stateid_t *outstateidp,
8406 struct nfsvattr *innap, int *inattrflagp, struct nfsvattr *outnap,
8407 int *outattrflagp, bool consecutive, int *commitp, struct ucred *cred,
8412 struct nfsrv_descript nfsd;
8413 struct nfsrv_descript *nd = &nfsd;
8414 struct nfsmount *nmp;
8415 nfsattrbit_t attrbits;
8418 nmp = VFSTONFS(outvp->v_mount);
8419 *inattrflagp = *outattrflagp = 0;
8420 *commitp = NFSWRITE_UNSTABLE;
8423 if (len > nfs_maxcopyrange)
8424 len = nfs_maxcopyrange;
8425 NFSCL_REQSTART(nd, NFSPROC_COPY, invp, cred);
8426 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
8427 *tl = txdr_unsigned(NFSV4OP_GETATTR);
8428 NFSGETATTR_ATTRBIT(&attrbits);
8429 nfsrv_putattrbit(nd, &attrbits);
8430 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
8431 *tl = txdr_unsigned(NFSV4OP_PUTFH);
8432 (void)nfsm_fhtom(nmp, nd, VTONFS(outvp)->n_fhp->nfh_fh,
8433 VTONFS(outvp)->n_fhp->nfh_len, 0);
8434 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
8435 *tl = txdr_unsigned(NFSV4OP_COPY);
8436 nfsm_stateidtom(nd, instateidp, NFSSTATEID_PUTSTATEID);
8437 nfsm_stateidtom(nd, outstateidp, NFSSTATEID_PUTSTATEID);
8438 NFSM_BUILD(tl, uint32_t *, 3 * NFSX_HYPER + 4 * NFSX_UNSIGNED);
8439 txdr_hyper(inoff, tl); tl += 2;
8440 txdr_hyper(outoff, tl); tl += 2;
8441 txdr_hyper(len, tl); tl += 2;
8443 *tl++ = newnfs_true;
8445 *tl++ = newnfs_false;
8446 *tl++ = newnfs_true;
8448 *tl = txdr_unsigned(NFSV4OP_GETATTR);
8449 NFSWRITEGETATTR_ATTRBIT(&attrbits);
8450 nfsrv_putattrbit(nd, &attrbits);
8451 error = nfscl_request(nd, invp, p, cred, NULL);
8454 if ((nd->nd_flag & ND_NOMOREDATA) == 0) {
8455 /* Get the input file's attributes. */
8456 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
8457 if (*(tl + 1) == 0) {
8458 error = nfsm_loadattr(nd, innap);
8463 nd->nd_flag |= ND_NOMOREDATA;
8465 /* Skip over return stat for PutFH. */
8466 if ((nd->nd_flag & ND_NOMOREDATA) == 0) {
8467 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
8469 nd->nd_flag |= ND_NOMOREDATA;
8471 /* Skip over return stat for Copy. */
8472 if ((nd->nd_flag & ND_NOMOREDATA) == 0)
8473 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
8474 if (nd->nd_repstat == 0) {
8475 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
8477 /* There should be no callback ids. */
8478 error = NFSERR_BADXDR;
8481 NFSM_DISSECT(tl, uint32_t *, NFSX_HYPER + 3 * NFSX_UNSIGNED +
8483 len = fxdr_hyper(tl); tl += 2;
8484 *commitp = fxdr_unsigned(int, *tl++);
8486 if (!NFSHASWRITEVERF(nmp)) {
8487 NFSBCOPY(tl, nmp->nm_verf, NFSX_VERF);
8488 NFSSETWRITEVERF(nmp);
8489 } else if (NFSBCMP(tl, nmp->nm_verf, NFSX_VERF)) {
8490 NFSBCOPY(tl, nmp->nm_verf, NFSX_VERF);
8491 nd->nd_repstat = NFSERR_STALEWRITEVERF;
8494 tl += (NFSX_VERF / NFSX_UNSIGNED);
8495 if (nd->nd_repstat == 0 && *++tl != newnfs_true)
8496 /* Must be a synchronous copy. */
8497 nd->nd_repstat = NFSERR_NOTSUPP;
8498 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
8499 error = nfsm_loadattr(nd, outnap);
8501 *outattrflagp = NFS_LATTR_NOSHRINK;
8502 if (nd->nd_repstat == 0)
8504 } else if (nd->nd_repstat == NFSERR_OFFLOADNOREQS) {
8506 * For the case where consecutive is not supported, but
8507 * synchronous is supported, we can try consecutive == false
8508 * by returning this error. Otherwise, return NFSERR_NOTSUPP,
8509 * since Copy cannot be done.
8511 if ((nd->nd_flag & ND_NOMOREDATA) == 0) {
8512 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
8513 if (!consecutive || *++tl == newnfs_false)
8514 nd->nd_repstat = NFSERR_NOTSUPP;
8516 nd->nd_repstat = NFSERR_BADXDR;
8519 error = nd->nd_repstat;
8521 m_freem(nd->nd_mrep);
8529 nfsrpc_seek(vnode_t vp, off_t *offp, bool *eofp, int content,
8530 struct ucred *cred, struct nfsvattr *nap, int *attrflagp)
8532 int error, expireret = 0, retrycnt;
8533 u_int32_t clidrev = 0;
8534 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
8535 struct nfsnode *np = VTONFS(vp);
8536 struct nfsfh *nfhp = NULL;
8537 nfsv4stateid_t stateid;
8540 if (nmp->nm_clp != NULL)
8541 clidrev = nmp->nm_clp->nfsc_clientidrev;
8546 nfscl_getstateid(vp, nfhp->nfh_fh, nfhp->nfh_len,
8547 NFSV4OPEN_ACCESSREAD, 0, cred, curthread, &stateid, &lckp);
8548 error = nfsrpc_seekrpc(vp, offp, &stateid, eofp, content,
8549 nap, attrflagp, cred);
8550 if (error == NFSERR_STALESTATEID)
8551 nfscl_initiate_recovery(nmp->nm_clp);
8553 nfscl_lockderef(lckp);
8554 if (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
8555 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
8556 error == NFSERR_OLDSTATEID || error == NFSERR_BADSESSION) {
8557 (void) nfs_catnap(PZERO, error, "nfs_seek");
8558 } else if ((error == NFSERR_EXPIRED || (!NFSHASINT(nmp) &&
8559 error == NFSERR_BADSTATEID)) && clidrev != 0) {
8560 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev,
8562 } else if (error == NFSERR_BADSTATEID && NFSHASINT(nmp)) {
8566 } while (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
8567 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
8568 error == NFSERR_BADSESSION ||
8569 (error == NFSERR_OLDSTATEID && retrycnt < 20) ||
8570 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
8571 expireret == 0 && clidrev != 0 && retrycnt < 4) ||
8572 (error == NFSERR_OPENMODE && retrycnt < 4));
8573 if (error && retrycnt >= 4)
8582 nfsrpc_seekrpc(vnode_t vp, off_t *offp, nfsv4stateid_t *stateidp, bool *eofp,
8583 int content, struct nfsvattr *nap, int *attrflagp, struct ucred *cred)
8587 struct nfsrv_descript nfsd;
8588 struct nfsrv_descript *nd = &nfsd;
8589 nfsattrbit_t attrbits;
8592 NFSCL_REQSTART(nd, NFSPROC_SEEK, vp, cred);
8593 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID);
8594 NFSM_BUILD(tl, uint32_t *, NFSX_HYPER + 2 * NFSX_UNSIGNED);
8595 txdr_hyper(*offp, tl); tl += 2;
8596 *tl++ = txdr_unsigned(content);
8597 *tl = txdr_unsigned(NFSV4OP_GETATTR);
8598 NFSGETATTR_ATTRBIT(&attrbits);
8599 nfsrv_putattrbit(nd, &attrbits);
8600 error = nfscl_request(nd, vp, curthread, cred, NULL);
8603 if (nd->nd_repstat == 0) {
8604 NFSM_DISSECT(tl, uint32_t *, 3 * NFSX_UNSIGNED + NFSX_HYPER);
8605 if (*tl++ == newnfs_true)
8609 *offp = fxdr_hyper(tl);
8610 /* Just skip over Getattr op status. */
8611 error = nfsm_loadattr(nd, nap);
8615 error = nd->nd_repstat;
8617 m_freem(nd->nd_mrep);
8622 * The getextattr RPC.
8625 nfsrpc_getextattr(vnode_t vp, const char *name, struct uio *uiop, ssize_t *lenp,
8626 struct nfsvattr *nap, int *attrflagp, struct ucred *cred, NFSPROC_T *p)
8630 struct nfsrv_descript nfsd;
8631 struct nfsrv_descript *nd = &nfsd;
8632 nfsattrbit_t attrbits;
8636 NFSCL_REQSTART(nd, NFSPROC_GETEXTATTR, vp, cred);
8637 nfsm_strtom(nd, name, strlen(name));
8638 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
8639 *tl = txdr_unsigned(NFSV4OP_GETATTR);
8640 NFSGETATTR_ATTRBIT(&attrbits);
8641 nfsrv_putattrbit(nd, &attrbits);
8642 error = nfscl_request(nd, vp, p, cred, NULL);
8645 if (nd->nd_repstat == 0) {
8646 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
8647 len = fxdr_unsigned(uint32_t, *tl);
8648 /* Sanity check lengths. */
8649 if (uiop != NULL && len > 0 && len <= IOSIZE_MAX &&
8650 uiop->uio_resid <= UINT32_MAX) {
8651 len2 = uiop->uio_resid;
8653 error = nfsm_mbufuio(nd, uiop, len);
8655 error = nfsm_mbufuio(nd, uiop, len2);
8658 * nfsm_mbufuio() advances to a multiple
8659 * of 4, so round up len2 as well. Then
8660 * we need to advance over the rest of
8661 * the data, rounding up the remaining
8664 len2 = NFSM_RNDUP(len2);
8665 len2 = NFSM_RNDUP(len - len2);
8667 error = nfsm_advance(nd, len2,
8671 } else if (uiop == NULL && len > 0) {
8672 /* Just wants the length and not the data. */
8673 error = nfsm_advance(nd, NFSM_RNDUP(len), -1);
8679 /* Just skip over Getattr op status. */
8680 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
8681 error = nfsm_loadattr(nd, nap);
8686 error = nd->nd_repstat;
8688 m_freem(nd->nd_mrep);
8693 * The setextattr RPC.
8696 nfsrpc_setextattr(vnode_t vp, const char *name, struct uio *uiop,
8697 struct nfsvattr *nap, int *attrflagp, struct ucred *cred, NFSPROC_T *p)
8701 struct nfsrv_descript nfsd;
8702 struct nfsrv_descript *nd = &nfsd;
8703 nfsattrbit_t attrbits;
8706 NFSCL_REQSTART(nd, NFSPROC_SETEXTATTR, vp, cred);
8707 if (uiop->uio_resid > nd->nd_maxreq) {
8708 /* nd_maxreq is set by NFSCL_REQSTART(). */
8709 m_freem(nd->nd_mreq);
8712 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
8713 *tl = txdr_unsigned(NFSV4SXATTR_EITHER);
8714 nfsm_strtom(nd, name, strlen(name));
8715 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
8716 *tl = txdr_unsigned(uiop->uio_resid);
8717 error = nfsm_uiombuf(nd, uiop, uiop->uio_resid);
8719 m_freem(nd->nd_mreq);
8722 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
8723 *tl = txdr_unsigned(NFSV4OP_GETATTR);
8724 NFSGETATTR_ATTRBIT(&attrbits);
8725 nfsrv_putattrbit(nd, &attrbits);
8726 error = nfscl_request(nd, vp, p, cred, NULL);
8729 if (nd->nd_repstat == 0) {
8730 /* Just skip over the reply and Getattr op status. */
8731 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_HYPER + 3 *
8733 error = nfsm_loadattr(nd, nap);
8738 error = nd->nd_repstat;
8740 m_freem(nd->nd_mrep);
8745 * The removeextattr RPC.
8748 nfsrpc_rmextattr(vnode_t vp, const char *name, struct nfsvattr *nap,
8749 int *attrflagp, struct ucred *cred, NFSPROC_T *p)
8753 struct nfsrv_descript nfsd;
8754 struct nfsrv_descript *nd = &nfsd;
8755 nfsattrbit_t attrbits;
8758 NFSCL_REQSTART(nd, NFSPROC_RMEXTATTR, vp, cred);
8759 nfsm_strtom(nd, name, strlen(name));
8760 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
8761 *tl = txdr_unsigned(NFSV4OP_GETATTR);
8762 NFSGETATTR_ATTRBIT(&attrbits);
8763 nfsrv_putattrbit(nd, &attrbits);
8764 error = nfscl_request(nd, vp, p, cred, NULL);
8767 if (nd->nd_repstat == 0) {
8768 /* Just skip over the reply and Getattr op status. */
8769 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_HYPER + 3 *
8771 error = nfsm_loadattr(nd, nap);
8776 error = nd->nd_repstat;
8778 m_freem(nd->nd_mrep);
8783 * The listextattr RPC.
8786 nfsrpc_listextattr(vnode_t vp, uint64_t *cookiep, struct uio *uiop,
8787 size_t *lenp, bool *eofp, struct nfsvattr *nap, int *attrflagp,
8788 struct ucred *cred, NFSPROC_T *p)
8791 int cnt, error, i, len;
8792 struct nfsrv_descript nfsd;
8793 struct nfsrv_descript *nd = &nfsd;
8794 nfsattrbit_t attrbits;
8798 NFSCL_REQSTART(nd, NFSPROC_LISTEXTATTR, vp, cred);
8799 NFSM_BUILD(tl, uint32_t *, NFSX_HYPER + 2 * NFSX_UNSIGNED);
8800 txdr_hyper(*cookiep, tl); tl += 2;
8801 *tl++ = txdr_unsigned(*lenp);
8802 *tl = txdr_unsigned(NFSV4OP_GETATTR);
8803 NFSGETATTR_ATTRBIT(&attrbits);
8804 nfsrv_putattrbit(nd, &attrbits);
8805 error = nfscl_request(nd, vp, p, cred, NULL);
8810 if (nd->nd_repstat == 0) {
8811 NFSM_DISSECT(tl, uint32_t *, NFSX_HYPER + NFSX_UNSIGNED);
8812 *cookiep = fxdr_hyper(tl); tl += 2;
8813 cnt = fxdr_unsigned(int, *tl);
8818 for (i = 0; i < cnt; i++) {
8819 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
8820 len = fxdr_unsigned(int, *tl);
8821 if (len <= 0 || len > EXTATTR_MAXNAMELEN) {
8826 error = nfsm_advance(nd, NFSM_RNDUP(len), -1);
8827 else if (uiop->uio_resid >= len + 1) {
8829 error = uiomove(&c, sizeof(c), uiop);
8831 error = nfsm_mbufuio(nd, uiop, len);
8833 error = nfsm_advance(nd, NFSM_RNDUP(len), -1);
8840 /* Get the eof and skip over the Getattr op status. */
8841 NFSM_DISSECT(tl, uint32_t *, 3 * NFSX_UNSIGNED);
8843 * *eofp is set false above, because it wasn't able to copy
8846 if (*eofp && *tl == 0)
8848 error = nfsm_loadattr(nd, nap);
8853 error = nd->nd_repstat;
8855 m_freem(nd->nd_mrep);
8860 * Split an mbuf list. For non-M_EXTPG mbufs, just use m_split().
8862 static struct mbuf *
8863 nfsm_split(struct mbuf *mp, uint64_t xfer)
8865 struct mbuf *m, *m2;
8867 int i, j, left, pgno, plen, trim;
8870 if ((mp->m_flags & M_EXTPG) == 0) {
8871 m = m_split(mp, xfer, M_WAITOK);
8875 /* Find the correct mbuf to split at. */
8876 for (m = mp; m != NULL && xfer > m->m_len; m = m->m_next)
8881 /* If xfer == m->m_len, we can just split the mbuf list. */
8882 if (xfer == m->m_len) {
8888 /* Find the page to split at. */
8893 plen = m_epg_pagelen(m, 0, m->m_epg_1st_off);
8895 plen = m_epg_pagelen(m, pgno, 0);
8900 } while (pgno < m->m_epg_npgs);
8901 if (pgno == m->m_epg_npgs)
8902 panic("nfsm_split: eroneous ext_pgs mbuf");
8904 m2 = mb_alloc_ext_pgs(M_WAITOK, mb_free_mext_pgs);
8905 m2->m_epg_flags |= EPG_FLAG_ANON;
8908 * If left < plen, allocate a new page for the new mbuf
8909 * and copy the data after left in the page to this new
8913 pg = vm_page_alloc_noobj(VM_ALLOC_WAITOK | VM_ALLOC_NODUMP |
8915 m2->m_epg_pa[0] = VM_PAGE_TO_PHYS(pg);
8918 /* Copy the data after left to the new page. */
8920 cp = (char *)(void *)PHYS_TO_DMAP(m->m_epg_pa[pgno]);
8922 cp += m->m_epg_1st_off;
8924 cp2 = (char *)(void *)PHYS_TO_DMAP(m2->m_epg_pa[0]);
8925 if (pgno == m->m_epg_npgs - 1)
8926 m2->m_epg_last_len = trim;
8928 cp2 += PAGE_SIZE - trim;
8929 m2->m_epg_1st_off = PAGE_SIZE - trim;
8930 m2->m_epg_last_len = m->m_epg_last_len;
8932 memcpy(cp2, cp, trim);
8936 m2->m_epg_last_len = m->m_epg_last_len;
8939 /* Move the pages beyond pgno to the new mbuf. */
8940 for (i = pgno + 1, j = m2->m_epg_npgs; i < m->m_epg_npgs; i++, j++) {
8941 m2->m_epg_pa[j] = m->m_epg_pa[i];
8942 /* Never moves page 0. */
8943 m2->m_len += m_epg_pagelen(m, i, 0);
8946 m->m_epg_npgs = pgno + 1;
8947 m->m_epg_last_len = left;
8950 m2->m_next = m->m_next;
8956 * Do the NFSv4.1 Bind Connection to Session.
8957 * Called from the reconnect layer of the krpc (sys/rpc/clnt_rc.c).
8960 nfsrpc_bindconnsess(CLIENT *cl, void *arg, struct ucred *cr)
8962 struct nfscl_reconarg *rcp = (struct nfscl_reconarg *)arg;
8964 struct nfsrv_descript nfsd;
8965 struct nfsrv_descript *nd = &nfsd;
8966 struct rpc_callextra ext;
8967 struct timeval utimeout;
8968 enum clnt_stat stat;
8971 nfscl_reqstart(nd, NFSPROC_BINDCONNTOSESS, NULL, NULL, 0, NULL, NULL,
8972 NFS_VER4, rcp->minorvers, NULL);
8973 NFSM_BUILD(tl, uint32_t *, NFSX_V4SESSIONID + 2 * NFSX_UNSIGNED);
8974 memcpy(tl, rcp->sessionid, NFSX_V4SESSIONID);
8975 tl += NFSX_V4SESSIONID / NFSX_UNSIGNED;
8976 *tl++ = txdr_unsigned(NFSCDFC4_FORE_OR_BOTH);
8979 memset(&ext, 0, sizeof(ext));
8980 utimeout.tv_sec = 30;
8981 utimeout.tv_usec = 0;
8982 ext.rc_auth = authunix_create(cr);
8984 stat = CLNT_CALL_MBUF(cl, &ext, NFSV4PROC_COMPOUND, nd->nd_mreq,
8985 &nd->nd_mrep, utimeout);
8986 AUTH_DESTROY(ext.rc_auth);
8987 if (stat != RPC_SUCCESS) {
8988 printf("nfsrpc_bindconnsess: call failed stat=%d\n", stat);
8991 if (nd->nd_mrep == NULL) {
8992 printf("nfsrpc_bindconnsess: no reply args\n");
8996 newnfs_realign(&nd->nd_mrep, M_WAITOK);
8997 nd->nd_md = nd->nd_mrep;
8998 nd->nd_dpos = mtod(nd->nd_md, char *);
8999 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
9000 nd->nd_repstat = fxdr_unsigned(uint32_t, *tl++);
9001 if (nd->nd_repstat == NFSERR_OK) {
9002 res = fxdr_unsigned(uint32_t, *tl);
9003 if (res > 0 && (error = nfsm_advance(nd, NFSM_RNDUP(res),
9006 NFSM_DISSECT(tl, uint32_t *, NFSX_V4SESSIONID +
9009 if (!NFSBCMP(tl, rcp->sessionid, NFSX_V4SESSIONID)) {
9010 tl += NFSX_V4SESSIONID / NFSX_UNSIGNED;
9011 res = fxdr_unsigned(uint32_t, *tl);
9012 if (res != NFSCDFS4_BOTH)
9013 printf("nfsrpc_bindconnsess: did not "
9014 "return FS4_BOTH\n");
9016 printf("nfsrpc_bindconnsess: not same "
9018 } else if (nd->nd_repstat != NFSERR_BADSESSION)
9019 printf("nfsrpc_bindconnsess: returned %d\n", nd->nd_repstat);
9022 printf("nfsrpc_bindconnsess: reply bad xdr\n");
9023 m_freem(nd->nd_mrep);
9027 * Do roughly what nfs_statfs() does for NFSv4, but when called with a shared
9031 nfscl_statfs(struct vnode *vp, struct ucred *cred, NFSPROC_T *td)
9033 struct nfsvattr nfsva;
9034 struct nfsfsinfo fs;
9035 struct nfsstatfs sb;
9037 struct nfsmount *nmp;
9039 int attrflag, error;
9043 error = nfsrpc_statfs(vp, &sb, &fs, &lease, cred, td, &nfsva,
9046 nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 1);
9049 if (nmp->nm_clp != NULL)
9050 nmp->nm_clp->nfsc_renew = NFSCL_RENEW(lease);
9052 mtx_lock(&nmp->nm_mtx);
9053 nfscl_loadfsinfo(nmp, &fs);
9054 nfscl_loadsbinfo(nmp, &sb, &mp->mnt_stat);
9055 mp->mnt_stat.f_iosize = newnfs_iosize(nmp);
9056 mtx_unlock(&nmp->nm_mtx);