]> git.saurik.com Git - apple/xnu.git/blobdiff - bsd/nfs/nfsm_subs.h
xnu-4570.61.1.tar.gz
[apple/xnu.git] / bsd / nfs / nfsm_subs.h
index cc1ac71b2ddecead02d11ba9dd3dfb4dd3032858..50f72c5fc6b99005679327f5f8ec3de7342f632f 100644 (file)
@@ -1,23 +1,29 @@
 /*
 /*
- * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
+ * Copyright (c) 2000-2011 Apple Inc. All rights reserved.
  *
  *
- * @APPLE_LICENSE_HEADER_START@
+ * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
  * 
  * 
- * The contents of this file constitute Original Code as defined in and
- * are subject to the Apple Public Source License Version 1.1 (the
- * "License").  You may not use this file except in compliance with the
- * License.  Please obtain a copy of the License at
- * http://www.apple.com/publicsource and read it before using this file.
+ * This file contains Original Code and/or Modifications of Original Code
+ * as defined in and that are subject to the Apple Public Source License
+ * Version 2.0 (the 'License'). You may not use this file except in
+ * compliance with the License. The rights granted to you under the License
+ * may not be used to create, or enable the creation or redistribution of,
+ * unlawful or unlicensed copies of an Apple operating system, or to
+ * circumvent, violate, or enable the circumvention or violation of, any
+ * terms of an Apple operating system software license agreement.
  * 
  * 
- * This Original Code and all software distributed under the License are
- * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
+ * Please obtain a copy of the License at
+ * http://www.opensource.apple.com/apsl/ and read it before using this file.
+ * 
+ * The Original Code and all software distributed under the License are
+ * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT.  Please see the
- * License for the specific language governing rights and limitations
- * under the License.
+ * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
+ * Please see the License for the specific language governing rights and
+ * limitations under the License.
  * 
  * 
- * @APPLE_LICENSE_HEADER_END@
+ * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
  */
 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
 /*
  */
 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
 /*
 #include <sys/appleapiopts.h>
 
 #ifdef __APPLE_API_PRIVATE
 #include <sys/appleapiopts.h>
 
 #ifdef __APPLE_API_PRIVATE
+
+int nfsm_rpchead(struct nfsreq *, mbuf_t, u_int64_t *, mbuf_t *);
+int nfsm_rpchead2(struct nfsmount *, 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 *, uio_t, 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_add_string_nfc(struct nfsm_chain *, const uint8_t *, uint32_t);
+
+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, uio_t);
+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 *);
+int nfsm_chain_get_secinfo(struct nfsm_chain *, uint32_t *, int *);
+
+#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 /* NFSSERVER */
+
+/* check name length */
+#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)
+
+/* Initialize a vnode_attr to retrieve attributes for the NFS server. */
+#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); \
+               if ((VERS) == NFS_VER3) \
+                       VATTR_WANTED((VAP), va_filerev); \
+       } while (0)
+
+/* Initialize a vnode_attr to retrieve pre-operation attributes for the NFS server. */
+#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)
+
+/* round up to a multiple of 4 */
+#define nfsm_rndup(a)  (((a)+3)&(~0x3))
+
+#define nfsm_pad(a)    (nfsm_rndup(a) - (a))
+
 /*
 /*
- * These macros do strange and peculiar things to mbuf chains for
- * the assistance of the nfs code. To attempt to use them for any
- * other purpose will be dangerous. (they make weird assumptions)
+ * control flow macros:
+ * go to the appropriate label on condition
  */
  */
+#define nfsmout_if(E)  do { if (E) goto nfsmout; } while (0)
+#define nfsmerr_if(E)  do { if (E) goto nfsmerr; } while (0)
 
 /*
 
 /*
- * First define what the actual subs. return
+ * For NFS v2 errors and EBADRPC, the reply contains only the error.
+ * This macro is used to skip any reply building code and go straight
+ * to nfsmout instead.
  */
  */
-struct mbuf *nfsm_reqh __P((struct vnode *vp, u_long procid, int hsiz,
-                           caddr_t *bposp));
-struct mbuf *nfsm_rpchead __P((struct ucred *cr, int nmflag, int procid,
-                              int auth_type, int auth_len, char *auth_str,
-                              int verf_len, char *verf_str,
-                              struct mbuf *mrest, int mrest_len,
-                              struct mbuf **mbp, u_long *xidp));
-
-#define        M_HASCL(m)      ((m)->m_flags & M_EXT)
-#define        NFSMINOFF(m) \
-               if (M_HASCL(m)) \
-                       (m)->m_data = (m)->m_ext.ext_buf; \
-               else if ((m)->m_flags & M_PKTHDR) \
-                       (m)->m_data = (m)->m_pktdat; \
+#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)
+
+/* initialize an mbuf chain */
+#define nfsm_chain_null(NMC) \
+       do { \
+               (NMC)->nmc_mhead = (NMC)->nmc_mcur = NULL; \
+               (NMC)->nmc_ptr = NULL; \
+       } while (0)
+
+/* cleanup an mbuf chain */
+#define nfsm_chain_cleanup(NMC) \
+       do { \
+               if (!(NMC)->nmc_mhead) break; \
+               mbuf_freem((NMC)->nmc_mhead); \
+               nfsm_chain_null(NMC); \
+       } while (0)
+
+/* get an mbuf given a size hint */
+#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 \
                else \
