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