]> git.saurik.com Git - apple/xnu.git/blob - bsd/nfs/nfsm_subs.h
xnu-1228.0.2.tar.gz
[apple/xnu.git] / bsd / nfs / nfsm_subs.h
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
76 int nfsm_rpchead(struct nfsreq *, int, mbuf_t, u_int64_t *, mbuf_t *);
77 int nfsm_rpchead2(int, int, int, int, int, int, kauth_cred_t, struct nfsreq *, mbuf_t, u_int64_t *, mbuf_t *);
78
79 int nfsm_chain_new_mbuf(struct nfsm_chain *, size_t);
80 int nfsm_chain_add_opaque_f(struct nfsm_chain *, const u_char *, uint32_t);
81 int nfsm_chain_add_opaque_nopad_f(struct nfsm_chain *, const u_char *, uint32_t);
82 int nfsm_chain_add_uio(struct nfsm_chain *, struct uio *, uint32_t);
83 int nfsm_chain_add_fattr4_f(struct nfsm_chain *, struct vnode_attr *, struct nfsmount *);
84 int nfsm_chain_add_v2sattr_f(struct nfsm_chain *, struct vnode_attr *, uint32_t);
85 int nfsm_chain_add_v3sattr_f(struct nfsm_chain *, struct vnode_attr *);
86
87 int nfsm_chain_advance(struct nfsm_chain *, uint32_t);
88 int nfsm_chain_offset(struct nfsm_chain *);
89 int nfsm_chain_reverse(struct nfsm_chain *, uint32_t);
90 int nfsm_chain_get_opaque_pointer_f(struct nfsm_chain *, uint32_t, u_char **);
91 int nfsm_chain_get_opaque_f(struct nfsm_chain *, uint32_t, u_char *);
92 int nfsm_chain_get_uio(struct nfsm_chain *, uint32_t, struct uio *);
93 int nfsm_chain_get_fh_attr(struct nfsm_chain *, nfsnode_t,
94 vfs_context_t, int, uint64_t *, fhandle_t *, struct nfs_vattr *);
95 int nfsm_chain_get_wcc_data_f(struct nfsm_chain *, nfsnode_t, struct timespec *, int *, u_int64_t *);
96
97 #if NFSSERVER
98 void nfsm_adj(mbuf_t, int, int);
99 int nfsm_mbuf_get_list(size_t, mbuf_t *, int *);
100
101 int nfsm_chain_add_fattr(struct nfsrv_descript *, struct nfsm_chain *, struct vnode_attr *);
102 int nfsm_chain_add_wcc_data_f(struct nfsrv_descript *, struct nfsm_chain *, int,
103 struct vnode_attr *, int, struct vnode_attr *);
104 int nfsm_chain_get_path_namei(struct nfsm_chain *, uint32_t, struct nameidata *);
105 int nfsm_chain_get_sattr(struct nfsrv_descript *, struct nfsm_chain *, struct vnode_attr *);
106 int 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 if (E) break; \
517 if ((NMC)->nmc_left >= (uint32_t)(LEN)) { \
518 (PTR) = (void*)(NMC)->nmc_ptr; \
519 (NMC)->nmc_left -= nfsm_rndup(LEN); \
520 (NMC)->nmc_ptr += nfsm_rndup(LEN); \
521 } else { \
522 (E) = nfsm_chain_get_opaque_pointer_f((NMC), (LEN), (u_char**)&(PTR)); \
523 } \
524 } while (0)
525
526 /* copy the next consecutive bytes of opaque data from an mbuf chain */
527 #define nfsm_chain_get_opaque(E, NMC, LEN, PTR) \
528 do { \
529 if (E) break; \
530 if ((NMC)->nmc_left >= (LEN)) { \
531 u_char *__tmpptr = (u_char*)(NMC)->nmc_ptr; \
532 (NMC)->nmc_left -= nfsm_rndup(LEN); \
533 (NMC)->nmc_ptr += nfsm_rndup(LEN); \
534 bcopy(__tmpptr, (PTR), (LEN)); \
535 } else { \
536 (E) = nfsm_chain_get_opaque_f((NMC), (LEN), (u_char*)(PTR)); \
537 } \
538 } while (0)
539
540 /* get the size of and a pointer to a file handle in an mbuf chain */
541 #define nfsm_chain_get_fh_ptr(E, NMC, VERS, FHP, FHSIZE) \
542 do { \
543 if ((VERS) != NFS_VER2) \
544 nfsm_chain_get_32((E), (NMC), (FHSIZE)); \
545 else \
546 (FHSIZE) = NFSX_V2FH;\
547 nfsm_chain_get_opaque_pointer((E), (NMC), (FHSIZE), (FHP));\
548 } while (0)
549
550 /* get the size of and data for a file handle in an mbuf chain */
551 #define nfsm_chain_get_fh(E, NMC, VERS, FHP) \
552 do { \
553 if ((VERS) != NFS_VER2) \
554 nfsm_chain_get_32((E), (NMC), (FHP)->fh_len); \
555 else \
556 (FHP)->fh_len = NFSX_V2FH;\
557 nfsm_chain_get_opaque((E), (NMC), (uint32_t)(FHP)->fh_len, (FHP)->fh_data);\
558 if (E) \
559 (FHP)->fh_len = 0;\
560 } while (0)
561
562 /* get an NFS v2 or v3 time from an mbuf chain */
563 #define nfsm_chain_get_time(E, NMC, VERS, TSEC, TNSEC) \
564 do { \
565 nfsm_chain_get_32((E), (NMC), (TSEC)); \
566 nfsm_chain_get_32((E), (NMC), (TNSEC)); \
567 if ((E) || ((VERS) != NFS_VER2)) break; \
568 if ((uint32_t)(TNSEC) == 0xffffffff) \
569 (TNSEC) = 0; \
570 else \
571 (TNSEC) *= 1000; \
572 } while (0)
573
574 /* get postop attributes from an mbuf chain */
575 #define nfsm_chain_postop_attr_get(E, NMC, F, VAP) \
576 do { \
577 (F) = 0; \
578 if ((E) || !(NMC)->nmc_mhead) break; \
579 nfsm_chain_get_32((E), (NMC), (F)); \
580 if ((E) || !(F)) break; \
581 if (((E) = nfs_parsefattr((NMC), NFS_VER3, (VAP)))) \
582 (F) = 0; \
583 } while (0)
584
585 /* update a node's attribute cache with postop attributes from an mbuf chain */
586 /* (F returns whether the attributes were updated or not) */
587 #define nfsm_chain_postop_attr_update_flag(E, NMC, NP, F, X) \
588 do { \
589 struct nfs_vattr ttvattr; \
590 nfsm_chain_postop_attr_get((E), (NMC), (F), &ttvattr); \
591 if ((E) || !(F)) break; \
592 if (((E) = nfs_loadattrcache((NP), &ttvattr, (X), 1))) { \
593 (F) = 0; \
594 break; \
595 } \
596 if (*(X) == 0) \
597 (F) = 0; \
598 } while (0)
599
600 /* update a node's attribute cache with postop attributes from an mbuf chain */
601 #define nfsm_chain_postop_attr_update(E, NMC, NP, X) \
602 do { \
603 int __dummy_flag = 0; \
604 nfsm_chain_postop_attr_update_flag((E), (NMC), (NP), __dummy_flag, (X)); \
605 } while (0)
606
607 /* get and process NFSv3 WCC data from an mbuf chain */
608 #define nfsm_chain_get_wcc_data(E, NMC, NP, PREMTIME, NEWPOSTATTR, X) \
609 do { \
610 if (E) break; \
611 (E) = nfsm_chain_get_wcc_data_f((NMC), (NP), (PREMTIME), (NEWPOSTATTR), (X)); \
612 } while (0)
613
614 /* update a node's attribute cache with attributes from an mbuf chain */
615 #define nfsm_chain_loadattr(E, NMC, NP, VERS, A, X) \
616 do { \
617 struct nfs_vattr ttvattr, *ttnvap; \
618 if (E) break; \
619 ttnvap = (A) ? (A) : &ttvattr; \
620 if ((VERS) == NFS_VER4) { \
621 NFS_CLEAR_ATTRIBUTES(ttnvap->nva_bitmap); \
622 (E) = nfs4_parsefattr((NMC), NULL, ttnvap, NULL, NULL); \
623 } else { \
624 (E) = nfs_parsefattr((NMC), (VERS), ttnvap); \
625 } \
626 if (E) break; \
627 (E) = nfs_loadattrcache((NP), ttnvap, (X), 0); \
628 } while (0)
629
630 /* get NFSv4 attr bitmap */
631 #define nfsm_chain_get_bitmap(E, NMC, B, LEN) \
632 do { \
633 uint32_t __len, __i; \
634 nfsm_chain_get_32((E), (NMC), __len); \
635 if (E) break; \
636 for (__i=0; __i < MIN(__len, (LEN)); __i++) \
637 nfsm_chain_get_32((E), (NMC), (B)[__i]); \
638 if (E) break; \
639 for (; __i < (LEN); __i++) \
640 (B)[__i] = 0; \
641 (LEN) = __len; \
642 } while (0)
643
644 #define nfsm_chain_skip_tag(E, NMC) \
645 do { \
646 uint32_t __val; \
647 nfsm_chain_get_32((E), (NMC), __val); \
648 nfsm_chain_adv((E), (NMC), nfsm_rndup(__val)); \
649 } while (0)
650
651 #define nfsm_chain_op_check(E, NMC, OP) \
652 do { \
653 uint32_t __val = 0; \
654 nfsm_chain_get_32((E), (NMC), __val); \
655 nfsm_assert((E), (__val == (OP)), EBADRPC); \
656 nfsm_chain_get_32((E), (NMC), __val); \
657 nfsm_assert((E), (__val == NFS_OK), __val); \
658 } while (0)
659
660 #define nfsm_chain_check_change_info(E, NMC, DNP) \
661 do { \
662 uint64_t __ci_before, __ci_after; \
663 uint32_t __ci_atomic = 0; \
664 nfsm_chain_get_32((E), (NMC), __ci_atomic); \
665 nfsm_chain_get_64((E), (NMC), __ci_before); \
666 nfsm_chain_get_64((E), (NMC), __ci_after); \
667 if (E) break; \
668 if (__ci_atomic && (__ci_before == (DNP)->n_ncchange)) \
669 (DNP)->n_ncchange = __ci_after; \
670 else \
671 cache_purge(NFSTOV(DNP)); \
672 } while (0)
673
674 #endif /* __APPLE_API_PRIVATE */
675 #endif /* _NFS_NFSM_SUBS_H_ */