-                       (m)->m_data = (m)->m_dat
-#define        NFSMADV(m, s)   (m)->m_data += (s)
-#define        NFSMSIZ(m)      ((M_HASCL(m))?MCLBYTES: \
-                               (((m)->m_flags & M_PKTHDR)?MHLEN:MLEN))
+                       (E) = mbuf_get(MBUF_WAITOK, MBUF_TYPE_DATA, (MBP)); \
+       } while (0)
+
 
 /*
 
 /*
- * Now for the macros that do the simple stuff and call the functions
- * for the hard stuff.
- * These macros use several vars. declared in nfsm_reqhead and these
- * vars. must not be used elsewhere unless you are careful not to corrupt
- * them. The vars. starting with pN and tN (N=1,2,3,..) are temporaries
- * that may be used so long as the value is not expected to retained
- * after a macro.
- * I know, this is kind of dorkey, but it makes the actual op functions
- * fairly clean and deals with the mess caused by the xdr discriminating
- * unions.
+ * macros for building NFS mbuf chains
  */
 
  */
 
-#define        nfsm_build(a,c,s) \
-               { if ((s) > M_TRAILINGSPACE(mb)) { \
-                       MGET(mb2, M_WAIT, MT_DATA); \
-                       if ((s) > MLEN) \
-                               panic("build > MLEN"); \
-                       mb->m_next = mb2; \
-                       mb = mb2; \
-                       mb->m_len = 0; \
-                       bpos = mtod(mb, caddr_t); \
+/* prepare an mbuf chain for building starting with the given mbuf */
+#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)
+
+/* prepare an mbuf chain for building starting with a newly allocated mbuf */
+#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)
+
+/* done building an mbuf chain */
+#define nfsm_chain_build_done(E, NMC) \
+       do { \
+               if ((E) || !(NMC)->nmc_mcur) break; \
+               /* cap off current mbuf */ \
+               mbuf_setlen((NMC)->nmc_mcur, \
+                       (NMC)->nmc_ptr - (caddr_t)mbuf_data((NMC)->nmc_mcur)); \
+       } while (0)
+
+/*
+ *  set the TCP record mark at the head of an mbuf chain -
+ *  assumes 4 bytes are already allocated in the head mbuf
+ */
+#define nfsm_chain_set_recmark(E, NMC, VAL) \
+       do { \
+               if (E) break; \
+               *((uint32_t*)mbuf_data((NMC)->nmc_mhead)) \
+                       = txdr_unsigned(VAL); \
+       } while (0)
+
+/* make sure there's room for size bytes in current mbuf */
+#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; \
                } \
                } \
