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