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);
1138 else if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) ==
1140 /* Load the directory attributes. */
1141 error = nfsm_loadattr(nd, dnap);
1147 if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) == ND_NFSV4) {
1148 /* Load the directory attributes. */
1149 error = nfsm_loadattr(nd, dnap);
1153 /* Skip over the Lookup and GetFH operation status values. */
1154 NFSM_DISSECT(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1156 error = nfsm_getfh(nd, nfhpp);
1160 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
1161 if ((nd->nd_flag & ND_NFSV3) && !error)
1162 error = nfscl_postop_attr(nd, dnap, dattrflagp, stuff);
1164 mbuf_freem(nd->nd_mrep);
1165 if (!error && nd->nd_repstat)
1166 error = nd->nd_repstat;
1171 * Do a readlink rpc.
1174 nfsrpc_readlink(vnode_t vp, struct uio *uiop, struct ucred *cred,
1175 NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, void *stuff)
1178 struct nfsrv_descript nfsd, *nd = &nfsd;
1179 struct nfsnode *np = VTONFS(vp);
1180 nfsattrbit_t attrbits;
1181 int error, len, cangetattr = 1;
1184 NFSCL_REQSTART(nd, NFSPROC_READLINK, vp);
1185 if (nd->nd_flag & ND_NFSV4) {
1187 * And do a Getattr op.
1189 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1190 *tl = txdr_unsigned(NFSV4OP_GETATTR);
1191 NFSGETATTR_ATTRBIT(&attrbits);
1192 (void) nfsrv_putattrbit(nd, &attrbits);
1194 error = nfscl_request(nd, vp, p, cred, stuff);
1197 if (nd->nd_flag & ND_NFSV3)
1198 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
1199 if (!nd->nd_repstat && !error) {
1200 NFSM_STRSIZ(len, NFS_MAXPATHLEN);
1202 * This seems weird to me, but must have been added to
1203 * FreeBSD for some reason. The only thing I can think of
1204 * is that there was/is some server that replies with
1205 * more link data than it should?
1207 if (len == NFS_MAXPATHLEN) {
1209 if (np->n_size > 0 && np->n_size < NFS_MAXPATHLEN) {
1215 error = nfsm_mbufuio(nd, uiop, len);
1216 if ((nd->nd_flag & ND_NFSV4) && !error && cangetattr)
1217 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
1219 if (nd->nd_repstat && !error)
1220 error = nd->nd_repstat;
1222 mbuf_freem(nd->nd_mrep);
1230 nfsrpc_read(vnode_t vp, struct uio *uiop, struct ucred *cred,
1231 NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, void *stuff)
1233 int error, expireret = 0, retrycnt;
1234 u_int32_t clidrev = 0;
1235 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
1236 struct nfsnode *np = VTONFS(vp);
1237 struct ucred *newcred;
1238 struct nfsfh *nfhp = NULL;
1239 nfsv4stateid_t stateid;
1242 if (nmp->nm_clp != NULL)
1243 clidrev = nmp->nm_clp->nfsc_clientidrev;
1245 if (NFSHASNFSV4(nmp)) {
1247 newcred = NFSNEWCRED(cred);
1252 if (NFSHASNFSV4(nmp))
1253 (void)nfscl_getstateid(vp, nfhp->nfh_fh, nfhp->nfh_len,
1254 NFSV4OPEN_ACCESSREAD, newcred, p, &stateid, &lckp);
1255 error = nfsrpc_readrpc(vp, uiop, newcred, &stateid, p, nap,
1257 if (error == NFSERR_STALESTATEID)
1258 nfscl_initiate_recovery(nmp->nm_clp);
1260 nfscl_lockderef(lckp);
1261 if (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
1262 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
1263 error == NFSERR_OLDSTATEID) {
1264 (void) nfs_catnap(PZERO, error, "nfs_read");
1265 } else if ((error == NFSERR_EXPIRED ||
1266 error == NFSERR_BADSTATEID) && clidrev != 0) {
1267 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
1270 } while (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
1271 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
1272 (error == NFSERR_OLDSTATEID && retrycnt < 20) ||
1273 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
1274 expireret == 0 && clidrev != 0 && retrycnt < 4));
1275 if (error && retrycnt >= 4)
1277 if (NFSHASNFSV4(nmp))
1278 NFSFREECRED(newcred);
1283 * The actual read RPC.
1286 nfsrpc_readrpc(vnode_t vp, struct uio *uiop, struct ucred *cred,
1287 nfsv4stateid_t *stateidp, NFSPROC_T *p, struct nfsvattr *nap,
1288 int *attrflagp, void *stuff)
1291 int error = 0, len, retlen, tsiz, eof = 0;
1292 struct nfsrv_descript nfsd;
1293 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
1294 struct nfsrv_descript *nd = &nfsd;
1299 tsiz = uio_uio_resid(uiop);
1300 tmp_off = uiop->uio_offset + tsiz;
1302 if (tmp_off > nmp->nm_maxfilesize || tmp_off < uiop->uio_offset) {
1306 rsize = nmp->nm_rsize;
1311 len = (tsiz > rsize) ? rsize : tsiz;
1312 NFSCL_REQSTART(nd, NFSPROC_READ, vp);
1313 if (nd->nd_flag & ND_NFSV4)
1314 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID);
1315 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED * 3);
1316 if (nd->nd_flag & ND_NFSV2) {
1317 *tl++ = txdr_unsigned(uiop->uio_offset);
1318 *tl++ = txdr_unsigned(len);
1321 txdr_hyper(uiop->uio_offset, tl);
1322 *(tl + 2) = txdr_unsigned(len);
1325 * Since I can't do a Getattr for NFSv4 for Write, there
1326 * doesn't seem any point in doing one here, either.
1327 * (See the comment in nfsrpc_writerpc() for more info.)
1329 error = nfscl_request(nd, vp, p, cred, stuff);
1332 if (nd->nd_flag & ND_NFSV3) {
1333 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
1334 } else if (!nd->nd_repstat && (nd->nd_flag & ND_NFSV2)) {
1335 error = nfsm_loadattr(nd, nap);
1339 if (nd->nd_repstat || error) {
1341 error = nd->nd_repstat;
1344 if (nd->nd_flag & ND_NFSV3) {
1345 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1346 eof = fxdr_unsigned(int, *(tl + 1));
1347 } else if (nd->nd_flag & ND_NFSV4) {
1348 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
1349 eof = fxdr_unsigned(int, *tl);
1351 NFSM_STRSIZ(retlen, len);
1352 error = nfsm_mbufuio(nd, uiop, retlen);
1355 mbuf_freem(nd->nd_mrep);
1358 if (!(nd->nd_flag & ND_NFSV2)) {
1359 if (eof || retlen == 0)
1361 } else if (retlen < len)
1366 if (nd->nd_mrep != NULL)
1367 mbuf_freem(nd->nd_mrep);
1372 * nfs write operation
1373 * When called_from_strategy != 0, it should return EIO for an error that
1374 * indicates recovery is in progress, so that the buffer will be left
1375 * dirty and be written back to the server later. If it loops around,
1376 * the recovery thread could get stuck waiting for the buffer and recovery
1377 * will then deadlock.
1380 nfsrpc_write(vnode_t vp, struct uio *uiop, int *iomode, int *must_commit,
1381 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp,
1382 void *stuff, int called_from_strategy)
1384 int error, expireret = 0, retrycnt, nostateid;
1385 u_int32_t clidrev = 0;
1386 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
1387 struct nfsnode *np = VTONFS(vp);
1388 struct ucred *newcred;
1389 struct nfsfh *nfhp = NULL;
1390 nfsv4stateid_t stateid;
1394 if (nmp->nm_clp != NULL)
1395 clidrev = nmp->nm_clp->nfsc_clientidrev;
1397 if (NFSHASNFSV4(nmp)) {
1398 newcred = NFSNEWCRED(cred);
1405 if (NFSHASNFSV4(nmp)) {
1406 (void)nfscl_getstateid(vp, nfhp->nfh_fh, nfhp->nfh_len,
1407 NFSV4OPEN_ACCESSWRITE, newcred, p, &stateid, &lckp);
1408 if (stateid.other[0] == 0 && stateid.other[1] == 0 &&
1409 stateid.other[2] == 0) {
1411 NFSCL_DEBUG(1, "stateid0 in write\n");
1416 * If there is no stateid for NFSv4, it means this is an
1417 * extraneous write after close. Basically a poorly
1418 * implemented buffer cache. Just don't do the write.
1423 error = nfsrpc_writerpc(vp, uiop, iomode, must_commit,
1424 newcred, &stateid, p, nap, attrflagp, stuff);
1425 if (error == NFSERR_STALESTATEID)
1426 nfscl_initiate_recovery(nmp->nm_clp);
1428 nfscl_lockderef(lckp);
1429 if (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
1430 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
1431 error == NFSERR_OLDSTATEID) {
1432 (void) nfs_catnap(PZERO, error, "nfs_write");
1433 } else if ((error == NFSERR_EXPIRED ||
1434 error == NFSERR_BADSTATEID) && clidrev != 0) {
1435 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
1438 } while (error == NFSERR_GRACE || error == NFSERR_DELAY ||
1439 ((error == NFSERR_STALESTATEID ||
1440 error == NFSERR_STALEDONTRECOVER) && called_from_strategy == 0) ||
1441 (error == NFSERR_OLDSTATEID && retrycnt < 20) ||
1442 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
1443 expireret == 0 && clidrev != 0 && retrycnt < 4));
1444 if (error != 0 && (retrycnt >= 4 ||
1445 ((error == NFSERR_STALESTATEID ||
1446 error == NFSERR_STALEDONTRECOVER) && called_from_strategy != 0)))
1448 if (NFSHASNFSV4(nmp))
1449 NFSFREECRED(newcred);
1454 * The actual write RPC.
1457 nfsrpc_writerpc(vnode_t vp, struct uio *uiop, int *iomode,
1458 int *must_commit, struct ucred *cred, nfsv4stateid_t *stateidp,
1459 NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, void *stuff)
1462 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
1463 struct nfsnode *np = VTONFS(vp);
1464 int error = 0, len, tsiz, rlen, commit, committed = NFSWRITE_FILESYNC;
1465 int wccflag = 0, wsize;
1467 struct nfsrv_descript nfsd;
1468 struct nfsrv_descript *nd = &nfsd;
1469 nfsattrbit_t attrbits;
1472 KASSERT(uiop->uio_iovcnt == 1, ("nfs: writerpc iovcnt > 1"));
1474 tsiz = uio_uio_resid(uiop);
1475 tmp_off = uiop->uio_offset + tsiz;
1477 if (tmp_off > nmp->nm_maxfilesize || tmp_off < uiop->uio_offset) {
1481 wsize = nmp->nm_wsize;
1483 nd->nd_mrep = NULL; /* NFSv2 sometimes does a write with */
1484 nd->nd_repstat = 0; /* uio_resid == 0, so the while is not done */
1487 len = (tsiz > wsize) ? wsize : tsiz;
1488 NFSCL_REQSTART(nd, NFSPROC_WRITE, vp);
1489 if (nd->nd_flag & ND_NFSV4) {
1490 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID);
1491 NFSM_BUILD(tl, u_int32_t *, NFSX_HYPER+2*NFSX_UNSIGNED);
1492 txdr_hyper(uiop->uio_offset, tl);
1494 *tl++ = txdr_unsigned(*iomode);
1495 *tl = txdr_unsigned(len);
1496 } else if (nd->nd_flag & ND_NFSV3) {
1497 NFSM_BUILD(tl, u_int32_t *, NFSX_HYPER+3*NFSX_UNSIGNED);
1498 txdr_hyper(uiop->uio_offset, tl);
1500 *tl++ = txdr_unsigned(len);
1501 *tl++ = txdr_unsigned(*iomode);
1502 *tl = txdr_unsigned(len);
1506 NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1508 * Not sure why someone changed this, since the
1509 * RFC clearly states that "beginoffset" and
1510 * "totalcount" are ignored, but it wouldn't
1511 * surprise me if there's a busted server out there.
1513 /* Set both "begin" and "current" to non-garbage. */
1514 x = txdr_unsigned((u_int32_t)uiop->uio_offset);
1515 *tl++ = x; /* "begin offset" */
1516 *tl++ = x; /* "current offset" */
1517 x = txdr_unsigned(len);
1518 *tl++ = x; /* total to this offset */
1519 *tl = x; /* size of this write */
1522 nfsm_uiombuf(nd, uiop, len);
1524 * Although it is tempting to do a normal Getattr Op in the
1525 * NFSv4 compound, the result can be a nearly hung client
1526 * system if the Getattr asks for Owner and/or OwnerGroup.
1527 * It occurs when the client can't map either the Owner or
1528 * Owner_group name in the Getattr reply to a uid/gid. When
1529 * there is a cache miss, the kernel does an upcall to the
1530 * nfsuserd. Then, it can try and read the local /etc/passwd
1531 * or /etc/group file. It can then block in getnewbuf(),
1532 * waiting for dirty writes to be pushed to the NFS server.
1533 * The only reason this doesn't result in a complete
1534 * deadlock, is that the upcall times out and allows
1535 * the write to complete. However, progress is so slow
1536 * that it might just as well be deadlocked.
1537 * As such, we get the rest of the attributes, but not
1538 * Owner or Owner_group.
1539 * nb: nfscl_loadattrcache() needs to be told that these
1540 * partial attributes from a write rpc are being
1541 * passed in, via a argument flag.
1543 if (nd->nd_flag & ND_NFSV4) {
1544 NFSWRITEGETATTR_ATTRBIT(&attrbits);
1545 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1546 *tl = txdr_unsigned(NFSV4OP_GETATTR);
1547 (void) nfsrv_putattrbit(nd, &attrbits);
1549 error = nfscl_request(nd, vp, p, cred, stuff);
1552 if (nd->nd_repstat) {
1554 * In case the rpc gets retried, roll
1555 * the uio fileds changed by nfsm_uiombuf()
1558 uiop->uio_offset -= len;
1559 uio_uio_resid_add(uiop, len);
1560 uio_iov_base_add(uiop, -len);
1561 uio_iov_len_add(uiop, len);
1563 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) {
1564 error = nfscl_wcc_data(nd, vp, nap, attrflagp,
1569 if (!nd->nd_repstat) {
1570 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) {
1571 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED
1573 rlen = fxdr_unsigned(int, *tl++);
1577 } else if (rlen < len) {
1578 backup = len - rlen;
1579 uio_iov_base_add(uiop, -(backup));
1580 uio_iov_len_add(uiop, backup);
1581 uiop->uio_offset -= backup;
1582 uio_uio_resid_add(uiop, backup);
1585 commit = fxdr_unsigned(int, *tl++);
1588 * Return the lowest committment level
1589 * obtained by any of the RPCs.
1591 if (committed == NFSWRITE_FILESYNC)
1593 else if (committed == NFSWRITE_DATASYNC &&
1594 commit == NFSWRITE_UNSTABLE)
1597 if (!NFSHASWRITEVERF(nmp)) {
1598 NFSBCOPY((caddr_t)tl,
1599 (caddr_t)&nmp->nm_verf[0],
1601 NFSSETWRITEVERF(nmp);
1602 } else if (NFSBCMP(tl, nmp->nm_verf,
1605 NFSBCOPY(tl, nmp->nm_verf, NFSX_VERF);
1609 if (nd->nd_flag & ND_NFSV4)
1610 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1611 if (nd->nd_flag & (ND_NFSV2 | ND_NFSV4)) {
1612 error = nfsm_loadattr(nd, nap);
1614 *attrflagp = NFS_LATTR_NOSHRINK;
1617 error = nd->nd_repstat;
1621 NFSWRITERPC_SETTIME(wccflag, np, (nd->nd_flag & ND_NFSV4));
1622 mbuf_freem(nd->nd_mrep);
1627 if (nd->nd_mrep != NULL)
1628 mbuf_freem(nd->nd_mrep);
1629 *iomode = committed;
1630 if (nd->nd_repstat && !error)
1631 error = nd->nd_repstat;
1637 * For NFS v2 this is a kludge. Use a create rpc but with the IFMT bits of the
1638 * mode set to specify the file type and the size field for rdev.
1641 nfsrpc_mknod(vnode_t dvp, char *name, int namelen, struct vattr *vap,
1642 u_int32_t rdev, enum vtype vtyp, struct ucred *cred, NFSPROC_T *p,
1643 struct nfsvattr *dnap, struct nfsvattr *nnap, struct nfsfh **nfhpp,
1644 int *attrflagp, int *dattrflagp, void *dstuff)
1648 struct nfsrv_descript nfsd, *nd = &nfsd;
1649 nfsattrbit_t attrbits;
1654 if (namelen > NFS_MAXNAMLEN)
1655 return (ENAMETOOLONG);
1656 NFSCL_REQSTART(nd, NFSPROC_MKNOD, dvp);
1657 if (nd->nd_flag & ND_NFSV4) {
1658 if (vtyp == VBLK || vtyp == VCHR) {
1659 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
1660 *tl++ = vtonfsv34_type(vtyp);
1661 *tl++ = txdr_unsigned(NFSMAJOR(rdev));
1662 *tl = txdr_unsigned(NFSMINOR(rdev));
1664 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1665 *tl = vtonfsv34_type(vtyp);
1668 (void) nfsm_strtom(nd, name, namelen);
1669 if (nd->nd_flag & ND_NFSV3) {
1670 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1671 *tl = vtonfsv34_type(vtyp);
1673 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4))
1674 nfscl_fillsattr(nd, vap, dvp, 0, 0);
1675 if ((nd->nd_flag & ND_NFSV3) &&
1676 (vtyp == VCHR || vtyp == VBLK)) {
1677 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1678 *tl++ = txdr_unsigned(NFSMAJOR(rdev));
1679 *tl = txdr_unsigned(NFSMINOR(rdev));
1681 if (nd->nd_flag & ND_NFSV4) {
1682 NFSGETATTR_ATTRBIT(&attrbits);
1683 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1684 *tl++ = txdr_unsigned(NFSV4OP_GETFH);
1685 *tl = txdr_unsigned(NFSV4OP_GETATTR);
1686 (void) nfsrv_putattrbit(nd, &attrbits);
1688 if (nd->nd_flag & ND_NFSV2)
1689 nfscl_fillsattr(nd, vap, dvp, NFSSATTR_SIZERDEV, rdev);
1690 error = nfscl_request(nd, dvp, p, cred, dstuff);
1693 if (nd->nd_flag & ND_NFSV4)
1694 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
1695 if (!nd->nd_repstat) {
1696 if (nd->nd_flag & ND_NFSV4) {
1697 NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
1698 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
1702 error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp);
1706 if (nd->nd_flag & ND_NFSV3)
1707 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
1708 if (!error && nd->nd_repstat)
1709 error = nd->nd_repstat;
1711 mbuf_freem(nd->nd_mrep);
1716 * nfs file create call
1717 * Mostly just call the approriate routine. (I separated out v4, so that
1718 * error recovery wouldn't be as difficult.)
1721 nfsrpc_create(vnode_t dvp, char *name, int namelen, struct vattr *vap,
1722 nfsquad_t cverf, int fmode, struct ucred *cred, NFSPROC_T *p,
1723 struct nfsvattr *dnap, struct nfsvattr *nnap, struct nfsfh **nfhpp,
1724 int *attrflagp, int *dattrflagp, void *dstuff)
1726 int error = 0, newone, expireret = 0, retrycnt, unlocked;
1727 struct nfsclowner *owp;
1728 struct nfscldeleg *dp;
1729 struct nfsmount *nmp = VFSTONFS(vnode_mount(dvp));
1732 if (NFSHASNFSV4(nmp)) {
1736 error = nfscl_open(dvp, NULL, 0, (NFSV4OPEN_ACCESSWRITE |
1737 NFSV4OPEN_ACCESSREAD), 0, cred, p, &owp, NULL, &newone,
1741 if (nmp->nm_clp != NULL)
1742 clidrev = nmp->nm_clp->nfsc_clientidrev;
1745 error = nfsrpc_createv4(dvp, name, namelen, vap, cverf, fmode,
1746 owp, &dp, cred, p, dnap, nnap, nfhpp, attrflagp, dattrflagp,
1749 * There is no need to invalidate cached attributes here,
1750 * since new post-delegation issue attributes are always
1751 * returned by nfsrpc_createv4() and these will update the
1755 (void) nfscl_deleg(nmp->nm_mountp, owp->nfsow_clp,
1756 (*nfhpp)->nfh_fh, (*nfhpp)->nfh_len, cred, p, &dp);
1757 nfscl_ownerrelease(owp, error, newone, unlocked);
1758 if (error == NFSERR_GRACE || error == NFSERR_STALECLIENTID ||
1759 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY) {
1760 (void) nfs_catnap(PZERO, error, "nfs_open");
1761 } else if ((error == NFSERR_EXPIRED ||
1762 error == NFSERR_BADSTATEID) && clidrev != 0) {
1763 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
1766 } while (error == NFSERR_GRACE || error == NFSERR_STALECLIENTID ||
1767 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
1768 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
1769 expireret == 0 && clidrev != 0 && retrycnt < 4));
1770 if (error && retrycnt >= 4)
1773 error = nfsrpc_createv23(dvp, name, namelen, vap, cverf,
1774 fmode, cred, p, dnap, nnap, nfhpp, attrflagp, dattrflagp,
1781 * The create rpc for v2 and 3.
1784 nfsrpc_createv23(vnode_t dvp, char *name, int namelen, struct vattr *vap,
1785 nfsquad_t cverf, int fmode, struct ucred *cred, NFSPROC_T *p,
1786 struct nfsvattr *dnap, struct nfsvattr *nnap, struct nfsfh **nfhpp,
1787 int *attrflagp, int *dattrflagp, void *dstuff)
1791 struct nfsrv_descript nfsd, *nd = &nfsd;
1796 if (namelen > NFS_MAXNAMLEN)
1797 return (ENAMETOOLONG);
1798 NFSCL_REQSTART(nd, NFSPROC_CREATE, dvp);
1799 (void) nfsm_strtom(nd, name, namelen);
1800 if (nd->nd_flag & ND_NFSV3) {
1801 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1802 if (fmode & O_EXCL) {
1803 *tl = txdr_unsigned(NFSCREATE_EXCLUSIVE);
1804 NFSM_BUILD(tl, u_int32_t *, NFSX_VERF);
1805 *tl++ = cverf.lval[0];
1806 *tl = cverf.lval[1];
1808 *tl = txdr_unsigned(NFSCREATE_UNCHECKED);
1809 nfscl_fillsattr(nd, vap, dvp, 0, 0);
1812 nfscl_fillsattr(nd, vap, dvp, NFSSATTR_SIZE0, 0);
1814 error = nfscl_request(nd, dvp, p, cred, dstuff);
1817 if (nd->nd_repstat == 0) {
1818 error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp);
1822 if (nd->nd_flag & ND_NFSV3)
1823 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
1824 if (nd->nd_repstat != 0 && error == 0)
1825 error = nd->nd_repstat;
1827 mbuf_freem(nd->nd_mrep);
1832 nfsrpc_createv4(vnode_t dvp, char *name, int namelen, struct vattr *vap,
1833 nfsquad_t cverf, int fmode, struct nfsclowner *owp, struct nfscldeleg **dpp,
1834 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap,
1835 struct nfsvattr *nnap, struct nfsfh **nfhpp, int *attrflagp,
1836 int *dattrflagp, void *dstuff, int *unlockedp)
1839 int error = 0, deleg, newone, ret, acesize, limitby;
1840 struct nfsrv_descript nfsd, *nd = &nfsd;
1841 struct nfsclopen *op;
1842 struct nfscldeleg *dp = NULL;
1845 nfsattrbit_t attrbits;
1846 nfsv4stateid_t stateid;
1855 if (namelen > NFS_MAXNAMLEN)
1856 return (ENAMETOOLONG);
1857 NFSCL_REQSTART(nd, NFSPROC_CREATE, dvp);
1859 * For V4, this is actually an Open op.
1861 NFSM_BUILD(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
1862 *tl++ = txdr_unsigned(owp->nfsow_seqid);
1863 *tl++ = txdr_unsigned(NFSV4OPEN_ACCESSWRITE |
1864 NFSV4OPEN_ACCESSREAD);
1865 *tl++ = txdr_unsigned(NFSV4OPEN_DENYNONE);
1866 *tl++ = owp->nfsow_clp->nfsc_clientid.lval[0];
1867 *tl = owp->nfsow_clp->nfsc_clientid.lval[1];
1868 (void) nfsm_strtom(nd, owp->nfsow_owner, NFSV4CL_LOCKNAMELEN);
1869 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1870 *tl++ = txdr_unsigned(NFSV4OPEN_CREATE);
1871 if (fmode & O_EXCL) {
1872 *tl = txdr_unsigned(NFSCREATE_EXCLUSIVE);
1873 NFSM_BUILD(tl, u_int32_t *, NFSX_VERF);
1874 *tl++ = cverf.lval[0];
1875 *tl = cverf.lval[1];
1877 *tl = txdr_unsigned(NFSCREATE_UNCHECKED);
1878 nfscl_fillsattr(nd, vap, dvp, 0, 0);
1880 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1881 *tl = txdr_unsigned(NFSV4OPEN_CLAIMNULL);
1882 (void) nfsm_strtom(nd, name, namelen);
1883 /* Get the new file's handle and attributes. */
1884 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1885 *tl++ = txdr_unsigned(NFSV4OP_GETFH);
1886 *tl = txdr_unsigned(NFSV4OP_GETATTR);
1887 NFSGETATTR_ATTRBIT(&attrbits);
1888 (void) nfsrv_putattrbit(nd, &attrbits);
1889 /* Get the directory's post-op attributes. */
1890 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1891 *tl = txdr_unsigned(NFSV4OP_PUTFH);
1892 (void) nfsm_fhtom(nd, np->n_fhp->nfh_fh, np->n_fhp->nfh_len, 0);
1893 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1894 *tl = txdr_unsigned(NFSV4OP_GETATTR);
1895 (void) nfsrv_putattrbit(nd, &attrbits);
1896 error = nfscl_request(nd, dvp, p, cred, dstuff);
1899 NFSCL_INCRSEQID(owp->nfsow_seqid, nd);
1900 if (nd->nd_repstat == 0) {
1901 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
1903 stateid.seqid = *tl++;
1904 stateid.other[0] = *tl++;
1905 stateid.other[1] = *tl++;
1906 stateid.other[2] = *tl;
1907 rflags = fxdr_unsigned(u_int32_t, *(tl + 6));
1908 (void) nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
1909 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
1910 deleg = fxdr_unsigned(int, *tl);
1911 if (deleg == NFSV4OPEN_DELEGATEREAD ||
1912 deleg == NFSV4OPEN_DELEGATEWRITE) {
1913 if (!(owp->nfsow_clp->nfsc_flags &
1914 NFSCLFLAGS_FIRSTDELEG))
1915 owp->nfsow_clp->nfsc_flags |=
1916 (NFSCLFLAGS_FIRSTDELEG | NFSCLFLAGS_GOTDELEG);
1917 MALLOC(dp, struct nfscldeleg *,
1918 sizeof (struct nfscldeleg) + NFSX_V4FHMAX,
1919 M_NFSCLDELEG, M_WAITOK);
1920 LIST_INIT(&dp->nfsdl_owner);
1921 LIST_INIT(&dp->nfsdl_lock);
1922 dp->nfsdl_clp = owp->nfsow_clp;
1923 newnfs_copyincred(cred, &dp->nfsdl_cred);
1924 nfscl_lockinit(&dp->nfsdl_rwlock);
1925 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
1927 dp->nfsdl_stateid.seqid = *tl++;
1928 dp->nfsdl_stateid.other[0] = *tl++;
1929 dp->nfsdl_stateid.other[1] = *tl++;
1930 dp->nfsdl_stateid.other[2] = *tl++;
1931 ret = fxdr_unsigned(int, *tl);
1932 if (deleg == NFSV4OPEN_DELEGATEWRITE) {
1933 dp->nfsdl_flags = NFSCLDL_WRITE;
1935 * Indicates how much the file can grow.
1937 NFSM_DISSECT(tl, u_int32_t *,
1939 limitby = fxdr_unsigned(int, *tl++);
1941 case NFSV4OPEN_LIMITSIZE:
1942 dp->nfsdl_sizelimit = fxdr_hyper(tl);
1944 case NFSV4OPEN_LIMITBLOCKS:
1945 dp->nfsdl_sizelimit =
1946 fxdr_unsigned(u_int64_t, *tl++);
1947 dp->nfsdl_sizelimit *=
1948 fxdr_unsigned(u_int64_t, *tl);
1951 error = NFSERR_BADXDR;
1955 dp->nfsdl_flags = NFSCLDL_READ;
1958 dp->nfsdl_flags |= NFSCLDL_RECALL;
1959 error = nfsrv_dissectace(nd, &dp->nfsdl_ace, &ret,
1963 } else if (deleg != NFSV4OPEN_DELEGATENONE) {
1964 error = NFSERR_BADXDR;
1967 error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp);
1970 /* Get rid of the PutFH and Getattr status values. */
1971 NFSM_DISSECT(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1972 /* Load the directory attributes. */
1973 error = nfsm_loadattr(nd, dnap);
1977 if (dp != NULL && *attrflagp) {
1978 dp->nfsdl_change = nnap->na_filerev;
1979 dp->nfsdl_modtime = nnap->na_mtime;
1980 dp->nfsdl_flags |= NFSCLDL_MODTIMESET;
1983 * We can now complete the Open state.
1987 dp->nfsdl_fhlen = nfhp->nfh_len;
1988 NFSBCOPY(nfhp->nfh_fh, dp->nfsdl_fh, nfhp->nfh_len);
1991 * Get an Open structure that will be
1992 * attached to the OpenOwner, acquired already.
1994 error = nfscl_open(dvp, nfhp->nfh_fh, nfhp->nfh_len,
1995 (NFSV4OPEN_ACCESSWRITE | NFSV4OPEN_ACCESSREAD), 0,
1996 cred, p, NULL, &op, &newone, NULL, 0);
1999 op->nfso_stateid = stateid;
2000 newnfs_copyincred(cred, &op->nfso_cred);
2001 if ((rflags & NFSV4OPEN_RESULTCONFIRM)) {
2003 ret = nfsrpc_openconfirm(dvp, nfhp->nfh_fh,
2004 nfhp->nfh_len, op, cred, p);
2005 if (ret == NFSERR_DELAY)
2006 (void) nfs_catnap(PZERO, ret, "nfs_create");
2007 } while (ret == NFSERR_DELAY);
2012 * If the server is handing out delegations, but we didn't
2013 * get one because an OpenConfirm was required, try the
2014 * Open again, to get a delegation. This is a harmless no-op,
2015 * from a server's point of view.
2017 if ((rflags & NFSV4OPEN_RESULTCONFIRM) &&
2018 (owp->nfsow_clp->nfsc_flags & NFSCLFLAGS_GOTDELEG) &&
2019 !error && dp == NULL) {
2021 ret = nfsrpc_openrpc(VFSTONFS(vnode_mount(dvp)), dvp,
2022 np->n_fhp->nfh_fh, np->n_fhp->nfh_len,
2023 nfhp->nfh_fh, nfhp->nfh_len,
2024 (NFSV4OPEN_ACCESSWRITE | NFSV4OPEN_ACCESSREAD), op,
2025 name, namelen, &dp, 0, 0x0, cred, p, 0, 1);
2026 if (ret == NFSERR_DELAY)
2027 (void) nfs_catnap(PZERO, ret, "nfs_crt2");
2028 } while (ret == NFSERR_DELAY);
2031 FREE((caddr_t)dp, M_NFSCLDELEG);
2032 if (ret == NFSERR_STALECLIENTID ||
2033 ret == NFSERR_STALEDONTRECOVER)
2037 nfscl_openrelease(op, error, newone);
2040 if (nd->nd_repstat != 0 && error == 0)
2041 error = nd->nd_repstat;
2042 if (error == NFSERR_STALECLIENTID)
2043 nfscl_initiate_recovery(owp->nfsow_clp);
2047 else if (dp != NULL)
2048 FREE((caddr_t)dp, M_NFSCLDELEG);
2049 mbuf_freem(nd->nd_mrep);
2057 nfsrpc_remove(vnode_t dvp, char *name, int namelen, vnode_t vp,
2058 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap, int *dattrflagp,
2062 struct nfsrv_descript nfsd, *nd = &nfsd;
2064 struct nfsmount *nmp;
2065 nfsv4stateid_t dstateid;
2066 int error, ret = 0, i;
2069 if (namelen > NFS_MAXNAMLEN)
2070 return (ENAMETOOLONG);
2071 nmp = VFSTONFS(vnode_mount(dvp));
2073 if (NFSHASNFSV4(nmp) && ret == 0) {
2074 ret = nfscl_removedeleg(vp, p, &dstateid);
2076 NFSCL_REQSTART(nd, NFSPROC_RETDELEGREMOVE, vp);
2077 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID +
2079 *tl++ = dstateid.seqid;
2080 *tl++ = dstateid.other[0];
2081 *tl++ = dstateid.other[1];
2082 *tl++ = dstateid.other[2];
2083 *tl = txdr_unsigned(NFSV4OP_PUTFH);
2085 (void) nfsm_fhtom(nd, np->n_fhp->nfh_fh,
2086 np->n_fhp->nfh_len, 0);
2087 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2088 *tl = txdr_unsigned(NFSV4OP_REMOVE);
2094 NFSCL_REQSTART(nd, NFSPROC_REMOVE, dvp);
2095 (void) nfsm_strtom(nd, name, namelen);
2096 error = nfscl_request(nd, dvp, p, cred, dstuff);
2099 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) {
2100 /* For NFSv4, parse out any Delereturn replies. */
2101 if (ret > 0 && nd->nd_repstat != 0 &&
2102 (nd->nd_flag & ND_NOMOREDATA)) {
2104 * If the Delegreturn failed, try again without
2105 * it. The server will Recall, as required.
2107 mbuf_freem(nd->nd_mrep);
2110 for (i = 0; i < (ret * 2); i++) {
2111 if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) ==
2113 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2115 nd->nd_flag |= ND_NOMOREDATA;
2118 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
2120 if (nd->nd_repstat && !error)
2121 error = nd->nd_repstat;
2123 mbuf_freem(nd->nd_mrep);
2128 * Do an nfs rename rpc.
2131 nfsrpc_rename(vnode_t fdvp, vnode_t fvp, char *fnameptr, int fnamelen,
2132 vnode_t tdvp, vnode_t tvp, char *tnameptr, int tnamelen, struct ucred *cred,
2133 NFSPROC_T *p, struct nfsvattr *fnap, struct nfsvattr *tnap,
2134 int *fattrflagp, int *tattrflagp, void *fstuff, void *tstuff)
2137 struct nfsrv_descript nfsd, *nd = &nfsd;
2138 struct nfsmount *nmp;
2140 nfsattrbit_t attrbits;
2141 nfsv4stateid_t fdstateid, tdstateid;
2142 int error = 0, ret = 0, gottd = 0, gotfd = 0, i;
2146 nmp = VFSTONFS(vnode_mount(fdvp));
2147 if (fnamelen > NFS_MAXNAMLEN || tnamelen > NFS_MAXNAMLEN)
2148 return (ENAMETOOLONG);
2150 if (NFSHASNFSV4(nmp) && ret == 0) {
2151 ret = nfscl_renamedeleg(fvp, &fdstateid, &gotfd, tvp,
2152 &tdstateid, &gottd, p);
2153 if (gotfd && gottd) {
2154 NFSCL_REQSTART(nd, NFSPROC_RETDELEGRENAME2, fvp);
2156 NFSCL_REQSTART(nd, NFSPROC_RETDELEGRENAME1, fvp);
2158 NFSCL_REQSTART(nd, NFSPROC_RETDELEGRENAME1, tvp);
2161 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID);
2162 *tl++ = fdstateid.seqid;
2163 *tl++ = fdstateid.other[0];
2164 *tl++ = fdstateid.other[1];
2165 *tl = fdstateid.other[2];
2167 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2168 *tl = txdr_unsigned(NFSV4OP_PUTFH);
2170 (void) nfsm_fhtom(nd, np->n_fhp->nfh_fh,
2171 np->n_fhp->nfh_len, 0);
2172 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2173 *tl = txdr_unsigned(NFSV4OP_DELEGRETURN);
2177 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID);
2178 *tl++ = tdstateid.seqid;
2179 *tl++ = tdstateid.other[0];
2180 *tl++ = tdstateid.other[1];
2181 *tl = tdstateid.other[2];
2184 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2185 *tl = txdr_unsigned(NFSV4OP_PUTFH);
2187 (void) nfsm_fhtom(nd, np->n_fhp->nfh_fh,
2188 np->n_fhp->nfh_len, 0);
2189 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2190 *tl = txdr_unsigned(NFSV4OP_SAVEFH);
2196 NFSCL_REQSTART(nd, NFSPROC_RENAME, fdvp);
2197 if (nd->nd_flag & ND_NFSV4) {
2198 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2199 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2200 NFSWCCATTR_ATTRBIT(&attrbits);
2201 (void) nfsrv_putattrbit(nd, &attrbits);
2202 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2203 *tl = txdr_unsigned(NFSV4OP_PUTFH);
2204 (void) nfsm_fhtom(nd, VTONFS(tdvp)->n_fhp->nfh_fh,
2205 VTONFS(tdvp)->n_fhp->nfh_len, 0);
2206 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2207 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2208 (void) nfsrv_putattrbit(nd, &attrbits);
2209 nd->nd_flag |= ND_V4WCCATTR;
2210 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2211 *tl = txdr_unsigned(NFSV4OP_RENAME);
2213 (void) nfsm_strtom(nd, fnameptr, fnamelen);
2214 if (!(nd->nd_flag & ND_NFSV4))
2215 (void) nfsm_fhtom(nd, VTONFS(tdvp)->n_fhp->nfh_fh,
2216 VTONFS(tdvp)->n_fhp->nfh_len, 0);
2217 (void) nfsm_strtom(nd, tnameptr, tnamelen);
2218 error = nfscl_request(nd, fdvp, p, cred, fstuff);
2221 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) {
2222 /* For NFSv4, parse out any Delereturn replies. */
2223 if (ret > 0 && nd->nd_repstat != 0 &&
2224 (nd->nd_flag & ND_NOMOREDATA)) {
2226 * If the Delegreturn failed, try again without
2227 * it. The server will Recall, as required.
2229 mbuf_freem(nd->nd_mrep);
2232 for (i = 0; i < (ret * 2); i++) {
2233 if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) ==
2235 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2237 if (i == 0 && ret > 1) {
2239 * If the Delegreturn failed, try again
2240 * without it. The server will Recall, as
2242 * If ret > 1, the first iteration of this
2243 * loop is the second DelegReturn result.
2245 mbuf_freem(nd->nd_mrep);
2248 nd->nd_flag |= ND_NOMOREDATA;
2253 /* Now, the first wcc attribute reply. */
2254 if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) == ND_NFSV4) {
2255 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2257 nd->nd_flag |= ND_NOMOREDATA;
2259 error = nfscl_wcc_data(nd, fdvp, fnap, fattrflagp, NULL,
2261 /* and the second wcc attribute reply. */
2262 if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) == ND_NFSV4 &&
2264 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2266 nd->nd_flag |= ND_NOMOREDATA;
2269 error = nfscl_wcc_data(nd, tdvp, tnap, tattrflagp,
2272 if (nd->nd_repstat && !error)
2273 error = nd->nd_repstat;
2275 mbuf_freem(nd->nd_mrep);
2280 * nfs hard link create rpc
2283 nfsrpc_link(vnode_t dvp, vnode_t vp, char *name, int namelen,
2284 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap,
2285 struct nfsvattr *nap, int *attrflagp, int *dattrflagp, void *dstuff)
2288 struct nfsrv_descript nfsd, *nd = &nfsd;
2289 nfsattrbit_t attrbits;
2294 if (namelen > NFS_MAXNAMLEN)
2295 return (ENAMETOOLONG);
2296 NFSCL_REQSTART(nd, NFSPROC_LINK, vp);
2297 if (nd->nd_flag & ND_NFSV4) {
2298 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2299 *tl = txdr_unsigned(NFSV4OP_PUTFH);
2301 (void) nfsm_fhtom(nd, VTONFS(dvp)->n_fhp->nfh_fh,
2302 VTONFS(dvp)->n_fhp->nfh_len, 0);
2303 if (nd->nd_flag & ND_NFSV4) {
2304 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2305 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2306 NFSWCCATTR_ATTRBIT(&attrbits);
2307 (void) nfsrv_putattrbit(nd, &attrbits);
2308 nd->nd_flag |= ND_V4WCCATTR;
2309 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2310 *tl = txdr_unsigned(NFSV4OP_LINK);
2312 (void) nfsm_strtom(nd, name, namelen);
2313 error = nfscl_request(nd, vp, p, cred, dstuff);
2316 if (nd->nd_flag & ND_NFSV3) {
2317 error = nfscl_postop_attr(nd, nap, attrflagp, dstuff);
2319 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp,
2321 } else if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) == ND_NFSV4) {
2323 * First, parse out the PutFH and Getattr result.
2325 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2327 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2329 nd->nd_flag |= ND_NOMOREDATA;
2331 * Get the pre-op attributes.
2333 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
2335 if (nd->nd_repstat && !error)
2336 error = nd->nd_repstat;
2338 mbuf_freem(nd->nd_mrep);
2343 * nfs symbolic link create rpc
2346 nfsrpc_symlink(vnode_t dvp, char *name, int namelen, char *target,
2347 struct vattr *vap, struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap,
2348 struct nfsvattr *nnap, struct nfsfh **nfhpp, int *attrflagp,
2349 int *dattrflagp, void *dstuff)
2352 struct nfsrv_descript nfsd, *nd = &nfsd;
2353 struct nfsmount *nmp;
2354 int slen, error = 0;
2359 nmp = VFSTONFS(vnode_mount(dvp));
2360 slen = strlen(target);
2361 if (slen > NFS_MAXPATHLEN || namelen > NFS_MAXNAMLEN)
2362 return (ENAMETOOLONG);
2363 NFSCL_REQSTART(nd, NFSPROC_SYMLINK, dvp);
2364 if (nd->nd_flag & ND_NFSV4) {
2365 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2366 *tl = txdr_unsigned(NFLNK);
2367 (void) nfsm_strtom(nd, target, slen);
2369 (void) nfsm_strtom(nd, name, namelen);
2370 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4))
2371 nfscl_fillsattr(nd, vap, dvp, 0, 0);
2372 if (!(nd->nd_flag & ND_NFSV4))
2373 (void) nfsm_strtom(nd, target, slen);
2374 if (nd->nd_flag & ND_NFSV2)
2375 nfscl_fillsattr(nd, vap, dvp, NFSSATTR_SIZENEG1, 0);
2376 error = nfscl_request(nd, dvp, p, cred, dstuff);
2379 if (nd->nd_flag & ND_NFSV4)
2380 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
2381 if ((nd->nd_flag & ND_NFSV3) && !error) {
2382 if (!nd->nd_repstat)
2383 error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp);
2385 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp,
2388 if (nd->nd_repstat && !error)
2389 error = nd->nd_repstat;
2390 mbuf_freem(nd->nd_mrep);
2392 * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry.
2394 if (error == EEXIST)
2403 nfsrpc_mkdir(vnode_t dvp, char *name, int namelen, struct vattr *vap,
2404 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap,
2405 struct nfsvattr *nnap, struct nfsfh **nfhpp, int *attrflagp,
2406 int *dattrflagp, void *dstuff)
2409 struct nfsrv_descript nfsd, *nd = &nfsd;
2410 nfsattrbit_t attrbits;
2417 fhp = VTONFS(dvp)->n_fhp;
2418 if (namelen > NFS_MAXNAMLEN)
2419 return (ENAMETOOLONG);
2420 NFSCL_REQSTART(nd, NFSPROC_MKDIR, dvp);
2421 if (nd->nd_flag & ND_NFSV4) {
2422 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2423 *tl = txdr_unsigned(NFDIR);
2425 (void) nfsm_strtom(nd, name, namelen);
2426 nfscl_fillsattr(nd, vap, dvp, NFSSATTR_SIZENEG1, 0);
2427 if (nd->nd_flag & ND_NFSV4) {
2428 NFSGETATTR_ATTRBIT(&attrbits);
2429 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2430 *tl++ = txdr_unsigned(NFSV4OP_GETFH);
2431 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2432 (void) nfsrv_putattrbit(nd, &attrbits);
2433 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2434 *tl = txdr_unsigned(NFSV4OP_PUTFH);
2435 (void) nfsm_fhtom(nd, fhp->nfh_fh, fhp->nfh_len, 0);
2436 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2437 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2438 (void) nfsrv_putattrbit(nd, &attrbits);
2440 error = nfscl_request(nd, dvp, p, cred, dstuff);
2443 if (nd->nd_flag & ND_NFSV4)
2444 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
2445 if (!nd->nd_repstat && !error) {
2446 if (nd->nd_flag & ND_NFSV4) {
2447 NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
2448 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
2451 error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp);
2452 if (error == 0 && (nd->nd_flag & ND_NFSV4) != 0) {
2453 /* Get rid of the PutFH and Getattr status values. */
2454 NFSM_DISSECT(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
2455 /* Load the directory attributes. */
2456 error = nfsm_loadattr(nd, dnap);
2461 if ((nd->nd_flag & ND_NFSV3) && !error)
2462 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
2463 if (nd->nd_repstat && !error)
2464 error = nd->nd_repstat;
2466 mbuf_freem(nd->nd_mrep);
2468 * Kludge: Map EEXIST => 0 assuming that you have a reply to a retry.
2470 if (error == EEXIST)
2476 * nfs remove directory call
2479 nfsrpc_rmdir(vnode_t dvp, char *name, int namelen, struct ucred *cred,
2480 NFSPROC_T *p, struct nfsvattr *dnap, int *dattrflagp, void *dstuff)
2482 struct nfsrv_descript nfsd, *nd = &nfsd;
2486 if (namelen > NFS_MAXNAMLEN)
2487 return (ENAMETOOLONG);
2488 NFSCL_REQSTART(nd, NFSPROC_RMDIR, dvp);
2489 (void) nfsm_strtom(nd, name, namelen);
2490 error = nfscl_request(nd, dvp, p, cred, dstuff);
2493 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4))
2494 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
2495 if (nd->nd_repstat && !error)
2496 error = nd->nd_repstat;
2497 mbuf_freem(nd->nd_mrep);
2499 * Kludge: Map ENOENT => 0 assuming that you have a reply to a retry.
2501 if (error == ENOENT)
2508 * Always returns with either uio_resid unchanged, if you are at the
2509 * end of the directory, or uio_resid == 0, with all DIRBLKSIZ chunks
2511 * I felt this would allow caching of directory blocks more easily
2512 * than returning a pertially filled block.
2513 * Directory offset cookies:
2514 * Oh my, what to do with them...
2515 * I can think of three ways to deal with them:
2516 * 1 - have the layer above these RPCs maintain a map between logical
2517 * directory byte offsets and the NFS directory offset cookies
2518 * 2 - pass the opaque directory offset cookies up into userland
2519 * and let the libc functions deal with them, via the system call
2520 * 3 - return them to userland in the "struct dirent", so future versions
2521 * of libc can use them and do whatever is necessary to amke things work
2522 * above these rpc calls, in the meantime
2523 * For now, I do #3 by "hiding" the directory offset cookies after the
2524 * d_name field in struct dirent. This is space inside d_reclen that
2525 * will be ignored by anything that doesn't know about them.
2526 * The directory offset cookies are filled in as the last 8 bytes of
2527 * each directory entry, after d_name. Someday, the userland libc
2528 * functions may be able to use these. In the meantime, it satisfies
2529 * OpenBSD's requirements for cookies being returned.
2530 * If expects the directory offset cookie for the read to be in uio_offset
2531 * and returns the one for the next entry after this directory block in
2535 nfsrpc_readdir(vnode_t vp, struct uio *uiop, nfsuint64 *cookiep,
2536 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp,
2537 int *eofp, void *stuff)
2540 struct dirent *dp = NULL;
2542 nfsquad_t cookie, ncookie;
2543 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
2544 struct nfsnode *dnp = VTONFS(vp);
2545 struct nfsvattr nfsva;
2546 struct nfsrv_descript nfsd, *nd = &nfsd;
2547 int error = 0, tlen, more_dirs = 1, blksiz = 0, bigenough = 1;
2548 int reqsize, tryformoredirs = 1, readsize, eof = 0, gotmnton = 0;
2549 long dotfileid, dotdotfileid = 0;
2550 u_int32_t fakefileno = 0xffffffff, rderr;
2552 nfsattrbit_t attrbits, dattrbits;
2553 u_int32_t *tl2 = NULL;
2556 KASSERT(uiop->uio_iovcnt == 1 &&
2557 (uio_uio_resid(uiop) & (DIRBLKSIZ - 1)) == 0,
2558 ("nfs readdirrpc bad uio"));
2561 * There is no point in reading a lot more than uio_resid, however
2562 * adding one additional DIRBLKSIZ makes sense. Since uio_resid
2563 * and nm_readdirsize are both exact multiples of DIRBLKSIZ, this
2564 * will never make readsize > nm_readdirsize.
2566 readsize = nmp->nm_readdirsize;
2567 if (readsize > uio_uio_resid(uiop))
2568 readsize = uio_uio_resid(uiop) + DIRBLKSIZ;
2573 tresid = uio_uio_resid(uiop);
2574 cookie.lval[0] = cookiep->nfsuquad[0];
2575 cookie.lval[1] = cookiep->nfsuquad[1];
2579 * For NFSv4, first create the "." and ".." entries.
2581 if (NFSHASNFSV4(nmp)) {
2582 reqsize = 6 * NFSX_UNSIGNED;
2583 NFSGETATTR_ATTRBIT(&dattrbits);
2584 NFSZERO_ATTRBIT(&attrbits);
2585 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_FILEID);
2586 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TYPE);
2587 if (NFSISSET_ATTRBIT(&dnp->n_vattr.na_suppattr,
2588 NFSATTRBIT_MOUNTEDONFILEID)) {
2589 NFSSETBIT_ATTRBIT(&attrbits,
2590 NFSATTRBIT_MOUNTEDONFILEID);
2594 * Must fake it. Use the fileno, except when the
2595 * fsid is != to that of the directory. For that
2596 * case, generate a fake fileno that is not the same.
2598 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_FSID);
2603 * Joy, oh joy. For V4 we get to hand craft '.' and '..'.
2605 if (uiop->uio_offset == 0) {
2606 NFSCL_REQSTART(nd, NFSPROC_LOOKUPP, vp);
2607 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2608 *tl++ = txdr_unsigned(NFSV4OP_GETFH);
2609 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2610 (void) nfsrv_putattrbit(nd, &attrbits);
2611 error = nfscl_request(nd, vp, p, cred, stuff);
2614 dotfileid = 0; /* Fake out the compiler. */
2615 if ((nd->nd_flag & ND_NOMOREDATA) == 0) {
2616 error = nfsm_loadattr(nd, &nfsva);
2619 dotfileid = nfsva.na_fileid;
2621 if (nd->nd_repstat == 0) {
2622 NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
2623 len = fxdr_unsigned(int, *(tl + 4));
2624 if (len > 0 && len <= NFSX_V4FHMAX)
2625 error = nfsm_advance(nd, NFSM_RNDUP(len), -1);
2629 NFSM_DISSECT(tl, u_int32_t *, 2*NFSX_UNSIGNED);
2630 nfsva.na_mntonfileno = 0xffffffff;
2631 error = nfsv4_loadattr(nd, NULL, &nfsva, NULL,
2632 NULL, 0, NULL, NULL, NULL, NULL, NULL, 0,
2633 NULL, NULL, NULL, p, cred);
2635 dotdotfileid = dotfileid;
2636 } else if (gotmnton) {
2637 if (nfsva.na_mntonfileno != 0xffffffff)
2638 dotdotfileid = nfsva.na_mntonfileno;
2640 dotdotfileid = nfsva.na_fileid;
2641 } else if (nfsva.na_filesid[0] ==
2642 dnp->n_vattr.na_filesid[0] &&
2643 nfsva.na_filesid[1] ==
2644 dnp->n_vattr.na_filesid[1]) {
2645 dotdotfileid = nfsva.na_fileid;
2649 } while (fakefileno ==
2651 dotdotfileid = fakefileno;
2654 } else if (nd->nd_repstat == NFSERR_NOENT) {
2656 * Lookupp returns NFSERR_NOENT when we are
2657 * at the root, so just use the current dir.
2660 dotdotfileid = dotfileid;
2662 error = nd->nd_repstat;
2664 mbuf_freem(nd->nd_mrep);
2668 dp = (struct dirent *) CAST_DOWN(caddr_t, uio_iov_base(uiop));
2669 dp->d_type = DT_DIR;
2670 dp->d_fileno = dotfileid;
2672 dp->d_name[0] = '.';
2673 dp->d_name[1] = '\0';
2674 dp->d_reclen = DIRENT_SIZE(dp) + NFSX_HYPER;
2676 * Just make these offset cookie 0.
2678 tl = (u_int32_t *)&dp->d_name[4];
2681 blksiz += dp->d_reclen;
2682 uio_uio_resid_add(uiop, -(dp->d_reclen));
2683 uiop->uio_offset += dp->d_reclen;
2684 uio_iov_base_add(uiop, dp->d_reclen);
2685 uio_iov_len_add(uiop, -(dp->d_reclen));
2686 dp = (struct dirent *) CAST_DOWN(caddr_t, uio_iov_base(uiop));
2687 dp->d_type = DT_DIR;
2688 dp->d_fileno = dotdotfileid;
2690 dp->d_name[0] = '.';
2691 dp->d_name[1] = '.';
2692 dp->d_name[2] = '\0';
2693 dp->d_reclen = DIRENT_SIZE(dp) + NFSX_HYPER;
2695 * Just make these offset cookie 0.
2697 tl = (u_int32_t *)&dp->d_name[4];
2700 blksiz += dp->d_reclen;
2701 uio_uio_resid_add(uiop, -(dp->d_reclen));
2702 uiop->uio_offset += dp->d_reclen;
2703 uio_iov_base_add(uiop, dp->d_reclen);
2704 uio_iov_len_add(uiop, -(dp->d_reclen));
2706 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_RDATTRERROR);
2708 reqsize = 5 * NFSX_UNSIGNED;
2713 * Loop around doing readdir rpc's of size readsize.
2714 * The stopping criteria is EOF or buffer full.
2716 while (more_dirs && bigenough) {
2718 NFSCL_REQSTART(nd, NFSPROC_READDIR, vp);
2719 if (nd->nd_flag & ND_NFSV2) {
2720 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2721 *tl++ = cookie.lval[1];
2722 *tl = txdr_unsigned(readsize);
2724 NFSM_BUILD(tl, u_int32_t *, reqsize);
2725 *tl++ = cookie.lval[0];
2726 *tl++ = cookie.lval[1];
2727 if (cookie.qval == 0) {
2732 *tl++ = dnp->n_cookieverf.nfsuquad[0];
2733 *tl++ = dnp->n_cookieverf.nfsuquad[1];
2736 if (nd->nd_flag & ND_NFSV4) {
2737 *tl++ = txdr_unsigned(readsize);
2738 *tl = txdr_unsigned(readsize);
2739 (void) nfsrv_putattrbit(nd, &attrbits);
2740 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2741 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2742 (void) nfsrv_putattrbit(nd, &dattrbits);
2744 *tl = txdr_unsigned(readsize);
2747 error = nfscl_request(nd, vp, p, cred, stuff);
2750 if (!(nd->nd_flag & ND_NFSV2)) {
2751 if (nd->nd_flag & ND_NFSV3)
2752 error = nfscl_postop_attr(nd, nap, attrflagp,
2754 if (!nd->nd_repstat && !error) {
2755 NFSM_DISSECT(tl, u_int32_t *, NFSX_HYPER);
2757 dnp->n_cookieverf.nfsuquad[0] = *tl++;
2758 dnp->n_cookieverf.nfsuquad[1] = *tl;
2762 if (nd->nd_repstat || error) {
2764 error = nd->nd_repstat;
2767 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2768 more_dirs = fxdr_unsigned(int, *tl);
2772 /* loop thru the dir entries, doctoring them to 4bsd form */
2773 while (more_dirs && bigenough) {
2774 if (nd->nd_flag & ND_NFSV4) {
2775 NFSM_DISSECT(tl, u_int32_t *, 3*NFSX_UNSIGNED);
2776 ncookie.lval[0] = *tl++;
2777 ncookie.lval[1] = *tl++;
2778 len = fxdr_unsigned(int, *tl);
2779 } else if (nd->nd_flag & ND_NFSV3) {
2780 NFSM_DISSECT(tl, u_int32_t *, 3*NFSX_UNSIGNED);
2781 nfsva.na_fileid = fxdr_hyper(tl);
2783 len = fxdr_unsigned(int, *tl);
2785 NFSM_DISSECT(tl, u_int32_t *, 2*NFSX_UNSIGNED);
2787 fxdr_unsigned(long, *tl++);
2788 len = fxdr_unsigned(int, *tl);
2790 if (len <= 0 || len > NFS_MAXNAMLEN) {
2794 tlen = NFSM_RNDUP(len);
2796 tlen += 4; /* To ensure null termination */
2797 left = DIRBLKSIZ - blksiz;
2798 if ((int)(tlen + DIRHDSIZ + NFSX_HYPER) > left) {
2799 dp->d_reclen += left;
2800 uio_iov_base_add(uiop, left);
2801 uio_iov_len_add(uiop, -(left));
2802 uio_uio_resid_add(uiop, -(left));
2803 uiop->uio_offset += left;
2806 if ((int)(tlen + DIRHDSIZ + NFSX_HYPER) > uio_uio_resid(uiop))
2809 dp = (struct dirent *) CAST_DOWN(caddr_t, uio_iov_base(uiop));
2811 dp->d_reclen = tlen + DIRHDSIZ + NFSX_HYPER;
2812 dp->d_type = DT_UNKNOWN;
2813 blksiz += dp->d_reclen;
2814 if (blksiz == DIRBLKSIZ)
2816 uio_uio_resid_add(uiop, -(DIRHDSIZ));
2817 uiop->uio_offset += DIRHDSIZ;
2818 uio_iov_base_add(uiop, DIRHDSIZ);
2819 uio_iov_len_add(uiop, -(DIRHDSIZ));
2820 error = nfsm_mbufuio(nd, uiop, len);
2823 cp = CAST_DOWN(caddr_t, uio_iov_base(uiop));
2825 *cp = '\0'; /* null terminate */
2826 cp += tlen; /* points to cookie storage */
2827 tl2 = (u_int32_t *)cp;
2828 uio_iov_base_add(uiop, (tlen + NFSX_HYPER));
2829 uio_iov_len_add(uiop, -(tlen + NFSX_HYPER));
2830 uio_uio_resid_add(uiop, -(tlen + NFSX_HYPER));
2831 uiop->uio_offset += (tlen + NFSX_HYPER);
2833 error = nfsm_advance(nd, NFSM_RNDUP(len), -1);
2837 if (nd->nd_flag & ND_NFSV4) {
2839 nfsva.na_mntonfileno = 0xffffffff;
2840 error = nfsv4_loadattr(nd, NULL, &nfsva, NULL,
2841 NULL, 0, NULL, NULL, NULL, NULL, NULL, 0,
2842 NULL, NULL, &rderr, p, cred);
2845 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2846 } else if (nd->nd_flag & ND_NFSV3) {
2847 NFSM_DISSECT(tl, u_int32_t *, 3*NFSX_UNSIGNED);
2848 ncookie.lval[0] = *tl++;
2849 ncookie.lval[1] = *tl++;
2851 NFSM_DISSECT(tl, u_int32_t *, 2*NFSX_UNSIGNED);
2852 ncookie.lval[0] = 0;
2853 ncookie.lval[1] = *tl++;
2856 if (nd->nd_flag & ND_NFSV4) {
2861 if (nfsva.na_mntonfileno != 0xffffffff)
2862 dp->d_fileno = nfsva.na_mntonfileno;
2864 dp->d_fileno = nfsva.na_fileid;
2865 } else if (nfsva.na_filesid[0] ==
2866 dnp->n_vattr.na_filesid[0] &&
2867 nfsva.na_filesid[1] ==
2868 dnp->n_vattr.na_filesid[1]) {
2869 dp->d_fileno = nfsva.na_fileid;
2873 } while (fakefileno ==
2875 dp->d_fileno = fakefileno;
2877 dp->d_type = vtonfs_dtype(nfsva.na_type);
2880 dp->d_fileno = nfsva.na_fileid;
2882 *tl2++ = cookiep->nfsuquad[0] = cookie.lval[0] =
2884 *tl2 = cookiep->nfsuquad[1] = cookie.lval[1] =
2887 more_dirs = fxdr_unsigned(int, *tl);
2890 * If at end of rpc data, get the eof boolean
2893 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2894 eof = fxdr_unsigned(int, *tl);
2897 if (nd->nd_flag & ND_NFSV4) {
2898 error = nfscl_postop_attr(nd, nap, attrflagp,
2904 mbuf_freem(nd->nd_mrep);
2908 * Fill last record, iff any, out to a multiple of DIRBLKSIZ
2909 * by increasing d_reclen for the last record.
2912 left = DIRBLKSIZ - blksiz;
2913 dp->d_reclen += left;
2914 uio_iov_base_add(uiop, left);
2915 uio_iov_len_add(uiop, -(left));
2916 uio_uio_resid_add(uiop, -(left));
2917 uiop->uio_offset += left;
2921 * If returning no data, assume end of file.
2922 * If not bigenough, return not end of file, since you aren't
2923 * returning all the data
2924 * Otherwise, return the eof flag from the server.
2927 if (tresid == ((size_t)(uio_uio_resid(uiop))))
2929 else if (!bigenough)
2936 * Add extra empty records to any remaining DIRBLKSIZ chunks.
2938 while (uio_uio_resid(uiop) > 0 && ((size_t)(uio_uio_resid(uiop))) != tresid) {
2939 dp = (struct dirent *) CAST_DOWN(caddr_t, uio_iov_base(uiop));
2940 dp->d_type = DT_UNKNOWN;
2943 dp->d_name[0] = '\0';
2944 tl = (u_int32_t *)&dp->d_name[4];
2945 *tl++ = cookie.lval[0];
2946 *tl = cookie.lval[1];
2947 dp->d_reclen = DIRBLKSIZ;
2948 uio_iov_base_add(uiop, DIRBLKSIZ);
2949 uio_iov_len_add(uiop, -(DIRBLKSIZ));
2950 uio_uio_resid_add(uiop, -(DIRBLKSIZ));
2951 uiop->uio_offset += DIRBLKSIZ;
2955 if (nd->nd_mrep != NULL)
2956 mbuf_freem(nd->nd_mrep);
2962 * NFS V3 readdir plus RPC. Used in place of nfsrpc_readdir().
2963 * (Also used for NFS V4 when mount flag set.)
2964 * (ditto above w.r.t. multiple of DIRBLKSIZ, etc.)
2967 nfsrpc_readdirplus(vnode_t vp, struct uio *uiop, nfsuint64 *cookiep,
2968 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp,
2969 int *eofp, void *stuff)
2972 struct dirent *dp = NULL;
2974 vnode_t newvp = NULLVP;
2975 struct nfsrv_descript nfsd, *nd = &nfsd;
2976 struct nameidata nami, *ndp = &nami;
2977 struct componentname *cnp = &ndp->ni_cnd;
2978 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
2979 struct nfsnode *dnp = VTONFS(vp), *np;
2980 struct nfsvattr nfsva;
2982 nfsquad_t cookie, ncookie;
2983 int error = 0, tlen, more_dirs = 1, blksiz = 0, bigenough = 1;
2984 int attrflag, tryformoredirs = 1, eof = 0, gotmnton = 0;
2985 int isdotdot = 0, unlocknewvp = 0;
2986 long dotfileid, dotdotfileid = 0, fileno = 0;
2988 nfsattrbit_t attrbits, dattrbits;
2990 u_int32_t *tl2 = NULL, fakefileno = 0xffffffff, rderr;
2991 struct timespec dctime;
2993 KASSERT(uiop->uio_iovcnt == 1 &&
2994 (uio_uio_resid(uiop) & (DIRBLKSIZ - 1)) == 0,
2995 ("nfs readdirplusrpc bad uio"));
2996 timespecclear(&dctime);
3002 cookie.lval[0] = cookiep->nfsuquad[0];
3003 cookie.lval[1] = cookiep->nfsuquad[1];
3004 tresid = uio_uio_resid(uiop);
3007 * For NFSv4, first create the "." and ".." entries.
3009 if (NFSHASNFSV4(nmp)) {
3010 NFSGETATTR_ATTRBIT(&dattrbits);
3011 NFSZERO_ATTRBIT(&attrbits);
3012 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_FILEID);
3013 if (NFSISSET_ATTRBIT(&dnp->n_vattr.na_suppattr,
3014 NFSATTRBIT_MOUNTEDONFILEID)) {
3015 NFSSETBIT_ATTRBIT(&attrbits,
3016 NFSATTRBIT_MOUNTEDONFILEID);
3020 * Must fake it. Use the fileno, except when the
3021 * fsid is != to that of the directory. For that
3022 * case, generate a fake fileno that is not the same.
3024 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_FSID);
3029 * Joy, oh joy. For V4 we get to hand craft '.' and '..'.
3031 if (uiop->uio_offset == 0) {
3032 NFSCL_REQSTART(nd, NFSPROC_LOOKUPP, vp);
3033 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
3034 *tl++ = txdr_unsigned(NFSV4OP_GETFH);
3035 *tl = txdr_unsigned(NFSV4OP_GETATTR);
3036 (void) nfsrv_putattrbit(nd, &attrbits);
3037 error = nfscl_request(nd, vp, p, cred, stuff);
3040 dotfileid = 0; /* Fake out the compiler. */
3041 if ((nd->nd_flag & ND_NOMOREDATA) == 0) {
3042 error = nfsm_loadattr(nd, &nfsva);
3045 dctime = nfsva.na_ctime;
3046 dotfileid = nfsva.na_fileid;
3048 if (nd->nd_repstat == 0) {
3049 NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
3050 len = fxdr_unsigned(int, *(tl + 4));
3051 if (len > 0 && len <= NFSX_V4FHMAX)
3052 error = nfsm_advance(nd, NFSM_RNDUP(len), -1);
3056 NFSM_DISSECT(tl, u_int32_t *, 2*NFSX_UNSIGNED);
3057 nfsva.na_mntonfileno = 0xffffffff;
3058 error = nfsv4_loadattr(nd, NULL, &nfsva, NULL,
3059 NULL, 0, NULL, NULL, NULL, NULL, NULL, 0,
3060 NULL, NULL, NULL, p, cred);
3062 dotdotfileid = dotfileid;
3063 } else if (gotmnton) {
3064 if (nfsva.na_mntonfileno != 0xffffffff)
3065 dotdotfileid = nfsva.na_mntonfileno;
3067 dotdotfileid = nfsva.na_fileid;
3068 } else if (nfsva.na_filesid[0] ==
3069 dnp->n_vattr.na_filesid[0] &&
3070 nfsva.na_filesid[1] ==
3071 dnp->n_vattr.na_filesid[1]) {
3072 dotdotfileid = nfsva.na_fileid;
3076 } while (fakefileno ==
3078 dotdotfileid = fakefileno;
3081 } else if (nd->nd_repstat == NFSERR_NOENT) {
3083 * Lookupp returns NFSERR_NOENT when we are
3084 * at the root, so just use the current dir.
3087 dotdotfileid = dotfileid;
3089 error = nd->nd_repstat;
3091 mbuf_freem(nd->nd_mrep);
3095 dp = (struct dirent *)uio_iov_base(uiop);
3096 dp->d_type = DT_DIR;
3097 dp->d_fileno = dotfileid;
3099 dp->d_name[0] = '.';
3100 dp->d_name[1] = '\0';
3101 dp->d_reclen = DIRENT_SIZE(dp) + NFSX_HYPER;
3103 * Just make these offset cookie 0.
3105 tl = (u_int32_t *)&dp->d_name[4];
3108 blksiz += dp->d_reclen;
3109 uio_uio_resid_add(uiop, -(dp->d_reclen));
3110 uiop->uio_offset += dp->d_reclen;
3111 uio_iov_base_add(uiop, dp->d_reclen);
3112 uio_iov_len_add(uiop, -(dp->d_reclen));
3113 dp = (struct dirent *)uio_iov_base(uiop);
3114 dp->d_type = DT_DIR;
3115 dp->d_fileno = dotdotfileid;
3117 dp->d_name[0] = '.';
3118 dp->d_name[1] = '.';
3119 dp->d_name[2] = '\0';
3120 dp->d_reclen = DIRENT_SIZE(dp) + NFSX_HYPER;
3122 * Just make these offset cookie 0.
3124 tl = (u_int32_t *)&dp->d_name[4];
3127 blksiz += dp->d_reclen;
3128 uio_uio_resid_add(uiop, -(dp->d_reclen));
3129 uiop->uio_offset += dp->d_reclen;
3130 uio_iov_base_add(uiop, dp->d_reclen);
3131 uio_iov_len_add(uiop, -(dp->d_reclen));
3133 NFSREADDIRPLUS_ATTRBIT(&attrbits);
3135 NFSSETBIT_ATTRBIT(&attrbits,
3136 NFSATTRBIT_MOUNTEDONFILEID);
3140 * Loop around doing readdir rpc's of size nm_readdirsize.
3141 * The stopping criteria is EOF or buffer full.
3143 while (more_dirs && bigenough) {
3145 NFSCL_REQSTART(nd, NFSPROC_READDIRPLUS, vp);
3146 NFSM_BUILD(tl, u_int32_t *, 6 * NFSX_UNSIGNED);
3147 *tl++ = cookie.lval[0];
3148 *tl++ = cookie.lval[1];
3149 if (cookie.qval == 0) {
3154 *tl++ = dnp->n_cookieverf.nfsuquad[0];
3155 *tl++ = dnp->n_cookieverf.nfsuquad[1];
3158 *tl++ = txdr_unsigned(nmp->nm_readdirsize);
3159 *tl = txdr_unsigned(nmp->nm_readdirsize);
3160 if (nd->nd_flag & ND_NFSV4) {
3161 (void) nfsrv_putattrbit(nd, &attrbits);
3162 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
3163 *tl = txdr_unsigned(NFSV4OP_GETATTR);
3164 (void) nfsrv_putattrbit(nd, &dattrbits);
3166 error = nfscl_request(nd, vp, p, cred, stuff);
3169 if (nd->nd_flag & ND_NFSV3)
3170 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
3171 if (nd->nd_repstat || error) {
3173 error = nd->nd_repstat;
3176 if ((nd->nd_flag & ND_NFSV3) != 0 && *attrflagp != 0)
3177 dctime = nap->na_ctime;
3178 NFSM_DISSECT(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
3180 dnp->n_cookieverf.nfsuquad[0] = *tl++;
3181 dnp->n_cookieverf.nfsuquad[1] = *tl++;
3183 more_dirs = fxdr_unsigned(int, *tl);
3187 /* loop thru the dir entries, doctoring them to 4bsd form */
3188 while (more_dirs && bigenough) {
3189 NFSM_DISSECT(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
3190 if (nd->nd_flag & ND_NFSV4) {
3191 ncookie.lval[0] = *tl++;
3192 ncookie.lval[1] = *tl++;
3194 fileno = fxdr_unsigned(long, *++tl);
3197 len = fxdr_unsigned(int, *tl);
3198 if (len <= 0 || len > NFS_MAXNAMLEN) {
3202 tlen = NFSM_RNDUP(len);
3204 tlen += 4; /* To ensure null termination */
3205 left = DIRBLKSIZ - blksiz;
3206 if ((tlen + DIRHDSIZ + NFSX_HYPER) > left) {
3207 dp->d_reclen += left;
3208 uio_iov_base_add(uiop, left);
3209 uio_iov_len_add(uiop, -(left));
3210 uio_uio_resid_add(uiop, -(left));
3211 uiop->uio_offset += left;
3214 if ((tlen + DIRHDSIZ + NFSX_HYPER) > uio_uio_resid(uiop))
3217 dp = (struct dirent *)uio_iov_base(uiop);
3219 dp->d_reclen = tlen + DIRHDSIZ + NFSX_HYPER;
3220 dp->d_type = DT_UNKNOWN;
3221 blksiz += dp->d_reclen;
3222 if (blksiz == DIRBLKSIZ)
3224 uio_uio_resid_add(uiop, -(DIRHDSIZ));
3225 uiop->uio_offset += DIRHDSIZ;
3226 uio_iov_base_add(uiop, DIRHDSIZ);
3227 uio_iov_len_add(uiop, -(DIRHDSIZ));
3228 cnp->cn_nameptr = uio_iov_base(uiop);
3229 cnp->cn_namelen = len;
3231 error = nfsm_mbufuio(nd, uiop, len);
3234 cp = uio_iov_base(uiop);
3237 cp += tlen; /* points to cookie storage */
3238 tl2 = (u_int32_t *)cp;
3239 if (len == 2 && cnp->cn_nameptr[0] == '.' &&
3240 cnp->cn_nameptr[1] == '.')
3244 uio_iov_base_add(uiop, (tlen + NFSX_HYPER));
3245 uio_iov_len_add(uiop, -(tlen + NFSX_HYPER));
3246 uio_uio_resid_add(uiop, -(tlen + NFSX_HYPER));
3247 uiop->uio_offset += (tlen + NFSX_HYPER);
3249 error = nfsm_advance(nd, NFSM_RNDUP(len), -1);
3254 if (nd->nd_flag & ND_NFSV3) {
3255 NFSM_DISSECT(tl, u_int32_t *, 3*NFSX_UNSIGNED);
3256 ncookie.lval[0] = *tl++;
3257 ncookie.lval[1] = *tl++;
3258 attrflag = fxdr_unsigned(int, *tl);
3260 error = nfsm_loadattr(nd, &nfsva);
3264 NFSM_DISSECT(tl,u_int32_t *,NFSX_UNSIGNED);
3266 error = nfsm_getfh(nd, &nfhp);
3270 if (!attrflag && nfhp != NULL) {
3271 FREE((caddr_t)nfhp, M_NFSFH);
3276 nfsva.na_mntonfileno = 0xffffffff;
3277 error = nfsv4_loadattr(nd, NULL, &nfsva, &nfhp,
3278 NULL, 0, NULL, NULL, NULL, NULL, NULL, 0,
3279 NULL, NULL, &rderr, p, cred);
3285 if (nd->nd_flag & ND_NFSV4) {
3288 } else if (gotmnton) {
3289 if (nfsva.na_mntonfileno != 0xffffffff)
3290 dp->d_fileno = nfsva.na_mntonfileno;
3292 dp->d_fileno = nfsva.na_fileid;
3293 } else if (nfsva.na_filesid[0] ==
3294 dnp->n_vattr.na_filesid[0] &&
3295 nfsva.na_filesid[1] ==
3296 dnp->n_vattr.na_filesid[1]) {
3297 dp->d_fileno = nfsva.na_fileid;
3301 } while (fakefileno ==
3303 dp->d_fileno = fakefileno;
3306 dp->d_fileno = fileno;
3308 *tl2++ = cookiep->nfsuquad[0] = cookie.lval[0] =
3310 *tl2 = cookiep->nfsuquad[1] = cookie.lval[1] =
3314 if (NFSRV_CMPFH(nfhp->nfh_fh, nfhp->nfh_len,
3315 dnp->n_fhp->nfh_fh, dnp->n_fhp->nfh_len)) {
3319 FREE((caddr_t)nfhp, M_NFSFH);
3321 } else if (isdotdot != 0) {
3323 * Skip doing a nfscl_nget() call for "..".
3324 * There's a race between acquiring the nfs
3325 * node here and lookups that look for the
3326 * directory being read (in the parent).
3327 * It would try to get a lock on ".." here,
3328 * owning the lock on the directory being
3329 * read. Lookup will hold the lock on ".."
3330 * and try to acquire the lock on the
3331 * directory being read.
3332 * If the directory is unlocked/relocked,
3333 * then there is a LOR with the buflock
3336 free(nfhp, M_NFSFH);
3338 error = nfscl_nget(vnode_mount(vp), vp,
3339 nfhp, cnp, p, &np, NULL, LK_EXCLUSIVE);
3346 if (newvp != NULLVP) {
3347 error = nfscl_loadattrcache(&newvp,
3348 &nfsva, NULL, NULL, 0, 0);
3357 vtonfs_dtype(np->n_vattr.na_type);
3359 NFSCNHASH(cnp, HASHINIT);
3360 if (cnp->cn_namelen <= NCHNAMLEN &&
3361 (newvp->v_type != VDIR ||
3362 dctime.tv_sec != 0)) {
3363 cache_enter_time(ndp->ni_dvp,
3366 newvp->v_type != VDIR ? NULL :
3376 } else if (nfhp != NULL) {
3377 FREE((caddr_t)nfhp, M_NFSFH);
3379 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3380 more_dirs = fxdr_unsigned(int, *tl);
3383 * If at end of rpc data, get the eof boolean
3386 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3387 eof = fxdr_unsigned(int, *tl);
3390 if (nd->nd_flag & ND_NFSV4) {
3391 error = nfscl_postop_attr(nd, nap, attrflagp,
3397 mbuf_freem(nd->nd_mrep);
3401 * Fill last record, iff any, out to a multiple of DIRBLKSIZ
3402 * by increasing d_reclen for the last record.
3405 left = DIRBLKSIZ - blksiz;
3406 dp->d_reclen += left;
3407 uio_iov_base_add(uiop, left);
3408 uio_iov_len_add(uiop, -(left));
3409 uio_uio_resid_add(uiop, -(left));
3410 uiop->uio_offset += left;
3414 * If returning no data, assume end of file.
3415 * If not bigenough, return not end of file, since you aren't
3416 * returning all the data
3417 * Otherwise, return the eof flag from the server.
3420 if (tresid == uio_uio_resid(uiop))
3422 else if (!bigenough)
3429 * Add extra empty records to any remaining DIRBLKSIZ chunks.
3431 while (uio_uio_resid(uiop) > 0 && uio_uio_resid(uiop) != tresid) {
3432 dp = (struct dirent *)uio_iov_base(uiop);
3433 dp->d_type = DT_UNKNOWN;
3436 dp->d_name[0] = '\0';
3437 tl = (u_int32_t *)&dp->d_name[4];
3438 *tl++ = cookie.lval[0];
3439 *tl = cookie.lval[1];
3440 dp->d_reclen = DIRBLKSIZ;
3441 uio_iov_base_add(uiop, DIRBLKSIZ);
3442 uio_iov_len_add(uiop, -(DIRBLKSIZ));
3443 uio_uio_resid_add(uiop, -(DIRBLKSIZ));
3444 uiop->uio_offset += DIRBLKSIZ;
3448 if (nd->nd_mrep != NULL)
3449 mbuf_freem(nd->nd_mrep);
3458 nfsrpc_commit(vnode_t vp, u_quad_t offset, int cnt, struct ucred *cred,
3459 NFSPROC_T *p, u_char *verfp, struct nfsvattr *nap, int *attrflagp,
3463 struct nfsrv_descript nfsd, *nd = &nfsd;
3464 nfsattrbit_t attrbits;
3468 NFSCL_REQSTART(nd, NFSPROC_COMMIT, vp);
3469 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
3470 txdr_hyper(offset, tl);
3472 *tl = txdr_unsigned(cnt);
3473 if (nd->nd_flag & ND_NFSV4) {
3475 * And do a Getattr op.
3477 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
3478 *tl = txdr_unsigned(NFSV4OP_GETATTR);
3479 NFSGETATTR_ATTRBIT(&attrbits);
3480 (void) nfsrv_putattrbit(nd, &attrbits);
3482 error = nfscl_request(nd, vp, p, cred, stuff);
3485 error = nfscl_wcc_data(nd, vp, nap, attrflagp, NULL, stuff);
3486 if (!error && !nd->nd_repstat) {
3487 NFSM_DISSECT(tl, u_int32_t *, NFSX_VERF);
3488 NFSBCOPY((caddr_t)tl, verfp, NFSX_VERF);
3489 if (nd->nd_flag & ND_NFSV4)
3490 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
3493 if (!error && nd->nd_repstat)
3494 error = nd->nd_repstat;
3495 mbuf_freem(nd->nd_mrep);
3500 * NFS byte range lock rpc.
3501 * (Mostly just calls one of the three lower level RPC routines.)
3504 nfsrpc_advlock(vnode_t vp, off_t size, int op, struct flock *fl,
3505 int reclaim, struct ucred *cred, NFSPROC_T *p, void *id, int flags)
3507 struct nfscllockowner *lp;
3508 struct nfsclclient *clp;
3510 struct nfsrv_descript nfsd, *nd = &nfsd;
3511 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
3514 u_int32_t clidrev = 0;
3515 int error = 0, newone = 0, expireret = 0, retrycnt, donelocally;
3519 * Convert the flock structure into a start and end and do POSIX
3522 switch (fl->l_whence) {
3526 * Caller is responsible for adding any necessary offset
3527 * when SEEK_CUR is used.
3529 start = fl->l_start;
3533 start = size + fl->l_start;
3534 off = size + fl->l_start;
3541 if (fl->l_len != 0) {
3542 end = start + fl->l_len - 1;
3553 if (op == F_GETLK) {
3554 error = nfscl_getcl(vp, cred, p, &clp);
3557 error = nfscl_lockt(vp, clp, off, len, fl, p, id, flags);
3559 clidrev = clp->nfsc_clientidrev;
3560 error = nfsrpc_lockt(nd, vp, clp, off, len, fl, cred,
3562 } else if (error == -1) {
3565 nfscl_clientrelease(clp);
3566 } else if (op == F_UNLCK && fl->l_type == F_UNLCK) {
3568 * We must loop around for all lockowner cases.
3571 error = nfscl_getcl(vp, cred, p, &clp);
3575 error = nfscl_relbytelock(vp, off, len, cred, p, callcnt,
3576 clp, id, flags, &lp, &dorpc);
3578 * If it returns a NULL lp, we're done.
3582 nfscl_clientrelease(clp);
3584 nfscl_releasealllocks(clp, vp, p, id, flags);
3587 if (nmp->nm_clp != NULL)
3588 clidrev = nmp->nm_clp->nfsc_clientidrev;
3592 * If the server doesn't support Posix lock semantics,
3593 * only allow locks on the entire file, since it won't
3594 * handle overlapping byte ranges.
3595 * There might still be a problem when a lock
3596 * upgrade/downgrade (read<->write) occurs, since the
3597 * server "might" expect an unlock first?
3599 if (dorpc && (lp->nfsl_open->nfso_posixlock ||
3600 (off == 0 && len == NFS64BITSSET))) {
3602 * Since the lock records will go away, we must
3603 * wait for grace and delay here.
3606 error = nfsrpc_locku(nd, nmp, lp, off, len,
3607 NFSV4LOCKT_READ, cred, p, 0);
3608 if ((nd->nd_repstat == NFSERR_GRACE ||
3609 nd->nd_repstat == NFSERR_DELAY) &&
3611 (void) nfs_catnap(PZERO, (int)nd->nd_repstat,
3613 } while ((nd->nd_repstat == NFSERR_GRACE ||
3614 nd->nd_repstat == NFSERR_DELAY) && error == 0);
3617 } while (error == 0 && nd->nd_repstat == 0);
3618 nfscl_releasealllocks(clp, vp, p, id, flags);
3619 } else if (op == F_SETLK) {
3620 error = nfscl_getbytelock(vp, off, len, fl->l_type, cred, p,
3621 NULL, 0, id, flags, NULL, NULL, &lp, &newone, &donelocally);
3622 if (error || donelocally) {
3625 if (nmp->nm_clp != NULL)
3626 clidrev = nmp->nm_clp->nfsc_clientidrev;
3629 nfhp = VTONFS(vp)->n_fhp;
3630 if (!lp->nfsl_open->nfso_posixlock &&
3631 (off != 0 || len != NFS64BITSSET)) {
3634 error = nfsrpc_lock(nd, nmp, vp, nfhp->nfh_fh,
3635 nfhp->nfh_len, lp, newone, reclaim, off,
3636 len, fl->l_type, cred, p, 0);
3639 error = nd->nd_repstat;
3640 nfscl_lockrelease(lp, error, newone);
3645 error = nd->nd_repstat;
3646 if (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
3647 error == NFSERR_STALEDONTRECOVER ||
3648 error == NFSERR_STALECLIENTID || error == NFSERR_DELAY) {
3649 (void) nfs_catnap(PZERO, error, "nfs_advlock");
3650 } else if ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID)
3652 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
3655 } while (error == NFSERR_GRACE ||
3656 error == NFSERR_STALECLIENTID || error == NFSERR_DELAY ||
3657 error == NFSERR_STALEDONTRECOVER || error == NFSERR_STALESTATEID ||
3658 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
3659 expireret == 0 && clidrev != 0 && retrycnt < 4));
3660 if (error && retrycnt >= 4)
3666 * The lower level routine for the LockT case.
3669 nfsrpc_lockt(struct nfsrv_descript *nd, vnode_t vp,
3670 struct nfsclclient *clp, u_int64_t off, u_int64_t len, struct flock *fl,
3671 struct ucred *cred, NFSPROC_T *p, void *id, int flags)
3674 int error, type, size;
3675 uint8_t own[NFSV4CL_LOCKNAMELEN + NFSX_V4FHMAX];
3678 NFSCL_REQSTART(nd, NFSPROC_LOCKT, vp);
3679 NFSM_BUILD(tl, u_int32_t *, 7 * NFSX_UNSIGNED);
3680 if (fl->l_type == F_RDLCK)
3681 *tl++ = txdr_unsigned(NFSV4LOCKT_READ);
3683 *tl++ = txdr_unsigned(NFSV4LOCKT_WRITE);
3684 txdr_hyper(off, tl);
3686 txdr_hyper(len, tl);
3688 *tl++ = clp->nfsc_clientid.lval[0];
3689 *tl = clp->nfsc_clientid.lval[1];
3690 nfscl_filllockowner(id, own, flags);
3692 NFSBCOPY(np->n_fhp->nfh_fh, &own[NFSV4CL_LOCKNAMELEN],
3693 np->n_fhp->nfh_len);
3694 (void)nfsm_strtom(nd, own, NFSV4CL_LOCKNAMELEN + np->n_fhp->nfh_len);
3695 error = nfscl_request(nd, vp, p, cred, NULL);
3698 if (nd->nd_repstat == 0) {
3699 fl->l_type = F_UNLCK;
3700 } else if (nd->nd_repstat == NFSERR_DENIED) {
3702 fl->l_whence = SEEK_SET;
3703 NFSM_DISSECT(tl, u_int32_t *, 8 * NFSX_UNSIGNED);
3704 fl->l_start = fxdr_hyper(tl);
3706 len = fxdr_hyper(tl);
3708 if (len == NFS64BITSSET)
3712 type = fxdr_unsigned(int, *tl++);
3713 if (type == NFSV4LOCKT_WRITE)
3714 fl->l_type = F_WRLCK;
3716 fl->l_type = F_RDLCK;
3718 * XXX For now, I have no idea what to do with the
3719 * conflicting lock_owner, so I'll just set the pid == 0
3720 * and skip over the lock_owner.
3722 fl->l_pid = (pid_t)0;
3724 size = fxdr_unsigned(int, *tl);
3725 if (size < 0 || size > NFSV4_OPAQUELIMIT)
3728 error = nfsm_advance(nd, NFSM_RNDUP(size), -1);
3729 } else if (nd->nd_repstat == NFSERR_STALECLIENTID)
3730 nfscl_initiate_recovery(clp);
3732 mbuf_freem(nd->nd_mrep);
3737 * Lower level function that performs the LockU RPC.
3740 nfsrpc_locku(struct nfsrv_descript *nd, struct nfsmount *nmp,
3741 struct nfscllockowner *lp, u_int64_t off, u_int64_t len,
3742 u_int32_t type, struct ucred *cred, NFSPROC_T *p, int syscred)
3747 nfscl_reqstart(nd, NFSPROC_LOCKU, nmp, lp->nfsl_open->nfso_fh,
3748 lp->nfsl_open->nfso_fhlen, NULL);
3749 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID + 6 * NFSX_UNSIGNED);
3750 *tl++ = txdr_unsigned(type);
3751 *tl = txdr_unsigned(lp->nfsl_seqid);
3752 if (nfstest_outofseq &&
3753 (arc4random() % nfstest_outofseq) == 0)
3754 *tl = txdr_unsigned(lp->nfsl_seqid + 1);
3756 *tl++ = lp->nfsl_stateid.seqid;
3757 *tl++ = lp->nfsl_stateid.other[0];
3758 *tl++ = lp->nfsl_stateid.other[1];
3759 *tl++ = lp->nfsl_stateid.other[2];
3760 txdr_hyper(off, tl);
3762 txdr_hyper(len, tl);
3764 nd->nd_flag |= ND_USEGSSNAME;
3765 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
3766 NFS_PROG, NFS_VER4, NULL, 1, NULL);
3767 NFSCL_INCRSEQID(lp->nfsl_seqid, nd);
3770 if (nd->nd_repstat == 0) {
3771 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID);
3772 lp->nfsl_stateid.seqid = *tl++;
3773 lp->nfsl_stateid.other[0] = *tl++;
3774 lp->nfsl_stateid.other[1] = *tl++;
3775 lp->nfsl_stateid.other[2] = *tl;
3776 } else if (nd->nd_repstat == NFSERR_STALESTATEID)
3777 nfscl_initiate_recovery(lp->nfsl_open->nfso_own->nfsow_clp);
3779 mbuf_freem(nd->nd_mrep);
3784 * The actual Lock RPC.
3787 nfsrpc_lock(struct nfsrv_descript *nd, struct nfsmount *nmp, vnode_t vp,
3788 u_int8_t *nfhp, int fhlen, struct nfscllockowner *lp, int newone,
3789 int reclaim, u_int64_t off, u_int64_t len, short type, struct ucred *cred,
3790 NFSPROC_T *p, int syscred)
3794 uint8_t own[NFSV4CL_LOCKNAMELEN + NFSX_V4FHMAX];
3796 nfscl_reqstart(nd, NFSPROC_LOCK, nmp, nfhp, fhlen, NULL);
3797 NFSM_BUILD(tl, u_int32_t *, 7 * NFSX_UNSIGNED);
3798 if (type == F_RDLCK)
3799 *tl++ = txdr_unsigned(NFSV4LOCKT_READ);
3801 *tl++ = txdr_unsigned(NFSV4LOCKT_WRITE);
3802 *tl++ = txdr_unsigned(reclaim);
3803 txdr_hyper(off, tl);
3805 txdr_hyper(len, tl);
3809 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID +
3810 2 * NFSX_UNSIGNED + NFSX_HYPER);
3811 *tl++ = txdr_unsigned(lp->nfsl_open->nfso_own->nfsow_seqid);
3812 *tl++ = lp->nfsl_open->nfso_stateid.seqid;
3813 *tl++ = lp->nfsl_open->nfso_stateid.other[0];
3814 *tl++ = lp->nfsl_open->nfso_stateid.other[1];
3815 *tl++ = lp->nfsl_open->nfso_stateid.other[2];
3816 *tl++ = txdr_unsigned(lp->nfsl_seqid);
3817 *tl++ = lp->nfsl_open->nfso_own->nfsow_clp->nfsc_clientid.lval[0];
3818 *tl = lp->nfsl_open->nfso_own->nfsow_clp->nfsc_clientid.lval[1];
3819 NFSBCOPY(lp->nfsl_owner, own, NFSV4CL_LOCKNAMELEN);
3820 NFSBCOPY(nfhp, &own[NFSV4CL_LOCKNAMELEN], fhlen);
3821 (void)nfsm_strtom(nd, own, NFSV4CL_LOCKNAMELEN + fhlen);
3824 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID + NFSX_UNSIGNED);
3825 *tl++ = lp->nfsl_stateid.seqid;
3826 *tl++ = lp->nfsl_stateid.other[0];
3827 *tl++ = lp->nfsl_stateid.other[1];
3828 *tl++ = lp->nfsl_stateid.other[2];
3829 *tl = txdr_unsigned(lp->nfsl_seqid);
3830 if (nfstest_outofseq &&
3831 (arc4random() % nfstest_outofseq) == 0)
3832 *tl = txdr_unsigned(lp->nfsl_seqid + 1);
3835 nd->nd_flag |= ND_USEGSSNAME;
3836 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, vp, p, cred,
3837 NFS_PROG, NFS_VER4, NULL, 1, NULL);
3841 NFSCL_INCRSEQID(lp->nfsl_open->nfso_own->nfsow_seqid, nd);
3842 NFSCL_INCRSEQID(lp->nfsl_seqid, nd);
3843 if (nd->nd_repstat == 0) {
3844 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID);
3845 lp->nfsl_stateid.seqid = *tl++;
3846 lp->nfsl_stateid.other[0] = *tl++;
3847 lp->nfsl_stateid.other[1] = *tl++;
3848 lp->nfsl_stateid.other[2] = *tl;
3849 } else if (nd->nd_repstat == NFSERR_DENIED) {
3850 NFSM_DISSECT(tl, u_int32_t *, 8 * NFSX_UNSIGNED);
3851 size = fxdr_unsigned(int, *(tl + 7));
3852 if (size < 0 || size > NFSV4_OPAQUELIMIT)
3855 error = nfsm_advance(nd, NFSM_RNDUP(size), -1);
3856 } else if (nd->nd_repstat == NFSERR_STALESTATEID)
3857 nfscl_initiate_recovery(lp->nfsl_open->nfso_own->nfsow_clp);
3859 mbuf_freem(nd->nd_mrep);
3865 * (always called with the vp for the mount point)
3868 nfsrpc_statfs(vnode_t vp, struct nfsstatfs *sbp, struct nfsfsinfo *fsp,
3869 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp,
3872 u_int32_t *tl = NULL;
3873 struct nfsrv_descript nfsd, *nd = &nfsd;
3874 struct nfsmount *nmp;
3875 nfsattrbit_t attrbits;
3879 nmp = VFSTONFS(vnode_mount(vp));
3880 if (NFSHASNFSV4(nmp)) {
3882 * For V4, you actually do a getattr.
3884 NFSCL_REQSTART(nd, NFSPROC_GETATTR, vp);
3885 NFSSTATFS_GETATTRBIT(&attrbits);
3886 (void) nfsrv_putattrbit(nd, &attrbits);
3887 nd->nd_flag |= ND_USEGSSNAME;
3888 error = nfscl_request(nd, vp, p, cred, stuff);
3891 if (nd->nd_repstat == 0) {
3892 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0,
3893 NULL, NULL, sbp, fsp, NULL, 0, NULL, NULL, NULL, p,
3896 nmp->nm_fsid[0] = nap->na_filesid[0];
3897 nmp->nm_fsid[1] = nap->na_filesid[1];
3898 NFSSETHASSETFSID(nmp);
3902 error = nd->nd_repstat;
3907 NFSCL_REQSTART(nd, NFSPROC_FSSTAT, vp);
3908 error = nfscl_request(nd, vp, p, cred, stuff);
3911 if (nd->nd_flag & ND_NFSV3) {
3912 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
3916 if (nd->nd_repstat) {
3917 error = nd->nd_repstat;
3920 NFSM_DISSECT(tl, u_int32_t *,
3921 NFSX_STATFS(nd->nd_flag & ND_NFSV3));
3923 if (NFSHASNFSV3(nmp)) {
3924 sbp->sf_tbytes = fxdr_hyper(tl); tl += 2;
3925 sbp->sf_fbytes = fxdr_hyper(tl); tl += 2;
3926 sbp->sf_abytes = fxdr_hyper(tl); tl += 2;
3927 sbp->sf_tfiles = fxdr_hyper(tl); tl += 2;
3928 sbp->sf_ffiles = fxdr_hyper(tl); tl += 2;
3929 sbp->sf_afiles = fxdr_hyper(tl); tl += 2;
3930 sbp->sf_invarsec = fxdr_unsigned(u_int32_t, *tl);
3931 } else if (NFSHASNFSV4(nmp) == 0) {
3932 sbp->sf_tsize = fxdr_unsigned(u_int32_t, *tl++);
3933 sbp->sf_bsize = fxdr_unsigned(u_int32_t, *tl++);
3934 sbp->sf_blocks = fxdr_unsigned(u_int32_t, *tl++);
3935 sbp->sf_bfree = fxdr_unsigned(u_int32_t, *tl++);
3936 sbp->sf_bavail = fxdr_unsigned(u_int32_t, *tl);
3939 mbuf_freem(nd->nd_mrep);
3947 nfsrpc_pathconf(vnode_t vp, struct nfsv3_pathconf *pc,
3948 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp,
3951 struct nfsrv_descript nfsd, *nd = &nfsd;
3952 struct nfsmount *nmp;
3954 nfsattrbit_t attrbits;
3958 nmp = VFSTONFS(vnode_mount(vp));
3959 if (NFSHASNFSV4(nmp)) {
3961 * For V4, you actually do a getattr.
3963 NFSCL_REQSTART(nd, NFSPROC_GETATTR, vp);
3964 NFSPATHCONF_GETATTRBIT(&attrbits);
3965 (void) nfsrv_putattrbit(nd, &attrbits);
3966 nd->nd_flag |= ND_USEGSSNAME;
3967 error = nfscl_request(nd, vp, p, cred, stuff);
3970 if (nd->nd_repstat == 0) {
3971 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0,
3972 pc, NULL, NULL, NULL, NULL, 0, NULL, NULL, NULL, p,
3977 error = nd->nd_repstat;
3980 NFSCL_REQSTART(nd, NFSPROC_PATHCONF, vp);
3981 error = nfscl_request(nd, vp, p, cred, stuff);
3984 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
3985 if (nd->nd_repstat && !error)
3986 error = nd->nd_repstat;
3988 NFSM_DISSECT(tl, u_int32_t *, NFSX_V3PATHCONF);
3989 pc->pc_linkmax = fxdr_unsigned(u_int32_t, *tl++);
3990 pc->pc_namemax = fxdr_unsigned(u_int32_t, *tl++);
3991 pc->pc_notrunc = fxdr_unsigned(u_int32_t, *tl++);
3992 pc->pc_chownrestricted =
3993 fxdr_unsigned(u_int32_t, *tl++);
3994 pc->pc_caseinsensitive =
3995 fxdr_unsigned(u_int32_t, *tl++);
3996 pc->pc_casepreserving = fxdr_unsigned(u_int32_t, *tl);
4000 mbuf_freem(nd->nd_mrep);
4005 * nfs version 3 fsinfo rpc call
4008 nfsrpc_fsinfo(vnode_t vp, struct nfsfsinfo *fsp, struct ucred *cred,
4009 NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, void *stuff)
4012 struct nfsrv_descript nfsd, *nd = &nfsd;
4016 NFSCL_REQSTART(nd, NFSPROC_FSINFO, vp);
4017 error = nfscl_request(nd, vp, p, cred, stuff);
4020 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
4021 if (nd->nd_repstat && !error)
4022 error = nd->nd_repstat;
4024 NFSM_DISSECT(tl, u_int32_t *, NFSX_V3FSINFO);
4025 fsp->fs_rtmax = fxdr_unsigned(u_int32_t, *tl++);
4026 fsp->fs_rtpref = fxdr_unsigned(u_int32_t, *tl++);
4027 fsp->fs_rtmult = fxdr_unsigned(u_int32_t, *tl++);
4028 fsp->fs_wtmax = fxdr_unsigned(u_int32_t, *tl++);
4029 fsp->fs_wtpref = fxdr_unsigned(u_int32_t, *tl++);
4030 fsp->fs_wtmult = fxdr_unsigned(u_int32_t, *tl++);
4031 fsp->fs_dtpref = fxdr_unsigned(u_int32_t, *tl++);
4032 fsp->fs_maxfilesize = fxdr_hyper(tl);
4034 fxdr_nfsv3time(tl, &fsp->fs_timedelta);
4036 fsp->fs_properties = fxdr_unsigned(u_int32_t, *tl);
4039 mbuf_freem(nd->nd_mrep);
4044 * This function performs the Renew RPC.
4047 nfsrpc_renew(struct nfsclclient *clp, struct ucred *cred, NFSPROC_T *p)
4050 struct nfsrv_descript nfsd;
4051 struct nfsrv_descript *nd = &nfsd;
4052 struct nfsmount *nmp;
4055 nmp = clp->nfsc_nmp;
4058 nfscl_reqstart(nd, NFSPROC_RENEW, nmp, NULL, 0, NULL);
4059 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
4060 *tl++ = clp->nfsc_clientid.lval[0];
4061 *tl = clp->nfsc_clientid.lval[1];
4062 nd->nd_flag |= ND_USEGSSNAME;
4063 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4064 NFS_PROG, NFS_VER4, NULL, 1, NULL);
4067 error = nd->nd_repstat;
4068 mbuf_freem(nd->nd_mrep);
4073 * This function performs the Releaselockowner RPC.
4076 nfsrpc_rellockown(struct nfsmount *nmp, struct nfscllockowner *lp,
4077 uint8_t *fh, int fhlen, struct ucred *cred, NFSPROC_T *p)
4079 struct nfsrv_descript nfsd, *nd = &nfsd;
4082 uint8_t own[NFSV4CL_LOCKNAMELEN + NFSX_V4FHMAX];
4084 nfscl_reqstart(nd, NFSPROC_RELEASELCKOWN, nmp, NULL, 0, NULL);
4085 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
4086 *tl++ = nmp->nm_clp->nfsc_clientid.lval[0];
4087 *tl = nmp->nm_clp->nfsc_clientid.lval[1];
4088 NFSBCOPY(lp->nfsl_owner, own, NFSV4CL_LOCKNAMELEN);
4089 NFSBCOPY(fh, &own[NFSV4CL_LOCKNAMELEN], fhlen);
4090 (void)nfsm_strtom(nd, own, NFSV4CL_LOCKNAMELEN + fhlen);
4091 nd->nd_flag |= ND_USEGSSNAME;
4092 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4093 NFS_PROG, NFS_VER4, NULL, 1, NULL);
4096 error = nd->nd_repstat;
4097 mbuf_freem(nd->nd_mrep);
4102 * This function performs the Compound to get the mount pt FH.
4105 nfsrpc_getdirpath(struct nfsmount *nmp, u_char *dirpath, struct ucred *cred,
4109 struct nfsrv_descript nfsd;
4110 struct nfsrv_descript *nd = &nfsd;
4112 int error, cnt, len, setnil;
4115 nfscl_reqstart(nd, NFSPROC_PUTROOTFH, nmp, NULL, 0, &opcntp);
4123 while (*cp2 != '\0' && *cp2 != '/')
4130 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
4131 *tl = txdr_unsigned(NFSV4OP_LOOKUP);
4132 nfsm_strtom(nd, cp, strlen(cp));
4138 } while (*cp != '\0');
4139 *opcntp = txdr_unsigned(2 + cnt);
4140 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
4141 *tl = txdr_unsigned(NFSV4OP_GETFH);
4142 nd->nd_flag |= ND_USEGSSNAME;
4143 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4144 NFS_PROG, NFS_VER4, NULL, 1, NULL);
4147 if (nd->nd_repstat == 0) {
4148 NFSM_DISSECT(tl, u_int32_t *, (3 + 2 * cnt) * NFSX_UNSIGNED);
4149 tl += (2 + 2 * cnt);
4150 if ((len = fxdr_unsigned(int, *tl)) <= 0 ||
4152 nd->nd_repstat = NFSERR_BADXDR;
4154 nd->nd_repstat = nfsrv_mtostr(nd, nmp->nm_fh, len);
4155 if (nd->nd_repstat == 0)
4156 nmp->nm_fhsize = len;
4159 error = nd->nd_repstat;
4161 mbuf_freem(nd->nd_mrep);
4166 * This function performs the Delegreturn RPC.
4169 nfsrpc_delegreturn(struct nfscldeleg *dp, struct ucred *cred,
4170 struct nfsmount *nmp, NFSPROC_T *p, int syscred)
4173 struct nfsrv_descript nfsd;
4174 struct nfsrv_descript *nd = &nfsd;
4177 nfscl_reqstart(nd, NFSPROC_DELEGRETURN, nmp, dp->nfsdl_fh,
4178 dp->nfsdl_fhlen, NULL);
4179 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID);
4180 *tl++ = dp->nfsdl_stateid.seqid;
4181 *tl++ = dp->nfsdl_stateid.other[0];
4182 *tl++ = dp->nfsdl_stateid.other[1];
4183 *tl = dp->nfsdl_stateid.other[2];
4185 nd->nd_flag |= ND_USEGSSNAME;
4186 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4187 NFS_PROG, NFS_VER4, NULL, 1, NULL);
4190 error = nd->nd_repstat;
4191 mbuf_freem(nd->nd_mrep);
4199 nfsrpc_getacl(vnode_t vp, struct ucred *cred, NFSPROC_T *p,
4200 struct acl *aclp, void *stuff)
4202 struct nfsrv_descript nfsd, *nd = &nfsd;
4204 nfsattrbit_t attrbits;
4205 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
4207 if (nfsrv_useacl == 0 || !NFSHASNFSV4(nmp))
4208 return (EOPNOTSUPP);
4209 NFSCL_REQSTART(nd, NFSPROC_GETACL, vp);
4210 NFSZERO_ATTRBIT(&attrbits);
4211 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_ACL);
4212 (void) nfsrv_putattrbit(nd, &attrbits);
4213 error = nfscl_request(nd, vp, p, cred, stuff);
4216 if (!nd->nd_repstat)
4217 error = nfsv4_loadattr(nd, vp, NULL, NULL, NULL, 0, NULL,
4218 NULL, NULL, NULL, aclp, 0, NULL, NULL, NULL, p, cred);
4220 error = nd->nd_repstat;
4221 mbuf_freem(nd->nd_mrep);
4229 nfsrpc_setacl(vnode_t vp, struct ucred *cred, NFSPROC_T *p,
4230 struct acl *aclp, void *stuff)
4233 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
4235 if (nfsrv_useacl == 0 || !NFSHASNFSV4(nmp))
4236 return (EOPNOTSUPP);
4237 error = nfsrpc_setattr(vp, NULL, aclp, cred, p, NULL, NULL, stuff);
4245 nfsrpc_setaclrpc(vnode_t vp, struct ucred *cred, NFSPROC_T *p,
4246 struct acl *aclp, nfsv4stateid_t *stateidp, void *stuff)
4248 struct nfsrv_descript nfsd, *nd = &nfsd;
4250 nfsattrbit_t attrbits;
4251 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
4253 if (!NFSHASNFSV4(nmp))
4254 return (EOPNOTSUPP);
4255 NFSCL_REQSTART(nd, NFSPROC_SETACL, vp);
4256 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID);
4257 NFSZERO_ATTRBIT(&attrbits);
4258 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_ACL);
4259 (void) nfsv4_fillattr(nd, vnode_mount(vp), vp, aclp, NULL, NULL, 0,
4260 &attrbits, NULL, NULL, 0, 0, 0, 0, (uint64_t)0);
4261 error = nfscl_request(nd, vp, p, cred, stuff);
4264 /* Don't care about the pre/postop attributes */
4265 mbuf_freem(nd->nd_mrep);
4266 return (nd->nd_repstat);