-               (a) = (c)(bpos); \
-               mb->m_len += (s); \
-               bpos += (s); }
-
-#define        nfsm_dissect(a, c, s) \
-               { t1 = mtod(md, caddr_t)+md->m_len-dpos; \
-               if (t1 >= (s)) { \
-                       (a) = (c)(dpos); \
-                       dpos += (s); \
-               } else if ((t1 = nfsm_disct(&md, &dpos, (s), t1, &cp2))) { \
-                       error = t1; \
-                       m_freem(mrep); \
-                       goto nfsmout; \
-               } else { \
-                       (a) = (c)cp2; \
-               } }
-
-#define nfsm_fhtom(v, v3) \
-             { if (v3) { \
-                       t2 = nfsm_rndup(VTONFS(v)->n_fhsize) + NFSX_UNSIGNED; \
-                       if (t2 <= M_TRAILINGSPACE(mb)) { \
-                               nfsm_build(tl, u_long *, t2); \
-                               *tl++ = txdr_unsigned(VTONFS(v)->n_fhsize); \
-                               *(tl + ((t2>>2) - 2)) = 0; \
-                               bcopy((caddr_t)VTONFS(v)->n_fhp,(caddr_t)tl, \
-                                       VTONFS(v)->n_fhsize); \
-                       } else if ((t2 = nfsm_strtmbuf(&mb, &bpos, \
-                               (caddr_t)VTONFS(v)->n_fhp, VTONFS(v)->n_fhsize))) { \
-                               error = t2; \
-                               m_freem(mreq); \
-                               goto nfsmout; \
-                       } \
-               } else { \
-                       nfsm_build(cp, caddr_t, NFSX_V2FH); \
-                       bcopy((caddr_t)VTONFS(v)->n_fhp, cp, NFSX_V2FH); \
-               } }
-
-#define nfsm_srvfhtom(f, v3) \
-               { if (v3) { \
-                       nfsm_build(tl, u_long *, NFSX_UNSIGNED + NFSX_V3FH); \
-                       *tl++ = txdr_unsigned(NFSX_V3FH); \
-                       bcopy((caddr_t)(f), (caddr_t)tl, NFSX_V3FH); \
+       } while (0)
+
+/* add a 32bit value to an mbuf chain extending if necessary */
+#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)
+
+/* add a 64bit value to an mbuf chain */
+#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)
+
+/* zero the last 4 bytes for a range of opaque */
+/* data to make sure any pad bytes will be zero. */
+#define nfsm_chain_zero_opaque_pad(BUF, LEN) \
+       *(((uint32_t*)(BUF))+((nfsm_rndup(LEN)>>2)-1)) = 0
+
+/* add buffer of opaque data to an mbuf chain */
+#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)
+
+/* add buffer of opaque data to an mbuf chain without padding */
+#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)
+
+/* finish an mbuf in a chain to allow subsequent insertion */
+#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)
+
+/* add a file handle to an mbuf chain */
+#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)
+
+/* add a string to an mbuf chain */
+#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)
+
+/* add a name to an mbuf chain */
+#define nfsm_chain_add_name(E, NMC, STR, LEN, NMP) \
+       do { \
+               if (E) break; \
+               if (NMFLAG((NMP), NFC)) \
+                       (E) = nfsm_chain_add_string_nfc((NMC), (const uint8_t*)(STR), (LEN)); \
+               else \
+                       nfsm_chain_add_string((E), (NMC), (STR), (LEN)); \
+       } while (0)
+
+/* add an NFSv2 time to an mbuf chain */
+#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 { \
                } else { \
-                       nfsm_build(cp, caddr_t, NFSX_V2FH); \
-                       bcopy((caddr_t)(f), cp, NFSX_V2FH); \
-               } }
-
-#define nfsm_srvpostop_fh(f) \
-               { nfsm_build(tl, u_long *, 2 * NFSX_UNSIGNED + NFSX_V3FH); \
-               *tl++ = nfs_true; \
-               *tl++ = txdr_unsigned(NFSX_V3FH); \
-               bcopy((caddr_t)(f), (caddr_t)tl, NFSX_V3FH); \
-               }
-
-#define nfsm_mtofh(d, v, v3, f, x) \
-               { struct nfsnode *ttnp; nfsfh_t *ttfhp; int ttfhsize; \
-               if (v3) { \
-                       nfsm_dissect(tl, u_long *, NFSX_UNSIGNED); \
-                       (f) = fxdr_unsigned(int, *tl); \
-               } else \
-                       (f) = 1; \
-               if (f) { \
-                       nfsm_getfh(ttfhp, ttfhsize, (v3)); \
-                       if ((t1 = nfs_nget((d)->v_mount, ttfhp, ttfhsize, \
-                               &ttnp))) { \
-                               error = t1; \
-                               m_freem(mrep); \
-                               goto nfsmout; \
-                       } \
-                       (v) = NFSTOV(ttnp); \
+                       /* no time... use -1 */ \
+                       nfsm_chain_add_32((E), (NMC), -1); \
+                       nfsm_chain_add_32((E), (NMC), -1); \
                } \
                } \
-               if (v3) { \
-                       nfsm_dissect(tl, u_long *, NFSX_UNSIGNED); \
-                       if (f) \
-                               (f) = fxdr_unsigned(int, *tl); \
-                       else if (fxdr_unsigned(int, *tl)) \
-                               nfsm_adv(NFSX_V3FATTR); \
+       } while (0)
+
+/* add an NFSv3 time to an mbuf chain */
+#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)
+
+/* add an NFS v2 or v3 time to an mbuf chain */
+#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)); \
                } \
                } \
-               if (f) \
-                       nfsm_loadattr((v), (struct vattr *)0, (x)); \
-               }
-
-#define nfsm_getfh(f, s, v3) \
-               { if (v3) { \
-                       nfsm_dissect(tl, u_long *, NFSX_UNSIGNED); \
-                       if (((s) = fxdr_unsigned(int, *tl)) <= 0 || \
-                               (s) > NFSX_V3FHMAX) { \
-                               m_freem(mrep); \
-                               error = EBADRPC; \
-                               goto nfsmout; \
-                       } \
-               } else \
-                       (s) = NFSX_V2FH; \
-               nfsm_dissect((f), nfsfh_t *, nfsm_rndup(s)); }
-
-#define        nfsm_loadattr(v, a, x) \
-               { struct vnode *ttvp = (v); \
-               if ((t1 = nfs_loadattrcache(&ttvp, &md, &dpos, (a), 0, \
-                                           (x)))) { \
-                       error = t1; \
-                       m_freem(mrep); \
-                       goto nfsmout; \
+       } while (0)
+
+/* add an NFSv3 postop file handle to an mbuf chain */
+#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)
+
+/* add NFSv3 postop attributes to an mbuf chain */
+#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; \
                } \
                } \
