nfs_clrpcops.c revision 1.1 1 1.1 dholland /* $NetBSD: nfs_clrpcops.c,v 1.1 2013/09/30 07:19:18 dholland Exp $ */
2 1.1 dholland /*-
3 1.1 dholland * Copyright (c) 1989, 1993
4 1.1 dholland * The Regents of the University of California. All rights reserved.
5 1.1 dholland *
6 1.1 dholland * This code is derived from software contributed to Berkeley by
7 1.1 dholland * Rick Macklem at The University of Guelph.
8 1.1 dholland *
9 1.1 dholland * Redistribution and use in source and binary forms, with or without
10 1.1 dholland * modification, are permitted provided that the following conditions
11 1.1 dholland * are met:
12 1.1 dholland * 1. Redistributions of source code must retain the above copyright
13 1.1 dholland * notice, this list of conditions and the following disclaimer.
14 1.1 dholland * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 dholland * notice, this list of conditions and the following disclaimer in the
16 1.1 dholland * documentation and/or other materials provided with the distribution.
17 1.1 dholland * 4. Neither the name of the University nor the names of its contributors
18 1.1 dholland * may be used to endorse or promote products derived from this software
19 1.1 dholland * without specific prior written permission.
20 1.1 dholland *
21 1.1 dholland * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 1.1 dholland * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 1.1 dholland * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 1.1 dholland * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 1.1 dholland * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 1.1 dholland * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 1.1 dholland * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 1.1 dholland * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 1.1 dholland * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 1.1 dholland * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 1.1 dholland * SUCH DAMAGE.
32 1.1 dholland *
33 1.1 dholland */
34 1.1 dholland
35 1.1 dholland #include <sys/cdefs.h>
36 1.1 dholland /* __FBSDID("FreeBSD: head/sys/fs/nfsclient/nfs_clrpcops.c 245977 2013-01-27 09:34:25Z kib "); */
37 1.1 dholland __RCSID("$NetBSD: nfs_clrpcops.c,v 1.1 2013/09/30 07:19:18 dholland Exp $");
38 1.1 dholland
39 1.1 dholland /*
40 1.1 dholland * Rpc op calls, generally called from the vnode op calls or through the
41 1.1 dholland * buffer cache, for NFS v2, 3 and 4.
42 1.1 dholland * These do not normally make any changes to vnode arguments or use
43 1.1 dholland * structures that might change between the VFS variants. The returned
44 1.1 dholland * arguments are all at the end, after the NFSPROC_T *p one.
45 1.1 dholland */
46 1.1 dholland
47 1.1 dholland #ifndef APPLEKEXT
48 1.1 dholland #include "opt_inet6.h"
49 1.1 dholland
50 1.1 dholland #include <fs/nfs/nfsport.h>
51 1.1 dholland
52 1.1 dholland /*
53 1.1 dholland * Global variables
54 1.1 dholland */
55 1.1 dholland extern int nfs_numnfscbd;
56 1.1 dholland extern struct timeval nfsboottime;
57 1.1 dholland extern u_int32_t newnfs_false, newnfs_true;
58 1.1 dholland extern nfstype nfsv34_type[9];
59 1.1 dholland extern int nfsrv_useacl;
60 1.1 dholland extern char nfsv4_callbackaddr[INET6_ADDRSTRLEN];
61 1.1 dholland extern int nfscl_debuglevel;
62 1.1 dholland NFSCLSTATEMUTEX;
63 1.1 dholland int nfstest_outofseq = 0;
64 1.1 dholland int nfscl_assumeposixlocks = 1;
65 1.1 dholland int nfscl_enablecallb = 0;
66 1.1 dholland short nfsv4_cbport = NFSV4_CBPORT;
67 1.1 dholland int nfstest_openallsetattr = 0;
68 1.1 dholland #endif /* !APPLEKEXT */
69 1.1 dholland
70 1.1 dholland #define DIRHDSIZ (sizeof (struct dirent) - (MAXNAMLEN + 1))
71 1.1 dholland
72 1.1 dholland /*
73 1.1 dholland * nfscl_getsameserver() can return one of three values:
74 1.1 dholland * NFSDSP_USETHISSESSION - Use this session for the DS.
75 1.1 dholland * NFSDSP_SEQTHISSESSION - Use the nfsclds_sequence field of this dsp for new
76 1.1 dholland * session.
77 1.1 dholland * NFSDSP_NOTFOUND - No matching server was found.
78 1.1 dholland */
79 1.1 dholland enum nfsclds_state {
80 1.1 dholland NFSDSP_USETHISSESSION = 0,
81 1.1 dholland NFSDSP_SEQTHISSESSION = 1,
82 1.1 dholland NFSDSP_NOTFOUND = 2,
83 1.1 dholland };
84 1.1 dholland
85 1.1 dholland static int nfsrpc_setattrrpc(vnode_t , struct vattr *, nfsv4stateid_t *,
86 1.1 dholland struct ucred *, NFSPROC_T *, struct nfsvattr *, int *, void *);
87 1.1 dholland static int nfsrpc_readrpc(vnode_t , struct uio *, struct ucred *,
88 1.1 dholland nfsv4stateid_t *, NFSPROC_T *, struct nfsvattr *, int *, void *);
89 1.1 dholland static int nfsrpc_writerpc(vnode_t , struct uio *, int *, int *,
90 1.1 dholland struct ucred *, nfsv4stateid_t *, NFSPROC_T *, struct nfsvattr *, int *,
91 1.1 dholland void *);
92 1.1 dholland static int nfsrpc_createv23(vnode_t , char *, int, struct vattr *,
93 1.1 dholland nfsquad_t, int, struct ucred *, NFSPROC_T *, struct nfsvattr *,
94 1.1 dholland struct nfsvattr *, struct nfsfh **, int *, int *, void *);
95 1.1 dholland static int nfsrpc_createv4(vnode_t , char *, int, struct vattr *,
96 1.1 dholland nfsquad_t, int, struct nfsclowner *, struct nfscldeleg **, struct ucred *,
97 1.1 dholland NFSPROC_T *, struct nfsvattr *, struct nfsvattr *, struct nfsfh **, int *,
98 1.1 dholland int *, void *, int *);
99 1.1 dholland static int nfsrpc_locku(struct nfsrv_descript *, struct nfsmount *,
100 1.1 dholland struct nfscllockowner *, u_int64_t, u_int64_t,
101 1.1 dholland u_int32_t, struct ucred *, NFSPROC_T *, int);
102 1.1 dholland static int nfsrpc_setaclrpc(vnode_t, struct ucred *, NFSPROC_T *,
103 1.1 dholland struct acl *, nfsv4stateid_t *, void *);
104 1.1 dholland static int nfsrpc_getlayout(struct nfsmount *, vnode_t, struct nfsfh *, int,
105 1.1 dholland uint32_t *, nfsv4stateid_t *, uint64_t, struct nfscllayout **,
106 1.1 dholland struct ucred *, NFSPROC_T *);
107 1.1 dholland static int nfsrpc_fillsa(struct nfsmount *, struct sockaddr_storage *,
108 1.1 dholland struct nfsclds **, NFSPROC_T *);
109 1.1 dholland static void nfscl_initsessionslots(struct nfsclsession *);
110 1.1 dholland static int nfscl_doflayoutio(vnode_t, struct uio *, int *, int *, int *,
111 1.1 dholland nfsv4stateid_t *, int, struct nfscldevinfo *, struct nfscllayout *,
112 1.1 dholland struct nfsclflayout *, uint64_t, uint64_t, struct ucred *, NFSPROC_T *);
113 1.1 dholland static int nfsrpc_readds(vnode_t, struct uio *, nfsv4stateid_t *, int *,
114 1.1 dholland struct nfsclds *, uint64_t, int, struct nfsfh *, struct ucred *,
115 1.1 dholland NFSPROC_T *);
116 1.1 dholland static int nfsrpc_writeds(vnode_t, struct uio *, int *, int *,
117 1.1 dholland nfsv4stateid_t *, struct nfsclds *, uint64_t, int,
118 1.1 dholland struct nfsfh *, int, struct ucred *, NFSPROC_T *);
119 1.1 dholland static enum nfsclds_state nfscl_getsameserver(struct nfsmount *,
120 1.1 dholland struct nfsclds *, struct nfsclds **);
121 1.1 dholland #ifdef notyet
122 1.1 dholland static int nfsrpc_commitds(vnode_t, uint64_t, int, struct nfsclds *,
123 1.1 dholland struct nfsfh *, struct ucred *, NFSPROC_T *, void *);
124 1.1 dholland #endif
125 1.1 dholland
126 1.1 dholland /*
127 1.1 dholland * nfs null call from vfs.
128 1.1 dholland */
129 1.1 dholland APPLESTATIC int
130 1.1 dholland nfsrpc_null(vnode_t vp, struct ucred *cred, NFSPROC_T *p)
131 1.1 dholland {
132 1.1 dholland int error;
133 1.1 dholland struct nfsrv_descript nfsd, *nd = &nfsd;
134 1.1 dholland
135 1.1 dholland NFSCL_REQSTART(nd, NFSPROC_NULL, vp);
136 1.1 dholland error = nfscl_request(nd, vp, p, cred, NULL);
137 1.1 dholland if (nd->nd_repstat && !error)
138 1.1 dholland error = nd->nd_repstat;
139 1.1 dholland mbuf_freem(nd->nd_mrep);
140 1.1 dholland return (error);
141 1.1 dholland }
142 1.1 dholland
143 1.1 dholland /*
144 1.1 dholland * nfs access rpc op.
145 1.1 dholland * For nfs version 3 and 4, use the access rpc to check accessibility. If file
146 1.1 dholland * modes are changed on the server, accesses might still fail later.
147 1.1 dholland */
148 1.1 dholland APPLESTATIC int
149 1.1 dholland nfsrpc_access(vnode_t vp, int acmode, struct ucred *cred,
150 1.1 dholland NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp)
151 1.1 dholland {
152 1.1 dholland int error;
153 1.1 dholland u_int32_t mode, rmode;
154 1.1 dholland
155 1.1 dholland if (acmode & VREAD)
156 1.1 dholland mode = NFSACCESS_READ;
157 1.1 dholland else
158 1.1 dholland mode = 0;
159 1.1 dholland if (vnode_vtype(vp) == VDIR) {
160 1.1 dholland if (acmode & VWRITE)
161 1.1 dholland mode |= (NFSACCESS_MODIFY | NFSACCESS_EXTEND |
162 1.1 dholland NFSACCESS_DELETE);
163 1.1 dholland if (acmode & VEXEC)
164 1.1 dholland mode |= NFSACCESS_LOOKUP;
165 1.1 dholland } else {
166 1.1 dholland if (acmode & VWRITE)
167 1.1 dholland mode |= (NFSACCESS_MODIFY | NFSACCESS_EXTEND);
168 1.1 dholland if (acmode & VEXEC)
169 1.1 dholland mode |= NFSACCESS_EXECUTE;
170 1.1 dholland }
171 1.1 dholland
172 1.1 dholland /*
173 1.1 dholland * Now, just call nfsrpc_accessrpc() to do the actual RPC.
174 1.1 dholland */
175 1.1 dholland error = nfsrpc_accessrpc(vp, mode, cred, p, nap, attrflagp, &rmode,
176 1.1 dholland NULL);
177 1.1 dholland
178 1.1 dholland /*
179 1.1 dholland * The NFS V3 spec does not clarify whether or not
180 1.1 dholland * the returned access bits can be a superset of
181 1.1 dholland * the ones requested, so...
182 1.1 dholland */
183 1.1 dholland if (!error && (rmode & mode) != mode)
184 1.1 dholland error = EACCES;
185 1.1 dholland return (error);
186 1.1 dholland }
187 1.1 dholland
188 1.1 dholland /*
189 1.1 dholland * The actual rpc, separated out for Darwin.
190 1.1 dholland */
191 1.1 dholland APPLESTATIC int
192 1.1 dholland nfsrpc_accessrpc(vnode_t vp, u_int32_t mode, struct ucred *cred,
193 1.1 dholland NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, u_int32_t *rmodep,
194 1.1 dholland void *stuff)
195 1.1 dholland {
196 1.1 dholland u_int32_t *tl;
197 1.1 dholland u_int32_t supported, rmode;
198 1.1 dholland int error;
199 1.1 dholland struct nfsrv_descript nfsd, *nd = &nfsd;
200 1.1 dholland nfsattrbit_t attrbits;
201 1.1 dholland
202 1.1 dholland *attrflagp = 0;
203 1.1 dholland supported = mode;
204 1.1 dholland NFSCL_REQSTART(nd, NFSPROC_ACCESS, vp);
205 1.1 dholland NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
206 1.1 dholland *tl = txdr_unsigned(mode);
207 1.1 dholland if (nd->nd_flag & ND_NFSV4) {
208 1.1 dholland /*
209 1.1 dholland * And do a Getattr op.
210 1.1 dholland */
211 1.1 dholland NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
212 1.1 dholland *tl = txdr_unsigned(NFSV4OP_GETATTR);
213 1.1 dholland NFSGETATTR_ATTRBIT(&attrbits);
214 1.1 dholland (void) nfsrv_putattrbit(nd, &attrbits);
215 1.1 dholland }
216 1.1 dholland error = nfscl_request(nd, vp, p, cred, stuff);
217 1.1 dholland if (error)
218 1.1 dholland return (error);
219 1.1 dholland if (nd->nd_flag & ND_NFSV3) {
220 1.1 dholland error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
221 1.1 dholland if (error)
222 1.1 dholland goto nfsmout;
223 1.1 dholland }
224 1.1 dholland if (!nd->nd_repstat) {
225 1.1 dholland if (nd->nd_flag & ND_NFSV4) {
226 1.1 dholland NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
227 1.1 dholland supported = fxdr_unsigned(u_int32_t, *tl++);
228 1.1 dholland } else {
229 1.1 dholland NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
230 1.1 dholland }
231 1.1 dholland rmode = fxdr_unsigned(u_int32_t, *tl);
232 1.1 dholland if (nd->nd_flag & ND_NFSV4)
233 1.1 dholland error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
234 1.1 dholland
235 1.1 dholland /*
236 1.1 dholland * It's not obvious what should be done about
237 1.1 dholland * unsupported access modes. For now, be paranoid
238 1.1 dholland * and clear the unsupported ones.
239 1.1 dholland */
240 1.1 dholland rmode &= supported;
241 1.1 dholland *rmodep = rmode;
242 1.1 dholland } else
243 1.1 dholland error = nd->nd_repstat;
244 1.1 dholland nfsmout:
245 1.1 dholland mbuf_freem(nd->nd_mrep);
246 1.1 dholland return (error);
247 1.1 dholland }
248 1.1 dholland
249 1.1 dholland /*
250 1.1 dholland * nfs open rpc
251 1.1 dholland */
252 1.1 dholland APPLESTATIC int
253 1.1 dholland nfsrpc_open(vnode_t vp, int amode, struct ucred *cred, NFSPROC_T *p)
254 1.1 dholland {
255 1.1 dholland struct nfsclopen *op;
256 1.1 dholland struct nfscldeleg *dp;
257 1.1 dholland struct nfsfh *nfhp;
258 1.1 dholland struct nfsnode *np = VTONFS(vp);
259 1.1 dholland struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
260 1.1 dholland u_int32_t mode, clidrev;
261 1.1 dholland int ret, newone, error, expireret = 0, retrycnt;
262 1.1 dholland
263 1.1 dholland /*
264 1.1 dholland * For NFSv4, Open Ops are only done on Regular Files.
265 1.1 dholland */
266 1.1 dholland if (vnode_vtype(vp) != VREG)
267 1.1 dholland return (0);
268 1.1 dholland mode = 0;
269 1.1 dholland if (amode & FREAD)
270 1.1 dholland mode |= NFSV4OPEN_ACCESSREAD;
271 1.1 dholland if (amode & FWRITE)
272 1.1 dholland mode |= NFSV4OPEN_ACCESSWRITE;
273 1.1 dholland nfhp = np->n_fhp;
274 1.1 dholland
275 1.1 dholland retrycnt = 0;
276 1.1 dholland #ifdef notdef
277 1.1 dholland { char name[100]; int namel;
278 1.1 dholland namel = (np->n_v4->n4_namelen < 100) ? np->n_v4->n4_namelen : 99;
279 1.1 dholland bcopy(NFS4NODENAME(np->n_v4), name, namel);
280 1.1 dholland name[namel] = '\0';
281 1.1 dholland printf("rpcopen p=0x%x name=%s",p->p_pid,name);
282 1.1 dholland if (nfhp->nfh_len > 0) printf(" fh=0x%x\n",nfhp->nfh_fh[12]);
283 1.1 dholland else printf(" fhl=0\n");
284 1.1 dholland }
285 1.1 dholland #endif
286 1.1 dholland do {
287 1.1 dholland dp = NULL;
288 1.1 dholland error = nfscl_open(vp, nfhp->nfh_fh, nfhp->nfh_len, mode, 1,
289 1.1 dholland cred, p, NULL, &op, &newone, &ret, 1);
290 1.1 dholland if (error) {
291 1.1 dholland return (error);
292 1.1 dholland }
293 1.1 dholland if (nmp->nm_clp != NULL)
294 1.1 dholland clidrev = nmp->nm_clp->nfsc_clientidrev;
295 1.1 dholland else
296 1.1 dholland clidrev = 0;
297 1.1 dholland if (ret == NFSCLOPEN_DOOPEN) {
298 1.1 dholland if (np->n_v4 != NULL) {
299 1.1 dholland error = nfsrpc_openrpc(nmp, vp, np->n_v4->n4_data,
300 1.1 dholland np->n_v4->n4_fhlen, np->n_fhp->nfh_fh,
301 1.1 dholland np->n_fhp->nfh_len, mode, op,
302 1.1 dholland NFS4NODENAME(np->n_v4), np->n_v4->n4_namelen, &dp,
303 1.1 dholland 0, 0x0, cred, p, 0, 0);
304 1.1 dholland if (dp != NULL) {
305 1.1 dholland #ifdef APPLE
306 1.1 dholland OSBitAndAtomic((int32_t)~NDELEGMOD, (UInt32 *)&np->n_flag);
307 1.1 dholland #else
308 1.1 dholland NFSLOCKNODE(np);
309 1.1 dholland np->n_flag &= ~NDELEGMOD;
310 1.1 dholland /*
311 1.1 dholland * Invalidate the attribute cache, so that
312 1.1 dholland * attributes that pre-date the issue of a
313 1.1 dholland * delegation are not cached, since the
314 1.1 dholland * cached attributes will remain valid while
315 1.1 dholland * the delegation is held.
316 1.1 dholland */
317 1.1 dholland NFSINVALATTRCACHE(np);
318 1.1 dholland NFSUNLOCKNODE(np);
319 1.1 dholland #endif
320 1.1 dholland (void) nfscl_deleg(nmp->nm_mountp,
321 1.1 dholland op->nfso_own->nfsow_clp,
322 1.1 dholland nfhp->nfh_fh, nfhp->nfh_len, cred, p, &dp);
323 1.1 dholland }
324 1.1 dholland } else {
325 1.1 dholland error = EIO;
326 1.1 dholland }
327 1.1 dholland newnfs_copyincred(cred, &op->nfso_cred);
328 1.1 dholland } else if (ret == NFSCLOPEN_SETCRED)
329 1.1 dholland /*
330 1.1 dholland * This is a new local open on a delegation. It needs
331 1.1 dholland * to have credentials so that an open can be done
332 1.1 dholland * against the server during recovery.
333 1.1 dholland */
334 1.1 dholland newnfs_copyincred(cred, &op->nfso_cred);
335 1.1 dholland
336 1.1 dholland /*
337 1.1 dholland * nfso_opencnt is the count of how many VOP_OPEN()s have
338 1.1 dholland * been done on this Open successfully and a VOP_CLOSE()
339 1.1 dholland * is expected for each of these.
340 1.1 dholland * If error is non-zero, don't increment it, since the Open
341 1.1 dholland * hasn't succeeded yet.
342 1.1 dholland */
343 1.1 dholland if (!error)
344 1.1 dholland op->nfso_opencnt++;
345 1.1 dholland nfscl_openrelease(op, error, newone);
346 1.1 dholland if (error == NFSERR_GRACE || error == NFSERR_STALECLIENTID ||
347 1.1 dholland error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
348 1.1 dholland error == NFSERR_BADSESSION) {
349 1.1 dholland (void) nfs_catnap(PZERO, error, "nfs_open");
350 1.1 dholland } else if ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID)
351 1.1 dholland && clidrev != 0) {
352 1.1 dholland expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
353 1.1 dholland retrycnt++;
354 1.1 dholland }
355 1.1 dholland } while (error == NFSERR_GRACE || error == NFSERR_STALECLIENTID ||
356 1.1 dholland error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
357 1.1 dholland error == NFSERR_BADSESSION ||
358 1.1 dholland ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
359 1.1 dholland expireret == 0 && clidrev != 0 && retrycnt < 4));
360 1.1 dholland if (error && retrycnt >= 4)
361 1.1 dholland error = EIO;
362 1.1 dholland return (error);
363 1.1 dholland }
364 1.1 dholland
365 1.1 dholland /*
366 1.1 dholland * the actual open rpc
367 1.1 dholland */
368 1.1 dholland APPLESTATIC int
369 1.1 dholland nfsrpc_openrpc(struct nfsmount *nmp, vnode_t vp, u_int8_t *nfhp, int fhlen,
370 1.1 dholland u_int8_t *newfhp, int newfhlen, u_int32_t mode, struct nfsclopen *op,
371 1.1 dholland u_int8_t *name, int namelen, struct nfscldeleg **dpp,
372 1.1 dholland int reclaim, u_int32_t delegtype, struct ucred *cred, NFSPROC_T *p,
373 1.1 dholland int syscred, int recursed)
374 1.1 dholland {
375 1.1 dholland u_int32_t *tl;
376 1.1 dholland struct nfsrv_descript nfsd, *nd = &nfsd;
377 1.1 dholland struct nfscldeleg *dp, *ndp = NULL;
378 1.1 dholland struct nfsvattr nfsva;
379 1.1 dholland u_int32_t rflags, deleg;
380 1.1 dholland nfsattrbit_t attrbits;
381 1.1 dholland int error, ret, acesize, limitby;
382 1.1 dholland
383 1.1 dholland dp = *dpp;
384 1.1 dholland *dpp = NULL;
385 1.1 dholland nfscl_reqstart(nd, NFSPROC_OPEN, nmp, nfhp, fhlen, NULL, NULL);
386 1.1 dholland NFSM_BUILD(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
387 1.1 dholland *tl++ = txdr_unsigned(op->nfso_own->nfsow_seqid);
388 1.1 dholland *tl++ = txdr_unsigned(mode & NFSV4OPEN_ACCESSBOTH);
389 1.1 dholland *tl++ = txdr_unsigned((mode >> NFSLCK_SHIFT) & NFSV4OPEN_DENYBOTH);
390 1.1 dholland *tl++ = NFSMNT_MDSSESSION(nmp)->nfsess_clientid.lval[0];
391 1.1 dholland *tl = NFSMNT_MDSSESSION(nmp)->nfsess_clientid.lval[1];
392 1.1 dholland (void) nfsm_strtom(nd, op->nfso_own->nfsow_owner, NFSV4CL_LOCKNAMELEN);
393 1.1 dholland NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
394 1.1 dholland *tl++ = txdr_unsigned(NFSV4OPEN_NOCREATE);
395 1.1 dholland if (reclaim) {
396 1.1 dholland *tl = txdr_unsigned(NFSV4OPEN_CLAIMPREVIOUS);
397 1.1 dholland NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
398 1.1 dholland *tl = txdr_unsigned(delegtype);
399 1.1 dholland } else {
400 1.1 dholland if (dp != NULL) {
401 1.1 dholland *tl = txdr_unsigned(NFSV4OPEN_CLAIMDELEGATECUR);
402 1.1 dholland NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID);
403 1.1 dholland if (NFSHASNFSV4N(nmp))
404 1.1 dholland *tl++ = 0;
405 1.1 dholland else
406 1.1 dholland *tl++ = dp->nfsdl_stateid.seqid;
407 1.1 dholland *tl++ = dp->nfsdl_stateid.other[0];
408 1.1 dholland *tl++ = dp->nfsdl_stateid.other[1];
409 1.1 dholland *tl = dp->nfsdl_stateid.other[2];
410 1.1 dholland } else {
411 1.1 dholland *tl = txdr_unsigned(NFSV4OPEN_CLAIMNULL);
412 1.1 dholland }
413 1.1 dholland (void) nfsm_strtom(nd, name, namelen);
414 1.1 dholland }
415 1.1 dholland NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
416 1.1 dholland *tl = txdr_unsigned(NFSV4OP_GETATTR);
417 1.1 dholland NFSZERO_ATTRBIT(&attrbits);
418 1.1 dholland NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_CHANGE);
419 1.1 dholland NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEMODIFY);
420 1.1 dholland (void) nfsrv_putattrbit(nd, &attrbits);
421 1.1 dholland if (syscred)
422 1.1 dholland nd->nd_flag |= ND_USEGSSNAME;
423 1.1 dholland error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, vp, p, cred,
424 1.1 dholland NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
425 1.1 dholland if (error)
426 1.1 dholland return (error);
427 1.1 dholland NFSCL_INCRSEQID(op->nfso_own->nfsow_seqid, nd);
428 1.1 dholland if (!nd->nd_repstat) {
429 1.1 dholland NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
430 1.1 dholland 6 * NFSX_UNSIGNED);
431 1.1 dholland op->nfso_stateid.seqid = *tl++;
432 1.1 dholland op->nfso_stateid.other[0] = *tl++;
433 1.1 dholland op->nfso_stateid.other[1] = *tl++;
434 1.1 dholland op->nfso_stateid.other[2] = *tl;
435 1.1 dholland rflags = fxdr_unsigned(u_int32_t, *(tl + 6));
436 1.1 dholland error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
437 1.1 dholland if (error)
438 1.1 dholland goto nfsmout;
439 1.1 dholland NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
440 1.1 dholland deleg = fxdr_unsigned(u_int32_t, *tl);
441 1.1 dholland if (deleg == NFSV4OPEN_DELEGATEREAD ||
442 1.1 dholland deleg == NFSV4OPEN_DELEGATEWRITE) {
443 1.1 dholland if (!(op->nfso_own->nfsow_clp->nfsc_flags &
444 1.1 dholland NFSCLFLAGS_FIRSTDELEG))
445 1.1 dholland op->nfso_own->nfsow_clp->nfsc_flags |=
446 1.1 dholland (NFSCLFLAGS_FIRSTDELEG | NFSCLFLAGS_GOTDELEG);
447 1.1 dholland MALLOC(ndp, struct nfscldeleg *,
448 1.1 dholland sizeof (struct nfscldeleg) + newfhlen,
449 1.1 dholland M_NFSCLDELEG, M_WAITOK);
450 1.1 dholland LIST_INIT(&ndp->nfsdl_owner);
451 1.1 dholland LIST_INIT(&ndp->nfsdl_lock);
452 1.1 dholland ndp->nfsdl_clp = op->nfso_own->nfsow_clp;
453 1.1 dholland ndp->nfsdl_fhlen = newfhlen;
454 1.1 dholland NFSBCOPY(newfhp, ndp->nfsdl_fh, newfhlen);
455 1.1 dholland newnfs_copyincred(cred, &ndp->nfsdl_cred);
456 1.1 dholland nfscl_lockinit(&ndp->nfsdl_rwlock);
457 1.1 dholland NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
458 1.1 dholland NFSX_UNSIGNED);
459 1.1 dholland ndp->nfsdl_stateid.seqid = *tl++;
460 1.1 dholland ndp->nfsdl_stateid.other[0] = *tl++;
461 1.1 dholland ndp->nfsdl_stateid.other[1] = *tl++;
462 1.1 dholland ndp->nfsdl_stateid.other[2] = *tl++;
463 1.1 dholland ret = fxdr_unsigned(int, *tl);
464 1.1 dholland if (deleg == NFSV4OPEN_DELEGATEWRITE) {
465 1.1 dholland ndp->nfsdl_flags = NFSCLDL_WRITE;
466 1.1 dholland /*
467 1.1 dholland * Indicates how much the file can grow.
468 1.1 dholland */
469 1.1 dholland NFSM_DISSECT(tl, u_int32_t *,
470 1.1 dholland 3 * NFSX_UNSIGNED);
471 1.1 dholland limitby = fxdr_unsigned(int, *tl++);
472 1.1 dholland switch (limitby) {
473 1.1 dholland case NFSV4OPEN_LIMITSIZE:
474 1.1 dholland ndp->nfsdl_sizelimit = fxdr_hyper(tl);
475 1.1 dholland break;
476 1.1 dholland case NFSV4OPEN_LIMITBLOCKS:
477 1.1 dholland ndp->nfsdl_sizelimit =
478 1.1 dholland fxdr_unsigned(u_int64_t, *tl++);
479 1.1 dholland ndp->nfsdl_sizelimit *=
480 1.1 dholland fxdr_unsigned(u_int64_t, *tl);
481 1.1 dholland break;
482 1.1 dholland default:
483 1.1 dholland error = NFSERR_BADXDR;
484 1.1 dholland goto nfsmout;
485 1.1 dholland };
486 1.1 dholland } else {
487 1.1 dholland ndp->nfsdl_flags = NFSCLDL_READ;
488 1.1 dholland }
489 1.1 dholland if (ret)
490 1.1 dholland ndp->nfsdl_flags |= NFSCLDL_RECALL;
491 1.1 dholland error = nfsrv_dissectace(nd, &ndp->nfsdl_ace, &ret,
492 1.1 dholland &acesize, p);
493 1.1 dholland if (error)
494 1.1 dholland goto nfsmout;
495 1.1 dholland } else if (deleg != NFSV4OPEN_DELEGATENONE) {
496 1.1 dholland error = NFSERR_BADXDR;
497 1.1 dholland goto nfsmout;
498 1.1 dholland }
499 1.1 dholland NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
500 1.1 dholland error = nfsv4_loadattr(nd, NULL, &nfsva, NULL,
501 1.1 dholland NULL, 0, NULL, NULL, NULL, NULL, NULL, 0,
502 1.1 dholland NULL, NULL, NULL, p, cred);
503 1.1 dholland if (error)
504 1.1 dholland goto nfsmout;
505 1.1 dholland if (ndp != NULL) {
506 1.1 dholland ndp->nfsdl_change = nfsva.na_filerev;
507 1.1 dholland ndp->nfsdl_modtime = nfsva.na_mtime;
508 1.1 dholland ndp->nfsdl_flags |= NFSCLDL_MODTIMESET;
509 1.1 dholland }
510 1.1 dholland if (!reclaim && (rflags & NFSV4OPEN_RESULTCONFIRM)) {
511 1.1 dholland do {
512 1.1 dholland ret = nfsrpc_openconfirm(vp, newfhp, newfhlen, op,
513 1.1 dholland cred, p);
514 1.1 dholland if (ret == NFSERR_DELAY)
515 1.1 dholland (void) nfs_catnap(PZERO, ret, "nfs_open");
516 1.1 dholland } while (ret == NFSERR_DELAY);
517 1.1 dholland error = ret;
518 1.1 dholland }
519 1.1 dholland if ((rflags & NFSV4OPEN_LOCKTYPEPOSIX) ||
520 1.1 dholland nfscl_assumeposixlocks)
521 1.1 dholland op->nfso_posixlock = 1;
522 1.1 dholland else
523 1.1 dholland op->nfso_posixlock = 0;
524 1.1 dholland
525 1.1 dholland /*
526 1.1 dholland * If the server is handing out delegations, but we didn't
527 1.1 dholland * get one because an OpenConfirm was required, try the
528 1.1 dholland * Open again, to get a delegation. This is a harmless no-op,
529 1.1 dholland * from a server's point of view.
530 1.1 dholland */
531 1.1 dholland if (!reclaim && (rflags & NFSV4OPEN_RESULTCONFIRM) &&
532 1.1 dholland (op->nfso_own->nfsow_clp->nfsc_flags & NFSCLFLAGS_GOTDELEG)
533 1.1 dholland && !error && dp == NULL && ndp == NULL && !recursed) {
534 1.1 dholland do {
535 1.1 dholland ret = nfsrpc_openrpc(nmp, vp, nfhp, fhlen, newfhp,
536 1.1 dholland newfhlen, mode, op, name, namelen, &ndp, 0, 0x0,
537 1.1 dholland cred, p, syscred, 1);
538 1.1 dholland if (ret == NFSERR_DELAY)
539 1.1 dholland (void) nfs_catnap(PZERO, ret, "nfs_open2");
540 1.1 dholland } while (ret == NFSERR_DELAY);
541 1.1 dholland if (ret) {
542 1.1 dholland if (ndp != NULL)
543 1.1 dholland FREE((caddr_t)ndp, M_NFSCLDELEG);
544 1.1 dholland if (ret == NFSERR_STALECLIENTID ||
545 1.1 dholland ret == NFSERR_STALEDONTRECOVER ||
546 1.1 dholland ret == NFSERR_BADSESSION)
547 1.1 dholland error = ret;
548 1.1 dholland }
549 1.1 dholland }
550 1.1 dholland }
551 1.1 dholland if (nd->nd_repstat != 0 && error == 0)
552 1.1 dholland error = nd->nd_repstat;
553 1.1 dholland if (error == NFSERR_STALECLIENTID || error == NFSERR_BADSESSION)
554 1.1 dholland nfscl_initiate_recovery(op->nfso_own->nfsow_clp);
555 1.1 dholland nfsmout:
556 1.1 dholland if (!error)
557 1.1 dholland *dpp = ndp;
558 1.1 dholland else if (ndp != NULL)
559 1.1 dholland FREE((caddr_t)ndp, M_NFSCLDELEG);
560 1.1 dholland mbuf_freem(nd->nd_mrep);
561 1.1 dholland return (error);
562 1.1 dholland }
563 1.1 dholland
564 1.1 dholland /*
565 1.1 dholland * open downgrade rpc
566 1.1 dholland */
567 1.1 dholland APPLESTATIC int
568 1.1 dholland nfsrpc_opendowngrade(vnode_t vp, u_int32_t mode, struct nfsclopen *op,
569 1.1 dholland struct ucred *cred, NFSPROC_T *p)
570 1.1 dholland {
571 1.1 dholland u_int32_t *tl;
572 1.1 dholland struct nfsrv_descript nfsd, *nd = &nfsd;
573 1.1 dholland int error;
574 1.1 dholland
575 1.1 dholland NFSCL_REQSTART(nd, NFSPROC_OPENDOWNGRADE, vp);
576 1.1 dholland NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID + 3 * NFSX_UNSIGNED);
577 1.1 dholland if (NFSHASNFSV4N(VFSTONFS(vnode_mount(vp))))
578 1.1 dholland *tl++ = 0;
579 1.1 dholland else
580 1.1 dholland *tl++ = op->nfso_stateid.seqid;
581 1.1 dholland *tl++ = op->nfso_stateid.other[0];
582 1.1 dholland *tl++ = op->nfso_stateid.other[1];
583 1.1 dholland *tl++ = op->nfso_stateid.other[2];
584 1.1 dholland *tl++ = txdr_unsigned(op->nfso_own->nfsow_seqid);
585 1.1 dholland *tl++ = txdr_unsigned(mode & NFSV4OPEN_ACCESSBOTH);
586 1.1 dholland *tl = txdr_unsigned((mode >> NFSLCK_SHIFT) & NFSV4OPEN_DENYBOTH);
587 1.1 dholland error = nfscl_request(nd, vp, p, cred, NULL);
588 1.1 dholland if (error)
589 1.1 dholland return (error);
590 1.1 dholland NFSCL_INCRSEQID(op->nfso_own->nfsow_seqid, nd);
591 1.1 dholland if (!nd->nd_repstat) {
592 1.1 dholland NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID);
593 1.1 dholland op->nfso_stateid.seqid = *tl++;
594 1.1 dholland op->nfso_stateid.other[0] = *tl++;
595 1.1 dholland op->nfso_stateid.other[1] = *tl++;
596 1.1 dholland op->nfso_stateid.other[2] = *tl;
597 1.1 dholland }
598 1.1 dholland if (nd->nd_repstat && error == 0)
599 1.1 dholland error = nd->nd_repstat;
600 1.1 dholland if (error == NFSERR_STALESTATEID || error == NFSERR_BADSESSION)
601 1.1 dholland nfscl_initiate_recovery(op->nfso_own->nfsow_clp);
602 1.1 dholland nfsmout:
603 1.1 dholland mbuf_freem(nd->nd_mrep);
604 1.1 dholland return (error);
605 1.1 dholland }
606 1.1 dholland
607 1.1 dholland /*
608 1.1 dholland * V4 Close operation.
609 1.1 dholland */
610 1.1 dholland APPLESTATIC int
611 1.1 dholland nfsrpc_close(vnode_t vp, int doclose, NFSPROC_T *p)
612 1.1 dholland {
613 1.1 dholland struct nfsclclient *clp;
614 1.1 dholland int error;
615 1.1 dholland
616 1.1 dholland if (vnode_vtype(vp) != VREG)
617 1.1 dholland return (0);
618 1.1 dholland if (doclose)
619 1.1 dholland error = nfscl_doclose(vp, &clp, p);
620 1.1 dholland else
621 1.1 dholland error = nfscl_getclose(vp, &clp);
622 1.1 dholland if (error)
623 1.1 dholland return (error);
624 1.1 dholland
625 1.1 dholland nfscl_clientrelease(clp);
626 1.1 dholland return (0);
627 1.1 dholland }
628 1.1 dholland
629 1.1 dholland /*
630 1.1 dholland * Close the open.
631 1.1 dholland */
632 1.1 dholland APPLESTATIC void
633 1.1 dholland nfsrpc_doclose(struct nfsmount *nmp, struct nfsclopen *op, NFSPROC_T *p)
634 1.1 dholland {
635 1.1 dholland struct nfsrv_descript nfsd, *nd = &nfsd;
636 1.1 dholland struct nfscllockowner *lp, *nlp;
637 1.1 dholland struct nfscllock *lop, *nlop;
638 1.1 dholland struct ucred *tcred;
639 1.1 dholland u_int64_t off = 0, len = 0;
640 1.1 dholland u_int32_t type = NFSV4LOCKT_READ;
641 1.1 dholland int error, do_unlock, trycnt;
642 1.1 dholland
643 1.1 dholland tcred = newnfs_getcred();
644 1.1 dholland newnfs_copycred(&op->nfso_cred, tcred);
645 1.1 dholland /*
646 1.1 dholland * (Theoretically this could be done in the same
647 1.1 dholland * compound as the close, but having multiple
648 1.1 dholland * sequenced Ops in the same compound might be
649 1.1 dholland * too scary for some servers.)
650 1.1 dholland */
651 1.1 dholland if (op->nfso_posixlock) {
652 1.1 dholland off = 0;
653 1.1 dholland len = NFS64BITSSET;
654 1.1 dholland type = NFSV4LOCKT_READ;
655 1.1 dholland }
656 1.1 dholland
657 1.1 dholland /*
658 1.1 dholland * Since this function is only called from VOP_INACTIVE(), no
659 1.1 dholland * other thread will be manipulating this Open. As such, the
660 1.1 dholland * lock lists are not being changed by other threads, so it should
661 1.1 dholland * be safe to do this without locking.
662 1.1 dholland */
663 1.1 dholland LIST_FOREACH(lp, &op->nfso_lock, nfsl_list) {
664 1.1 dholland do_unlock = 1;
665 1.1 dholland LIST_FOREACH_SAFE(lop, &lp->nfsl_lock, nfslo_list, nlop) {
666 1.1 dholland if (op->nfso_posixlock == 0) {
667 1.1 dholland off = lop->nfslo_first;
668 1.1 dholland len = lop->nfslo_end - lop->nfslo_first;
669 1.1 dholland if (lop->nfslo_type == F_WRLCK)
670 1.1 dholland type = NFSV4LOCKT_WRITE;
671 1.1 dholland else
672 1.1 dholland type = NFSV4LOCKT_READ;
673 1.1 dholland }
674 1.1 dholland if (do_unlock) {
675 1.1 dholland trycnt = 0;
676 1.1 dholland do {
677 1.1 dholland error = nfsrpc_locku(nd, nmp, lp, off,
678 1.1 dholland len, type, tcred, p, 0);
679 1.1 dholland if ((nd->nd_repstat == NFSERR_GRACE ||
680 1.1 dholland nd->nd_repstat == NFSERR_DELAY) &&
681 1.1 dholland error == 0)
682 1.1 dholland (void) nfs_catnap(PZERO,
683 1.1 dholland (int)nd->nd_repstat,
684 1.1 dholland "nfs_close");
685 1.1 dholland } while ((nd->nd_repstat == NFSERR_GRACE ||
686 1.1 dholland nd->nd_repstat == NFSERR_DELAY) &&
687 1.1 dholland error == 0 && trycnt++ < 5);
688 1.1 dholland if (op->nfso_posixlock)
689 1.1 dholland do_unlock = 0;
690 1.1 dholland }
691 1.1 dholland nfscl_freelock(lop, 0);
692 1.1 dholland }
693 1.1 dholland /*
694 1.1 dholland * Do a ReleaseLockOwner.
695 1.1 dholland * The lock owner name nfsl_owner may be used by other opens for
696 1.1 dholland * other files but the lock_owner4 name that nfsrpc_rellockown()
697 1.1 dholland * puts on the wire has the file handle for this file appended
698 1.1 dholland * to it, so it can be done now.
699 1.1 dholland */
700 1.1 dholland (void)nfsrpc_rellockown(nmp, lp, lp->nfsl_open->nfso_fh,
701 1.1 dholland lp->nfsl_open->nfso_fhlen, tcred, p);
702 1.1 dholland }
703 1.1 dholland
704 1.1 dholland /*
705 1.1 dholland * There could be other Opens for different files on the same
706 1.1 dholland * OpenOwner, so locking is required.
707 1.1 dholland */
708 1.1 dholland NFSLOCKCLSTATE();
709 1.1 dholland nfscl_lockexcl(&op->nfso_own->nfsow_rwlock, NFSCLSTATEMUTEXPTR);
710 1.1 dholland NFSUNLOCKCLSTATE();
711 1.1 dholland do {
712 1.1 dholland error = nfscl_tryclose(op, tcred, nmp, p);
713 1.1 dholland if (error == NFSERR_GRACE)
714 1.1 dholland (void) nfs_catnap(PZERO, error, "nfs_close");
715 1.1 dholland } while (error == NFSERR_GRACE);
716 1.1 dholland NFSLOCKCLSTATE();
717 1.1 dholland nfscl_lockunlock(&op->nfso_own->nfsow_rwlock);
718 1.1 dholland
719 1.1 dholland LIST_FOREACH_SAFE(lp, &op->nfso_lock, nfsl_list, nlp)
720 1.1 dholland nfscl_freelockowner(lp, 0);
721 1.1 dholland nfscl_freeopen(op, 0);
722 1.1 dholland NFSUNLOCKCLSTATE();
723 1.1 dholland NFSFREECRED(tcred);
724 1.1 dholland }
725 1.1 dholland
726 1.1 dholland /*
727 1.1 dholland * The actual Close RPC.
728 1.1 dholland */
729 1.1 dholland APPLESTATIC int
730 1.1 dholland nfsrpc_closerpc(struct nfsrv_descript *nd, struct nfsmount *nmp,
731 1.1 dholland struct nfsclopen *op, struct ucred *cred, NFSPROC_T *p,
732 1.1 dholland int syscred)
733 1.1 dholland {
734 1.1 dholland u_int32_t *tl;
735 1.1 dholland int error;
736 1.1 dholland
737 1.1 dholland nfscl_reqstart(nd, NFSPROC_CLOSE, nmp, op->nfso_fh,
738 1.1 dholland op->nfso_fhlen, NULL, NULL);
739 1.1 dholland NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED + NFSX_STATEID);
740 1.1 dholland *tl++ = txdr_unsigned(op->nfso_own->nfsow_seqid);
741 1.1 dholland if (NFSHASNFSV4N(nmp))
742 1.1 dholland *tl++ = 0;
743 1.1 dholland else
744 1.1 dholland *tl++ = op->nfso_stateid.seqid;
745 1.1 dholland *tl++ = op->nfso_stateid.other[0];
746 1.1 dholland *tl++ = op->nfso_stateid.other[1];
747 1.1 dholland *tl = op->nfso_stateid.other[2];
748 1.1 dholland if (syscred)
749 1.1 dholland nd->nd_flag |= ND_USEGSSNAME;
750 1.1 dholland error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
751 1.1 dholland NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
752 1.1 dholland if (error)
753 1.1 dholland return (error);
754 1.1 dholland NFSCL_INCRSEQID(op->nfso_own->nfsow_seqid, nd);
755 1.1 dholland if (nd->nd_repstat == 0)
756 1.1 dholland NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID);
757 1.1 dholland error = nd->nd_repstat;
758 1.1 dholland if (error == NFSERR_STALESTATEID || error == NFSERR_BADSESSION)
759 1.1 dholland nfscl_initiate_recovery(op->nfso_own->nfsow_clp);
760 1.1 dholland nfsmout:
761 1.1 dholland mbuf_freem(nd->nd_mrep);
762 1.1 dholland return (error);
763 1.1 dholland }
764 1.1 dholland
765 1.1 dholland /*
766 1.1 dholland * V4 Open Confirm RPC.
767 1.1 dholland */
768 1.1 dholland APPLESTATIC int
769 1.1 dholland nfsrpc_openconfirm(vnode_t vp, u_int8_t *nfhp, int fhlen,
770 1.1 dholland struct nfsclopen *op, struct ucred *cred, NFSPROC_T *p)
771 1.1 dholland {
772 1.1 dholland u_int32_t *tl;
773 1.1 dholland struct nfsrv_descript nfsd, *nd = &nfsd;
774 1.1 dholland struct nfsmount *nmp;
775 1.1 dholland int error;
776 1.1 dholland
777 1.1 dholland nmp = VFSTONFS(vnode_mount(vp));
778 1.1 dholland if (NFSHASNFSV4N(nmp))
779 1.1 dholland return (0); /* No confirmation for NFSv4.1. */
780 1.1 dholland nfscl_reqstart(nd, NFSPROC_OPENCONFIRM, nmp, nfhp, fhlen, NULL, NULL);
781 1.1 dholland NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED + NFSX_STATEID);
782 1.1 dholland *tl++ = op->nfso_stateid.seqid;
783 1.1 dholland *tl++ = op->nfso_stateid.other[0];
784 1.1 dholland *tl++ = op->nfso_stateid.other[1];
785 1.1 dholland *tl++ = op->nfso_stateid.other[2];
786 1.1 dholland *tl = txdr_unsigned(op->nfso_own->nfsow_seqid);
787 1.1 dholland error = nfscl_request(nd, vp, p, cred, NULL);
788 1.1 dholland if (error)
789 1.1 dholland return (error);
790 1.1 dholland NFSCL_INCRSEQID(op->nfso_own->nfsow_seqid, nd);
791 1.1 dholland if (!nd->nd_repstat) {
792 1.1 dholland NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID);
793 1.1 dholland op->nfso_stateid.seqid = *tl++;
794 1.1 dholland op->nfso_stateid.other[0] = *tl++;
795 1.1 dholland op->nfso_stateid.other[1] = *tl++;
796 1.1 dholland op->nfso_stateid.other[2] = *tl;
797 1.1 dholland }
798 1.1 dholland error = nd->nd_repstat;
799 1.1 dholland if (error == NFSERR_STALESTATEID || error == NFSERR_BADSESSION)
800 1.1 dholland nfscl_initiate_recovery(op->nfso_own->nfsow_clp);
801 1.1 dholland nfsmout:
802 1.1 dholland mbuf_freem(nd->nd_mrep);
803 1.1 dholland return (error);
804 1.1 dholland }
805 1.1 dholland
806 1.1 dholland /*
807 1.1 dholland * Do the setclientid and setclientid confirm RPCs. Called from nfs_statfs()
808 1.1 dholland * when a mount has just occurred and when the server replies NFSERR_EXPIRED.
809 1.1 dholland */
810 1.1 dholland APPLESTATIC int
811 1.1 dholland nfsrpc_setclient(struct nfsmount *nmp, struct nfsclclient *clp, int reclaim,
812 1.1 dholland struct ucred *cred, NFSPROC_T *p)
813 1.1 dholland {
814 1.1 dholland u_int32_t *tl;
815 1.1 dholland struct nfsrv_descript nfsd;
816 1.1 dholland struct nfsrv_descript *nd = &nfsd;
817 1.1 dholland nfsattrbit_t attrbits;
818 1.1 dholland u_int8_t *cp = NULL, *cp2, addr[INET6_ADDRSTRLEN + 9];
819 1.1 dholland u_short port;
820 1.1 dholland int error, isinet6 = 0, callblen;
821 1.1 dholland nfsquad_t confirm;
822 1.1 dholland u_int32_t lease;
823 1.1 dholland static u_int32_t rev = 0;
824 1.1 dholland struct nfsclds *dsp, *ndsp, *tdsp;
825 1.1 dholland
826 1.1 dholland if (nfsboottime.tv_sec == 0)
827 1.1 dholland NFSSETBOOTTIME(nfsboottime);
828 1.1 dholland clp->nfsc_rev = rev++;
829 1.1 dholland if (NFSHASNFSV4N(nmp)) {
830 1.1 dholland error = nfsrpc_exchangeid(nmp, clp, &nmp->nm_sockreq,
831 1.1 dholland NFSV4EXCH_USEPNFSMDS | NFSV4EXCH_USENONPNFS, &dsp, cred, p);
832 1.1 dholland NFSCL_DEBUG(1, "aft exch=%d\n", error);
833 1.1 dholland if (error == 0) {
834 1.1 dholland error = nfsrpc_createsession(nmp, &dsp->nfsclds_sess,
835 1.1 dholland &nmp->nm_sockreq,
836 1.1 dholland dsp->nfsclds_sess.nfsess_sequenceid, 1, cred, p);
837 1.1 dholland if (error == 0) {
838 1.1 dholland NFSLOCKMNT(nmp);
839 1.1 dholland TAILQ_FOREACH_SAFE(tdsp, &nmp->nm_sess,
840 1.1 dholland nfsclds_list, ndsp)
841 1.1 dholland nfscl_freenfsclds(tdsp);
842 1.1 dholland TAILQ_INIT(&nmp->nm_sess);
843 1.1 dholland TAILQ_INSERT_HEAD(&nmp->nm_sess, dsp,
844 1.1 dholland nfsclds_list);
845 1.1 dholland NFSUNLOCKMNT(nmp);
846 1.1 dholland } else
847 1.1 dholland nfscl_freenfsclds(dsp);
848 1.1 dholland NFSCL_DEBUG(1, "aft createsess=%d\n", error);
849 1.1 dholland }
850 1.1 dholland if (error == 0 && reclaim == 0) {
851 1.1 dholland error = nfsrpc_reclaimcomplete(nmp, cred, p);
852 1.1 dholland NFSCL_DEBUG(1, "aft reclaimcomp=%d\n", error);
853 1.1 dholland if (error == NFSERR_COMPLETEALREADY ||
854 1.1 dholland error == NFSERR_NOTSUPP)
855 1.1 dholland /* Ignore this error. */
856 1.1 dholland error = 0;
857 1.1 dholland }
858 1.1 dholland return (error);
859 1.1 dholland }
860 1.1 dholland
861 1.1 dholland /*
862 1.1 dholland * Allocate a single session structure for NFSv4.0, because some of
863 1.1 dholland * the fields are used by NFSv4.0 although it doesn't do a session.
864 1.1 dholland */
865 1.1 dholland dsp = malloc(sizeof(struct nfsclds), M_NFSCLDS, M_WAITOK | M_ZERO);
866 1.1 dholland mtx_init(&dsp->nfsclds_mtx, "nfsds", NULL, MTX_DEF);
867 1.1 dholland mtx_init(&dsp->nfsclds_sess.nfsess_mtx, "nfssession", NULL, MTX_DEF);
868 1.1 dholland NFSLOCKMNT(nmp);
869 1.1 dholland TAILQ_INSERT_HEAD(&nmp->nm_sess, dsp, nfsclds_list);
870 1.1 dholland NFSUNLOCKMNT(nmp);
871 1.1 dholland
872 1.1 dholland nfscl_reqstart(nd, NFSPROC_SETCLIENTID, nmp, NULL, 0, NULL, NULL);
873 1.1 dholland NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
874 1.1 dholland *tl++ = txdr_unsigned(nfsboottime.tv_sec);
875 1.1 dholland *tl = txdr_unsigned(clp->nfsc_rev);
876 1.1 dholland (void) nfsm_strtom(nd, clp->nfsc_id, clp->nfsc_idlen);
877 1.1 dholland
878 1.1 dholland /*
879 1.1 dholland * set up the callback address
880 1.1 dholland */
881 1.1 dholland NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
882 1.1 dholland *tl = txdr_unsigned(NFS_CALLBCKPROG);
883 1.1 dholland callblen = strlen(nfsv4_callbackaddr);
884 1.1 dholland if (callblen == 0)
885 1.1 dholland cp = nfscl_getmyip(nmp, &isinet6);
886 1.1 dholland if (nfscl_enablecallb && nfs_numnfscbd > 0 &&
887 1.1 dholland (callblen > 0 || cp != NULL)) {
888 1.1 dholland port = htons(nfsv4_cbport);
889 1.1 dholland cp2 = (u_int8_t *)&port;
890 1.1 dholland #ifdef INET6
891 1.1 dholland if ((callblen > 0 &&
892 1.1 dholland strchr(nfsv4_callbackaddr, ':')) || isinet6) {
893 1.1 dholland char ip6buf[INET6_ADDRSTRLEN], *ip6add;
894 1.1 dholland
895 1.1 dholland (void) nfsm_strtom(nd, "tcp6", 4);
896 1.1 dholland if (callblen == 0) {
897 1.1 dholland ip6_sprintf(ip6buf, (struct in6_addr *)cp);
898 1.1 dholland ip6add = ip6buf;
899 1.1 dholland } else {
900 1.1 dholland ip6add = nfsv4_callbackaddr;
901 1.1 dholland }
902 1.1 dholland snprintf(addr, INET6_ADDRSTRLEN + 9, "%s.%d.%d",
903 1.1 dholland ip6add, cp2[0], cp2[1]);
904 1.1 dholland } else
905 1.1 dholland #endif
906 1.1 dholland {
907 1.1 dholland (void) nfsm_strtom(nd, "tcp", 3);
908 1.1 dholland if (callblen == 0)
909 1.1 dholland snprintf(addr, INET6_ADDRSTRLEN + 9,
910 1.1 dholland "%d.%d.%d.%d.%d.%d", cp[0], cp[1],
911 1.1 dholland cp[2], cp[3], cp2[0], cp2[1]);
912 1.1 dholland else
913 1.1 dholland snprintf(addr, INET6_ADDRSTRLEN + 9,
914 1.1 dholland "%s.%d.%d", nfsv4_callbackaddr,
915 1.1 dholland cp2[0], cp2[1]);
916 1.1 dholland }
917 1.1 dholland (void) nfsm_strtom(nd, addr, strlen(addr));
918 1.1 dholland } else {
919 1.1 dholland (void) nfsm_strtom(nd, "tcp", 3);
920 1.1 dholland (void) nfsm_strtom(nd, "0.0.0.0.0.0", 11);
921 1.1 dholland }
922 1.1 dholland NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
923 1.1 dholland *tl = txdr_unsigned(clp->nfsc_cbident);
924 1.1 dholland nd->nd_flag |= ND_USEGSSNAME;
925 1.1 dholland error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
926 1.1 dholland NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
927 1.1 dholland if (error)
928 1.1 dholland return (error);
929 1.1 dholland if (nd->nd_repstat == 0) {
930 1.1 dholland NFSM_DISSECT(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
931 1.1 dholland NFSMNT_MDSSESSION(nmp)->nfsess_clientid.lval[0] = *tl++;
932 1.1 dholland NFSMNT_MDSSESSION(nmp)->nfsess_clientid.lval[1] = *tl++;
933 1.1 dholland confirm.lval[0] = *tl++;
934 1.1 dholland confirm.lval[1] = *tl;
935 1.1 dholland mbuf_freem(nd->nd_mrep);
936 1.1 dholland nd->nd_mrep = NULL;
937 1.1 dholland
938 1.1 dholland /*
939 1.1 dholland * and confirm it.
940 1.1 dholland */
941 1.1 dholland nfscl_reqstart(nd, NFSPROC_SETCLIENTIDCFRM, nmp, NULL, 0, NULL,
942 1.1 dholland NULL);
943 1.1 dholland NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
944 1.1 dholland *tl++ = NFSMNT_MDSSESSION(nmp)->nfsess_clientid.lval[0];
945 1.1 dholland *tl++ = NFSMNT_MDSSESSION(nmp)->nfsess_clientid.lval[1];
946 1.1 dholland *tl++ = confirm.lval[0];
947 1.1 dholland *tl = confirm.lval[1];
948 1.1 dholland nd->nd_flag |= ND_USEGSSNAME;
949 1.1 dholland error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p,
950 1.1 dholland cred, NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
951 1.1 dholland if (error)
952 1.1 dholland return (error);
953 1.1 dholland mbuf_freem(nd->nd_mrep);
954 1.1 dholland nd->nd_mrep = NULL;
955 1.1 dholland if (nd->nd_repstat == 0) {
956 1.1 dholland nfscl_reqstart(nd, NFSPROC_GETATTR, nmp, nmp->nm_fh,
957 1.1 dholland nmp->nm_fhsize, NULL, NULL);
958 1.1 dholland NFSZERO_ATTRBIT(&attrbits);
959 1.1 dholland NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_LEASETIME);
960 1.1 dholland (void) nfsrv_putattrbit(nd, &attrbits);
961 1.1 dholland nd->nd_flag |= ND_USEGSSNAME;
962 1.1 dholland error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p,
963 1.1 dholland cred, NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
964 1.1 dholland if (error)
965 1.1 dholland return (error);
966 1.1 dholland if (nd->nd_repstat == 0) {
967 1.1 dholland error = nfsv4_loadattr(nd, NULL, NULL, NULL, NULL, 0, NULL,
968 1.1 dholland NULL, NULL, NULL, NULL, 0, NULL, &lease, NULL, p, cred);
969 1.1 dholland if (error)
970 1.1 dholland goto nfsmout;
971 1.1 dholland clp->nfsc_renew = NFSCL_RENEW(lease);
972 1.1 dholland clp->nfsc_expire = NFSD_MONOSEC + clp->nfsc_renew;
973 1.1 dholland clp->nfsc_clientidrev++;
974 1.1 dholland if (clp->nfsc_clientidrev == 0)
975 1.1 dholland clp->nfsc_clientidrev++;
976 1.1 dholland }
977 1.1 dholland }
978 1.1 dholland }
979 1.1 dholland error = nd->nd_repstat;
980 1.1 dholland nfsmout:
981 1.1 dholland mbuf_freem(nd->nd_mrep);
982 1.1 dholland return (error);
983 1.1 dholland }
984 1.1 dholland
985 1.1 dholland /*
986 1.1 dholland * nfs getattr call.
987 1.1 dholland */
988 1.1 dholland APPLESTATIC int
989 1.1 dholland nfsrpc_getattr(vnode_t vp, struct ucred *cred, NFSPROC_T *p,
990 1.1 dholland struct nfsvattr *nap, void *stuff)
991 1.1 dholland {
992 1.1 dholland struct nfsrv_descript nfsd, *nd = &nfsd;
993 1.1 dholland int error;
994 1.1 dholland nfsattrbit_t attrbits;
995 1.1 dholland
996 1.1 dholland NFSCL_REQSTART(nd, NFSPROC_GETATTR, vp);
997 1.1 dholland if (nd->nd_flag & ND_NFSV4) {
998 1.1 dholland NFSGETATTR_ATTRBIT(&attrbits);
999 1.1 dholland (void) nfsrv_putattrbit(nd, &attrbits);
1000 1.1 dholland }
1001 1.1 dholland error = nfscl_request(nd, vp, p, cred, stuff);
1002 1.1 dholland if (error)
1003 1.1 dholland return (error);
1004 1.1 dholland if (!nd->nd_repstat)
1005 1.1 dholland error = nfsm_loadattr(nd, nap);
1006 1.1 dholland else
1007 1.1 dholland error = nd->nd_repstat;
1008 1.1 dholland mbuf_freem(nd->nd_mrep);
1009 1.1 dholland return (error);
1010 1.1 dholland }
1011 1.1 dholland
1012 1.1 dholland /*
1013 1.1 dholland * nfs getattr call with non-vnode arguemnts.
1014 1.1 dholland */
1015 1.1 dholland APPLESTATIC int
1016 1.1 dholland nfsrpc_getattrnovp(struct nfsmount *nmp, u_int8_t *fhp, int fhlen, int syscred,
1017 1.1 dholland struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, u_int64_t *xidp,
1018 1.1 dholland uint32_t *leasep)
1019 1.1 dholland {
1020 1.1 dholland struct nfsrv_descript nfsd, *nd = &nfsd;
1021 1.1 dholland int error, vers = NFS_VER2;
1022 1.1 dholland nfsattrbit_t attrbits;
1023 1.1 dholland
1024 1.1 dholland nfscl_reqstart(nd, NFSPROC_GETATTR, nmp, fhp, fhlen, NULL, NULL);
1025 1.1 dholland if (nd->nd_flag & ND_NFSV4) {
1026 1.1 dholland vers = NFS_VER4;
1027 1.1 dholland NFSGETATTR_ATTRBIT(&attrbits);
1028 1.1 dholland NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_LEASETIME);
1029 1.1 dholland (void) nfsrv_putattrbit(nd, &attrbits);
1030 1.1 dholland } else if (nd->nd_flag & ND_NFSV3) {
1031 1.1 dholland vers = NFS_VER3;
1032 1.1 dholland }
1033 1.1 dholland if (syscred)
1034 1.1 dholland nd->nd_flag |= ND_USEGSSNAME;
1035 1.1 dholland error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
1036 1.1 dholland NFS_PROG, vers, NULL, 1, xidp, NULL);
1037 1.1 dholland if (error)
1038 1.1 dholland return (error);
1039 1.1 dholland if (nd->nd_repstat == 0) {
1040 1.1 dholland if ((nd->nd_flag & ND_NFSV4) != 0)
1041 1.1 dholland error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0,
1042 1.1 dholland NULL, NULL, NULL, NULL, NULL, 0, NULL, leasep, NULL,
1043 1.1 dholland NULL, NULL);
1044 1.1 dholland else
1045 1.1 dholland error = nfsm_loadattr(nd, nap);
1046 1.1 dholland } else
1047 1.1 dholland error = nd->nd_repstat;
1048 1.1 dholland mbuf_freem(nd->nd_mrep);
1049 1.1 dholland return (error);
1050 1.1 dholland }
1051 1.1 dholland
1052 1.1 dholland /*
1053 1.1 dholland * Do an nfs setattr operation.
1054 1.1 dholland */
1055 1.1 dholland APPLESTATIC int
1056 1.1 dholland nfsrpc_setattr(vnode_t vp, struct vattr *vap, NFSACL_T *aclp,
1057 1.1 dholland struct ucred *cred, NFSPROC_T *p, struct nfsvattr *rnap, int *attrflagp,
1058 1.1 dholland void *stuff)
1059 1.1 dholland {
1060 1.1 dholland int error, expireret = 0, openerr, retrycnt;
1061 1.1 dholland u_int32_t clidrev = 0, mode;
1062 1.1 dholland struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
1063 1.1 dholland struct nfsfh *nfhp;
1064 1.1 dholland nfsv4stateid_t stateid;
1065 1.1 dholland void *lckp;
1066 1.1 dholland
1067 1.1 dholland if (nmp->nm_clp != NULL)
1068 1.1 dholland clidrev = nmp->nm_clp->nfsc_clientidrev;
1069 1.1 dholland if (vap != NULL && NFSATTRISSET(u_quad_t, vap, va_size))
1070 1.1 dholland mode = NFSV4OPEN_ACCESSWRITE;
1071 1.1 dholland else
1072 1.1 dholland mode = NFSV4OPEN_ACCESSREAD;
1073 1.1 dholland retrycnt = 0;
1074 1.1 dholland do {
1075 1.1 dholland lckp = NULL;
1076 1.1 dholland openerr = 1;
1077 1.1 dholland if (NFSHASNFSV4(nmp)) {
1078 1.1 dholland nfhp = VTONFS(vp)->n_fhp;
1079 1.1 dholland error = nfscl_getstateid(vp, nfhp->nfh_fh,
1080 1.1 dholland nfhp->nfh_len, mode, 0, cred, p, &stateid, &lckp);
1081 1.1 dholland if (error && vnode_vtype(vp) == VREG &&
1082 1.1 dholland (mode == NFSV4OPEN_ACCESSWRITE ||
1083 1.1 dholland nfstest_openallsetattr)) {
1084 1.1 dholland /*
1085 1.1 dholland * No Open stateid, so try and open the file
1086 1.1 dholland * now.
1087 1.1 dholland */
1088 1.1 dholland if (mode == NFSV4OPEN_ACCESSWRITE)
1089 1.1 dholland openerr = nfsrpc_open(vp, FWRITE, cred,
1090 1.1 dholland p);
1091 1.1 dholland else
1092 1.1 dholland openerr = nfsrpc_open(vp, FREAD, cred,
1093 1.1 dholland p);
1094 1.1 dholland if (!openerr)
1095 1.1 dholland (void) nfscl_getstateid(vp,
1096 1.1 dholland nfhp->nfh_fh, nfhp->nfh_len,
1097 1.1 dholland mode, 0, cred, p, &stateid, &lckp);
1098 1.1 dholland }
1099 1.1 dholland }
1100 1.1 dholland if (vap != NULL)
1101 1.1 dholland error = nfsrpc_setattrrpc(vp, vap, &stateid, cred, p,
1102 1.1 dholland rnap, attrflagp, stuff);
1103 1.1 dholland else
1104 1.1 dholland error = nfsrpc_setaclrpc(vp, cred, p, aclp, &stateid,
1105 1.1 dholland stuff);
1106 1.1 dholland if (error == NFSERR_STALESTATEID || error == NFSERR_BADSESSION)
1107 1.1 dholland nfscl_initiate_recovery(nmp->nm_clp);
1108 1.1 dholland if (lckp != NULL)
1109 1.1 dholland nfscl_lockderef(lckp);
1110 1.1 dholland if (!openerr)
1111 1.1 dholland (void) nfsrpc_close(vp, 0, p);
1112 1.1 dholland if (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
1113 1.1 dholland error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
1114 1.1 dholland error == NFSERR_OLDSTATEID || error == NFSERR_BADSESSION) {
1115 1.1 dholland (void) nfs_catnap(PZERO, error, "nfs_setattr");
1116 1.1 dholland } else if ((error == NFSERR_EXPIRED ||
1117 1.1 dholland error == NFSERR_BADSTATEID) && clidrev != 0) {
1118 1.1 dholland expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
1119 1.1 dholland }
1120 1.1 dholland retrycnt++;
1121 1.1 dholland } while (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
1122 1.1 dholland error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
1123 1.1 dholland error == NFSERR_BADSESSION ||
1124 1.1 dholland (error == NFSERR_OLDSTATEID && retrycnt < 20) ||
1125 1.1 dholland ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
1126 1.1 dholland expireret == 0 && clidrev != 0 && retrycnt < 4));
1127 1.1 dholland if (error && retrycnt >= 4)
1128 1.1 dholland error = EIO;
1129 1.1 dholland return (error);
1130 1.1 dholland }
1131 1.1 dholland
1132 1.1 dholland static int
1133 1.1 dholland nfsrpc_setattrrpc(vnode_t vp, struct vattr *vap,
1134 1.1 dholland nfsv4stateid_t *stateidp, struct ucred *cred, NFSPROC_T *p,
1135 1.1 dholland struct nfsvattr *rnap, int *attrflagp, void *stuff)
1136 1.1 dholland {
1137 1.1 dholland u_int32_t *tl;
1138 1.1 dholland struct nfsrv_descript nfsd, *nd = &nfsd;
1139 1.1 dholland int error;
1140 1.1 dholland nfsattrbit_t attrbits;
1141 1.1 dholland
1142 1.1 dholland *attrflagp = 0;
1143 1.1 dholland NFSCL_REQSTART(nd, NFSPROC_SETATTR, vp);
1144 1.1 dholland if (nd->nd_flag & ND_NFSV4)
1145 1.1 dholland nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID);
1146 1.1 dholland vap->va_type = vnode_vtype(vp);
1147 1.1 dholland nfscl_fillsattr(nd, vap, vp, NFSSATTR_FULL, 0);
1148 1.1 dholland if (nd->nd_flag & ND_NFSV3) {
1149 1.1 dholland NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1150 1.1 dholland *tl = newnfs_false;
1151 1.1 dholland } else if (nd->nd_flag & ND_NFSV4) {
1152 1.1 dholland NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1153 1.1 dholland *tl = txdr_unsigned(NFSV4OP_GETATTR);
1154 1.1 dholland NFSGETATTR_ATTRBIT(&attrbits);
1155 1.1 dholland (void) nfsrv_putattrbit(nd, &attrbits);
1156 1.1 dholland }
1157 1.1 dholland error = nfscl_request(nd, vp, p, cred, stuff);
1158 1.1 dholland if (error)
1159 1.1 dholland return (error);
1160 1.1 dholland if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4))
1161 1.1 dholland error = nfscl_wcc_data(nd, vp, rnap, attrflagp, NULL, stuff);
1162 1.1 dholland if ((nd->nd_flag & ND_NFSV4) && !error)
1163 1.1 dholland error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
1164 1.1 dholland if (!(nd->nd_flag & ND_NFSV3) && !nd->nd_repstat && !error)
1165 1.1 dholland error = nfscl_postop_attr(nd, rnap, attrflagp, stuff);
1166 1.1 dholland mbuf_freem(nd->nd_mrep);
1167 1.1 dholland if (nd->nd_repstat && !error)
1168 1.1 dholland error = nd->nd_repstat;
1169 1.1 dholland return (error);
1170 1.1 dholland }
1171 1.1 dholland
1172 1.1 dholland /*
1173 1.1 dholland * nfs lookup rpc
1174 1.1 dholland */
1175 1.1 dholland APPLESTATIC int
1176 1.1 dholland nfsrpc_lookup(vnode_t dvp, char *name, int len, struct ucred *cred,
1177 1.1 dholland NFSPROC_T *p, struct nfsvattr *dnap, struct nfsvattr *nap,
1178 1.1 dholland struct nfsfh **nfhpp, int *attrflagp, int *dattrflagp, void *stuff)
1179 1.1 dholland {
1180 1.1 dholland u_int32_t *tl;
1181 1.1 dholland struct nfsrv_descript nfsd, *nd = &nfsd;
1182 1.1 dholland struct nfsmount *nmp;
1183 1.1 dholland struct nfsnode *np;
1184 1.1 dholland struct nfsfh *nfhp;
1185 1.1 dholland nfsattrbit_t attrbits;
1186 1.1 dholland int error = 0, lookupp = 0;
1187 1.1 dholland
1188 1.1 dholland *attrflagp = 0;
1189 1.1 dholland *dattrflagp = 0;
1190 1.1 dholland if (vnode_vtype(dvp) != VDIR)
1191 1.1 dholland return (ENOTDIR);
1192 1.1 dholland nmp = VFSTONFS(vnode_mount(dvp));
1193 1.1 dholland if (len > NFS_MAXNAMLEN)
1194 1.1 dholland return (ENAMETOOLONG);
1195 1.1 dholland if (NFSHASNFSV4(nmp) && len == 1 &&
1196 1.1 dholland name[0] == '.') {
1197 1.1 dholland /*
1198 1.1 dholland * Just return the current dir's fh.
1199 1.1 dholland */
1200 1.1 dholland np = VTONFS(dvp);
1201 1.1 dholland MALLOC(nfhp, struct nfsfh *, sizeof (struct nfsfh) +
1202 1.1 dholland np->n_fhp->nfh_len, M_NFSFH, M_WAITOK);
1203 1.1 dholland nfhp->nfh_len = np->n_fhp->nfh_len;
1204 1.1 dholland NFSBCOPY(np->n_fhp->nfh_fh, nfhp->nfh_fh, nfhp->nfh_len);
1205 1.1 dholland *nfhpp = nfhp;
1206 1.1 dholland return (0);
1207 1.1 dholland }
1208 1.1 dholland if (NFSHASNFSV4(nmp) && len == 2 &&
1209 1.1 dholland name[0] == '.' && name[1] == '.') {
1210 1.1 dholland lookupp = 1;
1211 1.1 dholland NFSCL_REQSTART(nd, NFSPROC_LOOKUPP, dvp);
1212 1.1 dholland } else {
1213 1.1 dholland NFSCL_REQSTART(nd, NFSPROC_LOOKUP, dvp);
1214 1.1 dholland (void) nfsm_strtom(nd, name, len);
1215 1.1 dholland }
1216 1.1 dholland if (nd->nd_flag & ND_NFSV4) {
1217 1.1 dholland NFSGETATTR_ATTRBIT(&attrbits);
1218 1.1 dholland NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1219 1.1 dholland *tl++ = txdr_unsigned(NFSV4OP_GETFH);
1220 1.1 dholland *tl = txdr_unsigned(NFSV4OP_GETATTR);
1221 1.1 dholland (void) nfsrv_putattrbit(nd, &attrbits);
1222 1.1 dholland }
1223 1.1 dholland error = nfscl_request(nd, dvp, p, cred, stuff);
1224 1.1 dholland if (error)
1225 1.1 dholland return (error);
1226 1.1 dholland if (nd->nd_repstat) {
1227 1.1 dholland /*
1228 1.1 dholland * When an NFSv4 Lookupp returns ENOENT, it means that
1229 1.1 dholland * the lookup is at the root of an fs, so return this dir.
1230 1.1 dholland */
1231 1.1 dholland if (nd->nd_repstat == NFSERR_NOENT && lookupp) {
1232 1.1 dholland np = VTONFS(dvp);
1233 1.1 dholland MALLOC(nfhp, struct nfsfh *, sizeof (struct nfsfh) +
1234 1.1 dholland np->n_fhp->nfh_len, M_NFSFH, M_WAITOK);
1235 1.1 dholland nfhp->nfh_len = np->n_fhp->nfh_len;
1236 1.1 dholland NFSBCOPY(np->n_fhp->nfh_fh, nfhp->nfh_fh, nfhp->nfh_len);
1237 1.1 dholland *nfhpp = nfhp;
1238 1.1 dholland mbuf_freem(nd->nd_mrep);
1239 1.1 dholland return (0);
1240 1.1 dholland }
1241 1.1 dholland if (nd->nd_flag & ND_NFSV3)
1242 1.1 dholland error = nfscl_postop_attr(nd, dnap, dattrflagp, stuff);
1243 1.1 dholland goto nfsmout;
1244 1.1 dholland }
1245 1.1 dholland if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) == ND_NFSV4) {
1246 1.1 dholland NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1247 1.1 dholland if (*(tl + 1)) {
1248 1.1 dholland nd->nd_flag |= ND_NOMOREDATA;
1249 1.1 dholland goto nfsmout;
1250 1.1 dholland }
1251 1.1 dholland }
1252 1.1 dholland error = nfsm_getfh(nd, nfhpp);
1253 1.1 dholland if (error)
1254 1.1 dholland goto nfsmout;
1255 1.1 dholland
1256 1.1 dholland error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
1257 1.1 dholland if ((nd->nd_flag & ND_NFSV3) && !error)
1258 1.1 dholland error = nfscl_postop_attr(nd, dnap, dattrflagp, stuff);
1259 1.1 dholland nfsmout:
1260 1.1 dholland mbuf_freem(nd->nd_mrep);
1261 1.1 dholland if (!error && nd->nd_repstat)
1262 1.1 dholland error = nd->nd_repstat;
1263 1.1 dholland return (error);
1264 1.1 dholland }
1265 1.1 dholland
1266 1.1 dholland /*
1267 1.1 dholland * Do a readlink rpc.
1268 1.1 dholland */
1269 1.1 dholland APPLESTATIC int
1270 1.1 dholland nfsrpc_readlink(vnode_t vp, struct uio *uiop, struct ucred *cred,
1271 1.1 dholland NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, void *stuff)
1272 1.1 dholland {
1273 1.1 dholland u_int32_t *tl;
1274 1.1 dholland struct nfsrv_descript nfsd, *nd = &nfsd;
1275 1.1 dholland struct nfsnode *np = VTONFS(vp);
1276 1.1 dholland nfsattrbit_t attrbits;
1277 1.1 dholland int error, len, cangetattr = 1;
1278 1.1 dholland
1279 1.1 dholland *attrflagp = 0;
1280 1.1 dholland NFSCL_REQSTART(nd, NFSPROC_READLINK, vp);
1281 1.1 dholland if (nd->nd_flag & ND_NFSV4) {
1282 1.1 dholland /*
1283 1.1 dholland * And do a Getattr op.
1284 1.1 dholland */
1285 1.1 dholland NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1286 1.1 dholland *tl = txdr_unsigned(NFSV4OP_GETATTR);
1287 1.1 dholland NFSGETATTR_ATTRBIT(&attrbits);
1288 1.1 dholland (void) nfsrv_putattrbit(nd, &attrbits);
1289 1.1 dholland }
1290 1.1 dholland error = nfscl_request(nd, vp, p, cred, stuff);
1291 1.1 dholland if (error)
1292 1.1 dholland return (error);
1293 1.1 dholland if (nd->nd_flag & ND_NFSV3)
1294 1.1 dholland error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
1295 1.1 dholland if (!nd->nd_repstat && !error) {
1296 1.1 dholland NFSM_STRSIZ(len, NFS_MAXPATHLEN);
1297 1.1 dholland /*
1298 1.1 dholland * This seems weird to me, but must have been added to
1299 1.1 dholland * FreeBSD for some reason. The only thing I can think of
1300 1.1 dholland * is that there was/is some server that replies with
1301 1.1 dholland * more link data than it should?
1302 1.1 dholland */
1303 1.1 dholland if (len == NFS_MAXPATHLEN) {
1304 1.1 dholland NFSLOCKNODE(np);
1305 1.1 dholland if (np->n_size > 0 && np->n_size < NFS_MAXPATHLEN) {
1306 1.1 dholland len = np->n_size;
1307 1.1 dholland cangetattr = 0;
1308 1.1 dholland }
1309 1.1 dholland NFSUNLOCKNODE(np);
1310 1.1 dholland }
1311 1.1 dholland error = nfsm_mbufuio(nd, uiop, len);
1312 1.1 dholland if ((nd->nd_flag & ND_NFSV4) && !error && cangetattr)
1313 1.1 dholland error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
1314 1.1 dholland }
1315 1.1 dholland if (nd->nd_repstat && !error)
1316 1.1 dholland error = nd->nd_repstat;
1317 1.1 dholland nfsmout:
1318 1.1 dholland mbuf_freem(nd->nd_mrep);
1319 1.1 dholland return (error);
1320 1.1 dholland }
1321 1.1 dholland
1322 1.1 dholland /*
1323 1.1 dholland * Read operation.
1324 1.1 dholland */
1325 1.1 dholland APPLESTATIC int
1326 1.1 dholland nfsrpc_read(vnode_t vp, struct uio *uiop, struct ucred *cred,
1327 1.1 dholland NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, void *stuff)
1328 1.1 dholland {
1329 1.1 dholland int error, expireret = 0, retrycnt;
1330 1.1 dholland u_int32_t clidrev = 0;
1331 1.1 dholland struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
1332 1.1 dholland struct nfsnode *np = VTONFS(vp);
1333 1.1 dholland struct ucred *newcred;
1334 1.1 dholland struct nfsfh *nfhp = NULL;
1335 1.1 dholland nfsv4stateid_t stateid;
1336 1.1 dholland void *lckp;
1337 1.1 dholland
1338 1.1 dholland if (nmp->nm_clp != NULL)
1339 1.1 dholland clidrev = nmp->nm_clp->nfsc_clientidrev;
1340 1.1 dholland newcred = cred;
1341 1.1 dholland if (NFSHASNFSV4(nmp)) {
1342 1.1 dholland nfhp = np->n_fhp;
1343 1.1 dholland newcred = NFSNEWCRED(cred);
1344 1.1 dholland }
1345 1.1 dholland retrycnt = 0;
1346 1.1 dholland do {
1347 1.1 dholland lckp = NULL;
1348 1.1 dholland if (NFSHASNFSV4(nmp))
1349 1.1 dholland (void)nfscl_getstateid(vp, nfhp->nfh_fh, nfhp->nfh_len,
1350 1.1 dholland NFSV4OPEN_ACCESSREAD, 0, newcred, p, &stateid,
1351 1.1 dholland &lckp);
1352 1.1 dholland error = nfsrpc_readrpc(vp, uiop, newcred, &stateid, p, nap,
1353 1.1 dholland attrflagp, stuff);
1354 1.1 dholland if (error == NFSERR_STALESTATEID || error == NFSERR_BADSESSION)
1355 1.1 dholland nfscl_initiate_recovery(nmp->nm_clp);
1356 1.1 dholland if (lckp != NULL)
1357 1.1 dholland nfscl_lockderef(lckp);
1358 1.1 dholland if (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
1359 1.1 dholland error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
1360 1.1 dholland error == NFSERR_OLDSTATEID || error == NFSERR_BADSESSION) {
1361 1.1 dholland (void) nfs_catnap(PZERO, error, "nfs_read");
1362 1.1 dholland } else if ((error == NFSERR_EXPIRED ||
1363 1.1 dholland error == NFSERR_BADSTATEID) && clidrev != 0) {
1364 1.1 dholland expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
1365 1.1 dholland }
1366 1.1 dholland retrycnt++;
1367 1.1 dholland } while (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
1368 1.1 dholland error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
1369 1.1 dholland error == NFSERR_BADSESSION ||
1370 1.1 dholland (error == NFSERR_OLDSTATEID && retrycnt < 20) ||
1371 1.1 dholland ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
1372 1.1 dholland expireret == 0 && clidrev != 0 && retrycnt < 4));
1373 1.1 dholland if (error && retrycnt >= 4)
1374 1.1 dholland error = EIO;
1375 1.1 dholland if (NFSHASNFSV4(nmp))
1376 1.1 dholland NFSFREECRED(newcred);
1377 1.1 dholland return (error);
1378 1.1 dholland }
1379 1.1 dholland
1380 1.1 dholland /*
1381 1.1 dholland * The actual read RPC.
1382 1.1 dholland */
1383 1.1 dholland static int
1384 1.1 dholland nfsrpc_readrpc(vnode_t vp, struct uio *uiop, struct ucred *cred,
1385 1.1 dholland nfsv4stateid_t *stateidp, NFSPROC_T *p, struct nfsvattr *nap,
1386 1.1 dholland int *attrflagp, void *stuff)
1387 1.1 dholland {
1388 1.1 dholland u_int32_t *tl;
1389 1.1 dholland int error = 0, len, retlen, tsiz, eof = 0;
1390 1.1 dholland struct nfsrv_descript nfsd;
1391 1.1 dholland struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
1392 1.1 dholland struct nfsrv_descript *nd = &nfsd;
1393 1.1 dholland int rsize;
1394 1.1 dholland off_t tmp_off;
1395 1.1 dholland
1396 1.1 dholland *attrflagp = 0;
1397 1.1 dholland tsiz = uio_uio_resid(uiop);
1398 1.1 dholland tmp_off = uiop->uio_offset + tsiz;
1399 1.1 dholland NFSLOCKMNT(nmp);
1400 1.1 dholland if (tmp_off > nmp->nm_maxfilesize || tmp_off < uiop->uio_offset) {
1401 1.1 dholland NFSUNLOCKMNT(nmp);
1402 1.1 dholland return (EFBIG);
1403 1.1 dholland }
1404 1.1 dholland rsize = nmp->nm_rsize;
1405 1.1 dholland NFSUNLOCKMNT(nmp);
1406 1.1 dholland nd->nd_mrep = NULL;
1407 1.1 dholland while (tsiz > 0) {
1408 1.1 dholland *attrflagp = 0;
1409 1.1 dholland len = (tsiz > rsize) ? rsize : tsiz;
1410 1.1 dholland NFSCL_REQSTART(nd, NFSPROC_READ, vp);
1411 1.1 dholland if (nd->nd_flag & ND_NFSV4)
1412 1.1 dholland nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID);
1413 1.1 dholland NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED * 3);
1414 1.1 dholland if (nd->nd_flag & ND_NFSV2) {
1415 1.1 dholland *tl++ = txdr_unsigned(uiop->uio_offset);
1416 1.1 dholland *tl++ = txdr_unsigned(len);
1417 1.1 dholland *tl = 0;
1418 1.1 dholland } else {
1419 1.1 dholland txdr_hyper(uiop->uio_offset, tl);
1420 1.1 dholland *(tl + 2) = txdr_unsigned(len);
1421 1.1 dholland }
1422 1.1 dholland /*
1423 1.1 dholland * Since I can't do a Getattr for NFSv4 for Write, there
1424 1.1 dholland * doesn't seem any point in doing one here, either.
1425 1.1 dholland * (See the comment in nfsrpc_writerpc() for more info.)
1426 1.1 dholland */
1427 1.1 dholland error = nfscl_request(nd, vp, p, cred, stuff);
1428 1.1 dholland if (error)
1429 1.1 dholland return (error);
1430 1.1 dholland if (nd->nd_flag & ND_NFSV3) {
1431 1.1 dholland error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
1432 1.1 dholland } else if (!nd->nd_repstat && (nd->nd_flag & ND_NFSV2)) {
1433 1.1 dholland error = nfsm_loadattr(nd, nap);
1434 1.1 dholland if (!error)
1435 1.1 dholland *attrflagp = 1;
1436 1.1 dholland }
1437 1.1 dholland if (nd->nd_repstat || error) {
1438 1.1 dholland if (!error)
1439 1.1 dholland error = nd->nd_repstat;
1440 1.1 dholland goto nfsmout;
1441 1.1 dholland }
1442 1.1 dholland if (nd->nd_flag & ND_NFSV3) {
1443 1.1 dholland NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1444 1.1 dholland eof = fxdr_unsigned(int, *(tl + 1));
1445 1.1 dholland } else if (nd->nd_flag & ND_NFSV4) {
1446 1.1 dholland NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
1447 1.1 dholland eof = fxdr_unsigned(int, *tl);
1448 1.1 dholland }
1449 1.1 dholland NFSM_STRSIZ(retlen, len);
1450 1.1 dholland error = nfsm_mbufuio(nd, uiop, retlen);
1451 1.1 dholland if (error)
1452 1.1 dholland goto nfsmout;
1453 1.1 dholland mbuf_freem(nd->nd_mrep);
1454 1.1 dholland nd->nd_mrep = NULL;
1455 1.1 dholland tsiz -= retlen;
1456 1.1 dholland if (!(nd->nd_flag & ND_NFSV2)) {
1457 1.1 dholland if (eof || retlen == 0)
1458 1.1 dholland tsiz = 0;
1459 1.1 dholland } else if (retlen < len)
1460 1.1 dholland tsiz = 0;
1461 1.1 dholland }
1462 1.1 dholland return (0);
1463 1.1 dholland nfsmout:
1464 1.1 dholland if (nd->nd_mrep != NULL)
1465 1.1 dholland mbuf_freem(nd->nd_mrep);
1466 1.1 dholland return (error);
1467 1.1 dholland }
1468 1.1 dholland
1469 1.1 dholland /*
1470 1.1 dholland * nfs write operation
1471 1.1 dholland * When called_from_strategy != 0, it should return EIO for an error that
1472 1.1 dholland * indicates recovery is in progress, so that the buffer will be left
1473 1.1 dholland * dirty and be written back to the server later. If it loops around,
1474 1.1 dholland * the recovery thread could get stuck waiting for the buffer and recovery
1475 1.1 dholland * will then deadlock.
1476 1.1 dholland */
1477 1.1 dholland APPLESTATIC int
1478 1.1 dholland nfsrpc_write(vnode_t vp, struct uio *uiop, int *iomode, int *must_commit,
1479 1.1 dholland struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp,
1480 1.1 dholland void *stuff, int called_from_strategy)
1481 1.1 dholland {
1482 1.1 dholland int error, expireret = 0, retrycnt, nostateid;
1483 1.1 dholland u_int32_t clidrev = 0;
1484 1.1 dholland struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
1485 1.1 dholland struct nfsnode *np = VTONFS(vp);
1486 1.1 dholland struct ucred *newcred;
1487 1.1 dholland struct nfsfh *nfhp = NULL;
1488 1.1 dholland nfsv4stateid_t stateid;
1489 1.1 dholland void *lckp;
1490 1.1 dholland
1491 1.1 dholland *must_commit = 0;
1492 1.1 dholland if (nmp->nm_clp != NULL)
1493 1.1 dholland clidrev = nmp->nm_clp->nfsc_clientidrev;
1494 1.1 dholland newcred = cred;
1495 1.1 dholland if (NFSHASNFSV4(nmp)) {
1496 1.1 dholland newcred = NFSNEWCRED(cred);
1497 1.1 dholland nfhp = np->n_fhp;
1498 1.1 dholland }
1499 1.1 dholland retrycnt = 0;
1500 1.1 dholland do {
1501 1.1 dholland lckp = NULL;
1502 1.1 dholland nostateid = 0;
1503 1.1 dholland if (NFSHASNFSV4(nmp)) {
1504 1.1 dholland (void)nfscl_getstateid(vp, nfhp->nfh_fh, nfhp->nfh_len,
1505 1.1 dholland NFSV4OPEN_ACCESSWRITE, 0, newcred, p, &stateid,
1506 1.1 dholland &lckp);
1507 1.1 dholland if (stateid.other[0] == 0 && stateid.other[1] == 0 &&
1508 1.1 dholland stateid.other[2] == 0) {
1509 1.1 dholland nostateid = 1;
1510 1.1 dholland NFSCL_DEBUG(1, "stateid0 in write\n");
1511 1.1 dholland }
1512 1.1 dholland }
1513 1.1 dholland
1514 1.1 dholland /*
1515 1.1 dholland * If there is no stateid for NFSv4, it means this is an
1516 1.1 dholland * extraneous write after close. Basically a poorly
1517 1.1 dholland * implemented buffer cache. Just don't do the write.
1518 1.1 dholland */
1519 1.1 dholland if (nostateid)
1520 1.1 dholland error = 0;
1521 1.1 dholland else
1522 1.1 dholland error = nfsrpc_writerpc(vp, uiop, iomode, must_commit,
1523 1.1 dholland newcred, &stateid, p, nap, attrflagp, stuff);
1524 1.1 dholland if (error == NFSERR_STALESTATEID || error == NFSERR_BADSESSION)
1525 1.1 dholland nfscl_initiate_recovery(nmp->nm_clp);
1526 1.1 dholland if (lckp != NULL)
1527 1.1 dholland nfscl_lockderef(lckp);
1528 1.1 dholland if (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
1529 1.1 dholland error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
1530 1.1 dholland error == NFSERR_OLDSTATEID || error == NFSERR_BADSESSION) {
1531 1.1 dholland (void) nfs_catnap(PZERO, error, "nfs_write");
1532 1.1 dholland } else if ((error == NFSERR_EXPIRED ||
1533 1.1 dholland error == NFSERR_BADSTATEID) && clidrev != 0) {
1534 1.1 dholland expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
1535 1.1 dholland }
1536 1.1 dholland retrycnt++;
1537 1.1 dholland } while (error == NFSERR_GRACE || error == NFSERR_DELAY ||
1538 1.1 dholland ((error == NFSERR_STALESTATEID || error == NFSERR_BADSESSION ||
1539 1.1 dholland error == NFSERR_STALEDONTRECOVER) && called_from_strategy == 0) ||
1540 1.1 dholland (error == NFSERR_OLDSTATEID && retrycnt < 20) ||
1541 1.1 dholland ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
1542 1.1 dholland expireret == 0 && clidrev != 0 && retrycnt < 4));
1543 1.1 dholland if (error != 0 && (retrycnt >= 4 ||
1544 1.1 dholland ((error == NFSERR_STALESTATEID || error == NFSERR_BADSESSION ||
1545 1.1 dholland error == NFSERR_STALEDONTRECOVER) && called_from_strategy != 0)))
1546 1.1 dholland error = EIO;
1547 1.1 dholland if (NFSHASNFSV4(nmp))
1548 1.1 dholland NFSFREECRED(newcred);
1549 1.1 dholland return (error);
1550 1.1 dholland }
1551 1.1 dholland
1552 1.1 dholland /*
1553 1.1 dholland * The actual write RPC.
1554 1.1 dholland */
1555 1.1 dholland static int
1556 1.1 dholland nfsrpc_writerpc(vnode_t vp, struct uio *uiop, int *iomode,
1557 1.1 dholland int *must_commit, struct ucred *cred, nfsv4stateid_t *stateidp,
1558 1.1 dholland NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, void *stuff)
1559 1.1 dholland {
1560 1.1 dholland u_int32_t *tl;
1561 1.1 dholland struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
1562 1.1 dholland struct nfsnode *np = VTONFS(vp);
1563 1.1 dholland int error = 0, len, tsiz, rlen, commit, committed = NFSWRITE_FILESYNC;
1564 1.1 dholland int wccflag = 0, wsize;
1565 1.1 dholland int32_t backup;
1566 1.1 dholland struct nfsrv_descript nfsd;
1567 1.1 dholland struct nfsrv_descript *nd = &nfsd;
1568 1.1 dholland nfsattrbit_t attrbits;
1569 1.1 dholland off_t tmp_off;
1570 1.1 dholland
1571 1.1 dholland KASSERT(uiop->uio_iovcnt == 1, ("nfs: writerpc iovcnt > 1"));
1572 1.1 dholland *attrflagp = 0;
1573 1.1 dholland tsiz = uio_uio_resid(uiop);
1574 1.1 dholland tmp_off = uiop->uio_offset + tsiz;
1575 1.1 dholland NFSLOCKMNT(nmp);
1576 1.1 dholland if (tmp_off > nmp->nm_maxfilesize || tmp_off < uiop->uio_offset) {
1577 1.1 dholland NFSUNLOCKMNT(nmp);
1578 1.1 dholland return (EFBIG);
1579 1.1 dholland }
1580 1.1 dholland wsize = nmp->nm_wsize;
1581 1.1 dholland NFSUNLOCKMNT(nmp);
1582 1.1 dholland nd->nd_mrep = NULL; /* NFSv2 sometimes does a write with */
1583 1.1 dholland nd->nd_repstat = 0; /* uio_resid == 0, so the while is not done */
1584 1.1 dholland while (tsiz > 0) {
1585 1.1 dholland *attrflagp = 0;
1586 1.1 dholland len = (tsiz > wsize) ? wsize : tsiz;
1587 1.1 dholland NFSCL_REQSTART(nd, NFSPROC_WRITE, vp);
1588 1.1 dholland if (nd->nd_flag & ND_NFSV4) {
1589 1.1 dholland nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID);
1590 1.1 dholland NFSM_BUILD(tl, u_int32_t *, NFSX_HYPER+2*NFSX_UNSIGNED);
1591 1.1 dholland txdr_hyper(uiop->uio_offset, tl);
1592 1.1 dholland tl += 2;
1593 1.1 dholland *tl++ = txdr_unsigned(*iomode);
1594 1.1 dholland *tl = txdr_unsigned(len);
1595 1.1 dholland } else if (nd->nd_flag & ND_NFSV3) {
1596 1.1 dholland NFSM_BUILD(tl, u_int32_t *, NFSX_HYPER+3*NFSX_UNSIGNED);
1597 1.1 dholland txdr_hyper(uiop->uio_offset, tl);
1598 1.1 dholland tl += 2;
1599 1.1 dholland *tl++ = txdr_unsigned(len);
1600 1.1 dholland *tl++ = txdr_unsigned(*iomode);
1601 1.1 dholland *tl = txdr_unsigned(len);
1602 1.1 dholland } else {
1603 1.1 dholland u_int32_t x;
1604 1.1 dholland
1605 1.1 dholland NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1606 1.1 dholland /*
1607 1.1 dholland * Not sure why someone changed this, since the
1608 1.1 dholland * RFC clearly states that "beginoffset" and
1609 1.1 dholland * "totalcount" are ignored, but it wouldn't
1610 1.1 dholland * surprise me if there's a busted server out there.
1611 1.1 dholland */
1612 1.1 dholland /* Set both "begin" and "current" to non-garbage. */
1613 1.1 dholland x = txdr_unsigned((u_int32_t)uiop->uio_offset);
1614 1.1 dholland *tl++ = x; /* "begin offset" */
1615 1.1 dholland *tl++ = x; /* "current offset" */
1616 1.1 dholland x = txdr_unsigned(len);
1617 1.1 dholland *tl++ = x; /* total to this offset */
1618 1.1 dholland *tl = x; /* size of this write */
1619 1.1 dholland
1620 1.1 dholland }
1621 1.1 dholland nfsm_uiombuf(nd, uiop, len);
1622 1.1 dholland /*
1623 1.1 dholland * Although it is tempting to do a normal Getattr Op in the
1624 1.1 dholland * NFSv4 compound, the result can be a nearly hung client
1625 1.1 dholland * system if the Getattr asks for Owner and/or OwnerGroup.
1626 1.1 dholland * It occurs when the client can't map either the Owner or
1627 1.1 dholland * Owner_group name in the Getattr reply to a uid/gid. When
1628 1.1 dholland * there is a cache miss, the kernel does an upcall to the
1629 1.1 dholland * nfsuserd. Then, it can try and read the local /etc/passwd
1630 1.1 dholland * or /etc/group file. It can then block in getnewbuf(),
1631 1.1 dholland * waiting for dirty writes to be pushed to the NFS server.
1632 1.1 dholland * The only reason this doesn't result in a complete
1633 1.1 dholland * deadlock, is that the upcall times out and allows
1634 1.1 dholland * the write to complete. However, progress is so slow
1635 1.1 dholland * that it might just as well be deadlocked.
1636 1.1 dholland * As such, we get the rest of the attributes, but not
1637 1.1 dholland * Owner or Owner_group.
1638 1.1 dholland * nb: nfscl_loadattrcache() needs to be told that these
1639 1.1 dholland * partial attributes from a write rpc are being
1640 1.1 dholland * passed in, via a argument flag.
1641 1.1 dholland */
1642 1.1 dholland if (nd->nd_flag & ND_NFSV4) {
1643 1.1 dholland NFSWRITEGETATTR_ATTRBIT(&attrbits);
1644 1.1 dholland NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1645 1.1 dholland *tl = txdr_unsigned(NFSV4OP_GETATTR);
1646 1.1 dholland (void) nfsrv_putattrbit(nd, &attrbits);
1647 1.1 dholland }
1648 1.1 dholland error = nfscl_request(nd, vp, p, cred, stuff);
1649 1.1 dholland if (error)
1650 1.1 dholland return (error);
1651 1.1 dholland if (nd->nd_repstat) {
1652 1.1 dholland /*
1653 1.1 dholland * In case the rpc gets retried, roll
1654 1.1 dholland * the uio fileds changed by nfsm_uiombuf()
1655 1.1 dholland * back.
1656 1.1 dholland */
1657 1.1 dholland uiop->uio_offset -= len;
1658 1.1 dholland uio_uio_resid_add(uiop, len);
1659 1.1 dholland uio_iov_base_add(uiop, -len);
1660 1.1 dholland uio_iov_len_add(uiop, len);
1661 1.1 dholland }
1662 1.1 dholland if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) {
1663 1.1 dholland error = nfscl_wcc_data(nd, vp, nap, attrflagp,
1664 1.1 dholland &wccflag, stuff);
1665 1.1 dholland if (error)
1666 1.1 dholland goto nfsmout;
1667 1.1 dholland }
1668 1.1 dholland if (!nd->nd_repstat) {
1669 1.1 dholland if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) {
1670 1.1 dholland NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED
1671 1.1 dholland + NFSX_VERF);
1672 1.1 dholland rlen = fxdr_unsigned(int, *tl++);
1673 1.1 dholland if (rlen == 0) {
1674 1.1 dholland error = NFSERR_IO;
1675 1.1 dholland goto nfsmout;
1676 1.1 dholland } else if (rlen < len) {
1677 1.1 dholland backup = len - rlen;
1678 1.1 dholland uio_iov_base_add(uiop, -(backup));
1679 1.1 dholland uio_iov_len_add(uiop, backup);
1680 1.1 dholland uiop->uio_offset -= backup;
1681 1.1 dholland uio_uio_resid_add(uiop, backup);
1682 1.1 dholland len = rlen;
1683 1.1 dholland }
1684 1.1 dholland commit = fxdr_unsigned(int, *tl++);
1685 1.1 dholland
1686 1.1 dholland /*
1687 1.1 dholland * Return the lowest committment level
1688 1.1 dholland * obtained by any of the RPCs.
1689 1.1 dholland */
1690 1.1 dholland if (committed == NFSWRITE_FILESYNC)
1691 1.1 dholland committed = commit;
1692 1.1 dholland else if (committed == NFSWRITE_DATASYNC &&
1693 1.1 dholland commit == NFSWRITE_UNSTABLE)
1694 1.1 dholland committed = commit;
1695 1.1 dholland NFSLOCKMNT(nmp);
1696 1.1 dholland if (!NFSHASWRITEVERF(nmp)) {
1697 1.1 dholland NFSBCOPY((caddr_t)tl,
1698 1.1 dholland (caddr_t)&nmp->nm_verf[0],
1699 1.1 dholland NFSX_VERF);
1700 1.1 dholland NFSSETWRITEVERF(nmp);
1701 1.1 dholland } else if (NFSBCMP(tl, nmp->nm_verf,
1702 1.1 dholland NFSX_VERF)) {
1703 1.1 dholland *must_commit = 1;
1704 1.1 dholland NFSBCOPY(tl, nmp->nm_verf, NFSX_VERF);
1705 1.1 dholland }
1706 1.1 dholland NFSUNLOCKMNT(nmp);
1707 1.1 dholland }
1708 1.1 dholland if (nd->nd_flag & ND_NFSV4)
1709 1.1 dholland NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1710 1.1 dholland if (nd->nd_flag & (ND_NFSV2 | ND_NFSV4)) {
1711 1.1 dholland error = nfsm_loadattr(nd, nap);
1712 1.1 dholland if (!error)
1713 1.1 dholland *attrflagp = NFS_LATTR_NOSHRINK;
1714 1.1 dholland }
1715 1.1 dholland } else {
1716 1.1 dholland error = nd->nd_repstat;
1717 1.1 dholland }
1718 1.1 dholland if (error)
1719 1.1 dholland goto nfsmout;
1720 1.1 dholland NFSWRITERPC_SETTIME(wccflag, np, (nd->nd_flag & ND_NFSV4));
1721 1.1 dholland mbuf_freem(nd->nd_mrep);
1722 1.1 dholland nd->nd_mrep = NULL;
1723 1.1 dholland tsiz -= len;
1724 1.1 dholland }
1725 1.1 dholland nfsmout:
1726 1.1 dholland if (nd->nd_mrep != NULL)
1727 1.1 dholland mbuf_freem(nd->nd_mrep);
1728 1.1 dholland *iomode = committed;
1729 1.1 dholland if (nd->nd_repstat && !error)
1730 1.1 dholland error = nd->nd_repstat;
1731 1.1 dholland return (error);
1732 1.1 dholland }
1733 1.1 dholland
1734 1.1 dholland /*
1735 1.1 dholland * nfs mknod rpc
1736 1.1 dholland * For NFS v2 this is a kludge. Use a create rpc but with the IFMT bits of the
1737 1.1 dholland * mode set to specify the file type and the size field for rdev.
1738 1.1 dholland */
1739 1.1 dholland APPLESTATIC int
1740 1.1 dholland nfsrpc_mknod(vnode_t dvp, char *name, int namelen, struct vattr *vap,
1741 1.1 dholland u_int32_t rdev, enum vtype vtyp, struct ucred *cred, NFSPROC_T *p,
1742 1.1 dholland struct nfsvattr *dnap, struct nfsvattr *nnap, struct nfsfh **nfhpp,
1743 1.1 dholland int *attrflagp, int *dattrflagp, void *dstuff)
1744 1.1 dholland {
1745 1.1 dholland u_int32_t *tl;
1746 1.1 dholland int error = 0;
1747 1.1 dholland struct nfsrv_descript nfsd, *nd = &nfsd;
1748 1.1 dholland nfsattrbit_t attrbits;
1749 1.1 dholland
1750 1.1 dholland *nfhpp = NULL;
1751 1.1 dholland *attrflagp = 0;
1752 1.1 dholland *dattrflagp = 0;
1753 1.1 dholland if (namelen > NFS_MAXNAMLEN)
1754 1.1 dholland return (ENAMETOOLONG);
1755 1.1 dholland NFSCL_REQSTART(nd, NFSPROC_MKNOD, dvp);
1756 1.1 dholland if (nd->nd_flag & ND_NFSV4) {
1757 1.1 dholland if (vtyp == VBLK || vtyp == VCHR) {
1758 1.1 dholland NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
1759 1.1 dholland *tl++ = vtonfsv34_type(vtyp);
1760 1.1 dholland *tl++ = txdr_unsigned(NFSMAJOR(rdev));
1761 1.1 dholland *tl = txdr_unsigned(NFSMINOR(rdev));
1762 1.1 dholland } else {
1763 1.1 dholland NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1764 1.1 dholland *tl = vtonfsv34_type(vtyp);
1765 1.1 dholland }
1766 1.1 dholland }
1767 1.1 dholland (void) nfsm_strtom(nd, name, namelen);
1768 1.1 dholland if (nd->nd_flag & ND_NFSV3) {
1769 1.1 dholland NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1770 1.1 dholland *tl = vtonfsv34_type(vtyp);
1771 1.1 dholland }
1772 1.1 dholland if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4))
1773 1.1 dholland nfscl_fillsattr(nd, vap, dvp, 0, 0);
1774 1.1 dholland if ((nd->nd_flag & ND_NFSV3) &&
1775 1.1 dholland (vtyp == VCHR || vtyp == VBLK)) {
1776 1.1 dholland NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1777 1.1 dholland *tl++ = txdr_unsigned(NFSMAJOR(rdev));
1778 1.1 dholland *tl = txdr_unsigned(NFSMINOR(rdev));
1779 1.1 dholland }
1780 1.1 dholland if (nd->nd_flag & ND_NFSV4) {
1781 1.1 dholland NFSGETATTR_ATTRBIT(&attrbits);
1782 1.1 dholland NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1783 1.1 dholland *tl++ = txdr_unsigned(NFSV4OP_GETFH);
1784 1.1 dholland *tl = txdr_unsigned(NFSV4OP_GETATTR);
1785 1.1 dholland (void) nfsrv_putattrbit(nd, &attrbits);
1786 1.1 dholland }
1787 1.1 dholland if (nd->nd_flag & ND_NFSV2)
1788 1.1 dholland nfscl_fillsattr(nd, vap, dvp, NFSSATTR_SIZERDEV, rdev);
1789 1.1 dholland error = nfscl_request(nd, dvp, p, cred, dstuff);
1790 1.1 dholland if (error)
1791 1.1 dholland return (error);
1792 1.1 dholland if (nd->nd_flag & ND_NFSV4)
1793 1.1 dholland error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
1794 1.1 dholland if (!nd->nd_repstat) {
1795 1.1 dholland if (nd->nd_flag & ND_NFSV4) {
1796 1.1 dholland NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
1797 1.1 dholland error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
1798 1.1 dholland if (error)
1799 1.1 dholland goto nfsmout;
1800 1.1 dholland }
1801 1.1 dholland error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp);
1802 1.1 dholland if (error)
1803 1.1 dholland goto nfsmout;
1804 1.1 dholland }
1805 1.1 dholland if (nd->nd_flag & ND_NFSV3)
1806 1.1 dholland error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
1807 1.1 dholland if (!error && nd->nd_repstat)
1808 1.1 dholland error = nd->nd_repstat;
1809 1.1 dholland nfsmout:
1810 1.1 dholland mbuf_freem(nd->nd_mrep);
1811 1.1 dholland return (error);
1812 1.1 dholland }
1813 1.1 dholland
1814 1.1 dholland /*
1815 1.1 dholland * nfs file create call
1816 1.1 dholland * Mostly just call the approriate routine. (I separated out v4, so that
1817 1.1 dholland * error recovery wouldn't be as difficult.)
1818 1.1 dholland */
1819 1.1 dholland APPLESTATIC int
1820 1.1 dholland nfsrpc_create(vnode_t dvp, char *name, int namelen, struct vattr *vap,
1821 1.1 dholland nfsquad_t cverf, int fmode, struct ucred *cred, NFSPROC_T *p,
1822 1.1 dholland struct nfsvattr *dnap, struct nfsvattr *nnap, struct nfsfh **nfhpp,
1823 1.1 dholland int *attrflagp, int *dattrflagp, void *dstuff)
1824 1.1 dholland {
1825 1.1 dholland int error = 0, newone, expireret = 0, retrycnt, unlocked;
1826 1.1 dholland struct nfsclowner *owp;
1827 1.1 dholland struct nfscldeleg *dp;
1828 1.1 dholland struct nfsmount *nmp = VFSTONFS(vnode_mount(dvp));
1829 1.1 dholland u_int32_t clidrev;
1830 1.1 dholland
1831 1.1 dholland if (NFSHASNFSV4(nmp)) {
1832 1.1 dholland retrycnt = 0;
1833 1.1 dholland do {
1834 1.1 dholland dp = NULL;
1835 1.1 dholland error = nfscl_open(dvp, NULL, 0, (NFSV4OPEN_ACCESSWRITE |
1836 1.1 dholland NFSV4OPEN_ACCESSREAD), 0, cred, p, &owp, NULL, &newone,
1837 1.1 dholland NULL, 1);
1838 1.1 dholland if (error)
1839 1.1 dholland return (error);
1840 1.1 dholland if (nmp->nm_clp != NULL)
1841 1.1 dholland clidrev = nmp->nm_clp->nfsc_clientidrev;
1842 1.1 dholland else
1843 1.1 dholland clidrev = 0;
1844 1.1 dholland error = nfsrpc_createv4(dvp, name, namelen, vap, cverf, fmode,
1845 1.1 dholland owp, &dp, cred, p, dnap, nnap, nfhpp, attrflagp, dattrflagp,
1846 1.1 dholland dstuff, &unlocked);
1847 1.1 dholland /*
1848 1.1 dholland * There is no need to invalidate cached attributes here,
1849 1.1 dholland * since new post-delegation issue attributes are always
1850 1.1 dholland * returned by nfsrpc_createv4() and these will update the
1851 1.1 dholland * attribute cache.
1852 1.1 dholland */
1853 1.1 dholland if (dp != NULL)
1854 1.1 dholland (void) nfscl_deleg(nmp->nm_mountp, owp->nfsow_clp,
1855 1.1 dholland (*nfhpp)->nfh_fh, (*nfhpp)->nfh_len, cred, p, &dp);
1856 1.1 dholland nfscl_ownerrelease(owp, error, newone, unlocked);
1857 1.1 dholland if (error == NFSERR_GRACE || error == NFSERR_STALECLIENTID ||
1858 1.1 dholland error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
1859 1.1 dholland error == NFSERR_BADSESSION) {
1860 1.1 dholland (void) nfs_catnap(PZERO, error, "nfs_open");
1861 1.1 dholland } else if ((error == NFSERR_EXPIRED ||
1862 1.1 dholland error == NFSERR_BADSTATEID) && clidrev != 0) {
1863 1.1 dholland expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
1864 1.1 dholland retrycnt++;
1865 1.1 dholland }
1866 1.1 dholland } while (error == NFSERR_GRACE || error == NFSERR_STALECLIENTID ||
1867 1.1 dholland error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
1868 1.1 dholland error == NFSERR_BADSESSION ||
1869 1.1 dholland ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
1870 1.1 dholland expireret == 0 && clidrev != 0 && retrycnt < 4));
1871 1.1 dholland if (error && retrycnt >= 4)
1872 1.1 dholland error = EIO;
1873 1.1 dholland } else {
1874 1.1 dholland error = nfsrpc_createv23(dvp, name, namelen, vap, cverf,
1875 1.1 dholland fmode, cred, p, dnap, nnap, nfhpp, attrflagp, dattrflagp,
1876 1.1 dholland dstuff);
1877 1.1 dholland }
1878 1.1 dholland return (error);
1879 1.1 dholland }
1880 1.1 dholland
1881 1.1 dholland /*
1882 1.1 dholland * The create rpc for v2 and 3.
1883 1.1 dholland */
1884 1.1 dholland static int
1885 1.1 dholland nfsrpc_createv23(vnode_t dvp, char *name, int namelen, struct vattr *vap,
1886 1.1 dholland nfsquad_t cverf, int fmode, struct ucred *cred, NFSPROC_T *p,
1887 1.1 dholland struct nfsvattr *dnap, struct nfsvattr *nnap, struct nfsfh **nfhpp,
1888 1.1 dholland int *attrflagp, int *dattrflagp, void *dstuff)
1889 1.1 dholland {
1890 1.1 dholland u_int32_t *tl;
1891 1.1 dholland int error = 0;
1892 1.1 dholland struct nfsrv_descript nfsd, *nd = &nfsd;
1893 1.1 dholland
1894 1.1 dholland *nfhpp = NULL;
1895 1.1 dholland *attrflagp = 0;
1896 1.1 dholland *dattrflagp = 0;
1897 1.1 dholland if (namelen > NFS_MAXNAMLEN)
1898 1.1 dholland return (ENAMETOOLONG);
1899 1.1 dholland NFSCL_REQSTART(nd, NFSPROC_CREATE, dvp);
1900 1.1 dholland (void) nfsm_strtom(nd, name, namelen);
1901 1.1 dholland if (nd->nd_flag & ND_NFSV3) {
1902 1.1 dholland NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1903 1.1 dholland if (fmode & O_EXCL) {
1904 1.1 dholland *tl = txdr_unsigned(NFSCREATE_EXCLUSIVE);
1905 1.1 dholland NFSM_BUILD(tl, u_int32_t *, NFSX_VERF);
1906 1.1 dholland *tl++ = cverf.lval[0];
1907 1.1 dholland *tl = cverf.lval[1];
1908 1.1 dholland } else {
1909 1.1 dholland *tl = txdr_unsigned(NFSCREATE_UNCHECKED);
1910 1.1 dholland nfscl_fillsattr(nd, vap, dvp, 0, 0);
1911 1.1 dholland }
1912 1.1 dholland } else {
1913 1.1 dholland nfscl_fillsattr(nd, vap, dvp, NFSSATTR_SIZE0, 0);
1914 1.1 dholland }
1915 1.1 dholland error = nfscl_request(nd, dvp, p, cred, dstuff);
1916 1.1 dholland if (error)
1917 1.1 dholland return (error);
1918 1.1 dholland if (nd->nd_repstat == 0) {
1919 1.1 dholland error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp);
1920 1.1 dholland if (error)
1921 1.1 dholland goto nfsmout;
1922 1.1 dholland }
1923 1.1 dholland if (nd->nd_flag & ND_NFSV3)
1924 1.1 dholland error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
1925 1.1 dholland if (nd->nd_repstat != 0 && error == 0)
1926 1.1 dholland error = nd->nd_repstat;
1927 1.1 dholland nfsmout:
1928 1.1 dholland mbuf_freem(nd->nd_mrep);
1929 1.1 dholland return (error);
1930 1.1 dholland }
1931 1.1 dholland
1932 1.1 dholland static int
1933 1.1 dholland nfsrpc_createv4(vnode_t dvp, char *name, int namelen, struct vattr *vap,
1934 1.1 dholland nfsquad_t cverf, int fmode, struct nfsclowner *owp, struct nfscldeleg **dpp,
1935 1.1 dholland struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap,
1936 1.1 dholland struct nfsvattr *nnap, struct nfsfh **nfhpp, int *attrflagp,
1937 1.1 dholland int *dattrflagp, void *dstuff, int *unlockedp)
1938 1.1 dholland {
1939 1.1 dholland u_int32_t *tl;
1940 1.1 dholland int error = 0, deleg, newone, ret, acesize, limitby;
1941 1.1 dholland struct nfsrv_descript nfsd, *nd = &nfsd;
1942 1.1 dholland struct nfsclopen *op;
1943 1.1 dholland struct nfscldeleg *dp = NULL;
1944 1.1 dholland struct nfsnode *np;
1945 1.1 dholland struct nfsfh *nfhp;
1946 1.1 dholland nfsattrbit_t attrbits;
1947 1.1 dholland nfsv4stateid_t stateid;
1948 1.1 dholland u_int32_t rflags;
1949 1.1 dholland struct nfsmount *nmp;
1950 1.1 dholland
1951 1.1 dholland nmp = VFSTONFS(dvp->v_mount);
1952 1.1 dholland *unlockedp = 0;
1953 1.1 dholland *nfhpp = NULL;
1954 1.1 dholland *dpp = NULL;
1955 1.1 dholland *attrflagp = 0;
1956 1.1 dholland *dattrflagp = 0;
1957 1.1 dholland if (namelen > NFS_MAXNAMLEN)
1958 1.1 dholland return (ENAMETOOLONG);
1959 1.1 dholland NFSCL_REQSTART(nd, NFSPROC_CREATE, dvp);
1960 1.1 dholland /*
1961 1.1 dholland * For V4, this is actually an Open op.
1962 1.1 dholland */
1963 1.1 dholland NFSM_BUILD(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
1964 1.1 dholland *tl++ = txdr_unsigned(owp->nfsow_seqid);
1965 1.1 dholland *tl++ = txdr_unsigned(NFSV4OPEN_ACCESSWRITE |
1966 1.1 dholland NFSV4OPEN_ACCESSREAD);
1967 1.1 dholland *tl++ = txdr_unsigned(NFSV4OPEN_DENYNONE);
1968 1.1 dholland *tl++ = NFSMNT_MDSSESSION(nmp)->nfsess_clientid.lval[0];
1969 1.1 dholland *tl = NFSMNT_MDSSESSION(nmp)->nfsess_clientid.lval[1];
1970 1.1 dholland (void) nfsm_strtom(nd, owp->nfsow_owner, NFSV4CL_LOCKNAMELEN);
1971 1.1 dholland NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1972 1.1 dholland *tl++ = txdr_unsigned(NFSV4OPEN_CREATE);
1973 1.1 dholland if (fmode & O_EXCL) {
1974 1.1 dholland if (NFSHASNFSV4N(nmp)) {
1975 1.1 dholland if (NFSHASSESSPERSIST(nmp)) {
1976 1.1 dholland /* Use GUARDED for persistent sessions. */
1977 1.1 dholland *tl = txdr_unsigned(NFSCREATE_GUARDED);
1978 1.1 dholland nfscl_fillsattr(nd, vap, dvp, 0, 0);
1979 1.1 dholland } else {
1980 1.1 dholland /* Otherwise, use EXCLUSIVE4_1. */
1981 1.1 dholland *tl = txdr_unsigned(NFSCREATE_EXCLUSIVE41);
1982 1.1 dholland NFSM_BUILD(tl, u_int32_t *, NFSX_VERF);
1983 1.1 dholland *tl++ = cverf.lval[0];
1984 1.1 dholland *tl = cverf.lval[1];
1985 1.1 dholland nfscl_fillsattr(nd, vap, dvp, 0, 0);
1986 1.1 dholland }
1987 1.1 dholland } else {
1988 1.1 dholland /* NFSv4.0 */
1989 1.1 dholland *tl = txdr_unsigned(NFSCREATE_EXCLUSIVE);
1990 1.1 dholland NFSM_BUILD(tl, u_int32_t *, NFSX_VERF);
1991 1.1 dholland *tl++ = cverf.lval[0];
1992 1.1 dholland *tl = cverf.lval[1];
1993 1.1 dholland }
1994 1.1 dholland } else {
1995 1.1 dholland *tl = txdr_unsigned(NFSCREATE_UNCHECKED);
1996 1.1 dholland nfscl_fillsattr(nd, vap, dvp, 0, 0);
1997 1.1 dholland }
1998 1.1 dholland NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1999 1.1 dholland *tl = txdr_unsigned(NFSV4OPEN_CLAIMNULL);
2000 1.1 dholland (void) nfsm_strtom(nd, name, namelen);
2001 1.1 dholland NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2002 1.1 dholland *tl++ = txdr_unsigned(NFSV4OP_GETFH);
2003 1.1 dholland *tl = txdr_unsigned(NFSV4OP_GETATTR);
2004 1.1 dholland NFSGETATTR_ATTRBIT(&attrbits);
2005 1.1 dholland (void) nfsrv_putattrbit(nd, &attrbits);
2006 1.1 dholland error = nfscl_request(nd, dvp, p, cred, dstuff);
2007 1.1 dholland if (error)
2008 1.1 dholland return (error);
2009 1.1 dholland error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
2010 1.1 dholland if (error)
2011 1.1 dholland goto nfsmout;
2012 1.1 dholland NFSCL_INCRSEQID(owp->nfsow_seqid, nd);
2013 1.1 dholland if (nd->nd_repstat == 0) {
2014 1.1 dholland NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
2015 1.1 dholland 6 * NFSX_UNSIGNED);
2016 1.1 dholland stateid.seqid = *tl++;
2017 1.1 dholland stateid.other[0] = *tl++;
2018 1.1 dholland stateid.other[1] = *tl++;
2019 1.1 dholland stateid.other[2] = *tl;
2020 1.1 dholland rflags = fxdr_unsigned(u_int32_t, *(tl + 6));
2021 1.1 dholland (void) nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
2022 1.1 dholland NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2023 1.1 dholland deleg = fxdr_unsigned(int, *tl);
2024 1.1 dholland if (deleg == NFSV4OPEN_DELEGATEREAD ||
2025 1.1 dholland deleg == NFSV4OPEN_DELEGATEWRITE) {
2026 1.1 dholland if (!(owp->nfsow_clp->nfsc_flags &
2027 1.1 dholland NFSCLFLAGS_FIRSTDELEG))
2028 1.1 dholland owp->nfsow_clp->nfsc_flags |=
2029 1.1 dholland (NFSCLFLAGS_FIRSTDELEG | NFSCLFLAGS_GOTDELEG);
2030 1.1 dholland MALLOC(dp, struct nfscldeleg *,
2031 1.1 dholland sizeof (struct nfscldeleg) + NFSX_V4FHMAX,
2032 1.1 dholland M_NFSCLDELEG, M_WAITOK);
2033 1.1 dholland LIST_INIT(&dp->nfsdl_owner);
2034 1.1 dholland LIST_INIT(&dp->nfsdl_lock);
2035 1.1 dholland dp->nfsdl_clp = owp->nfsow_clp;
2036 1.1 dholland newnfs_copyincred(cred, &dp->nfsdl_cred);
2037 1.1 dholland nfscl_lockinit(&dp->nfsdl_rwlock);
2038 1.1 dholland NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
2039 1.1 dholland NFSX_UNSIGNED);
2040 1.1 dholland dp->nfsdl_stateid.seqid = *tl++;
2041 1.1 dholland dp->nfsdl_stateid.other[0] = *tl++;
2042 1.1 dholland dp->nfsdl_stateid.other[1] = *tl++;
2043 1.1 dholland dp->nfsdl_stateid.other[2] = *tl++;
2044 1.1 dholland ret = fxdr_unsigned(int, *tl);
2045 1.1 dholland if (deleg == NFSV4OPEN_DELEGATEWRITE) {
2046 1.1 dholland dp->nfsdl_flags = NFSCLDL_WRITE;
2047 1.1 dholland /*
2048 1.1 dholland * Indicates how much the file can grow.
2049 1.1 dholland */
2050 1.1 dholland NFSM_DISSECT(tl, u_int32_t *,
2051 1.1 dholland 3 * NFSX_UNSIGNED);
2052 1.1 dholland limitby = fxdr_unsigned(int, *tl++);
2053 1.1 dholland switch (limitby) {
2054 1.1 dholland case NFSV4OPEN_LIMITSIZE:
2055 1.1 dholland dp->nfsdl_sizelimit = fxdr_hyper(tl);
2056 1.1 dholland break;
2057 1.1 dholland case NFSV4OPEN_LIMITBLOCKS:
2058 1.1 dholland dp->nfsdl_sizelimit =
2059 1.1 dholland fxdr_unsigned(u_int64_t, *tl++);
2060 1.1 dholland dp->nfsdl_sizelimit *=
2061 1.1 dholland fxdr_unsigned(u_int64_t, *tl);
2062 1.1 dholland break;
2063 1.1 dholland default:
2064 1.1 dholland error = NFSERR_BADXDR;
2065 1.1 dholland goto nfsmout;
2066 1.1 dholland };
2067 1.1 dholland } else {
2068 1.1 dholland dp->nfsdl_flags = NFSCLDL_READ;
2069 1.1 dholland }
2070 1.1 dholland if (ret)
2071 1.1 dholland dp->nfsdl_flags |= NFSCLDL_RECALL;
2072 1.1 dholland error = nfsrv_dissectace(nd, &dp->nfsdl_ace, &ret,
2073 1.1 dholland &acesize, p);
2074 1.1 dholland if (error)
2075 1.1 dholland goto nfsmout;
2076 1.1 dholland } else if (deleg != NFSV4OPEN_DELEGATENONE) {
2077 1.1 dholland error = NFSERR_BADXDR;
2078 1.1 dholland goto nfsmout;
2079 1.1 dholland }
2080 1.1 dholland error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp);
2081 1.1 dholland if (error)
2082 1.1 dholland goto nfsmout;
2083 1.1 dholland if (dp != NULL && *attrflagp) {
2084 1.1 dholland dp->nfsdl_change = nnap->na_filerev;
2085 1.1 dholland dp->nfsdl_modtime = nnap->na_mtime;
2086 1.1 dholland dp->nfsdl_flags |= NFSCLDL_MODTIMESET;
2087 1.1 dholland }
2088 1.1 dholland /*
2089 1.1 dholland * We can now complete the Open state.
2090 1.1 dholland */
2091 1.1 dholland nfhp = *nfhpp;
2092 1.1 dholland if (dp != NULL) {
2093 1.1 dholland dp->nfsdl_fhlen = nfhp->nfh_len;
2094 1.1 dholland NFSBCOPY(nfhp->nfh_fh, dp->nfsdl_fh, nfhp->nfh_len);
2095 1.1 dholland }
2096 1.1 dholland /*
2097 1.1 dholland * Get an Open structure that will be
2098 1.1 dholland * attached to the OpenOwner, acquired already.
2099 1.1 dholland */
2100 1.1 dholland error = nfscl_open(dvp, nfhp->nfh_fh, nfhp->nfh_len,
2101 1.1 dholland (NFSV4OPEN_ACCESSWRITE | NFSV4OPEN_ACCESSREAD), 0,
2102 1.1 dholland cred, p, NULL, &op, &newone, NULL, 0);
2103 1.1 dholland if (error)
2104 1.1 dholland goto nfsmout;
2105 1.1 dholland op->nfso_stateid = stateid;
2106 1.1 dholland newnfs_copyincred(cred, &op->nfso_cred);
2107 1.1 dholland if ((rflags & NFSV4OPEN_RESULTCONFIRM)) {
2108 1.1 dholland do {
2109 1.1 dholland ret = nfsrpc_openconfirm(dvp, nfhp->nfh_fh,
2110 1.1 dholland nfhp->nfh_len, op, cred, p);
2111 1.1 dholland if (ret == NFSERR_DELAY)
2112 1.1 dholland (void) nfs_catnap(PZERO, ret, "nfs_create");
2113 1.1 dholland } while (ret == NFSERR_DELAY);
2114 1.1 dholland error = ret;
2115 1.1 dholland }
2116 1.1 dholland
2117 1.1 dholland /*
2118 1.1 dholland * If the server is handing out delegations, but we didn't
2119 1.1 dholland * get one because an OpenConfirm was required, try the
2120 1.1 dholland * Open again, to get a delegation. This is a harmless no-op,
2121 1.1 dholland * from a server's point of view.
2122 1.1 dholland */
2123 1.1 dholland if ((rflags & NFSV4OPEN_RESULTCONFIRM) &&
2124 1.1 dholland (owp->nfsow_clp->nfsc_flags & NFSCLFLAGS_GOTDELEG) &&
2125 1.1 dholland !error && dp == NULL) {
2126 1.1 dholland np = VTONFS(dvp);
2127 1.1 dholland do {
2128 1.1 dholland ret = nfsrpc_openrpc(VFSTONFS(vnode_mount(dvp)), dvp,
2129 1.1 dholland np->n_fhp->nfh_fh, np->n_fhp->nfh_len,
2130 1.1 dholland nfhp->nfh_fh, nfhp->nfh_len,
2131 1.1 dholland (NFSV4OPEN_ACCESSWRITE | NFSV4OPEN_ACCESSREAD), op,
2132 1.1 dholland name, namelen, &dp, 0, 0x0, cred, p, 0, 1);
2133 1.1 dholland if (ret == NFSERR_DELAY)
2134 1.1 dholland (void) nfs_catnap(PZERO, ret, "nfs_crt2");
2135 1.1 dholland } while (ret == NFSERR_DELAY);
2136 1.1 dholland if (ret) {
2137 1.1 dholland if (dp != NULL)
2138 1.1 dholland FREE((caddr_t)dp, M_NFSCLDELEG);
2139 1.1 dholland if (ret == NFSERR_STALECLIENTID ||
2140 1.1 dholland ret == NFSERR_STALEDONTRECOVER ||
2141 1.1 dholland ret == NFSERR_BADSESSION)
2142 1.1 dholland error = ret;
2143 1.1 dholland }
2144 1.1 dholland }
2145 1.1 dholland nfscl_openrelease(op, error, newone);
2146 1.1 dholland *unlockedp = 1;
2147 1.1 dholland }
2148 1.1 dholland if (nd->nd_repstat != 0 && error == 0)
2149 1.1 dholland error = nd->nd_repstat;
2150 1.1 dholland if (error == NFSERR_STALECLIENTID || error == NFSERR_BADSESSION)
2151 1.1 dholland nfscl_initiate_recovery(owp->nfsow_clp);
2152 1.1 dholland nfsmout:
2153 1.1 dholland if (!error)
2154 1.1 dholland *dpp = dp;
2155 1.1 dholland else if (dp != NULL)
2156 1.1 dholland FREE((caddr_t)dp, M_NFSCLDELEG);
2157 1.1 dholland mbuf_freem(nd->nd_mrep);
2158 1.1 dholland return (error);
2159 1.1 dholland }
2160 1.1 dholland
2161 1.1 dholland /*
2162 1.1 dholland * Nfs remove rpc
2163 1.1 dholland */
2164 1.1 dholland APPLESTATIC int
2165 1.1 dholland nfsrpc_remove(vnode_t dvp, char *name, int namelen, vnode_t vp,
2166 1.1 dholland struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap, int *dattrflagp,
2167 1.1 dholland void *dstuff)
2168 1.1 dholland {
2169 1.1 dholland u_int32_t *tl;
2170 1.1 dholland struct nfsrv_descript nfsd, *nd = &nfsd;
2171 1.1 dholland struct nfsnode *np;
2172 1.1 dholland struct nfsmount *nmp;
2173 1.1 dholland nfsv4stateid_t dstateid;
2174 1.1 dholland int error, ret = 0, i;
2175 1.1 dholland
2176 1.1 dholland *dattrflagp = 0;
2177 1.1 dholland if (namelen > NFS_MAXNAMLEN)
2178 1.1 dholland return (ENAMETOOLONG);
2179 1.1 dholland nmp = VFSTONFS(vnode_mount(dvp));
2180 1.1 dholland tryagain:
2181 1.1 dholland if (NFSHASNFSV4(nmp) && ret == 0) {
2182 1.1 dholland ret = nfscl_removedeleg(vp, p, &dstateid);
2183 1.1 dholland if (ret == 1) {
2184 1.1 dholland NFSCL_REQSTART(nd, NFSPROC_RETDELEGREMOVE, vp);
2185 1.1 dholland NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID +
2186 1.1 dholland NFSX_UNSIGNED);
2187 1.1 dholland if (NFSHASNFSV4N(nmp))
2188 1.1 dholland *tl++ = 0;
2189 1.1 dholland else
2190 1.1 dholland *tl++ = dstateid.seqid;
2191 1.1 dholland *tl++ = dstateid.other[0];
2192 1.1 dholland *tl++ = dstateid.other[1];
2193 1.1 dholland *tl++ = dstateid.other[2];
2194 1.1 dholland *tl = txdr_unsigned(NFSV4OP_PUTFH);
2195 1.1 dholland np = VTONFS(dvp);
2196 1.1 dholland (void) nfsm_fhtom(nd, np->n_fhp->nfh_fh,
2197 1.1 dholland np->n_fhp->nfh_len, 0);
2198 1.1 dholland NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2199 1.1 dholland *tl = txdr_unsigned(NFSV4OP_REMOVE);
2200 1.1 dholland }
2201 1.1 dholland } else {
2202 1.1 dholland ret = 0;
2203 1.1 dholland }
2204 1.1 dholland if (ret == 0)
2205 1.1 dholland NFSCL_REQSTART(nd, NFSPROC_REMOVE, dvp);
2206 1.1 dholland (void) nfsm_strtom(nd, name, namelen);
2207 1.1 dholland error = nfscl_request(nd, dvp, p, cred, dstuff);
2208 1.1 dholland if (error)
2209 1.1 dholland return (error);
2210 1.1 dholland if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) {
2211 1.1 dholland /* For NFSv4, parse out any Delereturn replies. */
2212 1.1 dholland if (ret > 0 && nd->nd_repstat != 0 &&
2213 1.1 dholland (nd->nd_flag & ND_NOMOREDATA)) {
2214 1.1 dholland /*
2215 1.1 dholland * If the Delegreturn failed, try again without
2216 1.1 dholland * it. The server will Recall, as required.
2217 1.1 dholland */
2218 1.1 dholland mbuf_freem(nd->nd_mrep);
2219 1.1 dholland goto tryagain;
2220 1.1 dholland }
2221 1.1 dholland for (i = 0; i < (ret * 2); i++) {
2222 1.1 dholland if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) ==
2223 1.1 dholland ND_NFSV4) {
2224 1.1 dholland NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2225 1.1 dholland if (*(tl + 1))
2226 1.1 dholland nd->nd_flag |= ND_NOMOREDATA;
2227 1.1 dholland }
2228 1.1 dholland }
2229 1.1 dholland error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
2230 1.1 dholland }
2231 1.1 dholland if (nd->nd_repstat && !error)
2232 1.1 dholland error = nd->nd_repstat;
2233 1.1 dholland nfsmout:
2234 1.1 dholland mbuf_freem(nd->nd_mrep);
2235 1.1 dholland return (error);
2236 1.1 dholland }
2237 1.1 dholland
2238 1.1 dholland /*
2239 1.1 dholland * Do an nfs rename rpc.
2240 1.1 dholland */
2241 1.1 dholland APPLESTATIC int
2242 1.1 dholland nfsrpc_rename(vnode_t fdvp, vnode_t fvp, char *fnameptr, int fnamelen,
2243 1.1 dholland vnode_t tdvp, vnode_t tvp, char *tnameptr, int tnamelen, struct ucred *cred,
2244 1.1 dholland NFSPROC_T *p, struct nfsvattr *fnap, struct nfsvattr *tnap,
2245 1.1 dholland int *fattrflagp, int *tattrflagp, void *fstuff, void *tstuff)
2246 1.1 dholland {
2247 1.1 dholland u_int32_t *tl;
2248 1.1 dholland struct nfsrv_descript nfsd, *nd = &nfsd;
2249 1.1 dholland struct nfsmount *nmp;
2250 1.1 dholland struct nfsnode *np;
2251 1.1 dholland nfsattrbit_t attrbits;
2252 1.1 dholland nfsv4stateid_t fdstateid, tdstateid;
2253 1.1 dholland int error = 0, ret = 0, gottd = 0, gotfd = 0, i;
2254 1.1 dholland
2255 1.1 dholland *fattrflagp = 0;
2256 1.1 dholland *tattrflagp = 0;
2257 1.1 dholland nmp = VFSTONFS(vnode_mount(fdvp));
2258 1.1 dholland if (fnamelen > NFS_MAXNAMLEN || tnamelen > NFS_MAXNAMLEN)
2259 1.1 dholland return (ENAMETOOLONG);
2260 1.1 dholland tryagain:
2261 1.1 dholland if (NFSHASNFSV4(nmp) && ret == 0) {
2262 1.1 dholland ret = nfscl_renamedeleg(fvp, &fdstateid, &gotfd, tvp,
2263 1.1 dholland &tdstateid, &gottd, p);
2264 1.1 dholland if (gotfd && gottd) {
2265 1.1 dholland NFSCL_REQSTART(nd, NFSPROC_RETDELEGRENAME2, fvp);
2266 1.1 dholland } else if (gotfd) {
2267 1.1 dholland NFSCL_REQSTART(nd, NFSPROC_RETDELEGRENAME1, fvp);
2268 1.1 dholland } else if (gottd) {
2269 1.1 dholland NFSCL_REQSTART(nd, NFSPROC_RETDELEGRENAME1, tvp);
2270 1.1 dholland }
2271 1.1 dholland if (gotfd) {
2272 1.1 dholland NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID);
2273 1.1 dholland if (NFSHASNFSV4N(nmp))
2274 1.1 dholland *tl++ = 0;
2275 1.1 dholland else
2276 1.1 dholland *tl++ = fdstateid.seqid;
2277 1.1 dholland *tl++ = fdstateid.other[0];
2278 1.1 dholland *tl++ = fdstateid.other[1];
2279 1.1 dholland *tl = fdstateid.other[2];
2280 1.1 dholland if (gottd) {
2281 1.1 dholland NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2282 1.1 dholland *tl = txdr_unsigned(NFSV4OP_PUTFH);
2283 1.1 dholland np = VTONFS(tvp);
2284 1.1 dholland (void) nfsm_fhtom(nd, np->n_fhp->nfh_fh,
2285 1.1 dholland np->n_fhp->nfh_len, 0);
2286 1.1 dholland NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2287 1.1 dholland *tl = txdr_unsigned(NFSV4OP_DELEGRETURN);
2288 1.1 dholland }
2289 1.1 dholland }
2290 1.1 dholland if (gottd) {
2291 1.1 dholland NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID);
2292 1.1 dholland if (NFSHASNFSV4N(nmp))
2293 1.1 dholland *tl++ = 0;
2294 1.1 dholland else
2295 1.1 dholland *tl++ = tdstateid.seqid;
2296 1.1 dholland *tl++ = tdstateid.other[0];
2297 1.1 dholland *tl++ = tdstateid.other[1];
2298 1.1 dholland *tl = tdstateid.other[2];
2299 1.1 dholland }
2300 1.1 dholland if (ret > 0) {
2301 1.1 dholland NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2302 1.1 dholland *tl = txdr_unsigned(NFSV4OP_PUTFH);
2303 1.1 dholland np = VTONFS(fdvp);
2304 1.1 dholland (void) nfsm_fhtom(nd, np->n_fhp->nfh_fh,
2305 1.1 dholland np->n_fhp->nfh_len, 0);
2306 1.1 dholland NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2307 1.1 dholland *tl = txdr_unsigned(NFSV4OP_SAVEFH);
2308 1.1 dholland }
2309 1.1 dholland } else {
2310 1.1 dholland ret = 0;
2311 1.1 dholland }
2312 1.1 dholland if (ret == 0)
2313 1.1 dholland NFSCL_REQSTART(nd, NFSPROC_RENAME, fdvp);
2314 1.1 dholland if (nd->nd_flag & ND_NFSV4) {
2315 1.1 dholland NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2316 1.1 dholland *tl = txdr_unsigned(NFSV4OP_GETATTR);
2317 1.1 dholland NFSWCCATTR_ATTRBIT(&attrbits);
2318 1.1 dholland (void) nfsrv_putattrbit(nd, &attrbits);
2319 1.1 dholland NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2320 1.1 dholland *tl = txdr_unsigned(NFSV4OP_PUTFH);
2321 1.1 dholland (void) nfsm_fhtom(nd, VTONFS(tdvp)->n_fhp->nfh_fh,
2322 1.1 dholland VTONFS(tdvp)->n_fhp->nfh_len, 0);
2323 1.1 dholland NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2324 1.1 dholland *tl = txdr_unsigned(NFSV4OP_GETATTR);
2325 1.1 dholland (void) nfsrv_putattrbit(nd, &attrbits);
2326 1.1 dholland nd->nd_flag |= ND_V4WCCATTR;
2327 1.1 dholland NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2328 1.1 dholland *tl = txdr_unsigned(NFSV4OP_RENAME);
2329 1.1 dholland }
2330 1.1 dholland (void) nfsm_strtom(nd, fnameptr, fnamelen);
2331 1.1 dholland if (!(nd->nd_flag & ND_NFSV4))
2332 1.1 dholland (void) nfsm_fhtom(nd, VTONFS(tdvp)->n_fhp->nfh_fh,
2333 1.1 dholland VTONFS(tdvp)->n_fhp->nfh_len, 0);
2334 1.1 dholland (void) nfsm_strtom(nd, tnameptr, tnamelen);
2335 1.1 dholland error = nfscl_request(nd, fdvp, p, cred, fstuff);
2336 1.1 dholland if (error)
2337 1.1 dholland return (error);
2338 1.1 dholland if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) {
2339 1.1 dholland /* For NFSv4, parse out any Delereturn replies. */
2340 1.1 dholland if (ret > 0 && nd->nd_repstat != 0 &&
2341 1.1 dholland (nd->nd_flag & ND_NOMOREDATA)) {
2342 1.1 dholland /*
2343 1.1 dholland * If the Delegreturn failed, try again without
2344 1.1 dholland * it. The server will Recall, as required.
2345 1.1 dholland */
2346 1.1 dholland mbuf_freem(nd->nd_mrep);
2347 1.1 dholland goto tryagain;
2348 1.1 dholland }
2349 1.1 dholland for (i = 0; i < (ret * 2); i++) {
2350 1.1 dholland if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) ==
2351 1.1 dholland ND_NFSV4) {
2352 1.1 dholland NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2353 1.1 dholland if (*(tl + 1)) {
2354 1.1 dholland if (i == 0 && ret > 1) {
2355 1.1 dholland /*
2356 1.1 dholland * If the Delegreturn failed, try again
2357 1.1 dholland * without it. The server will Recall, as
2358 1.1 dholland * required.
2359 1.1 dholland * If ret > 1, the first iteration of this
2360 1.1 dholland * loop is the second DelegReturn result.
2361 1.1 dholland */
2362 1.1 dholland mbuf_freem(nd->nd_mrep);
2363 1.1 dholland goto tryagain;
2364 1.1 dholland } else {
2365 1.1 dholland nd->nd_flag |= ND_NOMOREDATA;
2366 1.1 dholland }
2367 1.1 dholland }
2368 1.1 dholland }
2369 1.1 dholland }
2370 1.1 dholland /* Now, the first wcc attribute reply. */
2371 1.1 dholland if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) == ND_NFSV4) {
2372 1.1 dholland NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2373 1.1 dholland if (*(tl + 1))
2374 1.1 dholland nd->nd_flag |= ND_NOMOREDATA;
2375 1.1 dholland }
2376 1.1 dholland error = nfscl_wcc_data(nd, fdvp, fnap, fattrflagp, NULL,
2377 1.1 dholland fstuff);
2378 1.1 dholland /* and the second wcc attribute reply. */
2379 1.1 dholland if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) == ND_NFSV4 &&
2380 1.1 dholland !error) {
2381 1.1 dholland NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2382 1.1 dholland if (*(tl + 1))
2383 1.1 dholland nd->nd_flag |= ND_NOMOREDATA;
2384 1.1 dholland }
2385 1.1 dholland if (!error)
2386 1.1 dholland error = nfscl_wcc_data(nd, tdvp, tnap, tattrflagp,
2387 1.1 dholland NULL, tstuff);
2388 1.1 dholland }
2389 1.1 dholland if (nd->nd_repstat && !error)
2390 1.1 dholland error = nd->nd_repstat;
2391 1.1 dholland nfsmout:
2392 1.1 dholland mbuf_freem(nd->nd_mrep);
2393 1.1 dholland return (error);
2394 1.1 dholland }
2395 1.1 dholland
2396 1.1 dholland /*
2397 1.1 dholland * nfs hard link create rpc
2398 1.1 dholland */
2399 1.1 dholland APPLESTATIC int
2400 1.1 dholland nfsrpc_link(vnode_t dvp, vnode_t vp, char *name, int namelen,
2401 1.1 dholland struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap,
2402 1.1 dholland struct nfsvattr *nap, int *attrflagp, int *dattrflagp, void *dstuff)
2403 1.1 dholland {
2404 1.1 dholland u_int32_t *tl;
2405 1.1 dholland struct nfsrv_descript nfsd, *nd = &nfsd;
2406 1.1 dholland nfsattrbit_t attrbits;
2407 1.1 dholland int error = 0;
2408 1.1 dholland
2409 1.1 dholland *attrflagp = 0;
2410 1.1 dholland *dattrflagp = 0;
2411 1.1 dholland if (namelen > NFS_MAXNAMLEN)
2412 1.1 dholland return (ENAMETOOLONG);
2413 1.1 dholland NFSCL_REQSTART(nd, NFSPROC_LINK, vp);
2414 1.1 dholland if (nd->nd_flag & ND_NFSV4) {
2415 1.1 dholland NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2416 1.1 dholland *tl = txdr_unsigned(NFSV4OP_PUTFH);
2417 1.1 dholland }
2418 1.1 dholland (void) nfsm_fhtom(nd, VTONFS(dvp)->n_fhp->nfh_fh,
2419 1.1 dholland VTONFS(dvp)->n_fhp->nfh_len, 0);
2420 1.1 dholland if (nd->nd_flag & ND_NFSV4) {
2421 1.1 dholland NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2422 1.1 dholland *tl = txdr_unsigned(NFSV4OP_GETATTR);
2423 1.1 dholland NFSWCCATTR_ATTRBIT(&attrbits);
2424 1.1 dholland (void) nfsrv_putattrbit(nd, &attrbits);
2425 1.1 dholland nd->nd_flag |= ND_V4WCCATTR;
2426 1.1 dholland NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2427 1.1 dholland *tl = txdr_unsigned(NFSV4OP_LINK);
2428 1.1 dholland }
2429 1.1 dholland (void) nfsm_strtom(nd, name, namelen);
2430 1.1 dholland error = nfscl_request(nd, vp, p, cred, dstuff);
2431 1.1 dholland if (error)
2432 1.1 dholland return (error);
2433 1.1 dholland if (nd->nd_flag & ND_NFSV3) {
2434 1.1 dholland error = nfscl_postop_attr(nd, nap, attrflagp, dstuff);
2435 1.1 dholland if (!error)
2436 1.1 dholland error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp,
2437 1.1 dholland NULL, dstuff);
2438 1.1 dholland } else if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) == ND_NFSV4) {
2439 1.1 dholland /*
2440 1.1 dholland * First, parse out the PutFH and Getattr result.
2441 1.1 dholland */
2442 1.1 dholland NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2443 1.1 dholland if (!(*(tl + 1)))
2444 1.1 dholland NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2445 1.1 dholland if (*(tl + 1))
2446 1.1 dholland nd->nd_flag |= ND_NOMOREDATA;
2447 1.1 dholland /*
2448 1.1 dholland * Get the pre-op attributes.
2449 1.1 dholland */
2450 1.1 dholland error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
2451 1.1 dholland }
2452 1.1 dholland if (nd->nd_repstat && !error)
2453 1.1 dholland error = nd->nd_repstat;
2454 1.1 dholland nfsmout:
2455 1.1 dholland mbuf_freem(nd->nd_mrep);
2456 1.1 dholland return (error);
2457 1.1 dholland }
2458 1.1 dholland
2459 1.1 dholland /*
2460 1.1 dholland * nfs symbolic link create rpc
2461 1.1 dholland */
2462 1.1 dholland APPLESTATIC int
2463 1.1 dholland nfsrpc_symlink(vnode_t dvp, char *name, int namelen, char *target,
2464 1.1 dholland struct vattr *vap, struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap,
2465 1.1 dholland struct nfsvattr *nnap, struct nfsfh **nfhpp, int *attrflagp,
2466 1.1 dholland int *dattrflagp, void *dstuff)
2467 1.1 dholland {
2468 1.1 dholland u_int32_t *tl;
2469 1.1 dholland struct nfsrv_descript nfsd, *nd = &nfsd;
2470 1.1 dholland struct nfsmount *nmp;
2471 1.1 dholland int slen, error = 0;
2472 1.1 dholland
2473 1.1 dholland *nfhpp = NULL;
2474 1.1 dholland *attrflagp = 0;
2475 1.1 dholland *dattrflagp = 0;
2476 1.1 dholland nmp = VFSTONFS(vnode_mount(dvp));
2477 1.1 dholland slen = strlen(target);
2478 1.1 dholland if (slen > NFS_MAXPATHLEN || namelen > NFS_MAXNAMLEN)
2479 1.1 dholland return (ENAMETOOLONG);
2480 1.1 dholland NFSCL_REQSTART(nd, NFSPROC_SYMLINK, dvp);
2481 1.1 dholland if (nd->nd_flag & ND_NFSV4) {
2482 1.1 dholland NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2483 1.1 dholland *tl = txdr_unsigned(NFLNK);
2484 1.1 dholland (void) nfsm_strtom(nd, target, slen);
2485 1.1 dholland }
2486 1.1 dholland (void) nfsm_strtom(nd, name, namelen);
2487 1.1 dholland if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4))
2488 1.1 dholland nfscl_fillsattr(nd, vap, dvp, 0, 0);
2489 1.1 dholland if (!(nd->nd_flag & ND_NFSV4))
2490 1.1 dholland (void) nfsm_strtom(nd, target, slen);
2491 1.1 dholland if (nd->nd_flag & ND_NFSV2)
2492 1.1 dholland nfscl_fillsattr(nd, vap, dvp, NFSSATTR_SIZENEG1, 0);
2493 1.1 dholland error = nfscl_request(nd, dvp, p, cred, dstuff);
2494 1.1 dholland if (error)
2495 1.1 dholland return (error);
2496 1.1 dholland if (nd->nd_flag & ND_NFSV4)
2497 1.1 dholland error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
2498 1.1 dholland if ((nd->nd_flag & ND_NFSV3) && !error) {
2499 1.1 dholland if (!nd->nd_repstat)
2500 1.1 dholland error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp);
2501 1.1 dholland if (!error)
2502 1.1 dholland error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp,
2503 1.1 dholland NULL, dstuff);
2504 1.1 dholland }
2505 1.1 dholland if (nd->nd_repstat && !error)
2506 1.1 dholland error = nd->nd_repstat;
2507 1.1 dholland mbuf_freem(nd->nd_mrep);
2508 1.1 dholland /*
2509 1.1 dholland * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry.
2510 1.1 dholland */
2511 1.1 dholland if (error == EEXIST)
2512 1.1 dholland error = 0;
2513 1.1 dholland return (error);
2514 1.1 dholland }
2515 1.1 dholland
2516 1.1 dholland /*
2517 1.1 dholland * nfs make dir rpc
2518 1.1 dholland */
2519 1.1 dholland APPLESTATIC int
2520 1.1 dholland nfsrpc_mkdir(vnode_t dvp, char *name, int namelen, struct vattr *vap,
2521 1.1 dholland struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap,
2522 1.1 dholland struct nfsvattr *nnap, struct nfsfh **nfhpp, int *attrflagp,
2523 1.1 dholland int *dattrflagp, void *dstuff)
2524 1.1 dholland {
2525 1.1 dholland u_int32_t *tl;
2526 1.1 dholland struct nfsrv_descript nfsd, *nd = &nfsd;
2527 1.1 dholland nfsattrbit_t attrbits;
2528 1.1 dholland int error = 0;
2529 1.1 dholland
2530 1.1 dholland *nfhpp = NULL;
2531 1.1 dholland *attrflagp = 0;
2532 1.1 dholland *dattrflagp = 0;
2533 1.1 dholland if (namelen > NFS_MAXNAMLEN)
2534 1.1 dholland return (ENAMETOOLONG);
2535 1.1 dholland NFSCL_REQSTART(nd, NFSPROC_MKDIR, dvp);
2536 1.1 dholland if (nd->nd_flag & ND_NFSV4) {
2537 1.1 dholland NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2538 1.1 dholland *tl = txdr_unsigned(NFDIR);
2539 1.1 dholland }
2540 1.1 dholland (void) nfsm_strtom(nd, name, namelen);
2541 1.1 dholland nfscl_fillsattr(nd, vap, dvp, NFSSATTR_SIZENEG1, 0);
2542 1.1 dholland if (nd->nd_flag & ND_NFSV4) {
2543 1.1 dholland NFSGETATTR_ATTRBIT(&attrbits);
2544 1.1 dholland NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2545 1.1 dholland *tl++ = txdr_unsigned(NFSV4OP_GETFH);
2546 1.1 dholland *tl = txdr_unsigned(NFSV4OP_GETATTR);
2547 1.1 dholland (void) nfsrv_putattrbit(nd, &attrbits);
2548 1.1 dholland }
2549 1.1 dholland error = nfscl_request(nd, dvp, p, cred, dstuff);
2550 1.1 dholland if (error)
2551 1.1 dholland return (error);
2552 1.1 dholland if (nd->nd_flag & ND_NFSV4)
2553 1.1 dholland error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
2554 1.1 dholland if (!nd->nd_repstat && !error) {
2555 1.1 dholland if (nd->nd_flag & ND_NFSV4) {
2556 1.1 dholland NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
2557 1.1 dholland error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
2558 1.1 dholland }
2559 1.1 dholland if (!error)
2560 1.1 dholland error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp);
2561 1.1 dholland }
2562 1.1 dholland if ((nd->nd_flag & ND_NFSV3) && !error)
2563 1.1 dholland error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
2564 1.1 dholland if (nd->nd_repstat && !error)
2565 1.1 dholland error = nd->nd_repstat;
2566 1.1 dholland nfsmout:
2567 1.1 dholland mbuf_freem(nd->nd_mrep);
2568 1.1 dholland /*
2569 1.1 dholland * Kludge: Map EEXIST => 0 assuming that you have a reply to a retry.
2570 1.1 dholland */
2571 1.1 dholland if (error == EEXIST)
2572 1.1 dholland error = 0;
2573 1.1 dholland return (error);
2574 1.1 dholland }
2575 1.1 dholland
2576 1.1 dholland /*
2577 1.1 dholland * nfs remove directory call
2578 1.1 dholland */
2579 1.1 dholland APPLESTATIC int
2580 1.1 dholland nfsrpc_rmdir(vnode_t dvp, char *name, int namelen, struct ucred *cred,
2581 1.1 dholland NFSPROC_T *p, struct nfsvattr *dnap, int *dattrflagp, void *dstuff)
2582 1.1 dholland {
2583 1.1 dholland struct nfsrv_descript nfsd, *nd = &nfsd;
2584 1.1 dholland int error = 0;
2585 1.1 dholland
2586 1.1 dholland *dattrflagp = 0;
2587 1.1 dholland if (namelen > NFS_MAXNAMLEN)
2588 1.1 dholland return (ENAMETOOLONG);
2589 1.1 dholland NFSCL_REQSTART(nd, NFSPROC_RMDIR, dvp);
2590 1.1 dholland (void) nfsm_strtom(nd, name, namelen);
2591 1.1 dholland error = nfscl_request(nd, dvp, p, cred, dstuff);
2592 1.1 dholland if (error)
2593 1.1 dholland return (error);
2594 1.1 dholland if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4))
2595 1.1 dholland error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff);
2596 1.1 dholland if (nd->nd_repstat && !error)
2597 1.1 dholland error = nd->nd_repstat;
2598 1.1 dholland mbuf_freem(nd->nd_mrep);
2599 1.1 dholland /*
2600 1.1 dholland * Kludge: Map ENOENT => 0 assuming that you have a reply to a retry.
2601 1.1 dholland */
2602 1.1 dholland if (error == ENOENT)
2603 1.1 dholland error = 0;
2604 1.1 dholland return (error);
2605 1.1 dholland }
2606 1.1 dholland
2607 1.1 dholland /*
2608 1.1 dholland * Readdir rpc.
2609 1.1 dholland * Always returns with either uio_resid unchanged, if you are at the
2610 1.1 dholland * end of the directory, or uio_resid == 0, with all DIRBLKSIZ chunks
2611 1.1 dholland * filled in.
2612 1.1 dholland * I felt this would allow caching of directory blocks more easily
2613 1.1 dholland * than returning a pertially filled block.
2614 1.1 dholland * Directory offset cookies:
2615 1.1 dholland * Oh my, what to do with them...
2616 1.1 dholland * I can think of three ways to deal with them:
2617 1.1 dholland * 1 - have the layer above these RPCs maintain a map between logical
2618 1.1 dholland * directory byte offsets and the NFS directory offset cookies
2619 1.1 dholland * 2 - pass the opaque directory offset cookies up into userland
2620 1.1 dholland * and let the libc functions deal with them, via the system call
2621 1.1 dholland * 3 - return them to userland in the "struct dirent", so future versions
2622 1.1 dholland * of libc can use them and do whatever is necessary to amke things work
2623 1.1 dholland * above these rpc calls, in the meantime
2624 1.1 dholland * For now, I do #3 by "hiding" the directory offset cookies after the
2625 1.1 dholland * d_name field in struct dirent. This is space inside d_reclen that
2626 1.1 dholland * will be ignored by anything that doesn't know about them.
2627 1.1 dholland * The directory offset cookies are filled in as the last 8 bytes of
2628 1.1 dholland * each directory entry, after d_name. Someday, the userland libc
2629 1.1 dholland * functions may be able to use these. In the meantime, it satisfies
2630 1.1 dholland * OpenBSD's requirements for cookies being returned.
2631 1.1 dholland * If expects the directory offset cookie for the read to be in uio_offset
2632 1.1 dholland * and returns the one for the next entry after this directory block in
2633 1.1 dholland * there, as well.
2634 1.1 dholland */
2635 1.1 dholland APPLESTATIC int
2636 1.1 dholland nfsrpc_readdir(vnode_t vp, struct uio *uiop, nfsuint64 *cookiep,
2637 1.1 dholland struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp,
2638 1.1 dholland int *eofp, void *stuff)
2639 1.1 dholland {
2640 1.1 dholland int len, left;
2641 1.1 dholland struct dirent *dp = NULL;
2642 1.1 dholland u_int32_t *tl;
2643 1.1 dholland nfsquad_t cookie, ncookie;
2644 1.1 dholland struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
2645 1.1 dholland struct nfsnode *dnp = VTONFS(vp);
2646 1.1 dholland struct nfsvattr nfsva;
2647 1.1 dholland struct nfsrv_descript nfsd, *nd = &nfsd;
2648 1.1 dholland int error = 0, tlen, more_dirs = 1, blksiz = 0, bigenough = 1;
2649 1.1 dholland int reqsize, tryformoredirs = 1, readsize, eof = 0, gotmnton = 0;
2650 1.1 dholland long dotfileid, dotdotfileid = 0;
2651 1.1 dholland u_int32_t fakefileno = 0xffffffff, rderr;
2652 1.1 dholland char *cp;
2653 1.1 dholland nfsattrbit_t attrbits, dattrbits;
2654 1.1 dholland u_int32_t *tl2 = NULL;
2655 1.1 dholland size_t tresid;
2656 1.1 dholland
2657 1.1 dholland KASSERT(uiop->uio_iovcnt == 1 &&
2658 1.1 dholland (uio_uio_resid(uiop) & (DIRBLKSIZ - 1)) == 0,
2659 1.1 dholland ("nfs readdirrpc bad uio"));
2660 1.1 dholland
2661 1.1 dholland /*
2662 1.1 dholland * There is no point in reading a lot more than uio_resid, however
2663 1.1 dholland * adding one additional DIRBLKSIZ makes sense. Since uio_resid
2664 1.1 dholland * and nm_readdirsize are both exact multiples of DIRBLKSIZ, this
2665 1.1 dholland * will never make readsize > nm_readdirsize.
2666 1.1 dholland */
2667 1.1 dholland readsize = nmp->nm_readdirsize;
2668 1.1 dholland if (readsize > uio_uio_resid(uiop))
2669 1.1 dholland readsize = uio_uio_resid(uiop) + DIRBLKSIZ;
2670 1.1 dholland
2671 1.1 dholland *attrflagp = 0;
2672 1.1 dholland if (eofp)
2673 1.1 dholland *eofp = 0;
2674 1.1 dholland tresid = uio_uio_resid(uiop);
2675 1.1 dholland cookie.lval[0] = cookiep->nfsuquad[0];
2676 1.1 dholland cookie.lval[1] = cookiep->nfsuquad[1];
2677 1.1 dholland nd->nd_mrep = NULL;
2678 1.1 dholland
2679 1.1 dholland /*
2680 1.1 dholland * For NFSv4, first create the "." and ".." entries.
2681 1.1 dholland */
2682 1.1 dholland if (NFSHASNFSV4(nmp)) {
2683 1.1 dholland reqsize = 6 * NFSX_UNSIGNED;
2684 1.1 dholland NFSGETATTR_ATTRBIT(&dattrbits);
2685 1.1 dholland NFSZERO_ATTRBIT(&attrbits);
2686 1.1 dholland NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_FILEID);
2687 1.1 dholland NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TYPE);
2688 1.1 dholland if (NFSISSET_ATTRBIT(&dnp->n_vattr.na_suppattr,
2689 1.1 dholland NFSATTRBIT_MOUNTEDONFILEID)) {
2690 1.1 dholland NFSSETBIT_ATTRBIT(&attrbits,
2691 1.1 dholland NFSATTRBIT_MOUNTEDONFILEID);
2692 1.1 dholland gotmnton = 1;
2693 1.1 dholland } else {
2694 1.1 dholland /*
2695 1.1 dholland * Must fake it. Use the fileno, except when the
2696 1.1 dholland * fsid is != to that of the directory. For that
2697 1.1 dholland * case, generate a fake fileno that is not the same.
2698 1.1 dholland */
2699 1.1 dholland NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_FSID);
2700 1.1 dholland gotmnton = 0;
2701 1.1 dholland }
2702 1.1 dholland
2703 1.1 dholland /*
2704 1.1 dholland * Joy, oh joy. For V4 we get to hand craft '.' and '..'.
2705 1.1 dholland */
2706 1.1 dholland if (uiop->uio_offset == 0) {
2707 1.1 dholland #if defined(__FreeBSD_version) && __FreeBSD_version >= 800000
2708 1.1 dholland error = VOP_GETATTR(vp, &nfsva.na_vattr, cred);
2709 1.1 dholland #else
2710 1.1 dholland error = VOP_GETATTR(vp, &nfsva.na_vattr, cred, p);
2711 1.1 dholland #endif
2712 1.1 dholland if (error)
2713 1.1 dholland return (error);
2714 1.1 dholland dotfileid = nfsva.na_fileid;
2715 1.1 dholland NFSCL_REQSTART(nd, NFSPROC_LOOKUPP, vp);
2716 1.1 dholland NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2717 1.1 dholland *tl++ = txdr_unsigned(NFSV4OP_GETFH);
2718 1.1 dholland *tl = txdr_unsigned(NFSV4OP_GETATTR);
2719 1.1 dholland (void) nfsrv_putattrbit(nd, &attrbits);
2720 1.1 dholland error = nfscl_request(nd, vp, p, cred, stuff);
2721 1.1 dholland if (error)
2722 1.1 dholland return (error);
2723 1.1 dholland if (nd->nd_repstat == 0) {
2724 1.1 dholland NFSM_DISSECT(tl, u_int32_t *, 3*NFSX_UNSIGNED);
2725 1.1 dholland len = fxdr_unsigned(int, *(tl + 2));
2726 1.1 dholland if (len > 0 && len <= NFSX_V4FHMAX)
2727 1.1 dholland error = nfsm_advance(nd, NFSM_RNDUP(len), -1);
2728 1.1 dholland else
2729 1.1 dholland error = EPERM;
2730 1.1 dholland if (!error) {
2731 1.1 dholland NFSM_DISSECT(tl, u_int32_t *, 2*NFSX_UNSIGNED);
2732 1.1 dholland nfsva.na_mntonfileno = 0xffffffff;
2733 1.1 dholland error = nfsv4_loadattr(nd, NULL, &nfsva, NULL,
2734 1.1 dholland NULL, 0, NULL, NULL, NULL, NULL, NULL, 0,
2735 1.1 dholland NULL, NULL, NULL, p, cred);
2736 1.1 dholland if (error) {
2737 1.1 dholland dotdotfileid = dotfileid;
2738 1.1 dholland } else if (gotmnton) {
2739 1.1 dholland if (nfsva.na_mntonfileno != 0xffffffff)
2740 1.1 dholland dotdotfileid = nfsva.na_mntonfileno;
2741 1.1 dholland else
2742 1.1 dholland dotdotfileid = nfsva.na_fileid;
2743 1.1 dholland } else if (nfsva.na_filesid[0] ==
2744 1.1 dholland dnp->n_vattr.na_filesid[0] &&
2745 1.1 dholland nfsva.na_filesid[1] ==
2746 1.1 dholland dnp->n_vattr.na_filesid[1]) {
2747 1.1 dholland dotdotfileid = nfsva.na_fileid;
2748 1.1 dholland } else {
2749 1.1 dholland do {
2750 1.1 dholland fakefileno--;
2751 1.1 dholland } while (fakefileno ==
2752 1.1 dholland nfsva.na_fileid);
2753 1.1 dholland dotdotfileid = fakefileno;
2754 1.1 dholland }
2755 1.1 dholland }
2756 1.1 dholland } else if (nd->nd_repstat == NFSERR_NOENT) {
2757 1.1 dholland /*
2758 1.1 dholland * Lookupp returns NFSERR_NOENT when we are
2759 1.1 dholland * at the root, so just use the current dir.
2760 1.1 dholland */
2761 1.1 dholland nd->nd_repstat = 0;
2762 1.1 dholland dotdotfileid = dotfileid;
2763 1.1 dholland } else {
2764 1.1 dholland error = nd->nd_repstat;
2765 1.1 dholland }
2766 1.1 dholland mbuf_freem(nd->nd_mrep);
2767 1.1 dholland if (error)
2768 1.1 dholland return (error);
2769 1.1 dholland nd->nd_mrep = NULL;
2770 1.1 dholland dp = (struct dirent *) CAST_DOWN(caddr_t, uio_iov_base(uiop));
2771 1.1 dholland dp->d_type = DT_DIR;
2772 1.1 dholland dp->d_fileno = dotfileid;
2773 1.1 dholland dp->d_namlen = 1;
2774 1.1 dholland dp->d_name[0] = '.';
2775 1.1 dholland dp->d_name[1] = '\0';
2776 1.1 dholland dp->d_reclen = DIRENT_SIZE(dp) + NFSX_HYPER;
2777 1.1 dholland /*
2778 1.1 dholland * Just make these offset cookie 0.
2779 1.1 dholland */
2780 1.1 dholland tl = (u_int32_t *)&dp->d_name[4];
2781 1.1 dholland *tl++ = 0;
2782 1.1 dholland *tl = 0;
2783 1.1 dholland blksiz += dp->d_reclen;
2784 1.1 dholland uio_uio_resid_add(uiop, -(dp->d_reclen));
2785 1.1 dholland uiop->uio_offset += dp->d_reclen;
2786 1.1 dholland uio_iov_base_add(uiop, dp->d_reclen);
2787 1.1 dholland uio_iov_len_add(uiop, -(dp->d_reclen));
2788 1.1 dholland dp = (struct dirent *) CAST_DOWN(caddr_t, uio_iov_base(uiop));
2789 1.1 dholland dp->d_type = DT_DIR;
2790 1.1 dholland dp->d_fileno = dotdotfileid;
2791 1.1 dholland dp->d_namlen = 2;
2792 1.1 dholland dp->d_name[0] = '.';
2793 1.1 dholland dp->d_name[1] = '.';
2794 1.1 dholland dp->d_name[2] = '\0';
2795 1.1 dholland dp->d_reclen = DIRENT_SIZE(dp) + NFSX_HYPER;
2796 1.1 dholland /*
2797 1.1 dholland * Just make these offset cookie 0.
2798 1.1 dholland */
2799 1.1 dholland tl = (u_int32_t *)&dp->d_name[4];
2800 1.1 dholland *tl++ = 0;
2801 1.1 dholland *tl = 0;
2802 1.1 dholland blksiz += dp->d_reclen;
2803 1.1 dholland uio_uio_resid_add(uiop, -(dp->d_reclen));
2804 1.1 dholland uiop->uio_offset += dp->d_reclen;
2805 1.1 dholland uio_iov_base_add(uiop, dp->d_reclen);
2806 1.1 dholland uio_iov_len_add(uiop, -(dp->d_reclen));
2807 1.1 dholland }
2808 1.1 dholland NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_RDATTRERROR);
2809 1.1 dholland } else {
2810 1.1 dholland reqsize = 5 * NFSX_UNSIGNED;
2811 1.1 dholland }
2812 1.1 dholland
2813 1.1 dholland
2814 1.1 dholland /*
2815 1.1 dholland * Loop around doing readdir rpc's of size readsize.
2816 1.1 dholland * The stopping criteria is EOF or buffer full.
2817 1.1 dholland */
2818 1.1 dholland while (more_dirs && bigenough) {
2819 1.1 dholland *attrflagp = 0;
2820 1.1 dholland NFSCL_REQSTART(nd, NFSPROC_READDIR, vp);
2821 1.1 dholland if (nd->nd_flag & ND_NFSV2) {
2822 1.1 dholland NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2823 1.1 dholland *tl++ = cookie.lval[1];
2824 1.1 dholland *tl = txdr_unsigned(readsize);
2825 1.1 dholland } else {
2826 1.1 dholland NFSM_BUILD(tl, u_int32_t *, reqsize);
2827 1.1 dholland *tl++ = cookie.lval[0];
2828 1.1 dholland *tl++ = cookie.lval[1];
2829 1.1 dholland if (cookie.qval == 0) {
2830 1.1 dholland *tl++ = 0;
2831 1.1 dholland *tl++ = 0;
2832 1.1 dholland } else {
2833 1.1 dholland NFSLOCKNODE(dnp);
2834 1.1 dholland *tl++ = dnp->n_cookieverf.nfsuquad[0];
2835 1.1 dholland *tl++ = dnp->n_cookieverf.nfsuquad[1];
2836 1.1 dholland NFSUNLOCKNODE(dnp);
2837 1.1 dholland }
2838 1.1 dholland if (nd->nd_flag & ND_NFSV4) {
2839 1.1 dholland *tl++ = txdr_unsigned(readsize);
2840 1.1 dholland *tl = txdr_unsigned(readsize);
2841 1.1 dholland (void) nfsrv_putattrbit(nd, &attrbits);
2842 1.1 dholland NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2843 1.1 dholland *tl = txdr_unsigned(NFSV4OP_GETATTR);
2844 1.1 dholland (void) nfsrv_putattrbit(nd, &dattrbits);
2845 1.1 dholland } else {
2846 1.1 dholland *tl = txdr_unsigned(readsize);
2847 1.1 dholland }
2848 1.1 dholland }
2849 1.1 dholland error = nfscl_request(nd, vp, p, cred, stuff);
2850 1.1 dholland if (error)
2851 1.1 dholland return (error);
2852 1.1 dholland if (!(nd->nd_flag & ND_NFSV2)) {
2853 1.1 dholland if (nd->nd_flag & ND_NFSV3)
2854 1.1 dholland error = nfscl_postop_attr(nd, nap, attrflagp,
2855 1.1 dholland stuff);
2856 1.1 dholland if (!nd->nd_repstat && !error) {
2857 1.1 dholland NFSM_DISSECT(tl, u_int32_t *, NFSX_HYPER);
2858 1.1 dholland NFSLOCKNODE(dnp);
2859 1.1 dholland dnp->n_cookieverf.nfsuquad[0] = *tl++;
2860 1.1 dholland dnp->n_cookieverf.nfsuquad[1] = *tl;
2861 1.1 dholland NFSUNLOCKNODE(dnp);
2862 1.1 dholland }
2863 1.1 dholland }
2864 1.1 dholland if (nd->nd_repstat || error) {
2865 1.1 dholland if (!error)
2866 1.1 dholland error = nd->nd_repstat;
2867 1.1 dholland goto nfsmout;
2868 1.1 dholland }
2869 1.1 dholland NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2870 1.1 dholland more_dirs = fxdr_unsigned(int, *tl);
2871 1.1 dholland if (!more_dirs)
2872 1.1 dholland tryformoredirs = 0;
2873 1.1 dholland
2874 1.1 dholland /* loop thru the dir entries, doctoring them to 4bsd form */
2875 1.1 dholland while (more_dirs && bigenough) {
2876 1.1 dholland if (nd->nd_flag & ND_NFSV4) {
2877 1.1 dholland NFSM_DISSECT(tl, u_int32_t *, 3*NFSX_UNSIGNED);
2878 1.1 dholland ncookie.lval[0] = *tl++;
2879 1.1 dholland ncookie.lval[1] = *tl++;
2880 1.1 dholland len = fxdr_unsigned(int, *tl);
2881 1.1 dholland } else if (nd->nd_flag & ND_NFSV3) {
2882 1.1 dholland NFSM_DISSECT(tl, u_int32_t *, 3*NFSX_UNSIGNED);
2883 1.1 dholland nfsva.na_fileid = fxdr_hyper(tl);
2884 1.1 dholland tl += 2;
2885 1.1 dholland len = fxdr_unsigned(int, *tl);
2886 1.1 dholland } else {
2887 1.1 dholland NFSM_DISSECT(tl, u_int32_t *, 2*NFSX_UNSIGNED);
2888 1.1 dholland nfsva.na_fileid =
2889 1.1 dholland fxdr_unsigned(long, *tl++);
2890 1.1 dholland len = fxdr_unsigned(int, *tl);
2891 1.1 dholland }
2892 1.1 dholland if (len <= 0 || len > NFS_MAXNAMLEN) {
2893 1.1 dholland error = EBADRPC;
2894 1.1 dholland goto nfsmout;
2895 1.1 dholland }
2896 1.1 dholland tlen = NFSM_RNDUP(len);
2897 1.1 dholland if (tlen == len)
2898 1.1 dholland tlen += 4; /* To ensure null termination */
2899 1.1 dholland left = DIRBLKSIZ - blksiz;
2900 1.1 dholland if ((int)(tlen + DIRHDSIZ + NFSX_HYPER) > left) {
2901 1.1 dholland dp->d_reclen += left;
2902 1.1 dholland uio_iov_base_add(uiop, left);
2903 1.1 dholland uio_iov_len_add(uiop, -(left));
2904 1.1 dholland uio_uio_resid_add(uiop, -(left));
2905 1.1 dholland uiop->uio_offset += left;
2906 1.1 dholland blksiz = 0;
2907 1.1 dholland }
2908 1.1 dholland if ((int)(tlen + DIRHDSIZ + NFSX_HYPER) > uio_uio_resid(uiop))
2909 1.1 dholland bigenough = 0;
2910 1.1 dholland if (bigenough) {
2911 1.1 dholland dp = (struct dirent *) CAST_DOWN(caddr_t, uio_iov_base(uiop));
2912 1.1 dholland dp->d_namlen = len;
2913 1.1 dholland dp->d_reclen = tlen + DIRHDSIZ + NFSX_HYPER;
2914 1.1 dholland dp->d_type = DT_UNKNOWN;
2915 1.1 dholland blksiz += dp->d_reclen;
2916 1.1 dholland if (blksiz == DIRBLKSIZ)
2917 1.1 dholland blksiz = 0;
2918 1.1 dholland uio_uio_resid_add(uiop, -(DIRHDSIZ));
2919 1.1 dholland uiop->uio_offset += DIRHDSIZ;
2920 1.1 dholland uio_iov_base_add(uiop, DIRHDSIZ);
2921 1.1 dholland uio_iov_len_add(uiop, -(DIRHDSIZ));
2922 1.1 dholland error = nfsm_mbufuio(nd, uiop, len);
2923 1.1 dholland if (error)
2924 1.1 dholland goto nfsmout;
2925 1.1 dholland cp = CAST_DOWN(caddr_t, uio_iov_base(uiop));
2926 1.1 dholland tlen -= len;
2927 1.1 dholland *cp = '\0'; /* null terminate */
2928 1.1 dholland cp += tlen; /* points to cookie storage */
2929 1.1 dholland tl2 = (u_int32_t *)cp;
2930 1.1 dholland uio_iov_base_add(uiop, (tlen + NFSX_HYPER));
2931 1.1 dholland uio_iov_len_add(uiop, -(tlen + NFSX_HYPER));
2932 1.1 dholland uio_uio_resid_add(uiop, -(tlen + NFSX_HYPER));
2933 1.1 dholland uiop->uio_offset += (tlen + NFSX_HYPER);
2934 1.1 dholland } else {
2935 1.1 dholland error = nfsm_advance(nd, NFSM_RNDUP(len), -1);
2936 1.1 dholland if (error)
2937 1.1 dholland goto nfsmout;
2938 1.1 dholland }
2939 1.1 dholland if (nd->nd_flag & ND_NFSV4) {
2940 1.1 dholland rderr = 0;
2941 1.1 dholland nfsva.na_mntonfileno = 0xffffffff;
2942 1.1 dholland error = nfsv4_loadattr(nd, NULL, &nfsva, NULL,
2943 1.1 dholland NULL, 0, NULL, NULL, NULL, NULL, NULL, 0,
2944 1.1 dholland NULL, NULL, &rderr, p, cred);
2945 1.1 dholland if (error)
2946 1.1 dholland goto nfsmout;
2947 1.1 dholland NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2948 1.1 dholland } else if (nd->nd_flag & ND_NFSV3) {
2949 1.1 dholland NFSM_DISSECT(tl, u_int32_t *, 3*NFSX_UNSIGNED);
2950 1.1 dholland ncookie.lval[0] = *tl++;
2951 1.1 dholland ncookie.lval[1] = *tl++;
2952 1.1 dholland } else {
2953 1.1 dholland NFSM_DISSECT(tl, u_int32_t *, 2*NFSX_UNSIGNED);
2954 1.1 dholland ncookie.lval[0] = 0;
2955 1.1 dholland ncookie.lval[1] = *tl++;
2956 1.1 dholland }
2957 1.1 dholland if (bigenough) {
2958 1.1 dholland if (nd->nd_flag & ND_NFSV4) {
2959 1.1 dholland if (rderr) {
2960 1.1 dholland dp->d_fileno = 0;
2961 1.1 dholland } else {
2962 1.1 dholland if (gotmnton) {
2963 1.1 dholland if (nfsva.na_mntonfileno != 0xffffffff)
2964 1.1 dholland dp->d_fileno = nfsva.na_mntonfileno;
2965 1.1 dholland else
2966 1.1 dholland dp->d_fileno = nfsva.na_fileid;
2967 1.1 dholland } else if (nfsva.na_filesid[0] ==
2968 1.1 dholland dnp->n_vattr.na_filesid[0] &&
2969 1.1 dholland nfsva.na_filesid[1] ==
2970 1.1 dholland dnp->n_vattr.na_filesid[1]) {
2971 1.1 dholland dp->d_fileno = nfsva.na_fileid;
2972 1.1 dholland } else {
2973 1.1 dholland do {
2974 1.1 dholland fakefileno--;
2975 1.1 dholland } while (fakefileno ==
2976 1.1 dholland nfsva.na_fileid);
2977 1.1 dholland dp->d_fileno = fakefileno;
2978 1.1 dholland }
2979 1.1 dholland dp->d_type = vtonfs_dtype(nfsva.na_type);
2980 1.1 dholland }
2981 1.1 dholland } else {
2982 1.1 dholland dp->d_fileno = nfsva.na_fileid;
2983 1.1 dholland }
2984 1.1 dholland *tl2++ = cookiep->nfsuquad[0] = cookie.lval[0] =
2985 1.1 dholland ncookie.lval[0];
2986 1.1 dholland *tl2 = cookiep->nfsuquad[1] = cookie.lval[1] =
2987 1.1 dholland ncookie.lval[1];
2988 1.1 dholland }
2989 1.1 dholland more_dirs = fxdr_unsigned(int, *tl);
2990 1.1 dholland }
2991 1.1 dholland /*
2992 1.1 dholland * If at end of rpc data, get the eof boolean
2993 1.1 dholland */
2994 1.1 dholland if (!more_dirs) {
2995 1.1 dholland NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2996 1.1 dholland eof = fxdr_unsigned(int, *tl);
2997 1.1 dholland if (tryformoredirs)
2998 1.1 dholland more_dirs = !eof;
2999 1.1 dholland if (nd->nd_flag & ND_NFSV4) {
3000 1.1 dholland error = nfscl_postop_attr(nd, nap, attrflagp,
3001 1.1 dholland stuff);
3002 1.1 dholland if (error)
3003 1.1 dholland goto nfsmout;
3004 1.1 dholland }
3005 1.1 dholland }
3006 1.1 dholland mbuf_freem(nd->nd_mrep);
3007 1.1 dholland nd->nd_mrep = NULL;
3008 1.1 dholland }
3009 1.1 dholland /*
3010 1.1 dholland * Fill last record, iff any, out to a multiple of DIRBLKSIZ
3011 1.1 dholland * by increasing d_reclen for the last record.
3012 1.1 dholland */
3013 1.1 dholland if (blksiz > 0) {
3014 1.1 dholland left = DIRBLKSIZ - blksiz;
3015 1.1 dholland dp->d_reclen += left;
3016 1.1 dholland uio_iov_base_add(uiop, left);
3017 1.1 dholland uio_iov_len_add(uiop, -(left));
3018 1.1 dholland uio_uio_resid_add(uiop, -(left));
3019 1.1 dholland uiop->uio_offset += left;
3020 1.1 dholland }
3021 1.1 dholland
3022 1.1 dholland /*
3023 1.1 dholland * If returning no data, assume end of file.
3024 1.1 dholland * If not bigenough, return not end of file, since you aren't
3025 1.1 dholland * returning all the data
3026 1.1 dholland * Otherwise, return the eof flag from the server.
3027 1.1 dholland */
3028 1.1 dholland if (eofp) {
3029 1.1 dholland if (tresid == ((size_t)(uio_uio_resid(uiop))))
3030 1.1 dholland *eofp = 1;
3031 1.1 dholland else if (!bigenough)
3032 1.1 dholland *eofp = 0;
3033 1.1 dholland else
3034 1.1 dholland *eofp = eof;
3035 1.1 dholland }
3036 1.1 dholland
3037 1.1 dholland /*
3038 1.1 dholland * Add extra empty records to any remaining DIRBLKSIZ chunks.
3039 1.1 dholland */
3040 1.1 dholland while (uio_uio_resid(uiop) > 0 && ((size_t)(uio_uio_resid(uiop))) != tresid) {
3041 1.1 dholland dp = (struct dirent *) CAST_DOWN(caddr_t, uio_iov_base(uiop));
3042 1.1 dholland dp->d_type = DT_UNKNOWN;
3043 1.1 dholland dp->d_fileno = 0;
3044 1.1 dholland dp->d_namlen = 0;
3045 1.1 dholland dp->d_name[0] = '\0';
3046 1.1 dholland tl = (u_int32_t *)&dp->d_name[4];
3047 1.1 dholland *tl++ = cookie.lval[0];
3048 1.1 dholland *tl = cookie.lval[1];
3049 1.1 dholland dp->d_reclen = DIRBLKSIZ;
3050 1.1 dholland uio_iov_base_add(uiop, DIRBLKSIZ);
3051 1.1 dholland uio_iov_len_add(uiop, -(DIRBLKSIZ));
3052 1.1 dholland uio_uio_resid_add(uiop, -(DIRBLKSIZ));
3053 1.1 dholland uiop->uio_offset += DIRBLKSIZ;
3054 1.1 dholland }
3055 1.1 dholland
3056 1.1 dholland nfsmout:
3057 1.1 dholland if (nd->nd_mrep != NULL)
3058 1.1 dholland mbuf_freem(nd->nd_mrep);
3059 1.1 dholland return (error);
3060 1.1 dholland }
3061 1.1 dholland
3062 1.1 dholland #ifndef APPLE
3063 1.1 dholland /*
3064 1.1 dholland * NFS V3 readdir plus RPC. Used in place of nfsrpc_readdir().
3065 1.1 dholland * (Also used for NFS V4 when mount flag set.)
3066 1.1 dholland * (ditto above w.r.t. multiple of DIRBLKSIZ, etc.)
3067 1.1 dholland */
3068 1.1 dholland APPLESTATIC int
3069 1.1 dholland nfsrpc_readdirplus(vnode_t vp, struct uio *uiop, nfsuint64 *cookiep,
3070 1.1 dholland struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp,
3071 1.1 dholland int *eofp, void *stuff)
3072 1.1 dholland {
3073 1.1 dholland int len, left;
3074 1.1 dholland struct dirent *dp = NULL;
3075 1.1 dholland u_int32_t *tl;
3076 1.1 dholland vnode_t newvp = NULLVP;
3077 1.1 dholland struct nfsrv_descript nfsd, *nd = &nfsd;
3078 1.1 dholland struct nameidata nami, *ndp = &nami;
3079 1.1 dholland struct componentname *cnp = &ndp->ni_cnd;
3080 1.1 dholland struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
3081 1.1 dholland struct nfsnode *dnp = VTONFS(vp), *np;
3082 1.1 dholland struct nfsvattr nfsva;
3083 1.1 dholland struct nfsfh *nfhp;
3084 1.1 dholland nfsquad_t cookie, ncookie;
3085 1.1 dholland int error = 0, tlen, more_dirs = 1, blksiz = 0, bigenough = 1;
3086 1.1 dholland int attrflag, tryformoredirs = 1, eof = 0, gotmnton = 0;
3087 1.1 dholland int isdotdot = 0, unlocknewvp = 0;
3088 1.1 dholland long dotfileid, dotdotfileid = 0, fileno = 0;
3089 1.1 dholland char *cp;
3090 1.1 dholland nfsattrbit_t attrbits, dattrbits;
3091 1.1 dholland size_t tresid;
3092 1.1 dholland u_int32_t *tl2 = NULL, fakefileno = 0xffffffff, rderr;
3093 1.1 dholland struct timespec dctime;
3094 1.1 dholland
3095 1.1 dholland KASSERT(uiop->uio_iovcnt == 1 &&
3096 1.1 dholland (uio_uio_resid(uiop) & (DIRBLKSIZ - 1)) == 0,
3097 1.1 dholland ("nfs readdirplusrpc bad uio"));
3098 1.1 dholland timespecclear(&dctime);
3099 1.1 dholland *attrflagp = 0;
3100 1.1 dholland if (eofp != NULL)
3101 1.1 dholland *eofp = 0;
3102 1.1 dholland ndp->ni_dvp = vp;
3103 1.1 dholland nd->nd_mrep = NULL;
3104 1.1 dholland cookie.lval[0] = cookiep->nfsuquad[0];
3105 1.1 dholland cookie.lval[1] = cookiep->nfsuquad[1];
3106 1.1 dholland tresid = uio_uio_resid(uiop);
3107 1.1 dholland
3108 1.1 dholland /*
3109 1.1 dholland * For NFSv4, first create the "." and ".." entries.
3110 1.1 dholland */
3111 1.1 dholland if (NFSHASNFSV4(nmp)) {
3112 1.1 dholland NFSGETATTR_ATTRBIT(&dattrbits);
3113 1.1 dholland NFSZERO_ATTRBIT(&attrbits);
3114 1.1 dholland NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_FILEID);
3115 1.1 dholland if (NFSISSET_ATTRBIT(&dnp->n_vattr.na_suppattr,
3116 1.1 dholland NFSATTRBIT_MOUNTEDONFILEID)) {
3117 1.1 dholland NFSSETBIT_ATTRBIT(&attrbits,
3118 1.1 dholland NFSATTRBIT_MOUNTEDONFILEID);
3119 1.1 dholland gotmnton = 1;
3120 1.1 dholland } else {
3121 1.1 dholland /*
3122 1.1 dholland * Must fake it. Use the fileno, except when the
3123 1.1 dholland * fsid is != to that of the directory. For that
3124 1.1 dholland * case, generate a fake fileno that is not the same.
3125 1.1 dholland */
3126 1.1 dholland NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_FSID);
3127 1.1 dholland gotmnton = 0;
3128 1.1 dholland }
3129 1.1 dholland
3130 1.1 dholland /*
3131 1.1 dholland * Joy, oh joy. For V4 we get to hand craft '.' and '..'.
3132 1.1 dholland */
3133 1.1 dholland if (uiop->uio_offset == 0) {
3134 1.1 dholland #if defined(__FreeBSD_version) && __FreeBSD_version >= 800000
3135 1.1 dholland error = VOP_GETATTR(vp, &nfsva.na_vattr, cred);
3136 1.1 dholland #else
3137 1.1 dholland error = VOP_GETATTR(vp, &nfsva.na_vattr, cred, p);
3138 1.1 dholland #endif
3139 1.1 dholland if (error)
3140 1.1 dholland return (error);
3141 1.1 dholland dctime = nfsva.na_ctime;
3142 1.1 dholland dotfileid = nfsva.na_fileid;
3143 1.1 dholland NFSCL_REQSTART(nd, NFSPROC_LOOKUPP, vp);
3144 1.1 dholland NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
3145 1.1 dholland *tl++ = txdr_unsigned(NFSV4OP_GETFH);
3146 1.1 dholland *tl = txdr_unsigned(NFSV4OP_GETATTR);
3147 1.1 dholland (void) nfsrv_putattrbit(nd, &attrbits);
3148 1.1 dholland error = nfscl_request(nd, vp, p, cred, stuff);
3149 1.1 dholland if (error)
3150 1.1 dholland return (error);
3151 1.1 dholland if (nd->nd_repstat == 0) {
3152 1.1 dholland NFSM_DISSECT(tl, u_int32_t *, 3*NFSX_UNSIGNED);
3153 1.1 dholland len = fxdr_unsigned(int, *(tl + 2));
3154 1.1 dholland if (len > 0 && len <= NFSX_V4FHMAX)
3155 1.1 dholland error = nfsm_advance(nd, NFSM_RNDUP(len), -1);
3156 1.1 dholland else
3157 1.1 dholland error = EPERM;
3158 1.1 dholland if (!error) {
3159 1.1 dholland NFSM_DISSECT(tl, u_int32_t *, 2*NFSX_UNSIGNED);
3160 1.1 dholland nfsva.na_mntonfileno = 0xffffffff;
3161 1.1 dholland error = nfsv4_loadattr(nd, NULL, &nfsva, NULL,
3162 1.1 dholland NULL, 0, NULL, NULL, NULL, NULL, NULL, 0,
3163 1.1 dholland NULL, NULL, NULL, p, cred);
3164 1.1 dholland if (error) {
3165 1.1 dholland dotdotfileid = dotfileid;
3166 1.1 dholland } else if (gotmnton) {
3167 1.1 dholland if (nfsva.na_mntonfileno != 0xffffffff)
3168 1.1 dholland dotdotfileid = nfsva.na_mntonfileno;
3169 1.1 dholland else
3170 1.1 dholland dotdotfileid = nfsva.na_fileid;
3171 1.1 dholland } else if (nfsva.na_filesid[0] ==
3172 1.1 dholland dnp->n_vattr.na_filesid[0] &&
3173 1.1 dholland nfsva.na_filesid[1] ==
3174 1.1 dholland dnp->n_vattr.na_filesid[1]) {
3175 1.1 dholland dotdotfileid = nfsva.na_fileid;
3176 1.1 dholland } else {
3177 1.1 dholland do {
3178 1.1 dholland fakefileno--;
3179 1.1 dholland } while (fakefileno ==
3180 1.1 dholland nfsva.na_fileid);
3181 1.1 dholland dotdotfileid = fakefileno;
3182 1.1 dholland }
3183 1.1 dholland }
3184 1.1 dholland } else if (nd->nd_repstat == NFSERR_NOENT) {
3185 1.1 dholland /*
3186 1.1 dholland * Lookupp returns NFSERR_NOENT when we are
3187 1.1 dholland * at the root, so just use the current dir.
3188 1.1 dholland */
3189 1.1 dholland nd->nd_repstat = 0;
3190 1.1 dholland dotdotfileid = dotfileid;
3191 1.1 dholland } else {
3192 1.1 dholland error = nd->nd_repstat;
3193 1.1 dholland }
3194 1.1 dholland mbuf_freem(nd->nd_mrep);
3195 1.1 dholland if (error)
3196 1.1 dholland return (error);
3197 1.1 dholland nd->nd_mrep = NULL;
3198 1.1 dholland dp = (struct dirent *)uio_iov_base(uiop);
3199 1.1 dholland dp->d_type = DT_DIR;
3200 1.1 dholland dp->d_fileno = dotfileid;
3201 1.1 dholland dp->d_namlen = 1;
3202 1.1 dholland dp->d_name[0] = '.';
3203 1.1 dholland dp->d_name[1] = '\0';
3204 1.1 dholland dp->d_reclen = DIRENT_SIZE(dp) + NFSX_HYPER;
3205 1.1 dholland /*
3206 1.1 dholland * Just make these offset cookie 0.
3207 1.1 dholland */
3208 1.1 dholland tl = (u_int32_t *)&dp->d_name[4];
3209 1.1 dholland *tl++ = 0;
3210 1.1 dholland *tl = 0;
3211 1.1 dholland blksiz += dp->d_reclen;
3212 1.1 dholland uio_uio_resid_add(uiop, -(dp->d_reclen));
3213 1.1 dholland uiop->uio_offset += dp->d_reclen;
3214 1.1 dholland uio_iov_base_add(uiop, dp->d_reclen);
3215 1.1 dholland uio_iov_len_add(uiop, -(dp->d_reclen));
3216 1.1 dholland dp = (struct dirent *)uio_iov_base(uiop);
3217 1.1 dholland dp->d_type = DT_DIR;
3218 1.1 dholland dp->d_fileno = dotdotfileid;
3219 1.1 dholland dp->d_namlen = 2;
3220 1.1 dholland dp->d_name[0] = '.';
3221 1.1 dholland dp->d_name[1] = '.';
3222 1.1 dholland dp->d_name[2] = '\0';
3223 1.1 dholland dp->d_reclen = DIRENT_SIZE(dp) + NFSX_HYPER;
3224 1.1 dholland /*
3225 1.1 dholland * Just make these offset cookie 0.
3226 1.1 dholland */
3227 1.1 dholland tl = (u_int32_t *)&dp->d_name[4];
3228 1.1 dholland *tl++ = 0;
3229 1.1 dholland *tl = 0;
3230 1.1 dholland blksiz += dp->d_reclen;
3231 1.1 dholland uio_uio_resid_add(uiop, -(dp->d_reclen));
3232 1.1 dholland uiop->uio_offset += dp->d_reclen;
3233 1.1 dholland uio_iov_base_add(uiop, dp->d_reclen);
3234 1.1 dholland uio_iov_len_add(uiop, -(dp->d_reclen));
3235 1.1 dholland }
3236 1.1 dholland NFSREADDIRPLUS_ATTRBIT(&attrbits);
3237 1.1 dholland if (gotmnton)
3238 1.1 dholland NFSSETBIT_ATTRBIT(&attrbits,
3239 1.1 dholland NFSATTRBIT_MOUNTEDONFILEID);
3240 1.1 dholland }
3241 1.1 dholland
3242 1.1 dholland /*
3243 1.1 dholland * Loop around doing readdir rpc's of size nm_readdirsize.
3244 1.1 dholland * The stopping criteria is EOF or buffer full.
3245 1.1 dholland */
3246 1.1 dholland while (more_dirs && bigenough) {
3247 1.1 dholland *attrflagp = 0;
3248 1.1 dholland NFSCL_REQSTART(nd, NFSPROC_READDIRPLUS, vp);
3249 1.1 dholland NFSM_BUILD(tl, u_int32_t *, 6 * NFSX_UNSIGNED);
3250 1.1 dholland *tl++ = cookie.lval[0];
3251 1.1 dholland *tl++ = cookie.lval[1];
3252 1.1 dholland if (cookie.qval == 0) {
3253 1.1 dholland *tl++ = 0;
3254 1.1 dholland *tl++ = 0;
3255 1.1 dholland } else {
3256 1.1 dholland NFSLOCKNODE(dnp);
3257 1.1 dholland *tl++ = dnp->n_cookieverf.nfsuquad[0];
3258 1.1 dholland *tl++ = dnp->n_cookieverf.nfsuquad[1];
3259 1.1 dholland NFSUNLOCKNODE(dnp);
3260 1.1 dholland }
3261 1.1 dholland *tl++ = txdr_unsigned(nmp->nm_readdirsize);
3262 1.1 dholland *tl = txdr_unsigned(nmp->nm_readdirsize);
3263 1.1 dholland if (nd->nd_flag & ND_NFSV4) {
3264 1.1 dholland (void) nfsrv_putattrbit(nd, &attrbits);
3265 1.1 dholland NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
3266 1.1 dholland *tl = txdr_unsigned(NFSV4OP_GETATTR);
3267 1.1 dholland (void) nfsrv_putattrbit(nd, &dattrbits);
3268 1.1 dholland }
3269 1.1 dholland error = nfscl_request(nd, vp, p, cred, stuff);
3270 1.1 dholland if (error)
3271 1.1 dholland return (error);
3272 1.1 dholland if (nd->nd_flag & ND_NFSV3)
3273 1.1 dholland error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
3274 1.1 dholland if (nd->nd_repstat || error) {
3275 1.1 dholland if (!error)
3276 1.1 dholland error = nd->nd_repstat;
3277 1.1 dholland goto nfsmout;
3278 1.1 dholland }
3279 1.1 dholland if ((nd->nd_flag & ND_NFSV3) != 0 && *attrflagp != 0)
3280 1.1 dholland dctime = nap->na_ctime;
3281 1.1 dholland NFSM_DISSECT(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
3282 1.1 dholland NFSLOCKNODE(dnp);
3283 1.1 dholland dnp->n_cookieverf.nfsuquad[0] = *tl++;
3284 1.1 dholland dnp->n_cookieverf.nfsuquad[1] = *tl++;
3285 1.1 dholland NFSUNLOCKNODE(dnp);
3286 1.1 dholland more_dirs = fxdr_unsigned(int, *tl);
3287 1.1 dholland if (!more_dirs)
3288 1.1 dholland tryformoredirs = 0;
3289 1.1 dholland
3290 1.1 dholland /* loop thru the dir entries, doctoring them to 4bsd form */
3291 1.1 dholland while (more_dirs && bigenough) {
3292 1.1 dholland NFSM_DISSECT(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
3293 1.1 dholland if (nd->nd_flag & ND_NFSV4) {
3294 1.1 dholland ncookie.lval[0] = *tl++;
3295 1.1 dholland ncookie.lval[1] = *tl++;
3296 1.1 dholland } else {
3297 1.1 dholland fileno = fxdr_unsigned(long, *++tl);
3298 1.1 dholland tl++;
3299 1.1 dholland }
3300 1.1 dholland len = fxdr_unsigned(int, *tl);
3301 1.1 dholland if (len <= 0 || len > NFS_MAXNAMLEN) {
3302 1.1 dholland error = EBADRPC;
3303 1.1 dholland goto nfsmout;
3304 1.1 dholland }
3305 1.1 dholland tlen = NFSM_RNDUP(len);
3306 1.1 dholland if (tlen == len)
3307 1.1 dholland tlen += 4; /* To ensure null termination */
3308 1.1 dholland left = DIRBLKSIZ - blksiz;
3309 1.1 dholland if ((tlen + DIRHDSIZ + NFSX_HYPER) > left) {
3310 1.1 dholland dp->d_reclen += left;
3311 1.1 dholland uio_iov_base_add(uiop, left);
3312 1.1 dholland uio_iov_len_add(uiop, -(left));
3313 1.1 dholland uio_uio_resid_add(uiop, -(left));
3314 1.1 dholland uiop->uio_offset += left;
3315 1.1 dholland blksiz = 0;
3316 1.1 dholland }
3317 1.1 dholland if ((tlen + DIRHDSIZ + NFSX_HYPER) > uio_uio_resid(uiop))
3318 1.1 dholland bigenough = 0;
3319 1.1 dholland if (bigenough) {
3320 1.1 dholland dp = (struct dirent *)uio_iov_base(uiop);
3321 1.1 dholland dp->d_namlen = len;
3322 1.1 dholland dp->d_reclen = tlen + DIRHDSIZ + NFSX_HYPER;
3323 1.1 dholland dp->d_type = DT_UNKNOWN;
3324 1.1 dholland blksiz += dp->d_reclen;
3325 1.1 dholland if (blksiz == DIRBLKSIZ)
3326 1.1 dholland blksiz = 0;
3327 1.1 dholland uio_uio_resid_add(uiop, -(DIRHDSIZ));
3328 1.1 dholland uiop->uio_offset += DIRHDSIZ;
3329 1.1 dholland uio_iov_base_add(uiop, DIRHDSIZ);
3330 1.1 dholland uio_iov_len_add(uiop, -(DIRHDSIZ));
3331 1.1 dholland cnp->cn_nameptr = uio_iov_base(uiop);
3332 1.1 dholland cnp->cn_namelen = len;
3333 1.1 dholland NFSCNHASHZERO(cnp);
3334 1.1 dholland error = nfsm_mbufuio(nd, uiop, len);
3335 1.1 dholland if (error)
3336 1.1 dholland goto nfsmout;
3337 1.1 dholland cp = uio_iov_base(uiop);
3338 1.1 dholland tlen -= len;
3339 1.1 dholland *cp = '\0';
3340 1.1 dholland cp += tlen; /* points to cookie storage */
3341 1.1 dholland tl2 = (u_int32_t *)cp;
3342 1.1 dholland if (len == 2 && cnp->cn_nameptr[0] == '.' &&
3343 1.1 dholland cnp->cn_nameptr[1] == '.')
3344 1.1 dholland isdotdot = 1;
3345 1.1 dholland else
3346 1.1 dholland isdotdot = 0;
3347 1.1 dholland uio_iov_base_add(uiop, (tlen + NFSX_HYPER));
3348 1.1 dholland uio_iov_len_add(uiop, -(tlen + NFSX_HYPER));
3349 1.1 dholland uio_uio_resid_add(uiop, -(tlen + NFSX_HYPER));
3350 1.1 dholland uiop->uio_offset += (tlen + NFSX_HYPER);
3351 1.1 dholland } else {
3352 1.1 dholland error = nfsm_advance(nd, NFSM_RNDUP(len), -1);
3353 1.1 dholland if (error)
3354 1.1 dholland goto nfsmout;
3355 1.1 dholland }
3356 1.1 dholland nfhp = NULL;
3357 1.1 dholland if (nd->nd_flag & ND_NFSV3) {
3358 1.1 dholland NFSM_DISSECT(tl, u_int32_t *, 3*NFSX_UNSIGNED);
3359 1.1 dholland ncookie.lval[0] = *tl++;
3360 1.1 dholland ncookie.lval[1] = *tl++;
3361 1.1 dholland attrflag = fxdr_unsigned(int, *tl);
3362 1.1 dholland if (attrflag) {
3363 1.1 dholland error = nfsm_loadattr(nd, &nfsva);
3364 1.1 dholland if (error)
3365 1.1 dholland goto nfsmout;
3366 1.1 dholland }
3367 1.1 dholland NFSM_DISSECT(tl,u_int32_t *,NFSX_UNSIGNED);
3368 1.1 dholland if (*tl) {
3369 1.1 dholland error = nfsm_getfh(nd, &nfhp);
3370 1.1 dholland if (error)
3371 1.1 dholland goto nfsmout;
3372 1.1 dholland }
3373 1.1 dholland if (!attrflag && nfhp != NULL) {
3374 1.1 dholland FREE((caddr_t)nfhp, M_NFSFH);
3375 1.1 dholland nfhp = NULL;
3376 1.1 dholland }
3377 1.1 dholland } else {
3378 1.1 dholland rderr = 0;
3379 1.1 dholland nfsva.na_mntonfileno = 0xffffffff;
3380 1.1 dholland error = nfsv4_loadattr(nd, NULL, &nfsva, &nfhp,
3381 1.1 dholland NULL, 0, NULL, NULL, NULL, NULL, NULL, 0,
3382 1.1 dholland NULL, NULL, &rderr, p, cred);
3383 1.1 dholland if (error)
3384 1.1 dholland goto nfsmout;
3385 1.1 dholland }
3386 1.1 dholland
3387 1.1 dholland if (bigenough) {
3388 1.1 dholland if (nd->nd_flag & ND_NFSV4) {
3389 1.1 dholland if (rderr) {
3390 1.1 dholland dp->d_fileno = 0;
3391 1.1 dholland } else if (gotmnton) {
3392 1.1 dholland if (nfsva.na_mntonfileno != 0xffffffff)
3393 1.1 dholland dp->d_fileno = nfsva.na_mntonfileno;
3394 1.1 dholland else
3395 1.1 dholland dp->d_fileno = nfsva.na_fileid;
3396 1.1 dholland } else if (nfsva.na_filesid[0] ==
3397 1.1 dholland dnp->n_vattr.na_filesid[0] &&
3398 1.1 dholland nfsva.na_filesid[1] ==
3399 1.1 dholland dnp->n_vattr.na_filesid[1]) {
3400 1.1 dholland dp->d_fileno = nfsva.na_fileid;
3401 1.1 dholland } else {
3402 1.1 dholland do {
3403 1.1 dholland fakefileno--;
3404 1.1 dholland } while (fakefileno ==
3405 1.1 dholland nfsva.na_fileid);
3406 1.1 dholland dp->d_fileno = fakefileno;
3407 1.1 dholland }
3408 1.1 dholland } else {
3409 1.1 dholland dp->d_fileno = fileno;
3410 1.1 dholland }
3411 1.1 dholland *tl2++ = cookiep->nfsuquad[0] = cookie.lval[0] =
3412 1.1 dholland ncookie.lval[0];
3413 1.1 dholland *tl2 = cookiep->nfsuquad[1] = cookie.lval[1] =
3414 1.1 dholland ncookie.lval[1];
3415 1.1 dholland
3416 1.1 dholland if (nfhp != NULL) {
3417 1.1 dholland if (NFSRV_CMPFH(nfhp->nfh_fh, nfhp->nfh_len,
3418 1.1 dholland dnp->n_fhp->nfh_fh, dnp->n_fhp->nfh_len)) {
3419 1.1 dholland VREF(vp);
3420 1.1 dholland newvp = vp;
3421 1.1 dholland unlocknewvp = 0;
3422 1.1 dholland FREE((caddr_t)nfhp, M_NFSFH);
3423 1.1 dholland np = dnp;
3424 1.1 dholland } else if (isdotdot != 0) {
3425 1.1 dholland /*
3426 1.1 dholland * Skip doing a nfscl_nget() call for "..".
3427 1.1 dholland * There's a race between acquiring the nfs
3428 1.1 dholland * node here and lookups that look for the
3429 1.1 dholland * directory being read (in the parent).
3430 1.1 dholland * It would try to get a lock on ".." here,
3431 1.1 dholland * owning the lock on the directory being
3432 1.1 dholland * read. Lookup will hold the lock on ".."
3433 1.1 dholland * and try to acquire the lock on the
3434 1.1 dholland * directory being read.
3435 1.1 dholland * If the directory is unlocked/relocked,
3436 1.1 dholland * then there is a LOR with the buflock
3437 1.1 dholland * vp is relocked.
3438 1.1 dholland */
3439 1.1 dholland free(nfhp, M_NFSFH);
3440 1.1 dholland } else {
3441 1.1 dholland error = nfscl_nget(vnode_mount(vp), vp,
3442 1.1 dholland nfhp, cnp, p, &np, NULL, LK_EXCLUSIVE);
3443 1.1 dholland if (!error) {
3444 1.1 dholland newvp = NFSTOV(np);
3445 1.1 dholland unlocknewvp = 1;
3446 1.1 dholland }
3447 1.1 dholland }
3448 1.1 dholland nfhp = NULL;
3449 1.1 dholland if (newvp != NULLVP) {
3450 1.1 dholland error = nfscl_loadattrcache(&newvp,
3451 1.1 dholland &nfsva, NULL, NULL, 0, 0);
3452 1.1 dholland if (error) {
3453 1.1 dholland if (unlocknewvp)
3454 1.1 dholland vput(newvp);
3455 1.1 dholland else
3456 1.1 dholland vrele(newvp);
3457 1.1 dholland goto nfsmout;
3458 1.1 dholland }
3459 1.1 dholland dp->d_type =
3460 1.1 dholland vtonfs_dtype(np->n_vattr.na_type);
3461 1.1 dholland ndp->ni_vp = newvp;
3462 1.1 dholland NFSCNHASH(cnp, HASHINIT);
3463 1.1 dholland if (cnp->cn_namelen <= NCHNAMLEN &&
3464 1.1 dholland (newvp->v_type != VDIR ||
3465 1.1 dholland dctime.tv_sec != 0)) {
3466 1.1 dholland cache_enter_time(ndp->ni_dvp,
3467 1.1 dholland ndp->ni_vp, cnp,
3468 1.1 dholland &nfsva.na_ctime,
3469 1.1 dholland newvp->v_type != VDIR ? NULL :
3470 1.1 dholland &dctime);
3471 1.1 dholland }
3472 1.1 dholland if (unlocknewvp)
3473 1.1 dholland vput(newvp);
3474 1.1 dholland else
3475 1.1 dholland vrele(newvp);
3476 1.1 dholland newvp = NULLVP;
3477 1.1 dholland }
3478 1.1 dholland }
3479 1.1 dholland } else if (nfhp != NULL) {
3480 1.1 dholland FREE((caddr_t)nfhp, M_NFSFH);
3481 1.1 dholland }
3482 1.1 dholland NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3483 1.1 dholland more_dirs = fxdr_unsigned(int, *tl);
3484 1.1 dholland }
3485 1.1 dholland /*
3486 1.1 dholland * If at end of rpc data, get the eof boolean
3487 1.1 dholland */
3488 1.1 dholland if (!more_dirs) {
3489 1.1 dholland NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3490 1.1 dholland eof = fxdr_unsigned(int, *tl);
3491 1.1 dholland if (tryformoredirs)
3492 1.1 dholland more_dirs = !eof;
3493 1.1 dholland if (nd->nd_flag & ND_NFSV4) {
3494 1.1 dholland error = nfscl_postop_attr(nd, nap, attrflagp,
3495 1.1 dholland stuff);
3496 1.1 dholland if (error)
3497 1.1 dholland goto nfsmout;
3498 1.1 dholland }
3499 1.1 dholland }
3500 1.1 dholland mbuf_freem(nd->nd_mrep);
3501 1.1 dholland nd->nd_mrep = NULL;
3502 1.1 dholland }
3503 1.1 dholland /*
3504 1.1 dholland * Fill last record, iff any, out to a multiple of DIRBLKSIZ
3505 1.1 dholland * by increasing d_reclen for the last record.
3506 1.1 dholland */
3507 1.1 dholland if (blksiz > 0) {
3508 1.1 dholland left = DIRBLKSIZ - blksiz;
3509 1.1 dholland dp->d_reclen += left;
3510 1.1 dholland uio_iov_base_add(uiop, left);
3511 1.1 dholland uio_iov_len_add(uiop, -(left));
3512 1.1 dholland uio_uio_resid_add(uiop, -(left));
3513 1.1 dholland uiop->uio_offset += left;
3514 1.1 dholland }
3515 1.1 dholland
3516 1.1 dholland /*
3517 1.1 dholland * If returning no data, assume end of file.
3518 1.1 dholland * If not bigenough, return not end of file, since you aren't
3519 1.1 dholland * returning all the data
3520 1.1 dholland * Otherwise, return the eof flag from the server.
3521 1.1 dholland */
3522 1.1 dholland if (eofp != NULL) {
3523 1.1 dholland if (tresid == uio_uio_resid(uiop))
3524 1.1 dholland *eofp = 1;
3525 1.1 dholland else if (!bigenough)
3526 1.1 dholland *eofp = 0;
3527 1.1 dholland else
3528 1.1 dholland *eofp = eof;
3529 1.1 dholland }
3530 1.1 dholland
3531 1.1 dholland /*
3532 1.1 dholland * Add extra empty records to any remaining DIRBLKSIZ chunks.
3533 1.1 dholland */
3534 1.1 dholland while (uio_uio_resid(uiop) > 0 && uio_uio_resid(uiop) != tresid) {
3535 1.1 dholland dp = (struct dirent *)uio_iov_base(uiop);
3536 1.1 dholland dp->d_type = DT_UNKNOWN;
3537 1.1 dholland dp->d_fileno = 0;
3538 1.1 dholland dp->d_namlen = 0;
3539 1.1 dholland dp->d_name[0] = '\0';
3540 1.1 dholland tl = (u_int32_t *)&dp->d_name[4];
3541 1.1 dholland *tl++ = cookie.lval[0];
3542 1.1 dholland *tl = cookie.lval[1];
3543 1.1 dholland dp->d_reclen = DIRBLKSIZ;
3544 1.1 dholland uio_iov_base_add(uiop, DIRBLKSIZ);
3545 1.1 dholland uio_iov_len_add(uiop, -(DIRBLKSIZ));
3546 1.1 dholland uio_uio_resid_add(uiop, -(DIRBLKSIZ));
3547 1.1 dholland uiop->uio_offset += DIRBLKSIZ;
3548 1.1 dholland }
3549 1.1 dholland
3550 1.1 dholland nfsmout:
3551 1.1 dholland if (nd->nd_mrep != NULL)
3552 1.1 dholland mbuf_freem(nd->nd_mrep);
3553 1.1 dholland return (error);
3554 1.1 dholland }
3555 1.1 dholland #endif /* !APPLE */
3556 1.1 dholland
3557 1.1 dholland /*
3558 1.1 dholland * Nfs commit rpc
3559 1.1 dholland */
3560 1.1 dholland APPLESTATIC int
3561 1.1 dholland nfsrpc_commit(vnode_t vp, u_quad_t offset, int cnt, struct ucred *cred,
3562 1.1 dholland NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, void *stuff)
3563 1.1 dholland {
3564 1.1 dholland u_int32_t *tl;
3565 1.1 dholland struct nfsrv_descript nfsd, *nd = &nfsd;
3566 1.1 dholland nfsattrbit_t attrbits;
3567 1.1 dholland int error;
3568 1.1 dholland struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
3569 1.1 dholland
3570 1.1 dholland *attrflagp = 0;
3571 1.1 dholland NFSCL_REQSTART(nd, NFSPROC_COMMIT, vp);
3572 1.1 dholland NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
3573 1.1 dholland txdr_hyper(offset, tl);
3574 1.1 dholland tl += 2;
3575 1.1 dholland *tl = txdr_unsigned(cnt);
3576 1.1 dholland if (nd->nd_flag & ND_NFSV4) {
3577 1.1 dholland /*
3578 1.1 dholland * And do a Getattr op.
3579 1.1 dholland */
3580 1.1 dholland NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
3581 1.1 dholland *tl = txdr_unsigned(NFSV4OP_GETATTR);
3582 1.1 dholland NFSGETATTR_ATTRBIT(&attrbits);
3583 1.1 dholland (void) nfsrv_putattrbit(nd, &attrbits);
3584 1.1 dholland }
3585 1.1 dholland error = nfscl_request(nd, vp, p, cred, stuff);
3586 1.1 dholland if (error)
3587 1.1 dholland return (error);
3588 1.1 dholland error = nfscl_wcc_data(nd, vp, nap, attrflagp, NULL, stuff);
3589 1.1 dholland if (!error && !nd->nd_repstat) {
3590 1.1 dholland NFSM_DISSECT(tl, u_int32_t *, NFSX_VERF);
3591 1.1 dholland NFSLOCKMNT(nmp);
3592 1.1 dholland if (NFSBCMP(nmp->nm_verf, tl, NFSX_VERF)) {
3593 1.1 dholland NFSBCOPY(tl, nmp->nm_verf, NFSX_VERF);
3594 1.1 dholland nd->nd_repstat = NFSERR_STALEWRITEVERF;
3595 1.1 dholland }
3596 1.1 dholland NFSUNLOCKMNT(nmp);
3597 1.1 dholland if (nd->nd_flag & ND_NFSV4)
3598 1.1 dholland error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
3599 1.1 dholland }
3600 1.1 dholland nfsmout:
3601 1.1 dholland if (!error && nd->nd_repstat)
3602 1.1 dholland error = nd->nd_repstat;
3603 1.1 dholland mbuf_freem(nd->nd_mrep);
3604 1.1 dholland return (error);
3605 1.1 dholland }
3606 1.1 dholland
3607 1.1 dholland /*
3608 1.1 dholland * NFS byte range lock rpc.
3609 1.1 dholland * (Mostly just calls one of the three lower level RPC routines.)
3610 1.1 dholland */
3611 1.1 dholland APPLESTATIC int
3612 1.1 dholland nfsrpc_advlock(vnode_t vp, off_t size, int op, struct flock *fl,
3613 1.1 dholland int reclaim, struct ucred *cred, NFSPROC_T *p, void *id, int flags)
3614 1.1 dholland {
3615 1.1 dholland struct nfscllockowner *lp;
3616 1.1 dholland struct nfsclclient *clp;
3617 1.1 dholland struct nfsfh *nfhp;
3618 1.1 dholland struct nfsrv_descript nfsd, *nd = &nfsd;
3619 1.1 dholland struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
3620 1.1 dholland u_int64_t off, len;
3621 1.1 dholland off_t start, end;
3622 1.1 dholland u_int32_t clidrev = 0;
3623 1.1 dholland int error = 0, newone = 0, expireret = 0, retrycnt, donelocally;
3624 1.1 dholland int callcnt, dorpc;
3625 1.1 dholland
3626 1.1 dholland /*
3627 1.1 dholland * Convert the flock structure into a start and end and do POSIX
3628 1.1 dholland * bounds checking.
3629 1.1 dholland */
3630 1.1 dholland switch (fl->l_whence) {
3631 1.1 dholland case SEEK_SET:
3632 1.1 dholland case SEEK_CUR:
3633 1.1 dholland /*
3634 1.1 dholland * Caller is responsible for adding any necessary offset
3635 1.1 dholland * when SEEK_CUR is used.
3636 1.1 dholland */
3637 1.1 dholland start = fl->l_start;
3638 1.1 dholland off = fl->l_start;
3639 1.1 dholland break;
3640 1.1 dholland case SEEK_END:
3641 1.1 dholland start = size + fl->l_start;
3642 1.1 dholland off = size + fl->l_start;
3643 1.1 dholland break;
3644 1.1 dholland default:
3645 1.1 dholland return (EINVAL);
3646 1.1 dholland };
3647 1.1 dholland if (start < 0)
3648 1.1 dholland return (EINVAL);
3649 1.1 dholland if (fl->l_len != 0) {
3650 1.1 dholland end = start + fl->l_len - 1;
3651 1.1 dholland if (end < start)
3652 1.1 dholland return (EINVAL);
3653 1.1 dholland }
3654 1.1 dholland
3655 1.1 dholland len = fl->l_len;
3656 1.1 dholland if (len == 0)
3657 1.1 dholland len = NFS64BITSSET;
3658 1.1 dholland retrycnt = 0;
3659 1.1 dholland do {
3660 1.1 dholland nd->nd_repstat = 0;
3661 1.1 dholland if (op == F_GETLK) {
3662 1.1 dholland error = nfscl_getcl(vnode_mount(vp), cred, p, 1, &clp);
3663 1.1 dholland if (error)
3664 1.1 dholland return (error);
3665 1.1 dholland error = nfscl_lockt(vp, clp, off, len, fl, p, id, flags);
3666 1.1 dholland if (!error) {
3667 1.1 dholland clidrev = clp->nfsc_clientidrev;
3668 1.1 dholland error = nfsrpc_lockt(nd, vp, clp, off, len, fl, cred,
3669 1.1 dholland p, id, flags);
3670 1.1 dholland } else if (error == -1) {
3671 1.1 dholland error = 0;
3672 1.1 dholland }
3673 1.1 dholland nfscl_clientrelease(clp);
3674 1.1 dholland } else if (op == F_UNLCK && fl->l_type == F_UNLCK) {
3675 1.1 dholland /*
3676 1.1 dholland * We must loop around for all lockowner cases.
3677 1.1 dholland */
3678 1.1 dholland callcnt = 0;
3679 1.1 dholland error = nfscl_getcl(vnode_mount(vp), cred, p, 1, &clp);
3680 1.1 dholland if (error)
3681 1.1 dholland return (error);
3682 1.1 dholland do {
3683 1.1 dholland error = nfscl_relbytelock(vp, off, len, cred, p, callcnt,
3684 1.1 dholland clp, id, flags, &lp, &dorpc);
3685 1.1 dholland /*
3686 1.1 dholland * If it returns a NULL lp, we're done.
3687 1.1 dholland */
3688 1.1 dholland if (lp == NULL) {
3689 1.1 dholland if (callcnt == 0)
3690 1.1 dholland nfscl_clientrelease(clp);
3691 1.1 dholland else
3692 1.1 dholland nfscl_releasealllocks(clp, vp, p, id, flags);
3693 1.1 dholland return (error);
3694 1.1 dholland }
3695 1.1 dholland if (nmp->nm_clp != NULL)
3696 1.1 dholland clidrev = nmp->nm_clp->nfsc_clientidrev;
3697 1.1 dholland else
3698 1.1 dholland clidrev = 0;
3699 1.1 dholland /*
3700 1.1 dholland * If the server doesn't support Posix lock semantics,
3701 1.1 dholland * only allow locks on the entire file, since it won't
3702 1.1 dholland * handle overlapping byte ranges.
3703 1.1 dholland * There might still be a problem when a lock
3704 1.1 dholland * upgrade/downgrade (read<->write) occurs, since the
3705 1.1 dholland * server "might" expect an unlock first?
3706 1.1 dholland */
3707 1.1 dholland if (dorpc && (lp->nfsl_open->nfso_posixlock ||
3708 1.1 dholland (off == 0 && len == NFS64BITSSET))) {
3709 1.1 dholland /*
3710 1.1 dholland * Since the lock records will go away, we must
3711 1.1 dholland * wait for grace and delay here.
3712 1.1 dholland */
3713 1.1 dholland do {
3714 1.1 dholland error = nfsrpc_locku(nd, nmp, lp, off, len,
3715 1.1 dholland NFSV4LOCKT_READ, cred, p, 0);
3716 1.1 dholland if ((nd->nd_repstat == NFSERR_GRACE ||
3717 1.1 dholland nd->nd_repstat == NFSERR_DELAY) &&
3718 1.1 dholland error == 0)
3719 1.1 dholland (void) nfs_catnap(PZERO, (int)nd->nd_repstat,
3720 1.1 dholland "nfs_advlock");
3721 1.1 dholland } while ((nd->nd_repstat == NFSERR_GRACE ||
3722 1.1 dholland nd->nd_repstat == NFSERR_DELAY) && error == 0);
3723 1.1 dholland }
3724 1.1 dholland callcnt++;
3725 1.1 dholland } while (error == 0 && nd->nd_repstat == 0);
3726 1.1 dholland nfscl_releasealllocks(clp, vp, p, id, flags);
3727 1.1 dholland } else if (op == F_SETLK) {
3728 1.1 dholland error = nfscl_getbytelock(vp, off, len, fl->l_type, cred, p,
3729 1.1 dholland NULL, 0, id, flags, NULL, NULL, &lp, &newone, &donelocally);
3730 1.1 dholland if (error || donelocally) {
3731 1.1 dholland return (error);
3732 1.1 dholland }
3733 1.1 dholland if (nmp->nm_clp != NULL)
3734 1.1 dholland clidrev = nmp->nm_clp->nfsc_clientidrev;
3735 1.1 dholland else
3736 1.1 dholland clidrev = 0;
3737 1.1 dholland nfhp = VTONFS(vp)->n_fhp;
3738 1.1 dholland if (!lp->nfsl_open->nfso_posixlock &&
3739 1.1 dholland (off != 0 || len != NFS64BITSSET)) {
3740 1.1 dholland error = EINVAL;
3741 1.1 dholland } else {
3742 1.1 dholland error = nfsrpc_lock(nd, nmp, vp, nfhp->nfh_fh,
3743 1.1 dholland nfhp->nfh_len, lp, newone, reclaim, off,
3744 1.1 dholland len, fl->l_type, cred, p, 0);
3745 1.1 dholland }
3746 1.1 dholland if (!error)
3747 1.1 dholland error = nd->nd_repstat;
3748 1.1 dholland nfscl_lockrelease(lp, error, newone);
3749 1.1 dholland } else {
3750 1.1 dholland error = EINVAL;
3751 1.1 dholland }
3752 1.1 dholland if (!error)
3753 1.1 dholland error = nd->nd_repstat;
3754 1.1 dholland if (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
3755 1.1 dholland error == NFSERR_STALEDONTRECOVER ||
3756 1.1 dholland error == NFSERR_STALECLIENTID || error == NFSERR_DELAY ||
3757 1.1 dholland error == NFSERR_BADSESSION) {
3758 1.1 dholland (void) nfs_catnap(PZERO, error, "nfs_advlock");
3759 1.1 dholland } else if ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID)
3760 1.1 dholland && clidrev != 0) {
3761 1.1 dholland expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
3762 1.1 dholland retrycnt++;
3763 1.1 dholland }
3764 1.1 dholland } while (error == NFSERR_GRACE ||
3765 1.1 dholland error == NFSERR_STALECLIENTID || error == NFSERR_DELAY ||
3766 1.1 dholland error == NFSERR_STALEDONTRECOVER || error == NFSERR_STALESTATEID ||
3767 1.1 dholland error == NFSERR_BADSESSION ||
3768 1.1 dholland ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
3769 1.1 dholland expireret == 0 && clidrev != 0 && retrycnt < 4));
3770 1.1 dholland if (error && retrycnt >= 4)
3771 1.1 dholland error = EIO;
3772 1.1 dholland return (error);
3773 1.1 dholland }
3774 1.1 dholland
3775 1.1 dholland /*
3776 1.1 dholland * The lower level routine for the LockT case.
3777 1.1 dholland */
3778 1.1 dholland APPLESTATIC int
3779 1.1 dholland nfsrpc_lockt(struct nfsrv_descript *nd, vnode_t vp,
3780 1.1 dholland struct nfsclclient *clp, u_int64_t off, u_int64_t len, struct flock *fl,
3781 1.1 dholland struct ucred *cred, NFSPROC_T *p, void *id, int flags)
3782 1.1 dholland {
3783 1.1 dholland u_int32_t *tl;
3784 1.1 dholland int error, type, size;
3785 1.1 dholland uint8_t own[NFSV4CL_LOCKNAMELEN + NFSX_V4FHMAX];
3786 1.1 dholland struct nfsnode *np;
3787 1.1 dholland struct nfsmount *nmp;
3788 1.1 dholland
3789 1.1 dholland nmp = VFSTONFS(vp->v_mount);
3790 1.1 dholland NFSCL_REQSTART(nd, NFSPROC_LOCKT, vp);
3791 1.1 dholland NFSM_BUILD(tl, u_int32_t *, 7 * NFSX_UNSIGNED);
3792 1.1 dholland if (fl->l_type == F_RDLCK)
3793 1.1 dholland *tl++ = txdr_unsigned(NFSV4LOCKT_READ);
3794 1.1 dholland else
3795 1.1 dholland *tl++ = txdr_unsigned(NFSV4LOCKT_WRITE);
3796 1.1 dholland txdr_hyper(off, tl);
3797 1.1 dholland tl += 2;
3798 1.1 dholland txdr_hyper(len, tl);
3799 1.1 dholland tl += 2;
3800 1.1 dholland *tl++ = NFSMNT_MDSSESSION(nmp)->nfsess_clientid.lval[0];
3801 1.1 dholland *tl = NFSMNT_MDSSESSION(nmp)->nfsess_clientid.lval[1];
3802 1.1 dholland nfscl_filllockowner(id, own, flags);
3803 1.1 dholland np = VTONFS(vp);
3804 1.1 dholland NFSBCOPY(np->n_fhp->nfh_fh, &own[NFSV4CL_LOCKNAMELEN],
3805 1.1 dholland np->n_fhp->nfh_len);
3806 1.1 dholland (void)nfsm_strtom(nd, own, NFSV4CL_LOCKNAMELEN + np->n_fhp->nfh_len);
3807 1.1 dholland error = nfscl_request(nd, vp, p, cred, NULL);
3808 1.1 dholland if (error)
3809 1.1 dholland return (error);
3810 1.1 dholland if (nd->nd_repstat == 0) {
3811 1.1 dholland fl->l_type = F_UNLCK;
3812 1.1 dholland } else if (nd->nd_repstat == NFSERR_DENIED) {
3813 1.1 dholland nd->nd_repstat = 0;
3814 1.1 dholland fl->l_whence = SEEK_SET;
3815 1.1 dholland NFSM_DISSECT(tl, u_int32_t *, 8 * NFSX_UNSIGNED);
3816 1.1 dholland fl->l_start = fxdr_hyper(tl);
3817 1.1 dholland tl += 2;
3818 1.1 dholland len = fxdr_hyper(tl);
3819 1.1 dholland tl += 2;
3820 1.1 dholland if (len == NFS64BITSSET)
3821 1.1 dholland fl->l_len = 0;
3822 1.1 dholland else
3823 1.1 dholland fl->l_len = len;
3824 1.1 dholland type = fxdr_unsigned(int, *tl++);
3825 1.1 dholland if (type == NFSV4LOCKT_WRITE)
3826 1.1 dholland fl->l_type = F_WRLCK;
3827 1.1 dholland else
3828 1.1 dholland fl->l_type = F_RDLCK;
3829 1.1 dholland /*
3830 1.1 dholland * XXX For now, I have no idea what to do with the
3831 1.1 dholland * conflicting lock_owner, so I'll just set the pid == 0
3832 1.1 dholland * and skip over the lock_owner.
3833 1.1 dholland */
3834 1.1 dholland fl->l_pid = (pid_t)0;
3835 1.1 dholland tl += 2;
3836 1.1 dholland size = fxdr_unsigned(int, *tl);
3837 1.1 dholland if (size < 0 || size > NFSV4_OPAQUELIMIT)
3838 1.1 dholland error = EBADRPC;
3839 1.1 dholland if (!error)
3840 1.1 dholland error = nfsm_advance(nd, NFSM_RNDUP(size), -1);
3841 1.1 dholland } else if (nd->nd_repstat == NFSERR_STALECLIENTID ||
3842 1.1 dholland nd->nd_repstat == NFSERR_BADSESSION)
3843 1.1 dholland nfscl_initiate_recovery(clp);
3844 1.1 dholland nfsmout:
3845 1.1 dholland mbuf_freem(nd->nd_mrep);
3846 1.1 dholland return (error);
3847 1.1 dholland }
3848 1.1 dholland
3849 1.1 dholland /*
3850 1.1 dholland * Lower level function that performs the LockU RPC.
3851 1.1 dholland */
3852 1.1 dholland static int
3853 1.1 dholland nfsrpc_locku(struct nfsrv_descript *nd, struct nfsmount *nmp,
3854 1.1 dholland struct nfscllockowner *lp, u_int64_t off, u_int64_t len,
3855 1.1 dholland u_int32_t type, struct ucred *cred, NFSPROC_T *p, int syscred)
3856 1.1 dholland {
3857 1.1 dholland u_int32_t *tl;
3858 1.1 dholland int error;
3859 1.1 dholland
3860 1.1 dholland nfscl_reqstart(nd, NFSPROC_LOCKU, nmp, lp->nfsl_open->nfso_fh,
3861 1.1 dholland lp->nfsl_open->nfso_fhlen, NULL, NULL);
3862 1.1 dholland NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID + 6 * NFSX_UNSIGNED);
3863 1.1 dholland *tl++ = txdr_unsigned(type);
3864 1.1 dholland *tl = txdr_unsigned(lp->nfsl_seqid);
3865 1.1 dholland if (nfstest_outofseq &&
3866 1.1 dholland (arc4random() % nfstest_outofseq) == 0)
3867 1.1 dholland *tl = txdr_unsigned(lp->nfsl_seqid + 1);
3868 1.1 dholland tl++;
3869 1.1 dholland if (NFSHASNFSV4N(nmp))
3870 1.1 dholland *tl++ = 0;
3871 1.1 dholland else
3872 1.1 dholland *tl++ = lp->nfsl_stateid.seqid;
3873 1.1 dholland *tl++ = lp->nfsl_stateid.other[0];
3874 1.1 dholland *tl++ = lp->nfsl_stateid.other[1];
3875 1.1 dholland *tl++ = lp->nfsl_stateid.other[2];
3876 1.1 dholland txdr_hyper(off, tl);
3877 1.1 dholland tl += 2;
3878 1.1 dholland txdr_hyper(len, tl);
3879 1.1 dholland if (syscred)
3880 1.1 dholland nd->nd_flag |= ND_USEGSSNAME;
3881 1.1 dholland error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
3882 1.1 dholland NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
3883 1.1 dholland NFSCL_INCRSEQID(lp->nfsl_seqid, nd);
3884 1.1 dholland if (error)
3885 1.1 dholland return (error);
3886 1.1 dholland if (nd->nd_repstat == 0) {
3887 1.1 dholland NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID);
3888 1.1 dholland lp->nfsl_stateid.seqid = *tl++;
3889 1.1 dholland lp->nfsl_stateid.other[0] = *tl++;
3890 1.1 dholland lp->nfsl_stateid.other[1] = *tl++;
3891 1.1 dholland lp->nfsl_stateid.other[2] = *tl;
3892 1.1 dholland } else if (nd->nd_repstat == NFSERR_STALESTATEID ||
3893 1.1 dholland nd->nd_repstat == NFSERR_BADSESSION)
3894 1.1 dholland nfscl_initiate_recovery(lp->nfsl_open->nfso_own->nfsow_clp);
3895 1.1 dholland nfsmout:
3896 1.1 dholland mbuf_freem(nd->nd_mrep);
3897 1.1 dholland return (error);
3898 1.1 dholland }
3899 1.1 dholland
3900 1.1 dholland /*
3901 1.1 dholland * The actual Lock RPC.
3902 1.1 dholland */
3903 1.1 dholland APPLESTATIC int
3904 1.1 dholland nfsrpc_lock(struct nfsrv_descript *nd, struct nfsmount *nmp, vnode_t vp,
3905 1.1 dholland u_int8_t *nfhp, int fhlen, struct nfscllockowner *lp, int newone,
3906 1.1 dholland int reclaim, u_int64_t off, u_int64_t len, short type, struct ucred *cred,
3907 1.1 dholland NFSPROC_T *p, int syscred)
3908 1.1 dholland {
3909 1.1 dholland u_int32_t *tl;
3910 1.1 dholland int error, size;
3911 1.1 dholland uint8_t own[NFSV4CL_LOCKNAMELEN + NFSX_V4FHMAX];
3912 1.1 dholland
3913 1.1 dholland nfscl_reqstart(nd, NFSPROC_LOCK, nmp, nfhp, fhlen, NULL, NULL);
3914 1.1 dholland NFSM_BUILD(tl, u_int32_t *, 7 * NFSX_UNSIGNED);
3915 1.1 dholland if (type == F_RDLCK)
3916 1.1 dholland *tl++ = txdr_unsigned(NFSV4LOCKT_READ);
3917 1.1 dholland else
3918 1.1 dholland *tl++ = txdr_unsigned(NFSV4LOCKT_WRITE);
3919 1.1 dholland *tl++ = txdr_unsigned(reclaim);
3920 1.1 dholland txdr_hyper(off, tl);
3921 1.1 dholland tl += 2;
3922 1.1 dholland txdr_hyper(len, tl);
3923 1.1 dholland tl += 2;
3924 1.1 dholland if (newone) {
3925 1.1 dholland *tl = newnfs_true;
3926 1.1 dholland NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID +
3927 1.1 dholland 2 * NFSX_UNSIGNED + NFSX_HYPER);
3928 1.1 dholland *tl++ = txdr_unsigned(lp->nfsl_open->nfso_own->nfsow_seqid);
3929 1.1 dholland if (NFSHASNFSV4N(nmp))
3930 1.1 dholland *tl++ = 0;
3931 1.1 dholland else
3932 1.1 dholland *tl++ = lp->nfsl_open->nfso_stateid.seqid;
3933 1.1 dholland *tl++ = lp->nfsl_open->nfso_stateid.other[0];
3934 1.1 dholland *tl++ = lp->nfsl_open->nfso_stateid.other[1];
3935 1.1 dholland *tl++ = lp->nfsl_open->nfso_stateid.other[2];
3936 1.1 dholland *tl++ = txdr_unsigned(lp->nfsl_seqid);
3937 1.1 dholland *tl++ = NFSMNT_MDSSESSION(nmp)->nfsess_clientid.lval[0];
3938 1.1 dholland *tl = NFSMNT_MDSSESSION(nmp)->nfsess_clientid.lval[1];
3939 1.1 dholland NFSBCOPY(lp->nfsl_owner, own, NFSV4CL_LOCKNAMELEN);
3940 1.1 dholland NFSBCOPY(nfhp, &own[NFSV4CL_LOCKNAMELEN], fhlen);
3941 1.1 dholland (void)nfsm_strtom(nd, own, NFSV4CL_LOCKNAMELEN + fhlen);
3942 1.1 dholland } else {
3943 1.1 dholland *tl = newnfs_false;
3944 1.1 dholland NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID + NFSX_UNSIGNED);
3945 1.1 dholland if (NFSHASNFSV4N(nmp))
3946 1.1 dholland *tl++ = 0;
3947 1.1 dholland else
3948 1.1 dholland *tl++ = lp->nfsl_stateid.seqid;
3949 1.1 dholland *tl++ = lp->nfsl_stateid.other[0];
3950 1.1 dholland *tl++ = lp->nfsl_stateid.other[1];
3951 1.1 dholland *tl++ = lp->nfsl_stateid.other[2];
3952 1.1 dholland *tl = txdr_unsigned(lp->nfsl_seqid);
3953 1.1 dholland if (nfstest_outofseq &&
3954 1.1 dholland (arc4random() % nfstest_outofseq) == 0)
3955 1.1 dholland *tl = txdr_unsigned(lp->nfsl_seqid + 1);
3956 1.1 dholland }
3957 1.1 dholland if (syscred)
3958 1.1 dholland nd->nd_flag |= ND_USEGSSNAME;
3959 1.1 dholland error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, vp, p, cred,
3960 1.1 dholland NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
3961 1.1 dholland if (error)
3962 1.1 dholland return (error);
3963 1.1 dholland if (newone)
3964 1.1 dholland NFSCL_INCRSEQID(lp->nfsl_open->nfso_own->nfsow_seqid, nd);
3965 1.1 dholland NFSCL_INCRSEQID(lp->nfsl_seqid, nd);
3966 1.1 dholland if (nd->nd_repstat == 0) {
3967 1.1 dholland NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID);
3968 1.1 dholland lp->nfsl_stateid.seqid = *tl++;
3969 1.1 dholland lp->nfsl_stateid.other[0] = *tl++;
3970 1.1 dholland lp->nfsl_stateid.other[1] = *tl++;
3971 1.1 dholland lp->nfsl_stateid.other[2] = *tl;
3972 1.1 dholland } else if (nd->nd_repstat == NFSERR_DENIED) {
3973 1.1 dholland NFSM_DISSECT(tl, u_int32_t *, 8 * NFSX_UNSIGNED);
3974 1.1 dholland size = fxdr_unsigned(int, *(tl + 7));
3975 1.1 dholland if (size < 0 || size > NFSV4_OPAQUELIMIT)
3976 1.1 dholland error = EBADRPC;
3977 1.1 dholland if (!error)
3978 1.1 dholland error = nfsm_advance(nd, NFSM_RNDUP(size), -1);
3979 1.1 dholland } else if (nd->nd_repstat == NFSERR_STALESTATEID ||
3980 1.1 dholland nd->nd_repstat == NFSERR_BADSESSION)
3981 1.1 dholland nfscl_initiate_recovery(lp->nfsl_open->nfso_own->nfsow_clp);
3982 1.1 dholland nfsmout:
3983 1.1 dholland mbuf_freem(nd->nd_mrep);
3984 1.1 dholland return (error);
3985 1.1 dholland }
3986 1.1 dholland
3987 1.1 dholland /*
3988 1.1 dholland * nfs statfs rpc
3989 1.1 dholland * (always called with the vp for the mount point)
3990 1.1 dholland */
3991 1.1 dholland APPLESTATIC int
3992 1.1 dholland nfsrpc_statfs(vnode_t vp, struct nfsstatfs *sbp, struct nfsfsinfo *fsp,
3993 1.1 dholland struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp,
3994 1.1 dholland void *stuff)
3995 1.1 dholland {
3996 1.1 dholland u_int32_t *tl = NULL;
3997 1.1 dholland struct nfsrv_descript nfsd, *nd = &nfsd;
3998 1.1 dholland struct nfsmount *nmp;
3999 1.1 dholland nfsattrbit_t attrbits;
4000 1.1 dholland int error;
4001 1.1 dholland
4002 1.1 dholland *attrflagp = 0;
4003 1.1 dholland nmp = VFSTONFS(vnode_mount(vp));
4004 1.1 dholland if (NFSHASNFSV4(nmp)) {
4005 1.1 dholland /*
4006 1.1 dholland * For V4, you actually do a getattr.
4007 1.1 dholland */
4008 1.1 dholland NFSCL_REQSTART(nd, NFSPROC_GETATTR, vp);
4009 1.1 dholland NFSSTATFS_GETATTRBIT(&attrbits);
4010 1.1 dholland (void) nfsrv_putattrbit(nd, &attrbits);
4011 1.1 dholland nd->nd_flag |= ND_USEGSSNAME;
4012 1.1 dholland error = nfscl_request(nd, vp, p, cred, stuff);
4013 1.1 dholland if (error)
4014 1.1 dholland return (error);
4015 1.1 dholland if (nd->nd_repstat == 0) {
4016 1.1 dholland error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0,
4017 1.1 dholland NULL, NULL, sbp, fsp, NULL, 0, NULL, NULL, NULL, p,
4018 1.1 dholland cred);
4019 1.1 dholland if (!error) {
4020 1.1 dholland nmp->nm_fsid[0] = nap->na_filesid[0];
4021 1.1 dholland nmp->nm_fsid[1] = nap->na_filesid[1];
4022 1.1 dholland NFSSETHASSETFSID(nmp);
4023 1.1 dholland *attrflagp = 1;
4024 1.1 dholland }
4025 1.1 dholland } else {
4026 1.1 dholland error = nd->nd_repstat;
4027 1.1 dholland }
4028 1.1 dholland if (error)
4029 1.1 dholland goto nfsmout;
4030 1.1 dholland } else {
4031 1.1 dholland NFSCL_REQSTART(nd, NFSPROC_FSSTAT, vp);
4032 1.1 dholland error = nfscl_request(nd, vp, p, cred, stuff);
4033 1.1 dholland if (error)
4034 1.1 dholland return (error);
4035 1.1 dholland if (nd->nd_flag & ND_NFSV3) {
4036 1.1 dholland error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
4037 1.1 dholland if (error)
4038 1.1 dholland goto nfsmout;
4039 1.1 dholland }
4040 1.1 dholland if (nd->nd_repstat) {
4041 1.1 dholland error = nd->nd_repstat;
4042 1.1 dholland goto nfsmout;
4043 1.1 dholland }
4044 1.1 dholland NFSM_DISSECT(tl, u_int32_t *,
4045 1.1 dholland NFSX_STATFS(nd->nd_flag & ND_NFSV3));
4046 1.1 dholland }
4047 1.1 dholland if (NFSHASNFSV3(nmp)) {
4048 1.1 dholland sbp->sf_tbytes = fxdr_hyper(tl); tl += 2;
4049 1.1 dholland sbp->sf_fbytes = fxdr_hyper(tl); tl += 2;
4050 1.1 dholland sbp->sf_abytes = fxdr_hyper(tl); tl += 2;
4051 1.1 dholland sbp->sf_tfiles = fxdr_hyper(tl); tl += 2;
4052 1.1 dholland sbp->sf_ffiles = fxdr_hyper(tl); tl += 2;
4053 1.1 dholland sbp->sf_afiles = fxdr_hyper(tl); tl += 2;
4054 1.1 dholland sbp->sf_invarsec = fxdr_unsigned(u_int32_t, *tl);
4055 1.1 dholland } else if (NFSHASNFSV4(nmp) == 0) {
4056 1.1 dholland sbp->sf_tsize = fxdr_unsigned(u_int32_t, *tl++);
4057 1.1 dholland sbp->sf_bsize = fxdr_unsigned(u_int32_t, *tl++);
4058 1.1 dholland sbp->sf_blocks = fxdr_unsigned(u_int32_t, *tl++);
4059 1.1 dholland sbp->sf_bfree = fxdr_unsigned(u_int32_t, *tl++);
4060 1.1 dholland sbp->sf_bavail = fxdr_unsigned(u_int32_t, *tl);
4061 1.1 dholland }
4062 1.1 dholland nfsmout:
4063 1.1 dholland mbuf_freem(nd->nd_mrep);
4064 1.1 dholland return (error);
4065 1.1 dholland }
4066 1.1 dholland
4067 1.1 dholland /*
4068 1.1 dholland * nfs pathconf rpc
4069 1.1 dholland */
4070 1.1 dholland APPLESTATIC int
4071 1.1 dholland nfsrpc_pathconf(vnode_t vp, struct nfsv3_pathconf *pc,
4072 1.1 dholland struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp,
4073 1.1 dholland void *stuff)
4074 1.1 dholland {
4075 1.1 dholland struct nfsrv_descript nfsd, *nd = &nfsd;
4076 1.1 dholland struct nfsmount *nmp;
4077 1.1 dholland u_int32_t *tl;
4078 1.1 dholland nfsattrbit_t attrbits;
4079 1.1 dholland int error;
4080 1.1 dholland
4081 1.1 dholland *attrflagp = 0;
4082 1.1 dholland nmp = VFSTONFS(vnode_mount(vp));
4083 1.1 dholland if (NFSHASNFSV4(nmp)) {
4084 1.1 dholland /*
4085 1.1 dholland * For V4, you actually do a getattr.
4086 1.1 dholland */
4087 1.1 dholland NFSCL_REQSTART(nd, NFSPROC_GETATTR, vp);
4088 1.1 dholland NFSPATHCONF_GETATTRBIT(&attrbits);
4089 1.1 dholland (void) nfsrv_putattrbit(nd, &attrbits);
4090 1.1 dholland nd->nd_flag |= ND_USEGSSNAME;
4091 1.1 dholland error = nfscl_request(nd, vp, p, cred, stuff);
4092 1.1 dholland if (error)
4093 1.1 dholland return (error);
4094 1.1 dholland if (nd->nd_repstat == 0) {
4095 1.1 dholland error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0,
4096 1.1 dholland pc, NULL, NULL, NULL, NULL, 0, NULL, NULL, NULL, p,
4097 1.1 dholland cred);
4098 1.1 dholland if (!error)
4099 1.1 dholland *attrflagp = 1;
4100 1.1 dholland } else {
4101 1.1 dholland error = nd->nd_repstat;
4102 1.1 dholland }
4103 1.1 dholland } else {
4104 1.1 dholland NFSCL_REQSTART(nd, NFSPROC_PATHCONF, vp);
4105 1.1 dholland error = nfscl_request(nd, vp, p, cred, stuff);
4106 1.1 dholland if (error)
4107 1.1 dholland return (error);
4108 1.1 dholland error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
4109 1.1 dholland if (nd->nd_repstat && !error)
4110 1.1 dholland error = nd->nd_repstat;
4111 1.1 dholland if (!error) {
4112 1.1 dholland NFSM_DISSECT(tl, u_int32_t *, NFSX_V3PATHCONF);
4113 1.1 dholland pc->pc_linkmax = fxdr_unsigned(u_int32_t, *tl++);
4114 1.1 dholland pc->pc_namemax = fxdr_unsigned(u_int32_t, *tl++);
4115 1.1 dholland pc->pc_notrunc = fxdr_unsigned(u_int32_t, *tl++);
4116 1.1 dholland pc->pc_chownrestricted =
4117 1.1 dholland fxdr_unsigned(u_int32_t, *tl++);
4118 1.1 dholland pc->pc_caseinsensitive =
4119 1.1 dholland fxdr_unsigned(u_int32_t, *tl++);
4120 1.1 dholland pc->pc_casepreserving = fxdr_unsigned(u_int32_t, *tl);
4121 1.1 dholland }
4122 1.1 dholland }
4123 1.1 dholland nfsmout:
4124 1.1 dholland mbuf_freem(nd->nd_mrep);
4125 1.1 dholland return (error);
4126 1.1 dholland }
4127 1.1 dholland
4128 1.1 dholland /*
4129 1.1 dholland * nfs version 3 fsinfo rpc call
4130 1.1 dholland */
4131 1.1 dholland APPLESTATIC int
4132 1.1 dholland nfsrpc_fsinfo(vnode_t vp, struct nfsfsinfo *fsp, struct ucred *cred,
4133 1.1 dholland NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, void *stuff)
4134 1.1 dholland {
4135 1.1 dholland u_int32_t *tl;
4136 1.1 dholland struct nfsrv_descript nfsd, *nd = &nfsd;
4137 1.1 dholland int error;
4138 1.1 dholland
4139 1.1 dholland *attrflagp = 0;
4140 1.1 dholland NFSCL_REQSTART(nd, NFSPROC_FSINFO, vp);
4141 1.1 dholland error = nfscl_request(nd, vp, p, cred, stuff);
4142 1.1 dholland if (error)
4143 1.1 dholland return (error);
4144 1.1 dholland error = nfscl_postop_attr(nd, nap, attrflagp, stuff);
4145 1.1 dholland if (nd->nd_repstat && !error)
4146 1.1 dholland error = nd->nd_repstat;
4147 1.1 dholland if (!error) {
4148 1.1 dholland NFSM_DISSECT(tl, u_int32_t *, NFSX_V3FSINFO);
4149 1.1 dholland fsp->fs_rtmax = fxdr_unsigned(u_int32_t, *tl++);
4150 1.1 dholland fsp->fs_rtpref = fxdr_unsigned(u_int32_t, *tl++);
4151 1.1 dholland fsp->fs_rtmult = fxdr_unsigned(u_int32_t, *tl++);
4152 1.1 dholland fsp->fs_wtmax = fxdr_unsigned(u_int32_t, *tl++);
4153 1.1 dholland fsp->fs_wtpref = fxdr_unsigned(u_int32_t, *tl++);
4154 1.1 dholland fsp->fs_wtmult = fxdr_unsigned(u_int32_t, *tl++);
4155 1.1 dholland fsp->fs_dtpref = fxdr_unsigned(u_int32_t, *tl++);
4156 1.1 dholland fsp->fs_maxfilesize = fxdr_hyper(tl);
4157 1.1 dholland tl += 2;
4158 1.1 dholland fxdr_nfsv3time(tl, &fsp->fs_timedelta);
4159 1.1 dholland tl += 2;
4160 1.1 dholland fsp->fs_properties = fxdr_unsigned(u_int32_t, *tl);
4161 1.1 dholland }
4162 1.1 dholland nfsmout:
4163 1.1 dholland mbuf_freem(nd->nd_mrep);
4164 1.1 dholland return (error);
4165 1.1 dholland }
4166 1.1 dholland
4167 1.1 dholland /*
4168 1.1 dholland * This function performs the Renew RPC.
4169 1.1 dholland */
4170 1.1 dholland APPLESTATIC int
4171 1.1 dholland nfsrpc_renew(struct nfsclclient *clp, struct nfsclds *dsp, struct ucred *cred,
4172 1.1 dholland NFSPROC_T *p)
4173 1.1 dholland {
4174 1.1 dholland u_int32_t *tl;
4175 1.1 dholland struct nfsrv_descript nfsd;
4176 1.1 dholland struct nfsrv_descript *nd = &nfsd;
4177 1.1 dholland struct nfsmount *nmp;
4178 1.1 dholland int error;
4179 1.1 dholland struct nfssockreq *nrp;
4180 1.1 dholland
4181 1.1 dholland nmp = clp->nfsc_nmp;
4182 1.1 dholland if (nmp == NULL)
4183 1.1 dholland return (0);
4184 1.1 dholland nfscl_reqstart(nd, NFSPROC_RENEW, nmp, NULL, 0, NULL,
4185 1.1 dholland &dsp->nfsclds_sess);
4186 1.1 dholland if (!NFSHASNFSV4N(nmp)) {
4187 1.1 dholland /* NFSv4.1 just uses a Sequence Op and not a Renew. */
4188 1.1 dholland NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
4189 1.1 dholland *tl++ = NFSMNT_MDSSESSION(nmp)->nfsess_clientid.lval[0];
4190 1.1 dholland *tl = NFSMNT_MDSSESSION(nmp)->nfsess_clientid.lval[1];
4191 1.1 dholland }
4192 1.1 dholland nrp = dsp->nfsclds_sockp;
4193 1.1 dholland if (nrp == NULL)
4194 1.1 dholland /* If NULL, use the MDS socket. */
4195 1.1 dholland nrp = &nmp->nm_sockreq;
4196 1.1 dholland nd->nd_flag |= ND_USEGSSNAME;
4197 1.1 dholland error = newnfs_request(nd, nmp, NULL, nrp, NULL, p, cred,
4198 1.1 dholland NFS_PROG, NFS_VER4, NULL, 1, NULL, &dsp->nfsclds_sess);
4199 1.1 dholland if (error)
4200 1.1 dholland return (error);
4201 1.1 dholland error = nd->nd_repstat;
4202 1.1 dholland mbuf_freem(nd->nd_mrep);
4203 1.1 dholland return (error);
4204 1.1 dholland }
4205 1.1 dholland
4206 1.1 dholland /*
4207 1.1 dholland * This function performs the Releaselockowner RPC.
4208 1.1 dholland */
4209 1.1 dholland APPLESTATIC int
4210 1.1 dholland nfsrpc_rellockown(struct nfsmount *nmp, struct nfscllockowner *lp,
4211 1.1 dholland uint8_t *fh, int fhlen, struct ucred *cred, NFSPROC_T *p)
4212 1.1 dholland {
4213 1.1 dholland struct nfsrv_descript nfsd, *nd = &nfsd;
4214 1.1 dholland u_int32_t *tl;
4215 1.1 dholland int error;
4216 1.1 dholland uint8_t own[NFSV4CL_LOCKNAMELEN + NFSX_V4FHMAX];
4217 1.1 dholland
4218 1.1 dholland if (NFSHASNFSV4N(nmp)) {
4219 1.1 dholland /* For NFSv4.1, do a FreeStateID. */
4220 1.1 dholland nfscl_reqstart(nd, NFSPROC_FREESTATEID, nmp, NULL, 0, NULL,
4221 1.1 dholland NULL);
4222 1.1 dholland nfsm_stateidtom(nd, &lp->nfsl_stateid, NFSSTATEID_PUTSTATEID);
4223 1.1 dholland } else {
4224 1.1 dholland nfscl_reqstart(nd, NFSPROC_RELEASELCKOWN, nmp, NULL, 0, NULL,
4225 1.1 dholland NULL);
4226 1.1 dholland NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
4227 1.1 dholland *tl++ = NFSMNT_MDSSESSION(nmp)->nfsess_clientid.lval[0];
4228 1.1 dholland *tl = NFSMNT_MDSSESSION(nmp)->nfsess_clientid.lval[1];
4229 1.1 dholland NFSBCOPY(lp->nfsl_owner, own, NFSV4CL_LOCKNAMELEN);
4230 1.1 dholland NFSBCOPY(fh, &own[NFSV4CL_LOCKNAMELEN], fhlen);
4231 1.1 dholland (void)nfsm_strtom(nd, own, NFSV4CL_LOCKNAMELEN + fhlen);
4232 1.1 dholland }
4233 1.1 dholland nd->nd_flag |= ND_USEGSSNAME;
4234 1.1 dholland error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4235 1.1 dholland NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4236 1.1 dholland if (error)
4237 1.1 dholland return (error);
4238 1.1 dholland error = nd->nd_repstat;
4239 1.1 dholland mbuf_freem(nd->nd_mrep);
4240 1.1 dholland return (error);
4241 1.1 dholland }
4242 1.1 dholland
4243 1.1 dholland /*
4244 1.1 dholland * This function performs the Compound to get the mount pt FH.
4245 1.1 dholland */
4246 1.1 dholland APPLESTATIC int
4247 1.1 dholland nfsrpc_getdirpath(struct nfsmount *nmp, u_char *dirpath, struct ucred *cred,
4248 1.1 dholland NFSPROC_T *p)
4249 1.1 dholland {
4250 1.1 dholland u_int32_t *tl;
4251 1.1 dholland struct nfsrv_descript nfsd;
4252 1.1 dholland struct nfsrv_descript *nd = &nfsd;
4253 1.1 dholland u_char *cp, *cp2;
4254 1.1 dholland int error, cnt, len, setnil;
4255 1.1 dholland u_int32_t *opcntp;
4256 1.1 dholland
4257 1.1 dholland nfscl_reqstart(nd, NFSPROC_PUTROOTFH, nmp, NULL, 0, &opcntp, NULL);
4258 1.1 dholland cp = dirpath;
4259 1.1 dholland cnt = 0;
4260 1.1 dholland do {
4261 1.1 dholland setnil = 0;
4262 1.1 dholland while (*cp == '/')
4263 1.1 dholland cp++;
4264 1.1 dholland cp2 = cp;
4265 1.1 dholland while (*cp2 != '\0' && *cp2 != '/')
4266 1.1 dholland cp2++;
4267 1.1 dholland if (*cp2 == '/') {
4268 1.1 dholland setnil = 1;
4269 1.1 dholland *cp2 = '\0';
4270 1.1 dholland }
4271 1.1 dholland if (cp2 != cp) {
4272 1.1 dholland NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
4273 1.1 dholland *tl = txdr_unsigned(NFSV4OP_LOOKUP);
4274 1.1 dholland nfsm_strtom(nd, cp, strlen(cp));
4275 1.1 dholland cnt++;
4276 1.1 dholland }
4277 1.1 dholland if (setnil)
4278 1.1 dholland *cp2++ = '/';
4279 1.1 dholland cp = cp2;
4280 1.1 dholland } while (*cp != '\0');
4281 1.1 dholland if (NFSHASNFSV4N(nmp))
4282 1.1 dholland /* Has a Sequence Op done by nfscl_reqstart(). */
4283 1.1 dholland *opcntp = txdr_unsigned(3 + cnt);
4284 1.1 dholland else
4285 1.1 dholland *opcntp = txdr_unsigned(2 + cnt);
4286 1.1 dholland NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
4287 1.1 dholland *tl = txdr_unsigned(NFSV4OP_GETFH);
4288 1.1 dholland nd->nd_flag |= ND_USEGSSNAME;
4289 1.1 dholland error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4290 1.1 dholland NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4291 1.1 dholland if (error)
4292 1.1 dholland return (error);
4293 1.1 dholland if (nd->nd_repstat == 0) {
4294 1.1 dholland NFSM_DISSECT(tl, u_int32_t *, (3 + 2 * cnt) * NFSX_UNSIGNED);
4295 1.1 dholland tl += (2 + 2 * cnt);
4296 1.1 dholland if ((len = fxdr_unsigned(int, *tl)) <= 0 ||
4297 1.1 dholland len > NFSX_FHMAX) {
4298 1.1 dholland nd->nd_repstat = NFSERR_BADXDR;
4299 1.1 dholland } else {
4300 1.1 dholland nd->nd_repstat = nfsrv_mtostr(nd, nmp->nm_fh, len);
4301 1.1 dholland if (nd->nd_repstat == 0)
4302 1.1 dholland nmp->nm_fhsize = len;
4303 1.1 dholland }
4304 1.1 dholland }
4305 1.1 dholland error = nd->nd_repstat;
4306 1.1 dholland nfsmout:
4307 1.1 dholland mbuf_freem(nd->nd_mrep);
4308 1.1 dholland return (error);
4309 1.1 dholland }
4310 1.1 dholland
4311 1.1 dholland /*
4312 1.1 dholland * This function performs the Delegreturn RPC.
4313 1.1 dholland */
4314 1.1 dholland APPLESTATIC int
4315 1.1 dholland nfsrpc_delegreturn(struct nfscldeleg *dp, struct ucred *cred,
4316 1.1 dholland struct nfsmount *nmp, NFSPROC_T *p, int syscred)
4317 1.1 dholland {
4318 1.1 dholland u_int32_t *tl;
4319 1.1 dholland struct nfsrv_descript nfsd;
4320 1.1 dholland struct nfsrv_descript *nd = &nfsd;
4321 1.1 dholland int error;
4322 1.1 dholland
4323 1.1 dholland nfscl_reqstart(nd, NFSPROC_DELEGRETURN, nmp, dp->nfsdl_fh,
4324 1.1 dholland dp->nfsdl_fhlen, NULL, NULL);
4325 1.1 dholland NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID);
4326 1.1 dholland if (NFSHASNFSV4N(nmp))
4327 1.1 dholland *tl++ = 0;
4328 1.1 dholland else
4329 1.1 dholland *tl++ = dp->nfsdl_stateid.seqid;
4330 1.1 dholland *tl++ = dp->nfsdl_stateid.other[0];
4331 1.1 dholland *tl++ = dp->nfsdl_stateid.other[1];
4332 1.1 dholland *tl = dp->nfsdl_stateid.other[2];
4333 1.1 dholland if (syscred)
4334 1.1 dholland nd->nd_flag |= ND_USEGSSNAME;
4335 1.1 dholland error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4336 1.1 dholland NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4337 1.1 dholland if (error)
4338 1.1 dholland return (error);
4339 1.1 dholland error = nd->nd_repstat;
4340 1.1 dholland mbuf_freem(nd->nd_mrep);
4341 1.1 dholland return (error);
4342 1.1 dholland }
4343 1.1 dholland
4344 1.1 dholland /*
4345 1.1 dholland * nfs getacl call.
4346 1.1 dholland */
4347 1.1 dholland APPLESTATIC int
4348 1.1 dholland nfsrpc_getacl(vnode_t vp, struct ucred *cred, NFSPROC_T *p,
4349 1.1 dholland struct acl *aclp, void *stuff)
4350 1.1 dholland {
4351 1.1 dholland struct nfsrv_descript nfsd, *nd = &nfsd;
4352 1.1 dholland int error;
4353 1.1 dholland nfsattrbit_t attrbits;
4354 1.1 dholland struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
4355 1.1 dholland
4356 1.1 dholland if (nfsrv_useacl == 0 || !NFSHASNFSV4(nmp))
4357 1.1 dholland return (EOPNOTSUPP);
4358 1.1 dholland NFSCL_REQSTART(nd, NFSPROC_GETACL, vp);
4359 1.1 dholland NFSZERO_ATTRBIT(&attrbits);
4360 1.1 dholland NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_ACL);
4361 1.1 dholland (void) nfsrv_putattrbit(nd, &attrbits);
4362 1.1 dholland error = nfscl_request(nd, vp, p, cred, stuff);
4363 1.1 dholland if (error)
4364 1.1 dholland return (error);
4365 1.1 dholland if (!nd->nd_repstat)
4366 1.1 dholland error = nfsv4_loadattr(nd, vp, NULL, NULL, NULL, 0, NULL,
4367 1.1 dholland NULL, NULL, NULL, aclp, 0, NULL, NULL, NULL, p, cred);
4368 1.1 dholland else
4369 1.1 dholland error = nd->nd_repstat;
4370 1.1 dholland mbuf_freem(nd->nd_mrep);
4371 1.1 dholland return (error);
4372 1.1 dholland }
4373 1.1 dholland
4374 1.1 dholland /*
4375 1.1 dholland * nfs setacl call.
4376 1.1 dholland */
4377 1.1 dholland APPLESTATIC int
4378 1.1 dholland nfsrpc_setacl(vnode_t vp, struct ucred *cred, NFSPROC_T *p,
4379 1.1 dholland struct acl *aclp, void *stuff)
4380 1.1 dholland {
4381 1.1 dholland int error;
4382 1.1 dholland struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
4383 1.1 dholland
4384 1.1 dholland if (nfsrv_useacl == 0 || !NFSHASNFSV4(nmp))
4385 1.1 dholland return (EOPNOTSUPP);
4386 1.1 dholland error = nfsrpc_setattr(vp, NULL, aclp, cred, p, NULL, NULL, stuff);
4387 1.1 dholland return (error);
4388 1.1 dholland }
4389 1.1 dholland
4390 1.1 dholland /*
4391 1.1 dholland * nfs setacl call.
4392 1.1 dholland */
4393 1.1 dholland static int
4394 1.1 dholland nfsrpc_setaclrpc(vnode_t vp, struct ucred *cred, NFSPROC_T *p,
4395 1.1 dholland struct acl *aclp, nfsv4stateid_t *stateidp, void *stuff)
4396 1.1 dholland {
4397 1.1 dholland struct nfsrv_descript nfsd, *nd = &nfsd;
4398 1.1 dholland int error;
4399 1.1 dholland nfsattrbit_t attrbits;
4400 1.1 dholland struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
4401 1.1 dholland
4402 1.1 dholland if (!NFSHASNFSV4(nmp))
4403 1.1 dholland return (EOPNOTSUPP);
4404 1.1 dholland NFSCL_REQSTART(nd, NFSPROC_SETACL, vp);
4405 1.1 dholland nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID);
4406 1.1 dholland NFSZERO_ATTRBIT(&attrbits);
4407 1.1 dholland NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_ACL);
4408 1.1 dholland (void) nfsv4_fillattr(nd, vnode_mount(vp), vp, aclp, NULL, NULL, 0,
4409 1.1 dholland &attrbits, NULL, NULL, 0, 0, 0, 0, (uint64_t)0);
4410 1.1 dholland error = nfscl_request(nd, vp, p, cred, stuff);
4411 1.1 dholland if (error)
4412 1.1 dholland return (error);
4413 1.1 dholland /* Don't care about the pre/postop attributes */
4414 1.1 dholland mbuf_freem(nd->nd_mrep);
4415 1.1 dholland return (nd->nd_repstat);
4416 1.1 dholland }
4417 1.1 dholland
4418 1.1 dholland /*
4419 1.1 dholland * Do the NFSv4.1 Exchange ID.
4420 1.1 dholland */
4421 1.1 dholland int
4422 1.1 dholland nfsrpc_exchangeid(struct nfsmount *nmp, struct nfsclclient *clp,
4423 1.1 dholland struct nfssockreq *nrp, uint32_t exchflags, struct nfsclds **dspp,
4424 1.1 dholland struct ucred *cred, NFSPROC_T *p)
4425 1.1 dholland {
4426 1.1 dholland uint32_t *tl, v41flags;
4427 1.1 dholland struct nfsrv_descript nfsd;
4428 1.1 dholland struct nfsrv_descript *nd = &nfsd;
4429 1.1 dholland struct nfsclds *dsp;
4430 1.1 dholland struct timespec verstime;
4431 1.1 dholland int error, len;
4432 1.1 dholland
4433 1.1 dholland *dspp = NULL;
4434 1.1 dholland nfscl_reqstart(nd, NFSPROC_EXCHANGEID, nmp, NULL, 0, NULL, NULL);
4435 1.1 dholland NFSM_BUILD(tl, uint32_t *, 2 * NFSX_UNSIGNED);
4436 1.1 dholland *tl++ = txdr_unsigned(nfsboottime.tv_sec); /* Client owner */
4437 1.1 dholland *tl = txdr_unsigned(clp->nfsc_rev);
4438 1.1 dholland (void) nfsm_strtom(nd, clp->nfsc_id, clp->nfsc_idlen);
4439 1.1 dholland
4440 1.1 dholland NFSM_BUILD(tl, uint32_t *, 3 * NFSX_UNSIGNED);
4441 1.1 dholland *tl++ = txdr_unsigned(exchflags);
4442 1.1 dholland *tl++ = txdr_unsigned(NFSV4EXCH_SP4NONE);
4443 1.1 dholland
4444 1.1 dholland /* Set the implementation id4 */
4445 1.1 dholland *tl = txdr_unsigned(1);
4446 1.1 dholland (void) nfsm_strtom(nd, "freebsd.org", strlen("freebsd.org"));
4447 1.1 dholland (void) nfsm_strtom(nd, version, strlen(version));
4448 1.1 dholland NFSM_BUILD(tl, uint32_t *, NFSX_V4TIME);
4449 1.1 dholland verstime.tv_sec = 1293840000; /* Jan 1, 2011 */
4450 1.1 dholland verstime.tv_nsec = 0;
4451 1.1 dholland txdr_nfsv4time(&verstime, tl);
4452 1.1 dholland nd->nd_flag |= ND_USEGSSNAME;
4453 1.1 dholland error = newnfs_request(nd, nmp, NULL, nrp, NULL, p, cred,
4454 1.1 dholland NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4455 1.1 dholland NFSCL_DEBUG(1, "exchangeid err=%d reps=%d\n", error,
4456 1.1 dholland (int)nd->nd_repstat);
4457 1.1 dholland if (error != 0)
4458 1.1 dholland return (error);
4459 1.1 dholland if (nd->nd_repstat == 0) {
4460 1.1 dholland NFSM_DISSECT(tl, uint32_t *, 6 * NFSX_UNSIGNED + NFSX_HYPER);
4461 1.1 dholland len = fxdr_unsigned(int, *(tl + 7));
4462 1.1 dholland if (len < 0 || len > NFSV4_OPAQUELIMIT) {
4463 1.1 dholland error = NFSERR_BADXDR;
4464 1.1 dholland goto nfsmout;
4465 1.1 dholland }
4466 1.1 dholland dsp = malloc(sizeof(struct nfsclds) + len, M_NFSCLDS,
4467 1.1 dholland M_WAITOK | M_ZERO);
4468 1.1 dholland dsp->nfsclds_expire = NFSD_MONOSEC + clp->nfsc_renew;
4469 1.1 dholland dsp->nfsclds_servownlen = len;
4470 1.1 dholland dsp->nfsclds_sess.nfsess_clientid.lval[0] = *tl++;
4471 1.1 dholland dsp->nfsclds_sess.nfsess_clientid.lval[1] = *tl++;
4472 1.1 dholland dsp->nfsclds_sess.nfsess_sequenceid =
4473 1.1 dholland fxdr_unsigned(uint32_t, *tl++);
4474 1.1 dholland v41flags = fxdr_unsigned(uint32_t, *tl);
4475 1.1 dholland if ((v41flags & NFSV4EXCH_USEPNFSMDS) != 0 &&
4476 1.1 dholland NFSHASPNFSOPT(nmp)) {
4477 1.1 dholland NFSCL_DEBUG(1, "set PNFS\n");
4478 1.1 dholland NFSLOCKMNT(nmp);
4479 1.1 dholland nmp->nm_state |= NFSSTA_PNFS;
4480 1.1 dholland NFSUNLOCKMNT(nmp);
4481 1.1 dholland dsp->nfsclds_flags |= NFSCLDS_MDS;
4482 1.1 dholland }
4483 1.1 dholland if ((v41flags & NFSV4EXCH_USEPNFSDS) != 0)
4484 1.1 dholland dsp->nfsclds_flags |= NFSCLDS_DS;
4485 1.1 dholland if (len > 0)
4486 1.1 dholland nd->nd_repstat = nfsrv_mtostr(nd,
4487 1.1 dholland dsp->nfsclds_serverown, len);
4488 1.1 dholland if (nd->nd_repstat == 0) {
4489 1.1 dholland mtx_init(&dsp->nfsclds_mtx, "nfsds", NULL, MTX_DEF);
4490 1.1 dholland mtx_init(&dsp->nfsclds_sess.nfsess_mtx, "nfssession",
4491 1.1 dholland NULL, MTX_DEF);
4492 1.1 dholland nfscl_initsessionslots(&dsp->nfsclds_sess);
4493 1.1 dholland *dspp = dsp;
4494 1.1 dholland } else
4495 1.1 dholland free(dsp, M_NFSCLDS);
4496 1.1 dholland }
4497 1.1 dholland error = nd->nd_repstat;
4498 1.1 dholland nfsmout:
4499 1.1 dholland mbuf_freem(nd->nd_mrep);
4500 1.1 dholland return (error);
4501 1.1 dholland }
4502 1.1 dholland
4503 1.1 dholland /*
4504 1.1 dholland * Do the NFSv4.1 Create Session.
4505 1.1 dholland */
4506 1.1 dholland int
4507 1.1 dholland nfsrpc_createsession(struct nfsmount *nmp, struct nfsclsession *sep,
4508 1.1 dholland struct nfssockreq *nrp, uint32_t sequenceid, int mds, struct ucred *cred,
4509 1.1 dholland NFSPROC_T *p)
4510 1.1 dholland {
4511 1.1 dholland uint32_t crflags, *tl;
4512 1.1 dholland struct nfsrv_descript nfsd;
4513 1.1 dholland struct nfsrv_descript *nd = &nfsd;
4514 1.1 dholland int error, irdcnt;
4515 1.1 dholland
4516 1.1 dholland nfscl_reqstart(nd, NFSPROC_CREATESESSION, nmp, NULL, 0, NULL, NULL);
4517 1.1 dholland NFSM_BUILD(tl, uint32_t *, 4 * NFSX_UNSIGNED);
4518 1.1 dholland *tl++ = sep->nfsess_clientid.lval[0];
4519 1.1 dholland *tl++ = sep->nfsess_clientid.lval[1];
4520 1.1 dholland *tl++ = txdr_unsigned(sequenceid);
4521 1.1 dholland crflags = (NFSMNT_RDONLY(nmp->nm_mountp) ? 0 : NFSV4CRSESS_PERSIST);
4522 1.1 dholland if (nfscl_enablecallb != 0 && nfs_numnfscbd > 0)
4523 1.1 dholland crflags |= NFSV4CRSESS_CONNBACKCHAN;
4524 1.1 dholland *tl = txdr_unsigned(crflags);
4525 1.1 dholland
4526 1.1 dholland /* Fill in fore channel attributes. */
4527 1.1 dholland NFSM_BUILD(tl, uint32_t *, 7 * NFSX_UNSIGNED);
4528 1.1 dholland *tl++ = 0; /* Header pad size */
4529 1.1 dholland *tl++ = txdr_unsigned(100000); /* Max request size */
4530 1.1 dholland *tl++ = txdr_unsigned(100000); /* Max response size */
4531 1.1 dholland *tl++ = txdr_unsigned(4096); /* Max response size cached */
4532 1.1 dholland *tl++ = txdr_unsigned(20); /* Max operations */
4533 1.1 dholland *tl++ = txdr_unsigned(64); /* Max slots */
4534 1.1 dholland *tl = 0; /* No rdma ird */
4535 1.1 dholland
4536 1.1 dholland /* Fill in back channel attributes. */
4537 1.1 dholland NFSM_BUILD(tl, uint32_t *, 7 * NFSX_UNSIGNED);
4538 1.1 dholland *tl++ = 0; /* Header pad size */
4539 1.1 dholland *tl++ = txdr_unsigned(10000); /* Max request size */
4540 1.1 dholland *tl++ = txdr_unsigned(10000); /* Max response size */
4541 1.1 dholland *tl++ = txdr_unsigned(4096); /* Max response size cached */
4542 1.1 dholland *tl++ = txdr_unsigned(4); /* Max operations */
4543 1.1 dholland *tl++ = txdr_unsigned(NFSV4_CBSLOTS); /* Max slots */
4544 1.1 dholland *tl = 0; /* No rdma ird */
4545 1.1 dholland
4546 1.1 dholland NFSM_BUILD(tl, uint32_t *, 8 * NFSX_UNSIGNED);
4547 1.1 dholland *tl++ = txdr_unsigned(NFS_CALLBCKPROG); /* Call back prog # */
4548 1.1 dholland
4549 1.1 dholland /* Allow AUTH_SYS callbacks as uid, gid == 0. */
4550 1.1 dholland *tl++ = txdr_unsigned(1); /* Auth_sys only */
4551 1.1 dholland *tl++ = txdr_unsigned(AUTH_SYS); /* AUTH_SYS type */
4552 1.1 dholland *tl++ = txdr_unsigned(nfsboottime.tv_sec); /* time stamp */
4553 1.1 dholland *tl++ = 0; /* Null machine name */
4554 1.1 dholland *tl++ = 0; /* Uid == 0 */
4555 1.1 dholland *tl++ = 0; /* Gid == 0 */
4556 1.1 dholland *tl = 0; /* No additional gids */
4557 1.1 dholland nd->nd_flag |= ND_USEGSSNAME;
4558 1.1 dholland error = newnfs_request(nd, nmp, NULL, nrp, NULL, p, cred, NFS_PROG,
4559 1.1 dholland NFS_VER4, NULL, 1, NULL, NULL);
4560 1.1 dholland if (error != 0)
4561 1.1 dholland return (error);
4562 1.1 dholland if (nd->nd_repstat == 0) {
4563 1.1 dholland NFSM_DISSECT(tl, uint32_t *, NFSX_V4SESSIONID +
4564 1.1 dholland 2 * NFSX_UNSIGNED);
4565 1.1 dholland bcopy(tl, sep->nfsess_sessionid, NFSX_V4SESSIONID);
4566 1.1 dholland tl += NFSX_V4SESSIONID / NFSX_UNSIGNED;
4567 1.1 dholland sep->nfsess_sequenceid = fxdr_unsigned(uint32_t, *tl++);
4568 1.1 dholland crflags = fxdr_unsigned(uint32_t, *tl);
4569 1.1 dholland if ((crflags & NFSV4CRSESS_PERSIST) != 0 && mds != 0) {
4570 1.1 dholland NFSLOCKMNT(nmp);
4571 1.1 dholland nmp->nm_state |= NFSSTA_SESSPERSIST;
4572 1.1 dholland NFSUNLOCKMNT(nmp);
4573 1.1 dholland }
4574 1.1 dholland
4575 1.1 dholland /* Get the fore channel slot count. */
4576 1.1 dholland NFSM_DISSECT(tl, uint32_t *, 7 * NFSX_UNSIGNED);
4577 1.1 dholland tl += 3; /* Skip the other counts. */
4578 1.1 dholland sep->nfsess_maxcache = fxdr_unsigned(int, *tl++);
4579 1.1 dholland tl++;
4580 1.1 dholland sep->nfsess_foreslots = fxdr_unsigned(uint16_t, *tl++);
4581 1.1 dholland NFSCL_DEBUG(4, "fore slots=%d\n", (int)sep->nfsess_foreslots);
4582 1.1 dholland irdcnt = fxdr_unsigned(int, *tl);
4583 1.1 dholland if (irdcnt > 0)
4584 1.1 dholland NFSM_DISSECT(tl, uint32_t *, irdcnt * NFSX_UNSIGNED);
4585 1.1 dholland
4586 1.1 dholland /* and the back channel slot count. */
4587 1.1 dholland NFSM_DISSECT(tl, uint32_t *, 7 * NFSX_UNSIGNED);
4588 1.1 dholland tl += 5;
4589 1.1 dholland sep->nfsess_backslots = fxdr_unsigned(uint16_t, *tl);
4590 1.1 dholland NFSCL_DEBUG(4, "back slots=%d\n", (int)sep->nfsess_backslots);
4591 1.1 dholland }
4592 1.1 dholland error = nd->nd_repstat;
4593 1.1 dholland nfsmout:
4594 1.1 dholland mbuf_freem(nd->nd_mrep);
4595 1.1 dholland return (error);
4596 1.1 dholland }
4597 1.1 dholland
4598 1.1 dholland /*
4599 1.1 dholland * Do the NFSv4.1 Destroy Session.
4600 1.1 dholland */
4601 1.1 dholland int
4602 1.1 dholland nfsrpc_destroysession(struct nfsmount *nmp, struct nfsclclient *clp,
4603 1.1 dholland struct ucred *cred, NFSPROC_T *p)
4604 1.1 dholland {
4605 1.1 dholland uint32_t *tl;
4606 1.1 dholland struct nfsrv_descript nfsd;
4607 1.1 dholland struct nfsrv_descript *nd = &nfsd;
4608 1.1 dholland int error;
4609 1.1 dholland
4610 1.1 dholland nfscl_reqstart(nd, NFSPROC_DESTROYSESSION, nmp, NULL, 0, NULL, NULL);
4611 1.1 dholland NFSM_BUILD(tl, uint32_t *, NFSX_V4SESSIONID);
4612 1.1 dholland bcopy(NFSMNT_MDSSESSION(nmp)->nfsess_sessionid, tl, NFSX_V4SESSIONID);
4613 1.1 dholland nd->nd_flag |= ND_USEGSSNAME;
4614 1.1 dholland error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4615 1.1 dholland NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4616 1.1 dholland if (error != 0)
4617 1.1 dholland return (error);
4618 1.1 dholland error = nd->nd_repstat;
4619 1.1 dholland mbuf_freem(nd->nd_mrep);
4620 1.1 dholland return (error);
4621 1.1 dholland }
4622 1.1 dholland
4623 1.1 dholland /*
4624 1.1 dholland * Do the NFSv4.1 Destroy Client.
4625 1.1 dholland */
4626 1.1 dholland int
4627 1.1 dholland nfsrpc_destroyclient(struct nfsmount *nmp, struct nfsclclient *clp,
4628 1.1 dholland struct ucred *cred, NFSPROC_T *p)
4629 1.1 dholland {
4630 1.1 dholland uint32_t *tl;
4631 1.1 dholland struct nfsrv_descript nfsd;
4632 1.1 dholland struct nfsrv_descript *nd = &nfsd;
4633 1.1 dholland int error;
4634 1.1 dholland
4635 1.1 dholland nfscl_reqstart(nd, NFSPROC_DESTROYCLIENT, nmp, NULL, 0, NULL, NULL);
4636 1.1 dholland NFSM_BUILD(tl, uint32_t *, 2 * NFSX_UNSIGNED);
4637 1.1 dholland *tl++ = NFSMNT_MDSSESSION(nmp)->nfsess_clientid.lval[0];
4638 1.1 dholland *tl = NFSMNT_MDSSESSION(nmp)->nfsess_clientid.lval[1];
4639 1.1 dholland nd->nd_flag |= ND_USEGSSNAME;
4640 1.1 dholland error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4641 1.1 dholland NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4642 1.1 dholland if (error != 0)
4643 1.1 dholland return (error);
4644 1.1 dholland error = nd->nd_repstat;
4645 1.1 dholland mbuf_freem(nd->nd_mrep);
4646 1.1 dholland return (error);
4647 1.1 dholland }
4648 1.1 dholland
4649 1.1 dholland /*
4650 1.1 dholland * Do the NFSv4.1 LayoutGet.
4651 1.1 dholland */
4652 1.1 dholland int
4653 1.1 dholland nfsrpc_layoutget(struct nfsmount *nmp, uint8_t *fhp, int fhlen, int iomode,
4654 1.1 dholland uint64_t offset, uint64_t len, uint64_t minlen, int layoutlen,
4655 1.1 dholland nfsv4stateid_t *stateidp, int *retonclosep, struct nfsclflayouthead *flhp,
4656 1.1 dholland struct ucred *cred, NFSPROC_T *p, void *stuff)
4657 1.1 dholland {
4658 1.1 dholland uint32_t *tl;
4659 1.1 dholland struct nfsrv_descript nfsd, *nd = &nfsd;
4660 1.1 dholland struct nfsfh *nfhp;
4661 1.1 dholland struct nfsclflayout *flp, *prevflp, *tflp;
4662 1.1 dholland int cnt, error, gotiomode, fhcnt, nfhlen, i, j;
4663 1.1 dholland uint8_t *cp;
4664 1.1 dholland uint64_t retlen;
4665 1.1 dholland
4666 1.1 dholland flp = NULL;
4667 1.1 dholland gotiomode = -1;
4668 1.1 dholland nfscl_reqstart(nd, NFSPROC_LAYOUTGET, nmp, fhp, fhlen, NULL, NULL);
4669 1.1 dholland NFSM_BUILD(tl, uint32_t *, 4 * NFSX_UNSIGNED + 3 * NFSX_HYPER +
4670 1.1 dholland NFSX_STATEID);
4671 1.1 dholland *tl++ = newnfs_false; /* Don't signal availability. */
4672 1.1 dholland *tl++ = txdr_unsigned(NFSLAYOUT_NFSV4_1_FILES);
4673 1.1 dholland *tl++ = txdr_unsigned(iomode);
4674 1.1 dholland txdr_hyper(offset, tl);
4675 1.1 dholland tl += 2;
4676 1.1 dholland txdr_hyper(len, tl);
4677 1.1 dholland tl += 2;
4678 1.1 dholland txdr_hyper(minlen, tl);
4679 1.1 dholland tl += 2;
4680 1.1 dholland *tl++ = txdr_unsigned(stateidp->seqid);
4681 1.1 dholland NFSCL_DEBUG(4, "layget seq=%d\n", (int)stateidp->seqid);
4682 1.1 dholland *tl++ = stateidp->other[0];
4683 1.1 dholland *tl++ = stateidp->other[1];
4684 1.1 dholland *tl++ = stateidp->other[2];
4685 1.1 dholland *tl = txdr_unsigned(layoutlen);
4686 1.1 dholland nd->nd_flag |= ND_USEGSSNAME;
4687 1.1 dholland error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4688 1.1 dholland NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4689 1.1 dholland if (error != 0)
4690 1.1 dholland return (error);
4691 1.1 dholland if (nd->nd_repstat == 0) {
4692 1.1 dholland NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED + NFSX_STATEID);
4693 1.1 dholland if (*tl++ != 0)
4694 1.1 dholland *retonclosep = 1;
4695 1.1 dholland else
4696 1.1 dholland *retonclosep = 0;
4697 1.1 dholland stateidp->seqid = fxdr_unsigned(uint32_t, *tl++);
4698 1.1 dholland NFSCL_DEBUG(4, "retoncls=%d stseq=%d\n", *retonclosep,
4699 1.1 dholland (int)stateidp->seqid);
4700 1.1 dholland stateidp->other[0] = *tl++;
4701 1.1 dholland stateidp->other[1] = *tl++;
4702 1.1 dholland stateidp->other[2] = *tl++;
4703 1.1 dholland cnt = fxdr_unsigned(int, *tl);
4704 1.1 dholland NFSCL_DEBUG(4, "layg cnt=%d\n", cnt);
4705 1.1 dholland if (cnt <= 0 || cnt > 10000) {
4706 1.1 dholland /* Don't accept more than 10000 layouts in reply. */
4707 1.1 dholland error = NFSERR_BADXDR;
4708 1.1 dholland goto nfsmout;
4709 1.1 dholland }
4710 1.1 dholland for (i = 0; i < cnt; i++) {
4711 1.1 dholland /* Dissect all the way to the file handle cnt. */
4712 1.1 dholland NFSM_DISSECT(tl, uint32_t *, 3 * NFSX_HYPER +
4713 1.1 dholland 6 * NFSX_UNSIGNED + NFSX_V4DEVICEID);
4714 1.1 dholland fhcnt = fxdr_unsigned(int, *(tl + 11 +
4715 1.1 dholland NFSX_V4DEVICEID / NFSX_UNSIGNED));
4716 1.1 dholland NFSCL_DEBUG(4, "fhcnt=%d\n", fhcnt);
4717 1.1 dholland if (fhcnt < 0 || fhcnt > 100) {
4718 1.1 dholland /* Don't accept more than 100 file handles. */
4719 1.1 dholland error = NFSERR_BADXDR;
4720 1.1 dholland goto nfsmout;
4721 1.1 dholland }
4722 1.1 dholland if (fhcnt > 1)
4723 1.1 dholland flp = malloc(sizeof(*flp) + (fhcnt - 1) *
4724 1.1 dholland sizeof(struct nfsfh *),
4725 1.1 dholland M_NFSFLAYOUT, M_WAITOK);
4726 1.1 dholland else
4727 1.1 dholland flp = malloc(sizeof(*flp),
4728 1.1 dholland M_NFSFLAYOUT, M_WAITOK);
4729 1.1 dholland flp->nfsfl_flags = 0;
4730 1.1 dholland flp->nfsfl_fhcnt = 0;
4731 1.1 dholland flp->nfsfl_devp = NULL;
4732 1.1 dholland flp->nfsfl_off = fxdr_hyper(tl); tl += 2;
4733 1.1 dholland retlen = fxdr_hyper(tl); tl += 2;
4734 1.1 dholland if (flp->nfsfl_off + retlen < flp->nfsfl_off)
4735 1.1 dholland flp->nfsfl_end = UINT64_MAX - flp->nfsfl_off;
4736 1.1 dholland else
4737 1.1 dholland flp->nfsfl_end = flp->nfsfl_off + retlen;
4738 1.1 dholland flp->nfsfl_iomode = fxdr_unsigned(int, *tl++);
4739 1.1 dholland if (gotiomode == -1)
4740 1.1 dholland gotiomode = flp->nfsfl_iomode;
4741 1.1 dholland NFSCL_DEBUG(4, "layg reqiom=%d retiom=%d\n", iomode,
4742 1.1 dholland (int)flp->nfsfl_iomode);
4743 1.1 dholland if (fxdr_unsigned(int, *tl++) !=
4744 1.1 dholland NFSLAYOUT_NFSV4_1_FILES) {
4745 1.1 dholland printf("NFSv4.1: got non-files layout\n");
4746 1.1 dholland error = NFSERR_BADXDR;
4747 1.1 dholland goto nfsmout;
4748 1.1 dholland }
4749 1.1 dholland NFSBCOPY(++tl, flp->nfsfl_dev, NFSX_V4DEVICEID);
4750 1.1 dholland tl += (NFSX_V4DEVICEID / NFSX_UNSIGNED);
4751 1.1 dholland flp->nfsfl_util = fxdr_unsigned(uint32_t, *tl++);
4752 1.1 dholland NFSCL_DEBUG(4, "flutil=0x%x\n", flp->nfsfl_util);
4753 1.1 dholland flp->nfsfl_stripe1 = fxdr_unsigned(uint32_t, *tl++);
4754 1.1 dholland flp->nfsfl_patoff = fxdr_hyper(tl); tl += 2;
4755 1.1 dholland if (fxdr_unsigned(int, *tl) != fhcnt) {
4756 1.1 dholland printf("EEK! bad fhcnt\n");
4757 1.1 dholland error = NFSERR_BADXDR;
4758 1.1 dholland goto nfsmout;
4759 1.1 dholland }
4760 1.1 dholland for (j = 0; j < fhcnt; j++) {
4761 1.1 dholland NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
4762 1.1 dholland nfhlen = fxdr_unsigned(int, *tl);
4763 1.1 dholland if (nfhlen <= 0 || nfhlen > NFSX_V4FHMAX) {
4764 1.1 dholland error = NFSERR_BADXDR;
4765 1.1 dholland goto nfsmout;
4766 1.1 dholland }
4767 1.1 dholland nfhp = malloc(sizeof(*nfhp) + nfhlen - 1,
4768 1.1 dholland M_NFSFH, M_WAITOK);
4769 1.1 dholland flp->nfsfl_fh[j] = nfhp;
4770 1.1 dholland flp->nfsfl_fhcnt++;
4771 1.1 dholland nfhp->nfh_len = nfhlen;
4772 1.1 dholland NFSM_DISSECT(cp, uint8_t *, NFSM_RNDUP(nfhlen));
4773 1.1 dholland NFSBCOPY(cp, nfhp->nfh_fh, nfhlen);
4774 1.1 dholland }
4775 1.1 dholland if (flp->nfsfl_iomode == gotiomode) {
4776 1.1 dholland /* Keep the list in increasing offset order. */
4777 1.1 dholland tflp = LIST_FIRST(flhp);
4778 1.1 dholland prevflp = NULL;
4779 1.1 dholland while (tflp != NULL &&
4780 1.1 dholland tflp->nfsfl_off < flp->nfsfl_off) {
4781 1.1 dholland prevflp = tflp;
4782 1.1 dholland tflp = LIST_NEXT(tflp, nfsfl_list);
4783 1.1 dholland }
4784 1.1 dholland if (prevflp == NULL)
4785 1.1 dholland LIST_INSERT_HEAD(flhp, flp, nfsfl_list);
4786 1.1 dholland else
4787 1.1 dholland LIST_INSERT_AFTER(prevflp, flp,
4788 1.1 dholland nfsfl_list);
4789 1.1 dholland } else {
4790 1.1 dholland printf("nfscl_layoutget(): got wrong iomode\n");
4791 1.1 dholland nfscl_freeflayout(flp);
4792 1.1 dholland }
4793 1.1 dholland flp = NULL;
4794 1.1 dholland }
4795 1.1 dholland }
4796 1.1 dholland if (nd->nd_repstat != 0 && error == 0)
4797 1.1 dholland error = nd->nd_repstat;
4798 1.1 dholland nfsmout:
4799 1.1 dholland if (error != 0 && flp != NULL)
4800 1.1 dholland nfscl_freeflayout(flp);
4801 1.1 dholland mbuf_freem(nd->nd_mrep);
4802 1.1 dholland return (error);
4803 1.1 dholland }
4804 1.1 dholland
4805 1.1 dholland /*
4806 1.1 dholland * Do the NFSv4.1 Get Device Info.
4807 1.1 dholland */
4808 1.1 dholland int
4809 1.1 dholland nfsrpc_getdeviceinfo(struct nfsmount *nmp, uint8_t *deviceid, int layouttype,
4810 1.1 dholland uint32_t *notifybitsp, struct nfscldevinfo **ndip, struct ucred *cred,
4811 1.1 dholland NFSPROC_T *p)
4812 1.1 dholland {
4813 1.1 dholland uint32_t cnt, *tl;
4814 1.1 dholland struct nfsrv_descript nfsd;
4815 1.1 dholland struct nfsrv_descript *nd = &nfsd;
4816 1.1 dholland struct sockaddr_storage ss;
4817 1.1 dholland struct nfsclds *dsp = NULL, **dspp;
4818 1.1 dholland struct nfscldevinfo *ndi;
4819 1.1 dholland int addrcnt, bitcnt, error, i, isudp, j, pos, safilled, stripecnt;
4820 1.1 dholland uint8_t stripeindex;
4821 1.1 dholland
4822 1.1 dholland *ndip = NULL;
4823 1.1 dholland ndi = NULL;
4824 1.1 dholland nfscl_reqstart(nd, NFSPROC_GETDEVICEINFO, nmp, NULL, 0, NULL, NULL);
4825 1.1 dholland NFSM_BUILD(tl, uint32_t *, NFSX_V4DEVICEID + 3 * NFSX_UNSIGNED);
4826 1.1 dholland NFSBCOPY(deviceid, tl, NFSX_V4DEVICEID);
4827 1.1 dholland tl += (NFSX_V4DEVICEID / NFSX_UNSIGNED);
4828 1.1 dholland *tl++ = txdr_unsigned(layouttype);
4829 1.1 dholland *tl++ = txdr_unsigned(100000);
4830 1.1 dholland if (notifybitsp != NULL && *notifybitsp != 0) {
4831 1.1 dholland *tl = txdr_unsigned(1); /* One word of bits. */
4832 1.1 dholland NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
4833 1.1 dholland *tl = txdr_unsigned(*notifybitsp);
4834 1.1 dholland } else
4835 1.1 dholland *tl = txdr_unsigned(0);
4836 1.1 dholland nd->nd_flag |= ND_USEGSSNAME;
4837 1.1 dholland error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4838 1.1 dholland NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4839 1.1 dholland if (error != 0)
4840 1.1 dholland return (error);
4841 1.1 dholland if (nd->nd_repstat == 0) {
4842 1.1 dholland NFSM_DISSECT(tl, uint32_t *, 3 * NFSX_UNSIGNED);
4843 1.1 dholland if (layouttype != fxdr_unsigned(int, *tl++))
4844 1.1 dholland printf("EEK! devinfo layout type not same!\n");
4845 1.1 dholland stripecnt = fxdr_unsigned(int, *++tl);
4846 1.1 dholland NFSCL_DEBUG(4, "stripecnt=%d\n", stripecnt);
4847 1.1 dholland if (stripecnt < 1 || stripecnt > 4096) {
4848 1.1 dholland printf("NFS devinfo stripecnt %d: out of range\n",
4849 1.1 dholland stripecnt);
4850 1.1 dholland error = NFSERR_BADXDR;
4851 1.1 dholland goto nfsmout;
4852 1.1 dholland }
4853 1.1 dholland NFSM_DISSECT(tl, uint32_t *, (stripecnt + 1) * NFSX_UNSIGNED);
4854 1.1 dholland addrcnt = fxdr_unsigned(int, *(tl + stripecnt));
4855 1.1 dholland NFSCL_DEBUG(4, "addrcnt=%d\n", addrcnt);
4856 1.1 dholland if (addrcnt < 1 || addrcnt > 128) {
4857 1.1 dholland printf("NFS devinfo addrcnt %d: out of range\n",
4858 1.1 dholland addrcnt);
4859 1.1 dholland error = NFSERR_BADXDR;
4860 1.1 dholland goto nfsmout;
4861 1.1 dholland }
4862 1.1 dholland
4863 1.1 dholland /*
4864 1.1 dholland * Now we know how many stripe indices and addresses, so
4865 1.1 dholland * we can allocate the structure the correct size.
4866 1.1 dholland */
4867 1.1 dholland i = (stripecnt * sizeof(uint8_t)) / sizeof(struct nfsclds *)
4868 1.1 dholland + 1;
4869 1.1 dholland NFSCL_DEBUG(4, "stripeindices=%d\n", i);
4870 1.1 dholland ndi = malloc(sizeof(*ndi) + (addrcnt + i) *
4871 1.1 dholland sizeof(struct nfsclds *), M_NFSDEVINFO, M_WAITOK | M_ZERO);
4872 1.1 dholland NFSBCOPY(deviceid, ndi->nfsdi_deviceid, NFSX_V4DEVICEID);
4873 1.1 dholland ndi->nfsdi_refcnt = 0;
4874 1.1 dholland ndi->nfsdi_stripecnt = stripecnt;
4875 1.1 dholland ndi->nfsdi_addrcnt = addrcnt;
4876 1.1 dholland /* Fill in the stripe indices. */
4877 1.1 dholland for (i = 0; i < stripecnt; i++) {
4878 1.1 dholland stripeindex = fxdr_unsigned(uint8_t, *tl++);
4879 1.1 dholland NFSCL_DEBUG(4, "stripeind=%d\n", stripeindex);
4880 1.1 dholland if (stripeindex >= addrcnt) {
4881 1.1 dholland printf("NFS devinfo stripeindex %d: too big\n",
4882 1.1 dholland (int)stripeindex);
4883 1.1 dholland error = NFSERR_BADXDR;
4884 1.1 dholland goto nfsmout;
4885 1.1 dholland }
4886 1.1 dholland nfsfldi_setstripeindex(ndi, i, stripeindex);
4887 1.1 dholland }
4888 1.1 dholland
4889 1.1 dholland /* Now, dissect the server address(es). */
4890 1.1 dholland safilled = 0;
4891 1.1 dholland for (i = 0; i < addrcnt; i++) {
4892 1.1 dholland NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
4893 1.1 dholland cnt = fxdr_unsigned(uint32_t, *tl);
4894 1.1 dholland if (cnt == 0) {
4895 1.1 dholland printf("NFS devinfo 0 len addrlist\n");
4896 1.1 dholland error = NFSERR_BADXDR;
4897 1.1 dholland goto nfsmout;
4898 1.1 dholland }
4899 1.1 dholland dspp = nfsfldi_addr(ndi, i);
4900 1.1 dholland pos = arc4random() % cnt; /* Choose one. */
4901 1.1 dholland safilled = 0;
4902 1.1 dholland for (j = 0; j < cnt; j++) {
4903 1.1 dholland error = nfsv4_getipaddr(nd, &ss, &isudp);
4904 1.1 dholland if (error != 0 && error != EPERM) {
4905 1.1 dholland error = NFSERR_BADXDR;
4906 1.1 dholland goto nfsmout;
4907 1.1 dholland }
4908 1.1 dholland if (error == 0 && isudp == 0) {
4909 1.1 dholland /*
4910 1.1 dholland * The algorithm is:
4911 1.1 dholland * - use "pos" entry if it is of the
4912 1.1 dholland * same af_family or none of them
4913 1.1 dholland * is of the same af_family
4914 1.1 dholland * else
4915 1.1 dholland * - use the first one of the same
4916 1.1 dholland * af_family.
4917 1.1 dholland */
4918 1.1 dholland if ((safilled == 0 && ss.ss_family ==
4919 1.1 dholland nmp->nm_nam->sa_family) ||
4920 1.1 dholland (j == pos &&
4921 1.1 dholland (safilled == 0 || ss.ss_family ==
4922 1.1 dholland nmp->nm_nam->sa_family)) ||
4923 1.1 dholland (safilled == 1 && ss.ss_family ==
4924 1.1 dholland nmp->nm_nam->sa_family)) {
4925 1.1 dholland error = nfsrpc_fillsa(nmp, &ss,
4926 1.1 dholland &dsp, p);
4927 1.1 dholland if (error == 0) {
4928 1.1 dholland *dspp = dsp;
4929 1.1 dholland if (ss.ss_family ==
4930 1.1 dholland nmp->nm_nam->sa_family)
4931 1.1 dholland safilled = 2;
4932 1.1 dholland else
4933 1.1 dholland safilled = 1;
4934 1.1 dholland }
4935 1.1 dholland }
4936 1.1 dholland }
4937 1.1 dholland }
4938 1.1 dholland if (safilled == 0)
4939 1.1 dholland break;
4940 1.1 dholland }
4941 1.1 dholland
4942 1.1 dholland /* And the notify bits. */
4943 1.1 dholland NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
4944 1.1 dholland if (safilled != 0) {
4945 1.1 dholland bitcnt = fxdr_unsigned(int, *tl);
4946 1.1 dholland if (bitcnt > 0) {
4947 1.1 dholland NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
4948 1.1 dholland if (notifybitsp != NULL)
4949 1.1 dholland *notifybitsp =
4950 1.1 dholland fxdr_unsigned(uint32_t, *tl);
4951 1.1 dholland }
4952 1.1 dholland *ndip = ndi;
4953 1.1 dholland } else
4954 1.1 dholland error = EPERM;
4955 1.1 dholland }
4956 1.1 dholland if (nd->nd_repstat != 0)
4957 1.1 dholland error = nd->nd_repstat;
4958 1.1 dholland nfsmout:
4959 1.1 dholland if (error != 0 && ndi != NULL)
4960 1.1 dholland nfscl_freedevinfo(ndi);
4961 1.1 dholland mbuf_freem(nd->nd_mrep);
4962 1.1 dholland return (error);
4963 1.1 dholland }
4964 1.1 dholland
4965 1.1 dholland /*
4966 1.1 dholland * Do the NFSv4.1 LayoutCommit.
4967 1.1 dholland */
4968 1.1 dholland int
4969 1.1 dholland nfsrpc_layoutcommit(struct nfsmount *nmp, uint8_t *fh, int fhlen, int reclaim,
4970 1.1 dholland uint64_t off, uint64_t len, uint64_t lastbyte, nfsv4stateid_t *stateidp,
4971 1.1 dholland int layouttype, int layoutupdatecnt, uint8_t *layp, struct ucred *cred,
4972 1.1 dholland NFSPROC_T *p, void *stuff)
4973 1.1 dholland {
4974 1.1 dholland uint32_t *tl;
4975 1.1 dholland struct nfsrv_descript nfsd, *nd = &nfsd;
4976 1.1 dholland int error, outcnt, i;
4977 1.1 dholland uint8_t *cp;
4978 1.1 dholland
4979 1.1 dholland nfscl_reqstart(nd, NFSPROC_LAYOUTCOMMIT, nmp, fh, fhlen, NULL, NULL);
4980 1.1 dholland NFSM_BUILD(tl, uint32_t *, 5 * NFSX_UNSIGNED + 3 * NFSX_HYPER +
4981 1.1 dholland NFSX_STATEID);
4982 1.1 dholland txdr_hyper(off, tl);
4983 1.1 dholland tl += 2;
4984 1.1 dholland txdr_hyper(len, tl);
4985 1.1 dholland tl += 2;
4986 1.1 dholland if (reclaim != 0)
4987 1.1 dholland *tl++ = newnfs_true;
4988 1.1 dholland else
4989 1.1 dholland *tl++ = newnfs_false;
4990 1.1 dholland *tl++ = txdr_unsigned(stateidp->seqid);
4991 1.1 dholland *tl++ = stateidp->other[0];
4992 1.1 dholland *tl++ = stateidp->other[1];
4993 1.1 dholland *tl++ = stateidp->other[2];
4994 1.1 dholland *tl++ = newnfs_true;
4995 1.1 dholland if (lastbyte < off)
4996 1.1 dholland lastbyte = off;
4997 1.1 dholland else if (lastbyte >= (off + len))
4998 1.1 dholland lastbyte = off + len - 1;
4999 1.1 dholland txdr_hyper(lastbyte, tl);
5000 1.1 dholland tl += 2;
5001 1.1 dholland *tl++ = newnfs_false;
5002 1.1 dholland *tl++ = txdr_unsigned(layouttype);
5003 1.1 dholland *tl = txdr_unsigned(layoutupdatecnt);
5004 1.1 dholland if (layoutupdatecnt > 0) {
5005 1.1 dholland KASSERT(layouttype != NFSLAYOUT_NFSV4_1_FILES,
5006 1.1 dholland ("Must be nil for Files Layout"));
5007 1.1 dholland outcnt = NFSM_RNDUP(layoutupdatecnt);
5008 1.1 dholland NFSM_BUILD(cp, uint8_t *, outcnt);
5009 1.1 dholland NFSBCOPY(layp, cp, layoutupdatecnt);
5010 1.1 dholland cp += layoutupdatecnt;
5011 1.1 dholland for (i = 0; i < (outcnt - layoutupdatecnt); i++)
5012 1.1 dholland *cp++ = 0x0;
5013 1.1 dholland }
5014 1.1 dholland nd->nd_flag |= ND_USEGSSNAME;
5015 1.1 dholland error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
5016 1.1 dholland NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
5017 1.1 dholland if (error != 0)
5018 1.1 dholland return (error);
5019 1.1 dholland error = nd->nd_repstat;
5020 1.1 dholland mbuf_freem(nd->nd_mrep);
5021 1.1 dholland return (error);
5022 1.1 dholland }
5023 1.1 dholland
5024 1.1 dholland /*
5025 1.1 dholland * Do the NFSv4.1 LayoutReturn.
5026 1.1 dholland */
5027 1.1 dholland int
5028 1.1 dholland nfsrpc_layoutreturn(struct nfsmount *nmp, uint8_t *fh, int fhlen, int reclaim,
5029 1.1 dholland int layouttype, uint32_t iomode, int layoutreturn, uint64_t offset,
5030 1.1 dholland uint64_t len, nfsv4stateid_t *stateidp, int layoutcnt, uint32_t *layp,
5031 1.1 dholland struct ucred *cred, NFSPROC_T *p, void *stuff)
5032 1.1 dholland {
5033 1.1 dholland uint32_t *tl;
5034 1.1 dholland struct nfsrv_descript nfsd, *nd = &nfsd;
5035 1.1 dholland int error, outcnt, i;
5036 1.1 dholland uint8_t *cp;
5037 1.1 dholland
5038 1.1 dholland nfscl_reqstart(nd, NFSPROC_LAYOUTRETURN, nmp, fh, fhlen, NULL, NULL);
5039 1.1 dholland NFSM_BUILD(tl, uint32_t *, 4 * NFSX_UNSIGNED);
5040 1.1 dholland if (reclaim != 0)
5041 1.1 dholland *tl++ = newnfs_true;
5042 1.1 dholland else
5043 1.1 dholland *tl++ = newnfs_false;
5044 1.1 dholland *tl++ = txdr_unsigned(layouttype);
5045 1.1 dholland *tl++ = txdr_unsigned(iomode);
5046 1.1 dholland *tl = txdr_unsigned(layoutreturn);
5047 1.1 dholland if (layoutreturn == NFSLAYOUTRETURN_FILE) {
5048 1.1 dholland NFSM_BUILD(tl, uint32_t *, 2 * NFSX_HYPER + NFSX_STATEID +
5049 1.1 dholland NFSX_UNSIGNED);
5050 1.1 dholland txdr_hyper(offset, tl);
5051 1.1 dholland tl += 2;
5052 1.1 dholland txdr_hyper(len, tl);
5053 1.1 dholland tl += 2;
5054 1.1 dholland NFSCL_DEBUG(4, "layoutret stseq=%d\n", (int)stateidp->seqid);
5055 1.1 dholland *tl++ = txdr_unsigned(stateidp->seqid);
5056 1.1 dholland *tl++ = stateidp->other[0];
5057 1.1 dholland *tl++ = stateidp->other[1];
5058 1.1 dholland *tl++ = stateidp->other[2];
5059 1.1 dholland *tl = txdr_unsigned(layoutcnt);
5060 1.1 dholland if (layoutcnt > 0) {
5061 1.1 dholland outcnt = NFSM_RNDUP(layoutcnt);
5062 1.1 dholland NFSM_BUILD(cp, uint8_t *, outcnt);
5063 1.1 dholland NFSBCOPY(layp, cp, layoutcnt);
5064 1.1 dholland cp += layoutcnt;
5065 1.1 dholland for (i = 0; i < (outcnt - layoutcnt); i++)
5066 1.1 dholland *cp++ = 0x0;
5067 1.1 dholland }
5068 1.1 dholland }
5069 1.1 dholland nd->nd_flag |= ND_USEGSSNAME;
5070 1.1 dholland error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
5071 1.1 dholland NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
5072 1.1 dholland if (error != 0)
5073 1.1 dholland return (error);
5074 1.1 dholland if (nd->nd_repstat == 0) {
5075 1.1 dholland NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
5076 1.1 dholland if (*tl != 0) {
5077 1.1 dholland NFSM_DISSECT(tl, uint32_t *, NFSX_STATEID);
5078 1.1 dholland stateidp->seqid = fxdr_unsigned(uint32_t, *tl++);
5079 1.1 dholland stateidp->other[0] = *tl++;
5080 1.1 dholland stateidp->other[1] = *tl++;
5081 1.1 dholland stateidp->other[2] = *tl;
5082 1.1 dholland }
5083 1.1 dholland } else
5084 1.1 dholland error = nd->nd_repstat;
5085 1.1 dholland nfsmout:
5086 1.1 dholland mbuf_freem(nd->nd_mrep);
5087 1.1 dholland return (error);
5088 1.1 dholland }
5089 1.1 dholland
5090 1.1 dholland /*
5091 1.1 dholland * Acquire a layout and devinfo, if possible. The caller must have acquired
5092 1.1 dholland * a reference count on the nfsclclient structure before calling this.
5093 1.1 dholland * Return the layout in lypp with a reference count on it, if successful.
5094 1.1 dholland */
5095 1.1 dholland static int
5096 1.1 dholland nfsrpc_getlayout(struct nfsmount *nmp, vnode_t vp, struct nfsfh *nfhp,
5097 1.1 dholland int iomode, uint32_t *notifybitsp, nfsv4stateid_t *stateidp, uint64_t off,
5098 1.1 dholland struct nfscllayout **lypp, struct ucred *cred, NFSPROC_T *p)
5099 1.1 dholland {
5100 1.1 dholland struct nfscllayout *lyp;
5101 1.1 dholland struct nfsclflayout *flp, *tflp;
5102 1.1 dholland struct nfscldevinfo *dip;
5103 1.1 dholland struct nfsclflayouthead flh;
5104 1.1 dholland int error = 0, islocked, layoutlen, recalled, retonclose;
5105 1.1 dholland nfsv4stateid_t stateid;
5106 1.1 dholland
5107 1.1 dholland *lypp = NULL;
5108 1.1 dholland /*
5109 1.1 dholland * If lyp is returned non-NULL, there will be a refcnt (shared lock)
5110 1.1 dholland * on it, iff flp != NULL or a lock (exclusive lock) on it iff
5111 1.1 dholland * flp == NULL.
5112 1.1 dholland */
5113 1.1 dholland lyp = nfscl_getlayout(nmp->nm_clp, nfhp->nfh_fh, nfhp->nfh_len,
5114 1.1 dholland off, &flp, &recalled);
5115 1.1 dholland islocked = 0;
5116 1.1 dholland if (lyp == NULL || flp == NULL) {
5117 1.1 dholland if (recalled != 0)
5118 1.1 dholland return (EIO);
5119 1.1 dholland LIST_INIT(&flh);
5120 1.1 dholland layoutlen = NFSMNT_MDSSESSION(nmp)->nfsess_maxcache -
5121 1.1 dholland (NFSX_STATEID + 3 * NFSX_UNSIGNED);
5122 1.1 dholland if (lyp == NULL) {
5123 1.1 dholland stateid.seqid = 0;
5124 1.1 dholland stateid.other[0] = stateidp->other[0];
5125 1.1 dholland stateid.other[1] = stateidp->other[1];
5126 1.1 dholland stateid.other[2] = stateidp->other[2];
5127 1.1 dholland error = nfsrpc_layoutget(nmp, nfhp->nfh_fh,
5128 1.1 dholland nfhp->nfh_len, iomode, (uint64_t)0, INT64_MAX,
5129 1.1 dholland (uint64_t)0, layoutlen, &stateid, &retonclose,
5130 1.1 dholland &flh, cred, p, NULL);
5131 1.1 dholland } else {
5132 1.1 dholland islocked = 1;
5133 1.1 dholland stateid.seqid = lyp->nfsly_stateid.seqid;
5134 1.1 dholland stateid.other[0] = lyp->nfsly_stateid.other[0];
5135 1.1 dholland stateid.other[1] = lyp->nfsly_stateid.other[1];
5136 1.1 dholland stateid.other[2] = lyp->nfsly_stateid.other[2];
5137 1.1 dholland error = nfsrpc_layoutget(nmp, nfhp->nfh_fh,
5138 1.1 dholland nfhp->nfh_len, iomode, off, INT64_MAX,
5139 1.1 dholland (uint64_t)0, layoutlen, &stateid, &retonclose,
5140 1.1 dholland &flh, cred, p, NULL);
5141 1.1 dholland }
5142 1.1 dholland if (error == 0)
5143 1.1 dholland LIST_FOREACH(tflp, &flh, nfsfl_list) {
5144 1.1 dholland error = nfscl_adddevinfo(nmp, NULL, tflp);
5145 1.1 dholland if (error != 0) {
5146 1.1 dholland error = nfsrpc_getdeviceinfo(nmp,
5147 1.1 dholland tflp->nfsfl_dev,
5148 1.1 dholland NFSLAYOUT_NFSV4_1_FILES,
5149 1.1 dholland notifybitsp, &dip, cred, p);
5150 1.1 dholland if (error != 0)
5151 1.1 dholland break;
5152 1.1 dholland error = nfscl_adddevinfo(nmp, dip,
5153 1.1 dholland tflp);
5154 1.1 dholland if (error != 0)
5155 1.1 dholland printf(
5156 1.1 dholland "getlayout: cannot add\n");
5157 1.1 dholland }
5158 1.1 dholland }
5159 1.1 dholland if (error == 0) {
5160 1.1 dholland /*
5161 1.1 dholland * nfscl_layout() always returns with the nfsly_lock
5162 1.1 dholland * set to a refcnt (shared lock).
5163 1.1 dholland */
5164 1.1 dholland error = nfscl_layout(nmp, vp, nfhp->nfh_fh,
5165 1.1 dholland nfhp->nfh_len, &stateid, retonclose, &flh, &lyp,
5166 1.1 dholland cred, p);
5167 1.1 dholland if (error == 0)
5168 1.1 dholland *lypp = lyp;
5169 1.1 dholland } else if (islocked != 0)
5170 1.1 dholland nfsv4_unlock(&lyp->nfsly_lock, 0);
5171 1.1 dholland } else
5172 1.1 dholland *lypp = lyp;
5173 1.1 dholland return (error);
5174 1.1 dholland }
5175 1.1 dholland
5176 1.1 dholland /*
5177 1.1 dholland * Do a TCP connection plus exchange id and create session.
5178 1.1 dholland * If successful, a "struct nfsclds" is linked into the list for the
5179 1.1 dholland * mount point and a pointer to it is returned.
5180 1.1 dholland */
5181 1.1 dholland static int
5182 1.1 dholland nfsrpc_fillsa(struct nfsmount *nmp, struct sockaddr_storage *ssp,
5183 1.1 dholland struct nfsclds **dspp, NFSPROC_T *p)
5184 1.1 dholland {
5185 1.1 dholland struct sockaddr_in *msad, *sad, *ssd;
5186 1.1 dholland struct sockaddr_in6 *msad6, *sad6, *ssd6;
5187 1.1 dholland struct nfsclclient *clp;
5188 1.1 dholland struct nfssockreq *nrp;
5189 1.1 dholland struct nfsclds *dsp, *tdsp;
5190 1.1 dholland int error;
5191 1.1 dholland enum nfsclds_state retv;
5192 1.1 dholland uint32_t sequenceid;
5193 1.1 dholland
5194 1.1 dholland KASSERT(nmp->nm_sockreq.nr_cred != NULL,
5195 1.1 dholland ("nfsrpc_fillsa: NULL nr_cred"));
5196 1.1 dholland NFSLOCKCLSTATE();
5197 1.1 dholland clp = nmp->nm_clp;
5198 1.1 dholland NFSUNLOCKCLSTATE();
5199 1.1 dholland if (clp == NULL)
5200 1.1 dholland return (EPERM);
5201 1.1 dholland if (ssp->ss_family == AF_INET) {
5202 1.1 dholland ssd = (struct sockaddr_in *)ssp;
5203 1.1 dholland NFSLOCKMNT(nmp);
5204 1.1 dholland
5205 1.1 dholland /*
5206 1.1 dholland * Check to see if we already have a session for this
5207 1.1 dholland * address that is usable for a DS.
5208 1.1 dholland * Note that the MDS's address is in a different place
5209 1.1 dholland * than the sessions already acquired for DS's.
5210 1.1 dholland */
5211 1.1 dholland msad = (struct sockaddr_in *)nmp->nm_sockreq.nr_nam;
5212 1.1 dholland tdsp = TAILQ_FIRST(&nmp->nm_sess);
5213 1.1 dholland while (tdsp != NULL) {
5214 1.1 dholland if (msad != NULL && msad->sin_family == AF_INET &&
5215 1.1 dholland ssd->sin_addr.s_addr == msad->sin_addr.s_addr &&
5216 1.1 dholland ssd->sin_port == msad->sin_port &&
5217 1.1 dholland (tdsp->nfsclds_flags & NFSCLDS_DS) != 0) {
5218 1.1 dholland *dspp = tdsp;
5219 1.1 dholland NFSUNLOCKMNT(nmp);
5220 1.1 dholland NFSCL_DEBUG(4, "fnd same addr\n");
5221 1.1 dholland return (0);
5222 1.1 dholland }
5223 1.1 dholland tdsp = TAILQ_NEXT(tdsp, nfsclds_list);
5224 1.1 dholland if (tdsp != NULL && tdsp->nfsclds_sockp != NULL)
5225 1.1 dholland msad = (struct sockaddr_in *)
5226 1.1 dholland tdsp->nfsclds_sockp->nr_nam;
5227 1.1 dholland else
5228 1.1 dholland msad = NULL;
5229 1.1 dholland }
5230 1.1 dholland NFSUNLOCKMNT(nmp);
5231 1.1 dholland
5232 1.1 dholland /* No IP address match, so look for new/trunked one. */
5233 1.1 dholland sad = malloc(sizeof(*sad), M_SONAME, M_WAITOK | M_ZERO);
5234 1.1 dholland sad->sin_len = sizeof(*sad);
5235 1.1 dholland sad->sin_family = AF_INET;
5236 1.1 dholland sad->sin_port = ssd->sin_port;
5237 1.1 dholland sad->sin_addr.s_addr = ssd->sin_addr.s_addr;
5238 1.1 dholland nrp = malloc(sizeof(*nrp), M_NFSSOCKREQ, M_WAITOK | M_ZERO);
5239 1.1 dholland nrp->nr_nam = (struct sockaddr *)sad;
5240 1.1 dholland } else if (ssp->ss_family == AF_INET6) {
5241 1.1 dholland ssd6 = (struct sockaddr_in6 *)ssp;
5242 1.1 dholland NFSLOCKMNT(nmp);
5243 1.1 dholland
5244 1.1 dholland /*
5245 1.1 dholland * Check to see if we already have a session for this
5246 1.1 dholland * address that is usable for a DS.
5247 1.1 dholland * Note that the MDS's address is in a different place
5248 1.1 dholland * than the sessions already acquired for DS's.
5249 1.1 dholland */
5250 1.1 dholland msad6 = (struct sockaddr_in6 *)nmp->nm_sockreq.nr_nam;
5251 1.1 dholland tdsp = TAILQ_FIRST(&nmp->nm_sess);
5252 1.1 dholland while (tdsp != NULL) {
5253 1.1 dholland if (msad6 != NULL && msad6->sin6_family == AF_INET6 &&
5254 1.1 dholland IN6_ARE_ADDR_EQUAL(&ssd6->sin6_addr,
5255 1.1 dholland &msad6->sin6_addr) &&
5256 1.1 dholland ssd6->sin6_port == msad6->sin6_port &&
5257 1.1 dholland (tdsp->nfsclds_flags & NFSCLDS_DS) != 0) {
5258 1.1 dholland *dspp = tdsp;
5259 1.1 dholland NFSUNLOCKMNT(nmp);
5260 1.1 dholland return (0);
5261 1.1 dholland }
5262 1.1 dholland tdsp = TAILQ_NEXT(tdsp, nfsclds_list);
5263 1.1 dholland if (tdsp != NULL && tdsp->nfsclds_sockp != NULL)
5264 1.1 dholland msad6 = (struct sockaddr_in6 *)
5265 1.1 dholland tdsp->nfsclds_sockp->nr_nam;
5266 1.1 dholland else
5267 1.1 dholland msad6 = NULL;
5268 1.1 dholland }
5269 1.1 dholland NFSUNLOCKMNT(nmp);
5270 1.1 dholland
5271 1.1 dholland /* No IP address match, so look for new/trunked one. */
5272 1.1 dholland sad6 = malloc(sizeof(*sad6), M_SONAME, M_WAITOK | M_ZERO);
5273 1.1 dholland sad6->sin6_len = sizeof(*sad6);
5274 1.1 dholland sad6->sin6_family = AF_INET6;
5275 1.1 dholland sad6->sin6_port = ssd6->sin6_port;
5276 1.1 dholland NFSBCOPY(&ssd6->sin6_addr, &sad6->sin6_addr,
5277 1.1 dholland sizeof(struct in6_addr));
5278 1.1 dholland nrp = malloc(sizeof(*nrp), M_NFSSOCKREQ, M_WAITOK | M_ZERO);
5279 1.1 dholland nrp->nr_nam = (struct sockaddr *)sad6;
5280 1.1 dholland } else
5281 1.1 dholland return (EPERM);
5282 1.1 dholland
5283 1.1 dholland nrp->nr_sotype = SOCK_STREAM;
5284 1.1 dholland mtx_init(&nrp->nr_mtx, "nfssock", NULL, MTX_DEF);
5285 1.1 dholland nrp->nr_prog = NFS_PROG;
5286 1.1 dholland nrp->nr_vers = NFS_VER4;
5287 1.1 dholland
5288 1.1 dholland /*
5289 1.1 dholland * Use the credentials that were used for the mount, which are
5290 1.1 dholland * in nmp->nm_sockreq.nr_cred for newnfs_connect() etc.
5291 1.1 dholland * Ref. counting the credentials with crhold() is probably not
5292 1.1 dholland * necessary, since nm_sockreq.nr_cred won't be crfree()'d until
5293 1.1 dholland * unmount, but I did it anyhow.
5294 1.1 dholland */
5295 1.1 dholland nrp->nr_cred = crhold(nmp->nm_sockreq.nr_cred);
5296 1.1 dholland error = newnfs_connect(nmp, nrp, NULL, p, 0);
5297 1.1 dholland NFSCL_DEBUG(3, "DS connect=%d\n", error);
5298 1.1 dholland
5299 1.1 dholland /* Now, do the exchangeid and create session. */
5300 1.1 dholland if (error == 0)
5301 1.1 dholland error = nfsrpc_exchangeid(nmp, clp, nrp, NFSV4EXCH_USEPNFSDS,
5302 1.1 dholland &dsp, nrp->nr_cred, p);
5303 1.1 dholland NFSCL_DEBUG(3, "DS exchangeid=%d\n", error);
5304 1.1 dholland if (error == 0) {
5305 1.1 dholland dsp->nfsclds_sockp = nrp;
5306 1.1 dholland NFSLOCKMNT(nmp);
5307 1.1 dholland retv = nfscl_getsameserver(nmp, dsp, &tdsp);
5308 1.1 dholland NFSCL_DEBUG(3, "getsame ret=%d\n", retv);
5309 1.1 dholland if (retv == NFSDSP_USETHISSESSION) {
5310 1.1 dholland NFSUNLOCKMNT(nmp);
5311 1.1 dholland /*
5312 1.1 dholland * If there is already a session for this server,
5313 1.1 dholland * use it.
5314 1.1 dholland */
5315 1.1 dholland (void)newnfs_disconnect(nrp);
5316 1.1 dholland nfscl_freenfsclds(dsp);
5317 1.1 dholland *dspp = tdsp;
5318 1.1 dholland return (0);
5319 1.1 dholland }
5320 1.1 dholland if (retv == NFSDSP_SEQTHISSESSION)
5321 1.1 dholland sequenceid = tdsp->nfsclds_sess.nfsess_sequenceid;
5322 1.1 dholland else
5323 1.1 dholland sequenceid = dsp->nfsclds_sess.nfsess_sequenceid;
5324 1.1 dholland NFSUNLOCKMNT(nmp);
5325 1.1 dholland error = nfsrpc_createsession(nmp, &dsp->nfsclds_sess,
5326 1.1 dholland nrp, sequenceid, 0, nrp->nr_cred, p);
5327 1.1 dholland NFSCL_DEBUG(3, "DS createsess=%d\n", error);
5328 1.1 dholland } else {
5329 1.1 dholland NFSFREECRED(nrp->nr_cred);
5330 1.1 dholland NFSFREEMUTEX(&nrp->nr_mtx);
5331 1.1 dholland free(nrp->nr_nam, M_SONAME);
5332 1.1 dholland free(nrp, M_NFSSOCKREQ);
5333 1.1 dholland }
5334 1.1 dholland if (error == 0) {
5335 1.1 dholland NFSCL_DEBUG(3, "add DS session\n");
5336 1.1 dholland /*
5337 1.1 dholland * Put it at the end of the list. That way the list
5338 1.1 dholland * is ordered by when the entry was added. This matters
5339 1.1 dholland * since the one done first is the one that should be
5340 1.1 dholland * used for sequencid'ing any subsequent create sessions.
5341 1.1 dholland */
5342 1.1 dholland NFSLOCKMNT(nmp);
5343 1.1 dholland TAILQ_INSERT_TAIL(&nmp->nm_sess, dsp, nfsclds_list);
5344 1.1 dholland NFSUNLOCKMNT(nmp);
5345 1.1 dholland *dspp = dsp;
5346 1.1 dholland } else if (dsp != NULL)
5347 1.1 dholland nfscl_freenfsclds(dsp);
5348 1.1 dholland return (error);
5349 1.1 dholland }
5350 1.1 dholland
5351 1.1 dholland /*
5352 1.1 dholland * Do the NFSv4.1 Reclaim Complete.
5353 1.1 dholland */
5354 1.1 dholland int
5355 1.1 dholland nfsrpc_reclaimcomplete(struct nfsmount *nmp, struct ucred *cred, NFSPROC_T *p)
5356 1.1 dholland {
5357 1.1 dholland uint32_t *tl;
5358 1.1 dholland struct nfsrv_descript nfsd;
5359 1.1 dholland struct nfsrv_descript *nd = &nfsd;
5360 1.1 dholland int error;
5361 1.1 dholland
5362 1.1 dholland nfscl_reqstart(nd, NFSPROC_RECLAIMCOMPL, nmp, NULL, 0, NULL, NULL);
5363 1.1 dholland NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
5364 1.1 dholland *tl = newnfs_false;
5365 1.1 dholland nd->nd_flag |= ND_USEGSSNAME;
5366 1.1 dholland error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
5367 1.1 dholland NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
5368 1.1 dholland if (error != 0)
5369 1.1 dholland return (error);
5370 1.1 dholland error = nd->nd_repstat;
5371 1.1 dholland mbuf_freem(nd->nd_mrep);
5372 1.1 dholland return (error);
5373 1.1 dholland }
5374 1.1 dholland
5375 1.1 dholland /*
5376 1.1 dholland * Initialize the slot tables for a session.
5377 1.1 dholland */
5378 1.1 dholland static void
5379 1.1 dholland nfscl_initsessionslots(struct nfsclsession *sep)
5380 1.1 dholland {
5381 1.1 dholland int i;
5382 1.1 dholland
5383 1.1 dholland for (i = 0; i < NFSV4_CBSLOTS; i++) {
5384 1.1 dholland if (sep->nfsess_cbslots[i].nfssl_reply != NULL)
5385 1.1 dholland m_freem(sep->nfsess_cbslots[i].nfssl_reply);
5386 1.1 dholland NFSBZERO(&sep->nfsess_cbslots[i], sizeof(struct nfsslot));
5387 1.1 dholland }
5388 1.1 dholland for (i = 0; i < 64; i++)
5389 1.1 dholland sep->nfsess_slotseq[i] = 0;
5390 1.1 dholland sep->nfsess_slots = 0;
5391 1.1 dholland }
5392 1.1 dholland
5393 1.1 dholland /*
5394 1.1 dholland * Called to try and do an I/O operation via an NFSv4.1 Data Server (DS).
5395 1.1 dholland */
5396 1.1 dholland int
5397 1.1 dholland nfscl_doiods(vnode_t vp, struct uio *uiop, int *iomode, int *must_commit,
5398 1.1 dholland uint32_t rwaccess, struct ucred *cred, NFSPROC_T *p)
5399 1.1 dholland {
5400 1.1 dholland struct nfsnode *np = VTONFS(vp);
5401 1.1 dholland struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
5402 1.1 dholland struct nfscllayout *layp;
5403 1.1 dholland struct nfscldevinfo *dip;
5404 1.1 dholland struct nfsclflayout *rflp;
5405 1.1 dholland nfsv4stateid_t stateid;
5406 1.1 dholland struct ucred *newcred;
5407 1.1 dholland uint64_t lastbyte, len, off, oresid, xfer;
5408 1.1 dholland int eof, error, iolaymode, recalled;
5409 1.1 dholland void *lckp;
5410 1.1 dholland
5411 1.1 dholland if (!NFSHASPNFS(nmp) || nfscl_enablecallb == 0 || nfs_numnfscbd == 0 ||
5412 1.1 dholland (np->n_flag & NNOLAYOUT) != 0)
5413 1.1 dholland return (EIO);
5414 1.1 dholland /* Now, get a reference cnt on the clientid for this mount. */
5415 1.1 dholland if (nfscl_getref(nmp) == 0)
5416 1.1 dholland return (EIO);
5417 1.1 dholland
5418 1.1 dholland /* Find an appropriate stateid. */
5419 1.1 dholland newcred = NFSNEWCRED(cred);
5420 1.1 dholland error = nfscl_getstateid(vp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len,
5421 1.1 dholland rwaccess, 1, newcred, p, &stateid, &lckp);
5422 1.1 dholland if (error != 0) {
5423 1.1 dholland NFSFREECRED(newcred);
5424 1.1 dholland nfscl_relref(nmp);
5425 1.1 dholland return (error);
5426 1.1 dholland }
5427 1.1 dholland /* Search for a layout for this file. */
5428 1.1 dholland off = uiop->uio_offset;
5429 1.1 dholland layp = nfscl_getlayout(nmp->nm_clp, np->n_fhp->nfh_fh,
5430 1.1 dholland np->n_fhp->nfh_len, off, &rflp, &recalled);
5431 1.1 dholland if (layp == NULL || rflp == NULL) {
5432 1.1 dholland if (recalled != 0) {
5433 1.1 dholland NFSFREECRED(newcred);
5434 1.1 dholland nfscl_relref(nmp);
5435 1.1 dholland return (EIO);
5436 1.1 dholland }
5437 1.1 dholland if (layp != NULL) {
5438 1.1 dholland nfscl_rellayout(layp, (rflp == NULL) ? 1 : 0);
5439 1.1 dholland layp = NULL;
5440 1.1 dholland }
5441 1.1 dholland /* Try and get a Layout, if it is supported. */
5442 1.1 dholland if (rwaccess == NFSV4OPEN_ACCESSWRITE ||
5443 1.1 dholland (np->n_flag & NWRITEOPENED) != 0)
5444 1.1 dholland iolaymode = NFSLAYOUTIOMODE_RW;
5445 1.1 dholland else
5446 1.1 dholland iolaymode = NFSLAYOUTIOMODE_READ;
5447 1.1 dholland error = nfsrpc_getlayout(nmp, vp, np->n_fhp, iolaymode,
5448 1.1 dholland NULL, &stateid, off, &layp, newcred, p);
5449 1.1 dholland if (error != 0) {
5450 1.1 dholland NFSLOCKNODE(np);
5451 1.1 dholland np->n_flag |= NNOLAYOUT;
5452 1.1 dholland NFSUNLOCKNODE(np);
5453 1.1 dholland if (lckp != NULL)
5454 1.1 dholland nfscl_lockderef(lckp);
5455 1.1 dholland NFSFREECRED(newcred);
5456 1.1 dholland if (layp != NULL)
5457 1.1 dholland nfscl_rellayout(layp, 0);
5458 1.1 dholland nfscl_relref(nmp);
5459 1.1 dholland return (error);
5460 1.1 dholland }
5461 1.1 dholland }
5462 1.1 dholland
5463 1.1 dholland /*
5464 1.1 dholland * Loop around finding a layout that works for the first part of
5465 1.1 dholland * this I/O operation, and then call the function that actually
5466 1.1 dholland * does the RPC.
5467 1.1 dholland */
5468 1.1 dholland eof = 0;
5469 1.1 dholland len = (uint64_t)uiop->uio_resid;
5470 1.1 dholland while (len > 0 && error == 0 && eof == 0) {
5471 1.1 dholland off = uiop->uio_offset;
5472 1.1 dholland error = nfscl_findlayoutforio(layp, off, rwaccess, &rflp);
5473 1.1 dholland if (error == 0) {
5474 1.1 dholland oresid = xfer = (uint64_t)uiop->uio_resid;
5475 1.1 dholland if (xfer > (rflp->nfsfl_end - rflp->nfsfl_off))
5476 1.1 dholland xfer = rflp->nfsfl_end - rflp->nfsfl_off;
5477 1.1 dholland dip = nfscl_getdevinfo(nmp->nm_clp, rflp->nfsfl_dev,
5478 1.1 dholland rflp->nfsfl_devp);
5479 1.1 dholland if (dip != NULL) {
5480 1.1 dholland error = nfscl_doflayoutio(vp, uiop, iomode,
5481 1.1 dholland must_commit, &eof, &stateid, rwaccess, dip,
5482 1.1 dholland layp, rflp, off, xfer, newcred, p);
5483 1.1 dholland nfscl_reldevinfo(dip);
5484 1.1 dholland lastbyte = off + xfer - 1;
5485 1.1 dholland if (error == 0) {
5486 1.1 dholland NFSLOCKCLSTATE();
5487 1.1 dholland if (lastbyte > layp->nfsly_lastbyte)
5488 1.1 dholland layp->nfsly_lastbyte = lastbyte;
5489 1.1 dholland NFSUNLOCKCLSTATE();
5490 1.1 dholland }
5491 1.1 dholland } else
5492 1.1 dholland error = EIO;
5493 1.1 dholland if (error == 0)
5494 1.1 dholland len -= (oresid - (uint64_t)uiop->uio_resid);
5495 1.1 dholland }
5496 1.1 dholland }
5497 1.1 dholland if (lckp != NULL)
5498 1.1 dholland nfscl_lockderef(lckp);
5499 1.1 dholland NFSFREECRED(newcred);
5500 1.1 dholland nfscl_rellayout(layp, 0);
5501 1.1 dholland nfscl_relref(nmp);
5502 1.1 dholland return (error);
5503 1.1 dholland }
5504 1.1 dholland
5505 1.1 dholland /*
5506 1.1 dholland * Find a file layout that will handle the first bytes of the requested
5507 1.1 dholland * range and return the information from it needed to to the I/O operation.
5508 1.1 dholland */
5509 1.1 dholland int
5510 1.1 dholland nfscl_findlayoutforio(struct nfscllayout *lyp, uint64_t off, uint32_t rwaccess,
5511 1.1 dholland struct nfsclflayout **retflpp)
5512 1.1 dholland {
5513 1.1 dholland struct nfsclflayout *flp, *nflp, *rflp;
5514 1.1 dholland uint32_t rw;
5515 1.1 dholland
5516 1.1 dholland rflp = NULL;
5517 1.1 dholland rw = rwaccess;
5518 1.1 dholland /* For reading, do the Read list first and then the Write list. */
5519 1.1 dholland do {
5520 1.1 dholland if (rw == NFSV4OPEN_ACCESSREAD)
5521 1.1 dholland flp = LIST_FIRST(&lyp->nfsly_flayread);
5522 1.1 dholland else
5523 1.1 dholland flp = LIST_FIRST(&lyp->nfsly_flayrw);
5524 1.1 dholland while (flp != NULL) {
5525 1.1 dholland nflp = LIST_NEXT(flp, nfsfl_list);
5526 1.1 dholland if (flp->nfsfl_off > off)
5527 1.1 dholland break;
5528 1.1 dholland if (flp->nfsfl_end > off &&
5529 1.1 dholland (rflp == NULL || rflp->nfsfl_end < flp->nfsfl_end))
5530 1.1 dholland rflp = flp;
5531 1.1 dholland flp = nflp;
5532 1.1 dholland }
5533 1.1 dholland if (rw == NFSV4OPEN_ACCESSREAD)
5534 1.1 dholland rw = NFSV4OPEN_ACCESSWRITE;
5535 1.1 dholland else
5536 1.1 dholland rw = 0;
5537 1.1 dholland } while (rw != 0);
5538 1.1 dholland if (rflp != NULL) {
5539 1.1 dholland /* This one covers the most bytes starting at off. */
5540 1.1 dholland *retflpp = rflp;
5541 1.1 dholland return (0);
5542 1.1 dholland }
5543 1.1 dholland return (EIO);
5544 1.1 dholland }
5545 1.1 dholland
5546 1.1 dholland /*
5547 1.1 dholland * Do I/O using an NFSv4.1 file layout.
5548 1.1 dholland */
5549 1.1 dholland static int
5550 1.1 dholland nfscl_doflayoutio(vnode_t vp, struct uio *uiop, int *iomode, int *must_commit,
5551 1.1 dholland int *eofp, nfsv4stateid_t *stateidp, int rwflag, struct nfscldevinfo *dp,
5552 1.1 dholland struct nfscllayout *lyp, struct nfsclflayout *flp, uint64_t off,
5553 1.1 dholland uint64_t len, struct ucred *cred, NFSPROC_T *p)
5554 1.1 dholland {
5555 1.1 dholland uint64_t io_off, rel_off, stripe_unit_size, transfer, xfer;
5556 1.1 dholland int commit_thru_mds, error = 0, stripe_index, stripe_pos;
5557 1.1 dholland struct nfsnode *np;
5558 1.1 dholland struct nfsfh *fhp;
5559 1.1 dholland struct nfsclds **dspp;
5560 1.1 dholland
5561 1.1 dholland np = VTONFS(vp);
5562 1.1 dholland rel_off = off - flp->nfsfl_patoff;
5563 1.1 dholland stripe_unit_size = (flp->nfsfl_util >> 6) & 0x3ffffff;
5564 1.1 dholland stripe_pos = (rel_off / stripe_unit_size + flp->nfsfl_stripe1) %
5565 1.1 dholland dp->nfsdi_stripecnt;
5566 1.1 dholland transfer = stripe_unit_size - (rel_off % stripe_unit_size);
5567 1.1 dholland
5568 1.1 dholland /* Loop around, doing I/O for each stripe unit. */
5569 1.1 dholland while (len > 0 && error == 0) {
5570 1.1 dholland stripe_index = nfsfldi_stripeindex(dp, stripe_pos);
5571 1.1 dholland dspp = nfsfldi_addr(dp, stripe_index);
5572 1.1 dholland if (len > transfer)
5573 1.1 dholland xfer = transfer;
5574 1.1 dholland else
5575 1.1 dholland xfer = len;
5576 1.1 dholland if ((flp->nfsfl_util & NFSFLAYUTIL_DENSE) != 0) {
5577 1.1 dholland /* Dense layout. */
5578 1.1 dholland if (stripe_pos >= flp->nfsfl_fhcnt)
5579 1.1 dholland return (EIO);
5580 1.1 dholland fhp = flp->nfsfl_fh[stripe_pos];
5581 1.1 dholland io_off = (rel_off / (stripe_unit_size *
5582 1.1 dholland dp->nfsdi_stripecnt)) * stripe_unit_size +
5583 1.1 dholland rel_off % stripe_unit_size;
5584 1.1 dholland } else {
5585 1.1 dholland /* Sparse layout. */
5586 1.1 dholland if (flp->nfsfl_fhcnt > 1) {
5587 1.1 dholland if (stripe_index >= flp->nfsfl_fhcnt)
5588 1.1 dholland return (EIO);
5589 1.1 dholland fhp = flp->nfsfl_fh[stripe_index];
5590 1.1 dholland } else if (flp->nfsfl_fhcnt == 1)
5591 1.1 dholland fhp = flp->nfsfl_fh[0];
5592 1.1 dholland else
5593 1.1 dholland fhp = np->n_fhp;
5594 1.1 dholland io_off = off;
5595 1.1 dholland }
5596 1.1 dholland if ((flp->nfsfl_util & NFSFLAYUTIL_COMMIT_THRU_MDS) != 0)
5597 1.1 dholland commit_thru_mds = 1;
5598 1.1 dholland else
5599 1.1 dholland commit_thru_mds = 0;
5600 1.1 dholland if (rwflag == FREAD)
5601 1.1 dholland error = nfsrpc_readds(vp, uiop, stateidp, eofp, *dspp,
5602 1.1 dholland io_off, xfer, fhp, cred, p);
5603 1.1 dholland else {
5604 1.1 dholland error = nfsrpc_writeds(vp, uiop, iomode, must_commit,
5605 1.1 dholland stateidp, *dspp, io_off, xfer, fhp, commit_thru_mds,
5606 1.1 dholland cred, p);
5607 1.1 dholland if (error == 0) {
5608 1.1 dholland NFSLOCKCLSTATE();
5609 1.1 dholland lyp->nfsly_flags |= NFSLY_WRITTEN;
5610 1.1 dholland NFSUNLOCKCLSTATE();
5611 1.1 dholland }
5612 1.1 dholland }
5613 1.1 dholland if (error == 0) {
5614 1.1 dholland transfer = stripe_unit_size;
5615 1.1 dholland stripe_pos = (stripe_pos + 1) % dp->nfsdi_stripecnt;
5616 1.1 dholland len -= xfer;
5617 1.1 dholland off += xfer;
5618 1.1 dholland }
5619 1.1 dholland }
5620 1.1 dholland return (error);
5621 1.1 dholland }
5622 1.1 dholland
5623 1.1 dholland /*
5624 1.1 dholland * The actual read RPC done to a DS.
5625 1.1 dholland */
5626 1.1 dholland static int
5627 1.1 dholland nfsrpc_readds(vnode_t vp, struct uio *uiop, nfsv4stateid_t *stateidp, int *eofp,
5628 1.1 dholland struct nfsclds *dsp, uint64_t io_off, int len, struct nfsfh *fhp,
5629 1.1 dholland struct ucred *cred, NFSPROC_T *p)
5630 1.1 dholland {
5631 1.1 dholland uint32_t *tl;
5632 1.1 dholland int error, retlen;
5633 1.1 dholland struct nfsrv_descript nfsd;
5634 1.1 dholland struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
5635 1.1 dholland struct nfsrv_descript *nd = &nfsd;
5636 1.1 dholland struct nfssockreq *nrp;
5637 1.1 dholland
5638 1.1 dholland nd->nd_mrep = NULL;
5639 1.1 dholland nfscl_reqstart(nd, NFSPROC_READDS, nmp, fhp->nfh_fh, fhp->nfh_len,
5640 1.1 dholland NULL, &dsp->nfsclds_sess);
5641 1.1 dholland nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSEQIDZERO);
5642 1.1 dholland NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED * 3);
5643 1.1 dholland txdr_hyper(io_off, tl);
5644 1.1 dholland *(tl + 2) = txdr_unsigned(len);
5645 1.1 dholland nrp = dsp->nfsclds_sockp;
5646 1.1 dholland if (nrp == NULL)
5647 1.1 dholland /* If NULL, use the MDS socket. */
5648 1.1 dholland nrp = &nmp->nm_sockreq;
5649 1.1 dholland error = newnfs_request(nd, nmp, NULL, nrp, vp, p, cred,
5650 1.1 dholland NFS_PROG, NFS_VER4, NULL, 1, NULL, &dsp->nfsclds_sess);
5651 1.1 dholland if (error != 0)
5652 1.1 dholland return (error);
5653 1.1 dholland if (nd->nd_repstat != 0) {
5654 1.1 dholland error = nd->nd_repstat;
5655 1.1 dholland goto nfsmout;
5656 1.1 dholland }
5657 1.1 dholland NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
5658 1.1 dholland *eofp = fxdr_unsigned(int, *tl);
5659 1.1 dholland NFSM_STRSIZ(retlen, len);
5660 1.1 dholland error = nfsm_mbufuio(nd, uiop, retlen);
5661 1.1 dholland nfsmout:
5662 1.1 dholland if (nd->nd_mrep != NULL)
5663 1.1 dholland mbuf_freem(nd->nd_mrep);
5664 1.1 dholland return (error);
5665 1.1 dholland }
5666 1.1 dholland
5667 1.1 dholland /*
5668 1.1 dholland * The actual write RPC done to a DS.
5669 1.1 dholland */
5670 1.1 dholland static int
5671 1.1 dholland nfsrpc_writeds(vnode_t vp, struct uio *uiop, int *iomode, int *must_commit,
5672 1.1 dholland nfsv4stateid_t *stateidp, struct nfsclds *dsp, uint64_t io_off, int len,
5673 1.1 dholland struct nfsfh *fhp, int commit_thru_mds, struct ucred *cred, NFSPROC_T *p)
5674 1.1 dholland {
5675 1.1 dholland uint32_t *tl;
5676 1.1 dholland struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
5677 1.1 dholland int error, rlen, commit, committed = NFSWRITE_FILESYNC;
5678 1.1 dholland int32_t backup;
5679 1.1 dholland struct nfsrv_descript nfsd;
5680 1.1 dholland struct nfsrv_descript *nd = &nfsd;
5681 1.1 dholland struct nfssockreq *nrp;
5682 1.1 dholland
5683 1.1 dholland KASSERT(uiop->uio_iovcnt == 1, ("nfs: writerpc iovcnt > 1"));
5684 1.1 dholland nd->nd_mrep = NULL;
5685 1.1 dholland nfscl_reqstart(nd, NFSPROC_WRITEDS, nmp, fhp->nfh_fh, fhp->nfh_len,
5686 1.1 dholland NULL, &dsp->nfsclds_sess);
5687 1.1 dholland nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSEQIDZERO);
5688 1.1 dholland NFSM_BUILD(tl, uint32_t *, NFSX_HYPER + 2 * NFSX_UNSIGNED);
5689 1.1 dholland txdr_hyper(io_off, tl);
5690 1.1 dholland tl += 2;
5691 1.1 dholland *tl++ = txdr_unsigned(*iomode);
5692 1.1 dholland *tl = txdr_unsigned(len);
5693 1.1 dholland nfsm_uiombuf(nd, uiop, len);
5694 1.1 dholland nrp = dsp->nfsclds_sockp;
5695 1.1 dholland if (nrp == NULL)
5696 1.1 dholland /* If NULL, use the MDS socket. */
5697 1.1 dholland nrp = &nmp->nm_sockreq;
5698 1.1 dholland error = newnfs_request(nd, nmp, NULL, nrp, vp, p, cred,
5699 1.1 dholland NFS_PROG, NFS_VER4, NULL, 1, NULL, &dsp->nfsclds_sess);
5700 1.1 dholland if (error != 0)
5701 1.1 dholland return (error);
5702 1.1 dholland if (nd->nd_repstat != 0) {
5703 1.1 dholland /*
5704 1.1 dholland * In case the rpc gets retried, roll
5705 1.1 dholland * the uio fileds changed by nfsm_uiombuf()
5706 1.1 dholland * back.
5707 1.1 dholland */
5708 1.1 dholland uiop->uio_offset -= len;
5709 1.1 dholland uio_uio_resid_add(uiop, len);
5710 1.1 dholland uio_iov_base_add(uiop, -len);
5711 1.1 dholland uio_iov_len_add(uiop, len);
5712 1.1 dholland error = nd->nd_repstat;
5713 1.1 dholland } else {
5714 1.1 dholland NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED + NFSX_VERF);
5715 1.1 dholland rlen = fxdr_unsigned(int, *tl++);
5716 1.1 dholland if (rlen == 0) {
5717 1.1 dholland error = NFSERR_IO;
5718 1.1 dholland goto nfsmout;
5719 1.1 dholland } else if (rlen < len) {
5720 1.1 dholland backup = len - rlen;
5721 1.1 dholland uio_iov_base_add(uiop, -(backup));
5722 1.1 dholland uio_iov_len_add(uiop, backup);
5723 1.1 dholland uiop->uio_offset -= backup;
5724 1.1 dholland uio_uio_resid_add(uiop, backup);
5725 1.1 dholland len = rlen;
5726 1.1 dholland }
5727 1.1 dholland commit = fxdr_unsigned(int, *tl++);
5728 1.1 dholland
5729 1.1 dholland /*
5730 1.1 dholland * Return the lowest committment level
5731 1.1 dholland * obtained by any of the RPCs.
5732 1.1 dholland */
5733 1.1 dholland if (committed == NFSWRITE_FILESYNC)
5734 1.1 dholland committed = commit;
5735 1.1 dholland else if (committed == NFSWRITE_DATASYNC &&
5736 1.1 dholland commit == NFSWRITE_UNSTABLE)
5737 1.1 dholland committed = commit;
5738 1.1 dholland if (commit_thru_mds != 0) {
5739 1.1 dholland NFSLOCKMNT(nmp);
5740 1.1 dholland if (!NFSHASWRITEVERF(nmp)) {
5741 1.1 dholland NFSBCOPY(tl, nmp->nm_verf, NFSX_VERF);
5742 1.1 dholland NFSSETWRITEVERF(nmp);
5743 1.1 dholland } else if (NFSBCMP(tl, nmp->nm_verf, NFSX_VERF)) {
5744 1.1 dholland *must_commit = 1;
5745 1.1 dholland NFSBCOPY(tl, nmp->nm_verf, NFSX_VERF);
5746 1.1 dholland }
5747 1.1 dholland NFSUNLOCKMNT(nmp);
5748 1.1 dholland } else {
5749 1.1 dholland NFSLOCKDS(dsp);
5750 1.1 dholland if ((dsp->nfsclds_flags & NFSCLDS_HASWRITEVERF) == 0) {
5751 1.1 dholland NFSBCOPY(tl, dsp->nfsclds_verf, NFSX_VERF);
5752 1.1 dholland dsp->nfsclds_flags |= NFSCLDS_HASWRITEVERF;
5753 1.1 dholland } else if (NFSBCMP(tl, dsp->nfsclds_verf, NFSX_VERF)) {
5754 1.1 dholland *must_commit = 1;
5755 1.1 dholland NFSBCOPY(tl, dsp->nfsclds_verf, NFSX_VERF);
5756 1.1 dholland }
5757 1.1 dholland NFSUNLOCKDS(dsp);
5758 1.1 dholland }
5759 1.1 dholland }
5760 1.1 dholland nfsmout:
5761 1.1 dholland if (nd->nd_mrep != NULL)
5762 1.1 dholland mbuf_freem(nd->nd_mrep);
5763 1.1 dholland *iomode = committed;
5764 1.1 dholland if (nd->nd_repstat != 0 && error == 0)
5765 1.1 dholland error = nd->nd_repstat;
5766 1.1 dholland return (error);
5767 1.1 dholland }
5768 1.1 dholland
5769 1.1 dholland /*
5770 1.1 dholland * Free up the nfsclds structure.
5771 1.1 dholland */
5772 1.1 dholland void
5773 1.1 dholland nfscl_freenfsclds(struct nfsclds *dsp)
5774 1.1 dholland {
5775 1.1 dholland int i;
5776 1.1 dholland
5777 1.1 dholland if (dsp == NULL)
5778 1.1 dholland return;
5779 1.1 dholland if (dsp->nfsclds_sockp != NULL) {
5780 1.1 dholland NFSFREECRED(dsp->nfsclds_sockp->nr_cred);
5781 1.1 dholland NFSFREEMUTEX(&dsp->nfsclds_sockp->nr_mtx);
5782 1.1 dholland free(dsp->nfsclds_sockp->nr_nam, M_SONAME);
5783 1.1 dholland free(dsp->nfsclds_sockp, M_NFSSOCKREQ);
5784 1.1 dholland }
5785 1.1 dholland NFSFREEMUTEX(&dsp->nfsclds_mtx);
5786 1.1 dholland NFSFREEMUTEX(&dsp->nfsclds_sess.nfsess_mtx);
5787 1.1 dholland for (i = 0; i < NFSV4_CBSLOTS; i++) {
5788 1.1 dholland if (dsp->nfsclds_sess.nfsess_cbslots[i].nfssl_reply != NULL)
5789 1.1 dholland m_freem(
5790 1.1 dholland dsp->nfsclds_sess.nfsess_cbslots[i].nfssl_reply);
5791 1.1 dholland }
5792 1.1 dholland free(dsp, M_NFSCLDS);
5793 1.1 dholland }
5794 1.1 dholland
5795 1.1 dholland static enum nfsclds_state
5796 1.1 dholland nfscl_getsameserver(struct nfsmount *nmp, struct nfsclds *newdsp,
5797 1.1 dholland struct nfsclds **retdspp)
5798 1.1 dholland {
5799 1.1 dholland struct nfsclds *dsp, *cur_dsp;
5800 1.1 dholland
5801 1.1 dholland /*
5802 1.1 dholland * Search the list of nfsclds structures for one with the same
5803 1.1 dholland * server.
5804 1.1 dholland */
5805 1.1 dholland cur_dsp = NULL;
5806 1.1 dholland TAILQ_FOREACH(dsp, &nmp->nm_sess, nfsclds_list) {
5807 1.1 dholland if (dsp->nfsclds_servownlen == newdsp->nfsclds_servownlen &&
5808 1.1 dholland dsp->nfsclds_servownlen != 0 &&
5809 1.1 dholland !NFSBCMP(dsp->nfsclds_serverown, newdsp->nfsclds_serverown,
5810 1.1 dholland dsp->nfsclds_servownlen)) {
5811 1.1 dholland NFSCL_DEBUG(4, "fnd same fdsp=%p dsp=%p flg=0x%x\n",
5812 1.1 dholland TAILQ_FIRST(&nmp->nm_sess), dsp,
5813 1.1 dholland dsp->nfsclds_flags);
5814 1.1 dholland /* Server major id matches. */
5815 1.1 dholland if ((dsp->nfsclds_flags & NFSCLDS_DS) != 0) {
5816 1.1 dholland *retdspp = dsp;
5817 1.1 dholland return (NFSDSP_USETHISSESSION);
5818 1.1 dholland }
5819 1.1 dholland
5820 1.1 dholland /*
5821 1.1 dholland * Note the first match, so it can be used for
5822 1.1 dholland * sequence'ing new sessions.
5823 1.1 dholland */
5824 1.1 dholland if (cur_dsp == NULL)
5825 1.1 dholland cur_dsp = dsp;
5826 1.1 dholland }
5827 1.1 dholland }
5828 1.1 dholland if (cur_dsp != NULL) {
5829 1.1 dholland *retdspp = cur_dsp;
5830 1.1 dholland return (NFSDSP_SEQTHISSESSION);
5831 1.1 dholland }
5832 1.1 dholland return (NFSDSP_NOTFOUND);
5833 1.1 dholland }
5834 1.1 dholland
5835 1.1 dholland #ifdef notyet
5836 1.1 dholland /*
5837 1.1 dholland * NFS commit rpc to a DS.
5838 1.1 dholland */
5839 1.1 dholland static int
5840 1.1 dholland nfsrpc_commitds(vnode_t vp, uint64_t offset, int cnt, struct nfsclds *dsp,
5841 1.1 dholland struct nfsfh *fhp, struct ucred *cred, NFSPROC_T *p, void *stuff)
5842 1.1 dholland {
5843 1.1 dholland uint32_t *tl;
5844 1.1 dholland struct nfsrv_descript nfsd, *nd = &nfsd;
5845 1.1 dholland struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
5846 1.1 dholland struct nfssockreq *nrp;
5847 1.1 dholland int error;
5848 1.1 dholland
5849 1.1 dholland nfscl_reqstart(nd, NFSPROC_COMMITDS, nmp, fhp->nfh_fh, fhp->nfh_len,
5850 1.1 dholland NULL, &dsp->nfsclds_sess);
5851 1.1 dholland NFSM_BUILD(tl, uint32_t *, NFSX_HYPER + NFSX_UNSIGNED);
5852 1.1 dholland txdr_hyper(offset, tl);
5853 1.1 dholland tl += 2;
5854 1.1 dholland *tl = txdr_unsigned(cnt);
5855 1.1 dholland nrp = dsp->nfsclds_sockp;
5856 1.1 dholland if (nrp == NULL)
5857 1.1 dholland /* If NULL, use the MDS socket. */
5858 1.1 dholland nrp = &nmp->nm_sockreq;
5859 1.1 dholland error = newnfs_request(nd, nmp, NULL, nrp, vp, p, cred,
5860 1.1 dholland NFS_PROG, NFS_VER4, NULL, 1, NULL, &dsp->nfsclds_sess);
5861 1.1 dholland if (error)
5862 1.1 dholland return (error);
5863 1.1 dholland if (nd->nd_repstat == 0) {
5864 1.1 dholland NFSM_DISSECT(tl, u_int32_t *, NFSX_VERF);
5865 1.1 dholland NFSLOCKDS(dsp);
5866 1.1 dholland if (NFSBCMP(tl, dsp->nfsclds_verf, NFSX_VERF)) {
5867 1.1 dholland NFSBCOPY(tl, dsp->nfsclds_verf, NFSX_VERF);
5868 1.1 dholland error = NFSERR_STALEWRITEVERF;
5869 1.1 dholland }
5870 1.1 dholland NFSUNLOCKDS(dsp);
5871 1.1 dholland }
5872 1.1 dholland nfsmout:
5873 1.1 dholland if (error == 0 && nd->nd_repstat != 0)
5874 1.1 dholland error = nd->nd_repstat;
5875 1.1 dholland mbuf_freem(nd->nd_mrep);
5876 1.1 dholland return (error);
5877 1.1 dholland }
5878 1.1 dholland #endif
5879 1.1 dholland
5880