]> git.saurik.com Git - apple/xnu.git/blame - bsd/nfs/nfsm_subs.h
xnu-1504.15.3.tar.gz
[apple/xnu.git] / bsd / nfs / nfsm_subs.h
CommitLineData
1c79356b 1/*
b0d623f7 2 * Copyright (c) 2000-2009 Apple Inc. All rights reserved.
5d5c5d0d 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
1c79356b 5 *
2d21ac55
A
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.
8f6c56a5 14 *
2d21ac55
A
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
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
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.
8f6c56a5 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b
A
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
9bccf70c 72#include <sys/appleapiopts.h>
1c79356b 73
9bccf70c 74#ifdef __APPLE_API_PRIVATE
2d21ac55
A
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);
b0d623f7 82int nfsm_chain_add_uio(struct nfsm_chain *, uio_t, uint32_t);
2d21ac55
A
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 *);
b0d623f7 92int nfsm_chain_get_uio(struct nfsm_chain *, uint32_t, uio_t);
2d21ac55
A
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
1c79356b 161/*
2d21ac55
A
162 * control flow macros:
163 * go to the appropriate label on condition
1c79356b 164 */
2d21ac55
A
165#define nfsmout_if(E) do { if (E) goto nfsmout; } while (0)
166#define nfsmerr_if(E) do { if (E) goto nfsmerr; } while (0)
1c79356b
A
167
168/*
2d21ac55
A
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.
1c79356b 172 */
2d21ac55
A
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
1c79356b
A
207
208/*
2d21ac55 209 * macros for building NFS mbuf chains
1c79356b
A
210 */
211
2d21ac55
A
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; \
91447636 259 } \
2d21ac55
A
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; \
91447636 297 } \
2d21ac55
A
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; \
1c79356b 311 } \
2d21ac55
A
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); \
91447636 349 } else { \
2d21ac55
A
350 /* no time... use -1 */ \
351 nfsm_chain_add_32((E), (NMC), -1); \
352 nfsm_chain_add_32((E), (NMC), -1); \
1c79356b 353 } \
2d21ac55
A
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)); \
91447636 370 } \
2d21ac55
A
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; \
91447636 387 } \
2d21ac55
A
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 */
b0d623f7 423#define NFS4_TAG_LENGTH 12
2d21ac55
A
424#define nfsm_chain_add_compound_header(E, NMC, TAG, NUMOPS) \
425 do { \
b0d623f7
A
426 if ((TAG) && strlen(TAG)) { \
427 /* put tags into a fixed-length space-padded field */ \
428 char __nfstag[NFS4_TAG_LENGTH+1]; \
429 snprintf(__nfstag, sizeof(__nfstag), "%-*s", NFS4_TAG_LENGTH, (TAG)); \
430 nfsm_chain_add_32((E), (NMC), NFS4_TAG_LENGTH); \
431 nfsm_chain_add_opaque((E), (NMC), __nfstag, NFS4_TAG_LENGTH); \
432 } else { \
2d21ac55 433 nfsm_chain_add_32((E), (NMC), 0); \
b0d623f7 434 } \
2d21ac55
A
435 nfsm_chain_add_32((E), (NMC), 0); /*minorversion*/ \
436 nfsm_chain_add_32((E), (NMC), (NUMOPS)); \
437 } while (0)
438
439/* add NFSv4 attr bitmap */
440#define nfsm_chain_add_bitmap(E, NMC, B, LEN) \
441 do { \
442 int __i; \
443 nfsm_chain_add_32((E), (NMC), (LEN)); \
444 for (__i=0; __i < (LEN); __i++) \
445 nfsm_chain_add_32((E), (NMC), (B)[__i]); \
446 } while (0)
447
448/* add NFSv4 attr bitmap masked with the given mask */
449#define nfsm_chain_add_bitmap_masked(E, NMC, B, LEN, MASK) \
450 do { \
451 int __i; \
452 nfsm_chain_add_32((E), (NMC), (LEN)); \
453 for (__i=0; __i < (LEN); __i++) \
454 nfsm_chain_add_32((E), (NMC), ((B)[__i] & (MASK)[__i])); \
455 } while (0)
1c79356b 456
b0d623f7
A
457/* Add an NFSv4 "stateid" structure to an mbuf chain */
458#define nfsm_chain_add_stateid(E, NMC, SID) \
459 do { \
460 nfsm_chain_add_32((E), (NMC), (SID)->seqid); \
461 nfsm_chain_add_32((E), (NMC), (SID)->other[0]); \
462 nfsm_chain_add_32((E), (NMC), (SID)->other[1]); \
463 nfsm_chain_add_32((E), (NMC), (SID)->other[2]); \
464 } while (0)
465
466/* add an NFSv4 lock owner structure to an mbuf chain */
467#define nfsm_chain_add_lock_owner4(E, NMC, NMP, NLOP) \
468 do { \
469 nfsm_chain_add_64((E), (NMC), (NMP)->nm_clientid); \
470 nfsm_chain_add_32((E), (NMC), 5*NFSX_UNSIGNED); \
471 nfsm_chain_add_32((E), (NMC), (NLOP)->nlo_name); \
472 nfsm_chain_add_32((E), (NMC), (NLOP)->nlo_pid); \
473 nfsm_chain_add_64((E), (NMC), (NLOP)->nlo_pid_start.tv_sec); \
474 nfsm_chain_add_32((E), (NMC), (NLOP)->nlo_pid_start.tv_usec); \
475 } while (0)
476
2d21ac55
A
477/*
478 * macros for dissecting NFS mbuf chains
479 */
480
481/* prepare an mbuf chain for dissection starting with the given mbuf */
482#define nfsm_chain_dissect_init(E, NMC, H) \
483 do { \
484 if (!(H)) { \
485 (E) = EINVAL; \
486 break; \
91447636 487 } \
2d21ac55
A
488 (NMC)->nmc_mcur = (NMC)->nmc_mhead = (H); \
489 (NMC)->nmc_ptr = mbuf_data(H); \
490 (NMC)->nmc_left = mbuf_len(H); \
491 } while (0)
492
493/* skip a number of bytes in an mbuf chain */
494#define nfsm_chain_adv(E, NMC, LEN) \
495 do { \
496 if (E) break; \
497 if ((NMC)->nmc_left >= (uint32_t)(LEN)) { \
498 (NMC)->nmc_left -= (LEN); \
499 (NMC)->nmc_ptr += (LEN); \
91447636 500 } else { \
2d21ac55 501 (E) = nfsm_chain_advance((NMC), (LEN)); \
91447636 502 } \
2d21ac55
A
503 } while (0)
504
505/* get a 32bit value from an mbuf chain */
506#define nfsm_chain_get_32(E, NMC, LVAL) \
507 do { \
508 uint32_t __tmp32, *__tmpptr; \
509 if (E) break; \
510 if ((NMC)->nmc_left >= NFSX_UNSIGNED) { \
511 __tmpptr = (uint32_t*)(NMC)->nmc_ptr; \
512 (NMC)->nmc_left -= NFSX_UNSIGNED; \
513 (NMC)->nmc_ptr += NFSX_UNSIGNED; \
91447636 514 } else { \
2d21ac55
A
515 __tmpptr = &__tmp32; \
516 (E) = nfsm_chain_get_opaque_f((NMC), NFSX_UNSIGNED, (u_char*)__tmpptr); \
517 if (E) break; \
91447636 518 } \
2d21ac55
A
519 (LVAL) = fxdr_unsigned(uint32_t, *__tmpptr); \
520 } while (0)
521
522/* get a 64bit value from an mbuf chain */
523#define nfsm_chain_get_64(E, NMC, LVAL) \
524 do { \
525 uint64_t __tmp64, *__tmpptr; \
526 if (E) break; \
527 if ((NMC)->nmc_left >= 2 * NFSX_UNSIGNED) { \
528 __tmpptr = (uint64_t*)(NMC)->nmc_ptr; \
529 (NMC)->nmc_left -= 2 * NFSX_UNSIGNED; \
530 (NMC)->nmc_ptr += 2 * NFSX_UNSIGNED; \
91447636 531 } else { \
2d21ac55
A
532 __tmpptr = &__tmp64; \
533 (E) = nfsm_chain_get_opaque_f((NMC), 2 * NFSX_UNSIGNED, (u_char*)__tmpptr); \
534 if (E) break; \
91447636 535 } \
2d21ac55
A
536 fxdr_hyper(__tmpptr, &(LVAL)); \
537 } while (0)
538
539/* get a pointer to the next consecutive bytes in an mbuf chain */
540#define nfsm_chain_get_opaque_pointer(E, NMC, LEN, PTR) \
541 do { \
4a3eedf9 542 uint32_t rndlen; \
2d21ac55 543 if (E) break; \
4a3eedf9
A
544 rndlen = nfsm_rndup(LEN); \
545 if ((NMC)->nmc_left >= rndlen) { \
2d21ac55 546 (PTR) = (void*)(NMC)->nmc_ptr; \
4a3eedf9
A
547 (NMC)->nmc_left -= rndlen; \
548 (NMC)->nmc_ptr += rndlen; \
91447636 549 } else { \
2d21ac55 550 (E) = nfsm_chain_get_opaque_pointer_f((NMC), (LEN), (u_char**)&(PTR)); \
91447636 551 } \
2d21ac55
A
552 } while (0)
553
554/* copy the next consecutive bytes of opaque data from an mbuf chain */
555#define nfsm_chain_get_opaque(E, NMC, LEN, PTR) \
556 do { \
4a3eedf9 557 uint32_t rndlen; \
2d21ac55 558 if (E) break; \
4a3eedf9
A
559 rndlen = nfsm_rndup(LEN); \
560 if ((NMC)->nmc_left >= rndlen) { \
2d21ac55 561 u_char *__tmpptr = (u_char*)(NMC)->nmc_ptr; \
4a3eedf9
A
562 (NMC)->nmc_left -= rndlen; \
563 (NMC)->nmc_ptr += rndlen; \
2d21ac55 564 bcopy(__tmpptr, (PTR), (LEN)); \
91447636 565 } else { \
2d21ac55 566 (E) = nfsm_chain_get_opaque_f((NMC), (LEN), (u_char*)(PTR)); \
91447636 567 } \
2d21ac55 568 } while (0)
1c79356b 569
2d21ac55
A
570/* get the size of and a pointer to a file handle in an mbuf chain */
571#define nfsm_chain_get_fh_ptr(E, NMC, VERS, FHP, FHSIZE) \
572 do { \
573 if ((VERS) != NFS_VER2) \
574 nfsm_chain_get_32((E), (NMC), (FHSIZE)); \
1c79356b 575 else \
2d21ac55
A
576 (FHSIZE) = NFSX_V2FH;\
577 nfsm_chain_get_opaque_pointer((E), (NMC), (FHSIZE), (FHP));\
578 } while (0)
579
580/* get the size of and data for a file handle in an mbuf chain */
581#define nfsm_chain_get_fh(E, NMC, VERS, FHP) \
582 do { \
583 if ((VERS) != NFS_VER2) \
584 nfsm_chain_get_32((E), (NMC), (FHP)->fh_len); \
1c79356b 585 else \
2d21ac55
A
586 (FHP)->fh_len = NFSX_V2FH;\
587 nfsm_chain_get_opaque((E), (NMC), (uint32_t)(FHP)->fh_len, (FHP)->fh_data);\
588 if (E) \
589 (FHP)->fh_len = 0;\
590 } while (0)
591
592/* get an NFS v2 or v3 time from an mbuf chain */
593#define nfsm_chain_get_time(E, NMC, VERS, TSEC, TNSEC) \
594 do { \
595 nfsm_chain_get_32((E), (NMC), (TSEC)); \
596 nfsm_chain_get_32((E), (NMC), (TNSEC)); \
597 if ((E) || ((VERS) != NFS_VER2)) break; \
598 if ((uint32_t)(TNSEC) == 0xffffffff) \
599 (TNSEC) = 0; \
600 else \
601 (TNSEC) *= 1000; \
602 } while (0)
603
604/* get postop attributes from an mbuf chain */
605#define nfsm_chain_postop_attr_get(E, NMC, F, VAP) \
606 do { \
607 (F) = 0; \
608 if ((E) || !(NMC)->nmc_mhead) break; \
609 nfsm_chain_get_32((E), (NMC), (F)); \
610 if ((E) || !(F)) break; \
611 if (((E) = nfs_parsefattr((NMC), NFS_VER3, (VAP)))) \
612 (F) = 0; \
613 } while (0)
614
615/* update a node's attribute cache with postop attributes from an mbuf chain */
616/* (F returns whether the attributes were updated or not) */
617#define nfsm_chain_postop_attr_update_flag(E, NMC, NP, F, X) \
618 do { \
619 struct nfs_vattr ttvattr; \
620 nfsm_chain_postop_attr_get((E), (NMC), (F), &ttvattr); \
621 if ((E) || !(F)) break; \
622 if (((E) = nfs_loadattrcache((NP), &ttvattr, (X), 1))) { \
623 (F) = 0; \
624 break; \
1c79356b 625 } \
2d21ac55
A
626 if (*(X) == 0) \
627 (F) = 0; \
628 } while (0)
629
630/* update a node's attribute cache with postop attributes from an mbuf chain */
631#define nfsm_chain_postop_attr_update(E, NMC, NP, X) \
632 do { \
633 int __dummy_flag = 0; \
634 nfsm_chain_postop_attr_update_flag((E), (NMC), (NP), __dummy_flag, (X)); \
635 } while (0)
636
637/* get and process NFSv3 WCC data from an mbuf chain */
638#define nfsm_chain_get_wcc_data(E, NMC, NP, PREMTIME, NEWPOSTATTR, X) \
639 do { \
640 if (E) break; \
641 (E) = nfsm_chain_get_wcc_data_f((NMC), (NP), (PREMTIME), (NEWPOSTATTR), (X)); \
642 } while (0)
643
644/* update a node's attribute cache with attributes from an mbuf chain */
645#define nfsm_chain_loadattr(E, NMC, NP, VERS, A, X) \
646 do { \
647 struct nfs_vattr ttvattr, *ttnvap; \
648 if (E) break; \
649 ttnvap = (A) ? (A) : &ttvattr; \
650 if ((VERS) == NFS_VER4) { \
651 NFS_CLEAR_ATTRIBUTES(ttnvap->nva_bitmap); \
652 (E) = nfs4_parsefattr((NMC), NULL, ttnvap, NULL, NULL); \
653 } else { \
654 (E) = nfs_parsefattr((NMC), (VERS), ttnvap); \
1c79356b 655 } \
2d21ac55
A
656 if (E) break; \
657 (E) = nfs_loadattrcache((NP), ttnvap, (X), 0); \
658 } while (0)
659
660/* get NFSv4 attr bitmap */
661#define nfsm_chain_get_bitmap(E, NMC, B, LEN) \
662 do { \
b0d623f7 663 uint32_t __len = 0, __i; \
2d21ac55
A
664 nfsm_chain_get_32((E), (NMC), __len); \
665 if (E) break; \
666 for (__i=0; __i < MIN(__len, (LEN)); __i++) \
667 nfsm_chain_get_32((E), (NMC), (B)[__i]); \
668 if (E) break; \
b0d623f7
A
669 for (; __i < __len; __i++) \
670 nfsm_chain_adv((E), (NMC), NFSX_UNSIGNED); \
2d21ac55
A
671 for (; __i < (LEN); __i++) \
672 (B)[__i] = 0; \
673 (LEN) = __len; \
674 } while (0)
675
b0d623f7
A
676/* get an NFSv4 "stateid" structure from an mbuf chain */
677#define nfsm_chain_get_stateid(E, NMC, SID) \
678 do { \
679 nfsm_chain_get_32((E), (NMC), (SID)->seqid); \
680 nfsm_chain_get_32((E), (NMC), (SID)->other[0]); \
681 nfsm_chain_get_32((E), (NMC), (SID)->other[1]); \
682 nfsm_chain_get_32((E), (NMC), (SID)->other[2]); \
683 } while (0)
684
2d21ac55
A
685#define nfsm_chain_skip_tag(E, NMC) \
686 do { \
b0d623f7 687 uint32_t __val = 0; \
2d21ac55
A
688 nfsm_chain_get_32((E), (NMC), __val); \
689 nfsm_chain_adv((E), (NMC), nfsm_rndup(__val)); \
690 } while (0)
691
692#define nfsm_chain_op_check(E, NMC, OP) \
693 do { \
694 uint32_t __val = 0; \
695 nfsm_chain_get_32((E), (NMC), __val); \
696 nfsm_assert((E), (__val == (OP)), EBADRPC); \
697 nfsm_chain_get_32((E), (NMC), __val); \
698 nfsm_assert((E), (__val == NFS_OK), __val); \
699 } while (0)
700
701#define nfsm_chain_check_change_info(E, NMC, DNP) \
702 do { \
703 uint64_t __ci_before, __ci_after; \
704 uint32_t __ci_atomic = 0; \
705 nfsm_chain_get_32((E), (NMC), __ci_atomic); \
706 nfsm_chain_get_64((E), (NMC), __ci_before); \
707 nfsm_chain_get_64((E), (NMC), __ci_after); \
708 if (E) break; \
b0d623f7 709 if (__ci_atomic && (__ci_before == (DNP)->n_ncchange)) { \
2d21ac55 710 (DNP)->n_ncchange = __ci_after; \
b0d623f7 711 } else { \
2d21ac55 712 cache_purge(NFSTOV(DNP)); \
b0d623f7
A
713 (DNP)->n_ncgen++; \
714 } \
2d21ac55 715 } while (0)
1c79356b 716
9bccf70c
A
717#endif /* __APPLE_API_PRIVATE */
718#endif /* _NFS_NFSM_SUBS_H_ */