]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/fs/nfsclient/nfs_clrpcops.c
MFC: r363210
[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                                         (newvp->v_type != VDIR ||
3672                                          dctime.tv_sec != 0)) {
3673                                         cache_enter_time(ndp->ni_dvp,
3674                                             ndp->ni_vp, cnp,
3675                                             &nfsva.na_ctime,
3676                                             newvp->v_type != VDIR ? NULL :
3677                                             &dctime);
3678                                     }
3679                                     if (unlocknewvp)
3680                                         vput(newvp);
3681                                     else
3682                                         vrele(newvp);
3683                                     newvp = NULLVP;
3684                                 }
3685                             }
3686                         } else if (nfhp != NULL) {
3687                             free(nfhp, M_NFSFH);
3688                         }
3689                         NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3690                         more_dirs = fxdr_unsigned(int, *tl);
3691                 }
3692                 /*
3693                  * If at end of rpc data, get the eof boolean
3694                  */
3695                 if (!more_dirs) {
3696                         NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3697                         eof = fxdr_unsigned(int, *tl);
3698                         if (tryformoredirs)
3699                                 more_dirs = !eof;
3700                         if (nd->nd_flag & ND_NFSV4) {
3701                                 error = nfscl_postop_attr(nd, nap, attrflagp,
3702                                     stuff);
3703                                 if (error)
3704                                         goto nfsmout;
3705                         }
3706                 }
3707                 mbuf_freem(nd->nd_mrep);
3708                 nd->nd_mrep = NULL;
3709         }
3710         /*
3711          * Fill last record, iff any, out to a multiple of DIRBLKSIZ
3712          * by increasing d_reclen for the last record.
3713          */
3714         if (blksiz > 0) {
3715                 left = DIRBLKSIZ - blksiz;
3716                 NFSBZERO(uio_iov_base(uiop), left);
3717                 dp->d_reclen += left;
3718                 uio_iov_base_add(uiop, left);
3719                 uio_iov_len_add(uiop, -(left));
3720                 uio_uio_resid_add(uiop, -(left));
3721                 uiop->uio_offset += left;
3722         }
3723
3724         /*
3725          * If returning no data, assume end of file.
3726          * If not bigenough, return not end of file, since you aren't
3727          *    returning all the data
3728          * Otherwise, return the eof flag from the server.
3729          */
3730         if (eofp != NULL) {
3731                 if (tresid == uio_uio_resid(uiop))
3732                         *eofp = 1;
3733                 else if (!bigenough)
3734                         *eofp = 0;
3735                 else
3736                         *eofp = eof;
3737         }
3738
3739         /*
3740          * Add extra empty records to any remaining DIRBLKSIZ chunks.
3741          */
3742         while (uio_uio_resid(uiop) > 0 && uio_uio_resid(uiop) != tresid) {
3743                 dp = (struct dirent *)uio_iov_base(uiop);
3744                 NFSBZERO(dp, DIRBLKSIZ);
3745                 dp->d_type = DT_UNKNOWN;
3746                 tl = (u_int32_t *)&dp->d_name[4];
3747                 *tl++ = cookie.lval[0];
3748                 *tl = cookie.lval[1];
3749                 dp->d_reclen = DIRBLKSIZ;
3750                 uio_iov_base_add(uiop, DIRBLKSIZ);
3751                 uio_iov_len_add(uiop, -(DIRBLKSIZ));
3752                 uio_uio_resid_add(uiop, -(DIRBLKSIZ));
3753                 uiop->uio_offset += DIRBLKSIZ;
3754         }
3755
3756 nfsmout:
3757         if (nd->nd_mrep != NULL)
3758                 mbuf_freem(nd->nd_mrep);
3759         return (error);
3760 }
3761 #endif  /* !APPLE */
3762
3763 /*
3764  * Nfs commit rpc
3765  */
3766 int
3767 nfsrpc_commit(vnode_t vp, u_quad_t offset, int cnt, struct ucred *cred,
3768     NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, void *stuff)
3769 {
3770         u_int32_t *tl;
3771         struct nfsrv_descript nfsd, *nd = &nfsd;
3772         nfsattrbit_t attrbits;
3773         int error;
3774         struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
3775         
3776         *attrflagp = 0;
3777         NFSCL_REQSTART(nd, NFSPROC_COMMIT, vp);
3778         NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
3779         txdr_hyper(offset, tl);
3780         tl += 2;
3781         *tl = txdr_unsigned(cnt);
3782         if (nd->nd_flag & ND_NFSV4) {
3783                 /*
3784                  * And do a Getattr op.
3785                  */
3786                 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
3787                 *tl = txdr_unsigned(NFSV4OP_GETATTR);
3788                 NFSGETATTR_ATTRBIT(&attrbits);
3789                 (void) nfsrv_putattrbit(nd, &attrbits);
3790         }
3791         error = nfscl_request(nd, vp, p, cred, stuff);
3792         if (error)
3793                 return (error);
3794         error = nfscl_wcc_data(nd, vp, nap, attrflagp, NULL, stuff);
3795         if (!error && !nd->nd_repstat) {
3796                 NFSM_DISSECT(tl, u_int32_t *, NFSX_VERF);
3797                 NFSLOCKMNT(nmp);
3798                 if (NFSBCMP(nmp->nm_verf, tl, NFSX_VERF)) {
3799                         NFSBCOPY(tl, nmp->nm_verf, NFSX_VERF);
3800                         nd->nd_repstat = NFSERR_STALEWRITEVERF;
3801                 }
3802                 NFSUNLOCKMNT(nmp);
3803                 if (nd->nd_flag & ND_NFSV4)
3804                         error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
3805         }
3806 nfsmout:
3807         if (!error && nd->nd_repstat)
3808                 error = nd->nd_repstat;
3809         mbuf_freem(nd->nd_mrep);
3810         return (error);
3811 }
3812
3813 /*
3814  * NFS byte range lock rpc.
3815  * (Mostly just calls one of the three lower level RPC routines.)
3816  */
3817 int
3818 nfsrpc_advlock(vnode_t vp, off_t size, int op, struct flock *fl,
3819     int reclaim, struct ucred *cred, NFSPROC_T *p, void *id, int flags)
3820 {
3821         struct nfscllockowner *lp;
3822         struct nfsclclient *clp;
3823         struct nfsfh *nfhp;
3824         struct nfsrv_descript nfsd, *nd = &nfsd;
3825         struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
3826         u_int64_t off, len;
3827         off_t start, end;
3828         u_int32_t clidrev = 0;
3829         int error = 0, newone = 0, expireret = 0, retrycnt, donelocally;
3830         int callcnt, dorpc;
3831
3832         /*
3833          * Convert the flock structure into a start and end and do POSIX
3834          * bounds checking.
3835          */
3836         switch (fl->l_whence) {
3837         case SEEK_SET:
3838         case SEEK_CUR:
3839                 /*
3840                  * Caller is responsible for adding any necessary offset
3841                  * when SEEK_CUR is used.
3842                  */
3843                 start = fl->l_start;
3844                 off = fl->l_start;
3845                 break;
3846         case SEEK_END:
3847                 start = size + fl->l_start;
3848                 off = size + fl->l_start;
3849                 break;
3850         default:
3851                 return (EINVAL);
3852         }
3853         if (start < 0)
3854                 return (EINVAL);
3855         if (fl->l_len != 0) {
3856                 end = start + fl->l_len - 1;
3857                 if (end < start)
3858                         return (EINVAL);
3859         }
3860
3861         len = fl->l_len;
3862         if (len == 0)
3863                 len = NFS64BITSSET;
3864         retrycnt = 0;
3865         do {
3866             nd->nd_repstat = 0;
3867             if (op == F_GETLK) {
3868                 error = nfscl_getcl(vnode_mount(vp), cred, p, 1, &clp);
3869                 if (error)
3870                         return (error);
3871                 error = nfscl_lockt(vp, clp, off, len, fl, p, id, flags);
3872                 if (!error) {
3873                         clidrev = clp->nfsc_clientidrev;
3874                         error = nfsrpc_lockt(nd, vp, clp, off, len, fl, cred,
3875                             p, id, flags);
3876                 } else if (error == -1) {
3877                         error = 0;
3878                 }
3879                 nfscl_clientrelease(clp);
3880             } else if (op == F_UNLCK && fl->l_type == F_UNLCK) {
3881                 /*
3882                  * We must loop around for all lockowner cases.
3883                  */
3884                 callcnt = 0;
3885                 error = nfscl_getcl(vnode_mount(vp), cred, p, 1, &clp);
3886                 if (error)
3887                         return (error);
3888                 do {
3889                     error = nfscl_relbytelock(vp, off, len, cred, p, callcnt,
3890                         clp, id, flags, &lp, &dorpc);
3891                     /*
3892                      * If it returns a NULL lp, we're done.
3893                      */
3894                     if (lp == NULL) {
3895                         if (callcnt == 0)
3896                             nfscl_clientrelease(clp);
3897                         else
3898                             nfscl_releasealllocks(clp, vp, p, id, flags);
3899                         return (error);
3900                     }
3901                     if (nmp->nm_clp != NULL)
3902                         clidrev = nmp->nm_clp->nfsc_clientidrev;
3903                     else
3904                         clidrev = 0;
3905                     /*
3906                      * If the server doesn't support Posix lock semantics,
3907                      * only allow locks on the entire file, since it won't
3908                      * handle overlapping byte ranges.
3909                      * There might still be a problem when a lock
3910                      * upgrade/downgrade (read<->write) occurs, since the
3911                      * server "might" expect an unlock first?
3912                      */
3913                     if (dorpc && (lp->nfsl_open->nfso_posixlock ||
3914                         (off == 0 && len == NFS64BITSSET))) {
3915                         /*
3916                          * Since the lock records will go away, we must
3917                          * wait for grace and delay here.
3918                          */
3919                         do {
3920                             error = nfsrpc_locku(nd, nmp, lp, off, len,
3921                                 NFSV4LOCKT_READ, cred, p, 0);
3922                             if ((nd->nd_repstat == NFSERR_GRACE ||
3923                                  nd->nd_repstat == NFSERR_DELAY) &&
3924                                 error == 0)
3925                                 (void) nfs_catnap(PZERO, (int)nd->nd_repstat,
3926                                     "nfs_advlock");
3927                         } while ((nd->nd_repstat == NFSERR_GRACE ||
3928                             nd->nd_repstat == NFSERR_DELAY) && error == 0);
3929                     }
3930                     callcnt++;
3931                 } while (error == 0 && nd->nd_repstat == 0);
3932                 nfscl_releasealllocks(clp, vp, p, id, flags);
3933             } else if (op == F_SETLK) {
3934                 error = nfscl_getbytelock(vp, off, len, fl->l_type, cred, p,
3935                     NULL, 0, id, flags, NULL, NULL, &lp, &newone, &donelocally);
3936                 if (error || donelocally) {
3937                         return (error);
3938                 }
3939                 if (nmp->nm_clp != NULL)
3940                         clidrev = nmp->nm_clp->nfsc_clientidrev;
3941                 else
3942                         clidrev = 0;
3943                 nfhp = VTONFS(vp)->n_fhp;
3944                 if (!lp->nfsl_open->nfso_posixlock &&
3945                     (off != 0 || len != NFS64BITSSET)) {
3946                         error = EINVAL;
3947                 } else {
3948                         error = nfsrpc_lock(nd, nmp, vp, nfhp->nfh_fh,
3949                             nfhp->nfh_len, lp, newone, reclaim, off,
3950                             len, fl->l_type, cred, p, 0);
3951                 }
3952                 if (!error)
3953                         error = nd->nd_repstat;
3954                 nfscl_lockrelease(lp, error, newone);
3955             } else {
3956                 error = EINVAL;
3957             }
3958             if (!error)
3959                 error = nd->nd_repstat;
3960             if (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
3961                 error == NFSERR_STALEDONTRECOVER ||
3962                 error == NFSERR_STALECLIENTID || error == NFSERR_DELAY ||
3963                 error == NFSERR_BADSESSION) {
3964                 (void) nfs_catnap(PZERO, error, "nfs_advlock");
3965             } else if ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID)
3966                 && clidrev != 0) {
3967                 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
3968                 retrycnt++;
3969             }
3970         } while (error == NFSERR_GRACE ||
3971             error == NFSERR_STALECLIENTID || error == NFSERR_DELAY ||
3972             error == NFSERR_STALEDONTRECOVER || error == NFSERR_STALESTATEID ||
3973             error == NFSERR_BADSESSION ||
3974             ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
3975              expireret == 0 && clidrev != 0 && retrycnt < 4));
3976         if (error && retrycnt >= 4)
3977                 error = EIO;
3978         return (error);
3979 }
3980
3981 /*
3982  * The lower level routine for the LockT case.
3983  */
3984 int
3985 nfsrpc_lockt(struct nfsrv_descript *nd, vnode_t vp,
3986     struct nfsclclient *clp, u_int64_t off, u_int64_t len, struct flock *fl,
3987     struct ucred *cred, NFSPROC_T *p, void *id, int flags)
3988 {
3989         u_int32_t *tl;
3990         int error, type, size;
3991         uint8_t own[NFSV4CL_LOCKNAMELEN + NFSX_V4FHMAX];
3992         struct nfsnode *np;
3993         struct nfsmount *nmp;
3994         struct nfsclsession *tsep;
3995
3996         nmp = VFSTONFS(vp->v_mount);
3997         NFSCL_REQSTART(nd, NFSPROC_LOCKT, vp);
3998         NFSM_BUILD(tl, u_int32_t *, 7 * NFSX_UNSIGNED);
3999         if (fl->l_type == F_RDLCK)
4000                 *tl++ = txdr_unsigned(NFSV4LOCKT_READ);
4001         else
4002                 *tl++ = txdr_unsigned(NFSV4LOCKT_WRITE);
4003         txdr_hyper(off, tl);
4004         tl += 2;
4005         txdr_hyper(len, tl);
4006         tl += 2;
4007         tsep = nfsmnt_mdssession(nmp);
4008         *tl++ = tsep->nfsess_clientid.lval[0];
4009         *tl = tsep->nfsess_clientid.lval[1];
4010         nfscl_filllockowner(id, own, flags);
4011         np = VTONFS(vp);
4012         NFSBCOPY(np->n_fhp->nfh_fh, &own[NFSV4CL_LOCKNAMELEN],
4013             np->n_fhp->nfh_len);
4014         (void)nfsm_strtom(nd, own, NFSV4CL_LOCKNAMELEN + np->n_fhp->nfh_len);
4015         error = nfscl_request(nd, vp, p, cred, NULL);
4016         if (error)
4017                 return (error);
4018         if (nd->nd_repstat == 0) {
4019                 fl->l_type = F_UNLCK;
4020         } else if (nd->nd_repstat == NFSERR_DENIED) {
4021                 nd->nd_repstat = 0;
4022                 fl->l_whence = SEEK_SET;
4023                 NFSM_DISSECT(tl, u_int32_t *, 8 * NFSX_UNSIGNED);
4024                 fl->l_start = fxdr_hyper(tl);
4025                 tl += 2;
4026                 len = fxdr_hyper(tl);
4027                 tl += 2;
4028                 if (len == NFS64BITSSET)
4029                         fl->l_len = 0;
4030                 else
4031                         fl->l_len = len;
4032                 type = fxdr_unsigned(int, *tl++);
4033                 if (type == NFSV4LOCKT_WRITE)
4034                         fl->l_type = F_WRLCK;
4035                 else
4036                         fl->l_type = F_RDLCK;
4037                 /*
4038                  * XXX For now, I have no idea what to do with the
4039                  * conflicting lock_owner, so I'll just set the pid == 0
4040                  * and skip over the lock_owner.
4041                  */
4042                 fl->l_pid = (pid_t)0;
4043                 tl += 2;
4044                 size = fxdr_unsigned(int, *tl);
4045                 if (size < 0 || size > NFSV4_OPAQUELIMIT)
4046                         error = EBADRPC;
4047                 if (!error)
4048                         error = nfsm_advance(nd, NFSM_RNDUP(size), -1);
4049         } else if (nd->nd_repstat == NFSERR_STALECLIENTID)
4050                 nfscl_initiate_recovery(clp);
4051 nfsmout:
4052         mbuf_freem(nd->nd_mrep);
4053         return (error);
4054 }
4055
4056 /*
4057  * Lower level function that performs the LockU RPC.
4058  */
4059 static int
4060 nfsrpc_locku(struct nfsrv_descript *nd, struct nfsmount *nmp,
4061     struct nfscllockowner *lp, u_int64_t off, u_int64_t len,
4062     u_int32_t type, struct ucred *cred, NFSPROC_T *p, int syscred)
4063 {
4064         u_int32_t *tl;
4065         int error;
4066
4067         nfscl_reqstart(nd, NFSPROC_LOCKU, nmp, lp->nfsl_open->nfso_fh,
4068             lp->nfsl_open->nfso_fhlen, NULL, NULL, 0, 0);
4069         NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID + 6 * NFSX_UNSIGNED);
4070         *tl++ = txdr_unsigned(type);
4071         *tl = txdr_unsigned(lp->nfsl_seqid);
4072         if (nfstest_outofseq &&
4073             (arc4random() % nfstest_outofseq) == 0)
4074                 *tl = txdr_unsigned(lp->nfsl_seqid + 1);
4075         tl++;
4076         if (NFSHASNFSV4N(nmp))
4077                 *tl++ = 0;
4078         else
4079                 *tl++ = lp->nfsl_stateid.seqid;
4080         *tl++ = lp->nfsl_stateid.other[0];
4081         *tl++ = lp->nfsl_stateid.other[1];
4082         *tl++ = lp->nfsl_stateid.other[2];
4083         txdr_hyper(off, tl);
4084         tl += 2;
4085         txdr_hyper(len, tl);
4086         if (syscred)
4087                 nd->nd_flag |= ND_USEGSSNAME;
4088         error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4089             NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4090         NFSCL_INCRSEQID(lp->nfsl_seqid, nd);
4091         if (error)
4092                 return (error);
4093         if (nd->nd_repstat == 0) {
4094                 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID);
4095                 lp->nfsl_stateid.seqid = *tl++;
4096                 lp->nfsl_stateid.other[0] = *tl++;
4097                 lp->nfsl_stateid.other[1] = *tl++;
4098                 lp->nfsl_stateid.other[2] = *tl;
4099         } else if (nd->nd_repstat == NFSERR_STALESTATEID)
4100                 nfscl_initiate_recovery(lp->nfsl_open->nfso_own->nfsow_clp);
4101 nfsmout:
4102         mbuf_freem(nd->nd_mrep);
4103         return (error);
4104 }
4105
4106 /*
4107  * The actual Lock RPC.
4108  */
4109 int
4110 nfsrpc_lock(struct nfsrv_descript *nd, struct nfsmount *nmp, vnode_t vp,
4111     u_int8_t *nfhp, int fhlen, struct nfscllockowner *lp, int newone,
4112     int reclaim, u_int64_t off, u_int64_t len, short type, struct ucred *cred,
4113     NFSPROC_T *p, int syscred)
4114 {
4115         u_int32_t *tl;
4116         int error, size;
4117         uint8_t own[NFSV4CL_LOCKNAMELEN + NFSX_V4FHMAX];
4118         struct nfsclsession *tsep;
4119
4120         nfscl_reqstart(nd, NFSPROC_LOCK, nmp, nfhp, fhlen, NULL, NULL, 0, 0);
4121         NFSM_BUILD(tl, u_int32_t *, 7 * NFSX_UNSIGNED);
4122         if (type == F_RDLCK)
4123                 *tl++ = txdr_unsigned(NFSV4LOCKT_READ);
4124         else
4125                 *tl++ = txdr_unsigned(NFSV4LOCKT_WRITE);
4126         *tl++ = txdr_unsigned(reclaim);
4127         txdr_hyper(off, tl);
4128         tl += 2;
4129         txdr_hyper(len, tl);
4130         tl += 2;
4131         if (newone) {
4132             *tl = newnfs_true;
4133             NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID +
4134                 2 * NFSX_UNSIGNED + NFSX_HYPER);
4135             *tl++ = txdr_unsigned(lp->nfsl_open->nfso_own->nfsow_seqid);
4136             if (NFSHASNFSV4N(nmp))
4137                 *tl++ = 0;
4138             else
4139                 *tl++ = lp->nfsl_open->nfso_stateid.seqid;
4140             *tl++ = lp->nfsl_open->nfso_stateid.other[0];
4141             *tl++ = lp->nfsl_open->nfso_stateid.other[1];
4142             *tl++ = lp->nfsl_open->nfso_stateid.other[2];
4143             *tl++ = txdr_unsigned(lp->nfsl_seqid);
4144             tsep = nfsmnt_mdssession(nmp);
4145             *tl++ = tsep->nfsess_clientid.lval[0];
4146             *tl = tsep->nfsess_clientid.lval[1];
4147             NFSBCOPY(lp->nfsl_owner, own, NFSV4CL_LOCKNAMELEN);
4148             NFSBCOPY(nfhp, &own[NFSV4CL_LOCKNAMELEN], fhlen);
4149             (void)nfsm_strtom(nd, own, NFSV4CL_LOCKNAMELEN + fhlen);
4150         } else {
4151             *tl = newnfs_false;
4152             NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID + NFSX_UNSIGNED);
4153             if (NFSHASNFSV4N(nmp))
4154                 *tl++ = 0;
4155             else
4156                 *tl++ = lp->nfsl_stateid.seqid;
4157             *tl++ = lp->nfsl_stateid.other[0];
4158             *tl++ = lp->nfsl_stateid.other[1];
4159             *tl++ = lp->nfsl_stateid.other[2];
4160             *tl = txdr_unsigned(lp->nfsl_seqid);
4161             if (nfstest_outofseq &&
4162                 (arc4random() % nfstest_outofseq) == 0)
4163                     *tl = txdr_unsigned(lp->nfsl_seqid + 1);
4164         }
4165         if (syscred)
4166                 nd->nd_flag |= ND_USEGSSNAME;
4167         error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, vp, p, cred,
4168             NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4169         if (error)
4170                 return (error);
4171         if (newone)
4172             NFSCL_INCRSEQID(lp->nfsl_open->nfso_own->nfsow_seqid, nd);
4173         NFSCL_INCRSEQID(lp->nfsl_seqid, nd);
4174         if (nd->nd_repstat == 0) {
4175                 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID);
4176                 lp->nfsl_stateid.seqid = *tl++;
4177                 lp->nfsl_stateid.other[0] = *tl++;
4178                 lp->nfsl_stateid.other[1] = *tl++;
4179                 lp->nfsl_stateid.other[2] = *tl;
4180         } else if (nd->nd_repstat == NFSERR_DENIED) {
4181                 NFSM_DISSECT(tl, u_int32_t *, 8 * NFSX_UNSIGNED);
4182                 size = fxdr_unsigned(int, *(tl + 7));
4183                 if (size < 0 || size > NFSV4_OPAQUELIMIT)
4184                         error = EBADRPC;
4185                 if (!error)
4186                         error = nfsm_advance(nd, NFSM_RNDUP(size), -1);
4187         } else if (nd->nd_repstat == NFSERR_STALESTATEID)
4188                 nfscl_initiate_recovery(lp->nfsl_open->nfso_own->nfsow_clp);
4189 nfsmout:
4190         mbuf_freem(nd->nd_mrep);
4191         return (error);
4192 }
4193
4194 /*
4195  * nfs statfs rpc
4196  * (always called with the vp for the mount point)
4197  */
4198 int
4199 nfsrpc_statfs(vnode_t vp, struct nfsstatfs *sbp, struct nfsfsinfo *fsp,
4200     struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp,
4201     void *stuff)
4202 {
4203         u_int32_t *tl = NULL;
4204         struct nfsrv_descript nfsd, *nd = &nfsd;
4205         struct nfsmount *nmp;
4206         nfsattrbit_t attrbits;
4207         int error;
4208
4209         *attrflagp = 0;
4210         nmp = VFSTONFS(vnode_mount(vp));
4211         if (NFSHASNFSV4(nmp)) {
4212                 /*
4213                  * For V4, you actually do a getattr.
4214                  */
4215                 NFSCL_REQSTART(nd, NFSPROC_GETATTR, vp);
4216                 NFSSTATFS_GETATTRBIT(&attrbits);
4217                 (void) nfsrv_putattrbit(nd, &attrbits);
4218                 nd->nd_flag |= ND_USEGSSNAME;
4219                 error = nfscl_request(nd, vp, p, cred, stuff);
4220                 if (error)
4221                         return (error);
4222                 if (nd->nd_repstat == 0) {
4223                         error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0,
4224                             NULL, NULL, sbp, fsp, NULL, 0, NULL, NULL, NULL, p,
4225                             cred);
4226                         if (!error) {
4227                                 nmp->nm_fsid[0] = nap->na_filesid[0];
4228                                 nmp->nm_fsid[1] = nap->na_filesid[1];
4229                                 NFSSETHASSETFSID(nmp);
4230                                 *attrflagp = 1;
4231                         }
4232                 } else {
4233                         error = nd->nd_repstat;
4234                 }
4235                 if (error)
4236                         goto nfsmout;
4237         } else {
4238                 NFSCL_REQSTART(nd, NFSPROC_FSSTAT, vp);
4239                 error = nfscl_request(nd, vp, p, cred, stuff);
4240                 if (error)
4241                         return (error);
4242                 if (nd->nd_flag & ND_NFSV3) {
4243                         error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
4244                         if (error)
4245                                 goto nfsmout;
4246                 }
4247                 if (nd->nd_repstat) {
4248                         error = nd->nd_repstat;
4249                         goto nfsmout;
4250                 }
4251                 NFSM_DISSECT(tl, u_int32_t *,
4252                     NFSX_STATFS(nd->nd_flag & ND_NFSV3));
4253         }
4254         if (NFSHASNFSV3(nmp)) {
4255                 sbp->sf_tbytes = fxdr_hyper(tl); tl += 2;
4256                 sbp->sf_fbytes = fxdr_hyper(tl); tl += 2;
4257                 sbp->sf_abytes = fxdr_hyper(tl); tl += 2;
4258                 sbp->sf_tfiles = fxdr_hyper(tl); tl += 2;
4259                 sbp->sf_ffiles = fxdr_hyper(tl); tl += 2;
4260                 sbp->sf_afiles = fxdr_hyper(tl); tl += 2;
4261                 sbp->sf_invarsec = fxdr_unsigned(u_int32_t, *tl);
4262         } else if (NFSHASNFSV4(nmp) == 0) {
4263                 sbp->sf_tsize = fxdr_unsigned(u_int32_t, *tl++);
4264                 sbp->sf_bsize = fxdr_unsigned(u_int32_t, *tl++);
4265                 sbp->sf_blocks = fxdr_unsigned(u_int32_t, *tl++);
4266                 sbp->sf_bfree = fxdr_unsigned(u_int32_t, *tl++);
4267                 sbp->sf_bavail = fxdr_unsigned(u_int32_t, *tl);
4268         }
4269 nfsmout:
4270         mbuf_freem(nd->nd_mrep);
4271         return (error);
4272 }
4273
4274 /*
4275  * nfs pathconf rpc
4276  */
4277 int
4278 nfsrpc_pathconf(vnode_t vp, struct nfsv3_pathconf *pc,
4279     struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp,
4280     void *stuff)
4281 {
4282         struct nfsrv_descript nfsd, *nd = &nfsd;
4283         struct nfsmount *nmp;
4284         u_int32_t *tl;
4285         nfsattrbit_t attrbits;
4286         int error;
4287
4288         *attrflagp = 0;
4289         nmp = VFSTONFS(vnode_mount(vp));
4290         if (NFSHASNFSV4(nmp)) {
4291                 /*
4292                  * For V4, you actually do a getattr.
4293                  */
4294                 NFSCL_REQSTART(nd, NFSPROC_GETATTR, vp);
4295                 NFSPATHCONF_GETATTRBIT(&attrbits);
4296                 (void) nfsrv_putattrbit(nd, &attrbits);
4297                 nd->nd_flag |= ND_USEGSSNAME;
4298                 error = nfscl_request(nd, vp, p, cred, stuff);
4299                 if (error)
4300                         return (error);
4301                 if (nd->nd_repstat == 0) {
4302                         error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0,
4303                             pc, NULL, NULL, NULL, NULL, 0, NULL, NULL, NULL, p,
4304                             cred);
4305                         if (!error)
4306                                 *attrflagp = 1;
4307                 } else {
4308                         error = nd->nd_repstat;
4309                 }
4310         } else {
4311                 NFSCL_REQSTART(nd, NFSPROC_PATHCONF, vp);
4312                 error = nfscl_request(nd, vp, p, cred, stuff);
4313                 if (error)
4314                         return (error);
4315                 error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
4316                 if (nd->nd_repstat && !error)
4317                         error = nd->nd_repstat;
4318                 if (!error) {
4319                         NFSM_DISSECT(tl, u_int32_t *, NFSX_V3PATHCONF);
4320                         pc->pc_linkmax = fxdr_unsigned(u_int32_t, *tl++);
4321                         pc->pc_namemax = fxdr_unsigned(u_int32_t, *tl++);
4322                         pc->pc_notrunc = fxdr_unsigned(u_int32_t, *tl++);
4323                         pc->pc_chownrestricted =
4324                             fxdr_unsigned(u_int32_t, *tl++);
4325                         pc->pc_caseinsensitive =
4326                             fxdr_unsigned(u_int32_t, *tl++);
4327                         pc->pc_casepreserving = fxdr_unsigned(u_int32_t, *tl);
4328                 }
4329         }
4330 nfsmout:
4331         mbuf_freem(nd->nd_mrep);
4332         return (error);
4333 }
4334
4335 /*
4336  * nfs version 3 fsinfo rpc call
4337  */
4338 int
4339 nfsrpc_fsinfo(vnode_t vp, struct nfsfsinfo *fsp, struct ucred *cred,
4340     NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, void *stuff)
4341 {
4342         u_int32_t *tl;
4343         struct nfsrv_descript nfsd, *nd = &nfsd;
4344         int error;
4345
4346         *attrflagp = 0;
4347         NFSCL_REQSTART(nd, NFSPROC_FSINFO, vp);
4348         error = nfscl_request(nd, vp, p, cred, stuff);
4349         if (error)
4350                 return (error);
4351         error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
4352         if (nd->nd_repstat && !error)
4353                 error = nd->nd_repstat;
4354         if (!error) {
4355                 NFSM_DISSECT(tl, u_int32_t *, NFSX_V3FSINFO);
4356                 fsp->fs_rtmax = fxdr_unsigned(u_int32_t, *tl++);
4357                 fsp->fs_rtpref = fxdr_unsigned(u_int32_t, *tl++);
4358                 fsp->fs_rtmult = fxdr_unsigned(u_int32_t, *tl++);
4359                 fsp->fs_wtmax = fxdr_unsigned(u_int32_t, *tl++);
4360                 fsp->fs_wtpref = fxdr_unsigned(u_int32_t, *tl++);
4361                 fsp->fs_wtmult = fxdr_unsigned(u_int32_t, *tl++);
4362                 fsp->fs_dtpref = fxdr_unsigned(u_int32_t, *tl++);
4363                 fsp->fs_maxfilesize = fxdr_hyper(tl);
4364                 tl += 2;
4365                 fxdr_nfsv3time(tl, &fsp->fs_timedelta);
4366                 tl += 2;
4367                 fsp->fs_properties = fxdr_unsigned(u_int32_t, *tl);
4368         }
4369 nfsmout:
4370         mbuf_freem(nd->nd_mrep);
4371         return (error);
4372 }
4373
4374 /*
4375  * This function performs the Renew RPC.
4376  */
4377 int
4378 nfsrpc_renew(struct nfsclclient *clp, struct nfsclds *dsp, struct ucred *cred,
4379     NFSPROC_T *p)
4380 {
4381         u_int32_t *tl;
4382         struct nfsrv_descript nfsd;
4383         struct nfsrv_descript *nd = &nfsd;
4384         struct nfsmount *nmp;
4385         int error;
4386         struct nfssockreq *nrp;
4387         struct nfsclsession *tsep;
4388
4389         nmp = clp->nfsc_nmp;
4390         if (nmp == NULL)
4391                 return (0);
4392         if (dsp == NULL)
4393                 nfscl_reqstart(nd, NFSPROC_RENEW, nmp, NULL, 0, NULL, NULL, 0,
4394                     0);
4395         else
4396                 nfscl_reqstart(nd, NFSPROC_RENEW, nmp, NULL, 0, NULL,
4397                     &dsp->nfsclds_sess, 0, 0);
4398         if (!NFSHASNFSV4N(nmp)) {
4399                 /* NFSv4.1 just uses a Sequence Op and not a Renew. */
4400                 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
4401                 tsep = nfsmnt_mdssession(nmp);
4402                 *tl++ = tsep->nfsess_clientid.lval[0];
4403                 *tl = tsep->nfsess_clientid.lval[1];
4404         }
4405         nrp = NULL;
4406         if (dsp != NULL)
4407                 nrp = dsp->nfsclds_sockp;
4408         if (nrp == NULL)
4409                 /* If NULL, use the MDS socket. */
4410                 nrp = &nmp->nm_sockreq;
4411         nd->nd_flag |= ND_USEGSSNAME;
4412         if (dsp == NULL)
4413                 error = newnfs_request(nd, nmp, NULL, nrp, NULL, p, cred,
4414                     NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4415         else {
4416                 error = newnfs_request(nd, nmp, NULL, nrp, NULL, p, cred,
4417                     NFS_PROG, NFS_VER4, NULL, 1, NULL, &dsp->nfsclds_sess);
4418                 if (error == ENXIO)
4419                         nfscl_cancelreqs(dsp);
4420         }
4421         if (error)
4422                 return (error);
4423         error = nd->nd_repstat;
4424         mbuf_freem(nd->nd_mrep);
4425         return (error);
4426 }
4427
4428 /*
4429  * This function performs the Releaselockowner RPC.
4430  */
4431 int
4432 nfsrpc_rellockown(struct nfsmount *nmp, struct nfscllockowner *lp,
4433     uint8_t *fh, int fhlen, struct ucred *cred, NFSPROC_T *p)
4434 {
4435         struct nfsrv_descript nfsd, *nd = &nfsd;
4436         u_int32_t *tl;
4437         int error;
4438         uint8_t own[NFSV4CL_LOCKNAMELEN + NFSX_V4FHMAX];
4439         struct nfsclsession *tsep;
4440
4441         if (NFSHASNFSV4N(nmp)) {
4442                 /* For NFSv4.1, do a FreeStateID. */
4443                 nfscl_reqstart(nd, NFSPROC_FREESTATEID, nmp, NULL, 0, NULL,
4444                     NULL, 0, 0);
4445                 nfsm_stateidtom(nd, &lp->nfsl_stateid, NFSSTATEID_PUTSTATEID);
4446         } else {
4447                 nfscl_reqstart(nd, NFSPROC_RELEASELCKOWN, nmp, NULL, 0, NULL,
4448                     NULL, 0, 0);
4449                 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
4450                 tsep = nfsmnt_mdssession(nmp);
4451                 *tl++ = tsep->nfsess_clientid.lval[0];
4452                 *tl = tsep->nfsess_clientid.lval[1];
4453                 NFSBCOPY(lp->nfsl_owner, own, NFSV4CL_LOCKNAMELEN);
4454                 NFSBCOPY(fh, &own[NFSV4CL_LOCKNAMELEN], fhlen);
4455                 (void)nfsm_strtom(nd, own, NFSV4CL_LOCKNAMELEN + fhlen);
4456         }
4457         nd->nd_flag |= ND_USEGSSNAME;
4458         error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4459             NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4460         if (error)
4461                 return (error);
4462         error = nd->nd_repstat;
4463         mbuf_freem(nd->nd_mrep);
4464         return (error);
4465 }
4466
4467 /*
4468  * This function performs the Compound to get the mount pt FH.
4469  */
4470 int
4471 nfsrpc_getdirpath(struct nfsmount *nmp, u_char *dirpath, struct ucred *cred,
4472     NFSPROC_T *p)
4473 {
4474         u_int32_t *tl;
4475         struct nfsrv_descript nfsd;
4476         struct nfsrv_descript *nd = &nfsd;
4477         u_char *cp, *cp2;
4478         int error, cnt, len, setnil;
4479         u_int32_t *opcntp;
4480
4481         nfscl_reqstart(nd, NFSPROC_PUTROOTFH, nmp, NULL, 0, &opcntp, NULL, 0,
4482             0);
4483         cp = dirpath;
4484         cnt = 0;
4485         do {
4486                 setnil = 0;
4487                 while (*cp == '/')
4488                         cp++;
4489                 cp2 = cp;
4490                 while (*cp2 != '\0' && *cp2 != '/')
4491                         cp2++;
4492                 if (*cp2 == '/') {
4493                         setnil = 1;
4494                         *cp2 = '\0';
4495                 }
4496                 if (cp2 != cp) {
4497                         NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
4498                         *tl = txdr_unsigned(NFSV4OP_LOOKUP);
4499                         nfsm_strtom(nd, cp, strlen(cp));
4500                         cnt++;
4501                 }
4502                 if (setnil)
4503                         *cp2++ = '/';
4504                 cp = cp2;
4505         } while (*cp != '\0');
4506         if (NFSHASNFSV4N(nmp))
4507                 /* Has a Sequence Op done by nfscl_reqstart(). */
4508                 *opcntp = txdr_unsigned(3 + cnt);
4509         else
4510                 *opcntp = txdr_unsigned(2 + cnt);
4511         NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
4512         *tl = txdr_unsigned(NFSV4OP_GETFH);
4513         nd->nd_flag |= ND_USEGSSNAME;
4514         error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4515                 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4516         if (error)
4517                 return (error);
4518         if (nd->nd_repstat == 0) {
4519                 NFSM_DISSECT(tl, u_int32_t *, (3 + 2 * cnt) * NFSX_UNSIGNED);
4520                 tl += (2 + 2 * cnt);
4521                 if ((len = fxdr_unsigned(int, *tl)) <= 0 ||
4522                         len > NFSX_FHMAX) {
4523                         nd->nd_repstat = NFSERR_BADXDR;
4524                 } else {
4525                         nd->nd_repstat = nfsrv_mtostr(nd, nmp->nm_fh, len);
4526                         if (nd->nd_repstat == 0)
4527                                 nmp->nm_fhsize = len;
4528                 }
4529         }
4530         error = nd->nd_repstat;
4531 nfsmout:
4532         mbuf_freem(nd->nd_mrep);
4533         return (error);
4534 }
4535
4536 /*
4537  * This function performs the Delegreturn RPC.
4538  */
4539 int
4540 nfsrpc_delegreturn(struct nfscldeleg *dp, struct ucred *cred,
4541     struct nfsmount *nmp, NFSPROC_T *p, int syscred)
4542 {
4543         u_int32_t *tl;
4544         struct nfsrv_descript nfsd;
4545         struct nfsrv_descript *nd = &nfsd;
4546         int error;
4547
4548         nfscl_reqstart(nd, NFSPROC_DELEGRETURN, nmp, dp->nfsdl_fh,
4549             dp->nfsdl_fhlen, NULL, NULL, 0, 0);
4550         NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID);
4551         if (NFSHASNFSV4N(nmp))
4552                 *tl++ = 0;
4553         else
4554                 *tl++ = dp->nfsdl_stateid.seqid;
4555         *tl++ = dp->nfsdl_stateid.other[0];
4556         *tl++ = dp->nfsdl_stateid.other[1];
4557         *tl = dp->nfsdl_stateid.other[2];
4558         if (syscred)
4559                 nd->nd_flag |= ND_USEGSSNAME;
4560         error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4561             NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4562         if (error)
4563                 return (error);
4564         error = nd->nd_repstat;
4565         mbuf_freem(nd->nd_mrep);
4566         return (error);
4567 }
4568
4569 /*
4570  * nfs getacl call.
4571  */
4572 int
4573 nfsrpc_getacl(vnode_t vp, struct ucred *cred, NFSPROC_T *p,
4574     struct acl *aclp, void *stuff)
4575 {
4576         struct nfsrv_descript nfsd, *nd = &nfsd;
4577         int error;
4578         nfsattrbit_t attrbits;
4579         struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
4580         
4581         if (nfsrv_useacl == 0 || !NFSHASNFSV4(nmp))
4582                 return (EOPNOTSUPP);
4583         NFSCL_REQSTART(nd, NFSPROC_GETACL, vp);
4584         NFSZERO_ATTRBIT(&attrbits);
4585         NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_ACL);
4586         (void) nfsrv_putattrbit(nd, &attrbits);
4587         error = nfscl_request(nd, vp, p, cred, stuff);
4588         if (error)
4589                 return (error);
4590         if (!nd->nd_repstat)
4591                 error = nfsv4_loadattr(nd, vp, NULL, NULL, NULL, 0, NULL,
4592                     NULL, NULL, NULL, aclp, 0, NULL, NULL, NULL, p, cred);
4593         else
4594                 error = nd->nd_repstat;
4595         mbuf_freem(nd->nd_mrep);
4596         return (error);
4597 }
4598
4599 /*
4600  * nfs setacl call.
4601  */
4602 int
4603 nfsrpc_setacl(vnode_t vp, struct ucred *cred, NFSPROC_T *p,
4604     struct acl *aclp, void *stuff)
4605 {
4606         int error;
4607         struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
4608         
4609         if (nfsrv_useacl == 0 || !NFSHASNFSV4(nmp))
4610                 return (EOPNOTSUPP);
4611         error = nfsrpc_setattr(vp, NULL, aclp, cred, p, NULL, NULL, stuff);
4612         return (error);
4613 }
4614
4615 /*
4616  * nfs setacl call.
4617  */
4618 static int
4619 nfsrpc_setaclrpc(vnode_t vp, struct ucred *cred, NFSPROC_T *p,
4620     struct acl *aclp, nfsv4stateid_t *stateidp, void *stuff)
4621 {
4622         struct nfsrv_descript nfsd, *nd = &nfsd;
4623         int error;
4624         nfsattrbit_t attrbits;
4625         struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
4626         
4627         if (!NFSHASNFSV4(nmp))
4628                 return (EOPNOTSUPP);
4629         NFSCL_REQSTART(nd, NFSPROC_SETACL, vp);
4630         nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID);
4631         NFSZERO_ATTRBIT(&attrbits);
4632         NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_ACL);
4633         (void) nfsv4_fillattr(nd, vnode_mount(vp), vp, aclp, NULL, NULL, 0,
4634             &attrbits, NULL, NULL, 0, 0, 0, 0, (uint64_t)0, NULL);
4635         error = nfscl_request(nd, vp, p, cred, stuff);
4636         if (error)
4637                 return (error);
4638         /* Don't care about the pre/postop attributes */
4639         mbuf_freem(nd->nd_mrep);
4640         return (nd->nd_repstat);
4641 }
4642
4643 /*
4644  * Do the NFSv4.1 Exchange ID.
4645  */
4646 int
4647 nfsrpc_exchangeid(struct nfsmount *nmp, struct nfsclclient *clp,
4648     struct nfssockreq *nrp, uint32_t exchflags, struct nfsclds **dspp,
4649     struct ucred *cred, NFSPROC_T *p)
4650 {
4651         uint32_t *tl, v41flags;
4652         struct nfsrv_descript nfsd;
4653         struct nfsrv_descript *nd = &nfsd;
4654         struct nfsclds *dsp;
4655         struct timespec verstime;
4656         int error, len;
4657
4658         *dspp = NULL;
4659         nfscl_reqstart(nd, NFSPROC_EXCHANGEID, nmp, NULL, 0, NULL, NULL, 0, 0);
4660         NFSM_BUILD(tl, uint32_t *, 2 * NFSX_UNSIGNED);
4661         *tl++ = txdr_unsigned(nfsboottime.tv_sec);      /* Client owner */
4662         *tl = txdr_unsigned(clp->nfsc_rev);
4663         (void) nfsm_strtom(nd, clp->nfsc_id, clp->nfsc_idlen);
4664
4665         NFSM_BUILD(tl, uint32_t *, 3 * NFSX_UNSIGNED);
4666         *tl++ = txdr_unsigned(exchflags);
4667         *tl++ = txdr_unsigned(NFSV4EXCH_SP4NONE);
4668
4669         /* Set the implementation id4 */
4670         *tl = txdr_unsigned(1);
4671         (void) nfsm_strtom(nd, "freebsd.org", strlen("freebsd.org"));
4672         (void) nfsm_strtom(nd, version, strlen(version));
4673         NFSM_BUILD(tl, uint32_t *, NFSX_V4TIME);
4674         verstime.tv_sec = 1293840000;           /* Jan 1, 2011 */
4675         verstime.tv_nsec = 0;
4676         txdr_nfsv4time(&verstime, tl);
4677         nd->nd_flag |= ND_USEGSSNAME;
4678         error = newnfs_request(nd, nmp, NULL, nrp, NULL, p, cred,
4679             NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4680         NFSCL_DEBUG(1, "exchangeid err=%d reps=%d\n", error,
4681             (int)nd->nd_repstat);
4682         if (error != 0)
4683                 return (error);
4684         if (nd->nd_repstat == 0) {
4685                 NFSM_DISSECT(tl, uint32_t *, 6 * NFSX_UNSIGNED + NFSX_HYPER);
4686                 len = fxdr_unsigned(int, *(tl + 7));
4687                 if (len < 0 || len > NFSV4_OPAQUELIMIT) {
4688                         error = NFSERR_BADXDR;
4689                         goto nfsmout;
4690                 }
4691                 dsp = malloc(sizeof(struct nfsclds) + len + 1, M_NFSCLDS,
4692                     M_WAITOK | M_ZERO);
4693                 dsp->nfsclds_expire = NFSD_MONOSEC + clp->nfsc_renew;
4694                 dsp->nfsclds_servownlen = len;
4695                 dsp->nfsclds_sess.nfsess_clientid.lval[0] = *tl++;
4696                 dsp->nfsclds_sess.nfsess_clientid.lval[1] = *tl++;
4697                 dsp->nfsclds_sess.nfsess_sequenceid =
4698                     fxdr_unsigned(uint32_t, *tl++);
4699                 v41flags = fxdr_unsigned(uint32_t, *tl);
4700                 if ((v41flags & NFSV4EXCH_USEPNFSMDS) != 0 &&
4701                     NFSHASPNFSOPT(nmp)) {
4702                         NFSCL_DEBUG(1, "set PNFS\n");
4703                         NFSLOCKMNT(nmp);
4704                         nmp->nm_state |= NFSSTA_PNFS;
4705                         NFSUNLOCKMNT(nmp);
4706                         dsp->nfsclds_flags |= NFSCLDS_MDS;
4707                 }
4708                 if ((v41flags & NFSV4EXCH_USEPNFSDS) != 0)
4709                         dsp->nfsclds_flags |= NFSCLDS_DS;
4710                 if (len > 0)
4711                         nd->nd_repstat = nfsrv_mtostr(nd,
4712                             dsp->nfsclds_serverown, len);
4713                 if (nd->nd_repstat == 0) {
4714                         mtx_init(&dsp->nfsclds_mtx, "nfsds", NULL, MTX_DEF);
4715                         mtx_init(&dsp->nfsclds_sess.nfsess_mtx, "nfssession",
4716                             NULL, MTX_DEF);
4717                         nfscl_initsessionslots(&dsp->nfsclds_sess);
4718                         *dspp = dsp;
4719                 } else
4720                         free(dsp, M_NFSCLDS);
4721         }
4722         error = nd->nd_repstat;
4723 nfsmout:
4724         mbuf_freem(nd->nd_mrep);
4725         return (error);
4726 }
4727
4728 /*
4729  * Do the NFSv4.1 Create Session.
4730  */
4731 int
4732 nfsrpc_createsession(struct nfsmount *nmp, struct nfsclsession *sep,
4733     struct nfssockreq *nrp, uint32_t sequenceid, int mds, struct ucred *cred,
4734     NFSPROC_T *p)
4735 {
4736         uint32_t crflags, maxval, *tl;
4737         struct nfsrv_descript nfsd;
4738         struct nfsrv_descript *nd = &nfsd;
4739         int error, irdcnt;
4740
4741         /* Make sure nm_rsize, nm_wsize is set. */
4742         if (nmp->nm_rsize > NFS_MAXBSIZE || nmp->nm_rsize == 0)
4743                 nmp->nm_rsize = NFS_MAXBSIZE;
4744         if (nmp->nm_wsize > NFS_MAXBSIZE || nmp->nm_wsize == 0)
4745                 nmp->nm_wsize = NFS_MAXBSIZE;
4746         nfscl_reqstart(nd, NFSPROC_CREATESESSION, nmp, NULL, 0, NULL, NULL, 0,
4747             0);
4748         NFSM_BUILD(tl, uint32_t *, 4 * NFSX_UNSIGNED);
4749         *tl++ = sep->nfsess_clientid.lval[0];
4750         *tl++ = sep->nfsess_clientid.lval[1];
4751         *tl++ = txdr_unsigned(sequenceid);
4752         crflags = (NFSMNT_RDONLY(nmp->nm_mountp) ? 0 : NFSV4CRSESS_PERSIST);
4753         if (nfscl_enablecallb != 0 && nfs_numnfscbd > 0 && mds != 0)
4754                 crflags |= NFSV4CRSESS_CONNBACKCHAN;
4755         *tl = txdr_unsigned(crflags);
4756
4757         /* Fill in fore channel attributes. */
4758         NFSM_BUILD(tl, uint32_t *, 7 * NFSX_UNSIGNED);
4759         *tl++ = 0;                              /* Header pad size */
4760         *tl++ = txdr_unsigned(nmp->nm_wsize + NFS_MAXXDR);/* Max request size */
4761         *tl++ = txdr_unsigned(nmp->nm_rsize + NFS_MAXXDR);/* Max reply size */
4762         *tl++ = txdr_unsigned(4096);            /* Max response size cached */
4763         *tl++ = txdr_unsigned(20);              /* Max operations */
4764         *tl++ = txdr_unsigned(64);              /* Max slots */
4765         *tl = 0;                                /* No rdma ird */
4766
4767         /* Fill in back channel attributes. */
4768         NFSM_BUILD(tl, uint32_t *, 7 * NFSX_UNSIGNED);
4769         *tl++ = 0;                              /* Header pad size */
4770         *tl++ = txdr_unsigned(10000);           /* Max request size */
4771         *tl++ = txdr_unsigned(10000);           /* Max response size */
4772         *tl++ = txdr_unsigned(4096);            /* Max response size cached */
4773         *tl++ = txdr_unsigned(4);               /* Max operations */
4774         *tl++ = txdr_unsigned(NFSV4_CBSLOTS);   /* Max slots */
4775         *tl = 0;                                /* No rdma ird */
4776
4777         NFSM_BUILD(tl, uint32_t *, 8 * NFSX_UNSIGNED);
4778         *tl++ = txdr_unsigned(NFS_CALLBCKPROG); /* Call back prog # */
4779
4780         /* Allow AUTH_SYS callbacks as uid, gid == 0. */
4781         *tl++ = txdr_unsigned(1);               /* Auth_sys only */
4782         *tl++ = txdr_unsigned(AUTH_SYS);        /* AUTH_SYS type */
4783         *tl++ = txdr_unsigned(nfsboottime.tv_sec); /* time stamp */
4784         *tl++ = 0;                              /* Null machine name */
4785         *tl++ = 0;                              /* Uid == 0 */
4786         *tl++ = 0;                              /* Gid == 0 */
4787         *tl = 0;                                /* No additional gids */
4788         nd->nd_flag |= ND_USEGSSNAME;
4789         error = newnfs_request(nd, nmp, NULL, nrp, NULL, p, cred, NFS_PROG,
4790             NFS_VER4, NULL, 1, NULL, NULL);
4791         if (error != 0)
4792                 return (error);
4793         if (nd->nd_repstat == 0) {
4794                 NFSM_DISSECT(tl, uint32_t *, NFSX_V4SESSIONID +
4795                     2 * NFSX_UNSIGNED);
4796                 bcopy(tl, sep->nfsess_sessionid, NFSX_V4SESSIONID);
4797                 tl += NFSX_V4SESSIONID / NFSX_UNSIGNED;
4798                 sep->nfsess_sequenceid = fxdr_unsigned(uint32_t, *tl++);
4799                 crflags = fxdr_unsigned(uint32_t, *tl);
4800                 if ((crflags & NFSV4CRSESS_PERSIST) != 0 && mds != 0) {
4801                         NFSLOCKMNT(nmp);
4802                         nmp->nm_state |= NFSSTA_SESSPERSIST;
4803                         NFSUNLOCKMNT(nmp);
4804                 }
4805
4806                 /* Get the fore channel slot count. */
4807                 NFSM_DISSECT(tl, uint32_t *, 7 * NFSX_UNSIGNED);
4808                 tl++;                   /* Skip the header pad size. */
4809
4810                 /* Make sure nm_wsize is small enough. */
4811                 maxval = fxdr_unsigned(uint32_t, *tl++);
4812                 while (maxval < nmp->nm_wsize + NFS_MAXXDR) {
4813                         if (nmp->nm_wsize > 8096)
4814                                 nmp->nm_wsize /= 2;
4815                         else
4816                                 break;
4817                 }
4818
4819                 /* Make sure nm_rsize is small enough. */
4820                 maxval = fxdr_unsigned(uint32_t, *tl++);
4821                 while (maxval < nmp->nm_rsize + NFS_MAXXDR) {
4822                         if (nmp->nm_rsize > 8096)
4823                                 nmp->nm_rsize /= 2;
4824                         else
4825                                 break;
4826                 }
4827
4828                 sep->nfsess_maxcache = fxdr_unsigned(int, *tl++);
4829                 tl++;
4830                 sep->nfsess_foreslots = fxdr_unsigned(uint16_t, *tl++);
4831                 NFSCL_DEBUG(4, "fore slots=%d\n", (int)sep->nfsess_foreslots);
4832                 irdcnt = fxdr_unsigned(int, *tl);
4833                 if (irdcnt > 0)
4834                         NFSM_DISSECT(tl, uint32_t *, irdcnt * NFSX_UNSIGNED);
4835
4836                 /* and the back channel slot count. */
4837                 NFSM_DISSECT(tl, uint32_t *, 7 * NFSX_UNSIGNED);
4838                 tl += 5;
4839                 sep->nfsess_backslots = fxdr_unsigned(uint16_t, *tl);
4840                 NFSCL_DEBUG(4, "back slots=%d\n", (int)sep->nfsess_backslots);
4841         }
4842         error = nd->nd_repstat;
4843 nfsmout:
4844         mbuf_freem(nd->nd_mrep);
4845         return (error);
4846 }
4847
4848 /*
4849  * Do the NFSv4.1 Destroy Session.
4850  */
4851 int
4852 nfsrpc_destroysession(struct nfsmount *nmp, struct nfsclclient *clp,
4853     struct ucred *cred, NFSPROC_T *p)
4854 {
4855         uint32_t *tl;
4856         struct nfsrv_descript nfsd;
4857         struct nfsrv_descript *nd = &nfsd;
4858         int error;
4859         struct nfsclsession *tsep;
4860
4861         nfscl_reqstart(nd, NFSPROC_DESTROYSESSION, nmp, NULL, 0, NULL, NULL, 0,
4862             0);
4863         NFSM_BUILD(tl, uint32_t *, NFSX_V4SESSIONID);
4864         tsep = nfsmnt_mdssession(nmp);
4865         bcopy(tsep->nfsess_sessionid, tl, NFSX_V4SESSIONID);
4866         nd->nd_flag |= ND_USEGSSNAME;
4867         error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4868             NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4869         if (error != 0)
4870                 return (error);
4871         error = nd->nd_repstat;
4872         mbuf_freem(nd->nd_mrep);
4873         return (error);
4874 }
4875
4876 /*
4877  * Do the NFSv4.1 Destroy Client.
4878  */
4879 int
4880 nfsrpc_destroyclient(struct nfsmount *nmp, struct nfsclclient *clp,
4881     struct ucred *cred, NFSPROC_T *p)
4882 {
4883         uint32_t *tl;
4884         struct nfsrv_descript nfsd;
4885         struct nfsrv_descript *nd = &nfsd;
4886         int error;
4887         struct nfsclsession *tsep;
4888
4889         nfscl_reqstart(nd, NFSPROC_DESTROYCLIENT, nmp, NULL, 0, NULL, NULL, 0,
4890             0);
4891         NFSM_BUILD(tl, uint32_t *, 2 * NFSX_UNSIGNED);
4892         tsep = nfsmnt_mdssession(nmp);
4893         *tl++ = tsep->nfsess_clientid.lval[0];
4894         *tl = tsep->nfsess_clientid.lval[1];
4895         nd->nd_flag |= ND_USEGSSNAME;
4896         error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4897             NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4898         if (error != 0)
4899                 return (error);
4900         error = nd->nd_repstat;
4901         mbuf_freem(nd->nd_mrep);
4902         return (error);
4903 }
4904
4905 /*
4906  * Do the NFSv4.1 LayoutGet.
4907  */
4908 static int
4909 nfsrpc_layoutget(struct nfsmount *nmp, uint8_t *fhp, int fhlen, int iomode,
4910     uint64_t offset, uint64_t len, uint64_t minlen, int layouttype,
4911     int layoutlen, nfsv4stateid_t *stateidp, int *retonclosep,
4912     struct nfsclflayouthead *flhp, struct ucred *cred, NFSPROC_T *p,
4913     void *stuff)
4914 {
4915         struct nfsrv_descript nfsd, *nd = &nfsd;
4916         int error;
4917
4918         nfscl_reqstart(nd, NFSPROC_LAYOUTGET, nmp, fhp, fhlen, NULL, NULL, 0,
4919             0);
4920         nfsrv_setuplayoutget(nd, iomode, offset, len, minlen, stateidp,
4921             layouttype, layoutlen, 0);
4922         nd->nd_flag |= ND_USEGSSNAME;
4923         error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4924             NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4925         NFSCL_DEBUG(4, "layget err=%d st=%d\n", error, nd->nd_repstat);
4926         if (error != 0)
4927                 return (error);
4928         if (nd->nd_repstat == 0)
4929                 error = nfsrv_parselayoutget(nd, stateidp, retonclosep, flhp);
4930         if (error == 0 && nd->nd_repstat != 0)
4931                 error = nd->nd_repstat;
4932         mbuf_freem(nd->nd_mrep);
4933         return (error);
4934 }
4935
4936 /*
4937  * Do the NFSv4.1 Get Device Info.
4938  */
4939 int
4940 nfsrpc_getdeviceinfo(struct nfsmount *nmp, uint8_t *deviceid, int layouttype,
4941     uint32_t *notifybitsp, struct nfscldevinfo **ndip, struct ucred *cred,
4942     NFSPROC_T *p)
4943 {
4944         uint32_t cnt, *tl, vers, minorvers;
4945         struct nfsrv_descript nfsd;
4946         struct nfsrv_descript *nd = &nfsd;
4947         struct sockaddr_in sin, ssin;
4948         struct sockaddr_in6 sin6, ssin6;
4949         struct nfsclds *dsp = NULL, **dspp, **gotdspp;
4950         struct nfscldevinfo *ndi;
4951         int addrcnt = 0, bitcnt, error, gotvers, i, isudp, j, stripecnt;
4952         uint8_t stripeindex;
4953         sa_family_t af, safilled;
4954
4955         *ndip = NULL;
4956         ndi = NULL;
4957         gotdspp = NULL;
4958         nfscl_reqstart(nd, NFSPROC_GETDEVICEINFO, nmp, NULL, 0, NULL, NULL, 0,
4959             0);
4960         NFSM_BUILD(tl, uint32_t *, NFSX_V4DEVICEID + 3 * NFSX_UNSIGNED);
4961         NFSBCOPY(deviceid, tl, NFSX_V4DEVICEID);
4962         tl += (NFSX_V4DEVICEID / NFSX_UNSIGNED);
4963         *tl++ = txdr_unsigned(layouttype);
4964         *tl++ = txdr_unsigned(100000);
4965         if (notifybitsp != NULL && *notifybitsp != 0) {
4966                 *tl = txdr_unsigned(1);         /* One word of bits. */
4967                 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
4968                 *tl = txdr_unsigned(*notifybitsp);
4969         } else
4970                 *tl = txdr_unsigned(0);
4971         nd->nd_flag |= ND_USEGSSNAME;
4972         error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4973             NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4974         if (error != 0)
4975                 return (error);
4976         if (nd->nd_repstat == 0) {
4977                 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
4978                 if (layouttype != fxdr_unsigned(int, *tl))
4979                         printf("EEK! devinfo layout type not same!\n");
4980                 if (layouttype == NFSLAYOUT_NFSV4_1_FILES) {
4981                         NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
4982                         stripecnt = fxdr_unsigned(int, *tl);
4983                         NFSCL_DEBUG(4, "stripecnt=%d\n", stripecnt);
4984                         if (stripecnt < 1 || stripecnt > 4096) {
4985                                 printf("pNFS File layout devinfo stripecnt %d:"
4986                                     " out of range\n", stripecnt);
4987                                 error = NFSERR_BADXDR;
4988                                 goto nfsmout;
4989                         }
4990                         NFSM_DISSECT(tl, uint32_t *, (stripecnt + 1) *
4991                             NFSX_UNSIGNED);
4992                         addrcnt = fxdr_unsigned(int, *(tl + stripecnt));
4993                         NFSCL_DEBUG(4, "addrcnt=%d\n", addrcnt);
4994                         if (addrcnt < 1 || addrcnt > 128) {
4995                                 printf("NFS devinfo addrcnt %d: out of range\n",
4996                                     addrcnt);
4997                                 error = NFSERR_BADXDR;
4998                                 goto nfsmout;
4999                         }
5000         
5001                         /*
5002                          * Now we know how many stripe indices and addresses, so
5003                          * we can allocate the structure the correct size.
5004                          */
5005                         i = (stripecnt * sizeof(uint8_t)) /
5006                             sizeof(struct nfsclds *) + 1;
5007                         NFSCL_DEBUG(4, "stripeindices=%d\n", i);
5008                         ndi = malloc(sizeof(*ndi) + (addrcnt + i) *
5009                             sizeof(struct nfsclds *), M_NFSDEVINFO, M_WAITOK |
5010                             M_ZERO);
5011                         NFSBCOPY(deviceid, ndi->nfsdi_deviceid,
5012                             NFSX_V4DEVICEID);
5013                         ndi->nfsdi_refcnt = 0;
5014                         ndi->nfsdi_flags = NFSDI_FILELAYOUT;
5015                         ndi->nfsdi_stripecnt = stripecnt;
5016                         ndi->nfsdi_addrcnt = addrcnt;
5017                         /* Fill in the stripe indices. */
5018                         for (i = 0; i < stripecnt; i++) {
5019                                 stripeindex = fxdr_unsigned(uint8_t, *tl++);
5020                                 NFSCL_DEBUG(4, "stripeind=%d\n", stripeindex);
5021                                 if (stripeindex >= addrcnt) {
5022                                         printf("pNFS File Layout devinfo"
5023                                             " stripeindex %d: too big\n",
5024                                             (int)stripeindex);
5025                                         error = NFSERR_BADXDR;
5026                                         goto nfsmout;
5027                                 }
5028                                 nfsfldi_setstripeindex(ndi, i, stripeindex);
5029                         }
5030                 } else if (layouttype == NFSLAYOUT_FLEXFILE) {
5031                         /* For Flex File, we only get one address list. */
5032                         ndi = malloc(sizeof(*ndi) + sizeof(struct nfsclds *),
5033                             M_NFSDEVINFO, M_WAITOK | M_ZERO);
5034                         NFSBCOPY(deviceid, ndi->nfsdi_deviceid,
5035                             NFSX_V4DEVICEID);
5036                         ndi->nfsdi_refcnt = 0;
5037                         ndi->nfsdi_flags = NFSDI_FLEXFILE;
5038                         addrcnt = ndi->nfsdi_addrcnt = 1;
5039                 }
5040
5041                 /* Now, dissect the server address(es). */
5042                 safilled = AF_UNSPEC;
5043                 for (i = 0; i < addrcnt; i++) {
5044                         NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
5045                         cnt = fxdr_unsigned(uint32_t, *tl);
5046                         if (cnt == 0) {
5047                                 printf("NFS devinfo 0 len addrlist\n");
5048                                 error = NFSERR_BADXDR;
5049                                 goto nfsmout;
5050                         }
5051                         dspp = nfsfldi_addr(ndi, i);
5052                         safilled = AF_UNSPEC;
5053                         for (j = 0; j < cnt; j++) {
5054                                 error = nfsv4_getipaddr(nd, &sin, &sin6, &af,
5055                                     &isudp);
5056                                 if (error != 0 && error != EPERM) {
5057                                         error = NFSERR_BADXDR;
5058                                         goto nfsmout;
5059                                 }
5060                                 if (error == 0 && isudp == 0) {
5061                                         /*
5062                                          * The priority is:
5063                                          * - Same address family.
5064                                          * Save the address and dspp, so that
5065                                          * the connection can be done after
5066                                          * parsing is complete.
5067                                          */
5068                                         if (safilled == AF_UNSPEC ||
5069                                             (af == nmp->nm_nam->sa_family &&
5070                                              safilled != nmp->nm_nam->sa_family)
5071                                            ) {
5072                                                 if (af == AF_INET)
5073                                                         ssin = sin;
5074                                                 else
5075                                                         ssin6 = sin6;
5076                                                 safilled = af;
5077                                                 gotdspp = dspp;
5078                                         }
5079                                 }
5080                         }
5081                 }
5082
5083                 gotvers = NFS_VER4;     /* Always NFSv4 for File Layout. */
5084                 /* For Flex File, we will take one of the versions to use. */
5085                 if (layouttype == NFSLAYOUT_FLEXFILE) {
5086                         NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
5087                         j = fxdr_unsigned(int, *tl);
5088                         if (j < 1 || j > NFSDEV_MAXVERS) {
5089                                 printf("pNFS: too many versions\n");
5090                                 error = NFSERR_BADXDR;
5091                                 goto nfsmout;
5092                         }
5093                         gotvers = 0;
5094                         for (i = 0; i < j; i++) {
5095                                 NFSM_DISSECT(tl, uint32_t *, 5 * NFSX_UNSIGNED);
5096                                 vers = fxdr_unsigned(uint32_t, *tl++);
5097                                 minorvers = fxdr_unsigned(uint32_t, *tl++);
5098                                 if ((vers == NFS_VER4 && minorvers ==
5099                                     NFSV41_MINORVERSION) || (vers == NFS_VER3 &&
5100                                     gotvers == 0)) {
5101                                         gotvers = vers;
5102                                         /* We'll take this one. */
5103                                         ndi->nfsdi_versindex = i;
5104                                         ndi->nfsdi_vers = vers;
5105                                         ndi->nfsdi_minorvers = minorvers;
5106                                         ndi->nfsdi_rsize = fxdr_unsigned(
5107                                             uint32_t, *tl++);
5108                                         ndi->nfsdi_wsize = fxdr_unsigned(
5109                                             uint32_t, *tl++);
5110                                         if (*tl == newnfs_true)
5111                                                 ndi->nfsdi_flags |=
5112                                                     NFSDI_TIGHTCOUPLED;
5113                                         else
5114                                                 ndi->nfsdi_flags &=
5115                                                     ~NFSDI_TIGHTCOUPLED;
5116                                 }
5117                         }
5118                         if (gotvers == 0) {
5119                                 printf("pNFS: no NFSv3 or NFSv4.1\n");
5120                                 error = NFSERR_BADXDR;
5121                                 goto nfsmout;
5122                         }
5123                 }
5124
5125                 /* And the notify bits. */
5126                 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
5127                 bitcnt = fxdr_unsigned(int, *tl);
5128                 if (bitcnt > 0) {
5129                         NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
5130                         if (notifybitsp != NULL)
5131                                 *notifybitsp =
5132                                     fxdr_unsigned(uint32_t, *tl);
5133                 }
5134                 if (safilled != AF_UNSPEC) {
5135                         KASSERT(ndi != NULL, ("ndi is NULL"));
5136                         *ndip = ndi;
5137                 } else
5138                         error = EPERM;
5139                 if (error == 0) {
5140                         /*
5141                          * Now we can do a TCP connection for the correct
5142                          * NFS version and IP address.
5143                          */
5144                         error = nfsrpc_fillsa(nmp, &ssin, &ssin6, safilled,
5145                             gotvers, &dsp, p);
5146                 }
5147                 if (error == 0) {
5148                         KASSERT(gotdspp != NULL, ("gotdspp is NULL"));
5149                         *gotdspp = dsp;
5150                 }
5151         }
5152         if (nd->nd_repstat != 0 && error == 0)
5153                 error = nd->nd_repstat;
5154 nfsmout:
5155         if (error != 0 && ndi != NULL)
5156                 nfscl_freedevinfo(ndi);
5157         mbuf_freem(nd->nd_mrep);
5158         return (error);
5159 }
5160
5161 /*
5162  * Do the NFSv4.1 LayoutCommit.
5163  */
5164 int
5165 nfsrpc_layoutcommit(struct nfsmount *nmp, uint8_t *fh, int fhlen, int reclaim,
5166     uint64_t off, uint64_t len, uint64_t lastbyte, nfsv4stateid_t *stateidp,
5167     int layouttype, struct ucred *cred, NFSPROC_T *p, void *stuff)
5168 {
5169         uint32_t *tl;
5170         struct nfsrv_descript nfsd, *nd = &nfsd;
5171         int error;
5172
5173         nfscl_reqstart(nd, NFSPROC_LAYOUTCOMMIT, nmp, fh, fhlen, NULL, NULL,
5174             0, 0);
5175         NFSM_BUILD(tl, uint32_t *, 5 * NFSX_UNSIGNED + 3 * NFSX_HYPER +
5176             NFSX_STATEID);
5177         txdr_hyper(off, tl);
5178         tl += 2;
5179         txdr_hyper(len, tl);
5180         tl += 2;
5181         if (reclaim != 0)
5182                 *tl++ = newnfs_true;
5183         else
5184                 *tl++ = newnfs_false;
5185         *tl++ = txdr_unsigned(stateidp->seqid);
5186         *tl++ = stateidp->other[0];
5187         *tl++ = stateidp->other[1];
5188         *tl++ = stateidp->other[2];
5189         *tl++ = newnfs_true;
5190         if (lastbyte < off)
5191                 lastbyte = off;
5192         else if (lastbyte >= (off + len))
5193                 lastbyte = off + len - 1;
5194         txdr_hyper(lastbyte, tl);
5195         tl += 2;
5196         *tl++ = newnfs_false;
5197         *tl++ = txdr_unsigned(layouttype);
5198         /* All supported layouts are 0 length. */
5199         *tl = txdr_unsigned(0);
5200         nd->nd_flag |= ND_USEGSSNAME;
5201         error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
5202             NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
5203         if (error != 0)
5204                 return (error);
5205         error = nd->nd_repstat;
5206         mbuf_freem(nd->nd_mrep);
5207         return (error);
5208 }
5209
5210 /*
5211  * Do the NFSv4.1 LayoutReturn.
5212  */
5213 int
5214 nfsrpc_layoutreturn(struct nfsmount *nmp, uint8_t *fh, int fhlen, int reclaim,
5215     int layouttype, uint32_t iomode, int layoutreturn, uint64_t offset,
5216     uint64_t len, nfsv4stateid_t *stateidp, struct ucred *cred, NFSPROC_T *p,
5217     uint32_t stat, uint32_t op, char *devid)
5218 {
5219         uint32_t *tl;
5220         struct nfsrv_descript nfsd, *nd = &nfsd;
5221         uint64_t tu64;
5222         int error;
5223
5224         nfscl_reqstart(nd, NFSPROC_LAYOUTRETURN, nmp, fh, fhlen, NULL, NULL,
5225             0, 0);
5226         NFSM_BUILD(tl, uint32_t *, 4 * NFSX_UNSIGNED);
5227         if (reclaim != 0)
5228                 *tl++ = newnfs_true;
5229         else
5230                 *tl++ = newnfs_false;
5231         *tl++ = txdr_unsigned(layouttype);
5232         *tl++ = txdr_unsigned(iomode);
5233         *tl = txdr_unsigned(layoutreturn);
5234         if (layoutreturn == NFSLAYOUTRETURN_FILE) {
5235                 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_HYPER + NFSX_STATEID +
5236                     NFSX_UNSIGNED);
5237                 txdr_hyper(offset, tl);
5238                 tl += 2;
5239                 txdr_hyper(len, tl);
5240                 tl += 2;
5241                 NFSCL_DEBUG(4, "layoutret stseq=%d\n", (int)stateidp->seqid);
5242                 *tl++ = txdr_unsigned(stateidp->seqid);
5243                 *tl++ = stateidp->other[0];
5244                 *tl++ = stateidp->other[1];
5245                 *tl++ = stateidp->other[2];
5246                 if (layouttype == NFSLAYOUT_NFSV4_1_FILES)
5247                         *tl = txdr_unsigned(0);
5248                 else if (layouttype == NFSLAYOUT_FLEXFILE) {
5249                         if (stat != 0) {
5250                                 *tl = txdr_unsigned(2 * NFSX_HYPER +
5251                                     NFSX_STATEID + NFSX_V4DEVICEID + 5 *
5252                                     NFSX_UNSIGNED);
5253                                 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_HYPER +
5254                                     NFSX_STATEID + NFSX_V4DEVICEID + 5 *
5255                                     NFSX_UNSIGNED);
5256                                 *tl++ = txdr_unsigned(1);       /* One error. */
5257                                 tu64 = 0;                       /* Offset. */
5258                                 txdr_hyper(tu64, tl); tl += 2;
5259                                 tu64 = UINT64_MAX;              /* Length. */
5260                                 txdr_hyper(tu64, tl); tl += 2;
5261                                 NFSBCOPY(stateidp, tl, NFSX_STATEID);
5262                                 tl += (NFSX_STATEID / NFSX_UNSIGNED);
5263                                 *tl++ = txdr_unsigned(1);       /* One error. */
5264                                 NFSBCOPY(devid, tl, NFSX_V4DEVICEID);
5265                                 tl += (NFSX_V4DEVICEID / NFSX_UNSIGNED);
5266                                 *tl++ = txdr_unsigned(stat);
5267                                 *tl++ = txdr_unsigned(op);
5268                         } else {
5269                                 *tl = txdr_unsigned(2 * NFSX_UNSIGNED);
5270                                 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_UNSIGNED);
5271                                 /* No ioerrs. */
5272                                 *tl++ = 0;
5273                         }
5274                         *tl = 0;        /* No stats yet. */
5275                 }
5276         }
5277         nd->nd_flag |= ND_USEGSSNAME;
5278         error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
5279             NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
5280         if (error != 0)
5281                 return (error);
5282         if (nd->nd_repstat == 0) {
5283                 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
5284                 if (*tl != 0) {
5285                         NFSM_DISSECT(tl, uint32_t *, NFSX_STATEID);
5286                         stateidp->seqid = fxdr_unsigned(uint32_t, *tl++);
5287                         stateidp->other[0] = *tl++;
5288                         stateidp->other[1] = *tl++;
5289                         stateidp->other[2] = *tl;
5290                 }
5291         } else
5292                 error = nd->nd_repstat;
5293 nfsmout:
5294         mbuf_freem(nd->nd_mrep);
5295         return (error);
5296 }
5297
5298 /*
5299  * Acquire a layout and devinfo, if possible. The caller must have acquired
5300  * a reference count on the nfsclclient structure before calling this.
5301  * Return the layout in lypp with a reference count on it, if successful.
5302  */
5303 static int
5304 nfsrpc_getlayout(struct nfsmount *nmp, vnode_t vp, struct nfsfh *nfhp,
5305     int iomode, uint32_t *notifybitsp, nfsv4stateid_t *stateidp, uint64_t off,
5306     struct nfscllayout **lypp, struct ucred *cred, NFSPROC_T *p)
5307 {
5308         struct nfscllayout *lyp;
5309         struct nfsclflayout *flp;
5310         struct nfsclflayouthead flh;
5311         int error = 0, islocked, layoutlen, layouttype, recalled, retonclose;
5312         nfsv4stateid_t stateid;
5313         struct nfsclsession *tsep;
5314
5315         *lypp = NULL;
5316         if (NFSHASFLEXFILE(nmp))
5317                 layouttype = NFSLAYOUT_FLEXFILE;
5318         else
5319                 layouttype = NFSLAYOUT_NFSV4_1_FILES;
5320         /*
5321          * If lyp is returned non-NULL, there will be a refcnt (shared lock)
5322          * on it, iff flp != NULL or a lock (exclusive lock) on it iff
5323          * flp == NULL.
5324          */
5325         lyp = nfscl_getlayout(nmp->nm_clp, nfhp->nfh_fh, nfhp->nfh_len,
5326             off, &flp, &recalled);
5327         islocked = 0;
5328         if (lyp == NULL || flp == NULL) {
5329                 if (recalled != 0)
5330                         return (EIO);
5331                 LIST_INIT(&flh);
5332                 tsep = nfsmnt_mdssession(nmp);
5333                 layoutlen = tsep->nfsess_maxcache -
5334                     (NFSX_STATEID + 3 * NFSX_UNSIGNED);
5335                 if (lyp == NULL) {
5336                         stateid.seqid = 0;
5337                         stateid.other[0] = stateidp->other[0];
5338                         stateid.other[1] = stateidp->other[1];
5339                         stateid.other[2] = stateidp->other[2];
5340                         error = nfsrpc_layoutget(nmp, nfhp->nfh_fh,
5341                             nfhp->nfh_len, iomode, (uint64_t)0, UINT64_MAX,
5342                             (uint64_t)0, layouttype, layoutlen, &stateid,
5343                             &retonclose, &flh, cred, p, NULL);
5344                 } else {
5345                         islocked = 1;
5346                         stateid.seqid = lyp->nfsly_stateid.seqid;
5347                         stateid.other[0] = lyp->nfsly_stateid.other[0];
5348                         stateid.other[1] = lyp->nfsly_stateid.other[1];
5349                         stateid.other[2] = lyp->nfsly_stateid.other[2];
5350                         error = nfsrpc_layoutget(nmp, nfhp->nfh_fh,
5351                             nfhp->nfh_len, iomode, off, UINT64_MAX,
5352                             (uint64_t)0, layouttype, layoutlen, &stateid,
5353                             &retonclose, &flh, cred, p, NULL);
5354                 }
5355                 error = nfsrpc_layoutgetres(nmp, vp, nfhp->nfh_fh,
5356                     nfhp->nfh_len, &stateid, retonclose, notifybitsp, &lyp,
5357                     &flh, layouttype, error, NULL, cred, p);
5358                 if (error == 0)
5359                         *lypp = lyp;
5360                 else if (islocked != 0)
5361                         nfscl_rellayout(lyp, 1);
5362         } else
5363                 *lypp = lyp;
5364         return (error);
5365 }
5366
5367 /*
5368  * Do a TCP connection plus exchange id and create session.
5369  * If successful, a "struct nfsclds" is linked into the list for the
5370  * mount point and a pointer to it is returned.
5371  */
5372 static int
5373 nfsrpc_fillsa(struct nfsmount *nmp, struct sockaddr_in *sin,
5374     struct sockaddr_in6 *sin6, sa_family_t af, int vers, struct nfsclds **dspp,
5375     NFSPROC_T *p)
5376 {
5377         struct sockaddr_in *msad, *sad;
5378         struct sockaddr_in6 *msad6, *sad6;
5379         struct nfsclclient *clp;
5380         struct nfssockreq *nrp;
5381         struct nfsclds *dsp, *tdsp;
5382         int error;
5383         enum nfsclds_state retv;
5384         uint32_t sequenceid;
5385
5386         KASSERT(nmp->nm_sockreq.nr_cred != NULL,
5387             ("nfsrpc_fillsa: NULL nr_cred"));
5388         NFSLOCKCLSTATE();
5389         clp = nmp->nm_clp;
5390         NFSUNLOCKCLSTATE();
5391         if (clp == NULL)
5392                 return (EPERM);
5393         if (af == AF_INET) {
5394                 NFSLOCKMNT(nmp);
5395                 /*
5396                  * Check to see if we already have a session for this
5397                  * address that is usable for a DS.
5398                  * Note that the MDS's address is in a different place
5399                  * than the sessions already acquired for DS's.
5400                  */
5401                 msad = (struct sockaddr_in *)nmp->nm_sockreq.nr_nam;
5402                 tdsp = TAILQ_FIRST(&nmp->nm_sess);
5403                 while (tdsp != NULL) {
5404                         if (msad != NULL && msad->sin_family == AF_INET &&
5405                             sin->sin_addr.s_addr == msad->sin_addr.s_addr &&
5406                             sin->sin_port == msad->sin_port &&
5407                             (tdsp->nfsclds_flags & NFSCLDS_DS) != 0 &&
5408                             tdsp->nfsclds_sess.nfsess_defunct == 0) {
5409                                 *dspp = tdsp;
5410                                 NFSUNLOCKMNT(nmp);
5411                                 NFSCL_DEBUG(4, "fnd same addr\n");
5412                                 return (0);
5413                         }
5414                         tdsp = TAILQ_NEXT(tdsp, nfsclds_list);
5415                         if (tdsp != NULL && tdsp->nfsclds_sockp != NULL)
5416                                 msad = (struct sockaddr_in *)
5417                                     tdsp->nfsclds_sockp->nr_nam;
5418                         else
5419                                 msad = NULL;
5420                 }
5421                 NFSUNLOCKMNT(nmp);
5422
5423                 /* No IP address match, so look for new/trunked one. */
5424                 sad = malloc(sizeof(*sad), M_SONAME, M_WAITOK | M_ZERO);
5425                 sad->sin_len = sizeof(*sad);
5426                 sad->sin_family = AF_INET;
5427                 sad->sin_port = sin->sin_port;
5428                 sad->sin_addr.s_addr = sin->sin_addr.s_addr;
5429                 nrp = malloc(sizeof(*nrp), M_NFSSOCKREQ, M_WAITOK | M_ZERO);
5430                 nrp->nr_nam = (struct sockaddr *)sad;
5431         } else if (af == AF_INET6) {
5432                 NFSLOCKMNT(nmp);
5433                 /*
5434                  * Check to see if we already have a session for this
5435                  * address that is usable for a DS.
5436                  * Note that the MDS's address is in a different place
5437                  * than the sessions already acquired for DS's.
5438                  */
5439                 msad6 = (struct sockaddr_in6 *)nmp->nm_sockreq.nr_nam;
5440                 tdsp = TAILQ_FIRST(&nmp->nm_sess);
5441                 while (tdsp != NULL) {
5442                         if (msad6 != NULL && msad6->sin6_family == AF_INET6 &&
5443                             IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr,
5444                             &msad6->sin6_addr) &&
5445                             sin6->sin6_port == msad6->sin6_port &&
5446                             (tdsp->nfsclds_flags & NFSCLDS_DS) != 0 &&
5447                             tdsp->nfsclds_sess.nfsess_defunct == 0) {
5448                                 *dspp = tdsp;
5449                                 NFSUNLOCKMNT(nmp);
5450                                 return (0);
5451                         }
5452                         tdsp = TAILQ_NEXT(tdsp, nfsclds_list);
5453                         if (tdsp != NULL && tdsp->nfsclds_sockp != NULL)
5454                                 msad6 = (struct sockaddr_in6 *)
5455                                     tdsp->nfsclds_sockp->nr_nam;
5456                         else
5457                                 msad6 = NULL;
5458                 }
5459                 NFSUNLOCKMNT(nmp);
5460
5461                 /* No IP address match, so look for new/trunked one. */
5462                 sad6 = malloc(sizeof(*sad6), M_SONAME, M_WAITOK | M_ZERO);
5463                 sad6->sin6_len = sizeof(*sad6);
5464                 sad6->sin6_family = AF_INET6;
5465                 sad6->sin6_port = sin6->sin6_port;
5466                 NFSBCOPY(&sin6->sin6_addr, &sad6->sin6_addr,
5467                     sizeof(struct in6_addr));
5468                 nrp = malloc(sizeof(*nrp), M_NFSSOCKREQ, M_WAITOK | M_ZERO);
5469                 nrp->nr_nam = (struct sockaddr *)sad6;
5470         } else
5471                 return (EPERM);
5472
5473         nrp->nr_sotype = SOCK_STREAM;
5474         mtx_init(&nrp->nr_mtx, "nfssock", NULL, MTX_DEF);
5475         nrp->nr_prog = NFS_PROG;
5476         nrp->nr_vers = vers;
5477
5478         /*
5479          * Use the credentials that were used for the mount, which are
5480          * in nmp->nm_sockreq.nr_cred for newnfs_connect() etc.
5481          * Ref. counting the credentials with crhold() is probably not
5482          * necessary, since nm_sockreq.nr_cred won't be crfree()'d until
5483          * unmount, but I did it anyhow.
5484          */
5485         nrp->nr_cred = crhold(nmp->nm_sockreq.nr_cred);
5486         error = newnfs_connect(nmp, nrp, NULL, p, 0);
5487         NFSCL_DEBUG(3, "DS connect=%d\n", error);
5488
5489         dsp = NULL;
5490         /* Now, do the exchangeid and create session. */
5491         if (error == 0) {
5492                 if (vers == NFS_VER4) {
5493                         error = nfsrpc_exchangeid(nmp, clp, nrp,
5494                             NFSV4EXCH_USEPNFSDS, &dsp, nrp->nr_cred, p);
5495                         NFSCL_DEBUG(3, "DS exchangeid=%d\n", error);
5496                         if (error != 0)
5497                                 newnfs_disconnect(nrp);
5498                 } else {
5499                         dsp = malloc(sizeof(struct nfsclds), M_NFSCLDS,
5500                             M_WAITOK | M_ZERO);
5501                         dsp->nfsclds_flags |= NFSCLDS_DS;
5502                         dsp->nfsclds_expire = INT32_MAX; /* No renews needed. */
5503                         mtx_init(&dsp->nfsclds_mtx, "nfsds", NULL, MTX_DEF);
5504                         mtx_init(&dsp->nfsclds_sess.nfsess_mtx, "nfssession",
5505                             NULL, MTX_DEF);
5506                 }
5507         }
5508         if (error == 0) {
5509                 dsp->nfsclds_sockp = nrp;
5510                 if (vers == NFS_VER4) {
5511                         NFSLOCKMNT(nmp);
5512                         retv = nfscl_getsameserver(nmp, dsp, &tdsp,
5513                             &sequenceid);
5514                         NFSCL_DEBUG(3, "getsame ret=%d\n", retv);
5515                         if (retv == NFSDSP_USETHISSESSION &&
5516                             nfscl_dssameconn != 0) {
5517                                 NFSLOCKDS(tdsp);
5518                                 tdsp->nfsclds_flags |= NFSCLDS_SAMECONN;
5519                                 NFSUNLOCKDS(tdsp);
5520                                 NFSUNLOCKMNT(nmp);
5521                                 /*
5522                                  * If there is already a session for this
5523                                  * server, use it.
5524                                  */
5525                                 (void)newnfs_disconnect(nrp);
5526                                 nfscl_freenfsclds(dsp);
5527                                 *dspp = tdsp;
5528                                 return (0);
5529                         }
5530                         if (retv == NFSDSP_NOTFOUND)
5531                                 sequenceid =
5532                                     dsp->nfsclds_sess.nfsess_sequenceid;
5533                         NFSUNLOCKMNT(nmp);
5534                         error = nfsrpc_createsession(nmp, &dsp->nfsclds_sess,
5535                             nrp, sequenceid, 0, nrp->nr_cred, p);
5536                         NFSCL_DEBUG(3, "DS createsess=%d\n", error);
5537                 }
5538         } else {
5539                 NFSFREECRED(nrp->nr_cred);
5540                 NFSFREEMUTEX(&nrp->nr_mtx);
5541                 free(nrp->nr_nam, M_SONAME);
5542                 free(nrp, M_NFSSOCKREQ);
5543         }
5544         if (error == 0) {
5545                 NFSCL_DEBUG(3, "add DS session\n");
5546                 /*
5547                  * Put it at the end of the list. That way the list
5548                  * is ordered by when the entry was added. This matters
5549                  * since the one done first is the one that should be
5550                  * used for sequencid'ing any subsequent create sessions.
5551                  */
5552                 NFSLOCKMNT(nmp);
5553                 TAILQ_INSERT_TAIL(&nmp->nm_sess, dsp, nfsclds_list);
5554                 NFSUNLOCKMNT(nmp);
5555                 *dspp = dsp;
5556         } else if (dsp != NULL) {
5557                 newnfs_disconnect(nrp);
5558                 nfscl_freenfsclds(dsp);
5559         }
5560         return (error);
5561 }
5562
5563 /*
5564  * Do the NFSv4.1 Reclaim Complete.
5565  */
5566 int
5567 nfsrpc_reclaimcomplete(struct nfsmount *nmp, struct ucred *cred, NFSPROC_T *p)
5568 {
5569         uint32_t *tl;
5570         struct nfsrv_descript nfsd;
5571         struct nfsrv_descript *nd = &nfsd;
5572         int error;
5573
5574         nfscl_reqstart(nd, NFSPROC_RECLAIMCOMPL, nmp, NULL, 0, NULL, NULL, 0,
5575             0);
5576         NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
5577         *tl = newnfs_false;
5578         nd->nd_flag |= ND_USEGSSNAME;
5579         error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
5580             NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
5581         if (error != 0)
5582                 return (error);
5583         error = nd->nd_repstat;
5584         mbuf_freem(nd->nd_mrep);
5585         return (error);
5586 }
5587
5588 /*
5589  * Initialize the slot tables for a session.
5590  */
5591 static void
5592 nfscl_initsessionslots(struct nfsclsession *sep)
5593 {
5594         int i;
5595
5596         for (i = 0; i < NFSV4_CBSLOTS; i++) {
5597                 if (sep->nfsess_cbslots[i].nfssl_reply != NULL)
5598                         m_freem(sep->nfsess_cbslots[i].nfssl_reply);
5599                 NFSBZERO(&sep->nfsess_cbslots[i], sizeof(struct nfsslot));
5600         }
5601         for (i = 0; i < 64; i++)
5602                 sep->nfsess_slotseq[i] = 0;
5603         sep->nfsess_slots = 0;
5604 }
5605
5606 /*
5607  * Called to try and do an I/O operation via an NFSv4.1 Data Server (DS).
5608  */
5609 int
5610 nfscl_doiods(vnode_t vp, struct uio *uiop, int *iomode, int *must_commit,
5611     uint32_t rwaccess, int docommit, struct ucred *cred, NFSPROC_T *p)
5612 {
5613         struct nfsnode *np = VTONFS(vp);
5614         struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
5615         struct nfscllayout *layp;
5616         struct nfscldevinfo *dip;
5617         struct nfsclflayout *rflp;
5618         struct mbuf *m;
5619         struct nfsclwritedsdorpc *drpc, *tdrpc;
5620         nfsv4stateid_t stateid;
5621         struct ucred *newcred;
5622         uint64_t lastbyte, len, off, oresid, xfer;
5623         int eof, error, firstmirror, i, iolaymode, mirrorcnt, recalled, timo;
5624         void *lckp;
5625         uint8_t *dev;
5626         void *iovbase = NULL;
5627         size_t iovlen = 0;
5628         off_t offs = 0;
5629         ssize_t resid = 0;
5630
5631         if (!NFSHASPNFS(nmp) || nfscl_enablecallb == 0 || nfs_numnfscbd == 0 ||
5632             (np->n_flag & NNOLAYOUT) != 0)
5633                 return (EIO);
5634         /* Now, get a reference cnt on the clientid for this mount. */
5635         if (nfscl_getref(nmp) == 0)
5636                 return (EIO);
5637
5638         /* Find an appropriate stateid. */
5639         newcred = NFSNEWCRED(cred);
5640         error = nfscl_getstateid(vp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len,
5641             rwaccess, 1, newcred, p, &stateid, &lckp);
5642         if (error != 0) {
5643                 NFSFREECRED(newcred);
5644                 nfscl_relref(nmp);
5645                 return (error);
5646         }
5647         /* Search for a layout for this file. */
5648         off = uiop->uio_offset;
5649         layp = nfscl_getlayout(nmp->nm_clp, np->n_fhp->nfh_fh,
5650             np->n_fhp->nfh_len, off, &rflp, &recalled);
5651         if (layp == NULL || rflp == NULL) {
5652                 if (recalled != 0) {
5653                         NFSFREECRED(newcred);
5654                         nfscl_relref(nmp);
5655                         return (EIO);
5656                 }
5657                 if (layp != NULL) {
5658                         nfscl_rellayout(layp, (rflp == NULL) ? 1 : 0);
5659                         layp = NULL;
5660                 }
5661                 /* Try and get a Layout, if it is supported. */
5662                 if (rwaccess == NFSV4OPEN_ACCESSWRITE ||
5663                     (np->n_flag & NWRITEOPENED) != 0)
5664                         iolaymode = NFSLAYOUTIOMODE_RW;
5665                 else
5666                         iolaymode = NFSLAYOUTIOMODE_READ;
5667                 error = nfsrpc_getlayout(nmp, vp, np->n_fhp, iolaymode,
5668                     NULL, &stateid, off, &layp, newcred, p);
5669                 if (error != 0) {
5670                         NFSLOCKNODE(np);
5671                         np->n_flag |= NNOLAYOUT;
5672                         NFSUNLOCKNODE(np);
5673                         if (lckp != NULL)
5674                                 nfscl_lockderef(lckp);
5675                         NFSFREECRED(newcred);
5676                         if (layp != NULL)
5677                                 nfscl_rellayout(layp, 0);
5678                         nfscl_relref(nmp);
5679                         return (error);
5680                 }
5681         }
5682
5683         /*
5684          * Loop around finding a layout that works for the first part of
5685          * this I/O operation, and then call the function that actually
5686          * does the RPC.
5687          */
5688         eof = 0;
5689         len = (uint64_t)uiop->uio_resid;
5690         while (len > 0 && error == 0 && eof == 0) {
5691                 off = uiop->uio_offset;
5692                 error = nfscl_findlayoutforio(layp, off, rwaccess, &rflp);
5693                 if (error == 0) {
5694                         oresid = xfer = (uint64_t)uiop->uio_resid;
5695                         if (xfer > (rflp->nfsfl_end - rflp->nfsfl_off))
5696                                 xfer = rflp->nfsfl_end - rflp->nfsfl_off;
5697                         /*
5698                          * For Flex File layout with mirrored DSs, select one
5699                          * of them at random for reads. For writes and commits,
5700                          * do all mirrors.
5701                          */
5702                         m = NULL;
5703                         tdrpc = drpc = NULL;
5704                         firstmirror = 0;
5705                         mirrorcnt = 1;
5706                         if ((layp->nfsly_flags & NFSLY_FLEXFILE) != 0 &&
5707                             (mirrorcnt = rflp->nfsfl_mirrorcnt) > 1) {
5708                                 if (rwaccess == NFSV4OPEN_ACCESSREAD) {
5709                                         firstmirror = arc4random() % mirrorcnt;
5710                                         mirrorcnt = firstmirror + 1;
5711                                 } else {
5712                                         if (docommit == 0) {
5713                                                 /*
5714                                                  * Save values, so uiop can be
5715                                                  * rolled back upon a write
5716                                                  * error.
5717                                                  */
5718                                                 offs = uiop->uio_offset;
5719                                                 resid = uiop->uio_resid;
5720                                                 iovbase =
5721                                                     uiop->uio_iov->iov_base;
5722                                                 iovlen = uiop->uio_iov->iov_len;
5723                                                 m = nfsm_uiombuflist(uiop, len,
5724                                                     NULL, NULL);
5725                                         }
5726                                         tdrpc = drpc = malloc(sizeof(*drpc) *
5727                                             (mirrorcnt - 1), M_TEMP, M_WAITOK |
5728                                             M_ZERO);
5729                                 }
5730                         }
5731                         for (i = firstmirror; i < mirrorcnt && error == 0; i++){
5732                                 if ((layp->nfsly_flags & NFSLY_FLEXFILE) != 0) {
5733                                         dev = rflp->nfsfl_ffm[i].dev;
5734                                         dip = nfscl_getdevinfo(nmp->nm_clp, dev,
5735                                             rflp->nfsfl_ffm[i].devp);
5736                                 } else {
5737                                         dev = rflp->nfsfl_dev;
5738                                         dip = nfscl_getdevinfo(nmp->nm_clp, dev,
5739                                             rflp->nfsfl_devp);
5740                                 }
5741                                 if (dip != NULL) {
5742                                         if ((rflp->nfsfl_flags & NFSFL_FLEXFILE)
5743                                             != 0)
5744                                                 error = nfscl_dofflayoutio(vp,
5745                                                     uiop, iomode, must_commit,
5746                                                     &eof, &stateid, rwaccess,
5747                                                     dip, layp, rflp, off, xfer,
5748                                                     i, docommit, m, tdrpc,
5749                                                     newcred, p);
5750                                         else
5751                                                 error = nfscl_doflayoutio(vp,
5752                                                     uiop, iomode, must_commit,
5753                                                     &eof, &stateid, rwaccess,
5754                                                     dip, layp, rflp, off, xfer,
5755                                                     docommit, newcred, p);
5756                                         nfscl_reldevinfo(dip);
5757                                 } else
5758                                         error = EIO;
5759                                 tdrpc++;
5760                         }
5761                         if (m != NULL)
5762                                 m_freem(m);
5763                         tdrpc = drpc;
5764                         timo = hz / 50;         /* Wait for 20msec. */
5765                         if (timo < 1)
5766                                 timo = 1;
5767                         for (i = firstmirror; i < mirrorcnt - 1 &&
5768                             tdrpc != NULL; i++, tdrpc++) {
5769                                 /*
5770                                  * For the unused drpc entries, both inprog and
5771                                  * err == 0, so this loop won't break.
5772                                  */
5773                                 while (tdrpc->inprog != 0 && tdrpc->done == 0)
5774                                         tsleep(&tdrpc->tsk, PVFS, "clrpcio",
5775                                             timo);
5776                                 if (error == 0 && tdrpc->err != 0)
5777                                         error = tdrpc->err;
5778                         }
5779                         free(drpc, M_TEMP);
5780                         if (error == 0) {
5781                                 if (mirrorcnt > 1 && rwaccess ==
5782                                     NFSV4OPEN_ACCESSWRITE && docommit == 0) {
5783                                         NFSLOCKCLSTATE();
5784                                         layp->nfsly_flags |= NFSLY_WRITTEN;
5785                                         NFSUNLOCKCLSTATE();
5786                                 }
5787                                 lastbyte = off + xfer - 1;
5788                                 NFSLOCKCLSTATE();
5789                                 if (lastbyte > layp->nfsly_lastbyte)
5790                                         layp->nfsly_lastbyte = lastbyte;
5791                                 NFSUNLOCKCLSTATE();
5792                         } else if (error == NFSERR_OPENMODE &&
5793                             rwaccess == NFSV4OPEN_ACCESSREAD) {
5794                                 NFSLOCKMNT(nmp);
5795                                 nmp->nm_state |= NFSSTA_OPENMODE;
5796                                 NFSUNLOCKMNT(nmp);
5797                         } else
5798                                 error = EIO;
5799                         if (error == 0)
5800                                 len -= (oresid - (uint64_t)uiop->uio_resid);
5801                         else if (mirrorcnt > 1 && rwaccess ==
5802                             NFSV4OPEN_ACCESSWRITE && docommit == 0) {
5803                                 /*
5804                                  * In case the rpc gets retried, roll the
5805                                  * uio fields changed by nfsm_uiombuflist()
5806                                  * back.
5807                                  */
5808                                 uiop->uio_offset = offs;
5809                                 uiop->uio_resid = resid;
5810                                 uiop->uio_iov->iov_base = iovbase;
5811                                 uiop->uio_iov->iov_len = iovlen;
5812                         }
5813                 }
5814         }
5815         if (lckp != NULL)
5816                 nfscl_lockderef(lckp);
5817         NFSFREECRED(newcred);
5818         nfscl_rellayout(layp, 0);
5819         nfscl_relref(nmp);
5820         return (error);
5821 }
5822
5823 /*
5824  * Make a copy of the mbuf chain and add an mbuf for null padding, as required.
5825  */
5826 static struct mbuf *
5827 nfsm_copym(struct mbuf *m, int off, int xfer)
5828 {
5829         struct mbuf *m2, *m3, *m4;
5830         uint32_t *tl;
5831         int rem;
5832
5833         m2 = m_copym(m, off, xfer, M_WAITOK);
5834         rem = NFSM_RNDUP(xfer) - xfer;
5835         if (rem > 0) {
5836                 /*
5837                  * The zero padding to a multiple of 4 bytes is required by
5838                  * the XDR. So that the mbufs copied by reference aren't
5839                  * modified, add an mbuf with the zero'd bytes to the list.
5840                  * rem will be a maximum of 3, so one zero'd uint32_t is
5841                  * sufficient.
5842                  */
5843                 m3 = m2;
5844                 while (m3->m_next != NULL)
5845                         m3 = m3->m_next;
5846                 NFSMGET(m4);
5847                 tl = NFSMTOD(m4, uint32_t *);
5848                 *tl = 0;
5849                 mbuf_setlen(m4, rem);
5850                 mbuf_setnext(m3, m4);
5851         }
5852         return (m2);
5853 }
5854
5855 /*
5856  * Find a file layout that will handle the first bytes of the requested
5857  * range and return the information from it needed to the I/O operation.
5858  */
5859 int
5860 nfscl_findlayoutforio(struct nfscllayout *lyp, uint64_t off, uint32_t rwaccess,
5861     struct nfsclflayout **retflpp)
5862 {
5863         struct nfsclflayout *flp, *nflp, *rflp;
5864         uint32_t rw;
5865
5866         rflp = NULL;
5867         rw = rwaccess;
5868         /* For reading, do the Read list first and then the Write list. */
5869         do {
5870                 if (rw == NFSV4OPEN_ACCESSREAD)
5871                         flp = LIST_FIRST(&lyp->nfsly_flayread);
5872                 else
5873                         flp = LIST_FIRST(&lyp->nfsly_flayrw);
5874                 while (flp != NULL) {
5875                         nflp = LIST_NEXT(flp, nfsfl_list);
5876                         if (flp->nfsfl_off > off)
5877                                 break;
5878                         if (flp->nfsfl_end > off &&
5879                             (rflp == NULL || rflp->nfsfl_end < flp->nfsfl_end))
5880                                 rflp = flp;
5881                         flp = nflp;
5882                 }
5883                 if (rw == NFSV4OPEN_ACCESSREAD)
5884                         rw = NFSV4OPEN_ACCESSWRITE;
5885                 else
5886                         rw = 0;
5887         } while (rw != 0);
5888         if (rflp != NULL) {
5889                 /* This one covers the most bytes starting at off. */
5890                 *retflpp = rflp;
5891                 return (0);
5892         }
5893         return (EIO);
5894 }
5895
5896 /*
5897  * Do I/O using an NFSv4.1 file layout.
5898  */
5899 static int
5900 nfscl_doflayoutio(vnode_t vp, struct uio *uiop, int *iomode, int *must_commit,
5901     int *eofp, nfsv4stateid_t *stateidp, int rwflag, struct nfscldevinfo *dp,
5902     struct nfscllayout *lyp, struct nfsclflayout *flp, uint64_t off,
5903     uint64_t len, int docommit, struct ucred *cred, NFSPROC_T *p)
5904 {
5905         uint64_t io_off, rel_off, stripe_unit_size, transfer, xfer;
5906         int commit_thru_mds, error, stripe_index, stripe_pos;
5907         struct nfsnode *np;
5908         struct nfsfh *fhp;
5909         struct nfsclds **dspp;
5910
5911         np = VTONFS(vp);
5912         rel_off = off - flp->nfsfl_patoff;
5913         stripe_unit_size = (flp->nfsfl_util >> 6) & 0x3ffffff;
5914         stripe_pos = (rel_off / stripe_unit_size + flp->nfsfl_stripe1) %
5915             dp->nfsdi_stripecnt;
5916         transfer = stripe_unit_size - (rel_off % stripe_unit_size);
5917         error = 0;
5918
5919         /* Loop around, doing I/O for each stripe unit. */
5920         while (len > 0 && error == 0) {
5921                 stripe_index = nfsfldi_stripeindex(dp, stripe_pos);
5922                 dspp = nfsfldi_addr(dp, stripe_index);
5923                 if (len > transfer && docommit == 0)
5924                         xfer = transfer;
5925                 else
5926                         xfer = len;
5927                 if ((flp->nfsfl_util & NFSFLAYUTIL_DENSE) != 0) {
5928                         /* Dense layout. */
5929                         if (stripe_pos >= flp->nfsfl_fhcnt)
5930                                 return (EIO);
5931                         fhp = flp->nfsfl_fh[stripe_pos];
5932                         io_off = (rel_off / (stripe_unit_size *
5933                             dp->nfsdi_stripecnt)) * stripe_unit_size +
5934                             rel_off % stripe_unit_size;
5935                 } else {
5936                         /* Sparse layout. */
5937                         if (flp->nfsfl_fhcnt > 1) {
5938                                 if (stripe_index >= flp->nfsfl_fhcnt)
5939                                         return (EIO);
5940                                 fhp = flp->nfsfl_fh[stripe_index];
5941                         } else if (flp->nfsfl_fhcnt == 1)
5942                                 fhp = flp->nfsfl_fh[0];
5943                         else
5944                                 fhp = np->n_fhp;
5945                         io_off = off;
5946                 }
5947                 if ((flp->nfsfl_util & NFSFLAYUTIL_COMMIT_THRU_MDS) != 0) {
5948                         commit_thru_mds = 1;
5949                         if (docommit != 0)
5950                                 error = EIO;
5951                 } else {
5952                         commit_thru_mds = 0;
5953                         NFSLOCKNODE(np);
5954                         np->n_flag |= NDSCOMMIT;
5955                         NFSUNLOCKNODE(np);
5956                 }
5957                 if (docommit != 0) {
5958                         if (error == 0)
5959                                 error = nfsrpc_commitds(vp, io_off, xfer,
5960                                     *dspp, fhp, 0, 0, cred, p);
5961                         if (error == 0) {
5962                                 /*
5963                                  * Set both eof and uio_resid = 0 to end any
5964                                  * loops.
5965                                  */
5966                                 *eofp = 1;
5967                                 uiop->uio_resid = 0;
5968                         } else {
5969                                 NFSLOCKNODE(np);
5970                                 np->n_flag &= ~NDSCOMMIT;
5971                                 NFSUNLOCKNODE(np);
5972                         }
5973                 } else if (rwflag == NFSV4OPEN_ACCESSREAD)
5974                         error = nfsrpc_readds(vp, uiop, stateidp, eofp, *dspp,
5975                             io_off, xfer, fhp, 0, 0, 0, cred, p);
5976                 else {
5977                         error = nfsrpc_writeds(vp, uiop, iomode, must_commit,
5978                             stateidp, *dspp, io_off, xfer, fhp, commit_thru_mds,
5979                             0, 0, 0, cred, p);
5980                         if (error == 0) {
5981                                 NFSLOCKCLSTATE();
5982                                 lyp->nfsly_flags |= NFSLY_WRITTEN;
5983                                 NFSUNLOCKCLSTATE();
5984                         }
5985                 }
5986                 if (error == 0) {
5987                         transfer = stripe_unit_size;
5988                         stripe_pos = (stripe_pos + 1) % dp->nfsdi_stripecnt;
5989                         len -= xfer;
5990                         off += xfer;
5991                 }
5992         }
5993         return (error);
5994 }
5995
5996 /*
5997  * Do I/O using an NFSv4.1 flex file layout.
5998  */
5999 static int
6000 nfscl_dofflayoutio(vnode_t vp, struct uio *uiop, int *iomode, int *must_commit,
6001     int *eofp, nfsv4stateid_t *stateidp, int rwflag, struct nfscldevinfo *dp,
6002     struct nfscllayout *lyp, struct nfsclflayout *flp, uint64_t off,
6003     uint64_t len, int mirror, int docommit, struct mbuf *mp,
6004     struct nfsclwritedsdorpc *drpc, struct ucred *cred, NFSPROC_T *p)
6005 {
6006         uint64_t transfer, xfer;
6007         int error, rel_off;
6008         struct nfsnode *np;
6009         struct nfsfh *fhp;
6010         struct nfsclds **dspp;
6011         struct ucred *tcred;
6012         struct mbuf *m;
6013
6014         np = VTONFS(vp);
6015         error = 0;
6016         rel_off = 0;
6017         NFSCL_DEBUG(4, "nfscl_dofflayoutio: off=%ju len=%ju\n", (uintmax_t)off,
6018             (uintmax_t)len);
6019         /* Loop around, doing I/O for each stripe unit. */
6020         while (len > 0 && error == 0) {
6021                 dspp = nfsfldi_addr(dp, 0);
6022                 fhp = flp->nfsfl_ffm[mirror].fh[dp->nfsdi_versindex];
6023                 stateidp = &flp->nfsfl_ffm[mirror].st;
6024                 NFSCL_DEBUG(4, "mirror=%d vind=%d fhlen=%d st.seqid=0x%x\n",
6025                     mirror, dp->nfsdi_versindex, fhp->nfh_len, stateidp->seqid);
6026                 if ((dp->nfsdi_flags & NFSDI_TIGHTCOUPLED) == 0) {
6027                         tcred = NFSNEWCRED(cred);
6028                         tcred->cr_uid = flp->nfsfl_ffm[mirror].user;
6029                         tcred->cr_groups[0] = flp->nfsfl_ffm[mirror].group;
6030                         tcred->cr_ngroups = 1;
6031                 } else
6032                         tcred = cred;
6033                 if (rwflag == NFSV4OPEN_ACCESSREAD)
6034                         transfer = dp->nfsdi_rsize;
6035                 else
6036                         transfer = dp->nfsdi_wsize;
6037                 NFSLOCKNODE(np);
6038                 np->n_flag |= NDSCOMMIT;
6039                 NFSUNLOCKNODE(np);
6040                 if (len > transfer && docommit == 0)
6041                         xfer = transfer;
6042                 else
6043                         xfer = len;
6044                 if (docommit != 0) {
6045                         if (error == 0) {
6046                                 /*
6047                                  * Do last mirrored DS commit with this thread.
6048                                  */
6049                                 if (mirror < flp->nfsfl_mirrorcnt - 1)
6050                                         error = nfsio_commitds(vp, off, xfer,
6051                                             *dspp, fhp, dp->nfsdi_vers,
6052                                             dp->nfsdi_minorvers, drpc, tcred,
6053                                             p);
6054                                 else
6055                                         error = nfsrpc_commitds(vp, off, xfer,
6056                                             *dspp, fhp, dp->nfsdi_vers,
6057                                             dp->nfsdi_minorvers, tcred, p);
6058                                 NFSCL_DEBUG(4, "commitds=%d\n", error);
6059                                 if (error != 0 && error != EACCES && error !=
6060                                     ESTALE) {
6061                                         NFSCL_DEBUG(4,
6062                                             "DS layreterr for commit\n");
6063                                         nfscl_dserr(NFSV4OP_COMMIT, error, dp,
6064                                             lyp, *dspp);
6065                                 }
6066                         }
6067                         NFSCL_DEBUG(4, "aft nfsio_commitds=%d\n", error);
6068                         if (error == 0) {
6069                                 /*
6070                                  * Set both eof and uio_resid = 0 to end any
6071                                  * loops.
6072                                  */
6073                                 *eofp = 1;
6074                                 uiop->uio_resid = 0;
6075                         } else {
6076                                 NFSLOCKNODE(np);
6077                                 np->n_flag &= ~NDSCOMMIT;
6078                                 NFSUNLOCKNODE(np);
6079                         }
6080                 } else if (rwflag == NFSV4OPEN_ACCESSREAD) {
6081                         error = nfsrpc_readds(vp, uiop, stateidp, eofp, *dspp,
6082                             off, xfer, fhp, 1, dp->nfsdi_vers,
6083                             dp->nfsdi_minorvers, tcred, p);
6084                         NFSCL_DEBUG(4, "readds=%d\n", error);
6085                         if (error != 0 && error != EACCES && error != ESTALE) {
6086                                 NFSCL_DEBUG(4, "DS layreterr for read\n");
6087                                 nfscl_dserr(NFSV4OP_READ, error, dp, lyp,
6088                                     *dspp);
6089                         }
6090                 } else {
6091                         if (flp->nfsfl_mirrorcnt == 1) {
6092                                 error = nfsrpc_writeds(vp, uiop, iomode,
6093                                     must_commit, stateidp, *dspp, off, xfer,
6094                                     fhp, 0, 1, dp->nfsdi_vers,
6095                                     dp->nfsdi_minorvers, tcred, p);
6096                                 if (error == 0) {
6097                                         NFSLOCKCLSTATE();
6098                                         lyp->nfsly_flags |= NFSLY_WRITTEN;
6099                                         NFSUNLOCKCLSTATE();
6100                                 }
6101                         } else {
6102                                 m = nfsm_copym(mp, rel_off, xfer);
6103                                 NFSCL_DEBUG(4, "mcopy reloff=%d xfer=%jd\n",
6104                                     rel_off, (uintmax_t)xfer);
6105                                 /*
6106                                  * Do the writes after the first loop iteration
6107                                  * and the write for the last mirror via this
6108                                  * thread.
6109                                  * This loop only iterates for small values
6110                                  * of nfsdi_wsize, which may never occur in
6111                                  * practice.  However, the drpc is completely
6112                                  * used by the first iteration and, as such,
6113                                  * cannot be used after that.
6114                                  */
6115                                 if (mirror < flp->nfsfl_mirrorcnt - 1 &&
6116                                     rel_off == 0)
6117                                         error = nfsio_writedsmir(vp, iomode,
6118                                             must_commit, stateidp, *dspp, off,
6119                                             xfer, fhp, m, dp->nfsdi_vers,
6120                                             dp->nfsdi_minorvers, drpc, tcred,
6121                                             p);
6122                                 else
6123                                         error = nfsrpc_writedsmir(vp, iomode,
6124                                             must_commit, stateidp, *dspp, off,
6125                                             xfer, fhp, m, dp->nfsdi_vers,
6126                                             dp->nfsdi_minorvers, tcred, p);
6127                                 NFSCL_DEBUG(4, "nfsio_writedsmir=%d\n", error);
6128                                 if (error != 0 && error != EACCES && error !=
6129                                     ESTALE) {
6130                                         NFSCL_DEBUG(4,
6131                                             "DS layreterr for write\n");
6132                                         nfscl_dserr(NFSV4OP_WRITE, error, dp,
6133                                             lyp, *dspp);
6134                                 }
6135                         }
6136                 }
6137                 NFSCL_DEBUG(4, "aft read/writeds=%d\n", error);
6138                 if (error == 0) {
6139                         len -= xfer;
6140                         off += xfer;
6141                         rel_off += xfer;
6142                 }
6143                 if ((dp->nfsdi_flags & NFSDI_TIGHTCOUPLED) == 0)
6144                         NFSFREECRED(tcred);
6145         }
6146         NFSCL_DEBUG(4, "eo nfscl_dofflayoutio=%d\n", error);
6147         return (error);
6148 }
6149
6150 /*
6151  * The actual read RPC done to a DS.
6152  */
6153 static int
6154 nfsrpc_readds(vnode_t vp, struct uio *uiop, nfsv4stateid_t *stateidp, int *eofp,
6155     struct nfsclds *dsp, uint64_t io_off, int len, struct nfsfh *fhp, int flex,
6156     int vers, int minorvers, struct ucred *cred, NFSPROC_T *p)
6157 {
6158         uint32_t *tl;
6159         int attrflag, error, retlen;
6160         struct nfsrv_descript nfsd;
6161         struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
6162         struct nfsrv_descript *nd = &nfsd;
6163         struct nfssockreq *nrp;
6164         struct nfsvattr na;
6165
6166         nd->nd_mrep = NULL;
6167         if (vers == 0 || vers == NFS_VER4) {
6168                 nfscl_reqstart(nd, NFSPROC_READDS, nmp, fhp->nfh_fh,
6169                     fhp->nfh_len, NULL, &dsp->nfsclds_sess, vers, minorvers);
6170                 vers = NFS_VER4;
6171                 NFSCL_DEBUG(4, "nfsrpc_readds: vers4 minvers=%d\n", minorvers);
6172                 if (flex != 0)
6173                         nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID);
6174                 else
6175                         nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSEQIDZERO);
6176         } else {
6177                 nfscl_reqstart(nd, NFSPROC_READ, nmp, fhp->nfh_fh,
6178                     fhp->nfh_len, NULL, &dsp->nfsclds_sess, vers, minorvers);
6179                 NFSCL_DEBUG(4, "nfsrpc_readds: vers3\n");
6180         }
6181         NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED * 3);
6182         txdr_hyper(io_off, tl);
6183         *(tl + 2) = txdr_unsigned(len);
6184         nrp = dsp->nfsclds_sockp;
6185         NFSCL_DEBUG(4, "nfsrpc_readds: nrp=%p\n", nrp);
6186         if (nrp == NULL)
6187                 /* If NULL, use the MDS socket. */
6188                 nrp = &nmp->nm_sockreq;
6189         error = newnfs_request(nd, nmp, NULL, nrp, vp, p, cred,
6190             NFS_PROG, vers, NULL, 1, NULL, &dsp->nfsclds_sess);
6191         NFSCL_DEBUG(4, "nfsrpc_readds: stat=%d err=%d\n", nd->nd_repstat,
6192             error);
6193         if (error != 0)
6194                 return (error);
6195         if (vers == NFS_VER3) {
6196                 error = nfscl_postop_attr(nd, &na, &attrflag, NULL);
6197                 NFSCL_DEBUG(4, "nfsrpc_readds: postop=%d\n", error);
6198                 if (error != 0)
6199                         goto nfsmout;
6200         }
6201         if (nd->nd_repstat != 0) {
6202                 error = nd->nd_repstat;
6203                 goto nfsmout;
6204         }
6205         if (vers == NFS_VER3) {
6206                 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
6207                 *eofp = fxdr_unsigned(int, *(tl + 1));
6208         } else {
6209                 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
6210                 *eofp = fxdr_unsigned(int, *tl);
6211         }
6212         NFSM_STRSIZ(retlen, len);
6213         NFSCL_DEBUG(4, "nfsrpc_readds: retlen=%d eof=%d\n", retlen, *eofp);
6214         error = nfsm_mbufuio(nd, uiop, retlen);
6215 nfsmout:
6216         if (nd->nd_mrep != NULL)
6217                 mbuf_freem(nd->nd_mrep);
6218         return (error);
6219 }
6220
6221 /*
6222  * The actual write RPC done to a DS.
6223  */
6224 static int
6225 nfsrpc_writeds(vnode_t vp, struct uio *uiop, int *iomode, int *must_commit,
6226     nfsv4stateid_t *stateidp, struct nfsclds *dsp, uint64_t io_off, int len,
6227     struct nfsfh *fhp, int commit_thru_mds, int flex, int vers, int minorvers,
6228     struct ucred *cred, NFSPROC_T *p)
6229 {
6230         uint32_t *tl;
6231         struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
6232         int attrflag, error, rlen, commit, committed = NFSWRITE_FILESYNC;
6233         int32_t backup;
6234         struct nfsrv_descript nfsd;
6235         struct nfsrv_descript *nd = &nfsd;
6236         struct nfssockreq *nrp;
6237         struct nfsvattr na;
6238
6239         KASSERT(uiop->uio_iovcnt == 1, ("nfs: writerpc iovcnt > 1"));
6240         nd->nd_mrep = NULL;
6241         if (vers == 0 || vers == NFS_VER4) {
6242                 nfscl_reqstart(nd, NFSPROC_WRITEDS, nmp, fhp->nfh_fh,
6243                     fhp->nfh_len, NULL, &dsp->nfsclds_sess, vers, minorvers);
6244                 NFSCL_DEBUG(4, "nfsrpc_writeds: vers4 minvers=%d\n", minorvers);
6245                 vers = NFS_VER4;
6246                 if (flex != 0)
6247                         nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID);
6248                 else
6249                         nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSEQIDZERO);
6250                 NFSM_BUILD(tl, uint32_t *, NFSX_HYPER + 2 * NFSX_UNSIGNED);
6251         } else {
6252                 nfscl_reqstart(nd, NFSPROC_WRITE, nmp, fhp->nfh_fh,
6253                     fhp->nfh_len, NULL, &dsp->nfsclds_sess, vers, minorvers);
6254                 NFSCL_DEBUG(4, "nfsrpc_writeds: vers3\n");
6255                 NFSM_BUILD(tl, uint32_t *, NFSX_HYPER + 3 * NFSX_UNSIGNED);
6256         }
6257         txdr_hyper(io_off, tl);
6258         tl += 2;
6259         if (vers == NFS_VER3)
6260                 *tl++ = txdr_unsigned(len);
6261         *tl++ = txdr_unsigned(*iomode);
6262         *tl = txdr_unsigned(len);
6263         nfsm_uiombuf(nd, uiop, len);
6264         nrp = dsp->nfsclds_sockp;
6265         if (nrp == NULL)
6266                 /* If NULL, use the MDS socket. */
6267                 nrp = &nmp->nm_sockreq;
6268         error = newnfs_request(nd, nmp, NULL, nrp, vp, p, cred,
6269             NFS_PROG, vers, NULL, 1, NULL, &dsp->nfsclds_sess);
6270         NFSCL_DEBUG(4, "nfsrpc_writeds: err=%d stat=%d\n", error,
6271             nd->nd_repstat);
6272         if (error != 0)
6273                 return (error);
6274         if (nd->nd_repstat != 0) {
6275                 /*
6276                  * In case the rpc gets retried, roll
6277                  * the uio fileds changed by nfsm_uiombuf()
6278                  * back.
6279                  */
6280                 uiop->uio_offset -= len;
6281                 uio_uio_resid_add(uiop, len);
6282                 uio_iov_base_add(uiop, -len);
6283                 uio_iov_len_add(uiop, len);
6284                 error = nd->nd_repstat;
6285         } else {
6286                 if (vers == NFS_VER3) {
6287                         error = nfscl_wcc_data(nd, vp, &na, &attrflag, NULL,
6288                             NULL);
6289                         NFSCL_DEBUG(4, "nfsrpc_writeds: wcc_data=%d\n", error);
6290                         if (error != 0)
6291                                 goto nfsmout;
6292                 }
6293                 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED + NFSX_VERF);
6294                 rlen = fxdr_unsigned(int, *tl++);
6295                 NFSCL_DEBUG(4, "nfsrpc_writeds: len=%d rlen=%d\n", len, rlen);
6296                 if (rlen == 0) {
6297                         error = NFSERR_IO;
6298                         goto nfsmout;
6299                 } else if (rlen < len) {
6300                         backup = len - rlen;
6301                         uio_iov_base_add(uiop, -(backup));
6302                         uio_iov_len_add(uiop, backup);
6303                         uiop->uio_offset -= backup;
6304                         uio_uio_resid_add(uiop, backup);
6305                         len = rlen;
6306                 }
6307                 commit = fxdr_unsigned(int, *tl++);
6308
6309                 /*
6310                  * Return the lowest commitment level
6311                  * obtained by any of the RPCs.
6312                  */
6313                 if (committed == NFSWRITE_FILESYNC)
6314                         committed = commit;
6315                 else if (committed == NFSWRITE_DATASYNC &&
6316                     commit == NFSWRITE_UNSTABLE)
6317                         committed = commit;
6318                 if (commit_thru_mds != 0) {
6319                         NFSLOCKMNT(nmp);
6320                         if (!NFSHASWRITEVERF(nmp)) {
6321                                 NFSBCOPY(tl, nmp->nm_verf, NFSX_VERF);
6322                                 NFSSETWRITEVERF(nmp);
6323                         } else if (NFSBCMP(tl, nmp->nm_verf, NFSX_VERF)) {
6324                                 *must_commit = 1;
6325                                 NFSBCOPY(tl, nmp->nm_verf, NFSX_VERF);
6326                         }
6327                         NFSUNLOCKMNT(nmp);
6328                 } else {
6329                         NFSLOCKDS(dsp);
6330                         if ((dsp->nfsclds_flags & NFSCLDS_HASWRITEVERF) == 0) {
6331                                 NFSBCOPY(tl, dsp->nfsclds_verf, NFSX_VERF);
6332                                 dsp->nfsclds_flags |= NFSCLDS_HASWRITEVERF;
6333                         } else if (NFSBCMP(tl, dsp->nfsclds_verf, NFSX_VERF)) {
6334                                 *must_commit = 1;
6335                                 NFSBCOPY(tl, dsp->nfsclds_verf, NFSX_VERF);
6336                         }
6337                         NFSUNLOCKDS(dsp);
6338                 }
6339         }
6340 nfsmout:
6341         if (nd->nd_mrep != NULL)
6342                 mbuf_freem(nd->nd_mrep);
6343         *iomode = committed;
6344         if (nd->nd_repstat != 0 && error == 0)
6345                 error = nd->nd_repstat;
6346         return (error);
6347 }
6348
6349 /*
6350  * The actual write RPC done to a DS.
6351  * This variant is called from a separate kernel process for mirrors.
6352  * Any short write is considered an IO error.
6353  */
6354 static int
6355 nfsrpc_writedsmir(vnode_t vp, int *iomode, int *must_commit,
6356     nfsv4stateid_t *stateidp, struct nfsclds *dsp, uint64_t io_off, int len,
6357     struct nfsfh *fhp, struct mbuf *m, int vers, int minorvers,
6358     struct ucred *cred, NFSPROC_T *p)
6359 {
6360         uint32_t *tl;
6361         struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
6362         int attrflag, error, commit, committed = NFSWRITE_FILESYNC, rlen;
6363         struct nfsrv_descript nfsd;
6364         struct nfsrv_descript *nd = &nfsd;
6365         struct nfssockreq *nrp;
6366         struct nfsvattr na;
6367
6368         nd->nd_mrep = NULL;
6369         if (vers == 0 || vers == NFS_VER4) {
6370                 nfscl_reqstart(nd, NFSPROC_WRITEDS, nmp, fhp->nfh_fh,
6371                     fhp->nfh_len, NULL, &dsp->nfsclds_sess, vers, minorvers);
6372                 vers = NFS_VER4;
6373                 NFSCL_DEBUG(4, "nfsrpc_writedsmir: vers4 minvers=%d\n",
6374                     minorvers);
6375                 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID);
6376                 NFSM_BUILD(tl, uint32_t *, NFSX_HYPER + 2 * NFSX_UNSIGNED);
6377         } else {
6378                 nfscl_reqstart(nd, NFSPROC_WRITE, nmp, fhp->nfh_fh,
6379                     fhp->nfh_len, NULL, &dsp->nfsclds_sess, vers, minorvers);
6380                 NFSCL_DEBUG(4, "nfsrpc_writedsmir: vers3\n");
6381                 NFSM_BUILD(tl, uint32_t *, NFSX_HYPER + 3 * NFSX_UNSIGNED);
6382         }
6383         txdr_hyper(io_off, tl);
6384         tl += 2;
6385         if (vers == NFS_VER3)
6386                 *tl++ = txdr_unsigned(len);
6387         *tl++ = txdr_unsigned(*iomode);
6388         *tl = txdr_unsigned(len);
6389         if (len > 0) {
6390                 /* Put data in mbuf chain. */
6391                 nd->nd_mb->m_next = m;
6392                 /* Set nd_mb and nd_bpos to end of data. */
6393                 while (m->m_next != NULL)
6394                         m = m->m_next;
6395                 nd->nd_mb = m;
6396                 nd->nd_bpos = mtod(m, char *) + m->m_len;
6397                 NFSCL_DEBUG(4, "nfsrpc_writedsmir: lastmb len=%d\n", m->m_len);
6398         }
6399         nrp = dsp->nfsclds_sockp;
6400         if (nrp == NULL)
6401                 /* If NULL, use the MDS socket. */
6402                 nrp = &nmp->nm_sockreq;
6403         error = newnfs_request(nd, nmp, NULL, nrp, vp, p, cred,
6404             NFS_PROG, vers, NULL, 1, NULL, &dsp->nfsclds_sess);
6405         NFSCL_DEBUG(4, "nfsrpc_writedsmir: err=%d stat=%d\n", error,
6406             nd->nd_repstat);
6407         if (error != 0)
6408                 return (error);
6409         if (nd->nd_repstat != 0)
6410                 error = nd->nd_repstat;
6411         else {
6412                 if (vers == NFS_VER3) {
6413                         error = nfscl_wcc_data(nd, vp, &na, &attrflag, NULL,
6414                             NULL);
6415                         NFSCL_DEBUG(4, "nfsrpc_writedsmir: wcc_data=%d\n",
6416                             error);
6417                         if (error != 0)
6418                                 goto nfsmout;
6419                 }
6420                 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED + NFSX_VERF);
6421                 rlen = fxdr_unsigned(int, *tl++);
6422                 NFSCL_DEBUG(4, "nfsrpc_writedsmir: len=%d rlen=%d\n", len,
6423                     rlen);
6424                 if (rlen != len) {
6425                         error = NFSERR_IO;
6426                         NFSCL_DEBUG(4, "nfsrpc_writedsmir: len=%d rlen=%d\n",
6427                             len, rlen);
6428                         goto nfsmout;
6429                 }
6430                 commit = fxdr_unsigned(int, *tl++);
6431
6432                 /*
6433                  * Return the lowest commitment level
6434                  * obtained by any of the RPCs.
6435                  */
6436                 if (committed == NFSWRITE_FILESYNC)
6437                         committed = commit;
6438                 else if (committed == NFSWRITE_DATASYNC &&
6439                     commit == NFSWRITE_UNSTABLE)
6440                         committed = commit;
6441                 NFSLOCKDS(dsp);
6442                 if ((dsp->nfsclds_flags & NFSCLDS_HASWRITEVERF) == 0) {
6443                         NFSBCOPY(tl, dsp->nfsclds_verf, NFSX_VERF);
6444                         dsp->nfsclds_flags |= NFSCLDS_HASWRITEVERF;
6445                 } else if (NFSBCMP(tl, dsp->nfsclds_verf, NFSX_VERF)) {
6446                         *must_commit = 1;
6447                         NFSBCOPY(tl, dsp->nfsclds_verf, NFSX_VERF);
6448                 }
6449                 NFSUNLOCKDS(dsp);
6450         }
6451 nfsmout:
6452         if (nd->nd_mrep != NULL)
6453                 mbuf_freem(nd->nd_mrep);
6454         *iomode = committed;
6455         if (nd->nd_repstat != 0 && error == 0)
6456                 error = nd->nd_repstat;
6457         return (error);
6458 }
6459
6460 /*
6461  * Start up the thread that will execute nfsrpc_writedsmir().
6462  */
6463 static void
6464 start_writedsmir(void *arg, int pending)
6465 {
6466         struct nfsclwritedsdorpc *drpc;
6467
6468         drpc = (struct nfsclwritedsdorpc *)arg;
6469         drpc->err = nfsrpc_writedsmir(drpc->vp, &drpc->iomode,
6470             &drpc->must_commit, drpc->stateidp, drpc->dsp, drpc->off, drpc->len,
6471             drpc->fhp, drpc->m, drpc->vers, drpc->minorvers, drpc->cred,
6472             drpc->p);
6473         drpc->done = 1;
6474         NFSCL_DEBUG(4, "start_writedsmir: err=%d\n", drpc->err);
6475 }
6476
6477 /*
6478  * Set up the write DS mirror call for the pNFS I/O thread.
6479  */
6480 static int
6481 nfsio_writedsmir(vnode_t vp, int *iomode, int *must_commit,
6482     nfsv4stateid_t *stateidp, struct nfsclds *dsp, uint64_t off, int len,
6483     struct nfsfh *fhp, struct mbuf *m, int vers, int minorvers,
6484     struct nfsclwritedsdorpc *drpc, struct ucred *cred, NFSPROC_T *p)
6485 {
6486         int error, ret;
6487
6488         error = 0;
6489         drpc->done = 0;
6490         drpc->vp = vp;
6491         drpc->iomode = *iomode;
6492         drpc->must_commit = *must_commit;
6493         drpc->stateidp = stateidp;
6494         drpc->dsp = dsp;
6495         drpc->off = off;
6496         drpc->len = len;
6497         drpc->fhp = fhp;
6498         drpc->m = m;
6499         drpc->vers = vers;
6500         drpc->minorvers = minorvers;
6501         drpc->cred = cred;
6502         drpc->p = p;
6503         drpc->inprog = 0;
6504         ret = EIO;
6505         if (nfs_pnfsiothreads != 0) {
6506                 ret = nfs_pnfsio(start_writedsmir, drpc);
6507                 NFSCL_DEBUG(4, "nfsio_writedsmir: nfs_pnfsio=%d\n", ret);
6508         }
6509         if (ret != 0)
6510                 error = nfsrpc_writedsmir(vp, iomode, must_commit, stateidp,
6511                     dsp, off, len, fhp, m, vers, minorvers, cred, p);
6512         NFSCL_DEBUG(4, "nfsio_writedsmir: error=%d\n", error);
6513         return (error);
6514 }
6515
6516 /*
6517  * Free up the nfsclds structure.
6518  */
6519 void
6520 nfscl_freenfsclds(struct nfsclds *dsp)
6521 {
6522         int i;
6523
6524         if (dsp == NULL)
6525                 return;
6526         if (dsp->nfsclds_sockp != NULL) {
6527                 NFSFREECRED(dsp->nfsclds_sockp->nr_cred);
6528                 NFSFREEMUTEX(&dsp->nfsclds_sockp->nr_mtx);
6529                 free(dsp->nfsclds_sockp->nr_nam, M_SONAME);
6530                 free(dsp->nfsclds_sockp, M_NFSSOCKREQ);
6531         }
6532         NFSFREEMUTEX(&dsp->nfsclds_mtx);
6533         NFSFREEMUTEX(&dsp->nfsclds_sess.nfsess_mtx);
6534         for (i = 0; i < NFSV4_CBSLOTS; i++) {
6535                 if (dsp->nfsclds_sess.nfsess_cbslots[i].nfssl_reply != NULL)
6536                         m_freem(
6537                             dsp->nfsclds_sess.nfsess_cbslots[i].nfssl_reply);
6538         }
6539         free(dsp, M_NFSCLDS);
6540 }
6541
6542 static enum nfsclds_state
6543 nfscl_getsameserver(struct nfsmount *nmp, struct nfsclds *newdsp,
6544     struct nfsclds **retdspp, uint32_t *sequencep)
6545 {
6546         struct nfsclds *dsp;
6547         int fndseq;
6548
6549         /*
6550          * Search the list of nfsclds structures for one with the same
6551          * server.
6552          */
6553         fndseq = 0;
6554         TAILQ_FOREACH(dsp, &nmp->nm_sess, nfsclds_list) {
6555                 if (dsp->nfsclds_servownlen == newdsp->nfsclds_servownlen &&
6556                     dsp->nfsclds_servownlen != 0 &&
6557                     !NFSBCMP(dsp->nfsclds_serverown, newdsp->nfsclds_serverown,
6558                     dsp->nfsclds_servownlen) &&
6559                     dsp->nfsclds_sess.nfsess_defunct == 0) {
6560                         NFSCL_DEBUG(4, "fnd same fdsp=%p dsp=%p flg=0x%x\n",
6561                             TAILQ_FIRST(&nmp->nm_sess), dsp,
6562                             dsp->nfsclds_flags);
6563                         if (fndseq == 0) {
6564                                 /* Get sequenceid# from first entry. */
6565                                 *sequencep =
6566                                     dsp->nfsclds_sess.nfsess_sequenceid;
6567                                 fndseq = 1;
6568                         }
6569                         /* Server major id matches. */
6570                         if ((dsp->nfsclds_flags & NFSCLDS_DS) != 0) {
6571                                 *retdspp = dsp;
6572                                 return (NFSDSP_USETHISSESSION);
6573                         }
6574
6575                 }
6576         }
6577         if (fndseq != 0)
6578                 return (NFSDSP_SEQTHISSESSION);
6579         return (NFSDSP_NOTFOUND);
6580 }
6581
6582 /*
6583  * NFS commit rpc to a NFSv4.1 DS.
6584  */
6585 static int
6586 nfsrpc_commitds(vnode_t vp, uint64_t offset, int cnt, struct nfsclds *dsp,
6587     struct nfsfh *fhp, int vers, int minorvers, struct ucred *cred,
6588     NFSPROC_T *p)
6589 {
6590         uint32_t *tl;
6591         struct nfsrv_descript nfsd, *nd = &nfsd;
6592         struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
6593         struct nfssockreq *nrp;
6594         struct nfsvattr na;
6595         int attrflag, error;
6596         
6597         nd->nd_mrep = NULL;
6598         if (vers == 0 || vers == NFS_VER4) {
6599                 nfscl_reqstart(nd, NFSPROC_COMMITDS, nmp, fhp->nfh_fh,
6600                     fhp->nfh_len, NULL, &dsp->nfsclds_sess, vers, minorvers);
6601                 vers = NFS_VER4;
6602         } else
6603                 nfscl_reqstart(nd, NFSPROC_COMMIT, nmp, fhp->nfh_fh,
6604                     fhp->nfh_len, NULL, &dsp->nfsclds_sess, vers, minorvers);
6605         NFSCL_DEBUG(4, "nfsrpc_commitds: vers=%d minvers=%d\n", vers,
6606             minorvers);
6607         NFSM_BUILD(tl, uint32_t *, NFSX_HYPER + NFSX_UNSIGNED);
6608         txdr_hyper(offset, tl);
6609         tl += 2;
6610         *tl = txdr_unsigned(cnt);
6611         nrp = dsp->nfsclds_sockp;
6612         if (nrp == NULL)
6613                 /* If NULL, use the MDS socket. */
6614                 nrp = &nmp->nm_sockreq;
6615         error = newnfs_request(nd, nmp, NULL, nrp, vp, p, cred,
6616             NFS_PROG, vers, NULL, 1, NULL, &dsp->nfsclds_sess);
6617         NFSCL_DEBUG(4, "nfsrpc_commitds: err=%d stat=%d\n", error,
6618             nd->nd_repstat);
6619         if (error != 0)
6620                 return (error);
6621         if (nd->nd_repstat == 0) {
6622                 if (vers == NFS_VER3) {
6623                         error = nfscl_wcc_data(nd, vp, &na, &attrflag, NULL,
6624                             NULL);
6625                         NFSCL_DEBUG(4, "nfsrpc_commitds: wccdata=%d\n", error);
6626                         if (error != 0)
6627                                 goto nfsmout;
6628                 }
6629                 NFSM_DISSECT(tl, u_int32_t *, NFSX_VERF);
6630                 NFSLOCKDS(dsp);
6631                 if (NFSBCMP(tl, dsp->nfsclds_verf, NFSX_VERF)) {
6632                         NFSBCOPY(tl, dsp->nfsclds_verf, NFSX_VERF);
6633                         error = NFSERR_STALEWRITEVERF;
6634                 }
6635                 NFSUNLOCKDS(dsp);
6636         }
6637 nfsmout:
6638         if (error == 0 && nd->nd_repstat != 0)
6639                 error = nd->nd_repstat;
6640         mbuf_freem(nd->nd_mrep);
6641         return (error);
6642 }
6643
6644 /*
6645  * Start up the thread that will execute nfsrpc_commitds().
6646  */
6647 static void
6648 start_commitds(void *arg, int pending)
6649 {
6650         struct nfsclwritedsdorpc *drpc;
6651
6652         drpc = (struct nfsclwritedsdorpc *)arg;
6653         drpc->err = nfsrpc_commitds(drpc->vp, drpc->off, drpc->len,
6654             drpc->dsp, drpc->fhp, drpc->vers, drpc->minorvers, drpc->cred,
6655             drpc->p);
6656         drpc->done = 1;
6657         NFSCL_DEBUG(4, "start_commitds: err=%d\n", drpc->err);
6658 }
6659
6660 /*
6661  * Set up the commit DS mirror call for the pNFS I/O thread.
6662  */
6663 static int
6664 nfsio_commitds(vnode_t vp, uint64_t offset, int cnt, struct nfsclds *dsp,
6665     struct nfsfh *fhp, int vers, int minorvers,
6666     struct nfsclwritedsdorpc *drpc, struct ucred *cred, NFSPROC_T *p)
6667 {
6668         int error, ret;
6669
6670         error = 0;
6671         drpc->done = 0;
6672         drpc->vp = vp;
6673         drpc->off = offset;
6674         drpc->len = cnt;
6675         drpc->dsp = dsp;
6676         drpc->fhp = fhp;
6677         drpc->vers = vers;
6678         drpc->minorvers = minorvers;
6679         drpc->cred = cred;
6680         drpc->p = p;
6681         drpc->inprog = 0;
6682         ret = EIO;
6683         if (nfs_pnfsiothreads != 0) {
6684                 ret = nfs_pnfsio(start_commitds, drpc);
6685                 NFSCL_DEBUG(4, "nfsio_commitds: nfs_pnfsio=%d\n", ret);
6686         }
6687         if (ret != 0)
6688                 error = nfsrpc_commitds(vp, offset, cnt, dsp, fhp, vers,
6689                     minorvers, cred, p);
6690         NFSCL_DEBUG(4, "nfsio_commitds: error=%d\n", error);
6691         return (error);
6692 }
6693
6694 /*
6695  * Set up the XDR arguments for the LayoutGet operation.
6696  */
6697 static void
6698 nfsrv_setuplayoutget(struct nfsrv_descript *nd, int iomode, uint64_t offset,
6699     uint64_t len, uint64_t minlen, nfsv4stateid_t *stateidp, int layouttype,
6700     int layoutlen, int usecurstateid)
6701 {
6702         uint32_t *tl;
6703
6704         NFSM_BUILD(tl, uint32_t *, 4 * NFSX_UNSIGNED + 3 * NFSX_HYPER +
6705             NFSX_STATEID);
6706         *tl++ = newnfs_false;           /* Don't signal availability. */
6707         *tl++ = txdr_unsigned(layouttype);
6708         *tl++ = txdr_unsigned(iomode);
6709         txdr_hyper(offset, tl);
6710         tl += 2;
6711         txdr_hyper(len, tl);
6712         tl += 2;
6713         txdr_hyper(minlen, tl);
6714         tl += 2;
6715         if (usecurstateid != 0) {
6716                 /* Special stateid for Current stateid. */
6717                 *tl++ = txdr_unsigned(1);
6718                 *tl++ = 0;
6719                 *tl++ = 0;
6720                 *tl++ = 0;
6721         } else {
6722                 *tl++ = txdr_unsigned(stateidp->seqid);
6723                 NFSCL_DEBUG(4, "layget seq=%d\n", (int)stateidp->seqid);
6724                 *tl++ = stateidp->other[0];
6725                 *tl++ = stateidp->other[1];
6726                 *tl++ = stateidp->other[2];
6727         }
6728         *tl = txdr_unsigned(layoutlen);
6729 }
6730
6731 /*
6732  * Parse the reply for a successful LayoutGet operation.
6733  */
6734 static int
6735 nfsrv_parselayoutget(struct nfsrv_descript *nd, nfsv4stateid_t *stateidp,
6736     int *retonclosep, struct nfsclflayouthead *flhp)
6737 {
6738         uint32_t *tl;
6739         struct nfsclflayout *flp, *prevflp, *tflp;
6740         int cnt, error, fhcnt, gotiomode, i, iomode, j, k, l, laytype, nfhlen;
6741         int m, mirrorcnt;
6742         uint64_t retlen, off;
6743         struct nfsfh *nfhp;
6744         uint8_t *cp;
6745         uid_t user;
6746         gid_t grp;
6747
6748         NFSCL_DEBUG(4, "in nfsrv_parselayoutget\n");
6749         error = 0;
6750         flp = NULL;
6751         gotiomode = -1;
6752         NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED + NFSX_STATEID);
6753         if (*tl++ != 0)
6754                 *retonclosep = 1;
6755         else
6756                 *retonclosep = 0;
6757         stateidp->seqid = fxdr_unsigned(uint32_t, *tl++);
6758         NFSCL_DEBUG(4, "retoncls=%d stseq=%d\n", *retonclosep,
6759             (int)stateidp->seqid);
6760         stateidp->other[0] = *tl++;
6761         stateidp->other[1] = *tl++;
6762         stateidp->other[2] = *tl++;
6763         cnt = fxdr_unsigned(int, *tl);
6764         NFSCL_DEBUG(4, "layg cnt=%d\n", cnt);
6765         if (cnt <= 0 || cnt > 10000) {
6766                 /* Don't accept more than 10000 layouts in reply. */
6767                 error = NFSERR_BADXDR;
6768                 goto nfsmout;
6769         }
6770         for (i = 0; i < cnt; i++) {
6771                 /* Dissect to the layout type. */
6772                 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_HYPER +
6773                     3 * NFSX_UNSIGNED);
6774                 off = fxdr_hyper(tl); tl += 2;
6775                 retlen = fxdr_hyper(tl); tl += 2;
6776                 iomode = fxdr_unsigned(int, *tl++);
6777                 laytype = fxdr_unsigned(int, *tl);
6778                 NFSCL_DEBUG(4, "layt=%d off=%ju len=%ju iom=%d\n", laytype,
6779                     (uintmax_t)off, (uintmax_t)retlen, iomode);
6780                 /* Ignore length of layout body for now. */
6781                 if (laytype == NFSLAYOUT_NFSV4_1_FILES) {
6782                         /* Parse the File layout up to fhcnt. */
6783                         NFSM_DISSECT(tl, uint32_t *, 3 * NFSX_UNSIGNED +
6784                             NFSX_HYPER + NFSX_V4DEVICEID);
6785                         fhcnt = fxdr_unsigned(int, *(tl + 4 +
6786                             NFSX_V4DEVICEID / NFSX_UNSIGNED));
6787                         NFSCL_DEBUG(4, "fhcnt=%d\n", fhcnt);
6788                         if (fhcnt < 0 || fhcnt > 100) {
6789                                 /* Don't accept more than 100 file handles. */
6790                                 error = NFSERR_BADXDR;
6791                                 goto nfsmout;
6792                         }
6793                         if (fhcnt > 0)
6794                                 flp = malloc(sizeof(*flp) + fhcnt *
6795                                     sizeof(struct nfsfh *), M_NFSFLAYOUT,
6796                                     M_WAITOK);
6797                         else
6798                                 flp = malloc(sizeof(*flp), M_NFSFLAYOUT,
6799                                     M_WAITOK);
6800                         flp->nfsfl_flags = NFSFL_FILE;
6801                         flp->nfsfl_fhcnt = 0;
6802                         flp->nfsfl_devp = NULL;
6803                         flp->nfsfl_off = off;
6804                         if (flp->nfsfl_off + retlen < flp->nfsfl_off)
6805                                 flp->nfsfl_end = UINT64_MAX - flp->nfsfl_off;
6806                         else
6807                                 flp->nfsfl_end = flp->nfsfl_off + retlen;
6808                         flp->nfsfl_iomode = iomode;
6809                         if (gotiomode == -1)
6810                                 gotiomode = flp->nfsfl_iomode;
6811                         /* Ignore layout body length for now. */
6812                         NFSBCOPY(tl, flp->nfsfl_dev, NFSX_V4DEVICEID);
6813                         tl += (NFSX_V4DEVICEID / NFSX_UNSIGNED);
6814                         flp->nfsfl_util = fxdr_unsigned(uint32_t, *tl++);
6815                         NFSCL_DEBUG(4, "flutil=0x%x\n", flp->nfsfl_util);
6816                         flp->nfsfl_stripe1 = fxdr_unsigned(uint32_t, *tl++);
6817                         flp->nfsfl_patoff = fxdr_hyper(tl); tl += 2;
6818                         NFSCL_DEBUG(4, "stripe1=%u poff=%ju\n",
6819                             flp->nfsfl_stripe1, (uintmax_t)flp->nfsfl_patoff);
6820                         for (j = 0; j < fhcnt; j++) {
6821                                 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
6822                                 nfhlen = fxdr_unsigned(int, *tl);
6823                                 if (nfhlen <= 0 || nfhlen > NFSX_V4FHMAX) {
6824                                         error = NFSERR_BADXDR;
6825                                         goto nfsmout;
6826                                 }
6827                                 nfhp = malloc(sizeof(*nfhp) + nfhlen - 1,
6828                                     M_NFSFH, M_WAITOK);
6829                                 flp->nfsfl_fh[j] = nfhp;
6830                                 flp->nfsfl_fhcnt++;
6831                                 nfhp->nfh_len = nfhlen;
6832                                 NFSM_DISSECT(cp, uint8_t *, NFSM_RNDUP(nfhlen));
6833                                 NFSBCOPY(cp, nfhp->nfh_fh, nfhlen);
6834                         }
6835                 } else if (laytype == NFSLAYOUT_FLEXFILE) {
6836                         NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED +
6837                             NFSX_HYPER);
6838                         mirrorcnt = fxdr_unsigned(int, *(tl + 2));
6839                         NFSCL_DEBUG(4, "mirrorcnt=%d\n", mirrorcnt);
6840                         if (mirrorcnt < 1 || mirrorcnt > NFSDEV_MAXMIRRORS) {
6841                                 error = NFSERR_BADXDR;
6842                                 goto nfsmout;
6843                         }
6844                         flp = malloc(sizeof(*flp) + mirrorcnt *
6845                             sizeof(struct nfsffm), M_NFSFLAYOUT, M_WAITOK);
6846                         flp->nfsfl_flags = NFSFL_FLEXFILE;
6847                         flp->nfsfl_mirrorcnt = mirrorcnt;
6848                         for (j = 0; j < mirrorcnt; j++)
6849                                 flp->nfsfl_ffm[j].devp = NULL;
6850                         flp->nfsfl_off = off;
6851                         if (flp->nfsfl_off + retlen < flp->nfsfl_off)
6852                                 flp->nfsfl_end = UINT64_MAX - flp->nfsfl_off;
6853                         else
6854                                 flp->nfsfl_end = flp->nfsfl_off + retlen;
6855                         flp->nfsfl_iomode = iomode;
6856                         if (gotiomode == -1)
6857                                 gotiomode = flp->nfsfl_iomode;
6858                         flp->nfsfl_stripeunit = fxdr_hyper(tl);
6859                         NFSCL_DEBUG(4, "stripeunit=%ju\n",
6860                             (uintmax_t)flp->nfsfl_stripeunit);
6861                         for (j = 0; j < mirrorcnt; j++) {
6862                                 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
6863                                 k = fxdr_unsigned(int, *tl);
6864                                 if (k < 1 || k > 128) {
6865                                         error = NFSERR_BADXDR;
6866                                         goto nfsmout;
6867                                 }
6868                                 NFSCL_DEBUG(4, "servercnt=%d\n", k);
6869                                 for (l = 0; l < k; l++) {
6870                                         NFSM_DISSECT(tl, uint32_t *,
6871                                             NFSX_V4DEVICEID + NFSX_STATEID +
6872                                             2 * NFSX_UNSIGNED);
6873                                         if (l == 0) {
6874                                                 /* Just use the first server. */
6875                                                 NFSBCOPY(tl,
6876                                                     flp->nfsfl_ffm[j].dev,
6877                                                     NFSX_V4DEVICEID);
6878                                                 tl += (NFSX_V4DEVICEID /
6879                                                     NFSX_UNSIGNED);
6880                                                 tl++;
6881                                                 flp->nfsfl_ffm[j].st.seqid =
6882                                                     *tl++;
6883                                                 flp->nfsfl_ffm[j].st.other[0] =
6884                                                     *tl++;
6885                                                 flp->nfsfl_ffm[j].st.other[1] =
6886                                                     *tl++;
6887                                                 flp->nfsfl_ffm[j].st.other[2] =
6888                                                     *tl++;
6889                                                 NFSCL_DEBUG(4, "st.seqid=%u "
6890                                                  "st.o0=0x%x st.o1=0x%x "
6891                                                  "st.o2=0x%x\n",
6892                                                  flp->nfsfl_ffm[j].st.seqid,
6893                                                  flp->nfsfl_ffm[j].st.other[0],
6894                                                  flp->nfsfl_ffm[j].st.other[1],
6895                                                  flp->nfsfl_ffm[j].st.other[2]);
6896                                         } else
6897                                                 tl += ((NFSX_V4DEVICEID +
6898                                                     NFSX_STATEID +
6899                                                     NFSX_UNSIGNED) /
6900                                                     NFSX_UNSIGNED);
6901                                         fhcnt = fxdr_unsigned(int, *tl);
6902                                         NFSCL_DEBUG(4, "fhcnt=%d\n", fhcnt);
6903                                         if (fhcnt < 1 ||
6904                                             fhcnt > NFSDEV_MAXVERS) {
6905                                                 error = NFSERR_BADXDR;
6906                                                 goto nfsmout;
6907                                         }
6908                                         for (m = 0; m < fhcnt; m++) {
6909                                                 NFSM_DISSECT(tl, uint32_t *,
6910                                                     NFSX_UNSIGNED);
6911                                                 nfhlen = fxdr_unsigned(int,
6912                                                     *tl);
6913                                                 NFSCL_DEBUG(4, "nfhlen=%d\n",
6914                                                     nfhlen);
6915                                                 if (nfhlen <= 0 || nfhlen >
6916                                                     NFSX_V4FHMAX) {
6917                                                         error = NFSERR_BADXDR;
6918                                                         goto nfsmout;
6919                                                 }
6920                                                 NFSM_DISSECT(cp, uint8_t *,
6921                                                     NFSM_RNDUP(nfhlen));
6922                                                 if (l == 0) {
6923                                                         flp->nfsfl_ffm[j].fhcnt 
6924                                                             = fhcnt;
6925                                                         nfhp = malloc(
6926                                                             sizeof(*nfhp) +
6927                                                             nfhlen - 1, M_NFSFH,
6928                                                             M_WAITOK);
6929                                                         flp->nfsfl_ffm[j].fh[m]
6930                                                             = nfhp;
6931                                                         nfhp->nfh_len = nfhlen;
6932                                                         NFSBCOPY(cp,
6933                                                             nfhp->nfh_fh,
6934                                                             nfhlen);
6935                                                         NFSCL_DEBUG(4,
6936                                                             "got fh\n");
6937                                                 }
6938                                         }
6939                                         /* Now, get the ffsd_user/ffds_group. */
6940                                         error = nfsrv_parseug(nd, 0, &user,
6941                                             &grp, curthread);
6942                                         NFSCL_DEBUG(4, "after parseu=%d\n",
6943                                             error);
6944                                         if (error == 0)
6945                                                 error = nfsrv_parseug(nd, 1,
6946                                                     &user, &grp, curthread);
6947                                         NFSCL_DEBUG(4, "aft parseg=%d\n",
6948                                             grp);
6949                                         if (error != 0)
6950                                                 goto nfsmout;
6951                                         NFSCL_DEBUG(4, "user=%d group=%d\n",
6952                                             user, grp);
6953                                         if (l == 0) {
6954                                                 flp->nfsfl_ffm[j].user = user;
6955                                                 flp->nfsfl_ffm[j].group = grp;
6956                                                 NFSCL_DEBUG(4,
6957                                                     "usr=%d grp=%d\n", user,
6958                                                     grp);
6959                                         }
6960                                 }
6961                         }
6962                         NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
6963                         flp->nfsfl_fflags = fxdr_unsigned(uint32_t, *tl++);
6964                         flp->nfsfl_statshint = fxdr_unsigned(uint32_t, *tl);
6965                         NFSCL_DEBUG(4, "fflags=0x%x statshint=%d\n",
6966                             flp->nfsfl_fflags, flp->nfsfl_statshint);
6967                 } else {
6968                         error = NFSERR_BADXDR;
6969                         goto nfsmout;
6970                 }
6971                 if (flp->nfsfl_iomode == gotiomode) {
6972                         /* Keep the list in increasing offset order. */
6973                         tflp = LIST_FIRST(flhp);
6974                         prevflp = NULL;
6975                         while (tflp != NULL &&
6976                             tflp->nfsfl_off < flp->nfsfl_off) {
6977                                 prevflp = tflp;
6978                                 tflp = LIST_NEXT(tflp, nfsfl_list);
6979                         }
6980                         if (prevflp == NULL)
6981                                 LIST_INSERT_HEAD(flhp, flp, nfsfl_list);
6982                         else
6983                                 LIST_INSERT_AFTER(prevflp, flp,
6984                                     nfsfl_list);
6985                         NFSCL_DEBUG(4, "flp inserted\n");
6986                 } else {
6987                         printf("nfscl_layoutget(): got wrong iomode\n");
6988                         nfscl_freeflayout(flp);
6989                 }
6990                 flp = NULL;
6991         }
6992 nfsmout:
6993         NFSCL_DEBUG(4, "eo nfsrv_parselayoutget=%d\n", error);
6994         if (error != 0 && flp != NULL)
6995                 nfscl_freeflayout(flp);
6996         return (error);
6997 }
6998
6999 /*
7000  * Parse a user/group digit string.
7001  */
7002 static int
7003 nfsrv_parseug(struct nfsrv_descript *nd, int dogrp, uid_t *uidp, gid_t *gidp,
7004     NFSPROC_T *p)
7005 {
7006         uint32_t *tl;
7007         char *cp, *str, str0[NFSV4_SMALLSTR + 1];
7008         uint32_t len = 0;
7009         int error = 0;
7010
7011         NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
7012         len = fxdr_unsigned(uint32_t, *tl);
7013         str = NULL;
7014         if (len > NFSV4_OPAQUELIMIT) {
7015                 error = NFSERR_BADXDR;
7016                 goto nfsmout;
7017         }
7018         NFSCL_DEBUG(4, "nfsrv_parseug: len=%d\n", len);
7019         if (len == 0) {
7020                 if (dogrp != 0)
7021                         *gidp = GID_NOGROUP;
7022                 else
7023                         *uidp = UID_NOBODY;
7024                 return (0);
7025         }
7026         if (len > NFSV4_SMALLSTR)
7027                 str = malloc(len + 1, M_TEMP, M_WAITOK);
7028         else
7029                 str = str0;
7030         NFSM_DISSECT(cp, char *, NFSM_RNDUP(len));
7031         NFSBCOPY(cp, str, len);
7032         str[len] = '\0';
7033         NFSCL_DEBUG(4, "nfsrv_parseug: str=%s\n", str);
7034         if (dogrp != 0)
7035                 error = nfsv4_strtogid(nd, str, len, gidp, p);
7036         else
7037                 error = nfsv4_strtouid(nd, str, len, uidp, p);
7038 nfsmout:
7039         if (len > NFSV4_SMALLSTR)
7040                 free(str, M_TEMP);
7041         NFSCL_DEBUG(4, "eo nfsrv_parseug=%d\n", error);
7042         return (error);
7043 }
7044
7045 /*
7046  * Similar to nfsrpc_getlayout(), except that it uses nfsrpc_openlayget(),
7047  * so that it does both an Open and a Layoutget.
7048  */
7049 static int
7050 nfsrpc_getopenlayout(struct nfsmount *nmp, vnode_t vp, u_int8_t *nfhp,
7051     int fhlen, uint8_t *newfhp, int newfhlen, uint32_t mode,
7052     struct nfsclopen *op, uint8_t *name, int namelen, struct nfscldeleg **dpp,
7053     struct ucred *cred, NFSPROC_T *p)
7054 {
7055         struct nfscllayout *lyp;
7056         struct nfsclflayout *flp;
7057         struct nfsclflayouthead flh;
7058         int error, islocked, layoutlen, recalled, retonclose, usecurstateid;
7059         int layouttype, laystat;
7060         nfsv4stateid_t stateid;
7061         struct nfsclsession *tsep;
7062
7063         error = 0;
7064         if (NFSHASFLEXFILE(nmp))
7065                 layouttype = NFSLAYOUT_FLEXFILE;
7066         else
7067                 layouttype = NFSLAYOUT_NFSV4_1_FILES;
7068         /*
7069          * If lyp is returned non-NULL, there will be a refcnt (shared lock)
7070          * on it, iff flp != NULL or a lock (exclusive lock) on it iff
7071          * flp == NULL.
7072          */
7073         lyp = nfscl_getlayout(nmp->nm_clp, newfhp, newfhlen, 0, &flp,
7074             &recalled);
7075         NFSCL_DEBUG(4, "nfsrpc_getopenlayout nfscl_getlayout lyp=%p\n", lyp);
7076         if (lyp == NULL)
7077                 islocked = 0;
7078         else if (flp != NULL)
7079                 islocked = 1;
7080         else
7081                 islocked = 2;
7082         if ((lyp == NULL || flp == NULL) && recalled == 0) {
7083                 LIST_INIT(&flh);
7084                 tsep = nfsmnt_mdssession(nmp);
7085                 layoutlen = tsep->nfsess_maxcache - (NFSX_STATEID +
7086                     3 * NFSX_UNSIGNED);
7087                 if (lyp == NULL)
7088                         usecurstateid = 1;
7089                 else {
7090                         usecurstateid = 0;
7091                         stateid.seqid = lyp->nfsly_stateid.seqid;
7092                         stateid.other[0] = lyp->nfsly_stateid.other[0];
7093                         stateid.other[1] = lyp->nfsly_stateid.other[1];
7094                         stateid.other[2] = lyp->nfsly_stateid.other[2];
7095                 }
7096                 error = nfsrpc_openlayoutrpc(nmp, vp, nfhp, fhlen,
7097                     newfhp, newfhlen, mode, op, name, namelen,
7098                     dpp, &stateid, usecurstateid, layouttype, layoutlen,
7099                     &retonclose, &flh, &laystat, cred, p);
7100                 NFSCL_DEBUG(4, "aft nfsrpc_openlayoutrpc laystat=%d err=%d\n",
7101                     laystat, error);
7102                 laystat = nfsrpc_layoutgetres(nmp, vp, newfhp, newfhlen,
7103                     &stateid, retonclose, NULL, &lyp, &flh, layouttype, laystat,
7104                     &islocked, cred, p);
7105         } else
7106                 error = nfsrpc_openrpc(nmp, vp, nfhp, fhlen, newfhp, newfhlen,
7107                     mode, op, name, namelen, dpp, 0, 0, cred, p, 0, 0);
7108         if (islocked == 2)
7109                 nfscl_rellayout(lyp, 1);
7110         else if (islocked == 1)
7111                 nfscl_rellayout(lyp, 0);
7112         return (error);
7113 }
7114
7115 /*
7116  * This function does an Open+LayoutGet for an NFSv4.1 mount with pNFS
7117  * enabled, only for the CLAIM_NULL case.  All other NFSv4 Opens are
7118  * handled by nfsrpc_openrpc().
7119  * For the case where op == NULL, dvp is the directory.  When op != NULL, it
7120  * can be NULL.
7121  */
7122 static int
7123 nfsrpc_openlayoutrpc(struct nfsmount *nmp, vnode_t vp, u_int8_t *nfhp,
7124     int fhlen, uint8_t *newfhp, int newfhlen, uint32_t mode,
7125     struct nfsclopen *op, uint8_t *name, int namelen, struct nfscldeleg **dpp,
7126     nfsv4stateid_t *stateidp, int usecurstateid, int layouttype,
7127     int layoutlen, int *retonclosep, struct nfsclflayouthead *flhp,
7128     int *laystatp, struct ucred *cred, NFSPROC_T *p)
7129 {
7130         uint32_t *tl;
7131         struct nfsrv_descript nfsd, *nd = &nfsd;
7132         struct nfscldeleg *ndp = NULL;
7133         struct nfsvattr nfsva;
7134         struct nfsclsession *tsep;
7135         uint32_t rflags, deleg;
7136         nfsattrbit_t attrbits;
7137         int error, ret, acesize, limitby, iomode;
7138
7139         *dpp = NULL;
7140         *laystatp = ENXIO;
7141         nfscl_reqstart(nd, NFSPROC_OPENLAYGET, nmp, nfhp, fhlen, NULL, NULL,
7142             0, 0);
7143         NFSM_BUILD(tl, uint32_t *, 5 * NFSX_UNSIGNED);
7144         *tl++ = txdr_unsigned(op->nfso_own->nfsow_seqid);
7145         *tl++ = txdr_unsigned(mode & NFSV4OPEN_ACCESSBOTH);
7146         *tl++ = txdr_unsigned((mode >> NFSLCK_SHIFT) & NFSV4OPEN_DENYBOTH);
7147         tsep = nfsmnt_mdssession(nmp);
7148         *tl++ = tsep->nfsess_clientid.lval[0];
7149         *tl = tsep->nfsess_clientid.lval[1];
7150         nfsm_strtom(nd, op->nfso_own->nfsow_owner, NFSV4CL_LOCKNAMELEN);
7151         NFSM_BUILD(tl, uint32_t *, 2 * NFSX_UNSIGNED);
7152         *tl++ = txdr_unsigned(NFSV4OPEN_NOCREATE);
7153         *tl = txdr_unsigned(NFSV4OPEN_CLAIMNULL);
7154         nfsm_strtom(nd, name, namelen);
7155         NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
7156         *tl = txdr_unsigned(NFSV4OP_GETATTR);
7157         NFSZERO_ATTRBIT(&attrbits);
7158         NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_CHANGE);
7159         NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEMODIFY);
7160         nfsrv_putattrbit(nd, &attrbits);
7161         NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
7162         *tl = txdr_unsigned(NFSV4OP_LAYOUTGET);
7163         if ((mode & NFSV4OPEN_ACCESSWRITE) != 0)
7164                 iomode = NFSLAYOUTIOMODE_RW;
7165         else
7166                 iomode = NFSLAYOUTIOMODE_READ;
7167         nfsrv_setuplayoutget(nd, iomode, 0, UINT64_MAX, 0, stateidp,
7168             layouttype, layoutlen, usecurstateid);
7169         error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, vp, p, cred,
7170             NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
7171         if (error != 0)
7172                 return (error);
7173         NFSCL_INCRSEQID(op->nfso_own->nfsow_seqid, nd);
7174         if (nd->nd_repstat != 0)
7175                 *laystatp = nd->nd_repstat;
7176         if ((nd->nd_flag & ND_NOMOREDATA) == 0) {
7177                 /* ND_NOMOREDATA will be set if the Open operation failed. */
7178                 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
7179                     6 * NFSX_UNSIGNED);
7180                 op->nfso_stateid.seqid = *tl++;
7181                 op->nfso_stateid.other[0] = *tl++;
7182                 op->nfso_stateid.other[1] = *tl++;
7183                 op->nfso_stateid.other[2] = *tl;
7184                 rflags = fxdr_unsigned(u_int32_t, *(tl + 6));
7185                 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
7186                 if (error != 0)
7187                         goto nfsmout;
7188                 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
7189                 deleg = fxdr_unsigned(u_int32_t, *tl);
7190                 if (deleg == NFSV4OPEN_DELEGATEREAD ||
7191                     deleg == NFSV4OPEN_DELEGATEWRITE) {
7192                         if (!(op->nfso_own->nfsow_clp->nfsc_flags &
7193                               NFSCLFLAGS_FIRSTDELEG))
7194                                 op->nfso_own->nfsow_clp->nfsc_flags |=
7195                                   (NFSCLFLAGS_FIRSTDELEG | NFSCLFLAGS_GOTDELEG);
7196                         ndp = malloc(sizeof(struct nfscldeleg) + newfhlen,
7197                             M_NFSCLDELEG, M_WAITOK);
7198                         LIST_INIT(&ndp->nfsdl_owner);
7199                         LIST_INIT(&ndp->nfsdl_lock);
7200                         ndp->nfsdl_clp = op->nfso_own->nfsow_clp;
7201                         ndp->nfsdl_fhlen = newfhlen;
7202                         NFSBCOPY(newfhp, ndp->nfsdl_fh, newfhlen);
7203                         newnfs_copyincred(cred, &ndp->nfsdl_cred);
7204                         nfscl_lockinit(&ndp->nfsdl_rwlock);
7205                         NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
7206                             NFSX_UNSIGNED);
7207                         ndp->nfsdl_stateid.seqid = *tl++;
7208                         ndp->nfsdl_stateid.other[0] = *tl++;
7209                         ndp->nfsdl_stateid.other[1] = *tl++;
7210                         ndp->nfsdl_stateid.other[2] = *tl++;
7211                         ret = fxdr_unsigned(int, *tl);
7212                         if (deleg == NFSV4OPEN_DELEGATEWRITE) {
7213                                 ndp->nfsdl_flags = NFSCLDL_WRITE;
7214                                 /*
7215                                  * Indicates how much the file can grow.
7216                                  */
7217                                 NFSM_DISSECT(tl, u_int32_t *,
7218                                     3 * NFSX_UNSIGNED);
7219                                 limitby = fxdr_unsigned(int, *tl++);
7220                                 switch (limitby) {
7221                                 case NFSV4OPEN_LIMITSIZE:
7222                                         ndp->nfsdl_sizelimit = fxdr_hyper(tl);
7223                                         break;
7224                                 case NFSV4OPEN_LIMITBLOCKS:
7225                                         ndp->nfsdl_sizelimit =
7226                                             fxdr_unsigned(u_int64_t, *tl++);
7227                                         ndp->nfsdl_sizelimit *=
7228                                             fxdr_unsigned(u_int64_t, *tl);
7229                                         break;
7230                                 default:
7231                                         error = NFSERR_BADXDR;
7232                                         goto nfsmout;
7233                                 };
7234                         } else
7235                                 ndp->nfsdl_flags = NFSCLDL_READ;
7236                         if (ret != 0)
7237                                 ndp->nfsdl_flags |= NFSCLDL_RECALL;
7238                         error = nfsrv_dissectace(nd, &ndp->nfsdl_ace, &ret,
7239                             &acesize, p);
7240                         if (error != 0)
7241                                 goto nfsmout;
7242                 } else if (deleg != NFSV4OPEN_DELEGATENONE) {
7243                         error = NFSERR_BADXDR;
7244                         goto nfsmout;
7245                 }
7246                 if ((rflags & NFSV4OPEN_LOCKTYPEPOSIX) != 0 ||
7247                     nfscl_assumeposixlocks)
7248                         op->nfso_posixlock = 1;
7249                 else
7250                         op->nfso_posixlock = 0;
7251                 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
7252                 /* If the 2nd element == NFS_OK, the Getattr succeeded. */
7253                 if (*++tl == 0) {
7254                         error = nfsv4_loadattr(nd, NULL, &nfsva, NULL,
7255                             NULL, 0, NULL, NULL, NULL, NULL, NULL, 0,
7256                             NULL, NULL, NULL, p, cred);
7257                         if (error != 0)
7258                                 goto nfsmout;
7259                         if (ndp != NULL) {
7260                                 ndp->nfsdl_change = nfsva.na_filerev;
7261                                 ndp->nfsdl_modtime = nfsva.na_mtime;
7262                                 ndp->nfsdl_flags |= NFSCLDL_MODTIMESET;
7263                                 *dpp = ndp;
7264                                 ndp = NULL;
7265                         }
7266                         /*
7267                          * At this point, the Open has succeeded, so set
7268                          * nd_repstat = NFS_OK.  If the Layoutget failed,
7269                          * this function just won't return a layout.
7270                          */
7271                         if (nd->nd_repstat == 0) {
7272                                 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
7273                                 *laystatp = fxdr_unsigned(int, *++tl);
7274                                 if (*laystatp == 0) {
7275                                         error = nfsrv_parselayoutget(nd,
7276                                             stateidp, retonclosep, flhp);
7277                                         if (error != 0)
7278                                                 *laystatp = error;
7279                                 }
7280                         } else
7281                                 nd->nd_repstat = 0;     /* Return 0 for Open. */
7282                 }
7283         }
7284         if (nd->nd_repstat != 0 && error == 0)
7285                 error = nd->nd_repstat;
7286 nfsmout:
7287         free(ndp, M_NFSCLDELEG);
7288         mbuf_freem(nd->nd_mrep);
7289         return (error);
7290 }
7291
7292 /*
7293  * Similar nfsrpc_createv4(), but also does the LayoutGet operation.
7294  * Used only for mounts with pNFS enabled.
7295  */
7296 static int
7297 nfsrpc_createlayout(vnode_t dvp, char *name, int namelen, struct vattr *vap,
7298     nfsquad_t cverf, int fmode, struct nfsclowner *owp, struct nfscldeleg **dpp,
7299     struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap,
7300     struct nfsvattr *nnap, struct nfsfh **nfhpp, int *attrflagp,
7301     int *dattrflagp, void *dstuff, int *unlockedp, nfsv4stateid_t *stateidp,
7302     int usecurstateid, int layouttype, int layoutlen, int *retonclosep,
7303     struct nfsclflayouthead *flhp, int *laystatp)
7304 {
7305         uint32_t *tl;
7306         int error = 0, deleg, newone, ret, acesize, limitby;
7307         struct nfsrv_descript nfsd, *nd = &nfsd;
7308         struct nfsclopen *op;
7309         struct nfscldeleg *dp = NULL;
7310         struct nfsnode *np;
7311         struct nfsfh *nfhp;
7312         struct nfsclsession *tsep;
7313         nfsattrbit_t attrbits;
7314         nfsv4stateid_t stateid;
7315         struct nfsmount *nmp;
7316
7317         nmp = VFSTONFS(dvp->v_mount);
7318         np = VTONFS(dvp);
7319         *laystatp = ENXIO;
7320         *unlockedp = 0;
7321         *nfhpp = NULL;
7322         *dpp = NULL;
7323         *attrflagp = 0;
7324         *dattrflagp = 0;
7325         if (namelen > NFS_MAXNAMLEN)
7326                 return (ENAMETOOLONG);
7327         NFSCL_REQSTART(nd, NFSPROC_CREATELAYGET, dvp);
7328         /*
7329          * For V4, this is actually an Open op.
7330          */
7331         NFSM_BUILD(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
7332         *tl++ = txdr_unsigned(owp->nfsow_seqid);
7333         *tl++ = txdr_unsigned(NFSV4OPEN_ACCESSWRITE |
7334             NFSV4OPEN_ACCESSREAD);
7335         *tl++ = txdr_unsigned(NFSV4OPEN_DENYNONE);
7336         tsep = nfsmnt_mdssession(nmp);
7337         *tl++ = tsep->nfsess_clientid.lval[0];
7338         *tl = tsep->nfsess_clientid.lval[1];
7339         nfsm_strtom(nd, owp->nfsow_owner, NFSV4CL_LOCKNAMELEN);
7340         NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
7341         *tl++ = txdr_unsigned(NFSV4OPEN_CREATE);
7342         if ((fmode & O_EXCL) != 0) {
7343                 if (NFSHASSESSPERSIST(nmp)) {
7344                         /* Use GUARDED for persistent sessions. */
7345                         *tl = txdr_unsigned(NFSCREATE_GUARDED);
7346                         nfscl_fillsattr(nd, vap, dvp, 0, 0);
7347                 } else {
7348                         /* Otherwise, use EXCLUSIVE4_1. */
7349                         *tl = txdr_unsigned(NFSCREATE_EXCLUSIVE41);
7350                         NFSM_BUILD(tl, u_int32_t *, NFSX_VERF);
7351                         *tl++ = cverf.lval[0];
7352                         *tl = cverf.lval[1];
7353                         nfscl_fillsattr(nd, vap, dvp, 0, 0);
7354                 }
7355         } else {
7356                 *tl = txdr_unsigned(NFSCREATE_UNCHECKED);
7357                 nfscl_fillsattr(nd, vap, dvp, 0, 0);
7358         }
7359         NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
7360         *tl = txdr_unsigned(NFSV4OPEN_CLAIMNULL);
7361         nfsm_strtom(nd, name, namelen);
7362         /* Get the new file's handle and attributes, plus save the FH. */
7363         NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
7364         *tl++ = txdr_unsigned(NFSV4OP_SAVEFH);
7365         *tl++ = txdr_unsigned(NFSV4OP_GETFH);
7366         *tl = txdr_unsigned(NFSV4OP_GETATTR);
7367         NFSGETATTR_ATTRBIT(&attrbits);
7368         nfsrv_putattrbit(nd, &attrbits);
7369         /* Get the directory's post-op attributes. */
7370         NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
7371         *tl = txdr_unsigned(NFSV4OP_PUTFH);
7372         nfsm_fhtom(nd, np->n_fhp->nfh_fh, np->n_fhp->nfh_len, 0);
7373         NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
7374         *tl = txdr_unsigned(NFSV4OP_GETATTR);
7375         nfsrv_putattrbit(nd, &attrbits);
7376         NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
7377         *tl++ = txdr_unsigned(NFSV4OP_RESTOREFH);
7378         *tl = txdr_unsigned(NFSV4OP_LAYOUTGET);
7379         nfsrv_setuplayoutget(nd, NFSLAYOUTIOMODE_RW, 0, UINT64_MAX, 0, stateidp,
7380             layouttype, layoutlen, usecurstateid);
7381         error = nfscl_request(nd, dvp, p, cred, dstuff);
7382         if (error != 0)
7383                 return (error);
7384         NFSCL_DEBUG(4, "nfsrpc_createlayout stat=%d err=%d\n", nd->nd_repstat,
7385             error);
7386         if (nd->nd_repstat != 0)
7387                 *laystatp = nd->nd_repstat;
7388         NFSCL_INCRSEQID(owp->nfsow_seqid, nd);
7389         if ((nd->nd_flag & ND_NOMOREDATA) == 0) {
7390                 NFSCL_DEBUG(4, "nfsrpc_createlayout open succeeded\n");
7391                 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
7392                     6 * NFSX_UNSIGNED);
7393                 stateid.seqid = *tl++;
7394                 stateid.other[0] = *tl++;
7395                 stateid.other[1] = *tl++;
7396                 stateid.other[2] = *tl;
7397                 nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
7398                 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
7399                 deleg = fxdr_unsigned(int, *tl);
7400                 if (deleg == NFSV4OPEN_DELEGATEREAD ||
7401                     deleg == NFSV4OPEN_DELEGATEWRITE) {
7402                         if (!(owp->nfsow_clp->nfsc_flags &
7403                               NFSCLFLAGS_FIRSTDELEG))
7404                                 owp->nfsow_clp->nfsc_flags |=
7405                                   (NFSCLFLAGS_FIRSTDELEG | NFSCLFLAGS_GOTDELEG);
7406                         dp = malloc(sizeof(struct nfscldeleg) + NFSX_V4FHMAX,
7407                             M_NFSCLDELEG, M_WAITOK);
7408                         LIST_INIT(&dp->nfsdl_owner);
7409                         LIST_INIT(&dp->nfsdl_lock);
7410                         dp->nfsdl_clp = owp->nfsow_clp;
7411                         newnfs_copyincred(cred, &dp->nfsdl_cred);
7412                         nfscl_lockinit(&dp->nfsdl_rwlock);
7413                         NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
7414                             NFSX_UNSIGNED);
7415                         dp->nfsdl_stateid.seqid = *tl++;
7416                         dp->nfsdl_stateid.other[0] = *tl++;
7417                         dp->nfsdl_stateid.other[1] = *tl++;
7418                         dp->nfsdl_stateid.other[2] = *tl++;
7419                         ret = fxdr_unsigned(int, *tl);
7420                         if (deleg == NFSV4OPEN_DELEGATEWRITE) {
7421                                 dp->nfsdl_flags = NFSCLDL_WRITE;
7422                                 /*
7423                                  * Indicates how much the file can grow.
7424                                  */
7425                                 NFSM_DISSECT(tl, u_int32_t *,
7426                                     3 * NFSX_UNSIGNED);
7427                                 limitby = fxdr_unsigned(int, *tl++);
7428                                 switch (limitby) {
7429                                 case NFSV4OPEN_LIMITSIZE:
7430                                         dp->nfsdl_sizelimit = fxdr_hyper(tl);
7431                                         break;
7432                                 case NFSV4OPEN_LIMITBLOCKS:
7433                                         dp->nfsdl_sizelimit =
7434                                             fxdr_unsigned(u_int64_t, *tl++);
7435                                         dp->nfsdl_sizelimit *=
7436                                             fxdr_unsigned(u_int64_t, *tl);
7437                                         break;
7438                                 default:
7439                                         error = NFSERR_BADXDR;
7440                                         goto nfsmout;
7441                                 };
7442                         } else {
7443                                 dp->nfsdl_flags = NFSCLDL_READ;
7444                         }
7445                         if (ret != 0)
7446                                 dp->nfsdl_flags |= NFSCLDL_RECALL;
7447                         error = nfsrv_dissectace(nd, &dp->nfsdl_ace, &ret,
7448                             &acesize, p);
7449                         if (error != 0)
7450                                 goto nfsmout;
7451                 } else if (deleg != NFSV4OPEN_DELEGATENONE) {
7452                         error = NFSERR_BADXDR;
7453                         goto nfsmout;
7454                 }
7455
7456                 /* Now, we should have the status for the SaveFH. */
7457                 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
7458                 if (*++tl == 0) {
7459                         NFSCL_DEBUG(4, "nfsrpc_createlayout SaveFH ok\n");
7460                         /*
7461                          * Now, process the GetFH and Getattr for the newly
7462                          * created file. nfscl_mtofh() will set
7463                          * ND_NOMOREDATA if these weren't successful.
7464                          */
7465                         error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp);
7466                         NFSCL_DEBUG(4, "aft nfscl_mtofh err=%d\n", error);
7467                         if (error != 0)
7468                                 goto nfsmout;
7469                 } else
7470                         nd->nd_flag |= ND_NOMOREDATA;
7471                 /* Now we have the PutFH and Getattr for the directory. */
7472                 if ((nd->nd_flag & ND_NOMOREDATA) == 0) {
7473                         NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
7474                         if (*++tl != 0)
7475                                 nd->nd_flag |= ND_NOMOREDATA;
7476                         else {
7477                                 NFSM_DISSECT(tl, uint32_t *, 2 *
7478                                     NFSX_UNSIGNED);
7479                                 if (*++tl != 0)
7480                                         nd->nd_flag |= ND_NOMOREDATA;
7481                         }
7482                 }
7483                 if ((nd->nd_flag & ND_NOMOREDATA) == 0) {
7484                         /* Load the directory attributes. */
7485                         error = nfsm_loadattr(nd, dnap);
7486                         NFSCL_DEBUG(4, "aft nfsm_loadattr err=%d\n", error);
7487                         if (error != 0)
7488                                 goto nfsmout;
7489                         *dattrflagp = 1;
7490                         if (dp != NULL && *attrflagp != 0) {
7491                                 dp->nfsdl_change = nnap->na_filerev;
7492                                 dp->nfsdl_modtime = nnap->na_mtime;
7493                                 dp->nfsdl_flags |= NFSCLDL_MODTIMESET;
7494                         }
7495                         /*
7496                          * We can now complete the Open state.
7497                          */
7498                         nfhp = *nfhpp;
7499                         if (dp != NULL) {
7500                                 dp->nfsdl_fhlen = nfhp->nfh_len;
7501                                 NFSBCOPY(nfhp->nfh_fh, dp->nfsdl_fh,
7502                                     nfhp->nfh_len);
7503                         }
7504                         /*
7505                          * Get an Open structure that will be
7506                          * attached to the OpenOwner, acquired already.
7507                          */
7508                         error = nfscl_open(dvp, nfhp->nfh_fh, nfhp->nfh_len, 
7509                             (NFSV4OPEN_ACCESSWRITE | NFSV4OPEN_ACCESSREAD), 0,
7510                             cred, p, NULL, &op, &newone, NULL, 0);
7511                         if (error != 0)
7512                                 goto nfsmout;
7513                         op->nfso_stateid = stateid;
7514                         newnfs_copyincred(cred, &op->nfso_cred);
7515         
7516                         nfscl_openrelease(nmp, op, error, newone);
7517                         *unlockedp = 1;
7518
7519                         /* Now, handle the RestoreFH and LayoutGet. */
7520                         if (nd->nd_repstat == 0) {
7521                                 NFSM_DISSECT(tl, uint32_t *, 4 * NFSX_UNSIGNED);
7522                                 *laystatp = fxdr_unsigned(int, *(tl + 3));
7523                                 if (*laystatp == 0) {
7524                                         error = nfsrv_parselayoutget(nd,
7525                                             stateidp, retonclosep, flhp);
7526                                         if (error != 0)
7527                                                 *laystatp = error;
7528                                 }
7529                                 NFSCL_DEBUG(4, "aft nfsrv_parselayout err=%d\n",
7530                                     error);
7531                         } else
7532                                 nd->nd_repstat = 0;
7533                 }
7534         }
7535         if (nd->nd_repstat != 0 && error == 0)
7536                 error = nd->nd_repstat;
7537         if (error == NFSERR_STALECLIENTID || error == NFSERR_BADSESSION)
7538                 nfscl_initiate_recovery(owp->nfsow_clp);
7539 nfsmout:
7540         NFSCL_DEBUG(4, "eo nfsrpc_createlayout err=%d\n", error);
7541         if (error == 0)
7542                 *dpp = dp;
7543         else
7544                 free(dp, M_NFSCLDELEG);
7545         mbuf_freem(nd->nd_mrep);
7546         return (error);
7547 }
7548
7549 /*
7550  * Similar to nfsrpc_getopenlayout(), except that it used for the Create case.
7551  */
7552 static int
7553 nfsrpc_getcreatelayout(vnode_t dvp, char *name, int namelen, struct vattr *vap,
7554     nfsquad_t cverf, int fmode, struct nfsclowner *owp, struct nfscldeleg **dpp,
7555     struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap,
7556     struct nfsvattr *nnap, struct nfsfh **nfhpp, int *attrflagp,
7557     int *dattrflagp, void *dstuff, int *unlockedp)
7558 {
7559         struct nfscllayout *lyp;
7560         struct nfsclflayouthead flh;
7561         struct nfsfh *nfhp;
7562         struct nfsclsession *tsep;
7563         struct nfsmount *nmp;
7564         nfsv4stateid_t stateid;
7565         int error, layoutlen, layouttype, retonclose, laystat;
7566
7567         error = 0;
7568         nmp = VFSTONFS(dvp->v_mount);
7569         if (NFSHASFLEXFILE(nmp))
7570                 layouttype = NFSLAYOUT_FLEXFILE;
7571         else
7572                 layouttype = NFSLAYOUT_NFSV4_1_FILES;
7573         LIST_INIT(&flh);
7574         tsep = nfsmnt_mdssession(nmp);
7575         layoutlen = tsep->nfsess_maxcache - (NFSX_STATEID + 3 * NFSX_UNSIGNED);
7576         error = nfsrpc_createlayout(dvp, name, namelen, vap, cverf, fmode,
7577             owp, dpp, cred, p, dnap, nnap, nfhpp, attrflagp, dattrflagp,
7578             dstuff, unlockedp, &stateid, 1, layouttype, layoutlen, &retonclose,
7579             &flh, &laystat);
7580         NFSCL_DEBUG(4, "aft nfsrpc_createlayoutrpc laystat=%d err=%d\n",
7581             laystat, error);
7582         lyp = NULL;
7583         if (laystat == 0) {
7584                 nfhp = *nfhpp;
7585                 laystat = nfsrpc_layoutgetres(nmp, dvp, nfhp->nfh_fh,
7586                     nfhp->nfh_len, &stateid, retonclose, NULL, &lyp, &flh,
7587                     layouttype, laystat, NULL, cred, p);
7588         } else
7589                 laystat = nfsrpc_layoutgetres(nmp, dvp, NULL, 0, &stateid,
7590                     retonclose, NULL, &lyp, &flh, layouttype, laystat, NULL,
7591                     cred, p);
7592         if (laystat == 0)
7593                 nfscl_rellayout(lyp, 0);
7594         return (error);
7595 }
7596
7597 /*
7598  * Process the results of a layoutget() operation.
7599  */
7600 static int
7601 nfsrpc_layoutgetres(struct nfsmount *nmp, vnode_t vp, uint8_t *newfhp,
7602     int newfhlen, nfsv4stateid_t *stateidp, int retonclose, uint32_t *notifybit,
7603     struct nfscllayout **lypp, struct nfsclflayouthead *flhp, int layouttype,
7604     int laystat, int *islockedp, struct ucred *cred, NFSPROC_T *p)
7605 {
7606         struct nfsclflayout *tflp;
7607         struct nfscldevinfo *dip;
7608         uint8_t *dev;
7609         int i, mirrorcnt;
7610
7611         if (laystat == NFSERR_UNKNLAYOUTTYPE) {
7612                 NFSLOCKMNT(nmp);
7613                 if (!NFSHASFLEXFILE(nmp)) {
7614                         /* Switch to using Flex File Layout. */
7615                         nmp->nm_state |= NFSSTA_FLEXFILE;
7616                 } else if (layouttype == NFSLAYOUT_FLEXFILE) {
7617                         /* Disable pNFS. */
7618                         NFSCL_DEBUG(1, "disable PNFS\n");
7619                         nmp->nm_state &= ~(NFSSTA_PNFS | NFSSTA_FLEXFILE);
7620                 }
7621                 NFSUNLOCKMNT(nmp);
7622         }
7623         if (laystat == 0) {
7624                 NFSCL_DEBUG(4, "nfsrpc_layoutgetres at FOREACH\n");
7625                 LIST_FOREACH(tflp, flhp, nfsfl_list) {
7626                         if (layouttype == NFSLAYOUT_FLEXFILE)
7627                                 mirrorcnt = tflp->nfsfl_mirrorcnt;
7628                         else
7629                                 mirrorcnt = 1;
7630                         for (i = 0; i < mirrorcnt; i++) {
7631                                 laystat = nfscl_adddevinfo(nmp, NULL, i, tflp);
7632                                 NFSCL_DEBUG(4, "aft adddev=%d\n", laystat);
7633                                 if (laystat != 0) {
7634                                         if (layouttype == NFSLAYOUT_FLEXFILE)
7635                                                 dev = tflp->nfsfl_ffm[i].dev;
7636                                         else
7637                                                 dev = tflp->nfsfl_dev;
7638                                         laystat = nfsrpc_getdeviceinfo(nmp, dev,
7639                                             layouttype, notifybit, &dip, cred,
7640                                             p);
7641                                         NFSCL_DEBUG(4, "aft nfsrpc_gdi=%d\n",
7642                                             laystat);
7643                                         if (laystat != 0)
7644                                                 goto out;
7645                                         laystat = nfscl_adddevinfo(nmp, dip, i,
7646                                             tflp);
7647                                         if (laystat != 0)
7648                                                 printf("nfsrpc_layoutgetresout"
7649                                                     ": cannot add\n");
7650                                 }
7651                         }
7652                 }
7653         }
7654 out:
7655         if (laystat == 0) {
7656                 /*
7657                  * nfscl_layout() always returns with the nfsly_lock
7658                  * set to a refcnt (shared lock).
7659                  * Passing in dvp is sufficient, since it is only used to
7660                  * get the fsid for the file system.
7661                  */
7662                 laystat = nfscl_layout(nmp, vp, newfhp, newfhlen, stateidp,
7663                     layouttype, retonclose, flhp, lypp, cred, p);
7664                 NFSCL_DEBUG(4, "nfsrpc_layoutgetres: aft nfscl_layout=%d\n",
7665                     laystat);
7666                 if (laystat == 0 && islockedp != NULL)
7667                         *islockedp = 1;
7668         }
7669         return (laystat);
7670 }
7671