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