-               (v) = ttvp; }
-
-#define        nfsm_postop_attr(v, f, x) \
-               { struct vnode *ttvp = (v); \
-               nfsm_dissect(tl, u_long *, NFSX_UNSIGNED); \
-               if (((f) = fxdr_unsigned(int, *tl))) { \
-                       if ((t1 = nfs_loadattrcache(&ttvp, &md, &dpos, \
-                                       (struct vattr *)0, 1, (x)))) { \
-                               error = t1; \
-                               (f) = 0; \
-                               m_freem(mrep); \
-                               goto nfsmout; \
-                       } \
-                       if (*(x) == 0) \
-                               (f) = 0; \
-                       (v) = ttvp; \
-               } }
-
-#define        nfsm_wcc_data(v, premtime, newpostattr, x) \
-               { \
-               nfsm_dissect(tl, u_long *, NFSX_UNSIGNED); \
-               if (*tl == nfs_true) { \
-                       nfsm_dissect(tl, u_long *, 6 * NFSX_UNSIGNED); \
-                       (premtime) = fxdr_unsigned(time_t, *(tl + 2)); \
+               nfsm_chain_add_32((E), (NMC), TRUE); \
+               if (E) break; \
+               (E) = nfsm_chain_add_fattr((ND), (NMC), (VAP)); \
+       } while (0)
+
+/* Add an NFSv2 "sattr" structure to an mbuf chain */
+#define nfsm_chain_add_v2sattr(E, NMC, VAP, SZRDEV) \
+       do { \
+               if (E) break; \
+               (E) = nfsm_chain_add_v2sattr_f((NMC), (VAP), (SZRDEV)); \
+       } while (0)
+
+/* Add an NFSv3 "sattr" structure to an mbuf chain */
+#define nfsm_chain_add_v3sattr(E, NMC, VAP) \
+       do { \
+               if (E) break; \
+               (E) = nfsm_chain_add_v3sattr_f((NMC), (VAP)); \
+       } while (0)
+
+/* Add an NFSv4 "fattr" structure to an mbuf chain */
+#define nfsm_chain_add_fattr4(E, NMC, VAP, NMP) \
+       do { \
+               if (E) break; \
+               (E) = nfsm_chain_add_fattr4_f((NMC), (VAP), (NMP)); \
+       } while (0)
+
+/* add NFSv3 WCC data to an mbuf chain */
+#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)
+
+/* add NFSv4 COMPOUND header */
+#define NFS4_TAG_LENGTH        12
+#define nfsm_chain_add_compound_header(E, NMC, TAG, MINOR, NUMOPS) \
+       do { \
+               if ((TAG) && strlen(TAG)) { \
+                       /* put tags into a fixed-length space-padded field */ \
+                       char __nfstag[NFS4_TAG_LENGTH+1]; \
+                       snprintf(__nfstag, sizeof(__nfstag), "%-*s", NFS4_TAG_LENGTH, (TAG)); \
+                       nfsm_chain_add_32((E), (NMC), NFS4_TAG_LENGTH); \
+                       nfsm_chain_add_opaque((E), (NMC), __nfstag, NFS4_TAG_LENGTH); \
                } else { \
                } else { \
-                       (premtime) = 0; \
+                       nfsm_chain_add_32((E), (NMC), 0); \
                } \
                } \
-               nfsm_postop_attr((v), (newpostattr), (x)); \
-               }
-
-#define nfsm_v3sattr(s, a, u, g) \
-               { (s)->sa_modetrue = nfs_true; \
-               (s)->sa_mode = vtonfsv3_mode((a)->va_mode); \
-               (s)->sa_uidtrue = nfs_true; \
-               (s)->sa_uid = txdr_unsigned(u); \
-               (s)->sa_gidtrue = nfs_true; \
-               (s)->sa_gid = txdr_unsigned(g); \
-               (s)->sa_sizefalse = nfs_false; \
-               (s)->sa_atimetype = txdr_unsigned(NFSV3SATTRTIME_TOSERVER); \
-               (s)->sa_mtimetype = txdr_unsigned(NFSV3SATTRTIME_TOSERVER); \
-               }
-
-#define        nfsm_strsiz(s,m) \
-               { nfsm_dissect(tl,u_long *,NFSX_UNSIGNED); \
-               if (((s) = fxdr_unsigned(long,*tl)) > (m)) { \
-                       m_freem(mrep); \
-                       error = EBADRPC; \
-                       goto nfsmout; \
-               } }
-
-#define        nfsm_srvstrsiz(s,m) \
-               { nfsm_dissect(tl,u_long *,NFSX_UNSIGNED); \
-               if (((s) = fxdr_unsigned(long,*tl)) > (m) || (s) <= 0) { \
-                       error = EBADRPC; \
-                       nfsm_reply(0); \
-               } }
-
-#define        nfsm_srvnamesiz(s) \
-               { nfsm_dissect(tl,u_long *,NFSX_UNSIGNED); \
-               if (((s) = fxdr_unsigned(long,*tl)) > NFS_MAXNAMLEN) \
-                       error = NFSERR_NAMETOL; \
-               if ((s) <= 0) \
-                       error = EBADRPC; \
-               if (error) \
-                       nfsm_reply(0); \
-               }
-
-#define nfsm_mtouio(p,s) \
-               if ((s) > 0 && \
-                  (t1 = nfsm_mbuftouio(&md,(p),(s),&dpos))) { \
-                       error = t1; \
-                       m_freem(mrep); \
-                       goto nfsmout; \
-               }
-
-#define nfsm_uiotom(p,s) \
-               if ((t1 = nfsm_uiotombuf((p),&mb,(s),&bpos))) { \
-                       error = t1; \
-                       m_freem(mreq); \
-                       goto nfsmout; \
-               }
-
-#define        nfsm_reqhead(v,a,s) \
-               mb = mreq = nfsm_reqh((v),(a),(s),&bpos)
-
-#define nfsm_reqdone   m_freem(mrep); \
-               nfsmout:
+               nfsm_chain_add_32((E), (NMC), (MINOR)); /*minorversion*/ \
+               nfsm_chain_add_32((E), (NMC), (NUMOPS)); \
+       } while (0)
+
+/* add NFSv4 attr bitmap */
+#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)
+
+/* add NFSv4 attr bitmap masked with the given mask */
+#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)
+
+/* add NFSv4 attr bitmap masked with the supported attributes for this mount/node */
+#define nfsm_chain_add_bitmap_supported(E, NMC, B, NMP, NP) \
+       do { \
+               uint32_t __bitmap[NFS_ATTR_BITMAP_LEN], *__bmp = (B); \
+               int __nonamedattr = 0, __noacl = 0, __nomode = 0; \
+               if (!((NMP)->nm_fsattr.nfsa_flags & NFS_FSFLAG_NAMED_ATTR) || \
+                   ((NP) && (((nfsnode_t)(NP))->n_flag & (NISDOTZFS|NISDOTZFSCHILD)))) \
+                       __nonamedattr = 1; \
+               if (!((NMP)->nm_fsattr.nfsa_flags & NFS_FSFLAG_ACL)) \
+                       __noacl = 1; \
+               if (NMFLAG((NMP), ACLONLY)) \
+                       __nomode = 1; \
+               if (__nonamedattr || __noacl || __nomode) { \
+                       /* don't ask for attrs we're not supporting */ \
+                       /* some ".zfs" directories can't handle being asked for some attributes */ \
+                       int __ii; \
+                       NFS_CLEAR_ATTRIBUTES(__bitmap); \
+                       for (__ii=0; __ii < NFS_ATTR_BITMAP_LEN; __ii++) \
+                               __bitmap[__ii] = (B)[__ii]; \
+                       if (__nonamedattr) \
+                               NFS_BITMAP_CLR(__bitmap, NFS_FATTR_NAMED_ATTR); \
+                       if (__noacl) \
+                               NFS_BITMAP_CLR(__bitmap, NFS_FATTR_ACL); \
+                       if (__nomode) \
+                               NFS_BITMAP_CLR(__bitmap, NFS_FATTR_MODE); \
+                       __bmp = __bitmap; \
+               } \
+               nfsm_chain_add_bitmap_masked((E), (NMC), __bmp, NFS_ATTR_BITMAP_LEN, (NMP)->nm_fsattr.nfsa_supp_attr); \
+       } while (0)
+
+/* Add an NFSv4 "stateid" structure to an mbuf chain */
+#define nfsm_chain_add_stateid(E, NMC, SID) \
+       do { \
+               nfsm_chain_add_32((E), (NMC), (SID)->seqid); \
+               nfsm_chain_add_32((E), (NMC), (SID)->other[0]); \
+               nfsm_chain_add_32((E), (NMC), (SID)->other[1]); \
+               nfsm_chain_add_32((E), (NMC), (SID)->other[2]); \
+       } while (0)
+
+/* add an NFSv4 lock owner structure to an mbuf chain */
+#define nfsm_chain_add_lock_owner4(E, NMC, NMP, NLOP) \
+       do { \
+               nfsm_chain_add_64((E), (NMC), (NMP)->nm_clientid); \
+               nfsm_chain_add_32((E), (NMC), 5*NFSX_UNSIGNED); \
+               nfsm_chain_add_32((E), (NMC), (NLOP)->nlo_name); \
+               nfsm_chain_add_32((E), (NMC), (NLOP)->nlo_pid); \
+               nfsm_chain_add_64((E), (NMC), (NLOP)->nlo_pid_start.tv_sec); \
+               nfsm_chain_add_32((E), (NMC), (NLOP)->nlo_pid_start.tv_usec); \
+       } while (0)
 
 
-#define nfsm_rndup(a)  (((a)+3)&(~0x3))
+/*
+ * macros for dissecting NFS mbuf chains
+ */
 
 
-/* 
-* We seem to see cases mainly on shutdown where the vnode got recycled
-* on use while waiting on server. Maybe nfs vnode locking will help if
-* we implement that, but for now, check for bad vnodes and return an
-* error. This call spot should catch most of them. Note that NFSv2
-* just goes to nfsmout here, while nfsV3 goes back to caller's next
-* line for post-processing. It will do a nfsm_reqdone also making
-* m_freem(mrep). Wondering if some of our freeing problems could be
-* due to nfsv3 calling nfsm_reqdone unlike nfsv2. Separate problem.
-*/
-#define        nfsm_request(v, t, p, c, x)     \
-                { \
-                int nfsv3; \
-               if (!VFSTONFS((v)->v_mount)) { \
-                       error = ENXIO; \
-                       goto nfsmout; \
+/* prepare an mbuf chain for dissection starting with the given mbuf */
+#define nfsm_chain_dissect_init(E, NMC, H) \
+       do { \
+               if (!(H)) { \
+                       (E) = EINVAL; \
+                       break; \
                } \
                } \
-                nfsv3 = (VFSTONFS((v)->v_mount))->nm_flag & NFSMNT_NFSV3; \
-               if ((error = nfs_request((v), mreq, (t), (p), \
-                  (c), &mrep, &md, &dpos, (x)))) { \
-                       if (error & NFSERR_RETERR) \
-                               error &= ~NFSERR_RETERR; \
-                       else \
-                               goto nfsmout; \
+               (NMC)->nmc_mcur = (NMC)->nmc_mhead = (H); \
+               (NMC)->nmc_ptr = mbuf_data(H); \
+               (NMC)->nmc_left = mbuf_len(H); \
+       } while (0)
+
+/* skip a number of bytes in an mbuf chain */
+#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)); \
                } \
                } \
