#ifndef _NFS_NFSM_SUBS_H_
#define _NFS_NFSM_SUBS_H_
#include <sys/appleapiopts.h>
#ifdef __APPLE_API_PRIVATE
int nfsm_rpchead(struct nfsreq *, int, mbuf_t, u_int64_t *, mbuf_t *);
int nfsm_rpchead2(int, int, int, int, int, int, kauth_cred_t, struct nfsreq *, mbuf_t, u_int64_t *, mbuf_t *);
int nfsm_chain_new_mbuf(struct nfsm_chain *, size_t);
int nfsm_chain_add_opaque_f(struct nfsm_chain *, const u_char *, uint32_t);
int nfsm_chain_add_opaque_nopad_f(struct nfsm_chain *, const u_char *, uint32_t);
int nfsm_chain_add_uio(struct nfsm_chain *, struct uio *, uint32_t);
int nfsm_chain_add_fattr4_f(struct nfsm_chain *, struct vnode_attr *, struct nfsmount *);
int nfsm_chain_add_v2sattr_f(struct nfsm_chain *, struct vnode_attr *, uint32_t);
int nfsm_chain_add_v3sattr_f(struct nfsm_chain *, struct vnode_attr *);
int nfsm_chain_advance(struct nfsm_chain *, uint32_t);
int nfsm_chain_offset(struct nfsm_chain *);
int nfsm_chain_reverse(struct nfsm_chain *, uint32_t);
int nfsm_chain_get_opaque_pointer_f(struct nfsm_chain *, uint32_t, u_char **);
int nfsm_chain_get_opaque_f(struct nfsm_chain *, uint32_t, u_char *);
int nfsm_chain_get_uio(struct nfsm_chain *, uint32_t, struct uio *);
int nfsm_chain_get_fh_attr(struct nfsm_chain *, nfsnode_t,
vfs_context_t, int, uint64_t *, fhandle_t *, struct nfs_vattr *);
int nfsm_chain_get_wcc_data_f(struct nfsm_chain *, nfsnode_t, struct timespec *, int *, u_int64_t *);
#if NFSSERVER
void nfsm_adj(mbuf_t, int, int);
int nfsm_mbuf_get_list(size_t, mbuf_t *, int *);
int nfsm_chain_add_fattr(struct nfsrv_descript *, struct nfsm_chain *, struct vnode_attr *);
int nfsm_chain_add_wcc_data_f(struct nfsrv_descript *, struct nfsm_chain *, int,
struct vnode_attr *, int, struct vnode_attr *);
int nfsm_chain_get_path_namei(struct nfsm_chain *, uint32_t, struct nameidata *);
int nfsm_chain_get_sattr(struct nfsrv_descript *, struct nfsm_chain *, struct vnode_attr *);
int nfsm_chain_trim_data(struct nfsm_chain *, int, int *);
#endif
#define nfsm_name_len_check(E, ND, LEN) \
do { \
if (E) break; \
if (((ND)->nd_vers == NFS_VER2) && ((LEN) > NFS_MAXNAMLEN)) \
(E) = NFSERR_NAMETOL; \
if ((LEN) <= 0) \
error = EBADRPC; \
} while (0)
#define nfsm_assert(E, COND, ERR) \
do { \
if (E) break; \
if (!(COND)) \
(E) = (ERR); \
} while (0)
#define nfsm_srv_vattr_init(VAP, VERS) \
do { \
VATTR_INIT(VAP); \
VATTR_WANTED((VAP), va_type); \
VATTR_WANTED((VAP), va_mode); \
VATTR_WANTED((VAP), va_nlink); \
VATTR_WANTED((VAP), va_uid); \
VATTR_WANTED((VAP), va_gid); \
VATTR_WANTED((VAP), va_data_size); \
VATTR_WANTED((VAP), va_data_alloc); \
VATTR_WANTED((VAP), va_rdev); \
VATTR_WANTED((VAP), va_fsid); \
VATTR_WANTED((VAP), va_fileid); \
VATTR_WANTED((VAP), va_access_time); \
VATTR_WANTED((VAP), va_modify_time); \
VATTR_WANTED((VAP), va_change_time); \
if ((VERS) == NFS_VER2) \
VATTR_WANTED((VAP), va_iosize); \
} while (0)
#define nfsm_srv_pre_vattr_init(VAP) \
do { \
VATTR_INIT(VAP); \
VATTR_WANTED((VAP), va_data_size); \
VATTR_WANTED((VAP), va_modify_time); \
VATTR_WANTED((VAP), va_change_time); \
} while (0)
#define nfsm_rndup(a) (((a)+3)&(~0x3))
#define nfsm_pad(a) (nfsm_rndup(a) - (a))
#define nfsmout_if(E) do { if (E) goto nfsmout; } while (0)
#define nfsmerr_if(E) do { if (E) goto nfsmerr; } while (0)
#define nfsmout_on_status(ND, E) \
do { \
if (((ND)->nd_repstat == EBADRPC) || \
((ND)->nd_repstat && ((ND)->nd_vers == NFS_VER2))) { \
(E) = 0; \
goto nfsmout; \
} \
} while (0)
#define nfsm_chain_null(NMC) \
do { \
(NMC)->nmc_mhead = (NMC)->nmc_mcur = NULL; \
(NMC)->nmc_ptr = NULL; \
} while (0)
#define nfsm_chain_cleanup(NMC) \
do { \
if (!(NMC)->nmc_mhead) break; \
mbuf_freem((NMC)->nmc_mhead); \
nfsm_chain_null(NMC); \
} while (0)
#define nfsm_mbuf_get(E, MBP, SIZEHINT) \
do { \
*(MBP) = NULL; \
if ((size_t)(SIZEHINT) >= nfs_mbuf_minclsize) \
(E) = mbuf_mclget(MBUF_WAITOK, MBUF_TYPE_DATA, (MBP)); \
else \
(E) = mbuf_get(MBUF_WAITOK, MBUF_TYPE_DATA, (MBP)); \
} while (0)
#define nfsm_chain_init(NMC, MB) \
do { \
(NMC)->nmc_mhead = (MB); \
(NMC)->nmc_mcur = (NMC)->nmc_mhead; \
(NMC)->nmc_ptr = mbuf_data((NMC)->nmc_mcur); \
(NMC)->nmc_left = mbuf_trailingspace((NMC)->nmc_mcur); \
(NMC)->nmc_flags = 0; \
} while (0)
#define nfsm_chain_build_alloc_init(E, NMC, SIZEHINT) \
do { \
mbuf_t ncbimb; \
nfsm_mbuf_get((E), &ncbimb, (SIZEHINT)); \
if (E) break; \
nfsm_chain_init((NMC), ncbimb); \
} while (0)
#define nfsm_chain_build_done(E, NMC) \
do { \
if ((E) || !(NMC)->nmc_mcur) break; \
\
mbuf_setlen((NMC)->nmc_mcur, \
(NMC)->nmc_ptr - (caddr_t)mbuf_data((NMC)->nmc_mcur)); \
} while (0)
#define nfsm_chain_set_recmark(E, NMC, VAL) \
do { \
if (E) break; \
*((uint32_t*)mbuf_data((NMC)->nmc_mhead)) \
= txdr_unsigned(VAL); \
} while (0)
#define nfsm_chain_check_size(E, NMC, SIZE) \
do { \
if (E) break; \
if ((NMC)->nmc_left < (SIZE)) { \
(E) = nfsm_chain_new_mbuf((NMC), (SIZE)); \
if (!(E) && ((NMC)->nmc_left < (SIZE))) \
(E) = ENOMEM; \
} \
} while (0)
#define nfsm_chain_add_32(E, NMC, VAL) \
do { \
nfsm_chain_check_size((E), (NMC), NFSX_UNSIGNED); \
if (E) break; \
*((uint32_t*)(NMC)->nmc_ptr) = txdr_unsigned(VAL); \
(NMC)->nmc_ptr += NFSX_UNSIGNED; \
(NMC)->nmc_left -= NFSX_UNSIGNED; \
} while (0)
#define nfsm_chain_add_64(E, NMC, VAL) \
do { \
uint64_t __tmp64; \
nfsm_chain_check_size((E), (NMC), 2 * NFSX_UNSIGNED); \
if (E) break; \
__tmp64 = (VAL); \
txdr_hyper(&__tmp64, (NMC)->nmc_ptr); \
(NMC)->nmc_ptr += 2 * NFSX_UNSIGNED; \
(NMC)->nmc_left -= 2 * NFSX_UNSIGNED; \
} while (0)
#define nfsm_chain_zero_opaque_pad(BUF, LEN) \
*(((uint32_t*)(BUF))+((nfsm_rndup(LEN)>>2)-1)) = 0
#define nfsm_chain_add_opaque(E, NMC, BUF, LEN) \
do { \
uint32_t rndlen = nfsm_rndup(LEN); \
if (E) break; \
if ((NMC)->nmc_left < rndlen) { \
(E) = nfsm_chain_add_opaque_f((NMC), (const u_char*)(BUF), (LEN)); \
break; \
} \
nfsm_chain_zero_opaque_pad((NMC)->nmc_ptr, (LEN)); \
bcopy((BUF), (NMC)->nmc_ptr, (LEN)); \
(NMC)->nmc_ptr += rndlen; \
(NMC)->nmc_left -= rndlen; \
} while (0)
#define nfsm_chain_add_opaque_nopad(E, NMC, BUF, LEN) \
do { \
if (E) break; \
if ((NMC)->nmc_left < (uint32_t) (LEN)) { \
(E) = nfsm_chain_add_opaque_nopad_f((NMC), (const u_char*)(BUF), (LEN)); \
break; \
} \
bcopy((BUF), (NMC)->nmc_ptr, (LEN)); \
(NMC)->nmc_ptr += (LEN); \
(NMC)->nmc_left -= (LEN); \
} while (0)
#define nfsm_chain_finish_mbuf(E, NMC) \
do { \
if (E) break; \
mbuf_setlen((NMC)->nmc_mcur, \
(NMC)->nmc_ptr - (caddr_t)mbuf_data((NMC)->nmc_mcur)); \
(NMC)->nmc_left = 0; \
} while (0)
#define nfsm_chain_add_fh(E, NMC, VERS, FHP, FHLEN) \
do { \
if (E) break; \
if ((VERS) != NFS_VER2) \
nfsm_chain_add_32((E), (NMC), (FHLEN)); \
nfsm_chain_add_opaque((E), (NMC), (FHP), (FHLEN)); \
} while (0)
#define nfsm_chain_add_string(E, NMC, STR, LEN) \
do { \
nfsm_chain_add_32((E), (NMC), (LEN)); \
nfsm_chain_add_opaque((E), (NMC), (STR), (LEN)); \
} while (0)
#define nfsm_chain_add_v2time(E, NMC, TVP) \
do { \
if (TVP) { \
nfsm_chain_add_32((E), (NMC), (TVP)->tv_sec); \
nfsm_chain_add_32((E), (NMC), ((TVP)->tv_nsec != -1) ? \
((uint32_t)(TVP)->tv_nsec / 1000) : 0xffffffff); \
} else { \
\
nfsm_chain_add_32((E), (NMC), -1); \
nfsm_chain_add_32((E), (NMC), -1); \
} \
} while (0)
#define nfsm_chain_add_v3time(E, NMC, TVP) \
do { \
nfsm_chain_add_32((E), (NMC), (TVP)->tv_sec); \
nfsm_chain_add_32((E), (NMC), (TVP)->tv_nsec); \
} while (0)
#define nfsm_chain_add_time(E, NMC, VERS, TVP) \
do { \
if ((VERS) == NFS_VER2) { \
nfsm_chain_add_v2time((E), (NMC), (TVP)); \
} else { \
nfsm_chain_add_v3time((E), (NMC), (TVP)); \
} \
} while (0)
#define nfsm_chain_add_postop_fh(E, NMC, FHP, FHLEN) \
do { \
nfsm_chain_add_32((E), (NMC), TRUE); \
nfsm_chain_add_fh((E), (NMC), NFS_VER3, (FHP), (FHLEN)); \
} while (0)
#define nfsm_chain_add_postop_attr(E, ND, NMC, ATTRERR, VAP) \
do { \
if (E) break; \
if (ATTRERR) { \
nfsm_chain_add_32((E), (NMC), FALSE); \
break; \
} \
nfsm_chain_add_32((E), (NMC), TRUE); \
if (E) break; \
(E) = nfsm_chain_add_fattr((ND), (NMC), (VAP)); \
} while (0)
#define nfsm_chain_add_v2sattr(E, NMC, VAP, SZRDEV) \
do { \
if (E) break; \
(E) = nfsm_chain_add_v2sattr_f((NMC), (VAP), (SZRDEV)); \
} while (0)
#define nfsm_chain_add_v3sattr(E, NMC, VAP) \
do { \
if (E) break; \
(E) = nfsm_chain_add_v3sattr_f((NMC), (VAP)); \
} while (0)
#define nfsm_chain_add_fattr4(E, NMC, VAP, NMP) \
do { \
if (E) break; \
(E) = nfsm_chain_add_fattr4_f((NMC), (VAP), (NMP)); \
} while (0)
#define nfsm_chain_add_wcc_data(E, ND, NMC, PREERR, PREVAP, POSTERR, POSTVAP) \
do { \
if (E) break; \
(E) = nfsm_chain_add_wcc_data_f((ND), (NMC), \
(PREERR), (PREVAP), (POSTERR), (POSTVAP)); \
} while (0)
#define nfsm_chain_add_compound_header(E, NMC, TAG, NUMOPS) \
do { \
if ((TAG) && strlen(TAG)) \
nfsm_chain_add_string((E), (NMC), (TAG), strlen(TAG)); \
else \
nfsm_chain_add_32((E), (NMC), 0); \
nfsm_chain_add_32((E), (NMC), 0); \
nfsm_chain_add_32((E), (NMC), (NUMOPS)); \
} while (0)
#define nfsm_chain_add_bitmap(E, NMC, B, LEN) \
do { \
int __i; \
nfsm_chain_add_32((E), (NMC), (LEN)); \
for (__i=0; __i < (LEN); __i++) \
nfsm_chain_add_32((E), (NMC), (B)[__i]); \
} while (0)
#define nfsm_chain_add_bitmap_masked(E, NMC, B, LEN, MASK) \
do { \
int __i; \
nfsm_chain_add_32((E), (NMC), (LEN)); \
for (__i=0; __i < (LEN); __i++) \
nfsm_chain_add_32((E), (NMC), ((B)[__i] & (MASK)[__i])); \
} while (0)
#define nfsm_chain_dissect_init(E, NMC, H) \
do { \
if (!(H)) { \
(E) = EINVAL; \
break; \
} \
(NMC)->nmc_mcur = (NMC)->nmc_mhead = (H); \
(NMC)->nmc_ptr = mbuf_data(H); \
(NMC)->nmc_left = mbuf_len(H); \
} while (0)
#define nfsm_chain_adv(E, NMC, LEN) \
do { \
if (E) break; \
if ((NMC)->nmc_left >= (uint32_t)(LEN)) { \
(NMC)->nmc_left -= (LEN); \
(NMC)->nmc_ptr += (LEN); \
} else { \
(E) = nfsm_chain_advance((NMC), (LEN)); \
} \
} while (0)
#define nfsm_chain_get_32(E, NMC, LVAL) \
do { \
uint32_t __tmp32, *__tmpptr; \
if (E) break; \
if ((NMC)->nmc_left >= NFSX_UNSIGNED) { \
__tmpptr = (uint32_t*)(NMC)->nmc_ptr; \
(NMC)->nmc_left -= NFSX_UNSIGNED; \
(NMC)->nmc_ptr += NFSX_UNSIGNED; \
} else { \
__tmpptr = &__tmp32; \
(E) = nfsm_chain_get_opaque_f((NMC), NFSX_UNSIGNED, (u_char*)__tmpptr); \
if (E) break; \
} \
(LVAL) = fxdr_unsigned(uint32_t, *__tmpptr); \
} while (0)
#define nfsm_chain_get_64(E, NMC, LVAL) \
do { \
uint64_t __tmp64, *__tmpptr; \
if (E) break; \
if ((NMC)->nmc_left >= 2 * NFSX_UNSIGNED) { \
__tmpptr = (uint64_t*)(NMC)->nmc_ptr; \
(NMC)->nmc_left -= 2 * NFSX_UNSIGNED; \
(NMC)->nmc_ptr += 2 * NFSX_UNSIGNED; \
} else { \
__tmpptr = &__tmp64; \
(E) = nfsm_chain_get_opaque_f((NMC), 2 * NFSX_UNSIGNED, (u_char*)__tmpptr); \
if (E) break; \
} \
fxdr_hyper(__tmpptr, &(LVAL)); \
} while (0)
#define nfsm_chain_get_opaque_pointer(E, NMC, LEN, PTR) \
do { \
if (E) break; \
if ((NMC)->nmc_left >= (uint32_t)(LEN)) { \
(PTR) = (void*)(NMC)->nmc_ptr; \
(NMC)->nmc_left -= nfsm_rndup(LEN); \
(NMC)->nmc_ptr += nfsm_rndup(LEN); \
} else { \
(E) = nfsm_chain_get_opaque_pointer_f((NMC), (LEN), (u_char**)&(PTR)); \
} \
} while (0)
#define nfsm_chain_get_opaque(E, NMC, LEN, PTR) \
do { \
if (E) break; \
if ((NMC)->nmc_left >= (LEN)) { \
u_char *__tmpptr = (u_char*)(NMC)->nmc_ptr; \
(NMC)->nmc_left -= nfsm_rndup(LEN); \
(NMC)->nmc_ptr += nfsm_rndup(LEN); \
bcopy(__tmpptr, (PTR), (LEN)); \
} else { \
(E) = nfsm_chain_get_opaque_f((NMC), (LEN), (u_char*)(PTR)); \
} \
} while (0)
#define nfsm_chain_get_fh_ptr(E, NMC, VERS, FHP, FHSIZE) \
do { \
if ((VERS) != NFS_VER2) \
nfsm_chain_get_32((E), (NMC), (FHSIZE)); \
else \
(FHSIZE) = NFSX_V2FH;\
nfsm_chain_get_opaque_pointer((E), (NMC), (FHSIZE), (FHP));\
} while (0)
#define nfsm_chain_get_fh(E, NMC, VERS, FHP) \
do { \
if ((VERS) != NFS_VER2) \
nfsm_chain_get_32((E), (NMC), (FHP)->fh_len); \
else \
(FHP)->fh_len = NFSX_V2FH;\
nfsm_chain_get_opaque((E), (NMC), (uint32_t)(FHP)->fh_len, (FHP)->fh_data);\
if (E) \
(FHP)->fh_len = 0;\
} while (0)
#define nfsm_chain_get_time(E, NMC, VERS, TSEC, TNSEC) \
do { \
nfsm_chain_get_32((E), (NMC), (TSEC)); \
nfsm_chain_get_32((E), (NMC), (TNSEC)); \
if ((E) || ((VERS) != NFS_VER2)) break; \
if ((uint32_t)(TNSEC) == 0xffffffff) \
(TNSEC) = 0; \
else \
(TNSEC) *= 1000; \
} while (0)
#define nfsm_chain_postop_attr_get(E, NMC, F, VAP) \
do { \
(F) = 0; \
if ((E) || !(NMC)->nmc_mhead) break; \
nfsm_chain_get_32((E), (NMC), (F)); \
if ((E) || !(F)) break; \
if (((E) = nfs_parsefattr((NMC), NFS_VER3, (VAP)))) \
(F) = 0; \
} while (0)
#define nfsm_chain_postop_attr_update_flag(E, NMC, NP, F, X) \
do { \
struct nfs_vattr ttvattr; \
nfsm_chain_postop_attr_get((E), (NMC), (F), &ttvattr); \
if ((E) || !(F)) break; \
if (((E) = nfs_loadattrcache((NP), &ttvattr, (X), 1))) { \
(F) = 0; \
break; \
} \
if (*(X) == 0) \
(F) = 0; \
} while (0)
#define nfsm_chain_postop_attr_update(E, NMC, NP, X) \
do { \
int __dummy_flag = 0; \
nfsm_chain_postop_attr_update_flag((E), (NMC), (NP), __dummy_flag, (X)); \
} while (0)
#define nfsm_chain_get_wcc_data(E, NMC, NP, PREMTIME, NEWPOSTATTR, X) \
do { \
if (E) break; \
(E) = nfsm_chain_get_wcc_data_f((NMC), (NP), (PREMTIME), (NEWPOSTATTR), (X)); \
} while (0)
#define nfsm_chain_loadattr(E, NMC, NP, VERS, A, X) \
do { \
struct nfs_vattr ttvattr, *ttnvap; \
if (E) break; \
ttnvap = (A) ? (A) : &ttvattr; \
if ((VERS) == NFS_VER4) { \
NFS_CLEAR_ATTRIBUTES(ttnvap->nva_bitmap); \
(E) = nfs4_parsefattr((NMC), NULL, ttnvap, NULL, NULL); \
} else { \
(E) = nfs_parsefattr((NMC), (VERS), ttnvap); \
} \
if (E) break; \
(E) = nfs_loadattrcache((NP), ttnvap, (X), 0); \
} while (0)
#define nfsm_chain_get_bitmap(E, NMC, B, LEN) \
do { \
uint32_t __len, __i; \
nfsm_chain_get_32((E), (NMC), __len); \
if (E) break; \
for (__i=0; __i < MIN(__len, (LEN)); __i++) \
nfsm_chain_get_32((E), (NMC), (B)[__i]); \
if (E) break; \
for (; __i < (LEN); __i++) \
(B)[__i] = 0; \
(LEN) = __len; \
} while (0)
#define nfsm_chain_skip_tag(E, NMC) \
do { \
uint32_t __val; \
nfsm_chain_get_32((E), (NMC), __val); \
nfsm_chain_adv((E), (NMC), nfsm_rndup(__val)); \
} while (0)
#define nfsm_chain_op_check(E, NMC, OP) \
do { \
uint32_t __val = 0; \
nfsm_chain_get_32((E), (NMC), __val); \
nfsm_assert((E), (__val == (OP)), EBADRPC); \
nfsm_chain_get_32((E), (NMC), __val); \
nfsm_assert((E), (__val == NFS_OK), __val); \
} while (0)
#define nfsm_chain_check_change_info(E, NMC, DNP) \
do { \
uint64_t __ci_before, __ci_after; \
uint32_t __ci_atomic = 0; \
nfsm_chain_get_32((E), (NMC), __ci_atomic); \
nfsm_chain_get_64((E), (NMC), __ci_before); \
nfsm_chain_get_64((E), (NMC), __ci_after); \
if (E) break; \
if (__ci_atomic && (__ci_before == (DNP)->n_ncchange)) \
(DNP)->n_ncchange = __ci_after; \
else \
cache_purge(NFSTOV(DNP)); \
} while (0)
#endif
#endif