2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Rick Macklem at The University of Guelph.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
38 * Rpc op calls, generally called from the vnode op calls or through the
39 * buffer cache, for NFS v2, 3 and 4.
40 * These do not normally make any changes to vnode arguments or use
41 * structures that might change between the VFS variants. The returned
42 * arguments are all at the end, after the NFSPROC_T *p one.
46 #include "opt_inet6.h"
48 #include <fs/nfs/nfsport.h>
49 #include <sys/sysctl.h>
51 SYSCTL_DECL(_vfs_nfs);
53 static int nfsignore_eexist = 0;
54 SYSCTL_INT(_vfs_nfs, OID_AUTO, ignore_eexist, CTLFLAG_RW,
55 &nfsignore_eexist, 0, "NFS ignore EEXIST replies for mkdir/symlink");
60 extern int nfs_numnfscbd;
61 extern struct timeval nfsboottime;
62 extern u_int32_t newnfs_false, newnfs_true;
63 extern nfstype nfsv34_type[9];
64 extern int nfsrv_useacl;
65 extern char nfsv4_callbackaddr[INET6_ADDRSTRLEN];
66 extern int nfscl_debuglevel;
68 int nfstest_outofseq = 0;
69 int nfscl_assumeposixlocks = 1;
70 int nfscl_enablecallb = 0;
71 short nfsv4_cbport = NFSV4_CBPORT;
72 int nfstest_openallsetattr = 0;
73 #endif /* !APPLEKEXT */
75 #define DIRHDSIZ (sizeof (struct dirent) - (MAXNAMLEN + 1))
78 * nfscl_getsameserver() can return one of three values:
79 * NFSDSP_USETHISSESSION - Use this session for the DS.
80 * NFSDSP_SEQTHISSESSION - Use the nfsclds_sequence field of this dsp for new
82 * NFSDSP_NOTFOUND - No matching server was found.
85 NFSDSP_USETHISSESSION = 0,
86 NFSDSP_SEQTHISSESSION = 1,
90 static int nfsrpc_setattrrpc(vnode_t , struct vattr *, nfsv4stateid_t *,
91 struct ucred *, NFSPROC_T *, struct nfsvattr *, int *, void *);
92 static int nfsrpc_readrpc(vnode_t , struct uio *, struct ucred *,
93 nfsv4stateid_t *, NFSPROC_T *, struct nfsvattr *, int *, void *);
94 static int nfsrpc_writerpc(vnode_t , struct uio *, int *, int *,
95 struct ucred *, nfsv4stateid_t *, NFSPROC_T *, struct nfsvattr *, int *,
97 static int nfsrpc_createv23(vnode_t , char *, int, struct vattr *,
98 nfsquad_t, int, struct ucred *, NFSPROC_T *, struct nfsvattr *,
99 struct nfsvattr *, struct nfsfh **, int *, int *, void *);
100 static int nfsrpc_createv4(vnode_t , char *, int, struct vattr *,
101 nfsquad_t, int, struct nfsclowner *, struct nfscldeleg **, struct ucred *,
102 NFSPROC_T *, struct nfsvattr *, struct nfsvattr *, struct nfsfh **, int *,
103 int *, void *, int *);
104 static int nfsrpc_locku(struct nfsrv_descript *, struct nfsmount *,
105 struct nfscllockowner *, u_int64_t, u_int64_t,
106 u_int32_t, struct ucred *, NFSPROC_T *, int);
107 static int nfsrpc_setaclrpc(vnode_t, struct ucred *, NFSPROC_T *,
108 struct acl *, nfsv4stateid_t *, void *);
109 static int nfsrpc_getlayout(struct nfsmount *, vnode_t, struct nfsfh *, int,
110 uint32_t *, nfsv4stateid_t *, uint64_t, struct nfscllayout **,
111 struct ucred *, NFSPROC_T *);
112 static int nfsrpc_fillsa(struct nfsmount *, struct sockaddr_storage *,
113 struct nfsclds **, NFSPROC_T *);
114 static void nfscl_initsessionslots(struct nfsclsession *);
115 static int nfscl_doflayoutio(vnode_t, struct uio *, int *, int *, int *,
116 nfsv4stateid_t *, int, struct nfscldevinfo *, struct nfscllayout *,
117 struct nfsclflayout *, uint64_t, uint64_t, struct ucred *, NFSPROC_T *);
118 static int nfsrpc_readds(vnode_t, struct uio *, nfsv4stateid_t *, int *,
119 struct nfsclds *, uint64_t, int, struct nfsfh *, struct ucred *,
121 static int nfsrpc_writeds(vnode_t, struct uio *, int *, int *,
122 nfsv4stateid_t *, struct nfsclds *, uint64_t, int,
123 struct nfsfh *, int, struct ucred *, NFSPROC_T *);
124 static enum nfsclds_state nfscl_getsameserver(struct nfsmount *,
125 struct nfsclds *, struct nfsclds **);
127 static int nfsrpc_commitds(vnode_t, uint64_t, int, struct nfsclds *,
128 struct nfsfh *, struct ucred *, NFSPROC_T *, void *);
132 * nfs null call from vfs.
135 nfsrpc_null(vnode_t vp, struct ucred *cred, NFSPROC_T *p)
138 struct nfsrv_descript nfsd, *nd = &nfsd;
140 NFSCL_REQSTART(nd, NFSPROC_NULL, vp);
141 error = nfscl_request(nd, vp, p, cred, NULL);
142 if (nd->nd_repstat && !error)
143 error = nd->nd_repstat;
144 mbuf_freem(nd->nd_mrep);
150 * For nfs version 3 and 4, use the access rpc to check accessibility. If file
151 * modes are changed on the server, accesses might still fail later.
154 nfsrpc_access(vnode_t vp, int acmode, struct ucred *cred,
155 NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp)
158 u_int32_t mode, rmode;
161 mode = NFSACCESS_READ;
164 if (vnode_vtype(vp) == VDIR) {
166 mode |= (NFSACCESS_MODIFY | NFSACCESS_EXTEND |
169 mode |= NFSACCESS_LOOKUP;
172 mode |= (NFSACCESS_MODIFY | NFSACCESS_EXTEND);
174 mode |= NFSACCESS_EXECUTE;
178 * Now, just call nfsrpc_accessrpc() to do the actual RPC.
180 error = nfsrpc_accessrpc(vp, mode, cred, p, nap, attrflagp, &rmode,
184 * The NFS V3 spec does not clarify whether or not
185 * the returned access bits can be a superset of
186 * the ones requested, so...
188 if (!error && (rmode & mode) != mode)
194 * The actual rpc, separated out for Darwin.
197 nfsrpc_accessrpc(vnode_t vp, u_int32_t mode, struct ucred *cred,
198 NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, u_int32_t *rmodep,
202 u_int32_t supported, rmode;
204 struct nfsrv_descript nfsd, *nd = &nfsd;
205 nfsattrbit_t attrbits;
209 NFSCL_REQSTART(nd, NFSPROC_ACCESS, vp);
210 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
211 *tl = txdr_unsigned(mode);
212 if (nd->nd_flag & ND_NFSV4) {
214 * And do a Getattr op.
216 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
217 *tl = txdr_unsigned(NFSV4OP_GETATTR);
218 NFSGETATTR_ATTRBIT(&attrbits);
219 (void) nfsrv_putattrbit(nd, &attrbits);
221 error = nfscl_request(nd, vp, p, cred, stuff);
224 if (nd->nd_flag & ND_NFSV3) {
225 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
229 if (!nd->nd_repstat) {
230 if (nd->nd_flag & ND_NFSV4) {
231 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
232 supported = fxdr_unsigned(u_int32_t, *tl++);
234 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
236 rmode = fxdr_unsigned(u_int32_t, *tl);
237 if (nd->nd_flag & ND_NFSV4)
238 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
241 * It's not obvious what should be done about
242 * unsupported access modes. For now, be paranoid
243 * and clear the unsupported ones.
248 error = nd->nd_repstat;
250 mbuf_freem(nd->nd_mrep);
258 nfsrpc_open(vnode_t vp, int amode, struct ucred *cred, NFSPROC_T *p)
260 struct nfsclopen *op;
261 struct nfscldeleg *dp;
263 struct nfsnode *np = VTONFS(vp);
264 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
265 u_int32_t mode, clidrev;
266 int ret, newone, error, expireret = 0, retrycnt;
269 * For NFSv4, Open Ops are only done on Regular Files.
271 if (vnode_vtype(vp) != VREG)
275 mode |= NFSV4OPEN_ACCESSREAD;
277 mode |= NFSV4OPEN_ACCESSWRITE;
282 { char name[100]; int namel;
283 namel = (np->n_v4->n4_namelen < 100) ? np->n_v4->n4_namelen : 99;
284 bcopy(NFS4NODENAME(np->n_v4), name, namel);
286 printf("rpcopen p=0x%x name=%s",p->p_pid,name);
287 if (nfhp->nfh_len > 0) printf(" fh=0x%x\n",nfhp->nfh_fh[12]);
288 else printf(" fhl=0\n");
293 error = nfscl_open(vp, nfhp->nfh_fh, nfhp->nfh_len, mode, 1,
294 cred, p, NULL, &op, &newone, &ret, 1);
298 if (nmp->nm_clp != NULL)
299 clidrev = nmp->nm_clp->nfsc_clientidrev;
302 if (ret == NFSCLOPEN_DOOPEN) {
303 if (np->n_v4 != NULL) {
304 error = nfsrpc_openrpc(nmp, vp, np->n_v4->n4_data,
305 np->n_v4->n4_fhlen, np->n_fhp->nfh_fh,
306 np->n_fhp->nfh_len, mode, op,
307 NFS4NODENAME(np->n_v4), np->n_v4->n4_namelen, &dp,
308 0, 0x0, cred, p, 0, 0);
311 OSBitAndAtomic((int32_t)~NDELEGMOD, (UInt32 *)&np->n_flag);
314 np->n_flag &= ~NDELEGMOD;
316 * Invalidate the attribute cache, so that
317 * attributes that pre-date the issue of a
318 * delegation are not cached, since the
319 * cached attributes will remain valid while
320 * the delegation is held.
322 NFSINVALATTRCACHE(np);
325 (void) nfscl_deleg(nmp->nm_mountp,
326 op->nfso_own->nfsow_clp,
327 nfhp->nfh_fh, nfhp->nfh_len, cred, p, &dp);
332 newnfs_copyincred(cred, &op->nfso_cred);
333 } else if (ret == NFSCLOPEN_SETCRED)
335 * This is a new local open on a delegation. It needs
336 * to have credentials so that an open can be done
337 * against the server during recovery.
339 newnfs_copyincred(cred, &op->nfso_cred);
342 * nfso_opencnt is the count of how many VOP_OPEN()s have
343 * been done on this Open successfully and a VOP_CLOSE()
344 * is expected for each of these.
345 * If error is non-zero, don't increment it, since the Open
346 * hasn't succeeded yet.
350 nfscl_openrelease(op, error, newone);
351 if (error == NFSERR_GRACE || error == NFSERR_STALECLIENTID ||
352 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
353 error == NFSERR_BADSESSION) {
354 (void) nfs_catnap(PZERO, error, "nfs_open");
355 } else if ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID)
357 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
360 } while (error == NFSERR_GRACE || error == NFSERR_STALECLIENTID ||
361 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
362 error == NFSERR_BADSESSION ||
363 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
364 expireret == 0 && clidrev != 0 && retrycnt < 4));
365 if (error && retrycnt >= 4)
371 * the actual open rpc
374 nfsrpc_openrpc(struct nfsmount *nmp, vnode_t vp, u_int8_t *nfhp, int fhlen,
375 u_int8_t *newfhp, int newfhlen, u_int32_t mode, struct nfsclopen *op,
376 u_int8_t *name, int namelen, struct nfscldeleg **dpp,
377 int reclaim, u_int32_t delegtype, struct ucred *cred, NFSPROC_T *p,
378 int syscred, int recursed)
381 struct nfsrv_descript nfsd, *nd = &nfsd;
382 struct nfscldeleg *dp, *ndp = NULL;
383 struct nfsvattr nfsva;
384 u_int32_t rflags, deleg;
385 nfsattrbit_t attrbits;
386 int error, ret, acesize, limitby;
390 nfscl_reqstart(nd, NFSPROC_OPEN, nmp, nfhp, fhlen, NULL, NULL);
391 NFSM_BUILD(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
392 *tl++ = txdr_unsigned(op->nfso_own->nfsow_seqid);
393 *tl++ = txdr_unsigned(mode & NFSV4OPEN_ACCESSBOTH);
394 *tl++ = txdr_unsigned((mode >> NFSLCK_SHIFT) & NFSV4OPEN_DENYBOTH);
395 *tl++ = NFSMNT_MDSSESSION(nmp)->nfsess_clientid.lval[0];
396 *tl = NFSMNT_MDSSESSION(nmp)->nfsess_clientid.lval[1];
397 (void) nfsm_strtom(nd, op->nfso_own->nfsow_owner, NFSV4CL_LOCKNAMELEN);
398 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
399 *tl++ = txdr_unsigned(NFSV4OPEN_NOCREATE);
401 *tl = txdr_unsigned(NFSV4OPEN_CLAIMPREVIOUS);
402 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
403 *tl = txdr_unsigned(delegtype);
406 *tl = txdr_unsigned(NFSV4OPEN_CLAIMDELEGATECUR);
407 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID);
408 if (NFSHASNFSV4N(nmp))
411 *tl++ = dp->nfsdl_stateid.seqid;
412 *tl++ = dp->nfsdl_stateid.other[0];
413 *tl++ = dp->nfsdl_stateid.other[1];
414 *tl = dp->nfsdl_stateid.other[2];
416 *tl = txdr_unsigned(NFSV4OPEN_CLAIMNULL);
418 (void) nfsm_strtom(nd, name, namelen);
420 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
421 *tl = txdr_unsigned(NFSV4OP_GETATTR);
422 NFSZERO_ATTRBIT(&attrbits);
423 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_CHANGE);
424 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEMODIFY);
425 (void) nfsrv_putattrbit(nd, &attrbits);
427 nd->nd_flag |= ND_USEGSSNAME;
428 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, vp, p, cred,
429 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
432 NFSCL_INCRSEQID(op->nfso_own->nfsow_seqid, nd);
433 if (!nd->nd_repstat) {
434 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
436 op->nfso_stateid.seqid = *tl++;
437 op->nfso_stateid.other[0] = *tl++;
438 op->nfso_stateid.other[1] = *tl++;
439 op->nfso_stateid.other[2] = *tl;
440 rflags = fxdr_unsigned(u_int32_t, *(tl + 6));
441 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
444 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
445 deleg = fxdr_unsigned(u_int32_t, *tl);
446 if (deleg == NFSV4OPEN_DELEGATEREAD ||
447 deleg == NFSV4OPEN_DELEGATEWRITE) {
448 if (!(op->nfso_own->nfsow_clp->nfsc_flags &
449 NFSCLFLAGS_FIRSTDELEG))
450 op->nfso_own->nfsow_clp->nfsc_flags |=
451 (NFSCLFLAGS_FIRSTDELEG | NFSCLFLAGS_GOTDELEG);
452 MALLOC(ndp, struct nfscldeleg *,
453 sizeof (struct nfscldeleg) + newfhlen,
454 M_NFSCLDELEG, M_WAITOK);
455 LIST_INIT(&ndp->nfsdl_owner);
456 LIST_INIT(&ndp->nfsdl_lock);
457 ndp->nfsdl_clp = op->nfso_own->nfsow_clp;
458 ndp->nfsdl_fhlen = newfhlen;
459 NFSBCOPY(newfhp, ndp->nfsdl_fh, newfhlen);
460 newnfs_copyincred(cred, &ndp->nfsdl_cred);
461 nfscl_lockinit(&ndp->nfsdl_rwlock);
462 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
464 ndp->nfsdl_stateid.seqid = *tl++;
465 ndp->nfsdl_stateid.other[0] = *tl++;
466 ndp->nfsdl_stateid.other[1] = *tl++;
467 ndp->nfsdl_stateid.other[2] = *tl++;
468 ret = fxdr_unsigned(int, *tl);
469 if (deleg == NFSV4OPEN_DELEGATEWRITE) {
470 ndp->nfsdl_flags = NFSCLDL_WRITE;
472 * Indicates how much the file can grow.
474 NFSM_DISSECT(tl, u_int32_t *,
476 limitby = fxdr_unsigned(int, *tl++);
478 case NFSV4OPEN_LIMITSIZE:
479 ndp->nfsdl_sizelimit = fxdr_hyper(tl);
481 case NFSV4OPEN_LIMITBLOCKS:
482 ndp->nfsdl_sizelimit =
483 fxdr_unsigned(u_int64_t, *tl++);
484 ndp->nfsdl_sizelimit *=
485 fxdr_unsigned(u_int64_t, *tl);
488 error = NFSERR_BADXDR;
492 ndp->nfsdl_flags = NFSCLDL_READ;
495 ndp->nfsdl_flags |= NFSCLDL_RECALL;
496 error = nfsrv_dissectace(nd, &ndp->nfsdl_ace, &ret,
500 } else if (deleg != NFSV4OPEN_DELEGATENONE) {
501 error = NFSERR_BADXDR;
504 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
505 error = nfsv4_loadattr(nd, NULL, &nfsva, NULL,
506 NULL, 0, NULL, NULL, NULL, NULL, NULL, 0,
507 NULL, NULL, NULL, p, cred);
511 ndp->nfsdl_change = nfsva.na_filerev;
512 ndp->nfsdl_modtime = nfsva.na_mtime;
513 ndp->nfsdl_flags |= NFSCLDL_MODTIMESET;
515 if (!reclaim && (rflags & NFSV4OPEN_RESULTCONFIRM)) {
517 ret = nfsrpc_openconfirm(vp, newfhp, newfhlen, op,
519 if (ret == NFSERR_DELAY)
520 (void) nfs_catnap(PZERO, ret, "nfs_open");
521 } while (ret == NFSERR_DELAY);
524 if ((rflags & NFSV4OPEN_LOCKTYPEPOSIX) ||
525 nfscl_assumeposixlocks)
526 op->nfso_posixlock = 1;
528 op->nfso_posixlock = 0;
531 * If the server is handing out delegations, but we didn't
532 * get one because an OpenConfirm was required, try the
533 * Open again, to get a delegation. This is a harmless no-op,
534 * from a server's point of view.
536 if (!reclaim && (rflags & NFSV4OPEN_RESULTCONFIRM) &&
537 (op->nfso_own->nfsow_clp->nfsc_flags & NFSCLFLAGS_GOTDELEG)
538 && !error && dp == NULL && ndp == NULL && !recursed) {
540 ret = nfsrpc_openrpc(nmp, vp, nfhp, fhlen, newfhp,
541 newfhlen, mode, op, name, namelen, &ndp, 0, 0x0,
542 cred, p, syscred, 1);
543 if (ret == NFSERR_DELAY)
544 (void) nfs_catnap(PZERO, ret, "nfs_open2");
545 } while (ret == NFSERR_DELAY);
548 FREE((caddr_t)ndp, M_NFSCLDELEG);
551 if (ret == NFSERR_STALECLIENTID ||
552 ret == NFSERR_STALEDONTRECOVER ||
553 ret == NFSERR_BADSESSION)
558 if (nd->nd_repstat != 0 && error == 0)
559 error = nd->nd_repstat;
560 if (error == NFSERR_STALECLIENTID || error == NFSERR_BADSESSION)
561 nfscl_initiate_recovery(op->nfso_own->nfsow_clp);
565 else if (ndp != NULL)
566 FREE((caddr_t)ndp, M_NFSCLDELEG);
567 mbuf_freem(nd->nd_mrep);
575 nfsrpc_opendowngrade(vnode_t vp, u_int32_t mode, struct nfsclopen *op,
576 struct ucred *cred, NFSPROC_T *p)
579 struct nfsrv_descript nfsd, *nd = &nfsd;
582 NFSCL_REQSTART(nd, NFSPROC_OPENDOWNGRADE, vp);
583 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID + 3 * NFSX_UNSIGNED);
584 if (NFSHASNFSV4N(VFSTONFS(vnode_mount(vp))))
587 *tl++ = op->nfso_stateid.seqid;
588 *tl++ = op->nfso_stateid.other[0];
589 *tl++ = op->nfso_stateid.other[1];
590 *tl++ = op->nfso_stateid.other[2];
591 *tl++ = txdr_unsigned(op->nfso_own->nfsow_seqid);
592 *tl++ = txdr_unsigned(mode & NFSV4OPEN_ACCESSBOTH);
593 *tl = txdr_unsigned((mode >> NFSLCK_SHIFT) & NFSV4OPEN_DENYBOTH);
594 error = nfscl_request(nd, vp, p, cred, NULL);
597 NFSCL_INCRSEQID(op->nfso_own->nfsow_seqid, nd);
598 if (!nd->nd_repstat) {
599 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID);
600 op->nfso_stateid.seqid = *tl++;
601 op->nfso_stateid.other[0] = *tl++;
602 op->nfso_stateid.other[1] = *tl++;
603 op->nfso_stateid.other[2] = *tl;
605 if (nd->nd_repstat && error == 0)
606 error = nd->nd_repstat;
607 if (error == NFSERR_STALESTATEID || error == NFSERR_BADSESSION)
608 nfscl_initiate_recovery(op->nfso_own->nfsow_clp);
610 mbuf_freem(nd->nd_mrep);
615 * V4 Close operation.
618 nfsrpc_close(vnode_t vp, int doclose, NFSPROC_T *p)
620 struct nfsclclient *clp;
623 if (vnode_vtype(vp) != VREG)
626 error = nfscl_doclose(vp, &clp, p);
628 error = nfscl_getclose(vp, &clp);
632 nfscl_clientrelease(clp);
640 nfsrpc_doclose(struct nfsmount *nmp, struct nfsclopen *op, NFSPROC_T *p)
642 struct nfsrv_descript nfsd, *nd = &nfsd;
643 struct nfscllockowner *lp, *nlp;
644 struct nfscllock *lop, *nlop;
646 u_int64_t off = 0, len = 0;
647 u_int32_t type = NFSV4LOCKT_READ;
648 int error, do_unlock, trycnt;
650 tcred = newnfs_getcred();
651 newnfs_copycred(&op->nfso_cred, tcred);
653 * (Theoretically this could be done in the same
654 * compound as the close, but having multiple
655 * sequenced Ops in the same compound might be
656 * too scary for some servers.)
658 if (op->nfso_posixlock) {
661 type = NFSV4LOCKT_READ;
665 * Since this function is only called from VOP_INACTIVE(), no
666 * other thread will be manipulating this Open. As such, the
667 * lock lists are not being changed by other threads, so it should
668 * be safe to do this without locking.
670 LIST_FOREACH(lp, &op->nfso_lock, nfsl_list) {
672 LIST_FOREACH_SAFE(lop, &lp->nfsl_lock, nfslo_list, nlop) {
673 if (op->nfso_posixlock == 0) {
674 off = lop->nfslo_first;
675 len = lop->nfslo_end - lop->nfslo_first;
676 if (lop->nfslo_type == F_WRLCK)
677 type = NFSV4LOCKT_WRITE;
679 type = NFSV4LOCKT_READ;
684 error = nfsrpc_locku(nd, nmp, lp, off,
685 len, type, tcred, p, 0);
686 if ((nd->nd_repstat == NFSERR_GRACE ||
687 nd->nd_repstat == NFSERR_DELAY) &&
689 (void) nfs_catnap(PZERO,
692 } while ((nd->nd_repstat == NFSERR_GRACE ||
693 nd->nd_repstat == NFSERR_DELAY) &&
694 error == 0 && trycnt++ < 5);
695 if (op->nfso_posixlock)
698 nfscl_freelock(lop, 0);
701 * Do a ReleaseLockOwner.
702 * The lock owner name nfsl_owner may be used by other opens for
703 * other files but the lock_owner4 name that nfsrpc_rellockown()
704 * puts on the wire has the file handle for this file appended
705 * to it, so it can be done now.
707 (void)nfsrpc_rellockown(nmp, lp, lp->nfsl_open->nfso_fh,
708 lp->nfsl_open->nfso_fhlen, tcred, p);
712 * There could be other Opens for different files on the same
713 * OpenOwner, so locking is required.
716 nfscl_lockexcl(&op->nfso_own->nfsow_rwlock, NFSCLSTATEMUTEXPTR);
719 error = nfscl_tryclose(op, tcred, nmp, p);
720 if (error == NFSERR_GRACE)
721 (void) nfs_catnap(PZERO, error, "nfs_close");
722 } while (error == NFSERR_GRACE);
724 nfscl_lockunlock(&op->nfso_own->nfsow_rwlock);
726 LIST_FOREACH_SAFE(lp, &op->nfso_lock, nfsl_list, nlp)
727 nfscl_freelockowner(lp, 0);
728 nfscl_freeopen(op, 0);
734 * The actual Close RPC.
737 nfsrpc_closerpc(struct nfsrv_descript *nd, struct nfsmount *nmp,
738 struct nfsclopen *op, struct ucred *cred, NFSPROC_T *p,
744 nfscl_reqstart(nd, NFSPROC_CLOSE, nmp, op->nfso_fh,
745 op->nfso_fhlen, NULL, NULL);
746 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED + NFSX_STATEID);
747 *tl++ = txdr_unsigned(op->nfso_own->nfsow_seqid);
748 if (NFSHASNFSV4N(nmp))
751 *tl++ = op->nfso_stateid.seqid;
752 *tl++ = op->nfso_stateid.other[0];
753 *tl++ = op->nfso_stateid.other[1];
754 *tl = op->nfso_stateid.other[2];
756 nd->nd_flag |= ND_USEGSSNAME;
757 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
758 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
761 NFSCL_INCRSEQID(op->nfso_own->nfsow_seqid, nd);
762 if (nd->nd_repstat == 0)
763 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID);
764 error = nd->nd_repstat;
765 if (error == NFSERR_STALESTATEID || error == NFSERR_BADSESSION)
766 nfscl_initiate_recovery(op->nfso_own->nfsow_clp);
768 mbuf_freem(nd->nd_mrep);
773 * V4 Open Confirm RPC.
776 nfsrpc_openconfirm(vnode_t vp, u_int8_t *nfhp, int fhlen,
777 struct nfsclopen *op, struct ucred *cred, NFSPROC_T *p)
780 struct nfsrv_descript nfsd, *nd = &nfsd;
781 struct nfsmount *nmp;
784 nmp = VFSTONFS(vnode_mount(vp));
785 if (NFSHASNFSV4N(nmp))
786 return (0); /* No confirmation for NFSv4.1. */
787 nfscl_reqstart(nd, NFSPROC_OPENCONFIRM, nmp, nfhp, fhlen, NULL, NULL);
788 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED + NFSX_STATEID);
789 *tl++ = op->nfso_stateid.seqid;
790 *tl++ = op->nfso_stateid.other[0];
791 *tl++ = op->nfso_stateid.other[1];
792 *tl++ = op->nfso_stateid.other[2];
793 *tl = txdr_unsigned(op->nfso_own->nfsow_seqid);
794 error = nfscl_request(nd, vp, p, cred, NULL);
797 NFSCL_INCRSEQID(op->nfso_own->nfsow_seqid, nd);
798 if (!nd->nd_repstat) {
799 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID);
800 op->nfso_stateid.seqid = *tl++;
801 op->nfso_stateid.other[0] = *tl++;
802 op->nfso_stateid.other[1] = *tl++;
803 op->nfso_stateid.other[2] = *tl;
805 error = nd->nd_repstat;
806 if (error == NFSERR_STALESTATEID || error == NFSERR_BADSESSION)
807 nfscl_initiate_recovery(op->nfso_own->nfsow_clp);
809 mbuf_freem(nd->nd_mrep);
814 * Do the setclientid and setclientid confirm RPCs. Called from nfs_statfs()
815 * when a mount has just occurred and when the server replies NFSERR_EXPIRED.
818 nfsrpc_setclient(struct nfsmount *nmp, struct nfsclclient *clp, int reclaim,
819 struct ucred *cred, NFSPROC_T *p)
822 struct nfsrv_descript nfsd;
823 struct nfsrv_descript *nd = &nfsd;
824 nfsattrbit_t attrbits;
825 u_int8_t *cp = NULL, *cp2, addr[INET6_ADDRSTRLEN + 9];
827 int error, isinet6 = 0, callblen;
830 static u_int32_t rev = 0;
831 struct nfsclds *dsp, *ndsp, *tdsp;
833 if (nfsboottime.tv_sec == 0)
834 NFSSETBOOTTIME(nfsboottime);
835 clp->nfsc_rev = rev++;
836 if (NFSHASNFSV4N(nmp)) {
837 error = nfsrpc_exchangeid(nmp, clp, &nmp->nm_sockreq,
838 NFSV4EXCH_USEPNFSMDS | NFSV4EXCH_USENONPNFS, &dsp, cred, p);
839 NFSCL_DEBUG(1, "aft exch=%d\n", error);
841 error = nfsrpc_createsession(nmp, &dsp->nfsclds_sess,
843 dsp->nfsclds_sess.nfsess_sequenceid, 1, cred, p);
846 TAILQ_FOREACH_SAFE(tdsp, &nmp->nm_sess,
848 nfscl_freenfsclds(tdsp);
849 TAILQ_INIT(&nmp->nm_sess);
850 TAILQ_INSERT_HEAD(&nmp->nm_sess, dsp,
854 nfscl_freenfsclds(dsp);
855 NFSCL_DEBUG(1, "aft createsess=%d\n", error);
857 if (error == 0 && reclaim == 0) {
858 error = nfsrpc_reclaimcomplete(nmp, cred, p);
859 NFSCL_DEBUG(1, "aft reclaimcomp=%d\n", error);
860 if (error == NFSERR_COMPLETEALREADY ||
861 error == NFSERR_NOTSUPP)
862 /* Ignore this error. */
869 * Allocate a single session structure for NFSv4.0, because some of
870 * the fields are used by NFSv4.0 although it doesn't do a session.
872 dsp = malloc(sizeof(struct nfsclds), M_NFSCLDS, M_WAITOK | M_ZERO);
873 mtx_init(&dsp->nfsclds_mtx, "nfsds", NULL, MTX_DEF);
874 mtx_init(&dsp->nfsclds_sess.nfsess_mtx, "nfssession", NULL, MTX_DEF);
876 TAILQ_INSERT_HEAD(&nmp->nm_sess, dsp, nfsclds_list);
879 nfscl_reqstart(nd, NFSPROC_SETCLIENTID, nmp, NULL, 0, NULL, NULL);
880 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
881 *tl++ = txdr_unsigned(nfsboottime.tv_sec);
882 *tl = txdr_unsigned(clp->nfsc_rev);
883 (void) nfsm_strtom(nd, clp->nfsc_id, clp->nfsc_idlen);
886 * set up the callback address
888 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
889 *tl = txdr_unsigned(NFS_CALLBCKPROG);
890 callblen = strlen(nfsv4_callbackaddr);
892 cp = nfscl_getmyip(nmp, &isinet6);
893 if (nfscl_enablecallb && nfs_numnfscbd > 0 &&
894 (callblen > 0 || cp != NULL)) {
895 port = htons(nfsv4_cbport);
896 cp2 = (u_int8_t *)&port;
899 strchr(nfsv4_callbackaddr, ':')) || isinet6) {
900 char ip6buf[INET6_ADDRSTRLEN], *ip6add;
902 (void) nfsm_strtom(nd, "tcp6", 4);
904 ip6_sprintf(ip6buf, (struct in6_addr *)cp);
907 ip6add = nfsv4_callbackaddr;
909 snprintf(addr, INET6_ADDRSTRLEN + 9, "%s.%d.%d",
910 ip6add, cp2[0], cp2[1]);
914 (void) nfsm_strtom(nd, "tcp", 3);
916 snprintf(addr, INET6_ADDRSTRLEN + 9,
917 "%d.%d.%d.%d.%d.%d", cp[0], cp[1],
918 cp[2], cp[3], cp2[0], cp2[1]);
920 snprintf(addr, INET6_ADDRSTRLEN + 9,
921 "%s.%d.%d", nfsv4_callbackaddr,
924 (void) nfsm_strtom(nd, addr, strlen(addr));
926 (void) nfsm_strtom(nd, "tcp", 3);
927 (void) nfsm_strtom(nd, "0.0.0.0.0.0", 11);
929 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
930 *tl = txdr_unsigned(clp->nfsc_cbident);
931 nd->nd_flag |= ND_USEGSSNAME;
932 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
933 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
936 if (nd->nd_repstat == 0) {
937 NFSM_DISSECT(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
938 NFSMNT_MDSSESSION(nmp)->nfsess_clientid.lval[0] = *tl++;
939 NFSMNT_MDSSESSION(nmp)->nfsess_clientid.lval[1] = *tl++;
940 confirm.lval[0] = *tl++;
941 confirm.lval[1] = *tl;
942 mbuf_freem(nd->nd_mrep);
948 nfscl_reqstart(nd, NFSPROC_SETCLIENTIDCFRM, nmp, NULL, 0, NULL,
950 NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
951 *tl++ = NFSMNT_MDSSESSION(nmp)->nfsess_clientid.lval[0];
952 *tl++ = NFSMNT_MDSSESSION(nmp)->nfsess_clientid.lval[1];
953 *tl++ = confirm.lval[0];
954 *tl = confirm.lval[1];
955 nd->nd_flag |= ND_USEGSSNAME;
956 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p,
957 cred, NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
960 mbuf_freem(nd->nd_mrep);
962 if (nd->nd_repstat == 0) {
963 nfscl_reqstart(nd, NFSPROC_GETATTR, nmp, nmp->nm_fh,
964 nmp->nm_fhsize, NULL, NULL);
965 NFSZERO_ATTRBIT(&attrbits);
966 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_LEASETIME);
967 (void) nfsrv_putattrbit(nd, &attrbits);
968 nd->nd_flag |= ND_USEGSSNAME;
969 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p,
970 cred, NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
973 if (nd->nd_repstat == 0) {
974 error = nfsv4_loadattr(nd, NULL, NULL, NULL, NULL, 0, NULL,
975 NULL, NULL, NULL, NULL, 0, NULL, &lease, NULL, p, cred);
978 clp->nfsc_renew = NFSCL_RENEW(lease);
979 clp->nfsc_expire = NFSD_MONOSEC + clp->nfsc_renew;
980 clp->nfsc_clientidrev++;
981 if (clp->nfsc_clientidrev == 0)
982 clp->nfsc_clientidrev++;
986 error = nd->nd_repstat;
988 mbuf_freem(nd->nd_mrep);
996 nfsrpc_getattr(vnode_t vp, struct ucred *cred, NFSPROC_T *p,
997 struct nfsvattr *nap, void *stuff)
999 struct nfsrv_descript nfsd, *nd = &nfsd;
1001 nfsattrbit_t attrbits;
1003 NFSCL_REQSTART(nd, NFSPROC_GETATTR, vp);
1004 if (nd->nd_flag & ND_NFSV4) {
1005 NFSGETATTR_ATTRBIT(&attrbits);
1006 (void) nfsrv_putattrbit(nd, &attrbits);
1008 error = nfscl_request(nd, vp, p, cred, stuff);
1011 if (!nd->nd_repstat)
1012 error = nfsm_loadattr(nd, nap);
1014 error = nd->nd_repstat;
1015 mbuf_freem(nd->nd_mrep);
1020 * nfs getattr call with non-vnode arguemnts.
1023 nfsrpc_getattrnovp(struct nfsmount *nmp, u_int8_t *fhp, int fhlen, int syscred,
1024 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, u_int64_t *xidp,
1027 struct nfsrv_descript nfsd, *nd = &nfsd;
1028 int error, vers = NFS_VER2;
1029 nfsattrbit_t attrbits;
1031 nfscl_reqstart(nd, NFSPROC_GETATTR, nmp, fhp, fhlen, NULL, NULL);
1032 if (nd->nd_flag & ND_NFSV4) {
1034 NFSGETATTR_ATTRBIT(&attrbits);
1035 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_LEASETIME);
1036 (void) nfsrv_putattrbit(nd, &attrbits);
1037 } else if (nd->nd_flag & ND_NFSV3) {
1041 nd->nd_flag |= ND_USEGSSNAME;
1042 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
1043 NFS_PROG, vers, NULL, 1, xidp, NULL);
1046 if (nd->nd_repstat == 0) {
1047 if ((nd->nd_flag & ND_NFSV4) != 0)
1048 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0,
1049 NULL, NULL, NULL, NULL, NULL, 0, NULL, leasep, NULL,
1052 error = nfsm_loadattr(nd, nap);
1054 error = nd->nd_repstat;
1055 mbuf_freem(nd->nd_mrep);
1060 * Do an nfs setattr operation.
1063 nfsrpc_setattr(vnode_t vp, struct vattr *vap, NFSACL_T *aclp,
1064 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *rnap, int *attrflagp,
1067 int error, expireret = 0, openerr, retrycnt;
1068 u_int32_t clidrev = 0, mode;
1069 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
1071 nfsv4stateid_t stateid;
1074 if (nmp->nm_clp != NULL)
1075 clidrev = nmp->nm_clp->nfsc_clientidrev;
1076 if (vap != NULL && NFSATTRISSET(u_quad_t, vap, va_size))
1077 mode = NFSV4OPEN_ACCESSWRITE;
1079 mode = NFSV4OPEN_ACCESSREAD;
1084 if (NFSHASNFSV4(nmp)) {
1085 nfhp = VTONFS(vp)->n_fhp;
1086 error = nfscl_getstateid(vp, nfhp->nfh_fh,
1087 nfhp->nfh_len, mode, 0, cred, p, &stateid, &lckp);
1088 if (error && vnode_vtype(vp) == VREG &&
1089 (mode == NFSV4OPEN_ACCESSWRITE ||
1090 nfstest_openallsetattr)) {
1092 * No Open stateid, so try and open the file
1095 if (mode == NFSV4OPEN_ACCESSWRITE)
1096 openerr = nfsrpc_open(vp, FWRITE, cred,
1099 openerr = nfsrpc_open(vp, FREAD, cred,
1102 (void) nfscl_getstateid(vp,
1103 nfhp->nfh_fh, nfhp->nfh_len,
1104 mode, 0, cred, p, &stateid, &lckp);
1108 error = nfsrpc_setattrrpc(vp, vap, &stateid, cred, p,
1109 rnap, attrflagp, stuff);
1111 error = nfsrpc_setaclrpc(vp, cred, p, aclp, &stateid,
1113 if (error == NFSERR_STALESTATEID || error == NFSERR_BADSESSION)
1114 nfscl_initiate_recovery(nmp->nm_clp);
1116 nfscl_lockderef(lckp);
1118 (void) nfsrpc_close(vp, 0, p);
1119 if (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
1120 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
1121 error == NFSERR_OLDSTATEID || error == NFSERR_BADSESSION) {
1122 (void) nfs_catnap(PZERO, error, "nfs_setattr");
1123 } else if ((error == NFSERR_EXPIRED ||
1124 error == NFSERR_BADSTATEID) && clidrev != 0) {
1125 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
1128 } while (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
1129 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
1130 error == NFSERR_BADSESSION ||
1131 (error == NFSERR_OLDSTATEID && retrycnt < 20) ||
1132 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
1133 expireret == 0 && clidrev != 0 && retrycnt < 4));
1134 if (error && retrycnt >= 4)
1140 nfsrpc_setattrrpc(vnode_t vp, struct vattr *vap,
1141 nfsv4stateid_t *stateidp, struct ucred *cred, NFSPROC_T *p,
1142 struct nfsvattr *rnap, int *attrflagp, void *stuff)
1145 struct nfsrv_descript nfsd, *nd = &nfsd;
1147 nfsattrbit_t attrbits;
1150 NFSCL_REQSTART(nd, NFSPROC_SETATTR, vp);
1151 if (nd->nd_flag & ND_NFSV4)
1152 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID);
1153 vap->va_type = vnode_vtype(vp);
1154 nfscl_fillsattr(nd, vap, vp, NFSSATTR_FULL, 0);
1155 if (nd->nd_flag & ND_NFSV3) {
1156 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1158 } else if (nd->nd_flag & ND_NFSV4) {
1159 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1160 *tl = txdr_unsigned(NFSV4OP_GETATTR);
1161 NFSGETATTR_ATTRBIT(&attrbits);
1162 (void) nfsrv_putattrbit(nd, &attrbits);
1164 error = nfscl_request(nd, vp, p, cred, stuff);
1167 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4))
1168 error = nfscl_wcc_data(nd, vp, rnap, attrflagp, NULL, stuff);
1169 if ((nd->nd_flag & ND_NFSV4) && !error)
1170 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
1171 if (!(nd->nd_flag & ND_NFSV3) && !nd->nd_repstat && !error)
1172 error = nfscl_postop_attr(nd, rnap, attrflagp, stuff);
1173 mbuf_freem(nd->nd_mrep);
1174 if (nd->nd_repstat && !error)
1175 error = nd->nd_repstat;
1183 nfsrpc_lookup(vnode_t dvp, char *name, int len, struct ucred *cred,
1184 NFSPROC_T *p, struct nfsvattr *dnap, struct nfsvattr *nap,
1185 struct nfsfh **nfhpp, int *attrflagp, int *dattrflagp, void *stuff)
1188 struct nfsrv_descript nfsd, *nd = &nfsd;
1189 struct nfsmount *nmp;
1192 nfsattrbit_t attrbits;
1193 int error = 0, lookupp = 0;
1197 if (vnode_vtype(dvp) != VDIR)
1199 nmp = VFSTONFS(vnode_mount(dvp));
1200 if (len > NFS_MAXNAMLEN)
1201 return (ENAMETOOLONG);
1202 if (NFSHASNFSV4(nmp) && len == 1 &&
1205 * Just return the current dir's fh.
1208 MALLOC(nfhp, struct nfsfh *, sizeof (struct nfsfh) +
1209 np->n_fhp->nfh_len, M_NFSFH, M_WAITOK);
1210 nfhp->nfh_len = np->n_fhp->nfh_len;
1211 NFSBCOPY(np->n_fhp->nfh_fh, nfhp->nfh_fh, nfhp->nfh_len);
1215 if (NFSHASNFSV4(nmp) && len == 2 &&
1216 name[0] == '.' && name[1] == '.') {
1218 NFSCL_REQSTART(nd, NFSPROC_LOOKUPP, dvp);
1220 NFSCL_REQSTART(nd, NFSPROC_LOOKUP, dvp);
1221 (void) nfsm_strtom(nd, name, len);
1223 if (nd->nd_flag & ND_NFSV4) {
1224 NFSGETATTR_ATTRBIT(&attrbits);
1225 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1226 *tl++ = txdr_unsigned(NFSV4OP_GETFH);
1227 *tl = txdr_unsigned(NFSV4OP_GETATTR);
1228 (void) nfsrv_putattrbit(nd, &attrbits);
1230 error = nfscl_request(nd, dvp, p, cred, stuff);
1233 if (nd->nd_repstat) {
1235 * When an NFSv4 Lookupp returns ENOENT, it means that
1236 * the lookup is at the root of an fs, so return this dir.
1238 if (nd->nd_repstat == NFSERR_NOENT && lookupp) {
1240 MALLOC(nfhp, struct nfsfh *, sizeof (struct nfsfh) +
1241 np->n_fhp->nfh_len, M_NFSFH, M_WAITOK);
1242 nfhp->nfh_len = np->n_fhp->nfh_len;
1243 NFSBCOPY(np->n_fhp->nfh_fh, nfhp->nfh_fh, nfhp->nfh_len);
1245 mbuf_freem(nd->nd_mrep);
1248 if (nd->nd_flag & ND_NFSV3)
1249 error = nfscl_postop_attr(nd, dnap, dattrflagp, stuff);
1250 else if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) ==
1252 /* Load the directory attributes. */
1253 error = nfsm_loadattr(nd, dnap);
1259 if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) == ND_NFSV4) {
1260 /* Load the directory attributes. */
1261 error = nfsm_loadattr(nd, dnap);
1265 /* Skip over the Lookup and GetFH operation status values. */
1266 NFSM_DISSECT(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1268 error = nfsm_getfh(nd, nfhpp);
1272 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
1273 if ((nd->nd_flag & ND_NFSV3) && !error)
1274 error = nfscl_postop_attr(nd, dnap, dattrflagp, stuff);
1276 mbuf_freem(nd->nd_mrep);
1277 if (!error && nd->nd_repstat)
1278 error = nd->nd_repstat;
1283 * Do a readlink rpc.
1286 nfsrpc_readlink(vnode_t vp, struct uio *uiop, struct ucred *cred,
1287 NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, void *stuff)
1290 struct nfsrv_descript nfsd, *nd = &nfsd;
1291 struct nfsnode *np = VTONFS(vp);
1292 nfsattrbit_t attrbits;
1293 int error, len, cangetattr = 1;
1296 NFSCL_REQSTART(nd, NFSPROC_READLINK, vp);
1297 if (nd->nd_flag & ND_NFSV4) {
1299 * And do a Getattr op.
1301 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1302 *tl = txdr_unsigned(NFSV4OP_GETATTR);
1303 NFSGETATTR_ATTRBIT(&attrbits);
1304 (void) nfsrv_putattrbit(nd, &attrbits);
1306 error = nfscl_request(nd, vp, p, cred, stuff);
1309 if (nd->nd_flag & ND_NFSV3)
1310 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
1311 if (!nd->nd_repstat && !error) {
1312 NFSM_STRSIZ(len, NFS_MAXPATHLEN);
1314 * This seems weird to me, but must have been added to
1315 * FreeBSD for some reason. The only thing I can think of
1316 * is that there was/is some server that replies with
1317 * more link data than it should?
1319 if (len == NFS_MAXPATHLEN) {
1321 if (np->n_size > 0 && np->n_size < NFS_MAXPATHLEN) {
1327 error = nfsm_mbufuio(nd, uiop, len);
1328 if ((nd->nd_flag & ND_NFSV4) && !error && cangetattr)
1329 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
1331 if (nd->nd_repstat && !error)
1332 error = nd->nd_repstat;
1334 mbuf_freem(nd->nd_mrep);
1342 nfsrpc_read(vnode_t vp, struct uio *uiop, struct ucred *cred,
1343 NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, void *stuff)
1345 int error, expireret = 0, retrycnt;
1346 u_int32_t clidrev = 0;
1347 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
1348 struct nfsnode *np = VTONFS(vp);
1349 struct ucred *newcred;
1350 struct nfsfh *nfhp = NULL;
1351 nfsv4stateid_t stateid;
1354 if (nmp->nm_clp != NULL)
1355 clidrev = nmp->nm_clp->nfsc_clientidrev;
1357 if (NFSHASNFSV4(nmp)) {
1359 newcred = NFSNEWCRED(cred);
1364 if (NFSHASNFSV4(nmp))
1365 (void)nfscl_getstateid(vp, nfhp->nfh_fh, nfhp->nfh_len,
1366 NFSV4OPEN_ACCESSREAD, 0, newcred, p, &stateid,
1368 error = nfsrpc_readrpc(vp, uiop, newcred, &stateid, p, nap,
1370 if (error == NFSERR_STALESTATEID || error == NFSERR_BADSESSION)
1371 nfscl_initiate_recovery(nmp->nm_clp);
1373 nfscl_lockderef(lckp);
1374 if (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
1375 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
1376 error == NFSERR_OLDSTATEID || error == NFSERR_BADSESSION) {
1377 (void) nfs_catnap(PZERO, error, "nfs_read");
1378 } else if ((error == NFSERR_EXPIRED ||
1379 error == NFSERR_BADSTATEID) && clidrev != 0) {
1380 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
1383 } while (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
1384 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
1385 error == NFSERR_BADSESSION ||
1386 (error == NFSERR_OLDSTATEID && retrycnt < 20) ||
1387 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
1388 expireret == 0 && clidrev != 0 && retrycnt < 4));
1389 if (error && retrycnt >= 4)
1391 if (NFSHASNFSV4(nmp))
1392 NFSFREECRED(newcred);
1397 * The actual read RPC.
1400 nfsrpc_readrpc(vnode_t vp, struct uio *uiop, struct ucred *cred,
1401 nfsv4stateid_t *stateidp, NFSPROC_T *p, struct nfsvattr *nap,
1402 int *attrflagp, void *stuff)
1405 int error = 0, len, retlen, tsiz, eof = 0;
1406 struct nfsrv_descript nfsd;
1407 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
1408 struct nfsrv_descript *nd = &nfsd;
1413 tsiz = uio_uio_resid(uiop);
1414 tmp_off = uiop->uio_offset + tsiz;
1416 if (tmp_off > nmp->nm_maxfilesize || tmp_off < uiop->uio_offset) {
1420 rsize = nmp->nm_rsize;
1425 len = (tsiz > rsize) ? rsize : tsiz;
1426 NFSCL_REQSTART(nd, NFSPROC_READ, vp);
1427 if (nd->nd_flag & ND_NFSV4)
1428 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID);
1429 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED * 3);
1430 if (nd->nd_flag & ND_NFSV2) {
1431 *tl++ = txdr_unsigned(uiop->uio_offset);
1432 *tl++ = txdr_unsigned(len);
1435 txdr_hyper(uiop->uio_offset, tl);
1436 *(tl + 2) = txdr_unsigned(len);
1439 * Since I can't do a Getattr for NFSv4 for Write, there
1440 * doesn't seem any point in doing one here, either.
1441 * (See the comment in nfsrpc_writerpc() for more info.)
1443 error = nfscl_request(nd, vp, p, cred, stuff);
1446 if (nd->nd_flag & ND_NFSV3) {
1447 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
1448 } else if (!nd->nd_repstat && (nd->nd_flag & ND_NFSV2)) {
1449 error = nfsm_loadattr(nd, nap);
1453 if (nd->nd_repstat || error) {
1455 error = nd->nd_repstat;
1458 if (nd->nd_flag & ND_NFSV3) {
1459 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1460 eof = fxdr_unsigned(int, *(tl + 1));
1461 } else if (nd->nd_flag & ND_NFSV4) {
1462 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
1463 eof = fxdr_unsigned(int, *tl);
1465 NFSM_STRSIZ(retlen, len);
1466 error = nfsm_mbufuio(nd, uiop, retlen);
1469 mbuf_freem(nd->nd_mrep);
1472 if (!(nd->nd_flag & ND_NFSV2)) {
1473 if (eof || retlen == 0)
1475 } else if (retlen < len)
1480 if (nd->nd_mrep != NULL)
1481 mbuf_freem(nd->nd_mrep);
1486 * nfs write operation
1487 * When called_from_strategy != 0, it should return EIO for an error that
1488 * indicates recovery is in progress, so that the buffer will be left
1489 * dirty and be written back to the server later. If it loops around,
1490 * the recovery thread could get stuck waiting for the buffer and recovery
1491 * will then deadlock.
1494 nfsrpc_write(vnode_t vp, struct uio *uiop, int *iomode, int *must_commit,
1495 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp,
1496 void *stuff, int called_from_strategy)
1498 int error, expireret = 0, retrycnt, nostateid;
1499 u_int32_t clidrev = 0;
1500 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
1501 struct nfsnode *np = VTONFS(vp);
1502 struct ucred *newcred;
1503 struct nfsfh *nfhp = NULL;
1504 nfsv4stateid_t stateid;
1508 if (nmp->nm_clp != NULL)
1509 clidrev = nmp->nm_clp->nfsc_clientidrev;
1511 if (NFSHASNFSV4(nmp)) {
1512 newcred = NFSNEWCRED(cred);
1519 if (NFSHASNFSV4(nmp)) {
1520 (void)nfscl_getstateid(vp, nfhp->nfh_fh, nfhp->nfh_len,
1521 NFSV4OPEN_ACCESSWRITE, 0, newcred, p, &stateid,
1523 if (stateid.other[0] == 0 && stateid.other[1] == 0 &&
1524 stateid.other[2] == 0) {
1526 NFSCL_DEBUG(1, "stateid0 in write\n");
1531 * If there is no stateid for NFSv4, it means this is an
1532 * extraneous write after close. Basically a poorly
1533 * implemented buffer cache. Just don't do the write.
1538 error = nfsrpc_writerpc(vp, uiop, iomode, must_commit,
1539 newcred, &stateid, p, nap, attrflagp, stuff);
1540 if (error == NFSERR_STALESTATEID || error == NFSERR_BADSESSION)
1541 nfscl_initiate_recovery(nmp->nm_clp);
1543 nfscl_lockderef(lckp);
1544 if (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
1545 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
1546 error == NFSERR_OLDSTATEID || error == NFSERR_BADSESSION) {
1547 (void) nfs_catnap(PZERO, error, "nfs_write");
1548 } else if ((error == NFSERR_EXPIRED ||
1549 error == NFSERR_BADSTATEID) && clidrev != 0) {
1550 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
1553 } while (error == NFSERR_GRACE || error == NFSERR_DELAY ||
1554 ((error == NFSERR_STALESTATEID || error == NFSERR_BADSESSION ||
1555 error == NFSERR_STALEDONTRECOVER) && called_from_strategy == 0) ||
1556 (error == NFSERR_OLDSTATEID && retrycnt < 20) ||
1557 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
1558 expireret == 0 && clidrev != 0 && retrycnt < 4));
1559 if (error != 0 && (retrycnt >= 4 ||
1560 ((error == NFSERR_STALESTATEID || error == NFSERR_BADSESSION ||
1561 error == NFSERR_STALEDONTRECOVER) && called_from_strategy != 0)))
1563 if (NFSHASNFSV4(nmp))
1564 NFSFREECRED(newcred);
1569 * The actual write RPC.
1572 nfsrpc_writerpc(vnode_t vp, struct uio *uiop, int *iomode,
1573 int *must_commit, struct ucred *cred, nfsv4stateid_t *stateidp,
1574 NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, void *stuff)
1577 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
1578 struct nfsnode *np = VTONFS(vp);
1579 int error = 0, len, tsiz, rlen, commit, committed = NFSWRITE_FILESYNC;
1580 int wccflag = 0, wsize;
1582 struct nfsrv_descript nfsd;
1583 struct nfsrv_descript *nd = &nfsd;
1584 nfsattrbit_t attrbits;
1587 KASSERT(uiop->uio_iovcnt == 1, ("nfs: writerpc iovcnt > 1"));
1589 tsiz = uio_uio_resid(uiop);
1590 tmp_off = uiop->uio_offset + tsiz;
1592 if (tmp_off > nmp->nm_maxfilesize || tmp_off < uiop->uio_offset) {
1596 wsize = nmp->nm_wsize;
1598 nd->nd_mrep = NULL; /* NFSv2 sometimes does a write with */
1599 nd->nd_repstat = 0; /* uio_resid == 0, so the while is not done */
1602 len = (tsiz > wsize) ? wsize : tsiz;
1603 NFSCL_REQSTART(nd, NFSPROC_WRITE, vp);
1604 if (nd->nd_flag & ND_NFSV4) {
1605 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID);
1606 NFSM_BUILD(tl, u_int32_t *, NFSX_HYPER+2*NFSX_UNSIGNED);
1607 txdr_hyper(uiop->uio_offset, tl);
1609 *tl++ = txdr_unsigned(*iomode);
1610 *tl = txdr_unsigned(len);
1611 } else if (nd->nd_flag & ND_NFSV3) {
1612 NFSM_BUILD(tl, u_int32_t *, NFSX_HYPER+3*NFSX_UNSIGNED);
1613 txdr_hyper(uiop->uio_offset, tl);
1615 *tl++ = txdr_unsigned(len);
1616 *tl++ = txdr_unsigned(*iomode);
1617 *tl = txdr_unsigned(len);
1621 NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1623 * Not sure why someone changed this, since the
1624 * RFC clearly states that "beginoffset" and
1625 * "totalcount" are ignored, but it wouldn't
1626 * surprise me if there's a busted server out there.
1628 /* Set both "begin" and "current" to non-garbage. */
1629 x = txdr_unsigned((u_int32_t)uiop->uio_offset);
1630 *tl++ = x; /* "begin offset" */
1631 *tl++ = x; /* "current offset" */
1632 x = txdr_unsigned(len);
1633 *tl++ = x; /* total to this offset */
1634 *tl = x; /* size of this write */
1637 nfsm_uiombuf(nd, uiop, len);
1639 * Although it is tempting to do a normal Getattr Op in the
1640 * NFSv4 compound, the result can be a nearly hung client
1641 * system if the Getattr asks for Owner and/or OwnerGroup.
1642 * It occurs when the client can't map either the Owner or
1643 * Owner_group name in the Getattr reply to a uid/gid. When
1644 * there is a cache miss, the kernel does an upcall to the
1645 * nfsuserd. Then, it can try and read the local /etc/passwd
1646 * or /etc/group file. It can then block in getnewbuf(),
1647 * waiting for dirty writes to be pushed to the NFS server.
1648 * The only reason this doesn't result in a complete
1649 * deadlock, is that the upcall times out and allows
1650 * the write to complete. However, progress is so slow
1651 * that it might just as well be deadlocked.
1652 * As such, we get the rest of the attributes, but not
1653 * Owner or Owner_group.
1654 * nb: nfscl_loadattrcache() needs to be told that these
1655 * partial attributes from a write rpc are being
1656 * passed in, via a argument flag.
1658 if (nd->nd_flag & ND_NFSV4) {
1659 NFSWRITEGETATTR_ATTRBIT(&attrbits);
1660 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1661 *tl = txdr_unsigned(NFSV4OP_GETATTR);
1662 (void) nfsrv_putattrbit(nd, &attrbits);
1664 error = nfscl_request(nd, vp, p, cred, stuff);
1667 if (nd->nd_repstat) {
1669 * In case the rpc gets retried, roll
1670 * the uio fileds changed by nfsm_uiombuf()
1673 uiop->uio_offset -= len;
1674 uio_uio_resid_add(uiop, len);
1675 uio_iov_base_add(uiop, -len);
1676 uio_iov_len_add(uiop, len);
1678 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) {
1679 error = nfscl_wcc_data(nd, vp, nap, attrflagp,
1684 if (!nd->nd_repstat) {
1685 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) {
1686 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED
1688 rlen = fxdr_unsigned(int, *tl++);
1692 } else if (rlen < len) {
1693 backup = len - rlen;
1694 uio_iov_base_add(uiop, -(backup));
1695 uio_iov_len_add(uiop, backup);
1696 uiop->uio_offset -= backup;
1697 uio_uio_resid_add(uiop, backup);
1700 commit = fxdr_unsigned(int, *tl++);
1703 * Return the lowest committment level
1704 * obtained by any of the RPCs.
1706 if (committed == NFSWRITE_FILESYNC)
1708 else if (committed == NFSWRITE_DATASYNC &&
1709 commit == NFSWRITE_UNSTABLE)
1712 if (!NFSHASWRITEVERF(nmp)) {
1713 NFSBCOPY((caddr_t)tl,
1714 (caddr_t)&nmp->nm_verf[0],
1716 NFSSETWRITEVERF(nmp);
1717 } else if (NFSBCMP(tl, nmp->nm_verf,
1720 NFSBCOPY(tl, nmp->nm_verf, NFSX_VERF);
1724 if (nd->nd_flag & ND_NFSV4)
1725 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1726 if (nd->nd_flag & (ND_NFSV2 | ND_NFSV4)) {
1727 error = nfsm_loadattr(nd, nap);
1729 *attrflagp = NFS_LATTR_NOSHRINK;
1732 error = nd->nd_repstat;
1736 NFSWRITERPC_SETTIME(wccflag, np, (nd->nd_flag & ND_NFSV4));
1737 mbuf_freem(nd->nd_mrep);
1742 if (nd->nd_mrep != NULL)
1743 mbuf_freem(nd->nd_mrep);
1744 *iomode = committed;
1745 if (nd->nd_repstat && !error)
1746 error = nd->nd_repstat;
1752 * For NFS v2 this is a kludge. Use a create rpc but with the IFMT bits of the
1753 * mode set to specify the file type and the size field for rdev.
1756 nfsrpc_mknod(vnode_t dvp, char *name, int namelen, struct vattr *vap,
1757 u_int32_t rdev, enum vtype vtyp, struct ucred *cred, NFSPROC_T *p,
1758 struct nfsvattr *dnap, struct nfsvattr *nnap, struct nfsfh **nfhpp,
1759 int *attrflagp, int *dattrflagp, void *dstuff)
1763 struct nfsrv_descript nfsd, *nd = &nfsd;
1764 nfsattrbit_t attrbits;
1769 if (namelen > NFS_MAXNAMLEN)
1770 return (ENAMETOOLONG);
1771 NFSCL_REQSTART(nd, NFSPROC_MKNOD, dvp);
1772 if (nd->nd_flag & ND_NFSV4) {
1773 if (vtyp == VBLK || vtyp == VCHR) {
1774 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
1775 *tl++ = vtonfsv34_type(vtyp);
1776 *tl++ = txdr_unsigned(NFSMAJOR(rdev));
1777 *tl = txdr_unsigned(NFSMINOR(rdev));
1779 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1780 *tl = vtonfsv34_type(vtyp);
1783 (void) nfsm_strtom(nd, name, namelen);
1784 if (nd->nd_flag & ND_NFSV3) {
1785 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1786 *tl = vtonfsv34_type(vtyp);
1788 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4))
1789 nfscl_fillsattr(nd, vap, dvp, 0, 0);
1790 if ((nd->nd_flag & ND_NFSV3) &&
1791 (vtyp == VCHR || vtyp == VBLK)) {
1792 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1793 *tl++ = txdr_unsigned(NFSMAJOR(rdev));
1794 *tl = txdr_unsigned(NFSMINOR(rdev));
1796 if (nd->nd_flag & ND_NFSV4) {
1797 NFSGETATTR_ATTRBIT(&attrbits);
1798 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1799 *tl++ = txdr_unsigned(NFSV4OP_GETFH);
1800 *tl = txdr_unsigned(NFSV4OP_GETATTR);
1801 (void) nfsrv_putattrbit(nd, &attrbits);
1803 if (nd->nd_flag & ND_NFSV2)
1804 nfscl_fillsattr(nd, vap, dvp, NFSSATTR_SIZERDEV, rdev);
1805 error = nfscl_request(nd, dvp, p, cred, dstuff);
1808 if (nd->nd_flag & ND_NFSV4)
1809 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
1810 if (!nd->nd_repstat) {
1811 if (nd->nd_flag & ND_NFSV4) {
1812 NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
1813 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
1817 error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp);
1821 if (nd->nd_flag & ND_NFSV3)
1822 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
1823 if (!error && nd->nd_repstat)
1824 error = nd->nd_repstat;
1826 mbuf_freem(nd->nd_mrep);
1831 * nfs file create call
1832 * Mostly just call the approriate routine. (I separated out v4, so that
1833 * error recovery wouldn't be as difficult.)
1836 nfsrpc_create(vnode_t dvp, char *name, int namelen, struct vattr *vap,
1837 nfsquad_t cverf, int fmode, struct ucred *cred, NFSPROC_T *p,
1838 struct nfsvattr *dnap, struct nfsvattr *nnap, struct nfsfh **nfhpp,
1839 int *attrflagp, int *dattrflagp, void *dstuff)
1841 int error = 0, newone, expireret = 0, retrycnt, unlocked;
1842 struct nfsclowner *owp;
1843 struct nfscldeleg *dp;
1844 struct nfsmount *nmp = VFSTONFS(vnode_mount(dvp));
1847 if (NFSHASNFSV4(nmp)) {
1851 error = nfscl_open(dvp, NULL, 0, (NFSV4OPEN_ACCESSWRITE |
1852 NFSV4OPEN_ACCESSREAD), 0, cred, p, &owp, NULL, &newone,
1856 if (nmp->nm_clp != NULL)
1857 clidrev = nmp->nm_clp->nfsc_clientidrev;
1860 error = nfsrpc_createv4(dvp, name, namelen, vap, cverf, fmode,
1861 owp, &dp, cred, p, dnap, nnap, nfhpp, attrflagp, dattrflagp,
1864 * There is no need to invalidate cached attributes here,
1865 * since new post-delegation issue attributes are always
1866 * returned by nfsrpc_createv4() and these will update the
1870 (void) nfscl_deleg(nmp->nm_mountp, owp->nfsow_clp,
1871 (*nfhpp)->nfh_fh, (*nfhpp)->nfh_len, cred, p, &dp);
1872 nfscl_ownerrelease(owp, error, newone, unlocked);
1873 if (error == NFSERR_GRACE || error == NFSERR_STALECLIENTID ||
1874 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
1875 error == NFSERR_BADSESSION) {
1876 (void) nfs_catnap(PZERO, error, "nfs_open");
1877 } else if ((error == NFSERR_EXPIRED ||
1878 error == NFSERR_BADSTATEID) && clidrev != 0) {
1879 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
1882 } while (error == NFSERR_GRACE || error == NFSERR_STALECLIENTID ||
1883 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
1884 error == NFSERR_BADSESSION ||
1885 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
1886 expireret == 0 && clidrev != 0 && retrycnt < 4));
1887 if (error && retrycnt >= 4)
1890 error = nfsrpc_createv23(dvp, name, namelen, vap, cverf,
1891 fmode, cred, p, dnap, nnap, nfhpp, attrflagp, dattrflagp,
1898 * The create rpc for v2 and 3.
1901 nfsrpc_createv23(vnode_t dvp, char *name, int namelen, struct vattr *vap,
1902 nfsquad_t cverf, int fmode, struct ucred *cred, NFSPROC_T *p,
1903 struct nfsvattr *dnap, struct nfsvattr *nnap, struct nfsfh **nfhpp,
1904 int *attrflagp, int *dattrflagp, void *dstuff)
1908 struct nfsrv_descript nfsd, *nd = &nfsd;
1913 if (namelen > NFS_MAXNAMLEN)
1914 return (ENAMETOOLONG);
1915 NFSCL_REQSTART(nd, NFSPROC_CREATE, dvp);
1916 (void) nfsm_strtom(nd, name, namelen);
1917 if (nd->nd_flag & ND_NFSV3) {
1918 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1919 if (fmode & O_EXCL) {
1920 *tl = txdr_unsigned(NFSCREATE_EXCLUSIVE);
1921 NFSM_BUILD(tl, u_int32_t *, NFSX_VERF);
1922 *tl++ = cverf.lval[0];
1923 *tl = cverf.lval[1];
1925 *tl = txdr_unsigned(NFSCREATE_UNCHECKED);
1926 nfscl_fillsattr(nd, vap, dvp, 0, 0);
1929 nfscl_fillsattr(nd, vap, dvp, NFSSATTR_SIZE0, 0);
1931 error = nfscl_request(nd, dvp, p, cred, dstuff);
1934 if (nd->nd_repstat == 0) {
1935 error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp);
1939 if (nd->nd_flag & ND_NFSV3)
1940 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
1941 if (nd->nd_repstat != 0 && error == 0)
1942 error = nd->nd_repstat;
1944 mbuf_freem(nd->nd_mrep);
1949 nfsrpc_createv4(vnode_t dvp, char *name, int namelen, struct vattr *vap,
1950 nfsquad_t cverf, int fmode, struct nfsclowner *owp, struct nfscldeleg **dpp,
1951 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap,
1952 struct nfsvattr *nnap, struct nfsfh **nfhpp, int *attrflagp,
1953 int *dattrflagp, void *dstuff, int *unlockedp)
1956 int error = 0, deleg, newone, ret, acesize, limitby;
1957 struct nfsrv_descript nfsd, *nd = &nfsd;
1958 struct nfsclopen *op;
1959 struct nfscldeleg *dp = NULL;
1962 nfsattrbit_t attrbits;
1963 nfsv4stateid_t stateid;
1965 struct nfsmount *nmp;
1967 nmp = VFSTONFS(dvp->v_mount);
1974 if (namelen > NFS_MAXNAMLEN)
1975 return (ENAMETOOLONG);
1976 NFSCL_REQSTART(nd, NFSPROC_CREATE, dvp);
1978 * For V4, this is actually an Open op.
1980 NFSM_BUILD(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
1981 *tl++ = txdr_unsigned(owp->nfsow_seqid);
1982 *tl++ = txdr_unsigned(NFSV4OPEN_ACCESSWRITE |
1983 NFSV4OPEN_ACCESSREAD);
1984 *tl++ = txdr_unsigned(NFSV4OPEN_DENYNONE);
1985 *tl++ = NFSMNT_MDSSESSION(nmp)->nfsess_clientid.lval[0];
1986 *tl = NFSMNT_MDSSESSION(nmp)->nfsess_clientid.lval[1];
1987 (void) nfsm_strtom(nd, owp->nfsow_owner, NFSV4CL_LOCKNAMELEN);
1988 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1989 *tl++ = txdr_unsigned(NFSV4OPEN_CREATE);
1990 if (fmode & O_EXCL) {
1991 if (NFSHASNFSV4N(nmp)) {
1992 if (NFSHASSESSPERSIST(nmp)) {
1993 /* Use GUARDED for persistent sessions. */
1994 *tl = txdr_unsigned(NFSCREATE_GUARDED);
1995 nfscl_fillsattr(nd, vap, dvp, 0, 0);
1997 /* Otherwise, use EXCLUSIVE4_1. */
1998 *tl = txdr_unsigned(NFSCREATE_EXCLUSIVE41);
1999 NFSM_BUILD(tl, u_int32_t *, NFSX_VERF);
2000 *tl++ = cverf.lval[0];
2001 *tl = cverf.lval[1];
2002 nfscl_fillsattr(nd, vap, dvp, 0, 0);
2006 *tl = txdr_unsigned(NFSCREATE_EXCLUSIVE);
2007 NFSM_BUILD(tl, u_int32_t *, NFSX_VERF);
2008 *tl++ = cverf.lval[0];
2009 *tl = cverf.lval[1];
2012 *tl = txdr_unsigned(NFSCREATE_UNCHECKED);
2013 nfscl_fillsattr(nd, vap, dvp, 0, 0);
2015 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2016 *tl = txdr_unsigned(NFSV4OPEN_CLAIMNULL);
2017 (void) nfsm_strtom(nd, name, namelen);
2018 /* Get the new file's handle and attributes. */
2019 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2020 *tl++ = txdr_unsigned(NFSV4OP_GETFH);
2021 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2022 NFSGETATTR_ATTRBIT(&attrbits);
2023 (void) nfsrv_putattrbit(nd, &attrbits);
2024 /* Get the directory's post-op attributes. */
2025 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2026 *tl = txdr_unsigned(NFSV4OP_PUTFH);
2027 (void) nfsm_fhtom(nd, np->n_fhp->nfh_fh, np->n_fhp->nfh_len, 0);
2028 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2029 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2030 (void) nfsrv_putattrbit(nd, &attrbits);
2031 error = nfscl_request(nd, dvp, p, cred, dstuff);
2034 NFSCL_INCRSEQID(owp->nfsow_seqid, nd);
2035 if (nd->nd_repstat == 0) {
2036 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
2038 stateid.seqid = *tl++;
2039 stateid.other[0] = *tl++;
2040 stateid.other[1] = *tl++;
2041 stateid.other[2] = *tl;
2042 rflags = fxdr_unsigned(u_int32_t, *(tl + 6));
2043 (void) nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
2044 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2045 deleg = fxdr_unsigned(int, *tl);
2046 if (deleg == NFSV4OPEN_DELEGATEREAD ||
2047 deleg == NFSV4OPEN_DELEGATEWRITE) {
2048 if (!(owp->nfsow_clp->nfsc_flags &
2049 NFSCLFLAGS_FIRSTDELEG))
2050 owp->nfsow_clp->nfsc_flags |=
2051 (NFSCLFLAGS_FIRSTDELEG | NFSCLFLAGS_GOTDELEG);
2052 MALLOC(dp, struct nfscldeleg *,
2053 sizeof (struct nfscldeleg) + NFSX_V4FHMAX,
2054 M_NFSCLDELEG, M_WAITOK);
2055 LIST_INIT(&dp->nfsdl_owner);
2056 LIST_INIT(&dp->nfsdl_lock);
2057 dp->nfsdl_clp = owp->nfsow_clp;
2058 newnfs_copyincred(cred, &dp->nfsdl_cred);
2059 nfscl_lockinit(&dp->nfsdl_rwlock);
2060 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
2062 dp->nfsdl_stateid.seqid = *tl++;
2063 dp->nfsdl_stateid.other[0] = *tl++;
2064 dp->nfsdl_stateid.other[1] = *tl++;
2065 dp->nfsdl_stateid.other[2] = *tl++;
2066 ret = fxdr_unsigned(int, *tl);
2067 if (deleg == NFSV4OPEN_DELEGATEWRITE) {
2068 dp->nfsdl_flags = NFSCLDL_WRITE;
2070 * Indicates how much the file can grow.
2072 NFSM_DISSECT(tl, u_int32_t *,
2074 limitby = fxdr_unsigned(int, *tl++);
2076 case NFSV4OPEN_LIMITSIZE:
2077 dp->nfsdl_sizelimit = fxdr_hyper(tl);
2079 case NFSV4OPEN_LIMITBLOCKS:
2080 dp->nfsdl_sizelimit =
2081 fxdr_unsigned(u_int64_t, *tl++);
2082 dp->nfsdl_sizelimit *=
2083 fxdr_unsigned(u_int64_t, *tl);
2086 error = NFSERR_BADXDR;
2090 dp->nfsdl_flags = NFSCLDL_READ;
2093 dp->nfsdl_flags |= NFSCLDL_RECALL;
2094 error = nfsrv_dissectace(nd, &dp->nfsdl_ace, &ret,
2098 } else if (deleg != NFSV4OPEN_DELEGATENONE) {
2099 error = NFSERR_BADXDR;
2102 error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp);
2105 /* Get rid of the PutFH and Getattr status values. */
2106 NFSM_DISSECT(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
2107 /* Load the directory attributes. */
2108 error = nfsm_loadattr(nd, dnap);
2112 if (dp != NULL && *attrflagp) {
2113 dp->nfsdl_change = nnap->na_filerev;
2114 dp->nfsdl_modtime = nnap->na_mtime;
2115 dp->nfsdl_flags |= NFSCLDL_MODTIMESET;
2118 * We can now complete the Open state.
2122 dp->nfsdl_fhlen = nfhp->nfh_len;
2123 NFSBCOPY(nfhp->nfh_fh, dp->nfsdl_fh, nfhp->nfh_len);
2126 * Get an Open structure that will be
2127 * attached to the OpenOwner, acquired already.
2129 error = nfscl_open(dvp, nfhp->nfh_fh, nfhp->nfh_len,
2130 (NFSV4OPEN_ACCESSWRITE | NFSV4OPEN_ACCESSREAD), 0,
2131 cred, p, NULL, &op, &newone, NULL, 0);
2134 op->nfso_stateid = stateid;
2135 newnfs_copyincred(cred, &op->nfso_cred);
2136 if ((rflags & NFSV4OPEN_RESULTCONFIRM)) {
2138 ret = nfsrpc_openconfirm(dvp, nfhp->nfh_fh,
2139 nfhp->nfh_len, op, cred, p);
2140 if (ret == NFSERR_DELAY)
2141 (void) nfs_catnap(PZERO, ret, "nfs_create");
2142 } while (ret == NFSERR_DELAY);
2147 * If the server is handing out delegations, but we didn't
2148 * get one because an OpenConfirm was required, try the
2149 * Open again, to get a delegation. This is a harmless no-op,
2150 * from a server's point of view.
2152 if ((rflags & NFSV4OPEN_RESULTCONFIRM) &&
2153 (owp->nfsow_clp->nfsc_flags & NFSCLFLAGS_GOTDELEG) &&
2154 !error && dp == NULL) {
2156 ret = nfsrpc_openrpc(VFSTONFS(vnode_mount(dvp)), dvp,
2157 np->n_fhp->nfh_fh, np->n_fhp->nfh_len,
2158 nfhp->nfh_fh, nfhp->nfh_len,
2159 (NFSV4OPEN_ACCESSWRITE | NFSV4OPEN_ACCESSREAD), op,
2160 name, namelen, &dp, 0, 0x0, cred, p, 0, 1);
2161 if (ret == NFSERR_DELAY)
2162 (void) nfs_catnap(PZERO, ret, "nfs_crt2");
2163 } while (ret == NFSERR_DELAY);
2166 FREE((caddr_t)dp, M_NFSCLDELEG);
2169 if (ret == NFSERR_STALECLIENTID ||
2170 ret == NFSERR_STALEDONTRECOVER ||
2171 ret == NFSERR_BADSESSION)
2175 nfscl_openrelease(op, error, newone);
2178 if (nd->nd_repstat != 0 && error == 0)
2179 error = nd->nd_repstat;
2180 if (error == NFSERR_STALECLIENTID || error == NFSERR_BADSESSION)
2181 nfscl_initiate_recovery(owp->nfsow_clp);
2185 else if (dp != NULL)
2186 FREE((caddr_t)dp, M_NFSCLDELEG);
2187 mbuf_freem(nd->nd_mrep);
2195 nfsrpc_remove(vnode_t dvp, char *name, int namelen, vnode_t vp,
2196 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap, int *dattrflagp,
2200 struct nfsrv_descript nfsd, *nd = &nfsd;
2202 struct nfsmount *nmp;
2203 nfsv4stateid_t dstateid;
2204 int error, ret = 0, i;
2207 if (namelen > NFS_MAXNAMLEN)
2208 return (ENAMETOOLONG);
2209 nmp = VFSTONFS(vnode_mount(dvp));
2211 if (NFSHASNFSV4(nmp) && ret == 0) {
2212 ret = nfscl_removedeleg(vp, p, &dstateid);
2214 NFSCL_REQSTART(nd, NFSPROC_RETDELEGREMOVE, vp);
2215 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID +
2217 if (NFSHASNFSV4N(nmp))
2220 *tl++ = dstateid.seqid;
2221 *tl++ = dstateid.other[0];
2222 *tl++ = dstateid.other[1];
2223 *tl++ = dstateid.other[2];
2224 *tl = txdr_unsigned(NFSV4OP_PUTFH);
2226 (void) nfsm_fhtom(nd, np->n_fhp->nfh_fh,
2227 np->n_fhp->nfh_len, 0);
2228 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2229 *tl = txdr_unsigned(NFSV4OP_REMOVE);
2235 NFSCL_REQSTART(nd, NFSPROC_REMOVE, dvp);
2236 (void) nfsm_strtom(nd, name, namelen);
2237 error = nfscl_request(nd, dvp, p, cred, dstuff);
2240 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) {
2241 /* For NFSv4, parse out any Delereturn replies. */
2242 if (ret > 0 && nd->nd_repstat != 0 &&
2243 (nd->nd_flag & ND_NOMOREDATA)) {
2245 * If the Delegreturn failed, try again without
2246 * it. The server will Recall, as required.
2248 mbuf_freem(nd->nd_mrep);
2251 for (i = 0; i < (ret * 2); i++) {
2252 if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) ==
2254 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2256 nd->nd_flag |= ND_NOMOREDATA;
2259 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
2261 if (nd->nd_repstat && !error)
2262 error = nd->nd_repstat;
2264 mbuf_freem(nd->nd_mrep);
2269 * Do an nfs rename rpc.
2272 nfsrpc_rename(vnode_t fdvp, vnode_t fvp, char *fnameptr, int fnamelen,
2273 vnode_t tdvp, vnode_t tvp, char *tnameptr, int tnamelen, struct ucred *cred,
2274 NFSPROC_T *p, struct nfsvattr *fnap, struct nfsvattr *tnap,
2275 int *fattrflagp, int *tattrflagp, void *fstuff, void *tstuff)
2278 struct nfsrv_descript nfsd, *nd = &nfsd;
2279 struct nfsmount *nmp;
2281 nfsattrbit_t attrbits;
2282 nfsv4stateid_t fdstateid, tdstateid;
2283 int error = 0, ret = 0, gottd = 0, gotfd = 0, i;
2287 nmp = VFSTONFS(vnode_mount(fdvp));
2288 if (fnamelen > NFS_MAXNAMLEN || tnamelen > NFS_MAXNAMLEN)
2289 return (ENAMETOOLONG);
2291 if (NFSHASNFSV4(nmp) && ret == 0) {
2292 ret = nfscl_renamedeleg(fvp, &fdstateid, &gotfd, tvp,
2293 &tdstateid, &gottd, p);
2294 if (gotfd && gottd) {
2295 NFSCL_REQSTART(nd, NFSPROC_RETDELEGRENAME2, fvp);
2297 NFSCL_REQSTART(nd, NFSPROC_RETDELEGRENAME1, fvp);
2299 NFSCL_REQSTART(nd, NFSPROC_RETDELEGRENAME1, tvp);
2302 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID);
2303 if (NFSHASNFSV4N(nmp))
2306 *tl++ = fdstateid.seqid;
2307 *tl++ = fdstateid.other[0];
2308 *tl++ = fdstateid.other[1];
2309 *tl = fdstateid.other[2];
2311 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2312 *tl = txdr_unsigned(NFSV4OP_PUTFH);
2314 (void) nfsm_fhtom(nd, np->n_fhp->nfh_fh,
2315 np->n_fhp->nfh_len, 0);
2316 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2317 *tl = txdr_unsigned(NFSV4OP_DELEGRETURN);
2321 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID);
2322 if (NFSHASNFSV4N(nmp))
2325 *tl++ = tdstateid.seqid;
2326 *tl++ = tdstateid.other[0];
2327 *tl++ = tdstateid.other[1];
2328 *tl = tdstateid.other[2];
2331 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2332 *tl = txdr_unsigned(NFSV4OP_PUTFH);
2334 (void) nfsm_fhtom(nd, np->n_fhp->nfh_fh,
2335 np->n_fhp->nfh_len, 0);
2336 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2337 *tl = txdr_unsigned(NFSV4OP_SAVEFH);
2343 NFSCL_REQSTART(nd, NFSPROC_RENAME, fdvp);
2344 if (nd->nd_flag & ND_NFSV4) {
2345 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2346 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2347 NFSWCCATTR_ATTRBIT(&attrbits);
2348 (void) nfsrv_putattrbit(nd, &attrbits);
2349 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2350 *tl = txdr_unsigned(NFSV4OP_PUTFH);
2351 (void) nfsm_fhtom(nd, VTONFS(tdvp)->n_fhp->nfh_fh,
2352 VTONFS(tdvp)->n_fhp->nfh_len, 0);
2353 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2354 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2355 (void) nfsrv_putattrbit(nd, &attrbits);
2356 nd->nd_flag |= ND_V4WCCATTR;
2357 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2358 *tl = txdr_unsigned(NFSV4OP_RENAME);
2360 (void) nfsm_strtom(nd, fnameptr, fnamelen);
2361 if (!(nd->nd_flag & ND_NFSV4))
2362 (void) nfsm_fhtom(nd, VTONFS(tdvp)->n_fhp->nfh_fh,
2363 VTONFS(tdvp)->n_fhp->nfh_len, 0);
2364 (void) nfsm_strtom(nd, tnameptr, tnamelen);
2365 error = nfscl_request(nd, fdvp, p, cred, fstuff);
2368 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) {
2369 /* For NFSv4, parse out any Delereturn replies. */
2370 if (ret > 0 && nd->nd_repstat != 0 &&
2371 (nd->nd_flag & ND_NOMOREDATA)) {
2373 * If the Delegreturn failed, try again without
2374 * it. The server will Recall, as required.
2376 mbuf_freem(nd->nd_mrep);
2379 for (i = 0; i < (ret * 2); i++) {
2380 if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) ==
2382 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2384 if (i == 0 && ret > 1) {
2386 * If the Delegreturn failed, try again
2387 * without it. The server will Recall, as
2389 * If ret > 1, the first iteration of this
2390 * loop is the second DelegReturn result.
2392 mbuf_freem(nd->nd_mrep);
2395 nd->nd_flag |= ND_NOMOREDATA;
2400 /* Now, the first wcc attribute reply. */
2401 if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) == ND_NFSV4) {
2402 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2404 nd->nd_flag |= ND_NOMOREDATA;
2406 error = nfscl_wcc_data(nd, fdvp, fnap, fattrflagp, NULL,
2408 /* and the second wcc attribute reply. */
2409 if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) == ND_NFSV4 &&
2411 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2413 nd->nd_flag |= ND_NOMOREDATA;
2416 error = nfscl_wcc_data(nd, tdvp, tnap, tattrflagp,
2419 if (nd->nd_repstat && !error)
2420 error = nd->nd_repstat;
2422 mbuf_freem(nd->nd_mrep);
2427 * nfs hard link create rpc
2430 nfsrpc_link(vnode_t dvp, vnode_t vp, char *name, int namelen,
2431 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap,
2432 struct nfsvattr *nap, int *attrflagp, int *dattrflagp, void *dstuff)
2435 struct nfsrv_descript nfsd, *nd = &nfsd;
2436 nfsattrbit_t attrbits;
2441 if (namelen > NFS_MAXNAMLEN)
2442 return (ENAMETOOLONG);
2443 NFSCL_REQSTART(nd, NFSPROC_LINK, vp);
2444 if (nd->nd_flag & ND_NFSV4) {
2445 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2446 *tl = txdr_unsigned(NFSV4OP_PUTFH);
2448 (void) nfsm_fhtom(nd, VTONFS(dvp)->n_fhp->nfh_fh,
2449 VTONFS(dvp)->n_fhp->nfh_len, 0);
2450 if (nd->nd_flag & ND_NFSV4) {
2451 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2452 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2453 NFSWCCATTR_ATTRBIT(&attrbits);
2454 (void) nfsrv_putattrbit(nd, &attrbits);
2455 nd->nd_flag |= ND_V4WCCATTR;
2456 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2457 *tl = txdr_unsigned(NFSV4OP_LINK);
2459 (void) nfsm_strtom(nd, name, namelen);
2460 error = nfscl_request(nd, vp, p, cred, dstuff);
2463 if (nd->nd_flag & ND_NFSV3) {
2464 error = nfscl_postop_attr(nd, nap, attrflagp, dstuff);
2466 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp,
2468 } else if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) == ND_NFSV4) {
2470 * First, parse out the PutFH and Getattr result.
2472 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2474 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2476 nd->nd_flag |= ND_NOMOREDATA;
2478 * Get the pre-op attributes.
2480 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
2482 if (nd->nd_repstat && !error)
2483 error = nd->nd_repstat;
2485 mbuf_freem(nd->nd_mrep);
2490 * nfs symbolic link create rpc
2493 nfsrpc_symlink(vnode_t dvp, char *name, int namelen, char *target,
2494 struct vattr *vap, struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap,
2495 struct nfsvattr *nnap, struct nfsfh **nfhpp, int *attrflagp,
2496 int *dattrflagp, void *dstuff)
2499 struct nfsrv_descript nfsd, *nd = &nfsd;
2500 struct nfsmount *nmp;
2501 int slen, error = 0;
2506 nmp = VFSTONFS(vnode_mount(dvp));
2507 slen = strlen(target);
2508 if (slen > NFS_MAXPATHLEN || namelen > NFS_MAXNAMLEN)
2509 return (ENAMETOOLONG);
2510 NFSCL_REQSTART(nd, NFSPROC_SYMLINK, dvp);
2511 if (nd->nd_flag & ND_NFSV4) {
2512 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2513 *tl = txdr_unsigned(NFLNK);
2514 (void) nfsm_strtom(nd, target, slen);
2516 (void) nfsm_strtom(nd, name, namelen);
2517 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4))
2518 nfscl_fillsattr(nd, vap, dvp, 0, 0);
2519 if (!(nd->nd_flag & ND_NFSV4))
2520 (void) nfsm_strtom(nd, target, slen);
2521 if (nd->nd_flag & ND_NFSV2)
2522 nfscl_fillsattr(nd, vap, dvp, NFSSATTR_SIZENEG1, 0);
2523 error = nfscl_request(nd, dvp, p, cred, dstuff);
2526 if (nd->nd_flag & ND_NFSV4)
2527 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
2528 if ((nd->nd_flag & ND_NFSV3) && !error) {
2529 if (!nd->nd_repstat)
2530 error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp);
2532 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp,
2535 if (nd->nd_repstat && !error)
2536 error = nd->nd_repstat;
2537 mbuf_freem(nd->nd_mrep);
2539 * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry.
2540 * Only do this if vfs.nfs.ignore_eexist is set.
2541 * Never do this for NFSv4.1 or later minor versions, since sessions
2542 * should guarantee "exactly once" RPC semantics.
2544 if (error == EEXIST && nfsignore_eexist != 0 && (!NFSHASNFSV4(nmp) ||
2545 nmp->nm_minorvers == 0))
2554 nfsrpc_mkdir(vnode_t dvp, char *name, int namelen, struct vattr *vap,
2555 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap,
2556 struct nfsvattr *nnap, struct nfsfh **nfhpp, int *attrflagp,
2557 int *dattrflagp, void *dstuff)
2560 struct nfsrv_descript nfsd, *nd = &nfsd;
2561 nfsattrbit_t attrbits;
2564 struct nfsmount *nmp;
2569 nmp = VFSTONFS(vnode_mount(dvp));
2570 fhp = VTONFS(dvp)->n_fhp;
2571 if (namelen > NFS_MAXNAMLEN)
2572 return (ENAMETOOLONG);
2573 NFSCL_REQSTART(nd, NFSPROC_MKDIR, dvp);
2574 if (nd->nd_flag & ND_NFSV4) {
2575 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2576 *tl = txdr_unsigned(NFDIR);
2578 (void) nfsm_strtom(nd, name, namelen);
2579 nfscl_fillsattr(nd, vap, dvp, NFSSATTR_SIZENEG1, 0);
2580 if (nd->nd_flag & ND_NFSV4) {
2581 NFSGETATTR_ATTRBIT(&attrbits);
2582 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2583 *tl++ = txdr_unsigned(NFSV4OP_GETFH);
2584 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2585 (void) nfsrv_putattrbit(nd, &attrbits);
2586 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2587 *tl = txdr_unsigned(NFSV4OP_PUTFH);
2588 (void) nfsm_fhtom(nd, fhp->nfh_fh, fhp->nfh_len, 0);
2589 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2590 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2591 (void) nfsrv_putattrbit(nd, &attrbits);
2593 error = nfscl_request(nd, dvp, p, cred, dstuff);
2596 if (nd->nd_flag & ND_NFSV4)
2597 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
2598 if (!nd->nd_repstat && !error) {
2599 if (nd->nd_flag & ND_NFSV4) {
2600 NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
2601 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
2604 error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp);
2605 if (error == 0 && (nd->nd_flag & ND_NFSV4) != 0) {
2606 /* Get rid of the PutFH and Getattr status values. */
2607 NFSM_DISSECT(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
2608 /* Load the directory attributes. */
2609 error = nfsm_loadattr(nd, dnap);
2614 if ((nd->nd_flag & ND_NFSV3) && !error)
2615 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
2616 if (nd->nd_repstat && !error)
2617 error = nd->nd_repstat;
2619 mbuf_freem(nd->nd_mrep);
2621 * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry.
2622 * Only do this if vfs.nfs.ignore_eexist is set.
2623 * Never do this for NFSv4.1 or later minor versions, since sessions
2624 * should guarantee "exactly once" RPC semantics.
2626 if (error == EEXIST && nfsignore_eexist != 0 && (!NFSHASNFSV4(nmp) ||
2627 nmp->nm_minorvers == 0))
2633 * nfs remove directory call
2636 nfsrpc_rmdir(vnode_t dvp, char *name, int namelen, struct ucred *cred,
2637 NFSPROC_T *p, struct nfsvattr *dnap, int *dattrflagp, void *dstuff)
2639 struct nfsrv_descript nfsd, *nd = &nfsd;
2643 if (namelen > NFS_MAXNAMLEN)
2644 return (ENAMETOOLONG);
2645 NFSCL_REQSTART(nd, NFSPROC_RMDIR, dvp);
2646 (void) nfsm_strtom(nd, name, namelen);
2647 error = nfscl_request(nd, dvp, p, cred, dstuff);
2650 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4))
2651 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
2652 if (nd->nd_repstat && !error)
2653 error = nd->nd_repstat;
2654 mbuf_freem(nd->nd_mrep);
2656 * Kludge: Map ENOENT => 0 assuming that you have a reply to a retry.
2658 if (error == ENOENT)
2665 * Always returns with either uio_resid unchanged, if you are at the
2666 * end of the directory, or uio_resid == 0, with all DIRBLKSIZ chunks
2668 * I felt this would allow caching of directory blocks more easily
2669 * than returning a pertially filled block.
2670 * Directory offset cookies:
2671 * Oh my, what to do with them...
2672 * I can think of three ways to deal with them:
2673 * 1 - have the layer above these RPCs maintain a map between logical
2674 * directory byte offsets and the NFS directory offset cookies
2675 * 2 - pass the opaque directory offset cookies up into userland
2676 * and let the libc functions deal with them, via the system call
2677 * 3 - return them to userland in the "struct dirent", so future versions
2678 * of libc can use them and do whatever is necessary to amke things work
2679 * above these rpc calls, in the meantime
2680 * For now, I do #3 by "hiding" the directory offset cookies after the
2681 * d_name field in struct dirent. This is space inside d_reclen that
2682 * will be ignored by anything that doesn't know about them.
2683 * The directory offset cookies are filled in as the last 8 bytes of
2684 * each directory entry, after d_name. Someday, the userland libc
2685 * functions may be able to use these. In the meantime, it satisfies
2686 * OpenBSD's requirements for cookies being returned.
2687 * If expects the directory offset cookie for the read to be in uio_offset
2688 * and returns the one for the next entry after this directory block in
2692 nfsrpc_readdir(vnode_t vp, struct uio *uiop, nfsuint64 *cookiep,
2693 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp,
2694 int *eofp, void *stuff)
2697 struct dirent *dp = NULL;
2699 nfsquad_t cookie, ncookie;
2700 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
2701 struct nfsnode *dnp = VTONFS(vp);
2702 struct nfsvattr nfsva;
2703 struct nfsrv_descript nfsd, *nd = &nfsd;
2704 int error = 0, tlen, more_dirs = 1, blksiz = 0, bigenough = 1;
2705 int reqsize, tryformoredirs = 1, readsize, eof = 0, gotmnton = 0;
2706 long dotfileid, dotdotfileid = 0;
2707 u_int32_t fakefileno = 0xffffffff, rderr;
2709 nfsattrbit_t attrbits, dattrbits;
2710 u_int32_t *tl2 = NULL;
2713 KASSERT(uiop->uio_iovcnt == 1 &&
2714 (uio_uio_resid(uiop) & (DIRBLKSIZ - 1)) == 0,
2715 ("nfs readdirrpc bad uio"));
2718 * There is no point in reading a lot more than uio_resid, however
2719 * adding one additional DIRBLKSIZ makes sense. Since uio_resid
2720 * and nm_readdirsize are both exact multiples of DIRBLKSIZ, this
2721 * will never make readsize > nm_readdirsize.
2723 readsize = nmp->nm_readdirsize;
2724 if (readsize > uio_uio_resid(uiop))
2725 readsize = uio_uio_resid(uiop) + DIRBLKSIZ;
2730 tresid = uio_uio_resid(uiop);
2731 cookie.lval[0] = cookiep->nfsuquad[0];
2732 cookie.lval[1] = cookiep->nfsuquad[1];
2736 * For NFSv4, first create the "." and ".." entries.
2738 if (NFSHASNFSV4(nmp)) {
2739 reqsize = 6 * NFSX_UNSIGNED;
2740 NFSGETATTR_ATTRBIT(&dattrbits);
2741 NFSZERO_ATTRBIT(&attrbits);
2742 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_FILEID);
2743 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TYPE);
2744 if (NFSISSET_ATTRBIT(&dnp->n_vattr.na_suppattr,
2745 NFSATTRBIT_MOUNTEDONFILEID)) {
2746 NFSSETBIT_ATTRBIT(&attrbits,
2747 NFSATTRBIT_MOUNTEDONFILEID);
2751 * Must fake it. Use the fileno, except when the
2752 * fsid is != to that of the directory. For that
2753 * case, generate a fake fileno that is not the same.
2755 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_FSID);
2760 * Joy, oh joy. For V4 we get to hand craft '.' and '..'.
2762 if (uiop->uio_offset == 0) {
2763 NFSCL_REQSTART(nd, NFSPROC_LOOKUPP, vp);
2764 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2765 *tl++ = txdr_unsigned(NFSV4OP_GETFH);
2766 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2767 (void) nfsrv_putattrbit(nd, &attrbits);
2768 error = nfscl_request(nd, vp, p, cred, stuff);
2771 dotfileid = 0; /* Fake out the compiler. */
2772 if ((nd->nd_flag & ND_NOMOREDATA) == 0) {
2773 error = nfsm_loadattr(nd, &nfsva);
2776 dotfileid = nfsva.na_fileid;
2778 if (nd->nd_repstat == 0) {
2779 NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
2780 len = fxdr_unsigned(int, *(tl + 4));
2781 if (len > 0 && len <= NFSX_V4FHMAX)
2782 error = nfsm_advance(nd, NFSM_RNDUP(len), -1);
2786 NFSM_DISSECT(tl, u_int32_t *, 2*NFSX_UNSIGNED);
2787 nfsva.na_mntonfileno = 0xffffffff;
2788 error = nfsv4_loadattr(nd, NULL, &nfsva, NULL,
2789 NULL, 0, NULL, NULL, NULL, NULL, NULL, 0,
2790 NULL, NULL, NULL, p, cred);
2792 dotdotfileid = dotfileid;
2793 } else if (gotmnton) {
2794 if (nfsva.na_mntonfileno != 0xffffffff)
2795 dotdotfileid = nfsva.na_mntonfileno;
2797 dotdotfileid = nfsva.na_fileid;
2798 } else if (nfsva.na_filesid[0] ==
2799 dnp->n_vattr.na_filesid[0] &&
2800 nfsva.na_filesid[1] ==
2801 dnp->n_vattr.na_filesid[1]) {
2802 dotdotfileid = nfsva.na_fileid;
2806 } while (fakefileno ==
2808 dotdotfileid = fakefileno;
2811 } else if (nd->nd_repstat == NFSERR_NOENT) {
2813 * Lookupp returns NFSERR_NOENT when we are
2814 * at the root, so just use the current dir.
2817 dotdotfileid = dotfileid;
2819 error = nd->nd_repstat;
2821 mbuf_freem(nd->nd_mrep);
2825 dp = (struct dirent *) CAST_DOWN(caddr_t, uio_iov_base(uiop));
2826 dp->d_type = DT_DIR;
2827 dp->d_fileno = dotfileid;
2829 dp->d_name[0] = '.';
2830 dp->d_name[1] = '\0';
2831 dp->d_reclen = DIRENT_SIZE(dp) + NFSX_HYPER;
2833 * Just make these offset cookie 0.
2835 tl = (u_int32_t *)&dp->d_name[4];
2838 blksiz += dp->d_reclen;
2839 uio_uio_resid_add(uiop, -(dp->d_reclen));
2840 uiop->uio_offset += dp->d_reclen;
2841 uio_iov_base_add(uiop, dp->d_reclen);
2842 uio_iov_len_add(uiop, -(dp->d_reclen));
2843 dp = (struct dirent *) CAST_DOWN(caddr_t, uio_iov_base(uiop));
2844 dp->d_type = DT_DIR;
2845 dp->d_fileno = dotdotfileid;
2847 dp->d_name[0] = '.';
2848 dp->d_name[1] = '.';
2849 dp->d_name[2] = '\0';
2850 dp->d_reclen = DIRENT_SIZE(dp) + NFSX_HYPER;
2852 * Just make these offset cookie 0.
2854 tl = (u_int32_t *)&dp->d_name[4];
2857 blksiz += dp->d_reclen;
2858 uio_uio_resid_add(uiop, -(dp->d_reclen));
2859 uiop->uio_offset += dp->d_reclen;
2860 uio_iov_base_add(uiop, dp->d_reclen);
2861 uio_iov_len_add(uiop, -(dp->d_reclen));
2863 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_RDATTRERROR);
2865 reqsize = 5 * NFSX_UNSIGNED;
2870 * Loop around doing readdir rpc's of size readsize.
2871 * The stopping criteria is EOF or buffer full.
2873 while (more_dirs && bigenough) {
2875 NFSCL_REQSTART(nd, NFSPROC_READDIR, vp);
2876 if (nd->nd_flag & ND_NFSV2) {
2877 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2878 *tl++ = cookie.lval[1];
2879 *tl = txdr_unsigned(readsize);
2881 NFSM_BUILD(tl, u_int32_t *, reqsize);
2882 *tl++ = cookie.lval[0];
2883 *tl++ = cookie.lval[1];
2884 if (cookie.qval == 0) {
2889 *tl++ = dnp->n_cookieverf.nfsuquad[0];
2890 *tl++ = dnp->n_cookieverf.nfsuquad[1];
2893 if (nd->nd_flag & ND_NFSV4) {
2894 *tl++ = txdr_unsigned(readsize);
2895 *tl = txdr_unsigned(readsize);
2896 (void) nfsrv_putattrbit(nd, &attrbits);
2897 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2898 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2899 (void) nfsrv_putattrbit(nd, &dattrbits);
2901 *tl = txdr_unsigned(readsize);
2904 error = nfscl_request(nd, vp, p, cred, stuff);
2907 if (!(nd->nd_flag & ND_NFSV2)) {
2908 if (nd->nd_flag & ND_NFSV3)
2909 error = nfscl_postop_attr(nd, nap, attrflagp,
2911 if (!nd->nd_repstat && !error) {
2912 NFSM_DISSECT(tl, u_int32_t *, NFSX_HYPER);
2914 dnp->n_cookieverf.nfsuquad[0] = *tl++;
2915 dnp->n_cookieverf.nfsuquad[1] = *tl;
2919 if (nd->nd_repstat || error) {
2921 error = nd->nd_repstat;
2924 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2925 more_dirs = fxdr_unsigned(int, *tl);
2929 /* loop thru the dir entries, doctoring them to 4bsd form */
2930 while (more_dirs && bigenough) {
2931 if (nd->nd_flag & ND_NFSV4) {
2932 NFSM_DISSECT(tl, u_int32_t *, 3*NFSX_UNSIGNED);
2933 ncookie.lval[0] = *tl++;
2934 ncookie.lval[1] = *tl++;
2935 len = fxdr_unsigned(int, *tl);
2936 } else if (nd->nd_flag & ND_NFSV3) {
2937 NFSM_DISSECT(tl, u_int32_t *, 3*NFSX_UNSIGNED);
2938 nfsva.na_fileid = fxdr_hyper(tl);
2940 len = fxdr_unsigned(int, *tl);
2942 NFSM_DISSECT(tl, u_int32_t *, 2*NFSX_UNSIGNED);
2944 fxdr_unsigned(long, *tl++);
2945 len = fxdr_unsigned(int, *tl);
2947 if (len <= 0 || len > NFS_MAXNAMLEN) {
2951 tlen = NFSM_RNDUP(len);
2953 tlen += 4; /* To ensure null termination */
2954 left = DIRBLKSIZ - blksiz;
2955 if ((int)(tlen + DIRHDSIZ + NFSX_HYPER) > left) {
2956 dp->d_reclen += left;
2957 uio_iov_base_add(uiop, left);
2958 uio_iov_len_add(uiop, -(left));
2959 uio_uio_resid_add(uiop, -(left));
2960 uiop->uio_offset += left;
2963 if ((int)(tlen + DIRHDSIZ + NFSX_HYPER) > uio_uio_resid(uiop))
2966 dp = (struct dirent *) CAST_DOWN(caddr_t, uio_iov_base(uiop));
2968 dp->d_reclen = tlen + DIRHDSIZ + NFSX_HYPER;
2969 dp->d_type = DT_UNKNOWN;
2970 blksiz += dp->d_reclen;
2971 if (blksiz == DIRBLKSIZ)
2973 uio_uio_resid_add(uiop, -(DIRHDSIZ));
2974 uiop->uio_offset += DIRHDSIZ;
2975 uio_iov_base_add(uiop, DIRHDSIZ);
2976 uio_iov_len_add(uiop, -(DIRHDSIZ));
2977 error = nfsm_mbufuio(nd, uiop, len);
2980 cp = CAST_DOWN(caddr_t, uio_iov_base(uiop));
2982 *cp = '\0'; /* null terminate */
2983 cp += tlen; /* points to cookie storage */
2984 tl2 = (u_int32_t *)cp;
2985 uio_iov_base_add(uiop, (tlen + NFSX_HYPER));
2986 uio_iov_len_add(uiop, -(tlen + NFSX_HYPER));
2987 uio_uio_resid_add(uiop, -(tlen + NFSX_HYPER));
2988 uiop->uio_offset += (tlen + NFSX_HYPER);
2990 error = nfsm_advance(nd, NFSM_RNDUP(len), -1);
2994 if (nd->nd_flag & ND_NFSV4) {
2996 nfsva.na_mntonfileno = 0xffffffff;
2997 error = nfsv4_loadattr(nd, NULL, &nfsva, NULL,
2998 NULL, 0, NULL, NULL, NULL, NULL, NULL, 0,
2999 NULL, NULL, &rderr, p, cred);
3002 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3003 } else if (nd->nd_flag & ND_NFSV3) {
3004 NFSM_DISSECT(tl, u_int32_t *, 3*NFSX_UNSIGNED);
3005 ncookie.lval[0] = *tl++;
3006 ncookie.lval[1] = *tl++;
3008 NFSM_DISSECT(tl, u_int32_t *, 2*NFSX_UNSIGNED);
3009 ncookie.lval[0] = 0;
3010 ncookie.lval[1] = *tl++;
3013 if (nd->nd_flag & ND_NFSV4) {
3018 if (nfsva.na_mntonfileno != 0xffffffff)
3019 dp->d_fileno = nfsva.na_mntonfileno;
3021 dp->d_fileno = nfsva.na_fileid;
3022 } else if (nfsva.na_filesid[0] ==
3023 dnp->n_vattr.na_filesid[0] &&
3024 nfsva.na_filesid[1] ==
3025 dnp->n_vattr.na_filesid[1]) {
3026 dp->d_fileno = nfsva.na_fileid;
3030 } while (fakefileno ==
3032 dp->d_fileno = fakefileno;
3034 dp->d_type = vtonfs_dtype(nfsva.na_type);
3037 dp->d_fileno = nfsva.na_fileid;
3039 *tl2++ = cookiep->nfsuquad[0] = cookie.lval[0] =
3041 *tl2 = cookiep->nfsuquad[1] = cookie.lval[1] =
3044 more_dirs = fxdr_unsigned(int, *tl);
3047 * If at end of rpc data, get the eof boolean
3050 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3051 eof = fxdr_unsigned(int, *tl);
3054 if (nd->nd_flag & ND_NFSV4) {
3055 error = nfscl_postop_attr(nd, nap, attrflagp,
3061 mbuf_freem(nd->nd_mrep);
3065 * Fill last record, iff any, out to a multiple of DIRBLKSIZ
3066 * by increasing d_reclen for the last record.
3069 left = DIRBLKSIZ - blksiz;
3070 dp->d_reclen += left;
3071 uio_iov_base_add(uiop, left);
3072 uio_iov_len_add(uiop, -(left));
3073 uio_uio_resid_add(uiop, -(left));
3074 uiop->uio_offset += left;
3078 * If returning no data, assume end of file.
3079 * If not bigenough, return not end of file, since you aren't
3080 * returning all the data
3081 * Otherwise, return the eof flag from the server.
3084 if (tresid == ((size_t)(uio_uio_resid(uiop))))
3086 else if (!bigenough)
3093 * Add extra empty records to any remaining DIRBLKSIZ chunks.
3095 while (uio_uio_resid(uiop) > 0 && ((size_t)(uio_uio_resid(uiop))) != tresid) {
3096 dp = (struct dirent *) CAST_DOWN(caddr_t, uio_iov_base(uiop));
3097 dp->d_type = DT_UNKNOWN;
3100 dp->d_name[0] = '\0';
3101 tl = (u_int32_t *)&dp->d_name[4];
3102 *tl++ = cookie.lval[0];
3103 *tl = cookie.lval[1];
3104 dp->d_reclen = DIRBLKSIZ;
3105 uio_iov_base_add(uiop, DIRBLKSIZ);
3106 uio_iov_len_add(uiop, -(DIRBLKSIZ));
3107 uio_uio_resid_add(uiop, -(DIRBLKSIZ));
3108 uiop->uio_offset += DIRBLKSIZ;
3112 if (nd->nd_mrep != NULL)
3113 mbuf_freem(nd->nd_mrep);
3119 * NFS V3 readdir plus RPC. Used in place of nfsrpc_readdir().
3120 * (Also used for NFS V4 when mount flag set.)
3121 * (ditto above w.r.t. multiple of DIRBLKSIZ, etc.)
3124 nfsrpc_readdirplus(vnode_t vp, struct uio *uiop, nfsuint64 *cookiep,
3125 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp,
3126 int *eofp, void *stuff)
3129 struct dirent *dp = NULL;
3131 vnode_t newvp = NULLVP;
3132 struct nfsrv_descript nfsd, *nd = &nfsd;
3133 struct nameidata nami, *ndp = &nami;
3134 struct componentname *cnp = &ndp->ni_cnd;
3135 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
3136 struct nfsnode *dnp = VTONFS(vp), *np;
3137 struct nfsvattr nfsva;
3139 nfsquad_t cookie, ncookie;
3140 int error = 0, tlen, more_dirs = 1, blksiz = 0, bigenough = 1;
3141 int attrflag, tryformoredirs = 1, eof = 0, gotmnton = 0;
3142 int isdotdot = 0, unlocknewvp = 0;
3143 long dotfileid, dotdotfileid = 0, fileno = 0;
3145 nfsattrbit_t attrbits, dattrbits;
3147 u_int32_t *tl2 = NULL, fakefileno = 0xffffffff, rderr;
3148 struct timespec dctime;
3150 KASSERT(uiop->uio_iovcnt == 1 &&
3151 (uio_uio_resid(uiop) & (DIRBLKSIZ - 1)) == 0,
3152 ("nfs readdirplusrpc bad uio"));
3153 timespecclear(&dctime);
3159 cookie.lval[0] = cookiep->nfsuquad[0];
3160 cookie.lval[1] = cookiep->nfsuquad[1];
3161 tresid = uio_uio_resid(uiop);
3164 * For NFSv4, first create the "." and ".." entries.
3166 if (NFSHASNFSV4(nmp)) {
3167 NFSGETATTR_ATTRBIT(&dattrbits);
3168 NFSZERO_ATTRBIT(&attrbits);
3169 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_FILEID);
3170 if (NFSISSET_ATTRBIT(&dnp->n_vattr.na_suppattr,
3171 NFSATTRBIT_MOUNTEDONFILEID)) {
3172 NFSSETBIT_ATTRBIT(&attrbits,
3173 NFSATTRBIT_MOUNTEDONFILEID);
3177 * Must fake it. Use the fileno, except when the
3178 * fsid is != to that of the directory. For that
3179 * case, generate a fake fileno that is not the same.
3181 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_FSID);
3186 * Joy, oh joy. For V4 we get to hand craft '.' and '..'.
3188 if (uiop->uio_offset == 0) {
3189 NFSCL_REQSTART(nd, NFSPROC_LOOKUPP, vp);
3190 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
3191 *tl++ = txdr_unsigned(NFSV4OP_GETFH);
3192 *tl = txdr_unsigned(NFSV4OP_GETATTR);
3193 (void) nfsrv_putattrbit(nd, &attrbits);
3194 error = nfscl_request(nd, vp, p, cred, stuff);
3197 dotfileid = 0; /* Fake out the compiler. */
3198 if ((nd->nd_flag & ND_NOMOREDATA) == 0) {
3199 error = nfsm_loadattr(nd, &nfsva);
3202 dctime = nfsva.na_ctime;
3203 dotfileid = nfsva.na_fileid;
3205 if (nd->nd_repstat == 0) {
3206 NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
3207 len = fxdr_unsigned(int, *(tl + 4));
3208 if (len > 0 && len <= NFSX_V4FHMAX)
3209 error = nfsm_advance(nd, NFSM_RNDUP(len), -1);
3213 NFSM_DISSECT(tl, u_int32_t *, 2*NFSX_UNSIGNED);
3214 nfsva.na_mntonfileno = 0xffffffff;
3215 error = nfsv4_loadattr(nd, NULL, &nfsva, NULL,
3216 NULL, 0, NULL, NULL, NULL, NULL, NULL, 0,
3217 NULL, NULL, NULL, p, cred);
3219 dotdotfileid = dotfileid;
3220 } else if (gotmnton) {
3221 if (nfsva.na_mntonfileno != 0xffffffff)
3222 dotdotfileid = nfsva.na_mntonfileno;
3224 dotdotfileid = nfsva.na_fileid;
3225 } else if (nfsva.na_filesid[0] ==
3226 dnp->n_vattr.na_filesid[0] &&
3227 nfsva.na_filesid[1] ==
3228 dnp->n_vattr.na_filesid[1]) {
3229 dotdotfileid = nfsva.na_fileid;
3233 } while (fakefileno ==
3235 dotdotfileid = fakefileno;
3238 } else if (nd->nd_repstat == NFSERR_NOENT) {
3240 * Lookupp returns NFSERR_NOENT when we are
3241 * at the root, so just use the current dir.
3244 dotdotfileid = dotfileid;
3246 error = nd->nd_repstat;
3248 mbuf_freem(nd->nd_mrep);
3252 dp = (struct dirent *)uio_iov_base(uiop);
3253 dp->d_type = DT_DIR;
3254 dp->d_fileno = dotfileid;
3256 dp->d_name[0] = '.';
3257 dp->d_name[1] = '\0';
3258 dp->d_reclen = DIRENT_SIZE(dp) + NFSX_HYPER;
3260 * Just make these offset cookie 0.
3262 tl = (u_int32_t *)&dp->d_name[4];
3265 blksiz += dp->d_reclen;
3266 uio_uio_resid_add(uiop, -(dp->d_reclen));
3267 uiop->uio_offset += dp->d_reclen;
3268 uio_iov_base_add(uiop, dp->d_reclen);
3269 uio_iov_len_add(uiop, -(dp->d_reclen));
3270 dp = (struct dirent *)uio_iov_base(uiop);
3271 dp->d_type = DT_DIR;
3272 dp->d_fileno = dotdotfileid;
3274 dp->d_name[0] = '.';
3275 dp->d_name[1] = '.';
3276 dp->d_name[2] = '\0';
3277 dp->d_reclen = DIRENT_SIZE(dp) + NFSX_HYPER;
3279 * Just make these offset cookie 0.
3281 tl = (u_int32_t *)&dp->d_name[4];
3284 blksiz += dp->d_reclen;
3285 uio_uio_resid_add(uiop, -(dp->d_reclen));
3286 uiop->uio_offset += dp->d_reclen;
3287 uio_iov_base_add(uiop, dp->d_reclen);
3288 uio_iov_len_add(uiop, -(dp->d_reclen));
3290 NFSREADDIRPLUS_ATTRBIT(&attrbits);
3292 NFSSETBIT_ATTRBIT(&attrbits,
3293 NFSATTRBIT_MOUNTEDONFILEID);
3297 * Loop around doing readdir rpc's of size nm_readdirsize.
3298 * The stopping criteria is EOF or buffer full.
3300 while (more_dirs && bigenough) {
3302 NFSCL_REQSTART(nd, NFSPROC_READDIRPLUS, vp);
3303 NFSM_BUILD(tl, u_int32_t *, 6 * NFSX_UNSIGNED);
3304 *tl++ = cookie.lval[0];
3305 *tl++ = cookie.lval[1];
3306 if (cookie.qval == 0) {
3311 *tl++ = dnp->n_cookieverf.nfsuquad[0];
3312 *tl++ = dnp->n_cookieverf.nfsuquad[1];
3315 *tl++ = txdr_unsigned(nmp->nm_readdirsize);
3316 *tl = txdr_unsigned(nmp->nm_readdirsize);
3317 if (nd->nd_flag & ND_NFSV4) {
3318 (void) nfsrv_putattrbit(nd, &attrbits);
3319 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
3320 *tl = txdr_unsigned(NFSV4OP_GETATTR);
3321 (void) nfsrv_putattrbit(nd, &dattrbits);
3323 error = nfscl_request(nd, vp, p, cred, stuff);
3326 if (nd->nd_flag & ND_NFSV3)
3327 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
3328 if (nd->nd_repstat || error) {
3330 error = nd->nd_repstat;
3333 if ((nd->nd_flag & ND_NFSV3) != 0 && *attrflagp != 0)
3334 dctime = nap->na_ctime;
3335 NFSM_DISSECT(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
3337 dnp->n_cookieverf.nfsuquad[0] = *tl++;
3338 dnp->n_cookieverf.nfsuquad[1] = *tl++;
3340 more_dirs = fxdr_unsigned(int, *tl);
3344 /* loop thru the dir entries, doctoring them to 4bsd form */
3345 while (more_dirs && bigenough) {
3346 NFSM_DISSECT(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
3347 if (nd->nd_flag & ND_NFSV4) {
3348 ncookie.lval[0] = *tl++;
3349 ncookie.lval[1] = *tl++;
3351 fileno = fxdr_unsigned(long, *++tl);
3354 len = fxdr_unsigned(int, *tl);
3355 if (len <= 0 || len > NFS_MAXNAMLEN) {
3359 tlen = NFSM_RNDUP(len);
3361 tlen += 4; /* To ensure null termination */
3362 left = DIRBLKSIZ - blksiz;
3363 if ((tlen + DIRHDSIZ + NFSX_HYPER) > left) {
3364 dp->d_reclen += left;
3365 uio_iov_base_add(uiop, left);
3366 uio_iov_len_add(uiop, -(left));
3367 uio_uio_resid_add(uiop, -(left));
3368 uiop->uio_offset += left;
3371 if ((tlen + DIRHDSIZ + NFSX_HYPER) > uio_uio_resid(uiop))
3374 dp = (struct dirent *)uio_iov_base(uiop);
3376 dp->d_reclen = tlen + DIRHDSIZ + NFSX_HYPER;
3377 dp->d_type = DT_UNKNOWN;
3378 blksiz += dp->d_reclen;
3379 if (blksiz == DIRBLKSIZ)
3381 uio_uio_resid_add(uiop, -(DIRHDSIZ));
3382 uiop->uio_offset += DIRHDSIZ;
3383 uio_iov_base_add(uiop, DIRHDSIZ);
3384 uio_iov_len_add(uiop, -(DIRHDSIZ));
3385 cnp->cn_nameptr = uio_iov_base(uiop);
3386 cnp->cn_namelen = len;
3388 error = nfsm_mbufuio(nd, uiop, len);
3391 cp = uio_iov_base(uiop);
3394 cp += tlen; /* points to cookie storage */
3395 tl2 = (u_int32_t *)cp;
3396 if (len == 2 && cnp->cn_nameptr[0] == '.' &&
3397 cnp->cn_nameptr[1] == '.')
3401 uio_iov_base_add(uiop, (tlen + NFSX_HYPER));
3402 uio_iov_len_add(uiop, -(tlen + NFSX_HYPER));
3403 uio_uio_resid_add(uiop, -(tlen + NFSX_HYPER));
3404 uiop->uio_offset += (tlen + NFSX_HYPER);
3406 error = nfsm_advance(nd, NFSM_RNDUP(len), -1);
3411 if (nd->nd_flag & ND_NFSV3) {
3412 NFSM_DISSECT(tl, u_int32_t *, 3*NFSX_UNSIGNED);
3413 ncookie.lval[0] = *tl++;
3414 ncookie.lval[1] = *tl++;
3415 attrflag = fxdr_unsigned(int, *tl);
3417 error = nfsm_loadattr(nd, &nfsva);
3421 NFSM_DISSECT(tl,u_int32_t *,NFSX_UNSIGNED);
3423 error = nfsm_getfh(nd, &nfhp);
3427 if (!attrflag && nfhp != NULL) {
3428 FREE((caddr_t)nfhp, M_NFSFH);
3433 nfsva.na_mntonfileno = 0xffffffff;
3434 error = nfsv4_loadattr(nd, NULL, &nfsva, &nfhp,
3435 NULL, 0, NULL, NULL, NULL, NULL, NULL, 0,
3436 NULL, NULL, &rderr, p, cred);
3442 if (nd->nd_flag & ND_NFSV4) {
3445 } else if (gotmnton) {
3446 if (nfsva.na_mntonfileno != 0xffffffff)
3447 dp->d_fileno = nfsva.na_mntonfileno;
3449 dp->d_fileno = nfsva.na_fileid;
3450 } else if (nfsva.na_filesid[0] ==
3451 dnp->n_vattr.na_filesid[0] &&
3452 nfsva.na_filesid[1] ==
3453 dnp->n_vattr.na_filesid[1]) {
3454 dp->d_fileno = nfsva.na_fileid;
3458 } while (fakefileno ==
3460 dp->d_fileno = fakefileno;
3463 dp->d_fileno = fileno;
3465 *tl2++ = cookiep->nfsuquad[0] = cookie.lval[0] =
3467 *tl2 = cookiep->nfsuquad[1] = cookie.lval[1] =
3471 if (NFSRV_CMPFH(nfhp->nfh_fh, nfhp->nfh_len,
3472 dnp->n_fhp->nfh_fh, dnp->n_fhp->nfh_len)) {
3476 FREE((caddr_t)nfhp, M_NFSFH);
3478 } else if (isdotdot != 0) {
3480 * Skip doing a nfscl_nget() call for "..".
3481 * There's a race between acquiring the nfs
3482 * node here and lookups that look for the
3483 * directory being read (in the parent).
3484 * It would try to get a lock on ".." here,
3485 * owning the lock on the directory being
3486 * read. Lookup will hold the lock on ".."
3487 * and try to acquire the lock on the
3488 * directory being read.
3489 * If the directory is unlocked/relocked,
3490 * then there is a LOR with the buflock
3493 free(nfhp, M_NFSFH);
3495 error = nfscl_nget(vnode_mount(vp), vp,
3496 nfhp, cnp, p, &np, NULL, LK_EXCLUSIVE);
3503 if (newvp != NULLVP) {
3504 error = nfscl_loadattrcache(&newvp,
3505 &nfsva, NULL, NULL, 0, 0);
3514 vtonfs_dtype(np->n_vattr.na_type);
3516 NFSCNHASH(cnp, HASHINIT);
3517 if (cnp->cn_namelen <= NCHNAMLEN &&
3518 (newvp->v_type != VDIR ||
3519 dctime.tv_sec != 0)) {
3520 cache_enter_time(ndp->ni_dvp,
3523 newvp->v_type != VDIR ? NULL :
3533 } else if (nfhp != NULL) {
3534 FREE((caddr_t)nfhp, M_NFSFH);
3536 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3537 more_dirs = fxdr_unsigned(int, *tl);
3540 * If at end of rpc data, get the eof boolean
3543 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3544 eof = fxdr_unsigned(int, *tl);
3547 if (nd->nd_flag & ND_NFSV4) {
3548 error = nfscl_postop_attr(nd, nap, attrflagp,
3554 mbuf_freem(nd->nd_mrep);
3558 * Fill last record, iff any, out to a multiple of DIRBLKSIZ
3559 * by increasing d_reclen for the last record.
3562 left = DIRBLKSIZ - blksiz;
3563 dp->d_reclen += left;
3564 uio_iov_base_add(uiop, left);
3565 uio_iov_len_add(uiop, -(left));
3566 uio_uio_resid_add(uiop, -(left));
3567 uiop->uio_offset += left;
3571 * If returning no data, assume end of file.
3572 * If not bigenough, return not end of file, since you aren't
3573 * returning all the data
3574 * Otherwise, return the eof flag from the server.
3577 if (tresid == uio_uio_resid(uiop))
3579 else if (!bigenough)
3586 * Add extra empty records to any remaining DIRBLKSIZ chunks.
3588 while (uio_uio_resid(uiop) > 0 && uio_uio_resid(uiop) != tresid) {
3589 dp = (struct dirent *)uio_iov_base(uiop);
3590 dp->d_type = DT_UNKNOWN;
3593 dp->d_name[0] = '\0';
3594 tl = (u_int32_t *)&dp->d_name[4];
3595 *tl++ = cookie.lval[0];
3596 *tl = cookie.lval[1];
3597 dp->d_reclen = DIRBLKSIZ;
3598 uio_iov_base_add(uiop, DIRBLKSIZ);
3599 uio_iov_len_add(uiop, -(DIRBLKSIZ));
3600 uio_uio_resid_add(uiop, -(DIRBLKSIZ));
3601 uiop->uio_offset += DIRBLKSIZ;
3605 if (nd->nd_mrep != NULL)
3606 mbuf_freem(nd->nd_mrep);
3615 nfsrpc_commit(vnode_t vp, u_quad_t offset, int cnt, struct ucred *cred,
3616 NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, void *stuff)
3619 struct nfsrv_descript nfsd, *nd = &nfsd;
3620 nfsattrbit_t attrbits;
3622 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
3625 NFSCL_REQSTART(nd, NFSPROC_COMMIT, vp);
3626 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
3627 txdr_hyper(offset, tl);
3629 *tl = txdr_unsigned(cnt);
3630 if (nd->nd_flag & ND_NFSV4) {
3632 * And do a Getattr op.
3634 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
3635 *tl = txdr_unsigned(NFSV4OP_GETATTR);
3636 NFSGETATTR_ATTRBIT(&attrbits);
3637 (void) nfsrv_putattrbit(nd, &attrbits);
3639 error = nfscl_request(nd, vp, p, cred, stuff);
3642 error = nfscl_wcc_data(nd, vp, nap, attrflagp, NULL, stuff);
3643 if (!error && !nd->nd_repstat) {
3644 NFSM_DISSECT(tl, u_int32_t *, NFSX_VERF);
3646 if (NFSBCMP(nmp->nm_verf, tl, NFSX_VERF)) {
3647 NFSBCOPY(tl, nmp->nm_verf, NFSX_VERF);
3648 nd->nd_repstat = NFSERR_STALEWRITEVERF;
3651 if (nd->nd_flag & ND_NFSV4)
3652 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
3655 if (!error && nd->nd_repstat)
3656 error = nd->nd_repstat;
3657 mbuf_freem(nd->nd_mrep);
3662 * NFS byte range lock rpc.
3663 * (Mostly just calls one of the three lower level RPC routines.)
3666 nfsrpc_advlock(vnode_t vp, off_t size, int op, struct flock *fl,
3667 int reclaim, struct ucred *cred, NFSPROC_T *p, void *id, int flags)
3669 struct nfscllockowner *lp;
3670 struct nfsclclient *clp;
3672 struct nfsrv_descript nfsd, *nd = &nfsd;
3673 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
3676 u_int32_t clidrev = 0;
3677 int error = 0, newone = 0, expireret = 0, retrycnt, donelocally;
3681 * Convert the flock structure into a start and end and do POSIX
3684 switch (fl->l_whence) {
3688 * Caller is responsible for adding any necessary offset
3689 * when SEEK_CUR is used.
3691 start = fl->l_start;
3695 start = size + fl->l_start;
3696 off = size + fl->l_start;
3703 if (fl->l_len != 0) {
3704 end = start + fl->l_len - 1;
3715 if (op == F_GETLK) {
3716 error = nfscl_getcl(vnode_mount(vp), cred, p, 1, &clp);
3719 error = nfscl_lockt(vp, clp, off, len, fl, p, id, flags);
3721 clidrev = clp->nfsc_clientidrev;
3722 error = nfsrpc_lockt(nd, vp, clp, off, len, fl, cred,
3724 } else if (error == -1) {
3727 nfscl_clientrelease(clp);
3728 } else if (op == F_UNLCK && fl->l_type == F_UNLCK) {
3730 * We must loop around for all lockowner cases.
3733 error = nfscl_getcl(vnode_mount(vp), cred, p, 1, &clp);
3737 error = nfscl_relbytelock(vp, off, len, cred, p, callcnt,
3738 clp, id, flags, &lp, &dorpc);
3740 * If it returns a NULL lp, we're done.
3744 nfscl_clientrelease(clp);
3746 nfscl_releasealllocks(clp, vp, p, id, flags);
3749 if (nmp->nm_clp != NULL)
3750 clidrev = nmp->nm_clp->nfsc_clientidrev;
3754 * If the server doesn't support Posix lock semantics,
3755 * only allow locks on the entire file, since it won't
3756 * handle overlapping byte ranges.
3757 * There might still be a problem when a lock
3758 * upgrade/downgrade (read<->write) occurs, since the
3759 * server "might" expect an unlock first?
3761 if (dorpc && (lp->nfsl_open->nfso_posixlock ||
3762 (off == 0 && len == NFS64BITSSET))) {
3764 * Since the lock records will go away, we must
3765 * wait for grace and delay here.
3768 error = nfsrpc_locku(nd, nmp, lp, off, len,
3769 NFSV4LOCKT_READ, cred, p, 0);
3770 if ((nd->nd_repstat == NFSERR_GRACE ||
3771 nd->nd_repstat == NFSERR_DELAY) &&
3773 (void) nfs_catnap(PZERO, (int)nd->nd_repstat,
3775 } while ((nd->nd_repstat == NFSERR_GRACE ||
3776 nd->nd_repstat == NFSERR_DELAY) && error == 0);
3779 } while (error == 0 && nd->nd_repstat == 0);
3780 nfscl_releasealllocks(clp, vp, p, id, flags);
3781 } else if (op == F_SETLK) {
3782 error = nfscl_getbytelock(vp, off, len, fl->l_type, cred, p,
3783 NULL, 0, id, flags, NULL, NULL, &lp, &newone, &donelocally);
3784 if (error || donelocally) {
3787 if (nmp->nm_clp != NULL)
3788 clidrev = nmp->nm_clp->nfsc_clientidrev;
3791 nfhp = VTONFS(vp)->n_fhp;
3792 if (!lp->nfsl_open->nfso_posixlock &&
3793 (off != 0 || len != NFS64BITSSET)) {
3796 error = nfsrpc_lock(nd, nmp, vp, nfhp->nfh_fh,
3797 nfhp->nfh_len, lp, newone, reclaim, off,
3798 len, fl->l_type, cred, p, 0);
3801 error = nd->nd_repstat;
3802 nfscl_lockrelease(lp, error, newone);
3807 error = nd->nd_repstat;
3808 if (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
3809 error == NFSERR_STALEDONTRECOVER ||
3810 error == NFSERR_STALECLIENTID || error == NFSERR_DELAY ||
3811 error == NFSERR_BADSESSION) {
3812 (void) nfs_catnap(PZERO, error, "nfs_advlock");
3813 } else if ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID)
3815 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
3818 } while (error == NFSERR_GRACE ||
3819 error == NFSERR_STALECLIENTID || error == NFSERR_DELAY ||
3820 error == NFSERR_STALEDONTRECOVER || error == NFSERR_STALESTATEID ||
3821 error == NFSERR_BADSESSION ||
3822 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
3823 expireret == 0 && clidrev != 0 && retrycnt < 4));
3824 if (error && retrycnt >= 4)
3830 * The lower level routine for the LockT case.
3833 nfsrpc_lockt(struct nfsrv_descript *nd, vnode_t vp,
3834 struct nfsclclient *clp, u_int64_t off, u_int64_t len, struct flock *fl,
3835 struct ucred *cred, NFSPROC_T *p, void *id, int flags)
3838 int error, type, size;
3839 uint8_t own[NFSV4CL_LOCKNAMELEN + NFSX_V4FHMAX];
3841 struct nfsmount *nmp;
3843 nmp = VFSTONFS(vp->v_mount);
3844 NFSCL_REQSTART(nd, NFSPROC_LOCKT, vp);
3845 NFSM_BUILD(tl, u_int32_t *, 7 * NFSX_UNSIGNED);
3846 if (fl->l_type == F_RDLCK)
3847 *tl++ = txdr_unsigned(NFSV4LOCKT_READ);
3849 *tl++ = txdr_unsigned(NFSV4LOCKT_WRITE);
3850 txdr_hyper(off, tl);
3852 txdr_hyper(len, tl);
3854 *tl++ = NFSMNT_MDSSESSION(nmp)->nfsess_clientid.lval[0];
3855 *tl = NFSMNT_MDSSESSION(nmp)->nfsess_clientid.lval[1];
3856 nfscl_filllockowner(id, own, flags);
3858 NFSBCOPY(np->n_fhp->nfh_fh, &own[NFSV4CL_LOCKNAMELEN],
3859 np->n_fhp->nfh_len);
3860 (void)nfsm_strtom(nd, own, NFSV4CL_LOCKNAMELEN + np->n_fhp->nfh_len);
3861 error = nfscl_request(nd, vp, p, cred, NULL);
3864 if (nd->nd_repstat == 0) {
3865 fl->l_type = F_UNLCK;
3866 } else if (nd->nd_repstat == NFSERR_DENIED) {
3868 fl->l_whence = SEEK_SET;
3869 NFSM_DISSECT(tl, u_int32_t *, 8 * NFSX_UNSIGNED);
3870 fl->l_start = fxdr_hyper(tl);
3872 len = fxdr_hyper(tl);
3874 if (len == NFS64BITSSET)
3878 type = fxdr_unsigned(int, *tl++);
3879 if (type == NFSV4LOCKT_WRITE)
3880 fl->l_type = F_WRLCK;
3882 fl->l_type = F_RDLCK;
3884 * XXX For now, I have no idea what to do with the
3885 * conflicting lock_owner, so I'll just set the pid == 0
3886 * and skip over the lock_owner.
3888 fl->l_pid = (pid_t)0;
3890 size = fxdr_unsigned(int, *tl);
3891 if (size < 0 || size > NFSV4_OPAQUELIMIT)
3894 error = nfsm_advance(nd, NFSM_RNDUP(size), -1);
3895 } else if (nd->nd_repstat == NFSERR_STALECLIENTID ||
3896 nd->nd_repstat == NFSERR_BADSESSION)
3897 nfscl_initiate_recovery(clp);
3899 mbuf_freem(nd->nd_mrep);
3904 * Lower level function that performs the LockU RPC.
3907 nfsrpc_locku(struct nfsrv_descript *nd, struct nfsmount *nmp,
3908 struct nfscllockowner *lp, u_int64_t off, u_int64_t len,
3909 u_int32_t type, struct ucred *cred, NFSPROC_T *p, int syscred)
3914 nfscl_reqstart(nd, NFSPROC_LOCKU, nmp, lp->nfsl_open->nfso_fh,
3915 lp->nfsl_open->nfso_fhlen, NULL, NULL);
3916 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID + 6 * NFSX_UNSIGNED);
3917 *tl++ = txdr_unsigned(type);
3918 *tl = txdr_unsigned(lp->nfsl_seqid);
3919 if (nfstest_outofseq &&
3920 (arc4random() % nfstest_outofseq) == 0)
3921 *tl = txdr_unsigned(lp->nfsl_seqid + 1);
3923 if (NFSHASNFSV4N(nmp))
3926 *tl++ = lp->nfsl_stateid.seqid;
3927 *tl++ = lp->nfsl_stateid.other[0];
3928 *tl++ = lp->nfsl_stateid.other[1];
3929 *tl++ = lp->nfsl_stateid.other[2];
3930 txdr_hyper(off, tl);
3932 txdr_hyper(len, tl);
3934 nd->nd_flag |= ND_USEGSSNAME;
3935 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
3936 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
3937 NFSCL_INCRSEQID(lp->nfsl_seqid, nd);
3940 if (nd->nd_repstat == 0) {
3941 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID);
3942 lp->nfsl_stateid.seqid = *tl++;
3943 lp->nfsl_stateid.other[0] = *tl++;
3944 lp->nfsl_stateid.other[1] = *tl++;
3945 lp->nfsl_stateid.other[2] = *tl;
3946 } else if (nd->nd_repstat == NFSERR_STALESTATEID ||
3947 nd->nd_repstat == NFSERR_BADSESSION)
3948 nfscl_initiate_recovery(lp->nfsl_open->nfso_own->nfsow_clp);
3950 mbuf_freem(nd->nd_mrep);
3955 * The actual Lock RPC.
3958 nfsrpc_lock(struct nfsrv_descript *nd, struct nfsmount *nmp, vnode_t vp,
3959 u_int8_t *nfhp, int fhlen, struct nfscllockowner *lp, int newone,
3960 int reclaim, u_int64_t off, u_int64_t len, short type, struct ucred *cred,
3961 NFSPROC_T *p, int syscred)
3965 uint8_t own[NFSV4CL_LOCKNAMELEN + NFSX_V4FHMAX];
3967 nfscl_reqstart(nd, NFSPROC_LOCK, nmp, nfhp, fhlen, NULL, NULL);
3968 NFSM_BUILD(tl, u_int32_t *, 7 * NFSX_UNSIGNED);
3969 if (type == F_RDLCK)
3970 *tl++ = txdr_unsigned(NFSV4LOCKT_READ);
3972 *tl++ = txdr_unsigned(NFSV4LOCKT_WRITE);
3973 *tl++ = txdr_unsigned(reclaim);
3974 txdr_hyper(off, tl);
3976 txdr_hyper(len, tl);
3980 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID +
3981 2 * NFSX_UNSIGNED + NFSX_HYPER);
3982 *tl++ = txdr_unsigned(lp->nfsl_open->nfso_own->nfsow_seqid);
3983 if (NFSHASNFSV4N(nmp))
3986 *tl++ = lp->nfsl_open->nfso_stateid.seqid;
3987 *tl++ = lp->nfsl_open->nfso_stateid.other[0];
3988 *tl++ = lp->nfsl_open->nfso_stateid.other[1];
3989 *tl++ = lp->nfsl_open->nfso_stateid.other[2];
3990 *tl++ = txdr_unsigned(lp->nfsl_seqid);
3991 *tl++ = NFSMNT_MDSSESSION(nmp)->nfsess_clientid.lval[0];
3992 *tl = NFSMNT_MDSSESSION(nmp)->nfsess_clientid.lval[1];
3993 NFSBCOPY(lp->nfsl_owner, own, NFSV4CL_LOCKNAMELEN);
3994 NFSBCOPY(nfhp, &own[NFSV4CL_LOCKNAMELEN], fhlen);
3995 (void)nfsm_strtom(nd, own, NFSV4CL_LOCKNAMELEN + fhlen);
3998 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID + NFSX_UNSIGNED);
3999 if (NFSHASNFSV4N(nmp))
4002 *tl++ = lp->nfsl_stateid.seqid;
4003 *tl++ = lp->nfsl_stateid.other[0];
4004 *tl++ = lp->nfsl_stateid.other[1];
4005 *tl++ = lp->nfsl_stateid.other[2];
4006 *tl = txdr_unsigned(lp->nfsl_seqid);
4007 if (nfstest_outofseq &&
4008 (arc4random() % nfstest_outofseq) == 0)
4009 *tl = txdr_unsigned(lp->nfsl_seqid + 1);
4012 nd->nd_flag |= ND_USEGSSNAME;
4013 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, vp, p, cred,
4014 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4018 NFSCL_INCRSEQID(lp->nfsl_open->nfso_own->nfsow_seqid, nd);
4019 NFSCL_INCRSEQID(lp->nfsl_seqid, nd);
4020 if (nd->nd_repstat == 0) {
4021 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID);
4022 lp->nfsl_stateid.seqid = *tl++;
4023 lp->nfsl_stateid.other[0] = *tl++;
4024 lp->nfsl_stateid.other[1] = *tl++;
4025 lp->nfsl_stateid.other[2] = *tl;
4026 } else if (nd->nd_repstat == NFSERR_DENIED) {
4027 NFSM_DISSECT(tl, u_int32_t *, 8 * NFSX_UNSIGNED);
4028 size = fxdr_unsigned(int, *(tl + 7));
4029 if (size < 0 || size > NFSV4_OPAQUELIMIT)
4032 error = nfsm_advance(nd, NFSM_RNDUP(size), -1);
4033 } else if (nd->nd_repstat == NFSERR_STALESTATEID ||
4034 nd->nd_repstat == NFSERR_BADSESSION)
4035 nfscl_initiate_recovery(lp->nfsl_open->nfso_own->nfsow_clp);
4037 mbuf_freem(nd->nd_mrep);
4043 * (always called with the vp for the mount point)
4046 nfsrpc_statfs(vnode_t vp, struct nfsstatfs *sbp, struct nfsfsinfo *fsp,
4047 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp,
4050 u_int32_t *tl = NULL;
4051 struct nfsrv_descript nfsd, *nd = &nfsd;
4052 struct nfsmount *nmp;
4053 nfsattrbit_t attrbits;
4057 nmp = VFSTONFS(vnode_mount(vp));
4058 if (NFSHASNFSV4(nmp)) {
4060 * For V4, you actually do a getattr.
4062 NFSCL_REQSTART(nd, NFSPROC_GETATTR, vp);
4063 NFSSTATFS_GETATTRBIT(&attrbits);
4064 (void) nfsrv_putattrbit(nd, &attrbits);
4065 nd->nd_flag |= ND_USEGSSNAME;
4066 error = nfscl_request(nd, vp, p, cred, stuff);
4069 if (nd->nd_repstat == 0) {
4070 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0,
4071 NULL, NULL, sbp, fsp, NULL, 0, NULL, NULL, NULL, p,
4074 nmp->nm_fsid[0] = nap->na_filesid[0];
4075 nmp->nm_fsid[1] = nap->na_filesid[1];
4076 NFSSETHASSETFSID(nmp);
4080 error = nd->nd_repstat;
4085 NFSCL_REQSTART(nd, NFSPROC_FSSTAT, vp);
4086 error = nfscl_request(nd, vp, p, cred, stuff);
4089 if (nd->nd_flag & ND_NFSV3) {
4090 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
4094 if (nd->nd_repstat) {
4095 error = nd->nd_repstat;
4098 NFSM_DISSECT(tl, u_int32_t *,
4099 NFSX_STATFS(nd->nd_flag & ND_NFSV3));
4101 if (NFSHASNFSV3(nmp)) {
4102 sbp->sf_tbytes = fxdr_hyper(tl); tl += 2;
4103 sbp->sf_fbytes = fxdr_hyper(tl); tl += 2;
4104 sbp->sf_abytes = fxdr_hyper(tl); tl += 2;
4105 sbp->sf_tfiles = fxdr_hyper(tl); tl += 2;
4106 sbp->sf_ffiles = fxdr_hyper(tl); tl += 2;
4107 sbp->sf_afiles = fxdr_hyper(tl); tl += 2;
4108 sbp->sf_invarsec = fxdr_unsigned(u_int32_t, *tl);
4109 } else if (NFSHASNFSV4(nmp) == 0) {
4110 sbp->sf_tsize = fxdr_unsigned(u_int32_t, *tl++);
4111 sbp->sf_bsize = fxdr_unsigned(u_int32_t, *tl++);
4112 sbp->sf_blocks = fxdr_unsigned(u_int32_t, *tl++);
4113 sbp->sf_bfree = fxdr_unsigned(u_int32_t, *tl++);
4114 sbp->sf_bavail = fxdr_unsigned(u_int32_t, *tl);
4117 mbuf_freem(nd->nd_mrep);
4125 nfsrpc_pathconf(vnode_t vp, struct nfsv3_pathconf *pc,
4126 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp,
4129 struct nfsrv_descript nfsd, *nd = &nfsd;
4130 struct nfsmount *nmp;
4132 nfsattrbit_t attrbits;
4136 nmp = VFSTONFS(vnode_mount(vp));
4137 if (NFSHASNFSV4(nmp)) {
4139 * For V4, you actually do a getattr.
4141 NFSCL_REQSTART(nd, NFSPROC_GETATTR, vp);
4142 NFSPATHCONF_GETATTRBIT(&attrbits);
4143 (void) nfsrv_putattrbit(nd, &attrbits);
4144 nd->nd_flag |= ND_USEGSSNAME;
4145 error = nfscl_request(nd, vp, p, cred, stuff);
4148 if (nd->nd_repstat == 0) {
4149 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0,
4150 pc, NULL, NULL, NULL, NULL, 0, NULL, NULL, NULL, p,
4155 error = nd->nd_repstat;
4158 NFSCL_REQSTART(nd, NFSPROC_PATHCONF, vp);
4159 error = nfscl_request(nd, vp, p, cred, stuff);
4162 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
4163 if (nd->nd_repstat && !error)
4164 error = nd->nd_repstat;
4166 NFSM_DISSECT(tl, u_int32_t *, NFSX_V3PATHCONF);
4167 pc->pc_linkmax = fxdr_unsigned(u_int32_t, *tl++);
4168 pc->pc_namemax = fxdr_unsigned(u_int32_t, *tl++);
4169 pc->pc_notrunc = fxdr_unsigned(u_int32_t, *tl++);
4170 pc->pc_chownrestricted =
4171 fxdr_unsigned(u_int32_t, *tl++);
4172 pc->pc_caseinsensitive =
4173 fxdr_unsigned(u_int32_t, *tl++);
4174 pc->pc_casepreserving = fxdr_unsigned(u_int32_t, *tl);
4178 mbuf_freem(nd->nd_mrep);
4183 * nfs version 3 fsinfo rpc call
4186 nfsrpc_fsinfo(vnode_t vp, struct nfsfsinfo *fsp, struct ucred *cred,
4187 NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, void *stuff)
4190 struct nfsrv_descript nfsd, *nd = &nfsd;
4194 NFSCL_REQSTART(nd, NFSPROC_FSINFO, vp);
4195 error = nfscl_request(nd, vp, p, cred, stuff);
4198 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
4199 if (nd->nd_repstat && !error)
4200 error = nd->nd_repstat;
4202 NFSM_DISSECT(tl, u_int32_t *, NFSX_V3FSINFO);
4203 fsp->fs_rtmax = fxdr_unsigned(u_int32_t, *tl++);
4204 fsp->fs_rtpref = fxdr_unsigned(u_int32_t, *tl++);
4205 fsp->fs_rtmult = fxdr_unsigned(u_int32_t, *tl++);
4206 fsp->fs_wtmax = fxdr_unsigned(u_int32_t, *tl++);
4207 fsp->fs_wtpref = fxdr_unsigned(u_int32_t, *tl++);
4208 fsp->fs_wtmult = fxdr_unsigned(u_int32_t, *tl++);
4209 fsp->fs_dtpref = fxdr_unsigned(u_int32_t, *tl++);
4210 fsp->fs_maxfilesize = fxdr_hyper(tl);
4212 fxdr_nfsv3time(tl, &fsp->fs_timedelta);
4214 fsp->fs_properties = fxdr_unsigned(u_int32_t, *tl);
4217 mbuf_freem(nd->nd_mrep);
4222 * This function performs the Renew RPC.
4225 nfsrpc_renew(struct nfsclclient *clp, struct nfsclds *dsp, struct ucred *cred,
4229 struct nfsrv_descript nfsd;
4230 struct nfsrv_descript *nd = &nfsd;
4231 struct nfsmount *nmp;
4233 struct nfssockreq *nrp;
4235 nmp = clp->nfsc_nmp;
4238 nfscl_reqstart(nd, NFSPROC_RENEW, nmp, NULL, 0, NULL,
4239 &dsp->nfsclds_sess);
4240 if (!NFSHASNFSV4N(nmp)) {
4241 /* NFSv4.1 just uses a Sequence Op and not a Renew. */
4242 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
4243 *tl++ = NFSMNT_MDSSESSION(nmp)->nfsess_clientid.lval[0];
4244 *tl = NFSMNT_MDSSESSION(nmp)->nfsess_clientid.lval[1];
4246 nrp = dsp->nfsclds_sockp;
4248 /* If NULL, use the MDS socket. */
4249 nrp = &nmp->nm_sockreq;
4250 nd->nd_flag |= ND_USEGSSNAME;
4251 error = newnfs_request(nd, nmp, NULL, nrp, NULL, p, cred,
4252 NFS_PROG, NFS_VER4, NULL, 1, NULL, &dsp->nfsclds_sess);
4255 error = nd->nd_repstat;
4256 mbuf_freem(nd->nd_mrep);
4261 * This function performs the Releaselockowner RPC.
4264 nfsrpc_rellockown(struct nfsmount *nmp, struct nfscllockowner *lp,
4265 uint8_t *fh, int fhlen, struct ucred *cred, NFSPROC_T *p)
4267 struct nfsrv_descript nfsd, *nd = &nfsd;
4270 uint8_t own[NFSV4CL_LOCKNAMELEN + NFSX_V4FHMAX];
4272 if (NFSHASNFSV4N(nmp)) {
4273 /* For NFSv4.1, do a FreeStateID. */
4274 nfscl_reqstart(nd, NFSPROC_FREESTATEID, nmp, NULL, 0, NULL,
4276 nfsm_stateidtom(nd, &lp->nfsl_stateid, NFSSTATEID_PUTSTATEID);
4278 nfscl_reqstart(nd, NFSPROC_RELEASELCKOWN, nmp, NULL, 0, NULL,
4280 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
4281 *tl++ = NFSMNT_MDSSESSION(nmp)->nfsess_clientid.lval[0];
4282 *tl = NFSMNT_MDSSESSION(nmp)->nfsess_clientid.lval[1];
4283 NFSBCOPY(lp->nfsl_owner, own, NFSV4CL_LOCKNAMELEN);
4284 NFSBCOPY(fh, &own[NFSV4CL_LOCKNAMELEN], fhlen);
4285 (void)nfsm_strtom(nd, own, NFSV4CL_LOCKNAMELEN + fhlen);
4287 nd->nd_flag |= ND_USEGSSNAME;
4288 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4289 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4292 error = nd->nd_repstat;
4293 mbuf_freem(nd->nd_mrep);
4298 * This function performs the Compound to get the mount pt FH.
4301 nfsrpc_getdirpath(struct nfsmount *nmp, u_char *dirpath, struct ucred *cred,
4305 struct nfsrv_descript nfsd;
4306 struct nfsrv_descript *nd = &nfsd;
4308 int error, cnt, len, setnil;
4311 nfscl_reqstart(nd, NFSPROC_PUTROOTFH, nmp, NULL, 0, &opcntp, NULL);
4319 while (*cp2 != '\0' && *cp2 != '/')
4326 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
4327 *tl = txdr_unsigned(NFSV4OP_LOOKUP);
4328 nfsm_strtom(nd, cp, strlen(cp));
4334 } while (*cp != '\0');
4335 if (NFSHASNFSV4N(nmp))
4336 /* Has a Sequence Op done by nfscl_reqstart(). */
4337 *opcntp = txdr_unsigned(3 + cnt);
4339 *opcntp = txdr_unsigned(2 + cnt);
4340 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
4341 *tl = txdr_unsigned(NFSV4OP_GETFH);
4342 nd->nd_flag |= ND_USEGSSNAME;
4343 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4344 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4347 if (nd->nd_repstat == 0) {
4348 NFSM_DISSECT(tl, u_int32_t *, (3 + 2 * cnt) * NFSX_UNSIGNED);
4349 tl += (2 + 2 * cnt);
4350 if ((len = fxdr_unsigned(int, *tl)) <= 0 ||
4352 nd->nd_repstat = NFSERR_BADXDR;
4354 nd->nd_repstat = nfsrv_mtostr(nd, nmp->nm_fh, len);
4355 if (nd->nd_repstat == 0)
4356 nmp->nm_fhsize = len;
4359 error = nd->nd_repstat;
4361 mbuf_freem(nd->nd_mrep);
4366 * This function performs the Delegreturn RPC.
4369 nfsrpc_delegreturn(struct nfscldeleg *dp, struct ucred *cred,
4370 struct nfsmount *nmp, NFSPROC_T *p, int syscred)
4373 struct nfsrv_descript nfsd;
4374 struct nfsrv_descript *nd = &nfsd;
4377 nfscl_reqstart(nd, NFSPROC_DELEGRETURN, nmp, dp->nfsdl_fh,
4378 dp->nfsdl_fhlen, NULL, NULL);
4379 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID);
4380 if (NFSHASNFSV4N(nmp))
4383 *tl++ = dp->nfsdl_stateid.seqid;
4384 *tl++ = dp->nfsdl_stateid.other[0];
4385 *tl++ = dp->nfsdl_stateid.other[1];
4386 *tl = dp->nfsdl_stateid.other[2];
4388 nd->nd_flag |= ND_USEGSSNAME;
4389 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4390 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4393 error = nd->nd_repstat;
4394 mbuf_freem(nd->nd_mrep);
4402 nfsrpc_getacl(vnode_t vp, struct ucred *cred, NFSPROC_T *p,
4403 struct acl *aclp, void *stuff)
4405 struct nfsrv_descript nfsd, *nd = &nfsd;
4407 nfsattrbit_t attrbits;
4408 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
4410 if (nfsrv_useacl == 0 || !NFSHASNFSV4(nmp))
4411 return (EOPNOTSUPP);
4412 NFSCL_REQSTART(nd, NFSPROC_GETACL, vp);
4413 NFSZERO_ATTRBIT(&attrbits);
4414 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_ACL);
4415 (void) nfsrv_putattrbit(nd, &attrbits);
4416 error = nfscl_request(nd, vp, p, cred, stuff);
4419 if (!nd->nd_repstat)
4420 error = nfsv4_loadattr(nd, vp, NULL, NULL, NULL, 0, NULL,
4421 NULL, NULL, NULL, aclp, 0, NULL, NULL, NULL, p, cred);
4423 error = nd->nd_repstat;
4424 mbuf_freem(nd->nd_mrep);
4432 nfsrpc_setacl(vnode_t vp, struct ucred *cred, NFSPROC_T *p,
4433 struct acl *aclp, void *stuff)
4436 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
4438 if (nfsrv_useacl == 0 || !NFSHASNFSV4(nmp))
4439 return (EOPNOTSUPP);
4440 error = nfsrpc_setattr(vp, NULL, aclp, cred, p, NULL, NULL, stuff);
4448 nfsrpc_setaclrpc(vnode_t vp, struct ucred *cred, NFSPROC_T *p,
4449 struct acl *aclp, nfsv4stateid_t *stateidp, void *stuff)
4451 struct nfsrv_descript nfsd, *nd = &nfsd;
4453 nfsattrbit_t attrbits;
4454 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
4456 if (!NFSHASNFSV4(nmp))
4457 return (EOPNOTSUPP);
4458 NFSCL_REQSTART(nd, NFSPROC_SETACL, vp);
4459 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID);
4460 NFSZERO_ATTRBIT(&attrbits);
4461 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_ACL);
4462 (void) nfsv4_fillattr(nd, vnode_mount(vp), vp, aclp, NULL, NULL, 0,
4463 &attrbits, NULL, NULL, 0, 0, 0, 0, (uint64_t)0);
4464 error = nfscl_request(nd, vp, p, cred, stuff);
4467 /* Don't care about the pre/postop attributes */
4468 mbuf_freem(nd->nd_mrep);
4469 return (nd->nd_repstat);
4473 * Do the NFSv4.1 Exchange ID.
4476 nfsrpc_exchangeid(struct nfsmount *nmp, struct nfsclclient *clp,
4477 struct nfssockreq *nrp, uint32_t exchflags, struct nfsclds **dspp,
4478 struct ucred *cred, NFSPROC_T *p)
4480 uint32_t *tl, v41flags;
4481 struct nfsrv_descript nfsd;
4482 struct nfsrv_descript *nd = &nfsd;
4483 struct nfsclds *dsp;
4484 struct timespec verstime;
4488 nfscl_reqstart(nd, NFSPROC_EXCHANGEID, nmp, NULL, 0, NULL, NULL);
4489 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_UNSIGNED);
4490 *tl++ = txdr_unsigned(nfsboottime.tv_sec); /* Client owner */
4491 *tl = txdr_unsigned(clp->nfsc_rev);
4492 (void) nfsm_strtom(nd, clp->nfsc_id, clp->nfsc_idlen);
4494 NFSM_BUILD(tl, uint32_t *, 3 * NFSX_UNSIGNED);
4495 *tl++ = txdr_unsigned(exchflags);
4496 *tl++ = txdr_unsigned(NFSV4EXCH_SP4NONE);
4498 /* Set the implementation id4 */
4499 *tl = txdr_unsigned(1);
4500 (void) nfsm_strtom(nd, "freebsd.org", strlen("freebsd.org"));
4501 (void) nfsm_strtom(nd, version, strlen(version));
4502 NFSM_BUILD(tl, uint32_t *, NFSX_V4TIME);
4503 verstime.tv_sec = 1293840000; /* Jan 1, 2011 */
4504 verstime.tv_nsec = 0;
4505 txdr_nfsv4time(&verstime, tl);
4506 nd->nd_flag |= ND_USEGSSNAME;
4507 error = newnfs_request(nd, nmp, NULL, nrp, NULL, p, cred,
4508 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4509 NFSCL_DEBUG(1, "exchangeid err=%d reps=%d\n", error,
4510 (int)nd->nd_repstat);
4513 if (nd->nd_repstat == 0) {
4514 NFSM_DISSECT(tl, uint32_t *, 6 * NFSX_UNSIGNED + NFSX_HYPER);
4515 len = fxdr_unsigned(int, *(tl + 7));
4516 if (len < 0 || len > NFSV4_OPAQUELIMIT) {
4517 error = NFSERR_BADXDR;
4520 dsp = malloc(sizeof(struct nfsclds) + len, M_NFSCLDS,
4522 dsp->nfsclds_expire = NFSD_MONOSEC + clp->nfsc_renew;
4523 dsp->nfsclds_servownlen = len;
4524 dsp->nfsclds_sess.nfsess_clientid.lval[0] = *tl++;
4525 dsp->nfsclds_sess.nfsess_clientid.lval[1] = *tl++;
4526 dsp->nfsclds_sess.nfsess_sequenceid =
4527 fxdr_unsigned(uint32_t, *tl++);
4528 v41flags = fxdr_unsigned(uint32_t, *tl);
4529 if ((v41flags & NFSV4EXCH_USEPNFSMDS) != 0 &&
4530 NFSHASPNFSOPT(nmp)) {
4531 NFSCL_DEBUG(1, "set PNFS\n");
4533 nmp->nm_state |= NFSSTA_PNFS;
4535 dsp->nfsclds_flags |= NFSCLDS_MDS;
4537 if ((v41flags & NFSV4EXCH_USEPNFSDS) != 0)
4538 dsp->nfsclds_flags |= NFSCLDS_DS;
4540 nd->nd_repstat = nfsrv_mtostr(nd,
4541 dsp->nfsclds_serverown, len);
4542 if (nd->nd_repstat == 0) {
4543 mtx_init(&dsp->nfsclds_mtx, "nfsds", NULL, MTX_DEF);
4544 mtx_init(&dsp->nfsclds_sess.nfsess_mtx, "nfssession",
4546 nfscl_initsessionslots(&dsp->nfsclds_sess);
4549 free(dsp, M_NFSCLDS);
4551 error = nd->nd_repstat;
4553 mbuf_freem(nd->nd_mrep);
4558 * Do the NFSv4.1 Create Session.
4561 nfsrpc_createsession(struct nfsmount *nmp, struct nfsclsession *sep,
4562 struct nfssockreq *nrp, uint32_t sequenceid, int mds, struct ucred *cred,
4565 uint32_t crflags, *tl;
4566 struct nfsrv_descript nfsd;
4567 struct nfsrv_descript *nd = &nfsd;
4570 nfscl_reqstart(nd, NFSPROC_CREATESESSION, nmp, NULL, 0, NULL, NULL);
4571 NFSM_BUILD(tl, uint32_t *, 4 * NFSX_UNSIGNED);
4572 *tl++ = sep->nfsess_clientid.lval[0];
4573 *tl++ = sep->nfsess_clientid.lval[1];
4574 *tl++ = txdr_unsigned(sequenceid);
4575 crflags = (NFSMNT_RDONLY(nmp->nm_mountp) ? 0 : NFSV4CRSESS_PERSIST);
4576 if (nfscl_enablecallb != 0 && nfs_numnfscbd > 0)
4577 crflags |= NFSV4CRSESS_CONNBACKCHAN;
4578 *tl = txdr_unsigned(crflags);
4580 /* Fill in fore channel attributes. */
4581 NFSM_BUILD(tl, uint32_t *, 7 * NFSX_UNSIGNED);
4582 *tl++ = 0; /* Header pad size */
4583 *tl++ = txdr_unsigned(100000); /* Max request size */
4584 *tl++ = txdr_unsigned(100000); /* Max response size */
4585 *tl++ = txdr_unsigned(4096); /* Max response size cached */
4586 *tl++ = txdr_unsigned(20); /* Max operations */
4587 *tl++ = txdr_unsigned(64); /* Max slots */
4588 *tl = 0; /* No rdma ird */
4590 /* Fill in back channel attributes. */
4591 NFSM_BUILD(tl, uint32_t *, 7 * NFSX_UNSIGNED);
4592 *tl++ = 0; /* Header pad size */
4593 *tl++ = txdr_unsigned(10000); /* Max request size */
4594 *tl++ = txdr_unsigned(10000); /* Max response size */
4595 *tl++ = txdr_unsigned(4096); /* Max response size cached */
4596 *tl++ = txdr_unsigned(4); /* Max operations */
4597 *tl++ = txdr_unsigned(NFSV4_CBSLOTS); /* Max slots */
4598 *tl = 0; /* No rdma ird */
4600 NFSM_BUILD(tl, uint32_t *, 8 * NFSX_UNSIGNED);
4601 *tl++ = txdr_unsigned(NFS_CALLBCKPROG); /* Call back prog # */
4603 /* Allow AUTH_SYS callbacks as uid, gid == 0. */
4604 *tl++ = txdr_unsigned(1); /* Auth_sys only */
4605 *tl++ = txdr_unsigned(AUTH_SYS); /* AUTH_SYS type */
4606 *tl++ = txdr_unsigned(nfsboottime.tv_sec); /* time stamp */
4607 *tl++ = 0; /* Null machine name */
4608 *tl++ = 0; /* Uid == 0 */
4609 *tl++ = 0; /* Gid == 0 */
4610 *tl = 0; /* No additional gids */
4611 nd->nd_flag |= ND_USEGSSNAME;
4612 error = newnfs_request(nd, nmp, NULL, nrp, NULL, p, cred, NFS_PROG,
4613 NFS_VER4, NULL, 1, NULL, NULL);
4616 if (nd->nd_repstat == 0) {
4617 NFSM_DISSECT(tl, uint32_t *, NFSX_V4SESSIONID +
4619 bcopy(tl, sep->nfsess_sessionid, NFSX_V4SESSIONID);
4620 tl += NFSX_V4SESSIONID / NFSX_UNSIGNED;
4621 sep->nfsess_sequenceid = fxdr_unsigned(uint32_t, *tl++);
4622 crflags = fxdr_unsigned(uint32_t, *tl);
4623 if ((crflags & NFSV4CRSESS_PERSIST) != 0 && mds != 0) {
4625 nmp->nm_state |= NFSSTA_SESSPERSIST;
4629 /* Get the fore channel slot count. */
4630 NFSM_DISSECT(tl, uint32_t *, 7 * NFSX_UNSIGNED);
4631 tl += 3; /* Skip the other counts. */
4632 sep->nfsess_maxcache = fxdr_unsigned(int, *tl++);
4634 sep->nfsess_foreslots = fxdr_unsigned(uint16_t, *tl++);
4635 NFSCL_DEBUG(4, "fore slots=%d\n", (int)sep->nfsess_foreslots);
4636 irdcnt = fxdr_unsigned(int, *tl);
4638 NFSM_DISSECT(tl, uint32_t *, irdcnt * NFSX_UNSIGNED);
4640 /* and the back channel slot count. */
4641 NFSM_DISSECT(tl, uint32_t *, 7 * NFSX_UNSIGNED);
4643 sep->nfsess_backslots = fxdr_unsigned(uint16_t, *tl);
4644 NFSCL_DEBUG(4, "back slots=%d\n", (int)sep->nfsess_backslots);
4646 error = nd->nd_repstat;
4648 mbuf_freem(nd->nd_mrep);
4653 * Do the NFSv4.1 Destroy Session.
4656 nfsrpc_destroysession(struct nfsmount *nmp, struct nfsclclient *clp,
4657 struct ucred *cred, NFSPROC_T *p)
4660 struct nfsrv_descript nfsd;
4661 struct nfsrv_descript *nd = &nfsd;
4664 nfscl_reqstart(nd, NFSPROC_DESTROYSESSION, nmp, NULL, 0, NULL, NULL);
4665 NFSM_BUILD(tl, uint32_t *, NFSX_V4SESSIONID);
4666 bcopy(NFSMNT_MDSSESSION(nmp)->nfsess_sessionid, tl, NFSX_V4SESSIONID);
4667 nd->nd_flag |= ND_USEGSSNAME;
4668 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4669 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4672 error = nd->nd_repstat;
4673 mbuf_freem(nd->nd_mrep);
4678 * Do the NFSv4.1 Destroy Client.
4681 nfsrpc_destroyclient(struct nfsmount *nmp, struct nfsclclient *clp,
4682 struct ucred *cred, NFSPROC_T *p)
4685 struct nfsrv_descript nfsd;
4686 struct nfsrv_descript *nd = &nfsd;
4689 nfscl_reqstart(nd, NFSPROC_DESTROYCLIENT, nmp, NULL, 0, NULL, NULL);
4690 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_UNSIGNED);
4691 *tl++ = NFSMNT_MDSSESSION(nmp)->nfsess_clientid.lval[0];
4692 *tl = NFSMNT_MDSSESSION(nmp)->nfsess_clientid.lval[1];
4693 nd->nd_flag |= ND_USEGSSNAME;
4694 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4695 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4698 error = nd->nd_repstat;
4699 mbuf_freem(nd->nd_mrep);
4704 * Do the NFSv4.1 LayoutGet.
4707 nfsrpc_layoutget(struct nfsmount *nmp, uint8_t *fhp, int fhlen, int iomode,
4708 uint64_t offset, uint64_t len, uint64_t minlen, int layoutlen,
4709 nfsv4stateid_t *stateidp, int *retonclosep, struct nfsclflayouthead *flhp,
4710 struct ucred *cred, NFSPROC_T *p, void *stuff)
4713 struct nfsrv_descript nfsd, *nd = &nfsd;
4715 struct nfsclflayout *flp, *prevflp, *tflp;
4716 int cnt, error, gotiomode, fhcnt, nfhlen, i, j;
4722 nfscl_reqstart(nd, NFSPROC_LAYOUTGET, nmp, fhp, fhlen, NULL, NULL);
4723 NFSM_BUILD(tl, uint32_t *, 4 * NFSX_UNSIGNED + 3 * NFSX_HYPER +
4725 *tl++ = newnfs_false; /* Don't signal availability. */
4726 *tl++ = txdr_unsigned(NFSLAYOUT_NFSV4_1_FILES);
4727 *tl++ = txdr_unsigned(iomode);
4728 txdr_hyper(offset, tl);
4730 txdr_hyper(len, tl);
4732 txdr_hyper(minlen, tl);
4734 *tl++ = txdr_unsigned(stateidp->seqid);
4735 NFSCL_DEBUG(4, "layget seq=%d\n", (int)stateidp->seqid);
4736 *tl++ = stateidp->other[0];
4737 *tl++ = stateidp->other[1];
4738 *tl++ = stateidp->other[2];
4739 *tl = txdr_unsigned(layoutlen);
4740 nd->nd_flag |= ND_USEGSSNAME;
4741 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4742 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4745 if (nd->nd_repstat == 0) {
4746 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED + NFSX_STATEID);
4751 stateidp->seqid = fxdr_unsigned(uint32_t, *tl++);
4752 NFSCL_DEBUG(4, "retoncls=%d stseq=%d\n", *retonclosep,
4753 (int)stateidp->seqid);
4754 stateidp->other[0] = *tl++;
4755 stateidp->other[1] = *tl++;
4756 stateidp->other[2] = *tl++;
4757 cnt = fxdr_unsigned(int, *tl);
4758 NFSCL_DEBUG(4, "layg cnt=%d\n", cnt);
4759 if (cnt <= 0 || cnt > 10000) {
4760 /* Don't accept more than 10000 layouts in reply. */
4761 error = NFSERR_BADXDR;
4764 for (i = 0; i < cnt; i++) {
4765 /* Dissect all the way to the file handle cnt. */
4766 NFSM_DISSECT(tl, uint32_t *, 3 * NFSX_HYPER +
4767 6 * NFSX_UNSIGNED + NFSX_V4DEVICEID);
4768 fhcnt = fxdr_unsigned(int, *(tl + 11 +
4769 NFSX_V4DEVICEID / NFSX_UNSIGNED));
4770 NFSCL_DEBUG(4, "fhcnt=%d\n", fhcnt);
4771 if (fhcnt < 0 || fhcnt > 100) {
4772 /* Don't accept more than 100 file handles. */
4773 error = NFSERR_BADXDR;
4777 flp = malloc(sizeof(*flp) + (fhcnt - 1) *
4778 sizeof(struct nfsfh *),
4779 M_NFSFLAYOUT, M_WAITOK);
4781 flp = malloc(sizeof(*flp),
4782 M_NFSFLAYOUT, M_WAITOK);
4783 flp->nfsfl_flags = 0;
4784 flp->nfsfl_fhcnt = 0;
4785 flp->nfsfl_devp = NULL;
4786 flp->nfsfl_off = fxdr_hyper(tl); tl += 2;
4787 retlen = fxdr_hyper(tl); tl += 2;
4788 if (flp->nfsfl_off + retlen < flp->nfsfl_off)
4789 flp->nfsfl_end = UINT64_MAX - flp->nfsfl_off;
4791 flp->nfsfl_end = flp->nfsfl_off + retlen;
4792 flp->nfsfl_iomode = fxdr_unsigned(int, *tl++);
4793 if (gotiomode == -1)
4794 gotiomode = flp->nfsfl_iomode;
4795 NFSCL_DEBUG(4, "layg reqiom=%d retiom=%d\n", iomode,
4796 (int)flp->nfsfl_iomode);
4797 if (fxdr_unsigned(int, *tl++) !=
4798 NFSLAYOUT_NFSV4_1_FILES) {
4799 printf("NFSv4.1: got non-files layout\n");
4800 error = NFSERR_BADXDR;
4803 NFSBCOPY(++tl, flp->nfsfl_dev, NFSX_V4DEVICEID);
4804 tl += (NFSX_V4DEVICEID / NFSX_UNSIGNED);
4805 flp->nfsfl_util = fxdr_unsigned(uint32_t, *tl++);
4806 NFSCL_DEBUG(4, "flutil=0x%x\n", flp->nfsfl_util);
4807 flp->nfsfl_stripe1 = fxdr_unsigned(uint32_t, *tl++);
4808 flp->nfsfl_patoff = fxdr_hyper(tl); tl += 2;
4809 if (fxdr_unsigned(int, *tl) != fhcnt) {
4810 printf("EEK! bad fhcnt\n");
4811 error = NFSERR_BADXDR;
4814 for (j = 0; j < fhcnt; j++) {
4815 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
4816 nfhlen = fxdr_unsigned(int, *tl);
4817 if (nfhlen <= 0 || nfhlen > NFSX_V4FHMAX) {
4818 error = NFSERR_BADXDR;
4821 nfhp = malloc(sizeof(*nfhp) + nfhlen - 1,
4823 flp->nfsfl_fh[j] = nfhp;
4825 nfhp->nfh_len = nfhlen;
4826 NFSM_DISSECT(cp, uint8_t *, NFSM_RNDUP(nfhlen));
4827 NFSBCOPY(cp, nfhp->nfh_fh, nfhlen);
4829 if (flp->nfsfl_iomode == gotiomode) {
4830 /* Keep the list in increasing offset order. */
4831 tflp = LIST_FIRST(flhp);
4833 while (tflp != NULL &&
4834 tflp->nfsfl_off < flp->nfsfl_off) {
4836 tflp = LIST_NEXT(tflp, nfsfl_list);
4838 if (prevflp == NULL)
4839 LIST_INSERT_HEAD(flhp, flp, nfsfl_list);
4841 LIST_INSERT_AFTER(prevflp, flp,
4844 printf("nfscl_layoutget(): got wrong iomode\n");
4845 nfscl_freeflayout(flp);
4850 if (nd->nd_repstat != 0 && error == 0)
4851 error = nd->nd_repstat;
4853 if (error != 0 && flp != NULL)
4854 nfscl_freeflayout(flp);
4855 mbuf_freem(nd->nd_mrep);
4860 * Do the NFSv4.1 Get Device Info.
4863 nfsrpc_getdeviceinfo(struct nfsmount *nmp, uint8_t *deviceid, int layouttype,
4864 uint32_t *notifybitsp, struct nfscldevinfo **ndip, struct ucred *cred,
4868 struct nfsrv_descript nfsd;
4869 struct nfsrv_descript *nd = &nfsd;
4870 struct sockaddr_storage ss;
4871 struct nfsclds *dsp = NULL, **dspp;
4872 struct nfscldevinfo *ndi;
4873 int addrcnt, bitcnt, error, i, isudp, j, pos, safilled, stripecnt;
4874 uint8_t stripeindex;
4878 nfscl_reqstart(nd, NFSPROC_GETDEVICEINFO, nmp, NULL, 0, NULL, NULL);
4879 NFSM_BUILD(tl, uint32_t *, NFSX_V4DEVICEID + 3 * NFSX_UNSIGNED);
4880 NFSBCOPY(deviceid, tl, NFSX_V4DEVICEID);
4881 tl += (NFSX_V4DEVICEID / NFSX_UNSIGNED);
4882 *tl++ = txdr_unsigned(layouttype);
4883 *tl++ = txdr_unsigned(100000);
4884 if (notifybitsp != NULL && *notifybitsp != 0) {
4885 *tl = txdr_unsigned(1); /* One word of bits. */
4886 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
4887 *tl = txdr_unsigned(*notifybitsp);
4889 *tl = txdr_unsigned(0);
4890 nd->nd_flag |= ND_USEGSSNAME;
4891 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4892 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4895 if (nd->nd_repstat == 0) {
4896 NFSM_DISSECT(tl, uint32_t *, 3 * NFSX_UNSIGNED);
4897 if (layouttype != fxdr_unsigned(int, *tl++))
4898 printf("EEK! devinfo layout type not same!\n");
4899 stripecnt = fxdr_unsigned(int, *++tl);
4900 NFSCL_DEBUG(4, "stripecnt=%d\n", stripecnt);
4901 if (stripecnt < 1 || stripecnt > 4096) {
4902 printf("NFS devinfo stripecnt %d: out of range\n",
4904 error = NFSERR_BADXDR;
4907 NFSM_DISSECT(tl, uint32_t *, (stripecnt + 1) * NFSX_UNSIGNED);
4908 addrcnt = fxdr_unsigned(int, *(tl + stripecnt));
4909 NFSCL_DEBUG(4, "addrcnt=%d\n", addrcnt);
4910 if (addrcnt < 1 || addrcnt > 128) {
4911 printf("NFS devinfo addrcnt %d: out of range\n",
4913 error = NFSERR_BADXDR;
4918 * Now we know how many stripe indices and addresses, so
4919 * we can allocate the structure the correct size.
4921 i = (stripecnt * sizeof(uint8_t)) / sizeof(struct nfsclds *)
4923 NFSCL_DEBUG(4, "stripeindices=%d\n", i);
4924 ndi = malloc(sizeof(*ndi) + (addrcnt + i) *
4925 sizeof(struct nfsclds *), M_NFSDEVINFO, M_WAITOK | M_ZERO);
4926 NFSBCOPY(deviceid, ndi->nfsdi_deviceid, NFSX_V4DEVICEID);
4927 ndi->nfsdi_refcnt = 0;
4928 ndi->nfsdi_stripecnt = stripecnt;
4929 ndi->nfsdi_addrcnt = addrcnt;
4930 /* Fill in the stripe indices. */
4931 for (i = 0; i < stripecnt; i++) {
4932 stripeindex = fxdr_unsigned(uint8_t, *tl++);
4933 NFSCL_DEBUG(4, "stripeind=%d\n", stripeindex);
4934 if (stripeindex >= addrcnt) {
4935 printf("NFS devinfo stripeindex %d: too big\n",
4937 error = NFSERR_BADXDR;
4940 nfsfldi_setstripeindex(ndi, i, stripeindex);
4943 /* Now, dissect the server address(es). */
4945 for (i = 0; i < addrcnt; i++) {
4946 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
4947 cnt = fxdr_unsigned(uint32_t, *tl);
4949 printf("NFS devinfo 0 len addrlist\n");
4950 error = NFSERR_BADXDR;
4953 dspp = nfsfldi_addr(ndi, i);
4954 pos = arc4random() % cnt; /* Choose one. */
4956 for (j = 0; j < cnt; j++) {
4957 error = nfsv4_getipaddr(nd, &ss, &isudp);
4958 if (error != 0 && error != EPERM) {
4959 error = NFSERR_BADXDR;
4962 if (error == 0 && isudp == 0) {
4965 * - use "pos" entry if it is of the
4966 * same af_family or none of them
4967 * is of the same af_family
4969 * - use the first one of the same
4972 if ((safilled == 0 && ss.ss_family ==
4973 nmp->nm_nam->sa_family) ||
4975 (safilled == 0 || ss.ss_family ==
4976 nmp->nm_nam->sa_family)) ||
4977 (safilled == 1 && ss.ss_family ==
4978 nmp->nm_nam->sa_family)) {
4979 error = nfsrpc_fillsa(nmp, &ss,
4984 nmp->nm_nam->sa_family)
4996 /* And the notify bits. */
4997 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
4998 if (safilled != 0) {
4999 bitcnt = fxdr_unsigned(int, *tl);
5001 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
5002 if (notifybitsp != NULL)
5004 fxdr_unsigned(uint32_t, *tl);
5010 if (nd->nd_repstat != 0)
5011 error = nd->nd_repstat;
5013 if (error != 0 && ndi != NULL)
5014 nfscl_freedevinfo(ndi);
5015 mbuf_freem(nd->nd_mrep);
5020 * Do the NFSv4.1 LayoutCommit.
5023 nfsrpc_layoutcommit(struct nfsmount *nmp, uint8_t *fh, int fhlen, int reclaim,
5024 uint64_t off, uint64_t len, uint64_t lastbyte, nfsv4stateid_t *stateidp,
5025 int layouttype, int layoutupdatecnt, uint8_t *layp, struct ucred *cred,
5026 NFSPROC_T *p, void *stuff)
5029 struct nfsrv_descript nfsd, *nd = &nfsd;
5030 int error, outcnt, i;
5033 nfscl_reqstart(nd, NFSPROC_LAYOUTCOMMIT, nmp, fh, fhlen, NULL, NULL);
5034 NFSM_BUILD(tl, uint32_t *, 5 * NFSX_UNSIGNED + 3 * NFSX_HYPER +
5036 txdr_hyper(off, tl);
5038 txdr_hyper(len, tl);
5041 *tl++ = newnfs_true;
5043 *tl++ = newnfs_false;
5044 *tl++ = txdr_unsigned(stateidp->seqid);
5045 *tl++ = stateidp->other[0];
5046 *tl++ = stateidp->other[1];
5047 *tl++ = stateidp->other[2];
5048 *tl++ = newnfs_true;
5051 else if (lastbyte >= (off + len))
5052 lastbyte = off + len - 1;
5053 txdr_hyper(lastbyte, tl);
5055 *tl++ = newnfs_false;
5056 *tl++ = txdr_unsigned(layouttype);
5057 *tl = txdr_unsigned(layoutupdatecnt);
5058 if (layoutupdatecnt > 0) {
5059 KASSERT(layouttype != NFSLAYOUT_NFSV4_1_FILES,
5060 ("Must be nil for Files Layout"));
5061 outcnt = NFSM_RNDUP(layoutupdatecnt);
5062 NFSM_BUILD(cp, uint8_t *, outcnt);
5063 NFSBCOPY(layp, cp, layoutupdatecnt);
5064 cp += layoutupdatecnt;
5065 for (i = 0; i < (outcnt - layoutupdatecnt); i++)
5068 nd->nd_flag |= ND_USEGSSNAME;
5069 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
5070 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
5073 error = nd->nd_repstat;
5074 mbuf_freem(nd->nd_mrep);
5079 * Do the NFSv4.1 LayoutReturn.
5082 nfsrpc_layoutreturn(struct nfsmount *nmp, uint8_t *fh, int fhlen, int reclaim,
5083 int layouttype, uint32_t iomode, int layoutreturn, uint64_t offset,
5084 uint64_t len, nfsv4stateid_t *stateidp, int layoutcnt, uint32_t *layp,
5085 struct ucred *cred, NFSPROC_T *p, void *stuff)
5088 struct nfsrv_descript nfsd, *nd = &nfsd;
5089 int error, outcnt, i;
5092 nfscl_reqstart(nd, NFSPROC_LAYOUTRETURN, nmp, fh, fhlen, NULL, NULL);
5093 NFSM_BUILD(tl, uint32_t *, 4 * NFSX_UNSIGNED);
5095 *tl++ = newnfs_true;
5097 *tl++ = newnfs_false;
5098 *tl++ = txdr_unsigned(layouttype);
5099 *tl++ = txdr_unsigned(iomode);
5100 *tl = txdr_unsigned(layoutreturn);
5101 if (layoutreturn == NFSLAYOUTRETURN_FILE) {
5102 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_HYPER + NFSX_STATEID +
5104 txdr_hyper(offset, tl);
5106 txdr_hyper(len, tl);
5108 NFSCL_DEBUG(4, "layoutret stseq=%d\n", (int)stateidp->seqid);
5109 *tl++ = txdr_unsigned(stateidp->seqid);
5110 *tl++ = stateidp->other[0];
5111 *tl++ = stateidp->other[1];
5112 *tl++ = stateidp->other[2];
5113 *tl = txdr_unsigned(layoutcnt);
5114 if (layoutcnt > 0) {
5115 outcnt = NFSM_RNDUP(layoutcnt);
5116 NFSM_BUILD(cp, uint8_t *, outcnt);
5117 NFSBCOPY(layp, cp, layoutcnt);
5119 for (i = 0; i < (outcnt - layoutcnt); i++)
5123 nd->nd_flag |= ND_USEGSSNAME;
5124 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
5125 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
5128 if (nd->nd_repstat == 0) {
5129 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
5131 NFSM_DISSECT(tl, uint32_t *, NFSX_STATEID);
5132 stateidp->seqid = fxdr_unsigned(uint32_t, *tl++);
5133 stateidp->other[0] = *tl++;
5134 stateidp->other[1] = *tl++;
5135 stateidp->other[2] = *tl;
5138 error = nd->nd_repstat;
5140 mbuf_freem(nd->nd_mrep);
5145 * Acquire a layout and devinfo, if possible. The caller must have acquired
5146 * a reference count on the nfsclclient structure before calling this.
5147 * Return the layout in lypp with a reference count on it, if successful.
5150 nfsrpc_getlayout(struct nfsmount *nmp, vnode_t vp, struct nfsfh *nfhp,
5151 int iomode, uint32_t *notifybitsp, nfsv4stateid_t *stateidp, uint64_t off,
5152 struct nfscllayout **lypp, struct ucred *cred, NFSPROC_T *p)
5154 struct nfscllayout *lyp;
5155 struct nfsclflayout *flp, *tflp;
5156 struct nfscldevinfo *dip;
5157 struct nfsclflayouthead flh;
5158 int error = 0, islocked, layoutlen, recalled, retonclose;
5159 nfsv4stateid_t stateid;
5163 * If lyp is returned non-NULL, there will be a refcnt (shared lock)
5164 * on it, iff flp != NULL or a lock (exclusive lock) on it iff
5167 lyp = nfscl_getlayout(nmp->nm_clp, nfhp->nfh_fh, nfhp->nfh_len,
5168 off, &flp, &recalled);
5170 if (lyp == NULL || flp == NULL) {
5174 layoutlen = NFSMNT_MDSSESSION(nmp)->nfsess_maxcache -
5175 (NFSX_STATEID + 3 * NFSX_UNSIGNED);
5178 stateid.other[0] = stateidp->other[0];
5179 stateid.other[1] = stateidp->other[1];
5180 stateid.other[2] = stateidp->other[2];
5181 error = nfsrpc_layoutget(nmp, nfhp->nfh_fh,
5182 nfhp->nfh_len, iomode, (uint64_t)0, INT64_MAX,
5183 (uint64_t)0, layoutlen, &stateid, &retonclose,
5184 &flh, cred, p, NULL);
5187 stateid.seqid = lyp->nfsly_stateid.seqid;
5188 stateid.other[0] = lyp->nfsly_stateid.other[0];
5189 stateid.other[1] = lyp->nfsly_stateid.other[1];
5190 stateid.other[2] = lyp->nfsly_stateid.other[2];
5191 error = nfsrpc_layoutget(nmp, nfhp->nfh_fh,
5192 nfhp->nfh_len, iomode, off, INT64_MAX,
5193 (uint64_t)0, layoutlen, &stateid, &retonclose,
5194 &flh, cred, p, NULL);
5197 LIST_FOREACH(tflp, &flh, nfsfl_list) {
5198 error = nfscl_adddevinfo(nmp, NULL, tflp);
5200 error = nfsrpc_getdeviceinfo(nmp,
5202 NFSLAYOUT_NFSV4_1_FILES,
5203 notifybitsp, &dip, cred, p);
5206 error = nfscl_adddevinfo(nmp, dip,
5210 "getlayout: cannot add\n");
5215 * nfscl_layout() always returns with the nfsly_lock
5216 * set to a refcnt (shared lock).
5218 error = nfscl_layout(nmp, vp, nfhp->nfh_fh,
5219 nfhp->nfh_len, &stateid, retonclose, &flh, &lyp,
5223 } else if (islocked != 0)
5224 nfsv4_unlock(&lyp->nfsly_lock, 0);
5231 * Do a TCP connection plus exchange id and create session.
5232 * If successful, a "struct nfsclds" is linked into the list for the
5233 * mount point and a pointer to it is returned.
5236 nfsrpc_fillsa(struct nfsmount *nmp, struct sockaddr_storage *ssp,
5237 struct nfsclds **dspp, NFSPROC_T *p)
5239 struct sockaddr_in *msad, *sad, *ssd;
5240 struct sockaddr_in6 *msad6, *sad6, *ssd6;
5241 struct nfsclclient *clp;
5242 struct nfssockreq *nrp;
5243 struct nfsclds *dsp, *tdsp;
5245 enum nfsclds_state retv;
5246 uint32_t sequenceid;
5248 KASSERT(nmp->nm_sockreq.nr_cred != NULL,
5249 ("nfsrpc_fillsa: NULL nr_cred"));
5255 if (ssp->ss_family == AF_INET) {
5256 ssd = (struct sockaddr_in *)ssp;
5260 * Check to see if we already have a session for this
5261 * address that is usable for a DS.
5262 * Note that the MDS's address is in a different place
5263 * than the sessions already acquired for DS's.
5265 msad = (struct sockaddr_in *)nmp->nm_sockreq.nr_nam;
5266 tdsp = TAILQ_FIRST(&nmp->nm_sess);
5267 while (tdsp != NULL) {
5268 if (msad != NULL && msad->sin_family == AF_INET &&
5269 ssd->sin_addr.s_addr == msad->sin_addr.s_addr &&
5270 ssd->sin_port == msad->sin_port &&
5271 (tdsp->nfsclds_flags & NFSCLDS_DS) != 0) {
5274 NFSCL_DEBUG(4, "fnd same addr\n");
5277 tdsp = TAILQ_NEXT(tdsp, nfsclds_list);
5278 if (tdsp != NULL && tdsp->nfsclds_sockp != NULL)
5279 msad = (struct sockaddr_in *)
5280 tdsp->nfsclds_sockp->nr_nam;
5286 /* No IP address match, so look for new/trunked one. */
5287 sad = malloc(sizeof(*sad), M_SONAME, M_WAITOK | M_ZERO);
5288 sad->sin_len = sizeof(*sad);
5289 sad->sin_family = AF_INET;
5290 sad->sin_port = ssd->sin_port;
5291 sad->sin_addr.s_addr = ssd->sin_addr.s_addr;
5292 nrp = malloc(sizeof(*nrp), M_NFSSOCKREQ, M_WAITOK | M_ZERO);
5293 nrp->nr_nam = (struct sockaddr *)sad;
5294 } else if (ssp->ss_family == AF_INET6) {
5295 ssd6 = (struct sockaddr_in6 *)ssp;
5299 * Check to see if we already have a session for this
5300 * address that is usable for a DS.
5301 * Note that the MDS's address is in a different place
5302 * than the sessions already acquired for DS's.
5304 msad6 = (struct sockaddr_in6 *)nmp->nm_sockreq.nr_nam;
5305 tdsp = TAILQ_FIRST(&nmp->nm_sess);
5306 while (tdsp != NULL) {
5307 if (msad6 != NULL && msad6->sin6_family == AF_INET6 &&
5308 IN6_ARE_ADDR_EQUAL(&ssd6->sin6_addr,
5309 &msad6->sin6_addr) &&
5310 ssd6->sin6_port == msad6->sin6_port &&
5311 (tdsp->nfsclds_flags & NFSCLDS_DS) != 0) {
5316 tdsp = TAILQ_NEXT(tdsp, nfsclds_list);
5317 if (tdsp != NULL && tdsp->nfsclds_sockp != NULL)
5318 msad6 = (struct sockaddr_in6 *)
5319 tdsp->nfsclds_sockp->nr_nam;
5325 /* No IP address match, so look for new/trunked one. */
5326 sad6 = malloc(sizeof(*sad6), M_SONAME, M_WAITOK | M_ZERO);
5327 sad6->sin6_len = sizeof(*sad6);
5328 sad6->sin6_family = AF_INET6;
5329 sad6->sin6_port = ssd6->sin6_port;
5330 NFSBCOPY(&ssd6->sin6_addr, &sad6->sin6_addr,
5331 sizeof(struct in6_addr));
5332 nrp = malloc(sizeof(*nrp), M_NFSSOCKREQ, M_WAITOK | M_ZERO);
5333 nrp->nr_nam = (struct sockaddr *)sad6;
5337 nrp->nr_sotype = SOCK_STREAM;
5338 mtx_init(&nrp->nr_mtx, "nfssock", NULL, MTX_DEF);
5339 nrp->nr_prog = NFS_PROG;
5340 nrp->nr_vers = NFS_VER4;
5343 * Use the credentials that were used for the mount, which are
5344 * in nmp->nm_sockreq.nr_cred for newnfs_connect() etc.
5345 * Ref. counting the credentials with crhold() is probably not
5346 * necessary, since nm_sockreq.nr_cred won't be crfree()'d until
5347 * unmount, but I did it anyhow.
5349 nrp->nr_cred = crhold(nmp->nm_sockreq.nr_cred);
5350 error = newnfs_connect(nmp, nrp, NULL, p, 0);
5351 NFSCL_DEBUG(3, "DS connect=%d\n", error);
5353 /* Now, do the exchangeid and create session. */
5355 error = nfsrpc_exchangeid(nmp, clp, nrp, NFSV4EXCH_USEPNFSDS,
5356 &dsp, nrp->nr_cred, p);
5357 NFSCL_DEBUG(3, "DS exchangeid=%d\n", error);
5359 dsp->nfsclds_sockp = nrp;
5361 retv = nfscl_getsameserver(nmp, dsp, &tdsp);
5362 NFSCL_DEBUG(3, "getsame ret=%d\n", retv);
5363 if (retv == NFSDSP_USETHISSESSION) {
5366 * If there is already a session for this server,
5369 (void)newnfs_disconnect(nrp);
5370 nfscl_freenfsclds(dsp);
5374 if (retv == NFSDSP_SEQTHISSESSION)
5375 sequenceid = tdsp->nfsclds_sess.nfsess_sequenceid;
5377 sequenceid = dsp->nfsclds_sess.nfsess_sequenceid;
5379 error = nfsrpc_createsession(nmp, &dsp->nfsclds_sess,
5380 nrp, sequenceid, 0, nrp->nr_cred, p);
5381 NFSCL_DEBUG(3, "DS createsess=%d\n", error);
5383 NFSFREECRED(nrp->nr_cred);
5384 NFSFREEMUTEX(&nrp->nr_mtx);
5385 free(nrp->nr_nam, M_SONAME);
5386 free(nrp, M_NFSSOCKREQ);
5389 NFSCL_DEBUG(3, "add DS session\n");
5391 * Put it at the end of the list. That way the list
5392 * is ordered by when the entry was added. This matters
5393 * since the one done first is the one that should be
5394 * used for sequencid'ing any subsequent create sessions.
5397 TAILQ_INSERT_TAIL(&nmp->nm_sess, dsp, nfsclds_list);
5400 } else if (dsp != NULL)
5401 nfscl_freenfsclds(dsp);
5406 * Do the NFSv4.1 Reclaim Complete.
5409 nfsrpc_reclaimcomplete(struct nfsmount *nmp, struct ucred *cred, NFSPROC_T *p)
5412 struct nfsrv_descript nfsd;
5413 struct nfsrv_descript *nd = &nfsd;
5416 nfscl_reqstart(nd, NFSPROC_RECLAIMCOMPL, nmp, NULL, 0, NULL, NULL);
5417 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
5419 nd->nd_flag |= ND_USEGSSNAME;
5420 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
5421 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
5424 error = nd->nd_repstat;
5425 mbuf_freem(nd->nd_mrep);
5430 * Initialize the slot tables for a session.
5433 nfscl_initsessionslots(struct nfsclsession *sep)
5437 for (i = 0; i < NFSV4_CBSLOTS; i++) {
5438 if (sep->nfsess_cbslots[i].nfssl_reply != NULL)
5439 m_freem(sep->nfsess_cbslots[i].nfssl_reply);
5440 NFSBZERO(&sep->nfsess_cbslots[i], sizeof(struct nfsslot));
5442 for (i = 0; i < 64; i++)
5443 sep->nfsess_slotseq[i] = 0;
5444 sep->nfsess_slots = 0;
5448 * Called to try and do an I/O operation via an NFSv4.1 Data Server (DS).
5451 nfscl_doiods(vnode_t vp, struct uio *uiop, int *iomode, int *must_commit,
5452 uint32_t rwaccess, struct ucred *cred, NFSPROC_T *p)
5454 struct nfsnode *np = VTONFS(vp);
5455 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
5456 struct nfscllayout *layp;
5457 struct nfscldevinfo *dip;
5458 struct nfsclflayout *rflp;
5459 nfsv4stateid_t stateid;
5460 struct ucred *newcred;
5461 uint64_t lastbyte, len, off, oresid, xfer;
5462 int eof, error, iolaymode, recalled;
5465 if (!NFSHASPNFS(nmp) || nfscl_enablecallb == 0 || nfs_numnfscbd == 0 ||
5466 (np->n_flag & NNOLAYOUT) != 0)
5468 /* Now, get a reference cnt on the clientid for this mount. */
5469 if (nfscl_getref(nmp) == 0)
5472 /* Find an appropriate stateid. */
5473 newcred = NFSNEWCRED(cred);
5474 error = nfscl_getstateid(vp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len,
5475 rwaccess, 1, newcred, p, &stateid, &lckp);
5477 NFSFREECRED(newcred);
5481 /* Search for a layout for this file. */
5482 off = uiop->uio_offset;
5483 layp = nfscl_getlayout(nmp->nm_clp, np->n_fhp->nfh_fh,
5484 np->n_fhp->nfh_len, off, &rflp, &recalled);
5485 if (layp == NULL || rflp == NULL) {
5486 if (recalled != 0) {
5487 NFSFREECRED(newcred);
5492 nfscl_rellayout(layp, (rflp == NULL) ? 1 : 0);
5495 /* Try and get a Layout, if it is supported. */
5496 if (rwaccess == NFSV4OPEN_ACCESSWRITE ||
5497 (np->n_flag & NWRITEOPENED) != 0)
5498 iolaymode = NFSLAYOUTIOMODE_RW;
5500 iolaymode = NFSLAYOUTIOMODE_READ;
5501 error = nfsrpc_getlayout(nmp, vp, np->n_fhp, iolaymode,
5502 NULL, &stateid, off, &layp, newcred, p);
5505 np->n_flag |= NNOLAYOUT;
5508 nfscl_lockderef(lckp);
5509 NFSFREECRED(newcred);
5511 nfscl_rellayout(layp, 0);
5518 * Loop around finding a layout that works for the first part of
5519 * this I/O operation, and then call the function that actually
5523 len = (uint64_t)uiop->uio_resid;
5524 while (len > 0 && error == 0 && eof == 0) {
5525 off = uiop->uio_offset;
5526 error = nfscl_findlayoutforio(layp, off, rwaccess, &rflp);
5528 oresid = xfer = (uint64_t)uiop->uio_resid;
5529 if (xfer > (rflp->nfsfl_end - rflp->nfsfl_off))
5530 xfer = rflp->nfsfl_end - rflp->nfsfl_off;
5531 dip = nfscl_getdevinfo(nmp->nm_clp, rflp->nfsfl_dev,
5534 error = nfscl_doflayoutio(vp, uiop, iomode,
5535 must_commit, &eof, &stateid, rwaccess, dip,
5536 layp, rflp, off, xfer, newcred, p);
5537 nfscl_reldevinfo(dip);
5538 lastbyte = off + xfer - 1;
5541 if (lastbyte > layp->nfsly_lastbyte)
5542 layp->nfsly_lastbyte = lastbyte;
5548 len -= (oresid - (uint64_t)uiop->uio_resid);
5552 nfscl_lockderef(lckp);
5553 NFSFREECRED(newcred);
5554 nfscl_rellayout(layp, 0);
5560 * Find a file layout that will handle the first bytes of the requested
5561 * range and return the information from it needed to to the I/O operation.
5564 nfscl_findlayoutforio(struct nfscllayout *lyp, uint64_t off, uint32_t rwaccess,
5565 struct nfsclflayout **retflpp)
5567 struct nfsclflayout *flp, *nflp, *rflp;
5572 /* For reading, do the Read list first and then the Write list. */
5574 if (rw == NFSV4OPEN_ACCESSREAD)
5575 flp = LIST_FIRST(&lyp->nfsly_flayread);
5577 flp = LIST_FIRST(&lyp->nfsly_flayrw);
5578 while (flp != NULL) {
5579 nflp = LIST_NEXT(flp, nfsfl_list);
5580 if (flp->nfsfl_off > off)
5582 if (flp->nfsfl_end > off &&
5583 (rflp == NULL || rflp->nfsfl_end < flp->nfsfl_end))
5587 if (rw == NFSV4OPEN_ACCESSREAD)
5588 rw = NFSV4OPEN_ACCESSWRITE;
5593 /* This one covers the most bytes starting at off. */
5601 * Do I/O using an NFSv4.1 file layout.
5604 nfscl_doflayoutio(vnode_t vp, struct uio *uiop, int *iomode, int *must_commit,
5605 int *eofp, nfsv4stateid_t *stateidp, int rwflag, struct nfscldevinfo *dp,
5606 struct nfscllayout *lyp, struct nfsclflayout *flp, uint64_t off,
5607 uint64_t len, struct ucred *cred, NFSPROC_T *p)
5609 uint64_t io_off, rel_off, stripe_unit_size, transfer, xfer;
5610 int commit_thru_mds, error = 0, stripe_index, stripe_pos;
5613 struct nfsclds **dspp;
5616 rel_off = off - flp->nfsfl_patoff;
5617 stripe_unit_size = (flp->nfsfl_util >> 6) & 0x3ffffff;
5618 stripe_pos = (rel_off / stripe_unit_size + flp->nfsfl_stripe1) %
5619 dp->nfsdi_stripecnt;
5620 transfer = stripe_unit_size - (rel_off % stripe_unit_size);
5622 /* Loop around, doing I/O for each stripe unit. */
5623 while (len > 0 && error == 0) {
5624 stripe_index = nfsfldi_stripeindex(dp, stripe_pos);
5625 dspp = nfsfldi_addr(dp, stripe_index);
5630 if ((flp->nfsfl_util & NFSFLAYUTIL_DENSE) != 0) {
5632 if (stripe_pos >= flp->nfsfl_fhcnt)
5634 fhp = flp->nfsfl_fh[stripe_pos];
5635 io_off = (rel_off / (stripe_unit_size *
5636 dp->nfsdi_stripecnt)) * stripe_unit_size +
5637 rel_off % stripe_unit_size;
5639 /* Sparse layout. */
5640 if (flp->nfsfl_fhcnt > 1) {
5641 if (stripe_index >= flp->nfsfl_fhcnt)
5643 fhp = flp->nfsfl_fh[stripe_index];
5644 } else if (flp->nfsfl_fhcnt == 1)
5645 fhp = flp->nfsfl_fh[0];
5650 if ((flp->nfsfl_util & NFSFLAYUTIL_COMMIT_THRU_MDS) != 0)
5651 commit_thru_mds = 1;
5653 commit_thru_mds = 0;
5654 if (rwflag == FREAD)
5655 error = nfsrpc_readds(vp, uiop, stateidp, eofp, *dspp,
5656 io_off, xfer, fhp, cred, p);
5658 error = nfsrpc_writeds(vp, uiop, iomode, must_commit,
5659 stateidp, *dspp, io_off, xfer, fhp, commit_thru_mds,
5663 lyp->nfsly_flags |= NFSLY_WRITTEN;
5668 transfer = stripe_unit_size;
5669 stripe_pos = (stripe_pos + 1) % dp->nfsdi_stripecnt;
5678 * The actual read RPC done to a DS.
5681 nfsrpc_readds(vnode_t vp, struct uio *uiop, nfsv4stateid_t *stateidp, int *eofp,
5682 struct nfsclds *dsp, uint64_t io_off, int len, struct nfsfh *fhp,
5683 struct ucred *cred, NFSPROC_T *p)
5687 struct nfsrv_descript nfsd;
5688 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
5689 struct nfsrv_descript *nd = &nfsd;
5690 struct nfssockreq *nrp;
5693 nfscl_reqstart(nd, NFSPROC_READDS, nmp, fhp->nfh_fh, fhp->nfh_len,
5694 NULL, &dsp->nfsclds_sess);
5695 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSEQIDZERO);
5696 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED * 3);
5697 txdr_hyper(io_off, tl);
5698 *(tl + 2) = txdr_unsigned(len);
5699 nrp = dsp->nfsclds_sockp;
5701 /* If NULL, use the MDS socket. */
5702 nrp = &nmp->nm_sockreq;
5703 error = newnfs_request(nd, nmp, NULL, nrp, vp, p, cred,
5704 NFS_PROG, NFS_VER4, NULL, 1, NULL, &dsp->nfsclds_sess);
5707 if (nd->nd_repstat != 0) {
5708 error = nd->nd_repstat;
5711 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
5712 *eofp = fxdr_unsigned(int, *tl);
5713 NFSM_STRSIZ(retlen, len);
5714 error = nfsm_mbufuio(nd, uiop, retlen);
5716 if (nd->nd_mrep != NULL)
5717 mbuf_freem(nd->nd_mrep);
5722 * The actual write RPC done to a DS.
5725 nfsrpc_writeds(vnode_t vp, struct uio *uiop, int *iomode, int *must_commit,
5726 nfsv4stateid_t *stateidp, struct nfsclds *dsp, uint64_t io_off, int len,
5727 struct nfsfh *fhp, int commit_thru_mds, struct ucred *cred, NFSPROC_T *p)
5730 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
5731 int error, rlen, commit, committed = NFSWRITE_FILESYNC;
5733 struct nfsrv_descript nfsd;
5734 struct nfsrv_descript *nd = &nfsd;
5735 struct nfssockreq *nrp;
5737 KASSERT(uiop->uio_iovcnt == 1, ("nfs: writerpc iovcnt > 1"));
5739 nfscl_reqstart(nd, NFSPROC_WRITEDS, nmp, fhp->nfh_fh, fhp->nfh_len,
5740 NULL, &dsp->nfsclds_sess);
5741 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSEQIDZERO);
5742 NFSM_BUILD(tl, uint32_t *, NFSX_HYPER + 2 * NFSX_UNSIGNED);
5743 txdr_hyper(io_off, tl);
5745 *tl++ = txdr_unsigned(*iomode);
5746 *tl = txdr_unsigned(len);
5747 nfsm_uiombuf(nd, uiop, len);
5748 nrp = dsp->nfsclds_sockp;
5750 /* If NULL, use the MDS socket. */
5751 nrp = &nmp->nm_sockreq;
5752 error = newnfs_request(nd, nmp, NULL, nrp, vp, p, cred,
5753 NFS_PROG, NFS_VER4, NULL, 1, NULL, &dsp->nfsclds_sess);
5756 if (nd->nd_repstat != 0) {
5758 * In case the rpc gets retried, roll
5759 * the uio fileds changed by nfsm_uiombuf()
5762 uiop->uio_offset -= len;
5763 uio_uio_resid_add(uiop, len);
5764 uio_iov_base_add(uiop, -len);
5765 uio_iov_len_add(uiop, len);
5766 error = nd->nd_repstat;
5768 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED + NFSX_VERF);
5769 rlen = fxdr_unsigned(int, *tl++);
5773 } else if (rlen < len) {
5774 backup = len - rlen;
5775 uio_iov_base_add(uiop, -(backup));
5776 uio_iov_len_add(uiop, backup);
5777 uiop->uio_offset -= backup;
5778 uio_uio_resid_add(uiop, backup);
5781 commit = fxdr_unsigned(int, *tl++);
5784 * Return the lowest committment level
5785 * obtained by any of the RPCs.
5787 if (committed == NFSWRITE_FILESYNC)
5789 else if (committed == NFSWRITE_DATASYNC &&
5790 commit == NFSWRITE_UNSTABLE)
5792 if (commit_thru_mds != 0) {
5794 if (!NFSHASWRITEVERF(nmp)) {
5795 NFSBCOPY(tl, nmp->nm_verf, NFSX_VERF);
5796 NFSSETWRITEVERF(nmp);
5797 } else if (NFSBCMP(tl, nmp->nm_verf, NFSX_VERF)) {
5799 NFSBCOPY(tl, nmp->nm_verf, NFSX_VERF);
5804 if ((dsp->nfsclds_flags & NFSCLDS_HASWRITEVERF) == 0) {
5805 NFSBCOPY(tl, dsp->nfsclds_verf, NFSX_VERF);
5806 dsp->nfsclds_flags |= NFSCLDS_HASWRITEVERF;
5807 } else if (NFSBCMP(tl, dsp->nfsclds_verf, NFSX_VERF)) {
5809 NFSBCOPY(tl, dsp->nfsclds_verf, NFSX_VERF);
5815 if (nd->nd_mrep != NULL)
5816 mbuf_freem(nd->nd_mrep);
5817 *iomode = committed;
5818 if (nd->nd_repstat != 0 && error == 0)
5819 error = nd->nd_repstat;
5824 * Free up the nfsclds structure.
5827 nfscl_freenfsclds(struct nfsclds *dsp)
5833 if (dsp->nfsclds_sockp != NULL) {
5834 NFSFREECRED(dsp->nfsclds_sockp->nr_cred);
5835 NFSFREEMUTEX(&dsp->nfsclds_sockp->nr_mtx);
5836 free(dsp->nfsclds_sockp->nr_nam, M_SONAME);
5837 free(dsp->nfsclds_sockp, M_NFSSOCKREQ);
5839 NFSFREEMUTEX(&dsp->nfsclds_mtx);
5840 NFSFREEMUTEX(&dsp->nfsclds_sess.nfsess_mtx);
5841 for (i = 0; i < NFSV4_CBSLOTS; i++) {
5842 if (dsp->nfsclds_sess.nfsess_cbslots[i].nfssl_reply != NULL)
5844 dsp->nfsclds_sess.nfsess_cbslots[i].nfssl_reply);
5846 free(dsp, M_NFSCLDS);
5849 static enum nfsclds_state
5850 nfscl_getsameserver(struct nfsmount *nmp, struct nfsclds *newdsp,
5851 struct nfsclds **retdspp)
5853 struct nfsclds *dsp, *cur_dsp;
5856 * Search the list of nfsclds structures for one with the same
5860 TAILQ_FOREACH(dsp, &nmp->nm_sess, nfsclds_list) {
5861 if (dsp->nfsclds_servownlen == newdsp->nfsclds_servownlen &&
5862 dsp->nfsclds_servownlen != 0 &&
5863 !NFSBCMP(dsp->nfsclds_serverown, newdsp->nfsclds_serverown,
5864 dsp->nfsclds_servownlen)) {
5865 NFSCL_DEBUG(4, "fnd same fdsp=%p dsp=%p flg=0x%x\n",
5866 TAILQ_FIRST(&nmp->nm_sess), dsp,
5867 dsp->nfsclds_flags);
5868 /* Server major id matches. */
5869 if ((dsp->nfsclds_flags & NFSCLDS_DS) != 0) {
5871 return (NFSDSP_USETHISSESSION);
5875 * Note the first match, so it can be used for
5876 * sequence'ing new sessions.
5878 if (cur_dsp == NULL)
5882 if (cur_dsp != NULL) {
5884 return (NFSDSP_SEQTHISSESSION);
5886 return (NFSDSP_NOTFOUND);
5891 * NFS commit rpc to a DS.
5894 nfsrpc_commitds(vnode_t vp, uint64_t offset, int cnt, struct nfsclds *dsp,
5895 struct nfsfh *fhp, struct ucred *cred, NFSPROC_T *p, void *stuff)
5898 struct nfsrv_descript nfsd, *nd = &nfsd;
5899 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
5900 struct nfssockreq *nrp;
5903 nfscl_reqstart(nd, NFSPROC_COMMITDS, nmp, fhp->nfh_fh, fhp->nfh_len,
5904 NULL, &dsp->nfsclds_sess);
5905 NFSM_BUILD(tl, uint32_t *, NFSX_HYPER + NFSX_UNSIGNED);
5906 txdr_hyper(offset, tl);
5908 *tl = txdr_unsigned(cnt);
5909 nrp = dsp->nfsclds_sockp;
5911 /* If NULL, use the MDS socket. */
5912 nrp = &nmp->nm_sockreq;
5913 error = newnfs_request(nd, nmp, NULL, nrp, vp, p, cred,
5914 NFS_PROG, NFS_VER4, NULL, 1, NULL, &dsp->nfsclds_sess);
5917 if (nd->nd_repstat == 0) {
5918 NFSM_DISSECT(tl, u_int32_t *, NFSX_VERF);
5920 if (NFSBCMP(tl, dsp->nfsclds_verf, NFSX_VERF)) {
5921 NFSBCOPY(tl, dsp->nfsclds_verf, NFSX_VERF);
5922 error = NFSERR_STALEWRITEVERF;
5927 if (error == 0 && nd->nd_repstat != 0)
5928 error = nd->nd_repstat;
5929 mbuf_freem(nd->nd_mrep);