1 /*-
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Rick Macklem at The University of Guelph.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
34 */
35
36 #include <sys/cdefs.h>
37 /*
38 * Rpc op calls, generally called from the vnode op calls or through the
39 * buffer cache, for NFS v2, 3 and 4.
40 * These do not normally make any changes to vnode arguments or use
41 * structures that might change between the VFS variants. The returned
42 * arguments are all at the end, after the NFSPROC_T *p one.
43 */
44
45 #include "opt_inet6.h"
46
47 #include <fs/nfs/nfsport.h>
48 #include <fs/nfsclient/nfs.h>
49 #include <sys/extattr.h>
50 #include <sys/sysctl.h>
51 #include <sys/taskqueue.h>
52
53 SYSCTL_DECL(_vfs_nfs);
54
55 static int nfsignore_eexist = 0;
56 SYSCTL_INT(_vfs_nfs, OID_AUTO, ignore_eexist, CTLFLAG_RW,
57 &nfsignore_eexist, 0, "NFS ignore EEXIST replies for mkdir/symlink");
58
59 static int nfscl_dssameconn = 0;
60 SYSCTL_INT(_vfs_nfs, OID_AUTO, dssameconn, CTLFLAG_RW,
61 &nfscl_dssameconn, 0, "Use same TCP connection to multiple DSs");
62
63 static uint64_t nfs_maxcopyrange = SSIZE_MAX;
64 SYSCTL_U64(_vfs_nfs, OID_AUTO, maxcopyrange, CTLFLAG_RW,
65 &nfs_maxcopyrange, 0, "Max size of a Copy so RPC times reasonable");
66
67 /*
68 * Global variables
69 */
70 extern struct nfsstatsv1 nfsstatsv1;
71 extern int nfs_numnfscbd;
72 extern struct timeval nfsboottime;
73 extern u_int32_t newnfs_false, newnfs_true;
74 extern nfstype nfsv34_type[9];
75 extern int nfsrv_useacl;
76 extern char nfsv4_callbackaddr[INET6_ADDRSTRLEN];
77 extern int nfscl_debuglevel;
78 extern int nfs_pnfsiothreads;
79 extern u_long sb_max_adj;
80 NFSCLSTATEMUTEX;
81 int nfstest_outofseq = 0;
82 int nfscl_assumeposixlocks = 1;
83 int nfscl_enablecallb = 0;
84 short nfsv4_cbport = NFSV4_CBPORT;
85 int nfstest_openallsetattr = 0;
86
87 #define DIRHDSIZ offsetof(struct dirent, d_name)
88
89 /*
90 * nfscl_getsameserver() can return one of three values:
91 * NFSDSP_USETHISSESSION - Use this session for the DS.
92 * NFSDSP_SEQTHISSESSION - Use the nfsclds_sequence field of this dsp for new
93 * session.
94 * NFSDSP_NOTFOUND - No matching server was found.
95 */
96 enum nfsclds_state {
97 NFSDSP_USETHISSESSION = 0,
98 NFSDSP_SEQTHISSESSION = 1,
99 NFSDSP_NOTFOUND = 2,
100 };
101
102 /*
103 * Do a write RPC on a DS data file, using this structure for the arguments,
104 * so that this function can be executed by a separate kernel process.
105 */
106 struct nfsclwritedsdorpc {
107 int done;
108 int inprog;
109 struct task tsk;
110 struct vnode *vp;
111 int iomode;
112 int must_commit;
113 nfsv4stateid_t *stateidp;
114 struct nfsclds *dsp;
115 uint64_t off;
116 int len;
117 #ifdef notyet
118 int advise;
119 #endif
120 struct nfsfh *fhp;
121 struct mbuf *m;
122 int vers;
123 int minorvers;
124 struct ucred *cred;
125 NFSPROC_T *p;
126 int err;
127 };
128
129 static int nfsrpc_setattrrpc(vnode_t , struct vattr *, nfsv4stateid_t *,
130 struct ucred *, NFSPROC_T *, struct nfsvattr *, int *);
131 static int nfsrpc_readrpc(vnode_t , struct uio *, struct ucred *,
132 nfsv4stateid_t *, NFSPROC_T *, struct nfsvattr *, int *);
133 static int nfsrpc_writerpc(vnode_t , struct uio *, int *, int *,
134 struct ucred *, nfsv4stateid_t *, NFSPROC_T *, struct nfsvattr *, int *,
135 int);
136 static int nfsrpc_deallocaterpc(vnode_t, off_t, off_t, nfsv4stateid_t *,
137 struct nfsvattr *, int *, struct ucred *, NFSPROC_T *);
138 static int nfsrpc_createv23(vnode_t , char *, int, struct vattr *,
139 nfsquad_t, int, struct ucred *, NFSPROC_T *, struct nfsvattr *,
140 struct nfsvattr *, struct nfsfh **, int *, int *);
141 static int nfsrpc_createv4(vnode_t , char *, int, struct vattr *,
142 nfsquad_t, int, struct nfsclowner *, struct nfscldeleg **, struct ucred *,
143 NFSPROC_T *, struct nfsvattr *, struct nfsvattr *, struct nfsfh **, int *,
144 int *, int *);
145 static bool nfscl_invalidfname(bool, char *, int);
146 static int nfsrpc_locku(struct nfsrv_descript *, struct nfsmount *,
147 struct nfscllockowner *, u_int64_t, u_int64_t,
148 u_int32_t, struct ucred *, NFSPROC_T *, int);
149 static int nfsrpc_setaclrpc(vnode_t, struct ucred *, NFSPROC_T *,
150 struct acl *, acl_type_t, nfsv4stateid_t *);
151 static int nfsrpc_layouterror(struct nfsmount *, uint8_t *, int, uint64_t,
152 uint64_t, nfsv4stateid_t *, struct ucred *, NFSPROC_T *, uint32_t,
153 uint32_t, char *);
154 static int nfsrpc_getlayout(struct nfsmount *, vnode_t, struct nfsfh *, int,
155 uint32_t, uint32_t *, nfsv4stateid_t *, uint64_t, struct nfscllayout **,
156 struct ucred *, NFSPROC_T *);
157 static int nfsrpc_fillsa(struct nfsmount *, struct sockaddr_in *,
158 struct sockaddr_in6 *, sa_family_t, int, int, struct nfsclds **,
159 NFSPROC_T *);
160 static void nfscl_initsessionslots(struct nfsclsession *);
161 static int nfscl_doflayoutio(vnode_t, struct uio *, int *, int *, int *,
162 nfsv4stateid_t *, int, struct nfscldevinfo *, struct nfscllayout *,
163 struct nfsclflayout *, uint64_t, uint64_t, int, struct ucred *,
164 NFSPROC_T *);
165 static int nfscl_dofflayoutio(vnode_t, struct uio *, int *, int *, int *,
166 nfsv4stateid_t *, int, struct nfscldevinfo *, struct nfscllayout *,
167 struct nfsclflayout *, uint64_t, uint64_t, int, int, struct mbuf *,
168 struct nfsclwritedsdorpc *, struct ucred *, NFSPROC_T *);
169 static int nfsrpc_readds(vnode_t, struct uio *, nfsv4stateid_t *, int *,
170 struct nfsclds *, uint64_t, int, struct nfsfh *, int, int, int,
171 struct ucred *, NFSPROC_T *);
172 static int nfsrpc_writeds(vnode_t, struct uio *, int *, int *,
173 nfsv4stateid_t *, struct nfsclds *, uint64_t, int,
174 struct nfsfh *, int, int, int, int, struct ucred *, NFSPROC_T *);
175 static int nfsio_writedsmir(vnode_t, int *, int *, nfsv4stateid_t *,
176 struct nfsclds *, uint64_t, int, struct nfsfh *, struct mbuf *, int, int,
177 struct nfsclwritedsdorpc *, struct ucred *, NFSPROC_T *);
178 static int nfsrpc_writedsmir(vnode_t, int *, int *, nfsv4stateid_t *,
179 struct nfsclds *, uint64_t, int, struct nfsfh *, struct mbuf *, int, int,
180 struct ucred *, NFSPROC_T *);
181 static enum nfsclds_state nfscl_getsameserver(struct nfsmount *,
182 struct nfsclds *, struct nfsclds **, uint32_t *);
183 static int nfsio_commitds(vnode_t, uint64_t, int, struct nfsclds *,
184 struct nfsfh *, int, int, struct nfsclwritedsdorpc *, struct ucred *,
185 NFSPROC_T *);
186 static int nfsrpc_commitds(vnode_t, uint64_t, int, struct nfsclds *,
187 struct nfsfh *, int, int, struct ucred *, NFSPROC_T *);
188 #ifdef notyet
189 static int nfsio_adviseds(vnode_t, uint64_t, int, int, struct nfsclds *,
190 struct nfsfh *, int, int, struct nfsclwritedsdorpc *, struct ucred *,
191 NFSPROC_T *);
192 static int nfsrpc_adviseds(vnode_t, uint64_t, int, int, struct nfsclds *,
193 struct nfsfh *, int, int, struct ucred *, NFSPROC_T *);
194 #endif
195 static int nfsrpc_allocaterpc(vnode_t, off_t, off_t, nfsv4stateid_t *,
196 struct nfsvattr *, int *, struct ucred *, NFSPROC_T *);
197 static void nfsrv_setuplayoutget(struct nfsrv_descript *, int, uint64_t,
198 uint64_t, uint64_t, nfsv4stateid_t *, int, int, int);
199 static int nfsrv_parseug(struct nfsrv_descript *, int, uid_t *, gid_t *,
200 NFSPROC_T *);
201 static int nfsrv_parselayoutget(struct nfsmount *, struct nfsrv_descript *,
202 nfsv4stateid_t *, int *, struct nfsclflayouthead *);
203 static int nfsrpc_getopenlayout(struct nfsmount *, vnode_t, u_int8_t *,
204 int, uint8_t *, int, uint32_t, struct nfsclopen *, uint8_t *, int,
205 struct nfscldeleg **, struct ucred *, NFSPROC_T *);
206 static int nfsrpc_getcreatelayout(vnode_t, char *, int, struct vattr *,
207 nfsquad_t, int, struct nfsclowner *, struct nfscldeleg **,
208 struct ucred *, NFSPROC_T *, struct nfsvattr *, struct nfsvattr *,
209 struct nfsfh **, int *, int *, int *);
210 static int nfsrpc_openlayoutrpc(struct nfsmount *, vnode_t, u_int8_t *,
211 int, uint8_t *, int, uint32_t, struct nfsclopen *, uint8_t *, int,
212 struct nfscldeleg **, nfsv4stateid_t *, int, int, int, int *,
213 struct nfsclflayouthead *, int *, struct ucred *, NFSPROC_T *);
214 static int nfsrpc_createlayout(vnode_t, char *, int, struct vattr *,
215 nfsquad_t, int, struct nfsclowner *, struct nfscldeleg **,
216 struct ucred *, NFSPROC_T *, struct nfsvattr *, struct nfsvattr *,
217 struct nfsfh **, int *, int *, int *, nfsv4stateid_t *,
218 int, int, int, int *, struct nfsclflayouthead *, int *);
219 static int nfsrpc_layoutget(struct nfsmount *, uint8_t *, int, int, uint64_t,
220 uint64_t, uint64_t, int, int, nfsv4stateid_t *, int *,
221 struct nfsclflayouthead *, struct ucred *, NFSPROC_T *);
222 static int nfsrpc_layoutgetres(struct nfsmount *, vnode_t, uint8_t *,
223 int, nfsv4stateid_t *, int, uint32_t *, struct nfscllayout **,
224 struct nfsclflayouthead *, int, int, int *, struct ucred *, NFSPROC_T *);
225 static int nfsrpc_copyrpc(vnode_t, off_t, vnode_t, off_t, size_t *,
226 nfsv4stateid_t *, nfsv4stateid_t *, struct nfsvattr *, int *,
227 struct nfsvattr *, int *, bool, int *, struct ucred *, NFSPROC_T *);
228 static int nfsrpc_clonerpc(vnode_t, off_t, vnode_t, off_t, size_t *, bool,
229 nfsv4stateid_t *, nfsv4stateid_t *, struct nfsvattr *, int *,
230 struct nfsvattr *, int *, struct ucred *, NFSPROC_T *);
231 static int nfsrpc_seekrpc(vnode_t, off_t *, nfsv4stateid_t *, bool *,
232 int, struct nfsvattr *, int *, struct ucred *);
233 static struct mbuf *nfsm_split(struct mbuf *, uint64_t);
234 static void nfscl_statfs(struct vnode *, struct ucred *, NFSPROC_T *);
235
236 int nfs_pnfsio(task_fn_t *, void *);
237
238 /*
239 * nfs null call from vfs.
240 */
241 int
nfsrpc_null(vnode_t vp,struct ucred * cred,NFSPROC_T * p)242 nfsrpc_null(vnode_t vp, struct ucred *cred, NFSPROC_T *p)
243 {
244 int error;
245 struct nfsrv_descript nfsd, *nd = &nfsd;
246
247 NFSCL_REQSTART(nd, NFSPROC_NULL, vp, NULL);
248 error = nfscl_request(nd, vp, p, cred);
249 if (nd->nd_repstat && !error)
250 error = nd->nd_repstat;
251 m_freem(nd->nd_mrep);
252 return (error);
253 }
254
255 /*
256 * nfs access rpc op.
257 * For nfs version 3 and 4, use the access rpc to check accessibility. If file
258 * modes are changed on the server, accesses might still fail later.
259 */
260 int
nfsrpc_access(vnode_t vp,int acmode,struct ucred * cred,NFSPROC_T * p,struct nfsvattr * nap,int * attrflagp)261 nfsrpc_access(vnode_t vp, int acmode, struct ucred *cred,
262 NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp)
263 {
264 int error;
265 u_int32_t mode, rmode;
266
267 if (acmode & VREAD)
268 mode = NFSACCESS_READ;
269 else
270 mode = 0;
271 if (vp->v_type == VDIR) {
272 if (acmode & VWRITE)
273 mode |= (NFSACCESS_MODIFY | NFSACCESS_EXTEND |
274 NFSACCESS_DELETE);
275 if (acmode & VEXEC)
276 mode |= NFSACCESS_LOOKUP;
277 } else {
278 if (acmode & VWRITE)
279 mode |= (NFSACCESS_MODIFY | NFSACCESS_EXTEND);
280 if (acmode & VEXEC)
281 mode |= NFSACCESS_EXECUTE;
282 }
283
284 /*
285 * Now, just call nfsrpc_accessrpc() to do the actual RPC.
286 */
287 error = nfsrpc_accessrpc(vp, mode, cred, p, nap, attrflagp, &rmode);
288
289 /*
290 * The NFS V3 spec does not clarify whether or not
291 * the returned access bits can be a superset of
292 * the ones requested, so...
293 */
294 if (!error && (rmode & mode) != mode)
295 error = EACCES;
296 return (error);
297 }
298
299 /*
300 * The actual rpc, separated out for Darwin.
301 */
302 int
nfsrpc_accessrpc(vnode_t vp,u_int32_t mode,struct ucred * cred,NFSPROC_T * p,struct nfsvattr * nap,int * attrflagp,u_int32_t * rmodep)303 nfsrpc_accessrpc(vnode_t vp, u_int32_t mode, struct ucred *cred,
304 NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, u_int32_t *rmodep)
305 {
306 u_int32_t *tl;
307 u_int32_t supported, rmode;
308 int error;
309 struct nfsrv_descript nfsd, *nd = &nfsd;
310 nfsattrbit_t attrbits;
311 struct nfsmount *nmp;
312 struct nfsnode *np;
313
314 *attrflagp = 0;
315 supported = mode;
316 nmp = VFSTONFS(vp->v_mount);
317 np = VTONFS(vp);
318 if ((nmp->nm_privflag & NFSMNTP_FAKEROOTFH) != 0 &&
319 nmp->nm_fhsize == 0) {
320 /* Attempt to get the actual root file handle. */
321 error = nfsrpc_getdirpath(nmp, NFSMNT_DIRPATH(nmp), cred, p);
322 if (error != 0)
323 return (EACCES);
324 if (np->n_fhp->nfh_len == NFSX_FHMAX + 1)
325 nfscl_statfs(vp, cred, p);
326 }
327 NFSCL_REQSTART(nd, NFSPROC_ACCESS, vp, cred);
328 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
329 *tl = txdr_unsigned(mode);
330 if (nd->nd_flag & ND_NFSV4) {
331 /*
332 * And do a Getattr op.
333 */
334 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
335 *tl = txdr_unsigned(NFSV4OP_GETATTR);
336 NFSGETATTR_ATTRBIT(&attrbits);
337 (void) nfsrv_putattrbit(nd, &attrbits);
338 }
339 error = nfscl_request(nd, vp, p, cred);
340 if (error)
341 return (error);
342 if (nd->nd_flag & ND_NFSV3) {
343 error = nfscl_postop_attr(nd, nap, attrflagp);
344 if (error)
345 goto nfsmout;
346 }
347 if (!nd->nd_repstat) {
348 if (nd->nd_flag & ND_NFSV4) {
349 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
350 supported = fxdr_unsigned(u_int32_t, *tl++);
351 } else {
352 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
353 }
354 rmode = fxdr_unsigned(u_int32_t, *tl);
355 if (nd->nd_flag & ND_NFSV4)
356 error = nfscl_postop_attr(nd, nap, attrflagp);
357
358 /*
359 * It's not obvious what should be done about
360 * unsupported access modes. For now, be paranoid
361 * and clear the unsupported ones.
362 */
363 rmode &= supported;
364 *rmodep = rmode;
365 } else
366 error = nd->nd_repstat;
367 nfsmout:
368 m_freem(nd->nd_mrep);
369 return (error);
370 }
371
372 /*
373 * nfs open rpc
374 */
375 int
nfsrpc_open(vnode_t vp,int amode,struct ucred * cred,NFSPROC_T * p)376 nfsrpc_open(vnode_t vp, int amode, struct ucred *cred, NFSPROC_T *p)
377 {
378 struct nfsclopen *op;
379 struct nfscldeleg *dp;
380 struct nfsfh *nfhp;
381 struct nfsnode *np = VTONFS(vp);
382 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
383 u_int32_t mode, clidrev;
384 int ret, newone, error, expireret = 0, retrycnt;
385
386 /*
387 * For NFSv4, Open Ops are only done on Regular Files.
388 */
389 if (vp->v_type != VREG)
390 return (0);
391 mode = 0;
392 if (amode & FREAD)
393 mode |= NFSV4OPEN_ACCESSREAD;
394 if (amode & FWRITE)
395 mode |= NFSV4OPEN_ACCESSWRITE;
396 if (NFSHASNFSV4N(nmp)) {
397 if (!NFSHASPNFS(nmp) && nfscl_enablecallb != 0 &&
398 nfs_numnfscbd > 0 &&
399 (vn_irflag_read(vp) & VIRF_NAMEDATTR) == 0) {
400 if ((mode & NFSV4OPEN_ACCESSWRITE) != 0)
401 mode |= NFSV4OPEN_WANTWRITEDELEG;
402 else
403 mode |= NFSV4OPEN_WANTANYDELEG;
404 } else
405 mode |= NFSV4OPEN_WANTNODELEG;
406 }
407 nfhp = np->n_fhp;
408
409 retrycnt = 0;
410 do {
411 dp = NULL;
412 error = nfscl_open(vp, nfhp->nfh_fh, nfhp->nfh_len,
413 (mode & NFSV4OPEN_ACCESSBOTH), 1, cred, p, NULL,
414 &op, &newone, &ret, 1, true);
415 if (error) {
416 return (error);
417 }
418 if (nmp->nm_clp != NULL)
419 clidrev = nmp->nm_clp->nfsc_clientidrev;
420 else
421 clidrev = 0;
422 if (ret == NFSCLOPEN_DOOPEN) {
423 if (np->n_v4 != NULL) {
424 /*
425 * For the first attempt, try and get a layout, if
426 * pNFS is enabled for the mount.
427 */
428 if (!NFSHASPNFS(nmp) || nfscl_enablecallb == 0 ||
429 nfs_numnfscbd == 0 ||
430 (np->n_flag & NNOLAYOUT) != 0 || retrycnt > 0)
431 error = nfsrpc_openrpc(nmp, vp,
432 np->n_v4->n4_data,
433 np->n_v4->n4_fhlen, np->n_fhp->nfh_fh,
434 np->n_fhp->nfh_len, mode, op,
435 NFS4NODENAME(np->n_v4),
436 np->n_v4->n4_namelen,
437 &dp, 0, 0x0, cred, p, 0, 0);
438 else
439 error = nfsrpc_getopenlayout(nmp, vp,
440 np->n_v4->n4_data,
441 np->n_v4->n4_fhlen, np->n_fhp->nfh_fh,
442 np->n_fhp->nfh_len, mode, op,
443 NFS4NODENAME(np->n_v4),
444 np->n_v4->n4_namelen, &dp, cred, p);
445 if (dp != NULL) {
446 NFSLOCKNODE(np);
447 np->n_flag &= ~NDELEGMOD;
448 /*
449 * Invalidate the attribute cache, so that
450 * attributes that pre-date the issue of a
451 * delegation are not cached, since the
452 * cached attributes will remain valid while
453 * the delegation is held.
454 */
455 NFSINVALATTRCACHE(np);
456 NFSUNLOCKNODE(np);
457 (void) nfscl_deleg(nmp->nm_mountp,
458 op->nfso_own->nfsow_clp,
459 nfhp->nfh_fh, nfhp->nfh_len, cred, p, dp);
460 }
461 } else if (NFSHASNFSV4N(nmp)) {
462 /*
463 * For the first attempt, try and get a layout, if
464 * pNFS is enabled for the mount.
465 */
466 if (!NFSHASPNFS(nmp) || nfscl_enablecallb == 0 ||
467 nfs_numnfscbd == 0 ||
468 (np->n_flag & NNOLAYOUT) != 0 || retrycnt > 0)
469 error = nfsrpc_openrpc(nmp, vp, nfhp->nfh_fh,
470 nfhp->nfh_len, nfhp->nfh_fh, nfhp->nfh_len,
471 mode, op, NULL, 0, &dp, 0, 0x0, cred, p, 0,
472 0);
473 else
474 error = nfsrpc_getopenlayout(nmp, vp,
475 nfhp->nfh_fh, nfhp->nfh_len, nfhp->nfh_fh,
476 nfhp->nfh_len, mode, op, NULL, 0, &dp,
477 cred, p);
478 if (dp != NULL) {
479 NFSLOCKNODE(np);
480 np->n_flag &= ~NDELEGMOD;
481 /*
482 * Invalidate the attribute cache, so that
483 * attributes that pre-date the issue of a
484 * delegation are not cached, since the
485 * cached attributes will remain valid while
486 * the delegation is held.
487 */
488 NFSINVALATTRCACHE(np);
489 NFSUNLOCKNODE(np);
490 (void) nfscl_deleg(nmp->nm_mountp,
491 op->nfso_own->nfsow_clp,
492 nfhp->nfh_fh, nfhp->nfh_len, cred, p, dp);
493 }
494 } else {
495 error = EIO;
496 }
497 newnfs_copyincred(cred, &op->nfso_cred);
498 } else if (ret == NFSCLOPEN_SETCRED)
499 /*
500 * This is a new local open on a delegation. It needs
501 * to have credentials so that an open can be done
502 * against the server during recovery.
503 */
504 newnfs_copyincred(cred, &op->nfso_cred);
505
506 /*
507 * nfso_opencnt is the count of how many VOP_OPEN()s have
508 * been done on this Open successfully and a VOP_CLOSE()
509 * is expected for each of these.
510 * If error is non-zero, don't increment it, since the Open
511 * hasn't succeeded yet.
512 */
513 if (!error) {
514 op->nfso_opencnt++;
515 if (NFSHASNFSV4N(nmp) && NFSHASONEOPENOWN(nmp)) {
516 NFSLOCKNODE(np);
517 np->n_openstateid = op;
518 NFSUNLOCKNODE(np);
519 }
520 }
521 nfscl_openrelease(nmp, op, error, newone);
522 if (error == NFSERR_GRACE || error == NFSERR_STALECLIENTID ||
523 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
524 error == NFSERR_BADSESSION) {
525 (void) nfs_catnap(PZERO, error, "nfs_open");
526 } else if ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID)
527 && clidrev != 0) {
528 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
529 retrycnt++;
530 }
531 } while (error == NFSERR_GRACE || error == NFSERR_STALECLIENTID ||
532 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
533 error == NFSERR_BADSESSION ||
534 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
535 expireret == 0 && clidrev != 0 && retrycnt < 4));
536 if (error && retrycnt >= 4)
537 error = EIO;
538 return (error);
539 }
540
541 /*
542 * the actual open rpc
543 */
544 int
nfsrpc_openrpc(struct nfsmount * nmp,vnode_t vp,u_int8_t * nfhp,int fhlen,u_int8_t * newfhp,int newfhlen,u_int32_t mode,struct nfsclopen * op,u_int8_t * name,int namelen,struct nfscldeleg ** dpp,int reclaim,u_int32_t delegtype,struct ucred * cred,NFSPROC_T * p,int syscred,int recursed)545 nfsrpc_openrpc(struct nfsmount *nmp, vnode_t vp, u_int8_t *nfhp, int fhlen,
546 u_int8_t *newfhp, int newfhlen, u_int32_t mode, struct nfsclopen *op,
547 u_int8_t *name, int namelen, struct nfscldeleg **dpp,
548 int reclaim, u_int32_t delegtype, struct ucred *cred, NFSPROC_T *p,
549 int syscred, int recursed)
550 {
551 u_int32_t *tl;
552 struct nfsrv_descript nfsd, *nd = &nfsd;
553 struct nfscldeleg *dp, *ndp = NULL;
554 struct nfsvattr nfsva;
555 u_int32_t rflags, deleg;
556 nfsattrbit_t attrbits;
557 int error, ret, acesize, limitby;
558 struct nfsclsession *tsep;
559
560 dp = *dpp;
561 *dpp = NULL;
562 nfscl_reqstart(nd, NFSPROC_OPEN, nmp, nfhp, fhlen, NULL, NULL, 0, 0,
563 cred);
564 NFSM_BUILD(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
565 *tl++ = txdr_unsigned(op->nfso_own->nfsow_seqid);
566 *tl++ = txdr_unsigned(mode & (NFSV4OPEN_ACCESSBOTH |
567 NFSV4OPEN_WANTDELEGMASK));
568 *tl++ = txdr_unsigned((mode >> NFSLCK_SHIFT) & NFSV4OPEN_DENYBOTH);
569 tsep = nfsmnt_mdssession(nmp);
570 *tl++ = tsep->nfsess_clientid.lval[0];
571 *tl = tsep->nfsess_clientid.lval[1];
572 (void) nfsm_strtom(nd, op->nfso_own->nfsow_owner, NFSV4CL_LOCKNAMELEN);
573 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
574 *tl++ = txdr_unsigned(NFSV4OPEN_NOCREATE);
575 if (reclaim) {
576 *tl = txdr_unsigned(NFSV4OPEN_CLAIMPREVIOUS);
577 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
578 *tl = txdr_unsigned(delegtype);
579 } else {
580 if (dp != NULL) {
581 if (NFSHASNFSV4N(nmp))
582 *tl = txdr_unsigned(
583 NFSV4OPEN_CLAIMDELEGATECURFH);
584 else
585 *tl = txdr_unsigned(NFSV4OPEN_CLAIMDELEGATECUR);
586 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID);
587 if (NFSHASNFSV4N(nmp))
588 *tl++ = 0;
589 else
590 *tl++ = dp->nfsdl_stateid.seqid;
591 *tl++ = dp->nfsdl_stateid.other[0];
592 *tl++ = dp->nfsdl_stateid.other[1];
593 *tl = dp->nfsdl_stateid.other[2];
594 if (!NFSHASNFSV4N(nmp))
595 (void)nfsm_strtom(nd, name, namelen);
596 } else if (NFSHASNFSV4N(nmp)) {
597 *tl = txdr_unsigned(NFSV4OPEN_CLAIMFH);
598 } else {
599 *tl = txdr_unsigned(NFSV4OPEN_CLAIMNULL);
600 (void)nfsm_strtom(nd, name, namelen);
601 }
602 }
603 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
604 *tl = txdr_unsigned(NFSV4OP_GETATTR);
605 NFSZERO_ATTRBIT(&attrbits);
606 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_CHANGE);
607 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEMODIFY);
608 (void) nfsrv_putattrbit(nd, &attrbits);
609 if (syscred)
610 nd->nd_flag |= ND_USEGSSNAME;
611 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, vp, p, cred,
612 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
613 if (error)
614 return (error);
615 NFSCL_INCRSEQID(op->nfso_own->nfsow_seqid, nd);
616 if (nd->nd_repstat == 0 || (nd->nd_repstat == NFSERR_DELAY &&
617 reclaim != 0 && (nd->nd_flag & ND_NOMOREDATA) == 0)) {
618 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
619 6 * NFSX_UNSIGNED);
620 op->nfso_stateid.seqid = *tl++;
621 op->nfso_stateid.other[0] = *tl++;
622 op->nfso_stateid.other[1] = *tl++;
623 op->nfso_stateid.other[2] = *tl;
624 rflags = fxdr_unsigned(u_int32_t, *(tl + 6));
625 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
626 if (error)
627 goto nfsmout;
628 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
629 deleg = fxdr_unsigned(u_int32_t, *tl);
630 if (deleg == NFSV4OPEN_DELEGATEREAD ||
631 deleg == NFSV4OPEN_DELEGATEWRITE) {
632 if (!(op->nfso_own->nfsow_clp->nfsc_flags &
633 NFSCLFLAGS_FIRSTDELEG))
634 op->nfso_own->nfsow_clp->nfsc_flags |=
635 (NFSCLFLAGS_FIRSTDELEG | NFSCLFLAGS_GOTDELEG);
636 ndp = malloc(
637 sizeof (struct nfscldeleg) + newfhlen,
638 M_NFSCLDELEG, M_WAITOK);
639 LIST_INIT(&ndp->nfsdl_owner);
640 LIST_INIT(&ndp->nfsdl_lock);
641 ndp->nfsdl_clp = op->nfso_own->nfsow_clp;
642 ndp->nfsdl_fhlen = newfhlen;
643 NFSBCOPY(newfhp, ndp->nfsdl_fh, newfhlen);
644 newnfs_copyincred(cred, &ndp->nfsdl_cred);
645 nfscl_lockinit(&ndp->nfsdl_rwlock);
646 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
647 NFSX_UNSIGNED);
648 ndp->nfsdl_stateid.seqid = *tl++;
649 ndp->nfsdl_stateid.other[0] = *tl++;
650 ndp->nfsdl_stateid.other[1] = *tl++;
651 ndp->nfsdl_stateid.other[2] = *tl++;
652 ret = fxdr_unsigned(int, *tl);
653 if (deleg == NFSV4OPEN_DELEGATEWRITE) {
654 ndp->nfsdl_flags = NFSCLDL_WRITE;
655 /*
656 * Indicates how much the file can grow.
657 */
658 NFSM_DISSECT(tl, u_int32_t *,
659 3 * NFSX_UNSIGNED);
660 limitby = fxdr_unsigned(int, *tl++);
661 switch (limitby) {
662 case NFSV4OPEN_LIMITSIZE:
663 ndp->nfsdl_sizelimit = fxdr_hyper(tl);
664 break;
665 case NFSV4OPEN_LIMITBLOCKS:
666 ndp->nfsdl_sizelimit =
667 fxdr_unsigned(u_int64_t, *tl++);
668 ndp->nfsdl_sizelimit *=
669 fxdr_unsigned(u_int64_t, *tl);
670 break;
671 default:
672 error = NFSERR_BADXDR;
673 goto nfsmout;
674 }
675 } else {
676 ndp->nfsdl_flags = NFSCLDL_READ;
677 }
678 if (ret)
679 ndp->nfsdl_flags |= NFSCLDL_RECALL;
680 error = nfsrv_dissectace(nd, &ndp->nfsdl_ace, false,
681 &ret, &acesize, p);
682 if (error)
683 goto nfsmout;
684 } else if (deleg == NFSV4OPEN_DELEGATENONEEXT &&
685 NFSHASNFSV4N(nmp)) {
686 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
687 deleg = fxdr_unsigned(uint32_t, *tl);
688 if (deleg == NFSV4OPEN_CONTENTION ||
689 deleg == NFSV4OPEN_RESOURCE)
690 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
691 } else if (deleg != NFSV4OPEN_DELEGATENONE) {
692 error = NFSERR_BADXDR;
693 goto nfsmout;
694 }
695 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
696 /* If the 2nd element == NFS_OK, the Getattr succeeded. */
697 if (*++tl == 0) {
698 KASSERT(nd->nd_repstat == 0,
699 ("nfsrpc_openrpc: Getattr repstat"));
700 error = nfsv4_loadattr(nd, NULL, &nfsva, NULL,
701 NULL, 0, NULL, NULL, NULL, NULL, NULL, 0,
702 NULL, NULL, NULL, NULL, NULL, p, cred);
703 if (error)
704 goto nfsmout;
705 }
706 if (ndp != NULL) {
707 if (reclaim != 0 && dp != NULL) {
708 ndp->nfsdl_change = dp->nfsdl_change;
709 ndp->nfsdl_modtime = dp->nfsdl_modtime;
710 ndp->nfsdl_flags |= NFSCLDL_MODTIMESET;
711 } else if (nd->nd_repstat == 0) {
712 ndp->nfsdl_change = nfsva.na_filerev;
713 ndp->nfsdl_modtime = nfsva.na_mtime;
714 ndp->nfsdl_flags |= NFSCLDL_MODTIMESET;
715 } else
716 ndp->nfsdl_flags |= NFSCLDL_RECALL;
717 }
718 nd->nd_repstat = 0;
719 if (!reclaim && (rflags & NFSV4OPEN_RESULTCONFIRM)) {
720 do {
721 ret = nfsrpc_openconfirm(vp, newfhp, newfhlen, op,
722 cred, p);
723 if (ret == NFSERR_DELAY)
724 (void) nfs_catnap(PZERO, ret, "nfs_open");
725 } while (ret == NFSERR_DELAY);
726 error = ret;
727 }
728 if ((rflags & NFSV4OPEN_LOCKTYPEPOSIX) ||
729 nfscl_assumeposixlocks)
730 op->nfso_posixlock = 1;
731 else
732 op->nfso_posixlock = 0;
733
734 /*
735 * If the server is handing out delegations, but we didn't
736 * get one because an OpenConfirm was required, try the
737 * Open again, to get a delegation. This is a harmless no-op,
738 * from a server's point of view.
739 */
740 if (!reclaim && (rflags & NFSV4OPEN_RESULTCONFIRM) &&
741 (op->nfso_own->nfsow_clp->nfsc_flags & NFSCLFLAGS_GOTDELEG)
742 && !error && dp == NULL && ndp == NULL && !recursed) {
743 do {
744 ret = nfsrpc_openrpc(nmp, vp, nfhp, fhlen, newfhp,
745 newfhlen, mode, op, name, namelen, &ndp, 0, 0x0,
746 cred, p, syscred, 1);
747 if (ret == NFSERR_DELAY)
748 (void) nfs_catnap(PZERO, ret, "nfs_open2");
749 } while (ret == NFSERR_DELAY);
750 if (ret) {
751 if (ndp != NULL) {
752 free(ndp, M_NFSCLDELEG);
753 ndp = NULL;
754 }
755 if (ret == NFSERR_STALECLIENTID ||
756 ret == NFSERR_STALEDONTRECOVER ||
757 ret == NFSERR_BADSESSION)
758 error = ret;
759 }
760 }
761 }
762 if (nd->nd_repstat != 0 && error == 0)
763 error = nd->nd_repstat;
764 if (error == NFSERR_STALECLIENTID)
765 nfscl_initiate_recovery(op->nfso_own->nfsow_clp);
766 nfsmout:
767 if (!error)
768 *dpp = ndp;
769 else if (ndp != NULL)
770 free(ndp, M_NFSCLDELEG);
771 m_freem(nd->nd_mrep);
772 return (error);
773 }
774
775 /*
776 * open downgrade rpc
777 */
778 int
nfsrpc_opendowngrade(vnode_t vp,u_int32_t mode,struct nfsclopen * op,struct ucred * cred,NFSPROC_T * p)779 nfsrpc_opendowngrade(vnode_t vp, u_int32_t mode, struct nfsclopen *op,
780 struct ucred *cred, NFSPROC_T *p)
781 {
782 u_int32_t *tl;
783 struct nfsrv_descript nfsd, *nd = &nfsd;
784 int error;
785
786 NFSCL_REQSTART(nd, NFSPROC_OPENDOWNGRADE, vp, cred);
787 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID + 3 * NFSX_UNSIGNED);
788 if (NFSHASNFSV4N(VFSTONFS(vp->v_mount)))
789 *tl++ = 0;
790 else
791 *tl++ = op->nfso_stateid.seqid;
792 *tl++ = op->nfso_stateid.other[0];
793 *tl++ = op->nfso_stateid.other[1];
794 *tl++ = op->nfso_stateid.other[2];
795 *tl++ = txdr_unsigned(op->nfso_own->nfsow_seqid);
796 *tl++ = txdr_unsigned(mode & NFSV4OPEN_ACCESSBOTH);
797 *tl = txdr_unsigned((mode >> NFSLCK_SHIFT) & NFSV4OPEN_DENYBOTH);
798 error = nfscl_request(nd, vp, p, cred);
799 if (error)
800 return (error);
801 NFSCL_INCRSEQID(op->nfso_own->nfsow_seqid, nd);
802 if (!nd->nd_repstat) {
803 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID);
804 op->nfso_stateid.seqid = *tl++;
805 op->nfso_stateid.other[0] = *tl++;
806 op->nfso_stateid.other[1] = *tl++;
807 op->nfso_stateid.other[2] = *tl;
808 }
809 if (nd->nd_repstat && error == 0)
810 error = nd->nd_repstat;
811 if (error == NFSERR_STALESTATEID)
812 nfscl_initiate_recovery(op->nfso_own->nfsow_clp);
813 nfsmout:
814 m_freem(nd->nd_mrep);
815 return (error);
816 }
817
818 /*
819 * V4 Close operation.
820 */
821 int
nfsrpc_close(vnode_t vp,int doclose,NFSPROC_T * p)822 nfsrpc_close(vnode_t vp, int doclose, NFSPROC_T *p)
823 {
824 struct nfsclclient *clp;
825 int error;
826
827 if (vp->v_type != VREG)
828 return (0);
829 if (doclose)
830 error = nfscl_doclose(vp, &clp, p);
831 else {
832 error = nfscl_getclose(vp, &clp);
833 if (error == 0)
834 nfscl_clientrelease(clp);
835 }
836 return (error);
837 }
838
839 /*
840 * Close the open.
841 */
842 int
nfsrpc_doclose(struct nfsmount * nmp,struct nfsclopen * op,NFSPROC_T * p,bool loop_on_delayed,bool freeop)843 nfsrpc_doclose(struct nfsmount *nmp, struct nfsclopen *op, NFSPROC_T *p,
844 bool loop_on_delayed, bool freeop)
845 {
846 struct nfsrv_descript nfsd, *nd = &nfsd;
847 struct nfscllockowner *lp, *nlp;
848 struct nfscllock *lop, *nlop;
849 struct ucred *tcred;
850 u_int64_t off = 0, len = 0;
851 u_int32_t type = NFSV4LOCKT_READ;
852 int error, do_unlock, trycnt;
853 bool own_not_null;
854
855 tcred = newnfs_getcred();
856 newnfs_copycred(&op->nfso_cred, tcred);
857 /*
858 * (Theoretically this could be done in the same
859 * compound as the close, but having multiple
860 * sequenced Ops in the same compound might be
861 * too scary for some servers.)
862 */
863 if (op->nfso_posixlock) {
864 off = 0;
865 len = NFS64BITSSET;
866 type = NFSV4LOCKT_READ;
867 }
868
869 /*
870 * Since this function is only called from VOP_INACTIVE(), no
871 * other thread will be manipulating this Open. As such, the
872 * lock lists are not being changed by other threads, so it should
873 * be safe to do this without locking.
874 */
875 LIST_FOREACH(lp, &op->nfso_lock, nfsl_list) {
876 do_unlock = 1;
877 LIST_FOREACH_SAFE(lop, &lp->nfsl_lock, nfslo_list, nlop) {
878 if (op->nfso_posixlock == 0) {
879 off = lop->nfslo_first;
880 len = lop->nfslo_end - lop->nfslo_first;
881 if (lop->nfslo_type == F_WRLCK)
882 type = NFSV4LOCKT_WRITE;
883 else
884 type = NFSV4LOCKT_READ;
885 }
886 if (do_unlock) {
887 trycnt = 0;
888 do {
889 error = nfsrpc_locku(nd, nmp, lp, off,
890 len, type, tcred, p, 0);
891 if ((nd->nd_repstat == NFSERR_GRACE ||
892 nd->nd_repstat == NFSERR_DELAY) &&
893 error == 0)
894 (void) nfs_catnap(PZERO,
895 (int)nd->nd_repstat,
896 "nfs_close");
897 } while ((nd->nd_repstat == NFSERR_GRACE ||
898 nd->nd_repstat == NFSERR_DELAY) &&
899 error == 0 && trycnt++ < 5);
900 if (op->nfso_posixlock)
901 do_unlock = 0;
902 }
903 nfscl_freelock(lop, 0);
904 }
905 /*
906 * Do a ReleaseLockOwner.
907 * The lock owner name nfsl_owner may be used by other opens for
908 * other files but the lock_owner4 name that nfsrpc_rellockown()
909 * puts on the wire has the file handle for this file appended
910 * to it, so it can be done now.
911 */
912 (void)nfsrpc_rellockown(nmp, lp, lp->nfsl_open->nfso_fh,
913 lp->nfsl_open->nfso_fhlen, tcred, p);
914 }
915
916 /*
917 * There could be other Opens for different files on the same
918 * OpenOwner, so locking is required.
919 */
920 own_not_null = false;
921 if (op->nfso_own != NULL) {
922 own_not_null = true;
923 NFSLOCKCLSTATE();
924 nfscl_lockexcl(&op->nfso_own->nfsow_rwlock, NFSCLSTATEMUTEXPTR);
925 NFSUNLOCKCLSTATE();
926 }
927 do {
928 error = nfscl_tryclose(op, tcred, nmp, p, loop_on_delayed);
929 if (error == NFSERR_GRACE)
930 (void) nfs_catnap(PZERO, error, "nfs_close");
931 } while (error == NFSERR_GRACE);
932 if (own_not_null) {
933 NFSLOCKCLSTATE();
934 nfscl_lockunlock(&op->nfso_own->nfsow_rwlock);
935 }
936
937 LIST_FOREACH_SAFE(lp, &op->nfso_lock, nfsl_list, nlp)
938 nfscl_freelockowner(lp, 0);
939 if (freeop && error != NFSERR_DELAY)
940 nfscl_freeopen(op, 0, true);
941 if (own_not_null)
942 NFSUNLOCKCLSTATE();
943 NFSFREECRED(tcred);
944 return (error);
945 }
946
947 /*
948 * The actual Close RPC.
949 */
950 int
nfsrpc_closerpc(struct nfsrv_descript * nd,struct nfsmount * nmp,struct nfsclopen * op,struct ucred * cred,NFSPROC_T * p,int syscred)951 nfsrpc_closerpc(struct nfsrv_descript *nd, struct nfsmount *nmp,
952 struct nfsclopen *op, struct ucred *cred, NFSPROC_T *p,
953 int syscred)
954 {
955 u_int32_t *tl;
956 int error;
957
958 nfscl_reqstart(nd, NFSPROC_CLOSE, nmp, op->nfso_fh,
959 op->nfso_fhlen, NULL, NULL, 0, 0, cred);
960 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED + NFSX_STATEID);
961 if (NFSHASNFSV4N(nmp)) {
962 *tl++ = 0;
963 *tl++ = 0;
964 } else {
965 *tl++ = txdr_unsigned(op->nfso_own->nfsow_seqid);
966 *tl++ = op->nfso_stateid.seqid;
967 }
968 *tl++ = op->nfso_stateid.other[0];
969 *tl++ = op->nfso_stateid.other[1];
970 *tl = op->nfso_stateid.other[2];
971 if (syscred)
972 nd->nd_flag |= ND_USEGSSNAME;
973 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
974 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
975 if (error)
976 return (error);
977 if (!NFSHASNFSV4N(nmp))
978 NFSCL_INCRSEQID(op->nfso_own->nfsow_seqid, nd);
979 if (nd->nd_repstat == 0)
980 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID);
981 error = nd->nd_repstat;
982 if (!NFSHASNFSV4N(nmp) && error == NFSERR_STALESTATEID)
983 nfscl_initiate_recovery(op->nfso_own->nfsow_clp);
984 nfsmout:
985 m_freem(nd->nd_mrep);
986 return (error);
987 }
988
989 /*
990 * V4 Open Confirm RPC.
991 */
992 int
nfsrpc_openconfirm(vnode_t vp,u_int8_t * nfhp,int fhlen,struct nfsclopen * op,struct ucred * cred,NFSPROC_T * p)993 nfsrpc_openconfirm(vnode_t vp, u_int8_t *nfhp, int fhlen,
994 struct nfsclopen *op, struct ucred *cred, NFSPROC_T *p)
995 {
996 u_int32_t *tl;
997 struct nfsrv_descript nfsd, *nd = &nfsd;
998 struct nfsmount *nmp;
999 int error;
1000
1001 nmp = VFSTONFS(vp->v_mount);
1002 if (NFSHASNFSV4N(nmp))
1003 return (0); /* No confirmation for NFSv4.1. */
1004 nfscl_reqstart(nd, NFSPROC_OPENCONFIRM, nmp, nfhp, fhlen, NULL, NULL,
1005 0, 0, NULL);
1006 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED + NFSX_STATEID);
1007 *tl++ = op->nfso_stateid.seqid;
1008 *tl++ = op->nfso_stateid.other[0];
1009 *tl++ = op->nfso_stateid.other[1];
1010 *tl++ = op->nfso_stateid.other[2];
1011 *tl = txdr_unsigned(op->nfso_own->nfsow_seqid);
1012 error = nfscl_request(nd, vp, p, cred);
1013 if (error)
1014 return (error);
1015 NFSCL_INCRSEQID(op->nfso_own->nfsow_seqid, nd);
1016 if (!nd->nd_repstat) {
1017 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID);
1018 op->nfso_stateid.seqid = *tl++;
1019 op->nfso_stateid.other[0] = *tl++;
1020 op->nfso_stateid.other[1] = *tl++;
1021 op->nfso_stateid.other[2] = *tl;
1022 }
1023 error = nd->nd_repstat;
1024 if (error == NFSERR_STALESTATEID)
1025 nfscl_initiate_recovery(op->nfso_own->nfsow_clp);
1026 nfsmout:
1027 m_freem(nd->nd_mrep);
1028 return (error);
1029 }
1030
1031 /*
1032 * Do the setclientid and setclientid confirm RPCs. Called from nfs_statfs()
1033 * when a mount has just occurred and when the server replies NFSERR_EXPIRED.
1034 */
1035 int
nfsrpc_setclient(struct nfsmount * nmp,struct nfsclclient * clp,int reclaim,bool * retokp,struct ucred * cred,NFSPROC_T * p)1036 nfsrpc_setclient(struct nfsmount *nmp, struct nfsclclient *clp, int reclaim,
1037 bool *retokp, struct ucred *cred, NFSPROC_T *p)
1038 {
1039 u_int32_t *tl;
1040 struct nfsrv_descript nfsd;
1041 struct nfsrv_descript *nd = &nfsd;
1042 u_int8_t *cp = NULL, *cp2, addr[INET6_ADDRSTRLEN + 9];
1043 u_short port;
1044 int error, isinet6 = 0, callblen;
1045 nfsquad_t confirm;
1046 static u_int32_t rev = 0;
1047 struct nfsclds *dsp, *odsp;
1048 struct in6_addr a6;
1049 struct nfsclsession *tsep;
1050 struct rpc_reconupcall recon;
1051 struct nfscl_reconarg *rcp;
1052
1053 if (nfsboottime.tv_sec == 0)
1054 NFSSETBOOTTIME(nfsboottime);
1055 if (NFSHASNFSV4N(nmp)) {
1056 error = NFSERR_BADSESSION;
1057 odsp = dsp = NULL;
1058 if (retokp != NULL) {
1059 NFSLOCKMNT(nmp);
1060 odsp = TAILQ_FIRST(&nmp->nm_sess);
1061 NFSUNLOCKMNT(nmp);
1062 }
1063 if (odsp != NULL) {
1064 /*
1065 * When a session already exists, first try a
1066 * CreateSession with the extant ClientID.
1067 */
1068 dsp = malloc(sizeof(struct nfsclds) +
1069 odsp->nfsclds_servownlen + 1, M_NFSCLDS,
1070 M_WAITOK | M_ZERO);
1071 dsp->nfsclds_expire = NFSD_MONOSEC + clp->nfsc_renew;
1072 dsp->nfsclds_servownlen = odsp->nfsclds_servownlen;
1073 dsp->nfsclds_sess.nfsess_clientid =
1074 odsp->nfsclds_sess.nfsess_clientid;
1075 dsp->nfsclds_sess.nfsess_sequenceid =
1076 odsp->nfsclds_sess.nfsess_sequenceid + 1;
1077 dsp->nfsclds_flags = odsp->nfsclds_flags;
1078 if (dsp->nfsclds_servownlen > 0)
1079 memcpy(dsp->nfsclds_serverown,
1080 odsp->nfsclds_serverown,
1081 dsp->nfsclds_servownlen + 1);
1082 mtx_init(&dsp->nfsclds_mtx, "nfsds", NULL, MTX_DEF);
1083 mtx_init(&dsp->nfsclds_sess.nfsess_mtx, "nfssession",
1084 NULL, MTX_DEF);
1085 nfscl_initsessionslots(&dsp->nfsclds_sess);
1086 error = nfsrpc_createsession(nmp, &dsp->nfsclds_sess,
1087 &nmp->nm_sockreq, NULL,
1088 dsp->nfsclds_sess.nfsess_sequenceid, 1, cred, p);
1089 NFSCL_DEBUG(1, "create session for extant "
1090 "ClientID=%d\n", error);
1091 if (error != 0) {
1092 nfscl_freenfsclds(dsp);
1093 dsp = NULL;
1094 /*
1095 * If *retokp is true, return any error other
1096 * than NFSERR_STALECLIENTID,
1097 * NFSERR_BADSESSION or NFSERR_STALEDONTRECOVER
1098 * so that nfscl_recover() will not loop.
1099 */
1100 if (*retokp)
1101 return (NFSERR_IO);
1102 } else
1103 *retokp = true;
1104 } else if (retokp != NULL && *retokp)
1105 return (NFSERR_IO);
1106 if (error != 0) {
1107 /*
1108 * Either there was no previous session or the
1109 * CreateSession attempt failed, so...
1110 * do an ExchangeID followed by the CreateSession.
1111 */
1112 clp->nfsc_rev = rev++;
1113 error = nfsrpc_exchangeid(nmp, clp, &nmp->nm_sockreq, 0,
1114 NFSV4EXCH_USEPNFSMDS | NFSV4EXCH_USENONPNFS, &dsp,
1115 cred, p);
1116 NFSCL_DEBUG(1, "aft exch=%d\n", error);
1117 if (error == 0)
1118 error = nfsrpc_createsession(nmp,
1119 &dsp->nfsclds_sess, &nmp->nm_sockreq, NULL,
1120 dsp->nfsclds_sess.nfsess_sequenceid, 1,
1121 cred, p);
1122 NFSCL_DEBUG(1, "aft createsess=%d\n", error);
1123 }
1124 if (error == 0) {
1125 /*
1126 * If the session supports a backchannel, set up
1127 * the BindConnectionToSession call in the krpc
1128 * so that it is done on a reconnection.
1129 */
1130 if (nfscl_enablecallb != 0 && nfs_numnfscbd > 0) {
1131 rcp = mem_alloc(sizeof(*rcp));
1132 rcp->minorvers = nmp->nm_minorvers;
1133 memcpy(rcp->sessionid,
1134 dsp->nfsclds_sess.nfsess_sessionid,
1135 NFSX_V4SESSIONID);
1136 recon.call = nfsrpc_bindconnsess;
1137 recon.arg = rcp;
1138 CLNT_CONTROL(nmp->nm_client, CLSET_RECONUPCALL,
1139 &recon);
1140 }
1141
1142 NFSLOCKMNT(nmp);
1143 /*
1144 * The old sessions cannot be safely free'd
1145 * here, since they may still be used by
1146 * in-progress RPCs.
1147 */
1148 tsep = NULL;
1149 if (TAILQ_FIRST(&nmp->nm_sess) != NULL) {
1150 /*
1151 * Mark the old session defunct. Needed
1152 * when called from nfscl_hasexpired().
1153 */
1154 tsep = NFSMNT_MDSSESSION(nmp);
1155 tsep->nfsess_defunct = 1;
1156 }
1157 TAILQ_INSERT_HEAD(&nmp->nm_sess, dsp,
1158 nfsclds_list);
1159 /*
1160 * Wake up RPCs waiting for a slot on the
1161 * old session. These will then fail with
1162 * NFSERR_BADSESSION and be retried with the
1163 * new session by nfsv4_setsequence().
1164 * Also wakeup() processes waiting for the
1165 * new session.
1166 */
1167 if (tsep != NULL)
1168 wakeup(&tsep->nfsess_slots);
1169 wakeup(&nmp->nm_sess);
1170 NFSUNLOCKMNT(nmp);
1171 } else if (dsp != NULL)
1172 nfscl_freenfsclds(dsp);
1173 if (error == 0 && reclaim == 0) {
1174 error = nfsrpc_reclaimcomplete(nmp, cred, p);
1175 NFSCL_DEBUG(1, "aft reclaimcomp=%d\n", error);
1176 if (error == NFSERR_COMPLETEALREADY ||
1177 error == NFSERR_NOTSUPP)
1178 /* Ignore this error. */
1179 error = 0;
1180 }
1181 return (error);
1182 } else if (retokp != NULL && *retokp)
1183 return (NFSERR_IO);
1184 clp->nfsc_rev = rev++;
1185
1186 /*
1187 * Allocate a single session structure for NFSv4.0, because some of
1188 * the fields are used by NFSv4.0 although it doesn't do a session.
1189 */
1190 dsp = malloc(sizeof(struct nfsclds), M_NFSCLDS, M_WAITOK | M_ZERO);
1191 mtx_init(&dsp->nfsclds_mtx, "nfsds", NULL, MTX_DEF);
1192 mtx_init(&dsp->nfsclds_sess.nfsess_mtx, "nfssession", NULL, MTX_DEF);
1193 NFSLOCKMNT(nmp);
1194 TAILQ_INSERT_HEAD(&nmp->nm_sess, dsp, nfsclds_list);
1195 tsep = NFSMNT_MDSSESSION(nmp);
1196 NFSUNLOCKMNT(nmp);
1197
1198 nfscl_reqstart(nd, NFSPROC_SETCLIENTID, nmp, NULL, 0, NULL, NULL, 0, 0,
1199 NULL);
1200 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1201 *tl++ = txdr_unsigned(nfsboottime.tv_sec);
1202 *tl = txdr_unsigned(clp->nfsc_rev);
1203 (void) nfsm_strtom(nd, clp->nfsc_id, clp->nfsc_idlen);
1204
1205 /*
1206 * set up the callback address
1207 */
1208 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1209 *tl = txdr_unsigned(NFS_CALLBCKPROG);
1210 callblen = strlen(nfsv4_callbackaddr);
1211 if (callblen == 0)
1212 cp = nfscl_getmyip(nmp, &a6, &isinet6);
1213 if (nfscl_enablecallb && nfs_numnfscbd > 0 &&
1214 (callblen > 0 || cp != NULL)) {
1215 port = htons(nfsv4_cbport);
1216 cp2 = (u_int8_t *)&port;
1217 #ifdef INET6
1218 if ((callblen > 0 &&
1219 strchr(nfsv4_callbackaddr, ':')) || isinet6) {
1220 char ip6buf[INET6_ADDRSTRLEN], *ip6add;
1221
1222 (void) nfsm_strtom(nd, "tcp6", 4);
1223 if (callblen == 0) {
1224 ip6_sprintf(ip6buf, (struct in6_addr *)cp);
1225 ip6add = ip6buf;
1226 } else {
1227 ip6add = nfsv4_callbackaddr;
1228 }
1229 snprintf(addr, INET6_ADDRSTRLEN + 9, "%s.%d.%d",
1230 ip6add, cp2[0], cp2[1]);
1231 } else
1232 #endif
1233 {
1234 (void) nfsm_strtom(nd, "tcp", 3);
1235 if (callblen == 0)
1236 snprintf(addr, INET6_ADDRSTRLEN + 9,
1237 "%d.%d.%d.%d.%d.%d", cp[0], cp[1],
1238 cp[2], cp[3], cp2[0], cp2[1]);
1239 else
1240 snprintf(addr, INET6_ADDRSTRLEN + 9,
1241 "%s.%d.%d", nfsv4_callbackaddr,
1242 cp2[0], cp2[1]);
1243 }
1244 (void) nfsm_strtom(nd, addr, strlen(addr));
1245 } else {
1246 (void) nfsm_strtom(nd, "tcp", 3);
1247 (void) nfsm_strtom(nd, "0.0.0.0.0.0", 11);
1248 }
1249 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1250 *tl = txdr_unsigned(clp->nfsc_cbident);
1251 nd->nd_flag |= ND_USEGSSNAME;
1252 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
1253 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
1254 if (error)
1255 return (error);
1256 if (nd->nd_repstat == 0) {
1257 NFSM_DISSECT(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1258 tsep->nfsess_clientid.lval[0] = *tl++;
1259 tsep->nfsess_clientid.lval[1] = *tl++;
1260 confirm.lval[0] = *tl++;
1261 confirm.lval[1] = *tl;
1262 m_freem(nd->nd_mrep);
1263 nd->nd_mrep = NULL;
1264
1265 /*
1266 * and confirm it.
1267 */
1268 nfscl_reqstart(nd, NFSPROC_SETCLIENTIDCFRM, nmp, NULL, 0, NULL,
1269 NULL, 0, 0, NULL);
1270 NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1271 *tl++ = tsep->nfsess_clientid.lval[0];
1272 *tl++ = tsep->nfsess_clientid.lval[1];
1273 *tl++ = confirm.lval[0];
1274 *tl = confirm.lval[1];
1275 nd->nd_flag |= ND_USEGSSNAME;
1276 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p,
1277 cred, NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
1278 if (error)
1279 return (error);
1280 m_freem(nd->nd_mrep);
1281 nd->nd_mrep = NULL;
1282 }
1283 error = nd->nd_repstat;
1284 nfsmout:
1285 m_freem(nd->nd_mrep);
1286 return (error);
1287 }
1288
1289 /*
1290 * nfs getattr call.
1291 */
1292 int
nfsrpc_getattr(vnode_t vp,struct ucred * cred,NFSPROC_T * p,struct nfsvattr * nap)1293 nfsrpc_getattr(vnode_t vp, struct ucred *cred, NFSPROC_T *p,
1294 struct nfsvattr *nap)
1295 {
1296 struct nfsrv_descript nfsd, *nd = &nfsd;
1297 int error;
1298 nfsattrbit_t attrbits;
1299 struct nfsnode *np;
1300 struct nfsmount *nmp;
1301
1302 nmp = VFSTONFS(vp->v_mount);
1303 np = VTONFS(vp);
1304 if ((nmp->nm_privflag & NFSMNTP_FAKEROOTFH) != 0 &&
1305 nmp->nm_fhsize == 0) {
1306 /* Attempt to get the actual root file handle. */
1307 error = nfsrpc_getdirpath(nmp, NFSMNT_DIRPATH(nmp), cred, p);
1308 if (error != 0)
1309 return (EACCES);
1310 if (np->n_fhp->nfh_len == NFSX_FHMAX + 1)
1311 nfscl_statfs(vp, cred, p);
1312 }
1313 NFSCL_REQSTART(nd, NFSPROC_GETATTR, vp, cred);
1314 if (nd->nd_flag & ND_NFSV4) {
1315 NFSGETATTR_ATTRBIT(&attrbits);
1316 (void) nfsrv_putattrbit(nd, &attrbits);
1317 }
1318 error = nfscl_request(nd, vp, p, cred);
1319 if (error)
1320 return (error);
1321 if (!nd->nd_repstat)
1322 error = nfsm_loadattr(nd, nap);
1323 else
1324 error = nd->nd_repstat;
1325 m_freem(nd->nd_mrep);
1326 return (error);
1327 }
1328
1329 /*
1330 * nfs getattr call with non-vnode arguments.
1331 */
1332 int
nfsrpc_getattrnovp(struct nfsmount * nmp,u_int8_t * fhp,int fhlen,int syscred,struct ucred * cred,NFSPROC_T * p,struct nfsvattr * nap,u_int64_t * xidp,uint32_t * leasep)1333 nfsrpc_getattrnovp(struct nfsmount *nmp, u_int8_t *fhp, int fhlen, int syscred,
1334 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, u_int64_t *xidp,
1335 uint32_t *leasep)
1336 {
1337 struct nfsrv_descript nfsd, *nd = &nfsd;
1338 int error, vers = NFS_VER2;
1339 nfsattrbit_t attrbits;
1340
1341 nfscl_reqstart(nd, NFSPROC_GETATTR, nmp, fhp, fhlen, NULL, NULL, 0, 0,
1342 cred);
1343 if (nd->nd_flag & ND_NFSV4) {
1344 vers = NFS_VER4;
1345 NFSGETATTR_ATTRBIT(&attrbits);
1346 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_LEASETIME);
1347 (void) nfsrv_putattrbit(nd, &attrbits);
1348 } else if (nd->nd_flag & ND_NFSV3) {
1349 vers = NFS_VER3;
1350 }
1351 if (syscred)
1352 nd->nd_flag |= ND_USEGSSNAME;
1353 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
1354 NFS_PROG, vers, NULL, 1, xidp, NULL);
1355 if (error)
1356 return (error);
1357 if (nd->nd_repstat == 0) {
1358 if ((nd->nd_flag & ND_NFSV4) != 0)
1359 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0,
1360 NULL, NULL, NULL, NULL, NULL, 0, NULL, leasep, NULL,
1361 NULL, NULL, NULL, NULL);
1362 else
1363 error = nfsm_loadattr(nd, nap);
1364 } else
1365 error = nd->nd_repstat;
1366 m_freem(nd->nd_mrep);
1367 return (error);
1368 }
1369
1370 /*
1371 * Do an nfs setattr operation.
1372 */
1373 int
nfsrpc_setattr(vnode_t vp,struct vattr * vap,NFSACL_T * aclp,acl_type_t aclt,struct ucred * cred,NFSPROC_T * p,struct nfsvattr * rnap,int * attrflagp)1374 nfsrpc_setattr(vnode_t vp, struct vattr *vap, NFSACL_T *aclp, acl_type_t aclt,
1375 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *rnap, int *attrflagp)
1376 {
1377 int error, expireret = 0, openerr, retrycnt;
1378 u_int32_t clidrev = 0, mode;
1379 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1380 struct nfsfh *nfhp;
1381 nfsv4stateid_t stateid;
1382 void *lckp;
1383
1384 if (nmp->nm_clp != NULL)
1385 clidrev = nmp->nm_clp->nfsc_clientidrev;
1386 if (vap != NULL && NFSATTRISSET(u_quad_t, vap, va_size))
1387 mode = NFSV4OPEN_ACCESSWRITE;
1388 else
1389 mode = NFSV4OPEN_ACCESSREAD;
1390 retrycnt = 0;
1391 do {
1392 lckp = NULL;
1393 openerr = 1;
1394 if (NFSHASNFSV4(nmp)) {
1395 nfhp = VTONFS(vp)->n_fhp;
1396 error = nfscl_getstateid(vp, nfhp->nfh_fh,
1397 nfhp->nfh_len, mode, 0, cred, p, &stateid, &lckp);
1398 if (error && vp->v_type == VREG &&
1399 (mode == NFSV4OPEN_ACCESSWRITE ||
1400 nfstest_openallsetattr)) {
1401 /*
1402 * No Open stateid, so try and open the file
1403 * now.
1404 */
1405 if (mode == NFSV4OPEN_ACCESSWRITE)
1406 openerr = nfsrpc_open(vp, FWRITE, cred,
1407 p);
1408 else
1409 openerr = nfsrpc_open(vp, FREAD, cred,
1410 p);
1411 if (!openerr)
1412 (void) nfscl_getstateid(vp,
1413 nfhp->nfh_fh, nfhp->nfh_len,
1414 mode, 0, cred, p, &stateid, &lckp);
1415 }
1416 }
1417 if (vap != NULL)
1418 error = nfsrpc_setattrrpc(vp, vap, &stateid, cred, p,
1419 rnap, attrflagp);
1420 else
1421 error = nfsrpc_setaclrpc(vp, cred, p, aclp, aclt,
1422 &stateid);
1423 if (error == NFSERR_OPENMODE && mode == NFSV4OPEN_ACCESSREAD) {
1424 NFSLOCKMNT(nmp);
1425 nmp->nm_state |= NFSSTA_OPENMODE;
1426 NFSUNLOCKMNT(nmp);
1427 }
1428 if (error == NFSERR_STALESTATEID)
1429 nfscl_initiate_recovery(nmp->nm_clp);
1430 if (lckp != NULL)
1431 nfscl_lockderef(lckp);
1432 if (!openerr)
1433 (void) nfsrpc_close(vp, 0, p);
1434 if (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
1435 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
1436 error == NFSERR_OLDSTATEID || error == NFSERR_BADSESSION) {
1437 (void) nfs_catnap(PZERO, error, "nfs_setattr");
1438 } else if ((error == NFSERR_EXPIRED ||
1439 ((!NFSHASINT(nmp) || !NFSHASNFSV4N(nmp)) &&
1440 error == NFSERR_BADSTATEID)) && clidrev != 0) {
1441 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
1442 } else if (error == NFSERR_BADSTATEID && NFSHASINT(nmp) &&
1443 NFSHASNFSV4N(nmp)) {
1444 error = EIO;
1445 }
1446 retrycnt++;
1447 } while (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
1448 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
1449 error == NFSERR_BADSESSION ||
1450 (error == NFSERR_OLDSTATEID && retrycnt < 20) ||
1451 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
1452 expireret == 0 && clidrev != 0 && retrycnt < 4) ||
1453 (error == NFSERR_OPENMODE && mode == NFSV4OPEN_ACCESSREAD &&
1454 retrycnt < 4));
1455 if (error && retrycnt >= 4)
1456 error = EIO;
1457 return (error);
1458 }
1459
1460 static int
nfsrpc_setattrrpc(vnode_t vp,struct vattr * vap,nfsv4stateid_t * stateidp,struct ucred * cred,NFSPROC_T * p,struct nfsvattr * rnap,int * attrflagp)1461 nfsrpc_setattrrpc(vnode_t vp, struct vattr *vap,
1462 nfsv4stateid_t *stateidp, struct ucred *cred, NFSPROC_T *p,
1463 struct nfsvattr *rnap, int *attrflagp)
1464 {
1465 u_int32_t *tl;
1466 struct nfsrv_descript nfsd, *nd = &nfsd;
1467 int error;
1468 nfsattrbit_t attrbits;
1469
1470 *attrflagp = 0;
1471 NFSCL_REQSTART(nd, NFSPROC_SETATTR, vp, cred);
1472 if (nd->nd_flag & ND_NFSV4)
1473 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID);
1474 vap->va_type = vp->v_type;
1475 nfscl_fillsattr(nd, vap, vp, NFSSATTR_FULL, 0);
1476 if (nd->nd_flag & ND_NFSV3) {
1477 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1478 *tl = newnfs_false;
1479 } else if (nd->nd_flag & ND_NFSV4) {
1480 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1481 *tl = txdr_unsigned(NFSV4OP_GETATTR);
1482 NFSGETATTR_ATTRBIT(&attrbits);
1483 (void) nfsrv_putattrbit(nd, &attrbits);
1484 }
1485 error = nfscl_request(nd, vp, p, cred);
1486 if (error)
1487 return (error);
1488 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4))
1489 error = nfscl_wcc_data(nd, vp, rnap, attrflagp, NULL, NULL);
1490 if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) == ND_NFSV4 && !error)
1491 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
1492 if (!(nd->nd_flag & ND_NFSV3) && !nd->nd_repstat && !error)
1493 error = nfscl_postop_attr(nd, rnap, attrflagp);
1494 m_freem(nd->nd_mrep);
1495 if (nd->nd_repstat && !error)
1496 error = nd->nd_repstat;
1497 return (error);
1498 }
1499
1500 /*
1501 * nfs lookup rpc
1502 */
1503 int
nfsrpc_lookup(vnode_t dvp,char * name,int len,struct ucred * cred,NFSPROC_T * p,struct nfsvattr * dnap,struct nfsvattr * nap,struct nfsfh ** nfhpp,int * attrflagp,int * dattrflagp,uint32_t openmode)1504 nfsrpc_lookup(vnode_t dvp, char *name, int len, struct ucred *cred,
1505 NFSPROC_T *p, struct nfsvattr *dnap, struct nfsvattr *nap,
1506 struct nfsfh **nfhpp, int *attrflagp, int *dattrflagp, uint32_t openmode)
1507 {
1508 uint32_t deleg, rflags, *tl;
1509 struct nfsrv_descript nfsd, *nd = &nfsd;
1510 struct nfsmount *nmp;
1511 struct nfsnode *np;
1512 struct nfsfh *nfhp;
1513 nfsattrbit_t attrbits;
1514 int error = 0, lookupp = 0, newone, ret, retop;
1515 uint8_t own[NFSV4CL_LOCKNAMELEN];
1516 struct nfsclopen *op;
1517 struct nfscldeleg *ndp;
1518 nfsv4stateid_t stateid;
1519
1520 *attrflagp = 0;
1521 *dattrflagp = 0;
1522 if (dvp->v_type != VDIR)
1523 return (ENOTDIR);
1524 nmp = VFSTONFS(dvp->v_mount);
1525 if (len > NFS_MAXNAMLEN)
1526 return (ENAMETOOLONG);
1527 if (NFSHASNFSV4(nmp) && len == 1 &&
1528 name[0] == '.') {
1529 /*
1530 * Just return the current dir's fh.
1531 */
1532 np = VTONFS(dvp);
1533 nfhp = malloc(sizeof (struct nfsfh) +
1534 np->n_fhp->nfh_len, M_NFSFH, M_WAITOK);
1535 nfhp->nfh_len = np->n_fhp->nfh_len;
1536 NFSBCOPY(np->n_fhp->nfh_fh, nfhp->nfh_fh, nfhp->nfh_len);
1537 *nfhpp = nfhp;
1538 return (0);
1539 }
1540 if (NFSHASNFSV4(nmp) && len == 2 &&
1541 name[0] == '.' && name[1] == '.') {
1542 lookupp = 1;
1543 openmode = 0;
1544 NFSCL_REQSTART(nd, NFSPROC_LOOKUPP, dvp, cred);
1545 } else if (openmode != 0) {
1546 NFSCL_REQSTART(nd, NFSPROC_LOOKUPOPEN, dvp, cred);
1547 nfsm_strtom(nd, name, len);
1548 } else {
1549 NFSCL_REQSTART(nd, NFSPROC_LOOKUP, dvp, cred);
1550 (void) nfsm_strtom(nd, name, len);
1551 }
1552 if (nd->nd_flag & ND_NFSV4) {
1553 NFSGETATTR_ATTRBIT(&attrbits);
1554 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1555 *tl++ = txdr_unsigned(NFSV4OP_GETFH);
1556 *tl = txdr_unsigned(NFSV4OP_GETATTR);
1557 (void) nfsrv_putattrbit(nd, &attrbits);
1558 if (openmode != 0) {
1559 /* Test for a VREG file. */
1560 NFSZERO_ATTRBIT(&attrbits);
1561 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TYPE);
1562 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
1563 *tl = txdr_unsigned(NFSV4OP_VERIFY);
1564 nfsrv_putattrbit(nd, &attrbits);
1565 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_UNSIGNED);
1566 *tl++ = txdr_unsigned(NFSX_UNSIGNED);
1567 *tl = vtonfsv34_type(VREG);
1568
1569 /* Attempt the Open for VREG. */
1570 nfscl_filllockowner(NULL, own, F_POSIX);
1571 NFSM_BUILD(tl, uint32_t *, 6 * NFSX_UNSIGNED);
1572 *tl++ = txdr_unsigned(NFSV4OP_OPEN);
1573 *tl++ = 0; /* seqid, ignored. */
1574 *tl++ = txdr_unsigned(openmode | NFSV4OPEN_WANTNODELEG);
1575 *tl++ = txdr_unsigned(NFSV4OPEN_DENYNONE);
1576 *tl++ = 0; /* ClientID, ignored. */
1577 *tl = 0;
1578 nfsm_strtom(nd, own, NFSV4CL_LOCKNAMELEN);
1579 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_UNSIGNED);
1580 *tl++ = txdr_unsigned(NFSV4OPEN_NOCREATE);
1581 *tl = txdr_unsigned(NFSV4OPEN_CLAIMFH);
1582 }
1583 }
1584 error = nfscl_request(nd, dvp, p, cred);
1585 if (error)
1586 return (error);
1587 ndp = NULL;
1588 if (nd->nd_repstat) {
1589 /*
1590 * When an NFSv4 Lookupp returns ENOENT, it means that
1591 * the lookup is at the root of an fs, so return this dir.
1592 */
1593 if (nd->nd_repstat == NFSERR_NOENT && lookupp) {
1594 np = VTONFS(dvp);
1595 nfhp = malloc(sizeof (struct nfsfh) +
1596 np->n_fhp->nfh_len, M_NFSFH, M_WAITOK);
1597 nfhp->nfh_len = np->n_fhp->nfh_len;
1598 NFSBCOPY(np->n_fhp->nfh_fh, nfhp->nfh_fh, nfhp->nfh_len);
1599 *nfhpp = nfhp;
1600 m_freem(nd->nd_mrep);
1601 return (0);
1602 }
1603 if (nd->nd_flag & ND_NFSV3)
1604 error = nfscl_postop_attr(nd, dnap, dattrflagp);
1605 else if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) ==
1606 ND_NFSV4) {
1607 /* Load the directory attributes. */
1608 error = nfsm_loadattr(nd, dnap);
1609 if (error != 0)
1610 goto nfsmout;
1611 *dattrflagp = 1;
1612 }
1613 /* Check Lookup operation reply status. */
1614 if (openmode != 0 && (nd->nd_flag & ND_NOMOREDATA) == 0) {
1615 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
1616 if (*++tl != 0)
1617 goto nfsmout;
1618 }
1619 /* Look for GetFH reply. */
1620 if (openmode != 0 && (nd->nd_flag & ND_NOMOREDATA) == 0) {
1621 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
1622 if (*++tl != 0)
1623 goto nfsmout;
1624 error = nfsm_getfh(nd, nfhpp);
1625 if (error)
1626 goto nfsmout;
1627 }
1628 /* Look for Getattr reply. */
1629 if (openmode != 0 && (nd->nd_flag & ND_NOMOREDATA) == 0) {
1630 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
1631 if (*++tl != 0)
1632 goto nfsmout;
1633 error = nfsm_loadattr(nd, nap);
1634 if (error == 0) {
1635 /*
1636 * We have now successfully completed the
1637 * lookup, so set nd_repstat to 0.
1638 */
1639 nd->nd_repstat = 0;
1640 *attrflagp = 1;
1641 }
1642 }
1643 goto nfsmout;
1644 }
1645 if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) == ND_NFSV4) {
1646 /* Load the directory attributes. */
1647 error = nfsm_loadattr(nd, dnap);
1648 if (error != 0)
1649 goto nfsmout;
1650 *dattrflagp = 1;
1651 /* Skip over the Lookup and GetFH operation status values. */
1652 NFSM_DISSECT(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1653 }
1654 error = nfsm_getfh(nd, nfhpp);
1655 if (error)
1656 goto nfsmout;
1657
1658 error = nfscl_postop_attr(nd, nap, attrflagp);
1659 if (openmode != 0 && error == 0) {
1660 NFSM_DISSECT(tl, uint32_t *, NFSX_STATEID +
1661 10 * NFSX_UNSIGNED);
1662 tl += 4; /* Skip over Verify+Open status. */
1663 stateid.seqid = *tl++;
1664 stateid.other[0] = *tl++;
1665 stateid.other[1] = *tl++;
1666 stateid.other[2] = *tl;
1667 rflags = fxdr_unsigned(uint32_t, *(tl + 6));
1668 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
1669 if (error != 0)
1670 goto nfsmout;
1671 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
1672 deleg = fxdr_unsigned(uint32_t, *tl);
1673 if (deleg == NFSV4OPEN_DELEGATEREAD ||
1674 deleg == NFSV4OPEN_DELEGATEWRITE) {
1675 /*
1676 * Just need to fill in the fields used by
1677 * nfscl_trydelegreturn().
1678 * Mark the mount point as acquiring
1679 * delegations, so NFSPROC_LOOKUPOPEN will
1680 * no longer be done.
1681 */
1682 NFSLOCKMNT(nmp);
1683 nmp->nm_privflag |= NFSMNTP_DELEGISSUED;
1684 NFSUNLOCKMNT(nmp);
1685 ndp = malloc(sizeof(struct nfscldeleg) +
1686 (*nfhpp)->nfh_len, M_NFSCLDELEG, M_WAITOK);
1687 ndp->nfsdl_fhlen = (*nfhpp)->nfh_len;
1688 NFSBCOPY((*nfhpp)->nfh_fh, ndp->nfsdl_fh,
1689 ndp->nfsdl_fhlen);
1690 newnfs_copyincred(cred, &ndp->nfsdl_cred);
1691 NFSM_DISSECT(tl, uint32_t *, NFSX_STATEID);
1692 ndp->nfsdl_stateid.seqid = *tl++;
1693 ndp->nfsdl_stateid.other[0] = *tl++;
1694 ndp->nfsdl_stateid.other[1] = *tl++;
1695 ndp->nfsdl_stateid.other[2] = *tl++;
1696 } else if (deleg == NFSV4OPEN_DELEGATENONEEXT &&
1697 NFSHASNFSV4N(nmp)) {
1698 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
1699 deleg = fxdr_unsigned(uint32_t, *tl);
1700 if (deleg == NFSV4OPEN_CONTENTION ||
1701 deleg == NFSV4OPEN_RESOURCE)
1702 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
1703 } else if (deleg != NFSV4OPEN_DELEGATENONE) {
1704 error = NFSERR_BADXDR;
1705 goto nfsmout;
1706 }
1707 ret = nfscl_open(dvp, (*nfhpp)->nfh_fh, (*nfhpp)->nfh_len,
1708 openmode, 0, cred, p, NULL, &op, &newone, &retop, 1, true);
1709 if (ret != 0)
1710 goto nfsmout;
1711 if (newone != 0) {
1712 op->nfso_stateid.seqid = stateid.seqid;
1713 op->nfso_stateid.other[0] = stateid.other[0];
1714 op->nfso_stateid.other[1] = stateid.other[1];
1715 op->nfso_stateid.other[2] = stateid.other[2];
1716 op->nfso_mode = openmode;
1717 } else {
1718 op->nfso_stateid.seqid = stateid.seqid;
1719 if (retop == NFSCLOPEN_DOOPEN)
1720 op->nfso_mode |= openmode;
1721 }
1722 if ((rflags & NFSV4OPEN_LOCKTYPEPOSIX) != 0 ||
1723 nfscl_assumeposixlocks)
1724 op->nfso_posixlock = 1;
1725 else
1726 op->nfso_posixlock = 0;
1727 nfscl_openrelease(nmp, op, 0, 0);
1728 if (ndp != NULL) {
1729 /*
1730 * Since we do not have the vnode, we
1731 * cannot invalidate cached attributes.
1732 * Just return the delegation.
1733 */
1734 nfscl_trydelegreturn(ndp, cred, nmp, p);
1735 }
1736 }
1737 if ((nd->nd_flag & ND_NFSV3) && !error)
1738 error = nfscl_postop_attr(nd, dnap, dattrflagp);
1739 nfsmout:
1740 m_freem(nd->nd_mrep);
1741 if (!error && nd->nd_repstat)
1742 error = nd->nd_repstat;
1743 free(ndp, M_NFSCLDELEG);
1744 return (error);
1745 }
1746
1747 /*
1748 * Do a readlink rpc.
1749 */
1750 int
nfsrpc_readlink(vnode_t vp,struct uio * uiop,struct ucred * cred,NFSPROC_T * p,struct nfsvattr * nap,int * attrflagp)1751 nfsrpc_readlink(vnode_t vp, struct uio *uiop, struct ucred *cred,
1752 NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp)
1753 {
1754 u_int32_t *tl;
1755 struct nfsrv_descript nfsd, *nd = &nfsd;
1756 struct nfsnode *np = VTONFS(vp);
1757 nfsattrbit_t attrbits;
1758 int error, len, cangetattr = 1;
1759
1760 *attrflagp = 0;
1761 NFSCL_REQSTART(nd, NFSPROC_READLINK, vp, cred);
1762 if (nd->nd_flag & ND_NFSV4) {
1763 /*
1764 * And do a Getattr op.
1765 */
1766 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1767 *tl = txdr_unsigned(NFSV4OP_GETATTR);
1768 NFSGETATTR_ATTRBIT(&attrbits);
1769 (void) nfsrv_putattrbit(nd, &attrbits);
1770 }
1771 error = nfscl_request(nd, vp, p, cred);
1772 if (error)
1773 return (error);
1774 if (nd->nd_flag & ND_NFSV3)
1775 error = nfscl_postop_attr(nd, nap, attrflagp);
1776 if (!nd->nd_repstat && !error) {
1777 NFSM_STRSIZ(len, NFS_MAXPATHLEN);
1778 /*
1779 * This seems weird to me, but must have been added to
1780 * FreeBSD for some reason. The only thing I can think of
1781 * is that there was/is some server that replies with
1782 * more link data than it should?
1783 */
1784 if (len == NFS_MAXPATHLEN) {
1785 NFSLOCKNODE(np);
1786 if (np->n_size > 0 && np->n_size < NFS_MAXPATHLEN) {
1787 len = np->n_size;
1788 cangetattr = 0;
1789 }
1790 NFSUNLOCKNODE(np);
1791 }
1792 error = nfsm_mbufuio(nd, uiop, len);
1793 if ((nd->nd_flag & ND_NFSV4) && !error && cangetattr)
1794 error = nfscl_postop_attr(nd, nap, attrflagp);
1795 }
1796 if (nd->nd_repstat && !error)
1797 error = nd->nd_repstat;
1798 nfsmout:
1799 m_freem(nd->nd_mrep);
1800 return (error);
1801 }
1802
1803 /*
1804 * Read operation.
1805 */
1806 int
nfsrpc_read(vnode_t vp,struct uio * uiop,struct ucred * cred,NFSPROC_T * p,struct nfsvattr * nap,int * attrflagp)1807 nfsrpc_read(vnode_t vp, struct uio *uiop, struct ucred *cred,
1808 NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp)
1809 {
1810 int error, expireret = 0, retrycnt;
1811 u_int32_t clidrev = 0;
1812 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1813 struct nfsnode *np = VTONFS(vp);
1814 struct ucred *newcred;
1815 struct nfsfh *nfhp = NULL;
1816 nfsv4stateid_t stateid;
1817 void *lckp;
1818
1819 if (nmp->nm_clp != NULL)
1820 clidrev = nmp->nm_clp->nfsc_clientidrev;
1821 newcred = cred;
1822 if (NFSHASNFSV4(nmp)) {
1823 nfhp = np->n_fhp;
1824 newcred = NFSNEWCRED(cred);
1825 }
1826 retrycnt = 0;
1827 do {
1828 lckp = NULL;
1829 if (NFSHASNFSV4(nmp))
1830 (void)nfscl_getstateid(vp, nfhp->nfh_fh, nfhp->nfh_len,
1831 NFSV4OPEN_ACCESSREAD, 0, newcred, p, &stateid,
1832 &lckp);
1833 error = nfsrpc_readrpc(vp, uiop, newcred, &stateid, p, nap,
1834 attrflagp);
1835 if (error == NFSERR_OPENMODE) {
1836 NFSLOCKMNT(nmp);
1837 nmp->nm_state |= NFSSTA_OPENMODE;
1838 NFSUNLOCKMNT(nmp);
1839 }
1840 if (error == NFSERR_STALESTATEID)
1841 nfscl_initiate_recovery(nmp->nm_clp);
1842 if (lckp != NULL)
1843 nfscl_lockderef(lckp);
1844 if (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
1845 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
1846 error == NFSERR_OLDSTATEID || error == NFSERR_BADSESSION) {
1847 (void) nfs_catnap(PZERO, error, "nfs_read");
1848 } else if ((error == NFSERR_EXPIRED ||
1849 ((!NFSHASINT(nmp) || !NFSHASNFSV4N(nmp)) &&
1850 error == NFSERR_BADSTATEID)) && clidrev != 0) {
1851 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
1852 } else if (error == NFSERR_BADSTATEID && NFSHASINT(nmp) &&
1853 NFSHASNFSV4N(nmp)) {
1854 error = EIO;
1855 }
1856 retrycnt++;
1857 } while (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
1858 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
1859 error == NFSERR_BADSESSION ||
1860 (error == NFSERR_OLDSTATEID && retrycnt < 20) ||
1861 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
1862 expireret == 0 && clidrev != 0 && retrycnt < 4) ||
1863 (error == NFSERR_OPENMODE && retrycnt < 4));
1864 if (error && retrycnt >= 4)
1865 error = EIO;
1866 if (NFSHASNFSV4(nmp))
1867 NFSFREECRED(newcred);
1868 return (error);
1869 }
1870
1871 /*
1872 * The actual read RPC.
1873 */
1874 static int
nfsrpc_readrpc(vnode_t vp,struct uio * uiop,struct ucred * cred,nfsv4stateid_t * stateidp,NFSPROC_T * p,struct nfsvattr * nap,int * attrflagp)1875 nfsrpc_readrpc(vnode_t vp, struct uio *uiop, struct ucred *cred,
1876 nfsv4stateid_t *stateidp, NFSPROC_T *p, struct nfsvattr *nap,
1877 int *attrflagp)
1878 {
1879 u_int32_t *tl;
1880 int error = 0, len, retlen, tsiz, eof = 0;
1881 struct nfsrv_descript nfsd;
1882 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1883 struct nfsrv_descript *nd = &nfsd;
1884 int rsize;
1885 off_t tmp_off;
1886
1887 *attrflagp = 0;
1888 tsiz = uiop->uio_resid;
1889 tmp_off = uiop->uio_offset + tsiz;
1890 NFSLOCKMNT(nmp);
1891 if (tmp_off > nmp->nm_maxfilesize || tmp_off < uiop->uio_offset) {
1892 NFSUNLOCKMNT(nmp);
1893 return (EFBIG);
1894 }
1895 rsize = nmp->nm_rsize;
1896 NFSUNLOCKMNT(nmp);
1897 nd->nd_mrep = NULL;
1898 while (tsiz > 0) {
1899 *attrflagp = 0;
1900 len = (tsiz > rsize) ? rsize : tsiz;
1901 NFSCL_REQSTART(nd, NFSPROC_READ, vp, cred);
1902 if (nd->nd_flag & ND_NFSV4)
1903 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID);
1904 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED * 3);
1905 if (nd->nd_flag & ND_NFSV2) {
1906 *tl++ = txdr_unsigned(uiop->uio_offset);
1907 *tl++ = txdr_unsigned(len);
1908 *tl = 0;
1909 } else {
1910 txdr_hyper(uiop->uio_offset, tl);
1911 *(tl + 2) = txdr_unsigned(len);
1912 }
1913 /*
1914 * Since I can't do a Getattr for NFSv4 for Write, there
1915 * doesn't seem any point in doing one here, either.
1916 * (See the comment in nfsrpc_writerpc() for more info.)
1917 */
1918 error = nfscl_request(nd, vp, p, cred);
1919 if (error)
1920 return (error);
1921 if (nd->nd_flag & ND_NFSV3) {
1922 error = nfscl_postop_attr(nd, nap, attrflagp);
1923 } else if (!nd->nd_repstat && (nd->nd_flag & ND_NFSV2)) {
1924 error = nfsm_loadattr(nd, nap);
1925 if (!error)
1926 *attrflagp = 1;
1927 }
1928 if (nd->nd_repstat || error) {
1929 if (!error)
1930 error = nd->nd_repstat;
1931 goto nfsmout;
1932 }
1933 if (nd->nd_flag & ND_NFSV3) {
1934 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1935 eof = fxdr_unsigned(int, *(tl + 1));
1936 } else if (nd->nd_flag & ND_NFSV4) {
1937 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
1938 eof = fxdr_unsigned(int, *tl);
1939 }
1940 NFSM_STRSIZ(retlen, len);
1941 error = nfsm_mbufuio(nd, uiop, retlen);
1942 if (error)
1943 goto nfsmout;
1944 m_freem(nd->nd_mrep);
1945 nd->nd_mrep = NULL;
1946 tsiz -= retlen;
1947 if (!(nd->nd_flag & ND_NFSV2)) {
1948 if (eof || retlen == 0)
1949 tsiz = 0;
1950 } else if (retlen < len)
1951 tsiz = 0;
1952 }
1953 return (0);
1954 nfsmout:
1955 if (nd->nd_mrep != NULL)
1956 m_freem(nd->nd_mrep);
1957 return (error);
1958 }
1959
1960 /*
1961 * nfs write operation
1962 * When called_from_strategy != 0, it should return EIO for an error that
1963 * indicates recovery is in progress, so that the buffer will be left
1964 * dirty and be written back to the server later. If it loops around,
1965 * the recovery thread could get stuck waiting for the buffer and recovery
1966 * will then deadlock.
1967 */
1968 int
nfsrpc_write(vnode_t vp,struct uio * uiop,int * iomode,int * must_commit,struct ucred * cred,NFSPROC_T * p,struct nfsvattr * nap,int * attrflagp,int called_from_strategy,int ioflag)1969 nfsrpc_write(vnode_t vp, struct uio *uiop, int *iomode, int *must_commit,
1970 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp,
1971 int called_from_strategy, int ioflag)
1972 {
1973 int error, expireret = 0, retrycnt, nostateid;
1974 u_int32_t clidrev = 0;
1975 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1976 struct nfsnode *np = VTONFS(vp);
1977 struct ucred *newcred;
1978 struct nfsfh *nfhp = NULL;
1979 nfsv4stateid_t stateid;
1980 void *lckp;
1981
1982 KASSERT(*must_commit >= 0 && *must_commit <= 2,
1983 ("nfsrpc_write: must_commit out of range=%d", *must_commit));
1984 if (nmp->nm_clp != NULL)
1985 clidrev = nmp->nm_clp->nfsc_clientidrev;
1986 newcred = cred;
1987 if (NFSHASNFSV4(nmp)) {
1988 newcred = NFSNEWCRED(cred);
1989 nfhp = np->n_fhp;
1990 }
1991 retrycnt = 0;
1992 do {
1993 lckp = NULL;
1994 nostateid = 0;
1995 if (NFSHASNFSV4(nmp)) {
1996 (void)nfscl_getstateid(vp, nfhp->nfh_fh, nfhp->nfh_len,
1997 NFSV4OPEN_ACCESSWRITE, 0, newcred, p, &stateid,
1998 &lckp);
1999 if (stateid.other[0] == 0 && stateid.other[1] == 0 &&
2000 stateid.other[2] == 0) {
2001 nostateid = 1;
2002 NFSCL_DEBUG(1, "stateid0 in write\n");
2003 }
2004 }
2005
2006 /*
2007 * If there is no stateid for NFSv4, it means this is an
2008 * extraneous write after close. Basically a poorly
2009 * implemented buffer cache. Just don't do the write.
2010 */
2011 if (nostateid)
2012 error = 0;
2013 else
2014 error = nfsrpc_writerpc(vp, uiop, iomode, must_commit,
2015 newcred, &stateid, p, nap, attrflagp, ioflag);
2016 if (error == NFSERR_STALESTATEID)
2017 nfscl_initiate_recovery(nmp->nm_clp);
2018 if (lckp != NULL)
2019 nfscl_lockderef(lckp);
2020 if (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
2021 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
2022 error == NFSERR_OLDSTATEID || error == NFSERR_BADSESSION) {
2023 (void) nfs_catnap(PZERO, error, "nfs_write");
2024 } else if ((error == NFSERR_EXPIRED ||
2025 ((!NFSHASINT(nmp) || !NFSHASNFSV4N(nmp)) &&
2026 error == NFSERR_BADSTATEID)) && clidrev != 0) {
2027 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
2028 } else if (error == NFSERR_BADSTATEID && NFSHASINT(nmp) &&
2029 NFSHASNFSV4N(nmp)) {
2030 error = EIO;
2031 }
2032 retrycnt++;
2033 } while (error == NFSERR_GRACE || error == NFSERR_DELAY ||
2034 ((error == NFSERR_STALESTATEID || error == NFSERR_BADSESSION ||
2035 error == NFSERR_STALEDONTRECOVER) && called_from_strategy == 0) ||
2036 (error == NFSERR_OLDSTATEID && retrycnt < 20) ||
2037 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
2038 expireret == 0 && clidrev != 0 && retrycnt < 4));
2039 if (error != 0 && (retrycnt >= 4 ||
2040 ((error == NFSERR_STALESTATEID || error == NFSERR_BADSESSION ||
2041 error == NFSERR_STALEDONTRECOVER) && called_from_strategy != 0)))
2042 error = EIO;
2043 if (NFSHASNFSV4(nmp))
2044 NFSFREECRED(newcred);
2045 return (error);
2046 }
2047
2048 /*
2049 * The actual write RPC.
2050 */
2051 static int
nfsrpc_writerpc(vnode_t vp,struct uio * uiop,int * iomode,int * must_commit,struct ucred * cred,nfsv4stateid_t * stateidp,NFSPROC_T * p,struct nfsvattr * nap,int * attrflagp,int ioflag)2052 nfsrpc_writerpc(vnode_t vp, struct uio *uiop, int *iomode,
2053 int *must_commit, struct ucred *cred, nfsv4stateid_t *stateidp,
2054 NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, int ioflag)
2055 {
2056 u_int32_t *tl;
2057 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2058 struct nfsnode *np = VTONFS(vp);
2059 int error = 0, len, rlen, commit, committed = NFSWRITE_FILESYNC;
2060 int wccflag = 0;
2061 int32_t backup;
2062 struct nfsrv_descript *nd;
2063 nfsattrbit_t attrbits;
2064 uint64_t tmp_off;
2065 ssize_t tsiz, wsize;
2066 bool do_append;
2067
2068 KASSERT(uiop->uio_iovcnt == 1, ("nfs: writerpc iovcnt > 1"));
2069 *attrflagp = 0;
2070 tsiz = uiop->uio_resid;
2071 tmp_off = uiop->uio_offset + tsiz;
2072 NFSLOCKMNT(nmp);
2073 if (tmp_off > nmp->nm_maxfilesize || tmp_off < uiop->uio_offset) {
2074 NFSUNLOCKMNT(nmp);
2075 return (EFBIG);
2076 }
2077 wsize = nmp->nm_wsize;
2078 do_append = false;
2079 if ((ioflag & IO_APPEND) != 0 && NFSHASNFSV4(nmp) && !NFSHASPNFS(nmp))
2080 do_append = true;
2081 NFSUNLOCKMNT(nmp);
2082 nd = malloc(sizeof(*nd), M_TEMP, M_WAITOK);
2083 nd->nd_mrep = NULL; /* NFSv2 sometimes does a write with */
2084 nd->nd_repstat = 0; /* uio_resid == 0, so the while is not done */
2085 while (tsiz > 0) {
2086 *attrflagp = 0;
2087 len = (tsiz > wsize) ? wsize : tsiz;
2088 if (do_append)
2089 NFSCL_REQSTART(nd, NFSPROC_APPENDWRITE, vp, cred);
2090 else
2091 NFSCL_REQSTART(nd, NFSPROC_WRITE, vp, cred);
2092 if (nd->nd_flag & ND_NFSV4) {
2093 if (do_append) {
2094 NFSZERO_ATTRBIT(&attrbits);
2095 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_SIZE);
2096 nfsrv_putattrbit(nd, &attrbits);
2097 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_UNSIGNED +
2098 NFSX_HYPER);
2099 *tl++ = txdr_unsigned(NFSX_HYPER);
2100 txdr_hyper(uiop->uio_offset, tl); tl += 2;
2101 *tl = txdr_unsigned(NFSV4OP_WRITE);
2102 }
2103 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID);
2104 NFSM_BUILD(tl, u_int32_t *, NFSX_HYPER+2*NFSX_UNSIGNED);
2105 txdr_hyper(uiop->uio_offset, tl);
2106 tl += 2;
2107 *tl++ = txdr_unsigned(*iomode);
2108 *tl = txdr_unsigned(len);
2109 } else if (nd->nd_flag & ND_NFSV3) {
2110 NFSM_BUILD(tl, u_int32_t *, NFSX_HYPER+3*NFSX_UNSIGNED);
2111 txdr_hyper(uiop->uio_offset, tl);
2112 tl += 2;
2113 *tl++ = txdr_unsigned(len);
2114 *tl++ = txdr_unsigned(*iomode);
2115 *tl = txdr_unsigned(len);
2116 } else {
2117 u_int32_t x;
2118
2119 NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
2120 /*
2121 * Not sure why someone changed this, since the
2122 * RFC clearly states that "beginoffset" and
2123 * "totalcount" are ignored, but it wouldn't
2124 * surprise me if there's a busted server out there.
2125 */
2126 /* Set both "begin" and "current" to non-garbage. */
2127 x = txdr_unsigned((u_int32_t)uiop->uio_offset);
2128 *tl++ = x; /* "begin offset" */
2129 *tl++ = x; /* "current offset" */
2130 x = txdr_unsigned(len);
2131 *tl++ = x; /* total to this offset */
2132 *tl = x; /* size of this write */
2133 }
2134 error = nfsm_uiombuf(nd, uiop, len);
2135 if (error != 0) {
2136 m_freem(nd->nd_mreq);
2137 free(nd, M_TEMP);
2138 return (error);
2139 }
2140 /*
2141 * Although it is tempting to do a normal Getattr Op in the
2142 * NFSv4 compound, the result can be a nearly hung client
2143 * system if the Getattr asks for Owner and/or OwnerGroup.
2144 * It occurs when the client can't map either the Owner or
2145 * Owner_group name in the Getattr reply to a uid/gid. When
2146 * there is a cache miss, the kernel does an upcall to the
2147 * nfsuserd. Then, it can try and read the local /etc/passwd
2148 * or /etc/group file. It can then block in getnewbuf(),
2149 * waiting for dirty writes to be pushed to the NFS server.
2150 * The only reason this doesn't result in a complete
2151 * deadlock, is that the upcall times out and allows
2152 * the write to complete. However, progress is so slow
2153 * that it might just as well be deadlocked.
2154 * As such, we get the rest of the attributes, but not
2155 * Owner or Owner_group.
2156 * nb: nfscl_loadattrcache() needs to be told that these
2157 * partial attributes from a write rpc are being
2158 * passed in, via a argument flag.
2159 */
2160 if (nd->nd_flag & ND_NFSV4) {
2161 NFSWRITEGETATTR_ATTRBIT(&attrbits);
2162 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2163 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2164 (void) nfsrv_putattrbit(nd, &attrbits);
2165 }
2166 error = nfscl_request(nd, vp, p, cred);
2167 if (error) {
2168 free(nd, M_TEMP);
2169 return (error);
2170 }
2171 if (nd->nd_repstat) {
2172 /*
2173 * In case the rpc gets retried, roll
2174 * the uio fields changed by nfsm_uiombuf()
2175 * back.
2176 */
2177 uiop->uio_offset -= len;
2178 uiop->uio_resid += len;
2179 uiop->uio_iov->iov_base =
2180 (char *)uiop->uio_iov->iov_base - len;
2181 uiop->uio_iov->iov_len += len;
2182 }
2183 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) {
2184 error = nfscl_wcc_data(nd, vp, nap, attrflagp,
2185 &wccflag, &tmp_off);
2186 if (error)
2187 goto nfsmout;
2188 }
2189 if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) ==
2190 (ND_NFSV4 | ND_NOMOREDATA) &&
2191 nd->nd_repstat == NFSERR_NOTSAME && do_append) {
2192 /*
2193 * Verify of the file's size failed, so redo the
2194 * write using the file's size as returned in
2195 * the wcc attributes.
2196 */
2197 if (tmp_off + tsiz <= nmp->nm_maxfilesize) {
2198 do_append = false;
2199 uiop->uio_offset = tmp_off;
2200 m_freem(nd->nd_mrep);
2201 nd->nd_mrep = NULL;
2202 continue;
2203 } else
2204 nd->nd_repstat = EFBIG;
2205 }
2206 if (!nd->nd_repstat) {
2207 if (do_append) {
2208 /* Strip off the Write reply status. */
2209 do_append = false;
2210 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
2211 }
2212 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) {
2213 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED
2214 + NFSX_VERF);
2215 rlen = fxdr_unsigned(int, *tl++);
2216 if (rlen <= 0 || rlen > len) {
2217 error = NFSERR_IO;
2218 goto nfsmout;
2219 } else if (rlen < len) {
2220 backup = len - rlen;
2221 uiop->uio_iov->iov_base =
2222 (char *)uiop->uio_iov->iov_base -
2223 backup;
2224 uiop->uio_iov->iov_len += backup;
2225 uiop->uio_offset -= backup;
2226 uiop->uio_resid += backup;
2227 len = rlen;
2228 }
2229 commit = fxdr_unsigned(int, *tl++);
2230
2231 /*
2232 * Return the lowest commitment level
2233 * obtained by any of the RPCs.
2234 */
2235 if (committed == NFSWRITE_FILESYNC)
2236 committed = commit;
2237 else if (committed == NFSWRITE_DATASYNC &&
2238 commit == NFSWRITE_UNSTABLE)
2239 committed = commit;
2240 NFSLOCKMNT(nmp);
2241 if (!NFSHASWRITEVERF(nmp)) {
2242 NFSBCOPY((caddr_t)tl,
2243 (caddr_t)&nmp->nm_verf[0],
2244 NFSX_VERF);
2245 NFSSETWRITEVERF(nmp);
2246 } else if (NFSBCMP(tl, nmp->nm_verf,
2247 NFSX_VERF) && *must_commit != 2) {
2248 *must_commit = 1;
2249 NFSBCOPY(tl, nmp->nm_verf, NFSX_VERF);
2250 }
2251 NFSUNLOCKMNT(nmp);
2252 }
2253 if (nd->nd_flag & ND_NFSV4)
2254 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2255 if (nd->nd_flag & (ND_NFSV2 | ND_NFSV4)) {
2256 error = nfsm_loadattr(nd, nap);
2257 if (!error)
2258 *attrflagp = NFS_LATTR_NOSHRINK;
2259 }
2260 } else {
2261 error = nd->nd_repstat;
2262 }
2263 if (error)
2264 goto nfsmout;
2265 NFSWRITERPC_SETTIME(wccflag, np, nap, (nd->nd_flag & ND_NFSV4));
2266 m_freem(nd->nd_mrep);
2267 nd->nd_mrep = NULL;
2268 tsiz -= len;
2269 }
2270 nfsmout:
2271 if (nd->nd_mrep != NULL)
2272 m_freem(nd->nd_mrep);
2273 *iomode = committed;
2274 if (nd->nd_repstat && !error)
2275 error = nd->nd_repstat;
2276 free(nd, M_TEMP);
2277 return (error);
2278 }
2279
2280 /*
2281 * Do an nfs deallocate operation.
2282 */
2283 int
nfsrpc_deallocate(vnode_t vp,off_t offs,off_t len,struct nfsvattr * nap,int * attrflagp,struct ucred * cred,NFSPROC_T * p)2284 nfsrpc_deallocate(vnode_t vp, off_t offs, off_t len, struct nfsvattr *nap,
2285 int *attrflagp, struct ucred *cred, NFSPROC_T *p)
2286 {
2287 int error, expireret = 0, openerr, retrycnt;
2288 uint32_t clidrev = 0;
2289 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2290 struct nfsfh *nfhp;
2291 nfsv4stateid_t stateid;
2292 void *lckp;
2293
2294 if (nmp->nm_clp != NULL)
2295 clidrev = nmp->nm_clp->nfsc_clientidrev;
2296 retrycnt = 0;
2297 do {
2298 lckp = NULL;
2299 openerr = 1;
2300 nfhp = VTONFS(vp)->n_fhp;
2301 error = nfscl_getstateid(vp, nfhp->nfh_fh, nfhp->nfh_len,
2302 NFSV4OPEN_ACCESSWRITE, 0, cred, p, &stateid, &lckp);
2303 if (error != 0) {
2304 /*
2305 * No Open stateid, so try and open the file
2306 * now.
2307 */
2308 openerr = nfsrpc_open(vp, FWRITE, cred, p);
2309 if (openerr == 0)
2310 nfscl_getstateid(vp, nfhp->nfh_fh,
2311 nfhp->nfh_len, NFSV4OPEN_ACCESSWRITE, 0,
2312 cred, p, &stateid, &lckp);
2313 }
2314 error = nfsrpc_deallocaterpc(vp, offs, len, &stateid, nap,
2315 attrflagp, cred, p);
2316 if (error == NFSERR_STALESTATEID)
2317 nfscl_initiate_recovery(nmp->nm_clp);
2318 if (lckp != NULL)
2319 nfscl_lockderef(lckp);
2320 if (openerr == 0)
2321 nfsrpc_close(vp, 0, p);
2322 if (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
2323 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
2324 error == NFSERR_OLDSTATEID || error == NFSERR_BADSESSION) {
2325 (void) nfs_catnap(PZERO, error, "nfs_deallocate");
2326 } else if ((error == NFSERR_EXPIRED || (!NFSHASINT(nmp) &&
2327 error == NFSERR_BADSTATEID)) && clidrev != 0) {
2328 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
2329 } else if (error == NFSERR_BADSTATEID && NFSHASINT(nmp)) {
2330 error = EIO;
2331 }
2332 retrycnt++;
2333 } while (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
2334 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
2335 error == NFSERR_BADSESSION ||
2336 (error == NFSERR_OLDSTATEID && retrycnt < 20) ||
2337 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
2338 expireret == 0 && clidrev != 0 && retrycnt < 4));
2339 if (error && retrycnt >= 4)
2340 error = EIO;
2341 return (error);
2342 }
2343
2344 /*
2345 * The actual deallocate RPC.
2346 */
2347 static int
nfsrpc_deallocaterpc(vnode_t vp,off_t offs,off_t len,nfsv4stateid_t * stateidp,struct nfsvattr * nap,int * attrflagp,struct ucred * cred,NFSPROC_T * p)2348 nfsrpc_deallocaterpc(vnode_t vp, off_t offs, off_t len,
2349 nfsv4stateid_t *stateidp, struct nfsvattr *nap, int *attrflagp,
2350 struct ucred *cred, NFSPROC_T *p)
2351 {
2352 uint32_t *tl;
2353 struct nfsnode *np = VTONFS(vp);
2354 int error, wccflag;
2355 struct nfsrv_descript nfsd;
2356 struct nfsrv_descript *nd = &nfsd;
2357 nfsattrbit_t attrbits;
2358
2359 *attrflagp = 0;
2360 NFSCL_REQSTART(nd, NFSPROC_DEALLOCATE, vp, cred);
2361 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID);
2362 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_HYPER);
2363 txdr_hyper(offs, tl);
2364 tl += 2;
2365 txdr_hyper(len, tl);
2366 NFSWRITEGETATTR_ATTRBIT(&attrbits);
2367 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
2368 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2369 nfsrv_putattrbit(nd, &attrbits);
2370 error = nfscl_request(nd, vp, p, cred);
2371 if (error != 0)
2372 return (error);
2373 wccflag = 0;
2374 error = nfscl_wcc_data(nd, vp, nap, attrflagp, &wccflag, NULL);
2375 if (error != 0)
2376 goto nfsmout;
2377 if (nd->nd_repstat == 0) {
2378 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
2379 error = nfsm_loadattr(nd, nap);
2380 if (error != 0)
2381 goto nfsmout;
2382 *attrflagp = NFS_LATTR_NOSHRINK;
2383 }
2384 NFSWRITERPC_SETTIME(wccflag, np, nap, 1);
2385 nfsmout:
2386 m_freem(nd->nd_mrep);
2387 if (nd->nd_repstat != 0 && error == 0)
2388 error = nd->nd_repstat;
2389 return (error);
2390 }
2391
2392 /*
2393 * nfs mknod rpc
2394 * For NFS v2 this is a kludge. Use a create rpc but with the IFMT bits of the
2395 * mode set to specify the file type and the size field for rdev.
2396 */
2397 int
nfsrpc_mknod(vnode_t dvp,char * name,int namelen,struct vattr * vap,u_int32_t rdev,__enum_uint8 (vtype)vtyp,struct ucred * cred,NFSPROC_T * p,struct nfsvattr * dnap,struct nfsvattr * nnap,struct nfsfh ** nfhpp,int * attrflagp,int * dattrflagp)2398 nfsrpc_mknod(vnode_t dvp, char *name, int namelen, struct vattr *vap,
2399 u_int32_t rdev, __enum_uint8(vtype) vtyp, struct ucred *cred, NFSPROC_T *p,
2400 struct nfsvattr *dnap, struct nfsvattr *nnap, struct nfsfh **nfhpp,
2401 int *attrflagp, int *dattrflagp)
2402 {
2403 u_int32_t *tl;
2404 int error = 0;
2405 struct nfsrv_descript nfsd, *nd = &nfsd;
2406 nfsattrbit_t attrbits;
2407
2408 *nfhpp = NULL;
2409 *attrflagp = 0;
2410 *dattrflagp = 0;
2411 if (namelen > NFS_MAXNAMLEN)
2412 return (ENAMETOOLONG);
2413 NFSCL_REQSTART(nd, NFSPROC_MKNOD, dvp, cred);
2414 if (nd->nd_flag & ND_NFSV4) {
2415 if (vtyp == VBLK || vtyp == VCHR) {
2416 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2417 *tl++ = vtonfsv34_type(vtyp);
2418 *tl++ = txdr_unsigned(NFSMAJOR(rdev));
2419 *tl = txdr_unsigned(NFSMINOR(rdev));
2420 } else {
2421 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2422 *tl = vtonfsv34_type(vtyp);
2423 }
2424 }
2425 (void) nfsm_strtom(nd, name, namelen);
2426 if (nd->nd_flag & ND_NFSV3) {
2427 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2428 *tl = vtonfsv34_type(vtyp);
2429 }
2430 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4))
2431 nfscl_fillsattr(nd, vap, dvp, NFSSATTR_NEWFILE, 0);
2432 if ((nd->nd_flag & ND_NFSV3) &&
2433 (vtyp == VCHR || vtyp == VBLK)) {
2434 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2435 *tl++ = txdr_unsigned(NFSMAJOR(rdev));
2436 *tl = txdr_unsigned(NFSMINOR(rdev));
2437 }
2438 if (nd->nd_flag & ND_NFSV4) {
2439 NFSGETATTR_ATTRBIT(&attrbits);
2440 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2441 *tl++ = txdr_unsigned(NFSV4OP_GETFH);
2442 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2443 (void) nfsrv_putattrbit(nd, &attrbits);
2444 }
2445 if (nd->nd_flag & ND_NFSV2)
2446 nfscl_fillsattr(nd, vap, dvp, NFSSATTR_SIZERDEV, rdev);
2447 error = nfscl_request(nd, dvp, p, cred);
2448 if (error)
2449 return (error);
2450 if (nd->nd_flag & ND_NFSV4)
2451 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, NULL);
2452 if (!nd->nd_repstat) {
2453 if (nd->nd_flag & ND_NFSV4) {
2454 NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
2455 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
2456 if (error)
2457 goto nfsmout;
2458 }
2459 error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp);
2460 if (error)
2461 goto nfsmout;
2462 }
2463 if (nd->nd_flag & ND_NFSV3)
2464 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, NULL);
2465 if (!error && nd->nd_repstat)
2466 error = nd->nd_repstat;
2467 nfsmout:
2468 m_freem(nd->nd_mrep);
2469 return (error);
2470 }
2471
2472 /*
2473 * nfs file create call
2474 * Mostly just call the approriate routine. (I separated out v4, so that
2475 * error recovery wouldn't be as difficult.)
2476 */
2477 int
nfsrpc_create(vnode_t dvp,char * name,int namelen,struct vattr * vap,nfsquad_t cverf,int fmode,struct ucred * cred,NFSPROC_T * p,struct nfsvattr * dnap,struct nfsvattr * nnap,struct nfsfh ** nfhpp,int * attrflagp,int * dattrflagp)2478 nfsrpc_create(vnode_t dvp, char *name, int namelen, struct vattr *vap,
2479 nfsquad_t cverf, int fmode, struct ucred *cred, NFSPROC_T *p,
2480 struct nfsvattr *dnap, struct nfsvattr *nnap, struct nfsfh **nfhpp,
2481 int *attrflagp, int *dattrflagp)
2482 {
2483 int error = 0, newone, expireret = 0, retrycnt, unlocked;
2484 struct nfsclowner *owp;
2485 struct nfscldeleg *dp;
2486 struct nfsmount *nmp = VFSTONFS(dvp->v_mount);
2487 u_int32_t clidrev;
2488
2489 if (NFSHASNFSV4(nmp)) {
2490 retrycnt = 0;
2491 do {
2492 dp = NULL;
2493 error = nfscl_open(dvp, NULL, 0, (NFSV4OPEN_ACCESSWRITE |
2494 NFSV4OPEN_ACCESSREAD), 0, cred, p, &owp, NULL, &newone,
2495 NULL, 1, true);
2496 if (error)
2497 return (error);
2498 if (nmp->nm_clp != NULL)
2499 clidrev = nmp->nm_clp->nfsc_clientidrev;
2500 else
2501 clidrev = 0;
2502 if (!NFSHASPNFS(nmp) || nfscl_enablecallb == 0 ||
2503 nfs_numnfscbd == 0 || retrycnt > 0)
2504 error = nfsrpc_createv4(dvp, name, namelen, vap, cverf,
2505 fmode, owp, &dp, cred, p, dnap, nnap, nfhpp,
2506 attrflagp, dattrflagp, &unlocked);
2507 else
2508 error = nfsrpc_getcreatelayout(dvp, name, namelen, vap,
2509 cverf, fmode, owp, &dp, cred, p, dnap, nnap, nfhpp,
2510 attrflagp, dattrflagp, &unlocked);
2511 /*
2512 * There is no need to invalidate cached attributes here,
2513 * since new post-delegation issue attributes are always
2514 * returned by nfsrpc_createv4() and these will update the
2515 * attribute cache.
2516 */
2517 if (dp != NULL)
2518 (void) nfscl_deleg(nmp->nm_mountp, owp->nfsow_clp,
2519 (*nfhpp)->nfh_fh, (*nfhpp)->nfh_len, cred, p, dp);
2520 nfscl_ownerrelease(nmp, owp, error, newone, unlocked);
2521 if (error == NFSERR_GRACE || error == NFSERR_STALECLIENTID ||
2522 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
2523 error == NFSERR_BADSESSION) {
2524 (void) nfs_catnap(PZERO, error, "nfs_open");
2525 } else if ((error == NFSERR_EXPIRED ||
2526 error == NFSERR_BADSTATEID) && clidrev != 0) {
2527 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
2528 retrycnt++;
2529 }
2530 } while (error == NFSERR_GRACE || error == NFSERR_STALECLIENTID ||
2531 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
2532 error == NFSERR_BADSESSION ||
2533 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
2534 expireret == 0 && clidrev != 0 && retrycnt < 4));
2535 if (error && retrycnt >= 4)
2536 error = EIO;
2537 } else {
2538 error = nfsrpc_createv23(dvp, name, namelen, vap, cverf,
2539 fmode, cred, p, dnap, nnap, nfhpp, attrflagp, dattrflagp);
2540 }
2541 return (error);
2542 }
2543
2544 /*
2545 * The create rpc for v2 and 3.
2546 */
2547 static int
nfsrpc_createv23(vnode_t dvp,char * name,int namelen,struct vattr * vap,nfsquad_t cverf,int fmode,struct ucred * cred,NFSPROC_T * p,struct nfsvattr * dnap,struct nfsvattr * nnap,struct nfsfh ** nfhpp,int * attrflagp,int * dattrflagp)2548 nfsrpc_createv23(vnode_t dvp, char *name, int namelen, struct vattr *vap,
2549 nfsquad_t cverf, int fmode, struct ucred *cred, NFSPROC_T *p,
2550 struct nfsvattr *dnap, struct nfsvattr *nnap, struct nfsfh **nfhpp,
2551 int *attrflagp, int *dattrflagp)
2552 {
2553 u_int32_t *tl;
2554 int error = 0;
2555 struct nfsrv_descript nfsd, *nd = &nfsd;
2556
2557 *nfhpp = NULL;
2558 *attrflagp = 0;
2559 *dattrflagp = 0;
2560 if (namelen > NFS_MAXNAMLEN)
2561 return (ENAMETOOLONG);
2562 NFSCL_REQSTART(nd, NFSPROC_CREATE, dvp, cred);
2563 (void) nfsm_strtom(nd, name, namelen);
2564 if (nd->nd_flag & ND_NFSV3) {
2565 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2566 if (fmode & O_EXCL) {
2567 *tl = txdr_unsigned(NFSCREATE_EXCLUSIVE);
2568 NFSM_BUILD(tl, u_int32_t *, NFSX_VERF);
2569 *tl++ = cverf.lval[0];
2570 *tl = cverf.lval[1];
2571 } else {
2572 *tl = txdr_unsigned(NFSCREATE_UNCHECKED);
2573 nfscl_fillsattr(nd, vap, dvp, 0, 0);
2574 }
2575 } else {
2576 nfscl_fillsattr(nd, vap, dvp, NFSSATTR_SIZE0, 0);
2577 }
2578 error = nfscl_request(nd, dvp, p, cred);
2579 if (error)
2580 return (error);
2581 if (nd->nd_repstat == 0) {
2582 error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp);
2583 if (error)
2584 goto nfsmout;
2585 }
2586 if (nd->nd_flag & ND_NFSV3)
2587 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, NULL);
2588 if (nd->nd_repstat != 0 && error == 0)
2589 error = nd->nd_repstat;
2590 nfsmout:
2591 m_freem(nd->nd_mrep);
2592 return (error);
2593 }
2594
2595 static int
nfsrpc_createv4(vnode_t dvp,char * name,int namelen,struct vattr * vap,nfsquad_t cverf,int fmode,struct nfsclowner * owp,struct nfscldeleg ** dpp,struct ucred * cred,NFSPROC_T * p,struct nfsvattr * dnap,struct nfsvattr * nnap,struct nfsfh ** nfhpp,int * attrflagp,int * dattrflagp,int * unlockedp)2596 nfsrpc_createv4(vnode_t dvp, char *name, int namelen, struct vattr *vap,
2597 nfsquad_t cverf, int fmode, struct nfsclowner *owp, struct nfscldeleg **dpp,
2598 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap,
2599 struct nfsvattr *nnap, struct nfsfh **nfhpp, int *attrflagp,
2600 int *dattrflagp, int *unlockedp)
2601 {
2602 u_int32_t *tl;
2603 int error = 0, deleg, newone, ret, acesize, limitby;
2604 struct nfsrv_descript nfsd, *nd = &nfsd;
2605 struct nfsclopen *op;
2606 struct nfscldeleg *dp = NULL;
2607 struct nfsnode *np;
2608 struct nfsfh *nfhp;
2609 nfsattrbit_t attrbits;
2610 nfsv4stateid_t stateid;
2611 u_int32_t rflags;
2612 struct nfsmount *nmp;
2613 struct nfsclsession *tsep;
2614
2615 nmp = VFSTONFS(dvp->v_mount);
2616 np = VTONFS(dvp);
2617 *unlockedp = 0;
2618 *nfhpp = NULL;
2619 *dpp = NULL;
2620 *attrflagp = 0;
2621 *dattrflagp = 0;
2622 if (namelen > NFS_MAXNAMLEN)
2623 return (ENAMETOOLONG);
2624 NFSCL_REQSTART(nd, NFSPROC_CREATE, dvp, cred);
2625 /*
2626 * For V4, this is actually an Open op.
2627 */
2628 NFSM_BUILD(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
2629 *tl++ = txdr_unsigned(owp->nfsow_seqid);
2630 if (NFSHASNFSV4N(nmp)) {
2631 if (!NFSHASPNFS(nmp) && nfscl_enablecallb != 0 &&
2632 nfs_numnfscbd > 0)
2633 *tl++ = txdr_unsigned(NFSV4OPEN_ACCESSWRITE |
2634 NFSV4OPEN_ACCESSREAD | NFSV4OPEN_WANTWRITEDELEG);
2635 else
2636 *tl++ = txdr_unsigned(NFSV4OPEN_ACCESSWRITE |
2637 NFSV4OPEN_ACCESSREAD | NFSV4OPEN_WANTNODELEG);
2638 } else
2639 *tl++ = txdr_unsigned(NFSV4OPEN_ACCESSWRITE |
2640 NFSV4OPEN_ACCESSREAD);
2641 *tl++ = txdr_unsigned(NFSV4OPEN_DENYNONE);
2642 tsep = nfsmnt_mdssession(nmp);
2643 *tl++ = tsep->nfsess_clientid.lval[0];
2644 *tl = tsep->nfsess_clientid.lval[1];
2645 (void) nfsm_strtom(nd, owp->nfsow_owner, NFSV4CL_LOCKNAMELEN);
2646 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2647 *tl++ = txdr_unsigned(NFSV4OPEN_CREATE);
2648 if (fmode & O_EXCL) {
2649 if (NFSHASNFSV4N(nmp)) {
2650 if (NFSHASSESSPERSIST(nmp)) {
2651 /* Use GUARDED for persistent sessions. */
2652 *tl = txdr_unsigned(NFSCREATE_GUARDED);
2653 nfscl_fillsattr(nd, vap, dvp, NFSSATTR_NEWFILE,
2654 0);
2655 } else {
2656 /* Otherwise, use EXCLUSIVE4_1. */
2657 *tl = txdr_unsigned(NFSCREATE_EXCLUSIVE41);
2658 NFSM_BUILD(tl, u_int32_t *, NFSX_VERF);
2659 *tl++ = cverf.lval[0];
2660 *tl = cverf.lval[1];
2661 nfscl_fillsattr(nd, vap, dvp, NFSSATTR_NEWFILE,
2662 0);
2663 }
2664 } else {
2665 /* NFSv4.0 */
2666 *tl = txdr_unsigned(NFSCREATE_EXCLUSIVE);
2667 NFSM_BUILD(tl, u_int32_t *, NFSX_VERF);
2668 *tl++ = cverf.lval[0];
2669 *tl = cverf.lval[1];
2670 }
2671 } else {
2672 *tl = txdr_unsigned(NFSCREATE_UNCHECKED);
2673 nfscl_fillsattr(nd, vap, dvp, NFSSATTR_NEWFILE, 0);
2674 }
2675 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2676 *tl = txdr_unsigned(NFSV4OPEN_CLAIMNULL);
2677 (void) nfsm_strtom(nd, name, namelen);
2678 /* Get the new file's handle and attributes. */
2679 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2680 *tl++ = txdr_unsigned(NFSV4OP_GETFH);
2681 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2682 NFSGETATTR_ATTRBIT(&attrbits);
2683 (void) nfsrv_putattrbit(nd, &attrbits);
2684 /* Get the directory's post-op attributes. */
2685 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2686 *tl = txdr_unsigned(NFSV4OP_PUTFH);
2687 (void)nfsm_fhtom(nmp, nd, np->n_fhp->nfh_fh, np->n_fhp->nfh_len, 0);
2688 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2689 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2690 (void) nfsrv_putattrbit(nd, &attrbits);
2691 error = nfscl_request(nd, dvp, p, cred);
2692 if (error)
2693 return (error);
2694 NFSCL_INCRSEQID(owp->nfsow_seqid, nd);
2695 if (nd->nd_repstat == 0) {
2696 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
2697 6 * NFSX_UNSIGNED);
2698 stateid.seqid = *tl++;
2699 stateid.other[0] = *tl++;
2700 stateid.other[1] = *tl++;
2701 stateid.other[2] = *tl;
2702 rflags = fxdr_unsigned(u_int32_t, *(tl + 6));
2703 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
2704 if (error)
2705 goto nfsmout;
2706 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2707 deleg = fxdr_unsigned(int, *tl);
2708 if (deleg == NFSV4OPEN_DELEGATEREAD ||
2709 deleg == NFSV4OPEN_DELEGATEWRITE) {
2710 if (!(owp->nfsow_clp->nfsc_flags &
2711 NFSCLFLAGS_FIRSTDELEG))
2712 owp->nfsow_clp->nfsc_flags |=
2713 (NFSCLFLAGS_FIRSTDELEG | NFSCLFLAGS_GOTDELEG);
2714 dp = malloc(
2715 sizeof (struct nfscldeleg) + NFSX_V4FHMAX,
2716 M_NFSCLDELEG, M_WAITOK);
2717 LIST_INIT(&dp->nfsdl_owner);
2718 LIST_INIT(&dp->nfsdl_lock);
2719 dp->nfsdl_clp = owp->nfsow_clp;
2720 newnfs_copyincred(cred, &dp->nfsdl_cred);
2721 nfscl_lockinit(&dp->nfsdl_rwlock);
2722 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
2723 NFSX_UNSIGNED);
2724 dp->nfsdl_stateid.seqid = *tl++;
2725 dp->nfsdl_stateid.other[0] = *tl++;
2726 dp->nfsdl_stateid.other[1] = *tl++;
2727 dp->nfsdl_stateid.other[2] = *tl++;
2728 ret = fxdr_unsigned(int, *tl);
2729 if (deleg == NFSV4OPEN_DELEGATEWRITE) {
2730 dp->nfsdl_flags = NFSCLDL_WRITE;
2731 /*
2732 * Indicates how much the file can grow.
2733 */
2734 NFSM_DISSECT(tl, u_int32_t *,
2735 3 * NFSX_UNSIGNED);
2736 limitby = fxdr_unsigned(int, *tl++);
2737 switch (limitby) {
2738 case NFSV4OPEN_LIMITSIZE:
2739 dp->nfsdl_sizelimit = fxdr_hyper(tl);
2740 break;
2741 case NFSV4OPEN_LIMITBLOCKS:
2742 dp->nfsdl_sizelimit =
2743 fxdr_unsigned(u_int64_t, *tl++);
2744 dp->nfsdl_sizelimit *=
2745 fxdr_unsigned(u_int64_t, *tl);
2746 break;
2747 default:
2748 error = NFSERR_BADXDR;
2749 goto nfsmout;
2750 }
2751 } else {
2752 dp->nfsdl_flags = NFSCLDL_READ;
2753 }
2754 if (ret)
2755 dp->nfsdl_flags |= NFSCLDL_RECALL;
2756 error = nfsrv_dissectace(nd, &dp->nfsdl_ace, false,
2757 &ret, &acesize, p);
2758 if (error)
2759 goto nfsmout;
2760 } else if (deleg == NFSV4OPEN_DELEGATENONEEXT &&
2761 NFSHASNFSV4N(nmp)) {
2762 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
2763 deleg = fxdr_unsigned(uint32_t, *tl);
2764 if (deleg == NFSV4OPEN_CONTENTION ||
2765 deleg == NFSV4OPEN_RESOURCE)
2766 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
2767 } else if (deleg != NFSV4OPEN_DELEGATENONE) {
2768 error = NFSERR_BADXDR;
2769 goto nfsmout;
2770 }
2771 error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp);
2772 if (error)
2773 goto nfsmout;
2774 /* Get rid of the PutFH and Getattr status values. */
2775 NFSM_DISSECT(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
2776 /* Load the directory attributes. */
2777 error = nfsm_loadattr(nd, dnap);
2778 if (error)
2779 goto nfsmout;
2780 *dattrflagp = 1;
2781 if (dp != NULL && *attrflagp) {
2782 dp->nfsdl_change = nnap->na_filerev;
2783 dp->nfsdl_modtime = nnap->na_mtime;
2784 dp->nfsdl_flags |= NFSCLDL_MODTIMESET;
2785 }
2786 /*
2787 * We can now complete the Open state.
2788 */
2789 nfhp = *nfhpp;
2790 if (dp != NULL) {
2791 dp->nfsdl_fhlen = nfhp->nfh_len;
2792 NFSBCOPY(nfhp->nfh_fh, dp->nfsdl_fh, nfhp->nfh_len);
2793 }
2794 /*
2795 * Get an Open structure that will be
2796 * attached to the OpenOwner, acquired already.
2797 */
2798 error = nfscl_open(dvp, nfhp->nfh_fh, nfhp->nfh_len,
2799 (NFSV4OPEN_ACCESSWRITE | NFSV4OPEN_ACCESSREAD), 0,
2800 cred, p, NULL, &op, &newone, NULL, 0, false);
2801 if (error)
2802 goto nfsmout;
2803 op->nfso_stateid = stateid;
2804 newnfs_copyincred(cred, &op->nfso_cred);
2805 if ((rflags & NFSV4OPEN_RESULTCONFIRM)) {
2806 do {
2807 ret = nfsrpc_openconfirm(dvp, nfhp->nfh_fh,
2808 nfhp->nfh_len, op, cred, p);
2809 if (ret == NFSERR_DELAY)
2810 (void) nfs_catnap(PZERO, ret, "nfs_create");
2811 } while (ret == NFSERR_DELAY);
2812 error = ret;
2813 }
2814
2815 /*
2816 * If the server is handing out delegations, but we didn't
2817 * get one because an OpenConfirm was required, try the
2818 * Open again, to get a delegation. This is a harmless no-op,
2819 * from a server's point of view.
2820 */
2821 if ((rflags & NFSV4OPEN_RESULTCONFIRM) &&
2822 (owp->nfsow_clp->nfsc_flags & NFSCLFLAGS_GOTDELEG) &&
2823 !error && dp == NULL) {
2824 KASSERT(!NFSHASNFSV4N(nmp),
2825 ("nfsrpc_createv4: result confirm"));
2826 do {
2827 ret = nfsrpc_openrpc(VFSTONFS(dvp->v_mount), dvp,
2828 np->n_fhp->nfh_fh, np->n_fhp->nfh_len,
2829 nfhp->nfh_fh, nfhp->nfh_len,
2830 (NFSV4OPEN_ACCESSWRITE | NFSV4OPEN_ACCESSREAD), op,
2831 name, namelen, &dp, 0, 0x0, cred, p, 0, 1);
2832 if (ret == NFSERR_DELAY)
2833 (void) nfs_catnap(PZERO, ret, "nfs_crt2");
2834 } while (ret == NFSERR_DELAY);
2835 if (ret) {
2836 if (dp != NULL) {
2837 free(dp, M_NFSCLDELEG);
2838 dp = NULL;
2839 }
2840 if (ret == NFSERR_STALECLIENTID ||
2841 ret == NFSERR_STALEDONTRECOVER ||
2842 ret == NFSERR_BADSESSION)
2843 error = ret;
2844 }
2845 }
2846 nfscl_openrelease(nmp, op, error, newone);
2847 *unlockedp = 1;
2848 }
2849 if (nd->nd_repstat != 0 && error == 0)
2850 error = nd->nd_repstat;
2851 if (error == NFSERR_STALECLIENTID)
2852 nfscl_initiate_recovery(owp->nfsow_clp);
2853 nfsmout:
2854 if (!error)
2855 *dpp = dp;
2856 else if (dp != NULL)
2857 free(dp, M_NFSCLDELEG);
2858 m_freem(nd->nd_mrep);
2859 return (error);
2860 }
2861
2862 /*
2863 * Nfs remove rpc
2864 */
2865 int
nfsrpc_remove(struct vnode * dvp,char * name,int namelen,struct vnode * vp,struct nfsvattr * nap,int * attrflagp,nfsremove_status * file_status,struct nfsvattr * dnap,int * dattrflagp,struct ucred * cred,NFSPROC_T * p)2866 nfsrpc_remove(struct vnode *dvp, char *name, int namelen, struct vnode *vp,
2867 struct nfsvattr *nap, int *attrflagp, nfsremove_status *file_status,
2868 struct nfsvattr *dnap, int *dattrflagp, struct ucred *cred, NFSPROC_T *p)
2869 {
2870 uint32_t *tl;
2871 struct nfsrv_descript nfsd, *nd = &nfsd;
2872 struct nfsnode *np;
2873 struct nfsmount *nmp;
2874 nfsv4stateid_t dstateid;
2875 nfsattrbit_t attrbits;
2876 int error, i, ret;
2877
2878 *dattrflagp = 0;
2879 *attrflagp = 0;
2880 *file_status = UNKNOWN;
2881 ret = 0;
2882 if (namelen > NFS_MAXNAMLEN)
2883 return (ENAMETOOLONG);
2884 nmp = VFSTONFS(dvp->v_mount);
2885 tryagain:
2886 if (NFSHASNFSV4(nmp) && ((nmp->nm_flag & NFSMNT_NOCTO) == 0 ||
2887 !NFSHASNFSV4N(nmp)) && ret == 0) {
2888 ret = nfscl_removedeleg(vp, p, &dstateid);
2889 if (ret == 1) {
2890 NFSCL_REQSTART(nd, NFSPROC_RETDELEGREMOVE, vp, cred);
2891 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID +
2892 NFSX_UNSIGNED);
2893 if (NFSHASNFSV4N(nmp))
2894 *tl++ = 0;
2895 else
2896 *tl++ = dstateid.seqid;
2897 *tl++ = dstateid.other[0];
2898 *tl++ = dstateid.other[1];
2899 *tl++ = dstateid.other[2];
2900 *tl = txdr_unsigned(NFSV4OP_PUTFH);
2901 np = VTONFS(dvp);
2902 (void)nfsm_fhtom(nmp, nd, np->n_fhp->nfh_fh,
2903 np->n_fhp->nfh_len, 0);
2904 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2905 *tl = txdr_unsigned(NFSV4OP_REMOVE);
2906 }
2907 } else {
2908 ret = 0;
2909 }
2910 if (ret == 0)
2911 NFSCL_REQSTART(nd, NFSPROC_REMOVE, dvp, cred);
2912 (void)nfsm_strtom(nd, name, namelen);
2913 if (ret == 0 && (nd->nd_flag & ND_NFSV4) != 0) {
2914 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
2915 *tl = txdr_unsigned(NFSV4OP_PUTFH);
2916 np = VTONFS(vp);
2917 (void)nfsm_fhtom(nmp, nd, np->n_fhp->nfh_fh, np->n_fhp->nfh_len, 0);
2918 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
2919 NFSGETATTR_ATTRBIT(&attrbits);
2920 *tl = txdr_unsigned(NFSV4OP_GETATTR);
2921 (void)nfsrv_putattrbit(nd, &attrbits);
2922 }
2923 error = nfscl_request(nd, dvp, p, cred);
2924 if (error != 0)
2925 return (error);
2926 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) {
2927 /* For NFSv4, parse out any Delereturn replies. */
2928 if (ret > 0 && nd->nd_repstat != 0 &&
2929 (nd->nd_flag & ND_NOMOREDATA)) {
2930 /*
2931 * If the Delegreturn failed, try again without
2932 * it. The server will Recall, as required.
2933 */
2934 m_freem(nd->nd_mrep);
2935 goto tryagain;
2936 }
2937 for (i = 0; i < (ret * 2); i++) {
2938 if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) ==
2939 ND_NFSV4) {
2940 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2941 if (*(tl + 1))
2942 nd->nd_flag |= ND_NOMOREDATA;
2943 }
2944 }
2945 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, NULL);
2946 }
2947 if (ret == 0 && (nd->nd_flag & (ND_NFSV4 |
2948 ND_NOMOREDATA)) == ND_NFSV4) {
2949 /* Parse out the Remove reply for NFSPROC_REMOVE. */
2950 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED + 2 * NFSX_HYPER);
2951 /* No use for change info for now. */
2952 /* The Remove succeeded. */
2953 nd->nd_repstat = 0;
2954 }
2955 if (ret == 0 && (nd->nd_flag & (ND_NFSV4 |
2956 ND_NOMOREDATA)) == ND_NFSV4) {
2957 /* Parse out the PutFH, Getattr for NFSPROC_REMOVE. */
2958 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
2959 if (*(tl + 1) != 0) {
2960 i = fxdr_unsigned(int, *(tl + 1));
2961 if (i == NFSERR_STALE)
2962 *file_status = DELETED;
2963 } else {
2964 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
2965 if (*(tl + 1) != 0) {
2966 i = fxdr_unsigned(int, *(tl + 1));
2967 if (i == NFSERR_STALE)
2968 *file_status = DELETED;
2969 } else {
2970 error = nfsm_loadattr(nd, nap);
2971 if (error == 0) {
2972 *attrflagp = 1;
2973 if (nap->na_nlink == 0)
2974 *file_status = NLINK_ZERO;
2975 else
2976 *file_status = VALID;
2977 }
2978 }
2979 }
2980 }
2981 if (nd->nd_repstat != 0 && error == 0)
2982 error = nd->nd_repstat;
2983 nfsmout:
2984 m_freem(nd->nd_mrep);
2985 return (error);
2986 }
2987
2988 /*
2989 * Do an nfs rename rpc.
2990 */
2991 int
nfsrpc_rename(struct vnode * fdvp,struct vnode * fvp,char * fnameptr,int fnamelen,struct vnode * tdvp,struct vnode * tvp,char * tnameptr,int tnamelen,nfsremove_status * tvp_status,struct nfsvattr * fnap,struct nfsvattr * tnap,int * fattrflagp,int * tattrflagp,struct nfsvattr * tvpnap,int * tvpattrflagp,struct ucred * cred,NFSPROC_T * p)2992 nfsrpc_rename(struct vnode *fdvp, struct vnode *fvp, char *fnameptr,
2993 int fnamelen, struct vnode *tdvp, struct vnode *tvp, char *tnameptr,
2994 int tnamelen, nfsremove_status *tvp_status, struct nfsvattr *fnap,
2995 struct nfsvattr *tnap, int *fattrflagp, int *tattrflagp,
2996 struct nfsvattr *tvpnap, int *tvpattrflagp, struct ucred *cred,
2997 NFSPROC_T *p)
2998 {
2999 uint32_t *tl;
3000 struct nfsrv_descript nfsd, *nd = &nfsd;
3001 struct nfsmount *nmp;
3002 struct nfsnode *np;
3003 nfsattrbit_t attrbits;
3004 nfsv4stateid_t fdstateid, tdstateid;
3005 int error = 0, ret = 0, gottd = 0, gotfd = 0, i;
3006
3007 *fattrflagp = 0;
3008 *tattrflagp = 0;
3009 *tvpattrflagp = 0;
3010 *tvp_status = UNKNOWN;
3011 nmp = VFSTONFS(fdvp->v_mount);
3012 if (fnamelen > NFS_MAXNAMLEN || tnamelen > NFS_MAXNAMLEN)
3013 return (ENAMETOOLONG);
3014 tryagain:
3015 if (NFSHASNFSV4(nmp) && ((nmp->nm_flag & NFSMNT_NOCTO) == 0 ||
3016 !NFSHASNFSV4N(nmp)) && ret == 0) {
3017 ret = nfscl_renamedeleg(fvp, &fdstateid, &gotfd, tvp,
3018 &tdstateid, &gottd, p);
3019 if (gotfd && gottd) {
3020 NFSCL_REQSTART(nd, NFSPROC_RETDELEGRENAME2, fvp, cred);
3021 } else if (gotfd) {
3022 NFSCL_REQSTART(nd, NFSPROC_RETDELEGRENAME1, fvp, cred);
3023 } else if (gottd) {
3024 NFSCL_REQSTART(nd, NFSPROC_RETDELEGRENAME1, tvp, cred);
3025 }
3026 if (gotfd) {
3027 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID);
3028 if (NFSHASNFSV4N(nmp))
3029 *tl++ = 0;
3030 else
3031 *tl++ = fdstateid.seqid;
3032 *tl++ = fdstateid.other[0];
3033 *tl++ = fdstateid.other[1];
3034 *tl = fdstateid.other[2];
3035 if (gottd) {
3036 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
3037 *tl = txdr_unsigned(NFSV4OP_PUTFH);
3038 np = VTONFS(tvp);
3039 (void)nfsm_fhtom(nmp, nd, np->n_fhp->nfh_fh,
3040 np->n_fhp->nfh_len, 0);
3041 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
3042 *tl = txdr_unsigned(NFSV4OP_DELEGRETURN);
3043 }
3044 }
3045 if (gottd) {
3046 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID);
3047 if (NFSHASNFSV4N(nmp))
3048 *tl++ = 0;
3049 else
3050 *tl++ = tdstateid.seqid;
3051 *tl++ = tdstateid.other[0];
3052 *tl++ = tdstateid.other[1];
3053 *tl = tdstateid.other[2];
3054 }
3055 if (ret > 0) {
3056 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
3057 *tl = txdr_unsigned(NFSV4OP_PUTFH);
3058 np = VTONFS(fdvp);
3059 (void)nfsm_fhtom(nmp, nd, np->n_fhp->nfh_fh,
3060 np->n_fhp->nfh_len, 0);
3061 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
3062 *tl = txdr_unsigned(NFSV4OP_SAVEFH);
3063 }
3064 } else {
3065 ret = 0;
3066 }
3067 if (ret == 0)
3068 NFSCL_REQSTART(nd, NFSPROC_RENAME, fdvp, cred);
3069 if ((nd->nd_flag & ND_NFSV4) != 0) {
3070 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
3071 *tl = txdr_unsigned(NFSV4OP_GETATTR);
3072 NFSWCCATTR_ATTRBIT(&attrbits);
3073 (void)nfsrv_putattrbit(nd, &attrbits);
3074 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
3075 *tl = txdr_unsigned(NFSV4OP_PUTFH);
3076 (void)nfsm_fhtom(nmp, nd, VTONFS(tdvp)->n_fhp->nfh_fh,
3077 VTONFS(tdvp)->n_fhp->nfh_len, 0);
3078 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
3079 *tl = txdr_unsigned(NFSV4OP_GETATTR);
3080 (void)nfsrv_putattrbit(nd, &attrbits);
3081 nd->nd_flag |= ND_V4WCCATTR;
3082 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
3083 *tl = txdr_unsigned(NFSV4OP_RENAME);
3084 }
3085 (void)nfsm_strtom(nd, fnameptr, fnamelen);
3086 if ((nd->nd_flag & ND_NFSV4) == 0)
3087 (void)nfsm_fhtom(nmp, nd, VTONFS(tdvp)->n_fhp->nfh_fh,
3088 VTONFS(tdvp)->n_fhp->nfh_len, 0);
3089 (void)nfsm_strtom(nd, tnameptr, tnamelen);
3090 if (ret == 0 && (nd->nd_flag & ND_NFSV4) != 0) {
3091 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
3092 /* When tvp == NULL, it doesn't matter which dvp is used. */
3093 *tl = txdr_unsigned(NFSV4OP_PUTFH);
3094 if (tvp != NULL)
3095 (void)nfsm_fhtom(nmp, nd, VTONFS(tvp)->n_fhp->nfh_fh,
3096 VTONFS(tvp)->n_fhp->nfh_len, 0);
3097 else
3098 (void)nfsm_fhtom(nmp, nd, VTONFS(tdvp)->n_fhp->nfh_fh,
3099 VTONFS(tdvp)->n_fhp->nfh_len, 0);
3100 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
3101 *tl = txdr_unsigned(NFSV4OP_GETATTR);
3102 NFSGETATTR_ATTRBIT(&attrbits);
3103 (void)nfsrv_putattrbit(nd, &attrbits);
3104 }
3105 error = nfscl_request(nd, fdvp, p, cred);
3106 if (error != 0)
3107 return (error);
3108 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) {
3109 /* For NFSv4, parse out any Delereturn replies. */
3110 if (ret > 0 && nd->nd_repstat != 0 &&
3111 (nd->nd_flag & ND_NOMOREDATA)) {
3112 /*
3113 * If the Delegreturn failed, try again without
3114 * it. The server will Recall, as required.
3115 */
3116 m_freem(nd->nd_mrep);
3117 goto tryagain;
3118 }
3119 for (i = 0; i < (ret * 2); i++) {
3120 if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) ==
3121 ND_NFSV4) {
3122 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
3123 if (*(tl + 1)) {
3124 if (i == 1 && ret > 1) {
3125 /*
3126 * If the Delegreturn failed, try again
3127 * without it. The server will Recall, as
3128 * required.
3129 * If ret > 1, the second iteration of this
3130 * loop is the second DelegReturn result.
3131 */
3132 m_freem(nd->nd_mrep);
3133 goto tryagain;
3134 } else {
3135 nd->nd_flag |= ND_NOMOREDATA;
3136 }
3137 }
3138 }
3139 }
3140 /* Now, the first wcc attribute reply. */
3141 if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) == ND_NFSV4) {
3142 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
3143 if (*(tl + 1))
3144 nd->nd_flag |= ND_NOMOREDATA;
3145 }
3146 error = nfscl_wcc_data(nd, fdvp, fnap, fattrflagp, NULL, NULL);
3147 /* and the second wcc attribute reply. */
3148 if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) == ND_NFSV4 &&
3149 error == 0) {
3150 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
3151 if (*(tl + 1))
3152 nd->nd_flag |= ND_NOMOREDATA;
3153 }
3154 if (error == 0)
3155 error = nfscl_wcc_data(nd, tdvp, tnap, tattrflagp,
3156 NULL, NULL);
3157 }
3158 if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) == ND_NFSV4 &&
3159 ret == 0 && error == 0) {
3160 /* Parse out the rename successful reply. */
3161 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED +
3162 4 * NFSX_HYPER);
3163 nd->nd_repstat = 0; /* Rename succeeded. */
3164 /* Parse PutFH reply for tvp. */
3165 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
3166 if (*(tl + 1) != 0) {
3167 if (tvp != NULL) {
3168 i = fxdr_unsigned(int, *(tl + 1));
3169 if (i == NFSERR_STALE)
3170 *tvp_status = DELETED;
3171 }
3172 } else {
3173 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
3174 if (*(tl + 1) != 0) {
3175 if (tvp != NULL) {
3176 i = fxdr_unsigned(int, *(tl + 1));
3177 if (i == NFSERR_STALE)
3178 *tvp_status = DELETED;
3179 }
3180 } else {
3181 error = nfsm_loadattr(nd, tvpnap);
3182 if (error == 0 && tvp != NULL) {
3183 *tvpattrflagp = 1;
3184 if (tvpnap->na_nlink == 0)
3185 *tvp_status = NLINK_ZERO;
3186 else
3187 *tvp_status = VALID;
3188 }
3189 }
3190 }
3191 }
3192 if (nd->nd_repstat != 0 && error == 0)
3193 error = nd->nd_repstat;
3194 nfsmout:
3195 m_freem(nd->nd_mrep);
3196 return (error);
3197 }
3198
3199 /*
3200 * nfs hard link create rpc
3201 */
3202 int
nfsrpc_link(vnode_t dvp,vnode_t vp,char * name,int namelen,struct ucred * cred,NFSPROC_T * p,struct nfsvattr * dnap,struct nfsvattr * nap,int * attrflagp,int * dattrflagp)3203 nfsrpc_link(vnode_t dvp, vnode_t vp, char *name, int namelen,
3204 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap,
3205 struct nfsvattr *nap, int *attrflagp, int *dattrflagp)
3206 {
3207 u_int32_t *tl;
3208 struct nfsrv_descript nfsd, *nd = &nfsd;
3209 nfsattrbit_t attrbits;
3210 int error = 0;
3211
3212 *attrflagp = 0;
3213 *dattrflagp = 0;
3214 if (namelen > NFS_MAXNAMLEN)
3215 return (ENAMETOOLONG);
3216 NFSCL_REQSTART(nd, NFSPROC_LINK, vp, cred);
3217 if (nd->nd_flag & ND_NFSV4) {
3218 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
3219 *tl = txdr_unsigned(NFSV4OP_PUTFH);
3220 }
3221 (void)nfsm_fhtom(VFSTONFS(dvp->v_mount), nd, VTONFS(dvp)->n_fhp->nfh_fh,
3222 VTONFS(dvp)->n_fhp->nfh_len, 0);
3223 if (nd->nd_flag & ND_NFSV4) {
3224 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
3225 *tl = txdr_unsigned(NFSV4OP_LINK);
3226 }
3227 (void) nfsm_strtom(nd, name, namelen);
3228 if (nd->nd_flag & ND_NFSV4) {
3229 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
3230 *tl = txdr_unsigned(NFSV4OP_GETATTR);
3231 NFSGETATTR_ATTRBIT(&attrbits);
3232 (void)nfsrv_putattrbit(nd, &attrbits);
3233 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_UNSIGNED);
3234 *tl++ = txdr_unsigned(NFSV4OP_RESTOREFH);
3235 *tl = txdr_unsigned(NFSV4OP_GETATTR);
3236 (void)nfsrv_putattrbit(nd, &attrbits);
3237 }
3238 error = nfscl_request(nd, vp, p, cred);
3239 if (error)
3240 return (error);
3241 if (nd->nd_flag & ND_NFSV3) {
3242 error = nfscl_postop_attr(nd, nap, attrflagp);
3243 if (!error)
3244 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp,
3245 NULL, NULL);
3246 } else if (nd->nd_repstat == 0 && (nd->nd_flag & ND_NFSV4) != 0) {
3247 /*
3248 * First and parse out the PutFH and Link results.
3249 */
3250 NFSM_DISSECT(tl, uint32_t *, 5 * NFSX_UNSIGNED +
3251 2 * NFSX_HYPER);
3252 if (*(tl + 3))
3253 nd->nd_flag |= ND_NOMOREDATA;
3254 /*
3255 * Get the directory post-op attributes.
3256 */
3257 if ((nd->nd_flag & ND_NOMOREDATA) == 0)
3258 error = nfscl_postop_attr(nd, dnap, dattrflagp);
3259 if (error == 0 && (nd->nd_flag & ND_NOMOREDATA) == 0) {
3260 /* Get rid of the RestoreFH reply. */
3261 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
3262 if (*(tl + 1))
3263 nd->nd_flag |= ND_NOMOREDATA;
3264 }
3265 /* Get the file's post-op attributes. */
3266 if (error == 0 && (nd->nd_flag & ND_NOMOREDATA) == 0)
3267 error = nfscl_postop_attr(nd, nap, attrflagp);
3268 }
3269 if (nd->nd_repstat && !error)
3270 error = nd->nd_repstat;
3271 nfsmout:
3272 m_freem(nd->nd_mrep);
3273 return (error);
3274 }
3275
3276 /*
3277 * nfs symbolic link create rpc
3278 */
3279 int
nfsrpc_symlink(vnode_t dvp,char * name,int namelen,const char * target,struct vattr * vap,struct ucred * cred,NFSPROC_T * p,struct nfsvattr * dnap,struct nfsvattr * nnap,struct nfsfh ** nfhpp,int * attrflagp,int * dattrflagp)3280 nfsrpc_symlink(vnode_t dvp, char *name, int namelen, const char *target,
3281 struct vattr *vap, struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap,
3282 struct nfsvattr *nnap, struct nfsfh **nfhpp, int *attrflagp,
3283 int *dattrflagp)
3284 {
3285 u_int32_t *tl;
3286 struct nfsrv_descript nfsd, *nd = &nfsd;
3287 struct nfsmount *nmp;
3288 int slen, error = 0;
3289
3290 *nfhpp = NULL;
3291 *attrflagp = 0;
3292 *dattrflagp = 0;
3293 nmp = VFSTONFS(dvp->v_mount);
3294 slen = strlen(target);
3295 if (slen > NFS_MAXPATHLEN || namelen > NFS_MAXNAMLEN)
3296 return (ENAMETOOLONG);
3297 NFSCL_REQSTART(nd, NFSPROC_SYMLINK, dvp, cred);
3298 if (nd->nd_flag & ND_NFSV4) {
3299 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
3300 *tl = txdr_unsigned(NFLNK);
3301 (void) nfsm_strtom(nd, target, slen);
3302 }
3303 (void) nfsm_strtom(nd, name, namelen);
3304 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4))
3305 nfscl_fillsattr(nd, vap, dvp, 0, 0);
3306 if (!(nd->nd_flag & ND_NFSV4))
3307 (void) nfsm_strtom(nd, target, slen);
3308 if (nd->nd_flag & ND_NFSV2)
3309 nfscl_fillsattr(nd, vap, dvp, NFSSATTR_SIZENEG1, 0);
3310 error = nfscl_request(nd, dvp, p, cred);
3311 if (error)
3312 return (error);
3313 if (nd->nd_flag & ND_NFSV4)
3314 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, NULL);
3315 if ((nd->nd_flag & ND_NFSV3) && !error) {
3316 if (!nd->nd_repstat)
3317 error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp);
3318 if (!error)
3319 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp,
3320 NULL, NULL);
3321 }
3322 if (nd->nd_repstat && !error)
3323 error = nd->nd_repstat;
3324 m_freem(nd->nd_mrep);
3325 /*
3326 * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry.
3327 * Only do this if vfs.nfs.ignore_eexist is set.
3328 * Never do this for NFSv4.1 or later minor versions, since sessions
3329 * should guarantee "exactly once" RPC semantics.
3330 */
3331 if (error == EEXIST && nfsignore_eexist != 0 && (!NFSHASNFSV4(nmp) ||
3332 nmp->nm_minorvers == 0))
3333 error = 0;
3334 return (error);
3335 }
3336
3337 /*
3338 * nfs make dir rpc
3339 */
3340 int
nfsrpc_mkdir(vnode_t dvp,char * name,int namelen,struct vattr * vap,struct ucred * cred,NFSPROC_T * p,struct nfsvattr * dnap,struct nfsvattr * nnap,struct nfsfh ** nfhpp,int * attrflagp,int * dattrflagp)3341 nfsrpc_mkdir(vnode_t dvp, char *name, int namelen, struct vattr *vap,
3342 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap,
3343 struct nfsvattr *nnap, struct nfsfh **nfhpp, int *attrflagp,
3344 int *dattrflagp)
3345 {
3346 u_int32_t *tl;
3347 struct nfsrv_descript nfsd, *nd = &nfsd;
3348 nfsattrbit_t attrbits;
3349 int error = 0;
3350 struct nfsfh *fhp;
3351 struct nfsmount *nmp;
3352
3353 *nfhpp = NULL;
3354 *attrflagp = 0;
3355 *dattrflagp = 0;
3356 nmp = VFSTONFS(dvp->v_mount);
3357 fhp = VTONFS(dvp)->n_fhp;
3358 if (namelen > NFS_MAXNAMLEN)
3359 return (ENAMETOOLONG);
3360 NFSCL_REQSTART(nd, NFSPROC_MKDIR, dvp, cred);
3361 if (nd->nd_flag & ND_NFSV4) {
3362 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
3363 *tl = txdr_unsigned(NFDIR);
3364 }
3365 (void) nfsm_strtom(nd, name, namelen);
3366 nfscl_fillsattr(nd, vap, dvp, NFSSATTR_SIZENEG1 | NFSSATTR_NEWFILE, 0);
3367 if (nd->nd_flag & ND_NFSV4) {
3368 NFSGETATTR_ATTRBIT(&attrbits);
3369 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
3370 *tl++ = txdr_unsigned(NFSV4OP_GETFH);
3371 *tl = txdr_unsigned(NFSV4OP_GETATTR);
3372 (void) nfsrv_putattrbit(nd, &attrbits);
3373 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
3374 *tl = txdr_unsigned(NFSV4OP_PUTFH);
3375 (void)nfsm_fhtom(nmp, nd, fhp->nfh_fh, fhp->nfh_len, 0);
3376 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
3377 *tl = txdr_unsigned(NFSV4OP_GETATTR);
3378 (void) nfsrv_putattrbit(nd, &attrbits);
3379 }
3380 error = nfscl_request(nd, dvp, p, cred);
3381 if (error)
3382 return (error);
3383 if (nd->nd_flag & ND_NFSV4)
3384 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, NULL);
3385 if (!nd->nd_repstat && !error) {
3386 if (nd->nd_flag & ND_NFSV4) {
3387 NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
3388 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
3389 }
3390 if (!error)
3391 error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp);
3392 if (error == 0 && (nd->nd_flag & ND_NFSV4) != 0) {
3393 /* Get rid of the PutFH and Getattr status values. */
3394 NFSM_DISSECT(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
3395 /* Load the directory attributes. */
3396 error = nfsm_loadattr(nd, dnap);
3397 if (error == 0)
3398 *dattrflagp = 1;
3399 }
3400 }
3401 if ((nd->nd_flag & ND_NFSV3) && !error)
3402 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, NULL);
3403 if (nd->nd_repstat && !error)
3404 error = nd->nd_repstat;
3405 nfsmout:
3406 m_freem(nd->nd_mrep);
3407 /*
3408 * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry.
3409 * Only do this if vfs.nfs.ignore_eexist is set.
3410 * Never do this for NFSv4.1 or later minor versions, since sessions
3411 * should guarantee "exactly once" RPC semantics.
3412 */
3413 if (error == EEXIST && nfsignore_eexist != 0 && (!NFSHASNFSV4(nmp) ||
3414 nmp->nm_minorvers == 0))
3415 error = 0;
3416 return (error);
3417 }
3418
3419 /*
3420 * nfs remove directory call
3421 */
3422 int
nfsrpc_rmdir(vnode_t dvp,char * name,int namelen,struct ucred * cred,NFSPROC_T * p,struct nfsvattr * dnap,int * dattrflagp)3423 nfsrpc_rmdir(vnode_t dvp, char *name, int namelen, struct ucred *cred,
3424 NFSPROC_T *p, struct nfsvattr *dnap, int *dattrflagp)
3425 {
3426 struct nfsrv_descript nfsd, *nd = &nfsd;
3427 int error = 0;
3428
3429 *dattrflagp = 0;
3430 if (namelen > NFS_MAXNAMLEN)
3431 return (ENAMETOOLONG);
3432 NFSCL_REQSTART(nd, NFSPROC_RMDIR, dvp, cred);
3433 (void) nfsm_strtom(nd, name, namelen);
3434 error = nfscl_request(nd, dvp, p, cred);
3435 if (error)
3436 return (error);
3437 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4))
3438 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, NULL);
3439 if (nd->nd_repstat && !error)
3440 error = nd->nd_repstat;
3441 m_freem(nd->nd_mrep);
3442 /*
3443 * Kludge: Map ENOENT => 0 assuming that you have a reply to a retry.
3444 */
3445 if (error == ENOENT)
3446 error = 0;
3447 return (error);
3448 }
3449
3450 /*
3451 * Check to make sure the file name in a Readdir reply is valid.
3452 */
3453 static bool
nfscl_invalidfname(bool is_v4,char * name,int len)3454 nfscl_invalidfname(bool is_v4, char *name, int len)
3455 {
3456 int i;
3457 char *cp;
3458
3459 if (is_v4 && ((len == 1 && name[0] == '.') ||
3460 (len == 2 && name[0] == '.' && name[1] == '.'))) {
3461 printf("Readdir NFSv4 reply has dot or dotdot in it\n");
3462 return (true);
3463 }
3464 cp = name;
3465 for (i = 0; i < len; i++, cp++) {
3466 if (*cp == '/' || *cp == '\0') {
3467 printf("Readdir reply file name had imbedded / or nul"
3468 " byte\n");
3469 return (true);
3470 }
3471 }
3472 return (false);
3473 }
3474
3475 /*
3476 * Readdir rpc.
3477 * Always returns with either uio_resid unchanged, if you are at the
3478 * end of the directory, or uio_resid == 0, with all DIRBLKSIZ chunks
3479 * filled in.
3480 * I felt this would allow caching of directory blocks more easily
3481 * than returning a pertially filled block.
3482 * Directory offset cookies:
3483 * Oh my, what to do with them...
3484 * I can think of three ways to deal with them:
3485 * 1 - have the layer above these RPCs maintain a map between logical
3486 * directory byte offsets and the NFS directory offset cookies
3487 * 2 - pass the opaque directory offset cookies up into userland
3488 * and let the libc functions deal with them, via the system call
3489 * 3 - return them to userland in the "struct dirent", so future versions
3490 * of libc can use them and do whatever is necessary to make things work
3491 * above these rpc calls, in the meantime
3492 * For now, I do #3 by "hiding" the directory offset cookies after the
3493 * d_name field in struct dirent. This is space inside d_reclen that
3494 * will be ignored by anything that doesn't know about them.
3495 * The directory offset cookies are filled in as the last 8 bytes of
3496 * each directory entry, after d_name. Someday, the userland libc
3497 * functions may be able to use these. In the meantime, it satisfies
3498 * OpenBSD's requirements for cookies being returned.
3499 * If expects the directory offset cookie for the read to be in uio_offset
3500 * and returns the one for the next entry after this directory block in
3501 * there, as well.
3502 */
3503 int
nfsrpc_readdir(vnode_t vp,struct uio * uiop,nfsuint64 * cookiep,struct ucred * cred,NFSPROC_T * p,struct nfsvattr * nap,int * attrflagp,int * eofp)3504 nfsrpc_readdir(vnode_t vp, struct uio *uiop, nfsuint64 *cookiep,
3505 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp,
3506 int *eofp)
3507 {
3508 int len, left;
3509 struct dirent *dp = NULL;
3510 u_int32_t *tl;
3511 nfsquad_t cookie, ncookie;
3512 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
3513 struct nfsnode *dnp = VTONFS(vp);
3514 struct nfsvattr nfsva;
3515 struct nfsrv_descript nfsd, *nd = &nfsd;
3516 int error = 0, tlen, more_dirs = 1, blksiz = 0, bigenough = 1;
3517 int reqsize, tryformoredirs = 1, readsize, eof = 0, gotmnton = 0;
3518 u_int64_t dotfileid, dotdotfileid = 0, fakefileno = UINT64_MAX;
3519 char *cp;
3520 nfsattrbit_t attrbits, dattrbits;
3521 u_int32_t rderr, *tl2 = NULL;
3522 size_t tresid;
3523 bool validentry;
3524
3525 KASSERT(uiop->uio_iovcnt == 1 &&
3526 (uiop->uio_resid & (DIRBLKSIZ - 1)) == 0,
3527 ("nfs readdirrpc bad uio"));
3528 KASSERT(uiop->uio_segflg == UIO_SYSSPACE,
3529 ("nfsrpc_readdir: uio userspace"));
3530 ncookie.lval[0] = ncookie.lval[1] = 0;
3531 /*
3532 * There is no point in reading a lot more than uio_resid, however
3533 * adding one additional DIRBLKSIZ makes sense. Since uio_resid
3534 * and nm_readdirsize are both exact multiples of DIRBLKSIZ, this
3535 * will never make readsize > nm_readdirsize.
3536 */
3537 readsize = nmp->nm_readdirsize;
3538 if (readsize > uiop->uio_resid)
3539 readsize = uiop->uio_resid + DIRBLKSIZ;
3540
3541 *attrflagp = 0;
3542 if (eofp)
3543 *eofp = 0;
3544 tresid = uiop->uio_resid;
3545 cookie.lval[0] = cookiep->nfsuquad[0];
3546 cookie.lval[1] = cookiep->nfsuquad[1];
3547 nd->nd_mrep = NULL;
3548
3549 /*
3550 * For NFSv4, first create the "." and ".." entries.
3551 */
3552 if (NFSHASNFSV4(nmp)) {
3553 reqsize = 6 * NFSX_UNSIGNED;
3554 NFSGETATTR_ATTRBIT(&dattrbits);
3555 NFSZERO_ATTRBIT(&attrbits);
3556 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_FILEID);
3557 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TYPE);
3558 if (NFSISSET_ATTRBIT(&dnp->n_vattr.na_suppattr,
3559 NFSATTRBIT_MOUNTEDONFILEID)) {
3560 NFSSETBIT_ATTRBIT(&attrbits,
3561 NFSATTRBIT_MOUNTEDONFILEID);
3562 gotmnton = 1;
3563 } else {
3564 /*
3565 * Must fake it. Use the fileno, except when the
3566 * fsid is != to that of the directory. For that
3567 * case, generate a fake fileno that is not the same.
3568 */
3569 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_FSID);
3570 gotmnton = 0;
3571 }
3572
3573 /*
3574 * Joy, oh joy. For V4 we get to hand craft '.' and '..'.
3575 */
3576 if (uiop->uio_offset == 0) {
3577 NFSCL_REQSTART(nd, NFSPROC_LOOKUPP, vp, cred);
3578 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
3579 *tl++ = txdr_unsigned(NFSV4OP_GETFH);
3580 *tl = txdr_unsigned(NFSV4OP_GETATTR);
3581 (void) nfsrv_putattrbit(nd, &attrbits);
3582 error = nfscl_request(nd, vp, p, cred);
3583 if (error)
3584 return (error);
3585 dotfileid = 0; /* Fake out the compiler. */
3586 if ((nd->nd_flag & ND_NOMOREDATA) == 0) {
3587 error = nfsm_loadattr(nd, &nfsva);
3588 if (error != 0)
3589 goto nfsmout;
3590 dotfileid = nfsva.na_fileid;
3591 }
3592 if (nd->nd_repstat == 0) {
3593 NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
3594 len = fxdr_unsigned(int, *(tl + 4));
3595 if (len > 0 && len <= NFSX_V4FHMAX)
3596 error = nfsm_advance(nd, NFSM_RNDUP(len), -1);
3597 else
3598 error = EPERM;
3599 if (!error) {
3600 NFSM_DISSECT(tl, u_int32_t *, 2*NFSX_UNSIGNED);
3601 nfsva.na_mntonfileno = UINT64_MAX;
3602 error = nfsv4_loadattr(nd, NULL, &nfsva, NULL,
3603 NULL, 0, NULL, NULL, NULL, NULL, NULL, 0,
3604 NULL, NULL, NULL, NULL, NULL, p, cred);
3605 if (error) {
3606 dotdotfileid = dotfileid;
3607 } else if (gotmnton) {
3608 if (nfsva.na_mntonfileno != UINT64_MAX)
3609 dotdotfileid = nfsva.na_mntonfileno;
3610 else
3611 dotdotfileid = nfsva.na_fileid;
3612 } else if (nfsva.na_filesid[0] ==
3613 dnp->n_vattr.na_filesid[0] &&
3614 nfsva.na_filesid[1] ==
3615 dnp->n_vattr.na_filesid[1]) {
3616 dotdotfileid = nfsva.na_fileid;
3617 } else {
3618 do {
3619 fakefileno--;
3620 } while (fakefileno ==
3621 nfsva.na_fileid);
3622 dotdotfileid = fakefileno;
3623 }
3624 }
3625 } else if (nd->nd_repstat == NFSERR_NOENT) {
3626 /*
3627 * Lookupp returns NFSERR_NOENT when we are
3628 * at the root, so just use the current dir.
3629 */
3630 nd->nd_repstat = 0;
3631 dotdotfileid = dotfileid;
3632 } else {
3633 error = nd->nd_repstat;
3634 }
3635 m_freem(nd->nd_mrep);
3636 if (error)
3637 return (error);
3638 nd->nd_mrep = NULL;
3639 dp = (struct dirent *)uiop->uio_iov->iov_base;
3640 dp->d_pad0 = dp->d_pad1 = 0;
3641 dp->d_off = 0;
3642 dp->d_type = DT_DIR;
3643 dp->d_fileno = dotfileid;
3644 dp->d_namlen = 1;
3645 *((uint64_t *)dp->d_name) = 0; /* Zero pad it. */
3646 dp->d_name[0] = '.';
3647 dp->d_reclen = _GENERIC_DIRSIZ(dp) + NFSX_HYPER;
3648 /*
3649 * Just make these offset cookie 0.
3650 */
3651 tl = (u_int32_t *)&dp->d_name[8];
3652 *tl++ = 0;
3653 *tl = 0;
3654 blksiz += dp->d_reclen;
3655 uiop->uio_resid -= dp->d_reclen;
3656 uiop->uio_offset += dp->d_reclen;
3657 uiop->uio_iov->iov_base =
3658 (char *)uiop->uio_iov->iov_base + dp->d_reclen;
3659 uiop->uio_iov->iov_len -= dp->d_reclen;
3660 dp = (struct dirent *)uiop->uio_iov->iov_base;
3661 dp->d_pad0 = dp->d_pad1 = 0;
3662 dp->d_off = 0;
3663 dp->d_type = DT_DIR;
3664 dp->d_fileno = dotdotfileid;
3665 dp->d_namlen = 2;
3666 *((uint64_t *)dp->d_name) = 0;
3667 dp->d_name[0] = '.';
3668 dp->d_name[1] = '.';
3669 dp->d_reclen = _GENERIC_DIRSIZ(dp) + NFSX_HYPER;
3670 /*
3671 * Just make these offset cookie 0.
3672 */
3673 tl = (u_int32_t *)&dp->d_name[8];
3674 *tl++ = 0;
3675 *tl = 0;
3676 blksiz += dp->d_reclen;
3677 uiop->uio_resid -= dp->d_reclen;
3678 uiop->uio_offset += dp->d_reclen;
3679 uiop->uio_iov->iov_base =
3680 (char *)uiop->uio_iov->iov_base + dp->d_reclen;
3681 uiop->uio_iov->iov_len -= dp->d_reclen;
3682 }
3683 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_RDATTRERROR);
3684 } else {
3685 reqsize = 5 * NFSX_UNSIGNED;
3686 }
3687
3688 /*
3689 * Loop around doing readdir rpc's of size readsize.
3690 * The stopping criteria is EOF or buffer full.
3691 */
3692 while (more_dirs && bigenough) {
3693 *attrflagp = 0;
3694 NFSCL_REQSTART(nd, NFSPROC_READDIR, vp, cred);
3695 if (nd->nd_flag & ND_NFSV2) {
3696 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
3697 *tl++ = cookie.lval[1];
3698 *tl = txdr_unsigned(readsize);
3699 } else {
3700 NFSM_BUILD(tl, u_int32_t *, reqsize);
3701 *tl++ = cookie.lval[0];
3702 *tl++ = cookie.lval[1];
3703 if (cookie.qval == 0) {
3704 *tl++ = 0;
3705 *tl++ = 0;
3706 } else {
3707 NFSLOCKNODE(dnp);
3708 *tl++ = dnp->n_cookieverf.nfsuquad[0];
3709 *tl++ = dnp->n_cookieverf.nfsuquad[1];
3710 NFSUNLOCKNODE(dnp);
3711 }
3712 if (nd->nd_flag & ND_NFSV4) {
3713 *tl++ = txdr_unsigned(readsize);
3714 *tl = txdr_unsigned(readsize);
3715 (void) nfsrv_putattrbit(nd, &attrbits);
3716 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
3717 *tl = txdr_unsigned(NFSV4OP_GETATTR);
3718 (void) nfsrv_putattrbit(nd, &dattrbits);
3719 } else {
3720 *tl = txdr_unsigned(readsize);
3721 }
3722 }
3723 error = nfscl_request(nd, vp, p, cred);
3724 if (error)
3725 return (error);
3726 if (!(nd->nd_flag & ND_NFSV2)) {
3727 if (nd->nd_flag & ND_NFSV3)
3728 error = nfscl_postop_attr(nd, nap, attrflagp);
3729 if (!nd->nd_repstat && !error) {
3730 NFSM_DISSECT(tl, u_int32_t *, NFSX_HYPER);
3731 NFSLOCKNODE(dnp);
3732 dnp->n_cookieverf.nfsuquad[0] = *tl++;
3733 dnp->n_cookieverf.nfsuquad[1] = *tl;
3734 NFSUNLOCKNODE(dnp);
3735 }
3736 }
3737 if (nd->nd_repstat || error) {
3738 if (!error)
3739 error = nd->nd_repstat;
3740 goto nfsmout;
3741 }
3742 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3743 more_dirs = fxdr_unsigned(int, *tl);
3744 if (!more_dirs)
3745 tryformoredirs = 0;
3746
3747 /* loop through the dir entries, doctoring them to 4bsd form */
3748 while (more_dirs && bigenough) {
3749 validentry = true;
3750 if (nd->nd_flag & ND_NFSV4) {
3751 NFSM_DISSECT(tl, u_int32_t *, 3*NFSX_UNSIGNED);
3752 ncookie.lval[0] = *tl++;
3753 ncookie.lval[1] = *tl++;
3754 len = fxdr_unsigned(int, *tl);
3755 } else if (nd->nd_flag & ND_NFSV3) {
3756 NFSM_DISSECT(tl, u_int32_t *, 3*NFSX_UNSIGNED);
3757 nfsva.na_fileid = fxdr_hyper(tl);
3758 tl += 2;
3759 len = fxdr_unsigned(int, *tl);
3760 } else {
3761 NFSM_DISSECT(tl, u_int32_t *, 2*NFSX_UNSIGNED);
3762 nfsva.na_fileid = fxdr_unsigned(uint64_t,
3763 *tl++);
3764 len = fxdr_unsigned(int, *tl);
3765 }
3766 if (len <= 0 || len > NFS_MAXNAMLEN) {
3767 error = EBADRPC;
3768 goto nfsmout;
3769 }
3770 tlen = roundup2(len, 8);
3771 if (tlen == len)
3772 tlen += 8; /* To ensure null termination. */
3773 left = DIRBLKSIZ - blksiz;
3774 if (_GENERIC_DIRLEN(len) + NFSX_HYPER > left) {
3775 NFSBZERO(uiop->uio_iov->iov_base, left);
3776 dp->d_reclen += left;
3777 uiop->uio_iov->iov_base =
3778 (char *)uiop->uio_iov->iov_base + left;
3779 uiop->uio_iov->iov_len -= left;
3780 uiop->uio_resid -= left;
3781 uiop->uio_offset += left;
3782 blksiz = 0;
3783 }
3784 if (_GENERIC_DIRLEN(len) + NFSX_HYPER >
3785 uiop->uio_resid)
3786 bigenough = 0;
3787 if (bigenough) {
3788 struct iovec saviov;
3789 off_t savoff;
3790 ssize_t savresid;
3791 int savblksiz;
3792
3793 saviov.iov_base = uiop->uio_iov->iov_base;
3794 saviov.iov_len = uiop->uio_iov->iov_len;
3795 savoff = uiop->uio_offset;
3796 savresid = uiop->uio_resid;
3797 savblksiz = blksiz;
3798
3799 dp = (struct dirent *)uiop->uio_iov->iov_base;
3800 dp->d_pad0 = dp->d_pad1 = 0;
3801 dp->d_off = 0;
3802 dp->d_namlen = len;
3803 dp->d_reclen = _GENERIC_DIRLEN(len) +
3804 NFSX_HYPER;
3805 dp->d_type = DT_UNKNOWN;
3806 blksiz += dp->d_reclen;
3807 if (blksiz == DIRBLKSIZ)
3808 blksiz = 0;
3809 uiop->uio_resid -= DIRHDSIZ;
3810 uiop->uio_offset += DIRHDSIZ;
3811 uiop->uio_iov->iov_base =
3812 (char *)uiop->uio_iov->iov_base + DIRHDSIZ;
3813 uiop->uio_iov->iov_len -= DIRHDSIZ;
3814 cp = uiop->uio_iov->iov_base;
3815 error = nfsm_mbufuio(nd, uiop, len);
3816 if (error)
3817 goto nfsmout;
3818 /* Check for an invalid file name. */
3819 if (nfscl_invalidfname(
3820 (nd->nd_flag & ND_NFSV4) != 0, cp, len)) {
3821 /* Skip over this entry. */
3822 uiop->uio_iov->iov_base =
3823 saviov.iov_base;
3824 uiop->uio_iov->iov_len =
3825 saviov.iov_len;
3826 uiop->uio_offset = savoff;
3827 uiop->uio_resid = savresid;
3828 blksiz = savblksiz;
3829 validentry = false;
3830 } else {
3831 cp = uiop->uio_iov->iov_base;
3832 tlen -= len;
3833 NFSBZERO(cp, tlen);
3834 cp += tlen; /* points to cookie store */
3835 tl2 = (u_int32_t *)cp;
3836 uiop->uio_iov->iov_base =
3837 (char *)uiop->uio_iov->iov_base +
3838 tlen + NFSX_HYPER;
3839 uiop->uio_iov->iov_len -= tlen +
3840 NFSX_HYPER;
3841 uiop->uio_resid -= tlen + NFSX_HYPER;
3842 uiop->uio_offset += (tlen + NFSX_HYPER);
3843 }
3844 } else {
3845 error = nfsm_advance(nd, NFSM_RNDUP(len), -1);
3846 if (error)
3847 goto nfsmout;
3848 }
3849 if (nd->nd_flag & ND_NFSV4) {
3850 rderr = 0;
3851 nfsva.na_mntonfileno = UINT64_MAX;
3852 error = nfsv4_loadattr(nd, NULL, &nfsva, NULL,
3853 NULL, 0, NULL, NULL, NULL, NULL, NULL, 0,
3854 NULL, NULL, &rderr, NULL, NULL, p, cred);
3855 if (error)
3856 goto nfsmout;
3857 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3858 } else if (nd->nd_flag & ND_NFSV3) {
3859 NFSM_DISSECT(tl, u_int32_t *, 3*NFSX_UNSIGNED);
3860 ncookie.lval[0] = *tl++;
3861 ncookie.lval[1] = *tl++;
3862 } else {
3863 NFSM_DISSECT(tl, u_int32_t *, 2*NFSX_UNSIGNED);
3864 ncookie.lval[0] = 0;
3865 ncookie.lval[1] = *tl++;
3866 }
3867 if (bigenough && validentry) {
3868 if (nd->nd_flag & ND_NFSV4) {
3869 if (rderr) {
3870 dp->d_fileno = 0;
3871 } else {
3872 if (gotmnton) {
3873 if (nfsva.na_mntonfileno != UINT64_MAX)
3874 dp->d_fileno = nfsva.na_mntonfileno;
3875 else
3876 dp->d_fileno = nfsva.na_fileid;
3877 } else if (nfsva.na_filesid[0] ==
3878 dnp->n_vattr.na_filesid[0] &&
3879 nfsva.na_filesid[1] ==
3880 dnp->n_vattr.na_filesid[1]) {
3881 dp->d_fileno = nfsva.na_fileid;
3882 } else {
3883 do {
3884 fakefileno--;
3885 } while (fakefileno ==
3886 nfsva.na_fileid);
3887 dp->d_fileno = fakefileno;
3888 }
3889 dp->d_type = vtonfs_dtype(nfsva.na_type);
3890 }
3891 } else {
3892 dp->d_fileno = nfsva.na_fileid;
3893 }
3894 *tl2++ = cookiep->nfsuquad[0] = cookie.lval[0] =
3895 ncookie.lval[0];
3896 *tl2 = cookiep->nfsuquad[1] = cookie.lval[1] =
3897 ncookie.lval[1];
3898 }
3899 more_dirs = fxdr_unsigned(int, *tl);
3900 }
3901 /*
3902 * If at end of rpc data, get the eof boolean
3903 */
3904 if (!more_dirs) {
3905 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3906 eof = fxdr_unsigned(int, *tl);
3907 if (tryformoredirs)
3908 more_dirs = !eof;
3909 if (nd->nd_flag & ND_NFSV4) {
3910 error = nfscl_postop_attr(nd, nap, attrflagp);
3911 if (error)
3912 goto nfsmout;
3913 }
3914 }
3915 m_freem(nd->nd_mrep);
3916 nd->nd_mrep = NULL;
3917 }
3918 /*
3919 * Fill last record, iff any, out to a multiple of DIRBLKSIZ
3920 * by increasing d_reclen for the last record.
3921 */
3922 if (blksiz > 0) {
3923 left = DIRBLKSIZ - blksiz;
3924 NFSBZERO(uiop->uio_iov->iov_base, left);
3925 dp->d_reclen += left;
3926 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base +
3927 left;
3928 uiop->uio_iov->iov_len -= left;
3929 uiop->uio_resid -= left;
3930 uiop->uio_offset += left;
3931 }
3932
3933 /*
3934 * If returning no data, assume end of file.
3935 * If not bigenough, return not end of file, since you aren't
3936 * returning all the data
3937 * Otherwise, return the eof flag from the server.
3938 */
3939 if (eofp) {
3940 if (tresid == ((size_t)(uiop->uio_resid)))
3941 *eofp = 1;
3942 else if (!bigenough)
3943 *eofp = 0;
3944 else
3945 *eofp = eof;
3946 }
3947
3948 /*
3949 * Add extra empty records to any remaining DIRBLKSIZ chunks.
3950 */
3951 while (uiop->uio_resid > 0 && uiop->uio_resid != tresid) {
3952 dp = (struct dirent *)uiop->uio_iov->iov_base;
3953 NFSBZERO(dp, DIRBLKSIZ);
3954 dp->d_type = DT_UNKNOWN;
3955 tl = (u_int32_t *)&dp->d_name[4];
3956 *tl++ = cookie.lval[0];
3957 *tl = cookie.lval[1];
3958 dp->d_reclen = DIRBLKSIZ;
3959 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base +
3960 DIRBLKSIZ;
3961 uiop->uio_iov->iov_len -= DIRBLKSIZ;
3962 uiop->uio_resid -= DIRBLKSIZ;
3963 uiop->uio_offset += DIRBLKSIZ;
3964 }
3965
3966 nfsmout:
3967 if (nd->nd_mrep != NULL)
3968 m_freem(nd->nd_mrep);
3969 return (error);
3970 }
3971
3972 /*
3973 * NFS V3 readdir plus RPC. Used in place of nfsrpc_readdir().
3974 * (Also used for NFS V4 when mount flag set.)
3975 * (ditto above w.r.t. multiple of DIRBLKSIZ, etc.)
3976 */
3977 int
nfsrpc_readdirplus(vnode_t vp,struct uio * uiop,nfsuint64 * cookiep,struct ucred * cred,NFSPROC_T * p,struct nfsvattr * nap,int * attrflagp,int * eofp)3978 nfsrpc_readdirplus(vnode_t vp, struct uio *uiop, nfsuint64 *cookiep,
3979 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp,
3980 int *eofp)
3981 {
3982 int len, left;
3983 struct dirent *dp = NULL;
3984 u_int32_t *tl;
3985 vnode_t newvp = NULL;
3986 struct nfsrv_descript nfsd, *nd = &nfsd;
3987 struct nameidata nami, *ndp = &nami;
3988 struct componentname *cnp = &ndp->ni_cnd;
3989 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
3990 struct nfsnode *dnp = VTONFS(vp), *np;
3991 struct nfsvattr nfsva;
3992 struct nfsfh *nfhp;
3993 nfsquad_t cookie, ncookie;
3994 int error = 0, tlen, more_dirs = 1, blksiz = 0, bigenough = 1;
3995 int attrflag, tryformoredirs = 1, eof = 0, gotmnton = 0;
3996 int isdotdot = 0, unlocknewvp = 0;
3997 u_int64_t dotfileid, dotdotfileid = 0, fakefileno = UINT64_MAX;
3998 u_int64_t fileno = 0;
3999 char *cp;
4000 nfsattrbit_t attrbits, dattrbits;
4001 size_t tresid;
4002 u_int32_t *tl2 = NULL, rderr;
4003 struct timespec dctime, ts;
4004 bool attr_ok, named_dir, validentry;
4005
4006 KASSERT(uiop->uio_iovcnt == 1 &&
4007 (uiop->uio_resid & (DIRBLKSIZ - 1)) == 0,
4008 ("nfs readdirplusrpc bad uio"));
4009 KASSERT(uiop->uio_segflg == UIO_SYSSPACE,
4010 ("nfsrpc_readdirplus: uio userspace"));
4011 named_dir = false;
4012 if ((vp->v_irflag & VIRF_NAMEDDIR) != 0)
4013 named_dir = true;
4014 ncookie.lval[0] = ncookie.lval[1] = 0;
4015 timespecclear(&dctime);
4016 *attrflagp = 0;
4017 if (eofp != NULL)
4018 *eofp = 0;
4019 ndp->ni_dvp = vp;
4020 nd->nd_mrep = NULL;
4021 cookie.lval[0] = cookiep->nfsuquad[0];
4022 cookie.lval[1] = cookiep->nfsuquad[1];
4023 tresid = uiop->uio_resid;
4024
4025 /*
4026 * For NFSv4, first create the "." and ".." entries.
4027 */
4028 if (NFSHASNFSV4(nmp)) {
4029 NFSGETATTR_ATTRBIT(&dattrbits);
4030 NFSZERO_ATTRBIT(&attrbits);
4031 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_FILEID);
4032 if (NFSISSET_ATTRBIT(&dnp->n_vattr.na_suppattr,
4033 NFSATTRBIT_MOUNTEDONFILEID)) {
4034 NFSSETBIT_ATTRBIT(&attrbits,
4035 NFSATTRBIT_MOUNTEDONFILEID);
4036 gotmnton = 1;
4037 } else {
4038 /*
4039 * Must fake it. Use the fileno, except when the
4040 * fsid is != to that of the directory. For that
4041 * case, generate a fake fileno that is not the same.
4042 */
4043 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_FSID);
4044 gotmnton = 0;
4045 }
4046
4047 /*
4048 * Joy, oh joy. For V4 we get to hand craft '.' and '..'.
4049 */
4050 if (uiop->uio_offset == 0) {
4051 NFSCL_REQSTART(nd, NFSPROC_LOOKUPP, vp, cred);
4052 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
4053 *tl++ = txdr_unsigned(NFSV4OP_GETFH);
4054 *tl = txdr_unsigned(NFSV4OP_GETATTR);
4055 (void) nfsrv_putattrbit(nd, &attrbits);
4056 error = nfscl_request(nd, vp, p, cred);
4057 if (error)
4058 return (error);
4059 dotfileid = 0; /* Fake out the compiler. */
4060 if ((nd->nd_flag & ND_NOMOREDATA) == 0) {
4061 error = nfsm_loadattr(nd, &nfsva);
4062 if (error != 0)
4063 goto nfsmout;
4064 dctime = nfsva.na_ctime;
4065 dotfileid = nfsva.na_fileid;
4066 }
4067 if (nd->nd_repstat == 0) {
4068 NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
4069 len = fxdr_unsigned(int, *(tl + 4));
4070 if (len > 0 && len <= NFSX_V4FHMAX)
4071 error = nfsm_advance(nd, NFSM_RNDUP(len), -1);
4072 else
4073 error = EPERM;
4074 if (!error) {
4075 NFSM_DISSECT(tl, u_int32_t *, 2*NFSX_UNSIGNED);
4076 nfsva.na_mntonfileno = UINT64_MAX;
4077 error = nfsv4_loadattr(nd, NULL, &nfsva, NULL,
4078 NULL, 0, NULL, NULL, NULL, NULL, NULL, 0,
4079 NULL, NULL, NULL, NULL, NULL, p, cred);
4080 if (error) {
4081 dotdotfileid = dotfileid;
4082 } else if (gotmnton) {
4083 if (nfsva.na_mntonfileno != UINT64_MAX)
4084 dotdotfileid = nfsva.na_mntonfileno;
4085 else
4086 dotdotfileid = nfsva.na_fileid;
4087 } else if (nfsva.na_filesid[0] ==
4088 dnp->n_vattr.na_filesid[0] &&
4089 nfsva.na_filesid[1] ==
4090 dnp->n_vattr.na_filesid[1]) {
4091 dotdotfileid = nfsva.na_fileid;
4092 } else {
4093 do {
4094 fakefileno--;
4095 } while (fakefileno ==
4096 nfsva.na_fileid);
4097 dotdotfileid = fakefileno;
4098 }
4099 }
4100 } else if (nd->nd_repstat == NFSERR_NOENT) {
4101 /*
4102 * Lookupp returns NFSERR_NOENT when we are
4103 * at the root, so just use the current dir.
4104 */
4105 nd->nd_repstat = 0;
4106 dotdotfileid = dotfileid;
4107 } else {
4108 error = nd->nd_repstat;
4109 }
4110 m_freem(nd->nd_mrep);
4111 if (error)
4112 return (error);
4113 nd->nd_mrep = NULL;
4114 dp = (struct dirent *)uiop->uio_iov->iov_base;
4115 dp->d_pad0 = dp->d_pad1 = 0;
4116 dp->d_off = 0;
4117 dp->d_type = DT_DIR;
4118 dp->d_fileno = dotfileid;
4119 dp->d_namlen = 1;
4120 *((uint64_t *)dp->d_name) = 0; /* Zero pad it. */
4121 dp->d_name[0] = '.';
4122 dp->d_reclen = _GENERIC_DIRSIZ(dp) + NFSX_HYPER;
4123 /*
4124 * Just make these offset cookie 0.
4125 */
4126 tl = (u_int32_t *)&dp->d_name[8];
4127 *tl++ = 0;
4128 *tl = 0;
4129 blksiz += dp->d_reclen;
4130 uiop->uio_resid -= dp->d_reclen;
4131 uiop->uio_offset += dp->d_reclen;
4132 uiop->uio_iov->iov_base =
4133 (char *)uiop->uio_iov->iov_base + dp->d_reclen;
4134 uiop->uio_iov->iov_len -= dp->d_reclen;
4135 dp = (struct dirent *)uiop->uio_iov->iov_base;
4136 dp->d_pad0 = dp->d_pad1 = 0;
4137 dp->d_off = 0;
4138 dp->d_type = DT_DIR;
4139 dp->d_fileno = dotdotfileid;
4140 dp->d_namlen = 2;
4141 *((uint64_t *)dp->d_name) = 0;
4142 dp->d_name[0] = '.';
4143 dp->d_name[1] = '.';
4144 dp->d_reclen = _GENERIC_DIRSIZ(dp) + NFSX_HYPER;
4145 /*
4146 * Just make these offset cookie 0.
4147 */
4148 tl = (u_int32_t *)&dp->d_name[8];
4149 *tl++ = 0;
4150 *tl = 0;
4151 blksiz += dp->d_reclen;
4152 uiop->uio_resid -= dp->d_reclen;
4153 uiop->uio_offset += dp->d_reclen;
4154 uiop->uio_iov->iov_base =
4155 (char *)uiop->uio_iov->iov_base + dp->d_reclen;
4156 uiop->uio_iov->iov_len -= dp->d_reclen;
4157 }
4158 NFSREADDIRPLUS_ATTRBIT(&attrbits);
4159 if (gotmnton)
4160 NFSSETBIT_ATTRBIT(&attrbits,
4161 NFSATTRBIT_MOUNTEDONFILEID);
4162 if (!NFSISSET_ATTRBIT(&dnp->n_vattr.na_suppattr,
4163 NFSATTRBIT_TIMECREATE))
4164 NFSCLRBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMECREATE);
4165 if (!NFSISSET_ATTRBIT(&dnp->n_vattr.na_suppattr,
4166 NFSATTRBIT_ARCHIVE) ||
4167 !NFSISSET_ATTRBIT(&dnp->n_vattr.na_suppattr,
4168 NFSATTRBIT_HIDDEN) ||
4169 !NFSISSET_ATTRBIT(&dnp->n_vattr.na_suppattr,
4170 NFSATTRBIT_SYSTEM)) {
4171 NFSCLRBIT_ATTRBIT(&attrbits, NFSATTRBIT_ARCHIVE);
4172 NFSCLRBIT_ATTRBIT(&attrbits, NFSATTRBIT_HIDDEN);
4173 NFSCLRBIT_ATTRBIT(&attrbits, NFSATTRBIT_SYSTEM);
4174 }
4175 }
4176
4177 /*
4178 * Loop around doing readdir rpc's of size nm_readdirsize.
4179 * The stopping criteria is EOF or buffer full.
4180 */
4181 while (more_dirs && bigenough) {
4182 *attrflagp = 0;
4183 NFSCL_REQSTART(nd, NFSPROC_READDIRPLUS, vp, cred);
4184 NFSM_BUILD(tl, u_int32_t *, 6 * NFSX_UNSIGNED);
4185 *tl++ = cookie.lval[0];
4186 *tl++ = cookie.lval[1];
4187 if (cookie.qval == 0) {
4188 *tl++ = 0;
4189 *tl++ = 0;
4190 } else {
4191 NFSLOCKNODE(dnp);
4192 *tl++ = dnp->n_cookieverf.nfsuquad[0];
4193 *tl++ = dnp->n_cookieverf.nfsuquad[1];
4194 NFSUNLOCKNODE(dnp);
4195 }
4196 *tl++ = txdr_unsigned(nmp->nm_readdirsize);
4197 *tl = txdr_unsigned(nmp->nm_readdirsize);
4198 if (nd->nd_flag & ND_NFSV4) {
4199 (void) nfsrv_putattrbit(nd, &attrbits);
4200 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
4201 *tl = txdr_unsigned(NFSV4OP_GETATTR);
4202 (void) nfsrv_putattrbit(nd, &dattrbits);
4203 }
4204 nanouptime(&ts);
4205 error = nfscl_request(nd, vp, p, cred);
4206 if (error)
4207 return (error);
4208 if (nd->nd_flag & ND_NFSV3)
4209 error = nfscl_postop_attr(nd, nap, attrflagp);
4210 if (nd->nd_repstat || error) {
4211 if (!error)
4212 error = nd->nd_repstat;
4213 goto nfsmout;
4214 }
4215 if ((nd->nd_flag & ND_NFSV3) != 0 && *attrflagp != 0)
4216 dctime = nap->na_ctime;
4217 NFSM_DISSECT(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
4218 NFSLOCKNODE(dnp);
4219 dnp->n_cookieverf.nfsuquad[0] = *tl++;
4220 dnp->n_cookieverf.nfsuquad[1] = *tl++;
4221 NFSUNLOCKNODE(dnp);
4222 more_dirs = fxdr_unsigned(int, *tl);
4223 if (!more_dirs)
4224 tryformoredirs = 0;
4225
4226 /* loop through the dir entries, doctoring them to 4bsd form */
4227 while (more_dirs && bigenough) {
4228 validentry = true;
4229 NFSM_DISSECT(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
4230 if (nd->nd_flag & ND_NFSV4) {
4231 ncookie.lval[0] = *tl++;
4232 ncookie.lval[1] = *tl++;
4233 } else {
4234 fileno = fxdr_hyper(tl);
4235 tl += 2;
4236 }
4237 len = fxdr_unsigned(int, *tl);
4238 if (len <= 0 || len > NFS_MAXNAMLEN) {
4239 error = EBADRPC;
4240 goto nfsmout;
4241 }
4242 tlen = roundup2(len, 8);
4243 if (tlen == len)
4244 tlen += 8; /* To ensure null termination. */
4245 left = DIRBLKSIZ - blksiz;
4246 if (_GENERIC_DIRLEN(len) + NFSX_HYPER > left) {
4247 NFSBZERO(uiop->uio_iov->iov_base, left);
4248 dp->d_reclen += left;
4249 uiop->uio_iov->iov_base =
4250 (char *)uiop->uio_iov->iov_base + left;
4251 uiop->uio_iov->iov_len -= left;
4252 uiop->uio_resid -= left;
4253 uiop->uio_offset += left;
4254 blksiz = 0;
4255 }
4256 if (_GENERIC_DIRLEN(len) + NFSX_HYPER >
4257 uiop->uio_resid)
4258 bigenough = 0;
4259 if (bigenough) {
4260 struct iovec saviov;
4261 off_t savoff;
4262 ssize_t savresid;
4263 int savblksiz;
4264
4265 saviov.iov_base = uiop->uio_iov->iov_base;
4266 saviov.iov_len = uiop->uio_iov->iov_len;
4267 savoff = uiop->uio_offset;
4268 savresid = uiop->uio_resid;
4269 savblksiz = blksiz;
4270
4271 dp = (struct dirent *)uiop->uio_iov->iov_base;
4272 dp->d_pad0 = dp->d_pad1 = 0;
4273 dp->d_off = 0;
4274 dp->d_namlen = len;
4275 dp->d_reclen = _GENERIC_DIRLEN(len) +
4276 NFSX_HYPER;
4277 dp->d_type = DT_UNKNOWN;
4278 blksiz += dp->d_reclen;
4279 if (blksiz == DIRBLKSIZ)
4280 blksiz = 0;
4281 uiop->uio_resid -= DIRHDSIZ;
4282 uiop->uio_offset += DIRHDSIZ;
4283 uiop->uio_iov->iov_base =
4284 (char *)uiop->uio_iov->iov_base + DIRHDSIZ;
4285 uiop->uio_iov->iov_len -= DIRHDSIZ;
4286 cnp->cn_nameptr = uiop->uio_iov->iov_base;
4287 cnp->cn_namelen = len;
4288 NFSCNHASHZERO(cnp);
4289 cp = uiop->uio_iov->iov_base;
4290 error = nfsm_mbufuio(nd, uiop, len);
4291 if (error)
4292 goto nfsmout;
4293 /* Check for an invalid file name. */
4294 if (nfscl_invalidfname(
4295 (nd->nd_flag & ND_NFSV4) != 0, cp, len)) {
4296 /* Skip over this entry. */
4297 uiop->uio_iov->iov_base =
4298 saviov.iov_base;
4299 uiop->uio_iov->iov_len =
4300 saviov.iov_len;
4301 uiop->uio_offset = savoff;
4302 uiop->uio_resid = savresid;
4303 blksiz = savblksiz;
4304 validentry = false;
4305 } else {
4306 cp = uiop->uio_iov->iov_base;
4307 tlen -= len;
4308 NFSBZERO(cp, tlen);
4309 cp += tlen; /* points to cookie store */
4310 tl2 = (u_int32_t *)cp;
4311 if (len == 2 &&
4312 cnp->cn_nameptr[0] == '.' &&
4313 cnp->cn_nameptr[1] == '.')
4314 isdotdot = 1;
4315 else
4316 isdotdot = 0;
4317 uiop->uio_iov->iov_base =
4318 (char *)uiop->uio_iov->iov_base +
4319 tlen + NFSX_HYPER;
4320 uiop->uio_iov->iov_len -= tlen +
4321 NFSX_HYPER;
4322 uiop->uio_resid -= tlen + NFSX_HYPER;
4323 uiop->uio_offset += (tlen + NFSX_HYPER);
4324 }
4325 } else {
4326 error = nfsm_advance(nd, NFSM_RNDUP(len), -1);
4327 if (error)
4328 goto nfsmout;
4329 }
4330 nfhp = NULL;
4331 if (nd->nd_flag & ND_NFSV3) {
4332 NFSM_DISSECT(tl, u_int32_t *, 3*NFSX_UNSIGNED);
4333 ncookie.lval[0] = *tl++;
4334 ncookie.lval[1] = *tl++;
4335 attrflag = fxdr_unsigned(int, *tl);
4336 if (attrflag) {
4337 error = nfsm_loadattr(nd, &nfsva);
4338 if (error)
4339 goto nfsmout;
4340 }
4341 NFSM_DISSECT(tl,u_int32_t *,NFSX_UNSIGNED);
4342 if (*tl) {
4343 error = nfsm_getfh(nd, &nfhp);
4344 if (error)
4345 goto nfsmout;
4346 }
4347 if (!attrflag && nfhp != NULL) {
4348 free(nfhp, M_NFSFH);
4349 nfhp = NULL;
4350 }
4351 } else {
4352 rderr = 0;
4353 nfsva.na_mntonfileno = 0xffffffff;
4354 error = nfsv4_loadattr(nd, NULL, &nfsva, &nfhp,
4355 NULL, 0, NULL, NULL, NULL, NULL, NULL, 0,
4356 NULL, NULL, &rderr, NULL, NULL, p, cred);
4357 if (error)
4358 goto nfsmout;
4359 }
4360
4361 if (bigenough && validentry) {
4362 if (nd->nd_flag & ND_NFSV4) {
4363 if (rderr) {
4364 dp->d_fileno = 0;
4365 } else if (gotmnton) {
4366 if (nfsva.na_mntonfileno != 0xffffffff)
4367 dp->d_fileno = nfsva.na_mntonfileno;
4368 else
4369 dp->d_fileno = nfsva.na_fileid;
4370 } else if (nfsva.na_filesid[0] ==
4371 dnp->n_vattr.na_filesid[0] &&
4372 nfsva.na_filesid[1] ==
4373 dnp->n_vattr.na_filesid[1]) {
4374 dp->d_fileno = nfsva.na_fileid;
4375 } else {
4376 do {
4377 fakefileno--;
4378 } while (fakefileno ==
4379 nfsva.na_fileid);
4380 dp->d_fileno = fakefileno;
4381 }
4382 } else {
4383 dp->d_fileno = fileno;
4384 }
4385 *tl2++ = cookiep->nfsuquad[0] = cookie.lval[0] =
4386 ncookie.lval[0];
4387 *tl2 = cookiep->nfsuquad[1] = cookie.lval[1] =
4388 ncookie.lval[1];
4389
4390 if (nfhp != NULL) {
4391 attr_ok = true;
4392 if (NFSRV_CMPFH(nfhp->nfh_fh, nfhp->nfh_len,
4393 dnp->n_fhp->nfh_fh, dnp->n_fhp->nfh_len)) {
4394 vref(vp);
4395 newvp = vp;
4396 unlocknewvp = 0;
4397 free(nfhp, M_NFSFH);
4398 np = dnp;
4399 } else if (isdotdot != 0) {
4400 /*
4401 * Skip doing a nfscl_nget() call for "..".
4402 * There's a race between acquiring the nfs
4403 * node here and lookups that look for the
4404 * directory being read (in the parent).
4405 * It would try to get a lock on ".." here,
4406 * owning the lock on the directory being
4407 * read. Lookup will hold the lock on ".."
4408 * and try to acquire the lock on the
4409 * directory being read.
4410 * If the directory is unlocked/relocked,
4411 * then there is a LOR with the buflock
4412 * vp is relocked.
4413 */
4414 free(nfhp, M_NFSFH);
4415 } else {
4416 error = nfscl_nget(vp->v_mount, vp,
4417 nfhp, cnp, p, &np, LK_EXCLUSIVE);
4418 if (!error) {
4419 newvp = NFSTOV(np);
4420 unlocknewvp = 1;
4421 /*
4422 * If n_localmodtime >= time before RPC,
4423 * then a file modification operation,
4424 * such as VOP_SETATTR() of size, has
4425 * occurred while the Lookup RPC and
4426 * acquisition of the vnode happened. As
4427 * such, the attributes might be stale,
4428 * with possibly an incorrect size.
4429 */
4430 NFSLOCKNODE(np);
4431 if (timespecisset(
4432 &np->n_localmodtime) &&
4433 timespeccmp(&np->n_localmodtime,
4434 &ts, >=)) {
4435 NFSCL_DEBUG(4, "nfsrpc_readdirplus:"
4436 " localmod stale attributes\n");
4437 attr_ok = false;
4438 }
4439 NFSUNLOCKNODE(np);
4440 }
4441 }
4442 nfhp = NULL;
4443 if (newvp != NULL) {
4444 if (attr_ok)
4445 error = nfscl_loadattrcache(&newvp,
4446 &nfsva, NULL, 0, 0);
4447 if (error) {
4448 if (unlocknewvp)
4449 vput(newvp);
4450 else
4451 vrele(newvp);
4452 goto nfsmout;
4453 }
4454 dp->d_type =
4455 vtonfs_dtype(np->n_vattr.na_type);
4456 ndp->ni_vp = newvp;
4457 NFSCNHASH(cnp, HASHINIT);
4458 if (cnp->cn_namelen <= NCHNAMLEN &&
4459 ndp->ni_dvp != ndp->ni_vp &&
4460 (newvp->v_type != VDIR ||
4461 dctime.tv_sec != 0) &&
4462 !named_dir) {
4463 cache_enter_time_flags(ndp->ni_dvp,
4464 ndp->ni_vp, cnp,
4465 &nfsva.na_ctime,
4466 newvp->v_type != VDIR ? NULL :
4467 &dctime, VFS_CACHE_DROPOLD);
4468 }
4469 if (unlocknewvp)
4470 vput(newvp);
4471 else
4472 vrele(newvp);
4473 newvp = NULL;
4474 }
4475 }
4476 } else if (nfhp != NULL) {
4477 free(nfhp, M_NFSFH);
4478 }
4479 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
4480 more_dirs = fxdr_unsigned(int, *tl);
4481 }
4482 /*
4483 * If at end of rpc data, get the eof boolean
4484 */
4485 if (!more_dirs) {
4486 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
4487 eof = fxdr_unsigned(int, *tl);
4488 if (tryformoredirs)
4489 more_dirs = !eof;
4490 if (nd->nd_flag & ND_NFSV4) {
4491 error = nfscl_postop_attr(nd, nap, attrflagp);
4492 if (error)
4493 goto nfsmout;
4494 }
4495 }
4496 m_freem(nd->nd_mrep);
4497 nd->nd_mrep = NULL;
4498 }
4499 /*
4500 * Fill last record, iff any, out to a multiple of DIRBLKSIZ
4501 * by increasing d_reclen for the last record.
4502 */
4503 if (blksiz > 0) {
4504 left = DIRBLKSIZ - blksiz;
4505 NFSBZERO(uiop->uio_iov->iov_base, left);
4506 dp->d_reclen += left;
4507 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base +
4508 left;
4509 uiop->uio_iov->iov_len -= left;
4510 uiop->uio_resid -= left;
4511 uiop->uio_offset += left;
4512 }
4513
4514 /*
4515 * If returning no data, assume end of file.
4516 * If not bigenough, return not end of file, since you aren't
4517 * returning all the data
4518 * Otherwise, return the eof flag from the server.
4519 */
4520 if (eofp != NULL) {
4521 if (tresid == uiop->uio_resid)
4522 *eofp = 1;
4523 else if (!bigenough)
4524 *eofp = 0;
4525 else
4526 *eofp = eof;
4527 }
4528
4529 /*
4530 * Add extra empty records to any remaining DIRBLKSIZ chunks.
4531 */
4532 while (uiop->uio_resid > 0 && uiop->uio_resid != tresid) {
4533 dp = (struct dirent *)uiop->uio_iov->iov_base;
4534 NFSBZERO(dp, DIRBLKSIZ);
4535 dp->d_type = DT_UNKNOWN;
4536 tl = (u_int32_t *)&dp->d_name[4];
4537 *tl++ = cookie.lval[0];
4538 *tl = cookie.lval[1];
4539 dp->d_reclen = DIRBLKSIZ;
4540 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base +
4541 DIRBLKSIZ;
4542 uiop->uio_iov->iov_len -= DIRBLKSIZ;
4543 uiop->uio_resid -= DIRBLKSIZ;
4544 uiop->uio_offset += DIRBLKSIZ;
4545 }
4546
4547 nfsmout:
4548 if (nd->nd_mrep != NULL)
4549 m_freem(nd->nd_mrep);
4550 return (error);
4551 }
4552
4553 /*
4554 * Nfs commit rpc
4555 */
4556 int
nfsrpc_commit(vnode_t vp,u_quad_t offset,int cnt,struct ucred * cred,NFSPROC_T * p,struct nfsvattr * nap,int * attrflagp)4557 nfsrpc_commit(vnode_t vp, u_quad_t offset, int cnt, struct ucred *cred,
4558 NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp)
4559 {
4560 u_int32_t *tl;
4561 struct nfsrv_descript nfsd, *nd = &nfsd;
4562 nfsattrbit_t attrbits;
4563 int error;
4564 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
4565
4566 *attrflagp = 0;
4567 NFSCL_REQSTART(nd, NFSPROC_COMMIT, vp, cred);
4568 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
4569 txdr_hyper(offset, tl);
4570 tl += 2;
4571 *tl = txdr_unsigned(cnt);
4572 if (nd->nd_flag & ND_NFSV4) {
4573 /*
4574 * And do a Getattr op.
4575 */
4576 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
4577 *tl = txdr_unsigned(NFSV4OP_GETATTR);
4578 NFSGETATTR_ATTRBIT(&attrbits);
4579 (void) nfsrv_putattrbit(nd, &attrbits);
4580 }
4581 error = nfscl_request(nd, vp, p, cred);
4582 if (error)
4583 return (error);
4584 error = nfscl_wcc_data(nd, vp, nap, attrflagp, NULL, NULL);
4585 if (!error && !nd->nd_repstat) {
4586 NFSM_DISSECT(tl, u_int32_t *, NFSX_VERF);
4587 NFSLOCKMNT(nmp);
4588 if (NFSBCMP(nmp->nm_verf, tl, NFSX_VERF)) {
4589 NFSBCOPY(tl, nmp->nm_verf, NFSX_VERF);
4590 nd->nd_repstat = NFSERR_STALEWRITEVERF;
4591 }
4592 NFSUNLOCKMNT(nmp);
4593 if (nd->nd_flag & ND_NFSV4)
4594 error = nfscl_postop_attr(nd, nap, attrflagp);
4595 }
4596 nfsmout:
4597 if (!error && nd->nd_repstat)
4598 error = nd->nd_repstat;
4599 m_freem(nd->nd_mrep);
4600 return (error);
4601 }
4602
4603 /*
4604 * NFS byte range lock rpc.
4605 * (Mostly just calls one of the three lower level RPC routines.)
4606 */
4607 int
nfsrpc_advlock(vnode_t vp,off_t size,int op,struct flock * fl,int reclaim,struct ucred * cred,NFSPROC_T * p,void * id,int flags)4608 nfsrpc_advlock(vnode_t vp, off_t size, int op, struct flock *fl,
4609 int reclaim, struct ucred *cred, NFSPROC_T *p, void *id, int flags)
4610 {
4611 struct nfscllockowner *lp;
4612 struct nfsclclient *clp;
4613 struct nfsfh *nfhp;
4614 struct nfsrv_descript nfsd, *nd = &nfsd;
4615 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
4616 u_int64_t off, len;
4617 off_t start, end;
4618 u_int32_t clidrev = 0;
4619 int error = 0, newone = 0, expireret = 0, retrycnt, donelocally;
4620 int callcnt, dorpc;
4621
4622 /*
4623 * Convert the flock structure into a start and end and do POSIX
4624 * bounds checking.
4625 */
4626 switch (fl->l_whence) {
4627 case SEEK_SET:
4628 case SEEK_CUR:
4629 /*
4630 * Caller is responsible for adding any necessary offset
4631 * when SEEK_CUR is used.
4632 */
4633 start = fl->l_start;
4634 off = fl->l_start;
4635 break;
4636 case SEEK_END:
4637 start = size + fl->l_start;
4638 off = size + fl->l_start;
4639 break;
4640 default:
4641 return (EINVAL);
4642 }
4643 if (start < 0)
4644 return (EINVAL);
4645 if (fl->l_len != 0) {
4646 end = start + fl->l_len - 1;
4647 if (end < start)
4648 return (EINVAL);
4649 }
4650
4651 len = fl->l_len;
4652 if (len == 0)
4653 len = NFS64BITSSET;
4654 retrycnt = 0;
4655 do {
4656 nd->nd_repstat = 0;
4657 if (op == F_GETLK) {
4658 error = nfscl_getcl(vp->v_mount, cred, p, false, true, &clp);
4659 if (error)
4660 return (error);
4661 error = nfscl_lockt(vp, clp, off, len, fl, p, id, flags);
4662 if (!error) {
4663 clidrev = clp->nfsc_clientidrev;
4664 error = nfsrpc_lockt(nd, vp, clp, off, len, fl, cred,
4665 p, id, flags);
4666 } else if (error == -1) {
4667 error = 0;
4668 }
4669 nfscl_clientrelease(clp);
4670 } else if (op == F_UNLCK && fl->l_type == F_UNLCK) {
4671 /*
4672 * We must loop around for all lockowner cases.
4673 */
4674 callcnt = 0;
4675 error = nfscl_getcl(vp->v_mount, cred, p, false, true, &clp);
4676 if (error)
4677 return (error);
4678 do {
4679 error = nfscl_relbytelock(vp, off, len, cred, p, callcnt,
4680 clp, id, flags, &lp, &dorpc);
4681 /*
4682 * If it returns a NULL lp, we're done.
4683 */
4684 if (lp == NULL) {
4685 if (callcnt == 0)
4686 nfscl_clientrelease(clp);
4687 else
4688 nfscl_releasealllocks(clp, vp, p, id, flags);
4689 return (error);
4690 }
4691 if (nmp->nm_clp != NULL)
4692 clidrev = nmp->nm_clp->nfsc_clientidrev;
4693 else
4694 clidrev = 0;
4695 /*
4696 * If the server doesn't support Posix lock semantics,
4697 * only allow locks on the entire file, since it won't
4698 * handle overlapping byte ranges.
4699 * There might still be a problem when a lock
4700 * upgrade/downgrade (read<->write) occurs, since the
4701 * server "might" expect an unlock first?
4702 */
4703 if (dorpc && (lp->nfsl_open->nfso_posixlock ||
4704 (off == 0 && len == NFS64BITSSET))) {
4705 /*
4706 * Since the lock records will go away, we must
4707 * wait for grace and delay here.
4708 */
4709 do {
4710 error = nfsrpc_locku(nd, nmp, lp, off, len,
4711 NFSV4LOCKT_READ, cred, p, 0);
4712 if ((nd->nd_repstat == NFSERR_GRACE ||
4713 nd->nd_repstat == NFSERR_DELAY) &&
4714 error == 0)
4715 (void) nfs_catnap(PZERO, (int)nd->nd_repstat,
4716 "nfs_advlock");
4717 } while ((nd->nd_repstat == NFSERR_GRACE ||
4718 nd->nd_repstat == NFSERR_DELAY) && error == 0);
4719 }
4720 callcnt++;
4721 } while (error == 0 && nd->nd_repstat == 0);
4722 nfscl_releasealllocks(clp, vp, p, id, flags);
4723 } else if (op == F_SETLK) {
4724 error = nfscl_getbytelock(vp, off, len, fl->l_type, cred, p,
4725 NULL, 0, id, flags, NULL, NULL, &lp, &newone, &donelocally);
4726 if (error || donelocally) {
4727 return (error);
4728 }
4729 if (nmp->nm_clp != NULL)
4730 clidrev = nmp->nm_clp->nfsc_clientidrev;
4731 else
4732 clidrev = 0;
4733 nfhp = VTONFS(vp)->n_fhp;
4734 if (!lp->nfsl_open->nfso_posixlock &&
4735 (off != 0 || len != NFS64BITSSET)) {
4736 error = EINVAL;
4737 } else {
4738 error = nfsrpc_lock(nd, nmp, vp, nfhp->nfh_fh,
4739 nfhp->nfh_len, lp, newone, reclaim, off,
4740 len, fl->l_type, cred, p, 0);
4741 }
4742 if (!error)
4743 error = nd->nd_repstat;
4744 nfscl_lockrelease(lp, error, newone);
4745 } else {
4746 error = EINVAL;
4747 }
4748 if (!error)
4749 error = nd->nd_repstat;
4750 if (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
4751 error == NFSERR_STALEDONTRECOVER ||
4752 error == NFSERR_STALECLIENTID || error == NFSERR_DELAY ||
4753 error == NFSERR_BADSESSION) {
4754 (void) nfs_catnap(PZERO, error, "nfs_advlock");
4755 } else if ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID)
4756 && clidrev != 0) {
4757 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
4758 retrycnt++;
4759 }
4760 } while (error == NFSERR_GRACE ||
4761 error == NFSERR_STALECLIENTID || error == NFSERR_DELAY ||
4762 error == NFSERR_STALEDONTRECOVER || error == NFSERR_STALESTATEID ||
4763 error == NFSERR_BADSESSION ||
4764 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
4765 expireret == 0 && clidrev != 0 && retrycnt < 4));
4766 if (error && retrycnt >= 4)
4767 error = EIO;
4768 return (error);
4769 }
4770
4771 /*
4772 * The lower level routine for the LockT case.
4773 */
4774 int
nfsrpc_lockt(struct nfsrv_descript * nd,vnode_t vp,struct nfsclclient * clp,u_int64_t off,u_int64_t len,struct flock * fl,struct ucred * cred,NFSPROC_T * p,void * id,int flags)4775 nfsrpc_lockt(struct nfsrv_descript *nd, vnode_t vp,
4776 struct nfsclclient *clp, u_int64_t off, u_int64_t len, struct flock *fl,
4777 struct ucred *cred, NFSPROC_T *p, void *id, int flags)
4778 {
4779 u_int32_t *tl;
4780 int error, type, size;
4781 uint8_t own[NFSV4CL_LOCKNAMELEN + NFSX_V4FHMAX];
4782 struct nfsnode *np;
4783 struct nfsmount *nmp;
4784 struct nfsclsession *tsep;
4785
4786 nmp = VFSTONFS(vp->v_mount);
4787 NFSCL_REQSTART(nd, NFSPROC_LOCKT, vp, cred);
4788 NFSM_BUILD(tl, u_int32_t *, 7 * NFSX_UNSIGNED);
4789 if (fl->l_type == F_RDLCK)
4790 *tl++ = txdr_unsigned(NFSV4LOCKT_READ);
4791 else
4792 *tl++ = txdr_unsigned(NFSV4LOCKT_WRITE);
4793 txdr_hyper(off, tl);
4794 tl += 2;
4795 txdr_hyper(len, tl);
4796 tl += 2;
4797 tsep = nfsmnt_mdssession(nmp);
4798 *tl++ = tsep->nfsess_clientid.lval[0];
4799 *tl = tsep->nfsess_clientid.lval[1];
4800 nfscl_filllockowner(id, own, flags);
4801 np = VTONFS(vp);
4802 NFSBCOPY(np->n_fhp->nfh_fh, &own[NFSV4CL_LOCKNAMELEN],
4803 np->n_fhp->nfh_len);
4804 (void)nfsm_strtom(nd, own, NFSV4CL_LOCKNAMELEN + np->n_fhp->nfh_len);
4805 error = nfscl_request(nd, vp, p, cred);
4806 if (error)
4807 return (error);
4808 if (nd->nd_repstat == 0) {
4809 fl->l_type = F_UNLCK;
4810 } else if (nd->nd_repstat == NFSERR_DENIED) {
4811 nd->nd_repstat = 0;
4812 fl->l_whence = SEEK_SET;
4813 NFSM_DISSECT(tl, u_int32_t *, 8 * NFSX_UNSIGNED);
4814 fl->l_start = fxdr_hyper(tl);
4815 tl += 2;
4816 len = fxdr_hyper(tl);
4817 tl += 2;
4818 if (len == NFS64BITSSET)
4819 fl->l_len = 0;
4820 else
4821 fl->l_len = len;
4822 type = fxdr_unsigned(int, *tl++);
4823 if (type == NFSV4LOCKT_WRITE)
4824 fl->l_type = F_WRLCK;
4825 else
4826 fl->l_type = F_RDLCK;
4827 /*
4828 * XXX For now, I have no idea what to do with the
4829 * conflicting lock_owner, so I'll just set the pid == 0
4830 * and skip over the lock_owner.
4831 */
4832 fl->l_pid = (pid_t)0;
4833 tl += 2;
4834 size = fxdr_unsigned(int, *tl);
4835 if (size < 0 || size > NFSV4_OPAQUELIMIT)
4836 error = EBADRPC;
4837 if (!error)
4838 error = nfsm_advance(nd, NFSM_RNDUP(size), -1);
4839 } else if (nd->nd_repstat == NFSERR_STALECLIENTID)
4840 nfscl_initiate_recovery(clp);
4841 nfsmout:
4842 m_freem(nd->nd_mrep);
4843 return (error);
4844 }
4845
4846 /*
4847 * Lower level function that performs the LockU RPC.
4848 */
4849 static int
nfsrpc_locku(struct nfsrv_descript * nd,struct nfsmount * nmp,struct nfscllockowner * lp,u_int64_t off,u_int64_t len,u_int32_t type,struct ucred * cred,NFSPROC_T * p,int syscred)4850 nfsrpc_locku(struct nfsrv_descript *nd, struct nfsmount *nmp,
4851 struct nfscllockowner *lp, u_int64_t off, u_int64_t len,
4852 u_int32_t type, struct ucred *cred, NFSPROC_T *p, int syscred)
4853 {
4854 u_int32_t *tl;
4855 int error;
4856
4857 nfscl_reqstart(nd, NFSPROC_LOCKU, nmp, lp->nfsl_open->nfso_fh,
4858 lp->nfsl_open->nfso_fhlen, NULL, NULL, 0, 0, cred);
4859 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID + 6 * NFSX_UNSIGNED);
4860 *tl++ = txdr_unsigned(type);
4861 *tl = txdr_unsigned(lp->nfsl_seqid);
4862 if (nfstest_outofseq &&
4863 (arc4random() % nfstest_outofseq) == 0)
4864 *tl = txdr_unsigned(lp->nfsl_seqid + 1);
4865 tl++;
4866 if (NFSHASNFSV4N(nmp))
4867 *tl++ = 0;
4868 else
4869 *tl++ = lp->nfsl_stateid.seqid;
4870 *tl++ = lp->nfsl_stateid.other[0];
4871 *tl++ = lp->nfsl_stateid.other[1];
4872 *tl++ = lp->nfsl_stateid.other[2];
4873 txdr_hyper(off, tl);
4874 tl += 2;
4875 txdr_hyper(len, tl);
4876 if (syscred)
4877 nd->nd_flag |= ND_USEGSSNAME;
4878 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4879 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4880 NFSCL_INCRSEQID(lp->nfsl_seqid, nd);
4881 if (error)
4882 return (error);
4883 if (nd->nd_repstat == 0) {
4884 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID);
4885 lp->nfsl_stateid.seqid = *tl++;
4886 lp->nfsl_stateid.other[0] = *tl++;
4887 lp->nfsl_stateid.other[1] = *tl++;
4888 lp->nfsl_stateid.other[2] = *tl;
4889 } else if (nd->nd_repstat == NFSERR_STALESTATEID)
4890 nfscl_initiate_recovery(lp->nfsl_open->nfso_own->nfsow_clp);
4891 nfsmout:
4892 m_freem(nd->nd_mrep);
4893 return (error);
4894 }
4895
4896 /*
4897 * The actual Lock RPC.
4898 */
4899 int
nfsrpc_lock(struct nfsrv_descript * nd,struct nfsmount * nmp,vnode_t vp,u_int8_t * nfhp,int fhlen,struct nfscllockowner * lp,int newone,int reclaim,u_int64_t off,u_int64_t len,short type,struct ucred * cred,NFSPROC_T * p,int syscred)4900 nfsrpc_lock(struct nfsrv_descript *nd, struct nfsmount *nmp, vnode_t vp,
4901 u_int8_t *nfhp, int fhlen, struct nfscllockowner *lp, int newone,
4902 int reclaim, u_int64_t off, u_int64_t len, short type, struct ucred *cred,
4903 NFSPROC_T *p, int syscred)
4904 {
4905 u_int32_t *tl;
4906 int error, size;
4907 uint8_t own[NFSV4CL_LOCKNAMELEN + NFSX_V4FHMAX];
4908 struct nfsclsession *tsep;
4909
4910 nfscl_reqstart(nd, NFSPROC_LOCK, nmp, nfhp, fhlen, NULL, NULL, 0, 0,
4911 cred);
4912 NFSM_BUILD(tl, u_int32_t *, 7 * NFSX_UNSIGNED);
4913 if (type == F_RDLCK)
4914 *tl++ = txdr_unsigned(NFSV4LOCKT_READ);
4915 else
4916 *tl++ = txdr_unsigned(NFSV4LOCKT_WRITE);
4917 *tl++ = txdr_unsigned(reclaim);
4918 txdr_hyper(off, tl);
4919 tl += 2;
4920 txdr_hyper(len, tl);
4921 tl += 2;
4922 if (newone) {
4923 *tl = newnfs_true;
4924 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID +
4925 2 * NFSX_UNSIGNED + NFSX_HYPER);
4926 *tl++ = txdr_unsigned(lp->nfsl_open->nfso_own->nfsow_seqid);
4927 if (NFSHASNFSV4N(nmp))
4928 *tl++ = 0;
4929 else
4930 *tl++ = lp->nfsl_open->nfso_stateid.seqid;
4931 *tl++ = lp->nfsl_open->nfso_stateid.other[0];
4932 *tl++ = lp->nfsl_open->nfso_stateid.other[1];
4933 *tl++ = lp->nfsl_open->nfso_stateid.other[2];
4934 *tl++ = txdr_unsigned(lp->nfsl_seqid);
4935 tsep = nfsmnt_mdssession(nmp);
4936 *tl++ = tsep->nfsess_clientid.lval[0];
4937 *tl = tsep->nfsess_clientid.lval[1];
4938 NFSBCOPY(lp->nfsl_owner, own, NFSV4CL_LOCKNAMELEN);
4939 NFSBCOPY(nfhp, &own[NFSV4CL_LOCKNAMELEN], fhlen);
4940 (void)nfsm_strtom(nd, own, NFSV4CL_LOCKNAMELEN + fhlen);
4941 } else {
4942 *tl = newnfs_false;
4943 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID + NFSX_UNSIGNED);
4944 if (NFSHASNFSV4N(nmp))
4945 *tl++ = 0;
4946 else
4947 *tl++ = lp->nfsl_stateid.seqid;
4948 *tl++ = lp->nfsl_stateid.other[0];
4949 *tl++ = lp->nfsl_stateid.other[1];
4950 *tl++ = lp->nfsl_stateid.other[2];
4951 *tl = txdr_unsigned(lp->nfsl_seqid);
4952 if (nfstest_outofseq &&
4953 (arc4random() % nfstest_outofseq) == 0)
4954 *tl = txdr_unsigned(lp->nfsl_seqid + 1);
4955 }
4956 if (syscred)
4957 nd->nd_flag |= ND_USEGSSNAME;
4958 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, vp, p, cred,
4959 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4960 if (error)
4961 return (error);
4962 if (newone)
4963 NFSCL_INCRSEQID(lp->nfsl_open->nfso_own->nfsow_seqid, nd);
4964 NFSCL_INCRSEQID(lp->nfsl_seqid, nd);
4965 if (nd->nd_repstat == 0) {
4966 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID);
4967 lp->nfsl_stateid.seqid = *tl++;
4968 lp->nfsl_stateid.other[0] = *tl++;
4969 lp->nfsl_stateid.other[1] = *tl++;
4970 lp->nfsl_stateid.other[2] = *tl;
4971 } else if (nd->nd_repstat == NFSERR_DENIED) {
4972 NFSM_DISSECT(tl, u_int32_t *, 8 * NFSX_UNSIGNED);
4973 size = fxdr_unsigned(int, *(tl + 7));
4974 if (size < 0 || size > NFSV4_OPAQUELIMIT)
4975 error = EBADRPC;
4976 if (!error)
4977 error = nfsm_advance(nd, NFSM_RNDUP(size), -1);
4978 } else if (nd->nd_repstat == NFSERR_STALESTATEID)
4979 nfscl_initiate_recovery(lp->nfsl_open->nfso_own->nfsow_clp);
4980 nfsmout:
4981 m_freem(nd->nd_mrep);
4982 return (error);
4983 }
4984
4985 /*
4986 * nfs statfs rpc
4987 * (always called with the vp for the mount point)
4988 */
4989 int
nfsrpc_statfs(vnode_t vp,struct nfsstatfs * sbp,struct nfsfsinfo * fsp,uint32_t * leasep,uint32_t * cloneblksizep,struct ucred * cred,NFSPROC_T * p,struct nfsvattr * nap,int * attrflagp)4990 nfsrpc_statfs(vnode_t vp, struct nfsstatfs *sbp, struct nfsfsinfo *fsp,
4991 uint32_t *leasep, uint32_t *cloneblksizep, struct ucred *cred, NFSPROC_T *p,
4992 struct nfsvattr *nap, int *attrflagp)
4993 {
4994 u_int32_t *tl = NULL;
4995 struct nfsrv_descript nfsd, *nd = &nfsd;
4996 struct nfsmount *nmp;
4997 nfsattrbit_t attrbits;
4998 int error;
4999
5000 *attrflagp = 0;
5001 if (cloneblksizep != NULL)
5002 *cloneblksizep = 0;
5003 nmp = VFSTONFS(vp->v_mount);
5004 if (NFSHASNFSV4(nmp)) {
5005 /*
5006 * For V4, you actually do a getattr.
5007 */
5008 NFSCL_REQSTART(nd, NFSPROC_GETATTR, vp, cred);
5009 if (leasep != NULL)
5010 NFSROOTFS_GETATTRBIT(&attrbits);
5011 else
5012 NFSSTATFS_GETATTRBIT(&attrbits);
5013 (void) nfsrv_putattrbit(nd, &attrbits);
5014 nd->nd_flag |= ND_USEGSSNAME;
5015 error = nfscl_request(nd, vp, p, cred);
5016 if (error)
5017 return (error);
5018 if (nd->nd_repstat == 0) {
5019 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0,
5020 NULL, NULL, sbp, fsp, NULL, 0, NULL, leasep, NULL,
5021 NULL, cloneblksizep, p, cred);
5022 if (!error) {
5023 nmp->nm_fsid[0] = nap->na_filesid[0];
5024 nmp->nm_fsid[1] = nap->na_filesid[1];
5025 NFSSETHASSETFSID(nmp);
5026 *attrflagp = 1;
5027 }
5028 } else {
5029 error = nd->nd_repstat;
5030 }
5031 if (error)
5032 goto nfsmout;
5033 } else {
5034 NFSCL_REQSTART(nd, NFSPROC_FSSTAT, vp, NULL);
5035 error = nfscl_request(nd, vp, p, cred);
5036 if (error)
5037 return (error);
5038 if (nd->nd_flag & ND_NFSV3) {
5039 error = nfscl_postop_attr(nd, nap, attrflagp);
5040 if (error)
5041 goto nfsmout;
5042 }
5043 if (nd->nd_repstat) {
5044 error = nd->nd_repstat;
5045 goto nfsmout;
5046 }
5047 NFSM_DISSECT(tl, u_int32_t *,
5048 NFSX_STATFS(nd->nd_flag & ND_NFSV3));
5049 }
5050 if (NFSHASNFSV3(nmp)) {
5051 sbp->sf_tbytes = fxdr_hyper(tl); tl += 2;
5052 sbp->sf_fbytes = fxdr_hyper(tl); tl += 2;
5053 sbp->sf_abytes = fxdr_hyper(tl); tl += 2;
5054 sbp->sf_tfiles = fxdr_hyper(tl); tl += 2;
5055 sbp->sf_ffiles = fxdr_hyper(tl); tl += 2;
5056 sbp->sf_afiles = fxdr_hyper(tl); tl += 2;
5057 sbp->sf_invarsec = fxdr_unsigned(u_int32_t, *tl);
5058 } else if (NFSHASNFSV4(nmp) == 0) {
5059 sbp->sf_tsize = fxdr_unsigned(u_int32_t, *tl++);
5060 sbp->sf_bsize = fxdr_unsigned(u_int32_t, *tl++);
5061 sbp->sf_blocks = fxdr_unsigned(u_int32_t, *tl++);
5062 sbp->sf_bfree = fxdr_unsigned(u_int32_t, *tl++);
5063 sbp->sf_bavail = fxdr_unsigned(u_int32_t, *tl);
5064 }
5065 nfsmout:
5066 m_freem(nd->nd_mrep);
5067 return (error);
5068 }
5069
5070 /*
5071 * nfs pathconf rpc
5072 */
5073 int
nfsrpc_pathconf(vnode_t vp,struct nfsv3_pathconf * pc,bool * has_namedattrp,uint32_t * clone_blksizep,struct ucred * cred,NFSPROC_T * p,struct nfsvattr * nap,int * attrflagp,uint32_t * trueformp)5074 nfsrpc_pathconf(vnode_t vp, struct nfsv3_pathconf *pc, bool *has_namedattrp,
5075 uint32_t *clone_blksizep, struct ucred *cred, NFSPROC_T *p,
5076 struct nfsvattr *nap, int *attrflagp, uint32_t *trueformp)
5077 {
5078 struct nfsrv_descript nfsd, *nd = &nfsd;
5079 struct nfsmount *nmp;
5080 u_int32_t *tl;
5081 nfsattrbit_t attrbits;
5082 int error;
5083 struct nfsnode *np;
5084
5085 *has_namedattrp = false;
5086 *attrflagp = 0;
5087 *clone_blksizep = 0;
5088 nmp = VFSTONFS(vp->v_mount);
5089 if (NFSHASNFSV4(nmp)) {
5090 np = VTONFS(vp);
5091 if ((nmp->nm_privflag & NFSMNTP_FAKEROOTFH) != 0 &&
5092 nmp->nm_fhsize == 0) {
5093 /* Attempt to get the actual root file handle. */
5094 error = nfsrpc_getdirpath(nmp, NFSMNT_DIRPATH(nmp),
5095 cred, p);
5096 if (error != 0)
5097 return (EACCES);
5098 if (np->n_fhp->nfh_len == NFSX_FHMAX + 1)
5099 nfscl_statfs(vp, cred, p);
5100 }
5101 /*
5102 * For V4, you actually do a getattr.
5103 */
5104 NFSCL_REQSTART(nd, NFSPROC_GETATTR, vp, cred);
5105 NFSPATHCONF_GETATTRBIT(&attrbits);
5106 if (nmp->nm_minorvers >= NFSV42_MINORVERSION)
5107 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_ACLTRUEFORM);
5108 (void) nfsrv_putattrbit(nd, &attrbits);
5109 nd->nd_flag |= ND_USEGSSNAME;
5110 error = nfscl_request(nd, vp, p, cred);
5111 if (error)
5112 return (error);
5113 if (nd->nd_repstat == 0) {
5114 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0,
5115 pc, NULL, NULL, NULL, NULL, 0, NULL, NULL, NULL,
5116 has_namedattrp, clone_blksizep, p, cred);
5117 if (!error)
5118 *attrflagp = 1;
5119 } else {
5120 error = nd->nd_repstat;
5121 }
5122 } else {
5123 NFSCL_REQSTART(nd, NFSPROC_PATHCONF, vp, NULL);
5124 error = nfscl_request(nd, vp, p, cred);
5125 if (error)
5126 return (error);
5127 error = nfscl_postop_attr(nd, nap, attrflagp);
5128 if (nd->nd_repstat && !error)
5129 error = nd->nd_repstat;
5130 if (!error) {
5131 NFSM_DISSECT(tl, u_int32_t *, NFSX_V3PATHCONF);
5132 pc->pc_linkmax = fxdr_unsigned(u_int32_t, *tl++);
5133 pc->pc_namemax = fxdr_unsigned(u_int32_t, *tl++);
5134 pc->pc_notrunc = fxdr_unsigned(u_int32_t, *tl++);
5135 pc->pc_chownrestricted =
5136 fxdr_unsigned(u_int32_t, *tl++);
5137 pc->pc_caseinsensitive =
5138 fxdr_unsigned(u_int32_t, *tl++);
5139 pc->pc_casepreserving = fxdr_unsigned(u_int32_t, *tl);
5140 }
5141 }
5142 nfsmout:
5143 m_freem(nd->nd_mrep);
5144 return (error);
5145 }
5146
5147 /*
5148 * nfs version 3 fsinfo rpc call
5149 */
5150 int
nfsrpc_fsinfo(vnode_t vp,struct nfsfsinfo * fsp,struct ucred * cred,NFSPROC_T * p,struct nfsvattr * nap,int * attrflagp)5151 nfsrpc_fsinfo(vnode_t vp, struct nfsfsinfo *fsp, struct ucred *cred,
5152 NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp)
5153 {
5154 u_int32_t *tl;
5155 struct nfsrv_descript nfsd, *nd = &nfsd;
5156 int error;
5157
5158 *attrflagp = 0;
5159 NFSCL_REQSTART(nd, NFSPROC_FSINFO, vp, NULL);
5160 error = nfscl_request(nd, vp, p, cred);
5161 if (error)
5162 return (error);
5163 error = nfscl_postop_attr(nd, nap, attrflagp);
5164 if (nd->nd_repstat && !error)
5165 error = nd->nd_repstat;
5166 if (!error) {
5167 NFSM_DISSECT(tl, u_int32_t *, NFSX_V3FSINFO);
5168 fsp->fs_rtmax = fxdr_unsigned(u_int32_t, *tl++);
5169 fsp->fs_rtpref = fxdr_unsigned(u_int32_t, *tl++);
5170 fsp->fs_rtmult = fxdr_unsigned(u_int32_t, *tl++);
5171 fsp->fs_wtmax = fxdr_unsigned(u_int32_t, *tl++);
5172 fsp->fs_wtpref = fxdr_unsigned(u_int32_t, *tl++);
5173 fsp->fs_wtmult = fxdr_unsigned(u_int32_t, *tl++);
5174 fsp->fs_dtpref = fxdr_unsigned(u_int32_t, *tl++);
5175 fsp->fs_maxfilesize = fxdr_hyper(tl);
5176 tl += 2;
5177 fxdr_nfsv3time(tl, &fsp->fs_timedelta);
5178 tl += 2;
5179 fsp->fs_properties = fxdr_unsigned(u_int32_t, *tl);
5180 }
5181 nfsmout:
5182 m_freem(nd->nd_mrep);
5183 return (error);
5184 }
5185
5186 /*
5187 * This function performs the Renew RPC.
5188 */
5189 int
nfsrpc_renew(struct nfsclclient * clp,struct nfsclds * dsp,struct ucred * cred,NFSPROC_T * p)5190 nfsrpc_renew(struct nfsclclient *clp, struct nfsclds *dsp, struct ucred *cred,
5191 NFSPROC_T *p)
5192 {
5193 u_int32_t *tl;
5194 struct nfsrv_descript nfsd;
5195 struct nfsrv_descript *nd = &nfsd;
5196 struct nfsmount *nmp;
5197 int error;
5198 struct nfssockreq *nrp;
5199 struct nfsclsession *tsep;
5200
5201 nmp = clp->nfsc_nmp;
5202 if (nmp == NULL)
5203 return (0);
5204 if (dsp == NULL)
5205 nfscl_reqstart(nd, NFSPROC_RENEW, nmp, NULL, 0, NULL, NULL, 0,
5206 0, cred);
5207 else
5208 nfscl_reqstart(nd, NFSPROC_RENEW, nmp, NULL, 0, NULL,
5209 &dsp->nfsclds_sess, 0, 0, NULL);
5210 if (!NFSHASNFSV4N(nmp)) {
5211 /* NFSv4.1 just uses a Sequence Op and not a Renew. */
5212 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
5213 tsep = nfsmnt_mdssession(nmp);
5214 *tl++ = tsep->nfsess_clientid.lval[0];
5215 *tl = tsep->nfsess_clientid.lval[1];
5216 }
5217 nrp = NULL;
5218 if (dsp != NULL)
5219 nrp = dsp->nfsclds_sockp;
5220 if (nrp == NULL)
5221 /* If NULL, use the MDS socket. */
5222 nrp = &nmp->nm_sockreq;
5223 nd->nd_flag |= ND_USEGSSNAME;
5224 if (dsp == NULL)
5225 error = newnfs_request(nd, nmp, NULL, nrp, NULL, p, cred,
5226 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
5227 else {
5228 error = newnfs_request(nd, nmp, NULL, nrp, NULL, p, cred,
5229 NFS_PROG, NFS_VER4, NULL, 1, NULL, &dsp->nfsclds_sess);
5230 if (error == ENXIO)
5231 nfscl_cancelreqs(dsp);
5232 }
5233 if (error)
5234 return (error);
5235 error = nd->nd_repstat;
5236 m_freem(nd->nd_mrep);
5237 return (error);
5238 }
5239
5240 /*
5241 * This function performs the Releaselockowner RPC.
5242 */
5243 int
nfsrpc_rellockown(struct nfsmount * nmp,struct nfscllockowner * lp,uint8_t * fh,int fhlen,struct ucred * cred,NFSPROC_T * p)5244 nfsrpc_rellockown(struct nfsmount *nmp, struct nfscllockowner *lp,
5245 uint8_t *fh, int fhlen, struct ucred *cred, NFSPROC_T *p)
5246 {
5247 struct nfsrv_descript nfsd, *nd = &nfsd;
5248 u_int32_t *tl;
5249 int error;
5250 uint8_t own[NFSV4CL_LOCKNAMELEN + NFSX_V4FHMAX];
5251 struct nfsclsession *tsep;
5252
5253 if (NFSHASNFSV4N(nmp)) {
5254 /* For NFSv4.1, do a FreeStateID. */
5255 nfscl_reqstart(nd, NFSPROC_FREESTATEID, nmp, NULL, 0, NULL,
5256 NULL, 0, 0, cred);
5257 nfsm_stateidtom(nd, &lp->nfsl_stateid, NFSSTATEID_PUTSTATEID);
5258 } else {
5259 nfscl_reqstart(nd, NFSPROC_RELEASELCKOWN, nmp, NULL, 0, NULL,
5260 NULL, 0, 0, NULL);
5261 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
5262 tsep = nfsmnt_mdssession(nmp);
5263 *tl++ = tsep->nfsess_clientid.lval[0];
5264 *tl = tsep->nfsess_clientid.lval[1];
5265 NFSBCOPY(lp->nfsl_owner, own, NFSV4CL_LOCKNAMELEN);
5266 NFSBCOPY(fh, &own[NFSV4CL_LOCKNAMELEN], fhlen);
5267 (void)nfsm_strtom(nd, own, NFSV4CL_LOCKNAMELEN + fhlen);
5268 }
5269 nd->nd_flag |= ND_USEGSSNAME;
5270 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
5271 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
5272 if (error)
5273 return (error);
5274 error = nd->nd_repstat;
5275 m_freem(nd->nd_mrep);
5276 return (error);
5277 }
5278
5279 /*
5280 * This function performs the Compound to get the mount pt FH.
5281 */
5282 int
nfsrpc_getdirpath(struct nfsmount * nmp,u_char * dirpath,struct ucred * cred,NFSPROC_T * p)5283 nfsrpc_getdirpath(struct nfsmount *nmp, u_char *dirpath, struct ucred *cred,
5284 NFSPROC_T *p)
5285 {
5286 u_int32_t *tl;
5287 struct nfsrv_descript nfsd;
5288 struct nfsrv_descript *nd = &nfsd;
5289 u_char *cp, *cp2, *fhp;
5290 int error, cnt, i, len, setnil;
5291 u_int32_t *opcntp;
5292
5293 nfscl_reqstart(nd, NFSPROC_PUTROOTFH, nmp, NULL, 0, &opcntp, NULL, 0,
5294 0, NULL);
5295 cp = dirpath;
5296 cnt = 0;
5297 do {
5298 setnil = 0;
5299 while (*cp == '/')
5300 cp++;
5301 cp2 = cp;
5302 while (*cp2 != '\0' && *cp2 != '/')
5303 cp2++;
5304 if (*cp2 == '/') {
5305 setnil = 1;
5306 *cp2 = '\0';
5307 }
5308 if (cp2 != cp) {
5309 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
5310 *tl = txdr_unsigned(NFSV4OP_LOOKUP);
5311 nfsm_strtom(nd, cp, strlen(cp));
5312 cnt++;
5313 }
5314 if (setnil)
5315 *cp2++ = '/';
5316 cp = cp2;
5317 } while (*cp != '\0');
5318 if (NFSHASNFSV4N(nmp))
5319 /* Has a Sequence Op done by nfscl_reqstart(). */
5320 *opcntp = txdr_unsigned(3 + cnt);
5321 else
5322 *opcntp = txdr_unsigned(2 + cnt);
5323 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
5324 *tl = txdr_unsigned(NFSV4OP_GETFH);
5325 nd->nd_flag |= ND_USEGSSNAME;
5326 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
5327 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
5328 if (error)
5329 return (error);
5330 if (nd->nd_repstat == 0) {
5331 NFSM_DISSECT(tl, uint32_t *, 3 * NFSX_UNSIGNED);
5332 tl += 2;
5333 for (i = 0; i < cnt; i++) {
5334 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
5335 tl++;
5336 }
5337 if ((len = fxdr_unsigned(int, *tl)) <= 0 ||
5338 len > NFSX_FHMAX) {
5339 nd->nd_repstat = NFSERR_BADXDR;
5340 } else {
5341 fhp = malloc(len + 1, M_TEMP, M_WAITOK);
5342 nd->nd_repstat = nfsrv_mtostr(nd, fhp, len);
5343 if (nd->nd_repstat == 0) {
5344 NFSLOCKMNT(nmp);
5345 if (nmp->nm_fhsize == 0) {
5346 NFSBCOPY(fhp, nmp->nm_fh, len);
5347 nmp->nm_fhsize = len;
5348 }
5349 NFSUNLOCKMNT(nmp);
5350 }
5351 free(fhp, M_TEMP);
5352 }
5353 }
5354 error = nd->nd_repstat;
5355 nfsmout:
5356 m_freem(nd->nd_mrep);
5357 return (error);
5358 }
5359
5360 /*
5361 * This function performs the Delegreturn RPC.
5362 */
5363 int
nfsrpc_delegreturn(struct nfscldeleg * dp,struct ucred * cred,struct nfsmount * nmp,NFSPROC_T * p,int syscred)5364 nfsrpc_delegreturn(struct nfscldeleg *dp, struct ucred *cred,
5365 struct nfsmount *nmp, NFSPROC_T *p, int syscred)
5366 {
5367 u_int32_t *tl;
5368 struct nfsrv_descript nfsd;
5369 struct nfsrv_descript *nd = &nfsd;
5370 int error;
5371
5372 nfscl_reqstart(nd, NFSPROC_DELEGRETURN, nmp, dp->nfsdl_fh,
5373 dp->nfsdl_fhlen, NULL, NULL, 0, 0, cred);
5374 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID);
5375 if (NFSHASNFSV4N(nmp))
5376 *tl++ = 0;
5377 else
5378 *tl++ = dp->nfsdl_stateid.seqid;
5379 *tl++ = dp->nfsdl_stateid.other[0];
5380 *tl++ = dp->nfsdl_stateid.other[1];
5381 *tl = dp->nfsdl_stateid.other[2];
5382 if (syscred)
5383 nd->nd_flag |= ND_USEGSSNAME;
5384 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
5385 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
5386 if (error)
5387 return (error);
5388 error = nd->nd_repstat;
5389 m_freem(nd->nd_mrep);
5390 return (error);
5391 }
5392
5393 /*
5394 * nfs getacl call.
5395 */
5396 int
nfsrpc_getacl(struct vnode * vp,acl_type_t acltype,struct ucred * cred,NFSPROC_T * p,struct acl * aclp)5397 nfsrpc_getacl(struct vnode *vp, acl_type_t acltype, struct ucred *cred,
5398 NFSPROC_T *p, struct acl *aclp)
5399 {
5400 struct nfsrv_descript nfsd, *nd = &nfsd;
5401 int error;
5402 nfsattrbit_t attrbits;
5403 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
5404 struct nfsnode *np;
5405
5406 if (nfsrv_useacl == 0 || !NFSHASNFSV4(nmp))
5407 return (EOPNOTSUPP);
5408 np = VTONFS(vp);
5409 if (!NFSISSET_ATTRBIT(&np->n_vattr.na_suppattr, NFSATTRBIT_ACL) &&
5410 acltype == ACL_TYPE_NFS4)
5411 return (EOPNOTSUPP);
5412 if ((!NFSISSET_ATTRBIT(&np->n_vattr.na_suppattr,
5413 NFSATTRBIT_POSIXACCESSACL) ||
5414 !NFSISSET_ATTRBIT(&np->n_vattr.na_suppattr,
5415 NFSATTRBIT_POSIXDEFAULTACL)) &&
5416 (acltype == ACL_TYPE_ACCESS || acltype == ACL_TYPE_DEFAULT))
5417 return (EOPNOTSUPP);
5418 NFSCL_REQSTART(nd, NFSPROC_GETACL, vp, cred);
5419 NFSZERO_ATTRBIT(&attrbits);
5420 if (acltype == ACL_TYPE_NFS4)
5421 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_ACL);
5422 else if (acltype == ACL_TYPE_ACCESS)
5423 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_POSIXACCESSACL);
5424 else
5425 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_POSIXDEFAULTACL);
5426 (void) nfsrv_putattrbit(nd, &attrbits);
5427 error = nfscl_request(nd, vp, p, cred);
5428 if (error)
5429 return (error);
5430 if (!nd->nd_repstat)
5431 error = nfsv4_loadattr(nd, vp, NULL, NULL, NULL, 0, NULL,
5432 NULL, NULL, NULL, aclp, 0, NULL, NULL, NULL, NULL, NULL,
5433 p, cred);
5434 else
5435 error = nd->nd_repstat;
5436 m_freem(nd->nd_mrep);
5437 return (error);
5438 }
5439
5440 /*
5441 * nfs setacl call.
5442 */
5443 int
nfsrpc_setacl(struct vnode * vp,acl_type_t acltype,struct ucred * cred,NFSPROC_T * p,struct acl * aclp)5444 nfsrpc_setacl(struct vnode *vp, acl_type_t acltype, struct ucred *cred,
5445 NFSPROC_T *p, struct acl *aclp)
5446 {
5447 int error;
5448 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
5449 struct nfsnode *np;
5450
5451 if (nfsrv_useacl == 0 || !NFSHASNFSV4(nmp))
5452 return (EOPNOTSUPP);
5453 np = VTONFS(vp);
5454 if (!NFSISSET_ATTRBIT(&np->n_vattr.na_suppattr, NFSATTRBIT_ACL) &&
5455 acltype == ACL_TYPE_NFS4)
5456 return (EOPNOTSUPP);
5457 if ((!NFSISSET_ATTRBIT(&np->n_vattr.na_suppattr,
5458 NFSATTRBIT_POSIXACCESSACL) ||
5459 !NFSISSET_ATTRBIT(&np->n_vattr.na_suppattr,
5460 NFSATTRBIT_POSIXDEFAULTACL)) &&
5461 (acltype == ACL_TYPE_ACCESS || acltype == ACL_TYPE_DEFAULT))
5462 return (EOPNOTSUPP);
5463 error = nfsrpc_setattr(vp, NULL, aclp, acltype, cred, p, NULL, NULL);
5464 return (error);
5465 }
5466
5467 /*
5468 * nfs setacl call.
5469 */
5470 static int
nfsrpc_setaclrpc(struct vnode * vp,struct ucred * cred,NFSPROC_T * p,struct acl * aclp,acl_type_t aclt,nfsv4stateid_t * stateidp)5471 nfsrpc_setaclrpc(struct vnode *vp, struct ucred *cred, NFSPROC_T *p,
5472 struct acl *aclp, acl_type_t aclt, nfsv4stateid_t *stateidp)
5473 {
5474 struct nfsrv_descript nfsd, *nd = &nfsd;
5475 int error;
5476 nfsattrbit_t attrbits;
5477 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
5478
5479 if (!NFSHASNFSV4(nmp))
5480 return (EOPNOTSUPP);
5481 NFSCL_REQSTART(nd, NFSPROC_SETACL, vp, cred);
5482 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID);
5483 NFSZERO_ATTRBIT(&attrbits);
5484 if (aclt == ACL_TYPE_NFS4)
5485 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_ACL);
5486 else if (aclt == ACL_TYPE_ACCESS)
5487 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_POSIXACCESSACL);
5488 else
5489 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_POSIXDEFAULTACL);
5490 (void)nfsv4_fillattr(nd, vp->v_mount, vp, aclp, NULL, NULL, 0,
5491 &attrbits, NULL, NULL, 0, 0, 0, 0, (uint64_t)0, NULL, false,
5492 false, false, 0, NULL, false);
5493 error = nfscl_request(nd, vp, p, cred);
5494 if (error)
5495 return (error);
5496 /* Don't care about the pre/postop attributes */
5497 m_freem(nd->nd_mrep);
5498 return (nd->nd_repstat);
5499 }
5500
5501 /*
5502 * Do the NFSv4.1 Exchange ID.
5503 */
5504 int
nfsrpc_exchangeid(struct nfsmount * nmp,struct nfsclclient * clp,struct nfssockreq * nrp,int minorvers,uint32_t exchflags,struct nfsclds ** dspp,struct ucred * cred,NFSPROC_T * p)5505 nfsrpc_exchangeid(struct nfsmount *nmp, struct nfsclclient *clp,
5506 struct nfssockreq *nrp, int minorvers, uint32_t exchflags,
5507 struct nfsclds **dspp, struct ucred *cred, NFSPROC_T *p)
5508 {
5509 uint32_t *tl, v41flags;
5510 struct nfsrv_descript nfsd;
5511 struct nfsrv_descript *nd = &nfsd;
5512 struct nfsclds *dsp;
5513 struct timespec verstime;
5514 int error, len;
5515
5516 *dspp = NULL;
5517 if (minorvers == 0)
5518 minorvers = nmp->nm_minorvers;
5519 nfscl_reqstart(nd, NFSPROC_EXCHANGEID, nmp, NULL, 0, NULL, NULL,
5520 NFS_VER4, minorvers, NULL);
5521 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_UNSIGNED);
5522 *tl++ = txdr_unsigned(nfsboottime.tv_sec); /* Client owner */
5523 *tl = txdr_unsigned(clp->nfsc_rev);
5524 (void) nfsm_strtom(nd, clp->nfsc_id, clp->nfsc_idlen);
5525
5526 NFSM_BUILD(tl, uint32_t *, 3 * NFSX_UNSIGNED);
5527 *tl++ = txdr_unsigned(exchflags);
5528 *tl++ = txdr_unsigned(NFSV4EXCH_SP4NONE);
5529
5530 /* Set the implementation id4 */
5531 *tl = txdr_unsigned(1);
5532 (void) nfsm_strtom(nd, "freebsd.org", strlen("freebsd.org"));
5533 (void) nfsm_strtom(nd, version, strlen(version));
5534 NFSM_BUILD(tl, uint32_t *, NFSX_V4TIME);
5535 verstime.tv_sec = 1293840000; /* Jan 1, 2011 */
5536 verstime.tv_nsec = 0;
5537 txdr_nfsv4time(&verstime, tl);
5538 nd->nd_flag |= ND_USEGSSNAME;
5539 error = newnfs_request(nd, nmp, NULL, nrp, NULL, p, cred,
5540 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
5541 NFSCL_DEBUG(1, "exchangeid err=%d reps=%d\n", error,
5542 (int)nd->nd_repstat);
5543 if (error != 0)
5544 return (error);
5545 if (nd->nd_repstat == 0) {
5546 NFSM_DISSECT(tl, uint32_t *, 6 * NFSX_UNSIGNED + NFSX_HYPER);
5547 len = fxdr_unsigned(int, *(tl + 7));
5548 if (len < 0 || len > NFSV4_OPAQUELIMIT) {
5549 error = NFSERR_BADXDR;
5550 goto nfsmout;
5551 }
5552 dsp = malloc(sizeof(struct nfsclds) + len + 1, M_NFSCLDS,
5553 M_WAITOK | M_ZERO);
5554 dsp->nfsclds_expire = NFSD_MONOSEC + clp->nfsc_renew;
5555 dsp->nfsclds_servownlen = len;
5556 dsp->nfsclds_sess.nfsess_clientid.lval[0] = *tl++;
5557 dsp->nfsclds_sess.nfsess_clientid.lval[1] = *tl++;
5558 dsp->nfsclds_sess.nfsess_sequenceid =
5559 fxdr_unsigned(uint32_t, *tl++);
5560 v41flags = fxdr_unsigned(uint32_t, *tl);
5561 if ((v41flags & NFSV4EXCH_USEPNFSMDS) != 0 &&
5562 NFSHASPNFSOPT(nmp)) {
5563 NFSCL_DEBUG(1, "set PNFS\n");
5564 NFSLOCKMNT(nmp);
5565 nmp->nm_state |= NFSSTA_PNFS;
5566 NFSUNLOCKMNT(nmp);
5567 dsp->nfsclds_flags |= NFSCLDS_MDS;
5568 }
5569 if ((v41flags & NFSV4EXCH_USEPNFSDS) != 0)
5570 dsp->nfsclds_flags |= NFSCLDS_DS;
5571 if (minorvers == NFSV42_MINORVERSION)
5572 dsp->nfsclds_flags |= NFSCLDS_MINORV2;
5573 if (len > 0)
5574 nd->nd_repstat = nfsrv_mtostr(nd,
5575 dsp->nfsclds_serverown, len);
5576 if (nd->nd_repstat == 0) {
5577 mtx_init(&dsp->nfsclds_mtx, "nfsds", NULL, MTX_DEF);
5578 mtx_init(&dsp->nfsclds_sess.nfsess_mtx, "nfssession",
5579 NULL, MTX_DEF);
5580 nfscl_initsessionslots(&dsp->nfsclds_sess);
5581 *dspp = dsp;
5582 } else
5583 free(dsp, M_NFSCLDS);
5584 }
5585 error = nd->nd_repstat;
5586 nfsmout:
5587 m_freem(nd->nd_mrep);
5588 return (error);
5589 }
5590
5591 /*
5592 * Do the NFSv4.1 Create Session.
5593 */
5594 int
nfsrpc_createsession(struct nfsmount * nmp,struct nfsclsession * sep,struct nfssockreq * nrp,struct nfsclds * dsp,uint32_t sequenceid,int mds,struct ucred * cred,NFSPROC_T * p)5595 nfsrpc_createsession(struct nfsmount *nmp, struct nfsclsession *sep,
5596 struct nfssockreq *nrp, struct nfsclds *dsp, uint32_t sequenceid, int mds,
5597 struct ucred *cred, NFSPROC_T *p)
5598 {
5599 uint32_t crflags, maxval, *tl;
5600 struct nfsrv_descript nfsd;
5601 struct nfsrv_descript *nd = &nfsd;
5602 int error, irdcnt, minorvers;
5603
5604 /* Make sure nm_rsize, nm_wsize is set. */
5605 if (nmp->nm_rsize > NFS_MAXBSIZE || nmp->nm_rsize == 0)
5606 nmp->nm_rsize = NFS_MAXBSIZE;
5607 if (nmp->nm_wsize > NFS_MAXBSIZE || nmp->nm_wsize == 0)
5608 nmp->nm_wsize = NFS_MAXBSIZE;
5609 if (dsp == NULL)
5610 minorvers = nmp->nm_minorvers;
5611 else if ((dsp->nfsclds_flags & NFSCLDS_MINORV2) != 0)
5612 minorvers = NFSV42_MINORVERSION;
5613 else
5614 minorvers = NFSV41_MINORVERSION;
5615 nfscl_reqstart(nd, NFSPROC_CREATESESSION, nmp, NULL, 0, NULL, NULL,
5616 NFS_VER4, minorvers, NULL);
5617 NFSM_BUILD(tl, uint32_t *, 4 * NFSX_UNSIGNED);
5618 *tl++ = sep->nfsess_clientid.lval[0];
5619 *tl++ = sep->nfsess_clientid.lval[1];
5620 *tl++ = txdr_unsigned(sequenceid);
5621 crflags = (NFSMNT_RDONLY(nmp->nm_mountp) ? 0 : NFSV4CRSESS_PERSIST);
5622 if (nfscl_enablecallb != 0 && nfs_numnfscbd > 0 && mds != 0)
5623 crflags |= NFSV4CRSESS_CONNBACKCHAN;
5624 *tl = txdr_unsigned(crflags);
5625
5626 /* Fill in fore channel attributes. */
5627 NFSM_BUILD(tl, uint32_t *, 7 * NFSX_UNSIGNED);
5628 *tl++ = 0; /* Header pad size */
5629 if ((nd->nd_flag & ND_NFSV42) != 0 && mds != 0 && sb_max_adj >=
5630 nmp->nm_wsize && sb_max_adj >= nmp->nm_rsize) {
5631 /*
5632 * NFSv4.2 Extended Attribute operations may want to do
5633 * requests/replies that are larger than nm_rsize/nm_wsize.
5634 */
5635 *tl++ = txdr_unsigned(sb_max_adj - NFS_MAXXDR);
5636 *tl++ = txdr_unsigned(sb_max_adj - NFS_MAXXDR);
5637 } else {
5638 *tl++ = txdr_unsigned(nmp->nm_wsize + NFS_MAXXDR);
5639 *tl++ = txdr_unsigned(nmp->nm_rsize + NFS_MAXXDR);
5640 }
5641 *tl++ = txdr_unsigned(4096); /* Max response size cached */
5642 *tl++ = txdr_unsigned(20); /* Max operations */
5643 *tl++ = txdr_unsigned(NFSV4_SLOTS); /* Max slots */
5644 *tl = 0; /* No rdma ird */
5645
5646 /* Fill in back channel attributes. */
5647 NFSM_BUILD(tl, uint32_t *, 7 * NFSX_UNSIGNED);
5648 *tl++ = 0; /* Header pad size */
5649 *tl++ = txdr_unsigned(10000); /* Max request size */
5650 *tl++ = txdr_unsigned(10000); /* Max response size */
5651 *tl++ = txdr_unsigned(4096); /* Max response size cached */
5652 *tl++ = txdr_unsigned(4); /* Max operations */
5653 *tl++ = txdr_unsigned(NFSV4_CBSLOTS); /* Max slots */
5654 *tl = 0; /* No rdma ird */
5655
5656 NFSM_BUILD(tl, uint32_t *, 8 * NFSX_UNSIGNED);
5657 *tl++ = txdr_unsigned(NFS_CALLBCKPROG); /* Call back prog # */
5658
5659 /* Allow AUTH_SYS callbacks as uid, gid == 0. */
5660 *tl++ = txdr_unsigned(1); /* Auth_sys only */
5661 *tl++ = txdr_unsigned(AUTH_SYS); /* AUTH_SYS type */
5662 *tl++ = txdr_unsigned(nfsboottime.tv_sec); /* time stamp */
5663 *tl++ = 0; /* Null machine name */
5664 *tl++ = 0; /* Uid == 0 */
5665 *tl++ = 0; /* Gid == 0 */
5666 *tl = 0; /* No additional gids */
5667 nd->nd_flag |= ND_USEGSSNAME;
5668 error = newnfs_request(nd, nmp, NULL, nrp, NULL, p, cred, NFS_PROG,
5669 NFS_VER4, NULL, 1, NULL, NULL);
5670 if (error != 0)
5671 return (error);
5672 if (nd->nd_repstat == 0) {
5673 NFSM_DISSECT(tl, uint32_t *, NFSX_V4SESSIONID +
5674 2 * NFSX_UNSIGNED);
5675 bcopy(tl, sep->nfsess_sessionid, NFSX_V4SESSIONID);
5676 tl += NFSX_V4SESSIONID / NFSX_UNSIGNED;
5677 sep->nfsess_sequenceid = fxdr_unsigned(uint32_t, *tl++);
5678 crflags = fxdr_unsigned(uint32_t, *tl);
5679 if ((crflags & NFSV4CRSESS_PERSIST) != 0 && mds != 0) {
5680 NFSLOCKMNT(nmp);
5681 nmp->nm_state |= NFSSTA_SESSPERSIST;
5682 NFSUNLOCKMNT(nmp);
5683 }
5684
5685 /* Get the fore channel slot count. */
5686 NFSM_DISSECT(tl, uint32_t *, 7 * NFSX_UNSIGNED);
5687 tl++; /* Skip the header pad size. */
5688
5689 /* Make sure nm_wsize is small enough. */
5690 maxval = fxdr_unsigned(uint32_t, *tl++);
5691 while (maxval < nmp->nm_wsize + NFS_MAXXDR) {
5692 if (nmp->nm_wsize > 8096)
5693 nmp->nm_wsize /= 2;
5694 else
5695 break;
5696 }
5697 sep->nfsess_maxreq = maxval;
5698
5699 /* Make sure nm_rsize is small enough. */
5700 maxval = fxdr_unsigned(uint32_t, *tl++);
5701 while (maxval < nmp->nm_rsize + NFS_MAXXDR) {
5702 if (nmp->nm_rsize > 8096)
5703 nmp->nm_rsize /= 2;
5704 else
5705 break;
5706 }
5707 sep->nfsess_maxresp = maxval;
5708
5709 sep->nfsess_maxcache = fxdr_unsigned(int, *tl++);
5710 tl++;
5711 sep->nfsess_foreslots = fxdr_unsigned(uint16_t, *tl++);
5712 if (sep->nfsess_foreslots == 0) {
5713 error = NFSERR_BADXDR;
5714 goto nfsmout;
5715 } else if (sep->nfsess_foreslots > NFSV4_SLOTS)
5716 sep->nfsess_foreslots = NFSV4_SLOTS;
5717 NFSCL_DEBUG(4, "fore slots=%d\n", (int)sep->nfsess_foreslots);
5718 irdcnt = fxdr_unsigned(int, *tl);
5719 if (irdcnt < 0 || irdcnt > 1) {
5720 error = NFSERR_BADXDR;
5721 goto nfsmout;
5722 }
5723 if (irdcnt > 0)
5724 NFSM_DISSECT(tl, uint32_t *, irdcnt * NFSX_UNSIGNED);
5725
5726 /* and the back channel slot count. */
5727 NFSM_DISSECT(tl, uint32_t *, 7 * NFSX_UNSIGNED);
5728 tl += 5;
5729 sep->nfsess_backslots = fxdr_unsigned(uint16_t, *tl);
5730 if (sep->nfsess_backslots > NFSV4_CBSLOTS)
5731 sep->nfsess_backslots = NFSV4_CBSLOTS;
5732 NFSCL_DEBUG(4, "back slots=%d\n", (int)sep->nfsess_backslots);
5733 }
5734 error = nd->nd_repstat;
5735 nfsmout:
5736 m_freem(nd->nd_mrep);
5737 return (error);
5738 }
5739
5740 /*
5741 * Do the NFSv4.1 Destroy Client.
5742 */
5743 int
nfsrpc_destroyclient(struct nfsmount * nmp,struct nfsclclient * clp,struct ucred * cred,NFSPROC_T * p)5744 nfsrpc_destroyclient(struct nfsmount *nmp, struct nfsclclient *clp,
5745 struct ucred *cred, NFSPROC_T *p)
5746 {
5747 uint32_t *tl;
5748 struct nfsrv_descript nfsd;
5749 struct nfsrv_descript *nd = &nfsd;
5750 int error;
5751 struct nfsclsession *tsep;
5752
5753 nfscl_reqstart(nd, NFSPROC_DESTROYCLIENT, nmp, NULL, 0, NULL, NULL, 0,
5754 0, NULL);
5755 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_UNSIGNED);
5756 tsep = nfsmnt_mdssession(nmp);
5757 *tl++ = tsep->nfsess_clientid.lval[0];
5758 *tl = tsep->nfsess_clientid.lval[1];
5759 nd->nd_flag |= ND_USEGSSNAME;
5760 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
5761 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
5762 if (error != 0)
5763 return (error);
5764 error = nd->nd_repstat;
5765 m_freem(nd->nd_mrep);
5766 return (error);
5767 }
5768
5769 /*
5770 * Do the NFSv4.1 LayoutGet.
5771 */
5772 static int
nfsrpc_layoutget(struct nfsmount * nmp,uint8_t * fhp,int fhlen,int iomode,uint64_t offset,uint64_t len,uint64_t minlen,int layouttype,int layoutlen,nfsv4stateid_t * stateidp,int * retonclosep,struct nfsclflayouthead * flhp,struct ucred * cred,NFSPROC_T * p)5773 nfsrpc_layoutget(struct nfsmount *nmp, uint8_t *fhp, int fhlen, int iomode,
5774 uint64_t offset, uint64_t len, uint64_t minlen, int layouttype,
5775 int layoutlen, nfsv4stateid_t *stateidp, int *retonclosep,
5776 struct nfsclflayouthead *flhp, struct ucred *cred, NFSPROC_T *p)
5777 {
5778 struct nfsrv_descript nfsd, *nd = &nfsd;
5779 int error;
5780
5781 nfscl_reqstart(nd, NFSPROC_LAYOUTGET, nmp, fhp, fhlen, NULL, NULL, 0,
5782 0, cred);
5783 nfsrv_setuplayoutget(nd, iomode, offset, len, minlen, stateidp,
5784 layouttype, layoutlen, 0);
5785 nd->nd_flag |= ND_USEGSSNAME;
5786 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
5787 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
5788 NFSCL_DEBUG(4, "layget err=%d st=%d\n", error, nd->nd_repstat);
5789 if (error != 0)
5790 return (error);
5791 if (nd->nd_repstat == 0)
5792 error = nfsrv_parselayoutget(nmp, nd, stateidp, retonclosep,
5793 flhp);
5794 if (error == 0 && nd->nd_repstat != 0)
5795 error = nd->nd_repstat;
5796 m_freem(nd->nd_mrep);
5797 return (error);
5798 }
5799
5800 /*
5801 * Do the NFSv4.1 Get Device Info.
5802 */
5803 int
nfsrpc_getdeviceinfo(struct nfsmount * nmp,uint8_t * deviceid,int layouttype,uint32_t * notifybitsp,struct nfscldevinfo ** ndip,struct ucred * cred,NFSPROC_T * p)5804 nfsrpc_getdeviceinfo(struct nfsmount *nmp, uint8_t *deviceid, int layouttype,
5805 uint32_t *notifybitsp, struct nfscldevinfo **ndip, struct ucred *cred,
5806 NFSPROC_T *p)
5807 {
5808 uint32_t cnt, *tl, vers, minorvers;
5809 struct nfsrv_descript nfsd;
5810 struct nfsrv_descript *nd = &nfsd;
5811 struct sockaddr_in sin, ssin;
5812 struct sockaddr_in6 sin6, ssin6;
5813 struct nfsclds *dsp = NULL, **dspp, **gotdspp;
5814 struct nfscldevinfo *ndi;
5815 int addrcnt = 0, bitcnt, error, gotminor, gotvers, i, isudp, j;
5816 int stripecnt;
5817 uint8_t stripeindex;
5818 sa_family_t af, safilled;
5819
5820 ssin.sin_port = 0; /* To shut up compiler. */
5821 ssin.sin_addr.s_addr = 0; /* ditto */
5822 *ndip = NULL;
5823 ndi = NULL;
5824 gotdspp = NULL;
5825 nfscl_reqstart(nd, NFSPROC_GETDEVICEINFO, nmp, NULL, 0, NULL, NULL, 0,
5826 0, cred);
5827 NFSM_BUILD(tl, uint32_t *, NFSX_V4DEVICEID + 3 * NFSX_UNSIGNED);
5828 NFSBCOPY(deviceid, tl, NFSX_V4DEVICEID);
5829 tl += (NFSX_V4DEVICEID / NFSX_UNSIGNED);
5830 *tl++ = txdr_unsigned(layouttype);
5831 *tl++ = txdr_unsigned(100000);
5832 if (notifybitsp != NULL && *notifybitsp != 0) {
5833 *tl = txdr_unsigned(1); /* One word of bits. */
5834 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
5835 *tl = txdr_unsigned(*notifybitsp);
5836 } else
5837 *tl = txdr_unsigned(0);
5838 nd->nd_flag |= ND_USEGSSNAME;
5839 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
5840 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
5841 if (error != 0)
5842 return (error);
5843 if (nd->nd_repstat == 0) {
5844 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
5845 if (layouttype != fxdr_unsigned(int, *tl))
5846 printf("EEK! devinfo layout type not same!\n");
5847 if (layouttype == NFSLAYOUT_NFSV4_1_FILES) {
5848 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
5849 stripecnt = fxdr_unsigned(int, *tl);
5850 NFSCL_DEBUG(4, "stripecnt=%d\n", stripecnt);
5851 if (stripecnt >= MHLEN / NFSX_UNSIGNED ||
5852 stripecnt < 1) {
5853 printf("pNFS File layout devinfo stripecnt %d:"
5854 " out of range\n", stripecnt);
5855 error = NFSERR_BADXDR;
5856 goto nfsmout;
5857 }
5858 NFSM_DISSECT(tl, uint32_t *, (stripecnt + 1) *
5859 NFSX_UNSIGNED);
5860 addrcnt = fxdr_unsigned(int, *(tl + stripecnt));
5861 NFSCL_DEBUG(4, "addrcnt=%d\n", addrcnt);
5862 if (addrcnt < 1 || addrcnt > 128) {
5863 printf("NFS devinfo addrcnt %d: out of range\n",
5864 addrcnt);
5865 error = NFSERR_BADXDR;
5866 goto nfsmout;
5867 }
5868
5869 /*
5870 * Now we know how many stripe indices and addresses, so
5871 * we can allocate the structure the correct size.
5872 */
5873 i = (stripecnt * sizeof(uint8_t)) /
5874 sizeof(struct nfsclds *) + 1;
5875 NFSCL_DEBUG(4, "stripeindices=%d\n", i);
5876 ndi = malloc(sizeof(*ndi) + (addrcnt + i) *
5877 sizeof(struct nfsclds *), M_NFSDEVINFO, M_WAITOK |
5878 M_ZERO);
5879 NFSBCOPY(deviceid, ndi->nfsdi_deviceid,
5880 NFSX_V4DEVICEID);
5881 ndi->nfsdi_refcnt = 0;
5882 ndi->nfsdi_flags = NFSDI_FILELAYOUT;
5883 ndi->nfsdi_stripecnt = stripecnt;
5884 ndi->nfsdi_addrcnt = addrcnt;
5885 /* Fill in the stripe indices. */
5886 for (i = 0; i < stripecnt; i++) {
5887 stripeindex = fxdr_unsigned(uint8_t, *tl++);
5888 NFSCL_DEBUG(4, "stripeind=%d\n", stripeindex);
5889 if (stripeindex >= addrcnt) {
5890 printf("pNFS File Layout devinfo"
5891 " stripeindex %d: too big\n",
5892 (int)stripeindex);
5893 error = NFSERR_BADXDR;
5894 goto nfsmout;
5895 }
5896 nfsfldi_setstripeindex(ndi, i, stripeindex);
5897 }
5898 } else if (layouttype == NFSLAYOUT_FLEXFILE) {
5899 /* For Flex File, we only get one address list. */
5900 ndi = malloc(sizeof(*ndi) + sizeof(struct nfsclds *),
5901 M_NFSDEVINFO, M_WAITOK | M_ZERO);
5902 NFSBCOPY(deviceid, ndi->nfsdi_deviceid,
5903 NFSX_V4DEVICEID);
5904 ndi->nfsdi_refcnt = 0;
5905 ndi->nfsdi_flags = NFSDI_FLEXFILE;
5906 addrcnt = ndi->nfsdi_addrcnt = 1;
5907 }
5908
5909 /* Now, dissect the server address(es). */
5910 safilled = AF_UNSPEC;
5911 for (i = 0; i < addrcnt; i++) {
5912 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
5913 cnt = fxdr_unsigned(uint32_t, *tl);
5914 if (cnt == 0) {
5915 printf("NFS devinfo 0 len addrlist\n");
5916 error = NFSERR_BADXDR;
5917 goto nfsmout;
5918 }
5919 dspp = nfsfldi_addr(ndi, i);
5920 safilled = AF_UNSPEC;
5921 for (j = 0; j < cnt; j++) {
5922 error = nfsv4_getipaddr(nd, &sin, &sin6, &af,
5923 &isudp);
5924 if (error != 0 && error != EPERM) {
5925 error = NFSERR_BADXDR;
5926 goto nfsmout;
5927 }
5928 if (error == 0 && isudp == 0) {
5929 /*
5930 * The priority is:
5931 * - Same address family.
5932 * Save the address and dspp, so that
5933 * the connection can be done after
5934 * parsing is complete.
5935 */
5936 if (safilled == AF_UNSPEC ||
5937 (af == nmp->nm_nam->sa_family &&
5938 safilled != nmp->nm_nam->sa_family)
5939 ) {
5940 if (af == AF_INET)
5941 ssin = sin;
5942 else
5943 ssin6 = sin6;
5944 safilled = af;
5945 gotdspp = dspp;
5946 }
5947 }
5948 }
5949 }
5950
5951 gotvers = NFS_VER4; /* Default NFSv4.1 for File Layout. */
5952 gotminor = NFSV41_MINORVERSION;
5953 /* For Flex File, we will take one of the versions to use. */
5954 if (layouttype == NFSLAYOUT_FLEXFILE) {
5955 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
5956 j = fxdr_unsigned(int, *tl);
5957 if (j < 1 || j > NFSDEV_MAXVERS) {
5958 printf("pNFS: too many versions\n");
5959 error = NFSERR_BADXDR;
5960 goto nfsmout;
5961 }
5962 gotvers = 0;
5963 gotminor = 0;
5964 for (i = 0; i < j; i++) {
5965 NFSM_DISSECT(tl, uint32_t *, 5 * NFSX_UNSIGNED);
5966 vers = fxdr_unsigned(uint32_t, *tl++);
5967 minorvers = fxdr_unsigned(uint32_t, *tl++);
5968 if (vers == NFS_VER3)
5969 minorvers = 0;
5970 if ((vers == NFS_VER4 && ((minorvers ==
5971 NFSV41_MINORVERSION && gotminor == 0) ||
5972 minorvers == NFSV42_MINORVERSION)) ||
5973 (vers == NFS_VER3 && gotvers == 0)) {
5974 gotvers = vers;
5975 gotminor = minorvers;
5976 /* We'll take this one. */
5977 ndi->nfsdi_versindex = i;
5978 ndi->nfsdi_vers = vers;
5979 ndi->nfsdi_minorvers = minorvers;
5980 ndi->nfsdi_rsize = fxdr_unsigned(
5981 uint32_t, *tl++);
5982 ndi->nfsdi_wsize = fxdr_unsigned(
5983 uint32_t, *tl++);
5984 if (*tl == newnfs_true)
5985 ndi->nfsdi_flags |=
5986 NFSDI_TIGHTCOUPLED;
5987 else
5988 ndi->nfsdi_flags &=
5989 ~NFSDI_TIGHTCOUPLED;
5990 }
5991 }
5992 if (gotvers == 0) {
5993 printf("pNFS: no NFSv3, NFSv4.1 or NFSv4.2\n");
5994 error = NFSERR_BADXDR;
5995 goto nfsmout;
5996 }
5997 }
5998
5999 /* And the notify bits. */
6000 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
6001 bitcnt = fxdr_unsigned(int, *tl);
6002 if (bitcnt > 0) {
6003 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
6004 if (notifybitsp != NULL)
6005 *notifybitsp =
6006 fxdr_unsigned(uint32_t, *tl);
6007 }
6008 if (safilled != AF_UNSPEC) {
6009 KASSERT(ndi != NULL, ("ndi is NULL"));
6010 *ndip = ndi;
6011 } else
6012 error = EPERM;
6013 if (error == 0) {
6014 /*
6015 * Now we can do a TCP connection for the correct
6016 * NFS version and IP address.
6017 */
6018 error = nfsrpc_fillsa(nmp, &ssin, &ssin6, safilled,
6019 gotvers, gotminor, &dsp, p);
6020 }
6021 if (error == 0) {
6022 KASSERT(gotdspp != NULL, ("gotdspp is NULL"));
6023 *gotdspp = dsp;
6024 }
6025 }
6026 if (nd->nd_repstat != 0 && error == 0)
6027 error = nd->nd_repstat;
6028 nfsmout:
6029 if (error != 0 && ndi != NULL)
6030 nfscl_freedevinfo(ndi);
6031 m_freem(nd->nd_mrep);
6032 return (error);
6033 }
6034
6035 /*
6036 * Do the NFSv4.1 LayoutCommit.
6037 */
6038 int
nfsrpc_layoutcommit(struct nfsmount * nmp,uint8_t * fh,int fhlen,int reclaim,uint64_t off,uint64_t len,uint64_t lastbyte,nfsv4stateid_t * stateidp,int layouttype,struct ucred * cred,NFSPROC_T * p)6039 nfsrpc_layoutcommit(struct nfsmount *nmp, uint8_t *fh, int fhlen, int reclaim,
6040 uint64_t off, uint64_t len, uint64_t lastbyte, nfsv4stateid_t *stateidp,
6041 int layouttype, struct ucred *cred, NFSPROC_T *p)
6042 {
6043 uint32_t *tl;
6044 struct nfsrv_descript nfsd, *nd = &nfsd;
6045 int error;
6046
6047 nfscl_reqstart(nd, NFSPROC_LAYOUTCOMMIT, nmp, fh, fhlen, NULL, NULL,
6048 0, 0, cred);
6049 NFSM_BUILD(tl, uint32_t *, 5 * NFSX_UNSIGNED + 3 * NFSX_HYPER +
6050 NFSX_STATEID);
6051 txdr_hyper(off, tl);
6052 tl += 2;
6053 txdr_hyper(len, tl);
6054 tl += 2;
6055 if (reclaim != 0)
6056 *tl++ = newnfs_true;
6057 else
6058 *tl++ = newnfs_false;
6059 *tl++ = txdr_unsigned(stateidp->seqid);
6060 *tl++ = stateidp->other[0];
6061 *tl++ = stateidp->other[1];
6062 *tl++ = stateidp->other[2];
6063 *tl++ = newnfs_true;
6064 if (lastbyte < off)
6065 lastbyte = off;
6066 else if (lastbyte >= (off + len))
6067 lastbyte = off + len - 1;
6068 txdr_hyper(lastbyte, tl);
6069 tl += 2;
6070 *tl++ = newnfs_false;
6071 *tl++ = txdr_unsigned(layouttype);
6072 /* All supported layouts are 0 length. */
6073 *tl = txdr_unsigned(0);
6074 nd->nd_flag |= ND_USEGSSNAME;
6075 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
6076 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
6077 if (error != 0)
6078 return (error);
6079 error = nd->nd_repstat;
6080 m_freem(nd->nd_mrep);
6081 return (error);
6082 }
6083
6084 /*
6085 * Do the NFSv4.1 LayoutReturn.
6086 */
6087 int
nfsrpc_layoutreturn(struct nfsmount * nmp,uint8_t * fh,int fhlen,int reclaim,int layouttype,uint32_t iomode,int layoutreturn,uint64_t offset,uint64_t len,nfsv4stateid_t * stateidp,struct ucred * cred,NFSPROC_T * p,uint32_t stat,uint32_t op,char * devid)6088 nfsrpc_layoutreturn(struct nfsmount *nmp, uint8_t *fh, int fhlen, int reclaim,
6089 int layouttype, uint32_t iomode, int layoutreturn, uint64_t offset,
6090 uint64_t len, nfsv4stateid_t *stateidp, struct ucred *cred, NFSPROC_T *p,
6091 uint32_t stat, uint32_t op, char *devid)
6092 {
6093 uint32_t *tl;
6094 struct nfsrv_descript nfsd, *nd = &nfsd;
6095 uint64_t tu64;
6096 int error;
6097
6098 nfscl_reqstart(nd, NFSPROC_LAYOUTRETURN, nmp, fh, fhlen, NULL, NULL,
6099 0, 0, cred);
6100 NFSM_BUILD(tl, uint32_t *, 4 * NFSX_UNSIGNED);
6101 if (reclaim != 0)
6102 *tl++ = newnfs_true;
6103 else
6104 *tl++ = newnfs_false;
6105 *tl++ = txdr_unsigned(layouttype);
6106 *tl++ = txdr_unsigned(iomode);
6107 *tl = txdr_unsigned(layoutreturn);
6108 if (layoutreturn == NFSLAYOUTRETURN_FILE) {
6109 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_HYPER + NFSX_STATEID +
6110 NFSX_UNSIGNED);
6111 txdr_hyper(offset, tl);
6112 tl += 2;
6113 txdr_hyper(len, tl);
6114 tl += 2;
6115 NFSCL_DEBUG(4, "layoutret stseq=%d\n", (int)stateidp->seqid);
6116 *tl++ = txdr_unsigned(stateidp->seqid);
6117 *tl++ = stateidp->other[0];
6118 *tl++ = stateidp->other[1];
6119 *tl++ = stateidp->other[2];
6120 if (layouttype == NFSLAYOUT_NFSV4_1_FILES)
6121 *tl = txdr_unsigned(0);
6122 else if (layouttype == NFSLAYOUT_FLEXFILE) {
6123 if (stat != 0) {
6124 *tl = txdr_unsigned(2 * NFSX_HYPER +
6125 NFSX_STATEID + NFSX_V4DEVICEID + 5 *
6126 NFSX_UNSIGNED);
6127 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_HYPER +
6128 NFSX_STATEID + NFSX_V4DEVICEID + 5 *
6129 NFSX_UNSIGNED);
6130 *tl++ = txdr_unsigned(1); /* One error. */
6131 tu64 = 0; /* Offset. */
6132 txdr_hyper(tu64, tl); tl += 2;
6133 tu64 = UINT64_MAX; /* Length. */
6134 txdr_hyper(tu64, tl); tl += 2;
6135 NFSBCOPY(stateidp, tl, NFSX_STATEID);
6136 tl += (NFSX_STATEID / NFSX_UNSIGNED);
6137 *tl++ = txdr_unsigned(1); /* One error. */
6138 NFSBCOPY(devid, tl, NFSX_V4DEVICEID);
6139 tl += (NFSX_V4DEVICEID / NFSX_UNSIGNED);
6140 *tl++ = txdr_unsigned(stat);
6141 *tl++ = txdr_unsigned(op);
6142 } else {
6143 *tl = txdr_unsigned(2 * NFSX_UNSIGNED);
6144 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_UNSIGNED);
6145 /* No ioerrs. */
6146 *tl++ = 0;
6147 }
6148 *tl = 0; /* No stats yet. */
6149 }
6150 }
6151 nd->nd_flag |= ND_USEGSSNAME;
6152 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
6153 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
6154 if (error != 0)
6155 return (error);
6156 if (nd->nd_repstat == 0) {
6157 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
6158 if (*tl != 0) {
6159 NFSM_DISSECT(tl, uint32_t *, NFSX_STATEID);
6160 stateidp->seqid = fxdr_unsigned(uint32_t, *tl++);
6161 stateidp->other[0] = *tl++;
6162 stateidp->other[1] = *tl++;
6163 stateidp->other[2] = *tl;
6164 }
6165 } else
6166 error = nd->nd_repstat;
6167 nfsmout:
6168 m_freem(nd->nd_mrep);
6169 return (error);
6170 }
6171
6172 /*
6173 * Do the NFSv4.2 LayoutError.
6174 */
6175 static int
nfsrpc_layouterror(struct nfsmount * nmp,uint8_t * fh,int fhlen,uint64_t offset,uint64_t len,nfsv4stateid_t * stateidp,struct ucred * cred,NFSPROC_T * p,uint32_t stat,uint32_t op,char * devid)6176 nfsrpc_layouterror(struct nfsmount *nmp, uint8_t *fh, int fhlen, uint64_t offset,
6177 uint64_t len, nfsv4stateid_t *stateidp, struct ucred *cred, NFSPROC_T *p,
6178 uint32_t stat, uint32_t op, char *devid)
6179 {
6180 uint32_t *tl;
6181 struct nfsrv_descript nfsd, *nd = &nfsd;
6182 int error;
6183
6184 nfscl_reqstart(nd, NFSPROC_LAYOUTERROR, nmp, fh, fhlen, NULL, NULL,
6185 0, 0, cred);
6186 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_HYPER + NFSX_STATEID +
6187 NFSX_V4DEVICEID + 3 * NFSX_UNSIGNED);
6188 txdr_hyper(offset, tl); tl += 2;
6189 txdr_hyper(len, tl); tl += 2;
6190 *tl++ = txdr_unsigned(stateidp->seqid);
6191 *tl++ = stateidp->other[0];
6192 *tl++ = stateidp->other[1];
6193 *tl++ = stateidp->other[2];
6194 *tl++ = txdr_unsigned(1);
6195 NFSBCOPY(devid, tl, NFSX_V4DEVICEID);
6196 tl += (NFSX_V4DEVICEID / NFSX_UNSIGNED);
6197 *tl++ = txdr_unsigned(stat);
6198 *tl = txdr_unsigned(op);
6199 nd->nd_flag |= ND_USEGSSNAME;
6200 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
6201 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
6202 if (error != 0)
6203 return (error);
6204 if (nd->nd_repstat != 0)
6205 error = nd->nd_repstat;
6206 m_freem(nd->nd_mrep);
6207 return (error);
6208 }
6209
6210 /*
6211 * Acquire a layout and devinfo, if possible. The caller must have acquired
6212 * a reference count on the nfsclclient structure before calling this.
6213 * Return the layout in lypp with a reference count on it, if successful.
6214 */
6215 static int
nfsrpc_getlayout(struct nfsmount * nmp,vnode_t vp,struct nfsfh * nfhp,int iomode,uint32_t rw,uint32_t * notifybitsp,nfsv4stateid_t * stateidp,uint64_t off,struct nfscllayout ** lypp,struct ucred * cred,NFSPROC_T * p)6216 nfsrpc_getlayout(struct nfsmount *nmp, vnode_t vp, struct nfsfh *nfhp,
6217 int iomode, uint32_t rw, uint32_t *notifybitsp, nfsv4stateid_t *stateidp,
6218 uint64_t off, struct nfscllayout **lypp, struct ucred *cred, NFSPROC_T *p)
6219 {
6220 struct nfscllayout *lyp;
6221 struct nfsclflayout *flp;
6222 struct nfsclflayouthead flh;
6223 int error = 0, islocked, layoutlen, layouttype, recalled, retonclose;
6224 nfsv4stateid_t stateid;
6225 struct nfsclsession *tsep;
6226
6227 *lypp = NULL;
6228 if (NFSHASFLEXFILE(nmp))
6229 layouttype = NFSLAYOUT_FLEXFILE;
6230 else
6231 layouttype = NFSLAYOUT_NFSV4_1_FILES;
6232 /*
6233 * If lyp is returned non-NULL, there will be a refcnt (shared lock)
6234 * on it, iff flp != NULL or a lock (exclusive lock) on it iff
6235 * flp == NULL.
6236 */
6237 lyp = nfscl_getlayout(nmp->nm_clp, nfhp->nfh_fh, nfhp->nfh_len,
6238 off, rw, &flp, &recalled);
6239 islocked = 0;
6240 if (lyp == NULL || flp == NULL) {
6241 if (recalled != 0)
6242 return (EIO);
6243 LIST_INIT(&flh);
6244 tsep = nfsmnt_mdssession(nmp);
6245 layoutlen = tsep->nfsess_maxcache -
6246 (NFSX_STATEID + 3 * NFSX_UNSIGNED);
6247 if (lyp == NULL) {
6248 stateid.seqid = 0;
6249 stateid.other[0] = stateidp->other[0];
6250 stateid.other[1] = stateidp->other[1];
6251 stateid.other[2] = stateidp->other[2];
6252 error = nfsrpc_layoutget(nmp, nfhp->nfh_fh,
6253 nfhp->nfh_len, iomode, (uint64_t)0, UINT64_MAX,
6254 (uint64_t)0, layouttype, layoutlen, &stateid,
6255 &retonclose, &flh, cred, p);
6256 } else {
6257 islocked = 1;
6258 stateid.seqid = lyp->nfsly_stateid.seqid;
6259 stateid.other[0] = lyp->nfsly_stateid.other[0];
6260 stateid.other[1] = lyp->nfsly_stateid.other[1];
6261 stateid.other[2] = lyp->nfsly_stateid.other[2];
6262 error = nfsrpc_layoutget(nmp, nfhp->nfh_fh,
6263 nfhp->nfh_len, iomode, off, UINT64_MAX,
6264 (uint64_t)0, layouttype, layoutlen, &stateid,
6265 &retonclose, &flh, cred, p);
6266 }
6267 error = nfsrpc_layoutgetres(nmp, vp, nfhp->nfh_fh,
6268 nfhp->nfh_len, &stateid, retonclose, notifybitsp, &lyp,
6269 &flh, layouttype, error, NULL, cred, p);
6270 if (error == 0)
6271 *lypp = lyp;
6272 else if (islocked != 0)
6273 nfscl_rellayout(lyp, 1);
6274 } else
6275 *lypp = lyp;
6276 return (error);
6277 }
6278
6279 /*
6280 * Do a TCP connection plus exchange id and create session.
6281 * If successful, a "struct nfsclds" is linked into the list for the
6282 * mount point and a pointer to it is returned.
6283 */
6284 static int
nfsrpc_fillsa(struct nfsmount * nmp,struct sockaddr_in * sin,struct sockaddr_in6 * sin6,sa_family_t af,int vers,int minorvers,struct nfsclds ** dspp,NFSPROC_T * p)6285 nfsrpc_fillsa(struct nfsmount *nmp, struct sockaddr_in *sin,
6286 struct sockaddr_in6 *sin6, sa_family_t af, int vers, int minorvers,
6287 struct nfsclds **dspp, NFSPROC_T *p)
6288 {
6289 struct sockaddr_in *msad, *sad;
6290 struct sockaddr_in6 *msad6, *sad6;
6291 struct nfsclclient *clp;
6292 struct nfssockreq *nrp;
6293 struct nfsclds *dsp, *tdsp;
6294 int error, firsttry;
6295 enum nfsclds_state retv;
6296 uint32_t sequenceid = 0;
6297
6298 KASSERT(nmp->nm_sockreq.nr_cred != NULL,
6299 ("nfsrpc_fillsa: NULL nr_cred"));
6300 NFSLOCKCLSTATE();
6301 clp = nmp->nm_clp;
6302 NFSUNLOCKCLSTATE();
6303 if (clp == NULL)
6304 return (EPERM);
6305 if (af == AF_INET) {
6306 NFSLOCKMNT(nmp);
6307 /*
6308 * Check to see if we already have a session for this
6309 * address that is usable for a DS.
6310 * Note that the MDS's address is in a different place
6311 * than the sessions already acquired for DS's.
6312 */
6313 msad = (struct sockaddr_in *)nmp->nm_sockreq.nr_nam;
6314 tdsp = TAILQ_FIRST(&nmp->nm_sess);
6315 while (tdsp != NULL) {
6316 if (msad != NULL && msad->sin_family == AF_INET &&
6317 sin->sin_addr.s_addr == msad->sin_addr.s_addr &&
6318 sin->sin_port == msad->sin_port &&
6319 (tdsp->nfsclds_flags & NFSCLDS_DS) != 0 &&
6320 tdsp->nfsclds_sess.nfsess_defunct == 0) {
6321 *dspp = tdsp;
6322 NFSUNLOCKMNT(nmp);
6323 NFSCL_DEBUG(4, "fnd same addr\n");
6324 return (0);
6325 }
6326 tdsp = TAILQ_NEXT(tdsp, nfsclds_list);
6327 if (tdsp != NULL && tdsp->nfsclds_sockp != NULL)
6328 msad = (struct sockaddr_in *)
6329 tdsp->nfsclds_sockp->nr_nam;
6330 else
6331 msad = NULL;
6332 }
6333 NFSUNLOCKMNT(nmp);
6334
6335 /* No IP address match, so look for new/trunked one. */
6336 sad = malloc(sizeof(*sad), M_SONAME, M_WAITOK | M_ZERO);
6337 sad->sin_len = sizeof(*sad);
6338 sad->sin_family = AF_INET;
6339 sad->sin_port = sin->sin_port;
6340 sad->sin_addr.s_addr = sin->sin_addr.s_addr;
6341 if (NFSHASPNFS(nmp) && NFSHASKERB(nmp)) {
6342 /* For pNFS, a separate server principal is needed. */
6343 nrp = malloc(sizeof(*nrp) + NI_MAXSERV + NI_MAXHOST,
6344 M_NFSSOCKREQ, M_WAITOK | M_ZERO);
6345 /*
6346 * Use the latter part of nr_srvprinc as a temporary
6347 * buffer for the IP address.
6348 */
6349 inet_ntoa_r(sad->sin_addr,
6350 &nrp->nr_srvprinc[NI_MAXSERV]);
6351 NFSCL_DEBUG(1, "nfsrpc_fillsa: DS IP=%s\n",
6352 &nrp->nr_srvprinc[NI_MAXSERV]);
6353 if (!rpc_gss_ip_to_srv_principal_call(
6354 &nrp->nr_srvprinc[NI_MAXSERV], "nfs",
6355 nrp->nr_srvprinc))
6356 nrp->nr_srvprinc[0] = '\0';
6357 NFSCL_DEBUG(1, "nfsrpc_fillsa: srv principal=%s\n",
6358 nrp->nr_srvprinc);
6359 } else
6360 nrp = malloc(sizeof(*nrp), M_NFSSOCKREQ,
6361 M_WAITOK | M_ZERO);
6362 nrp->nr_nam = (struct sockaddr *)sad;
6363 } else if (af == AF_INET6) {
6364 NFSLOCKMNT(nmp);
6365 /*
6366 * Check to see if we already have a session for this
6367 * address that is usable for a DS.
6368 * Note that the MDS's address is in a different place
6369 * than the sessions already acquired for DS's.
6370 */
6371 msad6 = (struct sockaddr_in6 *)nmp->nm_sockreq.nr_nam;
6372 tdsp = TAILQ_FIRST(&nmp->nm_sess);
6373 while (tdsp != NULL) {
6374 if (msad6 != NULL && msad6->sin6_family == AF_INET6 &&
6375 IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr,
6376 &msad6->sin6_addr) &&
6377 sin6->sin6_port == msad6->sin6_port &&
6378 (tdsp->nfsclds_flags & NFSCLDS_DS) != 0 &&
6379 tdsp->nfsclds_sess.nfsess_defunct == 0) {
6380 *dspp = tdsp;
6381 NFSUNLOCKMNT(nmp);
6382 return (0);
6383 }
6384 tdsp = TAILQ_NEXT(tdsp, nfsclds_list);
6385 if (tdsp != NULL && tdsp->nfsclds_sockp != NULL)
6386 msad6 = (struct sockaddr_in6 *)
6387 tdsp->nfsclds_sockp->nr_nam;
6388 else
6389 msad6 = NULL;
6390 }
6391 NFSUNLOCKMNT(nmp);
6392
6393 /* No IP address match, so look for new/trunked one. */
6394 sad6 = malloc(sizeof(*sad6), M_SONAME, M_WAITOK | M_ZERO);
6395 sad6->sin6_len = sizeof(*sad6);
6396 sad6->sin6_family = AF_INET6;
6397 sad6->sin6_port = sin6->sin6_port;
6398 NFSBCOPY(&sin6->sin6_addr, &sad6->sin6_addr,
6399 sizeof(struct in6_addr));
6400 if (NFSHASPNFS(nmp) && NFSHASKERB(nmp)) {
6401 /* For pNFS, a separate server principal is needed. */
6402 nrp = malloc(sizeof(*nrp) + NI_MAXSERV + NI_MAXHOST,
6403 M_NFSSOCKREQ, M_WAITOK | M_ZERO);
6404 /*
6405 * Use the latter part of nr_srvprinc as a temporary
6406 * buffer for the IP address.
6407 */
6408 inet_ntop(AF_INET6, &sad6->sin6_addr,
6409 &nrp->nr_srvprinc[NI_MAXSERV], NI_MAXHOST);
6410 NFSCL_DEBUG(1, "nfsrpc_fillsa: DS IP=%s\n",
6411 &nrp->nr_srvprinc[NI_MAXSERV]);
6412 if (!rpc_gss_ip_to_srv_principal_call(
6413 &nrp->nr_srvprinc[NI_MAXSERV], "nfs",
6414 nrp->nr_srvprinc))
6415 nrp->nr_srvprinc[0] = '\0';
6416 NFSCL_DEBUG(1, "nfsrpc_fillsa: srv principal=%s\n",
6417 nrp->nr_srvprinc);
6418 } else
6419 nrp = malloc(sizeof(*nrp), M_NFSSOCKREQ,
6420 M_WAITOK | M_ZERO);
6421 nrp->nr_nam = (struct sockaddr *)sad6;
6422 } else
6423 return (EPERM);
6424
6425 nrp->nr_sotype = SOCK_STREAM;
6426 mtx_init(&nrp->nr_mtx, "nfssock", NULL, MTX_DEF);
6427 nrp->nr_prog = NFS_PROG;
6428 nrp->nr_vers = vers;
6429
6430 /*
6431 * Use the credentials that were used for the mount, which are
6432 * in nmp->nm_sockreq.nr_cred for newnfs_connect() etc.
6433 * Ref. counting the credentials with crhold() is probably not
6434 * necessary, since nm_sockreq.nr_cred won't be crfree()'d until
6435 * unmount, but I did it anyhow.
6436 */
6437 nrp->nr_cred = crhold(nmp->nm_sockreq.nr_cred);
6438 error = newnfs_connect(nmp, nrp, NULL, p, 0, false, &nrp->nr_client);
6439 NFSCL_DEBUG(3, "DS connect=%d\n", error);
6440
6441 dsp = NULL;
6442 /* Now, do the exchangeid and create session. */
6443 if (error == 0) {
6444 if (vers == NFS_VER4) {
6445 firsttry = 0;
6446 do {
6447 error = nfsrpc_exchangeid(nmp, clp, nrp,
6448 minorvers, NFSV4EXCH_USEPNFSDS, &dsp,
6449 nrp->nr_cred, p);
6450 NFSCL_DEBUG(3, "DS exchangeid=%d\n", error);
6451 if (error == NFSERR_MINORVERMISMATCH)
6452 minorvers = NFSV42_MINORVERSION;
6453 } while (error == NFSERR_MINORVERMISMATCH &&
6454 firsttry++ == 0);
6455 if (error != 0)
6456 newnfs_disconnect(NULL, nrp);
6457 } else {
6458 dsp = malloc(sizeof(struct nfsclds), M_NFSCLDS,
6459 M_WAITOK | M_ZERO);
6460 dsp->nfsclds_flags |= NFSCLDS_DS;
6461 dsp->nfsclds_expire = INT32_MAX; /* No renews needed. */
6462 mtx_init(&dsp->nfsclds_mtx, "nfsds", NULL, MTX_DEF);
6463 mtx_init(&dsp->nfsclds_sess.nfsess_mtx, "nfssession",
6464 NULL, MTX_DEF);
6465 }
6466 }
6467 if (error == 0) {
6468 dsp->nfsclds_sockp = nrp;
6469 if (vers == NFS_VER4) {
6470 NFSLOCKMNT(nmp);
6471 retv = nfscl_getsameserver(nmp, dsp, &tdsp,
6472 &sequenceid);
6473 NFSCL_DEBUG(3, "getsame ret=%d\n", retv);
6474 if (retv == NFSDSP_USETHISSESSION &&
6475 nfscl_dssameconn != 0) {
6476 NFSLOCKDS(tdsp);
6477 tdsp->nfsclds_flags |= NFSCLDS_SAMECONN;
6478 NFSUNLOCKDS(tdsp);
6479 NFSUNLOCKMNT(nmp);
6480 /*
6481 * If there is already a session for this
6482 * server, use it.
6483 */
6484 newnfs_disconnect(NULL, nrp);
6485 nfscl_freenfsclds(dsp);
6486 *dspp = tdsp;
6487 return (0);
6488 }
6489 if (retv == NFSDSP_NOTFOUND)
6490 sequenceid =
6491 dsp->nfsclds_sess.nfsess_sequenceid;
6492 NFSUNLOCKMNT(nmp);
6493 error = nfsrpc_createsession(nmp, &dsp->nfsclds_sess,
6494 nrp, dsp, sequenceid, 0, nrp->nr_cred, p);
6495 NFSCL_DEBUG(3, "DS createsess=%d\n", error);
6496 }
6497 } else {
6498 NFSFREECRED(nrp->nr_cred);
6499 NFSFREEMUTEX(&nrp->nr_mtx);
6500 free(nrp->nr_nam, M_SONAME);
6501 free(nrp, M_NFSSOCKREQ);
6502 }
6503 if (error == 0) {
6504 NFSCL_DEBUG(3, "add DS session\n");
6505 /*
6506 * Put it at the end of the list. That way the list
6507 * is ordered by when the entry was added. This matters
6508 * since the one done first is the one that should be
6509 * used for sequencid'ing any subsequent create sessions.
6510 */
6511 NFSLOCKMNT(nmp);
6512 TAILQ_INSERT_TAIL(&nmp->nm_sess, dsp, nfsclds_list);
6513 NFSUNLOCKMNT(nmp);
6514 *dspp = dsp;
6515 } else if (dsp != NULL) {
6516 newnfs_disconnect(NULL, nrp);
6517 nfscl_freenfsclds(dsp);
6518 }
6519 return (error);
6520 }
6521
6522 /*
6523 * Do the NFSv4.1 Reclaim Complete.
6524 */
6525 int
nfsrpc_reclaimcomplete(struct nfsmount * nmp,struct ucred * cred,NFSPROC_T * p)6526 nfsrpc_reclaimcomplete(struct nfsmount *nmp, struct ucred *cred, NFSPROC_T *p)
6527 {
6528 uint32_t *tl;
6529 struct nfsrv_descript nfsd;
6530 struct nfsrv_descript *nd = &nfsd;
6531 int error;
6532
6533 nfscl_reqstart(nd, NFSPROC_RECLAIMCOMPL, nmp, NULL, 0, NULL, NULL, 0,
6534 0, cred);
6535 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
6536 *tl = newnfs_false;
6537 nd->nd_flag |= ND_USEGSSNAME;
6538 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
6539 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
6540 if (error != 0)
6541 return (error);
6542 error = nd->nd_repstat;
6543 m_freem(nd->nd_mrep);
6544 return (error);
6545 }
6546
6547 /*
6548 * Initialize the slot tables for a session.
6549 */
6550 static void
nfscl_initsessionslots(struct nfsclsession * sep)6551 nfscl_initsessionslots(struct nfsclsession *sep)
6552 {
6553 int i;
6554
6555 for (i = 0; i < NFSV4_CBSLOTS; i++) {
6556 if (sep->nfsess_cbslots[i].nfssl_reply != NULL)
6557 m_freem(sep->nfsess_cbslots[i].nfssl_reply);
6558 NFSBZERO(&sep->nfsess_cbslots[i], sizeof(struct nfsslot));
6559 }
6560 for (i = 0; i < 64; i++)
6561 sep->nfsess_slotseq[i] = 0;
6562 sep->nfsess_slots = 0;
6563 sep->nfsess_badslots = 0;
6564 }
6565
6566 /*
6567 * Called to try and do an I/O operation via an NFSv4.1 Data Server (DS).
6568 */
6569 int
nfscl_doiods(vnode_t vp,struct uio * uiop,int * iomode,int * must_commit,uint32_t rwaccess,int docommit,struct ucred * cred,NFSPROC_T * p)6570 nfscl_doiods(vnode_t vp, struct uio *uiop, int *iomode, int *must_commit,
6571 uint32_t rwaccess, int docommit, struct ucred *cred, NFSPROC_T *p)
6572 {
6573 struct nfsnode *np = VTONFS(vp);
6574 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
6575 struct nfscllayout *layp;
6576 struct nfscldevinfo *dip;
6577 struct nfsclflayout *rflp;
6578 struct mbuf *m, *m2;
6579 struct nfsclwritedsdorpc *drpc, *tdrpc;
6580 nfsv4stateid_t stateid;
6581 struct ucred *newcred;
6582 uint64_t lastbyte, len, off, oresid, xfer;
6583 int eof, error, firstmirror, i, iolaymode, mirrorcnt, recalled, timo;
6584 void *lckp;
6585 uint8_t *dev;
6586 void *iovbase = NULL;
6587 size_t iovlen = 0;
6588 off_t offs = 0;
6589 ssize_t resid = 0;
6590 uint32_t op;
6591
6592 if (!NFSHASPNFS(nmp) || nfscl_enablecallb == 0 || nfs_numnfscbd == 0 ||
6593 (np->n_flag & NNOLAYOUT) != 0)
6594 return (EIO);
6595 /* Now, get a reference cnt on the clientid for this mount. */
6596 if (nfscl_getref(nmp) == 0)
6597 return (EIO);
6598
6599 /* Find an appropriate stateid. */
6600 newcred = NFSNEWCRED(cred);
6601 error = nfscl_getstateid(vp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len,
6602 rwaccess, 1, newcred, p, &stateid, &lckp);
6603 if (error != 0) {
6604 NFSFREECRED(newcred);
6605 nfscl_relref(nmp);
6606 return (error);
6607 }
6608 /* Search for a layout for this file. */
6609 off = uiop->uio_offset;
6610 layp = nfscl_getlayout(nmp->nm_clp, np->n_fhp->nfh_fh,
6611 np->n_fhp->nfh_len, off, rwaccess, &rflp, &recalled);
6612 if (layp == NULL || rflp == NULL) {
6613 if (recalled != 0) {
6614 NFSFREECRED(newcred);
6615 if (lckp != NULL)
6616 nfscl_lockderef(lckp);
6617 nfscl_relref(nmp);
6618 return (EIO);
6619 }
6620 if (layp != NULL) {
6621 nfscl_rellayout(layp, (rflp == NULL) ? 1 : 0);
6622 layp = NULL;
6623 }
6624 /* Try and get a Layout, if it is supported. */
6625 if (rwaccess == NFSV4OPEN_ACCESSWRITE ||
6626 (np->n_flag & NWRITEOPENED) != 0)
6627 iolaymode = NFSLAYOUTIOMODE_RW;
6628 else
6629 iolaymode = NFSLAYOUTIOMODE_READ;
6630 error = nfsrpc_getlayout(nmp, vp, np->n_fhp, iolaymode,
6631 rwaccess, NULL, &stateid, off, &layp, newcred, p);
6632 if (error != 0) {
6633 NFSLOCKNODE(np);
6634 np->n_flag |= NNOLAYOUT;
6635 NFSUNLOCKNODE(np);
6636 if (lckp != NULL)
6637 nfscl_lockderef(lckp);
6638 NFSFREECRED(newcred);
6639 if (layp != NULL)
6640 nfscl_rellayout(layp, 0);
6641 nfscl_relref(nmp);
6642 return (error);
6643 }
6644 }
6645
6646 /*
6647 * Loop around finding a layout that works for the first part of
6648 * this I/O operation, and then call the function that actually
6649 * does the RPC.
6650 */
6651 eof = 0;
6652 len = (uint64_t)uiop->uio_resid;
6653 while (len > 0 && error == 0 && eof == 0) {
6654 off = uiop->uio_offset;
6655 error = nfscl_findlayoutforio(layp, off, rwaccess, &rflp);
6656 if (error == 0) {
6657 oresid = xfer = (uint64_t)uiop->uio_resid;
6658 if (xfer > (rflp->nfsfl_end - rflp->nfsfl_off))
6659 xfer = rflp->nfsfl_end - rflp->nfsfl_off;
6660 /*
6661 * For Flex File layout with mirrored DSs, select one
6662 * of them at random for reads. For writes and commits,
6663 * do all mirrors.
6664 */
6665 m = NULL;
6666 tdrpc = drpc = NULL;
6667 firstmirror = 0;
6668 mirrorcnt = 1;
6669 if ((layp->nfsly_flags & NFSLY_FLEXFILE) != 0 &&
6670 (mirrorcnt = rflp->nfsfl_mirrorcnt) > 1) {
6671 if (rwaccess == NFSV4OPEN_ACCESSREAD) {
6672 firstmirror = arc4random() % mirrorcnt;
6673 mirrorcnt = firstmirror + 1;
6674 } else {
6675 if (docommit == 0) {
6676 /*
6677 * Save values, so uiop can be
6678 * rolled back upon a write
6679 * error.
6680 */
6681 offs = uiop->uio_offset;
6682 resid = uiop->uio_resid;
6683 iovbase =
6684 uiop->uio_iov->iov_base;
6685 iovlen = uiop->uio_iov->iov_len;
6686 m = nfsm_uiombuflist(uiop, len,
6687 0);
6688 if (m == NULL) {
6689 error = EFAULT;
6690 break;
6691 }
6692 }
6693 tdrpc = drpc = malloc(sizeof(*drpc) *
6694 (mirrorcnt - 1), M_TEMP, M_WAITOK |
6695 M_ZERO);
6696 }
6697 }
6698 for (i = firstmirror; i < mirrorcnt && error == 0; i++){
6699 m2 = NULL;
6700 if (m != NULL && i < mirrorcnt - 1)
6701 m2 = m_copym(m, 0, M_COPYALL, M_WAITOK);
6702 else {
6703 m2 = m;
6704 m = NULL;
6705 }
6706 if ((layp->nfsly_flags & NFSLY_FLEXFILE) != 0) {
6707 dev = rflp->nfsfl_ffm[i].dev;
6708 dip = nfscl_getdevinfo(nmp->nm_clp, dev,
6709 rflp->nfsfl_ffm[i].devp);
6710 } else {
6711 dev = rflp->nfsfl_dev;
6712 dip = nfscl_getdevinfo(nmp->nm_clp, dev,
6713 rflp->nfsfl_devp);
6714 }
6715 if (dip != NULL) {
6716 if ((rflp->nfsfl_flags & NFSFL_FLEXFILE)
6717 != 0)
6718 error = nfscl_dofflayoutio(vp,
6719 uiop, iomode, must_commit,
6720 &eof, &stateid, rwaccess,
6721 dip, layp, rflp, off, xfer,
6722 i, docommit, m2, tdrpc,
6723 newcred, p);
6724 else
6725 error = nfscl_doflayoutio(vp,
6726 uiop, iomode, must_commit,
6727 &eof, &stateid, rwaccess,
6728 dip, layp, rflp, off, xfer,
6729 docommit, newcred, p);
6730 nfscl_reldevinfo(dip);
6731 } else {
6732 if (m2 != NULL)
6733 m_freem(m2);
6734 error = EIO;
6735 }
6736 tdrpc++;
6737 }
6738 if (m != NULL)
6739 m_freem(m);
6740 tdrpc = drpc;
6741 timo = hz / 50; /* Wait for 20msec. */
6742 if (timo < 1)
6743 timo = 1;
6744 for (i = firstmirror; i < mirrorcnt - 1 &&
6745 tdrpc != NULL; i++, tdrpc++) {
6746 /*
6747 * For the unused drpc entries, both inprog and
6748 * err == 0, so this loop won't break.
6749 */
6750 while (tdrpc->inprog != 0 && tdrpc->done == 0)
6751 tsleep(&tdrpc->tsk, PVFS, "clrpcio",
6752 timo);
6753 if (error == 0 && tdrpc->err != 0)
6754 error = tdrpc->err;
6755 if (rwaccess != NFSV4OPEN_ACCESSREAD &&
6756 docommit == 0 && *must_commit == 0 &&
6757 tdrpc->must_commit == 1)
6758 *must_commit = 1;
6759 }
6760 free(drpc, M_TEMP);
6761 if (error == 0) {
6762 if (mirrorcnt > 1 && rwaccess ==
6763 NFSV4OPEN_ACCESSWRITE && docommit == 0) {
6764 NFSLOCKCLSTATE();
6765 layp->nfsly_flags |= NFSLY_WRITTEN;
6766 NFSUNLOCKCLSTATE();
6767 }
6768 lastbyte = off + xfer - 1;
6769 NFSLOCKCLSTATE();
6770 if (lastbyte > layp->nfsly_lastbyte)
6771 layp->nfsly_lastbyte = lastbyte;
6772 NFSUNLOCKCLSTATE();
6773 } else if (error == NFSERR_OPENMODE &&
6774 rwaccess == NFSV4OPEN_ACCESSREAD) {
6775 NFSLOCKMNT(nmp);
6776 nmp->nm_state |= NFSSTA_OPENMODE;
6777 NFSUNLOCKMNT(nmp);
6778 } else if ((error == NFSERR_NOSPC ||
6779 error == NFSERR_IO || error == NFSERR_NXIO) &&
6780 nmp->nm_minorvers == NFSV42_MINORVERSION) {
6781 if (docommit != 0)
6782 op = NFSV4OP_COMMIT;
6783 else if (rwaccess == NFSV4OPEN_ACCESSREAD)
6784 op = NFSV4OP_READ;
6785 else
6786 op = NFSV4OP_WRITE;
6787 nfsrpc_layouterror(nmp, np->n_fhp->nfh_fh,
6788 np->n_fhp->nfh_len, off, xfer,
6789 &layp->nfsly_stateid, newcred, p, error, op,
6790 dip->nfsdi_deviceid);
6791 error = EIO;
6792 } else
6793 error = EIO;
6794 if (error == 0)
6795 len -= (oresid - (uint64_t)uiop->uio_resid);
6796 else if (mirrorcnt > 1 && rwaccess ==
6797 NFSV4OPEN_ACCESSWRITE && docommit == 0) {
6798 /*
6799 * In case the rpc gets retried, roll the
6800 * uio fields changed by nfsm_uiombuflist()
6801 * back.
6802 */
6803 uiop->uio_offset = offs;
6804 uiop->uio_resid = resid;
6805 uiop->uio_iov->iov_base = iovbase;
6806 uiop->uio_iov->iov_len = iovlen;
6807 }
6808 }
6809 }
6810 if (lckp != NULL)
6811 nfscl_lockderef(lckp);
6812 NFSFREECRED(newcred);
6813 nfscl_rellayout(layp, 0);
6814 nfscl_relref(nmp);
6815 return (error);
6816 }
6817
6818 /*
6819 * Find a file layout that will handle the first bytes of the requested
6820 * range and return the information from it needed to the I/O operation.
6821 */
6822 int
nfscl_findlayoutforio(struct nfscllayout * lyp,uint64_t off,uint32_t rwaccess,struct nfsclflayout ** retflpp)6823 nfscl_findlayoutforio(struct nfscllayout *lyp, uint64_t off, uint32_t rwaccess,
6824 struct nfsclflayout **retflpp)
6825 {
6826 struct nfsclflayout *flp, *nflp, *rflp;
6827 uint32_t rw;
6828
6829 rflp = NULL;
6830 rw = rwaccess;
6831 /* For reading, do the Read list first and then the Write list. */
6832 do {
6833 if (rw == NFSV4OPEN_ACCESSREAD)
6834 flp = LIST_FIRST(&lyp->nfsly_flayread);
6835 else
6836 flp = LIST_FIRST(&lyp->nfsly_flayrw);
6837 while (flp != NULL) {
6838 nflp = LIST_NEXT(flp, nfsfl_list);
6839 if (flp->nfsfl_off > off)
6840 break;
6841 if (flp->nfsfl_end > off &&
6842 (rflp == NULL || rflp->nfsfl_end < flp->nfsfl_end))
6843 rflp = flp;
6844 flp = nflp;
6845 }
6846 if (rw == NFSV4OPEN_ACCESSREAD)
6847 rw = NFSV4OPEN_ACCESSWRITE;
6848 else
6849 rw = 0;
6850 } while (rw != 0);
6851 if (rflp != NULL) {
6852 /* This one covers the most bytes starting at off. */
6853 *retflpp = rflp;
6854 return (0);
6855 }
6856 return (EIO);
6857 }
6858
6859 /*
6860 * Do I/O using an NFSv4.1 or NFSv4.2 file layout.
6861 */
6862 static int
nfscl_doflayoutio(vnode_t vp,struct uio * uiop,int * iomode,int * must_commit,int * eofp,nfsv4stateid_t * stateidp,int rwflag,struct nfscldevinfo * dp,struct nfscllayout * lyp,struct nfsclflayout * flp,uint64_t off,uint64_t len,int docommit,struct ucred * cred,NFSPROC_T * p)6863 nfscl_doflayoutio(vnode_t vp, struct uio *uiop, int *iomode, int *must_commit,
6864 int *eofp, nfsv4stateid_t *stateidp, int rwflag, struct nfscldevinfo *dp,
6865 struct nfscllayout *lyp, struct nfsclflayout *flp, uint64_t off,
6866 uint64_t len, int docommit, struct ucred *cred, NFSPROC_T *p)
6867 {
6868 uint64_t io_off, rel_off, stripe_unit_size, transfer, xfer;
6869 int commit_thru_mds, error, stripe_index, stripe_pos, minorvers;
6870 struct nfsnode *np;
6871 struct nfsfh *fhp;
6872 struct nfsclds **dspp;
6873
6874 np = VTONFS(vp);
6875 rel_off = off - flp->nfsfl_patoff;
6876 stripe_unit_size = flp->nfsfl_util & NFSFLAYUTIL_STRIPE_MASK;
6877 stripe_pos = (rel_off / stripe_unit_size + flp->nfsfl_stripe1) %
6878 dp->nfsdi_stripecnt;
6879 transfer = stripe_unit_size - (rel_off % stripe_unit_size);
6880 error = 0;
6881
6882 /* Loop around, doing I/O for each stripe unit. */
6883 while (len > 0 && error == 0) {
6884 stripe_index = nfsfldi_stripeindex(dp, stripe_pos);
6885 dspp = nfsfldi_addr(dp, stripe_index);
6886 if (((*dspp)->nfsclds_flags & NFSCLDS_MINORV2) != 0)
6887 minorvers = NFSV42_MINORVERSION;
6888 else
6889 minorvers = NFSV41_MINORVERSION;
6890 if (len > transfer && docommit == 0)
6891 xfer = transfer;
6892 else
6893 xfer = len;
6894 if ((flp->nfsfl_util & NFSFLAYUTIL_DENSE) != 0) {
6895 /* Dense layout. */
6896 if (stripe_pos >= flp->nfsfl_fhcnt)
6897 return (EIO);
6898 fhp = flp->nfsfl_fh[stripe_pos];
6899 io_off = (rel_off / (stripe_unit_size *
6900 dp->nfsdi_stripecnt)) * stripe_unit_size +
6901 rel_off % stripe_unit_size;
6902 } else {
6903 /* Sparse layout. */
6904 if (flp->nfsfl_fhcnt > 1) {
6905 if (stripe_index >= flp->nfsfl_fhcnt)
6906 return (EIO);
6907 fhp = flp->nfsfl_fh[stripe_index];
6908 } else if (flp->nfsfl_fhcnt == 1)
6909 fhp = flp->nfsfl_fh[0];
6910 else
6911 fhp = np->n_fhp;
6912 io_off = off;
6913 }
6914 if ((flp->nfsfl_util & NFSFLAYUTIL_COMMIT_THRU_MDS) != 0) {
6915 commit_thru_mds = 1;
6916 if (docommit != 0)
6917 error = EIO;
6918 } else {
6919 commit_thru_mds = 0;
6920 NFSLOCKNODE(np);
6921 np->n_flag |= NDSCOMMIT;
6922 NFSUNLOCKNODE(np);
6923 }
6924 if (docommit != 0) {
6925 if (error == 0)
6926 error = nfsrpc_commitds(vp, io_off, xfer,
6927 *dspp, fhp, NFS_VER4, minorvers, cred, p);
6928 if (error == 0) {
6929 /*
6930 * Set both eof and uio_resid = 0 to end any
6931 * loops.
6932 */
6933 *eofp = 1;
6934 uiop->uio_resid = 0;
6935 } else {
6936 NFSLOCKNODE(np);
6937 np->n_flag &= ~NDSCOMMIT;
6938 NFSUNLOCKNODE(np);
6939 }
6940 } else if (rwflag == NFSV4OPEN_ACCESSREAD)
6941 error = nfsrpc_readds(vp, uiop, stateidp, eofp, *dspp,
6942 io_off, xfer, fhp, 0, NFS_VER4, minorvers, cred, p);
6943 else {
6944 error = nfsrpc_writeds(vp, uiop, iomode, must_commit,
6945 stateidp, *dspp, io_off, xfer, fhp, commit_thru_mds,
6946 0, NFS_VER4, minorvers, cred, p);
6947 if (error == 0) {
6948 NFSLOCKCLSTATE();
6949 lyp->nfsly_flags |= NFSLY_WRITTEN;
6950 NFSUNLOCKCLSTATE();
6951 }
6952 }
6953 if (error == 0) {
6954 transfer = stripe_unit_size;
6955 stripe_pos = (stripe_pos + 1) % dp->nfsdi_stripecnt;
6956 len -= xfer;
6957 off += xfer;
6958 }
6959 }
6960 return (error);
6961 }
6962
6963 /*
6964 * Do I/O using an NFSv4.1 flex file layout.
6965 */
6966 static int
nfscl_dofflayoutio(vnode_t vp,struct uio * uiop,int * iomode,int * must_commit,int * eofp,nfsv4stateid_t * stateidp,int rwflag,struct nfscldevinfo * dp,struct nfscllayout * lyp,struct nfsclflayout * flp,uint64_t off,uint64_t len,int mirror,int docommit,struct mbuf * mp,struct nfsclwritedsdorpc * drpc,struct ucred * cred,NFSPROC_T * p)6967 nfscl_dofflayoutio(vnode_t vp, struct uio *uiop, int *iomode, int *must_commit,
6968 int *eofp, nfsv4stateid_t *stateidp, int rwflag, struct nfscldevinfo *dp,
6969 struct nfscllayout *lyp, struct nfsclflayout *flp, uint64_t off,
6970 uint64_t len, int mirror, int docommit, struct mbuf *mp,
6971 struct nfsclwritedsdorpc *drpc, struct ucred *cred, NFSPROC_T *p)
6972 {
6973 uint64_t xfer;
6974 int error;
6975 struct nfsnode *np;
6976 struct nfsfh *fhp;
6977 struct nfsclds **dspp;
6978 struct ucred *tcred;
6979 struct mbuf *m, *m2;
6980 uint32_t copylen;
6981
6982 np = VTONFS(vp);
6983 error = 0;
6984 NFSCL_DEBUG(4, "nfscl_dofflayoutio: off=%ju len=%ju\n", (uintmax_t)off,
6985 (uintmax_t)len);
6986 /* Loop around, doing I/O for each stripe unit. */
6987 while (len > 0 && error == 0) {
6988 dspp = nfsfldi_addr(dp, 0);
6989 fhp = flp->nfsfl_ffm[mirror].fh[dp->nfsdi_versindex];
6990 stateidp = &flp->nfsfl_ffm[mirror].st;
6991 NFSCL_DEBUG(4, "mirror=%d vind=%d fhlen=%d st.seqid=0x%x\n",
6992 mirror, dp->nfsdi_versindex, fhp->nfh_len, stateidp->seqid);
6993 if ((dp->nfsdi_flags & NFSDI_TIGHTCOUPLED) == 0) {
6994 tcred = NFSNEWCRED(cred);
6995 tcred->cr_uid = flp->nfsfl_ffm[mirror].user;
6996 tcred->cr_gid = flp->nfsfl_ffm[mirror].group;
6997 tcred->cr_ngroups = 0;
6998 } else
6999 tcred = cred;
7000 if (rwflag == NFSV4OPEN_ACCESSREAD)
7001 copylen = dp->nfsdi_rsize;
7002 else {
7003 copylen = dp->nfsdi_wsize;
7004 if (len > copylen && mp != NULL) {
7005 /*
7006 * When a mirrored configuration needs to do
7007 * multiple writes to each mirror, all writes
7008 * except the last one must be a multiple of
7009 * 4 bytes. This is required so that the XDR
7010 * does not need padding.
7011 * If possible, clip the size to an exact
7012 * multiple of the mbuf length, so that the
7013 * split will be on an mbuf boundary.
7014 */
7015 copylen &= 0xfffffffc;
7016 if (copylen > mp->m_len)
7017 copylen = copylen / mp->m_len *
7018 mp->m_len;
7019 }
7020 }
7021 NFSLOCKNODE(np);
7022 np->n_flag |= NDSCOMMIT;
7023 NFSUNLOCKNODE(np);
7024 if (len > copylen && docommit == 0)
7025 xfer = copylen;
7026 else
7027 xfer = len;
7028 if (docommit != 0) {
7029 if (error == 0) {
7030 /*
7031 * Do last mirrored DS commit with this thread.
7032 */
7033 if (mirror < flp->nfsfl_mirrorcnt - 1)
7034 error = nfsio_commitds(vp, off, xfer,
7035 *dspp, fhp, dp->nfsdi_vers,
7036 dp->nfsdi_minorvers, drpc, tcred,
7037 p);
7038 else
7039 error = nfsrpc_commitds(vp, off, xfer,
7040 *dspp, fhp, dp->nfsdi_vers,
7041 dp->nfsdi_minorvers, tcred, p);
7042 NFSCL_DEBUG(4, "commitds=%d\n", error);
7043 if (error != 0 && error != EACCES && error !=
7044 ESTALE) {
7045 NFSCL_DEBUG(4,
7046 "DS layreterr for commit\n");
7047 nfscl_dserr(NFSV4OP_COMMIT, error, dp,
7048 lyp, *dspp);
7049 }
7050 }
7051 NFSCL_DEBUG(4, "aft nfsio_commitds=%d\n", error);
7052 if (error == 0) {
7053 /*
7054 * Set both eof and uio_resid = 0 to end any
7055 * loops.
7056 */
7057 *eofp = 1;
7058 uiop->uio_resid = 0;
7059 } else {
7060 NFSLOCKNODE(np);
7061 np->n_flag &= ~NDSCOMMIT;
7062 NFSUNLOCKNODE(np);
7063 }
7064 } else if (rwflag == NFSV4OPEN_ACCESSREAD) {
7065 error = nfsrpc_readds(vp, uiop, stateidp, eofp, *dspp,
7066 off, xfer, fhp, 1, dp->nfsdi_vers,
7067 dp->nfsdi_minorvers, tcred, p);
7068 NFSCL_DEBUG(4, "readds=%d\n", error);
7069 if (error != 0 && error != EACCES && error != ESTALE) {
7070 NFSCL_DEBUG(4, "DS layreterr for read\n");
7071 nfscl_dserr(NFSV4OP_READ, error, dp, lyp,
7072 *dspp);
7073 }
7074 } else {
7075 if (flp->nfsfl_mirrorcnt == 1) {
7076 error = nfsrpc_writeds(vp, uiop, iomode,
7077 must_commit, stateidp, *dspp, off, xfer,
7078 fhp, 0, 1, dp->nfsdi_vers,
7079 dp->nfsdi_minorvers, tcred, p);
7080 if (error == 0) {
7081 NFSLOCKCLSTATE();
7082 lyp->nfsly_flags |= NFSLY_WRITTEN;
7083 NFSUNLOCKCLSTATE();
7084 }
7085 } else {
7086 m = mp;
7087 if (xfer < len) {
7088 /* The mbuf list must be split. */
7089 m2 = nfsm_split(mp, xfer);
7090 if (m2 != NULL)
7091 mp = m2;
7092 else {
7093 m_freem(mp);
7094 error = EIO;
7095 }
7096 }
7097 NFSCL_DEBUG(4, "mcopy len=%jd xfer=%jd\n",
7098 (uintmax_t)len, (uintmax_t)xfer);
7099 /*
7100 * Do last write to a mirrored DS with this
7101 * thread.
7102 */
7103 if (error == 0) {
7104 if (mirror < flp->nfsfl_mirrorcnt - 1)
7105 error = nfsio_writedsmir(vp,
7106 iomode, must_commit,
7107 stateidp, *dspp, off,
7108 xfer, fhp, m,
7109 dp->nfsdi_vers,
7110 dp->nfsdi_minorvers, drpc,
7111 tcred, p);
7112 else
7113 error = nfsrpc_writedsmir(vp,
7114 iomode, must_commit,
7115 stateidp, *dspp, off,
7116 xfer, fhp, m,
7117 dp->nfsdi_vers,
7118 dp->nfsdi_minorvers, tcred,
7119 p);
7120 }
7121 NFSCL_DEBUG(4, "nfsio_writedsmir=%d\n", error);
7122 if (error != 0 && error != EACCES && error !=
7123 ESTALE) {
7124 NFSCL_DEBUG(4,
7125 "DS layreterr for write\n");
7126 nfscl_dserr(NFSV4OP_WRITE, error, dp,
7127 lyp, *dspp);
7128 }
7129 }
7130 }
7131 NFSCL_DEBUG(4, "aft read/writeds=%d\n", error);
7132 if (error == 0) {
7133 len -= xfer;
7134 off += xfer;
7135 }
7136 if ((dp->nfsdi_flags & NFSDI_TIGHTCOUPLED) == 0)
7137 NFSFREECRED(tcred);
7138 }
7139 NFSCL_DEBUG(4, "eo nfscl_dofflayoutio=%d\n", error);
7140 return (error);
7141 }
7142
7143 /*
7144 * The actual read RPC done to a DS.
7145 */
7146 static int
nfsrpc_readds(vnode_t vp,struct uio * uiop,nfsv4stateid_t * stateidp,int * eofp,struct nfsclds * dsp,uint64_t io_off,int len,struct nfsfh * fhp,int flex,int vers,int minorvers,struct ucred * cred,NFSPROC_T * p)7147 nfsrpc_readds(vnode_t vp, struct uio *uiop, nfsv4stateid_t *stateidp, int *eofp,
7148 struct nfsclds *dsp, uint64_t io_off, int len, struct nfsfh *fhp, int flex,
7149 int vers, int minorvers, struct ucred *cred, NFSPROC_T *p)
7150 {
7151 uint32_t *tl;
7152 int attrflag, error, retlen;
7153 struct nfsrv_descript nfsd;
7154 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
7155 struct nfsrv_descript *nd = &nfsd;
7156 struct nfssockreq *nrp;
7157 struct nfsvattr na;
7158
7159 nd->nd_mrep = NULL;
7160 if (vers == 0 || vers == NFS_VER4) {
7161 nfscl_reqstart(nd, NFSPROC_READDS, nmp, fhp->nfh_fh,
7162 fhp->nfh_len, NULL, &dsp->nfsclds_sess, vers, minorvers,
7163 NULL);
7164 vers = NFS_VER4;
7165 NFSCL_DEBUG(4, "nfsrpc_readds: vers4 minvers=%d\n", minorvers);
7166 if (flex != 0)
7167 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID);
7168 else
7169 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSEQIDZERO);
7170 } else {
7171 nfscl_reqstart(nd, NFSPROC_READ, nmp, fhp->nfh_fh,
7172 fhp->nfh_len, NULL, &dsp->nfsclds_sess, vers, minorvers,
7173 NULL);
7174 NFSDECRGLOBAL(nfsstatsv1.rpccnt[NFSPROC_READ]);
7175 NFSINCRGLOBAL(nfsstatsv1.rpccnt[NFSPROC_READDS]);
7176 NFSCL_DEBUG(4, "nfsrpc_readds: vers3\n");
7177 }
7178 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED * 3);
7179 txdr_hyper(io_off, tl);
7180 *(tl + 2) = txdr_unsigned(len);
7181 nrp = dsp->nfsclds_sockp;
7182 NFSCL_DEBUG(4, "nfsrpc_readds: nrp=%p\n", nrp);
7183 if (nrp == NULL)
7184 /* If NULL, use the MDS socket. */
7185 nrp = &nmp->nm_sockreq;
7186 error = newnfs_request(nd, nmp, NULL, nrp, vp, p, cred,
7187 NFS_PROG, vers, NULL, 1, NULL, &dsp->nfsclds_sess);
7188 NFSCL_DEBUG(4, "nfsrpc_readds: stat=%d err=%d\n", nd->nd_repstat,
7189 error);
7190 if (error != 0)
7191 return (error);
7192 if (vers == NFS_VER3) {
7193 error = nfscl_postop_attr(nd, &na, &attrflag);
7194 NFSCL_DEBUG(4, "nfsrpc_readds: postop=%d\n", error);
7195 if (error != 0)
7196 goto nfsmout;
7197 }
7198 if (nd->nd_repstat != 0) {
7199 error = nd->nd_repstat;
7200 goto nfsmout;
7201 }
7202 if (vers == NFS_VER3) {
7203 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
7204 *eofp = fxdr_unsigned(int, *(tl + 1));
7205 } else {
7206 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
7207 *eofp = fxdr_unsigned(int, *tl);
7208 }
7209 NFSM_STRSIZ(retlen, len);
7210 NFSCL_DEBUG(4, "nfsrpc_readds: retlen=%d eof=%d\n", retlen, *eofp);
7211 error = nfsm_mbufuio(nd, uiop, retlen);
7212 nfsmout:
7213 if (nd->nd_mrep != NULL)
7214 m_freem(nd->nd_mrep);
7215 return (error);
7216 }
7217
7218 /*
7219 * The actual write RPC done to a DS.
7220 */
7221 static int
nfsrpc_writeds(vnode_t vp,struct uio * uiop,int * iomode,int * must_commit,nfsv4stateid_t * stateidp,struct nfsclds * dsp,uint64_t io_off,int len,struct nfsfh * fhp,int commit_thru_mds,int flex,int vers,int minorvers,struct ucred * cred,NFSPROC_T * p)7222 nfsrpc_writeds(vnode_t vp, struct uio *uiop, int *iomode, int *must_commit,
7223 nfsv4stateid_t *stateidp, struct nfsclds *dsp, uint64_t io_off, int len,
7224 struct nfsfh *fhp, int commit_thru_mds, int flex, int vers, int minorvers,
7225 struct ucred *cred, NFSPROC_T *p)
7226 {
7227 uint32_t *tl;
7228 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
7229 int attrflag, error, rlen, commit, committed = NFSWRITE_FILESYNC;
7230 int32_t backup;
7231 struct nfsrv_descript nfsd;
7232 struct nfsrv_descript *nd = &nfsd;
7233 struct nfssockreq *nrp;
7234 struct nfsvattr na;
7235
7236 KASSERT(uiop->uio_iovcnt == 1, ("nfs: writerpc iovcnt > 1"));
7237 nd->nd_mrep = NULL;
7238 if (vers == 0 || vers == NFS_VER4) {
7239 nfscl_reqstart(nd, NFSPROC_WRITEDS, nmp, fhp->nfh_fh,
7240 fhp->nfh_len, NULL, &dsp->nfsclds_sess, vers, minorvers,
7241 NULL);
7242 NFSCL_DEBUG(4, "nfsrpc_writeds: vers4 minvers=%d\n", minorvers);
7243 vers = NFS_VER4;
7244 if (flex != 0)
7245 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID);
7246 else
7247 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSEQIDZERO);
7248 NFSM_BUILD(tl, uint32_t *, NFSX_HYPER + 2 * NFSX_UNSIGNED);
7249 } else {
7250 nfscl_reqstart(nd, NFSPROC_WRITE, nmp, fhp->nfh_fh,
7251 fhp->nfh_len, NULL, &dsp->nfsclds_sess, vers, minorvers,
7252 NULL);
7253 NFSDECRGLOBAL(nfsstatsv1.rpccnt[NFSPROC_WRITE]);
7254 NFSINCRGLOBAL(nfsstatsv1.rpccnt[NFSPROC_WRITEDS]);
7255 NFSCL_DEBUG(4, "nfsrpc_writeds: vers3\n");
7256 NFSM_BUILD(tl, uint32_t *, NFSX_HYPER + 3 * NFSX_UNSIGNED);
7257 }
7258 txdr_hyper(io_off, tl);
7259 tl += 2;
7260 if (vers == NFS_VER3)
7261 *tl++ = txdr_unsigned(len);
7262 *tl++ = txdr_unsigned(*iomode);
7263 *tl = txdr_unsigned(len);
7264 error = nfsm_uiombuf(nd, uiop, len);
7265 if (error != 0) {
7266 m_freem(nd->nd_mreq);
7267 return (error);
7268 }
7269 nrp = dsp->nfsclds_sockp;
7270 if (nrp == NULL)
7271 /* If NULL, use the MDS socket. */
7272 nrp = &nmp->nm_sockreq;
7273 error = newnfs_request(nd, nmp, NULL, nrp, vp, p, cred,
7274 NFS_PROG, vers, NULL, 1, NULL, &dsp->nfsclds_sess);
7275 NFSCL_DEBUG(4, "nfsrpc_writeds: err=%d stat=%d\n", error,
7276 nd->nd_repstat);
7277 if (error != 0)
7278 return (error);
7279 if (nd->nd_repstat != 0) {
7280 /*
7281 * In case the rpc gets retried, roll
7282 * the uio fields changed by nfsm_uiombuf()
7283 * back.
7284 */
7285 uiop->uio_offset -= len;
7286 uiop->uio_resid += len;
7287 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base - len;
7288 uiop->uio_iov->iov_len += len;
7289 error = nd->nd_repstat;
7290 } else {
7291 if (vers == NFS_VER3) {
7292 error = nfscl_wcc_data(nd, vp, &na, &attrflag, NULL,
7293 NULL);
7294 NFSCL_DEBUG(4, "nfsrpc_writeds: wcc_data=%d\n", error);
7295 if (error != 0)
7296 goto nfsmout;
7297 }
7298 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED + NFSX_VERF);
7299 rlen = fxdr_unsigned(int, *tl++);
7300 NFSCL_DEBUG(4, "nfsrpc_writeds: len=%d rlen=%d\n", len, rlen);
7301 if (rlen <= 0 || rlen > len) {
7302 error = NFSERR_IO;
7303 goto nfsmout;
7304 } else if (rlen < len) {
7305 backup = len - rlen;
7306 uiop->uio_iov->iov_base =
7307 (char *)uiop->uio_iov->iov_base - backup;
7308 uiop->uio_iov->iov_len += backup;
7309 uiop->uio_offset -= backup;
7310 uiop->uio_resid += backup;
7311 len = rlen;
7312 }
7313 commit = fxdr_unsigned(int, *tl++);
7314
7315 /*
7316 * Return the lowest commitment level
7317 * obtained by any of the RPCs.
7318 */
7319 if (committed == NFSWRITE_FILESYNC)
7320 committed = commit;
7321 else if (committed == NFSWRITE_DATASYNC &&
7322 commit == NFSWRITE_UNSTABLE)
7323 committed = commit;
7324 if (commit_thru_mds != 0) {
7325 NFSLOCKMNT(nmp);
7326 if (!NFSHASWRITEVERF(nmp)) {
7327 NFSBCOPY(tl, nmp->nm_verf, NFSX_VERF);
7328 NFSSETWRITEVERF(nmp);
7329 } else if (NFSBCMP(tl, nmp->nm_verf, NFSX_VERF) &&
7330 *must_commit != 2) {
7331 *must_commit = 1;
7332 NFSBCOPY(tl, nmp->nm_verf, NFSX_VERF);
7333 }
7334 NFSUNLOCKMNT(nmp);
7335 } else {
7336 NFSLOCKDS(dsp);
7337 if ((dsp->nfsclds_flags & NFSCLDS_HASWRITEVERF) == 0) {
7338 NFSBCOPY(tl, dsp->nfsclds_verf, NFSX_VERF);
7339 dsp->nfsclds_flags |= NFSCLDS_HASWRITEVERF;
7340 } else if (NFSBCMP(tl, dsp->nfsclds_verf, NFSX_VERF) &&
7341 *must_commit != 2) {
7342 *must_commit = 1;
7343 NFSBCOPY(tl, dsp->nfsclds_verf, NFSX_VERF);
7344 }
7345 NFSUNLOCKDS(dsp);
7346 }
7347 }
7348 nfsmout:
7349 if (nd->nd_mrep != NULL)
7350 m_freem(nd->nd_mrep);
7351 *iomode = committed;
7352 if (nd->nd_repstat != 0 && error == 0)
7353 error = nd->nd_repstat;
7354 return (error);
7355 }
7356
7357 /*
7358 * The actual write RPC done to a DS.
7359 * This variant is called from a separate kernel process for mirrors.
7360 * Any short write is considered an IO error.
7361 */
7362 static int
nfsrpc_writedsmir(vnode_t vp,int * iomode,int * must_commit,nfsv4stateid_t * stateidp,struct nfsclds * dsp,uint64_t io_off,int len,struct nfsfh * fhp,struct mbuf * m,int vers,int minorvers,struct ucred * cred,NFSPROC_T * p)7363 nfsrpc_writedsmir(vnode_t vp, int *iomode, int *must_commit,
7364 nfsv4stateid_t *stateidp, struct nfsclds *dsp, uint64_t io_off, int len,
7365 struct nfsfh *fhp, struct mbuf *m, int vers, int minorvers,
7366 struct ucred *cred, NFSPROC_T *p)
7367 {
7368 uint32_t *tl;
7369 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
7370 int attrflag, error, commit, committed = NFSWRITE_FILESYNC, rlen;
7371 struct nfsrv_descript nfsd;
7372 struct nfsrv_descript *nd = &nfsd;
7373 struct nfssockreq *nrp;
7374 struct nfsvattr na;
7375
7376 nd->nd_mrep = NULL;
7377 if (vers == 0 || vers == NFS_VER4) {
7378 nfscl_reqstart(nd, NFSPROC_WRITEDS, nmp, fhp->nfh_fh,
7379 fhp->nfh_len, NULL, &dsp->nfsclds_sess, vers, minorvers,
7380 NULL);
7381 vers = NFS_VER4;
7382 NFSCL_DEBUG(4, "nfsrpc_writedsmir: vers4 minvers=%d\n",
7383 minorvers);
7384 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID);
7385 NFSM_BUILD(tl, uint32_t *, NFSX_HYPER + 2 * NFSX_UNSIGNED);
7386 } else {
7387 nfscl_reqstart(nd, NFSPROC_WRITE, nmp, fhp->nfh_fh,
7388 fhp->nfh_len, NULL, &dsp->nfsclds_sess, vers, minorvers,
7389 NULL);
7390 NFSDECRGLOBAL(nfsstatsv1.rpccnt[NFSPROC_WRITE]);
7391 NFSINCRGLOBAL(nfsstatsv1.rpccnt[NFSPROC_WRITEDS]);
7392 NFSCL_DEBUG(4, "nfsrpc_writedsmir: vers3\n");
7393 NFSM_BUILD(tl, uint32_t *, NFSX_HYPER + 3 * NFSX_UNSIGNED);
7394 }
7395 txdr_hyper(io_off, tl);
7396 tl += 2;
7397 if (vers == NFS_VER3)
7398 *tl++ = txdr_unsigned(len);
7399 *tl++ = txdr_unsigned(*iomode);
7400 *tl = txdr_unsigned(len);
7401 if (len > 0) {
7402 /* Put data in mbuf chain. */
7403 nd->nd_mb->m_next = m;
7404 }
7405 nrp = dsp->nfsclds_sockp;
7406 if (nrp == NULL)
7407 /* If NULL, use the MDS socket. */
7408 nrp = &nmp->nm_sockreq;
7409 error = newnfs_request(nd, nmp, NULL, nrp, vp, p, cred,
7410 NFS_PROG, vers, NULL, 1, NULL, &dsp->nfsclds_sess);
7411 NFSCL_DEBUG(4, "nfsrpc_writedsmir: err=%d stat=%d\n", error,
7412 nd->nd_repstat);
7413 if (error != 0)
7414 return (error);
7415 if (nd->nd_repstat != 0)
7416 error = nd->nd_repstat;
7417 else {
7418 if (vers == NFS_VER3) {
7419 error = nfscl_wcc_data(nd, vp, &na, &attrflag, NULL,
7420 NULL);
7421 NFSCL_DEBUG(4, "nfsrpc_writedsmir: wcc_data=%d\n",
7422 error);
7423 if (error != 0)
7424 goto nfsmout;
7425 }
7426 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED + NFSX_VERF);
7427 rlen = fxdr_unsigned(int, *tl++);
7428 NFSCL_DEBUG(4, "nfsrpc_writedsmir: len=%d rlen=%d\n", len,
7429 rlen);
7430 if (rlen != len) {
7431 error = NFSERR_IO;
7432 NFSCL_DEBUG(4, "nfsrpc_writedsmir: len=%d rlen=%d\n",
7433 len, rlen);
7434 goto nfsmout;
7435 }
7436 commit = fxdr_unsigned(int, *tl++);
7437
7438 /*
7439 * Return the lowest commitment level
7440 * obtained by any of the RPCs.
7441 */
7442 if (committed == NFSWRITE_FILESYNC)
7443 committed = commit;
7444 else if (committed == NFSWRITE_DATASYNC &&
7445 commit == NFSWRITE_UNSTABLE)
7446 committed = commit;
7447 NFSLOCKDS(dsp);
7448 if ((dsp->nfsclds_flags & NFSCLDS_HASWRITEVERF) == 0) {
7449 NFSBCOPY(tl, dsp->nfsclds_verf, NFSX_VERF);
7450 dsp->nfsclds_flags |= NFSCLDS_HASWRITEVERF;
7451 } else if (NFSBCMP(tl, dsp->nfsclds_verf, NFSX_VERF) &&
7452 *must_commit != 2) {
7453 *must_commit = 1;
7454 NFSBCOPY(tl, dsp->nfsclds_verf, NFSX_VERF);
7455 }
7456 NFSUNLOCKDS(dsp);
7457 }
7458 nfsmout:
7459 if (nd->nd_mrep != NULL)
7460 m_freem(nd->nd_mrep);
7461 *iomode = committed;
7462 if (nd->nd_repstat != 0 && error == 0)
7463 error = nd->nd_repstat;
7464 return (error);
7465 }
7466
7467 /*
7468 * Start up the thread that will execute nfsrpc_writedsmir().
7469 */
7470 static void
start_writedsmir(void * arg,int pending)7471 start_writedsmir(void *arg, int pending)
7472 {
7473 struct nfsclwritedsdorpc *drpc;
7474
7475 drpc = (struct nfsclwritedsdorpc *)arg;
7476 drpc->err = nfsrpc_writedsmir(drpc->vp, &drpc->iomode,
7477 &drpc->must_commit, drpc->stateidp, drpc->dsp, drpc->off, drpc->len,
7478 drpc->fhp, drpc->m, drpc->vers, drpc->minorvers, drpc->cred,
7479 drpc->p);
7480 drpc->done = 1;
7481 crfree(drpc->cred);
7482 NFSCL_DEBUG(4, "start_writedsmir: err=%d\n", drpc->err);
7483 }
7484
7485 /*
7486 * Set up the write DS mirror call for the pNFS I/O thread.
7487 */
7488 static int
nfsio_writedsmir(vnode_t vp,int * iomode,int * must_commit,nfsv4stateid_t * stateidp,struct nfsclds * dsp,uint64_t off,int len,struct nfsfh * fhp,struct mbuf * m,int vers,int minorvers,struct nfsclwritedsdorpc * drpc,struct ucred * cred,NFSPROC_T * p)7489 nfsio_writedsmir(vnode_t vp, int *iomode, int *must_commit,
7490 nfsv4stateid_t *stateidp, struct nfsclds *dsp, uint64_t off, int len,
7491 struct nfsfh *fhp, struct mbuf *m, int vers, int minorvers,
7492 struct nfsclwritedsdorpc *drpc, struct ucred *cred, NFSPROC_T *p)
7493 {
7494 int error, ret;
7495
7496 error = 0;
7497 drpc->done = 0;
7498 drpc->vp = vp;
7499 drpc->iomode = *iomode;
7500 drpc->must_commit = *must_commit;
7501 drpc->stateidp = stateidp;
7502 drpc->dsp = dsp;
7503 drpc->off = off;
7504 drpc->len = len;
7505 drpc->fhp = fhp;
7506 drpc->m = m;
7507 drpc->vers = vers;
7508 drpc->minorvers = minorvers;
7509 drpc->cred = crhold(cred);
7510 drpc->p = p;
7511 drpc->inprog = 0;
7512 ret = EIO;
7513 if (nfs_pnfsiothreads != 0) {
7514 ret = nfs_pnfsio(start_writedsmir, drpc);
7515 NFSCL_DEBUG(4, "nfsio_writedsmir: nfs_pnfsio=%d\n", ret);
7516 }
7517 if (ret != 0) {
7518 error = nfsrpc_writedsmir(vp, iomode, &drpc->must_commit,
7519 stateidp, dsp, off, len, fhp, m, vers, minorvers, cred, p);
7520 crfree(drpc->cred);
7521 }
7522 NFSCL_DEBUG(4, "nfsio_writedsmir: error=%d\n", error);
7523 return (error);
7524 }
7525
7526 /*
7527 * Free up the nfsclds structure.
7528 */
7529 void
nfscl_freenfsclds(struct nfsclds * dsp)7530 nfscl_freenfsclds(struct nfsclds *dsp)
7531 {
7532 int i;
7533
7534 if (dsp == NULL)
7535 return;
7536 if (dsp->nfsclds_sockp != NULL) {
7537 NFSFREECRED(dsp->nfsclds_sockp->nr_cred);
7538 NFSFREEMUTEX(&dsp->nfsclds_sockp->nr_mtx);
7539 free(dsp->nfsclds_sockp->nr_nam, M_SONAME);
7540 free(dsp->nfsclds_sockp, M_NFSSOCKREQ);
7541 }
7542 NFSFREEMUTEX(&dsp->nfsclds_mtx);
7543 NFSFREEMUTEX(&dsp->nfsclds_sess.nfsess_mtx);
7544 for (i = 0; i < NFSV4_CBSLOTS; i++) {
7545 if (dsp->nfsclds_sess.nfsess_cbslots[i].nfssl_reply != NULL)
7546 m_freem(
7547 dsp->nfsclds_sess.nfsess_cbslots[i].nfssl_reply);
7548 }
7549 free(dsp, M_NFSCLDS);
7550 }
7551
7552 static enum nfsclds_state
nfscl_getsameserver(struct nfsmount * nmp,struct nfsclds * newdsp,struct nfsclds ** retdspp,uint32_t * sequencep)7553 nfscl_getsameserver(struct nfsmount *nmp, struct nfsclds *newdsp,
7554 struct nfsclds **retdspp, uint32_t *sequencep)
7555 {
7556 struct nfsclds *dsp;
7557 int fndseq;
7558
7559 /*
7560 * Search the list of nfsclds structures for one with the same
7561 * server.
7562 */
7563 fndseq = 0;
7564 TAILQ_FOREACH(dsp, &nmp->nm_sess, nfsclds_list) {
7565 if (dsp->nfsclds_servownlen == newdsp->nfsclds_servownlen &&
7566 dsp->nfsclds_servownlen != 0 &&
7567 !NFSBCMP(dsp->nfsclds_serverown, newdsp->nfsclds_serverown,
7568 dsp->nfsclds_servownlen) &&
7569 dsp->nfsclds_sess.nfsess_defunct == 0) {
7570 NFSCL_DEBUG(4, "fnd same fdsp=%p dsp=%p flg=0x%x\n",
7571 TAILQ_FIRST(&nmp->nm_sess), dsp,
7572 dsp->nfsclds_flags);
7573 if (fndseq == 0) {
7574 /* Get sequenceid# from first entry. */
7575 *sequencep =
7576 dsp->nfsclds_sess.nfsess_sequenceid;
7577 fndseq = 1;
7578 }
7579 /* Server major id matches. */
7580 if ((dsp->nfsclds_flags & NFSCLDS_DS) != 0) {
7581 *retdspp = dsp;
7582 return (NFSDSP_USETHISSESSION);
7583 }
7584 }
7585 }
7586 if (fndseq != 0)
7587 return (NFSDSP_SEQTHISSESSION);
7588 return (NFSDSP_NOTFOUND);
7589 }
7590
7591 /*
7592 * NFS commit rpc to a NFSv4.1 DS.
7593 */
7594 static int
nfsrpc_commitds(vnode_t vp,uint64_t offset,int cnt,struct nfsclds * dsp,struct nfsfh * fhp,int vers,int minorvers,struct ucred * cred,NFSPROC_T * p)7595 nfsrpc_commitds(vnode_t vp, uint64_t offset, int cnt, struct nfsclds *dsp,
7596 struct nfsfh *fhp, int vers, int minorvers, struct ucred *cred,
7597 NFSPROC_T *p)
7598 {
7599 uint32_t *tl;
7600 struct nfsrv_descript nfsd, *nd = &nfsd;
7601 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
7602 struct nfssockreq *nrp;
7603 struct nfsvattr na;
7604 int attrflag, error;
7605
7606 nd->nd_mrep = NULL;
7607 if (vers == 0 || vers == NFS_VER4) {
7608 nfscl_reqstart(nd, NFSPROC_COMMITDS, nmp, fhp->nfh_fh,
7609 fhp->nfh_len, NULL, &dsp->nfsclds_sess, vers, minorvers,
7610 NULL);
7611 vers = NFS_VER4;
7612 } else {
7613 nfscl_reqstart(nd, NFSPROC_COMMIT, nmp, fhp->nfh_fh,
7614 fhp->nfh_len, NULL, &dsp->nfsclds_sess, vers, minorvers,
7615 NULL);
7616 NFSDECRGLOBAL(nfsstatsv1.rpccnt[NFSPROC_COMMIT]);
7617 NFSINCRGLOBAL(nfsstatsv1.rpccnt[NFSPROC_COMMITDS]);
7618 }
7619 NFSCL_DEBUG(4, "nfsrpc_commitds: vers=%d minvers=%d\n", vers,
7620 minorvers);
7621 NFSM_BUILD(tl, uint32_t *, NFSX_HYPER + NFSX_UNSIGNED);
7622 txdr_hyper(offset, tl);
7623 tl += 2;
7624 *tl = txdr_unsigned(cnt);
7625 nrp = dsp->nfsclds_sockp;
7626 if (nrp == NULL)
7627 /* If NULL, use the MDS socket. */
7628 nrp = &nmp->nm_sockreq;
7629 error = newnfs_request(nd, nmp, NULL, nrp, vp, p, cred,
7630 NFS_PROG, vers, NULL, 1, NULL, &dsp->nfsclds_sess);
7631 NFSCL_DEBUG(4, "nfsrpc_commitds: err=%d stat=%d\n", error,
7632 nd->nd_repstat);
7633 if (error != 0)
7634 return (error);
7635 if (nd->nd_repstat == 0) {
7636 if (vers == NFS_VER3) {
7637 error = nfscl_wcc_data(nd, vp, &na, &attrflag, NULL,
7638 NULL);
7639 NFSCL_DEBUG(4, "nfsrpc_commitds: wccdata=%d\n", error);
7640 if (error != 0)
7641 goto nfsmout;
7642 }
7643 NFSM_DISSECT(tl, u_int32_t *, NFSX_VERF);
7644 NFSLOCKDS(dsp);
7645 if (NFSBCMP(tl, dsp->nfsclds_verf, NFSX_VERF)) {
7646 NFSBCOPY(tl, dsp->nfsclds_verf, NFSX_VERF);
7647 error = NFSERR_STALEWRITEVERF;
7648 }
7649 NFSUNLOCKDS(dsp);
7650 }
7651 nfsmout:
7652 if (error == 0 && nd->nd_repstat != 0)
7653 error = nd->nd_repstat;
7654 m_freem(nd->nd_mrep);
7655 return (error);
7656 }
7657
7658 /*
7659 * Start up the thread that will execute nfsrpc_commitds().
7660 */
7661 static void
start_commitds(void * arg,int pending)7662 start_commitds(void *arg, int pending)
7663 {
7664 struct nfsclwritedsdorpc *drpc;
7665
7666 drpc = (struct nfsclwritedsdorpc *)arg;
7667 drpc->err = nfsrpc_commitds(drpc->vp, drpc->off, drpc->len,
7668 drpc->dsp, drpc->fhp, drpc->vers, drpc->minorvers, drpc->cred,
7669 drpc->p);
7670 drpc->done = 1;
7671 crfree(drpc->cred);
7672 NFSCL_DEBUG(4, "start_commitds: err=%d\n", drpc->err);
7673 }
7674
7675 /*
7676 * Set up the commit DS mirror call for the pNFS I/O thread.
7677 */
7678 static int
nfsio_commitds(vnode_t vp,uint64_t offset,int cnt,struct nfsclds * dsp,struct nfsfh * fhp,int vers,int minorvers,struct nfsclwritedsdorpc * drpc,struct ucred * cred,NFSPROC_T * p)7679 nfsio_commitds(vnode_t vp, uint64_t offset, int cnt, struct nfsclds *dsp,
7680 struct nfsfh *fhp, int vers, int minorvers,
7681 struct nfsclwritedsdorpc *drpc, struct ucred *cred, NFSPROC_T *p)
7682 {
7683 int error, ret;
7684
7685 error = 0;
7686 drpc->done = 0;
7687 drpc->vp = vp;
7688 drpc->off = offset;
7689 drpc->len = cnt;
7690 drpc->dsp = dsp;
7691 drpc->fhp = fhp;
7692 drpc->vers = vers;
7693 drpc->minorvers = minorvers;
7694 drpc->cred = crhold(cred);
7695 drpc->p = p;
7696 drpc->inprog = 0;
7697 ret = EIO;
7698 if (nfs_pnfsiothreads != 0) {
7699 ret = nfs_pnfsio(start_commitds, drpc);
7700 NFSCL_DEBUG(4, "nfsio_commitds: nfs_pnfsio=%d\n", ret);
7701 }
7702 if (ret != 0) {
7703 error = nfsrpc_commitds(vp, offset, cnt, dsp, fhp, vers,
7704 minorvers, cred, p);
7705 crfree(drpc->cred);
7706 }
7707 NFSCL_DEBUG(4, "nfsio_commitds: error=%d\n", error);
7708 return (error);
7709 }
7710
7711 /*
7712 * NFS Advise rpc
7713 */
7714 int
nfsrpc_advise(vnode_t vp,off_t offset,uint64_t cnt,int advise,struct ucred * cred,NFSPROC_T * p)7715 nfsrpc_advise(vnode_t vp, off_t offset, uint64_t cnt, int advise,
7716 struct ucred *cred, NFSPROC_T *p)
7717 {
7718 u_int32_t *tl;
7719 struct nfsrv_descript nfsd, *nd = &nfsd;
7720 nfsattrbit_t hints;
7721 int error;
7722
7723 NFSZERO_ATTRBIT(&hints);
7724 if (advise == POSIX_FADV_WILLNEED)
7725 NFSSETBIT_ATTRBIT(&hints, NFSV4IOHINT_WILLNEED);
7726 else if (advise == POSIX_FADV_DONTNEED)
7727 NFSSETBIT_ATTRBIT(&hints, NFSV4IOHINT_DONTNEED);
7728 else
7729 return (0);
7730 NFSCL_REQSTART(nd, NFSPROC_IOADVISE, vp, cred);
7731 nfsm_stateidtom(nd, NULL, NFSSTATEID_PUTALLZERO);
7732 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_HYPER);
7733 txdr_hyper(offset, tl);
7734 tl += 2;
7735 txdr_hyper(cnt, tl);
7736 nfsrv_putattrbit(nd, &hints);
7737 error = nfscl_request(nd, vp, p, cred);
7738 if (error != 0)
7739 return (error);
7740 if (nd->nd_repstat != 0)
7741 error = nd->nd_repstat;
7742 m_freem(nd->nd_mrep);
7743 return (error);
7744 }
7745
7746 #ifdef notyet
7747 /*
7748 * NFS advise rpc to a NFSv4.2 DS.
7749 */
7750 static int
nfsrpc_adviseds(vnode_t vp,uint64_t offset,int cnt,int advise,struct nfsclds * dsp,struct nfsfh * fhp,int vers,int minorvers,struct ucred * cred,NFSPROC_T * p)7751 nfsrpc_adviseds(vnode_t vp, uint64_t offset, int cnt, int advise,
7752 struct nfsclds *dsp, struct nfsfh *fhp, int vers, int minorvers,
7753 struct ucred *cred, NFSPROC_T *p)
7754 {
7755 uint32_t *tl;
7756 struct nfsrv_descript nfsd, *nd = &nfsd;
7757 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
7758 struct nfssockreq *nrp;
7759 nfsattrbit_t hints;
7760 int error;
7761
7762 /* For NFS DSs prior to NFSv4.2, just return OK. */
7763 if (vers == NFS_VER3 || minorversion < NFSV42_MINORVERSION)
7764 return (0);
7765 NFSZERO_ATTRBIT(&hints);
7766 if (advise == POSIX_FADV_WILLNEED)
7767 NFSSETBIT_ATTRBIT(&hints, NFSV4IOHINT_WILLNEED);
7768 else if (advise == POSIX_FADV_DONTNEED)
7769 NFSSETBIT_ATTRBIT(&hints, NFSV4IOHINT_DONTNEED);
7770 else
7771 return (0);
7772 nd->nd_mrep = NULL;
7773 nfscl_reqstart(nd, NFSPROC_IOADVISEDS, nmp, fhp->nfh_fh,
7774 fhp->nfh_len, NULL, &dsp->nfsclds_sess, vers, minorvers, NULL);
7775 vers = NFS_VER4;
7776 NFSCL_DEBUG(4, "nfsrpc_adviseds: vers=%d minvers=%d\n", vers,
7777 minorvers);
7778 nfsm_stateidtom(nd, NULL, NFSSTATEID_PUTALLZERO);
7779 NFSM_BUILD(tl, uint32_t *, NFSX_HYPER + NFSX_UNSIGNED);
7780 txdr_hyper(offset, tl);
7781 tl += 2;
7782 *tl = txdr_unsigned(cnt);
7783 nfsrv_putattrbit(nd, &hints);
7784 nrp = dsp->nfsclds_sockp;
7785 if (nrp == NULL)
7786 /* If NULL, use the MDS socket. */
7787 nrp = &nmp->nm_sockreq;
7788 error = newnfs_request(nd, nmp, NULL, nrp, vp, p, cred,
7789 NFS_PROG, vers, NULL, 1, NULL, &dsp->nfsclds_sess);
7790 NFSCL_DEBUG(4, "nfsrpc_adviseds: err=%d stat=%d\n", error,
7791 nd->nd_repstat);
7792 if (error != 0)
7793 return (error);
7794 if (nd->nd_repstat != 0)
7795 error = nd->nd_repstat;
7796 m_freem(nd->nd_mrep);
7797 return (error);
7798 }
7799
7800 /*
7801 * Start up the thread that will execute nfsrpc_commitds().
7802 */
7803 static void
start_adviseds(void * arg,int pending)7804 start_adviseds(void *arg, int pending)
7805 {
7806 struct nfsclwritedsdorpc *drpc;
7807
7808 drpc = (struct nfsclwritedsdorpc *)arg;
7809 drpc->err = nfsrpc_adviseds(drpc->vp, drpc->off, drpc->len,
7810 drpc->advise, drpc->dsp, drpc->fhp, drpc->vers, drpc->minorvers,
7811 drpc->cred, drpc->p);
7812 drpc->done = 1;
7813 crfree(drpc->cred);
7814 NFSCL_DEBUG(4, "start_adviseds: err=%d\n", drpc->err);
7815 }
7816
7817 /*
7818 * Set up the advise DS mirror call for the pNFS I/O thread.
7819 */
7820 static int
nfsio_adviseds(vnode_t vp,uint64_t offset,int cnt,int advise,struct nfsclds * dsp,struct nfsfh * fhp,int vers,int minorvers,struct nfsclwritedsdorpc * drpc,struct ucred * cred,NFSPROC_T * p)7821 nfsio_adviseds(vnode_t vp, uint64_t offset, int cnt, int advise,
7822 struct nfsclds *dsp, struct nfsfh *fhp, int vers, int minorvers,
7823 struct nfsclwritedsdorpc *drpc, struct ucred *cred, NFSPROC_T *p)
7824 {
7825 int error, ret;
7826
7827 error = 0;
7828 drpc->done = 0;
7829 drpc->vp = vp;
7830 drpc->off = offset;
7831 drpc->len = cnt;
7832 drpc->advise = advise;
7833 drpc->dsp = dsp;
7834 drpc->fhp = fhp;
7835 drpc->vers = vers;
7836 drpc->minorvers = minorvers;
7837 drpc->cred = crhold(cred);
7838 drpc->p = p;
7839 drpc->inprog = 0;
7840 ret = EIO;
7841 if (nfs_pnfsiothreads != 0) {
7842 ret = nfs_pnfsio(start_adviseds, drpc);
7843 NFSCL_DEBUG(4, "nfsio_adviseds: nfs_pnfsio=%d\n", ret);
7844 }
7845 if (ret != 0) {
7846 error = nfsrpc_adviseds(vp, offset, cnt, advise, dsp, fhp, vers,
7847 minorvers, cred, p);
7848 crfree(drpc->cred);
7849 }
7850 NFSCL_DEBUG(4, "nfsio_adviseds: error=%d\n", error);
7851 return (error);
7852 }
7853 #endif /* notyet */
7854
7855 /*
7856 * Do the Allocate operation, retrying for recovery.
7857 */
7858 int
nfsrpc_allocate(vnode_t vp,off_t off,off_t len,struct nfsvattr * nap,int * attrflagp,struct ucred * cred,NFSPROC_T * p)7859 nfsrpc_allocate(vnode_t vp, off_t off, off_t len, struct nfsvattr *nap,
7860 int *attrflagp, struct ucred *cred, NFSPROC_T *p)
7861 {
7862 int error, expireret = 0, retrycnt, nostateid;
7863 uint32_t clidrev = 0;
7864 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
7865 struct nfsfh *nfhp = NULL;
7866 nfsv4stateid_t stateid;
7867 off_t tmp_off;
7868 void *lckp;
7869
7870 if (len < 0)
7871 return (EINVAL);
7872 if (len == 0)
7873 return (0);
7874 tmp_off = off + len;
7875 NFSLOCKMNT(nmp);
7876 if (tmp_off > nmp->nm_maxfilesize || tmp_off < off) {
7877 NFSUNLOCKMNT(nmp);
7878 return (EFBIG);
7879 }
7880 if (nmp->nm_clp != NULL)
7881 clidrev = nmp->nm_clp->nfsc_clientidrev;
7882 NFSUNLOCKMNT(nmp);
7883 nfhp = VTONFS(vp)->n_fhp;
7884 retrycnt = 0;
7885 do {
7886 lckp = NULL;
7887 nostateid = 0;
7888 nfscl_getstateid(vp, nfhp->nfh_fh, nfhp->nfh_len,
7889 NFSV4OPEN_ACCESSWRITE, 0, cred, p, &stateid, &lckp);
7890 if (stateid.other[0] == 0 && stateid.other[1] == 0 &&
7891 stateid.other[2] == 0) {
7892 nostateid = 1;
7893 NFSCL_DEBUG(1, "stateid0 in allocate\n");
7894 }
7895
7896 /*
7897 * Not finding a stateid should probably never happen,
7898 * but just return an error for this case.
7899 */
7900 if (nostateid != 0)
7901 error = EIO;
7902 else
7903 error = nfsrpc_allocaterpc(vp, off, len, &stateid,
7904 nap, attrflagp, cred, p);
7905 if (error == NFSERR_STALESTATEID)
7906 nfscl_initiate_recovery(nmp->nm_clp);
7907 if (lckp != NULL)
7908 nfscl_lockderef(lckp);
7909 if (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
7910 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
7911 error == NFSERR_OLDSTATEID || error == NFSERR_BADSESSION) {
7912 (void) nfs_catnap(PZERO, error, "nfs_allocate");
7913 } else if ((error == NFSERR_EXPIRED || (!NFSHASINT(nmp) &&
7914 error == NFSERR_BADSTATEID)) && clidrev != 0) {
7915 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p);
7916 } else if (error == NFSERR_BADSTATEID && NFSHASINT(nmp)) {
7917 error = EIO;
7918 }
7919 retrycnt++;
7920 } while (error == NFSERR_GRACE || error == NFSERR_DELAY ||
7921 error == NFSERR_STALESTATEID || error == NFSERR_BADSESSION ||
7922 error == NFSERR_STALEDONTRECOVER ||
7923 (error == NFSERR_OLDSTATEID && retrycnt < 20) ||
7924 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
7925 expireret == 0 && clidrev != 0 && retrycnt < 4));
7926 if (error != 0 && retrycnt >= 4)
7927 error = EIO;
7928 return (error);
7929 }
7930
7931 /*
7932 * The allocate RPC.
7933 */
7934 static int
nfsrpc_allocaterpc(vnode_t vp,off_t off,off_t len,nfsv4stateid_t * stateidp,struct nfsvattr * nap,int * attrflagp,struct ucred * cred,NFSPROC_T * p)7935 nfsrpc_allocaterpc(vnode_t vp, off_t off, off_t len, nfsv4stateid_t *stateidp,
7936 struct nfsvattr *nap, int *attrflagp, struct ucred *cred, NFSPROC_T *p)
7937 {
7938 uint32_t *tl;
7939 int error;
7940 struct nfsrv_descript nfsd;
7941 struct nfsrv_descript *nd = &nfsd;
7942 nfsattrbit_t attrbits;
7943
7944 *attrflagp = 0;
7945 NFSCL_REQSTART(nd, NFSPROC_ALLOCATE, vp, cred);
7946 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID);
7947 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_HYPER + NFSX_UNSIGNED);
7948 txdr_hyper(off, tl); tl += 2;
7949 txdr_hyper(len, tl); tl += 2;
7950 *tl = txdr_unsigned(NFSV4OP_GETATTR);
7951 NFSGETATTR_ATTRBIT(&attrbits);
7952 nfsrv_putattrbit(nd, &attrbits);
7953 error = nfscl_request(nd, vp, p, cred);
7954 if (error != 0)
7955 return (error);
7956 if (nd->nd_repstat == 0) {
7957 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
7958 error = nfsm_loadattr(nd, nap);
7959 if (error == 0)
7960 *attrflagp = NFS_LATTR_NOSHRINK;
7961 } else
7962 error = nd->nd_repstat;
7963 nfsmout:
7964 m_freem(nd->nd_mrep);
7965 return (error);
7966 }
7967
7968 /*
7969 * Set up the XDR arguments for the LayoutGet operation.
7970 */
7971 static void
nfsrv_setuplayoutget(struct nfsrv_descript * nd,int iomode,uint64_t offset,uint64_t len,uint64_t minlen,nfsv4stateid_t * stateidp,int layouttype,int layoutlen,int usecurstateid)7972 nfsrv_setuplayoutget(struct nfsrv_descript *nd, int iomode, uint64_t offset,
7973 uint64_t len, uint64_t minlen, nfsv4stateid_t *stateidp, int layouttype,
7974 int layoutlen, int usecurstateid)
7975 {
7976 uint32_t *tl;
7977
7978 NFSM_BUILD(tl, uint32_t *, 4 * NFSX_UNSIGNED + 3 * NFSX_HYPER +
7979 NFSX_STATEID);
7980 *tl++ = newnfs_false; /* Don't signal availability. */
7981 *tl++ = txdr_unsigned(layouttype);
7982 *tl++ = txdr_unsigned(iomode);
7983 txdr_hyper(offset, tl);
7984 tl += 2;
7985 txdr_hyper(len, tl);
7986 tl += 2;
7987 txdr_hyper(minlen, tl);
7988 tl += 2;
7989 if (usecurstateid != 0) {
7990 /* Special stateid for Current stateid. */
7991 *tl++ = txdr_unsigned(1);
7992 *tl++ = 0;
7993 *tl++ = 0;
7994 *tl++ = 0;
7995 } else {
7996 *tl++ = txdr_unsigned(stateidp->seqid);
7997 NFSCL_DEBUG(4, "layget seq=%d\n", (int)stateidp->seqid);
7998 *tl++ = stateidp->other[0];
7999 *tl++ = stateidp->other[1];
8000 *tl++ = stateidp->other[2];
8001 }
8002 *tl = txdr_unsigned(layoutlen);
8003 }
8004
8005 /*
8006 * Parse the reply for a successful LayoutGet operation.
8007 */
8008 static int
nfsrv_parselayoutget(struct nfsmount * nmp,struct nfsrv_descript * nd,nfsv4stateid_t * stateidp,int * retonclosep,struct nfsclflayouthead * flhp)8009 nfsrv_parselayoutget(struct nfsmount *nmp, struct nfsrv_descript *nd,
8010 nfsv4stateid_t *stateidp, int *retonclosep, struct nfsclflayouthead *flhp)
8011 {
8012 uint32_t *tl;
8013 struct nfsclflayout *flp, *prevflp, *tflp;
8014 int cnt, error, fhcnt, gotiomode, i, iomode, j, k, l, laytype, nfhlen;
8015 int m, mirrorcnt;
8016 uint64_t retlen, off;
8017 struct nfsfh *nfhp;
8018 uint8_t *cp;
8019 uid_t user;
8020 gid_t grp;
8021
8022 NFSCL_DEBUG(4, "in nfsrv_parselayoutget\n");
8023 error = 0;
8024 flp = NULL;
8025 gotiomode = -1;
8026 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED + NFSX_STATEID);
8027 if (*tl++ != 0)
8028 *retonclosep = 1;
8029 else
8030 *retonclosep = 0;
8031 stateidp->seqid = fxdr_unsigned(uint32_t, *tl++);
8032 NFSCL_DEBUG(4, "retoncls=%d stseq=%d\n", *retonclosep,
8033 (int)stateidp->seqid);
8034 stateidp->other[0] = *tl++;
8035 stateidp->other[1] = *tl++;
8036 stateidp->other[2] = *tl++;
8037 cnt = fxdr_unsigned(int, *tl);
8038 NFSCL_DEBUG(4, "layg cnt=%d\n", cnt);
8039 if (cnt <= 0 || cnt > 10000) {
8040 /* Don't accept more than 10000 layouts in reply. */
8041 error = NFSERR_BADXDR;
8042 goto nfsmout;
8043 }
8044 for (i = 0; i < cnt; i++) {
8045 /* Dissect to the layout type. */
8046 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_HYPER +
8047 3 * NFSX_UNSIGNED);
8048 off = fxdr_hyper(tl); tl += 2;
8049 retlen = fxdr_hyper(tl); tl += 2;
8050 iomode = fxdr_unsigned(int, *tl++);
8051 laytype = fxdr_unsigned(int, *tl);
8052 NFSCL_DEBUG(4, "layt=%d off=%ju len=%ju iom=%d\n", laytype,
8053 (uintmax_t)off, (uintmax_t)retlen, iomode);
8054 /* Ignore length of layout body for now. */
8055 if (laytype == NFSLAYOUT_NFSV4_1_FILES) {
8056 /* Parse the File layout up to fhcnt. */
8057 NFSM_DISSECT(tl, uint32_t *, 3 * NFSX_UNSIGNED +
8058 NFSX_HYPER + NFSX_V4DEVICEID);
8059 fhcnt = fxdr_unsigned(int, *(tl + 4 +
8060 NFSX_V4DEVICEID / NFSX_UNSIGNED));
8061 NFSCL_DEBUG(4, "fhcnt=%d\n", fhcnt);
8062 if (fhcnt < 0 || fhcnt > 100) {
8063 /* Don't accept more than 100 file handles. */
8064 error = NFSERR_BADXDR;
8065 goto nfsmout;
8066 }
8067 if (fhcnt > 0)
8068 flp = malloc(sizeof(*flp) + fhcnt *
8069 sizeof(struct nfsfh *), M_NFSFLAYOUT,
8070 M_WAITOK);
8071 else
8072 flp = malloc(sizeof(*flp), M_NFSFLAYOUT,
8073 M_WAITOK);
8074 flp->nfsfl_flags = NFSFL_FILE;
8075 flp->nfsfl_fhcnt = 0;
8076 flp->nfsfl_devp = NULL;
8077 flp->nfsfl_off = off;
8078 if (flp->nfsfl_off + retlen < flp->nfsfl_off)
8079 flp->nfsfl_end = UINT64_MAX - flp->nfsfl_off;
8080 else
8081 flp->nfsfl_end = flp->nfsfl_off + retlen;
8082 flp->nfsfl_iomode = iomode;
8083 if (gotiomode == -1)
8084 gotiomode = flp->nfsfl_iomode;
8085 /* Ignore layout body length for now. */
8086 NFSBCOPY(tl, flp->nfsfl_dev, NFSX_V4DEVICEID);
8087 tl += (NFSX_V4DEVICEID / NFSX_UNSIGNED);
8088 flp->nfsfl_util = fxdr_unsigned(uint32_t, *tl++);
8089 NFSCL_DEBUG(4, "flutil=0x%x\n", flp->nfsfl_util);
8090 mtx_lock(&nmp->nm_mtx);
8091 if (nmp->nm_minorvers > 1 && (flp->nfsfl_util &
8092 NFSFLAYUTIL_IOADVISE_THRU_MDS) != 0)
8093 nmp->nm_privflag |= NFSMNTP_IOADVISETHRUMDS;
8094 mtx_unlock(&nmp->nm_mtx);
8095 flp->nfsfl_stripe1 = fxdr_unsigned(uint32_t, *tl++);
8096 flp->nfsfl_patoff = fxdr_hyper(tl); tl += 2;
8097 NFSCL_DEBUG(4, "stripe1=%u poff=%ju\n",
8098 flp->nfsfl_stripe1, (uintmax_t)flp->nfsfl_patoff);
8099 for (j = 0; j < fhcnt; j++) {
8100 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
8101 nfhlen = fxdr_unsigned(int, *tl);
8102 if (nfhlen <= 0 || nfhlen > NFSX_V4FHMAX) {
8103 error = NFSERR_BADXDR;
8104 goto nfsmout;
8105 }
8106 nfhp = malloc(sizeof(*nfhp) + nfhlen - 1,
8107 M_NFSFH, M_WAITOK);
8108 flp->nfsfl_fh[j] = nfhp;
8109 flp->nfsfl_fhcnt++;
8110 nfhp->nfh_len = nfhlen;
8111 NFSM_DISSECT(cp, uint8_t *, NFSM_RNDUP(nfhlen));
8112 NFSBCOPY(cp, nfhp->nfh_fh, nfhlen);
8113 }
8114 } else if (laytype == NFSLAYOUT_FLEXFILE) {
8115 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED +
8116 NFSX_HYPER);
8117 mirrorcnt = fxdr_unsigned(int, *(tl + 2));
8118 NFSCL_DEBUG(4, "mirrorcnt=%d\n", mirrorcnt);
8119 if (mirrorcnt < 1 || mirrorcnt > NFSDEV_MAXMIRRORS) {
8120 error = NFSERR_BADXDR;
8121 goto nfsmout;
8122 }
8123 flp = malloc(sizeof(*flp) + mirrorcnt *
8124 sizeof(struct nfsffm), M_NFSFLAYOUT, M_WAITOK);
8125 flp->nfsfl_flags = NFSFL_FLEXFILE;
8126 flp->nfsfl_mirrorcnt = mirrorcnt;
8127 for (j = 0; j < mirrorcnt; j++)
8128 flp->nfsfl_ffm[j].devp = NULL;
8129 flp->nfsfl_off = off;
8130 if (flp->nfsfl_off + retlen < flp->nfsfl_off)
8131 flp->nfsfl_end = UINT64_MAX - flp->nfsfl_off;
8132 else
8133 flp->nfsfl_end = flp->nfsfl_off + retlen;
8134 flp->nfsfl_iomode = iomode;
8135 if (gotiomode == -1)
8136 gotiomode = flp->nfsfl_iomode;
8137 flp->nfsfl_stripeunit = fxdr_hyper(tl);
8138 NFSCL_DEBUG(4, "stripeunit=%ju\n",
8139 (uintmax_t)flp->nfsfl_stripeunit);
8140 for (j = 0; j < mirrorcnt; j++) {
8141 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
8142 k = fxdr_unsigned(int, *tl);
8143 if (k < 1 || k > 128) {
8144 error = NFSERR_BADXDR;
8145 goto nfsmout;
8146 }
8147 NFSCL_DEBUG(4, "servercnt=%d\n", k);
8148 for (l = 0; l < k; l++) {
8149 NFSM_DISSECT(tl, uint32_t *,
8150 NFSX_V4DEVICEID + NFSX_STATEID +
8151 2 * NFSX_UNSIGNED);
8152 if (l == 0) {
8153 /* Just use the first server. */
8154 NFSBCOPY(tl,
8155 flp->nfsfl_ffm[j].dev,
8156 NFSX_V4DEVICEID);
8157 tl += (NFSX_V4DEVICEID /
8158 NFSX_UNSIGNED);
8159 tl++;
8160 flp->nfsfl_ffm[j].st.seqid =
8161 *tl++;
8162 flp->nfsfl_ffm[j].st.other[0] =
8163 *tl++;
8164 flp->nfsfl_ffm[j].st.other[1] =
8165 *tl++;
8166 flp->nfsfl_ffm[j].st.other[2] =
8167 *tl++;
8168 NFSCL_DEBUG(4, "st.seqid=%u "
8169 "st.o0=0x%x st.o1=0x%x "
8170 "st.o2=0x%x\n",
8171 flp->nfsfl_ffm[j].st.seqid,
8172 flp->nfsfl_ffm[j].st.other[0],
8173 flp->nfsfl_ffm[j].st.other[1],
8174 flp->nfsfl_ffm[j].st.other[2]);
8175 } else
8176 tl += ((NFSX_V4DEVICEID +
8177 NFSX_STATEID +
8178 NFSX_UNSIGNED) /
8179 NFSX_UNSIGNED);
8180 fhcnt = fxdr_unsigned(int, *tl);
8181 NFSCL_DEBUG(4, "fhcnt=%d\n", fhcnt);
8182 if (fhcnt < 1 ||
8183 fhcnt > NFSDEV_MAXVERS) {
8184 error = NFSERR_BADXDR;
8185 goto nfsmout;
8186 }
8187 for (m = 0; m < fhcnt; m++) {
8188 NFSM_DISSECT(tl, uint32_t *,
8189 NFSX_UNSIGNED);
8190 nfhlen = fxdr_unsigned(int,
8191 *tl);
8192 NFSCL_DEBUG(4, "nfhlen=%d\n",
8193 nfhlen);
8194 if (nfhlen <= 0 || nfhlen >
8195 NFSX_V4FHMAX) {
8196 error = NFSERR_BADXDR;
8197 goto nfsmout;
8198 }
8199 NFSM_DISSECT(cp, uint8_t *,
8200 NFSM_RNDUP(nfhlen));
8201 if (l == 0) {
8202 flp->nfsfl_ffm[j].fhcnt
8203 = fhcnt;
8204 nfhp = malloc(
8205 sizeof(*nfhp) +
8206 nfhlen - 1, M_NFSFH,
8207 M_WAITOK);
8208 flp->nfsfl_ffm[j].fh[m]
8209 = nfhp;
8210 nfhp->nfh_len = nfhlen;
8211 NFSBCOPY(cp,
8212 nfhp->nfh_fh,
8213 nfhlen);
8214 NFSCL_DEBUG(4,
8215 "got fh\n");
8216 }
8217 }
8218 /* Now, get the ffsd_user/ffds_group. */
8219 error = nfsrv_parseug(nd, 0, &user,
8220 &grp, curthread);
8221 NFSCL_DEBUG(4, "after parseu=%d\n",
8222 error);
8223 if (error == 0)
8224 error = nfsrv_parseug(nd, 1,
8225 &user, &grp, curthread);
8226 NFSCL_DEBUG(4, "aft parseg=%d\n",
8227 grp);
8228 if (error != 0)
8229 goto nfsmout;
8230 NFSCL_DEBUG(4, "user=%d group=%d\n",
8231 user, grp);
8232 if (l == 0) {
8233 flp->nfsfl_ffm[j].user = user;
8234 flp->nfsfl_ffm[j].group = grp;
8235 NFSCL_DEBUG(4,
8236 "usr=%d grp=%d\n", user,
8237 grp);
8238 }
8239 }
8240 }
8241 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
8242 flp->nfsfl_fflags = fxdr_unsigned(uint32_t, *tl++);
8243 #ifdef notnow
8244 /*
8245 * At this time, there is no flag.
8246 * NFSFLEXFLAG_IOADVISE_THRU_MDS might need to be
8247 * added, or it may never exist?
8248 */
8249 mtx_lock(&nmp->nm_mtx);
8250 if (nmp->nm_minorvers > 1 && (flp->nfsfl_fflags &
8251 NFSFLEXFLAG_IOADVISE_THRU_MDS) != 0)
8252 nmp->nm_privflag |= NFSMNTP_IOADVISETHRUMDS;
8253 mtx_unlock(&nmp->nm_mtx);
8254 #endif
8255 flp->nfsfl_statshint = fxdr_unsigned(uint32_t, *tl);
8256 NFSCL_DEBUG(4, "fflags=0x%x statshint=%d\n",
8257 flp->nfsfl_fflags, flp->nfsfl_statshint);
8258 } else {
8259 error = NFSERR_BADXDR;
8260 goto nfsmout;
8261 }
8262 if (flp->nfsfl_iomode == gotiomode) {
8263 /* Keep the list in increasing offset order. */
8264 tflp = LIST_FIRST(flhp);
8265 prevflp = NULL;
8266 while (tflp != NULL &&
8267 tflp->nfsfl_off < flp->nfsfl_off) {
8268 prevflp = tflp;
8269 tflp = LIST_NEXT(tflp, nfsfl_list);
8270 }
8271 if (prevflp == NULL)
8272 LIST_INSERT_HEAD(flhp, flp, nfsfl_list);
8273 else
8274 LIST_INSERT_AFTER(prevflp, flp,
8275 nfsfl_list);
8276 NFSCL_DEBUG(4, "flp inserted\n");
8277 } else {
8278 printf("nfscl_layoutget(): got wrong iomode\n");
8279 nfscl_freeflayout(flp);
8280 }
8281 flp = NULL;
8282 }
8283 nfsmout:
8284 NFSCL_DEBUG(4, "eo nfsrv_parselayoutget=%d\n", error);
8285 if (error != 0 && flp != NULL)
8286 nfscl_freeflayout(flp);
8287 return (error);
8288 }
8289
8290 /*
8291 * Parse a user/group digit string.
8292 */
8293 static int
nfsrv_parseug(struct nfsrv_descript * nd,int dogrp,uid_t * uidp,gid_t * gidp,NFSPROC_T * p)8294 nfsrv_parseug(struct nfsrv_descript *nd, int dogrp, uid_t *uidp, gid_t *gidp,
8295 NFSPROC_T *p)
8296 {
8297 uint32_t *tl;
8298 char *str, str0[NFSV4_SMALLSTR + 1];
8299 uint32_t len = 0;
8300 int error = 0;
8301
8302 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
8303 len = fxdr_unsigned(uint32_t, *tl);
8304 str = NULL;
8305 if (len > NFSV4_OPAQUELIMIT) {
8306 error = NFSERR_BADXDR;
8307 goto nfsmout;
8308 }
8309 NFSCL_DEBUG(4, "nfsrv_parseug: len=%d\n", len);
8310 if (len == 0) {
8311 if (dogrp != 0)
8312 *gidp = GID_NOGROUP;
8313 else
8314 *uidp = UID_NOBODY;
8315 return (0);
8316 }
8317 if (len > NFSV4_SMALLSTR)
8318 str = malloc(len + 1, M_TEMP, M_WAITOK);
8319 else
8320 str = str0;
8321 error = nfsrv_mtostr(nd, str, len);
8322 if (error != 0)
8323 goto nfsmout;
8324 NFSCL_DEBUG(4, "nfsrv_parseug: str=%s\n", str);
8325 if (dogrp != 0)
8326 error = nfsv4_strtogid(nd, str, len, gidp);
8327 else
8328 error = nfsv4_strtouid(nd, str, len, uidp);
8329 nfsmout:
8330 if (len > NFSV4_SMALLSTR)
8331 free(str, M_TEMP);
8332 NFSCL_DEBUG(4, "eo nfsrv_parseug=%d\n", error);
8333 return (error);
8334 }
8335
8336 /*
8337 * Similar to nfsrpc_getlayout(), except that it uses nfsrpc_openlayget(),
8338 * so that it does both an Open and a Layoutget.
8339 */
8340 static int
nfsrpc_getopenlayout(struct nfsmount * nmp,vnode_t vp,u_int8_t * nfhp,int fhlen,uint8_t * newfhp,int newfhlen,uint32_t mode,struct nfsclopen * op,uint8_t * name,int namelen,struct nfscldeleg ** dpp,struct ucred * cred,NFSPROC_T * p)8341 nfsrpc_getopenlayout(struct nfsmount *nmp, vnode_t vp, u_int8_t *nfhp,
8342 int fhlen, uint8_t *newfhp, int newfhlen, uint32_t mode,
8343 struct nfsclopen *op, uint8_t *name, int namelen, struct nfscldeleg **dpp,
8344 struct ucred *cred, NFSPROC_T *p)
8345 {
8346 struct nfscllayout *lyp;
8347 struct nfsclflayout *flp;
8348 struct nfsclflayouthead flh;
8349 int error, islocked, layoutlen, recalled, retonclose, usecurstateid;
8350 int layouttype, laystat;
8351 nfsv4stateid_t stateid;
8352 struct nfsclsession *tsep;
8353
8354 error = 0;
8355 if (NFSHASFLEXFILE(nmp))
8356 layouttype = NFSLAYOUT_FLEXFILE;
8357 else
8358 layouttype = NFSLAYOUT_NFSV4_1_FILES;
8359 /*
8360 * If lyp is returned non-NULL, there will be a refcnt (shared lock)
8361 * on it, iff flp != NULL or a lock (exclusive lock) on it iff
8362 * flp == NULL.
8363 */
8364 lyp = nfscl_getlayout(nmp->nm_clp, newfhp, newfhlen, 0, mode, &flp,
8365 &recalled);
8366 NFSCL_DEBUG(4, "nfsrpc_getopenlayout nfscl_getlayout lyp=%p\n", lyp);
8367 if (lyp == NULL)
8368 islocked = 0;
8369 else if (flp != NULL)
8370 islocked = 1;
8371 else
8372 islocked = 2;
8373 if ((lyp == NULL || flp == NULL) && recalled == 0) {
8374 LIST_INIT(&flh);
8375 tsep = nfsmnt_mdssession(nmp);
8376 layoutlen = tsep->nfsess_maxcache - (NFSX_STATEID +
8377 3 * NFSX_UNSIGNED);
8378 if (lyp == NULL)
8379 usecurstateid = 1;
8380 else {
8381 usecurstateid = 0;
8382 stateid.seqid = lyp->nfsly_stateid.seqid;
8383 stateid.other[0] = lyp->nfsly_stateid.other[0];
8384 stateid.other[1] = lyp->nfsly_stateid.other[1];
8385 stateid.other[2] = lyp->nfsly_stateid.other[2];
8386 }
8387 error = nfsrpc_openlayoutrpc(nmp, vp, nfhp, fhlen,
8388 newfhp, newfhlen, mode, op, name, namelen,
8389 dpp, &stateid, usecurstateid, layouttype, layoutlen,
8390 &retonclose, &flh, &laystat, cred, p);
8391 NFSCL_DEBUG(4, "aft nfsrpc_openlayoutrpc laystat=%d err=%d\n",
8392 laystat, error);
8393 laystat = nfsrpc_layoutgetres(nmp, vp, newfhp, newfhlen,
8394 &stateid, retonclose, NULL, &lyp, &flh, layouttype, laystat,
8395 &islocked, cred, p);
8396 } else
8397 error = nfsrpc_openrpc(nmp, vp, nfhp, fhlen, newfhp, newfhlen,
8398 mode, op, name, namelen, dpp, 0, 0, cred, p, 0, 0);
8399 if (islocked == 2)
8400 nfscl_rellayout(lyp, 1);
8401 else if (islocked == 1)
8402 nfscl_rellayout(lyp, 0);
8403 return (error);
8404 }
8405
8406 /*
8407 * This function does an Open+LayoutGet for an NFSv4.1 mount with pNFS
8408 * enabled, only for the CLAIM_NULL case. All other NFSv4 Opens are
8409 * handled by nfsrpc_openrpc().
8410 * For the case where op == NULL, dvp is the directory. When op != NULL, it
8411 * can be NULL.
8412 */
8413 static int
nfsrpc_openlayoutrpc(struct nfsmount * nmp,vnode_t vp,u_int8_t * nfhp,int fhlen,uint8_t * newfhp,int newfhlen,uint32_t mode,struct nfsclopen * op,uint8_t * name,int namelen,struct nfscldeleg ** dpp,nfsv4stateid_t * stateidp,int usecurstateid,int layouttype,int layoutlen,int * retonclosep,struct nfsclflayouthead * flhp,int * laystatp,struct ucred * cred,NFSPROC_T * p)8414 nfsrpc_openlayoutrpc(struct nfsmount *nmp, vnode_t vp, u_int8_t *nfhp,
8415 int fhlen, uint8_t *newfhp, int newfhlen, uint32_t mode,
8416 struct nfsclopen *op, uint8_t *name, int namelen, struct nfscldeleg **dpp,
8417 nfsv4stateid_t *stateidp, int usecurstateid, int layouttype,
8418 int layoutlen, int *retonclosep, struct nfsclflayouthead *flhp,
8419 int *laystatp, struct ucred *cred, NFSPROC_T *p)
8420 {
8421 uint32_t *tl;
8422 struct nfsrv_descript nfsd, *nd = &nfsd;
8423 struct nfscldeleg *ndp = NULL;
8424 struct nfsvattr nfsva;
8425 struct nfsclsession *tsep;
8426 uint32_t rflags, deleg;
8427 nfsattrbit_t attrbits;
8428 int error, ret, acesize, limitby, iomode;
8429
8430 *dpp = NULL;
8431 *laystatp = ENXIO;
8432 nfscl_reqstart(nd, NFSPROC_OPENLAYGET, nmp, nfhp, fhlen, NULL, NULL,
8433 0, 0, cred);
8434 NFSM_BUILD(tl, uint32_t *, 5 * NFSX_UNSIGNED);
8435 *tl++ = txdr_unsigned(op->nfso_own->nfsow_seqid);
8436 *tl++ = txdr_unsigned(mode & (NFSV4OPEN_ACCESSBOTH |
8437 NFSV4OPEN_WANTDELEGMASK));
8438 *tl++ = txdr_unsigned((mode >> NFSLCK_SHIFT) & NFSV4OPEN_DENYBOTH);
8439 tsep = nfsmnt_mdssession(nmp);
8440 *tl++ = tsep->nfsess_clientid.lval[0];
8441 *tl = tsep->nfsess_clientid.lval[1];
8442 nfsm_strtom(nd, op->nfso_own->nfsow_owner, NFSV4CL_LOCKNAMELEN);
8443 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_UNSIGNED);
8444 *tl++ = txdr_unsigned(NFSV4OPEN_NOCREATE);
8445 if (NFSHASNFSV4N(nmp)) {
8446 *tl = txdr_unsigned(NFSV4OPEN_CLAIMFH);
8447 } else {
8448 *tl = txdr_unsigned(NFSV4OPEN_CLAIMNULL);
8449 nfsm_strtom(nd, name, namelen);
8450 }
8451 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
8452 *tl = txdr_unsigned(NFSV4OP_GETATTR);
8453 NFSZERO_ATTRBIT(&attrbits);
8454 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_CHANGE);
8455 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEMODIFY);
8456 nfsrv_putattrbit(nd, &attrbits);
8457 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
8458 *tl = txdr_unsigned(NFSV4OP_LAYOUTGET);
8459 if ((mode & NFSV4OPEN_ACCESSWRITE) != 0)
8460 iomode = NFSLAYOUTIOMODE_RW;
8461 else
8462 iomode = NFSLAYOUTIOMODE_READ;
8463 nfsrv_setuplayoutget(nd, iomode, 0, UINT64_MAX, 0, stateidp,
8464 layouttype, layoutlen, usecurstateid);
8465 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, vp, p, cred,
8466 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
8467 if (error != 0)
8468 return (error);
8469 NFSCL_INCRSEQID(op->nfso_own->nfsow_seqid, nd);
8470 if (nd->nd_repstat != 0)
8471 *laystatp = nd->nd_repstat;
8472 if ((nd->nd_flag & ND_NOMOREDATA) == 0) {
8473 /* ND_NOMOREDATA will be set if the Open operation failed. */
8474 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
8475 6 * NFSX_UNSIGNED);
8476 op->nfso_stateid.seqid = *tl++;
8477 op->nfso_stateid.other[0] = *tl++;
8478 op->nfso_stateid.other[1] = *tl++;
8479 op->nfso_stateid.other[2] = *tl;
8480 rflags = fxdr_unsigned(u_int32_t, *(tl + 6));
8481 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
8482 if (error != 0)
8483 goto nfsmout;
8484 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
8485 deleg = fxdr_unsigned(u_int32_t, *tl);
8486 if (deleg == NFSV4OPEN_DELEGATEREAD ||
8487 deleg == NFSV4OPEN_DELEGATEWRITE) {
8488 if (!(op->nfso_own->nfsow_clp->nfsc_flags &
8489 NFSCLFLAGS_FIRSTDELEG))
8490 op->nfso_own->nfsow_clp->nfsc_flags |=
8491 (NFSCLFLAGS_FIRSTDELEG | NFSCLFLAGS_GOTDELEG);
8492 ndp = malloc(sizeof(struct nfscldeleg) + newfhlen,
8493 M_NFSCLDELEG, M_WAITOK);
8494 LIST_INIT(&ndp->nfsdl_owner);
8495 LIST_INIT(&ndp->nfsdl_lock);
8496 ndp->nfsdl_clp = op->nfso_own->nfsow_clp;
8497 ndp->nfsdl_fhlen = newfhlen;
8498 NFSBCOPY(newfhp, ndp->nfsdl_fh, newfhlen);
8499 newnfs_copyincred(cred, &ndp->nfsdl_cred);
8500 nfscl_lockinit(&ndp->nfsdl_rwlock);
8501 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
8502 NFSX_UNSIGNED);
8503 ndp->nfsdl_stateid.seqid = *tl++;
8504 ndp->nfsdl_stateid.other[0] = *tl++;
8505 ndp->nfsdl_stateid.other[1] = *tl++;
8506 ndp->nfsdl_stateid.other[2] = *tl++;
8507 ret = fxdr_unsigned(int, *tl);
8508 if (deleg == NFSV4OPEN_DELEGATEWRITE) {
8509 ndp->nfsdl_flags = NFSCLDL_WRITE;
8510 /*
8511 * Indicates how much the file can grow.
8512 */
8513 NFSM_DISSECT(tl, u_int32_t *,
8514 3 * NFSX_UNSIGNED);
8515 limitby = fxdr_unsigned(int, *tl++);
8516 switch (limitby) {
8517 case NFSV4OPEN_LIMITSIZE:
8518 ndp->nfsdl_sizelimit = fxdr_hyper(tl);
8519 break;
8520 case NFSV4OPEN_LIMITBLOCKS:
8521 ndp->nfsdl_sizelimit =
8522 fxdr_unsigned(u_int64_t, *tl++);
8523 ndp->nfsdl_sizelimit *=
8524 fxdr_unsigned(u_int64_t, *tl);
8525 break;
8526 default:
8527 error = NFSERR_BADXDR;
8528 goto nfsmout;
8529 };
8530 } else
8531 ndp->nfsdl_flags = NFSCLDL_READ;
8532 if (ret != 0)
8533 ndp->nfsdl_flags |= NFSCLDL_RECALL;
8534 error = nfsrv_dissectace(nd, &ndp->nfsdl_ace, false,
8535 &ret, &acesize, p);
8536 if (error != 0)
8537 goto nfsmout;
8538 } else if (deleg == NFSV4OPEN_DELEGATENONEEXT &&
8539 NFSHASNFSV4N(nmp)) {
8540 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
8541 deleg = fxdr_unsigned(uint32_t, *tl);
8542 if (deleg == NFSV4OPEN_CONTENTION ||
8543 deleg == NFSV4OPEN_RESOURCE)
8544 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
8545 } else if (deleg != NFSV4OPEN_DELEGATENONE) {
8546 error = NFSERR_BADXDR;
8547 goto nfsmout;
8548 }
8549 if ((rflags & NFSV4OPEN_LOCKTYPEPOSIX) != 0 ||
8550 nfscl_assumeposixlocks)
8551 op->nfso_posixlock = 1;
8552 else
8553 op->nfso_posixlock = 0;
8554 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
8555 /* If the 2nd element == NFS_OK, the Getattr succeeded. */
8556 if (*++tl == 0) {
8557 error = nfsv4_loadattr(nd, NULL, &nfsva, NULL,
8558 NULL, 0, NULL, NULL, NULL, NULL, NULL, 0,
8559 NULL, NULL, NULL, NULL, NULL, p, cred);
8560 if (error != 0)
8561 goto nfsmout;
8562 if (ndp != NULL) {
8563 ndp->nfsdl_change = nfsva.na_filerev;
8564 ndp->nfsdl_modtime = nfsva.na_mtime;
8565 ndp->nfsdl_flags |= NFSCLDL_MODTIMESET;
8566 *dpp = ndp;
8567 ndp = NULL;
8568 }
8569 /*
8570 * At this point, the Open has succeeded, so set
8571 * nd_repstat = NFS_OK. If the Layoutget failed,
8572 * this function just won't return a layout.
8573 */
8574 if (nd->nd_repstat == 0) {
8575 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
8576 *laystatp = fxdr_unsigned(int, *++tl);
8577 if (*laystatp == 0) {
8578 error = nfsrv_parselayoutget(nmp, nd,
8579 stateidp, retonclosep, flhp);
8580 if (error != 0)
8581 *laystatp = error;
8582 }
8583 } else
8584 nd->nd_repstat = 0; /* Return 0 for Open. */
8585 }
8586 }
8587 if (nd->nd_repstat != 0 && error == 0)
8588 error = nd->nd_repstat;
8589 nfsmout:
8590 free(ndp, M_NFSCLDELEG);
8591 m_freem(nd->nd_mrep);
8592 return (error);
8593 }
8594
8595 /*
8596 * Similar nfsrpc_createv4(), but also does the LayoutGet operation.
8597 * Used only for mounts with pNFS enabled.
8598 */
8599 static int
nfsrpc_createlayout(vnode_t dvp,char * name,int namelen,struct vattr * vap,nfsquad_t cverf,int fmode,struct nfsclowner * owp,struct nfscldeleg ** dpp,struct ucred * cred,NFSPROC_T * p,struct nfsvattr * dnap,struct nfsvattr * nnap,struct nfsfh ** nfhpp,int * attrflagp,int * dattrflagp,int * unlockedp,nfsv4stateid_t * stateidp,int usecurstateid,int layouttype,int layoutlen,int * retonclosep,struct nfsclflayouthead * flhp,int * laystatp)8600 nfsrpc_createlayout(vnode_t dvp, char *name, int namelen, struct vattr *vap,
8601 nfsquad_t cverf, int fmode, struct nfsclowner *owp, struct nfscldeleg **dpp,
8602 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap,
8603 struct nfsvattr *nnap, struct nfsfh **nfhpp, int *attrflagp,
8604 int *dattrflagp, int *unlockedp, nfsv4stateid_t *stateidp,
8605 int usecurstateid, int layouttype, int layoutlen, int *retonclosep,
8606 struct nfsclflayouthead *flhp, int *laystatp)
8607 {
8608 uint32_t *tl;
8609 int error = 0, deleg, newone, ret, acesize, limitby;
8610 struct nfsrv_descript nfsd, *nd = &nfsd;
8611 struct nfsclopen *op;
8612 struct nfscldeleg *dp = NULL;
8613 struct nfsnode *np;
8614 struct nfsfh *nfhp;
8615 struct nfsclsession *tsep;
8616 nfsattrbit_t attrbits;
8617 nfsv4stateid_t stateid;
8618 struct nfsmount *nmp;
8619
8620 nmp = VFSTONFS(dvp->v_mount);
8621 np = VTONFS(dvp);
8622 *laystatp = ENXIO;
8623 *unlockedp = 0;
8624 *nfhpp = NULL;
8625 *dpp = NULL;
8626 *attrflagp = 0;
8627 *dattrflagp = 0;
8628 if (namelen > NFS_MAXNAMLEN)
8629 return (ENAMETOOLONG);
8630 NFSCL_REQSTART(nd, NFSPROC_CREATELAYGET, dvp, cred);
8631 /*
8632 * For V4, this is actually an Open op.
8633 */
8634 NFSM_BUILD(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
8635 *tl++ = txdr_unsigned(owp->nfsow_seqid);
8636 if (NFSHASNFSV4N(nmp)) {
8637 if (!NFSHASPNFS(nmp) && nfscl_enablecallb != 0 &&
8638 nfs_numnfscbd > 0)
8639 *tl++ = txdr_unsigned(NFSV4OPEN_ACCESSWRITE |
8640 NFSV4OPEN_ACCESSREAD | NFSV4OPEN_WANTWRITEDELEG);
8641 else
8642 *tl++ = txdr_unsigned(NFSV4OPEN_ACCESSWRITE |
8643 NFSV4OPEN_ACCESSREAD | NFSV4OPEN_WANTNODELEG);
8644 } else
8645 *tl++ = txdr_unsigned(NFSV4OPEN_ACCESSWRITE |
8646 NFSV4OPEN_ACCESSREAD);
8647 *tl++ = txdr_unsigned(NFSV4OPEN_DENYNONE);
8648 tsep = nfsmnt_mdssession(nmp);
8649 *tl++ = tsep->nfsess_clientid.lval[0];
8650 *tl = tsep->nfsess_clientid.lval[1];
8651 nfsm_strtom(nd, owp->nfsow_owner, NFSV4CL_LOCKNAMELEN);
8652 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
8653 *tl++ = txdr_unsigned(NFSV4OPEN_CREATE);
8654 if ((fmode & O_EXCL) != 0) {
8655 if (NFSHASSESSPERSIST(nmp)) {
8656 /* Use GUARDED for persistent sessions. */
8657 *tl = txdr_unsigned(NFSCREATE_GUARDED);
8658 nfscl_fillsattr(nd, vap, dvp, NFSSATTR_NEWFILE, 0);
8659 } else {
8660 /* Otherwise, use EXCLUSIVE4_1. */
8661 *tl = txdr_unsigned(NFSCREATE_EXCLUSIVE41);
8662 NFSM_BUILD(tl, u_int32_t *, NFSX_VERF);
8663 *tl++ = cverf.lval[0];
8664 *tl = cverf.lval[1];
8665 nfscl_fillsattr(nd, vap, dvp, NFSSATTR_NEWFILE, 0);
8666 }
8667 } else {
8668 *tl = txdr_unsigned(NFSCREATE_UNCHECKED);
8669 nfscl_fillsattr(nd, vap, dvp, NFSSATTR_NEWFILE, 0);
8670 }
8671 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
8672 *tl = txdr_unsigned(NFSV4OPEN_CLAIMNULL);
8673 nfsm_strtom(nd, name, namelen);
8674 /* Get the new file's handle and attributes, plus save the FH. */
8675 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
8676 *tl++ = txdr_unsigned(NFSV4OP_SAVEFH);
8677 *tl++ = txdr_unsigned(NFSV4OP_GETFH);
8678 *tl = txdr_unsigned(NFSV4OP_GETATTR);
8679 NFSGETATTR_ATTRBIT(&attrbits);
8680 nfsrv_putattrbit(nd, &attrbits);
8681 /* Get the directory's post-op attributes. */
8682 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
8683 *tl = txdr_unsigned(NFSV4OP_PUTFH);
8684 (void)nfsm_fhtom(nmp, nd, np->n_fhp->nfh_fh, np->n_fhp->nfh_len, 0);
8685 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
8686 *tl = txdr_unsigned(NFSV4OP_GETATTR);
8687 nfsrv_putattrbit(nd, &attrbits);
8688 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
8689 *tl++ = txdr_unsigned(NFSV4OP_RESTOREFH);
8690 *tl = txdr_unsigned(NFSV4OP_LAYOUTGET);
8691 nfsrv_setuplayoutget(nd, NFSLAYOUTIOMODE_RW, 0, UINT64_MAX, 0, stateidp,
8692 layouttype, layoutlen, usecurstateid);
8693 error = nfscl_request(nd, dvp, p, cred);
8694 if (error != 0)
8695 return (error);
8696 NFSCL_DEBUG(4, "nfsrpc_createlayout stat=%d err=%d\n", nd->nd_repstat,
8697 error);
8698 if (nd->nd_repstat != 0)
8699 *laystatp = nd->nd_repstat;
8700 NFSCL_INCRSEQID(owp->nfsow_seqid, nd);
8701 if ((nd->nd_flag & ND_NOMOREDATA) == 0) {
8702 NFSCL_DEBUG(4, "nfsrpc_createlayout open succeeded\n");
8703 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
8704 6 * NFSX_UNSIGNED);
8705 stateid.seqid = *tl++;
8706 stateid.other[0] = *tl++;
8707 stateid.other[1] = *tl++;
8708 stateid.other[2] = *tl;
8709 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
8710 if (error != 0)
8711 goto nfsmout;
8712 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
8713 deleg = fxdr_unsigned(int, *tl);
8714 if (deleg == NFSV4OPEN_DELEGATEREAD ||
8715 deleg == NFSV4OPEN_DELEGATEWRITE) {
8716 if (!(owp->nfsow_clp->nfsc_flags &
8717 NFSCLFLAGS_FIRSTDELEG))
8718 owp->nfsow_clp->nfsc_flags |=
8719 (NFSCLFLAGS_FIRSTDELEG | NFSCLFLAGS_GOTDELEG);
8720 dp = malloc(sizeof(struct nfscldeleg) + NFSX_V4FHMAX,
8721 M_NFSCLDELEG, M_WAITOK);
8722 LIST_INIT(&dp->nfsdl_owner);
8723 LIST_INIT(&dp->nfsdl_lock);
8724 dp->nfsdl_clp = owp->nfsow_clp;
8725 newnfs_copyincred(cred, &dp->nfsdl_cred);
8726 nfscl_lockinit(&dp->nfsdl_rwlock);
8727 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
8728 NFSX_UNSIGNED);
8729 dp->nfsdl_stateid.seqid = *tl++;
8730 dp->nfsdl_stateid.other[0] = *tl++;
8731 dp->nfsdl_stateid.other[1] = *tl++;
8732 dp->nfsdl_stateid.other[2] = *tl++;
8733 ret = fxdr_unsigned(int, *tl);
8734 if (deleg == NFSV4OPEN_DELEGATEWRITE) {
8735 dp->nfsdl_flags = NFSCLDL_WRITE;
8736 /*
8737 * Indicates how much the file can grow.
8738 */
8739 NFSM_DISSECT(tl, u_int32_t *,
8740 3 * NFSX_UNSIGNED);
8741 limitby = fxdr_unsigned(int, *tl++);
8742 switch (limitby) {
8743 case NFSV4OPEN_LIMITSIZE:
8744 dp->nfsdl_sizelimit = fxdr_hyper(tl);
8745 break;
8746 case NFSV4OPEN_LIMITBLOCKS:
8747 dp->nfsdl_sizelimit =
8748 fxdr_unsigned(u_int64_t, *tl++);
8749 dp->nfsdl_sizelimit *=
8750 fxdr_unsigned(u_int64_t, *tl);
8751 break;
8752 default:
8753 error = NFSERR_BADXDR;
8754 goto nfsmout;
8755 };
8756 } else {
8757 dp->nfsdl_flags = NFSCLDL_READ;
8758 }
8759 if (ret != 0)
8760 dp->nfsdl_flags |= NFSCLDL_RECALL;
8761 error = nfsrv_dissectace(nd, &dp->nfsdl_ace, false,
8762 &ret, &acesize, p);
8763 if (error != 0)
8764 goto nfsmout;
8765 } else if (deleg == NFSV4OPEN_DELEGATENONEEXT &&
8766 NFSHASNFSV4N(nmp)) {
8767 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
8768 deleg = fxdr_unsigned(uint32_t, *tl);
8769 if (deleg == NFSV4OPEN_CONTENTION ||
8770 deleg == NFSV4OPEN_RESOURCE)
8771 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
8772 } else if (deleg != NFSV4OPEN_DELEGATENONE) {
8773 error = NFSERR_BADXDR;
8774 goto nfsmout;
8775 }
8776
8777 /* Now, we should have the status for the SaveFH. */
8778 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
8779 if (*++tl == 0) {
8780 NFSCL_DEBUG(4, "nfsrpc_createlayout SaveFH ok\n");
8781 /*
8782 * Now, process the GetFH and Getattr for the newly
8783 * created file. nfscl_mtofh() will set
8784 * ND_NOMOREDATA if these weren't successful.
8785 */
8786 error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp);
8787 NFSCL_DEBUG(4, "aft nfscl_mtofh err=%d\n", error);
8788 if (error != 0)
8789 goto nfsmout;
8790 } else
8791 nd->nd_flag |= ND_NOMOREDATA;
8792 /* Now we have the PutFH and Getattr for the directory. */
8793 if ((nd->nd_flag & ND_NOMOREDATA) == 0) {
8794 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
8795 if (*++tl != 0)
8796 nd->nd_flag |= ND_NOMOREDATA;
8797 else {
8798 NFSM_DISSECT(tl, uint32_t *, 2 *
8799 NFSX_UNSIGNED);
8800 if (*++tl != 0)
8801 nd->nd_flag |= ND_NOMOREDATA;
8802 }
8803 }
8804 if ((nd->nd_flag & ND_NOMOREDATA) == 0) {
8805 /* Load the directory attributes. */
8806 error = nfsm_loadattr(nd, dnap);
8807 NFSCL_DEBUG(4, "aft nfsm_loadattr err=%d\n", error);
8808 if (error != 0)
8809 goto nfsmout;
8810 *dattrflagp = 1;
8811 if (dp != NULL && *attrflagp != 0) {
8812 dp->nfsdl_change = nnap->na_filerev;
8813 dp->nfsdl_modtime = nnap->na_mtime;
8814 dp->nfsdl_flags |= NFSCLDL_MODTIMESET;
8815 }
8816 /*
8817 * We can now complete the Open state.
8818 */
8819 nfhp = *nfhpp;
8820 if (dp != NULL) {
8821 dp->nfsdl_fhlen = nfhp->nfh_len;
8822 NFSBCOPY(nfhp->nfh_fh, dp->nfsdl_fh,
8823 nfhp->nfh_len);
8824 }
8825 /*
8826 * Get an Open structure that will be
8827 * attached to the OpenOwner, acquired already.
8828 */
8829 error = nfscl_open(dvp, nfhp->nfh_fh, nfhp->nfh_len,
8830 (NFSV4OPEN_ACCESSWRITE | NFSV4OPEN_ACCESSREAD), 0,
8831 cred, p, NULL, &op, &newone, NULL, 0, false);
8832 if (error != 0)
8833 goto nfsmout;
8834 op->nfso_stateid = stateid;
8835 newnfs_copyincred(cred, &op->nfso_cred);
8836
8837 nfscl_openrelease(nmp, op, error, newone);
8838 *unlockedp = 1;
8839
8840 /* Now, handle the RestoreFH and LayoutGet. */
8841 if (nd->nd_repstat == 0) {
8842 NFSM_DISSECT(tl, uint32_t *, 4 * NFSX_UNSIGNED);
8843 *laystatp = fxdr_unsigned(int, *(tl + 3));
8844 if (*laystatp == 0) {
8845 error = nfsrv_parselayoutget(nmp, nd,
8846 stateidp, retonclosep, flhp);
8847 if (error != 0)
8848 *laystatp = error;
8849 }
8850 NFSCL_DEBUG(4, "aft nfsrv_parselayout err=%d\n",
8851 error);
8852 } else
8853 nd->nd_repstat = 0;
8854 }
8855 }
8856 if (nd->nd_repstat != 0 && error == 0)
8857 error = nd->nd_repstat;
8858 if (error == NFSERR_STALECLIENTID)
8859 nfscl_initiate_recovery(owp->nfsow_clp);
8860 nfsmout:
8861 NFSCL_DEBUG(4, "eo nfsrpc_createlayout err=%d\n", error);
8862 if (error == 0)
8863 *dpp = dp;
8864 else
8865 free(dp, M_NFSCLDELEG);
8866 m_freem(nd->nd_mrep);
8867 return (error);
8868 }
8869
8870 /*
8871 * Similar to nfsrpc_getopenlayout(), except that it used for the Create case.
8872 */
8873 static int
nfsrpc_getcreatelayout(vnode_t dvp,char * name,int namelen,struct vattr * vap,nfsquad_t cverf,int fmode,struct nfsclowner * owp,struct nfscldeleg ** dpp,struct ucred * cred,NFSPROC_T * p,struct nfsvattr * dnap,struct nfsvattr * nnap,struct nfsfh ** nfhpp,int * attrflagp,int * dattrflagp,int * unlockedp)8874 nfsrpc_getcreatelayout(vnode_t dvp, char *name, int namelen, struct vattr *vap,
8875 nfsquad_t cverf, int fmode, struct nfsclowner *owp, struct nfscldeleg **dpp,
8876 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap,
8877 struct nfsvattr *nnap, struct nfsfh **nfhpp, int *attrflagp,
8878 int *dattrflagp, int *unlockedp)
8879 {
8880 struct nfscllayout *lyp;
8881 struct nfsclflayouthead flh;
8882 struct nfsfh *nfhp;
8883 struct nfsclsession *tsep;
8884 struct nfsmount *nmp;
8885 nfsv4stateid_t stateid;
8886 int error, layoutlen, layouttype, retonclose, laystat;
8887
8888 error = 0;
8889 nmp = VFSTONFS(dvp->v_mount);
8890 if (NFSHASFLEXFILE(nmp))
8891 layouttype = NFSLAYOUT_FLEXFILE;
8892 else
8893 layouttype = NFSLAYOUT_NFSV4_1_FILES;
8894 LIST_INIT(&flh);
8895 tsep = nfsmnt_mdssession(nmp);
8896 layoutlen = tsep->nfsess_maxcache - (NFSX_STATEID + 3 * NFSX_UNSIGNED);
8897 error = nfsrpc_createlayout(dvp, name, namelen, vap, cverf, fmode,
8898 owp, dpp, cred, p, dnap, nnap, nfhpp, attrflagp, dattrflagp,
8899 unlockedp, &stateid, 1, layouttype, layoutlen, &retonclose,
8900 &flh, &laystat);
8901 NFSCL_DEBUG(4, "aft nfsrpc_createlayoutrpc laystat=%d err=%d\n",
8902 laystat, error);
8903 lyp = NULL;
8904 if (laystat == 0) {
8905 nfhp = *nfhpp;
8906 laystat = nfsrpc_layoutgetres(nmp, dvp, nfhp->nfh_fh,
8907 nfhp->nfh_len, &stateid, retonclose, NULL, &lyp, &flh,
8908 layouttype, laystat, NULL, cred, p);
8909 } else
8910 laystat = nfsrpc_layoutgetres(nmp, dvp, NULL, 0, &stateid,
8911 retonclose, NULL, &lyp, &flh, layouttype, laystat, NULL,
8912 cred, p);
8913 if (laystat == 0)
8914 nfscl_rellayout(lyp, 0);
8915 return (error);
8916 }
8917
8918 /*
8919 * Process the results of a layoutget() operation.
8920 */
8921 static int
nfsrpc_layoutgetres(struct nfsmount * nmp,vnode_t vp,uint8_t * newfhp,int newfhlen,nfsv4stateid_t * stateidp,int retonclose,uint32_t * notifybit,struct nfscllayout ** lypp,struct nfsclflayouthead * flhp,int layouttype,int laystat,int * islockedp,struct ucred * cred,NFSPROC_T * p)8922 nfsrpc_layoutgetres(struct nfsmount *nmp, vnode_t vp, uint8_t *newfhp,
8923 int newfhlen, nfsv4stateid_t *stateidp, int retonclose, uint32_t *notifybit,
8924 struct nfscllayout **lypp, struct nfsclflayouthead *flhp, int layouttype,
8925 int laystat, int *islockedp, struct ucred *cred, NFSPROC_T *p)
8926 {
8927 struct nfsclflayout *tflp;
8928 struct nfscldevinfo *dip;
8929 uint8_t *dev;
8930 int i, mirrorcnt;
8931
8932 if (laystat == NFSERR_UNKNLAYOUTTYPE) {
8933 NFSLOCKMNT(nmp);
8934 if (!NFSHASFLEXFILE(nmp)) {
8935 /* Switch to using Flex File Layout. */
8936 nmp->nm_state |= NFSSTA_FLEXFILE;
8937 } else if (layouttype == NFSLAYOUT_FLEXFILE) {
8938 /* Disable pNFS. */
8939 NFSCL_DEBUG(1, "disable PNFS\n");
8940 nmp->nm_state &= ~(NFSSTA_PNFS | NFSSTA_FLEXFILE);
8941 }
8942 NFSUNLOCKMNT(nmp);
8943 }
8944 if (laystat == 0) {
8945 NFSCL_DEBUG(4, "nfsrpc_layoutgetres at FOREACH\n");
8946 LIST_FOREACH(tflp, flhp, nfsfl_list) {
8947 if (layouttype == NFSLAYOUT_FLEXFILE)
8948 mirrorcnt = tflp->nfsfl_mirrorcnt;
8949 else
8950 mirrorcnt = 1;
8951 for (i = 0; i < mirrorcnt; i++) {
8952 laystat = nfscl_adddevinfo(nmp, NULL, i, tflp);
8953 NFSCL_DEBUG(4, "aft adddev=%d\n", laystat);
8954 if (laystat != 0) {
8955 if (layouttype == NFSLAYOUT_FLEXFILE)
8956 dev = tflp->nfsfl_ffm[i].dev;
8957 else
8958 dev = tflp->nfsfl_dev;
8959 laystat = nfsrpc_getdeviceinfo(nmp, dev,
8960 layouttype, notifybit, &dip, cred,
8961 p);
8962 NFSCL_DEBUG(4, "aft nfsrpc_gdi=%d\n",
8963 laystat);
8964 if (laystat != 0)
8965 goto out;
8966 laystat = nfscl_adddevinfo(nmp, dip, i,
8967 tflp);
8968 if (laystat != 0)
8969 printf("nfsrpc_layoutgetresout"
8970 ": cannot add\n");
8971 }
8972 }
8973 }
8974 }
8975 out:
8976 if (laystat == 0) {
8977 /*
8978 * nfscl_layout() always returns with the nfsly_lock
8979 * set to a refcnt (shared lock).
8980 * Passing in dvp is sufficient, since it is only used to
8981 * get the fsid for the file system.
8982 */
8983 laystat = nfscl_layout(nmp, vp, newfhp, newfhlen, stateidp,
8984 layouttype, retonclose, flhp, lypp, cred, p);
8985 NFSCL_DEBUG(4, "nfsrpc_layoutgetres: aft nfscl_layout=%d\n",
8986 laystat);
8987 if (laystat == 0 && islockedp != NULL)
8988 *islockedp = 1;
8989 }
8990 return (laystat);
8991 }
8992
8993 /*
8994 * nfs copy_file_range operation.
8995 */
8996 int
nfsrpc_copy_file_range(vnode_t invp,off_t * inoffp,vnode_t outvp,off_t * outoffp,size_t * lenp,unsigned int flags,int * inattrflagp,struct nfsvattr * innap,int * outattrflagp,struct nfsvattr * outnap,struct ucred * cred,bool consecutive,bool * must_commitp)8997 nfsrpc_copy_file_range(vnode_t invp, off_t *inoffp, vnode_t outvp,
8998 off_t *outoffp, size_t *lenp, unsigned int flags, int *inattrflagp,
8999 struct nfsvattr *innap, int *outattrflagp, struct nfsvattr *outnap,
9000 struct ucred *cred, bool consecutive, bool *must_commitp)
9001 {
9002 int commit, error, expireret = 0, retrycnt;
9003 u_int32_t clidrev = 0;
9004 struct nfsmount *nmp = VFSTONFS(invp->v_mount);
9005 struct nfsfh *innfhp = NULL, *outnfhp = NULL;
9006 nfsv4stateid_t instateid, outstateid;
9007 void *inlckp, *outlckp;
9008
9009 if (nmp->nm_clp != NULL)
9010 clidrev = nmp->nm_clp->nfsc_clientidrev;
9011 innfhp = VTONFS(invp)->n_fhp;
9012 outnfhp = VTONFS(outvp)->n_fhp;
9013 retrycnt = 0;
9014 do {
9015 /* Get both stateids. */
9016 inlckp = NULL;
9017 nfscl_getstateid(invp, innfhp->nfh_fh, innfhp->nfh_len,
9018 NFSV4OPEN_ACCESSREAD, 0, NULL, curthread, &instateid,
9019 &inlckp);
9020 outlckp = NULL;
9021 nfscl_getstateid(outvp, outnfhp->nfh_fh, outnfhp->nfh_len,
9022 NFSV4OPEN_ACCESSWRITE, 0, NULL, curthread, &outstateid,
9023 &outlckp);
9024
9025 error = nfsrpc_copyrpc(invp, *inoffp, outvp, *outoffp, lenp,
9026 &instateid, &outstateid, innap, inattrflagp, outnap,
9027 outattrflagp, consecutive, &commit, cred, curthread);
9028 if (error == 0) {
9029 if (commit != NFSWRITE_FILESYNC)
9030 *must_commitp = true;
9031 *inoffp += *lenp;
9032 *outoffp += *lenp;
9033 } else if (error == NFSERR_STALESTATEID)
9034 nfscl_initiate_recovery(nmp->nm_clp);
9035 if (inlckp != NULL)
9036 nfscl_lockderef(inlckp);
9037 if (outlckp != NULL)
9038 nfscl_lockderef(outlckp);
9039 if (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
9040 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
9041 error == NFSERR_OLDSTATEID || error == NFSERR_BADSESSION) {
9042 (void) nfs_catnap(PZERO, error, "nfs_cfr");
9043 } else if ((error == NFSERR_EXPIRED || (!NFSHASINT(nmp) &&
9044 error == NFSERR_BADSTATEID)) && clidrev != 0) {
9045 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev,
9046 curthread);
9047 } else if (error == NFSERR_BADSTATEID && NFSHASINT(nmp)) {
9048 error = EIO;
9049 }
9050 retrycnt++;
9051 } while (error == NFSERR_GRACE || error == NFSERR_DELAY ||
9052 error == NFSERR_STALESTATEID || error == NFSERR_BADSESSION ||
9053 error == NFSERR_STALEDONTRECOVER ||
9054 (error == NFSERR_OLDSTATEID && retrycnt < 20) ||
9055 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
9056 expireret == 0 && clidrev != 0 && retrycnt < 4));
9057 if (error != 0 && (retrycnt >= 4 ||
9058 error == NFSERR_STALESTATEID || error == NFSERR_BADSESSION ||
9059 error == NFSERR_STALEDONTRECOVER))
9060 error = EIO;
9061 return (error);
9062 }
9063
9064 /*
9065 * The copy RPC.
9066 */
9067 static int
nfsrpc_copyrpc(vnode_t invp,off_t inoff,vnode_t outvp,off_t outoff,size_t * lenp,nfsv4stateid_t * instateidp,nfsv4stateid_t * outstateidp,struct nfsvattr * innap,int * inattrflagp,struct nfsvattr * outnap,int * outattrflagp,bool consecutive,int * commitp,struct ucred * cred,NFSPROC_T * p)9068 nfsrpc_copyrpc(vnode_t invp, off_t inoff, vnode_t outvp, off_t outoff,
9069 size_t *lenp, nfsv4stateid_t *instateidp, nfsv4stateid_t *outstateidp,
9070 struct nfsvattr *innap, int *inattrflagp, struct nfsvattr *outnap,
9071 int *outattrflagp, bool consecutive, int *commitp, struct ucred *cred,
9072 NFSPROC_T *p)
9073 {
9074 uint32_t *tl, *opcntp;
9075 int error;
9076 struct nfsrv_descript nfsd;
9077 struct nfsrv_descript *nd = &nfsd;
9078 struct nfsmount *nmp;
9079 nfsattrbit_t attrbits;
9080 struct vattr va;
9081 uint64_t len;
9082
9083 nmp = VFSTONFS(invp->v_mount);
9084 *inattrflagp = *outattrflagp = 0;
9085 *commitp = NFSWRITE_UNSTABLE;
9086 len = *lenp;
9087 *lenp = 0;
9088 if (len > nfs_maxcopyrange)
9089 len = nfs_maxcopyrange;
9090 nfscl_reqstart(nd, NFSPROC_COPY, nmp, VTONFS(invp)->n_fhp->nfh_fh,
9091 VTONFS(invp)->n_fhp->nfh_len, &opcntp, NULL, 0, 0, cred);
9092 /*
9093 * First do a Setattr of atime to the server's clock
9094 * time. The FreeBSD "collective" was of the opinion
9095 * that setting atime was necessary for this syscall.
9096 * Do the Setattr before the Copy, so that it can be
9097 * handled well if the server replies NFSERR_DELAY to
9098 * the Setattr operation.
9099 */
9100 if ((nmp->nm_mountp->mnt_flag & MNT_NOATIME) == 0) {
9101 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
9102 *tl = txdr_unsigned(NFSV4OP_SETATTR);
9103 nfsm_stateidtom(nd, instateidp, NFSSTATEID_PUTSTATEID);
9104 VATTR_NULL(&va);
9105 va.va_atime.tv_sec = va.va_atime.tv_nsec = 0;
9106 va.va_vaflags = VA_UTIMES_NULL;
9107 nfscl_fillsattr(nd, &va, invp, 0, 0);
9108 /* Bump opcnt from 7 to 8. */
9109 *opcntp = txdr_unsigned(8);
9110 }
9111
9112 /* Now Getattr the invp attributes. */
9113 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
9114 *tl = txdr_unsigned(NFSV4OP_GETATTR);
9115 NFSGETATTR_ATTRBIT(&attrbits);
9116 nfsrv_putattrbit(nd, &attrbits);
9117
9118 /* Set outvp. */
9119 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
9120 *tl = txdr_unsigned(NFSV4OP_PUTFH);
9121 (void)nfsm_fhtom(nmp, nd, VTONFS(outvp)->n_fhp->nfh_fh,
9122 VTONFS(outvp)->n_fhp->nfh_len, 0);
9123
9124 /* Do the Copy. */
9125 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
9126 *tl = txdr_unsigned(NFSV4OP_COPY);
9127 nfsm_stateidtom(nd, instateidp, NFSSTATEID_PUTSTATEID);
9128 nfsm_stateidtom(nd, outstateidp, NFSSTATEID_PUTSTATEID);
9129 NFSM_BUILD(tl, uint32_t *, 3 * NFSX_HYPER + 4 * NFSX_UNSIGNED);
9130 txdr_hyper(inoff, tl); tl += 2;
9131 txdr_hyper(outoff, tl); tl += 2;
9132 txdr_hyper(len, tl); tl += 2;
9133 if (consecutive)
9134 *tl++ = newnfs_true;
9135 else
9136 *tl++ = newnfs_false;
9137 *tl++ = newnfs_true;
9138 *tl++ = 0;
9139
9140 /* Get the outvp attributes. */
9141 *tl = txdr_unsigned(NFSV4OP_GETATTR);
9142 NFSWRITEGETATTR_ATTRBIT(&attrbits);
9143 nfsrv_putattrbit(nd, &attrbits);
9144
9145 error = nfscl_request(nd, invp, p, cred);
9146 if (error != 0)
9147 return (error);
9148 /* Skip over the Setattr reply. */
9149 if ((nd->nd_flag & ND_NOMOREDATA) == 0 &&
9150 (nmp->nm_mountp->mnt_flag & MNT_NOATIME) == 0) {
9151 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
9152 if (*(tl + 1) == 0) {
9153 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
9154 if (error != 0)
9155 goto nfsmout;
9156 } else
9157 nd->nd_flag |= ND_NOMOREDATA;
9158 }
9159 if ((nd->nd_flag & ND_NOMOREDATA) == 0) {
9160 /* Get the input file's attributes. */
9161 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
9162 if (*(tl + 1) == 0) {
9163 error = nfsm_loadattr(nd, innap);
9164 if (error != 0)
9165 goto nfsmout;
9166 *inattrflagp = 1;
9167 } else
9168 nd->nd_flag |= ND_NOMOREDATA;
9169 }
9170 /* Skip over return stat for PutFH. */
9171 if ((nd->nd_flag & ND_NOMOREDATA) == 0) {
9172 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
9173 if (*++tl != 0)
9174 nd->nd_flag |= ND_NOMOREDATA;
9175 }
9176 /* Skip over return stat for Copy. */
9177 if ((nd->nd_flag & ND_NOMOREDATA) == 0)
9178 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
9179 if (nd->nd_repstat == 0) {
9180 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
9181 if (*tl != 0) {
9182 /* There should be no callback ids. */
9183 error = NFSERR_BADXDR;
9184 goto nfsmout;
9185 }
9186 NFSM_DISSECT(tl, uint32_t *, NFSX_HYPER + 3 * NFSX_UNSIGNED +
9187 NFSX_VERF);
9188 len = fxdr_hyper(tl); tl += 2;
9189 *commitp = fxdr_unsigned(int, *tl++);
9190 NFSLOCKMNT(nmp);
9191 if (!NFSHASWRITEVERF(nmp)) {
9192 NFSBCOPY(tl, nmp->nm_verf, NFSX_VERF);
9193 NFSSETWRITEVERF(nmp);
9194 } else if (NFSBCMP(tl, nmp->nm_verf, NFSX_VERF)) {
9195 NFSBCOPY(tl, nmp->nm_verf, NFSX_VERF);
9196 nd->nd_repstat = NFSERR_STALEWRITEVERF;
9197 }
9198 NFSUNLOCKMNT(nmp);
9199 tl += (NFSX_VERF / NFSX_UNSIGNED);
9200 if (nd->nd_repstat == 0 && *++tl != newnfs_true)
9201 /* Must be a synchronous copy. */
9202 nd->nd_repstat = NFSERR_NOTSUPP;
9203 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
9204 error = nfsm_loadattr(nd, outnap);
9205 if (error == 0)
9206 *outattrflagp = NFS_LATTR_NOSHRINK;
9207 if (nd->nd_repstat == 0)
9208 *lenp = len;
9209 } else if (nd->nd_repstat == NFSERR_OFFLOADNOREQS) {
9210 /*
9211 * For the case where consecutive is not supported, but
9212 * synchronous is supported, we can try consecutive == false
9213 * by returning this error. Otherwise, return NFSERR_NOTSUPP,
9214 * since Copy cannot be done.
9215 */
9216 if ((nd->nd_flag & ND_NOMOREDATA) == 0) {
9217 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
9218 if (!consecutive || *++tl == newnfs_false)
9219 nd->nd_repstat = NFSERR_NOTSUPP;
9220 } else
9221 nd->nd_repstat = NFSERR_BADXDR;
9222 }
9223 if (error == 0)
9224 error = nd->nd_repstat;
9225 nfsmout:
9226 m_freem(nd->nd_mrep);
9227 return (error);
9228 }
9229
9230 /*
9231 * nfs clone operation.
9232 */
9233 int
nfsrpc_clone(vnode_t invp,off_t * inoffp,vnode_t outvp,off_t * outoffp,size_t * lenp,bool toeof,int * inattrflagp,struct nfsvattr * innap,int * outattrflagp,struct nfsvattr * outnap,struct ucred * cred)9234 nfsrpc_clone(vnode_t invp, off_t *inoffp, vnode_t outvp,
9235 off_t *outoffp, size_t *lenp, bool toeof, int *inattrflagp,
9236 struct nfsvattr *innap, int *outattrflagp, struct nfsvattr *outnap,
9237 struct ucred *cred)
9238 {
9239 int error, expireret = 0, retrycnt;
9240 uint32_t clidrev = 0;
9241 struct nfsmount *nmp = VFSTONFS(invp->v_mount);
9242 struct nfsfh *innfhp = NULL, *outnfhp = NULL;
9243 nfsv4stateid_t instateid, outstateid;
9244 void *inlckp, *outlckp;
9245
9246 if (nmp->nm_clp != NULL)
9247 clidrev = nmp->nm_clp->nfsc_clientidrev;
9248 innfhp = VTONFS(invp)->n_fhp;
9249 outnfhp = VTONFS(outvp)->n_fhp;
9250 retrycnt = 0;
9251 do {
9252 /* Get both stateids. */
9253 inlckp = NULL;
9254 nfscl_getstateid(invp, innfhp->nfh_fh, innfhp->nfh_len,
9255 NFSV4OPEN_ACCESSREAD, 0, NULL, curthread, &instateid,
9256 &inlckp);
9257 outlckp = NULL;
9258 nfscl_getstateid(outvp, outnfhp->nfh_fh, outnfhp->nfh_len,
9259 NFSV4OPEN_ACCESSWRITE, 0, NULL, curthread, &outstateid,
9260 &outlckp);
9261
9262 error = nfsrpc_clonerpc(invp, *inoffp, outvp, *outoffp, lenp,
9263 toeof, &instateid, &outstateid, innap, inattrflagp, outnap,
9264 outattrflagp, cred, curthread);
9265 if (error == 0) {
9266 *inoffp += *lenp;
9267 *outoffp += *lenp;
9268 } else if (error == NFSERR_STALESTATEID)
9269 nfscl_initiate_recovery(nmp->nm_clp);
9270 if (inlckp != NULL)
9271 nfscl_lockderef(inlckp);
9272 if (outlckp != NULL)
9273 nfscl_lockderef(outlckp);
9274 if (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
9275 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
9276 error == NFSERR_OLDSTATEID || error == NFSERR_BADSESSION) {
9277 (void) nfs_catnap(PZERO, error, "nfs_cfr");
9278 } else if ((error == NFSERR_EXPIRED || (!NFSHASINT(nmp) &&
9279 error == NFSERR_BADSTATEID)) && clidrev != 0) {
9280 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev,
9281 curthread);
9282 } else if (error == NFSERR_BADSTATEID && NFSHASINT(nmp)) {
9283 error = EIO;
9284 }
9285 retrycnt++;
9286 } while (error == NFSERR_GRACE || error == NFSERR_DELAY ||
9287 error == NFSERR_STALESTATEID || error == NFSERR_BADSESSION ||
9288 error == NFSERR_STALEDONTRECOVER ||
9289 (error == NFSERR_OLDSTATEID && retrycnt < 20) ||
9290 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
9291 expireret == 0 && clidrev != 0 && retrycnt < 4));
9292 if (error != 0 && (retrycnt >= 4 ||
9293 error == NFSERR_STALESTATEID || error == NFSERR_BADSESSION ||
9294 error == NFSERR_STALEDONTRECOVER))
9295 error = EIO;
9296 return (error);
9297 }
9298
9299 /*
9300 * The clone RPC.
9301 */
9302 static int
nfsrpc_clonerpc(vnode_t invp,off_t inoff,vnode_t outvp,off_t outoff,size_t * lenp,bool toeof,nfsv4stateid_t * instateidp,nfsv4stateid_t * outstateidp,struct nfsvattr * innap,int * inattrflagp,struct nfsvattr * outnap,int * outattrflagp,struct ucred * cred,NFSPROC_T * p)9303 nfsrpc_clonerpc(vnode_t invp, off_t inoff, vnode_t outvp, off_t outoff,
9304 size_t *lenp, bool toeof, nfsv4stateid_t *instateidp,
9305 nfsv4stateid_t *outstateidp, struct nfsvattr *innap, int *inattrflagp,
9306 struct nfsvattr *outnap, int *outattrflagp, struct ucred *cred,
9307 NFSPROC_T *p)
9308 {
9309 uint32_t *tl, *opcntp;
9310 int error;
9311 struct nfsrv_descript nfsd;
9312 struct nfsrv_descript *nd = &nfsd;
9313 struct nfsmount *nmp;
9314 nfsattrbit_t attrbits;
9315 struct vattr va;
9316 uint64_t len;
9317
9318 nmp = VFSTONFS(invp->v_mount);
9319 *inattrflagp = *outattrflagp = 0;
9320 len = *lenp;
9321 if (len == 0)
9322 return (0);
9323 if (toeof)
9324 len = 0;
9325 nfscl_reqstart(nd, NFSPROC_CLONE, nmp, VTONFS(invp)->n_fhp->nfh_fh,
9326 VTONFS(invp)->n_fhp->nfh_len, &opcntp, NULL, 0, 0, cred);
9327 /*
9328 * First do a Setattr of atime to the server's clock
9329 * time. The FreeBSD "collective" was of the opinion
9330 * that setting atime was necessary for this syscall.
9331 * Do the Setattr before the Clone, so that it can be
9332 * handled well if the server replies NFSERR_DELAY to
9333 * the Setattr operation.
9334 */
9335 if ((nmp->nm_mountp->mnt_flag & MNT_NOATIME) == 0) {
9336 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
9337 *tl = txdr_unsigned(NFSV4OP_SETATTR);
9338 nfsm_stateidtom(nd, instateidp, NFSSTATEID_PUTSTATEID);
9339 VATTR_NULL(&va);
9340 va.va_atime.tv_sec = va.va_atime.tv_nsec = 0;
9341 va.va_vaflags = VA_UTIMES_NULL;
9342 nfscl_fillsattr(nd, &va, invp, 0, 0);
9343 /* Bump opcnt from 7 to 8. */
9344 *opcntp = txdr_unsigned(8);
9345 }
9346
9347 /* Now Getattr the invp attributes. */
9348 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
9349 *tl = txdr_unsigned(NFSV4OP_GETATTR);
9350 NFSGETATTR_ATTRBIT(&attrbits);
9351 nfsrv_putattrbit(nd, &attrbits);
9352
9353 /* Set outvp. */
9354 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
9355 *tl = txdr_unsigned(NFSV4OP_PUTFH);
9356 (void)nfsm_fhtom(nmp, nd, VTONFS(outvp)->n_fhp->nfh_fh,
9357 VTONFS(outvp)->n_fhp->nfh_len, 0);
9358
9359 /* Do the Clone. */
9360 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
9361 *tl = txdr_unsigned(NFSV4OP_CLONE);
9362 nfsm_stateidtom(nd, instateidp, NFSSTATEID_PUTSTATEID);
9363 nfsm_stateidtom(nd, outstateidp, NFSSTATEID_PUTSTATEID);
9364 NFSM_BUILD(tl, uint32_t *, 3 * NFSX_HYPER + NFSX_UNSIGNED);
9365 txdr_hyper(inoff, tl); tl += 2;
9366 txdr_hyper(outoff, tl); tl += 2;
9367 txdr_hyper(len, tl); tl += 2;
9368
9369 /* Get the outvp attributes. */
9370 *tl = txdr_unsigned(NFSV4OP_GETATTR);
9371 NFSWRITEGETATTR_ATTRBIT(&attrbits);
9372 nfsrv_putattrbit(nd, &attrbits);
9373
9374 error = nfscl_request(nd, invp, p, cred);
9375 if (error != 0)
9376 return (error);
9377 /* Skip over the Setattr reply. */
9378 if ((nd->nd_flag & ND_NOMOREDATA) == 0 &&
9379 (nmp->nm_mountp->mnt_flag & MNT_NOATIME) == 0) {
9380 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
9381 if (*(tl + 1) == 0) {
9382 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
9383 if (error != 0)
9384 goto nfsmout;
9385 } else
9386 nd->nd_flag |= ND_NOMOREDATA;
9387 }
9388 if ((nd->nd_flag & ND_NOMOREDATA) == 0) {
9389 /* Get the input file's attributes. */
9390 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
9391 if (*(tl + 1) == 0) {
9392 error = nfsm_loadattr(nd, innap);
9393 if (error != 0)
9394 goto nfsmout;
9395 *inattrflagp = 1;
9396 } else
9397 nd->nd_flag |= ND_NOMOREDATA;
9398 }
9399 /* Skip over return stat for PutFH. */
9400 if ((nd->nd_flag & ND_NOMOREDATA) == 0) {
9401 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
9402 if (*++tl != 0)
9403 nd->nd_flag |= ND_NOMOREDATA;
9404 }
9405 /* Skip over return stat for Clone. */
9406 if ((nd->nd_flag & ND_NOMOREDATA) == 0)
9407 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
9408 if (nd->nd_repstat == 0) {
9409 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
9410 error = nfsm_loadattr(nd, outnap);
9411 if (error == 0)
9412 *outattrflagp = NFS_LATTR_NOSHRINK;
9413 } else {
9414 *lenp = 0;
9415 }
9416 if (error == 0)
9417 error = nd->nd_repstat;
9418 nfsmout:
9419 m_freem(nd->nd_mrep);
9420 return (error);
9421 }
9422
9423 /*
9424 * Seek operation.
9425 */
9426 int
nfsrpc_seek(vnode_t vp,off_t * offp,bool * eofp,int content,struct ucred * cred,struct nfsvattr * nap,int * attrflagp)9427 nfsrpc_seek(vnode_t vp, off_t *offp, bool *eofp, int content,
9428 struct ucred *cred, struct nfsvattr *nap, int *attrflagp)
9429 {
9430 int error, expireret = 0, retrycnt;
9431 u_int32_t clidrev = 0;
9432 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
9433 struct nfsnode *np = VTONFS(vp);
9434 struct nfsfh *nfhp = NULL;
9435 nfsv4stateid_t stateid;
9436 void *lckp;
9437
9438 if (nmp->nm_clp != NULL)
9439 clidrev = nmp->nm_clp->nfsc_clientidrev;
9440 nfhp = np->n_fhp;
9441 retrycnt = 0;
9442 do {
9443 lckp = NULL;
9444 nfscl_getstateid(vp, nfhp->nfh_fh, nfhp->nfh_len,
9445 NFSV4OPEN_ACCESSREAD, 0, cred, curthread, &stateid, &lckp);
9446 error = nfsrpc_seekrpc(vp, offp, &stateid, eofp, content,
9447 nap, attrflagp, cred);
9448 if (error == NFSERR_STALESTATEID)
9449 nfscl_initiate_recovery(nmp->nm_clp);
9450 if (lckp != NULL)
9451 nfscl_lockderef(lckp);
9452 if (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
9453 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
9454 error == NFSERR_OLDSTATEID || error == NFSERR_BADSESSION) {
9455 (void) nfs_catnap(PZERO, error, "nfs_seek");
9456 } else if ((error == NFSERR_EXPIRED || (!NFSHASINT(nmp) &&
9457 error == NFSERR_BADSTATEID)) && clidrev != 0) {
9458 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev,
9459 curthread);
9460 } else if (error == NFSERR_BADSTATEID && NFSHASINT(nmp)) {
9461 error = EIO;
9462 }
9463 retrycnt++;
9464 } while (error == NFSERR_GRACE || error == NFSERR_STALESTATEID ||
9465 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY ||
9466 error == NFSERR_BADSESSION ||
9467 (error == NFSERR_OLDSTATEID && retrycnt < 20) ||
9468 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) &&
9469 expireret == 0 && clidrev != 0 && retrycnt < 4) ||
9470 (error == NFSERR_OPENMODE && retrycnt < 4));
9471 if (error && retrycnt >= 4)
9472 error = EIO;
9473 return (error);
9474 }
9475
9476 /*
9477 * The seek RPC.
9478 */
9479 static int
nfsrpc_seekrpc(vnode_t vp,off_t * offp,nfsv4stateid_t * stateidp,bool * eofp,int content,struct nfsvattr * nap,int * attrflagp,struct ucred * cred)9480 nfsrpc_seekrpc(vnode_t vp, off_t *offp, nfsv4stateid_t *stateidp, bool *eofp,
9481 int content, struct nfsvattr *nap, int *attrflagp, struct ucred *cred)
9482 {
9483 uint32_t *tl;
9484 int error;
9485 struct nfsrv_descript nfsd;
9486 struct nfsrv_descript *nd = &nfsd;
9487 nfsattrbit_t attrbits;
9488
9489 *attrflagp = 0;
9490 NFSCL_REQSTART(nd, NFSPROC_SEEK, vp, cred);
9491 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID);
9492 NFSM_BUILD(tl, uint32_t *, NFSX_HYPER + 2 * NFSX_UNSIGNED);
9493 txdr_hyper(*offp, tl); tl += 2;
9494 *tl++ = txdr_unsigned(content);
9495 *tl = txdr_unsigned(NFSV4OP_GETATTR);
9496 NFSGETATTR_ATTRBIT(&attrbits);
9497 nfsrv_putattrbit(nd, &attrbits);
9498 error = nfscl_request(nd, vp, curthread, cred);
9499 if (error != 0)
9500 return (error);
9501 if (nd->nd_repstat == 0) {
9502 NFSM_DISSECT(tl, uint32_t *, 3 * NFSX_UNSIGNED + NFSX_HYPER);
9503 if (*tl++ == newnfs_true)
9504 *eofp = true;
9505 else
9506 *eofp = false;
9507 *offp = fxdr_hyper(tl);
9508 /* Just skip over Getattr op status. */
9509 error = nfsm_loadattr(nd, nap);
9510 if (error == 0)
9511 *attrflagp = 1;
9512 }
9513 error = nd->nd_repstat;
9514 nfsmout:
9515 m_freem(nd->nd_mrep);
9516 return (error);
9517 }
9518
9519 /*
9520 * The getextattr RPC.
9521 */
9522 int
nfsrpc_getextattr(vnode_t vp,const char * name,struct uio * uiop,ssize_t * lenp,struct nfsvattr * nap,int * attrflagp,struct ucred * cred,NFSPROC_T * p)9523 nfsrpc_getextattr(vnode_t vp, const char *name, struct uio *uiop, ssize_t *lenp,
9524 struct nfsvattr *nap, int *attrflagp, struct ucred *cred, NFSPROC_T *p)
9525 {
9526 uint32_t *tl;
9527 int error;
9528 struct nfsrv_descript nfsd;
9529 struct nfsrv_descript *nd = &nfsd;
9530 nfsattrbit_t attrbits;
9531 uint32_t len, len2;
9532
9533 *attrflagp = 0;
9534 NFSCL_REQSTART(nd, NFSPROC_GETEXTATTR, vp, cred);
9535 nfsm_strtom(nd, name, strlen(name));
9536 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
9537 *tl = txdr_unsigned(NFSV4OP_GETATTR);
9538 NFSGETATTR_ATTRBIT(&attrbits);
9539 nfsrv_putattrbit(nd, &attrbits);
9540 error = nfscl_request(nd, vp, p, cred);
9541 if (error != 0)
9542 return (error);
9543 if (nd->nd_repstat == 0) {
9544 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
9545 len = fxdr_unsigned(uint32_t, *tl);
9546 /* Sanity check lengths. */
9547 if (uiop != NULL && len > 0 && len <= IOSIZE_MAX &&
9548 uiop->uio_resid <= UINT32_MAX) {
9549 len2 = uiop->uio_resid;
9550 if (len2 >= len)
9551 error = nfsm_mbufuio(nd, uiop, len);
9552 else {
9553 error = nfsm_mbufuio(nd, uiop, len2);
9554 if (error == 0) {
9555 /*
9556 * nfsm_mbufuio() advances to a multiple
9557 * of 4, so round up len2 as well. Then
9558 * we need to advance over the rest of
9559 * the data, rounding up the remaining
9560 * length.
9561 */
9562 len2 = NFSM_RNDUP(len2);
9563 len2 = NFSM_RNDUP(len - len2);
9564 if (len2 > 0)
9565 error = nfsm_advance(nd, len2,
9566 -1);
9567 }
9568 }
9569 } else if (uiop == NULL && len > 0) {
9570 /* Just wants the length and not the data. */
9571 error = nfsm_advance(nd, NFSM_RNDUP(len), -1);
9572 } else if (len > 0)
9573 error = ENOATTR;
9574 if (error != 0)
9575 goto nfsmout;
9576 *lenp = len;
9577 /* Just skip over Getattr op status. */
9578 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
9579 error = nfsm_loadattr(nd, nap);
9580 if (error == 0)
9581 *attrflagp = 1;
9582 }
9583 if (error == 0)
9584 error = nd->nd_repstat;
9585 nfsmout:
9586 m_freem(nd->nd_mrep);
9587 return (error);
9588 }
9589
9590 /*
9591 * The setextattr RPC.
9592 */
9593 int
nfsrpc_setextattr(vnode_t vp,const char * name,struct uio * uiop,struct nfsvattr * nap,int * attrflagp,struct ucred * cred,NFSPROC_T * p)9594 nfsrpc_setextattr(vnode_t vp, const char *name, struct uio *uiop,
9595 struct nfsvattr *nap, int *attrflagp, struct ucred *cred, NFSPROC_T *p)
9596 {
9597 uint32_t *tl;
9598 int error;
9599 struct nfsrv_descript nfsd;
9600 struct nfsrv_descript *nd = &nfsd;
9601 nfsattrbit_t attrbits;
9602
9603 *attrflagp = 0;
9604 NFSCL_REQSTART(nd, NFSPROC_SETEXTATTR, vp, cred);
9605 if (uiop->uio_resid > nd->nd_maxreq) {
9606 /* nd_maxreq is set by NFSCL_REQSTART(). */
9607 m_freem(nd->nd_mreq);
9608 return (EINVAL);
9609 }
9610 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
9611 *tl = txdr_unsigned(NFSV4SXATTR_EITHER);
9612 nfsm_strtom(nd, name, strlen(name));
9613 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
9614 *tl = txdr_unsigned(uiop->uio_resid);
9615 error = nfsm_uiombuf(nd, uiop, uiop->uio_resid);
9616 if (error != 0) {
9617 m_freem(nd->nd_mreq);
9618 return (error);
9619 }
9620 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
9621 *tl = txdr_unsigned(NFSV4OP_GETATTR);
9622 NFSGETATTR_ATTRBIT(&attrbits);
9623 nfsrv_putattrbit(nd, &attrbits);
9624 error = nfscl_request(nd, vp, p, cred);
9625 if (error != 0)
9626 return (error);
9627 if (nd->nd_repstat == 0) {
9628 /* Just skip over the reply and Getattr op status. */
9629 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_HYPER + 3 *
9630 NFSX_UNSIGNED);
9631 error = nfsm_loadattr(nd, nap);
9632 if (error == 0)
9633 *attrflagp = 1;
9634 }
9635 if (error == 0)
9636 error = nd->nd_repstat;
9637 nfsmout:
9638 m_freem(nd->nd_mrep);
9639 return (error);
9640 }
9641
9642 /*
9643 * The removeextattr RPC.
9644 */
9645 int
nfsrpc_rmextattr(vnode_t vp,const char * name,struct nfsvattr * nap,int * attrflagp,struct ucred * cred,NFSPROC_T * p)9646 nfsrpc_rmextattr(vnode_t vp, const char *name, struct nfsvattr *nap,
9647 int *attrflagp, struct ucred *cred, NFSPROC_T *p)
9648 {
9649 uint32_t *tl;
9650 int error;
9651 struct nfsrv_descript nfsd;
9652 struct nfsrv_descript *nd = &nfsd;
9653 nfsattrbit_t attrbits;
9654
9655 *attrflagp = 0;
9656 NFSCL_REQSTART(nd, NFSPROC_RMEXTATTR, vp, cred);
9657 nfsm_strtom(nd, name, strlen(name));
9658 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
9659 *tl = txdr_unsigned(NFSV4OP_GETATTR);
9660 NFSGETATTR_ATTRBIT(&attrbits);
9661 nfsrv_putattrbit(nd, &attrbits);
9662 error = nfscl_request(nd, vp, p, cred);
9663 if (error != 0)
9664 return (error);
9665 if (nd->nd_repstat == 0) {
9666 /* Just skip over the reply and Getattr op status. */
9667 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_HYPER + 3 *
9668 NFSX_UNSIGNED);
9669 error = nfsm_loadattr(nd, nap);
9670 if (error == 0)
9671 *attrflagp = 1;
9672 }
9673 if (error == 0)
9674 error = nd->nd_repstat;
9675 nfsmout:
9676 m_freem(nd->nd_mrep);
9677 return (error);
9678 }
9679
9680 /*
9681 * The listextattr RPC.
9682 */
9683 int
nfsrpc_listextattr(vnode_t vp,uint64_t * cookiep,struct uio * uiop,size_t * lenp,bool * eofp,struct nfsvattr * nap,int * attrflagp,struct ucred * cred,NFSPROC_T * p)9684 nfsrpc_listextattr(vnode_t vp, uint64_t *cookiep, struct uio *uiop,
9685 size_t *lenp, bool *eofp, struct nfsvattr *nap, int *attrflagp,
9686 struct ucred *cred, NFSPROC_T *p)
9687 {
9688 uint32_t *tl;
9689 int cnt, error, i, len;
9690 struct nfsrv_descript nfsd;
9691 struct nfsrv_descript *nd = &nfsd;
9692 nfsattrbit_t attrbits;
9693 u_char c;
9694
9695 *attrflagp = 0;
9696 NFSCL_REQSTART(nd, NFSPROC_LISTEXTATTR, vp, cred);
9697 NFSM_BUILD(tl, uint32_t *, NFSX_HYPER + 2 * NFSX_UNSIGNED);
9698 txdr_hyper(*cookiep, tl); tl += 2;
9699 *tl++ = txdr_unsigned(*lenp);
9700 *tl = txdr_unsigned(NFSV4OP_GETATTR);
9701 NFSGETATTR_ATTRBIT(&attrbits);
9702 nfsrv_putattrbit(nd, &attrbits);
9703 error = nfscl_request(nd, vp, p, cred);
9704 if (error != 0)
9705 return (error);
9706 *eofp = true;
9707 *lenp = 0;
9708 if (nd->nd_repstat == 0) {
9709 NFSM_DISSECT(tl, uint32_t *, NFSX_HYPER + NFSX_UNSIGNED);
9710 *cookiep = fxdr_hyper(tl); tl += 2;
9711 cnt = fxdr_unsigned(int, *tl);
9712 if (cnt < 0) {
9713 error = EBADRPC;
9714 goto nfsmout;
9715 }
9716 for (i = 0; i < cnt; i++) {
9717 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
9718 len = fxdr_unsigned(int, *tl);
9719 if (len <= 0 || len > EXTATTR_MAXNAMELEN) {
9720 error = EBADRPC;
9721 goto nfsmout;
9722 }
9723 if (uiop == NULL)
9724 error = nfsm_advance(nd, NFSM_RNDUP(len), -1);
9725 else if (uiop->uio_resid >= len + 1) {
9726 c = len;
9727 error = uiomove(&c, sizeof(c), uiop);
9728 if (error == 0)
9729 error = nfsm_mbufuio(nd, uiop, len);
9730 } else {
9731 error = nfsm_advance(nd, NFSM_RNDUP(len), -1);
9732 *eofp = false;
9733 }
9734 if (error != 0)
9735 goto nfsmout;
9736 *lenp += (len + 1);
9737 }
9738 /* Get the eof and skip over the Getattr op status. */
9739 NFSM_DISSECT(tl, uint32_t *, 3 * NFSX_UNSIGNED);
9740 /*
9741 * *eofp is set false above, because it wasn't able to copy
9742 * all of the reply.
9743 */
9744 if (*eofp && *tl == 0)
9745 *eofp = false;
9746 error = nfsm_loadattr(nd, nap);
9747 if (error == 0)
9748 *attrflagp = 1;
9749 }
9750 if (error == 0)
9751 error = nd->nd_repstat;
9752 nfsmout:
9753 m_freem(nd->nd_mrep);
9754 return (error);
9755 }
9756
9757 /*
9758 * Split an mbuf list. For non-M_EXTPG mbufs, just use m_split().
9759 */
9760 static struct mbuf *
nfsm_split(struct mbuf * mp,uint64_t xfer)9761 nfsm_split(struct mbuf *mp, uint64_t xfer)
9762 {
9763 struct mbuf *m, *m2;
9764 vm_page_t pg;
9765 int i, j, left, pgno, plen, trim;
9766 char *cp, *cp2;
9767
9768 if ((mp->m_flags & M_EXTPG) == 0) {
9769 m = m_split(mp, xfer, M_WAITOK);
9770 return (m);
9771 }
9772
9773 /* Find the correct mbuf to split at. */
9774 for (m = mp; m != NULL && xfer > m->m_len; m = m->m_next)
9775 xfer -= m->m_len;
9776 if (m == NULL)
9777 return (NULL);
9778
9779 /* If xfer == m->m_len, we can just split the mbuf list. */
9780 if (xfer == m->m_len) {
9781 m2 = m->m_next;
9782 m->m_next = NULL;
9783 return (m2);
9784 }
9785
9786 /* Find the page to split at. */
9787 pgno = 0;
9788 left = xfer;
9789 do {
9790 if (pgno == 0)
9791 plen = m_epg_pagelen(m, 0, m->m_epg_1st_off);
9792 else
9793 plen = m_epg_pagelen(m, pgno, 0);
9794 if (left <= plen)
9795 break;
9796 left -= plen;
9797 pgno++;
9798 } while (pgno < m->m_epg_npgs);
9799 if (pgno == m->m_epg_npgs)
9800 panic("nfsm_split: erroneous ext_pgs mbuf");
9801
9802 m2 = mb_alloc_ext_pgs(M_WAITOK, mb_free_mext_pgs, 0);
9803 m2->m_epg_flags |= EPG_FLAG_ANON;
9804
9805 /*
9806 * If left < plen, allocate a new page for the new mbuf
9807 * and copy the data after left in the page to this new
9808 * page.
9809 */
9810 if (left < plen) {
9811 pg = vm_page_alloc_noobj(VM_ALLOC_WAITOK | VM_ALLOC_NODUMP |
9812 VM_ALLOC_WIRED);
9813 m2->m_epg_pa[0] = VM_PAGE_TO_PHYS(pg);
9814 m2->m_epg_npgs = 1;
9815
9816 /* Copy the data after left to the new page. */
9817 trim = plen - left;
9818 cp = (char *)(void *)PHYS_TO_DMAP(m->m_epg_pa[pgno]);
9819 if (pgno == 0)
9820 cp += m->m_epg_1st_off;
9821 cp += left;
9822 cp2 = (char *)(void *)PHYS_TO_DMAP(m2->m_epg_pa[0]);
9823 if (pgno == m->m_epg_npgs - 1)
9824 m2->m_epg_last_len = trim;
9825 else {
9826 cp2 += PAGE_SIZE - trim;
9827 m2->m_epg_1st_off = PAGE_SIZE - trim;
9828 m2->m_epg_last_len = m->m_epg_last_len;
9829 }
9830 memcpy(cp2, cp, trim);
9831 m2->m_len = trim;
9832 } else {
9833 m2->m_len = 0;
9834 m2->m_epg_last_len = m->m_epg_last_len;
9835 }
9836
9837 /* Move the pages beyond pgno to the new mbuf. */
9838 for (i = pgno + 1, j = m2->m_epg_npgs; i < m->m_epg_npgs; i++, j++) {
9839 m2->m_epg_pa[j] = m->m_epg_pa[i];
9840 /* Never moves page 0. */
9841 m2->m_len += m_epg_pagelen(m, i, 0);
9842 }
9843 m2->m_epg_npgs = j;
9844 m->m_epg_npgs = pgno + 1;
9845 m->m_epg_last_len = left;
9846 m->m_len = xfer;
9847
9848 m2->m_next = m->m_next;
9849 m->m_next = NULL;
9850 return (m2);
9851 }
9852
9853 /*
9854 * Do the NFSv4.1 Bind Connection to Session.
9855 * Called from the reconnect layer of the krpc (sys/rpc/clnt_rc.c).
9856 */
9857 void
nfsrpc_bindconnsess(CLIENT * cl,void * arg,struct ucred * cr)9858 nfsrpc_bindconnsess(CLIENT *cl, void *arg, struct ucred *cr)
9859 {
9860 struct nfscl_reconarg *rcp = (struct nfscl_reconarg *)arg;
9861 uint32_t res, *tl;
9862 struct nfsrv_descript nfsd;
9863 struct nfsrv_descript *nd = &nfsd;
9864 struct rpc_callextra ext;
9865 struct timeval utimeout;
9866 enum clnt_stat stat;
9867 int error;
9868
9869 nfscl_reqstart(nd, NFSPROC_BINDCONNTOSESS, NULL, NULL, 0, NULL, NULL,
9870 NFS_VER4, rcp->minorvers, NULL);
9871 NFSM_BUILD(tl, uint32_t *, NFSX_V4SESSIONID + 2 * NFSX_UNSIGNED);
9872 memcpy(tl, rcp->sessionid, NFSX_V4SESSIONID);
9873 tl += NFSX_V4SESSIONID / NFSX_UNSIGNED;
9874 *tl++ = txdr_unsigned(NFSCDFC4_FORE_OR_BOTH);
9875 *tl = newnfs_false;
9876
9877 memset(&ext, 0, sizeof(ext));
9878 utimeout.tv_sec = 30;
9879 utimeout.tv_usec = 0;
9880 ext.rc_auth = authunix_create(cr);
9881 nd->nd_mrep = NULL;
9882 stat = CLNT_CALL_MBUF(cl, &ext, NFSV4PROC_COMPOUND, nd->nd_mreq,
9883 &nd->nd_mrep, utimeout);
9884 AUTH_DESTROY(ext.rc_auth);
9885 if (stat != RPC_SUCCESS) {
9886 printf("nfsrpc_bindconnsess: call failed stat=%d\n", stat);
9887 return;
9888 }
9889 if (nd->nd_mrep == NULL) {
9890 printf("nfsrpc_bindconnsess: no reply args\n");
9891 return;
9892 }
9893 error = 0;
9894 newnfs_realign(&nd->nd_mrep, M_WAITOK);
9895 nd->nd_md = nd->nd_mrep;
9896 nd->nd_dpos = mtod(nd->nd_md, char *);
9897 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
9898 nd->nd_repstat = fxdr_unsigned(uint32_t, *tl++);
9899 if (nd->nd_repstat == NFSERR_OK) {
9900 res = fxdr_unsigned(uint32_t, *tl);
9901 if (res > 0 && (error = nfsm_advance(nd, NFSM_RNDUP(res),
9902 -1)) != 0)
9903 goto nfsmout;
9904 NFSM_DISSECT(tl, uint32_t *, NFSX_V4SESSIONID +
9905 4 * NFSX_UNSIGNED);
9906 tl += 3;
9907 if (!NFSBCMP(tl, rcp->sessionid, NFSX_V4SESSIONID)) {
9908 tl += NFSX_V4SESSIONID / NFSX_UNSIGNED;
9909 res = fxdr_unsigned(uint32_t, *tl);
9910 if (res != NFSCDFS4_BOTH)
9911 printf("nfsrpc_bindconnsess: did not "
9912 "return FS4_BOTH\n");
9913 } else
9914 printf("nfsrpc_bindconnsess: not same "
9915 "sessionid\n");
9916 } else if (nd->nd_repstat != NFSERR_BADSESSION)
9917 printf("nfsrpc_bindconnsess: returned %d\n", nd->nd_repstat);
9918 nfsmout:
9919 if (error != 0)
9920 printf("nfsrpc_bindconnsess: reply bad xdr\n");
9921 m_freem(nd->nd_mrep);
9922 }
9923
9924 /*
9925 * nfs opeattr rpc
9926 */
9927 int
nfsrpc_openattr(struct nfsmount * nmp,struct vnode * vp,uint8_t * fhp,int fhlen,bool createit,struct ucred * cred,NFSPROC_T * p,struct nfsvattr * nap,struct nfsfh ** nfhpp,int * attrflagp)9928 nfsrpc_openattr(struct nfsmount *nmp, struct vnode *vp, uint8_t *fhp, int fhlen,
9929 bool createit, struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap,
9930 struct nfsfh **nfhpp, int *attrflagp)
9931 {
9932 uint32_t *tl;
9933 struct nfsrv_descript nfsd, *nd = &nfsd;
9934 nfsattrbit_t attrbits;
9935 int error = 0;
9936
9937 *attrflagp = 0;
9938 nfscl_reqstart(nd, NFSPROC_OPENATTR, nmp, fhp, fhlen, NULL, NULL, 0, 0,
9939 cred);
9940 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED);
9941 if (createit)
9942 *tl = newnfs_true;
9943 else
9944 *tl = newnfs_false;
9945 NFSGETATTR_ATTRBIT(&attrbits);
9946 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_UNSIGNED);
9947 *tl++ = txdr_unsigned(NFSV4OP_GETFH);
9948 *tl = txdr_unsigned(NFSV4OP_GETATTR);
9949 (void)nfsrv_putattrbit(nd, &attrbits);
9950 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, vp, p, cred,
9951 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
9952 if (error != 0)
9953 return (error);
9954 if (nd->nd_repstat == 0) {
9955 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
9956 error = nfsm_getfh(nd, nfhpp);
9957 if (error != 0)
9958 goto nfsmout;
9959 error = nfscl_postop_attr(nd, nap, attrflagp);
9960 }
9961 nfsmout:
9962 m_freem(nd->nd_mrep);
9963 if (error == 0 && nd->nd_repstat != 0)
9964 error = nd->nd_repstat;
9965 return (error);
9966 }
9967
9968 /*
9969 * Do roughly what nfs_statfs() does for NFSv4, but when called with a shared
9970 * locked vnode.
9971 */
9972 static void
nfscl_statfs(struct vnode * vp,struct ucred * cred,NFSPROC_T * td)9973 nfscl_statfs(struct vnode *vp, struct ucred *cred, NFSPROC_T *td)
9974 {
9975 struct nfsvattr nfsva;
9976 struct nfsfsinfo fs;
9977 struct nfsstatfs sb;
9978 struct mount *mp;
9979 struct nfsmount *nmp;
9980 uint32_t clone_blksize, lease;
9981 int attrflag, error;
9982
9983 mp = vp->v_mount;
9984 nmp = VFSTONFS(mp);
9985 error = nfsrpc_statfs(vp, &sb, &fs, &lease, &clone_blksize, cred, td,
9986 &nfsva, &attrflag);
9987 if (attrflag != 0)
9988 (void) nfscl_loadattrcache(&vp, &nfsva, NULL, 0, 1);
9989 if (error == 0) {
9990 NFSLOCKCLSTATE();
9991 if (nmp->nm_clp != NULL)
9992 nmp->nm_clp->nfsc_renew = NFSCL_RENEW(lease);
9993 NFSUNLOCKCLSTATE();
9994 mtx_lock(&nmp->nm_mtx);
9995 nfscl_loadfsinfo(nmp, &fs, clone_blksize);
9996 nfscl_loadsbinfo(nmp, &sb, &mp->mnt_stat);
9997 mp->mnt_stat.f_iosize = newnfs_iosize(nmp);
9998 mtx_unlock(&nmp->nm_mtx);
9999 }
10000 }
10001