-                }
-
-#define        nfsm_strtom(a,s,m) \
-               if ((s) > (m)) { \
-                       m_freem(mreq); \
-                       error = ENAMETOOLONG; \
-                       goto nfsmout; \
+       } while (0)
+
+/* get a 32bit value from an mbuf chain */
+#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; \
                } \
                } \
-               t2 = nfsm_rndup(s)+NFSX_UNSIGNED; \
-               if (t2 <= M_TRAILINGSPACE(mb)) { \
-                       nfsm_build(tl,u_long *,t2); \
-                       *tl++ = txdr_unsigned(s); \
-                       *(tl+((t2>>2)-2)) = 0; \
-                       bcopy((caddr_t)(a), (caddr_t)tl, (s)); \
-               } else if ((t2 = nfsm_strtmbuf(&mb, &bpos, (a), (s)))) { \
-                       error = t2; \
-                       m_freem(mreq); \
-                       goto nfsmout; \
-               }
-
-#define        nfsm_srvdone \
-               nfsmout: \
-               return(error)
-
-#define        nfsm_reply(s) \
-               { \
-               nfsd->nd_repstat = error; \
-               if (error && !(nfsd->nd_flag & ND_NFSV3)) \
-                  (void) nfs_rephead(0, nfsd, slp, error, cache, &frev, \
-                       mrq, &mb, &bpos); \
-               else \
-                  (void) nfs_rephead((s), nfsd, slp, error, cache, &frev, \
-                       mrq, &mb, &bpos); \
-               m_freem(mrep); \
-               mrep = NULL; \
-               mreq = *mrq; \
-               if (error && (!(nfsd->nd_flag & ND_NFSV3) || \
-                       error == EBADRPC)) \
-                       return(0); \
-               }
-
-#define        nfsm_writereply(s, v3) \
-               { \
-               nfsd->nd_repstat = error; \
-               if (error && !(v3)) \
-                  (void) nfs_rephead(0, nfsd, slp, error, cache, &frev, \
-                       &mreq, &mb, &bpos); \
-               else \
-                  (void) nfs_rephead((s), nfsd, slp, error, cache, &frev, \
-                       &mreq, &mb, &bpos); \
-               }
-
-#define        nfsm_adv(s) \
-               { t1 = mtod(md, caddr_t)+md->m_len-dpos; \
-               if (t1 >= (s)) { \
-                       dpos += (s); \
-               } else if ((t1 = nfs_adv(&md, &dpos, (s), t1))) { \
-                       error = t1; \
-                       m_freem(mrep); \
-                       goto nfsmout; \
-               } }
-
-#define nfsm_srvmtofh(f) \
-               { if (nfsd->nd_flag & ND_NFSV3) { \
-                       nfsm_dissect(tl, u_long *, NFSX_UNSIGNED); \
-                       if (fxdr_unsigned(int, *tl) != NFSX_V3FH) { \
-                               error = EBADRPC; \
-                               nfsm_reply(0); \
-                       } \
+               (LVAL) = fxdr_unsigned(uint32_t, *__tmpptr); \
+       } while (0)
+
+/* get a 64bit value from an mbuf chain */
+#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)
+
+/* get a pointer to the next consecutive bytes in an mbuf chain */
+#define nfsm_chain_get_opaque_pointer(E, NMC, LEN, PTR) \
+       do { \
+               uint32_t rndlen; \
+               if (E) break; \
+               rndlen = nfsm_rndup(LEN); \
+               if ((NMC)->nmc_left >= rndlen) { \
+                       (PTR) = (void*)(NMC)->nmc_ptr; \
+                       (NMC)->nmc_left -= rndlen; \
+                       (NMC)->nmc_ptr += rndlen; \
+               } else { \
+                       (E) = nfsm_chain_get_opaque_pointer_f((NMC), (LEN), (u_char**)&(PTR)); \
                } \
                } \
-               nfsm_dissect(tl, u_long *, NFSX_V3FH); \
-               bcopy((caddr_t)tl, (caddr_t)(f), NFSX_V3FH); \
-               if ((nfsd->nd_flag & ND_NFSV3) == 0) \
-                       nfsm_adv(NFSX_V2FH - NFSX_V3FH); \
-               }
-
-#define        nfsm_clget \
-               if (bp >= be) { \
-                       if (mp == mb) \
-                               mp->m_len += bp-bpos; \
-                       MGET(mp, M_WAIT, MT_DATA); \
-                       MCLGET(mp, M_WAIT); \
-                       mp->m_len = NFSMSIZ(mp); \
-                       mp2->m_next = mp; \
-                       mp2 = mp; \
-                       bp = mtod(mp, caddr_t); \
-                       be = bp+mp->m_len; \
+       } while (0)
+
+/* copy the next consecutive bytes of opaque data from an mbuf chain */
+#define nfsm_chain_get_opaque(E, NMC, LEN, PTR) \
+       do { \
+               uint32_t rndlen; \
+               if (E) break; \
+               rndlen = nfsm_rndup(LEN); \
+               if ((NMC)->nmc_left >= rndlen) { \
+                       u_char *__tmpptr = (u_char*)(NMC)->nmc_ptr; \
+                       (NMC)->nmc_left -= rndlen; \
+                       (NMC)->nmc_ptr += rndlen; \
+                       bcopy(__tmpptr, (PTR), (LEN)); \
+               } else { \
+                       (E) = nfsm_chain_get_opaque_f((NMC), (LEN), (u_char*)(PTR)); \
                } \
                } \
