1/*
2 * Copyright (c) 2000-2011 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 *, mbuf_t, u_int64_t *, mbuf_t *);
77int nfsm_rpchead2(struct nfsmount *, 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 *, uio_t, 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 *);
86int nfsm_chain_add_string_nfc(struct nfsm_chain *, const uint8_t *, uint32_t);
87
88int nfsm_chain_advance(struct nfsm_chain *, uint32_t);
89int nfsm_chain_offset(struct nfsm_chain *);
90int nfsm_chain_reverse(struct nfsm_chain *, uint32_t);
91int nfsm_chain_get_opaque_pointer_f(struct nfsm_chain *, uint32_t, u_char **);
92int nfsm_chain_get_opaque_f(struct nfsm_chain *, uint32_t, u_char *);
93int nfsm_chain_get_uio(struct nfsm_chain *, uint32_t, uio_t);
94int nfsm_chain_get_fh_attr(struct nfsm_chain *, nfsnode_t,
95	vfs_context_t, int, uint64_t *, fhandle_t *, struct nfs_vattr *);
96int nfsm_chain_get_wcc_data_f(struct nfsm_chain *, nfsnode_t, struct timespec *, int *, u_int64_t *);
97int nfsm_chain_get_secinfo(struct nfsm_chain *, uint32_t *, int *);
98
99#if NFSSERVER
100void nfsm_adj(mbuf_t, int, int);
101int nfsm_mbuf_get_list(size_t, mbuf_t *, int *);
102
103int nfsm_chain_add_fattr(struct nfsrv_descript *, struct nfsm_chain *, struct vnode_attr *);
104int nfsm_chain_add_wcc_data_f(struct nfsrv_descript *, struct nfsm_chain *, int,
105	struct vnode_attr *, int, struct vnode_attr *);
106int nfsm_chain_get_path_namei(struct nfsm_chain *, uint32_t, struct nameidata *);
107int nfsm_chain_get_sattr(struct nfsrv_descript *, struct nfsm_chain *, struct vnode_attr *);
108int nfsm_chain_trim_data(struct nfsm_chain *, int, int *);
109#endif /* NFSSERVER */
110
111/* check name length */
112#define nfsm_name_len_check(E, ND, LEN) \
113	do { \
114		if (E) break; \
115		if (((ND)->nd_vers == NFS_VER2) && ((LEN) > NFS_MAXNAMLEN)) \
116			(E) = NFSERR_NAMETOL; \
117		if ((LEN) <= 0) \
118			error = EBADRPC; \
119	} while (0)
120
121#define nfsm_assert(E, COND, ERR) \
122	do { \
123		if (E) break; \
124		if (!(COND)) \
125			(E) = (ERR); \
126	} while (0)
127
128/* Initialize a vnode_attr to retrieve attributes for the NFS server. */
129#define nfsm_srv_vattr_init(VAP, VERS) \
130	do { \
131		VATTR_INIT(VAP); \
132		VATTR_WANTED((VAP), va_type); \
133		VATTR_WANTED((VAP), va_mode); \
134		VATTR_WANTED((VAP), va_nlink); \
135		VATTR_WANTED((VAP), va_uid); \
136		VATTR_WANTED((VAP), va_gid); \
137		VATTR_WANTED((VAP), va_data_size); \
138		VATTR_WANTED((VAP), va_data_alloc); \
139		VATTR_WANTED((VAP), va_rdev); \
140		VATTR_WANTED((VAP), va_fsid); \
141		VATTR_WANTED((VAP), va_fileid); \
142		VATTR_WANTED((VAP), va_access_time); \
143		VATTR_WANTED((VAP), va_modify_time); \
144		VATTR_WANTED((VAP), va_change_time); \
145		if ((VERS) == NFS_VER2) \
146			VATTR_WANTED((VAP), va_iosize); \
147	} while (0)
148
149/* Initialize a vnode_attr to retrieve pre-operation attributes for the NFS server. */
150#define nfsm_srv_pre_vattr_init(VAP) \
151	do { \
152		VATTR_INIT(VAP); \
153		VATTR_WANTED((VAP), va_data_size); \
154		VATTR_WANTED((VAP), va_modify_time); \
155		VATTR_WANTED((VAP), va_change_time); \
156	} while (0)
157
158/* round up to a multiple of 4 */
159#define nfsm_rndup(a)	(((a)+3)&(~0x3))
160
161#define nfsm_pad(a)	(nfsm_rndup(a) - (a))
162
163/*
164 * control flow macros:
165 * go to the appropriate label on condition
166 */
167#define nfsmout_if(E)	do { if (E) goto nfsmout; } while (0)
168#define nfsmerr_if(E)	do { if (E) goto nfsmerr; } while (0)
169
170/*
171 * For NFS v2 errors and EBADRPC, the reply contains only the error.
172 * This macro is used to skip any reply building code and go straight
173 * to nfsmout instead.
174 */
175#define nfsmout_on_status(ND, E) \
176	do { \
177		if (((ND)->nd_repstat == EBADRPC) || \
178		    ((ND)->nd_repstat && ((ND)->nd_vers == NFS_VER2))) { \
179			(E) = 0; \
180			goto nfsmout; \
181		} \
182	} while (0)
183
184/* initialize an mbuf chain */
185#define nfsm_chain_null(NMC) \
186	do { \
187		(NMC)->nmc_mhead = (NMC)->nmc_mcur = NULL; \
188		(NMC)->nmc_ptr = NULL; \
189	} while (0)
190
191/* cleanup an mbuf chain */
192#define nfsm_chain_cleanup(NMC) \
193	do { \
194		if (!(NMC)->nmc_mhead) break; \
195		mbuf_freem((NMC)->nmc_mhead); \
196		nfsm_chain_null(NMC); \
197	} while (0)
198
199/* get an mbuf given a size hint */
200#define nfsm_mbuf_get(E, MBP, SIZEHINT) \
201	do { \
202		*(MBP) = NULL; \
203		if ((size_t)(SIZEHINT) >= nfs_mbuf_minclsize) \
204			(E) = mbuf_mclget(MBUF_WAITOK, MBUF_TYPE_DATA, (MBP)); \
205		else \
206			(E) = mbuf_get(MBUF_WAITOK, MBUF_TYPE_DATA, (MBP)); \
207	} while (0)
208
209
210/*
211 * macros for building NFS mbuf chains
212 */
213
214/* prepare an mbuf chain for building starting with the given mbuf */
215#define nfsm_chain_init(NMC, MB) \
216	do { \
217		(NMC)->nmc_mhead = (MB); \
218		(NMC)->nmc_mcur = (NMC)->nmc_mhead; \
219		(NMC)->nmc_ptr = mbuf_data((NMC)->nmc_mcur); \
220		(NMC)->nmc_left = mbuf_trailingspace((NMC)->nmc_mcur); \
221		(NMC)->nmc_flags = 0; \
222	} while (0)
223
224/* prepare an mbuf chain for building starting with a newly allocated mbuf */
225#define nfsm_chain_build_alloc_init(E, NMC, SIZEHINT) \
226	do { \
227		mbuf_t ncbimb; \
228		nfsm_mbuf_get((E), &ncbimb, (SIZEHINT)); \
229		if (E) break; \
230		nfsm_chain_init((NMC), ncbimb); \
231	} while (0)
232
233/* done building an mbuf chain */
234#define nfsm_chain_build_done(E, NMC) \
235	do { \
236		if ((E) || !(NMC)->nmc_mcur) break; \
237		/* cap off current mbuf */ \
238		mbuf_setlen((NMC)->nmc_mcur, \
239			(NMC)->nmc_ptr - (caddr_t)mbuf_data((NMC)->nmc_mcur)); \
240	} while (0)
241
242/*
243 *  set the TCP record mark at the head of an mbuf chain -
244 *  assumes 4 bytes are already allocated in the head mbuf
245 */
246#define nfsm_chain_set_recmark(E, NMC, VAL) \
247	do { \
248		if (E) break; \
249		*((uint32_t*)mbuf_data((NMC)->nmc_mhead)) \
250			= txdr_unsigned(VAL); \
251	} while (0)
252
253/* make sure there's room for size bytes in current mbuf */
254#define nfsm_chain_check_size(E, NMC, SIZE) \
255	do { \
256		if (E) break; \
257		if ((NMC)->nmc_left < (SIZE)) { \
258			(E) = nfsm_chain_new_mbuf((NMC), (SIZE)); \
259			if (!(E) && ((NMC)->nmc_left < (SIZE))) \
260				(E) = ENOMEM; \
261		} \
262	} while (0)
263
264/* add a 32bit value to an mbuf chain extending if necessary */
265#define nfsm_chain_add_32(E, NMC, VAL) \
266	do { \
267		nfsm_chain_check_size((E), (NMC), NFSX_UNSIGNED); \
268		if (E) break; \
269		*((uint32_t*)(NMC)->nmc_ptr) = txdr_unsigned(VAL); \
270		(NMC)->nmc_ptr += NFSX_UNSIGNED; \
271		(NMC)->nmc_left -= NFSX_UNSIGNED; \
272	} while (0)
273
274/* add a 64bit value to an mbuf chain */
275#define nfsm_chain_add_64(E, NMC, VAL) \
276	do { \
277		uint64_t __tmp64; \
278		nfsm_chain_check_size((E), (NMC), 2 * NFSX_UNSIGNED); \
279		if (E) break; \
280		__tmp64 = (VAL); \
281		txdr_hyper(&__tmp64, (NMC)->nmc_ptr); \
282		(NMC)->nmc_ptr += 2 * NFSX_UNSIGNED; \
283		(NMC)->nmc_left -= 2 * NFSX_UNSIGNED; \
284	} while (0)
285
286/* zero the last 4 bytes for a range of opaque */
287/* data to make sure any pad bytes will be zero. */
288#define nfsm_chain_zero_opaque_pad(BUF, LEN) \
289	*(((uint32_t*)(BUF))+((nfsm_rndup(LEN)>>2)-1)) = 0
290
291/* add buffer of opaque data to an mbuf chain */
292#define nfsm_chain_add_opaque(E, NMC, BUF, LEN) \
293	do { \
294		uint32_t rndlen = nfsm_rndup(LEN); \
295		if (E) break; \
296		if ((NMC)->nmc_left < rndlen) { \
297			(E) = nfsm_chain_add_opaque_f((NMC), (const u_char*)(BUF), (LEN)); \
298			break; \
299		} \
300		nfsm_chain_zero_opaque_pad((NMC)->nmc_ptr, (LEN)); \
301		bcopy((BUF), (NMC)->nmc_ptr, (LEN)); \
302		(NMC)->nmc_ptr += rndlen; \
303		(NMC)->nmc_left -= rndlen; \
304	} while (0)
305
306/* add buffer of opaque data to an mbuf chain without padding */
307#define nfsm_chain_add_opaque_nopad(E, NMC, BUF, LEN) \
308	do { \
309		if (E) break; \
310		if ((NMC)->nmc_left < (uint32_t) (LEN)) { \
311			(E) = nfsm_chain_add_opaque_nopad_f((NMC), (const u_char*)(BUF), (LEN)); \
312			break; \
313		} \
314		bcopy((BUF), (NMC)->nmc_ptr, (LEN)); \
315		(NMC)->nmc_ptr += (LEN); \
316		(NMC)->nmc_left -= (LEN); \
317	} while (0)
318
319/* finish an mbuf in a chain to allow subsequent insertion */
320#define nfsm_chain_finish_mbuf(E, NMC) \
321	do { \
322		if (E) break; \
323		mbuf_setlen((NMC)->nmc_mcur, \
324			(NMC)->nmc_ptr - (caddr_t)mbuf_data((NMC)->nmc_mcur)); \
325		(NMC)->nmc_left = 0; \
326	} while (0)
327
328/* add a file handle to an mbuf chain */
329#define nfsm_chain_add_fh(E, NMC, VERS, FHP, FHLEN) \
330	do { \
331		if (E) break; \
332		if ((VERS) != NFS_VER2) \
333			nfsm_chain_add_32((E), (NMC), (FHLEN)); \
334		nfsm_chain_add_opaque((E), (NMC), (FHP), (FHLEN)); \
335	} while (0)
336
337/* add a string to an mbuf chain */
338#define nfsm_chain_add_string(E, NMC, STR, LEN) \
339	do { \
340		nfsm_chain_add_32((E), (NMC), (LEN)); \
341		nfsm_chain_add_opaque((E), (NMC), (STR), (LEN)); \
342	} while (0)
343
344/* add a name to an mbuf chain */
345#define nfsm_chain_add_name(E, NMC, STR, LEN, NMP) \
346	do { \
347		if (E) break; \
348		if (NMFLAG((NMP), NFC)) \
349			(E) = nfsm_chain_add_string_nfc((NMC), (const uint8_t*)(STR), (LEN)); \
350		else \
351			nfsm_chain_add_string((E), (NMC), (STR), (LEN)); \
352	} while (0)
353
354/* add an NFSv2 time to an mbuf chain */
355#define nfsm_chain_add_v2time(E, NMC, TVP) \
356	do { \
357		if (TVP) { \
358			nfsm_chain_add_32((E), (NMC), (TVP)->tv_sec); \
359			nfsm_chain_add_32((E), (NMC), ((TVP)->tv_nsec != -1) ? \
360				((uint32_t)(TVP)->tv_nsec / 1000) : 0xffffffff); \
361		} else { \
362			/* no time... use -1 */ \
363			nfsm_chain_add_32((E), (NMC), -1); \
364			nfsm_chain_add_32((E), (NMC), -1); \
365		} \
366	} while (0)
367
368/* add an NFSv3 time to an mbuf chain */
369#define nfsm_chain_add_v3time(E, NMC, TVP) \
370	do { \
371		nfsm_chain_add_32((E), (NMC), (TVP)->tv_sec); \
372		nfsm_chain_add_32((E), (NMC), (TVP)->tv_nsec); \
373	} while (0)
374
375/* add an NFS v2 or v3 time to an mbuf chain */
376#define nfsm_chain_add_time(E, NMC, VERS, TVP) \
377	do { \
378		if ((VERS) == NFS_VER2) { \
379			nfsm_chain_add_v2time((E), (NMC), (TVP)); \
380		} else { \
381			nfsm_chain_add_v3time((E), (NMC), (TVP)); \
382		} \
383	} while (0)
384
385/* add an NFSv3 postop file handle to an mbuf chain */
386#define nfsm_chain_add_postop_fh(E, NMC, FHP, FHLEN) \
387	do { \
388		nfsm_chain_add_32((E), (NMC), TRUE); \
389		nfsm_chain_add_fh((E), (NMC), NFS_VER3, (FHP), (FHLEN)); \
390	} while (0)
391
392/* add NFSv3 postop attributes to an mbuf chain */
393#define nfsm_chain_add_postop_attr(E, ND, NMC, ATTRERR, VAP) \
394	do { \
395		if (E) break; \
396		if (ATTRERR) { \
397			nfsm_chain_add_32((E), (NMC), FALSE); \
398			break; \
399		} \
400		nfsm_chain_add_32((E), (NMC), TRUE); \
401		if (E) break; \
402		(E) = nfsm_chain_add_fattr((ND), (NMC), (VAP)); \
403	} while (0)
404
405/* Add an NFSv2 "sattr" structure to an mbuf chain */
406#define nfsm_chain_add_v2sattr(E, NMC, VAP, SZRDEV) \
407	do { \
408		if (E) break; \
409		(E) = nfsm_chain_add_v2sattr_f((NMC), (VAP), (SZRDEV)); \
410	} while (0)
411
412/* Add an NFSv3 "sattr" structure to an mbuf chain */
413#define nfsm_chain_add_v3sattr(E, NMC, VAP) \
414	do { \
415		if (E) break; \
416		(E) = nfsm_chain_add_v3sattr_f((NMC), (VAP)); \
417	} while (0)
418
419/* Add an NFSv4 "fattr" structure to an mbuf chain */
420#define nfsm_chain_add_fattr4(E, NMC, VAP, NMP) \
421	do { \
422		if (E) break; \
423		(E) = nfsm_chain_add_fattr4_f((NMC), (VAP), (NMP)); \
424	} while (0)
425
426/* add NFSv3 WCC data to an mbuf chain */
427#define nfsm_chain_add_wcc_data(E, ND, NMC, PREERR, PREVAP, POSTERR, POSTVAP) \
428	do { \
429		if (E) break; \
430		(E) = nfsm_chain_add_wcc_data_f((ND), (NMC), \
431			(PREERR), (PREVAP), (POSTERR), (POSTVAP)); \
432	} while (0)
433
434/* add NFSv4 COMPOUND header */
435#define NFS4_TAG_LENGTH	12
436#define nfsm_chain_add_compound_header(E, NMC, TAG, NUMOPS) \
437	do { \
438		if ((TAG) && strlen(TAG)) { \
439			/* put tags into a fixed-length space-padded field */ \
440			char __nfstag[NFS4_TAG_LENGTH+1]; \
441			snprintf(__nfstag, sizeof(__nfstag), "%-*s", NFS4_TAG_LENGTH, (TAG)); \
442			nfsm_chain_add_32((E), (NMC), NFS4_TAG_LENGTH); \
443			nfsm_chain_add_opaque((E), (NMC), __nfstag, NFS4_TAG_LENGTH); \
444		} else { \
445			nfsm_chain_add_32((E), (NMC), 0); \
446		} \
447		nfsm_chain_add_32((E), (NMC), 0); /*minorversion*/ \
448		nfsm_chain_add_32((E), (NMC), (NUMOPS)); \
449	} while (0)
450
451/* add NFSv4 attr bitmap */
452#define nfsm_chain_add_bitmap(E, NMC, B, LEN) \
453	do { \
454		int __i; \
455		nfsm_chain_add_32((E), (NMC), (LEN)); \
456		for (__i=0; __i < (LEN); __i++) \
457			nfsm_chain_add_32((E), (NMC), (B)[__i]); \
458	} while (0)
459
460/* add NFSv4 attr bitmap masked with the given mask */
461#define nfsm_chain_add_bitmap_masked(E, NMC, B, LEN, MASK) \
462	do { \
463		int __i; \
464		nfsm_chain_add_32((E), (NMC), (LEN)); \
465		for (__i=0; __i < (LEN); __i++) \
466			nfsm_chain_add_32((E), (NMC), ((B)[__i] & (MASK)[__i])); \
467	} while (0)
468
469/* add NFSv4 attr bitmap masked with the supported attributes for this mount/node */
470#define nfsm_chain_add_bitmap_supported(E, NMC, B, NMP, NP) \
471	do { \
472		uint32_t __bitmap[NFS_ATTR_BITMAP_LEN], *__bmp = (B); \
473		int __nonamedattr = 0, __noacl = 0, __nomode = 0; \
474		if (!((NMP)->nm_fsattr.nfsa_flags & NFS_FSFLAG_NAMED_ATTR) || \
475		    ((NP) && (((nfsnode_t)(NP))->n_flag & (NISDOTZFS|NISDOTZFSCHILD)))) \
476			__nonamedattr = 1; \
477		if (!((NMP)->nm_fsattr.nfsa_flags & NFS_FSFLAG_ACL)) \
478			__noacl = 1; \
479		if (NMFLAG((NMP), ACLONLY)) \
480			__nomode = 1; \
481		if (__nonamedattr || __noacl || __nomode) { \
482			/* don't ask for attrs we're not supporting */ \
483			/* some ".zfs" directories can't handle being asked for some attributes */ \
484			int __ii; \
485			NFS_CLEAR_ATTRIBUTES(__bitmap); \
486			for (__ii=0; __ii < NFS_ATTR_BITMAP_LEN; __ii++) \
487				__bitmap[__ii] = (B)[__ii]; \
488			if (__nonamedattr) \
489				NFS_BITMAP_CLR(__bitmap, NFS_FATTR_NAMED_ATTR); \
490			if (__noacl) \
491				NFS_BITMAP_CLR(__bitmap, NFS_FATTR_ACL); \
492			if (__nomode) \
493				NFS_BITMAP_CLR(__bitmap, NFS_FATTR_MODE); \
494			__bmp = __bitmap; \
495		} \
496		nfsm_chain_add_bitmap_masked((E), (NMC), __bmp, NFS_ATTR_BITMAP_LEN, (NMP)->nm_fsattr.nfsa_supp_attr); \
497	} while (0)
498
499/* Add an NFSv4 "stateid" structure to an mbuf chain */
500#define nfsm_chain_add_stateid(E, NMC, SID) \
501	do { \
502		nfsm_chain_add_32((E), (NMC), (SID)->seqid); \
503		nfsm_chain_add_32((E), (NMC), (SID)->other[0]); \
504		nfsm_chain_add_32((E), (NMC), (SID)->other[1]); \
505		nfsm_chain_add_32((E), (NMC), (SID)->other[2]); \
506	} while (0)
507
508/* add an NFSv4 lock owner structure to an mbuf chain */
509#define nfsm_chain_add_lock_owner4(E, NMC, NMP, NLOP) \
510	do { \
511		nfsm_chain_add_64((E), (NMC), (NMP)->nm_clientid); \
512		nfsm_chain_add_32((E), (NMC), 5*NFSX_UNSIGNED); \
513		nfsm_chain_add_32((E), (NMC), (NLOP)->nlo_name); \
514		nfsm_chain_add_32((E), (NMC), (NLOP)->nlo_pid); \
515		nfsm_chain_add_64((E), (NMC), (NLOP)->nlo_pid_start.tv_sec); \
516		nfsm_chain_add_32((E), (NMC), (NLOP)->nlo_pid_start.tv_usec); \
517	} while (0)
518
519/*
520 * macros for dissecting NFS mbuf chains
521 */
522
523/* prepare an mbuf chain for dissection starting with the given mbuf */
524#define nfsm_chain_dissect_init(E, NMC, H) \
525	do { \
526		if (!(H)) { \
527			(E) = EINVAL; \
528			break; \
529		} \
530		(NMC)->nmc_mcur = (NMC)->nmc_mhead = (H); \
531		(NMC)->nmc_ptr = mbuf_data(H); \
532		(NMC)->nmc_left = mbuf_len(H); \
533	} while (0)
534
535/* skip a number of bytes in an mbuf chain */
536#define nfsm_chain_adv(E, NMC, LEN) \
537	do { \
538		if (E) break; \
539		if ((NMC)->nmc_left >= (uint32_t)(LEN)) { \
540			(NMC)->nmc_left -= (LEN); \
541			(NMC)->nmc_ptr += (LEN); \
542		} else { \
543			(E) = nfsm_chain_advance((NMC), (LEN)); \
544		} \
545	} while (0)
546
547/* get a 32bit value from an mbuf chain */
548#define nfsm_chain_get_32(E, NMC, LVAL) \
549	do { \
550		uint32_t __tmp32, *__tmpptr; \
551		if (E) break; \
552		if ((NMC)->nmc_left >= NFSX_UNSIGNED) { \
553			__tmpptr = (uint32_t*)(NMC)->nmc_ptr; \
554			(NMC)->nmc_left -= NFSX_UNSIGNED; \
555			(NMC)->nmc_ptr += NFSX_UNSIGNED; \
556		} else { \
557			__tmpptr = &__tmp32; \
558			(E) = nfsm_chain_get_opaque_f((NMC), NFSX_UNSIGNED, (u_char*)__tmpptr); \
559			if (E) break; \
560		} \
561		(LVAL) = fxdr_unsigned(uint32_t, *__tmpptr); \
562	} while (0)
563
564/* get a 64bit value from an mbuf chain */
565#define nfsm_chain_get_64(E, NMC, LVAL) \
566	do { \
567		uint64_t __tmp64, *__tmpptr; \
568		if (E) break; \
569		if ((NMC)->nmc_left >= 2 * NFSX_UNSIGNED) { \
570			__tmpptr = (uint64_t*)(NMC)->nmc_ptr; \
571			(NMC)->nmc_left -= 2 * NFSX_UNSIGNED; \
572			(NMC)->nmc_ptr += 2 * NFSX_UNSIGNED; \
573		} else { \
574			__tmpptr = &__tmp64; \
575			(E) = nfsm_chain_get_opaque_f((NMC), 2 * NFSX_UNSIGNED, (u_char*)__tmpptr); \
576			if (E) break; \
577		} \
578		fxdr_hyper(__tmpptr, &(LVAL)); \
579	} while (0)
580
581/* get a pointer to the next consecutive bytes in an mbuf chain */
582#define nfsm_chain_get_opaque_pointer(E, NMC, LEN, PTR) \
583	do { \
584		uint32_t rndlen; \
585		if (E) break; \
586		rndlen = nfsm_rndup(LEN); \
587		if ((NMC)->nmc_left >= rndlen) { \
588			(PTR) = (void*)(NMC)->nmc_ptr; \
589			(NMC)->nmc_left -= rndlen; \
590			(NMC)->nmc_ptr += rndlen; \
591		} else { \
592			(E) = nfsm_chain_get_opaque_pointer_f((NMC), (LEN), (u_char**)&(PTR)); \
593		} \
594	} while (0)
595
596/* copy the next consecutive bytes of opaque data from an mbuf chain */
597#define nfsm_chain_get_opaque(E, NMC, LEN, PTR) \
598	do { \
599		uint32_t rndlen; \
600		if (E) break; \
601		rndlen = nfsm_rndup(LEN); \
602		if ((NMC)->nmc_left >= rndlen) { \
603			u_char *__tmpptr = (u_char*)(NMC)->nmc_ptr; \
604			(NMC)->nmc_left -= rndlen; \
605			(NMC)->nmc_ptr += rndlen; \
606			bcopy(__tmpptr, (PTR), (LEN)); \
607		} else { \
608			(E) = nfsm_chain_get_opaque_f((NMC), (LEN), (u_char*)(PTR)); \
609		} \
610	} while (0)
611
612/* get the size of and a pointer to a file handle in an mbuf chain */
613#define nfsm_chain_get_fh_ptr(E, NMC, VERS, FHP, FHSIZE) \
614	do { \
615		if ((VERS) != NFS_VER2) \
616			nfsm_chain_get_32((E), (NMC), (FHSIZE)); \
617		else \
618			(FHSIZE) = NFSX_V2FH;\
619		nfsm_chain_get_opaque_pointer((E), (NMC), (FHSIZE), (FHP));\
620	} while (0)
621
622/* get the size of and data for a file handle in an mbuf chain */
623#define nfsm_chain_get_fh(E, NMC, VERS, FHP) \
624	do { \
625		if ((VERS) != NFS_VER2) \
626			nfsm_chain_get_32((E), (NMC), (FHP)->fh_len); \
627		else \
628			(FHP)->fh_len = NFSX_V2FH;\
629		nfsm_chain_get_opaque((E), (NMC), (uint32_t)(FHP)->fh_len, (FHP)->fh_data);\
630		if (E) \
631			(FHP)->fh_len = 0;\
632	} while (0)
633
634/* get an NFS v2 or v3 time from an mbuf chain */
635#define nfsm_chain_get_time(E, NMC, VERS, TSEC, TNSEC) \
636	do { \
637		nfsm_chain_get_32((E), (NMC), (TSEC)); \
638		nfsm_chain_get_32((E), (NMC), (TNSEC)); \
639		if ((E) || ((VERS) != NFS_VER2)) break; \
640		if ((uint32_t)(TNSEC) == 0xffffffff) \
641			(TNSEC) = 0; \
642		else \
643			(TNSEC) *= 1000; \
644	} while (0)
645
646/* get postop attributes from an mbuf chain */
647#define nfsm_chain_postop_attr_get(E, NMC, F, VAP) \
648	do { \
649		(F) = 0; \
650		if ((E) || !(NMC)->nmc_mhead) break; \
651		nfsm_chain_get_32((E), (NMC), (F)); \
652		if ((E) || !(F)) break; \
653		if (((E) = nfs_parsefattr((NMC), NFS_VER3, (VAP)))) \
654			(F) = 0; \
655	} while (0)
656
657/* update a node's attribute cache with postop attributes from an mbuf chain */
658/* (F returns whether the attributes were updated or not) */
659#define nfsm_chain_postop_attr_update_flag(E, NMC, NP, F, X) \
660	do { \
661		struct nfs_vattr ttvattr; \
662		nfsm_chain_postop_attr_get((E), (NMC), (F), &ttvattr); \
663		if ((E) || !(F)) break; \
664		if (((E) = nfs_loadattrcache((NP), &ttvattr, (X), 1))) { \
665			(F) = 0; \
666			break; \
667		} \
668		if (*(X) == 0) \
669			(F) = 0; \
670	} while (0)
671
672/* update a node's attribute cache with postop attributes from an mbuf chain */
673#define nfsm_chain_postop_attr_update(E, NMC, NP, X) \
674	do { \
675		int __dummy_flag = 0; \
676		nfsm_chain_postop_attr_update_flag((E), (NMC), (NP), __dummy_flag, (X)); \
677	} while (0)
678
679/* get and process NFSv3 WCC data from an mbuf chain */
680#define nfsm_chain_get_wcc_data(E, NMC, NP, PREMTIME, NEWPOSTATTR, X) \
681	do { \
682		if (E) break; \
683		(E) = nfsm_chain_get_wcc_data_f((NMC), (NP), (PREMTIME), (NEWPOSTATTR), (X)); \
684	} while (0)
685
686/* update a node's attribute cache with attributes from an mbuf chain */
687#define nfsm_chain_loadattr(E, NMC, NP, VERS, X) \
688	do { \
689		struct nfs_vattr ttvattr; \
690		if (E) break; \
691		if ((VERS) == NFS_VER4) { \
692			(E) = nfs4_parsefattr((NMC), NULL, &ttvattr, NULL, NULL, NULL); \
693		} else { \
694			(E) = nfs_parsefattr((NMC), (VERS), &ttvattr); \
695		} \
696		if (!(E) && (NP)) \
697			(E) = nfs_loadattrcache((NP), &ttvattr, (X), 0); \
698		NVATTR_CLEANUP(&ttvattr); \
699	} while (0)
700
701/* get NFSv4 attr bitmap */
702#define nfsm_chain_get_bitmap(E, NMC, B, LEN) \
703	do { \
704		uint32_t __len = 0, __i; \
705		nfsm_chain_get_32((E), (NMC), __len); \
706		if (E) break; \
707		for (__i=0; __i < MIN(__len, (LEN)); __i++) \
708			nfsm_chain_get_32((E), (NMC), (B)[__i]); \
709		if (E) break; \
710		for (; __i < __len; __i++) \
711			nfsm_chain_adv((E), (NMC), NFSX_UNSIGNED); \
712		for (; __i < (LEN); __i++) \
713			(B)[__i] = 0; \
714		(LEN) = __len; \
715	} while (0)
716
717/* get an NFSv4 "stateid" structure from an mbuf chain */
718#define nfsm_chain_get_stateid(E, NMC, SID) \
719	do { \
720		nfsm_chain_get_32((E), (NMC), (SID)->seqid); \
721		nfsm_chain_get_32((E), (NMC), (SID)->other[0]); \
722		nfsm_chain_get_32((E), (NMC), (SID)->other[1]); \
723		nfsm_chain_get_32((E), (NMC), (SID)->other[2]); \
724	} while (0)
725
726#define nfsm_chain_skip_tag(E, NMC) \
727	do { \
728		uint32_t __val = 0; \
729		nfsm_chain_get_32((E), (NMC), __val); \
730		nfsm_chain_adv((E), (NMC), nfsm_rndup(__val)); \
731	} while (0)
732
733#define nfsm_chain_op_check(E, NMC, OP) \
734	do { \
735		uint32_t __val = 0; \
736		nfsm_chain_get_32((E), (NMC), __val); \
737		/* [sigh] some implementations return the "illegal" op for unsupported ops */ \
738		nfsm_assert((E), ((__val == (OP)) || (__val == NFS_OP_ILLEGAL)), EBADRPC); \
739		nfsm_chain_get_32((E), (NMC), __val); \
740		nfsm_assert((E), (__val == NFS_OK), __val); \
741	} while (0)
742
743#define nfsm_chain_check_change_info(E, NMC, DNP) \
744	do { \
745		uint64_t __ci_before, __ci_after; \
746		uint32_t __ci_atomic = 0; \
747		nfsm_chain_get_32((E), (NMC), __ci_atomic); \
748		nfsm_chain_get_64((E), (NMC), __ci_before); \
749		nfsm_chain_get_64((E), (NMC), __ci_after); \
750		if ((E) || !(DNP)) break; \
751		if (__ci_atomic && (__ci_before == (DNP)->n_ncchange)) { \
752			(DNP)->n_ncchange = __ci_after; \
753		} else { \
754			cache_purge(NFSTOV(DNP)); \
755			(DNP)->n_ncgen++; \
756		} \
757	} while (0)
758
759#endif /* __APPLE_API_PRIVATE */
760#endif /* _NFS_NFSM_SUBS_H_ */
761