2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Rick Macklem at The University of Guelph.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
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.
46 #include "opt_inet6.h"
48 #include <fs/nfs/nfsport.h>
49 #include <sys/sysctl.h>
50 #include <sys/taskqueue.h>
52 SYSCTL_DECL(_vfs_nfs);
54 static int nfsignore_eexist = 0;
55 SYSCTL_INT(_vfs_nfs, OID_AUTO, ignore_eexist, CTLFLAG_RW,
56 &nfsignore_eexist, 0, "NFS ignore EEXIST replies for mkdir/symlink");
61 extern int nfs_numnfscbd;
62 extern struct timeval nfsboottime;
63 extern u_int32_t newnfs_false, newnfs_true;
64 extern nfstype nfsv34_type[9];
65 extern int nfsrv_useacl;
66 extern char nfsv4_callbackaddr[INET6_ADDRSTRLEN];
67 extern int nfscl_debuglevel;
68 extern int nfs_pnfsiothreads;
70 int nfstest_outofseq = 0;
71 int nfscl_assumeposixlocks = 1;
72 int nfscl_enablecallb = 0;
73 short nfsv4_cbport = NFSV4_CBPORT;
74 int nfstest_openallsetattr = 0;
75 #endif /* !APPLEKEXT */
77 #define DIRHDSIZ offsetof(struct dirent, d_name)
80 * nfscl_getsameserver() can return one of three values:
81 * NFSDSP_USETHISSESSION - Use this session for the DS.
82 * NFSDSP_SEQTHISSESSION - Use the nfsclds_sequence field of this dsp for new
84 * NFSDSP_NOTFOUND - No matching server was found.
87 NFSDSP_USETHISSESSION = 0,
88 NFSDSP_SEQTHISSESSION = 1,
93 * Do a write RPC on a DS data file, using this structure for the arguments,
94 * so that this function can be executed by a separate kernel process.
96 struct nfsclwritedsdorpc {
103 nfsv4stateid_t *stateidp;
116 static int nfsrpc_setattrrpc(vnode_t , struct vattr *, nfsv4stateid_t *,
117 struct ucred *, NFSPROC_T *, struct nfsvattr *, int *, void *);
118 static int nfsrpc_readrpc(vnode_t , struct uio *, struct ucred *,
119 nfsv4stateid_t *, NFSPROC_T *, struct nfsvattr *, int *, void *);
120 static int nfsrpc_writerpc(vnode_t , struct uio *, int *, int *,
121 struct ucred *, nfsv4stateid_t *, NFSPROC_T *, struct nfsvattr *, int *,
123 static int nfsrpc_createv23(vnode_t , char *, int, struct vattr *,
124 nfsquad_t, int, struct ucred *, NFSPROC_T *, struct nfsvattr *,
125 struct nfsvattr *, struct nfsfh **, int *, int *, void *);
126 static int nfsrpc_createv4(vnode_t , char *, int, struct vattr *,
127 nfsquad_t, int, struct nfsclowner *, struct nfscldeleg **, struct ucred *,
128 NFSPROC_T *, struct nfsvattr *, struct nfsvattr *, struct nfsfh **, int *,
129 int *, void *, int *);
130 static int nfsrpc_locku(struct nfsrv_descript *, struct nfsmount *,
131 struct nfscllockowner *, u_int64_t, u_int64_t,
132 u_int32_t, struct ucred *, NFSPROC_T *, int);
133 static int nfsrpc_setaclrpc(vnode_t, struct ucred *, NFSPROC_T *,
134 struct acl *, nfsv4stateid_t *, void *);
135 static int nfsrpc_getlayout(struct nfsmount *, vnode_t, struct nfsfh *, int,
136 uint32_t *, nfsv4stateid_t *, uint64_t, struct nfscllayout **,
137 struct ucred *, NFSPROC_T *);
138 static int nfsrpc_fillsa(struct nfsmount *, struct sockaddr_in *,
139 struct sockaddr_in6 *, sa_family_t, int, struct nfsclds **, NFSPROC_T *);
140 static void nfscl_initsessionslots(struct nfsclsession *);
141 static int nfscl_doflayoutio(vnode_t, struct uio *, int *, int *, int *,
142 nfsv4stateid_t *, int, struct nfscldevinfo *, struct nfscllayout *,
143 struct nfsclflayout *, uint64_t, uint64_t, int, struct ucred *,
145 static int nfscl_dofflayoutio(vnode_t, struct uio *, int *, int *, int *,
146 nfsv4stateid_t *, int, struct nfscldevinfo *, struct nfscllayout *,
147 struct nfsclflayout *, uint64_t, uint64_t, int, int, struct mbuf *,
148 struct nfsclwritedsdorpc *, struct ucred *, NFSPROC_T *);
149 static struct mbuf *nfsm_copym(struct mbuf *, int, int);
150 static int nfsrpc_readds(vnode_t, struct uio *, nfsv4stateid_t *, int *,
151 struct nfsclds *, uint64_t, int, struct nfsfh *, int, int, int,
152 struct ucred *, NFSPROC_T *);
153 static int nfsrpc_writeds(vnode_t, struct uio *, int *, int *,
154 nfsv4stateid_t *, struct nfsclds *, uint64_t, int,
155 struct nfsfh *, int, int, int, int, struct ucred *, NFSPROC_T *);
156 static int nfsio_writedsmir(vnode_t, int *, int *, nfsv4stateid_t *,
157 struct nfsclds *, uint64_t, int, struct nfsfh *, struct mbuf *, int, int,
158 struct nfsclwritedsdorpc *, struct ucred *, NFSPROC_T *);
159 static int nfsrpc_writedsmir(vnode_t, int *, int *, nfsv4stateid_t *,
160 struct nfsclds *, uint64_t, int, struct nfsfh *, struct mbuf *, int, int,
161 struct ucred *, NFSPROC_T *);
162 static enum nfsclds_state nfscl_getsameserver(struct nfsmount *,
163 struct nfsclds *, struct nfsclds **);
164 static int nfsio_commitds(vnode_t, uint64_t, int, struct nfsclds *,
165 struct nfsfh *, int, int, struct nfsclwritedsdorpc *, struct ucred *,
167 static int nfsrpc_commitds(vnode_t, uint64_t, int, struct nfsclds *,
168 struct nfsfh *, int, int, struct ucred *, NFSPROC_T *);
169 static void nfsrv_setuplayoutget(struct nfsrv_descript *, int, uint64_t,
170 uint64_t, uint64_t, nfsv4stateid_t *, int, int, int);
171 static int nfsrv_parseug(struct nfsrv_descript *, int, uid_t *, gid_t *,
173 static int nfsrv_parselayoutget(struct nfsrv_descript *, nfsv4stateid_t *,
174 int *, struct nfsclflayouthead *);
175 static int nfsrpc_getopenlayout(struct nfsmount *, vnode_t, u_int8_t *,
176 int, uint8_t *, int, uint32_t, struct nfsclopen *, uint8_t *, int,
177 struct nfscldeleg **, struct ucred *, NFSPROC_T *);
178 static int nfsrpc_getcreatelayout(vnode_t, char *, int, struct vattr *,
179 nfsquad_t, int, struct nfsclowner *, struct nfscldeleg **,
180 struct ucred *, NFSPROC_T *, struct nfsvattr *, struct nfsvattr *,
181 struct nfsfh **, int *, int *, void *, int *);
182 static int nfsrpc_openlayoutrpc(struct nfsmount *, vnode_t, u_int8_t *,
183 int, uint8_t *, int, uint32_t, struct nfsclopen *, uint8_t *, int,
184 struct nfscldeleg **, nfsv4stateid_t *, int, int, int, int *,
185 struct nfsclflayouthead *, int *, struct ucred *, NFSPROC_T *);
186 static int nfsrpc_createlayout(vnode_t, char *, int, struct vattr *,
187 nfsquad_t, int, struct nfsclowner *, struct nfscldeleg **,
188 struct ucred *, NFSPROC_T *, struct nfsvattr *, struct nfsvattr *,
189 struct nfsfh **, int *, int *, void *, int *, nfsv4stateid_t *,
190 int, int, int, int *, struct nfsclflayouthead *, int *);
191 static int nfsrpc_layoutget(struct nfsmount *, uint8_t *, int, int, uint64_t,
192 uint64_t, uint64_t, int, int, nfsv4stateid_t *, int *,
193 struct nfsclflayouthead *, struct ucred *, NFSPROC_T *, void *);
194 static int nfsrpc_layoutgetres(struct nfsmount *, vnode_t, uint8_t *,
195 int, nfsv4stateid_t *, int, uint32_t *, struct nfscllayout **,
196 struct nfsclflayouthead *, int, int, int *, struct ucred *, NFSPROC_T *);
198 int nfs_pnfsio(task_fn_t *, void *);
201 * nfs null call from vfs.
204 nfsrpc_null(vnode_t vp, struct ucred *cred, NFSPROC_T *p)
207 struct nfsrv_descript nfsd, *nd = &nfsd;
209 NFSCL_REQSTART(nd, NFSPROC_NULL, vp);
210 error = nfscl_request(nd, vp, p, cred, NULL);
211 if (nd->nd_repstat && !error)
212 error = nd->nd_repstat;
213 mbuf_freem(nd->nd_mrep);
219 * For nfs version 3 and 4, use the access rpc to check accessibility. If file
220 * modes are changed on the server, accesses might still fail later.
223 nfsrpc_access(vnode_t vp, int acmode, struct ucred *cred,
224 NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp)
227 u_int32_t mode, rmode;
230 mode = NFSACCESS_READ;
233 if (vnode_vtype(vp) == VDIR) {
235 mode |= (NFSACCESS_MODIFY | NFSACCESS_EXTEND |
238 mode |= NFSACCESS_LOOKUP;
241 mode |= (NFSACCESS_MODIFY | NFSACCESS_EXTEND);
243 mode |= NFSACCESS_EXECUTE;
247 * Now, just call nfsrpc_accessrpc() to do the actual RPC.
249 error = nfsrpc_accessrpc(vp, mode, cred, p, nap, attrflagp, &rmode,
253 * The NFS V3 spec does not clarify whether or not
254 * the returned access bits can be a superset of
255 * the ones requested, so...
257 if (!error && (rmode & mode) != mode)
263 * The actual rpc, separated out for Darwin.
266 nfsrpc_accessrpc(vnode_t vp, u_int32_t mode, struct ucred *cred,
267 NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, u_int32_t *rmodep,
271 u_int32_t supported, rmode;
273 struct nfsrv_descript nfsd, *nd = &nfsd;
274 nfsattrbit_t attrbits;
278 NFSCL_REQSTART(nd, NFSPROC_ACCESS, vp);
279 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
280 *tl = txdr_unsigned(mode);
281 if (nd->nd_flag & ND_NFSV4) {
283 * And do a Getattr op.
285 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
286 *tl = txdr_unsigned(NFSV4OP_GETATTR);
287 NFSGETATTR_ATTRBIT(&attrbits);
288 (void) nfsrv_putattrbit(nd, &attrbits);
290 error = nfscl_request(nd, vp, p, cred, stuff);
293 if (nd->nd_flag & ND_NFSV3) {
294 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
298 if (!nd->nd_repstat) {
299 if (nd->nd_flag & ND_NFSV4) {
300 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
301 supported = fxdr_unsigned(u_int32_t, *tl++);
303 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
305 rmode = fxdr_unsigned(u_int32_t, *tl);
306 if (nd->nd_flag & ND_NFSV4)
307 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
310 * It's not obvious what should be done about
311 * unsupported access modes. For now, be paranoid
312 * and clear the unsupported ones.
317 error = nd->nd_repstat;
319 mbuf_freem(nd->nd_mrep);
327 nfsrpc_open(vnode_t vp, int amode, struct ucred *cred, NFSPROC_T *p)
329 struct nfsclopen *op;
330 struct nfscldeleg *dp;
332 struct nfsnode *np = VTONFS(vp);
333 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
334 u_int32_t mode, clidrev;
335 int ret, newone, error, expireret = 0, retrycnt;
338 * For NFSv4, Open Ops are only done on Regular Files.
340 if (vnode_vtype(vp) != VREG)
344 mode |= NFSV4OPEN_ACCESSREAD;
346 mode |= NFSV4OPEN_ACCESSWRITE;
351 { char name[100]; int namel;
352 namel = (np->n_v4->n4_namelen < 100) ? np->n_v4->n4_namelen : 99;
353 bcopy(NFS4NODENAME(np->n_v4), name, namel);
355 printf("rpcopen p=0x%x name=%s",p->p_pid,name);
356 if (nfhp->nfh_len > 0) printf(" fh=0x%x\n",nfhp->nfh_fh[12]);
357 else printf(" fhl=0\n");
362 error = nfscl_open(vp, nfhp->nfh_fh, nfhp->nfh_len, mode, 1,
363 cred, p, NULL, &op, &newone, &ret, 1);
367 if (nmp->nm_clp != NULL)
368 clidrev = nmp->nm_clp->nfsc_clientidrev;
371 if (ret == NFSCLOPEN_DOOPEN) {
372 if (np->n_v4 != NULL) {
374 * For the first attempt, try and get a layout, if
375 * pNFS is enabled for the mount.
377 if (!NFSHASPNFS(nmp) || nfscl_enablecallb == 0 ||
378 nfs_numnfscbd == 0 ||
379 (np->n_flag & NNOLAYOUT) != 0 || retrycnt > 0)
380 error = nfsrpc_openrpc(nmp, vp,
382 np->n_v4->n4_fhlen, np->n_fhp->nfh_fh,
383 np->n_fhp->nfh_len, mode, op,
384 NFS4NODENAME(np->n_v4),
385 np->n_v4->n4_namelen,
386 &dp, 0, 0x0, cred, p, 0, 0);
388 error = nfsrpc_getopenlayout(nmp, vp,
390 np->n_v4->n4_fhlen, np->n_fhp->nfh_fh,
391 np->n_fhp->nfh_len, mode, op,
392 NFS4NODENAME(np->n_v4),
393 np->n_v4->n4_namelen, &dp, cred, p);
396 OSBitAndAtomic((int32_t)~NDELEGMOD, (UInt32 *)&np->n_flag);
399 np->n_flag &= ~NDELEGMOD;
401 * Invalidate the attribute cache, so that
402 * attributes that pre-date the issue of a
403 * delegation are not cached, since the
404 * cached attributes will remain valid while
405 * the delegation is held.
407 NFSINVALATTRCACHE(np);
410 (void) nfscl_deleg(nmp->nm_mountp,
411 op->nfso_own->nfsow_clp,
412 nfhp->nfh_fh, nfhp->nfh_len, cred, p, &dp);
417 newnfs_copyincred(cred, &op->nfso_cred);
418 } else if (ret == NFSCLOPEN_SETCRED)
420 * This is a new local open on a delegation. It needs
421 * to have credentials so that an open can be done
422 * against the server during recovery.
424 newnfs_copyincred(cred, &op->nfso_cred);
427 * nfso_opencnt is the count of how many VOP_OPEN()s have
428 * been done on this Open successfully and a VOP_CLOSE()
429 * is expected for each of these.
430 * If error is non-zero, don't increment it, since the Open
431 * hasn't succeeded yet.
435 nfscl_openrelease(nmp, op, error, newone);
436 if (error == NFSERR_GRACE || error == NFSERR_STALECLIENTID ||
437 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
438 error == NFSERR_BADSESSION) {
439 (void) nfs_catnap(PZERO, error, "nfs_open");
440 } else if ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID)
442 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
445 } while (error == NFSERR_GRACE || error == NFSERR_STALECLIENTID ||
446 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
447 error == NFSERR_BADSESSION ||
448 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
449 expireret == 0 && clidrev != 0 && retrycnt < 4));
450 if (error && retrycnt >= 4)
456 * the actual open rpc
459 nfsrpc_openrpc(struct nfsmount *nmp, vnode_t vp, u_int8_t *nfhp, int fhlen,
460 u_int8_t *newfhp, int newfhlen, u_int32_t mode, struct nfsclopen *op,
461 u_int8_t *name, int namelen, struct nfscldeleg **dpp,
462 int reclaim, u_int32_t delegtype, struct ucred *cred, NFSPROC_T *p,
463 int syscred, int recursed)
466 struct nfsrv_descript nfsd, *nd = &nfsd;
467 struct nfscldeleg *dp, *ndp = NULL;
468 struct nfsvattr nfsva;
469 u_int32_t rflags, deleg;
470 nfsattrbit_t attrbits;
471 int error, ret, acesize, limitby;
472 struct nfsclsession *tsep;
476 nfscl_reqstart(nd, NFSPROC_OPEN, nmp, nfhp, fhlen, NULL, NULL, 0, 0);
477 NFSM_BUILD(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
478 *tl++ = txdr_unsigned(op->nfso_own->nfsow_seqid);
479 *tl++ = txdr_unsigned(mode & NFSV4OPEN_ACCESSBOTH);
480 *tl++ = txdr_unsigned((mode >> NFSLCK_SHIFT) & NFSV4OPEN_DENYBOTH);
481 tsep = nfsmnt_mdssession(nmp);
482 *tl++ = tsep->nfsess_clientid.lval[0];
483 *tl = tsep->nfsess_clientid.lval[1];
484 (void) nfsm_strtom(nd, op->nfso_own->nfsow_owner, NFSV4CL_LOCKNAMELEN);
485 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
486 *tl++ = txdr_unsigned(NFSV4OPEN_NOCREATE);
488 *tl = txdr_unsigned(NFSV4OPEN_CLAIMPREVIOUS);
489 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
490 *tl = txdr_unsigned(delegtype);
493 *tl = txdr_unsigned(NFSV4OPEN_CLAIMDELEGATECUR);
494 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID);
495 if (NFSHASNFSV4N(nmp))
498 *tl++ = dp->nfsdl_stateid.seqid;
499 *tl++ = dp->nfsdl_stateid.other[0];
500 *tl++ = dp->nfsdl_stateid.other[1];
501 *tl = dp->nfsdl_stateid.other[2];
503 *tl = txdr_unsigned(NFSV4OPEN_CLAIMNULL);
505 (void) nfsm_strtom(nd, name, namelen);
507 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
508 *tl = txdr_unsigned(NFSV4OP_GETATTR);
509 NFSZERO_ATTRBIT(&attrbits);
510 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_CHANGE);
511 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEMODIFY);
512 (void) nfsrv_putattrbit(nd, &attrbits);
514 nd->nd_flag |= ND_USEGSSNAME;
515 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, vp, p, cred,
516 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
519 NFSCL_INCRSEQID(op->nfso_own->nfsow_seqid, nd);
520 if (!nd->nd_repstat) {
521 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
523 op->nfso_stateid.seqid = *tl++;
524 op->nfso_stateid.other[0] = *tl++;
525 op->nfso_stateid.other[1] = *tl++;
526 op->nfso_stateid.other[2] = *tl;
527 rflags = fxdr_unsigned(u_int32_t, *(tl + 6));
528 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
531 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
532 deleg = fxdr_unsigned(u_int32_t, *tl);
533 if (deleg == NFSV4OPEN_DELEGATEREAD ||
534 deleg == NFSV4OPEN_DELEGATEWRITE) {
535 if (!(op->nfso_own->nfsow_clp->nfsc_flags &
536 NFSCLFLAGS_FIRSTDELEG))
537 op->nfso_own->nfsow_clp->nfsc_flags |=
538 (NFSCLFLAGS_FIRSTDELEG | NFSCLFLAGS_GOTDELEG);
539 MALLOC(ndp, struct nfscldeleg *,
540 sizeof (struct nfscldeleg) + newfhlen,
541 M_NFSCLDELEG, M_WAITOK);
542 LIST_INIT(&ndp->nfsdl_owner);
543 LIST_INIT(&ndp->nfsdl_lock);
544 ndp->nfsdl_clp = op->nfso_own->nfsow_clp;
545 ndp->nfsdl_fhlen = newfhlen;
546 NFSBCOPY(newfhp, ndp->nfsdl_fh, newfhlen);
547 newnfs_copyincred(cred, &ndp->nfsdl_cred);
548 nfscl_lockinit(&ndp->nfsdl_rwlock);
549 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
551 ndp->nfsdl_stateid.seqid = *tl++;
552 ndp->nfsdl_stateid.other[0] = *tl++;
553 ndp->nfsdl_stateid.other[1] = *tl++;
554 ndp->nfsdl_stateid.other[2] = *tl++;
555 ret = fxdr_unsigned(int, *tl);
556 if (deleg == NFSV4OPEN_DELEGATEWRITE) {
557 ndp->nfsdl_flags = NFSCLDL_WRITE;
559 * Indicates how much the file can grow.
561 NFSM_DISSECT(tl, u_int32_t *,
563 limitby = fxdr_unsigned(int, *tl++);
565 case NFSV4OPEN_LIMITSIZE:
566 ndp->nfsdl_sizelimit = fxdr_hyper(tl);
568 case NFSV4OPEN_LIMITBLOCKS:
569 ndp->nfsdl_sizelimit =
570 fxdr_unsigned(u_int64_t, *tl++);
571 ndp->nfsdl_sizelimit *=
572 fxdr_unsigned(u_int64_t, *tl);
575 error = NFSERR_BADXDR;
579 ndp->nfsdl_flags = NFSCLDL_READ;
582 ndp->nfsdl_flags |= NFSCLDL_RECALL;
583 error = nfsrv_dissectace(nd, &ndp->nfsdl_ace, &ret,
587 } else if (deleg != NFSV4OPEN_DELEGATENONE) {
588 error = NFSERR_BADXDR;
591 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
592 error = nfsv4_loadattr(nd, NULL, &nfsva, NULL,
593 NULL, 0, NULL, NULL, NULL, NULL, NULL, 0,
594 NULL, NULL, NULL, p, cred);
598 ndp->nfsdl_change = nfsva.na_filerev;
599 ndp->nfsdl_modtime = nfsva.na_mtime;
600 ndp->nfsdl_flags |= NFSCLDL_MODTIMESET;
602 if (!reclaim && (rflags & NFSV4OPEN_RESULTCONFIRM)) {
604 ret = nfsrpc_openconfirm(vp, newfhp, newfhlen, op,
606 if (ret == NFSERR_DELAY)
607 (void) nfs_catnap(PZERO, ret, "nfs_open");
608 } while (ret == NFSERR_DELAY);
611 if ((rflags & NFSV4OPEN_LOCKTYPEPOSIX) ||
612 nfscl_assumeposixlocks)
613 op->nfso_posixlock = 1;
615 op->nfso_posixlock = 0;
618 * If the server is handing out delegations, but we didn't
619 * get one because an OpenConfirm was required, try the
620 * Open again, to get a delegation. This is a harmless no-op,
621 * from a server's point of view.
623 if (!reclaim && (rflags & NFSV4OPEN_RESULTCONFIRM) &&
624 (op->nfso_own->nfsow_clp->nfsc_flags & NFSCLFLAGS_GOTDELEG)
625 && !error && dp == NULL && ndp == NULL && !recursed) {
627 ret = nfsrpc_openrpc(nmp, vp, nfhp, fhlen, newfhp,
628 newfhlen, mode, op, name, namelen, &ndp, 0, 0x0,
629 cred, p, syscred, 1);
630 if (ret == NFSERR_DELAY)
631 (void) nfs_catnap(PZERO, ret, "nfs_open2");
632 } while (ret == NFSERR_DELAY);
635 FREE((caddr_t)ndp, M_NFSCLDELEG);
638 if (ret == NFSERR_STALECLIENTID ||
639 ret == NFSERR_STALEDONTRECOVER ||
640 ret == NFSERR_BADSESSION)
645 if (nd->nd_repstat != 0 && error == 0)
646 error = nd->nd_repstat;
647 if (error == NFSERR_STALECLIENTID)
648 nfscl_initiate_recovery(op->nfso_own->nfsow_clp);
652 else if (ndp != NULL)
653 FREE((caddr_t)ndp, M_NFSCLDELEG);
654 mbuf_freem(nd->nd_mrep);
662 nfsrpc_opendowngrade(vnode_t vp, u_int32_t mode, struct nfsclopen *op,
663 struct ucred *cred, NFSPROC_T *p)
666 struct nfsrv_descript nfsd, *nd = &nfsd;
669 NFSCL_REQSTART(nd, NFSPROC_OPENDOWNGRADE, vp);
670 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID + 3 * NFSX_UNSIGNED);
671 if (NFSHASNFSV4N(VFSTONFS(vnode_mount(vp))))
674 *tl++ = op->nfso_stateid.seqid;
675 *tl++ = op->nfso_stateid.other[0];
676 *tl++ = op->nfso_stateid.other[1];
677 *tl++ = op->nfso_stateid.other[2];
678 *tl++ = txdr_unsigned(op->nfso_own->nfsow_seqid);
679 *tl++ = txdr_unsigned(mode & NFSV4OPEN_ACCESSBOTH);
680 *tl = txdr_unsigned((mode >> NFSLCK_SHIFT) & NFSV4OPEN_DENYBOTH);
681 error = nfscl_request(nd, vp, p, cred, NULL);
684 NFSCL_INCRSEQID(op->nfso_own->nfsow_seqid, nd);
685 if (!nd->nd_repstat) {
686 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID);
687 op->nfso_stateid.seqid = *tl++;
688 op->nfso_stateid.other[0] = *tl++;
689 op->nfso_stateid.other[1] = *tl++;
690 op->nfso_stateid.other[2] = *tl;
692 if (nd->nd_repstat && error == 0)
693 error = nd->nd_repstat;
694 if (error == NFSERR_STALESTATEID)
695 nfscl_initiate_recovery(op->nfso_own->nfsow_clp);
697 mbuf_freem(nd->nd_mrep);
702 * V4 Close operation.
705 nfsrpc_close(vnode_t vp, int doclose, NFSPROC_T *p)
707 struct nfsclclient *clp;
710 if (vnode_vtype(vp) != VREG)
713 error = nfscl_doclose(vp, &clp, p);
715 error = nfscl_getclose(vp, &clp);
719 nfscl_clientrelease(clp);
727 nfsrpc_doclose(struct nfsmount *nmp, struct nfsclopen *op, NFSPROC_T *p)
729 struct nfsrv_descript nfsd, *nd = &nfsd;
730 struct nfscllockowner *lp, *nlp;
731 struct nfscllock *lop, *nlop;
733 u_int64_t off = 0, len = 0;
734 u_int32_t type = NFSV4LOCKT_READ;
735 int error, do_unlock, trycnt;
737 tcred = newnfs_getcred();
738 newnfs_copycred(&op->nfso_cred, tcred);
740 * (Theoretically this could be done in the same
741 * compound as the close, but having multiple
742 * sequenced Ops in the same compound might be
743 * too scary for some servers.)
745 if (op->nfso_posixlock) {
748 type = NFSV4LOCKT_READ;
752 * Since this function is only called from VOP_INACTIVE(), no
753 * other thread will be manipulating this Open. As such, the
754 * lock lists are not being changed by other threads, so it should
755 * be safe to do this without locking.
757 LIST_FOREACH(lp, &op->nfso_lock, nfsl_list) {
759 LIST_FOREACH_SAFE(lop, &lp->nfsl_lock, nfslo_list, nlop) {
760 if (op->nfso_posixlock == 0) {
761 off = lop->nfslo_first;
762 len = lop->nfslo_end - lop->nfslo_first;
763 if (lop->nfslo_type == F_WRLCK)
764 type = NFSV4LOCKT_WRITE;
766 type = NFSV4LOCKT_READ;
771 error = nfsrpc_locku(nd, nmp, lp, off,
772 len, type, tcred, p, 0);
773 if ((nd->nd_repstat == NFSERR_GRACE ||
774 nd->nd_repstat == NFSERR_DELAY) &&
776 (void) nfs_catnap(PZERO,
779 } while ((nd->nd_repstat == NFSERR_GRACE ||
780 nd->nd_repstat == NFSERR_DELAY) &&
781 error == 0 && trycnt++ < 5);
782 if (op->nfso_posixlock)
785 nfscl_freelock(lop, 0);
788 * Do a ReleaseLockOwner.
789 * The lock owner name nfsl_owner may be used by other opens for
790 * other files but the lock_owner4 name that nfsrpc_rellockown()
791 * puts on the wire has the file handle for this file appended
792 * to it, so it can be done now.
794 (void)nfsrpc_rellockown(nmp, lp, lp->nfsl_open->nfso_fh,
795 lp->nfsl_open->nfso_fhlen, tcred, p);
799 * There could be other Opens for different files on the same
800 * OpenOwner, so locking is required.
803 nfscl_lockexcl(&op->nfso_own->nfsow_rwlock, NFSCLSTATEMUTEXPTR);
806 error = nfscl_tryclose(op, tcred, nmp, p);
807 if (error == NFSERR_GRACE)
808 (void) nfs_catnap(PZERO, error, "nfs_close");
809 } while (error == NFSERR_GRACE);
811 nfscl_lockunlock(&op->nfso_own->nfsow_rwlock);
813 LIST_FOREACH_SAFE(lp, &op->nfso_lock, nfsl_list, nlp)
814 nfscl_freelockowner(lp, 0);
815 nfscl_freeopen(op, 0);
821 * The actual Close RPC.
824 nfsrpc_closerpc(struct nfsrv_descript *nd, struct nfsmount *nmp,
825 struct nfsclopen *op, struct ucred *cred, NFSPROC_T *p,
831 nfscl_reqstart(nd, NFSPROC_CLOSE, nmp, op->nfso_fh,
832 op->nfso_fhlen, NULL, NULL, 0, 0);
833 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED + NFSX_STATEID);
834 *tl++ = txdr_unsigned(op->nfso_own->nfsow_seqid);
835 if (NFSHASNFSV4N(nmp))
838 *tl++ = op->nfso_stateid.seqid;
839 *tl++ = op->nfso_stateid.other[0];
840 *tl++ = op->nfso_stateid.other[1];
841 *tl = op->nfso_stateid.other[2];
843 nd->nd_flag |= ND_USEGSSNAME;
844 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
845 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
848 NFSCL_INCRSEQID(op->nfso_own->nfsow_seqid, nd);
849 if (nd->nd_repstat == 0)
850 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID);
851 error = nd->nd_repstat;
852 if (error == NFSERR_STALESTATEID)
853 nfscl_initiate_recovery(op->nfso_own->nfsow_clp);
855 mbuf_freem(nd->nd_mrep);
860 * V4 Open Confirm RPC.
863 nfsrpc_openconfirm(vnode_t vp, u_int8_t *nfhp, int fhlen,
864 struct nfsclopen *op, struct ucred *cred, NFSPROC_T *p)
867 struct nfsrv_descript nfsd, *nd = &nfsd;
868 struct nfsmount *nmp;
871 nmp = VFSTONFS(vnode_mount(vp));
872 if (NFSHASNFSV4N(nmp))
873 return (0); /* No confirmation for NFSv4.1. */
874 nfscl_reqstart(nd, NFSPROC_OPENCONFIRM, nmp, nfhp, fhlen, NULL, NULL,
876 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED + NFSX_STATEID);
877 *tl++ = op->nfso_stateid.seqid;
878 *tl++ = op->nfso_stateid.other[0];
879 *tl++ = op->nfso_stateid.other[1];
880 *tl++ = op->nfso_stateid.other[2];
881 *tl = txdr_unsigned(op->nfso_own->nfsow_seqid);
882 error = nfscl_request(nd, vp, p, cred, NULL);
885 NFSCL_INCRSEQID(op->nfso_own->nfsow_seqid, nd);
886 if (!nd->nd_repstat) {
887 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID);
888 op->nfso_stateid.seqid = *tl++;
889 op->nfso_stateid.other[0] = *tl++;
890 op->nfso_stateid.other[1] = *tl++;
891 op->nfso_stateid.other[2] = *tl;
893 error = nd->nd_repstat;
894 if (error == NFSERR_STALESTATEID)
895 nfscl_initiate_recovery(op->nfso_own->nfsow_clp);
897 mbuf_freem(nd->nd_mrep);
902 * Do the setclientid and setclientid confirm RPCs. Called from nfs_statfs()
903 * when a mount has just occurred and when the server replies NFSERR_EXPIRED.
906 nfsrpc_setclient(struct nfsmount *nmp, struct nfsclclient *clp, int reclaim,
907 struct ucred *cred, NFSPROC_T *p)
910 struct nfsrv_descript nfsd;
911 struct nfsrv_descript *nd = &nfsd;
912 nfsattrbit_t attrbits;
913 u_int8_t *cp = NULL, *cp2, addr[INET6_ADDRSTRLEN + 9];
915 int error, isinet6 = 0, callblen;
918 static u_int32_t rev = 0;
921 struct nfsclsession *tsep;
923 if (nfsboottime.tv_sec == 0)
924 NFSSETBOOTTIME(nfsboottime);
925 clp->nfsc_rev = rev++;
926 if (NFSHASNFSV4N(nmp)) {
928 * Either there was no previous session or the
929 * previous session has failed, so...
930 * do an ExchangeID followed by the CreateSession.
932 error = nfsrpc_exchangeid(nmp, clp, &nmp->nm_sockreq,
933 NFSV4EXCH_USEPNFSMDS | NFSV4EXCH_USENONPNFS, &dsp, cred, p);
934 NFSCL_DEBUG(1, "aft exch=%d\n", error);
936 error = nfsrpc_createsession(nmp, &dsp->nfsclds_sess,
938 dsp->nfsclds_sess.nfsess_sequenceid, 1, cred, p);
942 * The old sessions cannot be safely free'd
943 * here, since they may still be used by
947 if (TAILQ_FIRST(&nmp->nm_sess) != NULL)
948 tsep = NFSMNT_MDSSESSION(nmp);
949 TAILQ_INSERT_HEAD(&nmp->nm_sess, dsp,
952 * Wake up RPCs waiting for a slot on the
953 * old session. These will then fail with
954 * NFSERR_BADSESSION and be retried with the
955 * new session by nfsv4_setsequence().
956 * Also wakeup() processes waiting for the
960 wakeup(&tsep->nfsess_slots);
961 wakeup(&nmp->nm_sess);
964 nfscl_freenfsclds(dsp);
965 NFSCL_DEBUG(1, "aft createsess=%d\n", error);
966 if (error == 0 && reclaim == 0) {
967 error = nfsrpc_reclaimcomplete(nmp, cred, p);
968 NFSCL_DEBUG(1, "aft reclaimcomp=%d\n", error);
969 if (error == NFSERR_COMPLETEALREADY ||
970 error == NFSERR_NOTSUPP)
971 /* Ignore this error. */
978 * Allocate a single session structure for NFSv4.0, because some of
979 * the fields are used by NFSv4.0 although it doesn't do a session.
981 dsp = malloc(sizeof(struct nfsclds), M_NFSCLDS, M_WAITOK | M_ZERO);
982 mtx_init(&dsp->nfsclds_mtx, "nfsds", NULL, MTX_DEF);
983 mtx_init(&dsp->nfsclds_sess.nfsess_mtx, "nfssession", NULL, MTX_DEF);
985 TAILQ_INSERT_HEAD(&nmp->nm_sess, dsp, nfsclds_list);
986 tsep = NFSMNT_MDSSESSION(nmp);
989 nfscl_reqstart(nd, NFSPROC_SETCLIENTID, nmp, NULL, 0, NULL, NULL, 0, 0);
990 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
991 *tl++ = txdr_unsigned(nfsboottime.tv_sec);
992 *tl = txdr_unsigned(clp->nfsc_rev);
993 (void) nfsm_strtom(nd, clp->nfsc_id, clp->nfsc_idlen);
996 * set up the callback address
998 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
999 *tl = txdr_unsigned(NFS_CALLBCKPROG);
1000 callblen = strlen(nfsv4_callbackaddr);
1002 cp = nfscl_getmyip(nmp, &a6, &isinet6);
1003 if (nfscl_enablecallb && nfs_numnfscbd > 0 &&
1004 (callblen > 0 || cp != NULL)) {
1005 port = htons(nfsv4_cbport);
1006 cp2 = (u_int8_t *)&port;
1008 if ((callblen > 0 &&
1009 strchr(nfsv4_callbackaddr, ':')) || isinet6) {
1010 char ip6buf[INET6_ADDRSTRLEN], *ip6add;
1012 (void) nfsm_strtom(nd, "tcp6", 4);
1013 if (callblen == 0) {
1014 ip6_sprintf(ip6buf, (struct in6_addr *)cp);
1017 ip6add = nfsv4_callbackaddr;
1019 snprintf(addr, INET6_ADDRSTRLEN + 9, "%s.%d.%d",
1020 ip6add, cp2[0], cp2[1]);
1024 (void) nfsm_strtom(nd, "tcp", 3);
1026 snprintf(addr, INET6_ADDRSTRLEN + 9,
1027 "%d.%d.%d.%d.%d.%d", cp[0], cp[1],
1028 cp[2], cp[3], cp2[0], cp2[1]);
1030 snprintf(addr, INET6_ADDRSTRLEN + 9,
1031 "%s.%d.%d", nfsv4_callbackaddr,
1034 (void) nfsm_strtom(nd, addr, strlen(addr));
1036 (void) nfsm_strtom(nd, "tcp", 3);
1037 (void) nfsm_strtom(nd, "0.0.0.0.0.0", 11);
1039 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1040 *tl = txdr_unsigned(clp->nfsc_cbident);
1041 nd->nd_flag |= ND_USEGSSNAME;
1042 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
1043 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
1046 if (nd->nd_repstat == 0) {
1047 NFSM_DISSECT(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1048 tsep->nfsess_clientid.lval[0] = *tl++;
1049 tsep->nfsess_clientid.lval[1] = *tl++;
1050 confirm.lval[0] = *tl++;
1051 confirm.lval[1] = *tl;
1052 mbuf_freem(nd->nd_mrep);
1058 nfscl_reqstart(nd, NFSPROC_SETCLIENTIDCFRM, nmp, NULL, 0, NULL,
1060 NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1061 *tl++ = tsep->nfsess_clientid.lval[0];
1062 *tl++ = tsep->nfsess_clientid.lval[1];
1063 *tl++ = confirm.lval[0];
1064 *tl = confirm.lval[1];
1065 nd->nd_flag |= ND_USEGSSNAME;
1066 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p,
1067 cred, NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
1070 mbuf_freem(nd->nd_mrep);
1072 if (nd->nd_repstat == 0) {
1073 nfscl_reqstart(nd, NFSPROC_GETATTR, nmp, nmp->nm_fh,
1074 nmp->nm_fhsize, NULL, NULL, 0, 0);
1075 NFSZERO_ATTRBIT(&attrbits);
1076 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_LEASETIME);
1077 (void) nfsrv_putattrbit(nd, &attrbits);
1078 nd->nd_flag |= ND_USEGSSNAME;
1079 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p,
1080 cred, NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
1083 if (nd->nd_repstat == 0) {
1084 error = nfsv4_loadattr(nd, NULL, NULL, NULL, NULL, 0, NULL,
1085 NULL, NULL, NULL, NULL, 0, NULL, &lease, NULL, p, cred);
1088 clp->nfsc_renew = NFSCL_RENEW(lease);
1089 clp->nfsc_expire = NFSD_MONOSEC + clp->nfsc_renew;
1090 clp->nfsc_clientidrev++;
1091 if (clp->nfsc_clientidrev == 0)
1092 clp->nfsc_clientidrev++;
1096 error = nd->nd_repstat;
1098 mbuf_freem(nd->nd_mrep);
1106 nfsrpc_getattr(vnode_t vp, struct ucred *cred, NFSPROC_T *p,
1107 struct nfsvattr *nap, void *stuff)
1109 struct nfsrv_descript nfsd, *nd = &nfsd;
1111 nfsattrbit_t attrbits;
1113 NFSCL_REQSTART(nd, NFSPROC_GETATTR, vp);
1114 if (nd->nd_flag & ND_NFSV4) {
1115 NFSGETATTR_ATTRBIT(&attrbits);
1116 (void) nfsrv_putattrbit(nd, &attrbits);
1118 error = nfscl_request(nd, vp, p, cred, stuff);
1121 if (!nd->nd_repstat)
1122 error = nfsm_loadattr(nd, nap);
1124 error = nd->nd_repstat;
1125 mbuf_freem(nd->nd_mrep);
1130 * nfs getattr call with non-vnode arguemnts.
1133 nfsrpc_getattrnovp(struct nfsmount *nmp, u_int8_t *fhp, int fhlen, int syscred,
1134 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, u_int64_t *xidp,
1137 struct nfsrv_descript nfsd, *nd = &nfsd;
1138 int error, vers = NFS_VER2;
1139 nfsattrbit_t attrbits;
1141 nfscl_reqstart(nd, NFSPROC_GETATTR, nmp, fhp, fhlen, NULL, NULL, 0, 0);
1142 if (nd->nd_flag & ND_NFSV4) {
1144 NFSGETATTR_ATTRBIT(&attrbits);
1145 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_LEASETIME);
1146 (void) nfsrv_putattrbit(nd, &attrbits);
1147 } else if (nd->nd_flag & ND_NFSV3) {
1151 nd->nd_flag |= ND_USEGSSNAME;
1152 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
1153 NFS_PROG, vers, NULL, 1, xidp, NULL);
1156 if (nd->nd_repstat == 0) {
1157 if ((nd->nd_flag & ND_NFSV4) != 0)
1158 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0,
1159 NULL, NULL, NULL, NULL, NULL, 0, NULL, leasep, NULL,
1162 error = nfsm_loadattr(nd, nap);
1164 error = nd->nd_repstat;
1165 mbuf_freem(nd->nd_mrep);
1170 * Do an nfs setattr operation.
1173 nfsrpc_setattr(vnode_t vp, struct vattr *vap, NFSACL_T *aclp,
1174 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *rnap, int *attrflagp,
1177 int error, expireret = 0, openerr, retrycnt;
1178 u_int32_t clidrev = 0, mode;
1179 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
1181 nfsv4stateid_t stateid;
1184 if (nmp->nm_clp != NULL)
1185 clidrev = nmp->nm_clp->nfsc_clientidrev;
1186 if (vap != NULL && NFSATTRISSET(u_quad_t, vap, va_size))
1187 mode = NFSV4OPEN_ACCESSWRITE;
1189 mode = NFSV4OPEN_ACCESSREAD;
1194 if (NFSHASNFSV4(nmp)) {
1195 nfhp = VTONFS(vp)->n_fhp;
1196 error = nfscl_getstateid(vp, nfhp->nfh_fh,
1197 nfhp->nfh_len, mode, 0, cred, p, &stateid, &lckp);
1198 if (error && vnode_vtype(vp) == VREG &&
1199 (mode == NFSV4OPEN_ACCESSWRITE ||
1200 nfstest_openallsetattr)) {
1202 * No Open stateid, so try and open the file
1205 if (mode == NFSV4OPEN_ACCESSWRITE)
1206 openerr = nfsrpc_open(vp, FWRITE, cred,
1209 openerr = nfsrpc_open(vp, FREAD, cred,
1212 (void) nfscl_getstateid(vp,
1213 nfhp->nfh_fh, nfhp->nfh_len,
1214 mode, 0, cred, p, &stateid, &lckp);
1218 error = nfsrpc_setattrrpc(vp, vap, &stateid, cred, p,
1219 rnap, attrflagp, stuff);
1221 error = nfsrpc_setaclrpc(vp, cred, p, aclp, &stateid,
1223 if (error == NFSERR_OPENMODE && mode == NFSV4OPEN_ACCESSREAD) {
1225 nmp->nm_state |= NFSSTA_OPENMODE;
1228 if (error == NFSERR_STALESTATEID)
1229 nfscl_initiate_recovery(nmp->nm_clp);
1231 nfscl_lockderef(lckp);
1233 (void) nfsrpc_close(vp, 0, p);
1234 if (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
1235 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
1236 error == NFSERR_OLDSTATEID || error == NFSERR_BADSESSION) {
1237 (void) nfs_catnap(PZERO, error, "nfs_setattr");
1238 } else if ((error == NFSERR_EXPIRED ||
1239 error == NFSERR_BADSTATEID) && clidrev != 0) {
1240 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
1243 } while (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
1244 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
1245 error == NFSERR_BADSESSION ||
1246 (error == NFSERR_OLDSTATEID && retrycnt < 20) ||
1247 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
1248 expireret == 0 && clidrev != 0 && retrycnt < 4) ||
1249 (error == NFSERR_OPENMODE && mode == NFSV4OPEN_ACCESSREAD &&
1251 if (error && retrycnt >= 4)
1257 nfsrpc_setattrrpc(vnode_t vp, struct vattr *vap,
1258 nfsv4stateid_t *stateidp, struct ucred *cred, NFSPROC_T *p,
1259 struct nfsvattr *rnap, int *attrflagp, void *stuff)
1262 struct nfsrv_descript nfsd, *nd = &nfsd;
1264 nfsattrbit_t attrbits;
1267 NFSCL_REQSTART(nd, NFSPROC_SETATTR, vp);
1268 if (nd->nd_flag & ND_NFSV4)
1269 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID);
1270 vap->va_type = vnode_vtype(vp);
1271 nfscl_fillsattr(nd, vap, vp, NFSSATTR_FULL, 0);
1272 if (nd->nd_flag & ND_NFSV3) {
1273 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1275 } else if (nd->nd_flag & ND_NFSV4) {
1276 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1277 *tl = txdr_unsigned(NFSV4OP_GETATTR);
1278 NFSGETATTR_ATTRBIT(&attrbits);
1279 (void) nfsrv_putattrbit(nd, &attrbits);
1281 error = nfscl_request(nd, vp, p, cred, stuff);
1284 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4))
1285 error = nfscl_wcc_data(nd, vp, rnap, attrflagp, NULL, stuff);
1286 if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) == ND_NFSV4 && !error)
1287 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
1288 if (!(nd->nd_flag & ND_NFSV3) && !nd->nd_repstat && !error)
1289 error = nfscl_postop_attr(nd, rnap, attrflagp, stuff);
1290 mbuf_freem(nd->nd_mrep);
1291 if (nd->nd_repstat && !error)
1292 error = nd->nd_repstat;
1300 nfsrpc_lookup(vnode_t dvp, char *name, int len, struct ucred *cred,
1301 NFSPROC_T *p, struct nfsvattr *dnap, struct nfsvattr *nap,
1302 struct nfsfh **nfhpp, int *attrflagp, int *dattrflagp, void *stuff)
1305 struct nfsrv_descript nfsd, *nd = &nfsd;
1306 struct nfsmount *nmp;
1309 nfsattrbit_t attrbits;
1310 int error = 0, lookupp = 0;
1314 if (vnode_vtype(dvp) != VDIR)
1316 nmp = VFSTONFS(vnode_mount(dvp));
1317 if (len > NFS_MAXNAMLEN)
1318 return (ENAMETOOLONG);
1319 if (NFSHASNFSV4(nmp) && len == 1 &&
1322 * Just return the current dir's fh.
1325 MALLOC(nfhp, struct nfsfh *, sizeof (struct nfsfh) +
1326 np->n_fhp->nfh_len, M_NFSFH, M_WAITOK);
1327 nfhp->nfh_len = np->n_fhp->nfh_len;
1328 NFSBCOPY(np->n_fhp->nfh_fh, nfhp->nfh_fh, nfhp->nfh_len);
1332 if (NFSHASNFSV4(nmp) && len == 2 &&
1333 name[0] == '.' && name[1] == '.') {
1335 NFSCL_REQSTART(nd, NFSPROC_LOOKUPP, dvp);
1337 NFSCL_REQSTART(nd, NFSPROC_LOOKUP, dvp);
1338 (void) nfsm_strtom(nd, name, len);
1340 if (nd->nd_flag & ND_NFSV4) {
1341 NFSGETATTR_ATTRBIT(&attrbits);
1342 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1343 *tl++ = txdr_unsigned(NFSV4OP_GETFH);
1344 *tl = txdr_unsigned(NFSV4OP_GETATTR);
1345 (void) nfsrv_putattrbit(nd, &attrbits);
1347 error = nfscl_request(nd, dvp, p, cred, stuff);
1350 if (nd->nd_repstat) {
1352 * When an NFSv4 Lookupp returns ENOENT, it means that
1353 * the lookup is at the root of an fs, so return this dir.
1355 if (nd->nd_repstat == NFSERR_NOENT && lookupp) {
1357 MALLOC(nfhp, struct nfsfh *, sizeof (struct nfsfh) +
1358 np->n_fhp->nfh_len, M_NFSFH, M_WAITOK);
1359 nfhp->nfh_len = np->n_fhp->nfh_len;
1360 NFSBCOPY(np->n_fhp->nfh_fh, nfhp->nfh_fh, nfhp->nfh_len);
1362 mbuf_freem(nd->nd_mrep);
1365 if (nd->nd_flag & ND_NFSV3)
1366 error = nfscl_postop_attr(nd, dnap, dattrflagp, stuff);
1367 else if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) ==
1369 /* Load the directory attributes. */
1370 error = nfsm_loadattr(nd, dnap);
1376 if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) == ND_NFSV4) {
1377 /* Load the directory attributes. */
1378 error = nfsm_loadattr(nd, dnap);
1382 /* Skip over the Lookup and GetFH operation status values. */
1383 NFSM_DISSECT(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1385 error = nfsm_getfh(nd, nfhpp);
1389 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
1390 if ((nd->nd_flag & ND_NFSV3) && !error)
1391 error = nfscl_postop_attr(nd, dnap, dattrflagp, stuff);
1393 mbuf_freem(nd->nd_mrep);
1394 if (!error && nd->nd_repstat)
1395 error = nd->nd_repstat;
1400 * Do a readlink rpc.
1403 nfsrpc_readlink(vnode_t vp, struct uio *uiop, struct ucred *cred,
1404 NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, void *stuff)
1407 struct nfsrv_descript nfsd, *nd = &nfsd;
1408 struct nfsnode *np = VTONFS(vp);
1409 nfsattrbit_t attrbits;
1410 int error, len, cangetattr = 1;
1413 NFSCL_REQSTART(nd, NFSPROC_READLINK, vp);
1414 if (nd->nd_flag & ND_NFSV4) {
1416 * And do a Getattr op.
1418 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1419 *tl = txdr_unsigned(NFSV4OP_GETATTR);
1420 NFSGETATTR_ATTRBIT(&attrbits);
1421 (void) nfsrv_putattrbit(nd, &attrbits);
1423 error = nfscl_request(nd, vp, p, cred, stuff);
1426 if (nd->nd_flag & ND_NFSV3)
1427 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
1428 if (!nd->nd_repstat && !error) {
1429 NFSM_STRSIZ(len, NFS_MAXPATHLEN);
1431 * This seems weird to me, but must have been added to
1432 * FreeBSD for some reason. The only thing I can think of
1433 * is that there was/is some server that replies with
1434 * more link data than it should?
1436 if (len == NFS_MAXPATHLEN) {
1438 if (np->n_size > 0 && np->n_size < NFS_MAXPATHLEN) {
1444 error = nfsm_mbufuio(nd, uiop, len);
1445 if ((nd->nd_flag & ND_NFSV4) && !error && cangetattr)
1446 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
1448 if (nd->nd_repstat && !error)
1449 error = nd->nd_repstat;
1451 mbuf_freem(nd->nd_mrep);
1459 nfsrpc_read(vnode_t vp, struct uio *uiop, struct ucred *cred,
1460 NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, void *stuff)
1462 int error, expireret = 0, retrycnt;
1463 u_int32_t clidrev = 0;
1464 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
1465 struct nfsnode *np = VTONFS(vp);
1466 struct ucred *newcred;
1467 struct nfsfh *nfhp = NULL;
1468 nfsv4stateid_t stateid;
1471 if (nmp->nm_clp != NULL)
1472 clidrev = nmp->nm_clp->nfsc_clientidrev;
1474 if (NFSHASNFSV4(nmp)) {
1476 newcred = NFSNEWCRED(cred);
1481 if (NFSHASNFSV4(nmp))
1482 (void)nfscl_getstateid(vp, nfhp->nfh_fh, nfhp->nfh_len,
1483 NFSV4OPEN_ACCESSREAD, 0, newcred, p, &stateid,
1485 error = nfsrpc_readrpc(vp, uiop, newcred, &stateid, p, nap,
1487 if (error == NFSERR_OPENMODE) {
1489 nmp->nm_state |= NFSSTA_OPENMODE;
1492 if (error == NFSERR_STALESTATEID)
1493 nfscl_initiate_recovery(nmp->nm_clp);
1495 nfscl_lockderef(lckp);
1496 if (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
1497 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
1498 error == NFSERR_OLDSTATEID || error == NFSERR_BADSESSION) {
1499 (void) nfs_catnap(PZERO, error, "nfs_read");
1500 } else if ((error == NFSERR_EXPIRED ||
1501 error == NFSERR_BADSTATEID) && clidrev != 0) {
1502 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
1505 } while (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
1506 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
1507 error == NFSERR_BADSESSION ||
1508 (error == NFSERR_OLDSTATEID && retrycnt < 20) ||
1509 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
1510 expireret == 0 && clidrev != 0 && retrycnt < 4) ||
1511 (error == NFSERR_OPENMODE && retrycnt < 4));
1512 if (error && retrycnt >= 4)
1514 if (NFSHASNFSV4(nmp))
1515 NFSFREECRED(newcred);
1520 * The actual read RPC.
1523 nfsrpc_readrpc(vnode_t vp, struct uio *uiop, struct ucred *cred,
1524 nfsv4stateid_t *stateidp, NFSPROC_T *p, struct nfsvattr *nap,
1525 int *attrflagp, void *stuff)
1528 int error = 0, len, retlen, tsiz, eof = 0;
1529 struct nfsrv_descript nfsd;
1530 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
1531 struct nfsrv_descript *nd = &nfsd;
1536 tsiz = uio_uio_resid(uiop);
1537 tmp_off = uiop->uio_offset + tsiz;
1539 if (tmp_off > nmp->nm_maxfilesize || tmp_off < uiop->uio_offset) {
1543 rsize = nmp->nm_rsize;
1548 len = (tsiz > rsize) ? rsize : tsiz;
1549 NFSCL_REQSTART(nd, NFSPROC_READ, vp);
1550 if (nd->nd_flag & ND_NFSV4)
1551 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID);
1552 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED * 3);
1553 if (nd->nd_flag & ND_NFSV2) {
1554 *tl++ = txdr_unsigned(uiop->uio_offset);
1555 *tl++ = txdr_unsigned(len);
1558 txdr_hyper(uiop->uio_offset, tl);
1559 *(tl + 2) = txdr_unsigned(len);
1562 * Since I can't do a Getattr for NFSv4 for Write, there
1563 * doesn't seem any point in doing one here, either.
1564 * (See the comment in nfsrpc_writerpc() for more info.)
1566 error = nfscl_request(nd, vp, p, cred, stuff);
1569 if (nd->nd_flag & ND_NFSV3) {
1570 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
1571 } else if (!nd->nd_repstat && (nd->nd_flag & ND_NFSV2)) {
1572 error = nfsm_loadattr(nd, nap);
1576 if (nd->nd_repstat || error) {
1578 error = nd->nd_repstat;
1581 if (nd->nd_flag & ND_NFSV3) {
1582 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1583 eof = fxdr_unsigned(int, *(tl + 1));
1584 } else if (nd->nd_flag & ND_NFSV4) {
1585 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
1586 eof = fxdr_unsigned(int, *tl);
1588 NFSM_STRSIZ(retlen, len);
1589 error = nfsm_mbufuio(nd, uiop, retlen);
1592 mbuf_freem(nd->nd_mrep);
1595 if (!(nd->nd_flag & ND_NFSV2)) {
1596 if (eof || retlen == 0)
1598 } else if (retlen < len)
1603 if (nd->nd_mrep != NULL)
1604 mbuf_freem(nd->nd_mrep);
1609 * nfs write operation
1610 * When called_from_strategy != 0, it should return EIO for an error that
1611 * indicates recovery is in progress, so that the buffer will be left
1612 * dirty and be written back to the server later. If it loops around,
1613 * the recovery thread could get stuck waiting for the buffer and recovery
1614 * will then deadlock.
1617 nfsrpc_write(vnode_t vp, struct uio *uiop, int *iomode, int *must_commit,
1618 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp,
1619 void *stuff, int called_from_strategy)
1621 int error, expireret = 0, retrycnt, nostateid;
1622 u_int32_t clidrev = 0;
1623 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
1624 struct nfsnode *np = VTONFS(vp);
1625 struct ucred *newcred;
1626 struct nfsfh *nfhp = NULL;
1627 nfsv4stateid_t stateid;
1631 if (nmp->nm_clp != NULL)
1632 clidrev = nmp->nm_clp->nfsc_clientidrev;
1634 if (NFSHASNFSV4(nmp)) {
1635 newcred = NFSNEWCRED(cred);
1642 if (NFSHASNFSV4(nmp)) {
1643 (void)nfscl_getstateid(vp, nfhp->nfh_fh, nfhp->nfh_len,
1644 NFSV4OPEN_ACCESSWRITE, 0, newcred, p, &stateid,
1646 if (stateid.other[0] == 0 && stateid.other[1] == 0 &&
1647 stateid.other[2] == 0) {
1649 NFSCL_DEBUG(1, "stateid0 in write\n");
1654 * If there is no stateid for NFSv4, it means this is an
1655 * extraneous write after close. Basically a poorly
1656 * implemented buffer cache. Just don't do the write.
1661 error = nfsrpc_writerpc(vp, uiop, iomode, must_commit,
1662 newcred, &stateid, p, nap, attrflagp, stuff);
1663 if (error == NFSERR_STALESTATEID)
1664 nfscl_initiate_recovery(nmp->nm_clp);
1666 nfscl_lockderef(lckp);
1667 if (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
1668 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
1669 error == NFSERR_OLDSTATEID || error == NFSERR_BADSESSION) {
1670 (void) nfs_catnap(PZERO, error, "nfs_write");
1671 } else if ((error == NFSERR_EXPIRED ||
1672 error == NFSERR_BADSTATEID) && clidrev != 0) {
1673 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
1676 } while (error == NFSERR_GRACE || error == NFSERR_DELAY ||
1677 ((error == NFSERR_STALESTATEID || error == NFSERR_BADSESSION ||
1678 error == NFSERR_STALEDONTRECOVER) && called_from_strategy == 0) ||
1679 (error == NFSERR_OLDSTATEID && retrycnt < 20) ||
1680 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
1681 expireret == 0 && clidrev != 0 && retrycnt < 4));
1682 if (error != 0 && (retrycnt >= 4 ||
1683 ((error == NFSERR_STALESTATEID || error == NFSERR_BADSESSION ||
1684 error == NFSERR_STALEDONTRECOVER) && called_from_strategy != 0)))
1686 if (NFSHASNFSV4(nmp))
1687 NFSFREECRED(newcred);
1692 * The actual write RPC.
1695 nfsrpc_writerpc(vnode_t vp, struct uio *uiop, int *iomode,
1696 int *must_commit, struct ucred *cred, nfsv4stateid_t *stateidp,
1697 NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, void *stuff)
1700 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
1701 struct nfsnode *np = VTONFS(vp);
1702 int error = 0, len, tsiz, rlen, commit, committed = NFSWRITE_FILESYNC;
1703 int wccflag = 0, wsize;
1705 struct nfsrv_descript nfsd;
1706 struct nfsrv_descript *nd = &nfsd;
1707 nfsattrbit_t attrbits;
1710 KASSERT(uiop->uio_iovcnt == 1, ("nfs: writerpc iovcnt > 1"));
1712 tsiz = uio_uio_resid(uiop);
1713 tmp_off = uiop->uio_offset + tsiz;
1715 if (tmp_off > nmp->nm_maxfilesize || tmp_off < uiop->uio_offset) {
1719 wsize = nmp->nm_wsize;
1721 nd->nd_mrep = NULL; /* NFSv2 sometimes does a write with */
1722 nd->nd_repstat = 0; /* uio_resid == 0, so the while is not done */
1725 len = (tsiz > wsize) ? wsize : tsiz;
1726 NFSCL_REQSTART(nd, NFSPROC_WRITE, vp);
1727 if (nd->nd_flag & ND_NFSV4) {
1728 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID);
1729 NFSM_BUILD(tl, u_int32_t *, NFSX_HYPER+2*NFSX_UNSIGNED);
1730 txdr_hyper(uiop->uio_offset, tl);
1732 *tl++ = txdr_unsigned(*iomode);
1733 *tl = txdr_unsigned(len);
1734 } else if (nd->nd_flag & ND_NFSV3) {
1735 NFSM_BUILD(tl, u_int32_t *, NFSX_HYPER+3*NFSX_UNSIGNED);
1736 txdr_hyper(uiop->uio_offset, tl);
1738 *tl++ = txdr_unsigned(len);
1739 *tl++ = txdr_unsigned(*iomode);
1740 *tl = txdr_unsigned(len);
1744 NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1746 * Not sure why someone changed this, since the
1747 * RFC clearly states that "beginoffset" and
1748 * "totalcount" are ignored, but it wouldn't
1749 * surprise me if there's a busted server out there.
1751 /* Set both "begin" and "current" to non-garbage. */
1752 x = txdr_unsigned((u_int32_t)uiop->uio_offset);
1753 *tl++ = x; /* "begin offset" */
1754 *tl++ = x; /* "current offset" */
1755 x = txdr_unsigned(len);
1756 *tl++ = x; /* total to this offset */
1757 *tl = x; /* size of this write */
1760 nfsm_uiombuf(nd, uiop, len);
1762 * Although it is tempting to do a normal Getattr Op in the
1763 * NFSv4 compound, the result can be a nearly hung client
1764 * system if the Getattr asks for Owner and/or OwnerGroup.
1765 * It occurs when the client can't map either the Owner or
1766 * Owner_group name in the Getattr reply to a uid/gid. When
1767 * there is a cache miss, the kernel does an upcall to the
1768 * nfsuserd. Then, it can try and read the local /etc/passwd
1769 * or /etc/group file. It can then block in getnewbuf(),
1770 * waiting for dirty writes to be pushed to the NFS server.
1771 * The only reason this doesn't result in a complete
1772 * deadlock, is that the upcall times out and allows
1773 * the write to complete. However, progress is so slow
1774 * that it might just as well be deadlocked.
1775 * As such, we get the rest of the attributes, but not
1776 * Owner or Owner_group.
1777 * nb: nfscl_loadattrcache() needs to be told that these
1778 * partial attributes from a write rpc are being
1779 * passed in, via a argument flag.
1781 if (nd->nd_flag & ND_NFSV4) {
1782 NFSWRITEGETATTR_ATTRBIT(&attrbits);
1783 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1784 *tl = txdr_unsigned(NFSV4OP_GETATTR);
1785 (void) nfsrv_putattrbit(nd, &attrbits);
1787 error = nfscl_request(nd, vp, p, cred, stuff);
1790 if (nd->nd_repstat) {
1792 * In case the rpc gets retried, roll
1793 * the uio fileds changed by nfsm_uiombuf()
1796 uiop->uio_offset -= len;
1797 uio_uio_resid_add(uiop, len);
1798 uio_iov_base_add(uiop, -len);
1799 uio_iov_len_add(uiop, len);
1801 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) {
1802 error = nfscl_wcc_data(nd, vp, nap, attrflagp,
1807 if (!nd->nd_repstat) {
1808 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) {
1809 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED
1811 rlen = fxdr_unsigned(int, *tl++);
1815 } else if (rlen < len) {
1816 backup = len - rlen;
1817 uio_iov_base_add(uiop, -(backup));
1818 uio_iov_len_add(uiop, backup);
1819 uiop->uio_offset -= backup;
1820 uio_uio_resid_add(uiop, backup);
1823 commit = fxdr_unsigned(int, *tl++);
1826 * Return the lowest commitment level
1827 * obtained by any of the RPCs.
1829 if (committed == NFSWRITE_FILESYNC)
1831 else if (committed == NFSWRITE_DATASYNC &&
1832 commit == NFSWRITE_UNSTABLE)
1835 if (!NFSHASWRITEVERF(nmp)) {
1836 NFSBCOPY((caddr_t)tl,
1837 (caddr_t)&nmp->nm_verf[0],
1839 NFSSETWRITEVERF(nmp);
1840 } else if (NFSBCMP(tl, nmp->nm_verf,
1843 NFSBCOPY(tl, nmp->nm_verf, NFSX_VERF);
1847 if (nd->nd_flag & ND_NFSV4)
1848 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1849 if (nd->nd_flag & (ND_NFSV2 | ND_NFSV4)) {
1850 error = nfsm_loadattr(nd, nap);
1852 *attrflagp = NFS_LATTR_NOSHRINK;
1855 error = nd->nd_repstat;
1859 NFSWRITERPC_SETTIME(wccflag, np, nap, (nd->nd_flag & ND_NFSV4));
1860 mbuf_freem(nd->nd_mrep);
1865 if (nd->nd_mrep != NULL)
1866 mbuf_freem(nd->nd_mrep);
1867 *iomode = committed;
1868 if (nd->nd_repstat && !error)
1869 error = nd->nd_repstat;
1875 * For NFS v2 this is a kludge. Use a create rpc but with the IFMT bits of the
1876 * mode set to specify the file type and the size field for rdev.
1879 nfsrpc_mknod(vnode_t dvp, char *name, int namelen, struct vattr *vap,
1880 u_int32_t rdev, enum vtype vtyp, struct ucred *cred, NFSPROC_T *p,
1881 struct nfsvattr *dnap, struct nfsvattr *nnap, struct nfsfh **nfhpp,
1882 int *attrflagp, int *dattrflagp, void *dstuff)
1886 struct nfsrv_descript nfsd, *nd = &nfsd;
1887 nfsattrbit_t attrbits;
1892 if (namelen > NFS_MAXNAMLEN)
1893 return (ENAMETOOLONG);
1894 NFSCL_REQSTART(nd, NFSPROC_MKNOD, dvp);
1895 if (nd->nd_flag & ND_NFSV4) {
1896 if (vtyp == VBLK || vtyp == VCHR) {
1897 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
1898 *tl++ = vtonfsv34_type(vtyp);
1899 *tl++ = txdr_unsigned(NFSMAJOR(rdev));
1900 *tl = txdr_unsigned(NFSMINOR(rdev));
1902 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1903 *tl = vtonfsv34_type(vtyp);
1906 (void) nfsm_strtom(nd, name, namelen);
1907 if (nd->nd_flag & ND_NFSV3) {
1908 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1909 *tl = vtonfsv34_type(vtyp);
1911 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4))
1912 nfscl_fillsattr(nd, vap, dvp, 0, 0);
1913 if ((nd->nd_flag & ND_NFSV3) &&
1914 (vtyp == VCHR || vtyp == VBLK)) {
1915 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1916 *tl++ = txdr_unsigned(NFSMAJOR(rdev));
1917 *tl = txdr_unsigned(NFSMINOR(rdev));
1919 if (nd->nd_flag & ND_NFSV4) {
1920 NFSGETATTR_ATTRBIT(&attrbits);
1921 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1922 *tl++ = txdr_unsigned(NFSV4OP_GETFH);
1923 *tl = txdr_unsigned(NFSV4OP_GETATTR);
1924 (void) nfsrv_putattrbit(nd, &attrbits);
1926 if (nd->nd_flag & ND_NFSV2)
1927 nfscl_fillsattr(nd, vap, dvp, NFSSATTR_SIZERDEV, rdev);
1928 error = nfscl_request(nd, dvp, p, cred, dstuff);
1931 if (nd->nd_flag & ND_NFSV4)
1932 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
1933 if (!nd->nd_repstat) {
1934 if (nd->nd_flag & ND_NFSV4) {
1935 NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
1936 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
1940 error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp);
1944 if (nd->nd_flag & ND_NFSV3)
1945 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
1946 if (!error && nd->nd_repstat)
1947 error = nd->nd_repstat;
1949 mbuf_freem(nd->nd_mrep);
1954 * nfs file create call
1955 * Mostly just call the approriate routine. (I separated out v4, so that
1956 * error recovery wouldn't be as difficult.)
1959 nfsrpc_create(vnode_t dvp, char *name, int namelen, struct vattr *vap,
1960 nfsquad_t cverf, int fmode, struct ucred *cred, NFSPROC_T *p,
1961 struct nfsvattr *dnap, struct nfsvattr *nnap, struct nfsfh **nfhpp,
1962 int *attrflagp, int *dattrflagp, void *dstuff)
1964 int error = 0, newone, expireret = 0, retrycnt, unlocked;
1965 struct nfsclowner *owp;
1966 struct nfscldeleg *dp;
1967 struct nfsmount *nmp = VFSTONFS(vnode_mount(dvp));
1970 if (NFSHASNFSV4(nmp)) {
1974 error = nfscl_open(dvp, NULL, 0, (NFSV4OPEN_ACCESSWRITE |
1975 NFSV4OPEN_ACCESSREAD), 0, cred, p, &owp, NULL, &newone,
1979 if (nmp->nm_clp != NULL)
1980 clidrev = nmp->nm_clp->nfsc_clientidrev;
1983 if (!NFSHASPNFS(nmp) || nfscl_enablecallb == 0 ||
1984 nfs_numnfscbd == 0 || retrycnt > 0)
1985 error = nfsrpc_createv4(dvp, name, namelen, vap, cverf,
1986 fmode, owp, &dp, cred, p, dnap, nnap, nfhpp,
1987 attrflagp, dattrflagp, dstuff, &unlocked);
1989 error = nfsrpc_getcreatelayout(dvp, name, namelen, vap,
1990 cverf, fmode, owp, &dp, cred, p, dnap, nnap, nfhpp,
1991 attrflagp, dattrflagp, dstuff, &unlocked);
1993 * There is no need to invalidate cached attributes here,
1994 * since new post-delegation issue attributes are always
1995 * returned by nfsrpc_createv4() and these will update the
1999 (void) nfscl_deleg(nmp->nm_mountp, owp->nfsow_clp,
2000 (*nfhpp)->nfh_fh, (*nfhpp)->nfh_len, cred, p, &dp);
2001 nfscl_ownerrelease(nmp, owp, error, newone, unlocked);
2002 if (error == NFSERR_GRACE || error == NFSERR_STALECLIENTID ||
2003 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
2004 error == NFSERR_BADSESSION) {
2005 (void) nfs_catnap(PZERO, error, "nfs_open");
2006 } else if ((error == NFSERR_EXPIRED ||
2007 error == NFSERR_BADSTATEID) && clidrev != 0) {
2008 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
2011 } while (error == NFSERR_GRACE || error == NFSERR_STALECLIENTID ||
2012 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
2013 error == NFSERR_BADSESSION ||
2014 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
2015 expireret == 0 && clidrev != 0 && retrycnt < 4));
2016 if (error && retrycnt >= 4)
2019 error = nfsrpc_createv23(dvp, name, namelen, vap, cverf,
2020 fmode, cred, p, dnap, nnap, nfhpp, attrflagp, dattrflagp,
2027 * The create rpc for v2 and 3.
2030 nfsrpc_createv23(vnode_t dvp, char *name, int namelen, struct vattr *vap,
2031 nfsquad_t cverf, int fmode, struct ucred *cred, NFSPROC_T *p,
2032 struct nfsvattr *dnap, struct nfsvattr *nnap, struct nfsfh **nfhpp,
2033 int *attrflagp, int *dattrflagp, void *dstuff)
2037 struct nfsrv_descript nfsd, *nd = &nfsd;
2042 if (namelen > NFS_MAXNAMLEN)
2043 return (ENAMETOOLONG);
2044 NFSCL_REQSTART(nd, NFSPROC_CREATE, dvp);
2045 (void) nfsm_strtom(nd, name, namelen);
2046 if (nd->nd_flag & ND_NFSV3) {
2047 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2048 if (fmode & O_EXCL) {
2049 *tl = txdr_unsigned(NFSCREATE_EXCLUSIVE);
2050 NFSM_BUILD(tl, u_int32_t *, NFSX_VERF);
2051 *tl++ = cverf.lval[0];
2052 *tl = cverf.lval[1];
2054 *tl = txdr_unsigned(NFSCREATE_UNCHECKED);
2055 nfscl_fillsattr(nd, vap, dvp, 0, 0);
2058 nfscl_fillsattr(nd, vap, dvp, NFSSATTR_SIZE0, 0);
2060 error = nfscl_request(nd, dvp, p, cred, dstuff);
2063 if (nd->nd_repstat == 0) {
2064 error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp);
2068 if (nd->nd_flag & ND_NFSV3)
2069 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
2070 if (nd->nd_repstat != 0 && error == 0)
2071 error = nd->nd_repstat;
2073 mbuf_freem(nd->nd_mrep);
2078 nfsrpc_createv4(vnode_t dvp, char *name, int namelen, struct vattr *vap,
2079 nfsquad_t cverf, int fmode, struct nfsclowner *owp, struct nfscldeleg **dpp,
2080 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap,
2081 struct nfsvattr *nnap, struct nfsfh **nfhpp, int *attrflagp,
2082 int *dattrflagp, void *dstuff, int *unlockedp)
2085 int error = 0, deleg, newone, ret, acesize, limitby;
2086 struct nfsrv_descript nfsd, *nd = &nfsd;
2087 struct nfsclopen *op;
2088 struct nfscldeleg *dp = NULL;
2091 nfsattrbit_t attrbits;
2092 nfsv4stateid_t stateid;
2094 struct nfsmount *nmp;
2095 struct nfsclsession *tsep;
2097 nmp = VFSTONFS(dvp->v_mount);
2104 if (namelen > NFS_MAXNAMLEN)
2105 return (ENAMETOOLONG);
2106 NFSCL_REQSTART(nd, NFSPROC_CREATE, dvp);
2108 * For V4, this is actually an Open op.
2110 NFSM_BUILD(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
2111 *tl++ = txdr_unsigned(owp->nfsow_seqid);
2112 *tl++ = txdr_unsigned(NFSV4OPEN_ACCESSWRITE |
2113 NFSV4OPEN_ACCESSREAD);
2114 *tl++ = txdr_unsigned(NFSV4OPEN_DENYNONE);
2115 tsep = nfsmnt_mdssession(nmp);
2116 *tl++ = tsep->nfsess_clientid.lval[0];
2117 *tl = tsep->nfsess_clientid.lval[1];
2118 (void) nfsm_strtom(nd, owp->nfsow_owner, NFSV4CL_LOCKNAMELEN);
2119 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2120 *tl++ = txdr_unsigned(NFSV4OPEN_CREATE);
2121 if (fmode & O_EXCL) {
2122 if (NFSHASNFSV4N(nmp)) {
2123 if (NFSHASSESSPERSIST(nmp)) {
2124 /* Use GUARDED for persistent sessions. */
2125 *tl = txdr_unsigned(NFSCREATE_GUARDED);
2126 nfscl_fillsattr(nd, vap, dvp, 0, 0);
2128 /* Otherwise, use EXCLUSIVE4_1. */
2129 *tl = txdr_unsigned(NFSCREATE_EXCLUSIVE41);
2130 NFSM_BUILD(tl, u_int32_t *, NFSX_VERF);
2131 *tl++ = cverf.lval[0];
2132 *tl = cverf.lval[1];
2133 nfscl_fillsattr(nd, vap, dvp, 0, 0);
2137 *tl = txdr_unsigned(NFSCREATE_EXCLUSIVE);
2138 NFSM_BUILD(tl, u_int32_t *, NFSX_VERF);
2139 *tl++ = cverf.lval[0];
2140 *tl = cverf.lval[1];
2143 *tl = txdr_unsigned(NFSCREATE_UNCHECKED);
2144 nfscl_fillsattr(nd, vap, dvp, 0, 0);
2146 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2147 *tl = txdr_unsigned(NFSV4OPEN_CLAIMNULL);
2148 (void) nfsm_strtom(nd, name, namelen);
2149 /* Get the new file's handle and attributes. */
2150 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2151 *tl++ = txdr_unsigned(NFSV4OP_GETFH);
2152 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2153 NFSGETATTR_ATTRBIT(&attrbits);
2154 (void) nfsrv_putattrbit(nd, &attrbits);
2155 /* Get the directory's post-op attributes. */
2156 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2157 *tl = txdr_unsigned(NFSV4OP_PUTFH);
2158 (void) nfsm_fhtom(nd, np->n_fhp->nfh_fh, np->n_fhp->nfh_len, 0);
2159 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2160 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2161 (void) nfsrv_putattrbit(nd, &attrbits);
2162 error = nfscl_request(nd, dvp, p, cred, dstuff);
2165 NFSCL_INCRSEQID(owp->nfsow_seqid, nd);
2166 if (nd->nd_repstat == 0) {
2167 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
2169 stateid.seqid = *tl++;
2170 stateid.other[0] = *tl++;
2171 stateid.other[1] = *tl++;
2172 stateid.other[2] = *tl;
2173 rflags = fxdr_unsigned(u_int32_t, *(tl + 6));
2174 (void) nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
2175 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2176 deleg = fxdr_unsigned(int, *tl);
2177 if (deleg == NFSV4OPEN_DELEGATEREAD ||
2178 deleg == NFSV4OPEN_DELEGATEWRITE) {
2179 if (!(owp->nfsow_clp->nfsc_flags &
2180 NFSCLFLAGS_FIRSTDELEG))
2181 owp->nfsow_clp->nfsc_flags |=
2182 (NFSCLFLAGS_FIRSTDELEG | NFSCLFLAGS_GOTDELEG);
2183 MALLOC(dp, struct nfscldeleg *,
2184 sizeof (struct nfscldeleg) + NFSX_V4FHMAX,
2185 M_NFSCLDELEG, M_WAITOK);
2186 LIST_INIT(&dp->nfsdl_owner);
2187 LIST_INIT(&dp->nfsdl_lock);
2188 dp->nfsdl_clp = owp->nfsow_clp;
2189 newnfs_copyincred(cred, &dp->nfsdl_cred);
2190 nfscl_lockinit(&dp->nfsdl_rwlock);
2191 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
2193 dp->nfsdl_stateid.seqid = *tl++;
2194 dp->nfsdl_stateid.other[0] = *tl++;
2195 dp->nfsdl_stateid.other[1] = *tl++;
2196 dp->nfsdl_stateid.other[2] = *tl++;
2197 ret = fxdr_unsigned(int, *tl);
2198 if (deleg == NFSV4OPEN_DELEGATEWRITE) {
2199 dp->nfsdl_flags = NFSCLDL_WRITE;
2201 * Indicates how much the file can grow.
2203 NFSM_DISSECT(tl, u_int32_t *,
2205 limitby = fxdr_unsigned(int, *tl++);
2207 case NFSV4OPEN_LIMITSIZE:
2208 dp->nfsdl_sizelimit = fxdr_hyper(tl);
2210 case NFSV4OPEN_LIMITBLOCKS:
2211 dp->nfsdl_sizelimit =
2212 fxdr_unsigned(u_int64_t, *tl++);
2213 dp->nfsdl_sizelimit *=
2214 fxdr_unsigned(u_int64_t, *tl);
2217 error = NFSERR_BADXDR;
2221 dp->nfsdl_flags = NFSCLDL_READ;
2224 dp->nfsdl_flags |= NFSCLDL_RECALL;
2225 error = nfsrv_dissectace(nd, &dp->nfsdl_ace, &ret,
2229 } else if (deleg != NFSV4OPEN_DELEGATENONE) {
2230 error = NFSERR_BADXDR;
2233 error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp);
2236 /* Get rid of the PutFH and Getattr status values. */
2237 NFSM_DISSECT(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
2238 /* Load the directory attributes. */
2239 error = nfsm_loadattr(nd, dnap);
2243 if (dp != NULL && *attrflagp) {
2244 dp->nfsdl_change = nnap->na_filerev;
2245 dp->nfsdl_modtime = nnap->na_mtime;
2246 dp->nfsdl_flags |= NFSCLDL_MODTIMESET;
2249 * We can now complete the Open state.
2253 dp->nfsdl_fhlen = nfhp->nfh_len;
2254 NFSBCOPY(nfhp->nfh_fh, dp->nfsdl_fh, nfhp->nfh_len);
2257 * Get an Open structure that will be
2258 * attached to the OpenOwner, acquired already.
2260 error = nfscl_open(dvp, nfhp->nfh_fh, nfhp->nfh_len,
2261 (NFSV4OPEN_ACCESSWRITE | NFSV4OPEN_ACCESSREAD), 0,
2262 cred, p, NULL, &op, &newone, NULL, 0);
2265 op->nfso_stateid = stateid;
2266 newnfs_copyincred(cred, &op->nfso_cred);
2267 if ((rflags & NFSV4OPEN_RESULTCONFIRM)) {
2269 ret = nfsrpc_openconfirm(dvp, nfhp->nfh_fh,
2270 nfhp->nfh_len, op, cred, p);
2271 if (ret == NFSERR_DELAY)
2272 (void) nfs_catnap(PZERO, ret, "nfs_create");
2273 } while (ret == NFSERR_DELAY);
2278 * If the server is handing out delegations, but we didn't
2279 * get one because an OpenConfirm was required, try the
2280 * Open again, to get a delegation. This is a harmless no-op,
2281 * from a server's point of view.
2283 if ((rflags & NFSV4OPEN_RESULTCONFIRM) &&
2284 (owp->nfsow_clp->nfsc_flags & NFSCLFLAGS_GOTDELEG) &&
2285 !error && dp == NULL) {
2287 ret = nfsrpc_openrpc(VFSTONFS(vnode_mount(dvp)), dvp,
2288 np->n_fhp->nfh_fh, np->n_fhp->nfh_len,
2289 nfhp->nfh_fh, nfhp->nfh_len,
2290 (NFSV4OPEN_ACCESSWRITE | NFSV4OPEN_ACCESSREAD), op,
2291 name, namelen, &dp, 0, 0x0, cred, p, 0, 1);
2292 if (ret == NFSERR_DELAY)
2293 (void) nfs_catnap(PZERO, ret, "nfs_crt2");
2294 } while (ret == NFSERR_DELAY);
2297 FREE((caddr_t)dp, M_NFSCLDELEG);
2300 if (ret == NFSERR_STALECLIENTID ||
2301 ret == NFSERR_STALEDONTRECOVER ||
2302 ret == NFSERR_BADSESSION)
2306 nfscl_openrelease(nmp, op, error, newone);
2309 if (nd->nd_repstat != 0 && error == 0)
2310 error = nd->nd_repstat;
2311 if (error == NFSERR_STALECLIENTID)
2312 nfscl_initiate_recovery(owp->nfsow_clp);
2316 else if (dp != NULL)
2317 FREE((caddr_t)dp, M_NFSCLDELEG);
2318 mbuf_freem(nd->nd_mrep);
2326 nfsrpc_remove(vnode_t dvp, char *name, int namelen, vnode_t vp,
2327 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap, int *dattrflagp,
2331 struct nfsrv_descript nfsd, *nd = &nfsd;
2333 struct nfsmount *nmp;
2334 nfsv4stateid_t dstateid;
2335 int error, ret = 0, i;
2338 if (namelen > NFS_MAXNAMLEN)
2339 return (ENAMETOOLONG);
2340 nmp = VFSTONFS(vnode_mount(dvp));
2342 if (NFSHASNFSV4(nmp) && ret == 0) {
2343 ret = nfscl_removedeleg(vp, p, &dstateid);
2345 NFSCL_REQSTART(nd, NFSPROC_RETDELEGREMOVE, vp);
2346 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID +
2348 if (NFSHASNFSV4N(nmp))
2351 *tl++ = dstateid.seqid;
2352 *tl++ = dstateid.other[0];
2353 *tl++ = dstateid.other[1];
2354 *tl++ = dstateid.other[2];
2355 *tl = txdr_unsigned(NFSV4OP_PUTFH);
2357 (void) nfsm_fhtom(nd, np->n_fhp->nfh_fh,
2358 np->n_fhp->nfh_len, 0);
2359 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2360 *tl = txdr_unsigned(NFSV4OP_REMOVE);
2366 NFSCL_REQSTART(nd, NFSPROC_REMOVE, dvp);
2367 (void) nfsm_strtom(nd, name, namelen);
2368 error = nfscl_request(nd, dvp, p, cred, dstuff);
2371 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) {
2372 /* For NFSv4, parse out any Delereturn replies. */
2373 if (ret > 0 && nd->nd_repstat != 0 &&
2374 (nd->nd_flag & ND_NOMOREDATA)) {
2376 * If the Delegreturn failed, try again without
2377 * it. The server will Recall, as required.
2379 mbuf_freem(nd->nd_mrep);
2382 for (i = 0; i < (ret * 2); i++) {
2383 if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) ==
2385 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2387 nd->nd_flag |= ND_NOMOREDATA;
2390 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
2392 if (nd->nd_repstat && !error)
2393 error = nd->nd_repstat;
2395 mbuf_freem(nd->nd_mrep);
2400 * Do an nfs rename rpc.
2403 nfsrpc_rename(vnode_t fdvp, vnode_t fvp, char *fnameptr, int fnamelen,
2404 vnode_t tdvp, vnode_t tvp, char *tnameptr, int tnamelen, struct ucred *cred,
2405 NFSPROC_T *p, struct nfsvattr *fnap, struct nfsvattr *tnap,
2406 int *fattrflagp, int *tattrflagp, void *fstuff, void *tstuff)
2409 struct nfsrv_descript nfsd, *nd = &nfsd;
2410 struct nfsmount *nmp;
2412 nfsattrbit_t attrbits;
2413 nfsv4stateid_t fdstateid, tdstateid;
2414 int error = 0, ret = 0, gottd = 0, gotfd = 0, i;
2418 nmp = VFSTONFS(vnode_mount(fdvp));
2419 if (fnamelen > NFS_MAXNAMLEN || tnamelen > NFS_MAXNAMLEN)
2420 return (ENAMETOOLONG);
2422 if (NFSHASNFSV4(nmp) && ret == 0) {
2423 ret = nfscl_renamedeleg(fvp, &fdstateid, &gotfd, tvp,
2424 &tdstateid, &gottd, p);
2425 if (gotfd && gottd) {
2426 NFSCL_REQSTART(nd, NFSPROC_RETDELEGRENAME2, fvp);
2428 NFSCL_REQSTART(nd, NFSPROC_RETDELEGRENAME1, fvp);
2430 NFSCL_REQSTART(nd, NFSPROC_RETDELEGRENAME1, tvp);
2433 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID);
2434 if (NFSHASNFSV4N(nmp))
2437 *tl++ = fdstateid.seqid;
2438 *tl++ = fdstateid.other[0];
2439 *tl++ = fdstateid.other[1];
2440 *tl = fdstateid.other[2];
2442 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2443 *tl = txdr_unsigned(NFSV4OP_PUTFH);
2445 (void) nfsm_fhtom(nd, np->n_fhp->nfh_fh,
2446 np->n_fhp->nfh_len, 0);
2447 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2448 *tl = txdr_unsigned(NFSV4OP_DELEGRETURN);
2452 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID);
2453 if (NFSHASNFSV4N(nmp))
2456 *tl++ = tdstateid.seqid;
2457 *tl++ = tdstateid.other[0];
2458 *tl++ = tdstateid.other[1];
2459 *tl = tdstateid.other[2];
2462 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2463 *tl = txdr_unsigned(NFSV4OP_PUTFH);
2465 (void) nfsm_fhtom(nd, np->n_fhp->nfh_fh,
2466 np->n_fhp->nfh_len, 0);
2467 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2468 *tl = txdr_unsigned(NFSV4OP_SAVEFH);
2474 NFSCL_REQSTART(nd, NFSPROC_RENAME, fdvp);
2475 if (nd->nd_flag & ND_NFSV4) {
2476 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2477 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2478 NFSWCCATTR_ATTRBIT(&attrbits);
2479 (void) nfsrv_putattrbit(nd, &attrbits);
2480 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2481 *tl = txdr_unsigned(NFSV4OP_PUTFH);
2482 (void) nfsm_fhtom(nd, VTONFS(tdvp)->n_fhp->nfh_fh,
2483 VTONFS(tdvp)->n_fhp->nfh_len, 0);
2484 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2485 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2486 (void) nfsrv_putattrbit(nd, &attrbits);
2487 nd->nd_flag |= ND_V4WCCATTR;
2488 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2489 *tl = txdr_unsigned(NFSV4OP_RENAME);
2491 (void) nfsm_strtom(nd, fnameptr, fnamelen);
2492 if (!(nd->nd_flag & ND_NFSV4))
2493 (void) nfsm_fhtom(nd, VTONFS(tdvp)->n_fhp->nfh_fh,
2494 VTONFS(tdvp)->n_fhp->nfh_len, 0);
2495 (void) nfsm_strtom(nd, tnameptr, tnamelen);
2496 error = nfscl_request(nd, fdvp, p, cred, fstuff);
2499 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) {
2500 /* For NFSv4, parse out any Delereturn replies. */
2501 if (ret > 0 && nd->nd_repstat != 0 &&
2502 (nd->nd_flag & ND_NOMOREDATA)) {
2504 * If the Delegreturn failed, try again without
2505 * it. The server will Recall, as required.
2507 mbuf_freem(nd->nd_mrep);
2510 for (i = 0; i < (ret * 2); i++) {
2511 if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) ==
2513 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2515 if (i == 0 && ret > 1) {
2517 * If the Delegreturn failed, try again
2518 * without it. The server will Recall, as
2520 * If ret > 1, the first iteration of this
2521 * loop is the second DelegReturn result.
2523 mbuf_freem(nd->nd_mrep);
2526 nd->nd_flag |= ND_NOMOREDATA;
2531 /* Now, the first wcc attribute reply. */
2532 if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) == ND_NFSV4) {
2533 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2535 nd->nd_flag |= ND_NOMOREDATA;
2537 error = nfscl_wcc_data(nd, fdvp, fnap, fattrflagp, NULL,
2539 /* and the second wcc attribute reply. */
2540 if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) == ND_NFSV4 &&
2542 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2544 nd->nd_flag |= ND_NOMOREDATA;
2547 error = nfscl_wcc_data(nd, tdvp, tnap, tattrflagp,
2550 if (nd->nd_repstat && !error)
2551 error = nd->nd_repstat;
2553 mbuf_freem(nd->nd_mrep);
2558 * nfs hard link create rpc
2561 nfsrpc_link(vnode_t dvp, vnode_t vp, char *name, int namelen,
2562 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap,
2563 struct nfsvattr *nap, int *attrflagp, int *dattrflagp, void *dstuff)
2566 struct nfsrv_descript nfsd, *nd = &nfsd;
2567 nfsattrbit_t attrbits;
2572 if (namelen > NFS_MAXNAMLEN)
2573 return (ENAMETOOLONG);
2574 NFSCL_REQSTART(nd, NFSPROC_LINK, vp);
2575 if (nd->nd_flag & ND_NFSV4) {
2576 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2577 *tl = txdr_unsigned(NFSV4OP_PUTFH);
2579 (void) nfsm_fhtom(nd, VTONFS(dvp)->n_fhp->nfh_fh,
2580 VTONFS(dvp)->n_fhp->nfh_len, 0);
2581 if (nd->nd_flag & ND_NFSV4) {
2582 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2583 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2584 NFSWCCATTR_ATTRBIT(&attrbits);
2585 (void) nfsrv_putattrbit(nd, &attrbits);
2586 nd->nd_flag |= ND_V4WCCATTR;
2587 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2588 *tl = txdr_unsigned(NFSV4OP_LINK);
2590 (void) nfsm_strtom(nd, name, namelen);
2591 error = nfscl_request(nd, vp, p, cred, dstuff);
2594 if (nd->nd_flag & ND_NFSV3) {
2595 error = nfscl_postop_attr(nd, nap, attrflagp, dstuff);
2597 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp,
2599 } else if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) == ND_NFSV4) {
2601 * First, parse out the PutFH and Getattr result.
2603 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2605 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2607 nd->nd_flag |= ND_NOMOREDATA;
2609 * Get the pre-op attributes.
2611 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
2613 if (nd->nd_repstat && !error)
2614 error = nd->nd_repstat;
2616 mbuf_freem(nd->nd_mrep);
2621 * nfs symbolic link create rpc
2624 nfsrpc_symlink(vnode_t dvp, char *name, int namelen, char *target,
2625 struct vattr *vap, struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap,
2626 struct nfsvattr *nnap, struct nfsfh **nfhpp, int *attrflagp,
2627 int *dattrflagp, void *dstuff)
2630 struct nfsrv_descript nfsd, *nd = &nfsd;
2631 struct nfsmount *nmp;
2632 int slen, error = 0;
2637 nmp = VFSTONFS(vnode_mount(dvp));
2638 slen = strlen(target);
2639 if (slen > NFS_MAXPATHLEN || namelen > NFS_MAXNAMLEN)
2640 return (ENAMETOOLONG);
2641 NFSCL_REQSTART(nd, NFSPROC_SYMLINK, dvp);
2642 if (nd->nd_flag & ND_NFSV4) {
2643 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2644 *tl = txdr_unsigned(NFLNK);
2645 (void) nfsm_strtom(nd, target, slen);
2647 (void) nfsm_strtom(nd, name, namelen);
2648 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4))
2649 nfscl_fillsattr(nd, vap, dvp, 0, 0);
2650 if (!(nd->nd_flag & ND_NFSV4))
2651 (void) nfsm_strtom(nd, target, slen);
2652 if (nd->nd_flag & ND_NFSV2)
2653 nfscl_fillsattr(nd, vap, dvp, NFSSATTR_SIZENEG1, 0);
2654 error = nfscl_request(nd, dvp, p, cred, dstuff);
2657 if (nd->nd_flag & ND_NFSV4)
2658 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
2659 if ((nd->nd_flag & ND_NFSV3) && !error) {
2660 if (!nd->nd_repstat)
2661 error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp);
2663 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp,
2666 if (nd->nd_repstat && !error)
2667 error = nd->nd_repstat;
2668 mbuf_freem(nd->nd_mrep);
2670 * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry.
2671 * Only do this if vfs.nfs.ignore_eexist is set.
2672 * Never do this for NFSv4.1 or later minor versions, since sessions
2673 * should guarantee "exactly once" RPC semantics.
2675 if (error == EEXIST && nfsignore_eexist != 0 && (!NFSHASNFSV4(nmp) ||
2676 nmp->nm_minorvers == 0))
2685 nfsrpc_mkdir(vnode_t dvp, char *name, int namelen, struct vattr *vap,
2686 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap,
2687 struct nfsvattr *nnap, struct nfsfh **nfhpp, int *attrflagp,
2688 int *dattrflagp, void *dstuff)
2691 struct nfsrv_descript nfsd, *nd = &nfsd;
2692 nfsattrbit_t attrbits;
2695 struct nfsmount *nmp;
2700 nmp = VFSTONFS(vnode_mount(dvp));
2701 fhp = VTONFS(dvp)->n_fhp;
2702 if (namelen > NFS_MAXNAMLEN)
2703 return (ENAMETOOLONG);
2704 NFSCL_REQSTART(nd, NFSPROC_MKDIR, dvp);
2705 if (nd->nd_flag & ND_NFSV4) {
2706 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2707 *tl = txdr_unsigned(NFDIR);
2709 (void) nfsm_strtom(nd, name, namelen);
2710 nfscl_fillsattr(nd, vap, dvp, NFSSATTR_SIZENEG1, 0);
2711 if (nd->nd_flag & ND_NFSV4) {
2712 NFSGETATTR_ATTRBIT(&attrbits);
2713 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2714 *tl++ = txdr_unsigned(NFSV4OP_GETFH);
2715 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2716 (void) nfsrv_putattrbit(nd, &attrbits);
2717 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2718 *tl = txdr_unsigned(NFSV4OP_PUTFH);
2719 (void) nfsm_fhtom(nd, fhp->nfh_fh, fhp->nfh_len, 0);
2720 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2721 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2722 (void) nfsrv_putattrbit(nd, &attrbits);
2724 error = nfscl_request(nd, dvp, p, cred, dstuff);
2727 if (nd->nd_flag & ND_NFSV4)
2728 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
2729 if (!nd->nd_repstat && !error) {
2730 if (nd->nd_flag & ND_NFSV4) {
2731 NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
2732 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
2735 error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp);
2736 if (error == 0 && (nd->nd_flag & ND_NFSV4) != 0) {
2737 /* Get rid of the PutFH and Getattr status values. */
2738 NFSM_DISSECT(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
2739 /* Load the directory attributes. */
2740 error = nfsm_loadattr(nd, dnap);
2745 if ((nd->nd_flag & ND_NFSV3) && !error)
2746 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
2747 if (nd->nd_repstat && !error)
2748 error = nd->nd_repstat;
2750 mbuf_freem(nd->nd_mrep);
2752 * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry.
2753 * Only do this if vfs.nfs.ignore_eexist is set.
2754 * Never do this for NFSv4.1 or later minor versions, since sessions
2755 * should guarantee "exactly once" RPC semantics.
2757 if (error == EEXIST && nfsignore_eexist != 0 && (!NFSHASNFSV4(nmp) ||
2758 nmp->nm_minorvers == 0))
2764 * nfs remove directory call
2767 nfsrpc_rmdir(vnode_t dvp, char *name, int namelen, struct ucred *cred,
2768 NFSPROC_T *p, struct nfsvattr *dnap, int *dattrflagp, void *dstuff)
2770 struct nfsrv_descript nfsd, *nd = &nfsd;
2774 if (namelen > NFS_MAXNAMLEN)
2775 return (ENAMETOOLONG);
2776 NFSCL_REQSTART(nd, NFSPROC_RMDIR, dvp);
2777 (void) nfsm_strtom(nd, name, namelen);
2778 error = nfscl_request(nd, dvp, p, cred, dstuff);
2781 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4))
2782 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
2783 if (nd->nd_repstat && !error)
2784 error = nd->nd_repstat;
2785 mbuf_freem(nd->nd_mrep);
2787 * Kludge: Map ENOENT => 0 assuming that you have a reply to a retry.
2789 if (error == ENOENT)
2796 * Always returns with either uio_resid unchanged, if you are at the
2797 * end of the directory, or uio_resid == 0, with all DIRBLKSIZ chunks
2799 * I felt this would allow caching of directory blocks more easily
2800 * than returning a pertially filled block.
2801 * Directory offset cookies:
2802 * Oh my, what to do with them...
2803 * I can think of three ways to deal with them:
2804 * 1 - have the layer above these RPCs maintain a map between logical
2805 * directory byte offsets and the NFS directory offset cookies
2806 * 2 - pass the opaque directory offset cookies up into userland
2807 * and let the libc functions deal with them, via the system call
2808 * 3 - return them to userland in the "struct dirent", so future versions
2809 * of libc can use them and do whatever is necessary to make things work
2810 * above these rpc calls, in the meantime
2811 * For now, I do #3 by "hiding" the directory offset cookies after the
2812 * d_name field in struct dirent. This is space inside d_reclen that
2813 * will be ignored by anything that doesn't know about them.
2814 * The directory offset cookies are filled in as the last 8 bytes of
2815 * each directory entry, after d_name. Someday, the userland libc
2816 * functions may be able to use these. In the meantime, it satisfies
2817 * OpenBSD's requirements for cookies being returned.
2818 * If expects the directory offset cookie for the read to be in uio_offset
2819 * and returns the one for the next entry after this directory block in
2823 nfsrpc_readdir(vnode_t vp, struct uio *uiop, nfsuint64 *cookiep,
2824 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp,
2825 int *eofp, void *stuff)
2828 struct dirent *dp = NULL;
2830 nfsquad_t cookie, ncookie;
2831 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
2832 struct nfsnode *dnp = VTONFS(vp);
2833 struct nfsvattr nfsva;
2834 struct nfsrv_descript nfsd, *nd = &nfsd;
2835 int error = 0, tlen, more_dirs = 1, blksiz = 0, bigenough = 1;
2836 int reqsize, tryformoredirs = 1, readsize, eof = 0, gotmnton = 0;
2837 u_int64_t dotfileid, dotdotfileid = 0, fakefileno = UINT64_MAX;
2839 nfsattrbit_t attrbits, dattrbits;
2840 u_int32_t rderr, *tl2 = NULL;
2843 KASSERT(uiop->uio_iovcnt == 1 &&
2844 (uio_uio_resid(uiop) & (DIRBLKSIZ - 1)) == 0,
2845 ("nfs readdirrpc bad uio"));
2848 * There is no point in reading a lot more than uio_resid, however
2849 * adding one additional DIRBLKSIZ makes sense. Since uio_resid
2850 * and nm_readdirsize are both exact multiples of DIRBLKSIZ, this
2851 * will never make readsize > nm_readdirsize.
2853 readsize = nmp->nm_readdirsize;
2854 if (readsize > uio_uio_resid(uiop))
2855 readsize = uio_uio_resid(uiop) + DIRBLKSIZ;
2860 tresid = uio_uio_resid(uiop);
2861 cookie.lval[0] = cookiep->nfsuquad[0];
2862 cookie.lval[1] = cookiep->nfsuquad[1];
2866 * For NFSv4, first create the "." and ".." entries.
2868 if (NFSHASNFSV4(nmp)) {
2869 reqsize = 6 * NFSX_UNSIGNED;
2870 NFSGETATTR_ATTRBIT(&dattrbits);
2871 NFSZERO_ATTRBIT(&attrbits);
2872 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_FILEID);
2873 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TYPE);
2874 if (NFSISSET_ATTRBIT(&dnp->n_vattr.na_suppattr,
2875 NFSATTRBIT_MOUNTEDONFILEID)) {
2876 NFSSETBIT_ATTRBIT(&attrbits,
2877 NFSATTRBIT_MOUNTEDONFILEID);
2881 * Must fake it. Use the fileno, except when the
2882 * fsid is != to that of the directory. For that
2883 * case, generate a fake fileno that is not the same.
2885 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_FSID);
2890 * Joy, oh joy. For V4 we get to hand craft '.' and '..'.
2892 if (uiop->uio_offset == 0) {
2893 NFSCL_REQSTART(nd, NFSPROC_LOOKUPP, vp);
2894 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2895 *tl++ = txdr_unsigned(NFSV4OP_GETFH);
2896 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2897 (void) nfsrv_putattrbit(nd, &attrbits);
2898 error = nfscl_request(nd, vp, p, cred, stuff);
2901 dotfileid = 0; /* Fake out the compiler. */
2902 if ((nd->nd_flag & ND_NOMOREDATA) == 0) {
2903 error = nfsm_loadattr(nd, &nfsva);
2906 dotfileid = nfsva.na_fileid;
2908 if (nd->nd_repstat == 0) {
2909 NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
2910 len = fxdr_unsigned(int, *(tl + 4));
2911 if (len > 0 && len <= NFSX_V4FHMAX)
2912 error = nfsm_advance(nd, NFSM_RNDUP(len), -1);
2916 NFSM_DISSECT(tl, u_int32_t *, 2*NFSX_UNSIGNED);
2917 nfsva.na_mntonfileno = UINT64_MAX;
2918 error = nfsv4_loadattr(nd, NULL, &nfsva, NULL,
2919 NULL, 0, NULL, NULL, NULL, NULL, NULL, 0,
2920 NULL, NULL, NULL, p, cred);
2922 dotdotfileid = dotfileid;
2923 } else if (gotmnton) {
2924 if (nfsva.na_mntonfileno != UINT64_MAX)
2925 dotdotfileid = nfsva.na_mntonfileno;
2927 dotdotfileid = nfsva.na_fileid;
2928 } else if (nfsva.na_filesid[0] ==
2929 dnp->n_vattr.na_filesid[0] &&
2930 nfsva.na_filesid[1] ==
2931 dnp->n_vattr.na_filesid[1]) {
2932 dotdotfileid = nfsva.na_fileid;
2936 } while (fakefileno ==
2938 dotdotfileid = fakefileno;
2941 } else if (nd->nd_repstat == NFSERR_NOENT) {
2943 * Lookupp returns NFSERR_NOENT when we are
2944 * at the root, so just use the current dir.
2947 dotdotfileid = dotfileid;
2949 error = nd->nd_repstat;
2951 mbuf_freem(nd->nd_mrep);
2955 dp = (struct dirent *)uio_iov_base(uiop);
2957 dp->d_type = DT_DIR;
2958 dp->d_fileno = dotfileid;
2960 *((uint64_t *)dp->d_name) = 0; /* Zero pad it. */
2961 dp->d_name[0] = '.';
2962 dp->d_reclen = _GENERIC_DIRSIZ(dp) + NFSX_HYPER;
2964 * Just make these offset cookie 0.
2966 tl = (u_int32_t *)&dp->d_name[8];
2969 blksiz += dp->d_reclen;
2970 uio_uio_resid_add(uiop, -(dp->d_reclen));
2971 uiop->uio_offset += dp->d_reclen;
2972 uio_iov_base_add(uiop, dp->d_reclen);
2973 uio_iov_len_add(uiop, -(dp->d_reclen));
2974 dp = (struct dirent *)uio_iov_base(uiop);
2976 dp->d_type = DT_DIR;
2977 dp->d_fileno = dotdotfileid;
2979 *((uint64_t *)dp->d_name) = 0;
2980 dp->d_name[0] = '.';
2981 dp->d_name[1] = '.';
2982 dp->d_reclen = _GENERIC_DIRSIZ(dp) + NFSX_HYPER;
2984 * Just make these offset cookie 0.
2986 tl = (u_int32_t *)&dp->d_name[8];
2989 blksiz += dp->d_reclen;
2990 uio_uio_resid_add(uiop, -(dp->d_reclen));
2991 uiop->uio_offset += dp->d_reclen;
2992 uio_iov_base_add(uiop, dp->d_reclen);
2993 uio_iov_len_add(uiop, -(dp->d_reclen));
2995 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_RDATTRERROR);
2997 reqsize = 5 * NFSX_UNSIGNED;
3002 * Loop around doing readdir rpc's of size readsize.
3003 * The stopping criteria is EOF or buffer full.
3005 while (more_dirs && bigenough) {
3007 NFSCL_REQSTART(nd, NFSPROC_READDIR, vp);
3008 if (nd->nd_flag & ND_NFSV2) {
3009 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
3010 *tl++ = cookie.lval[1];
3011 *tl = txdr_unsigned(readsize);
3013 NFSM_BUILD(tl, u_int32_t *, reqsize);
3014 *tl++ = cookie.lval[0];
3015 *tl++ = cookie.lval[1];
3016 if (cookie.qval == 0) {
3021 *tl++ = dnp->n_cookieverf.nfsuquad[0];
3022 *tl++ = dnp->n_cookieverf.nfsuquad[1];
3025 if (nd->nd_flag & ND_NFSV4) {
3026 *tl++ = txdr_unsigned(readsize);
3027 *tl = txdr_unsigned(readsize);
3028 (void) nfsrv_putattrbit(nd, &attrbits);
3029 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
3030 *tl = txdr_unsigned(NFSV4OP_GETATTR);
3031 (void) nfsrv_putattrbit(nd, &dattrbits);
3033 *tl = txdr_unsigned(readsize);
3036 error = nfscl_request(nd, vp, p, cred, stuff);
3039 if (!(nd->nd_flag & ND_NFSV2)) {
3040 if (nd->nd_flag & ND_NFSV3)
3041 error = nfscl_postop_attr(nd, nap, attrflagp,
3043 if (!nd->nd_repstat && !error) {
3044 NFSM_DISSECT(tl, u_int32_t *, NFSX_HYPER);
3046 dnp->n_cookieverf.nfsuquad[0] = *tl++;
3047 dnp->n_cookieverf.nfsuquad[1] = *tl;
3051 if (nd->nd_repstat || error) {
3053 error = nd->nd_repstat;
3056 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3057 more_dirs = fxdr_unsigned(int, *tl);
3061 /* loop through the dir entries, doctoring them to 4bsd form */
3062 while (more_dirs && bigenough) {
3063 if (nd->nd_flag & ND_NFSV4) {
3064 NFSM_DISSECT(tl, u_int32_t *, 3*NFSX_UNSIGNED);
3065 ncookie.lval[0] = *tl++;
3066 ncookie.lval[1] = *tl++;
3067 len = fxdr_unsigned(int, *tl);
3068 } else if (nd->nd_flag & ND_NFSV3) {
3069 NFSM_DISSECT(tl, u_int32_t *, 3*NFSX_UNSIGNED);
3070 nfsva.na_fileid = fxdr_hyper(tl);
3072 len = fxdr_unsigned(int, *tl);
3074 NFSM_DISSECT(tl, u_int32_t *, 2*NFSX_UNSIGNED);
3075 nfsva.na_fileid = fxdr_unsigned(uint64_t,
3077 len = fxdr_unsigned(int, *tl);
3079 if (len <= 0 || len > NFS_MAXNAMLEN) {
3083 tlen = roundup2(len, 8);
3085 tlen += 8; /* To ensure null termination. */
3086 left = DIRBLKSIZ - blksiz;
3087 if (_GENERIC_DIRLEN(len) + NFSX_HYPER > left) {
3088 dp->d_reclen += left;
3089 uio_iov_base_add(uiop, left);
3090 uio_iov_len_add(uiop, -(left));
3091 uio_uio_resid_add(uiop, -(left));
3092 uiop->uio_offset += left;
3095 if (_GENERIC_DIRLEN(len) + NFSX_HYPER >
3096 uio_uio_resid(uiop))
3099 dp = (struct dirent *)uio_iov_base(uiop);
3102 dp->d_reclen = _GENERIC_DIRLEN(len) +
3104 dp->d_type = DT_UNKNOWN;
3105 blksiz += dp->d_reclen;
3106 if (blksiz == DIRBLKSIZ)
3108 uio_uio_resid_add(uiop, -(DIRHDSIZ));
3109 uiop->uio_offset += DIRHDSIZ;
3110 uio_iov_base_add(uiop, DIRHDSIZ);
3111 uio_iov_len_add(uiop, -(DIRHDSIZ));
3112 error = nfsm_mbufuio(nd, uiop, len);
3115 cp = uio_iov_base(uiop);
3117 *cp = '\0'; /* null terminate */
3118 cp += tlen; /* points to cookie storage */
3119 tl2 = (u_int32_t *)cp;
3120 uio_iov_base_add(uiop, (tlen + NFSX_HYPER));
3121 uio_iov_len_add(uiop, -(tlen + NFSX_HYPER));
3122 uio_uio_resid_add(uiop, -(tlen + NFSX_HYPER));
3123 uiop->uio_offset += (tlen + NFSX_HYPER);
3125 error = nfsm_advance(nd, NFSM_RNDUP(len), -1);
3129 if (nd->nd_flag & ND_NFSV4) {
3131 nfsva.na_mntonfileno = UINT64_MAX;
3132 error = nfsv4_loadattr(nd, NULL, &nfsva, NULL,
3133 NULL, 0, NULL, NULL, NULL, NULL, NULL, 0,
3134 NULL, NULL, &rderr, p, cred);
3137 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3138 } else if (nd->nd_flag & ND_NFSV3) {
3139 NFSM_DISSECT(tl, u_int32_t *, 3*NFSX_UNSIGNED);
3140 ncookie.lval[0] = *tl++;
3141 ncookie.lval[1] = *tl++;
3143 NFSM_DISSECT(tl, u_int32_t *, 2*NFSX_UNSIGNED);
3144 ncookie.lval[0] = 0;
3145 ncookie.lval[1] = *tl++;
3148 if (nd->nd_flag & ND_NFSV4) {
3153 if (nfsva.na_mntonfileno != UINT64_MAX)
3154 dp->d_fileno = nfsva.na_mntonfileno;
3156 dp->d_fileno = nfsva.na_fileid;
3157 } else if (nfsva.na_filesid[0] ==
3158 dnp->n_vattr.na_filesid[0] &&
3159 nfsva.na_filesid[1] ==
3160 dnp->n_vattr.na_filesid[1]) {
3161 dp->d_fileno = nfsva.na_fileid;
3165 } while (fakefileno ==
3167 dp->d_fileno = fakefileno;
3169 dp->d_type = vtonfs_dtype(nfsva.na_type);
3172 dp->d_fileno = nfsva.na_fileid;
3174 *tl2++ = cookiep->nfsuquad[0] = cookie.lval[0] =
3176 *tl2 = cookiep->nfsuquad[1] = cookie.lval[1] =
3179 more_dirs = fxdr_unsigned(int, *tl);
3182 * If at end of rpc data, get the eof boolean
3185 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3186 eof = fxdr_unsigned(int, *tl);
3189 if (nd->nd_flag & ND_NFSV4) {
3190 error = nfscl_postop_attr(nd, nap, attrflagp,
3196 mbuf_freem(nd->nd_mrep);
3200 * Fill last record, iff any, out to a multiple of DIRBLKSIZ
3201 * by increasing d_reclen for the last record.
3204 left = DIRBLKSIZ - blksiz;
3205 dp->d_reclen += left;
3206 uio_iov_base_add(uiop, left);
3207 uio_iov_len_add(uiop, -(left));
3208 uio_uio_resid_add(uiop, -(left));
3209 uiop->uio_offset += left;
3213 * If returning no data, assume end of file.
3214 * If not bigenough, return not end of file, since you aren't
3215 * returning all the data
3216 * Otherwise, return the eof flag from the server.
3219 if (tresid == ((size_t)(uio_uio_resid(uiop))))
3221 else if (!bigenough)
3228 * Add extra empty records to any remaining DIRBLKSIZ chunks.
3230 while (uio_uio_resid(uiop) > 0 && uio_uio_resid(uiop) != tresid) {
3231 dp = (struct dirent *)uio_iov_base(uiop);
3232 dp->d_type = DT_UNKNOWN;
3235 dp->d_name[0] = '\0';
3236 tl = (u_int32_t *)&dp->d_name[4];
3237 *tl++ = cookie.lval[0];
3238 *tl = cookie.lval[1];
3239 dp->d_reclen = DIRBLKSIZ;
3240 uio_iov_base_add(uiop, DIRBLKSIZ);
3241 uio_iov_len_add(uiop, -(DIRBLKSIZ));
3242 uio_uio_resid_add(uiop, -(DIRBLKSIZ));
3243 uiop->uio_offset += DIRBLKSIZ;
3247 if (nd->nd_mrep != NULL)
3248 mbuf_freem(nd->nd_mrep);
3254 * NFS V3 readdir plus RPC. Used in place of nfsrpc_readdir().
3255 * (Also used for NFS V4 when mount flag set.)
3256 * (ditto above w.r.t. multiple of DIRBLKSIZ, etc.)
3259 nfsrpc_readdirplus(vnode_t vp, struct uio *uiop, nfsuint64 *cookiep,
3260 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp,
3261 int *eofp, void *stuff)
3264 struct dirent *dp = NULL;
3266 vnode_t newvp = NULLVP;
3267 struct nfsrv_descript nfsd, *nd = &nfsd;
3268 struct nameidata nami, *ndp = &nami;
3269 struct componentname *cnp = &ndp->ni_cnd;
3270 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
3271 struct nfsnode *dnp = VTONFS(vp), *np;
3272 struct nfsvattr nfsva;
3274 nfsquad_t cookie, ncookie;
3275 int error = 0, tlen, more_dirs = 1, blksiz = 0, bigenough = 1;
3276 int attrflag, tryformoredirs = 1, eof = 0, gotmnton = 0;
3277 int isdotdot = 0, unlocknewvp = 0;
3278 u_int64_t dotfileid, dotdotfileid = 0, fakefileno = UINT64_MAX;
3279 u_int64_t fileno = 0;
3281 nfsattrbit_t attrbits, dattrbits;
3283 u_int32_t *tl2 = NULL, rderr;
3284 struct timespec dctime;
3286 KASSERT(uiop->uio_iovcnt == 1 &&
3287 (uio_uio_resid(uiop) & (DIRBLKSIZ - 1)) == 0,
3288 ("nfs readdirplusrpc bad uio"));
3289 timespecclear(&dctime);
3295 cookie.lval[0] = cookiep->nfsuquad[0];
3296 cookie.lval[1] = cookiep->nfsuquad[1];
3297 tresid = uio_uio_resid(uiop);
3300 * For NFSv4, first create the "." and ".." entries.
3302 if (NFSHASNFSV4(nmp)) {
3303 NFSGETATTR_ATTRBIT(&dattrbits);
3304 NFSZERO_ATTRBIT(&attrbits);
3305 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_FILEID);
3306 if (NFSISSET_ATTRBIT(&dnp->n_vattr.na_suppattr,
3307 NFSATTRBIT_MOUNTEDONFILEID)) {
3308 NFSSETBIT_ATTRBIT(&attrbits,
3309 NFSATTRBIT_MOUNTEDONFILEID);
3313 * Must fake it. Use the fileno, except when the
3314 * fsid is != to that of the directory. For that
3315 * case, generate a fake fileno that is not the same.
3317 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_FSID);
3322 * Joy, oh joy. For V4 we get to hand craft '.' and '..'.
3324 if (uiop->uio_offset == 0) {
3325 NFSCL_REQSTART(nd, NFSPROC_LOOKUPP, vp);
3326 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
3327 *tl++ = txdr_unsigned(NFSV4OP_GETFH);
3328 *tl = txdr_unsigned(NFSV4OP_GETATTR);
3329 (void) nfsrv_putattrbit(nd, &attrbits);
3330 error = nfscl_request(nd, vp, p, cred, stuff);
3333 dotfileid = 0; /* Fake out the compiler. */
3334 if ((nd->nd_flag & ND_NOMOREDATA) == 0) {
3335 error = nfsm_loadattr(nd, &nfsva);
3338 dctime = nfsva.na_ctime;
3339 dotfileid = nfsva.na_fileid;
3341 if (nd->nd_repstat == 0) {
3342 NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
3343 len = fxdr_unsigned(int, *(tl + 4));
3344 if (len > 0 && len <= NFSX_V4FHMAX)
3345 error = nfsm_advance(nd, NFSM_RNDUP(len), -1);
3349 NFSM_DISSECT(tl, u_int32_t *, 2*NFSX_UNSIGNED);
3350 nfsva.na_mntonfileno = UINT64_MAX;
3351 error = nfsv4_loadattr(nd, NULL, &nfsva, NULL,
3352 NULL, 0, NULL, NULL, NULL, NULL, NULL, 0,
3353 NULL, NULL, NULL, p, cred);
3355 dotdotfileid = dotfileid;
3356 } else if (gotmnton) {
3357 if (nfsva.na_mntonfileno != UINT64_MAX)
3358 dotdotfileid = nfsva.na_mntonfileno;
3360 dotdotfileid = nfsva.na_fileid;
3361 } else if (nfsva.na_filesid[0] ==
3362 dnp->n_vattr.na_filesid[0] &&
3363 nfsva.na_filesid[1] ==
3364 dnp->n_vattr.na_filesid[1]) {
3365 dotdotfileid = nfsva.na_fileid;
3369 } while (fakefileno ==
3371 dotdotfileid = fakefileno;
3374 } else if (nd->nd_repstat == NFSERR_NOENT) {
3376 * Lookupp returns NFSERR_NOENT when we are
3377 * at the root, so just use the current dir.
3380 dotdotfileid = dotfileid;
3382 error = nd->nd_repstat;
3384 mbuf_freem(nd->nd_mrep);
3388 dp = (struct dirent *)uio_iov_base(uiop);
3390 dp->d_type = DT_DIR;
3391 dp->d_fileno = dotfileid;
3393 *((uint64_t *)dp->d_name) = 0; /* Zero pad it. */
3394 dp->d_name[0] = '.';
3395 dp->d_reclen = _GENERIC_DIRSIZ(dp) + NFSX_HYPER;
3397 * Just make these offset cookie 0.
3399 tl = (u_int32_t *)&dp->d_name[8];
3402 blksiz += dp->d_reclen;
3403 uio_uio_resid_add(uiop, -(dp->d_reclen));
3404 uiop->uio_offset += dp->d_reclen;
3405 uio_iov_base_add(uiop, dp->d_reclen);
3406 uio_iov_len_add(uiop, -(dp->d_reclen));
3407 dp = (struct dirent *)uio_iov_base(uiop);
3409 dp->d_type = DT_DIR;
3410 dp->d_fileno = dotdotfileid;
3412 *((uint64_t *)dp->d_name) = 0;
3413 dp->d_name[0] = '.';
3414 dp->d_name[1] = '.';
3415 dp->d_reclen = _GENERIC_DIRSIZ(dp) + NFSX_HYPER;
3417 * Just make these offset cookie 0.
3419 tl = (u_int32_t *)&dp->d_name[8];
3422 blksiz += dp->d_reclen;
3423 uio_uio_resid_add(uiop, -(dp->d_reclen));
3424 uiop->uio_offset += dp->d_reclen;
3425 uio_iov_base_add(uiop, dp->d_reclen);
3426 uio_iov_len_add(uiop, -(dp->d_reclen));
3428 NFSREADDIRPLUS_ATTRBIT(&attrbits);
3430 NFSSETBIT_ATTRBIT(&attrbits,
3431 NFSATTRBIT_MOUNTEDONFILEID);
3435 * Loop around doing readdir rpc's of size nm_readdirsize.
3436 * The stopping criteria is EOF or buffer full.
3438 while (more_dirs && bigenough) {
3440 NFSCL_REQSTART(nd, NFSPROC_READDIRPLUS, vp);
3441 NFSM_BUILD(tl, u_int32_t *, 6 * NFSX_UNSIGNED);
3442 *tl++ = cookie.lval[0];
3443 *tl++ = cookie.lval[1];
3444 if (cookie.qval == 0) {
3449 *tl++ = dnp->n_cookieverf.nfsuquad[0];
3450 *tl++ = dnp->n_cookieverf.nfsuquad[1];
3453 *tl++ = txdr_unsigned(nmp->nm_readdirsize);
3454 *tl = txdr_unsigned(nmp->nm_readdirsize);
3455 if (nd->nd_flag & ND_NFSV4) {
3456 (void) nfsrv_putattrbit(nd, &attrbits);
3457 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
3458 *tl = txdr_unsigned(NFSV4OP_GETATTR);
3459 (void) nfsrv_putattrbit(nd, &dattrbits);
3461 error = nfscl_request(nd, vp, p, cred, stuff);
3464 if (nd->nd_flag & ND_NFSV3)
3465 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
3466 if (nd->nd_repstat || error) {
3468 error = nd->nd_repstat;
3471 if ((nd->nd_flag & ND_NFSV3) != 0 && *attrflagp != 0)
3472 dctime = nap->na_ctime;
3473 NFSM_DISSECT(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
3475 dnp->n_cookieverf.nfsuquad[0] = *tl++;
3476 dnp->n_cookieverf.nfsuquad[1] = *tl++;
3478 more_dirs = fxdr_unsigned(int, *tl);
3482 /* loop through the dir entries, doctoring them to 4bsd form */
3483 while (more_dirs && bigenough) {
3484 NFSM_DISSECT(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
3485 if (nd->nd_flag & ND_NFSV4) {
3486 ncookie.lval[0] = *tl++;
3487 ncookie.lval[1] = *tl++;
3489 fileno = fxdr_hyper(tl);
3492 len = fxdr_unsigned(int, *tl);
3493 if (len <= 0 || len > NFS_MAXNAMLEN) {
3497 tlen = roundup2(len, 8);
3499 tlen += 8; /* To ensure null termination. */
3500 left = DIRBLKSIZ - blksiz;
3501 if (_GENERIC_DIRLEN(len) + NFSX_HYPER > left) {
3502 dp->d_reclen += left;
3503 uio_iov_base_add(uiop, left);
3504 uio_iov_len_add(uiop, -(left));
3505 uio_uio_resid_add(uiop, -(left));
3506 uiop->uio_offset += left;
3509 if (_GENERIC_DIRLEN(len) + NFSX_HYPER >
3510 uio_uio_resid(uiop))
3513 dp = (struct dirent *)uio_iov_base(uiop);
3516 dp->d_reclen = _GENERIC_DIRLEN(len) +
3518 dp->d_type = DT_UNKNOWN;
3519 blksiz += dp->d_reclen;
3520 if (blksiz == DIRBLKSIZ)
3522 uio_uio_resid_add(uiop, -(DIRHDSIZ));
3523 uiop->uio_offset += DIRHDSIZ;
3524 uio_iov_base_add(uiop, DIRHDSIZ);
3525 uio_iov_len_add(uiop, -(DIRHDSIZ));
3526 cnp->cn_nameptr = uio_iov_base(uiop);
3527 cnp->cn_namelen = len;
3529 error = nfsm_mbufuio(nd, uiop, len);
3532 cp = uio_iov_base(uiop);
3535 cp += tlen; /* points to cookie storage */
3536 tl2 = (u_int32_t *)cp;
3537 if (len == 2 && cnp->cn_nameptr[0] == '.' &&
3538 cnp->cn_nameptr[1] == '.')
3542 uio_iov_base_add(uiop, (tlen + NFSX_HYPER));
3543 uio_iov_len_add(uiop, -(tlen + NFSX_HYPER));
3544 uio_uio_resid_add(uiop, -(tlen + NFSX_HYPER));
3545 uiop->uio_offset += (tlen + NFSX_HYPER);
3547 error = nfsm_advance(nd, NFSM_RNDUP(len), -1);
3552 if (nd->nd_flag & ND_NFSV3) {
3553 NFSM_DISSECT(tl, u_int32_t *, 3*NFSX_UNSIGNED);
3554 ncookie.lval[0] = *tl++;
3555 ncookie.lval[1] = *tl++;
3556 attrflag = fxdr_unsigned(int, *tl);
3558 error = nfsm_loadattr(nd, &nfsva);
3562 NFSM_DISSECT(tl,u_int32_t *,NFSX_UNSIGNED);
3564 error = nfsm_getfh(nd, &nfhp);
3568 if (!attrflag && nfhp != NULL) {
3569 FREE((caddr_t)nfhp, M_NFSFH);
3574 nfsva.na_mntonfileno = 0xffffffff;
3575 error = nfsv4_loadattr(nd, NULL, &nfsva, &nfhp,
3576 NULL, 0, NULL, NULL, NULL, NULL, NULL, 0,
3577 NULL, NULL, &rderr, p, cred);
3583 if (nd->nd_flag & ND_NFSV4) {
3586 } else if (gotmnton) {
3587 if (nfsva.na_mntonfileno != 0xffffffff)
3588 dp->d_fileno = nfsva.na_mntonfileno;
3590 dp->d_fileno = nfsva.na_fileid;
3591 } else if (nfsva.na_filesid[0] ==
3592 dnp->n_vattr.na_filesid[0] &&
3593 nfsva.na_filesid[1] ==
3594 dnp->n_vattr.na_filesid[1]) {
3595 dp->d_fileno = nfsva.na_fileid;
3599 } while (fakefileno ==
3601 dp->d_fileno = fakefileno;
3604 dp->d_fileno = fileno;
3606 *tl2++ = cookiep->nfsuquad[0] = cookie.lval[0] =
3608 *tl2 = cookiep->nfsuquad[1] = cookie.lval[1] =
3612 if (NFSRV_CMPFH(nfhp->nfh_fh, nfhp->nfh_len,
3613 dnp->n_fhp->nfh_fh, dnp->n_fhp->nfh_len)) {
3617 FREE((caddr_t)nfhp, M_NFSFH);
3619 } else if (isdotdot != 0) {
3621 * Skip doing a nfscl_nget() call for "..".
3622 * There's a race between acquiring the nfs
3623 * node here and lookups that look for the
3624 * directory being read (in the parent).
3625 * It would try to get a lock on ".." here,
3626 * owning the lock on the directory being
3627 * read. Lookup will hold the lock on ".."
3628 * and try to acquire the lock on the
3629 * directory being read.
3630 * If the directory is unlocked/relocked,
3631 * then there is a LOR with the buflock
3634 free(nfhp, M_NFSFH);
3636 error = nfscl_nget(vnode_mount(vp), vp,
3637 nfhp, cnp, p, &np, NULL, LK_EXCLUSIVE);
3644 if (newvp != NULLVP) {
3645 error = nfscl_loadattrcache(&newvp,
3646 &nfsva, NULL, NULL, 0, 0);
3655 vtonfs_dtype(np->n_vattr.na_type);
3657 NFSCNHASH(cnp, HASHINIT);
3658 if (cnp->cn_namelen <= NCHNAMLEN &&
3659 (newvp->v_type != VDIR ||
3660 dctime.tv_sec != 0)) {
3661 cache_enter_time(ndp->ni_dvp,
3664 newvp->v_type != VDIR ? NULL :
3674 } else if (nfhp != NULL) {
3675 FREE((caddr_t)nfhp, M_NFSFH);
3677 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3678 more_dirs = fxdr_unsigned(int, *tl);
3681 * If at end of rpc data, get the eof boolean
3684 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3685 eof = fxdr_unsigned(int, *tl);
3688 if (nd->nd_flag & ND_NFSV4) {
3689 error = nfscl_postop_attr(nd, nap, attrflagp,
3695 mbuf_freem(nd->nd_mrep);
3699 * Fill last record, iff any, out to a multiple of DIRBLKSIZ
3700 * by increasing d_reclen for the last record.
3703 left = DIRBLKSIZ - blksiz;
3704 dp->d_reclen += left;
3705 uio_iov_base_add(uiop, left);
3706 uio_iov_len_add(uiop, -(left));
3707 uio_uio_resid_add(uiop, -(left));
3708 uiop->uio_offset += left;
3712 * If returning no data, assume end of file.
3713 * If not bigenough, return not end of file, since you aren't
3714 * returning all the data
3715 * Otherwise, return the eof flag from the server.
3718 if (tresid == uio_uio_resid(uiop))
3720 else if (!bigenough)
3727 * Add extra empty records to any remaining DIRBLKSIZ chunks.
3729 while (uio_uio_resid(uiop) > 0 && uio_uio_resid(uiop) != tresid) {
3730 dp = (struct dirent *)uio_iov_base(uiop);
3731 dp->d_type = DT_UNKNOWN;
3734 dp->d_name[0] = '\0';
3735 tl = (u_int32_t *)&dp->d_name[4];
3736 *tl++ = cookie.lval[0];
3737 *tl = cookie.lval[1];
3738 dp->d_reclen = DIRBLKSIZ;
3739 uio_iov_base_add(uiop, DIRBLKSIZ);
3740 uio_iov_len_add(uiop, -(DIRBLKSIZ));
3741 uio_uio_resid_add(uiop, -(DIRBLKSIZ));
3742 uiop->uio_offset += DIRBLKSIZ;
3746 if (nd->nd_mrep != NULL)
3747 mbuf_freem(nd->nd_mrep);
3756 nfsrpc_commit(vnode_t vp, u_quad_t offset, int cnt, struct ucred *cred,
3757 NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, void *stuff)
3760 struct nfsrv_descript nfsd, *nd = &nfsd;
3761 nfsattrbit_t attrbits;
3763 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
3766 NFSCL_REQSTART(nd, NFSPROC_COMMIT, vp);
3767 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
3768 txdr_hyper(offset, tl);
3770 *tl = txdr_unsigned(cnt);
3771 if (nd->nd_flag & ND_NFSV4) {
3773 * And do a Getattr op.
3775 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
3776 *tl = txdr_unsigned(NFSV4OP_GETATTR);
3777 NFSGETATTR_ATTRBIT(&attrbits);
3778 (void) nfsrv_putattrbit(nd, &attrbits);
3780 error = nfscl_request(nd, vp, p, cred, stuff);
3783 error = nfscl_wcc_data(nd, vp, nap, attrflagp, NULL, stuff);
3784 if (!error && !nd->nd_repstat) {
3785 NFSM_DISSECT(tl, u_int32_t *, NFSX_VERF);
3787 if (NFSBCMP(nmp->nm_verf, tl, NFSX_VERF)) {
3788 NFSBCOPY(tl, nmp->nm_verf, NFSX_VERF);
3789 nd->nd_repstat = NFSERR_STALEWRITEVERF;
3792 if (nd->nd_flag & ND_NFSV4)
3793 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
3796 if (!error && nd->nd_repstat)
3797 error = nd->nd_repstat;
3798 mbuf_freem(nd->nd_mrep);
3803 * NFS byte range lock rpc.
3804 * (Mostly just calls one of the three lower level RPC routines.)
3807 nfsrpc_advlock(vnode_t vp, off_t size, int op, struct flock *fl,
3808 int reclaim, struct ucred *cred, NFSPROC_T *p, void *id, int flags)
3810 struct nfscllockowner *lp;
3811 struct nfsclclient *clp;
3813 struct nfsrv_descript nfsd, *nd = &nfsd;
3814 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
3817 u_int32_t clidrev = 0;
3818 int error = 0, newone = 0, expireret = 0, retrycnt, donelocally;
3822 * Convert the flock structure into a start and end and do POSIX
3825 switch (fl->l_whence) {
3829 * Caller is responsible for adding any necessary offset
3830 * when SEEK_CUR is used.
3832 start = fl->l_start;
3836 start = size + fl->l_start;
3837 off = size + fl->l_start;
3844 if (fl->l_len != 0) {
3845 end = start + fl->l_len - 1;
3856 if (op == F_GETLK) {
3857 error = nfscl_getcl(vnode_mount(vp), cred, p, 1, &clp);
3860 error = nfscl_lockt(vp, clp, off, len, fl, p, id, flags);
3862 clidrev = clp->nfsc_clientidrev;
3863 error = nfsrpc_lockt(nd, vp, clp, off, len, fl, cred,
3865 } else if (error == -1) {
3868 nfscl_clientrelease(clp);
3869 } else if (op == F_UNLCK && fl->l_type == F_UNLCK) {
3871 * We must loop around for all lockowner cases.
3874 error = nfscl_getcl(vnode_mount(vp), cred, p, 1, &clp);
3878 error = nfscl_relbytelock(vp, off, len, cred, p, callcnt,
3879 clp, id, flags, &lp, &dorpc);
3881 * If it returns a NULL lp, we're done.
3885 nfscl_clientrelease(clp);
3887 nfscl_releasealllocks(clp, vp, p, id, flags);
3890 if (nmp->nm_clp != NULL)
3891 clidrev = nmp->nm_clp->nfsc_clientidrev;
3895 * If the server doesn't support Posix lock semantics,
3896 * only allow locks on the entire file, since it won't
3897 * handle overlapping byte ranges.
3898 * There might still be a problem when a lock
3899 * upgrade/downgrade (read<->write) occurs, since the
3900 * server "might" expect an unlock first?
3902 if (dorpc && (lp->nfsl_open->nfso_posixlock ||
3903 (off == 0 && len == NFS64BITSSET))) {
3905 * Since the lock records will go away, we must
3906 * wait for grace and delay here.
3909 error = nfsrpc_locku(nd, nmp, lp, off, len,
3910 NFSV4LOCKT_READ, cred, p, 0);
3911 if ((nd->nd_repstat == NFSERR_GRACE ||
3912 nd->nd_repstat == NFSERR_DELAY) &&
3914 (void) nfs_catnap(PZERO, (int)nd->nd_repstat,
3916 } while ((nd->nd_repstat == NFSERR_GRACE ||
3917 nd->nd_repstat == NFSERR_DELAY) && error == 0);
3920 } while (error == 0 && nd->nd_repstat == 0);
3921 nfscl_releasealllocks(clp, vp, p, id, flags);
3922 } else if (op == F_SETLK) {
3923 error = nfscl_getbytelock(vp, off, len, fl->l_type, cred, p,
3924 NULL, 0, id, flags, NULL, NULL, &lp, &newone, &donelocally);
3925 if (error || donelocally) {
3928 if (nmp->nm_clp != NULL)
3929 clidrev = nmp->nm_clp->nfsc_clientidrev;
3932 nfhp = VTONFS(vp)->n_fhp;
3933 if (!lp->nfsl_open->nfso_posixlock &&
3934 (off != 0 || len != NFS64BITSSET)) {
3937 error = nfsrpc_lock(nd, nmp, vp, nfhp->nfh_fh,
3938 nfhp->nfh_len, lp, newone, reclaim, off,
3939 len, fl->l_type, cred, p, 0);
3942 error = nd->nd_repstat;
3943 nfscl_lockrelease(lp, error, newone);
3948 error = nd->nd_repstat;
3949 if (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
3950 error == NFSERR_STALEDONTRECOVER ||
3951 error == NFSERR_STALECLIENTID || error == NFSERR_DELAY ||
3952 error == NFSERR_BADSESSION) {
3953 (void) nfs_catnap(PZERO, error, "nfs_advlock");
3954 } else if ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID)
3956 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
3959 } while (error == NFSERR_GRACE ||
3960 error == NFSERR_STALECLIENTID || error == NFSERR_DELAY ||
3961 error == NFSERR_STALEDONTRECOVER || error == NFSERR_STALESTATEID ||
3962 error == NFSERR_BADSESSION ||
3963 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
3964 expireret == 0 && clidrev != 0 && retrycnt < 4));
3965 if (error && retrycnt >= 4)
3971 * The lower level routine for the LockT case.
3974 nfsrpc_lockt(struct nfsrv_descript *nd, vnode_t vp,
3975 struct nfsclclient *clp, u_int64_t off, u_int64_t len, struct flock *fl,
3976 struct ucred *cred, NFSPROC_T *p, void *id, int flags)
3979 int error, type, size;
3980 uint8_t own[NFSV4CL_LOCKNAMELEN + NFSX_V4FHMAX];
3982 struct nfsmount *nmp;
3983 struct nfsclsession *tsep;
3985 nmp = VFSTONFS(vp->v_mount);
3986 NFSCL_REQSTART(nd, NFSPROC_LOCKT, vp);
3987 NFSM_BUILD(tl, u_int32_t *, 7 * NFSX_UNSIGNED);
3988 if (fl->l_type == F_RDLCK)
3989 *tl++ = txdr_unsigned(NFSV4LOCKT_READ);
3991 *tl++ = txdr_unsigned(NFSV4LOCKT_WRITE);
3992 txdr_hyper(off, tl);
3994 txdr_hyper(len, tl);
3996 tsep = nfsmnt_mdssession(nmp);
3997 *tl++ = tsep->nfsess_clientid.lval[0];
3998 *tl = tsep->nfsess_clientid.lval[1];
3999 nfscl_filllockowner(id, own, flags);
4001 NFSBCOPY(np->n_fhp->nfh_fh, &own[NFSV4CL_LOCKNAMELEN],
4002 np->n_fhp->nfh_len);
4003 (void)nfsm_strtom(nd, own, NFSV4CL_LOCKNAMELEN + np->n_fhp->nfh_len);
4004 error = nfscl_request(nd, vp, p, cred, NULL);
4007 if (nd->nd_repstat == 0) {
4008 fl->l_type = F_UNLCK;
4009 } else if (nd->nd_repstat == NFSERR_DENIED) {
4011 fl->l_whence = SEEK_SET;
4012 NFSM_DISSECT(tl, u_int32_t *, 8 * NFSX_UNSIGNED);
4013 fl->l_start = fxdr_hyper(tl);
4015 len = fxdr_hyper(tl);
4017 if (len == NFS64BITSSET)
4021 type = fxdr_unsigned(int, *tl++);
4022 if (type == NFSV4LOCKT_WRITE)
4023 fl->l_type = F_WRLCK;
4025 fl->l_type = F_RDLCK;
4027 * XXX For now, I have no idea what to do with the
4028 * conflicting lock_owner, so I'll just set the pid == 0
4029 * and skip over the lock_owner.
4031 fl->l_pid = (pid_t)0;
4033 size = fxdr_unsigned(int, *tl);
4034 if (size < 0 || size > NFSV4_OPAQUELIMIT)
4037 error = nfsm_advance(nd, NFSM_RNDUP(size), -1);
4038 } else if (nd->nd_repstat == NFSERR_STALECLIENTID)
4039 nfscl_initiate_recovery(clp);
4041 mbuf_freem(nd->nd_mrep);
4046 * Lower level function that performs the LockU RPC.
4049 nfsrpc_locku(struct nfsrv_descript *nd, struct nfsmount *nmp,
4050 struct nfscllockowner *lp, u_int64_t off, u_int64_t len,
4051 u_int32_t type, struct ucred *cred, NFSPROC_T *p, int syscred)
4056 nfscl_reqstart(nd, NFSPROC_LOCKU, nmp, lp->nfsl_open->nfso_fh,
4057 lp->nfsl_open->nfso_fhlen, NULL, NULL, 0, 0);
4058 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID + 6 * NFSX_UNSIGNED);
4059 *tl++ = txdr_unsigned(type);
4060 *tl = txdr_unsigned(lp->nfsl_seqid);
4061 if (nfstest_outofseq &&
4062 (arc4random() % nfstest_outofseq) == 0)
4063 *tl = txdr_unsigned(lp->nfsl_seqid + 1);
4065 if (NFSHASNFSV4N(nmp))
4068 *tl++ = lp->nfsl_stateid.seqid;
4069 *tl++ = lp->nfsl_stateid.other[0];
4070 *tl++ = lp->nfsl_stateid.other[1];
4071 *tl++ = lp->nfsl_stateid.other[2];
4072 txdr_hyper(off, tl);
4074 txdr_hyper(len, tl);
4076 nd->nd_flag |= ND_USEGSSNAME;
4077 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4078 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4079 NFSCL_INCRSEQID(lp->nfsl_seqid, nd);
4082 if (nd->nd_repstat == 0) {
4083 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID);
4084 lp->nfsl_stateid.seqid = *tl++;
4085 lp->nfsl_stateid.other[0] = *tl++;
4086 lp->nfsl_stateid.other[1] = *tl++;
4087 lp->nfsl_stateid.other[2] = *tl;
4088 } else if (nd->nd_repstat == NFSERR_STALESTATEID)
4089 nfscl_initiate_recovery(lp->nfsl_open->nfso_own->nfsow_clp);
4091 mbuf_freem(nd->nd_mrep);
4096 * The actual Lock RPC.
4099 nfsrpc_lock(struct nfsrv_descript *nd, struct nfsmount *nmp, vnode_t vp,
4100 u_int8_t *nfhp, int fhlen, struct nfscllockowner *lp, int newone,
4101 int reclaim, u_int64_t off, u_int64_t len, short type, struct ucred *cred,
4102 NFSPROC_T *p, int syscred)
4106 uint8_t own[NFSV4CL_LOCKNAMELEN + NFSX_V4FHMAX];
4107 struct nfsclsession *tsep;
4109 nfscl_reqstart(nd, NFSPROC_LOCK, nmp, nfhp, fhlen, NULL, NULL, 0, 0);
4110 NFSM_BUILD(tl, u_int32_t *, 7 * NFSX_UNSIGNED);
4111 if (type == F_RDLCK)
4112 *tl++ = txdr_unsigned(NFSV4LOCKT_READ);
4114 *tl++ = txdr_unsigned(NFSV4LOCKT_WRITE);
4115 *tl++ = txdr_unsigned(reclaim);
4116 txdr_hyper(off, tl);
4118 txdr_hyper(len, tl);
4122 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID +
4123 2 * NFSX_UNSIGNED + NFSX_HYPER);
4124 *tl++ = txdr_unsigned(lp->nfsl_open->nfso_own->nfsow_seqid);
4125 if (NFSHASNFSV4N(nmp))
4128 *tl++ = lp->nfsl_open->nfso_stateid.seqid;
4129 *tl++ = lp->nfsl_open->nfso_stateid.other[0];
4130 *tl++ = lp->nfsl_open->nfso_stateid.other[1];
4131 *tl++ = lp->nfsl_open->nfso_stateid.other[2];
4132 *tl++ = txdr_unsigned(lp->nfsl_seqid);
4133 tsep = nfsmnt_mdssession(nmp);
4134 *tl++ = tsep->nfsess_clientid.lval[0];
4135 *tl = tsep->nfsess_clientid.lval[1];
4136 NFSBCOPY(lp->nfsl_owner, own, NFSV4CL_LOCKNAMELEN);
4137 NFSBCOPY(nfhp, &own[NFSV4CL_LOCKNAMELEN], fhlen);
4138 (void)nfsm_strtom(nd, own, NFSV4CL_LOCKNAMELEN + fhlen);
4141 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID + NFSX_UNSIGNED);
4142 if (NFSHASNFSV4N(nmp))
4145 *tl++ = lp->nfsl_stateid.seqid;
4146 *tl++ = lp->nfsl_stateid.other[0];
4147 *tl++ = lp->nfsl_stateid.other[1];
4148 *tl++ = lp->nfsl_stateid.other[2];
4149 *tl = txdr_unsigned(lp->nfsl_seqid);
4150 if (nfstest_outofseq &&
4151 (arc4random() % nfstest_outofseq) == 0)
4152 *tl = txdr_unsigned(lp->nfsl_seqid + 1);
4155 nd->nd_flag |= ND_USEGSSNAME;
4156 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, vp, p, cred,
4157 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4161 NFSCL_INCRSEQID(lp->nfsl_open->nfso_own->nfsow_seqid, nd);
4162 NFSCL_INCRSEQID(lp->nfsl_seqid, nd);
4163 if (nd->nd_repstat == 0) {
4164 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID);
4165 lp->nfsl_stateid.seqid = *tl++;
4166 lp->nfsl_stateid.other[0] = *tl++;
4167 lp->nfsl_stateid.other[1] = *tl++;
4168 lp->nfsl_stateid.other[2] = *tl;
4169 } else if (nd->nd_repstat == NFSERR_DENIED) {
4170 NFSM_DISSECT(tl, u_int32_t *, 8 * NFSX_UNSIGNED);
4171 size = fxdr_unsigned(int, *(tl + 7));
4172 if (size < 0 || size > NFSV4_OPAQUELIMIT)
4175 error = nfsm_advance(nd, NFSM_RNDUP(size), -1);
4176 } else if (nd->nd_repstat == NFSERR_STALESTATEID)
4177 nfscl_initiate_recovery(lp->nfsl_open->nfso_own->nfsow_clp);
4179 mbuf_freem(nd->nd_mrep);
4185 * (always called with the vp for the mount point)
4188 nfsrpc_statfs(vnode_t vp, struct nfsstatfs *sbp, struct nfsfsinfo *fsp,
4189 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp,
4192 u_int32_t *tl = NULL;
4193 struct nfsrv_descript nfsd, *nd = &nfsd;
4194 struct nfsmount *nmp;
4195 nfsattrbit_t attrbits;
4199 nmp = VFSTONFS(vnode_mount(vp));
4200 if (NFSHASNFSV4(nmp)) {
4202 * For V4, you actually do a getattr.
4204 NFSCL_REQSTART(nd, NFSPROC_GETATTR, vp);
4205 NFSSTATFS_GETATTRBIT(&attrbits);
4206 (void) nfsrv_putattrbit(nd, &attrbits);
4207 nd->nd_flag |= ND_USEGSSNAME;
4208 error = nfscl_request(nd, vp, p, cred, stuff);
4211 if (nd->nd_repstat == 0) {
4212 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0,
4213 NULL, NULL, sbp, fsp, NULL, 0, NULL, NULL, NULL, p,
4216 nmp->nm_fsid[0] = nap->na_filesid[0];
4217 nmp->nm_fsid[1] = nap->na_filesid[1];
4218 NFSSETHASSETFSID(nmp);
4222 error = nd->nd_repstat;
4227 NFSCL_REQSTART(nd, NFSPROC_FSSTAT, vp);
4228 error = nfscl_request(nd, vp, p, cred, stuff);
4231 if (nd->nd_flag & ND_NFSV3) {
4232 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
4236 if (nd->nd_repstat) {
4237 error = nd->nd_repstat;
4240 NFSM_DISSECT(tl, u_int32_t *,
4241 NFSX_STATFS(nd->nd_flag & ND_NFSV3));
4243 if (NFSHASNFSV3(nmp)) {
4244 sbp->sf_tbytes = fxdr_hyper(tl); tl += 2;
4245 sbp->sf_fbytes = fxdr_hyper(tl); tl += 2;
4246 sbp->sf_abytes = fxdr_hyper(tl); tl += 2;
4247 sbp->sf_tfiles = fxdr_hyper(tl); tl += 2;
4248 sbp->sf_ffiles = fxdr_hyper(tl); tl += 2;
4249 sbp->sf_afiles = fxdr_hyper(tl); tl += 2;
4250 sbp->sf_invarsec = fxdr_unsigned(u_int32_t, *tl);
4251 } else if (NFSHASNFSV4(nmp) == 0) {
4252 sbp->sf_tsize = fxdr_unsigned(u_int32_t, *tl++);
4253 sbp->sf_bsize = fxdr_unsigned(u_int32_t, *tl++);
4254 sbp->sf_blocks = fxdr_unsigned(u_int32_t, *tl++);
4255 sbp->sf_bfree = fxdr_unsigned(u_int32_t, *tl++);
4256 sbp->sf_bavail = fxdr_unsigned(u_int32_t, *tl);
4259 mbuf_freem(nd->nd_mrep);
4267 nfsrpc_pathconf(vnode_t vp, struct nfsv3_pathconf *pc,
4268 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp,
4271 struct nfsrv_descript nfsd, *nd = &nfsd;
4272 struct nfsmount *nmp;
4274 nfsattrbit_t attrbits;
4278 nmp = VFSTONFS(vnode_mount(vp));
4279 if (NFSHASNFSV4(nmp)) {
4281 * For V4, you actually do a getattr.
4283 NFSCL_REQSTART(nd, NFSPROC_GETATTR, vp);
4284 NFSPATHCONF_GETATTRBIT(&attrbits);
4285 (void) nfsrv_putattrbit(nd, &attrbits);
4286 nd->nd_flag |= ND_USEGSSNAME;
4287 error = nfscl_request(nd, vp, p, cred, stuff);
4290 if (nd->nd_repstat == 0) {
4291 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0,
4292 pc, NULL, NULL, NULL, NULL, 0, NULL, NULL, NULL, p,
4297 error = nd->nd_repstat;
4300 NFSCL_REQSTART(nd, NFSPROC_PATHCONF, vp);
4301 error = nfscl_request(nd, vp, p, cred, stuff);
4304 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
4305 if (nd->nd_repstat && !error)
4306 error = nd->nd_repstat;
4308 NFSM_DISSECT(tl, u_int32_t *, NFSX_V3PATHCONF);
4309 pc->pc_linkmax = fxdr_unsigned(u_int32_t, *tl++);
4310 pc->pc_namemax = fxdr_unsigned(u_int32_t, *tl++);
4311 pc->pc_notrunc = fxdr_unsigned(u_int32_t, *tl++);
4312 pc->pc_chownrestricted =
4313 fxdr_unsigned(u_int32_t, *tl++);
4314 pc->pc_caseinsensitive =
4315 fxdr_unsigned(u_int32_t, *tl++);
4316 pc->pc_casepreserving = fxdr_unsigned(u_int32_t, *tl);
4320 mbuf_freem(nd->nd_mrep);
4325 * nfs version 3 fsinfo rpc call
4328 nfsrpc_fsinfo(vnode_t vp, struct nfsfsinfo *fsp, struct ucred *cred,
4329 NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, void *stuff)
4332 struct nfsrv_descript nfsd, *nd = &nfsd;
4336 NFSCL_REQSTART(nd, NFSPROC_FSINFO, vp);
4337 error = nfscl_request(nd, vp, p, cred, stuff);
4340 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
4341 if (nd->nd_repstat && !error)
4342 error = nd->nd_repstat;
4344 NFSM_DISSECT(tl, u_int32_t *, NFSX_V3FSINFO);
4345 fsp->fs_rtmax = fxdr_unsigned(u_int32_t, *tl++);
4346 fsp->fs_rtpref = fxdr_unsigned(u_int32_t, *tl++);
4347 fsp->fs_rtmult = fxdr_unsigned(u_int32_t, *tl++);
4348 fsp->fs_wtmax = fxdr_unsigned(u_int32_t, *tl++);
4349 fsp->fs_wtpref = fxdr_unsigned(u_int32_t, *tl++);
4350 fsp->fs_wtmult = fxdr_unsigned(u_int32_t, *tl++);
4351 fsp->fs_dtpref = fxdr_unsigned(u_int32_t, *tl++);
4352 fsp->fs_maxfilesize = fxdr_hyper(tl);
4354 fxdr_nfsv3time(tl, &fsp->fs_timedelta);
4356 fsp->fs_properties = fxdr_unsigned(u_int32_t, *tl);
4359 mbuf_freem(nd->nd_mrep);
4364 * This function performs the Renew RPC.
4367 nfsrpc_renew(struct nfsclclient *clp, struct nfsclds *dsp, struct ucred *cred,
4371 struct nfsrv_descript nfsd;
4372 struct nfsrv_descript *nd = &nfsd;
4373 struct nfsmount *nmp;
4375 struct nfssockreq *nrp;
4376 struct nfsclsession *tsep;
4378 nmp = clp->nfsc_nmp;
4382 nfscl_reqstart(nd, NFSPROC_RENEW, nmp, NULL, 0, NULL, NULL, 0,
4385 nfscl_reqstart(nd, NFSPROC_RENEW, nmp, NULL, 0, NULL,
4386 &dsp->nfsclds_sess, 0, 0);
4387 if (!NFSHASNFSV4N(nmp)) {
4388 /* NFSv4.1 just uses a Sequence Op and not a Renew. */
4389 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
4390 tsep = nfsmnt_mdssession(nmp);
4391 *tl++ = tsep->nfsess_clientid.lval[0];
4392 *tl = tsep->nfsess_clientid.lval[1];
4396 nrp = dsp->nfsclds_sockp;
4398 /* If NULL, use the MDS socket. */
4399 nrp = &nmp->nm_sockreq;
4400 nd->nd_flag |= ND_USEGSSNAME;
4402 error = newnfs_request(nd, nmp, NULL, nrp, NULL, p, cred,
4403 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4405 error = newnfs_request(nd, nmp, NULL, nrp, NULL, p, cred,
4406 NFS_PROG, NFS_VER4, NULL, 1, NULL, &dsp->nfsclds_sess);
4409 error = nd->nd_repstat;
4410 mbuf_freem(nd->nd_mrep);
4415 * This function performs the Releaselockowner RPC.
4418 nfsrpc_rellockown(struct nfsmount *nmp, struct nfscllockowner *lp,
4419 uint8_t *fh, int fhlen, struct ucred *cred, NFSPROC_T *p)
4421 struct nfsrv_descript nfsd, *nd = &nfsd;
4424 uint8_t own[NFSV4CL_LOCKNAMELEN + NFSX_V4FHMAX];
4425 struct nfsclsession *tsep;
4427 if (NFSHASNFSV4N(nmp)) {
4428 /* For NFSv4.1, do a FreeStateID. */
4429 nfscl_reqstart(nd, NFSPROC_FREESTATEID, nmp, NULL, 0, NULL,
4431 nfsm_stateidtom(nd, &lp->nfsl_stateid, NFSSTATEID_PUTSTATEID);
4433 nfscl_reqstart(nd, NFSPROC_RELEASELCKOWN, nmp, NULL, 0, NULL,
4435 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
4436 tsep = nfsmnt_mdssession(nmp);
4437 *tl++ = tsep->nfsess_clientid.lval[0];
4438 *tl = tsep->nfsess_clientid.lval[1];
4439 NFSBCOPY(lp->nfsl_owner, own, NFSV4CL_LOCKNAMELEN);
4440 NFSBCOPY(fh, &own[NFSV4CL_LOCKNAMELEN], fhlen);
4441 (void)nfsm_strtom(nd, own, NFSV4CL_LOCKNAMELEN + fhlen);
4443 nd->nd_flag |= ND_USEGSSNAME;
4444 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4445 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4448 error = nd->nd_repstat;
4449 mbuf_freem(nd->nd_mrep);
4454 * This function performs the Compound to get the mount pt FH.
4457 nfsrpc_getdirpath(struct nfsmount *nmp, u_char *dirpath, struct ucred *cred,
4461 struct nfsrv_descript nfsd;
4462 struct nfsrv_descript *nd = &nfsd;
4464 int error, cnt, len, setnil;
4467 nfscl_reqstart(nd, NFSPROC_PUTROOTFH, nmp, NULL, 0, &opcntp, NULL, 0,
4476 while (*cp2 != '\0' && *cp2 != '/')
4483 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
4484 *tl = txdr_unsigned(NFSV4OP_LOOKUP);
4485 nfsm_strtom(nd, cp, strlen(cp));
4491 } while (*cp != '\0');
4492 if (NFSHASNFSV4N(nmp))
4493 /* Has a Sequence Op done by nfscl_reqstart(). */
4494 *opcntp = txdr_unsigned(3 + cnt);
4496 *opcntp = txdr_unsigned(2 + cnt);
4497 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
4498 *tl = txdr_unsigned(NFSV4OP_GETFH);
4499 nd->nd_flag |= ND_USEGSSNAME;
4500 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4501 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4504 if (nd->nd_repstat == 0) {
4505 NFSM_DISSECT(tl, u_int32_t *, (3 + 2 * cnt) * NFSX_UNSIGNED);
4506 tl += (2 + 2 * cnt);
4507 if ((len = fxdr_unsigned(int, *tl)) <= 0 ||
4509 nd->nd_repstat = NFSERR_BADXDR;
4511 nd->nd_repstat = nfsrv_mtostr(nd, nmp->nm_fh, len);
4512 if (nd->nd_repstat == 0)
4513 nmp->nm_fhsize = len;
4516 error = nd->nd_repstat;
4518 mbuf_freem(nd->nd_mrep);
4523 * This function performs the Delegreturn RPC.
4526 nfsrpc_delegreturn(struct nfscldeleg *dp, struct ucred *cred,
4527 struct nfsmount *nmp, NFSPROC_T *p, int syscred)
4530 struct nfsrv_descript nfsd;
4531 struct nfsrv_descript *nd = &nfsd;
4534 nfscl_reqstart(nd, NFSPROC_DELEGRETURN, nmp, dp->nfsdl_fh,
4535 dp->nfsdl_fhlen, NULL, NULL, 0, 0);
4536 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID);
4537 if (NFSHASNFSV4N(nmp))
4540 *tl++ = dp->nfsdl_stateid.seqid;
4541 *tl++ = dp->nfsdl_stateid.other[0];
4542 *tl++ = dp->nfsdl_stateid.other[1];
4543 *tl = dp->nfsdl_stateid.other[2];
4545 nd->nd_flag |= ND_USEGSSNAME;
4546 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4547 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4550 error = nd->nd_repstat;
4551 mbuf_freem(nd->nd_mrep);
4559 nfsrpc_getacl(vnode_t vp, struct ucred *cred, NFSPROC_T *p,
4560 struct acl *aclp, void *stuff)
4562 struct nfsrv_descript nfsd, *nd = &nfsd;
4564 nfsattrbit_t attrbits;
4565 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
4567 if (nfsrv_useacl == 0 || !NFSHASNFSV4(nmp))
4568 return (EOPNOTSUPP);
4569 NFSCL_REQSTART(nd, NFSPROC_GETACL, vp);
4570 NFSZERO_ATTRBIT(&attrbits);
4571 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_ACL);
4572 (void) nfsrv_putattrbit(nd, &attrbits);
4573 error = nfscl_request(nd, vp, p, cred, stuff);
4576 if (!nd->nd_repstat)
4577 error = nfsv4_loadattr(nd, vp, NULL, NULL, NULL, 0, NULL,
4578 NULL, NULL, NULL, aclp, 0, NULL, NULL, NULL, p, cred);
4580 error = nd->nd_repstat;
4581 mbuf_freem(nd->nd_mrep);
4589 nfsrpc_setacl(vnode_t vp, struct ucred *cred, NFSPROC_T *p,
4590 struct acl *aclp, void *stuff)
4593 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
4595 if (nfsrv_useacl == 0 || !NFSHASNFSV4(nmp))
4596 return (EOPNOTSUPP);
4597 error = nfsrpc_setattr(vp, NULL, aclp, cred, p, NULL, NULL, stuff);
4605 nfsrpc_setaclrpc(vnode_t vp, struct ucred *cred, NFSPROC_T *p,
4606 struct acl *aclp, nfsv4stateid_t *stateidp, void *stuff)
4608 struct nfsrv_descript nfsd, *nd = &nfsd;
4610 nfsattrbit_t attrbits;
4611 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
4613 if (!NFSHASNFSV4(nmp))
4614 return (EOPNOTSUPP);
4615 NFSCL_REQSTART(nd, NFSPROC_SETACL, vp);
4616 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID);
4617 NFSZERO_ATTRBIT(&attrbits);
4618 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_ACL);
4619 (void) nfsv4_fillattr(nd, vnode_mount(vp), vp, aclp, NULL, NULL, 0,
4620 &attrbits, NULL, NULL, 0, 0, 0, 0, (uint64_t)0);
4621 error = nfscl_request(nd, vp, p, cred, stuff);
4624 /* Don't care about the pre/postop attributes */
4625 mbuf_freem(nd->nd_mrep);
4626 return (nd->nd_repstat);
4630 * Do the NFSv4.1 Exchange ID.
4633 nfsrpc_exchangeid(struct nfsmount *nmp, struct nfsclclient *clp,
4634 struct nfssockreq *nrp, uint32_t exchflags, struct nfsclds **dspp,
4635 struct ucred *cred, NFSPROC_T *p)
4637 uint32_t *tl, v41flags;
4638 struct nfsrv_descript nfsd;
4639 struct nfsrv_descript *nd = &nfsd;
4640 struct nfsclds *dsp;
4641 struct timespec verstime;
4645 nfscl_reqstart(nd, NFSPROC_EXCHANGEID, nmp, NULL, 0, NULL, NULL, 0, 0);
4646 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_UNSIGNED);
4647 *tl++ = txdr_unsigned(nfsboottime.tv_sec); /* Client owner */
4648 *tl = txdr_unsigned(clp->nfsc_rev);
4649 (void) nfsm_strtom(nd, clp->nfsc_id, clp->nfsc_idlen);
4651 NFSM_BUILD(tl, uint32_t *, 3 * NFSX_UNSIGNED);
4652 *tl++ = txdr_unsigned(exchflags);
4653 *tl++ = txdr_unsigned(NFSV4EXCH_SP4NONE);
4655 /* Set the implementation id4 */
4656 *tl = txdr_unsigned(1);
4657 (void) nfsm_strtom(nd, "freebsd.org", strlen("freebsd.org"));
4658 (void) nfsm_strtom(nd, version, strlen(version));
4659 NFSM_BUILD(tl, uint32_t *, NFSX_V4TIME);
4660 verstime.tv_sec = 1293840000; /* Jan 1, 2011 */
4661 verstime.tv_nsec = 0;
4662 txdr_nfsv4time(&verstime, tl);
4663 nd->nd_flag |= ND_USEGSSNAME;
4664 error = newnfs_request(nd, nmp, NULL, nrp, NULL, p, cred,
4665 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4666 NFSCL_DEBUG(1, "exchangeid err=%d reps=%d\n", error,
4667 (int)nd->nd_repstat);
4670 if (nd->nd_repstat == 0) {
4671 NFSM_DISSECT(tl, uint32_t *, 6 * NFSX_UNSIGNED + NFSX_HYPER);
4672 len = fxdr_unsigned(int, *(tl + 7));
4673 if (len < 0 || len > NFSV4_OPAQUELIMIT) {
4674 error = NFSERR_BADXDR;
4677 dsp = malloc(sizeof(struct nfsclds) + len + 1, M_NFSCLDS,
4679 dsp->nfsclds_expire = NFSD_MONOSEC + clp->nfsc_renew;
4680 dsp->nfsclds_servownlen = len;
4681 dsp->nfsclds_sess.nfsess_clientid.lval[0] = *tl++;
4682 dsp->nfsclds_sess.nfsess_clientid.lval[1] = *tl++;
4683 dsp->nfsclds_sess.nfsess_sequenceid =
4684 fxdr_unsigned(uint32_t, *tl++);
4685 v41flags = fxdr_unsigned(uint32_t, *tl);
4686 if ((v41flags & NFSV4EXCH_USEPNFSMDS) != 0 &&
4687 NFSHASPNFSOPT(nmp)) {
4688 NFSCL_DEBUG(1, "set PNFS\n");
4690 nmp->nm_state |= NFSSTA_PNFS;
4692 dsp->nfsclds_flags |= NFSCLDS_MDS;
4694 if ((v41flags & NFSV4EXCH_USEPNFSDS) != 0)
4695 dsp->nfsclds_flags |= NFSCLDS_DS;
4697 nd->nd_repstat = nfsrv_mtostr(nd,
4698 dsp->nfsclds_serverown, len);
4699 if (nd->nd_repstat == 0) {
4700 mtx_init(&dsp->nfsclds_mtx, "nfsds", NULL, MTX_DEF);
4701 mtx_init(&dsp->nfsclds_sess.nfsess_mtx, "nfssession",
4703 nfscl_initsessionslots(&dsp->nfsclds_sess);
4706 free(dsp, M_NFSCLDS);
4708 error = nd->nd_repstat;
4710 mbuf_freem(nd->nd_mrep);
4715 * Do the NFSv4.1 Create Session.
4718 nfsrpc_createsession(struct nfsmount *nmp, struct nfsclsession *sep,
4719 struct nfssockreq *nrp, uint32_t sequenceid, int mds, struct ucred *cred,
4722 uint32_t crflags, maxval, *tl;
4723 struct nfsrv_descript nfsd;
4724 struct nfsrv_descript *nd = &nfsd;
4727 /* Make sure nm_rsize, nm_wsize is set. */
4728 if (nmp->nm_rsize > NFS_MAXBSIZE || nmp->nm_rsize == 0)
4729 nmp->nm_rsize = NFS_MAXBSIZE;
4730 if (nmp->nm_wsize > NFS_MAXBSIZE || nmp->nm_wsize == 0)
4731 nmp->nm_wsize = NFS_MAXBSIZE;
4732 nfscl_reqstart(nd, NFSPROC_CREATESESSION, nmp, NULL, 0, NULL, NULL, 0,
4734 NFSM_BUILD(tl, uint32_t *, 4 * NFSX_UNSIGNED);
4735 *tl++ = sep->nfsess_clientid.lval[0];
4736 *tl++ = sep->nfsess_clientid.lval[1];
4737 *tl++ = txdr_unsigned(sequenceid);
4738 crflags = (NFSMNT_RDONLY(nmp->nm_mountp) ? 0 : NFSV4CRSESS_PERSIST);
4739 if (nfscl_enablecallb != 0 && nfs_numnfscbd > 0 && mds != 0)
4740 crflags |= NFSV4CRSESS_CONNBACKCHAN;
4741 *tl = txdr_unsigned(crflags);
4743 /* Fill in fore channel attributes. */
4744 NFSM_BUILD(tl, uint32_t *, 7 * NFSX_UNSIGNED);
4745 *tl++ = 0; /* Header pad size */
4746 *tl++ = txdr_unsigned(nmp->nm_wsize + NFS_MAXXDR);/* Max request size */
4747 *tl++ = txdr_unsigned(nmp->nm_rsize + NFS_MAXXDR);/* Max reply size */
4748 *tl++ = txdr_unsigned(4096); /* Max response size cached */
4749 *tl++ = txdr_unsigned(20); /* Max operations */
4750 *tl++ = txdr_unsigned(64); /* Max slots */
4751 *tl = 0; /* No rdma ird */
4753 /* Fill in back channel attributes. */
4754 NFSM_BUILD(tl, uint32_t *, 7 * NFSX_UNSIGNED);
4755 *tl++ = 0; /* Header pad size */
4756 *tl++ = txdr_unsigned(10000); /* Max request size */
4757 *tl++ = txdr_unsigned(10000); /* Max response size */
4758 *tl++ = txdr_unsigned(4096); /* Max response size cached */
4759 *tl++ = txdr_unsigned(4); /* Max operations */
4760 *tl++ = txdr_unsigned(NFSV4_CBSLOTS); /* Max slots */
4761 *tl = 0; /* No rdma ird */
4763 NFSM_BUILD(tl, uint32_t *, 8 * NFSX_UNSIGNED);
4764 *tl++ = txdr_unsigned(NFS_CALLBCKPROG); /* Call back prog # */
4766 /* Allow AUTH_SYS callbacks as uid, gid == 0. */
4767 *tl++ = txdr_unsigned(1); /* Auth_sys only */
4768 *tl++ = txdr_unsigned(AUTH_SYS); /* AUTH_SYS type */
4769 *tl++ = txdr_unsigned(nfsboottime.tv_sec); /* time stamp */
4770 *tl++ = 0; /* Null machine name */
4771 *tl++ = 0; /* Uid == 0 */
4772 *tl++ = 0; /* Gid == 0 */
4773 *tl = 0; /* No additional gids */
4774 nd->nd_flag |= ND_USEGSSNAME;
4775 error = newnfs_request(nd, nmp, NULL, nrp, NULL, p, cred, NFS_PROG,
4776 NFS_VER4, NULL, 1, NULL, NULL);
4779 if (nd->nd_repstat == 0) {
4780 NFSM_DISSECT(tl, uint32_t *, NFSX_V4SESSIONID +
4782 bcopy(tl, sep->nfsess_sessionid, NFSX_V4SESSIONID);
4783 tl += NFSX_V4SESSIONID / NFSX_UNSIGNED;
4784 sep->nfsess_sequenceid = fxdr_unsigned(uint32_t, *tl++);
4785 crflags = fxdr_unsigned(uint32_t, *tl);
4786 if ((crflags & NFSV4CRSESS_PERSIST) != 0 && mds != 0) {
4788 nmp->nm_state |= NFSSTA_SESSPERSIST;
4792 /* Get the fore channel slot count. */
4793 NFSM_DISSECT(tl, uint32_t *, 7 * NFSX_UNSIGNED);
4794 tl++; /* Skip the header pad size. */
4796 /* Make sure nm_wsize is small enough. */
4797 maxval = fxdr_unsigned(uint32_t, *tl++);
4798 while (maxval < nmp->nm_wsize + NFS_MAXXDR) {
4799 if (nmp->nm_wsize > 8096)
4805 /* Make sure nm_rsize is small enough. */
4806 maxval = fxdr_unsigned(uint32_t, *tl++);
4807 while (maxval < nmp->nm_rsize + NFS_MAXXDR) {
4808 if (nmp->nm_rsize > 8096)
4814 sep->nfsess_maxcache = fxdr_unsigned(int, *tl++);
4816 sep->nfsess_foreslots = fxdr_unsigned(uint16_t, *tl++);
4817 NFSCL_DEBUG(4, "fore slots=%d\n", (int)sep->nfsess_foreslots);
4818 irdcnt = fxdr_unsigned(int, *tl);
4820 NFSM_DISSECT(tl, uint32_t *, irdcnt * NFSX_UNSIGNED);
4822 /* and the back channel slot count. */
4823 NFSM_DISSECT(tl, uint32_t *, 7 * NFSX_UNSIGNED);
4825 sep->nfsess_backslots = fxdr_unsigned(uint16_t, *tl);
4826 NFSCL_DEBUG(4, "back slots=%d\n", (int)sep->nfsess_backslots);
4828 error = nd->nd_repstat;
4830 mbuf_freem(nd->nd_mrep);
4835 * Do the NFSv4.1 Destroy Session.
4838 nfsrpc_destroysession(struct nfsmount *nmp, struct nfsclclient *clp,
4839 struct ucred *cred, NFSPROC_T *p)
4842 struct nfsrv_descript nfsd;
4843 struct nfsrv_descript *nd = &nfsd;
4845 struct nfsclsession *tsep;
4847 nfscl_reqstart(nd, NFSPROC_DESTROYSESSION, nmp, NULL, 0, NULL, NULL, 0,
4849 NFSM_BUILD(tl, uint32_t *, NFSX_V4SESSIONID);
4850 tsep = nfsmnt_mdssession(nmp);
4851 bcopy(tsep->nfsess_sessionid, tl, NFSX_V4SESSIONID);
4852 nd->nd_flag |= ND_USEGSSNAME;
4853 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4854 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4857 error = nd->nd_repstat;
4858 mbuf_freem(nd->nd_mrep);
4863 * Do the NFSv4.1 Destroy Client.
4866 nfsrpc_destroyclient(struct nfsmount *nmp, struct nfsclclient *clp,
4867 struct ucred *cred, NFSPROC_T *p)
4870 struct nfsrv_descript nfsd;
4871 struct nfsrv_descript *nd = &nfsd;
4873 struct nfsclsession *tsep;
4875 nfscl_reqstart(nd, NFSPROC_DESTROYCLIENT, nmp, NULL, 0, NULL, NULL, 0,
4877 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_UNSIGNED);
4878 tsep = nfsmnt_mdssession(nmp);
4879 *tl++ = tsep->nfsess_clientid.lval[0];
4880 *tl = tsep->nfsess_clientid.lval[1];
4881 nd->nd_flag |= ND_USEGSSNAME;
4882 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4883 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4886 error = nd->nd_repstat;
4887 mbuf_freem(nd->nd_mrep);
4892 * Do the NFSv4.1 LayoutGet.
4895 nfsrpc_layoutget(struct nfsmount *nmp, uint8_t *fhp, int fhlen, int iomode,
4896 uint64_t offset, uint64_t len, uint64_t minlen, int layouttype,
4897 int layoutlen, nfsv4stateid_t *stateidp, int *retonclosep,
4898 struct nfsclflayouthead *flhp, struct ucred *cred, NFSPROC_T *p,
4901 struct nfsrv_descript nfsd, *nd = &nfsd;
4904 nfscl_reqstart(nd, NFSPROC_LAYOUTGET, nmp, fhp, fhlen, NULL, NULL, 0,
4906 nfsrv_setuplayoutget(nd, iomode, offset, len, minlen, stateidp,
4907 layouttype, layoutlen, 0);
4908 nd->nd_flag |= ND_USEGSSNAME;
4909 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4910 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4911 NFSCL_DEBUG(4, "layget err=%d st=%d\n", error, nd->nd_repstat);
4914 if (nd->nd_repstat == 0)
4915 error = nfsrv_parselayoutget(nd, stateidp, retonclosep, flhp);
4916 if (error == 0 && nd->nd_repstat != 0)
4917 error = nd->nd_repstat;
4918 mbuf_freem(nd->nd_mrep);
4923 * Do the NFSv4.1 Get Device Info.
4926 nfsrpc_getdeviceinfo(struct nfsmount *nmp, uint8_t *deviceid, int layouttype,
4927 uint32_t *notifybitsp, struct nfscldevinfo **ndip, struct ucred *cred,
4930 uint32_t cnt, *tl, vers, minorvers;
4931 struct nfsrv_descript nfsd;
4932 struct nfsrv_descript *nd = &nfsd;
4933 struct sockaddr_in sin, ssin;
4934 struct sockaddr_in6 sin6, ssin6;
4935 struct nfsclds *dsp = NULL, **dspp, **gotdspp;
4936 struct nfscldevinfo *ndi;
4937 int addrcnt = 0, bitcnt, error, gotvers, i, isudp, j, stripecnt;
4938 uint8_t stripeindex;
4939 sa_family_t af, safilled;
4944 nfscl_reqstart(nd, NFSPROC_GETDEVICEINFO, nmp, NULL, 0, NULL, NULL, 0,
4946 NFSM_BUILD(tl, uint32_t *, NFSX_V4DEVICEID + 3 * NFSX_UNSIGNED);
4947 NFSBCOPY(deviceid, tl, NFSX_V4DEVICEID);
4948 tl += (NFSX_V4DEVICEID / NFSX_UNSIGNED);
4949 *tl++ = txdr_unsigned(layouttype);
4950 *tl++ = txdr_unsigned(100000);
4951 if (notifybitsp != NULL && *notifybitsp != 0) {
4952 *tl = txdr_unsigned(1); /* One word of bits. */
4953 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
4954 *tl = txdr_unsigned(*notifybitsp);
4956 *tl = txdr_unsigned(0);
4957 nd->nd_flag |= ND_USEGSSNAME;
4958 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4959 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4962 if (nd->nd_repstat == 0) {
4963 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
4964 if (layouttype != fxdr_unsigned(int, *tl))
4965 printf("EEK! devinfo layout type not same!\n");
4966 if (layouttype == NFSLAYOUT_NFSV4_1_FILES) {
4967 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
4968 stripecnt = fxdr_unsigned(int, *tl);
4969 NFSCL_DEBUG(4, "stripecnt=%d\n", stripecnt);
4970 if (stripecnt < 1 || stripecnt > 4096) {
4971 printf("pNFS File layout devinfo stripecnt %d:"
4972 " out of range\n", stripecnt);
4973 error = NFSERR_BADXDR;
4976 NFSM_DISSECT(tl, uint32_t *, (stripecnt + 1) *
4978 addrcnt = fxdr_unsigned(int, *(tl + stripecnt));
4979 NFSCL_DEBUG(4, "addrcnt=%d\n", addrcnt);
4980 if (addrcnt < 1 || addrcnt > 128) {
4981 printf("NFS devinfo addrcnt %d: out of range\n",
4983 error = NFSERR_BADXDR;
4988 * Now we know how many stripe indices and addresses, so
4989 * we can allocate the structure the correct size.
4991 i = (stripecnt * sizeof(uint8_t)) /
4992 sizeof(struct nfsclds *) + 1;
4993 NFSCL_DEBUG(4, "stripeindices=%d\n", i);
4994 ndi = malloc(sizeof(*ndi) + (addrcnt + i) *
4995 sizeof(struct nfsclds *), M_NFSDEVINFO, M_WAITOK |
4997 NFSBCOPY(deviceid, ndi->nfsdi_deviceid,
4999 ndi->nfsdi_refcnt = 0;
5000 ndi->nfsdi_flags = NFSDI_FILELAYOUT;
5001 ndi->nfsdi_stripecnt = stripecnt;
5002 ndi->nfsdi_addrcnt = addrcnt;
5003 /* Fill in the stripe indices. */
5004 for (i = 0; i < stripecnt; i++) {
5005 stripeindex = fxdr_unsigned(uint8_t, *tl++);
5006 NFSCL_DEBUG(4, "stripeind=%d\n", stripeindex);
5007 if (stripeindex >= addrcnt) {
5008 printf("pNFS File Layout devinfo"
5009 " stripeindex %d: too big\n",
5011 error = NFSERR_BADXDR;
5014 nfsfldi_setstripeindex(ndi, i, stripeindex);
5016 } else if (layouttype == NFSLAYOUT_FLEXFILE) {
5017 /* For Flex File, we only get one address list. */
5018 ndi = malloc(sizeof(*ndi) + sizeof(struct nfsclds *),
5019 M_NFSDEVINFO, M_WAITOK | M_ZERO);
5020 NFSBCOPY(deviceid, ndi->nfsdi_deviceid,
5022 ndi->nfsdi_refcnt = 0;
5023 ndi->nfsdi_flags = NFSDI_FLEXFILE;
5024 addrcnt = ndi->nfsdi_addrcnt = 1;
5027 /* Now, dissect the server address(es). */
5028 safilled = AF_UNSPEC;
5029 for (i = 0; i < addrcnt; i++) {
5030 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
5031 cnt = fxdr_unsigned(uint32_t, *tl);
5033 printf("NFS devinfo 0 len addrlist\n");
5034 error = NFSERR_BADXDR;
5037 dspp = nfsfldi_addr(ndi, i);
5038 safilled = AF_UNSPEC;
5039 for (j = 0; j < cnt; j++) {
5040 error = nfsv4_getipaddr(nd, &sin, &sin6, &af,
5042 if (error != 0 && error != EPERM) {
5043 error = NFSERR_BADXDR;
5046 if (error == 0 && isudp == 0) {
5049 * - Same address family.
5050 * Save the address and dspp, so that
5051 * the connection can be done after
5052 * parsing is complete.
5054 if (safilled == AF_UNSPEC ||
5055 (af == nmp->nm_nam->sa_family &&
5056 safilled != nmp->nm_nam->sa_family)
5069 gotvers = NFS_VER4; /* Always NFSv4 for File Layout. */
5070 /* For Flex File, we will take one of the versions to use. */
5071 if (layouttype == NFSLAYOUT_FLEXFILE) {
5072 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
5073 j = fxdr_unsigned(int, *tl);
5074 if (j < 1 || j > NFSDEV_MAXVERS) {
5075 printf("pNFS: too many versions\n");
5076 error = NFSERR_BADXDR;
5080 for (i = 0; i < j; i++) {
5081 NFSM_DISSECT(tl, uint32_t *, 5 * NFSX_UNSIGNED);
5082 vers = fxdr_unsigned(uint32_t, *tl++);
5083 minorvers = fxdr_unsigned(uint32_t, *tl++);
5084 if ((vers == NFS_VER4 && minorvers ==
5085 NFSV41_MINORVERSION) || (vers == NFS_VER3 &&
5088 /* We'll take this one. */
5089 ndi->nfsdi_versindex = i;
5090 ndi->nfsdi_vers = vers;
5091 ndi->nfsdi_minorvers = minorvers;
5092 ndi->nfsdi_rsize = fxdr_unsigned(
5094 ndi->nfsdi_wsize = fxdr_unsigned(
5096 if (*tl == newnfs_true)
5101 ~NFSDI_TIGHTCOUPLED;
5105 printf("pNFS: no NFSv3 or NFSv4.1\n");
5106 error = NFSERR_BADXDR;
5111 /* And the notify bits. */
5112 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
5113 bitcnt = fxdr_unsigned(int, *tl);
5115 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
5116 if (notifybitsp != NULL)
5118 fxdr_unsigned(uint32_t, *tl);
5120 if (safilled != AF_UNSPEC) {
5121 KASSERT(ndi != NULL, ("ndi is NULL"));
5127 * Now we can do a TCP connection for the correct
5128 * NFS version and IP address.
5130 error = nfsrpc_fillsa(nmp, &ssin, &ssin6, safilled,
5134 KASSERT(gotdspp != NULL, ("gotdspp is NULL"));
5138 if (nd->nd_repstat != 0 && error == 0)
5139 error = nd->nd_repstat;
5141 if (error != 0 && ndi != NULL)
5142 nfscl_freedevinfo(ndi);
5143 mbuf_freem(nd->nd_mrep);
5148 * Do the NFSv4.1 LayoutCommit.
5151 nfsrpc_layoutcommit(struct nfsmount *nmp, uint8_t *fh, int fhlen, int reclaim,
5152 uint64_t off, uint64_t len, uint64_t lastbyte, nfsv4stateid_t *stateidp,
5153 int layouttype, struct ucred *cred, NFSPROC_T *p, void *stuff)
5156 struct nfsrv_descript nfsd, *nd = &nfsd;
5159 nfscl_reqstart(nd, NFSPROC_LAYOUTCOMMIT, nmp, fh, fhlen, NULL, NULL,
5161 NFSM_BUILD(tl, uint32_t *, 5 * NFSX_UNSIGNED + 3 * NFSX_HYPER +
5163 txdr_hyper(off, tl);
5165 txdr_hyper(len, tl);
5168 *tl++ = newnfs_true;
5170 *tl++ = newnfs_false;
5171 *tl++ = txdr_unsigned(stateidp->seqid);
5172 *tl++ = stateidp->other[0];
5173 *tl++ = stateidp->other[1];
5174 *tl++ = stateidp->other[2];
5175 *tl++ = newnfs_true;
5178 else if (lastbyte >= (off + len))
5179 lastbyte = off + len - 1;
5180 txdr_hyper(lastbyte, tl);
5182 *tl++ = newnfs_false;
5183 *tl++ = txdr_unsigned(layouttype);
5184 /* All supported layouts are 0 length. */
5185 *tl = txdr_unsigned(0);
5186 nd->nd_flag |= ND_USEGSSNAME;
5187 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
5188 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
5191 error = nd->nd_repstat;
5192 mbuf_freem(nd->nd_mrep);
5197 * Do the NFSv4.1 LayoutReturn.
5200 nfsrpc_layoutreturn(struct nfsmount *nmp, uint8_t *fh, int fhlen, int reclaim,
5201 int layouttype, uint32_t iomode, int layoutreturn, uint64_t offset,
5202 uint64_t len, nfsv4stateid_t *stateidp, struct ucred *cred, NFSPROC_T *p,
5206 struct nfsrv_descript nfsd, *nd = &nfsd;
5209 nfscl_reqstart(nd, NFSPROC_LAYOUTRETURN, nmp, fh, fhlen, NULL, NULL,
5211 NFSM_BUILD(tl, uint32_t *, 4 * NFSX_UNSIGNED);
5213 *tl++ = newnfs_true;
5215 *tl++ = newnfs_false;
5216 *tl++ = txdr_unsigned(layouttype);
5217 *tl++ = txdr_unsigned(iomode);
5218 *tl = txdr_unsigned(layoutreturn);
5219 if (layoutreturn == NFSLAYOUTRETURN_FILE) {
5220 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_HYPER + NFSX_STATEID +
5222 txdr_hyper(offset, tl);
5224 txdr_hyper(len, tl);
5226 NFSCL_DEBUG(4, "layoutret stseq=%d\n", (int)stateidp->seqid);
5227 *tl++ = txdr_unsigned(stateidp->seqid);
5228 *tl++ = stateidp->other[0];
5229 *tl++ = stateidp->other[1];
5230 *tl++ = stateidp->other[2];
5231 if (layouttype == NFSLAYOUT_NFSV4_1_FILES)
5232 *tl = txdr_unsigned(0);
5233 else if (layouttype == NFSLAYOUT_FLEXFILE) {
5234 *tl = txdr_unsigned(2 * NFSX_UNSIGNED);
5235 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_UNSIGNED);
5236 /* No ioerrs or stats yet. */
5241 nd->nd_flag |= ND_USEGSSNAME;
5242 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
5243 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
5246 if (nd->nd_repstat == 0) {
5247 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
5249 NFSM_DISSECT(tl, uint32_t *, NFSX_STATEID);
5250 stateidp->seqid = fxdr_unsigned(uint32_t, *tl++);
5251 stateidp->other[0] = *tl++;
5252 stateidp->other[1] = *tl++;
5253 stateidp->other[2] = *tl;
5256 error = nd->nd_repstat;
5258 mbuf_freem(nd->nd_mrep);
5263 * Acquire a layout and devinfo, if possible. The caller must have acquired
5264 * a reference count on the nfsclclient structure before calling this.
5265 * Return the layout in lypp with a reference count on it, if successful.
5268 nfsrpc_getlayout(struct nfsmount *nmp, vnode_t vp, struct nfsfh *nfhp,
5269 int iomode, uint32_t *notifybitsp, nfsv4stateid_t *stateidp, uint64_t off,
5270 struct nfscllayout **lypp, struct ucred *cred, NFSPROC_T *p)
5272 struct nfscllayout *lyp;
5273 struct nfsclflayout *flp;
5274 struct nfsclflayouthead flh;
5275 int error = 0, islocked, layoutlen, layouttype, recalled, retonclose;
5276 nfsv4stateid_t stateid;
5277 struct nfsclsession *tsep;
5280 if (NFSHASFLEXFILE(nmp))
5281 layouttype = NFSLAYOUT_FLEXFILE;
5283 layouttype = NFSLAYOUT_NFSV4_1_FILES;
5285 * If lyp is returned non-NULL, there will be a refcnt (shared lock)
5286 * on it, iff flp != NULL or a lock (exclusive lock) on it iff
5289 lyp = nfscl_getlayout(nmp->nm_clp, nfhp->nfh_fh, nfhp->nfh_len,
5290 off, &flp, &recalled);
5292 if (lyp == NULL || flp == NULL) {
5296 tsep = nfsmnt_mdssession(nmp);
5297 layoutlen = tsep->nfsess_maxcache -
5298 (NFSX_STATEID + 3 * NFSX_UNSIGNED);
5301 stateid.other[0] = stateidp->other[0];
5302 stateid.other[1] = stateidp->other[1];
5303 stateid.other[2] = stateidp->other[2];
5304 error = nfsrpc_layoutget(nmp, nfhp->nfh_fh,
5305 nfhp->nfh_len, iomode, (uint64_t)0, UINT64_MAX,
5306 (uint64_t)0, layouttype, layoutlen, &stateid,
5307 &retonclose, &flh, cred, p, NULL);
5310 stateid.seqid = lyp->nfsly_stateid.seqid;
5311 stateid.other[0] = lyp->nfsly_stateid.other[0];
5312 stateid.other[1] = lyp->nfsly_stateid.other[1];
5313 stateid.other[2] = lyp->nfsly_stateid.other[2];
5314 error = nfsrpc_layoutget(nmp, nfhp->nfh_fh,
5315 nfhp->nfh_len, iomode, off, UINT64_MAX,
5316 (uint64_t)0, layouttype, layoutlen, &stateid,
5317 &retonclose, &flh, cred, p, NULL);
5319 error = nfsrpc_layoutgetres(nmp, vp, nfhp->nfh_fh,
5320 nfhp->nfh_len, &stateid, retonclose, notifybitsp, &lyp,
5321 &flh, layouttype, error, NULL, cred, p);
5324 else if (islocked != 0)
5325 nfscl_rellayout(lyp, 1);
5332 * Do a TCP connection plus exchange id and create session.
5333 * If successful, a "struct nfsclds" is linked into the list for the
5334 * mount point and a pointer to it is returned.
5337 nfsrpc_fillsa(struct nfsmount *nmp, struct sockaddr_in *sin,
5338 struct sockaddr_in6 *sin6, sa_family_t af, int vers, struct nfsclds **dspp,
5341 struct sockaddr_in *msad, *sad;
5342 struct sockaddr_in6 *msad6, *sad6;
5343 struct nfsclclient *clp;
5344 struct nfssockreq *nrp;
5345 struct nfsclds *dsp, *tdsp;
5347 enum nfsclds_state retv;
5348 uint32_t sequenceid;
5350 KASSERT(nmp->nm_sockreq.nr_cred != NULL,
5351 ("nfsrpc_fillsa: NULL nr_cred"));
5357 if (af == AF_INET) {
5360 * Check to see if we already have a session for this
5361 * address that is usable for a DS.
5362 * Note that the MDS's address is in a different place
5363 * than the sessions already acquired for DS's.
5365 msad = (struct sockaddr_in *)nmp->nm_sockreq.nr_nam;
5366 tdsp = TAILQ_FIRST(&nmp->nm_sess);
5367 while (tdsp != NULL) {
5368 if (msad != NULL && msad->sin_family == AF_INET &&
5369 sin->sin_addr.s_addr == msad->sin_addr.s_addr &&
5370 sin->sin_port == msad->sin_port &&
5371 (tdsp->nfsclds_flags & NFSCLDS_DS) != 0 &&
5372 tdsp->nfsclds_sess.nfsess_defunct == 0) {
5375 NFSCL_DEBUG(4, "fnd same addr\n");
5378 tdsp = TAILQ_NEXT(tdsp, nfsclds_list);
5379 if (tdsp != NULL && tdsp->nfsclds_sockp != NULL)
5380 msad = (struct sockaddr_in *)
5381 tdsp->nfsclds_sockp->nr_nam;
5387 /* No IP address match, so look for new/trunked one. */
5388 sad = malloc(sizeof(*sad), M_SONAME, M_WAITOK | M_ZERO);
5389 sad->sin_len = sizeof(*sad);
5390 sad->sin_family = AF_INET;
5391 sad->sin_port = sin->sin_port;
5392 sad->sin_addr.s_addr = sin->sin_addr.s_addr;
5393 nrp = malloc(sizeof(*nrp), M_NFSSOCKREQ, M_WAITOK | M_ZERO);
5394 nrp->nr_nam = (struct sockaddr *)sad;
5395 } else if (af == AF_INET6) {
5398 * Check to see if we already have a session for this
5399 * address that is usable for a DS.
5400 * Note that the MDS's address is in a different place
5401 * than the sessions already acquired for DS's.
5403 msad6 = (struct sockaddr_in6 *)nmp->nm_sockreq.nr_nam;
5404 tdsp = TAILQ_FIRST(&nmp->nm_sess);
5405 while (tdsp != NULL) {
5406 if (msad6 != NULL && msad6->sin6_family == AF_INET6 &&
5407 IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr,
5408 &msad6->sin6_addr) &&
5409 sin6->sin6_port == msad6->sin6_port &&
5410 (tdsp->nfsclds_flags & NFSCLDS_DS) != 0 &&
5411 tdsp->nfsclds_sess.nfsess_defunct == 0) {
5416 tdsp = TAILQ_NEXT(tdsp, nfsclds_list);
5417 if (tdsp != NULL && tdsp->nfsclds_sockp != NULL)
5418 msad6 = (struct sockaddr_in6 *)
5419 tdsp->nfsclds_sockp->nr_nam;
5425 /* No IP address match, so look for new/trunked one. */
5426 sad6 = malloc(sizeof(*sad6), M_SONAME, M_WAITOK | M_ZERO);
5427 sad6->sin6_len = sizeof(*sad6);
5428 sad6->sin6_family = AF_INET6;
5429 sad6->sin6_port = sin6->sin6_port;
5430 NFSBCOPY(&sin6->sin6_addr, &sad6->sin6_addr,
5431 sizeof(struct in6_addr));
5432 nrp = malloc(sizeof(*nrp), M_NFSSOCKREQ, M_WAITOK | M_ZERO);
5433 nrp->nr_nam = (struct sockaddr *)sad6;
5437 nrp->nr_sotype = SOCK_STREAM;
5438 mtx_init(&nrp->nr_mtx, "nfssock", NULL, MTX_DEF);
5439 nrp->nr_prog = NFS_PROG;
5440 nrp->nr_vers = vers;
5443 * Use the credentials that were used for the mount, which are
5444 * in nmp->nm_sockreq.nr_cred for newnfs_connect() etc.
5445 * Ref. counting the credentials with crhold() is probably not
5446 * necessary, since nm_sockreq.nr_cred won't be crfree()'d until
5447 * unmount, but I did it anyhow.
5449 nrp->nr_cred = crhold(nmp->nm_sockreq.nr_cred);
5450 error = newnfs_connect(nmp, nrp, NULL, p, 0);
5451 NFSCL_DEBUG(3, "DS connect=%d\n", error);
5454 /* Now, do the exchangeid and create session. */
5456 if (vers == NFS_VER4) {
5457 error = nfsrpc_exchangeid(nmp, clp, nrp,
5458 NFSV4EXCH_USEPNFSDS, &dsp, nrp->nr_cred, p);
5459 NFSCL_DEBUG(3, "DS exchangeid=%d\n", error);
5461 newnfs_disconnect(nrp);
5463 dsp = malloc(sizeof(struct nfsclds), M_NFSCLDS,
5465 dsp->nfsclds_flags |= NFSCLDS_DS;
5466 dsp->nfsclds_expire = INT32_MAX; /* No renews needed. */
5467 mtx_init(&dsp->nfsclds_mtx, "nfsds", NULL, MTX_DEF);
5468 mtx_init(&dsp->nfsclds_sess.nfsess_mtx, "nfssession",
5473 dsp->nfsclds_sockp = nrp;
5474 if (vers == NFS_VER4) {
5476 retv = nfscl_getsameserver(nmp, dsp, &tdsp);
5477 NFSCL_DEBUG(3, "getsame ret=%d\n", retv);
5478 if (retv == NFSDSP_USETHISSESSION) {
5481 * If there is already a session for this
5484 (void)newnfs_disconnect(nrp);
5485 nfscl_freenfsclds(dsp);
5489 if (retv == NFSDSP_SEQTHISSESSION)
5491 tdsp->nfsclds_sess.nfsess_sequenceid;
5494 dsp->nfsclds_sess.nfsess_sequenceid;
5496 error = nfsrpc_createsession(nmp, &dsp->nfsclds_sess,
5497 nrp, sequenceid, 0, nrp->nr_cred, p);
5498 NFSCL_DEBUG(3, "DS createsess=%d\n", error);
5501 NFSFREECRED(nrp->nr_cred);
5502 NFSFREEMUTEX(&nrp->nr_mtx);
5503 free(nrp->nr_nam, M_SONAME);
5504 free(nrp, M_NFSSOCKREQ);
5507 NFSCL_DEBUG(3, "add DS session\n");
5509 * Put it at the end of the list. That way the list
5510 * is ordered by when the entry was added. This matters
5511 * since the one done first is the one that should be
5512 * used for sequencid'ing any subsequent create sessions.
5515 TAILQ_INSERT_TAIL(&nmp->nm_sess, dsp, nfsclds_list);
5518 } else if (dsp != NULL) {
5519 newnfs_disconnect(nrp);
5520 nfscl_freenfsclds(dsp);
5526 * Do the NFSv4.1 Reclaim Complete.
5529 nfsrpc_reclaimcomplete(struct nfsmount *nmp, struct ucred *cred, NFSPROC_T *p)
5532 struct nfsrv_descript nfsd;
5533 struct nfsrv_descript *nd = &nfsd;
5536 nfscl_reqstart(nd, NFSPROC_RECLAIMCOMPL, nmp, NULL, 0, NULL, NULL, 0,
5538 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
5540 nd->nd_flag |= ND_USEGSSNAME;
5541 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
5542 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
5545 error = nd->nd_repstat;
5546 mbuf_freem(nd->nd_mrep);
5551 * Initialize the slot tables for a session.
5554 nfscl_initsessionslots(struct nfsclsession *sep)
5558 for (i = 0; i < NFSV4_CBSLOTS; i++) {
5559 if (sep->nfsess_cbslots[i].nfssl_reply != NULL)
5560 m_freem(sep->nfsess_cbslots[i].nfssl_reply);
5561 NFSBZERO(&sep->nfsess_cbslots[i], sizeof(struct nfsslot));
5563 for (i = 0; i < 64; i++)
5564 sep->nfsess_slotseq[i] = 0;
5565 sep->nfsess_slots = 0;
5569 * Called to try and do an I/O operation via an NFSv4.1 Data Server (DS).
5572 nfscl_doiods(vnode_t vp, struct uio *uiop, int *iomode, int *must_commit,
5573 uint32_t rwaccess, int docommit, struct ucred *cred, NFSPROC_T *p)
5575 struct nfsnode *np = VTONFS(vp);
5576 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
5577 struct nfscllayout *layp;
5578 struct nfscldevinfo *dip;
5579 struct nfsclflayout *rflp;
5581 struct nfsclwritedsdorpc *drpc, *tdrpc;
5582 nfsv4stateid_t stateid;
5583 struct ucred *newcred;
5584 uint64_t lastbyte, len, off, oresid, xfer;
5585 int eof, error, firstmirror, i, iolaymode, mirrorcnt, recalled, timo;
5593 if (!NFSHASPNFS(nmp) || nfscl_enablecallb == 0 || nfs_numnfscbd == 0 ||
5594 (np->n_flag & NNOLAYOUT) != 0)
5596 /* Now, get a reference cnt on the clientid for this mount. */
5597 if (nfscl_getref(nmp) == 0)
5600 /* Find an appropriate stateid. */
5601 newcred = NFSNEWCRED(cred);
5602 error = nfscl_getstateid(vp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len,
5603 rwaccess, 1, newcred, p, &stateid, &lckp);
5605 NFSFREECRED(newcred);
5609 /* Search for a layout for this file. */
5610 off = uiop->uio_offset;
5611 layp = nfscl_getlayout(nmp->nm_clp, np->n_fhp->nfh_fh,
5612 np->n_fhp->nfh_len, off, &rflp, &recalled);
5613 if (layp == NULL || rflp == NULL) {
5614 if (recalled != 0) {
5615 NFSFREECRED(newcred);
5620 nfscl_rellayout(layp, (rflp == NULL) ? 1 : 0);
5623 /* Try and get a Layout, if it is supported. */
5624 if (rwaccess == NFSV4OPEN_ACCESSWRITE ||
5625 (np->n_flag & NWRITEOPENED) != 0)
5626 iolaymode = NFSLAYOUTIOMODE_RW;
5628 iolaymode = NFSLAYOUTIOMODE_READ;
5629 error = nfsrpc_getlayout(nmp, vp, np->n_fhp, iolaymode,
5630 NULL, &stateid, off, &layp, newcred, p);
5633 np->n_flag |= NNOLAYOUT;
5636 nfscl_lockderef(lckp);
5637 NFSFREECRED(newcred);
5639 nfscl_rellayout(layp, 0);
5646 * Loop around finding a layout that works for the first part of
5647 * this I/O operation, and then call the function that actually
5651 len = (uint64_t)uiop->uio_resid;
5652 while (len > 0 && error == 0 && eof == 0) {
5653 off = uiop->uio_offset;
5654 error = nfscl_findlayoutforio(layp, off, rwaccess, &rflp);
5656 oresid = xfer = (uint64_t)uiop->uio_resid;
5657 if (xfer > (rflp->nfsfl_end - rflp->nfsfl_off))
5658 xfer = rflp->nfsfl_end - rflp->nfsfl_off;
5660 * For Flex File layout with mirrored DSs, select one
5661 * of them at random for reads. For writes and commits,
5668 if ((layp->nfsly_flags & NFSLY_FLEXFILE) != 0 &&
5669 (mirrorcnt = rflp->nfsfl_mirrorcnt) > 1) {
5670 if (rwaccess == NFSV4OPEN_ACCESSREAD) {
5671 firstmirror = arc4random() % mirrorcnt;
5672 mirrorcnt = firstmirror + 1;
5674 if (docommit == 0) {
5676 * Save values, so uiop can be
5677 * rolled back upon a write
5680 offs = uiop->uio_offset;
5681 resid = uiop->uio_resid;
5683 uiop->uio_iov->iov_base;
5684 iovlen = uiop->uio_iov->iov_len;
5685 m = nfsm_uiombuflist(uiop, len,
5688 tdrpc = drpc = malloc(sizeof(*drpc) *
5689 (mirrorcnt - 1), M_TEMP, M_WAITOK |
5693 for (i = firstmirror; i < mirrorcnt && error == 0; i++){
5694 if ((layp->nfsly_flags & NFSLY_FLEXFILE) != 0)
5695 dev = rflp->nfsfl_ffm[i].dev;
5697 dev = rflp->nfsfl_dev;
5698 dip = nfscl_getdevinfo(nmp->nm_clp, dev,
5701 if ((rflp->nfsfl_flags & NFSFL_FLEXFILE)
5703 error = nfscl_dofflayoutio(vp,
5704 uiop, iomode, must_commit,
5705 &eof, &stateid, rwaccess,
5706 dip, layp, rflp, off, xfer,
5707 i, docommit, m, tdrpc,
5710 error = nfscl_doflayoutio(vp,
5711 uiop, iomode, must_commit,
5712 &eof, &stateid, rwaccess,
5713 dip, layp, rflp, off, xfer,
5714 docommit, newcred, p);
5715 nfscl_reldevinfo(dip);
5723 timo = hz / 50; /* Wait for 20msec. */
5726 for (i = firstmirror; i < mirrorcnt - 1 &&
5727 tdrpc != NULL; i++, tdrpc++) {
5729 * For the unused drpc entries, both inprog and
5730 * err == 0, so this loop won't break.
5732 while (tdrpc->inprog != 0 && tdrpc->done == 0)
5733 tsleep(&tdrpc->tsk, PVFS, "clrpcio",
5735 if (error == 0 && tdrpc->err != 0)
5740 if (mirrorcnt > 1 && rwaccess ==
5741 NFSV4OPEN_ACCESSWRITE && docommit == 0) {
5743 layp->nfsly_flags |= NFSLY_WRITTEN;
5746 lastbyte = off + xfer - 1;
5748 if (lastbyte > layp->nfsly_lastbyte)
5749 layp->nfsly_lastbyte = lastbyte;
5751 } else if (error == NFSERR_OPENMODE &&
5752 rwaccess == NFSV4OPEN_ACCESSREAD) {
5754 nmp->nm_state |= NFSSTA_OPENMODE;
5759 len -= (oresid - (uint64_t)uiop->uio_resid);
5760 else if (mirrorcnt > 1 && rwaccess ==
5761 NFSV4OPEN_ACCESSWRITE && docommit == 0) {
5763 * In case the rpc gets retried, roll the
5764 * uio fields changed by nfsm_uiombuflist()
5767 uiop->uio_offset = offs;
5768 uiop->uio_resid = resid;
5769 uiop->uio_iov->iov_base = iovbase;
5770 uiop->uio_iov->iov_len = iovlen;
5775 nfscl_lockderef(lckp);
5776 NFSFREECRED(newcred);
5777 nfscl_rellayout(layp, 0);
5783 * Make a copy of the mbuf chain and add an mbuf for null padding, as required.
5785 static struct mbuf *
5786 nfsm_copym(struct mbuf *m, int off, int xfer)
5788 struct mbuf *m2, *m3, *m4;
5792 m2 = m_copym(m, off, xfer, M_WAITOK);
5793 rem = NFSM_RNDUP(xfer) - xfer;
5796 * The zero padding to a multiple of 4 bytes is required by
5797 * the XDR. So that the mbufs copied by reference aren't
5798 * modified, add an mbuf with the zero'd bytes to the list.
5799 * rem will be a maximum of 3, so one zero'd uint32_t is
5803 while (m3->m_next != NULL)
5806 tl = NFSMTOD(m4, uint32_t *);
5808 mbuf_setlen(m4, rem);
5809 mbuf_setnext(m3, m4);
5815 * Find a file layout that will handle the first bytes of the requested
5816 * range and return the information from it needed to to the I/O operation.
5819 nfscl_findlayoutforio(struct nfscllayout *lyp, uint64_t off, uint32_t rwaccess,
5820 struct nfsclflayout **retflpp)
5822 struct nfsclflayout *flp, *nflp, *rflp;
5827 /* For reading, do the Read list first and then the Write list. */
5829 if (rw == NFSV4OPEN_ACCESSREAD)
5830 flp = LIST_FIRST(&lyp->nfsly_flayread);
5832 flp = LIST_FIRST(&lyp->nfsly_flayrw);
5833 while (flp != NULL) {
5834 nflp = LIST_NEXT(flp, nfsfl_list);
5835 if (flp->nfsfl_off > off)
5837 if (flp->nfsfl_end > off &&
5838 (rflp == NULL || rflp->nfsfl_end < flp->nfsfl_end))
5842 if (rw == NFSV4OPEN_ACCESSREAD)
5843 rw = NFSV4OPEN_ACCESSWRITE;
5848 /* This one covers the most bytes starting at off. */
5856 * Do I/O using an NFSv4.1 file layout.
5859 nfscl_doflayoutio(vnode_t vp, struct uio *uiop, int *iomode, int *must_commit,
5860 int *eofp, nfsv4stateid_t *stateidp, int rwflag, struct nfscldevinfo *dp,
5861 struct nfscllayout *lyp, struct nfsclflayout *flp, uint64_t off,
5862 uint64_t len, int docommit, struct ucred *cred, NFSPROC_T *p)
5864 uint64_t io_off, rel_off, stripe_unit_size, transfer, xfer;
5865 int commit_thru_mds, error, stripe_index, stripe_pos;
5868 struct nfsclds **dspp;
5871 rel_off = off - flp->nfsfl_patoff;
5872 stripe_unit_size = (flp->nfsfl_util >> 6) & 0x3ffffff;
5873 stripe_pos = (rel_off / stripe_unit_size + flp->nfsfl_stripe1) %
5874 dp->nfsdi_stripecnt;
5875 transfer = stripe_unit_size - (rel_off % stripe_unit_size);
5878 /* Loop around, doing I/O for each stripe unit. */
5879 while (len > 0 && error == 0) {
5880 stripe_index = nfsfldi_stripeindex(dp, stripe_pos);
5881 dspp = nfsfldi_addr(dp, stripe_index);
5882 if (len > transfer && docommit == 0)
5886 if ((flp->nfsfl_util & NFSFLAYUTIL_DENSE) != 0) {
5888 if (stripe_pos >= flp->nfsfl_fhcnt)
5890 fhp = flp->nfsfl_fh[stripe_pos];
5891 io_off = (rel_off / (stripe_unit_size *
5892 dp->nfsdi_stripecnt)) * stripe_unit_size +
5893 rel_off % stripe_unit_size;
5895 /* Sparse layout. */
5896 if (flp->nfsfl_fhcnt > 1) {
5897 if (stripe_index >= flp->nfsfl_fhcnt)
5899 fhp = flp->nfsfl_fh[stripe_index];
5900 } else if (flp->nfsfl_fhcnt == 1)
5901 fhp = flp->nfsfl_fh[0];
5906 if ((flp->nfsfl_util & NFSFLAYUTIL_COMMIT_THRU_MDS) != 0) {
5907 commit_thru_mds = 1;
5911 commit_thru_mds = 0;
5912 mtx_lock(&np->n_mtx);
5913 np->n_flag |= NDSCOMMIT;
5914 mtx_unlock(&np->n_mtx);
5916 if (docommit != 0) {
5918 error = nfsrpc_commitds(vp, io_off, xfer,
5919 *dspp, fhp, 0, 0, cred, p);
5922 * Set both eof and uio_resid = 0 to end any
5926 uiop->uio_resid = 0;
5928 mtx_lock(&np->n_mtx);
5929 np->n_flag &= ~NDSCOMMIT;
5930 mtx_unlock(&np->n_mtx);
5932 } else if (rwflag == NFSV4OPEN_ACCESSREAD)
5933 error = nfsrpc_readds(vp, uiop, stateidp, eofp, *dspp,
5934 io_off, xfer, fhp, 0, 0, 0, cred, p);
5936 error = nfsrpc_writeds(vp, uiop, iomode, must_commit,
5937 stateidp, *dspp, io_off, xfer, fhp, commit_thru_mds,
5941 lyp->nfsly_flags |= NFSLY_WRITTEN;
5946 transfer = stripe_unit_size;
5947 stripe_pos = (stripe_pos + 1) % dp->nfsdi_stripecnt;
5956 * Do I/O using an NFSv4.1 flex file layout.
5959 nfscl_dofflayoutio(vnode_t vp, struct uio *uiop, int *iomode, int *must_commit,
5960 int *eofp, nfsv4stateid_t *stateidp, int rwflag, struct nfscldevinfo *dp,
5961 struct nfscllayout *lyp, struct nfsclflayout *flp, uint64_t off,
5962 uint64_t len, int mirror, int docommit, struct mbuf *mp,
5963 struct nfsclwritedsdorpc *drpc, struct ucred *cred, NFSPROC_T *p)
5965 uint64_t transfer, xfer;
5969 struct nfsclds **dspp;
5970 struct ucred *tcred;
5976 NFSCL_DEBUG(4, "nfscl_dofflayoutio: off=%ju len=%ju\n", (uintmax_t)off,
5978 /* Loop around, doing I/O for each stripe unit. */
5979 while (len > 0 && error == 0) {
5980 dspp = nfsfldi_addr(dp, 0);
5981 fhp = flp->nfsfl_ffm[mirror].fh[dp->nfsdi_versindex];
5982 stateidp = &flp->nfsfl_ffm[mirror].st;
5983 NFSCL_DEBUG(4, "mirror=%d vind=%d fhlen=%d st.seqid=0x%x\n",
5984 mirror, dp->nfsdi_versindex, fhp->nfh_len, stateidp->seqid);
5985 if ((dp->nfsdi_flags & NFSDI_TIGHTCOUPLED) == 0) {
5986 tcred = NFSNEWCRED(cred);
5987 tcred->cr_uid = flp->nfsfl_ffm[mirror].user;
5988 tcred->cr_groups[0] = flp->nfsfl_ffm[mirror].group;
5989 tcred->cr_ngroups = 1;
5992 if (rwflag == NFSV4OPEN_ACCESSREAD)
5993 transfer = dp->nfsdi_rsize;
5995 transfer = dp->nfsdi_wsize;
5996 mtx_lock(&np->n_mtx);
5997 np->n_flag |= NDSCOMMIT;
5998 mtx_unlock(&np->n_mtx);
5999 if (len > transfer && docommit == 0)
6003 if (docommit != 0) {
6006 * Do last mirrored DS commit with this thread.
6008 if (mirror < flp->nfsfl_mirrorcnt - 1)
6009 error = nfsio_commitds(vp, off, xfer,
6010 *dspp, fhp, dp->nfsdi_vers,
6011 dp->nfsdi_minorvers, drpc, tcred,
6014 error = nfsrpc_commitds(vp, off, xfer,
6015 *dspp, fhp, dp->nfsdi_vers,
6016 dp->nfsdi_minorvers, tcred, p);
6018 NFSCL_DEBUG(4, "aft nfsio_commitds=%d\n", error);
6021 * Set both eof and uio_resid = 0 to end any
6025 uiop->uio_resid = 0;
6027 mtx_lock(&np->n_mtx);
6028 np->n_flag &= ~NDSCOMMIT;
6029 mtx_unlock(&np->n_mtx);
6031 } else if (rwflag == NFSV4OPEN_ACCESSREAD)
6032 error = nfsrpc_readds(vp, uiop, stateidp, eofp, *dspp,
6033 off, xfer, fhp, 1, dp->nfsdi_vers,
6034 dp->nfsdi_minorvers, tcred, p);
6036 if (flp->nfsfl_mirrorcnt == 1) {
6037 error = nfsrpc_writeds(vp, uiop, iomode,
6038 must_commit, stateidp, *dspp, off, xfer,
6039 fhp, 0, 1, dp->nfsdi_vers,
6040 dp->nfsdi_minorvers, tcred, p);
6043 lyp->nfsly_flags |= NFSLY_WRITTEN;
6047 m = nfsm_copym(mp, rel_off, xfer);
6048 NFSCL_DEBUG(4, "mcopy reloff=%d xfer=%jd\n",
6049 rel_off, (uintmax_t)xfer);
6051 * Do last write to a mirrored DS with this
6054 if (mirror < flp->nfsfl_mirrorcnt - 1)
6055 error = nfsio_writedsmir(vp, iomode,
6056 must_commit, stateidp, *dspp, off,
6057 xfer, fhp, m, dp->nfsdi_vers,
6058 dp->nfsdi_minorvers, drpc, tcred,
6061 error = nfsrpc_writedsmir(vp, iomode,
6062 must_commit, stateidp, *dspp, off,
6063 xfer, fhp, m, dp->nfsdi_vers,
6064 dp->nfsdi_minorvers, tcred, p);
6065 NFSCL_DEBUG(4, "nfsio_writedsmir=%d\n", error);
6068 NFSCL_DEBUG(4, "aft read/writeds=%d\n", error);
6074 if ((dp->nfsdi_flags & NFSDI_TIGHTCOUPLED) == 0)
6077 NFSCL_DEBUG(4, "eo nfscl_dofflayoutio=%d\n", error);
6082 * The actual read RPC done to a DS.
6085 nfsrpc_readds(vnode_t vp, struct uio *uiop, nfsv4stateid_t *stateidp, int *eofp,
6086 struct nfsclds *dsp, uint64_t io_off, int len, struct nfsfh *fhp, int flex,
6087 int vers, int minorvers, struct ucred *cred, NFSPROC_T *p)
6090 int attrflag, error, retlen;
6091 struct nfsrv_descript nfsd;
6092 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
6093 struct nfsrv_descript *nd = &nfsd;
6094 struct nfssockreq *nrp;
6098 if (vers == 0 || vers == NFS_VER4) {
6099 nfscl_reqstart(nd, NFSPROC_READDS, nmp, fhp->nfh_fh,
6100 fhp->nfh_len, NULL, &dsp->nfsclds_sess, vers, minorvers);
6102 NFSCL_DEBUG(4, "nfsrpc_readds: vers4 minvers=%d\n", minorvers);
6104 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID);
6106 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSEQIDZERO);
6108 nfscl_reqstart(nd, NFSPROC_READ, nmp, fhp->nfh_fh,
6109 fhp->nfh_len, NULL, &dsp->nfsclds_sess, vers, minorvers);
6110 NFSCL_DEBUG(4, "nfsrpc_readds: vers3\n");
6112 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED * 3);
6113 txdr_hyper(io_off, tl);
6114 *(tl + 2) = txdr_unsigned(len);
6115 nrp = dsp->nfsclds_sockp;
6116 NFSCL_DEBUG(4, "nfsrpc_readds: nrp=%p\n", nrp);
6118 /* If NULL, use the MDS socket. */
6119 nrp = &nmp->nm_sockreq;
6120 error = newnfs_request(nd, nmp, NULL, nrp, vp, p, cred,
6121 NFS_PROG, vers, NULL, 1, NULL, &dsp->nfsclds_sess);
6122 NFSCL_DEBUG(4, "nfsrpc_readds: stat=%d err=%d\n", nd->nd_repstat,
6126 if (vers == NFS_VER3) {
6127 error = nfscl_postop_attr(nd, &na, &attrflag, NULL);
6128 NFSCL_DEBUG(4, "nfsrpc_readds: postop=%d\n", error);
6132 if (nd->nd_repstat != 0) {
6133 error = nd->nd_repstat;
6136 if (vers == NFS_VER3) {
6137 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
6138 *eofp = fxdr_unsigned(int, *(tl + 1));
6140 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
6141 *eofp = fxdr_unsigned(int, *tl);
6143 NFSM_STRSIZ(retlen, len);
6144 NFSCL_DEBUG(4, "nfsrpc_readds: retlen=%d eof=%d\n", retlen, *eofp);
6145 error = nfsm_mbufuio(nd, uiop, retlen);
6147 if (nd->nd_mrep != NULL)
6148 mbuf_freem(nd->nd_mrep);
6153 * The actual write RPC done to a DS.
6156 nfsrpc_writeds(vnode_t vp, struct uio *uiop, int *iomode, int *must_commit,
6157 nfsv4stateid_t *stateidp, struct nfsclds *dsp, uint64_t io_off, int len,
6158 struct nfsfh *fhp, int commit_thru_mds, int flex, int vers, int minorvers,
6159 struct ucred *cred, NFSPROC_T *p)
6162 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
6163 int attrflag, error, rlen, commit, committed = NFSWRITE_FILESYNC;
6165 struct nfsrv_descript nfsd;
6166 struct nfsrv_descript *nd = &nfsd;
6167 struct nfssockreq *nrp;
6170 KASSERT(uiop->uio_iovcnt == 1, ("nfs: writerpc iovcnt > 1"));
6172 if (vers == 0 || vers == NFS_VER4) {
6173 nfscl_reqstart(nd, NFSPROC_WRITEDS, nmp, fhp->nfh_fh,
6174 fhp->nfh_len, NULL, &dsp->nfsclds_sess, vers, minorvers);
6175 NFSCL_DEBUG(4, "nfsrpc_writeds: vers4 minvers=%d\n", minorvers);
6178 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID);
6180 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSEQIDZERO);
6181 NFSM_BUILD(tl, uint32_t *, NFSX_HYPER + 2 * NFSX_UNSIGNED);
6183 nfscl_reqstart(nd, NFSPROC_WRITE, nmp, fhp->nfh_fh,
6184 fhp->nfh_len, NULL, &dsp->nfsclds_sess, vers, minorvers);
6185 NFSCL_DEBUG(4, "nfsrpc_writeds: vers3\n");
6186 NFSM_BUILD(tl, uint32_t *, NFSX_HYPER + 3 * NFSX_UNSIGNED);
6188 txdr_hyper(io_off, tl);
6190 if (vers == NFS_VER3)
6191 *tl++ = txdr_unsigned(len);
6192 *tl++ = txdr_unsigned(*iomode);
6193 *tl = txdr_unsigned(len);
6194 nfsm_uiombuf(nd, uiop, len);
6195 nrp = dsp->nfsclds_sockp;
6197 /* If NULL, use the MDS socket. */
6198 nrp = &nmp->nm_sockreq;
6199 error = newnfs_request(nd, nmp, NULL, nrp, vp, p, cred,
6200 NFS_PROG, vers, NULL, 1, NULL, &dsp->nfsclds_sess);
6201 NFSCL_DEBUG(4, "nfsrpc_writeds: err=%d stat=%d\n", error,
6205 if (nd->nd_repstat != 0) {
6207 * In case the rpc gets retried, roll
6208 * the uio fileds changed by nfsm_uiombuf()
6211 uiop->uio_offset -= len;
6212 uio_uio_resid_add(uiop, len);
6213 uio_iov_base_add(uiop, -len);
6214 uio_iov_len_add(uiop, len);
6215 error = nd->nd_repstat;
6217 if (vers == NFS_VER3) {
6218 error = nfscl_wcc_data(nd, vp, &na, &attrflag, NULL,
6220 NFSCL_DEBUG(4, "nfsrpc_writeds: wcc_data=%d\n", error);
6224 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED + NFSX_VERF);
6225 rlen = fxdr_unsigned(int, *tl++);
6226 NFSCL_DEBUG(4, "nfsrpc_writeds: len=%d rlen=%d\n", len, rlen);
6230 } else if (rlen < len) {
6231 backup = len - rlen;
6232 uio_iov_base_add(uiop, -(backup));
6233 uio_iov_len_add(uiop, backup);
6234 uiop->uio_offset -= backup;
6235 uio_uio_resid_add(uiop, backup);
6238 commit = fxdr_unsigned(int, *tl++);
6241 * Return the lowest commitment level
6242 * obtained by any of the RPCs.
6244 if (committed == NFSWRITE_FILESYNC)
6246 else if (committed == NFSWRITE_DATASYNC &&
6247 commit == NFSWRITE_UNSTABLE)
6249 if (commit_thru_mds != 0) {
6251 if (!NFSHASWRITEVERF(nmp)) {
6252 NFSBCOPY(tl, nmp->nm_verf, NFSX_VERF);
6253 NFSSETWRITEVERF(nmp);
6254 } else if (NFSBCMP(tl, nmp->nm_verf, NFSX_VERF)) {
6256 NFSBCOPY(tl, nmp->nm_verf, NFSX_VERF);
6261 if ((dsp->nfsclds_flags & NFSCLDS_HASWRITEVERF) == 0) {
6262 NFSBCOPY(tl, dsp->nfsclds_verf, NFSX_VERF);
6263 dsp->nfsclds_flags |= NFSCLDS_HASWRITEVERF;
6264 } else if (NFSBCMP(tl, dsp->nfsclds_verf, NFSX_VERF)) {
6266 NFSBCOPY(tl, dsp->nfsclds_verf, NFSX_VERF);
6272 if (nd->nd_mrep != NULL)
6273 mbuf_freem(nd->nd_mrep);
6274 *iomode = committed;
6275 if (nd->nd_repstat != 0 && error == 0)
6276 error = nd->nd_repstat;
6281 * The actual write RPC done to a DS.
6282 * This variant is called from a separate kernel process for mirrors.
6283 * Any short write is considered an IO error.
6286 nfsrpc_writedsmir(vnode_t vp, int *iomode, int *must_commit,
6287 nfsv4stateid_t *stateidp, struct nfsclds *dsp, uint64_t io_off, int len,
6288 struct nfsfh *fhp, struct mbuf *m, int vers, int minorvers,
6289 struct ucred *cred, NFSPROC_T *p)
6292 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
6293 int attrflag, error, commit, committed = NFSWRITE_FILESYNC, rlen;
6294 struct nfsrv_descript nfsd;
6295 struct nfsrv_descript *nd = &nfsd;
6296 struct nfssockreq *nrp;
6300 if (vers == 0 || vers == NFS_VER4) {
6301 nfscl_reqstart(nd, NFSPROC_WRITEDS, nmp, fhp->nfh_fh,
6302 fhp->nfh_len, NULL, &dsp->nfsclds_sess, vers, minorvers);
6304 NFSCL_DEBUG(4, "nfsrpc_writedsmir: vers4 minvers=%d\n",
6306 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID);
6307 NFSM_BUILD(tl, uint32_t *, NFSX_HYPER + 2 * NFSX_UNSIGNED);
6309 nfscl_reqstart(nd, NFSPROC_WRITE, nmp, fhp->nfh_fh,
6310 fhp->nfh_len, NULL, &dsp->nfsclds_sess, vers, minorvers);
6311 NFSCL_DEBUG(4, "nfsrpc_writedsmir: vers3\n");
6312 NFSM_BUILD(tl, uint32_t *, NFSX_HYPER + 3 * NFSX_UNSIGNED);
6314 txdr_hyper(io_off, tl);
6316 if (vers == NFS_VER3)
6317 *tl++ = txdr_unsigned(len);
6318 *tl++ = txdr_unsigned(*iomode);
6319 *tl = txdr_unsigned(len);
6321 /* Put data in mbuf chain. */
6322 nd->nd_mb->m_next = m;
6323 /* Set nd_mb and nd_bpos to end of data. */
6324 while (m->m_next != NULL)
6327 nd->nd_bpos = mtod(m, char *) + m->m_len;
6328 NFSCL_DEBUG(4, "nfsrpc_writedsmir: lastmb len=%d\n", m->m_len);
6330 nrp = dsp->nfsclds_sockp;
6332 /* If NULL, use the MDS socket. */
6333 nrp = &nmp->nm_sockreq;
6334 error = newnfs_request(nd, nmp, NULL, nrp, vp, p, cred,
6335 NFS_PROG, vers, NULL, 1, NULL, &dsp->nfsclds_sess);
6336 NFSCL_DEBUG(4, "nfsrpc_writedsmir: err=%d stat=%d\n", error,
6340 if (nd->nd_repstat != 0)
6341 error = nd->nd_repstat;
6343 if (vers == NFS_VER3) {
6344 error = nfscl_wcc_data(nd, vp, &na, &attrflag, NULL,
6346 NFSCL_DEBUG(4, "nfsrpc_writedsmir: wcc_data=%d\n",
6351 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED + NFSX_VERF);
6352 rlen = fxdr_unsigned(int, *tl++);
6353 NFSCL_DEBUG(4, "nfsrpc_writedsmir: len=%d rlen=%d\n", len,
6357 NFSCL_DEBUG(4, "nfsrpc_writedsmir: len=%d rlen=%d\n",
6361 commit = fxdr_unsigned(int, *tl++);
6364 * Return the lowest commitment level
6365 * obtained by any of the RPCs.
6367 if (committed == NFSWRITE_FILESYNC)
6369 else if (committed == NFSWRITE_DATASYNC &&
6370 commit == NFSWRITE_UNSTABLE)
6373 if ((dsp->nfsclds_flags & NFSCLDS_HASWRITEVERF) == 0) {
6374 NFSBCOPY(tl, dsp->nfsclds_verf, NFSX_VERF);
6375 dsp->nfsclds_flags |= NFSCLDS_HASWRITEVERF;
6376 } else if (NFSBCMP(tl, dsp->nfsclds_verf, NFSX_VERF)) {
6378 NFSBCOPY(tl, dsp->nfsclds_verf, NFSX_VERF);
6383 if (nd->nd_mrep != NULL)
6384 mbuf_freem(nd->nd_mrep);
6385 *iomode = committed;
6386 if (nd->nd_repstat != 0 && error == 0)
6387 error = nd->nd_repstat;
6392 * Start up the thread that will execute nfsrpc_writedsmir().
6395 start_writedsmir(void *arg, int pending)
6397 struct nfsclwritedsdorpc *drpc;
6399 drpc = (struct nfsclwritedsdorpc *)arg;
6400 drpc->err = nfsrpc_writedsmir(drpc->vp, &drpc->iomode,
6401 &drpc->must_commit, drpc->stateidp, drpc->dsp, drpc->off, drpc->len,
6402 drpc->fhp, drpc->m, drpc->vers, drpc->minorvers, drpc->cred,
6405 NFSCL_DEBUG(4, "start_writedsmir: err=%d\n", drpc->err);
6409 * Set up the write DS mirror call for the pNFS I/O thread.
6412 nfsio_writedsmir(vnode_t vp, int *iomode, int *must_commit,
6413 nfsv4stateid_t *stateidp, struct nfsclds *dsp, uint64_t off, int len,
6414 struct nfsfh *fhp, struct mbuf *m, int vers, int minorvers,
6415 struct nfsclwritedsdorpc *drpc, struct ucred *cred, NFSPROC_T *p)
6422 drpc->iomode = *iomode;
6423 drpc->must_commit = *must_commit;
6424 drpc->stateidp = stateidp;
6431 drpc->minorvers = minorvers;
6436 if (nfs_pnfsiothreads > 0) {
6437 ret = nfs_pnfsio(start_writedsmir, drpc);
6438 NFSCL_DEBUG(4, "nfsio_writedsmir: nfs_pnfsio=%d\n", ret);
6441 error = nfsrpc_writedsmir(vp, iomode, must_commit, stateidp,
6442 dsp, off, len, fhp, m, vers, minorvers, cred, p);
6443 NFSCL_DEBUG(4, "nfsio_writedsmir: error=%d\n", error);
6448 * Free up the nfsclds structure.
6451 nfscl_freenfsclds(struct nfsclds *dsp)
6457 if (dsp->nfsclds_sockp != NULL) {
6458 NFSFREECRED(dsp->nfsclds_sockp->nr_cred);
6459 NFSFREEMUTEX(&dsp->nfsclds_sockp->nr_mtx);
6460 free(dsp->nfsclds_sockp->nr_nam, M_SONAME);
6461 free(dsp->nfsclds_sockp, M_NFSSOCKREQ);
6463 NFSFREEMUTEX(&dsp->nfsclds_mtx);
6464 NFSFREEMUTEX(&dsp->nfsclds_sess.nfsess_mtx);
6465 for (i = 0; i < NFSV4_CBSLOTS; i++) {
6466 if (dsp->nfsclds_sess.nfsess_cbslots[i].nfssl_reply != NULL)
6468 dsp->nfsclds_sess.nfsess_cbslots[i].nfssl_reply);
6470 free(dsp, M_NFSCLDS);
6473 static enum nfsclds_state
6474 nfscl_getsameserver(struct nfsmount *nmp, struct nfsclds *newdsp,
6475 struct nfsclds **retdspp)
6477 struct nfsclds *dsp, *cur_dsp;
6480 * Search the list of nfsclds structures for one with the same
6484 TAILQ_FOREACH(dsp, &nmp->nm_sess, nfsclds_list) {
6485 if (dsp->nfsclds_servownlen == newdsp->nfsclds_servownlen &&
6486 dsp->nfsclds_servownlen != 0 &&
6487 !NFSBCMP(dsp->nfsclds_serverown, newdsp->nfsclds_serverown,
6488 dsp->nfsclds_servownlen) &&
6489 dsp->nfsclds_sess.nfsess_defunct == 0) {
6490 NFSCL_DEBUG(4, "fnd same fdsp=%p dsp=%p flg=0x%x\n",
6491 TAILQ_FIRST(&nmp->nm_sess), dsp,
6492 dsp->nfsclds_flags);
6493 /* Server major id matches. */
6494 if ((dsp->nfsclds_flags & NFSCLDS_DS) != 0) {
6496 return (NFSDSP_USETHISSESSION);
6500 * Note the first match, so it can be used for
6501 * sequence'ing new sessions.
6503 if (cur_dsp == NULL)
6507 if (cur_dsp != NULL) {
6509 return (NFSDSP_SEQTHISSESSION);
6511 return (NFSDSP_NOTFOUND);
6515 * NFS commit rpc to a NFSv4.1 DS.
6518 nfsrpc_commitds(vnode_t vp, uint64_t offset, int cnt, struct nfsclds *dsp,
6519 struct nfsfh *fhp, int vers, int minorvers, struct ucred *cred,
6523 struct nfsrv_descript nfsd, *nd = &nfsd;
6524 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
6525 struct nfssockreq *nrp;
6527 int attrflag, error;
6530 if (vers == 0 || vers == NFS_VER4) {
6531 nfscl_reqstart(nd, NFSPROC_COMMITDS, nmp, fhp->nfh_fh,
6532 fhp->nfh_len, NULL, &dsp->nfsclds_sess, vers, minorvers);
6535 nfscl_reqstart(nd, NFSPROC_COMMIT, nmp, fhp->nfh_fh,
6536 fhp->nfh_len, NULL, &dsp->nfsclds_sess, vers, minorvers);
6537 NFSCL_DEBUG(4, "nfsrpc_commitds: vers=%d minvers=%d\n", vers,
6539 NFSM_BUILD(tl, uint32_t *, NFSX_HYPER + NFSX_UNSIGNED);
6540 txdr_hyper(offset, tl);
6542 *tl = txdr_unsigned(cnt);
6543 nrp = dsp->nfsclds_sockp;
6545 /* If NULL, use the MDS socket. */
6546 nrp = &nmp->nm_sockreq;
6547 error = newnfs_request(nd, nmp, NULL, nrp, vp, p, cred,
6548 NFS_PROG, vers, NULL, 1, NULL, &dsp->nfsclds_sess);
6549 NFSCL_DEBUG(4, "nfsrpc_commitds: err=%d stat=%d\n", error,
6553 if (nd->nd_repstat == 0) {
6554 if (vers == NFS_VER3) {
6555 error = nfscl_wcc_data(nd, vp, &na, &attrflag, NULL,
6557 NFSCL_DEBUG(4, "nfsrpc_commitds: wccdata=%d\n", error);
6561 NFSM_DISSECT(tl, u_int32_t *, NFSX_VERF);
6563 if (NFSBCMP(tl, dsp->nfsclds_verf, NFSX_VERF)) {
6564 NFSBCOPY(tl, dsp->nfsclds_verf, NFSX_VERF);
6565 error = NFSERR_STALEWRITEVERF;
6570 if (error == 0 && nd->nd_repstat != 0)
6571 error = nd->nd_repstat;
6572 mbuf_freem(nd->nd_mrep);
6577 * Start up the thread that will execute nfsrpc_commitds().
6580 start_commitds(void *arg, int pending)
6582 struct nfsclwritedsdorpc *drpc;
6584 drpc = (struct nfsclwritedsdorpc *)arg;
6585 drpc->err = nfsrpc_commitds(drpc->vp, drpc->off, drpc->len,
6586 drpc->dsp, drpc->fhp, drpc->vers, drpc->minorvers, drpc->cred,
6589 NFSCL_DEBUG(4, "start_commitds: err=%d\n", drpc->err);
6593 * Set up the commit DS mirror call for the pNFS I/O thread.
6596 nfsio_commitds(vnode_t vp, uint64_t offset, int cnt, struct nfsclds *dsp,
6597 struct nfsfh *fhp, int vers, int minorvers,
6598 struct nfsclwritedsdorpc *drpc, struct ucred *cred, NFSPROC_T *p)
6610 drpc->minorvers = minorvers;
6615 if (nfs_pnfsiothreads > 0) {
6616 ret = nfs_pnfsio(start_commitds, drpc);
6617 NFSCL_DEBUG(4, "nfsio_commitds: nfs_pnfsio=%d\n", ret);
6620 error = nfsrpc_commitds(vp, offset, cnt, dsp, fhp, vers,
6621 minorvers, cred, p);
6622 NFSCL_DEBUG(4, "nfsio_commitds: error=%d\n", error);
6627 * Set up the XDR arguments for the LayoutGet operation.
6630 nfsrv_setuplayoutget(struct nfsrv_descript *nd, int iomode, uint64_t offset,
6631 uint64_t len, uint64_t minlen, nfsv4stateid_t *stateidp, int layouttype,
6632 int layoutlen, int usecurstateid)
6636 NFSM_BUILD(tl, uint32_t *, 4 * NFSX_UNSIGNED + 3 * NFSX_HYPER +
6638 *tl++ = newnfs_false; /* Don't signal availability. */
6639 *tl++ = txdr_unsigned(layouttype);
6640 *tl++ = txdr_unsigned(iomode);
6641 txdr_hyper(offset, tl);
6643 txdr_hyper(len, tl);
6645 txdr_hyper(minlen, tl);
6647 if (usecurstateid != 0) {
6648 /* Special stateid for Current stateid. */
6649 *tl++ = txdr_unsigned(1);
6654 *tl++ = txdr_unsigned(stateidp->seqid);
6655 NFSCL_DEBUG(4, "layget seq=%d\n", (int)stateidp->seqid);
6656 *tl++ = stateidp->other[0];
6657 *tl++ = stateidp->other[1];
6658 *tl++ = stateidp->other[2];
6660 *tl = txdr_unsigned(layoutlen);
6664 * Parse the reply for a successful LayoutGet operation.
6667 nfsrv_parselayoutget(struct nfsrv_descript *nd, nfsv4stateid_t *stateidp,
6668 int *retonclosep, struct nfsclflayouthead *flhp)
6671 struct nfsclflayout *flp, *prevflp, *tflp;
6672 int cnt, error, fhcnt, gotiomode, i, iomode, j, k, l, laytype, nfhlen;
6674 uint64_t retlen, off;
6680 NFSCL_DEBUG(4, "in nfsrv_parselayoutget\n");
6684 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED + NFSX_STATEID);
6689 stateidp->seqid = fxdr_unsigned(uint32_t, *tl++);
6690 NFSCL_DEBUG(4, "retoncls=%d stseq=%d\n", *retonclosep,
6691 (int)stateidp->seqid);
6692 stateidp->other[0] = *tl++;
6693 stateidp->other[1] = *tl++;
6694 stateidp->other[2] = *tl++;
6695 cnt = fxdr_unsigned(int, *tl);
6696 NFSCL_DEBUG(4, "layg cnt=%d\n", cnt);
6697 if (cnt <= 0 || cnt > 10000) {
6698 /* Don't accept more than 10000 layouts in reply. */
6699 error = NFSERR_BADXDR;
6702 for (i = 0; i < cnt; i++) {
6703 /* Dissect to the layout type. */
6704 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_HYPER +
6706 off = fxdr_hyper(tl); tl += 2;
6707 retlen = fxdr_hyper(tl); tl += 2;
6708 iomode = fxdr_unsigned(int, *tl++);
6709 laytype = fxdr_unsigned(int, *tl);
6710 NFSCL_DEBUG(4, "layt=%d off=%ju len=%ju iom=%d\n", laytype,
6711 (uintmax_t)off, (uintmax_t)retlen, iomode);
6712 /* Ignore length of layout body for now. */
6713 if (laytype == NFSLAYOUT_NFSV4_1_FILES) {
6714 /* Parse the File layout up to fhcnt. */
6715 NFSM_DISSECT(tl, uint32_t *, 3 * NFSX_UNSIGNED +
6716 NFSX_HYPER + NFSX_V4DEVICEID);
6717 fhcnt = fxdr_unsigned(int, *(tl + 4 +
6718 NFSX_V4DEVICEID / NFSX_UNSIGNED));
6719 NFSCL_DEBUG(4, "fhcnt=%d\n", fhcnt);
6720 if (fhcnt < 0 || fhcnt > 100) {
6721 /* Don't accept more than 100 file handles. */
6722 error = NFSERR_BADXDR;
6726 flp = malloc(sizeof(*flp) + fhcnt *
6727 sizeof(struct nfsfh *), M_NFSFLAYOUT,
6730 flp = malloc(sizeof(*flp), M_NFSFLAYOUT,
6732 flp->nfsfl_flags = NFSFL_FILE;
6733 flp->nfsfl_fhcnt = 0;
6734 flp->nfsfl_devp = NULL;
6735 flp->nfsfl_off = off;
6736 if (flp->nfsfl_off + retlen < flp->nfsfl_off)
6737 flp->nfsfl_end = UINT64_MAX - flp->nfsfl_off;
6739 flp->nfsfl_end = flp->nfsfl_off + retlen;
6740 flp->nfsfl_iomode = iomode;
6741 if (gotiomode == -1)
6742 gotiomode = flp->nfsfl_iomode;
6743 /* Ignore layout body length for now. */
6744 NFSBCOPY(tl, flp->nfsfl_dev, NFSX_V4DEVICEID);
6745 tl += (NFSX_V4DEVICEID / NFSX_UNSIGNED);
6746 flp->nfsfl_util = fxdr_unsigned(uint32_t, *tl++);
6747 NFSCL_DEBUG(4, "flutil=0x%x\n", flp->nfsfl_util);
6748 flp->nfsfl_stripe1 = fxdr_unsigned(uint32_t, *tl++);
6749 flp->nfsfl_patoff = fxdr_hyper(tl); tl += 2;
6750 NFSCL_DEBUG(4, "stripe1=%u poff=%ju\n",
6751 flp->nfsfl_stripe1, (uintmax_t)flp->nfsfl_patoff);
6752 for (j = 0; j < fhcnt; j++) {
6753 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
6754 nfhlen = fxdr_unsigned(int, *tl);
6755 if (nfhlen <= 0 || nfhlen > NFSX_V4FHMAX) {
6756 error = NFSERR_BADXDR;
6759 nfhp = malloc(sizeof(*nfhp) + nfhlen - 1,
6761 flp->nfsfl_fh[j] = nfhp;
6763 nfhp->nfh_len = nfhlen;
6764 NFSM_DISSECT(cp, uint8_t *, NFSM_RNDUP(nfhlen));
6765 NFSBCOPY(cp, nfhp->nfh_fh, nfhlen);
6767 } else if (laytype == NFSLAYOUT_FLEXFILE) {
6768 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED +
6770 mirrorcnt = fxdr_unsigned(int, *(tl + 2));
6771 NFSCL_DEBUG(4, "mirrorcnt=%d\n", mirrorcnt);
6772 if (mirrorcnt < 1 || mirrorcnt > NFSDEV_MAXMIRRORS) {
6773 error = NFSERR_BADXDR;
6776 flp = malloc(sizeof(*flp) + mirrorcnt *
6777 sizeof(struct nfsffm), M_NFSFLAYOUT, M_WAITOK);
6778 flp->nfsfl_flags = NFSFL_FLEXFILE;
6779 flp->nfsfl_mirrorcnt = mirrorcnt;
6780 flp->nfsfl_devp = NULL;
6781 flp->nfsfl_off = off;
6782 if (flp->nfsfl_off + retlen < flp->nfsfl_off)
6783 flp->nfsfl_end = UINT64_MAX - flp->nfsfl_off;
6785 flp->nfsfl_end = flp->nfsfl_off + retlen;
6786 flp->nfsfl_iomode = iomode;
6787 if (gotiomode == -1)
6788 gotiomode = flp->nfsfl_iomode;
6789 flp->nfsfl_stripeunit = fxdr_hyper(tl);
6790 NFSCL_DEBUG(4, "stripeunit=%ju\n",
6791 (uintmax_t)flp->nfsfl_stripeunit);
6792 for (j = 0; j < mirrorcnt; j++) {
6793 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
6794 k = fxdr_unsigned(int, *tl);
6795 if (k < 1 || k > 128) {
6796 error = NFSERR_BADXDR;
6799 NFSCL_DEBUG(4, "servercnt=%d\n", k);
6800 for (l = 0; l < k; l++) {
6801 NFSM_DISSECT(tl, uint32_t *,
6802 NFSX_V4DEVICEID + NFSX_STATEID +
6805 /* Just use the first server. */
6807 flp->nfsfl_ffm[j].dev,
6809 tl += (NFSX_V4DEVICEID /
6812 flp->nfsfl_ffm[j].st.seqid =
6814 flp->nfsfl_ffm[j].st.other[0] =
6816 flp->nfsfl_ffm[j].st.other[1] =
6818 flp->nfsfl_ffm[j].st.other[2] =
6820 NFSCL_DEBUG(4, "st.seqid=%u "
6821 "st.o0=0x%x st.o1=0x%x "
6823 flp->nfsfl_ffm[j].st.seqid,
6824 flp->nfsfl_ffm[j].st.other[0],
6825 flp->nfsfl_ffm[j].st.other[1],
6826 flp->nfsfl_ffm[j].st.other[2]);
6828 tl += ((NFSX_V4DEVICEID +
6832 fhcnt = fxdr_unsigned(int, *tl);
6833 NFSCL_DEBUG(4, "fhcnt=%d\n", fhcnt);
6835 fhcnt > NFSDEV_MAXVERS) {
6836 error = NFSERR_BADXDR;
6839 for (m = 0; m < fhcnt; m++) {
6840 NFSM_DISSECT(tl, uint32_t *,
6842 nfhlen = fxdr_unsigned(int,
6844 NFSCL_DEBUG(4, "nfhlen=%d\n",
6846 if (nfhlen <= 0 || nfhlen >
6848 error = NFSERR_BADXDR;
6851 NFSM_DISSECT(cp, uint8_t *,
6852 NFSM_RNDUP(nfhlen));
6854 flp->nfsfl_ffm[j].fhcnt
6858 nfhlen - 1, M_NFSFH,
6860 flp->nfsfl_ffm[j].fh[m]
6862 nfhp->nfh_len = nfhlen;
6870 /* Now, get the ffsd_user/ffds_group. */
6871 error = nfsrv_parseug(nd, 0, &user,
6873 NFSCL_DEBUG(4, "after parseu=%d\n",
6876 error = nfsrv_parseug(nd, 1,
6877 &user, &grp, curthread);
6878 NFSCL_DEBUG(4, "aft parseg=%d\n",
6882 NFSCL_DEBUG(4, "user=%d group=%d\n",
6885 flp->nfsfl_ffm[j].user = user;
6886 flp->nfsfl_ffm[j].group = grp;
6888 "usr=%d grp=%d\n", user,
6893 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
6894 flp->nfsfl_fflags = fxdr_unsigned(uint32_t, *tl++);
6895 flp->nfsfl_statshint = fxdr_unsigned(uint32_t, *tl);
6896 NFSCL_DEBUG(4, "fflags=0x%x statshint=%d\n",
6897 flp->nfsfl_fflags, flp->nfsfl_statshint);
6899 error = NFSERR_BADXDR;
6902 if (flp->nfsfl_iomode == gotiomode) {
6903 /* Keep the list in increasing offset order. */
6904 tflp = LIST_FIRST(flhp);
6906 while (tflp != NULL &&
6907 tflp->nfsfl_off < flp->nfsfl_off) {
6909 tflp = LIST_NEXT(tflp, nfsfl_list);
6911 if (prevflp == NULL)
6912 LIST_INSERT_HEAD(flhp, flp, nfsfl_list);
6914 LIST_INSERT_AFTER(prevflp, flp,
6916 NFSCL_DEBUG(4, "flp inserted\n");
6918 printf("nfscl_layoutget(): got wrong iomode\n");
6919 nfscl_freeflayout(flp);
6924 NFSCL_DEBUG(4, "eo nfsrv_parselayoutget=%d\n", error);
6925 if (error != 0 && flp != NULL)
6926 nfscl_freeflayout(flp);
6931 * Parse a user/group digit string.
6934 nfsrv_parseug(struct nfsrv_descript *nd, int dogrp, uid_t *uidp, gid_t *gidp,
6938 char *cp, *str, str0[NFSV4_SMALLSTR + 1];
6942 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
6943 len = fxdr_unsigned(uint32_t, *tl);
6944 if (len > NFSV4_OPAQUELIMIT) {
6945 error = NFSERR_BADXDR;
6948 NFSCL_DEBUG(4, "nfsrv_parseug: len=%d\n", len);
6951 *gidp = GID_NOGROUP;
6956 if (len > NFSV4_SMALLSTR)
6957 str = malloc(len + 1, M_TEMP, M_WAITOK);
6960 NFSM_DISSECT(cp, char *, NFSM_RNDUP(len));
6961 NFSBCOPY(cp, str, len);
6963 NFSCL_DEBUG(4, "nfsrv_parseug: str=%s\n", str);
6965 error = nfsv4_strtogid(nd, str, len, gidp, p);
6967 error = nfsv4_strtouid(nd, str, len, uidp, p);
6969 if (len > NFSV4_SMALLSTR)
6971 NFSCL_DEBUG(4, "eo nfsrv_parseug=%d\n", error);
6976 * Similar to nfsrpc_getlayout(), except that it uses nfsrpc_openlayget(),
6977 * so that it does both an Open and a Layoutget.
6980 nfsrpc_getopenlayout(struct nfsmount *nmp, vnode_t vp, u_int8_t *nfhp,
6981 int fhlen, uint8_t *newfhp, int newfhlen, uint32_t mode,
6982 struct nfsclopen *op, uint8_t *name, int namelen, struct nfscldeleg **dpp,
6983 struct ucred *cred, NFSPROC_T *p)
6985 struct nfscllayout *lyp;
6986 struct nfsclflayout *flp;
6987 struct nfsclflayouthead flh;
6988 int error, islocked, layoutlen, recalled, retonclose, usecurstateid;
6989 int layouttype, laystat;
6990 nfsv4stateid_t stateid;
6991 struct nfsclsession *tsep;
6994 if (NFSHASFLEXFILE(nmp))
6995 layouttype = NFSLAYOUT_FLEXFILE;
6997 layouttype = NFSLAYOUT_NFSV4_1_FILES;
6999 * If lyp is returned non-NULL, there will be a refcnt (shared lock)
7000 * on it, iff flp != NULL or a lock (exclusive lock) on it iff
7003 lyp = nfscl_getlayout(nmp->nm_clp, newfhp, newfhlen, 0, &flp,
7005 NFSCL_DEBUG(4, "nfsrpc_getopenlayout nfscl_getlayout lyp=%p\n", lyp);
7008 else if (flp != NULL)
7012 if ((lyp == NULL || flp == NULL) && recalled == 0) {
7014 tsep = nfsmnt_mdssession(nmp);
7015 layoutlen = tsep->nfsess_maxcache - (NFSX_STATEID +
7021 stateid.seqid = lyp->nfsly_stateid.seqid;
7022 stateid.other[0] = lyp->nfsly_stateid.other[0];
7023 stateid.other[1] = lyp->nfsly_stateid.other[1];
7024 stateid.other[2] = lyp->nfsly_stateid.other[2];
7026 error = nfsrpc_openlayoutrpc(nmp, vp, nfhp, fhlen,
7027 newfhp, newfhlen, mode, op, name, namelen,
7028 dpp, &stateid, usecurstateid, layouttype, layoutlen,
7029 &retonclose, &flh, &laystat, cred, p);
7030 NFSCL_DEBUG(4, "aft nfsrpc_openlayoutrpc laystat=%d err=%d\n",
7032 laystat = nfsrpc_layoutgetres(nmp, vp, newfhp, newfhlen,
7033 &stateid, retonclose, NULL, &lyp, &flh, layouttype, laystat,
7034 &islocked, cred, p);
7036 error = nfsrpc_openrpc(nmp, vp, nfhp, fhlen, newfhp, newfhlen,
7037 mode, op, name, namelen, dpp, 0, 0, cred, p, 0, 0);
7039 nfscl_rellayout(lyp, 1);
7040 else if (islocked == 1)
7041 nfscl_rellayout(lyp, 0);
7046 * This function does an Open+LayoutGet for an NFSv4.1 mount with pNFS
7047 * enabled, only for the CLAIM_NULL case. All other NFSv4 Opens are
7048 * handled by nfsrpc_openrpc().
7049 * For the case where op == NULL, dvp is the directory. When op != NULL, it
7053 nfsrpc_openlayoutrpc(struct nfsmount *nmp, vnode_t vp, u_int8_t *nfhp,
7054 int fhlen, uint8_t *newfhp, int newfhlen, uint32_t mode,
7055 struct nfsclopen *op, uint8_t *name, int namelen, struct nfscldeleg **dpp,
7056 nfsv4stateid_t *stateidp, int usecurstateid, int layouttype,
7057 int layoutlen, int *retonclosep, struct nfsclflayouthead *flhp,
7058 int *laystatp, struct ucred *cred, NFSPROC_T *p)
7061 struct nfsrv_descript nfsd, *nd = &nfsd;
7062 struct nfscldeleg *ndp = NULL;
7063 struct nfsvattr nfsva;
7064 struct nfsclsession *tsep;
7065 uint32_t rflags, deleg;
7066 nfsattrbit_t attrbits;
7067 int error, ret, acesize, limitby, iomode;
7071 nfscl_reqstart(nd, NFSPROC_OPENLAYGET, nmp, nfhp, fhlen, NULL, NULL,
7073 NFSM_BUILD(tl, uint32_t *, 5 * NFSX_UNSIGNED);
7074 *tl++ = txdr_unsigned(op->nfso_own->nfsow_seqid);
7075 *tl++ = txdr_unsigned(mode & NFSV4OPEN_ACCESSBOTH);
7076 *tl++ = txdr_unsigned((mode >> NFSLCK_SHIFT) & NFSV4OPEN_DENYBOTH);
7077 tsep = nfsmnt_mdssession(nmp);
7078 *tl++ = tsep->nfsess_clientid.lval[0];
7079 *tl = tsep->nfsess_clientid.lval[1];
7080 nfsm_strtom(nd, op->nfso_own->nfsow_owner, NFSV4CL_LOCKNAMELEN);
7081 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_UNSIGNED);
7082 *tl++ = txdr_unsigned(NFSV4OPEN_NOCREATE);
7083 *tl = txdr_unsigned(NFSV4OPEN_CLAIMNULL);
7084 nfsm_strtom(nd, name, namelen);
7085 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
7086 *tl = txdr_unsigned(NFSV4OP_GETATTR);
7087 NFSZERO_ATTRBIT(&attrbits);
7088 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_CHANGE);
7089 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEMODIFY);
7090 nfsrv_putattrbit(nd, &attrbits);
7091 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
7092 *tl = txdr_unsigned(NFSV4OP_LAYOUTGET);
7093 if ((mode & NFSV4OPEN_ACCESSWRITE) != 0)
7094 iomode = NFSLAYOUTIOMODE_RW;
7096 iomode = NFSLAYOUTIOMODE_READ;
7097 nfsrv_setuplayoutget(nd, iomode, 0, UINT64_MAX, 0, stateidp,
7098 layouttype, layoutlen, usecurstateid);
7099 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, vp, p, cred,
7100 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
7103 NFSCL_INCRSEQID(op->nfso_own->nfsow_seqid, nd);
7104 if (nd->nd_repstat != 0)
7105 *laystatp = nd->nd_repstat;
7106 if ((nd->nd_flag & ND_NOMOREDATA) == 0) {
7107 /* ND_NOMOREDATA will be set if the Open operation failed. */
7108 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
7110 op->nfso_stateid.seqid = *tl++;
7111 op->nfso_stateid.other[0] = *tl++;
7112 op->nfso_stateid.other[1] = *tl++;
7113 op->nfso_stateid.other[2] = *tl;
7114 rflags = fxdr_unsigned(u_int32_t, *(tl + 6));
7115 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
7118 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
7119 deleg = fxdr_unsigned(u_int32_t, *tl);
7120 if (deleg == NFSV4OPEN_DELEGATEREAD ||
7121 deleg == NFSV4OPEN_DELEGATEWRITE) {
7122 if (!(op->nfso_own->nfsow_clp->nfsc_flags &
7123 NFSCLFLAGS_FIRSTDELEG))
7124 op->nfso_own->nfsow_clp->nfsc_flags |=
7125 (NFSCLFLAGS_FIRSTDELEG | NFSCLFLAGS_GOTDELEG);
7126 ndp = malloc(sizeof(struct nfscldeleg) + newfhlen,
7127 M_NFSCLDELEG, M_WAITOK);
7128 LIST_INIT(&ndp->nfsdl_owner);
7129 LIST_INIT(&ndp->nfsdl_lock);
7130 ndp->nfsdl_clp = op->nfso_own->nfsow_clp;
7131 ndp->nfsdl_fhlen = newfhlen;
7132 NFSBCOPY(newfhp, ndp->nfsdl_fh, newfhlen);
7133 newnfs_copyincred(cred, &ndp->nfsdl_cred);
7134 nfscl_lockinit(&ndp->nfsdl_rwlock);
7135 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
7137 ndp->nfsdl_stateid.seqid = *tl++;
7138 ndp->nfsdl_stateid.other[0] = *tl++;
7139 ndp->nfsdl_stateid.other[1] = *tl++;
7140 ndp->nfsdl_stateid.other[2] = *tl++;
7141 ret = fxdr_unsigned(int, *tl);
7142 if (deleg == NFSV4OPEN_DELEGATEWRITE) {
7143 ndp->nfsdl_flags = NFSCLDL_WRITE;
7145 * Indicates how much the file can grow.
7147 NFSM_DISSECT(tl, u_int32_t *,
7149 limitby = fxdr_unsigned(int, *tl++);
7151 case NFSV4OPEN_LIMITSIZE:
7152 ndp->nfsdl_sizelimit = fxdr_hyper(tl);
7154 case NFSV4OPEN_LIMITBLOCKS:
7155 ndp->nfsdl_sizelimit =
7156 fxdr_unsigned(u_int64_t, *tl++);
7157 ndp->nfsdl_sizelimit *=
7158 fxdr_unsigned(u_int64_t, *tl);
7161 error = NFSERR_BADXDR;
7165 ndp->nfsdl_flags = NFSCLDL_READ;
7167 ndp->nfsdl_flags |= NFSCLDL_RECALL;
7168 error = nfsrv_dissectace(nd, &ndp->nfsdl_ace, &ret,
7172 } else if (deleg != NFSV4OPEN_DELEGATENONE) {
7173 error = NFSERR_BADXDR;
7176 if ((rflags & NFSV4OPEN_LOCKTYPEPOSIX) != 0 ||
7177 nfscl_assumeposixlocks)
7178 op->nfso_posixlock = 1;
7180 op->nfso_posixlock = 0;
7181 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
7182 /* If the 2nd element == NFS_OK, the Getattr succeeded. */
7184 error = nfsv4_loadattr(nd, NULL, &nfsva, NULL,
7185 NULL, 0, NULL, NULL, NULL, NULL, NULL, 0,
7186 NULL, NULL, NULL, p, cred);
7190 ndp->nfsdl_change = nfsva.na_filerev;
7191 ndp->nfsdl_modtime = nfsva.na_mtime;
7192 ndp->nfsdl_flags |= NFSCLDL_MODTIMESET;
7197 * At this point, the Open has succeeded, so set
7198 * nd_repstat = NFS_OK. If the Layoutget failed,
7199 * this function just won't return a layout.
7201 if (nd->nd_repstat == 0) {
7202 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
7203 *laystatp = fxdr_unsigned(int, *++tl);
7204 if (*laystatp == 0) {
7205 error = nfsrv_parselayoutget(nd,
7206 stateidp, retonclosep, flhp);
7211 nd->nd_repstat = 0; /* Return 0 for Open. */
7214 if (nd->nd_repstat != 0 && error == 0)
7215 error = nd->nd_repstat;
7217 free(ndp, M_NFSCLDELEG);
7218 mbuf_freem(nd->nd_mrep);
7223 * Similar nfsrpc_createv4(), but also does the LayoutGet operation.
7224 * Used only for mounts with pNFS enabled.
7227 nfsrpc_createlayout(vnode_t dvp, char *name, int namelen, struct vattr *vap,
7228 nfsquad_t cverf, int fmode, struct nfsclowner *owp, struct nfscldeleg **dpp,
7229 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap,
7230 struct nfsvattr *nnap, struct nfsfh **nfhpp, int *attrflagp,
7231 int *dattrflagp, void *dstuff, int *unlockedp, nfsv4stateid_t *stateidp,
7232 int usecurstateid, int layouttype, int layoutlen, int *retonclosep,
7233 struct nfsclflayouthead *flhp, int *laystatp)
7236 int error = 0, deleg, newone, ret, acesize, limitby;
7237 struct nfsrv_descript nfsd, *nd = &nfsd;
7238 struct nfsclopen *op;
7239 struct nfscldeleg *dp = NULL;
7242 struct nfsclsession *tsep;
7243 nfsattrbit_t attrbits;
7244 nfsv4stateid_t stateid;
7246 struct nfsmount *nmp;
7248 nmp = VFSTONFS(dvp->v_mount);
7256 if (namelen > NFS_MAXNAMLEN)
7257 return (ENAMETOOLONG);
7258 NFSCL_REQSTART(nd, NFSPROC_CREATELAYGET, dvp);
7260 * For V4, this is actually an Open op.
7262 NFSM_BUILD(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
7263 *tl++ = txdr_unsigned(owp->nfsow_seqid);
7264 *tl++ = txdr_unsigned(NFSV4OPEN_ACCESSWRITE |
7265 NFSV4OPEN_ACCESSREAD);
7266 *tl++ = txdr_unsigned(NFSV4OPEN_DENYNONE);
7267 tsep = nfsmnt_mdssession(nmp);
7268 *tl++ = tsep->nfsess_clientid.lval[0];
7269 *tl = tsep->nfsess_clientid.lval[1];
7270 nfsm_strtom(nd, owp->nfsow_owner, NFSV4CL_LOCKNAMELEN);
7271 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
7272 *tl++ = txdr_unsigned(NFSV4OPEN_CREATE);
7273 if ((fmode & O_EXCL) != 0) {
7274 if (NFSHASSESSPERSIST(nmp)) {
7275 /* Use GUARDED for persistent sessions. */
7276 *tl = txdr_unsigned(NFSCREATE_GUARDED);
7277 nfscl_fillsattr(nd, vap, dvp, 0, 0);
7279 /* Otherwise, use EXCLUSIVE4_1. */
7280 *tl = txdr_unsigned(NFSCREATE_EXCLUSIVE41);
7281 NFSM_BUILD(tl, u_int32_t *, NFSX_VERF);
7282 *tl++ = cverf.lval[0];
7283 *tl = cverf.lval[1];
7284 nfscl_fillsattr(nd, vap, dvp, 0, 0);
7287 *tl = txdr_unsigned(NFSCREATE_UNCHECKED);
7288 nfscl_fillsattr(nd, vap, dvp, 0, 0);
7290 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
7291 *tl = txdr_unsigned(NFSV4OPEN_CLAIMNULL);
7292 nfsm_strtom(nd, name, namelen);
7293 /* Get the new file's handle and attributes, plus save the FH. */
7294 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
7295 *tl++ = txdr_unsigned(NFSV4OP_SAVEFH);
7296 *tl++ = txdr_unsigned(NFSV4OP_GETFH);
7297 *tl = txdr_unsigned(NFSV4OP_GETATTR);
7298 NFSGETATTR_ATTRBIT(&attrbits);
7299 nfsrv_putattrbit(nd, &attrbits);
7300 /* Get the directory's post-op attributes. */
7301 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
7302 *tl = txdr_unsigned(NFSV4OP_PUTFH);
7303 nfsm_fhtom(nd, np->n_fhp->nfh_fh, np->n_fhp->nfh_len, 0);
7304 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
7305 *tl = txdr_unsigned(NFSV4OP_GETATTR);
7306 nfsrv_putattrbit(nd, &attrbits);
7307 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
7308 *tl++ = txdr_unsigned(NFSV4OP_RESTOREFH);
7309 *tl = txdr_unsigned(NFSV4OP_LAYOUTGET);
7310 nfsrv_setuplayoutget(nd, NFSLAYOUTIOMODE_RW, 0, UINT64_MAX, 0, stateidp,
7311 layouttype, layoutlen, usecurstateid);
7312 error = nfscl_request(nd, dvp, p, cred, dstuff);
7315 NFSCL_DEBUG(4, "nfsrpc_createlayout stat=%d err=%d\n", nd->nd_repstat,
7317 if (nd->nd_repstat != 0)
7318 *laystatp = nd->nd_repstat;
7319 NFSCL_INCRSEQID(owp->nfsow_seqid, nd);
7320 if ((nd->nd_flag & ND_NOMOREDATA) == 0) {
7321 NFSCL_DEBUG(4, "nfsrpc_createlayout open succeeded\n");
7322 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
7324 stateid.seqid = *tl++;
7325 stateid.other[0] = *tl++;
7326 stateid.other[1] = *tl++;
7327 stateid.other[2] = *tl;
7328 rflags = fxdr_unsigned(u_int32_t, *(tl + 6));
7329 nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
7330 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
7331 deleg = fxdr_unsigned(int, *tl);
7332 if (deleg == NFSV4OPEN_DELEGATEREAD ||
7333 deleg == NFSV4OPEN_DELEGATEWRITE) {
7334 if (!(owp->nfsow_clp->nfsc_flags &
7335 NFSCLFLAGS_FIRSTDELEG))
7336 owp->nfsow_clp->nfsc_flags |=
7337 (NFSCLFLAGS_FIRSTDELEG | NFSCLFLAGS_GOTDELEG);
7338 dp = malloc(sizeof(struct nfscldeleg) + NFSX_V4FHMAX,
7339 M_NFSCLDELEG, M_WAITOK);
7340 LIST_INIT(&dp->nfsdl_owner);
7341 LIST_INIT(&dp->nfsdl_lock);
7342 dp->nfsdl_clp = owp->nfsow_clp;
7343 newnfs_copyincred(cred, &dp->nfsdl_cred);
7344 nfscl_lockinit(&dp->nfsdl_rwlock);
7345 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
7347 dp->nfsdl_stateid.seqid = *tl++;
7348 dp->nfsdl_stateid.other[0] = *tl++;
7349 dp->nfsdl_stateid.other[1] = *tl++;
7350 dp->nfsdl_stateid.other[2] = *tl++;
7351 ret = fxdr_unsigned(int, *tl);
7352 if (deleg == NFSV4OPEN_DELEGATEWRITE) {
7353 dp->nfsdl_flags = NFSCLDL_WRITE;
7355 * Indicates how much the file can grow.
7357 NFSM_DISSECT(tl, u_int32_t *,
7359 limitby = fxdr_unsigned(int, *tl++);
7361 case NFSV4OPEN_LIMITSIZE:
7362 dp->nfsdl_sizelimit = fxdr_hyper(tl);
7364 case NFSV4OPEN_LIMITBLOCKS:
7365 dp->nfsdl_sizelimit =
7366 fxdr_unsigned(u_int64_t, *tl++);
7367 dp->nfsdl_sizelimit *=
7368 fxdr_unsigned(u_int64_t, *tl);
7371 error = NFSERR_BADXDR;
7375 dp->nfsdl_flags = NFSCLDL_READ;
7378 dp->nfsdl_flags |= NFSCLDL_RECALL;
7379 error = nfsrv_dissectace(nd, &dp->nfsdl_ace, &ret,
7383 } else if (deleg != NFSV4OPEN_DELEGATENONE) {
7384 error = NFSERR_BADXDR;
7388 /* Now, we should have the status for the SaveFH. */
7389 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
7391 NFSCL_DEBUG(4, "nfsrpc_createlayout SaveFH ok\n");
7393 * Now, process the GetFH and Getattr for the newly
7394 * created file. nfscl_mtofh() will set
7395 * ND_NOMOREDATA if these weren't successful.
7397 error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp);
7398 NFSCL_DEBUG(4, "aft nfscl_mtofh err=%d\n", error);
7402 nd->nd_flag |= ND_NOMOREDATA;
7403 /* Now we have the PutFH and Getattr for the directory. */
7404 if ((nd->nd_flag & ND_NOMOREDATA) == 0) {
7405 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
7407 nd->nd_flag |= ND_NOMOREDATA;
7409 NFSM_DISSECT(tl, uint32_t *, 2 *
7412 nd->nd_flag |= ND_NOMOREDATA;
7415 if ((nd->nd_flag & ND_NOMOREDATA) == 0) {
7416 /* Load the directory attributes. */
7417 error = nfsm_loadattr(nd, dnap);
7418 NFSCL_DEBUG(4, "aft nfsm_loadattr err=%d\n", error);
7422 if (dp != NULL && *attrflagp != 0) {
7423 dp->nfsdl_change = nnap->na_filerev;
7424 dp->nfsdl_modtime = nnap->na_mtime;
7425 dp->nfsdl_flags |= NFSCLDL_MODTIMESET;
7428 * We can now complete the Open state.
7432 dp->nfsdl_fhlen = nfhp->nfh_len;
7433 NFSBCOPY(nfhp->nfh_fh, dp->nfsdl_fh,
7437 * Get an Open structure that will be
7438 * attached to the OpenOwner, acquired already.
7440 error = nfscl_open(dvp, nfhp->nfh_fh, nfhp->nfh_len,
7441 (NFSV4OPEN_ACCESSWRITE | NFSV4OPEN_ACCESSREAD), 0,
7442 cred, p, NULL, &op, &newone, NULL, 0);
7445 op->nfso_stateid = stateid;
7446 newnfs_copyincred(cred, &op->nfso_cred);
7448 nfscl_openrelease(nmp, op, error, newone);
7451 /* Now, handle the RestoreFH and LayoutGet. */
7452 if (nd->nd_repstat == 0) {
7453 NFSM_DISSECT(tl, uint32_t *, 4 * NFSX_UNSIGNED);
7454 *laystatp = fxdr_unsigned(int, *(tl + 3));
7455 if (*laystatp == 0) {
7456 error = nfsrv_parselayoutget(nd,
7457 stateidp, retonclosep, flhp);
7461 NFSCL_DEBUG(4, "aft nfsrv_parselayout err=%d\n",
7467 if (nd->nd_repstat != 0 && error == 0)
7468 error = nd->nd_repstat;
7469 if (error == NFSERR_STALECLIENTID || error == NFSERR_BADSESSION)
7470 nfscl_initiate_recovery(owp->nfsow_clp);
7472 NFSCL_DEBUG(4, "eo nfsrpc_createlayout err=%d\n", error);
7476 free(dp, M_NFSCLDELEG);
7477 mbuf_freem(nd->nd_mrep);
7482 * Similar to nfsrpc_getopenlayout(), except that it used for the Create case.
7485 nfsrpc_getcreatelayout(vnode_t dvp, char *name, int namelen, struct vattr *vap,
7486 nfsquad_t cverf, int fmode, struct nfsclowner *owp, struct nfscldeleg **dpp,
7487 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap,
7488 struct nfsvattr *nnap, struct nfsfh **nfhpp, int *attrflagp,
7489 int *dattrflagp, void *dstuff, int *unlockedp)
7491 struct nfscllayout *lyp;
7492 struct nfsclflayouthead flh;
7494 struct nfsclsession *tsep;
7495 struct nfsmount *nmp;
7496 nfsv4stateid_t stateid;
7497 int error, layoutlen, layouttype, retonclose, laystat;
7500 nmp = VFSTONFS(dvp->v_mount);
7501 if (NFSHASFLEXFILE(nmp))
7502 layouttype = NFSLAYOUT_FLEXFILE;
7504 layouttype = NFSLAYOUT_NFSV4_1_FILES;
7506 tsep = nfsmnt_mdssession(nmp);
7507 layoutlen = tsep->nfsess_maxcache - (NFSX_STATEID + 3 * NFSX_UNSIGNED);
7508 error = nfsrpc_createlayout(dvp, name, namelen, vap, cverf, fmode,
7509 owp, dpp, cred, p, dnap, nnap, nfhpp, attrflagp, dattrflagp,
7510 dstuff, unlockedp, &stateid, 1, layouttype, layoutlen, &retonclose,
7512 NFSCL_DEBUG(4, "aft nfsrpc_createlayoutrpc laystat=%d err=%d\n",
7517 laystat = nfsrpc_layoutgetres(nmp, dvp, nfhp->nfh_fh,
7518 nfhp->nfh_len, &stateid, retonclose, NULL, &lyp, &flh,
7519 layouttype, laystat, NULL, cred, p);
7521 laystat = nfsrpc_layoutgetres(nmp, dvp, NULL, 0, &stateid,
7522 retonclose, NULL, &lyp, &flh, layouttype, laystat, NULL,
7525 nfscl_rellayout(lyp, 0);
7530 * Process the results of a layoutget() operation.
7533 nfsrpc_layoutgetres(struct nfsmount *nmp, vnode_t vp, uint8_t *newfhp,
7534 int newfhlen, nfsv4stateid_t *stateidp, int retonclose, uint32_t *notifybit,
7535 struct nfscllayout **lypp, struct nfsclflayouthead *flhp, int layouttype,
7536 int laystat, int *islockedp, struct ucred *cred, NFSPROC_T *p)
7538 struct nfsclflayout *tflp;
7539 struct nfscldevinfo *dip;
7542 if (laystat == NFSERR_UNKNLAYOUTTYPE) {
7544 if (!NFSHASFLEXFILE(nmp)) {
7545 /* Switch to using Flex File Layout. */
7546 nmp->nm_state |= NFSSTA_FLEXFILE;
7547 } else if (layouttype == NFSLAYOUT_FLEXFILE) {
7549 NFSCL_DEBUG(1, "disable PNFS\n");
7550 nmp->nm_state &= ~(NFSSTA_PNFS | NFSSTA_FLEXFILE);
7555 NFSCL_DEBUG(4, "nfsrpc_layoutgetres at FOREACH\n");
7556 LIST_FOREACH(tflp, flhp, nfsfl_list) {
7557 laystat = nfscl_adddevinfo(nmp, NULL, tflp);
7558 NFSCL_DEBUG(4, "aft adddev=%d\n", laystat);
7560 if (layouttype == NFSLAYOUT_FLEXFILE)
7561 dev = tflp->nfsfl_ffm[0].dev;
7563 dev = tflp->nfsfl_dev;
7564 laystat = nfsrpc_getdeviceinfo(nmp, dev,
7565 layouttype, notifybit, &dip, cred, p);
7566 NFSCL_DEBUG(4, "aft nfsrpc_gdi=%d\n",
7570 laystat = nfscl_adddevinfo(nmp, dip, tflp);
7572 printf("getlayout: cannot add\n");
7578 * nfscl_layout() always returns with the nfsly_lock
7579 * set to a refcnt (shared lock).
7580 * Passing in dvp is sufficient, since it is only used to
7581 * get the fsid for the file system.
7583 laystat = nfscl_layout(nmp, vp, newfhp, newfhlen, stateidp,
7584 layouttype, retonclose, flhp, lypp, cred, p);
7585 NFSCL_DEBUG(4, "nfsrpc_layoutgetres: aft nfscl_layout=%d\n",
7587 if (laystat == 0 && islockedp != NULL)