-               tl = (u_long *)bp
-
-#define        nfsm_srvfillattr(a, f) \
-               nfsm_srvfattr(nfsd, (a), (f))
-
-#define nfsm_srvwcc_data(br, b, ar, a) \
-               nfsm_srvwcc(nfsd, (br), (b), (ar), (a), &mb, &bpos)
+       } while (0)
 
 
-#define nfsm_srvpostop_attr(r, a) \
-               nfsm_srvpostopattr(nfsd, (r), (a), &mb, &bpos)
-
-#define nfsm_srvsattr(a) \
-               { \
-               struct timeval now; \
-               nfsm_dissect(tl, u_long *, NFSX_UNSIGNED); \
-               if (*tl == nfs_true) { \
-                       nfsm_dissect(tl, u_long *, NFSX_UNSIGNED); \
-                       (a)->va_mode = nfstov_mode(*tl); \
-               } \
-               nfsm_dissect(tl, u_long *, NFSX_UNSIGNED); \
-               if (*tl == nfs_true) { \
-                       nfsm_dissect(tl, u_long *, NFSX_UNSIGNED); \
-                       (a)->va_uid = fxdr_unsigned(uid_t, *tl); \
+/* get the size of and a pointer to a file handle in an mbuf chain */
+#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)
+
+/* get the size of and data for a file handle in an mbuf chain */
+#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)
+
+/* get an NFS v2 or v3 time from an mbuf chain */
+#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)
+
+/* get postop attributes from an mbuf chain */
+#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)
+
+/* update a node's attribute cache with postop attributes from an mbuf chain */
+/* (F returns whether the attributes were updated or not) */
+#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; \
                } \
                } \
