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, nap, (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);
2034 if (ret == NFSERR_STALECLIENTID ||
2035 ret == NFSERR_STALEDONTRECOVER)
2039 nfscl_openrelease(op, error, newone);
2042 if (nd->nd_repstat != 0 && error == 0)
2043 error = nd->nd_repstat;
2044 if (error == NFSERR_STALECLIENTID)
2045 nfscl_initiate_recovery(owp->nfsow_clp);
2049 else if (dp != NULL)
2050 FREE((caddr_t)dp, M_NFSCLDELEG);
2051 mbuf_freem(nd->nd_mrep);
2059 nfsrpc_remove(vnode_t dvp, char *name, int namelen, vnode_t vp,
2060 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap, int *dattrflagp,
2064 struct nfsrv_descript nfsd, *nd = &nfsd;
2066 struct nfsmount *nmp;
2067 nfsv4stateid_t dstateid;
2068 int error, ret = 0, i;
2071 if (namelen > NFS_MAXNAMLEN)
2072 return (ENAMETOOLONG);
2073 nmp = VFSTONFS(vnode_mount(dvp));
2075 if (NFSHASNFSV4(nmp) && ret == 0) {
2076 ret = nfscl_removedeleg(vp, p, &dstateid);
2078 NFSCL_REQSTART(nd, NFSPROC_RETDELEGREMOVE, vp);
2079 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID +
2081 *tl++ = dstateid.seqid;
2082 *tl++ = dstateid.other[0];
2083 *tl++ = dstateid.other[1];
2084 *tl++ = dstateid.other[2];
2085 *tl = txdr_unsigned(NFSV4OP_PUTFH);
2087 (void) nfsm_fhtom(nd, np->n_fhp->nfh_fh,
2088 np->n_fhp->nfh_len, 0);
2089 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2090 *tl = txdr_unsigned(NFSV4OP_REMOVE);
2096 NFSCL_REQSTART(nd, NFSPROC_REMOVE, dvp);
2097 (void) nfsm_strtom(nd, name, namelen);
2098 error = nfscl_request(nd, dvp, p, cred, dstuff);
2101 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) {
2102 /* For NFSv4, parse out any Delereturn replies. */
2103 if (ret > 0 && nd->nd_repstat != 0 &&
2104 (nd->nd_flag & ND_NOMOREDATA)) {
2106 * If the Delegreturn failed, try again without
2107 * it. The server will Recall, as required.
2109 mbuf_freem(nd->nd_mrep);
2112 for (i = 0; i < (ret * 2); i++) {
2113 if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) ==
2115 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2117 nd->nd_flag |= ND_NOMOREDATA;
2120 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
2122 if (nd->nd_repstat && !error)
2123 error = nd->nd_repstat;
2125 mbuf_freem(nd->nd_mrep);
2130 * Do an nfs rename rpc.
2133 nfsrpc_rename(vnode_t fdvp, vnode_t fvp, char *fnameptr, int fnamelen,
2134 vnode_t tdvp, vnode_t tvp, char *tnameptr, int tnamelen, struct ucred *cred,
2135 NFSPROC_T *p, struct nfsvattr *fnap, struct nfsvattr *tnap,
2136 int *fattrflagp, int *tattrflagp, void *fstuff, void *tstuff)
2139 struct nfsrv_descript nfsd, *nd = &nfsd;
2140 struct nfsmount *nmp;
2142 nfsattrbit_t attrbits;
2143 nfsv4stateid_t fdstateid, tdstateid;
2144 int error = 0, ret = 0, gottd = 0, gotfd = 0, i;
2148 nmp = VFSTONFS(vnode_mount(fdvp));
2149 if (fnamelen > NFS_MAXNAMLEN || tnamelen > NFS_MAXNAMLEN)
2150 return (ENAMETOOLONG);
2152 if (NFSHASNFSV4(nmp) && ret == 0) {
2153 ret = nfscl_renamedeleg(fvp, &fdstateid, &gotfd, tvp,
2154 &tdstateid, &gottd, p);
2155 if (gotfd && gottd) {
2156 NFSCL_REQSTART(nd, NFSPROC_RETDELEGRENAME2, fvp);
2158 NFSCL_REQSTART(nd, NFSPROC_RETDELEGRENAME1, fvp);
2160 NFSCL_REQSTART(nd, NFSPROC_RETDELEGRENAME1, tvp);
2163 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID);
2164 *tl++ = fdstateid.seqid;
2165 *tl++ = fdstateid.other[0];
2166 *tl++ = fdstateid.other[1];
2167 *tl = fdstateid.other[2];
2169 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2170 *tl = txdr_unsigned(NFSV4OP_PUTFH);
2172 (void) nfsm_fhtom(nd, np->n_fhp->nfh_fh,
2173 np->n_fhp->nfh_len, 0);
2174 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2175 *tl = txdr_unsigned(NFSV4OP_DELEGRETURN);
2179 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID);
2180 *tl++ = tdstateid.seqid;
2181 *tl++ = tdstateid.other[0];
2182 *tl++ = tdstateid.other[1];
2183 *tl = tdstateid.other[2];
2186 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2187 *tl = txdr_unsigned(NFSV4OP_PUTFH);
2189 (void) nfsm_fhtom(nd, np->n_fhp->nfh_fh,
2190 np->n_fhp->nfh_len, 0);
2191 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2192 *tl = txdr_unsigned(NFSV4OP_SAVEFH);
2198 NFSCL_REQSTART(nd, NFSPROC_RENAME, fdvp);
2199 if (nd->nd_flag & ND_NFSV4) {
2200 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2201 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2202 NFSWCCATTR_ATTRBIT(&attrbits);
2203 (void) nfsrv_putattrbit(nd, &attrbits);
2204 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2205 *tl = txdr_unsigned(NFSV4OP_PUTFH);
2206 (void) nfsm_fhtom(nd, VTONFS(tdvp)->n_fhp->nfh_fh,
2207 VTONFS(tdvp)->n_fhp->nfh_len, 0);
2208 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2209 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2210 (void) nfsrv_putattrbit(nd, &attrbits);
2211 nd->nd_flag |= ND_V4WCCATTR;
2212 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2213 *tl = txdr_unsigned(NFSV4OP_RENAME);
2215 (void) nfsm_strtom(nd, fnameptr, fnamelen);
2216 if (!(nd->nd_flag & ND_NFSV4))
2217 (void) nfsm_fhtom(nd, VTONFS(tdvp)->n_fhp->nfh_fh,
2218 VTONFS(tdvp)->n_fhp->nfh_len, 0);
2219 (void) nfsm_strtom(nd, tnameptr, tnamelen);
2220 error = nfscl_request(nd, fdvp, p, cred, fstuff);
2223 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) {
2224 /* For NFSv4, parse out any Delereturn replies. */
2225 if (ret > 0 && nd->nd_repstat != 0 &&
2226 (nd->nd_flag & ND_NOMOREDATA)) {
2228 * If the Delegreturn failed, try again without
2229 * it. The server will Recall, as required.
2231 mbuf_freem(nd->nd_mrep);
2234 for (i = 0; i < (ret * 2); i++) {
2235 if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) ==
2237 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2239 if (i == 0 && ret > 1) {
2241 * If the Delegreturn failed, try again
2242 * without it. The server will Recall, as
2244 * If ret > 1, the first iteration of this
2245 * loop is the second DelegReturn result.
2247 mbuf_freem(nd->nd_mrep);
2250 nd->nd_flag |= ND_NOMOREDATA;
2255 /* Now, the first wcc attribute reply. */
2256 if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) == ND_NFSV4) {
2257 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2259 nd->nd_flag |= ND_NOMOREDATA;
2261 error = nfscl_wcc_data(nd, fdvp, fnap, fattrflagp, NULL,
2263 /* and the second wcc attribute reply. */
2264 if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) == ND_NFSV4 &&
2266 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2268 nd->nd_flag |= ND_NOMOREDATA;
2271 error = nfscl_wcc_data(nd, tdvp, tnap, tattrflagp,
2274 if (nd->nd_repstat && !error)
2275 error = nd->nd_repstat;
2277 mbuf_freem(nd->nd_mrep);
2282 * nfs hard link create rpc
2285 nfsrpc_link(vnode_t dvp, vnode_t vp, char *name, int namelen,
2286 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap,
2287 struct nfsvattr *nap, int *attrflagp, int *dattrflagp, void *dstuff)
2290 struct nfsrv_descript nfsd, *nd = &nfsd;
2291 nfsattrbit_t attrbits;
2296 if (namelen > NFS_MAXNAMLEN)
2297 return (ENAMETOOLONG);
2298 NFSCL_REQSTART(nd, NFSPROC_LINK, vp);
2299 if (nd->nd_flag & ND_NFSV4) {
2300 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2301 *tl = txdr_unsigned(NFSV4OP_PUTFH);
2303 (void) nfsm_fhtom(nd, VTONFS(dvp)->n_fhp->nfh_fh,
2304 VTONFS(dvp)->n_fhp->nfh_len, 0);
2305 if (nd->nd_flag & ND_NFSV4) {
2306 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2307 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2308 NFSWCCATTR_ATTRBIT(&attrbits);
2309 (void) nfsrv_putattrbit(nd, &attrbits);
2310 nd->nd_flag |= ND_V4WCCATTR;
2311 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2312 *tl = txdr_unsigned(NFSV4OP_LINK);
2314 (void) nfsm_strtom(nd, name, namelen);
2315 error = nfscl_request(nd, vp, p, cred, dstuff);
2318 if (nd->nd_flag & ND_NFSV3) {
2319 error = nfscl_postop_attr(nd, nap, attrflagp, dstuff);
2321 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp,
2323 } else if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) == ND_NFSV4) {
2325 * First, parse out the PutFH and Getattr result.
2327 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2329 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2331 nd->nd_flag |= ND_NOMOREDATA;
2333 * Get the pre-op attributes.
2335 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
2337 if (nd->nd_repstat && !error)
2338 error = nd->nd_repstat;
2340 mbuf_freem(nd->nd_mrep);
2345 * nfs symbolic link create rpc
2348 nfsrpc_symlink(vnode_t dvp, char *name, int namelen, char *target,
2349 struct vattr *vap, struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap,
2350 struct nfsvattr *nnap, struct nfsfh **nfhpp, int *attrflagp,
2351 int *dattrflagp, void *dstuff)
2354 struct nfsrv_descript nfsd, *nd = &nfsd;
2355 struct nfsmount *nmp;
2356 int slen, error = 0;
2361 nmp = VFSTONFS(vnode_mount(dvp));
2362 slen = strlen(target);
2363 if (slen > NFS_MAXPATHLEN || namelen > NFS_MAXNAMLEN)
2364 return (ENAMETOOLONG);
2365 NFSCL_REQSTART(nd, NFSPROC_SYMLINK, dvp);
2366 if (nd->nd_flag & ND_NFSV4) {
2367 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2368 *tl = txdr_unsigned(NFLNK);
2369 (void) nfsm_strtom(nd, target, slen);
2371 (void) nfsm_strtom(nd, name, namelen);
2372 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4))
2373 nfscl_fillsattr(nd, vap, dvp, 0, 0);
2374 if (!(nd->nd_flag & ND_NFSV4))
2375 (void) nfsm_strtom(nd, target, slen);
2376 if (nd->nd_flag & ND_NFSV2)
2377 nfscl_fillsattr(nd, vap, dvp, NFSSATTR_SIZENEG1, 0);
2378 error = nfscl_request(nd, dvp, p, cred, dstuff);
2381 if (nd->nd_flag & ND_NFSV4)
2382 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
2383 if ((nd->nd_flag & ND_NFSV3) && !error) {
2384 if (!nd->nd_repstat)
2385 error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp);
2387 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp,
2390 if (nd->nd_repstat && !error)
2391 error = nd->nd_repstat;
2392 mbuf_freem(nd->nd_mrep);
2394 * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry.
2396 if (error == EEXIST)
2405 nfsrpc_mkdir(vnode_t dvp, char *name, int namelen, struct vattr *vap,
2406 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap,
2407 struct nfsvattr *nnap, struct nfsfh **nfhpp, int *attrflagp,
2408 int *dattrflagp, void *dstuff)
2411 struct nfsrv_descript nfsd, *nd = &nfsd;
2412 nfsattrbit_t attrbits;
2419 fhp = VTONFS(dvp)->n_fhp;
2420 if (namelen > NFS_MAXNAMLEN)
2421 return (ENAMETOOLONG);
2422 NFSCL_REQSTART(nd, NFSPROC_MKDIR, dvp);
2423 if (nd->nd_flag & ND_NFSV4) {
2424 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2425 *tl = txdr_unsigned(NFDIR);
2427 (void) nfsm_strtom(nd, name, namelen);
2428 nfscl_fillsattr(nd, vap, dvp, NFSSATTR_SIZENEG1, 0);
2429 if (nd->nd_flag & ND_NFSV4) {
2430 NFSGETATTR_ATTRBIT(&attrbits);
2431 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2432 *tl++ = txdr_unsigned(NFSV4OP_GETFH);
2433 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2434 (void) nfsrv_putattrbit(nd, &attrbits);
2435 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2436 *tl = txdr_unsigned(NFSV4OP_PUTFH);
2437 (void) nfsm_fhtom(nd, fhp->nfh_fh, fhp->nfh_len, 0);
2438 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2439 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2440 (void) nfsrv_putattrbit(nd, &attrbits);
2442 error = nfscl_request(nd, dvp, p, cred, dstuff);
2445 if (nd->nd_flag & ND_NFSV4)
2446 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
2447 if (!nd->nd_repstat && !error) {
2448 if (nd->nd_flag & ND_NFSV4) {
2449 NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
2450 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
2453 error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp);
2454 if (error == 0 && (nd->nd_flag & ND_NFSV4) != 0) {
2455 /* Get rid of the PutFH and Getattr status values. */
2456 NFSM_DISSECT(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
2457 /* Load the directory attributes. */
2458 error = nfsm_loadattr(nd, dnap);
2463 if ((nd->nd_flag & ND_NFSV3) && !error)
2464 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
2465 if (nd->nd_repstat && !error)
2466 error = nd->nd_repstat;
2468 mbuf_freem(nd->nd_mrep);
2470 * Kludge: Map EEXIST => 0 assuming that you have a reply to a retry.
2472 if (error == EEXIST)
2478 * nfs remove directory call
2481 nfsrpc_rmdir(vnode_t dvp, char *name, int namelen, struct ucred *cred,
2482 NFSPROC_T *p, struct nfsvattr *dnap, int *dattrflagp, void *dstuff)
2484 struct nfsrv_descript nfsd, *nd = &nfsd;
2488 if (namelen > NFS_MAXNAMLEN)
2489 return (ENAMETOOLONG);
2490 NFSCL_REQSTART(nd, NFSPROC_RMDIR, dvp);
2491 (void) nfsm_strtom(nd, name, namelen);
2492 error = nfscl_request(nd, dvp, p, cred, dstuff);
2495 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4))
2496 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
2497 if (nd->nd_repstat && !error)
2498 error = nd->nd_repstat;
2499 mbuf_freem(nd->nd_mrep);
2501 * Kludge: Map ENOENT => 0 assuming that you have a reply to a retry.
2503 if (error == ENOENT)
2510 * Always returns with either uio_resid unchanged, if you are at the
2511 * end of the directory, or uio_resid == 0, with all DIRBLKSIZ chunks
2513 * I felt this would allow caching of directory blocks more easily
2514 * than returning a pertially filled block.
2515 * Directory offset cookies:
2516 * Oh my, what to do with them...
2517 * I can think of three ways to deal with them:
2518 * 1 - have the layer above these RPCs maintain a map between logical
2519 * directory byte offsets and the NFS directory offset cookies
2520 * 2 - pass the opaque directory offset cookies up into userland
2521 * and let the libc functions deal with them, via the system call
2522 * 3 - return them to userland in the "struct dirent", so future versions
2523 * of libc can use them and do whatever is necessary to amke things work
2524 * above these rpc calls, in the meantime
2525 * For now, I do #3 by "hiding" the directory offset cookies after the
2526 * d_name field in struct dirent. This is space inside d_reclen that
2527 * will be ignored by anything that doesn't know about them.
2528 * The directory offset cookies are filled in as the last 8 bytes of
2529 * each directory entry, after d_name. Someday, the userland libc
2530 * functions may be able to use these. In the meantime, it satisfies
2531 * OpenBSD's requirements for cookies being returned.
2532 * If expects the directory offset cookie for the read to be in uio_offset
2533 * and returns the one for the next entry after this directory block in
2537 nfsrpc_readdir(vnode_t vp, struct uio *uiop, nfsuint64 *cookiep,
2538 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp,
2539 int *eofp, void *stuff)
2542 struct dirent *dp = NULL;
2544 nfsquad_t cookie, ncookie;
2545 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
2546 struct nfsnode *dnp = VTONFS(vp);
2547 struct nfsvattr nfsva;
2548 struct nfsrv_descript nfsd, *nd = &nfsd;
2549 int error = 0, tlen, more_dirs = 1, blksiz = 0, bigenough = 1;
2550 int reqsize, tryformoredirs = 1, readsize, eof = 0, gotmnton = 0;
2551 long dotfileid, dotdotfileid = 0;
2552 u_int32_t fakefileno = 0xffffffff, rderr;
2554 nfsattrbit_t attrbits, dattrbits;
2555 u_int32_t *tl2 = NULL;
2558 KASSERT(uiop->uio_iovcnt == 1 &&
2559 (uio_uio_resid(uiop) & (DIRBLKSIZ - 1)) == 0,
2560 ("nfs readdirrpc bad uio"));
2563 * There is no point in reading a lot more than uio_resid, however
2564 * adding one additional DIRBLKSIZ makes sense. Since uio_resid
2565 * and nm_readdirsize are both exact multiples of DIRBLKSIZ, this
2566 * will never make readsize > nm_readdirsize.
2568 readsize = nmp->nm_readdirsize;
2569 if (readsize > uio_uio_resid(uiop))
2570 readsize = uio_uio_resid(uiop) + DIRBLKSIZ;
2575 tresid = uio_uio_resid(uiop);
2576 cookie.lval[0] = cookiep->nfsuquad[0];
2577 cookie.lval[1] = cookiep->nfsuquad[1];
2581 * For NFSv4, first create the "." and ".." entries.
2583 if (NFSHASNFSV4(nmp)) {
2584 reqsize = 6 * NFSX_UNSIGNED;
2585 NFSGETATTR_ATTRBIT(&dattrbits);
2586 NFSZERO_ATTRBIT(&attrbits);
2587 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_FILEID);
2588 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TYPE);
2589 if (NFSISSET_ATTRBIT(&dnp->n_vattr.na_suppattr,
2590 NFSATTRBIT_MOUNTEDONFILEID)) {
2591 NFSSETBIT_ATTRBIT(&attrbits,
2592 NFSATTRBIT_MOUNTEDONFILEID);
2596 * Must fake it. Use the fileno, except when the
2597 * fsid is != to that of the directory. For that
2598 * case, generate a fake fileno that is not the same.
2600 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_FSID);
2605 * Joy, oh joy. For V4 we get to hand craft '.' and '..'.
2607 if (uiop->uio_offset == 0) {
2608 NFSCL_REQSTART(nd, NFSPROC_LOOKUPP, vp);
2609 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2610 *tl++ = txdr_unsigned(NFSV4OP_GETFH);
2611 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2612 (void) nfsrv_putattrbit(nd, &attrbits);
2613 error = nfscl_request(nd, vp, p, cred, stuff);
2616 dotfileid = 0; /* Fake out the compiler. */
2617 if ((nd->nd_flag & ND_NOMOREDATA) == 0) {
2618 error = nfsm_loadattr(nd, &nfsva);
2621 dotfileid = nfsva.na_fileid;
2623 if (nd->nd_repstat == 0) {
2624 NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
2625 len = fxdr_unsigned(int, *(tl + 4));
2626 if (len > 0 && len <= NFSX_V4FHMAX)
2627 error = nfsm_advance(nd, NFSM_RNDUP(len), -1);
2631 NFSM_DISSECT(tl, u_int32_t *, 2*NFSX_UNSIGNED);
2632 nfsva.na_mntonfileno = 0xffffffff;
2633 error = nfsv4_loadattr(nd, NULL, &nfsva, NULL,
2634 NULL, 0, NULL, NULL, NULL, NULL, NULL, 0,
2635 NULL, NULL, NULL, p, cred);
2637 dotdotfileid = dotfileid;
2638 } else if (gotmnton) {
2639 if (nfsva.na_mntonfileno != 0xffffffff)
2640 dotdotfileid = nfsva.na_mntonfileno;
2642 dotdotfileid = nfsva.na_fileid;
2643 } else if (nfsva.na_filesid[0] ==
2644 dnp->n_vattr.na_filesid[0] &&
2645 nfsva.na_filesid[1] ==
2646 dnp->n_vattr.na_filesid[1]) {
2647 dotdotfileid = nfsva.na_fileid;
2651 } while (fakefileno ==
2653 dotdotfileid = fakefileno;
2656 } else if (nd->nd_repstat == NFSERR_NOENT) {
2658 * Lookupp returns NFSERR_NOENT when we are
2659 * at the root, so just use the current dir.
2662 dotdotfileid = dotfileid;
2664 error = nd->nd_repstat;
2666 mbuf_freem(nd->nd_mrep);
2670 dp = (struct dirent *) CAST_DOWN(caddr_t, uio_iov_base(uiop));
2671 dp->d_type = DT_DIR;
2672 dp->d_fileno = dotfileid;
2674 dp->d_name[0] = '.';
2675 dp->d_name[1] = '\0';
2676 dp->d_reclen = DIRENT_SIZE(dp) + NFSX_HYPER;
2678 * Just make these offset cookie 0.
2680 tl = (u_int32_t *)&dp->d_name[4];
2683 blksiz += dp->d_reclen;
2684 uio_uio_resid_add(uiop, -(dp->d_reclen));
2685 uiop->uio_offset += dp->d_reclen;
2686 uio_iov_base_add(uiop, dp->d_reclen);
2687 uio_iov_len_add(uiop, -(dp->d_reclen));
2688 dp = (struct dirent *) CAST_DOWN(caddr_t, uio_iov_base(uiop));
2689 dp->d_type = DT_DIR;
2690 dp->d_fileno = dotdotfileid;
2692 dp->d_name[0] = '.';
2693 dp->d_name[1] = '.';
2694 dp->d_name[2] = '\0';
2695 dp->d_reclen = DIRENT_SIZE(dp) + NFSX_HYPER;
2697 * Just make these offset cookie 0.
2699 tl = (u_int32_t *)&dp->d_name[4];
2702 blksiz += dp->d_reclen;
2703 uio_uio_resid_add(uiop, -(dp->d_reclen));
2704 uiop->uio_offset += dp->d_reclen;
2705 uio_iov_base_add(uiop, dp->d_reclen);
2706 uio_iov_len_add(uiop, -(dp->d_reclen));
2708 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_RDATTRERROR);
2710 reqsize = 5 * NFSX_UNSIGNED;
2715 * Loop around doing readdir rpc's of size readsize.
2716 * The stopping criteria is EOF or buffer full.
2718 while (more_dirs && bigenough) {
2720 NFSCL_REQSTART(nd, NFSPROC_READDIR, vp);
2721 if (nd->nd_flag & ND_NFSV2) {
2722 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2723 *tl++ = cookie.lval[1];
2724 *tl = txdr_unsigned(readsize);
2726 NFSM_BUILD(tl, u_int32_t *, reqsize);
2727 *tl++ = cookie.lval[0];
2728 *tl++ = cookie.lval[1];
2729 if (cookie.qval == 0) {
2734 *tl++ = dnp->n_cookieverf.nfsuquad[0];
2735 *tl++ = dnp->n_cookieverf.nfsuquad[1];
2738 if (nd->nd_flag & ND_NFSV4) {
2739 *tl++ = txdr_unsigned(readsize);
2740 *tl = txdr_unsigned(readsize);
2741 (void) nfsrv_putattrbit(nd, &attrbits);
2742 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2743 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2744 (void) nfsrv_putattrbit(nd, &dattrbits);
2746 *tl = txdr_unsigned(readsize);
2749 error = nfscl_request(nd, vp, p, cred, stuff);
2752 if (!(nd->nd_flag & ND_NFSV2)) {
2753 if (nd->nd_flag & ND_NFSV3)
2754 error = nfscl_postop_attr(nd, nap, attrflagp,
2756 if (!nd->nd_repstat && !error) {
2757 NFSM_DISSECT(tl, u_int32_t *, NFSX_HYPER);
2759 dnp->n_cookieverf.nfsuquad[0] = *tl++;
2760 dnp->n_cookieverf.nfsuquad[1] = *tl;
2764 if (nd->nd_repstat || error) {
2766 error = nd->nd_repstat;
2769 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2770 more_dirs = fxdr_unsigned(int, *tl);
2774 /* loop thru the dir entries, doctoring them to 4bsd form */
2775 while (more_dirs && bigenough) {
2776 if (nd->nd_flag & ND_NFSV4) {
2777 NFSM_DISSECT(tl, u_int32_t *, 3*NFSX_UNSIGNED);
2778 ncookie.lval[0] = *tl++;
2779 ncookie.lval[1] = *tl++;
2780 len = fxdr_unsigned(int, *tl);
2781 } else if (nd->nd_flag & ND_NFSV3) {
2782 NFSM_DISSECT(tl, u_int32_t *, 3*NFSX_UNSIGNED);
2783 nfsva.na_fileid = fxdr_hyper(tl);
2785 len = fxdr_unsigned(int, *tl);
2787 NFSM_DISSECT(tl, u_int32_t *, 2*NFSX_UNSIGNED);
2789 fxdr_unsigned(long, *tl++);
2790 len = fxdr_unsigned(int, *tl);
2792 if (len <= 0 || len > NFS_MAXNAMLEN) {
2796 tlen = NFSM_RNDUP(len);
2798 tlen += 4; /* To ensure null termination */
2799 left = DIRBLKSIZ - blksiz;
2800 if ((int)(tlen + DIRHDSIZ + NFSX_HYPER) > left) {
2801 dp->d_reclen += left;
2802 uio_iov_base_add(uiop, left);
2803 uio_iov_len_add(uiop, -(left));
2804 uio_uio_resid_add(uiop, -(left));
2805 uiop->uio_offset += left;
2808 if ((int)(tlen + DIRHDSIZ + NFSX_HYPER) > uio_uio_resid(uiop))
2811 dp = (struct dirent *) CAST_DOWN(caddr_t, uio_iov_base(uiop));
2813 dp->d_reclen = tlen + DIRHDSIZ + NFSX_HYPER;
2814 dp->d_type = DT_UNKNOWN;
2815 blksiz += dp->d_reclen;
2816 if (blksiz == DIRBLKSIZ)
2818 uio_uio_resid_add(uiop, -(DIRHDSIZ));
2819 uiop->uio_offset += DIRHDSIZ;
2820 uio_iov_base_add(uiop, DIRHDSIZ);
2821 uio_iov_len_add(uiop, -(DIRHDSIZ));
2822 error = nfsm_mbufuio(nd, uiop, len);
2825 cp = CAST_DOWN(caddr_t, uio_iov_base(uiop));
2827 *cp = '\0'; /* null terminate */
2828 cp += tlen; /* points to cookie storage */
2829 tl2 = (u_int32_t *)cp;
2830 uio_iov_base_add(uiop, (tlen + NFSX_HYPER));
2831 uio_iov_len_add(uiop, -(tlen + NFSX_HYPER));
2832 uio_uio_resid_add(uiop, -(tlen + NFSX_HYPER));
2833 uiop->uio_offset += (tlen + NFSX_HYPER);
2835 error = nfsm_advance(nd, NFSM_RNDUP(len), -1);
2839 if (nd->nd_flag & ND_NFSV4) {
2841 nfsva.na_mntonfileno = 0xffffffff;
2842 error = nfsv4_loadattr(nd, NULL, &nfsva, NULL,
2843 NULL, 0, NULL, NULL, NULL, NULL, NULL, 0,
2844 NULL, NULL, &rderr, p, cred);
2847 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2848 } else if (nd->nd_flag & ND_NFSV3) {
2849 NFSM_DISSECT(tl, u_int32_t *, 3*NFSX_UNSIGNED);
2850 ncookie.lval[0] = *tl++;
2851 ncookie.lval[1] = *tl++;
2853 NFSM_DISSECT(tl, u_int32_t *, 2*NFSX_UNSIGNED);
2854 ncookie.lval[0] = 0;
2855 ncookie.lval[1] = *tl++;
2858 if (nd->nd_flag & ND_NFSV4) {
2863 if (nfsva.na_mntonfileno != 0xffffffff)
2864 dp->d_fileno = nfsva.na_mntonfileno;
2866 dp->d_fileno = nfsva.na_fileid;
2867 } else if (nfsva.na_filesid[0] ==
2868 dnp->n_vattr.na_filesid[0] &&
2869 nfsva.na_filesid[1] ==
2870 dnp->n_vattr.na_filesid[1]) {
2871 dp->d_fileno = nfsva.na_fileid;
2875 } while (fakefileno ==
2877 dp->d_fileno = fakefileno;
2879 dp->d_type = vtonfs_dtype(nfsva.na_type);
2882 dp->d_fileno = nfsva.na_fileid;
2884 *tl2++ = cookiep->nfsuquad[0] = cookie.lval[0] =
2886 *tl2 = cookiep->nfsuquad[1] = cookie.lval[1] =
2889 more_dirs = fxdr_unsigned(int, *tl);
2892 * If at end of rpc data, get the eof boolean
2895 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2896 eof = fxdr_unsigned(int, *tl);
2899 if (nd->nd_flag & ND_NFSV4) {
2900 error = nfscl_postop_attr(nd, nap, attrflagp,
2906 mbuf_freem(nd->nd_mrep);
2910 * Fill last record, iff any, out to a multiple of DIRBLKSIZ
2911 * by increasing d_reclen for the last record.
2914 left = DIRBLKSIZ - blksiz;
2915 dp->d_reclen += left;
2916 uio_iov_base_add(uiop, left);
2917 uio_iov_len_add(uiop, -(left));
2918 uio_uio_resid_add(uiop, -(left));
2919 uiop->uio_offset += left;
2923 * If returning no data, assume end of file.
2924 * If not bigenough, return not end of file, since you aren't
2925 * returning all the data
2926 * Otherwise, return the eof flag from the server.
2929 if (tresid == ((size_t)(uio_uio_resid(uiop))))
2931 else if (!bigenough)
2938 * Add extra empty records to any remaining DIRBLKSIZ chunks.
2940 while (uio_uio_resid(uiop) > 0 && ((size_t)(uio_uio_resid(uiop))) != tresid) {
2941 dp = (struct dirent *) CAST_DOWN(caddr_t, uio_iov_base(uiop));
2942 dp->d_type = DT_UNKNOWN;
2945 dp->d_name[0] = '\0';
2946 tl = (u_int32_t *)&dp->d_name[4];
2947 *tl++ = cookie.lval[0];
2948 *tl = cookie.lval[1];
2949 dp->d_reclen = DIRBLKSIZ;
2950 uio_iov_base_add(uiop, DIRBLKSIZ);
2951 uio_iov_len_add(uiop, -(DIRBLKSIZ));
2952 uio_uio_resid_add(uiop, -(DIRBLKSIZ));
2953 uiop->uio_offset += DIRBLKSIZ;
2957 if (nd->nd_mrep != NULL)
2958 mbuf_freem(nd->nd_mrep);
2964 * NFS V3 readdir plus RPC. Used in place of nfsrpc_readdir().
2965 * (Also used for NFS V4 when mount flag set.)
2966 * (ditto above w.r.t. multiple of DIRBLKSIZ, etc.)
2969 nfsrpc_readdirplus(vnode_t vp, struct uio *uiop, nfsuint64 *cookiep,
2970 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp,
2971 int *eofp, void *stuff)
2974 struct dirent *dp = NULL;
2976 vnode_t newvp = NULLVP;
2977 struct nfsrv_descript nfsd, *nd = &nfsd;
2978 struct nameidata nami, *ndp = &nami;
2979 struct componentname *cnp = &ndp->ni_cnd;
2980 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
2981 struct nfsnode *dnp = VTONFS(vp), *np;
2982 struct nfsvattr nfsva;
2984 nfsquad_t cookie, ncookie;
2985 int error = 0, tlen, more_dirs = 1, blksiz = 0, bigenough = 1;
2986 int attrflag, tryformoredirs = 1, eof = 0, gotmnton = 0;
2987 int isdotdot = 0, unlocknewvp = 0;
2988 long dotfileid, dotdotfileid = 0, fileno = 0;
2990 nfsattrbit_t attrbits, dattrbits;
2992 u_int32_t *tl2 = NULL, fakefileno = 0xffffffff, rderr;
2993 struct timespec dctime;
2995 KASSERT(uiop->uio_iovcnt == 1 &&
2996 (uio_uio_resid(uiop) & (DIRBLKSIZ - 1)) == 0,
2997 ("nfs readdirplusrpc bad uio"));
2998 timespecclear(&dctime);
3004 cookie.lval[0] = cookiep->nfsuquad[0];
3005 cookie.lval[1] = cookiep->nfsuquad[1];
3006 tresid = uio_uio_resid(uiop);
3009 * For NFSv4, first create the "." and ".." entries.
3011 if (NFSHASNFSV4(nmp)) {
3012 NFSGETATTR_ATTRBIT(&dattrbits);
3013 NFSZERO_ATTRBIT(&attrbits);
3014 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_FILEID);
3015 if (NFSISSET_ATTRBIT(&dnp->n_vattr.na_suppattr,
3016 NFSATTRBIT_MOUNTEDONFILEID)) {
3017 NFSSETBIT_ATTRBIT(&attrbits,
3018 NFSATTRBIT_MOUNTEDONFILEID);
3022 * Must fake it. Use the fileno, except when the
3023 * fsid is != to that of the directory. For that
3024 * case, generate a fake fileno that is not the same.
3026 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_FSID);
3031 * Joy, oh joy. For V4 we get to hand craft '.' and '..'.
3033 if (uiop->uio_offset == 0) {
3034 NFSCL_REQSTART(nd, NFSPROC_LOOKUPP, vp);
3035 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
3036 *tl++ = txdr_unsigned(NFSV4OP_GETFH);
3037 *tl = txdr_unsigned(NFSV4OP_GETATTR);
3038 (void) nfsrv_putattrbit(nd, &attrbits);
3039 error = nfscl_request(nd, vp, p, cred, stuff);
3042 dotfileid = 0; /* Fake out the compiler. */
3043 if ((nd->nd_flag & ND_NOMOREDATA) == 0) {
3044 error = nfsm_loadattr(nd, &nfsva);
3047 dctime = nfsva.na_ctime;
3048 dotfileid = nfsva.na_fileid;
3050 if (nd->nd_repstat == 0) {
3051 NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
3052 len = fxdr_unsigned(int, *(tl + 4));
3053 if (len > 0 && len <= NFSX_V4FHMAX)
3054 error = nfsm_advance(nd, NFSM_RNDUP(len), -1);
3058 NFSM_DISSECT(tl, u_int32_t *, 2*NFSX_UNSIGNED);
3059 nfsva.na_mntonfileno = 0xffffffff;
3060 error = nfsv4_loadattr(nd, NULL, &nfsva, NULL,
3061 NULL, 0, NULL, NULL, NULL, NULL, NULL, 0,
3062 NULL, NULL, NULL, p, cred);
3064 dotdotfileid = dotfileid;
3065 } else if (gotmnton) {
3066 if (nfsva.na_mntonfileno != 0xffffffff)
3067 dotdotfileid = nfsva.na_mntonfileno;
3069 dotdotfileid = nfsva.na_fileid;
3070 } else if (nfsva.na_filesid[0] ==
3071 dnp->n_vattr.na_filesid[0] &&
3072 nfsva.na_filesid[1] ==
3073 dnp->n_vattr.na_filesid[1]) {
3074 dotdotfileid = nfsva.na_fileid;
3078 } while (fakefileno ==
3080 dotdotfileid = fakefileno;
3083 } else if (nd->nd_repstat == NFSERR_NOENT) {
3085 * Lookupp returns NFSERR_NOENT when we are
3086 * at the root, so just use the current dir.
3089 dotdotfileid = dotfileid;
3091 error = nd->nd_repstat;
3093 mbuf_freem(nd->nd_mrep);
3097 dp = (struct dirent *)uio_iov_base(uiop);
3098 dp->d_type = DT_DIR;
3099 dp->d_fileno = dotfileid;
3101 dp->d_name[0] = '.';
3102 dp->d_name[1] = '\0';
3103 dp->d_reclen = DIRENT_SIZE(dp) + NFSX_HYPER;
3105 * Just make these offset cookie 0.
3107 tl = (u_int32_t *)&dp->d_name[4];
3110 blksiz += dp->d_reclen;
3111 uio_uio_resid_add(uiop, -(dp->d_reclen));
3112 uiop->uio_offset += dp->d_reclen;
3113 uio_iov_base_add(uiop, dp->d_reclen);
3114 uio_iov_len_add(uiop, -(dp->d_reclen));
3115 dp = (struct dirent *)uio_iov_base(uiop);
3116 dp->d_type = DT_DIR;
3117 dp->d_fileno = dotdotfileid;
3119 dp->d_name[0] = '.';
3120 dp->d_name[1] = '.';
3121 dp->d_name[2] = '\0';
3122 dp->d_reclen = DIRENT_SIZE(dp) + NFSX_HYPER;
3124 * Just make these offset cookie 0.
3126 tl = (u_int32_t *)&dp->d_name[4];
3129 blksiz += dp->d_reclen;
3130 uio_uio_resid_add(uiop, -(dp->d_reclen));
3131 uiop->uio_offset += dp->d_reclen;
3132 uio_iov_base_add(uiop, dp->d_reclen);
3133 uio_iov_len_add(uiop, -(dp->d_reclen));
3135 NFSREADDIRPLUS_ATTRBIT(&attrbits);
3137 NFSSETBIT_ATTRBIT(&attrbits,
3138 NFSATTRBIT_MOUNTEDONFILEID);
3142 * Loop around doing readdir rpc's of size nm_readdirsize.
3143 * The stopping criteria is EOF or buffer full.
3145 while (more_dirs && bigenough) {
3147 NFSCL_REQSTART(nd, NFSPROC_READDIRPLUS, vp);
3148 NFSM_BUILD(tl, u_int32_t *, 6 * NFSX_UNSIGNED);
3149 *tl++ = cookie.lval[0];
3150 *tl++ = cookie.lval[1];
3151 if (cookie.qval == 0) {
3156 *tl++ = dnp->n_cookieverf.nfsuquad[0];
3157 *tl++ = dnp->n_cookieverf.nfsuquad[1];
3160 *tl++ = txdr_unsigned(nmp->nm_readdirsize);
3161 *tl = txdr_unsigned(nmp->nm_readdirsize);
3162 if (nd->nd_flag & ND_NFSV4) {
3163 (void) nfsrv_putattrbit(nd, &attrbits);
3164 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
3165 *tl = txdr_unsigned(NFSV4OP_GETATTR);
3166 (void) nfsrv_putattrbit(nd, &dattrbits);
3168 error = nfscl_request(nd, vp, p, cred, stuff);
3171 if (nd->nd_flag & ND_NFSV3)
3172 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
3173 if (nd->nd_repstat || error) {
3175 error = nd->nd_repstat;
3178 if ((nd->nd_flag & ND_NFSV3) != 0 && *attrflagp != 0)
3179 dctime = nap->na_ctime;
3180 NFSM_DISSECT(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
3182 dnp->n_cookieverf.nfsuquad[0] = *tl++;
3183 dnp->n_cookieverf.nfsuquad[1] = *tl++;
3185 more_dirs = fxdr_unsigned(int, *tl);
3189 /* loop thru the dir entries, doctoring them to 4bsd form */
3190 while (more_dirs && bigenough) {
3191 NFSM_DISSECT(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
3192 if (nd->nd_flag & ND_NFSV4) {
3193 ncookie.lval[0] = *tl++;
3194 ncookie.lval[1] = *tl++;
3196 fileno = fxdr_unsigned(long, *++tl);
3199 len = fxdr_unsigned(int, *tl);
3200 if (len <= 0 || len > NFS_MAXNAMLEN) {
3204 tlen = NFSM_RNDUP(len);
3206 tlen += 4; /* To ensure null termination */
3207 left = DIRBLKSIZ - blksiz;
3208 if ((tlen + DIRHDSIZ + NFSX_HYPER) > left) {
3209 dp->d_reclen += left;
3210 uio_iov_base_add(uiop, left);
3211 uio_iov_len_add(uiop, -(left));
3212 uio_uio_resid_add(uiop, -(left));
3213 uiop->uio_offset += left;
3216 if ((tlen + DIRHDSIZ + NFSX_HYPER) > uio_uio_resid(uiop))
3219 dp = (struct dirent *)uio_iov_base(uiop);
3221 dp->d_reclen = tlen + DIRHDSIZ + NFSX_HYPER;
3222 dp->d_type = DT_UNKNOWN;
3223 blksiz += dp->d_reclen;
3224 if (blksiz == DIRBLKSIZ)
3226 uio_uio_resid_add(uiop, -(DIRHDSIZ));
3227 uiop->uio_offset += DIRHDSIZ;
3228 uio_iov_base_add(uiop, DIRHDSIZ);
3229 uio_iov_len_add(uiop, -(DIRHDSIZ));
3230 cnp->cn_nameptr = uio_iov_base(uiop);
3231 cnp->cn_namelen = len;
3233 error = nfsm_mbufuio(nd, uiop, len);
3236 cp = uio_iov_base(uiop);
3239 cp += tlen; /* points to cookie storage */
3240 tl2 = (u_int32_t *)cp;
3241 if (len == 2 && cnp->cn_nameptr[0] == '.' &&
3242 cnp->cn_nameptr[1] == '.')
3246 uio_iov_base_add(uiop, (tlen + NFSX_HYPER));
3247 uio_iov_len_add(uiop, -(tlen + NFSX_HYPER));
3248 uio_uio_resid_add(uiop, -(tlen + NFSX_HYPER));
3249 uiop->uio_offset += (tlen + NFSX_HYPER);
3251 error = nfsm_advance(nd, NFSM_RNDUP(len), -1);
3256 if (nd->nd_flag & ND_NFSV3) {
3257 NFSM_DISSECT(tl, u_int32_t *, 3*NFSX_UNSIGNED);
3258 ncookie.lval[0] = *tl++;
3259 ncookie.lval[1] = *tl++;
3260 attrflag = fxdr_unsigned(int, *tl);
3262 error = nfsm_loadattr(nd, &nfsva);
3266 NFSM_DISSECT(tl,u_int32_t *,NFSX_UNSIGNED);
3268 error = nfsm_getfh(nd, &nfhp);
3272 if (!attrflag && nfhp != NULL) {
3273 FREE((caddr_t)nfhp, M_NFSFH);
3278 nfsva.na_mntonfileno = 0xffffffff;
3279 error = nfsv4_loadattr(nd, NULL, &nfsva, &nfhp,
3280 NULL, 0, NULL, NULL, NULL, NULL, NULL, 0,
3281 NULL, NULL, &rderr, p, cred);
3287 if (nd->nd_flag & ND_NFSV4) {
3290 } else if (gotmnton) {
3291 if (nfsva.na_mntonfileno != 0xffffffff)
3292 dp->d_fileno = nfsva.na_mntonfileno;
3294 dp->d_fileno = nfsva.na_fileid;
3295 } else if (nfsva.na_filesid[0] ==
3296 dnp->n_vattr.na_filesid[0] &&
3297 nfsva.na_filesid[1] ==
3298 dnp->n_vattr.na_filesid[1]) {
3299 dp->d_fileno = nfsva.na_fileid;
3303 } while (fakefileno ==
3305 dp->d_fileno = fakefileno;
3308 dp->d_fileno = fileno;
3310 *tl2++ = cookiep->nfsuquad[0] = cookie.lval[0] =
3312 *tl2 = cookiep->nfsuquad[1] = cookie.lval[1] =
3316 if (NFSRV_CMPFH(nfhp->nfh_fh, nfhp->nfh_len,
3317 dnp->n_fhp->nfh_fh, dnp->n_fhp->nfh_len)) {
3321 FREE((caddr_t)nfhp, M_NFSFH);
3323 } else if (isdotdot != 0) {
3325 * Skip doing a nfscl_nget() call for "..".
3326 * There's a race between acquiring the nfs
3327 * node here and lookups that look for the
3328 * directory being read (in the parent).
3329 * It would try to get a lock on ".." here,
3330 * owning the lock on the directory being
3331 * read. Lookup will hold the lock on ".."
3332 * and try to acquire the lock on the
3333 * directory being read.
3334 * If the directory is unlocked/relocked,
3335 * then there is a LOR with the buflock
3338 free(nfhp, M_NFSFH);
3340 error = nfscl_nget(vnode_mount(vp), vp,
3341 nfhp, cnp, p, &np, NULL, LK_EXCLUSIVE);
3348 if (newvp != NULLVP) {
3349 error = nfscl_loadattrcache(&newvp,
3350 &nfsva, NULL, NULL, 0, 0);
3359 vtonfs_dtype(np->n_vattr.na_type);
3361 NFSCNHASH(cnp, HASHINIT);
3362 if (cnp->cn_namelen <= NCHNAMLEN &&
3363 (newvp->v_type != VDIR ||
3364 dctime.tv_sec != 0)) {
3365 cache_enter_time(ndp->ni_dvp,
3368 newvp->v_type != VDIR ? NULL :
3378 } else if (nfhp != NULL) {
3379 FREE((caddr_t)nfhp, M_NFSFH);
3381 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3382 more_dirs = fxdr_unsigned(int, *tl);
3385 * If at end of rpc data, get the eof boolean
3388 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3389 eof = fxdr_unsigned(int, *tl);
3392 if (nd->nd_flag & ND_NFSV4) {
3393 error = nfscl_postop_attr(nd, nap, attrflagp,
3399 mbuf_freem(nd->nd_mrep);
3403 * Fill last record, iff any, out to a multiple of DIRBLKSIZ
3404 * by increasing d_reclen for the last record.
3407 left = DIRBLKSIZ - blksiz;
3408 dp->d_reclen += left;
3409 uio_iov_base_add(uiop, left);
3410 uio_iov_len_add(uiop, -(left));
3411 uio_uio_resid_add(uiop, -(left));
3412 uiop->uio_offset += left;
3416 * If returning no data, assume end of file.
3417 * If not bigenough, return not end of file, since you aren't
3418 * returning all the data
3419 * Otherwise, return the eof flag from the server.
3422 if (tresid == uio_uio_resid(uiop))
3424 else if (!bigenough)
3431 * Add extra empty records to any remaining DIRBLKSIZ chunks.
3433 while (uio_uio_resid(uiop) > 0 && uio_uio_resid(uiop) != tresid) {
3434 dp = (struct dirent *)uio_iov_base(uiop);
3435 dp->d_type = DT_UNKNOWN;
3438 dp->d_name[0] = '\0';
3439 tl = (u_int32_t *)&dp->d_name[4];
3440 *tl++ = cookie.lval[0];
3441 *tl = cookie.lval[1];
3442 dp->d_reclen = DIRBLKSIZ;
3443 uio_iov_base_add(uiop, DIRBLKSIZ);
3444 uio_iov_len_add(uiop, -(DIRBLKSIZ));
3445 uio_uio_resid_add(uiop, -(DIRBLKSIZ));
3446 uiop->uio_offset += DIRBLKSIZ;
3450 if (nd->nd_mrep != NULL)
3451 mbuf_freem(nd->nd_mrep);
3460 nfsrpc_commit(vnode_t vp, u_quad_t offset, int cnt, struct ucred *cred,
3461 NFSPROC_T *p, u_char *verfp, struct nfsvattr *nap, int *attrflagp,
3465 struct nfsrv_descript nfsd, *nd = &nfsd;
3466 nfsattrbit_t attrbits;
3470 NFSCL_REQSTART(nd, NFSPROC_COMMIT, vp);
3471 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
3472 txdr_hyper(offset, tl);
3474 *tl = txdr_unsigned(cnt);
3475 if (nd->nd_flag & ND_NFSV4) {
3477 * And do a Getattr op.
3479 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
3480 *tl = txdr_unsigned(NFSV4OP_GETATTR);
3481 NFSGETATTR_ATTRBIT(&attrbits);
3482 (void) nfsrv_putattrbit(nd, &attrbits);
3484 error = nfscl_request(nd, vp, p, cred, stuff);
3487 error = nfscl_wcc_data(nd, vp, nap, attrflagp, NULL, stuff);
3488 if (!error && !nd->nd_repstat) {
3489 NFSM_DISSECT(tl, u_int32_t *, NFSX_VERF);
3490 NFSBCOPY((caddr_t)tl, verfp, NFSX_VERF);
3491 if (nd->nd_flag & ND_NFSV4)
3492 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
3495 if (!error && nd->nd_repstat)
3496 error = nd->nd_repstat;
3497 mbuf_freem(nd->nd_mrep);
3502 * NFS byte range lock rpc.
3503 * (Mostly just calls one of the three lower level RPC routines.)
3506 nfsrpc_advlock(vnode_t vp, off_t size, int op, struct flock *fl,
3507 int reclaim, struct ucred *cred, NFSPROC_T *p, void *id, int flags)
3509 struct nfscllockowner *lp;
3510 struct nfsclclient *clp;
3512 struct nfsrv_descript nfsd, *nd = &nfsd;
3513 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
3516 u_int32_t clidrev = 0;
3517 int error = 0, newone = 0, expireret = 0, retrycnt, donelocally;
3521 * Convert the flock structure into a start and end and do POSIX
3524 switch (fl->l_whence) {
3528 * Caller is responsible for adding any necessary offset
3529 * when SEEK_CUR is used.
3531 start = fl->l_start;
3535 start = size + fl->l_start;
3536 off = size + fl->l_start;
3543 if (fl->l_len != 0) {
3544 end = start + fl->l_len - 1;
3555 if (op == F_GETLK) {
3556 error = nfscl_getcl(vp, cred, p, &clp);
3559 error = nfscl_lockt(vp, clp, off, len, fl, p, id, flags);
3561 clidrev = clp->nfsc_clientidrev;
3562 error = nfsrpc_lockt(nd, vp, clp, off, len, fl, cred,
3564 } else if (error == -1) {
3567 nfscl_clientrelease(clp);
3568 } else if (op == F_UNLCK && fl->l_type == F_UNLCK) {
3570 * We must loop around for all lockowner cases.
3573 error = nfscl_getcl(vp, cred, p, &clp);
3577 error = nfscl_relbytelock(vp, off, len, cred, p, callcnt,
3578 clp, id, flags, &lp, &dorpc);
3580 * If it returns a NULL lp, we're done.
3584 nfscl_clientrelease(clp);
3586 nfscl_releasealllocks(clp, vp, p, id, flags);
3589 if (nmp->nm_clp != NULL)
3590 clidrev = nmp->nm_clp->nfsc_clientidrev;
3594 * If the server doesn't support Posix lock semantics,
3595 * only allow locks on the entire file, since it won't
3596 * handle overlapping byte ranges.
3597 * There might still be a problem when a lock
3598 * upgrade/downgrade (read<->write) occurs, since the
3599 * server "might" expect an unlock first?
3601 if (dorpc && (lp->nfsl_open->nfso_posixlock ||
3602 (off == 0 && len == NFS64BITSSET))) {
3604 * Since the lock records will go away, we must
3605 * wait for grace and delay here.
3608 error = nfsrpc_locku(nd, nmp, lp, off, len,
3609 NFSV4LOCKT_READ, cred, p, 0);
3610 if ((nd->nd_repstat == NFSERR_GRACE ||
3611 nd->nd_repstat == NFSERR_DELAY) &&
3613 (void) nfs_catnap(PZERO, (int)nd->nd_repstat,
3615 } while ((nd->nd_repstat == NFSERR_GRACE ||
3616 nd->nd_repstat == NFSERR_DELAY) && error == 0);
3619 } while (error == 0 && nd->nd_repstat == 0);
3620 nfscl_releasealllocks(clp, vp, p, id, flags);
3621 } else if (op == F_SETLK) {
3622 error = nfscl_getbytelock(vp, off, len, fl->l_type, cred, p,
3623 NULL, 0, id, flags, NULL, NULL, &lp, &newone, &donelocally);
3624 if (error || donelocally) {
3627 if (nmp->nm_clp != NULL)
3628 clidrev = nmp->nm_clp->nfsc_clientidrev;
3631 nfhp = VTONFS(vp)->n_fhp;
3632 if (!lp->nfsl_open->nfso_posixlock &&
3633 (off != 0 || len != NFS64BITSSET)) {
3636 error = nfsrpc_lock(nd, nmp, vp, nfhp->nfh_fh,
3637 nfhp->nfh_len, lp, newone, reclaim, off,
3638 len, fl->l_type, cred, p, 0);
3641 error = nd->nd_repstat;
3642 nfscl_lockrelease(lp, error, newone);
3647 error = nd->nd_repstat;
3648 if (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
3649 error == NFSERR_STALEDONTRECOVER ||
3650 error == NFSERR_STALECLIENTID || error == NFSERR_DELAY) {
3651 (void) nfs_catnap(PZERO, error, "nfs_advlock");
3652 } else if ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID)
3654 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
3657 } while (error == NFSERR_GRACE ||
3658 error == NFSERR_STALECLIENTID || error == NFSERR_DELAY ||
3659 error == NFSERR_STALEDONTRECOVER || error == NFSERR_STALESTATEID ||
3660 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
3661 expireret == 0 && clidrev != 0 && retrycnt < 4));
3662 if (error && retrycnt >= 4)
3668 * The lower level routine for the LockT case.
3671 nfsrpc_lockt(struct nfsrv_descript *nd, vnode_t vp,
3672 struct nfsclclient *clp, u_int64_t off, u_int64_t len, struct flock *fl,
3673 struct ucred *cred, NFSPROC_T *p, void *id, int flags)
3676 int error, type, size;
3677 uint8_t own[NFSV4CL_LOCKNAMELEN + NFSX_V4FHMAX];
3680 NFSCL_REQSTART(nd, NFSPROC_LOCKT, vp);
3681 NFSM_BUILD(tl, u_int32_t *, 7 * NFSX_UNSIGNED);
3682 if (fl->l_type == F_RDLCK)
3683 *tl++ = txdr_unsigned(NFSV4LOCKT_READ);
3685 *tl++ = txdr_unsigned(NFSV4LOCKT_WRITE);
3686 txdr_hyper(off, tl);
3688 txdr_hyper(len, tl);
3690 *tl++ = clp->nfsc_clientid.lval[0];
3691 *tl = clp->nfsc_clientid.lval[1];
3692 nfscl_filllockowner(id, own, flags);
3694 NFSBCOPY(np->n_fhp->nfh_fh, &own[NFSV4CL_LOCKNAMELEN],
3695 np->n_fhp->nfh_len);
3696 (void)nfsm_strtom(nd, own, NFSV4CL_LOCKNAMELEN + np->n_fhp->nfh_len);
3697 error = nfscl_request(nd, vp, p, cred, NULL);
3700 if (nd->nd_repstat == 0) {
3701 fl->l_type = F_UNLCK;
3702 } else if (nd->nd_repstat == NFSERR_DENIED) {
3704 fl->l_whence = SEEK_SET;
3705 NFSM_DISSECT(tl, u_int32_t *, 8 * NFSX_UNSIGNED);
3706 fl->l_start = fxdr_hyper(tl);
3708 len = fxdr_hyper(tl);
3710 if (len == NFS64BITSSET)
3714 type = fxdr_unsigned(int, *tl++);
3715 if (type == NFSV4LOCKT_WRITE)
3716 fl->l_type = F_WRLCK;
3718 fl->l_type = F_RDLCK;
3720 * XXX For now, I have no idea what to do with the
3721 * conflicting lock_owner, so I'll just set the pid == 0
3722 * and skip over the lock_owner.
3724 fl->l_pid = (pid_t)0;
3726 size = fxdr_unsigned(int, *tl);
3727 if (size < 0 || size > NFSV4_OPAQUELIMIT)
3730 error = nfsm_advance(nd, NFSM_RNDUP(size), -1);
3731 } else if (nd->nd_repstat == NFSERR_STALECLIENTID)
3732 nfscl_initiate_recovery(clp);
3734 mbuf_freem(nd->nd_mrep);
3739 * Lower level function that performs the LockU RPC.
3742 nfsrpc_locku(struct nfsrv_descript *nd, struct nfsmount *nmp,
3743 struct nfscllockowner *lp, u_int64_t off, u_int64_t len,
3744 u_int32_t type, struct ucred *cred, NFSPROC_T *p, int syscred)
3749 nfscl_reqstart(nd, NFSPROC_LOCKU, nmp, lp->nfsl_open->nfso_fh,
3750 lp->nfsl_open->nfso_fhlen, NULL);
3751 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID + 6 * NFSX_UNSIGNED);
3752 *tl++ = txdr_unsigned(type);
3753 *tl = txdr_unsigned(lp->nfsl_seqid);
3754 if (nfstest_outofseq &&
3755 (arc4random() % nfstest_outofseq) == 0)
3756 *tl = txdr_unsigned(lp->nfsl_seqid + 1);
3758 *tl++ = lp->nfsl_stateid.seqid;
3759 *tl++ = lp->nfsl_stateid.other[0];
3760 *tl++ = lp->nfsl_stateid.other[1];
3761 *tl++ = lp->nfsl_stateid.other[2];
3762 txdr_hyper(off, tl);
3764 txdr_hyper(len, tl);
3766 nd->nd_flag |= ND_USEGSSNAME;
3767 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
3768 NFS_PROG, NFS_VER4, NULL, 1, NULL);
3769 NFSCL_INCRSEQID(lp->nfsl_seqid, nd);
3772 if (nd->nd_repstat == 0) {
3773 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID);
3774 lp->nfsl_stateid.seqid = *tl++;
3775 lp->nfsl_stateid.other[0] = *tl++;
3776 lp->nfsl_stateid.other[1] = *tl++;
3777 lp->nfsl_stateid.other[2] = *tl;
3778 } else if (nd->nd_repstat == NFSERR_STALESTATEID)
3779 nfscl_initiate_recovery(lp->nfsl_open->nfso_own->nfsow_clp);
3781 mbuf_freem(nd->nd_mrep);
3786 * The actual Lock RPC.
3789 nfsrpc_lock(struct nfsrv_descript *nd, struct nfsmount *nmp, vnode_t vp,
3790 u_int8_t *nfhp, int fhlen, struct nfscllockowner *lp, int newone,
3791 int reclaim, u_int64_t off, u_int64_t len, short type, struct ucred *cred,
3792 NFSPROC_T *p, int syscred)
3796 uint8_t own[NFSV4CL_LOCKNAMELEN + NFSX_V4FHMAX];
3798 nfscl_reqstart(nd, NFSPROC_LOCK, nmp, nfhp, fhlen, NULL);
3799 NFSM_BUILD(tl, u_int32_t *, 7 * NFSX_UNSIGNED);
3800 if (type == F_RDLCK)
3801 *tl++ = txdr_unsigned(NFSV4LOCKT_READ);
3803 *tl++ = txdr_unsigned(NFSV4LOCKT_WRITE);
3804 *tl++ = txdr_unsigned(reclaim);
3805 txdr_hyper(off, tl);
3807 txdr_hyper(len, tl);
3811 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID +
3812 2 * NFSX_UNSIGNED + NFSX_HYPER);
3813 *tl++ = txdr_unsigned(lp->nfsl_open->nfso_own->nfsow_seqid);
3814 *tl++ = lp->nfsl_open->nfso_stateid.seqid;
3815 *tl++ = lp->nfsl_open->nfso_stateid.other[0];
3816 *tl++ = lp->nfsl_open->nfso_stateid.other[1];
3817 *tl++ = lp->nfsl_open->nfso_stateid.other[2];
3818 *tl++ = txdr_unsigned(lp->nfsl_seqid);
3819 *tl++ = lp->nfsl_open->nfso_own->nfsow_clp->nfsc_clientid.lval[0];
3820 *tl = lp->nfsl_open->nfso_own->nfsow_clp->nfsc_clientid.lval[1];
3821 NFSBCOPY(lp->nfsl_owner, own, NFSV4CL_LOCKNAMELEN);
3822 NFSBCOPY(nfhp, &own[NFSV4CL_LOCKNAMELEN], fhlen);
3823 (void)nfsm_strtom(nd, own, NFSV4CL_LOCKNAMELEN + fhlen);
3826 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID + NFSX_UNSIGNED);
3827 *tl++ = lp->nfsl_stateid.seqid;
3828 *tl++ = lp->nfsl_stateid.other[0];
3829 *tl++ = lp->nfsl_stateid.other[1];
3830 *tl++ = lp->nfsl_stateid.other[2];
3831 *tl = txdr_unsigned(lp->nfsl_seqid);
3832 if (nfstest_outofseq &&
3833 (arc4random() % nfstest_outofseq) == 0)
3834 *tl = txdr_unsigned(lp->nfsl_seqid + 1);
3837 nd->nd_flag |= ND_USEGSSNAME;
3838 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, vp, p, cred,
3839 NFS_PROG, NFS_VER4, NULL, 1, NULL);
3843 NFSCL_INCRSEQID(lp->nfsl_open->nfso_own->nfsow_seqid, nd);
3844 NFSCL_INCRSEQID(lp->nfsl_seqid, nd);
3845 if (nd->nd_repstat == 0) {
3846 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID);
3847 lp->nfsl_stateid.seqid = *tl++;
3848 lp->nfsl_stateid.other[0] = *tl++;
3849 lp->nfsl_stateid.other[1] = *tl++;
3850 lp->nfsl_stateid.other[2] = *tl;
3851 } else if (nd->nd_repstat == NFSERR_DENIED) {
3852 NFSM_DISSECT(tl, u_int32_t *, 8 * NFSX_UNSIGNED);
3853 size = fxdr_unsigned(int, *(tl + 7));
3854 if (size < 0 || size > NFSV4_OPAQUELIMIT)
3857 error = nfsm_advance(nd, NFSM_RNDUP(size), -1);
3858 } else if (nd->nd_repstat == NFSERR_STALESTATEID)
3859 nfscl_initiate_recovery(lp->nfsl_open->nfso_own->nfsow_clp);
3861 mbuf_freem(nd->nd_mrep);
3867 * (always called with the vp for the mount point)
3870 nfsrpc_statfs(vnode_t vp, struct nfsstatfs *sbp, struct nfsfsinfo *fsp,
3871 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp,
3874 u_int32_t *tl = NULL;
3875 struct nfsrv_descript nfsd, *nd = &nfsd;
3876 struct nfsmount *nmp;
3877 nfsattrbit_t attrbits;
3881 nmp = VFSTONFS(vnode_mount(vp));
3882 if (NFSHASNFSV4(nmp)) {
3884 * For V4, you actually do a getattr.
3886 NFSCL_REQSTART(nd, NFSPROC_GETATTR, vp);
3887 NFSSTATFS_GETATTRBIT(&attrbits);
3888 (void) nfsrv_putattrbit(nd, &attrbits);
3889 nd->nd_flag |= ND_USEGSSNAME;
3890 error = nfscl_request(nd, vp, p, cred, stuff);
3893 if (nd->nd_repstat == 0) {
3894 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0,
3895 NULL, NULL, sbp, fsp, NULL, 0, NULL, NULL, NULL, p,
3898 nmp->nm_fsid[0] = nap->na_filesid[0];
3899 nmp->nm_fsid[1] = nap->na_filesid[1];
3900 NFSSETHASSETFSID(nmp);
3904 error = nd->nd_repstat;
3909 NFSCL_REQSTART(nd, NFSPROC_FSSTAT, vp);
3910 error = nfscl_request(nd, vp, p, cred, stuff);
3913 if (nd->nd_flag & ND_NFSV3) {
3914 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
3918 if (nd->nd_repstat) {
3919 error = nd->nd_repstat;
3922 NFSM_DISSECT(tl, u_int32_t *,
3923 NFSX_STATFS(nd->nd_flag & ND_NFSV3));
3925 if (NFSHASNFSV3(nmp)) {
3926 sbp->sf_tbytes = fxdr_hyper(tl); tl += 2;
3927 sbp->sf_fbytes = fxdr_hyper(tl); tl += 2;
3928 sbp->sf_abytes = fxdr_hyper(tl); tl += 2;
3929 sbp->sf_tfiles = fxdr_hyper(tl); tl += 2;
3930 sbp->sf_ffiles = fxdr_hyper(tl); tl += 2;
3931 sbp->sf_afiles = fxdr_hyper(tl); tl += 2;
3932 sbp->sf_invarsec = fxdr_unsigned(u_int32_t, *tl);
3933 } else if (NFSHASNFSV4(nmp) == 0) {
3934 sbp->sf_tsize = fxdr_unsigned(u_int32_t, *tl++);
3935 sbp->sf_bsize = fxdr_unsigned(u_int32_t, *tl++);
3936 sbp->sf_blocks = fxdr_unsigned(u_int32_t, *tl++);
3937 sbp->sf_bfree = fxdr_unsigned(u_int32_t, *tl++);
3938 sbp->sf_bavail = fxdr_unsigned(u_int32_t, *tl);
3941 mbuf_freem(nd->nd_mrep);
3949 nfsrpc_pathconf(vnode_t vp, struct nfsv3_pathconf *pc,
3950 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp,
3953 struct nfsrv_descript nfsd, *nd = &nfsd;
3954 struct nfsmount *nmp;
3956 nfsattrbit_t attrbits;
3960 nmp = VFSTONFS(vnode_mount(vp));
3961 if (NFSHASNFSV4(nmp)) {
3963 * For V4, you actually do a getattr.
3965 NFSCL_REQSTART(nd, NFSPROC_GETATTR, vp);
3966 NFSPATHCONF_GETATTRBIT(&attrbits);
3967 (void) nfsrv_putattrbit(nd, &attrbits);
3968 nd->nd_flag |= ND_USEGSSNAME;
3969 error = nfscl_request(nd, vp, p, cred, stuff);
3972 if (nd->nd_repstat == 0) {
3973 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0,
3974 pc, NULL, NULL, NULL, NULL, 0, NULL, NULL, NULL, p,
3979 error = nd->nd_repstat;
3982 NFSCL_REQSTART(nd, NFSPROC_PATHCONF, vp);
3983 error = nfscl_request(nd, vp, p, cred, stuff);
3986 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
3987 if (nd->nd_repstat && !error)
3988 error = nd->nd_repstat;
3990 NFSM_DISSECT(tl, u_int32_t *, NFSX_V3PATHCONF);
3991 pc->pc_linkmax = fxdr_unsigned(u_int32_t, *tl++);
3992 pc->pc_namemax = fxdr_unsigned(u_int32_t, *tl++);
3993 pc->pc_notrunc = fxdr_unsigned(u_int32_t, *tl++);
3994 pc->pc_chownrestricted =
3995 fxdr_unsigned(u_int32_t, *tl++);
3996 pc->pc_caseinsensitive =
3997 fxdr_unsigned(u_int32_t, *tl++);
3998 pc->pc_casepreserving = fxdr_unsigned(u_int32_t, *tl);
4002 mbuf_freem(nd->nd_mrep);
4007 * nfs version 3 fsinfo rpc call
4010 nfsrpc_fsinfo(vnode_t vp, struct nfsfsinfo *fsp, struct ucred *cred,
4011 NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, void *stuff)
4014 struct nfsrv_descript nfsd, *nd = &nfsd;
4018 NFSCL_REQSTART(nd, NFSPROC_FSINFO, vp);
4019 error = nfscl_request(nd, vp, p, cred, stuff);
4022 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
4023 if (nd->nd_repstat && !error)
4024 error = nd->nd_repstat;
4026 NFSM_DISSECT(tl, u_int32_t *, NFSX_V3FSINFO);
4027 fsp->fs_rtmax = fxdr_unsigned(u_int32_t, *tl++);
4028 fsp->fs_rtpref = fxdr_unsigned(u_int32_t, *tl++);
4029 fsp->fs_rtmult = fxdr_unsigned(u_int32_t, *tl++);
4030 fsp->fs_wtmax = fxdr_unsigned(u_int32_t, *tl++);
4031 fsp->fs_wtpref = fxdr_unsigned(u_int32_t, *tl++);
4032 fsp->fs_wtmult = fxdr_unsigned(u_int32_t, *tl++);
4033 fsp->fs_dtpref = fxdr_unsigned(u_int32_t, *tl++);
4034 fsp->fs_maxfilesize = fxdr_hyper(tl);
4036 fxdr_nfsv3time(tl, &fsp->fs_timedelta);
4038 fsp->fs_properties = fxdr_unsigned(u_int32_t, *tl);
4041 mbuf_freem(nd->nd_mrep);
4046 * This function performs the Renew RPC.
4049 nfsrpc_renew(struct nfsclclient *clp, struct ucred *cred, NFSPROC_T *p)
4052 struct nfsrv_descript nfsd;
4053 struct nfsrv_descript *nd = &nfsd;
4054 struct nfsmount *nmp;
4057 nmp = clp->nfsc_nmp;
4060 nfscl_reqstart(nd, NFSPROC_RENEW, nmp, NULL, 0, NULL);
4061 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
4062 *tl++ = clp->nfsc_clientid.lval[0];
4063 *tl = clp->nfsc_clientid.lval[1];
4064 nd->nd_flag |= ND_USEGSSNAME;
4065 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4066 NFS_PROG, NFS_VER4, NULL, 1, NULL);
4069 error = nd->nd_repstat;
4070 mbuf_freem(nd->nd_mrep);
4075 * This function performs the Releaselockowner RPC.
4078 nfsrpc_rellockown(struct nfsmount *nmp, struct nfscllockowner *lp,
4079 uint8_t *fh, int fhlen, struct ucred *cred, NFSPROC_T *p)
4081 struct nfsrv_descript nfsd, *nd = &nfsd;
4084 uint8_t own[NFSV4CL_LOCKNAMELEN + NFSX_V4FHMAX];
4086 nfscl_reqstart(nd, NFSPROC_RELEASELCKOWN, nmp, NULL, 0, NULL);
4087 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
4088 *tl++ = nmp->nm_clp->nfsc_clientid.lval[0];
4089 *tl = nmp->nm_clp->nfsc_clientid.lval[1];
4090 NFSBCOPY(lp->nfsl_owner, own, NFSV4CL_LOCKNAMELEN);
4091 NFSBCOPY(fh, &own[NFSV4CL_LOCKNAMELEN], fhlen);
4092 (void)nfsm_strtom(nd, own, NFSV4CL_LOCKNAMELEN + fhlen);
4093 nd->nd_flag |= ND_USEGSSNAME;
4094 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4095 NFS_PROG, NFS_VER4, NULL, 1, NULL);
4098 error = nd->nd_repstat;
4099 mbuf_freem(nd->nd_mrep);
4104 * This function performs the Compound to get the mount pt FH.
4107 nfsrpc_getdirpath(struct nfsmount *nmp, u_char *dirpath, struct ucred *cred,
4111 struct nfsrv_descript nfsd;
4112 struct nfsrv_descript *nd = &nfsd;
4114 int error, cnt, len, setnil;
4117 nfscl_reqstart(nd, NFSPROC_PUTROOTFH, nmp, NULL, 0, &opcntp);
4125 while (*cp2 != '\0' && *cp2 != '/')
4132 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
4133 *tl = txdr_unsigned(NFSV4OP_LOOKUP);
4134 nfsm_strtom(nd, cp, strlen(cp));
4140 } while (*cp != '\0');
4141 *opcntp = txdr_unsigned(2 + cnt);
4142 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
4143 *tl = txdr_unsigned(NFSV4OP_GETFH);
4144 nd->nd_flag |= ND_USEGSSNAME;
4145 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4146 NFS_PROG, NFS_VER4, NULL, 1, NULL);
4149 if (nd->nd_repstat == 0) {
4150 NFSM_DISSECT(tl, u_int32_t *, (3 + 2 * cnt) * NFSX_UNSIGNED);
4151 tl += (2 + 2 * cnt);
4152 if ((len = fxdr_unsigned(int, *tl)) <= 0 ||
4154 nd->nd_repstat = NFSERR_BADXDR;
4156 nd->nd_repstat = nfsrv_mtostr(nd, nmp->nm_fh, len);
4157 if (nd->nd_repstat == 0)
4158 nmp->nm_fhsize = len;
4161 error = nd->nd_repstat;
4163 mbuf_freem(nd->nd_mrep);
4168 * This function performs the Delegreturn RPC.
4171 nfsrpc_delegreturn(struct nfscldeleg *dp, struct ucred *cred,
4172 struct nfsmount *nmp, NFSPROC_T *p, int syscred)
4175 struct nfsrv_descript nfsd;
4176 struct nfsrv_descript *nd = &nfsd;
4179 nfscl_reqstart(nd, NFSPROC_DELEGRETURN, nmp, dp->nfsdl_fh,
4180 dp->nfsdl_fhlen, NULL);
4181 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID);
4182 *tl++ = dp->nfsdl_stateid.seqid;
4183 *tl++ = dp->nfsdl_stateid.other[0];
4184 *tl++ = dp->nfsdl_stateid.other[1];
4185 *tl = dp->nfsdl_stateid.other[2];
4187 nd->nd_flag |= ND_USEGSSNAME;
4188 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4189 NFS_PROG, NFS_VER4, NULL, 1, NULL);
4192 error = nd->nd_repstat;
4193 mbuf_freem(nd->nd_mrep);
4201 nfsrpc_getacl(vnode_t vp, struct ucred *cred, NFSPROC_T *p,
4202 struct acl *aclp, void *stuff)
4204 struct nfsrv_descript nfsd, *nd = &nfsd;
4206 nfsattrbit_t attrbits;
4207 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
4209 if (nfsrv_useacl == 0 || !NFSHASNFSV4(nmp))
4210 return (EOPNOTSUPP);
4211 NFSCL_REQSTART(nd, NFSPROC_GETACL, vp);
4212 NFSZERO_ATTRBIT(&attrbits);
4213 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_ACL);
4214 (void) nfsrv_putattrbit(nd, &attrbits);
4215 error = nfscl_request(nd, vp, p, cred, stuff);
4218 if (!nd->nd_repstat)
4219 error = nfsv4_loadattr(nd, vp, NULL, NULL, NULL, 0, NULL,
4220 NULL, NULL, NULL, aclp, 0, NULL, NULL, NULL, p, cred);
4222 error = nd->nd_repstat;
4223 mbuf_freem(nd->nd_mrep);
4231 nfsrpc_setacl(vnode_t vp, struct ucred *cred, NFSPROC_T *p,
4232 struct acl *aclp, void *stuff)
4235 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
4237 if (nfsrv_useacl == 0 || !NFSHASNFSV4(nmp))
4238 return (EOPNOTSUPP);
4239 error = nfsrpc_setattr(vp, NULL, aclp, cred, p, NULL, NULL, stuff);
4247 nfsrpc_setaclrpc(vnode_t vp, struct ucred *cred, NFSPROC_T *p,
4248 struct acl *aclp, nfsv4stateid_t *stateidp, void *stuff)
4250 struct nfsrv_descript nfsd, *nd = &nfsd;
4252 nfsattrbit_t attrbits;
4253 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
4255 if (!NFSHASNFSV4(nmp))
4256 return (EOPNOTSUPP);
4257 NFSCL_REQSTART(nd, NFSPROC_SETACL, vp);
4258 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID);
4259 NFSZERO_ATTRBIT(&attrbits);
4260 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_ACL);
4261 (void) nfsv4_fillattr(nd, vnode_mount(vp), vp, aclp, NULL, NULL, 0,
4262 &attrbits, NULL, NULL, 0, 0, 0, 0, (uint64_t)0);
4263 error = nfscl_request(nd, vp, p, cred, stuff);
4266 /* Don't care about the pre/postop attributes */
4267 mbuf_freem(nd->nd_mrep);
4268 return (nd->nd_repstat);