1/*
2 * Copyright (c) 2000-2007 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28/* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
29/*
30 * Copyright (c) 1989, 1993
31 *	The Regents of the University of California.  All rights reserved.
32 *
33 * This code is derived from software contributed to Berkeley by
34 * Rick Macklem at The University of Guelph.
35 *
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
39 * 1. Redistributions of source code must retain the above copyright
40 *    notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 *    notice, this list of conditions and the following disclaimer in the
43 *    documentation and/or other materials provided with the distribution.
44 * 3. All advertising materials mentioning features or use of this software
45 *    must display the following acknowledgement:
46 *	This product includes software developed by the University of
47 *	California, Berkeley and its contributors.
48 * 4. Neither the name of the University nor the names of its contributors
49 *    may be used to endorse or promote products derived from this software
50 *    without specific prior written permission.
51 *
52 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
53 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
56 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62 * SUCH DAMAGE.
63 *
64 *	@(#)nfsm_subs.h	8.2 (Berkeley) 3/30/95
65 * FreeBSD-Id: nfsm_subs.h,v 1.13 1997/07/16 09:06:30 dfr Exp $
66 */
67
68
69#ifndef _NFS_NFSM_SUBS_H_
70#define _NFS_NFSM_SUBS_H_
71
72#include <sys/appleapiopts.h>
73
74#ifdef __APPLE_API_PRIVATE
75
76int nfsm_rpchead(struct nfsreq *, int, mbuf_t, u_int64_t *, mbuf_t *);
77int nfsm_rpchead2(int, int, int, int, int, int, kauth_cred_t, struct nfsreq *, mbuf_t, u_int64_t *, mbuf_t *);
78
79int nfsm_chain_new_mbuf(struct nfsm_chain *, size_t);
80int nfsm_chain_add_opaque_f(struct nfsm_chain *, const u_char *, uint32_t);
81int nfsm_chain_add_opaque_nopad_f(struct nfsm_chain *, const u_char *, uint32_t);
82int nfsm_chain_add_uio(struct nfsm_chain *, struct uio *, uint32_t);
83int nfsm_chain_add_fattr4_f(struct nfsm_chain *, struct vnode_attr *, struct nfsmount *);
84int nfsm_chain_add_v2sattr_f(struct nfsm_chain *, struct vnode_attr *, uint32_t);
85int nfsm_chain_add_v3sattr_f(struct nfsm_chain *, struct vnode_attr *);
86
87int nfsm_chain_advance(struct nfsm_chain *, uint32_t);
88int nfsm_chain_offset(struct nfsm_chain *);
89int nfsm_chain_reverse(struct nfsm_chain *, uint32_t);
90int nfsm_chain_get_opaque_pointer_f(struct nfsm_chain *, uint32_t, u_char **);
91int nfsm_chain_get_opaque_f(struct nfsm_chain *, uint32_t, u_char *);
92int nfsm_chain_get_uio(struct nfsm_chain *, uint32_t, struct uio *);
93int nfsm_chain_get_fh_attr(struct nfsm_chain *, nfsnode_t,
94	vfs_context_t, int, uint64_t *, fhandle_t *, struct nfs_vattr *);
95int nfsm_chain_get_wcc_data_f(struct nfsm_chain *, nfsnode_t, struct timespec *, int *, u_int64_t *);
96
97#if NFSSERVER
98void nfsm_adj(mbuf_t, int, int);
99int nfsm_mbuf_get_list(size_t, mbuf_t *, int *);
100
101int nfsm_chain_add_fattr(struct nfsrv_descript *, struct nfsm_chain *, struct vnode_attr *);
102int nfsm_chain_add_wcc_data_f(struct nfsrv_descript *, struct nfsm_chain *, int,
103	struct vnode_attr *, int, struct vnode_attr *);
104int nfsm_chain_get_path_namei(struct nfsm_chain *, uint32_t, struct nameidata *);
105int nfsm_chain_get_sattr(struct nfsrv_descript *, struct nfsm_chain *, struct vnode_attr *);
106int nfsm_chain_trim_data(struct nfsm_chain *, int, int *);
107#endif /* NFSSERVER */
108
109/* check name length */
110#define nfsm_name_len_check(E, ND, LEN) \
111	do { \
112		if (E) break; \
113		if (((ND)->nd_vers == NFS_VER2) && ((LEN) > NFS_MAXNAMLEN)) \
114			(E) = NFSERR_NAMETOL; \
115		if ((LEN) <= 0) \
116			error = EBADRPC; \
117	} while (0)
118
119#define nfsm_assert(E, COND, ERR) \
120	do { \
121		if (E) break; \
122		if (!(COND)) \
123			(E) = (ERR); \
124	} while (0)
125
126/* Initialize a vnode_attr to retrieve attributes for the NFS server. */
127#define nfsm_srv_vattr_init(VAP, VERS) \
128	do { \
129		VATTR_INIT(VAP); \
130		VATTR_WANTED((VAP), va_type); \
131		VATTR_WANTED((VAP), va_mode); \
132		VATTR_WANTED((VAP), va_nlink); \
133		VATTR_WANTED((VAP), va_uid); \
134		VATTR_WANTED((VAP), va_gid); \
135		VATTR_WANTED((VAP), va_data_size); \
136		VATTR_WANTED((VAP), va_data_alloc); \
137		VATTR_WANTED((VAP), va_rdev); \
138		VATTR_WANTED((VAP), va_fsid); \
139		VATTR_WANTED((VAP), va_fileid); \
140		VATTR_WANTED((VAP), va_access_time); \
141		VATTR_WANTED((VAP), va_modify_time); \
142		VATTR_WANTED((VAP), va_change_time); \
143		if ((VERS) == NFS_VER2) \
144			VATTR_WANTED((VAP), va_iosize); \
145	} while (0)
146
147/* Initialize a vnode_attr to retrieve pre-operation attributes for the NFS server. */
148#define nfsm_srv_pre_vattr_init(VAP) \
149	do { \
150		VATTR_INIT(VAP); \
151		VATTR_WANTED((VAP), va_data_size); \
152		VATTR_WANTED((VAP), va_modify_time); \
153		VATTR_WANTED((VAP), va_change_time); \
154	} while (0)
155
156/* round up to a multiple of 4 */
157#define nfsm_rndup(a)	(((a)+3)&(~0x3))
158
159#define nfsm_pad(a)	(nfsm_rndup(a) - (a))
160
161/*
162 * control flow macros:
163 * go to the appropriate label on condition
164 */
165#define nfsmout_if(E)	do { if (E) goto nfsmout; } while (0)
166#define nfsmerr_if(E)	do { if (E) goto nfsmerr; } while (0)
167
168/*
169 * For NFS v2 errors and EBADRPC, the reply contains only the error.
170 * This macro is used to skip any reply building code and go straight
171 * to nfsmout instead.
172 */
173#define nfsmout_on_status(ND, E) \
174	do { \
175		if (((ND)->nd_repstat == EBADRPC) || \
176		    ((ND)->nd_repstat && ((ND)->nd_vers == NFS_VER2))) { \
177			(E) = 0; \
178			goto nfsmout; \
179		} \
180	} while (0)
181
182/* initialize an mbuf chain */
183#define nfsm_chain_null(NMC) \
184	do { \
185		(NMC)->nmc_mhead = (NMC)->nmc_mcur = NULL; \
186		(NMC)->nmc_ptr = NULL; \
187	} while (0)
188
189/* cleanup an mbuf chain */
190#define nfsm_chain_cleanup(NMC) \
191	do { \
192		if (!(NMC)->nmc_mhead) break; \
193		mbuf_freem((NMC)->nmc_mhead); \
194		nfsm_chain_null(NMC); \
195	} while (0)
196
197/* get an mbuf given a size hint */
198#define nfsm_mbuf_get(E, MBP, SIZEHINT) \
199	do { \
200		*(MBP) = NULL; \
201		if ((size_t)(SIZEHINT) >= nfs_mbuf_minclsize) \
202			(E) = mbuf_mclget(MBUF_WAITOK, MBUF_TYPE_DATA, (MBP)); \
203		else \
204			(E) = mbuf_get(MBUF_WAITOK, MBUF_TYPE_DATA, (MBP)); \
205	} while (0)
206
207
208/*
209 * macros for building NFS mbuf chains
210 */
211
212/* prepare an mbuf chain for building starting with the given mbuf */
213#define nfsm_chain_init(NMC, MB) \
214	do { \
215		(NMC)->nmc_mhead = (MB); \
216		(NMC)->nmc_mcur = (NMC)->nmc_mhead; \
217		(NMC)->nmc_ptr = mbuf_data((NMC)->nmc_mcur); \
218		(NMC)->nmc_left = mbuf_trailingspace((NMC)->nmc_mcur); \
219		(NMC)->nmc_flags = 0; \
220	} while (0)
221
222/* prepare an mbuf chain for building starting with a newly allocated mbuf */
223#define nfsm_chain_build_alloc_init(E, NMC, SIZEHINT) \
224	do { \
225		mbuf_t ncbimb; \
226		nfsm_mbuf_get((E), &ncbimb, (SIZEHINT)); \
227		if (E) break; \
228		nfsm_chain_init((NMC), ncbimb); \
229	} while (0)
230
231/* done building an mbuf chain */
232#define nfsm_chain_build_done(E, NMC) \
233	do { \
234		if ((E) || !(NMC)->nmc_mcur) break; \
235		/* cap off current mbuf */ \
236		mbuf_setlen((NMC)->nmc_mcur, \
237			(NMC)->nmc_ptr - (caddr_t)mbuf_data((NMC)->nmc_mcur)); \
238	} while (0)
239
240/*
241 *  set the TCP record mark at the head of an mbuf chain -
242 *  assumes 4 bytes are already allocated in the head mbuf
243 */
244#define nfsm_chain_set_recmark(E, NMC, VAL) \
245	do { \
246		if (E) break; \
247		*((uint32_t*)mbuf_data((NMC)->nmc_mhead)) \
248			= txdr_unsigned(VAL); \
249	} while (0)
250
251/* make sure there's room for size bytes in current mbuf */
252#define nfsm_chain_check_size(E, NMC, SIZE) \
253	do { \
254		if (E) break; \
255		if ((NMC)->nmc_left < (SIZE)) { \
256			(E) = nfsm_chain_new_mbuf((NMC), (SIZE)); \
257			if (!(E) && ((NMC)->nmc_left < (SIZE))) \
258				(E) = ENOMEM; \
259		} \
260	} while (0)
261
262/* add a 32bit value to an mbuf chain extending if necessary */
263#define nfsm_chain_add_32(E, NMC, VAL) \
264	do { \
265		nfsm_chain_check_size((E), (NMC), NFSX_UNSIGNED); \
266		if (E) break; \
267		*((uint32_t*)(NMC)->nmc_ptr) = txdr_unsigned(VAL); \
268		(NMC)->nmc_ptr += NFSX_UNSIGNED; \
269		(NMC)->nmc_left -= NFSX_UNSIGNED; \
270	} while (0)
271
272/* add a 64bit value to an mbuf chain */
273#define nfsm_chain_add_64(E, NMC, VAL) \
274	do { \
275		uint64_t __tmp64; \
276		nfsm_chain_check_size((E), (NMC), 2 * NFSX_UNSIGNED); \
277		if (E) break; \
278		__tmp64 = (VAL); \
279		txdr_hyper(&__tmp64, (NMC)->nmc_ptr); \
280		(NMC)->nmc_ptr += 2 * NFSX_UNSIGNED; \
281		(NMC)->nmc_left -= 2 * NFSX_UNSIGNED; \
282	} while (0)
283
284/* zero the last 4 bytes for a range of opaque */
285/* data to make sure any pad bytes will be zero. */
286#define nfsm_chain_zero_opaque_pad(BUF, LEN) \
287	*(((uint32_t*)(BUF))+((nfsm_rndup(LEN)>>2)-1)) = 0
288
289/* add buffer of opaque data to an mbuf chain */
290#define nfsm_chain_add_opaque(E, NMC, BUF, LEN) \
291	do { \
292		uint32_t rndlen = nfsm_rndup(LEN); \
293		if (E) break; \
294		if ((NMC)->nmc_left < rndlen) { \
295			(E) = nfsm_chain_add_opaque_f((NMC), (const u_char*)(BUF), (LEN)); \
296			break; \
297		} \
298		nfsm_chain_zero_opaque_pad((NMC)->nmc_ptr, (LEN)); \
299		bcopy((BUF), (NMC)->nmc_ptr, (LEN)); \
300		(NMC)->nmc_ptr += rndlen; \
301		(NMC)->nmc_left -= rndlen; \
302	} while (0)
303
304/* add buffer of opaque data to an mbuf chain without padding */
305#define nfsm_chain_add_opaque_nopad(E, NMC, BUF, LEN) \
306	do { \
307		if (E) break; \
308		if ((NMC)->nmc_left < (uint32_t) (LEN)) { \
309			(E) = nfsm_chain_add_opaque_nopad_f((NMC), (const u_char*)(BUF), (LEN)); \
310			break; \
311		} \
312		bcopy((BUF), (NMC)->nmc_ptr, (LEN)); \
313		(NMC)->nmc_ptr += (LEN); \
314		(NMC)->nmc_left -= (LEN); \
315	} while (0)
316
317/* finish an mbuf in a chain to allow subsequent insertion */
318#define nfsm_chain_finish_mbuf(E, NMC) \
319	do { \
320		if (E) break; \
321		mbuf_setlen((NMC)->nmc_mcur, \
322			(NMC)->nmc_ptr - (caddr_t)mbuf_data((NMC)->nmc_mcur)); \
323		(NMC)->nmc_left = 0; \
324	} while (0)
325
326/* add a file handle to an mbuf chain */
327#define nfsm_chain_add_fh(E, NMC, VERS, FHP, FHLEN) \
328	do { \
329		if (E) break; \
330		if ((VERS) != NFS_VER2) \
331			nfsm_chain_add_32((E), (NMC), (FHLEN)); \
332		nfsm_chain_add_opaque((E), (NMC), (FHP), (FHLEN)); \
333	} while (0)
334
335/* add a string to an mbuf chain */
336#define nfsm_chain_add_string(E, NMC, STR, LEN) \
337	do { \
338		nfsm_chain_add_32((E), (NMC), (LEN)); \
339		nfsm_chain_add_opaque((E), (NMC), (STR), (LEN)); \
340	} while (0)
341
342/* add an NFSv2 time to an mbuf chain */
343#define nfsm_chain_add_v2time(E, NMC, TVP) \
344	do { \
345		if (TVP) { \
346			nfsm_chain_add_32((E), (NMC), (TVP)->tv_sec); \
347			nfsm_chain_add_32((E), (NMC), ((TVP)->tv_nsec != -1) ? \
348				((uint32_t)(TVP)->tv_nsec / 1000) : 0xffffffff); \
349		} else { \
350			/* no time... use -1 */ \
351			nfsm_chain_add_32((E), (NMC), -1); \
352			nfsm_chain_add_32((E), (NMC), -1); \
353		} \
354	} while (0)
355
356/* add an NFSv3 time to an mbuf chain */
357#define nfsm_chain_add_v3time(E, NMC, TVP) \
358	do { \
359		nfsm_chain_add_32((E), (NMC), (TVP)->tv_sec); \
360		nfsm_chain_add_32((E), (NMC), (TVP)->tv_nsec); \
361	} while (0)
362
363/* add an NFS v2 or v3 time to an mbuf chain */
364#define nfsm_chain_add_time(E, NMC, VERS, TVP) \
365	do { \
366		if ((VERS) == NFS_VER2) { \
367			nfsm_chain_add_v2time((E), (NMC), (TVP)); \
368		} else { \
369			nfsm_chain_add_v3time((E), (NMC), (TVP)); \
370		} \
371	} while (0)
372
373/* add an NFSv3 postop file handle to an mbuf chain */
374#define nfsm_chain_add_postop_fh(E, NMC, FHP, FHLEN) \
375	do { \
376		nfsm_chain_add_32((E), (NMC), TRUE); \
377		nfsm_chain_add_fh((E), (NMC), NFS_VER3, (FHP), (FHLEN)); \
378	} while (0)
379
380/* add NFSv3 postop attributes to an mbuf chain */
381#define nfsm_chain_add_postop_attr(E, ND, NMC, ATTRERR, VAP) \
382	do { \
383		if (E) break; \
384		if (ATTRERR) { \
385			nfsm_chain_add_32((E), (NMC), FALSE); \
386			break; \
387		} \
388		nfsm_chain_add_32((E), (NMC), TRUE); \
389		if (E) break; \
390		(E) = nfsm_chain_add_fattr((ND), (NMC), (VAP)); \
391	} while (0)
392
393/* Add an NFSv2 "sattr" structure to an mbuf chain */
394#define nfsm_chain_add_v2sattr(E, NMC, VAP, SZRDEV) \
395	do { \
396		if (E) break; \
397		(E) = nfsm_chain_add_v2sattr_f((NMC), (VAP), (SZRDEV)); \
398	} while (0)
399
400/* Add an NFSv3 "sattr" structure to an mbuf chain */
401#define nfsm_chain_add_v3sattr(E, NMC, VAP) \
402	do { \
403		if (E) break; \
404		(E) = nfsm_chain_add_v3sattr_f((NMC), (VAP)); \
405	} while (0)
406
407/* Add an NFSv4 "fattr" structure to an mbuf chain */
408#define nfsm_chain_add_fattr4(E, NMC, VAP, NMP) \
409	do { \
410		if (E) break; \
411		(E) = nfsm_chain_add_fattr4_f((NMC), (VAP), (NMP)); \
412	} while (0)
413
414/* add NFSv3 WCC data to an mbuf chain */
415#define nfsm_chain_add_wcc_data(E, ND, NMC, PREERR, PREVAP, POSTERR, POSTVAP) \
416	do { \
417		if (E) break; \
418		(E) = nfsm_chain_add_wcc_data_f((ND), (NMC), \
419			(PREERR), (PREVAP), (POSTERR), (POSTVAP)); \
420	} while (0)
421
422/* add NFSv4 COMPOUND header */
423#define nfsm_chain_add_compound_header(E, NMC, TAG, NUMOPS) \
424	do { \
425		if ((TAG) && strlen(TAG)) \
426			nfsm_chain_add_string((E), (NMC), (TAG), strlen(TAG)); \
427		else \
428			nfsm_chain_add_32((E), (NMC), 0); \
429		nfsm_chain_add_32((E), (NMC), 0); /*minorversion*/ \
430		nfsm_chain_add_32((E), (NMC), (NUMOPS)); \
431	} while (0)
432
433/* add NFSv4 attr bitmap */
434#define nfsm_chain_add_bitmap(E, NMC, B, LEN) \
435	do { \
436		int __i; \
437		nfsm_chain_add_32((E), (NMC), (LEN)); \
438		for (__i=0; __i < (LEN); __i++) \
439			nfsm_chain_add_32((E), (NMC), (B)[__i]); \
440	} while (0)
441
442/* add NFSv4 attr bitmap masked with the given mask */
443#define nfsm_chain_add_bitmap_masked(E, NMC, B, LEN, MASK) \
444	do { \
445		int __i; \
446		nfsm_chain_add_32((E), (NMC), (LEN)); \
447		for (__i=0; __i < (LEN); __i++) \
448			nfsm_chain_add_32((E), (NMC), ((B)[__i] & (MASK)[__i])); \
449	} while (0)
450
451/*
452 * macros for dissecting NFS mbuf chains
453 */
454
455/* prepare an mbuf chain for dissection starting with the given mbuf */
456#define nfsm_chain_dissect_init(E, NMC, H) \
457	do { \
458		if (!(H)) { \
459			(E) = EINVAL; \
460			break; \
461		} \
462		(NMC)->nmc_mcur = (NMC)->nmc_mhead = (H); \
463		(NMC)->nmc_ptr = mbuf_data(H); \
464		(NMC)->nmc_left = mbuf_len(H); \
465	} while (0)
466
467/* skip a number of bytes in an mbuf chain */
468#define nfsm_chain_adv(E, NMC, LEN) \
469	do { \
470		if (E) break; \
471		if ((NMC)->nmc_left >= (uint32_t)(LEN)) { \
472			(NMC)->nmc_left -= (LEN); \
473			(NMC)->nmc_ptr += (LEN); \
474		} else { \
475			(E) = nfsm_chain_advance((NMC), (LEN)); \
476		} \
477	} while (0)
478
479/* get a 32bit value from an mbuf chain */
480#define nfsm_chain_get_32(E, NMC, LVAL) \
481	do { \
482		uint32_t __tmp32, *__tmpptr; \
483		if (E) break; \
484		if ((NMC)->nmc_left >= NFSX_UNSIGNED) { \
485			__tmpptr = (uint32_t*)(NMC)->nmc_ptr; \
486			(NMC)->nmc_left -= NFSX_UNSIGNED; \
487			(NMC)->nmc_ptr += NFSX_UNSIGNED; \
488		} else { \
489			__tmpptr = &__tmp32; \
490			(E) = nfsm_chain_get_opaque_f((NMC), NFSX_UNSIGNED, (u_char*)__tmpptr); \
491			if (E) break; \
492		} \
493		(LVAL) = fxdr_unsigned(uint32_t, *__tmpptr); \
494	} while (0)
495
496/* get a 64bit value from an mbuf chain */
497#define nfsm_chain_get_64(E, NMC, LVAL) \
498	do { \
499		uint64_t __tmp64, *__tmpptr; \
500		if (E) break; \
501		if ((NMC)->nmc_left >= 2 * NFSX_UNSIGNED) { \
502			__tmpptr = (uint64_t*)(NMC)->nmc_ptr; \
503			(NMC)->nmc_left -= 2 * NFSX_UNSIGNED; \
504			(NMC)->nmc_ptr += 2 * NFSX_UNSIGNED; \
505		} else { \
506			__tmpptr = &__tmp64; \
507			(E) = nfsm_chain_get_opaque_f((NMC), 2 * NFSX_UNSIGNED, (u_char*)__tmpptr); \
508			if (E) break; \
509		} \
510		fxdr_hyper(__tmpptr, &(LVAL)); \
511	} while (0)
512
513/* get a pointer to the next consecutive bytes in an mbuf chain */
514#define nfsm_chain_get_opaque_pointer(E, NMC, LEN, PTR) \
515	do { \
516		uint32_t rndlen; \
517		if (E) break; \
518		rndlen = nfsm_rndup(LEN); \
519		if ((NMC)->nmc_left >= rndlen) { \
520			(PTR) = (void*)(NMC)->nmc_ptr; \
521			(NMC)->nmc_left -= rndlen; \
522			(NMC)->nmc_ptr += rndlen; \
523		} else { \
524			(E) = nfsm_chain_get_opaque_pointer_f((NMC), (LEN), (u_char**)&(PTR)); \
525		} \
526	} while (0)
527
528/* copy the next consecutive bytes of opaque data from an mbuf chain */
529#define nfsm_chain_get_opaque(E, NMC, LEN, PTR) \
530	do { \
531		uint32_t rndlen; \
532		if (E) break; \
533		rndlen = nfsm_rndup(LEN); \
534		if ((NMC)->nmc_left >= rndlen) { \
535			u_char *__tmpptr = (u_char*)(NMC)->nmc_ptr; \
536			(NMC)->nmc_left -= rndlen; \
537			(NMC)->nmc_ptr += rndlen; \
538			bcopy(__tmpptr, (PTR), (LEN)); \
539		} else { \
540			(E) = nfsm_chain_get_opaque_f((NMC), (LEN), (u_char*)(PTR)); \
541		} \
542	} while (0)
543
544/* get the size of and a pointer to a file handle in an mbuf chain */
545#define nfsm_chain_get_fh_ptr(E, NMC, VERS, FHP, FHSIZE) \
546	do { \
547		if ((VERS) != NFS_VER2) \
548			nfsm_chain_get_32((E), (NMC), (FHSIZE)); \
549		else \
550			(FHSIZE) = NFSX_V2FH;\
551		nfsm_chain_get_opaque_pointer((E), (NMC), (FHSIZE), (FHP));\
552	} while (0)
553
554/* get the size of and data for a file handle in an mbuf chain */
555#define nfsm_chain_get_fh(E, NMC, VERS, FHP) \
556	do { \
557		if ((VERS) != NFS_VER2) \
558			nfsm_chain_get_32((E), (NMC), (FHP)->fh_len); \
559		else \
560			(FHP)->fh_len = NFSX_V2FH;\
561		nfsm_chain_get_opaque((E), (NMC), (uint32_t)(FHP)->fh_len, (FHP)->fh_data);\
562		if (E) \
563			(FHP)->fh_len = 0;\
564	} while (0)
565
566/* get an NFS v2 or v3 time from an mbuf chain */
567#define nfsm_chain_get_time(E, NMC, VERS, TSEC, TNSEC) \
568	do { \
569		nfsm_chain_get_32((E), (NMC), (TSEC)); \
570		nfsm_chain_get_32((E), (NMC), (TNSEC)); \
571		if ((E) || ((VERS) != NFS_VER2)) break; \
572		if ((uint32_t)(TNSEC) == 0xffffffff) \
573			(TNSEC) = 0; \
574		else \
575			(TNSEC) *= 1000; \
576	} while (0)
577
578/* get postop attributes from an mbuf chain */
579#define nfsm_chain_postop_attr_get(E, NMC, F, VAP) \
580	do { \
581		(F) = 0; \
582		if ((E) || !(NMC)->nmc_mhead) break; \
583		nfsm_chain_get_32((E), (NMC), (F)); \
584		if ((E) || !(F)) break; \
585		if (((E) = nfs_parsefattr((NMC), NFS_VER3, (VAP)))) \
586			(F) = 0; \
587	} while (0)
588
589/* update a node's attribute cache with postop attributes from an mbuf chain */
590/* (F returns whether the attributes were updated or not) */
591#define nfsm_chain_postop_attr_update_flag(E, NMC, NP, F, X) \
592	do { \
593		struct nfs_vattr ttvattr; \
594		nfsm_chain_postop_attr_get((E), (NMC), (F), &ttvattr); \
595		if ((E) || !(F)) break; \
596		if (((E) = nfs_loadattrcache((NP), &ttvattr, (X), 1))) { \
597			(F) = 0; \
598			break; \
599		} \
600		if (*(X) == 0) \
601			(F) = 0; \
602	} while (0)
603
604/* update a node's attribute cache with postop attributes from an mbuf chain */
605#define nfsm_chain_postop_attr_update(E, NMC, NP, X) \
606	do { \
607		int __dummy_flag = 0; \
608		nfsm_chain_postop_attr_update_flag((E), (NMC), (NP), __dummy_flag, (X)); \
609	} while (0)
610
611/* get and process NFSv3 WCC data from an mbuf chain */
612#define nfsm_chain_get_wcc_data(E, NMC, NP, PREMTIME, NEWPOSTATTR, X) \
613	do { \
614		if (E) break; \
615		(E) = nfsm_chain_get_wcc_data_f((NMC), (NP), (PREMTIME), (NEWPOSTATTR), (X)); \
616	} while (0)
617
618/* update a node's attribute cache with attributes from an mbuf chain */
619#define nfsm_chain_loadattr(E, NMC, NP, VERS, A, X) \
620	do { \
621		struct nfs_vattr ttvattr, *ttnvap; \
622		if (E) break; \
623		ttnvap = (A) ? (A) : &ttvattr; \
624		if ((VERS) == NFS_VER4) { \
625			NFS_CLEAR_ATTRIBUTES(ttnvap->nva_bitmap); \
626			(E) = nfs4_parsefattr((NMC), NULL, ttnvap, NULL, NULL); \
627		} else { \
628			(E) = nfs_parsefattr((NMC), (VERS), ttnvap); \
629		} \
630		if (E) break; \
631		(E) = nfs_loadattrcache((NP), ttnvap, (X), 0); \
632	} while (0)
633
634/* get NFSv4 attr bitmap */
635#define nfsm_chain_get_bitmap(E, NMC, B, LEN) \
636	do { \
637		uint32_t __len, __i; \
638		nfsm_chain_get_32((E), (NMC), __len); \
639		if (E) break; \
640		for (__i=0; __i < MIN(__len, (LEN)); __i++) \
641			nfsm_chain_get_32((E), (NMC), (B)[__i]); \
642		if (E) break; \
643		for (; __i < (LEN); __i++) \
644			(B)[__i] = 0; \
645		(LEN) = __len; \
646	} while (0)
647
648#define nfsm_chain_skip_tag(E, NMC) \
649	do { \
650		uint32_t __val; \
651		nfsm_chain_get_32((E), (NMC), __val); \
652		nfsm_chain_adv((E), (NMC), nfsm_rndup(__val)); \
653	} while (0)
654
655#define nfsm_chain_op_check(E, NMC, OP) \
656	do { \
657		uint32_t __val = 0; \
658		nfsm_chain_get_32((E), (NMC), __val); \
659		nfsm_assert((E), (__val == (OP)), EBADRPC); \
660		nfsm_chain_get_32((E), (NMC), __val); \
661		nfsm_assert((E), (__val == NFS_OK), __val); \
662	} while (0)
663
664#define nfsm_chain_check_change_info(E, NMC, DNP) \
665	do { \
666		uint64_t __ci_before, __ci_after; \
667		uint32_t __ci_atomic = 0; \
668		nfsm_chain_get_32((E), (NMC), __ci_atomic); \
669		nfsm_chain_get_64((E), (NMC), __ci_before); \
670		nfsm_chain_get_64((E), (NMC), __ci_after); \
671		if (E) break; \
672		if (__ci_atomic && (__ci_before == (DNP)->n_ncchange)) \
673			(DNP)->n_ncchange = __ci_after; \
674		else \
675			cache_purge(NFSTOV(DNP)); \
676	} while (0)
677
678#endif /* __APPLE_API_PRIVATE */
679#endif /* _NFS_NFSM_SUBS_H_ */
680