source: rtems-libbsd/freebsd/sys/fs/nfsclient/nfs_clrpcops.c @ c56a34f

6-freebsd-12
Last change on this file since c56a34f was c56a34f, checked in by Chris Johns <chrisj@…>, on 06/20/22 at 02:44:41

freebsd/nfsv4: Fix misaligned 64bit user dirent write

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