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 * 4. 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>
51 SYSCTL_DECL(_vfs_nfs);
53 static int nfsignore_eexist = 0;
54 SYSCTL_INT(_vfs_nfs, OID_AUTO, ignore_eexist, CTLFLAG_RW,
55 &nfsignore_eexist, 0, "NFS ignore EEXIST replies for mkdir/symlink");
60 extern int nfs_numnfscbd;
61 extern struct timeval nfsboottime;
62 extern u_int32_t newnfs_false, newnfs_true;
63 extern nfstype nfsv34_type[9];
64 extern int nfsrv_useacl;
65 extern char nfsv4_callbackaddr[INET6_ADDRSTRLEN];
66 extern int nfscl_debuglevel;
68 int nfstest_outofseq = 0;
69 int nfscl_assumeposixlocks = 1;
70 int nfscl_enablecallb = 0;
71 short nfsv4_cbport = NFSV4_CBPORT;
72 int nfstest_openallsetattr = 0;
73 #endif /* !APPLEKEXT */
75 #define DIRHDSIZ (sizeof (struct dirent) - (MAXNAMLEN + 1))
78 * nfscl_getsameserver() can return one of three values:
79 * NFSDSP_USETHISSESSION - Use this session for the DS.
80 * NFSDSP_SEQTHISSESSION - Use the nfsclds_sequence field of this dsp for new
82 * NFSDSP_NOTFOUND - No matching server was found.
85 NFSDSP_USETHISSESSION = 0,
86 NFSDSP_SEQTHISSESSION = 1,
90 static int nfsrpc_setattrrpc(vnode_t , struct vattr *, nfsv4stateid_t *,
91 struct ucred *, NFSPROC_T *, struct nfsvattr *, int *, void *);
92 static int nfsrpc_readrpc(vnode_t , struct uio *, struct ucred *,
93 nfsv4stateid_t *, NFSPROC_T *, struct nfsvattr *, int *, void *);
94 static int nfsrpc_writerpc(vnode_t , struct uio *, int *, int *,
95 struct ucred *, nfsv4stateid_t *, NFSPROC_T *, struct nfsvattr *, int *,
97 static int nfsrpc_createv23(vnode_t , char *, int, struct vattr *,
98 nfsquad_t, int, struct ucred *, NFSPROC_T *, struct nfsvattr *,
99 struct nfsvattr *, struct nfsfh **, int *, int *, void *);
100 static int nfsrpc_createv4(vnode_t , char *, int, struct vattr *,
101 nfsquad_t, int, struct nfsclowner *, struct nfscldeleg **, struct ucred *,
102 NFSPROC_T *, struct nfsvattr *, struct nfsvattr *, struct nfsfh **, int *,
103 int *, void *, int *);
104 static int nfsrpc_locku(struct nfsrv_descript *, struct nfsmount *,
105 struct nfscllockowner *, u_int64_t, u_int64_t,
106 u_int32_t, struct ucred *, NFSPROC_T *, int);
107 static int nfsrpc_setaclrpc(vnode_t, struct ucred *, NFSPROC_T *,
108 struct acl *, nfsv4stateid_t *, void *);
109 static int nfsrpc_getlayout(struct nfsmount *, vnode_t, struct nfsfh *, int,
110 uint32_t *, nfsv4stateid_t *, uint64_t, struct nfscllayout **,
111 struct ucred *, NFSPROC_T *);
112 static int nfsrpc_fillsa(struct nfsmount *, struct sockaddr_storage *,
113 struct nfsclds **, NFSPROC_T *);
114 static void nfscl_initsessionslots(struct nfsclsession *);
115 static int nfscl_doflayoutio(vnode_t, struct uio *, int *, int *, int *,
116 nfsv4stateid_t *, int, struct nfscldevinfo *, struct nfscllayout *,
117 struct nfsclflayout *, uint64_t, uint64_t, struct ucred *, NFSPROC_T *);
118 static int nfsrpc_readds(vnode_t, struct uio *, nfsv4stateid_t *, int *,
119 struct nfsclds *, uint64_t, int, struct nfsfh *, struct ucred *,
121 static int nfsrpc_writeds(vnode_t, struct uio *, int *, int *,
122 nfsv4stateid_t *, struct nfsclds *, uint64_t, int,
123 struct nfsfh *, int, struct ucred *, NFSPROC_T *);
124 static enum nfsclds_state nfscl_getsameserver(struct nfsmount *,
125 struct nfsclds *, struct nfsclds **);
127 static int nfsrpc_commitds(vnode_t, uint64_t, int, struct nfsclds *,
128 struct nfsfh *, struct ucred *, NFSPROC_T *, void *);
132 * nfs null call from vfs.
135 nfsrpc_null(vnode_t vp, struct ucred *cred, NFSPROC_T *p)
138 struct nfsrv_descript nfsd, *nd = &nfsd;
140 NFSCL_REQSTART(nd, NFSPROC_NULL, vp);
141 error = nfscl_request(nd, vp, p, cred, NULL);
142 if (nd->nd_repstat && !error)
143 error = nd->nd_repstat;
144 mbuf_freem(nd->nd_mrep);
150 * For nfs version 3 and 4, use the access rpc to check accessibility. If file
151 * modes are changed on the server, accesses might still fail later.
154 nfsrpc_access(vnode_t vp, int acmode, struct ucred *cred,
155 NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp)
158 u_int32_t mode, rmode;
161 mode = NFSACCESS_READ;
164 if (vnode_vtype(vp) == VDIR) {
166 mode |= (NFSACCESS_MODIFY | NFSACCESS_EXTEND |
169 mode |= NFSACCESS_LOOKUP;
172 mode |= (NFSACCESS_MODIFY | NFSACCESS_EXTEND);
174 mode |= NFSACCESS_EXECUTE;
178 * Now, just call nfsrpc_accessrpc() to do the actual RPC.
180 error = nfsrpc_accessrpc(vp, mode, cred, p, nap, attrflagp, &rmode,
184 * The NFS V3 spec does not clarify whether or not
185 * the returned access bits can be a superset of
186 * the ones requested, so...
188 if (!error && (rmode & mode) != mode)
194 * The actual rpc, separated out for Darwin.
197 nfsrpc_accessrpc(vnode_t vp, u_int32_t mode, struct ucred *cred,
198 NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, u_int32_t *rmodep,
202 u_int32_t supported, rmode;
204 struct nfsrv_descript nfsd, *nd = &nfsd;
205 nfsattrbit_t attrbits;
209 NFSCL_REQSTART(nd, NFSPROC_ACCESS, vp);
210 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
211 *tl = txdr_unsigned(mode);
212 if (nd->nd_flag & ND_NFSV4) {
214 * And do a Getattr op.
216 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
217 *tl = txdr_unsigned(NFSV4OP_GETATTR);
218 NFSGETATTR_ATTRBIT(&attrbits);
219 (void) nfsrv_putattrbit(nd, &attrbits);
221 error = nfscl_request(nd, vp, p, cred, stuff);
224 if (nd->nd_flag & ND_NFSV3) {
225 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
229 if (!nd->nd_repstat) {
230 if (nd->nd_flag & ND_NFSV4) {
231 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
232 supported = fxdr_unsigned(u_int32_t, *tl++);
234 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
236 rmode = fxdr_unsigned(u_int32_t, *tl);
237 if (nd->nd_flag & ND_NFSV4)
238 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
241 * It's not obvious what should be done about
242 * unsupported access modes. For now, be paranoid
243 * and clear the unsupported ones.
248 error = nd->nd_repstat;
250 mbuf_freem(nd->nd_mrep);
258 nfsrpc_open(vnode_t vp, int amode, struct ucred *cred, NFSPROC_T *p)
260 struct nfsclopen *op;
261 struct nfscldeleg *dp;
263 struct nfsnode *np = VTONFS(vp);
264 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
265 u_int32_t mode, clidrev;
266 int ret, newone, error, expireret = 0, retrycnt;
269 * For NFSv4, Open Ops are only done on Regular Files.
271 if (vnode_vtype(vp) != VREG)
275 mode |= NFSV4OPEN_ACCESSREAD;
277 mode |= NFSV4OPEN_ACCESSWRITE;
282 { char name[100]; int namel;
283 namel = (np->n_v4->n4_namelen < 100) ? np->n_v4->n4_namelen : 99;
284 bcopy(NFS4NODENAME(np->n_v4), name, namel);
286 printf("rpcopen p=0x%x name=%s",p->p_pid,name);
287 if (nfhp->nfh_len > 0) printf(" fh=0x%x\n",nfhp->nfh_fh[12]);
288 else printf(" fhl=0\n");
293 error = nfscl_open(vp, nfhp->nfh_fh, nfhp->nfh_len, mode, 1,
294 cred, p, NULL, &op, &newone, &ret, 1);
298 if (nmp->nm_clp != NULL)
299 clidrev = nmp->nm_clp->nfsc_clientidrev;
302 if (ret == NFSCLOPEN_DOOPEN) {
303 if (np->n_v4 != NULL) {
304 error = nfsrpc_openrpc(nmp, vp, np->n_v4->n4_data,
305 np->n_v4->n4_fhlen, np->n_fhp->nfh_fh,
306 np->n_fhp->nfh_len, mode, op,
307 NFS4NODENAME(np->n_v4), np->n_v4->n4_namelen, &dp,
308 0, 0x0, cred, p, 0, 0);
311 OSBitAndAtomic((int32_t)~NDELEGMOD, (UInt32 *)&np->n_flag);
314 np->n_flag &= ~NDELEGMOD;
316 * Invalidate the attribute cache, so that
317 * attributes that pre-date the issue of a
318 * delegation are not cached, since the
319 * cached attributes will remain valid while
320 * the delegation is held.
322 NFSINVALATTRCACHE(np);
325 (void) nfscl_deleg(nmp->nm_mountp,
326 op->nfso_own->nfsow_clp,
327 nfhp->nfh_fh, nfhp->nfh_len, cred, p, &dp);
332 newnfs_copyincred(cred, &op->nfso_cred);
333 } else if (ret == NFSCLOPEN_SETCRED)
335 * This is a new local open on a delegation. It needs
336 * to have credentials so that an open can be done
337 * against the server during recovery.
339 newnfs_copyincred(cred, &op->nfso_cred);
342 * nfso_opencnt is the count of how many VOP_OPEN()s have
343 * been done on this Open successfully and a VOP_CLOSE()
344 * is expected for each of these.
345 * If error is non-zero, don't increment it, since the Open
346 * hasn't succeeded yet.
350 nfscl_openrelease(op, error, newone);
351 if (error == NFSERR_GRACE || error == NFSERR_STALECLIENTID ||
352 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
353 error == NFSERR_BADSESSION) {
354 (void) nfs_catnap(PZERO, error, "nfs_open");
355 } else if ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID)
357 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
360 } while (error == NFSERR_GRACE || error == NFSERR_STALECLIENTID ||
361 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
362 error == NFSERR_BADSESSION ||
363 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
364 expireret == 0 && clidrev != 0 && retrycnt < 4));
365 if (error && retrycnt >= 4)
371 * the actual open rpc
374 nfsrpc_openrpc(struct nfsmount *nmp, vnode_t vp, u_int8_t *nfhp, int fhlen,
375 u_int8_t *newfhp, int newfhlen, u_int32_t mode, struct nfsclopen *op,
376 u_int8_t *name, int namelen, struct nfscldeleg **dpp,
377 int reclaim, u_int32_t delegtype, struct ucred *cred, NFSPROC_T *p,
378 int syscred, int recursed)
381 struct nfsrv_descript nfsd, *nd = &nfsd;
382 struct nfscldeleg *dp, *ndp = NULL;
383 struct nfsvattr nfsva;
384 u_int32_t rflags, deleg;
385 nfsattrbit_t attrbits;
386 int error, ret, acesize, limitby;
390 nfscl_reqstart(nd, NFSPROC_OPEN, nmp, nfhp, fhlen, NULL, NULL);
391 NFSM_BUILD(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
392 *tl++ = txdr_unsigned(op->nfso_own->nfsow_seqid);
393 *tl++ = txdr_unsigned(mode & NFSV4OPEN_ACCESSBOTH);
394 *tl++ = txdr_unsigned((mode >> NFSLCK_SHIFT) & NFSV4OPEN_DENYBOTH);
395 *tl++ = NFSMNT_MDSSESSION(nmp)->nfsess_clientid.lval[0];
396 *tl = NFSMNT_MDSSESSION(nmp)->nfsess_clientid.lval[1];
397 (void) nfsm_strtom(nd, op->nfso_own->nfsow_owner, NFSV4CL_LOCKNAMELEN);
398 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
399 *tl++ = txdr_unsigned(NFSV4OPEN_NOCREATE);
401 *tl = txdr_unsigned(NFSV4OPEN_CLAIMPREVIOUS);
402 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
403 *tl = txdr_unsigned(delegtype);
406 *tl = txdr_unsigned(NFSV4OPEN_CLAIMDELEGATECUR);
407 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID);
408 if (NFSHASNFSV4N(nmp))
411 *tl++ = dp->nfsdl_stateid.seqid;
412 *tl++ = dp->nfsdl_stateid.other[0];
413 *tl++ = dp->nfsdl_stateid.other[1];
414 *tl = dp->nfsdl_stateid.other[2];
416 *tl = txdr_unsigned(NFSV4OPEN_CLAIMNULL);
418 (void) nfsm_strtom(nd, name, namelen);
420 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
421 *tl = txdr_unsigned(NFSV4OP_GETATTR);
422 NFSZERO_ATTRBIT(&attrbits);
423 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_CHANGE);
424 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEMODIFY);
425 (void) nfsrv_putattrbit(nd, &attrbits);
427 nd->nd_flag |= ND_USEGSSNAME;
428 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, vp, p, cred,
429 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
432 NFSCL_INCRSEQID(op->nfso_own->nfsow_seqid, nd);
433 if (!nd->nd_repstat) {
434 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
436 op->nfso_stateid.seqid = *tl++;
437 op->nfso_stateid.other[0] = *tl++;
438 op->nfso_stateid.other[1] = *tl++;
439 op->nfso_stateid.other[2] = *tl;
440 rflags = fxdr_unsigned(u_int32_t, *(tl + 6));
441 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
444 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
445 deleg = fxdr_unsigned(u_int32_t, *tl);
446 if (deleg == NFSV4OPEN_DELEGATEREAD ||
447 deleg == NFSV4OPEN_DELEGATEWRITE) {
448 if (!(op->nfso_own->nfsow_clp->nfsc_flags &
449 NFSCLFLAGS_FIRSTDELEG))
450 op->nfso_own->nfsow_clp->nfsc_flags |=
451 (NFSCLFLAGS_FIRSTDELEG | NFSCLFLAGS_GOTDELEG);
452 MALLOC(ndp, struct nfscldeleg *,
453 sizeof (struct nfscldeleg) + newfhlen,
454 M_NFSCLDELEG, M_WAITOK);
455 LIST_INIT(&ndp->nfsdl_owner);
456 LIST_INIT(&ndp->nfsdl_lock);
457 ndp->nfsdl_clp = op->nfso_own->nfsow_clp;
458 ndp->nfsdl_fhlen = newfhlen;
459 NFSBCOPY(newfhp, ndp->nfsdl_fh, newfhlen);
460 newnfs_copyincred(cred, &ndp->nfsdl_cred);
461 nfscl_lockinit(&ndp->nfsdl_rwlock);
462 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
464 ndp->nfsdl_stateid.seqid = *tl++;
465 ndp->nfsdl_stateid.other[0] = *tl++;
466 ndp->nfsdl_stateid.other[1] = *tl++;
467 ndp->nfsdl_stateid.other[2] = *tl++;
468 ret = fxdr_unsigned(int, *tl);
469 if (deleg == NFSV4OPEN_DELEGATEWRITE) {
470 ndp->nfsdl_flags = NFSCLDL_WRITE;
472 * Indicates how much the file can grow.
474 NFSM_DISSECT(tl, u_int32_t *,
476 limitby = fxdr_unsigned(int, *tl++);
478 case NFSV4OPEN_LIMITSIZE:
479 ndp->nfsdl_sizelimit = fxdr_hyper(tl);
481 case NFSV4OPEN_LIMITBLOCKS:
482 ndp->nfsdl_sizelimit =
483 fxdr_unsigned(u_int64_t, *tl++);
484 ndp->nfsdl_sizelimit *=
485 fxdr_unsigned(u_int64_t, *tl);
488 error = NFSERR_BADXDR;
492 ndp->nfsdl_flags = NFSCLDL_READ;
495 ndp->nfsdl_flags |= NFSCLDL_RECALL;
496 error = nfsrv_dissectace(nd, &ndp->nfsdl_ace, &ret,
500 } else if (deleg != NFSV4OPEN_DELEGATENONE) {
501 error = NFSERR_BADXDR;
504 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
505 error = nfsv4_loadattr(nd, NULL, &nfsva, NULL,
506 NULL, 0, NULL, NULL, NULL, NULL, NULL, 0,
507 NULL, NULL, NULL, p, cred);
511 ndp->nfsdl_change = nfsva.na_filerev;
512 ndp->nfsdl_modtime = nfsva.na_mtime;
513 ndp->nfsdl_flags |= NFSCLDL_MODTIMESET;
515 if (!reclaim && (rflags & NFSV4OPEN_RESULTCONFIRM)) {
517 ret = nfsrpc_openconfirm(vp, newfhp, newfhlen, op,
519 if (ret == NFSERR_DELAY)
520 (void) nfs_catnap(PZERO, ret, "nfs_open");
521 } while (ret == NFSERR_DELAY);
524 if ((rflags & NFSV4OPEN_LOCKTYPEPOSIX) ||
525 nfscl_assumeposixlocks)
526 op->nfso_posixlock = 1;
528 op->nfso_posixlock = 0;
531 * If the server is handing out delegations, but we didn't
532 * get one because an OpenConfirm was required, try the
533 * Open again, to get a delegation. This is a harmless no-op,
534 * from a server's point of view.
536 if (!reclaim && (rflags & NFSV4OPEN_RESULTCONFIRM) &&
537 (op->nfso_own->nfsow_clp->nfsc_flags & NFSCLFLAGS_GOTDELEG)
538 && !error && dp == NULL && ndp == NULL && !recursed) {
540 ret = nfsrpc_openrpc(nmp, vp, nfhp, fhlen, newfhp,
541 newfhlen, mode, op, name, namelen, &ndp, 0, 0x0,
542 cred, p, syscred, 1);
543 if (ret == NFSERR_DELAY)
544 (void) nfs_catnap(PZERO, ret, "nfs_open2");
545 } while (ret == NFSERR_DELAY);
548 FREE((caddr_t)ndp, M_NFSCLDELEG);
551 if (ret == NFSERR_STALECLIENTID ||
552 ret == NFSERR_STALEDONTRECOVER ||
553 ret == NFSERR_BADSESSION)
558 if (nd->nd_repstat != 0 && error == 0)
559 error = nd->nd_repstat;
560 if (error == NFSERR_STALECLIENTID || error == NFSERR_BADSESSION)
561 nfscl_initiate_recovery(op->nfso_own->nfsow_clp);
565 else if (ndp != NULL)
566 FREE((caddr_t)ndp, M_NFSCLDELEG);
567 mbuf_freem(nd->nd_mrep);
575 nfsrpc_opendowngrade(vnode_t vp, u_int32_t mode, struct nfsclopen *op,
576 struct ucred *cred, NFSPROC_T *p)
579 struct nfsrv_descript nfsd, *nd = &nfsd;
582 NFSCL_REQSTART(nd, NFSPROC_OPENDOWNGRADE, vp);
583 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID + 3 * NFSX_UNSIGNED);
584 if (NFSHASNFSV4N(VFSTONFS(vnode_mount(vp))))
587 *tl++ = op->nfso_stateid.seqid;
588 *tl++ = op->nfso_stateid.other[0];
589 *tl++ = op->nfso_stateid.other[1];
590 *tl++ = op->nfso_stateid.other[2];
591 *tl++ = txdr_unsigned(op->nfso_own->nfsow_seqid);
592 *tl++ = txdr_unsigned(mode & NFSV4OPEN_ACCESSBOTH);
593 *tl = txdr_unsigned((mode >> NFSLCK_SHIFT) & NFSV4OPEN_DENYBOTH);
594 error = nfscl_request(nd, vp, p, cred, NULL);
597 NFSCL_INCRSEQID(op->nfso_own->nfsow_seqid, nd);
598 if (!nd->nd_repstat) {
599 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID);
600 op->nfso_stateid.seqid = *tl++;
601 op->nfso_stateid.other[0] = *tl++;
602 op->nfso_stateid.other[1] = *tl++;
603 op->nfso_stateid.other[2] = *tl;
605 if (nd->nd_repstat && error == 0)
606 error = nd->nd_repstat;
607 if (error == NFSERR_STALESTATEID || error == NFSERR_BADSESSION)
608 nfscl_initiate_recovery(op->nfso_own->nfsow_clp);
610 mbuf_freem(nd->nd_mrep);
615 * V4 Close operation.
618 nfsrpc_close(vnode_t vp, int doclose, NFSPROC_T *p)
620 struct nfsclclient *clp;
623 if (vnode_vtype(vp) != VREG)
626 error = nfscl_doclose(vp, &clp, p);
628 error = nfscl_getclose(vp, &clp);
632 nfscl_clientrelease(clp);
640 nfsrpc_doclose(struct nfsmount *nmp, struct nfsclopen *op, NFSPROC_T *p)
642 struct nfsrv_descript nfsd, *nd = &nfsd;
643 struct nfscllockowner *lp, *nlp;
644 struct nfscllock *lop, *nlop;
646 u_int64_t off = 0, len = 0;
647 u_int32_t type = NFSV4LOCKT_READ;
648 int error, do_unlock, trycnt;
650 tcred = newnfs_getcred();
651 newnfs_copycred(&op->nfso_cred, tcred);
653 * (Theoretically this could be done in the same
654 * compound as the close, but having multiple
655 * sequenced Ops in the same compound might be
656 * too scary for some servers.)
658 if (op->nfso_posixlock) {
661 type = NFSV4LOCKT_READ;
665 * Since this function is only called from VOP_INACTIVE(), no
666 * other thread will be manipulating this Open. As such, the
667 * lock lists are not being changed by other threads, so it should
668 * be safe to do this without locking.
670 LIST_FOREACH(lp, &op->nfso_lock, nfsl_list) {
672 LIST_FOREACH_SAFE(lop, &lp->nfsl_lock, nfslo_list, nlop) {
673 if (op->nfso_posixlock == 0) {
674 off = lop->nfslo_first;
675 len = lop->nfslo_end - lop->nfslo_first;
676 if (lop->nfslo_type == F_WRLCK)
677 type = NFSV4LOCKT_WRITE;
679 type = NFSV4LOCKT_READ;
684 error = nfsrpc_locku(nd, nmp, lp, off,
685 len, type, tcred, p, 0);
686 if ((nd->nd_repstat == NFSERR_GRACE ||
687 nd->nd_repstat == NFSERR_DELAY) &&
689 (void) nfs_catnap(PZERO,
692 } while ((nd->nd_repstat == NFSERR_GRACE ||
693 nd->nd_repstat == NFSERR_DELAY) &&
694 error == 0 && trycnt++ < 5);
695 if (op->nfso_posixlock)
698 nfscl_freelock(lop, 0);
701 * Do a ReleaseLockOwner.
702 * The lock owner name nfsl_owner may be used by other opens for
703 * other files but the lock_owner4 name that nfsrpc_rellockown()
704 * puts on the wire has the file handle for this file appended
705 * to it, so it can be done now.
707 (void)nfsrpc_rellockown(nmp, lp, lp->nfsl_open->nfso_fh,
708 lp->nfsl_open->nfso_fhlen, tcred, p);
712 * There could be other Opens for different files on the same
713 * OpenOwner, so locking is required.
716 nfscl_lockexcl(&op->nfso_own->nfsow_rwlock, NFSCLSTATEMUTEXPTR);
719 error = nfscl_tryclose(op, tcred, nmp, p);
720 if (error == NFSERR_GRACE)
721 (void) nfs_catnap(PZERO, error, "nfs_close");
722 } while (error == NFSERR_GRACE);
724 nfscl_lockunlock(&op->nfso_own->nfsow_rwlock);
726 LIST_FOREACH_SAFE(lp, &op->nfso_lock, nfsl_list, nlp)
727 nfscl_freelockowner(lp, 0);
728 nfscl_freeopen(op, 0);
734 * The actual Close RPC.
737 nfsrpc_closerpc(struct nfsrv_descript *nd, struct nfsmount *nmp,
738 struct nfsclopen *op, struct ucred *cred, NFSPROC_T *p,
744 nfscl_reqstart(nd, NFSPROC_CLOSE, nmp, op->nfso_fh,
745 op->nfso_fhlen, NULL, NULL);
746 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED + NFSX_STATEID);
747 *tl++ = txdr_unsigned(op->nfso_own->nfsow_seqid);
748 if (NFSHASNFSV4N(nmp))
751 *tl++ = op->nfso_stateid.seqid;
752 *tl++ = op->nfso_stateid.other[0];
753 *tl++ = op->nfso_stateid.other[1];
754 *tl = op->nfso_stateid.other[2];
756 nd->nd_flag |= ND_USEGSSNAME;
757 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
758 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
761 NFSCL_INCRSEQID(op->nfso_own->nfsow_seqid, nd);
762 if (nd->nd_repstat == 0)
763 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID);
764 error = nd->nd_repstat;
765 if (error == NFSERR_STALESTATEID || error == NFSERR_BADSESSION)
766 nfscl_initiate_recovery(op->nfso_own->nfsow_clp);
768 mbuf_freem(nd->nd_mrep);
773 * V4 Open Confirm RPC.
776 nfsrpc_openconfirm(vnode_t vp, u_int8_t *nfhp, int fhlen,
777 struct nfsclopen *op, struct ucred *cred, NFSPROC_T *p)
780 struct nfsrv_descript nfsd, *nd = &nfsd;
781 struct nfsmount *nmp;
784 nmp = VFSTONFS(vnode_mount(vp));
785 if (NFSHASNFSV4N(nmp))
786 return (0); /* No confirmation for NFSv4.1. */
787 nfscl_reqstart(nd, NFSPROC_OPENCONFIRM, nmp, nfhp, fhlen, NULL, NULL);
788 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED + NFSX_STATEID);
789 *tl++ = op->nfso_stateid.seqid;
790 *tl++ = op->nfso_stateid.other[0];
791 *tl++ = op->nfso_stateid.other[1];
792 *tl++ = op->nfso_stateid.other[2];
793 *tl = txdr_unsigned(op->nfso_own->nfsow_seqid);
794 error = nfscl_request(nd, vp, p, cred, NULL);
797 NFSCL_INCRSEQID(op->nfso_own->nfsow_seqid, nd);
798 if (!nd->nd_repstat) {
799 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID);
800 op->nfso_stateid.seqid = *tl++;
801 op->nfso_stateid.other[0] = *tl++;
802 op->nfso_stateid.other[1] = *tl++;
803 op->nfso_stateid.other[2] = *tl;
805 error = nd->nd_repstat;
806 if (error == NFSERR_STALESTATEID || error == NFSERR_BADSESSION)
807 nfscl_initiate_recovery(op->nfso_own->nfsow_clp);
809 mbuf_freem(nd->nd_mrep);
814 * Do the setclientid and setclientid confirm RPCs. Called from nfs_statfs()
815 * when a mount has just occurred and when the server replies NFSERR_EXPIRED.
818 nfsrpc_setclient(struct nfsmount *nmp, struct nfsclclient *clp, int reclaim,
819 struct ucred *cred, NFSPROC_T *p)
822 struct nfsrv_descript nfsd;
823 struct nfsrv_descript *nd = &nfsd;
824 nfsattrbit_t attrbits;
825 u_int8_t *cp = NULL, *cp2, addr[INET6_ADDRSTRLEN + 9];
827 int error, isinet6 = 0, callblen;
830 static u_int32_t rev = 0;
831 struct nfsclds *dsp, *ndsp, *tdsp;
833 if (nfsboottime.tv_sec == 0)
834 NFSSETBOOTTIME(nfsboottime);
835 clp->nfsc_rev = rev++;
836 if (NFSHASNFSV4N(nmp)) {
837 error = nfsrpc_exchangeid(nmp, clp, &nmp->nm_sockreq,
838 NFSV4EXCH_USEPNFSMDS | NFSV4EXCH_USENONPNFS, &dsp, cred, p);
839 NFSCL_DEBUG(1, "aft exch=%d\n", error);
841 error = nfsrpc_createsession(nmp, &dsp->nfsclds_sess,
843 dsp->nfsclds_sess.nfsess_sequenceid, 1, cred, p);
846 TAILQ_FOREACH_SAFE(tdsp, &nmp->nm_sess,
848 nfscl_freenfsclds(tdsp);
849 TAILQ_INIT(&nmp->nm_sess);
850 TAILQ_INSERT_HEAD(&nmp->nm_sess, dsp,
854 nfscl_freenfsclds(dsp);
855 NFSCL_DEBUG(1, "aft createsess=%d\n", error);
857 if (error == 0 && reclaim == 0) {
858 error = nfsrpc_reclaimcomplete(nmp, cred, p);
859 NFSCL_DEBUG(1, "aft reclaimcomp=%d\n", error);
860 if (error == NFSERR_COMPLETEALREADY ||
861 error == NFSERR_NOTSUPP)
862 /* Ignore this error. */
869 * Allocate a single session structure for NFSv4.0, because some of
870 * the fields are used by NFSv4.0 although it doesn't do a session.
872 dsp = malloc(sizeof(struct nfsclds), M_NFSCLDS, M_WAITOK | M_ZERO);
873 mtx_init(&dsp->nfsclds_mtx, "nfsds", NULL, MTX_DEF);
874 mtx_init(&dsp->nfsclds_sess.nfsess_mtx, "nfssession", NULL, MTX_DEF);
876 TAILQ_INSERT_HEAD(&nmp->nm_sess, dsp, nfsclds_list);
879 nfscl_reqstart(nd, NFSPROC_SETCLIENTID, nmp, NULL, 0, NULL, NULL);
880 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
881 *tl++ = txdr_unsigned(nfsboottime.tv_sec);
882 *tl = txdr_unsigned(clp->nfsc_rev);
883 (void) nfsm_strtom(nd, clp->nfsc_id, clp->nfsc_idlen);
886 * set up the callback address
888 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
889 *tl = txdr_unsigned(NFS_CALLBCKPROG);
890 callblen = strlen(nfsv4_callbackaddr);
892 cp = nfscl_getmyip(nmp, &isinet6);
893 if (nfscl_enablecallb && nfs_numnfscbd > 0 &&
894 (callblen > 0 || cp != NULL)) {
895 port = htons(nfsv4_cbport);
896 cp2 = (u_int8_t *)&port;
899 strchr(nfsv4_callbackaddr, ':')) || isinet6) {
900 char ip6buf[INET6_ADDRSTRLEN], *ip6add;
902 (void) nfsm_strtom(nd, "tcp6", 4);
904 ip6_sprintf(ip6buf, (struct in6_addr *)cp);
907 ip6add = nfsv4_callbackaddr;
909 snprintf(addr, INET6_ADDRSTRLEN + 9, "%s.%d.%d",
910 ip6add, cp2[0], cp2[1]);
914 (void) nfsm_strtom(nd, "tcp", 3);
916 snprintf(addr, INET6_ADDRSTRLEN + 9,
917 "%d.%d.%d.%d.%d.%d", cp[0], cp[1],
918 cp[2], cp[3], cp2[0], cp2[1]);
920 snprintf(addr, INET6_ADDRSTRLEN + 9,
921 "%s.%d.%d", nfsv4_callbackaddr,
924 (void) nfsm_strtom(nd, addr, strlen(addr));
926 (void) nfsm_strtom(nd, "tcp", 3);
927 (void) nfsm_strtom(nd, "0.0.0.0.0.0", 11);
929 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
930 *tl = txdr_unsigned(clp->nfsc_cbident);
931 nd->nd_flag |= ND_USEGSSNAME;
932 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
933 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
936 if (nd->nd_repstat == 0) {
937 NFSM_DISSECT(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
938 NFSMNT_MDSSESSION(nmp)->nfsess_clientid.lval[0] = *tl++;
939 NFSMNT_MDSSESSION(nmp)->nfsess_clientid.lval[1] = *tl++;
940 confirm.lval[0] = *tl++;
941 confirm.lval[1] = *tl;
942 mbuf_freem(nd->nd_mrep);
948 nfscl_reqstart(nd, NFSPROC_SETCLIENTIDCFRM, nmp, NULL, 0, NULL,
950 NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
951 *tl++ = NFSMNT_MDSSESSION(nmp)->nfsess_clientid.lval[0];
952 *tl++ = NFSMNT_MDSSESSION(nmp)->nfsess_clientid.lval[1];
953 *tl++ = confirm.lval[0];
954 *tl = confirm.lval[1];
955 nd->nd_flag |= ND_USEGSSNAME;
956 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p,
957 cred, NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
960 mbuf_freem(nd->nd_mrep);
962 if (nd->nd_repstat == 0) {
963 nfscl_reqstart(nd, NFSPROC_GETATTR, nmp, nmp->nm_fh,
964 nmp->nm_fhsize, NULL, NULL);
965 NFSZERO_ATTRBIT(&attrbits);
966 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_LEASETIME);
967 (void) nfsrv_putattrbit(nd, &attrbits);
968 nd->nd_flag |= ND_USEGSSNAME;
969 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p,
970 cred, NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
973 if (nd->nd_repstat == 0) {
974 error = nfsv4_loadattr(nd, NULL, NULL, NULL, NULL, 0, NULL,
975 NULL, NULL, NULL, NULL, 0, NULL, &lease, NULL, p, cred);
978 clp->nfsc_renew = NFSCL_RENEW(lease);
979 clp->nfsc_expire = NFSD_MONOSEC + clp->nfsc_renew;
980 clp->nfsc_clientidrev++;
981 if (clp->nfsc_clientidrev == 0)
982 clp->nfsc_clientidrev++;
986 error = nd->nd_repstat;
988 mbuf_freem(nd->nd_mrep);
996 nfsrpc_getattr(vnode_t vp, struct ucred *cred, NFSPROC_T *p,
997 struct nfsvattr *nap, void *stuff)
999 struct nfsrv_descript nfsd, *nd = &nfsd;
1001 nfsattrbit_t attrbits;
1003 NFSCL_REQSTART(nd, NFSPROC_GETATTR, vp);
1004 if (nd->nd_flag & ND_NFSV4) {
1005 NFSGETATTR_ATTRBIT(&attrbits);
1006 (void) nfsrv_putattrbit(nd, &attrbits);
1008 error = nfscl_request(nd, vp, p, cred, stuff);
1011 if (!nd->nd_repstat)
1012 error = nfsm_loadattr(nd, nap);
1014 error = nd->nd_repstat;
1015 mbuf_freem(nd->nd_mrep);
1020 * nfs getattr call with non-vnode arguemnts.
1023 nfsrpc_getattrnovp(struct nfsmount *nmp, u_int8_t *fhp, int fhlen, int syscred,
1024 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, u_int64_t *xidp,
1027 struct nfsrv_descript nfsd, *nd = &nfsd;
1028 int error, vers = NFS_VER2;
1029 nfsattrbit_t attrbits;
1031 nfscl_reqstart(nd, NFSPROC_GETATTR, nmp, fhp, fhlen, NULL, NULL);
1032 if (nd->nd_flag & ND_NFSV4) {
1034 NFSGETATTR_ATTRBIT(&attrbits);
1035 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_LEASETIME);
1036 (void) nfsrv_putattrbit(nd, &attrbits);
1037 } else if (nd->nd_flag & ND_NFSV3) {
1041 nd->nd_flag |= ND_USEGSSNAME;
1042 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
1043 NFS_PROG, vers, NULL, 1, xidp, NULL);
1046 if (nd->nd_repstat == 0) {
1047 if ((nd->nd_flag & ND_NFSV4) != 0)
1048 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0,
1049 NULL, NULL, NULL, NULL, NULL, 0, NULL, leasep, NULL,
1052 error = nfsm_loadattr(nd, nap);
1054 error = nd->nd_repstat;
1055 mbuf_freem(nd->nd_mrep);
1060 * Do an nfs setattr operation.
1063 nfsrpc_setattr(vnode_t vp, struct vattr *vap, NFSACL_T *aclp,
1064 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *rnap, int *attrflagp,
1067 int error, expireret = 0, openerr, retrycnt;
1068 u_int32_t clidrev = 0, mode;
1069 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
1071 nfsv4stateid_t stateid;
1074 if (nmp->nm_clp != NULL)
1075 clidrev = nmp->nm_clp->nfsc_clientidrev;
1076 if (vap != NULL && NFSATTRISSET(u_quad_t, vap, va_size))
1077 mode = NFSV4OPEN_ACCESSWRITE;
1079 mode = NFSV4OPEN_ACCESSREAD;
1084 if (NFSHASNFSV4(nmp)) {
1085 nfhp = VTONFS(vp)->n_fhp;
1086 error = nfscl_getstateid(vp, nfhp->nfh_fh,
1087 nfhp->nfh_len, mode, 0, cred, p, &stateid, &lckp);
1088 if (error && vnode_vtype(vp) == VREG &&
1089 (mode == NFSV4OPEN_ACCESSWRITE ||
1090 nfstest_openallsetattr)) {
1092 * No Open stateid, so try and open the file
1095 if (mode == NFSV4OPEN_ACCESSWRITE)
1096 openerr = nfsrpc_open(vp, FWRITE, cred,
1099 openerr = nfsrpc_open(vp, FREAD, cred,
1102 (void) nfscl_getstateid(vp,
1103 nfhp->nfh_fh, nfhp->nfh_len,
1104 mode, 0, cred, p, &stateid, &lckp);
1108 error = nfsrpc_setattrrpc(vp, vap, &stateid, cred, p,
1109 rnap, attrflagp, stuff);
1111 error = nfsrpc_setaclrpc(vp, cred, p, aclp, &stateid,
1113 if (error == NFSERR_STALESTATEID || error == NFSERR_BADSESSION)
1114 nfscl_initiate_recovery(nmp->nm_clp);
1116 nfscl_lockderef(lckp);
1118 (void) nfsrpc_close(vp, 0, p);
1119 if (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
1120 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
1121 error == NFSERR_OLDSTATEID || error == NFSERR_BADSESSION) {
1122 (void) nfs_catnap(PZERO, error, "nfs_setattr");
1123 } else if ((error == NFSERR_EXPIRED ||
1124 error == NFSERR_BADSTATEID) && clidrev != 0) {
1125 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
1128 } while (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
1129 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
1130 error == NFSERR_BADSESSION ||
1131 (error == NFSERR_OLDSTATEID && retrycnt < 20) ||
1132 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
1133 expireret == 0 && clidrev != 0 && retrycnt < 4));
1134 if (error && retrycnt >= 4)
1140 nfsrpc_setattrrpc(vnode_t vp, struct vattr *vap,
1141 nfsv4stateid_t *stateidp, struct ucred *cred, NFSPROC_T *p,
1142 struct nfsvattr *rnap, int *attrflagp, void *stuff)
1145 struct nfsrv_descript nfsd, *nd = &nfsd;
1147 nfsattrbit_t attrbits;
1150 NFSCL_REQSTART(nd, NFSPROC_SETATTR, vp);
1151 if (nd->nd_flag & ND_NFSV4)
1152 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID);
1153 vap->va_type = vnode_vtype(vp);
1154 nfscl_fillsattr(nd, vap, vp, NFSSATTR_FULL, 0);
1155 if (nd->nd_flag & ND_NFSV3) {
1156 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1158 } else if (nd->nd_flag & ND_NFSV4) {
1159 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1160 *tl = txdr_unsigned(NFSV4OP_GETATTR);
1161 NFSGETATTR_ATTRBIT(&attrbits);
1162 (void) nfsrv_putattrbit(nd, &attrbits);
1164 error = nfscl_request(nd, vp, p, cred, stuff);
1167 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4))
1168 error = nfscl_wcc_data(nd, vp, rnap, attrflagp, NULL, stuff);
1169 if ((nd->nd_flag & ND_NFSV4) && !error)
1170 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
1171 if (!(nd->nd_flag & ND_NFSV3) && !nd->nd_repstat && !error)
1172 error = nfscl_postop_attr(nd, rnap, attrflagp, stuff);
1173 mbuf_freem(nd->nd_mrep);
1174 if (nd->nd_repstat && !error)
1175 error = nd->nd_repstat;
1183 nfsrpc_lookup(vnode_t dvp, char *name, int len, struct ucred *cred,
1184 NFSPROC_T *p, struct nfsvattr *dnap, struct nfsvattr *nap,
1185 struct nfsfh **nfhpp, int *attrflagp, int *dattrflagp, void *stuff)
1188 struct nfsrv_descript nfsd, *nd = &nfsd;
1189 struct nfsmount *nmp;
1192 nfsattrbit_t attrbits;
1193 int error = 0, lookupp = 0;
1197 if (vnode_vtype(dvp) != VDIR)
1199 nmp = VFSTONFS(vnode_mount(dvp));
1200 if (len > NFS_MAXNAMLEN)
1201 return (ENAMETOOLONG);
1202 if (NFSHASNFSV4(nmp) && len == 1 &&
1205 * Just return the current dir's fh.
1208 MALLOC(nfhp, struct nfsfh *, sizeof (struct nfsfh) +
1209 np->n_fhp->nfh_len, M_NFSFH, M_WAITOK);
1210 nfhp->nfh_len = np->n_fhp->nfh_len;
1211 NFSBCOPY(np->n_fhp->nfh_fh, nfhp->nfh_fh, nfhp->nfh_len);
1215 if (NFSHASNFSV4(nmp) && len == 2 &&
1216 name[0] == '.' && name[1] == '.') {
1218 NFSCL_REQSTART(nd, NFSPROC_LOOKUPP, dvp);
1220 NFSCL_REQSTART(nd, NFSPROC_LOOKUP, dvp);
1221 (void) nfsm_strtom(nd, name, len);
1223 if (nd->nd_flag & ND_NFSV4) {
1224 NFSGETATTR_ATTRBIT(&attrbits);
1225 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1226 *tl++ = txdr_unsigned(NFSV4OP_GETFH);
1227 *tl = txdr_unsigned(NFSV4OP_GETATTR);
1228 (void) nfsrv_putattrbit(nd, &attrbits);
1230 error = nfscl_request(nd, dvp, p, cred, stuff);
1233 if (nd->nd_repstat) {
1235 * When an NFSv4 Lookupp returns ENOENT, it means that
1236 * the lookup is at the root of an fs, so return this dir.
1238 if (nd->nd_repstat == NFSERR_NOENT && lookupp) {
1240 MALLOC(nfhp, struct nfsfh *, sizeof (struct nfsfh) +
1241 np->n_fhp->nfh_len, M_NFSFH, M_WAITOK);
1242 nfhp->nfh_len = np->n_fhp->nfh_len;
1243 NFSBCOPY(np->n_fhp->nfh_fh, nfhp->nfh_fh, nfhp->nfh_len);
1245 mbuf_freem(nd->nd_mrep);
1248 if (nd->nd_flag & ND_NFSV3)
1249 error = nfscl_postop_attr(nd, dnap, dattrflagp, stuff);
1250 else if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) ==
1252 /* Load the directory attributes. */
1253 error = nfsm_loadattr(nd, dnap);
1259 if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) == ND_NFSV4) {
1260 /* Load the directory attributes. */
1261 error = nfsm_loadattr(nd, dnap);
1265 /* Skip over the Lookup and GetFH operation status values. */
1266 NFSM_DISSECT(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1268 error = nfsm_getfh(nd, nfhpp);
1272 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
1273 if ((nd->nd_flag & ND_NFSV3) && !error)
1274 error = nfscl_postop_attr(nd, dnap, dattrflagp, stuff);
1276 mbuf_freem(nd->nd_mrep);
1277 if (!error && nd->nd_repstat)
1278 error = nd->nd_repstat;
1283 * Do a readlink rpc.
1286 nfsrpc_readlink(vnode_t vp, struct uio *uiop, struct ucred *cred,
1287 NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, void *stuff)
1290 struct nfsrv_descript nfsd, *nd = &nfsd;
1291 struct nfsnode *np = VTONFS(vp);
1292 nfsattrbit_t attrbits;
1293 int error, len, cangetattr = 1;
1296 NFSCL_REQSTART(nd, NFSPROC_READLINK, vp);
1297 if (nd->nd_flag & ND_NFSV4) {
1299 * And do a Getattr op.
1301 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1302 *tl = txdr_unsigned(NFSV4OP_GETATTR);
1303 NFSGETATTR_ATTRBIT(&attrbits);
1304 (void) nfsrv_putattrbit(nd, &attrbits);
1306 error = nfscl_request(nd, vp, p, cred, stuff);
1309 if (nd->nd_flag & ND_NFSV3)
1310 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
1311 if (!nd->nd_repstat && !error) {
1312 NFSM_STRSIZ(len, NFS_MAXPATHLEN);
1314 * This seems weird to me, but must have been added to
1315 * FreeBSD for some reason. The only thing I can think of
1316 * is that there was/is some server that replies with
1317 * more link data than it should?
1319 if (len == NFS_MAXPATHLEN) {
1321 if (np->n_size > 0 && np->n_size < NFS_MAXPATHLEN) {
1327 error = nfsm_mbufuio(nd, uiop, len);
1328 if ((nd->nd_flag & ND_NFSV4) && !error && cangetattr)
1329 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
1331 if (nd->nd_repstat && !error)
1332 error = nd->nd_repstat;
1334 mbuf_freem(nd->nd_mrep);
1342 nfsrpc_read(vnode_t vp, struct uio *uiop, struct ucred *cred,
1343 NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, void *stuff)
1345 int error, expireret = 0, retrycnt;
1346 u_int32_t clidrev = 0;
1347 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
1348 struct nfsnode *np = VTONFS(vp);
1349 struct ucred *newcred;
1350 struct nfsfh *nfhp = NULL;
1351 nfsv4stateid_t stateid;
1354 if (nmp->nm_clp != NULL)
1355 clidrev = nmp->nm_clp->nfsc_clientidrev;
1357 if (NFSHASNFSV4(nmp)) {
1359 newcred = NFSNEWCRED(cred);
1364 if (NFSHASNFSV4(nmp))
1365 (void)nfscl_getstateid(vp, nfhp->nfh_fh, nfhp->nfh_len,
1366 NFSV4OPEN_ACCESSREAD, 0, newcred, p, &stateid,
1368 error = nfsrpc_readrpc(vp, uiop, newcred, &stateid, p, nap,
1370 if (error == NFSERR_STALESTATEID || error == NFSERR_BADSESSION)
1371 nfscl_initiate_recovery(nmp->nm_clp);
1373 nfscl_lockderef(lckp);
1374 if (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
1375 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
1376 error == NFSERR_OLDSTATEID || error == NFSERR_BADSESSION) {
1377 (void) nfs_catnap(PZERO, error, "nfs_read");
1378 } else if ((error == NFSERR_EXPIRED ||
1379 error == NFSERR_BADSTATEID) && clidrev != 0) {
1380 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
1383 } while (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
1384 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
1385 error == NFSERR_BADSESSION ||
1386 (error == NFSERR_OLDSTATEID && retrycnt < 20) ||
1387 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
1388 expireret == 0 && clidrev != 0 && retrycnt < 4));
1389 if (error && retrycnt >= 4)
1391 if (NFSHASNFSV4(nmp))
1392 NFSFREECRED(newcred);
1397 * The actual read RPC.
1400 nfsrpc_readrpc(vnode_t vp, struct uio *uiop, struct ucred *cred,
1401 nfsv4stateid_t *stateidp, NFSPROC_T *p, struct nfsvattr *nap,
1402 int *attrflagp, void *stuff)
1405 int error = 0, len, retlen, tsiz, eof = 0;
1406 struct nfsrv_descript nfsd;
1407 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
1408 struct nfsrv_descript *nd = &nfsd;
1413 tsiz = uio_uio_resid(uiop);
1414 tmp_off = uiop->uio_offset + tsiz;
1416 if (tmp_off > nmp->nm_maxfilesize || tmp_off < uiop->uio_offset) {
1420 rsize = nmp->nm_rsize;
1425 len = (tsiz > rsize) ? rsize : tsiz;
1426 NFSCL_REQSTART(nd, NFSPROC_READ, vp);
1427 if (nd->nd_flag & ND_NFSV4)
1428 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID);
1429 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED * 3);
1430 if (nd->nd_flag & ND_NFSV2) {
1431 *tl++ = txdr_unsigned(uiop->uio_offset);
1432 *tl++ = txdr_unsigned(len);
1435 txdr_hyper(uiop->uio_offset, tl);
1436 *(tl + 2) = txdr_unsigned(len);
1439 * Since I can't do a Getattr for NFSv4 for Write, there
1440 * doesn't seem any point in doing one here, either.
1441 * (See the comment in nfsrpc_writerpc() for more info.)
1443 error = nfscl_request(nd, vp, p, cred, stuff);
1446 if (nd->nd_flag & ND_NFSV3) {
1447 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
1448 } else if (!nd->nd_repstat && (nd->nd_flag & ND_NFSV2)) {
1449 error = nfsm_loadattr(nd, nap);
1453 if (nd->nd_repstat || error) {
1455 error = nd->nd_repstat;
1458 if (nd->nd_flag & ND_NFSV3) {
1459 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1460 eof = fxdr_unsigned(int, *(tl + 1));
1461 } else if (nd->nd_flag & ND_NFSV4) {
1462 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
1463 eof = fxdr_unsigned(int, *tl);
1465 NFSM_STRSIZ(retlen, len);
1466 error = nfsm_mbufuio(nd, uiop, retlen);
1469 mbuf_freem(nd->nd_mrep);
1472 if (!(nd->nd_flag & ND_NFSV2)) {
1473 if (eof || retlen == 0)
1475 } else if (retlen < len)
1480 if (nd->nd_mrep != NULL)
1481 mbuf_freem(nd->nd_mrep);
1486 * nfs write operation
1487 * When called_from_strategy != 0, it should return EIO for an error that
1488 * indicates recovery is in progress, so that the buffer will be left
1489 * dirty and be written back to the server later. If it loops around,
1490 * the recovery thread could get stuck waiting for the buffer and recovery
1491 * will then deadlock.
1494 nfsrpc_write(vnode_t vp, struct uio *uiop, int *iomode, int *must_commit,
1495 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp,
1496 void *stuff, int called_from_strategy)
1498 int error, expireret = 0, retrycnt, nostateid;
1499 u_int32_t clidrev = 0;
1500 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
1501 struct nfsnode *np = VTONFS(vp);
1502 struct ucred *newcred;
1503 struct nfsfh *nfhp = NULL;
1504 nfsv4stateid_t stateid;
1508 if (nmp->nm_clp != NULL)
1509 clidrev = nmp->nm_clp->nfsc_clientidrev;
1511 if (NFSHASNFSV4(nmp)) {
1512 newcred = NFSNEWCRED(cred);
1519 if (NFSHASNFSV4(nmp)) {
1520 (void)nfscl_getstateid(vp, nfhp->nfh_fh, nfhp->nfh_len,
1521 NFSV4OPEN_ACCESSWRITE, 0, newcred, p, &stateid,
1523 if (stateid.other[0] == 0 && stateid.other[1] == 0 &&
1524 stateid.other[2] == 0) {
1526 NFSCL_DEBUG(1, "stateid0 in write\n");
1531 * If there is no stateid for NFSv4, it means this is an
1532 * extraneous write after close. Basically a poorly
1533 * implemented buffer cache. Just don't do the write.
1538 error = nfsrpc_writerpc(vp, uiop, iomode, must_commit,
1539 newcred, &stateid, p, nap, attrflagp, stuff);
1540 if (error == NFSERR_STALESTATEID || error == NFSERR_BADSESSION)
1541 nfscl_initiate_recovery(nmp->nm_clp);
1543 nfscl_lockderef(lckp);
1544 if (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
1545 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
1546 error == NFSERR_OLDSTATEID || error == NFSERR_BADSESSION) {
1547 (void) nfs_catnap(PZERO, error, "nfs_write");
1548 } else if ((error == NFSERR_EXPIRED ||
1549 error == NFSERR_BADSTATEID) && clidrev != 0) {
1550 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
1553 } while (error == NFSERR_GRACE || error == NFSERR_DELAY ||
1554 ((error == NFSERR_STALESTATEID || error == NFSERR_BADSESSION ||
1555 error == NFSERR_STALEDONTRECOVER) && called_from_strategy == 0) ||
1556 (error == NFSERR_OLDSTATEID && retrycnt < 20) ||
1557 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
1558 expireret == 0 && clidrev != 0 && retrycnt < 4));
1559 if (error != 0 && (retrycnt >= 4 ||
1560 ((error == NFSERR_STALESTATEID || error == NFSERR_BADSESSION ||
1561 error == NFSERR_STALEDONTRECOVER) && called_from_strategy != 0)))
1563 if (NFSHASNFSV4(nmp))
1564 NFSFREECRED(newcred);
1569 * The actual write RPC.
1572 nfsrpc_writerpc(vnode_t vp, struct uio *uiop, int *iomode,
1573 int *must_commit, struct ucred *cred, nfsv4stateid_t *stateidp,
1574 NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, void *stuff)
1577 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
1578 struct nfsnode *np = VTONFS(vp);
1579 int error = 0, len, tsiz, rlen, commit, committed = NFSWRITE_FILESYNC;
1580 int wccflag = 0, wsize;
1582 struct nfsrv_descript nfsd;
1583 struct nfsrv_descript *nd = &nfsd;
1584 nfsattrbit_t attrbits;
1587 KASSERT(uiop->uio_iovcnt == 1, ("nfs: writerpc iovcnt > 1"));
1589 tsiz = uio_uio_resid(uiop);
1590 tmp_off = uiop->uio_offset + tsiz;
1592 if (tmp_off > nmp->nm_maxfilesize || tmp_off < uiop->uio_offset) {
1596 wsize = nmp->nm_wsize;
1598 nd->nd_mrep = NULL; /* NFSv2 sometimes does a write with */
1599 nd->nd_repstat = 0; /* uio_resid == 0, so the while is not done */
1602 len = (tsiz > wsize) ? wsize : tsiz;
1603 NFSCL_REQSTART(nd, NFSPROC_WRITE, vp);
1604 if (nd->nd_flag & ND_NFSV4) {
1605 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID);
1606 NFSM_BUILD(tl, u_int32_t *, NFSX_HYPER+2*NFSX_UNSIGNED);
1607 txdr_hyper(uiop->uio_offset, tl);
1609 *tl++ = txdr_unsigned(*iomode);
1610 *tl = txdr_unsigned(len);
1611 } else if (nd->nd_flag & ND_NFSV3) {
1612 NFSM_BUILD(tl, u_int32_t *, NFSX_HYPER+3*NFSX_UNSIGNED);
1613 txdr_hyper(uiop->uio_offset, tl);
1615 *tl++ = txdr_unsigned(len);
1616 *tl++ = txdr_unsigned(*iomode);
1617 *tl = txdr_unsigned(len);
1621 NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1623 * Not sure why someone changed this, since the
1624 * RFC clearly states that "beginoffset" and
1625 * "totalcount" are ignored, but it wouldn't
1626 * surprise me if there's a busted server out there.
1628 /* Set both "begin" and "current" to non-garbage. */
1629 x = txdr_unsigned((u_int32_t)uiop->uio_offset);
1630 *tl++ = x; /* "begin offset" */
1631 *tl++ = x; /* "current offset" */
1632 x = txdr_unsigned(len);
1633 *tl++ = x; /* total to this offset */
1634 *tl = x; /* size of this write */
1637 nfsm_uiombuf(nd, uiop, len);
1639 * Although it is tempting to do a normal Getattr Op in the
1640 * NFSv4 compound, the result can be a nearly hung client
1641 * system if the Getattr asks for Owner and/or OwnerGroup.
1642 * It occurs when the client can't map either the Owner or
1643 * Owner_group name in the Getattr reply to a uid/gid. When
1644 * there is a cache miss, the kernel does an upcall to the
1645 * nfsuserd. Then, it can try and read the local /etc/passwd
1646 * or /etc/group file. It can then block in getnewbuf(),
1647 * waiting for dirty writes to be pushed to the NFS server.
1648 * The only reason this doesn't result in a complete
1649 * deadlock, is that the upcall times out and allows
1650 * the write to complete. However, progress is so slow
1651 * that it might just as well be deadlocked.
1652 * As such, we get the rest of the attributes, but not
1653 * Owner or Owner_group.
1654 * nb: nfscl_loadattrcache() needs to be told that these
1655 * partial attributes from a write rpc are being
1656 * passed in, via a argument flag.
1658 if (nd->nd_flag & ND_NFSV4) {
1659 NFSWRITEGETATTR_ATTRBIT(&attrbits);
1660 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1661 *tl = txdr_unsigned(NFSV4OP_GETATTR);
1662 (void) nfsrv_putattrbit(nd, &attrbits);
1664 error = nfscl_request(nd, vp, p, cred, stuff);
1667 if (nd->nd_repstat) {
1669 * In case the rpc gets retried, roll
1670 * the uio fileds changed by nfsm_uiombuf()
1673 uiop->uio_offset -= len;
1674 uio_uio_resid_add(uiop, len);
1675 uio_iov_base_add(uiop, -len);
1676 uio_iov_len_add(uiop, len);
1678 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) {
1679 error = nfscl_wcc_data(nd, vp, nap, attrflagp,
1684 if (!nd->nd_repstat) {
1685 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) {
1686 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED
1688 rlen = fxdr_unsigned(int, *tl++);
1692 } else if (rlen < len) {
1693 backup = len - rlen;
1694 uio_iov_base_add(uiop, -(backup));
1695 uio_iov_len_add(uiop, backup);
1696 uiop->uio_offset -= backup;
1697 uio_uio_resid_add(uiop, backup);
1700 commit = fxdr_unsigned(int, *tl++);
1703 * Return the lowest committment level
1704 * obtained by any of the RPCs.
1706 if (committed == NFSWRITE_FILESYNC)
1708 else if (committed == NFSWRITE_DATASYNC &&
1709 commit == NFSWRITE_UNSTABLE)
1712 if (!NFSHASWRITEVERF(nmp)) {
1713 NFSBCOPY((caddr_t)tl,
1714 (caddr_t)&nmp->nm_verf[0],
1716 NFSSETWRITEVERF(nmp);
1717 } else if (NFSBCMP(tl, nmp->nm_verf,
1720 NFSBCOPY(tl, nmp->nm_verf, NFSX_VERF);
1724 if (nd->nd_flag & ND_NFSV4)
1725 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1726 if (nd->nd_flag & (ND_NFSV2 | ND_NFSV4)) {
1727 error = nfsm_loadattr(nd, nap);
1729 *attrflagp = NFS_LATTR_NOSHRINK;
1732 error = nd->nd_repstat;
1736 NFSWRITERPC_SETTIME(wccflag, np, (nd->nd_flag & ND_NFSV4));
1737 mbuf_freem(nd->nd_mrep);
1742 if (nd->nd_mrep != NULL)
1743 mbuf_freem(nd->nd_mrep);
1744 *iomode = committed;
1745 if (nd->nd_repstat && !error)
1746 error = nd->nd_repstat;
1752 * For NFS v2 this is a kludge. Use a create rpc but with the IFMT bits of the
1753 * mode set to specify the file type and the size field for rdev.
1756 nfsrpc_mknod(vnode_t dvp, char *name, int namelen, struct vattr *vap,
1757 u_int32_t rdev, enum vtype vtyp, struct ucred *cred, NFSPROC_T *p,
1758 struct nfsvattr *dnap, struct nfsvattr *nnap, struct nfsfh **nfhpp,
1759 int *attrflagp, int *dattrflagp, void *dstuff)
1763 struct nfsrv_descript nfsd, *nd = &nfsd;
1764 nfsattrbit_t attrbits;
1769 if (namelen > NFS_MAXNAMLEN)
1770 return (ENAMETOOLONG);
1771 NFSCL_REQSTART(nd, NFSPROC_MKNOD, dvp);
1772 if (nd->nd_flag & ND_NFSV4) {
1773 if (vtyp == VBLK || vtyp == VCHR) {
1774 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
1775 *tl++ = vtonfsv34_type(vtyp);
1776 *tl++ = txdr_unsigned(NFSMAJOR(rdev));
1777 *tl = txdr_unsigned(NFSMINOR(rdev));
1779 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1780 *tl = vtonfsv34_type(vtyp);
1783 (void) nfsm_strtom(nd, name, namelen);
1784 if (nd->nd_flag & ND_NFSV3) {
1785 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1786 *tl = vtonfsv34_type(vtyp);
1788 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4))
1789 nfscl_fillsattr(nd, vap, dvp, 0, 0);
1790 if ((nd->nd_flag & ND_NFSV3) &&
1791 (vtyp == VCHR || vtyp == VBLK)) {
1792 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1793 *tl++ = txdr_unsigned(NFSMAJOR(rdev));
1794 *tl = txdr_unsigned(NFSMINOR(rdev));
1796 if (nd->nd_flag & ND_NFSV4) {
1797 NFSGETATTR_ATTRBIT(&attrbits);
1798 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1799 *tl++ = txdr_unsigned(NFSV4OP_GETFH);
1800 *tl = txdr_unsigned(NFSV4OP_GETATTR);
1801 (void) nfsrv_putattrbit(nd, &attrbits);
1803 if (nd->nd_flag & ND_NFSV2)
1804 nfscl_fillsattr(nd, vap, dvp, NFSSATTR_SIZERDEV, rdev);
1805 error = nfscl_request(nd, dvp, p, cred, dstuff);
1808 if (nd->nd_flag & ND_NFSV4)
1809 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
1810 if (!nd->nd_repstat) {
1811 if (nd->nd_flag & ND_NFSV4) {
1812 NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
1813 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
1817 error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp);
1821 if (nd->nd_flag & ND_NFSV3)
1822 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
1823 if (!error && nd->nd_repstat)
1824 error = nd->nd_repstat;
1826 mbuf_freem(nd->nd_mrep);
1831 * nfs file create call
1832 * Mostly just call the approriate routine. (I separated out v4, so that
1833 * error recovery wouldn't be as difficult.)
1836 nfsrpc_create(vnode_t dvp, char *name, int namelen, struct vattr *vap,
1837 nfsquad_t cverf, int fmode, struct ucred *cred, NFSPROC_T *p,
1838 struct nfsvattr *dnap, struct nfsvattr *nnap, struct nfsfh **nfhpp,
1839 int *attrflagp, int *dattrflagp, void *dstuff)
1841 int error = 0, newone, expireret = 0, retrycnt, unlocked;
1842 struct nfsclowner *owp;
1843 struct nfscldeleg *dp;
1844 struct nfsmount *nmp = VFSTONFS(vnode_mount(dvp));
1847 if (NFSHASNFSV4(nmp)) {
1851 error = nfscl_open(dvp, NULL, 0, (NFSV4OPEN_ACCESSWRITE |
1852 NFSV4OPEN_ACCESSREAD), 0, cred, p, &owp, NULL, &newone,
1856 if (nmp->nm_clp != NULL)
1857 clidrev = nmp->nm_clp->nfsc_clientidrev;
1860 error = nfsrpc_createv4(dvp, name, namelen, vap, cverf, fmode,
1861 owp, &dp, cred, p, dnap, nnap, nfhpp, attrflagp, dattrflagp,
1864 * There is no need to invalidate cached attributes here,
1865 * since new post-delegation issue attributes are always
1866 * returned by nfsrpc_createv4() and these will update the
1870 (void) nfscl_deleg(nmp->nm_mountp, owp->nfsow_clp,
1871 (*nfhpp)->nfh_fh, (*nfhpp)->nfh_len, cred, p, &dp);
1872 nfscl_ownerrelease(owp, error, newone, unlocked);
1873 if (error == NFSERR_GRACE || error == NFSERR_STALECLIENTID ||
1874 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
1875 error == NFSERR_BADSESSION) {
1876 (void) nfs_catnap(PZERO, error, "nfs_open");
1877 } else if ((error == NFSERR_EXPIRED ||
1878 error == NFSERR_BADSTATEID) && clidrev != 0) {
1879 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
1882 } while (error == NFSERR_GRACE || error == NFSERR_STALECLIENTID ||
1883 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
1884 error == NFSERR_BADSESSION ||
1885 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
1886 expireret == 0 && clidrev != 0 && retrycnt < 4));
1887 if (error && retrycnt >= 4)
1890 error = nfsrpc_createv23(dvp, name, namelen, vap, cverf,
1891 fmode, cred, p, dnap, nnap, nfhpp, attrflagp, dattrflagp,
1898 * The create rpc for v2 and 3.
1901 nfsrpc_createv23(vnode_t dvp, char *name, int namelen, struct vattr *vap,
1902 nfsquad_t cverf, int fmode, struct ucred *cred, NFSPROC_T *p,
1903 struct nfsvattr *dnap, struct nfsvattr *nnap, struct nfsfh **nfhpp,
1904 int *attrflagp, int *dattrflagp, void *dstuff)
1908 struct nfsrv_descript nfsd, *nd = &nfsd;
1913 if (namelen > NFS_MAXNAMLEN)
1914 return (ENAMETOOLONG);
1915 NFSCL_REQSTART(nd, NFSPROC_CREATE, dvp);
1916 (void) nfsm_strtom(nd, name, namelen);
1917 if (nd->nd_flag & ND_NFSV3) {
1918 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1919 if (fmode & O_EXCL) {
1920 *tl = txdr_unsigned(NFSCREATE_EXCLUSIVE);
1921 NFSM_BUILD(tl, u_int32_t *, NFSX_VERF);
1922 *tl++ = cverf.lval[0];
1923 *tl = cverf.lval[1];
1925 *tl = txdr_unsigned(NFSCREATE_UNCHECKED);
1926 nfscl_fillsattr(nd, vap, dvp, 0, 0);
1929 nfscl_fillsattr(nd, vap, dvp, NFSSATTR_SIZE0, 0);
1931 error = nfscl_request(nd, dvp, p, cred, dstuff);
1934 if (nd->nd_repstat == 0) {
1935 error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp);
1939 if (nd->nd_flag & ND_NFSV3)
1940 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
1941 if (nd->nd_repstat != 0 && error == 0)
1942 error = nd->nd_repstat;
1944 mbuf_freem(nd->nd_mrep);
1949 nfsrpc_createv4(vnode_t dvp, char *name, int namelen, struct vattr *vap,
1950 nfsquad_t cverf, int fmode, struct nfsclowner *owp, struct nfscldeleg **dpp,
1951 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap,
1952 struct nfsvattr *nnap, struct nfsfh **nfhpp, int *attrflagp,
1953 int *dattrflagp, void *dstuff, int *unlockedp)
1956 int error = 0, deleg, newone, ret, acesize, limitby;
1957 struct nfsrv_descript nfsd, *nd = &nfsd;
1958 struct nfsclopen *op;
1959 struct nfscldeleg *dp = NULL;
1962 nfsattrbit_t attrbits;
1963 nfsv4stateid_t stateid;
1965 struct nfsmount *nmp;
1967 nmp = VFSTONFS(dvp->v_mount);
1974 if (namelen > NFS_MAXNAMLEN)
1975 return (ENAMETOOLONG);
1976 NFSCL_REQSTART(nd, NFSPROC_CREATE, dvp);
1978 * For V4, this is actually an Open op.
1980 NFSM_BUILD(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
1981 *tl++ = txdr_unsigned(owp->nfsow_seqid);
1982 *tl++ = txdr_unsigned(NFSV4OPEN_ACCESSWRITE |
1983 NFSV4OPEN_ACCESSREAD);
1984 *tl++ = txdr_unsigned(NFSV4OPEN_DENYNONE);
1985 *tl++ = NFSMNT_MDSSESSION(nmp)->nfsess_clientid.lval[0];
1986 *tl = NFSMNT_MDSSESSION(nmp)->nfsess_clientid.lval[1];
1987 (void) nfsm_strtom(nd, owp->nfsow_owner, NFSV4CL_LOCKNAMELEN);
1988 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1989 *tl++ = txdr_unsigned(NFSV4OPEN_CREATE);
1990 if (fmode & O_EXCL) {
1991 if (NFSHASNFSV4N(nmp)) {
1992 if (NFSHASSESSPERSIST(nmp)) {
1993 /* Use GUARDED for persistent sessions. */
1994 *tl = txdr_unsigned(NFSCREATE_GUARDED);
1995 nfscl_fillsattr(nd, vap, dvp, 0, 0);
1997 /* Otherwise, use EXCLUSIVE4_1. */
1998 *tl = txdr_unsigned(NFSCREATE_EXCLUSIVE41);
1999 NFSM_BUILD(tl, u_int32_t *, NFSX_VERF);
2000 *tl++ = cverf.lval[0];
2001 *tl = cverf.lval[1];
2002 nfscl_fillsattr(nd, vap, dvp, 0, 0);
2006 *tl = txdr_unsigned(NFSCREATE_EXCLUSIVE);
2007 NFSM_BUILD(tl, u_int32_t *, NFSX_VERF);
2008 *tl++ = cverf.lval[0];
2009 *tl = cverf.lval[1];
2012 *tl = txdr_unsigned(NFSCREATE_UNCHECKED);
2013 nfscl_fillsattr(nd, vap, dvp, 0, 0);
2015 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2016 *tl = txdr_unsigned(NFSV4OPEN_CLAIMNULL);
2017 (void) nfsm_strtom(nd, name, namelen);
2018 /* Get the new file's handle and attributes. */
2019 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2020 *tl++ = txdr_unsigned(NFSV4OP_GETFH);
2021 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2022 NFSGETATTR_ATTRBIT(&attrbits);
2023 (void) nfsrv_putattrbit(nd, &attrbits);
2024 /* Get the directory's post-op attributes. */
2025 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2026 *tl = txdr_unsigned(NFSV4OP_PUTFH);
2027 (void) nfsm_fhtom(nd, np->n_fhp->nfh_fh, np->n_fhp->nfh_len, 0);
2028 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2029 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2030 (void) nfsrv_putattrbit(nd, &attrbits);
2031 error = nfscl_request(nd, dvp, p, cred, dstuff);
2034 NFSCL_INCRSEQID(owp->nfsow_seqid, nd);
2035 if (nd->nd_repstat == 0) {
2036 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
2038 stateid.seqid = *tl++;
2039 stateid.other[0] = *tl++;
2040 stateid.other[1] = *tl++;
2041 stateid.other[2] = *tl;
2042 rflags = fxdr_unsigned(u_int32_t, *(tl + 6));
2043 (void) nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
2044 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2045 deleg = fxdr_unsigned(int, *tl);
2046 if (deleg == NFSV4OPEN_DELEGATEREAD ||
2047 deleg == NFSV4OPEN_DELEGATEWRITE) {
2048 if (!(owp->nfsow_clp->nfsc_flags &
2049 NFSCLFLAGS_FIRSTDELEG))
2050 owp->nfsow_clp->nfsc_flags |=
2051 (NFSCLFLAGS_FIRSTDELEG | NFSCLFLAGS_GOTDELEG);
2052 MALLOC(dp, struct nfscldeleg *,
2053 sizeof (struct nfscldeleg) + NFSX_V4FHMAX,
2054 M_NFSCLDELEG, M_WAITOK);
2055 LIST_INIT(&dp->nfsdl_owner);
2056 LIST_INIT(&dp->nfsdl_lock);
2057 dp->nfsdl_clp = owp->nfsow_clp;
2058 newnfs_copyincred(cred, &dp->nfsdl_cred);
2059 nfscl_lockinit(&dp->nfsdl_rwlock);
2060 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
2062 dp->nfsdl_stateid.seqid = *tl++;
2063 dp->nfsdl_stateid.other[0] = *tl++;
2064 dp->nfsdl_stateid.other[1] = *tl++;
2065 dp->nfsdl_stateid.other[2] = *tl++;
2066 ret = fxdr_unsigned(int, *tl);
2067 if (deleg == NFSV4OPEN_DELEGATEWRITE) {
2068 dp->nfsdl_flags = NFSCLDL_WRITE;
2070 * Indicates how much the file can grow.
2072 NFSM_DISSECT(tl, u_int32_t *,
2074 limitby = fxdr_unsigned(int, *tl++);
2076 case NFSV4OPEN_LIMITSIZE:
2077 dp->nfsdl_sizelimit = fxdr_hyper(tl);
2079 case NFSV4OPEN_LIMITBLOCKS:
2080 dp->nfsdl_sizelimit =
2081 fxdr_unsigned(u_int64_t, *tl++);
2082 dp->nfsdl_sizelimit *=
2083 fxdr_unsigned(u_int64_t, *tl);
2086 error = NFSERR_BADXDR;
2090 dp->nfsdl_flags = NFSCLDL_READ;
2093 dp->nfsdl_flags |= NFSCLDL_RECALL;
2094 error = nfsrv_dissectace(nd, &dp->nfsdl_ace, &ret,
2098 } else if (deleg != NFSV4OPEN_DELEGATENONE) {
2099 error = NFSERR_BADXDR;
2102 error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp);
2105 /* Get rid of the PutFH and Getattr status values. */
2106 NFSM_DISSECT(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
2107 /* Load the directory attributes. */
2108 error = nfsm_loadattr(nd, dnap);
2112 if (dp != NULL && *attrflagp) {
2113 dp->nfsdl_change = nnap->na_filerev;
2114 dp->nfsdl_modtime = nnap->na_mtime;
2115 dp->nfsdl_flags |= NFSCLDL_MODTIMESET;
2118 * We can now complete the Open state.
2122 dp->nfsdl_fhlen = nfhp->nfh_len;
2123 NFSBCOPY(nfhp->nfh_fh, dp->nfsdl_fh, nfhp->nfh_len);
2126 * Get an Open structure that will be
2127 * attached to the OpenOwner, acquired already.
2129 error = nfscl_open(dvp, nfhp->nfh_fh, nfhp->nfh_len,
2130 (NFSV4OPEN_ACCESSWRITE | NFSV4OPEN_ACCESSREAD), 0,
2131 cred, p, NULL, &op, &newone, NULL, 0);
2134 op->nfso_stateid = stateid;
2135 newnfs_copyincred(cred, &op->nfso_cred);
2136 if ((rflags & NFSV4OPEN_RESULTCONFIRM)) {
2138 ret = nfsrpc_openconfirm(dvp, nfhp->nfh_fh,
2139 nfhp->nfh_len, op, cred, p);
2140 if (ret == NFSERR_DELAY)
2141 (void) nfs_catnap(PZERO, ret, "nfs_create");
2142 } while (ret == NFSERR_DELAY);
2147 * If the server is handing out delegations, but we didn't
2148 * get one because an OpenConfirm was required, try the
2149 * Open again, to get a delegation. This is a harmless no-op,
2150 * from a server's point of view.
2152 if ((rflags & NFSV4OPEN_RESULTCONFIRM) &&
2153 (owp->nfsow_clp->nfsc_flags & NFSCLFLAGS_GOTDELEG) &&
2154 !error && dp == NULL) {
2156 ret = nfsrpc_openrpc(VFSTONFS(vnode_mount(dvp)), dvp,
2157 np->n_fhp->nfh_fh, np->n_fhp->nfh_len,
2158 nfhp->nfh_fh, nfhp->nfh_len,
2159 (NFSV4OPEN_ACCESSWRITE | NFSV4OPEN_ACCESSREAD), op,
2160 name, namelen, &dp, 0, 0x0, cred, p, 0, 1);
2161 if (ret == NFSERR_DELAY)
2162 (void) nfs_catnap(PZERO, ret, "nfs_crt2");
2163 } while (ret == NFSERR_DELAY);
2166 FREE((caddr_t)dp, M_NFSCLDELEG);
2169 if (ret == NFSERR_STALECLIENTID ||
2170 ret == NFSERR_STALEDONTRECOVER ||
2171 ret == NFSERR_BADSESSION)
2175 nfscl_openrelease(op, error, newone);
2178 if (nd->nd_repstat != 0 && error == 0)
2179 error = nd->nd_repstat;
2180 if (error == NFSERR_STALECLIENTID || error == NFSERR_BADSESSION)
2181 nfscl_initiate_recovery(owp->nfsow_clp);
2185 else if (dp != NULL)
2186 FREE((caddr_t)dp, M_NFSCLDELEG);
2187 mbuf_freem(nd->nd_mrep);
2195 nfsrpc_remove(vnode_t dvp, char *name, int namelen, vnode_t vp,
2196 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap, int *dattrflagp,
2200 struct nfsrv_descript nfsd, *nd = &nfsd;
2202 struct nfsmount *nmp;
2203 nfsv4stateid_t dstateid;
2204 int error, ret = 0, i;
2207 if (namelen > NFS_MAXNAMLEN)
2208 return (ENAMETOOLONG);
2209 nmp = VFSTONFS(vnode_mount(dvp));
2211 if (NFSHASNFSV4(nmp) && ret == 0) {
2212 ret = nfscl_removedeleg(vp, p, &dstateid);
2214 NFSCL_REQSTART(nd, NFSPROC_RETDELEGREMOVE, vp);
2215 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID +
2217 if (NFSHASNFSV4N(nmp))
2220 *tl++ = dstateid.seqid;
2221 *tl++ = dstateid.other[0];
2222 *tl++ = dstateid.other[1];
2223 *tl++ = dstateid.other[2];
2224 *tl = txdr_unsigned(NFSV4OP_PUTFH);
2226 (void) nfsm_fhtom(nd, np->n_fhp->nfh_fh,
2227 np->n_fhp->nfh_len, 0);
2228 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2229 *tl = txdr_unsigned(NFSV4OP_REMOVE);
2235 NFSCL_REQSTART(nd, NFSPROC_REMOVE, dvp);
2236 (void) nfsm_strtom(nd, name, namelen);
2237 error = nfscl_request(nd, dvp, p, cred, dstuff);
2240 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) {
2241 /* For NFSv4, parse out any Delereturn replies. */
2242 if (ret > 0 && nd->nd_repstat != 0 &&
2243 (nd->nd_flag & ND_NOMOREDATA)) {
2245 * If the Delegreturn failed, try again without
2246 * it. The server will Recall, as required.
2248 mbuf_freem(nd->nd_mrep);
2251 for (i = 0; i < (ret * 2); i++) {
2252 if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) ==
2254 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2256 nd->nd_flag |= ND_NOMOREDATA;
2259 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
2261 if (nd->nd_repstat && !error)
2262 error = nd->nd_repstat;
2264 mbuf_freem(nd->nd_mrep);
2269 * Do an nfs rename rpc.
2272 nfsrpc_rename(vnode_t fdvp, vnode_t fvp, char *fnameptr, int fnamelen,
2273 vnode_t tdvp, vnode_t tvp, char *tnameptr, int tnamelen, struct ucred *cred,
2274 NFSPROC_T *p, struct nfsvattr *fnap, struct nfsvattr *tnap,
2275 int *fattrflagp, int *tattrflagp, void *fstuff, void *tstuff)
2278 struct nfsrv_descript nfsd, *nd = &nfsd;
2279 struct nfsmount *nmp;
2281 nfsattrbit_t attrbits;
2282 nfsv4stateid_t fdstateid, tdstateid;
2283 int error = 0, ret = 0, gottd = 0, gotfd = 0, i;
2287 nmp = VFSTONFS(vnode_mount(fdvp));
2288 if (fnamelen > NFS_MAXNAMLEN || tnamelen > NFS_MAXNAMLEN)
2289 return (ENAMETOOLONG);
2291 if (NFSHASNFSV4(nmp) && ret == 0) {
2292 ret = nfscl_renamedeleg(fvp, &fdstateid, &gotfd, tvp,
2293 &tdstateid, &gottd, p);
2294 if (gotfd && gottd) {
2295 NFSCL_REQSTART(nd, NFSPROC_RETDELEGRENAME2, fvp);
2297 NFSCL_REQSTART(nd, NFSPROC_RETDELEGRENAME1, fvp);
2299 NFSCL_REQSTART(nd, NFSPROC_RETDELEGRENAME1, tvp);
2302 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID);
2303 if (NFSHASNFSV4N(nmp))
2306 *tl++ = fdstateid.seqid;
2307 *tl++ = fdstateid.other[0];
2308 *tl++ = fdstateid.other[1];
2309 *tl = fdstateid.other[2];
2311 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2312 *tl = txdr_unsigned(NFSV4OP_PUTFH);
2314 (void) nfsm_fhtom(nd, np->n_fhp->nfh_fh,
2315 np->n_fhp->nfh_len, 0);
2316 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2317 *tl = txdr_unsigned(NFSV4OP_DELEGRETURN);
2321 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID);
2322 if (NFSHASNFSV4N(nmp))
2325 *tl++ = tdstateid.seqid;
2326 *tl++ = tdstateid.other[0];
2327 *tl++ = tdstateid.other[1];
2328 *tl = tdstateid.other[2];
2331 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2332 *tl = txdr_unsigned(NFSV4OP_PUTFH);
2334 (void) nfsm_fhtom(nd, np->n_fhp->nfh_fh,
2335 np->n_fhp->nfh_len, 0);
2336 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2337 *tl = txdr_unsigned(NFSV4OP_SAVEFH);
2343 NFSCL_REQSTART(nd, NFSPROC_RENAME, fdvp);
2344 if (nd->nd_flag & ND_NFSV4) {
2345 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2346 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2347 NFSWCCATTR_ATTRBIT(&attrbits);
2348 (void) nfsrv_putattrbit(nd, &attrbits);
2349 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2350 *tl = txdr_unsigned(NFSV4OP_PUTFH);
2351 (void) nfsm_fhtom(nd, VTONFS(tdvp)->n_fhp->nfh_fh,
2352 VTONFS(tdvp)->n_fhp->nfh_len, 0);
2353 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2354 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2355 (void) nfsrv_putattrbit(nd, &attrbits);
2356 nd->nd_flag |= ND_V4WCCATTR;
2357 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2358 *tl = txdr_unsigned(NFSV4OP_RENAME);
2360 (void) nfsm_strtom(nd, fnameptr, fnamelen);
2361 if (!(nd->nd_flag & ND_NFSV4))
2362 (void) nfsm_fhtom(nd, VTONFS(tdvp)->n_fhp->nfh_fh,
2363 VTONFS(tdvp)->n_fhp->nfh_len, 0);
2364 (void) nfsm_strtom(nd, tnameptr, tnamelen);
2365 error = nfscl_request(nd, fdvp, p, cred, fstuff);
2368 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) {
2369 /* For NFSv4, parse out any Delereturn replies. */
2370 if (ret > 0 && nd->nd_repstat != 0 &&
2371 (nd->nd_flag & ND_NOMOREDATA)) {
2373 * If the Delegreturn failed, try again without
2374 * it. The server will Recall, as required.
2376 mbuf_freem(nd->nd_mrep);
2379 for (i = 0; i < (ret * 2); i++) {
2380 if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) ==
2382 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2384 if (i == 0 && ret > 1) {
2386 * If the Delegreturn failed, try again
2387 * without it. The server will Recall, as
2389 * If ret > 1, the first iteration of this
2390 * loop is the second DelegReturn result.
2392 mbuf_freem(nd->nd_mrep);
2395 nd->nd_flag |= ND_NOMOREDATA;
2400 /* Now, the first wcc attribute reply. */
2401 if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) == ND_NFSV4) {
2402 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2404 nd->nd_flag |= ND_NOMOREDATA;
2406 error = nfscl_wcc_data(nd, fdvp, fnap, fattrflagp, NULL,
2408 /* and the second wcc attribute reply. */
2409 if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) == ND_NFSV4 &&
2411 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2413 nd->nd_flag |= ND_NOMOREDATA;
2416 error = nfscl_wcc_data(nd, tdvp, tnap, tattrflagp,
2419 if (nd->nd_repstat && !error)
2420 error = nd->nd_repstat;
2422 mbuf_freem(nd->nd_mrep);
2427 * nfs hard link create rpc
2430 nfsrpc_link(vnode_t dvp, vnode_t vp, char *name, int namelen,
2431 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap,
2432 struct nfsvattr *nap, int *attrflagp, int *dattrflagp, void *dstuff)
2435 struct nfsrv_descript nfsd, *nd = &nfsd;
2436 nfsattrbit_t attrbits;
2441 if (namelen > NFS_MAXNAMLEN)
2442 return (ENAMETOOLONG);
2443 NFSCL_REQSTART(nd, NFSPROC_LINK, vp);
2444 if (nd->nd_flag & ND_NFSV4) {
2445 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2446 *tl = txdr_unsigned(NFSV4OP_PUTFH);
2448 (void) nfsm_fhtom(nd, VTONFS(dvp)->n_fhp->nfh_fh,
2449 VTONFS(dvp)->n_fhp->nfh_len, 0);
2450 if (nd->nd_flag & ND_NFSV4) {
2451 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2452 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2453 NFSWCCATTR_ATTRBIT(&attrbits);
2454 (void) nfsrv_putattrbit(nd, &attrbits);
2455 nd->nd_flag |= ND_V4WCCATTR;
2456 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2457 *tl = txdr_unsigned(NFSV4OP_LINK);
2459 (void) nfsm_strtom(nd, name, namelen);
2460 error = nfscl_request(nd, vp, p, cred, dstuff);
2463 if (nd->nd_flag & ND_NFSV3) {
2464 error = nfscl_postop_attr(nd, nap, attrflagp, dstuff);
2466 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp,
2468 } else if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) == ND_NFSV4) {
2470 * First, parse out the PutFH and Getattr result.
2472 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2474 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2476 nd->nd_flag |= ND_NOMOREDATA;
2478 * Get the pre-op attributes.
2480 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
2482 if (nd->nd_repstat && !error)
2483 error = nd->nd_repstat;
2485 mbuf_freem(nd->nd_mrep);
2490 * nfs symbolic link create rpc
2493 nfsrpc_symlink(vnode_t dvp, char *name, int namelen, char *target,
2494 struct vattr *vap, struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap,
2495 struct nfsvattr *nnap, struct nfsfh **nfhpp, int *attrflagp,
2496 int *dattrflagp, void *dstuff)
2499 struct nfsrv_descript nfsd, *nd = &nfsd;
2500 struct nfsmount *nmp;
2501 int slen, error = 0;
2506 nmp = VFSTONFS(vnode_mount(dvp));
2507 slen = strlen(target);
2508 if (slen > NFS_MAXPATHLEN || namelen > NFS_MAXNAMLEN)
2509 return (ENAMETOOLONG);
2510 NFSCL_REQSTART(nd, NFSPROC_SYMLINK, dvp);
2511 if (nd->nd_flag & ND_NFSV4) {
2512 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2513 *tl = txdr_unsigned(NFLNK);
2514 (void) nfsm_strtom(nd, target, slen);
2516 (void) nfsm_strtom(nd, name, namelen);
2517 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4))
2518 nfscl_fillsattr(nd, vap, dvp, 0, 0);
2519 if (!(nd->nd_flag & ND_NFSV4))
2520 (void) nfsm_strtom(nd, target, slen);
2521 if (nd->nd_flag & ND_NFSV2)
2522 nfscl_fillsattr(nd, vap, dvp, NFSSATTR_SIZENEG1, 0);
2523 error = nfscl_request(nd, dvp, p, cred, dstuff);
2526 if (nd->nd_flag & ND_NFSV4)
2527 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
2528 if ((nd->nd_flag & ND_NFSV3) && !error) {
2529 if (!nd->nd_repstat)
2530 error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp);
2532 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp,
2535 if (nd->nd_repstat && !error)
2536 error = nd->nd_repstat;
2537 mbuf_freem(nd->nd_mrep);
2539 * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry.
2540 * Only do this if vfs.nfs.ignore_eexist is set.
2541 * Never do this for NFSv4.1 or later minor versions, since sessions
2542 * should guarantee "exactly once" RPC semantics.
2544 if (error == EEXIST && nfsignore_eexist != 0 && (!NFSHASNFSV4(nmp) ||
2545 nmp->nm_minorvers == 0))
2554 nfsrpc_mkdir(vnode_t dvp, char *name, int namelen, struct vattr *vap,
2555 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap,
2556 struct nfsvattr *nnap, struct nfsfh **nfhpp, int *attrflagp,
2557 int *dattrflagp, void *dstuff)
2560 struct nfsrv_descript nfsd, *nd = &nfsd;
2561 nfsattrbit_t attrbits;
2564 struct nfsmount *nmp;
2569 nmp = VFSTONFS(vnode_mount(dvp));
2570 fhp = VTONFS(dvp)->n_fhp;
2571 if (namelen > NFS_MAXNAMLEN)
2572 return (ENAMETOOLONG);
2573 NFSCL_REQSTART(nd, NFSPROC_MKDIR, dvp);
2574 if (nd->nd_flag & ND_NFSV4) {
2575 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2576 *tl = txdr_unsigned(NFDIR);
2578 (void) nfsm_strtom(nd, name, namelen);
2579 nfscl_fillsattr(nd, vap, dvp, NFSSATTR_SIZENEG1, 0);
2580 if (nd->nd_flag & ND_NFSV4) {
2581 NFSGETATTR_ATTRBIT(&attrbits);
2582 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2583 *tl++ = txdr_unsigned(NFSV4OP_GETFH);
2584 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2585 (void) nfsrv_putattrbit(nd, &attrbits);
2586 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2587 *tl = txdr_unsigned(NFSV4OP_PUTFH);
2588 (void) nfsm_fhtom(nd, fhp->nfh_fh, fhp->nfh_len, 0);
2589 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2590 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2591 (void) nfsrv_putattrbit(nd, &attrbits);
2593 error = nfscl_request(nd, dvp, p, cred, dstuff);
2596 if (nd->nd_flag & ND_NFSV4)
2597 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
2598 if (!nd->nd_repstat && !error) {
2599 if (nd->nd_flag & ND_NFSV4) {
2600 NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
2601 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
2604 error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp);
2605 if (error == 0 && (nd->nd_flag & ND_NFSV4) != 0) {
2606 /* Get rid of the PutFH and Getattr status values. */
2607 NFSM_DISSECT(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
2608 /* Load the directory attributes. */
2609 error = nfsm_loadattr(nd, dnap);
2614 if ((nd->nd_flag & ND_NFSV3) && !error)
2615 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
2616 if (nd->nd_repstat && !error)
2617 error = nd->nd_repstat;
2619 mbuf_freem(nd->nd_mrep);
2621 * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry.
2622 * Only do this if vfs.nfs.ignore_eexist is set.
2623 * Never do this for NFSv4.1 or later minor versions, since sessions
2624 * should guarantee "exactly once" RPC semantics.
2626 if (error == EEXIST && nfsignore_eexist != 0 && (!NFSHASNFSV4(nmp) ||
2627 nmp->nm_minorvers == 0))
2633 * nfs remove directory call
2636 nfsrpc_rmdir(vnode_t dvp, char *name, int namelen, struct ucred *cred,
2637 NFSPROC_T *p, struct nfsvattr *dnap, int *dattrflagp, void *dstuff)
2639 struct nfsrv_descript nfsd, *nd = &nfsd;
2643 if (namelen > NFS_MAXNAMLEN)
2644 return (ENAMETOOLONG);
2645 NFSCL_REQSTART(nd, NFSPROC_RMDIR, dvp);
2646 (void) nfsm_strtom(nd, name, namelen);
2647 error = nfscl_request(nd, dvp, p, cred, dstuff);
2650 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4))
2651 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
2652 if (nd->nd_repstat && !error)
2653 error = nd->nd_repstat;
2654 mbuf_freem(nd->nd_mrep);
2656 * Kludge: Map ENOENT => 0 assuming that you have a reply to a retry.
2658 if (error == ENOENT)
2665 * Always returns with either uio_resid unchanged, if you are at the
2666 * end of the directory, or uio_resid == 0, with all DIRBLKSIZ chunks
2668 * I felt this would allow caching of directory blocks more easily
2669 * than returning a pertially filled block.
2670 * Directory offset cookies:
2671 * Oh my, what to do with them...
2672 * I can think of three ways to deal with them:
2673 * 1 - have the layer above these RPCs maintain a map between logical
2674 * directory byte offsets and the NFS directory offset cookies
2675 * 2 - pass the opaque directory offset cookies up into userland
2676 * and let the libc functions deal with them, via the system call
2677 * 3 - return them to userland in the "struct dirent", so future versions
2678 * of libc can use them and do whatever is necessary to amke things work
2679 * above these rpc calls, in the meantime
2680 * For now, I do #3 by "hiding" the directory offset cookies after the
2681 * d_name field in struct dirent. This is space inside d_reclen that
2682 * will be ignored by anything that doesn't know about them.
2683 * The directory offset cookies are filled in as the last 8 bytes of
2684 * each directory entry, after d_name. Someday, the userland libc
2685 * functions may be able to use these. In the meantime, it satisfies
2686 * OpenBSD's requirements for cookies being returned.
2687 * If expects the directory offset cookie for the read to be in uio_offset
2688 * and returns the one for the next entry after this directory block in
2692 nfsrpc_readdir(vnode_t vp, struct uio *uiop, nfsuint64 *cookiep,
2693 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp,
2694 int *eofp, void *stuff)
2697 struct dirent *dp = NULL;
2699 nfsquad_t cookie, ncookie;
2700 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
2701 struct nfsnode *dnp = VTONFS(vp);
2702 struct nfsvattr nfsva;
2703 struct nfsrv_descript nfsd, *nd = &nfsd;
2704 int error = 0, tlen, more_dirs = 1, blksiz = 0, bigenough = 1;
2705 int reqsize, tryformoredirs = 1, readsize, eof = 0, gotmnton = 0;
2706 long dotfileid, dotdotfileid = 0;
2707 u_int32_t fakefileno = 0xffffffff, rderr;
2709 nfsattrbit_t attrbits, dattrbits;
2710 u_int32_t *tl2 = NULL;
2713 KASSERT(uiop->uio_iovcnt == 1 &&
2714 (uio_uio_resid(uiop) & (DIRBLKSIZ - 1)) == 0,
2715 ("nfs readdirrpc bad uio"));
2718 * There is no point in reading a lot more than uio_resid, however
2719 * adding one additional DIRBLKSIZ makes sense. Since uio_resid
2720 * and nm_readdirsize are both exact multiples of DIRBLKSIZ, this
2721 * will never make readsize > nm_readdirsize.
2723 readsize = nmp->nm_readdirsize;
2724 if (readsize > uio_uio_resid(uiop))
2725 readsize = uio_uio_resid(uiop) + DIRBLKSIZ;
2730 tresid = uio_uio_resid(uiop);
2731 cookie.lval[0] = cookiep->nfsuquad[0];
2732 cookie.lval[1] = cookiep->nfsuquad[1];
2736 * For NFSv4, first create the "." and ".." entries.
2738 if (NFSHASNFSV4(nmp)) {
2739 reqsize = 6 * NFSX_UNSIGNED;
2740 NFSGETATTR_ATTRBIT(&dattrbits);
2741 NFSZERO_ATTRBIT(&attrbits);
2742 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_FILEID);
2743 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TYPE);
2744 if (NFSISSET_ATTRBIT(&dnp->n_vattr.na_suppattr,
2745 NFSATTRBIT_MOUNTEDONFILEID)) {
2746 NFSSETBIT_ATTRBIT(&attrbits,
2747 NFSATTRBIT_MOUNTEDONFILEID);
2751 * Must fake it. Use the fileno, except when the
2752 * fsid is != to that of the directory. For that
2753 * case, generate a fake fileno that is not the same.
2755 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_FSID);
2760 * Joy, oh joy. For V4 we get to hand craft '.' and '..'.
2762 if (uiop->uio_offset == 0) {
2763 NFSCL_REQSTART(nd, NFSPROC_LOOKUPP, vp);
2764 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2765 *tl++ = txdr_unsigned(NFSV4OP_GETFH);
2766 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2767 (void) nfsrv_putattrbit(nd, &attrbits);
2768 error = nfscl_request(nd, vp, p, cred, stuff);
2771 dotfileid = 0; /* Fake out the compiler. */
2772 if ((nd->nd_flag & ND_NOMOREDATA) == 0) {
2773 error = nfsm_loadattr(nd, &nfsva);
2776 dotfileid = nfsva.na_fileid;
2778 if (nd->nd_repstat == 0) {
2779 NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
2780 len = fxdr_unsigned(int, *(tl + 4));
2781 if (len > 0 && len <= NFSX_V4FHMAX)
2782 error = nfsm_advance(nd, NFSM_RNDUP(len), -1);
2786 NFSM_DISSECT(tl, u_int32_t *, 2*NFSX_UNSIGNED);
2787 nfsva.na_mntonfileno = 0xffffffff;
2788 error = nfsv4_loadattr(nd, NULL, &nfsva, NULL,
2789 NULL, 0, NULL, NULL, NULL, NULL, NULL, 0,
2790 NULL, NULL, NULL, p, cred);
2792 dotdotfileid = dotfileid;
2793 } else if (gotmnton) {
2794 if (nfsva.na_mntonfileno != 0xffffffff)
2795 dotdotfileid = nfsva.na_mntonfileno;
2797 dotdotfileid = nfsva.na_fileid;
2798 } else if (nfsva.na_filesid[0] ==
2799 dnp->n_vattr.na_filesid[0] &&
2800 nfsva.na_filesid[1] ==
2801 dnp->n_vattr.na_filesid[1]) {
2802 dotdotfileid = nfsva.na_fileid;
2806 } while (fakefileno ==
2808 dotdotfileid = fakefileno;
2811 } else if (nd->nd_repstat == NFSERR_NOENT) {
2813 * Lookupp returns NFSERR_NOENT when we are
2814 * at the root, so just use the current dir.
2817 dotdotfileid = dotfileid;
2819 error = nd->nd_repstat;
2821 mbuf_freem(nd->nd_mrep);
2825 dp = (struct dirent *) CAST_DOWN(caddr_t, uio_iov_base(uiop));
2826 dp->d_type = DT_DIR;
2827 dp->d_fileno = dotfileid;
2829 dp->d_name[0] = '.';
2830 dp->d_name[1] = '\0';
2831 dp->d_reclen = DIRENT_SIZE(dp) + NFSX_HYPER;
2833 * Just make these offset cookie 0.
2835 tl = (u_int32_t *)&dp->d_name[4];
2838 blksiz += dp->d_reclen;
2839 uio_uio_resid_add(uiop, -(dp->d_reclen));
2840 uiop->uio_offset += dp->d_reclen;
2841 uio_iov_base_add(uiop, dp->d_reclen);
2842 uio_iov_len_add(uiop, -(dp->d_reclen));
2843 dp = (struct dirent *) CAST_DOWN(caddr_t, uio_iov_base(uiop));
2844 dp->d_type = DT_DIR;
2845 dp->d_fileno = dotdotfileid;
2847 dp->d_name[0] = '.';
2848 dp->d_name[1] = '.';
2849 dp->d_name[2] = '\0';
2850 dp->d_reclen = DIRENT_SIZE(dp) + NFSX_HYPER;
2852 * Just make these offset cookie 0.
2854 tl = (u_int32_t *)&dp->d_name[4];
2857 blksiz += dp->d_reclen;
2858 uio_uio_resid_add(uiop, -(dp->d_reclen));
2859 uiop->uio_offset += dp->d_reclen;
2860 uio_iov_base_add(uiop, dp->d_reclen);
2861 uio_iov_len_add(uiop, -(dp->d_reclen));
2863 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_RDATTRERROR);
2865 reqsize = 5 * NFSX_UNSIGNED;
2870 * Loop around doing readdir rpc's of size readsize.
2871 * The stopping criteria is EOF or buffer full.
2873 while (more_dirs && bigenough) {
2875 NFSCL_REQSTART(nd, NFSPROC_READDIR, vp);
2876 if (nd->nd_flag & ND_NFSV2) {
2877 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2878 *tl++ = cookie.lval[1];
2879 *tl = txdr_unsigned(readsize);
2881 NFSM_BUILD(tl, u_int32_t *, reqsize);
2882 *tl++ = cookie.lval[0];
2883 *tl++ = cookie.lval[1];
2884 if (cookie.qval == 0) {
2889 *tl++ = dnp->n_cookieverf.nfsuquad[0];
2890 *tl++ = dnp->n_cookieverf.nfsuquad[1];
2893 if (nd->nd_flag & ND_NFSV4) {
2894 *tl++ = txdr_unsigned(readsize);
2895 *tl = txdr_unsigned(readsize);
2896 (void) nfsrv_putattrbit(nd, &attrbits);
2897 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2898 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2899 (void) nfsrv_putattrbit(nd, &dattrbits);
2901 *tl = txdr_unsigned(readsize);
2904 error = nfscl_request(nd, vp, p, cred, stuff);
2907 if (!(nd->nd_flag & ND_NFSV2)) {
2908 if (nd->nd_flag & ND_NFSV3)
2909 error = nfscl_postop_attr(nd, nap, attrflagp,
2911 if (!nd->nd_repstat && !error) {
2912 NFSM_DISSECT(tl, u_int32_t *, NFSX_HYPER);
2914 dnp->n_cookieverf.nfsuquad[0] = *tl++;
2915 dnp->n_cookieverf.nfsuquad[1] = *tl;
2919 if (nd->nd_repstat || error) {
2921 error = nd->nd_repstat;
2924 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2925 more_dirs = fxdr_unsigned(int, *tl);
2929 /* loop thru the dir entries, doctoring them to 4bsd form */
2930 while (more_dirs && bigenough) {
2931 if (nd->nd_flag & ND_NFSV4) {
2932 NFSM_DISSECT(tl, u_int32_t *, 3*NFSX_UNSIGNED);
2933 ncookie.lval[0] = *tl++;
2934 ncookie.lval[1] = *tl++;
2935 len = fxdr_unsigned(int, *tl);
2936 } else if (nd->nd_flag & ND_NFSV3) {
2937 NFSM_DISSECT(tl, u_int32_t *, 3*NFSX_UNSIGNED);
2938 nfsva.na_fileid = fxdr_hyper(tl);
2940 len = fxdr_unsigned(int, *tl);
2942 NFSM_DISSECT(tl, u_int32_t *, 2*NFSX_UNSIGNED);
2944 fxdr_unsigned(long, *tl++);
2945 len = fxdr_unsigned(int, *tl);
2947 if (len <= 0 || len > NFS_MAXNAMLEN) {
2951 tlen = NFSM_RNDUP(len);
2953 tlen += 4; /* To ensure null termination */
2954 left = DIRBLKSIZ - blksiz;
2955 if ((int)(tlen + DIRHDSIZ + NFSX_HYPER) > left) {
2956 dp->d_reclen += left;
2957 uio_iov_base_add(uiop, left);
2958 uio_iov_len_add(uiop, -(left));
2959 uio_uio_resid_add(uiop, -(left));
2960 uiop->uio_offset += left;
2963 if ((int)(tlen + DIRHDSIZ + NFSX_HYPER) > uio_uio_resid(uiop))
2966 dp = (struct dirent *) CAST_DOWN(caddr_t, uio_iov_base(uiop));
2968 dp->d_reclen = tlen + DIRHDSIZ + NFSX_HYPER;
2969 dp->d_type = DT_UNKNOWN;
2970 blksiz += dp->d_reclen;
2971 if (blksiz == DIRBLKSIZ)
2973 uio_uio_resid_add(uiop, -(DIRHDSIZ));
2974 uiop->uio_offset += DIRHDSIZ;
2975 uio_iov_base_add(uiop, DIRHDSIZ);
2976 uio_iov_len_add(uiop, -(DIRHDSIZ));
2977 error = nfsm_mbufuio(nd, uiop, len);
2980 cp = CAST_DOWN(caddr_t, uio_iov_base(uiop));
2982 *cp = '\0'; /* null terminate */
2983 cp += tlen; /* points to cookie storage */
2984 tl2 = (u_int32_t *)cp;
2985 uio_iov_base_add(uiop, (tlen + NFSX_HYPER));
2986 uio_iov_len_add(uiop, -(tlen + NFSX_HYPER));
2987 uio_uio_resid_add(uiop, -(tlen + NFSX_HYPER));
2988 uiop->uio_offset += (tlen + NFSX_HYPER);
2990 error = nfsm_advance(nd, NFSM_RNDUP(len), -1);
2994 if (nd->nd_flag & ND_NFSV4) {
2996 nfsva.na_mntonfileno = 0xffffffff;
2997 error = nfsv4_loadattr(nd, NULL, &nfsva, NULL,
2998 NULL, 0, NULL, NULL, NULL, NULL, NULL, 0,
2999 NULL, NULL, &rderr, p, cred);
3002 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3003 } else if (nd->nd_flag & ND_NFSV3) {
3004 NFSM_DISSECT(tl, u_int32_t *, 3*NFSX_UNSIGNED);
3005 ncookie.lval[0] = *tl++;
3006 ncookie.lval[1] = *tl++;
3008 NFSM_DISSECT(tl, u_int32_t *, 2*NFSX_UNSIGNED);
3009 ncookie.lval[0] = 0;
3010 ncookie.lval[1] = *tl++;
3013 if (nd->nd_flag & ND_NFSV4) {
3018 if (nfsva.na_mntonfileno != 0xffffffff)
3019 dp->d_fileno = nfsva.na_mntonfileno;
3021 dp->d_fileno = nfsva.na_fileid;
3022 } else if (nfsva.na_filesid[0] ==
3023 dnp->n_vattr.na_filesid[0] &&
3024 nfsva.na_filesid[1] ==
3025 dnp->n_vattr.na_filesid[1]) {
3026 dp->d_fileno = nfsva.na_fileid;
3030 } while (fakefileno ==
3032 dp->d_fileno = fakefileno;
3034 dp->d_type = vtonfs_dtype(nfsva.na_type);
3037 dp->d_fileno = nfsva.na_fileid;
3039 *tl2++ = cookiep->nfsuquad[0] = cookie.lval[0] =
3041 *tl2 = cookiep->nfsuquad[1] = cookie.lval[1] =
3044 more_dirs = fxdr_unsigned(int, *tl);
3047 * If at end of rpc data, get the eof boolean
3050 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3051 eof = fxdr_unsigned(int, *tl);
3054 if (nd->nd_flag & ND_NFSV4) {
3055 error = nfscl_postop_attr(nd, nap, attrflagp,
3061 mbuf_freem(nd->nd_mrep);
3065 * Fill last record, iff any, out to a multiple of DIRBLKSIZ
3066 * by increasing d_reclen for the last record.
3069 left = DIRBLKSIZ - blksiz;
3070 dp->d_reclen += left;
3071 uio_iov_base_add(uiop, left);
3072 uio_iov_len_add(uiop, -(left));
3073 uio_uio_resid_add(uiop, -(left));
3074 uiop->uio_offset += left;
3078 * If returning no data, assume end of file.
3079 * If not bigenough, return not end of file, since you aren't
3080 * returning all the data
3081 * Otherwise, return the eof flag from the server.
3084 if (tresid == ((size_t)(uio_uio_resid(uiop))))
3086 else if (!bigenough)
3093 if (nd->nd_mrep != NULL)
3094 mbuf_freem(nd->nd_mrep);
3100 * NFS V3 readdir plus RPC. Used in place of nfsrpc_readdir().
3101 * (Also used for NFS V4 when mount flag set.)
3102 * (ditto above w.r.t. multiple of DIRBLKSIZ, etc.)
3105 nfsrpc_readdirplus(vnode_t vp, struct uio *uiop, nfsuint64 *cookiep,
3106 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp,
3107 int *eofp, void *stuff)
3110 struct dirent *dp = NULL;
3112 vnode_t newvp = NULLVP;
3113 struct nfsrv_descript nfsd, *nd = &nfsd;
3114 struct nameidata nami, *ndp = &nami;
3115 struct componentname *cnp = &ndp->ni_cnd;
3116 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
3117 struct nfsnode *dnp = VTONFS(vp), *np;
3118 struct nfsvattr nfsva;
3120 nfsquad_t cookie, ncookie;
3121 int error = 0, tlen, more_dirs = 1, blksiz = 0, bigenough = 1;
3122 int attrflag, tryformoredirs = 1, eof = 0, gotmnton = 0;
3123 int isdotdot = 0, unlocknewvp = 0;
3124 long dotfileid, dotdotfileid = 0, fileno = 0;
3126 nfsattrbit_t attrbits, dattrbits;
3128 u_int32_t *tl2 = NULL, fakefileno = 0xffffffff, rderr;
3129 struct timespec dctime;
3131 KASSERT(uiop->uio_iovcnt == 1 &&
3132 (uio_uio_resid(uiop) & (DIRBLKSIZ - 1)) == 0,
3133 ("nfs readdirplusrpc bad uio"));
3134 timespecclear(&dctime);
3140 cookie.lval[0] = cookiep->nfsuquad[0];
3141 cookie.lval[1] = cookiep->nfsuquad[1];
3142 tresid = uio_uio_resid(uiop);
3145 * For NFSv4, first create the "." and ".." entries.
3147 if (NFSHASNFSV4(nmp)) {
3148 NFSGETATTR_ATTRBIT(&dattrbits);
3149 NFSZERO_ATTRBIT(&attrbits);
3150 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_FILEID);
3151 if (NFSISSET_ATTRBIT(&dnp->n_vattr.na_suppattr,
3152 NFSATTRBIT_MOUNTEDONFILEID)) {
3153 NFSSETBIT_ATTRBIT(&attrbits,
3154 NFSATTRBIT_MOUNTEDONFILEID);
3158 * Must fake it. Use the fileno, except when the
3159 * fsid is != to that of the directory. For that
3160 * case, generate a fake fileno that is not the same.
3162 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_FSID);
3167 * Joy, oh joy. For V4 we get to hand craft '.' and '..'.
3169 if (uiop->uio_offset == 0) {
3170 NFSCL_REQSTART(nd, NFSPROC_LOOKUPP, vp);
3171 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
3172 *tl++ = txdr_unsigned(NFSV4OP_GETFH);
3173 *tl = txdr_unsigned(NFSV4OP_GETATTR);
3174 (void) nfsrv_putattrbit(nd, &attrbits);
3175 error = nfscl_request(nd, vp, p, cred, stuff);
3178 dotfileid = 0; /* Fake out the compiler. */
3179 if ((nd->nd_flag & ND_NOMOREDATA) == 0) {
3180 error = nfsm_loadattr(nd, &nfsva);
3183 dctime = nfsva.na_ctime;
3184 dotfileid = nfsva.na_fileid;
3186 if (nd->nd_repstat == 0) {
3187 NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
3188 len = fxdr_unsigned(int, *(tl + 4));
3189 if (len > 0 && len <= NFSX_V4FHMAX)
3190 error = nfsm_advance(nd, NFSM_RNDUP(len), -1);
3194 NFSM_DISSECT(tl, u_int32_t *, 2*NFSX_UNSIGNED);
3195 nfsva.na_mntonfileno = 0xffffffff;
3196 error = nfsv4_loadattr(nd, NULL, &nfsva, NULL,
3197 NULL, 0, NULL, NULL, NULL, NULL, NULL, 0,
3198 NULL, NULL, NULL, p, cred);
3200 dotdotfileid = dotfileid;
3201 } else if (gotmnton) {
3202 if (nfsva.na_mntonfileno != 0xffffffff)
3203 dotdotfileid = nfsva.na_mntonfileno;
3205 dotdotfileid = nfsva.na_fileid;
3206 } else if (nfsva.na_filesid[0] ==
3207 dnp->n_vattr.na_filesid[0] &&
3208 nfsva.na_filesid[1] ==
3209 dnp->n_vattr.na_filesid[1]) {
3210 dotdotfileid = nfsva.na_fileid;
3214 } while (fakefileno ==
3216 dotdotfileid = fakefileno;
3219 } else if (nd->nd_repstat == NFSERR_NOENT) {
3221 * Lookupp returns NFSERR_NOENT when we are
3222 * at the root, so just use the current dir.
3225 dotdotfileid = dotfileid;
3227 error = nd->nd_repstat;
3229 mbuf_freem(nd->nd_mrep);
3233 dp = (struct dirent *)uio_iov_base(uiop);
3234 dp->d_type = DT_DIR;
3235 dp->d_fileno = dotfileid;
3237 dp->d_name[0] = '.';
3238 dp->d_name[1] = '\0';
3239 dp->d_reclen = DIRENT_SIZE(dp) + NFSX_HYPER;
3241 * Just make these offset cookie 0.
3243 tl = (u_int32_t *)&dp->d_name[4];
3246 blksiz += dp->d_reclen;
3247 uio_uio_resid_add(uiop, -(dp->d_reclen));
3248 uiop->uio_offset += dp->d_reclen;
3249 uio_iov_base_add(uiop, dp->d_reclen);
3250 uio_iov_len_add(uiop, -(dp->d_reclen));
3251 dp = (struct dirent *)uio_iov_base(uiop);
3252 dp->d_type = DT_DIR;
3253 dp->d_fileno = dotdotfileid;
3255 dp->d_name[0] = '.';
3256 dp->d_name[1] = '.';
3257 dp->d_name[2] = '\0';
3258 dp->d_reclen = DIRENT_SIZE(dp) + NFSX_HYPER;
3260 * Just make these offset cookie 0.
3262 tl = (u_int32_t *)&dp->d_name[4];
3265 blksiz += dp->d_reclen;
3266 uio_uio_resid_add(uiop, -(dp->d_reclen));
3267 uiop->uio_offset += dp->d_reclen;
3268 uio_iov_base_add(uiop, dp->d_reclen);
3269 uio_iov_len_add(uiop, -(dp->d_reclen));
3271 NFSREADDIRPLUS_ATTRBIT(&attrbits);
3273 NFSSETBIT_ATTRBIT(&attrbits,
3274 NFSATTRBIT_MOUNTEDONFILEID);
3278 * Loop around doing readdir rpc's of size nm_readdirsize.
3279 * The stopping criteria is EOF or buffer full.
3281 while (more_dirs && bigenough) {
3283 NFSCL_REQSTART(nd, NFSPROC_READDIRPLUS, vp);
3284 NFSM_BUILD(tl, u_int32_t *, 6 * NFSX_UNSIGNED);
3285 *tl++ = cookie.lval[0];
3286 *tl++ = cookie.lval[1];
3287 if (cookie.qval == 0) {
3292 *tl++ = dnp->n_cookieverf.nfsuquad[0];
3293 *tl++ = dnp->n_cookieverf.nfsuquad[1];
3296 *tl++ = txdr_unsigned(nmp->nm_readdirsize);
3297 *tl = txdr_unsigned(nmp->nm_readdirsize);
3298 if (nd->nd_flag & ND_NFSV4) {
3299 (void) nfsrv_putattrbit(nd, &attrbits);
3300 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
3301 *tl = txdr_unsigned(NFSV4OP_GETATTR);
3302 (void) nfsrv_putattrbit(nd, &dattrbits);
3304 error = nfscl_request(nd, vp, p, cred, stuff);
3307 if (nd->nd_flag & ND_NFSV3)
3308 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
3309 if (nd->nd_repstat || error) {
3311 error = nd->nd_repstat;
3314 if ((nd->nd_flag & ND_NFSV3) != 0 && *attrflagp != 0)
3315 dctime = nap->na_ctime;
3316 NFSM_DISSECT(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
3318 dnp->n_cookieverf.nfsuquad[0] = *tl++;
3319 dnp->n_cookieverf.nfsuquad[1] = *tl++;
3321 more_dirs = fxdr_unsigned(int, *tl);
3325 /* loop thru the dir entries, doctoring them to 4bsd form */
3326 while (more_dirs && bigenough) {
3327 NFSM_DISSECT(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
3328 if (nd->nd_flag & ND_NFSV4) {
3329 ncookie.lval[0] = *tl++;
3330 ncookie.lval[1] = *tl++;
3332 fileno = fxdr_unsigned(long, *++tl);
3335 len = fxdr_unsigned(int, *tl);
3336 if (len <= 0 || len > NFS_MAXNAMLEN) {
3340 tlen = NFSM_RNDUP(len);
3342 tlen += 4; /* To ensure null termination */
3343 left = DIRBLKSIZ - blksiz;
3344 if ((tlen + DIRHDSIZ + NFSX_HYPER) > left) {
3345 dp->d_reclen += left;
3346 uio_iov_base_add(uiop, left);
3347 uio_iov_len_add(uiop, -(left));
3348 uio_uio_resid_add(uiop, -(left));
3349 uiop->uio_offset += left;
3352 if ((tlen + DIRHDSIZ + NFSX_HYPER) > uio_uio_resid(uiop))
3355 dp = (struct dirent *)uio_iov_base(uiop);
3357 dp->d_reclen = tlen + DIRHDSIZ + NFSX_HYPER;
3358 dp->d_type = DT_UNKNOWN;
3359 blksiz += dp->d_reclen;
3360 if (blksiz == DIRBLKSIZ)
3362 uio_uio_resid_add(uiop, -(DIRHDSIZ));
3363 uiop->uio_offset += DIRHDSIZ;
3364 uio_iov_base_add(uiop, DIRHDSIZ);
3365 uio_iov_len_add(uiop, -(DIRHDSIZ));
3366 cnp->cn_nameptr = uio_iov_base(uiop);
3367 cnp->cn_namelen = len;
3369 error = nfsm_mbufuio(nd, uiop, len);
3372 cp = uio_iov_base(uiop);
3375 cp += tlen; /* points to cookie storage */
3376 tl2 = (u_int32_t *)cp;
3377 if (len == 2 && cnp->cn_nameptr[0] == '.' &&
3378 cnp->cn_nameptr[1] == '.')
3382 uio_iov_base_add(uiop, (tlen + NFSX_HYPER));
3383 uio_iov_len_add(uiop, -(tlen + NFSX_HYPER));
3384 uio_uio_resid_add(uiop, -(tlen + NFSX_HYPER));
3385 uiop->uio_offset += (tlen + NFSX_HYPER);
3387 error = nfsm_advance(nd, NFSM_RNDUP(len), -1);
3392 if (nd->nd_flag & ND_NFSV3) {
3393 NFSM_DISSECT(tl, u_int32_t *, 3*NFSX_UNSIGNED);
3394 ncookie.lval[0] = *tl++;
3395 ncookie.lval[1] = *tl++;
3396 attrflag = fxdr_unsigned(int, *tl);
3398 error = nfsm_loadattr(nd, &nfsva);
3402 NFSM_DISSECT(tl,u_int32_t *,NFSX_UNSIGNED);
3404 error = nfsm_getfh(nd, &nfhp);
3408 if (!attrflag && nfhp != NULL) {
3409 FREE((caddr_t)nfhp, M_NFSFH);
3414 nfsva.na_mntonfileno = 0xffffffff;
3415 error = nfsv4_loadattr(nd, NULL, &nfsva, &nfhp,
3416 NULL, 0, NULL, NULL, NULL, NULL, NULL, 0,
3417 NULL, NULL, &rderr, p, cred);
3423 if (nd->nd_flag & ND_NFSV4) {
3426 } else if (gotmnton) {
3427 if (nfsva.na_mntonfileno != 0xffffffff)
3428 dp->d_fileno = nfsva.na_mntonfileno;
3430 dp->d_fileno = nfsva.na_fileid;
3431 } else if (nfsva.na_filesid[0] ==
3432 dnp->n_vattr.na_filesid[0] &&
3433 nfsva.na_filesid[1] ==
3434 dnp->n_vattr.na_filesid[1]) {
3435 dp->d_fileno = nfsva.na_fileid;
3439 } while (fakefileno ==
3441 dp->d_fileno = fakefileno;
3444 dp->d_fileno = fileno;
3446 *tl2++ = cookiep->nfsuquad[0] = cookie.lval[0] =
3448 *tl2 = cookiep->nfsuquad[1] = cookie.lval[1] =
3452 if (NFSRV_CMPFH(nfhp->nfh_fh, nfhp->nfh_len,
3453 dnp->n_fhp->nfh_fh, dnp->n_fhp->nfh_len)) {
3457 FREE((caddr_t)nfhp, M_NFSFH);
3459 } else if (isdotdot != 0) {
3461 * Skip doing a nfscl_nget() call for "..".
3462 * There's a race between acquiring the nfs
3463 * node here and lookups that look for the
3464 * directory being read (in the parent).
3465 * It would try to get a lock on ".." here,
3466 * owning the lock on the directory being
3467 * read. Lookup will hold the lock on ".."
3468 * and try to acquire the lock on the
3469 * directory being read.
3470 * If the directory is unlocked/relocked,
3471 * then there is a LOR with the buflock
3474 free(nfhp, M_NFSFH);
3476 error = nfscl_nget(vnode_mount(vp), vp,
3477 nfhp, cnp, p, &np, NULL, LK_EXCLUSIVE);
3484 if (newvp != NULLVP) {
3485 error = nfscl_loadattrcache(&newvp,
3486 &nfsva, NULL, NULL, 0, 0);
3495 vtonfs_dtype(np->n_vattr.na_type);
3497 NFSCNHASH(cnp, HASHINIT);
3498 if (cnp->cn_namelen <= NCHNAMLEN &&
3499 (newvp->v_type != VDIR ||
3500 dctime.tv_sec != 0)) {
3501 cache_enter_time(ndp->ni_dvp,
3504 newvp->v_type != VDIR ? NULL :
3514 } else if (nfhp != NULL) {
3515 FREE((caddr_t)nfhp, M_NFSFH);
3517 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3518 more_dirs = fxdr_unsigned(int, *tl);
3521 * If at end of rpc data, get the eof boolean
3524 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3525 eof = fxdr_unsigned(int, *tl);
3528 if (nd->nd_flag & ND_NFSV4) {
3529 error = nfscl_postop_attr(nd, nap, attrflagp,
3535 mbuf_freem(nd->nd_mrep);
3539 * Fill last record, iff any, out to a multiple of DIRBLKSIZ
3540 * by increasing d_reclen for the last record.
3543 left = DIRBLKSIZ - blksiz;
3544 dp->d_reclen += left;
3545 uio_iov_base_add(uiop, left);
3546 uio_iov_len_add(uiop, -(left));
3547 uio_uio_resid_add(uiop, -(left));
3548 uiop->uio_offset += left;
3552 * If returning no data, assume end of file.
3553 * If not bigenough, return not end of file, since you aren't
3554 * returning all the data
3555 * Otherwise, return the eof flag from the server.
3558 if (tresid == uio_uio_resid(uiop))
3560 else if (!bigenough)
3567 if (nd->nd_mrep != NULL)
3568 mbuf_freem(nd->nd_mrep);
3577 nfsrpc_commit(vnode_t vp, u_quad_t offset, int cnt, struct ucred *cred,
3578 NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, void *stuff)
3581 struct nfsrv_descript nfsd, *nd = &nfsd;
3582 nfsattrbit_t attrbits;
3584 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
3587 NFSCL_REQSTART(nd, NFSPROC_COMMIT, vp);
3588 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
3589 txdr_hyper(offset, tl);
3591 *tl = txdr_unsigned(cnt);
3592 if (nd->nd_flag & ND_NFSV4) {
3594 * And do a Getattr op.
3596 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
3597 *tl = txdr_unsigned(NFSV4OP_GETATTR);
3598 NFSGETATTR_ATTRBIT(&attrbits);
3599 (void) nfsrv_putattrbit(nd, &attrbits);
3601 error = nfscl_request(nd, vp, p, cred, stuff);
3604 error = nfscl_wcc_data(nd, vp, nap, attrflagp, NULL, stuff);
3605 if (!error && !nd->nd_repstat) {
3606 NFSM_DISSECT(tl, u_int32_t *, NFSX_VERF);
3608 if (NFSBCMP(nmp->nm_verf, tl, NFSX_VERF)) {
3609 NFSBCOPY(tl, nmp->nm_verf, NFSX_VERF);
3610 nd->nd_repstat = NFSERR_STALEWRITEVERF;
3613 if (nd->nd_flag & ND_NFSV4)
3614 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
3617 if (!error && nd->nd_repstat)
3618 error = nd->nd_repstat;
3619 mbuf_freem(nd->nd_mrep);
3624 * NFS byte range lock rpc.
3625 * (Mostly just calls one of the three lower level RPC routines.)
3628 nfsrpc_advlock(vnode_t vp, off_t size, int op, struct flock *fl,
3629 int reclaim, struct ucred *cred, NFSPROC_T *p, void *id, int flags)
3631 struct nfscllockowner *lp;
3632 struct nfsclclient *clp;
3634 struct nfsrv_descript nfsd, *nd = &nfsd;
3635 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
3638 u_int32_t clidrev = 0;
3639 int error = 0, newone = 0, expireret = 0, retrycnt, donelocally;
3643 * Convert the flock structure into a start and end and do POSIX
3646 switch (fl->l_whence) {
3650 * Caller is responsible for adding any necessary offset
3651 * when SEEK_CUR is used.
3653 start = fl->l_start;
3657 start = size + fl->l_start;
3658 off = size + fl->l_start;
3665 if (fl->l_len != 0) {
3666 end = start + fl->l_len - 1;
3677 if (op == F_GETLK) {
3678 error = nfscl_getcl(vnode_mount(vp), cred, p, 1, &clp);
3681 error = nfscl_lockt(vp, clp, off, len, fl, p, id, flags);
3683 clidrev = clp->nfsc_clientidrev;
3684 error = nfsrpc_lockt(nd, vp, clp, off, len, fl, cred,
3686 } else if (error == -1) {
3689 nfscl_clientrelease(clp);
3690 } else if (op == F_UNLCK && fl->l_type == F_UNLCK) {
3692 * We must loop around for all lockowner cases.
3695 error = nfscl_getcl(vnode_mount(vp), cred, p, 1, &clp);
3699 error = nfscl_relbytelock(vp, off, len, cred, p, callcnt,
3700 clp, id, flags, &lp, &dorpc);
3702 * If it returns a NULL lp, we're done.
3706 nfscl_clientrelease(clp);
3708 nfscl_releasealllocks(clp, vp, p, id, flags);
3711 if (nmp->nm_clp != NULL)
3712 clidrev = nmp->nm_clp->nfsc_clientidrev;
3716 * If the server doesn't support Posix lock semantics,
3717 * only allow locks on the entire file, since it won't
3718 * handle overlapping byte ranges.
3719 * There might still be a problem when a lock
3720 * upgrade/downgrade (read<->write) occurs, since the
3721 * server "might" expect an unlock first?
3723 if (dorpc && (lp->nfsl_open->nfso_posixlock ||
3724 (off == 0 && len == NFS64BITSSET))) {
3726 * Since the lock records will go away, we must
3727 * wait for grace and delay here.
3730 error = nfsrpc_locku(nd, nmp, lp, off, len,
3731 NFSV4LOCKT_READ, cred, p, 0);
3732 if ((nd->nd_repstat == NFSERR_GRACE ||
3733 nd->nd_repstat == NFSERR_DELAY) &&
3735 (void) nfs_catnap(PZERO, (int)nd->nd_repstat,
3737 } while ((nd->nd_repstat == NFSERR_GRACE ||
3738 nd->nd_repstat == NFSERR_DELAY) && error == 0);
3741 } while (error == 0 && nd->nd_repstat == 0);
3742 nfscl_releasealllocks(clp, vp, p, id, flags);
3743 } else if (op == F_SETLK) {
3744 error = nfscl_getbytelock(vp, off, len, fl->l_type, cred, p,
3745 NULL, 0, id, flags, NULL, NULL, &lp, &newone, &donelocally);
3746 if (error || donelocally) {
3749 if (nmp->nm_clp != NULL)
3750 clidrev = nmp->nm_clp->nfsc_clientidrev;
3753 nfhp = VTONFS(vp)->n_fhp;
3754 if (!lp->nfsl_open->nfso_posixlock &&
3755 (off != 0 || len != NFS64BITSSET)) {
3758 error = nfsrpc_lock(nd, nmp, vp, nfhp->nfh_fh,
3759 nfhp->nfh_len, lp, newone, reclaim, off,
3760 len, fl->l_type, cred, p, 0);
3763 error = nd->nd_repstat;
3764 nfscl_lockrelease(lp, error, newone);
3769 error = nd->nd_repstat;
3770 if (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
3771 error == NFSERR_STALEDONTRECOVER ||
3772 error == NFSERR_STALECLIENTID || error == NFSERR_DELAY ||
3773 error == NFSERR_BADSESSION) {
3774 (void) nfs_catnap(PZERO, error, "nfs_advlock");
3775 } else if ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID)
3777 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
3780 } while (error == NFSERR_GRACE ||
3781 error == NFSERR_STALECLIENTID || error == NFSERR_DELAY ||
3782 error == NFSERR_STALEDONTRECOVER || error == NFSERR_STALESTATEID ||
3783 error == NFSERR_BADSESSION ||
3784 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
3785 expireret == 0 && clidrev != 0 && retrycnt < 4));
3786 if (error && retrycnt >= 4)
3792 * The lower level routine for the LockT case.
3795 nfsrpc_lockt(struct nfsrv_descript *nd, vnode_t vp,
3796 struct nfsclclient *clp, u_int64_t off, u_int64_t len, struct flock *fl,
3797 struct ucred *cred, NFSPROC_T *p, void *id, int flags)
3800 int error, type, size;
3801 uint8_t own[NFSV4CL_LOCKNAMELEN + NFSX_V4FHMAX];
3803 struct nfsmount *nmp;
3805 nmp = VFSTONFS(vp->v_mount);
3806 NFSCL_REQSTART(nd, NFSPROC_LOCKT, vp);
3807 NFSM_BUILD(tl, u_int32_t *, 7 * NFSX_UNSIGNED);
3808 if (fl->l_type == F_RDLCK)
3809 *tl++ = txdr_unsigned(NFSV4LOCKT_READ);
3811 *tl++ = txdr_unsigned(NFSV4LOCKT_WRITE);
3812 txdr_hyper(off, tl);
3814 txdr_hyper(len, tl);
3816 *tl++ = NFSMNT_MDSSESSION(nmp)->nfsess_clientid.lval[0];
3817 *tl = NFSMNT_MDSSESSION(nmp)->nfsess_clientid.lval[1];
3818 nfscl_filllockowner(id, own, flags);
3820 NFSBCOPY(np->n_fhp->nfh_fh, &own[NFSV4CL_LOCKNAMELEN],
3821 np->n_fhp->nfh_len);
3822 (void)nfsm_strtom(nd, own, NFSV4CL_LOCKNAMELEN + np->n_fhp->nfh_len);
3823 error = nfscl_request(nd, vp, p, cred, NULL);
3826 if (nd->nd_repstat == 0) {
3827 fl->l_type = F_UNLCK;
3828 } else if (nd->nd_repstat == NFSERR_DENIED) {
3830 fl->l_whence = SEEK_SET;
3831 NFSM_DISSECT(tl, u_int32_t *, 8 * NFSX_UNSIGNED);
3832 fl->l_start = fxdr_hyper(tl);
3834 len = fxdr_hyper(tl);
3836 if (len == NFS64BITSSET)
3840 type = fxdr_unsigned(int, *tl++);
3841 if (type == NFSV4LOCKT_WRITE)
3842 fl->l_type = F_WRLCK;
3844 fl->l_type = F_RDLCK;
3846 * XXX For now, I have no idea what to do with the
3847 * conflicting lock_owner, so I'll just set the pid == 0
3848 * and skip over the lock_owner.
3850 fl->l_pid = (pid_t)0;
3852 size = fxdr_unsigned(int, *tl);
3853 if (size < 0 || size > NFSV4_OPAQUELIMIT)
3856 error = nfsm_advance(nd, NFSM_RNDUP(size), -1);
3857 } else if (nd->nd_repstat == NFSERR_STALECLIENTID ||
3858 nd->nd_repstat == NFSERR_BADSESSION)
3859 nfscl_initiate_recovery(clp);
3861 mbuf_freem(nd->nd_mrep);
3866 * Lower level function that performs the LockU RPC.
3869 nfsrpc_locku(struct nfsrv_descript *nd, struct nfsmount *nmp,
3870 struct nfscllockowner *lp, u_int64_t off, u_int64_t len,
3871 u_int32_t type, struct ucred *cred, NFSPROC_T *p, int syscred)
3876 nfscl_reqstart(nd, NFSPROC_LOCKU, nmp, lp->nfsl_open->nfso_fh,
3877 lp->nfsl_open->nfso_fhlen, NULL, NULL);
3878 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID + 6 * NFSX_UNSIGNED);
3879 *tl++ = txdr_unsigned(type);
3880 *tl = txdr_unsigned(lp->nfsl_seqid);
3881 if (nfstest_outofseq &&
3882 (arc4random() % nfstest_outofseq) == 0)
3883 *tl = txdr_unsigned(lp->nfsl_seqid + 1);
3885 if (NFSHASNFSV4N(nmp))
3888 *tl++ = lp->nfsl_stateid.seqid;
3889 *tl++ = lp->nfsl_stateid.other[0];
3890 *tl++ = lp->nfsl_stateid.other[1];
3891 *tl++ = lp->nfsl_stateid.other[2];
3892 txdr_hyper(off, tl);
3894 txdr_hyper(len, tl);
3896 nd->nd_flag |= ND_USEGSSNAME;
3897 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
3898 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
3899 NFSCL_INCRSEQID(lp->nfsl_seqid, nd);
3902 if (nd->nd_repstat == 0) {
3903 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID);
3904 lp->nfsl_stateid.seqid = *tl++;
3905 lp->nfsl_stateid.other[0] = *tl++;
3906 lp->nfsl_stateid.other[1] = *tl++;
3907 lp->nfsl_stateid.other[2] = *tl;
3908 } else if (nd->nd_repstat == NFSERR_STALESTATEID ||
3909 nd->nd_repstat == NFSERR_BADSESSION)
3910 nfscl_initiate_recovery(lp->nfsl_open->nfso_own->nfsow_clp);
3912 mbuf_freem(nd->nd_mrep);
3917 * The actual Lock RPC.
3920 nfsrpc_lock(struct nfsrv_descript *nd, struct nfsmount *nmp, vnode_t vp,
3921 u_int8_t *nfhp, int fhlen, struct nfscllockowner *lp, int newone,
3922 int reclaim, u_int64_t off, u_int64_t len, short type, struct ucred *cred,
3923 NFSPROC_T *p, int syscred)
3927 uint8_t own[NFSV4CL_LOCKNAMELEN + NFSX_V4FHMAX];
3929 nfscl_reqstart(nd, NFSPROC_LOCK, nmp, nfhp, fhlen, NULL, NULL);
3930 NFSM_BUILD(tl, u_int32_t *, 7 * NFSX_UNSIGNED);
3931 if (type == F_RDLCK)
3932 *tl++ = txdr_unsigned(NFSV4LOCKT_READ);
3934 *tl++ = txdr_unsigned(NFSV4LOCKT_WRITE);
3935 *tl++ = txdr_unsigned(reclaim);
3936 txdr_hyper(off, tl);
3938 txdr_hyper(len, tl);
3942 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID +
3943 2 * NFSX_UNSIGNED + NFSX_HYPER);
3944 *tl++ = txdr_unsigned(lp->nfsl_open->nfso_own->nfsow_seqid);
3945 if (NFSHASNFSV4N(nmp))
3948 *tl++ = lp->nfsl_open->nfso_stateid.seqid;
3949 *tl++ = lp->nfsl_open->nfso_stateid.other[0];
3950 *tl++ = lp->nfsl_open->nfso_stateid.other[1];
3951 *tl++ = lp->nfsl_open->nfso_stateid.other[2];
3952 *tl++ = txdr_unsigned(lp->nfsl_seqid);
3953 *tl++ = NFSMNT_MDSSESSION(nmp)->nfsess_clientid.lval[0];
3954 *tl = NFSMNT_MDSSESSION(nmp)->nfsess_clientid.lval[1];
3955 NFSBCOPY(lp->nfsl_owner, own, NFSV4CL_LOCKNAMELEN);
3956 NFSBCOPY(nfhp, &own[NFSV4CL_LOCKNAMELEN], fhlen);
3957 (void)nfsm_strtom(nd, own, NFSV4CL_LOCKNAMELEN + fhlen);
3960 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID + NFSX_UNSIGNED);
3961 if (NFSHASNFSV4N(nmp))
3964 *tl++ = lp->nfsl_stateid.seqid;
3965 *tl++ = lp->nfsl_stateid.other[0];
3966 *tl++ = lp->nfsl_stateid.other[1];
3967 *tl++ = lp->nfsl_stateid.other[2];
3968 *tl = txdr_unsigned(lp->nfsl_seqid);
3969 if (nfstest_outofseq &&
3970 (arc4random() % nfstest_outofseq) == 0)
3971 *tl = txdr_unsigned(lp->nfsl_seqid + 1);
3974 nd->nd_flag |= ND_USEGSSNAME;
3975 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, vp, p, cred,
3976 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
3980 NFSCL_INCRSEQID(lp->nfsl_open->nfso_own->nfsow_seqid, nd);
3981 NFSCL_INCRSEQID(lp->nfsl_seqid, nd);
3982 if (nd->nd_repstat == 0) {
3983 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID);
3984 lp->nfsl_stateid.seqid = *tl++;
3985 lp->nfsl_stateid.other[0] = *tl++;
3986 lp->nfsl_stateid.other[1] = *tl++;
3987 lp->nfsl_stateid.other[2] = *tl;
3988 } else if (nd->nd_repstat == NFSERR_DENIED) {
3989 NFSM_DISSECT(tl, u_int32_t *, 8 * NFSX_UNSIGNED);
3990 size = fxdr_unsigned(int, *(tl + 7));
3991 if (size < 0 || size > NFSV4_OPAQUELIMIT)
3994 error = nfsm_advance(nd, NFSM_RNDUP(size), -1);
3995 } else if (nd->nd_repstat == NFSERR_STALESTATEID ||
3996 nd->nd_repstat == NFSERR_BADSESSION)
3997 nfscl_initiate_recovery(lp->nfsl_open->nfso_own->nfsow_clp);
3999 mbuf_freem(nd->nd_mrep);
4005 * (always called with the vp for the mount point)
4008 nfsrpc_statfs(vnode_t vp, struct nfsstatfs *sbp, struct nfsfsinfo *fsp,
4009 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp,
4012 u_int32_t *tl = NULL;
4013 struct nfsrv_descript nfsd, *nd = &nfsd;
4014 struct nfsmount *nmp;
4015 nfsattrbit_t attrbits;
4019 nmp = VFSTONFS(vnode_mount(vp));
4020 if (NFSHASNFSV4(nmp)) {
4022 * For V4, you actually do a getattr.
4024 NFSCL_REQSTART(nd, NFSPROC_GETATTR, vp);
4025 NFSSTATFS_GETATTRBIT(&attrbits);
4026 (void) nfsrv_putattrbit(nd, &attrbits);
4027 nd->nd_flag |= ND_USEGSSNAME;
4028 error = nfscl_request(nd, vp, p, cred, stuff);
4031 if (nd->nd_repstat == 0) {
4032 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0,
4033 NULL, NULL, sbp, fsp, NULL, 0, NULL, NULL, NULL, p,
4036 nmp->nm_fsid[0] = nap->na_filesid[0];
4037 nmp->nm_fsid[1] = nap->na_filesid[1];
4038 NFSSETHASSETFSID(nmp);
4042 error = nd->nd_repstat;
4047 NFSCL_REQSTART(nd, NFSPROC_FSSTAT, vp);
4048 error = nfscl_request(nd, vp, p, cred, stuff);
4051 if (nd->nd_flag & ND_NFSV3) {
4052 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
4056 if (nd->nd_repstat) {
4057 error = nd->nd_repstat;
4060 NFSM_DISSECT(tl, u_int32_t *,
4061 NFSX_STATFS(nd->nd_flag & ND_NFSV3));
4063 if (NFSHASNFSV3(nmp)) {
4064 sbp->sf_tbytes = fxdr_hyper(tl); tl += 2;
4065 sbp->sf_fbytes = fxdr_hyper(tl); tl += 2;
4066 sbp->sf_abytes = fxdr_hyper(tl); tl += 2;
4067 sbp->sf_tfiles = fxdr_hyper(tl); tl += 2;
4068 sbp->sf_ffiles = fxdr_hyper(tl); tl += 2;
4069 sbp->sf_afiles = fxdr_hyper(tl); tl += 2;
4070 sbp->sf_invarsec = fxdr_unsigned(u_int32_t, *tl);
4071 } else if (NFSHASNFSV4(nmp) == 0) {
4072 sbp->sf_tsize = fxdr_unsigned(u_int32_t, *tl++);
4073 sbp->sf_bsize = fxdr_unsigned(u_int32_t, *tl++);
4074 sbp->sf_blocks = fxdr_unsigned(u_int32_t, *tl++);
4075 sbp->sf_bfree = fxdr_unsigned(u_int32_t, *tl++);
4076 sbp->sf_bavail = fxdr_unsigned(u_int32_t, *tl);
4079 mbuf_freem(nd->nd_mrep);
4087 nfsrpc_pathconf(vnode_t vp, struct nfsv3_pathconf *pc,
4088 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp,
4091 struct nfsrv_descript nfsd, *nd = &nfsd;
4092 struct nfsmount *nmp;
4094 nfsattrbit_t attrbits;
4098 nmp = VFSTONFS(vnode_mount(vp));
4099 if (NFSHASNFSV4(nmp)) {
4101 * For V4, you actually do a getattr.
4103 NFSCL_REQSTART(nd, NFSPROC_GETATTR, vp);
4104 NFSPATHCONF_GETATTRBIT(&attrbits);
4105 (void) nfsrv_putattrbit(nd, &attrbits);
4106 nd->nd_flag |= ND_USEGSSNAME;
4107 error = nfscl_request(nd, vp, p, cred, stuff);
4110 if (nd->nd_repstat == 0) {
4111 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0,
4112 pc, NULL, NULL, NULL, NULL, 0, NULL, NULL, NULL, p,
4117 error = nd->nd_repstat;
4120 NFSCL_REQSTART(nd, NFSPROC_PATHCONF, vp);
4121 error = nfscl_request(nd, vp, p, cred, stuff);
4124 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
4125 if (nd->nd_repstat && !error)
4126 error = nd->nd_repstat;
4128 NFSM_DISSECT(tl, u_int32_t *, NFSX_V3PATHCONF);
4129 pc->pc_linkmax = fxdr_unsigned(u_int32_t, *tl++);
4130 pc->pc_namemax = fxdr_unsigned(u_int32_t, *tl++);
4131 pc->pc_notrunc = fxdr_unsigned(u_int32_t, *tl++);
4132 pc->pc_chownrestricted =
4133 fxdr_unsigned(u_int32_t, *tl++);
4134 pc->pc_caseinsensitive =
4135 fxdr_unsigned(u_int32_t, *tl++);
4136 pc->pc_casepreserving = fxdr_unsigned(u_int32_t, *tl);
4140 mbuf_freem(nd->nd_mrep);
4145 * nfs version 3 fsinfo rpc call
4148 nfsrpc_fsinfo(vnode_t vp, struct nfsfsinfo *fsp, struct ucred *cred,
4149 NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, void *stuff)
4152 struct nfsrv_descript nfsd, *nd = &nfsd;
4156 NFSCL_REQSTART(nd, NFSPROC_FSINFO, vp);
4157 error = nfscl_request(nd, vp, p, cred, stuff);
4160 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
4161 if (nd->nd_repstat && !error)
4162 error = nd->nd_repstat;
4164 NFSM_DISSECT(tl, u_int32_t *, NFSX_V3FSINFO);
4165 fsp->fs_rtmax = fxdr_unsigned(u_int32_t, *tl++);
4166 fsp->fs_rtpref = fxdr_unsigned(u_int32_t, *tl++);
4167 fsp->fs_rtmult = fxdr_unsigned(u_int32_t, *tl++);
4168 fsp->fs_wtmax = fxdr_unsigned(u_int32_t, *tl++);
4169 fsp->fs_wtpref = fxdr_unsigned(u_int32_t, *tl++);
4170 fsp->fs_wtmult = fxdr_unsigned(u_int32_t, *tl++);
4171 fsp->fs_dtpref = fxdr_unsigned(u_int32_t, *tl++);
4172 fsp->fs_maxfilesize = fxdr_hyper(tl);
4174 fxdr_nfsv3time(tl, &fsp->fs_timedelta);
4176 fsp->fs_properties = fxdr_unsigned(u_int32_t, *tl);
4179 mbuf_freem(nd->nd_mrep);
4184 * This function performs the Renew RPC.
4187 nfsrpc_renew(struct nfsclclient *clp, struct nfsclds *dsp, struct ucred *cred,
4191 struct nfsrv_descript nfsd;
4192 struct nfsrv_descript *nd = &nfsd;
4193 struct nfsmount *nmp;
4195 struct nfssockreq *nrp;
4197 nmp = clp->nfsc_nmp;
4200 nfscl_reqstart(nd, NFSPROC_RENEW, nmp, NULL, 0, NULL,
4201 &dsp->nfsclds_sess);
4202 if (!NFSHASNFSV4N(nmp)) {
4203 /* NFSv4.1 just uses a Sequence Op and not a Renew. */
4204 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
4205 *tl++ = NFSMNT_MDSSESSION(nmp)->nfsess_clientid.lval[0];
4206 *tl = NFSMNT_MDSSESSION(nmp)->nfsess_clientid.lval[1];
4208 nrp = dsp->nfsclds_sockp;
4210 /* If NULL, use the MDS socket. */
4211 nrp = &nmp->nm_sockreq;
4212 nd->nd_flag |= ND_USEGSSNAME;
4213 error = newnfs_request(nd, nmp, NULL, nrp, NULL, p, cred,
4214 NFS_PROG, NFS_VER4, NULL, 1, NULL, &dsp->nfsclds_sess);
4217 error = nd->nd_repstat;
4218 mbuf_freem(nd->nd_mrep);
4223 * This function performs the Releaselockowner RPC.
4226 nfsrpc_rellockown(struct nfsmount *nmp, struct nfscllockowner *lp,
4227 uint8_t *fh, int fhlen, struct ucred *cred, NFSPROC_T *p)
4229 struct nfsrv_descript nfsd, *nd = &nfsd;
4232 uint8_t own[NFSV4CL_LOCKNAMELEN + NFSX_V4FHMAX];
4234 if (NFSHASNFSV4N(nmp)) {
4235 /* For NFSv4.1, do a FreeStateID. */
4236 nfscl_reqstart(nd, NFSPROC_FREESTATEID, nmp, NULL, 0, NULL,
4238 nfsm_stateidtom(nd, &lp->nfsl_stateid, NFSSTATEID_PUTSTATEID);
4240 nfscl_reqstart(nd, NFSPROC_RELEASELCKOWN, nmp, NULL, 0, NULL,
4242 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
4243 *tl++ = NFSMNT_MDSSESSION(nmp)->nfsess_clientid.lval[0];
4244 *tl = NFSMNT_MDSSESSION(nmp)->nfsess_clientid.lval[1];
4245 NFSBCOPY(lp->nfsl_owner, own, NFSV4CL_LOCKNAMELEN);
4246 NFSBCOPY(fh, &own[NFSV4CL_LOCKNAMELEN], fhlen);
4247 (void)nfsm_strtom(nd, own, NFSV4CL_LOCKNAMELEN + fhlen);
4249 nd->nd_flag |= ND_USEGSSNAME;
4250 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4251 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4254 error = nd->nd_repstat;
4255 mbuf_freem(nd->nd_mrep);
4260 * This function performs the Compound to get the mount pt FH.
4263 nfsrpc_getdirpath(struct nfsmount *nmp, u_char *dirpath, struct ucred *cred,
4267 struct nfsrv_descript nfsd;
4268 struct nfsrv_descript *nd = &nfsd;
4270 int error, cnt, len, setnil;
4273 nfscl_reqstart(nd, NFSPROC_PUTROOTFH, nmp, NULL, 0, &opcntp, NULL);
4281 while (*cp2 != '\0' && *cp2 != '/')
4288 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
4289 *tl = txdr_unsigned(NFSV4OP_LOOKUP);
4290 nfsm_strtom(nd, cp, strlen(cp));
4296 } while (*cp != '\0');
4297 if (NFSHASNFSV4N(nmp))
4298 /* Has a Sequence Op done by nfscl_reqstart(). */
4299 *opcntp = txdr_unsigned(3 + cnt);
4301 *opcntp = txdr_unsigned(2 + cnt);
4302 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
4303 *tl = txdr_unsigned(NFSV4OP_GETFH);
4304 nd->nd_flag |= ND_USEGSSNAME;
4305 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4306 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4309 if (nd->nd_repstat == 0) {
4310 NFSM_DISSECT(tl, u_int32_t *, (3 + 2 * cnt) * NFSX_UNSIGNED);
4311 tl += (2 + 2 * cnt);
4312 if ((len = fxdr_unsigned(int, *tl)) <= 0 ||
4314 nd->nd_repstat = NFSERR_BADXDR;
4316 nd->nd_repstat = nfsrv_mtostr(nd, nmp->nm_fh, len);
4317 if (nd->nd_repstat == 0)
4318 nmp->nm_fhsize = len;
4321 error = nd->nd_repstat;
4323 mbuf_freem(nd->nd_mrep);
4328 * This function performs the Delegreturn RPC.
4331 nfsrpc_delegreturn(struct nfscldeleg *dp, struct ucred *cred,
4332 struct nfsmount *nmp, NFSPROC_T *p, int syscred)
4335 struct nfsrv_descript nfsd;
4336 struct nfsrv_descript *nd = &nfsd;
4339 nfscl_reqstart(nd, NFSPROC_DELEGRETURN, nmp, dp->nfsdl_fh,
4340 dp->nfsdl_fhlen, NULL, NULL);
4341 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID);
4342 if (NFSHASNFSV4N(nmp))
4345 *tl++ = dp->nfsdl_stateid.seqid;
4346 *tl++ = dp->nfsdl_stateid.other[0];
4347 *tl++ = dp->nfsdl_stateid.other[1];
4348 *tl = dp->nfsdl_stateid.other[2];
4350 nd->nd_flag |= ND_USEGSSNAME;
4351 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4352 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4355 error = nd->nd_repstat;
4356 mbuf_freem(nd->nd_mrep);
4364 nfsrpc_getacl(vnode_t vp, struct ucred *cred, NFSPROC_T *p,
4365 struct acl *aclp, void *stuff)
4367 struct nfsrv_descript nfsd, *nd = &nfsd;
4369 nfsattrbit_t attrbits;
4370 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
4372 if (nfsrv_useacl == 0 || !NFSHASNFSV4(nmp))
4373 return (EOPNOTSUPP);
4374 NFSCL_REQSTART(nd, NFSPROC_GETACL, vp);
4375 NFSZERO_ATTRBIT(&attrbits);
4376 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_ACL);
4377 (void) nfsrv_putattrbit(nd, &attrbits);
4378 error = nfscl_request(nd, vp, p, cred, stuff);
4381 if (!nd->nd_repstat)
4382 error = nfsv4_loadattr(nd, vp, NULL, NULL, NULL, 0, NULL,
4383 NULL, NULL, NULL, aclp, 0, NULL, NULL, NULL, p, cred);
4385 error = nd->nd_repstat;
4386 mbuf_freem(nd->nd_mrep);
4394 nfsrpc_setacl(vnode_t vp, struct ucred *cred, NFSPROC_T *p,
4395 struct acl *aclp, void *stuff)
4398 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
4400 if (nfsrv_useacl == 0 || !NFSHASNFSV4(nmp))
4401 return (EOPNOTSUPP);
4402 error = nfsrpc_setattr(vp, NULL, aclp, cred, p, NULL, NULL, stuff);
4410 nfsrpc_setaclrpc(vnode_t vp, struct ucred *cred, NFSPROC_T *p,
4411 struct acl *aclp, nfsv4stateid_t *stateidp, void *stuff)
4413 struct nfsrv_descript nfsd, *nd = &nfsd;
4415 nfsattrbit_t attrbits;
4416 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
4418 if (!NFSHASNFSV4(nmp))
4419 return (EOPNOTSUPP);
4420 NFSCL_REQSTART(nd, NFSPROC_SETACL, vp);
4421 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID);
4422 NFSZERO_ATTRBIT(&attrbits);
4423 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_ACL);
4424 (void) nfsv4_fillattr(nd, vnode_mount(vp), vp, aclp, NULL, NULL, 0,
4425 &attrbits, NULL, NULL, 0, 0, 0, 0, (uint64_t)0);
4426 error = nfscl_request(nd, vp, p, cred, stuff);
4429 /* Don't care about the pre/postop attributes */
4430 mbuf_freem(nd->nd_mrep);
4431 return (nd->nd_repstat);
4435 * Do the NFSv4.1 Exchange ID.
4438 nfsrpc_exchangeid(struct nfsmount *nmp, struct nfsclclient *clp,
4439 struct nfssockreq *nrp, uint32_t exchflags, struct nfsclds **dspp,
4440 struct ucred *cred, NFSPROC_T *p)
4442 uint32_t *tl, v41flags;
4443 struct nfsrv_descript nfsd;
4444 struct nfsrv_descript *nd = &nfsd;
4445 struct nfsclds *dsp;
4446 struct timespec verstime;
4450 nfscl_reqstart(nd, NFSPROC_EXCHANGEID, nmp, NULL, 0, NULL, NULL);
4451 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_UNSIGNED);
4452 *tl++ = txdr_unsigned(nfsboottime.tv_sec); /* Client owner */
4453 *tl = txdr_unsigned(clp->nfsc_rev);
4454 (void) nfsm_strtom(nd, clp->nfsc_id, clp->nfsc_idlen);
4456 NFSM_BUILD(tl, uint32_t *, 3 * NFSX_UNSIGNED);
4457 *tl++ = txdr_unsigned(exchflags);
4458 *tl++ = txdr_unsigned(NFSV4EXCH_SP4NONE);
4460 /* Set the implementation id4 */
4461 *tl = txdr_unsigned(1);
4462 (void) nfsm_strtom(nd, "freebsd.org", strlen("freebsd.org"));
4463 (void) nfsm_strtom(nd, version, strlen(version));
4464 NFSM_BUILD(tl, uint32_t *, NFSX_V4TIME);
4465 verstime.tv_sec = 1293840000; /* Jan 1, 2011 */
4466 verstime.tv_nsec = 0;
4467 txdr_nfsv4time(&verstime, tl);
4468 nd->nd_flag |= ND_USEGSSNAME;
4469 error = newnfs_request(nd, nmp, NULL, nrp, NULL, p, cred,
4470 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4471 NFSCL_DEBUG(1, "exchangeid err=%d reps=%d\n", error,
4472 (int)nd->nd_repstat);
4475 if (nd->nd_repstat == 0) {
4476 NFSM_DISSECT(tl, uint32_t *, 6 * NFSX_UNSIGNED + NFSX_HYPER);
4477 len = fxdr_unsigned(int, *(tl + 7));
4478 if (len < 0 || len > NFSV4_OPAQUELIMIT) {
4479 error = NFSERR_BADXDR;
4482 dsp = malloc(sizeof(struct nfsclds) + len, M_NFSCLDS,
4484 dsp->nfsclds_expire = NFSD_MONOSEC + clp->nfsc_renew;
4485 dsp->nfsclds_servownlen = len;
4486 dsp->nfsclds_sess.nfsess_clientid.lval[0] = *tl++;
4487 dsp->nfsclds_sess.nfsess_clientid.lval[1] = *tl++;
4488 dsp->nfsclds_sess.nfsess_sequenceid =
4489 fxdr_unsigned(uint32_t, *tl++);
4490 v41flags = fxdr_unsigned(uint32_t, *tl);
4491 if ((v41flags & NFSV4EXCH_USEPNFSMDS) != 0 &&
4492 NFSHASPNFSOPT(nmp)) {
4493 NFSCL_DEBUG(1, "set PNFS\n");
4495 nmp->nm_state |= NFSSTA_PNFS;
4497 dsp->nfsclds_flags |= NFSCLDS_MDS;
4499 if ((v41flags & NFSV4EXCH_USEPNFSDS) != 0)
4500 dsp->nfsclds_flags |= NFSCLDS_DS;
4502 nd->nd_repstat = nfsrv_mtostr(nd,
4503 dsp->nfsclds_serverown, len);
4504 if (nd->nd_repstat == 0) {
4505 mtx_init(&dsp->nfsclds_mtx, "nfsds", NULL, MTX_DEF);
4506 mtx_init(&dsp->nfsclds_sess.nfsess_mtx, "nfssession",
4508 nfscl_initsessionslots(&dsp->nfsclds_sess);
4511 free(dsp, M_NFSCLDS);
4513 error = nd->nd_repstat;
4515 mbuf_freem(nd->nd_mrep);
4520 * Do the NFSv4.1 Create Session.
4523 nfsrpc_createsession(struct nfsmount *nmp, struct nfsclsession *sep,
4524 struct nfssockreq *nrp, uint32_t sequenceid, int mds, struct ucred *cred,
4527 uint32_t crflags, *tl;
4528 struct nfsrv_descript nfsd;
4529 struct nfsrv_descript *nd = &nfsd;
4532 nfscl_reqstart(nd, NFSPROC_CREATESESSION, nmp, NULL, 0, NULL, NULL);
4533 NFSM_BUILD(tl, uint32_t *, 4 * NFSX_UNSIGNED);
4534 *tl++ = sep->nfsess_clientid.lval[0];
4535 *tl++ = sep->nfsess_clientid.lval[1];
4536 *tl++ = txdr_unsigned(sequenceid);
4537 crflags = (NFSMNT_RDONLY(nmp->nm_mountp) ? 0 : NFSV4CRSESS_PERSIST);
4538 if (nfscl_enablecallb != 0 && nfs_numnfscbd > 0)
4539 crflags |= NFSV4CRSESS_CONNBACKCHAN;
4540 *tl = txdr_unsigned(crflags);
4542 /* Fill in fore channel attributes. */
4543 NFSM_BUILD(tl, uint32_t *, 7 * NFSX_UNSIGNED);
4544 *tl++ = 0; /* Header pad size */
4545 *tl++ = txdr_unsigned(100000); /* Max request size */
4546 *tl++ = txdr_unsigned(100000); /* Max response size */
4547 *tl++ = txdr_unsigned(4096); /* Max response size cached */
4548 *tl++ = txdr_unsigned(20); /* Max operations */
4549 *tl++ = txdr_unsigned(64); /* Max slots */
4550 *tl = 0; /* No rdma ird */
4552 /* Fill in back channel attributes. */
4553 NFSM_BUILD(tl, uint32_t *, 7 * NFSX_UNSIGNED);
4554 *tl++ = 0; /* Header pad size */
4555 *tl++ = txdr_unsigned(10000); /* Max request size */
4556 *tl++ = txdr_unsigned(10000); /* Max response size */
4557 *tl++ = txdr_unsigned(4096); /* Max response size cached */
4558 *tl++ = txdr_unsigned(4); /* Max operations */
4559 *tl++ = txdr_unsigned(NFSV4_CBSLOTS); /* Max slots */
4560 *tl = 0; /* No rdma ird */
4562 NFSM_BUILD(tl, uint32_t *, 8 * NFSX_UNSIGNED);
4563 *tl++ = txdr_unsigned(NFS_CALLBCKPROG); /* Call back prog # */
4565 /* Allow AUTH_SYS callbacks as uid, gid == 0. */
4566 *tl++ = txdr_unsigned(1); /* Auth_sys only */
4567 *tl++ = txdr_unsigned(AUTH_SYS); /* AUTH_SYS type */
4568 *tl++ = txdr_unsigned(nfsboottime.tv_sec); /* time stamp */
4569 *tl++ = 0; /* Null machine name */
4570 *tl++ = 0; /* Uid == 0 */
4571 *tl++ = 0; /* Gid == 0 */
4572 *tl = 0; /* No additional gids */
4573 nd->nd_flag |= ND_USEGSSNAME;
4574 error = newnfs_request(nd, nmp, NULL, nrp, NULL, p, cred, NFS_PROG,
4575 NFS_VER4, NULL, 1, NULL, NULL);
4578 if (nd->nd_repstat == 0) {
4579 NFSM_DISSECT(tl, uint32_t *, NFSX_V4SESSIONID +
4581 bcopy(tl, sep->nfsess_sessionid, NFSX_V4SESSIONID);
4582 tl += NFSX_V4SESSIONID / NFSX_UNSIGNED;
4583 sep->nfsess_sequenceid = fxdr_unsigned(uint32_t, *tl++);
4584 crflags = fxdr_unsigned(uint32_t, *tl);
4585 if ((crflags & NFSV4CRSESS_PERSIST) != 0 && mds != 0) {
4587 nmp->nm_state |= NFSSTA_SESSPERSIST;
4591 /* Get the fore channel slot count. */
4592 NFSM_DISSECT(tl, uint32_t *, 7 * NFSX_UNSIGNED);
4593 tl += 3; /* Skip the other counts. */
4594 sep->nfsess_maxcache = fxdr_unsigned(int, *tl++);
4596 sep->nfsess_foreslots = fxdr_unsigned(uint16_t, *tl++);
4597 NFSCL_DEBUG(4, "fore slots=%d\n", (int)sep->nfsess_foreslots);
4598 irdcnt = fxdr_unsigned(int, *tl);
4600 NFSM_DISSECT(tl, uint32_t *, irdcnt * NFSX_UNSIGNED);
4602 /* and the back channel slot count. */
4603 NFSM_DISSECT(tl, uint32_t *, 7 * NFSX_UNSIGNED);
4605 sep->nfsess_backslots = fxdr_unsigned(uint16_t, *tl);
4606 NFSCL_DEBUG(4, "back slots=%d\n", (int)sep->nfsess_backslots);
4608 error = nd->nd_repstat;
4610 mbuf_freem(nd->nd_mrep);
4615 * Do the NFSv4.1 Destroy Session.
4618 nfsrpc_destroysession(struct nfsmount *nmp, struct nfsclclient *clp,
4619 struct ucred *cred, NFSPROC_T *p)
4622 struct nfsrv_descript nfsd;
4623 struct nfsrv_descript *nd = &nfsd;
4626 nfscl_reqstart(nd, NFSPROC_DESTROYSESSION, nmp, NULL, 0, NULL, NULL);
4627 NFSM_BUILD(tl, uint32_t *, NFSX_V4SESSIONID);
4628 bcopy(NFSMNT_MDSSESSION(nmp)->nfsess_sessionid, tl, NFSX_V4SESSIONID);
4629 nd->nd_flag |= ND_USEGSSNAME;
4630 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4631 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4634 error = nd->nd_repstat;
4635 mbuf_freem(nd->nd_mrep);
4640 * Do the NFSv4.1 Destroy Client.
4643 nfsrpc_destroyclient(struct nfsmount *nmp, struct nfsclclient *clp,
4644 struct ucred *cred, NFSPROC_T *p)
4647 struct nfsrv_descript nfsd;
4648 struct nfsrv_descript *nd = &nfsd;
4651 nfscl_reqstart(nd, NFSPROC_DESTROYCLIENT, nmp, NULL, 0, NULL, NULL);
4652 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_UNSIGNED);
4653 *tl++ = NFSMNT_MDSSESSION(nmp)->nfsess_clientid.lval[0];
4654 *tl = NFSMNT_MDSSESSION(nmp)->nfsess_clientid.lval[1];
4655 nd->nd_flag |= ND_USEGSSNAME;
4656 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4657 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4660 error = nd->nd_repstat;
4661 mbuf_freem(nd->nd_mrep);
4666 * Do the NFSv4.1 LayoutGet.
4669 nfsrpc_layoutget(struct nfsmount *nmp, uint8_t *fhp, int fhlen, int iomode,
4670 uint64_t offset, uint64_t len, uint64_t minlen, int layoutlen,
4671 nfsv4stateid_t *stateidp, int *retonclosep, struct nfsclflayouthead *flhp,
4672 struct ucred *cred, NFSPROC_T *p, void *stuff)
4675 struct nfsrv_descript nfsd, *nd = &nfsd;
4677 struct nfsclflayout *flp, *prevflp, *tflp;
4678 int cnt, error, gotiomode, fhcnt, nfhlen, i, j;
4684 nfscl_reqstart(nd, NFSPROC_LAYOUTGET, nmp, fhp, fhlen, NULL, NULL);
4685 NFSM_BUILD(tl, uint32_t *, 4 * NFSX_UNSIGNED + 3 * NFSX_HYPER +
4687 *tl++ = newnfs_false; /* Don't signal availability. */
4688 *tl++ = txdr_unsigned(NFSLAYOUT_NFSV4_1_FILES);
4689 *tl++ = txdr_unsigned(iomode);
4690 txdr_hyper(offset, tl);
4692 txdr_hyper(len, tl);
4694 txdr_hyper(minlen, tl);
4696 *tl++ = txdr_unsigned(stateidp->seqid);
4697 NFSCL_DEBUG(4, "layget seq=%d\n", (int)stateidp->seqid);
4698 *tl++ = stateidp->other[0];
4699 *tl++ = stateidp->other[1];
4700 *tl++ = stateidp->other[2];
4701 *tl = txdr_unsigned(layoutlen);
4702 nd->nd_flag |= ND_USEGSSNAME;
4703 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4704 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4707 if (nd->nd_repstat == 0) {
4708 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED + NFSX_STATEID);
4713 stateidp->seqid = fxdr_unsigned(uint32_t, *tl++);
4714 NFSCL_DEBUG(4, "retoncls=%d stseq=%d\n", *retonclosep,
4715 (int)stateidp->seqid);
4716 stateidp->other[0] = *tl++;
4717 stateidp->other[1] = *tl++;
4718 stateidp->other[2] = *tl++;
4719 cnt = fxdr_unsigned(int, *tl);
4720 NFSCL_DEBUG(4, "layg cnt=%d\n", cnt);
4721 if (cnt <= 0 || cnt > 10000) {
4722 /* Don't accept more than 10000 layouts in reply. */
4723 error = NFSERR_BADXDR;
4726 for (i = 0; i < cnt; i++) {
4727 /* Dissect all the way to the file handle cnt. */
4728 NFSM_DISSECT(tl, uint32_t *, 3 * NFSX_HYPER +
4729 6 * NFSX_UNSIGNED + NFSX_V4DEVICEID);
4730 fhcnt = fxdr_unsigned(int, *(tl + 11 +
4731 NFSX_V4DEVICEID / NFSX_UNSIGNED));
4732 NFSCL_DEBUG(4, "fhcnt=%d\n", fhcnt);
4733 if (fhcnt < 0 || fhcnt > 100) {
4734 /* Don't accept more than 100 file handles. */
4735 error = NFSERR_BADXDR;
4739 flp = malloc(sizeof(*flp) + (fhcnt - 1) *
4740 sizeof(struct nfsfh *),
4741 M_NFSFLAYOUT, M_WAITOK);
4743 flp = malloc(sizeof(*flp),
4744 M_NFSFLAYOUT, M_WAITOK);
4745 flp->nfsfl_flags = 0;
4746 flp->nfsfl_fhcnt = 0;
4747 flp->nfsfl_devp = NULL;
4748 flp->nfsfl_off = fxdr_hyper(tl); tl += 2;
4749 retlen = fxdr_hyper(tl); tl += 2;
4750 if (flp->nfsfl_off + retlen < flp->nfsfl_off)
4751 flp->nfsfl_end = UINT64_MAX - flp->nfsfl_off;
4753 flp->nfsfl_end = flp->nfsfl_off + retlen;
4754 flp->nfsfl_iomode = fxdr_unsigned(int, *tl++);
4755 if (gotiomode == -1)
4756 gotiomode = flp->nfsfl_iomode;
4757 NFSCL_DEBUG(4, "layg reqiom=%d retiom=%d\n", iomode,
4758 (int)flp->nfsfl_iomode);
4759 if (fxdr_unsigned(int, *tl++) !=
4760 NFSLAYOUT_NFSV4_1_FILES) {
4761 printf("NFSv4.1: got non-files layout\n");
4762 error = NFSERR_BADXDR;
4765 NFSBCOPY(++tl, flp->nfsfl_dev, NFSX_V4DEVICEID);
4766 tl += (NFSX_V4DEVICEID / NFSX_UNSIGNED);
4767 flp->nfsfl_util = fxdr_unsigned(uint32_t, *tl++);
4768 NFSCL_DEBUG(4, "flutil=0x%x\n", flp->nfsfl_util);
4769 flp->nfsfl_stripe1 = fxdr_unsigned(uint32_t, *tl++);
4770 flp->nfsfl_patoff = fxdr_hyper(tl); tl += 2;
4771 if (fxdr_unsigned(int, *tl) != fhcnt) {
4772 printf("EEK! bad fhcnt\n");
4773 error = NFSERR_BADXDR;
4776 for (j = 0; j < fhcnt; j++) {
4777 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
4778 nfhlen = fxdr_unsigned(int, *tl);
4779 if (nfhlen <= 0 || nfhlen > NFSX_V4FHMAX) {
4780 error = NFSERR_BADXDR;
4783 nfhp = malloc(sizeof(*nfhp) + nfhlen - 1,
4785 flp->nfsfl_fh[j] = nfhp;
4787 nfhp->nfh_len = nfhlen;
4788 NFSM_DISSECT(cp, uint8_t *, NFSM_RNDUP(nfhlen));
4789 NFSBCOPY(cp, nfhp->nfh_fh, nfhlen);
4791 if (flp->nfsfl_iomode == gotiomode) {
4792 /* Keep the list in increasing offset order. */
4793 tflp = LIST_FIRST(flhp);
4795 while (tflp != NULL &&
4796 tflp->nfsfl_off < flp->nfsfl_off) {
4798 tflp = LIST_NEXT(tflp, nfsfl_list);
4800 if (prevflp == NULL)
4801 LIST_INSERT_HEAD(flhp, flp, nfsfl_list);
4803 LIST_INSERT_AFTER(prevflp, flp,
4806 printf("nfscl_layoutget(): got wrong iomode\n");
4807 nfscl_freeflayout(flp);
4812 if (nd->nd_repstat != 0 && error == 0)
4813 error = nd->nd_repstat;
4815 if (error != 0 && flp != NULL)
4816 nfscl_freeflayout(flp);
4817 mbuf_freem(nd->nd_mrep);
4822 * Do the NFSv4.1 Get Device Info.
4825 nfsrpc_getdeviceinfo(struct nfsmount *nmp, uint8_t *deviceid, int layouttype,
4826 uint32_t *notifybitsp, struct nfscldevinfo **ndip, struct ucred *cred,
4830 struct nfsrv_descript nfsd;
4831 struct nfsrv_descript *nd = &nfsd;
4832 struct sockaddr_storage ss;
4833 struct nfsclds *dsp = NULL, **dspp;
4834 struct nfscldevinfo *ndi;
4835 int addrcnt, bitcnt, error, i, isudp, j, pos, safilled, stripecnt;
4836 uint8_t stripeindex;
4840 nfscl_reqstart(nd, NFSPROC_GETDEVICEINFO, nmp, NULL, 0, NULL, NULL);
4841 NFSM_BUILD(tl, uint32_t *, NFSX_V4DEVICEID + 3 * NFSX_UNSIGNED);
4842 NFSBCOPY(deviceid, tl, NFSX_V4DEVICEID);
4843 tl += (NFSX_V4DEVICEID / NFSX_UNSIGNED);
4844 *tl++ = txdr_unsigned(layouttype);
4845 *tl++ = txdr_unsigned(100000);
4846 if (notifybitsp != NULL && *notifybitsp != 0) {
4847 *tl = txdr_unsigned(1); /* One word of bits. */
4848 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
4849 *tl = txdr_unsigned(*notifybitsp);
4851 *tl = txdr_unsigned(0);
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 if (nd->nd_repstat == 0) {
4858 NFSM_DISSECT(tl, uint32_t *, 3 * NFSX_UNSIGNED);
4859 if (layouttype != fxdr_unsigned(int, *tl++))
4860 printf("EEK! devinfo layout type not same!\n");
4861 stripecnt = fxdr_unsigned(int, *++tl);
4862 NFSCL_DEBUG(4, "stripecnt=%d\n", stripecnt);
4863 if (stripecnt < 1 || stripecnt > 4096) {
4864 printf("NFS devinfo stripecnt %d: out of range\n",
4866 error = NFSERR_BADXDR;
4869 NFSM_DISSECT(tl, uint32_t *, (stripecnt + 1) * NFSX_UNSIGNED);
4870 addrcnt = fxdr_unsigned(int, *(tl + stripecnt));
4871 NFSCL_DEBUG(4, "addrcnt=%d\n", addrcnt);
4872 if (addrcnt < 1 || addrcnt > 128) {
4873 printf("NFS devinfo addrcnt %d: out of range\n",
4875 error = NFSERR_BADXDR;
4880 * Now we know how many stripe indices and addresses, so
4881 * we can allocate the structure the correct size.
4883 i = (stripecnt * sizeof(uint8_t)) / sizeof(struct nfsclds *)
4885 NFSCL_DEBUG(4, "stripeindices=%d\n", i);
4886 ndi = malloc(sizeof(*ndi) + (addrcnt + i) *
4887 sizeof(struct nfsclds *), M_NFSDEVINFO, M_WAITOK | M_ZERO);
4888 NFSBCOPY(deviceid, ndi->nfsdi_deviceid, NFSX_V4DEVICEID);
4889 ndi->nfsdi_refcnt = 0;
4890 ndi->nfsdi_stripecnt = stripecnt;
4891 ndi->nfsdi_addrcnt = addrcnt;
4892 /* Fill in the stripe indices. */
4893 for (i = 0; i < stripecnt; i++) {
4894 stripeindex = fxdr_unsigned(uint8_t, *tl++);
4895 NFSCL_DEBUG(4, "stripeind=%d\n", stripeindex);
4896 if (stripeindex >= addrcnt) {
4897 printf("NFS devinfo stripeindex %d: too big\n",
4899 error = NFSERR_BADXDR;
4902 nfsfldi_setstripeindex(ndi, i, stripeindex);
4905 /* Now, dissect the server address(es). */
4907 for (i = 0; i < addrcnt; i++) {
4908 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
4909 cnt = fxdr_unsigned(uint32_t, *tl);
4911 printf("NFS devinfo 0 len addrlist\n");
4912 error = NFSERR_BADXDR;
4915 dspp = nfsfldi_addr(ndi, i);
4916 pos = arc4random() % cnt; /* Choose one. */
4918 for (j = 0; j < cnt; j++) {
4919 error = nfsv4_getipaddr(nd, &ss, &isudp);
4920 if (error != 0 && error != EPERM) {
4921 error = NFSERR_BADXDR;
4924 if (error == 0 && isudp == 0) {
4927 * - use "pos" entry if it is of the
4928 * same af_family or none of them
4929 * is of the same af_family
4931 * - use the first one of the same
4934 if ((safilled == 0 && ss.ss_family ==
4935 nmp->nm_nam->sa_family) ||
4937 (safilled == 0 || ss.ss_family ==
4938 nmp->nm_nam->sa_family)) ||
4939 (safilled == 1 && ss.ss_family ==
4940 nmp->nm_nam->sa_family)) {
4941 error = nfsrpc_fillsa(nmp, &ss,
4946 nmp->nm_nam->sa_family)
4958 /* And the notify bits. */
4959 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
4960 if (safilled != 0) {
4961 bitcnt = fxdr_unsigned(int, *tl);
4963 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
4964 if (notifybitsp != NULL)
4966 fxdr_unsigned(uint32_t, *tl);
4972 if (nd->nd_repstat != 0)
4973 error = nd->nd_repstat;
4975 if (error != 0 && ndi != NULL)
4976 nfscl_freedevinfo(ndi);
4977 mbuf_freem(nd->nd_mrep);
4982 * Do the NFSv4.1 LayoutCommit.
4985 nfsrpc_layoutcommit(struct nfsmount *nmp, uint8_t *fh, int fhlen, int reclaim,
4986 uint64_t off, uint64_t len, uint64_t lastbyte, nfsv4stateid_t *stateidp,
4987 int layouttype, int layoutupdatecnt, uint8_t *layp, struct ucred *cred,
4988 NFSPROC_T *p, void *stuff)
4991 struct nfsrv_descript nfsd, *nd = &nfsd;
4992 int error, outcnt, i;
4995 nfscl_reqstart(nd, NFSPROC_LAYOUTCOMMIT, nmp, fh, fhlen, NULL, NULL);
4996 NFSM_BUILD(tl, uint32_t *, 5 * NFSX_UNSIGNED + 3 * NFSX_HYPER +
4998 txdr_hyper(off, tl);
5000 txdr_hyper(len, tl);
5003 *tl++ = newnfs_true;
5005 *tl++ = newnfs_false;
5006 *tl++ = txdr_unsigned(stateidp->seqid);
5007 *tl++ = stateidp->other[0];
5008 *tl++ = stateidp->other[1];
5009 *tl++ = stateidp->other[2];
5010 *tl++ = newnfs_true;
5013 else if (lastbyte >= (off + len))
5014 lastbyte = off + len - 1;
5015 txdr_hyper(lastbyte, tl);
5017 *tl++ = newnfs_false;
5018 *tl++ = txdr_unsigned(layouttype);
5019 *tl = txdr_unsigned(layoutupdatecnt);
5020 if (layoutupdatecnt > 0) {
5021 KASSERT(layouttype != NFSLAYOUT_NFSV4_1_FILES,
5022 ("Must be nil for Files Layout"));
5023 outcnt = NFSM_RNDUP(layoutupdatecnt);
5024 NFSM_BUILD(cp, uint8_t *, outcnt);
5025 NFSBCOPY(layp, cp, layoutupdatecnt);
5026 cp += layoutupdatecnt;
5027 for (i = 0; i < (outcnt - layoutupdatecnt); i++)
5030 nd->nd_flag |= ND_USEGSSNAME;
5031 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
5032 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
5035 error = nd->nd_repstat;
5036 mbuf_freem(nd->nd_mrep);
5041 * Do the NFSv4.1 LayoutReturn.
5044 nfsrpc_layoutreturn(struct nfsmount *nmp, uint8_t *fh, int fhlen, int reclaim,
5045 int layouttype, uint32_t iomode, int layoutreturn, uint64_t offset,
5046 uint64_t len, nfsv4stateid_t *stateidp, int layoutcnt, uint32_t *layp,
5047 struct ucred *cred, NFSPROC_T *p, void *stuff)
5050 struct nfsrv_descript nfsd, *nd = &nfsd;
5051 int error, outcnt, i;
5054 nfscl_reqstart(nd, NFSPROC_LAYOUTRETURN, nmp, fh, fhlen, NULL, NULL);
5055 NFSM_BUILD(tl, uint32_t *, 4 * NFSX_UNSIGNED);
5057 *tl++ = newnfs_true;
5059 *tl++ = newnfs_false;
5060 *tl++ = txdr_unsigned(layouttype);
5061 *tl++ = txdr_unsigned(iomode);
5062 *tl = txdr_unsigned(layoutreturn);
5063 if (layoutreturn == NFSLAYOUTRETURN_FILE) {
5064 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_HYPER + NFSX_STATEID +
5066 txdr_hyper(offset, tl);
5068 txdr_hyper(len, tl);
5070 NFSCL_DEBUG(4, "layoutret stseq=%d\n", (int)stateidp->seqid);
5071 *tl++ = txdr_unsigned(stateidp->seqid);
5072 *tl++ = stateidp->other[0];
5073 *tl++ = stateidp->other[1];
5074 *tl++ = stateidp->other[2];
5075 *tl = txdr_unsigned(layoutcnt);
5076 if (layoutcnt > 0) {
5077 outcnt = NFSM_RNDUP(layoutcnt);
5078 NFSM_BUILD(cp, uint8_t *, outcnt);
5079 NFSBCOPY(layp, cp, layoutcnt);
5081 for (i = 0; i < (outcnt - layoutcnt); i++)
5085 nd->nd_flag |= ND_USEGSSNAME;
5086 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
5087 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
5090 if (nd->nd_repstat == 0) {
5091 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
5093 NFSM_DISSECT(tl, uint32_t *, NFSX_STATEID);
5094 stateidp->seqid = fxdr_unsigned(uint32_t, *tl++);
5095 stateidp->other[0] = *tl++;
5096 stateidp->other[1] = *tl++;
5097 stateidp->other[2] = *tl;
5100 error = nd->nd_repstat;
5102 mbuf_freem(nd->nd_mrep);
5107 * Acquire a layout and devinfo, if possible. The caller must have acquired
5108 * a reference count on the nfsclclient structure before calling this.
5109 * Return the layout in lypp with a reference count on it, if successful.
5112 nfsrpc_getlayout(struct nfsmount *nmp, vnode_t vp, struct nfsfh *nfhp,
5113 int iomode, uint32_t *notifybitsp, nfsv4stateid_t *stateidp, uint64_t off,
5114 struct nfscllayout **lypp, struct ucred *cred, NFSPROC_T *p)
5116 struct nfscllayout *lyp;
5117 struct nfsclflayout *flp, *tflp;
5118 struct nfscldevinfo *dip;
5119 struct nfsclflayouthead flh;
5120 int error = 0, islocked, layoutlen, recalled, retonclose;
5121 nfsv4stateid_t stateid;
5125 * If lyp is returned non-NULL, there will be a refcnt (shared lock)
5126 * on it, iff flp != NULL or a lock (exclusive lock) on it iff
5129 lyp = nfscl_getlayout(nmp->nm_clp, nfhp->nfh_fh, nfhp->nfh_len,
5130 off, &flp, &recalled);
5132 if (lyp == NULL || flp == NULL) {
5136 layoutlen = NFSMNT_MDSSESSION(nmp)->nfsess_maxcache -
5137 (NFSX_STATEID + 3 * NFSX_UNSIGNED);
5140 stateid.other[0] = stateidp->other[0];
5141 stateid.other[1] = stateidp->other[1];
5142 stateid.other[2] = stateidp->other[2];
5143 error = nfsrpc_layoutget(nmp, nfhp->nfh_fh,
5144 nfhp->nfh_len, iomode, (uint64_t)0, INT64_MAX,
5145 (uint64_t)0, layoutlen, &stateid, &retonclose,
5146 &flh, cred, p, NULL);
5149 stateid.seqid = lyp->nfsly_stateid.seqid;
5150 stateid.other[0] = lyp->nfsly_stateid.other[0];
5151 stateid.other[1] = lyp->nfsly_stateid.other[1];
5152 stateid.other[2] = lyp->nfsly_stateid.other[2];
5153 error = nfsrpc_layoutget(nmp, nfhp->nfh_fh,
5154 nfhp->nfh_len, iomode, off, INT64_MAX,
5155 (uint64_t)0, layoutlen, &stateid, &retonclose,
5156 &flh, cred, p, NULL);
5159 LIST_FOREACH(tflp, &flh, nfsfl_list) {
5160 error = nfscl_adddevinfo(nmp, NULL, tflp);
5162 error = nfsrpc_getdeviceinfo(nmp,
5164 NFSLAYOUT_NFSV4_1_FILES,
5165 notifybitsp, &dip, cred, p);
5168 error = nfscl_adddevinfo(nmp, dip,
5172 "getlayout: cannot add\n");
5177 * nfscl_layout() always returns with the nfsly_lock
5178 * set to a refcnt (shared lock).
5180 error = nfscl_layout(nmp, vp, nfhp->nfh_fh,
5181 nfhp->nfh_len, &stateid, retonclose, &flh, &lyp,
5185 } else if (islocked != 0)
5186 nfsv4_unlock(&lyp->nfsly_lock, 0);
5193 * Do a TCP connection plus exchange id and create session.
5194 * If successful, a "struct nfsclds" is linked into the list for the
5195 * mount point and a pointer to it is returned.
5198 nfsrpc_fillsa(struct nfsmount *nmp, struct sockaddr_storage *ssp,
5199 struct nfsclds **dspp, NFSPROC_T *p)
5201 struct sockaddr_in *msad, *sad, *ssd;
5202 struct sockaddr_in6 *msad6, *sad6, *ssd6;
5203 struct nfsclclient *clp;
5204 struct nfssockreq *nrp;
5205 struct nfsclds *dsp, *tdsp;
5207 enum nfsclds_state retv;
5208 uint32_t sequenceid;
5210 KASSERT(nmp->nm_sockreq.nr_cred != NULL,
5211 ("nfsrpc_fillsa: NULL nr_cred"));
5217 if (ssp->ss_family == AF_INET) {
5218 ssd = (struct sockaddr_in *)ssp;
5222 * Check to see if we already have a session for this
5223 * address that is usable for a DS.
5224 * Note that the MDS's address is in a different place
5225 * than the sessions already acquired for DS's.
5227 msad = (struct sockaddr_in *)nmp->nm_sockreq.nr_nam;
5228 tdsp = TAILQ_FIRST(&nmp->nm_sess);
5229 while (tdsp != NULL) {
5230 if (msad != NULL && msad->sin_family == AF_INET &&
5231 ssd->sin_addr.s_addr == msad->sin_addr.s_addr &&
5232 ssd->sin_port == msad->sin_port &&
5233 (tdsp->nfsclds_flags & NFSCLDS_DS) != 0) {
5236 NFSCL_DEBUG(4, "fnd same addr\n");
5239 tdsp = TAILQ_NEXT(tdsp, nfsclds_list);
5240 if (tdsp != NULL && tdsp->nfsclds_sockp != NULL)
5241 msad = (struct sockaddr_in *)
5242 tdsp->nfsclds_sockp->nr_nam;
5248 /* No IP address match, so look for new/trunked one. */
5249 sad = malloc(sizeof(*sad), M_SONAME, M_WAITOK | M_ZERO);
5250 sad->sin_len = sizeof(*sad);
5251 sad->sin_family = AF_INET;
5252 sad->sin_port = ssd->sin_port;
5253 sad->sin_addr.s_addr = ssd->sin_addr.s_addr;
5254 nrp = malloc(sizeof(*nrp), M_NFSSOCKREQ, M_WAITOK | M_ZERO);
5255 nrp->nr_nam = (struct sockaddr *)sad;
5256 } else if (ssp->ss_family == AF_INET6) {
5257 ssd6 = (struct sockaddr_in6 *)ssp;
5261 * Check to see if we already have a session for this
5262 * address that is usable for a DS.
5263 * Note that the MDS's address is in a different place
5264 * than the sessions already acquired for DS's.
5266 msad6 = (struct sockaddr_in6 *)nmp->nm_sockreq.nr_nam;
5267 tdsp = TAILQ_FIRST(&nmp->nm_sess);
5268 while (tdsp != NULL) {
5269 if (msad6 != NULL && msad6->sin6_family == AF_INET6 &&
5270 IN6_ARE_ADDR_EQUAL(&ssd6->sin6_addr,
5271 &msad6->sin6_addr) &&
5272 ssd6->sin6_port == msad6->sin6_port &&
5273 (tdsp->nfsclds_flags & NFSCLDS_DS) != 0) {
5278 tdsp = TAILQ_NEXT(tdsp, nfsclds_list);
5279 if (tdsp != NULL && tdsp->nfsclds_sockp != NULL)
5280 msad6 = (struct sockaddr_in6 *)
5281 tdsp->nfsclds_sockp->nr_nam;
5287 /* No IP address match, so look for new/trunked one. */
5288 sad6 = malloc(sizeof(*sad6), M_SONAME, M_WAITOK | M_ZERO);
5289 sad6->sin6_len = sizeof(*sad6);
5290 sad6->sin6_family = AF_INET6;
5291 sad6->sin6_port = ssd6->sin6_port;
5292 NFSBCOPY(&ssd6->sin6_addr, &sad6->sin6_addr,
5293 sizeof(struct in6_addr));
5294 nrp = malloc(sizeof(*nrp), M_NFSSOCKREQ, M_WAITOK | M_ZERO);
5295 nrp->nr_nam = (struct sockaddr *)sad6;
5299 nrp->nr_sotype = SOCK_STREAM;
5300 mtx_init(&nrp->nr_mtx, "nfssock", NULL, MTX_DEF);
5301 nrp->nr_prog = NFS_PROG;
5302 nrp->nr_vers = NFS_VER4;
5305 * Use the credentials that were used for the mount, which are
5306 * in nmp->nm_sockreq.nr_cred for newnfs_connect() etc.
5307 * Ref. counting the credentials with crhold() is probably not
5308 * necessary, since nm_sockreq.nr_cred won't be crfree()'d until
5309 * unmount, but I did it anyhow.
5311 nrp->nr_cred = crhold(nmp->nm_sockreq.nr_cred);
5312 error = newnfs_connect(nmp, nrp, NULL, p, 0);
5313 NFSCL_DEBUG(3, "DS connect=%d\n", error);
5315 /* Now, do the exchangeid and create session. */
5317 error = nfsrpc_exchangeid(nmp, clp, nrp, NFSV4EXCH_USEPNFSDS,
5318 &dsp, nrp->nr_cred, p);
5319 NFSCL_DEBUG(3, "DS exchangeid=%d\n", error);
5321 dsp->nfsclds_sockp = nrp;
5323 retv = nfscl_getsameserver(nmp, dsp, &tdsp);
5324 NFSCL_DEBUG(3, "getsame ret=%d\n", retv);
5325 if (retv == NFSDSP_USETHISSESSION) {
5328 * If there is already a session for this server,
5331 (void)newnfs_disconnect(nrp);
5332 nfscl_freenfsclds(dsp);
5336 if (retv == NFSDSP_SEQTHISSESSION)
5337 sequenceid = tdsp->nfsclds_sess.nfsess_sequenceid;
5339 sequenceid = dsp->nfsclds_sess.nfsess_sequenceid;
5341 error = nfsrpc_createsession(nmp, &dsp->nfsclds_sess,
5342 nrp, sequenceid, 0, nrp->nr_cred, p);
5343 NFSCL_DEBUG(3, "DS createsess=%d\n", error);
5345 NFSFREECRED(nrp->nr_cred);
5346 NFSFREEMUTEX(&nrp->nr_mtx);
5347 free(nrp->nr_nam, M_SONAME);
5348 free(nrp, M_NFSSOCKREQ);
5351 NFSCL_DEBUG(3, "add DS session\n");
5353 * Put it at the end of the list. That way the list
5354 * is ordered by when the entry was added. This matters
5355 * since the one done first is the one that should be
5356 * used for sequencid'ing any subsequent create sessions.
5359 TAILQ_INSERT_TAIL(&nmp->nm_sess, dsp, nfsclds_list);
5362 } else if (dsp != NULL)
5363 nfscl_freenfsclds(dsp);
5368 * Do the NFSv4.1 Reclaim Complete.
5371 nfsrpc_reclaimcomplete(struct nfsmount *nmp, struct ucred *cred, NFSPROC_T *p)
5374 struct nfsrv_descript nfsd;
5375 struct nfsrv_descript *nd = &nfsd;
5378 nfscl_reqstart(nd, NFSPROC_RECLAIMCOMPL, nmp, NULL, 0, NULL, NULL);
5379 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
5381 nd->nd_flag |= ND_USEGSSNAME;
5382 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
5383 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
5386 error = nd->nd_repstat;
5387 mbuf_freem(nd->nd_mrep);
5392 * Initialize the slot tables for a session.
5395 nfscl_initsessionslots(struct nfsclsession *sep)
5399 for (i = 0; i < NFSV4_CBSLOTS; i++) {
5400 if (sep->nfsess_cbslots[i].nfssl_reply != NULL)
5401 m_freem(sep->nfsess_cbslots[i].nfssl_reply);
5402 NFSBZERO(&sep->nfsess_cbslots[i], sizeof(struct nfsslot));
5404 for (i = 0; i < 64; i++)
5405 sep->nfsess_slotseq[i] = 0;
5406 sep->nfsess_slots = 0;
5410 * Called to try and do an I/O operation via an NFSv4.1 Data Server (DS).
5413 nfscl_doiods(vnode_t vp, struct uio *uiop, int *iomode, int *must_commit,
5414 uint32_t rwaccess, struct ucred *cred, NFSPROC_T *p)
5416 struct nfsnode *np = VTONFS(vp);
5417 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
5418 struct nfscllayout *layp;
5419 struct nfscldevinfo *dip;
5420 struct nfsclflayout *rflp;
5421 nfsv4stateid_t stateid;
5422 struct ucred *newcred;
5423 uint64_t lastbyte, len, off, oresid, xfer;
5424 int eof, error, iolaymode, recalled;
5427 if (!NFSHASPNFS(nmp) || nfscl_enablecallb == 0 || nfs_numnfscbd == 0 ||
5428 (np->n_flag & NNOLAYOUT) != 0)
5430 /* Now, get a reference cnt on the clientid for this mount. */
5431 if (nfscl_getref(nmp) == 0)
5434 /* Find an appropriate stateid. */
5435 newcred = NFSNEWCRED(cred);
5436 error = nfscl_getstateid(vp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len,
5437 rwaccess, 1, newcred, p, &stateid, &lckp);
5439 NFSFREECRED(newcred);
5443 /* Search for a layout for this file. */
5444 off = uiop->uio_offset;
5445 layp = nfscl_getlayout(nmp->nm_clp, np->n_fhp->nfh_fh,
5446 np->n_fhp->nfh_len, off, &rflp, &recalled);
5447 if (layp == NULL || rflp == NULL) {
5448 if (recalled != 0) {
5449 NFSFREECRED(newcred);
5454 nfscl_rellayout(layp, (rflp == NULL) ? 1 : 0);
5457 /* Try and get a Layout, if it is supported. */
5458 if (rwaccess == NFSV4OPEN_ACCESSWRITE ||
5459 (np->n_flag & NWRITEOPENED) != 0)
5460 iolaymode = NFSLAYOUTIOMODE_RW;
5462 iolaymode = NFSLAYOUTIOMODE_READ;
5463 error = nfsrpc_getlayout(nmp, vp, np->n_fhp, iolaymode,
5464 NULL, &stateid, off, &layp, newcred, p);
5467 np->n_flag |= NNOLAYOUT;
5470 nfscl_lockderef(lckp);
5471 NFSFREECRED(newcred);
5473 nfscl_rellayout(layp, 0);
5480 * Loop around finding a layout that works for the first part of
5481 * this I/O operation, and then call the function that actually
5485 len = (uint64_t)uiop->uio_resid;
5486 while (len > 0 && error == 0 && eof == 0) {
5487 off = uiop->uio_offset;
5488 error = nfscl_findlayoutforio(layp, off, rwaccess, &rflp);
5490 oresid = xfer = (uint64_t)uiop->uio_resid;
5491 if (xfer > (rflp->nfsfl_end - rflp->nfsfl_off))
5492 xfer = rflp->nfsfl_end - rflp->nfsfl_off;
5493 dip = nfscl_getdevinfo(nmp->nm_clp, rflp->nfsfl_dev,
5496 error = nfscl_doflayoutio(vp, uiop, iomode,
5497 must_commit, &eof, &stateid, rwaccess, dip,
5498 layp, rflp, off, xfer, newcred, p);
5499 nfscl_reldevinfo(dip);
5500 lastbyte = off + xfer - 1;
5503 if (lastbyte > layp->nfsly_lastbyte)
5504 layp->nfsly_lastbyte = lastbyte;
5510 len -= (oresid - (uint64_t)uiop->uio_resid);
5514 nfscl_lockderef(lckp);
5515 NFSFREECRED(newcred);
5516 nfscl_rellayout(layp, 0);
5522 * Find a file layout that will handle the first bytes of the requested
5523 * range and return the information from it needed to to the I/O operation.
5526 nfscl_findlayoutforio(struct nfscllayout *lyp, uint64_t off, uint32_t rwaccess,
5527 struct nfsclflayout **retflpp)
5529 struct nfsclflayout *flp, *nflp, *rflp;
5534 /* For reading, do the Read list first and then the Write list. */
5536 if (rw == NFSV4OPEN_ACCESSREAD)
5537 flp = LIST_FIRST(&lyp->nfsly_flayread);
5539 flp = LIST_FIRST(&lyp->nfsly_flayrw);
5540 while (flp != NULL) {
5541 nflp = LIST_NEXT(flp, nfsfl_list);
5542 if (flp->nfsfl_off > off)
5544 if (flp->nfsfl_end > off &&
5545 (rflp == NULL || rflp->nfsfl_end < flp->nfsfl_end))
5549 if (rw == NFSV4OPEN_ACCESSREAD)
5550 rw = NFSV4OPEN_ACCESSWRITE;
5555 /* This one covers the most bytes starting at off. */
5563 * Do I/O using an NFSv4.1 file layout.
5566 nfscl_doflayoutio(vnode_t vp, struct uio *uiop, int *iomode, int *must_commit,
5567 int *eofp, nfsv4stateid_t *stateidp, int rwflag, struct nfscldevinfo *dp,
5568 struct nfscllayout *lyp, struct nfsclflayout *flp, uint64_t off,
5569 uint64_t len, struct ucred *cred, NFSPROC_T *p)
5571 uint64_t io_off, rel_off, stripe_unit_size, transfer, xfer;
5572 int commit_thru_mds, error = 0, stripe_index, stripe_pos;
5575 struct nfsclds **dspp;
5578 rel_off = off - flp->nfsfl_patoff;
5579 stripe_unit_size = (flp->nfsfl_util >> 6) & 0x3ffffff;
5580 stripe_pos = (rel_off / stripe_unit_size + flp->nfsfl_stripe1) %
5581 dp->nfsdi_stripecnt;
5582 transfer = stripe_unit_size - (rel_off % stripe_unit_size);
5584 /* Loop around, doing I/O for each stripe unit. */
5585 while (len > 0 && error == 0) {
5586 stripe_index = nfsfldi_stripeindex(dp, stripe_pos);
5587 dspp = nfsfldi_addr(dp, stripe_index);
5592 if ((flp->nfsfl_util & NFSFLAYUTIL_DENSE) != 0) {
5594 if (stripe_pos >= flp->nfsfl_fhcnt)
5596 fhp = flp->nfsfl_fh[stripe_pos];
5597 io_off = (rel_off / (stripe_unit_size *
5598 dp->nfsdi_stripecnt)) * stripe_unit_size +
5599 rel_off % stripe_unit_size;
5601 /* Sparse layout. */
5602 if (flp->nfsfl_fhcnt > 1) {
5603 if (stripe_index >= flp->nfsfl_fhcnt)
5605 fhp = flp->nfsfl_fh[stripe_index];
5606 } else if (flp->nfsfl_fhcnt == 1)
5607 fhp = flp->nfsfl_fh[0];
5612 if ((flp->nfsfl_util & NFSFLAYUTIL_COMMIT_THRU_MDS) != 0)
5613 commit_thru_mds = 1;
5615 commit_thru_mds = 0;
5616 if (rwflag == FREAD)
5617 error = nfsrpc_readds(vp, uiop, stateidp, eofp, *dspp,
5618 io_off, xfer, fhp, cred, p);
5620 error = nfsrpc_writeds(vp, uiop, iomode, must_commit,
5621 stateidp, *dspp, io_off, xfer, fhp, commit_thru_mds,
5625 lyp->nfsly_flags |= NFSLY_WRITTEN;
5630 transfer = stripe_unit_size;
5631 stripe_pos = (stripe_pos + 1) % dp->nfsdi_stripecnt;
5640 * The actual read RPC done to a DS.
5643 nfsrpc_readds(vnode_t vp, struct uio *uiop, nfsv4stateid_t *stateidp, int *eofp,
5644 struct nfsclds *dsp, uint64_t io_off, int len, struct nfsfh *fhp,
5645 struct ucred *cred, NFSPROC_T *p)
5649 struct nfsrv_descript nfsd;
5650 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
5651 struct nfsrv_descript *nd = &nfsd;
5652 struct nfssockreq *nrp;
5655 nfscl_reqstart(nd, NFSPROC_READDS, nmp, fhp->nfh_fh, fhp->nfh_len,
5656 NULL, &dsp->nfsclds_sess);
5657 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSEQIDZERO);
5658 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED * 3);
5659 txdr_hyper(io_off, tl);
5660 *(tl + 2) = txdr_unsigned(len);
5661 nrp = dsp->nfsclds_sockp;
5663 /* If NULL, use the MDS socket. */
5664 nrp = &nmp->nm_sockreq;
5665 error = newnfs_request(nd, nmp, NULL, nrp, vp, p, cred,
5666 NFS_PROG, NFS_VER4, NULL, 1, NULL, &dsp->nfsclds_sess);
5669 if (nd->nd_repstat != 0) {
5670 error = nd->nd_repstat;
5673 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
5674 *eofp = fxdr_unsigned(int, *tl);
5675 NFSM_STRSIZ(retlen, len);
5676 error = nfsm_mbufuio(nd, uiop, retlen);
5678 if (nd->nd_mrep != NULL)
5679 mbuf_freem(nd->nd_mrep);
5684 * The actual write RPC done to a DS.
5687 nfsrpc_writeds(vnode_t vp, struct uio *uiop, int *iomode, int *must_commit,
5688 nfsv4stateid_t *stateidp, struct nfsclds *dsp, uint64_t io_off, int len,
5689 struct nfsfh *fhp, int commit_thru_mds, struct ucred *cred, NFSPROC_T *p)
5692 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
5693 int error, rlen, commit, committed = NFSWRITE_FILESYNC;
5695 struct nfsrv_descript nfsd;
5696 struct nfsrv_descript *nd = &nfsd;
5697 struct nfssockreq *nrp;
5699 KASSERT(uiop->uio_iovcnt == 1, ("nfs: writerpc iovcnt > 1"));
5701 nfscl_reqstart(nd, NFSPROC_WRITEDS, nmp, fhp->nfh_fh, fhp->nfh_len,
5702 NULL, &dsp->nfsclds_sess);
5703 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSEQIDZERO);
5704 NFSM_BUILD(tl, uint32_t *, NFSX_HYPER + 2 * NFSX_UNSIGNED);
5705 txdr_hyper(io_off, tl);
5707 *tl++ = txdr_unsigned(*iomode);
5708 *tl = txdr_unsigned(len);
5709 nfsm_uiombuf(nd, uiop, len);
5710 nrp = dsp->nfsclds_sockp;
5712 /* If NULL, use the MDS socket. */
5713 nrp = &nmp->nm_sockreq;
5714 error = newnfs_request(nd, nmp, NULL, nrp, vp, p, cred,
5715 NFS_PROG, NFS_VER4, NULL, 1, NULL, &dsp->nfsclds_sess);
5718 if (nd->nd_repstat != 0) {
5720 * In case the rpc gets retried, roll
5721 * the uio fileds changed by nfsm_uiombuf()
5724 uiop->uio_offset -= len;
5725 uio_uio_resid_add(uiop, len);
5726 uio_iov_base_add(uiop, -len);
5727 uio_iov_len_add(uiop, len);
5728 error = nd->nd_repstat;
5730 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED + NFSX_VERF);
5731 rlen = fxdr_unsigned(int, *tl++);
5735 } else if (rlen < len) {
5736 backup = len - rlen;
5737 uio_iov_base_add(uiop, -(backup));
5738 uio_iov_len_add(uiop, backup);
5739 uiop->uio_offset -= backup;
5740 uio_uio_resid_add(uiop, backup);
5743 commit = fxdr_unsigned(int, *tl++);
5746 * Return the lowest committment level
5747 * obtained by any of the RPCs.
5749 if (committed == NFSWRITE_FILESYNC)
5751 else if (committed == NFSWRITE_DATASYNC &&
5752 commit == NFSWRITE_UNSTABLE)
5754 if (commit_thru_mds != 0) {
5756 if (!NFSHASWRITEVERF(nmp)) {
5757 NFSBCOPY(tl, nmp->nm_verf, NFSX_VERF);
5758 NFSSETWRITEVERF(nmp);
5759 } else if (NFSBCMP(tl, nmp->nm_verf, NFSX_VERF)) {
5761 NFSBCOPY(tl, nmp->nm_verf, NFSX_VERF);
5766 if ((dsp->nfsclds_flags & NFSCLDS_HASWRITEVERF) == 0) {
5767 NFSBCOPY(tl, dsp->nfsclds_verf, NFSX_VERF);
5768 dsp->nfsclds_flags |= NFSCLDS_HASWRITEVERF;
5769 } else if (NFSBCMP(tl, dsp->nfsclds_verf, NFSX_VERF)) {
5771 NFSBCOPY(tl, dsp->nfsclds_verf, NFSX_VERF);
5777 if (nd->nd_mrep != NULL)
5778 mbuf_freem(nd->nd_mrep);
5779 *iomode = committed;
5780 if (nd->nd_repstat != 0 && error == 0)
5781 error = nd->nd_repstat;
5786 * Free up the nfsclds structure.
5789 nfscl_freenfsclds(struct nfsclds *dsp)
5795 if (dsp->nfsclds_sockp != NULL) {
5796 NFSFREECRED(dsp->nfsclds_sockp->nr_cred);
5797 NFSFREEMUTEX(&dsp->nfsclds_sockp->nr_mtx);
5798 free(dsp->nfsclds_sockp->nr_nam, M_SONAME);
5799 free(dsp->nfsclds_sockp, M_NFSSOCKREQ);
5801 NFSFREEMUTEX(&dsp->nfsclds_mtx);
5802 NFSFREEMUTEX(&dsp->nfsclds_sess.nfsess_mtx);
5803 for (i = 0; i < NFSV4_CBSLOTS; i++) {
5804 if (dsp->nfsclds_sess.nfsess_cbslots[i].nfssl_reply != NULL)
5806 dsp->nfsclds_sess.nfsess_cbslots[i].nfssl_reply);
5808 free(dsp, M_NFSCLDS);
5811 static enum nfsclds_state
5812 nfscl_getsameserver(struct nfsmount *nmp, struct nfsclds *newdsp,
5813 struct nfsclds **retdspp)
5815 struct nfsclds *dsp, *cur_dsp;
5818 * Search the list of nfsclds structures for one with the same
5822 TAILQ_FOREACH(dsp, &nmp->nm_sess, nfsclds_list) {
5823 if (dsp->nfsclds_servownlen == newdsp->nfsclds_servownlen &&
5824 dsp->nfsclds_servownlen != 0 &&
5825 !NFSBCMP(dsp->nfsclds_serverown, newdsp->nfsclds_serverown,
5826 dsp->nfsclds_servownlen)) {
5827 NFSCL_DEBUG(4, "fnd same fdsp=%p dsp=%p flg=0x%x\n",
5828 TAILQ_FIRST(&nmp->nm_sess), dsp,
5829 dsp->nfsclds_flags);
5830 /* Server major id matches. */
5831 if ((dsp->nfsclds_flags & NFSCLDS_DS) != 0) {
5833 return (NFSDSP_USETHISSESSION);
5837 * Note the first match, so it can be used for
5838 * sequence'ing new sessions.
5840 if (cur_dsp == NULL)
5844 if (cur_dsp != NULL) {
5846 return (NFSDSP_SEQTHISSESSION);
5848 return (NFSDSP_NOTFOUND);
5853 * NFS commit rpc to a DS.
5856 nfsrpc_commitds(vnode_t vp, uint64_t offset, int cnt, struct nfsclds *dsp,
5857 struct nfsfh *fhp, struct ucred *cred, NFSPROC_T *p, void *stuff)
5860 struct nfsrv_descript nfsd, *nd = &nfsd;
5861 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
5862 struct nfssockreq *nrp;
5865 nfscl_reqstart(nd, NFSPROC_COMMITDS, nmp, fhp->nfh_fh, fhp->nfh_len,
5866 NULL, &dsp->nfsclds_sess);
5867 NFSM_BUILD(tl, uint32_t *, NFSX_HYPER + NFSX_UNSIGNED);
5868 txdr_hyper(offset, tl);
5870 *tl = txdr_unsigned(cnt);
5871 nrp = dsp->nfsclds_sockp;
5873 /* If NULL, use the MDS socket. */
5874 nrp = &nmp->nm_sockreq;
5875 error = newnfs_request(nd, nmp, NULL, nrp, vp, p, cred,
5876 NFS_PROG, NFS_VER4, NULL, 1, NULL, &dsp->nfsclds_sess);
5879 if (nd->nd_repstat == 0) {
5880 NFSM_DISSECT(tl, u_int32_t *, NFSX_VERF);
5882 if (NFSBCMP(tl, dsp->nfsclds_verf, NFSX_VERF)) {
5883 NFSBCOPY(tl, dsp->nfsclds_verf, NFSX_VERF);
5884 error = NFSERR_STALEWRITEVERF;
5889 if (error == 0 && nd->nd_repstat != 0)
5890 error = nd->nd_repstat;
5891 mbuf_freem(nd->nd_mrep);