-               nfsm_dissect(tl, u_long *, NFSX_UNSIGNED); \
-               if (*tl == nfs_true) { \
-                       nfsm_dissect(tl, u_long *, NFSX_UNSIGNED); \
-                       (a)->va_gid = fxdr_unsigned(gid_t, *tl); \
+               if (*(X) == 0) \
+                       (F) = 0; \
+       } while (0)
+
+/* update a node's attribute cache with postop attributes from an mbuf chain */
+#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)
+
+/* get and process NFSv3 WCC data from an mbuf chain */
+#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)
+
+/* update a node's attribute cache with attributes from an mbuf chain */
+#define nfsm_chain_loadattr(E, NMC, NP, VERS, X) \
+       do { \
+               struct nfs_vattr ttvattr; \
+               if (E) break; \
+               if ((VERS) == NFS_VER4) { \
+                       (E) = nfs4_parsefattr((NMC), NULL, &ttvattr, NULL, NULL, NULL); \
+               } else { \
+                       (E) = nfs_parsefattr((NMC), (VERS), &ttvattr); \
                } \
                } \
-               nfsm_dissect(tl, u_long *, NFSX_UNSIGNED); \
-               if (*tl == nfs_true) { \
-                       nfsm_dissect(tl, u_long *, 2 * NFSX_UNSIGNED); \
-                       fxdr_hyper(tl, &(a)->va_size); \
+               if (!(E) && (NP)) \
+                       (E) = nfs_loadattrcache((NP), &ttvattr, (X), 0); \
+               NVATTR_CLEANUP(&ttvattr); \
+       } while (0)
+
+/* get NFSv4 attr bitmap */
+#define nfsm_chain_get_bitmap(E, NMC, B, LEN) \
+       do { \
+               uint32_t __len = 0, __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++) \
+                       nfsm_chain_adv((E), (NMC), NFSX_UNSIGNED); \
+               for (; __i < (LEN); __i++) \
+                       (B)[__i] = 0; \
+               (LEN) = __len; \
+       } while (0)
+
+/* get an NFSv4 "stateid" structure from an mbuf chain */
+#define nfsm_chain_get_stateid(E, NMC, SID) \
+       do { \
+               nfsm_chain_get_32((E), (NMC), (SID)->seqid); \
+               nfsm_chain_get_32((E), (NMC), (SID)->other[0]); \
+               nfsm_chain_get_32((E), (NMC), (SID)->other[1]); \
+               nfsm_chain_get_32((E), (NMC), (SID)->other[2]); \
+       } while (0)
+
+#define nfsm_chain_skip_tag(E, NMC) \
+       do { \
+               uint32_t __val = 0; \
+               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); \
+               /* [sigh] some implementations return the "illegal" op for unsupported ops */ \
+               nfsm_assert((E), ((__val == (OP)) || (__val == NFS_OP_ILLEGAL)), 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) || !(DNP)) break; \
+               if (__ci_atomic && (__ci_before == (DNP)->n_ncchange)) { \
+                       (DNP)->n_ncchange = __ci_after; \
+               } else { \
+                       cache_purge(NFSTOV(DNP)); \
+                       (DNP)->n_ncgen++; \
                } \
                } \
