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