]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/fs/nfsclient/nfs_clrpcops.c
Change nfsv4_getipaddr() and nfsrpc_fillsa() to not use sockaddr_storage.
[FreeBSD/FreeBSD.git] / sys / fs / nfsclient / nfs_clrpcops.c
1 /*-
2  * Copyright (c) 1989, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Rick Macklem at The University of Guelph.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
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.
19  *
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
30  * SUCH DAMAGE.
31  *
32  */
33
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
36
37 /*
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.
43  */
44
45 #ifndef APPLEKEXT
46 #include "opt_inet6.h"
47
48 #include <fs/nfs/nfsport.h>
49 #include <sys/sysctl.h>
50
51 SYSCTL_DECL(_vfs_nfs);
52
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");
56
57 /*
58  * Global variables
59  */
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;
67 NFSCLSTATEMUTEX;
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 */
74
75 #define DIRHDSIZ        offsetof(struct dirent, d_name)
76
77 /*
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
81  *     session.
82  * NFSDSP_NOTFOUND - No matching server was found.
83  */
84 enum nfsclds_state {
85         NFSDSP_USETHISSESSION = 0,
86         NFSDSP_SEQTHISSESSION = 1,
87         NFSDSP_NOTFOUND = 2,
88 };
89
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 *,
96     void *);
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 *,
118     NFSPROC_T *);
119 static int nfsrpc_readds(vnode_t, struct uio *, nfsv4stateid_t *, int *,
120     struct nfsclds *, uint64_t, int, struct nfsfh *, struct ucred *,
121     NFSPROC_T *);
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 *);
155
156 /*
157  * nfs null call from vfs.
158  */
159 APPLESTATIC int
160 nfsrpc_null(vnode_t vp, struct ucred *cred, NFSPROC_T *p)
161 {
162         int error;
163         struct nfsrv_descript nfsd, *nd = &nfsd;
164         
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);
170         return (error);
171 }
172
173 /*
174  * nfs access rpc op.
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.
177  */
178 APPLESTATIC int
179 nfsrpc_access(vnode_t vp, int acmode, struct ucred *cred,
180     NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp)
181 {
182         int error;
183         u_int32_t mode, rmode;
184
185         if (acmode & VREAD)
186                 mode = NFSACCESS_READ;
187         else
188                 mode = 0;
189         if (vnode_vtype(vp) == VDIR) {
190                 if (acmode & VWRITE)
191                         mode |= (NFSACCESS_MODIFY | NFSACCESS_EXTEND |
192                                  NFSACCESS_DELETE);
193                 if (acmode & VEXEC)
194                         mode |= NFSACCESS_LOOKUP;
195         } else {
196                 if (acmode & VWRITE)
197                         mode |= (NFSACCESS_MODIFY | NFSACCESS_EXTEND);
198                 if (acmode & VEXEC)
199                         mode |= NFSACCESS_EXECUTE;
200         }
201
202         /*
203          * Now, just call nfsrpc_accessrpc() to do the actual RPC.
204          */
205         error = nfsrpc_accessrpc(vp, mode, cred, p, nap, attrflagp, &rmode,
206             NULL);
207
208         /*
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...
212          */
213         if (!error && (rmode & mode) != mode)
214                 error = EACCES;
215         return (error);
216 }
217
218 /*
219  * The actual rpc, separated out for Darwin.
220  */
221 APPLESTATIC int
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,
224     void *stuff)
225 {
226         u_int32_t *tl;
227         u_int32_t supported, rmode;
228         int error;
229         struct nfsrv_descript nfsd, *nd = &nfsd;
230         nfsattrbit_t attrbits;
231
232         *attrflagp = 0;
233         supported = mode;
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) {
238                 /*
239                  * And do a Getattr op.
240                  */
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);
245         }
246         error = nfscl_request(nd, vp, p, cred, stuff);
247         if (error)
248                 return (error);
249         if (nd->nd_flag & ND_NFSV3) {
250                 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
251                 if (error)
252                         goto nfsmout;
253         }
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++);
258                 } else {
259                         NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
260                 }
261                 rmode = fxdr_unsigned(u_int32_t, *tl);
262                 if (nd->nd_flag & ND_NFSV4)
263                         error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
264
265                 /*
266                  * It's not obvious what should be done about
267                  * unsupported access modes. For now, be paranoid
268                  * and clear the unsupported ones.
269                  */
270                 rmode &= supported;
271                 *rmodep = rmode;
272         } else
273                 error = nd->nd_repstat;
274 nfsmout:
275         mbuf_freem(nd->nd_mrep);
276         return (error);
277 }
278
279 /*
280  * nfs open rpc
281  */
282 APPLESTATIC int
283 nfsrpc_open(vnode_t vp, int amode, struct ucred *cred, NFSPROC_T *p)
284 {
285         struct nfsclopen *op;
286         struct nfscldeleg *dp;
287         struct nfsfh *nfhp;
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;
292
293         /*
294          * For NFSv4, Open Ops are only done on Regular Files.
295          */
296         if (vnode_vtype(vp) != VREG)
297                 return (0);
298         mode = 0;
299         if (amode & FREAD)
300                 mode |= NFSV4OPEN_ACCESSREAD;
301         if (amode & FWRITE)
302                 mode |= NFSV4OPEN_ACCESSWRITE;
303         nfhp = np->n_fhp;
304
305         retrycnt = 0;
306 #ifdef notdef
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);
310 name[namel] = '\0';
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");
314 }
315 #endif
316         do {
317             dp = NULL;
318             error = nfscl_open(vp, nfhp->nfh_fh, nfhp->nfh_len, mode, 1,
319                 cred, p, NULL, &op, &newone, &ret, 1);
320             if (error) {
321                 return (error);
322             }
323             if (nmp->nm_clp != NULL)
324                 clidrev = nmp->nm_clp->nfsc_clientidrev;
325             else
326                 clidrev = 0;
327             if (ret == NFSCLOPEN_DOOPEN) {
328                 if (np->n_v4 != NULL) {
329                         /*
330                          * For the first attempt, try and get a layout, if
331                          * pNFS is enabled for the mount.
332                          */
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,
337                                     np->n_v4->n4_data,
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);
343                         else
344                                 error = nfsrpc_getopenlayout(nmp, vp,
345                                     np->n_v4->n4_data,
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);
350                         if (dp != NULL) {
351 #ifdef APPLE
352                                 OSBitAndAtomic((int32_t)~NDELEGMOD, (UInt32 *)&np->n_flag);
353 #else
354                                 NFSLOCKNODE(np);
355                                 np->n_flag &= ~NDELEGMOD;
356                                 /*
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.
362                                  */
363                                 NFSINVALATTRCACHE(np);
364                                 NFSUNLOCKNODE(np);
365 #endif
366                                 (void) nfscl_deleg(nmp->nm_mountp,
367                                     op->nfso_own->nfsow_clp,
368                                     nfhp->nfh_fh, nfhp->nfh_len, cred, p, &dp);
369                         }
370                 } else {
371                         error = EIO;
372                 }
373                 newnfs_copyincred(cred, &op->nfso_cred);
374             } else if (ret == NFSCLOPEN_SETCRED)
375                 /*
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.
379                  */
380                 newnfs_copyincred(cred, &op->nfso_cred);
381
382             /*
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.
388              */
389             if (!error)
390                 op->nfso_opencnt++;
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)
397                 && clidrev != 0) {
398                 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
399                 retrycnt++;
400             }
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)
407                 error = EIO;
408         return (error);
409 }
410
411 /*
412  * the actual open rpc
413  */
414 APPLESTATIC int
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)
420 {
421         u_int32_t *tl;
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;
429
430         dp = *dpp;
431         *dpp = NULL;
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);
443         if (reclaim) {
444                 *tl = txdr_unsigned(NFSV4OPEN_CLAIMPREVIOUS);
445                 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
446                 *tl = txdr_unsigned(delegtype);
447         } else {
448                 if (dp != NULL) {
449                         *tl = txdr_unsigned(NFSV4OPEN_CLAIMDELEGATECUR);
450                         NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID);
451                         if (NFSHASNFSV4N(nmp))
452                                 *tl++ = 0;
453                         else
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];
458                 } else {
459                         *tl = txdr_unsigned(NFSV4OPEN_CLAIMNULL);
460                 }
461                 (void) nfsm_strtom(nd, name, namelen);
462         }
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);
469         if (syscred)
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);
473         if (error)
474                 return (error);
475         NFSCL_INCRSEQID(op->nfso_own->nfsow_seqid, nd);
476         if (!nd->nd_repstat) {
477                 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
478                     6 * NFSX_UNSIGNED);
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);
485                 if (error)
486                         goto nfsmout;
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 +
506                             NFSX_UNSIGNED);
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;
514                                 /*
515                                  * Indicates how much the file can grow.
516                                  */
517                                 NFSM_DISSECT(tl, u_int32_t *,
518                                     3 * NFSX_UNSIGNED);
519                                 limitby = fxdr_unsigned(int, *tl++);
520                                 switch (limitby) {
521                                 case NFSV4OPEN_LIMITSIZE:
522                                         ndp->nfsdl_sizelimit = fxdr_hyper(tl);
523                                         break;
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);
529                                         break;
530                                 default:
531                                         error = NFSERR_BADXDR;
532                                         goto nfsmout;
533                                 }
534                         } else {
535                                 ndp->nfsdl_flags = NFSCLDL_READ;
536                         }
537                         if (ret)
538                                 ndp->nfsdl_flags |= NFSCLDL_RECALL;
539                         error = nfsrv_dissectace(nd, &ndp->nfsdl_ace, &ret,
540                             &acesize, p);
541                         if (error)
542                                 goto nfsmout;
543                 } else if (deleg != NFSV4OPEN_DELEGATENONE) {
544                         error = NFSERR_BADXDR;
545                         goto nfsmout;
546                 }
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);
551                 if (error)
552                         goto nfsmout;
553                 if (ndp != NULL) {
554                         ndp->nfsdl_change = nfsva.na_filerev;
555                         ndp->nfsdl_modtime = nfsva.na_mtime;
556                         ndp->nfsdl_flags |= NFSCLDL_MODTIMESET;
557                 }
558                 if (!reclaim && (rflags & NFSV4OPEN_RESULTCONFIRM)) {
559                     do {
560                         ret = nfsrpc_openconfirm(vp, newfhp, newfhlen, op,
561                             cred, p);
562                         if (ret == NFSERR_DELAY)
563                             (void) nfs_catnap(PZERO, ret, "nfs_open");
564                     } while (ret == NFSERR_DELAY);
565                     error = ret;
566                 }
567                 if ((rflags & NFSV4OPEN_LOCKTYPEPOSIX) ||
568                     nfscl_assumeposixlocks)
569                     op->nfso_posixlock = 1;
570                 else
571                     op->nfso_posixlock = 0;
572
573                 /*
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.
578                  */
579                 if (!reclaim && (rflags & NFSV4OPEN_RESULTCONFIRM) &&
580                     (op->nfso_own->nfsow_clp->nfsc_flags & NFSCLFLAGS_GOTDELEG)
581                     && !error && dp == NULL && ndp == NULL && !recursed) {
582                     do {
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);
589                     if (ret) {
590                         if (ndp != NULL) {
591                                 FREE((caddr_t)ndp, M_NFSCLDELEG);
592                                 ndp = NULL;
593                         }
594                         if (ret == NFSERR_STALECLIENTID ||
595                             ret == NFSERR_STALEDONTRECOVER ||
596                             ret == NFSERR_BADSESSION)
597                                 error = ret;
598                     }
599                 }
600         }
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);
605 nfsmout:
606         if (!error)
607                 *dpp = ndp;
608         else if (ndp != NULL)
609                 FREE((caddr_t)ndp, M_NFSCLDELEG);
610         mbuf_freem(nd->nd_mrep);
611         return (error);
612 }
613
614 /*
615  * open downgrade rpc
616  */
617 APPLESTATIC int
618 nfsrpc_opendowngrade(vnode_t vp, u_int32_t mode, struct nfsclopen *op,
619     struct ucred *cred, NFSPROC_T *p)
620 {
621         u_int32_t *tl;
622         struct nfsrv_descript nfsd, *nd = &nfsd;
623         int error;
624
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))))
628                 *tl++ = 0;
629         else
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);
638         if (error)
639                 return (error);
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;
647         }
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);
652 nfsmout:
653         mbuf_freem(nd->nd_mrep);
654         return (error);
655 }
656
657 /*
658  * V4 Close operation.
659  */
660 APPLESTATIC int
661 nfsrpc_close(vnode_t vp, int doclose, NFSPROC_T *p)
662 {
663         struct nfsclclient *clp;
664         int error;
665
666         if (vnode_vtype(vp) != VREG)
667                 return (0);
668         if (doclose)
669                 error = nfscl_doclose(vp, &clp, p);
670         else
671                 error = nfscl_getclose(vp, &clp);
672         if (error)
673                 return (error);
674
675         nfscl_clientrelease(clp);
676         return (0);
677 }
678
679 /*
680  * Close the open.
681  */
682 APPLESTATIC void
683 nfsrpc_doclose(struct nfsmount *nmp, struct nfsclopen *op, NFSPROC_T *p)
684 {
685         struct nfsrv_descript nfsd, *nd = &nfsd;
686         struct nfscllockowner *lp, *nlp;
687         struct nfscllock *lop, *nlop;
688         struct ucred *tcred;
689         u_int64_t off = 0, len = 0;
690         u_int32_t type = NFSV4LOCKT_READ;
691         int error, do_unlock, trycnt;
692
693         tcred = newnfs_getcred();
694         newnfs_copycred(&op->nfso_cred, tcred);
695         /*
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.)
700          */
701         if (op->nfso_posixlock) {
702                 off = 0;
703                 len = NFS64BITSSET;
704                 type = NFSV4LOCKT_READ;
705         }
706
707         /*
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.
712          */
713         LIST_FOREACH(lp, &op->nfso_lock, nfsl_list) {
714                 do_unlock = 1;
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;
721                                 else
722                                         type = NFSV4LOCKT_READ;
723                         }
724                         if (do_unlock) {
725                                 trycnt = 0;
726                                 do {
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) &&
731                                             error == 0)
732                                                 (void) nfs_catnap(PZERO,
733                                                     (int)nd->nd_repstat,
734                                                     "nfs_close");
735                                 } while ((nd->nd_repstat == NFSERR_GRACE ||
736                                     nd->nd_repstat == NFSERR_DELAY) &&
737                                     error == 0 && trycnt++ < 5);
738                                 if (op->nfso_posixlock)
739                                         do_unlock = 0;
740                         }
741                         nfscl_freelock(lop, 0);
742                 }
743                 /*
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.
749                  */
750                 (void)nfsrpc_rellockown(nmp, lp, lp->nfsl_open->nfso_fh,
751                     lp->nfsl_open->nfso_fhlen, tcred, p);
752         }
753
754         /*
755          * There could be other Opens for different files on the same
756          * OpenOwner, so locking is required.
757          */
758         NFSLOCKCLSTATE();
759         nfscl_lockexcl(&op->nfso_own->nfsow_rwlock, NFSCLSTATEMUTEXPTR);
760         NFSUNLOCKCLSTATE();
761         do {
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);
766         NFSLOCKCLSTATE();
767         nfscl_lockunlock(&op->nfso_own->nfsow_rwlock);
768
769         LIST_FOREACH_SAFE(lp, &op->nfso_lock, nfsl_list, nlp)
770                 nfscl_freelockowner(lp, 0);
771         nfscl_freeopen(op, 0);
772         NFSUNLOCKCLSTATE();
773         NFSFREECRED(tcred);
774 }
775
776 /*
777  * The actual Close RPC.
778  */
779 APPLESTATIC int
780 nfsrpc_closerpc(struct nfsrv_descript *nd, struct nfsmount *nmp,
781     struct nfsclopen *op, struct ucred *cred, NFSPROC_T *p,
782     int syscred)
783 {
784         u_int32_t *tl;
785         int error;
786
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))
792                 *tl++ = 0;
793         else
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];
798         if (syscred)
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);
802         if (error)
803                 return (error);
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);
810 nfsmout:
811         mbuf_freem(nd->nd_mrep);
812         return (error);
813 }
814
815 /*
816  * V4 Open Confirm RPC.
817  */
818 APPLESTATIC int
819 nfsrpc_openconfirm(vnode_t vp, u_int8_t *nfhp, int fhlen,
820     struct nfsclopen *op, struct ucred *cred, NFSPROC_T *p)
821 {
822         u_int32_t *tl;
823         struct nfsrv_descript nfsd, *nd = &nfsd;
824         struct nfsmount *nmp;
825         int error;
826
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,
831             0, 0);
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);
839         if (error)
840                 return (error);
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;
848         }
849         error = nd->nd_repstat;
850         if (error == NFSERR_STALESTATEID)
851                 nfscl_initiate_recovery(op->nfso_own->nfsow_clp);
852 nfsmout:
853         mbuf_freem(nd->nd_mrep);
854         return (error);
855 }
856
857 /*
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.
860  */
861 APPLESTATIC int
862 nfsrpc_setclient(struct nfsmount *nmp, struct nfsclclient *clp, int reclaim,
863     struct ucred *cred, NFSPROC_T *p)
864 {
865         u_int32_t *tl;
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];
870         u_short port;
871         int error, isinet6 = 0, callblen;
872         nfsquad_t confirm;
873         u_int32_t lease;
874         static u_int32_t rev = 0;
875         struct nfsclds *dsp;
876         struct in6_addr a6;
877         struct nfsclsession *tsep;
878
879         if (nfsboottime.tv_sec == 0)
880                 NFSSETBOOTTIME(nfsboottime);
881         clp->nfsc_rev = rev++;
882         if (NFSHASNFSV4N(nmp)) {
883                 /*
884                  * Either there was no previous session or the
885                  * previous session has failed, so...
886                  * do an ExchangeID followed by the CreateSession.
887                  */
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);
891                 if (error == 0)
892                         error = nfsrpc_createsession(nmp, &dsp->nfsclds_sess,
893                             &nmp->nm_sockreq,
894                             dsp->nfsclds_sess.nfsess_sequenceid, 1, cred, p);
895                 if (error == 0) {
896                         NFSLOCKMNT(nmp);
897                         /*
898                          * The old sessions cannot be safely free'd
899                          * here, since they may still be used by
900                          * in-progress RPCs.
901                          */
902                         tsep = NULL;
903                         if (TAILQ_FIRST(&nmp->nm_sess) != NULL)
904                                 tsep = NFSMNT_MDSSESSION(nmp);
905                         TAILQ_INSERT_HEAD(&nmp->nm_sess, dsp,
906                             nfsclds_list);
907                         /*
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
913                          * new session.
914                          */
915                         if (tsep != NULL)
916                                 wakeup(&tsep->nfsess_slots);
917                         wakeup(&nmp->nm_sess);
918                         NFSUNLOCKMNT(nmp);
919                 } else
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. */
928                                 error = 0;
929                 }
930                 return (error);
931         }
932
933         /*
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.
936          */
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);
940         NFSLOCKMNT(nmp);
941         TAILQ_INSERT_HEAD(&nmp->nm_sess, dsp, nfsclds_list);
942         tsep = NFSMNT_MDSSESSION(nmp);
943         NFSUNLOCKMNT(nmp);
944
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);
950
951         /*
952          * set up the callback address
953          */
954         NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
955         *tl = txdr_unsigned(NFS_CALLBCKPROG);
956         callblen = strlen(nfsv4_callbackaddr);
957         if (callblen == 0)
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;
963 #ifdef INET6
964                 if ((callblen > 0 &&
965                      strchr(nfsv4_callbackaddr, ':')) || isinet6) {
966                         char ip6buf[INET6_ADDRSTRLEN], *ip6add;
967
968                         (void) nfsm_strtom(nd, "tcp6", 4);
969                         if (callblen == 0) {
970                                 ip6_sprintf(ip6buf, (struct in6_addr *)cp);
971                                 ip6add = ip6buf;
972                         } else {
973                                 ip6add = nfsv4_callbackaddr;
974                         }
975                         snprintf(addr, INET6_ADDRSTRLEN + 9, "%s.%d.%d",
976                             ip6add, cp2[0], cp2[1]);
977                 } else
978 #endif
979                 {
980                         (void) nfsm_strtom(nd, "tcp", 3);
981                         if (callblen == 0)
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]);
985                         else
986                                 snprintf(addr, INET6_ADDRSTRLEN + 9,
987                                     "%s.%d.%d", nfsv4_callbackaddr,
988                                     cp2[0], cp2[1]);
989                 }
990                 (void) nfsm_strtom(nd, addr, strlen(addr));
991         } else {
992                 (void) nfsm_strtom(nd, "tcp", 3);
993                 (void) nfsm_strtom(nd, "0.0.0.0.0.0", 11);
994         }
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);
1000         if (error)
1001                 return (error);
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);
1009             nd->nd_mrep = NULL;
1010
1011             /*
1012              * and confirm it.
1013              */
1014             nfscl_reqstart(nd, NFSPROC_SETCLIENTIDCFRM, nmp, NULL, 0, NULL,
1015                 NULL, 0, 0);
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);
1024             if (error)
1025                 return (error);
1026             mbuf_freem(nd->nd_mrep);
1027             nd->nd_mrep = NULL;
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);
1037                 if (error)
1038                     return (error);
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);
1042                     if (error)
1043                         goto nfsmout;
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++;
1049                 }
1050             }
1051         }
1052         error = nd->nd_repstat;
1053 nfsmout:
1054         mbuf_freem(nd->nd_mrep);
1055         return (error);
1056 }
1057
1058 /*
1059  * nfs getattr call.
1060  */
1061 APPLESTATIC int
1062 nfsrpc_getattr(vnode_t vp, struct ucred *cred, NFSPROC_T *p,
1063     struct nfsvattr *nap, void *stuff)
1064 {
1065         struct nfsrv_descript nfsd, *nd = &nfsd;
1066         int error;
1067         nfsattrbit_t attrbits;
1068         
1069         NFSCL_REQSTART(nd, NFSPROC_GETATTR, vp);
1070         if (nd->nd_flag & ND_NFSV4) {
1071                 NFSGETATTR_ATTRBIT(&attrbits);
1072                 (void) nfsrv_putattrbit(nd, &attrbits);
1073         }
1074         error = nfscl_request(nd, vp, p, cred, stuff);
1075         if (error)
1076                 return (error);
1077         if (!nd->nd_repstat)
1078                 error = nfsm_loadattr(nd, nap);
1079         else
1080                 error = nd->nd_repstat;
1081         mbuf_freem(nd->nd_mrep);
1082         return (error);
1083 }
1084
1085 /*
1086  * nfs getattr call with non-vnode arguemnts.
1087  */
1088 APPLESTATIC int
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,
1091     uint32_t *leasep)
1092 {
1093         struct nfsrv_descript nfsd, *nd = &nfsd;
1094         int error, vers = NFS_VER2;
1095         nfsattrbit_t attrbits;
1096         
1097         nfscl_reqstart(nd, NFSPROC_GETATTR, nmp, fhp, fhlen, NULL, NULL, 0, 0);
1098         if (nd->nd_flag & ND_NFSV4) {
1099                 vers = NFS_VER4;
1100                 NFSGETATTR_ATTRBIT(&attrbits);
1101                 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_LEASETIME);
1102                 (void) nfsrv_putattrbit(nd, &attrbits);
1103         } else if (nd->nd_flag & ND_NFSV3) {
1104                 vers = NFS_VER3;
1105         }
1106         if (syscred)
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);
1110         if (error)
1111                 return (error);
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,
1116                             NULL, NULL);
1117                 else
1118                         error = nfsm_loadattr(nd, nap);
1119         } else
1120                 error = nd->nd_repstat;
1121         mbuf_freem(nd->nd_mrep);
1122         return (error);
1123 }
1124
1125 /*
1126  * Do an nfs setattr operation.
1127  */
1128 APPLESTATIC int
1129 nfsrpc_setattr(vnode_t vp, struct vattr *vap, NFSACL_T *aclp,
1130     struct ucred *cred, NFSPROC_T *p, struct nfsvattr *rnap, int *attrflagp,
1131     void *stuff)
1132 {
1133         int error, expireret = 0, openerr, retrycnt;
1134         u_int32_t clidrev = 0, mode;
1135         struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
1136         struct nfsfh *nfhp;
1137         nfsv4stateid_t stateid;
1138         void *lckp;
1139
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;
1144         else
1145                 mode = NFSV4OPEN_ACCESSREAD;
1146         retrycnt = 0;
1147         do {
1148                 lckp = NULL;
1149                 openerr = 1;
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)) {
1157                                 /*
1158                                  * No Open stateid, so try and open the file
1159                                  * now.
1160                                  */
1161                                 if (mode == NFSV4OPEN_ACCESSWRITE)
1162                                         openerr = nfsrpc_open(vp, FWRITE, cred,
1163                                             p);
1164                                 else
1165                                         openerr = nfsrpc_open(vp, FREAD, cred,
1166                                             p);
1167                                 if (!openerr)
1168                                         (void) nfscl_getstateid(vp,
1169                                             nfhp->nfh_fh, nfhp->nfh_len,
1170                                             mode, 0, cred, p, &stateid, &lckp);
1171                         }
1172                 }
1173                 if (vap != NULL)
1174                         error = nfsrpc_setattrrpc(vp, vap, &stateid, cred, p,
1175                             rnap, attrflagp, stuff);
1176                 else
1177                         error = nfsrpc_setaclrpc(vp, cred, p, aclp, &stateid,
1178                             stuff);
1179                 if (error == NFSERR_OPENMODE && mode == NFSV4OPEN_ACCESSREAD) {
1180                         NFSLOCKMNT(nmp);
1181                         nmp->nm_state |= NFSSTA_OPENMODE;
1182                         NFSUNLOCKMNT(nmp);
1183                 }
1184                 if (error == NFSERR_STALESTATEID)
1185                         nfscl_initiate_recovery(nmp->nm_clp);
1186                 if (lckp != NULL)
1187                         nfscl_lockderef(lckp);
1188                 if (!openerr)
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);
1197                 }
1198                 retrycnt++;
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 &&
1206              retrycnt < 4));
1207         if (error && retrycnt >= 4)
1208                 error = EIO;
1209         return (error);
1210 }
1211
1212 static int
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)
1216 {
1217         u_int32_t *tl;
1218         struct nfsrv_descript nfsd, *nd = &nfsd;
1219         int error;
1220         nfsattrbit_t attrbits;
1221
1222         *attrflagp = 0;
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);
1230                 *tl = newnfs_false;
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);
1236         }
1237         error = nfscl_request(nd, vp, p, cred, stuff);
1238         if (error)
1239                 return (error);
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;
1249         return (error);
1250 }
1251
1252 /*
1253  * nfs lookup rpc
1254  */
1255 APPLESTATIC int
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)
1259 {
1260         u_int32_t *tl;
1261         struct nfsrv_descript nfsd, *nd = &nfsd;
1262         struct nfsmount *nmp;
1263         struct nfsnode *np;
1264         struct nfsfh *nfhp;
1265         nfsattrbit_t attrbits;
1266         int error = 0, lookupp = 0;
1267
1268         *attrflagp = 0;
1269         *dattrflagp = 0;
1270         if (vnode_vtype(dvp) != VDIR)
1271                 return (ENOTDIR);
1272         nmp = VFSTONFS(vnode_mount(dvp));
1273         if (len > NFS_MAXNAMLEN)
1274                 return (ENAMETOOLONG);
1275         if (NFSHASNFSV4(nmp) && len == 1 &&
1276                 name[0] == '.') {
1277                 /*
1278                  * Just return the current dir's fh.
1279                  */
1280                 np = VTONFS(dvp);
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);
1285                 *nfhpp = nfhp;
1286                 return (0);
1287         }
1288         if (NFSHASNFSV4(nmp) && len == 2 &&
1289                 name[0] == '.' && name[1] == '.') {
1290                 lookupp = 1;
1291                 NFSCL_REQSTART(nd, NFSPROC_LOOKUPP, dvp);
1292         } else {
1293                 NFSCL_REQSTART(nd, NFSPROC_LOOKUP, dvp);
1294                 (void) nfsm_strtom(nd, name, len);
1295         }
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);
1302         }
1303         error = nfscl_request(nd, dvp, p, cred, stuff);
1304         if (error)
1305                 return (error);
1306         if (nd->nd_repstat) {
1307                 /*
1308                  * When an NFSv4 Lookupp returns ENOENT, it means that
1309                  * the lookup is at the root of an fs, so return this dir.
1310                  */
1311                 if (nd->nd_repstat == NFSERR_NOENT && lookupp) {
1312                     np = VTONFS(dvp);
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);
1317                     *nfhpp = nfhp;
1318                     mbuf_freem(nd->nd_mrep);
1319                     return (0);
1320                 }
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)) ==
1324                     ND_NFSV4) {
1325                         /* Load the directory attributes. */
1326                         error = nfsm_loadattr(nd, dnap);
1327                         if (error == 0)
1328                                 *dattrflagp = 1;
1329                 }
1330                 goto nfsmout;
1331         }
1332         if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) == ND_NFSV4) {
1333                 /* Load the directory attributes. */
1334                 error = nfsm_loadattr(nd, dnap);
1335                 if (error != 0)
1336                         goto nfsmout;
1337                 *dattrflagp = 1;
1338                 /* Skip over the Lookup and GetFH operation status values. */
1339                 NFSM_DISSECT(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1340         }
1341         error = nfsm_getfh(nd, nfhpp);
1342         if (error)
1343                 goto nfsmout;
1344
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);
1348 nfsmout:
1349         mbuf_freem(nd->nd_mrep);
1350         if (!error && nd->nd_repstat)
1351                 error = nd->nd_repstat;
1352         return (error);
1353 }
1354
1355 /*
1356  * Do a readlink rpc.
1357  */
1358 APPLESTATIC int
1359 nfsrpc_readlink(vnode_t vp, struct uio *uiop, struct ucred *cred,
1360     NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, void *stuff)
1361 {
1362         u_int32_t *tl;
1363         struct nfsrv_descript nfsd, *nd = &nfsd;
1364         struct nfsnode *np = VTONFS(vp);
1365         nfsattrbit_t attrbits;
1366         int error, len, cangetattr = 1;
1367
1368         *attrflagp = 0;
1369         NFSCL_REQSTART(nd, NFSPROC_READLINK, vp);
1370         if (nd->nd_flag & ND_NFSV4) {
1371                 /*
1372                  * And do a Getattr op.
1373                  */
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);
1378         }
1379         error = nfscl_request(nd, vp, p, cred, stuff);
1380         if (error)
1381                 return (error);
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);
1386                 /*
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?
1391                  */
1392                 if (len == NFS_MAXPATHLEN) {
1393                         NFSLOCKNODE(np);
1394                         if (np->n_size > 0 && np->n_size < NFS_MAXPATHLEN) {
1395                                 len = np->n_size;
1396                                 cangetattr = 0;
1397                         }
1398                         NFSUNLOCKNODE(np);
1399                 }
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);
1403         }
1404         if (nd->nd_repstat && !error)
1405                 error = nd->nd_repstat;
1406 nfsmout:
1407         mbuf_freem(nd->nd_mrep);
1408         return (error);
1409 }
1410
1411 /*
1412  * Read operation.
1413  */
1414 APPLESTATIC int
1415 nfsrpc_read(vnode_t vp, struct uio *uiop, struct ucred *cred,
1416     NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, void *stuff)
1417 {
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;
1425         void *lckp;
1426
1427         if (nmp->nm_clp != NULL)
1428                 clidrev = nmp->nm_clp->nfsc_clientidrev;
1429         newcred = cred;
1430         if (NFSHASNFSV4(nmp)) {
1431                 nfhp = np->n_fhp;
1432                 newcred = NFSNEWCRED(cred);
1433         }
1434         retrycnt = 0;
1435         do {
1436                 lckp = NULL;
1437                 if (NFSHASNFSV4(nmp))
1438                         (void)nfscl_getstateid(vp, nfhp->nfh_fh, nfhp->nfh_len,
1439                             NFSV4OPEN_ACCESSREAD, 0, newcred, p, &stateid,
1440                             &lckp);
1441                 error = nfsrpc_readrpc(vp, uiop, newcred, &stateid, p, nap,
1442                     attrflagp, stuff);
1443                 if (error == NFSERR_OPENMODE) {
1444                         NFSLOCKMNT(nmp);
1445                         nmp->nm_state |= NFSSTA_OPENMODE;
1446                         NFSUNLOCKMNT(nmp);
1447                 }
1448                 if (error == NFSERR_STALESTATEID)
1449                         nfscl_initiate_recovery(nmp->nm_clp);
1450                 if (lckp != NULL)
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);
1459                 }
1460                 retrycnt++;
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)
1469                 error = EIO;
1470         if (NFSHASNFSV4(nmp))
1471                 NFSFREECRED(newcred);
1472         return (error);
1473 }
1474
1475 /*
1476  * The actual read RPC.
1477  */
1478 static int
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)
1482 {
1483         u_int32_t *tl;
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;
1488         int rsize;
1489         off_t tmp_off;
1490
1491         *attrflagp = 0;
1492         tsiz = uio_uio_resid(uiop);
1493         tmp_off = uiop->uio_offset + tsiz;
1494         NFSLOCKMNT(nmp);
1495         if (tmp_off > nmp->nm_maxfilesize || tmp_off < uiop->uio_offset) {
1496                 NFSUNLOCKMNT(nmp);
1497                 return (EFBIG);
1498         }
1499         rsize = nmp->nm_rsize;
1500         NFSUNLOCKMNT(nmp);
1501         nd->nd_mrep = NULL;
1502         while (tsiz > 0) {
1503                 *attrflagp = 0;
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);
1512                         *tl = 0;
1513                 } else {
1514                         txdr_hyper(uiop->uio_offset, tl);
1515                         *(tl + 2) = txdr_unsigned(len);
1516                 }
1517                 /*
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.)
1521                  */
1522                 error = nfscl_request(nd, vp, p, cred, stuff);
1523                 if (error)
1524                         return (error);
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);
1529                         if (!error)
1530                                 *attrflagp = 1;
1531                 }
1532                 if (nd->nd_repstat || error) {
1533                         if (!error)
1534                                 error = nd->nd_repstat;
1535                         goto nfsmout;
1536                 }
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);
1543                 }
1544                 NFSM_STRSIZ(retlen, len);
1545                 error = nfsm_mbufuio(nd, uiop, retlen);
1546                 if (error)
1547                         goto nfsmout;
1548                 mbuf_freem(nd->nd_mrep);
1549                 nd->nd_mrep = NULL;
1550                 tsiz -= retlen;
1551                 if (!(nd->nd_flag & ND_NFSV2)) {
1552                         if (eof || retlen == 0)
1553                                 tsiz = 0;
1554                 } else if (retlen < len)
1555                         tsiz = 0;
1556         }
1557         return (0);
1558 nfsmout:
1559         if (nd->nd_mrep != NULL)
1560                 mbuf_freem(nd->nd_mrep);
1561         return (error);
1562 }
1563
1564 /*
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.
1571  */
1572 APPLESTATIC int
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)
1576 {
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;
1584         void *lckp;
1585
1586         *must_commit = 0;
1587         if (nmp->nm_clp != NULL)
1588                 clidrev = nmp->nm_clp->nfsc_clientidrev;
1589         newcred = cred;
1590         if (NFSHASNFSV4(nmp)) {
1591                 newcred = NFSNEWCRED(cred);
1592                 nfhp = np->n_fhp;
1593         }
1594         retrycnt = 0;
1595         do {
1596                 lckp = NULL;
1597                 nostateid = 0;
1598                 if (NFSHASNFSV4(nmp)) {
1599                         (void)nfscl_getstateid(vp, nfhp->nfh_fh, nfhp->nfh_len,
1600                             NFSV4OPEN_ACCESSWRITE, 0, newcred, p, &stateid,
1601                             &lckp);
1602                         if (stateid.other[0] == 0 && stateid.other[1] == 0 &&
1603                             stateid.other[2] == 0) {
1604                                 nostateid = 1;
1605                                 NFSCL_DEBUG(1, "stateid0 in write\n");
1606                         }
1607                 }
1608
1609                 /*
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.
1613                  */
1614                 if (nostateid)
1615                         error = 0;
1616                 else
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);
1621                 if (lckp != NULL)
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);
1630                 }
1631                 retrycnt++;
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)))
1641                 error = EIO;
1642         if (NFSHASNFSV4(nmp))
1643                 NFSFREECRED(newcred);
1644         return (error);
1645 }
1646
1647 /*
1648  * The actual write RPC.
1649  */
1650 static int
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)
1654 {
1655         u_int32_t *tl;
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;
1660         int32_t backup;
1661         struct nfsrv_descript nfsd;
1662         struct nfsrv_descript *nd = &nfsd;
1663         nfsattrbit_t attrbits;
1664         off_t tmp_off;
1665
1666         KASSERT(uiop->uio_iovcnt == 1, ("nfs: writerpc iovcnt > 1"));
1667         *attrflagp = 0;
1668         tsiz = uio_uio_resid(uiop);
1669         tmp_off = uiop->uio_offset + tsiz;
1670         NFSLOCKMNT(nmp);
1671         if (tmp_off > nmp->nm_maxfilesize || tmp_off < uiop->uio_offset) {
1672                 NFSUNLOCKMNT(nmp);
1673                 return (EFBIG);
1674         }
1675         wsize = nmp->nm_wsize;
1676         NFSUNLOCKMNT(nmp);
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 */
1679         while (tsiz > 0) {
1680                 *attrflagp = 0;
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);
1687                         tl += 2;
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);
1693                         tl += 2;
1694                         *tl++ = txdr_unsigned(len);
1695                         *tl++ = txdr_unsigned(*iomode);
1696                         *tl = txdr_unsigned(len);
1697                 } else {
1698                         u_int32_t x;
1699
1700                         NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1701                         /*
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.
1706                          */
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 */
1714
1715                 }
1716                 nfsm_uiombuf(nd, uiop, len);
1717                 /*
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.
1736                  */
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);
1742                 }
1743                 error = nfscl_request(nd, vp, p, cred, stuff);
1744                 if (error)
1745                         return (error);
1746                 if (nd->nd_repstat) {
1747                         /*
1748                          * In case the rpc gets retried, roll
1749                          * the uio fileds changed by nfsm_uiombuf()
1750                          * back.
1751                          */
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);
1756                 }
1757                 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) {
1758                         error = nfscl_wcc_data(nd, vp, nap, attrflagp,
1759                             &wccflag, stuff);
1760                         if (error)
1761                                 goto nfsmout;
1762                 }
1763                 if (!nd->nd_repstat) {
1764                         if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) {
1765                                 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED
1766                                         + NFSX_VERF);
1767                                 rlen = fxdr_unsigned(int, *tl++);
1768                                 if (rlen == 0) {
1769                                         error = NFSERR_IO;
1770                                         goto nfsmout;
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);
1777                                         len = rlen;
1778                                 }
1779                                 commit = fxdr_unsigned(int, *tl++);
1780
1781                                 /*
1782                                  * Return the lowest commitment level
1783                                  * obtained by any of the RPCs.
1784                                  */
1785                                 if (committed == NFSWRITE_FILESYNC)
1786                                         committed = commit;
1787                                 else if (committed == NFSWRITE_DATASYNC &&
1788                                         commit == NFSWRITE_UNSTABLE)
1789                                         committed = commit;
1790                                 NFSLOCKMNT(nmp);
1791                                 if (!NFSHASWRITEVERF(nmp)) {
1792                                         NFSBCOPY((caddr_t)tl,
1793                                             (caddr_t)&nmp->nm_verf[0],
1794                                             NFSX_VERF);
1795                                         NFSSETWRITEVERF(nmp);
1796                                 } else if (NFSBCMP(tl, nmp->nm_verf,
1797                                     NFSX_VERF)) {
1798                                         *must_commit = 1;
1799                                         NFSBCOPY(tl, nmp->nm_verf, NFSX_VERF);
1800                                 }
1801                                 NFSUNLOCKMNT(nmp);
1802                         }
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);
1807                                 if (!error)
1808                                         *attrflagp = NFS_LATTR_NOSHRINK;
1809                         }
1810                 } else {
1811                         error = nd->nd_repstat;
1812                 }
1813                 if (error)
1814                         goto nfsmout;
1815                 NFSWRITERPC_SETTIME(wccflag, np, nap, (nd->nd_flag & ND_NFSV4));
1816                 mbuf_freem(nd->nd_mrep);
1817                 nd->nd_mrep = NULL;
1818                 tsiz -= len;
1819         }
1820 nfsmout:
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;
1826         return (error);
1827 }
1828
1829 /*
1830  * nfs mknod rpc
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.
1833  */
1834 APPLESTATIC int
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)
1839 {
1840         u_int32_t *tl;
1841         int error = 0;
1842         struct nfsrv_descript nfsd, *nd = &nfsd;
1843         nfsattrbit_t attrbits;
1844
1845         *nfhpp = NULL;
1846         *attrflagp = 0;
1847         *dattrflagp = 0;
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));
1857                 } else {
1858                         NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1859                         *tl = vtonfsv34_type(vtyp);
1860                 }
1861         }
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);
1866         }
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));
1874         }
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);
1881         }
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);
1885         if (error)
1886                 return (error);
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);
1893                         if (error)
1894                                 goto nfsmout;
1895                 }
1896                 error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp);
1897                 if (error)
1898                         goto nfsmout;
1899         }
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;
1904 nfsmout:
1905         mbuf_freem(nd->nd_mrep);
1906         return (error);
1907 }
1908
1909 /*
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.)
1913  */
1914 APPLESTATIC int
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)
1919 {
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));
1924         u_int32_t clidrev;
1925
1926         if (NFSHASNFSV4(nmp)) {
1927             retrycnt = 0;
1928             do {
1929                 dp = NULL;
1930                 error = nfscl_open(dvp, NULL, 0, (NFSV4OPEN_ACCESSWRITE |
1931                     NFSV4OPEN_ACCESSREAD), 0, cred, p, &owp, NULL, &newone,
1932                     NULL, 1);
1933                 if (error)
1934                         return (error);
1935                 if (nmp->nm_clp != NULL)
1936                         clidrev = nmp->nm_clp->nfsc_clientidrev;
1937                 else
1938                         clidrev = 0;
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);
1944                 else
1945                         error = nfsrpc_getcreatelayout(dvp, name, namelen, vap,
1946                           cverf, fmode, owp, &dp, cred, p, dnap, nnap, nfhpp,
1947                           attrflagp, dattrflagp, dstuff, &unlocked);
1948                 /*
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
1952                  * attribute cache.
1953                  */
1954                 if (dp != NULL)
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);
1965                         retrycnt++;
1966                 }
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)
1973                     error = EIO;
1974         } else {
1975                 error = nfsrpc_createv23(dvp, name, namelen, vap, cverf,
1976                     fmode, cred, p, dnap, nnap, nfhpp, attrflagp, dattrflagp,
1977                     dstuff);
1978         }
1979         return (error);
1980 }
1981
1982 /*
1983  * The create rpc for v2 and 3.
1984  */
1985 static int
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)
1990 {
1991         u_int32_t *tl;
1992         int error = 0;
1993         struct nfsrv_descript nfsd, *nd = &nfsd;
1994
1995         *nfhpp = NULL;
1996         *attrflagp = 0;
1997         *dattrflagp = 0;
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];
2009                 } else {
2010                         *tl = txdr_unsigned(NFSCREATE_UNCHECKED);
2011                         nfscl_fillsattr(nd, vap, dvp, 0, 0);
2012                 }
2013         } else {
2014                 nfscl_fillsattr(nd, vap, dvp, NFSSATTR_SIZE0, 0);
2015         }
2016         error = nfscl_request(nd, dvp, p, cred, dstuff);
2017         if (error)
2018                 return (error);
2019         if (nd->nd_repstat == 0) {
2020                 error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp);
2021                 if (error)
2022                         goto nfsmout;
2023         }
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;
2028 nfsmout:
2029         mbuf_freem(nd->nd_mrep);
2030         return (error);
2031 }
2032
2033 static int
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)
2039 {
2040         u_int32_t *tl;
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;
2045         struct nfsnode *np;
2046         struct nfsfh *nfhp;
2047         nfsattrbit_t attrbits;
2048         nfsv4stateid_t stateid;
2049         u_int32_t rflags;
2050         struct nfsmount *nmp;
2051         struct nfsclsession *tsep;
2052
2053         nmp = VFSTONFS(dvp->v_mount);
2054         np = VTONFS(dvp);
2055         *unlockedp = 0;
2056         *nfhpp = NULL;
2057         *dpp = NULL;
2058         *attrflagp = 0;
2059         *dattrflagp = 0;
2060         if (namelen > NFS_MAXNAMLEN)
2061                 return (ENAMETOOLONG);
2062         NFSCL_REQSTART(nd, NFSPROC_CREATE, dvp);
2063         /*
2064          * For V4, this is actually an Open op.
2065          */
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);
2083                         } else {
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);
2090                         }
2091                 } else {
2092                         /* NFSv4.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];
2097                 }
2098         } else {
2099                 *tl = txdr_unsigned(NFSCREATE_UNCHECKED);
2100                 nfscl_fillsattr(nd, vap, dvp, 0, 0);
2101         }
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);
2119         if (error)
2120                 return (error);
2121         NFSCL_INCRSEQID(owp->nfsow_seqid, nd);
2122         if (nd->nd_repstat == 0) {
2123                 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
2124                     6 * NFSX_UNSIGNED);
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 +
2148                             NFSX_UNSIGNED);
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;
2156                                 /*
2157                                  * Indicates how much the file can grow.
2158                                  */
2159                                 NFSM_DISSECT(tl, u_int32_t *,
2160                                     3 * NFSX_UNSIGNED);
2161                                 limitby = fxdr_unsigned(int, *tl++);
2162                                 switch (limitby) {
2163                                 case NFSV4OPEN_LIMITSIZE:
2164                                         dp->nfsdl_sizelimit = fxdr_hyper(tl);
2165                                         break;
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);
2171                                         break;
2172                                 default:
2173                                         error = NFSERR_BADXDR;
2174                                         goto nfsmout;
2175                                 }
2176                         } else {
2177                                 dp->nfsdl_flags = NFSCLDL_READ;
2178                         }
2179                         if (ret)
2180                                 dp->nfsdl_flags |= NFSCLDL_RECALL;
2181                         error = nfsrv_dissectace(nd, &dp->nfsdl_ace, &ret,
2182                             &acesize, p);
2183                         if (error)
2184                                 goto nfsmout;
2185                 } else if (deleg != NFSV4OPEN_DELEGATENONE) {
2186                         error = NFSERR_BADXDR;
2187                         goto nfsmout;
2188                 }
2189                 error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp);
2190                 if (error)
2191                         goto nfsmout;
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);
2196                 if (error)
2197                         goto nfsmout;
2198                 *dattrflagp = 1;
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;
2203                 }
2204                 /*
2205                  * We can now complete the Open state.
2206                  */
2207                 nfhp = *nfhpp;
2208                 if (dp != NULL) {
2209                         dp->nfsdl_fhlen = nfhp->nfh_len;
2210                         NFSBCOPY(nfhp->nfh_fh, dp->nfsdl_fh, nfhp->nfh_len);
2211                 }
2212                 /*
2213                  * Get an Open structure that will be
2214                  * attached to the OpenOwner, acquired already.
2215                  */
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);
2219                 if (error)
2220                         goto nfsmout;
2221                 op->nfso_stateid = stateid;
2222                 newnfs_copyincred(cred, &op->nfso_cred);
2223                 if ((rflags & NFSV4OPEN_RESULTCONFIRM)) {
2224                     do {
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);
2230                     error = ret;
2231                 }
2232
2233                 /*
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.
2238                  */
2239                 if ((rflags & NFSV4OPEN_RESULTCONFIRM) &&
2240                     (owp->nfsow_clp->nfsc_flags & NFSCLFLAGS_GOTDELEG) &&
2241                     !error && dp == NULL) {
2242                     do {
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);
2251                     if (ret) {
2252                         if (dp != NULL) {
2253                                 FREE((caddr_t)dp, M_NFSCLDELEG);
2254                                 dp = NULL;
2255                         }
2256                         if (ret == NFSERR_STALECLIENTID ||
2257                             ret == NFSERR_STALEDONTRECOVER ||
2258                             ret == NFSERR_BADSESSION)
2259                                 error = ret;
2260                     }
2261                 }
2262                 nfscl_openrelease(nmp, op, error, newone);
2263                 *unlockedp = 1;
2264         }
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);
2269 nfsmout:
2270         if (!error)
2271                 *dpp = dp;
2272         else if (dp != NULL)
2273                 FREE((caddr_t)dp, M_NFSCLDELEG);
2274         mbuf_freem(nd->nd_mrep);
2275         return (error);
2276 }
2277
2278 /*
2279  * Nfs remove rpc
2280  */
2281 APPLESTATIC int
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,
2284     void *dstuff)
2285 {
2286         u_int32_t *tl;
2287         struct nfsrv_descript nfsd, *nd = &nfsd;
2288         struct nfsnode *np;
2289         struct nfsmount *nmp;
2290         nfsv4stateid_t dstateid;
2291         int error, ret = 0, i;
2292
2293         *dattrflagp = 0;
2294         if (namelen > NFS_MAXNAMLEN)
2295                 return (ENAMETOOLONG);
2296         nmp = VFSTONFS(vnode_mount(dvp));
2297 tryagain:
2298         if (NFSHASNFSV4(nmp) && ret == 0) {
2299                 ret = nfscl_removedeleg(vp, p, &dstateid);
2300                 if (ret == 1) {
2301                         NFSCL_REQSTART(nd, NFSPROC_RETDELEGREMOVE, vp);
2302                         NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID +
2303                             NFSX_UNSIGNED);
2304                         if (NFSHASNFSV4N(nmp))
2305                                 *tl++ = 0;
2306                         else
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);
2312                         np = VTONFS(dvp);
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);
2317                 }
2318         } else {
2319                 ret = 0;
2320         }
2321         if (ret == 0)
2322                 NFSCL_REQSTART(nd, NFSPROC_REMOVE, dvp);
2323         (void) nfsm_strtom(nd, name, namelen);
2324         error = nfscl_request(nd, dvp, p, cred, dstuff);
2325         if (error)
2326                 return (error);
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)) {
2331                         /*
2332                          * If the Delegreturn failed, try again without
2333                          * it. The server will Recall, as required.
2334                          */
2335                         mbuf_freem(nd->nd_mrep);
2336                         goto tryagain;
2337                 }
2338                 for (i = 0; i < (ret * 2); i++) {
2339                         if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) ==
2340                             ND_NFSV4) {
2341                             NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2342                             if (*(tl + 1))
2343                                 nd->nd_flag |= ND_NOMOREDATA;
2344                         }
2345                 }
2346                 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
2347         }
2348         if (nd->nd_repstat && !error)
2349                 error = nd->nd_repstat;
2350 nfsmout:
2351         mbuf_freem(nd->nd_mrep);
2352         return (error);
2353 }
2354
2355 /*
2356  * Do an nfs rename rpc.
2357  */
2358 APPLESTATIC int
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)
2363 {
2364         u_int32_t *tl;
2365         struct nfsrv_descript nfsd, *nd = &nfsd;
2366         struct nfsmount *nmp;
2367         struct nfsnode *np;
2368         nfsattrbit_t attrbits;
2369         nfsv4stateid_t fdstateid, tdstateid;
2370         int error = 0, ret = 0, gottd = 0, gotfd = 0, i;
2371         
2372         *fattrflagp = 0;
2373         *tattrflagp = 0;
2374         nmp = VFSTONFS(vnode_mount(fdvp));
2375         if (fnamelen > NFS_MAXNAMLEN || tnamelen > NFS_MAXNAMLEN)
2376                 return (ENAMETOOLONG);
2377 tryagain:
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);
2383                 } else if (gotfd) {
2384                         NFSCL_REQSTART(nd, NFSPROC_RETDELEGRENAME1, fvp);
2385                 } else if (gottd) {
2386                         NFSCL_REQSTART(nd, NFSPROC_RETDELEGRENAME1, tvp);
2387                 }
2388                 if (gotfd) {
2389                         NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID);
2390                         if (NFSHASNFSV4N(nmp))
2391                                 *tl++ = 0;
2392                         else
2393                                 *tl++ = fdstateid.seqid;
2394                         *tl++ = fdstateid.other[0];
2395                         *tl++ = fdstateid.other[1];
2396                         *tl = fdstateid.other[2];
2397                         if (gottd) {
2398                                 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2399                                 *tl = txdr_unsigned(NFSV4OP_PUTFH);
2400                                 np = VTONFS(tvp);
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);
2405                         }
2406                 }
2407                 if (gottd) {
2408                         NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID);
2409                         if (NFSHASNFSV4N(nmp))
2410                                 *tl++ = 0;
2411                         else
2412                                 *tl++ = tdstateid.seqid;
2413                         *tl++ = tdstateid.other[0];
2414                         *tl++ = tdstateid.other[1];
2415                         *tl = tdstateid.other[2];
2416                 }
2417                 if (ret > 0) {
2418                         NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2419                         *tl = txdr_unsigned(NFSV4OP_PUTFH);
2420                         np = VTONFS(fdvp);
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);
2425                 }
2426         } else {
2427                 ret = 0;
2428         }
2429         if (ret == 0)
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);
2446         }
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);
2453         if (error)
2454                 return (error);
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)) {
2459                         /*
2460                          * If the Delegreturn failed, try again without
2461                          * it. The server will Recall, as required.
2462                          */
2463                         mbuf_freem(nd->nd_mrep);
2464                         goto tryagain;
2465                 }
2466                 for (i = 0; i < (ret * 2); i++) {
2467                         if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) ==
2468                             ND_NFSV4) {
2469                             NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2470                             if (*(tl + 1)) {
2471                                 if (i == 0 && ret > 1) {
2472                                     /*
2473                                      * If the Delegreturn failed, try again
2474                                      * without it. The server will Recall, as
2475                                      * required.
2476                                      * If ret > 1, the first iteration of this
2477                                      * loop is the second DelegReturn result.
2478                                      */
2479                                     mbuf_freem(nd->nd_mrep);
2480                                     goto tryagain;
2481                                 } else {
2482                                     nd->nd_flag |= ND_NOMOREDATA;
2483                                 }
2484                             }
2485                         }
2486                 }
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);
2490                         if (*(tl + 1))
2491                                 nd->nd_flag |= ND_NOMOREDATA;
2492                 }
2493                 error = nfscl_wcc_data(nd, fdvp, fnap, fattrflagp, NULL,
2494                     fstuff);
2495                 /* and the second wcc attribute reply. */
2496                 if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) == ND_NFSV4 &&
2497                     !error) {
2498                         NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2499                         if (*(tl + 1))
2500                                 nd->nd_flag |= ND_NOMOREDATA;
2501                 }
2502                 if (!error)
2503                         error = nfscl_wcc_data(nd, tdvp, tnap, tattrflagp,
2504                             NULL, tstuff);
2505         }
2506         if (nd->nd_repstat && !error)
2507                 error = nd->nd_repstat;
2508 nfsmout:
2509         mbuf_freem(nd->nd_mrep);
2510         return (error);
2511 }
2512
2513 /*
2514  * nfs hard link create rpc
2515  */
2516 APPLESTATIC int
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)
2520 {
2521         u_int32_t *tl;
2522         struct nfsrv_descript nfsd, *nd = &nfsd;
2523         nfsattrbit_t attrbits;
2524         int error = 0;
2525
2526         *attrflagp = 0;
2527         *dattrflagp = 0;
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);
2534         }
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);
2545         }
2546         (void) nfsm_strtom(nd, name, namelen);
2547         error = nfscl_request(nd, vp, p, cred, dstuff);
2548         if (error)
2549                 return (error);
2550         if (nd->nd_flag & ND_NFSV3) {
2551                 error = nfscl_postop_attr(nd, nap, attrflagp, dstuff);
2552                 if (!error)
2553                         error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp,
2554                             NULL, dstuff);
2555         } else if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) == ND_NFSV4) {
2556                 /*
2557                  * First, parse out the PutFH and Getattr result.
2558                  */
2559                 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2560                 if (!(*(tl + 1)))
2561                         NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2562                 if (*(tl + 1))
2563                         nd->nd_flag |= ND_NOMOREDATA;
2564                 /*
2565                  * Get the pre-op attributes.
2566                  */
2567                 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
2568         }
2569         if (nd->nd_repstat && !error)
2570                 error = nd->nd_repstat;
2571 nfsmout:
2572         mbuf_freem(nd->nd_mrep);
2573         return (error);
2574 }
2575
2576 /*
2577  * nfs symbolic link create rpc
2578  */
2579 APPLESTATIC int
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)
2584 {
2585         u_int32_t *tl;
2586         struct nfsrv_descript nfsd, *nd = &nfsd;
2587         struct nfsmount *nmp;
2588         int slen, error = 0;
2589
2590         *nfhpp = NULL;
2591         *attrflagp = 0;
2592         *dattrflagp = 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);
2602         }
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);
2611         if (error)
2612                 return (error);
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);
2618                 if (!error)
2619                         error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp,
2620                             NULL, dstuff);
2621         }
2622         if (nd->nd_repstat && !error)
2623                 error = nd->nd_repstat;
2624         mbuf_freem(nd->nd_mrep);
2625         /*
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.
2630          */
2631         if (error == EEXIST && nfsignore_eexist != 0 && (!NFSHASNFSV4(nmp) ||
2632             nmp->nm_minorvers == 0))
2633                 error = 0;
2634         return (error);
2635 }
2636
2637 /*
2638  * nfs make dir rpc
2639  */
2640 APPLESTATIC int
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)
2645 {
2646         u_int32_t *tl;
2647         struct nfsrv_descript nfsd, *nd = &nfsd;
2648         nfsattrbit_t attrbits;
2649         int error = 0;
2650         struct nfsfh *fhp;
2651         struct nfsmount *nmp;
2652
2653         *nfhpp = NULL;
2654         *attrflagp = 0;
2655         *dattrflagp = 0;
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);
2664         }
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);
2679         }
2680         error = nfscl_request(nd, dvp, p, cred, dstuff);
2681         if (error)
2682                 return (error);
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);
2689                 }
2690                 if (!error)
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);
2697                         if (error == 0)
2698                                 *dattrflagp = 1;
2699                 }
2700         }
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;
2705 nfsmout:
2706         mbuf_freem(nd->nd_mrep);
2707         /*
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.
2712          */
2713         if (error == EEXIST && nfsignore_eexist != 0 && (!NFSHASNFSV4(nmp) ||
2714             nmp->nm_minorvers == 0))
2715                 error = 0;
2716         return (error);
2717 }
2718
2719 /*
2720  * nfs remove directory call
2721  */
2722 APPLESTATIC int
2723 nfsrpc_rmdir(vnode_t dvp, char *name, int namelen, struct ucred *cred,
2724     NFSPROC_T *p, struct nfsvattr *dnap, int *dattrflagp, void *dstuff)
2725 {
2726         struct nfsrv_descript nfsd, *nd = &nfsd;
2727         int error = 0;
2728
2729         *dattrflagp = 0;
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);
2735         if (error)
2736                 return (error);
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);
2742         /*
2743          * Kludge: Map ENOENT => 0 assuming that you have a reply to a retry.
2744          */
2745         if (error == ENOENT)
2746                 error = 0;
2747         return (error);
2748 }
2749
2750 /*
2751  * Readdir rpc.
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
2754  * filled in.
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
2776  * there, as well.
2777  */
2778 APPLESTATIC int
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)
2782 {
2783         int len, left;
2784         struct dirent *dp = NULL;
2785         u_int32_t *tl;
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;
2794         char *cp;
2795         nfsattrbit_t attrbits, dattrbits;
2796         u_int32_t rderr, *tl2 = NULL;
2797         size_t tresid;
2798
2799         KASSERT(uiop->uio_iovcnt == 1 &&
2800             (uio_uio_resid(uiop) & (DIRBLKSIZ - 1)) == 0,
2801             ("nfs readdirrpc bad uio"));
2802
2803         /*
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.
2808          */
2809         readsize = nmp->nm_readdirsize;
2810         if (readsize > uio_uio_resid(uiop))
2811                 readsize = uio_uio_resid(uiop) + DIRBLKSIZ;
2812
2813         *attrflagp = 0;
2814         if (eofp)
2815                 *eofp = 0;
2816         tresid = uio_uio_resid(uiop);
2817         cookie.lval[0] = cookiep->nfsuquad[0];
2818         cookie.lval[1] = cookiep->nfsuquad[1];
2819         nd->nd_mrep = NULL;
2820
2821         /*
2822          * For NFSv4, first create the "." and ".." entries.
2823          */
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);
2834                         gotmnton = 1;
2835                 } else {
2836                         /*
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.
2840                          */
2841                         NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_FSID);
2842                         gotmnton = 0;
2843                 }
2844
2845                 /*
2846                  * Joy, oh joy. For V4 we get to hand craft '.' and '..'.
2847                  */
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);
2855                         if (error)
2856                             return (error);
2857                         dotfileid = 0;  /* Fake out the compiler. */
2858                         if ((nd->nd_flag & ND_NOMOREDATA) == 0) {
2859                             error = nfsm_loadattr(nd, &nfsva);
2860                             if (error != 0)
2861                                 goto nfsmout;
2862                             dotfileid = nfsva.na_fileid;
2863                         }
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);
2869                             else
2870                                 error = EPERM;
2871                             if (!error) {
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);
2877                                 if (error) {
2878                                     dotdotfileid = dotfileid;
2879                                 } else if (gotmnton) {
2880                                     if (nfsva.na_mntonfileno != UINT64_MAX)
2881                                         dotdotfileid = nfsva.na_mntonfileno;
2882                                     else
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;
2889                                 } else {
2890                                     do {
2891                                         fakefileno--;
2892                                     } while (fakefileno ==
2893                                         nfsva.na_fileid);
2894                                     dotdotfileid = fakefileno;
2895                                 }
2896                             }
2897                         } else if (nd->nd_repstat == NFSERR_NOENT) {
2898                             /*
2899                              * Lookupp returns NFSERR_NOENT when we are
2900                              * at the root, so just use the current dir.
2901                              */
2902                             nd->nd_repstat = 0;
2903                             dotdotfileid = dotfileid;
2904                         } else {
2905                             error = nd->nd_repstat;
2906                         }
2907                         mbuf_freem(nd->nd_mrep);
2908                         if (error)
2909                             return (error);
2910                         nd->nd_mrep = NULL;
2911                         dp = (struct dirent *)uio_iov_base(uiop);
2912                         dp->d_off = 0;
2913                         dp->d_type = DT_DIR;
2914                         dp->d_fileno = dotfileid;
2915                         dp->d_namlen = 1;
2916                         *((uint64_t *)dp->d_name) = 0;  /* Zero pad it. */
2917                         dp->d_name[0] = '.';
2918                         dp->d_reclen = _GENERIC_DIRSIZ(dp) + NFSX_HYPER;
2919                         /*
2920                          * Just make these offset cookie 0.
2921                          */
2922                         tl = (u_int32_t *)&dp->d_name[8];
2923                         *tl++ = 0;
2924                         *tl = 0;
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);
2931                         dp->d_off = 0;
2932                         dp->d_type = DT_DIR;
2933                         dp->d_fileno = dotdotfileid;
2934                         dp->d_namlen = 2;
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;
2939                         /*
2940                          * Just make these offset cookie 0.
2941                          */
2942                         tl = (u_int32_t *)&dp->d_name[8];
2943                         *tl++ = 0;
2944                         *tl = 0;
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));
2950                 }
2951                 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_RDATTRERROR);
2952         } else {
2953                 reqsize = 5 * NFSX_UNSIGNED;
2954         }
2955
2956
2957         /*
2958          * Loop around doing readdir rpc's of size readsize.
2959          * The stopping criteria is EOF or buffer full.
2960          */
2961         while (more_dirs && bigenough) {
2962                 *attrflagp = 0;
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);
2968                 } else {
2969                         NFSM_BUILD(tl, u_int32_t *, reqsize);
2970                         *tl++ = cookie.lval[0];
2971                         *tl++ = cookie.lval[1];
2972                         if (cookie.qval == 0) {
2973                                 *tl++ = 0;
2974                                 *tl++ = 0;
2975                         } else {
2976                                 NFSLOCKNODE(dnp);
2977                                 *tl++ = dnp->n_cookieverf.nfsuquad[0];
2978                                 *tl++ = dnp->n_cookieverf.nfsuquad[1];
2979                                 NFSUNLOCKNODE(dnp);
2980                         }
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);
2988                         } else {
2989                                 *tl = txdr_unsigned(readsize);
2990                         }
2991                 }
2992                 error = nfscl_request(nd, vp, p, cred, stuff);
2993                 if (error)
2994                         return (error);
2995                 if (!(nd->nd_flag & ND_NFSV2)) {
2996                         if (nd->nd_flag & ND_NFSV3)
2997                                 error = nfscl_postop_attr(nd, nap, attrflagp,
2998                                     stuff);
2999                         if (!nd->nd_repstat && !error) {
3000                                 NFSM_DISSECT(tl, u_int32_t *, NFSX_HYPER);
3001                                 NFSLOCKNODE(dnp);
3002                                 dnp->n_cookieverf.nfsuquad[0] = *tl++;
3003                                 dnp->n_cookieverf.nfsuquad[1] = *tl;
3004                                 NFSUNLOCKNODE(dnp);
3005                         }
3006                 }
3007                 if (nd->nd_repstat || error) {
3008                         if (!error)
3009                                 error = nd->nd_repstat;
3010                         goto nfsmout;
3011                 }
3012                 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3013                 more_dirs = fxdr_unsigned(int, *tl);
3014                 if (!more_dirs)
3015                         tryformoredirs = 0;
3016         
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);
3027                                 tl += 2;
3028                                 len = fxdr_unsigned(int, *tl);
3029                         } else {
3030                                 NFSM_DISSECT(tl, u_int32_t *, 2*NFSX_UNSIGNED);
3031                                 nfsva.na_fileid = fxdr_unsigned(uint64_t,
3032                                     *tl++);
3033                                 len = fxdr_unsigned(int, *tl);
3034                         }
3035                         if (len <= 0 || len > NFS_MAXNAMLEN) {
3036                                 error = EBADRPC;
3037                                 goto nfsmout;
3038                         }
3039                         tlen = roundup2(len, 8);
3040                         if (tlen == len)
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;
3049                                 blksiz = 0;
3050                         }
3051                         if (_GENERIC_DIRLEN(len) + NFSX_HYPER >
3052                             uio_uio_resid(uiop))
3053                                 bigenough = 0;
3054                         if (bigenough) {
3055                                 dp = (struct dirent *)uio_iov_base(uiop);
3056                                 dp->d_off = 0;
3057                                 dp->d_namlen = len;
3058                                 dp->d_reclen = _GENERIC_DIRLEN(len) +
3059                                     NFSX_HYPER;
3060                                 dp->d_type = DT_UNKNOWN;
3061                                 blksiz += dp->d_reclen;
3062                                 if (blksiz == DIRBLKSIZ)
3063                                         blksiz = 0;
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);
3069                                 if (error)
3070                                         goto nfsmout;
3071                                 cp = uio_iov_base(uiop);
3072                                 tlen -= len;
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);
3080                         } else {
3081                                 error = nfsm_advance(nd, NFSM_RNDUP(len), -1);
3082                                 if (error)
3083                                         goto nfsmout;
3084                         }
3085                         if (nd->nd_flag & ND_NFSV4) {
3086                                 rderr = 0;
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);
3091                                 if (error)
3092                                         goto nfsmout;
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++;
3098                         } else {
3099                                 NFSM_DISSECT(tl, u_int32_t *, 2*NFSX_UNSIGNED);
3100                                 ncookie.lval[0] = 0;
3101                                 ncookie.lval[1] = *tl++;
3102                         }
3103                         if (bigenough) {
3104                             if (nd->nd_flag & ND_NFSV4) {
3105                                 if (rderr) {
3106                                     dp->d_fileno = 0;
3107                                 } else {
3108                                     if (gotmnton) {
3109                                         if (nfsva.na_mntonfileno != UINT64_MAX)
3110                                             dp->d_fileno = nfsva.na_mntonfileno;
3111                                         else
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;
3118                                     } else {
3119                                         do {
3120                                             fakefileno--;
3121                                         } while (fakefileno ==
3122                                             nfsva.na_fileid);
3123                                         dp->d_fileno = fakefileno;
3124                                     }
3125                                     dp->d_type = vtonfs_dtype(nfsva.na_type);
3126                                 }
3127                             } else {
3128                                 dp->d_fileno = nfsva.na_fileid;
3129                             }
3130                             *tl2++ = cookiep->nfsuquad[0] = cookie.lval[0] =
3131                                 ncookie.lval[0];
3132                             *tl2 = cookiep->nfsuquad[1] = cookie.lval[1] =
3133                                 ncookie.lval[1];
3134                         }
3135                         more_dirs = fxdr_unsigned(int, *tl);
3136                 }
3137                 /*
3138                  * If at end of rpc data, get the eof boolean
3139                  */
3140                 if (!more_dirs) {
3141                         NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3142                         eof = fxdr_unsigned(int, *tl);
3143                         if (tryformoredirs)
3144                                 more_dirs = !eof;
3145                         if (nd->nd_flag & ND_NFSV4) {
3146                                 error = nfscl_postop_attr(nd, nap, attrflagp,
3147                                     stuff);
3148                                 if (error)
3149                                         goto nfsmout;
3150                         }
3151                 }
3152                 mbuf_freem(nd->nd_mrep);
3153                 nd->nd_mrep = NULL;
3154         }
3155         /*
3156          * Fill last record, iff any, out to a multiple of DIRBLKSIZ
3157          * by increasing d_reclen for the last record.
3158          */
3159         if (blksiz > 0) {
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;
3166         }
3167
3168         /*
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.
3173          */
3174         if (eofp) {
3175                 if (tresid == ((size_t)(uio_uio_resid(uiop))))
3176                         *eofp = 1;
3177                 else if (!bigenough)
3178                         *eofp = 0;
3179                 else
3180                         *eofp = eof;
3181         }
3182
3183         /*
3184          * Add extra empty records to any remaining DIRBLKSIZ chunks.
3185          */
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;
3189                 dp->d_fileno = 0;
3190                 dp->d_namlen = 0;
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;
3200         }
3201
3202 nfsmout:
3203         if (nd->nd_mrep != NULL)
3204                 mbuf_freem(nd->nd_mrep);
3205         return (error);
3206 }
3207
3208 #ifndef APPLE
3209 /*
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.)
3213  */
3214 APPLESTATIC int
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)
3218 {
3219         int len, left;
3220         struct dirent *dp = NULL;
3221         u_int32_t *tl;
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;
3229         struct nfsfh *nfhp;
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;
3236         char *cp;
3237         nfsattrbit_t attrbits, dattrbits;
3238         size_t tresid;
3239         u_int32_t *tl2 = NULL, rderr;
3240         struct timespec dctime;
3241
3242         KASSERT(uiop->uio_iovcnt == 1 &&
3243             (uio_uio_resid(uiop) & (DIRBLKSIZ - 1)) == 0,
3244             ("nfs readdirplusrpc bad uio"));
3245         timespecclear(&dctime);
3246         *attrflagp = 0;
3247         if (eofp != NULL)
3248                 *eofp = 0;
3249         ndp->ni_dvp = vp;
3250         nd->nd_mrep = NULL;
3251         cookie.lval[0] = cookiep->nfsuquad[0];
3252         cookie.lval[1] = cookiep->nfsuquad[1];
3253         tresid = uio_uio_resid(uiop);
3254
3255         /*
3256          * For NFSv4, first create the "." and ".." entries.
3257          */
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);
3266                         gotmnton = 1;
3267                 } else {
3268                         /*
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.
3272                          */
3273                         NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_FSID);
3274                         gotmnton = 0;
3275                 }
3276
3277                 /*
3278                  * Joy, oh joy. For V4 we get to hand craft '.' and '..'.
3279                  */
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);
3287                         if (error)
3288                             return (error);
3289                         dotfileid = 0;  /* Fake out the compiler. */
3290                         if ((nd->nd_flag & ND_NOMOREDATA) == 0) {
3291                             error = nfsm_loadattr(nd, &nfsva);
3292                             if (error != 0)
3293                                 goto nfsmout;
3294                             dctime = nfsva.na_ctime;
3295                             dotfileid = nfsva.na_fileid;
3296                         }
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);
3302                             else
3303                                 error = EPERM;
3304                             if (!error) {
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);
3310                                 if (error) {
3311                                     dotdotfileid = dotfileid;
3312                                 } else if (gotmnton) {
3313                                     if (nfsva.na_mntonfileno != UINT64_MAX)
3314                                         dotdotfileid = nfsva.na_mntonfileno;
3315                                     else
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;
3322                                 } else {
3323                                     do {
3324                                         fakefileno--;
3325                                     } while (fakefileno ==
3326                                         nfsva.na_fileid);
3327                                     dotdotfileid = fakefileno;
3328                                 }
3329                             }
3330                         } else if (nd->nd_repstat == NFSERR_NOENT) {
3331                             /*
3332                              * Lookupp returns NFSERR_NOENT when we are
3333                              * at the root, so just use the current dir.
3334                              */
3335                             nd->nd_repstat = 0;
3336                             dotdotfileid = dotfileid;
3337                         } else {
3338                             error = nd->nd_repstat;
3339                         }
3340                         mbuf_freem(nd->nd_mrep);
3341                         if (error)
3342                             return (error);
3343                         nd->nd_mrep = NULL;
3344                         dp = (struct dirent *)uio_iov_base(uiop);
3345                         dp->d_off = 0;
3346                         dp->d_type = DT_DIR;
3347                         dp->d_fileno = dotfileid;
3348                         dp->d_namlen = 1;
3349                         *((uint64_t *)dp->d_name) = 0;  /* Zero pad it. */
3350                         dp->d_name[0] = '.';
3351                         dp->d_reclen = _GENERIC_DIRSIZ(dp) + NFSX_HYPER;
3352                         /*
3353                          * Just make these offset cookie 0.
3354                          */
3355                         tl = (u_int32_t *)&dp->d_name[8];
3356                         *tl++ = 0;
3357                         *tl = 0;
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);
3364                         dp->d_off = 0;
3365                         dp->d_type = DT_DIR;
3366                         dp->d_fileno = dotdotfileid;
3367                         dp->d_namlen = 2;
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;
3372                         /*
3373                          * Just make these offset cookie 0.
3374                          */
3375                         tl = (u_int32_t *)&dp->d_name[8];
3376                         *tl++ = 0;
3377                         *tl = 0;
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));
3383                 }
3384                 NFSREADDIRPLUS_ATTRBIT(&attrbits);
3385                 if (gotmnton)
3386                         NFSSETBIT_ATTRBIT(&attrbits,
3387                             NFSATTRBIT_MOUNTEDONFILEID);
3388         }
3389
3390         /*
3391          * Loop around doing readdir rpc's of size nm_readdirsize.
3392          * The stopping criteria is EOF or buffer full.
3393          */
3394         while (more_dirs && bigenough) {
3395                 *attrflagp = 0;
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) {
3401                         *tl++ = 0;
3402                         *tl++ = 0;
3403                 } else {
3404                         NFSLOCKNODE(dnp);
3405                         *tl++ = dnp->n_cookieverf.nfsuquad[0];
3406                         *tl++ = dnp->n_cookieverf.nfsuquad[1];
3407                         NFSUNLOCKNODE(dnp);
3408                 }
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);
3416                 }
3417                 error = nfscl_request(nd, vp, p, cred, stuff);
3418                 if (error)
3419                         return (error);
3420                 if (nd->nd_flag & ND_NFSV3)
3421                         error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
3422                 if (nd->nd_repstat || error) {
3423                         if (!error)
3424                                 error = nd->nd_repstat;
3425                         goto nfsmout;
3426                 }
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);
3430                 NFSLOCKNODE(dnp);
3431                 dnp->n_cookieverf.nfsuquad[0] = *tl++;
3432                 dnp->n_cookieverf.nfsuquad[1] = *tl++;
3433                 NFSUNLOCKNODE(dnp);
3434                 more_dirs = fxdr_unsigned(int, *tl);
3435                 if (!more_dirs)
3436                         tryformoredirs = 0;
3437         
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++;
3444                         } else {
3445                                 fileno = fxdr_hyper(tl);
3446                                 tl += 2;
3447                         }
3448                         len = fxdr_unsigned(int, *tl);
3449                         if (len <= 0 || len > NFS_MAXNAMLEN) {
3450                                 error = EBADRPC;
3451                                 goto nfsmout;
3452                         }
3453                         tlen = roundup2(len, 8);
3454                         if (tlen == len)
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;
3463                                 blksiz = 0;
3464                         }
3465                         if (_GENERIC_DIRLEN(len) + NFSX_HYPER >
3466                             uio_uio_resid(uiop))
3467                                 bigenough = 0;
3468                         if (bigenough) {
3469                                 dp = (struct dirent *)uio_iov_base(uiop);
3470                                 dp->d_off = 0;
3471                                 dp->d_namlen = len;
3472                                 dp->d_reclen = _GENERIC_DIRLEN(len) +
3473                                     NFSX_HYPER;
3474                                 dp->d_type = DT_UNKNOWN;
3475                                 blksiz += dp->d_reclen;
3476                                 if (blksiz == DIRBLKSIZ)
3477                                         blksiz = 0;
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;
3484                                 NFSCNHASHZERO(cnp);
3485                                 error = nfsm_mbufuio(nd, uiop, len);
3486                                 if (error)
3487                                         goto nfsmout;
3488                                 cp = uio_iov_base(uiop);
3489                                 tlen -= len;
3490                                 *cp = '\0';
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] == '.')
3495                                         isdotdot = 1;
3496                                 else
3497                                         isdotdot = 0;
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);
3502                         } else {
3503                                 error = nfsm_advance(nd, NFSM_RNDUP(len), -1);
3504                                 if (error)
3505                                         goto nfsmout;
3506                         }
3507                         nfhp = NULL;
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);
3513                                 if (attrflag) {
3514                                   error = nfsm_loadattr(nd, &nfsva);
3515                                   if (error)
3516                                         goto nfsmout;
3517                                 }
3518                                 NFSM_DISSECT(tl,u_int32_t *,NFSX_UNSIGNED);
3519                                 if (*tl) {
3520                                         error = nfsm_getfh(nd, &nfhp);
3521                                         if (error)
3522                                             goto nfsmout;
3523                                 }
3524                                 if (!attrflag && nfhp != NULL) {
3525                                         FREE((caddr_t)nfhp, M_NFSFH);
3526                                         nfhp = NULL;
3527                                 }
3528                         } else {
3529                                 rderr = 0;
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);
3534                                 if (error)
3535                                         goto nfsmout;
3536                         }
3537
3538                         if (bigenough) {
3539                             if (nd->nd_flag & ND_NFSV4) {
3540                                 if (rderr) {
3541                                     dp->d_fileno = 0;
3542                                 } else if (gotmnton) {
3543                                     if (nfsva.na_mntonfileno != 0xffffffff)
3544                                         dp->d_fileno = nfsva.na_mntonfileno;
3545                                     else
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;
3552                                 } else {
3553                                     do {
3554                                         fakefileno--;
3555                                     } while (fakefileno ==
3556                                         nfsva.na_fileid);
3557                                     dp->d_fileno = fakefileno;
3558                                 }
3559                             } else {
3560                                 dp->d_fileno = fileno;
3561                             }
3562                             *tl2++ = cookiep->nfsuquad[0] = cookie.lval[0] =
3563                                 ncookie.lval[0];
3564                             *tl2 = cookiep->nfsuquad[1] = cookie.lval[1] =
3565                                 ncookie.lval[1];
3566
3567                             if (nfhp != NULL) {
3568                                 if (NFSRV_CMPFH(nfhp->nfh_fh, nfhp->nfh_len,
3569                                     dnp->n_fhp->nfh_fh, dnp->n_fhp->nfh_len)) {
3570                                     VREF(vp);
3571                                     newvp = vp;
3572                                     unlocknewvp = 0;
3573                                     FREE((caddr_t)nfhp, M_NFSFH);
3574                                     np = dnp;
3575                                 } else if (isdotdot != 0) {
3576                                     /*
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
3588                                      * vp is relocked.
3589                                      */
3590                                     free(nfhp, M_NFSFH);
3591                                 } else {
3592                                     error = nfscl_nget(vnode_mount(vp), vp,
3593                                       nfhp, cnp, p, &np, NULL, LK_EXCLUSIVE);
3594                                     if (!error) {
3595                                         newvp = NFSTOV(np);
3596                                         unlocknewvp = 1;
3597                                     }
3598                                 }
3599                                 nfhp = NULL;
3600                                 if (newvp != NULLVP) {
3601                                     error = nfscl_loadattrcache(&newvp,
3602                                         &nfsva, NULL, NULL, 0, 0);
3603                                     if (error) {
3604                                         if (unlocknewvp)
3605                                             vput(newvp);
3606                                         else
3607                                             vrele(newvp);
3608                                         goto nfsmout;
3609                                     }
3610                                     dp->d_type =
3611                                         vtonfs_dtype(np->n_vattr.na_type);
3612                                     ndp->ni_vp = newvp;
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,
3618                                             ndp->ni_vp, cnp,
3619                                             &nfsva.na_ctime,
3620                                             newvp->v_type != VDIR ? NULL :
3621                                             &dctime);
3622                                     }
3623                                     if (unlocknewvp)
3624                                         vput(newvp);
3625                                     else
3626                                         vrele(newvp);
3627                                     newvp = NULLVP;
3628                                 }
3629                             }
3630                         } else if (nfhp != NULL) {
3631                             FREE((caddr_t)nfhp, M_NFSFH);
3632                         }
3633                         NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3634                         more_dirs = fxdr_unsigned(int, *tl);
3635                 }
3636                 /*
3637                  * If at end of rpc data, get the eof boolean
3638                  */
3639                 if (!more_dirs) {
3640                         NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3641                         eof = fxdr_unsigned(int, *tl);
3642                         if (tryformoredirs)
3643                                 more_dirs = !eof;
3644                         if (nd->nd_flag & ND_NFSV4) {
3645                                 error = nfscl_postop_attr(nd, nap, attrflagp,
3646                                     stuff);
3647                                 if (error)
3648                                         goto nfsmout;
3649                         }
3650                 }
3651                 mbuf_freem(nd->nd_mrep);
3652                 nd->nd_mrep = NULL;
3653         }
3654         /*
3655          * Fill last record, iff any, out to a multiple of DIRBLKSIZ
3656          * by increasing d_reclen for the last record.
3657          */
3658         if (blksiz > 0) {
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;
3665         }
3666
3667         /*
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.
3672          */
3673         if (eofp != NULL) {
3674                 if (tresid == uio_uio_resid(uiop))
3675                         *eofp = 1;
3676                 else if (!bigenough)
3677                         *eofp = 0;
3678                 else
3679                         *eofp = eof;
3680         }
3681
3682         /*
3683          * Add extra empty records to any remaining DIRBLKSIZ chunks.
3684          */
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;
3688                 dp->d_fileno = 0;
3689                 dp->d_namlen = 0;
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;
3699         }
3700
3701 nfsmout:
3702         if (nd->nd_mrep != NULL)
3703                 mbuf_freem(nd->nd_mrep);
3704         return (error);
3705 }
3706 #endif  /* !APPLE */
3707
3708 /*
3709  * Nfs commit rpc
3710  */
3711 APPLESTATIC int
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)
3714 {
3715         u_int32_t *tl;
3716         struct nfsrv_descript nfsd, *nd = &nfsd;
3717         nfsattrbit_t attrbits;
3718         int error;
3719         struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
3720         
3721         *attrflagp = 0;
3722         NFSCL_REQSTART(nd, NFSPROC_COMMIT, vp);
3723         NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
3724         txdr_hyper(offset, tl);
3725         tl += 2;
3726         *tl = txdr_unsigned(cnt);
3727         if (nd->nd_flag & ND_NFSV4) {
3728                 /*
3729                  * And do a Getattr op.
3730                  */
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);
3735         }
3736         error = nfscl_request(nd, vp, p, cred, stuff);
3737         if (error)
3738                 return (error);
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);
3742                 NFSLOCKMNT(nmp);
3743                 if (NFSBCMP(nmp->nm_verf, tl, NFSX_VERF)) {
3744                         NFSBCOPY(tl, nmp->nm_verf, NFSX_VERF);
3745                         nd->nd_repstat = NFSERR_STALEWRITEVERF;
3746                 }
3747                 NFSUNLOCKMNT(nmp);
3748                 if (nd->nd_flag & ND_NFSV4)
3749                         error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
3750         }
3751 nfsmout:
3752         if (!error && nd->nd_repstat)
3753                 error = nd->nd_repstat;
3754         mbuf_freem(nd->nd_mrep);
3755         return (error);
3756 }
3757
3758 /*
3759  * NFS byte range lock rpc.
3760  * (Mostly just calls one of the three lower level RPC routines.)
3761  */
3762 APPLESTATIC int
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)
3765 {
3766         struct nfscllockowner *lp;
3767         struct nfsclclient *clp;
3768         struct nfsfh *nfhp;
3769         struct nfsrv_descript nfsd, *nd = &nfsd;
3770         struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
3771         u_int64_t off, len;
3772         off_t start, end;
3773         u_int32_t clidrev = 0;
3774         int error = 0, newone = 0, expireret = 0, retrycnt, donelocally;
3775         int callcnt, dorpc;
3776
3777         /*
3778          * Convert the flock structure into a start and end and do POSIX
3779          * bounds checking.
3780          */
3781         switch (fl->l_whence) {
3782         case SEEK_SET:
3783         case SEEK_CUR:
3784                 /*
3785                  * Caller is responsible for adding any necessary offset
3786                  * when SEEK_CUR is used.
3787                  */
3788                 start = fl->l_start;
3789                 off = fl->l_start;
3790                 break;
3791         case SEEK_END:
3792                 start = size + fl->l_start;
3793                 off = size + fl->l_start;
3794                 break;
3795         default:
3796                 return (EINVAL);
3797         }
3798         if (start < 0)
3799                 return (EINVAL);
3800         if (fl->l_len != 0) {
3801                 end = start + fl->l_len - 1;
3802                 if (end < start)
3803                         return (EINVAL);
3804         }
3805
3806         len = fl->l_len;
3807         if (len == 0)
3808                 len = NFS64BITSSET;
3809         retrycnt = 0;
3810         do {
3811             nd->nd_repstat = 0;
3812             if (op == F_GETLK) {
3813                 error = nfscl_getcl(vnode_mount(vp), cred, p, 1, &clp);
3814                 if (error)
3815                         return (error);
3816                 error = nfscl_lockt(vp, clp, off, len, fl, p, id, flags);
3817                 if (!error) {
3818                         clidrev = clp->nfsc_clientidrev;
3819                         error = nfsrpc_lockt(nd, vp, clp, off, len, fl, cred,
3820                             p, id, flags);
3821                 } else if (error == -1) {
3822                         error = 0;
3823                 }
3824                 nfscl_clientrelease(clp);
3825             } else if (op == F_UNLCK && fl->l_type == F_UNLCK) {
3826                 /*
3827                  * We must loop around for all lockowner cases.
3828                  */
3829                 callcnt = 0;
3830                 error = nfscl_getcl(vnode_mount(vp), cred, p, 1, &clp);
3831                 if (error)
3832                         return (error);
3833                 do {
3834                     error = nfscl_relbytelock(vp, off, len, cred, p, callcnt,
3835                         clp, id, flags, &lp, &dorpc);
3836                     /*
3837                      * If it returns a NULL lp, we're done.
3838                      */
3839                     if (lp == NULL) {
3840                         if (callcnt == 0)
3841                             nfscl_clientrelease(clp);
3842                         else
3843                             nfscl_releasealllocks(clp, vp, p, id, flags);
3844                         return (error);
3845                     }
3846                     if (nmp->nm_clp != NULL)
3847                         clidrev = nmp->nm_clp->nfsc_clientidrev;
3848                     else
3849                         clidrev = 0;
3850                     /*
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?
3857                      */
3858                     if (dorpc && (lp->nfsl_open->nfso_posixlock ||
3859                         (off == 0 && len == NFS64BITSSET))) {
3860                         /*
3861                          * Since the lock records will go away, we must
3862                          * wait for grace and delay here.
3863                          */
3864                         do {
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) &&
3869                                 error == 0)
3870                                 (void) nfs_catnap(PZERO, (int)nd->nd_repstat,
3871                                     "nfs_advlock");
3872                         } while ((nd->nd_repstat == NFSERR_GRACE ||
3873                             nd->nd_repstat == NFSERR_DELAY) && error == 0);
3874                     }
3875                     callcnt++;
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) {
3882                         return (error);
3883                 }
3884                 if (nmp->nm_clp != NULL)
3885                         clidrev = nmp->nm_clp->nfsc_clientidrev;
3886                 else
3887                         clidrev = 0;
3888                 nfhp = VTONFS(vp)->n_fhp;
3889                 if (!lp->nfsl_open->nfso_posixlock &&
3890                     (off != 0 || len != NFS64BITSSET)) {
3891                         error = EINVAL;
3892                 } else {
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);
3896                 }
3897                 if (!error)
3898                         error = nd->nd_repstat;
3899                 nfscl_lockrelease(lp, error, newone);
3900             } else {
3901                 error = EINVAL;
3902             }
3903             if (!error)
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)
3911                 && clidrev != 0) {
3912                 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
3913                 retrycnt++;
3914             }
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)
3922                 error = EIO;
3923         return (error);
3924 }
3925
3926 /*
3927  * The lower level routine for the LockT case.
3928  */
3929 APPLESTATIC int
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)
3933 {
3934         u_int32_t *tl;
3935         int error, type, size;
3936         uint8_t own[NFSV4CL_LOCKNAMELEN + NFSX_V4FHMAX];
3937         struct nfsnode *np;
3938         struct nfsmount *nmp;
3939         struct nfsclsession *tsep;
3940
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);
3946         else
3947                 *tl++ = txdr_unsigned(NFSV4LOCKT_WRITE);
3948         txdr_hyper(off, tl);
3949         tl += 2;
3950         txdr_hyper(len, tl);
3951         tl += 2;
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);
3956         np = VTONFS(vp);
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);
3961         if (error)
3962                 return (error);
3963         if (nd->nd_repstat == 0) {
3964                 fl->l_type = F_UNLCK;
3965         } else if (nd->nd_repstat == NFSERR_DENIED) {
3966                 nd->nd_repstat = 0;
3967                 fl->l_whence = SEEK_SET;
3968                 NFSM_DISSECT(tl, u_int32_t *, 8 * NFSX_UNSIGNED);
3969                 fl->l_start = fxdr_hyper(tl);
3970                 tl += 2;
3971                 len = fxdr_hyper(tl);
3972                 tl += 2;
3973                 if (len == NFS64BITSSET)
3974                         fl->l_len = 0;
3975                 else
3976                         fl->l_len = len;
3977                 type = fxdr_unsigned(int, *tl++);
3978                 if (type == NFSV4LOCKT_WRITE)
3979                         fl->l_type = F_WRLCK;
3980                 else
3981                         fl->l_type = F_RDLCK;
3982                 /*
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.
3986                  */
3987                 fl->l_pid = (pid_t)0;
3988                 tl += 2;
3989                 size = fxdr_unsigned(int, *tl);
3990                 if (size < 0 || size > NFSV4_OPAQUELIMIT)
3991                         error = EBADRPC;
3992                 if (!error)
3993                         error = nfsm_advance(nd, NFSM_RNDUP(size), -1);
3994         } else if (nd->nd_repstat == NFSERR_STALECLIENTID)
3995                 nfscl_initiate_recovery(clp);
3996 nfsmout:
3997         mbuf_freem(nd->nd_mrep);
3998         return (error);
3999 }
4000
4001 /*
4002  * Lower level function that performs the LockU RPC.
4003  */
4004 static int
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)
4008 {
4009         u_int32_t *tl;
4010         int error;
4011
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);
4020         tl++;
4021         if (NFSHASNFSV4N(nmp))
4022                 *tl++ = 0;
4023         else
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);
4029         tl += 2;
4030         txdr_hyper(len, tl);
4031         if (syscred)
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);
4036         if (error)
4037                 return (error);
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);
4046 nfsmout:
4047         mbuf_freem(nd->nd_mrep);
4048         return (error);
4049 }
4050
4051 /*
4052  * The actual Lock RPC.
4053  */
4054 APPLESTATIC int
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)
4059 {
4060         u_int32_t *tl;
4061         int error, size;
4062         uint8_t own[NFSV4CL_LOCKNAMELEN + NFSX_V4FHMAX];
4063         struct nfsclsession *tsep;
4064
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);
4069         else
4070                 *tl++ = txdr_unsigned(NFSV4LOCKT_WRITE);
4071         *tl++ = txdr_unsigned(reclaim);
4072         txdr_hyper(off, tl);
4073         tl += 2;
4074         txdr_hyper(len, tl);
4075         tl += 2;
4076         if (newone) {
4077             *tl = newnfs_true;
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))
4082                 *tl++ = 0;
4083             else
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);
4095         } else {
4096             *tl = newnfs_false;
4097             NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID + NFSX_UNSIGNED);
4098             if (NFSHASNFSV4N(nmp))
4099                 *tl++ = 0;
4100             else
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);
4109         }
4110         if (syscred)
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);
4114         if (error)
4115                 return (error);
4116         if (newone)
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)
4129                         error = EBADRPC;
4130                 if (!error)
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);
4134 nfsmout:
4135         mbuf_freem(nd->nd_mrep);
4136         return (error);
4137 }
4138
4139 /*
4140  * nfs statfs rpc
4141  * (always called with the vp for the mount point)
4142  */
4143 APPLESTATIC int
4144 nfsrpc_statfs(vnode_t vp, struct nfsstatfs *sbp, struct nfsfsinfo *fsp,
4145     struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp,
4146     void *stuff)
4147 {
4148         u_int32_t *tl = NULL;
4149         struct nfsrv_descript nfsd, *nd = &nfsd;
4150         struct nfsmount *nmp;
4151         nfsattrbit_t attrbits;
4152         int error;
4153
4154         *attrflagp = 0;
4155         nmp = VFSTONFS(vnode_mount(vp));
4156         if (NFSHASNFSV4(nmp)) {
4157                 /*
4158                  * For V4, you actually do a getattr.
4159                  */
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);
4165                 if (error)
4166                         return (error);
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,
4170                             cred);
4171                         if (!error) {
4172                                 nmp->nm_fsid[0] = nap->na_filesid[0];
4173                                 nmp->nm_fsid[1] = nap->na_filesid[1];
4174                                 NFSSETHASSETFSID(nmp);
4175                                 *attrflagp = 1;
4176                         }
4177                 } else {
4178                         error = nd->nd_repstat;
4179                 }
4180                 if (error)
4181                         goto nfsmout;
4182         } else {
4183                 NFSCL_REQSTART(nd, NFSPROC_FSSTAT, vp);
4184                 error = nfscl_request(nd, vp, p, cred, stuff);
4185                 if (error)
4186                         return (error);
4187                 if (nd->nd_flag & ND_NFSV3) {
4188                         error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
4189                         if (error)
4190                                 goto nfsmout;
4191                 }
4192                 if (nd->nd_repstat) {
4193                         error = nd->nd_repstat;
4194                         goto nfsmout;
4195                 }
4196                 NFSM_DISSECT(tl, u_int32_t *,
4197                     NFSX_STATFS(nd->nd_flag & ND_NFSV3));
4198         }
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);
4213         }
4214 nfsmout:
4215         mbuf_freem(nd->nd_mrep);
4216         return (error);
4217 }
4218
4219 /*
4220  * nfs pathconf rpc
4221  */
4222 APPLESTATIC int
4223 nfsrpc_pathconf(vnode_t vp, struct nfsv3_pathconf *pc,
4224     struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp,
4225     void *stuff)
4226 {
4227         struct nfsrv_descript nfsd, *nd = &nfsd;
4228         struct nfsmount *nmp;
4229         u_int32_t *tl;
4230         nfsattrbit_t attrbits;
4231         int error;
4232
4233         *attrflagp = 0;
4234         nmp = VFSTONFS(vnode_mount(vp));
4235         if (NFSHASNFSV4(nmp)) {
4236                 /*
4237                  * For V4, you actually do a getattr.
4238                  */
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);
4244                 if (error)
4245                         return (error);
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,
4249                             cred);
4250                         if (!error)
4251                                 *attrflagp = 1;
4252                 } else {
4253                         error = nd->nd_repstat;
4254                 }
4255         } else {
4256                 NFSCL_REQSTART(nd, NFSPROC_PATHCONF, vp);
4257                 error = nfscl_request(nd, vp, p, cred, stuff);
4258                 if (error)
4259                         return (error);
4260                 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
4261                 if (nd->nd_repstat && !error)
4262                         error = nd->nd_repstat;
4263                 if (!error) {
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);
4273                 }
4274         }
4275 nfsmout:
4276         mbuf_freem(nd->nd_mrep);
4277         return (error);
4278 }
4279
4280 /*
4281  * nfs version 3 fsinfo rpc call
4282  */
4283 APPLESTATIC int
4284 nfsrpc_fsinfo(vnode_t vp, struct nfsfsinfo *fsp, struct ucred *cred,
4285     NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, void *stuff)
4286 {
4287         u_int32_t *tl;
4288         struct nfsrv_descript nfsd, *nd = &nfsd;
4289         int error;
4290
4291         *attrflagp = 0;
4292         NFSCL_REQSTART(nd, NFSPROC_FSINFO, vp);
4293         error = nfscl_request(nd, vp, p, cred, stuff);
4294         if (error)
4295                 return (error);
4296         error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
4297         if (nd->nd_repstat && !error)
4298                 error = nd->nd_repstat;
4299         if (!error) {
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);
4309                 tl += 2;
4310                 fxdr_nfsv3time(tl, &fsp->fs_timedelta);
4311                 tl += 2;
4312                 fsp->fs_properties = fxdr_unsigned(u_int32_t, *tl);
4313         }
4314 nfsmout:
4315         mbuf_freem(nd->nd_mrep);
4316         return (error);
4317 }
4318
4319 /*
4320  * This function performs the Renew RPC.
4321  */
4322 APPLESTATIC int
4323 nfsrpc_renew(struct nfsclclient *clp, struct nfsclds *dsp, struct ucred *cred,
4324     NFSPROC_T *p)
4325 {
4326         u_int32_t *tl;
4327         struct nfsrv_descript nfsd;
4328         struct nfsrv_descript *nd = &nfsd;
4329         struct nfsmount *nmp;
4330         int error;
4331         struct nfssockreq *nrp;
4332         struct nfsclsession *tsep;
4333
4334         nmp = clp->nfsc_nmp;
4335         if (nmp == NULL)
4336                 return (0);
4337         if (dsp == NULL)
4338                 nfscl_reqstart(nd, NFSPROC_RENEW, nmp, NULL, 0, NULL, NULL, 0,
4339                     0);
4340         else
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];
4349         }
4350         nrp = NULL;
4351         if (dsp != NULL)
4352                 nrp = dsp->nfsclds_sockp;
4353         if (nrp == NULL)
4354                 /* If NULL, use the MDS socket. */
4355                 nrp = &nmp->nm_sockreq;
4356         nd->nd_flag |= ND_USEGSSNAME;
4357         if (dsp == NULL)
4358                 error = newnfs_request(nd, nmp, NULL, nrp, NULL, p, cred,
4359                     NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4360         else
4361                 error = newnfs_request(nd, nmp, NULL, nrp, NULL, p, cred,
4362                     NFS_PROG, NFS_VER4, NULL, 1, NULL, &dsp->nfsclds_sess);
4363         if (error)
4364                 return (error);
4365         error = nd->nd_repstat;
4366         mbuf_freem(nd->nd_mrep);
4367         return (error);
4368 }
4369
4370 /*
4371  * This function performs the Releaselockowner RPC.
4372  */
4373 APPLESTATIC int
4374 nfsrpc_rellockown(struct nfsmount *nmp, struct nfscllockowner *lp,
4375     uint8_t *fh, int fhlen, struct ucred *cred, NFSPROC_T *p)
4376 {
4377         struct nfsrv_descript nfsd, *nd = &nfsd;
4378         u_int32_t *tl;
4379         int error;
4380         uint8_t own[NFSV4CL_LOCKNAMELEN + NFSX_V4FHMAX];
4381         struct nfsclsession *tsep;
4382
4383         if (NFSHASNFSV4N(nmp)) {
4384                 /* For NFSv4.1, do a FreeStateID. */
4385                 nfscl_reqstart(nd, NFSPROC_FREESTATEID, nmp, NULL, 0, NULL,
4386                     NULL, 0, 0);
4387                 nfsm_stateidtom(nd, &lp->nfsl_stateid, NFSSTATEID_PUTSTATEID);
4388         } else {
4389                 nfscl_reqstart(nd, NFSPROC_RELEASELCKOWN, nmp, NULL, 0, NULL,
4390                     NULL, 0, 0);
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);
4398         }
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);
4402         if (error)
4403                 return (error);
4404         error = nd->nd_repstat;
4405         mbuf_freem(nd->nd_mrep);
4406         return (error);
4407 }
4408
4409 /*
4410  * This function performs the Compound to get the mount pt FH.
4411  */
4412 APPLESTATIC int
4413 nfsrpc_getdirpath(struct nfsmount *nmp, u_char *dirpath, struct ucred *cred,
4414     NFSPROC_T *p)
4415 {
4416         u_int32_t *tl;
4417         struct nfsrv_descript nfsd;
4418         struct nfsrv_descript *nd = &nfsd;
4419         u_char *cp, *cp2;
4420         int error, cnt, len, setnil;
4421         u_int32_t *opcntp;
4422
4423         nfscl_reqstart(nd, NFSPROC_PUTROOTFH, nmp, NULL, 0, &opcntp, NULL, 0,
4424             0);
4425         cp = dirpath;
4426         cnt = 0;
4427         do {
4428                 setnil = 0;
4429                 while (*cp == '/')
4430                         cp++;
4431                 cp2 = cp;
4432                 while (*cp2 != '\0' && *cp2 != '/')
4433                         cp2++;
4434                 if (*cp2 == '/') {
4435                         setnil = 1;
4436                         *cp2 = '\0';
4437                 }
4438                 if (cp2 != cp) {
4439                         NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
4440                         *tl = txdr_unsigned(NFSV4OP_LOOKUP);
4441                         nfsm_strtom(nd, cp, strlen(cp));
4442                         cnt++;
4443                 }
4444                 if (setnil)
4445                         *cp2++ = '/';
4446                 cp = cp2;
4447         } while (*cp != '\0');
4448         if (NFSHASNFSV4N(nmp))
4449                 /* Has a Sequence Op done by nfscl_reqstart(). */
4450                 *opcntp = txdr_unsigned(3 + cnt);
4451         else
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);
4458         if (error)
4459                 return (error);
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 ||
4464                         len > NFSX_FHMAX) {
4465                         nd->nd_repstat = NFSERR_BADXDR;
4466                 } else {
4467                         nd->nd_repstat = nfsrv_mtostr(nd, nmp->nm_fh, len);
4468                         if (nd->nd_repstat == 0)
4469                                 nmp->nm_fhsize = len;
4470                 }
4471         }
4472         error = nd->nd_repstat;
4473 nfsmout:
4474         mbuf_freem(nd->nd_mrep);
4475         return (error);
4476 }
4477
4478 /*
4479  * This function performs the Delegreturn RPC.
4480  */
4481 APPLESTATIC int
4482 nfsrpc_delegreturn(struct nfscldeleg *dp, struct ucred *cred,
4483     struct nfsmount *nmp, NFSPROC_T *p, int syscred)
4484 {
4485         u_int32_t *tl;
4486         struct nfsrv_descript nfsd;
4487         struct nfsrv_descript *nd = &nfsd;
4488         int error;
4489
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))
4494                 *tl++ = 0;
4495         else
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];
4500         if (syscred)
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);
4504         if (error)
4505                 return (error);
4506         error = nd->nd_repstat;
4507         mbuf_freem(nd->nd_mrep);
4508         return (error);
4509 }
4510
4511 /*
4512  * nfs getacl call.
4513  */
4514 APPLESTATIC int
4515 nfsrpc_getacl(vnode_t vp, struct ucred *cred, NFSPROC_T *p,
4516     struct acl *aclp, void *stuff)
4517 {
4518         struct nfsrv_descript nfsd, *nd = &nfsd;
4519         int error;
4520         nfsattrbit_t attrbits;
4521         struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
4522         
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);
4530         if (error)
4531                 return (error);
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);
4535         else
4536                 error = nd->nd_repstat;
4537         mbuf_freem(nd->nd_mrep);
4538         return (error);
4539 }
4540
4541 /*
4542  * nfs setacl call.
4543  */
4544 APPLESTATIC int
4545 nfsrpc_setacl(vnode_t vp, struct ucred *cred, NFSPROC_T *p,
4546     struct acl *aclp, void *stuff)
4547 {
4548         int error;
4549         struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
4550         
4551         if (nfsrv_useacl == 0 || !NFSHASNFSV4(nmp))
4552                 return (EOPNOTSUPP);
4553         error = nfsrpc_setattr(vp, NULL, aclp, cred, p, NULL, NULL, stuff);
4554         return (error);
4555 }
4556
4557 /*
4558  * nfs setacl call.
4559  */
4560 static int
4561 nfsrpc_setaclrpc(vnode_t vp, struct ucred *cred, NFSPROC_T *p,
4562     struct acl *aclp, nfsv4stateid_t *stateidp, void *stuff)
4563 {
4564         struct nfsrv_descript nfsd, *nd = &nfsd;
4565         int error;
4566         nfsattrbit_t attrbits;
4567         struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
4568         
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);
4578         if (error)
4579                 return (error);
4580         /* Don't care about the pre/postop attributes */
4581         mbuf_freem(nd->nd_mrep);
4582         return (nd->nd_repstat);
4583 }
4584
4585 /*
4586  * Do the NFSv4.1 Exchange ID.
4587  */
4588 int
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)
4592 {
4593         uint32_t *tl, v41flags;
4594         struct nfsrv_descript nfsd;
4595         struct nfsrv_descript *nd = &nfsd;
4596         struct nfsclds *dsp;
4597         struct timespec verstime;
4598         int error, len;
4599
4600         *dspp = NULL;
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);
4606
4607         NFSM_BUILD(tl, uint32_t *, 3 * NFSX_UNSIGNED);
4608         *tl++ = txdr_unsigned(exchflags);
4609         *tl++ = txdr_unsigned(NFSV4EXCH_SP4NONE);
4610
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);
4624         if (error != 0)
4625                 return (error);
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;
4631                         goto nfsmout;
4632                 }
4633                 dsp = malloc(sizeof(struct nfsclds) + len + 1, M_NFSCLDS,
4634                     M_WAITOK | M_ZERO);
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");
4645                         NFSLOCKMNT(nmp);
4646                         nmp->nm_state |= NFSSTA_PNFS;
4647                         NFSUNLOCKMNT(nmp);
4648                         dsp->nfsclds_flags |= NFSCLDS_MDS;
4649                 }
4650                 if ((v41flags & NFSV4EXCH_USEPNFSDS) != 0)
4651                         dsp->nfsclds_flags |= NFSCLDS_DS;
4652                 if (len > 0)
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",
4658                             NULL, MTX_DEF);
4659                         nfscl_initsessionslots(&dsp->nfsclds_sess);
4660                         *dspp = dsp;
4661                 } else
4662                         free(dsp, M_NFSCLDS);
4663         }
4664         error = nd->nd_repstat;
4665 nfsmout:
4666         mbuf_freem(nd->nd_mrep);
4667         return (error);
4668 }
4669
4670 /*
4671  * Do the NFSv4.1 Create Session.
4672  */
4673 int
4674 nfsrpc_createsession(struct nfsmount *nmp, struct nfsclsession *sep,
4675     struct nfssockreq *nrp, uint32_t sequenceid, int mds, struct ucred *cred,
4676     NFSPROC_T *p)
4677 {
4678         uint32_t crflags, maxval, *tl;
4679         struct nfsrv_descript nfsd;
4680         struct nfsrv_descript *nd = &nfsd;
4681         int error, irdcnt;
4682
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,
4689             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);
4698
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 */
4708
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 */
4718
4719         NFSM_BUILD(tl, uint32_t *, 8 * NFSX_UNSIGNED);
4720         *tl++ = txdr_unsigned(NFS_CALLBCKPROG); /* Call back prog # */
4721
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);
4733         if (error != 0)
4734                 return (error);
4735         if (nd->nd_repstat == 0) {
4736                 NFSM_DISSECT(tl, uint32_t *, NFSX_V4SESSIONID +
4737                     2 * NFSX_UNSIGNED);
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) {
4743                         NFSLOCKMNT(nmp);
4744                         nmp->nm_state |= NFSSTA_SESSPERSIST;
4745                         NFSUNLOCKMNT(nmp);
4746                 }
4747
4748                 /* Get the fore channel slot count. */
4749                 NFSM_DISSECT(tl, uint32_t *, 7 * NFSX_UNSIGNED);
4750                 tl++;                   /* Skip the header pad size. */
4751
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)
4756                                 nmp->nm_wsize /= 2;
4757                         else
4758                                 break;
4759                 }
4760
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)
4765                                 nmp->nm_rsize /= 2;
4766                         else
4767                                 break;
4768                 }
4769
4770                 sep->nfsess_maxcache = fxdr_unsigned(int, *tl++);
4771                 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);
4775                 if (irdcnt > 0)
4776                         NFSM_DISSECT(tl, uint32_t *, irdcnt * NFSX_UNSIGNED);
4777
4778                 /* and the back channel slot count. */
4779                 NFSM_DISSECT(tl, uint32_t *, 7 * NFSX_UNSIGNED);
4780                 tl += 5;
4781                 sep->nfsess_backslots = fxdr_unsigned(uint16_t, *tl);
4782                 NFSCL_DEBUG(4, "back slots=%d\n", (int)sep->nfsess_backslots);
4783         }
4784         error = nd->nd_repstat;
4785 nfsmout:
4786         mbuf_freem(nd->nd_mrep);
4787         return (error);
4788 }
4789
4790 /*
4791  * Do the NFSv4.1 Destroy Session.
4792  */
4793 int
4794 nfsrpc_destroysession(struct nfsmount *nmp, struct nfsclclient *clp,
4795     struct ucred *cred, NFSPROC_T *p)
4796 {
4797         uint32_t *tl;
4798         struct nfsrv_descript nfsd;
4799         struct nfsrv_descript *nd = &nfsd;
4800         int error;
4801         struct nfsclsession *tsep;
4802
4803         nfscl_reqstart(nd, NFSPROC_DESTROYSESSION, nmp, NULL, 0, NULL, NULL, 0,
4804             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);
4811         if (error != 0)
4812                 return (error);
4813         error = nd->nd_repstat;
4814         mbuf_freem(nd->nd_mrep);
4815         return (error);
4816 }
4817
4818 /*
4819  * Do the NFSv4.1 Destroy Client.
4820  */
4821 int
4822 nfsrpc_destroyclient(struct nfsmount *nmp, struct nfsclclient *clp,
4823     struct ucred *cred, NFSPROC_T *p)
4824 {
4825         uint32_t *tl;
4826         struct nfsrv_descript nfsd;
4827         struct nfsrv_descript *nd = &nfsd;
4828         int error;
4829         struct nfsclsession *tsep;
4830
4831         nfscl_reqstart(nd, NFSPROC_DESTROYCLIENT, nmp, NULL, 0, NULL, NULL, 0,
4832             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);
4840         if (error != 0)
4841                 return (error);
4842         error = nd->nd_repstat;
4843         mbuf_freem(nd->nd_mrep);
4844         return (error);
4845 }
4846
4847 /*
4848  * Do the NFSv4.1 LayoutGet.
4849  */
4850 static int
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)
4855 {
4856         struct nfsrv_descript nfsd, *nd = &nfsd;
4857         int error;
4858
4859         nfscl_reqstart(nd, NFSPROC_LAYOUTGET, nmp, fhp, fhlen, NULL, NULL, 0,
4860             0);
4861         nfsrv_setuplayoutget(nd, iomode, offset, len, minlen, stateidp,
4862             layoutlen, 0);
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);
4867         if (error != 0)
4868                 return (error);
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);
4874         return (error);
4875 }
4876
4877 /*
4878  * Do the NFSv4.1 Get Device Info.
4879  */
4880 int
4881 nfsrpc_getdeviceinfo(struct nfsmount *nmp, uint8_t *deviceid, int layouttype,
4882     uint32_t *notifybitsp, struct nfscldevinfo **ndip, struct ucred *cred,
4883     NFSPROC_T *p)
4884 {
4885         uint32_t cnt, *tl;
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;
4895
4896         *ndip = NULL;
4897         ndi = NULL;
4898         gotdspp = NULL;
4899         nfscl_reqstart(nd, NFSPROC_GETDEVICEINFO, nmp, NULL, 0, NULL, NULL, 0,
4900             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);
4910         } else
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);
4915         if (error != 0)
4916                 return (error);
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",
4925                             stripecnt);
4926                         error = NFSERR_BADXDR;
4927                         goto nfsmout;
4928                 }
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",
4934                             addrcnt);
4935                         error = NFSERR_BADXDR;
4936                         goto nfsmout;
4937                 }
4938
4939                 /*
4940                  * Now we know how many stripe indices and addresses, so
4941                  * we can allocate the structure the correct size.
4942                  */
4943                 i = (stripecnt * sizeof(uint8_t)) / sizeof(struct nfsclds *)
4944                     + 1;
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",
4958                                     (int)stripeindex);
4959                                 error = NFSERR_BADXDR;
4960                                 goto nfsmout;
4961                         }
4962                         nfsfldi_setstripeindex(ndi, i, stripeindex);
4963                 }
4964
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);
4970                         if (cnt == 0) {
4971                                 printf("NFS devinfo 0 len addrlist\n");
4972                                 error = NFSERR_BADXDR;
4973                                 goto nfsmout;
4974                         }
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,
4979                                     &isudp);
4980                                 if (error != 0 && error != EPERM) {
4981                                         error = NFSERR_BADXDR;
4982                                         goto nfsmout;
4983                                 }
4984                                 if (error == 0 && isudp == 0) {
4985                                         /*
4986                                          * The priority is:
4987                                          * - Same address family.
4988                                          * Save the address and dspp, so that
4989                                          * the connection can be done after
4990                                          * parsing is complete.
4991                                          */
4992                                         if (safilled == AF_UNSPEC ||
4993                                             (af == nmp->nm_nam->sa_family &&
4994                                              safilled != nmp->nm_nam->sa_family)
4995                                            ) {
4996                                                 if (af == AF_INET)
4997                                                         ssin = sin;
4998                                                 else
4999                                                         ssin6 = sin6;
5000                                                 safilled = af;
5001                                                 gotdspp = dspp;
5002                                         }
5003                                 }
5004                         }
5005                 }
5006
5007                 gotvers = NFS_VER4;     /* Always NFSv4 for File Layout. */
5008
5009                 /* And the notify bits. */
5010                 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
5011                 bitcnt = fxdr_unsigned(int, *tl);
5012                 if (bitcnt > 0) {
5013                         NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
5014                         if (notifybitsp != NULL)
5015                                 *notifybitsp =
5016                                     fxdr_unsigned(uint32_t, *tl);
5017                 }
5018                 if (safilled != AF_UNSPEC) {
5019                         KASSERT(ndi != NULL, ("ndi is NULL"));
5020                         *ndip = ndi;
5021                 } else
5022                         error = EPERM;
5023                 if (error == 0) {
5024                         /*
5025                          * Now we can do a TCP connection for the correct
5026                          * NFS version and IP address.
5027                          */
5028                         error = nfsrpc_fillsa(nmp, &ssin, &ssin6, safilled,
5029                             gotvers, &dsp, p);
5030                 }
5031                 if (error == 0) {
5032                         KASSERT(gotdspp != NULL, ("gotdspp is NULL"));
5033                         *gotdspp = dsp;
5034                 }
5035         }
5036         if (nd->nd_repstat != 0)
5037                 error = nd->nd_repstat;
5038 nfsmout:
5039         if (error != 0 && ndi != NULL)
5040                 nfscl_freedevinfo(ndi);
5041         mbuf_freem(nd->nd_mrep);
5042         return (error);
5043 }
5044
5045 /*
5046  * Do the NFSv4.1 LayoutCommit.
5047  */
5048 int
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)
5052 {
5053         uint32_t *tl;
5054         struct nfsrv_descript nfsd, *nd = &nfsd;
5055         int error;
5056
5057         nfscl_reqstart(nd, NFSPROC_LAYOUTCOMMIT, nmp, fh, fhlen, NULL, NULL,
5058             0, 0);
5059         NFSM_BUILD(tl, uint32_t *, 5 * NFSX_UNSIGNED + 3 * NFSX_HYPER +
5060             NFSX_STATEID);
5061         txdr_hyper(off, tl);
5062         tl += 2;
5063         txdr_hyper(len, tl);
5064         tl += 2;
5065         if (reclaim != 0)
5066                 *tl++ = newnfs_true;
5067         else
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;
5074         if (lastbyte < off)
5075                 lastbyte = off;
5076         else if (lastbyte >= (off + len))
5077                 lastbyte = off + len - 1;
5078         txdr_hyper(lastbyte, tl);
5079         tl += 2;
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);
5087         if (error != 0)
5088                 return (error);
5089         error = nd->nd_repstat;
5090         mbuf_freem(nd->nd_mrep);
5091         return (error);
5092 }
5093
5094 /*
5095  * Do the NFSv4.1 LayoutReturn.
5096  */
5097 int
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,
5101     void *stuff)
5102 {
5103         uint32_t *tl;
5104         struct nfsrv_descript nfsd, *nd = &nfsd;
5105         int error;
5106
5107         nfscl_reqstart(nd, NFSPROC_LAYOUTRETURN, nmp, fh, fhlen, NULL, NULL,
5108             0, 0);
5109         NFSM_BUILD(tl, uint32_t *, 4 * NFSX_UNSIGNED);
5110         if (reclaim != 0)
5111                 *tl++ = newnfs_true;
5112         else
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 +
5119                     NFSX_UNSIGNED);
5120                 txdr_hyper(offset, tl);
5121                 tl += 2;
5122                 txdr_hyper(len, tl);
5123                 tl += 2;
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);
5130         }
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);
5134         if (error != 0)
5135                 return (error);
5136         if (nd->nd_repstat == 0) {
5137                 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
5138                 if (*tl != 0) {
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;
5144                 }
5145         } else
5146                 error = nd->nd_repstat;
5147 nfsmout:
5148         mbuf_freem(nd->nd_mrep);
5149         return (error);
5150 }
5151
5152 /*
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.
5156  */
5157 static int
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)
5161 {
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;
5168
5169         *lypp = NULL;
5170         /*
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
5173          * flp == NULL.
5174          */
5175         lyp = nfscl_getlayout(nmp->nm_clp, nfhp->nfh_fh, nfhp->nfh_len,
5176             off, &flp, &recalled);
5177         islocked = 0;
5178         if (lyp == NULL || flp == NULL) {
5179                 if (recalled != 0)
5180                         return (EIO);
5181                 LIST_INIT(&flh);
5182                 tsep = nfsmnt_mdssession(nmp);
5183                 layoutlen = tsep->nfsess_maxcache -
5184                     (NFSX_STATEID + 3 * NFSX_UNSIGNED);
5185                 if (lyp == NULL) {
5186                         stateid.seqid = 0;
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);
5194                 } else {
5195                         islocked = 1;
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);
5204                 }
5205                 error = nfsrpc_layoutgetres(nmp, vp, nfhp->nfh_fh,
5206                     nfhp->nfh_len, &stateid, retonclose, notifybitsp, &lyp,
5207                     &flh, error, NULL, cred, p);
5208                 if (error == 0)
5209                         *lypp = lyp;
5210                 else if (islocked != 0)
5211                         nfscl_rellayout(lyp, 1);
5212         } else
5213                 *lypp = lyp;
5214         return (error);
5215 }
5216
5217 /*
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.
5221  */
5222 static int
5223 nfsrpc_fillsa(struct nfsmount *nmp, struct sockaddr_in *sin,
5224     struct sockaddr_in6 *sin6, sa_family_t af, int vers, struct nfsclds **dspp,
5225     NFSPROC_T *p)
5226 {
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;
5232         int error;
5233         enum nfsclds_state retv;
5234         uint32_t sequenceid;
5235
5236         KASSERT(nmp->nm_sockreq.nr_cred != NULL,
5237             ("nfsrpc_fillsa: NULL nr_cred"));
5238         NFSLOCKCLSTATE();
5239         clp = nmp->nm_clp;
5240         NFSUNLOCKCLSTATE();
5241         if (clp == NULL)
5242                 return (EPERM);
5243         if (af == AF_INET) {
5244                 NFSLOCKMNT(nmp);
5245                 /*
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.
5250                  */
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) {
5259                                 *dspp = tdsp;
5260                                 NFSUNLOCKMNT(nmp);
5261                                 NFSCL_DEBUG(4, "fnd same addr\n");
5262                                 return (0);
5263                         }
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;
5268                         else
5269                                 msad = NULL;
5270                 }
5271                 NFSUNLOCKMNT(nmp);
5272
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) {
5282                 NFSLOCKMNT(nmp);
5283                 /*
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.
5288                  */
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) {
5298                                 *dspp = tdsp;
5299                                 NFSUNLOCKMNT(nmp);
5300                                 return (0);
5301                         }
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;
5306                         else
5307                                 msad6 = NULL;
5308                 }
5309                 NFSUNLOCKMNT(nmp);
5310
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;
5320         } else
5321                 return (EPERM);
5322
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;
5327
5328         /*
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.
5334          */
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);
5338
5339         /* Now, do the exchangeid and create session. */
5340         if (error == 0) {
5341                 error = nfsrpc_exchangeid(nmp, clp, nrp, NFSV4EXCH_USEPNFSDS,
5342                     &dsp, nrp->nr_cred, p);
5343                 NFSCL_DEBUG(3, "DS exchangeid=%d\n", error);
5344                 if (error != 0)
5345                         newnfs_disconnect(nrp);
5346         }
5347         if (error == 0) {
5348                 dsp->nfsclds_sockp = nrp;
5349                 NFSLOCKMNT(nmp);
5350                 retv = nfscl_getsameserver(nmp, dsp, &tdsp);
5351                 NFSCL_DEBUG(3, "getsame ret=%d\n", retv);
5352                 if (retv == NFSDSP_USETHISSESSION) {
5353                         NFSUNLOCKMNT(nmp);
5354                         /*
5355                          * If there is already a session for this server,
5356                          * use it.
5357                          */
5358                         (void)newnfs_disconnect(nrp);
5359                         nfscl_freenfsclds(dsp);
5360                         *dspp = tdsp;
5361                         return (0);
5362                 }
5363                 if (retv == NFSDSP_SEQTHISSESSION)
5364                         sequenceid = tdsp->nfsclds_sess.nfsess_sequenceid;
5365                 else
5366                         sequenceid = dsp->nfsclds_sess.nfsess_sequenceid;
5367                 NFSUNLOCKMNT(nmp);
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);
5371         } else {
5372                 NFSFREECRED(nrp->nr_cred);
5373                 NFSFREEMUTEX(&nrp->nr_mtx);
5374                 free(nrp->nr_nam, M_SONAME);
5375                 free(nrp, M_NFSSOCKREQ);
5376         }
5377         if (error == 0) {
5378                 NFSCL_DEBUG(3, "add DS session\n");
5379                 /*
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.
5384                  */
5385                 NFSLOCKMNT(nmp);
5386                 TAILQ_INSERT_TAIL(&nmp->nm_sess, dsp, nfsclds_list);
5387                 NFSUNLOCKMNT(nmp);
5388                 *dspp = dsp;
5389         } else if (dsp != NULL) {
5390                 newnfs_disconnect(nrp);
5391                 nfscl_freenfsclds(dsp);
5392         }
5393         return (error);
5394 }
5395
5396 /*
5397  * Do the NFSv4.1 Reclaim Complete.
5398  */
5399 int
5400 nfsrpc_reclaimcomplete(struct nfsmount *nmp, struct ucred *cred, NFSPROC_T *p)
5401 {
5402         uint32_t *tl;
5403         struct nfsrv_descript nfsd;
5404         struct nfsrv_descript *nd = &nfsd;
5405         int error;
5406
5407         nfscl_reqstart(nd, NFSPROC_RECLAIMCOMPL, nmp, NULL, 0, NULL, NULL, 0,
5408             0);
5409         NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
5410         *tl = newnfs_false;
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);
5414         if (error != 0)
5415                 return (error);
5416         error = nd->nd_repstat;
5417         mbuf_freem(nd->nd_mrep);
5418         return (error);
5419 }
5420
5421 /*
5422  * Initialize the slot tables for a session.
5423  */
5424 static void
5425 nfscl_initsessionslots(struct nfsclsession *sep)
5426 {
5427         int i;
5428
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));
5433         }
5434         for (i = 0; i < 64; i++)
5435                 sep->nfsess_slotseq[i] = 0;
5436         sep->nfsess_slots = 0;
5437 }
5438
5439 /*
5440  * Called to try and do an I/O operation via an NFSv4.1 Data Server (DS).
5441  */
5442 int
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)
5445 {
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;
5455         void *lckp;
5456
5457         if (!NFSHASPNFS(nmp) || nfscl_enablecallb == 0 || nfs_numnfscbd == 0 ||
5458             (np->n_flag & NNOLAYOUT) != 0)
5459                 return (EIO);
5460         /* Now, get a reference cnt on the clientid for this mount. */
5461         if (nfscl_getref(nmp) == 0)
5462                 return (EIO);
5463
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);
5468         if (error != 0) {
5469                 NFSFREECRED(newcred);
5470                 nfscl_relref(nmp);
5471                 return (error);
5472         }
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);
5480                         nfscl_relref(nmp);
5481                         return (EIO);
5482                 }
5483                 if (layp != NULL) {
5484                         nfscl_rellayout(layp, (rflp == NULL) ? 1 : 0);
5485                         layp = NULL;
5486                 }
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;
5491                 else
5492                         iolaymode = NFSLAYOUTIOMODE_READ;
5493                 error = nfsrpc_getlayout(nmp, vp, np->n_fhp, iolaymode,
5494                     NULL, &stateid, off, &layp, newcred, p);
5495                 if (error != 0) {
5496                         NFSLOCKNODE(np);
5497                         np->n_flag |= NNOLAYOUT;
5498                         NFSUNLOCKNODE(np);
5499                         if (lckp != NULL)
5500                                 nfscl_lockderef(lckp);
5501                         NFSFREECRED(newcred);
5502                         if (layp != NULL)
5503                                 nfscl_rellayout(layp, 0);
5504                         nfscl_relref(nmp);
5505                         return (error);
5506                 }
5507         }
5508
5509         /*
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
5512          * does the RPC.
5513          */
5514         eof = 0;
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);
5519                 if (error == 0) {
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,
5524                             rflp->nfsfl_devp);
5525                         if (dip != NULL) {
5526                                 error = nfscl_doflayoutio(vp, uiop, iomode,
5527                                     must_commit, &eof, &stateid, rwaccess, dip,
5528                                     layp, rflp, off, xfer, docommit, newcred,
5529                                     p);
5530                                 nfscl_reldevinfo(dip);
5531                                 lastbyte = off + xfer - 1;
5532                                 if (error == 0) {
5533                                         NFSLOCKCLSTATE();
5534                                         if (lastbyte > layp->nfsly_lastbyte)
5535                                                 layp->nfsly_lastbyte = lastbyte;
5536                                         NFSUNLOCKCLSTATE();
5537                                 } else if (error == NFSERR_OPENMODE &&
5538                                     rwaccess == NFSV4OPEN_ACCESSREAD) {
5539                                         NFSLOCKMNT(nmp);
5540                                         nmp->nm_state |= NFSSTA_OPENMODE;
5541                                         NFSUNLOCKMNT(nmp);
5542                                 }
5543                         } else
5544                                 error = EIO;
5545                         if (error == 0)
5546                                 len -= (oresid - (uint64_t)uiop->uio_resid);
5547                 }
5548         }
5549         if (lckp != NULL)
5550                 nfscl_lockderef(lckp);
5551         NFSFREECRED(newcred);
5552         nfscl_rellayout(layp, 0);
5553         nfscl_relref(nmp);
5554         return (error);
5555 }
5556
5557 /*
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.
5560  */
5561 int
5562 nfscl_findlayoutforio(struct nfscllayout *lyp, uint64_t off, uint32_t rwaccess,
5563     struct nfsclflayout **retflpp)
5564 {
5565         struct nfsclflayout *flp, *nflp, *rflp;
5566         uint32_t rw;
5567
5568         rflp = NULL;
5569         rw = rwaccess;
5570         /* For reading, do the Read list first and then the Write list. */
5571         do {
5572                 if (rw == NFSV4OPEN_ACCESSREAD)
5573                         flp = LIST_FIRST(&lyp->nfsly_flayread);
5574                 else
5575                         flp = LIST_FIRST(&lyp->nfsly_flayrw);
5576                 while (flp != NULL) {
5577                         nflp = LIST_NEXT(flp, nfsfl_list);
5578                         if (flp->nfsfl_off > off)
5579                                 break;
5580                         if (flp->nfsfl_end > off &&
5581                             (rflp == NULL || rflp->nfsfl_end < flp->nfsfl_end))
5582                                 rflp = flp;
5583                         flp = nflp;
5584                 }
5585                 if (rw == NFSV4OPEN_ACCESSREAD)
5586                         rw = NFSV4OPEN_ACCESSWRITE;
5587                 else
5588                         rw = 0;
5589         } while (rw != 0);
5590         if (rflp != NULL) {
5591                 /* This one covers the most bytes starting at off. */
5592                 *retflpp = rflp;
5593                 return (0);
5594         }
5595         return (EIO);
5596 }
5597
5598 /*
5599  * Do I/O using an NFSv4.1 file layout.
5600  */
5601 static int
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)
5606 {
5607         uint64_t io_off, rel_off, stripe_unit_size, transfer, xfer;
5608         int commit_thru_mds, error, stripe_index, stripe_pos;
5609         struct nfsnode *np;
5610         struct nfsfh *fhp;
5611         struct nfsclds **dspp;
5612
5613         np = VTONFS(vp);
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);
5619         error = 0;
5620
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)
5626                         xfer = transfer;
5627                 else
5628                         xfer = len;
5629                 if ((flp->nfsfl_util & NFSFLAYUTIL_DENSE) != 0) {
5630                         /* Dense layout. */
5631                         if (stripe_pos >= flp->nfsfl_fhcnt)
5632                                 return (EIO);
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;
5637                 } else {
5638                         /* Sparse layout. */
5639                         if (flp->nfsfl_fhcnt > 1) {
5640                                 if (stripe_index >= flp->nfsfl_fhcnt)
5641                                         return (EIO);
5642                                 fhp = flp->nfsfl_fh[stripe_index];
5643                         } else if (flp->nfsfl_fhcnt == 1)
5644                                 fhp = flp->nfsfl_fh[0];
5645                         else
5646                                 fhp = np->n_fhp;
5647                         io_off = off;
5648                 }
5649                 if ((flp->nfsfl_util & NFSFLAYUTIL_COMMIT_THRU_MDS) != 0) {
5650                         commit_thru_mds = 1;
5651                         if (docommit != 0)
5652                                 error = EIO;
5653                 } else {
5654                         commit_thru_mds = 0;
5655                         mtx_lock(&np->n_mtx);
5656                         np->n_flag |= NDSCOMMIT;
5657                         mtx_unlock(&np->n_mtx);
5658                 }
5659                 if (docommit != 0) {
5660                         if (error == 0)
5661                                 error = nfsrpc_commitds(vp, io_off, xfer,
5662                                     *dspp, fhp, cred, p);
5663                         if (error == 0) {
5664                                 /*
5665                                  * Set both eof and uio_resid = 0 to end any
5666                                  * loops.
5667                                  */
5668                                 *eofp = 1;
5669                                 uiop->uio_resid = 0;
5670                         } else {
5671                                 mtx_lock(&np->n_mtx);
5672                                 np->n_flag &= ~NDSCOMMIT;
5673                                 mtx_unlock(&np->n_mtx);
5674                         }
5675                 } else if (rwflag == NFSV4OPEN_ACCESSREAD)
5676                         error = nfsrpc_readds(vp, uiop, stateidp, eofp, *dspp,
5677                             io_off, xfer, fhp, cred, p);
5678                 else {
5679                         error = nfsrpc_writeds(vp, uiop, iomode, must_commit,
5680                             stateidp, *dspp, io_off, xfer, fhp, commit_thru_mds,
5681                             cred, p);
5682                         if (error == 0) {
5683                                 NFSLOCKCLSTATE();
5684                                 lyp->nfsly_flags |= NFSLY_WRITTEN;
5685                                 NFSUNLOCKCLSTATE();
5686                         }
5687                 }
5688                 if (error == 0) {
5689                         transfer = stripe_unit_size;
5690                         stripe_pos = (stripe_pos + 1) % dp->nfsdi_stripecnt;
5691                         len -= xfer;
5692                         off += xfer;
5693                 }
5694         }
5695         return (error);
5696 }
5697
5698 /*
5699  * The actual read RPC done to a DS.
5700  */
5701 static int
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)
5705 {
5706         uint32_t *tl;
5707         int error, retlen;
5708         struct nfsrv_descript nfsd;
5709         struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
5710         struct nfsrv_descript *nd = &nfsd;
5711         struct nfssockreq *nrp;
5712
5713         nd->nd_mrep = NULL;
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;
5721         if (nrp == NULL)
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);
5726         if (error != 0)
5727                 return (error);
5728         if (nd->nd_repstat != 0) {
5729                 error = nd->nd_repstat;
5730                 goto nfsmout;
5731         }
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);
5736 nfsmout:
5737         if (nd->nd_mrep != NULL)
5738                 mbuf_freem(nd->nd_mrep);
5739         return (error);
5740 }
5741
5742 /*
5743  * The actual write RPC done to a DS.
5744  */
5745 static int
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)
5749 {
5750         uint32_t *tl;
5751         struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
5752         int error, rlen, commit, committed = NFSWRITE_FILESYNC;
5753         int32_t backup;
5754         struct nfsrv_descript nfsd;
5755         struct nfsrv_descript *nd = &nfsd;
5756         struct nfssockreq *nrp;
5757
5758         KASSERT(uiop->uio_iovcnt == 1, ("nfs: writerpc iovcnt > 1"));
5759         nd->nd_mrep = NULL;
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);
5765         tl += 2;
5766         *tl++ = txdr_unsigned(*iomode);
5767         *tl = txdr_unsigned(len);
5768         nfsm_uiombuf(nd, uiop, len);
5769         nrp = dsp->nfsclds_sockp;
5770         if (nrp == NULL)
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);
5775         if (error != 0)
5776                 return (error);
5777         if (nd->nd_repstat != 0) {
5778                 /*
5779                  * In case the rpc gets retried, roll
5780                  * the uio fileds changed by nfsm_uiombuf()
5781                  * back.
5782                  */
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;
5788         } else {
5789                 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED + NFSX_VERF);
5790                 rlen = fxdr_unsigned(int, *tl++);
5791                 if (rlen == 0) {
5792                         error = NFSERR_IO;
5793                         goto nfsmout;
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);
5800                         len = rlen;
5801                 }
5802                 commit = fxdr_unsigned(int, *tl++);
5803
5804                 /*
5805                  * Return the lowest commitment level
5806                  * obtained by any of the RPCs.
5807                  */
5808                 if (committed == NFSWRITE_FILESYNC)
5809                         committed = commit;
5810                 else if (committed == NFSWRITE_DATASYNC &&
5811                     commit == NFSWRITE_UNSTABLE)
5812                         committed = commit;
5813                 if (commit_thru_mds != 0) {
5814                         NFSLOCKMNT(nmp);
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)) {
5819                                 *must_commit = 1;
5820                                 NFSBCOPY(tl, nmp->nm_verf, NFSX_VERF);
5821                         }
5822                         NFSUNLOCKMNT(nmp);
5823                 } else {
5824                         NFSLOCKDS(dsp);
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)) {
5829                                 *must_commit = 1;
5830                                 NFSBCOPY(tl, dsp->nfsclds_verf, NFSX_VERF);
5831                         }
5832                         NFSUNLOCKDS(dsp);
5833                 }
5834         }
5835 nfsmout:
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;
5841         return (error);
5842 }
5843
5844 /*
5845  * Free up the nfsclds structure.
5846  */
5847 void
5848 nfscl_freenfsclds(struct nfsclds *dsp)
5849 {
5850         int i;
5851
5852         if (dsp == NULL)
5853                 return;
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);
5859         }
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)
5864                         m_freem(
5865                             dsp->nfsclds_sess.nfsess_cbslots[i].nfssl_reply);
5866         }
5867         free(dsp, M_NFSCLDS);
5868 }
5869
5870 static enum nfsclds_state
5871 nfscl_getsameserver(struct nfsmount *nmp, struct nfsclds *newdsp,
5872     struct nfsclds **retdspp)
5873 {
5874         struct nfsclds *dsp, *cur_dsp;
5875
5876         /*
5877          * Search the list of nfsclds structures for one with the same
5878          * server.
5879          */
5880         cur_dsp = NULL;
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) {
5892                                 *retdspp = dsp;
5893                                 return (NFSDSP_USETHISSESSION);
5894                         }
5895
5896                         /*
5897                          * Note the first match, so it can be used for
5898                          * sequence'ing new sessions.
5899                          */
5900                         if (cur_dsp == NULL)
5901                                 cur_dsp = dsp;
5902                 }
5903         }
5904         if (cur_dsp != NULL) {
5905                 *retdspp = cur_dsp;
5906                 return (NFSDSP_SEQTHISSESSION);
5907         }
5908         return (NFSDSP_NOTFOUND);
5909 }
5910
5911 /*
5912  * NFS commit rpc to a NFSv4.1 DS.
5913  */
5914 static int
5915 nfsrpc_commitds(vnode_t vp, uint64_t offset, int cnt, struct nfsclds *dsp,
5916     struct nfsfh *fhp, struct ucred *cred, NFSPROC_T *p)
5917 {
5918         uint32_t *tl;
5919         struct nfsrv_descript nfsd, *nd = &nfsd;
5920         struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
5921         struct nfssockreq *nrp;
5922         int error;
5923         
5924         nd->nd_mrep = NULL;
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);
5929         tl += 2;
5930         *tl = txdr_unsigned(cnt);
5931         nrp = dsp->nfsclds_sockp;
5932         if (nrp == NULL)
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);
5937         if (error != 0)
5938                 return (error);
5939         if (nd->nd_repstat == 0) {
5940                 NFSM_DISSECT(tl, u_int32_t *, NFSX_VERF);
5941                 NFSLOCKDS(dsp);
5942                 if (NFSBCMP(tl, dsp->nfsclds_verf, NFSX_VERF)) {
5943                         NFSBCOPY(tl, dsp->nfsclds_verf, NFSX_VERF);
5944                         error = NFSERR_STALEWRITEVERF;
5945                 }
5946                 NFSUNLOCKDS(dsp);
5947         }
5948 nfsmout:
5949         if (error == 0 && nd->nd_repstat != 0)
5950                 error = nd->nd_repstat;
5951         mbuf_freem(nd->nd_mrep);
5952         return (error);
5953 }
5954
5955 /*
5956  * Set up the XDR arguments for the LayoutGet operation.
5957  */
5958 static void
5959 nfsrv_setuplayoutget(struct nfsrv_descript *nd, int iomode, uint64_t offset,
5960     uint64_t len, uint64_t minlen, nfsv4stateid_t *stateidp, int layoutlen,
5961     int usecurstateid)
5962 {
5963         uint32_t *tl;
5964
5965         NFSM_BUILD(tl, uint32_t *, 4 * NFSX_UNSIGNED + 3 * NFSX_HYPER +
5966             NFSX_STATEID);
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);
5971         tl += 2;
5972         txdr_hyper(len, tl);
5973         tl += 2;
5974         txdr_hyper(minlen, tl);
5975         tl += 2;
5976         if (usecurstateid != 0) {
5977                 /* Special stateid for Current stateid. */
5978                 *tl++ = txdr_unsigned(1);
5979                 *tl++ = 0;
5980                 *tl++ = 0;
5981                 *tl++ = 0;
5982         } else {
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];
5988         }
5989         *tl = txdr_unsigned(layoutlen);
5990 }
5991
5992 /*
5993  * Parse the reply for a successful LayoutGet operation.
5994  */
5995 static int
5996 nfsrv_parselayoutget(struct nfsrv_descript *nd, nfsv4stateid_t *stateidp,
5997     int *retonclosep, struct nfsclflayouthead *flhp)
5998 {
5999         uint32_t *tl;
6000         struct nfsclflayout *flp, *prevflp, *tflp;
6001         int cnt, error, gotiomode, fhcnt, nfhlen, i, j;
6002         uint64_t retlen;
6003         struct nfsfh *nfhp;
6004         uint8_t *cp;
6005
6006         error = 0;
6007         flp = NULL;
6008         gotiomode = -1;
6009         NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED + NFSX_STATEID);
6010         if (*tl++ != 0)
6011                 *retonclosep = 1;
6012         else
6013                 *retonclosep = 0;
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;
6025                 goto nfsmout;
6026         }
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;
6037                         goto nfsmout;
6038                 }
6039                 if (fhcnt > 1)
6040                         flp = malloc(sizeof(*flp) + (fhcnt - 1) *
6041                             sizeof(struct nfsfh *), M_NFSFLAYOUT, M_WAITOK);
6042                 else
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;
6051                 else
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;
6059                         goto nfsmout;
6060                 }
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;
6070                         goto nfsmout;
6071                 }
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;
6077                                 goto nfsmout;
6078                         }
6079                         nfhp = malloc(sizeof(*nfhp) + nfhlen - 1, M_NFSFH,
6080                             M_WAITOK);
6081                         flp->nfsfl_fh[j] = nfhp;
6082                         flp->nfsfl_fhcnt++;
6083                         nfhp->nfh_len = nfhlen;
6084                         NFSM_DISSECT(cp, uint8_t *, NFSM_RNDUP(nfhlen));
6085                         NFSBCOPY(cp, nfhp->nfh_fh, nfhlen);
6086                 }
6087                 if (flp->nfsfl_iomode == gotiomode) {
6088                         /* Keep the list in increasing offset order. */
6089                         tflp = LIST_FIRST(flhp);
6090                         prevflp = NULL;
6091                         while (tflp != NULL &&
6092                             tflp->nfsfl_off < flp->nfsfl_off) {
6093                                 prevflp = tflp;
6094                                 tflp = LIST_NEXT(tflp, nfsfl_list);
6095                         }
6096                         if (prevflp == NULL)
6097                                 LIST_INSERT_HEAD(flhp, flp, nfsfl_list);
6098                         else
6099                                 LIST_INSERT_AFTER(prevflp, flp,
6100                                     nfsfl_list);
6101                 } else {
6102                         printf("nfscl_layoutget(): got wrong iomode\n");
6103                         nfscl_freeflayout(flp);
6104                 }
6105                 flp = NULL;
6106         }
6107 nfsmout:
6108         if (error != 0 && flp != NULL)
6109                 nfscl_freeflayout(flp);
6110         return (error);
6111 }
6112
6113 /*
6114  * Similar to nfsrpc_getlayout(), except that it uses nfsrpc_openlayget(),
6115  * so that it does both an Open and a Layoutget.
6116  */
6117 static int
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)
6122 {
6123         struct nfscllayout *lyp;
6124         struct nfsclflayout *flp;
6125         struct nfsclflayouthead flh;
6126         int error, islocked, layoutlen, recalled, retonclose, usecurstateid;
6127         int laystat;
6128         nfsv4stateid_t stateid;
6129         struct nfsclsession *tsep;
6130
6131         error = 0;
6132         /*
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
6135          * flp == NULL.
6136          */
6137         lyp = nfscl_getlayout(nmp->nm_clp, newfhp, newfhlen, 0, &flp,
6138             &recalled);
6139         NFSCL_DEBUG(4, "nfsrpc_getopenlayout nfscl_getlayout lyp=%p\n", lyp);
6140         if (lyp == NULL)
6141                 islocked = 0;
6142         else if (flp != NULL)
6143                 islocked = 1;
6144         else
6145                 islocked = 2;
6146         if ((lyp == NULL || flp == NULL) && recalled == 0) {
6147                 LIST_INIT(&flh);
6148                 tsep = nfsmnt_mdssession(nmp);
6149                 layoutlen = tsep->nfsess_maxcache - (NFSX_STATEID +
6150                     3 * NFSX_UNSIGNED);
6151                 if (lyp == NULL)
6152                         usecurstateid = 1;
6153                 else {
6154                         usecurstateid = 0;
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];
6159                 }
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",
6165                     laystat, error);
6166                 laystat = nfsrpc_layoutgetres(nmp, vp, newfhp, newfhlen,
6167                     &stateid, retonclose, NULL, &lyp, &flh, laystat, &islocked,
6168                     cred, p);
6169         } else
6170                 error = nfsrpc_openrpc(nmp, vp, nfhp, fhlen, newfhp, newfhlen,
6171                     mode, op, name, namelen, dpp, 0, 0, cred, p, 0, 0);
6172         if (islocked == 2)
6173                 nfscl_rellayout(lyp, 1);
6174         else if (islocked == 1)
6175                 nfscl_rellayout(lyp, 0);
6176         return (error);
6177 }
6178
6179 /*
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
6184  * can be NULL.
6185  */
6186 static int
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)
6193 {
6194         uint32_t *tl;
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;
6202
6203         *dpp = NULL;
6204         *laystatp = ENXIO;
6205         nfscl_reqstart(nd, NFSPROC_OPENLAYGET, nmp, nfhp, fhlen, NULL, NULL,
6206             0, 0);
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;
6229         else
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);
6235         if (error != 0)
6236                 return (error);
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 +
6243                     6 * NFSX_UNSIGNED);
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);
6250                 if (error != 0)
6251                         goto nfsmout;
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 +
6270                             NFSX_UNSIGNED);
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;
6278                                 /*
6279                                  * Indicates how much the file can grow.
6280                                  */
6281                                 NFSM_DISSECT(tl, u_int32_t *,
6282                                     3 * NFSX_UNSIGNED);
6283                                 limitby = fxdr_unsigned(int, *tl++);
6284                                 switch (limitby) {
6285                                 case NFSV4OPEN_LIMITSIZE:
6286                                         ndp->nfsdl_sizelimit = fxdr_hyper(tl);
6287                                         break;
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);
6293                                         break;
6294                                 default:
6295                                         error = NFSERR_BADXDR;
6296                                         goto nfsmout;
6297                                 };
6298                         } else
6299                                 ndp->nfsdl_flags = NFSCLDL_READ;
6300                         if (ret != 0)
6301                                 ndp->nfsdl_flags |= NFSCLDL_RECALL;
6302                         error = nfsrv_dissectace(nd, &ndp->nfsdl_ace, &ret,
6303                             &acesize, p);
6304                         if (error != 0)
6305                                 goto nfsmout;
6306                 } else if (deleg != NFSV4OPEN_DELEGATENONE) {
6307                         error = NFSERR_BADXDR;
6308                         goto nfsmout;
6309                 }
6310                 if ((rflags & NFSV4OPEN_LOCKTYPEPOSIX) != 0 ||
6311                     nfscl_assumeposixlocks)
6312                         op->nfso_posixlock = 1;
6313                 else
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. */
6317                 if (*++tl == 0) {
6318                         error = nfsv4_loadattr(nd, NULL, &nfsva, NULL,
6319                             NULL, 0, NULL, NULL, NULL, NULL, NULL, 0,
6320                             NULL, NULL, NULL, p, cred);
6321                         if (error != 0)
6322                                 goto nfsmout;
6323                         if (ndp != NULL) {
6324                                 ndp->nfsdl_change = nfsva.na_filerev;
6325                                 ndp->nfsdl_modtime = nfsva.na_mtime;
6326                                 ndp->nfsdl_flags |= NFSCLDL_MODTIMESET;
6327                                 *dpp = ndp;
6328                                 ndp = NULL;
6329                         }
6330                         /*
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.
6334                          */
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);
6341                                         if (error != 0)
6342                                                 *laystatp = error;
6343                                 }
6344                         } else
6345                                 nd->nd_repstat = 0;     /* Return 0 for Open. */
6346                 }
6347         }
6348         if (nd->nd_repstat != 0 && error == 0)
6349                 error = nd->nd_repstat;
6350 nfsmout:
6351         free(ndp, M_NFSCLDELEG);
6352         mbuf_freem(nd->nd_mrep);
6353         return (error);
6354 }
6355
6356 /*
6357  * Similar nfsrpc_createv4(), but also does the LayoutGet operation.
6358  * Used only for mounts with pNFS enabled.
6359  */
6360 static int
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)
6368 {
6369         uint32_t *tl;
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;
6374         struct nfsnode *np;
6375         struct nfsfh *nfhp;
6376         struct nfsclsession *tsep;
6377         nfsattrbit_t attrbits;
6378         nfsv4stateid_t stateid;
6379         uint32_t rflags;
6380         struct nfsmount *nmp;
6381
6382         nmp = VFSTONFS(dvp->v_mount);
6383         np = VTONFS(dvp);
6384         *laystatp = ENXIO;
6385         *unlockedp = 0;
6386         *nfhpp = NULL;
6387         *dpp = NULL;
6388         *attrflagp = 0;
6389         *dattrflagp = 0;
6390         if (namelen > NFS_MAXNAMLEN)
6391                 return (ENAMETOOLONG);
6392         NFSCL_REQSTART(nd, NFSPROC_CREATELAYGET, dvp);
6393         /*
6394          * For V4, this is actually an Open op.
6395          */
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);
6412                 } else {
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);
6419                 }
6420         } else {
6421                 *tl = txdr_unsigned(NFSCREATE_UNCHECKED);
6422                 nfscl_fillsattr(nd, vap, dvp, 0, 0);
6423         }
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);
6447         if (error != 0)
6448                 return (error);
6449         NFSCL_DEBUG(4, "nfsrpc_createlayout stat=%d err=%d\n", nd->nd_repstat,
6450             error);
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 +
6457                     6 * NFSX_UNSIGNED);
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 +
6480                             NFSX_UNSIGNED);
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;
6488                                 /*
6489                                  * Indicates how much the file can grow.
6490                                  */
6491                                 NFSM_DISSECT(tl, u_int32_t *,
6492                                     3 * NFSX_UNSIGNED);
6493                                 limitby = fxdr_unsigned(int, *tl++);
6494                                 switch (limitby) {
6495                                 case NFSV4OPEN_LIMITSIZE:
6496                                         dp->nfsdl_sizelimit = fxdr_hyper(tl);
6497                                         break;
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);
6503                                         break;
6504                                 default:
6505                                         error = NFSERR_BADXDR;
6506                                         goto nfsmout;
6507                                 };
6508                         } else {
6509                                 dp->nfsdl_flags = NFSCLDL_READ;
6510                         }
6511                         if (ret != 0)
6512                                 dp->nfsdl_flags |= NFSCLDL_RECALL;
6513                         error = nfsrv_dissectace(nd, &dp->nfsdl_ace, &ret,
6514                             &acesize, p);
6515                         if (error != 0)
6516                                 goto nfsmout;
6517                 } else if (deleg != NFSV4OPEN_DELEGATENONE) {
6518                         error = NFSERR_BADXDR;
6519                         goto nfsmout;
6520                 }
6521
6522                 /* Now, we should have the status for the SaveFH. */
6523                 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
6524                 if (*++tl == 0) {
6525                         NFSCL_DEBUG(4, "nfsrpc_createlayout SaveFH ok\n");
6526                         /*
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.
6530                          */
6531                         error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp);
6532                         NFSCL_DEBUG(4, "aft nfscl_mtofh err=%d\n", error);
6533                         if (error != 0)
6534                                 goto nfsmout;
6535                 } else
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);
6540                         if (*++tl != 0)
6541                                 nd->nd_flag |= ND_NOMOREDATA;
6542                         else {
6543                                 NFSM_DISSECT(tl, uint32_t *, 2 *
6544                                     NFSX_UNSIGNED);
6545                                 if (*++tl != 0)
6546                                         nd->nd_flag |= ND_NOMOREDATA;
6547                         }
6548                 }
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);
6553                         if (error != 0)
6554                                 goto nfsmout;
6555                         *dattrflagp = 1;
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;
6560                         }
6561                         /*
6562                          * We can now complete the Open state.
6563                          */
6564                         nfhp = *nfhpp;
6565                         if (dp != NULL) {
6566                                 dp->nfsdl_fhlen = nfhp->nfh_len;
6567                                 NFSBCOPY(nfhp->nfh_fh, dp->nfsdl_fh,
6568                                     nfhp->nfh_len);
6569                         }
6570                         /*
6571                          * Get an Open structure that will be
6572                          * attached to the OpenOwner, acquired already.
6573                          */
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);
6577                         if (error != 0)
6578                                 goto nfsmout;
6579                         op->nfso_stateid = stateid;
6580                         newnfs_copyincred(cred, &op->nfso_cred);
6581         
6582                         nfscl_openrelease(nmp, op, error, newone);
6583                         *unlockedp = 1;
6584
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);
6592                                         if (error != 0)
6593                                                 *laystatp = error;
6594                                 }
6595                                 NFSCL_DEBUG(4, "aft nfsrv_parselayout err=%d\n",
6596                                     error);
6597                         } else
6598                                 nd->nd_repstat = 0;
6599                 }
6600         }
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);
6605 nfsmout:
6606         NFSCL_DEBUG(4, "eo nfsrpc_createlayout err=%d\n", error);
6607         if (error == 0)
6608                 *dpp = dp;
6609         else
6610                 free(dp, M_NFSCLDELEG);
6611         mbuf_freem(nd->nd_mrep);
6612         return (error);
6613 }
6614
6615 /*
6616  * Similar to nfsrpc_getopenlayout(), except that it used for the Create case.
6617  */
6618 static int
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)
6624 {
6625         struct nfscllayout *lyp;
6626         struct nfsclflayouthead flh;
6627         struct nfsfh *nfhp;
6628         struct nfsclsession *tsep;
6629         struct nfsmount *nmp;
6630         nfsv4stateid_t stateid;
6631         int error, layoutlen, retonclose, laystat;
6632
6633         error = 0;
6634         nmp = VFSTONFS(dvp->v_mount);
6635         LIST_INIT(&flh);
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,
6641             &laystat);
6642         NFSCL_DEBUG(4, "aft nfsrpc_createlayoutrpc laystat=%d err=%d\n",
6643             laystat, error);
6644         lyp = NULL;
6645         if (laystat == 0) {
6646                 nfhp = *nfhpp;
6647                 laystat = nfsrpc_layoutgetres(nmp, dvp, nfhp->nfh_fh,
6648                     nfhp->nfh_len, &stateid, retonclose, NULL, &lyp, &flh,
6649                     laystat, NULL, cred, p);
6650         } else
6651                 laystat = nfsrpc_layoutgetres(nmp, dvp, NULL, 0, &stateid,
6652                     retonclose, NULL, &lyp, &flh, laystat, NULL, cred, p);
6653         if (laystat == 0)
6654                 nfscl_rellayout(lyp, 0);
6655         return (error);
6656 }
6657
6658 /*
6659  * Process the results of a layoutget() operation.
6660  */
6661 static int
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)
6666 {
6667         struct nfsclflayout *tflp;
6668         struct nfscldevinfo *dip;
6669
6670         if (laystat == NFSERR_UNKNLAYOUTTYPE) {
6671                 /* Disable PNFS. */
6672                 NFSCL_DEBUG(1, "disable PNFS\n");
6673                 NFSLOCKMNT(nmp);
6674                 nmp->nm_state &= ~NFSSTA_PNFS;
6675                 NFSUNLOCKMNT(nmp);
6676         }
6677         if (laystat == 0) {
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);
6682                         if (laystat != 0) {
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",
6687                                     laystat);
6688                                 if (laystat != 0)
6689                                         break;
6690                                 laystat = nfscl_adddevinfo(nmp, dip, tflp);
6691                                 if (laystat != 0)
6692                                         printf("getlayout: cannot add\n");
6693                         }
6694                 }
6695         }
6696         if (laystat == 0) {
6697                 /*
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.
6702                  */
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",
6706                     laystat);
6707                 if (laystat == 0 && islockedp != NULL)
6708                         *islockedp = 1;
6709         }
6710         return (laystat);
6711 }
6712