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