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 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
38 * Rpc op calls, generally called from the vnode op calls or through the
39 * buffer cache, for NFS v2, 3 and 4.
40 * These do not normally make any changes to vnode arguments or use
41 * structures that might change between the VFS variants. The returned
42 * arguments are all at the end, after the NFSPROC_T *p one.
46 #include "opt_inet6.h"
48 #include <fs/nfs/nfsport.h>
49 #include <sys/sysctl.h>
51 SYSCTL_DECL(_vfs_nfs);
53 static int nfsignore_eexist = 0;
54 SYSCTL_INT(_vfs_nfs, OID_AUTO, ignore_eexist, CTLFLAG_RW,
55 &nfsignore_eexist, 0, "NFS ignore EEXIST replies for mkdir/symlink");
60 extern int nfs_numnfscbd;
61 extern struct timeval nfsboottime;
62 extern u_int32_t newnfs_false, newnfs_true;
63 extern nfstype nfsv34_type[9];
64 extern int nfsrv_useacl;
65 extern char nfsv4_callbackaddr[INET6_ADDRSTRLEN];
66 extern int nfscl_debuglevel;
68 int nfstest_outofseq = 0;
69 int nfscl_assumeposixlocks = 1;
70 int nfscl_enablecallb = 0;
71 short nfsv4_cbport = NFSV4_CBPORT;
72 int nfstest_openallsetattr = 0;
73 #endif /* !APPLEKEXT */
75 #define DIRHDSIZ offsetof(struct dirent, d_name)
78 * nfscl_getsameserver() can return one of three values:
79 * NFSDSP_USETHISSESSION - Use this session for the DS.
80 * NFSDSP_SEQTHISSESSION - Use the nfsclds_sequence field of this dsp for new
82 * NFSDSP_NOTFOUND - No matching server was found.
85 NFSDSP_USETHISSESSION = 0,
86 NFSDSP_SEQTHISSESSION = 1,
90 static int nfsrpc_setattrrpc(vnode_t , struct vattr *, nfsv4stateid_t *,
91 struct ucred *, NFSPROC_T *, struct nfsvattr *, int *, void *);
92 static int nfsrpc_readrpc(vnode_t , struct uio *, struct ucred *,
93 nfsv4stateid_t *, NFSPROC_T *, struct nfsvattr *, int *, void *);
94 static int nfsrpc_writerpc(vnode_t , struct uio *, int *, int *,
95 struct ucred *, nfsv4stateid_t *, NFSPROC_T *, struct nfsvattr *, int *,
97 static int nfsrpc_createv23(vnode_t , char *, int, struct vattr *,
98 nfsquad_t, int, struct ucred *, NFSPROC_T *, struct nfsvattr *,
99 struct nfsvattr *, struct nfsfh **, int *, int *, void *);
100 static int nfsrpc_createv4(vnode_t , char *, int, struct vattr *,
101 nfsquad_t, int, struct nfsclowner *, struct nfscldeleg **, struct ucred *,
102 NFSPROC_T *, struct nfsvattr *, struct nfsvattr *, struct nfsfh **, int *,
103 int *, void *, int *);
104 static int nfsrpc_locku(struct nfsrv_descript *, struct nfsmount *,
105 struct nfscllockowner *, u_int64_t, u_int64_t,
106 u_int32_t, struct ucred *, NFSPROC_T *, int);
107 static int nfsrpc_setaclrpc(vnode_t, struct ucred *, NFSPROC_T *,
108 struct acl *, nfsv4stateid_t *, void *);
109 static int nfsrpc_getlayout(struct nfsmount *, vnode_t, struct nfsfh *, int,
110 uint32_t *, nfsv4stateid_t *, uint64_t, struct nfscllayout **,
111 struct ucred *, NFSPROC_T *);
112 static int nfsrpc_fillsa(struct nfsmount *, struct sockaddr_in *,
113 struct sockaddr_in6 *, sa_family_t, int, struct nfsclds **, NFSPROC_T *);
114 static void nfscl_initsessionslots(struct nfsclsession *);
115 static int nfscl_doflayoutio(vnode_t, struct uio *, int *, int *, int *,
116 nfsv4stateid_t *, int, struct nfscldevinfo *, struct nfscllayout *,
117 struct nfsclflayout *, uint64_t, uint64_t, int, struct ucred *,
119 static int nfsrpc_readds(vnode_t, struct uio *, nfsv4stateid_t *, int *,
120 struct nfsclds *, uint64_t, int, struct nfsfh *, struct ucred *,
122 static int nfsrpc_writeds(vnode_t, struct uio *, int *, int *,
123 nfsv4stateid_t *, struct nfsclds *, uint64_t, int,
124 struct nfsfh *, int, struct ucred *, NFSPROC_T *);
125 static enum nfsclds_state nfscl_getsameserver(struct nfsmount *,
126 struct nfsclds *, struct nfsclds **);
127 static int nfsrpc_commitds(vnode_t, uint64_t, int, struct nfsclds *,
128 struct nfsfh *, struct ucred *, NFSPROC_T *);
129 static void nfsrv_setuplayoutget(struct nfsrv_descript *, int, uint64_t,
130 uint64_t, uint64_t, nfsv4stateid_t *, int, int);
131 static int nfsrv_parselayoutget(struct nfsrv_descript *, nfsv4stateid_t *,
132 int *, struct nfsclflayouthead *);
133 static int nfsrpc_getopenlayout(struct nfsmount *, vnode_t, u_int8_t *,
134 int, uint8_t *, int, uint32_t, struct nfsclopen *, uint8_t *, int,
135 struct nfscldeleg **, struct ucred *, NFSPROC_T *);
136 static int nfsrpc_getcreatelayout(vnode_t, char *, int, struct vattr *,
137 nfsquad_t, int, struct nfsclowner *, struct nfscldeleg **,
138 struct ucred *, NFSPROC_T *, struct nfsvattr *, struct nfsvattr *,
139 struct nfsfh **, int *, int *, void *, int *);
140 static int nfsrpc_openlayoutrpc(struct nfsmount *, vnode_t, u_int8_t *,
141 int, uint8_t *, int, uint32_t, struct nfsclopen *, uint8_t *, int,
142 struct nfscldeleg **, nfsv4stateid_t *, int, int, int *,
143 struct nfsclflayouthead *, int *, struct ucred *, NFSPROC_T *);
144 static int nfsrpc_createlayout(vnode_t, char *, int, struct vattr *,
145 nfsquad_t, int, struct nfsclowner *, struct nfscldeleg **,
146 struct ucred *, NFSPROC_T *, struct nfsvattr *, struct nfsvattr *,
147 struct nfsfh **, int *, int *, void *, int *, nfsv4stateid_t *,
148 int, int, int *, struct nfsclflayouthead *, int *);
149 static int nfsrpc_layoutget(struct nfsmount *, uint8_t *, int, int, uint64_t,
150 uint64_t, uint64_t, int, nfsv4stateid_t *, int *, struct nfsclflayouthead *,
151 struct ucred *, NFSPROC_T *, void *);
152 static int nfsrpc_layoutgetres(struct nfsmount *, vnode_t, uint8_t *,
153 int, nfsv4stateid_t *, int, uint32_t *, struct nfscllayout **,
154 struct nfsclflayouthead *, int, int *, struct ucred *, NFSPROC_T *);
157 * nfs null call from vfs.
160 nfsrpc_null(vnode_t vp, struct ucred *cred, NFSPROC_T *p)
163 struct nfsrv_descript nfsd, *nd = &nfsd;
165 NFSCL_REQSTART(nd, NFSPROC_NULL, vp);
166 error = nfscl_request(nd, vp, p, cred, NULL);
167 if (nd->nd_repstat && !error)
168 error = nd->nd_repstat;
169 mbuf_freem(nd->nd_mrep);
175 * For nfs version 3 and 4, use the access rpc to check accessibility. If file
176 * modes are changed on the server, accesses might still fail later.
179 nfsrpc_access(vnode_t vp, int acmode, struct ucred *cred,
180 NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp)
183 u_int32_t mode, rmode;
186 mode = NFSACCESS_READ;
189 if (vnode_vtype(vp) == VDIR) {
191 mode |= (NFSACCESS_MODIFY | NFSACCESS_EXTEND |
194 mode |= NFSACCESS_LOOKUP;
197 mode |= (NFSACCESS_MODIFY | NFSACCESS_EXTEND);
199 mode |= NFSACCESS_EXECUTE;
203 * Now, just call nfsrpc_accessrpc() to do the actual RPC.
205 error = nfsrpc_accessrpc(vp, mode, cred, p, nap, attrflagp, &rmode,
209 * The NFS V3 spec does not clarify whether or not
210 * the returned access bits can be a superset of
211 * the ones requested, so...
213 if (!error && (rmode & mode) != mode)
219 * The actual rpc, separated out for Darwin.
222 nfsrpc_accessrpc(vnode_t vp, u_int32_t mode, struct ucred *cred,
223 NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, u_int32_t *rmodep,
227 u_int32_t supported, rmode;
229 struct nfsrv_descript nfsd, *nd = &nfsd;
230 nfsattrbit_t attrbits;
234 NFSCL_REQSTART(nd, NFSPROC_ACCESS, vp);
235 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
236 *tl = txdr_unsigned(mode);
237 if (nd->nd_flag & ND_NFSV4) {
239 * And do a Getattr op.
241 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
242 *tl = txdr_unsigned(NFSV4OP_GETATTR);
243 NFSGETATTR_ATTRBIT(&attrbits);
244 (void) nfsrv_putattrbit(nd, &attrbits);
246 error = nfscl_request(nd, vp, p, cred, stuff);
249 if (nd->nd_flag & ND_NFSV3) {
250 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
254 if (!nd->nd_repstat) {
255 if (nd->nd_flag & ND_NFSV4) {
256 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
257 supported = fxdr_unsigned(u_int32_t, *tl++);
259 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
261 rmode = fxdr_unsigned(u_int32_t, *tl);
262 if (nd->nd_flag & ND_NFSV4)
263 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
266 * It's not obvious what should be done about
267 * unsupported access modes. For now, be paranoid
268 * and clear the unsupported ones.
273 error = nd->nd_repstat;
275 mbuf_freem(nd->nd_mrep);
283 nfsrpc_open(vnode_t vp, int amode, struct ucred *cred, NFSPROC_T *p)
285 struct nfsclopen *op;
286 struct nfscldeleg *dp;
288 struct nfsnode *np = VTONFS(vp);
289 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
290 u_int32_t mode, clidrev;
291 int ret, newone, error, expireret = 0, retrycnt;
294 * For NFSv4, Open Ops are only done on Regular Files.
296 if (vnode_vtype(vp) != VREG)
300 mode |= NFSV4OPEN_ACCESSREAD;
302 mode |= NFSV4OPEN_ACCESSWRITE;
307 { char name[100]; int namel;
308 namel = (np->n_v4->n4_namelen < 100) ? np->n_v4->n4_namelen : 99;
309 bcopy(NFS4NODENAME(np->n_v4), name, namel);
311 printf("rpcopen p=0x%x name=%s",p->p_pid,name);
312 if (nfhp->nfh_len > 0) printf(" fh=0x%x\n",nfhp->nfh_fh[12]);
313 else printf(" fhl=0\n");
318 error = nfscl_open(vp, nfhp->nfh_fh, nfhp->nfh_len, mode, 1,
319 cred, p, NULL, &op, &newone, &ret, 1);
323 if (nmp->nm_clp != NULL)
324 clidrev = nmp->nm_clp->nfsc_clientidrev;
327 if (ret == NFSCLOPEN_DOOPEN) {
328 if (np->n_v4 != NULL) {
330 * For the first attempt, try and get a layout, if
331 * pNFS is enabled for the mount.
333 if (!NFSHASPNFS(nmp) || nfscl_enablecallb == 0 ||
334 nfs_numnfscbd == 0 ||
335 (np->n_flag & NNOLAYOUT) != 0 || retrycnt > 0)
336 error = nfsrpc_openrpc(nmp, vp,
338 np->n_v4->n4_fhlen, np->n_fhp->nfh_fh,
339 np->n_fhp->nfh_len, mode, op,
340 NFS4NODENAME(np->n_v4),
341 np->n_v4->n4_namelen,
342 &dp, 0, 0x0, cred, p, 0, 0);
344 error = nfsrpc_getopenlayout(nmp, vp,
346 np->n_v4->n4_fhlen, np->n_fhp->nfh_fh,
347 np->n_fhp->nfh_len, mode, op,
348 NFS4NODENAME(np->n_v4),
349 np->n_v4->n4_namelen, &dp, cred, p);
352 OSBitAndAtomic((int32_t)~NDELEGMOD, (UInt32 *)&np->n_flag);
355 np->n_flag &= ~NDELEGMOD;
357 * Invalidate the attribute cache, so that
358 * attributes that pre-date the issue of a
359 * delegation are not cached, since the
360 * cached attributes will remain valid while
361 * the delegation is held.
363 NFSINVALATTRCACHE(np);
366 (void) nfscl_deleg(nmp->nm_mountp,
367 op->nfso_own->nfsow_clp,
368 nfhp->nfh_fh, nfhp->nfh_len, cred, p, &dp);
373 newnfs_copyincred(cred, &op->nfso_cred);
374 } else if (ret == NFSCLOPEN_SETCRED)
376 * This is a new local open on a delegation. It needs
377 * to have credentials so that an open can be done
378 * against the server during recovery.
380 newnfs_copyincred(cred, &op->nfso_cred);
383 * nfso_opencnt is the count of how many VOP_OPEN()s have
384 * been done on this Open successfully and a VOP_CLOSE()
385 * is expected for each of these.
386 * If error is non-zero, don't increment it, since the Open
387 * hasn't succeeded yet.
391 nfscl_openrelease(nmp, op, error, newone);
392 if (error == NFSERR_GRACE || error == NFSERR_STALECLIENTID ||
393 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
394 error == NFSERR_BADSESSION) {
395 (void) nfs_catnap(PZERO, error, "nfs_open");
396 } else if ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID)
398 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
401 } while (error == NFSERR_GRACE || error == NFSERR_STALECLIENTID ||
402 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
403 error == NFSERR_BADSESSION ||
404 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
405 expireret == 0 && clidrev != 0 && retrycnt < 4));
406 if (error && retrycnt >= 4)
412 * the actual open rpc
415 nfsrpc_openrpc(struct nfsmount *nmp, vnode_t vp, u_int8_t *nfhp, int fhlen,
416 u_int8_t *newfhp, int newfhlen, u_int32_t mode, struct nfsclopen *op,
417 u_int8_t *name, int namelen, struct nfscldeleg **dpp,
418 int reclaim, u_int32_t delegtype, struct ucred *cred, NFSPROC_T *p,
419 int syscred, int recursed)
422 struct nfsrv_descript nfsd, *nd = &nfsd;
423 struct nfscldeleg *dp, *ndp = NULL;
424 struct nfsvattr nfsva;
425 u_int32_t rflags, deleg;
426 nfsattrbit_t attrbits;
427 int error, ret, acesize, limitby;
428 struct nfsclsession *tsep;
432 nfscl_reqstart(nd, NFSPROC_OPEN, nmp, nfhp, fhlen, NULL, NULL, 0, 0);
433 NFSM_BUILD(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
434 *tl++ = txdr_unsigned(op->nfso_own->nfsow_seqid);
435 *tl++ = txdr_unsigned(mode & NFSV4OPEN_ACCESSBOTH);
436 *tl++ = txdr_unsigned((mode >> NFSLCK_SHIFT) & NFSV4OPEN_DENYBOTH);
437 tsep = nfsmnt_mdssession(nmp);
438 *tl++ = tsep->nfsess_clientid.lval[0];
439 *tl = tsep->nfsess_clientid.lval[1];
440 (void) nfsm_strtom(nd, op->nfso_own->nfsow_owner, NFSV4CL_LOCKNAMELEN);
441 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
442 *tl++ = txdr_unsigned(NFSV4OPEN_NOCREATE);
444 *tl = txdr_unsigned(NFSV4OPEN_CLAIMPREVIOUS);
445 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
446 *tl = txdr_unsigned(delegtype);
449 *tl = txdr_unsigned(NFSV4OPEN_CLAIMDELEGATECUR);
450 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID);
451 if (NFSHASNFSV4N(nmp))
454 *tl++ = dp->nfsdl_stateid.seqid;
455 *tl++ = dp->nfsdl_stateid.other[0];
456 *tl++ = dp->nfsdl_stateid.other[1];
457 *tl = dp->nfsdl_stateid.other[2];
459 *tl = txdr_unsigned(NFSV4OPEN_CLAIMNULL);
461 (void) nfsm_strtom(nd, name, namelen);
463 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
464 *tl = txdr_unsigned(NFSV4OP_GETATTR);
465 NFSZERO_ATTRBIT(&attrbits);
466 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_CHANGE);
467 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEMODIFY);
468 (void) nfsrv_putattrbit(nd, &attrbits);
470 nd->nd_flag |= ND_USEGSSNAME;
471 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, vp, p, cred,
472 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
475 NFSCL_INCRSEQID(op->nfso_own->nfsow_seqid, nd);
476 if (!nd->nd_repstat) {
477 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
479 op->nfso_stateid.seqid = *tl++;
480 op->nfso_stateid.other[0] = *tl++;
481 op->nfso_stateid.other[1] = *tl++;
482 op->nfso_stateid.other[2] = *tl;
483 rflags = fxdr_unsigned(u_int32_t, *(tl + 6));
484 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
487 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
488 deleg = fxdr_unsigned(u_int32_t, *tl);
489 if (deleg == NFSV4OPEN_DELEGATEREAD ||
490 deleg == NFSV4OPEN_DELEGATEWRITE) {
491 if (!(op->nfso_own->nfsow_clp->nfsc_flags &
492 NFSCLFLAGS_FIRSTDELEG))
493 op->nfso_own->nfsow_clp->nfsc_flags |=
494 (NFSCLFLAGS_FIRSTDELEG | NFSCLFLAGS_GOTDELEG);
495 MALLOC(ndp, struct nfscldeleg *,
496 sizeof (struct nfscldeleg) + newfhlen,
497 M_NFSCLDELEG, M_WAITOK);
498 LIST_INIT(&ndp->nfsdl_owner);
499 LIST_INIT(&ndp->nfsdl_lock);
500 ndp->nfsdl_clp = op->nfso_own->nfsow_clp;
501 ndp->nfsdl_fhlen = newfhlen;
502 NFSBCOPY(newfhp, ndp->nfsdl_fh, newfhlen);
503 newnfs_copyincred(cred, &ndp->nfsdl_cred);
504 nfscl_lockinit(&ndp->nfsdl_rwlock);
505 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
507 ndp->nfsdl_stateid.seqid = *tl++;
508 ndp->nfsdl_stateid.other[0] = *tl++;
509 ndp->nfsdl_stateid.other[1] = *tl++;
510 ndp->nfsdl_stateid.other[2] = *tl++;
511 ret = fxdr_unsigned(int, *tl);
512 if (deleg == NFSV4OPEN_DELEGATEWRITE) {
513 ndp->nfsdl_flags = NFSCLDL_WRITE;
515 * Indicates how much the file can grow.
517 NFSM_DISSECT(tl, u_int32_t *,
519 limitby = fxdr_unsigned(int, *tl++);
521 case NFSV4OPEN_LIMITSIZE:
522 ndp->nfsdl_sizelimit = fxdr_hyper(tl);
524 case NFSV4OPEN_LIMITBLOCKS:
525 ndp->nfsdl_sizelimit =
526 fxdr_unsigned(u_int64_t, *tl++);
527 ndp->nfsdl_sizelimit *=
528 fxdr_unsigned(u_int64_t, *tl);
531 error = NFSERR_BADXDR;
535 ndp->nfsdl_flags = NFSCLDL_READ;
538 ndp->nfsdl_flags |= NFSCLDL_RECALL;
539 error = nfsrv_dissectace(nd, &ndp->nfsdl_ace, &ret,
543 } else if (deleg != NFSV4OPEN_DELEGATENONE) {
544 error = NFSERR_BADXDR;
547 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
548 error = nfsv4_loadattr(nd, NULL, &nfsva, NULL,
549 NULL, 0, NULL, NULL, NULL, NULL, NULL, 0,
550 NULL, NULL, NULL, p, cred);
554 ndp->nfsdl_change = nfsva.na_filerev;
555 ndp->nfsdl_modtime = nfsva.na_mtime;
556 ndp->nfsdl_flags |= NFSCLDL_MODTIMESET;
558 if (!reclaim && (rflags & NFSV4OPEN_RESULTCONFIRM)) {
560 ret = nfsrpc_openconfirm(vp, newfhp, newfhlen, op,
562 if (ret == NFSERR_DELAY)
563 (void) nfs_catnap(PZERO, ret, "nfs_open");
564 } while (ret == NFSERR_DELAY);
567 if ((rflags & NFSV4OPEN_LOCKTYPEPOSIX) ||
568 nfscl_assumeposixlocks)
569 op->nfso_posixlock = 1;
571 op->nfso_posixlock = 0;
574 * If the server is handing out delegations, but we didn't
575 * get one because an OpenConfirm was required, try the
576 * Open again, to get a delegation. This is a harmless no-op,
577 * from a server's point of view.
579 if (!reclaim && (rflags & NFSV4OPEN_RESULTCONFIRM) &&
580 (op->nfso_own->nfsow_clp->nfsc_flags & NFSCLFLAGS_GOTDELEG)
581 && !error && dp == NULL && ndp == NULL && !recursed) {
583 ret = nfsrpc_openrpc(nmp, vp, nfhp, fhlen, newfhp,
584 newfhlen, mode, op, name, namelen, &ndp, 0, 0x0,
585 cred, p, syscred, 1);
586 if (ret == NFSERR_DELAY)
587 (void) nfs_catnap(PZERO, ret, "nfs_open2");
588 } while (ret == NFSERR_DELAY);
591 FREE((caddr_t)ndp, M_NFSCLDELEG);
594 if (ret == NFSERR_STALECLIENTID ||
595 ret == NFSERR_STALEDONTRECOVER ||
596 ret == NFSERR_BADSESSION)
601 if (nd->nd_repstat != 0 && error == 0)
602 error = nd->nd_repstat;
603 if (error == NFSERR_STALECLIENTID)
604 nfscl_initiate_recovery(op->nfso_own->nfsow_clp);
608 else if (ndp != NULL)
609 FREE((caddr_t)ndp, M_NFSCLDELEG);
610 mbuf_freem(nd->nd_mrep);
618 nfsrpc_opendowngrade(vnode_t vp, u_int32_t mode, struct nfsclopen *op,
619 struct ucred *cred, NFSPROC_T *p)
622 struct nfsrv_descript nfsd, *nd = &nfsd;
625 NFSCL_REQSTART(nd, NFSPROC_OPENDOWNGRADE, vp);
626 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID + 3 * NFSX_UNSIGNED);
627 if (NFSHASNFSV4N(VFSTONFS(vnode_mount(vp))))
630 *tl++ = op->nfso_stateid.seqid;
631 *tl++ = op->nfso_stateid.other[0];
632 *tl++ = op->nfso_stateid.other[1];
633 *tl++ = op->nfso_stateid.other[2];
634 *tl++ = txdr_unsigned(op->nfso_own->nfsow_seqid);
635 *tl++ = txdr_unsigned(mode & NFSV4OPEN_ACCESSBOTH);
636 *tl = txdr_unsigned((mode >> NFSLCK_SHIFT) & NFSV4OPEN_DENYBOTH);
637 error = nfscl_request(nd, vp, p, cred, NULL);
640 NFSCL_INCRSEQID(op->nfso_own->nfsow_seqid, nd);
641 if (!nd->nd_repstat) {
642 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID);
643 op->nfso_stateid.seqid = *tl++;
644 op->nfso_stateid.other[0] = *tl++;
645 op->nfso_stateid.other[1] = *tl++;
646 op->nfso_stateid.other[2] = *tl;
648 if (nd->nd_repstat && error == 0)
649 error = nd->nd_repstat;
650 if (error == NFSERR_STALESTATEID)
651 nfscl_initiate_recovery(op->nfso_own->nfsow_clp);
653 mbuf_freem(nd->nd_mrep);
658 * V4 Close operation.
661 nfsrpc_close(vnode_t vp, int doclose, NFSPROC_T *p)
663 struct nfsclclient *clp;
666 if (vnode_vtype(vp) != VREG)
669 error = nfscl_doclose(vp, &clp, p);
671 error = nfscl_getclose(vp, &clp);
675 nfscl_clientrelease(clp);
683 nfsrpc_doclose(struct nfsmount *nmp, struct nfsclopen *op, NFSPROC_T *p)
685 struct nfsrv_descript nfsd, *nd = &nfsd;
686 struct nfscllockowner *lp, *nlp;
687 struct nfscllock *lop, *nlop;
689 u_int64_t off = 0, len = 0;
690 u_int32_t type = NFSV4LOCKT_READ;
691 int error, do_unlock, trycnt;
693 tcred = newnfs_getcred();
694 newnfs_copycred(&op->nfso_cred, tcred);
696 * (Theoretically this could be done in the same
697 * compound as the close, but having multiple
698 * sequenced Ops in the same compound might be
699 * too scary for some servers.)
701 if (op->nfso_posixlock) {
704 type = NFSV4LOCKT_READ;
708 * Since this function is only called from VOP_INACTIVE(), no
709 * other thread will be manipulating this Open. As such, the
710 * lock lists are not being changed by other threads, so it should
711 * be safe to do this without locking.
713 LIST_FOREACH(lp, &op->nfso_lock, nfsl_list) {
715 LIST_FOREACH_SAFE(lop, &lp->nfsl_lock, nfslo_list, nlop) {
716 if (op->nfso_posixlock == 0) {
717 off = lop->nfslo_first;
718 len = lop->nfslo_end - lop->nfslo_first;
719 if (lop->nfslo_type == F_WRLCK)
720 type = NFSV4LOCKT_WRITE;
722 type = NFSV4LOCKT_READ;
727 error = nfsrpc_locku(nd, nmp, lp, off,
728 len, type, tcred, p, 0);
729 if ((nd->nd_repstat == NFSERR_GRACE ||
730 nd->nd_repstat == NFSERR_DELAY) &&
732 (void) nfs_catnap(PZERO,
735 } while ((nd->nd_repstat == NFSERR_GRACE ||
736 nd->nd_repstat == NFSERR_DELAY) &&
737 error == 0 && trycnt++ < 5);
738 if (op->nfso_posixlock)
741 nfscl_freelock(lop, 0);
744 * Do a ReleaseLockOwner.
745 * The lock owner name nfsl_owner may be used by other opens for
746 * other files but the lock_owner4 name that nfsrpc_rellockown()
747 * puts on the wire has the file handle for this file appended
748 * to it, so it can be done now.
750 (void)nfsrpc_rellockown(nmp, lp, lp->nfsl_open->nfso_fh,
751 lp->nfsl_open->nfso_fhlen, tcred, p);
755 * There could be other Opens for different files on the same
756 * OpenOwner, so locking is required.
759 nfscl_lockexcl(&op->nfso_own->nfsow_rwlock, NFSCLSTATEMUTEXPTR);
762 error = nfscl_tryclose(op, tcred, nmp, p);
763 if (error == NFSERR_GRACE)
764 (void) nfs_catnap(PZERO, error, "nfs_close");
765 } while (error == NFSERR_GRACE);
767 nfscl_lockunlock(&op->nfso_own->nfsow_rwlock);
769 LIST_FOREACH_SAFE(lp, &op->nfso_lock, nfsl_list, nlp)
770 nfscl_freelockowner(lp, 0);
771 nfscl_freeopen(op, 0);
777 * The actual Close RPC.
780 nfsrpc_closerpc(struct nfsrv_descript *nd, struct nfsmount *nmp,
781 struct nfsclopen *op, struct ucred *cred, NFSPROC_T *p,
787 nfscl_reqstart(nd, NFSPROC_CLOSE, nmp, op->nfso_fh,
788 op->nfso_fhlen, NULL, NULL, 0, 0);
789 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED + NFSX_STATEID);
790 *tl++ = txdr_unsigned(op->nfso_own->nfsow_seqid);
791 if (NFSHASNFSV4N(nmp))
794 *tl++ = op->nfso_stateid.seqid;
795 *tl++ = op->nfso_stateid.other[0];
796 *tl++ = op->nfso_stateid.other[1];
797 *tl = op->nfso_stateid.other[2];
799 nd->nd_flag |= ND_USEGSSNAME;
800 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
801 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
804 NFSCL_INCRSEQID(op->nfso_own->nfsow_seqid, nd);
805 if (nd->nd_repstat == 0)
806 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID);
807 error = nd->nd_repstat;
808 if (error == NFSERR_STALESTATEID)
809 nfscl_initiate_recovery(op->nfso_own->nfsow_clp);
811 mbuf_freem(nd->nd_mrep);
816 * V4 Open Confirm RPC.
819 nfsrpc_openconfirm(vnode_t vp, u_int8_t *nfhp, int fhlen,
820 struct nfsclopen *op, struct ucred *cred, NFSPROC_T *p)
823 struct nfsrv_descript nfsd, *nd = &nfsd;
824 struct nfsmount *nmp;
827 nmp = VFSTONFS(vnode_mount(vp));
828 if (NFSHASNFSV4N(nmp))
829 return (0); /* No confirmation for NFSv4.1. */
830 nfscl_reqstart(nd, NFSPROC_OPENCONFIRM, nmp, nfhp, fhlen, NULL, NULL,
832 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED + NFSX_STATEID);
833 *tl++ = op->nfso_stateid.seqid;
834 *tl++ = op->nfso_stateid.other[0];
835 *tl++ = op->nfso_stateid.other[1];
836 *tl++ = op->nfso_stateid.other[2];
837 *tl = txdr_unsigned(op->nfso_own->nfsow_seqid);
838 error = nfscl_request(nd, vp, p, cred, NULL);
841 NFSCL_INCRSEQID(op->nfso_own->nfsow_seqid, nd);
842 if (!nd->nd_repstat) {
843 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID);
844 op->nfso_stateid.seqid = *tl++;
845 op->nfso_stateid.other[0] = *tl++;
846 op->nfso_stateid.other[1] = *tl++;
847 op->nfso_stateid.other[2] = *tl;
849 error = nd->nd_repstat;
850 if (error == NFSERR_STALESTATEID)
851 nfscl_initiate_recovery(op->nfso_own->nfsow_clp);
853 mbuf_freem(nd->nd_mrep);
858 * Do the setclientid and setclientid confirm RPCs. Called from nfs_statfs()
859 * when a mount has just occurred and when the server replies NFSERR_EXPIRED.
862 nfsrpc_setclient(struct nfsmount *nmp, struct nfsclclient *clp, int reclaim,
863 struct ucred *cred, NFSPROC_T *p)
866 struct nfsrv_descript nfsd;
867 struct nfsrv_descript *nd = &nfsd;
868 nfsattrbit_t attrbits;
869 u_int8_t *cp = NULL, *cp2, addr[INET6_ADDRSTRLEN + 9];
871 int error, isinet6 = 0, callblen;
874 static u_int32_t rev = 0;
877 struct nfsclsession *tsep;
879 if (nfsboottime.tv_sec == 0)
880 NFSSETBOOTTIME(nfsboottime);
881 clp->nfsc_rev = rev++;
882 if (NFSHASNFSV4N(nmp)) {
884 * Either there was no previous session or the
885 * previous session has failed, so...
886 * do an ExchangeID followed by the CreateSession.
888 error = nfsrpc_exchangeid(nmp, clp, &nmp->nm_sockreq,
889 NFSV4EXCH_USEPNFSMDS | NFSV4EXCH_USENONPNFS, &dsp, cred, p);
890 NFSCL_DEBUG(1, "aft exch=%d\n", error);
892 error = nfsrpc_createsession(nmp, &dsp->nfsclds_sess,
894 dsp->nfsclds_sess.nfsess_sequenceid, 1, cred, p);
898 * The old sessions cannot be safely free'd
899 * here, since they may still be used by
903 if (TAILQ_FIRST(&nmp->nm_sess) != NULL)
904 tsep = NFSMNT_MDSSESSION(nmp);
905 TAILQ_INSERT_HEAD(&nmp->nm_sess, dsp,
908 * Wake up RPCs waiting for a slot on the
909 * old session. These will then fail with
910 * NFSERR_BADSESSION and be retried with the
911 * new session by nfsv4_setsequence().
912 * Also wakeup() processes waiting for the
916 wakeup(&tsep->nfsess_slots);
917 wakeup(&nmp->nm_sess);
920 nfscl_freenfsclds(dsp);
921 NFSCL_DEBUG(1, "aft createsess=%d\n", error);
922 if (error == 0 && reclaim == 0) {
923 error = nfsrpc_reclaimcomplete(nmp, cred, p);
924 NFSCL_DEBUG(1, "aft reclaimcomp=%d\n", error);
925 if (error == NFSERR_COMPLETEALREADY ||
926 error == NFSERR_NOTSUPP)
927 /* Ignore this error. */
934 * Allocate a single session structure for NFSv4.0, because some of
935 * the fields are used by NFSv4.0 although it doesn't do a session.
937 dsp = malloc(sizeof(struct nfsclds), M_NFSCLDS, M_WAITOK | M_ZERO);
938 mtx_init(&dsp->nfsclds_mtx, "nfsds", NULL, MTX_DEF);
939 mtx_init(&dsp->nfsclds_sess.nfsess_mtx, "nfssession", NULL, MTX_DEF);
941 TAILQ_INSERT_HEAD(&nmp->nm_sess, dsp, nfsclds_list);
942 tsep = NFSMNT_MDSSESSION(nmp);
945 nfscl_reqstart(nd, NFSPROC_SETCLIENTID, nmp, NULL, 0, NULL, NULL, 0, 0);
946 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
947 *tl++ = txdr_unsigned(nfsboottime.tv_sec);
948 *tl = txdr_unsigned(clp->nfsc_rev);
949 (void) nfsm_strtom(nd, clp->nfsc_id, clp->nfsc_idlen);
952 * set up the callback address
954 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
955 *tl = txdr_unsigned(NFS_CALLBCKPROG);
956 callblen = strlen(nfsv4_callbackaddr);
958 cp = nfscl_getmyip(nmp, &a6, &isinet6);
959 if (nfscl_enablecallb && nfs_numnfscbd > 0 &&
960 (callblen > 0 || cp != NULL)) {
961 port = htons(nfsv4_cbport);
962 cp2 = (u_int8_t *)&port;
965 strchr(nfsv4_callbackaddr, ':')) || isinet6) {
966 char ip6buf[INET6_ADDRSTRLEN], *ip6add;
968 (void) nfsm_strtom(nd, "tcp6", 4);
970 ip6_sprintf(ip6buf, (struct in6_addr *)cp);
973 ip6add = nfsv4_callbackaddr;
975 snprintf(addr, INET6_ADDRSTRLEN + 9, "%s.%d.%d",
976 ip6add, cp2[0], cp2[1]);
980 (void) nfsm_strtom(nd, "tcp", 3);
982 snprintf(addr, INET6_ADDRSTRLEN + 9,
983 "%d.%d.%d.%d.%d.%d", cp[0], cp[1],
984 cp[2], cp[3], cp2[0], cp2[1]);
986 snprintf(addr, INET6_ADDRSTRLEN + 9,
987 "%s.%d.%d", nfsv4_callbackaddr,
990 (void) nfsm_strtom(nd, addr, strlen(addr));
992 (void) nfsm_strtom(nd, "tcp", 3);
993 (void) nfsm_strtom(nd, "0.0.0.0.0.0", 11);
995 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
996 *tl = txdr_unsigned(clp->nfsc_cbident);
997 nd->nd_flag |= ND_USEGSSNAME;
998 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
999 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
1002 if (nd->nd_repstat == 0) {
1003 NFSM_DISSECT(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1004 tsep->nfsess_clientid.lval[0] = *tl++;
1005 tsep->nfsess_clientid.lval[1] = *tl++;
1006 confirm.lval[0] = *tl++;
1007 confirm.lval[1] = *tl;
1008 mbuf_freem(nd->nd_mrep);
1014 nfscl_reqstart(nd, NFSPROC_SETCLIENTIDCFRM, nmp, NULL, 0, NULL,
1016 NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1017 *tl++ = tsep->nfsess_clientid.lval[0];
1018 *tl++ = tsep->nfsess_clientid.lval[1];
1019 *tl++ = confirm.lval[0];
1020 *tl = confirm.lval[1];
1021 nd->nd_flag |= ND_USEGSSNAME;
1022 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p,
1023 cred, NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
1026 mbuf_freem(nd->nd_mrep);
1028 if (nd->nd_repstat == 0) {
1029 nfscl_reqstart(nd, NFSPROC_GETATTR, nmp, nmp->nm_fh,
1030 nmp->nm_fhsize, NULL, NULL, 0, 0);
1031 NFSZERO_ATTRBIT(&attrbits);
1032 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_LEASETIME);
1033 (void) nfsrv_putattrbit(nd, &attrbits);
1034 nd->nd_flag |= ND_USEGSSNAME;
1035 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p,
1036 cred, NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
1039 if (nd->nd_repstat == 0) {
1040 error = nfsv4_loadattr(nd, NULL, NULL, NULL, NULL, 0, NULL,
1041 NULL, NULL, NULL, NULL, 0, NULL, &lease, NULL, p, cred);
1044 clp->nfsc_renew = NFSCL_RENEW(lease);
1045 clp->nfsc_expire = NFSD_MONOSEC + clp->nfsc_renew;
1046 clp->nfsc_clientidrev++;
1047 if (clp->nfsc_clientidrev == 0)
1048 clp->nfsc_clientidrev++;
1052 error = nd->nd_repstat;
1054 mbuf_freem(nd->nd_mrep);
1062 nfsrpc_getattr(vnode_t vp, struct ucred *cred, NFSPROC_T *p,
1063 struct nfsvattr *nap, void *stuff)
1065 struct nfsrv_descript nfsd, *nd = &nfsd;
1067 nfsattrbit_t attrbits;
1069 NFSCL_REQSTART(nd, NFSPROC_GETATTR, vp);
1070 if (nd->nd_flag & ND_NFSV4) {
1071 NFSGETATTR_ATTRBIT(&attrbits);
1072 (void) nfsrv_putattrbit(nd, &attrbits);
1074 error = nfscl_request(nd, vp, p, cred, stuff);
1077 if (!nd->nd_repstat)
1078 error = nfsm_loadattr(nd, nap);
1080 error = nd->nd_repstat;
1081 mbuf_freem(nd->nd_mrep);
1086 * nfs getattr call with non-vnode arguemnts.
1089 nfsrpc_getattrnovp(struct nfsmount *nmp, u_int8_t *fhp, int fhlen, int syscred,
1090 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, u_int64_t *xidp,
1093 struct nfsrv_descript nfsd, *nd = &nfsd;
1094 int error, vers = NFS_VER2;
1095 nfsattrbit_t attrbits;
1097 nfscl_reqstart(nd, NFSPROC_GETATTR, nmp, fhp, fhlen, NULL, NULL, 0, 0);
1098 if (nd->nd_flag & ND_NFSV4) {
1100 NFSGETATTR_ATTRBIT(&attrbits);
1101 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_LEASETIME);
1102 (void) nfsrv_putattrbit(nd, &attrbits);
1103 } else if (nd->nd_flag & ND_NFSV3) {
1107 nd->nd_flag |= ND_USEGSSNAME;
1108 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
1109 NFS_PROG, vers, NULL, 1, xidp, NULL);
1112 if (nd->nd_repstat == 0) {
1113 if ((nd->nd_flag & ND_NFSV4) != 0)
1114 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0,
1115 NULL, NULL, NULL, NULL, NULL, 0, NULL, leasep, NULL,
1118 error = nfsm_loadattr(nd, nap);
1120 error = nd->nd_repstat;
1121 mbuf_freem(nd->nd_mrep);
1126 * Do an nfs setattr operation.
1129 nfsrpc_setattr(vnode_t vp, struct vattr *vap, NFSACL_T *aclp,
1130 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *rnap, int *attrflagp,
1133 int error, expireret = 0, openerr, retrycnt;
1134 u_int32_t clidrev = 0, mode;
1135 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
1137 nfsv4stateid_t stateid;
1140 if (nmp->nm_clp != NULL)
1141 clidrev = nmp->nm_clp->nfsc_clientidrev;
1142 if (vap != NULL && NFSATTRISSET(u_quad_t, vap, va_size))
1143 mode = NFSV4OPEN_ACCESSWRITE;
1145 mode = NFSV4OPEN_ACCESSREAD;
1150 if (NFSHASNFSV4(nmp)) {
1151 nfhp = VTONFS(vp)->n_fhp;
1152 error = nfscl_getstateid(vp, nfhp->nfh_fh,
1153 nfhp->nfh_len, mode, 0, cred, p, &stateid, &lckp);
1154 if (error && vnode_vtype(vp) == VREG &&
1155 (mode == NFSV4OPEN_ACCESSWRITE ||
1156 nfstest_openallsetattr)) {
1158 * No Open stateid, so try and open the file
1161 if (mode == NFSV4OPEN_ACCESSWRITE)
1162 openerr = nfsrpc_open(vp, FWRITE, cred,
1165 openerr = nfsrpc_open(vp, FREAD, cred,
1168 (void) nfscl_getstateid(vp,
1169 nfhp->nfh_fh, nfhp->nfh_len,
1170 mode, 0, cred, p, &stateid, &lckp);
1174 error = nfsrpc_setattrrpc(vp, vap, &stateid, cred, p,
1175 rnap, attrflagp, stuff);
1177 error = nfsrpc_setaclrpc(vp, cred, p, aclp, &stateid,
1179 if (error == NFSERR_OPENMODE && mode == NFSV4OPEN_ACCESSREAD) {
1181 nmp->nm_state |= NFSSTA_OPENMODE;
1184 if (error == NFSERR_STALESTATEID)
1185 nfscl_initiate_recovery(nmp->nm_clp);
1187 nfscl_lockderef(lckp);
1189 (void) nfsrpc_close(vp, 0, p);
1190 if (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
1191 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
1192 error == NFSERR_OLDSTATEID || error == NFSERR_BADSESSION) {
1193 (void) nfs_catnap(PZERO, error, "nfs_setattr");
1194 } else if ((error == NFSERR_EXPIRED ||
1195 error == NFSERR_BADSTATEID) && clidrev != 0) {
1196 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
1199 } while (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
1200 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
1201 error == NFSERR_BADSESSION ||
1202 (error == NFSERR_OLDSTATEID && retrycnt < 20) ||
1203 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
1204 expireret == 0 && clidrev != 0 && retrycnt < 4) ||
1205 (error == NFSERR_OPENMODE && mode == NFSV4OPEN_ACCESSREAD &&
1207 if (error && retrycnt >= 4)
1213 nfsrpc_setattrrpc(vnode_t vp, struct vattr *vap,
1214 nfsv4stateid_t *stateidp, struct ucred *cred, NFSPROC_T *p,
1215 struct nfsvattr *rnap, int *attrflagp, void *stuff)
1218 struct nfsrv_descript nfsd, *nd = &nfsd;
1220 nfsattrbit_t attrbits;
1223 NFSCL_REQSTART(nd, NFSPROC_SETATTR, vp);
1224 if (nd->nd_flag & ND_NFSV4)
1225 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID);
1226 vap->va_type = vnode_vtype(vp);
1227 nfscl_fillsattr(nd, vap, vp, NFSSATTR_FULL, 0);
1228 if (nd->nd_flag & ND_NFSV3) {
1229 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1231 } else if (nd->nd_flag & ND_NFSV4) {
1232 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1233 *tl = txdr_unsigned(NFSV4OP_GETATTR);
1234 NFSGETATTR_ATTRBIT(&attrbits);
1235 (void) nfsrv_putattrbit(nd, &attrbits);
1237 error = nfscl_request(nd, vp, p, cred, stuff);
1240 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4))
1241 error = nfscl_wcc_data(nd, vp, rnap, attrflagp, NULL, stuff);
1242 if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) == ND_NFSV4 && !error)
1243 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
1244 if (!(nd->nd_flag & ND_NFSV3) && !nd->nd_repstat && !error)
1245 error = nfscl_postop_attr(nd, rnap, attrflagp, stuff);
1246 mbuf_freem(nd->nd_mrep);
1247 if (nd->nd_repstat && !error)
1248 error = nd->nd_repstat;
1256 nfsrpc_lookup(vnode_t dvp, char *name, int len, struct ucred *cred,
1257 NFSPROC_T *p, struct nfsvattr *dnap, struct nfsvattr *nap,
1258 struct nfsfh **nfhpp, int *attrflagp, int *dattrflagp, void *stuff)
1261 struct nfsrv_descript nfsd, *nd = &nfsd;
1262 struct nfsmount *nmp;
1265 nfsattrbit_t attrbits;
1266 int error = 0, lookupp = 0;
1270 if (vnode_vtype(dvp) != VDIR)
1272 nmp = VFSTONFS(vnode_mount(dvp));
1273 if (len > NFS_MAXNAMLEN)
1274 return (ENAMETOOLONG);
1275 if (NFSHASNFSV4(nmp) && len == 1 &&
1278 * Just return the current dir's fh.
1281 MALLOC(nfhp, struct nfsfh *, sizeof (struct nfsfh) +
1282 np->n_fhp->nfh_len, M_NFSFH, M_WAITOK);
1283 nfhp->nfh_len = np->n_fhp->nfh_len;
1284 NFSBCOPY(np->n_fhp->nfh_fh, nfhp->nfh_fh, nfhp->nfh_len);
1288 if (NFSHASNFSV4(nmp) && len == 2 &&
1289 name[0] == '.' && name[1] == '.') {
1291 NFSCL_REQSTART(nd, NFSPROC_LOOKUPP, dvp);
1293 NFSCL_REQSTART(nd, NFSPROC_LOOKUP, dvp);
1294 (void) nfsm_strtom(nd, name, len);
1296 if (nd->nd_flag & ND_NFSV4) {
1297 NFSGETATTR_ATTRBIT(&attrbits);
1298 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1299 *tl++ = txdr_unsigned(NFSV4OP_GETFH);
1300 *tl = txdr_unsigned(NFSV4OP_GETATTR);
1301 (void) nfsrv_putattrbit(nd, &attrbits);
1303 error = nfscl_request(nd, dvp, p, cred, stuff);
1306 if (nd->nd_repstat) {
1308 * When an NFSv4 Lookupp returns ENOENT, it means that
1309 * the lookup is at the root of an fs, so return this dir.
1311 if (nd->nd_repstat == NFSERR_NOENT && lookupp) {
1313 MALLOC(nfhp, struct nfsfh *, sizeof (struct nfsfh) +
1314 np->n_fhp->nfh_len, M_NFSFH, M_WAITOK);
1315 nfhp->nfh_len = np->n_fhp->nfh_len;
1316 NFSBCOPY(np->n_fhp->nfh_fh, nfhp->nfh_fh, nfhp->nfh_len);
1318 mbuf_freem(nd->nd_mrep);
1321 if (nd->nd_flag & ND_NFSV3)
1322 error = nfscl_postop_attr(nd, dnap, dattrflagp, stuff);
1323 else if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) ==
1325 /* Load the directory attributes. */
1326 error = nfsm_loadattr(nd, dnap);
1332 if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) == ND_NFSV4) {
1333 /* Load the directory attributes. */
1334 error = nfsm_loadattr(nd, dnap);
1338 /* Skip over the Lookup and GetFH operation status values. */
1339 NFSM_DISSECT(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1341 error = nfsm_getfh(nd, nfhpp);
1345 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
1346 if ((nd->nd_flag & ND_NFSV3) && !error)
1347 error = nfscl_postop_attr(nd, dnap, dattrflagp, stuff);
1349 mbuf_freem(nd->nd_mrep);
1350 if (!error && nd->nd_repstat)
1351 error = nd->nd_repstat;
1356 * Do a readlink rpc.
1359 nfsrpc_readlink(vnode_t vp, struct uio *uiop, struct ucred *cred,
1360 NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, void *stuff)
1363 struct nfsrv_descript nfsd, *nd = &nfsd;
1364 struct nfsnode *np = VTONFS(vp);
1365 nfsattrbit_t attrbits;
1366 int error, len, cangetattr = 1;
1369 NFSCL_REQSTART(nd, NFSPROC_READLINK, vp);
1370 if (nd->nd_flag & ND_NFSV4) {
1372 * And do a Getattr op.
1374 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1375 *tl = txdr_unsigned(NFSV4OP_GETATTR);
1376 NFSGETATTR_ATTRBIT(&attrbits);
1377 (void) nfsrv_putattrbit(nd, &attrbits);
1379 error = nfscl_request(nd, vp, p, cred, stuff);
1382 if (nd->nd_flag & ND_NFSV3)
1383 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
1384 if (!nd->nd_repstat && !error) {
1385 NFSM_STRSIZ(len, NFS_MAXPATHLEN);
1387 * This seems weird to me, but must have been added to
1388 * FreeBSD for some reason. The only thing I can think of
1389 * is that there was/is some server that replies with
1390 * more link data than it should?
1392 if (len == NFS_MAXPATHLEN) {
1394 if (np->n_size > 0 && np->n_size < NFS_MAXPATHLEN) {
1400 error = nfsm_mbufuio(nd, uiop, len);
1401 if ((nd->nd_flag & ND_NFSV4) && !error && cangetattr)
1402 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
1404 if (nd->nd_repstat && !error)
1405 error = nd->nd_repstat;
1407 mbuf_freem(nd->nd_mrep);
1415 nfsrpc_read(vnode_t vp, struct uio *uiop, struct ucred *cred,
1416 NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, void *stuff)
1418 int error, expireret = 0, retrycnt;
1419 u_int32_t clidrev = 0;
1420 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
1421 struct nfsnode *np = VTONFS(vp);
1422 struct ucred *newcred;
1423 struct nfsfh *nfhp = NULL;
1424 nfsv4stateid_t stateid;
1427 if (nmp->nm_clp != NULL)
1428 clidrev = nmp->nm_clp->nfsc_clientidrev;
1430 if (NFSHASNFSV4(nmp)) {
1432 newcred = NFSNEWCRED(cred);
1437 if (NFSHASNFSV4(nmp))
1438 (void)nfscl_getstateid(vp, nfhp->nfh_fh, nfhp->nfh_len,
1439 NFSV4OPEN_ACCESSREAD, 0, newcred, p, &stateid,
1441 error = nfsrpc_readrpc(vp, uiop, newcred, &stateid, p, nap,
1443 if (error == NFSERR_OPENMODE) {
1445 nmp->nm_state |= NFSSTA_OPENMODE;
1448 if (error == NFSERR_STALESTATEID)
1449 nfscl_initiate_recovery(nmp->nm_clp);
1451 nfscl_lockderef(lckp);
1452 if (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
1453 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
1454 error == NFSERR_OLDSTATEID || error == NFSERR_BADSESSION) {
1455 (void) nfs_catnap(PZERO, error, "nfs_read");
1456 } else if ((error == NFSERR_EXPIRED ||
1457 error == NFSERR_BADSTATEID) && clidrev != 0) {
1458 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
1461 } while (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
1462 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
1463 error == NFSERR_BADSESSION ||
1464 (error == NFSERR_OLDSTATEID && retrycnt < 20) ||
1465 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
1466 expireret == 0 && clidrev != 0 && retrycnt < 4) ||
1467 (error == NFSERR_OPENMODE && retrycnt < 4));
1468 if (error && retrycnt >= 4)
1470 if (NFSHASNFSV4(nmp))
1471 NFSFREECRED(newcred);
1476 * The actual read RPC.
1479 nfsrpc_readrpc(vnode_t vp, struct uio *uiop, struct ucred *cred,
1480 nfsv4stateid_t *stateidp, NFSPROC_T *p, struct nfsvattr *nap,
1481 int *attrflagp, void *stuff)
1484 int error = 0, len, retlen, tsiz, eof = 0;
1485 struct nfsrv_descript nfsd;
1486 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
1487 struct nfsrv_descript *nd = &nfsd;
1492 tsiz = uio_uio_resid(uiop);
1493 tmp_off = uiop->uio_offset + tsiz;
1495 if (tmp_off > nmp->nm_maxfilesize || tmp_off < uiop->uio_offset) {
1499 rsize = nmp->nm_rsize;
1504 len = (tsiz > rsize) ? rsize : tsiz;
1505 NFSCL_REQSTART(nd, NFSPROC_READ, vp);
1506 if (nd->nd_flag & ND_NFSV4)
1507 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID);
1508 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED * 3);
1509 if (nd->nd_flag & ND_NFSV2) {
1510 *tl++ = txdr_unsigned(uiop->uio_offset);
1511 *tl++ = txdr_unsigned(len);
1514 txdr_hyper(uiop->uio_offset, tl);
1515 *(tl + 2) = txdr_unsigned(len);
1518 * Since I can't do a Getattr for NFSv4 for Write, there
1519 * doesn't seem any point in doing one here, either.
1520 * (See the comment in nfsrpc_writerpc() for more info.)
1522 error = nfscl_request(nd, vp, p, cred, stuff);
1525 if (nd->nd_flag & ND_NFSV3) {
1526 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
1527 } else if (!nd->nd_repstat && (nd->nd_flag & ND_NFSV2)) {
1528 error = nfsm_loadattr(nd, nap);
1532 if (nd->nd_repstat || error) {
1534 error = nd->nd_repstat;
1537 if (nd->nd_flag & ND_NFSV3) {
1538 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1539 eof = fxdr_unsigned(int, *(tl + 1));
1540 } else if (nd->nd_flag & ND_NFSV4) {
1541 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
1542 eof = fxdr_unsigned(int, *tl);
1544 NFSM_STRSIZ(retlen, len);
1545 error = nfsm_mbufuio(nd, uiop, retlen);
1548 mbuf_freem(nd->nd_mrep);
1551 if (!(nd->nd_flag & ND_NFSV2)) {
1552 if (eof || retlen == 0)
1554 } else if (retlen < len)
1559 if (nd->nd_mrep != NULL)
1560 mbuf_freem(nd->nd_mrep);
1565 * nfs write operation
1566 * When called_from_strategy != 0, it should return EIO for an error that
1567 * indicates recovery is in progress, so that the buffer will be left
1568 * dirty and be written back to the server later. If it loops around,
1569 * the recovery thread could get stuck waiting for the buffer and recovery
1570 * will then deadlock.
1573 nfsrpc_write(vnode_t vp, struct uio *uiop, int *iomode, int *must_commit,
1574 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp,
1575 void *stuff, int called_from_strategy)
1577 int error, expireret = 0, retrycnt, nostateid;
1578 u_int32_t clidrev = 0;
1579 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
1580 struct nfsnode *np = VTONFS(vp);
1581 struct ucred *newcred;
1582 struct nfsfh *nfhp = NULL;
1583 nfsv4stateid_t stateid;
1587 if (nmp->nm_clp != NULL)
1588 clidrev = nmp->nm_clp->nfsc_clientidrev;
1590 if (NFSHASNFSV4(nmp)) {
1591 newcred = NFSNEWCRED(cred);
1598 if (NFSHASNFSV4(nmp)) {
1599 (void)nfscl_getstateid(vp, nfhp->nfh_fh, nfhp->nfh_len,
1600 NFSV4OPEN_ACCESSWRITE, 0, newcred, p, &stateid,
1602 if (stateid.other[0] == 0 && stateid.other[1] == 0 &&
1603 stateid.other[2] == 0) {
1605 NFSCL_DEBUG(1, "stateid0 in write\n");
1610 * If there is no stateid for NFSv4, it means this is an
1611 * extraneous write after close. Basically a poorly
1612 * implemented buffer cache. Just don't do the write.
1617 error = nfsrpc_writerpc(vp, uiop, iomode, must_commit,
1618 newcred, &stateid, p, nap, attrflagp, stuff);
1619 if (error == NFSERR_STALESTATEID)
1620 nfscl_initiate_recovery(nmp->nm_clp);
1622 nfscl_lockderef(lckp);
1623 if (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
1624 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
1625 error == NFSERR_OLDSTATEID || error == NFSERR_BADSESSION) {
1626 (void) nfs_catnap(PZERO, error, "nfs_write");
1627 } else if ((error == NFSERR_EXPIRED ||
1628 error == NFSERR_BADSTATEID) && clidrev != 0) {
1629 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
1632 } while (error == NFSERR_GRACE || error == NFSERR_DELAY ||
1633 ((error == NFSERR_STALESTATEID || error == NFSERR_BADSESSION ||
1634 error == NFSERR_STALEDONTRECOVER) && called_from_strategy == 0) ||
1635 (error == NFSERR_OLDSTATEID && retrycnt < 20) ||
1636 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
1637 expireret == 0 && clidrev != 0 && retrycnt < 4));
1638 if (error != 0 && (retrycnt >= 4 ||
1639 ((error == NFSERR_STALESTATEID || error == NFSERR_BADSESSION ||
1640 error == NFSERR_STALEDONTRECOVER) && called_from_strategy != 0)))
1642 if (NFSHASNFSV4(nmp))
1643 NFSFREECRED(newcred);
1648 * The actual write RPC.
1651 nfsrpc_writerpc(vnode_t vp, struct uio *uiop, int *iomode,
1652 int *must_commit, struct ucred *cred, nfsv4stateid_t *stateidp,
1653 NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, void *stuff)
1656 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
1657 struct nfsnode *np = VTONFS(vp);
1658 int error = 0, len, tsiz, rlen, commit, committed = NFSWRITE_FILESYNC;
1659 int wccflag = 0, wsize;
1661 struct nfsrv_descript nfsd;
1662 struct nfsrv_descript *nd = &nfsd;
1663 nfsattrbit_t attrbits;
1666 KASSERT(uiop->uio_iovcnt == 1, ("nfs: writerpc iovcnt > 1"));
1668 tsiz = uio_uio_resid(uiop);
1669 tmp_off = uiop->uio_offset + tsiz;
1671 if (tmp_off > nmp->nm_maxfilesize || tmp_off < uiop->uio_offset) {
1675 wsize = nmp->nm_wsize;
1677 nd->nd_mrep = NULL; /* NFSv2 sometimes does a write with */
1678 nd->nd_repstat = 0; /* uio_resid == 0, so the while is not done */
1681 len = (tsiz > wsize) ? wsize : tsiz;
1682 NFSCL_REQSTART(nd, NFSPROC_WRITE, vp);
1683 if (nd->nd_flag & ND_NFSV4) {
1684 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID);
1685 NFSM_BUILD(tl, u_int32_t *, NFSX_HYPER+2*NFSX_UNSIGNED);
1686 txdr_hyper(uiop->uio_offset, tl);
1688 *tl++ = txdr_unsigned(*iomode);
1689 *tl = txdr_unsigned(len);
1690 } else if (nd->nd_flag & ND_NFSV3) {
1691 NFSM_BUILD(tl, u_int32_t *, NFSX_HYPER+3*NFSX_UNSIGNED);
1692 txdr_hyper(uiop->uio_offset, tl);
1694 *tl++ = txdr_unsigned(len);
1695 *tl++ = txdr_unsigned(*iomode);
1696 *tl = txdr_unsigned(len);
1700 NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1702 * Not sure why someone changed this, since the
1703 * RFC clearly states that "beginoffset" and
1704 * "totalcount" are ignored, but it wouldn't
1705 * surprise me if there's a busted server out there.
1707 /* Set both "begin" and "current" to non-garbage. */
1708 x = txdr_unsigned((u_int32_t)uiop->uio_offset);
1709 *tl++ = x; /* "begin offset" */
1710 *tl++ = x; /* "current offset" */
1711 x = txdr_unsigned(len);
1712 *tl++ = x; /* total to this offset */
1713 *tl = x; /* size of this write */
1716 nfsm_uiombuf(nd, uiop, len);
1718 * Although it is tempting to do a normal Getattr Op in the
1719 * NFSv4 compound, the result can be a nearly hung client
1720 * system if the Getattr asks for Owner and/or OwnerGroup.
1721 * It occurs when the client can't map either the Owner or
1722 * Owner_group name in the Getattr reply to a uid/gid. When
1723 * there is a cache miss, the kernel does an upcall to the
1724 * nfsuserd. Then, it can try and read the local /etc/passwd
1725 * or /etc/group file. It can then block in getnewbuf(),
1726 * waiting for dirty writes to be pushed to the NFS server.
1727 * The only reason this doesn't result in a complete
1728 * deadlock, is that the upcall times out and allows
1729 * the write to complete. However, progress is so slow
1730 * that it might just as well be deadlocked.
1731 * As such, we get the rest of the attributes, but not
1732 * Owner or Owner_group.
1733 * nb: nfscl_loadattrcache() needs to be told that these
1734 * partial attributes from a write rpc are being
1735 * passed in, via a argument flag.
1737 if (nd->nd_flag & ND_NFSV4) {
1738 NFSWRITEGETATTR_ATTRBIT(&attrbits);
1739 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1740 *tl = txdr_unsigned(NFSV4OP_GETATTR);
1741 (void) nfsrv_putattrbit(nd, &attrbits);
1743 error = nfscl_request(nd, vp, p, cred, stuff);
1746 if (nd->nd_repstat) {
1748 * In case the rpc gets retried, roll
1749 * the uio fileds changed by nfsm_uiombuf()
1752 uiop->uio_offset -= len;
1753 uio_uio_resid_add(uiop, len);
1754 uio_iov_base_add(uiop, -len);
1755 uio_iov_len_add(uiop, len);
1757 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) {
1758 error = nfscl_wcc_data(nd, vp, nap, attrflagp,
1763 if (!nd->nd_repstat) {
1764 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) {
1765 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED
1767 rlen = fxdr_unsigned(int, *tl++);
1771 } else if (rlen < len) {
1772 backup = len - rlen;
1773 uio_iov_base_add(uiop, -(backup));
1774 uio_iov_len_add(uiop, backup);
1775 uiop->uio_offset -= backup;
1776 uio_uio_resid_add(uiop, backup);
1779 commit = fxdr_unsigned(int, *tl++);
1782 * Return the lowest commitment level
1783 * obtained by any of the RPCs.
1785 if (committed == NFSWRITE_FILESYNC)
1787 else if (committed == NFSWRITE_DATASYNC &&
1788 commit == NFSWRITE_UNSTABLE)
1791 if (!NFSHASWRITEVERF(nmp)) {
1792 NFSBCOPY((caddr_t)tl,
1793 (caddr_t)&nmp->nm_verf[0],
1795 NFSSETWRITEVERF(nmp);
1796 } else if (NFSBCMP(tl, nmp->nm_verf,
1799 NFSBCOPY(tl, nmp->nm_verf, NFSX_VERF);
1803 if (nd->nd_flag & ND_NFSV4)
1804 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1805 if (nd->nd_flag & (ND_NFSV2 | ND_NFSV4)) {
1806 error = nfsm_loadattr(nd, nap);
1808 *attrflagp = NFS_LATTR_NOSHRINK;
1811 error = nd->nd_repstat;
1815 NFSWRITERPC_SETTIME(wccflag, np, nap, (nd->nd_flag & ND_NFSV4));
1816 mbuf_freem(nd->nd_mrep);
1821 if (nd->nd_mrep != NULL)
1822 mbuf_freem(nd->nd_mrep);
1823 *iomode = committed;
1824 if (nd->nd_repstat && !error)
1825 error = nd->nd_repstat;
1831 * For NFS v2 this is a kludge. Use a create rpc but with the IFMT bits of the
1832 * mode set to specify the file type and the size field for rdev.
1835 nfsrpc_mknod(vnode_t dvp, char *name, int namelen, struct vattr *vap,
1836 u_int32_t rdev, enum vtype vtyp, struct ucred *cred, NFSPROC_T *p,
1837 struct nfsvattr *dnap, struct nfsvattr *nnap, struct nfsfh **nfhpp,
1838 int *attrflagp, int *dattrflagp, void *dstuff)
1842 struct nfsrv_descript nfsd, *nd = &nfsd;
1843 nfsattrbit_t attrbits;
1848 if (namelen > NFS_MAXNAMLEN)
1849 return (ENAMETOOLONG);
1850 NFSCL_REQSTART(nd, NFSPROC_MKNOD, dvp);
1851 if (nd->nd_flag & ND_NFSV4) {
1852 if (vtyp == VBLK || vtyp == VCHR) {
1853 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
1854 *tl++ = vtonfsv34_type(vtyp);
1855 *tl++ = txdr_unsigned(NFSMAJOR(rdev));
1856 *tl = txdr_unsigned(NFSMINOR(rdev));
1858 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1859 *tl = vtonfsv34_type(vtyp);
1862 (void) nfsm_strtom(nd, name, namelen);
1863 if (nd->nd_flag & ND_NFSV3) {
1864 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1865 *tl = vtonfsv34_type(vtyp);
1867 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4))
1868 nfscl_fillsattr(nd, vap, dvp, 0, 0);
1869 if ((nd->nd_flag & ND_NFSV3) &&
1870 (vtyp == VCHR || vtyp == VBLK)) {
1871 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1872 *tl++ = txdr_unsigned(NFSMAJOR(rdev));
1873 *tl = txdr_unsigned(NFSMINOR(rdev));
1875 if (nd->nd_flag & ND_NFSV4) {
1876 NFSGETATTR_ATTRBIT(&attrbits);
1877 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1878 *tl++ = txdr_unsigned(NFSV4OP_GETFH);
1879 *tl = txdr_unsigned(NFSV4OP_GETATTR);
1880 (void) nfsrv_putattrbit(nd, &attrbits);
1882 if (nd->nd_flag & ND_NFSV2)
1883 nfscl_fillsattr(nd, vap, dvp, NFSSATTR_SIZERDEV, rdev);
1884 error = nfscl_request(nd, dvp, p, cred, dstuff);
1887 if (nd->nd_flag & ND_NFSV4)
1888 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
1889 if (!nd->nd_repstat) {
1890 if (nd->nd_flag & ND_NFSV4) {
1891 NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
1892 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
1896 error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp);
1900 if (nd->nd_flag & ND_NFSV3)
1901 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
1902 if (!error && nd->nd_repstat)
1903 error = nd->nd_repstat;
1905 mbuf_freem(nd->nd_mrep);
1910 * nfs file create call
1911 * Mostly just call the approriate routine. (I separated out v4, so that
1912 * error recovery wouldn't be as difficult.)
1915 nfsrpc_create(vnode_t dvp, char *name, int namelen, struct vattr *vap,
1916 nfsquad_t cverf, int fmode, struct ucred *cred, NFSPROC_T *p,
1917 struct nfsvattr *dnap, struct nfsvattr *nnap, struct nfsfh **nfhpp,
1918 int *attrflagp, int *dattrflagp, void *dstuff)
1920 int error = 0, newone, expireret = 0, retrycnt, unlocked;
1921 struct nfsclowner *owp;
1922 struct nfscldeleg *dp;
1923 struct nfsmount *nmp = VFSTONFS(vnode_mount(dvp));
1926 if (NFSHASNFSV4(nmp)) {
1930 error = nfscl_open(dvp, NULL, 0, (NFSV4OPEN_ACCESSWRITE |
1931 NFSV4OPEN_ACCESSREAD), 0, cred, p, &owp, NULL, &newone,
1935 if (nmp->nm_clp != NULL)
1936 clidrev = nmp->nm_clp->nfsc_clientidrev;
1939 if (!NFSHASPNFS(nmp) || nfscl_enablecallb == 0 ||
1940 nfs_numnfscbd == 0 || retrycnt > 0)
1941 error = nfsrpc_createv4(dvp, name, namelen, vap, cverf,
1942 fmode, owp, &dp, cred, p, dnap, nnap, nfhpp,
1943 attrflagp, dattrflagp, dstuff, &unlocked);
1945 error = nfsrpc_getcreatelayout(dvp, name, namelen, vap,
1946 cverf, fmode, owp, &dp, cred, p, dnap, nnap, nfhpp,
1947 attrflagp, dattrflagp, dstuff, &unlocked);
1949 * There is no need to invalidate cached attributes here,
1950 * since new post-delegation issue attributes are always
1951 * returned by nfsrpc_createv4() and these will update the
1955 (void) nfscl_deleg(nmp->nm_mountp, owp->nfsow_clp,
1956 (*nfhpp)->nfh_fh, (*nfhpp)->nfh_len, cred, p, &dp);
1957 nfscl_ownerrelease(nmp, owp, error, newone, unlocked);
1958 if (error == NFSERR_GRACE || error == NFSERR_STALECLIENTID ||
1959 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
1960 error == NFSERR_BADSESSION) {
1961 (void) nfs_catnap(PZERO, error, "nfs_open");
1962 } else if ((error == NFSERR_EXPIRED ||
1963 error == NFSERR_BADSTATEID) && clidrev != 0) {
1964 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
1967 } while (error == NFSERR_GRACE || error == NFSERR_STALECLIENTID ||
1968 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
1969 error == NFSERR_BADSESSION ||
1970 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
1971 expireret == 0 && clidrev != 0 && retrycnt < 4));
1972 if (error && retrycnt >= 4)
1975 error = nfsrpc_createv23(dvp, name, namelen, vap, cverf,
1976 fmode, cred, p, dnap, nnap, nfhpp, attrflagp, dattrflagp,
1983 * The create rpc for v2 and 3.
1986 nfsrpc_createv23(vnode_t dvp, char *name, int namelen, struct vattr *vap,
1987 nfsquad_t cverf, int fmode, struct ucred *cred, NFSPROC_T *p,
1988 struct nfsvattr *dnap, struct nfsvattr *nnap, struct nfsfh **nfhpp,
1989 int *attrflagp, int *dattrflagp, void *dstuff)
1993 struct nfsrv_descript nfsd, *nd = &nfsd;
1998 if (namelen > NFS_MAXNAMLEN)
1999 return (ENAMETOOLONG);
2000 NFSCL_REQSTART(nd, NFSPROC_CREATE, dvp);
2001 (void) nfsm_strtom(nd, name, namelen);
2002 if (nd->nd_flag & ND_NFSV3) {
2003 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2004 if (fmode & O_EXCL) {
2005 *tl = txdr_unsigned(NFSCREATE_EXCLUSIVE);
2006 NFSM_BUILD(tl, u_int32_t *, NFSX_VERF);
2007 *tl++ = cverf.lval[0];
2008 *tl = cverf.lval[1];
2010 *tl = txdr_unsigned(NFSCREATE_UNCHECKED);
2011 nfscl_fillsattr(nd, vap, dvp, 0, 0);
2014 nfscl_fillsattr(nd, vap, dvp, NFSSATTR_SIZE0, 0);
2016 error = nfscl_request(nd, dvp, p, cred, dstuff);
2019 if (nd->nd_repstat == 0) {
2020 error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp);
2024 if (nd->nd_flag & ND_NFSV3)
2025 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
2026 if (nd->nd_repstat != 0 && error == 0)
2027 error = nd->nd_repstat;
2029 mbuf_freem(nd->nd_mrep);
2034 nfsrpc_createv4(vnode_t dvp, char *name, int namelen, struct vattr *vap,
2035 nfsquad_t cverf, int fmode, struct nfsclowner *owp, struct nfscldeleg **dpp,
2036 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap,
2037 struct nfsvattr *nnap, struct nfsfh **nfhpp, int *attrflagp,
2038 int *dattrflagp, void *dstuff, int *unlockedp)
2041 int error = 0, deleg, newone, ret, acesize, limitby;
2042 struct nfsrv_descript nfsd, *nd = &nfsd;
2043 struct nfsclopen *op;
2044 struct nfscldeleg *dp = NULL;
2047 nfsattrbit_t attrbits;
2048 nfsv4stateid_t stateid;
2050 struct nfsmount *nmp;
2051 struct nfsclsession *tsep;
2053 nmp = VFSTONFS(dvp->v_mount);
2060 if (namelen > NFS_MAXNAMLEN)
2061 return (ENAMETOOLONG);
2062 NFSCL_REQSTART(nd, NFSPROC_CREATE, dvp);
2064 * For V4, this is actually an Open op.
2066 NFSM_BUILD(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
2067 *tl++ = txdr_unsigned(owp->nfsow_seqid);
2068 *tl++ = txdr_unsigned(NFSV4OPEN_ACCESSWRITE |
2069 NFSV4OPEN_ACCESSREAD);
2070 *tl++ = txdr_unsigned(NFSV4OPEN_DENYNONE);
2071 tsep = nfsmnt_mdssession(nmp);
2072 *tl++ = tsep->nfsess_clientid.lval[0];
2073 *tl = tsep->nfsess_clientid.lval[1];
2074 (void) nfsm_strtom(nd, owp->nfsow_owner, NFSV4CL_LOCKNAMELEN);
2075 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2076 *tl++ = txdr_unsigned(NFSV4OPEN_CREATE);
2077 if (fmode & O_EXCL) {
2078 if (NFSHASNFSV4N(nmp)) {
2079 if (NFSHASSESSPERSIST(nmp)) {
2080 /* Use GUARDED for persistent sessions. */
2081 *tl = txdr_unsigned(NFSCREATE_GUARDED);
2082 nfscl_fillsattr(nd, vap, dvp, 0, 0);
2084 /* Otherwise, use EXCLUSIVE4_1. */
2085 *tl = txdr_unsigned(NFSCREATE_EXCLUSIVE41);
2086 NFSM_BUILD(tl, u_int32_t *, NFSX_VERF);
2087 *tl++ = cverf.lval[0];
2088 *tl = cverf.lval[1];
2089 nfscl_fillsattr(nd, vap, dvp, 0, 0);
2093 *tl = txdr_unsigned(NFSCREATE_EXCLUSIVE);
2094 NFSM_BUILD(tl, u_int32_t *, NFSX_VERF);
2095 *tl++ = cverf.lval[0];
2096 *tl = cverf.lval[1];
2099 *tl = txdr_unsigned(NFSCREATE_UNCHECKED);
2100 nfscl_fillsattr(nd, vap, dvp, 0, 0);
2102 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2103 *tl = txdr_unsigned(NFSV4OPEN_CLAIMNULL);
2104 (void) nfsm_strtom(nd, name, namelen);
2105 /* Get the new file's handle and attributes. */
2106 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2107 *tl++ = txdr_unsigned(NFSV4OP_GETFH);
2108 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2109 NFSGETATTR_ATTRBIT(&attrbits);
2110 (void) nfsrv_putattrbit(nd, &attrbits);
2111 /* Get the directory's post-op attributes. */
2112 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2113 *tl = txdr_unsigned(NFSV4OP_PUTFH);
2114 (void) nfsm_fhtom(nd, np->n_fhp->nfh_fh, np->n_fhp->nfh_len, 0);
2115 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2116 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2117 (void) nfsrv_putattrbit(nd, &attrbits);
2118 error = nfscl_request(nd, dvp, p, cred, dstuff);
2121 NFSCL_INCRSEQID(owp->nfsow_seqid, nd);
2122 if (nd->nd_repstat == 0) {
2123 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
2125 stateid.seqid = *tl++;
2126 stateid.other[0] = *tl++;
2127 stateid.other[1] = *tl++;
2128 stateid.other[2] = *tl;
2129 rflags = fxdr_unsigned(u_int32_t, *(tl + 6));
2130 (void) nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
2131 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2132 deleg = fxdr_unsigned(int, *tl);
2133 if (deleg == NFSV4OPEN_DELEGATEREAD ||
2134 deleg == NFSV4OPEN_DELEGATEWRITE) {
2135 if (!(owp->nfsow_clp->nfsc_flags &
2136 NFSCLFLAGS_FIRSTDELEG))
2137 owp->nfsow_clp->nfsc_flags |=
2138 (NFSCLFLAGS_FIRSTDELEG | NFSCLFLAGS_GOTDELEG);
2139 MALLOC(dp, struct nfscldeleg *,
2140 sizeof (struct nfscldeleg) + NFSX_V4FHMAX,
2141 M_NFSCLDELEG, M_WAITOK);
2142 LIST_INIT(&dp->nfsdl_owner);
2143 LIST_INIT(&dp->nfsdl_lock);
2144 dp->nfsdl_clp = owp->nfsow_clp;
2145 newnfs_copyincred(cred, &dp->nfsdl_cred);
2146 nfscl_lockinit(&dp->nfsdl_rwlock);
2147 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
2149 dp->nfsdl_stateid.seqid = *tl++;
2150 dp->nfsdl_stateid.other[0] = *tl++;
2151 dp->nfsdl_stateid.other[1] = *tl++;
2152 dp->nfsdl_stateid.other[2] = *tl++;
2153 ret = fxdr_unsigned(int, *tl);
2154 if (deleg == NFSV4OPEN_DELEGATEWRITE) {
2155 dp->nfsdl_flags = NFSCLDL_WRITE;
2157 * Indicates how much the file can grow.
2159 NFSM_DISSECT(tl, u_int32_t *,
2161 limitby = fxdr_unsigned(int, *tl++);
2163 case NFSV4OPEN_LIMITSIZE:
2164 dp->nfsdl_sizelimit = fxdr_hyper(tl);
2166 case NFSV4OPEN_LIMITBLOCKS:
2167 dp->nfsdl_sizelimit =
2168 fxdr_unsigned(u_int64_t, *tl++);
2169 dp->nfsdl_sizelimit *=
2170 fxdr_unsigned(u_int64_t, *tl);
2173 error = NFSERR_BADXDR;
2177 dp->nfsdl_flags = NFSCLDL_READ;
2180 dp->nfsdl_flags |= NFSCLDL_RECALL;
2181 error = nfsrv_dissectace(nd, &dp->nfsdl_ace, &ret,
2185 } else if (deleg != NFSV4OPEN_DELEGATENONE) {
2186 error = NFSERR_BADXDR;
2189 error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp);
2192 /* Get rid of the PutFH and Getattr status values. */
2193 NFSM_DISSECT(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
2194 /* Load the directory attributes. */
2195 error = nfsm_loadattr(nd, dnap);
2199 if (dp != NULL && *attrflagp) {
2200 dp->nfsdl_change = nnap->na_filerev;
2201 dp->nfsdl_modtime = nnap->na_mtime;
2202 dp->nfsdl_flags |= NFSCLDL_MODTIMESET;
2205 * We can now complete the Open state.
2209 dp->nfsdl_fhlen = nfhp->nfh_len;
2210 NFSBCOPY(nfhp->nfh_fh, dp->nfsdl_fh, nfhp->nfh_len);
2213 * Get an Open structure that will be
2214 * attached to the OpenOwner, acquired already.
2216 error = nfscl_open(dvp, nfhp->nfh_fh, nfhp->nfh_len,
2217 (NFSV4OPEN_ACCESSWRITE | NFSV4OPEN_ACCESSREAD), 0,
2218 cred, p, NULL, &op, &newone, NULL, 0);
2221 op->nfso_stateid = stateid;
2222 newnfs_copyincred(cred, &op->nfso_cred);
2223 if ((rflags & NFSV4OPEN_RESULTCONFIRM)) {
2225 ret = nfsrpc_openconfirm(dvp, nfhp->nfh_fh,
2226 nfhp->nfh_len, op, cred, p);
2227 if (ret == NFSERR_DELAY)
2228 (void) nfs_catnap(PZERO, ret, "nfs_create");
2229 } while (ret == NFSERR_DELAY);
2234 * If the server is handing out delegations, but we didn't
2235 * get one because an OpenConfirm was required, try the
2236 * Open again, to get a delegation. This is a harmless no-op,
2237 * from a server's point of view.
2239 if ((rflags & NFSV4OPEN_RESULTCONFIRM) &&
2240 (owp->nfsow_clp->nfsc_flags & NFSCLFLAGS_GOTDELEG) &&
2241 !error && dp == NULL) {
2243 ret = nfsrpc_openrpc(VFSTONFS(vnode_mount(dvp)), dvp,
2244 np->n_fhp->nfh_fh, np->n_fhp->nfh_len,
2245 nfhp->nfh_fh, nfhp->nfh_len,
2246 (NFSV4OPEN_ACCESSWRITE | NFSV4OPEN_ACCESSREAD), op,
2247 name, namelen, &dp, 0, 0x0, cred, p, 0, 1);
2248 if (ret == NFSERR_DELAY)
2249 (void) nfs_catnap(PZERO, ret, "nfs_crt2");
2250 } while (ret == NFSERR_DELAY);
2253 FREE((caddr_t)dp, M_NFSCLDELEG);
2256 if (ret == NFSERR_STALECLIENTID ||
2257 ret == NFSERR_STALEDONTRECOVER ||
2258 ret == NFSERR_BADSESSION)
2262 nfscl_openrelease(nmp, op, error, newone);
2265 if (nd->nd_repstat != 0 && error == 0)
2266 error = nd->nd_repstat;
2267 if (error == NFSERR_STALECLIENTID)
2268 nfscl_initiate_recovery(owp->nfsow_clp);
2272 else if (dp != NULL)
2273 FREE((caddr_t)dp, M_NFSCLDELEG);
2274 mbuf_freem(nd->nd_mrep);
2282 nfsrpc_remove(vnode_t dvp, char *name, int namelen, vnode_t vp,
2283 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap, int *dattrflagp,
2287 struct nfsrv_descript nfsd, *nd = &nfsd;
2289 struct nfsmount *nmp;
2290 nfsv4stateid_t dstateid;
2291 int error, ret = 0, i;
2294 if (namelen > NFS_MAXNAMLEN)
2295 return (ENAMETOOLONG);
2296 nmp = VFSTONFS(vnode_mount(dvp));
2298 if (NFSHASNFSV4(nmp) && ret == 0) {
2299 ret = nfscl_removedeleg(vp, p, &dstateid);
2301 NFSCL_REQSTART(nd, NFSPROC_RETDELEGREMOVE, vp);
2302 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID +
2304 if (NFSHASNFSV4N(nmp))
2307 *tl++ = dstateid.seqid;
2308 *tl++ = dstateid.other[0];
2309 *tl++ = dstateid.other[1];
2310 *tl++ = dstateid.other[2];
2311 *tl = txdr_unsigned(NFSV4OP_PUTFH);
2313 (void) nfsm_fhtom(nd, np->n_fhp->nfh_fh,
2314 np->n_fhp->nfh_len, 0);
2315 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2316 *tl = txdr_unsigned(NFSV4OP_REMOVE);
2322 NFSCL_REQSTART(nd, NFSPROC_REMOVE, dvp);
2323 (void) nfsm_strtom(nd, name, namelen);
2324 error = nfscl_request(nd, dvp, p, cred, dstuff);
2327 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) {
2328 /* For NFSv4, parse out any Delereturn replies. */
2329 if (ret > 0 && nd->nd_repstat != 0 &&
2330 (nd->nd_flag & ND_NOMOREDATA)) {
2332 * If the Delegreturn failed, try again without
2333 * it. The server will Recall, as required.
2335 mbuf_freem(nd->nd_mrep);
2338 for (i = 0; i < (ret * 2); i++) {
2339 if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) ==
2341 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2343 nd->nd_flag |= ND_NOMOREDATA;
2346 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
2348 if (nd->nd_repstat && !error)
2349 error = nd->nd_repstat;
2351 mbuf_freem(nd->nd_mrep);
2356 * Do an nfs rename rpc.
2359 nfsrpc_rename(vnode_t fdvp, vnode_t fvp, char *fnameptr, int fnamelen,
2360 vnode_t tdvp, vnode_t tvp, char *tnameptr, int tnamelen, struct ucred *cred,
2361 NFSPROC_T *p, struct nfsvattr *fnap, struct nfsvattr *tnap,
2362 int *fattrflagp, int *tattrflagp, void *fstuff, void *tstuff)
2365 struct nfsrv_descript nfsd, *nd = &nfsd;
2366 struct nfsmount *nmp;
2368 nfsattrbit_t attrbits;
2369 nfsv4stateid_t fdstateid, tdstateid;
2370 int error = 0, ret = 0, gottd = 0, gotfd = 0, i;
2374 nmp = VFSTONFS(vnode_mount(fdvp));
2375 if (fnamelen > NFS_MAXNAMLEN || tnamelen > NFS_MAXNAMLEN)
2376 return (ENAMETOOLONG);
2378 if (NFSHASNFSV4(nmp) && ret == 0) {
2379 ret = nfscl_renamedeleg(fvp, &fdstateid, &gotfd, tvp,
2380 &tdstateid, &gottd, p);
2381 if (gotfd && gottd) {
2382 NFSCL_REQSTART(nd, NFSPROC_RETDELEGRENAME2, fvp);
2384 NFSCL_REQSTART(nd, NFSPROC_RETDELEGRENAME1, fvp);
2386 NFSCL_REQSTART(nd, NFSPROC_RETDELEGRENAME1, tvp);
2389 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID);
2390 if (NFSHASNFSV4N(nmp))
2393 *tl++ = fdstateid.seqid;
2394 *tl++ = fdstateid.other[0];
2395 *tl++ = fdstateid.other[1];
2396 *tl = fdstateid.other[2];
2398 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2399 *tl = txdr_unsigned(NFSV4OP_PUTFH);
2401 (void) nfsm_fhtom(nd, np->n_fhp->nfh_fh,
2402 np->n_fhp->nfh_len, 0);
2403 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2404 *tl = txdr_unsigned(NFSV4OP_DELEGRETURN);
2408 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID);
2409 if (NFSHASNFSV4N(nmp))
2412 *tl++ = tdstateid.seqid;
2413 *tl++ = tdstateid.other[0];
2414 *tl++ = tdstateid.other[1];
2415 *tl = tdstateid.other[2];
2418 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2419 *tl = txdr_unsigned(NFSV4OP_PUTFH);
2421 (void) nfsm_fhtom(nd, np->n_fhp->nfh_fh,
2422 np->n_fhp->nfh_len, 0);
2423 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2424 *tl = txdr_unsigned(NFSV4OP_SAVEFH);
2430 NFSCL_REQSTART(nd, NFSPROC_RENAME, fdvp);
2431 if (nd->nd_flag & ND_NFSV4) {
2432 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2433 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2434 NFSWCCATTR_ATTRBIT(&attrbits);
2435 (void) nfsrv_putattrbit(nd, &attrbits);
2436 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2437 *tl = txdr_unsigned(NFSV4OP_PUTFH);
2438 (void) nfsm_fhtom(nd, VTONFS(tdvp)->n_fhp->nfh_fh,
2439 VTONFS(tdvp)->n_fhp->nfh_len, 0);
2440 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2441 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2442 (void) nfsrv_putattrbit(nd, &attrbits);
2443 nd->nd_flag |= ND_V4WCCATTR;
2444 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2445 *tl = txdr_unsigned(NFSV4OP_RENAME);
2447 (void) nfsm_strtom(nd, fnameptr, fnamelen);
2448 if (!(nd->nd_flag & ND_NFSV4))
2449 (void) nfsm_fhtom(nd, VTONFS(tdvp)->n_fhp->nfh_fh,
2450 VTONFS(tdvp)->n_fhp->nfh_len, 0);
2451 (void) nfsm_strtom(nd, tnameptr, tnamelen);
2452 error = nfscl_request(nd, fdvp, p, cred, fstuff);
2455 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) {
2456 /* For NFSv4, parse out any Delereturn replies. */
2457 if (ret > 0 && nd->nd_repstat != 0 &&
2458 (nd->nd_flag & ND_NOMOREDATA)) {
2460 * If the Delegreturn failed, try again without
2461 * it. The server will Recall, as required.
2463 mbuf_freem(nd->nd_mrep);
2466 for (i = 0; i < (ret * 2); i++) {
2467 if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) ==
2469 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2471 if (i == 0 && ret > 1) {
2473 * If the Delegreturn failed, try again
2474 * without it. The server will Recall, as
2476 * If ret > 1, the first iteration of this
2477 * loop is the second DelegReturn result.
2479 mbuf_freem(nd->nd_mrep);
2482 nd->nd_flag |= ND_NOMOREDATA;
2487 /* Now, the first wcc attribute reply. */
2488 if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) == ND_NFSV4) {
2489 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2491 nd->nd_flag |= ND_NOMOREDATA;
2493 error = nfscl_wcc_data(nd, fdvp, fnap, fattrflagp, NULL,
2495 /* and the second wcc attribute reply. */
2496 if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) == ND_NFSV4 &&
2498 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2500 nd->nd_flag |= ND_NOMOREDATA;
2503 error = nfscl_wcc_data(nd, tdvp, tnap, tattrflagp,
2506 if (nd->nd_repstat && !error)
2507 error = nd->nd_repstat;
2509 mbuf_freem(nd->nd_mrep);
2514 * nfs hard link create rpc
2517 nfsrpc_link(vnode_t dvp, vnode_t vp, char *name, int namelen,
2518 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap,
2519 struct nfsvattr *nap, int *attrflagp, int *dattrflagp, void *dstuff)
2522 struct nfsrv_descript nfsd, *nd = &nfsd;
2523 nfsattrbit_t attrbits;
2528 if (namelen > NFS_MAXNAMLEN)
2529 return (ENAMETOOLONG);
2530 NFSCL_REQSTART(nd, NFSPROC_LINK, vp);
2531 if (nd->nd_flag & ND_NFSV4) {
2532 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2533 *tl = txdr_unsigned(NFSV4OP_PUTFH);
2535 (void) nfsm_fhtom(nd, VTONFS(dvp)->n_fhp->nfh_fh,
2536 VTONFS(dvp)->n_fhp->nfh_len, 0);
2537 if (nd->nd_flag & ND_NFSV4) {
2538 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2539 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2540 NFSWCCATTR_ATTRBIT(&attrbits);
2541 (void) nfsrv_putattrbit(nd, &attrbits);
2542 nd->nd_flag |= ND_V4WCCATTR;
2543 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2544 *tl = txdr_unsigned(NFSV4OP_LINK);
2546 (void) nfsm_strtom(nd, name, namelen);
2547 error = nfscl_request(nd, vp, p, cred, dstuff);
2550 if (nd->nd_flag & ND_NFSV3) {
2551 error = nfscl_postop_attr(nd, nap, attrflagp, dstuff);
2553 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp,
2555 } else if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) == ND_NFSV4) {
2557 * First, parse out the PutFH and Getattr result.
2559 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2561 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2563 nd->nd_flag |= ND_NOMOREDATA;
2565 * Get the pre-op attributes.
2567 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
2569 if (nd->nd_repstat && !error)
2570 error = nd->nd_repstat;
2572 mbuf_freem(nd->nd_mrep);
2577 * nfs symbolic link create rpc
2580 nfsrpc_symlink(vnode_t dvp, char *name, int namelen, char *target,
2581 struct vattr *vap, struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap,
2582 struct nfsvattr *nnap, struct nfsfh **nfhpp, int *attrflagp,
2583 int *dattrflagp, void *dstuff)
2586 struct nfsrv_descript nfsd, *nd = &nfsd;
2587 struct nfsmount *nmp;
2588 int slen, error = 0;
2593 nmp = VFSTONFS(vnode_mount(dvp));
2594 slen = strlen(target);
2595 if (slen > NFS_MAXPATHLEN || namelen > NFS_MAXNAMLEN)
2596 return (ENAMETOOLONG);
2597 NFSCL_REQSTART(nd, NFSPROC_SYMLINK, dvp);
2598 if (nd->nd_flag & ND_NFSV4) {
2599 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2600 *tl = txdr_unsigned(NFLNK);
2601 (void) nfsm_strtom(nd, target, slen);
2603 (void) nfsm_strtom(nd, name, namelen);
2604 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4))
2605 nfscl_fillsattr(nd, vap, dvp, 0, 0);
2606 if (!(nd->nd_flag & ND_NFSV4))
2607 (void) nfsm_strtom(nd, target, slen);
2608 if (nd->nd_flag & ND_NFSV2)
2609 nfscl_fillsattr(nd, vap, dvp, NFSSATTR_SIZENEG1, 0);
2610 error = nfscl_request(nd, dvp, p, cred, dstuff);
2613 if (nd->nd_flag & ND_NFSV4)
2614 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
2615 if ((nd->nd_flag & ND_NFSV3) && !error) {
2616 if (!nd->nd_repstat)
2617 error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp);
2619 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp,
2622 if (nd->nd_repstat && !error)
2623 error = nd->nd_repstat;
2624 mbuf_freem(nd->nd_mrep);
2626 * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry.
2627 * Only do this if vfs.nfs.ignore_eexist is set.
2628 * Never do this for NFSv4.1 or later minor versions, since sessions
2629 * should guarantee "exactly once" RPC semantics.
2631 if (error == EEXIST && nfsignore_eexist != 0 && (!NFSHASNFSV4(nmp) ||
2632 nmp->nm_minorvers == 0))
2641 nfsrpc_mkdir(vnode_t dvp, char *name, int namelen, struct vattr *vap,
2642 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap,
2643 struct nfsvattr *nnap, struct nfsfh **nfhpp, int *attrflagp,
2644 int *dattrflagp, void *dstuff)
2647 struct nfsrv_descript nfsd, *nd = &nfsd;
2648 nfsattrbit_t attrbits;
2651 struct nfsmount *nmp;
2656 nmp = VFSTONFS(vnode_mount(dvp));
2657 fhp = VTONFS(dvp)->n_fhp;
2658 if (namelen > NFS_MAXNAMLEN)
2659 return (ENAMETOOLONG);
2660 NFSCL_REQSTART(nd, NFSPROC_MKDIR, dvp);
2661 if (nd->nd_flag & ND_NFSV4) {
2662 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2663 *tl = txdr_unsigned(NFDIR);
2665 (void) nfsm_strtom(nd, name, namelen);
2666 nfscl_fillsattr(nd, vap, dvp, NFSSATTR_SIZENEG1, 0);
2667 if (nd->nd_flag & ND_NFSV4) {
2668 NFSGETATTR_ATTRBIT(&attrbits);
2669 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2670 *tl++ = txdr_unsigned(NFSV4OP_GETFH);
2671 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2672 (void) nfsrv_putattrbit(nd, &attrbits);
2673 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2674 *tl = txdr_unsigned(NFSV4OP_PUTFH);
2675 (void) nfsm_fhtom(nd, fhp->nfh_fh, fhp->nfh_len, 0);
2676 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2677 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2678 (void) nfsrv_putattrbit(nd, &attrbits);
2680 error = nfscl_request(nd, dvp, p, cred, dstuff);
2683 if (nd->nd_flag & ND_NFSV4)
2684 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
2685 if (!nd->nd_repstat && !error) {
2686 if (nd->nd_flag & ND_NFSV4) {
2687 NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
2688 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
2691 error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp);
2692 if (error == 0 && (nd->nd_flag & ND_NFSV4) != 0) {
2693 /* Get rid of the PutFH and Getattr status values. */
2694 NFSM_DISSECT(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
2695 /* Load the directory attributes. */
2696 error = nfsm_loadattr(nd, dnap);
2701 if ((nd->nd_flag & ND_NFSV3) && !error)
2702 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
2703 if (nd->nd_repstat && !error)
2704 error = nd->nd_repstat;
2706 mbuf_freem(nd->nd_mrep);
2708 * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry.
2709 * Only do this if vfs.nfs.ignore_eexist is set.
2710 * Never do this for NFSv4.1 or later minor versions, since sessions
2711 * should guarantee "exactly once" RPC semantics.
2713 if (error == EEXIST && nfsignore_eexist != 0 && (!NFSHASNFSV4(nmp) ||
2714 nmp->nm_minorvers == 0))
2720 * nfs remove directory call
2723 nfsrpc_rmdir(vnode_t dvp, char *name, int namelen, struct ucred *cred,
2724 NFSPROC_T *p, struct nfsvattr *dnap, int *dattrflagp, void *dstuff)
2726 struct nfsrv_descript nfsd, *nd = &nfsd;
2730 if (namelen > NFS_MAXNAMLEN)
2731 return (ENAMETOOLONG);
2732 NFSCL_REQSTART(nd, NFSPROC_RMDIR, dvp);
2733 (void) nfsm_strtom(nd, name, namelen);
2734 error = nfscl_request(nd, dvp, p, cred, dstuff);
2737 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4))
2738 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
2739 if (nd->nd_repstat && !error)
2740 error = nd->nd_repstat;
2741 mbuf_freem(nd->nd_mrep);
2743 * Kludge: Map ENOENT => 0 assuming that you have a reply to a retry.
2745 if (error == ENOENT)
2752 * Always returns with either uio_resid unchanged, if you are at the
2753 * end of the directory, or uio_resid == 0, with all DIRBLKSIZ chunks
2755 * I felt this would allow caching of directory blocks more easily
2756 * than returning a pertially filled block.
2757 * Directory offset cookies:
2758 * Oh my, what to do with them...
2759 * I can think of three ways to deal with them:
2760 * 1 - have the layer above these RPCs maintain a map between logical
2761 * directory byte offsets and the NFS directory offset cookies
2762 * 2 - pass the opaque directory offset cookies up into userland
2763 * and let the libc functions deal with them, via the system call
2764 * 3 - return them to userland in the "struct dirent", so future versions
2765 * of libc can use them and do whatever is necessary to make things work
2766 * above these rpc calls, in the meantime
2767 * For now, I do #3 by "hiding" the directory offset cookies after the
2768 * d_name field in struct dirent. This is space inside d_reclen that
2769 * will be ignored by anything that doesn't know about them.
2770 * The directory offset cookies are filled in as the last 8 bytes of
2771 * each directory entry, after d_name. Someday, the userland libc
2772 * functions may be able to use these. In the meantime, it satisfies
2773 * OpenBSD's requirements for cookies being returned.
2774 * If expects the directory offset cookie for the read to be in uio_offset
2775 * and returns the one for the next entry after this directory block in
2779 nfsrpc_readdir(vnode_t vp, struct uio *uiop, nfsuint64 *cookiep,
2780 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp,
2781 int *eofp, void *stuff)
2784 struct dirent *dp = NULL;
2786 nfsquad_t cookie, ncookie;
2787 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
2788 struct nfsnode *dnp = VTONFS(vp);
2789 struct nfsvattr nfsva;
2790 struct nfsrv_descript nfsd, *nd = &nfsd;
2791 int error = 0, tlen, more_dirs = 1, blksiz = 0, bigenough = 1;
2792 int reqsize, tryformoredirs = 1, readsize, eof = 0, gotmnton = 0;
2793 u_int64_t dotfileid, dotdotfileid = 0, fakefileno = UINT64_MAX;
2795 nfsattrbit_t attrbits, dattrbits;
2796 u_int32_t rderr, *tl2 = NULL;
2799 KASSERT(uiop->uio_iovcnt == 1 &&
2800 (uio_uio_resid(uiop) & (DIRBLKSIZ - 1)) == 0,
2801 ("nfs readdirrpc bad uio"));
2804 * There is no point in reading a lot more than uio_resid, however
2805 * adding one additional DIRBLKSIZ makes sense. Since uio_resid
2806 * and nm_readdirsize are both exact multiples of DIRBLKSIZ, this
2807 * will never make readsize > nm_readdirsize.
2809 readsize = nmp->nm_readdirsize;
2810 if (readsize > uio_uio_resid(uiop))
2811 readsize = uio_uio_resid(uiop) + DIRBLKSIZ;
2816 tresid = uio_uio_resid(uiop);
2817 cookie.lval[0] = cookiep->nfsuquad[0];
2818 cookie.lval[1] = cookiep->nfsuquad[1];
2822 * For NFSv4, first create the "." and ".." entries.
2824 if (NFSHASNFSV4(nmp)) {
2825 reqsize = 6 * NFSX_UNSIGNED;
2826 NFSGETATTR_ATTRBIT(&dattrbits);
2827 NFSZERO_ATTRBIT(&attrbits);
2828 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_FILEID);
2829 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TYPE);
2830 if (NFSISSET_ATTRBIT(&dnp->n_vattr.na_suppattr,
2831 NFSATTRBIT_MOUNTEDONFILEID)) {
2832 NFSSETBIT_ATTRBIT(&attrbits,
2833 NFSATTRBIT_MOUNTEDONFILEID);
2837 * Must fake it. Use the fileno, except when the
2838 * fsid is != to that of the directory. For that
2839 * case, generate a fake fileno that is not the same.
2841 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_FSID);
2846 * Joy, oh joy. For V4 we get to hand craft '.' and '..'.
2848 if (uiop->uio_offset == 0) {
2849 NFSCL_REQSTART(nd, NFSPROC_LOOKUPP, vp);
2850 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2851 *tl++ = txdr_unsigned(NFSV4OP_GETFH);
2852 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2853 (void) nfsrv_putattrbit(nd, &attrbits);
2854 error = nfscl_request(nd, vp, p, cred, stuff);
2857 dotfileid = 0; /* Fake out the compiler. */
2858 if ((nd->nd_flag & ND_NOMOREDATA) == 0) {
2859 error = nfsm_loadattr(nd, &nfsva);
2862 dotfileid = nfsva.na_fileid;
2864 if (nd->nd_repstat == 0) {
2865 NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
2866 len = fxdr_unsigned(int, *(tl + 4));
2867 if (len > 0 && len <= NFSX_V4FHMAX)
2868 error = nfsm_advance(nd, NFSM_RNDUP(len), -1);
2872 NFSM_DISSECT(tl, u_int32_t *, 2*NFSX_UNSIGNED);
2873 nfsva.na_mntonfileno = UINT64_MAX;
2874 error = nfsv4_loadattr(nd, NULL, &nfsva, NULL,
2875 NULL, 0, NULL, NULL, NULL, NULL, NULL, 0,
2876 NULL, NULL, NULL, p, cred);
2878 dotdotfileid = dotfileid;
2879 } else if (gotmnton) {
2880 if (nfsva.na_mntonfileno != UINT64_MAX)
2881 dotdotfileid = nfsva.na_mntonfileno;
2883 dotdotfileid = nfsva.na_fileid;
2884 } else if (nfsva.na_filesid[0] ==
2885 dnp->n_vattr.na_filesid[0] &&
2886 nfsva.na_filesid[1] ==
2887 dnp->n_vattr.na_filesid[1]) {
2888 dotdotfileid = nfsva.na_fileid;
2892 } while (fakefileno ==
2894 dotdotfileid = fakefileno;
2897 } else if (nd->nd_repstat == NFSERR_NOENT) {
2899 * Lookupp returns NFSERR_NOENT when we are
2900 * at the root, so just use the current dir.
2903 dotdotfileid = dotfileid;
2905 error = nd->nd_repstat;
2907 mbuf_freem(nd->nd_mrep);
2911 dp = (struct dirent *)uio_iov_base(uiop);
2913 dp->d_type = DT_DIR;
2914 dp->d_fileno = dotfileid;
2916 *((uint64_t *)dp->d_name) = 0; /* Zero pad it. */
2917 dp->d_name[0] = '.';
2918 dp->d_reclen = _GENERIC_DIRSIZ(dp) + NFSX_HYPER;
2920 * Just make these offset cookie 0.
2922 tl = (u_int32_t *)&dp->d_name[8];
2925 blksiz += dp->d_reclen;
2926 uio_uio_resid_add(uiop, -(dp->d_reclen));
2927 uiop->uio_offset += dp->d_reclen;
2928 uio_iov_base_add(uiop, dp->d_reclen);
2929 uio_iov_len_add(uiop, -(dp->d_reclen));
2930 dp = (struct dirent *)uio_iov_base(uiop);
2932 dp->d_type = DT_DIR;
2933 dp->d_fileno = dotdotfileid;
2935 *((uint64_t *)dp->d_name) = 0;
2936 dp->d_name[0] = '.';
2937 dp->d_name[1] = '.';
2938 dp->d_reclen = _GENERIC_DIRSIZ(dp) + NFSX_HYPER;
2940 * Just make these offset cookie 0.
2942 tl = (u_int32_t *)&dp->d_name[8];
2945 blksiz += dp->d_reclen;
2946 uio_uio_resid_add(uiop, -(dp->d_reclen));
2947 uiop->uio_offset += dp->d_reclen;
2948 uio_iov_base_add(uiop, dp->d_reclen);
2949 uio_iov_len_add(uiop, -(dp->d_reclen));
2951 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_RDATTRERROR);
2953 reqsize = 5 * NFSX_UNSIGNED;
2958 * Loop around doing readdir rpc's of size readsize.
2959 * The stopping criteria is EOF or buffer full.
2961 while (more_dirs && bigenough) {
2963 NFSCL_REQSTART(nd, NFSPROC_READDIR, vp);
2964 if (nd->nd_flag & ND_NFSV2) {
2965 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2966 *tl++ = cookie.lval[1];
2967 *tl = txdr_unsigned(readsize);
2969 NFSM_BUILD(tl, u_int32_t *, reqsize);
2970 *tl++ = cookie.lval[0];
2971 *tl++ = cookie.lval[1];
2972 if (cookie.qval == 0) {
2977 *tl++ = dnp->n_cookieverf.nfsuquad[0];
2978 *tl++ = dnp->n_cookieverf.nfsuquad[1];
2981 if (nd->nd_flag & ND_NFSV4) {
2982 *tl++ = txdr_unsigned(readsize);
2983 *tl = txdr_unsigned(readsize);
2984 (void) nfsrv_putattrbit(nd, &attrbits);
2985 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2986 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2987 (void) nfsrv_putattrbit(nd, &dattrbits);
2989 *tl = txdr_unsigned(readsize);
2992 error = nfscl_request(nd, vp, p, cred, stuff);
2995 if (!(nd->nd_flag & ND_NFSV2)) {
2996 if (nd->nd_flag & ND_NFSV3)
2997 error = nfscl_postop_attr(nd, nap, attrflagp,
2999 if (!nd->nd_repstat && !error) {
3000 NFSM_DISSECT(tl, u_int32_t *, NFSX_HYPER);
3002 dnp->n_cookieverf.nfsuquad[0] = *tl++;
3003 dnp->n_cookieverf.nfsuquad[1] = *tl;
3007 if (nd->nd_repstat || error) {
3009 error = nd->nd_repstat;
3012 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3013 more_dirs = fxdr_unsigned(int, *tl);
3017 /* loop through the dir entries, doctoring them to 4bsd form */
3018 while (more_dirs && bigenough) {
3019 if (nd->nd_flag & ND_NFSV4) {
3020 NFSM_DISSECT(tl, u_int32_t *, 3*NFSX_UNSIGNED);
3021 ncookie.lval[0] = *tl++;
3022 ncookie.lval[1] = *tl++;
3023 len = fxdr_unsigned(int, *tl);
3024 } else if (nd->nd_flag & ND_NFSV3) {
3025 NFSM_DISSECT(tl, u_int32_t *, 3*NFSX_UNSIGNED);
3026 nfsva.na_fileid = fxdr_hyper(tl);
3028 len = fxdr_unsigned(int, *tl);
3030 NFSM_DISSECT(tl, u_int32_t *, 2*NFSX_UNSIGNED);
3031 nfsva.na_fileid = fxdr_unsigned(uint64_t,
3033 len = fxdr_unsigned(int, *tl);
3035 if (len <= 0 || len > NFS_MAXNAMLEN) {
3039 tlen = roundup2(len, 8);
3041 tlen += 8; /* To ensure null termination. */
3042 left = DIRBLKSIZ - blksiz;
3043 if (_GENERIC_DIRLEN(len) + NFSX_HYPER > left) {
3044 dp->d_reclen += left;
3045 uio_iov_base_add(uiop, left);
3046 uio_iov_len_add(uiop, -(left));
3047 uio_uio_resid_add(uiop, -(left));
3048 uiop->uio_offset += left;
3051 if (_GENERIC_DIRLEN(len) + NFSX_HYPER >
3052 uio_uio_resid(uiop))
3055 dp = (struct dirent *)uio_iov_base(uiop);
3058 dp->d_reclen = _GENERIC_DIRLEN(len) +
3060 dp->d_type = DT_UNKNOWN;
3061 blksiz += dp->d_reclen;
3062 if (blksiz == DIRBLKSIZ)
3064 uio_uio_resid_add(uiop, -(DIRHDSIZ));
3065 uiop->uio_offset += DIRHDSIZ;
3066 uio_iov_base_add(uiop, DIRHDSIZ);
3067 uio_iov_len_add(uiop, -(DIRHDSIZ));
3068 error = nfsm_mbufuio(nd, uiop, len);
3071 cp = uio_iov_base(uiop);
3073 *cp = '\0'; /* null terminate */
3074 cp += tlen; /* points to cookie storage */
3075 tl2 = (u_int32_t *)cp;
3076 uio_iov_base_add(uiop, (tlen + NFSX_HYPER));
3077 uio_iov_len_add(uiop, -(tlen + NFSX_HYPER));
3078 uio_uio_resid_add(uiop, -(tlen + NFSX_HYPER));
3079 uiop->uio_offset += (tlen + NFSX_HYPER);
3081 error = nfsm_advance(nd, NFSM_RNDUP(len), -1);
3085 if (nd->nd_flag & ND_NFSV4) {
3087 nfsva.na_mntonfileno = UINT64_MAX;
3088 error = nfsv4_loadattr(nd, NULL, &nfsva, NULL,
3089 NULL, 0, NULL, NULL, NULL, NULL, NULL, 0,
3090 NULL, NULL, &rderr, p, cred);
3093 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3094 } else if (nd->nd_flag & ND_NFSV3) {
3095 NFSM_DISSECT(tl, u_int32_t *, 3*NFSX_UNSIGNED);
3096 ncookie.lval[0] = *tl++;
3097 ncookie.lval[1] = *tl++;
3099 NFSM_DISSECT(tl, u_int32_t *, 2*NFSX_UNSIGNED);
3100 ncookie.lval[0] = 0;
3101 ncookie.lval[1] = *tl++;
3104 if (nd->nd_flag & ND_NFSV4) {
3109 if (nfsva.na_mntonfileno != UINT64_MAX)
3110 dp->d_fileno = nfsva.na_mntonfileno;
3112 dp->d_fileno = nfsva.na_fileid;
3113 } else if (nfsva.na_filesid[0] ==
3114 dnp->n_vattr.na_filesid[0] &&
3115 nfsva.na_filesid[1] ==
3116 dnp->n_vattr.na_filesid[1]) {
3117 dp->d_fileno = nfsva.na_fileid;
3121 } while (fakefileno ==
3123 dp->d_fileno = fakefileno;
3125 dp->d_type = vtonfs_dtype(nfsva.na_type);
3128 dp->d_fileno = nfsva.na_fileid;
3130 *tl2++ = cookiep->nfsuquad[0] = cookie.lval[0] =
3132 *tl2 = cookiep->nfsuquad[1] = cookie.lval[1] =
3135 more_dirs = fxdr_unsigned(int, *tl);
3138 * If at end of rpc data, get the eof boolean
3141 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3142 eof = fxdr_unsigned(int, *tl);
3145 if (nd->nd_flag & ND_NFSV4) {
3146 error = nfscl_postop_attr(nd, nap, attrflagp,
3152 mbuf_freem(nd->nd_mrep);
3156 * Fill last record, iff any, out to a multiple of DIRBLKSIZ
3157 * by increasing d_reclen for the last record.
3160 left = DIRBLKSIZ - blksiz;
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;
3169 * If returning no data, assume end of file.
3170 * If not bigenough, return not end of file, since you aren't
3171 * returning all the data
3172 * Otherwise, return the eof flag from the server.
3175 if (tresid == ((size_t)(uio_uio_resid(uiop))))
3177 else if (!bigenough)
3184 * Add extra empty records to any remaining DIRBLKSIZ chunks.
3186 while (uio_uio_resid(uiop) > 0 && uio_uio_resid(uiop) != tresid) {
3187 dp = (struct dirent *)uio_iov_base(uiop);
3188 dp->d_type = DT_UNKNOWN;
3191 dp->d_name[0] = '\0';
3192 tl = (u_int32_t *)&dp->d_name[4];
3193 *tl++ = cookie.lval[0];
3194 *tl = cookie.lval[1];
3195 dp->d_reclen = DIRBLKSIZ;
3196 uio_iov_base_add(uiop, DIRBLKSIZ);
3197 uio_iov_len_add(uiop, -(DIRBLKSIZ));
3198 uio_uio_resid_add(uiop, -(DIRBLKSIZ));
3199 uiop->uio_offset += DIRBLKSIZ;
3203 if (nd->nd_mrep != NULL)
3204 mbuf_freem(nd->nd_mrep);
3210 * NFS V3 readdir plus RPC. Used in place of nfsrpc_readdir().
3211 * (Also used for NFS V4 when mount flag set.)
3212 * (ditto above w.r.t. multiple of DIRBLKSIZ, etc.)
3215 nfsrpc_readdirplus(vnode_t vp, struct uio *uiop, nfsuint64 *cookiep,
3216 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp,
3217 int *eofp, void *stuff)
3220 struct dirent *dp = NULL;
3222 vnode_t newvp = NULLVP;
3223 struct nfsrv_descript nfsd, *nd = &nfsd;
3224 struct nameidata nami, *ndp = &nami;
3225 struct componentname *cnp = &ndp->ni_cnd;
3226 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
3227 struct nfsnode *dnp = VTONFS(vp), *np;
3228 struct nfsvattr nfsva;
3230 nfsquad_t cookie, ncookie;
3231 int error = 0, tlen, more_dirs = 1, blksiz = 0, bigenough = 1;
3232 int attrflag, tryformoredirs = 1, eof = 0, gotmnton = 0;
3233 int isdotdot = 0, unlocknewvp = 0;
3234 u_int64_t dotfileid, dotdotfileid = 0, fakefileno = UINT64_MAX;
3235 u_int64_t fileno = 0;
3237 nfsattrbit_t attrbits, dattrbits;
3239 u_int32_t *tl2 = NULL, rderr;
3240 struct timespec dctime;
3242 KASSERT(uiop->uio_iovcnt == 1 &&
3243 (uio_uio_resid(uiop) & (DIRBLKSIZ - 1)) == 0,
3244 ("nfs readdirplusrpc bad uio"));
3245 timespecclear(&dctime);
3251 cookie.lval[0] = cookiep->nfsuquad[0];
3252 cookie.lval[1] = cookiep->nfsuquad[1];
3253 tresid = uio_uio_resid(uiop);
3256 * For NFSv4, first create the "." and ".." entries.
3258 if (NFSHASNFSV4(nmp)) {
3259 NFSGETATTR_ATTRBIT(&dattrbits);
3260 NFSZERO_ATTRBIT(&attrbits);
3261 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_FILEID);
3262 if (NFSISSET_ATTRBIT(&dnp->n_vattr.na_suppattr,
3263 NFSATTRBIT_MOUNTEDONFILEID)) {
3264 NFSSETBIT_ATTRBIT(&attrbits,
3265 NFSATTRBIT_MOUNTEDONFILEID);
3269 * Must fake it. Use the fileno, except when the
3270 * fsid is != to that of the directory. For that
3271 * case, generate a fake fileno that is not the same.
3273 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_FSID);
3278 * Joy, oh joy. For V4 we get to hand craft '.' and '..'.
3280 if (uiop->uio_offset == 0) {
3281 NFSCL_REQSTART(nd, NFSPROC_LOOKUPP, vp);
3282 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
3283 *tl++ = txdr_unsigned(NFSV4OP_GETFH);
3284 *tl = txdr_unsigned(NFSV4OP_GETATTR);
3285 (void) nfsrv_putattrbit(nd, &attrbits);
3286 error = nfscl_request(nd, vp, p, cred, stuff);
3289 dotfileid = 0; /* Fake out the compiler. */
3290 if ((nd->nd_flag & ND_NOMOREDATA) == 0) {
3291 error = nfsm_loadattr(nd, &nfsva);
3294 dctime = nfsva.na_ctime;
3295 dotfileid = nfsva.na_fileid;
3297 if (nd->nd_repstat == 0) {
3298 NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
3299 len = fxdr_unsigned(int, *(tl + 4));
3300 if (len > 0 && len <= NFSX_V4FHMAX)
3301 error = nfsm_advance(nd, NFSM_RNDUP(len), -1);
3305 NFSM_DISSECT(tl, u_int32_t *, 2*NFSX_UNSIGNED);
3306 nfsva.na_mntonfileno = UINT64_MAX;
3307 error = nfsv4_loadattr(nd, NULL, &nfsva, NULL,
3308 NULL, 0, NULL, NULL, NULL, NULL, NULL, 0,
3309 NULL, NULL, NULL, p, cred);
3311 dotdotfileid = dotfileid;
3312 } else if (gotmnton) {
3313 if (nfsva.na_mntonfileno != UINT64_MAX)
3314 dotdotfileid = nfsva.na_mntonfileno;
3316 dotdotfileid = nfsva.na_fileid;
3317 } else if (nfsva.na_filesid[0] ==
3318 dnp->n_vattr.na_filesid[0] &&
3319 nfsva.na_filesid[1] ==
3320 dnp->n_vattr.na_filesid[1]) {
3321 dotdotfileid = nfsva.na_fileid;
3325 } while (fakefileno ==
3327 dotdotfileid = fakefileno;
3330 } else if (nd->nd_repstat == NFSERR_NOENT) {
3332 * Lookupp returns NFSERR_NOENT when we are
3333 * at the root, so just use the current dir.
3336 dotdotfileid = dotfileid;
3338 error = nd->nd_repstat;
3340 mbuf_freem(nd->nd_mrep);
3344 dp = (struct dirent *)uio_iov_base(uiop);
3346 dp->d_type = DT_DIR;
3347 dp->d_fileno = dotfileid;
3349 *((uint64_t *)dp->d_name) = 0; /* Zero pad it. */
3350 dp->d_name[0] = '.';
3351 dp->d_reclen = _GENERIC_DIRSIZ(dp) + NFSX_HYPER;
3353 * Just make these offset cookie 0.
3355 tl = (u_int32_t *)&dp->d_name[8];
3358 blksiz += dp->d_reclen;
3359 uio_uio_resid_add(uiop, -(dp->d_reclen));
3360 uiop->uio_offset += dp->d_reclen;
3361 uio_iov_base_add(uiop, dp->d_reclen);
3362 uio_iov_len_add(uiop, -(dp->d_reclen));
3363 dp = (struct dirent *)uio_iov_base(uiop);
3365 dp->d_type = DT_DIR;
3366 dp->d_fileno = dotdotfileid;
3368 *((uint64_t *)dp->d_name) = 0;
3369 dp->d_name[0] = '.';
3370 dp->d_name[1] = '.';
3371 dp->d_reclen = _GENERIC_DIRSIZ(dp) + NFSX_HYPER;
3373 * Just make these offset cookie 0.
3375 tl = (u_int32_t *)&dp->d_name[8];
3378 blksiz += dp->d_reclen;
3379 uio_uio_resid_add(uiop, -(dp->d_reclen));
3380 uiop->uio_offset += dp->d_reclen;
3381 uio_iov_base_add(uiop, dp->d_reclen);
3382 uio_iov_len_add(uiop, -(dp->d_reclen));
3384 NFSREADDIRPLUS_ATTRBIT(&attrbits);
3386 NFSSETBIT_ATTRBIT(&attrbits,
3387 NFSATTRBIT_MOUNTEDONFILEID);
3391 * Loop around doing readdir rpc's of size nm_readdirsize.
3392 * The stopping criteria is EOF or buffer full.
3394 while (more_dirs && bigenough) {
3396 NFSCL_REQSTART(nd, NFSPROC_READDIRPLUS, vp);
3397 NFSM_BUILD(tl, u_int32_t *, 6 * NFSX_UNSIGNED);
3398 *tl++ = cookie.lval[0];
3399 *tl++ = cookie.lval[1];
3400 if (cookie.qval == 0) {
3405 *tl++ = dnp->n_cookieverf.nfsuquad[0];
3406 *tl++ = dnp->n_cookieverf.nfsuquad[1];
3409 *tl++ = txdr_unsigned(nmp->nm_readdirsize);
3410 *tl = txdr_unsigned(nmp->nm_readdirsize);
3411 if (nd->nd_flag & ND_NFSV4) {
3412 (void) nfsrv_putattrbit(nd, &attrbits);
3413 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
3414 *tl = txdr_unsigned(NFSV4OP_GETATTR);
3415 (void) nfsrv_putattrbit(nd, &dattrbits);
3417 error = nfscl_request(nd, vp, p, cred, stuff);
3420 if (nd->nd_flag & ND_NFSV3)
3421 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
3422 if (nd->nd_repstat || error) {
3424 error = nd->nd_repstat;
3427 if ((nd->nd_flag & ND_NFSV3) != 0 && *attrflagp != 0)
3428 dctime = nap->na_ctime;
3429 NFSM_DISSECT(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
3431 dnp->n_cookieverf.nfsuquad[0] = *tl++;
3432 dnp->n_cookieverf.nfsuquad[1] = *tl++;
3434 more_dirs = fxdr_unsigned(int, *tl);
3438 /* loop through the dir entries, doctoring them to 4bsd form */
3439 while (more_dirs && bigenough) {
3440 NFSM_DISSECT(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
3441 if (nd->nd_flag & ND_NFSV4) {
3442 ncookie.lval[0] = *tl++;
3443 ncookie.lval[1] = *tl++;
3445 fileno = fxdr_hyper(tl);
3448 len = fxdr_unsigned(int, *tl);
3449 if (len <= 0 || len > NFS_MAXNAMLEN) {
3453 tlen = roundup2(len, 8);
3455 tlen += 8; /* To ensure null termination. */
3456 left = DIRBLKSIZ - blksiz;
3457 if (_GENERIC_DIRLEN(len) + NFSX_HYPER > left) {
3458 dp->d_reclen += left;
3459 uio_iov_base_add(uiop, left);
3460 uio_iov_len_add(uiop, -(left));
3461 uio_uio_resid_add(uiop, -(left));
3462 uiop->uio_offset += left;
3465 if (_GENERIC_DIRLEN(len) + NFSX_HYPER >
3466 uio_uio_resid(uiop))
3469 dp = (struct dirent *)uio_iov_base(uiop);
3472 dp->d_reclen = _GENERIC_DIRLEN(len) +
3474 dp->d_type = DT_UNKNOWN;
3475 blksiz += dp->d_reclen;
3476 if (blksiz == DIRBLKSIZ)
3478 uio_uio_resid_add(uiop, -(DIRHDSIZ));
3479 uiop->uio_offset += DIRHDSIZ;
3480 uio_iov_base_add(uiop, DIRHDSIZ);
3481 uio_iov_len_add(uiop, -(DIRHDSIZ));
3482 cnp->cn_nameptr = uio_iov_base(uiop);
3483 cnp->cn_namelen = len;
3485 error = nfsm_mbufuio(nd, uiop, len);
3488 cp = uio_iov_base(uiop);
3491 cp += tlen; /* points to cookie storage */
3492 tl2 = (u_int32_t *)cp;
3493 if (len == 2 && cnp->cn_nameptr[0] == '.' &&
3494 cnp->cn_nameptr[1] == '.')
3498 uio_iov_base_add(uiop, (tlen + NFSX_HYPER));
3499 uio_iov_len_add(uiop, -(tlen + NFSX_HYPER));
3500 uio_uio_resid_add(uiop, -(tlen + NFSX_HYPER));
3501 uiop->uio_offset += (tlen + NFSX_HYPER);
3503 error = nfsm_advance(nd, NFSM_RNDUP(len), -1);
3508 if (nd->nd_flag & ND_NFSV3) {
3509 NFSM_DISSECT(tl, u_int32_t *, 3*NFSX_UNSIGNED);
3510 ncookie.lval[0] = *tl++;
3511 ncookie.lval[1] = *tl++;
3512 attrflag = fxdr_unsigned(int, *tl);
3514 error = nfsm_loadattr(nd, &nfsva);
3518 NFSM_DISSECT(tl,u_int32_t *,NFSX_UNSIGNED);
3520 error = nfsm_getfh(nd, &nfhp);
3524 if (!attrflag && nfhp != NULL) {
3525 FREE((caddr_t)nfhp, M_NFSFH);
3530 nfsva.na_mntonfileno = 0xffffffff;
3531 error = nfsv4_loadattr(nd, NULL, &nfsva, &nfhp,
3532 NULL, 0, NULL, NULL, NULL, NULL, NULL, 0,
3533 NULL, NULL, &rderr, p, cred);
3539 if (nd->nd_flag & ND_NFSV4) {
3542 } else if (gotmnton) {
3543 if (nfsva.na_mntonfileno != 0xffffffff)
3544 dp->d_fileno = nfsva.na_mntonfileno;
3546 dp->d_fileno = nfsva.na_fileid;
3547 } else if (nfsva.na_filesid[0] ==
3548 dnp->n_vattr.na_filesid[0] &&
3549 nfsva.na_filesid[1] ==
3550 dnp->n_vattr.na_filesid[1]) {
3551 dp->d_fileno = nfsva.na_fileid;
3555 } while (fakefileno ==
3557 dp->d_fileno = fakefileno;
3560 dp->d_fileno = fileno;
3562 *tl2++ = cookiep->nfsuquad[0] = cookie.lval[0] =
3564 *tl2 = cookiep->nfsuquad[1] = cookie.lval[1] =
3568 if (NFSRV_CMPFH(nfhp->nfh_fh, nfhp->nfh_len,
3569 dnp->n_fhp->nfh_fh, dnp->n_fhp->nfh_len)) {
3573 FREE((caddr_t)nfhp, M_NFSFH);
3575 } else if (isdotdot != 0) {
3577 * Skip doing a nfscl_nget() call for "..".
3578 * There's a race between acquiring the nfs
3579 * node here and lookups that look for the
3580 * directory being read (in the parent).
3581 * It would try to get a lock on ".." here,
3582 * owning the lock on the directory being
3583 * read. Lookup will hold the lock on ".."
3584 * and try to acquire the lock on the
3585 * directory being read.
3586 * If the directory is unlocked/relocked,
3587 * then there is a LOR with the buflock
3590 free(nfhp, M_NFSFH);
3592 error = nfscl_nget(vnode_mount(vp), vp,
3593 nfhp, cnp, p, &np, NULL, LK_EXCLUSIVE);
3600 if (newvp != NULLVP) {
3601 error = nfscl_loadattrcache(&newvp,
3602 &nfsva, NULL, NULL, 0, 0);
3611 vtonfs_dtype(np->n_vattr.na_type);
3613 NFSCNHASH(cnp, HASHINIT);
3614 if (cnp->cn_namelen <= NCHNAMLEN &&
3615 (newvp->v_type != VDIR ||
3616 dctime.tv_sec != 0)) {
3617 cache_enter_time(ndp->ni_dvp,
3620 newvp->v_type != VDIR ? NULL :
3630 } else if (nfhp != NULL) {
3631 FREE((caddr_t)nfhp, M_NFSFH);
3633 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3634 more_dirs = fxdr_unsigned(int, *tl);
3637 * If at end of rpc data, get the eof boolean
3640 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3641 eof = fxdr_unsigned(int, *tl);
3644 if (nd->nd_flag & ND_NFSV4) {
3645 error = nfscl_postop_attr(nd, nap, attrflagp,
3651 mbuf_freem(nd->nd_mrep);
3655 * Fill last record, iff any, out to a multiple of DIRBLKSIZ
3656 * by increasing d_reclen for the last record.
3659 left = DIRBLKSIZ - blksiz;
3660 dp->d_reclen += left;
3661 uio_iov_base_add(uiop, left);
3662 uio_iov_len_add(uiop, -(left));
3663 uio_uio_resid_add(uiop, -(left));
3664 uiop->uio_offset += left;
3668 * If returning no data, assume end of file.
3669 * If not bigenough, return not end of file, since you aren't
3670 * returning all the data
3671 * Otherwise, return the eof flag from the server.
3674 if (tresid == uio_uio_resid(uiop))
3676 else if (!bigenough)
3683 * Add extra empty records to any remaining DIRBLKSIZ chunks.
3685 while (uio_uio_resid(uiop) > 0 && uio_uio_resid(uiop) != tresid) {
3686 dp = (struct dirent *)uio_iov_base(uiop);
3687 dp->d_type = DT_UNKNOWN;
3690 dp->d_name[0] = '\0';
3691 tl = (u_int32_t *)&dp->d_name[4];
3692 *tl++ = cookie.lval[0];
3693 *tl = cookie.lval[1];
3694 dp->d_reclen = DIRBLKSIZ;
3695 uio_iov_base_add(uiop, DIRBLKSIZ);
3696 uio_iov_len_add(uiop, -(DIRBLKSIZ));
3697 uio_uio_resid_add(uiop, -(DIRBLKSIZ));
3698 uiop->uio_offset += DIRBLKSIZ;
3702 if (nd->nd_mrep != NULL)
3703 mbuf_freem(nd->nd_mrep);
3712 nfsrpc_commit(vnode_t vp, u_quad_t offset, int cnt, struct ucred *cred,
3713 NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, void *stuff)
3716 struct nfsrv_descript nfsd, *nd = &nfsd;
3717 nfsattrbit_t attrbits;
3719 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
3722 NFSCL_REQSTART(nd, NFSPROC_COMMIT, vp);
3723 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
3724 txdr_hyper(offset, tl);
3726 *tl = txdr_unsigned(cnt);
3727 if (nd->nd_flag & ND_NFSV4) {
3729 * And do a Getattr op.
3731 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
3732 *tl = txdr_unsigned(NFSV4OP_GETATTR);
3733 NFSGETATTR_ATTRBIT(&attrbits);
3734 (void) nfsrv_putattrbit(nd, &attrbits);
3736 error = nfscl_request(nd, vp, p, cred, stuff);
3739 error = nfscl_wcc_data(nd, vp, nap, attrflagp, NULL, stuff);
3740 if (!error && !nd->nd_repstat) {
3741 NFSM_DISSECT(tl, u_int32_t *, NFSX_VERF);
3743 if (NFSBCMP(nmp->nm_verf, tl, NFSX_VERF)) {
3744 NFSBCOPY(tl, nmp->nm_verf, NFSX_VERF);
3745 nd->nd_repstat = NFSERR_STALEWRITEVERF;
3748 if (nd->nd_flag & ND_NFSV4)
3749 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
3752 if (!error && nd->nd_repstat)
3753 error = nd->nd_repstat;
3754 mbuf_freem(nd->nd_mrep);
3759 * NFS byte range lock rpc.
3760 * (Mostly just calls one of the three lower level RPC routines.)
3763 nfsrpc_advlock(vnode_t vp, off_t size, int op, struct flock *fl,
3764 int reclaim, struct ucred *cred, NFSPROC_T *p, void *id, int flags)
3766 struct nfscllockowner *lp;
3767 struct nfsclclient *clp;
3769 struct nfsrv_descript nfsd, *nd = &nfsd;
3770 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
3773 u_int32_t clidrev = 0;
3774 int error = 0, newone = 0, expireret = 0, retrycnt, donelocally;
3778 * Convert the flock structure into a start and end and do POSIX
3781 switch (fl->l_whence) {
3785 * Caller is responsible for adding any necessary offset
3786 * when SEEK_CUR is used.
3788 start = fl->l_start;
3792 start = size + fl->l_start;
3793 off = size + fl->l_start;
3800 if (fl->l_len != 0) {
3801 end = start + fl->l_len - 1;
3812 if (op == F_GETLK) {
3813 error = nfscl_getcl(vnode_mount(vp), cred, p, 1, &clp);
3816 error = nfscl_lockt(vp, clp, off, len, fl, p, id, flags);
3818 clidrev = clp->nfsc_clientidrev;
3819 error = nfsrpc_lockt(nd, vp, clp, off, len, fl, cred,
3821 } else if (error == -1) {
3824 nfscl_clientrelease(clp);
3825 } else if (op == F_UNLCK && fl->l_type == F_UNLCK) {
3827 * We must loop around for all lockowner cases.
3830 error = nfscl_getcl(vnode_mount(vp), cred, p, 1, &clp);
3834 error = nfscl_relbytelock(vp, off, len, cred, p, callcnt,
3835 clp, id, flags, &lp, &dorpc);
3837 * If it returns a NULL lp, we're done.
3841 nfscl_clientrelease(clp);
3843 nfscl_releasealllocks(clp, vp, p, id, flags);
3846 if (nmp->nm_clp != NULL)
3847 clidrev = nmp->nm_clp->nfsc_clientidrev;
3851 * If the server doesn't support Posix lock semantics,
3852 * only allow locks on the entire file, since it won't
3853 * handle overlapping byte ranges.
3854 * There might still be a problem when a lock
3855 * upgrade/downgrade (read<->write) occurs, since the
3856 * server "might" expect an unlock first?
3858 if (dorpc && (lp->nfsl_open->nfso_posixlock ||
3859 (off == 0 && len == NFS64BITSSET))) {
3861 * Since the lock records will go away, we must
3862 * wait for grace and delay here.
3865 error = nfsrpc_locku(nd, nmp, lp, off, len,
3866 NFSV4LOCKT_READ, cred, p, 0);
3867 if ((nd->nd_repstat == NFSERR_GRACE ||
3868 nd->nd_repstat == NFSERR_DELAY) &&
3870 (void) nfs_catnap(PZERO, (int)nd->nd_repstat,
3872 } while ((nd->nd_repstat == NFSERR_GRACE ||
3873 nd->nd_repstat == NFSERR_DELAY) && error == 0);
3876 } while (error == 0 && nd->nd_repstat == 0);
3877 nfscl_releasealllocks(clp, vp, p, id, flags);
3878 } else if (op == F_SETLK) {
3879 error = nfscl_getbytelock(vp, off, len, fl->l_type, cred, p,
3880 NULL, 0, id, flags, NULL, NULL, &lp, &newone, &donelocally);
3881 if (error || donelocally) {
3884 if (nmp->nm_clp != NULL)
3885 clidrev = nmp->nm_clp->nfsc_clientidrev;
3888 nfhp = VTONFS(vp)->n_fhp;
3889 if (!lp->nfsl_open->nfso_posixlock &&
3890 (off != 0 || len != NFS64BITSSET)) {
3893 error = nfsrpc_lock(nd, nmp, vp, nfhp->nfh_fh,
3894 nfhp->nfh_len, lp, newone, reclaim, off,
3895 len, fl->l_type, cred, p, 0);
3898 error = nd->nd_repstat;
3899 nfscl_lockrelease(lp, error, newone);
3904 error = nd->nd_repstat;
3905 if (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
3906 error == NFSERR_STALEDONTRECOVER ||
3907 error == NFSERR_STALECLIENTID || error == NFSERR_DELAY ||
3908 error == NFSERR_BADSESSION) {
3909 (void) nfs_catnap(PZERO, error, "nfs_advlock");
3910 } else if ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID)
3912 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
3915 } while (error == NFSERR_GRACE ||
3916 error == NFSERR_STALECLIENTID || error == NFSERR_DELAY ||
3917 error == NFSERR_STALEDONTRECOVER || error == NFSERR_STALESTATEID ||
3918 error == NFSERR_BADSESSION ||
3919 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
3920 expireret == 0 && clidrev != 0 && retrycnt < 4));
3921 if (error && retrycnt >= 4)
3927 * The lower level routine for the LockT case.
3930 nfsrpc_lockt(struct nfsrv_descript *nd, vnode_t vp,
3931 struct nfsclclient *clp, u_int64_t off, u_int64_t len, struct flock *fl,
3932 struct ucred *cred, NFSPROC_T *p, void *id, int flags)
3935 int error, type, size;
3936 uint8_t own[NFSV4CL_LOCKNAMELEN + NFSX_V4FHMAX];
3938 struct nfsmount *nmp;
3939 struct nfsclsession *tsep;
3941 nmp = VFSTONFS(vp->v_mount);
3942 NFSCL_REQSTART(nd, NFSPROC_LOCKT, vp);
3943 NFSM_BUILD(tl, u_int32_t *, 7 * NFSX_UNSIGNED);
3944 if (fl->l_type == F_RDLCK)
3945 *tl++ = txdr_unsigned(NFSV4LOCKT_READ);
3947 *tl++ = txdr_unsigned(NFSV4LOCKT_WRITE);
3948 txdr_hyper(off, tl);
3950 txdr_hyper(len, tl);
3952 tsep = nfsmnt_mdssession(nmp);
3953 *tl++ = tsep->nfsess_clientid.lval[0];
3954 *tl = tsep->nfsess_clientid.lval[1];
3955 nfscl_filllockowner(id, own, flags);
3957 NFSBCOPY(np->n_fhp->nfh_fh, &own[NFSV4CL_LOCKNAMELEN],
3958 np->n_fhp->nfh_len);
3959 (void)nfsm_strtom(nd, own, NFSV4CL_LOCKNAMELEN + np->n_fhp->nfh_len);
3960 error = nfscl_request(nd, vp, p, cred, NULL);
3963 if (nd->nd_repstat == 0) {
3964 fl->l_type = F_UNLCK;
3965 } else if (nd->nd_repstat == NFSERR_DENIED) {
3967 fl->l_whence = SEEK_SET;
3968 NFSM_DISSECT(tl, u_int32_t *, 8 * NFSX_UNSIGNED);
3969 fl->l_start = fxdr_hyper(tl);
3971 len = fxdr_hyper(tl);
3973 if (len == NFS64BITSSET)
3977 type = fxdr_unsigned(int, *tl++);
3978 if (type == NFSV4LOCKT_WRITE)
3979 fl->l_type = F_WRLCK;
3981 fl->l_type = F_RDLCK;
3983 * XXX For now, I have no idea what to do with the
3984 * conflicting lock_owner, so I'll just set the pid == 0
3985 * and skip over the lock_owner.
3987 fl->l_pid = (pid_t)0;
3989 size = fxdr_unsigned(int, *tl);
3990 if (size < 0 || size > NFSV4_OPAQUELIMIT)
3993 error = nfsm_advance(nd, NFSM_RNDUP(size), -1);
3994 } else if (nd->nd_repstat == NFSERR_STALECLIENTID)
3995 nfscl_initiate_recovery(clp);
3997 mbuf_freem(nd->nd_mrep);
4002 * Lower level function that performs the LockU RPC.
4005 nfsrpc_locku(struct nfsrv_descript *nd, struct nfsmount *nmp,
4006 struct nfscllockowner *lp, u_int64_t off, u_int64_t len,
4007 u_int32_t type, struct ucred *cred, NFSPROC_T *p, int syscred)
4012 nfscl_reqstart(nd, NFSPROC_LOCKU, nmp, lp->nfsl_open->nfso_fh,
4013 lp->nfsl_open->nfso_fhlen, NULL, NULL, 0, 0);
4014 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID + 6 * NFSX_UNSIGNED);
4015 *tl++ = txdr_unsigned(type);
4016 *tl = txdr_unsigned(lp->nfsl_seqid);
4017 if (nfstest_outofseq &&
4018 (arc4random() % nfstest_outofseq) == 0)
4019 *tl = txdr_unsigned(lp->nfsl_seqid + 1);
4021 if (NFSHASNFSV4N(nmp))
4024 *tl++ = lp->nfsl_stateid.seqid;
4025 *tl++ = lp->nfsl_stateid.other[0];
4026 *tl++ = lp->nfsl_stateid.other[1];
4027 *tl++ = lp->nfsl_stateid.other[2];
4028 txdr_hyper(off, tl);
4030 txdr_hyper(len, tl);
4032 nd->nd_flag |= ND_USEGSSNAME;
4033 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4034 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4035 NFSCL_INCRSEQID(lp->nfsl_seqid, nd);
4038 if (nd->nd_repstat == 0) {
4039 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID);
4040 lp->nfsl_stateid.seqid = *tl++;
4041 lp->nfsl_stateid.other[0] = *tl++;
4042 lp->nfsl_stateid.other[1] = *tl++;
4043 lp->nfsl_stateid.other[2] = *tl;
4044 } else if (nd->nd_repstat == NFSERR_STALESTATEID)
4045 nfscl_initiate_recovery(lp->nfsl_open->nfso_own->nfsow_clp);
4047 mbuf_freem(nd->nd_mrep);
4052 * The actual Lock RPC.
4055 nfsrpc_lock(struct nfsrv_descript *nd, struct nfsmount *nmp, vnode_t vp,
4056 u_int8_t *nfhp, int fhlen, struct nfscllockowner *lp, int newone,
4057 int reclaim, u_int64_t off, u_int64_t len, short type, struct ucred *cred,
4058 NFSPROC_T *p, int syscred)
4062 uint8_t own[NFSV4CL_LOCKNAMELEN + NFSX_V4FHMAX];
4063 struct nfsclsession *tsep;
4065 nfscl_reqstart(nd, NFSPROC_LOCK, nmp, nfhp, fhlen, NULL, NULL, 0, 0);
4066 NFSM_BUILD(tl, u_int32_t *, 7 * NFSX_UNSIGNED);
4067 if (type == F_RDLCK)
4068 *tl++ = txdr_unsigned(NFSV4LOCKT_READ);
4070 *tl++ = txdr_unsigned(NFSV4LOCKT_WRITE);
4071 *tl++ = txdr_unsigned(reclaim);
4072 txdr_hyper(off, tl);
4074 txdr_hyper(len, tl);
4078 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID +
4079 2 * NFSX_UNSIGNED + NFSX_HYPER);
4080 *tl++ = txdr_unsigned(lp->nfsl_open->nfso_own->nfsow_seqid);
4081 if (NFSHASNFSV4N(nmp))
4084 *tl++ = lp->nfsl_open->nfso_stateid.seqid;
4085 *tl++ = lp->nfsl_open->nfso_stateid.other[0];
4086 *tl++ = lp->nfsl_open->nfso_stateid.other[1];
4087 *tl++ = lp->nfsl_open->nfso_stateid.other[2];
4088 *tl++ = txdr_unsigned(lp->nfsl_seqid);
4089 tsep = nfsmnt_mdssession(nmp);
4090 *tl++ = tsep->nfsess_clientid.lval[0];
4091 *tl = tsep->nfsess_clientid.lval[1];
4092 NFSBCOPY(lp->nfsl_owner, own, NFSV4CL_LOCKNAMELEN);
4093 NFSBCOPY(nfhp, &own[NFSV4CL_LOCKNAMELEN], fhlen);
4094 (void)nfsm_strtom(nd, own, NFSV4CL_LOCKNAMELEN + fhlen);
4097 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID + NFSX_UNSIGNED);
4098 if (NFSHASNFSV4N(nmp))
4101 *tl++ = lp->nfsl_stateid.seqid;
4102 *tl++ = lp->nfsl_stateid.other[0];
4103 *tl++ = lp->nfsl_stateid.other[1];
4104 *tl++ = lp->nfsl_stateid.other[2];
4105 *tl = txdr_unsigned(lp->nfsl_seqid);
4106 if (nfstest_outofseq &&
4107 (arc4random() % nfstest_outofseq) == 0)
4108 *tl = txdr_unsigned(lp->nfsl_seqid + 1);
4111 nd->nd_flag |= ND_USEGSSNAME;
4112 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, vp, p, cred,
4113 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4117 NFSCL_INCRSEQID(lp->nfsl_open->nfso_own->nfsow_seqid, nd);
4118 NFSCL_INCRSEQID(lp->nfsl_seqid, nd);
4119 if (nd->nd_repstat == 0) {
4120 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID);
4121 lp->nfsl_stateid.seqid = *tl++;
4122 lp->nfsl_stateid.other[0] = *tl++;
4123 lp->nfsl_stateid.other[1] = *tl++;
4124 lp->nfsl_stateid.other[2] = *tl;
4125 } else if (nd->nd_repstat == NFSERR_DENIED) {
4126 NFSM_DISSECT(tl, u_int32_t *, 8 * NFSX_UNSIGNED);
4127 size = fxdr_unsigned(int, *(tl + 7));
4128 if (size < 0 || size > NFSV4_OPAQUELIMIT)
4131 error = nfsm_advance(nd, NFSM_RNDUP(size), -1);
4132 } else if (nd->nd_repstat == NFSERR_STALESTATEID)
4133 nfscl_initiate_recovery(lp->nfsl_open->nfso_own->nfsow_clp);
4135 mbuf_freem(nd->nd_mrep);
4141 * (always called with the vp for the mount point)
4144 nfsrpc_statfs(vnode_t vp, struct nfsstatfs *sbp, struct nfsfsinfo *fsp,
4145 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp,
4148 u_int32_t *tl = NULL;
4149 struct nfsrv_descript nfsd, *nd = &nfsd;
4150 struct nfsmount *nmp;
4151 nfsattrbit_t attrbits;
4155 nmp = VFSTONFS(vnode_mount(vp));
4156 if (NFSHASNFSV4(nmp)) {
4158 * For V4, you actually do a getattr.
4160 NFSCL_REQSTART(nd, NFSPROC_GETATTR, vp);
4161 NFSSTATFS_GETATTRBIT(&attrbits);
4162 (void) nfsrv_putattrbit(nd, &attrbits);
4163 nd->nd_flag |= ND_USEGSSNAME;
4164 error = nfscl_request(nd, vp, p, cred, stuff);
4167 if (nd->nd_repstat == 0) {
4168 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0,
4169 NULL, NULL, sbp, fsp, NULL, 0, NULL, NULL, NULL, p,
4172 nmp->nm_fsid[0] = nap->na_filesid[0];
4173 nmp->nm_fsid[1] = nap->na_filesid[1];
4174 NFSSETHASSETFSID(nmp);
4178 error = nd->nd_repstat;
4183 NFSCL_REQSTART(nd, NFSPROC_FSSTAT, vp);
4184 error = nfscl_request(nd, vp, p, cred, stuff);
4187 if (nd->nd_flag & ND_NFSV3) {
4188 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
4192 if (nd->nd_repstat) {
4193 error = nd->nd_repstat;
4196 NFSM_DISSECT(tl, u_int32_t *,
4197 NFSX_STATFS(nd->nd_flag & ND_NFSV3));
4199 if (NFSHASNFSV3(nmp)) {
4200 sbp->sf_tbytes = fxdr_hyper(tl); tl += 2;
4201 sbp->sf_fbytes = fxdr_hyper(tl); tl += 2;
4202 sbp->sf_abytes = fxdr_hyper(tl); tl += 2;
4203 sbp->sf_tfiles = fxdr_hyper(tl); tl += 2;
4204 sbp->sf_ffiles = fxdr_hyper(tl); tl += 2;
4205 sbp->sf_afiles = fxdr_hyper(tl); tl += 2;
4206 sbp->sf_invarsec = fxdr_unsigned(u_int32_t, *tl);
4207 } else if (NFSHASNFSV4(nmp) == 0) {
4208 sbp->sf_tsize = fxdr_unsigned(u_int32_t, *tl++);
4209 sbp->sf_bsize = fxdr_unsigned(u_int32_t, *tl++);
4210 sbp->sf_blocks = fxdr_unsigned(u_int32_t, *tl++);
4211 sbp->sf_bfree = fxdr_unsigned(u_int32_t, *tl++);
4212 sbp->sf_bavail = fxdr_unsigned(u_int32_t, *tl);
4215 mbuf_freem(nd->nd_mrep);
4223 nfsrpc_pathconf(vnode_t vp, struct nfsv3_pathconf *pc,
4224 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp,
4227 struct nfsrv_descript nfsd, *nd = &nfsd;
4228 struct nfsmount *nmp;
4230 nfsattrbit_t attrbits;
4234 nmp = VFSTONFS(vnode_mount(vp));
4235 if (NFSHASNFSV4(nmp)) {
4237 * For V4, you actually do a getattr.
4239 NFSCL_REQSTART(nd, NFSPROC_GETATTR, vp);
4240 NFSPATHCONF_GETATTRBIT(&attrbits);
4241 (void) nfsrv_putattrbit(nd, &attrbits);
4242 nd->nd_flag |= ND_USEGSSNAME;
4243 error = nfscl_request(nd, vp, p, cred, stuff);
4246 if (nd->nd_repstat == 0) {
4247 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0,
4248 pc, NULL, NULL, NULL, NULL, 0, NULL, NULL, NULL, p,
4253 error = nd->nd_repstat;
4256 NFSCL_REQSTART(nd, NFSPROC_PATHCONF, vp);
4257 error = nfscl_request(nd, vp, p, cred, stuff);
4260 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
4261 if (nd->nd_repstat && !error)
4262 error = nd->nd_repstat;
4264 NFSM_DISSECT(tl, u_int32_t *, NFSX_V3PATHCONF);
4265 pc->pc_linkmax = fxdr_unsigned(u_int32_t, *tl++);
4266 pc->pc_namemax = fxdr_unsigned(u_int32_t, *tl++);
4267 pc->pc_notrunc = fxdr_unsigned(u_int32_t, *tl++);
4268 pc->pc_chownrestricted =
4269 fxdr_unsigned(u_int32_t, *tl++);
4270 pc->pc_caseinsensitive =
4271 fxdr_unsigned(u_int32_t, *tl++);
4272 pc->pc_casepreserving = fxdr_unsigned(u_int32_t, *tl);
4276 mbuf_freem(nd->nd_mrep);
4281 * nfs version 3 fsinfo rpc call
4284 nfsrpc_fsinfo(vnode_t vp, struct nfsfsinfo *fsp, struct ucred *cred,
4285 NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, void *stuff)
4288 struct nfsrv_descript nfsd, *nd = &nfsd;
4292 NFSCL_REQSTART(nd, NFSPROC_FSINFO, vp);
4293 error = nfscl_request(nd, vp, p, cred, stuff);
4296 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
4297 if (nd->nd_repstat && !error)
4298 error = nd->nd_repstat;
4300 NFSM_DISSECT(tl, u_int32_t *, NFSX_V3FSINFO);
4301 fsp->fs_rtmax = fxdr_unsigned(u_int32_t, *tl++);
4302 fsp->fs_rtpref = fxdr_unsigned(u_int32_t, *tl++);
4303 fsp->fs_rtmult = fxdr_unsigned(u_int32_t, *tl++);
4304 fsp->fs_wtmax = fxdr_unsigned(u_int32_t, *tl++);
4305 fsp->fs_wtpref = fxdr_unsigned(u_int32_t, *tl++);
4306 fsp->fs_wtmult = fxdr_unsigned(u_int32_t, *tl++);
4307 fsp->fs_dtpref = fxdr_unsigned(u_int32_t, *tl++);
4308 fsp->fs_maxfilesize = fxdr_hyper(tl);
4310 fxdr_nfsv3time(tl, &fsp->fs_timedelta);
4312 fsp->fs_properties = fxdr_unsigned(u_int32_t, *tl);
4315 mbuf_freem(nd->nd_mrep);
4320 * This function performs the Renew RPC.
4323 nfsrpc_renew(struct nfsclclient *clp, struct nfsclds *dsp, struct ucred *cred,
4327 struct nfsrv_descript nfsd;
4328 struct nfsrv_descript *nd = &nfsd;
4329 struct nfsmount *nmp;
4331 struct nfssockreq *nrp;
4332 struct nfsclsession *tsep;
4334 nmp = clp->nfsc_nmp;
4338 nfscl_reqstart(nd, NFSPROC_RENEW, nmp, NULL, 0, NULL, NULL, 0,
4341 nfscl_reqstart(nd, NFSPROC_RENEW, nmp, NULL, 0, NULL,
4342 &dsp->nfsclds_sess, 0, 0);
4343 if (!NFSHASNFSV4N(nmp)) {
4344 /* NFSv4.1 just uses a Sequence Op and not a Renew. */
4345 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
4346 tsep = nfsmnt_mdssession(nmp);
4347 *tl++ = tsep->nfsess_clientid.lval[0];
4348 *tl = tsep->nfsess_clientid.lval[1];
4352 nrp = dsp->nfsclds_sockp;
4354 /* If NULL, use the MDS socket. */
4355 nrp = &nmp->nm_sockreq;
4356 nd->nd_flag |= ND_USEGSSNAME;
4358 error = newnfs_request(nd, nmp, NULL, nrp, NULL, p, cred,
4359 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4361 error = newnfs_request(nd, nmp, NULL, nrp, NULL, p, cred,
4362 NFS_PROG, NFS_VER4, NULL, 1, NULL, &dsp->nfsclds_sess);
4365 error = nd->nd_repstat;
4366 mbuf_freem(nd->nd_mrep);
4371 * This function performs the Releaselockowner RPC.
4374 nfsrpc_rellockown(struct nfsmount *nmp, struct nfscllockowner *lp,
4375 uint8_t *fh, int fhlen, struct ucred *cred, NFSPROC_T *p)
4377 struct nfsrv_descript nfsd, *nd = &nfsd;
4380 uint8_t own[NFSV4CL_LOCKNAMELEN + NFSX_V4FHMAX];
4381 struct nfsclsession *tsep;
4383 if (NFSHASNFSV4N(nmp)) {
4384 /* For NFSv4.1, do a FreeStateID. */
4385 nfscl_reqstart(nd, NFSPROC_FREESTATEID, nmp, NULL, 0, NULL,
4387 nfsm_stateidtom(nd, &lp->nfsl_stateid, NFSSTATEID_PUTSTATEID);
4389 nfscl_reqstart(nd, NFSPROC_RELEASELCKOWN, nmp, NULL, 0, NULL,
4391 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
4392 tsep = nfsmnt_mdssession(nmp);
4393 *tl++ = tsep->nfsess_clientid.lval[0];
4394 *tl = tsep->nfsess_clientid.lval[1];
4395 NFSBCOPY(lp->nfsl_owner, own, NFSV4CL_LOCKNAMELEN);
4396 NFSBCOPY(fh, &own[NFSV4CL_LOCKNAMELEN], fhlen);
4397 (void)nfsm_strtom(nd, own, NFSV4CL_LOCKNAMELEN + fhlen);
4399 nd->nd_flag |= ND_USEGSSNAME;
4400 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4401 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4404 error = nd->nd_repstat;
4405 mbuf_freem(nd->nd_mrep);
4410 * This function performs the Compound to get the mount pt FH.
4413 nfsrpc_getdirpath(struct nfsmount *nmp, u_char *dirpath, struct ucred *cred,
4417 struct nfsrv_descript nfsd;
4418 struct nfsrv_descript *nd = &nfsd;
4420 int error, cnt, len, setnil;
4423 nfscl_reqstart(nd, NFSPROC_PUTROOTFH, nmp, NULL, 0, &opcntp, NULL, 0,
4432 while (*cp2 != '\0' && *cp2 != '/')
4439 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
4440 *tl = txdr_unsigned(NFSV4OP_LOOKUP);
4441 nfsm_strtom(nd, cp, strlen(cp));
4447 } while (*cp != '\0');
4448 if (NFSHASNFSV4N(nmp))
4449 /* Has a Sequence Op done by nfscl_reqstart(). */
4450 *opcntp = txdr_unsigned(3 + cnt);
4452 *opcntp = txdr_unsigned(2 + cnt);
4453 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
4454 *tl = txdr_unsigned(NFSV4OP_GETFH);
4455 nd->nd_flag |= ND_USEGSSNAME;
4456 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4457 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4460 if (nd->nd_repstat == 0) {
4461 NFSM_DISSECT(tl, u_int32_t *, (3 + 2 * cnt) * NFSX_UNSIGNED);
4462 tl += (2 + 2 * cnt);
4463 if ((len = fxdr_unsigned(int, *tl)) <= 0 ||
4465 nd->nd_repstat = NFSERR_BADXDR;
4467 nd->nd_repstat = nfsrv_mtostr(nd, nmp->nm_fh, len);
4468 if (nd->nd_repstat == 0)
4469 nmp->nm_fhsize = len;
4472 error = nd->nd_repstat;
4474 mbuf_freem(nd->nd_mrep);
4479 * This function performs the Delegreturn RPC.
4482 nfsrpc_delegreturn(struct nfscldeleg *dp, struct ucred *cred,
4483 struct nfsmount *nmp, NFSPROC_T *p, int syscred)
4486 struct nfsrv_descript nfsd;
4487 struct nfsrv_descript *nd = &nfsd;
4490 nfscl_reqstart(nd, NFSPROC_DELEGRETURN, nmp, dp->nfsdl_fh,
4491 dp->nfsdl_fhlen, NULL, NULL, 0, 0);
4492 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID);
4493 if (NFSHASNFSV4N(nmp))
4496 *tl++ = dp->nfsdl_stateid.seqid;
4497 *tl++ = dp->nfsdl_stateid.other[0];
4498 *tl++ = dp->nfsdl_stateid.other[1];
4499 *tl = dp->nfsdl_stateid.other[2];
4501 nd->nd_flag |= ND_USEGSSNAME;
4502 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4503 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4506 error = nd->nd_repstat;
4507 mbuf_freem(nd->nd_mrep);
4515 nfsrpc_getacl(vnode_t vp, struct ucred *cred, NFSPROC_T *p,
4516 struct acl *aclp, void *stuff)
4518 struct nfsrv_descript nfsd, *nd = &nfsd;
4520 nfsattrbit_t attrbits;
4521 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
4523 if (nfsrv_useacl == 0 || !NFSHASNFSV4(nmp))
4524 return (EOPNOTSUPP);
4525 NFSCL_REQSTART(nd, NFSPROC_GETACL, vp);
4526 NFSZERO_ATTRBIT(&attrbits);
4527 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_ACL);
4528 (void) nfsrv_putattrbit(nd, &attrbits);
4529 error = nfscl_request(nd, vp, p, cred, stuff);
4532 if (!nd->nd_repstat)
4533 error = nfsv4_loadattr(nd, vp, NULL, NULL, NULL, 0, NULL,
4534 NULL, NULL, NULL, aclp, 0, NULL, NULL, NULL, p, cred);
4536 error = nd->nd_repstat;
4537 mbuf_freem(nd->nd_mrep);
4545 nfsrpc_setacl(vnode_t vp, struct ucred *cred, NFSPROC_T *p,
4546 struct acl *aclp, void *stuff)
4549 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
4551 if (nfsrv_useacl == 0 || !NFSHASNFSV4(nmp))
4552 return (EOPNOTSUPP);
4553 error = nfsrpc_setattr(vp, NULL, aclp, cred, p, NULL, NULL, stuff);
4561 nfsrpc_setaclrpc(vnode_t vp, struct ucred *cred, NFSPROC_T *p,
4562 struct acl *aclp, nfsv4stateid_t *stateidp, void *stuff)
4564 struct nfsrv_descript nfsd, *nd = &nfsd;
4566 nfsattrbit_t attrbits;
4567 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
4569 if (!NFSHASNFSV4(nmp))
4570 return (EOPNOTSUPP);
4571 NFSCL_REQSTART(nd, NFSPROC_SETACL, vp);
4572 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID);
4573 NFSZERO_ATTRBIT(&attrbits);
4574 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_ACL);
4575 (void) nfsv4_fillattr(nd, vnode_mount(vp), vp, aclp, NULL, NULL, 0,
4576 &attrbits, NULL, NULL, 0, 0, 0, 0, (uint64_t)0);
4577 error = nfscl_request(nd, vp, p, cred, stuff);
4580 /* Don't care about the pre/postop attributes */
4581 mbuf_freem(nd->nd_mrep);
4582 return (nd->nd_repstat);
4586 * Do the NFSv4.1 Exchange ID.
4589 nfsrpc_exchangeid(struct nfsmount *nmp, struct nfsclclient *clp,
4590 struct nfssockreq *nrp, uint32_t exchflags, struct nfsclds **dspp,
4591 struct ucred *cred, NFSPROC_T *p)
4593 uint32_t *tl, v41flags;
4594 struct nfsrv_descript nfsd;
4595 struct nfsrv_descript *nd = &nfsd;
4596 struct nfsclds *dsp;
4597 struct timespec verstime;
4601 nfscl_reqstart(nd, NFSPROC_EXCHANGEID, nmp, NULL, 0, NULL, NULL, 0, 0);
4602 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_UNSIGNED);
4603 *tl++ = txdr_unsigned(nfsboottime.tv_sec); /* Client owner */
4604 *tl = txdr_unsigned(clp->nfsc_rev);
4605 (void) nfsm_strtom(nd, clp->nfsc_id, clp->nfsc_idlen);
4607 NFSM_BUILD(tl, uint32_t *, 3 * NFSX_UNSIGNED);
4608 *tl++ = txdr_unsigned(exchflags);
4609 *tl++ = txdr_unsigned(NFSV4EXCH_SP4NONE);
4611 /* Set the implementation id4 */
4612 *tl = txdr_unsigned(1);
4613 (void) nfsm_strtom(nd, "freebsd.org", strlen("freebsd.org"));
4614 (void) nfsm_strtom(nd, version, strlen(version));
4615 NFSM_BUILD(tl, uint32_t *, NFSX_V4TIME);
4616 verstime.tv_sec = 1293840000; /* Jan 1, 2011 */
4617 verstime.tv_nsec = 0;
4618 txdr_nfsv4time(&verstime, tl);
4619 nd->nd_flag |= ND_USEGSSNAME;
4620 error = newnfs_request(nd, nmp, NULL, nrp, NULL, p, cred,
4621 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4622 NFSCL_DEBUG(1, "exchangeid err=%d reps=%d\n", error,
4623 (int)nd->nd_repstat);
4626 if (nd->nd_repstat == 0) {
4627 NFSM_DISSECT(tl, uint32_t *, 6 * NFSX_UNSIGNED + NFSX_HYPER);
4628 len = fxdr_unsigned(int, *(tl + 7));
4629 if (len < 0 || len > NFSV4_OPAQUELIMIT) {
4630 error = NFSERR_BADXDR;
4633 dsp = malloc(sizeof(struct nfsclds) + len + 1, M_NFSCLDS,
4635 dsp->nfsclds_expire = NFSD_MONOSEC + clp->nfsc_renew;
4636 dsp->nfsclds_servownlen = len;
4637 dsp->nfsclds_sess.nfsess_clientid.lval[0] = *tl++;
4638 dsp->nfsclds_sess.nfsess_clientid.lval[1] = *tl++;
4639 dsp->nfsclds_sess.nfsess_sequenceid =
4640 fxdr_unsigned(uint32_t, *tl++);
4641 v41flags = fxdr_unsigned(uint32_t, *tl);
4642 if ((v41flags & NFSV4EXCH_USEPNFSMDS) != 0 &&
4643 NFSHASPNFSOPT(nmp)) {
4644 NFSCL_DEBUG(1, "set PNFS\n");
4646 nmp->nm_state |= NFSSTA_PNFS;
4648 dsp->nfsclds_flags |= NFSCLDS_MDS;
4650 if ((v41flags & NFSV4EXCH_USEPNFSDS) != 0)
4651 dsp->nfsclds_flags |= NFSCLDS_DS;
4653 nd->nd_repstat = nfsrv_mtostr(nd,
4654 dsp->nfsclds_serverown, len);
4655 if (nd->nd_repstat == 0) {
4656 mtx_init(&dsp->nfsclds_mtx, "nfsds", NULL, MTX_DEF);
4657 mtx_init(&dsp->nfsclds_sess.nfsess_mtx, "nfssession",
4659 nfscl_initsessionslots(&dsp->nfsclds_sess);
4662 free(dsp, M_NFSCLDS);
4664 error = nd->nd_repstat;
4666 mbuf_freem(nd->nd_mrep);
4671 * Do the NFSv4.1 Create Session.
4674 nfsrpc_createsession(struct nfsmount *nmp, struct nfsclsession *sep,
4675 struct nfssockreq *nrp, uint32_t sequenceid, int mds, struct ucred *cred,
4678 uint32_t crflags, maxval, *tl;
4679 struct nfsrv_descript nfsd;
4680 struct nfsrv_descript *nd = &nfsd;
4683 /* Make sure nm_rsize, nm_wsize is set. */
4684 if (nmp->nm_rsize > NFS_MAXBSIZE || nmp->nm_rsize == 0)
4685 nmp->nm_rsize = NFS_MAXBSIZE;
4686 if (nmp->nm_wsize > NFS_MAXBSIZE || nmp->nm_wsize == 0)
4687 nmp->nm_wsize = NFS_MAXBSIZE;
4688 nfscl_reqstart(nd, NFSPROC_CREATESESSION, nmp, NULL, 0, NULL, NULL, 0,
4690 NFSM_BUILD(tl, uint32_t *, 4 * NFSX_UNSIGNED);
4691 *tl++ = sep->nfsess_clientid.lval[0];
4692 *tl++ = sep->nfsess_clientid.lval[1];
4693 *tl++ = txdr_unsigned(sequenceid);
4694 crflags = (NFSMNT_RDONLY(nmp->nm_mountp) ? 0 : NFSV4CRSESS_PERSIST);
4695 if (nfscl_enablecallb != 0 && nfs_numnfscbd > 0 && mds != 0)
4696 crflags |= NFSV4CRSESS_CONNBACKCHAN;
4697 *tl = txdr_unsigned(crflags);
4699 /* Fill in fore channel attributes. */
4700 NFSM_BUILD(tl, uint32_t *, 7 * NFSX_UNSIGNED);
4701 *tl++ = 0; /* Header pad size */
4702 *tl++ = txdr_unsigned(nmp->nm_wsize + NFS_MAXXDR);/* Max request size */
4703 *tl++ = txdr_unsigned(nmp->nm_rsize + NFS_MAXXDR);/* Max reply size */
4704 *tl++ = txdr_unsigned(4096); /* Max response size cached */
4705 *tl++ = txdr_unsigned(20); /* Max operations */
4706 *tl++ = txdr_unsigned(64); /* Max slots */
4707 *tl = 0; /* No rdma ird */
4709 /* Fill in back channel attributes. */
4710 NFSM_BUILD(tl, uint32_t *, 7 * NFSX_UNSIGNED);
4711 *tl++ = 0; /* Header pad size */
4712 *tl++ = txdr_unsigned(10000); /* Max request size */
4713 *tl++ = txdr_unsigned(10000); /* Max response size */
4714 *tl++ = txdr_unsigned(4096); /* Max response size cached */
4715 *tl++ = txdr_unsigned(4); /* Max operations */
4716 *tl++ = txdr_unsigned(NFSV4_CBSLOTS); /* Max slots */
4717 *tl = 0; /* No rdma ird */
4719 NFSM_BUILD(tl, uint32_t *, 8 * NFSX_UNSIGNED);
4720 *tl++ = txdr_unsigned(NFS_CALLBCKPROG); /* Call back prog # */
4722 /* Allow AUTH_SYS callbacks as uid, gid == 0. */
4723 *tl++ = txdr_unsigned(1); /* Auth_sys only */
4724 *tl++ = txdr_unsigned(AUTH_SYS); /* AUTH_SYS type */
4725 *tl++ = txdr_unsigned(nfsboottime.tv_sec); /* time stamp */
4726 *tl++ = 0; /* Null machine name */
4727 *tl++ = 0; /* Uid == 0 */
4728 *tl++ = 0; /* Gid == 0 */
4729 *tl = 0; /* No additional gids */
4730 nd->nd_flag |= ND_USEGSSNAME;
4731 error = newnfs_request(nd, nmp, NULL, nrp, NULL, p, cred, NFS_PROG,
4732 NFS_VER4, NULL, 1, NULL, NULL);
4735 if (nd->nd_repstat == 0) {
4736 NFSM_DISSECT(tl, uint32_t *, NFSX_V4SESSIONID +
4738 bcopy(tl, sep->nfsess_sessionid, NFSX_V4SESSIONID);
4739 tl += NFSX_V4SESSIONID / NFSX_UNSIGNED;
4740 sep->nfsess_sequenceid = fxdr_unsigned(uint32_t, *tl++);
4741 crflags = fxdr_unsigned(uint32_t, *tl);
4742 if ((crflags & NFSV4CRSESS_PERSIST) != 0 && mds != 0) {
4744 nmp->nm_state |= NFSSTA_SESSPERSIST;
4748 /* Get the fore channel slot count. */
4749 NFSM_DISSECT(tl, uint32_t *, 7 * NFSX_UNSIGNED);
4750 tl++; /* Skip the header pad size. */
4752 /* Make sure nm_wsize is small enough. */
4753 maxval = fxdr_unsigned(uint32_t, *tl++);
4754 while (maxval < nmp->nm_wsize + NFS_MAXXDR) {
4755 if (nmp->nm_wsize > 8096)
4761 /* Make sure nm_rsize is small enough. */
4762 maxval = fxdr_unsigned(uint32_t, *tl++);
4763 while (maxval < nmp->nm_rsize + NFS_MAXXDR) {
4764 if (nmp->nm_rsize > 8096)
4770 sep->nfsess_maxcache = fxdr_unsigned(int, *tl++);
4772 sep->nfsess_foreslots = fxdr_unsigned(uint16_t, *tl++);
4773 NFSCL_DEBUG(4, "fore slots=%d\n", (int)sep->nfsess_foreslots);
4774 irdcnt = fxdr_unsigned(int, *tl);
4776 NFSM_DISSECT(tl, uint32_t *, irdcnt * NFSX_UNSIGNED);
4778 /* and the back channel slot count. */
4779 NFSM_DISSECT(tl, uint32_t *, 7 * NFSX_UNSIGNED);
4781 sep->nfsess_backslots = fxdr_unsigned(uint16_t, *tl);
4782 NFSCL_DEBUG(4, "back slots=%d\n", (int)sep->nfsess_backslots);
4784 error = nd->nd_repstat;
4786 mbuf_freem(nd->nd_mrep);
4791 * Do the NFSv4.1 Destroy Session.
4794 nfsrpc_destroysession(struct nfsmount *nmp, struct nfsclclient *clp,
4795 struct ucred *cred, NFSPROC_T *p)
4798 struct nfsrv_descript nfsd;
4799 struct nfsrv_descript *nd = &nfsd;
4801 struct nfsclsession *tsep;
4803 nfscl_reqstart(nd, NFSPROC_DESTROYSESSION, nmp, NULL, 0, NULL, NULL, 0,
4805 NFSM_BUILD(tl, uint32_t *, NFSX_V4SESSIONID);
4806 tsep = nfsmnt_mdssession(nmp);
4807 bcopy(tsep->nfsess_sessionid, tl, NFSX_V4SESSIONID);
4808 nd->nd_flag |= ND_USEGSSNAME;
4809 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4810 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4813 error = nd->nd_repstat;
4814 mbuf_freem(nd->nd_mrep);
4819 * Do the NFSv4.1 Destroy Client.
4822 nfsrpc_destroyclient(struct nfsmount *nmp, struct nfsclclient *clp,
4823 struct ucred *cred, NFSPROC_T *p)
4826 struct nfsrv_descript nfsd;
4827 struct nfsrv_descript *nd = &nfsd;
4829 struct nfsclsession *tsep;
4831 nfscl_reqstart(nd, NFSPROC_DESTROYCLIENT, nmp, NULL, 0, NULL, NULL, 0,
4833 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_UNSIGNED);
4834 tsep = nfsmnt_mdssession(nmp);
4835 *tl++ = tsep->nfsess_clientid.lval[0];
4836 *tl = tsep->nfsess_clientid.lval[1];
4837 nd->nd_flag |= ND_USEGSSNAME;
4838 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4839 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4842 error = nd->nd_repstat;
4843 mbuf_freem(nd->nd_mrep);
4848 * Do the NFSv4.1 LayoutGet.
4851 nfsrpc_layoutget(struct nfsmount *nmp, uint8_t *fhp, int fhlen, int iomode,
4852 uint64_t offset, uint64_t len, uint64_t minlen, int layoutlen,
4853 nfsv4stateid_t *stateidp, int *retonclosep, struct nfsclflayouthead *flhp,
4854 struct ucred *cred, NFSPROC_T *p, void *stuff)
4856 struct nfsrv_descript nfsd, *nd = &nfsd;
4859 nfscl_reqstart(nd, NFSPROC_LAYOUTGET, nmp, fhp, fhlen, NULL, NULL, 0,
4861 nfsrv_setuplayoutget(nd, iomode, offset, len, minlen, stateidp,
4863 nd->nd_flag |= ND_USEGSSNAME;
4864 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4865 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4866 NFSCL_DEBUG(4, "layget err=%d st=%d\n", error, nd->nd_repstat);
4869 if (nd->nd_repstat == 0)
4870 error = nfsrv_parselayoutget(nd, stateidp, retonclosep, flhp);
4871 if (error == 0 && nd->nd_repstat != 0)
4872 error = nd->nd_repstat;
4873 mbuf_freem(nd->nd_mrep);
4878 * Do the NFSv4.1 Get Device Info.
4881 nfsrpc_getdeviceinfo(struct nfsmount *nmp, uint8_t *deviceid, int layouttype,
4882 uint32_t *notifybitsp, struct nfscldevinfo **ndip, struct ucred *cred,
4886 struct nfsrv_descript nfsd;
4887 struct nfsrv_descript *nd = &nfsd;
4888 struct sockaddr_in sin, ssin;
4889 struct sockaddr_in6 sin6, ssin6;
4890 struct nfsclds *dsp = NULL, **dspp, **gotdspp;
4891 struct nfscldevinfo *ndi;
4892 int addrcnt = 0, bitcnt, error, gotvers, i, isudp, j, stripecnt;
4893 uint8_t stripeindex;
4894 sa_family_t af, safilled;
4899 nfscl_reqstart(nd, NFSPROC_GETDEVICEINFO, nmp, NULL, 0, NULL, NULL, 0,
4901 NFSM_BUILD(tl, uint32_t *, NFSX_V4DEVICEID + 3 * NFSX_UNSIGNED);
4902 NFSBCOPY(deviceid, tl, NFSX_V4DEVICEID);
4903 tl += (NFSX_V4DEVICEID / NFSX_UNSIGNED);
4904 *tl++ = txdr_unsigned(layouttype);
4905 *tl++ = txdr_unsigned(100000);
4906 if (notifybitsp != NULL && *notifybitsp != 0) {
4907 *tl = txdr_unsigned(1); /* One word of bits. */
4908 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
4909 *tl = txdr_unsigned(*notifybitsp);
4911 *tl = txdr_unsigned(0);
4912 nd->nd_flag |= ND_USEGSSNAME;
4913 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4914 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4917 if (nd->nd_repstat == 0) {
4918 NFSM_DISSECT(tl, uint32_t *, 3 * NFSX_UNSIGNED);
4919 if (layouttype != fxdr_unsigned(int, *tl++))
4920 printf("EEK! devinfo layout type not same!\n");
4921 stripecnt = fxdr_unsigned(int, *++tl);
4922 NFSCL_DEBUG(4, "stripecnt=%d\n", stripecnt);
4923 if (stripecnt < 1 || stripecnt > 4096) {
4924 printf("NFS devinfo stripecnt %d: out of range\n",
4926 error = NFSERR_BADXDR;
4929 NFSM_DISSECT(tl, uint32_t *, (stripecnt + 1) * NFSX_UNSIGNED);
4930 addrcnt = fxdr_unsigned(int, *(tl + stripecnt));
4931 NFSCL_DEBUG(4, "addrcnt=%d\n", addrcnt);
4932 if (addrcnt < 1 || addrcnt > 128) {
4933 printf("NFS devinfo addrcnt %d: out of range\n",
4935 error = NFSERR_BADXDR;
4940 * Now we know how many stripe indices and addresses, so
4941 * we can allocate the structure the correct size.
4943 i = (stripecnt * sizeof(uint8_t)) / sizeof(struct nfsclds *)
4945 NFSCL_DEBUG(4, "stripeindices=%d\n", i);
4946 ndi = malloc(sizeof(*ndi) + (addrcnt + i) *
4947 sizeof(struct nfsclds *), M_NFSDEVINFO, M_WAITOK | M_ZERO);
4948 NFSBCOPY(deviceid, ndi->nfsdi_deviceid, NFSX_V4DEVICEID);
4949 ndi->nfsdi_refcnt = 0;
4950 ndi->nfsdi_stripecnt = stripecnt;
4951 ndi->nfsdi_addrcnt = addrcnt;
4952 /* Fill in the stripe indices. */
4953 for (i = 0; i < stripecnt; i++) {
4954 stripeindex = fxdr_unsigned(uint8_t, *tl++);
4955 NFSCL_DEBUG(4, "stripeind=%d\n", stripeindex);
4956 if (stripeindex >= addrcnt) {
4957 printf("NFS devinfo stripeindex %d: too big\n",
4959 error = NFSERR_BADXDR;
4962 nfsfldi_setstripeindex(ndi, i, stripeindex);
4965 /* Now, dissect the server address(es). */
4966 safilled = AF_UNSPEC;
4967 for (i = 0; i < addrcnt; i++) {
4968 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
4969 cnt = fxdr_unsigned(uint32_t, *tl);
4971 printf("NFS devinfo 0 len addrlist\n");
4972 error = NFSERR_BADXDR;
4975 dspp = nfsfldi_addr(ndi, i);
4976 safilled = AF_UNSPEC;
4977 for (j = 0; j < cnt; j++) {
4978 error = nfsv4_getipaddr(nd, &sin, &sin6, &af,
4980 if (error != 0 && error != EPERM) {
4981 error = NFSERR_BADXDR;
4984 if (error == 0 && isudp == 0) {
4987 * - Same address family.
4988 * Save the address and dspp, so that
4989 * the connection can be done after
4990 * parsing is complete.
4992 if (safilled == AF_UNSPEC ||
4993 (af == nmp->nm_nam->sa_family &&
4994 safilled != nmp->nm_nam->sa_family)
5007 gotvers = NFS_VER4; /* Always NFSv4 for File Layout. */
5009 /* And the notify bits. */
5010 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
5011 bitcnt = fxdr_unsigned(int, *tl);
5013 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
5014 if (notifybitsp != NULL)
5016 fxdr_unsigned(uint32_t, *tl);
5018 if (safilled != AF_UNSPEC) {
5019 KASSERT(ndi != NULL, ("ndi is NULL"));
5025 * Now we can do a TCP connection for the correct
5026 * NFS version and IP address.
5028 error = nfsrpc_fillsa(nmp, &ssin, &ssin6, safilled,
5032 KASSERT(gotdspp != NULL, ("gotdspp is NULL"));
5036 if (nd->nd_repstat != 0)
5037 error = nd->nd_repstat;
5039 if (error != 0 && ndi != NULL)
5040 nfscl_freedevinfo(ndi);
5041 mbuf_freem(nd->nd_mrep);
5046 * Do the NFSv4.1 LayoutCommit.
5049 nfsrpc_layoutcommit(struct nfsmount *nmp, uint8_t *fh, int fhlen, int reclaim,
5050 uint64_t off, uint64_t len, uint64_t lastbyte, nfsv4stateid_t *stateidp,
5051 int layouttype, struct ucred *cred, NFSPROC_T *p, void *stuff)
5054 struct nfsrv_descript nfsd, *nd = &nfsd;
5057 nfscl_reqstart(nd, NFSPROC_LAYOUTCOMMIT, nmp, fh, fhlen, NULL, NULL,
5059 NFSM_BUILD(tl, uint32_t *, 5 * NFSX_UNSIGNED + 3 * NFSX_HYPER +
5061 txdr_hyper(off, tl);
5063 txdr_hyper(len, tl);
5066 *tl++ = newnfs_true;
5068 *tl++ = newnfs_false;
5069 *tl++ = txdr_unsigned(stateidp->seqid);
5070 *tl++ = stateidp->other[0];
5071 *tl++ = stateidp->other[1];
5072 *tl++ = stateidp->other[2];
5073 *tl++ = newnfs_true;
5076 else if (lastbyte >= (off + len))
5077 lastbyte = off + len - 1;
5078 txdr_hyper(lastbyte, tl);
5080 *tl++ = newnfs_false;
5081 *tl++ = txdr_unsigned(layouttype);
5082 /* All supported layouts are 0 length. */
5083 *tl = txdr_unsigned(0);
5084 nd->nd_flag |= ND_USEGSSNAME;
5085 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
5086 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
5089 error = nd->nd_repstat;
5090 mbuf_freem(nd->nd_mrep);
5095 * Do the NFSv4.1 LayoutReturn.
5098 nfsrpc_layoutreturn(struct nfsmount *nmp, uint8_t *fh, int fhlen, int reclaim,
5099 int layouttype, uint32_t iomode, int layoutreturn, uint64_t offset,
5100 uint64_t len, nfsv4stateid_t *stateidp, struct ucred *cred, NFSPROC_T *p,
5104 struct nfsrv_descript nfsd, *nd = &nfsd;
5107 nfscl_reqstart(nd, NFSPROC_LAYOUTRETURN, nmp, fh, fhlen, NULL, NULL,
5109 NFSM_BUILD(tl, uint32_t *, 4 * NFSX_UNSIGNED);
5111 *tl++ = newnfs_true;
5113 *tl++ = newnfs_false;
5114 *tl++ = txdr_unsigned(layouttype);
5115 *tl++ = txdr_unsigned(iomode);
5116 *tl = txdr_unsigned(layoutreturn);
5117 if (layoutreturn == NFSLAYOUTRETURN_FILE) {
5118 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_HYPER + NFSX_STATEID +
5120 txdr_hyper(offset, tl);
5122 txdr_hyper(len, tl);
5124 NFSCL_DEBUG(4, "layoutret stseq=%d\n", (int)stateidp->seqid);
5125 *tl++ = txdr_unsigned(stateidp->seqid);
5126 *tl++ = stateidp->other[0];
5127 *tl++ = stateidp->other[1];
5128 *tl++ = stateidp->other[2];
5129 *tl = txdr_unsigned(0);
5131 nd->nd_flag |= ND_USEGSSNAME;
5132 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
5133 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
5136 if (nd->nd_repstat == 0) {
5137 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
5139 NFSM_DISSECT(tl, uint32_t *, NFSX_STATEID);
5140 stateidp->seqid = fxdr_unsigned(uint32_t, *tl++);
5141 stateidp->other[0] = *tl++;
5142 stateidp->other[1] = *tl++;
5143 stateidp->other[2] = *tl;
5146 error = nd->nd_repstat;
5148 mbuf_freem(nd->nd_mrep);
5153 * Acquire a layout and devinfo, if possible. The caller must have acquired
5154 * a reference count on the nfsclclient structure before calling this.
5155 * Return the layout in lypp with a reference count on it, if successful.
5158 nfsrpc_getlayout(struct nfsmount *nmp, vnode_t vp, struct nfsfh *nfhp,
5159 int iomode, uint32_t *notifybitsp, nfsv4stateid_t *stateidp, uint64_t off,
5160 struct nfscllayout **lypp, struct ucred *cred, NFSPROC_T *p)
5162 struct nfscllayout *lyp;
5163 struct nfsclflayout *flp;
5164 struct nfsclflayouthead flh;
5165 int error = 0, islocked, layoutlen, recalled, retonclose;
5166 nfsv4stateid_t stateid;
5167 struct nfsclsession *tsep;
5171 * If lyp is returned non-NULL, there will be a refcnt (shared lock)
5172 * on it, iff flp != NULL or a lock (exclusive lock) on it iff
5175 lyp = nfscl_getlayout(nmp->nm_clp, nfhp->nfh_fh, nfhp->nfh_len,
5176 off, &flp, &recalled);
5178 if (lyp == NULL || flp == NULL) {
5182 tsep = nfsmnt_mdssession(nmp);
5183 layoutlen = tsep->nfsess_maxcache -
5184 (NFSX_STATEID + 3 * NFSX_UNSIGNED);
5187 stateid.other[0] = stateidp->other[0];
5188 stateid.other[1] = stateidp->other[1];
5189 stateid.other[2] = stateidp->other[2];
5190 error = nfsrpc_layoutget(nmp, nfhp->nfh_fh,
5191 nfhp->nfh_len, iomode, (uint64_t)0, UINT64_MAX,
5192 (uint64_t)0, layoutlen, &stateid, &retonclose,
5193 &flh, cred, p, NULL);
5196 stateid.seqid = lyp->nfsly_stateid.seqid;
5197 stateid.other[0] = lyp->nfsly_stateid.other[0];
5198 stateid.other[1] = lyp->nfsly_stateid.other[1];
5199 stateid.other[2] = lyp->nfsly_stateid.other[2];
5200 error = nfsrpc_layoutget(nmp, nfhp->nfh_fh,
5201 nfhp->nfh_len, iomode, off, UINT64_MAX,
5202 (uint64_t)0, layoutlen, &stateid, &retonclose,
5203 &flh, cred, p, NULL);
5205 error = nfsrpc_layoutgetres(nmp, vp, nfhp->nfh_fh,
5206 nfhp->nfh_len, &stateid, retonclose, notifybitsp, &lyp,
5207 &flh, error, NULL, cred, p);
5210 else if (islocked != 0)
5211 nfscl_rellayout(lyp, 1);
5218 * Do a TCP connection plus exchange id and create session.
5219 * If successful, a "struct nfsclds" is linked into the list for the
5220 * mount point and a pointer to it is returned.
5223 nfsrpc_fillsa(struct nfsmount *nmp, struct sockaddr_in *sin,
5224 struct sockaddr_in6 *sin6, sa_family_t af, int vers, struct nfsclds **dspp,
5227 struct sockaddr_in *msad, *sad;
5228 struct sockaddr_in6 *msad6, *sad6;
5229 struct nfsclclient *clp;
5230 struct nfssockreq *nrp;
5231 struct nfsclds *dsp, *tdsp;
5233 enum nfsclds_state retv;
5234 uint32_t sequenceid;
5236 KASSERT(nmp->nm_sockreq.nr_cred != NULL,
5237 ("nfsrpc_fillsa: NULL nr_cred"));
5243 if (af == AF_INET) {
5246 * Check to see if we already have a session for this
5247 * address that is usable for a DS.
5248 * Note that the MDS's address is in a different place
5249 * than the sessions already acquired for DS's.
5251 msad = (struct sockaddr_in *)nmp->nm_sockreq.nr_nam;
5252 tdsp = TAILQ_FIRST(&nmp->nm_sess);
5253 while (tdsp != NULL) {
5254 if (msad != NULL && msad->sin_family == AF_INET &&
5255 sin->sin_addr.s_addr == msad->sin_addr.s_addr &&
5256 sin->sin_port == msad->sin_port &&
5257 (tdsp->nfsclds_flags & NFSCLDS_DS) != 0 &&
5258 tdsp->nfsclds_sess.nfsess_defunct == 0) {
5261 NFSCL_DEBUG(4, "fnd same addr\n");
5264 tdsp = TAILQ_NEXT(tdsp, nfsclds_list);
5265 if (tdsp != NULL && tdsp->nfsclds_sockp != NULL)
5266 msad = (struct sockaddr_in *)
5267 tdsp->nfsclds_sockp->nr_nam;
5273 /* No IP address match, so look for new/trunked one. */
5274 sad = malloc(sizeof(*sad), M_SONAME, M_WAITOK | M_ZERO);
5275 sad->sin_len = sizeof(*sad);
5276 sad->sin_family = AF_INET;
5277 sad->sin_port = sin->sin_port;
5278 sad->sin_addr.s_addr = sin->sin_addr.s_addr;
5279 nrp = malloc(sizeof(*nrp), M_NFSSOCKREQ, M_WAITOK | M_ZERO);
5280 nrp->nr_nam = (struct sockaddr *)sad;
5281 } else if (af == AF_INET6) {
5284 * Check to see if we already have a session for this
5285 * address that is usable for a DS.
5286 * Note that the MDS's address is in a different place
5287 * than the sessions already acquired for DS's.
5289 msad6 = (struct sockaddr_in6 *)nmp->nm_sockreq.nr_nam;
5290 tdsp = TAILQ_FIRST(&nmp->nm_sess);
5291 while (tdsp != NULL) {
5292 if (msad6 != NULL && msad6->sin6_family == AF_INET6 &&
5293 IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr,
5294 &msad6->sin6_addr) &&
5295 sin6->sin6_port == msad6->sin6_port &&
5296 (tdsp->nfsclds_flags & NFSCLDS_DS) != 0 &&
5297 tdsp->nfsclds_sess.nfsess_defunct == 0) {
5302 tdsp = TAILQ_NEXT(tdsp, nfsclds_list);
5303 if (tdsp != NULL && tdsp->nfsclds_sockp != NULL)
5304 msad6 = (struct sockaddr_in6 *)
5305 tdsp->nfsclds_sockp->nr_nam;
5311 /* No IP address match, so look for new/trunked one. */
5312 sad6 = malloc(sizeof(*sad6), M_SONAME, M_WAITOK | M_ZERO);
5313 sad6->sin6_len = sizeof(*sad6);
5314 sad6->sin6_family = AF_INET6;
5315 sad6->sin6_port = sin6->sin6_port;
5316 NFSBCOPY(&sin6->sin6_addr, &sad6->sin6_addr,
5317 sizeof(struct in6_addr));
5318 nrp = malloc(sizeof(*nrp), M_NFSSOCKREQ, M_WAITOK | M_ZERO);
5319 nrp->nr_nam = (struct sockaddr *)sad6;
5323 nrp->nr_sotype = SOCK_STREAM;
5324 mtx_init(&nrp->nr_mtx, "nfssock", NULL, MTX_DEF);
5325 nrp->nr_prog = NFS_PROG;
5326 nrp->nr_vers = vers;
5329 * Use the credentials that were used for the mount, which are
5330 * in nmp->nm_sockreq.nr_cred for newnfs_connect() etc.
5331 * Ref. counting the credentials with crhold() is probably not
5332 * necessary, since nm_sockreq.nr_cred won't be crfree()'d until
5333 * unmount, but I did it anyhow.
5335 nrp->nr_cred = crhold(nmp->nm_sockreq.nr_cred);
5336 error = newnfs_connect(nmp, nrp, NULL, p, 0);
5337 NFSCL_DEBUG(3, "DS connect=%d\n", error);
5339 /* Now, do the exchangeid and create session. */
5341 error = nfsrpc_exchangeid(nmp, clp, nrp, NFSV4EXCH_USEPNFSDS,
5342 &dsp, nrp->nr_cred, p);
5343 NFSCL_DEBUG(3, "DS exchangeid=%d\n", error);
5345 newnfs_disconnect(nrp);
5348 dsp->nfsclds_sockp = nrp;
5350 retv = nfscl_getsameserver(nmp, dsp, &tdsp);
5351 NFSCL_DEBUG(3, "getsame ret=%d\n", retv);
5352 if (retv == NFSDSP_USETHISSESSION) {
5355 * If there is already a session for this server,
5358 (void)newnfs_disconnect(nrp);
5359 nfscl_freenfsclds(dsp);
5363 if (retv == NFSDSP_SEQTHISSESSION)
5364 sequenceid = tdsp->nfsclds_sess.nfsess_sequenceid;
5366 sequenceid = dsp->nfsclds_sess.nfsess_sequenceid;
5368 error = nfsrpc_createsession(nmp, &dsp->nfsclds_sess,
5369 nrp, sequenceid, 0, nrp->nr_cred, p);
5370 NFSCL_DEBUG(3, "DS createsess=%d\n", error);
5372 NFSFREECRED(nrp->nr_cred);
5373 NFSFREEMUTEX(&nrp->nr_mtx);
5374 free(nrp->nr_nam, M_SONAME);
5375 free(nrp, M_NFSSOCKREQ);
5378 NFSCL_DEBUG(3, "add DS session\n");
5380 * Put it at the end of the list. That way the list
5381 * is ordered by when the entry was added. This matters
5382 * since the one done first is the one that should be
5383 * used for sequencid'ing any subsequent create sessions.
5386 TAILQ_INSERT_TAIL(&nmp->nm_sess, dsp, nfsclds_list);
5389 } else if (dsp != NULL) {
5390 newnfs_disconnect(nrp);
5391 nfscl_freenfsclds(dsp);
5397 * Do the NFSv4.1 Reclaim Complete.
5400 nfsrpc_reclaimcomplete(struct nfsmount *nmp, struct ucred *cred, NFSPROC_T *p)
5403 struct nfsrv_descript nfsd;
5404 struct nfsrv_descript *nd = &nfsd;
5407 nfscl_reqstart(nd, NFSPROC_RECLAIMCOMPL, nmp, NULL, 0, NULL, NULL, 0,
5409 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
5411 nd->nd_flag |= ND_USEGSSNAME;
5412 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
5413 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
5416 error = nd->nd_repstat;
5417 mbuf_freem(nd->nd_mrep);
5422 * Initialize the slot tables for a session.
5425 nfscl_initsessionslots(struct nfsclsession *sep)
5429 for (i = 0; i < NFSV4_CBSLOTS; i++) {
5430 if (sep->nfsess_cbslots[i].nfssl_reply != NULL)
5431 m_freem(sep->nfsess_cbslots[i].nfssl_reply);
5432 NFSBZERO(&sep->nfsess_cbslots[i], sizeof(struct nfsslot));
5434 for (i = 0; i < 64; i++)
5435 sep->nfsess_slotseq[i] = 0;
5436 sep->nfsess_slots = 0;
5440 * Called to try and do an I/O operation via an NFSv4.1 Data Server (DS).
5443 nfscl_doiods(vnode_t vp, struct uio *uiop, int *iomode, int *must_commit,
5444 uint32_t rwaccess, int docommit, struct ucred *cred, NFSPROC_T *p)
5446 struct nfsnode *np = VTONFS(vp);
5447 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
5448 struct nfscllayout *layp;
5449 struct nfscldevinfo *dip;
5450 struct nfsclflayout *rflp;
5451 nfsv4stateid_t stateid;
5452 struct ucred *newcred;
5453 uint64_t lastbyte, len, off, oresid, xfer;
5454 int eof, error, iolaymode, recalled;
5457 if (!NFSHASPNFS(nmp) || nfscl_enablecallb == 0 || nfs_numnfscbd == 0 ||
5458 (np->n_flag & NNOLAYOUT) != 0)
5460 /* Now, get a reference cnt on the clientid for this mount. */
5461 if (nfscl_getref(nmp) == 0)
5464 /* Find an appropriate stateid. */
5465 newcred = NFSNEWCRED(cred);
5466 error = nfscl_getstateid(vp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len,
5467 rwaccess, 1, newcred, p, &stateid, &lckp);
5469 NFSFREECRED(newcred);
5473 /* Search for a layout for this file. */
5474 off = uiop->uio_offset;
5475 layp = nfscl_getlayout(nmp->nm_clp, np->n_fhp->nfh_fh,
5476 np->n_fhp->nfh_len, off, &rflp, &recalled);
5477 if (layp == NULL || rflp == NULL) {
5478 if (recalled != 0) {
5479 NFSFREECRED(newcred);
5484 nfscl_rellayout(layp, (rflp == NULL) ? 1 : 0);
5487 /* Try and get a Layout, if it is supported. */
5488 if (rwaccess == NFSV4OPEN_ACCESSWRITE ||
5489 (np->n_flag & NWRITEOPENED) != 0)
5490 iolaymode = NFSLAYOUTIOMODE_RW;
5492 iolaymode = NFSLAYOUTIOMODE_READ;
5493 error = nfsrpc_getlayout(nmp, vp, np->n_fhp, iolaymode,
5494 NULL, &stateid, off, &layp, newcred, p);
5497 np->n_flag |= NNOLAYOUT;
5500 nfscl_lockderef(lckp);
5501 NFSFREECRED(newcred);
5503 nfscl_rellayout(layp, 0);
5510 * Loop around finding a layout that works for the first part of
5511 * this I/O operation, and then call the function that actually
5515 len = (uint64_t)uiop->uio_resid;
5516 while (len > 0 && error == 0 && eof == 0) {
5517 off = uiop->uio_offset;
5518 error = nfscl_findlayoutforio(layp, off, rwaccess, &rflp);
5520 oresid = xfer = (uint64_t)uiop->uio_resid;
5521 if (xfer > (rflp->nfsfl_end - rflp->nfsfl_off))
5522 xfer = rflp->nfsfl_end - rflp->nfsfl_off;
5523 dip = nfscl_getdevinfo(nmp->nm_clp, rflp->nfsfl_dev,
5526 error = nfscl_doflayoutio(vp, uiop, iomode,
5527 must_commit, &eof, &stateid, rwaccess, dip,
5528 layp, rflp, off, xfer, docommit, newcred,
5530 nfscl_reldevinfo(dip);
5531 lastbyte = off + xfer - 1;
5534 if (lastbyte > layp->nfsly_lastbyte)
5535 layp->nfsly_lastbyte = lastbyte;
5537 } else if (error == NFSERR_OPENMODE &&
5538 rwaccess == NFSV4OPEN_ACCESSREAD) {
5540 nmp->nm_state |= NFSSTA_OPENMODE;
5546 len -= (oresid - (uint64_t)uiop->uio_resid);
5550 nfscl_lockderef(lckp);
5551 NFSFREECRED(newcred);
5552 nfscl_rellayout(layp, 0);
5558 * Find a file layout that will handle the first bytes of the requested
5559 * range and return the information from it needed to to the I/O operation.
5562 nfscl_findlayoutforio(struct nfscllayout *lyp, uint64_t off, uint32_t rwaccess,
5563 struct nfsclflayout **retflpp)
5565 struct nfsclflayout *flp, *nflp, *rflp;
5570 /* For reading, do the Read list first and then the Write list. */
5572 if (rw == NFSV4OPEN_ACCESSREAD)
5573 flp = LIST_FIRST(&lyp->nfsly_flayread);
5575 flp = LIST_FIRST(&lyp->nfsly_flayrw);
5576 while (flp != NULL) {
5577 nflp = LIST_NEXT(flp, nfsfl_list);
5578 if (flp->nfsfl_off > off)
5580 if (flp->nfsfl_end > off &&
5581 (rflp == NULL || rflp->nfsfl_end < flp->nfsfl_end))
5585 if (rw == NFSV4OPEN_ACCESSREAD)
5586 rw = NFSV4OPEN_ACCESSWRITE;
5591 /* This one covers the most bytes starting at off. */
5599 * Do I/O using an NFSv4.1 file layout.
5602 nfscl_doflayoutio(vnode_t vp, struct uio *uiop, int *iomode, int *must_commit,
5603 int *eofp, nfsv4stateid_t *stateidp, int rwflag, struct nfscldevinfo *dp,
5604 struct nfscllayout *lyp, struct nfsclflayout *flp, uint64_t off,
5605 uint64_t len, int docommit, struct ucred *cred, NFSPROC_T *p)
5607 uint64_t io_off, rel_off, stripe_unit_size, transfer, xfer;
5608 int commit_thru_mds, error, stripe_index, stripe_pos;
5611 struct nfsclds **dspp;
5614 rel_off = off - flp->nfsfl_patoff;
5615 stripe_unit_size = (flp->nfsfl_util >> 6) & 0x3ffffff;
5616 stripe_pos = (rel_off / stripe_unit_size + flp->nfsfl_stripe1) %
5617 dp->nfsdi_stripecnt;
5618 transfer = stripe_unit_size - (rel_off % stripe_unit_size);
5621 /* Loop around, doing I/O for each stripe unit. */
5622 while (len > 0 && error == 0) {
5623 stripe_index = nfsfldi_stripeindex(dp, stripe_pos);
5624 dspp = nfsfldi_addr(dp, stripe_index);
5625 if (len > transfer && docommit == 0)
5629 if ((flp->nfsfl_util & NFSFLAYUTIL_DENSE) != 0) {
5631 if (stripe_pos >= flp->nfsfl_fhcnt)
5633 fhp = flp->nfsfl_fh[stripe_pos];
5634 io_off = (rel_off / (stripe_unit_size *
5635 dp->nfsdi_stripecnt)) * stripe_unit_size +
5636 rel_off % stripe_unit_size;
5638 /* Sparse layout. */
5639 if (flp->nfsfl_fhcnt > 1) {
5640 if (stripe_index >= flp->nfsfl_fhcnt)
5642 fhp = flp->nfsfl_fh[stripe_index];
5643 } else if (flp->nfsfl_fhcnt == 1)
5644 fhp = flp->nfsfl_fh[0];
5649 if ((flp->nfsfl_util & NFSFLAYUTIL_COMMIT_THRU_MDS) != 0) {
5650 commit_thru_mds = 1;
5654 commit_thru_mds = 0;
5655 mtx_lock(&np->n_mtx);
5656 np->n_flag |= NDSCOMMIT;
5657 mtx_unlock(&np->n_mtx);
5659 if (docommit != 0) {
5661 error = nfsrpc_commitds(vp, io_off, xfer,
5662 *dspp, fhp, cred, p);
5665 * Set both eof and uio_resid = 0 to end any
5669 uiop->uio_resid = 0;
5671 mtx_lock(&np->n_mtx);
5672 np->n_flag &= ~NDSCOMMIT;
5673 mtx_unlock(&np->n_mtx);
5675 } else if (rwflag == NFSV4OPEN_ACCESSREAD)
5676 error = nfsrpc_readds(vp, uiop, stateidp, eofp, *dspp,
5677 io_off, xfer, fhp, cred, p);
5679 error = nfsrpc_writeds(vp, uiop, iomode, must_commit,
5680 stateidp, *dspp, io_off, xfer, fhp, commit_thru_mds,
5684 lyp->nfsly_flags |= NFSLY_WRITTEN;
5689 transfer = stripe_unit_size;
5690 stripe_pos = (stripe_pos + 1) % dp->nfsdi_stripecnt;
5699 * The actual read RPC done to a DS.
5702 nfsrpc_readds(vnode_t vp, struct uio *uiop, nfsv4stateid_t *stateidp, int *eofp,
5703 struct nfsclds *dsp, uint64_t io_off, int len, struct nfsfh *fhp,
5704 struct ucred *cred, NFSPROC_T *p)
5708 struct nfsrv_descript nfsd;
5709 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
5710 struct nfsrv_descript *nd = &nfsd;
5711 struct nfssockreq *nrp;
5714 nfscl_reqstart(nd, NFSPROC_READDS, nmp, fhp->nfh_fh, fhp->nfh_len,
5715 NULL, &dsp->nfsclds_sess, 0, 0);
5716 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSEQIDZERO);
5717 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED * 3);
5718 txdr_hyper(io_off, tl);
5719 *(tl + 2) = txdr_unsigned(len);
5720 nrp = dsp->nfsclds_sockp;
5722 /* If NULL, use the MDS socket. */
5723 nrp = &nmp->nm_sockreq;
5724 error = newnfs_request(nd, nmp, NULL, nrp, vp, p, cred,
5725 NFS_PROG, NFS_VER4, NULL, 1, NULL, &dsp->nfsclds_sess);
5728 if (nd->nd_repstat != 0) {
5729 error = nd->nd_repstat;
5732 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
5733 *eofp = fxdr_unsigned(int, *tl);
5734 NFSM_STRSIZ(retlen, len);
5735 error = nfsm_mbufuio(nd, uiop, retlen);
5737 if (nd->nd_mrep != NULL)
5738 mbuf_freem(nd->nd_mrep);
5743 * The actual write RPC done to a DS.
5746 nfsrpc_writeds(vnode_t vp, struct uio *uiop, int *iomode, int *must_commit,
5747 nfsv4stateid_t *stateidp, struct nfsclds *dsp, uint64_t io_off, int len,
5748 struct nfsfh *fhp, int commit_thru_mds, struct ucred *cred, NFSPROC_T *p)
5751 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
5752 int error, rlen, commit, committed = NFSWRITE_FILESYNC;
5754 struct nfsrv_descript nfsd;
5755 struct nfsrv_descript *nd = &nfsd;
5756 struct nfssockreq *nrp;
5758 KASSERT(uiop->uio_iovcnt == 1, ("nfs: writerpc iovcnt > 1"));
5760 nfscl_reqstart(nd, NFSPROC_WRITEDS, nmp, fhp->nfh_fh, fhp->nfh_len,
5761 NULL, &dsp->nfsclds_sess, 0, 0);
5762 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSEQIDZERO);
5763 NFSM_BUILD(tl, uint32_t *, NFSX_HYPER + 2 * NFSX_UNSIGNED);
5764 txdr_hyper(io_off, tl);
5766 *tl++ = txdr_unsigned(*iomode);
5767 *tl = txdr_unsigned(len);
5768 nfsm_uiombuf(nd, uiop, len);
5769 nrp = dsp->nfsclds_sockp;
5771 /* If NULL, use the MDS socket. */
5772 nrp = &nmp->nm_sockreq;
5773 error = newnfs_request(nd, nmp, NULL, nrp, vp, p, cred,
5774 NFS_PROG, NFS_VER4, NULL, 1, NULL, &dsp->nfsclds_sess);
5777 if (nd->nd_repstat != 0) {
5779 * In case the rpc gets retried, roll
5780 * the uio fileds changed by nfsm_uiombuf()
5783 uiop->uio_offset -= len;
5784 uio_uio_resid_add(uiop, len);
5785 uio_iov_base_add(uiop, -len);
5786 uio_iov_len_add(uiop, len);
5787 error = nd->nd_repstat;
5789 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED + NFSX_VERF);
5790 rlen = fxdr_unsigned(int, *tl++);
5794 } else if (rlen < len) {
5795 backup = len - rlen;
5796 uio_iov_base_add(uiop, -(backup));
5797 uio_iov_len_add(uiop, backup);
5798 uiop->uio_offset -= backup;
5799 uio_uio_resid_add(uiop, backup);
5802 commit = fxdr_unsigned(int, *tl++);
5805 * Return the lowest commitment level
5806 * obtained by any of the RPCs.
5808 if (committed == NFSWRITE_FILESYNC)
5810 else if (committed == NFSWRITE_DATASYNC &&
5811 commit == NFSWRITE_UNSTABLE)
5813 if (commit_thru_mds != 0) {
5815 if (!NFSHASWRITEVERF(nmp)) {
5816 NFSBCOPY(tl, nmp->nm_verf, NFSX_VERF);
5817 NFSSETWRITEVERF(nmp);
5818 } else if (NFSBCMP(tl, nmp->nm_verf, NFSX_VERF)) {
5820 NFSBCOPY(tl, nmp->nm_verf, NFSX_VERF);
5825 if ((dsp->nfsclds_flags & NFSCLDS_HASWRITEVERF) == 0) {
5826 NFSBCOPY(tl, dsp->nfsclds_verf, NFSX_VERF);
5827 dsp->nfsclds_flags |= NFSCLDS_HASWRITEVERF;
5828 } else if (NFSBCMP(tl, dsp->nfsclds_verf, NFSX_VERF)) {
5830 NFSBCOPY(tl, dsp->nfsclds_verf, NFSX_VERF);
5836 if (nd->nd_mrep != NULL)
5837 mbuf_freem(nd->nd_mrep);
5838 *iomode = committed;
5839 if (nd->nd_repstat != 0 && error == 0)
5840 error = nd->nd_repstat;
5845 * Free up the nfsclds structure.
5848 nfscl_freenfsclds(struct nfsclds *dsp)
5854 if (dsp->nfsclds_sockp != NULL) {
5855 NFSFREECRED(dsp->nfsclds_sockp->nr_cred);
5856 NFSFREEMUTEX(&dsp->nfsclds_sockp->nr_mtx);
5857 free(dsp->nfsclds_sockp->nr_nam, M_SONAME);
5858 free(dsp->nfsclds_sockp, M_NFSSOCKREQ);
5860 NFSFREEMUTEX(&dsp->nfsclds_mtx);
5861 NFSFREEMUTEX(&dsp->nfsclds_sess.nfsess_mtx);
5862 for (i = 0; i < NFSV4_CBSLOTS; i++) {
5863 if (dsp->nfsclds_sess.nfsess_cbslots[i].nfssl_reply != NULL)
5865 dsp->nfsclds_sess.nfsess_cbslots[i].nfssl_reply);
5867 free(dsp, M_NFSCLDS);
5870 static enum nfsclds_state
5871 nfscl_getsameserver(struct nfsmount *nmp, struct nfsclds *newdsp,
5872 struct nfsclds **retdspp)
5874 struct nfsclds *dsp, *cur_dsp;
5877 * Search the list of nfsclds structures for one with the same
5881 TAILQ_FOREACH(dsp, &nmp->nm_sess, nfsclds_list) {
5882 if (dsp->nfsclds_servownlen == newdsp->nfsclds_servownlen &&
5883 dsp->nfsclds_servownlen != 0 &&
5884 !NFSBCMP(dsp->nfsclds_serverown, newdsp->nfsclds_serverown,
5885 dsp->nfsclds_servownlen) &&
5886 dsp->nfsclds_sess.nfsess_defunct == 0) {
5887 NFSCL_DEBUG(4, "fnd same fdsp=%p dsp=%p flg=0x%x\n",
5888 TAILQ_FIRST(&nmp->nm_sess), dsp,
5889 dsp->nfsclds_flags);
5890 /* Server major id matches. */
5891 if ((dsp->nfsclds_flags & NFSCLDS_DS) != 0) {
5893 return (NFSDSP_USETHISSESSION);
5897 * Note the first match, so it can be used for
5898 * sequence'ing new sessions.
5900 if (cur_dsp == NULL)
5904 if (cur_dsp != NULL) {
5906 return (NFSDSP_SEQTHISSESSION);
5908 return (NFSDSP_NOTFOUND);
5912 * NFS commit rpc to a NFSv4.1 DS.
5915 nfsrpc_commitds(vnode_t vp, uint64_t offset, int cnt, struct nfsclds *dsp,
5916 struct nfsfh *fhp, struct ucred *cred, NFSPROC_T *p)
5919 struct nfsrv_descript nfsd, *nd = &nfsd;
5920 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
5921 struct nfssockreq *nrp;
5925 nfscl_reqstart(nd, NFSPROC_COMMITDS, nmp, fhp->nfh_fh, fhp->nfh_len,
5926 NULL, &dsp->nfsclds_sess, 0, 0);
5927 NFSM_BUILD(tl, uint32_t *, NFSX_HYPER + NFSX_UNSIGNED);
5928 txdr_hyper(offset, tl);
5930 *tl = txdr_unsigned(cnt);
5931 nrp = dsp->nfsclds_sockp;
5933 /* If NULL, use the MDS socket. */
5934 nrp = &nmp->nm_sockreq;
5935 error = newnfs_request(nd, nmp, NULL, nrp, vp, p, cred,
5936 NFS_PROG, NFS_VER4, NULL, 1, NULL, &dsp->nfsclds_sess);
5939 if (nd->nd_repstat == 0) {
5940 NFSM_DISSECT(tl, u_int32_t *, NFSX_VERF);
5942 if (NFSBCMP(tl, dsp->nfsclds_verf, NFSX_VERF)) {
5943 NFSBCOPY(tl, dsp->nfsclds_verf, NFSX_VERF);
5944 error = NFSERR_STALEWRITEVERF;
5949 if (error == 0 && nd->nd_repstat != 0)
5950 error = nd->nd_repstat;
5951 mbuf_freem(nd->nd_mrep);
5956 * Set up the XDR arguments for the LayoutGet operation.
5959 nfsrv_setuplayoutget(struct nfsrv_descript *nd, int iomode, uint64_t offset,
5960 uint64_t len, uint64_t minlen, nfsv4stateid_t *stateidp, int layoutlen,
5965 NFSM_BUILD(tl, uint32_t *, 4 * NFSX_UNSIGNED + 3 * NFSX_HYPER +
5967 *tl++ = newnfs_false; /* Don't signal availability. */
5968 *tl++ = txdr_unsigned(NFSLAYOUT_NFSV4_1_FILES);
5969 *tl++ = txdr_unsigned(iomode);
5970 txdr_hyper(offset, tl);
5972 txdr_hyper(len, tl);
5974 txdr_hyper(minlen, tl);
5976 if (usecurstateid != 0) {
5977 /* Special stateid for Current stateid. */
5978 *tl++ = txdr_unsigned(1);
5983 *tl++ = txdr_unsigned(stateidp->seqid);
5984 NFSCL_DEBUG(4, "layget seq=%d\n", (int)stateidp->seqid);
5985 *tl++ = stateidp->other[0];
5986 *tl++ = stateidp->other[1];
5987 *tl++ = stateidp->other[2];
5989 *tl = txdr_unsigned(layoutlen);
5993 * Parse the reply for a successful LayoutGet operation.
5996 nfsrv_parselayoutget(struct nfsrv_descript *nd, nfsv4stateid_t *stateidp,
5997 int *retonclosep, struct nfsclflayouthead *flhp)
6000 struct nfsclflayout *flp, *prevflp, *tflp;
6001 int cnt, error, gotiomode, fhcnt, nfhlen, i, j;
6009 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED + NFSX_STATEID);
6014 stateidp->seqid = fxdr_unsigned(uint32_t, *tl++);
6015 NFSCL_DEBUG(4, "retoncls=%d stseq=%d\n", *retonclosep,
6016 (int)stateidp->seqid);
6017 stateidp->other[0] = *tl++;
6018 stateidp->other[1] = *tl++;
6019 stateidp->other[2] = *tl++;
6020 cnt = fxdr_unsigned(int, *tl);
6021 NFSCL_DEBUG(4, "layg cnt=%d\n", cnt);
6022 if (cnt <= 0 || cnt > 10000) {
6023 /* Don't accept more than 10000 layouts in reply. */
6024 error = NFSERR_BADXDR;
6027 for (i = 0; i < cnt; i++) {
6028 /* Dissect all the way to the file handle cnt. */
6029 NFSM_DISSECT(tl, uint32_t *, 3 * NFSX_HYPER +
6030 6 * NFSX_UNSIGNED + NFSX_V4DEVICEID);
6031 fhcnt = fxdr_unsigned(int, *(tl + 11 +
6032 NFSX_V4DEVICEID / NFSX_UNSIGNED));
6033 NFSCL_DEBUG(4, "fhcnt=%d\n", fhcnt);
6034 if (fhcnt < 0 || fhcnt > 100) {
6035 /* Don't accept more than 100 file handles. */
6036 error = NFSERR_BADXDR;
6040 flp = malloc(sizeof(*flp) + (fhcnt - 1) *
6041 sizeof(struct nfsfh *), M_NFSFLAYOUT, M_WAITOK);
6043 flp = malloc(sizeof(*flp), M_NFSFLAYOUT, M_WAITOK);
6044 flp->nfsfl_flags = 0;
6045 flp->nfsfl_fhcnt = 0;
6046 flp->nfsfl_devp = NULL;
6047 flp->nfsfl_off = fxdr_hyper(tl); tl += 2;
6048 retlen = fxdr_hyper(tl); tl += 2;
6049 if (flp->nfsfl_off + retlen < flp->nfsfl_off)
6050 flp->nfsfl_end = UINT64_MAX - flp->nfsfl_off;
6052 flp->nfsfl_end = flp->nfsfl_off + retlen;
6053 flp->nfsfl_iomode = fxdr_unsigned(int, *tl++);
6054 if (gotiomode == -1)
6055 gotiomode = flp->nfsfl_iomode;
6056 if (fxdr_unsigned(int, *tl++) != NFSLAYOUT_NFSV4_1_FILES) {
6057 printf("NFSv4.1: got non-files layout\n");
6058 error = NFSERR_BADXDR;
6061 NFSBCOPY(++tl, flp->nfsfl_dev, NFSX_V4DEVICEID);
6062 tl += (NFSX_V4DEVICEID / NFSX_UNSIGNED);
6063 flp->nfsfl_util = fxdr_unsigned(uint32_t, *tl++);
6064 NFSCL_DEBUG(4, "flutil=0x%x\n", flp->nfsfl_util);
6065 flp->nfsfl_stripe1 = fxdr_unsigned(uint32_t, *tl++);
6066 flp->nfsfl_patoff = fxdr_hyper(tl); tl += 2;
6067 if (fxdr_unsigned(int, *tl) != fhcnt) {
6068 printf("EEK! bad fhcnt\n");
6069 error = NFSERR_BADXDR;
6072 for (j = 0; j < fhcnt; j++) {
6073 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
6074 nfhlen = fxdr_unsigned(int, *tl);
6075 if (nfhlen <= 0 || nfhlen > NFSX_V4FHMAX) {
6076 error = NFSERR_BADXDR;
6079 nfhp = malloc(sizeof(*nfhp) + nfhlen - 1, M_NFSFH,
6081 flp->nfsfl_fh[j] = nfhp;
6083 nfhp->nfh_len = nfhlen;
6084 NFSM_DISSECT(cp, uint8_t *, NFSM_RNDUP(nfhlen));
6085 NFSBCOPY(cp, nfhp->nfh_fh, nfhlen);
6087 if (flp->nfsfl_iomode == gotiomode) {
6088 /* Keep the list in increasing offset order. */
6089 tflp = LIST_FIRST(flhp);
6091 while (tflp != NULL &&
6092 tflp->nfsfl_off < flp->nfsfl_off) {
6094 tflp = LIST_NEXT(tflp, nfsfl_list);
6096 if (prevflp == NULL)
6097 LIST_INSERT_HEAD(flhp, flp, nfsfl_list);
6099 LIST_INSERT_AFTER(prevflp, flp,
6102 printf("nfscl_layoutget(): got wrong iomode\n");
6103 nfscl_freeflayout(flp);
6108 if (error != 0 && flp != NULL)
6109 nfscl_freeflayout(flp);
6114 * Similar to nfsrpc_getlayout(), except that it uses nfsrpc_openlayget(),
6115 * so that it does both an Open and a Layoutget.
6118 nfsrpc_getopenlayout(struct nfsmount *nmp, vnode_t vp, u_int8_t *nfhp,
6119 int fhlen, uint8_t *newfhp, int newfhlen, uint32_t mode,
6120 struct nfsclopen *op, uint8_t *name, int namelen, struct nfscldeleg **dpp,
6121 struct ucred *cred, NFSPROC_T *p)
6123 struct nfscllayout *lyp;
6124 struct nfsclflayout *flp;
6125 struct nfsclflayouthead flh;
6126 int error, islocked, layoutlen, recalled, retonclose, usecurstateid;
6128 nfsv4stateid_t stateid;
6129 struct nfsclsession *tsep;
6133 * If lyp is returned non-NULL, there will be a refcnt (shared lock)
6134 * on it, iff flp != NULL or a lock (exclusive lock) on it iff
6137 lyp = nfscl_getlayout(nmp->nm_clp, newfhp, newfhlen, 0, &flp,
6139 NFSCL_DEBUG(4, "nfsrpc_getopenlayout nfscl_getlayout lyp=%p\n", lyp);
6142 else if (flp != NULL)
6146 if ((lyp == NULL || flp == NULL) && recalled == 0) {
6148 tsep = nfsmnt_mdssession(nmp);
6149 layoutlen = tsep->nfsess_maxcache - (NFSX_STATEID +
6155 stateid.seqid = lyp->nfsly_stateid.seqid;
6156 stateid.other[0] = lyp->nfsly_stateid.other[0];
6157 stateid.other[1] = lyp->nfsly_stateid.other[1];
6158 stateid.other[2] = lyp->nfsly_stateid.other[2];
6160 error = nfsrpc_openlayoutrpc(nmp, vp, nfhp, fhlen,
6161 newfhp, newfhlen, mode, op, name, namelen,
6162 dpp, &stateid, usecurstateid, layoutlen,
6163 &retonclose, &flh, &laystat, cred, p);
6164 NFSCL_DEBUG(4, "aft nfsrpc_openlayoutrpc laystat=%d err=%d\n",
6166 laystat = nfsrpc_layoutgetres(nmp, vp, newfhp, newfhlen,
6167 &stateid, retonclose, NULL, &lyp, &flh, laystat, &islocked,
6170 error = nfsrpc_openrpc(nmp, vp, nfhp, fhlen, newfhp, newfhlen,
6171 mode, op, name, namelen, dpp, 0, 0, cred, p, 0, 0);
6173 nfscl_rellayout(lyp, 1);
6174 else if (islocked == 1)
6175 nfscl_rellayout(lyp, 0);
6180 * This function does an Open+LayoutGet for an NFSv4.1 mount with pNFS
6181 * enabled, only for the CLAIM_NULL case. All other NFSv4 Opens are
6182 * handled by nfsrpc_openrpc().
6183 * For the case where op == NULL, dvp is the directory. When op != NULL, it
6187 nfsrpc_openlayoutrpc(struct nfsmount *nmp, vnode_t vp, u_int8_t *nfhp,
6188 int fhlen, uint8_t *newfhp, int newfhlen, uint32_t mode,
6189 struct nfsclopen *op, uint8_t *name, int namelen, struct nfscldeleg **dpp,
6190 nfsv4stateid_t *stateidp, int usecurstateid,
6191 int layoutlen, int *retonclosep, struct nfsclflayouthead *flhp,
6192 int *laystatp, struct ucred *cred, NFSPROC_T *p)
6195 struct nfsrv_descript nfsd, *nd = &nfsd;
6196 struct nfscldeleg *ndp = NULL;
6197 struct nfsvattr nfsva;
6198 struct nfsclsession *tsep;
6199 uint32_t rflags, deleg;
6200 nfsattrbit_t attrbits;
6201 int error, ret, acesize, limitby, iomode;
6205 nfscl_reqstart(nd, NFSPROC_OPENLAYGET, nmp, nfhp, fhlen, NULL, NULL,
6207 NFSM_BUILD(tl, uint32_t *, 5 * NFSX_UNSIGNED);
6208 *tl++ = txdr_unsigned(op->nfso_own->nfsow_seqid);
6209 *tl++ = txdr_unsigned(mode & NFSV4OPEN_ACCESSBOTH);
6210 *tl++ = txdr_unsigned((mode >> NFSLCK_SHIFT) & NFSV4OPEN_DENYBOTH);
6211 tsep = nfsmnt_mdssession(nmp);
6212 *tl++ = tsep->nfsess_clientid.lval[0];
6213 *tl = tsep->nfsess_clientid.lval[1];
6214 nfsm_strtom(nd, op->nfso_own->nfsow_owner, NFSV4CL_LOCKNAMELEN);
6215 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_UNSIGNED);
6216 *tl++ = txdr_unsigned(NFSV4OPEN_NOCREATE);
6217 *tl = txdr_unsigned(NFSV4OPEN_CLAIMNULL);
6218 nfsm_strtom(nd, name, namelen);
6219 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
6220 *tl = txdr_unsigned(NFSV4OP_GETATTR);
6221 NFSZERO_ATTRBIT(&attrbits);
6222 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_CHANGE);
6223 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEMODIFY);
6224 nfsrv_putattrbit(nd, &attrbits);
6225 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
6226 *tl = txdr_unsigned(NFSV4OP_LAYOUTGET);
6227 if ((mode & NFSV4OPEN_ACCESSWRITE) != 0)
6228 iomode = NFSLAYOUTIOMODE_RW;
6230 iomode = NFSLAYOUTIOMODE_READ;
6231 nfsrv_setuplayoutget(nd, iomode, 0, UINT64_MAX, 0, stateidp,
6232 layoutlen, usecurstateid);
6233 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, vp, p, cred,
6234 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
6237 NFSCL_INCRSEQID(op->nfso_own->nfsow_seqid, nd);
6238 if (nd->nd_repstat != 0)
6239 *laystatp = nd->nd_repstat;
6240 if ((nd->nd_flag & ND_NOMOREDATA) == 0) {
6241 /* ND_NOMOREDATA will be set if the Open operation failed. */
6242 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
6244 op->nfso_stateid.seqid = *tl++;
6245 op->nfso_stateid.other[0] = *tl++;
6246 op->nfso_stateid.other[1] = *tl++;
6247 op->nfso_stateid.other[2] = *tl;
6248 rflags = fxdr_unsigned(u_int32_t, *(tl + 6));
6249 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
6252 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
6253 deleg = fxdr_unsigned(u_int32_t, *tl);
6254 if (deleg == NFSV4OPEN_DELEGATEREAD ||
6255 deleg == NFSV4OPEN_DELEGATEWRITE) {
6256 if (!(op->nfso_own->nfsow_clp->nfsc_flags &
6257 NFSCLFLAGS_FIRSTDELEG))
6258 op->nfso_own->nfsow_clp->nfsc_flags |=
6259 (NFSCLFLAGS_FIRSTDELEG | NFSCLFLAGS_GOTDELEG);
6260 ndp = malloc(sizeof(struct nfscldeleg) + newfhlen,
6261 M_NFSCLDELEG, M_WAITOK);
6262 LIST_INIT(&ndp->nfsdl_owner);
6263 LIST_INIT(&ndp->nfsdl_lock);
6264 ndp->nfsdl_clp = op->nfso_own->nfsow_clp;
6265 ndp->nfsdl_fhlen = newfhlen;
6266 NFSBCOPY(newfhp, ndp->nfsdl_fh, newfhlen);
6267 newnfs_copyincred(cred, &ndp->nfsdl_cred);
6268 nfscl_lockinit(&ndp->nfsdl_rwlock);
6269 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
6271 ndp->nfsdl_stateid.seqid = *tl++;
6272 ndp->nfsdl_stateid.other[0] = *tl++;
6273 ndp->nfsdl_stateid.other[1] = *tl++;
6274 ndp->nfsdl_stateid.other[2] = *tl++;
6275 ret = fxdr_unsigned(int, *tl);
6276 if (deleg == NFSV4OPEN_DELEGATEWRITE) {
6277 ndp->nfsdl_flags = NFSCLDL_WRITE;
6279 * Indicates how much the file can grow.
6281 NFSM_DISSECT(tl, u_int32_t *,
6283 limitby = fxdr_unsigned(int, *tl++);
6285 case NFSV4OPEN_LIMITSIZE:
6286 ndp->nfsdl_sizelimit = fxdr_hyper(tl);
6288 case NFSV4OPEN_LIMITBLOCKS:
6289 ndp->nfsdl_sizelimit =
6290 fxdr_unsigned(u_int64_t, *tl++);
6291 ndp->nfsdl_sizelimit *=
6292 fxdr_unsigned(u_int64_t, *tl);
6295 error = NFSERR_BADXDR;
6299 ndp->nfsdl_flags = NFSCLDL_READ;
6301 ndp->nfsdl_flags |= NFSCLDL_RECALL;
6302 error = nfsrv_dissectace(nd, &ndp->nfsdl_ace, &ret,
6306 } else if (deleg != NFSV4OPEN_DELEGATENONE) {
6307 error = NFSERR_BADXDR;
6310 if ((rflags & NFSV4OPEN_LOCKTYPEPOSIX) != 0 ||
6311 nfscl_assumeposixlocks)
6312 op->nfso_posixlock = 1;
6314 op->nfso_posixlock = 0;
6315 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
6316 /* If the 2nd element == NFS_OK, the Getattr succeeded. */
6318 error = nfsv4_loadattr(nd, NULL, &nfsva, NULL,
6319 NULL, 0, NULL, NULL, NULL, NULL, NULL, 0,
6320 NULL, NULL, NULL, p, cred);
6324 ndp->nfsdl_change = nfsva.na_filerev;
6325 ndp->nfsdl_modtime = nfsva.na_mtime;
6326 ndp->nfsdl_flags |= NFSCLDL_MODTIMESET;
6331 * At this point, the Open has succeeded, so set
6332 * nd_repstat = NFS_OK. If the Layoutget failed,
6333 * this function just won't return a layout.
6335 if (nd->nd_repstat == 0) {
6336 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
6337 *laystatp = fxdr_unsigned(int, *++tl);
6338 if (*laystatp == 0) {
6339 error = nfsrv_parselayoutget(nd,
6340 stateidp, retonclosep, flhp);
6345 nd->nd_repstat = 0; /* Return 0 for Open. */
6348 if (nd->nd_repstat != 0 && error == 0)
6349 error = nd->nd_repstat;
6351 free(ndp, M_NFSCLDELEG);
6352 mbuf_freem(nd->nd_mrep);
6357 * Similar nfsrpc_createv4(), but also does the LayoutGet operation.
6358 * Used only for mounts with pNFS enabled.
6361 nfsrpc_createlayout(vnode_t dvp, char *name, int namelen, struct vattr *vap,
6362 nfsquad_t cverf, int fmode, struct nfsclowner *owp, struct nfscldeleg **dpp,
6363 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap,
6364 struct nfsvattr *nnap, struct nfsfh **nfhpp, int *attrflagp,
6365 int *dattrflagp, void *dstuff, int *unlockedp, nfsv4stateid_t *stateidp,
6366 int usecurstateid, int layoutlen, int *retonclosep,
6367 struct nfsclflayouthead *flhp, int *laystatp)
6370 int error = 0, deleg, newone, ret, acesize, limitby;
6371 struct nfsrv_descript nfsd, *nd = &nfsd;
6372 struct nfsclopen *op;
6373 struct nfscldeleg *dp = NULL;
6376 struct nfsclsession *tsep;
6377 nfsattrbit_t attrbits;
6378 nfsv4stateid_t stateid;
6380 struct nfsmount *nmp;
6382 nmp = VFSTONFS(dvp->v_mount);
6390 if (namelen > NFS_MAXNAMLEN)
6391 return (ENAMETOOLONG);
6392 NFSCL_REQSTART(nd, NFSPROC_CREATELAYGET, dvp);
6394 * For V4, this is actually an Open op.
6396 NFSM_BUILD(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
6397 *tl++ = txdr_unsigned(owp->nfsow_seqid);
6398 *tl++ = txdr_unsigned(NFSV4OPEN_ACCESSWRITE |
6399 NFSV4OPEN_ACCESSREAD);
6400 *tl++ = txdr_unsigned(NFSV4OPEN_DENYNONE);
6401 tsep = nfsmnt_mdssession(nmp);
6402 *tl++ = tsep->nfsess_clientid.lval[0];
6403 *tl = tsep->nfsess_clientid.lval[1];
6404 nfsm_strtom(nd, owp->nfsow_owner, NFSV4CL_LOCKNAMELEN);
6405 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
6406 *tl++ = txdr_unsigned(NFSV4OPEN_CREATE);
6407 if ((fmode & O_EXCL) != 0) {
6408 if (NFSHASSESSPERSIST(nmp)) {
6409 /* Use GUARDED for persistent sessions. */
6410 *tl = txdr_unsigned(NFSCREATE_GUARDED);
6411 nfscl_fillsattr(nd, vap, dvp, 0, 0);
6413 /* Otherwise, use EXCLUSIVE4_1. */
6414 *tl = txdr_unsigned(NFSCREATE_EXCLUSIVE41);
6415 NFSM_BUILD(tl, u_int32_t *, NFSX_VERF);
6416 *tl++ = cverf.lval[0];
6417 *tl = cverf.lval[1];
6418 nfscl_fillsattr(nd, vap, dvp, 0, 0);
6421 *tl = txdr_unsigned(NFSCREATE_UNCHECKED);
6422 nfscl_fillsattr(nd, vap, dvp, 0, 0);
6424 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
6425 *tl = txdr_unsigned(NFSV4OPEN_CLAIMNULL);
6426 nfsm_strtom(nd, name, namelen);
6427 /* Get the new file's handle and attributes, plus save the FH. */
6428 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
6429 *tl++ = txdr_unsigned(NFSV4OP_SAVEFH);
6430 *tl++ = txdr_unsigned(NFSV4OP_GETFH);
6431 *tl = txdr_unsigned(NFSV4OP_GETATTR);
6432 NFSGETATTR_ATTRBIT(&attrbits);
6433 nfsrv_putattrbit(nd, &attrbits);
6434 /* Get the directory's post-op attributes. */
6435 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
6436 *tl = txdr_unsigned(NFSV4OP_PUTFH);
6437 nfsm_fhtom(nd, np->n_fhp->nfh_fh, np->n_fhp->nfh_len, 0);
6438 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
6439 *tl = txdr_unsigned(NFSV4OP_GETATTR);
6440 nfsrv_putattrbit(nd, &attrbits);
6441 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
6442 *tl++ = txdr_unsigned(NFSV4OP_RESTOREFH);
6443 *tl = txdr_unsigned(NFSV4OP_LAYOUTGET);
6444 nfsrv_setuplayoutget(nd, NFSLAYOUTIOMODE_RW, 0, UINT64_MAX, 0, stateidp,
6445 layoutlen, usecurstateid);
6446 error = nfscl_request(nd, dvp, p, cred, dstuff);
6449 NFSCL_DEBUG(4, "nfsrpc_createlayout stat=%d err=%d\n", nd->nd_repstat,
6451 if (nd->nd_repstat != 0)
6452 *laystatp = nd->nd_repstat;
6453 NFSCL_INCRSEQID(owp->nfsow_seqid, nd);
6454 if ((nd->nd_flag & ND_NOMOREDATA) == 0) {
6455 NFSCL_DEBUG(4, "nfsrpc_createlayout open succeeded\n");
6456 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
6458 stateid.seqid = *tl++;
6459 stateid.other[0] = *tl++;
6460 stateid.other[1] = *tl++;
6461 stateid.other[2] = *tl;
6462 rflags = fxdr_unsigned(u_int32_t, *(tl + 6));
6463 nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
6464 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
6465 deleg = fxdr_unsigned(int, *tl);
6466 if (deleg == NFSV4OPEN_DELEGATEREAD ||
6467 deleg == NFSV4OPEN_DELEGATEWRITE) {
6468 if (!(owp->nfsow_clp->nfsc_flags &
6469 NFSCLFLAGS_FIRSTDELEG))
6470 owp->nfsow_clp->nfsc_flags |=
6471 (NFSCLFLAGS_FIRSTDELEG | NFSCLFLAGS_GOTDELEG);
6472 dp = malloc(sizeof(struct nfscldeleg) + NFSX_V4FHMAX,
6473 M_NFSCLDELEG, M_WAITOK);
6474 LIST_INIT(&dp->nfsdl_owner);
6475 LIST_INIT(&dp->nfsdl_lock);
6476 dp->nfsdl_clp = owp->nfsow_clp;
6477 newnfs_copyincred(cred, &dp->nfsdl_cred);
6478 nfscl_lockinit(&dp->nfsdl_rwlock);
6479 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
6481 dp->nfsdl_stateid.seqid = *tl++;
6482 dp->nfsdl_stateid.other[0] = *tl++;
6483 dp->nfsdl_stateid.other[1] = *tl++;
6484 dp->nfsdl_stateid.other[2] = *tl++;
6485 ret = fxdr_unsigned(int, *tl);
6486 if (deleg == NFSV4OPEN_DELEGATEWRITE) {
6487 dp->nfsdl_flags = NFSCLDL_WRITE;
6489 * Indicates how much the file can grow.
6491 NFSM_DISSECT(tl, u_int32_t *,
6493 limitby = fxdr_unsigned(int, *tl++);
6495 case NFSV4OPEN_LIMITSIZE:
6496 dp->nfsdl_sizelimit = fxdr_hyper(tl);
6498 case NFSV4OPEN_LIMITBLOCKS:
6499 dp->nfsdl_sizelimit =
6500 fxdr_unsigned(u_int64_t, *tl++);
6501 dp->nfsdl_sizelimit *=
6502 fxdr_unsigned(u_int64_t, *tl);
6505 error = NFSERR_BADXDR;
6509 dp->nfsdl_flags = NFSCLDL_READ;
6512 dp->nfsdl_flags |= NFSCLDL_RECALL;
6513 error = nfsrv_dissectace(nd, &dp->nfsdl_ace, &ret,
6517 } else if (deleg != NFSV4OPEN_DELEGATENONE) {
6518 error = NFSERR_BADXDR;
6522 /* Now, we should have the status for the SaveFH. */
6523 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
6525 NFSCL_DEBUG(4, "nfsrpc_createlayout SaveFH ok\n");
6527 * Now, process the GetFH and Getattr for the newly
6528 * created file. nfscl_mtofh() will set
6529 * ND_NOMOREDATA if these weren't successful.
6531 error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp);
6532 NFSCL_DEBUG(4, "aft nfscl_mtofh err=%d\n", error);
6536 nd->nd_flag |= ND_NOMOREDATA;
6537 /* Now we have the PutFH and Getattr for the directory. */
6538 if ((nd->nd_flag & ND_NOMOREDATA) == 0) {
6539 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
6541 nd->nd_flag |= ND_NOMOREDATA;
6543 NFSM_DISSECT(tl, uint32_t *, 2 *
6546 nd->nd_flag |= ND_NOMOREDATA;
6549 if ((nd->nd_flag & ND_NOMOREDATA) == 0) {
6550 /* Load the directory attributes. */
6551 error = nfsm_loadattr(nd, dnap);
6552 NFSCL_DEBUG(4, "aft nfsm_loadattr err=%d\n", error);
6556 if (dp != NULL && *attrflagp != 0) {
6557 dp->nfsdl_change = nnap->na_filerev;
6558 dp->nfsdl_modtime = nnap->na_mtime;
6559 dp->nfsdl_flags |= NFSCLDL_MODTIMESET;
6562 * We can now complete the Open state.
6566 dp->nfsdl_fhlen = nfhp->nfh_len;
6567 NFSBCOPY(nfhp->nfh_fh, dp->nfsdl_fh,
6571 * Get an Open structure that will be
6572 * attached to the OpenOwner, acquired already.
6574 error = nfscl_open(dvp, nfhp->nfh_fh, nfhp->nfh_len,
6575 (NFSV4OPEN_ACCESSWRITE | NFSV4OPEN_ACCESSREAD), 0,
6576 cred, p, NULL, &op, &newone, NULL, 0);
6579 op->nfso_stateid = stateid;
6580 newnfs_copyincred(cred, &op->nfso_cred);
6582 nfscl_openrelease(nmp, op, error, newone);
6585 /* Now, handle the RestoreFH and LayoutGet. */
6586 if (nd->nd_repstat == 0) {
6587 NFSM_DISSECT(tl, uint32_t *, 4 * NFSX_UNSIGNED);
6588 *laystatp = fxdr_unsigned(int, *(tl + 3));
6589 if (*laystatp == 0) {
6590 error = nfsrv_parselayoutget(nd,
6591 stateidp, retonclosep, flhp);
6595 NFSCL_DEBUG(4, "aft nfsrv_parselayout err=%d\n",
6601 if (nd->nd_repstat != 0 && error == 0)
6602 error = nd->nd_repstat;
6603 if (error == NFSERR_STALECLIENTID || error == NFSERR_BADSESSION)
6604 nfscl_initiate_recovery(owp->nfsow_clp);
6606 NFSCL_DEBUG(4, "eo nfsrpc_createlayout err=%d\n", error);
6610 free(dp, M_NFSCLDELEG);
6611 mbuf_freem(nd->nd_mrep);
6616 * Similar to nfsrpc_getopenlayout(), except that it used for the Create case.
6619 nfsrpc_getcreatelayout(vnode_t dvp, char *name, int namelen, struct vattr *vap,
6620 nfsquad_t cverf, int fmode, struct nfsclowner *owp, struct nfscldeleg **dpp,
6621 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap,
6622 struct nfsvattr *nnap, struct nfsfh **nfhpp, int *attrflagp,
6623 int *dattrflagp, void *dstuff, int *unlockedp)
6625 struct nfscllayout *lyp;
6626 struct nfsclflayouthead flh;
6628 struct nfsclsession *tsep;
6629 struct nfsmount *nmp;
6630 nfsv4stateid_t stateid;
6631 int error, layoutlen, retonclose, laystat;
6634 nmp = VFSTONFS(dvp->v_mount);
6636 tsep = nfsmnt_mdssession(nmp);
6637 layoutlen = tsep->nfsess_maxcache - (NFSX_STATEID + 3 * NFSX_UNSIGNED);
6638 error = nfsrpc_createlayout(dvp, name, namelen, vap, cverf, fmode,
6639 owp, dpp, cred, p, dnap, nnap, nfhpp, attrflagp, dattrflagp,
6640 dstuff, unlockedp, &stateid, 1, layoutlen, &retonclose, &flh,
6642 NFSCL_DEBUG(4, "aft nfsrpc_createlayoutrpc laystat=%d err=%d\n",
6647 laystat = nfsrpc_layoutgetres(nmp, dvp, nfhp->nfh_fh,
6648 nfhp->nfh_len, &stateid, retonclose, NULL, &lyp, &flh,
6649 laystat, NULL, cred, p);
6651 laystat = nfsrpc_layoutgetres(nmp, dvp, NULL, 0, &stateid,
6652 retonclose, NULL, &lyp, &flh, laystat, NULL, cred, p);
6654 nfscl_rellayout(lyp, 0);
6659 * Process the results of a layoutget() operation.
6662 nfsrpc_layoutgetres(struct nfsmount *nmp, vnode_t vp, uint8_t *newfhp,
6663 int newfhlen, nfsv4stateid_t *stateidp, int retonclose, uint32_t *notifybit,
6664 struct nfscllayout **lypp, struct nfsclflayouthead *flhp,
6665 int laystat, int *islockedp, struct ucred *cred, NFSPROC_T *p)
6667 struct nfsclflayout *tflp;
6668 struct nfscldevinfo *dip;
6670 if (laystat == NFSERR_UNKNLAYOUTTYPE) {
6672 NFSCL_DEBUG(1, "disable PNFS\n");
6674 nmp->nm_state &= ~NFSSTA_PNFS;
6678 NFSCL_DEBUG(4, "nfsrpc_layoutgetres at FOREACH\n");
6679 LIST_FOREACH(tflp, flhp, nfsfl_list) {
6680 laystat = nfscl_adddevinfo(nmp, NULL, tflp);
6681 NFSCL_DEBUG(4, "aft adddev=%d\n", laystat);
6683 laystat = nfsrpc_getdeviceinfo(nmp,
6684 tflp->nfsfl_dev, NFSLAYOUT_NFSV4_1_FILES,
6685 notifybit, &dip, cred, p);
6686 NFSCL_DEBUG(4, "aft nfsrpc_gdi=%d\n",
6690 laystat = nfscl_adddevinfo(nmp, dip, tflp);
6692 printf("getlayout: cannot add\n");
6698 * nfscl_layout() always returns with the nfsly_lock
6699 * set to a refcnt (shared lock).
6700 * Passing in dvp is sufficient, since it is only used to
6701 * get the fsid for the file system.
6703 laystat = nfscl_layout(nmp, vp, newfhp, newfhlen, stateidp,
6704 retonclose, flhp, lypp, cred, p);
6705 NFSCL_DEBUG(4, "nfsrpc_layoutgetres: aft nfscl_layout=%d\n",
6707 if (laystat == 0 && islockedp != NULL)