-               nfsm_dissect(tl, u_long *, NFSX_UNSIGNED); \
-               microtime(&now); \
-               switch (fxdr_unsigned(int, *tl)) { \
-               case NFSV3SATTRTIME_TOCLIENT: \
-                       nfsm_dissect(tl, u_long *, 2 * NFSX_UNSIGNED); \
-                       fxdr_nfsv3time(tl, &(a)->va_atime); \
-                       break; \
-               case NFSV3SATTRTIME_TOSERVER: \
-                       (a)->va_atime.tv_sec = now.tv_sec; \
-                       (a)->va_atime.tv_nsec = now.tv_usec * 1000; \
-                       break; \
-               }; \
-               nfsm_dissect(tl, u_long *, NFSX_UNSIGNED); \
-               switch (fxdr_unsigned(int, *tl)) { \
-               case NFSV3SATTRTIME_TOCLIENT: \
-                       nfsm_dissect(tl, u_long *, 2 * NFSX_UNSIGNED); \
-                       fxdr_nfsv3time(tl, &(a)->va_mtime); \
-                       break; \
-               case NFSV3SATTRTIME_TOSERVER: \
-                       (a)->va_mtime.tv_sec = now.tv_sec; \
-                       (a)->va_mtime.tv_nsec = now.tv_usec * 1000; \
-                       break; \
-               }; }
+       } while (0)
 
 #endif /* __APPLE_API_PRIVATE */
 #endif /* _NFS_NFSM_SUBS_H_ */
 
 #endif /* __APPLE_API_PRIVATE */
 #endif /* _NFS_NFSM_SUBS_H_ */