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 <fs/nfs/nfsport.h>
51 extern int nfs_numnfscbd;
52 extern struct timeval nfsboottime;
53 extern u_int32_t newnfs_false, newnfs_true;
54 extern nfstype nfsv34_type[9];
55 extern int nfsrv_useacl;
56 extern char nfsv4_callbackaddr[INET6_ADDRSTRLEN];
58 int nfstest_outofseq = 0;
59 int nfscl_assumeposixlocks = 1;
60 int nfscl_enablecallb = 0;
61 short nfsv4_cbport = NFSV4_CBPORT;
62 int nfstest_openallsetattr = 0;
63 #endif /* !APPLEKEXT */
65 #define DIRHDSIZ (sizeof (struct dirent) - (MAXNAMLEN + 1))
67 static int nfsrpc_setattrrpc(vnode_t , struct vattr *, nfsv4stateid_t *,
68 struct ucred *, NFSPROC_T *, struct nfsvattr *, int *, void *);
69 static int nfsrpc_readrpc(vnode_t , struct uio *, struct ucred *,
70 nfsv4stateid_t *, NFSPROC_T *, struct nfsvattr *, int *, void *);
71 static int nfsrpc_writerpc(vnode_t , struct uio *, int *, u_char *,
72 struct ucred *, nfsv4stateid_t *, NFSPROC_T *, struct nfsvattr *, int *,
74 static int nfsrpc_createv23(vnode_t , char *, int, struct vattr *,
75 nfsquad_t, int, struct ucred *, NFSPROC_T *, struct nfsvattr *,
76 struct nfsvattr *, struct nfsfh **, int *, int *, void *);
77 static int nfsrpc_createv4(vnode_t , char *, int, struct vattr *,
78 nfsquad_t, int, struct nfsclowner *, struct nfscldeleg **, struct ucred *,
79 NFSPROC_T *, struct nfsvattr *, struct nfsvattr *, struct nfsfh **, int *,
80 int *, void *, int *);
81 static int nfsrpc_locku(struct nfsrv_descript *, struct nfsmount *,
82 struct nfscllockowner *, u_int64_t, u_int64_t,
83 u_int32_t, struct ucred *, NFSPROC_T *, int);
84 #ifdef NFS4_ACL_EXTATTR_NAME
85 static int nfsrpc_setaclrpc(vnode_t, struct ucred *, NFSPROC_T *,
86 struct acl *, nfsv4stateid_t *, void *);
90 * nfs null call from vfs.
93 nfsrpc_null(vnode_t vp, struct ucred *cred, NFSPROC_T *p)
96 struct nfsrv_descript nfsd, *nd = &nfsd;
98 NFSCL_REQSTART(nd, NFSPROC_NULL, vp);
99 error = nfscl_request(nd, vp, p, cred, NULL);
100 if (nd->nd_repstat && !error)
101 error = nd->nd_repstat;
102 mbuf_freem(nd->nd_mrep);
108 * For nfs version 3 and 4, use the access rpc to check accessibility. If file
109 * modes are changed on the server, accesses might still fail later.
112 nfsrpc_access(vnode_t vp, int acmode, struct ucred *cred,
113 NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp)
116 u_int32_t mode, rmode;
119 mode = NFSACCESS_READ;
122 if (vnode_vtype(vp) == VDIR) {
124 mode |= (NFSACCESS_MODIFY | NFSACCESS_EXTEND |
127 mode |= NFSACCESS_LOOKUP;
130 mode |= (NFSACCESS_MODIFY | NFSACCESS_EXTEND);
132 mode |= NFSACCESS_EXECUTE;
136 * Now, just call nfsrpc_accessrpc() to do the actual RPC.
138 error = nfsrpc_accessrpc(vp, mode, cred, p, nap, attrflagp, &rmode,
142 * The NFS V3 spec does not clarify whether or not
143 * the returned access bits can be a superset of
144 * the ones requested, so...
146 if (!error && (rmode & mode) != mode)
152 * The actual rpc, separated out for Darwin.
155 nfsrpc_accessrpc(vnode_t vp, u_int32_t mode, struct ucred *cred,
156 NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, u_int32_t *rmodep,
160 u_int32_t supported, rmode;
162 struct nfsrv_descript nfsd, *nd = &nfsd;
163 nfsattrbit_t attrbits;
167 NFSCL_REQSTART(nd, NFSPROC_ACCESS, vp);
168 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
169 *tl = txdr_unsigned(mode);
170 if (nd->nd_flag & ND_NFSV4) {
172 * And do a Getattr op.
174 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
175 *tl = txdr_unsigned(NFSV4OP_GETATTR);
176 NFSGETATTR_ATTRBIT(&attrbits);
177 (void) nfsrv_putattrbit(nd, &attrbits);
179 error = nfscl_request(nd, vp, p, cred, stuff);
182 if (nd->nd_flag & ND_NFSV3) {
183 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
187 if (!nd->nd_repstat) {
188 if (nd->nd_flag & ND_NFSV4) {
189 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
190 supported = fxdr_unsigned(u_int32_t, *tl++);
192 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
194 rmode = fxdr_unsigned(u_int32_t, *tl);
195 if (nd->nd_flag & ND_NFSV4)
196 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
199 * It's not obvious what should be done about
200 * unsupported access modes. For now, be paranoid
201 * and clear the unsupported ones.
206 error = nd->nd_repstat;
208 mbuf_freem(nd->nd_mrep);
216 nfsrpc_open(vnode_t vp, int amode, struct ucred *cred, NFSPROC_T *p)
218 struct nfsclopen *op;
219 struct nfscldeleg *dp;
221 struct nfsnode *np = VTONFS(vp);
222 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
223 u_int32_t mode, clidrev;
224 int ret, newone, error, expireret = 0, retrycnt;
227 * For NFSv4, Open Ops are only done on Regular Files.
229 if (vnode_vtype(vp) != VREG)
233 mode |= NFSV4OPEN_ACCESSREAD;
235 mode |= NFSV4OPEN_ACCESSWRITE;
240 { char name[100]; int namel;
241 namel = (np->n_v4->n4_namelen < 100) ? np->n_v4->n4_namelen : 99;
242 bcopy(NFS4NODENAME(np->n_v4), name, namel);
244 printf("rpcopen p=0x%x name=%s",p->p_pid,name);
245 if (nfhp->nfh_len > 0) printf(" fh=0x%x\n",nfhp->nfh_fh[12]);
246 else printf(" fhl=0\n");
251 error = nfscl_open(vp, nfhp->nfh_fh, nfhp->nfh_len, mode, 1,
252 cred, p, NULL, &op, &newone, &ret, 1);
256 if (nmp->nm_clp != NULL)
257 clidrev = nmp->nm_clp->nfsc_clientidrev;
260 if (ret == NFSCLOPEN_DOOPEN) {
261 if (np->n_v4 != NULL) {
262 error = nfsrpc_openrpc(nmp, vp, np->n_v4->n4_data,
263 np->n_v4->n4_fhlen, np->n_fhp->nfh_fh,
264 np->n_fhp->nfh_len, mode, op,
265 NFS4NODENAME(np->n_v4), np->n_v4->n4_namelen, &dp,
266 0, 0x0, cred, p, 0, 0);
269 OSBitAndAtomic((int32_t)~NDELEGMOD, (UInt32 *)&np->n_flag);
272 np->n_flag &= ~NDELEGMOD;
275 (void) nfscl_deleg(nmp->nm_mountp,
276 op->nfso_own->nfsow_clp,
277 nfhp->nfh_fh, nfhp->nfh_len, cred, p, &dp);
282 newnfs_copyincred(cred, &op->nfso_cred);
283 } else if (ret == NFSCLOPEN_SETCRED)
285 * This is a new local open on a delegation. It needs
286 * to have credentials so that an open can be done
287 * against the server during recovery.
289 newnfs_copyincred(cred, &op->nfso_cred);
292 * nfso_opencnt is the count of how many VOP_OPEN()s have
293 * been done on this Open successfully and a VOP_CLOSE()
294 * is expected for each of these.
295 * If error is non-zero, don't increment it, since the Open
296 * hasn't succeeded yet.
300 nfscl_openrelease(op, error, newone);
301 if (error == NFSERR_GRACE || error == NFSERR_STALECLIENTID ||
302 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY) {
303 (void) nfs_catnap(PZERO, error, "nfs_open");
304 } else if ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID)
306 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
309 } while (error == NFSERR_GRACE || error == NFSERR_STALECLIENTID ||
310 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
311 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
312 expireret == 0 && clidrev != 0 && retrycnt < 4));
313 if (error && retrycnt >= 4)
319 * the actual open rpc
322 nfsrpc_openrpc(struct nfsmount *nmp, vnode_t vp, u_int8_t *nfhp, int fhlen,
323 u_int8_t *newfhp, int newfhlen, u_int32_t mode, struct nfsclopen *op,
324 u_int8_t *name, int namelen, struct nfscldeleg **dpp,
325 int reclaim, u_int32_t delegtype, struct ucred *cred, NFSPROC_T *p,
326 int syscred, int recursed)
329 struct nfsrv_descript nfsd, *nd = &nfsd;
330 struct nfscldeleg *dp, *ndp = NULL;
331 struct nfsvattr nfsva;
332 u_int32_t rflags, deleg;
333 nfsattrbit_t attrbits;
334 int error, ret, acesize, limitby;
338 nfscl_reqstart(nd, NFSPROC_OPEN, nmp, nfhp, fhlen, NULL);
339 NFSM_BUILD(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
340 *tl++ = txdr_unsigned(op->nfso_own->nfsow_seqid);
341 *tl++ = txdr_unsigned(mode & NFSV4OPEN_ACCESSBOTH);
342 *tl++ = txdr_unsigned((mode >> NFSLCK_SHIFT) & NFSV4OPEN_DENYBOTH);
343 *tl++ = op->nfso_own->nfsow_clp->nfsc_clientid.lval[0];
344 *tl = op->nfso_own->nfsow_clp->nfsc_clientid.lval[1];
345 (void) nfsm_strtom(nd, op->nfso_own->nfsow_owner, NFSV4CL_LOCKNAMELEN);
346 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
347 *tl++ = txdr_unsigned(NFSV4OPEN_NOCREATE);
349 *tl = txdr_unsigned(NFSV4OPEN_CLAIMPREVIOUS);
350 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
351 *tl = txdr_unsigned(delegtype);
354 *tl = txdr_unsigned(NFSV4OPEN_CLAIMDELEGATECUR);
355 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID);
356 *tl++ = dp->nfsdl_stateid.seqid;
357 *tl++ = dp->nfsdl_stateid.other[0];
358 *tl++ = dp->nfsdl_stateid.other[1];
359 *tl = dp->nfsdl_stateid.other[2];
361 *tl = txdr_unsigned(NFSV4OPEN_CLAIMNULL);
363 (void) nfsm_strtom(nd, name, namelen);
365 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
366 *tl = txdr_unsigned(NFSV4OP_GETATTR);
367 NFSZERO_ATTRBIT(&attrbits);
368 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_CHANGE);
369 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEMODIFY);
370 (void) nfsrv_putattrbit(nd, &attrbits);
372 nd->nd_flag |= ND_USEGSSNAME;
373 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, vp, p, cred,
374 NFS_PROG, NFS_VER4, NULL, 1, NULL);
377 NFSCL_INCRSEQID(op->nfso_own->nfsow_seqid, nd);
378 if (!nd->nd_repstat) {
379 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
381 op->nfso_stateid.seqid = *tl++;
382 op->nfso_stateid.other[0] = *tl++;
383 op->nfso_stateid.other[1] = *tl++;
384 op->nfso_stateid.other[2] = *tl;
385 rflags = fxdr_unsigned(u_int32_t, *(tl + 6));
386 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
389 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
390 deleg = fxdr_unsigned(u_int32_t, *tl);
391 if (deleg == NFSV4OPEN_DELEGATEREAD ||
392 deleg == NFSV4OPEN_DELEGATEWRITE) {
393 if (!(op->nfso_own->nfsow_clp->nfsc_flags &
394 NFSCLFLAGS_FIRSTDELEG))
395 op->nfso_own->nfsow_clp->nfsc_flags |=
396 (NFSCLFLAGS_FIRSTDELEG | NFSCLFLAGS_GOTDELEG);
397 MALLOC(ndp, struct nfscldeleg *,
398 sizeof (struct nfscldeleg) + newfhlen,
399 M_NFSCLDELEG, M_WAITOK);
400 LIST_INIT(&ndp->nfsdl_owner);
401 LIST_INIT(&ndp->nfsdl_lock);
402 ndp->nfsdl_clp = op->nfso_own->nfsow_clp;
403 ndp->nfsdl_fhlen = newfhlen;
404 NFSBCOPY(newfhp, ndp->nfsdl_fh, newfhlen);
405 newnfs_copyincred(cred, &ndp->nfsdl_cred);
406 nfscl_lockinit(&ndp->nfsdl_rwlock);
407 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
409 ndp->nfsdl_stateid.seqid = *tl++;
410 ndp->nfsdl_stateid.other[0] = *tl++;
411 ndp->nfsdl_stateid.other[1] = *tl++;
412 ndp->nfsdl_stateid.other[2] = *tl++;
413 ret = fxdr_unsigned(int, *tl);
414 if (deleg == NFSV4OPEN_DELEGATEWRITE) {
415 ndp->nfsdl_flags = NFSCLDL_WRITE;
417 * Indicates how much the file can grow.
419 NFSM_DISSECT(tl, u_int32_t *,
421 limitby = fxdr_unsigned(int, *tl++);
423 case NFSV4OPEN_LIMITSIZE:
424 ndp->nfsdl_sizelimit = fxdr_hyper(tl);
426 case NFSV4OPEN_LIMITBLOCKS:
427 ndp->nfsdl_sizelimit =
428 fxdr_unsigned(u_int64_t, *tl++);
429 ndp->nfsdl_sizelimit *=
430 fxdr_unsigned(u_int64_t, *tl);
433 error = NFSERR_BADXDR;
437 ndp->nfsdl_flags = NFSCLDL_READ;
440 ndp->nfsdl_flags |= NFSCLDL_RECALL;
441 error = nfsrv_dissectace(nd, &ndp->nfsdl_ace, &ret,
445 } else if (deleg != NFSV4OPEN_DELEGATENONE) {
446 error = NFSERR_BADXDR;
449 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
450 error = nfsv4_loadattr(nd, NULL, &nfsva, NULL,
451 NULL, 0, NULL, NULL, NULL, NULL, NULL, 0,
452 NULL, NULL, NULL, p, cred);
456 ndp->nfsdl_change = nfsva.na_filerev;
457 ndp->nfsdl_modtime = nfsva.na_mtime;
458 ndp->nfsdl_flags |= NFSCLDL_MODTIMESET;
460 if (!reclaim && (rflags & NFSV4OPEN_RESULTCONFIRM)) {
462 ret = nfsrpc_openconfirm(vp, newfhp, newfhlen, op,
464 if (ret == NFSERR_DELAY)
465 (void) nfs_catnap(PZERO, ret, "nfs_open");
466 } while (ret == NFSERR_DELAY);
469 if ((rflags & NFSV4OPEN_LOCKTYPEPOSIX) ||
470 nfscl_assumeposixlocks)
471 op->nfso_posixlock = 1;
473 op->nfso_posixlock = 0;
476 * If the server is handing out delegations, but we didn't
477 * get one because an OpenConfirm was required, try the
478 * Open again, to get a delegation. This is a harmless no-op,
479 * from a server's point of view.
481 if (!reclaim && (rflags & NFSV4OPEN_RESULTCONFIRM) &&
482 (op->nfso_own->nfsow_clp->nfsc_flags & NFSCLFLAGS_GOTDELEG)
483 && !error && dp == NULL && ndp == NULL && !recursed) {
485 ret = nfsrpc_openrpc(nmp, vp, nfhp, fhlen, newfhp,
486 newfhlen, mode, op, name, namelen, &ndp, 0, 0x0,
487 cred, p, syscred, 1);
488 if (ret == NFSERR_DELAY)
489 (void) nfs_catnap(PZERO, ret, "nfs_open2");
490 } while (ret == NFSERR_DELAY);
493 FREE((caddr_t)ndp, M_NFSCLDELEG);
494 if (ret == NFSERR_STALECLIENTID ||
495 ret == NFSERR_STALEDONTRECOVER)
500 if (nd->nd_repstat != 0 && error == 0)
501 error = nd->nd_repstat;
502 if (error == NFSERR_STALECLIENTID)
503 nfscl_initiate_recovery(op->nfso_own->nfsow_clp);
507 else if (ndp != NULL)
508 FREE((caddr_t)ndp, M_NFSCLDELEG);
509 mbuf_freem(nd->nd_mrep);
517 nfsrpc_opendowngrade(vnode_t vp, u_int32_t mode, struct nfsclopen *op,
518 struct ucred *cred, NFSPROC_T *p)
521 struct nfsrv_descript nfsd, *nd = &nfsd;
524 NFSCL_REQSTART(nd, NFSPROC_OPENDOWNGRADE, vp);
525 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID + 3 * NFSX_UNSIGNED);
526 *tl++ = op->nfso_stateid.seqid;
527 *tl++ = op->nfso_stateid.other[0];
528 *tl++ = op->nfso_stateid.other[1];
529 *tl++ = op->nfso_stateid.other[2];
530 *tl++ = txdr_unsigned(op->nfso_own->nfsow_seqid);
531 *tl++ = txdr_unsigned(mode & NFSV4OPEN_ACCESSBOTH);
532 *tl = txdr_unsigned((mode >> NFSLCK_SHIFT) & NFSV4OPEN_DENYBOTH);
533 error = nfscl_request(nd, vp, p, cred, NULL);
536 NFSCL_INCRSEQID(op->nfso_own->nfsow_seqid, nd);
537 if (!nd->nd_repstat) {
538 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID);
539 op->nfso_stateid.seqid = *tl++;
540 op->nfso_stateid.other[0] = *tl++;
541 op->nfso_stateid.other[1] = *tl++;
542 op->nfso_stateid.other[2] = *tl;
544 if (nd->nd_repstat && error == 0)
545 error = nd->nd_repstat;
546 if (error == NFSERR_STALESTATEID)
547 nfscl_initiate_recovery(op->nfso_own->nfsow_clp);
549 mbuf_freem(nd->nd_mrep);
554 * V4 Close operation.
557 nfsrpc_close(vnode_t vp, int doclose, NFSPROC_T *p)
559 struct nfsclclient *clp;
562 if (vnode_vtype(vp) != VREG)
565 error = nfscl_doclose(vp, &clp, p);
567 error = nfscl_getclose(vp, &clp);
571 nfscl_clientrelease(clp);
579 nfsrpc_doclose(struct nfsmount *nmp, struct nfsclopen *op, NFSPROC_T *p)
581 struct nfsrv_descript nfsd, *nd = &nfsd;
582 struct nfscllockowner *lp;
583 struct nfscllock *lop, *nlop;
585 u_int64_t off = 0, len = 0;
586 u_int32_t type = NFSV4LOCKT_READ;
587 int error, do_unlock, trycnt;
589 tcred = newnfs_getcred();
590 newnfs_copycred(&op->nfso_cred, tcred);
592 * (Theoretically this could be done in the same
593 * compound as the close, but having multiple
594 * sequenced Ops in the same compound might be
595 * too scary for some servers.)
597 if (op->nfso_posixlock) {
600 type = NFSV4LOCKT_READ;
604 * Since this function is only called from VOP_INACTIVE(), no
605 * other thread will be manipulating this Open. As such, the
606 * lock lists are not being changed by other threads, so it should
607 * be safe to do this without locking.
609 LIST_FOREACH(lp, &op->nfso_lock, nfsl_list) {
611 LIST_FOREACH_SAFE(lop, &lp->nfsl_lock, nfslo_list, nlop) {
612 if (op->nfso_posixlock == 0) {
613 off = lop->nfslo_first;
614 len = lop->nfslo_end - lop->nfslo_first;
615 if (lop->nfslo_type == F_WRLCK)
616 type = NFSV4LOCKT_WRITE;
618 type = NFSV4LOCKT_READ;
623 error = nfsrpc_locku(nd, nmp, lp, off,
624 len, type, tcred, p, 0);
625 if ((nd->nd_repstat == NFSERR_GRACE ||
626 nd->nd_repstat == NFSERR_DELAY) &&
628 (void) nfs_catnap(PZERO,
631 } while ((nd->nd_repstat == NFSERR_GRACE ||
632 nd->nd_repstat == NFSERR_DELAY) &&
633 error == 0 && trycnt++ < 5);
634 if (op->nfso_posixlock)
637 nfscl_freelock(lop, 0);
642 * There could be other Opens for different files on the same
643 * OpenOwner, so locking is required.
646 nfscl_lockexcl(&op->nfso_own->nfsow_rwlock, NFSCLSTATEMUTEXPTR);
649 error = nfscl_tryclose(op, tcred, nmp, p);
650 if (error == NFSERR_GRACE)
651 (void) nfs_catnap(PZERO, error, "nfs_close");
652 } while (error == NFSERR_GRACE);
654 nfscl_lockunlock(&op->nfso_own->nfsow_rwlock);
657 * Move the lockowner to nfsc_defunctlockowner,
658 * so the Renew thread will do the ReleaseLockOwner
659 * Op on it later. There might still be other
660 * opens using the same lockowner name.
662 lp = LIST_FIRST(&op->nfso_lock);
664 while (LIST_NEXT(lp, nfsl_list) != NULL)
665 lp = LIST_NEXT(lp, nfsl_list);
666 LIST_PREPEND(&nmp->nm_clp->nfsc_defunctlockowner,
667 &op->nfso_lock, lp, nfsl_list);
668 LIST_INIT(&op->nfso_lock);
670 nfscl_freeopen(op, 0);
676 * The actual Close RPC.
679 nfsrpc_closerpc(struct nfsrv_descript *nd, struct nfsmount *nmp,
680 struct nfsclopen *op, struct ucred *cred, NFSPROC_T *p,
686 nfscl_reqstart(nd, NFSPROC_CLOSE, nmp, op->nfso_fh,
687 op->nfso_fhlen, NULL);
688 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED + NFSX_STATEID);
689 *tl++ = txdr_unsigned(op->nfso_own->nfsow_seqid);
690 *tl++ = op->nfso_stateid.seqid;
691 *tl++ = op->nfso_stateid.other[0];
692 *tl++ = op->nfso_stateid.other[1];
693 *tl = op->nfso_stateid.other[2];
695 nd->nd_flag |= ND_USEGSSNAME;
696 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
697 NFS_PROG, NFS_VER4, NULL, 1, NULL);
700 NFSCL_INCRSEQID(op->nfso_own->nfsow_seqid, nd);
701 if (nd->nd_repstat == 0)
702 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID);
703 error = nd->nd_repstat;
704 if (error == NFSERR_STALESTATEID)
705 nfscl_initiate_recovery(op->nfso_own->nfsow_clp);
707 mbuf_freem(nd->nd_mrep);
712 * V4 Open Confirm RPC.
715 nfsrpc_openconfirm(vnode_t vp, u_int8_t *nfhp, int fhlen,
716 struct nfsclopen *op, struct ucred *cred, NFSPROC_T *p)
719 struct nfsrv_descript nfsd, *nd = &nfsd;
722 nfscl_reqstart(nd, NFSPROC_OPENCONFIRM, VFSTONFS(vnode_mount(vp)),
724 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED + NFSX_STATEID);
725 *tl++ = op->nfso_stateid.seqid;
726 *tl++ = op->nfso_stateid.other[0];
727 *tl++ = op->nfso_stateid.other[1];
728 *tl++ = op->nfso_stateid.other[2];
729 *tl = txdr_unsigned(op->nfso_own->nfsow_seqid);
730 error = nfscl_request(nd, vp, p, cred, NULL);
733 NFSCL_INCRSEQID(op->nfso_own->nfsow_seqid, nd);
734 if (!nd->nd_repstat) {
735 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID);
736 op->nfso_stateid.seqid = *tl++;
737 op->nfso_stateid.other[0] = *tl++;
738 op->nfso_stateid.other[1] = *tl++;
739 op->nfso_stateid.other[2] = *tl;
741 error = nd->nd_repstat;
742 if (error == NFSERR_STALESTATEID)
743 nfscl_initiate_recovery(op->nfso_own->nfsow_clp);
745 mbuf_freem(nd->nd_mrep);
750 * Do the setclientid and setclientid confirm RPCs. Called from nfs_statfs()
751 * when a mount has just occurred and when the server replies NFSERR_EXPIRED.
754 nfsrpc_setclient(struct nfsmount *nmp, struct nfsclclient *clp,
755 struct ucred *cred, NFSPROC_T *p)
758 struct nfsrv_descript nfsd;
759 struct nfsrv_descript *nd = &nfsd;
760 nfsattrbit_t attrbits;
761 u_int8_t *cp = NULL, *cp2, addr[INET6_ADDRSTRLEN + 9];
763 int error, isinet6 = 0, callblen;
766 static u_int32_t rev = 0;
768 if (nfsboottime.tv_sec == 0)
769 NFSSETBOOTTIME(nfsboottime);
770 nfscl_reqstart(nd, NFSPROC_SETCLIENTID, nmp, NULL, 0, NULL);
771 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
772 *tl++ = txdr_unsigned(nfsboottime.tv_sec);
773 *tl = txdr_unsigned(rev++);
774 (void) nfsm_strtom(nd, clp->nfsc_id, clp->nfsc_idlen);
777 * set up the callback address
779 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
780 *tl = txdr_unsigned(NFS_CALLBCKPROG);
781 callblen = strlen(nfsv4_callbackaddr);
783 cp = nfscl_getmyip(nmp, &isinet6);
784 if (nfscl_enablecallb && nfs_numnfscbd > 0 &&
785 (callblen > 0 || cp != NULL)) {
786 port = htons(nfsv4_cbport);
787 cp2 = (u_int8_t *)&port;
790 strchr(nfsv4_callbackaddr, ':')) || isinet6) {
791 char ip6buf[INET6_ADDRSTRLEN], *ip6add;
793 (void) nfsm_strtom(nd, "tcp6", 4);
795 ip6_sprintf(ip6buf, (struct in6_addr *)cp);
798 ip6add = nfsv4_callbackaddr;
800 snprintf(addr, INET6_ADDRSTRLEN + 9, "%s.%d.%d",
801 ip6add, cp2[0], cp2[1]);
805 (void) nfsm_strtom(nd, "tcp", 3);
807 snprintf(addr, INET6_ADDRSTRLEN + 9,
808 "%d.%d.%d.%d.%d.%d", cp[0], cp[1],
809 cp[2], cp[3], cp2[0], cp2[1]);
811 snprintf(addr, INET6_ADDRSTRLEN + 9,
812 "%s.%d.%d", nfsv4_callbackaddr,
815 (void) nfsm_strtom(nd, addr, strlen(addr));
817 (void) nfsm_strtom(nd, "tcp", 3);
818 (void) nfsm_strtom(nd, "0.0.0.0.0.0", 11);
820 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
821 *tl = txdr_unsigned(clp->nfsc_cbident);
822 nd->nd_flag |= ND_USEGSSNAME;
823 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
824 NFS_PROG, NFS_VER4, NULL, 1, NULL);
827 if (nd->nd_repstat == 0) {
828 NFSM_DISSECT(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
829 clp->nfsc_clientid.lval[0] = *tl++;
830 clp->nfsc_clientid.lval[1] = *tl++;
831 confirm.lval[0] = *tl++;
832 confirm.lval[1] = *tl;
833 mbuf_freem(nd->nd_mrep);
839 nfscl_reqstart(nd, NFSPROC_SETCLIENTIDCFRM, nmp, NULL, 0, NULL);
840 NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
841 *tl++ = clp->nfsc_clientid.lval[0];
842 *tl++ = clp->nfsc_clientid.lval[1];
843 *tl++ = confirm.lval[0];
844 *tl = confirm.lval[1];
845 nd->nd_flag |= ND_USEGSSNAME;
846 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p,
847 cred, NFS_PROG, NFS_VER4, NULL, 1, NULL);
850 mbuf_freem(nd->nd_mrep);
852 if (nd->nd_repstat == 0) {
853 nfscl_reqstart(nd, NFSPROC_GETATTR, nmp, nmp->nm_fh,
854 nmp->nm_fhsize, NULL);
855 NFSZERO_ATTRBIT(&attrbits);
856 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_LEASETIME);
857 (void) nfsrv_putattrbit(nd, &attrbits);
858 nd->nd_flag |= ND_USEGSSNAME;
859 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p,
860 cred, NFS_PROG, NFS_VER4, NULL, 1, NULL);
863 if (nd->nd_repstat == 0) {
864 error = nfsv4_loadattr(nd, NULL, NULL, NULL, NULL, 0, NULL,
865 NULL, NULL, NULL, NULL, 0, NULL, &lease, NULL, p, cred);
868 clp->nfsc_renew = NFSCL_RENEW(lease);
869 clp->nfsc_expire = NFSD_MONOSEC + clp->nfsc_renew;
870 clp->nfsc_clientidrev++;
871 if (clp->nfsc_clientidrev == 0)
872 clp->nfsc_clientidrev++;
876 error = nd->nd_repstat;
878 mbuf_freem(nd->nd_mrep);
886 nfsrpc_getattr(vnode_t vp, struct ucred *cred, NFSPROC_T *p,
887 struct nfsvattr *nap, void *stuff)
889 struct nfsrv_descript nfsd, *nd = &nfsd;
891 nfsattrbit_t attrbits;
893 NFSCL_REQSTART(nd, NFSPROC_GETATTR, vp);
894 if (nd->nd_flag & ND_NFSV4) {
895 NFSGETATTR_ATTRBIT(&attrbits);
896 (void) nfsrv_putattrbit(nd, &attrbits);
898 error = nfscl_request(nd, vp, p, cred, stuff);
902 error = nfsm_loadattr(nd, nap);
904 error = nd->nd_repstat;
905 mbuf_freem(nd->nd_mrep);
910 * nfs getattr call with non-vnode arguemnts.
913 nfsrpc_getattrnovp(struct nfsmount *nmp, u_int8_t *fhp, int fhlen, int syscred,
914 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, u_int64_t *xidp)
916 struct nfsrv_descript nfsd, *nd = &nfsd;
917 int error, vers = NFS_VER2;
918 nfsattrbit_t attrbits;
920 nfscl_reqstart(nd, NFSPROC_GETATTR, nmp, fhp, fhlen, NULL);
921 if (nd->nd_flag & ND_NFSV4) {
923 NFSGETATTR_ATTRBIT(&attrbits);
924 (void) nfsrv_putattrbit(nd, &attrbits);
925 } else if (nd->nd_flag & ND_NFSV3) {
929 nd->nd_flag |= ND_USEGSSNAME;
930 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
931 NFS_PROG, vers, NULL, 1, xidp);
935 error = nfsm_loadattr(nd, nap);
937 error = nd->nd_repstat;
938 mbuf_freem(nd->nd_mrep);
943 * Do an nfs setattr operation.
946 nfsrpc_setattr(vnode_t vp, struct vattr *vap, NFSACL_T *aclp,
947 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *rnap, int *attrflagp,
950 int error, expireret = 0, openerr, retrycnt;
951 u_int32_t clidrev = 0, mode;
952 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
954 nfsv4stateid_t stateid;
957 if (nmp->nm_clp != NULL)
958 clidrev = nmp->nm_clp->nfsc_clientidrev;
959 if (vap != NULL && NFSATTRISSET(u_quad_t, vap, va_size))
960 mode = NFSV4OPEN_ACCESSWRITE;
962 mode = NFSV4OPEN_ACCESSREAD;
967 if (NFSHASNFSV4(nmp)) {
968 nfhp = VTONFS(vp)->n_fhp;
969 error = nfscl_getstateid(vp, nfhp->nfh_fh,
970 nfhp->nfh_len, mode, cred, p, &stateid, &lckp);
971 if (error && vnode_vtype(vp) == VREG &&
972 (mode == NFSV4OPEN_ACCESSWRITE ||
973 nfstest_openallsetattr)) {
975 * No Open stateid, so try and open the file
978 if (mode == NFSV4OPEN_ACCESSWRITE)
979 openerr = nfsrpc_open(vp, FWRITE, cred,
982 openerr = nfsrpc_open(vp, FREAD, cred,
985 (void) nfscl_getstateid(vp,
986 nfhp->nfh_fh, nfhp->nfh_len,
987 mode, cred, p, &stateid, &lckp);
991 error = nfsrpc_setattrrpc(vp, vap, &stateid, cred, p,
992 rnap, attrflagp, stuff);
993 #ifdef NFS4_ACL_EXTATTR_NAME
995 error = nfsrpc_setaclrpc(vp, cred, p, aclp, &stateid,
1001 if (error == NFSERR_STALESTATEID)
1002 nfscl_initiate_recovery(nmp->nm_clp);
1004 nfscl_lockderef(lckp);
1006 (void) nfsrpc_close(vp, 0, p);
1007 if (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
1008 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
1009 error == NFSERR_OLDSTATEID) {
1010 (void) nfs_catnap(PZERO, error, "nfs_setattr");
1011 } else if ((error == NFSERR_EXPIRED ||
1012 error == NFSERR_BADSTATEID) && clidrev != 0) {
1013 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
1016 } while (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
1017 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
1018 (error == NFSERR_OLDSTATEID && retrycnt < 20) ||
1019 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
1020 expireret == 0 && clidrev != 0 && retrycnt < 4));
1021 if (error && retrycnt >= 4)
1027 nfsrpc_setattrrpc(vnode_t vp, struct vattr *vap,
1028 nfsv4stateid_t *stateidp, struct ucred *cred, NFSPROC_T *p,
1029 struct nfsvattr *rnap, int *attrflagp, void *stuff)
1032 struct nfsrv_descript nfsd, *nd = &nfsd;
1034 nfsattrbit_t attrbits;
1037 NFSCL_REQSTART(nd, NFSPROC_SETATTR, vp);
1038 if (nd->nd_flag & ND_NFSV4)
1039 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID);
1040 vap->va_type = vnode_vtype(vp);
1041 nfscl_fillsattr(nd, vap, vp, NFSSATTR_FULL, 0);
1042 if (nd->nd_flag & ND_NFSV3) {
1043 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1045 } else if (nd->nd_flag & ND_NFSV4) {
1046 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1047 *tl = txdr_unsigned(NFSV4OP_GETATTR);
1048 NFSGETATTR_ATTRBIT(&attrbits);
1049 (void) nfsrv_putattrbit(nd, &attrbits);
1051 error = nfscl_request(nd, vp, p, cred, stuff);
1054 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4))
1055 error = nfscl_wcc_data(nd, vp, rnap, attrflagp, NULL, stuff);
1056 if ((nd->nd_flag & ND_NFSV4) && !error)
1057 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
1058 if (!(nd->nd_flag & ND_NFSV3) && !nd->nd_repstat && !error)
1059 error = nfscl_postop_attr(nd, rnap, attrflagp, stuff);
1060 mbuf_freem(nd->nd_mrep);
1061 if (nd->nd_repstat && !error)
1062 error = nd->nd_repstat;
1070 nfsrpc_lookup(vnode_t dvp, char *name, int len, struct ucred *cred,
1071 NFSPROC_T *p, struct nfsvattr *dnap, struct nfsvattr *nap,
1072 struct nfsfh **nfhpp, int *attrflagp, int *dattrflagp, void *stuff)
1075 struct nfsrv_descript nfsd, *nd = &nfsd;
1076 struct nfsmount *nmp;
1079 nfsattrbit_t attrbits;
1080 int error = 0, lookupp = 0;
1084 if (vnode_vtype(dvp) != VDIR)
1086 nmp = VFSTONFS(vnode_mount(dvp));
1087 if (len > NFS_MAXNAMLEN)
1088 return (ENAMETOOLONG);
1089 if (NFSHASNFSV4(nmp) && len == 1 &&
1092 * Just return the current dir's fh.
1095 MALLOC(nfhp, struct nfsfh *, sizeof (struct nfsfh) +
1096 np->n_fhp->nfh_len, M_NFSFH, M_WAITOK);
1097 nfhp->nfh_len = np->n_fhp->nfh_len;
1098 NFSBCOPY(np->n_fhp->nfh_fh, nfhp->nfh_fh, nfhp->nfh_len);
1102 if (NFSHASNFSV4(nmp) && len == 2 &&
1103 name[0] == '.' && name[1] == '.') {
1105 NFSCL_REQSTART(nd, NFSPROC_LOOKUPP, dvp);
1107 NFSCL_REQSTART(nd, NFSPROC_LOOKUP, dvp);
1108 (void) nfsm_strtom(nd, name, len);
1110 if (nd->nd_flag & ND_NFSV4) {
1111 NFSGETATTR_ATTRBIT(&attrbits);
1112 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1113 *tl++ = txdr_unsigned(NFSV4OP_GETFH);
1114 *tl = txdr_unsigned(NFSV4OP_GETATTR);
1115 (void) nfsrv_putattrbit(nd, &attrbits);
1117 error = nfscl_request(nd, dvp, p, cred, stuff);
1120 if (nd->nd_repstat) {
1122 * When an NFSv4 Lookupp returns ENOENT, it means that
1123 * the lookup is at the root of an fs, so return this dir.
1125 if (nd->nd_repstat == NFSERR_NOENT && lookupp) {
1127 MALLOC(nfhp, struct nfsfh *, sizeof (struct nfsfh) +
1128 np->n_fhp->nfh_len, M_NFSFH, M_WAITOK);
1129 nfhp->nfh_len = np->n_fhp->nfh_len;
1130 NFSBCOPY(np->n_fhp->nfh_fh, nfhp->nfh_fh, nfhp->nfh_len);
1132 mbuf_freem(nd->nd_mrep);
1135 if (nd->nd_flag & ND_NFSV3)
1136 error = nfscl_postop_attr(nd, dnap, dattrflagp, stuff);
1139 if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) == ND_NFSV4) {
1140 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1142 nd->nd_flag |= ND_NOMOREDATA;
1146 error = nfsm_getfh(nd, nfhpp);
1150 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
1151 if ((nd->nd_flag & ND_NFSV3) && !error)
1152 error = nfscl_postop_attr(nd, dnap, dattrflagp, stuff);
1154 mbuf_freem(nd->nd_mrep);
1155 if (!error && nd->nd_repstat)
1156 error = nd->nd_repstat;
1161 * Do a readlink rpc.
1164 nfsrpc_readlink(vnode_t vp, struct uio *uiop, struct ucred *cred,
1165 NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, void *stuff)
1168 struct nfsrv_descript nfsd, *nd = &nfsd;
1169 struct nfsnode *np = VTONFS(vp);
1170 nfsattrbit_t attrbits;
1171 int error, len, cangetattr = 1;
1174 NFSCL_REQSTART(nd, NFSPROC_READLINK, vp);
1175 if (nd->nd_flag & ND_NFSV4) {
1177 * And do a Getattr op.
1179 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1180 *tl = txdr_unsigned(NFSV4OP_GETATTR);
1181 NFSGETATTR_ATTRBIT(&attrbits);
1182 (void) nfsrv_putattrbit(nd, &attrbits);
1184 error = nfscl_request(nd, vp, p, cred, stuff);
1187 if (nd->nd_flag & ND_NFSV3)
1188 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
1189 if (!nd->nd_repstat && !error) {
1190 NFSM_STRSIZ(len, NFS_MAXPATHLEN);
1192 * This seems weird to me, but must have been added to
1193 * FreeBSD for some reason. The only thing I can think of
1194 * is that there was/is some server that replies with
1195 * more link data than it should?
1197 if (len == NFS_MAXPATHLEN) {
1199 if (np->n_size > 0 && np->n_size < NFS_MAXPATHLEN) {
1205 error = nfsm_mbufuio(nd, uiop, len);
1206 if ((nd->nd_flag & ND_NFSV4) && !error && cangetattr)
1207 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
1209 if (nd->nd_repstat && !error)
1210 error = nd->nd_repstat;
1212 mbuf_freem(nd->nd_mrep);
1220 nfsrpc_read(vnode_t vp, struct uio *uiop, struct ucred *cred,
1221 NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, void *stuff)
1223 int error, expireret = 0, retrycnt;
1224 u_int32_t clidrev = 0;
1225 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
1226 struct nfsnode *np = VTONFS(vp);
1227 struct ucred *newcred;
1228 struct nfsfh *nfhp = NULL;
1229 nfsv4stateid_t stateid;
1232 if (nmp->nm_clp != NULL)
1233 clidrev = nmp->nm_clp->nfsc_clientidrev;
1235 if (NFSHASNFSV4(nmp)) {
1238 newcred = NFSNEWCRED(cred);
1243 if (NFSHASNFSV4(nmp))
1244 (void)nfscl_getstateid(vp, nfhp->nfh_fh, nfhp->nfh_len,
1245 NFSV4OPEN_ACCESSREAD, newcred, p, &stateid, &lckp);
1246 error = nfsrpc_readrpc(vp, uiop, newcred, &stateid, p, nap,
1248 if (error == NFSERR_STALESTATEID)
1249 nfscl_initiate_recovery(nmp->nm_clp);
1251 nfscl_lockderef(lckp);
1252 if (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
1253 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
1254 error == NFSERR_OLDSTATEID) {
1255 (void) nfs_catnap(PZERO, error, "nfs_read");
1256 } else if ((error == NFSERR_EXPIRED ||
1257 error == NFSERR_BADSTATEID) && clidrev != 0) {
1258 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
1261 } while (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
1262 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
1263 (error == NFSERR_OLDSTATEID && retrycnt < 20) ||
1264 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
1265 expireret == 0 && clidrev != 0 && retrycnt < 4));
1266 if (error && retrycnt >= 4)
1268 if (NFSHASNFSV4(nmp) && p == NULL)
1269 NFSFREECRED(newcred);
1274 * The actual read RPC.
1277 nfsrpc_readrpc(vnode_t vp, struct uio *uiop, struct ucred *cred,
1278 nfsv4stateid_t *stateidp, NFSPROC_T *p, struct nfsvattr *nap,
1279 int *attrflagp, void *stuff)
1282 int error = 0, len, retlen, tsiz, eof = 0;
1283 struct nfsrv_descript nfsd;
1284 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
1285 struct nfsrv_descript *nd = &nfsd;
1288 tsiz = uio_uio_resid(uiop);
1289 if (uiop->uio_offset + tsiz > 0xffffffff &&
1290 !NFSHASNFSV3OR4(nmp))
1295 len = (tsiz > nmp->nm_rsize) ? nmp->nm_rsize : tsiz;
1296 NFSCL_REQSTART(nd, NFSPROC_READ, vp);
1297 if (nd->nd_flag & ND_NFSV4)
1298 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID);
1299 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED * 3);
1300 if (nd->nd_flag & ND_NFSV2) {
1301 *tl++ = txdr_unsigned(uiop->uio_offset);
1302 *tl++ = txdr_unsigned(len);
1305 txdr_hyper(uiop->uio_offset, tl);
1306 *(tl + 2) = txdr_unsigned(len);
1309 * Since I can't do a Getattr for NFSv4 for Write, there
1310 * doesn't seem any point in doing one here, either.
1311 * (See the comment in nfsrpc_writerpc() for more info.)
1313 error = nfscl_request(nd, vp, p, cred, stuff);
1316 if (nd->nd_flag & ND_NFSV3) {
1317 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
1318 } else if (!nd->nd_repstat && (nd->nd_flag & ND_NFSV2)) {
1319 error = nfsm_loadattr(nd, nap);
1323 if (nd->nd_repstat || error) {
1325 error = nd->nd_repstat;
1328 if (nd->nd_flag & ND_NFSV3) {
1329 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1330 eof = fxdr_unsigned(int, *(tl + 1));
1331 } else if (nd->nd_flag & ND_NFSV4) {
1332 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
1333 eof = fxdr_unsigned(int, *tl);
1335 NFSM_STRSIZ(retlen, nmp->nm_rsize);
1336 error = nfsm_mbufuio(nd, uiop, retlen);
1339 mbuf_freem(nd->nd_mrep);
1342 if (!(nd->nd_flag & ND_NFSV2)) {
1343 if (eof || retlen == 0)
1345 } else if (retlen < len)
1350 if (nd->nd_mrep != NULL)
1351 mbuf_freem(nd->nd_mrep);
1356 * nfs write operation
1357 * When called_from_strategy != 0, it should return EIO for an error that
1358 * indicates recovery is in progress, so that the buffer will be left
1359 * dirty and be written back to the server later. If it loops around,
1360 * the recovery thread could get stuck waiting for the buffer and recovery
1361 * will then deadlock.
1364 nfsrpc_write(vnode_t vp, struct uio *uiop, int *iomode, u_char *verfp,
1365 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp,
1366 void *stuff, int called_from_strategy)
1368 int error, expireret = 0, retrycnt, nostateid;
1369 u_int32_t clidrev = 0;
1370 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
1371 struct nfsnode *np = VTONFS(vp);
1372 struct ucred *newcred;
1373 struct nfsfh *nfhp = NULL;
1374 nfsv4stateid_t stateid;
1377 if (nmp->nm_clp != NULL)
1378 clidrev = nmp->nm_clp->nfsc_clientidrev;
1380 if (NFSHASNFSV4(nmp)) {
1382 newcred = NFSNEWCRED(cred);
1389 if (NFSHASNFSV4(nmp)) {
1390 (void)nfscl_getstateid(vp, nfhp->nfh_fh, nfhp->nfh_len,
1391 NFSV4OPEN_ACCESSWRITE, newcred, p, &stateid, &lckp);
1392 if (stateid.other[0] == 0 && stateid.other[1] == 0 &&
1393 stateid.other[2] == 0) {
1395 printf("stateid0 in write\n");
1400 * If there is no stateid for NFSv4, it means this is an
1401 * extraneous write after close. Basically a poorly
1402 * implemented buffer cache. Just don't do the write.
1407 error = nfsrpc_writerpc(vp, uiop, iomode, verfp,
1408 newcred, &stateid, p, nap, attrflagp, stuff);
1409 if (error == NFSERR_STALESTATEID)
1410 nfscl_initiate_recovery(nmp->nm_clp);
1412 nfscl_lockderef(lckp);
1413 if (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
1414 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
1415 error == NFSERR_OLDSTATEID) {
1416 (void) nfs_catnap(PZERO, error, "nfs_write");
1417 } else if ((error == NFSERR_EXPIRED ||
1418 error == NFSERR_BADSTATEID) && clidrev != 0) {
1419 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
1422 } while (error == NFSERR_GRACE || error == NFSERR_DELAY ||
1423 ((error == NFSERR_STALESTATEID ||
1424 error == NFSERR_STALEDONTRECOVER) && called_from_strategy == 0) ||
1425 (error == NFSERR_OLDSTATEID && retrycnt < 20) ||
1426 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
1427 expireret == 0 && clidrev != 0 && retrycnt < 4));
1428 if (error != 0 && (retrycnt >= 4 ||
1429 ((error == NFSERR_STALESTATEID ||
1430 error == NFSERR_STALEDONTRECOVER) && called_from_strategy != 0)))
1432 if (NFSHASNFSV4(nmp) && p == NULL)
1433 NFSFREECRED(newcred);
1438 * The actual write RPC.
1441 nfsrpc_writerpc(vnode_t vp, struct uio *uiop, int *iomode,
1442 u_char *verfp, struct ucred *cred, nfsv4stateid_t *stateidp,
1443 NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, void *stuff)
1446 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
1447 struct nfsnode *np = VTONFS(vp);
1448 int error = 0, len, tsiz, rlen, commit, committed = NFSWRITE_FILESYNC;
1449 int wccflag = 0, wsize;
1451 struct nfsrv_descript nfsd;
1452 struct nfsrv_descript *nd = &nfsd;
1453 nfsattrbit_t attrbits;
1456 if (uiop->uio_iovcnt != 1)
1457 panic("nfs: writerpc iovcnt > 1");
1460 tsiz = uio_uio_resid(uiop);
1462 if (uiop->uio_offset + tsiz > 0xffffffff &&
1463 !NFSHASNFSV3OR4(nmp)) {
1467 wsize = nmp->nm_wsize;
1469 nd->nd_mrep = NULL; /* NFSv2 sometimes does a write with */
1470 nd->nd_repstat = 0; /* uio_resid == 0, so the while is not done */
1472 nmp = VFSTONFS(vnode_mount(vp));
1478 len = (tsiz > wsize) ? wsize : tsiz;
1479 NFSCL_REQSTART(nd, NFSPROC_WRITE, vp);
1480 if (nd->nd_flag & ND_NFSV4) {
1481 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID);
1482 NFSM_BUILD(tl, u_int32_t *, NFSX_HYPER+2*NFSX_UNSIGNED);
1483 txdr_hyper(uiop->uio_offset, tl);
1485 *tl++ = txdr_unsigned(*iomode);
1486 *tl = txdr_unsigned(len);
1487 } else if (nd->nd_flag & ND_NFSV3) {
1488 NFSM_BUILD(tl, u_int32_t *, NFSX_HYPER+3*NFSX_UNSIGNED);
1489 txdr_hyper(uiop->uio_offset, tl);
1491 *tl++ = txdr_unsigned(len);
1492 *tl++ = txdr_unsigned(*iomode);
1493 *tl = txdr_unsigned(len);
1497 NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1499 * Not sure why someone changed this, since the
1500 * RFC clearly states that "beginoffset" and
1501 * "totalcount" are ignored, but it wouldn't
1502 * surprise me if there's a busted server out there.
1504 /* Set both "begin" and "current" to non-garbage. */
1505 x = txdr_unsigned((u_int32_t)uiop->uio_offset);
1506 *tl++ = x; /* "begin offset" */
1507 *tl++ = x; /* "current offset" */
1508 x = txdr_unsigned(len);
1509 *tl++ = x; /* total to this offset */
1510 *tl = x; /* size of this write */
1513 nfsm_uiombuf(nd, uiop, len);
1515 * Although it is tempting to do a normal Getattr Op in the
1516 * NFSv4 compound, the result can be a nearly hung client
1517 * system if the Getattr asks for Owner and/or OwnerGroup.
1518 * It occurs when the client can't map either the Owner or
1519 * Owner_group name in the Getattr reply to a uid/gid. When
1520 * there is a cache miss, the kernel does an upcall to the
1521 * nfsuserd. Then, it can try and read the local /etc/passwd
1522 * or /etc/group file. It can then block in getnewbuf(),
1523 * waiting for dirty writes to be pushed to the NFS server.
1524 * The only reason this doesn't result in a complete
1525 * deadlock, is that the upcall times out and allows
1526 * the write to complete. However, progress is so slow
1527 * that it might just as well be deadlocked.
1528 * So, we just get the attributes that change with each
1530 * nb: nfscl_loadattrcache() needs to be told that these
1531 * partial attributes from a write rpc are being
1532 * passed in, via a argument flag.
1534 if (nd->nd_flag & ND_NFSV4) {
1535 NFSWRITEGETATTR_ATTRBIT(&attrbits);
1536 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1537 *tl = txdr_unsigned(NFSV4OP_GETATTR);
1538 (void) nfsrv_putattrbit(nd, &attrbits);
1540 error = nfscl_request(nd, vp, p, cred, stuff);
1543 if (nd->nd_repstat) {
1545 * In case the rpc gets retried, roll
1546 * the uio fileds changed by nfsm_uiombuf()
1549 uiop->uio_offset -= len;
1550 uio_uio_resid_add(uiop, len);
1551 uio_iov_base_add(uiop, -len);
1552 uio_iov_len_add(uiop, len);
1554 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) {
1555 error = nfscl_wcc_data(nd, vp, nap, attrflagp,
1560 if (!nd->nd_repstat) {
1561 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) {
1562 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED
1564 rlen = fxdr_unsigned(int, *tl++);
1568 } else if (rlen < len) {
1569 backup = len - rlen;
1570 uio_iov_base_add(uiop, -(backup));
1571 uio_iov_len_add(uiop, backup);
1572 uiop->uio_offset -= backup;
1573 uio_uio_resid_add(uiop, backup);
1576 commit = fxdr_unsigned(int, *tl++);
1579 * Return the lowest committment level
1580 * obtained by any of the RPCs.
1582 if (committed == NFSWRITE_FILESYNC)
1584 else if (committed == NFSWRITE_DATASYNC &&
1585 commit == NFSWRITE_UNSTABLE)
1588 NFSBCOPY((caddr_t)tl, verfp, NFSX_VERF);
1590 if (!NFSHASWRITEVERF(nmp)) {
1591 NFSBCOPY((caddr_t)tl,
1592 (caddr_t)&nmp->nm_verf[0],
1594 NFSSETWRITEVERF(nmp);
1598 if (nd->nd_flag & ND_NFSV4)
1599 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1600 if (nd->nd_flag & (ND_NFSV2 | ND_NFSV4)) {
1601 error = nfsm_loadattr(nd, nap);
1603 *attrflagp = NFS_LATTR_NOSHRINK;
1606 error = nd->nd_repstat;
1610 NFSWRITERPC_SETTIME(wccflag, np, (nd->nd_flag & ND_NFSV4));
1611 mbuf_freem(nd->nd_mrep);
1616 if (nd->nd_mrep != NULL)
1617 mbuf_freem(nd->nd_mrep);
1618 *iomode = committed;
1619 if (nd->nd_repstat && !error)
1620 error = nd->nd_repstat;
1626 * For NFS v2 this is a kludge. Use a create rpc but with the IFMT bits of the
1627 * mode set to specify the file type and the size field for rdev.
1630 nfsrpc_mknod(vnode_t dvp, char *name, int namelen, struct vattr *vap,
1631 u_int32_t rdev, enum vtype vtyp, struct ucred *cred, NFSPROC_T *p,
1632 struct nfsvattr *dnap, struct nfsvattr *nnap, struct nfsfh **nfhpp,
1633 int *attrflagp, int *dattrflagp, void *dstuff)
1637 struct nfsrv_descript nfsd, *nd = &nfsd;
1638 nfsattrbit_t attrbits;
1643 if (namelen > NFS_MAXNAMLEN)
1644 return (ENAMETOOLONG);
1645 NFSCL_REQSTART(nd, NFSPROC_MKNOD, dvp);
1646 if (nd->nd_flag & ND_NFSV4) {
1647 if (vtyp == VBLK || vtyp == VCHR) {
1648 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
1649 *tl++ = vtonfsv34_type(vtyp);
1650 *tl++ = txdr_unsigned(NFSMAJOR(rdev));
1651 *tl = txdr_unsigned(NFSMINOR(rdev));
1653 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1654 *tl = vtonfsv34_type(vtyp);
1657 (void) nfsm_strtom(nd, name, namelen);
1658 if (nd->nd_flag & ND_NFSV3) {
1659 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1660 *tl = vtonfsv34_type(vtyp);
1662 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4))
1663 nfscl_fillsattr(nd, vap, dvp, 0, 0);
1664 if ((nd->nd_flag & ND_NFSV3) &&
1665 (vtyp == VCHR || vtyp == VBLK)) {
1666 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1667 *tl++ = txdr_unsigned(NFSMAJOR(rdev));
1668 *tl = txdr_unsigned(NFSMINOR(rdev));
1670 if (nd->nd_flag & ND_NFSV4) {
1671 NFSGETATTR_ATTRBIT(&attrbits);
1672 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1673 *tl++ = txdr_unsigned(NFSV4OP_GETFH);
1674 *tl = txdr_unsigned(NFSV4OP_GETATTR);
1675 (void) nfsrv_putattrbit(nd, &attrbits);
1677 if (nd->nd_flag & ND_NFSV2)
1678 nfscl_fillsattr(nd, vap, dvp, NFSSATTR_SIZERDEV, rdev);
1679 error = nfscl_request(nd, dvp, p, cred, dstuff);
1682 if (nd->nd_flag & ND_NFSV4)
1683 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
1684 if (!nd->nd_repstat) {
1685 if (nd->nd_flag & ND_NFSV4) {
1686 NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
1687 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
1691 error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp);
1695 if (nd->nd_flag & ND_NFSV3)
1696 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
1697 if (!error && nd->nd_repstat)
1698 error = nd->nd_repstat;
1700 mbuf_freem(nd->nd_mrep);
1705 * nfs file create call
1706 * Mostly just call the approriate routine. (I separated out v4, so that
1707 * error recovery wouldn't be as difficult.)
1710 nfsrpc_create(vnode_t dvp, char *name, int namelen, struct vattr *vap,
1711 nfsquad_t cverf, int fmode, struct ucred *cred, NFSPROC_T *p,
1712 struct nfsvattr *dnap, struct nfsvattr *nnap, struct nfsfh **nfhpp,
1713 int *attrflagp, int *dattrflagp, void *dstuff)
1715 int error = 0, newone, expireret = 0, retrycnt, unlocked;
1716 struct nfsclowner *owp;
1717 struct nfscldeleg *dp;
1718 struct nfsmount *nmp = VFSTONFS(vnode_mount(dvp));
1721 if (NFSHASNFSV4(nmp)) {
1725 error = nfscl_open(dvp, NULL, 0, (NFSV4OPEN_ACCESSWRITE |
1726 NFSV4OPEN_ACCESSREAD), 0, cred, p, &owp, NULL, &newone,
1730 if (nmp->nm_clp != NULL)
1731 clidrev = nmp->nm_clp->nfsc_clientidrev;
1734 error = nfsrpc_createv4(dvp, name, namelen, vap, cverf, fmode,
1735 owp, &dp, cred, p, dnap, nnap, nfhpp, attrflagp, dattrflagp,
1738 (void) nfscl_deleg(nmp->nm_mountp, owp->nfsow_clp,
1739 (*nfhpp)->nfh_fh, (*nfhpp)->nfh_len, cred, p, &dp);
1740 nfscl_ownerrelease(owp, error, newone, unlocked);
1741 if (error == NFSERR_GRACE || error == NFSERR_STALECLIENTID ||
1742 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY) {
1743 (void) nfs_catnap(PZERO, error, "nfs_open");
1744 } else if ((error == NFSERR_EXPIRED ||
1745 error == NFSERR_BADSTATEID) && clidrev != 0) {
1746 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
1749 } while (error == NFSERR_GRACE || error == NFSERR_STALECLIENTID ||
1750 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
1751 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
1752 expireret == 0 && clidrev != 0 && retrycnt < 4));
1753 if (error && retrycnt >= 4)
1756 error = nfsrpc_createv23(dvp, name, namelen, vap, cverf,
1757 fmode, cred, p, dnap, nnap, nfhpp, attrflagp, dattrflagp,
1764 * The create rpc for v2 and 3.
1767 nfsrpc_createv23(vnode_t dvp, char *name, int namelen, struct vattr *vap,
1768 nfsquad_t cverf, int fmode, struct ucred *cred, NFSPROC_T *p,
1769 struct nfsvattr *dnap, struct nfsvattr *nnap, struct nfsfh **nfhpp,
1770 int *attrflagp, int *dattrflagp, void *dstuff)
1774 struct nfsrv_descript nfsd, *nd = &nfsd;
1779 if (namelen > NFS_MAXNAMLEN)
1780 return (ENAMETOOLONG);
1781 NFSCL_REQSTART(nd, NFSPROC_CREATE, dvp);
1782 (void) nfsm_strtom(nd, name, namelen);
1783 if (nd->nd_flag & ND_NFSV3) {
1784 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1785 if (fmode & O_EXCL) {
1786 *tl = txdr_unsigned(NFSCREATE_EXCLUSIVE);
1787 NFSM_BUILD(tl, u_int32_t *, NFSX_VERF);
1788 *tl++ = cverf.lval[0];
1789 *tl = cverf.lval[1];
1791 *tl = txdr_unsigned(NFSCREATE_UNCHECKED);
1792 nfscl_fillsattr(nd, vap, dvp, 0, 0);
1795 nfscl_fillsattr(nd, vap, dvp, NFSSATTR_SIZE0, 0);
1797 error = nfscl_request(nd, dvp, p, cred, dstuff);
1800 if (nd->nd_repstat == 0) {
1801 error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp);
1805 if (nd->nd_flag & ND_NFSV3)
1806 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
1807 if (nd->nd_repstat != 0 && error == 0)
1808 error = nd->nd_repstat;
1810 mbuf_freem(nd->nd_mrep);
1815 nfsrpc_createv4(vnode_t dvp, char *name, int namelen, struct vattr *vap,
1816 nfsquad_t cverf, int fmode, struct nfsclowner *owp, struct nfscldeleg **dpp,
1817 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap,
1818 struct nfsvattr *nnap, struct nfsfh **nfhpp, int *attrflagp,
1819 int *dattrflagp, void *dstuff, int *unlockedp)
1822 int error = 0, deleg, newone, ret, acesize, limitby;
1823 struct nfsrv_descript nfsd, *nd = &nfsd;
1824 struct nfsclopen *op;
1825 struct nfscldeleg *dp = NULL;
1828 nfsattrbit_t attrbits;
1829 nfsv4stateid_t stateid;
1837 if (namelen > NFS_MAXNAMLEN)
1838 return (ENAMETOOLONG);
1839 NFSCL_REQSTART(nd, NFSPROC_CREATE, dvp);
1841 * For V4, this is actually an Open op.
1843 NFSM_BUILD(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
1844 *tl++ = txdr_unsigned(owp->nfsow_seqid);
1845 *tl++ = txdr_unsigned(NFSV4OPEN_ACCESSWRITE |
1846 NFSV4OPEN_ACCESSREAD);
1847 *tl++ = txdr_unsigned(NFSV4OPEN_DENYNONE);
1848 *tl++ = owp->nfsow_clp->nfsc_clientid.lval[0];
1849 *tl = owp->nfsow_clp->nfsc_clientid.lval[1];
1850 (void) nfsm_strtom(nd, owp->nfsow_owner, NFSV4CL_LOCKNAMELEN);
1851 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1852 *tl++ = txdr_unsigned(NFSV4OPEN_CREATE);
1853 if (fmode & O_EXCL) {
1854 *tl = txdr_unsigned(NFSCREATE_EXCLUSIVE);
1855 NFSM_BUILD(tl, u_int32_t *, NFSX_VERF);
1856 *tl++ = cverf.lval[0];
1857 *tl = cverf.lval[1];
1859 *tl = txdr_unsigned(NFSCREATE_UNCHECKED);
1860 nfscl_fillsattr(nd, vap, dvp, 0, 0);
1862 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1863 *tl = txdr_unsigned(NFSV4OPEN_CLAIMNULL);
1864 (void) nfsm_strtom(nd, name, namelen);
1865 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1866 *tl++ = txdr_unsigned(NFSV4OP_GETFH);
1867 *tl = txdr_unsigned(NFSV4OP_GETATTR);
1868 NFSGETATTR_ATTRBIT(&attrbits);
1869 (void) nfsrv_putattrbit(nd, &attrbits);
1870 error = nfscl_request(nd, dvp, p, cred, dstuff);
1873 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
1876 NFSCL_INCRSEQID(owp->nfsow_seqid, nd);
1877 if (nd->nd_repstat == 0) {
1878 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
1880 stateid.seqid = *tl++;
1881 stateid.other[0] = *tl++;
1882 stateid.other[1] = *tl++;
1883 stateid.other[2] = *tl;
1884 rflags = fxdr_unsigned(u_int32_t, *(tl + 6));
1885 (void) nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
1886 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
1887 deleg = fxdr_unsigned(int, *tl);
1888 if (deleg == NFSV4OPEN_DELEGATEREAD ||
1889 deleg == NFSV4OPEN_DELEGATEWRITE) {
1890 if (!(owp->nfsow_clp->nfsc_flags &
1891 NFSCLFLAGS_FIRSTDELEG))
1892 owp->nfsow_clp->nfsc_flags |=
1893 (NFSCLFLAGS_FIRSTDELEG | NFSCLFLAGS_GOTDELEG);
1894 MALLOC(dp, struct nfscldeleg *,
1895 sizeof (struct nfscldeleg) + NFSX_V4FHMAX,
1896 M_NFSCLDELEG, M_WAITOK);
1897 LIST_INIT(&dp->nfsdl_owner);
1898 LIST_INIT(&dp->nfsdl_lock);
1899 dp->nfsdl_clp = owp->nfsow_clp;
1900 newnfs_copyincred(cred, &dp->nfsdl_cred);
1901 nfscl_lockinit(&dp->nfsdl_rwlock);
1902 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
1904 dp->nfsdl_stateid.seqid = *tl++;
1905 dp->nfsdl_stateid.other[0] = *tl++;
1906 dp->nfsdl_stateid.other[1] = *tl++;
1907 dp->nfsdl_stateid.other[2] = *tl++;
1908 ret = fxdr_unsigned(int, *tl);
1909 if (deleg == NFSV4OPEN_DELEGATEWRITE) {
1910 dp->nfsdl_flags = NFSCLDL_WRITE;
1912 * Indicates how much the file can grow.
1914 NFSM_DISSECT(tl, u_int32_t *,
1916 limitby = fxdr_unsigned(int, *tl++);
1918 case NFSV4OPEN_LIMITSIZE:
1919 dp->nfsdl_sizelimit = fxdr_hyper(tl);
1921 case NFSV4OPEN_LIMITBLOCKS:
1922 dp->nfsdl_sizelimit =
1923 fxdr_unsigned(u_int64_t, *tl++);
1924 dp->nfsdl_sizelimit *=
1925 fxdr_unsigned(u_int64_t, *tl);
1928 error = NFSERR_BADXDR;
1932 dp->nfsdl_flags = NFSCLDL_READ;
1935 dp->nfsdl_flags |= NFSCLDL_RECALL;
1936 error = nfsrv_dissectace(nd, &dp->nfsdl_ace, &ret,
1940 } else if (deleg != NFSV4OPEN_DELEGATENONE) {
1941 error = NFSERR_BADXDR;
1944 error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp);
1947 if (dp != NULL && *attrflagp) {
1948 dp->nfsdl_change = nnap->na_filerev;
1949 dp->nfsdl_modtime = nnap->na_mtime;
1950 dp->nfsdl_flags |= NFSCLDL_MODTIMESET;
1953 * We can now complete the Open state.
1957 dp->nfsdl_fhlen = nfhp->nfh_len;
1958 NFSBCOPY(nfhp->nfh_fh, dp->nfsdl_fh, nfhp->nfh_len);
1961 * Get an Open structure that will be
1962 * attached to the OpenOwner, acquired already.
1964 error = nfscl_open(dvp, nfhp->nfh_fh, nfhp->nfh_len,
1965 (NFSV4OPEN_ACCESSWRITE | NFSV4OPEN_ACCESSREAD), 0,
1966 cred, p, NULL, &op, &newone, NULL, 0);
1969 op->nfso_stateid = stateid;
1970 newnfs_copyincred(cred, &op->nfso_cred);
1971 if ((rflags & NFSV4OPEN_RESULTCONFIRM)) {
1973 ret = nfsrpc_openconfirm(dvp, nfhp->nfh_fh,
1974 nfhp->nfh_len, op, cred, p);
1975 if (ret == NFSERR_DELAY)
1976 (void) nfs_catnap(PZERO, ret, "nfs_create");
1977 } while (ret == NFSERR_DELAY);
1982 * If the server is handing out delegations, but we didn't
1983 * get one because an OpenConfirm was required, try the
1984 * Open again, to get a delegation. This is a harmless no-op,
1985 * from a server's point of view.
1987 if ((rflags & NFSV4OPEN_RESULTCONFIRM) &&
1988 (owp->nfsow_clp->nfsc_flags & NFSCLFLAGS_GOTDELEG) &&
1989 !error && dp == NULL) {
1992 ret = nfsrpc_openrpc(VFSTONFS(vnode_mount(dvp)), dvp,
1993 np->n_fhp->nfh_fh, np->n_fhp->nfh_len,
1994 nfhp->nfh_fh, nfhp->nfh_len,
1995 (NFSV4OPEN_ACCESSWRITE | NFSV4OPEN_ACCESSREAD), op,
1996 name, namelen, &dp, 0, 0x0, cred, p, 0, 1);
1997 if (ret == NFSERR_DELAY)
1998 (void) nfs_catnap(PZERO, ret, "nfs_crt2");
1999 } while (ret == NFSERR_DELAY);
2002 FREE((caddr_t)dp, M_NFSCLDELEG);
2003 if (ret == NFSERR_STALECLIENTID ||
2004 ret == NFSERR_STALEDONTRECOVER)
2008 nfscl_openrelease(op, error, newone);
2011 if (nd->nd_repstat != 0 && error == 0)
2012 error = nd->nd_repstat;
2013 if (error == NFSERR_STALECLIENTID)
2014 nfscl_initiate_recovery(owp->nfsow_clp);
2018 else if (dp != NULL)
2019 FREE((caddr_t)dp, M_NFSCLDELEG);
2020 mbuf_freem(nd->nd_mrep);
2028 nfsrpc_remove(vnode_t dvp, char *name, int namelen, vnode_t vp,
2029 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap, int *dattrflagp,
2033 struct nfsrv_descript nfsd, *nd = &nfsd;
2035 struct nfsmount *nmp;
2036 nfsv4stateid_t dstateid;
2037 int error, ret = 0, i;
2040 if (namelen > NFS_MAXNAMLEN)
2041 return (ENAMETOOLONG);
2042 nmp = VFSTONFS(vnode_mount(dvp));
2044 if (NFSHASNFSV4(nmp) && ret == 0) {
2045 ret = nfscl_removedeleg(vp, p, &dstateid);
2047 NFSCL_REQSTART(nd, NFSPROC_RETDELEGREMOVE, vp);
2048 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID +
2050 *tl++ = dstateid.seqid;
2051 *tl++ = dstateid.other[0];
2052 *tl++ = dstateid.other[1];
2053 *tl++ = dstateid.other[2];
2054 *tl = txdr_unsigned(NFSV4OP_PUTFH);
2056 (void) nfsm_fhtom(nd, np->n_fhp->nfh_fh,
2057 np->n_fhp->nfh_len, 0);
2058 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2059 *tl = txdr_unsigned(NFSV4OP_REMOVE);
2065 NFSCL_REQSTART(nd, NFSPROC_REMOVE, dvp);
2066 (void) nfsm_strtom(nd, name, namelen);
2067 error = nfscl_request(nd, dvp, p, cred, dstuff);
2070 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) {
2071 /* For NFSv4, parse out any Delereturn replies. */
2072 if (ret > 0 && nd->nd_repstat != 0 &&
2073 (nd->nd_flag & ND_NOMOREDATA)) {
2075 * If the Delegreturn failed, try again without
2076 * it. The server will Recall, as required.
2078 mbuf_freem(nd->nd_mrep);
2081 for (i = 0; i < (ret * 2); i++) {
2082 if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) ==
2084 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2086 nd->nd_flag |= ND_NOMOREDATA;
2089 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
2091 if (nd->nd_repstat && !error)
2092 error = nd->nd_repstat;
2094 mbuf_freem(nd->nd_mrep);
2099 * Do an nfs rename rpc.
2102 nfsrpc_rename(vnode_t fdvp, vnode_t fvp, char *fnameptr, int fnamelen,
2103 vnode_t tdvp, vnode_t tvp, char *tnameptr, int tnamelen, struct ucred *cred,
2104 NFSPROC_T *p, struct nfsvattr *fnap, struct nfsvattr *tnap,
2105 int *fattrflagp, int *tattrflagp, void *fstuff, void *tstuff)
2108 struct nfsrv_descript nfsd, *nd = &nfsd;
2109 struct nfsmount *nmp;
2111 nfsattrbit_t attrbits;
2112 nfsv4stateid_t fdstateid, tdstateid;
2113 int error = 0, ret = 0, gottd = 0, gotfd = 0, i;
2117 nmp = VFSTONFS(vnode_mount(fdvp));
2118 if (fnamelen > NFS_MAXNAMLEN || tnamelen > NFS_MAXNAMLEN)
2119 return (ENAMETOOLONG);
2121 if (NFSHASNFSV4(nmp) && ret == 0) {
2122 ret = nfscl_renamedeleg(fvp, &fdstateid, &gotfd, tvp,
2123 &tdstateid, &gottd, p);
2124 if (gotfd && gottd) {
2125 NFSCL_REQSTART(nd, NFSPROC_RETDELEGRENAME2, fvp);
2127 NFSCL_REQSTART(nd, NFSPROC_RETDELEGRENAME1, fvp);
2129 NFSCL_REQSTART(nd, NFSPROC_RETDELEGRENAME1, tvp);
2132 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID);
2133 *tl++ = fdstateid.seqid;
2134 *tl++ = fdstateid.other[0];
2135 *tl++ = fdstateid.other[1];
2136 *tl = fdstateid.other[2];
2138 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2139 *tl = txdr_unsigned(NFSV4OP_PUTFH);
2141 (void) nfsm_fhtom(nd, np->n_fhp->nfh_fh,
2142 np->n_fhp->nfh_len, 0);
2143 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2144 *tl = txdr_unsigned(NFSV4OP_DELEGRETURN);
2148 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID);
2149 *tl++ = tdstateid.seqid;
2150 *tl++ = tdstateid.other[0];
2151 *tl++ = tdstateid.other[1];
2152 *tl = tdstateid.other[2];
2155 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2156 *tl = txdr_unsigned(NFSV4OP_PUTFH);
2158 (void) nfsm_fhtom(nd, np->n_fhp->nfh_fh,
2159 np->n_fhp->nfh_len, 0);
2160 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2161 *tl = txdr_unsigned(NFSV4OP_SAVEFH);
2167 NFSCL_REQSTART(nd, NFSPROC_RENAME, fdvp);
2168 if (nd->nd_flag & ND_NFSV4) {
2169 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2170 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2171 NFSWCCATTR_ATTRBIT(&attrbits);
2172 (void) nfsrv_putattrbit(nd, &attrbits);
2173 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2174 *tl = txdr_unsigned(NFSV4OP_PUTFH);
2175 (void) nfsm_fhtom(nd, VTONFS(tdvp)->n_fhp->nfh_fh,
2176 VTONFS(tdvp)->n_fhp->nfh_len, 0);
2177 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2178 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2179 (void) nfsrv_putattrbit(nd, &attrbits);
2180 nd->nd_flag |= ND_V4WCCATTR;
2181 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2182 *tl = txdr_unsigned(NFSV4OP_RENAME);
2184 (void) nfsm_strtom(nd, fnameptr, fnamelen);
2185 if (!(nd->nd_flag & ND_NFSV4))
2186 (void) nfsm_fhtom(nd, VTONFS(tdvp)->n_fhp->nfh_fh,
2187 VTONFS(tdvp)->n_fhp->nfh_len, 0);
2188 (void) nfsm_strtom(nd, tnameptr, tnamelen);
2189 error = nfscl_request(nd, fdvp, p, cred, fstuff);
2192 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) {
2193 /* For NFSv4, parse out any Delereturn replies. */
2194 if (ret > 0 && nd->nd_repstat != 0 &&
2195 (nd->nd_flag & ND_NOMOREDATA)) {
2197 * If the Delegreturn failed, try again without
2198 * it. The server will Recall, as required.
2200 mbuf_freem(nd->nd_mrep);
2203 for (i = 0; i < (ret * 2); i++) {
2204 if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) ==
2206 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2208 if (i == 0 && ret > 1) {
2210 * If the Delegreturn failed, try again
2211 * without it. The server will Recall, as
2213 * If ret > 1, the first iteration of this
2214 * loop is the second DelegReturn result.
2216 mbuf_freem(nd->nd_mrep);
2219 nd->nd_flag |= ND_NOMOREDATA;
2224 /* Now, the first wcc attribute reply. */
2225 if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) == ND_NFSV4) {
2226 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2228 nd->nd_flag |= ND_NOMOREDATA;
2230 error = nfscl_wcc_data(nd, fdvp, fnap, fattrflagp, NULL,
2232 /* and the second wcc attribute reply. */
2233 if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) == ND_NFSV4 &&
2235 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2237 nd->nd_flag |= ND_NOMOREDATA;
2240 error = nfscl_wcc_data(nd, tdvp, tnap, tattrflagp,
2243 if (nd->nd_repstat && !error)
2244 error = nd->nd_repstat;
2246 mbuf_freem(nd->nd_mrep);
2251 * nfs hard link create rpc
2254 nfsrpc_link(vnode_t dvp, vnode_t vp, char *name, int namelen,
2255 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap,
2256 struct nfsvattr *nap, int *attrflagp, int *dattrflagp, void *dstuff)
2259 struct nfsrv_descript nfsd, *nd = &nfsd;
2260 nfsattrbit_t attrbits;
2265 if (namelen > NFS_MAXNAMLEN)
2266 return (ENAMETOOLONG);
2267 NFSCL_REQSTART(nd, NFSPROC_LINK, vp);
2268 if (nd->nd_flag & ND_NFSV4) {
2269 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2270 *tl = txdr_unsigned(NFSV4OP_PUTFH);
2272 (void) nfsm_fhtom(nd, VTONFS(dvp)->n_fhp->nfh_fh,
2273 VTONFS(dvp)->n_fhp->nfh_len, 0);
2274 if (nd->nd_flag & ND_NFSV4) {
2275 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2276 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2277 NFSWCCATTR_ATTRBIT(&attrbits);
2278 (void) nfsrv_putattrbit(nd, &attrbits);
2279 nd->nd_flag |= ND_V4WCCATTR;
2280 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2281 *tl = txdr_unsigned(NFSV4OP_LINK);
2283 (void) nfsm_strtom(nd, name, namelen);
2284 error = nfscl_request(nd, vp, p, cred, dstuff);
2287 if (nd->nd_flag & ND_NFSV3) {
2288 error = nfscl_postop_attr(nd, nap, attrflagp, dstuff);
2290 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp,
2292 } else if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) == ND_NFSV4) {
2294 * First, parse out the PutFH and Getattr result.
2296 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2298 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2300 nd->nd_flag |= ND_NOMOREDATA;
2302 * Get the pre-op attributes.
2304 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
2306 if (nd->nd_repstat && !error)
2307 error = nd->nd_repstat;
2309 mbuf_freem(nd->nd_mrep);
2314 * nfs symbolic link create rpc
2317 nfsrpc_symlink(vnode_t dvp, char *name, int namelen, char *target,
2318 struct vattr *vap, struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap,
2319 struct nfsvattr *nnap, struct nfsfh **nfhpp, int *attrflagp,
2320 int *dattrflagp, void *dstuff)
2323 struct nfsrv_descript nfsd, *nd = &nfsd;
2324 struct nfsmount *nmp;
2325 int slen, error = 0;
2330 nmp = VFSTONFS(vnode_mount(dvp));
2331 slen = strlen(target);
2332 if (slen > NFS_MAXPATHLEN || namelen > NFS_MAXNAMLEN)
2333 return (ENAMETOOLONG);
2334 NFSCL_REQSTART(nd, NFSPROC_SYMLINK, dvp);
2335 if (nd->nd_flag & ND_NFSV4) {
2336 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2337 *tl = txdr_unsigned(NFLNK);
2338 (void) nfsm_strtom(nd, target, slen);
2340 (void) nfsm_strtom(nd, name, namelen);
2341 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4))
2342 nfscl_fillsattr(nd, vap, dvp, 0, 0);
2343 if (!(nd->nd_flag & ND_NFSV4))
2344 (void) nfsm_strtom(nd, target, slen);
2345 if (nd->nd_flag & ND_NFSV2)
2346 nfscl_fillsattr(nd, vap, dvp, NFSSATTR_SIZENEG1, 0);
2347 error = nfscl_request(nd, dvp, p, cred, dstuff);
2350 if (nd->nd_flag & ND_NFSV4)
2351 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
2352 if ((nd->nd_flag & ND_NFSV3) && !error) {
2353 if (!nd->nd_repstat)
2354 error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp);
2356 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp,
2359 if (nd->nd_repstat && !error)
2360 error = nd->nd_repstat;
2361 mbuf_freem(nd->nd_mrep);
2363 * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry.
2365 if (error == EEXIST)
2374 nfsrpc_mkdir(vnode_t dvp, char *name, int namelen, struct vattr *vap,
2375 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap,
2376 struct nfsvattr *nnap, struct nfsfh **nfhpp, int *attrflagp,
2377 int *dattrflagp, void *dstuff)
2380 struct nfsrv_descript nfsd, *nd = &nfsd;
2381 nfsattrbit_t attrbits;
2387 if (namelen > NFS_MAXNAMLEN)
2388 return (ENAMETOOLONG);
2389 NFSCL_REQSTART(nd, NFSPROC_MKDIR, dvp);
2390 if (nd->nd_flag & ND_NFSV4) {
2391 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2392 *tl = txdr_unsigned(NFDIR);
2394 (void) nfsm_strtom(nd, name, namelen);
2395 nfscl_fillsattr(nd, vap, dvp, NFSSATTR_SIZENEG1, 0);
2396 if (nd->nd_flag & ND_NFSV4) {
2397 NFSGETATTR_ATTRBIT(&attrbits);
2398 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2399 *tl++ = txdr_unsigned(NFSV4OP_GETFH);
2400 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2401 (void) nfsrv_putattrbit(nd, &attrbits);
2403 error = nfscl_request(nd, dvp, p, cred, dstuff);
2406 if (nd->nd_flag & ND_NFSV4)
2407 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
2408 if (!nd->nd_repstat && !error) {
2409 if (nd->nd_flag & ND_NFSV4) {
2410 NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
2411 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
2414 error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp);
2416 if ((nd->nd_flag & ND_NFSV3) && !error)
2417 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
2418 if (nd->nd_repstat && !error)
2419 error = nd->nd_repstat;
2421 mbuf_freem(nd->nd_mrep);
2423 * Kludge: Map EEXIST => 0 assuming that you have a reply to a retry.
2425 if (error == EEXIST)
2431 * nfs remove directory call
2434 nfsrpc_rmdir(vnode_t dvp, char *name, int namelen, struct ucred *cred,
2435 NFSPROC_T *p, struct nfsvattr *dnap, int *dattrflagp, void *dstuff)
2437 struct nfsrv_descript nfsd, *nd = &nfsd;
2441 if (namelen > NFS_MAXNAMLEN)
2442 return (ENAMETOOLONG);
2443 NFSCL_REQSTART(nd, NFSPROC_RMDIR, dvp);
2444 (void) nfsm_strtom(nd, name, namelen);
2445 error = nfscl_request(nd, dvp, p, cred, dstuff);
2448 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4))
2449 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
2450 if (nd->nd_repstat && !error)
2451 error = nd->nd_repstat;
2452 mbuf_freem(nd->nd_mrep);
2454 * Kludge: Map ENOENT => 0 assuming that you have a reply to a retry.
2456 if (error == ENOENT)
2463 * Always returns with either uio_resid unchanged, if you are at the
2464 * end of the directory, or uio_resid == 0, with all DIRBLKSIZ chunks
2466 * I felt this would allow caching of directory blocks more easily
2467 * than returning a pertially filled block.
2468 * Directory offset cookies:
2469 * Oh my, what to do with them...
2470 * I can think of three ways to deal with them:
2471 * 1 - have the layer above these RPCs maintain a map between logical
2472 * directory byte offsets and the NFS directory offset cookies
2473 * 2 - pass the opaque directory offset cookies up into userland
2474 * and let the libc functions deal with them, via the system call
2475 * 3 - return them to userland in the "struct dirent", so future versions
2476 * of libc can use them and do whatever is necessary to amke things work
2477 * above these rpc calls, in the meantime
2478 * For now, I do #3 by "hiding" the directory offset cookies after the
2479 * d_name field in struct dirent. This is space inside d_reclen that
2480 * will be ignored by anything that doesn't know about them.
2481 * The directory offset cookies are filled in as the last 8 bytes of
2482 * each directory entry, after d_name. Someday, the userland libc
2483 * functions may be able to use these. In the meantime, it satisfies
2484 * OpenBSD's requirements for cookies being returned.
2485 * If expects the directory offset cookie for the read to be in uio_offset
2486 * and returns the one for the next entry after this directory block in
2490 nfsrpc_readdir(vnode_t vp, struct uio *uiop, nfsuint64 *cookiep,
2491 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp,
2492 int *eofp, void *stuff)
2495 struct dirent *dp = NULL;
2497 nfsquad_t cookie, ncookie;
2498 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
2499 struct nfsnode *dnp = VTONFS(vp);
2500 struct nfsvattr nfsva;
2501 struct nfsrv_descript nfsd, *nd = &nfsd;
2502 int error = 0, tlen, more_dirs = 1, blksiz = 0, bigenough = 1;
2503 int reqsize, tryformoredirs = 1, readsize, eof = 0, gotmnton = 0;
2504 long dotfileid, dotdotfileid = 0;
2505 u_int32_t fakefileno = 0xffffffff, rderr;
2507 nfsattrbit_t attrbits, dattrbits;
2508 u_int32_t *tl2 = NULL;
2512 if (uiop->uio_iovcnt != 1 || (uio_uio_resid(uiop) & (DIRBLKSIZ - 1)))
2513 panic("nfs readdirrpc bad uio");
2517 * There is no point in reading a lot more than uio_resid, however
2518 * adding one additional DIRBLKSIZ makes sense. Since uio_resid
2519 * and nm_readdirsize are both exact multiples of DIRBLKSIZ, this
2520 * will never make readsize > nm_readdirsize.
2522 readsize = nmp->nm_readdirsize;
2523 if (readsize > uio_uio_resid(uiop))
2524 readsize = uio_uio_resid(uiop) + DIRBLKSIZ;
2529 tresid = uio_uio_resid(uiop);
2530 cookie.lval[0] = cookiep->nfsuquad[0];
2531 cookie.lval[1] = cookiep->nfsuquad[1];
2535 * For NFSv4, first create the "." and ".." entries.
2537 if (NFSHASNFSV4(nmp)) {
2538 reqsize = 6 * NFSX_UNSIGNED;
2539 NFSGETATTR_ATTRBIT(&dattrbits);
2540 NFSZERO_ATTRBIT(&attrbits);
2541 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_FILEID);
2542 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TYPE);
2543 if (NFSISSET_ATTRBIT(&dnp->n_vattr.na_suppattr,
2544 NFSATTRBIT_MOUNTEDONFILEID)) {
2545 NFSSETBIT_ATTRBIT(&attrbits,
2546 NFSATTRBIT_MOUNTEDONFILEID);
2550 * Must fake it. Use the fileno, except when the
2551 * fsid is != to that of the directory. For that
2552 * case, generate a fake fileno that is not the same.
2554 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_FSID);
2559 * Joy, oh joy. For V4 we get to hand craft '.' and '..'.
2561 if (uiop->uio_offset == 0) {
2562 #if defined(__FreeBSD_version) && __FreeBSD_version >= 800000
2563 error = VOP_GETATTR(vp, &nfsva.na_vattr, cred);
2565 error = VOP_GETATTR(vp, &nfsva.na_vattr, cred, p);
2569 dotfileid = nfsva.na_fileid;
2570 NFSCL_REQSTART(nd, NFSPROC_LOOKUPP, vp);
2571 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2572 *tl++ = txdr_unsigned(NFSV4OP_GETFH);
2573 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2574 (void) nfsrv_putattrbit(nd, &attrbits);
2575 error = nfscl_request(nd, vp, p, cred, stuff);
2578 if (nd->nd_repstat == 0) {
2579 NFSM_DISSECT(tl, u_int32_t *, 3*NFSX_UNSIGNED);
2580 len = fxdr_unsigned(int, *(tl + 2));
2581 if (len > 0 && len <= NFSX_V4FHMAX)
2582 error = nfsm_advance(nd, NFSM_RNDUP(len), -1);
2586 NFSM_DISSECT(tl, u_int32_t *, 2*NFSX_UNSIGNED);
2587 nfsva.na_mntonfileno = 0xffffffff;
2588 error = nfsv4_loadattr(nd, NULL, &nfsva, NULL,
2589 NULL, 0, NULL, NULL, NULL, NULL, NULL, 0,
2590 NULL, NULL, NULL, p, cred);
2592 dotdotfileid = dotfileid;
2593 } else if (gotmnton) {
2594 if (nfsva.na_mntonfileno != 0xffffffff)
2595 dotdotfileid = nfsva.na_mntonfileno;
2597 dotdotfileid = nfsva.na_fileid;
2598 } else if (nfsva.na_filesid[0] ==
2599 dnp->n_vattr.na_filesid[0] &&
2600 nfsva.na_filesid[1] ==
2601 dnp->n_vattr.na_filesid[1]) {
2602 dotdotfileid = nfsva.na_fileid;
2606 } while (fakefileno ==
2608 dotdotfileid = fakefileno;
2611 } else if (nd->nd_repstat == NFSERR_NOENT) {
2613 * Lookupp returns NFSERR_NOENT when we are
2614 * at the root, so just use the current dir.
2617 dotdotfileid = dotfileid;
2619 error = nd->nd_repstat;
2621 mbuf_freem(nd->nd_mrep);
2625 dp = (struct dirent *) CAST_DOWN(caddr_t, uio_iov_base(uiop));
2626 dp->d_type = DT_DIR;
2627 dp->d_fileno = dotfileid;
2629 dp->d_name[0] = '.';
2630 dp->d_name[1] = '\0';
2631 dp->d_reclen = DIRENT_SIZE(dp) + NFSX_HYPER;
2633 * Just make these offset cookie 0.
2635 tl = (u_int32_t *)&dp->d_name[4];
2638 blksiz += dp->d_reclen;
2639 uio_uio_resid_add(uiop, -(dp->d_reclen));
2640 uiop->uio_offset += dp->d_reclen;
2641 uio_iov_base_add(uiop, dp->d_reclen);
2642 uio_iov_len_add(uiop, -(dp->d_reclen));
2643 dp = (struct dirent *) CAST_DOWN(caddr_t, uio_iov_base(uiop));
2644 dp->d_type = DT_DIR;
2645 dp->d_fileno = dotdotfileid;
2647 dp->d_name[0] = '.';
2648 dp->d_name[1] = '.';
2649 dp->d_name[2] = '\0';
2650 dp->d_reclen = DIRENT_SIZE(dp) + NFSX_HYPER;
2652 * Just make these offset cookie 0.
2654 tl = (u_int32_t *)&dp->d_name[4];
2657 blksiz += dp->d_reclen;
2658 uio_uio_resid_add(uiop, -(dp->d_reclen));
2659 uiop->uio_offset += dp->d_reclen;
2660 uio_iov_base_add(uiop, dp->d_reclen);
2661 uio_iov_len_add(uiop, -(dp->d_reclen));
2663 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_RDATTRERROR);
2665 reqsize = 5 * NFSX_UNSIGNED;
2670 * Loop around doing readdir rpc's of size readsize.
2671 * The stopping criteria is EOF or buffer full.
2673 while (more_dirs && bigenough) {
2675 NFSCL_REQSTART(nd, NFSPROC_READDIR, vp);
2676 if (nd->nd_flag & ND_NFSV2) {
2677 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2678 *tl++ = cookie.lval[1];
2679 *tl = txdr_unsigned(readsize);
2681 NFSM_BUILD(tl, u_int32_t *, reqsize);
2682 *tl++ = cookie.lval[0];
2683 *tl++ = cookie.lval[1];
2684 if (cookie.qval == 0) {
2689 *tl++ = dnp->n_cookieverf.nfsuquad[0];
2690 *tl++ = dnp->n_cookieverf.nfsuquad[1];
2693 if (nd->nd_flag & ND_NFSV4) {
2694 *tl++ = txdr_unsigned(readsize);
2695 *tl = txdr_unsigned(readsize);
2696 (void) nfsrv_putattrbit(nd, &attrbits);
2697 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2698 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2699 (void) nfsrv_putattrbit(nd, &dattrbits);
2701 *tl = txdr_unsigned(readsize);
2704 error = nfscl_request(nd, vp, p, cred, stuff);
2707 if (!(nd->nd_flag & ND_NFSV2)) {
2708 if (nd->nd_flag & ND_NFSV3)
2709 error = nfscl_postop_attr(nd, nap, attrflagp,
2711 if (!nd->nd_repstat && !error) {
2712 NFSM_DISSECT(tl, u_int32_t *, NFSX_HYPER);
2714 dnp->n_cookieverf.nfsuquad[0] = *tl++;
2715 dnp->n_cookieverf.nfsuquad[1] = *tl;
2719 if (nd->nd_repstat || error) {
2721 error = nd->nd_repstat;
2724 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2725 more_dirs = fxdr_unsigned(int, *tl);
2729 /* loop thru the dir entries, doctoring them to 4bsd form */
2730 while (more_dirs && bigenough) {
2731 if (nd->nd_flag & ND_NFSV4) {
2732 NFSM_DISSECT(tl, u_int32_t *, 3*NFSX_UNSIGNED);
2733 ncookie.lval[0] = *tl++;
2734 ncookie.lval[1] = *tl++;
2735 len = fxdr_unsigned(int, *tl);
2736 } else if (nd->nd_flag & ND_NFSV3) {
2737 NFSM_DISSECT(tl, u_int32_t *, 3*NFSX_UNSIGNED);
2739 fxdr_unsigned(long, *++tl);
2740 len = fxdr_unsigned(int, *++tl);
2742 NFSM_DISSECT(tl, u_int32_t *, 2*NFSX_UNSIGNED);
2744 fxdr_unsigned(long, *tl++);
2745 len = fxdr_unsigned(int, *tl);
2747 if (len <= 0 || len > NFS_MAXNAMLEN) {
2751 tlen = NFSM_RNDUP(len);
2753 tlen += 4; /* To ensure null termination */
2754 left = DIRBLKSIZ - blksiz;
2755 if ((int)(tlen + DIRHDSIZ + NFSX_HYPER) > left) {
2756 dp->d_reclen += left;
2757 uio_iov_base_add(uiop, left);
2758 uio_iov_len_add(uiop, -(left));
2759 uio_uio_resid_add(uiop, -(left));
2760 uiop->uio_offset += left;
2763 if ((int)(tlen + DIRHDSIZ + NFSX_HYPER) > uio_uio_resid(uiop))
2766 dp = (struct dirent *) CAST_DOWN(caddr_t, uio_iov_base(uiop));
2768 dp->d_reclen = tlen + DIRHDSIZ + NFSX_HYPER;
2769 dp->d_type = DT_UNKNOWN;
2770 blksiz += dp->d_reclen;
2771 if (blksiz == DIRBLKSIZ)
2773 uio_uio_resid_add(uiop, -(DIRHDSIZ));
2774 uiop->uio_offset += DIRHDSIZ;
2775 uio_iov_base_add(uiop, DIRHDSIZ);
2776 uio_iov_len_add(uiop, -(DIRHDSIZ));
2777 error = nfsm_mbufuio(nd, uiop, len);
2780 cp = CAST_DOWN(caddr_t, uio_iov_base(uiop));
2782 *cp = '\0'; /* null terminate */
2783 cp += tlen; /* points to cookie storage */
2784 tl2 = (u_int32_t *)cp;
2785 uio_iov_base_add(uiop, (tlen + NFSX_HYPER));
2786 uio_iov_len_add(uiop, -(tlen + NFSX_HYPER));
2787 uio_uio_resid_add(uiop, -(tlen + NFSX_HYPER));
2788 uiop->uio_offset += (tlen + NFSX_HYPER);
2790 error = nfsm_advance(nd, NFSM_RNDUP(len), -1);
2794 if (nd->nd_flag & ND_NFSV4) {
2796 nfsva.na_mntonfileno = 0xffffffff;
2797 error = nfsv4_loadattr(nd, NULL, &nfsva, NULL,
2798 NULL, 0, NULL, NULL, NULL, NULL, NULL, 0,
2799 NULL, NULL, &rderr, p, cred);
2802 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2803 } else if (nd->nd_flag & ND_NFSV3) {
2804 NFSM_DISSECT(tl, u_int32_t *, 3*NFSX_UNSIGNED);
2805 ncookie.lval[0] = *tl++;
2806 ncookie.lval[1] = *tl++;
2808 NFSM_DISSECT(tl, u_int32_t *, 2*NFSX_UNSIGNED);
2809 ncookie.lval[0] = 0;
2810 ncookie.lval[1] = *tl++;
2813 if (nd->nd_flag & ND_NFSV4) {
2818 if (nfsva.na_mntonfileno != 0xffffffff)
2819 dp->d_fileno = nfsva.na_mntonfileno;
2821 dp->d_fileno = nfsva.na_fileid;
2822 } else if (nfsva.na_filesid[0] ==
2823 dnp->n_vattr.na_filesid[0] &&
2824 nfsva.na_filesid[1] ==
2825 dnp->n_vattr.na_filesid[1]) {
2826 dp->d_fileno = nfsva.na_fileid;
2830 } while (fakefileno ==
2832 dp->d_fileno = fakefileno;
2834 dp->d_type = vtonfs_dtype(nfsva.na_type);
2837 dp->d_fileno = nfsva.na_fileid;
2839 *tl2++ = cookiep->nfsuquad[0] = cookie.lval[0] =
2841 *tl2 = cookiep->nfsuquad[1] = cookie.lval[1] =
2844 more_dirs = fxdr_unsigned(int, *tl);
2847 * If at end of rpc data, get the eof boolean
2850 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2851 eof = fxdr_unsigned(int, *tl);
2854 if (nd->nd_flag & ND_NFSV4) {
2855 error = nfscl_postop_attr(nd, nap, attrflagp,
2861 mbuf_freem(nd->nd_mrep);
2865 * Fill last record, iff any, out to a multiple of DIRBLKSIZ
2866 * by increasing d_reclen for the last record.
2869 left = DIRBLKSIZ - blksiz;
2870 dp->d_reclen += left;
2871 uio_iov_base_add(uiop, left);
2872 uio_iov_len_add(uiop, -(left));
2873 uio_uio_resid_add(uiop, -(left));
2874 uiop->uio_offset += left;
2878 * If returning no data, assume end of file.
2879 * If not bigenough, return not end of file, since you aren't
2880 * returning all the data
2881 * Otherwise, return the eof flag from the server.
2884 if (tresid == ((size_t)(uio_uio_resid(uiop))))
2886 else if (!bigenough)
2893 * Add extra empty records to any remaining DIRBLKSIZ chunks.
2895 while (uio_uio_resid(uiop) > 0 && ((size_t)(uio_uio_resid(uiop))) != tresid) {
2896 dp = (struct dirent *) CAST_DOWN(caddr_t, uio_iov_base(uiop));
2897 dp->d_type = DT_UNKNOWN;
2900 dp->d_name[0] = '\0';
2901 tl = (u_int32_t *)&dp->d_name[4];
2902 *tl++ = cookie.lval[0];
2903 *tl = cookie.lval[1];
2904 dp->d_reclen = DIRBLKSIZ;
2905 uio_iov_base_add(uiop, DIRBLKSIZ);
2906 uio_iov_len_add(uiop, -(DIRBLKSIZ));
2907 uio_uio_resid_add(uiop, -(DIRBLKSIZ));
2908 uiop->uio_offset += DIRBLKSIZ;
2912 if (nd->nd_mrep != NULL)
2913 mbuf_freem(nd->nd_mrep);
2919 * NFS V3 readdir plus RPC. Used in place of nfsrpc_readdir().
2920 * (Also used for NFS V4 when mount flag set.)
2921 * (ditto above w.r.t. multiple of DIRBLKSIZ, etc.)
2924 nfsrpc_readdirplus(vnode_t vp, struct uio *uiop, nfsuint64 *cookiep,
2925 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp,
2926 int *eofp, void *stuff)
2929 struct dirent *dp = NULL;
2931 vnode_t newvp = NULLVP;
2932 struct nfsrv_descript nfsd, *nd = &nfsd;
2933 struct nameidata nami, *ndp = &nami;
2934 struct componentname *cnp = &ndp->ni_cnd;
2935 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
2936 struct nfsnode *dnp = VTONFS(vp), *np;
2937 struct nfsvattr nfsva;
2939 nfsquad_t cookie, ncookie;
2940 int error = 0, tlen, more_dirs = 1, blksiz = 0, bigenough = 1;
2941 int attrflag, tryformoredirs = 1, eof = 0, gotmnton = 0;
2942 int unlocknewvp = 0;
2943 long dotfileid, dotdotfileid = 0, fileno = 0;
2945 nfsattrbit_t attrbits, dattrbits;
2947 u_int32_t *tl2 = NULL, fakefileno = 0xffffffff, rderr;
2950 if (uiop->uio_iovcnt != 1 || (uio_uio_resid(uiop) & (DIRBLKSIZ - 1)))
2951 panic("nfs readdirplusrpc bad uio");
2958 cookie.lval[0] = cookiep->nfsuquad[0];
2959 cookie.lval[1] = cookiep->nfsuquad[1];
2960 tresid = uio_uio_resid(uiop);
2963 * For NFSv4, first create the "." and ".." entries.
2965 if (NFSHASNFSV4(nmp)) {
2966 NFSGETATTR_ATTRBIT(&dattrbits);
2967 NFSZERO_ATTRBIT(&attrbits);
2968 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_FILEID);
2969 if (NFSISSET_ATTRBIT(&dnp->n_vattr.na_suppattr,
2970 NFSATTRBIT_MOUNTEDONFILEID)) {
2971 NFSSETBIT_ATTRBIT(&attrbits,
2972 NFSATTRBIT_MOUNTEDONFILEID);
2976 * Must fake it. Use the fileno, except when the
2977 * fsid is != to that of the directory. For that
2978 * case, generate a fake fileno that is not the same.
2980 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_FSID);
2985 * Joy, oh joy. For V4 we get to hand craft '.' and '..'.
2987 if (uiop->uio_offset == 0) {
2988 #if defined(__FreeBSD_version) && __FreeBSD_version >= 800000
2989 error = VOP_GETATTR(vp, &nfsva.na_vattr, cred);
2991 error = VOP_GETATTR(vp, &nfsva.na_vattr, cred, p);
2995 dotfileid = nfsva.na_fileid;
2996 NFSCL_REQSTART(nd, NFSPROC_LOOKUPP, vp);
2997 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2998 *tl++ = txdr_unsigned(NFSV4OP_GETFH);
2999 *tl = txdr_unsigned(NFSV4OP_GETATTR);
3000 (void) nfsrv_putattrbit(nd, &attrbits);
3001 error = nfscl_request(nd, vp, p, cred, stuff);
3004 if (nd->nd_repstat == 0) {
3005 NFSM_DISSECT(tl, u_int32_t *, 3*NFSX_UNSIGNED);
3006 len = fxdr_unsigned(int, *(tl + 2));
3007 if (len > 0 && len <= NFSX_V4FHMAX)
3008 error = nfsm_advance(nd, NFSM_RNDUP(len), -1);
3012 NFSM_DISSECT(tl, u_int32_t *, 2*NFSX_UNSIGNED);
3013 nfsva.na_mntonfileno = 0xffffffff;
3014 error = nfsv4_loadattr(nd, NULL, &nfsva, NULL,
3015 NULL, 0, NULL, NULL, NULL, NULL, NULL, 0,
3016 NULL, NULL, NULL, p, cred);
3018 dotdotfileid = dotfileid;
3019 } else if (gotmnton) {
3020 if (nfsva.na_mntonfileno != 0xffffffff)
3021 dotdotfileid = nfsva.na_mntonfileno;
3023 dotdotfileid = nfsva.na_fileid;
3024 } else if (nfsva.na_filesid[0] ==
3025 dnp->n_vattr.na_filesid[0] &&
3026 nfsva.na_filesid[1] ==
3027 dnp->n_vattr.na_filesid[1]) {
3028 dotdotfileid = nfsva.na_fileid;
3032 } while (fakefileno ==
3034 dotdotfileid = fakefileno;
3037 } else if (nd->nd_repstat == NFSERR_NOENT) {
3039 * Lookupp returns NFSERR_NOENT when we are
3040 * at the root, so just use the current dir.
3043 dotdotfileid = dotfileid;
3045 error = nd->nd_repstat;
3047 mbuf_freem(nd->nd_mrep);
3051 dp = (struct dirent *)uio_iov_base(uiop);
3052 dp->d_type = DT_DIR;
3053 dp->d_fileno = dotfileid;
3055 dp->d_name[0] = '.';
3056 dp->d_name[1] = '\0';
3057 dp->d_reclen = DIRENT_SIZE(dp) + NFSX_HYPER;
3059 * Just make these offset cookie 0.
3061 tl = (u_int32_t *)&dp->d_name[4];
3064 blksiz += dp->d_reclen;
3065 uio_uio_resid_add(uiop, -(dp->d_reclen));
3066 uiop->uio_offset += dp->d_reclen;
3067 uio_iov_base_add(uiop, dp->d_reclen);
3068 uio_iov_len_add(uiop, -(dp->d_reclen));
3069 dp = (struct dirent *)uio_iov_base(uiop);
3070 dp->d_type = DT_DIR;
3071 dp->d_fileno = dotdotfileid;
3073 dp->d_name[0] = '.';
3074 dp->d_name[1] = '.';
3075 dp->d_name[2] = '\0';
3076 dp->d_reclen = DIRENT_SIZE(dp) + NFSX_HYPER;
3078 * Just make these offset cookie 0.
3080 tl = (u_int32_t *)&dp->d_name[4];
3083 blksiz += dp->d_reclen;
3084 uio_uio_resid_add(uiop, -(dp->d_reclen));
3085 uiop->uio_offset += dp->d_reclen;
3086 uio_iov_base_add(uiop, dp->d_reclen);
3087 uio_iov_len_add(uiop, -(dp->d_reclen));
3089 NFSREADDIRPLUS_ATTRBIT(&attrbits);
3091 NFSSETBIT_ATTRBIT(&attrbits,
3092 NFSATTRBIT_MOUNTEDONFILEID);
3096 * Loop around doing readdir rpc's of size nm_readdirsize.
3097 * The stopping criteria is EOF or buffer full.
3099 while (more_dirs && bigenough) {
3101 NFSCL_REQSTART(nd, NFSPROC_READDIRPLUS, vp);
3102 NFSM_BUILD(tl, u_int32_t *, 6 * NFSX_UNSIGNED);
3103 *tl++ = cookie.lval[0];
3104 *tl++ = cookie.lval[1];
3105 if (cookie.qval == 0) {
3110 *tl++ = dnp->n_cookieverf.nfsuquad[0];
3111 *tl++ = dnp->n_cookieverf.nfsuquad[1];
3114 *tl++ = txdr_unsigned(nmp->nm_readdirsize);
3115 *tl = txdr_unsigned(nmp->nm_readdirsize);
3116 if (nd->nd_flag & ND_NFSV4) {
3117 (void) nfsrv_putattrbit(nd, &attrbits);
3118 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
3119 *tl = txdr_unsigned(NFSV4OP_GETATTR);
3120 (void) nfsrv_putattrbit(nd, &dattrbits);
3122 error = nfscl_request(nd, vp, p, cred, stuff);
3125 if (nd->nd_flag & ND_NFSV3)
3126 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
3127 if (nd->nd_repstat || error) {
3129 error = nd->nd_repstat;
3132 NFSM_DISSECT(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
3134 dnp->n_cookieverf.nfsuquad[0] = *tl++;
3135 dnp->n_cookieverf.nfsuquad[1] = *tl++;
3137 more_dirs = fxdr_unsigned(int, *tl);
3141 /* loop thru the dir entries, doctoring them to 4bsd form */
3142 while (more_dirs && bigenough) {
3143 NFSM_DISSECT(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
3144 if (nd->nd_flag & ND_NFSV4) {
3145 ncookie.lval[0] = *tl++;
3146 ncookie.lval[1] = *tl++;
3148 fileno = fxdr_unsigned(long, *++tl);
3151 len = fxdr_unsigned(int, *tl);
3152 if (len <= 0 || len > NFS_MAXNAMLEN) {
3156 tlen = NFSM_RNDUP(len);
3158 tlen += 4; /* To ensure null termination */
3159 left = DIRBLKSIZ - blksiz;
3160 if ((tlen + DIRHDSIZ + NFSX_HYPER) > left) {
3161 dp->d_reclen += left;
3162 uio_iov_base_add(uiop, left);
3163 uio_iov_len_add(uiop, -(left));
3164 uio_uio_resid_add(uiop, -(left));
3165 uiop->uio_offset += left;
3168 if ((tlen + DIRHDSIZ + NFSX_HYPER) > uio_uio_resid(uiop))
3171 dp = (struct dirent *)uio_iov_base(uiop);
3173 dp->d_reclen = tlen + DIRHDSIZ + NFSX_HYPER;
3174 dp->d_type = DT_UNKNOWN;
3175 blksiz += dp->d_reclen;
3176 if (blksiz == DIRBLKSIZ)
3178 uio_uio_resid_add(uiop, -(DIRHDSIZ));
3179 uiop->uio_offset += DIRHDSIZ;
3180 uio_iov_base_add(uiop, DIRHDSIZ);
3181 uio_iov_len_add(uiop, -(DIRHDSIZ));
3182 cnp->cn_nameptr = uio_iov_base(uiop);
3183 cnp->cn_namelen = len;
3185 error = nfsm_mbufuio(nd, uiop, len);
3188 cp = uio_iov_base(uiop);
3191 cp += tlen; /* points to cookie storage */
3192 tl2 = (u_int32_t *)cp;
3193 uio_iov_base_add(uiop, (tlen + NFSX_HYPER));
3194 uio_iov_len_add(uiop, -(tlen + NFSX_HYPER));
3195 uio_uio_resid_add(uiop, -(tlen + NFSX_HYPER));
3196 uiop->uio_offset += (tlen + NFSX_HYPER);
3198 error = nfsm_advance(nd, NFSM_RNDUP(len), -1);
3203 if (nd->nd_flag & ND_NFSV3) {
3204 NFSM_DISSECT(tl, u_int32_t *, 3*NFSX_UNSIGNED);
3205 ncookie.lval[0] = *tl++;
3206 ncookie.lval[1] = *tl++;
3207 attrflag = fxdr_unsigned(int, *tl);
3209 error = nfsm_loadattr(nd, &nfsva);
3213 NFSM_DISSECT(tl,u_int32_t *,NFSX_UNSIGNED);
3215 error = nfsm_getfh(nd, &nfhp);
3219 if (!attrflag && nfhp != NULL) {
3220 FREE((caddr_t)nfhp, M_NFSFH);
3225 nfsva.na_mntonfileno = 0xffffffff;
3226 error = nfsv4_loadattr(nd, NULL, &nfsva, &nfhp,
3227 NULL, 0, NULL, NULL, NULL, NULL, NULL, 0,
3228 NULL, NULL, &rderr, p, cred);
3234 if (nd->nd_flag & ND_NFSV4) {
3237 } else if (gotmnton) {
3238 if (nfsva.na_mntonfileno != 0xffffffff)
3239 dp->d_fileno = nfsva.na_mntonfileno;
3241 dp->d_fileno = nfsva.na_fileid;
3242 } else if (nfsva.na_filesid[0] ==
3243 dnp->n_vattr.na_filesid[0] &&
3244 nfsva.na_filesid[1] ==
3245 dnp->n_vattr.na_filesid[1]) {
3246 dp->d_fileno = nfsva.na_fileid;
3250 } while (fakefileno ==
3252 dp->d_fileno = fakefileno;
3255 dp->d_fileno = fileno;
3257 *tl2++ = cookiep->nfsuquad[0] = cookie.lval[0] =
3259 *tl2 = cookiep->nfsuquad[1] = cookie.lval[1] =
3263 if (NFSRV_CMPFH(nfhp->nfh_fh, nfhp->nfh_len,
3264 dnp->n_fhp->nfh_fh, dnp->n_fhp->nfh_len)) {
3268 FREE((caddr_t)nfhp, M_NFSFH);
3271 error = nfscl_nget(vnode_mount(vp), vp,
3272 nfhp, cnp, p, &np, NULL);
3279 if (newvp != NULLVP) {
3280 error = nfscl_loadattrcache(&newvp,
3281 &nfsva, NULL, NULL, 0, 0);
3290 vtonfs_dtype(np->n_vattr.na_type);
3292 NFSCNHASH(cnp, HASHINIT);
3293 if (cnp->cn_namelen <= NCHNAMLEN) {
3295 np->n_vattr.na_ctime.tv_sec;
3296 cache_enter(ndp->ni_dvp,ndp->ni_vp,cnp);
3305 } else if (nfhp != NULL) {
3306 FREE((caddr_t)nfhp, M_NFSFH);
3308 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3309 more_dirs = fxdr_unsigned(int, *tl);
3312 * If at end of rpc data, get the eof boolean
3315 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3316 eof = fxdr_unsigned(int, *tl);
3319 if (nd->nd_flag & ND_NFSV4) {
3320 error = nfscl_postop_attr(nd, nap, attrflagp,
3326 mbuf_freem(nd->nd_mrep);
3330 * Fill last record, iff any, out to a multiple of DIRBLKSIZ
3331 * by increasing d_reclen for the last record.
3334 left = DIRBLKSIZ - blksiz;
3335 dp->d_reclen += left;
3336 uio_iov_base_add(uiop, left);
3337 uio_iov_len_add(uiop, -(left));
3338 uio_uio_resid_add(uiop, -(left));
3339 uiop->uio_offset += left;
3343 * If returning no data, assume end of file.
3344 * If not bigenough, return not end of file, since you aren't
3345 * returning all the data
3346 * Otherwise, return the eof flag from the server.
3349 if (tresid == uio_uio_resid(uiop))
3351 else if (!bigenough)
3358 * Add extra empty records to any remaining DIRBLKSIZ chunks.
3360 while (uio_uio_resid(uiop) > 0 && uio_uio_resid(uiop) != tresid) {
3361 dp = (struct dirent *)uio_iov_base(uiop);
3362 dp->d_type = DT_UNKNOWN;
3365 dp->d_name[0] = '\0';
3366 tl = (u_int32_t *)&dp->d_name[4];
3367 *tl++ = cookie.lval[0];
3368 *tl = cookie.lval[1];
3369 dp->d_reclen = DIRBLKSIZ;
3370 uio_iov_base_add(uiop, DIRBLKSIZ);
3371 uio_iov_len_add(uiop, -(DIRBLKSIZ));
3372 uio_uio_resid_add(uiop, -(DIRBLKSIZ));
3373 uiop->uio_offset += DIRBLKSIZ;
3377 if (nd->nd_mrep != NULL)
3378 mbuf_freem(nd->nd_mrep);
3387 nfsrpc_commit(vnode_t vp, u_quad_t offset, int cnt, struct ucred *cred,
3388 NFSPROC_T *p, u_char *verfp, struct nfsvattr *nap, int *attrflagp,
3392 struct nfsrv_descript nfsd, *nd = &nfsd;
3393 nfsattrbit_t attrbits;
3397 NFSCL_REQSTART(nd, NFSPROC_COMMIT, vp);
3398 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
3399 txdr_hyper(offset, tl);
3401 *tl = txdr_unsigned(cnt);
3402 if (nd->nd_flag & ND_NFSV4) {
3404 * And do a Getattr op.
3406 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
3407 *tl = txdr_unsigned(NFSV4OP_GETATTR);
3408 NFSGETATTR_ATTRBIT(&attrbits);
3409 (void) nfsrv_putattrbit(nd, &attrbits);
3411 error = nfscl_request(nd, vp, p, cred, stuff);
3414 error = nfscl_wcc_data(nd, vp, nap, attrflagp, NULL, stuff);
3415 if (!error && !nd->nd_repstat) {
3416 NFSM_DISSECT(tl, u_int32_t *, NFSX_VERF);
3417 NFSBCOPY((caddr_t)tl, verfp, NFSX_VERF);
3418 if (nd->nd_flag & ND_NFSV4)
3419 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
3422 if (!error && nd->nd_repstat)
3423 error = nd->nd_repstat;
3424 mbuf_freem(nd->nd_mrep);
3429 * NFS byte range lock rpc.
3430 * (Mostly just calls one of the three lower level RPC routines.)
3433 nfsrpc_advlock(vnode_t vp, off_t size, int op, struct flock *fl,
3434 int reclaim, struct ucred *cred, NFSPROC_T *p)
3436 struct nfscllockowner *lp;
3437 struct nfsclclient *clp;
3439 struct nfsrv_descript nfsd, *nd = &nfsd;
3440 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
3443 u_int32_t clidrev = 0;
3444 int error = 0, newone = 0, expireret = 0, retrycnt, donelocally;
3448 * Convert the flock structure into a start and end and do POSIX
3451 switch (fl->l_whence) {
3455 * Caller is responsible for adding any necessary offset
3456 * when SEEK_CUR is used.
3458 start = fl->l_start;
3462 start = size + fl->l_start;
3463 off = size + fl->l_start;
3470 if (fl->l_len != 0) {
3471 end = start + fl->l_len - 1;
3482 if (op == F_GETLK) {
3483 error = nfscl_getcl(vp, cred, p, &clp);
3486 error = nfscl_lockt(vp, clp, off, len, fl, p);
3488 clidrev = clp->nfsc_clientidrev;
3489 error = nfsrpc_lockt(nd, vp, clp, off, len, fl, cred,
3491 } else if (error == -1) {
3494 nfscl_clientrelease(clp);
3495 } else if (op == F_UNLCK && fl->l_type == F_UNLCK) {
3497 * We must loop around for all lockowner cases.
3500 error = nfscl_getcl(vp, cred, p, &clp);
3504 error = nfscl_relbytelock(vp, off, len, cred, p, callcnt,
3507 * If it returns a NULL lp, we're done.
3511 nfscl_clientrelease(clp);
3513 nfscl_releasealllocks(clp, vp, p);
3516 if (nmp->nm_clp != NULL)
3517 clidrev = nmp->nm_clp->nfsc_clientidrev;
3521 * If the server doesn't support Posix lock semantics,
3522 * only allow locks on the entire file, since it won't
3523 * handle overlapping byte ranges.
3524 * There might still be a problem when a lock
3525 * upgrade/downgrade (read<->write) occurs, since the
3526 * server "might" expect an unlock first?
3528 if (dorpc && (lp->nfsl_open->nfso_posixlock ||
3529 (off == 0 && len == NFS64BITSSET))) {
3531 * Since the lock records will go away, we must
3532 * wait for grace and delay here.
3535 error = nfsrpc_locku(nd, nmp, lp, off, len,
3536 NFSV4LOCKT_READ, cred, p, 0);
3537 if ((nd->nd_repstat == NFSERR_GRACE ||
3538 nd->nd_repstat == NFSERR_DELAY) &&
3540 (void) nfs_catnap(PZERO, (int)nd->nd_repstat,
3542 } while ((nd->nd_repstat == NFSERR_GRACE ||
3543 nd->nd_repstat == NFSERR_DELAY) && error == 0);
3546 } while (error == 0 && nd->nd_repstat == 0);
3547 nfscl_releasealllocks(clp, vp, p);
3548 } else if (op == F_SETLK) {
3549 error = nfscl_getbytelock(vp, off, len, fl->l_type, cred, p,
3550 NULL, 0, NULL, NULL, &lp, &newone, &donelocally);
3551 if (error || donelocally) {
3554 if (nmp->nm_clp != NULL)
3555 clidrev = nmp->nm_clp->nfsc_clientidrev;
3558 nfhp = VTONFS(vp)->n_fhp;
3559 if (!lp->nfsl_open->nfso_posixlock &&
3560 (off != 0 || len != NFS64BITSSET)) {
3563 error = nfsrpc_lock(nd, nmp, vp, nfhp->nfh_fh,
3564 nfhp->nfh_len, lp, newone, reclaim, off,
3565 len, fl->l_type, cred, p, 0);
3568 error = nd->nd_repstat;
3569 nfscl_lockrelease(lp, error, newone);
3574 error = nd->nd_repstat;
3575 if (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
3576 error == NFSERR_STALEDONTRECOVER ||
3577 error == NFSERR_STALECLIENTID || error == NFSERR_DELAY) {
3578 (void) nfs_catnap(PZERO, error, "nfs_advlock");
3579 } else if ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID)
3581 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
3584 } while (error == NFSERR_GRACE ||
3585 error == NFSERR_STALECLIENTID || error == NFSERR_DELAY ||
3586 error == NFSERR_STALEDONTRECOVER || error == NFSERR_STALESTATEID ||
3587 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
3588 expireret == 0 && clidrev != 0 && retrycnt < 4));
3589 if (error && retrycnt >= 4)
3595 * The lower level routine for the LockT case.
3598 nfsrpc_lockt(struct nfsrv_descript *nd, vnode_t vp,
3599 struct nfsclclient *clp, u_int64_t off, u_int64_t len, struct flock *fl,
3600 struct ucred *cred, NFSPROC_T *p)
3603 int error, type, size;
3604 u_int8_t own[NFSV4CL_LOCKNAMELEN];
3606 NFSCL_REQSTART(nd, NFSPROC_LOCKT, vp);
3607 NFSM_BUILD(tl, u_int32_t *, 7 * NFSX_UNSIGNED);
3608 if (fl->l_type == F_RDLCK)
3609 *tl++ = txdr_unsigned(NFSV4LOCKT_READ);
3611 *tl++ = txdr_unsigned(NFSV4LOCKT_WRITE);
3612 txdr_hyper(off, tl);
3614 txdr_hyper(len, tl);
3616 *tl++ = clp->nfsc_clientid.lval[0];
3617 *tl = clp->nfsc_clientid.lval[1];
3618 nfscl_filllockowner(p, own);
3619 (void) nfsm_strtom(nd, own, NFSV4CL_LOCKNAMELEN);
3620 error = nfscl_request(nd, vp, p, cred, NULL);
3623 if (nd->nd_repstat == 0) {
3624 fl->l_type = F_UNLCK;
3625 } else if (nd->nd_repstat == NFSERR_DENIED) {
3627 fl->l_whence = SEEK_SET;
3628 NFSM_DISSECT(tl, u_int32_t *, 8 * NFSX_UNSIGNED);
3629 fl->l_start = fxdr_hyper(tl);
3631 len = fxdr_hyper(tl);
3633 if (len == NFS64BITSSET)
3637 type = fxdr_unsigned(int, *tl++);
3638 if (type == NFSV4LOCKT_WRITE)
3639 fl->l_type = F_WRLCK;
3641 fl->l_type = F_RDLCK;
3643 * XXX For now, I have no idea what to do with the
3644 * conflicting lock_owner, so I'll just set the pid == 0
3645 * and skip over the lock_owner.
3647 fl->l_pid = (pid_t)0;
3649 size = fxdr_unsigned(int, *tl);
3650 if (size < 0 || size > NFSV4_OPAQUELIMIT)
3653 error = nfsm_advance(nd, NFSM_RNDUP(size), -1);
3654 } else if (nd->nd_repstat == NFSERR_STALECLIENTID)
3655 nfscl_initiate_recovery(clp);
3657 mbuf_freem(nd->nd_mrep);
3662 * Lower level function that performs the LockU RPC.
3665 nfsrpc_locku(struct nfsrv_descript *nd, struct nfsmount *nmp,
3666 struct nfscllockowner *lp, u_int64_t off, u_int64_t len,
3667 u_int32_t type, struct ucred *cred, NFSPROC_T *p, int syscred)
3672 nfscl_reqstart(nd, NFSPROC_LOCKU, nmp, lp->nfsl_open->nfso_fh,
3673 lp->nfsl_open->nfso_fhlen, NULL);
3674 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID + 6 * NFSX_UNSIGNED);
3675 *tl++ = txdr_unsigned(type);
3676 *tl = txdr_unsigned(lp->nfsl_seqid);
3677 if (nfstest_outofseq &&
3678 (arc4random() % nfstest_outofseq) == 0)
3679 *tl = txdr_unsigned(lp->nfsl_seqid + 1);
3681 *tl++ = lp->nfsl_stateid.seqid;
3682 *tl++ = lp->nfsl_stateid.other[0];
3683 *tl++ = lp->nfsl_stateid.other[1];
3684 *tl++ = lp->nfsl_stateid.other[2];
3685 txdr_hyper(off, tl);
3687 txdr_hyper(len, tl);
3689 nd->nd_flag |= ND_USEGSSNAME;
3690 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
3691 NFS_PROG, NFS_VER4, NULL, 1, NULL);
3692 NFSCL_INCRSEQID(lp->nfsl_seqid, nd);
3695 if (nd->nd_repstat == 0) {
3696 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID);
3697 lp->nfsl_stateid.seqid = *tl++;
3698 lp->nfsl_stateid.other[0] = *tl++;
3699 lp->nfsl_stateid.other[1] = *tl++;
3700 lp->nfsl_stateid.other[2] = *tl;
3701 } else if (nd->nd_repstat == NFSERR_STALESTATEID)
3702 nfscl_initiate_recovery(lp->nfsl_open->nfso_own->nfsow_clp);
3704 mbuf_freem(nd->nd_mrep);
3709 * The actual Lock RPC.
3712 nfsrpc_lock(struct nfsrv_descript *nd, struct nfsmount *nmp, vnode_t vp,
3713 u_int8_t *nfhp, int fhlen, struct nfscllockowner *lp, int newone,
3714 int reclaim, u_int64_t off, u_int64_t len, short type, struct ucred *cred,
3715 NFSPROC_T *p, int syscred)
3720 nfscl_reqstart(nd, NFSPROC_LOCK, nmp, nfhp, fhlen, NULL);
3721 NFSM_BUILD(tl, u_int32_t *, 7 * NFSX_UNSIGNED);
3722 if (type == F_RDLCK)
3723 *tl++ = txdr_unsigned(NFSV4LOCKT_READ);
3725 *tl++ = txdr_unsigned(NFSV4LOCKT_WRITE);
3726 *tl++ = txdr_unsigned(reclaim);
3727 txdr_hyper(off, tl);
3729 txdr_hyper(len, tl);
3733 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID +
3734 2 * NFSX_UNSIGNED + NFSX_HYPER);
3735 *tl++ = txdr_unsigned(lp->nfsl_open->nfso_own->nfsow_seqid);
3736 *tl++ = lp->nfsl_open->nfso_stateid.seqid;
3737 *tl++ = lp->nfsl_open->nfso_stateid.other[0];
3738 *tl++ = lp->nfsl_open->nfso_stateid.other[1];
3739 *tl++ = lp->nfsl_open->nfso_stateid.other[2];
3740 *tl++ = txdr_unsigned(lp->nfsl_seqid);
3741 *tl++ = lp->nfsl_open->nfso_own->nfsow_clp->nfsc_clientid.lval[0];
3742 *tl = lp->nfsl_open->nfso_own->nfsow_clp->nfsc_clientid.lval[1];
3743 (void) nfsm_strtom(nd, lp->nfsl_owner, NFSV4CL_LOCKNAMELEN);
3746 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID + NFSX_UNSIGNED);
3747 *tl++ = lp->nfsl_stateid.seqid;
3748 *tl++ = lp->nfsl_stateid.other[0];
3749 *tl++ = lp->nfsl_stateid.other[1];
3750 *tl++ = lp->nfsl_stateid.other[2];
3751 *tl = txdr_unsigned(lp->nfsl_seqid);
3752 if (nfstest_outofseq &&
3753 (arc4random() % nfstest_outofseq) == 0)
3754 *tl = txdr_unsigned(lp->nfsl_seqid + 1);
3757 nd->nd_flag |= ND_USEGSSNAME;
3758 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, vp, p, cred,
3759 NFS_PROG, NFS_VER4, NULL, 1, NULL);
3763 NFSCL_INCRSEQID(lp->nfsl_open->nfso_own->nfsow_seqid, nd);
3764 NFSCL_INCRSEQID(lp->nfsl_seqid, nd);
3765 if (nd->nd_repstat == 0) {
3766 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID);
3767 lp->nfsl_stateid.seqid = *tl++;
3768 lp->nfsl_stateid.other[0] = *tl++;
3769 lp->nfsl_stateid.other[1] = *tl++;
3770 lp->nfsl_stateid.other[2] = *tl;
3771 } else if (nd->nd_repstat == NFSERR_DENIED) {
3772 NFSM_DISSECT(tl, u_int32_t *, 8 * NFSX_UNSIGNED);
3773 size = fxdr_unsigned(int, *(tl + 7));
3774 if (size < 0 || size > NFSV4_OPAQUELIMIT)
3777 error = nfsm_advance(nd, NFSM_RNDUP(size), -1);
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);
3787 * (always called with the vp for the mount point)
3790 nfsrpc_statfs(vnode_t vp, struct nfsstatfs *sbp, struct nfsfsinfo *fsp,
3791 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp,
3794 u_int32_t *tl = NULL;
3795 struct nfsrv_descript nfsd, *nd = &nfsd;
3796 struct nfsmount *nmp;
3797 nfsattrbit_t attrbits;
3801 nmp = VFSTONFS(vnode_mount(vp));
3802 if (NFSHASNFSV4(nmp)) {
3804 * For V4, you actually do a getattr.
3806 NFSCL_REQSTART(nd, NFSPROC_GETATTR, vp);
3807 NFSSTATFS_GETATTRBIT(&attrbits);
3808 (void) nfsrv_putattrbit(nd, &attrbits);
3809 nd->nd_flag |= ND_USEGSSNAME;
3810 error = nfscl_request(nd, vp, p, cred, stuff);
3813 if (nd->nd_repstat == 0) {
3814 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0,
3815 NULL, NULL, sbp, fsp, NULL, 0, NULL, NULL, NULL, p,
3818 nmp->nm_fsid[0] = nap->na_filesid[0];
3819 nmp->nm_fsid[1] = nap->na_filesid[1];
3820 NFSSETHASSETFSID(nmp);
3824 error = nd->nd_repstat;
3829 NFSCL_REQSTART(nd, NFSPROC_FSSTAT, vp);
3830 error = nfscl_request(nd, vp, p, cred, stuff);
3833 if (nd->nd_flag & ND_NFSV3) {
3834 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
3838 if (nd->nd_repstat) {
3839 error = nd->nd_repstat;
3842 NFSM_DISSECT(tl, u_int32_t *,
3843 NFSX_STATFS(nd->nd_flag & ND_NFSV3));
3845 if (NFSHASNFSV3(nmp)) {
3846 sbp->sf_tbytes = fxdr_hyper(tl); tl += 2;
3847 sbp->sf_fbytes = fxdr_hyper(tl); tl += 2;
3848 sbp->sf_abytes = fxdr_hyper(tl); tl += 2;
3849 sbp->sf_tfiles = fxdr_hyper(tl); tl += 2;
3850 sbp->sf_ffiles = fxdr_hyper(tl); tl += 2;
3851 sbp->sf_afiles = fxdr_hyper(tl); tl += 2;
3852 sbp->sf_invarsec = fxdr_unsigned(u_int32_t, *tl);
3853 } else if (NFSHASNFSV4(nmp) == 0) {
3854 sbp->sf_tsize = fxdr_unsigned(u_int32_t, *tl++);
3855 sbp->sf_bsize = fxdr_unsigned(u_int32_t, *tl++);
3856 sbp->sf_blocks = fxdr_unsigned(u_int32_t, *tl++);
3857 sbp->sf_bfree = fxdr_unsigned(u_int32_t, *tl++);
3858 sbp->sf_bavail = fxdr_unsigned(u_int32_t, *tl);
3861 mbuf_freem(nd->nd_mrep);
3869 nfsrpc_pathconf(vnode_t vp, struct nfsv3_pathconf *pc,
3870 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp,
3873 struct nfsrv_descript nfsd, *nd = &nfsd;
3874 struct nfsmount *nmp;
3876 nfsattrbit_t attrbits;
3880 nmp = VFSTONFS(vnode_mount(vp));
3881 if (NFSHASNFSV4(nmp)) {
3883 * For V4, you actually do a getattr.
3885 NFSCL_REQSTART(nd, NFSPROC_GETATTR, vp);
3886 NFSPATHCONF_GETATTRBIT(&attrbits);
3887 (void) nfsrv_putattrbit(nd, &attrbits);
3888 nd->nd_flag |= ND_USEGSSNAME;
3889 error = nfscl_request(nd, vp, p, cred, stuff);
3892 if (nd->nd_repstat == 0) {
3893 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0,
3894 pc, NULL, NULL, NULL, NULL, 0, NULL, NULL, NULL, p,
3899 error = nd->nd_repstat;
3902 NFSCL_REQSTART(nd, NFSPROC_PATHCONF, vp);
3903 error = nfscl_request(nd, vp, p, cred, stuff);
3906 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
3907 if (nd->nd_repstat && !error)
3908 error = nd->nd_repstat;
3910 NFSM_DISSECT(tl, u_int32_t *, NFSX_V3PATHCONF);
3911 pc->pc_linkmax = fxdr_unsigned(u_int32_t, *tl++);
3912 pc->pc_namemax = fxdr_unsigned(u_int32_t, *tl++);
3913 pc->pc_notrunc = fxdr_unsigned(u_int32_t, *tl++);
3914 pc->pc_chownrestricted =
3915 fxdr_unsigned(u_int32_t, *tl++);
3916 pc->pc_caseinsensitive =
3917 fxdr_unsigned(u_int32_t, *tl++);
3918 pc->pc_casepreserving = fxdr_unsigned(u_int32_t, *tl);
3922 mbuf_freem(nd->nd_mrep);
3927 * nfs version 3 fsinfo rpc call
3930 nfsrpc_fsinfo(vnode_t vp, struct nfsfsinfo *fsp, struct ucred *cred,
3931 NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, void *stuff)
3934 struct nfsrv_descript nfsd, *nd = &nfsd;
3938 NFSCL_REQSTART(nd, NFSPROC_FSINFO, vp);
3939 error = nfscl_request(nd, vp, p, cred, stuff);
3942 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
3943 if (nd->nd_repstat && !error)
3944 error = nd->nd_repstat;
3946 NFSM_DISSECT(tl, u_int32_t *, NFSX_V3FSINFO);
3947 fsp->fs_rtmax = fxdr_unsigned(u_int32_t, *tl++);
3948 fsp->fs_rtpref = fxdr_unsigned(u_int32_t, *tl++);
3949 fsp->fs_rtmult = fxdr_unsigned(u_int32_t, *tl++);
3950 fsp->fs_wtmax = fxdr_unsigned(u_int32_t, *tl++);
3951 fsp->fs_wtpref = fxdr_unsigned(u_int32_t, *tl++);
3952 fsp->fs_wtmult = fxdr_unsigned(u_int32_t, *tl++);
3953 fsp->fs_dtpref = fxdr_unsigned(u_int32_t, *tl++);
3954 fsp->fs_maxfilesize = fxdr_hyper(tl);
3956 fxdr_nfsv3time(tl, &fsp->fs_timedelta);
3958 fsp->fs_properties = fxdr_unsigned(u_int32_t, *tl);
3961 mbuf_freem(nd->nd_mrep);
3966 * This function performs the Renew RPC.
3969 nfsrpc_renew(struct nfsclclient *clp, struct ucred *cred, NFSPROC_T *p)
3972 struct nfsrv_descript nfsd;
3973 struct nfsrv_descript *nd = &nfsd;
3974 struct nfsmount *nmp;
3977 nmp = clp->nfsc_nmp;
3980 nfscl_reqstart(nd, NFSPROC_RENEW, nmp, NULL, 0, NULL);
3981 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
3982 *tl++ = clp->nfsc_clientid.lval[0];
3983 *tl = clp->nfsc_clientid.lval[1];
3984 nd->nd_flag |= ND_USEGSSNAME;
3985 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
3986 NFS_PROG, NFS_VER4, NULL, 1, NULL);
3989 error = nd->nd_repstat;
3990 mbuf_freem(nd->nd_mrep);
3995 * This function performs the Releaselockowner RPC.
3998 nfsrpc_rellockown(struct nfsmount *nmp, struct nfscllockowner *lp,
3999 struct ucred *cred, NFSPROC_T *p)
4001 struct nfsrv_descript nfsd, *nd = &nfsd;
4005 nfscl_reqstart(nd, NFSPROC_RELEASELCKOWN, nmp, NULL, 0, NULL);
4006 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
4007 *tl++ = nmp->nm_clp->nfsc_clientid.lval[0];
4008 *tl = nmp->nm_clp->nfsc_clientid.lval[1];
4009 (void) nfsm_strtom(nd, lp->nfsl_owner, NFSV4CL_LOCKNAMELEN);
4010 nd->nd_flag |= ND_USEGSSNAME;
4011 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4012 NFS_PROG, NFS_VER4, NULL, 1, NULL);
4015 error = nd->nd_repstat;
4016 mbuf_freem(nd->nd_mrep);
4021 * This function performs the Compound to get the mount pt FH.
4024 nfsrpc_getdirpath(struct nfsmount *nmp, u_char *dirpath, struct ucred *cred,
4028 struct nfsrv_descript nfsd;
4029 struct nfsrv_descript *nd = &nfsd;
4031 int error, cnt, len, setnil;
4034 nfscl_reqstart(nd, NFSPROC_PUTROOTFH, nmp, NULL, 0, &opcntp);
4042 while (*cp2 != '\0' && *cp2 != '/')
4049 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
4050 *tl = txdr_unsigned(NFSV4OP_LOOKUP);
4051 nfsm_strtom(nd, cp, strlen(cp));
4057 } while (*cp != '\0');
4058 *opcntp = txdr_unsigned(2 + cnt);
4059 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
4060 *tl = txdr_unsigned(NFSV4OP_GETFH);
4061 nd->nd_flag |= ND_USEGSSNAME;
4062 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4063 NFS_PROG, NFS_VER4, NULL, 1, NULL);
4066 if (nd->nd_repstat == 0) {
4067 NFSM_DISSECT(tl, u_int32_t *, (3 + 2 * cnt) * NFSX_UNSIGNED);
4068 tl += (2 + 2 * cnt);
4069 if ((len = fxdr_unsigned(int, *tl)) <= 0 ||
4071 nd->nd_repstat = NFSERR_BADXDR;
4073 nd->nd_repstat = nfsrv_mtostr(nd, nmp->nm_fh, len);
4074 if (nd->nd_repstat == 0)
4075 nmp->nm_fhsize = len;
4078 error = nd->nd_repstat;
4080 mbuf_freem(nd->nd_mrep);
4085 * This function performs the Delegreturn RPC.
4088 nfsrpc_delegreturn(struct nfscldeleg *dp, struct ucred *cred,
4089 struct nfsmount *nmp, NFSPROC_T *p, int syscred)
4092 struct nfsrv_descript nfsd;
4093 struct nfsrv_descript *nd = &nfsd;
4096 nfscl_reqstart(nd, NFSPROC_DELEGRETURN, nmp, dp->nfsdl_fh,
4097 dp->nfsdl_fhlen, NULL);
4098 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID);
4099 *tl++ = dp->nfsdl_stateid.seqid;
4100 *tl++ = dp->nfsdl_stateid.other[0];
4101 *tl++ = dp->nfsdl_stateid.other[1];
4102 *tl = dp->nfsdl_stateid.other[2];
4104 nd->nd_flag |= ND_USEGSSNAME;
4105 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4106 NFS_PROG, NFS_VER4, NULL, 1, NULL);
4109 error = nd->nd_repstat;
4110 mbuf_freem(nd->nd_mrep);
4114 #ifdef NFS4_ACL_EXTATTR_NAME
4119 nfsrpc_getacl(vnode_t vp, struct ucred *cred, NFSPROC_T *p,
4120 struct acl *aclp, void *stuff)
4122 struct nfsrv_descript nfsd, *nd = &nfsd;
4124 nfsattrbit_t attrbits;
4125 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
4127 if (nfsrv_useacl == 0 || !NFSHASNFSV4(nmp))
4128 return (EOPNOTSUPP);
4129 NFSCL_REQSTART(nd, NFSPROC_GETACL, vp);
4130 NFSZERO_ATTRBIT(&attrbits);
4131 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_ACL);
4132 (void) nfsrv_putattrbit(nd, &attrbits);
4133 error = nfscl_request(nd, vp, p, cred, stuff);
4136 if (!nd->nd_repstat)
4137 error = nfsv4_loadattr(nd, vp, NULL, NULL, NULL, 0, NULL,
4138 NULL, NULL, NULL, aclp, 0, NULL, NULL, NULL, p, cred);
4140 error = nd->nd_repstat;
4141 mbuf_freem(nd->nd_mrep);
4149 nfsrpc_setacl(vnode_t vp, struct ucred *cred, NFSPROC_T *p,
4150 struct acl *aclp, void *stuff)
4153 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
4155 if (nfsrv_useacl == 0 || !NFSHASNFSV4(nmp))
4156 return (EOPNOTSUPP);
4157 error = nfsrpc_setattr(vp, NULL, aclp, cred, p, NULL, NULL, stuff);
4165 nfsrpc_setaclrpc(vnode_t vp, struct ucred *cred, NFSPROC_T *p,
4166 struct acl *aclp, nfsv4stateid_t *stateidp, void *stuff)
4168 struct nfsrv_descript nfsd, *nd = &nfsd;
4170 nfsattrbit_t attrbits;
4171 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
4173 if (!NFSHASNFSV4(nmp))
4174 return (EOPNOTSUPP);
4175 NFSCL_REQSTART(nd, NFSPROC_SETACL, vp);
4176 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID);
4177 NFSZERO_ATTRBIT(&attrbits);
4178 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_ACL);
4179 (void) nfsv4_fillattr(nd, vp, aclp, NULL, NULL, 0, &attrbits,
4181 error = nfscl_request(nd, vp, p, cred, stuff);
4184 /* Don't care about the pre/postop attributes */
4185 mbuf_freem(nd->nd_mrep);
4186 return (nd->nd_repstat);
4189 #endif /* NFS4_ACL_EXTATTR_NAME */