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>
53 extern int nfs_numnfscbd;
54 extern struct timeval nfsboottime;
55 extern u_int32_t newnfs_false, newnfs_true;
56 extern nfstype nfsv34_type[9];
57 extern int nfsrv_useacl;
58 extern char nfsv4_callbackaddr[INET6_ADDRSTRLEN];
59 extern int nfscl_debuglevel;
61 int nfstest_outofseq = 0;
62 int nfscl_assumeposixlocks = 1;
63 int nfscl_enablecallb = 0;
64 short nfsv4_cbport = NFSV4_CBPORT;
65 int nfstest_openallsetattr = 0;
66 #endif /* !APPLEKEXT */
68 #define DIRHDSIZ (sizeof (struct dirent) - (MAXNAMLEN + 1))
70 static int nfsrpc_setattrrpc(vnode_t , struct vattr *, nfsv4stateid_t *,
71 struct ucred *, NFSPROC_T *, struct nfsvattr *, int *, void *);
72 static int nfsrpc_readrpc(vnode_t , struct uio *, struct ucred *,
73 nfsv4stateid_t *, NFSPROC_T *, struct nfsvattr *, int *, void *);
74 static int nfsrpc_writerpc(vnode_t , struct uio *, int *, int *,
75 struct ucred *, nfsv4stateid_t *, NFSPROC_T *, struct nfsvattr *, int *,
77 static int nfsrpc_createv23(vnode_t , char *, int, struct vattr *,
78 nfsquad_t, int, struct ucred *, NFSPROC_T *, struct nfsvattr *,
79 struct nfsvattr *, struct nfsfh **, int *, int *, void *);
80 static int nfsrpc_createv4(vnode_t , char *, int, struct vattr *,
81 nfsquad_t, int, struct nfsclowner *, struct nfscldeleg **, struct ucred *,
82 NFSPROC_T *, struct nfsvattr *, struct nfsvattr *, struct nfsfh **, int *,
83 int *, void *, int *);
84 static int nfsrpc_locku(struct nfsrv_descript *, struct nfsmount *,
85 struct nfscllockowner *, u_int64_t, u_int64_t,
86 u_int32_t, struct ucred *, NFSPROC_T *, int);
87 static int nfsrpc_setaclrpc(vnode_t, struct ucred *, NFSPROC_T *,
88 struct acl *, nfsv4stateid_t *, void *);
91 * nfs null call from vfs.
94 nfsrpc_null(vnode_t vp, struct ucred *cred, NFSPROC_T *p)
97 struct nfsrv_descript nfsd, *nd = &nfsd;
99 NFSCL_REQSTART(nd, NFSPROC_NULL, vp);
100 error = nfscl_request(nd, vp, p, cred, NULL);
101 if (nd->nd_repstat && !error)
102 error = nd->nd_repstat;
103 mbuf_freem(nd->nd_mrep);
109 * For nfs version 3 and 4, use the access rpc to check accessibility. If file
110 * modes are changed on the server, accesses might still fail later.
113 nfsrpc_access(vnode_t vp, int acmode, struct ucred *cred,
114 NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp)
117 u_int32_t mode, rmode;
120 mode = NFSACCESS_READ;
123 if (vnode_vtype(vp) == VDIR) {
125 mode |= (NFSACCESS_MODIFY | NFSACCESS_EXTEND |
128 mode |= NFSACCESS_LOOKUP;
131 mode |= (NFSACCESS_MODIFY | NFSACCESS_EXTEND);
133 mode |= NFSACCESS_EXECUTE;
137 * Now, just call nfsrpc_accessrpc() to do the actual RPC.
139 error = nfsrpc_accessrpc(vp, mode, cred, p, nap, attrflagp, &rmode,
143 * The NFS V3 spec does not clarify whether or not
144 * the returned access bits can be a superset of
145 * the ones requested, so...
147 if (!error && (rmode & mode) != mode)
153 * The actual rpc, separated out for Darwin.
156 nfsrpc_accessrpc(vnode_t vp, u_int32_t mode, struct ucred *cred,
157 NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, u_int32_t *rmodep,
161 u_int32_t supported, rmode;
163 struct nfsrv_descript nfsd, *nd = &nfsd;
164 nfsattrbit_t attrbits;
168 NFSCL_REQSTART(nd, NFSPROC_ACCESS, vp);
169 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
170 *tl = txdr_unsigned(mode);
171 if (nd->nd_flag & ND_NFSV4) {
173 * And do a Getattr op.
175 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
176 *tl = txdr_unsigned(NFSV4OP_GETATTR);
177 NFSGETATTR_ATTRBIT(&attrbits);
178 (void) nfsrv_putattrbit(nd, &attrbits);
180 error = nfscl_request(nd, vp, p, cred, stuff);
183 if (nd->nd_flag & ND_NFSV3) {
184 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
188 if (!nd->nd_repstat) {
189 if (nd->nd_flag & ND_NFSV4) {
190 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
191 supported = fxdr_unsigned(u_int32_t, *tl++);
193 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
195 rmode = fxdr_unsigned(u_int32_t, *tl);
196 if (nd->nd_flag & ND_NFSV4)
197 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
200 * It's not obvious what should be done about
201 * unsupported access modes. For now, be paranoid
202 * and clear the unsupported ones.
207 error = nd->nd_repstat;
209 mbuf_freem(nd->nd_mrep);
217 nfsrpc_open(vnode_t vp, int amode, struct ucred *cred, NFSPROC_T *p)
219 struct nfsclopen *op;
220 struct nfscldeleg *dp;
222 struct nfsnode *np = VTONFS(vp);
223 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
224 u_int32_t mode, clidrev;
225 int ret, newone, error, expireret = 0, retrycnt;
228 * For NFSv4, Open Ops are only done on Regular Files.
230 if (vnode_vtype(vp) != VREG)
234 mode |= NFSV4OPEN_ACCESSREAD;
236 mode |= NFSV4OPEN_ACCESSWRITE;
241 { char name[100]; int namel;
242 namel = (np->n_v4->n4_namelen < 100) ? np->n_v4->n4_namelen : 99;
243 bcopy(NFS4NODENAME(np->n_v4), name, namel);
245 printf("rpcopen p=0x%x name=%s",p->p_pid,name);
246 if (nfhp->nfh_len > 0) printf(" fh=0x%x\n",nfhp->nfh_fh[12]);
247 else printf(" fhl=0\n");
252 error = nfscl_open(vp, nfhp->nfh_fh, nfhp->nfh_len, mode, 1,
253 cred, p, NULL, &op, &newone, &ret, 1);
257 if (nmp->nm_clp != NULL)
258 clidrev = nmp->nm_clp->nfsc_clientidrev;
261 if (ret == NFSCLOPEN_DOOPEN) {
262 if (np->n_v4 != NULL) {
263 error = nfsrpc_openrpc(nmp, vp, np->n_v4->n4_data,
264 np->n_v4->n4_fhlen, np->n_fhp->nfh_fh,
265 np->n_fhp->nfh_len, mode, op,
266 NFS4NODENAME(np->n_v4), np->n_v4->n4_namelen, &dp,
267 0, 0x0, cred, p, 0, 0);
270 OSBitAndAtomic((int32_t)~NDELEGMOD, (UInt32 *)&np->n_flag);
273 np->n_flag &= ~NDELEGMOD;
275 * Invalidate the attribute cache, so that
276 * attributes that pre-date the issue of a
277 * delegation are not cached, since the
278 * cached attributes will remain valid while
279 * the delegation is held.
281 NFSINVALATTRCACHE(np);
284 (void) nfscl_deleg(nmp->nm_mountp,
285 op->nfso_own->nfsow_clp,
286 nfhp->nfh_fh, nfhp->nfh_len, cred, p, &dp);
291 newnfs_copyincred(cred, &op->nfso_cred);
292 } else if (ret == NFSCLOPEN_SETCRED)
294 * This is a new local open on a delegation. It needs
295 * to have credentials so that an open can be done
296 * against the server during recovery.
298 newnfs_copyincred(cred, &op->nfso_cred);
301 * nfso_opencnt is the count of how many VOP_OPEN()s have
302 * been done on this Open successfully and a VOP_CLOSE()
303 * is expected for each of these.
304 * If error is non-zero, don't increment it, since the Open
305 * hasn't succeeded yet.
309 nfscl_openrelease(op, error, newone);
310 if (error == NFSERR_GRACE || error == NFSERR_STALECLIENTID ||
311 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY) {
312 (void) nfs_catnap(PZERO, error, "nfs_open");
313 } else if ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID)
315 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
318 } while (error == NFSERR_GRACE || error == NFSERR_STALECLIENTID ||
319 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
320 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
321 expireret == 0 && clidrev != 0 && retrycnt < 4));
322 if (error && retrycnt >= 4)
328 * the actual open rpc
331 nfsrpc_openrpc(struct nfsmount *nmp, vnode_t vp, u_int8_t *nfhp, int fhlen,
332 u_int8_t *newfhp, int newfhlen, u_int32_t mode, struct nfsclopen *op,
333 u_int8_t *name, int namelen, struct nfscldeleg **dpp,
334 int reclaim, u_int32_t delegtype, struct ucred *cred, NFSPROC_T *p,
335 int syscred, int recursed)
338 struct nfsrv_descript nfsd, *nd = &nfsd;
339 struct nfscldeleg *dp, *ndp = NULL;
340 struct nfsvattr nfsva;
341 u_int32_t rflags, deleg;
342 nfsattrbit_t attrbits;
343 int error, ret, acesize, limitby;
347 nfscl_reqstart(nd, NFSPROC_OPEN, nmp, nfhp, fhlen, NULL);
348 NFSM_BUILD(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
349 *tl++ = txdr_unsigned(op->nfso_own->nfsow_seqid);
350 *tl++ = txdr_unsigned(mode & NFSV4OPEN_ACCESSBOTH);
351 *tl++ = txdr_unsigned((mode >> NFSLCK_SHIFT) & NFSV4OPEN_DENYBOTH);
352 *tl++ = op->nfso_own->nfsow_clp->nfsc_clientid.lval[0];
353 *tl = op->nfso_own->nfsow_clp->nfsc_clientid.lval[1];
354 (void) nfsm_strtom(nd, op->nfso_own->nfsow_owner, NFSV4CL_LOCKNAMELEN);
355 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
356 *tl++ = txdr_unsigned(NFSV4OPEN_NOCREATE);
358 *tl = txdr_unsigned(NFSV4OPEN_CLAIMPREVIOUS);
359 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
360 *tl = txdr_unsigned(delegtype);
363 *tl = txdr_unsigned(NFSV4OPEN_CLAIMDELEGATECUR);
364 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID);
365 *tl++ = dp->nfsdl_stateid.seqid;
366 *tl++ = dp->nfsdl_stateid.other[0];
367 *tl++ = dp->nfsdl_stateid.other[1];
368 *tl = dp->nfsdl_stateid.other[2];
370 *tl = txdr_unsigned(NFSV4OPEN_CLAIMNULL);
372 (void) nfsm_strtom(nd, name, namelen);
374 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
375 *tl = txdr_unsigned(NFSV4OP_GETATTR);
376 NFSZERO_ATTRBIT(&attrbits);
377 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_CHANGE);
378 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEMODIFY);
379 (void) nfsrv_putattrbit(nd, &attrbits);
381 nd->nd_flag |= ND_USEGSSNAME;
382 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, vp, p, cred,
383 NFS_PROG, NFS_VER4, NULL, 1, NULL);
386 NFSCL_INCRSEQID(op->nfso_own->nfsow_seqid, nd);
387 if (!nd->nd_repstat) {
388 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
390 op->nfso_stateid.seqid = *tl++;
391 op->nfso_stateid.other[0] = *tl++;
392 op->nfso_stateid.other[1] = *tl++;
393 op->nfso_stateid.other[2] = *tl;
394 rflags = fxdr_unsigned(u_int32_t, *(tl + 6));
395 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
398 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
399 deleg = fxdr_unsigned(u_int32_t, *tl);
400 if (deleg == NFSV4OPEN_DELEGATEREAD ||
401 deleg == NFSV4OPEN_DELEGATEWRITE) {
402 if (!(op->nfso_own->nfsow_clp->nfsc_flags &
403 NFSCLFLAGS_FIRSTDELEG))
404 op->nfso_own->nfsow_clp->nfsc_flags |=
405 (NFSCLFLAGS_FIRSTDELEG | NFSCLFLAGS_GOTDELEG);
406 MALLOC(ndp, struct nfscldeleg *,
407 sizeof (struct nfscldeleg) + newfhlen,
408 M_NFSCLDELEG, M_WAITOK);
409 LIST_INIT(&ndp->nfsdl_owner);
410 LIST_INIT(&ndp->nfsdl_lock);
411 ndp->nfsdl_clp = op->nfso_own->nfsow_clp;
412 ndp->nfsdl_fhlen = newfhlen;
413 NFSBCOPY(newfhp, ndp->nfsdl_fh, newfhlen);
414 newnfs_copyincred(cred, &ndp->nfsdl_cred);
415 nfscl_lockinit(&ndp->nfsdl_rwlock);
416 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
418 ndp->nfsdl_stateid.seqid = *tl++;
419 ndp->nfsdl_stateid.other[0] = *tl++;
420 ndp->nfsdl_stateid.other[1] = *tl++;
421 ndp->nfsdl_stateid.other[2] = *tl++;
422 ret = fxdr_unsigned(int, *tl);
423 if (deleg == NFSV4OPEN_DELEGATEWRITE) {
424 ndp->nfsdl_flags = NFSCLDL_WRITE;
426 * Indicates how much the file can grow.
428 NFSM_DISSECT(tl, u_int32_t *,
430 limitby = fxdr_unsigned(int, *tl++);
432 case NFSV4OPEN_LIMITSIZE:
433 ndp->nfsdl_sizelimit = fxdr_hyper(tl);
435 case NFSV4OPEN_LIMITBLOCKS:
436 ndp->nfsdl_sizelimit =
437 fxdr_unsigned(u_int64_t, *tl++);
438 ndp->nfsdl_sizelimit *=
439 fxdr_unsigned(u_int64_t, *tl);
442 error = NFSERR_BADXDR;
446 ndp->nfsdl_flags = NFSCLDL_READ;
449 ndp->nfsdl_flags |= NFSCLDL_RECALL;
450 error = nfsrv_dissectace(nd, &ndp->nfsdl_ace, &ret,
454 } else if (deleg != NFSV4OPEN_DELEGATENONE) {
455 error = NFSERR_BADXDR;
458 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
459 error = nfsv4_loadattr(nd, NULL, &nfsva, NULL,
460 NULL, 0, NULL, NULL, NULL, NULL, NULL, 0,
461 NULL, NULL, NULL, p, cred);
465 ndp->nfsdl_change = nfsva.na_filerev;
466 ndp->nfsdl_modtime = nfsva.na_mtime;
467 ndp->nfsdl_flags |= NFSCLDL_MODTIMESET;
469 if (!reclaim && (rflags & NFSV4OPEN_RESULTCONFIRM)) {
471 ret = nfsrpc_openconfirm(vp, newfhp, newfhlen, op,
473 if (ret == NFSERR_DELAY)
474 (void) nfs_catnap(PZERO, ret, "nfs_open");
475 } while (ret == NFSERR_DELAY);
478 if ((rflags & NFSV4OPEN_LOCKTYPEPOSIX) ||
479 nfscl_assumeposixlocks)
480 op->nfso_posixlock = 1;
482 op->nfso_posixlock = 0;
485 * If the server is handing out delegations, but we didn't
486 * get one because an OpenConfirm was required, try the
487 * Open again, to get a delegation. This is a harmless no-op,
488 * from a server's point of view.
490 if (!reclaim && (rflags & NFSV4OPEN_RESULTCONFIRM) &&
491 (op->nfso_own->nfsow_clp->nfsc_flags & NFSCLFLAGS_GOTDELEG)
492 && !error && dp == NULL && ndp == NULL && !recursed) {
494 ret = nfsrpc_openrpc(nmp, vp, nfhp, fhlen, newfhp,
495 newfhlen, mode, op, name, namelen, &ndp, 0, 0x0,
496 cred, p, syscred, 1);
497 if (ret == NFSERR_DELAY)
498 (void) nfs_catnap(PZERO, ret, "nfs_open2");
499 } while (ret == NFSERR_DELAY);
502 FREE((caddr_t)ndp, M_NFSCLDELEG);
503 if (ret == NFSERR_STALECLIENTID ||
504 ret == NFSERR_STALEDONTRECOVER)
509 if (nd->nd_repstat != 0 && error == 0)
510 error = nd->nd_repstat;
511 if (error == NFSERR_STALECLIENTID)
512 nfscl_initiate_recovery(op->nfso_own->nfsow_clp);
516 else if (ndp != NULL)
517 FREE((caddr_t)ndp, M_NFSCLDELEG);
518 mbuf_freem(nd->nd_mrep);
526 nfsrpc_opendowngrade(vnode_t vp, u_int32_t mode, struct nfsclopen *op,
527 struct ucred *cred, NFSPROC_T *p)
530 struct nfsrv_descript nfsd, *nd = &nfsd;
533 NFSCL_REQSTART(nd, NFSPROC_OPENDOWNGRADE, vp);
534 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID + 3 * NFSX_UNSIGNED);
535 *tl++ = op->nfso_stateid.seqid;
536 *tl++ = op->nfso_stateid.other[0];
537 *tl++ = op->nfso_stateid.other[1];
538 *tl++ = op->nfso_stateid.other[2];
539 *tl++ = txdr_unsigned(op->nfso_own->nfsow_seqid);
540 *tl++ = txdr_unsigned(mode & NFSV4OPEN_ACCESSBOTH);
541 *tl = txdr_unsigned((mode >> NFSLCK_SHIFT) & NFSV4OPEN_DENYBOTH);
542 error = nfscl_request(nd, vp, p, cred, NULL);
545 NFSCL_INCRSEQID(op->nfso_own->nfsow_seqid, nd);
546 if (!nd->nd_repstat) {
547 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID);
548 op->nfso_stateid.seqid = *tl++;
549 op->nfso_stateid.other[0] = *tl++;
550 op->nfso_stateid.other[1] = *tl++;
551 op->nfso_stateid.other[2] = *tl;
553 if (nd->nd_repstat && error == 0)
554 error = nd->nd_repstat;
555 if (error == NFSERR_STALESTATEID)
556 nfscl_initiate_recovery(op->nfso_own->nfsow_clp);
558 mbuf_freem(nd->nd_mrep);
563 * V4 Close operation.
566 nfsrpc_close(vnode_t vp, int doclose, NFSPROC_T *p)
568 struct nfsclclient *clp;
571 if (vnode_vtype(vp) != VREG)
574 error = nfscl_doclose(vp, &clp, p);
576 error = nfscl_getclose(vp, &clp);
580 nfscl_clientrelease(clp);
588 nfsrpc_doclose(struct nfsmount *nmp, struct nfsclopen *op, NFSPROC_T *p)
590 struct nfsrv_descript nfsd, *nd = &nfsd;
591 struct nfscllockowner *lp, *nlp;
592 struct nfscllock *lop, *nlop;
594 u_int64_t off = 0, len = 0;
595 u_int32_t type = NFSV4LOCKT_READ;
596 int error, do_unlock, trycnt;
598 tcred = newnfs_getcred();
599 newnfs_copycred(&op->nfso_cred, tcred);
601 * (Theoretically this could be done in the same
602 * compound as the close, but having multiple
603 * sequenced Ops in the same compound might be
604 * too scary for some servers.)
606 if (op->nfso_posixlock) {
609 type = NFSV4LOCKT_READ;
613 * Since this function is only called from VOP_INACTIVE(), no
614 * other thread will be manipulating this Open. As such, the
615 * lock lists are not being changed by other threads, so it should
616 * be safe to do this without locking.
618 LIST_FOREACH(lp, &op->nfso_lock, nfsl_list) {
620 LIST_FOREACH_SAFE(lop, &lp->nfsl_lock, nfslo_list, nlop) {
621 if (op->nfso_posixlock == 0) {
622 off = lop->nfslo_first;
623 len = lop->nfslo_end - lop->nfslo_first;
624 if (lop->nfslo_type == F_WRLCK)
625 type = NFSV4LOCKT_WRITE;
627 type = NFSV4LOCKT_READ;
632 error = nfsrpc_locku(nd, nmp, lp, off,
633 len, type, tcred, p, 0);
634 if ((nd->nd_repstat == NFSERR_GRACE ||
635 nd->nd_repstat == NFSERR_DELAY) &&
637 (void) nfs_catnap(PZERO,
640 } while ((nd->nd_repstat == NFSERR_GRACE ||
641 nd->nd_repstat == NFSERR_DELAY) &&
642 error == 0 && trycnt++ < 5);
643 if (op->nfso_posixlock)
646 nfscl_freelock(lop, 0);
649 * Do a ReleaseLockOwner.
650 * The lock owner name nfsl_owner may be used by other opens for
651 * other files but the lock_owner4 name that nfsrpc_rellockown()
652 * puts on the wire has the file handle for this file appended
653 * to it, so it can be done now.
655 (void)nfsrpc_rellockown(nmp, lp, lp->nfsl_open->nfso_fh,
656 lp->nfsl_open->nfso_fhlen, tcred, p);
660 * There could be other Opens for different files on the same
661 * OpenOwner, so locking is required.
664 nfscl_lockexcl(&op->nfso_own->nfsow_rwlock, NFSCLSTATEMUTEXPTR);
667 error = nfscl_tryclose(op, tcred, nmp, p);
668 if (error == NFSERR_GRACE)
669 (void) nfs_catnap(PZERO, error, "nfs_close");
670 } while (error == NFSERR_GRACE);
672 nfscl_lockunlock(&op->nfso_own->nfsow_rwlock);
674 LIST_FOREACH_SAFE(lp, &op->nfso_lock, nfsl_list, nlp)
675 nfscl_freelockowner(lp, 0);
676 nfscl_freeopen(op, 0);
682 * The actual Close RPC.
685 nfsrpc_closerpc(struct nfsrv_descript *nd, struct nfsmount *nmp,
686 struct nfsclopen *op, struct ucred *cred, NFSPROC_T *p,
692 nfscl_reqstart(nd, NFSPROC_CLOSE, nmp, op->nfso_fh,
693 op->nfso_fhlen, NULL);
694 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED + NFSX_STATEID);
695 *tl++ = txdr_unsigned(op->nfso_own->nfsow_seqid);
696 *tl++ = op->nfso_stateid.seqid;
697 *tl++ = op->nfso_stateid.other[0];
698 *tl++ = op->nfso_stateid.other[1];
699 *tl = op->nfso_stateid.other[2];
701 nd->nd_flag |= ND_USEGSSNAME;
702 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
703 NFS_PROG, NFS_VER4, NULL, 1, NULL);
706 NFSCL_INCRSEQID(op->nfso_own->nfsow_seqid, nd);
707 if (nd->nd_repstat == 0)
708 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID);
709 error = nd->nd_repstat;
710 if (error == NFSERR_STALESTATEID)
711 nfscl_initiate_recovery(op->nfso_own->nfsow_clp);
713 mbuf_freem(nd->nd_mrep);
718 * V4 Open Confirm RPC.
721 nfsrpc_openconfirm(vnode_t vp, u_int8_t *nfhp, int fhlen,
722 struct nfsclopen *op, struct ucred *cred, NFSPROC_T *p)
725 struct nfsrv_descript nfsd, *nd = &nfsd;
728 nfscl_reqstart(nd, NFSPROC_OPENCONFIRM, VFSTONFS(vnode_mount(vp)),
730 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED + NFSX_STATEID);
731 *tl++ = op->nfso_stateid.seqid;
732 *tl++ = op->nfso_stateid.other[0];
733 *tl++ = op->nfso_stateid.other[1];
734 *tl++ = op->nfso_stateid.other[2];
735 *tl = txdr_unsigned(op->nfso_own->nfsow_seqid);
736 error = nfscl_request(nd, vp, p, cred, NULL);
739 NFSCL_INCRSEQID(op->nfso_own->nfsow_seqid, nd);
740 if (!nd->nd_repstat) {
741 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID);
742 op->nfso_stateid.seqid = *tl++;
743 op->nfso_stateid.other[0] = *tl++;
744 op->nfso_stateid.other[1] = *tl++;
745 op->nfso_stateid.other[2] = *tl;
747 error = nd->nd_repstat;
748 if (error == NFSERR_STALESTATEID)
749 nfscl_initiate_recovery(op->nfso_own->nfsow_clp);
751 mbuf_freem(nd->nd_mrep);
756 * Do the setclientid and setclientid confirm RPCs. Called from nfs_statfs()
757 * when a mount has just occurred and when the server replies NFSERR_EXPIRED.
760 nfsrpc_setclient(struct nfsmount *nmp, struct nfsclclient *clp,
761 struct ucred *cred, NFSPROC_T *p)
764 struct nfsrv_descript nfsd;
765 struct nfsrv_descript *nd = &nfsd;
766 nfsattrbit_t attrbits;
767 u_int8_t *cp = NULL, *cp2, addr[INET6_ADDRSTRLEN + 9];
769 int error, isinet6 = 0, callblen;
772 static u_int32_t rev = 0;
774 if (nfsboottime.tv_sec == 0)
775 NFSSETBOOTTIME(nfsboottime);
776 nfscl_reqstart(nd, NFSPROC_SETCLIENTID, nmp, NULL, 0, NULL);
777 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
778 *tl++ = txdr_unsigned(nfsboottime.tv_sec);
779 *tl = txdr_unsigned(rev++);
780 (void) nfsm_strtom(nd, clp->nfsc_id, clp->nfsc_idlen);
783 * set up the callback address
785 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
786 *tl = txdr_unsigned(NFS_CALLBCKPROG);
787 callblen = strlen(nfsv4_callbackaddr);
789 cp = nfscl_getmyip(nmp, &isinet6);
790 if (nfscl_enablecallb && nfs_numnfscbd > 0 &&
791 (callblen > 0 || cp != NULL)) {
792 port = htons(nfsv4_cbport);
793 cp2 = (u_int8_t *)&port;
796 strchr(nfsv4_callbackaddr, ':')) || isinet6) {
797 char ip6buf[INET6_ADDRSTRLEN], *ip6add;
799 (void) nfsm_strtom(nd, "tcp6", 4);
801 ip6_sprintf(ip6buf, (struct in6_addr *)cp);
804 ip6add = nfsv4_callbackaddr;
806 snprintf(addr, INET6_ADDRSTRLEN + 9, "%s.%d.%d",
807 ip6add, cp2[0], cp2[1]);
811 (void) nfsm_strtom(nd, "tcp", 3);
813 snprintf(addr, INET6_ADDRSTRLEN + 9,
814 "%d.%d.%d.%d.%d.%d", cp[0], cp[1],
815 cp[2], cp[3], cp2[0], cp2[1]);
817 snprintf(addr, INET6_ADDRSTRLEN + 9,
818 "%s.%d.%d", nfsv4_callbackaddr,
821 (void) nfsm_strtom(nd, addr, strlen(addr));
823 (void) nfsm_strtom(nd, "tcp", 3);
824 (void) nfsm_strtom(nd, "0.0.0.0.0.0", 11);
826 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
827 *tl = txdr_unsigned(clp->nfsc_cbident);
828 nd->nd_flag |= ND_USEGSSNAME;
829 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
830 NFS_PROG, NFS_VER4, NULL, 1, NULL);
833 if (nd->nd_repstat == 0) {
834 NFSM_DISSECT(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
835 clp->nfsc_clientid.lval[0] = *tl++;
836 clp->nfsc_clientid.lval[1] = *tl++;
837 confirm.lval[0] = *tl++;
838 confirm.lval[1] = *tl;
839 mbuf_freem(nd->nd_mrep);
845 nfscl_reqstart(nd, NFSPROC_SETCLIENTIDCFRM, nmp, NULL, 0, NULL);
846 NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
847 *tl++ = clp->nfsc_clientid.lval[0];
848 *tl++ = clp->nfsc_clientid.lval[1];
849 *tl++ = confirm.lval[0];
850 *tl = confirm.lval[1];
851 nd->nd_flag |= ND_USEGSSNAME;
852 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p,
853 cred, NFS_PROG, NFS_VER4, NULL, 1, NULL);
856 mbuf_freem(nd->nd_mrep);
858 if (nd->nd_repstat == 0) {
859 nfscl_reqstart(nd, NFSPROC_GETATTR, nmp, nmp->nm_fh,
860 nmp->nm_fhsize, NULL);
861 NFSZERO_ATTRBIT(&attrbits);
862 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_LEASETIME);
863 (void) nfsrv_putattrbit(nd, &attrbits);
864 nd->nd_flag |= ND_USEGSSNAME;
865 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p,
866 cred, NFS_PROG, NFS_VER4, NULL, 1, NULL);
869 if (nd->nd_repstat == 0) {
870 error = nfsv4_loadattr(nd, NULL, NULL, NULL, NULL, 0, NULL,
871 NULL, NULL, NULL, NULL, 0, NULL, &lease, NULL, p, cred);
874 clp->nfsc_renew = NFSCL_RENEW(lease);
875 clp->nfsc_expire = NFSD_MONOSEC + clp->nfsc_renew;
876 clp->nfsc_clientidrev++;
877 if (clp->nfsc_clientidrev == 0)
878 clp->nfsc_clientidrev++;
882 error = nd->nd_repstat;
884 mbuf_freem(nd->nd_mrep);
892 nfsrpc_getattr(vnode_t vp, struct ucred *cred, NFSPROC_T *p,
893 struct nfsvattr *nap, void *stuff)
895 struct nfsrv_descript nfsd, *nd = &nfsd;
897 nfsattrbit_t attrbits;
899 NFSCL_REQSTART(nd, NFSPROC_GETATTR, vp);
900 if (nd->nd_flag & ND_NFSV4) {
901 NFSGETATTR_ATTRBIT(&attrbits);
902 (void) nfsrv_putattrbit(nd, &attrbits);
904 error = nfscl_request(nd, vp, p, cred, stuff);
908 error = nfsm_loadattr(nd, nap);
910 error = nd->nd_repstat;
911 mbuf_freem(nd->nd_mrep);
916 * nfs getattr call with non-vnode arguemnts.
919 nfsrpc_getattrnovp(struct nfsmount *nmp, u_int8_t *fhp, int fhlen, int syscred,
920 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, u_int64_t *xidp)
922 struct nfsrv_descript nfsd, *nd = &nfsd;
923 int error, vers = NFS_VER2;
924 nfsattrbit_t attrbits;
926 nfscl_reqstart(nd, NFSPROC_GETATTR, nmp, fhp, fhlen, NULL);
927 if (nd->nd_flag & ND_NFSV4) {
929 NFSGETATTR_ATTRBIT(&attrbits);
930 (void) nfsrv_putattrbit(nd, &attrbits);
931 } else if (nd->nd_flag & ND_NFSV3) {
935 nd->nd_flag |= ND_USEGSSNAME;
936 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
937 NFS_PROG, vers, NULL, 1, xidp);
941 error = nfsm_loadattr(nd, nap);
943 error = nd->nd_repstat;
944 mbuf_freem(nd->nd_mrep);
949 * Do an nfs setattr operation.
952 nfsrpc_setattr(vnode_t vp, struct vattr *vap, NFSACL_T *aclp,
953 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *rnap, int *attrflagp,
956 int error, expireret = 0, openerr, retrycnt;
957 u_int32_t clidrev = 0, mode;
958 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
960 nfsv4stateid_t stateid;
963 if (nmp->nm_clp != NULL)
964 clidrev = nmp->nm_clp->nfsc_clientidrev;
965 if (vap != NULL && NFSATTRISSET(u_quad_t, vap, va_size))
966 mode = NFSV4OPEN_ACCESSWRITE;
968 mode = NFSV4OPEN_ACCESSREAD;
973 if (NFSHASNFSV4(nmp)) {
974 nfhp = VTONFS(vp)->n_fhp;
975 error = nfscl_getstateid(vp, nfhp->nfh_fh,
976 nfhp->nfh_len, mode, cred, p, &stateid, &lckp);
977 if (error && vnode_vtype(vp) == VREG &&
978 (mode == NFSV4OPEN_ACCESSWRITE ||
979 nfstest_openallsetattr)) {
981 * No Open stateid, so try and open the file
984 if (mode == NFSV4OPEN_ACCESSWRITE)
985 openerr = nfsrpc_open(vp, FWRITE, cred,
988 openerr = nfsrpc_open(vp, FREAD, cred,
991 (void) nfscl_getstateid(vp,
992 nfhp->nfh_fh, nfhp->nfh_len,
993 mode, cred, p, &stateid, &lckp);
997 error = nfsrpc_setattrrpc(vp, vap, &stateid, cred, p,
998 rnap, attrflagp, stuff);
1000 error = nfsrpc_setaclrpc(vp, cred, p, aclp, &stateid,
1002 if (error == NFSERR_STALESTATEID)
1003 nfscl_initiate_recovery(nmp->nm_clp);
1005 nfscl_lockderef(lckp);
1007 (void) nfsrpc_close(vp, 0, p);
1008 if (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
1009 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
1010 error == NFSERR_OLDSTATEID) {
1011 (void) nfs_catnap(PZERO, error, "nfs_setattr");
1012 } else if ((error == NFSERR_EXPIRED ||
1013 error == NFSERR_BADSTATEID) && clidrev != 0) {
1014 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
1017 } while (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
1018 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
1019 (error == NFSERR_OLDSTATEID && retrycnt < 20) ||
1020 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
1021 expireret == 0 && clidrev != 0 && retrycnt < 4));
1022 if (error && retrycnt >= 4)
1028 nfsrpc_setattrrpc(vnode_t vp, struct vattr *vap,
1029 nfsv4stateid_t *stateidp, struct ucred *cred, NFSPROC_T *p,
1030 struct nfsvattr *rnap, int *attrflagp, void *stuff)
1033 struct nfsrv_descript nfsd, *nd = &nfsd;
1035 nfsattrbit_t attrbits;
1038 NFSCL_REQSTART(nd, NFSPROC_SETATTR, vp);
1039 if (nd->nd_flag & ND_NFSV4)
1040 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID);
1041 vap->va_type = vnode_vtype(vp);
1042 nfscl_fillsattr(nd, vap, vp, NFSSATTR_FULL, 0);
1043 if (nd->nd_flag & ND_NFSV3) {
1044 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1046 } else if (nd->nd_flag & ND_NFSV4) {
1047 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1048 *tl = txdr_unsigned(NFSV4OP_GETATTR);
1049 NFSGETATTR_ATTRBIT(&attrbits);
1050 (void) nfsrv_putattrbit(nd, &attrbits);
1052 error = nfscl_request(nd, vp, p, cred, stuff);
1055 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4))
1056 error = nfscl_wcc_data(nd, vp, rnap, attrflagp, NULL, stuff);
1057 if ((nd->nd_flag & ND_NFSV4) && !error)
1058 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
1059 if (!(nd->nd_flag & ND_NFSV3) && !nd->nd_repstat && !error)
1060 error = nfscl_postop_attr(nd, rnap, attrflagp, stuff);
1061 mbuf_freem(nd->nd_mrep);
1062 if (nd->nd_repstat && !error)
1063 error = nd->nd_repstat;
1071 nfsrpc_lookup(vnode_t dvp, char *name, int len, struct ucred *cred,
1072 NFSPROC_T *p, struct nfsvattr *dnap, struct nfsvattr *nap,
1073 struct nfsfh **nfhpp, int *attrflagp, int *dattrflagp, void *stuff)
1076 struct nfsrv_descript nfsd, *nd = &nfsd;
1077 struct nfsmount *nmp;
1080 nfsattrbit_t attrbits;
1081 int error = 0, lookupp = 0;
1085 if (vnode_vtype(dvp) != VDIR)
1087 nmp = VFSTONFS(vnode_mount(dvp));
1088 if (len > NFS_MAXNAMLEN)
1089 return (ENAMETOOLONG);
1090 if (NFSHASNFSV4(nmp) && len == 1 &&
1093 * Just return the current dir's fh.
1096 MALLOC(nfhp, struct nfsfh *, sizeof (struct nfsfh) +
1097 np->n_fhp->nfh_len, M_NFSFH, M_WAITOK);
1098 nfhp->nfh_len = np->n_fhp->nfh_len;
1099 NFSBCOPY(np->n_fhp->nfh_fh, nfhp->nfh_fh, nfhp->nfh_len);
1103 if (NFSHASNFSV4(nmp) && len == 2 &&
1104 name[0] == '.' && name[1] == '.') {
1106 NFSCL_REQSTART(nd, NFSPROC_LOOKUPP, dvp);
1108 NFSCL_REQSTART(nd, NFSPROC_LOOKUP, dvp);
1109 (void) nfsm_strtom(nd, name, len);
1111 if (nd->nd_flag & ND_NFSV4) {
1112 NFSGETATTR_ATTRBIT(&attrbits);
1113 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1114 *tl++ = txdr_unsigned(NFSV4OP_GETFH);
1115 *tl = txdr_unsigned(NFSV4OP_GETATTR);
1116 (void) nfsrv_putattrbit(nd, &attrbits);
1118 error = nfscl_request(nd, dvp, p, cred, stuff);
1121 if (nd->nd_repstat) {
1123 * When an NFSv4 Lookupp returns ENOENT, it means that
1124 * the lookup is at the root of an fs, so return this dir.
1126 if (nd->nd_repstat == NFSERR_NOENT && lookupp) {
1128 MALLOC(nfhp, struct nfsfh *, sizeof (struct nfsfh) +
1129 np->n_fhp->nfh_len, M_NFSFH, M_WAITOK);
1130 nfhp->nfh_len = np->n_fhp->nfh_len;
1131 NFSBCOPY(np->n_fhp->nfh_fh, nfhp->nfh_fh, nfhp->nfh_len);
1133 mbuf_freem(nd->nd_mrep);
1136 if (nd->nd_flag & ND_NFSV3)
1137 error = nfscl_postop_attr(nd, dnap, dattrflagp, stuff);
1140 if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) == ND_NFSV4) {
1141 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1143 nd->nd_flag |= ND_NOMOREDATA;
1147 error = nfsm_getfh(nd, nfhpp);
1151 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
1152 if ((nd->nd_flag & ND_NFSV3) && !error)
1153 error = nfscl_postop_attr(nd, dnap, dattrflagp, stuff);
1155 mbuf_freem(nd->nd_mrep);
1156 if (!error && nd->nd_repstat)
1157 error = nd->nd_repstat;
1162 * Do a readlink rpc.
1165 nfsrpc_readlink(vnode_t vp, struct uio *uiop, struct ucred *cred,
1166 NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, void *stuff)
1169 struct nfsrv_descript nfsd, *nd = &nfsd;
1170 struct nfsnode *np = VTONFS(vp);
1171 nfsattrbit_t attrbits;
1172 int error, len, cangetattr = 1;
1175 NFSCL_REQSTART(nd, NFSPROC_READLINK, vp);
1176 if (nd->nd_flag & ND_NFSV4) {
1178 * And do a Getattr op.
1180 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1181 *tl = txdr_unsigned(NFSV4OP_GETATTR);
1182 NFSGETATTR_ATTRBIT(&attrbits);
1183 (void) nfsrv_putattrbit(nd, &attrbits);
1185 error = nfscl_request(nd, vp, p, cred, stuff);
1188 if (nd->nd_flag & ND_NFSV3)
1189 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
1190 if (!nd->nd_repstat && !error) {
1191 NFSM_STRSIZ(len, NFS_MAXPATHLEN);
1193 * This seems weird to me, but must have been added to
1194 * FreeBSD for some reason. The only thing I can think of
1195 * is that there was/is some server that replies with
1196 * more link data than it should?
1198 if (len == NFS_MAXPATHLEN) {
1200 if (np->n_size > 0 && np->n_size < NFS_MAXPATHLEN) {
1206 error = nfsm_mbufuio(nd, uiop, len);
1207 if ((nd->nd_flag & ND_NFSV4) && !error && cangetattr)
1208 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
1210 if (nd->nd_repstat && !error)
1211 error = nd->nd_repstat;
1213 mbuf_freem(nd->nd_mrep);
1221 nfsrpc_read(vnode_t vp, struct uio *uiop, struct ucred *cred,
1222 NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, void *stuff)
1224 int error, expireret = 0, retrycnt;
1225 u_int32_t clidrev = 0;
1226 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
1227 struct nfsnode *np = VTONFS(vp);
1228 struct ucred *newcred;
1229 struct nfsfh *nfhp = NULL;
1230 nfsv4stateid_t stateid;
1233 if (nmp->nm_clp != NULL)
1234 clidrev = nmp->nm_clp->nfsc_clientidrev;
1236 if (NFSHASNFSV4(nmp)) {
1238 newcred = NFSNEWCRED(cred);
1243 if (NFSHASNFSV4(nmp))
1244 (void)nfscl_getstateid(vp, nfhp->nfh_fh, nfhp->nfh_len,
1245 NFSV4OPEN_ACCESSREAD, newcred, p, &stateid, &lckp);
1246 error = nfsrpc_readrpc(vp, uiop, newcred, &stateid, p, nap,
1248 if (error == NFSERR_STALESTATEID)
1249 nfscl_initiate_recovery(nmp->nm_clp);
1251 nfscl_lockderef(lckp);
1252 if (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
1253 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
1254 error == NFSERR_OLDSTATEID) {
1255 (void) nfs_catnap(PZERO, error, "nfs_read");
1256 } else if ((error == NFSERR_EXPIRED ||
1257 error == NFSERR_BADSTATEID) && clidrev != 0) {
1258 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
1261 } while (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
1262 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
1263 (error == NFSERR_OLDSTATEID && retrycnt < 20) ||
1264 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
1265 expireret == 0 && clidrev != 0 && retrycnt < 4));
1266 if (error && retrycnt >= 4)
1268 if (NFSHASNFSV4(nmp))
1269 NFSFREECRED(newcred);
1274 * The actual read RPC.
1277 nfsrpc_readrpc(vnode_t vp, struct uio *uiop, struct ucred *cred,
1278 nfsv4stateid_t *stateidp, NFSPROC_T *p, struct nfsvattr *nap,
1279 int *attrflagp, void *stuff)
1282 int error = 0, len, retlen, tsiz, eof = 0;
1283 struct nfsrv_descript nfsd;
1284 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
1285 struct nfsrv_descript *nd = &nfsd;
1290 tsiz = uio_uio_resid(uiop);
1291 tmp_off = uiop->uio_offset + tsiz;
1293 if (tmp_off > nmp->nm_maxfilesize || tmp_off < uiop->uio_offset) {
1297 rsize = nmp->nm_rsize;
1302 len = (tsiz > rsize) ? rsize : tsiz;
1303 NFSCL_REQSTART(nd, NFSPROC_READ, vp);
1304 if (nd->nd_flag & ND_NFSV4)
1305 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID);
1306 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED * 3);
1307 if (nd->nd_flag & ND_NFSV2) {
1308 *tl++ = txdr_unsigned(uiop->uio_offset);
1309 *tl++ = txdr_unsigned(len);
1312 txdr_hyper(uiop->uio_offset, tl);
1313 *(tl + 2) = txdr_unsigned(len);
1316 * Since I can't do a Getattr for NFSv4 for Write, there
1317 * doesn't seem any point in doing one here, either.
1318 * (See the comment in nfsrpc_writerpc() for more info.)
1320 error = nfscl_request(nd, vp, p, cred, stuff);
1323 if (nd->nd_flag & ND_NFSV3) {
1324 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
1325 } else if (!nd->nd_repstat && (nd->nd_flag & ND_NFSV2)) {
1326 error = nfsm_loadattr(nd, nap);
1330 if (nd->nd_repstat || error) {
1332 error = nd->nd_repstat;
1335 if (nd->nd_flag & ND_NFSV3) {
1336 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1337 eof = fxdr_unsigned(int, *(tl + 1));
1338 } else if (nd->nd_flag & ND_NFSV4) {
1339 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
1340 eof = fxdr_unsigned(int, *tl);
1342 NFSM_STRSIZ(retlen, len);
1343 error = nfsm_mbufuio(nd, uiop, retlen);
1346 mbuf_freem(nd->nd_mrep);
1349 if (!(nd->nd_flag & ND_NFSV2)) {
1350 if (eof || retlen == 0)
1352 } else if (retlen < len)
1357 if (nd->nd_mrep != NULL)
1358 mbuf_freem(nd->nd_mrep);
1363 * nfs write operation
1364 * When called_from_strategy != 0, it should return EIO for an error that
1365 * indicates recovery is in progress, so that the buffer will be left
1366 * dirty and be written back to the server later. If it loops around,
1367 * the recovery thread could get stuck waiting for the buffer and recovery
1368 * will then deadlock.
1371 nfsrpc_write(vnode_t vp, struct uio *uiop, int *iomode, int *must_commit,
1372 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp,
1373 void *stuff, int called_from_strategy)
1375 int error, expireret = 0, retrycnt, nostateid;
1376 u_int32_t clidrev = 0;
1377 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
1378 struct nfsnode *np = VTONFS(vp);
1379 struct ucred *newcred;
1380 struct nfsfh *nfhp = NULL;
1381 nfsv4stateid_t stateid;
1385 if (nmp->nm_clp != NULL)
1386 clidrev = nmp->nm_clp->nfsc_clientidrev;
1388 if (NFSHASNFSV4(nmp)) {
1389 newcred = NFSNEWCRED(cred);
1396 if (NFSHASNFSV4(nmp)) {
1397 (void)nfscl_getstateid(vp, nfhp->nfh_fh, nfhp->nfh_len,
1398 NFSV4OPEN_ACCESSWRITE, newcred, p, &stateid, &lckp);
1399 if (stateid.other[0] == 0 && stateid.other[1] == 0 &&
1400 stateid.other[2] == 0) {
1402 NFSCL_DEBUG(1, "stateid0 in write\n");
1407 * If there is no stateid for NFSv4, it means this is an
1408 * extraneous write after close. Basically a poorly
1409 * implemented buffer cache. Just don't do the write.
1414 error = nfsrpc_writerpc(vp, uiop, iomode, must_commit,
1415 newcred, &stateid, p, nap, attrflagp, stuff);
1416 if (error == NFSERR_STALESTATEID)
1417 nfscl_initiate_recovery(nmp->nm_clp);
1419 nfscl_lockderef(lckp);
1420 if (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
1421 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
1422 error == NFSERR_OLDSTATEID) {
1423 (void) nfs_catnap(PZERO, error, "nfs_write");
1424 } else if ((error == NFSERR_EXPIRED ||
1425 error == NFSERR_BADSTATEID) && clidrev != 0) {
1426 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
1429 } while (error == NFSERR_GRACE || error == NFSERR_DELAY ||
1430 ((error == NFSERR_STALESTATEID ||
1431 error == NFSERR_STALEDONTRECOVER) && called_from_strategy == 0) ||
1432 (error == NFSERR_OLDSTATEID && retrycnt < 20) ||
1433 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
1434 expireret == 0 && clidrev != 0 && retrycnt < 4));
1435 if (error != 0 && (retrycnt >= 4 ||
1436 ((error == NFSERR_STALESTATEID ||
1437 error == NFSERR_STALEDONTRECOVER) && called_from_strategy != 0)))
1439 if (NFSHASNFSV4(nmp))
1440 NFSFREECRED(newcred);
1445 * The actual write RPC.
1448 nfsrpc_writerpc(vnode_t vp, struct uio *uiop, int *iomode,
1449 int *must_commit, struct ucred *cred, nfsv4stateid_t *stateidp,
1450 NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, void *stuff)
1453 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
1454 struct nfsnode *np = VTONFS(vp);
1455 int error = 0, len, tsiz, rlen, commit, committed = NFSWRITE_FILESYNC;
1456 int wccflag = 0, wsize;
1458 struct nfsrv_descript nfsd;
1459 struct nfsrv_descript *nd = &nfsd;
1460 nfsattrbit_t attrbits;
1463 KASSERT(uiop->uio_iovcnt == 1, ("nfs: writerpc iovcnt > 1"));
1465 tsiz = uio_uio_resid(uiop);
1466 tmp_off = uiop->uio_offset + tsiz;
1468 if (tmp_off > nmp->nm_maxfilesize || tmp_off < uiop->uio_offset) {
1472 wsize = nmp->nm_wsize;
1474 nd->nd_mrep = NULL; /* NFSv2 sometimes does a write with */
1475 nd->nd_repstat = 0; /* uio_resid == 0, so the while is not done */
1478 len = (tsiz > wsize) ? wsize : tsiz;
1479 NFSCL_REQSTART(nd, NFSPROC_WRITE, vp);
1480 if (nd->nd_flag & ND_NFSV4) {
1481 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID);
1482 NFSM_BUILD(tl, u_int32_t *, NFSX_HYPER+2*NFSX_UNSIGNED);
1483 txdr_hyper(uiop->uio_offset, tl);
1485 *tl++ = txdr_unsigned(*iomode);
1486 *tl = txdr_unsigned(len);
1487 } else if (nd->nd_flag & ND_NFSV3) {
1488 NFSM_BUILD(tl, u_int32_t *, NFSX_HYPER+3*NFSX_UNSIGNED);
1489 txdr_hyper(uiop->uio_offset, tl);
1491 *tl++ = txdr_unsigned(len);
1492 *tl++ = txdr_unsigned(*iomode);
1493 *tl = txdr_unsigned(len);
1497 NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1499 * Not sure why someone changed this, since the
1500 * RFC clearly states that "beginoffset" and
1501 * "totalcount" are ignored, but it wouldn't
1502 * surprise me if there's a busted server out there.
1504 /* Set both "begin" and "current" to non-garbage. */
1505 x = txdr_unsigned((u_int32_t)uiop->uio_offset);
1506 *tl++ = x; /* "begin offset" */
1507 *tl++ = x; /* "current offset" */
1508 x = txdr_unsigned(len);
1509 *tl++ = x; /* total to this offset */
1510 *tl = x; /* size of this write */
1513 nfsm_uiombuf(nd, uiop, len);
1515 * Although it is tempting to do a normal Getattr Op in the
1516 * NFSv4 compound, the result can be a nearly hung client
1517 * system if the Getattr asks for Owner and/or OwnerGroup.
1518 * It occurs when the client can't map either the Owner or
1519 * Owner_group name in the Getattr reply to a uid/gid. When
1520 * there is a cache miss, the kernel does an upcall to the
1521 * nfsuserd. Then, it can try and read the local /etc/passwd
1522 * or /etc/group file. It can then block in getnewbuf(),
1523 * waiting for dirty writes to be pushed to the NFS server.
1524 * The only reason this doesn't result in a complete
1525 * deadlock, is that the upcall times out and allows
1526 * the write to complete. However, progress is so slow
1527 * that it might just as well be deadlocked.
1528 * As such, we get the rest of the attributes, but not
1529 * Owner or Owner_group.
1530 * nb: nfscl_loadattrcache() needs to be told that these
1531 * partial attributes from a write rpc are being
1532 * passed in, via a argument flag.
1534 if (nd->nd_flag & ND_NFSV4) {
1535 NFSWRITEGETATTR_ATTRBIT(&attrbits);
1536 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1537 *tl = txdr_unsigned(NFSV4OP_GETATTR);
1538 (void) nfsrv_putattrbit(nd, &attrbits);
1540 error = nfscl_request(nd, vp, p, cred, stuff);
1543 if (nd->nd_repstat) {
1545 * In case the rpc gets retried, roll
1546 * the uio fileds changed by nfsm_uiombuf()
1549 uiop->uio_offset -= len;
1550 uio_uio_resid_add(uiop, len);
1551 uio_iov_base_add(uiop, -len);
1552 uio_iov_len_add(uiop, len);
1554 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) {
1555 error = nfscl_wcc_data(nd, vp, nap, attrflagp,
1560 if (!nd->nd_repstat) {
1561 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) {
1562 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED
1564 rlen = fxdr_unsigned(int, *tl++);
1568 } else if (rlen < len) {
1569 backup = len - rlen;
1570 uio_iov_base_add(uiop, -(backup));
1571 uio_iov_len_add(uiop, backup);
1572 uiop->uio_offset -= backup;
1573 uio_uio_resid_add(uiop, backup);
1576 commit = fxdr_unsigned(int, *tl++);
1579 * Return the lowest committment level
1580 * obtained by any of the RPCs.
1582 if (committed == NFSWRITE_FILESYNC)
1584 else if (committed == NFSWRITE_DATASYNC &&
1585 commit == NFSWRITE_UNSTABLE)
1588 if (!NFSHASWRITEVERF(nmp)) {
1589 NFSBCOPY((caddr_t)tl,
1590 (caddr_t)&nmp->nm_verf[0],
1592 NFSSETWRITEVERF(nmp);
1593 } else if (NFSBCMP(tl, nmp->nm_verf,
1596 NFSBCOPY(tl, nmp->nm_verf, NFSX_VERF);
1600 if (nd->nd_flag & ND_NFSV4)
1601 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1602 if (nd->nd_flag & (ND_NFSV2 | ND_NFSV4)) {
1603 error = nfsm_loadattr(nd, nap);
1605 *attrflagp = NFS_LATTR_NOSHRINK;
1608 error = nd->nd_repstat;
1612 NFSWRITERPC_SETTIME(wccflag, np, (nd->nd_flag & ND_NFSV4));
1613 mbuf_freem(nd->nd_mrep);
1618 if (nd->nd_mrep != NULL)
1619 mbuf_freem(nd->nd_mrep);
1620 *iomode = committed;
1621 if (nd->nd_repstat && !error)
1622 error = nd->nd_repstat;
1628 * For NFS v2 this is a kludge. Use a create rpc but with the IFMT bits of the
1629 * mode set to specify the file type and the size field for rdev.
1632 nfsrpc_mknod(vnode_t dvp, char *name, int namelen, struct vattr *vap,
1633 u_int32_t rdev, enum vtype vtyp, struct ucred *cred, NFSPROC_T *p,
1634 struct nfsvattr *dnap, struct nfsvattr *nnap, struct nfsfh **nfhpp,
1635 int *attrflagp, int *dattrflagp, void *dstuff)
1639 struct nfsrv_descript nfsd, *nd = &nfsd;
1640 nfsattrbit_t attrbits;
1645 if (namelen > NFS_MAXNAMLEN)
1646 return (ENAMETOOLONG);
1647 NFSCL_REQSTART(nd, NFSPROC_MKNOD, dvp);
1648 if (nd->nd_flag & ND_NFSV4) {
1649 if (vtyp == VBLK || vtyp == VCHR) {
1650 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
1651 *tl++ = vtonfsv34_type(vtyp);
1652 *tl++ = txdr_unsigned(NFSMAJOR(rdev));
1653 *tl = txdr_unsigned(NFSMINOR(rdev));
1655 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1656 *tl = vtonfsv34_type(vtyp);
1659 (void) nfsm_strtom(nd, name, namelen);
1660 if (nd->nd_flag & ND_NFSV3) {
1661 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1662 *tl = vtonfsv34_type(vtyp);
1664 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4))
1665 nfscl_fillsattr(nd, vap, dvp, 0, 0);
1666 if ((nd->nd_flag & ND_NFSV3) &&
1667 (vtyp == VCHR || vtyp == VBLK)) {
1668 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1669 *tl++ = txdr_unsigned(NFSMAJOR(rdev));
1670 *tl = txdr_unsigned(NFSMINOR(rdev));
1672 if (nd->nd_flag & ND_NFSV4) {
1673 NFSGETATTR_ATTRBIT(&attrbits);
1674 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1675 *tl++ = txdr_unsigned(NFSV4OP_GETFH);
1676 *tl = txdr_unsigned(NFSV4OP_GETATTR);
1677 (void) nfsrv_putattrbit(nd, &attrbits);
1679 if (nd->nd_flag & ND_NFSV2)
1680 nfscl_fillsattr(nd, vap, dvp, NFSSATTR_SIZERDEV, rdev);
1681 error = nfscl_request(nd, dvp, p, cred, dstuff);
1684 if (nd->nd_flag & ND_NFSV4)
1685 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
1686 if (!nd->nd_repstat) {
1687 if (nd->nd_flag & ND_NFSV4) {
1688 NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
1689 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
1693 error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp);
1697 if (nd->nd_flag & ND_NFSV3)
1698 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
1699 if (!error && nd->nd_repstat)
1700 error = nd->nd_repstat;
1702 mbuf_freem(nd->nd_mrep);
1707 * nfs file create call
1708 * Mostly just call the approriate routine. (I separated out v4, so that
1709 * error recovery wouldn't be as difficult.)
1712 nfsrpc_create(vnode_t dvp, char *name, int namelen, struct vattr *vap,
1713 nfsquad_t cverf, int fmode, struct ucred *cred, NFSPROC_T *p,
1714 struct nfsvattr *dnap, struct nfsvattr *nnap, struct nfsfh **nfhpp,
1715 int *attrflagp, int *dattrflagp, void *dstuff)
1717 int error = 0, newone, expireret = 0, retrycnt, unlocked;
1718 struct nfsclowner *owp;
1719 struct nfscldeleg *dp;
1720 struct nfsmount *nmp = VFSTONFS(vnode_mount(dvp));
1723 if (NFSHASNFSV4(nmp)) {
1727 error = nfscl_open(dvp, NULL, 0, (NFSV4OPEN_ACCESSWRITE |
1728 NFSV4OPEN_ACCESSREAD), 0, cred, p, &owp, NULL, &newone,
1732 if (nmp->nm_clp != NULL)
1733 clidrev = nmp->nm_clp->nfsc_clientidrev;
1736 error = nfsrpc_createv4(dvp, name, namelen, vap, cverf, fmode,
1737 owp, &dp, cred, p, dnap, nnap, nfhpp, attrflagp, dattrflagp,
1740 * There is no need to invalidate cached attributes here,
1741 * since new post-delegation issue attributes are always
1742 * returned by nfsrpc_createv4() and these will update the
1746 (void) nfscl_deleg(nmp->nm_mountp, owp->nfsow_clp,
1747 (*nfhpp)->nfh_fh, (*nfhpp)->nfh_len, cred, p, &dp);
1748 nfscl_ownerrelease(owp, error, newone, unlocked);
1749 if (error == NFSERR_GRACE || error == NFSERR_STALECLIENTID ||
1750 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY) {
1751 (void) nfs_catnap(PZERO, error, "nfs_open");
1752 } else if ((error == NFSERR_EXPIRED ||
1753 error == NFSERR_BADSTATEID) && clidrev != 0) {
1754 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
1757 } while (error == NFSERR_GRACE || error == NFSERR_STALECLIENTID ||
1758 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
1759 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
1760 expireret == 0 && clidrev != 0 && retrycnt < 4));
1761 if (error && retrycnt >= 4)
1764 error = nfsrpc_createv23(dvp, name, namelen, vap, cverf,
1765 fmode, cred, p, dnap, nnap, nfhpp, attrflagp, dattrflagp,
1772 * The create rpc for v2 and 3.
1775 nfsrpc_createv23(vnode_t dvp, char *name, int namelen, struct vattr *vap,
1776 nfsquad_t cverf, int fmode, struct ucred *cred, NFSPROC_T *p,
1777 struct nfsvattr *dnap, struct nfsvattr *nnap, struct nfsfh **nfhpp,
1778 int *attrflagp, int *dattrflagp, void *dstuff)
1782 struct nfsrv_descript nfsd, *nd = &nfsd;
1787 if (namelen > NFS_MAXNAMLEN)
1788 return (ENAMETOOLONG);
1789 NFSCL_REQSTART(nd, NFSPROC_CREATE, dvp);
1790 (void) nfsm_strtom(nd, name, namelen);
1791 if (nd->nd_flag & ND_NFSV3) {
1792 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1793 if (fmode & O_EXCL) {
1794 *tl = txdr_unsigned(NFSCREATE_EXCLUSIVE);
1795 NFSM_BUILD(tl, u_int32_t *, NFSX_VERF);
1796 *tl++ = cverf.lval[0];
1797 *tl = cverf.lval[1];
1799 *tl = txdr_unsigned(NFSCREATE_UNCHECKED);
1800 nfscl_fillsattr(nd, vap, dvp, 0, 0);
1803 nfscl_fillsattr(nd, vap, dvp, NFSSATTR_SIZE0, 0);
1805 error = nfscl_request(nd, dvp, p, cred, dstuff);
1808 if (nd->nd_repstat == 0) {
1809 error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp);
1813 if (nd->nd_flag & ND_NFSV3)
1814 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
1815 if (nd->nd_repstat != 0 && error == 0)
1816 error = nd->nd_repstat;
1818 mbuf_freem(nd->nd_mrep);
1823 nfsrpc_createv4(vnode_t dvp, char *name, int namelen, struct vattr *vap,
1824 nfsquad_t cverf, int fmode, struct nfsclowner *owp, struct nfscldeleg **dpp,
1825 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap,
1826 struct nfsvattr *nnap, struct nfsfh **nfhpp, int *attrflagp,
1827 int *dattrflagp, void *dstuff, int *unlockedp)
1830 int error = 0, deleg, newone, ret, acesize, limitby;
1831 struct nfsrv_descript nfsd, *nd = &nfsd;
1832 struct nfsclopen *op;
1833 struct nfscldeleg *dp = NULL;
1836 nfsattrbit_t attrbits;
1837 nfsv4stateid_t stateid;
1845 if (namelen > NFS_MAXNAMLEN)
1846 return (ENAMETOOLONG);
1847 NFSCL_REQSTART(nd, NFSPROC_CREATE, dvp);
1849 * For V4, this is actually an Open op.
1851 NFSM_BUILD(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
1852 *tl++ = txdr_unsigned(owp->nfsow_seqid);
1853 *tl++ = txdr_unsigned(NFSV4OPEN_ACCESSWRITE |
1854 NFSV4OPEN_ACCESSREAD);
1855 *tl++ = txdr_unsigned(NFSV4OPEN_DENYNONE);
1856 *tl++ = owp->nfsow_clp->nfsc_clientid.lval[0];
1857 *tl = owp->nfsow_clp->nfsc_clientid.lval[1];
1858 (void) nfsm_strtom(nd, owp->nfsow_owner, NFSV4CL_LOCKNAMELEN);
1859 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1860 *tl++ = txdr_unsigned(NFSV4OPEN_CREATE);
1861 if (fmode & O_EXCL) {
1862 *tl = txdr_unsigned(NFSCREATE_EXCLUSIVE);
1863 NFSM_BUILD(tl, u_int32_t *, NFSX_VERF);
1864 *tl++ = cverf.lval[0];
1865 *tl = cverf.lval[1];
1867 *tl = txdr_unsigned(NFSCREATE_UNCHECKED);
1868 nfscl_fillsattr(nd, vap, dvp, 0, 0);
1870 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1871 *tl = txdr_unsigned(NFSV4OPEN_CLAIMNULL);
1872 (void) nfsm_strtom(nd, name, namelen);
1873 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1874 *tl++ = txdr_unsigned(NFSV4OP_GETFH);
1875 *tl = txdr_unsigned(NFSV4OP_GETATTR);
1876 NFSGETATTR_ATTRBIT(&attrbits);
1877 (void) nfsrv_putattrbit(nd, &attrbits);
1878 error = nfscl_request(nd, dvp, p, cred, dstuff);
1881 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
1884 NFSCL_INCRSEQID(owp->nfsow_seqid, nd);
1885 if (nd->nd_repstat == 0) {
1886 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
1888 stateid.seqid = *tl++;
1889 stateid.other[0] = *tl++;
1890 stateid.other[1] = *tl++;
1891 stateid.other[2] = *tl;
1892 rflags = fxdr_unsigned(u_int32_t, *(tl + 6));
1893 (void) nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
1894 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
1895 deleg = fxdr_unsigned(int, *tl);
1896 if (deleg == NFSV4OPEN_DELEGATEREAD ||
1897 deleg == NFSV4OPEN_DELEGATEWRITE) {
1898 if (!(owp->nfsow_clp->nfsc_flags &
1899 NFSCLFLAGS_FIRSTDELEG))
1900 owp->nfsow_clp->nfsc_flags |=
1901 (NFSCLFLAGS_FIRSTDELEG | NFSCLFLAGS_GOTDELEG);
1902 MALLOC(dp, struct nfscldeleg *,
1903 sizeof (struct nfscldeleg) + NFSX_V4FHMAX,
1904 M_NFSCLDELEG, M_WAITOK);
1905 LIST_INIT(&dp->nfsdl_owner);
1906 LIST_INIT(&dp->nfsdl_lock);
1907 dp->nfsdl_clp = owp->nfsow_clp;
1908 newnfs_copyincred(cred, &dp->nfsdl_cred);
1909 nfscl_lockinit(&dp->nfsdl_rwlock);
1910 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
1912 dp->nfsdl_stateid.seqid = *tl++;
1913 dp->nfsdl_stateid.other[0] = *tl++;
1914 dp->nfsdl_stateid.other[1] = *tl++;
1915 dp->nfsdl_stateid.other[2] = *tl++;
1916 ret = fxdr_unsigned(int, *tl);
1917 if (deleg == NFSV4OPEN_DELEGATEWRITE) {
1918 dp->nfsdl_flags = NFSCLDL_WRITE;
1920 * Indicates how much the file can grow.
1922 NFSM_DISSECT(tl, u_int32_t *,
1924 limitby = fxdr_unsigned(int, *tl++);
1926 case NFSV4OPEN_LIMITSIZE:
1927 dp->nfsdl_sizelimit = fxdr_hyper(tl);
1929 case NFSV4OPEN_LIMITBLOCKS:
1930 dp->nfsdl_sizelimit =
1931 fxdr_unsigned(u_int64_t, *tl++);
1932 dp->nfsdl_sizelimit *=
1933 fxdr_unsigned(u_int64_t, *tl);
1936 error = NFSERR_BADXDR;
1940 dp->nfsdl_flags = NFSCLDL_READ;
1943 dp->nfsdl_flags |= NFSCLDL_RECALL;
1944 error = nfsrv_dissectace(nd, &dp->nfsdl_ace, &ret,
1948 } else if (deleg != NFSV4OPEN_DELEGATENONE) {
1949 error = NFSERR_BADXDR;
1952 error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp);
1955 if (dp != NULL && *attrflagp) {
1956 dp->nfsdl_change = nnap->na_filerev;
1957 dp->nfsdl_modtime = nnap->na_mtime;
1958 dp->nfsdl_flags |= NFSCLDL_MODTIMESET;
1961 * We can now complete the Open state.
1965 dp->nfsdl_fhlen = nfhp->nfh_len;
1966 NFSBCOPY(nfhp->nfh_fh, dp->nfsdl_fh, nfhp->nfh_len);
1969 * Get an Open structure that will be
1970 * attached to the OpenOwner, acquired already.
1972 error = nfscl_open(dvp, nfhp->nfh_fh, nfhp->nfh_len,
1973 (NFSV4OPEN_ACCESSWRITE | NFSV4OPEN_ACCESSREAD), 0,
1974 cred, p, NULL, &op, &newone, NULL, 0);
1977 op->nfso_stateid = stateid;
1978 newnfs_copyincred(cred, &op->nfso_cred);
1979 if ((rflags & NFSV4OPEN_RESULTCONFIRM)) {
1981 ret = nfsrpc_openconfirm(dvp, nfhp->nfh_fh,
1982 nfhp->nfh_len, op, cred, p);
1983 if (ret == NFSERR_DELAY)
1984 (void) nfs_catnap(PZERO, ret, "nfs_create");
1985 } while (ret == NFSERR_DELAY);
1990 * If the server is handing out delegations, but we didn't
1991 * get one because an OpenConfirm was required, try the
1992 * Open again, to get a delegation. This is a harmless no-op,
1993 * from a server's point of view.
1995 if ((rflags & NFSV4OPEN_RESULTCONFIRM) &&
1996 (owp->nfsow_clp->nfsc_flags & NFSCLFLAGS_GOTDELEG) &&
1997 !error && dp == NULL) {
2000 ret = nfsrpc_openrpc(VFSTONFS(vnode_mount(dvp)), dvp,
2001 np->n_fhp->nfh_fh, np->n_fhp->nfh_len,
2002 nfhp->nfh_fh, nfhp->nfh_len,
2003 (NFSV4OPEN_ACCESSWRITE | NFSV4OPEN_ACCESSREAD), op,
2004 name, namelen, &dp, 0, 0x0, cred, p, 0, 1);
2005 if (ret == NFSERR_DELAY)
2006 (void) nfs_catnap(PZERO, ret, "nfs_crt2");
2007 } while (ret == NFSERR_DELAY);
2010 FREE((caddr_t)dp, M_NFSCLDELEG);
2011 if (ret == NFSERR_STALECLIENTID ||
2012 ret == NFSERR_STALEDONTRECOVER)
2016 nfscl_openrelease(op, error, newone);
2019 if (nd->nd_repstat != 0 && error == 0)
2020 error = nd->nd_repstat;
2021 if (error == NFSERR_STALECLIENTID)
2022 nfscl_initiate_recovery(owp->nfsow_clp);
2026 else if (dp != NULL)
2027 FREE((caddr_t)dp, M_NFSCLDELEG);
2028 mbuf_freem(nd->nd_mrep);
2036 nfsrpc_remove(vnode_t dvp, char *name, int namelen, vnode_t vp,
2037 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap, int *dattrflagp,
2041 struct nfsrv_descript nfsd, *nd = &nfsd;
2043 struct nfsmount *nmp;
2044 nfsv4stateid_t dstateid;
2045 int error, ret = 0, i;
2048 if (namelen > NFS_MAXNAMLEN)
2049 return (ENAMETOOLONG);
2050 nmp = VFSTONFS(vnode_mount(dvp));
2052 if (NFSHASNFSV4(nmp) && ret == 0) {
2053 ret = nfscl_removedeleg(vp, p, &dstateid);
2055 NFSCL_REQSTART(nd, NFSPROC_RETDELEGREMOVE, vp);
2056 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID +
2058 *tl++ = dstateid.seqid;
2059 *tl++ = dstateid.other[0];
2060 *tl++ = dstateid.other[1];
2061 *tl++ = dstateid.other[2];
2062 *tl = txdr_unsigned(NFSV4OP_PUTFH);
2064 (void) nfsm_fhtom(nd, np->n_fhp->nfh_fh,
2065 np->n_fhp->nfh_len, 0);
2066 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2067 *tl = txdr_unsigned(NFSV4OP_REMOVE);
2073 NFSCL_REQSTART(nd, NFSPROC_REMOVE, dvp);
2074 (void) nfsm_strtom(nd, name, namelen);
2075 error = nfscl_request(nd, dvp, p, cred, dstuff);
2078 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) {
2079 /* For NFSv4, parse out any Delereturn replies. */
2080 if (ret > 0 && nd->nd_repstat != 0 &&
2081 (nd->nd_flag & ND_NOMOREDATA)) {
2083 * If the Delegreturn failed, try again without
2084 * it. The server will Recall, as required.
2086 mbuf_freem(nd->nd_mrep);
2089 for (i = 0; i < (ret * 2); i++) {
2090 if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) ==
2092 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2094 nd->nd_flag |= ND_NOMOREDATA;
2097 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
2099 if (nd->nd_repstat && !error)
2100 error = nd->nd_repstat;
2102 mbuf_freem(nd->nd_mrep);
2107 * Do an nfs rename rpc.
2110 nfsrpc_rename(vnode_t fdvp, vnode_t fvp, char *fnameptr, int fnamelen,
2111 vnode_t tdvp, vnode_t tvp, char *tnameptr, int tnamelen, struct ucred *cred,
2112 NFSPROC_T *p, struct nfsvattr *fnap, struct nfsvattr *tnap,
2113 int *fattrflagp, int *tattrflagp, void *fstuff, void *tstuff)
2116 struct nfsrv_descript nfsd, *nd = &nfsd;
2117 struct nfsmount *nmp;
2119 nfsattrbit_t attrbits;
2120 nfsv4stateid_t fdstateid, tdstateid;
2121 int error = 0, ret = 0, gottd = 0, gotfd = 0, i;
2125 nmp = VFSTONFS(vnode_mount(fdvp));
2126 if (fnamelen > NFS_MAXNAMLEN || tnamelen > NFS_MAXNAMLEN)
2127 return (ENAMETOOLONG);
2129 if (NFSHASNFSV4(nmp) && ret == 0) {
2130 ret = nfscl_renamedeleg(fvp, &fdstateid, &gotfd, tvp,
2131 &tdstateid, &gottd, p);
2132 if (gotfd && gottd) {
2133 NFSCL_REQSTART(nd, NFSPROC_RETDELEGRENAME2, fvp);
2135 NFSCL_REQSTART(nd, NFSPROC_RETDELEGRENAME1, fvp);
2137 NFSCL_REQSTART(nd, NFSPROC_RETDELEGRENAME1, tvp);
2140 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID);
2141 *tl++ = fdstateid.seqid;
2142 *tl++ = fdstateid.other[0];
2143 *tl++ = fdstateid.other[1];
2144 *tl = fdstateid.other[2];
2146 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2147 *tl = txdr_unsigned(NFSV4OP_PUTFH);
2149 (void) nfsm_fhtom(nd, np->n_fhp->nfh_fh,
2150 np->n_fhp->nfh_len, 0);
2151 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2152 *tl = txdr_unsigned(NFSV4OP_DELEGRETURN);
2156 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID);
2157 *tl++ = tdstateid.seqid;
2158 *tl++ = tdstateid.other[0];
2159 *tl++ = tdstateid.other[1];
2160 *tl = tdstateid.other[2];
2163 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2164 *tl = txdr_unsigned(NFSV4OP_PUTFH);
2166 (void) nfsm_fhtom(nd, np->n_fhp->nfh_fh,
2167 np->n_fhp->nfh_len, 0);
2168 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2169 *tl = txdr_unsigned(NFSV4OP_SAVEFH);
2175 NFSCL_REQSTART(nd, NFSPROC_RENAME, fdvp);
2176 if (nd->nd_flag & ND_NFSV4) {
2177 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2178 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2179 NFSWCCATTR_ATTRBIT(&attrbits);
2180 (void) nfsrv_putattrbit(nd, &attrbits);
2181 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2182 *tl = txdr_unsigned(NFSV4OP_PUTFH);
2183 (void) nfsm_fhtom(nd, VTONFS(tdvp)->n_fhp->nfh_fh,
2184 VTONFS(tdvp)->n_fhp->nfh_len, 0);
2185 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2186 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2187 (void) nfsrv_putattrbit(nd, &attrbits);
2188 nd->nd_flag |= ND_V4WCCATTR;
2189 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2190 *tl = txdr_unsigned(NFSV4OP_RENAME);
2192 (void) nfsm_strtom(nd, fnameptr, fnamelen);
2193 if (!(nd->nd_flag & ND_NFSV4))
2194 (void) nfsm_fhtom(nd, VTONFS(tdvp)->n_fhp->nfh_fh,
2195 VTONFS(tdvp)->n_fhp->nfh_len, 0);
2196 (void) nfsm_strtom(nd, tnameptr, tnamelen);
2197 error = nfscl_request(nd, fdvp, p, cred, fstuff);
2200 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) {
2201 /* For NFSv4, parse out any Delereturn replies. */
2202 if (ret > 0 && nd->nd_repstat != 0 &&
2203 (nd->nd_flag & ND_NOMOREDATA)) {
2205 * If the Delegreturn failed, try again without
2206 * it. The server will Recall, as required.
2208 mbuf_freem(nd->nd_mrep);
2211 for (i = 0; i < (ret * 2); i++) {
2212 if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) ==
2214 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2216 if (i == 0 && ret > 1) {
2218 * If the Delegreturn failed, try again
2219 * without it. The server will Recall, as
2221 * If ret > 1, the first iteration of this
2222 * loop is the second DelegReturn result.
2224 mbuf_freem(nd->nd_mrep);
2227 nd->nd_flag |= ND_NOMOREDATA;
2232 /* Now, the first wcc attribute reply. */
2233 if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) == ND_NFSV4) {
2234 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2236 nd->nd_flag |= ND_NOMOREDATA;
2238 error = nfscl_wcc_data(nd, fdvp, fnap, fattrflagp, NULL,
2240 /* and the second wcc attribute reply. */
2241 if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) == ND_NFSV4 &&
2243 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2245 nd->nd_flag |= ND_NOMOREDATA;
2248 error = nfscl_wcc_data(nd, tdvp, tnap, tattrflagp,
2251 if (nd->nd_repstat && !error)
2252 error = nd->nd_repstat;
2254 mbuf_freem(nd->nd_mrep);
2259 * nfs hard link create rpc
2262 nfsrpc_link(vnode_t dvp, vnode_t vp, char *name, int namelen,
2263 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap,
2264 struct nfsvattr *nap, int *attrflagp, int *dattrflagp, void *dstuff)
2267 struct nfsrv_descript nfsd, *nd = &nfsd;
2268 nfsattrbit_t attrbits;
2273 if (namelen > NFS_MAXNAMLEN)
2274 return (ENAMETOOLONG);
2275 NFSCL_REQSTART(nd, NFSPROC_LINK, vp);
2276 if (nd->nd_flag & ND_NFSV4) {
2277 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2278 *tl = txdr_unsigned(NFSV4OP_PUTFH);
2280 (void) nfsm_fhtom(nd, VTONFS(dvp)->n_fhp->nfh_fh,
2281 VTONFS(dvp)->n_fhp->nfh_len, 0);
2282 if (nd->nd_flag & ND_NFSV4) {
2283 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2284 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2285 NFSWCCATTR_ATTRBIT(&attrbits);
2286 (void) nfsrv_putattrbit(nd, &attrbits);
2287 nd->nd_flag |= ND_V4WCCATTR;
2288 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2289 *tl = txdr_unsigned(NFSV4OP_LINK);
2291 (void) nfsm_strtom(nd, name, namelen);
2292 error = nfscl_request(nd, vp, p, cred, dstuff);
2295 if (nd->nd_flag & ND_NFSV3) {
2296 error = nfscl_postop_attr(nd, nap, attrflagp, dstuff);
2298 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp,
2300 } else if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) == ND_NFSV4) {
2302 * First, parse out the PutFH and Getattr result.
2304 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2306 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2308 nd->nd_flag |= ND_NOMOREDATA;
2310 * Get the pre-op attributes.
2312 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
2314 if (nd->nd_repstat && !error)
2315 error = nd->nd_repstat;
2317 mbuf_freem(nd->nd_mrep);
2322 * nfs symbolic link create rpc
2325 nfsrpc_symlink(vnode_t dvp, char *name, int namelen, char *target,
2326 struct vattr *vap, struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap,
2327 struct nfsvattr *nnap, struct nfsfh **nfhpp, int *attrflagp,
2328 int *dattrflagp, void *dstuff)
2331 struct nfsrv_descript nfsd, *nd = &nfsd;
2332 struct nfsmount *nmp;
2333 int slen, error = 0;
2338 nmp = VFSTONFS(vnode_mount(dvp));
2339 slen = strlen(target);
2340 if (slen > NFS_MAXPATHLEN || namelen > NFS_MAXNAMLEN)
2341 return (ENAMETOOLONG);
2342 NFSCL_REQSTART(nd, NFSPROC_SYMLINK, dvp);
2343 if (nd->nd_flag & ND_NFSV4) {
2344 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2345 *tl = txdr_unsigned(NFLNK);
2346 (void) nfsm_strtom(nd, target, slen);
2348 (void) nfsm_strtom(nd, name, namelen);
2349 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4))
2350 nfscl_fillsattr(nd, vap, dvp, 0, 0);
2351 if (!(nd->nd_flag & ND_NFSV4))
2352 (void) nfsm_strtom(nd, target, slen);
2353 if (nd->nd_flag & ND_NFSV2)
2354 nfscl_fillsattr(nd, vap, dvp, NFSSATTR_SIZENEG1, 0);
2355 error = nfscl_request(nd, dvp, p, cred, dstuff);
2358 if (nd->nd_flag & ND_NFSV4)
2359 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
2360 if ((nd->nd_flag & ND_NFSV3) && !error) {
2361 if (!nd->nd_repstat)
2362 error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp);
2364 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp,
2367 if (nd->nd_repstat && !error)
2368 error = nd->nd_repstat;
2369 mbuf_freem(nd->nd_mrep);
2371 * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry.
2373 if (error == EEXIST)
2382 nfsrpc_mkdir(vnode_t dvp, char *name, int namelen, struct vattr *vap,
2383 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap,
2384 struct nfsvattr *nnap, struct nfsfh **nfhpp, int *attrflagp,
2385 int *dattrflagp, void *dstuff)
2388 struct nfsrv_descript nfsd, *nd = &nfsd;
2389 nfsattrbit_t attrbits;
2395 if (namelen > NFS_MAXNAMLEN)
2396 return (ENAMETOOLONG);
2397 NFSCL_REQSTART(nd, NFSPROC_MKDIR, dvp);
2398 if (nd->nd_flag & ND_NFSV4) {
2399 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2400 *tl = txdr_unsigned(NFDIR);
2402 (void) nfsm_strtom(nd, name, namelen);
2403 nfscl_fillsattr(nd, vap, dvp, NFSSATTR_SIZENEG1, 0);
2404 if (nd->nd_flag & ND_NFSV4) {
2405 NFSGETATTR_ATTRBIT(&attrbits);
2406 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2407 *tl++ = txdr_unsigned(NFSV4OP_GETFH);
2408 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2409 (void) nfsrv_putattrbit(nd, &attrbits);
2411 error = nfscl_request(nd, dvp, p, cred, dstuff);
2414 if (nd->nd_flag & ND_NFSV4)
2415 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
2416 if (!nd->nd_repstat && !error) {
2417 if (nd->nd_flag & ND_NFSV4) {
2418 NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
2419 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
2422 error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp);
2424 if ((nd->nd_flag & ND_NFSV3) && !error)
2425 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
2426 if (nd->nd_repstat && !error)
2427 error = nd->nd_repstat;
2429 mbuf_freem(nd->nd_mrep);
2431 * Kludge: Map EEXIST => 0 assuming that you have a reply to a retry.
2433 if (error == EEXIST)
2439 * nfs remove directory call
2442 nfsrpc_rmdir(vnode_t dvp, char *name, int namelen, struct ucred *cred,
2443 NFSPROC_T *p, struct nfsvattr *dnap, int *dattrflagp, void *dstuff)
2445 struct nfsrv_descript nfsd, *nd = &nfsd;
2449 if (namelen > NFS_MAXNAMLEN)
2450 return (ENAMETOOLONG);
2451 NFSCL_REQSTART(nd, NFSPROC_RMDIR, dvp);
2452 (void) nfsm_strtom(nd, name, namelen);
2453 error = nfscl_request(nd, dvp, p, cred, dstuff);
2456 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4))
2457 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
2458 if (nd->nd_repstat && !error)
2459 error = nd->nd_repstat;
2460 mbuf_freem(nd->nd_mrep);
2462 * Kludge: Map ENOENT => 0 assuming that you have a reply to a retry.
2464 if (error == ENOENT)
2471 * Always returns with either uio_resid unchanged, if you are at the
2472 * end of the directory, or uio_resid == 0, with all DIRBLKSIZ chunks
2474 * I felt this would allow caching of directory blocks more easily
2475 * than returning a pertially filled block.
2476 * Directory offset cookies:
2477 * Oh my, what to do with them...
2478 * I can think of three ways to deal with them:
2479 * 1 - have the layer above these RPCs maintain a map between logical
2480 * directory byte offsets and the NFS directory offset cookies
2481 * 2 - pass the opaque directory offset cookies up into userland
2482 * and let the libc functions deal with them, via the system call
2483 * 3 - return them to userland in the "struct dirent", so future versions
2484 * of libc can use them and do whatever is necessary to amke things work
2485 * above these rpc calls, in the meantime
2486 * For now, I do #3 by "hiding" the directory offset cookies after the
2487 * d_name field in struct dirent. This is space inside d_reclen that
2488 * will be ignored by anything that doesn't know about them.
2489 * The directory offset cookies are filled in as the last 8 bytes of
2490 * each directory entry, after d_name. Someday, the userland libc
2491 * functions may be able to use these. In the meantime, it satisfies
2492 * OpenBSD's requirements for cookies being returned.
2493 * If expects the directory offset cookie for the read to be in uio_offset
2494 * and returns the one for the next entry after this directory block in
2498 nfsrpc_readdir(vnode_t vp, struct uio *uiop, nfsuint64 *cookiep,
2499 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp,
2500 int *eofp, void *stuff)
2503 struct dirent *dp = NULL;
2505 nfsquad_t cookie, ncookie;
2506 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
2507 struct nfsnode *dnp = VTONFS(vp);
2508 struct nfsvattr nfsva;
2509 struct nfsrv_descript nfsd, *nd = &nfsd;
2510 int error = 0, tlen, more_dirs = 1, blksiz = 0, bigenough = 1;
2511 int reqsize, tryformoredirs = 1, readsize, eof = 0, gotmnton = 0;
2512 long dotfileid, dotdotfileid = 0;
2513 u_int32_t fakefileno = 0xffffffff, rderr;
2515 nfsattrbit_t attrbits, dattrbits;
2516 u_int32_t *tl2 = NULL;
2519 KASSERT(uiop->uio_iovcnt == 1 &&
2520 (uio_uio_resid(uiop) & (DIRBLKSIZ - 1)) == 0,
2521 ("nfs readdirrpc bad uio"));
2524 * There is no point in reading a lot more than uio_resid, however
2525 * adding one additional DIRBLKSIZ makes sense. Since uio_resid
2526 * and nm_readdirsize are both exact multiples of DIRBLKSIZ, this
2527 * will never make readsize > nm_readdirsize.
2529 readsize = nmp->nm_readdirsize;
2530 if (readsize > uio_uio_resid(uiop))
2531 readsize = uio_uio_resid(uiop) + DIRBLKSIZ;
2536 tresid = uio_uio_resid(uiop);
2537 cookie.lval[0] = cookiep->nfsuquad[0];
2538 cookie.lval[1] = cookiep->nfsuquad[1];
2542 * For NFSv4, first create the "." and ".." entries.
2544 if (NFSHASNFSV4(nmp)) {
2545 reqsize = 6 * NFSX_UNSIGNED;
2546 NFSGETATTR_ATTRBIT(&dattrbits);
2547 NFSZERO_ATTRBIT(&attrbits);
2548 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_FILEID);
2549 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TYPE);
2550 if (NFSISSET_ATTRBIT(&dnp->n_vattr.na_suppattr,
2551 NFSATTRBIT_MOUNTEDONFILEID)) {
2552 NFSSETBIT_ATTRBIT(&attrbits,
2553 NFSATTRBIT_MOUNTEDONFILEID);
2557 * Must fake it. Use the fileno, except when the
2558 * fsid is != to that of the directory. For that
2559 * case, generate a fake fileno that is not the same.
2561 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_FSID);
2566 * Joy, oh joy. For V4 we get to hand craft '.' and '..'.
2568 if (uiop->uio_offset == 0) {
2569 #if defined(__FreeBSD_version) && __FreeBSD_version >= 800000
2570 error = VOP_GETATTR(vp, &nfsva.na_vattr, cred);
2572 error = VOP_GETATTR(vp, &nfsva.na_vattr, cred, p);
2576 dotfileid = nfsva.na_fileid;
2577 NFSCL_REQSTART(nd, NFSPROC_LOOKUPP, vp);
2578 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2579 *tl++ = txdr_unsigned(NFSV4OP_GETFH);
2580 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2581 (void) nfsrv_putattrbit(nd, &attrbits);
2582 error = nfscl_request(nd, vp, p, cred, stuff);
2585 if (nd->nd_repstat == 0) {
2586 NFSM_DISSECT(tl, u_int32_t *, 3*NFSX_UNSIGNED);
2587 len = fxdr_unsigned(int, *(tl + 2));
2588 if (len > 0 && len <= NFSX_V4FHMAX)
2589 error = nfsm_advance(nd, NFSM_RNDUP(len), -1);
2593 NFSM_DISSECT(tl, u_int32_t *, 2*NFSX_UNSIGNED);
2594 nfsva.na_mntonfileno = 0xffffffff;
2595 error = nfsv4_loadattr(nd, NULL, &nfsva, NULL,
2596 NULL, 0, NULL, NULL, NULL, NULL, NULL, 0,
2597 NULL, NULL, NULL, p, cred);
2599 dotdotfileid = dotfileid;
2600 } else if (gotmnton) {
2601 if (nfsva.na_mntonfileno != 0xffffffff)
2602 dotdotfileid = nfsva.na_mntonfileno;
2604 dotdotfileid = nfsva.na_fileid;
2605 } else if (nfsva.na_filesid[0] ==
2606 dnp->n_vattr.na_filesid[0] &&
2607 nfsva.na_filesid[1] ==
2608 dnp->n_vattr.na_filesid[1]) {
2609 dotdotfileid = nfsva.na_fileid;
2613 } while (fakefileno ==
2615 dotdotfileid = fakefileno;
2618 } else if (nd->nd_repstat == NFSERR_NOENT) {
2620 * Lookupp returns NFSERR_NOENT when we are
2621 * at the root, so just use the current dir.
2624 dotdotfileid = dotfileid;
2626 error = nd->nd_repstat;
2628 mbuf_freem(nd->nd_mrep);
2632 dp = (struct dirent *) CAST_DOWN(caddr_t, uio_iov_base(uiop));
2633 dp->d_type = DT_DIR;
2634 dp->d_fileno = dotfileid;
2636 dp->d_name[0] = '.';
2637 dp->d_name[1] = '\0';
2638 dp->d_reclen = DIRENT_SIZE(dp) + NFSX_HYPER;
2640 * Just make these offset cookie 0.
2642 tl = (u_int32_t *)&dp->d_name[4];
2645 blksiz += dp->d_reclen;
2646 uio_uio_resid_add(uiop, -(dp->d_reclen));
2647 uiop->uio_offset += dp->d_reclen;
2648 uio_iov_base_add(uiop, dp->d_reclen);
2649 uio_iov_len_add(uiop, -(dp->d_reclen));
2650 dp = (struct dirent *) CAST_DOWN(caddr_t, uio_iov_base(uiop));
2651 dp->d_type = DT_DIR;
2652 dp->d_fileno = dotdotfileid;
2654 dp->d_name[0] = '.';
2655 dp->d_name[1] = '.';
2656 dp->d_name[2] = '\0';
2657 dp->d_reclen = DIRENT_SIZE(dp) + NFSX_HYPER;
2659 * Just make these offset cookie 0.
2661 tl = (u_int32_t *)&dp->d_name[4];
2664 blksiz += dp->d_reclen;
2665 uio_uio_resid_add(uiop, -(dp->d_reclen));
2666 uiop->uio_offset += dp->d_reclen;
2667 uio_iov_base_add(uiop, dp->d_reclen);
2668 uio_iov_len_add(uiop, -(dp->d_reclen));
2670 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_RDATTRERROR);
2672 reqsize = 5 * NFSX_UNSIGNED;
2677 * Loop around doing readdir rpc's of size readsize.
2678 * The stopping criteria is EOF or buffer full.
2680 while (more_dirs && bigenough) {
2682 NFSCL_REQSTART(nd, NFSPROC_READDIR, vp);
2683 if (nd->nd_flag & ND_NFSV2) {
2684 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2685 *tl++ = cookie.lval[1];
2686 *tl = txdr_unsigned(readsize);
2688 NFSM_BUILD(tl, u_int32_t *, reqsize);
2689 *tl++ = cookie.lval[0];
2690 *tl++ = cookie.lval[1];
2691 if (cookie.qval == 0) {
2696 *tl++ = dnp->n_cookieverf.nfsuquad[0];
2697 *tl++ = dnp->n_cookieverf.nfsuquad[1];
2700 if (nd->nd_flag & ND_NFSV4) {
2701 *tl++ = txdr_unsigned(readsize);
2702 *tl = txdr_unsigned(readsize);
2703 (void) nfsrv_putattrbit(nd, &attrbits);
2704 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2705 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2706 (void) nfsrv_putattrbit(nd, &dattrbits);
2708 *tl = txdr_unsigned(readsize);
2711 error = nfscl_request(nd, vp, p, cred, stuff);
2714 if (!(nd->nd_flag & ND_NFSV2)) {
2715 if (nd->nd_flag & ND_NFSV3)
2716 error = nfscl_postop_attr(nd, nap, attrflagp,
2718 if (!nd->nd_repstat && !error) {
2719 NFSM_DISSECT(tl, u_int32_t *, NFSX_HYPER);
2721 dnp->n_cookieverf.nfsuquad[0] = *tl++;
2722 dnp->n_cookieverf.nfsuquad[1] = *tl;
2726 if (nd->nd_repstat || error) {
2728 error = nd->nd_repstat;
2731 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2732 more_dirs = fxdr_unsigned(int, *tl);
2736 /* loop thru the dir entries, doctoring them to 4bsd form */
2737 while (more_dirs && bigenough) {
2738 if (nd->nd_flag & ND_NFSV4) {
2739 NFSM_DISSECT(tl, u_int32_t *, 3*NFSX_UNSIGNED);
2740 ncookie.lval[0] = *tl++;
2741 ncookie.lval[1] = *tl++;
2742 len = fxdr_unsigned(int, *tl);
2743 } else if (nd->nd_flag & ND_NFSV3) {
2744 NFSM_DISSECT(tl, u_int32_t *, 3*NFSX_UNSIGNED);
2745 nfsva.na_fileid = fxdr_hyper(tl);
2747 len = fxdr_unsigned(int, *tl);
2749 NFSM_DISSECT(tl, u_int32_t *, 2*NFSX_UNSIGNED);
2751 fxdr_unsigned(long, *tl++);
2752 len = fxdr_unsigned(int, *tl);
2754 if (len <= 0 || len > NFS_MAXNAMLEN) {
2758 tlen = NFSM_RNDUP(len);
2760 tlen += 4; /* To ensure null termination */
2761 left = DIRBLKSIZ - blksiz;
2762 if ((int)(tlen + DIRHDSIZ + NFSX_HYPER) > left) {
2763 dp->d_reclen += left;
2764 uio_iov_base_add(uiop, left);
2765 uio_iov_len_add(uiop, -(left));
2766 uio_uio_resid_add(uiop, -(left));
2767 uiop->uio_offset += left;
2770 if ((int)(tlen + DIRHDSIZ + NFSX_HYPER) > uio_uio_resid(uiop))
2773 dp = (struct dirent *) CAST_DOWN(caddr_t, uio_iov_base(uiop));
2775 dp->d_reclen = tlen + DIRHDSIZ + NFSX_HYPER;
2776 dp->d_type = DT_UNKNOWN;
2777 blksiz += dp->d_reclen;
2778 if (blksiz == DIRBLKSIZ)
2780 uio_uio_resid_add(uiop, -(DIRHDSIZ));
2781 uiop->uio_offset += DIRHDSIZ;
2782 uio_iov_base_add(uiop, DIRHDSIZ);
2783 uio_iov_len_add(uiop, -(DIRHDSIZ));
2784 error = nfsm_mbufuio(nd, uiop, len);
2787 cp = CAST_DOWN(caddr_t, uio_iov_base(uiop));
2789 *cp = '\0'; /* null terminate */
2790 cp += tlen; /* points to cookie storage */
2791 tl2 = (u_int32_t *)cp;
2792 uio_iov_base_add(uiop, (tlen + NFSX_HYPER));
2793 uio_iov_len_add(uiop, -(tlen + NFSX_HYPER));
2794 uio_uio_resid_add(uiop, -(tlen + NFSX_HYPER));
2795 uiop->uio_offset += (tlen + NFSX_HYPER);
2797 error = nfsm_advance(nd, NFSM_RNDUP(len), -1);
2801 if (nd->nd_flag & ND_NFSV4) {
2803 nfsva.na_mntonfileno = 0xffffffff;
2804 error = nfsv4_loadattr(nd, NULL, &nfsva, NULL,
2805 NULL, 0, NULL, NULL, NULL, NULL, NULL, 0,
2806 NULL, NULL, &rderr, p, cred);
2809 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2810 } else if (nd->nd_flag & ND_NFSV3) {
2811 NFSM_DISSECT(tl, u_int32_t *, 3*NFSX_UNSIGNED);
2812 ncookie.lval[0] = *tl++;
2813 ncookie.lval[1] = *tl++;
2815 NFSM_DISSECT(tl, u_int32_t *, 2*NFSX_UNSIGNED);
2816 ncookie.lval[0] = 0;
2817 ncookie.lval[1] = *tl++;
2820 if (nd->nd_flag & ND_NFSV4) {
2825 if (nfsva.na_mntonfileno != 0xffffffff)
2826 dp->d_fileno = nfsva.na_mntonfileno;
2828 dp->d_fileno = nfsva.na_fileid;
2829 } else if (nfsva.na_filesid[0] ==
2830 dnp->n_vattr.na_filesid[0] &&
2831 nfsva.na_filesid[1] ==
2832 dnp->n_vattr.na_filesid[1]) {
2833 dp->d_fileno = nfsva.na_fileid;
2837 } while (fakefileno ==
2839 dp->d_fileno = fakefileno;
2841 dp->d_type = vtonfs_dtype(nfsva.na_type);
2844 dp->d_fileno = nfsva.na_fileid;
2846 *tl2++ = cookiep->nfsuquad[0] = cookie.lval[0] =
2848 *tl2 = cookiep->nfsuquad[1] = cookie.lval[1] =
2851 more_dirs = fxdr_unsigned(int, *tl);
2854 * If at end of rpc data, get the eof boolean
2857 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2858 eof = fxdr_unsigned(int, *tl);
2861 if (nd->nd_flag & ND_NFSV4) {
2862 error = nfscl_postop_attr(nd, nap, attrflagp,
2868 mbuf_freem(nd->nd_mrep);
2872 * Fill last record, iff any, out to a multiple of DIRBLKSIZ
2873 * by increasing d_reclen for the last record.
2876 left = DIRBLKSIZ - blksiz;
2877 dp->d_reclen += left;
2878 uio_iov_base_add(uiop, left);
2879 uio_iov_len_add(uiop, -(left));
2880 uio_uio_resid_add(uiop, -(left));
2881 uiop->uio_offset += left;
2885 * If returning no data, assume end of file.
2886 * If not bigenough, return not end of file, since you aren't
2887 * returning all the data
2888 * Otherwise, return the eof flag from the server.
2891 if (tresid == ((size_t)(uio_uio_resid(uiop))))
2893 else if (!bigenough)
2900 * Add extra empty records to any remaining DIRBLKSIZ chunks.
2902 while (uio_uio_resid(uiop) > 0 && ((size_t)(uio_uio_resid(uiop))) != tresid) {
2903 dp = (struct dirent *) CAST_DOWN(caddr_t, uio_iov_base(uiop));
2904 dp->d_type = DT_UNKNOWN;
2907 dp->d_name[0] = '\0';
2908 tl = (u_int32_t *)&dp->d_name[4];
2909 *tl++ = cookie.lval[0];
2910 *tl = cookie.lval[1];
2911 dp->d_reclen = DIRBLKSIZ;
2912 uio_iov_base_add(uiop, DIRBLKSIZ);
2913 uio_iov_len_add(uiop, -(DIRBLKSIZ));
2914 uio_uio_resid_add(uiop, -(DIRBLKSIZ));
2915 uiop->uio_offset += DIRBLKSIZ;
2919 if (nd->nd_mrep != NULL)
2920 mbuf_freem(nd->nd_mrep);
2926 * NFS V3 readdir plus RPC. Used in place of nfsrpc_readdir().
2927 * (Also used for NFS V4 when mount flag set.)
2928 * (ditto above w.r.t. multiple of DIRBLKSIZ, etc.)
2931 nfsrpc_readdirplus(vnode_t vp, struct uio *uiop, nfsuint64 *cookiep,
2932 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp,
2933 int *eofp, void *stuff)
2936 struct dirent *dp = NULL;
2938 vnode_t newvp = NULLVP;
2939 struct nfsrv_descript nfsd, *nd = &nfsd;
2940 struct nameidata nami, *ndp = &nami;
2941 struct componentname *cnp = &ndp->ni_cnd;
2942 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
2943 struct nfsnode *dnp = VTONFS(vp), *np;
2944 struct nfsvattr nfsva;
2946 nfsquad_t cookie, ncookie;
2947 int error = 0, tlen, more_dirs = 1, blksiz = 0, bigenough = 1;
2948 int attrflag, tryformoredirs = 1, eof = 0, gotmnton = 0;
2949 int isdotdot = 0, unlocknewvp = 0;
2950 long dotfileid, dotdotfileid = 0, fileno = 0;
2952 nfsattrbit_t attrbits, dattrbits;
2954 u_int32_t *tl2 = NULL, fakefileno = 0xffffffff, rderr;
2955 struct timespec dctime;
2957 KASSERT(uiop->uio_iovcnt == 1 &&
2958 (uio_uio_resid(uiop) & (DIRBLKSIZ - 1)) == 0,
2959 ("nfs readdirplusrpc bad uio"));
2960 timespecclear(&dctime);
2966 cookie.lval[0] = cookiep->nfsuquad[0];
2967 cookie.lval[1] = cookiep->nfsuquad[1];
2968 tresid = uio_uio_resid(uiop);
2971 * For NFSv4, first create the "." and ".." entries.
2973 if (NFSHASNFSV4(nmp)) {
2974 NFSGETATTR_ATTRBIT(&dattrbits);
2975 NFSZERO_ATTRBIT(&attrbits);
2976 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_FILEID);
2977 if (NFSISSET_ATTRBIT(&dnp->n_vattr.na_suppattr,
2978 NFSATTRBIT_MOUNTEDONFILEID)) {
2979 NFSSETBIT_ATTRBIT(&attrbits,
2980 NFSATTRBIT_MOUNTEDONFILEID);
2984 * Must fake it. Use the fileno, except when the
2985 * fsid is != to that of the directory. For that
2986 * case, generate a fake fileno that is not the same.
2988 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_FSID);
2993 * Joy, oh joy. For V4 we get to hand craft '.' and '..'.
2995 if (uiop->uio_offset == 0) {
2996 #if defined(__FreeBSD_version) && __FreeBSD_version >= 800000
2997 error = VOP_GETATTR(vp, &nfsva.na_vattr, cred);
2999 error = VOP_GETATTR(vp, &nfsva.na_vattr, cred, p);
3003 dctime = nfsva.na_ctime;
3004 dotfileid = nfsva.na_fileid;
3005 NFSCL_REQSTART(nd, NFSPROC_LOOKUPP, vp);
3006 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
3007 *tl++ = txdr_unsigned(NFSV4OP_GETFH);
3008 *tl = txdr_unsigned(NFSV4OP_GETATTR);
3009 (void) nfsrv_putattrbit(nd, &attrbits);
3010 error = nfscl_request(nd, vp, p, cred, stuff);
3013 if (nd->nd_repstat == 0) {
3014 NFSM_DISSECT(tl, u_int32_t *, 3*NFSX_UNSIGNED);
3015 len = fxdr_unsigned(int, *(tl + 2));
3016 if (len > 0 && len <= NFSX_V4FHMAX)
3017 error = nfsm_advance(nd, NFSM_RNDUP(len), -1);
3021 NFSM_DISSECT(tl, u_int32_t *, 2*NFSX_UNSIGNED);
3022 nfsva.na_mntonfileno = 0xffffffff;
3023 error = nfsv4_loadattr(nd, NULL, &nfsva, NULL,
3024 NULL, 0, NULL, NULL, NULL, NULL, NULL, 0,
3025 NULL, NULL, NULL, p, cred);
3027 dotdotfileid = dotfileid;
3028 } else if (gotmnton) {
3029 if (nfsva.na_mntonfileno != 0xffffffff)
3030 dotdotfileid = nfsva.na_mntonfileno;
3032 dotdotfileid = nfsva.na_fileid;
3033 } else if (nfsva.na_filesid[0] ==
3034 dnp->n_vattr.na_filesid[0] &&
3035 nfsva.na_filesid[1] ==
3036 dnp->n_vattr.na_filesid[1]) {
3037 dotdotfileid = nfsva.na_fileid;
3041 } while (fakefileno ==
3043 dotdotfileid = fakefileno;
3046 } else if (nd->nd_repstat == NFSERR_NOENT) {
3048 * Lookupp returns NFSERR_NOENT when we are
3049 * at the root, so just use the current dir.
3052 dotdotfileid = dotfileid;
3054 error = nd->nd_repstat;
3056 mbuf_freem(nd->nd_mrep);
3060 dp = (struct dirent *)uio_iov_base(uiop);
3061 dp->d_type = DT_DIR;
3062 dp->d_fileno = dotfileid;
3064 dp->d_name[0] = '.';
3065 dp->d_name[1] = '\0';
3066 dp->d_reclen = DIRENT_SIZE(dp) + NFSX_HYPER;
3068 * Just make these offset cookie 0.
3070 tl = (u_int32_t *)&dp->d_name[4];
3073 blksiz += dp->d_reclen;
3074 uio_uio_resid_add(uiop, -(dp->d_reclen));
3075 uiop->uio_offset += dp->d_reclen;
3076 uio_iov_base_add(uiop, dp->d_reclen);
3077 uio_iov_len_add(uiop, -(dp->d_reclen));
3078 dp = (struct dirent *)uio_iov_base(uiop);
3079 dp->d_type = DT_DIR;
3080 dp->d_fileno = dotdotfileid;
3082 dp->d_name[0] = '.';
3083 dp->d_name[1] = '.';
3084 dp->d_name[2] = '\0';
3085 dp->d_reclen = DIRENT_SIZE(dp) + NFSX_HYPER;
3087 * Just make these offset cookie 0.
3089 tl = (u_int32_t *)&dp->d_name[4];
3092 blksiz += dp->d_reclen;
3093 uio_uio_resid_add(uiop, -(dp->d_reclen));
3094 uiop->uio_offset += dp->d_reclen;
3095 uio_iov_base_add(uiop, dp->d_reclen);
3096 uio_iov_len_add(uiop, -(dp->d_reclen));
3098 NFSREADDIRPLUS_ATTRBIT(&attrbits);
3100 NFSSETBIT_ATTRBIT(&attrbits,
3101 NFSATTRBIT_MOUNTEDONFILEID);
3105 * Loop around doing readdir rpc's of size nm_readdirsize.
3106 * The stopping criteria is EOF or buffer full.
3108 while (more_dirs && bigenough) {
3110 NFSCL_REQSTART(nd, NFSPROC_READDIRPLUS, vp);
3111 NFSM_BUILD(tl, u_int32_t *, 6 * NFSX_UNSIGNED);
3112 *tl++ = cookie.lval[0];
3113 *tl++ = cookie.lval[1];
3114 if (cookie.qval == 0) {
3119 *tl++ = dnp->n_cookieverf.nfsuquad[0];
3120 *tl++ = dnp->n_cookieverf.nfsuquad[1];
3123 *tl++ = txdr_unsigned(nmp->nm_readdirsize);
3124 *tl = txdr_unsigned(nmp->nm_readdirsize);
3125 if (nd->nd_flag & ND_NFSV4) {
3126 (void) nfsrv_putattrbit(nd, &attrbits);
3127 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
3128 *tl = txdr_unsigned(NFSV4OP_GETATTR);
3129 (void) nfsrv_putattrbit(nd, &dattrbits);
3131 error = nfscl_request(nd, vp, p, cred, stuff);
3134 if (nd->nd_flag & ND_NFSV3)
3135 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
3136 if (nd->nd_repstat || error) {
3138 error = nd->nd_repstat;
3141 if ((nd->nd_flag & ND_NFSV3) != 0 && *attrflagp != 0)
3142 dctime = nap->na_ctime;
3143 NFSM_DISSECT(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
3145 dnp->n_cookieverf.nfsuquad[0] = *tl++;
3146 dnp->n_cookieverf.nfsuquad[1] = *tl++;
3148 more_dirs = fxdr_unsigned(int, *tl);
3152 /* loop thru the dir entries, doctoring them to 4bsd form */
3153 while (more_dirs && bigenough) {
3154 NFSM_DISSECT(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
3155 if (nd->nd_flag & ND_NFSV4) {
3156 ncookie.lval[0] = *tl++;
3157 ncookie.lval[1] = *tl++;
3159 fileno = fxdr_unsigned(long, *++tl);
3162 len = fxdr_unsigned(int, *tl);
3163 if (len <= 0 || len > NFS_MAXNAMLEN) {
3167 tlen = NFSM_RNDUP(len);
3169 tlen += 4; /* To ensure null termination */
3170 left = DIRBLKSIZ - blksiz;
3171 if ((tlen + DIRHDSIZ + NFSX_HYPER) > left) {
3172 dp->d_reclen += left;
3173 uio_iov_base_add(uiop, left);
3174 uio_iov_len_add(uiop, -(left));
3175 uio_uio_resid_add(uiop, -(left));
3176 uiop->uio_offset += left;
3179 if ((tlen + DIRHDSIZ + NFSX_HYPER) > uio_uio_resid(uiop))
3182 dp = (struct dirent *)uio_iov_base(uiop);
3184 dp->d_reclen = tlen + DIRHDSIZ + NFSX_HYPER;
3185 dp->d_type = DT_UNKNOWN;
3186 blksiz += dp->d_reclen;
3187 if (blksiz == DIRBLKSIZ)
3189 uio_uio_resid_add(uiop, -(DIRHDSIZ));
3190 uiop->uio_offset += DIRHDSIZ;
3191 uio_iov_base_add(uiop, DIRHDSIZ);
3192 uio_iov_len_add(uiop, -(DIRHDSIZ));
3193 cnp->cn_nameptr = uio_iov_base(uiop);
3194 cnp->cn_namelen = len;
3196 error = nfsm_mbufuio(nd, uiop, len);
3199 cp = uio_iov_base(uiop);
3202 cp += tlen; /* points to cookie storage */
3203 tl2 = (u_int32_t *)cp;
3204 if (len == 2 && cnp->cn_nameptr[0] == '.' &&
3205 cnp->cn_nameptr[1] == '.')
3209 uio_iov_base_add(uiop, (tlen + NFSX_HYPER));
3210 uio_iov_len_add(uiop, -(tlen + NFSX_HYPER));
3211 uio_uio_resid_add(uiop, -(tlen + NFSX_HYPER));
3212 uiop->uio_offset += (tlen + NFSX_HYPER);
3214 error = nfsm_advance(nd, NFSM_RNDUP(len), -1);
3219 if (nd->nd_flag & ND_NFSV3) {
3220 NFSM_DISSECT(tl, u_int32_t *, 3*NFSX_UNSIGNED);
3221 ncookie.lval[0] = *tl++;
3222 ncookie.lval[1] = *tl++;
3223 attrflag = fxdr_unsigned(int, *tl);
3225 error = nfsm_loadattr(nd, &nfsva);
3229 NFSM_DISSECT(tl,u_int32_t *,NFSX_UNSIGNED);
3231 error = nfsm_getfh(nd, &nfhp);
3235 if (!attrflag && nfhp != NULL) {
3236 FREE((caddr_t)nfhp, M_NFSFH);
3241 nfsva.na_mntonfileno = 0xffffffff;
3242 error = nfsv4_loadattr(nd, NULL, &nfsva, &nfhp,
3243 NULL, 0, NULL, NULL, NULL, NULL, NULL, 0,
3244 NULL, NULL, &rderr, p, cred);
3250 if (nd->nd_flag & ND_NFSV4) {
3253 } else if (gotmnton) {
3254 if (nfsva.na_mntonfileno != 0xffffffff)
3255 dp->d_fileno = nfsva.na_mntonfileno;
3257 dp->d_fileno = nfsva.na_fileid;
3258 } else if (nfsva.na_filesid[0] ==
3259 dnp->n_vattr.na_filesid[0] &&
3260 nfsva.na_filesid[1] ==
3261 dnp->n_vattr.na_filesid[1]) {
3262 dp->d_fileno = nfsva.na_fileid;
3266 } while (fakefileno ==
3268 dp->d_fileno = fakefileno;
3271 dp->d_fileno = fileno;
3273 *tl2++ = cookiep->nfsuquad[0] = cookie.lval[0] =
3275 *tl2 = cookiep->nfsuquad[1] = cookie.lval[1] =
3279 if (NFSRV_CMPFH(nfhp->nfh_fh, nfhp->nfh_len,
3280 dnp->n_fhp->nfh_fh, dnp->n_fhp->nfh_len)) {
3284 FREE((caddr_t)nfhp, M_NFSFH);
3286 } else if (isdotdot != 0) {
3288 * Skip doing a nfscl_nget() call for "..".
3289 * There's a race between acquiring the nfs
3290 * node here and lookups that look for the
3291 * directory being read (in the parent).
3292 * It would try to get a lock on ".." here,
3293 * owning the lock on the directory being
3294 * read. Lookup will hold the lock on ".."
3295 * and try to acquire the lock on the
3296 * directory being read.
3297 * If the directory is unlocked/relocked,
3298 * then there is a LOR with the buflock
3301 free(nfhp, M_NFSFH);
3303 error = nfscl_nget(vnode_mount(vp), vp,
3304 nfhp, cnp, p, &np, NULL, LK_EXCLUSIVE);
3311 if (newvp != NULLVP) {
3312 error = nfscl_loadattrcache(&newvp,
3313 &nfsva, NULL, NULL, 0, 0);
3322 vtonfs_dtype(np->n_vattr.na_type);
3324 NFSCNHASH(cnp, HASHINIT);
3325 if (cnp->cn_namelen <= NCHNAMLEN &&
3326 (newvp->v_type != VDIR ||
3327 dctime.tv_sec != 0)) {
3328 cache_enter_time(ndp->ni_dvp,
3331 newvp->v_type != VDIR ? NULL :
3341 } else if (nfhp != NULL) {
3342 FREE((caddr_t)nfhp, M_NFSFH);
3344 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3345 more_dirs = fxdr_unsigned(int, *tl);
3348 * If at end of rpc data, get the eof boolean
3351 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3352 eof = fxdr_unsigned(int, *tl);
3355 if (nd->nd_flag & ND_NFSV4) {
3356 error = nfscl_postop_attr(nd, nap, attrflagp,
3362 mbuf_freem(nd->nd_mrep);
3366 * Fill last record, iff any, out to a multiple of DIRBLKSIZ
3367 * by increasing d_reclen for the last record.
3370 left = DIRBLKSIZ - blksiz;
3371 dp->d_reclen += left;
3372 uio_iov_base_add(uiop, left);
3373 uio_iov_len_add(uiop, -(left));
3374 uio_uio_resid_add(uiop, -(left));
3375 uiop->uio_offset += left;
3379 * If returning no data, assume end of file.
3380 * If not bigenough, return not end of file, since you aren't
3381 * returning all the data
3382 * Otherwise, return the eof flag from the server.
3385 if (tresid == uio_uio_resid(uiop))
3387 else if (!bigenough)
3394 * Add extra empty records to any remaining DIRBLKSIZ chunks.
3396 while (uio_uio_resid(uiop) > 0 && uio_uio_resid(uiop) != tresid) {
3397 dp = (struct dirent *)uio_iov_base(uiop);
3398 dp->d_type = DT_UNKNOWN;
3401 dp->d_name[0] = '\0';
3402 tl = (u_int32_t *)&dp->d_name[4];
3403 *tl++ = cookie.lval[0];
3404 *tl = cookie.lval[1];
3405 dp->d_reclen = DIRBLKSIZ;
3406 uio_iov_base_add(uiop, DIRBLKSIZ);
3407 uio_iov_len_add(uiop, -(DIRBLKSIZ));
3408 uio_uio_resid_add(uiop, -(DIRBLKSIZ));
3409 uiop->uio_offset += DIRBLKSIZ;
3413 if (nd->nd_mrep != NULL)
3414 mbuf_freem(nd->nd_mrep);
3423 nfsrpc_commit(vnode_t vp, u_quad_t offset, int cnt, struct ucred *cred,
3424 NFSPROC_T *p, u_char *verfp, struct nfsvattr *nap, int *attrflagp,
3428 struct nfsrv_descript nfsd, *nd = &nfsd;
3429 nfsattrbit_t attrbits;
3433 NFSCL_REQSTART(nd, NFSPROC_COMMIT, vp);
3434 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
3435 txdr_hyper(offset, tl);
3437 *tl = txdr_unsigned(cnt);
3438 if (nd->nd_flag & ND_NFSV4) {
3440 * And do a Getattr op.
3442 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
3443 *tl = txdr_unsigned(NFSV4OP_GETATTR);
3444 NFSGETATTR_ATTRBIT(&attrbits);
3445 (void) nfsrv_putattrbit(nd, &attrbits);
3447 error = nfscl_request(nd, vp, p, cred, stuff);
3450 error = nfscl_wcc_data(nd, vp, nap, attrflagp, NULL, stuff);
3451 if (!error && !nd->nd_repstat) {
3452 NFSM_DISSECT(tl, u_int32_t *, NFSX_VERF);
3453 NFSBCOPY((caddr_t)tl, verfp, NFSX_VERF);
3454 if (nd->nd_flag & ND_NFSV4)
3455 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
3458 if (!error && nd->nd_repstat)
3459 error = nd->nd_repstat;
3460 mbuf_freem(nd->nd_mrep);
3465 * NFS byte range lock rpc.
3466 * (Mostly just calls one of the three lower level RPC routines.)
3469 nfsrpc_advlock(vnode_t vp, off_t size, int op, struct flock *fl,
3470 int reclaim, struct ucred *cred, NFSPROC_T *p, void *id, int flags)
3472 struct nfscllockowner *lp;
3473 struct nfsclclient *clp;
3475 struct nfsrv_descript nfsd, *nd = &nfsd;
3476 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
3479 u_int32_t clidrev = 0;
3480 int error = 0, newone = 0, expireret = 0, retrycnt, donelocally;
3484 * Convert the flock structure into a start and end and do POSIX
3487 switch (fl->l_whence) {
3491 * Caller is responsible for adding any necessary offset
3492 * when SEEK_CUR is used.
3494 start = fl->l_start;
3498 start = size + fl->l_start;
3499 off = size + fl->l_start;
3506 if (fl->l_len != 0) {
3507 end = start + fl->l_len - 1;
3518 if (op == F_GETLK) {
3519 error = nfscl_getcl(vp, cred, p, &clp);
3522 error = nfscl_lockt(vp, clp, off, len, fl, p, id, flags);
3524 clidrev = clp->nfsc_clientidrev;
3525 error = nfsrpc_lockt(nd, vp, clp, off, len, fl, cred,
3527 } else if (error == -1) {
3530 nfscl_clientrelease(clp);
3531 } else if (op == F_UNLCK && fl->l_type == F_UNLCK) {
3533 * We must loop around for all lockowner cases.
3536 error = nfscl_getcl(vp, cred, p, &clp);
3540 error = nfscl_relbytelock(vp, off, len, cred, p, callcnt,
3541 clp, id, flags, &lp, &dorpc);
3543 * If it returns a NULL lp, we're done.
3547 nfscl_clientrelease(clp);
3549 nfscl_releasealllocks(clp, vp, p, id, flags);
3552 if (nmp->nm_clp != NULL)
3553 clidrev = nmp->nm_clp->nfsc_clientidrev;
3557 * If the server doesn't support Posix lock semantics,
3558 * only allow locks on the entire file, since it won't
3559 * handle overlapping byte ranges.
3560 * There might still be a problem when a lock
3561 * upgrade/downgrade (read<->write) occurs, since the
3562 * server "might" expect an unlock first?
3564 if (dorpc && (lp->nfsl_open->nfso_posixlock ||
3565 (off == 0 && len == NFS64BITSSET))) {
3567 * Since the lock records will go away, we must
3568 * wait for grace and delay here.
3571 error = nfsrpc_locku(nd, nmp, lp, off, len,
3572 NFSV4LOCKT_READ, cred, p, 0);
3573 if ((nd->nd_repstat == NFSERR_GRACE ||
3574 nd->nd_repstat == NFSERR_DELAY) &&
3576 (void) nfs_catnap(PZERO, (int)nd->nd_repstat,
3578 } while ((nd->nd_repstat == NFSERR_GRACE ||
3579 nd->nd_repstat == NFSERR_DELAY) && error == 0);
3582 } while (error == 0 && nd->nd_repstat == 0);
3583 nfscl_releasealllocks(clp, vp, p, id, flags);
3584 } else if (op == F_SETLK) {
3585 error = nfscl_getbytelock(vp, off, len, fl->l_type, cred, p,
3586 NULL, 0, id, flags, NULL, NULL, &lp, &newone, &donelocally);
3587 if (error || donelocally) {
3590 if (nmp->nm_clp != NULL)
3591 clidrev = nmp->nm_clp->nfsc_clientidrev;
3594 nfhp = VTONFS(vp)->n_fhp;
3595 if (!lp->nfsl_open->nfso_posixlock &&
3596 (off != 0 || len != NFS64BITSSET)) {
3599 error = nfsrpc_lock(nd, nmp, vp, nfhp->nfh_fh,
3600 nfhp->nfh_len, lp, newone, reclaim, off,
3601 len, fl->l_type, cred, p, 0);
3604 error = nd->nd_repstat;
3605 nfscl_lockrelease(lp, error, newone);
3610 error = nd->nd_repstat;
3611 if (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
3612 error == NFSERR_STALEDONTRECOVER ||
3613 error == NFSERR_STALECLIENTID || error == NFSERR_DELAY) {
3614 (void) nfs_catnap(PZERO, error, "nfs_advlock");
3615 } else if ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID)
3617 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
3620 } while (error == NFSERR_GRACE ||
3621 error == NFSERR_STALECLIENTID || error == NFSERR_DELAY ||
3622 error == NFSERR_STALEDONTRECOVER || error == NFSERR_STALESTATEID ||
3623 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
3624 expireret == 0 && clidrev != 0 && retrycnt < 4));
3625 if (error && retrycnt >= 4)
3631 * The lower level routine for the LockT case.
3634 nfsrpc_lockt(struct nfsrv_descript *nd, vnode_t vp,
3635 struct nfsclclient *clp, u_int64_t off, u_int64_t len, struct flock *fl,
3636 struct ucred *cred, NFSPROC_T *p, void *id, int flags)
3639 int error, type, size;
3640 uint8_t own[NFSV4CL_LOCKNAMELEN + NFSX_V4FHMAX];
3643 NFSCL_REQSTART(nd, NFSPROC_LOCKT, vp);
3644 NFSM_BUILD(tl, u_int32_t *, 7 * NFSX_UNSIGNED);
3645 if (fl->l_type == F_RDLCK)
3646 *tl++ = txdr_unsigned(NFSV4LOCKT_READ);
3648 *tl++ = txdr_unsigned(NFSV4LOCKT_WRITE);
3649 txdr_hyper(off, tl);
3651 txdr_hyper(len, tl);
3653 *tl++ = clp->nfsc_clientid.lval[0];
3654 *tl = clp->nfsc_clientid.lval[1];
3655 nfscl_filllockowner(id, own, flags);
3657 NFSBCOPY(np->n_fhp->nfh_fh, &own[NFSV4CL_LOCKNAMELEN],
3658 np->n_fhp->nfh_len);
3659 (void)nfsm_strtom(nd, own, NFSV4CL_LOCKNAMELEN + np->n_fhp->nfh_len);
3660 error = nfscl_request(nd, vp, p, cred, NULL);
3663 if (nd->nd_repstat == 0) {
3664 fl->l_type = F_UNLCK;
3665 } else if (nd->nd_repstat == NFSERR_DENIED) {
3667 fl->l_whence = SEEK_SET;
3668 NFSM_DISSECT(tl, u_int32_t *, 8 * NFSX_UNSIGNED);
3669 fl->l_start = fxdr_hyper(tl);
3671 len = fxdr_hyper(tl);
3673 if (len == NFS64BITSSET)
3677 type = fxdr_unsigned(int, *tl++);
3678 if (type == NFSV4LOCKT_WRITE)
3679 fl->l_type = F_WRLCK;
3681 fl->l_type = F_RDLCK;
3683 * XXX For now, I have no idea what to do with the
3684 * conflicting lock_owner, so I'll just set the pid == 0
3685 * and skip over the lock_owner.
3687 fl->l_pid = (pid_t)0;
3689 size = fxdr_unsigned(int, *tl);
3690 if (size < 0 || size > NFSV4_OPAQUELIMIT)
3693 error = nfsm_advance(nd, NFSM_RNDUP(size), -1);
3694 } else if (nd->nd_repstat == NFSERR_STALECLIENTID)
3695 nfscl_initiate_recovery(clp);
3697 mbuf_freem(nd->nd_mrep);
3702 * Lower level function that performs the LockU RPC.
3705 nfsrpc_locku(struct nfsrv_descript *nd, struct nfsmount *nmp,
3706 struct nfscllockowner *lp, u_int64_t off, u_int64_t len,
3707 u_int32_t type, struct ucred *cred, NFSPROC_T *p, int syscred)
3712 nfscl_reqstart(nd, NFSPROC_LOCKU, nmp, lp->nfsl_open->nfso_fh,
3713 lp->nfsl_open->nfso_fhlen, NULL);
3714 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID + 6 * NFSX_UNSIGNED);
3715 *tl++ = txdr_unsigned(type);
3716 *tl = txdr_unsigned(lp->nfsl_seqid);
3717 if (nfstest_outofseq &&
3718 (arc4random() % nfstest_outofseq) == 0)
3719 *tl = txdr_unsigned(lp->nfsl_seqid + 1);
3721 *tl++ = lp->nfsl_stateid.seqid;
3722 *tl++ = lp->nfsl_stateid.other[0];
3723 *tl++ = lp->nfsl_stateid.other[1];
3724 *tl++ = lp->nfsl_stateid.other[2];
3725 txdr_hyper(off, tl);
3727 txdr_hyper(len, tl);
3729 nd->nd_flag |= ND_USEGSSNAME;
3730 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
3731 NFS_PROG, NFS_VER4, NULL, 1, NULL);
3732 NFSCL_INCRSEQID(lp->nfsl_seqid, nd);
3735 if (nd->nd_repstat == 0) {
3736 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID);
3737 lp->nfsl_stateid.seqid = *tl++;
3738 lp->nfsl_stateid.other[0] = *tl++;
3739 lp->nfsl_stateid.other[1] = *tl++;
3740 lp->nfsl_stateid.other[2] = *tl;
3741 } else if (nd->nd_repstat == NFSERR_STALESTATEID)
3742 nfscl_initiate_recovery(lp->nfsl_open->nfso_own->nfsow_clp);
3744 mbuf_freem(nd->nd_mrep);
3749 * The actual Lock RPC.
3752 nfsrpc_lock(struct nfsrv_descript *nd, struct nfsmount *nmp, vnode_t vp,
3753 u_int8_t *nfhp, int fhlen, struct nfscllockowner *lp, int newone,
3754 int reclaim, u_int64_t off, u_int64_t len, short type, struct ucred *cred,
3755 NFSPROC_T *p, int syscred)
3759 uint8_t own[NFSV4CL_LOCKNAMELEN + NFSX_V4FHMAX];
3761 nfscl_reqstart(nd, NFSPROC_LOCK, nmp, nfhp, fhlen, NULL);
3762 NFSM_BUILD(tl, u_int32_t *, 7 * NFSX_UNSIGNED);
3763 if (type == F_RDLCK)
3764 *tl++ = txdr_unsigned(NFSV4LOCKT_READ);
3766 *tl++ = txdr_unsigned(NFSV4LOCKT_WRITE);
3767 *tl++ = txdr_unsigned(reclaim);
3768 txdr_hyper(off, tl);
3770 txdr_hyper(len, tl);
3774 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID +
3775 2 * NFSX_UNSIGNED + NFSX_HYPER);
3776 *tl++ = txdr_unsigned(lp->nfsl_open->nfso_own->nfsow_seqid);
3777 *tl++ = lp->nfsl_open->nfso_stateid.seqid;
3778 *tl++ = lp->nfsl_open->nfso_stateid.other[0];
3779 *tl++ = lp->nfsl_open->nfso_stateid.other[1];
3780 *tl++ = lp->nfsl_open->nfso_stateid.other[2];
3781 *tl++ = txdr_unsigned(lp->nfsl_seqid);
3782 *tl++ = lp->nfsl_open->nfso_own->nfsow_clp->nfsc_clientid.lval[0];
3783 *tl = lp->nfsl_open->nfso_own->nfsow_clp->nfsc_clientid.lval[1];
3784 NFSBCOPY(lp->nfsl_owner, own, NFSV4CL_LOCKNAMELEN);
3785 NFSBCOPY(nfhp, &own[NFSV4CL_LOCKNAMELEN], fhlen);
3786 (void)nfsm_strtom(nd, own, NFSV4CL_LOCKNAMELEN + fhlen);
3789 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID + NFSX_UNSIGNED);
3790 *tl++ = lp->nfsl_stateid.seqid;
3791 *tl++ = lp->nfsl_stateid.other[0];
3792 *tl++ = lp->nfsl_stateid.other[1];
3793 *tl++ = lp->nfsl_stateid.other[2];
3794 *tl = txdr_unsigned(lp->nfsl_seqid);
3795 if (nfstest_outofseq &&
3796 (arc4random() % nfstest_outofseq) == 0)
3797 *tl = txdr_unsigned(lp->nfsl_seqid + 1);
3800 nd->nd_flag |= ND_USEGSSNAME;
3801 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, vp, p, cred,
3802 NFS_PROG, NFS_VER4, NULL, 1, NULL);
3806 NFSCL_INCRSEQID(lp->nfsl_open->nfso_own->nfsow_seqid, nd);
3807 NFSCL_INCRSEQID(lp->nfsl_seqid, nd);
3808 if (nd->nd_repstat == 0) {
3809 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID);
3810 lp->nfsl_stateid.seqid = *tl++;
3811 lp->nfsl_stateid.other[0] = *tl++;
3812 lp->nfsl_stateid.other[1] = *tl++;
3813 lp->nfsl_stateid.other[2] = *tl;
3814 } else if (nd->nd_repstat == NFSERR_DENIED) {
3815 NFSM_DISSECT(tl, u_int32_t *, 8 * NFSX_UNSIGNED);
3816 size = fxdr_unsigned(int, *(tl + 7));
3817 if (size < 0 || size > NFSV4_OPAQUELIMIT)
3820 error = nfsm_advance(nd, NFSM_RNDUP(size), -1);
3821 } else if (nd->nd_repstat == NFSERR_STALESTATEID)
3822 nfscl_initiate_recovery(lp->nfsl_open->nfso_own->nfsow_clp);
3824 mbuf_freem(nd->nd_mrep);
3830 * (always called with the vp for the mount point)
3833 nfsrpc_statfs(vnode_t vp, struct nfsstatfs *sbp, struct nfsfsinfo *fsp,
3834 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp,
3837 u_int32_t *tl = NULL;
3838 struct nfsrv_descript nfsd, *nd = &nfsd;
3839 struct nfsmount *nmp;
3840 nfsattrbit_t attrbits;
3844 nmp = VFSTONFS(vnode_mount(vp));
3845 if (NFSHASNFSV4(nmp)) {
3847 * For V4, you actually do a getattr.
3849 NFSCL_REQSTART(nd, NFSPROC_GETATTR, vp);
3850 NFSSTATFS_GETATTRBIT(&attrbits);
3851 (void) nfsrv_putattrbit(nd, &attrbits);
3852 nd->nd_flag |= ND_USEGSSNAME;
3853 error = nfscl_request(nd, vp, p, cred, stuff);
3856 if (nd->nd_repstat == 0) {
3857 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0,
3858 NULL, NULL, sbp, fsp, NULL, 0, NULL, NULL, NULL, p,
3861 nmp->nm_fsid[0] = nap->na_filesid[0];
3862 nmp->nm_fsid[1] = nap->na_filesid[1];
3863 NFSSETHASSETFSID(nmp);
3867 error = nd->nd_repstat;
3872 NFSCL_REQSTART(nd, NFSPROC_FSSTAT, vp);
3873 error = nfscl_request(nd, vp, p, cred, stuff);
3876 if (nd->nd_flag & ND_NFSV3) {
3877 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
3881 if (nd->nd_repstat) {
3882 error = nd->nd_repstat;
3885 NFSM_DISSECT(tl, u_int32_t *,
3886 NFSX_STATFS(nd->nd_flag & ND_NFSV3));
3888 if (NFSHASNFSV3(nmp)) {
3889 sbp->sf_tbytes = fxdr_hyper(tl); tl += 2;
3890 sbp->sf_fbytes = fxdr_hyper(tl); tl += 2;
3891 sbp->sf_abytes = fxdr_hyper(tl); tl += 2;
3892 sbp->sf_tfiles = fxdr_hyper(tl); tl += 2;
3893 sbp->sf_ffiles = fxdr_hyper(tl); tl += 2;
3894 sbp->sf_afiles = fxdr_hyper(tl); tl += 2;
3895 sbp->sf_invarsec = fxdr_unsigned(u_int32_t, *tl);
3896 } else if (NFSHASNFSV4(nmp) == 0) {
3897 sbp->sf_tsize = fxdr_unsigned(u_int32_t, *tl++);
3898 sbp->sf_bsize = fxdr_unsigned(u_int32_t, *tl++);
3899 sbp->sf_blocks = fxdr_unsigned(u_int32_t, *tl++);
3900 sbp->sf_bfree = fxdr_unsigned(u_int32_t, *tl++);
3901 sbp->sf_bavail = fxdr_unsigned(u_int32_t, *tl);
3904 mbuf_freem(nd->nd_mrep);
3912 nfsrpc_pathconf(vnode_t vp, struct nfsv3_pathconf *pc,
3913 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp,
3916 struct nfsrv_descript nfsd, *nd = &nfsd;
3917 struct nfsmount *nmp;
3919 nfsattrbit_t attrbits;
3923 nmp = VFSTONFS(vnode_mount(vp));
3924 if (NFSHASNFSV4(nmp)) {
3926 * For V4, you actually do a getattr.
3928 NFSCL_REQSTART(nd, NFSPROC_GETATTR, vp);
3929 NFSPATHCONF_GETATTRBIT(&attrbits);
3930 (void) nfsrv_putattrbit(nd, &attrbits);
3931 nd->nd_flag |= ND_USEGSSNAME;
3932 error = nfscl_request(nd, vp, p, cred, stuff);
3935 if (nd->nd_repstat == 0) {
3936 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0,
3937 pc, NULL, NULL, NULL, NULL, 0, NULL, NULL, NULL, p,
3942 error = nd->nd_repstat;
3945 NFSCL_REQSTART(nd, NFSPROC_PATHCONF, vp);
3946 error = nfscl_request(nd, vp, p, cred, stuff);
3949 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
3950 if (nd->nd_repstat && !error)
3951 error = nd->nd_repstat;
3953 NFSM_DISSECT(tl, u_int32_t *, NFSX_V3PATHCONF);
3954 pc->pc_linkmax = fxdr_unsigned(u_int32_t, *tl++);
3955 pc->pc_namemax = fxdr_unsigned(u_int32_t, *tl++);
3956 pc->pc_notrunc = fxdr_unsigned(u_int32_t, *tl++);
3957 pc->pc_chownrestricted =
3958 fxdr_unsigned(u_int32_t, *tl++);
3959 pc->pc_caseinsensitive =
3960 fxdr_unsigned(u_int32_t, *tl++);
3961 pc->pc_casepreserving = fxdr_unsigned(u_int32_t, *tl);
3965 mbuf_freem(nd->nd_mrep);
3970 * nfs version 3 fsinfo rpc call
3973 nfsrpc_fsinfo(vnode_t vp, struct nfsfsinfo *fsp, struct ucred *cred,
3974 NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, void *stuff)
3977 struct nfsrv_descript nfsd, *nd = &nfsd;
3981 NFSCL_REQSTART(nd, NFSPROC_FSINFO, vp);
3982 error = nfscl_request(nd, vp, p, cred, stuff);
3985 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
3986 if (nd->nd_repstat && !error)
3987 error = nd->nd_repstat;
3989 NFSM_DISSECT(tl, u_int32_t *, NFSX_V3FSINFO);
3990 fsp->fs_rtmax = fxdr_unsigned(u_int32_t, *tl++);
3991 fsp->fs_rtpref = fxdr_unsigned(u_int32_t, *tl++);
3992 fsp->fs_rtmult = fxdr_unsigned(u_int32_t, *tl++);
3993 fsp->fs_wtmax = fxdr_unsigned(u_int32_t, *tl++);
3994 fsp->fs_wtpref = fxdr_unsigned(u_int32_t, *tl++);
3995 fsp->fs_wtmult = fxdr_unsigned(u_int32_t, *tl++);
3996 fsp->fs_dtpref = fxdr_unsigned(u_int32_t, *tl++);
3997 fsp->fs_maxfilesize = fxdr_hyper(tl);
3999 fxdr_nfsv3time(tl, &fsp->fs_timedelta);
4001 fsp->fs_properties = fxdr_unsigned(u_int32_t, *tl);
4004 mbuf_freem(nd->nd_mrep);
4009 * This function performs the Renew RPC.
4012 nfsrpc_renew(struct nfsclclient *clp, struct ucred *cred, NFSPROC_T *p)
4015 struct nfsrv_descript nfsd;
4016 struct nfsrv_descript *nd = &nfsd;
4017 struct nfsmount *nmp;
4020 nmp = clp->nfsc_nmp;
4023 nfscl_reqstart(nd, NFSPROC_RENEW, nmp, NULL, 0, NULL);
4024 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
4025 *tl++ = clp->nfsc_clientid.lval[0];
4026 *tl = clp->nfsc_clientid.lval[1];
4027 nd->nd_flag |= ND_USEGSSNAME;
4028 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4029 NFS_PROG, NFS_VER4, NULL, 1, NULL);
4032 error = nd->nd_repstat;
4033 mbuf_freem(nd->nd_mrep);
4038 * This function performs the Releaselockowner RPC.
4041 nfsrpc_rellockown(struct nfsmount *nmp, struct nfscllockowner *lp,
4042 uint8_t *fh, int fhlen, struct ucred *cred, NFSPROC_T *p)
4044 struct nfsrv_descript nfsd, *nd = &nfsd;
4047 uint8_t own[NFSV4CL_LOCKNAMELEN + NFSX_V4FHMAX];
4049 nfscl_reqstart(nd, NFSPROC_RELEASELCKOWN, nmp, NULL, 0, NULL);
4050 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
4051 *tl++ = nmp->nm_clp->nfsc_clientid.lval[0];
4052 *tl = nmp->nm_clp->nfsc_clientid.lval[1];
4053 NFSBCOPY(lp->nfsl_owner, own, NFSV4CL_LOCKNAMELEN);
4054 NFSBCOPY(fh, &own[NFSV4CL_LOCKNAMELEN], fhlen);
4055 (void)nfsm_strtom(nd, own, NFSV4CL_LOCKNAMELEN + fhlen);
4056 nd->nd_flag |= ND_USEGSSNAME;
4057 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4058 NFS_PROG, NFS_VER4, NULL, 1, NULL);
4061 error = nd->nd_repstat;
4062 mbuf_freem(nd->nd_mrep);
4067 * This function performs the Compound to get the mount pt FH.
4070 nfsrpc_getdirpath(struct nfsmount *nmp, u_char *dirpath, struct ucred *cred,
4074 struct nfsrv_descript nfsd;
4075 struct nfsrv_descript *nd = &nfsd;
4077 int error, cnt, len, setnil;
4080 nfscl_reqstart(nd, NFSPROC_PUTROOTFH, nmp, NULL, 0, &opcntp);
4088 while (*cp2 != '\0' && *cp2 != '/')
4095 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
4096 *tl = txdr_unsigned(NFSV4OP_LOOKUP);
4097 nfsm_strtom(nd, cp, strlen(cp));
4103 } while (*cp != '\0');
4104 *opcntp = txdr_unsigned(2 + cnt);
4105 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
4106 *tl = txdr_unsigned(NFSV4OP_GETFH);
4107 nd->nd_flag |= ND_USEGSSNAME;
4108 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4109 NFS_PROG, NFS_VER4, NULL, 1, NULL);
4112 if (nd->nd_repstat == 0) {
4113 NFSM_DISSECT(tl, u_int32_t *, (3 + 2 * cnt) * NFSX_UNSIGNED);
4114 tl += (2 + 2 * cnt);
4115 if ((len = fxdr_unsigned(int, *tl)) <= 0 ||
4117 nd->nd_repstat = NFSERR_BADXDR;
4119 nd->nd_repstat = nfsrv_mtostr(nd, nmp->nm_fh, len);
4120 if (nd->nd_repstat == 0)
4121 nmp->nm_fhsize = len;
4124 error = nd->nd_repstat;
4126 mbuf_freem(nd->nd_mrep);
4131 * This function performs the Delegreturn RPC.
4134 nfsrpc_delegreturn(struct nfscldeleg *dp, struct ucred *cred,
4135 struct nfsmount *nmp, NFSPROC_T *p, int syscred)
4138 struct nfsrv_descript nfsd;
4139 struct nfsrv_descript *nd = &nfsd;
4142 nfscl_reqstart(nd, NFSPROC_DELEGRETURN, nmp, dp->nfsdl_fh,
4143 dp->nfsdl_fhlen, NULL);
4144 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID);
4145 *tl++ = dp->nfsdl_stateid.seqid;
4146 *tl++ = dp->nfsdl_stateid.other[0];
4147 *tl++ = dp->nfsdl_stateid.other[1];
4148 *tl = dp->nfsdl_stateid.other[2];
4150 nd->nd_flag |= ND_USEGSSNAME;
4151 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4152 NFS_PROG, NFS_VER4, NULL, 1, NULL);
4155 error = nd->nd_repstat;
4156 mbuf_freem(nd->nd_mrep);
4164 nfsrpc_getacl(vnode_t vp, struct ucred *cred, NFSPROC_T *p,
4165 struct acl *aclp, void *stuff)
4167 struct nfsrv_descript nfsd, *nd = &nfsd;
4169 nfsattrbit_t attrbits;
4170 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
4172 if (nfsrv_useacl == 0 || !NFSHASNFSV4(nmp))
4173 return (EOPNOTSUPP);
4174 NFSCL_REQSTART(nd, NFSPROC_GETACL, vp);
4175 NFSZERO_ATTRBIT(&attrbits);
4176 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_ACL);
4177 (void) nfsrv_putattrbit(nd, &attrbits);
4178 error = nfscl_request(nd, vp, p, cred, stuff);
4181 if (!nd->nd_repstat)
4182 error = nfsv4_loadattr(nd, vp, NULL, NULL, NULL, 0, NULL,
4183 NULL, NULL, NULL, aclp, 0, NULL, NULL, NULL, p, cred);
4185 error = nd->nd_repstat;
4186 mbuf_freem(nd->nd_mrep);
4194 nfsrpc_setacl(vnode_t vp, struct ucred *cred, NFSPROC_T *p,
4195 struct acl *aclp, void *stuff)
4198 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
4200 if (nfsrv_useacl == 0 || !NFSHASNFSV4(nmp))
4201 return (EOPNOTSUPP);
4202 error = nfsrpc_setattr(vp, NULL, aclp, cred, p, NULL, NULL, stuff);
4210 nfsrpc_setaclrpc(vnode_t vp, struct ucred *cred, NFSPROC_T *p,
4211 struct acl *aclp, nfsv4stateid_t *stateidp, void *stuff)
4213 struct nfsrv_descript nfsd, *nd = &nfsd;
4215 nfsattrbit_t attrbits;
4216 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
4218 if (!NFSHASNFSV4(nmp))
4219 return (EOPNOTSUPP);
4220 NFSCL_REQSTART(nd, NFSPROC_SETACL, vp);
4221 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID);
4222 NFSZERO_ATTRBIT(&attrbits);
4223 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_ACL);
4224 (void) nfsv4_fillattr(nd, vnode_mount(vp), vp, aclp, NULL, NULL, 0,
4225 &attrbits, NULL, NULL, 0, 0, 0, 0, (uint64_t)0);
4226 error = nfscl_request(nd, vp, p, cred, stuff);
4229 /* Don't care about the pre/postop attributes */
4230 mbuf_freem(nd->nd_mrep);
4231 return (nd->nd_repstat);