]> git.saurik.com Git - apple/xnu.git/blob - bsd/nfs/nfsm_subs.h
xnu-4570.61.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 int nfsm_rpchead(struct nfsreq *, mbuf_t, u_int64_t *, mbuf_t *);
77 int nfsm_rpchead2(struct nfsmount *, int, int, int, int, int, kauth_cred_t, struct nfsreq *, mbuf_t, u_int64_t *, mbuf_t *);
78
79 int nfsm_chain_new_mbuf(struct nfsm_chain *, size_t);
80 int nfsm_chain_add_opaque_f(struct nfsm_chain *, const u_char *, uint32_t);
81 int nfsm_chain_add_opaque_nopad_f(struct nfsm_chain *, const u_char *, uint32_t);
82 int nfsm_chain_add_uio(struct nfsm_chain *, uio_t, uint32_t);
83 int nfsm_chain_add_fattr4_f(struct nfsm_chain *, struct vnode_attr *, struct nfsmount *);
84 int nfsm_chain_add_v2sattr_f(struct nfsm_chain *, struct vnode_attr *, uint32_t);
85 int nfsm_chain_add_v3sattr_f(struct nfsm_chain *, struct vnode_attr *);
86 int nfsm_chain_add_string_nfc(struct nfsm_chain *, const uint8_t *, uint32_t);
87
88 int nfsm_chain_advance(struct nfsm_chain *, uint32_t);
89 int nfsm_chain_offset(struct nfsm_chain *);
90 int nfsm_chain_reverse(struct nfsm_chain *, uint32_t);
91 int nfsm_chain_get_opaque_pointer_f(struct nfsm_chain *, uint32_t, u_char **);
92 int nfsm_chain_get_opaque_f(struct nfsm_chain *, uint32_t, u_char *);
93 int nfsm_chain_get_uio(struct nfsm_chain *, uint32_t, uio_t);
94 int nfsm_chain_get_fh_attr(struct nfsm_chain *, nfsnode_t,
95 vfs_context_t, int, uint64_t *, fhandle_t *, struct nfs_vattr *);
96 int nfsm_chain_get_wcc_data_f(struct nfsm_chain *, nfsnode_t, struct timespec *, int *, u_int64_t *);
97 int nfsm_chain_get_secinfo(struct nfsm_chain *, uint32_t *, int *);
98
99 #if NFSSERVER
100 void nfsm_adj(mbuf_t, int, int);
101 int nfsm_mbuf_get_list(size_t, mbuf_t *, int *);
102
103 int nfsm_chain_add_fattr(struct nfsrv_descript *, struct nfsm_chain *, struct vnode_attr *);
104 int nfsm_chain_add_wcc_data_f(struct nfsrv_descript *, struct nfsm_chain *, int,
105 struct vnode_attr *, int, struct vnode_attr *);
106 int nfsm_chain_get_path_namei(struct nfsm_chain *, uint32_t, struct nameidata *);
107 int nfsm_chain_get_sattr(struct nfsrv_descript *, struct nfsm_chain *, struct vnode_attr *);
108 int 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 { \
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; \
119 } while (0)
120
121 #define nfsm_assert(E, COND, ERR) \
122 do { \
123 if (E) break; \
124 if (!(COND)) \
125 (E) = (ERR); \
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 { \
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); \
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 { \
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); \
158 } while (0)
159
160 /* round up to a multiple of 4 */
161 #define nfsm_rndup(a) (((a)+3)&(~0x3))
162
163 #define nfsm_pad(a) (nfsm_rndup(a) - (a))
164
165 /*
166 * control flow macros:
167 * go to the appropriate label on condition
168 */
169 #define nfsmout_if(E) do { if (E) goto nfsmout; } while (0)
170 #define nfsmerr_if(E) do { if (E) goto nfsmerr; } while (0)
171
172 /*
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.
176 */
177 #define nfsmout_on_status(ND, E) \
178 do { \
179 if (((ND)->nd_repstat == EBADRPC) || \
180 ((ND)->nd_repstat && ((ND)->nd_vers == NFS_VER2))) { \
181 (E) = 0; \
182 goto nfsmout; \
183 } \
184 } while (0)
185
186 /* initialize an mbuf chain */
187 #define nfsm_chain_null(NMC) \
188 do { \
189 (NMC)->nmc_mhead = (NMC)->nmc_mcur = NULL; \
190 (NMC)->nmc_ptr = NULL; \
191 } while (0)
192
193 /* cleanup an mbuf chain */
194 #define nfsm_chain_cleanup(NMC) \
195 do { \
196 if (!(NMC)->nmc_mhead) break; \
197 mbuf_freem((NMC)->nmc_mhead); \
198 nfsm_chain_null(NMC); \
199 } while (0)
200
201 /* get an mbuf given a size hint */
202 #define nfsm_mbuf_get(E, MBP, SIZEHINT) \
203 do { \
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)); \
209 } while (0)
210
211
212 /*
213 * macros for building NFS mbuf chains
214 */
215
216 /* prepare an mbuf chain for building starting with the given mbuf */
217 #define nfsm_chain_init(NMC, MB) \
218 do { \
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; \
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 { \
229 mbuf_t ncbimb; \
230 nfsm_mbuf_get((E), &ncbimb, (SIZEHINT)); \
231 if (E) break; \
232 nfsm_chain_init((NMC), ncbimb); \
233 } while (0)
234
235 /* done building an mbuf chain */
236 #define nfsm_chain_build_done(E, NMC) \
237 do { \
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)); \
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 { \
250 if (E) break; \
251 *((uint32_t*)mbuf_data((NMC)->nmc_mhead)) \
252 = txdr_unsigned(VAL); \
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 { \
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 } \
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 { \
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; \
274 } while (0)
275
276 /* add a 64bit value to an mbuf chain */
277 #define nfsm_chain_add_64(E, NMC, VAL) \
278 do { \
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; \
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) \
291 *(((uint32_t*)(BUF))+((nfsm_rndup(LEN)>>2)-1)) = 0
292
293 /* add buffer of opaque data to an mbuf chain */
294 #define nfsm_chain_add_opaque(E, NMC, BUF, LEN) \
295 do { \
296 uint32_t rndlen = nfsm_rndup(LEN); \
297 if (E) break; \
298 if ((NMC)->nmc_left < rndlen) { \
299 (E) = nfsm_chain_add_opaque_f((NMC), (const u_char*)(BUF), (LEN)); \
300 break; \
301 } \
302 nfsm_chain_zero_opaque_pad((NMC)->nmc_ptr, (LEN)); \
303 bcopy((BUF), (NMC)->nmc_ptr, (LEN)); \
304 (NMC)->nmc_ptr += rndlen; \
305 (NMC)->nmc_left -= rndlen; \
306 } while (0)
307
308 /* add buffer of opaque data to an mbuf chain without padding */
309 #define nfsm_chain_add_opaque_nopad(E, NMC, BUF, LEN) \
310 do { \
311 if (E) break; \
312 if ((NMC)->nmc_left < (uint32_t) (LEN)) { \
313 (E) = nfsm_chain_add_opaque_nopad_f((NMC), (const u_char*)(BUF), (LEN)); \
314 break; \
315 } \
316 bcopy((BUF), (NMC)->nmc_ptr, (LEN)); \
317 (NMC)->nmc_ptr += (LEN); \
318 (NMC)->nmc_left -= (LEN); \
319 } while (0)
320
321 /* finish an mbuf in a chain to allow subsequent insertion */
322 #define nfsm_chain_finish_mbuf(E, NMC) \
323 do { \
324 if (E) break; \
325 mbuf_setlen((NMC)->nmc_mcur, \
326 (NMC)->nmc_ptr - (caddr_t)mbuf_data((NMC)->nmc_mcur)); \
327 (NMC)->nmc_left = 0; \
328 } while (0)
329
330 /* add a file handle to an mbuf chain */
331 #define nfsm_chain_add_fh(E, NMC, VERS, FHP, FHLEN) \
332 do { \
333 if (E) break; \
334 if ((VERS) != NFS_VER2) \
335 nfsm_chain_add_32((E), (NMC), (FHLEN)); \
336 nfsm_chain_add_opaque((E), (NMC), (FHP), (FHLEN)); \
337 } while (0)
338
339 /* add a string to an mbuf chain */
340 #define nfsm_chain_add_string(E, NMC, STR, LEN) \
341 do { \
342 nfsm_chain_add_32((E), (NMC), (LEN)); \
343 nfsm_chain_add_opaque((E), (NMC), (STR), (LEN)); \
344 } while (0)
345
346 /* add a name to an mbuf chain */
347 #define nfsm_chain_add_name(E, NMC, STR, LEN, NMP) \
348 do { \
349 if (E) break; \
350 if (NMFLAG((NMP), NFC)) \
351 (E) = nfsm_chain_add_string_nfc((NMC), (const uint8_t*)(STR), (LEN)); \
352 else \
353 nfsm_chain_add_string((E), (NMC), (STR), (LEN)); \
354 } while (0)
355
356 /* add an NFSv2 time to an mbuf chain */
357 #define nfsm_chain_add_v2time(E, NMC, TVP) \
358 do { \
359 if (TVP) { \
360 nfsm_chain_add_32((E), (NMC), (TVP)->tv_sec); \
361 nfsm_chain_add_32((E), (NMC), ((TVP)->tv_nsec != -1) ? \
362 ((uint32_t)(TVP)->tv_nsec / 1000) : 0xffffffff); \
363 } else { \
364 /* no time... use -1 */ \
365 nfsm_chain_add_32((E), (NMC), -1); \
366 nfsm_chain_add_32((E), (NMC), -1); \
367 } \
368 } while (0)
369
370 /* add an NFSv3 time to an mbuf chain */
371 #define nfsm_chain_add_v3time(E, NMC, TVP) \
372 do { \
373 nfsm_chain_add_32((E), (NMC), (TVP)->tv_sec); \
374 nfsm_chain_add_32((E), (NMC), (TVP)->tv_nsec); \
375 } while (0)
376
377 /* add an NFS v2 or v3 time to an mbuf chain */
378 #define nfsm_chain_add_time(E, NMC, VERS, TVP) \
379 do { \
380 if ((VERS) == NFS_VER2) { \
381 nfsm_chain_add_v2time((E), (NMC), (TVP)); \
382 } else { \
383 nfsm_chain_add_v3time((E), (NMC), (TVP)); \
384 } \
385 } while (0)
386
387 /* add an NFSv3 postop file handle to an mbuf chain */
388 #define nfsm_chain_add_postop_fh(E, NMC, FHP, FHLEN) \
389 do { \
390 nfsm_chain_add_32((E), (NMC), TRUE); \
391 nfsm_chain_add_fh((E), (NMC), NFS_VER3, (FHP), (FHLEN)); \
392 } while (0)
393
394 /* add NFSv3 postop attributes to an mbuf chain */
395 #define nfsm_chain_add_postop_attr(E, ND, NMC, ATTRERR, VAP) \
396 do { \
397 if (E) break; \
398 if (ATTRERR) { \
399 nfsm_chain_add_32((E), (NMC), FALSE); \
400 break; \
401 } \
402 nfsm_chain_add_32((E), (NMC), TRUE); \
403 if (E) break; \
404 (E) = nfsm_chain_add_fattr((ND), (NMC), (VAP)); \
405 } while (0)
406
407 /* Add an NFSv2 "sattr" structure to an mbuf chain */
408 #define nfsm_chain_add_v2sattr(E, NMC, VAP, SZRDEV) \
409 do { \
410 if (E) break; \
411 (E) = nfsm_chain_add_v2sattr_f((NMC), (VAP), (SZRDEV)); \
412 } while (0)
413
414 /* Add an NFSv3 "sattr" structure to an mbuf chain */
415 #define nfsm_chain_add_v3sattr(E, NMC, VAP) \
416 do { \
417 if (E) break; \
418 (E) = nfsm_chain_add_v3sattr_f((NMC), (VAP)); \
419 } while (0)
420
421 /* Add an NFSv4 "fattr" structure to an mbuf chain */
422 #define nfsm_chain_add_fattr4(E, NMC, VAP, NMP) \
423 do { \
424 if (E) break; \
425 (E) = nfsm_chain_add_fattr4_f((NMC), (VAP), (NMP)); \
426 } while (0)
427
428 /* add NFSv3 WCC data to an mbuf chain */
429 #define nfsm_chain_add_wcc_data(E, ND, NMC, PREERR, PREVAP, POSTERR, POSTVAP) \
430 do { \
431 if (E) break; \
432 (E) = nfsm_chain_add_wcc_data_f((ND), (NMC), \
433 (PREERR), (PREVAP), (POSTERR), (POSTVAP)); \
434 } while (0)
435
436 /* add NFSv4 COMPOUND header */
437 #define NFS4_TAG_LENGTH 12
438 #define nfsm_chain_add_compound_header(E, NMC, TAG, MINOR, NUMOPS) \
439 do { \
440 if ((TAG) && strlen(TAG)) { \
441 /* put tags into a fixed-length space-padded field */ \
442 char __nfstag[NFS4_TAG_LENGTH+1]; \
443 snprintf(__nfstag, sizeof(__nfstag), "%-*s", NFS4_TAG_LENGTH, (TAG)); \
444 nfsm_chain_add_32((E), (NMC), NFS4_TAG_LENGTH); \
445 nfsm_chain_add_opaque((E), (NMC), __nfstag, NFS4_TAG_LENGTH); \
446 } else { \
447 nfsm_chain_add_32((E), (NMC), 0); \
448 } \
449 nfsm_chain_add_32((E), (NMC), (MINOR)); /*minorversion*/ \
450 nfsm_chain_add_32((E), (NMC), (NUMOPS)); \
451 } while (0)
452
453 /* add NFSv4 attr bitmap */
454 #define nfsm_chain_add_bitmap(E, NMC, B, LEN) \
455 do { \
456 int __i; \
457 nfsm_chain_add_32((E), (NMC), (LEN)); \
458 for (__i=0; __i < (LEN); __i++) \
459 nfsm_chain_add_32((E), (NMC), (B)[__i]); \
460 } while (0)
461
462 /* add NFSv4 attr bitmap masked with the given mask */
463 #define nfsm_chain_add_bitmap_masked(E, NMC, B, LEN, MASK) \
464 do { \
465 int __i; \
466 nfsm_chain_add_32((E), (NMC), (LEN)); \
467 for (__i=0; __i < (LEN); __i++) \
468 nfsm_chain_add_32((E), (NMC), ((B)[__i] & (MASK)[__i])); \
469 } while (0)
470
471 /* add NFSv4 attr bitmap masked with the supported attributes for this mount/node */
472 #define nfsm_chain_add_bitmap_supported(E, NMC, B, NMP, NP) \
473 do { \
474 uint32_t __bitmap[NFS_ATTR_BITMAP_LEN], *__bmp = (B); \
475 int __nonamedattr = 0, __noacl = 0, __nomode = 0; \
476 if (!((NMP)->nm_fsattr.nfsa_flags & NFS_FSFLAG_NAMED_ATTR) || \
477 ((NP) && (((nfsnode_t)(NP))->n_flag & (NISDOTZFS|NISDOTZFSCHILD)))) \
478 __nonamedattr = 1; \
479 if (!((NMP)->nm_fsattr.nfsa_flags & NFS_FSFLAG_ACL)) \
480 __noacl = 1; \
481 if (NMFLAG((NMP), ACLONLY)) \
482 __nomode = 1; \
483 if (__nonamedattr || __noacl || __nomode) { \
484 /* don't ask for attrs we're not supporting */ \
485 /* some ".zfs" directories can't handle being asked for some attributes */ \
486 int __ii; \
487 NFS_CLEAR_ATTRIBUTES(__bitmap); \
488 for (__ii=0; __ii < NFS_ATTR_BITMAP_LEN; __ii++) \
489 __bitmap[__ii] = (B)[__ii]; \
490 if (__nonamedattr) \
491 NFS_BITMAP_CLR(__bitmap, NFS_FATTR_NAMED_ATTR); \
492 if (__noacl) \
493 NFS_BITMAP_CLR(__bitmap, NFS_FATTR_ACL); \
494 if (__nomode) \
495 NFS_BITMAP_CLR(__bitmap, NFS_FATTR_MODE); \
496 __bmp = __bitmap; \
497 } \
498 nfsm_chain_add_bitmap_masked((E), (NMC), __bmp, NFS_ATTR_BITMAP_LEN, (NMP)->nm_fsattr.nfsa_supp_attr); \
499 } while (0)
500
501 /* Add an NFSv4 "stateid" structure to an mbuf chain */
502 #define nfsm_chain_add_stateid(E, NMC, SID) \
503 do { \
504 nfsm_chain_add_32((E), (NMC), (SID)->seqid); \
505 nfsm_chain_add_32((E), (NMC), (SID)->other[0]); \
506 nfsm_chain_add_32((E), (NMC), (SID)->other[1]); \
507 nfsm_chain_add_32((E), (NMC), (SID)->other[2]); \
508 } while (0)
509
510 /* add an NFSv4 lock owner structure to an mbuf chain */
511 #define nfsm_chain_add_lock_owner4(E, NMC, NMP, NLOP) \
512 do { \
513 nfsm_chain_add_64((E), (NMC), (NMP)->nm_clientid); \
514 nfsm_chain_add_32((E), (NMC), 5*NFSX_UNSIGNED); \
515 nfsm_chain_add_32((E), (NMC), (NLOP)->nlo_name); \
516 nfsm_chain_add_32((E), (NMC), (NLOP)->nlo_pid); \
517 nfsm_chain_add_64((E), (NMC), (NLOP)->nlo_pid_start.tv_sec); \
518 nfsm_chain_add_32((E), (NMC), (NLOP)->nlo_pid_start.tv_usec); \
519 } while (0)
520
521 /*
522 * macros for dissecting NFS mbuf chains
523 */
524
525 /* prepare an mbuf chain for dissection starting with the given mbuf */
526 #define nfsm_chain_dissect_init(E, NMC, H) \
527 do { \
528 if (!(H)) { \
529 (E) = EINVAL; \
530 break; \
531 } \
532 (NMC)->nmc_mcur = (NMC)->nmc_mhead = (H); \
533 (NMC)->nmc_ptr = mbuf_data(H); \
534 (NMC)->nmc_left = mbuf_len(H); \
535 } while (0)
536
537 /* skip a number of bytes in an mbuf chain */
538 #define nfsm_chain_adv(E, NMC, LEN) \
539 do { \
540 if (E) break; \
541 if ((NMC)->nmc_left >= (uint32_t)(LEN)) { \
542 (NMC)->nmc_left -= (LEN); \
543 (NMC)->nmc_ptr += (LEN); \
544 } else { \
545 (E) = nfsm_chain_advance((NMC), (LEN)); \
546 } \
547 } while (0)
548
549 /* get a 32bit value from an mbuf chain */
550 #define nfsm_chain_get_32(E, NMC, LVAL) \
551 do { \
552 uint32_t __tmp32, *__tmpptr; \
553 if (E) break; \
554 if ((NMC)->nmc_left >= NFSX_UNSIGNED) { \
555 __tmpptr = (uint32_t*)(NMC)->nmc_ptr; \
556 (NMC)->nmc_left -= NFSX_UNSIGNED; \
557 (NMC)->nmc_ptr += NFSX_UNSIGNED; \
558 } else { \
559 __tmpptr = &__tmp32; \
560 (E) = nfsm_chain_get_opaque_f((NMC), NFSX_UNSIGNED, (u_char*)__tmpptr); \
561 if (E) break; \
562 } \
563 (LVAL) = fxdr_unsigned(uint32_t, *__tmpptr); \
564 } while (0)
565
566 /* get a 64bit value from an mbuf chain */
567 #define nfsm_chain_get_64(E, NMC, LVAL) \
568 do { \
569 uint64_t __tmp64, *__tmpptr; \
570 if (E) break; \
571 if ((NMC)->nmc_left >= 2 * NFSX_UNSIGNED) { \
572 __tmpptr = (uint64_t*)(NMC)->nmc_ptr; \
573 (NMC)->nmc_left -= 2 * NFSX_UNSIGNED; \
574 (NMC)->nmc_ptr += 2 * NFSX_UNSIGNED; \
575 } else { \
576 __tmpptr = &__tmp64; \
577 (E) = nfsm_chain_get_opaque_f((NMC), 2 * NFSX_UNSIGNED, (u_char*)__tmpptr); \
578 if (E) break; \
579 } \
580 fxdr_hyper(__tmpptr, &(LVAL)); \
581 } while (0)
582
583 /* get a pointer to the next consecutive bytes in an mbuf chain */
584 #define nfsm_chain_get_opaque_pointer(E, NMC, LEN, PTR) \
585 do { \
586 uint32_t rndlen; \
587 if (E) break; \
588 rndlen = nfsm_rndup(LEN); \
589 if ((NMC)->nmc_left >= rndlen) { \
590 (PTR) = (void*)(NMC)->nmc_ptr; \
591 (NMC)->nmc_left -= rndlen; \
592 (NMC)->nmc_ptr += rndlen; \
593 } else { \
594 (E) = nfsm_chain_get_opaque_pointer_f((NMC), (LEN), (u_char**)&(PTR)); \
595 } \
596 } while (0)
597
598 /* copy the next consecutive bytes of opaque data from an mbuf chain */
599 #define nfsm_chain_get_opaque(E, NMC, LEN, PTR) \
600 do { \
601 uint32_t rndlen; \
602 if (E) break; \
603 rndlen = nfsm_rndup(LEN); \
604 if ((NMC)->nmc_left >= rndlen) { \
605 u_char *__tmpptr = (u_char*)(NMC)->nmc_ptr; \
606 (NMC)->nmc_left -= rndlen; \
607 (NMC)->nmc_ptr += rndlen; \
608 bcopy(__tmpptr, (PTR), (LEN)); \
609 } else { \
610 (E) = nfsm_chain_get_opaque_f((NMC), (LEN), (u_char*)(PTR)); \
611 } \
612 } while (0)
613
614 /* get the size of and a pointer to a file handle in an mbuf chain */
615 #define nfsm_chain_get_fh_ptr(E, NMC, VERS, FHP, FHSIZE) \
616 do { \
617 if ((VERS) != NFS_VER2) \
618 nfsm_chain_get_32((E), (NMC), (FHSIZE)); \
619 else \
620 (FHSIZE) = NFSX_V2FH;\
621 nfsm_chain_get_opaque_pointer((E), (NMC), (FHSIZE), (FHP));\
622 } while (0)
623
624 /* get the size of and data for a file handle in an mbuf chain */
625 #define nfsm_chain_get_fh(E, NMC, VERS, FHP) \
626 do { \
627 if ((VERS) != NFS_VER2) \
628 nfsm_chain_get_32((E), (NMC), (FHP)->fh_len); \
629 else \
630 (FHP)->fh_len = NFSX_V2FH;\
631 nfsm_chain_get_opaque((E), (NMC), (uint32_t)(FHP)->fh_len, (FHP)->fh_data);\
632 if (E) \
633 (FHP)->fh_len = 0;\
634 } while (0)
635
636 /* get an NFS v2 or v3 time from an mbuf chain */
637 #define nfsm_chain_get_time(E, NMC, VERS, TSEC, TNSEC) \
638 do { \
639 nfsm_chain_get_32((E), (NMC), (TSEC)); \
640 nfsm_chain_get_32((E), (NMC), (TNSEC)); \
641 if ((E) || ((VERS) != NFS_VER2)) break; \
642 if ((uint32_t)(TNSEC) == 0xffffffff) \
643 (TNSEC) = 0; \
644 else \
645 (TNSEC) *= 1000; \
646 } while (0)
647
648 /* get postop attributes from an mbuf chain */
649 #define nfsm_chain_postop_attr_get(E, NMC, F, VAP) \
650 do { \
651 (F) = 0; \
652 if ((E) || !(NMC)->nmc_mhead) break; \
653 nfsm_chain_get_32((E), (NMC), (F)); \
654 if ((E) || !(F)) break; \
655 if (((E) = nfs_parsefattr((NMC), NFS_VER3, (VAP)))) \
656 (F) = 0; \
657 } while (0)
658
659 /* update a node's attribute cache with postop attributes from an mbuf chain */
660 /* (F returns whether the attributes were updated or not) */
661 #define nfsm_chain_postop_attr_update_flag(E, NMC, NP, F, X) \
662 do { \
663 struct nfs_vattr ttvattr; \
664 nfsm_chain_postop_attr_get((E), (NMC), (F), &ttvattr); \
665 if ((E) || !(F)) break; \
666 if (((E) = nfs_loadattrcache((NP), &ttvattr, (X), 1))) { \
667 (F) = 0; \
668 break; \
669 } \
670 if (*(X) == 0) \
671 (F) = 0; \
672 } while (0)
673
674 /* update a node's attribute cache with postop attributes from an mbuf chain */
675 #define nfsm_chain_postop_attr_update(E, NMC, NP, X) \
676 do { \
677 int __dummy_flag = 0; \
678 nfsm_chain_postop_attr_update_flag((E), (NMC), (NP), __dummy_flag, (X)); \
679 } while (0)
680
681 /* get and process NFSv3 WCC data from an mbuf chain */
682 #define nfsm_chain_get_wcc_data(E, NMC, NP, PREMTIME, NEWPOSTATTR, X) \
683 do { \
684 if (E) break; \
685 (E) = nfsm_chain_get_wcc_data_f((NMC), (NP), (PREMTIME), (NEWPOSTATTR), (X)); \
686 } while (0)
687
688 /* update a node's attribute cache with attributes from an mbuf chain */
689 #define nfsm_chain_loadattr(E, NMC, NP, VERS, X) \
690 do { \
691 struct nfs_vattr ttvattr; \
692 if (E) break; \
693 if ((VERS) == NFS_VER4) { \
694 (E) = nfs4_parsefattr((NMC), NULL, &ttvattr, NULL, NULL, NULL); \
695 } else { \
696 (E) = nfs_parsefattr((NMC), (VERS), &ttvattr); \
697 } \
698 if (!(E) && (NP)) \
699 (E) = nfs_loadattrcache((NP), &ttvattr, (X), 0); \
700 NVATTR_CLEANUP(&ttvattr); \
701 } while (0)
702
703 /* get NFSv4 attr bitmap */
704 #define nfsm_chain_get_bitmap(E, NMC, B, LEN) \
705 do { \
706 uint32_t __len = 0, __i; \
707 nfsm_chain_get_32((E), (NMC), __len); \
708 if (E) break; \
709 for (__i=0; __i < MIN(__len, (LEN)); __i++) \
710 nfsm_chain_get_32((E), (NMC), (B)[__i]); \
711 if (E) break; \
712 for (; __i < __len; __i++) \
713 nfsm_chain_adv((E), (NMC), NFSX_UNSIGNED); \
714 for (; __i < (LEN); __i++) \
715 (B)[__i] = 0; \
716 (LEN) = __len; \
717 } while (0)
718
719 /* get an NFSv4 "stateid" structure from an mbuf chain */
720 #define nfsm_chain_get_stateid(E, NMC, SID) \
721 do { \
722 nfsm_chain_get_32((E), (NMC), (SID)->seqid); \
723 nfsm_chain_get_32((E), (NMC), (SID)->other[0]); \
724 nfsm_chain_get_32((E), (NMC), (SID)->other[1]); \
725 nfsm_chain_get_32((E), (NMC), (SID)->other[2]); \
726 } while (0)
727
728 #define nfsm_chain_skip_tag(E, NMC) \
729 do { \
730 uint32_t __val = 0; \
731 nfsm_chain_get_32((E), (NMC), __val); \
732 nfsm_chain_adv((E), (NMC), nfsm_rndup(__val)); \
733 } while (0)
734
735 #define nfsm_chain_op_check(E, NMC, OP) \
736 do { \
737 uint32_t __val = 0; \
738 nfsm_chain_get_32((E), (NMC), __val); \
739 /* [sigh] some implementations return the "illegal" op for unsupported ops */ \
740 nfsm_assert((E), ((__val == (OP)) || (__val == NFS_OP_ILLEGAL)), EBADRPC); \
741 nfsm_chain_get_32((E), (NMC), __val); \
742 nfsm_assert((E), (__val == NFS_OK), __val); \
743 } while (0)
744
745 #define nfsm_chain_check_change_info(E, NMC, DNP) \
746 do { \
747 uint64_t __ci_before, __ci_after; \
748 uint32_t __ci_atomic = 0; \
749 nfsm_chain_get_32((E), (NMC), __ci_atomic); \
750 nfsm_chain_get_64((E), (NMC), __ci_before); \
751 nfsm_chain_get_64((E), (NMC), __ci_after); \
752 if ((E) || !(DNP)) break; \
753 if (__ci_atomic && (__ci_before == (DNP)->n_ncchange)) { \
754 (DNP)->n_ncchange = __ci_after; \
755 } else { \
756 cache_purge(NFSTOV(DNP)); \
757 (DNP)->n_ncgen++; \
758 } \
759 } while (0)
760
761 #endif /* __APPLE_API_PRIVATE */
762 #endif /* _NFS_NFSM_SUBS_H_ */