]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/fs/nfsclient/nfs_clrpcops.c
nfsclient: Fix ReadDS/WriteDS/CommitDS nfsstats RPC counts for a NFSv3 DS
[FreeBSD/FreeBSD.git] / sys / fs / nfsclient / nfs_clrpcops.c
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1989, 1993
5  *      The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Rick Macklem at The University of Guelph.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  */
35
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
38
39 /*
40  * Rpc op calls, generally called from the vnode op calls or through the
41  * buffer cache, for NFS v2, 3 and 4.
42  * These do not normally make any changes to vnode arguments or use
43  * structures that might change between the VFS variants. The returned
44  * arguments are all at the end, after the NFSPROC_T *p one.
45  */
46
47 #include "opt_inet6.h"
48
49 #include <fs/nfs/nfsport.h>
50 #include <sys/sysctl.h>
51 #include <sys/taskqueue.h>
52
53 SYSCTL_DECL(_vfs_nfs);
54
55 static int      nfsignore_eexist = 0;
56 SYSCTL_INT(_vfs_nfs, OID_AUTO, ignore_eexist, CTLFLAG_RW,
57     &nfsignore_eexist, 0, "NFS ignore EEXIST replies for mkdir/symlink");
58
59 static int      nfscl_dssameconn = 0;
60 SYSCTL_INT(_vfs_nfs, OID_AUTO, dssameconn, CTLFLAG_RW,
61     &nfscl_dssameconn, 0, "Use same TCP connection to multiple DSs");
62
63 /*
64  * Global variables
65  */
66 extern struct nfsstatsv1 nfsstatsv1;
67 extern int nfs_numnfscbd;
68 extern struct timeval nfsboottime;
69 extern u_int32_t newnfs_false, newnfs_true;
70 extern nfstype nfsv34_type[9];
71 extern int nfsrv_useacl;
72 extern char nfsv4_callbackaddr[INET6_ADDRSTRLEN];
73 extern int nfscl_debuglevel;
74 extern int nfs_pnfsiothreads;
75 NFSCLSTATEMUTEX;
76 int nfstest_outofseq = 0;
77 int nfscl_assumeposixlocks = 1;
78 int nfscl_enablecallb = 0;
79 short nfsv4_cbport = NFSV4_CBPORT;
80 int nfstest_openallsetattr = 0;
81
82 #define DIRHDSIZ        offsetof(struct dirent, d_name)
83
84 /*
85  * nfscl_getsameserver() can return one of three values:
86  * NFSDSP_USETHISSESSION - Use this session for the DS.
87  * NFSDSP_SEQTHISSESSION - Use the nfsclds_sequence field of this dsp for new
88  *     session.
89  * NFSDSP_NOTFOUND - No matching server was found.
90  */
91 enum nfsclds_state {
92         NFSDSP_USETHISSESSION = 0,
93         NFSDSP_SEQTHISSESSION = 1,
94         NFSDSP_NOTFOUND = 2,
95 };
96
97 /*
98  * Do a write RPC on a DS data file, using this structure for the arguments,
99  * so that this function can be executed by a separate kernel process.
100  */
101 struct nfsclwritedsdorpc {
102         int                     done;
103         int                     inprog;
104         struct task             tsk;
105         struct vnode            *vp;
106         int                     iomode;
107         int                     must_commit;
108         nfsv4stateid_t          *stateidp;
109         struct nfsclds          *dsp;
110         uint64_t                off;
111         int                     len;
112         struct nfsfh            *fhp;
113         struct mbuf             *m;
114         int                     vers;
115         int                     minorvers;
116         struct ucred            *cred;
117         NFSPROC_T               *p;
118         int                     err;
119 };
120
121 static int nfsrpc_setattrrpc(vnode_t , struct vattr *, nfsv4stateid_t *,
122     struct ucred *, NFSPROC_T *, struct nfsvattr *, int *, void *);
123 static int nfsrpc_readrpc(vnode_t , struct uio *, struct ucred *,
124     nfsv4stateid_t *, NFSPROC_T *, struct nfsvattr *, int *, void *);
125 static int nfsrpc_writerpc(vnode_t , struct uio *, int *, int *,
126     struct ucred *, nfsv4stateid_t *, NFSPROC_T *, struct nfsvattr *, int *,
127     void *);
128 static int nfsrpc_createv23(vnode_t , char *, int, struct vattr *,
129     nfsquad_t, int, struct ucred *, NFSPROC_T *, struct nfsvattr *,
130     struct nfsvattr *, struct nfsfh **, int *, int *, void *);
131 static int nfsrpc_createv4(vnode_t , char *, int, struct vattr *,
132     nfsquad_t, int, struct nfsclowner *, struct nfscldeleg **, struct ucred *,
133     NFSPROC_T *, struct nfsvattr *, struct nfsvattr *, struct nfsfh **, int *,
134     int *, void *, int *);
135 static int nfsrpc_locku(struct nfsrv_descript *, struct nfsmount *,
136     struct nfscllockowner *, u_int64_t, u_int64_t,
137     u_int32_t, struct ucred *, NFSPROC_T *, int);
138 static int nfsrpc_setaclrpc(vnode_t, struct ucred *, NFSPROC_T *,
139     struct acl *, nfsv4stateid_t *, void *);
140 static int nfsrpc_getlayout(struct nfsmount *, vnode_t, struct nfsfh *, int,
141     uint32_t *, nfsv4stateid_t *, uint64_t, struct nfscllayout **,
142     struct ucred *, NFSPROC_T *);
143 static int nfsrpc_fillsa(struct nfsmount *, struct sockaddr_in *,
144     struct sockaddr_in6 *, sa_family_t, int, struct nfsclds **, NFSPROC_T *);
145 static void nfscl_initsessionslots(struct nfsclsession *);
146 static int nfscl_doflayoutio(vnode_t, struct uio *, int *, int *, int *,
147     nfsv4stateid_t *, int, struct nfscldevinfo *, struct nfscllayout *,
148     struct nfsclflayout *, uint64_t, uint64_t, int, struct ucred *,
149     NFSPROC_T *);
150 static int nfscl_dofflayoutio(vnode_t, struct uio *, int *, int *, int *,
151     nfsv4stateid_t *, int, struct nfscldevinfo *, struct nfscllayout *,
152     struct nfsclflayout *, uint64_t, uint64_t, int, int, struct mbuf *,
153     struct nfsclwritedsdorpc *, struct ucred *, NFSPROC_T *);
154 static struct mbuf *nfsm_copym(struct mbuf *, int, int);
155 static int nfsrpc_readds(vnode_t, struct uio *, nfsv4stateid_t *, int *,
156     struct nfsclds *, uint64_t, int, struct nfsfh *, int, int, int,
157     struct ucred *, NFSPROC_T *);
158 static int nfsrpc_writeds(vnode_t, struct uio *, int *, int *,
159     nfsv4stateid_t *, struct nfsclds *, uint64_t, int,
160     struct nfsfh *, int, int, int, int, struct ucred *, NFSPROC_T *);
161 static int nfsio_writedsmir(vnode_t, int *, int *, nfsv4stateid_t *,
162     struct nfsclds *, uint64_t, int, struct nfsfh *, struct mbuf *, int, int,
163     struct nfsclwritedsdorpc *, struct ucred *, NFSPROC_T *);
164 static int nfsrpc_writedsmir(vnode_t, int *, int *, nfsv4stateid_t *,
165     struct nfsclds *, uint64_t, int, struct nfsfh *, struct mbuf *, int, int,
166     struct ucred *, NFSPROC_T *);
167 static enum nfsclds_state nfscl_getsameserver(struct nfsmount *,
168     struct nfsclds *, struct nfsclds **, uint32_t *);
169 static int nfsio_commitds(vnode_t, uint64_t, int, struct nfsclds *,
170     struct nfsfh *, int, int, struct nfsclwritedsdorpc *, struct ucred *,
171     NFSPROC_T *);
172 static int nfsrpc_commitds(vnode_t, uint64_t, int, struct nfsclds *,
173     struct nfsfh *, int, int, struct ucred *, NFSPROC_T *);
174 static void nfsrv_setuplayoutget(struct nfsrv_descript *, int, uint64_t,
175     uint64_t, uint64_t, nfsv4stateid_t *, int, int, int);
176 static int nfsrv_parseug(struct nfsrv_descript *, int, uid_t *, gid_t *,
177     NFSPROC_T *);
178 static int nfsrv_parselayoutget(struct nfsrv_descript *, nfsv4stateid_t *,
179     int *, struct nfsclflayouthead *);
180 static int nfsrpc_getopenlayout(struct nfsmount *, vnode_t, u_int8_t *,
181     int, uint8_t *, int, uint32_t, struct nfsclopen *, uint8_t *, int,
182     struct nfscldeleg **, struct ucred *, NFSPROC_T *);
183 static int nfsrpc_getcreatelayout(vnode_t, char *, int, struct vattr *,
184     nfsquad_t, int, struct nfsclowner *, struct nfscldeleg **,
185     struct ucred *, NFSPROC_T *, struct nfsvattr *, struct nfsvattr *,
186     struct nfsfh **, int *, int *, void *, int *);
187 static int nfsrpc_openlayoutrpc(struct nfsmount *, vnode_t, u_int8_t *,
188     int, uint8_t *, int, uint32_t, struct nfsclopen *, uint8_t *, int,
189     struct nfscldeleg **, nfsv4stateid_t *, int, int, int, int *,
190     struct nfsclflayouthead *, int *, struct ucred *, NFSPROC_T *);
191 static int nfsrpc_createlayout(vnode_t, char *, int, struct vattr *,
192     nfsquad_t, int, struct nfsclowner *, struct nfscldeleg **,
193     struct ucred *, NFSPROC_T *, struct nfsvattr *, struct nfsvattr *,
194     struct nfsfh **, int *, int *, void *, int *, nfsv4stateid_t *,
195     int, int, int, int *, struct nfsclflayouthead *, int *);
196 static int nfsrpc_layoutget(struct nfsmount *, uint8_t *, int, int, uint64_t,
197     uint64_t, uint64_t, int, int, nfsv4stateid_t *, int *,
198     struct nfsclflayouthead *, struct ucred *, NFSPROC_T *, void *);
199 static int nfsrpc_layoutgetres(struct nfsmount *, vnode_t, uint8_t *,
200     int, nfsv4stateid_t *, int, uint32_t *, struct nfscllayout **,
201     struct nfsclflayouthead *, int, int, int *, struct ucred *, NFSPROC_T *);
202
203 int nfs_pnfsio(task_fn_t *, void *);
204
205 /*
206  * nfs null call from vfs.
207  */
208 int
209 nfsrpc_null(vnode_t vp, struct ucred *cred, NFSPROC_T *p)
210 {
211         int error;
212         struct nfsrv_descript nfsd, *nd = &nfsd;
213         
214         NFSCL_REQSTART(nd, NFSPROC_NULL, vp);
215         error = nfscl_request(nd, vp, p, cred, NULL);
216         if (nd->nd_repstat && !error)
217                 error = nd->nd_repstat;
218         mbuf_freem(nd->nd_mrep);
219         return (error);
220 }
221
222 /*
223  * nfs access rpc op.
224  * For nfs version 3 and 4, use the access rpc to check accessibility. If file
225  * modes are changed on the server, accesses might still fail later.
226  */
227 int
228 nfsrpc_access(vnode_t vp, int acmode, struct ucred *cred,
229     NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp)
230 {
231         int error;
232         u_int32_t mode, rmode;
233
234         if (acmode & VREAD)
235                 mode = NFSACCESS_READ;
236         else
237                 mode = 0;
238         if (vnode_vtype(vp) == VDIR) {
239                 if (acmode & VWRITE)
240                         mode |= (NFSACCESS_MODIFY | NFSACCESS_EXTEND |
241                                  NFSACCESS_DELETE);
242                 if (acmode & VEXEC)
243                         mode |= NFSACCESS_LOOKUP;
244         } else {
245                 if (acmode & VWRITE)
246                         mode |= (NFSACCESS_MODIFY | NFSACCESS_EXTEND);
247                 if (acmode & VEXEC)
248                         mode |= NFSACCESS_EXECUTE;
249         }
250
251         /*
252          * Now, just call nfsrpc_accessrpc() to do the actual RPC.
253          */
254         error = nfsrpc_accessrpc(vp, mode, cred, p, nap, attrflagp, &rmode,
255             NULL);
256
257         /*
258          * The NFS V3 spec does not clarify whether or not
259          * the returned access bits can be a superset of
260          * the ones requested, so...
261          */
262         if (!error && (rmode & mode) != mode)
263                 error = EACCES;
264         return (error);
265 }
266
267 /*
268  * The actual rpc, separated out for Darwin.
269  */
270 int
271 nfsrpc_accessrpc(vnode_t vp, u_int32_t mode, struct ucred *cred,
272     NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, u_int32_t *rmodep,
273     void *stuff)
274 {
275         u_int32_t *tl;
276         u_int32_t supported, rmode;
277         int error;
278         struct nfsrv_descript nfsd, *nd = &nfsd;
279         nfsattrbit_t attrbits;
280
281         *attrflagp = 0;
282         supported = mode;
283         NFSCL_REQSTART(nd, NFSPROC_ACCESS, vp);
284         NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
285         *tl = txdr_unsigned(mode);
286         if (nd->nd_flag & ND_NFSV4) {
287                 /*
288                  * And do a Getattr op.
289                  */
290                 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
291                 *tl = txdr_unsigned(NFSV4OP_GETATTR);
292                 NFSGETATTR_ATTRBIT(&attrbits);
293                 (void) nfsrv_putattrbit(nd, &attrbits);
294         }
295         error = nfscl_request(nd, vp, p, cred, stuff);
296         if (error)
297                 return (error);
298         if (nd->nd_flag & ND_NFSV3) {
299                 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
300                 if (error)
301                         goto nfsmout;
302         }
303         if (!nd->nd_repstat) {
304                 if (nd->nd_flag & ND_NFSV4) {
305                         NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
306                         supported = fxdr_unsigned(u_int32_t, *tl++);
307                 } else {
308                         NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
309                 }
310                 rmode = fxdr_unsigned(u_int32_t, *tl);
311                 if (nd->nd_flag & ND_NFSV4)
312                         error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
313
314                 /*
315                  * It's not obvious what should be done about
316                  * unsupported access modes. For now, be paranoid
317                  * and clear the unsupported ones.
318                  */
319                 rmode &= supported;
320                 *rmodep = rmode;
321         } else
322                 error = nd->nd_repstat;
323 nfsmout:
324         mbuf_freem(nd->nd_mrep);
325         return (error);
326 }
327
328 /*
329  * nfs open rpc
330  */
331 int
332 nfsrpc_open(vnode_t vp, int amode, struct ucred *cred, NFSPROC_T *p)
333 {
334         struct nfsclopen *op;
335         struct nfscldeleg *dp;
336         struct nfsfh *nfhp;
337         struct nfsnode *np = VTONFS(vp);
338         struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
339         u_int32_t mode, clidrev;
340         int ret, newone, error, expireret = 0, retrycnt;
341
342         /*
343          * For NFSv4, Open Ops are only done on Regular Files.
344          */
345         if (vnode_vtype(vp) != VREG)
346                 return (0);
347         mode = 0;
348         if (amode & FREAD)
349                 mode |= NFSV4OPEN_ACCESSREAD;
350         if (amode & FWRITE)
351                 mode |= NFSV4OPEN_ACCESSWRITE;
352         nfhp = np->n_fhp;
353
354         retrycnt = 0;
355 #ifdef notdef
356 { char name[100]; int namel;
357 namel = (np->n_v4->n4_namelen < 100) ? np->n_v4->n4_namelen : 99;
358 bcopy(NFS4NODENAME(np->n_v4), name, namel);
359 name[namel] = '\0';
360 printf("rpcopen p=0x%x name=%s",p->p_pid,name);
361 if (nfhp->nfh_len > 0) printf(" fh=0x%x\n",nfhp->nfh_fh[12]);
362 else printf(" fhl=0\n");
363 }
364 #endif
365         do {
366             dp = NULL;
367             error = nfscl_open(vp, nfhp->nfh_fh, nfhp->nfh_len, mode, 1,
368                 cred, p, NULL, &op, &newone, &ret, 1);
369             if (error) {
370                 return (error);
371             }
372             if (nmp->nm_clp != NULL)
373                 clidrev = nmp->nm_clp->nfsc_clientidrev;
374             else
375                 clidrev = 0;
376             if (ret == NFSCLOPEN_DOOPEN) {
377                 if (np->n_v4 != NULL) {
378                         /*
379                          * For the first attempt, try and get a layout, if
380                          * pNFS is enabled for the mount.
381                          */
382                         if (!NFSHASPNFS(nmp) || nfscl_enablecallb == 0 ||
383                             nfs_numnfscbd == 0 ||
384                             (np->n_flag & NNOLAYOUT) != 0 || retrycnt > 0)
385                                 error = nfsrpc_openrpc(nmp, vp,
386                                     np->n_v4->n4_data,
387                                     np->n_v4->n4_fhlen, np->n_fhp->nfh_fh,
388                                     np->n_fhp->nfh_len, mode, op,
389                                     NFS4NODENAME(np->n_v4),
390                                     np->n_v4->n4_namelen,
391                                     &dp, 0, 0x0, cred, p, 0, 0);
392                         else
393                                 error = nfsrpc_getopenlayout(nmp, vp,
394                                     np->n_v4->n4_data,
395                                     np->n_v4->n4_fhlen, np->n_fhp->nfh_fh,
396                                     np->n_fhp->nfh_len, mode, op,
397                                     NFS4NODENAME(np->n_v4),
398                                     np->n_v4->n4_namelen, &dp, cred, p);
399                         if (dp != NULL) {
400 #ifdef APPLE
401                                 OSBitAndAtomic((int32_t)~NDELEGMOD, (UInt32 *)&np->n_flag);
402 #else
403                                 NFSLOCKNODE(np);
404                                 np->n_flag &= ~NDELEGMOD;
405                                 /*
406                                  * Invalidate the attribute cache, so that
407                                  * attributes that pre-date the issue of a
408                                  * delegation are not cached, since the
409                                  * cached attributes will remain valid while
410                                  * the delegation is held.
411                                  */
412                                 NFSINVALATTRCACHE(np);
413                                 NFSUNLOCKNODE(np);
414 #endif
415                                 (void) nfscl_deleg(nmp->nm_mountp,
416                                     op->nfso_own->nfsow_clp,
417                                     nfhp->nfh_fh, nfhp->nfh_len, cred, p, &dp);
418                         }
419                 } else {
420                         error = EIO;
421                 }
422                 newnfs_copyincred(cred, &op->nfso_cred);
423             } else if (ret == NFSCLOPEN_SETCRED)
424                 /*
425                  * This is a new local open on a delegation. It needs
426                  * to have credentials so that an open can be done
427                  * against the server during recovery.
428                  */
429                 newnfs_copyincred(cred, &op->nfso_cred);
430
431             /*
432              * nfso_opencnt is the count of how many VOP_OPEN()s have
433              * been done on this Open successfully and a VOP_CLOSE()
434              * is expected for each of these.
435              * If error is non-zero, don't increment it, since the Open
436              * hasn't succeeded yet.
437              */
438             if (!error)
439                 op->nfso_opencnt++;
440             nfscl_openrelease(nmp, op, error, newone);
441             if (error == NFSERR_GRACE || error == NFSERR_STALECLIENTID ||
442                 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
443                 error == NFSERR_BADSESSION) {
444                 (void) nfs_catnap(PZERO, error, "nfs_open");
445             } else if ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID)
446                 && clidrev != 0) {
447                 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
448                 retrycnt++;
449             }
450         } while (error == NFSERR_GRACE || error == NFSERR_STALECLIENTID ||
451             error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
452             error == NFSERR_BADSESSION ||
453             ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
454              expireret == 0 && clidrev != 0 && retrycnt < 4));
455         if (error && retrycnt >= 4)
456                 error = EIO;
457         return (error);
458 }
459
460 /*
461  * the actual open rpc
462  */
463 int
464 nfsrpc_openrpc(struct nfsmount *nmp, vnode_t vp, u_int8_t *nfhp, int fhlen,
465     u_int8_t *newfhp, int newfhlen, u_int32_t mode, struct nfsclopen *op,
466     u_int8_t *name, int namelen, struct nfscldeleg **dpp,
467     int reclaim, u_int32_t delegtype, struct ucred *cred, NFSPROC_T *p,
468     int syscred, int recursed)
469 {
470         u_int32_t *tl;
471         struct nfsrv_descript nfsd, *nd = &nfsd;
472         struct nfscldeleg *dp, *ndp = NULL;
473         struct nfsvattr nfsva;
474         u_int32_t rflags, deleg;
475         nfsattrbit_t attrbits;
476         int error, ret, acesize, limitby;
477         struct nfsclsession *tsep;
478
479         dp = *dpp;
480         *dpp = NULL;
481         nfscl_reqstart(nd, NFSPROC_OPEN, nmp, nfhp, fhlen, NULL, NULL, 0, 0);
482         NFSM_BUILD(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
483         *tl++ = txdr_unsigned(op->nfso_own->nfsow_seqid);
484         *tl++ = txdr_unsigned(mode & NFSV4OPEN_ACCESSBOTH);
485         *tl++ = txdr_unsigned((mode >> NFSLCK_SHIFT) & NFSV4OPEN_DENYBOTH);
486         tsep = nfsmnt_mdssession(nmp);
487         *tl++ = tsep->nfsess_clientid.lval[0];
488         *tl = tsep->nfsess_clientid.lval[1];
489         (void) nfsm_strtom(nd, op->nfso_own->nfsow_owner, NFSV4CL_LOCKNAMELEN);
490         NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
491         *tl++ = txdr_unsigned(NFSV4OPEN_NOCREATE);
492         if (reclaim) {
493                 *tl = txdr_unsigned(NFSV4OPEN_CLAIMPREVIOUS);
494                 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
495                 *tl = txdr_unsigned(delegtype);
496         } else {
497                 if (dp != NULL) {
498                         *tl = txdr_unsigned(NFSV4OPEN_CLAIMDELEGATECUR);
499                         NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID);
500                         if (NFSHASNFSV4N(nmp))
501                                 *tl++ = 0;
502                         else
503                                 *tl++ = dp->nfsdl_stateid.seqid;
504                         *tl++ = dp->nfsdl_stateid.other[0];
505                         *tl++ = dp->nfsdl_stateid.other[1];
506                         *tl = dp->nfsdl_stateid.other[2];
507                 } else {
508                         *tl = txdr_unsigned(NFSV4OPEN_CLAIMNULL);
509                 }
510                 (void) nfsm_strtom(nd, name, namelen);
511         }
512         NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
513         *tl = txdr_unsigned(NFSV4OP_GETATTR);
514         NFSZERO_ATTRBIT(&attrbits);
515         NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_CHANGE);
516         NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEMODIFY);
517         (void) nfsrv_putattrbit(nd, &attrbits);
518         if (syscred)
519                 nd->nd_flag |= ND_USEGSSNAME;
520         error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, vp, p, cred,
521             NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
522         if (error)
523                 return (error);
524         NFSCL_INCRSEQID(op->nfso_own->nfsow_seqid, nd);
525         if (!nd->nd_repstat) {
526                 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
527                     6 * NFSX_UNSIGNED);
528                 op->nfso_stateid.seqid = *tl++;
529                 op->nfso_stateid.other[0] = *tl++;
530                 op->nfso_stateid.other[1] = *tl++;
531                 op->nfso_stateid.other[2] = *tl;
532                 rflags = fxdr_unsigned(u_int32_t, *(tl + 6));
533                 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
534                 if (error)
535                         goto nfsmout;
536                 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
537                 deleg = fxdr_unsigned(u_int32_t, *tl);
538                 if (deleg == NFSV4OPEN_DELEGATEREAD ||
539                     deleg == NFSV4OPEN_DELEGATEWRITE) {
540                         if (!(op->nfso_own->nfsow_clp->nfsc_flags &
541                               NFSCLFLAGS_FIRSTDELEG))
542                                 op->nfso_own->nfsow_clp->nfsc_flags |=
543                                   (NFSCLFLAGS_FIRSTDELEG | NFSCLFLAGS_GOTDELEG);
544                         ndp = malloc(
545                             sizeof (struct nfscldeleg) + newfhlen,
546                             M_NFSCLDELEG, M_WAITOK);
547                         LIST_INIT(&ndp->nfsdl_owner);
548                         LIST_INIT(&ndp->nfsdl_lock);
549                         ndp->nfsdl_clp = op->nfso_own->nfsow_clp;
550                         ndp->nfsdl_fhlen = newfhlen;
551                         NFSBCOPY(newfhp, ndp->nfsdl_fh, newfhlen);
552                         newnfs_copyincred(cred, &ndp->nfsdl_cred);
553                         nfscl_lockinit(&ndp->nfsdl_rwlock);
554                         NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
555                             NFSX_UNSIGNED);
556                         ndp->nfsdl_stateid.seqid = *tl++;
557                         ndp->nfsdl_stateid.other[0] = *tl++;
558                         ndp->nfsdl_stateid.other[1] = *tl++;
559                         ndp->nfsdl_stateid.other[2] = *tl++;
560                         ret = fxdr_unsigned(int, *tl);
561                         if (deleg == NFSV4OPEN_DELEGATEWRITE) {
562                                 ndp->nfsdl_flags = NFSCLDL_WRITE;
563                                 /*
564                                  * Indicates how much the file can grow.
565                                  */
566                                 NFSM_DISSECT(tl, u_int32_t *,
567                                     3 * NFSX_UNSIGNED);
568                                 limitby = fxdr_unsigned(int, *tl++);
569                                 switch (limitby) {
570                                 case NFSV4OPEN_LIMITSIZE:
571                                         ndp->nfsdl_sizelimit = fxdr_hyper(tl);
572                                         break;
573                                 case NFSV4OPEN_LIMITBLOCKS:
574                                         ndp->nfsdl_sizelimit =
575                                             fxdr_unsigned(u_int64_t, *tl++);
576                                         ndp->nfsdl_sizelimit *=
577                                             fxdr_unsigned(u_int64_t, *tl);
578                                         break;
579                                 default:
580                                         error = NFSERR_BADXDR;
581                                         goto nfsmout;
582                                 }
583                         } else {
584                                 ndp->nfsdl_flags = NFSCLDL_READ;
585                         }
586                         if (ret)
587                                 ndp->nfsdl_flags |= NFSCLDL_RECALL;
588                         error = nfsrv_dissectace(nd, &ndp->nfsdl_ace, &ret,
589                             &acesize, p);
590                         if (error)
591                                 goto nfsmout;
592                 } else if (deleg != NFSV4OPEN_DELEGATENONE) {
593                         error = NFSERR_BADXDR;
594                         goto nfsmout;
595                 }
596                 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
597                 error = nfsv4_loadattr(nd, NULL, &nfsva, NULL,
598                     NULL, 0, NULL, NULL, NULL, NULL, NULL, 0,
599                     NULL, NULL, NULL, p, cred);
600                 if (error)
601                         goto nfsmout;
602                 if (ndp != NULL) {
603                         ndp->nfsdl_change = nfsva.na_filerev;
604                         ndp->nfsdl_modtime = nfsva.na_mtime;
605                         ndp->nfsdl_flags |= NFSCLDL_MODTIMESET;
606                 }
607                 if (!reclaim && (rflags & NFSV4OPEN_RESULTCONFIRM)) {
608                     do {
609                         ret = nfsrpc_openconfirm(vp, newfhp, newfhlen, op,
610                             cred, p);
611                         if (ret == NFSERR_DELAY)
612                             (void) nfs_catnap(PZERO, ret, "nfs_open");
613                     } while (ret == NFSERR_DELAY);
614                     error = ret;
615                 }
616                 if ((rflags & NFSV4OPEN_LOCKTYPEPOSIX) ||
617                     nfscl_assumeposixlocks)
618                     op->nfso_posixlock = 1;
619                 else
620                     op->nfso_posixlock = 0;
621
622                 /*
623                  * If the server is handing out delegations, but we didn't
624                  * get one because an OpenConfirm was required, try the
625                  * Open again, to get a delegation. This is a harmless no-op,
626                  * from a server's point of view.
627                  */
628                 if (!reclaim && (rflags & NFSV4OPEN_RESULTCONFIRM) &&
629                     (op->nfso_own->nfsow_clp->nfsc_flags & NFSCLFLAGS_GOTDELEG)
630                     && !error && dp == NULL && ndp == NULL && !recursed) {
631                     do {
632                         ret = nfsrpc_openrpc(nmp, vp, nfhp, fhlen, newfhp,
633                             newfhlen, mode, op, name, namelen, &ndp, 0, 0x0,
634                             cred, p, syscred, 1);
635                         if (ret == NFSERR_DELAY)
636                             (void) nfs_catnap(PZERO, ret, "nfs_open2");
637                     } while (ret == NFSERR_DELAY);
638                     if (ret) {
639                         if (ndp != NULL) {
640                                 free(ndp, M_NFSCLDELEG);
641                                 ndp = NULL;
642                         }
643                         if (ret == NFSERR_STALECLIENTID ||
644                             ret == NFSERR_STALEDONTRECOVER ||
645                             ret == NFSERR_BADSESSION)
646                                 error = ret;
647                     }
648                 }
649         }
650         if (nd->nd_repstat != 0 && error == 0)
651                 error = nd->nd_repstat;
652         if (error == NFSERR_STALECLIENTID)
653                 nfscl_initiate_recovery(op->nfso_own->nfsow_clp);
654 nfsmout:
655         if (!error)
656                 *dpp = ndp;
657         else if (ndp != NULL)
658                 free(ndp, M_NFSCLDELEG);
659         mbuf_freem(nd->nd_mrep);
660         return (error);
661 }
662
663 /*
664  * open downgrade rpc
665  */
666 int
667 nfsrpc_opendowngrade(vnode_t vp, u_int32_t mode, struct nfsclopen *op,
668     struct ucred *cred, NFSPROC_T *p)
669 {
670         u_int32_t *tl;
671         struct nfsrv_descript nfsd, *nd = &nfsd;
672         int error;
673
674         NFSCL_REQSTART(nd, NFSPROC_OPENDOWNGRADE, vp);
675         NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID + 3 * NFSX_UNSIGNED);
676         if (NFSHASNFSV4N(VFSTONFS(vnode_mount(vp))))
677                 *tl++ = 0;
678         else
679                 *tl++ = op->nfso_stateid.seqid;
680         *tl++ = op->nfso_stateid.other[0];
681         *tl++ = op->nfso_stateid.other[1];
682         *tl++ = op->nfso_stateid.other[2];
683         *tl++ = txdr_unsigned(op->nfso_own->nfsow_seqid);
684         *tl++ = txdr_unsigned(mode & NFSV4OPEN_ACCESSBOTH);
685         *tl = txdr_unsigned((mode >> NFSLCK_SHIFT) & NFSV4OPEN_DENYBOTH);
686         error = nfscl_request(nd, vp, p, cred, NULL);
687         if (error)
688                 return (error);
689         NFSCL_INCRSEQID(op->nfso_own->nfsow_seqid, nd);
690         if (!nd->nd_repstat) {
691                 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID);
692                 op->nfso_stateid.seqid = *tl++;
693                 op->nfso_stateid.other[0] = *tl++;
694                 op->nfso_stateid.other[1] = *tl++;
695                 op->nfso_stateid.other[2] = *tl;
696         }
697         if (nd->nd_repstat && error == 0)
698                 error = nd->nd_repstat;
699         if (error == NFSERR_STALESTATEID)
700                 nfscl_initiate_recovery(op->nfso_own->nfsow_clp);
701 nfsmout:
702         mbuf_freem(nd->nd_mrep);
703         return (error);
704 }
705
706 /*
707  * V4 Close operation.
708  */
709 int
710 nfsrpc_close(vnode_t vp, int doclose, NFSPROC_T *p)
711 {
712         struct nfsclclient *clp;
713         int error;
714
715         if (vnode_vtype(vp) != VREG)
716                 return (0);
717         if (doclose)
718                 error = nfscl_doclose(vp, &clp, p);
719         else
720                 error = nfscl_getclose(vp, &clp);
721         if (error)
722                 return (error);
723
724         nfscl_clientrelease(clp);
725         return (0);
726 }
727
728 /*
729  * Close the open.
730  */
731 void
732 nfsrpc_doclose(struct nfsmount *nmp, struct nfsclopen *op, NFSPROC_T *p)
733 {
734         struct nfsrv_descript nfsd, *nd = &nfsd;
735         struct nfscllockowner *lp, *nlp;
736         struct nfscllock *lop, *nlop;
737         struct ucred *tcred;
738         u_int64_t off = 0, len = 0;
739         u_int32_t type = NFSV4LOCKT_READ;
740         int error, do_unlock, trycnt;
741
742         tcred = newnfs_getcred();
743         newnfs_copycred(&op->nfso_cred, tcred);
744         /*
745          * (Theoretically this could be done in the same
746          *  compound as the close, but having multiple
747          *  sequenced Ops in the same compound might be
748          *  too scary for some servers.)
749          */
750         if (op->nfso_posixlock) {
751                 off = 0;
752                 len = NFS64BITSSET;
753                 type = NFSV4LOCKT_READ;
754         }
755
756         /*
757          * Since this function is only called from VOP_INACTIVE(), no
758          * other thread will be manipulating this Open. As such, the
759          * lock lists are not being changed by other threads, so it should
760          * be safe to do this without locking.
761          */
762         LIST_FOREACH(lp, &op->nfso_lock, nfsl_list) {
763                 do_unlock = 1;
764                 LIST_FOREACH_SAFE(lop, &lp->nfsl_lock, nfslo_list, nlop) {
765                         if (op->nfso_posixlock == 0) {
766                                 off = lop->nfslo_first;
767                                 len = lop->nfslo_end - lop->nfslo_first;
768                                 if (lop->nfslo_type == F_WRLCK)
769                                         type = NFSV4LOCKT_WRITE;
770                                 else
771                                         type = NFSV4LOCKT_READ;
772                         }
773                         if (do_unlock) {
774                                 trycnt = 0;
775                                 do {
776                                         error = nfsrpc_locku(nd, nmp, lp, off,
777                                             len, type, tcred, p, 0);
778                                         if ((nd->nd_repstat == NFSERR_GRACE ||
779                                             nd->nd_repstat == NFSERR_DELAY) &&
780                                             error == 0)
781                                                 (void) nfs_catnap(PZERO,
782                                                     (int)nd->nd_repstat,
783                                                     "nfs_close");
784                                 } while ((nd->nd_repstat == NFSERR_GRACE ||
785                                     nd->nd_repstat == NFSERR_DELAY) &&
786                                     error == 0 && trycnt++ < 5);
787                                 if (op->nfso_posixlock)
788                                         do_unlock = 0;
789                         }
790                         nfscl_freelock(lop, 0);
791                 }
792                 /*
793                  * Do a ReleaseLockOwner.
794                  * The lock owner name nfsl_owner may be used by other opens for
795                  * other files but the lock_owner4 name that nfsrpc_rellockown()
796                  * puts on the wire has the file handle for this file appended
797                  * to it, so it can be done now.
798                  */
799                 (void)nfsrpc_rellockown(nmp, lp, lp->nfsl_open->nfso_fh,
800                     lp->nfsl_open->nfso_fhlen, tcred, p);
801         }
802
803         /*
804          * There could be other Opens for different files on the same
805          * OpenOwner, so locking is required.
806          */
807         NFSLOCKCLSTATE();
808         nfscl_lockexcl(&op->nfso_own->nfsow_rwlock, NFSCLSTATEMUTEXPTR);
809         NFSUNLOCKCLSTATE();
810         do {
811                 error = nfscl_tryclose(op, tcred, nmp, p);
812                 if (error == NFSERR_GRACE)
813                         (void) nfs_catnap(PZERO, error, "nfs_close");
814         } while (error == NFSERR_GRACE);
815         NFSLOCKCLSTATE();
816         nfscl_lockunlock(&op->nfso_own->nfsow_rwlock);
817
818         LIST_FOREACH_SAFE(lp, &op->nfso_lock, nfsl_list, nlp)
819                 nfscl_freelockowner(lp, 0);
820         nfscl_freeopen(op, 0);
821         NFSUNLOCKCLSTATE();
822         NFSFREECRED(tcred);
823 }
824
825 /*
826  * The actual Close RPC.
827  */
828 int
829 nfsrpc_closerpc(struct nfsrv_descript *nd, struct nfsmount *nmp,
830     struct nfsclopen *op, struct ucred *cred, NFSPROC_T *p,
831     int syscred)
832 {
833         u_int32_t *tl;
834         int error;
835
836         nfscl_reqstart(nd, NFSPROC_CLOSE, nmp, op->nfso_fh,
837             op->nfso_fhlen, NULL, NULL, 0, 0);
838         NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED + NFSX_STATEID);
839         *tl++ = txdr_unsigned(op->nfso_own->nfsow_seqid);
840         if (NFSHASNFSV4N(nmp))
841                 *tl++ = 0;
842         else
843                 *tl++ = op->nfso_stateid.seqid;
844         *tl++ = op->nfso_stateid.other[0];
845         *tl++ = op->nfso_stateid.other[1];
846         *tl = op->nfso_stateid.other[2];
847         if (syscred)
848                 nd->nd_flag |= ND_USEGSSNAME;
849         error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
850             NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
851         if (error)
852                 return (error);
853         NFSCL_INCRSEQID(op->nfso_own->nfsow_seqid, nd);
854         if (nd->nd_repstat == 0)
855                 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID);
856         error = nd->nd_repstat;
857         if (error == NFSERR_STALESTATEID)
858                 nfscl_initiate_recovery(op->nfso_own->nfsow_clp);
859 nfsmout:
860         mbuf_freem(nd->nd_mrep);
861         return (error);
862 }
863
864 /*
865  * V4 Open Confirm RPC.
866  */
867 int
868 nfsrpc_openconfirm(vnode_t vp, u_int8_t *nfhp, int fhlen,
869     struct nfsclopen *op, struct ucred *cred, NFSPROC_T *p)
870 {
871         u_int32_t *tl;
872         struct nfsrv_descript nfsd, *nd = &nfsd;
873         struct nfsmount *nmp;
874         int error;
875
876         nmp = VFSTONFS(vnode_mount(vp));
877         if (NFSHASNFSV4N(nmp))
878                 return (0);             /* No confirmation for NFSv4.1. */
879         nfscl_reqstart(nd, NFSPROC_OPENCONFIRM, nmp, nfhp, fhlen, NULL, NULL,
880             0, 0);
881         NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED + NFSX_STATEID);
882         *tl++ = op->nfso_stateid.seqid;
883         *tl++ = op->nfso_stateid.other[0];
884         *tl++ = op->nfso_stateid.other[1];
885         *tl++ = op->nfso_stateid.other[2];
886         *tl = txdr_unsigned(op->nfso_own->nfsow_seqid);
887         error = nfscl_request(nd, vp, p, cred, NULL);
888         if (error)
889                 return (error);
890         NFSCL_INCRSEQID(op->nfso_own->nfsow_seqid, nd);
891         if (!nd->nd_repstat) {
892                 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID);
893                 op->nfso_stateid.seqid = *tl++;
894                 op->nfso_stateid.other[0] = *tl++;
895                 op->nfso_stateid.other[1] = *tl++;
896                 op->nfso_stateid.other[2] = *tl;
897         }
898         error = nd->nd_repstat;
899         if (error == NFSERR_STALESTATEID)
900                 nfscl_initiate_recovery(op->nfso_own->nfsow_clp);
901 nfsmout:
902         mbuf_freem(nd->nd_mrep);
903         return (error);
904 }
905
906 /*
907  * Do the setclientid and setclientid confirm RPCs. Called from nfs_statfs()
908  * when a mount has just occurred and when the server replies NFSERR_EXPIRED.
909  */
910 int
911 nfsrpc_setclient(struct nfsmount *nmp, struct nfsclclient *clp, int reclaim,
912     struct ucred *cred, NFSPROC_T *p)
913 {
914         u_int32_t *tl;
915         struct nfsrv_descript nfsd;
916         struct nfsrv_descript *nd = &nfsd;
917         nfsattrbit_t attrbits;
918         u_int8_t *cp = NULL, *cp2, addr[INET6_ADDRSTRLEN + 9];
919         u_short port;
920         int error, isinet6 = 0, callblen;
921         nfsquad_t confirm;
922         u_int32_t lease;
923         static u_int32_t rev = 0;
924         struct nfsclds *dsp;
925         struct in6_addr a6;
926         struct nfsclsession *tsep;
927
928         if (nfsboottime.tv_sec == 0)
929                 NFSSETBOOTTIME(nfsboottime);
930         clp->nfsc_rev = rev++;
931         if (NFSHASNFSV4N(nmp)) {
932                 /*
933                  * Either there was no previous session or the
934                  * previous session has failed, so...
935                  * do an ExchangeID followed by the CreateSession.
936                  */
937                 error = nfsrpc_exchangeid(nmp, clp, &nmp->nm_sockreq,
938                     NFSV4EXCH_USEPNFSMDS | NFSV4EXCH_USENONPNFS, &dsp, cred, p);
939                 NFSCL_DEBUG(1, "aft exch=%d\n", error);
940                 if (error == 0)
941                         error = nfsrpc_createsession(nmp, &dsp->nfsclds_sess,
942                             &nmp->nm_sockreq,
943                             dsp->nfsclds_sess.nfsess_sequenceid, 1, cred, p);
944                 if (error == 0) {
945                         NFSLOCKMNT(nmp);
946                         /*
947                          * The old sessions cannot be safely free'd
948                          * here, since they may still be used by
949                          * in-progress RPCs.
950                          */
951                         tsep = NULL;
952                         if (TAILQ_FIRST(&nmp->nm_sess) != NULL)
953                                 tsep = NFSMNT_MDSSESSION(nmp);
954                         TAILQ_INSERT_HEAD(&nmp->nm_sess, dsp,
955                             nfsclds_list);
956                         /*
957                          * Wake up RPCs waiting for a slot on the
958                          * old session. These will then fail with
959                          * NFSERR_BADSESSION and be retried with the
960                          * new session by nfsv4_setsequence().
961                          * Also wakeup() processes waiting for the
962                          * new session.
963                          */
964                         if (tsep != NULL)
965                                 wakeup(&tsep->nfsess_slots);
966                         wakeup(&nmp->nm_sess);
967                         NFSUNLOCKMNT(nmp);
968                 } else
969                         nfscl_freenfsclds(dsp);
970                 NFSCL_DEBUG(1, "aft createsess=%d\n", error);
971                 if (error == 0 && reclaim == 0) {
972                         error = nfsrpc_reclaimcomplete(nmp, cred, p);
973                         NFSCL_DEBUG(1, "aft reclaimcomp=%d\n", error);
974                         if (error == NFSERR_COMPLETEALREADY ||
975                             error == NFSERR_NOTSUPP)
976                                 /* Ignore this error. */
977                                 error = 0;
978                 }
979                 return (error);
980         }
981
982         /*
983          * Allocate a single session structure for NFSv4.0, because some of
984          * the fields are used by NFSv4.0 although it doesn't do a session.
985          */
986         dsp = malloc(sizeof(struct nfsclds), M_NFSCLDS, M_WAITOK | M_ZERO);
987         mtx_init(&dsp->nfsclds_mtx, "nfsds", NULL, MTX_DEF);
988         mtx_init(&dsp->nfsclds_sess.nfsess_mtx, "nfssession", NULL, MTX_DEF);
989         NFSLOCKMNT(nmp);
990         TAILQ_INSERT_HEAD(&nmp->nm_sess, dsp, nfsclds_list);
991         tsep = NFSMNT_MDSSESSION(nmp);
992         NFSUNLOCKMNT(nmp);
993
994         nfscl_reqstart(nd, NFSPROC_SETCLIENTID, nmp, NULL, 0, NULL, NULL, 0, 0);
995         NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
996         *tl++ = txdr_unsigned(nfsboottime.tv_sec);
997         *tl = txdr_unsigned(clp->nfsc_rev);
998         (void) nfsm_strtom(nd, clp->nfsc_id, clp->nfsc_idlen);
999
1000         /*
1001          * set up the callback address
1002          */
1003         NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1004         *tl = txdr_unsigned(NFS_CALLBCKPROG);
1005         callblen = strlen(nfsv4_callbackaddr);
1006         if (callblen == 0)
1007                 cp = nfscl_getmyip(nmp, &a6, &isinet6);
1008         if (nfscl_enablecallb && nfs_numnfscbd > 0 &&
1009             (callblen > 0 || cp != NULL)) {
1010                 port = htons(nfsv4_cbport);
1011                 cp2 = (u_int8_t *)&port;
1012 #ifdef INET6
1013                 if ((callblen > 0 &&
1014                      strchr(nfsv4_callbackaddr, ':')) || isinet6) {
1015                         char ip6buf[INET6_ADDRSTRLEN], *ip6add;
1016
1017                         (void) nfsm_strtom(nd, "tcp6", 4);
1018                         if (callblen == 0) {
1019                                 ip6_sprintf(ip6buf, (struct in6_addr *)cp);
1020                                 ip6add = ip6buf;
1021                         } else {
1022                                 ip6add = nfsv4_callbackaddr;
1023                         }
1024                         snprintf(addr, INET6_ADDRSTRLEN + 9, "%s.%d.%d",
1025                             ip6add, cp2[0], cp2[1]);
1026                 } else
1027 #endif
1028                 {
1029                         (void) nfsm_strtom(nd, "tcp", 3);
1030                         if (callblen == 0)
1031                                 snprintf(addr, INET6_ADDRSTRLEN + 9,
1032                                     "%d.%d.%d.%d.%d.%d", cp[0], cp[1],
1033                                     cp[2], cp[3], cp2[0], cp2[1]);
1034                         else
1035                                 snprintf(addr, INET6_ADDRSTRLEN + 9,
1036                                     "%s.%d.%d", nfsv4_callbackaddr,
1037                                     cp2[0], cp2[1]);
1038                 }
1039                 (void) nfsm_strtom(nd, addr, strlen(addr));
1040         } else {
1041                 (void) nfsm_strtom(nd, "tcp", 3);
1042                 (void) nfsm_strtom(nd, "0.0.0.0.0.0", 11);
1043         }
1044         NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1045         *tl = txdr_unsigned(clp->nfsc_cbident);
1046         nd->nd_flag |= ND_USEGSSNAME;
1047         error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
1048                 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
1049         if (error)
1050                 return (error);
1051         if (nd->nd_repstat == 0) {
1052             NFSM_DISSECT(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1053             tsep->nfsess_clientid.lval[0] = *tl++;
1054             tsep->nfsess_clientid.lval[1] = *tl++;
1055             confirm.lval[0] = *tl++;
1056             confirm.lval[1] = *tl;
1057             mbuf_freem(nd->nd_mrep);
1058             nd->nd_mrep = NULL;
1059
1060             /*
1061              * and confirm it.
1062              */
1063             nfscl_reqstart(nd, NFSPROC_SETCLIENTIDCFRM, nmp, NULL, 0, NULL,
1064                 NULL, 0, 0);
1065             NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1066             *tl++ = tsep->nfsess_clientid.lval[0];
1067             *tl++ = tsep->nfsess_clientid.lval[1];
1068             *tl++ = confirm.lval[0];
1069             *tl = confirm.lval[1];
1070             nd->nd_flag |= ND_USEGSSNAME;
1071             error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p,
1072                 cred, NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
1073             if (error)
1074                 return (error);
1075             mbuf_freem(nd->nd_mrep);
1076             nd->nd_mrep = NULL;
1077             if (nd->nd_repstat == 0) {
1078                 nfscl_reqstart(nd, NFSPROC_GETATTR, nmp, nmp->nm_fh,
1079                     nmp->nm_fhsize, NULL, NULL, 0, 0);
1080                 NFSZERO_ATTRBIT(&attrbits);
1081                 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_LEASETIME);
1082                 (void) nfsrv_putattrbit(nd, &attrbits);
1083                 nd->nd_flag |= ND_USEGSSNAME;
1084                 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p,
1085                     cred, NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
1086                 if (error)
1087                     return (error);
1088                 if (nd->nd_repstat == 0) {
1089                     error = nfsv4_loadattr(nd, NULL, NULL, NULL, NULL, 0, NULL,
1090                         NULL, NULL, NULL, NULL, 0, NULL, &lease, NULL, p, cred);
1091                     if (error)
1092                         goto nfsmout;
1093                     clp->nfsc_renew = NFSCL_RENEW(lease);
1094                     clp->nfsc_expire = NFSD_MONOSEC + clp->nfsc_renew;
1095                     clp->nfsc_clientidrev++;
1096                     if (clp->nfsc_clientidrev == 0)
1097                         clp->nfsc_clientidrev++;
1098                 }
1099             }
1100         }
1101         error = nd->nd_repstat;
1102 nfsmout:
1103         mbuf_freem(nd->nd_mrep);
1104         return (error);
1105 }
1106
1107 /*
1108  * nfs getattr call.
1109  */
1110 int
1111 nfsrpc_getattr(vnode_t vp, struct ucred *cred, NFSPROC_T *p,
1112     struct nfsvattr *nap, void *stuff)
1113 {
1114         struct nfsrv_descript nfsd, *nd = &nfsd;
1115         int error;
1116         nfsattrbit_t attrbits;
1117         
1118         NFSCL_REQSTART(nd, NFSPROC_GETATTR, vp);
1119         if (nd->nd_flag & ND_NFSV4) {
1120                 NFSGETATTR_ATTRBIT(&attrbits);
1121                 (void) nfsrv_putattrbit(nd, &attrbits);
1122         }
1123         error = nfscl_request(nd, vp, p, cred, stuff);
1124         if (error)
1125                 return (error);
1126         if (!nd->nd_repstat)
1127                 error = nfsm_loadattr(nd, nap);
1128         else
1129                 error = nd->nd_repstat;
1130         mbuf_freem(nd->nd_mrep);
1131         return (error);
1132 }
1133
1134 /*
1135  * nfs getattr call with non-vnode arguemnts.
1136  */
1137 int
1138 nfsrpc_getattrnovp(struct nfsmount *nmp, u_int8_t *fhp, int fhlen, int syscred,
1139     struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, u_int64_t *xidp,
1140     uint32_t *leasep)
1141 {
1142         struct nfsrv_descript nfsd, *nd = &nfsd;
1143         int error, vers = NFS_VER2;
1144         nfsattrbit_t attrbits;
1145         
1146         nfscl_reqstart(nd, NFSPROC_GETATTR, nmp, fhp, fhlen, NULL, NULL, 0, 0);
1147         if (nd->nd_flag & ND_NFSV4) {
1148                 vers = NFS_VER4;
1149                 NFSGETATTR_ATTRBIT(&attrbits);
1150                 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_LEASETIME);
1151                 (void) nfsrv_putattrbit(nd, &attrbits);
1152         } else if (nd->nd_flag & ND_NFSV3) {
1153                 vers = NFS_VER3;
1154         }
1155         if (syscred)
1156                 nd->nd_flag |= ND_USEGSSNAME;
1157         error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
1158             NFS_PROG, vers, NULL, 1, xidp, NULL);
1159         if (error)
1160                 return (error);
1161         if (nd->nd_repstat == 0) {
1162                 if ((nd->nd_flag & ND_NFSV4) != 0)
1163                         error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0,
1164                             NULL, NULL, NULL, NULL, NULL, 0, NULL, leasep, NULL,
1165                             NULL, NULL);
1166                 else
1167                         error = nfsm_loadattr(nd, nap);
1168         } else
1169                 error = nd->nd_repstat;
1170         mbuf_freem(nd->nd_mrep);
1171         return (error);
1172 }
1173
1174 /*
1175  * Do an nfs setattr operation.
1176  */
1177 int
1178 nfsrpc_setattr(vnode_t vp, struct vattr *vap, NFSACL_T *aclp,
1179     struct ucred *cred, NFSPROC_T *p, struct nfsvattr *rnap, int *attrflagp,
1180     void *stuff)
1181 {
1182         int error, expireret = 0, openerr, retrycnt;
1183         u_int32_t clidrev = 0, mode;
1184         struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
1185         struct nfsfh *nfhp;
1186         nfsv4stateid_t stateid;
1187         void *lckp;
1188
1189         if (nmp->nm_clp != NULL)
1190                 clidrev = nmp->nm_clp->nfsc_clientidrev;
1191         if (vap != NULL && NFSATTRISSET(u_quad_t, vap, va_size))
1192                 mode = NFSV4OPEN_ACCESSWRITE;
1193         else
1194                 mode = NFSV4OPEN_ACCESSREAD;
1195         retrycnt = 0;
1196         do {
1197                 lckp = NULL;
1198                 openerr = 1;
1199                 if (NFSHASNFSV4(nmp)) {
1200                         nfhp = VTONFS(vp)->n_fhp;
1201                         error = nfscl_getstateid(vp, nfhp->nfh_fh,
1202                             nfhp->nfh_len, mode, 0, cred, p, &stateid, &lckp);
1203                         if (error && vnode_vtype(vp) == VREG &&
1204                             (mode == NFSV4OPEN_ACCESSWRITE ||
1205                              nfstest_openallsetattr)) {
1206                                 /*
1207                                  * No Open stateid, so try and open the file
1208                                  * now.
1209                                  */
1210                                 if (mode == NFSV4OPEN_ACCESSWRITE)
1211                                         openerr = nfsrpc_open(vp, FWRITE, cred,
1212                                             p);
1213                                 else
1214                                         openerr = nfsrpc_open(vp, FREAD, cred,
1215                                             p);
1216                                 if (!openerr)
1217                                         (void) nfscl_getstateid(vp,
1218                                             nfhp->nfh_fh, nfhp->nfh_len,
1219                                             mode, 0, cred, p, &stateid, &lckp);
1220                         }
1221                 }
1222                 if (vap != NULL)
1223                         error = nfsrpc_setattrrpc(vp, vap, &stateid, cred, p,
1224                             rnap, attrflagp, stuff);
1225                 else
1226                         error = nfsrpc_setaclrpc(vp, cred, p, aclp, &stateid,
1227                             stuff);
1228                 if (error == NFSERR_OPENMODE && mode == NFSV4OPEN_ACCESSREAD) {
1229                         NFSLOCKMNT(nmp);
1230                         nmp->nm_state |= NFSSTA_OPENMODE;
1231                         NFSUNLOCKMNT(nmp);
1232                 }
1233                 if (error == NFSERR_STALESTATEID)
1234                         nfscl_initiate_recovery(nmp->nm_clp);
1235                 if (lckp != NULL)
1236                         nfscl_lockderef(lckp);
1237                 if (!openerr)
1238                         (void) nfsrpc_close(vp, 0, p);
1239                 if (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
1240                     error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
1241                     error == NFSERR_OLDSTATEID || error == NFSERR_BADSESSION) {
1242                         (void) nfs_catnap(PZERO, error, "nfs_setattr");
1243                 } else if ((error == NFSERR_EXPIRED ||
1244                     error == NFSERR_BADSTATEID) && clidrev != 0) {
1245                         expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
1246                 }
1247                 retrycnt++;
1248         } while (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
1249             error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
1250             error == NFSERR_BADSESSION ||
1251             (error == NFSERR_OLDSTATEID && retrycnt < 20) ||
1252             ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
1253              expireret == 0 && clidrev != 0 && retrycnt < 4) ||
1254             (error == NFSERR_OPENMODE && mode == NFSV4OPEN_ACCESSREAD &&
1255              retrycnt < 4));
1256         if (error && retrycnt >= 4)
1257                 error = EIO;
1258         return (error);
1259 }
1260
1261 static int
1262 nfsrpc_setattrrpc(vnode_t vp, struct vattr *vap,
1263     nfsv4stateid_t *stateidp, struct ucred *cred, NFSPROC_T *p,
1264     struct nfsvattr *rnap, int *attrflagp, void *stuff)
1265 {
1266         u_int32_t *tl;
1267         struct nfsrv_descript nfsd, *nd = &nfsd;
1268         int error;
1269         nfsattrbit_t attrbits;
1270
1271         *attrflagp = 0;
1272         NFSCL_REQSTART(nd, NFSPROC_SETATTR, vp);
1273         if (nd->nd_flag & ND_NFSV4)
1274                 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID);
1275         vap->va_type = vnode_vtype(vp);
1276         nfscl_fillsattr(nd, vap, vp, NFSSATTR_FULL, 0);
1277         if (nd->nd_flag & ND_NFSV3) {
1278                 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1279                 *tl = newnfs_false;
1280         } else if (nd->nd_flag & ND_NFSV4) {
1281                 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1282                 *tl = txdr_unsigned(NFSV4OP_GETATTR);
1283                 NFSGETATTR_ATTRBIT(&attrbits);
1284                 (void) nfsrv_putattrbit(nd, &attrbits);
1285         }
1286         error = nfscl_request(nd, vp, p, cred, stuff);
1287         if (error)
1288                 return (error);
1289         if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4))
1290                 error = nfscl_wcc_data(nd, vp, rnap, attrflagp, NULL, stuff);
1291         if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) == ND_NFSV4 && !error)
1292                 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
1293         if (!(nd->nd_flag & ND_NFSV3) && !nd->nd_repstat && !error)
1294                 error = nfscl_postop_attr(nd, rnap, attrflagp, stuff);
1295         mbuf_freem(nd->nd_mrep);
1296         if (nd->nd_repstat && !error)
1297                 error = nd->nd_repstat;
1298         return (error);
1299 }
1300
1301 /*
1302  * nfs lookup rpc
1303  */
1304 int
1305 nfsrpc_lookup(vnode_t dvp, char *name, int len, struct ucred *cred,
1306     NFSPROC_T *p, struct nfsvattr *dnap, struct nfsvattr *nap,
1307     struct nfsfh **nfhpp, int *attrflagp, int *dattrflagp, void *stuff)
1308 {
1309         u_int32_t *tl;
1310         struct nfsrv_descript nfsd, *nd = &nfsd;
1311         struct nfsmount *nmp;
1312         struct nfsnode *np;
1313         struct nfsfh *nfhp;
1314         nfsattrbit_t attrbits;
1315         int error = 0, lookupp = 0;
1316
1317         *attrflagp = 0;
1318         *dattrflagp = 0;
1319         if (vnode_vtype(dvp) != VDIR)
1320                 return (ENOTDIR);
1321         nmp = VFSTONFS(vnode_mount(dvp));
1322         if (len > NFS_MAXNAMLEN)
1323                 return (ENAMETOOLONG);
1324         if (NFSHASNFSV4(nmp) && len == 1 &&
1325                 name[0] == '.') {
1326                 /*
1327                  * Just return the current dir's fh.
1328                  */
1329                 np = VTONFS(dvp);
1330                 nfhp = malloc(sizeof (struct nfsfh) +
1331                         np->n_fhp->nfh_len, M_NFSFH, M_WAITOK);
1332                 nfhp->nfh_len = np->n_fhp->nfh_len;
1333                 NFSBCOPY(np->n_fhp->nfh_fh, nfhp->nfh_fh, nfhp->nfh_len);
1334                 *nfhpp = nfhp;
1335                 return (0);
1336         }
1337         if (NFSHASNFSV4(nmp) && len == 2 &&
1338                 name[0] == '.' && name[1] == '.') {
1339                 lookupp = 1;
1340                 NFSCL_REQSTART(nd, NFSPROC_LOOKUPP, dvp);
1341         } else {
1342                 NFSCL_REQSTART(nd, NFSPROC_LOOKUP, dvp);
1343                 (void) nfsm_strtom(nd, name, len);
1344         }
1345         if (nd->nd_flag & ND_NFSV4) {
1346                 NFSGETATTR_ATTRBIT(&attrbits);
1347                 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1348                 *tl++ = txdr_unsigned(NFSV4OP_GETFH);
1349                 *tl = txdr_unsigned(NFSV4OP_GETATTR);
1350                 (void) nfsrv_putattrbit(nd, &attrbits);
1351         }
1352         error = nfscl_request(nd, dvp, p, cred, stuff);
1353         if (error)
1354                 return (error);
1355         if (nd->nd_repstat) {
1356                 /*
1357                  * When an NFSv4 Lookupp returns ENOENT, it means that
1358                  * the lookup is at the root of an fs, so return this dir.
1359                  */
1360                 if (nd->nd_repstat == NFSERR_NOENT && lookupp) {
1361                     np = VTONFS(dvp);
1362                     nfhp = malloc(sizeof (struct nfsfh) +
1363                         np->n_fhp->nfh_len, M_NFSFH, M_WAITOK);
1364                     nfhp->nfh_len = np->n_fhp->nfh_len;
1365                     NFSBCOPY(np->n_fhp->nfh_fh, nfhp->nfh_fh, nfhp->nfh_len);
1366                     *nfhpp = nfhp;
1367                     mbuf_freem(nd->nd_mrep);
1368                     return (0);
1369                 }
1370                 if (nd->nd_flag & ND_NFSV3)
1371                     error = nfscl_postop_attr(nd, dnap, dattrflagp, stuff);
1372                 else if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) ==
1373                     ND_NFSV4) {
1374                         /* Load the directory attributes. */
1375                         error = nfsm_loadattr(nd, dnap);
1376                         if (error == 0)
1377                                 *dattrflagp = 1;
1378                 }
1379                 goto nfsmout;
1380         }
1381         if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) == ND_NFSV4) {
1382                 /* Load the directory attributes. */
1383                 error = nfsm_loadattr(nd, dnap);
1384                 if (error != 0)
1385                         goto nfsmout;
1386                 *dattrflagp = 1;
1387                 /* Skip over the Lookup and GetFH operation status values. */
1388                 NFSM_DISSECT(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1389         }
1390         error = nfsm_getfh(nd, nfhpp);
1391         if (error)
1392                 goto nfsmout;
1393
1394         error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
1395         if ((nd->nd_flag & ND_NFSV3) && !error)
1396                 error = nfscl_postop_attr(nd, dnap, dattrflagp, stuff);
1397 nfsmout:
1398         mbuf_freem(nd->nd_mrep);
1399         if (!error && nd->nd_repstat)
1400                 error = nd->nd_repstat;
1401         return (error);
1402 }
1403
1404 /*
1405  * Do a readlink rpc.
1406  */
1407 int
1408 nfsrpc_readlink(vnode_t vp, struct uio *uiop, struct ucred *cred,
1409     NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, void *stuff)
1410 {
1411         u_int32_t *tl;
1412         struct nfsrv_descript nfsd, *nd = &nfsd;
1413         struct nfsnode *np = VTONFS(vp);
1414         nfsattrbit_t attrbits;
1415         int error, len, cangetattr = 1;
1416
1417         *attrflagp = 0;
1418         NFSCL_REQSTART(nd, NFSPROC_READLINK, vp);
1419         if (nd->nd_flag & ND_NFSV4) {
1420                 /*
1421                  * And do a Getattr op.
1422                  */
1423                 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1424                 *tl = txdr_unsigned(NFSV4OP_GETATTR);
1425                 NFSGETATTR_ATTRBIT(&attrbits);
1426                 (void) nfsrv_putattrbit(nd, &attrbits);
1427         }
1428         error = nfscl_request(nd, vp, p, cred, stuff);
1429         if (error)
1430                 return (error);
1431         if (nd->nd_flag & ND_NFSV3)
1432                 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
1433         if (!nd->nd_repstat && !error) {
1434                 NFSM_STRSIZ(len, NFS_MAXPATHLEN);
1435                 /*
1436                  * This seems weird to me, but must have been added to
1437                  * FreeBSD for some reason. The only thing I can think of
1438                  * is that there was/is some server that replies with
1439                  * more link data than it should?
1440                  */
1441                 if (len == NFS_MAXPATHLEN) {
1442                         NFSLOCKNODE(np);
1443                         if (np->n_size > 0 && np->n_size < NFS_MAXPATHLEN) {
1444                                 len = np->n_size;
1445                                 cangetattr = 0;
1446                         }
1447                         NFSUNLOCKNODE(np);
1448                 }
1449                 error = nfsm_mbufuio(nd, uiop, len);
1450                 if ((nd->nd_flag & ND_NFSV4) && !error && cangetattr)
1451                         error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
1452         }
1453         if (nd->nd_repstat && !error)
1454                 error = nd->nd_repstat;
1455 nfsmout:
1456         mbuf_freem(nd->nd_mrep);
1457         return (error);
1458 }
1459
1460 /*
1461  * Read operation.
1462  */
1463 int
1464 nfsrpc_read(vnode_t vp, struct uio *uiop, struct ucred *cred,
1465     NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, void *stuff)
1466 {
1467         int error, expireret = 0, retrycnt;
1468         u_int32_t clidrev = 0;
1469         struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
1470         struct nfsnode *np = VTONFS(vp);
1471         struct ucred *newcred;
1472         struct nfsfh *nfhp = NULL;
1473         nfsv4stateid_t stateid;
1474         void *lckp;
1475
1476         if (nmp->nm_clp != NULL)
1477                 clidrev = nmp->nm_clp->nfsc_clientidrev;
1478         newcred = cred;
1479         if (NFSHASNFSV4(nmp)) {
1480                 nfhp = np->n_fhp;
1481                 newcred = NFSNEWCRED(cred);
1482         }
1483         retrycnt = 0;
1484         do {
1485                 lckp = NULL;
1486                 if (NFSHASNFSV4(nmp))
1487                         (void)nfscl_getstateid(vp, nfhp->nfh_fh, nfhp->nfh_len,
1488                             NFSV4OPEN_ACCESSREAD, 0, newcred, p, &stateid,
1489                             &lckp);
1490                 error = nfsrpc_readrpc(vp, uiop, newcred, &stateid, p, nap,
1491                     attrflagp, stuff);
1492                 if (error == NFSERR_OPENMODE) {
1493                         NFSLOCKMNT(nmp);
1494                         nmp->nm_state |= NFSSTA_OPENMODE;
1495                         NFSUNLOCKMNT(nmp);
1496                 }
1497                 if (error == NFSERR_STALESTATEID)
1498                         nfscl_initiate_recovery(nmp->nm_clp);
1499                 if (lckp != NULL)
1500                         nfscl_lockderef(lckp);
1501                 if (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
1502                     error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
1503                     error == NFSERR_OLDSTATEID || error == NFSERR_BADSESSION) {
1504                         (void) nfs_catnap(PZERO, error, "nfs_read");
1505                 } else if ((error == NFSERR_EXPIRED ||
1506                     error == NFSERR_BADSTATEID) && clidrev != 0) {
1507                         expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
1508                 }
1509                 retrycnt++;
1510         } while (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
1511             error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
1512             error == NFSERR_BADSESSION ||
1513             (error == NFSERR_OLDSTATEID && retrycnt < 20) ||
1514             ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
1515              expireret == 0 && clidrev != 0 && retrycnt < 4) ||
1516             (error == NFSERR_OPENMODE && retrycnt < 4));
1517         if (error && retrycnt >= 4)
1518                 error = EIO;
1519         if (NFSHASNFSV4(nmp))
1520                 NFSFREECRED(newcred);
1521         return (error);
1522 }
1523
1524 /*
1525  * The actual read RPC.
1526  */
1527 static int
1528 nfsrpc_readrpc(vnode_t vp, struct uio *uiop, struct ucred *cred,
1529     nfsv4stateid_t *stateidp, NFSPROC_T *p, struct nfsvattr *nap,
1530     int *attrflagp, void *stuff)
1531 {
1532         u_int32_t *tl;
1533         int error = 0, len, retlen, tsiz, eof = 0;
1534         struct nfsrv_descript nfsd;
1535         struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
1536         struct nfsrv_descript *nd = &nfsd;
1537         int rsize;
1538         off_t tmp_off;
1539
1540         *attrflagp = 0;
1541         tsiz = uio_uio_resid(uiop);
1542         tmp_off = uiop->uio_offset + tsiz;
1543         NFSLOCKMNT(nmp);
1544         if (tmp_off > nmp->nm_maxfilesize || tmp_off < uiop->uio_offset) {
1545                 NFSUNLOCKMNT(nmp);
1546                 return (EFBIG);
1547         }
1548         rsize = nmp->nm_rsize;
1549         NFSUNLOCKMNT(nmp);
1550         nd->nd_mrep = NULL;
1551         while (tsiz > 0) {
1552                 *attrflagp = 0;
1553                 len = (tsiz > rsize) ? rsize : tsiz;
1554                 NFSCL_REQSTART(nd, NFSPROC_READ, vp);
1555                 if (nd->nd_flag & ND_NFSV4)
1556                         nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID);
1557                 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED * 3);
1558                 if (nd->nd_flag & ND_NFSV2) {
1559                         *tl++ = txdr_unsigned(uiop->uio_offset);
1560                         *tl++ = txdr_unsigned(len);
1561                         *tl = 0;
1562                 } else {
1563                         txdr_hyper(uiop->uio_offset, tl);
1564                         *(tl + 2) = txdr_unsigned(len);
1565                 }
1566                 /*
1567                  * Since I can't do a Getattr for NFSv4 for Write, there
1568                  * doesn't seem any point in doing one here, either.
1569                  * (See the comment in nfsrpc_writerpc() for more info.)
1570                  */
1571                 error = nfscl_request(nd, vp, p, cred, stuff);
1572                 if (error)
1573                         return (error);
1574                 if (nd->nd_flag & ND_NFSV3) {
1575                         error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
1576                 } else if (!nd->nd_repstat && (nd->nd_flag & ND_NFSV2)) {
1577                         error = nfsm_loadattr(nd, nap);
1578                         if (!error)
1579                                 *attrflagp = 1;
1580                 }
1581                 if (nd->nd_repstat || error) {
1582                         if (!error)
1583                                 error = nd->nd_repstat;
1584                         goto nfsmout;
1585                 }
1586                 if (nd->nd_flag & ND_NFSV3) {
1587                         NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1588                         eof = fxdr_unsigned(int, *(tl + 1));
1589                 } else if (nd->nd_flag & ND_NFSV4) {
1590                         NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
1591                         eof = fxdr_unsigned(int, *tl);
1592                 }
1593                 NFSM_STRSIZ(retlen, len);
1594                 error = nfsm_mbufuio(nd, uiop, retlen);
1595                 if (error)
1596                         goto nfsmout;
1597                 mbuf_freem(nd->nd_mrep);
1598                 nd->nd_mrep = NULL;
1599                 tsiz -= retlen;
1600                 if (!(nd->nd_flag & ND_NFSV2)) {
1601                         if (eof || retlen == 0)
1602                                 tsiz = 0;
1603                 } else if (retlen < len)
1604                         tsiz = 0;
1605         }
1606         return (0);
1607 nfsmout:
1608         if (nd->nd_mrep != NULL)
1609                 mbuf_freem(nd->nd_mrep);
1610         return (error);
1611 }
1612
1613 /*
1614  * nfs write operation
1615  * When called_from_strategy != 0, it should return EIO for an error that
1616  * indicates recovery is in progress, so that the buffer will be left
1617  * dirty and be written back to the server later. If it loops around,
1618  * the recovery thread could get stuck waiting for the buffer and recovery
1619  * will then deadlock.
1620  */
1621 int
1622 nfsrpc_write(vnode_t vp, struct uio *uiop, int *iomode, int *must_commit,
1623     struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp,
1624     void *stuff, int called_from_strategy)
1625 {
1626         int error, expireret = 0, retrycnt, nostateid;
1627         u_int32_t clidrev = 0;
1628         struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
1629         struct nfsnode *np = VTONFS(vp);
1630         struct ucred *newcred;
1631         struct nfsfh *nfhp = NULL;
1632         nfsv4stateid_t stateid;
1633         void *lckp;
1634
1635         *must_commit = 0;
1636         if (nmp->nm_clp != NULL)
1637                 clidrev = nmp->nm_clp->nfsc_clientidrev;
1638         newcred = cred;
1639         if (NFSHASNFSV4(nmp)) {
1640                 newcred = NFSNEWCRED(cred);
1641                 nfhp = np->n_fhp;
1642         }
1643         retrycnt = 0;
1644         do {
1645                 lckp = NULL;
1646                 nostateid = 0;
1647                 if (NFSHASNFSV4(nmp)) {
1648                         (void)nfscl_getstateid(vp, nfhp->nfh_fh, nfhp->nfh_len,
1649                             NFSV4OPEN_ACCESSWRITE, 0, newcred, p, &stateid,
1650                             &lckp);
1651                         if (stateid.other[0] == 0 && stateid.other[1] == 0 &&
1652                             stateid.other[2] == 0) {
1653                                 nostateid = 1;
1654                                 NFSCL_DEBUG(1, "stateid0 in write\n");
1655                         }
1656                 }
1657
1658                 /*
1659                  * If there is no stateid for NFSv4, it means this is an
1660                  * extraneous write after close. Basically a poorly
1661                  * implemented buffer cache. Just don't do the write.
1662                  */
1663                 if (nostateid)
1664                         error = 0;
1665                 else
1666                         error = nfsrpc_writerpc(vp, uiop, iomode, must_commit,
1667                             newcred, &stateid, p, nap, attrflagp, stuff);
1668                 if (error == NFSERR_STALESTATEID)
1669                         nfscl_initiate_recovery(nmp->nm_clp);
1670                 if (lckp != NULL)
1671                         nfscl_lockderef(lckp);
1672                 if (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
1673                     error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
1674                     error == NFSERR_OLDSTATEID || error == NFSERR_BADSESSION) {
1675                         (void) nfs_catnap(PZERO, error, "nfs_write");
1676                 } else if ((error == NFSERR_EXPIRED ||
1677                     error == NFSERR_BADSTATEID) && clidrev != 0) {
1678                         expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
1679                 }
1680                 retrycnt++;
1681         } while (error == NFSERR_GRACE || error == NFSERR_DELAY ||
1682             ((error == NFSERR_STALESTATEID || error == NFSERR_BADSESSION ||
1683               error == NFSERR_STALEDONTRECOVER) && called_from_strategy == 0) ||
1684             (error == NFSERR_OLDSTATEID && retrycnt < 20) ||
1685             ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
1686              expireret == 0 && clidrev != 0 && retrycnt < 4));
1687         if (error != 0 && (retrycnt >= 4 ||
1688             ((error == NFSERR_STALESTATEID || error == NFSERR_BADSESSION ||
1689               error == NFSERR_STALEDONTRECOVER) && called_from_strategy != 0)))
1690                 error = EIO;
1691         if (NFSHASNFSV4(nmp))
1692                 NFSFREECRED(newcred);
1693         return (error);
1694 }
1695
1696 /*
1697  * The actual write RPC.
1698  */
1699 static int
1700 nfsrpc_writerpc(vnode_t vp, struct uio *uiop, int *iomode,
1701     int *must_commit, struct ucred *cred, nfsv4stateid_t *stateidp,
1702     NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, void *stuff)
1703 {
1704         u_int32_t *tl;
1705         struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
1706         struct nfsnode *np = VTONFS(vp);
1707         int error = 0, len, tsiz, rlen, commit, committed = NFSWRITE_FILESYNC;
1708         int wccflag = 0, wsize;
1709         int32_t backup;
1710         struct nfsrv_descript nfsd;
1711         struct nfsrv_descript *nd = &nfsd;
1712         nfsattrbit_t attrbits;
1713         off_t tmp_off;
1714
1715         KASSERT(uiop->uio_iovcnt == 1, ("nfs: writerpc iovcnt > 1"));
1716         *attrflagp = 0;
1717         tsiz = uio_uio_resid(uiop);
1718         tmp_off = uiop->uio_offset + tsiz;
1719         NFSLOCKMNT(nmp);
1720         if (tmp_off > nmp->nm_maxfilesize || tmp_off < uiop->uio_offset) {
1721                 NFSUNLOCKMNT(nmp);
1722                 return (EFBIG);
1723         }
1724         wsize = nmp->nm_wsize;
1725         NFSUNLOCKMNT(nmp);
1726         nd->nd_mrep = NULL;     /* NFSv2 sometimes does a write with */
1727         nd->nd_repstat = 0;     /* uio_resid == 0, so the while is not done */
1728         while (tsiz > 0) {
1729                 *attrflagp = 0;
1730                 len = (tsiz > wsize) ? wsize : tsiz;
1731                 NFSCL_REQSTART(nd, NFSPROC_WRITE, vp);
1732                 if (nd->nd_flag & ND_NFSV4) {
1733                         nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID);
1734                         NFSM_BUILD(tl, u_int32_t *, NFSX_HYPER+2*NFSX_UNSIGNED);
1735                         txdr_hyper(uiop->uio_offset, tl);
1736                         tl += 2;
1737                         *tl++ = txdr_unsigned(*iomode);
1738                         *tl = txdr_unsigned(len);
1739                 } else if (nd->nd_flag & ND_NFSV3) {
1740                         NFSM_BUILD(tl, u_int32_t *, NFSX_HYPER+3*NFSX_UNSIGNED);
1741                         txdr_hyper(uiop->uio_offset, tl);
1742                         tl += 2;
1743                         *tl++ = txdr_unsigned(len);
1744                         *tl++ = txdr_unsigned(*iomode);
1745                         *tl = txdr_unsigned(len);
1746                 } else {
1747                         u_int32_t x;
1748
1749                         NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1750                         /*
1751                          * Not sure why someone changed this, since the
1752                          * RFC clearly states that "beginoffset" and
1753                          * "totalcount" are ignored, but it wouldn't
1754                          * surprise me if there's a busted server out there.
1755                          */
1756                         /* Set both "begin" and "current" to non-garbage. */
1757                         x = txdr_unsigned((u_int32_t)uiop->uio_offset);
1758                         *tl++ = x;      /* "begin offset" */
1759                         *tl++ = x;      /* "current offset" */
1760                         x = txdr_unsigned(len);
1761                         *tl++ = x;      /* total to this offset */
1762                         *tl = x;        /* size of this write */
1763
1764                 }
1765                 nfsm_uiombuf(nd, uiop, len);
1766                 /*
1767                  * Although it is tempting to do a normal Getattr Op in the
1768                  * NFSv4 compound, the result can be a nearly hung client
1769                  * system if the Getattr asks for Owner and/or OwnerGroup.
1770                  * It occurs when the client can't map either the Owner or
1771                  * Owner_group name in the Getattr reply to a uid/gid. When
1772                  * there is a cache miss, the kernel does an upcall to the
1773                  * nfsuserd. Then, it can try and read the local /etc/passwd
1774                  * or /etc/group file. It can then block in getnewbuf(),
1775                  * waiting for dirty writes to be pushed to the NFS server.
1776                  * The only reason this doesn't result in a complete
1777                  * deadlock, is that the upcall times out and allows
1778                  * the write to complete. However, progress is so slow
1779                  * that it might just as well be deadlocked.
1780                  * As such, we get the rest of the attributes, but not
1781                  * Owner or Owner_group.
1782                  * nb: nfscl_loadattrcache() needs to be told that these
1783                  *     partial attributes from a write rpc are being
1784                  *     passed in, via a argument flag.
1785                  */
1786                 if (nd->nd_flag & ND_NFSV4) {
1787                         NFSWRITEGETATTR_ATTRBIT(&attrbits);
1788                         NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1789                         *tl = txdr_unsigned(NFSV4OP_GETATTR);
1790                         (void) nfsrv_putattrbit(nd, &attrbits);
1791                 }
1792                 error = nfscl_request(nd, vp, p, cred, stuff);
1793                 if (error)
1794                         return (error);
1795                 if (nd->nd_repstat) {
1796                         /*
1797                          * In case the rpc gets retried, roll
1798                          * the uio fileds changed by nfsm_uiombuf()
1799                          * back.
1800                          */
1801                         uiop->uio_offset -= len;
1802                         uio_uio_resid_add(uiop, len);
1803                         uio_iov_base_add(uiop, -len);
1804                         uio_iov_len_add(uiop, len);
1805                 }
1806                 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) {
1807                         error = nfscl_wcc_data(nd, vp, nap, attrflagp,
1808                             &wccflag, stuff);
1809                         if (error)
1810                                 goto nfsmout;
1811                 }
1812                 if (!nd->nd_repstat) {
1813                         if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) {
1814                                 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED
1815                                         + NFSX_VERF);
1816                                 rlen = fxdr_unsigned(int, *tl++);
1817                                 if (rlen == 0) {
1818                                         error = NFSERR_IO;
1819                                         goto nfsmout;
1820                                 } else if (rlen < len) {
1821                                         backup = len - rlen;
1822                                         uio_iov_base_add(uiop, -(backup));
1823                                         uio_iov_len_add(uiop, backup);
1824                                         uiop->uio_offset -= backup;
1825                                         uio_uio_resid_add(uiop, backup);
1826                                         len = rlen;
1827                                 }
1828                                 commit = fxdr_unsigned(int, *tl++);
1829
1830                                 /*
1831                                  * Return the lowest commitment level
1832                                  * obtained by any of the RPCs.
1833                                  */
1834                                 if (committed == NFSWRITE_FILESYNC)
1835                                         committed = commit;
1836                                 else if (committed == NFSWRITE_DATASYNC &&
1837                                         commit == NFSWRITE_UNSTABLE)
1838                                         committed = commit;
1839                                 NFSLOCKMNT(nmp);
1840                                 if (!NFSHASWRITEVERF(nmp)) {
1841                                         NFSBCOPY((caddr_t)tl,
1842                                             (caddr_t)&nmp->nm_verf[0],
1843                                             NFSX_VERF);
1844                                         NFSSETWRITEVERF(nmp);
1845                                 } else if (NFSBCMP(tl, nmp->nm_verf,
1846                                     NFSX_VERF)) {
1847                                         *must_commit = 1;
1848                                         NFSBCOPY(tl, nmp->nm_verf, NFSX_VERF);
1849                                 }
1850                                 NFSUNLOCKMNT(nmp);
1851                         }
1852                         if (nd->nd_flag & ND_NFSV4)
1853                                 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1854                         if (nd->nd_flag & (ND_NFSV2 | ND_NFSV4)) {
1855                                 error = nfsm_loadattr(nd, nap);
1856                                 if (!error)
1857                                         *attrflagp = NFS_LATTR_NOSHRINK;
1858                         }
1859                 } else {
1860                         error = nd->nd_repstat;
1861                 }
1862                 if (error)
1863                         goto nfsmout;
1864                 NFSWRITERPC_SETTIME(wccflag, np, nap, (nd->nd_flag & ND_NFSV4));
1865                 mbuf_freem(nd->nd_mrep);
1866                 nd->nd_mrep = NULL;
1867                 tsiz -= len;
1868         }
1869 nfsmout:
1870         if (nd->nd_mrep != NULL)
1871                 mbuf_freem(nd->nd_mrep);
1872         *iomode = committed;
1873         if (nd->nd_repstat && !error)
1874                 error = nd->nd_repstat;
1875         return (error);
1876 }
1877
1878 /*
1879  * nfs mknod rpc
1880  * For NFS v2 this is a kludge. Use a create rpc but with the IFMT bits of the
1881  * mode set to specify the file type and the size field for rdev.
1882  */
1883 int
1884 nfsrpc_mknod(vnode_t dvp, char *name, int namelen, struct vattr *vap,
1885     u_int32_t rdev, enum vtype vtyp, struct ucred *cred, NFSPROC_T *p,
1886     struct nfsvattr *dnap, struct nfsvattr *nnap, struct nfsfh **nfhpp,
1887     int *attrflagp, int *dattrflagp, void *dstuff)
1888 {
1889         u_int32_t *tl;
1890         int error = 0;
1891         struct nfsrv_descript nfsd, *nd = &nfsd;
1892         nfsattrbit_t attrbits;
1893
1894         *nfhpp = NULL;
1895         *attrflagp = 0;
1896         *dattrflagp = 0;
1897         if (namelen > NFS_MAXNAMLEN)
1898                 return (ENAMETOOLONG);
1899         NFSCL_REQSTART(nd, NFSPROC_MKNOD, dvp);
1900         if (nd->nd_flag & ND_NFSV4) {
1901                 if (vtyp == VBLK || vtyp == VCHR) {
1902                         NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
1903                         *tl++ = vtonfsv34_type(vtyp);
1904                         *tl++ = txdr_unsigned(NFSMAJOR(rdev));
1905                         *tl = txdr_unsigned(NFSMINOR(rdev));
1906                 } else {
1907                         NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1908                         *tl = vtonfsv34_type(vtyp);
1909                 }
1910         }
1911         (void) nfsm_strtom(nd, name, namelen);
1912         if (nd->nd_flag & ND_NFSV3) {
1913                 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1914                 *tl = vtonfsv34_type(vtyp);
1915         }
1916         if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4))
1917                 nfscl_fillsattr(nd, vap, dvp, 0, 0);
1918         if ((nd->nd_flag & ND_NFSV3) &&
1919             (vtyp == VCHR || vtyp == VBLK)) {
1920                 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1921                 *tl++ = txdr_unsigned(NFSMAJOR(rdev));
1922                 *tl = txdr_unsigned(NFSMINOR(rdev));
1923         }
1924         if (nd->nd_flag & ND_NFSV4) {
1925                 NFSGETATTR_ATTRBIT(&attrbits);
1926                 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1927                 *tl++ = txdr_unsigned(NFSV4OP_GETFH);
1928                 *tl = txdr_unsigned(NFSV4OP_GETATTR);
1929                 (void) nfsrv_putattrbit(nd, &attrbits);
1930         }
1931         if (nd->nd_flag & ND_NFSV2)
1932                 nfscl_fillsattr(nd, vap, dvp, NFSSATTR_SIZERDEV, rdev);
1933         error = nfscl_request(nd, dvp, p, cred, dstuff);
1934         if (error)
1935                 return (error);
1936         if (nd->nd_flag & ND_NFSV4)
1937                 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
1938         if (!nd->nd_repstat) {
1939                 if (nd->nd_flag & ND_NFSV4) {
1940                         NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
1941                         error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
1942                         if (error)
1943                                 goto nfsmout;
1944                 }
1945                 error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp);
1946                 if (error)
1947                         goto nfsmout;
1948         }
1949         if (nd->nd_flag & ND_NFSV3)
1950                 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
1951         if (!error && nd->nd_repstat)
1952                 error = nd->nd_repstat;
1953 nfsmout:
1954         mbuf_freem(nd->nd_mrep);
1955         return (error);
1956 }
1957
1958 /*
1959  * nfs file create call
1960  * Mostly just call the approriate routine. (I separated out v4, so that
1961  * error recovery wouldn't be as difficult.)
1962  */
1963 int
1964 nfsrpc_create(vnode_t dvp, char *name, int namelen, struct vattr *vap,
1965     nfsquad_t cverf, int fmode, struct ucred *cred, NFSPROC_T *p,
1966     struct nfsvattr *dnap, struct nfsvattr *nnap, struct nfsfh **nfhpp,
1967     int *attrflagp, int *dattrflagp, void *dstuff)
1968 {
1969         int error = 0, newone, expireret = 0, retrycnt, unlocked;
1970         struct nfsclowner *owp;
1971         struct nfscldeleg *dp;
1972         struct nfsmount *nmp = VFSTONFS(vnode_mount(dvp));
1973         u_int32_t clidrev;
1974
1975         if (NFSHASNFSV4(nmp)) {
1976             retrycnt = 0;
1977             do {
1978                 dp = NULL;
1979                 error = nfscl_open(dvp, NULL, 0, (NFSV4OPEN_ACCESSWRITE |
1980                     NFSV4OPEN_ACCESSREAD), 0, cred, p, &owp, NULL, &newone,
1981                     NULL, 1);
1982                 if (error)
1983                         return (error);
1984                 if (nmp->nm_clp != NULL)
1985                         clidrev = nmp->nm_clp->nfsc_clientidrev;
1986                 else
1987                         clidrev = 0;
1988                 if (!NFSHASPNFS(nmp) || nfscl_enablecallb == 0 ||
1989                     nfs_numnfscbd == 0 || retrycnt > 0)
1990                         error = nfsrpc_createv4(dvp, name, namelen, vap, cverf,
1991                           fmode, owp, &dp, cred, p, dnap, nnap, nfhpp,
1992                           attrflagp, dattrflagp, dstuff, &unlocked);
1993                 else
1994                         error = nfsrpc_getcreatelayout(dvp, name, namelen, vap,
1995                           cverf, fmode, owp, &dp, cred, p, dnap, nnap, nfhpp,
1996                           attrflagp, dattrflagp, dstuff, &unlocked);
1997                 /*
1998                  * There is no need to invalidate cached attributes here,
1999                  * since new post-delegation issue attributes are always
2000                  * returned by nfsrpc_createv4() and these will update the
2001                  * attribute cache.
2002                  */
2003                 if (dp != NULL)
2004                         (void) nfscl_deleg(nmp->nm_mountp, owp->nfsow_clp,
2005                             (*nfhpp)->nfh_fh, (*nfhpp)->nfh_len, cred, p, &dp);
2006                 nfscl_ownerrelease(nmp, owp, error, newone, unlocked);
2007                 if (error == NFSERR_GRACE || error == NFSERR_STALECLIENTID ||
2008                     error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
2009                     error == NFSERR_BADSESSION) {
2010                         (void) nfs_catnap(PZERO, error, "nfs_open");
2011                 } else if ((error == NFSERR_EXPIRED ||
2012                     error == NFSERR_BADSTATEID) && clidrev != 0) {
2013                         expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
2014                         retrycnt++;
2015                 }
2016             } while (error == NFSERR_GRACE || error == NFSERR_STALECLIENTID ||
2017                 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
2018                 error == NFSERR_BADSESSION ||
2019                 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
2020                  expireret == 0 && clidrev != 0 && retrycnt < 4));
2021             if (error && retrycnt >= 4)
2022                     error = EIO;
2023         } else {
2024                 error = nfsrpc_createv23(dvp, name, namelen, vap, cverf,
2025                     fmode, cred, p, dnap, nnap, nfhpp, attrflagp, dattrflagp,
2026                     dstuff);
2027         }
2028         return (error);
2029 }
2030
2031 /*
2032  * The create rpc for v2 and 3.
2033  */
2034 static int
2035 nfsrpc_createv23(vnode_t dvp, char *name, int namelen, struct vattr *vap,
2036     nfsquad_t cverf, int fmode, struct ucred *cred, NFSPROC_T *p,
2037     struct nfsvattr *dnap, struct nfsvattr *nnap, struct nfsfh **nfhpp,
2038     int *attrflagp, int *dattrflagp, void *dstuff)
2039 {
2040         u_int32_t *tl;
2041         int error = 0;
2042         struct nfsrv_descript nfsd, *nd = &nfsd;
2043
2044         *nfhpp = NULL;
2045         *attrflagp = 0;
2046         *dattrflagp = 0;
2047         if (namelen > NFS_MAXNAMLEN)
2048                 return (ENAMETOOLONG);
2049         NFSCL_REQSTART(nd, NFSPROC_CREATE, dvp);
2050         (void) nfsm_strtom(nd, name, namelen);
2051         if (nd->nd_flag & ND_NFSV3) {
2052                 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2053                 if (fmode & O_EXCL) {
2054                         *tl = txdr_unsigned(NFSCREATE_EXCLUSIVE);
2055                         NFSM_BUILD(tl, u_int32_t *, NFSX_VERF);
2056                         *tl++ = cverf.lval[0];
2057                         *tl = cverf.lval[1];
2058                 } else {
2059                         *tl = txdr_unsigned(NFSCREATE_UNCHECKED);
2060                         nfscl_fillsattr(nd, vap, dvp, 0, 0);
2061                 }
2062         } else {
2063                 nfscl_fillsattr(nd, vap, dvp, NFSSATTR_SIZE0, 0);
2064         }
2065         error = nfscl_request(nd, dvp, p, cred, dstuff);
2066         if (error)
2067                 return (error);
2068         if (nd->nd_repstat == 0) {
2069                 error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp);
2070                 if (error)
2071                         goto nfsmout;
2072         }
2073         if (nd->nd_flag & ND_NFSV3)
2074                 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
2075         if (nd->nd_repstat != 0 && error == 0)
2076                 error = nd->nd_repstat;
2077 nfsmout:
2078         mbuf_freem(nd->nd_mrep);
2079         return (error);
2080 }
2081
2082 static int
2083 nfsrpc_createv4(vnode_t dvp, char *name, int namelen, struct vattr *vap,
2084     nfsquad_t cverf, int fmode, struct nfsclowner *owp, struct nfscldeleg **dpp,
2085     struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap,
2086     struct nfsvattr *nnap, struct nfsfh **nfhpp, int *attrflagp,
2087     int *dattrflagp, void *dstuff, int *unlockedp)
2088 {
2089         u_int32_t *tl;
2090         int error = 0, deleg, newone, ret, acesize, limitby;
2091         struct nfsrv_descript nfsd, *nd = &nfsd;
2092         struct nfsclopen *op;
2093         struct nfscldeleg *dp = NULL;
2094         struct nfsnode *np;
2095         struct nfsfh *nfhp;
2096         nfsattrbit_t attrbits;
2097         nfsv4stateid_t stateid;
2098         u_int32_t rflags;
2099         struct nfsmount *nmp;
2100         struct nfsclsession *tsep;
2101
2102         nmp = VFSTONFS(dvp->v_mount);
2103         np = VTONFS(dvp);
2104         *unlockedp = 0;
2105         *nfhpp = NULL;
2106         *dpp = NULL;
2107         *attrflagp = 0;
2108         *dattrflagp = 0;
2109         if (namelen > NFS_MAXNAMLEN)
2110                 return (ENAMETOOLONG);
2111         NFSCL_REQSTART(nd, NFSPROC_CREATE, dvp);
2112         /*
2113          * For V4, this is actually an Open op.
2114          */
2115         NFSM_BUILD(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
2116         *tl++ = txdr_unsigned(owp->nfsow_seqid);
2117         *tl++ = txdr_unsigned(NFSV4OPEN_ACCESSWRITE |
2118             NFSV4OPEN_ACCESSREAD);
2119         *tl++ = txdr_unsigned(NFSV4OPEN_DENYNONE);
2120         tsep = nfsmnt_mdssession(nmp);
2121         *tl++ = tsep->nfsess_clientid.lval[0];
2122         *tl = tsep->nfsess_clientid.lval[1];
2123         (void) nfsm_strtom(nd, owp->nfsow_owner, NFSV4CL_LOCKNAMELEN);
2124         NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2125         *tl++ = txdr_unsigned(NFSV4OPEN_CREATE);
2126         if (fmode & O_EXCL) {
2127                 if (NFSHASNFSV4N(nmp)) {
2128                         if (NFSHASSESSPERSIST(nmp)) {
2129                                 /* Use GUARDED for persistent sessions. */
2130                                 *tl = txdr_unsigned(NFSCREATE_GUARDED);
2131                                 nfscl_fillsattr(nd, vap, dvp, 0, 0);
2132                         } else {
2133                                 /* Otherwise, use EXCLUSIVE4_1. */
2134                                 *tl = txdr_unsigned(NFSCREATE_EXCLUSIVE41);
2135                                 NFSM_BUILD(tl, u_int32_t *, NFSX_VERF);
2136                                 *tl++ = cverf.lval[0];
2137                                 *tl = cverf.lval[1];
2138                                 nfscl_fillsattr(nd, vap, dvp, 0, 0);
2139                         }
2140                 } else {
2141                         /* NFSv4.0 */
2142                         *tl = txdr_unsigned(NFSCREATE_EXCLUSIVE);
2143                         NFSM_BUILD(tl, u_int32_t *, NFSX_VERF);
2144                         *tl++ = cverf.lval[0];
2145                         *tl = cverf.lval[1];
2146                 }
2147         } else {
2148                 *tl = txdr_unsigned(NFSCREATE_UNCHECKED);
2149                 nfscl_fillsattr(nd, vap, dvp, 0, 0);
2150         }
2151         NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2152         *tl = txdr_unsigned(NFSV4OPEN_CLAIMNULL);
2153         (void) nfsm_strtom(nd, name, namelen);
2154         /* Get the new file's handle and attributes. */
2155         NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2156         *tl++ = txdr_unsigned(NFSV4OP_GETFH);
2157         *tl = txdr_unsigned(NFSV4OP_GETATTR);
2158         NFSGETATTR_ATTRBIT(&attrbits);
2159         (void) nfsrv_putattrbit(nd, &attrbits);
2160         /* Get the directory's post-op attributes. */
2161         NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2162         *tl = txdr_unsigned(NFSV4OP_PUTFH);
2163         (void) nfsm_fhtom(nd, np->n_fhp->nfh_fh, np->n_fhp->nfh_len, 0);
2164         NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2165         *tl = txdr_unsigned(NFSV4OP_GETATTR);
2166         (void) nfsrv_putattrbit(nd, &attrbits);
2167         error = nfscl_request(nd, dvp, p, cred, dstuff);
2168         if (error)
2169                 return (error);
2170         NFSCL_INCRSEQID(owp->nfsow_seqid, nd);
2171         if (nd->nd_repstat == 0) {
2172                 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
2173                     6 * NFSX_UNSIGNED);
2174                 stateid.seqid = *tl++;
2175                 stateid.other[0] = *tl++;
2176                 stateid.other[1] = *tl++;
2177                 stateid.other[2] = *tl;
2178                 rflags = fxdr_unsigned(u_int32_t, *(tl + 6));
2179                 (void) nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
2180                 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2181                 deleg = fxdr_unsigned(int, *tl);
2182                 if (deleg == NFSV4OPEN_DELEGATEREAD ||
2183                     deleg == NFSV4OPEN_DELEGATEWRITE) {
2184                         if (!(owp->nfsow_clp->nfsc_flags &
2185                               NFSCLFLAGS_FIRSTDELEG))
2186                                 owp->nfsow_clp->nfsc_flags |=
2187                                   (NFSCLFLAGS_FIRSTDELEG | NFSCLFLAGS_GOTDELEG);
2188                         dp = malloc(
2189                             sizeof (struct nfscldeleg) + NFSX_V4FHMAX,
2190                             M_NFSCLDELEG, M_WAITOK);
2191                         LIST_INIT(&dp->nfsdl_owner);
2192                         LIST_INIT(&dp->nfsdl_lock);
2193                         dp->nfsdl_clp = owp->nfsow_clp;
2194                         newnfs_copyincred(cred, &dp->nfsdl_cred);
2195                         nfscl_lockinit(&dp->nfsdl_rwlock);
2196                         NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
2197                             NFSX_UNSIGNED);
2198                         dp->nfsdl_stateid.seqid = *tl++;
2199                         dp->nfsdl_stateid.other[0] = *tl++;
2200                         dp->nfsdl_stateid.other[1] = *tl++;
2201                         dp->nfsdl_stateid.other[2] = *tl++;
2202                         ret = fxdr_unsigned(int, *tl);
2203                         if (deleg == NFSV4OPEN_DELEGATEWRITE) {
2204                                 dp->nfsdl_flags = NFSCLDL_WRITE;
2205                                 /*
2206                                  * Indicates how much the file can grow.
2207                                  */
2208                                 NFSM_DISSECT(tl, u_int32_t *,
2209                                     3 * NFSX_UNSIGNED);
2210                                 limitby = fxdr_unsigned(int, *tl++);
2211                                 switch (limitby) {
2212                                 case NFSV4OPEN_LIMITSIZE:
2213                                         dp->nfsdl_sizelimit = fxdr_hyper(tl);
2214                                         break;
2215                                 case NFSV4OPEN_LIMITBLOCKS:
2216                                         dp->nfsdl_sizelimit =
2217                                             fxdr_unsigned(u_int64_t, *tl++);
2218                                         dp->nfsdl_sizelimit *=
2219                                             fxdr_unsigned(u_int64_t, *tl);
2220                                         break;
2221                                 default:
2222                                         error = NFSERR_BADXDR;
2223                                         goto nfsmout;
2224                                 }
2225                         } else {
2226                                 dp->nfsdl_flags = NFSCLDL_READ;
2227                         }
2228                         if (ret)
2229                                 dp->nfsdl_flags |= NFSCLDL_RECALL;
2230                         error = nfsrv_dissectace(nd, &dp->nfsdl_ace, &ret,
2231                             &acesize, p);
2232                         if (error)
2233                                 goto nfsmout;
2234                 } else if (deleg != NFSV4OPEN_DELEGATENONE) {
2235                         error = NFSERR_BADXDR;
2236                         goto nfsmout;
2237                 }
2238                 error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp);
2239                 if (error)
2240                         goto nfsmout;
2241                 /* Get rid of the PutFH and Getattr status values. */
2242                 NFSM_DISSECT(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
2243                 /* Load the directory attributes. */
2244                 error = nfsm_loadattr(nd, dnap);
2245                 if (error)
2246                         goto nfsmout;
2247                 *dattrflagp = 1;
2248                 if (dp != NULL && *attrflagp) {
2249                         dp->nfsdl_change = nnap->na_filerev;
2250                         dp->nfsdl_modtime = nnap->na_mtime;
2251                         dp->nfsdl_flags |= NFSCLDL_MODTIMESET;
2252                 }
2253                 /*
2254                  * We can now complete the Open state.
2255                  */
2256                 nfhp = *nfhpp;
2257                 if (dp != NULL) {
2258                         dp->nfsdl_fhlen = nfhp->nfh_len;
2259                         NFSBCOPY(nfhp->nfh_fh, dp->nfsdl_fh, nfhp->nfh_len);
2260                 }
2261                 /*
2262                  * Get an Open structure that will be
2263                  * attached to the OpenOwner, acquired already.
2264                  */
2265                 error = nfscl_open(dvp, nfhp->nfh_fh, nfhp->nfh_len, 
2266                     (NFSV4OPEN_ACCESSWRITE | NFSV4OPEN_ACCESSREAD), 0,
2267                     cred, p, NULL, &op, &newone, NULL, 0);
2268                 if (error)
2269                         goto nfsmout;
2270                 op->nfso_stateid = stateid;
2271                 newnfs_copyincred(cred, &op->nfso_cred);
2272                 if ((rflags & NFSV4OPEN_RESULTCONFIRM)) {
2273                     do {
2274                         ret = nfsrpc_openconfirm(dvp, nfhp->nfh_fh,
2275                             nfhp->nfh_len, op, cred, p);
2276                         if (ret == NFSERR_DELAY)
2277                             (void) nfs_catnap(PZERO, ret, "nfs_create");
2278                     } while (ret == NFSERR_DELAY);
2279                     error = ret;
2280                 }
2281
2282                 /*
2283                  * If the server is handing out delegations, but we didn't
2284                  * get one because an OpenConfirm was required, try the
2285                  * Open again, to get a delegation. This is a harmless no-op,
2286                  * from a server's point of view.
2287                  */
2288                 if ((rflags & NFSV4OPEN_RESULTCONFIRM) &&
2289                     (owp->nfsow_clp->nfsc_flags & NFSCLFLAGS_GOTDELEG) &&
2290                     !error && dp == NULL) {
2291                     do {
2292                         ret = nfsrpc_openrpc(VFSTONFS(vnode_mount(dvp)), dvp,
2293                             np->n_fhp->nfh_fh, np->n_fhp->nfh_len,
2294                             nfhp->nfh_fh, nfhp->nfh_len,
2295                             (NFSV4OPEN_ACCESSWRITE | NFSV4OPEN_ACCESSREAD), op,
2296                             name, namelen, &dp, 0, 0x0, cred, p, 0, 1);
2297                         if (ret == NFSERR_DELAY)
2298                             (void) nfs_catnap(PZERO, ret, "nfs_crt2");
2299                     } while (ret == NFSERR_DELAY);
2300                     if (ret) {
2301                         if (dp != NULL) {
2302                                 free(dp, M_NFSCLDELEG);
2303                                 dp = NULL;
2304                         }
2305                         if (ret == NFSERR_STALECLIENTID ||
2306                             ret == NFSERR_STALEDONTRECOVER ||
2307                             ret == NFSERR_BADSESSION)
2308                                 error = ret;
2309                     }
2310                 }
2311                 nfscl_openrelease(nmp, op, error, newone);
2312                 *unlockedp = 1;
2313         }
2314         if (nd->nd_repstat != 0 && error == 0)
2315                 error = nd->nd_repstat;
2316         if (error == NFSERR_STALECLIENTID)
2317                 nfscl_initiate_recovery(owp->nfsow_clp);
2318 nfsmout:
2319         if (!error)
2320                 *dpp = dp;
2321         else if (dp != NULL)
2322                 free(dp, M_NFSCLDELEG);
2323         mbuf_freem(nd->nd_mrep);
2324         return (error);
2325 }
2326
2327 /*
2328  * Nfs remove rpc
2329  */
2330 int
2331 nfsrpc_remove(vnode_t dvp, char *name, int namelen, vnode_t vp,
2332     struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap, int *dattrflagp,
2333     void *dstuff)
2334 {
2335         u_int32_t *tl;
2336         struct nfsrv_descript nfsd, *nd = &nfsd;
2337         struct nfsnode *np;
2338         struct nfsmount *nmp;
2339         nfsv4stateid_t dstateid;
2340         int error, ret = 0, i;
2341
2342         *dattrflagp = 0;
2343         if (namelen > NFS_MAXNAMLEN)
2344                 return (ENAMETOOLONG);
2345         nmp = VFSTONFS(vnode_mount(dvp));
2346 tryagain:
2347         if (NFSHASNFSV4(nmp) && ret == 0) {
2348                 ret = nfscl_removedeleg(vp, p, &dstateid);
2349                 if (ret == 1) {
2350                         NFSCL_REQSTART(nd, NFSPROC_RETDELEGREMOVE, vp);
2351                         NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID +
2352                             NFSX_UNSIGNED);
2353                         if (NFSHASNFSV4N(nmp))
2354                                 *tl++ = 0;
2355                         else
2356                                 *tl++ = dstateid.seqid;
2357                         *tl++ = dstateid.other[0];
2358                         *tl++ = dstateid.other[1];
2359                         *tl++ = dstateid.other[2];
2360                         *tl = txdr_unsigned(NFSV4OP_PUTFH);
2361                         np = VTONFS(dvp);
2362                         (void) nfsm_fhtom(nd, np->n_fhp->nfh_fh,
2363                             np->n_fhp->nfh_len, 0);
2364                         NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2365                         *tl = txdr_unsigned(NFSV4OP_REMOVE);
2366                 }
2367         } else {
2368                 ret = 0;
2369         }
2370         if (ret == 0)
2371                 NFSCL_REQSTART(nd, NFSPROC_REMOVE, dvp);
2372         (void) nfsm_strtom(nd, name, namelen);
2373         error = nfscl_request(nd, dvp, p, cred, dstuff);
2374         if (error)
2375                 return (error);
2376         if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) {
2377                 /* For NFSv4, parse out any Delereturn replies. */
2378                 if (ret > 0 && nd->nd_repstat != 0 &&
2379                     (nd->nd_flag & ND_NOMOREDATA)) {
2380                         /*
2381                          * If the Delegreturn failed, try again without
2382                          * it. The server will Recall, as required.
2383                          */
2384                         mbuf_freem(nd->nd_mrep);
2385                         goto tryagain;
2386                 }
2387                 for (i = 0; i < (ret * 2); i++) {
2388                         if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) ==
2389                             ND_NFSV4) {
2390                             NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2391                             if (*(tl + 1))
2392                                 nd->nd_flag |= ND_NOMOREDATA;
2393                         }
2394                 }
2395                 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
2396         }
2397         if (nd->nd_repstat && !error)
2398                 error = nd->nd_repstat;
2399 nfsmout:
2400         mbuf_freem(nd->nd_mrep);
2401         return (error);
2402 }
2403
2404 /*
2405  * Do an nfs rename rpc.
2406  */
2407 int
2408 nfsrpc_rename(vnode_t fdvp, vnode_t fvp, char *fnameptr, int fnamelen,
2409     vnode_t tdvp, vnode_t tvp, char *tnameptr, int tnamelen, struct ucred *cred,
2410     NFSPROC_T *p, struct nfsvattr *fnap, struct nfsvattr *tnap,
2411     int *fattrflagp, int *tattrflagp, void *fstuff, void *tstuff)
2412 {
2413         u_int32_t *tl;
2414         struct nfsrv_descript nfsd, *nd = &nfsd;
2415         struct nfsmount *nmp;
2416         struct nfsnode *np;
2417         nfsattrbit_t attrbits;
2418         nfsv4stateid_t fdstateid, tdstateid;
2419         int error = 0, ret = 0, gottd = 0, gotfd = 0, i;
2420         
2421         *fattrflagp = 0;
2422         *tattrflagp = 0;
2423         nmp = VFSTONFS(vnode_mount(fdvp));
2424         if (fnamelen > NFS_MAXNAMLEN || tnamelen > NFS_MAXNAMLEN)
2425                 return (ENAMETOOLONG);
2426 tryagain:
2427         if (NFSHASNFSV4(nmp) && ret == 0) {
2428                 ret = nfscl_renamedeleg(fvp, &fdstateid, &gotfd, tvp,
2429                     &tdstateid, &gottd, p);
2430                 if (gotfd && gottd) {
2431                         NFSCL_REQSTART(nd, NFSPROC_RETDELEGRENAME2, fvp);
2432                 } else if (gotfd) {
2433                         NFSCL_REQSTART(nd, NFSPROC_RETDELEGRENAME1, fvp);
2434                 } else if (gottd) {
2435                         NFSCL_REQSTART(nd, NFSPROC_RETDELEGRENAME1, tvp);
2436                 }
2437                 if (gotfd) {
2438                         NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID);
2439                         if (NFSHASNFSV4N(nmp))
2440                                 *tl++ = 0;
2441                         else
2442                                 *tl++ = fdstateid.seqid;
2443                         *tl++ = fdstateid.other[0];
2444                         *tl++ = fdstateid.other[1];
2445                         *tl = fdstateid.other[2];
2446                         if (gottd) {
2447                                 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2448                                 *tl = txdr_unsigned(NFSV4OP_PUTFH);
2449                                 np = VTONFS(tvp);
2450                                 (void) nfsm_fhtom(nd, np->n_fhp->nfh_fh,
2451                                     np->n_fhp->nfh_len, 0);
2452                                 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2453                                 *tl = txdr_unsigned(NFSV4OP_DELEGRETURN);
2454                         }
2455                 }
2456                 if (gottd) {
2457                         NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID);
2458                         if (NFSHASNFSV4N(nmp))
2459                                 *tl++ = 0;
2460                         else
2461                                 *tl++ = tdstateid.seqid;
2462                         *tl++ = tdstateid.other[0];
2463                         *tl++ = tdstateid.other[1];
2464                         *tl = tdstateid.other[2];
2465                 }
2466                 if (ret > 0) {
2467                         NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2468                         *tl = txdr_unsigned(NFSV4OP_PUTFH);
2469                         np = VTONFS(fdvp);
2470                         (void) nfsm_fhtom(nd, np->n_fhp->nfh_fh,
2471                             np->n_fhp->nfh_len, 0);
2472                         NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2473                         *tl = txdr_unsigned(NFSV4OP_SAVEFH);
2474                 }
2475         } else {
2476                 ret = 0;
2477         }
2478         if (ret == 0)
2479                 NFSCL_REQSTART(nd, NFSPROC_RENAME, fdvp);
2480         if (nd->nd_flag & ND_NFSV4) {
2481                 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2482                 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2483                 NFSWCCATTR_ATTRBIT(&attrbits);
2484                 (void) nfsrv_putattrbit(nd, &attrbits);
2485                 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2486                 *tl = txdr_unsigned(NFSV4OP_PUTFH);
2487                 (void) nfsm_fhtom(nd, VTONFS(tdvp)->n_fhp->nfh_fh,
2488                     VTONFS(tdvp)->n_fhp->nfh_len, 0);
2489                 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2490                 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2491                 (void) nfsrv_putattrbit(nd, &attrbits);
2492                 nd->nd_flag |= ND_V4WCCATTR;
2493                 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2494                 *tl = txdr_unsigned(NFSV4OP_RENAME);
2495         }
2496         (void) nfsm_strtom(nd, fnameptr, fnamelen);
2497         if (!(nd->nd_flag & ND_NFSV4))
2498                 (void) nfsm_fhtom(nd, VTONFS(tdvp)->n_fhp->nfh_fh,
2499                         VTONFS(tdvp)->n_fhp->nfh_len, 0);
2500         (void) nfsm_strtom(nd, tnameptr, tnamelen);
2501         error = nfscl_request(nd, fdvp, p, cred, fstuff);
2502         if (error)
2503                 return (error);
2504         if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) {
2505                 /* For NFSv4, parse out any Delereturn replies. */
2506                 if (ret > 0 && nd->nd_repstat != 0 &&
2507                     (nd->nd_flag & ND_NOMOREDATA)) {
2508                         /*
2509                          * If the Delegreturn failed, try again without
2510                          * it. The server will Recall, as required.
2511                          */
2512                         mbuf_freem(nd->nd_mrep);
2513                         goto tryagain;
2514                 }
2515                 for (i = 0; i < (ret * 2); i++) {
2516                         if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) ==
2517                             ND_NFSV4) {
2518                             NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2519                             if (*(tl + 1)) {
2520                                 if (i == 0 && ret > 1) {
2521                                     /*
2522                                      * If the Delegreturn failed, try again
2523                                      * without it. The server will Recall, as
2524                                      * required.
2525                                      * If ret > 1, the first iteration of this
2526                                      * loop is the second DelegReturn result.
2527                                      */
2528                                     mbuf_freem(nd->nd_mrep);
2529                                     goto tryagain;
2530                                 } else {
2531                                     nd->nd_flag |= ND_NOMOREDATA;
2532                                 }
2533                             }
2534                         }
2535                 }
2536                 /* Now, the first wcc attribute reply. */
2537                 if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) == ND_NFSV4) {
2538                         NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2539                         if (*(tl + 1))
2540                                 nd->nd_flag |= ND_NOMOREDATA;
2541                 }
2542                 error = nfscl_wcc_data(nd, fdvp, fnap, fattrflagp, NULL,
2543                     fstuff);
2544                 /* and the second wcc attribute reply. */
2545                 if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) == ND_NFSV4 &&
2546                     !error) {
2547                         NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2548                         if (*(tl + 1))
2549                                 nd->nd_flag |= ND_NOMOREDATA;
2550                 }
2551                 if (!error)
2552                         error = nfscl_wcc_data(nd, tdvp, tnap, tattrflagp,
2553                             NULL, tstuff);
2554         }
2555         if (nd->nd_repstat && !error)
2556                 error = nd->nd_repstat;
2557 nfsmout:
2558         mbuf_freem(nd->nd_mrep);
2559         return (error);
2560 }
2561
2562 /*
2563  * nfs hard link create rpc
2564  */
2565 int
2566 nfsrpc_link(vnode_t dvp, vnode_t vp, char *name, int namelen,
2567     struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap,
2568     struct nfsvattr *nap, int *attrflagp, int *dattrflagp, void *dstuff)
2569 {
2570         u_int32_t *tl;
2571         struct nfsrv_descript nfsd, *nd = &nfsd;
2572         nfsattrbit_t attrbits;
2573         int error = 0;
2574
2575         *attrflagp = 0;
2576         *dattrflagp = 0;
2577         if (namelen > NFS_MAXNAMLEN)
2578                 return (ENAMETOOLONG);
2579         NFSCL_REQSTART(nd, NFSPROC_LINK, vp);
2580         if (nd->nd_flag & ND_NFSV4) {
2581                 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2582                 *tl = txdr_unsigned(NFSV4OP_PUTFH);
2583         }
2584         (void) nfsm_fhtom(nd, VTONFS(dvp)->n_fhp->nfh_fh,
2585                 VTONFS(dvp)->n_fhp->nfh_len, 0);
2586         if (nd->nd_flag & ND_NFSV4) {
2587                 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2588                 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2589                 NFSWCCATTR_ATTRBIT(&attrbits);
2590                 (void) nfsrv_putattrbit(nd, &attrbits);
2591                 nd->nd_flag |= ND_V4WCCATTR;
2592                 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2593                 *tl = txdr_unsigned(NFSV4OP_LINK);
2594         }
2595         (void) nfsm_strtom(nd, name, namelen);
2596         error = nfscl_request(nd, vp, p, cred, dstuff);
2597         if (error)
2598                 return (error);
2599         if (nd->nd_flag & ND_NFSV3) {
2600                 error = nfscl_postop_attr(nd, nap, attrflagp, dstuff);
2601                 if (!error)
2602                         error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp,
2603                             NULL, dstuff);
2604         } else if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) == ND_NFSV4) {
2605                 /*
2606                  * First, parse out the PutFH and Getattr result.
2607                  */
2608                 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2609                 if (!(*(tl + 1)))
2610                         NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2611                 if (*(tl + 1))
2612                         nd->nd_flag |= ND_NOMOREDATA;
2613                 /*
2614                  * Get the pre-op attributes.
2615                  */
2616                 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
2617         }
2618         if (nd->nd_repstat && !error)
2619                 error = nd->nd_repstat;
2620 nfsmout:
2621         mbuf_freem(nd->nd_mrep);
2622         return (error);
2623 }
2624
2625 /*
2626  * nfs symbolic link create rpc
2627  */
2628 int
2629 nfsrpc_symlink(vnode_t dvp, char *name, int namelen, char *target,
2630     struct vattr *vap, struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap,
2631     struct nfsvattr *nnap, struct nfsfh **nfhpp, int *attrflagp,
2632     int *dattrflagp, void *dstuff)
2633 {
2634         u_int32_t *tl;
2635         struct nfsrv_descript nfsd, *nd = &nfsd;
2636         struct nfsmount *nmp;
2637         int slen, error = 0;
2638
2639         *nfhpp = NULL;
2640         *attrflagp = 0;
2641         *dattrflagp = 0;
2642         nmp = VFSTONFS(vnode_mount(dvp));
2643         slen = strlen(target);
2644         if (slen > NFS_MAXPATHLEN || namelen > NFS_MAXNAMLEN)
2645                 return (ENAMETOOLONG);
2646         NFSCL_REQSTART(nd, NFSPROC_SYMLINK, dvp);
2647         if (nd->nd_flag & ND_NFSV4) {
2648                 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2649                 *tl = txdr_unsigned(NFLNK);
2650                 (void) nfsm_strtom(nd, target, slen);
2651         }
2652         (void) nfsm_strtom(nd, name, namelen);
2653         if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4))
2654                 nfscl_fillsattr(nd, vap, dvp, 0, 0);
2655         if (!(nd->nd_flag & ND_NFSV4))
2656                 (void) nfsm_strtom(nd, target, slen);
2657         if (nd->nd_flag & ND_NFSV2)
2658                 nfscl_fillsattr(nd, vap, dvp, NFSSATTR_SIZENEG1, 0);
2659         error = nfscl_request(nd, dvp, p, cred, dstuff);
2660         if (error)
2661                 return (error);
2662         if (nd->nd_flag & ND_NFSV4)
2663                 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
2664         if ((nd->nd_flag & ND_NFSV3) && !error) {
2665                 if (!nd->nd_repstat)
2666                         error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp);
2667                 if (!error)
2668                         error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp,
2669                             NULL, dstuff);
2670         }
2671         if (nd->nd_repstat && !error)
2672                 error = nd->nd_repstat;
2673         mbuf_freem(nd->nd_mrep);
2674         /*
2675          * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry.
2676          * Only do this if vfs.nfs.ignore_eexist is set.
2677          * Never do this for NFSv4.1 or later minor versions, since sessions
2678          * should guarantee "exactly once" RPC semantics.
2679          */
2680         if (error == EEXIST && nfsignore_eexist != 0 && (!NFSHASNFSV4(nmp) ||
2681             nmp->nm_minorvers == 0))
2682                 error = 0;
2683         return (error);
2684 }
2685
2686 /*
2687  * nfs make dir rpc
2688  */
2689 int
2690 nfsrpc_mkdir(vnode_t dvp, char *name, int namelen, struct vattr *vap,
2691     struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap,
2692     struct nfsvattr *nnap, struct nfsfh **nfhpp, int *attrflagp,
2693     int *dattrflagp, void *dstuff)
2694 {
2695         u_int32_t *tl;
2696         struct nfsrv_descript nfsd, *nd = &nfsd;
2697         nfsattrbit_t attrbits;
2698         int error = 0;
2699         struct nfsfh *fhp;
2700         struct nfsmount *nmp;
2701
2702         *nfhpp = NULL;
2703         *attrflagp = 0;
2704         *dattrflagp = 0;
2705         nmp = VFSTONFS(vnode_mount(dvp));
2706         fhp = VTONFS(dvp)->n_fhp;
2707         if (namelen > NFS_MAXNAMLEN)
2708                 return (ENAMETOOLONG);
2709         NFSCL_REQSTART(nd, NFSPROC_MKDIR, dvp);
2710         if (nd->nd_flag & ND_NFSV4) {
2711                 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2712                 *tl = txdr_unsigned(NFDIR);
2713         }
2714         (void) nfsm_strtom(nd, name, namelen);
2715         nfscl_fillsattr(nd, vap, dvp, NFSSATTR_SIZENEG1, 0);
2716         if (nd->nd_flag & ND_NFSV4) {
2717                 NFSGETATTR_ATTRBIT(&attrbits);
2718                 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2719                 *tl++ = txdr_unsigned(NFSV4OP_GETFH);
2720                 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2721                 (void) nfsrv_putattrbit(nd, &attrbits);
2722                 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2723                 *tl = txdr_unsigned(NFSV4OP_PUTFH);
2724                 (void) nfsm_fhtom(nd, fhp->nfh_fh, fhp->nfh_len, 0);
2725                 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2726                 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2727                 (void) nfsrv_putattrbit(nd, &attrbits);
2728         }
2729         error = nfscl_request(nd, dvp, p, cred, dstuff);
2730         if (error)
2731                 return (error);
2732         if (nd->nd_flag & ND_NFSV4)
2733                 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
2734         if (!nd->nd_repstat && !error) {
2735                 if (nd->nd_flag & ND_NFSV4) {
2736                         NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
2737                         error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
2738                 }
2739                 if (!error)
2740                         error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp);
2741                 if (error == 0 && (nd->nd_flag & ND_NFSV4) != 0) {
2742                         /* Get rid of the PutFH and Getattr status values. */
2743                         NFSM_DISSECT(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
2744                         /* Load the directory attributes. */
2745                         error = nfsm_loadattr(nd, dnap);
2746                         if (error == 0)
2747                                 *dattrflagp = 1;
2748                 }
2749         }
2750         if ((nd->nd_flag & ND_NFSV3) && !error)
2751                 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
2752         if (nd->nd_repstat && !error)
2753                 error = nd->nd_repstat;
2754 nfsmout:
2755         mbuf_freem(nd->nd_mrep);
2756         /*
2757          * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry.
2758          * Only do this if vfs.nfs.ignore_eexist is set.
2759          * Never do this for NFSv4.1 or later minor versions, since sessions
2760          * should guarantee "exactly once" RPC semantics.
2761          */
2762         if (error == EEXIST && nfsignore_eexist != 0 && (!NFSHASNFSV4(nmp) ||
2763             nmp->nm_minorvers == 0))
2764                 error = 0;
2765         return (error);
2766 }
2767
2768 /*
2769  * nfs remove directory call
2770  */
2771 int
2772 nfsrpc_rmdir(vnode_t dvp, char *name, int namelen, struct ucred *cred,
2773     NFSPROC_T *p, struct nfsvattr *dnap, int *dattrflagp, void *dstuff)
2774 {
2775         struct nfsrv_descript nfsd, *nd = &nfsd;
2776         int error = 0;
2777
2778         *dattrflagp = 0;
2779         if (namelen > NFS_MAXNAMLEN)
2780                 return (ENAMETOOLONG);
2781         NFSCL_REQSTART(nd, NFSPROC_RMDIR, dvp);
2782         (void) nfsm_strtom(nd, name, namelen);
2783         error = nfscl_request(nd, dvp, p, cred, dstuff);
2784         if (error)
2785                 return (error);
2786         if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4))
2787                 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
2788         if (nd->nd_repstat && !error)
2789                 error = nd->nd_repstat;
2790         mbuf_freem(nd->nd_mrep);
2791         /*
2792          * Kludge: Map ENOENT => 0 assuming that you have a reply to a retry.
2793          */
2794         if (error == ENOENT)
2795                 error = 0;
2796         return (error);
2797 }
2798
2799 /*
2800  * Readdir rpc.
2801  * Always returns with either uio_resid unchanged, if you are at the
2802  * end of the directory, or uio_resid == 0, with all DIRBLKSIZ chunks
2803  * filled in.
2804  * I felt this would allow caching of directory blocks more easily
2805  * than returning a pertially filled block.
2806  * Directory offset cookies:
2807  * Oh my, what to do with them...
2808  * I can think of three ways to deal with them:
2809  * 1 - have the layer above these RPCs maintain a map between logical
2810  *     directory byte offsets and the NFS directory offset cookies
2811  * 2 - pass the opaque directory offset cookies up into userland
2812  *     and let the libc functions deal with them, via the system call
2813  * 3 - return them to userland in the "struct dirent", so future versions
2814  *     of libc can use them and do whatever is necessary to make things work
2815  *     above these rpc calls, in the meantime
2816  * For now, I do #3 by "hiding" the directory offset cookies after the
2817  * d_name field in struct dirent. This is space inside d_reclen that
2818  * will be ignored by anything that doesn't know about them.
2819  * The directory offset cookies are filled in as the last 8 bytes of
2820  * each directory entry, after d_name. Someday, the userland libc
2821  * functions may be able to use these. In the meantime, it satisfies
2822  * OpenBSD's requirements for cookies being returned.
2823  * If expects the directory offset cookie for the read to be in uio_offset
2824  * and returns the one for the next entry after this directory block in
2825  * there, as well.
2826  */
2827 int
2828 nfsrpc_readdir(vnode_t vp, struct uio *uiop, nfsuint64 *cookiep,
2829     struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp,
2830     int *eofp, void *stuff)
2831 {
2832         int len, left;
2833         struct dirent *dp = NULL;
2834         u_int32_t *tl;
2835         nfsquad_t cookie, ncookie;
2836         struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
2837         struct nfsnode *dnp = VTONFS(vp);
2838         struct nfsvattr nfsva;
2839         struct nfsrv_descript nfsd, *nd = &nfsd;
2840         int error = 0, tlen, more_dirs = 1, blksiz = 0, bigenough = 1;
2841         int reqsize, tryformoredirs = 1, readsize, eof = 0, gotmnton = 0;
2842         u_int64_t dotfileid, dotdotfileid = 0, fakefileno = UINT64_MAX;
2843         char *cp;
2844         nfsattrbit_t attrbits, dattrbits;
2845         u_int32_t rderr, *tl2 = NULL;
2846         size_t tresid;
2847
2848         KASSERT(uiop->uio_iovcnt == 1 &&
2849             (uio_uio_resid(uiop) & (DIRBLKSIZ - 1)) == 0,
2850             ("nfs readdirrpc bad uio"));
2851         ncookie.lval[0] = ncookie.lval[1] = 0;
2852         /*
2853          * There is no point in reading a lot more than uio_resid, however
2854          * adding one additional DIRBLKSIZ makes sense. Since uio_resid
2855          * and nm_readdirsize are both exact multiples of DIRBLKSIZ, this
2856          * will never make readsize > nm_readdirsize.
2857          */
2858         readsize = nmp->nm_readdirsize;
2859         if (readsize > uio_uio_resid(uiop))
2860                 readsize = uio_uio_resid(uiop) + DIRBLKSIZ;
2861
2862         *attrflagp = 0;
2863         if (eofp)
2864                 *eofp = 0;
2865         tresid = uio_uio_resid(uiop);
2866         cookie.lval[0] = cookiep->nfsuquad[0];
2867         cookie.lval[1] = cookiep->nfsuquad[1];
2868         nd->nd_mrep = NULL;
2869
2870         /*
2871          * For NFSv4, first create the "." and ".." entries.
2872          */
2873         if (NFSHASNFSV4(nmp)) {
2874                 reqsize = 6 * NFSX_UNSIGNED;
2875                 NFSGETATTR_ATTRBIT(&dattrbits);
2876                 NFSZERO_ATTRBIT(&attrbits);
2877                 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_FILEID);
2878                 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TYPE);
2879                 if (NFSISSET_ATTRBIT(&dnp->n_vattr.na_suppattr,
2880                     NFSATTRBIT_MOUNTEDONFILEID)) {
2881                         NFSSETBIT_ATTRBIT(&attrbits,
2882                             NFSATTRBIT_MOUNTEDONFILEID);
2883                         gotmnton = 1;
2884                 } else {
2885                         /*
2886                          * Must fake it. Use the fileno, except when the
2887                          * fsid is != to that of the directory. For that
2888                          * case, generate a fake fileno that is not the same.
2889                          */
2890                         NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_FSID);
2891                         gotmnton = 0;
2892                 }
2893
2894                 /*
2895                  * Joy, oh joy. For V4 we get to hand craft '.' and '..'.
2896                  */
2897                 if (uiop->uio_offset == 0) {
2898                         NFSCL_REQSTART(nd, NFSPROC_LOOKUPP, vp);
2899                         NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2900                         *tl++ = txdr_unsigned(NFSV4OP_GETFH);
2901                         *tl = txdr_unsigned(NFSV4OP_GETATTR);
2902                         (void) nfsrv_putattrbit(nd, &attrbits);
2903                         error = nfscl_request(nd, vp, p, cred, stuff);
2904                         if (error)
2905                             return (error);
2906                         dotfileid = 0;  /* Fake out the compiler. */
2907                         if ((nd->nd_flag & ND_NOMOREDATA) == 0) {
2908                             error = nfsm_loadattr(nd, &nfsva);
2909                             if (error != 0)
2910                                 goto nfsmout;
2911                             dotfileid = nfsva.na_fileid;
2912                         }
2913                         if (nd->nd_repstat == 0) {
2914                             NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
2915                             len = fxdr_unsigned(int, *(tl + 4));
2916                             if (len > 0 && len <= NFSX_V4FHMAX)
2917                                 error = nfsm_advance(nd, NFSM_RNDUP(len), -1);
2918                             else
2919                                 error = EPERM;
2920                             if (!error) {
2921                                 NFSM_DISSECT(tl, u_int32_t *, 2*NFSX_UNSIGNED);
2922                                 nfsva.na_mntonfileno = UINT64_MAX;
2923                                 error = nfsv4_loadattr(nd, NULL, &nfsva, NULL,
2924                                     NULL, 0, NULL, NULL, NULL, NULL, NULL, 0,
2925                                     NULL, NULL, NULL, p, cred);
2926                                 if (error) {
2927                                     dotdotfileid = dotfileid;
2928                                 } else if (gotmnton) {
2929                                     if (nfsva.na_mntonfileno != UINT64_MAX)
2930                                         dotdotfileid = nfsva.na_mntonfileno;
2931                                     else
2932                                         dotdotfileid = nfsva.na_fileid;
2933                                 } else if (nfsva.na_filesid[0] ==
2934                                     dnp->n_vattr.na_filesid[0] &&
2935                                     nfsva.na_filesid[1] ==
2936                                     dnp->n_vattr.na_filesid[1]) {
2937                                     dotdotfileid = nfsva.na_fileid;
2938                                 } else {
2939                                     do {
2940                                         fakefileno--;
2941                                     } while (fakefileno ==
2942                                         nfsva.na_fileid);
2943                                     dotdotfileid = fakefileno;
2944                                 }
2945                             }
2946                         } else if (nd->nd_repstat == NFSERR_NOENT) {
2947                             /*
2948                              * Lookupp returns NFSERR_NOENT when we are
2949                              * at the root, so just use the current dir.
2950                              */
2951                             nd->nd_repstat = 0;
2952                             dotdotfileid = dotfileid;
2953                         } else {
2954                             error = nd->nd_repstat;
2955                         }
2956                         mbuf_freem(nd->nd_mrep);
2957                         if (error)
2958                             return (error);
2959                         nd->nd_mrep = NULL;
2960                         dp = (struct dirent *)uio_iov_base(uiop);
2961                         dp->d_pad0 = dp->d_pad1 = 0;
2962                         dp->d_off = 0;
2963                         dp->d_type = DT_DIR;
2964                         dp->d_fileno = dotfileid;
2965                         dp->d_namlen = 1;
2966                         *((uint64_t *)dp->d_name) = 0;  /* Zero pad it. */
2967                         dp->d_name[0] = '.';
2968                         dp->d_reclen = _GENERIC_DIRSIZ(dp) + NFSX_HYPER;
2969                         /*
2970                          * Just make these offset cookie 0.
2971                          */
2972                         tl = (u_int32_t *)&dp->d_name[8];
2973                         *tl++ = 0;
2974                         *tl = 0;
2975                         blksiz += dp->d_reclen;
2976                         uio_uio_resid_add(uiop, -(dp->d_reclen));
2977                         uiop->uio_offset += dp->d_reclen;
2978                         uio_iov_base_add(uiop, dp->d_reclen);
2979                         uio_iov_len_add(uiop, -(dp->d_reclen));
2980                         dp = (struct dirent *)uio_iov_base(uiop);
2981                         dp->d_pad0 = dp->d_pad1 = 0;
2982                         dp->d_off = 0;
2983                         dp->d_type = DT_DIR;
2984                         dp->d_fileno = dotdotfileid;
2985                         dp->d_namlen = 2;
2986                         *((uint64_t *)dp->d_name) = 0;
2987                         dp->d_name[0] = '.';
2988                         dp->d_name[1] = '.';
2989                         dp->d_reclen = _GENERIC_DIRSIZ(dp) + NFSX_HYPER;
2990                         /*
2991                          * Just make these offset cookie 0.
2992                          */
2993                         tl = (u_int32_t *)&dp->d_name[8];
2994                         *tl++ = 0;
2995                         *tl = 0;
2996                         blksiz += dp->d_reclen;
2997                         uio_uio_resid_add(uiop, -(dp->d_reclen));
2998                         uiop->uio_offset += dp->d_reclen;
2999                         uio_iov_base_add(uiop, dp->d_reclen);
3000                         uio_iov_len_add(uiop, -(dp->d_reclen));
3001                 }
3002                 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_RDATTRERROR);
3003         } else {
3004                 reqsize = 5 * NFSX_UNSIGNED;
3005         }
3006
3007
3008         /*
3009          * Loop around doing readdir rpc's of size readsize.
3010          * The stopping criteria is EOF or buffer full.
3011          */
3012         while (more_dirs && bigenough) {
3013                 *attrflagp = 0;
3014                 NFSCL_REQSTART(nd, NFSPROC_READDIR, vp);
3015                 if (nd->nd_flag & ND_NFSV2) {
3016                         NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
3017                         *tl++ = cookie.lval[1];
3018                         *tl = txdr_unsigned(readsize);
3019                 } else {
3020                         NFSM_BUILD(tl, u_int32_t *, reqsize);
3021                         *tl++ = cookie.lval[0];
3022                         *tl++ = cookie.lval[1];
3023                         if (cookie.qval == 0) {
3024                                 *tl++ = 0;
3025                                 *tl++ = 0;
3026                         } else {
3027                                 NFSLOCKNODE(dnp);
3028                                 *tl++ = dnp->n_cookieverf.nfsuquad[0];
3029                                 *tl++ = dnp->n_cookieverf.nfsuquad[1];
3030                                 NFSUNLOCKNODE(dnp);
3031                         }
3032                         if (nd->nd_flag & ND_NFSV4) {
3033                                 *tl++ = txdr_unsigned(readsize);
3034                                 *tl = txdr_unsigned(readsize);
3035                                 (void) nfsrv_putattrbit(nd, &attrbits);
3036                                 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
3037                                 *tl = txdr_unsigned(NFSV4OP_GETATTR);
3038                                 (void) nfsrv_putattrbit(nd, &dattrbits);
3039                         } else {
3040                                 *tl = txdr_unsigned(readsize);
3041                         }
3042                 }
3043                 error = nfscl_request(nd, vp, p, cred, stuff);
3044                 if (error)
3045                         return (error);
3046                 if (!(nd->nd_flag & ND_NFSV2)) {
3047                         if (nd->nd_flag & ND_NFSV3)
3048                                 error = nfscl_postop_attr(nd, nap, attrflagp,
3049                                     stuff);
3050                         if (!nd->nd_repstat && !error) {
3051                                 NFSM_DISSECT(tl, u_int32_t *, NFSX_HYPER);
3052                                 NFSLOCKNODE(dnp);
3053                                 dnp->n_cookieverf.nfsuquad[0] = *tl++;
3054                                 dnp->n_cookieverf.nfsuquad[1] = *tl;
3055                                 NFSUNLOCKNODE(dnp);
3056                         }
3057                 }
3058                 if (nd->nd_repstat || error) {
3059                         if (!error)
3060                                 error = nd->nd_repstat;
3061                         goto nfsmout;
3062                 }
3063                 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3064                 more_dirs = fxdr_unsigned(int, *tl);
3065                 if (!more_dirs)
3066                         tryformoredirs = 0;
3067         
3068                 /* loop through the dir entries, doctoring them to 4bsd form */
3069                 while (more_dirs && bigenough) {
3070                         if (nd->nd_flag & ND_NFSV4) {
3071                                 NFSM_DISSECT(tl, u_int32_t *, 3*NFSX_UNSIGNED);
3072                                 ncookie.lval[0] = *tl++;
3073                                 ncookie.lval[1] = *tl++;
3074                                 len = fxdr_unsigned(int, *tl);
3075                         } else if (nd->nd_flag & ND_NFSV3) {
3076                                 NFSM_DISSECT(tl, u_int32_t *, 3*NFSX_UNSIGNED);
3077                                 nfsva.na_fileid = fxdr_hyper(tl);
3078                                 tl += 2;
3079                                 len = fxdr_unsigned(int, *tl);
3080                         } else {
3081                                 NFSM_DISSECT(tl, u_int32_t *, 2*NFSX_UNSIGNED);
3082                                 nfsva.na_fileid = fxdr_unsigned(uint64_t,
3083                                     *tl++);
3084                                 len = fxdr_unsigned(int, *tl);
3085                         }
3086                         if (len <= 0 || len > NFS_MAXNAMLEN) {
3087                                 error = EBADRPC;
3088                                 goto nfsmout;
3089                         }
3090                         tlen = roundup2(len, 8);
3091                         if (tlen == len)
3092                                 tlen += 8;  /* To ensure null termination. */
3093                         left = DIRBLKSIZ - blksiz;
3094                         if (_GENERIC_DIRLEN(len) + NFSX_HYPER > left) {
3095                                 NFSBZERO(uio_iov_base(uiop), left);
3096                                 dp->d_reclen += left;
3097                                 uio_iov_base_add(uiop, left);
3098                                 uio_iov_len_add(uiop, -(left));
3099                                 uio_uio_resid_add(uiop, -(left));
3100                                 uiop->uio_offset += left;
3101                                 blksiz = 0;
3102                         }
3103                         if (_GENERIC_DIRLEN(len) + NFSX_HYPER >
3104                             uio_uio_resid(uiop))
3105                                 bigenough = 0;
3106                         if (bigenough) {
3107                                 dp = (struct dirent *)uio_iov_base(uiop);
3108                                 dp->d_pad0 = dp->d_pad1 = 0;
3109                                 dp->d_off = 0;
3110                                 dp->d_namlen = len;
3111                                 dp->d_reclen = _GENERIC_DIRLEN(len) +
3112                                     NFSX_HYPER;
3113                                 dp->d_type = DT_UNKNOWN;
3114                                 blksiz += dp->d_reclen;
3115                                 if (blksiz == DIRBLKSIZ)
3116                                         blksiz = 0;
3117                                 uio_uio_resid_add(uiop, -(DIRHDSIZ));
3118                                 uiop->uio_offset += DIRHDSIZ;
3119                                 uio_iov_base_add(uiop, DIRHDSIZ);
3120                                 uio_iov_len_add(uiop, -(DIRHDSIZ));
3121                                 error = nfsm_mbufuio(nd, uiop, len);
3122                                 if (error)
3123                                         goto nfsmout;
3124                                 cp = uio_iov_base(uiop);
3125                                 tlen -= len;
3126                                 NFSBZERO(cp, tlen);
3127                                 cp += tlen;     /* points to cookie storage */
3128                                 tl2 = (u_int32_t *)cp;
3129                                 uio_iov_base_add(uiop, (tlen + NFSX_HYPER));
3130                                 uio_iov_len_add(uiop, -(tlen + NFSX_HYPER));
3131                                 uio_uio_resid_add(uiop, -(tlen + NFSX_HYPER));
3132                                 uiop->uio_offset += (tlen + NFSX_HYPER);
3133                         } else {
3134                                 error = nfsm_advance(nd, NFSM_RNDUP(len), -1);
3135                                 if (error)
3136                                         goto nfsmout;
3137                         }
3138                         if (nd->nd_flag & ND_NFSV4) {
3139                                 rderr = 0;
3140                                 nfsva.na_mntonfileno = UINT64_MAX;
3141                                 error = nfsv4_loadattr(nd, NULL, &nfsva, NULL,
3142                                     NULL, 0, NULL, NULL, NULL, NULL, NULL, 0,
3143                                     NULL, NULL, &rderr, p, cred);
3144                                 if (error)
3145                                         goto nfsmout;
3146                                 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3147                         } else if (nd->nd_flag & ND_NFSV3) {
3148                                 NFSM_DISSECT(tl, u_int32_t *, 3*NFSX_UNSIGNED);
3149                                 ncookie.lval[0] = *tl++;
3150                                 ncookie.lval[1] = *tl++;
3151                         } else {
3152                                 NFSM_DISSECT(tl, u_int32_t *, 2*NFSX_UNSIGNED);
3153                                 ncookie.lval[0] = 0;
3154                                 ncookie.lval[1] = *tl++;
3155                         }
3156                         if (bigenough) {
3157                             if (nd->nd_flag & ND_NFSV4) {
3158                                 if (rderr) {
3159                                     dp->d_fileno = 0;
3160                                 } else {
3161                                     if (gotmnton) {
3162                                         if (nfsva.na_mntonfileno != UINT64_MAX)
3163                                             dp->d_fileno = nfsva.na_mntonfileno;
3164                                         else
3165                                             dp->d_fileno = nfsva.na_fileid;
3166                                     } else if (nfsva.na_filesid[0] ==
3167                                         dnp->n_vattr.na_filesid[0] &&
3168                                         nfsva.na_filesid[1] ==
3169                                         dnp->n_vattr.na_filesid[1]) {
3170                                         dp->d_fileno = nfsva.na_fileid;
3171                                     } else {
3172                                         do {
3173                                             fakefileno--;
3174                                         } while (fakefileno ==
3175                                             nfsva.na_fileid);
3176                                         dp->d_fileno = fakefileno;
3177                                     }
3178                                     dp->d_type = vtonfs_dtype(nfsva.na_type);
3179                                 }
3180                             } else {
3181                                 dp->d_fileno = nfsva.na_fileid;
3182                             }
3183                             *tl2++ = cookiep->nfsuquad[0] = cookie.lval[0] =
3184                                 ncookie.lval[0];
3185                             *tl2 = cookiep->nfsuquad[1] = cookie.lval[1] =
3186                                 ncookie.lval[1];
3187                         }
3188                         more_dirs = fxdr_unsigned(int, *tl);
3189                 }
3190                 /*
3191                  * If at end of rpc data, get the eof boolean
3192                  */
3193                 if (!more_dirs) {
3194                         NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3195                         eof = fxdr_unsigned(int, *tl);
3196                         if (tryformoredirs)
3197                                 more_dirs = !eof;
3198                         if (nd->nd_flag & ND_NFSV4) {
3199                                 error = nfscl_postop_attr(nd, nap, attrflagp,
3200                                     stuff);
3201                                 if (error)
3202                                         goto nfsmout;
3203                         }
3204                 }
3205                 mbuf_freem(nd->nd_mrep);
3206                 nd->nd_mrep = NULL;
3207         }
3208         /*
3209          * Fill last record, iff any, out to a multiple of DIRBLKSIZ
3210          * by increasing d_reclen for the last record.
3211          */
3212         if (blksiz > 0) {
3213                 left = DIRBLKSIZ - blksiz;
3214                 NFSBZERO(uio_iov_base(uiop), left);
3215                 dp->d_reclen += left;
3216                 uio_iov_base_add(uiop, left);
3217                 uio_iov_len_add(uiop, -(left));
3218                 uio_uio_resid_add(uiop, -(left));
3219                 uiop->uio_offset += left;
3220         }
3221
3222         /*
3223          * If returning no data, assume end of file.
3224          * If not bigenough, return not end of file, since you aren't
3225          *    returning all the data
3226          * Otherwise, return the eof flag from the server.
3227          */
3228         if (eofp) {
3229                 if (tresid == ((size_t)(uio_uio_resid(uiop))))
3230                         *eofp = 1;
3231                 else if (!bigenough)
3232                         *eofp = 0;
3233                 else
3234                         *eofp = eof;
3235         }
3236
3237         /*
3238          * Add extra empty records to any remaining DIRBLKSIZ chunks.
3239          */
3240         while (uio_uio_resid(uiop) > 0 && uio_uio_resid(uiop) != tresid) {
3241                 dp = (struct dirent *)uio_iov_base(uiop);
3242                 NFSBZERO(dp, DIRBLKSIZ);
3243                 dp->d_type = DT_UNKNOWN;
3244                 tl = (u_int32_t *)&dp->d_name[4];
3245                 *tl++ = cookie.lval[0];
3246                 *tl = cookie.lval[1];
3247                 dp->d_reclen = DIRBLKSIZ;
3248                 uio_iov_base_add(uiop, DIRBLKSIZ);
3249                 uio_iov_len_add(uiop, -(DIRBLKSIZ));
3250                 uio_uio_resid_add(uiop, -(DIRBLKSIZ));
3251                 uiop->uio_offset += DIRBLKSIZ;
3252         }
3253
3254 nfsmout:
3255         if (nd->nd_mrep != NULL)
3256                 mbuf_freem(nd->nd_mrep);
3257         return (error);
3258 }
3259
3260 #ifndef APPLE
3261 /*
3262  * NFS V3 readdir plus RPC. Used in place of nfsrpc_readdir().
3263  * (Also used for NFS V4 when mount flag set.)
3264  * (ditto above w.r.t. multiple of DIRBLKSIZ, etc.)
3265  */
3266 int
3267 nfsrpc_readdirplus(vnode_t vp, struct uio *uiop, nfsuint64 *cookiep,
3268     struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp,
3269     int *eofp, void *stuff)
3270 {
3271         int len, left;
3272         struct dirent *dp = NULL;
3273         u_int32_t *tl;
3274         vnode_t newvp = NULLVP;
3275         struct nfsrv_descript nfsd, *nd = &nfsd;
3276         struct nameidata nami, *ndp = &nami;
3277         struct componentname *cnp = &ndp->ni_cnd;
3278         struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
3279         struct nfsnode *dnp = VTONFS(vp), *np;
3280         struct nfsvattr nfsva;
3281         struct nfsfh *nfhp;
3282         nfsquad_t cookie, ncookie;
3283         int error = 0, tlen, more_dirs = 1, blksiz = 0, bigenough = 1;
3284         int attrflag, tryformoredirs = 1, eof = 0, gotmnton = 0;
3285         int isdotdot = 0, unlocknewvp = 0;
3286         u_int64_t dotfileid, dotdotfileid = 0, fakefileno = UINT64_MAX;
3287         u_int64_t fileno = 0;
3288         char *cp;
3289         nfsattrbit_t attrbits, dattrbits;
3290         size_t tresid;
3291         u_int32_t *tl2 = NULL, rderr;
3292         struct timespec dctime;
3293
3294         KASSERT(uiop->uio_iovcnt == 1 &&
3295             (uio_uio_resid(uiop) & (DIRBLKSIZ - 1)) == 0,
3296             ("nfs readdirplusrpc bad uio"));
3297         ncookie.lval[0] = ncookie.lval[1] = 0;
3298         timespecclear(&dctime);
3299         *attrflagp = 0;
3300         if (eofp != NULL)
3301                 *eofp = 0;
3302         ndp->ni_dvp = vp;
3303         nd->nd_mrep = NULL;
3304         cookie.lval[0] = cookiep->nfsuquad[0];
3305         cookie.lval[1] = cookiep->nfsuquad[1];
3306         tresid = uio_uio_resid(uiop);
3307
3308         /*
3309          * For NFSv4, first create the "." and ".." entries.
3310          */
3311         if (NFSHASNFSV4(nmp)) {
3312                 NFSGETATTR_ATTRBIT(&dattrbits);
3313                 NFSZERO_ATTRBIT(&attrbits);
3314                 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_FILEID);
3315                 if (NFSISSET_ATTRBIT(&dnp->n_vattr.na_suppattr,
3316                     NFSATTRBIT_MOUNTEDONFILEID)) {
3317                         NFSSETBIT_ATTRBIT(&attrbits,
3318                             NFSATTRBIT_MOUNTEDONFILEID);
3319                         gotmnton = 1;
3320                 } else {
3321                         /*
3322                          * Must fake it. Use the fileno, except when the
3323                          * fsid is != to that of the directory. For that
3324                          * case, generate a fake fileno that is not the same.
3325                          */
3326                         NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_FSID);
3327                         gotmnton = 0;
3328                 }
3329
3330                 /*
3331                  * Joy, oh joy. For V4 we get to hand craft '.' and '..'.
3332                  */
3333                 if (uiop->uio_offset == 0) {
3334                         NFSCL_REQSTART(nd, NFSPROC_LOOKUPP, vp);
3335                         NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
3336                         *tl++ = txdr_unsigned(NFSV4OP_GETFH);
3337                         *tl = txdr_unsigned(NFSV4OP_GETATTR);
3338                         (void) nfsrv_putattrbit(nd, &attrbits);
3339                         error = nfscl_request(nd, vp, p, cred, stuff);
3340                         if (error)
3341                             return (error);
3342                         dotfileid = 0;  /* Fake out the compiler. */
3343                         if ((nd->nd_flag & ND_NOMOREDATA) == 0) {
3344                             error = nfsm_loadattr(nd, &nfsva);
3345                             if (error != 0)
3346                                 goto nfsmout;
3347                             dctime = nfsva.na_ctime;
3348                             dotfileid = nfsva.na_fileid;
3349                         }
3350                         if (nd->nd_repstat == 0) {
3351                             NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
3352                             len = fxdr_unsigned(int, *(tl + 4));
3353                             if (len > 0 && len <= NFSX_V4FHMAX)
3354                                 error = nfsm_advance(nd, NFSM_RNDUP(len), -1);
3355                             else
3356                                 error = EPERM;
3357                             if (!error) {
3358                                 NFSM_DISSECT(tl, u_int32_t *, 2*NFSX_UNSIGNED);
3359                                 nfsva.na_mntonfileno = UINT64_MAX;
3360                                 error = nfsv4_loadattr(nd, NULL, &nfsva, NULL,
3361                                     NULL, 0, NULL, NULL, NULL, NULL, NULL, 0,
3362                                     NULL, NULL, NULL, p, cred);
3363                                 if (error) {
3364                                     dotdotfileid = dotfileid;
3365                                 } else if (gotmnton) {
3366                                     if (nfsva.na_mntonfileno != UINT64_MAX)
3367                                         dotdotfileid = nfsva.na_mntonfileno;
3368                                     else
3369                                         dotdotfileid = nfsva.na_fileid;
3370                                 } else if (nfsva.na_filesid[0] ==
3371                                     dnp->n_vattr.na_filesid[0] &&
3372                                     nfsva.na_filesid[1] ==
3373                                     dnp->n_vattr.na_filesid[1]) {
3374                                     dotdotfileid = nfsva.na_fileid;
3375                                 } else {
3376                                     do {
3377                                         fakefileno--;
3378                                     } while (fakefileno ==
3379                                         nfsva.na_fileid);
3380                                     dotdotfileid = fakefileno;
3381                                 }
3382                             }
3383                         } else if (nd->nd_repstat == NFSERR_NOENT) {
3384                             /*
3385                              * Lookupp returns NFSERR_NOENT when we are
3386                              * at the root, so just use the current dir.
3387                              */
3388                             nd->nd_repstat = 0;
3389                             dotdotfileid = dotfileid;
3390                         } else {
3391                             error = nd->nd_repstat;
3392                         }
3393                         mbuf_freem(nd->nd_mrep);
3394                         if (error)
3395                             return (error);
3396                         nd->nd_mrep = NULL;
3397                         dp = (struct dirent *)uio_iov_base(uiop);
3398                         dp->d_pad0 = dp->d_pad1 = 0;
3399                         dp->d_off = 0;
3400                         dp->d_type = DT_DIR;
3401                         dp->d_fileno = dotfileid;
3402                         dp->d_namlen = 1;
3403                         *((uint64_t *)dp->d_name) = 0;  /* Zero pad it. */
3404                         dp->d_name[0] = '.';
3405                         dp->d_reclen = _GENERIC_DIRSIZ(dp) + NFSX_HYPER;
3406                         /*
3407                          * Just make these offset cookie 0.
3408                          */
3409                         tl = (u_int32_t *)&dp->d_name[8];
3410                         *tl++ = 0;
3411                         *tl = 0;
3412                         blksiz += dp->d_reclen;
3413                         uio_uio_resid_add(uiop, -(dp->d_reclen));
3414                         uiop->uio_offset += dp->d_reclen;
3415                         uio_iov_base_add(uiop, dp->d_reclen);
3416                         uio_iov_len_add(uiop, -(dp->d_reclen));
3417                         dp = (struct dirent *)uio_iov_base(uiop);
3418                         dp->d_pad0 = dp->d_pad1 = 0;
3419                         dp->d_off = 0;
3420                         dp->d_type = DT_DIR;
3421                         dp->d_fileno = dotdotfileid;
3422                         dp->d_namlen = 2;
3423                         *((uint64_t *)dp->d_name) = 0;
3424                         dp->d_name[0] = '.';
3425                         dp->d_name[1] = '.';
3426                         dp->d_reclen = _GENERIC_DIRSIZ(dp) + NFSX_HYPER;
3427                         /*
3428                          * Just make these offset cookie 0.
3429                          */
3430                         tl = (u_int32_t *)&dp->d_name[8];
3431                         *tl++ = 0;
3432                         *tl = 0;
3433                         blksiz += dp->d_reclen;
3434                         uio_uio_resid_add(uiop, -(dp->d_reclen));
3435                         uiop->uio_offset += dp->d_reclen;
3436                         uio_iov_base_add(uiop, dp->d_reclen);
3437                         uio_iov_len_add(uiop, -(dp->d_reclen));
3438                 }
3439                 NFSREADDIRPLUS_ATTRBIT(&attrbits);
3440                 if (gotmnton)
3441                         NFSSETBIT_ATTRBIT(&attrbits,
3442                             NFSATTRBIT_MOUNTEDONFILEID);
3443         }
3444
3445         /*
3446          * Loop around doing readdir rpc's of size nm_readdirsize.
3447          * The stopping criteria is EOF or buffer full.
3448          */
3449         while (more_dirs && bigenough) {
3450                 *attrflagp = 0;
3451                 NFSCL_REQSTART(nd, NFSPROC_READDIRPLUS, vp);
3452                 NFSM_BUILD(tl, u_int32_t *, 6 * NFSX_UNSIGNED);
3453                 *tl++ = cookie.lval[0];
3454                 *tl++ = cookie.lval[1];
3455                 if (cookie.qval == 0) {
3456                         *tl++ = 0;
3457                         *tl++ = 0;
3458                 } else {
3459                         NFSLOCKNODE(dnp);
3460                         *tl++ = dnp->n_cookieverf.nfsuquad[0];
3461                         *tl++ = dnp->n_cookieverf.nfsuquad[1];
3462                         NFSUNLOCKNODE(dnp);
3463                 }
3464                 *tl++ = txdr_unsigned(nmp->nm_readdirsize);
3465                 *tl = txdr_unsigned(nmp->nm_readdirsize);
3466                 if (nd->nd_flag & ND_NFSV4) {
3467                         (void) nfsrv_putattrbit(nd, &attrbits);
3468                         NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
3469                         *tl = txdr_unsigned(NFSV4OP_GETATTR);
3470                         (void) nfsrv_putattrbit(nd, &dattrbits);
3471                 }
3472                 error = nfscl_request(nd, vp, p, cred, stuff);
3473                 if (error)
3474                         return (error);
3475                 if (nd->nd_flag & ND_NFSV3)
3476                         error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
3477                 if (nd->nd_repstat || error) {
3478                         if (!error)
3479                                 error = nd->nd_repstat;
3480                         goto nfsmout;
3481                 }
3482                 if ((nd->nd_flag & ND_NFSV3) != 0 && *attrflagp != 0)
3483                         dctime = nap->na_ctime;
3484                 NFSM_DISSECT(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
3485                 NFSLOCKNODE(dnp);
3486                 dnp->n_cookieverf.nfsuquad[0] = *tl++;
3487                 dnp->n_cookieverf.nfsuquad[1] = *tl++;
3488                 NFSUNLOCKNODE(dnp);
3489                 more_dirs = fxdr_unsigned(int, *tl);
3490                 if (!more_dirs)
3491                         tryformoredirs = 0;
3492         
3493                 /* loop through the dir entries, doctoring them to 4bsd form */
3494                 while (more_dirs && bigenough) {
3495                         NFSM_DISSECT(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
3496                         if (nd->nd_flag & ND_NFSV4) {
3497                                 ncookie.lval[0] = *tl++;
3498                                 ncookie.lval[1] = *tl++;
3499                         } else {
3500                                 fileno = fxdr_hyper(tl);
3501                                 tl += 2;
3502                         }
3503                         len = fxdr_unsigned(int, *tl);
3504                         if (len <= 0 || len > NFS_MAXNAMLEN) {
3505                                 error = EBADRPC;
3506                                 goto nfsmout;
3507                         }
3508                         tlen = roundup2(len, 8);
3509                         if (tlen == len)
3510                                 tlen += 8;  /* To ensure null termination. */
3511                         left = DIRBLKSIZ - blksiz;
3512                         if (_GENERIC_DIRLEN(len) + NFSX_HYPER > left) {
3513                                 NFSBZERO(uio_iov_base(uiop), left);
3514                                 dp->d_reclen += left;
3515                                 uio_iov_base_add(uiop, left);
3516                                 uio_iov_len_add(uiop, -(left));
3517                                 uio_uio_resid_add(uiop, -(left));
3518                                 uiop->uio_offset += left;
3519                                 blksiz = 0;
3520                         }
3521                         if (_GENERIC_DIRLEN(len) + NFSX_HYPER >
3522                             uio_uio_resid(uiop))
3523                                 bigenough = 0;
3524                         if (bigenough) {
3525                                 dp = (struct dirent *)uio_iov_base(uiop);
3526                                 dp->d_pad0 = dp->d_pad1 = 0;
3527                                 dp->d_off = 0;
3528                                 dp->d_namlen = len;
3529                                 dp->d_reclen = _GENERIC_DIRLEN(len) +
3530                                     NFSX_HYPER;
3531                                 dp->d_type = DT_UNKNOWN;
3532                                 blksiz += dp->d_reclen;
3533                                 if (blksiz == DIRBLKSIZ)
3534                                         blksiz = 0;
3535                                 uio_uio_resid_add(uiop, -(DIRHDSIZ));
3536                                 uiop->uio_offset += DIRHDSIZ;
3537                                 uio_iov_base_add(uiop, DIRHDSIZ);
3538                                 uio_iov_len_add(uiop, -(DIRHDSIZ));
3539                                 cnp->cn_nameptr = uio_iov_base(uiop);
3540                                 cnp->cn_namelen = len;
3541                                 NFSCNHASHZERO(cnp);
3542                                 error = nfsm_mbufuio(nd, uiop, len);
3543                                 if (error)
3544                                         goto nfsmout;
3545                                 cp = uio_iov_base(uiop);
3546                                 tlen -= len;
3547                                 NFSBZERO(cp, tlen);
3548                                 cp += tlen;     /* points to cookie storage */
3549                                 tl2 = (u_int32_t *)cp;
3550                                 if (len == 2 && cnp->cn_nameptr[0] == '.' &&
3551                                     cnp->cn_nameptr[1] == '.')
3552                                         isdotdot = 1;
3553                                 else
3554                                         isdotdot = 0;
3555                                 uio_iov_base_add(uiop, (tlen + NFSX_HYPER));
3556                                 uio_iov_len_add(uiop, -(tlen + NFSX_HYPER));
3557                                 uio_uio_resid_add(uiop, -(tlen + NFSX_HYPER));
3558                                 uiop->uio_offset += (tlen + NFSX_HYPER);
3559                         } else {
3560                                 error = nfsm_advance(nd, NFSM_RNDUP(len), -1);
3561                                 if (error)
3562                                         goto nfsmout;
3563                         }
3564                         nfhp = NULL;
3565                         if (nd->nd_flag & ND_NFSV3) {
3566                                 NFSM_DISSECT(tl, u_int32_t *, 3*NFSX_UNSIGNED);
3567                                 ncookie.lval[0] = *tl++;
3568                                 ncookie.lval[1] = *tl++;
3569                                 attrflag = fxdr_unsigned(int, *tl);
3570                                 if (attrflag) {
3571                                   error = nfsm_loadattr(nd, &nfsva);
3572                                   if (error)
3573                                         goto nfsmout;
3574                                 }
3575                                 NFSM_DISSECT(tl,u_int32_t *,NFSX_UNSIGNED);
3576                                 if (*tl) {
3577                                         error = nfsm_getfh(nd, &nfhp);
3578                                         if (error)
3579                                             goto nfsmout;
3580                                 }
3581                                 if (!attrflag && nfhp != NULL) {
3582                                         free(nfhp, M_NFSFH);
3583                                         nfhp = NULL;
3584                                 }
3585                         } else {
3586                                 rderr = 0;
3587                                 nfsva.na_mntonfileno = 0xffffffff;
3588                                 error = nfsv4_loadattr(nd, NULL, &nfsva, &nfhp,
3589                                     NULL, 0, NULL, NULL, NULL, NULL, NULL, 0,
3590                                     NULL, NULL, &rderr, p, cred);
3591                                 if (error)
3592                                         goto nfsmout;
3593                         }
3594
3595                         if (bigenough) {
3596                             if (nd->nd_flag & ND_NFSV4) {
3597                                 if (rderr) {
3598                                     dp->d_fileno = 0;
3599                                 } else if (gotmnton) {
3600                                     if (nfsva.na_mntonfileno != 0xffffffff)
3601                                         dp->d_fileno = nfsva.na_mntonfileno;
3602                                     else
3603                                         dp->d_fileno = nfsva.na_fileid;
3604                                 } else if (nfsva.na_filesid[0] ==
3605                                     dnp->n_vattr.na_filesid[0] &&
3606                                     nfsva.na_filesid[1] ==
3607                                     dnp->n_vattr.na_filesid[1]) {
3608                                     dp->d_fileno = nfsva.na_fileid;
3609                                 } else {
3610                                     do {
3611                                         fakefileno--;
3612                                     } while (fakefileno ==
3613                                         nfsva.na_fileid);
3614                                     dp->d_fileno = fakefileno;
3615                                 }
3616                             } else {
3617                                 dp->d_fileno = fileno;
3618                             }
3619                             *tl2++ = cookiep->nfsuquad[0] = cookie.lval[0] =
3620                                 ncookie.lval[0];
3621                             *tl2 = cookiep->nfsuquad[1] = cookie.lval[1] =
3622                                 ncookie.lval[1];
3623
3624                             if (nfhp != NULL) {
3625                                 if (NFSRV_CMPFH(nfhp->nfh_fh, nfhp->nfh_len,
3626                                     dnp->n_fhp->nfh_fh, dnp->n_fhp->nfh_len)) {
3627                                     VREF(vp);
3628                                     newvp = vp;
3629                                     unlocknewvp = 0;
3630                                     free(nfhp, M_NFSFH);
3631                                     np = dnp;
3632                                 } else if (isdotdot != 0) {
3633                                     /*
3634                                      * Skip doing a nfscl_nget() call for "..".
3635                                      * There's a race between acquiring the nfs
3636                                      * node here and lookups that look for the
3637                                      * directory being read (in the parent).
3638                                      * It would try to get a lock on ".." here,
3639                                      * owning the lock on the directory being
3640                                      * read. Lookup will hold the lock on ".."
3641                                      * and try to acquire the lock on the
3642                                      * directory being read.
3643                                      * If the directory is unlocked/relocked,
3644                                      * then there is a LOR with the buflock
3645                                      * vp is relocked.
3646                                      */
3647                                     free(nfhp, M_NFSFH);
3648                                 } else {
3649                                     error = nfscl_nget(vnode_mount(vp), vp,
3650                                       nfhp, cnp, p, &np, NULL, LK_EXCLUSIVE);
3651                                     if (!error) {
3652                                         newvp = NFSTOV(np);
3653                                         unlocknewvp = 1;
3654                                     }
3655                                 }
3656                                 nfhp = NULL;
3657                                 if (newvp != NULLVP) {
3658                                     error = nfscl_loadattrcache(&newvp,
3659                                         &nfsva, NULL, NULL, 0, 0);
3660                                     if (error) {
3661                                         if (unlocknewvp)
3662                                             vput(newvp);
3663                                         else
3664                                             vrele(newvp);
3665                                         goto nfsmout;
3666                                     }
3667                                     dp->d_type =
3668                                         vtonfs_dtype(np->n_vattr.na_type);
3669                                     ndp->ni_vp = newvp;
3670                                     NFSCNHASH(cnp, HASHINIT);
3671                                     if (cnp->cn_namelen <= NCHNAMLEN &&
3672                                         ndp->ni_dvp != ndp->ni_vp &&
3673                                         (newvp->v_type != VDIR ||
3674                                          dctime.tv_sec != 0)) {
3675                                         cache_enter_time(ndp->ni_dvp,
3676                                             ndp->ni_vp, cnp,
3677                                             &nfsva.na_ctime,
3678                                             newvp->v_type != VDIR ? NULL :
3679                                             &dctime);
3680                                     }
3681                                     if (unlocknewvp)
3682                                         vput(newvp);
3683                                     else
3684                                         vrele(newvp);
3685                                     newvp = NULLVP;
3686                                 }
3687                             }
3688                         } else if (nfhp != NULL) {
3689                             free(nfhp, M_NFSFH);
3690                         }
3691                         NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3692                         more_dirs = fxdr_unsigned(int, *tl);
3693                 }
3694                 /*
3695                  * If at end of rpc data, get the eof boolean
3696                  */
3697                 if (!more_dirs) {
3698                         NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3699                         eof = fxdr_unsigned(int, *tl);
3700                         if (tryformoredirs)
3701                                 more_dirs = !eof;
3702                         if (nd->nd_flag & ND_NFSV4) {
3703                                 error = nfscl_postop_attr(nd, nap, attrflagp,
3704                                     stuff);
3705                                 if (error)
3706                                         goto nfsmout;
3707                         }
3708                 }
3709                 mbuf_freem(nd->nd_mrep);
3710                 nd->nd_mrep = NULL;
3711         }
3712         /*
3713          * Fill last record, iff any, out to a multiple of DIRBLKSIZ
3714          * by increasing d_reclen for the last record.
3715          */
3716         if (blksiz > 0) {
3717                 left = DIRBLKSIZ - blksiz;
3718                 NFSBZERO(uio_iov_base(uiop), left);
3719                 dp->d_reclen += left;
3720                 uio_iov_base_add(uiop, left);
3721                 uio_iov_len_add(uiop, -(left));
3722                 uio_uio_resid_add(uiop, -(left));
3723                 uiop->uio_offset += left;
3724         }
3725
3726         /*
3727          * If returning no data, assume end of file.
3728          * If not bigenough, return not end of file, since you aren't
3729          *    returning all the data
3730          * Otherwise, return the eof flag from the server.
3731          */
3732         if (eofp != NULL) {
3733                 if (tresid == uio_uio_resid(uiop))
3734                         *eofp = 1;
3735                 else if (!bigenough)
3736                         *eofp = 0;
3737                 else
3738                         *eofp = eof;
3739         }
3740
3741         /*
3742          * Add extra empty records to any remaining DIRBLKSIZ chunks.
3743          */
3744         while (uio_uio_resid(uiop) > 0 && uio_uio_resid(uiop) != tresid) {
3745                 dp = (struct dirent *)uio_iov_base(uiop);
3746                 NFSBZERO(dp, DIRBLKSIZ);
3747                 dp->d_type = DT_UNKNOWN;
3748                 tl = (u_int32_t *)&dp->d_name[4];
3749                 *tl++ = cookie.lval[0];
3750                 *tl = cookie.lval[1];
3751                 dp->d_reclen = DIRBLKSIZ;
3752                 uio_iov_base_add(uiop, DIRBLKSIZ);
3753                 uio_iov_len_add(uiop, -(DIRBLKSIZ));
3754                 uio_uio_resid_add(uiop, -(DIRBLKSIZ));
3755                 uiop->uio_offset += DIRBLKSIZ;
3756         }
3757
3758 nfsmout:
3759         if (nd->nd_mrep != NULL)
3760                 mbuf_freem(nd->nd_mrep);
3761         return (error);
3762 }
3763 #endif  /* !APPLE */
3764
3765 /*
3766  * Nfs commit rpc
3767  */
3768 int
3769 nfsrpc_commit(vnode_t vp, u_quad_t offset, int cnt, struct ucred *cred,
3770     NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, void *stuff)
3771 {
3772         u_int32_t *tl;
3773         struct nfsrv_descript nfsd, *nd = &nfsd;
3774         nfsattrbit_t attrbits;
3775         int error;
3776         struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
3777         
3778         *attrflagp = 0;
3779         NFSCL_REQSTART(nd, NFSPROC_COMMIT, vp);
3780         NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
3781         txdr_hyper(offset, tl);
3782         tl += 2;
3783         *tl = txdr_unsigned(cnt);
3784         if (nd->nd_flag & ND_NFSV4) {
3785                 /*
3786                  * And do a Getattr op.
3787                  */
3788                 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
3789                 *tl = txdr_unsigned(NFSV4OP_GETATTR);
3790                 NFSGETATTR_ATTRBIT(&attrbits);
3791                 (void) nfsrv_putattrbit(nd, &attrbits);
3792         }
3793         error = nfscl_request(nd, vp, p, cred, stuff);
3794         if (error)
3795                 return (error);
3796         error = nfscl_wcc_data(nd, vp, nap, attrflagp, NULL, stuff);
3797         if (!error && !nd->nd_repstat) {
3798                 NFSM_DISSECT(tl, u_int32_t *, NFSX_VERF);
3799                 NFSLOCKMNT(nmp);
3800                 if (NFSBCMP(nmp->nm_verf, tl, NFSX_VERF)) {
3801                         NFSBCOPY(tl, nmp->nm_verf, NFSX_VERF);
3802                         nd->nd_repstat = NFSERR_STALEWRITEVERF;
3803                 }
3804                 NFSUNLOCKMNT(nmp);
3805                 if (nd->nd_flag & ND_NFSV4)
3806                         error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
3807         }
3808 nfsmout:
3809         if (!error && nd->nd_repstat)
3810                 error = nd->nd_repstat;
3811         mbuf_freem(nd->nd_mrep);
3812         return (error);
3813 }
3814
3815 /*
3816  * NFS byte range lock rpc.
3817  * (Mostly just calls one of the three lower level RPC routines.)
3818  */
3819 int
3820 nfsrpc_advlock(vnode_t vp, off_t size, int op, struct flock *fl,
3821     int reclaim, struct ucred *cred, NFSPROC_T *p, void *id, int flags)
3822 {
3823         struct nfscllockowner *lp;
3824         struct nfsclclient *clp;
3825         struct nfsfh *nfhp;
3826         struct nfsrv_descript nfsd, *nd = &nfsd;
3827         struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
3828         u_int64_t off, len;
3829         off_t start, end;
3830         u_int32_t clidrev = 0;
3831         int error = 0, newone = 0, expireret = 0, retrycnt, donelocally;
3832         int callcnt, dorpc;
3833
3834         /*
3835          * Convert the flock structure into a start and end and do POSIX
3836          * bounds checking.
3837          */
3838         switch (fl->l_whence) {
3839         case SEEK_SET:
3840         case SEEK_CUR:
3841                 /*
3842                  * Caller is responsible for adding any necessary offset
3843                  * when SEEK_CUR is used.
3844                  */
3845                 start = fl->l_start;
3846                 off = fl->l_start;
3847                 break;
3848         case SEEK_END:
3849                 start = size + fl->l_start;
3850                 off = size + fl->l_start;
3851                 break;
3852         default:
3853                 return (EINVAL);
3854         }
3855         if (start < 0)
3856                 return (EINVAL);
3857         if (fl->l_len != 0) {
3858                 end = start + fl->l_len - 1;
3859                 if (end < start)
3860                         return (EINVAL);
3861         }
3862
3863         len = fl->l_len;
3864         if (len == 0)
3865                 len = NFS64BITSSET;
3866         retrycnt = 0;
3867         do {
3868             nd->nd_repstat = 0;
3869             if (op == F_GETLK) {
3870                 error = nfscl_getcl(vnode_mount(vp), cred, p, 1, &clp);
3871                 if (error)
3872                         return (error);
3873                 error = nfscl_lockt(vp, clp, off, len, fl, p, id, flags);
3874                 if (!error) {
3875                         clidrev = clp->nfsc_clientidrev;
3876                         error = nfsrpc_lockt(nd, vp, clp, off, len, fl, cred,
3877                             p, id, flags);
3878                 } else if (error == -1) {
3879                         error = 0;
3880                 }
3881                 nfscl_clientrelease(clp);
3882             } else if (op == F_UNLCK && fl->l_type == F_UNLCK) {
3883                 /*
3884                  * We must loop around for all lockowner cases.
3885                  */
3886                 callcnt = 0;
3887                 error = nfscl_getcl(vnode_mount(vp), cred, p, 1, &clp);
3888                 if (error)
3889                         return (error);
3890                 do {
3891                     error = nfscl_relbytelock(vp, off, len, cred, p, callcnt,
3892                         clp, id, flags, &lp, &dorpc);
3893                     /*
3894                      * If it returns a NULL lp, we're done.
3895                      */
3896                     if (lp == NULL) {
3897                         if (callcnt == 0)
3898                             nfscl_clientrelease(clp);
3899                         else
3900                             nfscl_releasealllocks(clp, vp, p, id, flags);
3901                         return (error);
3902                     }
3903                     if (nmp->nm_clp != NULL)
3904                         clidrev = nmp->nm_clp->nfsc_clientidrev;
3905                     else
3906                         clidrev = 0;
3907                     /*
3908                      * If the server doesn't support Posix lock semantics,
3909                      * only allow locks on the entire file, since it won't
3910                      * handle overlapping byte ranges.
3911                      * There might still be a problem when a lock
3912                      * upgrade/downgrade (read<->write) occurs, since the
3913                      * server "might" expect an unlock first?
3914                      */
3915                     if (dorpc && (lp->nfsl_open->nfso_posixlock ||
3916                         (off == 0 && len == NFS64BITSSET))) {
3917                         /*
3918                          * Since the lock records will go away, we must
3919                          * wait for grace and delay here.
3920                          */
3921                         do {
3922                             error = nfsrpc_locku(nd, nmp, lp, off, len,
3923                                 NFSV4LOCKT_READ, cred, p, 0);
3924                             if ((nd->nd_repstat == NFSERR_GRACE ||
3925                                  nd->nd_repstat == NFSERR_DELAY) &&
3926                                 error == 0)
3927                                 (void) nfs_catnap(PZERO, (int)nd->nd_repstat,
3928                                     "nfs_advlock");
3929                         } while ((nd->nd_repstat == NFSERR_GRACE ||
3930                             nd->nd_repstat == NFSERR_DELAY) && error == 0);
3931                     }
3932                     callcnt++;
3933                 } while (error == 0 && nd->nd_repstat == 0);
3934                 nfscl_releasealllocks(clp, vp, p, id, flags);
3935             } else if (op == F_SETLK) {
3936                 error = nfscl_getbytelock(vp, off, len, fl->l_type, cred, p,
3937                     NULL, 0, id, flags, NULL, NULL, &lp, &newone, &donelocally);
3938                 if (error || donelocally) {
3939                         return (error);
3940                 }
3941                 if (nmp->nm_clp != NULL)
3942                         clidrev = nmp->nm_clp->nfsc_clientidrev;
3943                 else
3944                         clidrev = 0;
3945                 nfhp = VTONFS(vp)->n_fhp;
3946                 if (!lp->nfsl_open->nfso_posixlock &&
3947                     (off != 0 || len != NFS64BITSSET)) {
3948                         error = EINVAL;
3949                 } else {
3950                         error = nfsrpc_lock(nd, nmp, vp, nfhp->nfh_fh,
3951                             nfhp->nfh_len, lp, newone, reclaim, off,
3952                             len, fl->l_type, cred, p, 0);
3953                 }
3954                 if (!error)
3955                         error = nd->nd_repstat;
3956                 nfscl_lockrelease(lp, error, newone);
3957             } else {
3958                 error = EINVAL;
3959             }
3960             if (!error)
3961                 error = nd->nd_repstat;
3962             if (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
3963                 error == NFSERR_STALEDONTRECOVER ||
3964                 error == NFSERR_STALECLIENTID || error == NFSERR_DELAY ||
3965                 error == NFSERR_BADSESSION) {
3966                 (void) nfs_catnap(PZERO, error, "nfs_advlock");
3967             } else if ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID)
3968                 && clidrev != 0) {
3969                 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
3970                 retrycnt++;
3971             }
3972         } while (error == NFSERR_GRACE ||
3973             error == NFSERR_STALECLIENTID || error == NFSERR_DELAY ||
3974             error == NFSERR_STALEDONTRECOVER || error == NFSERR_STALESTATEID ||
3975             error == NFSERR_BADSESSION ||
3976             ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
3977              expireret == 0 && clidrev != 0 && retrycnt < 4));
3978         if (error && retrycnt >= 4)
3979                 error = EIO;
3980         return (error);
3981 }
3982
3983 /*
3984  * The lower level routine for the LockT case.
3985  */
3986 int
3987 nfsrpc_lockt(struct nfsrv_descript *nd, vnode_t vp,
3988     struct nfsclclient *clp, u_int64_t off, u_int64_t len, struct flock *fl,
3989     struct ucred *cred, NFSPROC_T *p, void *id, int flags)
3990 {
3991         u_int32_t *tl;
3992         int error, type, size;
3993         uint8_t own[NFSV4CL_LOCKNAMELEN + NFSX_V4FHMAX];
3994         struct nfsnode *np;
3995         struct nfsmount *nmp;
3996         struct nfsclsession *tsep;
3997
3998         nmp = VFSTONFS(vp->v_mount);
3999         NFSCL_REQSTART(nd, NFSPROC_LOCKT, vp);
4000         NFSM_BUILD(tl, u_int32_t *, 7 * NFSX_UNSIGNED);
4001         if (fl->l_type == F_RDLCK)
4002                 *tl++ = txdr_unsigned(NFSV4LOCKT_READ);
4003         else
4004                 *tl++ = txdr_unsigned(NFSV4LOCKT_WRITE);
4005         txdr_hyper(off, tl);
4006         tl += 2;
4007         txdr_hyper(len, tl);
4008         tl += 2;
4009         tsep = nfsmnt_mdssession(nmp);
4010         *tl++ = tsep->nfsess_clientid.lval[0];
4011         *tl = tsep->nfsess_clientid.lval[1];
4012         nfscl_filllockowner(id, own, flags);
4013         np = VTONFS(vp);
4014         NFSBCOPY(np->n_fhp->nfh_fh, &own[NFSV4CL_LOCKNAMELEN],
4015             np->n_fhp->nfh_len);
4016         (void)nfsm_strtom(nd, own, NFSV4CL_LOCKNAMELEN + np->n_fhp->nfh_len);
4017         error = nfscl_request(nd, vp, p, cred, NULL);
4018         if (error)
4019                 return (error);
4020         if (nd->nd_repstat == 0) {
4021                 fl->l_type = F_UNLCK;
4022         } else if (nd->nd_repstat == NFSERR_DENIED) {
4023                 nd->nd_repstat = 0;
4024                 fl->l_whence = SEEK_SET;
4025                 NFSM_DISSECT(tl, u_int32_t *, 8 * NFSX_UNSIGNED);
4026                 fl->l_start = fxdr_hyper(tl);
4027                 tl += 2;
4028                 len = fxdr_hyper(tl);
4029                 tl += 2;
4030                 if (len == NFS64BITSSET)
4031                         fl->l_len = 0;
4032                 else
4033                         fl->l_len = len;
4034                 type = fxdr_unsigned(int, *tl++);
4035                 if (type == NFSV4LOCKT_WRITE)
4036                         fl->l_type = F_WRLCK;
4037                 else
4038                         fl->l_type = F_RDLCK;
4039                 /*
4040                  * XXX For now, I have no idea what to do with the
4041                  * conflicting lock_owner, so I'll just set the pid == 0
4042                  * and skip over the lock_owner.
4043                  */
4044                 fl->l_pid = (pid_t)0;
4045                 tl += 2;
4046                 size = fxdr_unsigned(int, *tl);
4047                 if (size < 0 || size > NFSV4_OPAQUELIMIT)
4048                         error = EBADRPC;
4049                 if (!error)
4050                         error = nfsm_advance(nd, NFSM_RNDUP(size), -1);
4051         } else if (nd->nd_repstat == NFSERR_STALECLIENTID)
4052                 nfscl_initiate_recovery(clp);
4053 nfsmout:
4054         mbuf_freem(nd->nd_mrep);
4055         return (error);
4056 }
4057
4058 /*
4059  * Lower level function that performs the LockU RPC.
4060  */
4061 static int
4062 nfsrpc_locku(struct nfsrv_descript *nd, struct nfsmount *nmp,
4063     struct nfscllockowner *lp, u_int64_t off, u_int64_t len,
4064     u_int32_t type, struct ucred *cred, NFSPROC_T *p, int syscred)
4065 {
4066         u_int32_t *tl;
4067         int error;
4068
4069         nfscl_reqstart(nd, NFSPROC_LOCKU, nmp, lp->nfsl_open->nfso_fh,
4070             lp->nfsl_open->nfso_fhlen, NULL, NULL, 0, 0);
4071         NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID + 6 * NFSX_UNSIGNED);
4072         *tl++ = txdr_unsigned(type);
4073         *tl = txdr_unsigned(lp->nfsl_seqid);
4074         if (nfstest_outofseq &&
4075             (arc4random() % nfstest_outofseq) == 0)
4076                 *tl = txdr_unsigned(lp->nfsl_seqid + 1);
4077         tl++;
4078         if (NFSHASNFSV4N(nmp))
4079                 *tl++ = 0;
4080         else
4081                 *tl++ = lp->nfsl_stateid.seqid;
4082         *tl++ = lp->nfsl_stateid.other[0];
4083         *tl++ = lp->nfsl_stateid.other[1];
4084         *tl++ = lp->nfsl_stateid.other[2];
4085         txdr_hyper(off, tl);
4086         tl += 2;
4087         txdr_hyper(len, tl);
4088         if (syscred)
4089                 nd->nd_flag |= ND_USEGSSNAME;
4090         error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4091             NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4092         NFSCL_INCRSEQID(lp->nfsl_seqid, nd);
4093         if (error)
4094                 return (error);
4095         if (nd->nd_repstat == 0) {
4096                 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID);
4097                 lp->nfsl_stateid.seqid = *tl++;
4098                 lp->nfsl_stateid.other[0] = *tl++;
4099                 lp->nfsl_stateid.other[1] = *tl++;
4100                 lp->nfsl_stateid.other[2] = *tl;
4101         } else if (nd->nd_repstat == NFSERR_STALESTATEID)
4102                 nfscl_initiate_recovery(lp->nfsl_open->nfso_own->nfsow_clp);
4103 nfsmout:
4104         mbuf_freem(nd->nd_mrep);
4105         return (error);
4106 }
4107
4108 /*
4109  * The actual Lock RPC.
4110  */
4111 int
4112 nfsrpc_lock(struct nfsrv_descript *nd, struct nfsmount *nmp, vnode_t vp,
4113     u_int8_t *nfhp, int fhlen, struct nfscllockowner *lp, int newone,
4114     int reclaim, u_int64_t off, u_int64_t len, short type, struct ucred *cred,
4115     NFSPROC_T *p, int syscred)
4116 {
4117         u_int32_t *tl;
4118         int error, size;
4119         uint8_t own[NFSV4CL_LOCKNAMELEN + NFSX_V4FHMAX];
4120         struct nfsclsession *tsep;
4121
4122         nfscl_reqstart(nd, NFSPROC_LOCK, nmp, nfhp, fhlen, NULL, NULL, 0, 0);
4123         NFSM_BUILD(tl, u_int32_t *, 7 * NFSX_UNSIGNED);
4124         if (type == F_RDLCK)
4125                 *tl++ = txdr_unsigned(NFSV4LOCKT_READ);
4126         else
4127                 *tl++ = txdr_unsigned(NFSV4LOCKT_WRITE);
4128         *tl++ = txdr_unsigned(reclaim);
4129         txdr_hyper(off, tl);
4130         tl += 2;
4131         txdr_hyper(len, tl);
4132         tl += 2;
4133         if (newone) {
4134             *tl = newnfs_true;
4135             NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID +
4136                 2 * NFSX_UNSIGNED + NFSX_HYPER);
4137             *tl++ = txdr_unsigned(lp->nfsl_open->nfso_own->nfsow_seqid);
4138             if (NFSHASNFSV4N(nmp))
4139                 *tl++ = 0;
4140             else
4141                 *tl++ = lp->nfsl_open->nfso_stateid.seqid;
4142             *tl++ = lp->nfsl_open->nfso_stateid.other[0];
4143             *tl++ = lp->nfsl_open->nfso_stateid.other[1];
4144             *tl++ = lp->nfsl_open->nfso_stateid.other[2];
4145             *tl++ = txdr_unsigned(lp->nfsl_seqid);
4146             tsep = nfsmnt_mdssession(nmp);
4147             *tl++ = tsep->nfsess_clientid.lval[0];
4148             *tl = tsep->nfsess_clientid.lval[1];
4149             NFSBCOPY(lp->nfsl_owner, own, NFSV4CL_LOCKNAMELEN);
4150             NFSBCOPY(nfhp, &own[NFSV4CL_LOCKNAMELEN], fhlen);
4151             (void)nfsm_strtom(nd, own, NFSV4CL_LOCKNAMELEN + fhlen);
4152         } else {
4153             *tl = newnfs_false;
4154             NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID + NFSX_UNSIGNED);
4155             if (NFSHASNFSV4N(nmp))
4156                 *tl++ = 0;
4157             else
4158                 *tl++ = lp->nfsl_stateid.seqid;
4159             *tl++ = lp->nfsl_stateid.other[0];
4160             *tl++ = lp->nfsl_stateid.other[1];
4161             *tl++ = lp->nfsl_stateid.other[2];
4162             *tl = txdr_unsigned(lp->nfsl_seqid);
4163             if (nfstest_outofseq &&
4164                 (arc4random() % nfstest_outofseq) == 0)
4165                     *tl = txdr_unsigned(lp->nfsl_seqid + 1);
4166         }
4167         if (syscred)
4168                 nd->nd_flag |= ND_USEGSSNAME;
4169         error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, vp, p, cred,
4170             NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4171         if (error)
4172                 return (error);
4173         if (newone)
4174             NFSCL_INCRSEQID(lp->nfsl_open->nfso_own->nfsow_seqid, nd);
4175         NFSCL_INCRSEQID(lp->nfsl_seqid, nd);
4176         if (nd->nd_repstat == 0) {
4177                 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID);
4178                 lp->nfsl_stateid.seqid = *tl++;
4179                 lp->nfsl_stateid.other[0] = *tl++;
4180                 lp->nfsl_stateid.other[1] = *tl++;
4181                 lp->nfsl_stateid.other[2] = *tl;
4182         } else if (nd->nd_repstat == NFSERR_DENIED) {
4183                 NFSM_DISSECT(tl, u_int32_t *, 8 * NFSX_UNSIGNED);
4184                 size = fxdr_unsigned(int, *(tl + 7));
4185                 if (size < 0 || size > NFSV4_OPAQUELIMIT)
4186                         error = EBADRPC;
4187                 if (!error)
4188                         error = nfsm_advance(nd, NFSM_RNDUP(size), -1);
4189         } else if (nd->nd_repstat == NFSERR_STALESTATEID)
4190                 nfscl_initiate_recovery(lp->nfsl_open->nfso_own->nfsow_clp);
4191 nfsmout:
4192         mbuf_freem(nd->nd_mrep);
4193         return (error);
4194 }
4195
4196 /*
4197  * nfs statfs rpc
4198  * (always called with the vp for the mount point)
4199  */
4200 int
4201 nfsrpc_statfs(vnode_t vp, struct nfsstatfs *sbp, struct nfsfsinfo *fsp,
4202     struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp,
4203     void *stuff)
4204 {
4205         u_int32_t *tl = NULL;
4206         struct nfsrv_descript nfsd, *nd = &nfsd;
4207         struct nfsmount *nmp;
4208         nfsattrbit_t attrbits;
4209         int error;
4210
4211         *attrflagp = 0;
4212         nmp = VFSTONFS(vnode_mount(vp));
4213         if (NFSHASNFSV4(nmp)) {
4214                 /*
4215                  * For V4, you actually do a getattr.
4216                  */
4217                 NFSCL_REQSTART(nd, NFSPROC_GETATTR, vp);
4218                 NFSSTATFS_GETATTRBIT(&attrbits);
4219                 (void) nfsrv_putattrbit(nd, &attrbits);
4220                 nd->nd_flag |= ND_USEGSSNAME;
4221                 error = nfscl_request(nd, vp, p, cred, stuff);
4222                 if (error)
4223                         return (error);
4224                 if (nd->nd_repstat == 0) {
4225                         error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0,
4226                             NULL, NULL, sbp, fsp, NULL, 0, NULL, NULL, NULL, p,
4227                             cred);
4228                         if (!error) {
4229                                 nmp->nm_fsid[0] = nap->na_filesid[0];
4230                                 nmp->nm_fsid[1] = nap->na_filesid[1];
4231                                 NFSSETHASSETFSID(nmp);
4232                                 *attrflagp = 1;
4233                         }
4234                 } else {
4235                         error = nd->nd_repstat;
4236                 }
4237                 if (error)
4238                         goto nfsmout;
4239         } else {
4240                 NFSCL_REQSTART(nd, NFSPROC_FSSTAT, vp);
4241                 error = nfscl_request(nd, vp, p, cred, stuff);
4242                 if (error)
4243                         return (error);
4244                 if (nd->nd_flag & ND_NFSV3) {
4245                         error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
4246                         if (error)
4247                                 goto nfsmout;
4248                 }
4249                 if (nd->nd_repstat) {
4250                         error = nd->nd_repstat;
4251                         goto nfsmout;
4252                 }
4253                 NFSM_DISSECT(tl, u_int32_t *,
4254                     NFSX_STATFS(nd->nd_flag & ND_NFSV3));
4255         }
4256         if (NFSHASNFSV3(nmp)) {
4257                 sbp->sf_tbytes = fxdr_hyper(tl); tl += 2;
4258                 sbp->sf_fbytes = fxdr_hyper(tl); tl += 2;
4259                 sbp->sf_abytes = fxdr_hyper(tl); tl += 2;
4260                 sbp->sf_tfiles = fxdr_hyper(tl); tl += 2;
4261                 sbp->sf_ffiles = fxdr_hyper(tl); tl += 2;
4262                 sbp->sf_afiles = fxdr_hyper(tl); tl += 2;
4263                 sbp->sf_invarsec = fxdr_unsigned(u_int32_t, *tl);
4264         } else if (NFSHASNFSV4(nmp) == 0) {
4265                 sbp->sf_tsize = fxdr_unsigned(u_int32_t, *tl++);
4266                 sbp->sf_bsize = fxdr_unsigned(u_int32_t, *tl++);
4267                 sbp->sf_blocks = fxdr_unsigned(u_int32_t, *tl++);
4268                 sbp->sf_bfree = fxdr_unsigned(u_int32_t, *tl++);
4269                 sbp->sf_bavail = fxdr_unsigned(u_int32_t, *tl);
4270         }
4271 nfsmout:
4272         mbuf_freem(nd->nd_mrep);
4273         return (error);
4274 }
4275
4276 /*
4277  * nfs pathconf rpc
4278  */
4279 int
4280 nfsrpc_pathconf(vnode_t vp, struct nfsv3_pathconf *pc,
4281     struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp,
4282     void *stuff)
4283 {
4284         struct nfsrv_descript nfsd, *nd = &nfsd;
4285         struct nfsmount *nmp;
4286         u_int32_t *tl;
4287         nfsattrbit_t attrbits;
4288         int error;
4289
4290         *attrflagp = 0;
4291         nmp = VFSTONFS(vnode_mount(vp));
4292         if (NFSHASNFSV4(nmp)) {
4293                 /*
4294                  * For V4, you actually do a getattr.
4295                  */
4296                 NFSCL_REQSTART(nd, NFSPROC_GETATTR, vp);
4297                 NFSPATHCONF_GETATTRBIT(&attrbits);
4298                 (void) nfsrv_putattrbit(nd, &attrbits);
4299                 nd->nd_flag |= ND_USEGSSNAME;
4300                 error = nfscl_request(nd, vp, p, cred, stuff);
4301                 if (error)
4302                         return (error);
4303                 if (nd->nd_repstat == 0) {
4304                         error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0,
4305                             pc, NULL, NULL, NULL, NULL, 0, NULL, NULL, NULL, p,
4306                             cred);
4307                         if (!error)
4308                                 *attrflagp = 1;
4309                 } else {
4310                         error = nd->nd_repstat;
4311                 }
4312         } else {
4313                 NFSCL_REQSTART(nd, NFSPROC_PATHCONF, vp);
4314                 error = nfscl_request(nd, vp, p, cred, stuff);
4315                 if (error)
4316                         return (error);
4317                 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
4318                 if (nd->nd_repstat && !error)
4319                         error = nd->nd_repstat;
4320                 if (!error) {
4321                         NFSM_DISSECT(tl, u_int32_t *, NFSX_V3PATHCONF);
4322                         pc->pc_linkmax = fxdr_unsigned(u_int32_t, *tl++);
4323                         pc->pc_namemax = fxdr_unsigned(u_int32_t, *tl++);
4324                         pc->pc_notrunc = fxdr_unsigned(u_int32_t, *tl++);
4325                         pc->pc_chownrestricted =
4326                             fxdr_unsigned(u_int32_t, *tl++);
4327                         pc->pc_caseinsensitive =
4328                             fxdr_unsigned(u_int32_t, *tl++);
4329                         pc->pc_casepreserving = fxdr_unsigned(u_int32_t, *tl);
4330                 }
4331         }
4332 nfsmout:
4333         mbuf_freem(nd->nd_mrep);
4334         return (error);
4335 }
4336
4337 /*
4338  * nfs version 3 fsinfo rpc call
4339  */
4340 int
4341 nfsrpc_fsinfo(vnode_t vp, struct nfsfsinfo *fsp, struct ucred *cred,
4342     NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, void *stuff)
4343 {
4344         u_int32_t *tl;
4345         struct nfsrv_descript nfsd, *nd = &nfsd;
4346         int error;
4347
4348         *attrflagp = 0;
4349         NFSCL_REQSTART(nd, NFSPROC_FSINFO, vp);
4350         error = nfscl_request(nd, vp, p, cred, stuff);
4351         if (error)
4352                 return (error);
4353         error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
4354         if (nd->nd_repstat && !error)
4355                 error = nd->nd_repstat;
4356         if (!error) {
4357                 NFSM_DISSECT(tl, u_int32_t *, NFSX_V3FSINFO);
4358                 fsp->fs_rtmax = fxdr_unsigned(u_int32_t, *tl++);
4359                 fsp->fs_rtpref = fxdr_unsigned(u_int32_t, *tl++);
4360                 fsp->fs_rtmult = fxdr_unsigned(u_int32_t, *tl++);
4361                 fsp->fs_wtmax = fxdr_unsigned(u_int32_t, *tl++);
4362                 fsp->fs_wtpref = fxdr_unsigned(u_int32_t, *tl++);
4363                 fsp->fs_wtmult = fxdr_unsigned(u_int32_t, *tl++);
4364                 fsp->fs_dtpref = fxdr_unsigned(u_int32_t, *tl++);
4365                 fsp->fs_maxfilesize = fxdr_hyper(tl);
4366                 tl += 2;
4367                 fxdr_nfsv3time(tl, &fsp->fs_timedelta);
4368                 tl += 2;
4369                 fsp->fs_properties = fxdr_unsigned(u_int32_t, *tl);
4370         }
4371 nfsmout:
4372         mbuf_freem(nd->nd_mrep);
4373         return (error);
4374 }
4375
4376 /*
4377  * This function performs the Renew RPC.
4378  */
4379 int
4380 nfsrpc_renew(struct nfsclclient *clp, struct nfsclds *dsp, struct ucred *cred,
4381     NFSPROC_T *p)
4382 {
4383         u_int32_t *tl;
4384         struct nfsrv_descript nfsd;
4385         struct nfsrv_descript *nd = &nfsd;
4386         struct nfsmount *nmp;
4387         int error;
4388         struct nfssockreq *nrp;
4389         struct nfsclsession *tsep;
4390
4391         nmp = clp->nfsc_nmp;
4392         if (nmp == NULL)
4393                 return (0);
4394         if (dsp == NULL)
4395                 nfscl_reqstart(nd, NFSPROC_RENEW, nmp, NULL, 0, NULL, NULL, 0,
4396                     0);
4397         else
4398                 nfscl_reqstart(nd, NFSPROC_RENEW, nmp, NULL, 0, NULL,
4399                     &dsp->nfsclds_sess, 0, 0);
4400         if (!NFSHASNFSV4N(nmp)) {
4401                 /* NFSv4.1 just uses a Sequence Op and not a Renew. */
4402                 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
4403                 tsep = nfsmnt_mdssession(nmp);
4404                 *tl++ = tsep->nfsess_clientid.lval[0];
4405                 *tl = tsep->nfsess_clientid.lval[1];
4406         }
4407         nrp = NULL;
4408         if (dsp != NULL)
4409                 nrp = dsp->nfsclds_sockp;
4410         if (nrp == NULL)
4411                 /* If NULL, use the MDS socket. */
4412                 nrp = &nmp->nm_sockreq;
4413         nd->nd_flag |= ND_USEGSSNAME;
4414         if (dsp == NULL)
4415                 error = newnfs_request(nd, nmp, NULL, nrp, NULL, p, cred,
4416                     NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4417         else {
4418                 error = newnfs_request(nd, nmp, NULL, nrp, NULL, p, cred,
4419                     NFS_PROG, NFS_VER4, NULL, 1, NULL, &dsp->nfsclds_sess);
4420                 if (error == ENXIO)
4421                         nfscl_cancelreqs(dsp);
4422         }
4423         if (error)
4424                 return (error);
4425         error = nd->nd_repstat;
4426         mbuf_freem(nd->nd_mrep);
4427         return (error);
4428 }
4429
4430 /*
4431  * This function performs the Releaselockowner RPC.
4432  */
4433 int
4434 nfsrpc_rellockown(struct nfsmount *nmp, struct nfscllockowner *lp,
4435     uint8_t *fh, int fhlen, struct ucred *cred, NFSPROC_T *p)
4436 {
4437         struct nfsrv_descript nfsd, *nd = &nfsd;
4438         u_int32_t *tl;
4439         int error;
4440         uint8_t own[NFSV4CL_LOCKNAMELEN + NFSX_V4FHMAX];
4441         struct nfsclsession *tsep;
4442
4443         if (NFSHASNFSV4N(nmp)) {
4444                 /* For NFSv4.1, do a FreeStateID. */
4445                 nfscl_reqstart(nd, NFSPROC_FREESTATEID, nmp, NULL, 0, NULL,
4446                     NULL, 0, 0);
4447                 nfsm_stateidtom(nd, &lp->nfsl_stateid, NFSSTATEID_PUTSTATEID);
4448         } else {
4449                 nfscl_reqstart(nd, NFSPROC_RELEASELCKOWN, nmp, NULL, 0, NULL,
4450                     NULL, 0, 0);
4451                 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
4452                 tsep = nfsmnt_mdssession(nmp);
4453                 *tl++ = tsep->nfsess_clientid.lval[0];
4454                 *tl = tsep->nfsess_clientid.lval[1];
4455                 NFSBCOPY(lp->nfsl_owner, own, NFSV4CL_LOCKNAMELEN);
4456                 NFSBCOPY(fh, &own[NFSV4CL_LOCKNAMELEN], fhlen);
4457                 (void)nfsm_strtom(nd, own, NFSV4CL_LOCKNAMELEN + fhlen);
4458         }
4459         nd->nd_flag |= ND_USEGSSNAME;
4460         error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4461             NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4462         if (error)
4463                 return (error);
4464         error = nd->nd_repstat;
4465         mbuf_freem(nd->nd_mrep);
4466         return (error);
4467 }
4468
4469 /*
4470  * This function performs the Compound to get the mount pt FH.
4471  */
4472 int
4473 nfsrpc_getdirpath(struct nfsmount *nmp, u_char *dirpath, struct ucred *cred,
4474     NFSPROC_T *p)
4475 {
4476         u_int32_t *tl;
4477         struct nfsrv_descript nfsd;
4478         struct nfsrv_descript *nd = &nfsd;
4479         u_char *cp, *cp2;
4480         int error, cnt, len, setnil;
4481         u_int32_t *opcntp;
4482
4483         nfscl_reqstart(nd, NFSPROC_PUTROOTFH, nmp, NULL, 0, &opcntp, NULL, 0,
4484             0);
4485         cp = dirpath;
4486         cnt = 0;
4487         do {
4488                 setnil = 0;
4489                 while (*cp == '/')
4490                         cp++;
4491                 cp2 = cp;
4492                 while (*cp2 != '\0' && *cp2 != '/')
4493                         cp2++;
4494                 if (*cp2 == '/') {
4495                         setnil = 1;
4496                         *cp2 = '\0';
4497                 }
4498                 if (cp2 != cp) {
4499                         NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
4500                         *tl = txdr_unsigned(NFSV4OP_LOOKUP);
4501                         nfsm_strtom(nd, cp, strlen(cp));
4502                         cnt++;
4503                 }
4504                 if (setnil)
4505                         *cp2++ = '/';
4506                 cp = cp2;
4507         } while (*cp != '\0');
4508         if (NFSHASNFSV4N(nmp))
4509                 /* Has a Sequence Op done by nfscl_reqstart(). */
4510                 *opcntp = txdr_unsigned(3 + cnt);
4511         else
4512                 *opcntp = txdr_unsigned(2 + cnt);
4513         NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
4514         *tl = txdr_unsigned(NFSV4OP_GETFH);
4515         nd->nd_flag |= ND_USEGSSNAME;
4516         error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4517                 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4518         if (error)
4519                 return (error);
4520         if (nd->nd_repstat == 0) {
4521                 NFSM_DISSECT(tl, u_int32_t *, (3 + 2 * cnt) * NFSX_UNSIGNED);
4522                 tl += (2 + 2 * cnt);
4523                 if ((len = fxdr_unsigned(int, *tl)) <= 0 ||
4524                         len > NFSX_FHMAX) {
4525                         nd->nd_repstat = NFSERR_BADXDR;
4526                 } else {
4527                         nd->nd_repstat = nfsrv_mtostr(nd, nmp->nm_fh, len);
4528                         if (nd->nd_repstat == 0)
4529                                 nmp->nm_fhsize = len;
4530                 }
4531         }
4532         error = nd->nd_repstat;
4533 nfsmout:
4534         mbuf_freem(nd->nd_mrep);
4535         return (error);
4536 }
4537
4538 /*
4539  * This function performs the Delegreturn RPC.
4540  */
4541 int
4542 nfsrpc_delegreturn(struct nfscldeleg *dp, struct ucred *cred,
4543     struct nfsmount *nmp, NFSPROC_T *p, int syscred)
4544 {
4545         u_int32_t *tl;
4546         struct nfsrv_descript nfsd;
4547         struct nfsrv_descript *nd = &nfsd;
4548         int error;
4549
4550         nfscl_reqstart(nd, NFSPROC_DELEGRETURN, nmp, dp->nfsdl_fh,
4551             dp->nfsdl_fhlen, NULL, NULL, 0, 0);
4552         NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID);
4553         if (NFSHASNFSV4N(nmp))
4554                 *tl++ = 0;
4555         else
4556                 *tl++ = dp->nfsdl_stateid.seqid;
4557         *tl++ = dp->nfsdl_stateid.other[0];
4558         *tl++ = dp->nfsdl_stateid.other[1];
4559         *tl = dp->nfsdl_stateid.other[2];
4560         if (syscred)
4561                 nd->nd_flag |= ND_USEGSSNAME;
4562         error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4563             NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4564         if (error)
4565                 return (error);
4566         error = nd->nd_repstat;
4567         mbuf_freem(nd->nd_mrep);
4568         return (error);
4569 }
4570
4571 /*
4572  * nfs getacl call.
4573  */
4574 int
4575 nfsrpc_getacl(vnode_t vp, struct ucred *cred, NFSPROC_T *p,
4576     struct acl *aclp, void *stuff)
4577 {
4578         struct nfsrv_descript nfsd, *nd = &nfsd;
4579         int error;
4580         nfsattrbit_t attrbits;
4581         struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
4582         
4583         if (nfsrv_useacl == 0 || !NFSHASNFSV4(nmp))
4584                 return (EOPNOTSUPP);
4585         NFSCL_REQSTART(nd, NFSPROC_GETACL, vp);
4586         NFSZERO_ATTRBIT(&attrbits);
4587         NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_ACL);
4588         (void) nfsrv_putattrbit(nd, &attrbits);
4589         error = nfscl_request(nd, vp, p, cred, stuff);
4590         if (error)
4591                 return (error);
4592         if (!nd->nd_repstat)
4593                 error = nfsv4_loadattr(nd, vp, NULL, NULL, NULL, 0, NULL,
4594                     NULL, NULL, NULL, aclp, 0, NULL, NULL, NULL, p, cred);
4595         else
4596                 error = nd->nd_repstat;
4597         mbuf_freem(nd->nd_mrep);
4598         return (error);
4599 }
4600
4601 /*
4602  * nfs setacl call.
4603  */
4604 int
4605 nfsrpc_setacl(vnode_t vp, struct ucred *cred, NFSPROC_T *p,
4606     struct acl *aclp, void *stuff)
4607 {
4608         int error;
4609         struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
4610         
4611         if (nfsrv_useacl == 0 || !NFSHASNFSV4(nmp))
4612                 return (EOPNOTSUPP);
4613         error = nfsrpc_setattr(vp, NULL, aclp, cred, p, NULL, NULL, stuff);
4614         return (error);
4615 }
4616
4617 /*
4618  * nfs setacl call.
4619  */
4620 static int
4621 nfsrpc_setaclrpc(vnode_t vp, struct ucred *cred, NFSPROC_T *p,
4622     struct acl *aclp, nfsv4stateid_t *stateidp, void *stuff)
4623 {
4624         struct nfsrv_descript nfsd, *nd = &nfsd;
4625         int error;
4626         nfsattrbit_t attrbits;
4627         struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
4628         
4629         if (!NFSHASNFSV4(nmp))
4630                 return (EOPNOTSUPP);
4631         NFSCL_REQSTART(nd, NFSPROC_SETACL, vp);
4632         nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID);
4633         NFSZERO_ATTRBIT(&attrbits);
4634         NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_ACL);
4635         (void) nfsv4_fillattr(nd, vnode_mount(vp), vp, aclp, NULL, NULL, 0,
4636             &attrbits, NULL, NULL, 0, 0, 0, 0, (uint64_t)0, NULL);
4637         error = nfscl_request(nd, vp, p, cred, stuff);
4638         if (error)
4639                 return (error);
4640         /* Don't care about the pre/postop attributes */
4641         mbuf_freem(nd->nd_mrep);
4642         return (nd->nd_repstat);
4643 }
4644
4645 /*
4646  * Do the NFSv4.1 Exchange ID.
4647  */
4648 int
4649 nfsrpc_exchangeid(struct nfsmount *nmp, struct nfsclclient *clp,
4650     struct nfssockreq *nrp, uint32_t exchflags, struct nfsclds **dspp,
4651     struct ucred *cred, NFSPROC_T *p)
4652 {
4653         uint32_t *tl, v41flags;
4654         struct nfsrv_descript nfsd;
4655         struct nfsrv_descript *nd = &nfsd;
4656         struct nfsclds *dsp;
4657         struct timespec verstime;
4658         int error, len;
4659
4660         *dspp = NULL;
4661         nfscl_reqstart(nd, NFSPROC_EXCHANGEID, nmp, NULL, 0, NULL, NULL, 0, 0);
4662         NFSM_BUILD(tl, uint32_t *, 2 * NFSX_UNSIGNED);
4663         *tl++ = txdr_unsigned(nfsboottime.tv_sec);      /* Client owner */
4664         *tl = txdr_unsigned(clp->nfsc_rev);
4665         (void) nfsm_strtom(nd, clp->nfsc_id, clp->nfsc_idlen);
4666
4667         NFSM_BUILD(tl, uint32_t *, 3 * NFSX_UNSIGNED);
4668         *tl++ = txdr_unsigned(exchflags);
4669         *tl++ = txdr_unsigned(NFSV4EXCH_SP4NONE);
4670
4671         /* Set the implementation id4 */
4672         *tl = txdr_unsigned(1);
4673         (void) nfsm_strtom(nd, "freebsd.org", strlen("freebsd.org"));
4674         (void) nfsm_strtom(nd, version, strlen(version));
4675         NFSM_BUILD(tl, uint32_t *, NFSX_V4TIME);
4676         verstime.tv_sec = 1293840000;           /* Jan 1, 2011 */
4677         verstime.tv_nsec = 0;
4678         txdr_nfsv4time(&verstime, tl);
4679         nd->nd_flag |= ND_USEGSSNAME;
4680         error = newnfs_request(nd, nmp, NULL, nrp, NULL, p, cred,
4681             NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4682         NFSCL_DEBUG(1, "exchangeid err=%d reps=%d\n", error,
4683             (int)nd->nd_repstat);
4684         if (error != 0)
4685                 return (error);
4686         if (nd->nd_repstat == 0) {
4687                 NFSM_DISSECT(tl, uint32_t *, 6 * NFSX_UNSIGNED + NFSX_HYPER);
4688                 len = fxdr_unsigned(int, *(tl + 7));
4689                 if (len < 0 || len > NFSV4_OPAQUELIMIT) {
4690                         error = NFSERR_BADXDR;
4691                         goto nfsmout;
4692                 }
4693                 dsp = malloc(sizeof(struct nfsclds) + len + 1, M_NFSCLDS,
4694                     M_WAITOK | M_ZERO);
4695                 dsp->nfsclds_expire = NFSD_MONOSEC + clp->nfsc_renew;
4696                 dsp->nfsclds_servownlen = len;
4697                 dsp->nfsclds_sess.nfsess_clientid.lval[0] = *tl++;
4698                 dsp->nfsclds_sess.nfsess_clientid.lval[1] = *tl++;
4699                 dsp->nfsclds_sess.nfsess_sequenceid =
4700                     fxdr_unsigned(uint32_t, *tl++);
4701                 v41flags = fxdr_unsigned(uint32_t, *tl);
4702                 if ((v41flags & NFSV4EXCH_USEPNFSMDS) != 0 &&
4703                     NFSHASPNFSOPT(nmp)) {
4704                         NFSCL_DEBUG(1, "set PNFS\n");
4705                         NFSLOCKMNT(nmp);
4706                         nmp->nm_state |= NFSSTA_PNFS;
4707                         NFSUNLOCKMNT(nmp);
4708                         dsp->nfsclds_flags |= NFSCLDS_MDS;
4709                 }
4710                 if ((v41flags & NFSV4EXCH_USEPNFSDS) != 0)
4711                         dsp->nfsclds_flags |= NFSCLDS_DS;
4712                 if (len > 0)
4713                         nd->nd_repstat = nfsrv_mtostr(nd,
4714                             dsp->nfsclds_serverown, len);
4715                 if (nd->nd_repstat == 0) {
4716                         mtx_init(&dsp->nfsclds_mtx, "nfsds", NULL, MTX_DEF);
4717                         mtx_init(&dsp->nfsclds_sess.nfsess_mtx, "nfssession",
4718                             NULL, MTX_DEF);
4719                         nfscl_initsessionslots(&dsp->nfsclds_sess);
4720                         *dspp = dsp;
4721                 } else
4722                         free(dsp, M_NFSCLDS);
4723         }
4724         error = nd->nd_repstat;
4725 nfsmout:
4726         mbuf_freem(nd->nd_mrep);
4727         return (error);
4728 }
4729
4730 /*
4731  * Do the NFSv4.1 Create Session.
4732  */
4733 int
4734 nfsrpc_createsession(struct nfsmount *nmp, struct nfsclsession *sep,
4735     struct nfssockreq *nrp, uint32_t sequenceid, int mds, struct ucred *cred,
4736     NFSPROC_T *p)
4737 {
4738         uint32_t crflags, maxval, *tl;
4739         struct nfsrv_descript nfsd;
4740         struct nfsrv_descript *nd = &nfsd;
4741         int error, irdcnt;
4742
4743         /* Make sure nm_rsize, nm_wsize is set. */
4744         if (nmp->nm_rsize > NFS_MAXBSIZE || nmp->nm_rsize == 0)
4745                 nmp->nm_rsize = NFS_MAXBSIZE;
4746         if (nmp->nm_wsize > NFS_MAXBSIZE || nmp->nm_wsize == 0)
4747                 nmp->nm_wsize = NFS_MAXBSIZE;
4748         nfscl_reqstart(nd, NFSPROC_CREATESESSION, nmp, NULL, 0, NULL, NULL, 0,
4749             0);
4750         NFSM_BUILD(tl, uint32_t *, 4 * NFSX_UNSIGNED);
4751         *tl++ = sep->nfsess_clientid.lval[0];
4752         *tl++ = sep->nfsess_clientid.lval[1];
4753         *tl++ = txdr_unsigned(sequenceid);
4754         crflags = (NFSMNT_RDONLY(nmp->nm_mountp) ? 0 : NFSV4CRSESS_PERSIST);
4755         if (nfscl_enablecallb != 0 && nfs_numnfscbd > 0 && mds != 0)
4756                 crflags |= NFSV4CRSESS_CONNBACKCHAN;
4757         *tl = txdr_unsigned(crflags);
4758
4759         /* Fill in fore channel attributes. */
4760         NFSM_BUILD(tl, uint32_t *, 7 * NFSX_UNSIGNED);
4761         *tl++ = 0;                              /* Header pad size */
4762         *tl++ = txdr_unsigned(nmp->nm_wsize + NFS_MAXXDR);/* Max request size */
4763         *tl++ = txdr_unsigned(nmp->nm_rsize + NFS_MAXXDR);/* Max reply size */
4764         *tl++ = txdr_unsigned(4096);            /* Max response size cached */
4765         *tl++ = txdr_unsigned(20);              /* Max operations */
4766         *tl++ = txdr_unsigned(64);              /* Max slots */
4767         *tl = 0;                                /* No rdma ird */
4768
4769         /* Fill in back channel attributes. */
4770         NFSM_BUILD(tl, uint32_t *, 7 * NFSX_UNSIGNED);
4771         *tl++ = 0;                              /* Header pad size */
4772         *tl++ = txdr_unsigned(10000);           /* Max request size */
4773         *tl++ = txdr_unsigned(10000);           /* Max response size */
4774         *tl++ = txdr_unsigned(4096);            /* Max response size cached */
4775         *tl++ = txdr_unsigned(4);               /* Max operations */
4776         *tl++ = txdr_unsigned(NFSV4_CBSLOTS);   /* Max slots */
4777         *tl = 0;                                /* No rdma ird */
4778
4779         NFSM_BUILD(tl, uint32_t *, 8 * NFSX_UNSIGNED);
4780         *tl++ = txdr_unsigned(NFS_CALLBCKPROG); /* Call back prog # */
4781
4782         /* Allow AUTH_SYS callbacks as uid, gid == 0. */
4783         *tl++ = txdr_unsigned(1);               /* Auth_sys only */
4784         *tl++ = txdr_unsigned(AUTH_SYS);        /* AUTH_SYS type */
4785         *tl++ = txdr_unsigned(nfsboottime.tv_sec); /* time stamp */
4786         *tl++ = 0;                              /* Null machine name */
4787         *tl++ = 0;                              /* Uid == 0 */
4788         *tl++ = 0;                              /* Gid == 0 */
4789         *tl = 0;                                /* No additional gids */
4790         nd->nd_flag |= ND_USEGSSNAME;
4791         error = newnfs_request(nd, nmp, NULL, nrp, NULL, p, cred, NFS_PROG,
4792             NFS_VER4, NULL, 1, NULL, NULL);
4793         if (error != 0)
4794                 return (error);
4795         if (nd->nd_repstat == 0) {
4796                 NFSM_DISSECT(tl, uint32_t *, NFSX_V4SESSIONID +
4797                     2 * NFSX_UNSIGNED);
4798                 bcopy(tl, sep->nfsess_sessionid, NFSX_V4SESSIONID);
4799                 tl += NFSX_V4SESSIONID / NFSX_UNSIGNED;
4800                 sep->nfsess_sequenceid = fxdr_unsigned(uint32_t, *tl++);
4801                 crflags = fxdr_unsigned(uint32_t, *tl);
4802                 if ((crflags & NFSV4CRSESS_PERSIST) != 0 && mds != 0) {
4803                         NFSLOCKMNT(nmp);
4804                         nmp->nm_state |= NFSSTA_SESSPERSIST;
4805                         NFSUNLOCKMNT(nmp);
4806                 }
4807
4808                 /* Get the fore channel slot count. */
4809                 NFSM_DISSECT(tl, uint32_t *, 7 * NFSX_UNSIGNED);
4810                 tl++;                   /* Skip the header pad size. */
4811
4812                 /* Make sure nm_wsize is small enough. */
4813                 maxval = fxdr_unsigned(uint32_t, *tl++);
4814                 while (maxval < nmp->nm_wsize + NFS_MAXXDR) {
4815                         if (nmp->nm_wsize > 8096)
4816                                 nmp->nm_wsize /= 2;
4817                         else
4818                                 break;
4819                 }
4820
4821                 /* Make sure nm_rsize is small enough. */
4822                 maxval = fxdr_unsigned(uint32_t, *tl++);
4823                 while (maxval < nmp->nm_rsize + NFS_MAXXDR) {
4824                         if (nmp->nm_rsize > 8096)
4825                                 nmp->nm_rsize /= 2;
4826                         else
4827                                 break;
4828                 }
4829
4830                 sep->nfsess_maxcache = fxdr_unsigned(int, *tl++);
4831                 tl++;
4832                 sep->nfsess_foreslots = fxdr_unsigned(uint16_t, *tl++);
4833                 NFSCL_DEBUG(4, "fore slots=%d\n", (int)sep->nfsess_foreslots);
4834                 irdcnt = fxdr_unsigned(int, *tl);
4835                 if (irdcnt > 0)
4836                         NFSM_DISSECT(tl, uint32_t *, irdcnt * NFSX_UNSIGNED);
4837
4838                 /* and the back channel slot count. */
4839                 NFSM_DISSECT(tl, uint32_t *, 7 * NFSX_UNSIGNED);
4840                 tl += 5;
4841                 sep->nfsess_backslots = fxdr_unsigned(uint16_t, *tl);
4842                 NFSCL_DEBUG(4, "back slots=%d\n", (int)sep->nfsess_backslots);
4843         }
4844         error = nd->nd_repstat;
4845 nfsmout:
4846         mbuf_freem(nd->nd_mrep);
4847         return (error);
4848 }
4849
4850 /*
4851  * Do the NFSv4.1 Destroy Session.
4852  */
4853 int
4854 nfsrpc_destroysession(struct nfsmount *nmp, struct nfsclclient *clp,
4855     struct ucred *cred, NFSPROC_T *p)
4856 {
4857         uint32_t *tl;
4858         struct nfsrv_descript nfsd;
4859         struct nfsrv_descript *nd = &nfsd;
4860         int error;
4861         struct nfsclsession *tsep;
4862
4863         nfscl_reqstart(nd, NFSPROC_DESTROYSESSION, nmp, NULL, 0, NULL, NULL, 0,
4864             0);
4865         NFSM_BUILD(tl, uint32_t *, NFSX_V4SESSIONID);
4866         tsep = nfsmnt_mdssession(nmp);
4867         bcopy(tsep->nfsess_sessionid, tl, NFSX_V4SESSIONID);
4868         nd->nd_flag |= ND_USEGSSNAME;
4869         error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4870             NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4871         if (error != 0)
4872                 return (error);
4873         error = nd->nd_repstat;
4874         mbuf_freem(nd->nd_mrep);
4875         return (error);
4876 }
4877
4878 /*
4879  * Do the NFSv4.1 Destroy Client.
4880  */
4881 int
4882 nfsrpc_destroyclient(struct nfsmount *nmp, struct nfsclclient *clp,
4883     struct ucred *cred, NFSPROC_T *p)
4884 {
4885         uint32_t *tl;
4886         struct nfsrv_descript nfsd;
4887         struct nfsrv_descript *nd = &nfsd;
4888         int error;
4889         struct nfsclsession *tsep;
4890
4891         nfscl_reqstart(nd, NFSPROC_DESTROYCLIENT, nmp, NULL, 0, NULL, NULL, 0,
4892             0);
4893         NFSM_BUILD(tl, uint32_t *, 2 * NFSX_UNSIGNED);
4894         tsep = nfsmnt_mdssession(nmp);
4895         *tl++ = tsep->nfsess_clientid.lval[0];
4896         *tl = tsep->nfsess_clientid.lval[1];
4897         nd->nd_flag |= ND_USEGSSNAME;
4898         error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4899             NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4900         if (error != 0)
4901                 return (error);
4902         error = nd->nd_repstat;
4903         mbuf_freem(nd->nd_mrep);
4904         return (error);
4905 }
4906
4907 /*
4908  * Do the NFSv4.1 LayoutGet.
4909  */
4910 static int
4911 nfsrpc_layoutget(struct nfsmount *nmp, uint8_t *fhp, int fhlen, int iomode,
4912     uint64_t offset, uint64_t len, uint64_t minlen, int layouttype,
4913     int layoutlen, nfsv4stateid_t *stateidp, int *retonclosep,
4914     struct nfsclflayouthead *flhp, struct ucred *cred, NFSPROC_T *p,
4915     void *stuff)
4916 {
4917         struct nfsrv_descript nfsd, *nd = &nfsd;
4918         int error;
4919
4920         nfscl_reqstart(nd, NFSPROC_LAYOUTGET, nmp, fhp, fhlen, NULL, NULL, 0,
4921             0);
4922         nfsrv_setuplayoutget(nd, iomode, offset, len, minlen, stateidp,
4923             layouttype, layoutlen, 0);
4924         nd->nd_flag |= ND_USEGSSNAME;
4925         error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4926             NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4927         NFSCL_DEBUG(4, "layget err=%d st=%d\n", error, nd->nd_repstat);
4928         if (error != 0)
4929                 return (error);
4930         if (nd->nd_repstat == 0)
4931                 error = nfsrv_parselayoutget(nd, stateidp, retonclosep, flhp);
4932         if (error == 0 && nd->nd_repstat != 0)
4933                 error = nd->nd_repstat;
4934         mbuf_freem(nd->nd_mrep);
4935         return (error);
4936 }
4937
4938 /*
4939  * Do the NFSv4.1 Get Device Info.
4940  */
4941 int
4942 nfsrpc_getdeviceinfo(struct nfsmount *nmp, uint8_t *deviceid, int layouttype,
4943     uint32_t *notifybitsp, struct nfscldevinfo **ndip, struct ucred *cred,
4944     NFSPROC_T *p)
4945 {
4946         uint32_t cnt, *tl, vers, minorvers;
4947         struct nfsrv_descript nfsd;
4948         struct nfsrv_descript *nd = &nfsd;
4949         struct sockaddr_in sin, ssin;
4950         struct sockaddr_in6 sin6, ssin6;
4951         struct nfsclds *dsp = NULL, **dspp, **gotdspp;
4952         struct nfscldevinfo *ndi;
4953         int addrcnt = 0, bitcnt, error, gotvers, i, isudp, j, stripecnt;
4954         uint8_t stripeindex;
4955         sa_family_t af, safilled;
4956
4957         *ndip = NULL;
4958         ndi = NULL;
4959         gotdspp = NULL;
4960         nfscl_reqstart(nd, NFSPROC_GETDEVICEINFO, nmp, NULL, 0, NULL, NULL, 0,
4961             0);
4962         NFSM_BUILD(tl, uint32_t *, NFSX_V4DEVICEID + 3 * NFSX_UNSIGNED);
4963         NFSBCOPY(deviceid, tl, NFSX_V4DEVICEID);
4964         tl += (NFSX_V4DEVICEID / NFSX_UNSIGNED);
4965         *tl++ = txdr_unsigned(layouttype);
4966         *tl++ = txdr_unsigned(100000);
4967         if (notifybitsp != NULL && *notifybitsp != 0) {
4968                 *tl = txdr_unsigned(1);         /* One word of bits. */
4969                 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
4970                 *tl = txdr_unsigned(*notifybitsp);
4971         } else
4972                 *tl = txdr_unsigned(0);
4973         nd->nd_flag |= ND_USEGSSNAME;
4974         error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4975             NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4976         if (error != 0)
4977                 return (error);
4978         if (nd->nd_repstat == 0) {
4979                 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
4980                 if (layouttype != fxdr_unsigned(int, *tl))
4981                         printf("EEK! devinfo layout type not same!\n");
4982                 if (layouttype == NFSLAYOUT_NFSV4_1_FILES) {
4983                         NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
4984                         stripecnt = fxdr_unsigned(int, *tl);
4985                         NFSCL_DEBUG(4, "stripecnt=%d\n", stripecnt);
4986                         if (stripecnt < 1 || stripecnt > 4096) {
4987                                 printf("pNFS File layout devinfo stripecnt %d:"
4988                                     " out of range\n", stripecnt);
4989                                 error = NFSERR_BADXDR;
4990                                 goto nfsmout;
4991                         }
4992                         NFSM_DISSECT(tl, uint32_t *, (stripecnt + 1) *
4993                             NFSX_UNSIGNED);
4994                         addrcnt = fxdr_unsigned(int, *(tl + stripecnt));
4995                         NFSCL_DEBUG(4, "addrcnt=%d\n", addrcnt);
4996                         if (addrcnt < 1 || addrcnt > 128) {
4997                                 printf("NFS devinfo addrcnt %d: out of range\n",
4998                                     addrcnt);
4999                                 error = NFSERR_BADXDR;
5000                                 goto nfsmout;
5001                         }
5002         
5003                         /*
5004                          * Now we know how many stripe indices and addresses, so
5005                          * we can allocate the structure the correct size.
5006                          */
5007                         i = (stripecnt * sizeof(uint8_t)) /
5008                             sizeof(struct nfsclds *) + 1;
5009                         NFSCL_DEBUG(4, "stripeindices=%d\n", i);
5010                         ndi = malloc(sizeof(*ndi) + (addrcnt + i) *
5011                             sizeof(struct nfsclds *), M_NFSDEVINFO, M_WAITOK |
5012                             M_ZERO);
5013                         NFSBCOPY(deviceid, ndi->nfsdi_deviceid,
5014                             NFSX_V4DEVICEID);
5015                         ndi->nfsdi_refcnt = 0;
5016                         ndi->nfsdi_flags = NFSDI_FILELAYOUT;
5017                         ndi->nfsdi_stripecnt = stripecnt;
5018                         ndi->nfsdi_addrcnt = addrcnt;
5019                         /* Fill in the stripe indices. */
5020                         for (i = 0; i < stripecnt; i++) {
5021                                 stripeindex = fxdr_unsigned(uint8_t, *tl++);
5022                                 NFSCL_DEBUG(4, "stripeind=%d\n", stripeindex);
5023                                 if (stripeindex >= addrcnt) {
5024                                         printf("pNFS File Layout devinfo"
5025                                             " stripeindex %d: too big\n",
5026                                             (int)stripeindex);
5027                                         error = NFSERR_BADXDR;
5028                                         goto nfsmout;
5029                                 }
5030                                 nfsfldi_setstripeindex(ndi, i, stripeindex);
5031                         }
5032                 } else if (layouttype == NFSLAYOUT_FLEXFILE) {
5033                         /* For Flex File, we only get one address list. */
5034                         ndi = malloc(sizeof(*ndi) + sizeof(struct nfsclds *),
5035                             M_NFSDEVINFO, M_WAITOK | M_ZERO);
5036                         NFSBCOPY(deviceid, ndi->nfsdi_deviceid,
5037                             NFSX_V4DEVICEID);
5038                         ndi->nfsdi_refcnt = 0;
5039                         ndi->nfsdi_flags = NFSDI_FLEXFILE;
5040                         addrcnt = ndi->nfsdi_addrcnt = 1;
5041                 }
5042
5043                 /* Now, dissect the server address(es). */
5044                 safilled = AF_UNSPEC;
5045                 for (i = 0; i < addrcnt; i++) {
5046                         NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
5047                         cnt = fxdr_unsigned(uint32_t, *tl);
5048                         if (cnt == 0) {
5049                                 printf("NFS devinfo 0 len addrlist\n");
5050                                 error = NFSERR_BADXDR;
5051                                 goto nfsmout;
5052                         }
5053                         dspp = nfsfldi_addr(ndi, i);
5054                         safilled = AF_UNSPEC;
5055                         for (j = 0; j < cnt; j++) {
5056                                 error = nfsv4_getipaddr(nd, &sin, &sin6, &af,
5057                                     &isudp);
5058                                 if (error != 0 && error != EPERM) {
5059                                         error = NFSERR_BADXDR;
5060                                         goto nfsmout;
5061                                 }
5062                                 if (error == 0 && isudp == 0) {
5063                                         /*
5064                                          * The priority is:
5065                                          * - Same address family.
5066                                          * Save the address and dspp, so that
5067                                          * the connection can be done after
5068                                          * parsing is complete.
5069                                          */
5070                                         if (safilled == AF_UNSPEC ||
5071                                             (af == nmp->nm_nam->sa_family &&
5072                                              safilled != nmp->nm_nam->sa_family)
5073                                            ) {
5074                                                 if (af == AF_INET)
5075                                                         ssin = sin;
5076                                                 else
5077                                                         ssin6 = sin6;
5078                                                 safilled = af;
5079                                                 gotdspp = dspp;
5080                                         }
5081                                 }
5082                         }
5083                 }
5084
5085                 gotvers = NFS_VER4;     /* Always NFSv4 for File Layout. */
5086                 /* For Flex File, we will take one of the versions to use. */
5087                 if (layouttype == NFSLAYOUT_FLEXFILE) {
5088                         NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
5089                         j = fxdr_unsigned(int, *tl);
5090                         if (j < 1 || j > NFSDEV_MAXVERS) {
5091                                 printf("pNFS: too many versions\n");
5092                                 error = NFSERR_BADXDR;
5093                                 goto nfsmout;
5094                         }
5095                         gotvers = 0;
5096                         for (i = 0; i < j; i++) {
5097                                 NFSM_DISSECT(tl, uint32_t *, 5 * NFSX_UNSIGNED);
5098                                 vers = fxdr_unsigned(uint32_t, *tl++);
5099                                 minorvers = fxdr_unsigned(uint32_t, *tl++);
5100                                 if ((vers == NFS_VER4 && minorvers ==
5101                                     NFSV41_MINORVERSION) || (vers == NFS_VER3 &&
5102                                     gotvers == 0)) {
5103                                         gotvers = vers;
5104                                         /* We'll take this one. */
5105                                         ndi->nfsdi_versindex = i;
5106                                         ndi->nfsdi_vers = vers;
5107                                         ndi->nfsdi_minorvers = minorvers;
5108                                         ndi->nfsdi_rsize = fxdr_unsigned(
5109                                             uint32_t, *tl++);
5110                                         ndi->nfsdi_wsize = fxdr_unsigned(
5111                                             uint32_t, *tl++);
5112                                         if (*tl == newnfs_true)
5113                                                 ndi->nfsdi_flags |=
5114                                                     NFSDI_TIGHTCOUPLED;
5115                                         else
5116                                                 ndi->nfsdi_flags &=
5117                                                     ~NFSDI_TIGHTCOUPLED;
5118                                 }
5119                         }
5120                         if (gotvers == 0) {
5121                                 printf("pNFS: no NFSv3 or NFSv4.1\n");
5122                                 error = NFSERR_BADXDR;
5123                                 goto nfsmout;
5124                         }
5125                 }
5126
5127                 /* And the notify bits. */
5128                 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
5129                 bitcnt = fxdr_unsigned(int, *tl);
5130                 if (bitcnt > 0) {
5131                         NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
5132                         if (notifybitsp != NULL)
5133                                 *notifybitsp =
5134                                     fxdr_unsigned(uint32_t, *tl);
5135                 }
5136                 if (safilled != AF_UNSPEC) {
5137                         KASSERT(ndi != NULL, ("ndi is NULL"));
5138                         *ndip = ndi;
5139                 } else
5140                         error = EPERM;
5141                 if (error == 0) {
5142                         /*
5143                          * Now we can do a TCP connection for the correct
5144                          * NFS version and IP address.
5145                          */
5146                         error = nfsrpc_fillsa(nmp, &ssin, &ssin6, safilled,
5147                             gotvers, &dsp, p);
5148                 }
5149                 if (error == 0) {
5150                         KASSERT(gotdspp != NULL, ("gotdspp is NULL"));
5151                         *gotdspp = dsp;
5152                 }
5153         }
5154         if (nd->nd_repstat != 0 && error == 0)
5155                 error = nd->nd_repstat;
5156 nfsmout:
5157         if (error != 0 && ndi != NULL)
5158                 nfscl_freedevinfo(ndi);
5159         mbuf_freem(nd->nd_mrep);
5160         return (error);
5161 }
5162
5163 /*
5164  * Do the NFSv4.1 LayoutCommit.
5165  */
5166 int
5167 nfsrpc_layoutcommit(struct nfsmount *nmp, uint8_t *fh, int fhlen, int reclaim,
5168     uint64_t off, uint64_t len, uint64_t lastbyte, nfsv4stateid_t *stateidp,
5169     int layouttype, struct ucred *cred, NFSPROC_T *p, void *stuff)
5170 {
5171         uint32_t *tl;
5172         struct nfsrv_descript nfsd, *nd = &nfsd;
5173         int error;
5174
5175         nfscl_reqstart(nd, NFSPROC_LAYOUTCOMMIT, nmp, fh, fhlen, NULL, NULL,
5176             0, 0);
5177         NFSM_BUILD(tl, uint32_t *, 5 * NFSX_UNSIGNED + 3 * NFSX_HYPER +
5178             NFSX_STATEID);
5179         txdr_hyper(off, tl);
5180         tl += 2;
5181         txdr_hyper(len, tl);
5182         tl += 2;
5183         if (reclaim != 0)
5184                 *tl++ = newnfs_true;
5185         else
5186                 *tl++ = newnfs_false;
5187         *tl++ = txdr_unsigned(stateidp->seqid);
5188         *tl++ = stateidp->other[0];
5189         *tl++ = stateidp->other[1];
5190         *tl++ = stateidp->other[2];
5191         *tl++ = newnfs_true;
5192         if (lastbyte < off)
5193                 lastbyte = off;
5194         else if (lastbyte >= (off + len))
5195                 lastbyte = off + len - 1;
5196         txdr_hyper(lastbyte, tl);
5197         tl += 2;
5198         *tl++ = newnfs_false;
5199         *tl++ = txdr_unsigned(layouttype);
5200         /* All supported layouts are 0 length. */
5201         *tl = txdr_unsigned(0);
5202         nd->nd_flag |= ND_USEGSSNAME;
5203         error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
5204             NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
5205         if (error != 0)
5206                 return (error);
5207         error = nd->nd_repstat;
5208         mbuf_freem(nd->nd_mrep);
5209         return (error);
5210 }
5211
5212 /*
5213  * Do the NFSv4.1 LayoutReturn.
5214  */
5215 int
5216 nfsrpc_layoutreturn(struct nfsmount *nmp, uint8_t *fh, int fhlen, int reclaim,
5217     int layouttype, uint32_t iomode, int layoutreturn, uint64_t offset,
5218     uint64_t len, nfsv4stateid_t *stateidp, struct ucred *cred, NFSPROC_T *p,
5219     uint32_t stat, uint32_t op, char *devid)
5220 {
5221         uint32_t *tl;
5222         struct nfsrv_descript nfsd, *nd = &nfsd;
5223         uint64_t tu64;
5224         int error;
5225
5226         nfscl_reqstart(nd, NFSPROC_LAYOUTRETURN, nmp, fh, fhlen, NULL, NULL,
5227             0, 0);
5228         NFSM_BUILD(tl, uint32_t *, 4 * NFSX_UNSIGNED);
5229         if (reclaim != 0)
5230                 *tl++ = newnfs_true;
5231         else
5232                 *tl++ = newnfs_false;
5233         *tl++ = txdr_unsigned(layouttype);
5234         *tl++ = txdr_unsigned(iomode);
5235         *tl = txdr_unsigned(layoutreturn);
5236         if (layoutreturn == NFSLAYOUTRETURN_FILE) {
5237                 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_HYPER + NFSX_STATEID +
5238                     NFSX_UNSIGNED);
5239                 txdr_hyper(offset, tl);
5240                 tl += 2;
5241                 txdr_hyper(len, tl);
5242                 tl += 2;
5243                 NFSCL_DEBUG(4, "layoutret stseq=%d\n", (int)stateidp->seqid);
5244                 *tl++ = txdr_unsigned(stateidp->seqid);
5245                 *tl++ = stateidp->other[0];
5246                 *tl++ = stateidp->other[1];
5247                 *tl++ = stateidp->other[2];
5248                 if (layouttype == NFSLAYOUT_NFSV4_1_FILES)
5249                         *tl = txdr_unsigned(0);
5250                 else if (layouttype == NFSLAYOUT_FLEXFILE) {
5251                         if (stat != 0) {
5252                                 *tl = txdr_unsigned(2 * NFSX_HYPER +
5253                                     NFSX_STATEID + NFSX_V4DEVICEID + 5 *
5254                                     NFSX_UNSIGNED);
5255                                 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_HYPER +
5256                                     NFSX_STATEID + NFSX_V4DEVICEID + 5 *
5257                                     NFSX_UNSIGNED);
5258                                 *tl++ = txdr_unsigned(1);       /* One error. */
5259                                 tu64 = 0;                       /* Offset. */
5260                                 txdr_hyper(tu64, tl); tl += 2;
5261                                 tu64 = UINT64_MAX;              /* Length. */
5262                                 txdr_hyper(tu64, tl); tl += 2;
5263                                 NFSBCOPY(stateidp, tl, NFSX_STATEID);
5264                                 tl += (NFSX_STATEID / NFSX_UNSIGNED);
5265                                 *tl++ = txdr_unsigned(1);       /* One error. */
5266                                 NFSBCOPY(devid, tl, NFSX_V4DEVICEID);
5267                                 tl += (NFSX_V4DEVICEID / NFSX_UNSIGNED);
5268                                 *tl++ = txdr_unsigned(stat);
5269                                 *tl++ = txdr_unsigned(op);
5270                         } else {
5271                                 *tl = txdr_unsigned(2 * NFSX_UNSIGNED);
5272                                 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_UNSIGNED);
5273                                 /* No ioerrs. */
5274                                 *tl++ = 0;
5275                         }
5276                         *tl = 0;        /* No stats yet. */
5277                 }
5278         }
5279         nd->nd_flag |= ND_USEGSSNAME;
5280         error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
5281             NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
5282         if (error != 0)
5283                 return (error);
5284         if (nd->nd_repstat == 0) {
5285                 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
5286                 if (*tl != 0) {
5287                         NFSM_DISSECT(tl, uint32_t *, NFSX_STATEID);
5288                         stateidp->seqid = fxdr_unsigned(uint32_t, *tl++);
5289                         stateidp->other[0] = *tl++;
5290                         stateidp->other[1] = *tl++;
5291                         stateidp->other[2] = *tl;
5292                 }
5293         } else
5294                 error = nd->nd_repstat;
5295 nfsmout:
5296         mbuf_freem(nd->nd_mrep);
5297         return (error);
5298 }
5299
5300 /*
5301  * Acquire a layout and devinfo, if possible. The caller must have acquired
5302  * a reference count on the nfsclclient structure before calling this.
5303  * Return the layout in lypp with a reference count on it, if successful.
5304  */
5305 static int
5306 nfsrpc_getlayout(struct nfsmount *nmp, vnode_t vp, struct nfsfh *nfhp,
5307     int iomode, uint32_t *notifybitsp, nfsv4stateid_t *stateidp, uint64_t off,
5308     struct nfscllayout **lypp, struct ucred *cred, NFSPROC_T *p)
5309 {
5310         struct nfscllayout *lyp;
5311         struct nfsclflayout *flp;
5312         struct nfsclflayouthead flh;
5313         int error = 0, islocked, layoutlen, layouttype, recalled, retonclose;
5314         nfsv4stateid_t stateid;
5315         struct nfsclsession *tsep;
5316
5317         *lypp = NULL;
5318         if (NFSHASFLEXFILE(nmp))
5319                 layouttype = NFSLAYOUT_FLEXFILE;
5320         else
5321                 layouttype = NFSLAYOUT_NFSV4_1_FILES;
5322         /*
5323          * If lyp is returned non-NULL, there will be a refcnt (shared lock)
5324          * on it, iff flp != NULL or a lock (exclusive lock) on it iff
5325          * flp == NULL.
5326          */
5327         lyp = nfscl_getlayout(nmp->nm_clp, nfhp->nfh_fh, nfhp->nfh_len,
5328             off, &flp, &recalled);
5329         islocked = 0;
5330         if (lyp == NULL || flp == NULL) {
5331                 if (recalled != 0)
5332                         return (EIO);
5333                 LIST_INIT(&flh);
5334                 tsep = nfsmnt_mdssession(nmp);
5335                 layoutlen = tsep->nfsess_maxcache -
5336                     (NFSX_STATEID + 3 * NFSX_UNSIGNED);
5337                 if (lyp == NULL) {
5338                         stateid.seqid = 0;
5339                         stateid.other[0] = stateidp->other[0];
5340                         stateid.other[1] = stateidp->other[1];
5341                         stateid.other[2] = stateidp->other[2];
5342                         error = nfsrpc_layoutget(nmp, nfhp->nfh_fh,
5343                             nfhp->nfh_len, iomode, (uint64_t)0, UINT64_MAX,
5344                             (uint64_t)0, layouttype, layoutlen, &stateid,
5345                             &retonclose, &flh, cred, p, NULL);
5346                 } else {
5347                         islocked = 1;
5348                         stateid.seqid = lyp->nfsly_stateid.seqid;
5349                         stateid.other[0] = lyp->nfsly_stateid.other[0];
5350                         stateid.other[1] = lyp->nfsly_stateid.other[1];
5351                         stateid.other[2] = lyp->nfsly_stateid.other[2];
5352                         error = nfsrpc_layoutget(nmp, nfhp->nfh_fh,
5353                             nfhp->nfh_len, iomode, off, UINT64_MAX,
5354                             (uint64_t)0, layouttype, layoutlen, &stateid,
5355                             &retonclose, &flh, cred, p, NULL);
5356                 }
5357                 error = nfsrpc_layoutgetres(nmp, vp, nfhp->nfh_fh,
5358                     nfhp->nfh_len, &stateid, retonclose, notifybitsp, &lyp,
5359                     &flh, layouttype, error, NULL, cred, p);
5360                 if (error == 0)
5361                         *lypp = lyp;
5362                 else if (islocked != 0)
5363                         nfscl_rellayout(lyp, 1);
5364         } else
5365                 *lypp = lyp;
5366         return (error);
5367 }
5368
5369 /*
5370  * Do a TCP connection plus exchange id and create session.
5371  * If successful, a "struct nfsclds" is linked into the list for the
5372  * mount point and a pointer to it is returned.
5373  */
5374 static int
5375 nfsrpc_fillsa(struct nfsmount *nmp, struct sockaddr_in *sin,
5376     struct sockaddr_in6 *sin6, sa_family_t af, int vers, struct nfsclds **dspp,
5377     NFSPROC_T *p)
5378 {
5379         struct sockaddr_in *msad, *sad;
5380         struct sockaddr_in6 *msad6, *sad6;
5381         struct nfsclclient *clp;
5382         struct nfssockreq *nrp;
5383         struct nfsclds *dsp, *tdsp;
5384         int error;
5385         enum nfsclds_state retv;
5386         uint32_t sequenceid;
5387
5388         KASSERT(nmp->nm_sockreq.nr_cred != NULL,
5389             ("nfsrpc_fillsa: NULL nr_cred"));
5390         NFSLOCKCLSTATE();
5391         clp = nmp->nm_clp;
5392         NFSUNLOCKCLSTATE();
5393         if (clp == NULL)
5394                 return (EPERM);
5395         if (af == AF_INET) {
5396                 NFSLOCKMNT(nmp);
5397                 /*
5398                  * Check to see if we already have a session for this
5399                  * address that is usable for a DS.
5400                  * Note that the MDS's address is in a different place
5401                  * than the sessions already acquired for DS's.
5402                  */
5403                 msad = (struct sockaddr_in *)nmp->nm_sockreq.nr_nam;
5404                 tdsp = TAILQ_FIRST(&nmp->nm_sess);
5405                 while (tdsp != NULL) {
5406                         if (msad != NULL && msad->sin_family == AF_INET &&
5407                             sin->sin_addr.s_addr == msad->sin_addr.s_addr &&
5408                             sin->sin_port == msad->sin_port &&
5409                             (tdsp->nfsclds_flags & NFSCLDS_DS) != 0 &&
5410                             tdsp->nfsclds_sess.nfsess_defunct == 0) {
5411                                 *dspp = tdsp;
5412                                 NFSUNLOCKMNT(nmp);
5413                                 NFSCL_DEBUG(4, "fnd same addr\n");
5414                                 return (0);
5415                         }
5416                         tdsp = TAILQ_NEXT(tdsp, nfsclds_list);
5417                         if (tdsp != NULL && tdsp->nfsclds_sockp != NULL)
5418                                 msad = (struct sockaddr_in *)
5419                                     tdsp->nfsclds_sockp->nr_nam;
5420                         else
5421                                 msad = NULL;
5422                 }
5423                 NFSUNLOCKMNT(nmp);
5424
5425                 /* No IP address match, so look for new/trunked one. */
5426                 sad = malloc(sizeof(*sad), M_SONAME, M_WAITOK | M_ZERO);
5427                 sad->sin_len = sizeof(*sad);
5428                 sad->sin_family = AF_INET;
5429                 sad->sin_port = sin->sin_port;
5430                 sad->sin_addr.s_addr = sin->sin_addr.s_addr;
5431                 nrp = malloc(sizeof(*nrp), M_NFSSOCKREQ, M_WAITOK | M_ZERO);
5432                 nrp->nr_nam = (struct sockaddr *)sad;
5433         } else if (af == AF_INET6) {
5434                 NFSLOCKMNT(nmp);
5435                 /*
5436                  * Check to see if we already have a session for this
5437                  * address that is usable for a DS.
5438                  * Note that the MDS's address is in a different place
5439                  * than the sessions already acquired for DS's.
5440                  */
5441                 msad6 = (struct sockaddr_in6 *)nmp->nm_sockreq.nr_nam;
5442                 tdsp = TAILQ_FIRST(&nmp->nm_sess);
5443                 while (tdsp != NULL) {
5444                         if (msad6 != NULL && msad6->sin6_family == AF_INET6 &&
5445                             IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr,
5446                             &msad6->sin6_addr) &&
5447                             sin6->sin6_port == msad6->sin6_port &&
5448                             (tdsp->nfsclds_flags & NFSCLDS_DS) != 0 &&
5449                             tdsp->nfsclds_sess.nfsess_defunct == 0) {
5450                                 *dspp = tdsp;
5451                                 NFSUNLOCKMNT(nmp);
5452                                 return (0);
5453                         }
5454                         tdsp = TAILQ_NEXT(tdsp, nfsclds_list);
5455                         if (tdsp != NULL && tdsp->nfsclds_sockp != NULL)
5456                                 msad6 = (struct sockaddr_in6 *)
5457                                     tdsp->nfsclds_sockp->nr_nam;
5458                         else
5459                                 msad6 = NULL;
5460                 }
5461                 NFSUNLOCKMNT(nmp);
5462
5463                 /* No IP address match, so look for new/trunked one. */
5464                 sad6 = malloc(sizeof(*sad6), M_SONAME, M_WAITOK | M_ZERO);
5465                 sad6->sin6_len = sizeof(*sad6);
5466                 sad6->sin6_family = AF_INET6;
5467                 sad6->sin6_port = sin6->sin6_port;
5468                 NFSBCOPY(&sin6->sin6_addr, &sad6->sin6_addr,
5469                     sizeof(struct in6_addr));
5470                 nrp = malloc(sizeof(*nrp), M_NFSSOCKREQ, M_WAITOK | M_ZERO);
5471                 nrp->nr_nam = (struct sockaddr *)sad6;
5472         } else
5473                 return (EPERM);
5474
5475         nrp->nr_sotype = SOCK_STREAM;
5476         mtx_init(&nrp->nr_mtx, "nfssock", NULL, MTX_DEF);
5477         nrp->nr_prog = NFS_PROG;
5478         nrp->nr_vers = vers;
5479
5480         /*
5481          * Use the credentials that were used for the mount, which are
5482          * in nmp->nm_sockreq.nr_cred for newnfs_connect() etc.
5483          * Ref. counting the credentials with crhold() is probably not
5484          * necessary, since nm_sockreq.nr_cred won't be crfree()'d until
5485          * unmount, but I did it anyhow.
5486          */
5487         nrp->nr_cred = crhold(nmp->nm_sockreq.nr_cred);
5488         error = newnfs_connect(nmp, nrp, NULL, p, 0);
5489         NFSCL_DEBUG(3, "DS connect=%d\n", error);
5490
5491         dsp = NULL;
5492         /* Now, do the exchangeid and create session. */
5493         if (error == 0) {
5494                 if (vers == NFS_VER4) {
5495                         error = nfsrpc_exchangeid(nmp, clp, nrp,
5496                             NFSV4EXCH_USEPNFSDS, &dsp, nrp->nr_cred, p);
5497                         NFSCL_DEBUG(3, "DS exchangeid=%d\n", error);
5498                         if (error != 0)
5499                                 newnfs_disconnect(nrp);
5500                 } else {
5501                         dsp = malloc(sizeof(struct nfsclds), M_NFSCLDS,
5502                             M_WAITOK | M_ZERO);
5503                         dsp->nfsclds_flags |= NFSCLDS_DS;
5504                         dsp->nfsclds_expire = INT32_MAX; /* No renews needed. */
5505                         mtx_init(&dsp->nfsclds_mtx, "nfsds", NULL, MTX_DEF);
5506                         mtx_init(&dsp->nfsclds_sess.nfsess_mtx, "nfssession",
5507                             NULL, MTX_DEF);
5508                 }
5509         }
5510         if (error == 0) {
5511                 dsp->nfsclds_sockp = nrp;
5512                 if (vers == NFS_VER4) {
5513                         NFSLOCKMNT(nmp);
5514                         retv = nfscl_getsameserver(nmp, dsp, &tdsp,
5515                             &sequenceid);
5516                         NFSCL_DEBUG(3, "getsame ret=%d\n", retv);
5517                         if (retv == NFSDSP_USETHISSESSION &&
5518                             nfscl_dssameconn != 0) {
5519                                 NFSLOCKDS(tdsp);
5520                                 tdsp->nfsclds_flags |= NFSCLDS_SAMECONN;
5521                                 NFSUNLOCKDS(tdsp);
5522                                 NFSUNLOCKMNT(nmp);
5523                                 /*
5524                                  * If there is already a session for this
5525                                  * server, use it.
5526                                  */
5527                                 (void)newnfs_disconnect(nrp);
5528                                 nfscl_freenfsclds(dsp);
5529                                 *dspp = tdsp;
5530                                 return (0);
5531                         }
5532                         if (retv == NFSDSP_NOTFOUND)
5533                                 sequenceid =
5534                                     dsp->nfsclds_sess.nfsess_sequenceid;
5535                         NFSUNLOCKMNT(nmp);
5536                         error = nfsrpc_createsession(nmp, &dsp->nfsclds_sess,
5537                             nrp, sequenceid, 0, nrp->nr_cred, p);
5538                         NFSCL_DEBUG(3, "DS createsess=%d\n", error);
5539                 }
5540         } else {
5541                 NFSFREECRED(nrp->nr_cred);
5542                 NFSFREEMUTEX(&nrp->nr_mtx);
5543                 free(nrp->nr_nam, M_SONAME);
5544                 free(nrp, M_NFSSOCKREQ);
5545         }
5546         if (error == 0) {
5547                 NFSCL_DEBUG(3, "add DS session\n");
5548                 /*
5549                  * Put it at the end of the list. That way the list
5550                  * is ordered by when the entry was added. This matters
5551                  * since the one done first is the one that should be
5552                  * used for sequencid'ing any subsequent create sessions.
5553                  */
5554                 NFSLOCKMNT(nmp);
5555                 TAILQ_INSERT_TAIL(&nmp->nm_sess, dsp, nfsclds_list);
5556                 NFSUNLOCKMNT(nmp);
5557                 *dspp = dsp;
5558         } else if (dsp != NULL) {
5559                 newnfs_disconnect(nrp);
5560                 nfscl_freenfsclds(dsp);
5561         }
5562         return (error);
5563 }
5564
5565 /*
5566  * Do the NFSv4.1 Reclaim Complete.
5567  */
5568 int
5569 nfsrpc_reclaimcomplete(struct nfsmount *nmp, struct ucred *cred, NFSPROC_T *p)
5570 {
5571         uint32_t *tl;
5572         struct nfsrv_descript nfsd;
5573         struct nfsrv_descript *nd = &nfsd;
5574         int error;
5575
5576         nfscl_reqstart(nd, NFSPROC_RECLAIMCOMPL, nmp, NULL, 0, NULL, NULL, 0,
5577             0);
5578         NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
5579         *tl = newnfs_false;
5580         nd->nd_flag |= ND_USEGSSNAME;
5581         error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
5582             NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
5583         if (error != 0)
5584                 return (error);
5585         error = nd->nd_repstat;
5586         mbuf_freem(nd->nd_mrep);
5587         return (error);
5588 }
5589
5590 /*
5591  * Initialize the slot tables for a session.
5592  */
5593 static void
5594 nfscl_initsessionslots(struct nfsclsession *sep)
5595 {
5596         int i;
5597
5598         for (i = 0; i < NFSV4_CBSLOTS; i++) {
5599                 if (sep->nfsess_cbslots[i].nfssl_reply != NULL)
5600                         m_freem(sep->nfsess_cbslots[i].nfssl_reply);
5601                 NFSBZERO(&sep->nfsess_cbslots[i], sizeof(struct nfsslot));
5602         }
5603         for (i = 0; i < 64; i++)
5604                 sep->nfsess_slotseq[i] = 0;
5605         sep->nfsess_slots = 0;
5606 }
5607
5608 /*
5609  * Called to try and do an I/O operation via an NFSv4.1 Data Server (DS).
5610  */
5611 int
5612 nfscl_doiods(vnode_t vp, struct uio *uiop, int *iomode, int *must_commit,
5613     uint32_t rwaccess, int docommit, struct ucred *cred, NFSPROC_T *p)
5614 {
5615         struct nfsnode *np = VTONFS(vp);
5616         struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
5617         struct nfscllayout *layp;
5618         struct nfscldevinfo *dip;
5619         struct nfsclflayout *rflp;
5620         struct mbuf *m;
5621         struct nfsclwritedsdorpc *drpc, *tdrpc;
5622         nfsv4stateid_t stateid;
5623         struct ucred *newcred;
5624         uint64_t lastbyte, len, off, oresid, xfer;
5625         int eof, error, firstmirror, i, iolaymode, mirrorcnt, recalled, timo;
5626         void *lckp;
5627         uint8_t *dev;
5628         void *iovbase = NULL;
5629         size_t iovlen = 0;
5630         off_t offs = 0;
5631         ssize_t resid = 0;
5632
5633         if (!NFSHASPNFS(nmp) || nfscl_enablecallb == 0 || nfs_numnfscbd == 0 ||
5634             (np->n_flag & NNOLAYOUT) != 0)
5635                 return (EIO);
5636         /* Now, get a reference cnt on the clientid for this mount. */
5637         if (nfscl_getref(nmp) == 0)
5638                 return (EIO);
5639
5640         /* Find an appropriate stateid. */
5641         newcred = NFSNEWCRED(cred);
5642         error = nfscl_getstateid(vp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len,
5643             rwaccess, 1, newcred, p, &stateid, &lckp);
5644         if (error != 0) {
5645                 NFSFREECRED(newcred);
5646                 nfscl_relref(nmp);
5647                 return (error);
5648         }
5649         /* Search for a layout for this file. */
5650         off = uiop->uio_offset;
5651         layp = nfscl_getlayout(nmp->nm_clp, np->n_fhp->nfh_fh,
5652             np->n_fhp->nfh_len, off, &rflp, &recalled);
5653         if (layp == NULL || rflp == NULL) {
5654                 if (recalled != 0) {
5655                         NFSFREECRED(newcred);
5656                         nfscl_relref(nmp);
5657                         return (EIO);
5658                 }
5659                 if (layp != NULL) {
5660                         nfscl_rellayout(layp, (rflp == NULL) ? 1 : 0);
5661                         layp = NULL;
5662                 }
5663                 /* Try and get a Layout, if it is supported. */
5664                 if (rwaccess == NFSV4OPEN_ACCESSWRITE ||
5665                     (np->n_flag & NWRITEOPENED) != 0)
5666                         iolaymode = NFSLAYOUTIOMODE_RW;
5667                 else
5668                         iolaymode = NFSLAYOUTIOMODE_READ;
5669                 error = nfsrpc_getlayout(nmp, vp, np->n_fhp, iolaymode,
5670                     NULL, &stateid, off, &layp, newcred, p);
5671                 if (error != 0) {
5672                         NFSLOCKNODE(np);
5673                         np->n_flag |= NNOLAYOUT;
5674                         NFSUNLOCKNODE(np);
5675                         if (lckp != NULL)
5676                                 nfscl_lockderef(lckp);
5677                         NFSFREECRED(newcred);
5678                         if (layp != NULL)
5679                                 nfscl_rellayout(layp, 0);
5680                         nfscl_relref(nmp);
5681                         return (error);
5682                 }
5683         }
5684
5685         /*
5686          * Loop around finding a layout that works for the first part of
5687          * this I/O operation, and then call the function that actually
5688          * does the RPC.
5689          */
5690         eof = 0;
5691         len = (uint64_t)uiop->uio_resid;
5692         while (len > 0 && error == 0 && eof == 0) {
5693                 off = uiop->uio_offset;
5694                 error = nfscl_findlayoutforio(layp, off, rwaccess, &rflp);
5695                 if (error == 0) {
5696                         oresid = xfer = (uint64_t)uiop->uio_resid;
5697                         if (xfer > (rflp->nfsfl_end - rflp->nfsfl_off))
5698                                 xfer = rflp->nfsfl_end - rflp->nfsfl_off;
5699                         /*
5700                          * For Flex File layout with mirrored DSs, select one
5701                          * of them at random for reads. For writes and commits,
5702                          * do all mirrors.
5703                          */
5704                         m = NULL;
5705                         tdrpc = drpc = NULL;
5706                         firstmirror = 0;
5707                         mirrorcnt = 1;
5708                         if ((layp->nfsly_flags & NFSLY_FLEXFILE) != 0 &&
5709                             (mirrorcnt = rflp->nfsfl_mirrorcnt) > 1) {
5710                                 if (rwaccess == NFSV4OPEN_ACCESSREAD) {
5711                                         firstmirror = arc4random() % mirrorcnt;
5712                                         mirrorcnt = firstmirror + 1;
5713                                 } else {
5714                                         if (docommit == 0) {
5715                                                 /*
5716                                                  * Save values, so uiop can be
5717                                                  * rolled back upon a write
5718                                                  * error.
5719                                                  */
5720                                                 offs = uiop->uio_offset;
5721                                                 resid = uiop->uio_resid;
5722                                                 iovbase =
5723                                                     uiop->uio_iov->iov_base;
5724                                                 iovlen = uiop->uio_iov->iov_len;
5725                                                 m = nfsm_uiombuflist(uiop, len,
5726                                                     NULL, NULL);
5727                                         }
5728                                         tdrpc = drpc = malloc(sizeof(*drpc) *
5729                                             (mirrorcnt - 1), M_TEMP, M_WAITOK |
5730                                             M_ZERO);
5731                                 }
5732                         }
5733                         for (i = firstmirror; i < mirrorcnt && error == 0; i++){
5734                                 if ((layp->nfsly_flags & NFSLY_FLEXFILE) != 0) {
5735                                         dev = rflp->nfsfl_ffm[i].dev;
5736                                         dip = nfscl_getdevinfo(nmp->nm_clp, dev,
5737                                             rflp->nfsfl_ffm[i].devp);
5738                                 } else {
5739                                         dev = rflp->nfsfl_dev;
5740                                         dip = nfscl_getdevinfo(nmp->nm_clp, dev,
5741                                             rflp->nfsfl_devp);
5742                                 }
5743                                 if (dip != NULL) {
5744                                         if ((rflp->nfsfl_flags & NFSFL_FLEXFILE)
5745                                             != 0)
5746                                                 error = nfscl_dofflayoutio(vp,
5747                                                     uiop, iomode, must_commit,
5748                                                     &eof, &stateid, rwaccess,
5749                                                     dip, layp, rflp, off, xfer,
5750                                                     i, docommit, m, tdrpc,
5751                                                     newcred, p);
5752                                         else
5753                                                 error = nfscl_doflayoutio(vp,
5754                                                     uiop, iomode, must_commit,
5755                                                     &eof, &stateid, rwaccess,
5756                                                     dip, layp, rflp, off, xfer,
5757                                                     docommit, newcred, p);
5758                                         nfscl_reldevinfo(dip);
5759                                 } else
5760                                         error = EIO;
5761                                 tdrpc++;
5762                         }
5763                         if (m != NULL)
5764                                 m_freem(m);
5765                         tdrpc = drpc;
5766                         timo = hz / 50;         /* Wait for 20msec. */
5767                         if (timo < 1)
5768                                 timo = 1;
5769                         for (i = firstmirror; i < mirrorcnt - 1 &&
5770                             tdrpc != NULL; i++, tdrpc++) {
5771                                 /*
5772                                  * For the unused drpc entries, both inprog and
5773                                  * err == 0, so this loop won't break.
5774                                  */
5775                                 while (tdrpc->inprog != 0 && tdrpc->done == 0)
5776                                         tsleep(&tdrpc->tsk, PVFS, "clrpcio",
5777                                             timo);
5778                                 if (error == 0 && tdrpc->err != 0)
5779                                         error = tdrpc->err;
5780                         }
5781                         free(drpc, M_TEMP);
5782                         if (error == 0) {
5783                                 if (mirrorcnt > 1 && rwaccess ==
5784                                     NFSV4OPEN_ACCESSWRITE && docommit == 0) {
5785                                         NFSLOCKCLSTATE();
5786                                         layp->nfsly_flags |= NFSLY_WRITTEN;
5787                                         NFSUNLOCKCLSTATE();
5788                                 }
5789                                 lastbyte = off + xfer - 1;
5790                                 NFSLOCKCLSTATE();
5791                                 if (lastbyte > layp->nfsly_lastbyte)
5792                                         layp->nfsly_lastbyte = lastbyte;
5793                                 NFSUNLOCKCLSTATE();
5794                         } else if (error == NFSERR_OPENMODE &&
5795                             rwaccess == NFSV4OPEN_ACCESSREAD) {
5796                                 NFSLOCKMNT(nmp);
5797                                 nmp->nm_state |= NFSSTA_OPENMODE;
5798                                 NFSUNLOCKMNT(nmp);
5799                         } else
5800                                 error = EIO;
5801                         if (error == 0)
5802                                 len -= (oresid - (uint64_t)uiop->uio_resid);
5803                         else if (mirrorcnt > 1 && rwaccess ==
5804                             NFSV4OPEN_ACCESSWRITE && docommit == 0) {
5805                                 /*
5806                                  * In case the rpc gets retried, roll the
5807                                  * uio fields changed by nfsm_uiombuflist()
5808                                  * back.
5809                                  */
5810                                 uiop->uio_offset = offs;
5811                                 uiop->uio_resid = resid;
5812                                 uiop->uio_iov->iov_base = iovbase;
5813                                 uiop->uio_iov->iov_len = iovlen;
5814                         }
5815                 }
5816         }
5817         if (lckp != NULL)
5818                 nfscl_lockderef(lckp);
5819         NFSFREECRED(newcred);
5820         nfscl_rellayout(layp, 0);
5821         nfscl_relref(nmp);
5822         return (error);
5823 }
5824
5825 /*
5826  * Make a copy of the mbuf chain and add an mbuf for null padding, as required.
5827  */
5828 static struct mbuf *
5829 nfsm_copym(struct mbuf *m, int off, int xfer)
5830 {
5831         struct mbuf *m2, *m3, *m4;
5832         uint32_t *tl;
5833         int rem;
5834
5835         m2 = m_copym(m, off, xfer, M_WAITOK);
5836         rem = NFSM_RNDUP(xfer) - xfer;
5837         if (rem > 0) {
5838                 /*
5839                  * The zero padding to a multiple of 4 bytes is required by
5840                  * the XDR. So that the mbufs copied by reference aren't
5841                  * modified, add an mbuf with the zero'd bytes to the list.
5842                  * rem will be a maximum of 3, so one zero'd uint32_t is
5843                  * sufficient.
5844                  */
5845                 m3 = m2;
5846                 while (m3->m_next != NULL)
5847                         m3 = m3->m_next;
5848                 NFSMGET(m4);
5849                 tl = NFSMTOD(m4, uint32_t *);
5850                 *tl = 0;
5851                 mbuf_setlen(m4, rem);
5852                 mbuf_setnext(m3, m4);
5853         }
5854         return (m2);
5855 }
5856
5857 /*
5858  * Find a file layout that will handle the first bytes of the requested
5859  * range and return the information from it needed to the I/O operation.
5860  */
5861 int
5862 nfscl_findlayoutforio(struct nfscllayout *lyp, uint64_t off, uint32_t rwaccess,
5863     struct nfsclflayout **retflpp)
5864 {
5865         struct nfsclflayout *flp, *nflp, *rflp;
5866         uint32_t rw;
5867
5868         rflp = NULL;
5869         rw = rwaccess;
5870         /* For reading, do the Read list first and then the Write list. */
5871         do {
5872                 if (rw == NFSV4OPEN_ACCESSREAD)
5873                         flp = LIST_FIRST(&lyp->nfsly_flayread);
5874                 else
5875                         flp = LIST_FIRST(&lyp->nfsly_flayrw);
5876                 while (flp != NULL) {
5877                         nflp = LIST_NEXT(flp, nfsfl_list);
5878                         if (flp->nfsfl_off > off)
5879                                 break;
5880                         if (flp->nfsfl_end > off &&
5881                             (rflp == NULL || rflp->nfsfl_end < flp->nfsfl_end))
5882                                 rflp = flp;
5883                         flp = nflp;
5884                 }
5885                 if (rw == NFSV4OPEN_ACCESSREAD)
5886                         rw = NFSV4OPEN_ACCESSWRITE;
5887                 else
5888                         rw = 0;
5889         } while (rw != 0);
5890         if (rflp != NULL) {
5891                 /* This one covers the most bytes starting at off. */
5892                 *retflpp = rflp;
5893                 return (0);
5894         }
5895         return (EIO);
5896 }
5897
5898 /*
5899  * Do I/O using an NFSv4.1 file layout.
5900  */
5901 static int
5902 nfscl_doflayoutio(vnode_t vp, struct uio *uiop, int *iomode, int *must_commit,
5903     int *eofp, nfsv4stateid_t *stateidp, int rwflag, struct nfscldevinfo *dp,
5904     struct nfscllayout *lyp, struct nfsclflayout *flp, uint64_t off,
5905     uint64_t len, int docommit, struct ucred *cred, NFSPROC_T *p)
5906 {
5907         uint64_t io_off, rel_off, stripe_unit_size, transfer, xfer;
5908         int commit_thru_mds, error, stripe_index, stripe_pos;
5909         struct nfsnode *np;
5910         struct nfsfh *fhp;
5911         struct nfsclds **dspp;
5912
5913         np = VTONFS(vp);
5914         rel_off = off - flp->nfsfl_patoff;
5915         stripe_unit_size = flp->nfsfl_util & NFSFLAYUTIL_STRIPE_MASK;
5916         stripe_pos = (rel_off / stripe_unit_size + flp->nfsfl_stripe1) %
5917             dp->nfsdi_stripecnt;
5918         transfer = stripe_unit_size - (rel_off % stripe_unit_size);
5919         error = 0;
5920
5921         /* Loop around, doing I/O for each stripe unit. */
5922         while (len > 0 && error == 0) {
5923                 stripe_index = nfsfldi_stripeindex(dp, stripe_pos);
5924                 dspp = nfsfldi_addr(dp, stripe_index);
5925                 if (len > transfer && docommit == 0)
5926                         xfer = transfer;
5927                 else
5928                         xfer = len;
5929                 if ((flp->nfsfl_util & NFSFLAYUTIL_DENSE) != 0) {
5930                         /* Dense layout. */
5931                         if (stripe_pos >= flp->nfsfl_fhcnt)
5932                                 return (EIO);
5933                         fhp = flp->nfsfl_fh[stripe_pos];
5934                         io_off = (rel_off / (stripe_unit_size *
5935                             dp->nfsdi_stripecnt)) * stripe_unit_size +
5936                             rel_off % stripe_unit_size;
5937                 } else {
5938                         /* Sparse layout. */
5939                         if (flp->nfsfl_fhcnt > 1) {
5940                                 if (stripe_index >= flp->nfsfl_fhcnt)
5941                                         return (EIO);
5942                                 fhp = flp->nfsfl_fh[stripe_index];
5943                         } else if (flp->nfsfl_fhcnt == 1)
5944                                 fhp = flp->nfsfl_fh[0];
5945                         else
5946                                 fhp = np->n_fhp;
5947                         io_off = off;
5948                 }
5949                 if ((flp->nfsfl_util & NFSFLAYUTIL_COMMIT_THRU_MDS) != 0) {
5950                         commit_thru_mds = 1;
5951                         if (docommit != 0)
5952                                 error = EIO;
5953                 } else {
5954                         commit_thru_mds = 0;
5955                         NFSLOCKNODE(np);
5956                         np->n_flag |= NDSCOMMIT;
5957                         NFSUNLOCKNODE(np);
5958                 }
5959                 if (docommit != 0) {
5960                         if (error == 0)
5961                                 error = nfsrpc_commitds(vp, io_off, xfer,
5962                                     *dspp, fhp, 0, 0, cred, p);
5963                         if (error == 0) {
5964                                 /*
5965                                  * Set both eof and uio_resid = 0 to end any
5966                                  * loops.
5967                                  */
5968                                 *eofp = 1;
5969                                 uiop->uio_resid = 0;
5970                         } else {
5971                                 NFSLOCKNODE(np);
5972                                 np->n_flag &= ~NDSCOMMIT;
5973                                 NFSUNLOCKNODE(np);
5974                         }
5975                 } else if (rwflag == NFSV4OPEN_ACCESSREAD)
5976                         error = nfsrpc_readds(vp, uiop, stateidp, eofp, *dspp,
5977                             io_off, xfer, fhp, 0, 0, 0, cred, p);
5978                 else {
5979                         error = nfsrpc_writeds(vp, uiop, iomode, must_commit,
5980                             stateidp, *dspp, io_off, xfer, fhp, commit_thru_mds,
5981                             0, 0, 0, cred, p);
5982                         if (error == 0) {
5983                                 NFSLOCKCLSTATE();
5984                                 lyp->nfsly_flags |= NFSLY_WRITTEN;
5985                                 NFSUNLOCKCLSTATE();
5986                         }
5987                 }
5988                 if (error == 0) {
5989                         transfer = stripe_unit_size;
5990                         stripe_pos = (stripe_pos + 1) % dp->nfsdi_stripecnt;
5991                         len -= xfer;
5992                         off += xfer;
5993                 }
5994         }
5995         return (error);
5996 }
5997
5998 /*
5999  * Do I/O using an NFSv4.1 flex file layout.
6000  */
6001 static int
6002 nfscl_dofflayoutio(vnode_t vp, struct uio *uiop, int *iomode, int *must_commit,
6003     int *eofp, nfsv4stateid_t *stateidp, int rwflag, struct nfscldevinfo *dp,
6004     struct nfscllayout *lyp, struct nfsclflayout *flp, uint64_t off,
6005     uint64_t len, int mirror, int docommit, struct mbuf *mp,
6006     struct nfsclwritedsdorpc *drpc, struct ucred *cred, NFSPROC_T *p)
6007 {
6008         uint64_t transfer, xfer;
6009         int error, rel_off;
6010         struct nfsnode *np;
6011         struct nfsfh *fhp;
6012         struct nfsclds **dspp;
6013         struct ucred *tcred;
6014         struct mbuf *m;
6015
6016         np = VTONFS(vp);
6017         error = 0;
6018         rel_off = 0;
6019         NFSCL_DEBUG(4, "nfscl_dofflayoutio: off=%ju len=%ju\n", (uintmax_t)off,
6020             (uintmax_t)len);
6021         /* Loop around, doing I/O for each stripe unit. */
6022         while (len > 0 && error == 0) {
6023                 dspp = nfsfldi_addr(dp, 0);
6024                 fhp = flp->nfsfl_ffm[mirror].fh[dp->nfsdi_versindex];
6025                 stateidp = &flp->nfsfl_ffm[mirror].st;
6026                 NFSCL_DEBUG(4, "mirror=%d vind=%d fhlen=%d st.seqid=0x%x\n",
6027                     mirror, dp->nfsdi_versindex, fhp->nfh_len, stateidp->seqid);
6028                 if ((dp->nfsdi_flags & NFSDI_TIGHTCOUPLED) == 0) {
6029                         tcred = NFSNEWCRED(cred);
6030                         tcred->cr_uid = flp->nfsfl_ffm[mirror].user;
6031                         tcred->cr_groups[0] = flp->nfsfl_ffm[mirror].group;
6032                         tcred->cr_ngroups = 1;
6033                 } else
6034                         tcred = cred;
6035                 if (rwflag == NFSV4OPEN_ACCESSREAD)
6036                         transfer = dp->nfsdi_rsize;
6037                 else
6038                         transfer = dp->nfsdi_wsize;
6039                 NFSLOCKNODE(np);
6040                 np->n_flag |= NDSCOMMIT;
6041                 NFSUNLOCKNODE(np);
6042                 if (len > transfer && docommit == 0)
6043                         xfer = transfer;
6044                 else
6045                         xfer = len;
6046                 if (docommit != 0) {
6047                         if (error == 0) {
6048                                 /*
6049                                  * Do last mirrored DS commit with this thread.
6050                                  */
6051                                 if (mirror < flp->nfsfl_mirrorcnt - 1)
6052                                         error = nfsio_commitds(vp, off, xfer,
6053                                             *dspp, fhp, dp->nfsdi_vers,
6054                                             dp->nfsdi_minorvers, drpc, tcred,
6055                                             p);
6056                                 else
6057                                         error = nfsrpc_commitds(vp, off, xfer,
6058                                             *dspp, fhp, dp->nfsdi_vers,
6059                                             dp->nfsdi_minorvers, tcred, p);
6060                                 NFSCL_DEBUG(4, "commitds=%d\n", error);
6061                                 if (error != 0 && error != EACCES && error !=
6062                                     ESTALE) {
6063                                         NFSCL_DEBUG(4,
6064                                             "DS layreterr for commit\n");
6065                                         nfscl_dserr(NFSV4OP_COMMIT, error, dp,
6066                                             lyp, *dspp);
6067                                 }
6068                         }
6069                         NFSCL_DEBUG(4, "aft nfsio_commitds=%d\n", error);
6070                         if (error == 0) {
6071                                 /*
6072                                  * Set both eof and uio_resid = 0 to end any
6073                                  * loops.
6074                                  */
6075                                 *eofp = 1;
6076                                 uiop->uio_resid = 0;
6077                         } else {
6078                                 NFSLOCKNODE(np);
6079                                 np->n_flag &= ~NDSCOMMIT;
6080                                 NFSUNLOCKNODE(np);
6081                         }
6082                 } else if (rwflag == NFSV4OPEN_ACCESSREAD) {
6083                         error = nfsrpc_readds(vp, uiop, stateidp, eofp, *dspp,
6084                             off, xfer, fhp, 1, dp->nfsdi_vers,
6085                             dp->nfsdi_minorvers, tcred, p);
6086                         NFSCL_DEBUG(4, "readds=%d\n", error);
6087                         if (error != 0 && error != EACCES && error != ESTALE) {
6088                                 NFSCL_DEBUG(4, "DS layreterr for read\n");
6089                                 nfscl_dserr(NFSV4OP_READ, error, dp, lyp,
6090                                     *dspp);
6091                         }
6092                 } else {
6093                         if (flp->nfsfl_mirrorcnt == 1) {
6094                                 error = nfsrpc_writeds(vp, uiop, iomode,
6095                                     must_commit, stateidp, *dspp, off, xfer,
6096                                     fhp, 0, 1, dp->nfsdi_vers,
6097                                     dp->nfsdi_minorvers, tcred, p);
6098                                 if (error == 0) {
6099                                         NFSLOCKCLSTATE();
6100                                         lyp->nfsly_flags |= NFSLY_WRITTEN;
6101                                         NFSUNLOCKCLSTATE();
6102                                 }
6103                         } else {
6104                                 m = nfsm_copym(mp, rel_off, xfer);
6105                                 NFSCL_DEBUG(4, "mcopy reloff=%d xfer=%jd\n",
6106                                     rel_off, (uintmax_t)xfer);
6107                                 /*
6108                                  * Do the writes after the first loop iteration
6109                                  * and the write for the last mirror via this
6110                                  * thread.
6111                                  * This loop only iterates for small values
6112                                  * of nfsdi_wsize, which may never occur in
6113                                  * practice.  However, the drpc is completely
6114                                  * used by the first iteration and, as such,
6115                                  * cannot be used after that.
6116                                  */
6117                                 if (mirror < flp->nfsfl_mirrorcnt - 1 &&
6118                                     rel_off == 0)
6119                                         error = nfsio_writedsmir(vp, iomode,
6120                                             must_commit, stateidp, *dspp, off,
6121                                             xfer, fhp, m, dp->nfsdi_vers,
6122                                             dp->nfsdi_minorvers, drpc, tcred,
6123                                             p);
6124                                 else
6125                                         error = nfsrpc_writedsmir(vp, iomode,
6126                                             must_commit, stateidp, *dspp, off,
6127                                             xfer, fhp, m, dp->nfsdi_vers,
6128                                             dp->nfsdi_minorvers, tcred, p);
6129                                 NFSCL_DEBUG(4, "nfsio_writedsmir=%d\n", error);
6130                                 if (error != 0 && error != EACCES && error !=
6131                                     ESTALE) {
6132                                         NFSCL_DEBUG(4,
6133                                             "DS layreterr for write\n");
6134                                         nfscl_dserr(NFSV4OP_WRITE, error, dp,
6135                                             lyp, *dspp);
6136                                 }
6137                         }
6138                 }
6139                 NFSCL_DEBUG(4, "aft read/writeds=%d\n", error);
6140                 if (error == 0) {
6141                         len -= xfer;
6142                         off += xfer;
6143                         rel_off += xfer;
6144                 }
6145                 if ((dp->nfsdi_flags & NFSDI_TIGHTCOUPLED) == 0)
6146                         NFSFREECRED(tcred);
6147         }
6148         NFSCL_DEBUG(4, "eo nfscl_dofflayoutio=%d\n", error);
6149         return (error);
6150 }
6151
6152 /*
6153  * The actual read RPC done to a DS.
6154  */
6155 static int
6156 nfsrpc_readds(vnode_t vp, struct uio *uiop, nfsv4stateid_t *stateidp, int *eofp,
6157     struct nfsclds *dsp, uint64_t io_off, int len, struct nfsfh *fhp, int flex,
6158     int vers, int minorvers, struct ucred *cred, NFSPROC_T *p)
6159 {
6160         uint32_t *tl;
6161         int attrflag, error, retlen;
6162         struct nfsrv_descript nfsd;
6163         struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
6164         struct nfsrv_descript *nd = &nfsd;
6165         struct nfssockreq *nrp;
6166         struct nfsvattr na;
6167
6168         nd->nd_mrep = NULL;
6169         if (vers == 0 || vers == NFS_VER4) {
6170                 nfscl_reqstart(nd, NFSPROC_READDS, nmp, fhp->nfh_fh,
6171                     fhp->nfh_len, NULL, &dsp->nfsclds_sess, vers, minorvers);
6172                 vers = NFS_VER4;
6173                 NFSCL_DEBUG(4, "nfsrpc_readds: vers4 minvers=%d\n", minorvers);
6174                 if (flex != 0)
6175                         nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID);
6176                 else
6177                         nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSEQIDZERO);
6178         } else {
6179                 nfscl_reqstart(nd, NFSPROC_READ, nmp, fhp->nfh_fh,
6180                     fhp->nfh_len, NULL, &dsp->nfsclds_sess, vers, minorvers);
6181                 NFSDECRGLOBAL(nfsstatsv1.rpccnt[NFSPROC_READ]);
6182                 NFSINCRGLOBAL(nfsstatsv1.rpccnt[NFSPROC_READDS]);
6183                 NFSCL_DEBUG(4, "nfsrpc_readds: vers3\n");
6184         }
6185         NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED * 3);
6186         txdr_hyper(io_off, tl);
6187         *(tl + 2) = txdr_unsigned(len);
6188         nrp = dsp->nfsclds_sockp;
6189         NFSCL_DEBUG(4, "nfsrpc_readds: nrp=%p\n", nrp);
6190         if (nrp == NULL)
6191                 /* If NULL, use the MDS socket. */
6192                 nrp = &nmp->nm_sockreq;
6193         error = newnfs_request(nd, nmp, NULL, nrp, vp, p, cred,
6194             NFS_PROG, vers, NULL, 1, NULL, &dsp->nfsclds_sess);
6195         NFSCL_DEBUG(4, "nfsrpc_readds: stat=%d err=%d\n", nd->nd_repstat,
6196             error);
6197         if (error != 0)
6198                 return (error);
6199         if (vers == NFS_VER3) {
6200                 error = nfscl_postop_attr(nd, &na, &attrflag, NULL);
6201                 NFSCL_DEBUG(4, "nfsrpc_readds: postop=%d\n", error);
6202                 if (error != 0)
6203                         goto nfsmout;
6204         }
6205         if (nd->nd_repstat != 0) {
6206                 error = nd->nd_repstat;
6207                 goto nfsmout;
6208         }
6209         if (vers == NFS_VER3) {
6210                 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
6211                 *eofp = fxdr_unsigned(int, *(tl + 1));
6212         } else {
6213                 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
6214                 *eofp = fxdr_unsigned(int, *tl);
6215         }
6216         NFSM_STRSIZ(retlen, len);
6217         NFSCL_DEBUG(4, "nfsrpc_readds: retlen=%d eof=%d\n", retlen, *eofp);
6218         error = nfsm_mbufuio(nd, uiop, retlen);
6219 nfsmout:
6220         if (nd->nd_mrep != NULL)
6221                 mbuf_freem(nd->nd_mrep);
6222         return (error);
6223 }
6224
6225 /*
6226  * The actual write RPC done to a DS.
6227  */
6228 static int
6229 nfsrpc_writeds(vnode_t vp, struct uio *uiop, int *iomode, int *must_commit,
6230     nfsv4stateid_t *stateidp, struct nfsclds *dsp, uint64_t io_off, int len,
6231     struct nfsfh *fhp, int commit_thru_mds, int flex, int vers, int minorvers,
6232     struct ucred *cred, NFSPROC_T *p)
6233 {
6234         uint32_t *tl;
6235         struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
6236         int attrflag, error, rlen, commit, committed = NFSWRITE_FILESYNC;
6237         int32_t backup;
6238         struct nfsrv_descript nfsd;
6239         struct nfsrv_descript *nd = &nfsd;
6240         struct nfssockreq *nrp;
6241         struct nfsvattr na;
6242
6243         KASSERT(uiop->uio_iovcnt == 1, ("nfs: writerpc iovcnt > 1"));
6244         nd->nd_mrep = NULL;
6245         if (vers == 0 || vers == NFS_VER4) {
6246                 nfscl_reqstart(nd, NFSPROC_WRITEDS, nmp, fhp->nfh_fh,
6247                     fhp->nfh_len, NULL, &dsp->nfsclds_sess, vers, minorvers);
6248                 NFSCL_DEBUG(4, "nfsrpc_writeds: vers4 minvers=%d\n", minorvers);
6249                 vers = NFS_VER4;
6250                 if (flex != 0)
6251                         nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID);
6252                 else
6253                         nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSEQIDZERO);
6254                 NFSM_BUILD(tl, uint32_t *, NFSX_HYPER + 2 * NFSX_UNSIGNED);
6255         } else {
6256                 nfscl_reqstart(nd, NFSPROC_WRITE, nmp, fhp->nfh_fh,
6257                     fhp->nfh_len, NULL, &dsp->nfsclds_sess, vers, minorvers);
6258                 NFSDECRGLOBAL(nfsstatsv1.rpccnt[NFSPROC_WRITE]);
6259                 NFSINCRGLOBAL(nfsstatsv1.rpccnt[NFSPROC_WRITEDS]);
6260                 NFSCL_DEBUG(4, "nfsrpc_writeds: vers3\n");
6261                 NFSM_BUILD(tl, uint32_t *, NFSX_HYPER + 3 * NFSX_UNSIGNED);
6262         }
6263         txdr_hyper(io_off, tl);
6264         tl += 2;
6265         if (vers == NFS_VER3)
6266                 *tl++ = txdr_unsigned(len);
6267         *tl++ = txdr_unsigned(*iomode);
6268         *tl = txdr_unsigned(len);
6269         nfsm_uiombuf(nd, uiop, len);
6270         nrp = dsp->nfsclds_sockp;
6271         if (nrp == NULL)
6272                 /* If NULL, use the MDS socket. */
6273                 nrp = &nmp->nm_sockreq;
6274         error = newnfs_request(nd, nmp, NULL, nrp, vp, p, cred,
6275             NFS_PROG, vers, NULL, 1, NULL, &dsp->nfsclds_sess);
6276         NFSCL_DEBUG(4, "nfsrpc_writeds: err=%d stat=%d\n", error,
6277             nd->nd_repstat);
6278         if (error != 0)
6279                 return (error);
6280         if (nd->nd_repstat != 0) {
6281                 /*
6282                  * In case the rpc gets retried, roll
6283                  * the uio fileds changed by nfsm_uiombuf()
6284                  * back.
6285                  */
6286                 uiop->uio_offset -= len;
6287                 uio_uio_resid_add(uiop, len);
6288                 uio_iov_base_add(uiop, -len);
6289                 uio_iov_len_add(uiop, len);
6290                 error = nd->nd_repstat;
6291         } else {
6292                 if (vers == NFS_VER3) {
6293                         error = nfscl_wcc_data(nd, vp, &na, &attrflag, NULL,
6294                             NULL);
6295                         NFSCL_DEBUG(4, "nfsrpc_writeds: wcc_data=%d\n", error);
6296                         if (error != 0)
6297                                 goto nfsmout;
6298                 }
6299                 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED + NFSX_VERF);
6300                 rlen = fxdr_unsigned(int, *tl++);
6301                 NFSCL_DEBUG(4, "nfsrpc_writeds: len=%d rlen=%d\n", len, rlen);
6302                 if (rlen == 0) {
6303                         error = NFSERR_IO;
6304                         goto nfsmout;
6305                 } else if (rlen < len) {
6306                         backup = len - rlen;
6307                         uio_iov_base_add(uiop, -(backup));
6308                         uio_iov_len_add(uiop, backup);
6309                         uiop->uio_offset -= backup;
6310                         uio_uio_resid_add(uiop, backup);
6311                         len = rlen;
6312                 }
6313                 commit = fxdr_unsigned(int, *tl++);
6314
6315                 /*
6316                  * Return the lowest commitment level
6317                  * obtained by any of the RPCs.
6318                  */
6319                 if (committed == NFSWRITE_FILESYNC)
6320                         committed = commit;
6321                 else if (committed == NFSWRITE_DATASYNC &&
6322                     commit == NFSWRITE_UNSTABLE)
6323                         committed = commit;
6324                 if (commit_thru_mds != 0) {
6325                         NFSLOCKMNT(nmp);
6326                         if (!NFSHASWRITEVERF(nmp)) {
6327                                 NFSBCOPY(tl, nmp->nm_verf, NFSX_VERF);
6328                                 NFSSETWRITEVERF(nmp);
6329                         } else if (NFSBCMP(tl, nmp->nm_verf, NFSX_VERF)) {
6330                                 *must_commit = 1;
6331                                 NFSBCOPY(tl, nmp->nm_verf, NFSX_VERF);
6332                         }
6333                         NFSUNLOCKMNT(nmp);
6334                 } else {
6335                         NFSLOCKDS(dsp);
6336                         if ((dsp->nfsclds_flags & NFSCLDS_HASWRITEVERF) == 0) {
6337                                 NFSBCOPY(tl, dsp->nfsclds_verf, NFSX_VERF);
6338                                 dsp->nfsclds_flags |= NFSCLDS_HASWRITEVERF;
6339                         } else if (NFSBCMP(tl, dsp->nfsclds_verf, NFSX_VERF)) {
6340                                 *must_commit = 1;
6341                                 NFSBCOPY(tl, dsp->nfsclds_verf, NFSX_VERF);
6342                         }
6343                         NFSUNLOCKDS(dsp);
6344                 }
6345         }
6346 nfsmout:
6347         if (nd->nd_mrep != NULL)
6348                 mbuf_freem(nd->nd_mrep);
6349         *iomode = committed;
6350         if (nd->nd_repstat != 0 && error == 0)
6351                 error = nd->nd_repstat;
6352         return (error);
6353 }
6354
6355 /*
6356  * The actual write RPC done to a DS.
6357  * This variant is called from a separate kernel process for mirrors.
6358  * Any short write is considered an IO error.
6359  */
6360 static int
6361 nfsrpc_writedsmir(vnode_t vp, int *iomode, int *must_commit,
6362     nfsv4stateid_t *stateidp, struct nfsclds *dsp, uint64_t io_off, int len,
6363     struct nfsfh *fhp, struct mbuf *m, int vers, int minorvers,
6364     struct ucred *cred, NFSPROC_T *p)
6365 {
6366         uint32_t *tl;
6367         struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
6368         int attrflag, error, commit, committed = NFSWRITE_FILESYNC, rlen;
6369         struct nfsrv_descript nfsd;
6370         struct nfsrv_descript *nd = &nfsd;
6371         struct nfssockreq *nrp;
6372         struct nfsvattr na;
6373
6374         nd->nd_mrep = NULL;
6375         if (vers == 0 || vers == NFS_VER4) {
6376                 nfscl_reqstart(nd, NFSPROC_WRITEDS, nmp, fhp->nfh_fh,
6377                     fhp->nfh_len, NULL, &dsp->nfsclds_sess, vers, minorvers);
6378                 vers = NFS_VER4;
6379                 NFSCL_DEBUG(4, "nfsrpc_writedsmir: vers4 minvers=%d\n",
6380                     minorvers);
6381                 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID);
6382                 NFSM_BUILD(tl, uint32_t *, NFSX_HYPER + 2 * NFSX_UNSIGNED);
6383         } else {
6384                 nfscl_reqstart(nd, NFSPROC_WRITE, nmp, fhp->nfh_fh,
6385                     fhp->nfh_len, NULL, &dsp->nfsclds_sess, vers, minorvers);
6386                 NFSDECRGLOBAL(nfsstatsv1.rpccnt[NFSPROC_WRITE]);
6387                 NFSINCRGLOBAL(nfsstatsv1.rpccnt[NFSPROC_WRITEDS]);
6388                 NFSCL_DEBUG(4, "nfsrpc_writedsmir: vers3\n");
6389                 NFSM_BUILD(tl, uint32_t *, NFSX_HYPER + 3 * NFSX_UNSIGNED);
6390         }
6391         txdr_hyper(io_off, tl);
6392         tl += 2;
6393         if (vers == NFS_VER3)
6394                 *tl++ = txdr_unsigned(len);
6395         *tl++ = txdr_unsigned(*iomode);
6396         *tl = txdr_unsigned(len);
6397         if (len > 0) {
6398                 /* Put data in mbuf chain. */
6399                 nd->nd_mb->m_next = m;
6400                 /* Set nd_mb and nd_bpos to end of data. */
6401                 while (m->m_next != NULL)
6402                         m = m->m_next;
6403                 nd->nd_mb = m;
6404                 nd->nd_bpos = mtod(m, char *) + m->m_len;
6405                 NFSCL_DEBUG(4, "nfsrpc_writedsmir: lastmb len=%d\n", m->m_len);
6406         }
6407         nrp = dsp->nfsclds_sockp;
6408         if (nrp == NULL)
6409                 /* If NULL, use the MDS socket. */
6410                 nrp = &nmp->nm_sockreq;
6411         error = newnfs_request(nd, nmp, NULL, nrp, vp, p, cred,
6412             NFS_PROG, vers, NULL, 1, NULL, &dsp->nfsclds_sess);
6413         NFSCL_DEBUG(4, "nfsrpc_writedsmir: err=%d stat=%d\n", error,
6414             nd->nd_repstat);
6415         if (error != 0)
6416                 return (error);
6417         if (nd->nd_repstat != 0)
6418                 error = nd->nd_repstat;
6419         else {
6420                 if (vers == NFS_VER3) {
6421                         error = nfscl_wcc_data(nd, vp, &na, &attrflag, NULL,
6422                             NULL);
6423                         NFSCL_DEBUG(4, "nfsrpc_writedsmir: wcc_data=%d\n",
6424                             error);
6425                         if (error != 0)
6426                                 goto nfsmout;
6427                 }
6428                 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED + NFSX_VERF);
6429                 rlen = fxdr_unsigned(int, *tl++);
6430                 NFSCL_DEBUG(4, "nfsrpc_writedsmir: len=%d rlen=%d\n", len,
6431                     rlen);
6432                 if (rlen != len) {
6433                         error = NFSERR_IO;
6434                         NFSCL_DEBUG(4, "nfsrpc_writedsmir: len=%d rlen=%d\n",
6435                             len, rlen);
6436                         goto nfsmout;
6437                 }
6438                 commit = fxdr_unsigned(int, *tl++);
6439
6440                 /*
6441                  * Return the lowest commitment level
6442                  * obtained by any of the RPCs.
6443                  */
6444                 if (committed == NFSWRITE_FILESYNC)
6445                         committed = commit;
6446                 else if (committed == NFSWRITE_DATASYNC &&
6447                     commit == NFSWRITE_UNSTABLE)
6448                         committed = commit;
6449                 NFSLOCKDS(dsp);
6450                 if ((dsp->nfsclds_flags & NFSCLDS_HASWRITEVERF) == 0) {
6451                         NFSBCOPY(tl, dsp->nfsclds_verf, NFSX_VERF);
6452                         dsp->nfsclds_flags |= NFSCLDS_HASWRITEVERF;
6453                 } else if (NFSBCMP(tl, dsp->nfsclds_verf, NFSX_VERF)) {
6454                         *must_commit = 1;
6455                         NFSBCOPY(tl, dsp->nfsclds_verf, NFSX_VERF);
6456                 }
6457                 NFSUNLOCKDS(dsp);
6458         }
6459 nfsmout:
6460         if (nd->nd_mrep != NULL)
6461                 mbuf_freem(nd->nd_mrep);
6462         *iomode = committed;
6463         if (nd->nd_repstat != 0 && error == 0)
6464                 error = nd->nd_repstat;
6465         return (error);
6466 }
6467
6468 /*
6469  * Start up the thread that will execute nfsrpc_writedsmir().
6470  */
6471 static void
6472 start_writedsmir(void *arg, int pending)
6473 {
6474         struct nfsclwritedsdorpc *drpc;
6475
6476         drpc = (struct nfsclwritedsdorpc *)arg;
6477         drpc->err = nfsrpc_writedsmir(drpc->vp, &drpc->iomode,
6478             &drpc->must_commit, drpc->stateidp, drpc->dsp, drpc->off, drpc->len,
6479             drpc->fhp, drpc->m, drpc->vers, drpc->minorvers, drpc->cred,
6480             drpc->p);
6481         drpc->done = 1;
6482         NFSCL_DEBUG(4, "start_writedsmir: err=%d\n", drpc->err);
6483 }
6484
6485 /*
6486  * Set up the write DS mirror call for the pNFS I/O thread.
6487  */
6488 static int
6489 nfsio_writedsmir(vnode_t vp, int *iomode, int *must_commit,
6490     nfsv4stateid_t *stateidp, struct nfsclds *dsp, uint64_t off, int len,
6491     struct nfsfh *fhp, struct mbuf *m, int vers, int minorvers,
6492     struct nfsclwritedsdorpc *drpc, struct ucred *cred, NFSPROC_T *p)
6493 {
6494         int error, ret;
6495
6496         error = 0;
6497         drpc->done = 0;
6498         drpc->vp = vp;
6499         drpc->iomode = *iomode;
6500         drpc->must_commit = *must_commit;
6501         drpc->stateidp = stateidp;
6502         drpc->dsp = dsp;
6503         drpc->off = off;
6504         drpc->len = len;
6505         drpc->fhp = fhp;
6506         drpc->m = m;
6507         drpc->vers = vers;
6508         drpc->minorvers = minorvers;
6509         drpc->cred = cred;
6510         drpc->p = p;
6511         drpc->inprog = 0;
6512         ret = EIO;
6513         if (nfs_pnfsiothreads != 0) {
6514                 ret = nfs_pnfsio(start_writedsmir, drpc);
6515                 NFSCL_DEBUG(4, "nfsio_writedsmir: nfs_pnfsio=%d\n", ret);
6516         }
6517         if (ret != 0)
6518                 error = nfsrpc_writedsmir(vp, iomode, must_commit, stateidp,
6519                     dsp, off, len, fhp, m, vers, minorvers, cred, p);
6520         NFSCL_DEBUG(4, "nfsio_writedsmir: error=%d\n", error);
6521         return (error);
6522 }
6523
6524 /*
6525  * Free up the nfsclds structure.
6526  */
6527 void
6528 nfscl_freenfsclds(struct nfsclds *dsp)
6529 {
6530         int i;
6531
6532         if (dsp == NULL)
6533                 return;
6534         if (dsp->nfsclds_sockp != NULL) {
6535                 NFSFREECRED(dsp->nfsclds_sockp->nr_cred);
6536                 NFSFREEMUTEX(&dsp->nfsclds_sockp->nr_mtx);
6537                 free(dsp->nfsclds_sockp->nr_nam, M_SONAME);
6538                 free(dsp->nfsclds_sockp, M_NFSSOCKREQ);
6539         }
6540         NFSFREEMUTEX(&dsp->nfsclds_mtx);
6541         NFSFREEMUTEX(&dsp->nfsclds_sess.nfsess_mtx);
6542         for (i = 0; i < NFSV4_CBSLOTS; i++) {
6543                 if (dsp->nfsclds_sess.nfsess_cbslots[i].nfssl_reply != NULL)
6544                         m_freem(
6545                             dsp->nfsclds_sess.nfsess_cbslots[i].nfssl_reply);
6546         }
6547         free(dsp, M_NFSCLDS);
6548 }
6549
6550 static enum nfsclds_state
6551 nfscl_getsameserver(struct nfsmount *nmp, struct nfsclds *newdsp,
6552     struct nfsclds **retdspp, uint32_t *sequencep)
6553 {
6554         struct nfsclds *dsp;
6555         int fndseq;
6556
6557         /*
6558          * Search the list of nfsclds structures for one with the same
6559          * server.
6560          */
6561         fndseq = 0;
6562         TAILQ_FOREACH(dsp, &nmp->nm_sess, nfsclds_list) {
6563                 if (dsp->nfsclds_servownlen == newdsp->nfsclds_servownlen &&
6564                     dsp->nfsclds_servownlen != 0 &&
6565                     !NFSBCMP(dsp->nfsclds_serverown, newdsp->nfsclds_serverown,
6566                     dsp->nfsclds_servownlen) &&
6567                     dsp->nfsclds_sess.nfsess_defunct == 0) {
6568                         NFSCL_DEBUG(4, "fnd same fdsp=%p dsp=%p flg=0x%x\n",
6569                             TAILQ_FIRST(&nmp->nm_sess), dsp,
6570                             dsp->nfsclds_flags);
6571                         if (fndseq == 0) {
6572                                 /* Get sequenceid# from first entry. */
6573                                 *sequencep =
6574                                     dsp->nfsclds_sess.nfsess_sequenceid;
6575                                 fndseq = 1;
6576                         }
6577                         /* Server major id matches. */
6578                         if ((dsp->nfsclds_flags & NFSCLDS_DS) != 0) {
6579                                 *retdspp = dsp;
6580                                 return (NFSDSP_USETHISSESSION);
6581                         }
6582
6583                 }
6584         }
6585         if (fndseq != 0)
6586                 return (NFSDSP_SEQTHISSESSION);
6587         return (NFSDSP_NOTFOUND);
6588 }
6589
6590 /*
6591  * NFS commit rpc to a NFSv4.1 DS.
6592  */
6593 static int
6594 nfsrpc_commitds(vnode_t vp, uint64_t offset, int cnt, struct nfsclds *dsp,
6595     struct nfsfh *fhp, int vers, int minorvers, struct ucred *cred,
6596     NFSPROC_T *p)
6597 {
6598         uint32_t *tl;
6599         struct nfsrv_descript nfsd, *nd = &nfsd;
6600         struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
6601         struct nfssockreq *nrp;
6602         struct nfsvattr na;
6603         int attrflag, error;
6604         
6605         nd->nd_mrep = NULL;
6606         if (vers == 0 || vers == NFS_VER4) {
6607                 nfscl_reqstart(nd, NFSPROC_COMMITDS, nmp, fhp->nfh_fh,
6608                     fhp->nfh_len, NULL, &dsp->nfsclds_sess, vers, minorvers);
6609                 vers = NFS_VER4;
6610         } else {
6611                 nfscl_reqstart(nd, NFSPROC_COMMIT, nmp, fhp->nfh_fh,
6612                     fhp->nfh_len, NULL, &dsp->nfsclds_sess, vers, minorvers);
6613                 NFSDECRGLOBAL(nfsstatsv1.rpccnt[NFSPROC_COMMIT]);
6614                 NFSINCRGLOBAL(nfsstatsv1.rpccnt[NFSPROC_COMMITDS]);
6615         }
6616         NFSCL_DEBUG(4, "nfsrpc_commitds: vers=%d minvers=%d\n", vers,
6617             minorvers);
6618         NFSM_BUILD(tl, uint32_t *, NFSX_HYPER + NFSX_UNSIGNED);
6619         txdr_hyper(offset, tl);
6620         tl += 2;
6621         *tl = txdr_unsigned(cnt);
6622         nrp = dsp->nfsclds_sockp;
6623         if (nrp == NULL)
6624                 /* If NULL, use the MDS socket. */
6625                 nrp = &nmp->nm_sockreq;
6626         error = newnfs_request(nd, nmp, NULL, nrp, vp, p, cred,
6627             NFS_PROG, vers, NULL, 1, NULL, &dsp->nfsclds_sess);
6628         NFSCL_DEBUG(4, "nfsrpc_commitds: err=%d stat=%d\n", error,
6629             nd->nd_repstat);
6630         if (error != 0)
6631                 return (error);
6632         if (nd->nd_repstat == 0) {
6633                 if (vers == NFS_VER3) {
6634                         error = nfscl_wcc_data(nd, vp, &na, &attrflag, NULL,
6635                             NULL);
6636                         NFSCL_DEBUG(4, "nfsrpc_commitds: wccdata=%d\n", error);
6637                         if (error != 0)
6638                                 goto nfsmout;
6639                 }
6640                 NFSM_DISSECT(tl, u_int32_t *, NFSX_VERF);
6641                 NFSLOCKDS(dsp);
6642                 if (NFSBCMP(tl, dsp->nfsclds_verf, NFSX_VERF)) {
6643                         NFSBCOPY(tl, dsp->nfsclds_verf, NFSX_VERF);
6644                         error = NFSERR_STALEWRITEVERF;
6645                 }
6646                 NFSUNLOCKDS(dsp);
6647         }
6648 nfsmout:
6649         if (error == 0 && nd->nd_repstat != 0)
6650                 error = nd->nd_repstat;
6651         mbuf_freem(nd->nd_mrep);
6652         return (error);
6653 }
6654
6655 /*
6656  * Start up the thread that will execute nfsrpc_commitds().
6657  */
6658 static void
6659 start_commitds(void *arg, int pending)
6660 {
6661         struct nfsclwritedsdorpc *drpc;
6662
6663         drpc = (struct nfsclwritedsdorpc *)arg;
6664         drpc->err = nfsrpc_commitds(drpc->vp, drpc->off, drpc->len,
6665             drpc->dsp, drpc->fhp, drpc->vers, drpc->minorvers, drpc->cred,
6666             drpc->p);
6667         drpc->done = 1;
6668         NFSCL_DEBUG(4, "start_commitds: err=%d\n", drpc->err);
6669 }
6670
6671 /*
6672  * Set up the commit DS mirror call for the pNFS I/O thread.
6673  */
6674 static int
6675 nfsio_commitds(vnode_t vp, uint64_t offset, int cnt, struct nfsclds *dsp,
6676     struct nfsfh *fhp, int vers, int minorvers,
6677     struct nfsclwritedsdorpc *drpc, struct ucred *cred, NFSPROC_T *p)
6678 {
6679         int error, ret;
6680
6681         error = 0;
6682         drpc->done = 0;
6683         drpc->vp = vp;
6684         drpc->off = offset;
6685         drpc->len = cnt;
6686         drpc->dsp = dsp;
6687         drpc->fhp = fhp;
6688         drpc->vers = vers;
6689         drpc->minorvers = minorvers;
6690         drpc->cred = cred;
6691         drpc->p = p;
6692         drpc->inprog = 0;
6693         ret = EIO;
6694         if (nfs_pnfsiothreads != 0) {
6695                 ret = nfs_pnfsio(start_commitds, drpc);
6696                 NFSCL_DEBUG(4, "nfsio_commitds: nfs_pnfsio=%d\n", ret);
6697         }
6698         if (ret != 0)
6699                 error = nfsrpc_commitds(vp, offset, cnt, dsp, fhp, vers,
6700                     minorvers, cred, p);
6701         NFSCL_DEBUG(4, "nfsio_commitds: error=%d\n", error);
6702         return (error);
6703 }
6704
6705 /*
6706  * Set up the XDR arguments for the LayoutGet operation.
6707  */
6708 static void
6709 nfsrv_setuplayoutget(struct nfsrv_descript *nd, int iomode, uint64_t offset,
6710     uint64_t len, uint64_t minlen, nfsv4stateid_t *stateidp, int layouttype,
6711     int layoutlen, int usecurstateid)
6712 {
6713         uint32_t *tl;
6714
6715         NFSM_BUILD(tl, uint32_t *, 4 * NFSX_UNSIGNED + 3 * NFSX_HYPER +
6716             NFSX_STATEID);
6717         *tl++ = newnfs_false;           /* Don't signal availability. */
6718         *tl++ = txdr_unsigned(layouttype);
6719         *tl++ = txdr_unsigned(iomode);
6720         txdr_hyper(offset, tl);
6721         tl += 2;
6722         txdr_hyper(len, tl);
6723         tl += 2;
6724         txdr_hyper(minlen, tl);
6725         tl += 2;
6726         if (usecurstateid != 0) {
6727                 /* Special stateid for Current stateid. */
6728                 *tl++ = txdr_unsigned(1);
6729                 *tl++ = 0;
6730                 *tl++ = 0;
6731                 *tl++ = 0;
6732         } else {
6733                 *tl++ = txdr_unsigned(stateidp->seqid);
6734                 NFSCL_DEBUG(4, "layget seq=%d\n", (int)stateidp->seqid);
6735                 *tl++ = stateidp->other[0];
6736                 *tl++ = stateidp->other[1];
6737                 *tl++ = stateidp->other[2];
6738         }
6739         *tl = txdr_unsigned(layoutlen);
6740 }
6741
6742 /*
6743  * Parse the reply for a successful LayoutGet operation.
6744  */
6745 static int
6746 nfsrv_parselayoutget(struct nfsrv_descript *nd, nfsv4stateid_t *stateidp,
6747     int *retonclosep, struct nfsclflayouthead *flhp)
6748 {
6749         uint32_t *tl;
6750         struct nfsclflayout *flp, *prevflp, *tflp;
6751         int cnt, error, fhcnt, gotiomode, i, iomode, j, k, l, laytype, nfhlen;
6752         int m, mirrorcnt;
6753         uint64_t retlen, off;
6754         struct nfsfh *nfhp;
6755         uint8_t *cp;
6756         uid_t user;
6757         gid_t grp;
6758
6759         NFSCL_DEBUG(4, "in nfsrv_parselayoutget\n");
6760         error = 0;
6761         flp = NULL;
6762         gotiomode = -1;
6763         NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED + NFSX_STATEID);
6764         if (*tl++ != 0)
6765                 *retonclosep = 1;
6766         else
6767                 *retonclosep = 0;
6768         stateidp->seqid = fxdr_unsigned(uint32_t, *tl++);
6769         NFSCL_DEBUG(4, "retoncls=%d stseq=%d\n", *retonclosep,
6770             (int)stateidp->seqid);
6771         stateidp->other[0] = *tl++;
6772         stateidp->other[1] = *tl++;
6773         stateidp->other[2] = *tl++;
6774         cnt = fxdr_unsigned(int, *tl);
6775         NFSCL_DEBUG(4, "layg cnt=%d\n", cnt);
6776         if (cnt <= 0 || cnt > 10000) {
6777                 /* Don't accept more than 10000 layouts in reply. */
6778                 error = NFSERR_BADXDR;
6779                 goto nfsmout;
6780         }
6781         for (i = 0; i < cnt; i++) {
6782                 /* Dissect to the layout type. */
6783                 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_HYPER +
6784                     3 * NFSX_UNSIGNED);
6785                 off = fxdr_hyper(tl); tl += 2;
6786                 retlen = fxdr_hyper(tl); tl += 2;
6787                 iomode = fxdr_unsigned(int, *tl++);
6788                 laytype = fxdr_unsigned(int, *tl);
6789                 NFSCL_DEBUG(4, "layt=%d off=%ju len=%ju iom=%d\n", laytype,
6790                     (uintmax_t)off, (uintmax_t)retlen, iomode);
6791                 /* Ignore length of layout body for now. */
6792                 if (laytype == NFSLAYOUT_NFSV4_1_FILES) {
6793                         /* Parse the File layout up to fhcnt. */
6794                         NFSM_DISSECT(tl, uint32_t *, 3 * NFSX_UNSIGNED +
6795                             NFSX_HYPER + NFSX_V4DEVICEID);
6796                         fhcnt = fxdr_unsigned(int, *(tl + 4 +
6797                             NFSX_V4DEVICEID / NFSX_UNSIGNED));
6798                         NFSCL_DEBUG(4, "fhcnt=%d\n", fhcnt);
6799                         if (fhcnt < 0 || fhcnt > 100) {
6800                                 /* Don't accept more than 100 file handles. */
6801                                 error = NFSERR_BADXDR;
6802                                 goto nfsmout;
6803                         }
6804                         if (fhcnt > 0)
6805                                 flp = malloc(sizeof(*flp) + fhcnt *
6806                                     sizeof(struct nfsfh *), M_NFSFLAYOUT,
6807                                     M_WAITOK);
6808                         else
6809                                 flp = malloc(sizeof(*flp), M_NFSFLAYOUT,
6810                                     M_WAITOK);
6811                         flp->nfsfl_flags = NFSFL_FILE;
6812                         flp->nfsfl_fhcnt = 0;
6813                         flp->nfsfl_devp = NULL;
6814                         flp->nfsfl_off = off;
6815                         if (flp->nfsfl_off + retlen < flp->nfsfl_off)
6816                                 flp->nfsfl_end = UINT64_MAX - flp->nfsfl_off;
6817                         else
6818                                 flp->nfsfl_end = flp->nfsfl_off + retlen;
6819                         flp->nfsfl_iomode = iomode;
6820                         if (gotiomode == -1)
6821                                 gotiomode = flp->nfsfl_iomode;
6822                         /* Ignore layout body length for now. */
6823                         NFSBCOPY(tl, flp->nfsfl_dev, NFSX_V4DEVICEID);
6824                         tl += (NFSX_V4DEVICEID / NFSX_UNSIGNED);
6825                         flp->nfsfl_util = fxdr_unsigned(uint32_t, *tl++);
6826                         NFSCL_DEBUG(4, "flutil=0x%x\n", flp->nfsfl_util);
6827                         flp->nfsfl_stripe1 = fxdr_unsigned(uint32_t, *tl++);
6828                         flp->nfsfl_patoff = fxdr_hyper(tl); tl += 2;
6829                         NFSCL_DEBUG(4, "stripe1=%u poff=%ju\n",
6830                             flp->nfsfl_stripe1, (uintmax_t)flp->nfsfl_patoff);
6831                         for (j = 0; j < fhcnt; j++) {
6832                                 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
6833                                 nfhlen = fxdr_unsigned(int, *tl);
6834                                 if (nfhlen <= 0 || nfhlen > NFSX_V4FHMAX) {
6835                                         error = NFSERR_BADXDR;
6836                                         goto nfsmout;
6837                                 }
6838                                 nfhp = malloc(sizeof(*nfhp) + nfhlen - 1,
6839                                     M_NFSFH, M_WAITOK);
6840                                 flp->nfsfl_fh[j] = nfhp;
6841                                 flp->nfsfl_fhcnt++;
6842                                 nfhp->nfh_len = nfhlen;
6843                                 NFSM_DISSECT(cp, uint8_t *, NFSM_RNDUP(nfhlen));
6844                                 NFSBCOPY(cp, nfhp->nfh_fh, nfhlen);
6845                         }
6846                 } else if (laytype == NFSLAYOUT_FLEXFILE) {
6847                         NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED +
6848                             NFSX_HYPER);
6849                         mirrorcnt = fxdr_unsigned(int, *(tl + 2));
6850                         NFSCL_DEBUG(4, "mirrorcnt=%d\n", mirrorcnt);
6851                         if (mirrorcnt < 1 || mirrorcnt > NFSDEV_MAXMIRRORS) {
6852                                 error = NFSERR_BADXDR;
6853                                 goto nfsmout;
6854                         }
6855                         flp = malloc(sizeof(*flp) + mirrorcnt *
6856                             sizeof(struct nfsffm), M_NFSFLAYOUT, M_WAITOK);
6857                         flp->nfsfl_flags = NFSFL_FLEXFILE;
6858                         flp->nfsfl_mirrorcnt = mirrorcnt;
6859                         for (j = 0; j < mirrorcnt; j++)
6860                                 flp->nfsfl_ffm[j].devp = NULL;
6861                         flp->nfsfl_off = off;
6862                         if (flp->nfsfl_off + retlen < flp->nfsfl_off)
6863                                 flp->nfsfl_end = UINT64_MAX - flp->nfsfl_off;
6864                         else
6865                                 flp->nfsfl_end = flp->nfsfl_off + retlen;
6866                         flp->nfsfl_iomode = iomode;
6867                         if (gotiomode == -1)
6868                                 gotiomode = flp->nfsfl_iomode;
6869                         flp->nfsfl_stripeunit = fxdr_hyper(tl);
6870                         NFSCL_DEBUG(4, "stripeunit=%ju\n",
6871                             (uintmax_t)flp->nfsfl_stripeunit);
6872                         for (j = 0; j < mirrorcnt; j++) {
6873                                 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
6874                                 k = fxdr_unsigned(int, *tl);
6875                                 if (k < 1 || k > 128) {
6876                                         error = NFSERR_BADXDR;
6877                                         goto nfsmout;
6878                                 }
6879                                 NFSCL_DEBUG(4, "servercnt=%d\n", k);
6880                                 for (l = 0; l < k; l++) {
6881                                         NFSM_DISSECT(tl, uint32_t *,
6882                                             NFSX_V4DEVICEID + NFSX_STATEID +
6883                                             2 * NFSX_UNSIGNED);
6884                                         if (l == 0) {
6885                                                 /* Just use the first server. */
6886                                                 NFSBCOPY(tl,
6887                                                     flp->nfsfl_ffm[j].dev,
6888                                                     NFSX_V4DEVICEID);
6889                                                 tl += (NFSX_V4DEVICEID /
6890                                                     NFSX_UNSIGNED);
6891                                                 tl++;
6892                                                 flp->nfsfl_ffm[j].st.seqid =
6893                                                     *tl++;
6894                                                 flp->nfsfl_ffm[j].st.other[0] =
6895                                                     *tl++;
6896                                                 flp->nfsfl_ffm[j].st.other[1] =
6897                                                     *tl++;
6898                                                 flp->nfsfl_ffm[j].st.other[2] =
6899                                                     *tl++;
6900                                                 NFSCL_DEBUG(4, "st.seqid=%u "
6901                                                  "st.o0=0x%x st.o1=0x%x "
6902                                                  "st.o2=0x%x\n",
6903                                                  flp->nfsfl_ffm[j].st.seqid,
6904                                                  flp->nfsfl_ffm[j].st.other[0],
6905                                                  flp->nfsfl_ffm[j].st.other[1],
6906                                                  flp->nfsfl_ffm[j].st.other[2]);
6907                                         } else
6908                                                 tl += ((NFSX_V4DEVICEID +
6909                                                     NFSX_STATEID +
6910                                                     NFSX_UNSIGNED) /
6911                                                     NFSX_UNSIGNED);
6912                                         fhcnt = fxdr_unsigned(int, *tl);
6913                                         NFSCL_DEBUG(4, "fhcnt=%d\n", fhcnt);
6914                                         if (fhcnt < 1 ||
6915                                             fhcnt > NFSDEV_MAXVERS) {
6916                                                 error = NFSERR_BADXDR;
6917                                                 goto nfsmout;
6918                                         }
6919                                         for (m = 0; m < fhcnt; m++) {
6920                                                 NFSM_DISSECT(tl, uint32_t *,
6921                                                     NFSX_UNSIGNED);
6922                                                 nfhlen = fxdr_unsigned(int,
6923                                                     *tl);
6924                                                 NFSCL_DEBUG(4, "nfhlen=%d\n",
6925                                                     nfhlen);
6926                                                 if (nfhlen <= 0 || nfhlen >
6927                                                     NFSX_V4FHMAX) {
6928                                                         error = NFSERR_BADXDR;
6929                                                         goto nfsmout;
6930                                                 }
6931                                                 NFSM_DISSECT(cp, uint8_t *,
6932                                                     NFSM_RNDUP(nfhlen));
6933                                                 if (l == 0) {
6934                                                         flp->nfsfl_ffm[j].fhcnt 
6935                                                             = fhcnt;
6936                                                         nfhp = malloc(
6937                                                             sizeof(*nfhp) +
6938                                                             nfhlen - 1, M_NFSFH,
6939                                                             M_WAITOK);
6940                                                         flp->nfsfl_ffm[j].fh[m]
6941                                                             = nfhp;
6942                                                         nfhp->nfh_len = nfhlen;
6943                                                         NFSBCOPY(cp,
6944                                                             nfhp->nfh_fh,
6945                                                             nfhlen);
6946                                                         NFSCL_DEBUG(4,
6947                                                             "got fh\n");
6948                                                 }
6949                                         }
6950                                         /* Now, get the ffsd_user/ffds_group. */
6951                                         error = nfsrv_parseug(nd, 0, &user,
6952                                             &grp, curthread);
6953                                         NFSCL_DEBUG(4, "after parseu=%d\n",
6954                                             error);
6955                                         if (error == 0)
6956                                                 error = nfsrv_parseug(nd, 1,
6957                                                     &user, &grp, curthread);
6958                                         NFSCL_DEBUG(4, "aft parseg=%d\n",
6959                                             grp);
6960                                         if (error != 0)
6961                                                 goto nfsmout;
6962                                         NFSCL_DEBUG(4, "user=%d group=%d\n",
6963                                             user, grp);
6964                                         if (l == 0) {
6965                                                 flp->nfsfl_ffm[j].user = user;
6966                                                 flp->nfsfl_ffm[j].group = grp;
6967                                                 NFSCL_DEBUG(4,
6968                                                     "usr=%d grp=%d\n", user,
6969                                                     grp);
6970                                         }
6971                                 }
6972                         }
6973                         NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
6974                         flp->nfsfl_fflags = fxdr_unsigned(uint32_t, *tl++);
6975                         flp->nfsfl_statshint = fxdr_unsigned(uint32_t, *tl);
6976                         NFSCL_DEBUG(4, "fflags=0x%x statshint=%d\n",
6977                             flp->nfsfl_fflags, flp->nfsfl_statshint);
6978                 } else {
6979                         error = NFSERR_BADXDR;
6980                         goto nfsmout;
6981                 }
6982                 if (flp->nfsfl_iomode == gotiomode) {
6983                         /* Keep the list in increasing offset order. */
6984                         tflp = LIST_FIRST(flhp);
6985                         prevflp = NULL;
6986                         while (tflp != NULL &&
6987                             tflp->nfsfl_off < flp->nfsfl_off) {
6988                                 prevflp = tflp;
6989                                 tflp = LIST_NEXT(tflp, nfsfl_list);
6990                         }
6991                         if (prevflp == NULL)
6992                                 LIST_INSERT_HEAD(flhp, flp, nfsfl_list);
6993                         else
6994                                 LIST_INSERT_AFTER(prevflp, flp,
6995                                     nfsfl_list);
6996                         NFSCL_DEBUG(4, "flp inserted\n");
6997                 } else {
6998                         printf("nfscl_layoutget(): got wrong iomode\n");
6999                         nfscl_freeflayout(flp);
7000                 }
7001                 flp = NULL;
7002         }
7003 nfsmout:
7004         NFSCL_DEBUG(4, "eo nfsrv_parselayoutget=%d\n", error);
7005         if (error != 0 && flp != NULL)
7006                 nfscl_freeflayout(flp);
7007         return (error);
7008 }
7009
7010 /*
7011  * Parse a user/group digit string.
7012  */
7013 static int
7014 nfsrv_parseug(struct nfsrv_descript *nd, int dogrp, uid_t *uidp, gid_t *gidp,
7015     NFSPROC_T *p)
7016 {
7017         uint32_t *tl;
7018         char *cp, *str, str0[NFSV4_SMALLSTR + 1];
7019         uint32_t len = 0;
7020         int error = 0;
7021
7022         NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
7023         len = fxdr_unsigned(uint32_t, *tl);
7024         str = NULL;
7025         if (len > NFSV4_OPAQUELIMIT) {
7026                 error = NFSERR_BADXDR;
7027                 goto nfsmout;
7028         }
7029         NFSCL_DEBUG(4, "nfsrv_parseug: len=%d\n", len);
7030         if (len == 0) {
7031                 if (dogrp != 0)
7032                         *gidp = GID_NOGROUP;
7033                 else
7034                         *uidp = UID_NOBODY;
7035                 return (0);
7036         }
7037         if (len > NFSV4_SMALLSTR)
7038                 str = malloc(len + 1, M_TEMP, M_WAITOK);
7039         else
7040                 str = str0;
7041         NFSM_DISSECT(cp, char *, NFSM_RNDUP(len));
7042         NFSBCOPY(cp, str, len);
7043         str[len] = '\0';
7044         NFSCL_DEBUG(4, "nfsrv_parseug: str=%s\n", str);
7045         if (dogrp != 0)
7046                 error = nfsv4_strtogid(nd, str, len, gidp, p);
7047         else
7048                 error = nfsv4_strtouid(nd, str, len, uidp, p);
7049 nfsmout:
7050         if (len > NFSV4_SMALLSTR)
7051                 free(str, M_TEMP);
7052         NFSCL_DEBUG(4, "eo nfsrv_parseug=%d\n", error);
7053         return (error);
7054 }
7055
7056 /*
7057  * Similar to nfsrpc_getlayout(), except that it uses nfsrpc_openlayget(),
7058  * so that it does both an Open and a Layoutget.
7059  */
7060 static int
7061 nfsrpc_getopenlayout(struct nfsmount *nmp, vnode_t vp, u_int8_t *nfhp,
7062     int fhlen, uint8_t *newfhp, int newfhlen, uint32_t mode,
7063     struct nfsclopen *op, uint8_t *name, int namelen, struct nfscldeleg **dpp,
7064     struct ucred *cred, NFSPROC_T *p)
7065 {
7066         struct nfscllayout *lyp;
7067         struct nfsclflayout *flp;
7068         struct nfsclflayouthead flh;
7069         int error, islocked, layoutlen, recalled, retonclose, usecurstateid;
7070         int layouttype, laystat;
7071         nfsv4stateid_t stateid;
7072         struct nfsclsession *tsep;
7073
7074         error = 0;
7075         if (NFSHASFLEXFILE(nmp))
7076                 layouttype = NFSLAYOUT_FLEXFILE;
7077         else
7078                 layouttype = NFSLAYOUT_NFSV4_1_FILES;
7079         /*
7080          * If lyp is returned non-NULL, there will be a refcnt (shared lock)
7081          * on it, iff flp != NULL or a lock (exclusive lock) on it iff
7082          * flp == NULL.
7083          */
7084         lyp = nfscl_getlayout(nmp->nm_clp, newfhp, newfhlen, 0, &flp,
7085             &recalled);
7086         NFSCL_DEBUG(4, "nfsrpc_getopenlayout nfscl_getlayout lyp=%p\n", lyp);
7087         if (lyp == NULL)
7088                 islocked = 0;
7089         else if (flp != NULL)
7090                 islocked = 1;
7091         else
7092                 islocked = 2;
7093         if ((lyp == NULL || flp == NULL) && recalled == 0) {
7094                 LIST_INIT(&flh);
7095                 tsep = nfsmnt_mdssession(nmp);
7096                 layoutlen = tsep->nfsess_maxcache - (NFSX_STATEID +
7097                     3 * NFSX_UNSIGNED);
7098                 if (lyp == NULL)
7099                         usecurstateid = 1;
7100                 else {
7101                         usecurstateid = 0;
7102                         stateid.seqid = lyp->nfsly_stateid.seqid;
7103                         stateid.other[0] = lyp->nfsly_stateid.other[0];
7104                         stateid.other[1] = lyp->nfsly_stateid.other[1];
7105                         stateid.other[2] = lyp->nfsly_stateid.other[2];
7106                 }
7107                 error = nfsrpc_openlayoutrpc(nmp, vp, nfhp, fhlen,
7108                     newfhp, newfhlen, mode, op, name, namelen,
7109                     dpp, &stateid, usecurstateid, layouttype, layoutlen,
7110                     &retonclose, &flh, &laystat, cred, p);
7111                 NFSCL_DEBUG(4, "aft nfsrpc_openlayoutrpc laystat=%d err=%d\n",
7112                     laystat, error);
7113                 laystat = nfsrpc_layoutgetres(nmp, vp, newfhp, newfhlen,
7114                     &stateid, retonclose, NULL, &lyp, &flh, layouttype, laystat,
7115                     &islocked, cred, p);
7116         } else
7117                 error = nfsrpc_openrpc(nmp, vp, nfhp, fhlen, newfhp, newfhlen,
7118                     mode, op, name, namelen, dpp, 0, 0, cred, p, 0, 0);
7119         if (islocked == 2)
7120                 nfscl_rellayout(lyp, 1);
7121         else if (islocked == 1)
7122                 nfscl_rellayout(lyp, 0);
7123         return (error);
7124 }
7125
7126 /*
7127  * This function does an Open+LayoutGet for an NFSv4.1 mount with pNFS
7128  * enabled, only for the CLAIM_NULL case.  All other NFSv4 Opens are
7129  * handled by nfsrpc_openrpc().
7130  * For the case where op == NULL, dvp is the directory.  When op != NULL, it
7131  * can be NULL.
7132  */
7133 static int
7134 nfsrpc_openlayoutrpc(struct nfsmount *nmp, vnode_t vp, u_int8_t *nfhp,
7135     int fhlen, uint8_t *newfhp, int newfhlen, uint32_t mode,
7136     struct nfsclopen *op, uint8_t *name, int namelen, struct nfscldeleg **dpp,
7137     nfsv4stateid_t *stateidp, int usecurstateid, int layouttype,
7138     int layoutlen, int *retonclosep, struct nfsclflayouthead *flhp,
7139     int *laystatp, struct ucred *cred, NFSPROC_T *p)
7140 {
7141         uint32_t *tl;
7142         struct nfsrv_descript nfsd, *nd = &nfsd;
7143         struct nfscldeleg *ndp = NULL;
7144         struct nfsvattr nfsva;
7145         struct nfsclsession *tsep;
7146         uint32_t rflags, deleg;
7147         nfsattrbit_t attrbits;
7148         int error, ret, acesize, limitby, iomode;
7149
7150         *dpp = NULL;
7151         *laystatp = ENXIO;
7152         nfscl_reqstart(nd, NFSPROC_OPENLAYGET, nmp, nfhp, fhlen, NULL, NULL,
7153             0, 0);
7154         NFSM_BUILD(tl, uint32_t *, 5 * NFSX_UNSIGNED);
7155         *tl++ = txdr_unsigned(op->nfso_own->nfsow_seqid);
7156         *tl++ = txdr_unsigned(mode & NFSV4OPEN_ACCESSBOTH);
7157         *tl++ = txdr_unsigned((mode >> NFSLCK_SHIFT) & NFSV4OPEN_DENYBOTH);
7158         tsep = nfsmnt_mdssession(nmp);
7159         *tl++ = tsep->nfsess_clientid.lval[0];
7160         *tl = tsep->nfsess_clientid.lval[1];
7161         nfsm_strtom(nd, op->nfso_own->nfsow_owner, NFSV4CL_LOCKNAMELEN);
7162         NFSM_BUILD(tl, uint32_t *, 2 * NFSX_UNSIGNED);
7163         *tl++ = txdr_unsigned(NFSV4OPEN_NOCREATE);
7164         *tl = txdr_unsigned(NFSV4OPEN_CLAIMNULL);
7165         nfsm_strtom(nd, name, namelen);
7166         NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
7167         *tl = txdr_unsigned(NFSV4OP_GETATTR);
7168         NFSZERO_ATTRBIT(&attrbits);
7169         NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_CHANGE);
7170         NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEMODIFY);
7171         nfsrv_putattrbit(nd, &attrbits);
7172         NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
7173         *tl = txdr_unsigned(NFSV4OP_LAYOUTGET);
7174         if ((mode & NFSV4OPEN_ACCESSWRITE) != 0)
7175                 iomode = NFSLAYOUTIOMODE_RW;
7176         else
7177                 iomode = NFSLAYOUTIOMODE_READ;
7178         nfsrv_setuplayoutget(nd, iomode, 0, UINT64_MAX, 0, stateidp,
7179             layouttype, layoutlen, usecurstateid);
7180         error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, vp, p, cred,
7181             NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
7182         if (error != 0)
7183                 return (error);
7184         NFSCL_INCRSEQID(op->nfso_own->nfsow_seqid, nd);
7185         if (nd->nd_repstat != 0)
7186                 *laystatp = nd->nd_repstat;
7187         if ((nd->nd_flag & ND_NOMOREDATA) == 0) {
7188                 /* ND_NOMOREDATA will be set if the Open operation failed. */
7189                 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
7190                     6 * NFSX_UNSIGNED);
7191                 op->nfso_stateid.seqid = *tl++;
7192                 op->nfso_stateid.other[0] = *tl++;
7193                 op->nfso_stateid.other[1] = *tl++;
7194                 op->nfso_stateid.other[2] = *tl;
7195                 rflags = fxdr_unsigned(u_int32_t, *(tl + 6));
7196                 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
7197                 if (error != 0)
7198                         goto nfsmout;
7199                 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
7200                 deleg = fxdr_unsigned(u_int32_t, *tl);
7201                 if (deleg == NFSV4OPEN_DELEGATEREAD ||
7202                     deleg == NFSV4OPEN_DELEGATEWRITE) {
7203                         if (!(op->nfso_own->nfsow_clp->nfsc_flags &
7204                               NFSCLFLAGS_FIRSTDELEG))
7205                                 op->nfso_own->nfsow_clp->nfsc_flags |=
7206                                   (NFSCLFLAGS_FIRSTDELEG | NFSCLFLAGS_GOTDELEG);
7207                         ndp = malloc(sizeof(struct nfscldeleg) + newfhlen,
7208                             M_NFSCLDELEG, M_WAITOK);
7209                         LIST_INIT(&ndp->nfsdl_owner);
7210                         LIST_INIT(&ndp->nfsdl_lock);
7211                         ndp->nfsdl_clp = op->nfso_own->nfsow_clp;
7212                         ndp->nfsdl_fhlen = newfhlen;
7213                         NFSBCOPY(newfhp, ndp->nfsdl_fh, newfhlen);
7214                         newnfs_copyincred(cred, &ndp->nfsdl_cred);
7215                         nfscl_lockinit(&ndp->nfsdl_rwlock);
7216                         NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
7217                             NFSX_UNSIGNED);
7218                         ndp->nfsdl_stateid.seqid = *tl++;
7219                         ndp->nfsdl_stateid.other[0] = *tl++;
7220                         ndp->nfsdl_stateid.other[1] = *tl++;
7221                         ndp->nfsdl_stateid.other[2] = *tl++;
7222                         ret = fxdr_unsigned(int, *tl);
7223                         if (deleg == NFSV4OPEN_DELEGATEWRITE) {
7224                                 ndp->nfsdl_flags = NFSCLDL_WRITE;
7225                                 /*
7226                                  * Indicates how much the file can grow.
7227                                  */
7228                                 NFSM_DISSECT(tl, u_int32_t *,
7229                                     3 * NFSX_UNSIGNED);
7230                                 limitby = fxdr_unsigned(int, *tl++);
7231                                 switch (limitby) {
7232                                 case NFSV4OPEN_LIMITSIZE:
7233                                         ndp->nfsdl_sizelimit = fxdr_hyper(tl);
7234                                         break;
7235                                 case NFSV4OPEN_LIMITBLOCKS:
7236                                         ndp->nfsdl_sizelimit =
7237                                             fxdr_unsigned(u_int64_t, *tl++);
7238                                         ndp->nfsdl_sizelimit *=
7239                                             fxdr_unsigned(u_int64_t, *tl);
7240                                         break;
7241                                 default:
7242                                         error = NFSERR_BADXDR;
7243                                         goto nfsmout;
7244                                 };
7245                         } else
7246                                 ndp->nfsdl_flags = NFSCLDL_READ;
7247                         if (ret != 0)
7248                                 ndp->nfsdl_flags |= NFSCLDL_RECALL;
7249                         error = nfsrv_dissectace(nd, &ndp->nfsdl_ace, &ret,
7250                             &acesize, p);
7251                         if (error != 0)
7252                                 goto nfsmout;
7253                 } else if (deleg != NFSV4OPEN_DELEGATENONE) {
7254                         error = NFSERR_BADXDR;
7255                         goto nfsmout;
7256                 }
7257                 if ((rflags & NFSV4OPEN_LOCKTYPEPOSIX) != 0 ||
7258                     nfscl_assumeposixlocks)
7259                         op->nfso_posixlock = 1;
7260                 else
7261                         op->nfso_posixlock = 0;
7262                 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
7263                 /* If the 2nd element == NFS_OK, the Getattr succeeded. */
7264                 if (*++tl == 0) {
7265                         error = nfsv4_loadattr(nd, NULL, &nfsva, NULL,
7266                             NULL, 0, NULL, NULL, NULL, NULL, NULL, 0,
7267                             NULL, NULL, NULL, p, cred);
7268                         if (error != 0)
7269                                 goto nfsmout;
7270                         if (ndp != NULL) {
7271                                 ndp->nfsdl_change = nfsva.na_filerev;
7272                                 ndp->nfsdl_modtime = nfsva.na_mtime;
7273                                 ndp->nfsdl_flags |= NFSCLDL_MODTIMESET;
7274                                 *dpp = ndp;
7275                                 ndp = NULL;
7276                         }
7277                         /*
7278                          * At this point, the Open has succeeded, so set
7279                          * nd_repstat = NFS_OK.  If the Layoutget failed,
7280                          * this function just won't return a layout.
7281                          */
7282                         if (nd->nd_repstat == 0) {
7283                                 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
7284                                 *laystatp = fxdr_unsigned(int, *++tl);
7285                                 if (*laystatp == 0) {
7286                                         error = nfsrv_parselayoutget(nd,
7287                                             stateidp, retonclosep, flhp);
7288                                         if (error != 0)
7289                                                 *laystatp = error;
7290                                 }
7291                         } else
7292                                 nd->nd_repstat = 0;     /* Return 0 for Open. */
7293                 }
7294         }
7295         if (nd->nd_repstat != 0 && error == 0)
7296                 error = nd->nd_repstat;
7297 nfsmout:
7298         free(ndp, M_NFSCLDELEG);
7299         mbuf_freem(nd->nd_mrep);
7300         return (error);
7301 }
7302
7303 /*
7304  * Similar nfsrpc_createv4(), but also does the LayoutGet operation.
7305  * Used only for mounts with pNFS enabled.
7306  */
7307 static int
7308 nfsrpc_createlayout(vnode_t dvp, char *name, int namelen, struct vattr *vap,
7309     nfsquad_t cverf, int fmode, struct nfsclowner *owp, struct nfscldeleg **dpp,
7310     struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap,
7311     struct nfsvattr *nnap, struct nfsfh **nfhpp, int *attrflagp,
7312     int *dattrflagp, void *dstuff, int *unlockedp, nfsv4stateid_t *stateidp,
7313     int usecurstateid, int layouttype, int layoutlen, int *retonclosep,
7314     struct nfsclflayouthead *flhp, int *laystatp)
7315 {
7316         uint32_t *tl;
7317         int error = 0, deleg, newone, ret, acesize, limitby;
7318         struct nfsrv_descript nfsd, *nd = &nfsd;
7319         struct nfsclopen *op;
7320         struct nfscldeleg *dp = NULL;
7321         struct nfsnode *np;
7322         struct nfsfh *nfhp;
7323         struct nfsclsession *tsep;
7324         nfsattrbit_t attrbits;
7325         nfsv4stateid_t stateid;
7326         struct nfsmount *nmp;
7327
7328         nmp = VFSTONFS(dvp->v_mount);
7329         np = VTONFS(dvp);
7330         *laystatp = ENXIO;
7331         *unlockedp = 0;
7332         *nfhpp = NULL;
7333         *dpp = NULL;
7334         *attrflagp = 0;
7335         *dattrflagp = 0;
7336         if (namelen > NFS_MAXNAMLEN)
7337                 return (ENAMETOOLONG);
7338         NFSCL_REQSTART(nd, NFSPROC_CREATELAYGET, dvp);
7339         /*
7340          * For V4, this is actually an Open op.
7341          */
7342         NFSM_BUILD(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
7343         *tl++ = txdr_unsigned(owp->nfsow_seqid);
7344         *tl++ = txdr_unsigned(NFSV4OPEN_ACCESSWRITE |
7345             NFSV4OPEN_ACCESSREAD);
7346         *tl++ = txdr_unsigned(NFSV4OPEN_DENYNONE);
7347         tsep = nfsmnt_mdssession(nmp);
7348         *tl++ = tsep->nfsess_clientid.lval[0];
7349         *tl = tsep->nfsess_clientid.lval[1];
7350         nfsm_strtom(nd, owp->nfsow_owner, NFSV4CL_LOCKNAMELEN);
7351         NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
7352         *tl++ = txdr_unsigned(NFSV4OPEN_CREATE);
7353         if ((fmode & O_EXCL) != 0) {
7354                 if (NFSHASSESSPERSIST(nmp)) {
7355                         /* Use GUARDED for persistent sessions. */
7356                         *tl = txdr_unsigned(NFSCREATE_GUARDED);
7357                         nfscl_fillsattr(nd, vap, dvp, 0, 0);
7358                 } else {
7359                         /* Otherwise, use EXCLUSIVE4_1. */
7360                         *tl = txdr_unsigned(NFSCREATE_EXCLUSIVE41);
7361                         NFSM_BUILD(tl, u_int32_t *, NFSX_VERF);
7362                         *tl++ = cverf.lval[0];
7363                         *tl = cverf.lval[1];
7364                         nfscl_fillsattr(nd, vap, dvp, 0, 0);
7365                 }
7366         } else {
7367                 *tl = txdr_unsigned(NFSCREATE_UNCHECKED);
7368                 nfscl_fillsattr(nd, vap, dvp, 0, 0);
7369         }
7370         NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
7371         *tl = txdr_unsigned(NFSV4OPEN_CLAIMNULL);
7372         nfsm_strtom(nd, name, namelen);
7373         /* Get the new file's handle and attributes, plus save the FH. */
7374         NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
7375         *tl++ = txdr_unsigned(NFSV4OP_SAVEFH);
7376         *tl++ = txdr_unsigned(NFSV4OP_GETFH);
7377         *tl = txdr_unsigned(NFSV4OP_GETATTR);
7378         NFSGETATTR_ATTRBIT(&attrbits);
7379         nfsrv_putattrbit(nd, &attrbits);
7380         /* Get the directory's post-op attributes. */
7381         NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
7382         *tl = txdr_unsigned(NFSV4OP_PUTFH);
7383         nfsm_fhtom(nd, np->n_fhp->nfh_fh, np->n_fhp->nfh_len, 0);
7384         NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
7385         *tl = txdr_unsigned(NFSV4OP_GETATTR);
7386         nfsrv_putattrbit(nd, &attrbits);
7387         NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
7388         *tl++ = txdr_unsigned(NFSV4OP_RESTOREFH);
7389         *tl = txdr_unsigned(NFSV4OP_LAYOUTGET);
7390         nfsrv_setuplayoutget(nd, NFSLAYOUTIOMODE_RW, 0, UINT64_MAX, 0, stateidp,
7391             layouttype, layoutlen, usecurstateid);
7392         error = nfscl_request(nd, dvp, p, cred, dstuff);
7393         if (error != 0)
7394                 return (error);
7395         NFSCL_DEBUG(4, "nfsrpc_createlayout stat=%d err=%d\n", nd->nd_repstat,
7396             error);
7397         if (nd->nd_repstat != 0)
7398                 *laystatp = nd->nd_repstat;
7399         NFSCL_INCRSEQID(owp->nfsow_seqid, nd);
7400         if ((nd->nd_flag & ND_NOMOREDATA) == 0) {
7401                 NFSCL_DEBUG(4, "nfsrpc_createlayout open succeeded\n");
7402                 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
7403                     6 * NFSX_UNSIGNED);
7404                 stateid.seqid = *tl++;
7405                 stateid.other[0] = *tl++;
7406                 stateid.other[1] = *tl++;
7407                 stateid.other[2] = *tl;
7408                 nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
7409                 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
7410                 deleg = fxdr_unsigned(int, *tl);
7411                 if (deleg == NFSV4OPEN_DELEGATEREAD ||
7412                     deleg == NFSV4OPEN_DELEGATEWRITE) {
7413                         if (!(owp->nfsow_clp->nfsc_flags &
7414                               NFSCLFLAGS_FIRSTDELEG))
7415                                 owp->nfsow_clp->nfsc_flags |=
7416                                   (NFSCLFLAGS_FIRSTDELEG | NFSCLFLAGS_GOTDELEG);
7417                         dp = malloc(sizeof(struct nfscldeleg) + NFSX_V4FHMAX,
7418                             M_NFSCLDELEG, M_WAITOK);
7419                         LIST_INIT(&dp->nfsdl_owner);
7420                         LIST_INIT(&dp->nfsdl_lock);
7421                         dp->nfsdl_clp = owp->nfsow_clp;
7422                         newnfs_copyincred(cred, &dp->nfsdl_cred);
7423                         nfscl_lockinit(&dp->nfsdl_rwlock);
7424                         NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
7425                             NFSX_UNSIGNED);
7426                         dp->nfsdl_stateid.seqid = *tl++;
7427                         dp->nfsdl_stateid.other[0] = *tl++;
7428                         dp->nfsdl_stateid.other[1] = *tl++;
7429                         dp->nfsdl_stateid.other[2] = *tl++;
7430                         ret = fxdr_unsigned(int, *tl);
7431                         if (deleg == NFSV4OPEN_DELEGATEWRITE) {
7432                                 dp->nfsdl_flags = NFSCLDL_WRITE;
7433                                 /*
7434                                  * Indicates how much the file can grow.
7435                                  */
7436                                 NFSM_DISSECT(tl, u_int32_t *,
7437                                     3 * NFSX_UNSIGNED);
7438                                 limitby = fxdr_unsigned(int, *tl++);
7439                                 switch (limitby) {
7440                                 case NFSV4OPEN_LIMITSIZE:
7441                                         dp->nfsdl_sizelimit = fxdr_hyper(tl);
7442                                         break;
7443                                 case NFSV4OPEN_LIMITBLOCKS:
7444                                         dp->nfsdl_sizelimit =
7445                                             fxdr_unsigned(u_int64_t, *tl++);
7446                                         dp->nfsdl_sizelimit *=
7447                                             fxdr_unsigned(u_int64_t, *tl);
7448                                         break;
7449                                 default:
7450                                         error = NFSERR_BADXDR;
7451                                         goto nfsmout;
7452                                 };
7453                         } else {
7454                                 dp->nfsdl_flags = NFSCLDL_READ;
7455                         }
7456                         if (ret != 0)
7457                                 dp->nfsdl_flags |= NFSCLDL_RECALL;
7458                         error = nfsrv_dissectace(nd, &dp->nfsdl_ace, &ret,
7459                             &acesize, p);
7460                         if (error != 0)
7461                                 goto nfsmout;
7462                 } else if (deleg != NFSV4OPEN_DELEGATENONE) {
7463                         error = NFSERR_BADXDR;
7464                         goto nfsmout;
7465                 }
7466
7467                 /* Now, we should have the status for the SaveFH. */
7468                 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
7469                 if (*++tl == 0) {
7470                         NFSCL_DEBUG(4, "nfsrpc_createlayout SaveFH ok\n");
7471                         /*
7472                          * Now, process the GetFH and Getattr for the newly
7473                          * created file. nfscl_mtofh() will set
7474                          * ND_NOMOREDATA if these weren't successful.
7475                          */
7476                         error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp);
7477                         NFSCL_DEBUG(4, "aft nfscl_mtofh err=%d\n", error);
7478                         if (error != 0)
7479                                 goto nfsmout;
7480                 } else
7481                         nd->nd_flag |= ND_NOMOREDATA;
7482                 /* Now we have the PutFH and Getattr for the directory. */
7483                 if ((nd->nd_flag & ND_NOMOREDATA) == 0) {
7484                         NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
7485                         if (*++tl != 0)
7486                                 nd->nd_flag |= ND_NOMOREDATA;
7487                         else {
7488                                 NFSM_DISSECT(tl, uint32_t *, 2 *
7489                                     NFSX_UNSIGNED);
7490                                 if (*++tl != 0)
7491                                         nd->nd_flag |= ND_NOMOREDATA;
7492                         }
7493                 }
7494                 if ((nd->nd_flag & ND_NOMOREDATA) == 0) {
7495                         /* Load the directory attributes. */
7496                         error = nfsm_loadattr(nd, dnap);
7497                         NFSCL_DEBUG(4, "aft nfsm_loadattr err=%d\n", error);
7498                         if (error != 0)
7499                                 goto nfsmout;
7500                         *dattrflagp = 1;
7501                         if (dp != NULL && *attrflagp != 0) {
7502                                 dp->nfsdl_change = nnap->na_filerev;
7503                                 dp->nfsdl_modtime = nnap->na_mtime;
7504                                 dp->nfsdl_flags |= NFSCLDL_MODTIMESET;
7505                         }
7506                         /*
7507                          * We can now complete the Open state.
7508                          */
7509                         nfhp = *nfhpp;
7510                         if (dp != NULL) {
7511                                 dp->nfsdl_fhlen = nfhp->nfh_len;
7512                                 NFSBCOPY(nfhp->nfh_fh, dp->nfsdl_fh,
7513                                     nfhp->nfh_len);
7514                         }
7515                         /*
7516                          * Get an Open structure that will be
7517                          * attached to the OpenOwner, acquired already.
7518                          */
7519                         error = nfscl_open(dvp, nfhp->nfh_fh, nfhp->nfh_len, 
7520                             (NFSV4OPEN_ACCESSWRITE | NFSV4OPEN_ACCESSREAD), 0,
7521                             cred, p, NULL, &op, &newone, NULL, 0);
7522                         if (error != 0)
7523                                 goto nfsmout;
7524                         op->nfso_stateid = stateid;
7525                         newnfs_copyincred(cred, &op->nfso_cred);
7526         
7527                         nfscl_openrelease(nmp, op, error, newone);
7528                         *unlockedp = 1;
7529
7530                         /* Now, handle the RestoreFH and LayoutGet. */
7531                         if (nd->nd_repstat == 0) {
7532                                 NFSM_DISSECT(tl, uint32_t *, 4 * NFSX_UNSIGNED);
7533                                 *laystatp = fxdr_unsigned(int, *(tl + 3));
7534                                 if (*laystatp == 0) {
7535                                         error = nfsrv_parselayoutget(nd,
7536                                             stateidp, retonclosep, flhp);
7537                                         if (error != 0)
7538                                                 *laystatp = error;
7539                                 }
7540                                 NFSCL_DEBUG(4, "aft nfsrv_parselayout err=%d\n",
7541                                     error);
7542                         } else
7543                                 nd->nd_repstat = 0;
7544                 }
7545         }
7546         if (nd->nd_repstat != 0 && error == 0)
7547                 error = nd->nd_repstat;
7548         if (error == NFSERR_STALECLIENTID || error == NFSERR_BADSESSION)
7549                 nfscl_initiate_recovery(owp->nfsow_clp);
7550 nfsmout:
7551         NFSCL_DEBUG(4, "eo nfsrpc_createlayout err=%d\n", error);
7552         if (error == 0)
7553                 *dpp = dp;
7554         else
7555                 free(dp, M_NFSCLDELEG);
7556         mbuf_freem(nd->nd_mrep);
7557         return (error);
7558 }
7559
7560 /*
7561  * Similar to nfsrpc_getopenlayout(), except that it used for the Create case.
7562  */
7563 static int
7564 nfsrpc_getcreatelayout(vnode_t dvp, char *name, int namelen, struct vattr *vap,
7565     nfsquad_t cverf, int fmode, struct nfsclowner *owp, struct nfscldeleg **dpp,
7566     struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap,
7567     struct nfsvattr *nnap, struct nfsfh **nfhpp, int *attrflagp,
7568     int *dattrflagp, void *dstuff, int *unlockedp)
7569 {
7570         struct nfscllayout *lyp;
7571         struct nfsclflayouthead flh;
7572         struct nfsfh *nfhp;
7573         struct nfsclsession *tsep;
7574         struct nfsmount *nmp;
7575         nfsv4stateid_t stateid;
7576         int error, layoutlen, layouttype, retonclose, laystat;
7577
7578         error = 0;
7579         nmp = VFSTONFS(dvp->v_mount);
7580         if (NFSHASFLEXFILE(nmp))
7581                 layouttype = NFSLAYOUT_FLEXFILE;
7582         else
7583                 layouttype = NFSLAYOUT_NFSV4_1_FILES;
7584         LIST_INIT(&flh);
7585         tsep = nfsmnt_mdssession(nmp);
7586         layoutlen = tsep->nfsess_maxcache - (NFSX_STATEID + 3 * NFSX_UNSIGNED);
7587         error = nfsrpc_createlayout(dvp, name, namelen, vap, cverf, fmode,
7588             owp, dpp, cred, p, dnap, nnap, nfhpp, attrflagp, dattrflagp,
7589             dstuff, unlockedp, &stateid, 1, layouttype, layoutlen, &retonclose,
7590             &flh, &laystat);
7591         NFSCL_DEBUG(4, "aft nfsrpc_createlayoutrpc laystat=%d err=%d\n",
7592             laystat, error);
7593         lyp = NULL;
7594         if (laystat == 0) {
7595                 nfhp = *nfhpp;
7596                 laystat = nfsrpc_layoutgetres(nmp, dvp, nfhp->nfh_fh,
7597                     nfhp->nfh_len, &stateid, retonclose, NULL, &lyp, &flh,
7598                     layouttype, laystat, NULL, cred, p);
7599         } else
7600                 laystat = nfsrpc_layoutgetres(nmp, dvp, NULL, 0, &stateid,
7601                     retonclose, NULL, &lyp, &flh, layouttype, laystat, NULL,
7602                     cred, p);
7603         if (laystat == 0)
7604                 nfscl_rellayout(lyp, 0);
7605         return (error);
7606 }
7607
7608 /*
7609  * Process the results of a layoutget() operation.
7610  */
7611 static int
7612 nfsrpc_layoutgetres(struct nfsmount *nmp, vnode_t vp, uint8_t *newfhp,
7613     int newfhlen, nfsv4stateid_t *stateidp, int retonclose, uint32_t *notifybit,
7614     struct nfscllayout **lypp, struct nfsclflayouthead *flhp, int layouttype,
7615     int laystat, int *islockedp, struct ucred *cred, NFSPROC_T *p)
7616 {
7617         struct nfsclflayout *tflp;
7618         struct nfscldevinfo *dip;
7619         uint8_t *dev;
7620         int i, mirrorcnt;
7621
7622         if (laystat == NFSERR_UNKNLAYOUTTYPE) {
7623                 NFSLOCKMNT(nmp);
7624                 if (!NFSHASFLEXFILE(nmp)) {
7625                         /* Switch to using Flex File Layout. */
7626                         nmp->nm_state |= NFSSTA_FLEXFILE;
7627                 } else if (layouttype == NFSLAYOUT_FLEXFILE) {
7628                         /* Disable pNFS. */
7629                         NFSCL_DEBUG(1, "disable PNFS\n");
7630                         nmp->nm_state &= ~(NFSSTA_PNFS | NFSSTA_FLEXFILE);
7631                 }
7632                 NFSUNLOCKMNT(nmp);
7633         }
7634         if (laystat == 0) {
7635                 NFSCL_DEBUG(4, "nfsrpc_layoutgetres at FOREACH\n");
7636                 LIST_FOREACH(tflp, flhp, nfsfl_list) {
7637                         if (layouttype == NFSLAYOUT_FLEXFILE)
7638                                 mirrorcnt = tflp->nfsfl_mirrorcnt;
7639                         else
7640                                 mirrorcnt = 1;
7641                         for (i = 0; i < mirrorcnt; i++) {
7642                                 laystat = nfscl_adddevinfo(nmp, NULL, i, tflp);
7643                                 NFSCL_DEBUG(4, "aft adddev=%d\n", laystat);
7644                                 if (laystat != 0) {
7645                                         if (layouttype == NFSLAYOUT_FLEXFILE)
7646                                                 dev = tflp->nfsfl_ffm[i].dev;
7647                                         else
7648                                                 dev = tflp->nfsfl_dev;
7649                                         laystat = nfsrpc_getdeviceinfo(nmp, dev,
7650                                             layouttype, notifybit, &dip, cred,
7651                                             p);
7652                                         NFSCL_DEBUG(4, "aft nfsrpc_gdi=%d\n",
7653                                             laystat);
7654                                         if (laystat != 0)
7655                                                 goto out;
7656                                         laystat = nfscl_adddevinfo(nmp, dip, i,
7657                                             tflp);
7658                                         if (laystat != 0)
7659                                                 printf("nfsrpc_layoutgetresout"
7660                                                     ": cannot add\n");
7661                                 }
7662                         }
7663                 }
7664         }
7665 out:
7666         if (laystat == 0) {
7667                 /*
7668                  * nfscl_layout() always returns with the nfsly_lock
7669                  * set to a refcnt (shared lock).
7670                  * Passing in dvp is sufficient, since it is only used to
7671                  * get the fsid for the file system.
7672                  */
7673                 laystat = nfscl_layout(nmp, vp, newfhp, newfhlen, stateidp,
7674                     layouttype, retonclose, flhp, lypp, cred, p);
7675                 NFSCL_DEBUG(4, "nfsrpc_layoutgetres: aft nfscl_layout=%d\n",
7676                     laystat);
7677                 if (laystat == 0 && islockedp != NULL)
7678                         *islockedp = 1;
7679         }
7680         return (laystat);
7681 }
7682