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