]> git.saurik.com Git - apple/xnu.git/blob - bsd/nfs/nfsm_subs.h
xnu-7195.101.1.tar.gz
[apple/xnu.git] / bsd / nfs / nfsm_subs.h
1 /*
2 * Copyright (c) 2000-2011 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
29 /*
30 * Copyright (c) 1989, 1993
31 * The Regents of the University of California. All rights reserved.
32 *
33 * This code is derived from software contributed to Berkeley by
34 * Rick Macklem at The University of Guelph.
35 *
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 3. All advertising materials mentioning features or use of this software
45 * must display the following acknowledgement:
46 * This product includes software developed by the University of
47 * California, Berkeley and its contributors.
48 * 4. Neither the name of the University nor the names of its contributors
49 * may be used to endorse or promote products derived from this software
50 * without specific prior written permission.
51 *
52 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
53 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
56 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62 * SUCH DAMAGE.
63 *
64 * @(#)nfsm_subs.h 8.2 (Berkeley) 3/30/95
65 * FreeBSD-Id: nfsm_subs.h,v 1.13 1997/07/16 09:06:30 dfr Exp $
66 */
67
68
69 #ifndef _NFS_NFSM_SUBS_H_
70 #define _NFS_NFSM_SUBS_H_
71
72 #include <sys/appleapiopts.h>
73
74 #ifdef __APPLE_API_PRIVATE
75
76 #include <nfs/nfs_conf.h>
77
78 int nfsm_rpchead(struct nfsreq *, mbuf_t, u_int64_t *, mbuf_t *);
79 int nfsm_rpchead2(struct nfsmount *, int, int, int, int, int, kauth_cred_t, struct nfsreq *, mbuf_t, u_int64_t *, mbuf_t *);
80
81 int nfsm_chain_new_mbuf(struct nfsm_chain *, size_t);
82 int nfsm_chain_add_opaque_f(struct nfsm_chain *, const u_char *, size_t);
83 int nfsm_chain_add_opaque_nopad_f(struct nfsm_chain *, const u_char *, size_t);
84 int nfsm_chain_add_uio(struct nfsm_chain *, uio_t, size_t);
85 int nfsm_chain_add_fattr4_f(struct nfsm_chain *, struct vnode_attr *, struct nfsmount *);
86 int nfsm_chain_add_v2sattr_f(struct nfsm_chain *, struct vnode_attr *, uint32_t);
87 int nfsm_chain_add_v3sattr_f(struct nfsmount *, struct nfsm_chain *, struct vnode_attr *);
88 int nfsm_chain_add_string_nfc(struct nfsm_chain *, const uint8_t *, size_t);
89
90 int nfsm_chain_advance(struct nfsm_chain *, size_t);
91 size_t nfsm_chain_offset(struct nfsm_chain *);
92 int nfsm_chain_reverse(struct nfsm_chain *, size_t);
93 int nfsm_chain_get_opaque_pointer_f(struct nfsm_chain *, uint32_t, u_char **);
94 int nfsm_chain_get_opaque_f(struct nfsm_chain *, size_t, u_char *);
95 int nfsm_chain_get_uio(struct nfsm_chain *, size_t, uio_t);
96 int nfsm_chain_get_fh_attr(struct nfsmount *, struct nfsm_chain *, nfsnode_t,
97 vfs_context_t, int, uint64_t *, fhandle_t *, struct nfs_vattr *);
98 int nfsm_chain_get_wcc_data_f(struct nfsm_chain *, nfsnode_t, struct timespec *, int *, u_int64_t *);
99 int nfsm_chain_get_secinfo(struct nfsm_chain *, uint32_t *, int *);
100
101 #if CONFIG_NFS_SERVER
102 void nfsm_adj(mbuf_t, int, int);
103 int nfsm_mbuf_get_list(size_t, mbuf_t *, int *);
104
105 int nfsm_chain_add_fattr(struct nfsrv_descript *, struct nfsm_chain *, struct vnode_attr *);
106 int nfsm_chain_add_wcc_data_f(struct nfsrv_descript *, struct nfsm_chain *, int,
107 struct vnode_attr *, int, struct vnode_attr *);
108 int nfsm_chain_get_path_namei(struct nfsm_chain *, uint32_t, struct nameidata *);
109 int nfsm_chain_get_sattr(struct nfsrv_descript *, struct nfsm_chain *, struct vnode_attr *);
110 int nfsm_chain_trim_data(struct nfsm_chain *, int, int *);
111 #endif /* CONFIG_NFS_SERVER */
112
113 /* check name length */
114 #define nfsm_name_len_check(E, ND, LEN) \
115 do { \
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; \
121 } while (0)
122
123 #define nfsm_assert(E, COND, ERR) \
124 do { \
125 if (E) break; \
126 if (!(COND)) \
127 (E) = (ERR); \
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 { \
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); \
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 { \
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); \
160 } while (0)
161
162 /* round up to a multiple of 4 */
163 #define nfsm_rndup(a) (((a)+3)&(~0x3))
164
165 #define nfsm_pad(a) (nfsm_rndup(a) - (a))
166
167 /*
168 * control flow macros:
169 * go to the appropriate label on condition
170 */
171 #define nfsmout_if(E) do { if (E) goto nfsmout; } while (0)
172 #define nfsmerr_if(E) do { if (E) goto nfsmerr; } while (0)
173
174 /*
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.
178 */
179 #define nfsmout_on_status(ND, E) \
180 do { \
181 if (((ND)->nd_repstat == EBADRPC) || \
182 ((ND)->nd_repstat && ((ND)->nd_vers == NFS_VER2))) { \
183 (E) = 0; \
184 goto nfsmout; \
185 } \
186 } while (0)
187
188 /* initialize an mbuf chain */
189 #define nfsm_chain_null(NMC) \
190 do { \
191 (NMC)->nmc_mhead = (NMC)->nmc_mcur = NULL; \
192 (NMC)->nmc_ptr = NULL; \
193 } while (0)
194
195 /* cleanup an mbuf chain */
196 #define nfsm_chain_cleanup(NMC) \
197 do { \
198 if (!(NMC)->nmc_mhead) break; \
199 mbuf_freem((NMC)->nmc_mhead); \
200 nfsm_chain_null(NMC); \
201 } while (0)
202
203 /* get an mbuf given a size hint */
204 #define nfsm_mbuf_get(E, MBP, SIZEHINT) \
205 do { \
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)); \
211 } while (0)
212
213 /*
214 * get an mbuf with size of M16KCLBYTES given a size hint
215 * According to mbuf_getcluster() documentation, clusters greater than 4096 bytes might
216 * not be available in all configurations; the caller must additionally check for ENOTSUP.
217 * */
218 #define nfsm_mbuf_getcluster(E, MBP, SIZEHINT) \
219 do { \
220 *(MBP) = NULL; \
221 if ((size_t)(SIZEHINT) > MBIGCLBYTES) { \
222 (E) = mbuf_getcluster(MBUF_WAITOK, MBUF_TYPE_DATA, M16KCLBYTES, (MBP)); \
223 if ((E) == 0) { \
224 break; \
225 } \
226 } \
227 if ((size_t)(SIZEHINT) > MCLBYTES) { \
228 (E) = mbuf_getcluster(MBUF_WAITOK, MBUF_TYPE_DATA, MBIGCLBYTES, (MBP)); \
229 if ((E) == 0) { \
230 break; \
231 } \
232 } \
233 nfsm_mbuf_get(E, MBP, SIZEHINT); \
234 } while (0)
235
236
237 /*
238 * macros for building NFS mbuf chains
239 */
240
241 /* prepare an mbuf chain for building starting with the given mbuf */
242 #define nfsm_chain_init(NMC, MB) \
243 do { \
244 (NMC)->nmc_mhead = (MB); \
245 (NMC)->nmc_mcur = (NMC)->nmc_mhead; \
246 (NMC)->nmc_ptr = mbuf_data((NMC)->nmc_mcur); \
247 (NMC)->nmc_left = mbuf_trailingspace((NMC)->nmc_mcur); \
248 (NMC)->nmc_flags = 0; \
249 } while (0)
250
251 /* prepare an mbuf chain for building starting with a newly allocated mbuf */
252 #define nfsm_chain_build_alloc_init(E, NMC, SIZEHINT) \
253 do { \
254 mbuf_t ncbimb; \
255 nfsm_mbuf_get((E), &ncbimb, (SIZEHINT)); \
256 if (E) break; \
257 nfsm_chain_init((NMC), ncbimb); \
258 } while (0)
259
260 /* done building an mbuf chain */
261 #define nfsm_chain_build_done(E, NMC) \
262 do { \
263 if ((E) || !(NMC)->nmc_mcur) break; \
264 /* cap off current mbuf */ \
265 mbuf_setlen((NMC)->nmc_mcur, \
266 (NMC)->nmc_ptr - (caddr_t)mbuf_data((NMC)->nmc_mcur)); \
267 } while (0)
268
269 /*
270 * set the TCP record mark at the head of an mbuf chain -
271 * assumes 4 bytes are already allocated in the head mbuf
272 */
273 #define nfsm_chain_set_recmark(E, NMC, VAL) \
274 do { \
275 if (E) break; \
276 *((uint32_t*)mbuf_data((NMC)->nmc_mhead)) \
277 = txdr_unsigned(VAL); \
278 } while (0)
279
280 /* make sure there's room for size bytes in current mbuf */
281 #define nfsm_chain_check_size(E, NMC, SIZE) \
282 do { \
283 if (E) break; \
284 if ((NMC)->nmc_left < (SIZE)) { \
285 (E) = nfsm_chain_new_mbuf((NMC), (SIZE)); \
286 if (!(E) && ((NMC)->nmc_left < (SIZE))) \
287 (E) = ENOMEM; \
288 } \
289 } while (0)
290
291 /* add a 32bit value to an mbuf chain extending if necessary */
292 #define nfsm_chain_add_32(E, NMC, VAL) \
293 do { \
294 nfsm_chain_check_size((E), (NMC), NFSX_UNSIGNED); \
295 if (E) break; \
296 *((uint32_t*)(NMC)->nmc_ptr) = txdr_unsigned(VAL); \
297 (NMC)->nmc_ptr += NFSX_UNSIGNED; \
298 (NMC)->nmc_left -= NFSX_UNSIGNED; \
299 } while (0)
300
301 /* add a 64bit value to an mbuf chain */
302 #define nfsm_chain_add_64(E, NMC, VAL) \
303 do { \
304 uint64_t __tmp64; \
305 nfsm_chain_check_size((E), (NMC), 2 * NFSX_UNSIGNED); \
306 if (E) break; \
307 __tmp64 = (VAL); \
308 txdr_hyper(&__tmp64, (NMC)->nmc_ptr); \
309 (NMC)->nmc_ptr += 2 * NFSX_UNSIGNED; \
310 (NMC)->nmc_left -= 2 * NFSX_UNSIGNED; \
311 } while (0)
312
313 /* zero the last 4 bytes for a range of opaque */
314 /* data to make sure any pad bytes will be zero. */
315 #define nfsm_chain_zero_opaque_pad(BUF, LEN) \
316 do { \
317 if ((LEN) > 0) \
318 *(((uint32_t*)(BUF))+((nfsm_rndup(LEN)>>2)-1)) = 0; \
319 } while (0)
320
321 /* add buffer of opaque data to an mbuf chain */
322 #define nfsm_chain_add_opaque(E, NMC, BUF, LEN) \
323 do { \
324 size_t rndlen = nfsm_rndup(LEN); \
325 if (E) break; \
326 if ((NMC)->nmc_left < rndlen) { \
327 (E) = nfsm_chain_add_opaque_f((NMC), (const u_char*)(BUF), (LEN)); \
328 break; \
329 } \
330 nfsm_chain_zero_opaque_pad((NMC)->nmc_ptr, (LEN)); \
331 bcopy((BUF), (NMC)->nmc_ptr, (LEN)); \
332 (NMC)->nmc_ptr += rndlen; \
333 (NMC)->nmc_left -= rndlen; \
334 } while (0)
335
336 /* add buffer of opaque data to an mbuf chain without padding */
337 #define nfsm_chain_add_opaque_nopad(E, NMC, BUF, LEN) \
338 do { \
339 if (E) break; \
340 if ((NMC)->nmc_left < (uint32_t) (LEN)) { \
341 (E) = nfsm_chain_add_opaque_nopad_f((NMC), (const u_char*)(BUF), (LEN)); \
342 break; \
343 } \
344 bcopy((BUF), (NMC)->nmc_ptr, (LEN)); \
345 (NMC)->nmc_ptr += (LEN); \
346 (NMC)->nmc_left -= (LEN); \
347 } while (0)
348
349 /* finish an mbuf in a chain to allow subsequent insertion */
350 #define nfsm_chain_finish_mbuf(E, NMC) \
351 do { \
352 if (E) break; \
353 mbuf_setlen((NMC)->nmc_mcur, \
354 (NMC)->nmc_ptr - (caddr_t)mbuf_data((NMC)->nmc_mcur)); \
355 (NMC)->nmc_left = 0; \
356 } while (0)
357
358 /* add a file handle to an mbuf chain */
359 #define nfsm_chain_add_fh(E, NMC, VERS, FHP, FHLEN) \
360 do { \
361 if (E) break; \
362 if ((VERS) != NFS_VER2) \
363 nfsm_chain_add_32((E), (NMC), (FHLEN)); \
364 nfsm_chain_add_opaque((E), (NMC), (FHP), (FHLEN)); \
365 } while (0)
366
367 /* add a string to an mbuf chain */
368 #define nfsm_chain_add_string(E, NMC, STR, LEN) \
369 do { \
370 nfsm_chain_add_32((E), (NMC), (LEN)); \
371 nfsm_chain_add_opaque((E), (NMC), (STR), (LEN)); \
372 } while (0)
373
374 /* add a name to an mbuf chain */
375 #define nfsm_chain_add_name(E, NMC, STR, LEN, NMP) \
376 do { \
377 if (E) break; \
378 if (NMFLAG((NMP), NFC)) \
379 (E) = nfsm_chain_add_string_nfc((NMC), (const uint8_t*)(STR), (LEN)); \
380 else \
381 nfsm_chain_add_string((E), (NMC), (STR), (LEN)); \
382 } while (0)
383
384 /* add an NFSv2 time to an mbuf chain */
385 #define nfsm_chain_add_v2time(E, NMC, TVP) \
386 do { \
387 if (TVP) { \
388 nfsm_chain_add_32((E), (NMC), (TVP)->tv_sec); \
389 nfsm_chain_add_32((E), (NMC), ((TVP)->tv_nsec != -1) ? \
390 ((uint32_t)(TVP)->tv_nsec / 1000) : 0xffffffff); \
391 } else { \
392 /* no time... use -1 */ \
393 nfsm_chain_add_32((E), (NMC), -1); \
394 nfsm_chain_add_32((E), (NMC), -1); \
395 } \
396 } while (0)
397
398 /* add an NFSv3 time to an mbuf chain */
399 #define nfsm_chain_add_v3time(E, NMC, TVP) \
400 do { \
401 nfsm_chain_add_32((E), (NMC), (TVP)->tv_sec); \
402 nfsm_chain_add_32((E), (NMC), (TVP)->tv_nsec); \
403 } while (0)
404
405 /* add an NFS v2 or v3 time to an mbuf chain */
406 #define nfsm_chain_add_time(E, NMC, VERS, TVP) \
407 do { \
408 if ((VERS) == NFS_VER2) { \
409 nfsm_chain_add_v2time((E), (NMC), (TVP)); \
410 } else { \
411 nfsm_chain_add_v3time((E), (NMC), (TVP)); \
412 } \
413 } while (0)
414
415 /* add an NFSv3 postop file handle to an mbuf chain */
416 #define nfsm_chain_add_postop_fh(E, NMC, FHP, FHLEN) \
417 do { \
418 nfsm_chain_add_32((E), (NMC), TRUE); \
419 nfsm_chain_add_fh((E), (NMC), NFS_VER3, (FHP), (FHLEN)); \
420 } while (0)
421
422 /* add NFSv3 postop attributes to an mbuf chain */
423 #define nfsm_chain_add_postop_attr(E, ND, NMC, ATTRERR, VAP) \
424 do { \
425 if (E) break; \
426 if (ATTRERR) { \
427 nfsm_chain_add_32((E), (NMC), FALSE); \
428 break; \
429 } \
430 nfsm_chain_add_32((E), (NMC), TRUE); \
431 if (E) break; \
432 (E) = nfsm_chain_add_fattr((ND), (NMC), (VAP)); \
433 } while (0)
434
435 /* Add an NFSv2 "sattr" structure to an mbuf chain */
436 #define nfsm_chain_add_v2sattr(E, NMC, VAP, SZRDEV) \
437 do { \
438 if (E) break; \
439 (E) = nfsm_chain_add_v2sattr_f((NMC), (VAP), (SZRDEV)); \
440 } while (0)
441
442 /* Add an NFSv3 "sattr" structure to an mbuf chain */
443 #define nfsm_chain_add_v3sattr(NMP, E, NMC, VAP) \
444 do { \
445 if (E) break; \
446 (E) = nfsm_chain_add_v3sattr_f((NMP), (NMC), (VAP)); \
447 } while (0)
448
449 /* Add an NFSv4 "fattr" structure to an mbuf chain */
450 #define nfsm_chain_add_fattr4(E, NMC, VAP, NMP) \
451 do { \
452 if (E) break; \
453 (E) = nfsm_chain_add_fattr4_f((NMC), (VAP), (NMP)); \
454 } while (0)
455
456 /* add NFSv3 WCC data to an mbuf chain */
457 #define nfsm_chain_add_wcc_data(E, ND, NMC, PREERR, PREVAP, POSTERR, POSTVAP) \
458 do { \
459 if (E) break; \
460 (E) = nfsm_chain_add_wcc_data_f((ND), (NMC), \
461 (PREERR), (PREVAP), (POSTERR), (POSTVAP)); \
462 } while (0)
463
464 /* add NFSv4 COMPOUND header */
465 #define NFS4_TAG_LENGTH 12
466 #define nfsm_chain_add_compound_header(E, NMC, TAG, MINOR, NUMOPS) \
467 do { \
468 if ((TAG) && strlen(TAG)) { \
469 /* put tags into a fixed-length space-padded field */ \
470 char __nfstag[NFS4_TAG_LENGTH+1]; \
471 snprintf(__nfstag, sizeof(__nfstag), "%-*s", NFS4_TAG_LENGTH, (TAG)); \
472 nfsm_chain_add_32((E), (NMC), NFS4_TAG_LENGTH); \
473 nfsm_chain_add_opaque((E), (NMC), __nfstag, NFS4_TAG_LENGTH); \
474 } else { \
475 nfsm_chain_add_32((E), (NMC), 0); \
476 } \
477 nfsm_chain_add_32((E), (NMC), (MINOR)); /*minorversion*/ \
478 nfsm_chain_add_32((E), (NMC), (NUMOPS)); \
479 } while (0)
480
481 /* add NFSv4 attr bitmap */
482 #define nfsm_chain_add_bitmap(E, NMC, B, LEN) \
483 do { \
484 int __i; \
485 nfsm_chain_add_32((E), (NMC), (LEN)); \
486 for (__i=0; __i < (LEN); __i++) \
487 nfsm_chain_add_32((E), (NMC), (B)[__i]); \
488 } while (0)
489
490 /* add NFSv4 attr bitmap masked with the given mask */
491 #define nfsm_chain_add_bitmap_masked(E, NMC, B, LEN, MASK) \
492 do { \
493 int __i; \
494 nfsm_chain_add_32((E), (NMC), (LEN)); \
495 for (__i=0; __i < (LEN); __i++) \
496 nfsm_chain_add_32((E), (NMC), ((B)[__i] & (MASK)[__i])); \
497 } while (0)
498
499 /* add NFSv4 attr bitmap masked with the supported attributes for this mount/node */
500 #define nfsm_chain_add_bitmap_supported(E, NMC, B, NMP, NP) \
501 do { \
502 uint32_t __bitmap[NFS_ATTR_BITMAP_LEN], *__bmp = (B); \
503 int __nonamedattr = 0, __noacl = 0, __nomode = 0; \
504 if (!((NMP)->nm_fsattr.nfsa_flags & NFS_FSFLAG_NAMED_ATTR) || \
505 ((NP) && (((nfsnode_t)(NP))->n_flag & (NISDOTZFS|NISDOTZFSCHILD)))) \
506 __nonamedattr = 1; \
507 if (!((NMP)->nm_fsattr.nfsa_flags & NFS_FSFLAG_ACL)) \
508 __noacl = 1; \
509 if (NMFLAG((NMP), ACLONLY)) \
510 __nomode = 1; \
511 if (__nonamedattr || __noacl || __nomode) { \
512 /* don't ask for attrs we're not supporting */ \
513 /* some ".zfs" directories can't handle being asked for some attributes */ \
514 int __ii; \
515 NFS_CLEAR_ATTRIBUTES(__bitmap); \
516 for (__ii=0; __ii < NFS_ATTR_BITMAP_LEN; __ii++) \
517 __bitmap[__ii] = (B)[__ii]; \
518 if (__nonamedattr) \
519 NFS_BITMAP_CLR(__bitmap, NFS_FATTR_NAMED_ATTR); \
520 if (__noacl) \
521 NFS_BITMAP_CLR(__bitmap, NFS_FATTR_ACL); \
522 if (__nomode) \
523 NFS_BITMAP_CLR(__bitmap, NFS_FATTR_MODE); \
524 __bmp = __bitmap; \
525 } \
526 nfsm_chain_add_bitmap_masked((E), (NMC), __bmp, NFS_ATTR_BITMAP_LEN, (NMP)->nm_fsattr.nfsa_supp_attr); \
527 } while (0)
528
529 /* Add an NFSv4 "stateid" structure to an mbuf chain */
530 #define nfsm_chain_add_stateid(E, NMC, SID) \
531 do { \
532 nfsm_chain_add_32((E), (NMC), (SID)->seqid); \
533 nfsm_chain_add_32((E), (NMC), (SID)->other[0]); \
534 nfsm_chain_add_32((E), (NMC), (SID)->other[1]); \
535 nfsm_chain_add_32((E), (NMC), (SID)->other[2]); \
536 } while (0)
537
538 /* add an NFSv4 lock owner structure to an mbuf chain */
539 #define nfsm_chain_add_lock_owner4(E, NMC, NMP, NLOP) \
540 do { \
541 nfsm_chain_add_64((E), (NMC), (NMP)->nm_clientid); \
542 nfsm_chain_add_32((E), (NMC), 5*NFSX_UNSIGNED); \
543 nfsm_chain_add_32((E), (NMC), (NLOP)->nlo_name); \
544 nfsm_chain_add_32((E), (NMC), (NLOP)->nlo_pid); \
545 nfsm_chain_add_64((E), (NMC), (NLOP)->nlo_pid_start.tv_sec); \
546 nfsm_chain_add_32((E), (NMC), (NLOP)->nlo_pid_start.tv_usec); \
547 } while (0)
548
549 /*
550 * macros for dissecting NFS mbuf chains
551 */
552
553 /* prepare an mbuf chain for dissection starting with the given mbuf */
554 #define nfsm_chain_dissect_init(E, NMC, H) \
555 do { \
556 if (!(H)) { \
557 (E) = EINVAL; \
558 break; \
559 } \
560 (NMC)->nmc_mcur = (NMC)->nmc_mhead = (H); \
561 (NMC)->nmc_ptr = mbuf_data(H); \
562 (NMC)->nmc_left = mbuf_len(H); \
563 } while (0)
564
565 /* skip a number of bytes in an mbuf chain */
566 #define nfsm_chain_adv(E, NMC, LEN) \
567 do { \
568 if (E) break; \
569 if ((NMC)->nmc_left >= (uint32_t)(LEN)) { \
570 (NMC)->nmc_left -= (LEN); \
571 (NMC)->nmc_ptr += (LEN); \
572 } else { \
573 (E) = nfsm_chain_advance((NMC), (LEN)); \
574 } \
575 } while (0)
576
577 /* get a 32bit value from an mbuf chain */
578 #define nfsm_chain_get_32(E, NMC, LVAL) \
579 do { \
580 uint32_t __tmp32, *__tmpptr; \
581 (LVAL) = 0; \
582 if (E) break; \
583 if ((NMC)->nmc_left >= NFSX_UNSIGNED) { \
584 __tmpptr = (uint32_t*)(NMC)->nmc_ptr; \
585 (NMC)->nmc_left -= NFSX_UNSIGNED; \
586 (NMC)->nmc_ptr += NFSX_UNSIGNED; \
587 } else { \
588 __tmpptr = &__tmp32; \
589 (E) = nfsm_chain_get_opaque_f((NMC), NFSX_UNSIGNED, (u_char*)__tmpptr); \
590 if (E) break; \
591 } \
592 (LVAL) = fxdr_unsigned(uint32_t, *__tmpptr); \
593 } while (0)
594
595 /* get a 64bit value from an mbuf chain */
596 #define nfsm_chain_get_64(E, NMC, LVAL) \
597 do { \
598 uint64_t __tmp64, *__tmpptr; \
599 (LVAL) = 0; \
600 if (E) break; \
601 if ((NMC)->nmc_left >= 2 * NFSX_UNSIGNED) { \
602 __tmpptr = (uint64_t*)(NMC)->nmc_ptr; \
603 (NMC)->nmc_left -= 2 * NFSX_UNSIGNED; \
604 (NMC)->nmc_ptr += 2 * NFSX_UNSIGNED; \
605 } else { \
606 __tmpptr = &__tmp64; \
607 (E) = nfsm_chain_get_opaque_f((NMC), 2 * NFSX_UNSIGNED, (u_char*)__tmpptr); \
608 if (E) break; \
609 } \
610 fxdr_hyper(__tmpptr, &(LVAL)); \
611 } while (0)
612
613 /* get a pointer to the next consecutive bytes in an mbuf chain */
614 #define nfsm_chain_get_opaque_pointer(E, NMC, LEN, PTR) \
615 do { \
616 uint32_t rndlen; \
617 if (E) break; \
618 rndlen = nfsm_rndup(LEN); \
619 if (rndlen < (LEN)) { \
620 (E) = EBADRPC; \
621 break; \
622 } \
623 if ((NMC)->nmc_left >= rndlen) { \
624 (PTR) = (void*)(NMC)->nmc_ptr; \
625 (NMC)->nmc_left -= rndlen; \
626 (NMC)->nmc_ptr += rndlen; \
627 } else { \
628 (E) = nfsm_chain_get_opaque_pointer_f((NMC), (LEN), (u_char**)&(PTR)); \
629 } \
630 } while (0)
631
632 /* copy the next consecutive bytes of opaque data from an mbuf chain */
633 #define nfsm_chain_get_opaque(E, NMC, LEN, PTR) \
634 do { \
635 size_t rndlen; \
636 if (E) break; \
637 rndlen = nfsm_rndup(LEN); \
638 if (rndlen < (LEN)) { \
639 (E) = EBADRPC; \
640 break; \
641 } \
642 if ((NMC)->nmc_left >= rndlen) { \
643 u_char *__tmpptr = (u_char*)(NMC)->nmc_ptr; \
644 (NMC)->nmc_left -= rndlen; \
645 (NMC)->nmc_ptr += rndlen; \
646 bcopy(__tmpptr, (PTR), (LEN)); \
647 } else { \
648 (E) = nfsm_chain_get_opaque_f((NMC), (LEN), (u_char*)(PTR)); \
649 } \
650 } while (0)
651
652 /* get the size of and a pointer to a file handle in an mbuf chain */
653 #define nfsm_chain_get_fh_ptr(E, NMC, VERS, FHP, FHSIZE) \
654 do { \
655 if ((VERS) != NFS_VER2) { \
656 nfsm_chain_get_32((E), (NMC), (FHSIZE)); \
657 if (E) break; \
658 if ((FHSIZE) > NFS_MAX_FH_SIZE) \
659 (E) = EBADRPC; \
660 } else \
661 (FHSIZE) = NFSX_V2FH;\
662 if ((E) == 0) \
663 nfsm_chain_get_opaque_pointer((E), (NMC), (FHSIZE), (FHP));\
664 } while (0)
665
666 /* get the size of and data for a file handle in an mbuf chain */
667 #define nfsm_chain_get_fh(E, NMC, VERS, FHP) \
668 do { \
669 if ((VERS) != NFS_VER2) { \
670 nfsm_chain_get_32((E), (NMC), (FHP)->fh_len); \
671 if ((FHP)->fh_len > sizeof((FHP)->fh_data)) \
672 (E) = EBADRPC; \
673 } else \
674 (FHP)->fh_len = NFSX_V2FH;\
675 if ((E) == 0) \
676 nfsm_chain_get_opaque((E), (NMC), (uint32_t)(FHP)->fh_len, (FHP)->fh_data);\
677 else \
678 (FHP)->fh_len = 0;\
679 } while (0)
680
681 /* get an NFS v2 or v3 time from an mbuf chain */
682 #define nfsm_chain_get_time(E, NMC, VERS, TSEC, TNSEC) \
683 do { \
684 nfsm_chain_get_32((E), (NMC), (TSEC)); \
685 nfsm_chain_get_32((E), (NMC), (TNSEC)); \
686 if ((E) || ((VERS) != NFS_VER2)) break; \
687 if ((uint32_t)(TNSEC) == 0xffffffff) \
688 (TNSEC) = 0; \
689 else \
690 (TNSEC) *= 1000; \
691 } while (0)
692
693 /* get postop attributes from an mbuf chain */
694 #define nfsm_chain_postop_attr_get(NMP, E, NMC, F, VAP) \
695 do { \
696 (F) = 0; \
697 if ((E) || !(NMC)->nmc_mhead) break; \
698 nfsm_chain_get_32((E), (NMC), (F)); \
699 if ((E) || !(F)) break; \
700 if (((E) = nfs_parsefattr((NMP), (NMC), NFS_VER3, (VAP)))) \
701 (F) = 0; \
702 } while (0)
703
704 /* update a node's attribute cache with postop attributes from an mbuf chain */
705 /* (F returns whether the attributes were updated or not) */
706 #define nfsm_chain_postop_attr_update_flag(E, NMC, NP, F, X) \
707 do { \
708 struct nfs_vattr ttvattr; \
709 nfsm_chain_postop_attr_get(NFSTONMP(NP), (E), (NMC), (F), &ttvattr); \
710 if ((E) || !(F)) break; \
711 if (((E) = nfs_loadattrcache((NP), &ttvattr, (X), 1))) { \
712 (F) = 0; \
713 break; \
714 } \
715 if (*(X) == 0) \
716 (F) = 0; \
717 } while (0)
718
719 /* update a node's attribute cache with postop attributes from an mbuf chain */
720 #define nfsm_chain_postop_attr_update(E, NMC, NP, X) \
721 do { \
722 int __dummy_flag = 0; \
723 nfsm_chain_postop_attr_update_flag((E), (NMC), (NP), __dummy_flag, (X)); \
724 } while (0)
725
726 /* get and process NFSv3 WCC data from an mbuf chain */
727 #define nfsm_chain_get_wcc_data(E, NMC, NP, PREMTIME, NEWPOSTATTR, X) \
728 do { \
729 if (E) break; \
730 (E) = nfsm_chain_get_wcc_data_f((NMC), (NP), (PREMTIME), (NEWPOSTATTR), (X)); \
731 } while (0)
732
733 #if CONFIG_NFS4
734 /* separate v4 variant for loading attrs that only runs when NFSv4 is set */
735 #define __nfsm_chain_loadattr_v4(E, NMC, VERS, X, VATTR) \
736 do { \
737 (E) = nfs4_parsefattr((NMC), NULL, (VATTR), NULL, NULL, NULL); \
738 } while (0)
739 #else
740 #define __nfsm_chain_loadattr_v4(E, NMC, VERS, X, VATTR) \
741 do { \
742 break; \
743 } while (0)
744 #endif
745
746 /* update a node's attribute cache with attributes from an mbuf chain */
747 #define nfsm_chain_loadattr(E, NMC, NP, VERS, X) \
748 do { \
749 struct nfs_vattr ttvattr; \
750 if (E) break; \
751 if ((VERS) == NFS_VER4) { \
752 __nfsm_chain_loadattr_v4((E), (NMC), (VERS), (X), &ttvattr); \
753 } else { \
754 (E) = nfs_parsefattr(NFSTONMP(NP), (NMC), (VERS), &ttvattr); \
755 } \
756 if (!(E) && (NP)) \
757 (E) = nfs_loadattrcache((NP), &ttvattr, (X), 0); \
758 NVATTR_CLEANUP(&ttvattr); \
759 } while (0)
760
761 /* get NFSv4 attr bitmap */
762 #define nfsm_chain_get_bitmap(E, NMC, B, LEN) \
763 do { \
764 uint32_t __len = 0, __i; \
765 nfsm_chain_get_32((E), (NMC), __len); \
766 if (E) break; \
767 for (__i=0; __i < MIN(__len, (LEN)); __i++) \
768 nfsm_chain_get_32((E), (NMC), (B)[__i]); \
769 if (E) break; \
770 for (; __i < __len; __i++) \
771 nfsm_chain_adv((E), (NMC), NFSX_UNSIGNED); \
772 for (; __i < (LEN); __i++) \
773 (B)[__i] = 0; \
774 (LEN) = __len; \
775 } while (0)
776
777 /* get an NFSv4 "stateid" structure from an mbuf chain */
778 #define nfsm_chain_get_stateid(E, NMC, SID) \
779 do { \
780 nfsm_chain_get_32((E), (NMC), (SID)->seqid); \
781 nfsm_chain_get_32((E), (NMC), (SID)->other[0]); \
782 nfsm_chain_get_32((E), (NMC), (SID)->other[1]); \
783 nfsm_chain_get_32((E), (NMC), (SID)->other[2]); \
784 } while (0)
785
786 #define nfsm_chain_skip_tag(E, NMC) \
787 do { \
788 uint32_t __val = 0; \
789 nfsm_chain_get_32((E), (NMC), __val); \
790 nfsm_chain_adv((E), (NMC), nfsm_rndup(__val)); \
791 } while (0)
792
793 #define nfsm_chain_op_check(E, NMC, OP) \
794 do { \
795 uint32_t __val = 0; \
796 nfsm_chain_get_32((E), (NMC), __val); \
797 /* [sigh] some implementations return the "illegal" op for unsupported ops */ \
798 nfsm_assert((E), ((__val == (OP)) || (__val == NFS_OP_ILLEGAL)), EBADRPC); \
799 nfsm_chain_get_32((E), (NMC), __val); \
800 nfsm_assert((E), (__val == NFS_OK), __val); \
801 } while (0)
802
803 #define nfsm_chain_check_change_info(E, NMC, DNP) \
804 do { \
805 uint64_t __ci_before = 0, __ci_after = 0; \
806 uint32_t __ci_atomic = 0; \
807 nfsm_chain_get_32((E), (NMC), __ci_atomic); \
808 nfsm_chain_get_64((E), (NMC), __ci_before); \
809 nfsm_chain_get_64((E), (NMC), __ci_after); \
810 if ((E) || !(DNP)) break; \
811 if (__ci_atomic && (__ci_before == (DNP)->n_ncchange)) { \
812 (DNP)->n_ncchange = __ci_after; \
813 } else { \
814 cache_purge(NFSTOV(DNP)); \
815 (DNP)->n_ncgen++; \
816 } \
817 } while (0)
818
819 #endif /* __APPLE_API_PRIVATE */
820 #endif /* _NFS_NFSM_SUBS_H_ */