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;
834 if (nfsboottime.tv_sec == 0)
835 NFSSETBOOTTIME(nfsboottime);
836 clp->nfsc_rev = rev++;
837 if (NFSHASNFSV4N(nmp)) {
838 error = nfsrpc_exchangeid(nmp, clp, &nmp->nm_sockreq,
839 NFSV4EXCH_USEPNFSMDS | NFSV4EXCH_USENONPNFS, &dsp, cred, p);
840 NFSCL_DEBUG(1, "aft exch=%d\n", error);
842 error = nfsrpc_createsession(nmp, &dsp->nfsclds_sess,
844 dsp->nfsclds_sess.nfsess_sequenceid, 1, cred, p);
847 TAILQ_FOREACH_SAFE(tdsp, &nmp->nm_sess,
849 nfscl_freenfsclds(tdsp);
850 TAILQ_INIT(&nmp->nm_sess);
851 TAILQ_INSERT_HEAD(&nmp->nm_sess, dsp,
855 nfscl_freenfsclds(dsp);
856 NFSCL_DEBUG(1, "aft createsess=%d\n", error);
858 if (error == 0 && reclaim == 0) {
859 error = nfsrpc_reclaimcomplete(nmp, cred, p);
860 NFSCL_DEBUG(1, "aft reclaimcomp=%d\n", error);
861 if (error == NFSERR_COMPLETEALREADY ||
862 error == NFSERR_NOTSUPP)
863 /* Ignore this error. */
870 * Allocate a single session structure for NFSv4.0, because some of
871 * the fields are used by NFSv4.0 although it doesn't do a session.
873 dsp = malloc(sizeof(struct nfsclds), M_NFSCLDS, M_WAITOK | M_ZERO);
874 mtx_init(&dsp->nfsclds_mtx, "nfsds", NULL, MTX_DEF);
875 mtx_init(&dsp->nfsclds_sess.nfsess_mtx, "nfssession", NULL, MTX_DEF);
877 TAILQ_INSERT_HEAD(&nmp->nm_sess, dsp, nfsclds_list);
880 nfscl_reqstart(nd, NFSPROC_SETCLIENTID, nmp, NULL, 0, NULL, NULL);
881 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
882 *tl++ = txdr_unsigned(nfsboottime.tv_sec);
883 *tl = txdr_unsigned(clp->nfsc_rev);
884 (void) nfsm_strtom(nd, clp->nfsc_id, clp->nfsc_idlen);
887 * set up the callback address
889 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
890 *tl = txdr_unsigned(NFS_CALLBCKPROG);
891 callblen = strlen(nfsv4_callbackaddr);
893 cp = nfscl_getmyip(nmp, &a6, &isinet6);
894 if (nfscl_enablecallb && nfs_numnfscbd > 0 &&
895 (callblen > 0 || cp != NULL)) {
896 port = htons(nfsv4_cbport);
897 cp2 = (u_int8_t *)&port;
900 strchr(nfsv4_callbackaddr, ':')) || isinet6) {
901 char ip6buf[INET6_ADDRSTRLEN], *ip6add;
903 (void) nfsm_strtom(nd, "tcp6", 4);
905 ip6_sprintf(ip6buf, (struct in6_addr *)cp);
908 ip6add = nfsv4_callbackaddr;
910 snprintf(addr, INET6_ADDRSTRLEN + 9, "%s.%d.%d",
911 ip6add, cp2[0], cp2[1]);
915 (void) nfsm_strtom(nd, "tcp", 3);
917 snprintf(addr, INET6_ADDRSTRLEN + 9,
918 "%d.%d.%d.%d.%d.%d", cp[0], cp[1],
919 cp[2], cp[3], cp2[0], cp2[1]);
921 snprintf(addr, INET6_ADDRSTRLEN + 9,
922 "%s.%d.%d", nfsv4_callbackaddr,
925 (void) nfsm_strtom(nd, addr, strlen(addr));
927 (void) nfsm_strtom(nd, "tcp", 3);
928 (void) nfsm_strtom(nd, "0.0.0.0.0.0", 11);
930 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
931 *tl = txdr_unsigned(clp->nfsc_cbident);
932 nd->nd_flag |= ND_USEGSSNAME;
933 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
934 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
937 if (nd->nd_repstat == 0) {
938 NFSM_DISSECT(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
939 NFSMNT_MDSSESSION(nmp)->nfsess_clientid.lval[0] = *tl++;
940 NFSMNT_MDSSESSION(nmp)->nfsess_clientid.lval[1] = *tl++;
941 confirm.lval[0] = *tl++;
942 confirm.lval[1] = *tl;
943 mbuf_freem(nd->nd_mrep);
949 nfscl_reqstart(nd, NFSPROC_SETCLIENTIDCFRM, nmp, NULL, 0, NULL,
951 NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
952 *tl++ = NFSMNT_MDSSESSION(nmp)->nfsess_clientid.lval[0];
953 *tl++ = NFSMNT_MDSSESSION(nmp)->nfsess_clientid.lval[1];
954 *tl++ = confirm.lval[0];
955 *tl = confirm.lval[1];
956 nd->nd_flag |= ND_USEGSSNAME;
957 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p,
958 cred, NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
961 mbuf_freem(nd->nd_mrep);
963 if (nd->nd_repstat == 0) {
964 nfscl_reqstart(nd, NFSPROC_GETATTR, nmp, nmp->nm_fh,
965 nmp->nm_fhsize, NULL, NULL);
966 NFSZERO_ATTRBIT(&attrbits);
967 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_LEASETIME);
968 (void) nfsrv_putattrbit(nd, &attrbits);
969 nd->nd_flag |= ND_USEGSSNAME;
970 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p,
971 cred, NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
974 if (nd->nd_repstat == 0) {
975 error = nfsv4_loadattr(nd, NULL, NULL, NULL, NULL, 0, NULL,
976 NULL, NULL, NULL, NULL, 0, NULL, &lease, NULL, p, cred);
979 clp->nfsc_renew = NFSCL_RENEW(lease);
980 clp->nfsc_expire = NFSD_MONOSEC + clp->nfsc_renew;
981 clp->nfsc_clientidrev++;
982 if (clp->nfsc_clientidrev == 0)
983 clp->nfsc_clientidrev++;
987 error = nd->nd_repstat;
989 mbuf_freem(nd->nd_mrep);
997 nfsrpc_getattr(vnode_t vp, struct ucred *cred, NFSPROC_T *p,
998 struct nfsvattr *nap, void *stuff)
1000 struct nfsrv_descript nfsd, *nd = &nfsd;
1002 nfsattrbit_t attrbits;
1004 NFSCL_REQSTART(nd, NFSPROC_GETATTR, vp);
1005 if (nd->nd_flag & ND_NFSV4) {
1006 NFSGETATTR_ATTRBIT(&attrbits);
1007 (void) nfsrv_putattrbit(nd, &attrbits);
1009 error = nfscl_request(nd, vp, p, cred, stuff);
1012 if (!nd->nd_repstat)
1013 error = nfsm_loadattr(nd, nap);
1015 error = nd->nd_repstat;
1016 mbuf_freem(nd->nd_mrep);
1021 * nfs getattr call with non-vnode arguemnts.
1024 nfsrpc_getattrnovp(struct nfsmount *nmp, u_int8_t *fhp, int fhlen, int syscred,
1025 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, u_int64_t *xidp,
1028 struct nfsrv_descript nfsd, *nd = &nfsd;
1029 int error, vers = NFS_VER2;
1030 nfsattrbit_t attrbits;
1032 nfscl_reqstart(nd, NFSPROC_GETATTR, nmp, fhp, fhlen, NULL, NULL);
1033 if (nd->nd_flag & ND_NFSV4) {
1035 NFSGETATTR_ATTRBIT(&attrbits);
1036 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_LEASETIME);
1037 (void) nfsrv_putattrbit(nd, &attrbits);
1038 } else if (nd->nd_flag & ND_NFSV3) {
1042 nd->nd_flag |= ND_USEGSSNAME;
1043 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
1044 NFS_PROG, vers, NULL, 1, xidp, NULL);
1047 if (nd->nd_repstat == 0) {
1048 if ((nd->nd_flag & ND_NFSV4) != 0)
1049 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0,
1050 NULL, NULL, NULL, NULL, NULL, 0, NULL, leasep, NULL,
1053 error = nfsm_loadattr(nd, nap);
1055 error = nd->nd_repstat;
1056 mbuf_freem(nd->nd_mrep);
1061 * Do an nfs setattr operation.
1064 nfsrpc_setattr(vnode_t vp, struct vattr *vap, NFSACL_T *aclp,
1065 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *rnap, int *attrflagp,
1068 int error, expireret = 0, openerr, retrycnt;
1069 u_int32_t clidrev = 0, mode;
1070 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
1072 nfsv4stateid_t stateid;
1075 if (nmp->nm_clp != NULL)
1076 clidrev = nmp->nm_clp->nfsc_clientidrev;
1077 if (vap != NULL && NFSATTRISSET(u_quad_t, vap, va_size))
1078 mode = NFSV4OPEN_ACCESSWRITE;
1080 mode = NFSV4OPEN_ACCESSREAD;
1085 if (NFSHASNFSV4(nmp)) {
1086 nfhp = VTONFS(vp)->n_fhp;
1087 error = nfscl_getstateid(vp, nfhp->nfh_fh,
1088 nfhp->nfh_len, mode, 0, cred, p, &stateid, &lckp);
1089 if (error && vnode_vtype(vp) == VREG &&
1090 (mode == NFSV4OPEN_ACCESSWRITE ||
1091 nfstest_openallsetattr)) {
1093 * No Open stateid, so try and open the file
1096 if (mode == NFSV4OPEN_ACCESSWRITE)
1097 openerr = nfsrpc_open(vp, FWRITE, cred,
1100 openerr = nfsrpc_open(vp, FREAD, cred,
1103 (void) nfscl_getstateid(vp,
1104 nfhp->nfh_fh, nfhp->nfh_len,
1105 mode, 0, cred, p, &stateid, &lckp);
1109 error = nfsrpc_setattrrpc(vp, vap, &stateid, cred, p,
1110 rnap, attrflagp, stuff);
1112 error = nfsrpc_setaclrpc(vp, cred, p, aclp, &stateid,
1114 if (error == NFSERR_STALESTATEID || error == NFSERR_BADSESSION)
1115 nfscl_initiate_recovery(nmp->nm_clp);
1117 nfscl_lockderef(lckp);
1119 (void) nfsrpc_close(vp, 0, p);
1120 if (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
1121 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
1122 error == NFSERR_OLDSTATEID || error == NFSERR_BADSESSION) {
1123 (void) nfs_catnap(PZERO, error, "nfs_setattr");
1124 } else if ((error == NFSERR_EXPIRED ||
1125 error == NFSERR_BADSTATEID) && clidrev != 0) {
1126 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
1129 } while (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
1130 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
1131 error == NFSERR_BADSESSION ||
1132 (error == NFSERR_OLDSTATEID && retrycnt < 20) ||
1133 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
1134 expireret == 0 && clidrev != 0 && retrycnt < 4));
1135 if (error && retrycnt >= 4)
1141 nfsrpc_setattrrpc(vnode_t vp, struct vattr *vap,
1142 nfsv4stateid_t *stateidp, struct ucred *cred, NFSPROC_T *p,
1143 struct nfsvattr *rnap, int *attrflagp, void *stuff)
1146 struct nfsrv_descript nfsd, *nd = &nfsd;
1148 nfsattrbit_t attrbits;
1151 NFSCL_REQSTART(nd, NFSPROC_SETATTR, vp);
1152 if (nd->nd_flag & ND_NFSV4)
1153 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID);
1154 vap->va_type = vnode_vtype(vp);
1155 nfscl_fillsattr(nd, vap, vp, NFSSATTR_FULL, 0);
1156 if (nd->nd_flag & ND_NFSV3) {
1157 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1159 } else if (nd->nd_flag & ND_NFSV4) {
1160 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1161 *tl = txdr_unsigned(NFSV4OP_GETATTR);
1162 NFSGETATTR_ATTRBIT(&attrbits);
1163 (void) nfsrv_putattrbit(nd, &attrbits);
1165 error = nfscl_request(nd, vp, p, cred, stuff);
1168 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4))
1169 error = nfscl_wcc_data(nd, vp, rnap, attrflagp, NULL, stuff);
1170 if ((nd->nd_flag & ND_NFSV4) && !error)
1171 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
1172 if (!(nd->nd_flag & ND_NFSV3) && !nd->nd_repstat && !error)
1173 error = nfscl_postop_attr(nd, rnap, attrflagp, stuff);
1174 mbuf_freem(nd->nd_mrep);
1175 if (nd->nd_repstat && !error)
1176 error = nd->nd_repstat;
1184 nfsrpc_lookup(vnode_t dvp, char *name, int len, struct ucred *cred,
1185 NFSPROC_T *p, struct nfsvattr *dnap, struct nfsvattr *nap,
1186 struct nfsfh **nfhpp, int *attrflagp, int *dattrflagp, void *stuff)
1189 struct nfsrv_descript nfsd, *nd = &nfsd;
1190 struct nfsmount *nmp;
1193 nfsattrbit_t attrbits;
1194 int error = 0, lookupp = 0;
1198 if (vnode_vtype(dvp) != VDIR)
1200 nmp = VFSTONFS(vnode_mount(dvp));
1201 if (len > NFS_MAXNAMLEN)
1202 return (ENAMETOOLONG);
1203 if (NFSHASNFSV4(nmp) && len == 1 &&
1206 * Just return the current dir's fh.
1209 MALLOC(nfhp, struct nfsfh *, sizeof (struct nfsfh) +
1210 np->n_fhp->nfh_len, M_NFSFH, M_WAITOK);
1211 nfhp->nfh_len = np->n_fhp->nfh_len;
1212 NFSBCOPY(np->n_fhp->nfh_fh, nfhp->nfh_fh, nfhp->nfh_len);
1216 if (NFSHASNFSV4(nmp) && len == 2 &&
1217 name[0] == '.' && name[1] == '.') {
1219 NFSCL_REQSTART(nd, NFSPROC_LOOKUPP, dvp);
1221 NFSCL_REQSTART(nd, NFSPROC_LOOKUP, dvp);
1222 (void) nfsm_strtom(nd, name, len);
1224 if (nd->nd_flag & ND_NFSV4) {
1225 NFSGETATTR_ATTRBIT(&attrbits);
1226 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1227 *tl++ = txdr_unsigned(NFSV4OP_GETFH);
1228 *tl = txdr_unsigned(NFSV4OP_GETATTR);
1229 (void) nfsrv_putattrbit(nd, &attrbits);
1231 error = nfscl_request(nd, dvp, p, cred, stuff);
1234 if (nd->nd_repstat) {
1236 * When an NFSv4 Lookupp returns ENOENT, it means that
1237 * the lookup is at the root of an fs, so return this dir.
1239 if (nd->nd_repstat == NFSERR_NOENT && lookupp) {
1241 MALLOC(nfhp, struct nfsfh *, sizeof (struct nfsfh) +
1242 np->n_fhp->nfh_len, M_NFSFH, M_WAITOK);
1243 nfhp->nfh_len = np->n_fhp->nfh_len;
1244 NFSBCOPY(np->n_fhp->nfh_fh, nfhp->nfh_fh, nfhp->nfh_len);
1246 mbuf_freem(nd->nd_mrep);
1249 if (nd->nd_flag & ND_NFSV3)
1250 error = nfscl_postop_attr(nd, dnap, dattrflagp, stuff);
1251 else if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) ==
1253 /* Load the directory attributes. */
1254 error = nfsm_loadattr(nd, dnap);
1260 if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) == ND_NFSV4) {
1261 /* Load the directory attributes. */
1262 error = nfsm_loadattr(nd, dnap);
1266 /* Skip over the Lookup and GetFH operation status values. */
1267 NFSM_DISSECT(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1269 error = nfsm_getfh(nd, nfhpp);
1273 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
1274 if ((nd->nd_flag & ND_NFSV3) && !error)
1275 error = nfscl_postop_attr(nd, dnap, dattrflagp, stuff);
1277 mbuf_freem(nd->nd_mrep);
1278 if (!error && nd->nd_repstat)
1279 error = nd->nd_repstat;
1284 * Do a readlink rpc.
1287 nfsrpc_readlink(vnode_t vp, struct uio *uiop, struct ucred *cred,
1288 NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, void *stuff)
1291 struct nfsrv_descript nfsd, *nd = &nfsd;
1292 struct nfsnode *np = VTONFS(vp);
1293 nfsattrbit_t attrbits;
1294 int error, len, cangetattr = 1;
1297 NFSCL_REQSTART(nd, NFSPROC_READLINK, vp);
1298 if (nd->nd_flag & ND_NFSV4) {
1300 * And do a Getattr op.
1302 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1303 *tl = txdr_unsigned(NFSV4OP_GETATTR);
1304 NFSGETATTR_ATTRBIT(&attrbits);
1305 (void) nfsrv_putattrbit(nd, &attrbits);
1307 error = nfscl_request(nd, vp, p, cred, stuff);
1310 if (nd->nd_flag & ND_NFSV3)
1311 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
1312 if (!nd->nd_repstat && !error) {
1313 NFSM_STRSIZ(len, NFS_MAXPATHLEN);
1315 * This seems weird to me, but must have been added to
1316 * FreeBSD for some reason. The only thing I can think of
1317 * is that there was/is some server that replies with
1318 * more link data than it should?
1320 if (len == NFS_MAXPATHLEN) {
1322 if (np->n_size > 0 && np->n_size < NFS_MAXPATHLEN) {
1328 error = nfsm_mbufuio(nd, uiop, len);
1329 if ((nd->nd_flag & ND_NFSV4) && !error && cangetattr)
1330 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
1332 if (nd->nd_repstat && !error)
1333 error = nd->nd_repstat;
1335 mbuf_freem(nd->nd_mrep);
1343 nfsrpc_read(vnode_t vp, struct uio *uiop, struct ucred *cred,
1344 NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, void *stuff)
1346 int error, expireret = 0, retrycnt;
1347 u_int32_t clidrev = 0;
1348 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
1349 struct nfsnode *np = VTONFS(vp);
1350 struct ucred *newcred;
1351 struct nfsfh *nfhp = NULL;
1352 nfsv4stateid_t stateid;
1355 if (nmp->nm_clp != NULL)
1356 clidrev = nmp->nm_clp->nfsc_clientidrev;
1358 if (NFSHASNFSV4(nmp)) {
1360 newcred = NFSNEWCRED(cred);
1365 if (NFSHASNFSV4(nmp))
1366 (void)nfscl_getstateid(vp, nfhp->nfh_fh, nfhp->nfh_len,
1367 NFSV4OPEN_ACCESSREAD, 0, newcred, p, &stateid,
1369 error = nfsrpc_readrpc(vp, uiop, newcred, &stateid, p, nap,
1371 if (error == NFSERR_STALESTATEID || error == NFSERR_BADSESSION)
1372 nfscl_initiate_recovery(nmp->nm_clp);
1374 nfscl_lockderef(lckp);
1375 if (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
1376 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
1377 error == NFSERR_OLDSTATEID || error == NFSERR_BADSESSION) {
1378 (void) nfs_catnap(PZERO, error, "nfs_read");
1379 } else if ((error == NFSERR_EXPIRED ||
1380 error == NFSERR_BADSTATEID) && clidrev != 0) {
1381 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
1384 } while (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
1385 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
1386 error == NFSERR_BADSESSION ||
1387 (error == NFSERR_OLDSTATEID && retrycnt < 20) ||
1388 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
1389 expireret == 0 && clidrev != 0 && retrycnt < 4));
1390 if (error && retrycnt >= 4)
1392 if (NFSHASNFSV4(nmp))
1393 NFSFREECRED(newcred);
1398 * The actual read RPC.
1401 nfsrpc_readrpc(vnode_t vp, struct uio *uiop, struct ucred *cred,
1402 nfsv4stateid_t *stateidp, NFSPROC_T *p, struct nfsvattr *nap,
1403 int *attrflagp, void *stuff)
1406 int error = 0, len, retlen, tsiz, eof = 0;
1407 struct nfsrv_descript nfsd;
1408 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
1409 struct nfsrv_descript *nd = &nfsd;
1414 tsiz = uio_uio_resid(uiop);
1415 tmp_off = uiop->uio_offset + tsiz;
1417 if (tmp_off > nmp->nm_maxfilesize || tmp_off < uiop->uio_offset) {
1421 rsize = nmp->nm_rsize;
1426 len = (tsiz > rsize) ? rsize : tsiz;
1427 NFSCL_REQSTART(nd, NFSPROC_READ, vp);
1428 if (nd->nd_flag & ND_NFSV4)
1429 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID);
1430 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED * 3);
1431 if (nd->nd_flag & ND_NFSV2) {
1432 *tl++ = txdr_unsigned(uiop->uio_offset);
1433 *tl++ = txdr_unsigned(len);
1436 txdr_hyper(uiop->uio_offset, tl);
1437 *(tl + 2) = txdr_unsigned(len);
1440 * Since I can't do a Getattr for NFSv4 for Write, there
1441 * doesn't seem any point in doing one here, either.
1442 * (See the comment in nfsrpc_writerpc() for more info.)
1444 error = nfscl_request(nd, vp, p, cred, stuff);
1447 if (nd->nd_flag & ND_NFSV3) {
1448 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
1449 } else if (!nd->nd_repstat && (nd->nd_flag & ND_NFSV2)) {
1450 error = nfsm_loadattr(nd, nap);
1454 if (nd->nd_repstat || error) {
1456 error = nd->nd_repstat;
1459 if (nd->nd_flag & ND_NFSV3) {
1460 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1461 eof = fxdr_unsigned(int, *(tl + 1));
1462 } else if (nd->nd_flag & ND_NFSV4) {
1463 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
1464 eof = fxdr_unsigned(int, *tl);
1466 NFSM_STRSIZ(retlen, len);
1467 error = nfsm_mbufuio(nd, uiop, retlen);
1470 mbuf_freem(nd->nd_mrep);
1473 if (!(nd->nd_flag & ND_NFSV2)) {
1474 if (eof || retlen == 0)
1476 } else if (retlen < len)
1481 if (nd->nd_mrep != NULL)
1482 mbuf_freem(nd->nd_mrep);
1487 * nfs write operation
1488 * When called_from_strategy != 0, it should return EIO for an error that
1489 * indicates recovery is in progress, so that the buffer will be left
1490 * dirty and be written back to the server later. If it loops around,
1491 * the recovery thread could get stuck waiting for the buffer and recovery
1492 * will then deadlock.
1495 nfsrpc_write(vnode_t vp, struct uio *uiop, int *iomode, int *must_commit,
1496 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp,
1497 void *stuff, int called_from_strategy)
1499 int error, expireret = 0, retrycnt, nostateid;
1500 u_int32_t clidrev = 0;
1501 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
1502 struct nfsnode *np = VTONFS(vp);
1503 struct ucred *newcred;
1504 struct nfsfh *nfhp = NULL;
1505 nfsv4stateid_t stateid;
1509 if (nmp->nm_clp != NULL)
1510 clidrev = nmp->nm_clp->nfsc_clientidrev;
1512 if (NFSHASNFSV4(nmp)) {
1513 newcred = NFSNEWCRED(cred);
1520 if (NFSHASNFSV4(nmp)) {
1521 (void)nfscl_getstateid(vp, nfhp->nfh_fh, nfhp->nfh_len,
1522 NFSV4OPEN_ACCESSWRITE, 0, newcred, p, &stateid,
1524 if (stateid.other[0] == 0 && stateid.other[1] == 0 &&
1525 stateid.other[2] == 0) {
1527 NFSCL_DEBUG(1, "stateid0 in write\n");
1532 * If there is no stateid for NFSv4, it means this is an
1533 * extraneous write after close. Basically a poorly
1534 * implemented buffer cache. Just don't do the write.
1539 error = nfsrpc_writerpc(vp, uiop, iomode, must_commit,
1540 newcred, &stateid, p, nap, attrflagp, stuff);
1541 if (error == NFSERR_STALESTATEID || error == NFSERR_BADSESSION)
1542 nfscl_initiate_recovery(nmp->nm_clp);
1544 nfscl_lockderef(lckp);
1545 if (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
1546 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
1547 error == NFSERR_OLDSTATEID || error == NFSERR_BADSESSION) {
1548 (void) nfs_catnap(PZERO, error, "nfs_write");
1549 } else if ((error == NFSERR_EXPIRED ||
1550 error == NFSERR_BADSTATEID) && clidrev != 0) {
1551 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
1554 } while (error == NFSERR_GRACE || error == NFSERR_DELAY ||
1555 ((error == NFSERR_STALESTATEID || error == NFSERR_BADSESSION ||
1556 error == NFSERR_STALEDONTRECOVER) && called_from_strategy == 0) ||
1557 (error == NFSERR_OLDSTATEID && retrycnt < 20) ||
1558 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
1559 expireret == 0 && clidrev != 0 && retrycnt < 4));
1560 if (error != 0 && (retrycnt >= 4 ||
1561 ((error == NFSERR_STALESTATEID || error == NFSERR_BADSESSION ||
1562 error == NFSERR_STALEDONTRECOVER) && called_from_strategy != 0)))
1564 if (NFSHASNFSV4(nmp))
1565 NFSFREECRED(newcred);
1570 * The actual write RPC.
1573 nfsrpc_writerpc(vnode_t vp, struct uio *uiop, int *iomode,
1574 int *must_commit, struct ucred *cred, nfsv4stateid_t *stateidp,
1575 NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, void *stuff)
1578 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
1579 struct nfsnode *np = VTONFS(vp);
1580 int error = 0, len, tsiz, rlen, commit, committed = NFSWRITE_FILESYNC;
1581 int wccflag = 0, wsize;
1583 struct nfsrv_descript nfsd;
1584 struct nfsrv_descript *nd = &nfsd;
1585 nfsattrbit_t attrbits;
1588 KASSERT(uiop->uio_iovcnt == 1, ("nfs: writerpc iovcnt > 1"));
1590 tsiz = uio_uio_resid(uiop);
1591 tmp_off = uiop->uio_offset + tsiz;
1593 if (tmp_off > nmp->nm_maxfilesize || tmp_off < uiop->uio_offset) {
1597 wsize = nmp->nm_wsize;
1599 nd->nd_mrep = NULL; /* NFSv2 sometimes does a write with */
1600 nd->nd_repstat = 0; /* uio_resid == 0, so the while is not done */
1603 len = (tsiz > wsize) ? wsize : tsiz;
1604 NFSCL_REQSTART(nd, NFSPROC_WRITE, vp);
1605 if (nd->nd_flag & ND_NFSV4) {
1606 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID);
1607 NFSM_BUILD(tl, u_int32_t *, NFSX_HYPER+2*NFSX_UNSIGNED);
1608 txdr_hyper(uiop->uio_offset, tl);
1610 *tl++ = txdr_unsigned(*iomode);
1611 *tl = txdr_unsigned(len);
1612 } else if (nd->nd_flag & ND_NFSV3) {
1613 NFSM_BUILD(tl, u_int32_t *, NFSX_HYPER+3*NFSX_UNSIGNED);
1614 txdr_hyper(uiop->uio_offset, tl);
1616 *tl++ = txdr_unsigned(len);
1617 *tl++ = txdr_unsigned(*iomode);
1618 *tl = txdr_unsigned(len);
1622 NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1624 * Not sure why someone changed this, since the
1625 * RFC clearly states that "beginoffset" and
1626 * "totalcount" are ignored, but it wouldn't
1627 * surprise me if there's a busted server out there.
1629 /* Set both "begin" and "current" to non-garbage. */
1630 x = txdr_unsigned((u_int32_t)uiop->uio_offset);
1631 *tl++ = x; /* "begin offset" */
1632 *tl++ = x; /* "current offset" */
1633 x = txdr_unsigned(len);
1634 *tl++ = x; /* total to this offset */
1635 *tl = x; /* size of this write */
1638 nfsm_uiombuf(nd, uiop, len);
1640 * Although it is tempting to do a normal Getattr Op in the
1641 * NFSv4 compound, the result can be a nearly hung client
1642 * system if the Getattr asks for Owner and/or OwnerGroup.
1643 * It occurs when the client can't map either the Owner or
1644 * Owner_group name in the Getattr reply to a uid/gid. When
1645 * there is a cache miss, the kernel does an upcall to the
1646 * nfsuserd. Then, it can try and read the local /etc/passwd
1647 * or /etc/group file. It can then block in getnewbuf(),
1648 * waiting for dirty writes to be pushed to the NFS server.
1649 * The only reason this doesn't result in a complete
1650 * deadlock, is that the upcall times out and allows
1651 * the write to complete. However, progress is so slow
1652 * that it might just as well be deadlocked.
1653 * As such, we get the rest of the attributes, but not
1654 * Owner or Owner_group.
1655 * nb: nfscl_loadattrcache() needs to be told that these
1656 * partial attributes from a write rpc are being
1657 * passed in, via a argument flag.
1659 if (nd->nd_flag & ND_NFSV4) {
1660 NFSWRITEGETATTR_ATTRBIT(&attrbits);
1661 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1662 *tl = txdr_unsigned(NFSV4OP_GETATTR);
1663 (void) nfsrv_putattrbit(nd, &attrbits);
1665 error = nfscl_request(nd, vp, p, cred, stuff);
1668 if (nd->nd_repstat) {
1670 * In case the rpc gets retried, roll
1671 * the uio fileds changed by nfsm_uiombuf()
1674 uiop->uio_offset -= len;
1675 uio_uio_resid_add(uiop, len);
1676 uio_iov_base_add(uiop, -len);
1677 uio_iov_len_add(uiop, len);
1679 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) {
1680 error = nfscl_wcc_data(nd, vp, nap, attrflagp,
1685 if (!nd->nd_repstat) {
1686 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) {
1687 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED
1689 rlen = fxdr_unsigned(int, *tl++);
1693 } else if (rlen < len) {
1694 backup = len - rlen;
1695 uio_iov_base_add(uiop, -(backup));
1696 uio_iov_len_add(uiop, backup);
1697 uiop->uio_offset -= backup;
1698 uio_uio_resid_add(uiop, backup);
1701 commit = fxdr_unsigned(int, *tl++);
1704 * Return the lowest commitment level
1705 * obtained by any of the RPCs.
1707 if (committed == NFSWRITE_FILESYNC)
1709 else if (committed == NFSWRITE_DATASYNC &&
1710 commit == NFSWRITE_UNSTABLE)
1713 if (!NFSHASWRITEVERF(nmp)) {
1714 NFSBCOPY((caddr_t)tl,
1715 (caddr_t)&nmp->nm_verf[0],
1717 NFSSETWRITEVERF(nmp);
1718 } else if (NFSBCMP(tl, nmp->nm_verf,
1721 NFSBCOPY(tl, nmp->nm_verf, NFSX_VERF);
1725 if (nd->nd_flag & ND_NFSV4)
1726 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1727 if (nd->nd_flag & (ND_NFSV2 | ND_NFSV4)) {
1728 error = nfsm_loadattr(nd, nap);
1730 *attrflagp = NFS_LATTR_NOSHRINK;
1733 error = nd->nd_repstat;
1737 NFSWRITERPC_SETTIME(wccflag, np, nap, (nd->nd_flag & ND_NFSV4));
1738 mbuf_freem(nd->nd_mrep);
1743 if (nd->nd_mrep != NULL)
1744 mbuf_freem(nd->nd_mrep);
1745 *iomode = committed;
1746 if (nd->nd_repstat && !error)
1747 error = nd->nd_repstat;
1753 * For NFS v2 this is a kludge. Use a create rpc but with the IFMT bits of the
1754 * mode set to specify the file type and the size field for rdev.
1757 nfsrpc_mknod(vnode_t dvp, char *name, int namelen, struct vattr *vap,
1758 u_int32_t rdev, enum vtype vtyp, struct ucred *cred, NFSPROC_T *p,
1759 struct nfsvattr *dnap, struct nfsvattr *nnap, struct nfsfh **nfhpp,
1760 int *attrflagp, int *dattrflagp, void *dstuff)
1764 struct nfsrv_descript nfsd, *nd = &nfsd;
1765 nfsattrbit_t attrbits;
1770 if (namelen > NFS_MAXNAMLEN)
1771 return (ENAMETOOLONG);
1772 NFSCL_REQSTART(nd, NFSPROC_MKNOD, dvp);
1773 if (nd->nd_flag & ND_NFSV4) {
1774 if (vtyp == VBLK || vtyp == VCHR) {
1775 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
1776 *tl++ = vtonfsv34_type(vtyp);
1777 *tl++ = txdr_unsigned(NFSMAJOR(rdev));
1778 *tl = txdr_unsigned(NFSMINOR(rdev));
1780 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1781 *tl = vtonfsv34_type(vtyp);
1784 (void) nfsm_strtom(nd, name, namelen);
1785 if (nd->nd_flag & ND_NFSV3) {
1786 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1787 *tl = vtonfsv34_type(vtyp);
1789 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4))
1790 nfscl_fillsattr(nd, vap, dvp, 0, 0);
1791 if ((nd->nd_flag & ND_NFSV3) &&
1792 (vtyp == VCHR || vtyp == VBLK)) {
1793 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1794 *tl++ = txdr_unsigned(NFSMAJOR(rdev));
1795 *tl = txdr_unsigned(NFSMINOR(rdev));
1797 if (nd->nd_flag & ND_NFSV4) {
1798 NFSGETATTR_ATTRBIT(&attrbits);
1799 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1800 *tl++ = txdr_unsigned(NFSV4OP_GETFH);
1801 *tl = txdr_unsigned(NFSV4OP_GETATTR);
1802 (void) nfsrv_putattrbit(nd, &attrbits);
1804 if (nd->nd_flag & ND_NFSV2)
1805 nfscl_fillsattr(nd, vap, dvp, NFSSATTR_SIZERDEV, rdev);
1806 error = nfscl_request(nd, dvp, p, cred, dstuff);
1809 if (nd->nd_flag & ND_NFSV4)
1810 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
1811 if (!nd->nd_repstat) {
1812 if (nd->nd_flag & ND_NFSV4) {
1813 NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
1814 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
1818 error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp);
1822 if (nd->nd_flag & ND_NFSV3)
1823 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
1824 if (!error && nd->nd_repstat)
1825 error = nd->nd_repstat;
1827 mbuf_freem(nd->nd_mrep);
1832 * nfs file create call
1833 * Mostly just call the approriate routine. (I separated out v4, so that
1834 * error recovery wouldn't be as difficult.)
1837 nfsrpc_create(vnode_t dvp, char *name, int namelen, struct vattr *vap,
1838 nfsquad_t cverf, int fmode, struct ucred *cred, NFSPROC_T *p,
1839 struct nfsvattr *dnap, struct nfsvattr *nnap, struct nfsfh **nfhpp,
1840 int *attrflagp, int *dattrflagp, void *dstuff)
1842 int error = 0, newone, expireret = 0, retrycnt, unlocked;
1843 struct nfsclowner *owp;
1844 struct nfscldeleg *dp;
1845 struct nfsmount *nmp = VFSTONFS(vnode_mount(dvp));
1848 if (NFSHASNFSV4(nmp)) {
1852 error = nfscl_open(dvp, NULL, 0, (NFSV4OPEN_ACCESSWRITE |
1853 NFSV4OPEN_ACCESSREAD), 0, cred, p, &owp, NULL, &newone,
1857 if (nmp->nm_clp != NULL)
1858 clidrev = nmp->nm_clp->nfsc_clientidrev;
1861 error = nfsrpc_createv4(dvp, name, namelen, vap, cverf, fmode,
1862 owp, &dp, cred, p, dnap, nnap, nfhpp, attrflagp, dattrflagp,
1865 * There is no need to invalidate cached attributes here,
1866 * since new post-delegation issue attributes are always
1867 * returned by nfsrpc_createv4() and these will update the
1871 (void) nfscl_deleg(nmp->nm_mountp, owp->nfsow_clp,
1872 (*nfhpp)->nfh_fh, (*nfhpp)->nfh_len, cred, p, &dp);
1873 nfscl_ownerrelease(owp, error, newone, unlocked);
1874 if (error == NFSERR_GRACE || error == NFSERR_STALECLIENTID ||
1875 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
1876 error == NFSERR_BADSESSION) {
1877 (void) nfs_catnap(PZERO, error, "nfs_open");
1878 } else if ((error == NFSERR_EXPIRED ||
1879 error == NFSERR_BADSTATEID) && clidrev != 0) {
1880 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
1883 } while (error == NFSERR_GRACE || error == NFSERR_STALECLIENTID ||
1884 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
1885 error == NFSERR_BADSESSION ||
1886 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
1887 expireret == 0 && clidrev != 0 && retrycnt < 4));
1888 if (error && retrycnt >= 4)
1891 error = nfsrpc_createv23(dvp, name, namelen, vap, cverf,
1892 fmode, cred, p, dnap, nnap, nfhpp, attrflagp, dattrflagp,
1899 * The create rpc for v2 and 3.
1902 nfsrpc_createv23(vnode_t dvp, char *name, int namelen, struct vattr *vap,
1903 nfsquad_t cverf, int fmode, struct ucred *cred, NFSPROC_T *p,
1904 struct nfsvattr *dnap, struct nfsvattr *nnap, struct nfsfh **nfhpp,
1905 int *attrflagp, int *dattrflagp, void *dstuff)
1909 struct nfsrv_descript nfsd, *nd = &nfsd;
1914 if (namelen > NFS_MAXNAMLEN)
1915 return (ENAMETOOLONG);
1916 NFSCL_REQSTART(nd, NFSPROC_CREATE, dvp);
1917 (void) nfsm_strtom(nd, name, namelen);
1918 if (nd->nd_flag & ND_NFSV3) {
1919 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1920 if (fmode & O_EXCL) {
1921 *tl = txdr_unsigned(NFSCREATE_EXCLUSIVE);
1922 NFSM_BUILD(tl, u_int32_t *, NFSX_VERF);
1923 *tl++ = cverf.lval[0];
1924 *tl = cverf.lval[1];
1926 *tl = txdr_unsigned(NFSCREATE_UNCHECKED);
1927 nfscl_fillsattr(nd, vap, dvp, 0, 0);
1930 nfscl_fillsattr(nd, vap, dvp, NFSSATTR_SIZE0, 0);
1932 error = nfscl_request(nd, dvp, p, cred, dstuff);
1935 if (nd->nd_repstat == 0) {
1936 error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp);
1940 if (nd->nd_flag & ND_NFSV3)
1941 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
1942 if (nd->nd_repstat != 0 && error == 0)
1943 error = nd->nd_repstat;
1945 mbuf_freem(nd->nd_mrep);
1950 nfsrpc_createv4(vnode_t dvp, char *name, int namelen, struct vattr *vap,
1951 nfsquad_t cverf, int fmode, struct nfsclowner *owp, struct nfscldeleg **dpp,
1952 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap,
1953 struct nfsvattr *nnap, struct nfsfh **nfhpp, int *attrflagp,
1954 int *dattrflagp, void *dstuff, int *unlockedp)
1957 int error = 0, deleg, newone, ret, acesize, limitby;
1958 struct nfsrv_descript nfsd, *nd = &nfsd;
1959 struct nfsclopen *op;
1960 struct nfscldeleg *dp = NULL;
1963 nfsattrbit_t attrbits;
1964 nfsv4stateid_t stateid;
1966 struct nfsmount *nmp;
1968 nmp = VFSTONFS(dvp->v_mount);
1975 if (namelen > NFS_MAXNAMLEN)
1976 return (ENAMETOOLONG);
1977 NFSCL_REQSTART(nd, NFSPROC_CREATE, dvp);
1979 * For V4, this is actually an Open op.
1981 NFSM_BUILD(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
1982 *tl++ = txdr_unsigned(owp->nfsow_seqid);
1983 *tl++ = txdr_unsigned(NFSV4OPEN_ACCESSWRITE |
1984 NFSV4OPEN_ACCESSREAD);
1985 *tl++ = txdr_unsigned(NFSV4OPEN_DENYNONE);
1986 *tl++ = NFSMNT_MDSSESSION(nmp)->nfsess_clientid.lval[0];
1987 *tl = NFSMNT_MDSSESSION(nmp)->nfsess_clientid.lval[1];
1988 (void) nfsm_strtom(nd, owp->nfsow_owner, NFSV4CL_LOCKNAMELEN);
1989 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1990 *tl++ = txdr_unsigned(NFSV4OPEN_CREATE);
1991 if (fmode & O_EXCL) {
1992 if (NFSHASNFSV4N(nmp)) {
1993 if (NFSHASSESSPERSIST(nmp)) {
1994 /* Use GUARDED for persistent sessions. */
1995 *tl = txdr_unsigned(NFSCREATE_GUARDED);
1996 nfscl_fillsattr(nd, vap, dvp, 0, 0);
1998 /* Otherwise, use EXCLUSIVE4_1. */
1999 *tl = txdr_unsigned(NFSCREATE_EXCLUSIVE41);
2000 NFSM_BUILD(tl, u_int32_t *, NFSX_VERF);
2001 *tl++ = cverf.lval[0];
2002 *tl = cverf.lval[1];
2003 nfscl_fillsattr(nd, vap, dvp, 0, 0);
2007 *tl = txdr_unsigned(NFSCREATE_EXCLUSIVE);
2008 NFSM_BUILD(tl, u_int32_t *, NFSX_VERF);
2009 *tl++ = cverf.lval[0];
2010 *tl = cverf.lval[1];
2013 *tl = txdr_unsigned(NFSCREATE_UNCHECKED);
2014 nfscl_fillsattr(nd, vap, dvp, 0, 0);
2016 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2017 *tl = txdr_unsigned(NFSV4OPEN_CLAIMNULL);
2018 (void) nfsm_strtom(nd, name, namelen);
2019 /* Get the new file's handle and attributes. */
2020 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2021 *tl++ = txdr_unsigned(NFSV4OP_GETFH);
2022 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2023 NFSGETATTR_ATTRBIT(&attrbits);
2024 (void) nfsrv_putattrbit(nd, &attrbits);
2025 /* Get the directory's post-op attributes. */
2026 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2027 *tl = txdr_unsigned(NFSV4OP_PUTFH);
2028 (void) nfsm_fhtom(nd, np->n_fhp->nfh_fh, np->n_fhp->nfh_len, 0);
2029 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2030 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2031 (void) nfsrv_putattrbit(nd, &attrbits);
2032 error = nfscl_request(nd, dvp, p, cred, dstuff);
2035 NFSCL_INCRSEQID(owp->nfsow_seqid, nd);
2036 if (nd->nd_repstat == 0) {
2037 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
2039 stateid.seqid = *tl++;
2040 stateid.other[0] = *tl++;
2041 stateid.other[1] = *tl++;
2042 stateid.other[2] = *tl;
2043 rflags = fxdr_unsigned(u_int32_t, *(tl + 6));
2044 (void) nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
2045 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2046 deleg = fxdr_unsigned(int, *tl);
2047 if (deleg == NFSV4OPEN_DELEGATEREAD ||
2048 deleg == NFSV4OPEN_DELEGATEWRITE) {
2049 if (!(owp->nfsow_clp->nfsc_flags &
2050 NFSCLFLAGS_FIRSTDELEG))
2051 owp->nfsow_clp->nfsc_flags |=
2052 (NFSCLFLAGS_FIRSTDELEG | NFSCLFLAGS_GOTDELEG);
2053 MALLOC(dp, struct nfscldeleg *,
2054 sizeof (struct nfscldeleg) + NFSX_V4FHMAX,
2055 M_NFSCLDELEG, M_WAITOK);
2056 LIST_INIT(&dp->nfsdl_owner);
2057 LIST_INIT(&dp->nfsdl_lock);
2058 dp->nfsdl_clp = owp->nfsow_clp;
2059 newnfs_copyincred(cred, &dp->nfsdl_cred);
2060 nfscl_lockinit(&dp->nfsdl_rwlock);
2061 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
2063 dp->nfsdl_stateid.seqid = *tl++;
2064 dp->nfsdl_stateid.other[0] = *tl++;
2065 dp->nfsdl_stateid.other[1] = *tl++;
2066 dp->nfsdl_stateid.other[2] = *tl++;
2067 ret = fxdr_unsigned(int, *tl);
2068 if (deleg == NFSV4OPEN_DELEGATEWRITE) {
2069 dp->nfsdl_flags = NFSCLDL_WRITE;
2071 * Indicates how much the file can grow.
2073 NFSM_DISSECT(tl, u_int32_t *,
2075 limitby = fxdr_unsigned(int, *tl++);
2077 case NFSV4OPEN_LIMITSIZE:
2078 dp->nfsdl_sizelimit = fxdr_hyper(tl);
2080 case NFSV4OPEN_LIMITBLOCKS:
2081 dp->nfsdl_sizelimit =
2082 fxdr_unsigned(u_int64_t, *tl++);
2083 dp->nfsdl_sizelimit *=
2084 fxdr_unsigned(u_int64_t, *tl);
2087 error = NFSERR_BADXDR;
2091 dp->nfsdl_flags = NFSCLDL_READ;
2094 dp->nfsdl_flags |= NFSCLDL_RECALL;
2095 error = nfsrv_dissectace(nd, &dp->nfsdl_ace, &ret,
2099 } else if (deleg != NFSV4OPEN_DELEGATENONE) {
2100 error = NFSERR_BADXDR;
2103 error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp);
2106 /* Get rid of the PutFH and Getattr status values. */
2107 NFSM_DISSECT(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
2108 /* Load the directory attributes. */
2109 error = nfsm_loadattr(nd, dnap);
2113 if (dp != NULL && *attrflagp) {
2114 dp->nfsdl_change = nnap->na_filerev;
2115 dp->nfsdl_modtime = nnap->na_mtime;
2116 dp->nfsdl_flags |= NFSCLDL_MODTIMESET;
2119 * We can now complete the Open state.
2123 dp->nfsdl_fhlen = nfhp->nfh_len;
2124 NFSBCOPY(nfhp->nfh_fh, dp->nfsdl_fh, nfhp->nfh_len);
2127 * Get an Open structure that will be
2128 * attached to the OpenOwner, acquired already.
2130 error = nfscl_open(dvp, nfhp->nfh_fh, nfhp->nfh_len,
2131 (NFSV4OPEN_ACCESSWRITE | NFSV4OPEN_ACCESSREAD), 0,
2132 cred, p, NULL, &op, &newone, NULL, 0);
2135 op->nfso_stateid = stateid;
2136 newnfs_copyincred(cred, &op->nfso_cred);
2137 if ((rflags & NFSV4OPEN_RESULTCONFIRM)) {
2139 ret = nfsrpc_openconfirm(dvp, nfhp->nfh_fh,
2140 nfhp->nfh_len, op, cred, p);
2141 if (ret == NFSERR_DELAY)
2142 (void) nfs_catnap(PZERO, ret, "nfs_create");
2143 } while (ret == NFSERR_DELAY);
2148 * If the server is handing out delegations, but we didn't
2149 * get one because an OpenConfirm was required, try the
2150 * Open again, to get a delegation. This is a harmless no-op,
2151 * from a server's point of view.
2153 if ((rflags & NFSV4OPEN_RESULTCONFIRM) &&
2154 (owp->nfsow_clp->nfsc_flags & NFSCLFLAGS_GOTDELEG) &&
2155 !error && dp == NULL) {
2157 ret = nfsrpc_openrpc(VFSTONFS(vnode_mount(dvp)), dvp,
2158 np->n_fhp->nfh_fh, np->n_fhp->nfh_len,
2159 nfhp->nfh_fh, nfhp->nfh_len,
2160 (NFSV4OPEN_ACCESSWRITE | NFSV4OPEN_ACCESSREAD), op,
2161 name, namelen, &dp, 0, 0x0, cred, p, 0, 1);
2162 if (ret == NFSERR_DELAY)
2163 (void) nfs_catnap(PZERO, ret, "nfs_crt2");
2164 } while (ret == NFSERR_DELAY);
2167 FREE((caddr_t)dp, M_NFSCLDELEG);
2170 if (ret == NFSERR_STALECLIENTID ||
2171 ret == NFSERR_STALEDONTRECOVER ||
2172 ret == NFSERR_BADSESSION)
2176 nfscl_openrelease(op, error, newone);
2179 if (nd->nd_repstat != 0 && error == 0)
2180 error = nd->nd_repstat;
2181 if (error == NFSERR_STALECLIENTID || error == NFSERR_BADSESSION)
2182 nfscl_initiate_recovery(owp->nfsow_clp);
2186 else if (dp != NULL)
2187 FREE((caddr_t)dp, M_NFSCLDELEG);
2188 mbuf_freem(nd->nd_mrep);
2196 nfsrpc_remove(vnode_t dvp, char *name, int namelen, vnode_t vp,
2197 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap, int *dattrflagp,
2201 struct nfsrv_descript nfsd, *nd = &nfsd;
2203 struct nfsmount *nmp;
2204 nfsv4stateid_t dstateid;
2205 int error, ret = 0, i;
2208 if (namelen > NFS_MAXNAMLEN)
2209 return (ENAMETOOLONG);
2210 nmp = VFSTONFS(vnode_mount(dvp));
2212 if (NFSHASNFSV4(nmp) && ret == 0) {
2213 ret = nfscl_removedeleg(vp, p, &dstateid);
2215 NFSCL_REQSTART(nd, NFSPROC_RETDELEGREMOVE, vp);
2216 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID +
2218 if (NFSHASNFSV4N(nmp))
2221 *tl++ = dstateid.seqid;
2222 *tl++ = dstateid.other[0];
2223 *tl++ = dstateid.other[1];
2224 *tl++ = dstateid.other[2];
2225 *tl = txdr_unsigned(NFSV4OP_PUTFH);
2227 (void) nfsm_fhtom(nd, np->n_fhp->nfh_fh,
2228 np->n_fhp->nfh_len, 0);
2229 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2230 *tl = txdr_unsigned(NFSV4OP_REMOVE);
2236 NFSCL_REQSTART(nd, NFSPROC_REMOVE, dvp);
2237 (void) nfsm_strtom(nd, name, namelen);
2238 error = nfscl_request(nd, dvp, p, cred, dstuff);
2241 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) {
2242 /* For NFSv4, parse out any Delereturn replies. */
2243 if (ret > 0 && nd->nd_repstat != 0 &&
2244 (nd->nd_flag & ND_NOMOREDATA)) {
2246 * If the Delegreturn failed, try again without
2247 * it. The server will Recall, as required.
2249 mbuf_freem(nd->nd_mrep);
2252 for (i = 0; i < (ret * 2); i++) {
2253 if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) ==
2255 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2257 nd->nd_flag |= ND_NOMOREDATA;
2260 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
2262 if (nd->nd_repstat && !error)
2263 error = nd->nd_repstat;
2265 mbuf_freem(nd->nd_mrep);
2270 * Do an nfs rename rpc.
2273 nfsrpc_rename(vnode_t fdvp, vnode_t fvp, char *fnameptr, int fnamelen,
2274 vnode_t tdvp, vnode_t tvp, char *tnameptr, int tnamelen, struct ucred *cred,
2275 NFSPROC_T *p, struct nfsvattr *fnap, struct nfsvattr *tnap,
2276 int *fattrflagp, int *tattrflagp, void *fstuff, void *tstuff)
2279 struct nfsrv_descript nfsd, *nd = &nfsd;
2280 struct nfsmount *nmp;
2282 nfsattrbit_t attrbits;
2283 nfsv4stateid_t fdstateid, tdstateid;
2284 int error = 0, ret = 0, gottd = 0, gotfd = 0, i;
2288 nmp = VFSTONFS(vnode_mount(fdvp));
2289 if (fnamelen > NFS_MAXNAMLEN || tnamelen > NFS_MAXNAMLEN)
2290 return (ENAMETOOLONG);
2292 if (NFSHASNFSV4(nmp) && ret == 0) {
2293 ret = nfscl_renamedeleg(fvp, &fdstateid, &gotfd, tvp,
2294 &tdstateid, &gottd, p);
2295 if (gotfd && gottd) {
2296 NFSCL_REQSTART(nd, NFSPROC_RETDELEGRENAME2, fvp);
2298 NFSCL_REQSTART(nd, NFSPROC_RETDELEGRENAME1, fvp);
2300 NFSCL_REQSTART(nd, NFSPROC_RETDELEGRENAME1, tvp);
2303 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID);
2304 if (NFSHASNFSV4N(nmp))
2307 *tl++ = fdstateid.seqid;
2308 *tl++ = fdstateid.other[0];
2309 *tl++ = fdstateid.other[1];
2310 *tl = fdstateid.other[2];
2312 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2313 *tl = txdr_unsigned(NFSV4OP_PUTFH);
2315 (void) nfsm_fhtom(nd, np->n_fhp->nfh_fh,
2316 np->n_fhp->nfh_len, 0);
2317 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2318 *tl = txdr_unsigned(NFSV4OP_DELEGRETURN);
2322 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID);
2323 if (NFSHASNFSV4N(nmp))
2326 *tl++ = tdstateid.seqid;
2327 *tl++ = tdstateid.other[0];
2328 *tl++ = tdstateid.other[1];
2329 *tl = tdstateid.other[2];
2332 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2333 *tl = txdr_unsigned(NFSV4OP_PUTFH);
2335 (void) nfsm_fhtom(nd, np->n_fhp->nfh_fh,
2336 np->n_fhp->nfh_len, 0);
2337 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2338 *tl = txdr_unsigned(NFSV4OP_SAVEFH);
2344 NFSCL_REQSTART(nd, NFSPROC_RENAME, fdvp);
2345 if (nd->nd_flag & ND_NFSV4) {
2346 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2347 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2348 NFSWCCATTR_ATTRBIT(&attrbits);
2349 (void) nfsrv_putattrbit(nd, &attrbits);
2350 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2351 *tl = txdr_unsigned(NFSV4OP_PUTFH);
2352 (void) nfsm_fhtom(nd, VTONFS(tdvp)->n_fhp->nfh_fh,
2353 VTONFS(tdvp)->n_fhp->nfh_len, 0);
2354 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2355 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2356 (void) nfsrv_putattrbit(nd, &attrbits);
2357 nd->nd_flag |= ND_V4WCCATTR;
2358 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2359 *tl = txdr_unsigned(NFSV4OP_RENAME);
2361 (void) nfsm_strtom(nd, fnameptr, fnamelen);
2362 if (!(nd->nd_flag & ND_NFSV4))
2363 (void) nfsm_fhtom(nd, VTONFS(tdvp)->n_fhp->nfh_fh,
2364 VTONFS(tdvp)->n_fhp->nfh_len, 0);
2365 (void) nfsm_strtom(nd, tnameptr, tnamelen);
2366 error = nfscl_request(nd, fdvp, p, cred, fstuff);
2369 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) {
2370 /* For NFSv4, parse out any Delereturn replies. */
2371 if (ret > 0 && nd->nd_repstat != 0 &&
2372 (nd->nd_flag & ND_NOMOREDATA)) {
2374 * If the Delegreturn failed, try again without
2375 * it. The server will Recall, as required.
2377 mbuf_freem(nd->nd_mrep);
2380 for (i = 0; i < (ret * 2); i++) {
2381 if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) ==
2383 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2385 if (i == 0 && ret > 1) {
2387 * If the Delegreturn failed, try again
2388 * without it. The server will Recall, as
2390 * If ret > 1, the first iteration of this
2391 * loop is the second DelegReturn result.
2393 mbuf_freem(nd->nd_mrep);
2396 nd->nd_flag |= ND_NOMOREDATA;
2401 /* Now, the first wcc attribute reply. */
2402 if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) == ND_NFSV4) {
2403 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2405 nd->nd_flag |= ND_NOMOREDATA;
2407 error = nfscl_wcc_data(nd, fdvp, fnap, fattrflagp, NULL,
2409 /* and the second wcc attribute reply. */
2410 if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) == ND_NFSV4 &&
2412 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2414 nd->nd_flag |= ND_NOMOREDATA;
2417 error = nfscl_wcc_data(nd, tdvp, tnap, tattrflagp,
2420 if (nd->nd_repstat && !error)
2421 error = nd->nd_repstat;
2423 mbuf_freem(nd->nd_mrep);
2428 * nfs hard link create rpc
2431 nfsrpc_link(vnode_t dvp, vnode_t vp, char *name, int namelen,
2432 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap,
2433 struct nfsvattr *nap, int *attrflagp, int *dattrflagp, void *dstuff)
2436 struct nfsrv_descript nfsd, *nd = &nfsd;
2437 nfsattrbit_t attrbits;
2442 if (namelen > NFS_MAXNAMLEN)
2443 return (ENAMETOOLONG);
2444 NFSCL_REQSTART(nd, NFSPROC_LINK, vp);
2445 if (nd->nd_flag & ND_NFSV4) {
2446 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2447 *tl = txdr_unsigned(NFSV4OP_PUTFH);
2449 (void) nfsm_fhtom(nd, VTONFS(dvp)->n_fhp->nfh_fh,
2450 VTONFS(dvp)->n_fhp->nfh_len, 0);
2451 if (nd->nd_flag & ND_NFSV4) {
2452 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2453 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2454 NFSWCCATTR_ATTRBIT(&attrbits);
2455 (void) nfsrv_putattrbit(nd, &attrbits);
2456 nd->nd_flag |= ND_V4WCCATTR;
2457 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2458 *tl = txdr_unsigned(NFSV4OP_LINK);
2460 (void) nfsm_strtom(nd, name, namelen);
2461 error = nfscl_request(nd, vp, p, cred, dstuff);
2464 if (nd->nd_flag & ND_NFSV3) {
2465 error = nfscl_postop_attr(nd, nap, attrflagp, dstuff);
2467 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp,
2469 } else if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) == ND_NFSV4) {
2471 * First, parse out the PutFH and Getattr result.
2473 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2475 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2477 nd->nd_flag |= ND_NOMOREDATA;
2479 * Get the pre-op attributes.
2481 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
2483 if (nd->nd_repstat && !error)
2484 error = nd->nd_repstat;
2486 mbuf_freem(nd->nd_mrep);
2491 * nfs symbolic link create rpc
2494 nfsrpc_symlink(vnode_t dvp, char *name, int namelen, char *target,
2495 struct vattr *vap, struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap,
2496 struct nfsvattr *nnap, struct nfsfh **nfhpp, int *attrflagp,
2497 int *dattrflagp, void *dstuff)
2500 struct nfsrv_descript nfsd, *nd = &nfsd;
2501 struct nfsmount *nmp;
2502 int slen, error = 0;
2507 nmp = VFSTONFS(vnode_mount(dvp));
2508 slen = strlen(target);
2509 if (slen > NFS_MAXPATHLEN || namelen > NFS_MAXNAMLEN)
2510 return (ENAMETOOLONG);
2511 NFSCL_REQSTART(nd, NFSPROC_SYMLINK, dvp);
2512 if (nd->nd_flag & ND_NFSV4) {
2513 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2514 *tl = txdr_unsigned(NFLNK);
2515 (void) nfsm_strtom(nd, target, slen);
2517 (void) nfsm_strtom(nd, name, namelen);
2518 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4))
2519 nfscl_fillsattr(nd, vap, dvp, 0, 0);
2520 if (!(nd->nd_flag & ND_NFSV4))
2521 (void) nfsm_strtom(nd, target, slen);
2522 if (nd->nd_flag & ND_NFSV2)
2523 nfscl_fillsattr(nd, vap, dvp, NFSSATTR_SIZENEG1, 0);
2524 error = nfscl_request(nd, dvp, p, cred, dstuff);
2527 if (nd->nd_flag & ND_NFSV4)
2528 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
2529 if ((nd->nd_flag & ND_NFSV3) && !error) {
2530 if (!nd->nd_repstat)
2531 error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp);
2533 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp,
2536 if (nd->nd_repstat && !error)
2537 error = nd->nd_repstat;
2538 mbuf_freem(nd->nd_mrep);
2540 * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry.
2541 * Only do this if vfs.nfs.ignore_eexist is set.
2542 * Never do this for NFSv4.1 or later minor versions, since sessions
2543 * should guarantee "exactly once" RPC semantics.
2545 if (error == EEXIST && nfsignore_eexist != 0 && (!NFSHASNFSV4(nmp) ||
2546 nmp->nm_minorvers == 0))
2555 nfsrpc_mkdir(vnode_t dvp, char *name, int namelen, struct vattr *vap,
2556 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap,
2557 struct nfsvattr *nnap, struct nfsfh **nfhpp, int *attrflagp,
2558 int *dattrflagp, void *dstuff)
2561 struct nfsrv_descript nfsd, *nd = &nfsd;
2562 nfsattrbit_t attrbits;
2565 struct nfsmount *nmp;
2570 nmp = VFSTONFS(vnode_mount(dvp));
2571 fhp = VTONFS(dvp)->n_fhp;
2572 if (namelen > NFS_MAXNAMLEN)
2573 return (ENAMETOOLONG);
2574 NFSCL_REQSTART(nd, NFSPROC_MKDIR, dvp);
2575 if (nd->nd_flag & ND_NFSV4) {
2576 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2577 *tl = txdr_unsigned(NFDIR);
2579 (void) nfsm_strtom(nd, name, namelen);
2580 nfscl_fillsattr(nd, vap, dvp, NFSSATTR_SIZENEG1, 0);
2581 if (nd->nd_flag & ND_NFSV4) {
2582 NFSGETATTR_ATTRBIT(&attrbits);
2583 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2584 *tl++ = txdr_unsigned(NFSV4OP_GETFH);
2585 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2586 (void) nfsrv_putattrbit(nd, &attrbits);
2587 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2588 *tl = txdr_unsigned(NFSV4OP_PUTFH);
2589 (void) nfsm_fhtom(nd, fhp->nfh_fh, fhp->nfh_len, 0);
2590 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2591 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2592 (void) nfsrv_putattrbit(nd, &attrbits);
2594 error = nfscl_request(nd, dvp, p, cred, dstuff);
2597 if (nd->nd_flag & ND_NFSV4)
2598 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
2599 if (!nd->nd_repstat && !error) {
2600 if (nd->nd_flag & ND_NFSV4) {
2601 NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
2602 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
2605 error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp);
2606 if (error == 0 && (nd->nd_flag & ND_NFSV4) != 0) {
2607 /* Get rid of the PutFH and Getattr status values. */
2608 NFSM_DISSECT(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
2609 /* Load the directory attributes. */
2610 error = nfsm_loadattr(nd, dnap);
2615 if ((nd->nd_flag & ND_NFSV3) && !error)
2616 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
2617 if (nd->nd_repstat && !error)
2618 error = nd->nd_repstat;
2620 mbuf_freem(nd->nd_mrep);
2622 * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry.
2623 * Only do this if vfs.nfs.ignore_eexist is set.
2624 * Never do this for NFSv4.1 or later minor versions, since sessions
2625 * should guarantee "exactly once" RPC semantics.
2627 if (error == EEXIST && nfsignore_eexist != 0 && (!NFSHASNFSV4(nmp) ||
2628 nmp->nm_minorvers == 0))
2634 * nfs remove directory call
2637 nfsrpc_rmdir(vnode_t dvp, char *name, int namelen, struct ucred *cred,
2638 NFSPROC_T *p, struct nfsvattr *dnap, int *dattrflagp, void *dstuff)
2640 struct nfsrv_descript nfsd, *nd = &nfsd;
2644 if (namelen > NFS_MAXNAMLEN)
2645 return (ENAMETOOLONG);
2646 NFSCL_REQSTART(nd, NFSPROC_RMDIR, dvp);
2647 (void) nfsm_strtom(nd, name, namelen);
2648 error = nfscl_request(nd, dvp, p, cred, dstuff);
2651 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4))
2652 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
2653 if (nd->nd_repstat && !error)
2654 error = nd->nd_repstat;
2655 mbuf_freem(nd->nd_mrep);
2657 * Kludge: Map ENOENT => 0 assuming that you have a reply to a retry.
2659 if (error == ENOENT)
2666 * Always returns with either uio_resid unchanged, if you are at the
2667 * end of the directory, or uio_resid == 0, with all DIRBLKSIZ chunks
2669 * I felt this would allow caching of directory blocks more easily
2670 * than returning a pertially filled block.
2671 * Directory offset cookies:
2672 * Oh my, what to do with them...
2673 * I can think of three ways to deal with them:
2674 * 1 - have the layer above these RPCs maintain a map between logical
2675 * directory byte offsets and the NFS directory offset cookies
2676 * 2 - pass the opaque directory offset cookies up into userland
2677 * and let the libc functions deal with them, via the system call
2678 * 3 - return them to userland in the "struct dirent", so future versions
2679 * of libc can use them and do whatever is necessary to make things work
2680 * above these rpc calls, in the meantime
2681 * For now, I do #3 by "hiding" the directory offset cookies after the
2682 * d_name field in struct dirent. This is space inside d_reclen that
2683 * will be ignored by anything that doesn't know about them.
2684 * The directory offset cookies are filled in as the last 8 bytes of
2685 * each directory entry, after d_name. Someday, the userland libc
2686 * functions may be able to use these. In the meantime, it satisfies
2687 * OpenBSD's requirements for cookies being returned.
2688 * If expects the directory offset cookie for the read to be in uio_offset
2689 * and returns the one for the next entry after this directory block in
2693 nfsrpc_readdir(vnode_t vp, struct uio *uiop, nfsuint64 *cookiep,
2694 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp,
2695 int *eofp, void *stuff)
2698 struct dirent *dp = NULL;
2700 nfsquad_t cookie, ncookie;
2701 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
2702 struct nfsnode *dnp = VTONFS(vp);
2703 struct nfsvattr nfsva;
2704 struct nfsrv_descript nfsd, *nd = &nfsd;
2705 int error = 0, tlen, more_dirs = 1, blksiz = 0, bigenough = 1;
2706 int reqsize, tryformoredirs = 1, readsize, eof = 0, gotmnton = 0;
2707 long dotfileid, dotdotfileid = 0;
2708 u_int32_t fakefileno = 0xffffffff, rderr;
2710 nfsattrbit_t attrbits, dattrbits;
2711 u_int32_t *tl2 = NULL;
2714 KASSERT(uiop->uio_iovcnt == 1 &&
2715 (uio_uio_resid(uiop) & (DIRBLKSIZ - 1)) == 0,
2716 ("nfs readdirrpc bad uio"));
2719 * There is no point in reading a lot more than uio_resid, however
2720 * adding one additional DIRBLKSIZ makes sense. Since uio_resid
2721 * and nm_readdirsize are both exact multiples of DIRBLKSIZ, this
2722 * will never make readsize > nm_readdirsize.
2724 readsize = nmp->nm_readdirsize;
2725 if (readsize > uio_uio_resid(uiop))
2726 readsize = uio_uio_resid(uiop) + DIRBLKSIZ;
2731 tresid = uio_uio_resid(uiop);
2732 cookie.lval[0] = cookiep->nfsuquad[0];
2733 cookie.lval[1] = cookiep->nfsuquad[1];
2737 * For NFSv4, first create the "." and ".." entries.
2739 if (NFSHASNFSV4(nmp)) {
2740 reqsize = 6 * NFSX_UNSIGNED;
2741 NFSGETATTR_ATTRBIT(&dattrbits);
2742 NFSZERO_ATTRBIT(&attrbits);
2743 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_FILEID);
2744 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TYPE);
2745 if (NFSISSET_ATTRBIT(&dnp->n_vattr.na_suppattr,
2746 NFSATTRBIT_MOUNTEDONFILEID)) {
2747 NFSSETBIT_ATTRBIT(&attrbits,
2748 NFSATTRBIT_MOUNTEDONFILEID);
2752 * Must fake it. Use the fileno, except when the
2753 * fsid is != to that of the directory. For that
2754 * case, generate a fake fileno that is not the same.
2756 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_FSID);
2761 * Joy, oh joy. For V4 we get to hand craft '.' and '..'.
2763 if (uiop->uio_offset == 0) {
2764 NFSCL_REQSTART(nd, NFSPROC_LOOKUPP, vp);
2765 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2766 *tl++ = txdr_unsigned(NFSV4OP_GETFH);
2767 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2768 (void) nfsrv_putattrbit(nd, &attrbits);
2769 error = nfscl_request(nd, vp, p, cred, stuff);
2772 dotfileid = 0; /* Fake out the compiler. */
2773 if ((nd->nd_flag & ND_NOMOREDATA) == 0) {
2774 error = nfsm_loadattr(nd, &nfsva);
2777 dotfileid = nfsva.na_fileid;
2779 if (nd->nd_repstat == 0) {
2780 NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
2781 len = fxdr_unsigned(int, *(tl + 4));
2782 if (len > 0 && len <= NFSX_V4FHMAX)
2783 error = nfsm_advance(nd, NFSM_RNDUP(len), -1);
2787 NFSM_DISSECT(tl, u_int32_t *, 2*NFSX_UNSIGNED);
2788 nfsva.na_mntonfileno = 0xffffffff;
2789 error = nfsv4_loadattr(nd, NULL, &nfsva, NULL,
2790 NULL, 0, NULL, NULL, NULL, NULL, NULL, 0,
2791 NULL, NULL, NULL, p, cred);
2793 dotdotfileid = dotfileid;
2794 } else if (gotmnton) {
2795 if (nfsva.na_mntonfileno != 0xffffffff)
2796 dotdotfileid = nfsva.na_mntonfileno;
2798 dotdotfileid = nfsva.na_fileid;
2799 } else if (nfsva.na_filesid[0] ==
2800 dnp->n_vattr.na_filesid[0] &&
2801 nfsva.na_filesid[1] ==
2802 dnp->n_vattr.na_filesid[1]) {
2803 dotdotfileid = nfsva.na_fileid;
2807 } while (fakefileno ==
2809 dotdotfileid = fakefileno;
2812 } else if (nd->nd_repstat == NFSERR_NOENT) {
2814 * Lookupp returns NFSERR_NOENT when we are
2815 * at the root, so just use the current dir.
2818 dotdotfileid = dotfileid;
2820 error = nd->nd_repstat;
2822 mbuf_freem(nd->nd_mrep);
2826 dp = (struct dirent *) CAST_DOWN(caddr_t, uio_iov_base(uiop));
2827 dp->d_type = DT_DIR;
2828 dp->d_fileno = dotfileid;
2830 dp->d_name[0] = '.';
2831 dp->d_name[1] = '\0';
2832 dp->d_reclen = DIRENT_SIZE(dp) + NFSX_HYPER;
2834 * Just make these offset cookie 0.
2836 tl = (u_int32_t *)&dp->d_name[4];
2839 blksiz += dp->d_reclen;
2840 uio_uio_resid_add(uiop, -(dp->d_reclen));
2841 uiop->uio_offset += dp->d_reclen;
2842 uio_iov_base_add(uiop, dp->d_reclen);
2843 uio_iov_len_add(uiop, -(dp->d_reclen));
2844 dp = (struct dirent *) CAST_DOWN(caddr_t, uio_iov_base(uiop));
2845 dp->d_type = DT_DIR;
2846 dp->d_fileno = dotdotfileid;
2848 dp->d_name[0] = '.';
2849 dp->d_name[1] = '.';
2850 dp->d_name[2] = '\0';
2851 dp->d_reclen = DIRENT_SIZE(dp) + NFSX_HYPER;
2853 * Just make these offset cookie 0.
2855 tl = (u_int32_t *)&dp->d_name[4];
2858 blksiz += dp->d_reclen;
2859 uio_uio_resid_add(uiop, -(dp->d_reclen));
2860 uiop->uio_offset += dp->d_reclen;
2861 uio_iov_base_add(uiop, dp->d_reclen);
2862 uio_iov_len_add(uiop, -(dp->d_reclen));
2864 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_RDATTRERROR);
2866 reqsize = 5 * NFSX_UNSIGNED;
2871 * Loop around doing readdir rpc's of size readsize.
2872 * The stopping criteria is EOF or buffer full.
2874 while (more_dirs && bigenough) {
2876 NFSCL_REQSTART(nd, NFSPROC_READDIR, vp);
2877 if (nd->nd_flag & ND_NFSV2) {
2878 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2879 *tl++ = cookie.lval[1];
2880 *tl = txdr_unsigned(readsize);
2882 NFSM_BUILD(tl, u_int32_t *, reqsize);
2883 *tl++ = cookie.lval[0];
2884 *tl++ = cookie.lval[1];
2885 if (cookie.qval == 0) {
2890 *tl++ = dnp->n_cookieverf.nfsuquad[0];
2891 *tl++ = dnp->n_cookieverf.nfsuquad[1];
2894 if (nd->nd_flag & ND_NFSV4) {
2895 *tl++ = txdr_unsigned(readsize);
2896 *tl = txdr_unsigned(readsize);
2897 (void) nfsrv_putattrbit(nd, &attrbits);
2898 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2899 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2900 (void) nfsrv_putattrbit(nd, &dattrbits);
2902 *tl = txdr_unsigned(readsize);
2905 error = nfscl_request(nd, vp, p, cred, stuff);
2908 if (!(nd->nd_flag & ND_NFSV2)) {
2909 if (nd->nd_flag & ND_NFSV3)
2910 error = nfscl_postop_attr(nd, nap, attrflagp,
2912 if (!nd->nd_repstat && !error) {
2913 NFSM_DISSECT(tl, u_int32_t *, NFSX_HYPER);
2915 dnp->n_cookieverf.nfsuquad[0] = *tl++;
2916 dnp->n_cookieverf.nfsuquad[1] = *tl;
2920 if (nd->nd_repstat || error) {
2922 error = nd->nd_repstat;
2925 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2926 more_dirs = fxdr_unsigned(int, *tl);
2930 /* loop through the dir entries, doctoring them to 4bsd form */
2931 while (more_dirs && bigenough) {
2932 if (nd->nd_flag & ND_NFSV4) {
2933 NFSM_DISSECT(tl, u_int32_t *, 3*NFSX_UNSIGNED);
2934 ncookie.lval[0] = *tl++;
2935 ncookie.lval[1] = *tl++;
2936 len = fxdr_unsigned(int, *tl);
2937 } else if (nd->nd_flag & ND_NFSV3) {
2938 NFSM_DISSECT(tl, u_int32_t *, 3*NFSX_UNSIGNED);
2939 nfsva.na_fileid = fxdr_hyper(tl);
2941 len = fxdr_unsigned(int, *tl);
2943 NFSM_DISSECT(tl, u_int32_t *, 2*NFSX_UNSIGNED);
2945 fxdr_unsigned(long, *tl++);
2946 len = fxdr_unsigned(int, *tl);
2948 if (len <= 0 || len > NFS_MAXNAMLEN) {
2952 tlen = NFSM_RNDUP(len);
2954 tlen += 4; /* To ensure null termination */
2955 left = DIRBLKSIZ - blksiz;
2956 if ((int)(tlen + DIRHDSIZ + NFSX_HYPER) > left) {
2957 dp->d_reclen += left;
2958 uio_iov_base_add(uiop, left);
2959 uio_iov_len_add(uiop, -(left));
2960 uio_uio_resid_add(uiop, -(left));
2961 uiop->uio_offset += left;
2964 if ((int)(tlen + DIRHDSIZ + NFSX_HYPER) > uio_uio_resid(uiop))
2967 dp = (struct dirent *) CAST_DOWN(caddr_t, uio_iov_base(uiop));
2969 dp->d_reclen = tlen + DIRHDSIZ + NFSX_HYPER;
2970 dp->d_type = DT_UNKNOWN;
2971 blksiz += dp->d_reclen;
2972 if (blksiz == DIRBLKSIZ)
2974 uio_uio_resid_add(uiop, -(DIRHDSIZ));
2975 uiop->uio_offset += DIRHDSIZ;
2976 uio_iov_base_add(uiop, DIRHDSIZ);
2977 uio_iov_len_add(uiop, -(DIRHDSIZ));
2978 error = nfsm_mbufuio(nd, uiop, len);
2981 cp = CAST_DOWN(caddr_t, uio_iov_base(uiop));
2983 *cp = '\0'; /* null terminate */
2984 cp += tlen; /* points to cookie storage */
2985 tl2 = (u_int32_t *)cp;
2986 uio_iov_base_add(uiop, (tlen + NFSX_HYPER));
2987 uio_iov_len_add(uiop, -(tlen + NFSX_HYPER));
2988 uio_uio_resid_add(uiop, -(tlen + NFSX_HYPER));
2989 uiop->uio_offset += (tlen + NFSX_HYPER);
2991 error = nfsm_advance(nd, NFSM_RNDUP(len), -1);
2995 if (nd->nd_flag & ND_NFSV4) {
2997 nfsva.na_mntonfileno = 0xffffffff;
2998 error = nfsv4_loadattr(nd, NULL, &nfsva, NULL,
2999 NULL, 0, NULL, NULL, NULL, NULL, NULL, 0,
3000 NULL, NULL, &rderr, p, cred);
3003 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3004 } else if (nd->nd_flag & ND_NFSV3) {
3005 NFSM_DISSECT(tl, u_int32_t *, 3*NFSX_UNSIGNED);
3006 ncookie.lval[0] = *tl++;
3007 ncookie.lval[1] = *tl++;
3009 NFSM_DISSECT(tl, u_int32_t *, 2*NFSX_UNSIGNED);
3010 ncookie.lval[0] = 0;
3011 ncookie.lval[1] = *tl++;
3014 if (nd->nd_flag & ND_NFSV4) {
3019 if (nfsva.na_mntonfileno != 0xffffffff)
3020 dp->d_fileno = nfsva.na_mntonfileno;
3022 dp->d_fileno = nfsva.na_fileid;
3023 } else if (nfsva.na_filesid[0] ==
3024 dnp->n_vattr.na_filesid[0] &&
3025 nfsva.na_filesid[1] ==
3026 dnp->n_vattr.na_filesid[1]) {
3027 dp->d_fileno = nfsva.na_fileid;
3031 } while (fakefileno ==
3033 dp->d_fileno = fakefileno;
3035 dp->d_type = vtonfs_dtype(nfsva.na_type);
3038 dp->d_fileno = nfsva.na_fileid;
3040 *tl2++ = cookiep->nfsuquad[0] = cookie.lval[0] =
3042 *tl2 = cookiep->nfsuquad[1] = cookie.lval[1] =
3045 more_dirs = fxdr_unsigned(int, *tl);
3048 * If at end of rpc data, get the eof boolean
3051 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3052 eof = fxdr_unsigned(int, *tl);
3055 if (nd->nd_flag & ND_NFSV4) {
3056 error = nfscl_postop_attr(nd, nap, attrflagp,
3062 mbuf_freem(nd->nd_mrep);
3066 * Fill last record, iff any, out to a multiple of DIRBLKSIZ
3067 * by increasing d_reclen for the last record.
3070 left = DIRBLKSIZ - blksiz;
3071 dp->d_reclen += left;
3072 uio_iov_base_add(uiop, left);
3073 uio_iov_len_add(uiop, -(left));
3074 uio_uio_resid_add(uiop, -(left));
3075 uiop->uio_offset += left;
3079 * If returning no data, assume end of file.
3080 * If not bigenough, return not end of file, since you aren't
3081 * returning all the data
3082 * Otherwise, return the eof flag from the server.
3085 if (tresid == ((size_t)(uio_uio_resid(uiop))))
3087 else if (!bigenough)
3094 * Add extra empty records to any remaining DIRBLKSIZ chunks.
3096 while (uio_uio_resid(uiop) > 0 && ((size_t)(uio_uio_resid(uiop))) != tresid) {
3097 dp = (struct dirent *) CAST_DOWN(caddr_t, uio_iov_base(uiop));
3098 dp->d_type = DT_UNKNOWN;
3101 dp->d_name[0] = '\0';
3102 tl = (u_int32_t *)&dp->d_name[4];
3103 *tl++ = cookie.lval[0];
3104 *tl = cookie.lval[1];
3105 dp->d_reclen = DIRBLKSIZ;
3106 uio_iov_base_add(uiop, DIRBLKSIZ);
3107 uio_iov_len_add(uiop, -(DIRBLKSIZ));
3108 uio_uio_resid_add(uiop, -(DIRBLKSIZ));
3109 uiop->uio_offset += DIRBLKSIZ;
3113 if (nd->nd_mrep != NULL)
3114 mbuf_freem(nd->nd_mrep);
3120 * NFS V3 readdir plus RPC. Used in place of nfsrpc_readdir().
3121 * (Also used for NFS V4 when mount flag set.)
3122 * (ditto above w.r.t. multiple of DIRBLKSIZ, etc.)
3125 nfsrpc_readdirplus(vnode_t vp, struct uio *uiop, nfsuint64 *cookiep,
3126 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp,
3127 int *eofp, void *stuff)
3130 struct dirent *dp = NULL;
3132 vnode_t newvp = NULLVP;
3133 struct nfsrv_descript nfsd, *nd = &nfsd;
3134 struct nameidata nami, *ndp = &nami;
3135 struct componentname *cnp = &ndp->ni_cnd;
3136 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
3137 struct nfsnode *dnp = VTONFS(vp), *np;
3138 struct nfsvattr nfsva;
3140 nfsquad_t cookie, ncookie;
3141 int error = 0, tlen, more_dirs = 1, blksiz = 0, bigenough = 1;
3142 int attrflag, tryformoredirs = 1, eof = 0, gotmnton = 0;
3143 int isdotdot = 0, unlocknewvp = 0;
3144 long dotfileid, dotdotfileid = 0, fileno = 0;
3146 nfsattrbit_t attrbits, dattrbits;
3148 u_int32_t *tl2 = NULL, fakefileno = 0xffffffff, rderr;
3149 struct timespec dctime;
3151 KASSERT(uiop->uio_iovcnt == 1 &&
3152 (uio_uio_resid(uiop) & (DIRBLKSIZ - 1)) == 0,
3153 ("nfs readdirplusrpc bad uio"));
3154 timespecclear(&dctime);
3160 cookie.lval[0] = cookiep->nfsuquad[0];
3161 cookie.lval[1] = cookiep->nfsuquad[1];
3162 tresid = uio_uio_resid(uiop);
3165 * For NFSv4, first create the "." and ".." entries.
3167 if (NFSHASNFSV4(nmp)) {
3168 NFSGETATTR_ATTRBIT(&dattrbits);
3169 NFSZERO_ATTRBIT(&attrbits);
3170 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_FILEID);
3171 if (NFSISSET_ATTRBIT(&dnp->n_vattr.na_suppattr,
3172 NFSATTRBIT_MOUNTEDONFILEID)) {
3173 NFSSETBIT_ATTRBIT(&attrbits,
3174 NFSATTRBIT_MOUNTEDONFILEID);
3178 * Must fake it. Use the fileno, except when the
3179 * fsid is != to that of the directory. For that
3180 * case, generate a fake fileno that is not the same.
3182 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_FSID);
3187 * Joy, oh joy. For V4 we get to hand craft '.' and '..'.
3189 if (uiop->uio_offset == 0) {
3190 NFSCL_REQSTART(nd, NFSPROC_LOOKUPP, vp);
3191 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
3192 *tl++ = txdr_unsigned(NFSV4OP_GETFH);
3193 *tl = txdr_unsigned(NFSV4OP_GETATTR);
3194 (void) nfsrv_putattrbit(nd, &attrbits);
3195 error = nfscl_request(nd, vp, p, cred, stuff);
3198 dotfileid = 0; /* Fake out the compiler. */
3199 if ((nd->nd_flag & ND_NOMOREDATA) == 0) {
3200 error = nfsm_loadattr(nd, &nfsva);
3203 dctime = nfsva.na_ctime;
3204 dotfileid = nfsva.na_fileid;
3206 if (nd->nd_repstat == 0) {
3207 NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
3208 len = fxdr_unsigned(int, *(tl + 4));
3209 if (len > 0 && len <= NFSX_V4FHMAX)
3210 error = nfsm_advance(nd, NFSM_RNDUP(len), -1);
3214 NFSM_DISSECT(tl, u_int32_t *, 2*NFSX_UNSIGNED);
3215 nfsva.na_mntonfileno = 0xffffffff;
3216 error = nfsv4_loadattr(nd, NULL, &nfsva, NULL,
3217 NULL, 0, NULL, NULL, NULL, NULL, NULL, 0,
3218 NULL, NULL, NULL, p, cred);
3220 dotdotfileid = dotfileid;
3221 } else if (gotmnton) {
3222 if (nfsva.na_mntonfileno != 0xffffffff)
3223 dotdotfileid = nfsva.na_mntonfileno;
3225 dotdotfileid = nfsva.na_fileid;
3226 } else if (nfsva.na_filesid[0] ==
3227 dnp->n_vattr.na_filesid[0] &&
3228 nfsva.na_filesid[1] ==
3229 dnp->n_vattr.na_filesid[1]) {
3230 dotdotfileid = nfsva.na_fileid;
3234 } while (fakefileno ==
3236 dotdotfileid = fakefileno;
3239 } else if (nd->nd_repstat == NFSERR_NOENT) {
3241 * Lookupp returns NFSERR_NOENT when we are
3242 * at the root, so just use the current dir.
3245 dotdotfileid = dotfileid;
3247 error = nd->nd_repstat;
3249 mbuf_freem(nd->nd_mrep);
3253 dp = (struct dirent *)uio_iov_base(uiop);
3254 dp->d_type = DT_DIR;
3255 dp->d_fileno = dotfileid;
3257 dp->d_name[0] = '.';
3258 dp->d_name[1] = '\0';
3259 dp->d_reclen = DIRENT_SIZE(dp) + NFSX_HYPER;
3261 * Just make these offset cookie 0.
3263 tl = (u_int32_t *)&dp->d_name[4];
3266 blksiz += dp->d_reclen;
3267 uio_uio_resid_add(uiop, -(dp->d_reclen));
3268 uiop->uio_offset += dp->d_reclen;
3269 uio_iov_base_add(uiop, dp->d_reclen);
3270 uio_iov_len_add(uiop, -(dp->d_reclen));
3271 dp = (struct dirent *)uio_iov_base(uiop);
3272 dp->d_type = DT_DIR;
3273 dp->d_fileno = dotdotfileid;
3275 dp->d_name[0] = '.';
3276 dp->d_name[1] = '.';
3277 dp->d_name[2] = '\0';
3278 dp->d_reclen = DIRENT_SIZE(dp) + NFSX_HYPER;
3280 * Just make these offset cookie 0.
3282 tl = (u_int32_t *)&dp->d_name[4];
3285 blksiz += dp->d_reclen;
3286 uio_uio_resid_add(uiop, -(dp->d_reclen));
3287 uiop->uio_offset += dp->d_reclen;
3288 uio_iov_base_add(uiop, dp->d_reclen);
3289 uio_iov_len_add(uiop, -(dp->d_reclen));
3291 NFSREADDIRPLUS_ATTRBIT(&attrbits);
3293 NFSSETBIT_ATTRBIT(&attrbits,
3294 NFSATTRBIT_MOUNTEDONFILEID);
3298 * Loop around doing readdir rpc's of size nm_readdirsize.
3299 * The stopping criteria is EOF or buffer full.
3301 while (more_dirs && bigenough) {
3303 NFSCL_REQSTART(nd, NFSPROC_READDIRPLUS, vp);
3304 NFSM_BUILD(tl, u_int32_t *, 6 * NFSX_UNSIGNED);
3305 *tl++ = cookie.lval[0];
3306 *tl++ = cookie.lval[1];
3307 if (cookie.qval == 0) {
3312 *tl++ = dnp->n_cookieverf.nfsuquad[0];
3313 *tl++ = dnp->n_cookieverf.nfsuquad[1];
3316 *tl++ = txdr_unsigned(nmp->nm_readdirsize);
3317 *tl = txdr_unsigned(nmp->nm_readdirsize);
3318 if (nd->nd_flag & ND_NFSV4) {
3319 (void) nfsrv_putattrbit(nd, &attrbits);
3320 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
3321 *tl = txdr_unsigned(NFSV4OP_GETATTR);
3322 (void) nfsrv_putattrbit(nd, &dattrbits);
3324 error = nfscl_request(nd, vp, p, cred, stuff);
3327 if (nd->nd_flag & ND_NFSV3)
3328 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
3329 if (nd->nd_repstat || error) {
3331 error = nd->nd_repstat;
3334 if ((nd->nd_flag & ND_NFSV3) != 0 && *attrflagp != 0)
3335 dctime = nap->na_ctime;
3336 NFSM_DISSECT(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
3338 dnp->n_cookieverf.nfsuquad[0] = *tl++;
3339 dnp->n_cookieverf.nfsuquad[1] = *tl++;
3341 more_dirs = fxdr_unsigned(int, *tl);
3345 /* loop through the dir entries, doctoring them to 4bsd form */
3346 while (more_dirs && bigenough) {
3347 NFSM_DISSECT(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
3348 if (nd->nd_flag & ND_NFSV4) {
3349 ncookie.lval[0] = *tl++;
3350 ncookie.lval[1] = *tl++;
3352 fileno = fxdr_unsigned(long, *++tl);
3355 len = fxdr_unsigned(int, *tl);
3356 if (len <= 0 || len > NFS_MAXNAMLEN) {
3360 tlen = NFSM_RNDUP(len);
3362 tlen += 4; /* To ensure null termination */
3363 left = DIRBLKSIZ - blksiz;
3364 if ((tlen + DIRHDSIZ + NFSX_HYPER) > left) {
3365 dp->d_reclen += left;
3366 uio_iov_base_add(uiop, left);
3367 uio_iov_len_add(uiop, -(left));
3368 uio_uio_resid_add(uiop, -(left));
3369 uiop->uio_offset += left;
3372 if ((tlen + DIRHDSIZ + NFSX_HYPER) > uio_uio_resid(uiop))
3375 dp = (struct dirent *)uio_iov_base(uiop);
3377 dp->d_reclen = tlen + DIRHDSIZ + NFSX_HYPER;
3378 dp->d_type = DT_UNKNOWN;
3379 blksiz += dp->d_reclen;
3380 if (blksiz == DIRBLKSIZ)
3382 uio_uio_resid_add(uiop, -(DIRHDSIZ));
3383 uiop->uio_offset += DIRHDSIZ;
3384 uio_iov_base_add(uiop, DIRHDSIZ);
3385 uio_iov_len_add(uiop, -(DIRHDSIZ));
3386 cnp->cn_nameptr = uio_iov_base(uiop);
3387 cnp->cn_namelen = len;
3389 error = nfsm_mbufuio(nd, uiop, len);
3392 cp = uio_iov_base(uiop);
3395 cp += tlen; /* points to cookie storage */
3396 tl2 = (u_int32_t *)cp;
3397 if (len == 2 && cnp->cn_nameptr[0] == '.' &&
3398 cnp->cn_nameptr[1] == '.')
3402 uio_iov_base_add(uiop, (tlen + NFSX_HYPER));
3403 uio_iov_len_add(uiop, -(tlen + NFSX_HYPER));
3404 uio_uio_resid_add(uiop, -(tlen + NFSX_HYPER));
3405 uiop->uio_offset += (tlen + NFSX_HYPER);
3407 error = nfsm_advance(nd, NFSM_RNDUP(len), -1);
3412 if (nd->nd_flag & ND_NFSV3) {
3413 NFSM_DISSECT(tl, u_int32_t *, 3*NFSX_UNSIGNED);
3414 ncookie.lval[0] = *tl++;
3415 ncookie.lval[1] = *tl++;
3416 attrflag = fxdr_unsigned(int, *tl);
3418 error = nfsm_loadattr(nd, &nfsva);
3422 NFSM_DISSECT(tl,u_int32_t *,NFSX_UNSIGNED);
3424 error = nfsm_getfh(nd, &nfhp);
3428 if (!attrflag && nfhp != NULL) {
3429 FREE((caddr_t)nfhp, M_NFSFH);
3434 nfsva.na_mntonfileno = 0xffffffff;
3435 error = nfsv4_loadattr(nd, NULL, &nfsva, &nfhp,
3436 NULL, 0, NULL, NULL, NULL, NULL, NULL, 0,
3437 NULL, NULL, &rderr, p, cred);
3443 if (nd->nd_flag & ND_NFSV4) {
3446 } else if (gotmnton) {
3447 if (nfsva.na_mntonfileno != 0xffffffff)
3448 dp->d_fileno = nfsva.na_mntonfileno;
3450 dp->d_fileno = nfsva.na_fileid;
3451 } else if (nfsva.na_filesid[0] ==
3452 dnp->n_vattr.na_filesid[0] &&
3453 nfsva.na_filesid[1] ==
3454 dnp->n_vattr.na_filesid[1]) {
3455 dp->d_fileno = nfsva.na_fileid;
3459 } while (fakefileno ==
3461 dp->d_fileno = fakefileno;
3464 dp->d_fileno = fileno;
3466 *tl2++ = cookiep->nfsuquad[0] = cookie.lval[0] =
3468 *tl2 = cookiep->nfsuquad[1] = cookie.lval[1] =
3472 if (NFSRV_CMPFH(nfhp->nfh_fh, nfhp->nfh_len,
3473 dnp->n_fhp->nfh_fh, dnp->n_fhp->nfh_len)) {
3477 FREE((caddr_t)nfhp, M_NFSFH);
3479 } else if (isdotdot != 0) {
3481 * Skip doing a nfscl_nget() call for "..".
3482 * There's a race between acquiring the nfs
3483 * node here and lookups that look for the
3484 * directory being read (in the parent).
3485 * It would try to get a lock on ".." here,
3486 * owning the lock on the directory being
3487 * read. Lookup will hold the lock on ".."
3488 * and try to acquire the lock on the
3489 * directory being read.
3490 * If the directory is unlocked/relocked,
3491 * then there is a LOR with the buflock
3494 free(nfhp, M_NFSFH);
3496 error = nfscl_nget(vnode_mount(vp), vp,
3497 nfhp, cnp, p, &np, NULL, LK_EXCLUSIVE);
3504 if (newvp != NULLVP) {
3505 error = nfscl_loadattrcache(&newvp,
3506 &nfsva, NULL, NULL, 0, 0);
3515 vtonfs_dtype(np->n_vattr.na_type);
3517 NFSCNHASH(cnp, HASHINIT);
3518 if (cnp->cn_namelen <= NCHNAMLEN &&
3519 (newvp->v_type != VDIR ||
3520 dctime.tv_sec != 0)) {
3521 cache_enter_time(ndp->ni_dvp,
3524 newvp->v_type != VDIR ? NULL :
3534 } else if (nfhp != NULL) {
3535 FREE((caddr_t)nfhp, M_NFSFH);
3537 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3538 more_dirs = fxdr_unsigned(int, *tl);
3541 * If at end of rpc data, get the eof boolean
3544 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3545 eof = fxdr_unsigned(int, *tl);
3548 if (nd->nd_flag & ND_NFSV4) {
3549 error = nfscl_postop_attr(nd, nap, attrflagp,
3555 mbuf_freem(nd->nd_mrep);
3559 * Fill last record, iff any, out to a multiple of DIRBLKSIZ
3560 * by increasing d_reclen for the last record.
3563 left = DIRBLKSIZ - blksiz;
3564 dp->d_reclen += left;
3565 uio_iov_base_add(uiop, left);
3566 uio_iov_len_add(uiop, -(left));
3567 uio_uio_resid_add(uiop, -(left));
3568 uiop->uio_offset += left;
3572 * If returning no data, assume end of file.
3573 * If not bigenough, return not end of file, since you aren't
3574 * returning all the data
3575 * Otherwise, return the eof flag from the server.
3578 if (tresid == uio_uio_resid(uiop))
3580 else if (!bigenough)
3587 * Add extra empty records to any remaining DIRBLKSIZ chunks.
3589 while (uio_uio_resid(uiop) > 0 && uio_uio_resid(uiop) != tresid) {
3590 dp = (struct dirent *)uio_iov_base(uiop);
3591 dp->d_type = DT_UNKNOWN;
3594 dp->d_name[0] = '\0';
3595 tl = (u_int32_t *)&dp->d_name[4];
3596 *tl++ = cookie.lval[0];
3597 *tl = cookie.lval[1];
3598 dp->d_reclen = DIRBLKSIZ;
3599 uio_iov_base_add(uiop, DIRBLKSIZ);
3600 uio_iov_len_add(uiop, -(DIRBLKSIZ));
3601 uio_uio_resid_add(uiop, -(DIRBLKSIZ));
3602 uiop->uio_offset += DIRBLKSIZ;
3606 if (nd->nd_mrep != NULL)
3607 mbuf_freem(nd->nd_mrep);
3616 nfsrpc_commit(vnode_t vp, u_quad_t offset, int cnt, struct ucred *cred,
3617 NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, void *stuff)
3620 struct nfsrv_descript nfsd, *nd = &nfsd;
3621 nfsattrbit_t attrbits;
3623 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
3626 NFSCL_REQSTART(nd, NFSPROC_COMMIT, vp);
3627 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
3628 txdr_hyper(offset, tl);
3630 *tl = txdr_unsigned(cnt);
3631 if (nd->nd_flag & ND_NFSV4) {
3633 * And do a Getattr op.
3635 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
3636 *tl = txdr_unsigned(NFSV4OP_GETATTR);
3637 NFSGETATTR_ATTRBIT(&attrbits);
3638 (void) nfsrv_putattrbit(nd, &attrbits);
3640 error = nfscl_request(nd, vp, p, cred, stuff);
3643 error = nfscl_wcc_data(nd, vp, nap, attrflagp, NULL, stuff);
3644 if (!error && !nd->nd_repstat) {
3645 NFSM_DISSECT(tl, u_int32_t *, NFSX_VERF);
3647 if (NFSBCMP(nmp->nm_verf, tl, NFSX_VERF)) {
3648 NFSBCOPY(tl, nmp->nm_verf, NFSX_VERF);
3649 nd->nd_repstat = NFSERR_STALEWRITEVERF;
3652 if (nd->nd_flag & ND_NFSV4)
3653 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
3656 if (!error && nd->nd_repstat)
3657 error = nd->nd_repstat;
3658 mbuf_freem(nd->nd_mrep);
3663 * NFS byte range lock rpc.
3664 * (Mostly just calls one of the three lower level RPC routines.)
3667 nfsrpc_advlock(vnode_t vp, off_t size, int op, struct flock *fl,
3668 int reclaim, struct ucred *cred, NFSPROC_T *p, void *id, int flags)
3670 struct nfscllockowner *lp;
3671 struct nfsclclient *clp;
3673 struct nfsrv_descript nfsd, *nd = &nfsd;
3674 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
3677 u_int32_t clidrev = 0;
3678 int error = 0, newone = 0, expireret = 0, retrycnt, donelocally;
3682 * Convert the flock structure into a start and end and do POSIX
3685 switch (fl->l_whence) {
3689 * Caller is responsible for adding any necessary offset
3690 * when SEEK_CUR is used.
3692 start = fl->l_start;
3696 start = size + fl->l_start;
3697 off = size + fl->l_start;
3704 if (fl->l_len != 0) {
3705 end = start + fl->l_len - 1;
3716 if (op == F_GETLK) {
3717 error = nfscl_getcl(vnode_mount(vp), cred, p, 1, &clp);
3720 error = nfscl_lockt(vp, clp, off, len, fl, p, id, flags);
3722 clidrev = clp->nfsc_clientidrev;
3723 error = nfsrpc_lockt(nd, vp, clp, off, len, fl, cred,
3725 } else if (error == -1) {
3728 nfscl_clientrelease(clp);
3729 } else if (op == F_UNLCK && fl->l_type == F_UNLCK) {
3731 * We must loop around for all lockowner cases.
3734 error = nfscl_getcl(vnode_mount(vp), cred, p, 1, &clp);
3738 error = nfscl_relbytelock(vp, off, len, cred, p, callcnt,
3739 clp, id, flags, &lp, &dorpc);
3741 * If it returns a NULL lp, we're done.
3745 nfscl_clientrelease(clp);
3747 nfscl_releasealllocks(clp, vp, p, id, flags);
3750 if (nmp->nm_clp != NULL)
3751 clidrev = nmp->nm_clp->nfsc_clientidrev;
3755 * If the server doesn't support Posix lock semantics,
3756 * only allow locks on the entire file, since it won't
3757 * handle overlapping byte ranges.
3758 * There might still be a problem when a lock
3759 * upgrade/downgrade (read<->write) occurs, since the
3760 * server "might" expect an unlock first?
3762 if (dorpc && (lp->nfsl_open->nfso_posixlock ||
3763 (off == 0 && len == NFS64BITSSET))) {
3765 * Since the lock records will go away, we must
3766 * wait for grace and delay here.
3769 error = nfsrpc_locku(nd, nmp, lp, off, len,
3770 NFSV4LOCKT_READ, cred, p, 0);
3771 if ((nd->nd_repstat == NFSERR_GRACE ||
3772 nd->nd_repstat == NFSERR_DELAY) &&
3774 (void) nfs_catnap(PZERO, (int)nd->nd_repstat,
3776 } while ((nd->nd_repstat == NFSERR_GRACE ||
3777 nd->nd_repstat == NFSERR_DELAY) && error == 0);
3780 } while (error == 0 && nd->nd_repstat == 0);
3781 nfscl_releasealllocks(clp, vp, p, id, flags);
3782 } else if (op == F_SETLK) {
3783 error = nfscl_getbytelock(vp, off, len, fl->l_type, cred, p,
3784 NULL, 0, id, flags, NULL, NULL, &lp, &newone, &donelocally);
3785 if (error || donelocally) {
3788 if (nmp->nm_clp != NULL)
3789 clidrev = nmp->nm_clp->nfsc_clientidrev;
3792 nfhp = VTONFS(vp)->n_fhp;
3793 if (!lp->nfsl_open->nfso_posixlock &&
3794 (off != 0 || len != NFS64BITSSET)) {
3797 error = nfsrpc_lock(nd, nmp, vp, nfhp->nfh_fh,
3798 nfhp->nfh_len, lp, newone, reclaim, off,
3799 len, fl->l_type, cred, p, 0);
3802 error = nd->nd_repstat;
3803 nfscl_lockrelease(lp, error, newone);
3808 error = nd->nd_repstat;
3809 if (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
3810 error == NFSERR_STALEDONTRECOVER ||
3811 error == NFSERR_STALECLIENTID || error == NFSERR_DELAY ||
3812 error == NFSERR_BADSESSION) {
3813 (void) nfs_catnap(PZERO, error, "nfs_advlock");
3814 } else if ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID)
3816 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
3819 } while (error == NFSERR_GRACE ||
3820 error == NFSERR_STALECLIENTID || error == NFSERR_DELAY ||
3821 error == NFSERR_STALEDONTRECOVER || error == NFSERR_STALESTATEID ||
3822 error == NFSERR_BADSESSION ||
3823 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
3824 expireret == 0 && clidrev != 0 && retrycnt < 4));
3825 if (error && retrycnt >= 4)
3831 * The lower level routine for the LockT case.
3834 nfsrpc_lockt(struct nfsrv_descript *nd, vnode_t vp,
3835 struct nfsclclient *clp, u_int64_t off, u_int64_t len, struct flock *fl,
3836 struct ucred *cred, NFSPROC_T *p, void *id, int flags)
3839 int error, type, size;
3840 uint8_t own[NFSV4CL_LOCKNAMELEN + NFSX_V4FHMAX];
3842 struct nfsmount *nmp;
3844 nmp = VFSTONFS(vp->v_mount);
3845 NFSCL_REQSTART(nd, NFSPROC_LOCKT, vp);
3846 NFSM_BUILD(tl, u_int32_t *, 7 * NFSX_UNSIGNED);
3847 if (fl->l_type == F_RDLCK)
3848 *tl++ = txdr_unsigned(NFSV4LOCKT_READ);
3850 *tl++ = txdr_unsigned(NFSV4LOCKT_WRITE);
3851 txdr_hyper(off, tl);
3853 txdr_hyper(len, tl);
3855 *tl++ = NFSMNT_MDSSESSION(nmp)->nfsess_clientid.lval[0];
3856 *tl = NFSMNT_MDSSESSION(nmp)->nfsess_clientid.lval[1];
3857 nfscl_filllockowner(id, own, flags);
3859 NFSBCOPY(np->n_fhp->nfh_fh, &own[NFSV4CL_LOCKNAMELEN],
3860 np->n_fhp->nfh_len);
3861 (void)nfsm_strtom(nd, own, NFSV4CL_LOCKNAMELEN + np->n_fhp->nfh_len);
3862 error = nfscl_request(nd, vp, p, cred, NULL);
3865 if (nd->nd_repstat == 0) {
3866 fl->l_type = F_UNLCK;
3867 } else if (nd->nd_repstat == NFSERR_DENIED) {
3869 fl->l_whence = SEEK_SET;
3870 NFSM_DISSECT(tl, u_int32_t *, 8 * NFSX_UNSIGNED);
3871 fl->l_start = fxdr_hyper(tl);
3873 len = fxdr_hyper(tl);
3875 if (len == NFS64BITSSET)
3879 type = fxdr_unsigned(int, *tl++);
3880 if (type == NFSV4LOCKT_WRITE)
3881 fl->l_type = F_WRLCK;
3883 fl->l_type = F_RDLCK;
3885 * XXX For now, I have no idea what to do with the
3886 * conflicting lock_owner, so I'll just set the pid == 0
3887 * and skip over the lock_owner.
3889 fl->l_pid = (pid_t)0;
3891 size = fxdr_unsigned(int, *tl);
3892 if (size < 0 || size > NFSV4_OPAQUELIMIT)
3895 error = nfsm_advance(nd, NFSM_RNDUP(size), -1);
3896 } else if (nd->nd_repstat == NFSERR_STALECLIENTID ||
3897 nd->nd_repstat == NFSERR_BADSESSION)
3898 nfscl_initiate_recovery(clp);
3900 mbuf_freem(nd->nd_mrep);
3905 * Lower level function that performs the LockU RPC.
3908 nfsrpc_locku(struct nfsrv_descript *nd, struct nfsmount *nmp,
3909 struct nfscllockowner *lp, u_int64_t off, u_int64_t len,
3910 u_int32_t type, struct ucred *cred, NFSPROC_T *p, int syscred)
3915 nfscl_reqstart(nd, NFSPROC_LOCKU, nmp, lp->nfsl_open->nfso_fh,
3916 lp->nfsl_open->nfso_fhlen, NULL, NULL);
3917 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID + 6 * NFSX_UNSIGNED);
3918 *tl++ = txdr_unsigned(type);
3919 *tl = txdr_unsigned(lp->nfsl_seqid);
3920 if (nfstest_outofseq &&
3921 (arc4random() % nfstest_outofseq) == 0)
3922 *tl = txdr_unsigned(lp->nfsl_seqid + 1);
3924 if (NFSHASNFSV4N(nmp))
3927 *tl++ = lp->nfsl_stateid.seqid;
3928 *tl++ = lp->nfsl_stateid.other[0];
3929 *tl++ = lp->nfsl_stateid.other[1];
3930 *tl++ = lp->nfsl_stateid.other[2];
3931 txdr_hyper(off, tl);
3933 txdr_hyper(len, tl);
3935 nd->nd_flag |= ND_USEGSSNAME;
3936 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
3937 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
3938 NFSCL_INCRSEQID(lp->nfsl_seqid, nd);
3941 if (nd->nd_repstat == 0) {
3942 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID);
3943 lp->nfsl_stateid.seqid = *tl++;
3944 lp->nfsl_stateid.other[0] = *tl++;
3945 lp->nfsl_stateid.other[1] = *tl++;
3946 lp->nfsl_stateid.other[2] = *tl;
3947 } else if (nd->nd_repstat == NFSERR_STALESTATEID ||
3948 nd->nd_repstat == NFSERR_BADSESSION)
3949 nfscl_initiate_recovery(lp->nfsl_open->nfso_own->nfsow_clp);
3951 mbuf_freem(nd->nd_mrep);
3956 * The actual Lock RPC.
3959 nfsrpc_lock(struct nfsrv_descript *nd, struct nfsmount *nmp, vnode_t vp,
3960 u_int8_t *nfhp, int fhlen, struct nfscllockowner *lp, int newone,
3961 int reclaim, u_int64_t off, u_int64_t len, short type, struct ucred *cred,
3962 NFSPROC_T *p, int syscred)
3966 uint8_t own[NFSV4CL_LOCKNAMELEN + NFSX_V4FHMAX];
3968 nfscl_reqstart(nd, NFSPROC_LOCK, nmp, nfhp, fhlen, NULL, NULL);
3969 NFSM_BUILD(tl, u_int32_t *, 7 * NFSX_UNSIGNED);
3970 if (type == F_RDLCK)
3971 *tl++ = txdr_unsigned(NFSV4LOCKT_READ);
3973 *tl++ = txdr_unsigned(NFSV4LOCKT_WRITE);
3974 *tl++ = txdr_unsigned(reclaim);
3975 txdr_hyper(off, tl);
3977 txdr_hyper(len, tl);
3981 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID +
3982 2 * NFSX_UNSIGNED + NFSX_HYPER);
3983 *tl++ = txdr_unsigned(lp->nfsl_open->nfso_own->nfsow_seqid);
3984 if (NFSHASNFSV4N(nmp))
3987 *tl++ = lp->nfsl_open->nfso_stateid.seqid;
3988 *tl++ = lp->nfsl_open->nfso_stateid.other[0];
3989 *tl++ = lp->nfsl_open->nfso_stateid.other[1];
3990 *tl++ = lp->nfsl_open->nfso_stateid.other[2];
3991 *tl++ = txdr_unsigned(lp->nfsl_seqid);
3992 *tl++ = NFSMNT_MDSSESSION(nmp)->nfsess_clientid.lval[0];
3993 *tl = NFSMNT_MDSSESSION(nmp)->nfsess_clientid.lval[1];
3994 NFSBCOPY(lp->nfsl_owner, own, NFSV4CL_LOCKNAMELEN);
3995 NFSBCOPY(nfhp, &own[NFSV4CL_LOCKNAMELEN], fhlen);
3996 (void)nfsm_strtom(nd, own, NFSV4CL_LOCKNAMELEN + fhlen);
3999 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID + NFSX_UNSIGNED);
4000 if (NFSHASNFSV4N(nmp))
4003 *tl++ = lp->nfsl_stateid.seqid;
4004 *tl++ = lp->nfsl_stateid.other[0];
4005 *tl++ = lp->nfsl_stateid.other[1];
4006 *tl++ = lp->nfsl_stateid.other[2];
4007 *tl = txdr_unsigned(lp->nfsl_seqid);
4008 if (nfstest_outofseq &&
4009 (arc4random() % nfstest_outofseq) == 0)
4010 *tl = txdr_unsigned(lp->nfsl_seqid + 1);
4013 nd->nd_flag |= ND_USEGSSNAME;
4014 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, vp, p, cred,
4015 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4019 NFSCL_INCRSEQID(lp->nfsl_open->nfso_own->nfsow_seqid, nd);
4020 NFSCL_INCRSEQID(lp->nfsl_seqid, nd);
4021 if (nd->nd_repstat == 0) {
4022 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID);
4023 lp->nfsl_stateid.seqid = *tl++;
4024 lp->nfsl_stateid.other[0] = *tl++;
4025 lp->nfsl_stateid.other[1] = *tl++;
4026 lp->nfsl_stateid.other[2] = *tl;
4027 } else if (nd->nd_repstat == NFSERR_DENIED) {
4028 NFSM_DISSECT(tl, u_int32_t *, 8 * NFSX_UNSIGNED);
4029 size = fxdr_unsigned(int, *(tl + 7));
4030 if (size < 0 || size > NFSV4_OPAQUELIMIT)
4033 error = nfsm_advance(nd, NFSM_RNDUP(size), -1);
4034 } else if (nd->nd_repstat == NFSERR_STALESTATEID ||
4035 nd->nd_repstat == NFSERR_BADSESSION)
4036 nfscl_initiate_recovery(lp->nfsl_open->nfso_own->nfsow_clp);
4038 mbuf_freem(nd->nd_mrep);
4044 * (always called with the vp for the mount point)
4047 nfsrpc_statfs(vnode_t vp, struct nfsstatfs *sbp, struct nfsfsinfo *fsp,
4048 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp,
4051 u_int32_t *tl = NULL;
4052 struct nfsrv_descript nfsd, *nd = &nfsd;
4053 struct nfsmount *nmp;
4054 nfsattrbit_t attrbits;
4058 nmp = VFSTONFS(vnode_mount(vp));
4059 if (NFSHASNFSV4(nmp)) {
4061 * For V4, you actually do a getattr.
4063 NFSCL_REQSTART(nd, NFSPROC_GETATTR, vp);
4064 NFSSTATFS_GETATTRBIT(&attrbits);
4065 (void) nfsrv_putattrbit(nd, &attrbits);
4066 nd->nd_flag |= ND_USEGSSNAME;
4067 error = nfscl_request(nd, vp, p, cred, stuff);
4070 if (nd->nd_repstat == 0) {
4071 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0,
4072 NULL, NULL, sbp, fsp, NULL, 0, NULL, NULL, NULL, p,
4075 nmp->nm_fsid[0] = nap->na_filesid[0];
4076 nmp->nm_fsid[1] = nap->na_filesid[1];
4077 NFSSETHASSETFSID(nmp);
4081 error = nd->nd_repstat;
4086 NFSCL_REQSTART(nd, NFSPROC_FSSTAT, vp);
4087 error = nfscl_request(nd, vp, p, cred, stuff);
4090 if (nd->nd_flag & ND_NFSV3) {
4091 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
4095 if (nd->nd_repstat) {
4096 error = nd->nd_repstat;
4099 NFSM_DISSECT(tl, u_int32_t *,
4100 NFSX_STATFS(nd->nd_flag & ND_NFSV3));
4102 if (NFSHASNFSV3(nmp)) {
4103 sbp->sf_tbytes = fxdr_hyper(tl); tl += 2;
4104 sbp->sf_fbytes = fxdr_hyper(tl); tl += 2;
4105 sbp->sf_abytes = fxdr_hyper(tl); tl += 2;
4106 sbp->sf_tfiles = fxdr_hyper(tl); tl += 2;
4107 sbp->sf_ffiles = fxdr_hyper(tl); tl += 2;
4108 sbp->sf_afiles = fxdr_hyper(tl); tl += 2;
4109 sbp->sf_invarsec = fxdr_unsigned(u_int32_t, *tl);
4110 } else if (NFSHASNFSV4(nmp) == 0) {
4111 sbp->sf_tsize = fxdr_unsigned(u_int32_t, *tl++);
4112 sbp->sf_bsize = fxdr_unsigned(u_int32_t, *tl++);
4113 sbp->sf_blocks = fxdr_unsigned(u_int32_t, *tl++);
4114 sbp->sf_bfree = fxdr_unsigned(u_int32_t, *tl++);
4115 sbp->sf_bavail = fxdr_unsigned(u_int32_t, *tl);
4118 mbuf_freem(nd->nd_mrep);
4126 nfsrpc_pathconf(vnode_t vp, struct nfsv3_pathconf *pc,
4127 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp,
4130 struct nfsrv_descript nfsd, *nd = &nfsd;
4131 struct nfsmount *nmp;
4133 nfsattrbit_t attrbits;
4137 nmp = VFSTONFS(vnode_mount(vp));
4138 if (NFSHASNFSV4(nmp)) {
4140 * For V4, you actually do a getattr.
4142 NFSCL_REQSTART(nd, NFSPROC_GETATTR, vp);
4143 NFSPATHCONF_GETATTRBIT(&attrbits);
4144 (void) nfsrv_putattrbit(nd, &attrbits);
4145 nd->nd_flag |= ND_USEGSSNAME;
4146 error = nfscl_request(nd, vp, p, cred, stuff);
4149 if (nd->nd_repstat == 0) {
4150 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0,
4151 pc, NULL, NULL, NULL, NULL, 0, NULL, NULL, NULL, p,
4156 error = nd->nd_repstat;
4159 NFSCL_REQSTART(nd, NFSPROC_PATHCONF, vp);
4160 error = nfscl_request(nd, vp, p, cred, stuff);
4163 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
4164 if (nd->nd_repstat && !error)
4165 error = nd->nd_repstat;
4167 NFSM_DISSECT(tl, u_int32_t *, NFSX_V3PATHCONF);
4168 pc->pc_linkmax = fxdr_unsigned(u_int32_t, *tl++);
4169 pc->pc_namemax = fxdr_unsigned(u_int32_t, *tl++);
4170 pc->pc_notrunc = fxdr_unsigned(u_int32_t, *tl++);
4171 pc->pc_chownrestricted =
4172 fxdr_unsigned(u_int32_t, *tl++);
4173 pc->pc_caseinsensitive =
4174 fxdr_unsigned(u_int32_t, *tl++);
4175 pc->pc_casepreserving = fxdr_unsigned(u_int32_t, *tl);
4179 mbuf_freem(nd->nd_mrep);
4184 * nfs version 3 fsinfo rpc call
4187 nfsrpc_fsinfo(vnode_t vp, struct nfsfsinfo *fsp, struct ucred *cred,
4188 NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, void *stuff)
4191 struct nfsrv_descript nfsd, *nd = &nfsd;
4195 NFSCL_REQSTART(nd, NFSPROC_FSINFO, vp);
4196 error = nfscl_request(nd, vp, p, cred, stuff);
4199 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
4200 if (nd->nd_repstat && !error)
4201 error = nd->nd_repstat;
4203 NFSM_DISSECT(tl, u_int32_t *, NFSX_V3FSINFO);
4204 fsp->fs_rtmax = fxdr_unsigned(u_int32_t, *tl++);
4205 fsp->fs_rtpref = fxdr_unsigned(u_int32_t, *tl++);
4206 fsp->fs_rtmult = fxdr_unsigned(u_int32_t, *tl++);
4207 fsp->fs_wtmax = fxdr_unsigned(u_int32_t, *tl++);
4208 fsp->fs_wtpref = fxdr_unsigned(u_int32_t, *tl++);
4209 fsp->fs_wtmult = fxdr_unsigned(u_int32_t, *tl++);
4210 fsp->fs_dtpref = fxdr_unsigned(u_int32_t, *tl++);
4211 fsp->fs_maxfilesize = fxdr_hyper(tl);
4213 fxdr_nfsv3time(tl, &fsp->fs_timedelta);
4215 fsp->fs_properties = fxdr_unsigned(u_int32_t, *tl);
4218 mbuf_freem(nd->nd_mrep);
4223 * This function performs the Renew RPC.
4226 nfsrpc_renew(struct nfsclclient *clp, struct nfsclds *dsp, struct ucred *cred,
4230 struct nfsrv_descript nfsd;
4231 struct nfsrv_descript *nd = &nfsd;
4232 struct nfsmount *nmp;
4234 struct nfssockreq *nrp;
4236 nmp = clp->nfsc_nmp;
4239 nfscl_reqstart(nd, NFSPROC_RENEW, nmp, NULL, 0, NULL,
4240 &dsp->nfsclds_sess);
4241 if (!NFSHASNFSV4N(nmp)) {
4242 /* NFSv4.1 just uses a Sequence Op and not a Renew. */
4243 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
4244 *tl++ = NFSMNT_MDSSESSION(nmp)->nfsess_clientid.lval[0];
4245 *tl = NFSMNT_MDSSESSION(nmp)->nfsess_clientid.lval[1];
4247 nrp = dsp->nfsclds_sockp;
4249 /* If NULL, use the MDS socket. */
4250 nrp = &nmp->nm_sockreq;
4251 nd->nd_flag |= ND_USEGSSNAME;
4252 error = newnfs_request(nd, nmp, NULL, nrp, NULL, p, cred,
4253 NFS_PROG, NFS_VER4, NULL, 1, NULL, &dsp->nfsclds_sess);
4256 error = nd->nd_repstat;
4257 mbuf_freem(nd->nd_mrep);
4262 * This function performs the Releaselockowner RPC.
4265 nfsrpc_rellockown(struct nfsmount *nmp, struct nfscllockowner *lp,
4266 uint8_t *fh, int fhlen, struct ucred *cred, NFSPROC_T *p)
4268 struct nfsrv_descript nfsd, *nd = &nfsd;
4271 uint8_t own[NFSV4CL_LOCKNAMELEN + NFSX_V4FHMAX];
4273 if (NFSHASNFSV4N(nmp)) {
4274 /* For NFSv4.1, do a FreeStateID. */
4275 nfscl_reqstart(nd, NFSPROC_FREESTATEID, nmp, NULL, 0, NULL,
4277 nfsm_stateidtom(nd, &lp->nfsl_stateid, NFSSTATEID_PUTSTATEID);
4279 nfscl_reqstart(nd, NFSPROC_RELEASELCKOWN, nmp, NULL, 0, NULL,
4281 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
4282 *tl++ = NFSMNT_MDSSESSION(nmp)->nfsess_clientid.lval[0];
4283 *tl = NFSMNT_MDSSESSION(nmp)->nfsess_clientid.lval[1];
4284 NFSBCOPY(lp->nfsl_owner, own, NFSV4CL_LOCKNAMELEN);
4285 NFSBCOPY(fh, &own[NFSV4CL_LOCKNAMELEN], fhlen);
4286 (void)nfsm_strtom(nd, own, NFSV4CL_LOCKNAMELEN + fhlen);
4288 nd->nd_flag |= ND_USEGSSNAME;
4289 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4290 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4293 error = nd->nd_repstat;
4294 mbuf_freem(nd->nd_mrep);
4299 * This function performs the Compound to get the mount pt FH.
4302 nfsrpc_getdirpath(struct nfsmount *nmp, u_char *dirpath, struct ucred *cred,
4306 struct nfsrv_descript nfsd;
4307 struct nfsrv_descript *nd = &nfsd;
4309 int error, cnt, len, setnil;
4312 nfscl_reqstart(nd, NFSPROC_PUTROOTFH, nmp, NULL, 0, &opcntp, NULL);
4320 while (*cp2 != '\0' && *cp2 != '/')
4327 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
4328 *tl = txdr_unsigned(NFSV4OP_LOOKUP);
4329 nfsm_strtom(nd, cp, strlen(cp));
4335 } while (*cp != '\0');
4336 if (NFSHASNFSV4N(nmp))
4337 /* Has a Sequence Op done by nfscl_reqstart(). */
4338 *opcntp = txdr_unsigned(3 + cnt);
4340 *opcntp = txdr_unsigned(2 + cnt);
4341 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
4342 *tl = txdr_unsigned(NFSV4OP_GETFH);
4343 nd->nd_flag |= ND_USEGSSNAME;
4344 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4345 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4348 if (nd->nd_repstat == 0) {
4349 NFSM_DISSECT(tl, u_int32_t *, (3 + 2 * cnt) * NFSX_UNSIGNED);
4350 tl += (2 + 2 * cnt);
4351 if ((len = fxdr_unsigned(int, *tl)) <= 0 ||
4353 nd->nd_repstat = NFSERR_BADXDR;
4355 nd->nd_repstat = nfsrv_mtostr(nd, nmp->nm_fh, len);
4356 if (nd->nd_repstat == 0)
4357 nmp->nm_fhsize = len;
4360 error = nd->nd_repstat;
4362 mbuf_freem(nd->nd_mrep);
4367 * This function performs the Delegreturn RPC.
4370 nfsrpc_delegreturn(struct nfscldeleg *dp, struct ucred *cred,
4371 struct nfsmount *nmp, NFSPROC_T *p, int syscred)
4374 struct nfsrv_descript nfsd;
4375 struct nfsrv_descript *nd = &nfsd;
4378 nfscl_reqstart(nd, NFSPROC_DELEGRETURN, nmp, dp->nfsdl_fh,
4379 dp->nfsdl_fhlen, NULL, NULL);
4380 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID);
4381 if (NFSHASNFSV4N(nmp))
4384 *tl++ = dp->nfsdl_stateid.seqid;
4385 *tl++ = dp->nfsdl_stateid.other[0];
4386 *tl++ = dp->nfsdl_stateid.other[1];
4387 *tl = dp->nfsdl_stateid.other[2];
4389 nd->nd_flag |= ND_USEGSSNAME;
4390 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4391 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4394 error = nd->nd_repstat;
4395 mbuf_freem(nd->nd_mrep);
4403 nfsrpc_getacl(vnode_t vp, struct ucred *cred, NFSPROC_T *p,
4404 struct acl *aclp, void *stuff)
4406 struct nfsrv_descript nfsd, *nd = &nfsd;
4408 nfsattrbit_t attrbits;
4409 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
4411 if (nfsrv_useacl == 0 || !NFSHASNFSV4(nmp))
4412 return (EOPNOTSUPP);
4413 NFSCL_REQSTART(nd, NFSPROC_GETACL, vp);
4414 NFSZERO_ATTRBIT(&attrbits);
4415 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_ACL);
4416 (void) nfsrv_putattrbit(nd, &attrbits);
4417 error = nfscl_request(nd, vp, p, cred, stuff);
4420 if (!nd->nd_repstat)
4421 error = nfsv4_loadattr(nd, vp, NULL, NULL, NULL, 0, NULL,
4422 NULL, NULL, NULL, aclp, 0, NULL, NULL, NULL, p, cred);
4424 error = nd->nd_repstat;
4425 mbuf_freem(nd->nd_mrep);
4433 nfsrpc_setacl(vnode_t vp, struct ucred *cred, NFSPROC_T *p,
4434 struct acl *aclp, void *stuff)
4437 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
4439 if (nfsrv_useacl == 0 || !NFSHASNFSV4(nmp))
4440 return (EOPNOTSUPP);
4441 error = nfsrpc_setattr(vp, NULL, aclp, cred, p, NULL, NULL, stuff);
4449 nfsrpc_setaclrpc(vnode_t vp, struct ucred *cred, NFSPROC_T *p,
4450 struct acl *aclp, nfsv4stateid_t *stateidp, void *stuff)
4452 struct nfsrv_descript nfsd, *nd = &nfsd;
4454 nfsattrbit_t attrbits;
4455 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
4457 if (!NFSHASNFSV4(nmp))
4458 return (EOPNOTSUPP);
4459 NFSCL_REQSTART(nd, NFSPROC_SETACL, vp);
4460 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID);
4461 NFSZERO_ATTRBIT(&attrbits);
4462 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_ACL);
4463 (void) nfsv4_fillattr(nd, vnode_mount(vp), vp, aclp, NULL, NULL, 0,
4464 &attrbits, NULL, NULL, 0, 0, 0, 0, (uint64_t)0);
4465 error = nfscl_request(nd, vp, p, cred, stuff);
4468 /* Don't care about the pre/postop attributes */
4469 mbuf_freem(nd->nd_mrep);
4470 return (nd->nd_repstat);
4474 * Do the NFSv4.1 Exchange ID.
4477 nfsrpc_exchangeid(struct nfsmount *nmp, struct nfsclclient *clp,
4478 struct nfssockreq *nrp, uint32_t exchflags, struct nfsclds **dspp,
4479 struct ucred *cred, NFSPROC_T *p)
4481 uint32_t *tl, v41flags;
4482 struct nfsrv_descript nfsd;
4483 struct nfsrv_descript *nd = &nfsd;
4484 struct nfsclds *dsp;
4485 struct timespec verstime;
4489 nfscl_reqstart(nd, NFSPROC_EXCHANGEID, nmp, NULL, 0, NULL, NULL);
4490 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_UNSIGNED);
4491 *tl++ = txdr_unsigned(nfsboottime.tv_sec); /* Client owner */
4492 *tl = txdr_unsigned(clp->nfsc_rev);
4493 (void) nfsm_strtom(nd, clp->nfsc_id, clp->nfsc_idlen);
4495 NFSM_BUILD(tl, uint32_t *, 3 * NFSX_UNSIGNED);
4496 *tl++ = txdr_unsigned(exchflags);
4497 *tl++ = txdr_unsigned(NFSV4EXCH_SP4NONE);
4499 /* Set the implementation id4 */
4500 *tl = txdr_unsigned(1);
4501 (void) nfsm_strtom(nd, "freebsd.org", strlen("freebsd.org"));
4502 (void) nfsm_strtom(nd, version, strlen(version));
4503 NFSM_BUILD(tl, uint32_t *, NFSX_V4TIME);
4504 verstime.tv_sec = 1293840000; /* Jan 1, 2011 */
4505 verstime.tv_nsec = 0;
4506 txdr_nfsv4time(&verstime, tl);
4507 nd->nd_flag |= ND_USEGSSNAME;
4508 error = newnfs_request(nd, nmp, NULL, nrp, NULL, p, cred,
4509 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4510 NFSCL_DEBUG(1, "exchangeid err=%d reps=%d\n", error,
4511 (int)nd->nd_repstat);
4514 if (nd->nd_repstat == 0) {
4515 NFSM_DISSECT(tl, uint32_t *, 6 * NFSX_UNSIGNED + NFSX_HYPER);
4516 len = fxdr_unsigned(int, *(tl + 7));
4517 if (len < 0 || len > NFSV4_OPAQUELIMIT) {
4518 error = NFSERR_BADXDR;
4521 dsp = malloc(sizeof(struct nfsclds) + len, M_NFSCLDS,
4523 dsp->nfsclds_expire = NFSD_MONOSEC + clp->nfsc_renew;
4524 dsp->nfsclds_servownlen = len;
4525 dsp->nfsclds_sess.nfsess_clientid.lval[0] = *tl++;
4526 dsp->nfsclds_sess.nfsess_clientid.lval[1] = *tl++;
4527 dsp->nfsclds_sess.nfsess_sequenceid =
4528 fxdr_unsigned(uint32_t, *tl++);
4529 v41flags = fxdr_unsigned(uint32_t, *tl);
4530 if ((v41flags & NFSV4EXCH_USEPNFSMDS) != 0 &&
4531 NFSHASPNFSOPT(nmp)) {
4532 NFSCL_DEBUG(1, "set PNFS\n");
4534 nmp->nm_state |= NFSSTA_PNFS;
4536 dsp->nfsclds_flags |= NFSCLDS_MDS;
4538 if ((v41flags & NFSV4EXCH_USEPNFSDS) != 0)
4539 dsp->nfsclds_flags |= NFSCLDS_DS;
4541 nd->nd_repstat = nfsrv_mtostr(nd,
4542 dsp->nfsclds_serverown, len);
4543 if (nd->nd_repstat == 0) {
4544 mtx_init(&dsp->nfsclds_mtx, "nfsds", NULL, MTX_DEF);
4545 mtx_init(&dsp->nfsclds_sess.nfsess_mtx, "nfssession",
4547 nfscl_initsessionslots(&dsp->nfsclds_sess);
4550 free(dsp, M_NFSCLDS);
4552 error = nd->nd_repstat;
4554 mbuf_freem(nd->nd_mrep);
4559 * Do the NFSv4.1 Create Session.
4562 nfsrpc_createsession(struct nfsmount *nmp, struct nfsclsession *sep,
4563 struct nfssockreq *nrp, uint32_t sequenceid, int mds, struct ucred *cred,
4566 uint32_t crflags, *tl;
4567 struct nfsrv_descript nfsd;
4568 struct nfsrv_descript *nd = &nfsd;
4571 nfscl_reqstart(nd, NFSPROC_CREATESESSION, nmp, NULL, 0, NULL, NULL);
4572 NFSM_BUILD(tl, uint32_t *, 4 * NFSX_UNSIGNED);
4573 *tl++ = sep->nfsess_clientid.lval[0];
4574 *tl++ = sep->nfsess_clientid.lval[1];
4575 *tl++ = txdr_unsigned(sequenceid);
4576 crflags = (NFSMNT_RDONLY(nmp->nm_mountp) ? 0 : NFSV4CRSESS_PERSIST);
4577 if (nfscl_enablecallb != 0 && nfs_numnfscbd > 0)
4578 crflags |= NFSV4CRSESS_CONNBACKCHAN;
4579 *tl = txdr_unsigned(crflags);
4581 /* Fill in fore channel attributes. */
4582 NFSM_BUILD(tl, uint32_t *, 7 * NFSX_UNSIGNED);
4583 *tl++ = 0; /* Header pad size */
4584 *tl++ = txdr_unsigned(100000); /* Max request size */
4585 *tl++ = txdr_unsigned(100000); /* Max response size */
4586 *tl++ = txdr_unsigned(4096); /* Max response size cached */
4587 *tl++ = txdr_unsigned(20); /* Max operations */
4588 *tl++ = txdr_unsigned(64); /* Max slots */
4589 *tl = 0; /* No rdma ird */
4591 /* Fill in back channel attributes. */
4592 NFSM_BUILD(tl, uint32_t *, 7 * NFSX_UNSIGNED);
4593 *tl++ = 0; /* Header pad size */
4594 *tl++ = txdr_unsigned(10000); /* Max request size */
4595 *tl++ = txdr_unsigned(10000); /* Max response size */
4596 *tl++ = txdr_unsigned(4096); /* Max response size cached */
4597 *tl++ = txdr_unsigned(4); /* Max operations */
4598 *tl++ = txdr_unsigned(NFSV4_CBSLOTS); /* Max slots */
4599 *tl = 0; /* No rdma ird */
4601 NFSM_BUILD(tl, uint32_t *, 8 * NFSX_UNSIGNED);
4602 *tl++ = txdr_unsigned(NFS_CALLBCKPROG); /* Call back prog # */
4604 /* Allow AUTH_SYS callbacks as uid, gid == 0. */
4605 *tl++ = txdr_unsigned(1); /* Auth_sys only */
4606 *tl++ = txdr_unsigned(AUTH_SYS); /* AUTH_SYS type */
4607 *tl++ = txdr_unsigned(nfsboottime.tv_sec); /* time stamp */
4608 *tl++ = 0; /* Null machine name */
4609 *tl++ = 0; /* Uid == 0 */
4610 *tl++ = 0; /* Gid == 0 */
4611 *tl = 0; /* No additional gids */
4612 nd->nd_flag |= ND_USEGSSNAME;
4613 error = newnfs_request(nd, nmp, NULL, nrp, NULL, p, cred, NFS_PROG,
4614 NFS_VER4, NULL, 1, NULL, NULL);
4617 if (nd->nd_repstat == 0) {
4618 NFSM_DISSECT(tl, uint32_t *, NFSX_V4SESSIONID +
4620 bcopy(tl, sep->nfsess_sessionid, NFSX_V4SESSIONID);
4621 tl += NFSX_V4SESSIONID / NFSX_UNSIGNED;
4622 sep->nfsess_sequenceid = fxdr_unsigned(uint32_t, *tl++);
4623 crflags = fxdr_unsigned(uint32_t, *tl);
4624 if ((crflags & NFSV4CRSESS_PERSIST) != 0 && mds != 0) {
4626 nmp->nm_state |= NFSSTA_SESSPERSIST;
4630 /* Get the fore channel slot count. */
4631 NFSM_DISSECT(tl, uint32_t *, 7 * NFSX_UNSIGNED);
4632 tl += 3; /* Skip the other counts. */
4633 sep->nfsess_maxcache = fxdr_unsigned(int, *tl++);
4635 sep->nfsess_foreslots = fxdr_unsigned(uint16_t, *tl++);
4636 NFSCL_DEBUG(4, "fore slots=%d\n", (int)sep->nfsess_foreslots);
4637 irdcnt = fxdr_unsigned(int, *tl);
4639 NFSM_DISSECT(tl, uint32_t *, irdcnt * NFSX_UNSIGNED);
4641 /* and the back channel slot count. */
4642 NFSM_DISSECT(tl, uint32_t *, 7 * NFSX_UNSIGNED);
4644 sep->nfsess_backslots = fxdr_unsigned(uint16_t, *tl);
4645 NFSCL_DEBUG(4, "back slots=%d\n", (int)sep->nfsess_backslots);
4647 error = nd->nd_repstat;
4649 mbuf_freem(nd->nd_mrep);
4654 * Do the NFSv4.1 Destroy Session.
4657 nfsrpc_destroysession(struct nfsmount *nmp, struct nfsclclient *clp,
4658 struct ucred *cred, NFSPROC_T *p)
4661 struct nfsrv_descript nfsd;
4662 struct nfsrv_descript *nd = &nfsd;
4665 nfscl_reqstart(nd, NFSPROC_DESTROYSESSION, nmp, NULL, 0, NULL, NULL);
4666 NFSM_BUILD(tl, uint32_t *, NFSX_V4SESSIONID);
4667 bcopy(NFSMNT_MDSSESSION(nmp)->nfsess_sessionid, tl, NFSX_V4SESSIONID);
4668 nd->nd_flag |= ND_USEGSSNAME;
4669 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4670 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4673 error = nd->nd_repstat;
4674 mbuf_freem(nd->nd_mrep);
4679 * Do the NFSv4.1 Destroy Client.
4682 nfsrpc_destroyclient(struct nfsmount *nmp, struct nfsclclient *clp,
4683 struct ucred *cred, NFSPROC_T *p)
4686 struct nfsrv_descript nfsd;
4687 struct nfsrv_descript *nd = &nfsd;
4690 nfscl_reqstart(nd, NFSPROC_DESTROYCLIENT, nmp, NULL, 0, NULL, NULL);
4691 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_UNSIGNED);
4692 *tl++ = NFSMNT_MDSSESSION(nmp)->nfsess_clientid.lval[0];
4693 *tl = NFSMNT_MDSSESSION(nmp)->nfsess_clientid.lval[1];
4694 nd->nd_flag |= ND_USEGSSNAME;
4695 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4696 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4699 error = nd->nd_repstat;
4700 mbuf_freem(nd->nd_mrep);
4705 * Do the NFSv4.1 LayoutGet.
4708 nfsrpc_layoutget(struct nfsmount *nmp, uint8_t *fhp, int fhlen, int iomode,
4709 uint64_t offset, uint64_t len, uint64_t minlen, int layoutlen,
4710 nfsv4stateid_t *stateidp, int *retonclosep, struct nfsclflayouthead *flhp,
4711 struct ucred *cred, NFSPROC_T *p, void *stuff)
4714 struct nfsrv_descript nfsd, *nd = &nfsd;
4716 struct nfsclflayout *flp, *prevflp, *tflp;
4717 int cnt, error, gotiomode, fhcnt, nfhlen, i, j;
4723 nfscl_reqstart(nd, NFSPROC_LAYOUTGET, nmp, fhp, fhlen, NULL, NULL);
4724 NFSM_BUILD(tl, uint32_t *, 4 * NFSX_UNSIGNED + 3 * NFSX_HYPER +
4726 *tl++ = newnfs_false; /* Don't signal availability. */
4727 *tl++ = txdr_unsigned(NFSLAYOUT_NFSV4_1_FILES);
4728 *tl++ = txdr_unsigned(iomode);
4729 txdr_hyper(offset, tl);
4731 txdr_hyper(len, tl);
4733 txdr_hyper(minlen, tl);
4735 *tl++ = txdr_unsigned(stateidp->seqid);
4736 NFSCL_DEBUG(4, "layget seq=%d\n", (int)stateidp->seqid);
4737 *tl++ = stateidp->other[0];
4738 *tl++ = stateidp->other[1];
4739 *tl++ = stateidp->other[2];
4740 *tl = txdr_unsigned(layoutlen);
4741 nd->nd_flag |= ND_USEGSSNAME;
4742 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4743 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4746 if (nd->nd_repstat == 0) {
4747 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED + NFSX_STATEID);
4752 stateidp->seqid = fxdr_unsigned(uint32_t, *tl++);
4753 NFSCL_DEBUG(4, "retoncls=%d stseq=%d\n", *retonclosep,
4754 (int)stateidp->seqid);
4755 stateidp->other[0] = *tl++;
4756 stateidp->other[1] = *tl++;
4757 stateidp->other[2] = *tl++;
4758 cnt = fxdr_unsigned(int, *tl);
4759 NFSCL_DEBUG(4, "layg cnt=%d\n", cnt);
4760 if (cnt <= 0 || cnt > 10000) {
4761 /* Don't accept more than 10000 layouts in reply. */
4762 error = NFSERR_BADXDR;
4765 for (i = 0; i < cnt; i++) {
4766 /* Dissect all the way to the file handle cnt. */
4767 NFSM_DISSECT(tl, uint32_t *, 3 * NFSX_HYPER +
4768 6 * NFSX_UNSIGNED + NFSX_V4DEVICEID);
4769 fhcnt = fxdr_unsigned(int, *(tl + 11 +
4770 NFSX_V4DEVICEID / NFSX_UNSIGNED));
4771 NFSCL_DEBUG(4, "fhcnt=%d\n", fhcnt);
4772 if (fhcnt < 0 || fhcnt > 100) {
4773 /* Don't accept more than 100 file handles. */
4774 error = NFSERR_BADXDR;
4778 flp = malloc(sizeof(*flp) + (fhcnt - 1) *
4779 sizeof(struct nfsfh *),
4780 M_NFSFLAYOUT, M_WAITOK);
4782 flp = malloc(sizeof(*flp),
4783 M_NFSFLAYOUT, M_WAITOK);
4784 flp->nfsfl_flags = 0;
4785 flp->nfsfl_fhcnt = 0;
4786 flp->nfsfl_devp = NULL;
4787 flp->nfsfl_off = fxdr_hyper(tl); tl += 2;
4788 retlen = fxdr_hyper(tl); tl += 2;
4789 if (flp->nfsfl_off + retlen < flp->nfsfl_off)
4790 flp->nfsfl_end = UINT64_MAX - flp->nfsfl_off;
4792 flp->nfsfl_end = flp->nfsfl_off + retlen;
4793 flp->nfsfl_iomode = fxdr_unsigned(int, *tl++);
4794 if (gotiomode == -1)
4795 gotiomode = flp->nfsfl_iomode;
4796 NFSCL_DEBUG(4, "layg reqiom=%d retiom=%d\n", iomode,
4797 (int)flp->nfsfl_iomode);
4798 if (fxdr_unsigned(int, *tl++) !=
4799 NFSLAYOUT_NFSV4_1_FILES) {
4800 printf("NFSv4.1: got non-files layout\n");
4801 error = NFSERR_BADXDR;
4804 NFSBCOPY(++tl, flp->nfsfl_dev, NFSX_V4DEVICEID);
4805 tl += (NFSX_V4DEVICEID / NFSX_UNSIGNED);
4806 flp->nfsfl_util = fxdr_unsigned(uint32_t, *tl++);
4807 NFSCL_DEBUG(4, "flutil=0x%x\n", flp->nfsfl_util);
4808 flp->nfsfl_stripe1 = fxdr_unsigned(uint32_t, *tl++);
4809 flp->nfsfl_patoff = fxdr_hyper(tl); tl += 2;
4810 if (fxdr_unsigned(int, *tl) != fhcnt) {
4811 printf("EEK! bad fhcnt\n");
4812 error = NFSERR_BADXDR;
4815 for (j = 0; j < fhcnt; j++) {
4816 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
4817 nfhlen = fxdr_unsigned(int, *tl);
4818 if (nfhlen <= 0 || nfhlen > NFSX_V4FHMAX) {
4819 error = NFSERR_BADXDR;
4822 nfhp = malloc(sizeof(*nfhp) + nfhlen - 1,
4824 flp->nfsfl_fh[j] = nfhp;
4826 nfhp->nfh_len = nfhlen;
4827 NFSM_DISSECT(cp, uint8_t *, NFSM_RNDUP(nfhlen));
4828 NFSBCOPY(cp, nfhp->nfh_fh, nfhlen);
4830 if (flp->nfsfl_iomode == gotiomode) {
4831 /* Keep the list in increasing offset order. */
4832 tflp = LIST_FIRST(flhp);
4834 while (tflp != NULL &&
4835 tflp->nfsfl_off < flp->nfsfl_off) {
4837 tflp = LIST_NEXT(tflp, nfsfl_list);
4839 if (prevflp == NULL)
4840 LIST_INSERT_HEAD(flhp, flp, nfsfl_list);
4842 LIST_INSERT_AFTER(prevflp, flp,
4845 printf("nfscl_layoutget(): got wrong iomode\n");
4846 nfscl_freeflayout(flp);
4851 if (nd->nd_repstat != 0 && error == 0)
4852 error = nd->nd_repstat;
4854 if (error != 0 && flp != NULL)
4855 nfscl_freeflayout(flp);
4856 mbuf_freem(nd->nd_mrep);
4861 * Do the NFSv4.1 Get Device Info.
4864 nfsrpc_getdeviceinfo(struct nfsmount *nmp, uint8_t *deviceid, int layouttype,
4865 uint32_t *notifybitsp, struct nfscldevinfo **ndip, struct ucred *cred,
4869 struct nfsrv_descript nfsd;
4870 struct nfsrv_descript *nd = &nfsd;
4871 struct sockaddr_storage ss;
4872 struct nfsclds *dsp = NULL, **dspp;
4873 struct nfscldevinfo *ndi;
4874 int addrcnt, bitcnt, error, i, isudp, j, pos, safilled, stripecnt;
4875 uint8_t stripeindex;
4879 nfscl_reqstart(nd, NFSPROC_GETDEVICEINFO, nmp, NULL, 0, NULL, NULL);
4880 NFSM_BUILD(tl, uint32_t *, NFSX_V4DEVICEID + 3 * NFSX_UNSIGNED);
4881 NFSBCOPY(deviceid, tl, NFSX_V4DEVICEID);
4882 tl += (NFSX_V4DEVICEID / NFSX_UNSIGNED);
4883 *tl++ = txdr_unsigned(layouttype);
4884 *tl++ = txdr_unsigned(100000);
4885 if (notifybitsp != NULL && *notifybitsp != 0) {
4886 *tl = txdr_unsigned(1); /* One word of bits. */
4887 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
4888 *tl = txdr_unsigned(*notifybitsp);
4890 *tl = txdr_unsigned(0);
4891 nd->nd_flag |= ND_USEGSSNAME;
4892 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4893 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4896 if (nd->nd_repstat == 0) {
4897 NFSM_DISSECT(tl, uint32_t *, 3 * NFSX_UNSIGNED);
4898 if (layouttype != fxdr_unsigned(int, *tl++))
4899 printf("EEK! devinfo layout type not same!\n");
4900 stripecnt = fxdr_unsigned(int, *++tl);
4901 NFSCL_DEBUG(4, "stripecnt=%d\n", stripecnt);
4902 if (stripecnt < 1 || stripecnt > 4096) {
4903 printf("NFS devinfo stripecnt %d: out of range\n",
4905 error = NFSERR_BADXDR;
4908 NFSM_DISSECT(tl, uint32_t *, (stripecnt + 1) * NFSX_UNSIGNED);
4909 addrcnt = fxdr_unsigned(int, *(tl + stripecnt));
4910 NFSCL_DEBUG(4, "addrcnt=%d\n", addrcnt);
4911 if (addrcnt < 1 || addrcnt > 128) {
4912 printf("NFS devinfo addrcnt %d: out of range\n",
4914 error = NFSERR_BADXDR;
4919 * Now we know how many stripe indices and addresses, so
4920 * we can allocate the structure the correct size.
4922 i = (stripecnt * sizeof(uint8_t)) / sizeof(struct nfsclds *)
4924 NFSCL_DEBUG(4, "stripeindices=%d\n", i);
4925 ndi = malloc(sizeof(*ndi) + (addrcnt + i) *
4926 sizeof(struct nfsclds *), M_NFSDEVINFO, M_WAITOK | M_ZERO);
4927 NFSBCOPY(deviceid, ndi->nfsdi_deviceid, NFSX_V4DEVICEID);
4928 ndi->nfsdi_refcnt = 0;
4929 ndi->nfsdi_stripecnt = stripecnt;
4930 ndi->nfsdi_addrcnt = addrcnt;
4931 /* Fill in the stripe indices. */
4932 for (i = 0; i < stripecnt; i++) {
4933 stripeindex = fxdr_unsigned(uint8_t, *tl++);
4934 NFSCL_DEBUG(4, "stripeind=%d\n", stripeindex);
4935 if (stripeindex >= addrcnt) {
4936 printf("NFS devinfo stripeindex %d: too big\n",
4938 error = NFSERR_BADXDR;
4941 nfsfldi_setstripeindex(ndi, i, stripeindex);
4944 /* Now, dissect the server address(es). */
4946 for (i = 0; i < addrcnt; i++) {
4947 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
4948 cnt = fxdr_unsigned(uint32_t, *tl);
4950 printf("NFS devinfo 0 len addrlist\n");
4951 error = NFSERR_BADXDR;
4954 dspp = nfsfldi_addr(ndi, i);
4955 pos = arc4random() % cnt; /* Choose one. */
4957 for (j = 0; j < cnt; j++) {
4958 error = nfsv4_getipaddr(nd, &ss, &isudp);
4959 if (error != 0 && error != EPERM) {
4960 error = NFSERR_BADXDR;
4963 if (error == 0 && isudp == 0) {
4966 * - use "pos" entry if it is of the
4967 * same af_family or none of them
4968 * is of the same af_family
4970 * - use the first one of the same
4973 if ((safilled == 0 && ss.ss_family ==
4974 nmp->nm_nam->sa_family) ||
4976 (safilled == 0 || ss.ss_family ==
4977 nmp->nm_nam->sa_family)) ||
4978 (safilled == 1 && ss.ss_family ==
4979 nmp->nm_nam->sa_family)) {
4980 error = nfsrpc_fillsa(nmp, &ss,
4985 nmp->nm_nam->sa_family)
4997 /* And the notify bits. */
4998 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
4999 if (safilled != 0) {
5000 bitcnt = fxdr_unsigned(int, *tl);
5002 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
5003 if (notifybitsp != NULL)
5005 fxdr_unsigned(uint32_t, *tl);
5011 if (nd->nd_repstat != 0)
5012 error = nd->nd_repstat;
5014 if (error != 0 && ndi != NULL)
5015 nfscl_freedevinfo(ndi);
5016 mbuf_freem(nd->nd_mrep);
5021 * Do the NFSv4.1 LayoutCommit.
5024 nfsrpc_layoutcommit(struct nfsmount *nmp, uint8_t *fh, int fhlen, int reclaim,
5025 uint64_t off, uint64_t len, uint64_t lastbyte, nfsv4stateid_t *stateidp,
5026 int layouttype, int layoutupdatecnt, uint8_t *layp, struct ucred *cred,
5027 NFSPROC_T *p, void *stuff)
5030 struct nfsrv_descript nfsd, *nd = &nfsd;
5031 int error, outcnt, i;
5034 nfscl_reqstart(nd, NFSPROC_LAYOUTCOMMIT, nmp, fh, fhlen, NULL, NULL);
5035 NFSM_BUILD(tl, uint32_t *, 5 * NFSX_UNSIGNED + 3 * NFSX_HYPER +
5037 txdr_hyper(off, tl);
5039 txdr_hyper(len, tl);
5042 *tl++ = newnfs_true;
5044 *tl++ = newnfs_false;
5045 *tl++ = txdr_unsigned(stateidp->seqid);
5046 *tl++ = stateidp->other[0];
5047 *tl++ = stateidp->other[1];
5048 *tl++ = stateidp->other[2];
5049 *tl++ = newnfs_true;
5052 else if (lastbyte >= (off + len))
5053 lastbyte = off + len - 1;
5054 txdr_hyper(lastbyte, tl);
5056 *tl++ = newnfs_false;
5057 *tl++ = txdr_unsigned(layouttype);
5058 *tl = txdr_unsigned(layoutupdatecnt);
5059 if (layoutupdatecnt > 0) {
5060 KASSERT(layouttype != NFSLAYOUT_NFSV4_1_FILES,
5061 ("Must be nil for Files Layout"));
5062 outcnt = NFSM_RNDUP(layoutupdatecnt);
5063 NFSM_BUILD(cp, uint8_t *, outcnt);
5064 NFSBCOPY(layp, cp, layoutupdatecnt);
5065 cp += layoutupdatecnt;
5066 for (i = 0; i < (outcnt - layoutupdatecnt); i++)
5069 nd->nd_flag |= ND_USEGSSNAME;
5070 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
5071 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
5074 error = nd->nd_repstat;
5075 mbuf_freem(nd->nd_mrep);
5080 * Do the NFSv4.1 LayoutReturn.
5083 nfsrpc_layoutreturn(struct nfsmount *nmp, uint8_t *fh, int fhlen, int reclaim,
5084 int layouttype, uint32_t iomode, int layoutreturn, uint64_t offset,
5085 uint64_t len, nfsv4stateid_t *stateidp, int layoutcnt, uint32_t *layp,
5086 struct ucred *cred, NFSPROC_T *p, void *stuff)
5089 struct nfsrv_descript nfsd, *nd = &nfsd;
5090 int error, outcnt, i;
5093 nfscl_reqstart(nd, NFSPROC_LAYOUTRETURN, nmp, fh, fhlen, NULL, NULL);
5094 NFSM_BUILD(tl, uint32_t *, 4 * NFSX_UNSIGNED);
5096 *tl++ = newnfs_true;
5098 *tl++ = newnfs_false;
5099 *tl++ = txdr_unsigned(layouttype);
5100 *tl++ = txdr_unsigned(iomode);
5101 *tl = txdr_unsigned(layoutreturn);
5102 if (layoutreturn == NFSLAYOUTRETURN_FILE) {
5103 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_HYPER + NFSX_STATEID +
5105 txdr_hyper(offset, tl);
5107 txdr_hyper(len, tl);
5109 NFSCL_DEBUG(4, "layoutret stseq=%d\n", (int)stateidp->seqid);
5110 *tl++ = txdr_unsigned(stateidp->seqid);
5111 *tl++ = stateidp->other[0];
5112 *tl++ = stateidp->other[1];
5113 *tl++ = stateidp->other[2];
5114 *tl = txdr_unsigned(layoutcnt);
5115 if (layoutcnt > 0) {
5116 outcnt = NFSM_RNDUP(layoutcnt);
5117 NFSM_BUILD(cp, uint8_t *, outcnt);
5118 NFSBCOPY(layp, cp, layoutcnt);
5120 for (i = 0; i < (outcnt - layoutcnt); i++)
5124 nd->nd_flag |= ND_USEGSSNAME;
5125 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
5126 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
5129 if (nd->nd_repstat == 0) {
5130 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
5132 NFSM_DISSECT(tl, uint32_t *, NFSX_STATEID);
5133 stateidp->seqid = fxdr_unsigned(uint32_t, *tl++);
5134 stateidp->other[0] = *tl++;
5135 stateidp->other[1] = *tl++;
5136 stateidp->other[2] = *tl;
5139 error = nd->nd_repstat;
5141 mbuf_freem(nd->nd_mrep);
5146 * Acquire a layout and devinfo, if possible. The caller must have acquired
5147 * a reference count on the nfsclclient structure before calling this.
5148 * Return the layout in lypp with a reference count on it, if successful.
5151 nfsrpc_getlayout(struct nfsmount *nmp, vnode_t vp, struct nfsfh *nfhp,
5152 int iomode, uint32_t *notifybitsp, nfsv4stateid_t *stateidp, uint64_t off,
5153 struct nfscllayout **lypp, struct ucred *cred, NFSPROC_T *p)
5155 struct nfscllayout *lyp;
5156 struct nfsclflayout *flp, *tflp;
5157 struct nfscldevinfo *dip;
5158 struct nfsclflayouthead flh;
5159 int error = 0, islocked, layoutlen, recalled, retonclose;
5160 nfsv4stateid_t stateid;
5164 * If lyp is returned non-NULL, there will be a refcnt (shared lock)
5165 * on it, iff flp != NULL or a lock (exclusive lock) on it iff
5168 lyp = nfscl_getlayout(nmp->nm_clp, nfhp->nfh_fh, nfhp->nfh_len,
5169 off, &flp, &recalled);
5171 if (lyp == NULL || flp == NULL) {
5175 layoutlen = NFSMNT_MDSSESSION(nmp)->nfsess_maxcache -
5176 (NFSX_STATEID + 3 * NFSX_UNSIGNED);
5179 stateid.other[0] = stateidp->other[0];
5180 stateid.other[1] = stateidp->other[1];
5181 stateid.other[2] = stateidp->other[2];
5182 error = nfsrpc_layoutget(nmp, nfhp->nfh_fh,
5183 nfhp->nfh_len, iomode, (uint64_t)0, INT64_MAX,
5184 (uint64_t)0, layoutlen, &stateid, &retonclose,
5185 &flh, cred, p, NULL);
5188 stateid.seqid = lyp->nfsly_stateid.seqid;
5189 stateid.other[0] = lyp->nfsly_stateid.other[0];
5190 stateid.other[1] = lyp->nfsly_stateid.other[1];
5191 stateid.other[2] = lyp->nfsly_stateid.other[2];
5192 error = nfsrpc_layoutget(nmp, nfhp->nfh_fh,
5193 nfhp->nfh_len, iomode, off, INT64_MAX,
5194 (uint64_t)0, layoutlen, &stateid, &retonclose,
5195 &flh, cred, p, NULL);
5198 LIST_FOREACH(tflp, &flh, nfsfl_list) {
5199 error = nfscl_adddevinfo(nmp, NULL, tflp);
5201 error = nfsrpc_getdeviceinfo(nmp,
5203 NFSLAYOUT_NFSV4_1_FILES,
5204 notifybitsp, &dip, cred, p);
5207 error = nfscl_adddevinfo(nmp, dip,
5211 "getlayout: cannot add\n");
5216 * nfscl_layout() always returns with the nfsly_lock
5217 * set to a refcnt (shared lock).
5219 error = nfscl_layout(nmp, vp, nfhp->nfh_fh,
5220 nfhp->nfh_len, &stateid, retonclose, &flh, &lyp,
5224 } else if (islocked != 0)
5225 nfsv4_unlock(&lyp->nfsly_lock, 0);
5232 * Do a TCP connection plus exchange id and create session.
5233 * If successful, a "struct nfsclds" is linked into the list for the
5234 * mount point and a pointer to it is returned.
5237 nfsrpc_fillsa(struct nfsmount *nmp, struct sockaddr_storage *ssp,
5238 struct nfsclds **dspp, NFSPROC_T *p)
5240 struct sockaddr_in *msad, *sad, *ssd;
5241 struct sockaddr_in6 *msad6, *sad6, *ssd6;
5242 struct nfsclclient *clp;
5243 struct nfssockreq *nrp;
5244 struct nfsclds *dsp, *tdsp;
5246 enum nfsclds_state retv;
5247 uint32_t sequenceid;
5249 KASSERT(nmp->nm_sockreq.nr_cred != NULL,
5250 ("nfsrpc_fillsa: NULL nr_cred"));
5256 if (ssp->ss_family == AF_INET) {
5257 ssd = (struct sockaddr_in *)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 msad = (struct sockaddr_in *)nmp->nm_sockreq.nr_nam;
5267 tdsp = TAILQ_FIRST(&nmp->nm_sess);
5268 while (tdsp != NULL) {
5269 if (msad != NULL && msad->sin_family == AF_INET &&
5270 ssd->sin_addr.s_addr == msad->sin_addr.s_addr &&
5271 ssd->sin_port == msad->sin_port &&
5272 (tdsp->nfsclds_flags & NFSCLDS_DS) != 0) {
5275 NFSCL_DEBUG(4, "fnd same addr\n");
5278 tdsp = TAILQ_NEXT(tdsp, nfsclds_list);
5279 if (tdsp != NULL && tdsp->nfsclds_sockp != NULL)
5280 msad = (struct sockaddr_in *)
5281 tdsp->nfsclds_sockp->nr_nam;
5287 /* No IP address match, so look for new/trunked one. */
5288 sad = malloc(sizeof(*sad), M_SONAME, M_WAITOK | M_ZERO);
5289 sad->sin_len = sizeof(*sad);
5290 sad->sin_family = AF_INET;
5291 sad->sin_port = ssd->sin_port;
5292 sad->sin_addr.s_addr = ssd->sin_addr.s_addr;
5293 nrp = malloc(sizeof(*nrp), M_NFSSOCKREQ, M_WAITOK | M_ZERO);
5294 nrp->nr_nam = (struct sockaddr *)sad;
5295 } else if (ssp->ss_family == AF_INET6) {
5296 ssd6 = (struct sockaddr_in6 *)ssp;
5300 * Check to see if we already have a session for this
5301 * address that is usable for a DS.
5302 * Note that the MDS's address is in a different place
5303 * than the sessions already acquired for DS's.
5305 msad6 = (struct sockaddr_in6 *)nmp->nm_sockreq.nr_nam;
5306 tdsp = TAILQ_FIRST(&nmp->nm_sess);
5307 while (tdsp != NULL) {
5308 if (msad6 != NULL && msad6->sin6_family == AF_INET6 &&
5309 IN6_ARE_ADDR_EQUAL(&ssd6->sin6_addr,
5310 &msad6->sin6_addr) &&
5311 ssd6->sin6_port == msad6->sin6_port &&
5312 (tdsp->nfsclds_flags & NFSCLDS_DS) != 0) {
5317 tdsp = TAILQ_NEXT(tdsp, nfsclds_list);
5318 if (tdsp != NULL && tdsp->nfsclds_sockp != NULL)
5319 msad6 = (struct sockaddr_in6 *)
5320 tdsp->nfsclds_sockp->nr_nam;
5326 /* No IP address match, so look for new/trunked one. */
5327 sad6 = malloc(sizeof(*sad6), M_SONAME, M_WAITOK | M_ZERO);
5328 sad6->sin6_len = sizeof(*sad6);
5329 sad6->sin6_family = AF_INET6;
5330 sad6->sin6_port = ssd6->sin6_port;
5331 NFSBCOPY(&ssd6->sin6_addr, &sad6->sin6_addr,
5332 sizeof(struct in6_addr));
5333 nrp = malloc(sizeof(*nrp), M_NFSSOCKREQ, M_WAITOK | M_ZERO);
5334 nrp->nr_nam = (struct sockaddr *)sad6;
5338 nrp->nr_sotype = SOCK_STREAM;
5339 mtx_init(&nrp->nr_mtx, "nfssock", NULL, MTX_DEF);
5340 nrp->nr_prog = NFS_PROG;
5341 nrp->nr_vers = NFS_VER4;
5344 * Use the credentials that were used for the mount, which are
5345 * in nmp->nm_sockreq.nr_cred for newnfs_connect() etc.
5346 * Ref. counting the credentials with crhold() is probably not
5347 * necessary, since nm_sockreq.nr_cred won't be crfree()'d until
5348 * unmount, but I did it anyhow.
5350 nrp->nr_cred = crhold(nmp->nm_sockreq.nr_cred);
5351 error = newnfs_connect(nmp, nrp, NULL, p, 0);
5352 NFSCL_DEBUG(3, "DS connect=%d\n", error);
5354 /* Now, do the exchangeid and create session. */
5356 error = nfsrpc_exchangeid(nmp, clp, nrp, NFSV4EXCH_USEPNFSDS,
5357 &dsp, nrp->nr_cred, p);
5358 NFSCL_DEBUG(3, "DS exchangeid=%d\n", error);
5360 dsp->nfsclds_sockp = nrp;
5362 retv = nfscl_getsameserver(nmp, dsp, &tdsp);
5363 NFSCL_DEBUG(3, "getsame ret=%d\n", retv);
5364 if (retv == NFSDSP_USETHISSESSION) {
5367 * If there is already a session for this server,
5370 (void)newnfs_disconnect(nrp);
5371 nfscl_freenfsclds(dsp);
5375 if (retv == NFSDSP_SEQTHISSESSION)
5376 sequenceid = tdsp->nfsclds_sess.nfsess_sequenceid;
5378 sequenceid = dsp->nfsclds_sess.nfsess_sequenceid;
5380 error = nfsrpc_createsession(nmp, &dsp->nfsclds_sess,
5381 nrp, sequenceid, 0, nrp->nr_cred, p);
5382 NFSCL_DEBUG(3, "DS createsess=%d\n", error);
5384 NFSFREECRED(nrp->nr_cred);
5385 NFSFREEMUTEX(&nrp->nr_mtx);
5386 free(nrp->nr_nam, M_SONAME);
5387 free(nrp, M_NFSSOCKREQ);
5390 NFSCL_DEBUG(3, "add DS session\n");
5392 * Put it at the end of the list. That way the list
5393 * is ordered by when the entry was added. This matters
5394 * since the one done first is the one that should be
5395 * used for sequencid'ing any subsequent create sessions.
5398 TAILQ_INSERT_TAIL(&nmp->nm_sess, dsp, nfsclds_list);
5401 } else if (dsp != NULL)
5402 nfscl_freenfsclds(dsp);
5407 * Do the NFSv4.1 Reclaim Complete.
5410 nfsrpc_reclaimcomplete(struct nfsmount *nmp, struct ucred *cred, NFSPROC_T *p)
5413 struct nfsrv_descript nfsd;
5414 struct nfsrv_descript *nd = &nfsd;
5417 nfscl_reqstart(nd, NFSPROC_RECLAIMCOMPL, nmp, NULL, 0, NULL, NULL);
5418 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
5420 nd->nd_flag |= ND_USEGSSNAME;
5421 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
5422 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
5425 error = nd->nd_repstat;
5426 mbuf_freem(nd->nd_mrep);
5431 * Initialize the slot tables for a session.
5434 nfscl_initsessionslots(struct nfsclsession *sep)
5438 for (i = 0; i < NFSV4_CBSLOTS; i++) {
5439 if (sep->nfsess_cbslots[i].nfssl_reply != NULL)
5440 m_freem(sep->nfsess_cbslots[i].nfssl_reply);
5441 NFSBZERO(&sep->nfsess_cbslots[i], sizeof(struct nfsslot));
5443 for (i = 0; i < 64; i++)
5444 sep->nfsess_slotseq[i] = 0;
5445 sep->nfsess_slots = 0;
5449 * Called to try and do an I/O operation via an NFSv4.1 Data Server (DS).
5452 nfscl_doiods(vnode_t vp, struct uio *uiop, int *iomode, int *must_commit,
5453 uint32_t rwaccess, struct ucred *cred, NFSPROC_T *p)
5455 struct nfsnode *np = VTONFS(vp);
5456 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
5457 struct nfscllayout *layp;
5458 struct nfscldevinfo *dip;
5459 struct nfsclflayout *rflp;
5460 nfsv4stateid_t stateid;
5461 struct ucred *newcred;
5462 uint64_t lastbyte, len, off, oresid, xfer;
5463 int eof, error, iolaymode, recalled;
5466 if (!NFSHASPNFS(nmp) || nfscl_enablecallb == 0 || nfs_numnfscbd == 0 ||
5467 (np->n_flag & NNOLAYOUT) != 0)
5469 /* Now, get a reference cnt on the clientid for this mount. */
5470 if (nfscl_getref(nmp) == 0)
5473 /* Find an appropriate stateid. */
5474 newcred = NFSNEWCRED(cred);
5475 error = nfscl_getstateid(vp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len,
5476 rwaccess, 1, newcred, p, &stateid, &lckp);
5478 NFSFREECRED(newcred);
5482 /* Search for a layout for this file. */
5483 off = uiop->uio_offset;
5484 layp = nfscl_getlayout(nmp->nm_clp, np->n_fhp->nfh_fh,
5485 np->n_fhp->nfh_len, off, &rflp, &recalled);
5486 if (layp == NULL || rflp == NULL) {
5487 if (recalled != 0) {
5488 NFSFREECRED(newcred);
5493 nfscl_rellayout(layp, (rflp == NULL) ? 1 : 0);
5496 /* Try and get a Layout, if it is supported. */
5497 if (rwaccess == NFSV4OPEN_ACCESSWRITE ||
5498 (np->n_flag & NWRITEOPENED) != 0)
5499 iolaymode = NFSLAYOUTIOMODE_RW;
5501 iolaymode = NFSLAYOUTIOMODE_READ;
5502 error = nfsrpc_getlayout(nmp, vp, np->n_fhp, iolaymode,
5503 NULL, &stateid, off, &layp, newcred, p);
5506 np->n_flag |= NNOLAYOUT;
5509 nfscl_lockderef(lckp);
5510 NFSFREECRED(newcred);
5512 nfscl_rellayout(layp, 0);
5519 * Loop around finding a layout that works for the first part of
5520 * this I/O operation, and then call the function that actually
5524 len = (uint64_t)uiop->uio_resid;
5525 while (len > 0 && error == 0 && eof == 0) {
5526 off = uiop->uio_offset;
5527 error = nfscl_findlayoutforio(layp, off, rwaccess, &rflp);
5529 oresid = xfer = (uint64_t)uiop->uio_resid;
5530 if (xfer > (rflp->nfsfl_end - rflp->nfsfl_off))
5531 xfer = rflp->nfsfl_end - rflp->nfsfl_off;
5532 dip = nfscl_getdevinfo(nmp->nm_clp, rflp->nfsfl_dev,
5535 error = nfscl_doflayoutio(vp, uiop, iomode,
5536 must_commit, &eof, &stateid, rwaccess, dip,
5537 layp, rflp, off, xfer, newcred, p);
5538 nfscl_reldevinfo(dip);
5539 lastbyte = off + xfer - 1;
5542 if (lastbyte > layp->nfsly_lastbyte)
5543 layp->nfsly_lastbyte = lastbyte;
5549 len -= (oresid - (uint64_t)uiop->uio_resid);
5553 nfscl_lockderef(lckp);
5554 NFSFREECRED(newcred);
5555 nfscl_rellayout(layp, 0);
5561 * Find a file layout that will handle the first bytes of the requested
5562 * range and return the information from it needed to to the I/O operation.
5565 nfscl_findlayoutforio(struct nfscllayout *lyp, uint64_t off, uint32_t rwaccess,
5566 struct nfsclflayout **retflpp)
5568 struct nfsclflayout *flp, *nflp, *rflp;
5573 /* For reading, do the Read list first and then the Write list. */
5575 if (rw == NFSV4OPEN_ACCESSREAD)
5576 flp = LIST_FIRST(&lyp->nfsly_flayread);
5578 flp = LIST_FIRST(&lyp->nfsly_flayrw);
5579 while (flp != NULL) {
5580 nflp = LIST_NEXT(flp, nfsfl_list);
5581 if (flp->nfsfl_off > off)
5583 if (flp->nfsfl_end > off &&
5584 (rflp == NULL || rflp->nfsfl_end < flp->nfsfl_end))
5588 if (rw == NFSV4OPEN_ACCESSREAD)
5589 rw = NFSV4OPEN_ACCESSWRITE;
5594 /* This one covers the most bytes starting at off. */
5602 * Do I/O using an NFSv4.1 file layout.
5605 nfscl_doflayoutio(vnode_t vp, struct uio *uiop, int *iomode, int *must_commit,
5606 int *eofp, nfsv4stateid_t *stateidp, int rwflag, struct nfscldevinfo *dp,
5607 struct nfscllayout *lyp, struct nfsclflayout *flp, uint64_t off,
5608 uint64_t len, struct ucred *cred, NFSPROC_T *p)
5610 uint64_t io_off, rel_off, stripe_unit_size, transfer, xfer;
5611 int commit_thru_mds, error = 0, stripe_index, stripe_pos;
5614 struct nfsclds **dspp;
5617 rel_off = off - flp->nfsfl_patoff;
5618 stripe_unit_size = (flp->nfsfl_util >> 6) & 0x3ffffff;
5619 stripe_pos = (rel_off / stripe_unit_size + flp->nfsfl_stripe1) %
5620 dp->nfsdi_stripecnt;
5621 transfer = stripe_unit_size - (rel_off % stripe_unit_size);
5623 /* Loop around, doing I/O for each stripe unit. */
5624 while (len > 0 && error == 0) {
5625 stripe_index = nfsfldi_stripeindex(dp, stripe_pos);
5626 dspp = nfsfldi_addr(dp, stripe_index);
5631 if ((flp->nfsfl_util & NFSFLAYUTIL_DENSE) != 0) {
5633 if (stripe_pos >= flp->nfsfl_fhcnt)
5635 fhp = flp->nfsfl_fh[stripe_pos];
5636 io_off = (rel_off / (stripe_unit_size *
5637 dp->nfsdi_stripecnt)) * stripe_unit_size +
5638 rel_off % stripe_unit_size;
5640 /* Sparse layout. */
5641 if (flp->nfsfl_fhcnt > 1) {
5642 if (stripe_index >= flp->nfsfl_fhcnt)
5644 fhp = flp->nfsfl_fh[stripe_index];
5645 } else if (flp->nfsfl_fhcnt == 1)
5646 fhp = flp->nfsfl_fh[0];
5651 if ((flp->nfsfl_util & NFSFLAYUTIL_COMMIT_THRU_MDS) != 0)
5652 commit_thru_mds = 1;
5654 commit_thru_mds = 0;
5655 if (rwflag == FREAD)
5656 error = nfsrpc_readds(vp, uiop, stateidp, eofp, *dspp,
5657 io_off, xfer, fhp, cred, p);
5659 error = nfsrpc_writeds(vp, uiop, iomode, must_commit,
5660 stateidp, *dspp, io_off, xfer, fhp, commit_thru_mds,
5664 lyp->nfsly_flags |= NFSLY_WRITTEN;
5669 transfer = stripe_unit_size;
5670 stripe_pos = (stripe_pos + 1) % dp->nfsdi_stripecnt;
5679 * The actual read RPC done to a DS.
5682 nfsrpc_readds(vnode_t vp, struct uio *uiop, nfsv4stateid_t *stateidp, int *eofp,
5683 struct nfsclds *dsp, uint64_t io_off, int len, struct nfsfh *fhp,
5684 struct ucred *cred, NFSPROC_T *p)
5688 struct nfsrv_descript nfsd;
5689 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
5690 struct nfsrv_descript *nd = &nfsd;
5691 struct nfssockreq *nrp;
5694 nfscl_reqstart(nd, NFSPROC_READDS, nmp, fhp->nfh_fh, fhp->nfh_len,
5695 NULL, &dsp->nfsclds_sess);
5696 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSEQIDZERO);
5697 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED * 3);
5698 txdr_hyper(io_off, tl);
5699 *(tl + 2) = txdr_unsigned(len);
5700 nrp = dsp->nfsclds_sockp;
5702 /* If NULL, use the MDS socket. */
5703 nrp = &nmp->nm_sockreq;
5704 error = newnfs_request(nd, nmp, NULL, nrp, vp, p, cred,
5705 NFS_PROG, NFS_VER4, NULL, 1, NULL, &dsp->nfsclds_sess);
5708 if (nd->nd_repstat != 0) {
5709 error = nd->nd_repstat;
5712 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
5713 *eofp = fxdr_unsigned(int, *tl);
5714 NFSM_STRSIZ(retlen, len);
5715 error = nfsm_mbufuio(nd, uiop, retlen);
5717 if (nd->nd_mrep != NULL)
5718 mbuf_freem(nd->nd_mrep);
5723 * The actual write RPC done to a DS.
5726 nfsrpc_writeds(vnode_t vp, struct uio *uiop, int *iomode, int *must_commit,
5727 nfsv4stateid_t *stateidp, struct nfsclds *dsp, uint64_t io_off, int len,
5728 struct nfsfh *fhp, int commit_thru_mds, struct ucred *cred, NFSPROC_T *p)
5731 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
5732 int error, rlen, commit, committed = NFSWRITE_FILESYNC;
5734 struct nfsrv_descript nfsd;
5735 struct nfsrv_descript *nd = &nfsd;
5736 struct nfssockreq *nrp;
5738 KASSERT(uiop->uio_iovcnt == 1, ("nfs: writerpc iovcnt > 1"));
5740 nfscl_reqstart(nd, NFSPROC_WRITEDS, nmp, fhp->nfh_fh, fhp->nfh_len,
5741 NULL, &dsp->nfsclds_sess);
5742 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSEQIDZERO);
5743 NFSM_BUILD(tl, uint32_t *, NFSX_HYPER + 2 * NFSX_UNSIGNED);
5744 txdr_hyper(io_off, tl);
5746 *tl++ = txdr_unsigned(*iomode);
5747 *tl = txdr_unsigned(len);
5748 nfsm_uiombuf(nd, uiop, len);
5749 nrp = dsp->nfsclds_sockp;
5751 /* If NULL, use the MDS socket. */
5752 nrp = &nmp->nm_sockreq;
5753 error = newnfs_request(nd, nmp, NULL, nrp, vp, p, cred,
5754 NFS_PROG, NFS_VER4, NULL, 1, NULL, &dsp->nfsclds_sess);
5757 if (nd->nd_repstat != 0) {
5759 * In case the rpc gets retried, roll
5760 * the uio fileds changed by nfsm_uiombuf()
5763 uiop->uio_offset -= len;
5764 uio_uio_resid_add(uiop, len);
5765 uio_iov_base_add(uiop, -len);
5766 uio_iov_len_add(uiop, len);
5767 error = nd->nd_repstat;
5769 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED + NFSX_VERF);
5770 rlen = fxdr_unsigned(int, *tl++);
5774 } else if (rlen < len) {
5775 backup = len - rlen;
5776 uio_iov_base_add(uiop, -(backup));
5777 uio_iov_len_add(uiop, backup);
5778 uiop->uio_offset -= backup;
5779 uio_uio_resid_add(uiop, backup);
5782 commit = fxdr_unsigned(int, *tl++);
5785 * Return the lowest commitment level
5786 * obtained by any of the RPCs.
5788 if (committed == NFSWRITE_FILESYNC)
5790 else if (committed == NFSWRITE_DATASYNC &&
5791 commit == NFSWRITE_UNSTABLE)
5793 if (commit_thru_mds != 0) {
5795 if (!NFSHASWRITEVERF(nmp)) {
5796 NFSBCOPY(tl, nmp->nm_verf, NFSX_VERF);
5797 NFSSETWRITEVERF(nmp);
5798 } else if (NFSBCMP(tl, nmp->nm_verf, NFSX_VERF)) {
5800 NFSBCOPY(tl, nmp->nm_verf, NFSX_VERF);
5805 if ((dsp->nfsclds_flags & NFSCLDS_HASWRITEVERF) == 0) {
5806 NFSBCOPY(tl, dsp->nfsclds_verf, NFSX_VERF);
5807 dsp->nfsclds_flags |= NFSCLDS_HASWRITEVERF;
5808 } else if (NFSBCMP(tl, dsp->nfsclds_verf, NFSX_VERF)) {
5810 NFSBCOPY(tl, dsp->nfsclds_verf, NFSX_VERF);
5816 if (nd->nd_mrep != NULL)
5817 mbuf_freem(nd->nd_mrep);
5818 *iomode = committed;
5819 if (nd->nd_repstat != 0 && error == 0)
5820 error = nd->nd_repstat;
5825 * Free up the nfsclds structure.
5828 nfscl_freenfsclds(struct nfsclds *dsp)
5834 if (dsp->nfsclds_sockp != NULL) {
5835 NFSFREECRED(dsp->nfsclds_sockp->nr_cred);
5836 NFSFREEMUTEX(&dsp->nfsclds_sockp->nr_mtx);
5837 free(dsp->nfsclds_sockp->nr_nam, M_SONAME);
5838 free(dsp->nfsclds_sockp, M_NFSSOCKREQ);
5840 NFSFREEMUTEX(&dsp->nfsclds_mtx);
5841 NFSFREEMUTEX(&dsp->nfsclds_sess.nfsess_mtx);
5842 for (i = 0; i < NFSV4_CBSLOTS; i++) {
5843 if (dsp->nfsclds_sess.nfsess_cbslots[i].nfssl_reply != NULL)
5845 dsp->nfsclds_sess.nfsess_cbslots[i].nfssl_reply);
5847 free(dsp, M_NFSCLDS);
5850 static enum nfsclds_state
5851 nfscl_getsameserver(struct nfsmount *nmp, struct nfsclds *newdsp,
5852 struct nfsclds **retdspp)
5854 struct nfsclds *dsp, *cur_dsp;
5857 * Search the list of nfsclds structures for one with the same
5861 TAILQ_FOREACH(dsp, &nmp->nm_sess, nfsclds_list) {
5862 if (dsp->nfsclds_servownlen == newdsp->nfsclds_servownlen &&
5863 dsp->nfsclds_servownlen != 0 &&
5864 !NFSBCMP(dsp->nfsclds_serverown, newdsp->nfsclds_serverown,
5865 dsp->nfsclds_servownlen)) {
5866 NFSCL_DEBUG(4, "fnd same fdsp=%p dsp=%p flg=0x%x\n",
5867 TAILQ_FIRST(&nmp->nm_sess), dsp,
5868 dsp->nfsclds_flags);
5869 /* Server major id matches. */
5870 if ((dsp->nfsclds_flags & NFSCLDS_DS) != 0) {
5872 return (NFSDSP_USETHISSESSION);
5876 * Note the first match, so it can be used for
5877 * sequence'ing new sessions.
5879 if (cur_dsp == NULL)
5883 if (cur_dsp != NULL) {
5885 return (NFSDSP_SEQTHISSESSION);
5887 return (NFSDSP_NOTFOUND);
5892 * NFS commit rpc to a DS.
5895 nfsrpc_commitds(vnode_t vp, uint64_t offset, int cnt, struct nfsclds *dsp,
5896 struct nfsfh *fhp, struct ucred *cred, NFSPROC_T *p, void *stuff)
5899 struct nfsrv_descript nfsd, *nd = &nfsd;
5900 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
5901 struct nfssockreq *nrp;
5904 nfscl_reqstart(nd, NFSPROC_COMMITDS, nmp, fhp->nfh_fh, fhp->nfh_len,
5905 NULL, &dsp->nfsclds_sess);
5906 NFSM_BUILD(tl, uint32_t *, NFSX_HYPER + NFSX_UNSIGNED);
5907 txdr_hyper(offset, tl);
5909 *tl = txdr_unsigned(cnt);
5910 nrp = dsp->nfsclds_sockp;
5912 /* If NULL, use the MDS socket. */
5913 nrp = &nmp->nm_sockreq;
5914 error = newnfs_request(nd, nmp, NULL, nrp, vp, p, cred,
5915 NFS_PROG, NFS_VER4, NULL, 1, NULL, &dsp->nfsclds_sess);
5918 if (nd->nd_repstat == 0) {
5919 NFSM_DISSECT(tl, u_int32_t *, NFSX_VERF);
5921 if (NFSBCMP(tl, dsp->nfsclds_verf, NFSX_VERF)) {
5922 NFSBCOPY(tl, dsp->nfsclds_verf, NFSX_VERF);
5923 error = NFSERR_STALEWRITEVERF;
5928 if (error == 0 && nd->nd_repstat != 0)
5929 error = nd->nd_repstat;
5930 mbuf_freem(nd->nd_mrep);