Home | History | Annotate | Line # | Download | only in nfs
      1  1.59  riastrad /*	$NetBSD: nfsm_subs.h,v 1.59 2024/12/07 02:05:55 riastradh Exp $	*/
      2   1.5       cgd 
      3   1.1   mycroft /*
      4   1.4   mycroft  * Copyright (c) 1989, 1993
      5   1.4   mycroft  *	The Regents of the University of California.  All rights reserved.
      6   1.1   mycroft  *
      7   1.1   mycroft  * This code is derived from software contributed to Berkeley by
      8   1.1   mycroft  * Rick Macklem at The University of Guelph.
      9   1.1   mycroft  *
     10   1.1   mycroft  * Redistribution and use in source and binary forms, with or without
     11   1.1   mycroft  * modification, are permitted provided that the following conditions
     12   1.1   mycroft  * are met:
     13   1.1   mycroft  * 1. Redistributions of source code must retain the above copyright
     14   1.1   mycroft  *    notice, this list of conditions and the following disclaimer.
     15   1.1   mycroft  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1   mycroft  *    notice, this list of conditions and the following disclaimer in the
     17   1.1   mycroft  *    documentation and/or other materials provided with the distribution.
     18  1.31       agc  * 3. Neither the name of the University nor the names of its contributors
     19   1.1   mycroft  *    may be used to endorse or promote products derived from this software
     20   1.1   mycroft  *    without specific prior written permission.
     21   1.1   mycroft  *
     22   1.1   mycroft  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23   1.1   mycroft  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24   1.1   mycroft  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25   1.1   mycroft  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26   1.1   mycroft  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27   1.1   mycroft  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28   1.1   mycroft  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29   1.1   mycroft  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30   1.1   mycroft  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31   1.1   mycroft  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32   1.1   mycroft  * SUCH DAMAGE.
     33   1.1   mycroft  *
     34   1.9      fvdl  *	@(#)nfsm_subs.h	8.2 (Berkeley) 3/30/95
     35   1.1   mycroft  */
     36   1.1   mycroft 
     37   1.9      fvdl #ifndef _NFS_NFSM_SUBS_H_
     38   1.9      fvdl #define _NFS_NFSM_SUBS_H_
     39   1.9      fvdl 
     40   1.9      fvdl 
     41   1.1   mycroft /*
     42   1.1   mycroft  * These macros do strange and peculiar things to mbuf chains for
     43   1.1   mycroft  * the assistance of the nfs code. To attempt to use them for any
     44   1.1   mycroft  * other purpose will be dangerous. (they make weird assumptions)
     45   1.1   mycroft  */
     46   1.1   mycroft 
     47   1.1   mycroft /*
     48   1.1   mycroft  * First define what the actual subs. return
     49   1.1   mycroft  */
     50   1.9      fvdl 
     51   1.1   mycroft #define	M_HASCL(m)	((m)->m_flags & M_EXT)
     52   1.1   mycroft #define	NFSMADV(m, s)	(m)->m_data += (s)
     53  1.15   thorpej #define	NFSMSIZ(m)	((M_HASCL(m)) ? (m)->m_ext.ext_size : \
     54  1.15   thorpej 				(((m)->m_flags & M_PKTHDR) ? MHLEN : MLEN))
     55   1.1   mycroft 
     56   1.1   mycroft /*
     57  1.54   mlelstv  * NFSv2 can only handle signed 32bit quantities and some clients
     58  1.54   mlelstv  * get confused by larger than 16bit block sizes. Limit values
     59  1.54   mlelstv  * for better compatibility.
     60  1.54   mlelstv  */
     61  1.54   mlelstv #define NFS_V2CLAMP32(x) ((x) > INT32_MAX ? INT32_MAX : (int32_t)(x))
     62  1.54   mlelstv #define NFS_V2CLAMP16(x) ((x) > INT16_MAX ? INT16_MAX : (int32_t)(x))
     63  1.54   mlelstv 
     64  1.54   mlelstv /*
     65   1.1   mycroft  * Now for the macros that do the simple stuff and call the functions
     66   1.1   mycroft  * for the hard stuff.
     67   1.1   mycroft  * These macros use several vars. declared in nfsm_reqhead and these
     68   1.1   mycroft  * vars. must not be used elsewhere unless you are careful not to corrupt
     69   1.1   mycroft  * them. The vars. starting with pN and tN (N=1,2,3,..) are temporaries
     70   1.1   mycroft  * that may be used so long as the value is not expected to retained
     71   1.1   mycroft  * after a macro.
     72   1.1   mycroft  * I know, this is kind of dorkey, but it makes the actual op functions
     73   1.1   mycroft  * fairly clean and deals with the mess caused by the xdr discriminating
     74   1.1   mycroft  * unions.
     75   1.1   mycroft  */
     76   1.1   mycroft 
     77   1.1   mycroft #define	nfsm_build(a,c,s) \
     78   1.4   mycroft 		{ if ((s) > M_TRAILINGSPACE(mb)) { \
     79  1.23      matt 			struct mbuf *mb2; \
     80  1.23      matt 			mb2 = m_get(M_WAIT, MT_DATA); \
     81  1.23      matt 			MCLAIM(mb2, &nfs_mowner); \
     82   1.1   mycroft 			if ((s) > MLEN) \
     83   1.1   mycroft 				panic("build > MLEN"); \
     84   1.1   mycroft 			mb->m_next = mb2; \
     85   1.1   mycroft 			mb = mb2; \
     86   1.1   mycroft 			mb->m_len = 0; \
     87  1.50  christos 			bpos = mtod(mb, char *); \
     88   1.1   mycroft 		} \
     89   1.1   mycroft 		(a) = (c)(bpos); \
     90   1.1   mycroft 		mb->m_len += (s); \
     91   1.4   mycroft 		bpos += (s); }
     92   1.1   mycroft 
     93  1.14      fvdl #define nfsm_aligned(p) ALIGNED_POINTER(p,u_int32_t)
     94  1.12      fvdl 
     95   1.9      fvdl #define	nfsm_dissect(a, c, s) \
     96  1.50  christos 		{ t1 = mtod(md, char *) + md->m_len-dpos; \
     97  1.12      fvdl 		if (t1 >= (s) && nfsm_aligned(dpos)) { \
     98   1.1   mycroft 			(a) = (c)(dpos); \
     99   1.1   mycroft 			dpos += (s); \
    100   1.9      fvdl 		} else if ((t1 = nfsm_disct(&md, &dpos, (s), t1, &cp2)) != 0){ \
    101   1.9      fvdl 			error = t1; \
    102   1.1   mycroft 			m_freem(mrep); \
    103   1.1   mycroft 			goto nfsmout; \
    104   1.1   mycroft 		} else { \
    105   1.1   mycroft 			(a) = (c)cp2; \
    106   1.4   mycroft 		} }
    107   1.1   mycroft 
    108  1.26  drochner #define nfsm_fhtom(n, v3) \
    109   1.9      fvdl 	      { if (v3) { \
    110  1.26  drochner 			t2 = nfsm_rndup((n)->n_fhsize) + NFSX_UNSIGNED; \
    111   1.9      fvdl 			if (t2 <= M_TRAILINGSPACE(mb)) { \
    112   1.9      fvdl 				nfsm_build(tl, u_int32_t *, t2); \
    113  1.26  drochner 				*tl++ = txdr_unsigned((n)->n_fhsize); \
    114   1.9      fvdl 				*(tl + ((t2>>2) - 2)) = 0; \
    115  1.50  christos 				memcpy(tl,(n)->n_fhp, (n)->n_fhsize); \
    116   1.9      fvdl 			} else if ((t2 = nfsm_strtmbuf(&mb, &bpos, \
    117  1.50  christos 				(void *)(n)->n_fhp, (n)->n_fhsize)) != 0) { \
    118   1.9      fvdl 				error = t2; \
    119   1.9      fvdl 				m_freem(mreq); \
    120   1.9      fvdl 				goto nfsmout; \
    121   1.9      fvdl 			} \
    122   1.9      fvdl 		} else { \
    123  1.50  christos 			nfsm_build(cp, void *, NFSX_V2FH); \
    124  1.50  christos 			memcpy(cp, (n)->n_fhp, NFSX_V2FH); \
    125   1.9      fvdl 		} }
    126   1.9      fvdl 
    127   1.9      fvdl #define nfsm_srvfhtom(f, v3) \
    128   1.9      fvdl 		{ if (v3) { \
    129  1.47      yamt 			nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED + \
    130  1.47      yamt 			    NFSRVFH_SIZE(f)); \
    131  1.47      yamt 			*tl++ = txdr_unsigned(NFSRVFH_SIZE(f)); \
    132  1.47      yamt 			memcpy(tl, NFSRVFH_DATA(f), NFSRVFH_SIZE(f)); \
    133   1.9      fvdl 		} else { \
    134  1.47      yamt 			KASSERT(NFSRVFH_SIZE(f) == NFSX_V2FH); \
    135  1.50  christos 			nfsm_build(cp, void *, NFSX_V2FH); \
    136  1.47      yamt 			memcpy(cp, NFSRVFH_DATA(f), NFSX_V2FH); \
    137   1.9      fvdl 		} }
    138   1.9      fvdl 
    139   1.9      fvdl #define nfsm_srvpostop_fh(f) \
    140  1.47      yamt 		{ nfsm_build(tl, u_int32_t *, \
    141  1.47      yamt 		    2 * NFSX_UNSIGNED + NFSRVFH_SIZE(f)); \
    142   1.9      fvdl 		*tl++ = nfs_true; \
    143  1.47      yamt 		*tl++ = txdr_unsigned(NFSRVFH_SIZE(f)); \
    144  1.47      yamt 		memcpy(tl, NFSRVFH_DATA(f), NFSRVFH_SIZE(f)); \
    145   1.9      fvdl 		}
    146   1.9      fvdl 
    147  1.34      yamt /*
    148  1.34      yamt  * nfsm_mtofh: dissect a "resulted obj" part of create-like operations
    149  1.34      yamt  * like mkdir.
    150  1.34      yamt  *
    151  1.34      yamt  * for nfsv3, dissect post_op_fh3 and following post_op_attr.
    152  1.34      yamt  * for nfsv2, dissect fhandle and following fattr.
    153  1.34      yamt  *
    154  1.55    andvar  * d: (IN) the vnode of the parent directory.
    155  1.34      yamt  * v: (OUT) the corresponding vnode (we allocate one if needed)
    156  1.34      yamt  * v3: (IN) true for nfsv3.
    157  1.34      yamt  * f: (OUT) true if we got valid filehandle.  always true for nfsv2.
    158  1.34      yamt  */
    159  1.34      yamt 
    160  1.30      fvdl #define nfsm_mtofh(d, v, v3, f) \
    161   1.9      fvdl 		{ struct nfsnode *ttnp; nfsfh_t *ttfhp; int ttfhsize; \
    162  1.36      yamt 		int hasattr = 0; \
    163   1.9      fvdl 		if (v3) { \
    164   1.9      fvdl 			nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
    165   1.9      fvdl 			(f) = fxdr_unsigned(int, *tl); \
    166  1.36      yamt 		} else { \
    167   1.9      fvdl 			(f) = 1; \
    168  1.36      yamt 			hasattr = 1; \
    169  1.36      yamt 		} \
    170   1.9      fvdl 		if (f) { \
    171   1.9      fvdl 			nfsm_getfh(ttfhp, ttfhsize, (v3)); \
    172   1.9      fvdl 			if ((t1 = nfs_nget((d)->v_mount, ttfhp, ttfhsize, \
    173  1.30      fvdl 				&ttnp)) != 0) { \
    174   1.9      fvdl 				error = t1; \
    175   1.9      fvdl 				m_freem(mrep); \
    176   1.9      fvdl 				goto nfsmout; \
    177   1.9      fvdl 			} \
    178   1.9      fvdl 			(v) = NFSTOV(ttnp); \
    179   1.9      fvdl 		} \
    180   1.9      fvdl 		if (v3) { \
    181   1.9      fvdl 			nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
    182   1.9      fvdl 			if (f) \
    183  1.36      yamt 				hasattr = fxdr_unsigned(int, *tl); \
    184   1.9      fvdl 			else if (fxdr_unsigned(int, *tl)) \
    185   1.9      fvdl 				nfsm_adv(NFSX_V3FATTR); \
    186   1.1   mycroft 		} \
    187  1.36      yamt 		if (f && hasattr) \
    188  1.22      yamt 			nfsm_loadattr((v), (struct vattr *)0, 0); \
    189   1.1   mycroft 		}
    190   1.1   mycroft 
    191  1.34      yamt /*
    192  1.34      yamt  * nfsm_getfh: dissect a filehandle.
    193  1.34      yamt  *
    194  1.34      yamt  * f: (OUT) a filehandle.
    195  1.34      yamt  * s: (OUT) size of the filehandle in bytes.
    196  1.34      yamt  * v3: (IN) true if nfsv3.
    197  1.34      yamt  */
    198  1.34      yamt 
    199   1.9      fvdl #define nfsm_getfh(f, s, v3) \
    200   1.9      fvdl 		{ if (v3) { \
    201   1.9      fvdl 			nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
    202   1.9      fvdl 			if (((s) = fxdr_unsigned(int, *tl)) <= 0 || \
    203   1.9      fvdl 				(s) > NFSX_V3FHMAX) { \
    204   1.9      fvdl 				m_freem(mrep); \
    205   1.9      fvdl 				error = EBADRPC; \
    206   1.9      fvdl 				goto nfsmout; \
    207   1.9      fvdl 			} \
    208   1.9      fvdl 		} else \
    209   1.9      fvdl 			(s) = NFSX_V2FH; \
    210   1.9      fvdl 		nfsm_dissect((f), nfsfh_t *, nfsm_rndup(s)); }
    211   1.9      fvdl 
    212  1.22      yamt #define	nfsm_loadattr(v, a, flags) \
    213   1.9      fvdl 		{ struct vnode *ttvp = (v); \
    214  1.22      yamt 		if ((t1 = nfsm_loadattrcache(&ttvp, &md, &dpos, (a), (flags))) \
    215  1.22      yamt 		    != 0) { \
    216   1.9      fvdl 			error = t1; \
    217   1.1   mycroft 			m_freem(mrep); \
    218   1.1   mycroft 			goto nfsmout; \
    219   1.1   mycroft 		} \
    220   1.9      fvdl 		(v) = ttvp; }
    221   1.9      fvdl 
    222  1.33      yamt /*
    223  1.33      yamt  * nfsm_postop_attr: process nfsv3 post_op_attr
    224  1.33      yamt  *
    225  1.33      yamt  * dissect post_op_attr.  if we got a one,
    226  1.33      yamt  * call nfsm_loadattrcache to update attribute cache.
    227  1.33      yamt  *
    228  1.33      yamt  * v: (IN/OUT) the corresponding vnode
    229  1.33      yamt  * f: (OUT) true if we got valid attribute
    230  1.33      yamt  * flags: (IN) flags for nfsm_loadattrcache
    231  1.33      yamt  */
    232  1.33      yamt 
    233  1.22      yamt #define	nfsm_postop_attr(v, f, flags) \
    234   1.9      fvdl 		{ struct vnode *ttvp = (v); \
    235   1.9      fvdl 		nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
    236   1.9      fvdl 		if (((f) = fxdr_unsigned(int, *tl)) != 0) { \
    237  1.17      fvdl 			if ((t1 = nfsm_loadattrcache(&ttvp, &md, &dpos, \
    238  1.22      yamt 				(struct vattr *)0, (flags))) != 0) { \
    239   1.9      fvdl 				error = t1; \
    240   1.9      fvdl 				(f) = 0; \
    241   1.9      fvdl 				m_freem(mrep); \
    242   1.9      fvdl 				goto nfsmout; \
    243   1.9      fvdl 			} \
    244   1.9      fvdl 			(v) = ttvp; \
    245   1.9      fvdl 		} }
    246   1.9      fvdl 
    247  1.33      yamt /*
    248  1.33      yamt  * nfsm_wcc_data: process nfsv3 wcc_data
    249  1.33      yamt  *
    250  1.33      yamt  * dissect pre_op_attr and then let nfsm_postop_attr dissect post_op_attr.
    251  1.33      yamt  *
    252  1.33      yamt  * v: (IN/OUT) the corresponding vnode
    253  1.33      yamt  * f: (IN/OUT)
    254  1.33      yamt  *	NFSV3_WCCRATTR	return true if we got valid post_op_attr.
    255  1.33      yamt  *	NFSV3_WCCCHK	return true if pre_op_attr's mtime is the same
    256  1.33      yamt  *			as our n_mtime.  (ie. our cache isn't stale.)
    257  1.33      yamt  * flags: (IN) flags for nfsm_loadattrcache
    258  1.49   thorpej  * docheck: (IN) true if timestamp change is expected
    259  1.33      yamt  */
    260  1.33      yamt 
    261   1.9      fvdl /* Used as (f) for nfsm_wcc_data() */
    262   1.9      fvdl #define NFSV3_WCCRATTR	0
    263   1.9      fvdl #define NFSV3_WCCCHK	1
    264   1.9      fvdl 
    265  1.39      yamt #define	nfsm_wcc_data(v, f, flags, docheck) \
    266  1.35      yamt 		{ int ttattrf, ttretf = 0, renewctime = 0, renewnctime = 0; \
    267  1.39      yamt 		struct timespec ctime, mtime; \
    268  1.41  christos 		struct nfsnode *nfsp = VTONFS(v); \
    269  1.49   thorpej 		bool haspreopattr = false; \
    270   1.9      fvdl 		nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
    271   1.9      fvdl 		if (*tl == nfs_true) { \
    272  1.49   thorpej 			haspreopattr = true; \
    273   1.9      fvdl 			nfsm_dissect(tl, u_int32_t *, 6 * NFSX_UNSIGNED); \
    274  1.39      yamt 			fxdr_nfsv3time(tl + 2, &mtime); \
    275  1.35      yamt 			fxdr_nfsv3time(tl + 4, &ctime); \
    276  1.41  christos 			if (nfsp->n_ctime == ctime.tv_sec) \
    277  1.35      yamt 				renewctime = 1; \
    278  1.35      yamt 			if ((v)->v_type == VDIR) { \
    279  1.41  christos 				if (timespeccmp(&nfsp->n_nctime, &ctime, ==)) \
    280  1.35      yamt 					renewnctime = 1; \
    281  1.35      yamt 			} \
    282  1.32      yamt 			if (f) { \
    283  1.41  christos 				ttretf = timespeccmp(&nfsp->n_mtime, &mtime, ==);\
    284  1.32      yamt 			} \
    285   1.9      fvdl 		} \
    286  1.22      yamt 		nfsm_postop_attr((v), ttattrf, (flags)); \
    287  1.41  christos 		nfsp = VTONFS(v); \
    288  1.39      yamt 		if (ttattrf) { \
    289  1.39      yamt 			if (haspreopattr && \
    290  1.41  christos 			    nfs_check_wccdata(nfsp, &ctime, &mtime, (docheck))) \
    291  1.39      yamt 				renewctime = renewnctime = ttretf = 0; \
    292  1.39      yamt 			if (renewctime) \
    293  1.41  christos 				nfsp->n_ctime = nfsp->n_vattr->va_ctime.tv_sec; \
    294  1.39      yamt 			if (renewnctime) \
    295  1.41  christos 				nfsp->n_nctime = nfsp->n_vattr->va_ctime; \
    296  1.39      yamt 		} \
    297   1.9      fvdl 		if (f) { \
    298   1.9      fvdl 			(f) = ttretf; \
    299   1.9      fvdl 		} else { \
    300   1.9      fvdl 			(f) = ttattrf; \
    301   1.9      fvdl 		} }
    302   1.9      fvdl 
    303  1.20      fvdl /* If full is true, set all fields, otherwise just set mode and time fields */
    304  1.20      fvdl #define nfsm_v3attrbuild(a, full)						\
    305  1.20      fvdl 		{ if ((a)->va_mode != (mode_t)VNOVAL) {				\
    306  1.20      fvdl 			nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);		\
    307  1.20      fvdl 			*tl++ = nfs_true;					\
    308  1.20      fvdl 			*tl = txdr_unsigned((a)->va_mode);			\
    309  1.20      fvdl 		} else {							\
    310  1.20      fvdl 			nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);		\
    311  1.20      fvdl 			*tl = nfs_false;					\
    312  1.20      fvdl 		}								\
    313  1.20      fvdl 		if ((full) && (a)->va_uid != (uid_t)VNOVAL) {			\
    314  1.20      fvdl 			nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);		\
    315  1.20      fvdl 			*tl++ = nfs_true;					\
    316  1.20      fvdl 			*tl = txdr_unsigned((a)->va_uid);			\
    317  1.20      fvdl 		} else {							\
    318  1.20      fvdl 			nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);		\
    319  1.20      fvdl 			*tl = nfs_false;					\
    320  1.20      fvdl 		}								\
    321  1.20      fvdl 		if ((full) && (a)->va_gid != (gid_t)VNOVAL) {			\
    322  1.20      fvdl 			nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);		\
    323  1.20      fvdl 			*tl++ = nfs_true;					\
    324  1.20      fvdl 			*tl = txdr_unsigned((a)->va_gid);			\
    325  1.20      fvdl 		} else {							\
    326  1.20      fvdl 			nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);		\
    327  1.20      fvdl 			*tl = nfs_false;					\
    328  1.20      fvdl 		}								\
    329  1.20      fvdl 		if ((full) && (a)->va_size != VNOVAL) {				\
    330  1.20      fvdl 			nfsm_build(tl, u_int32_t *, 3 * NFSX_UNSIGNED);		\
    331  1.20      fvdl 			*tl++ = nfs_true;					\
    332  1.20      fvdl 			txdr_hyper((a)->va_size, tl);				\
    333  1.20      fvdl 		} else {							\
    334  1.20      fvdl 			nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);		\
    335  1.20      fvdl 			*tl = nfs_false;					\
    336  1.20      fvdl 		}								\
    337  1.20      fvdl 		if ((a)->va_atime.tv_sec != VNOVAL) {				\
    338  1.45    kardel 			if ((a)->va_atime.tv_sec != time_second) {		\
    339  1.20      fvdl 				nfsm_build(tl, u_int32_t *, 3 * NFSX_UNSIGNED);	\
    340  1.20      fvdl 				*tl++ = txdr_unsigned(NFSV3SATTRTIME_TOCLIENT);	\
    341  1.20      fvdl 				txdr_nfsv3time(&(a)->va_atime, tl);		\
    342  1.20      fvdl 			} else {						\
    343  1.20      fvdl 				nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);	\
    344  1.20      fvdl 				*tl = txdr_unsigned(NFSV3SATTRTIME_TOSERVER);	\
    345  1.20      fvdl 			}							\
    346  1.20      fvdl 		} else {							\
    347  1.20      fvdl 			nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);		\
    348  1.20      fvdl 			*tl = txdr_unsigned(NFSV3SATTRTIME_DONTCHANGE);		\
    349  1.20      fvdl 		}								\
    350  1.20      fvdl 		if ((a)->va_mtime.tv_sec != VNOVAL) {				\
    351  1.45    kardel 			if ((a)->va_mtime.tv_sec != time_second) {		\
    352  1.20      fvdl 				nfsm_build(tl, u_int32_t *, 3 * NFSX_UNSIGNED);	\
    353  1.20      fvdl 				*tl++ = txdr_unsigned(NFSV3SATTRTIME_TOCLIENT);	\
    354  1.20      fvdl 				txdr_nfsv3time(&(a)->va_mtime, tl);		\
    355  1.20      fvdl 			} else {						\
    356  1.20      fvdl 				nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);	\
    357  1.20      fvdl 				*tl = txdr_unsigned(NFSV3SATTRTIME_TOSERVER);	\
    358  1.20      fvdl 			}							\
    359  1.20      fvdl 		} else {							\
    360  1.20      fvdl 			nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);		\
    361  1.20      fvdl 			*tl = txdr_unsigned(NFSV3SATTRTIME_DONTCHANGE);		\
    362  1.20      fvdl 		}								\
    363   1.9      fvdl 		}
    364  1.40     perry 
    365   1.1   mycroft 
    366   1.1   mycroft #define	nfsm_strsiz(s,m) \
    367  1.25      yamt 		{ nfsm_dissect(tl,uint32_t *,NFSX_UNSIGNED); \
    368  1.57  riastrad 		if ((uint32_t)((s) = fxdr_unsigned(uint32_t,*tl)) > (m)) { \
    369   1.1   mycroft 			m_freem(mrep); \
    370   1.1   mycroft 			error = EBADRPC; \
    371   1.1   mycroft 			goto nfsmout; \
    372   1.4   mycroft 		} }
    373   1.1   mycroft 
    374   1.9      fvdl #define	nfsm_srvnamesiz(s) \
    375  1.25      yamt 		{ nfsm_dissect(tl,uint32_t *,NFSX_UNSIGNED); \
    376  1.57  riastrad 		if ((uint32_t)((s) = fxdr_unsigned(uint32_t,*tl)) > \
    377  1.57  riastrad 		    NFS_MAXNAMLEN) \
    378   1.9      fvdl 			error = NFSERR_NAMETOL; \
    379   1.9      fvdl 		if (error) \
    380   1.9      fvdl 			nfsm_reply(0); \
    381   1.9      fvdl 		}
    382   1.9      fvdl 
    383   1.1   mycroft #define nfsm_mtouio(p,s) \
    384   1.1   mycroft 		if ((s) > 0 && \
    385   1.9      fvdl 		   (t1 = nfsm_mbuftouio(&md,(p),(s),&dpos)) != 0) { \
    386   1.9      fvdl 			error = t1; \
    387   1.1   mycroft 			m_freem(mrep); \
    388   1.1   mycroft 			goto nfsmout; \
    389   1.1   mycroft 		}
    390   1.1   mycroft 
    391   1.1   mycroft #define nfsm_uiotom(p,s) \
    392   1.9      fvdl 		if ((t1 = nfsm_uiotombuf((p),&mb,(s),&bpos)) != 0) { \
    393   1.9      fvdl 			error = t1; \
    394   1.1   mycroft 			m_freem(mreq); \
    395   1.1   mycroft 			goto nfsmout; \
    396   1.1   mycroft 		}
    397   1.1   mycroft 
    398  1.26  drochner #define	nfsm_reqhead(n,a,s) \
    399  1.26  drochner 		mb = mreq = nfsm_reqh((n),(a),(s),&bpos)
    400   1.1   mycroft 
    401   1.1   mycroft #define nfsm_reqdone	m_freem(mrep); \
    402  1.40     perry 		nfsmout:
    403   1.1   mycroft 
    404   1.1   mycroft #define nfsm_rndup(a)	(((a)+3)&(~0x3))
    405  1.28      yamt #define nfsm_padlen(a)	(nfsm_rndup(a) - (a))
    406   1.1   mycroft 
    407  1.37      yamt #define	nfsm_request1(v, t, p, c, rexmitp)	\
    408   1.8  christos 		if ((error = nfs_request((v), mreq, (t), (p), \
    409  1.37      yamt 		   (c), &mrep, &md, &dpos, (rexmitp))) != 0) { \
    410   1.9      fvdl 			if (error & NFSERR_RETERR) \
    411   1.9      fvdl 				error &= ~NFSERR_RETERR; \
    412   1.9      fvdl 			else \
    413   1.9      fvdl 				goto nfsmout; \
    414   1.9      fvdl 		}
    415   1.1   mycroft 
    416  1.37      yamt #define	nfsm_request(v, t, p, c)	nfsm_request1((v), (t), (p), (c), NULL)
    417  1.37      yamt 
    418   1.1   mycroft #define	nfsm_strtom(a,s,m) \
    419   1.1   mycroft 		if ((s) > (m)) { \
    420   1.1   mycroft 			m_freem(mreq); \
    421   1.1   mycroft 			error = ENAMETOOLONG; \
    422   1.1   mycroft 			goto nfsmout; \
    423   1.1   mycroft 		} \
    424   1.1   mycroft 		t2 = nfsm_rndup(s)+NFSX_UNSIGNED; \
    425   1.4   mycroft 		if (t2 <= M_TRAILINGSPACE(mb)) { \
    426   1.7       cgd 			nfsm_build(tl,u_int32_t *,t2); \
    427   1.1   mycroft 			*tl++ = txdr_unsigned(s); \
    428   1.1   mycroft 			*(tl+((t2>>2)-2)) = 0; \
    429  1.50  christos 			memcpy(tl, (const char *)(a), (s)); \
    430   1.9      fvdl 		} else if ((t2 = nfsm_strtmbuf(&mb, &bpos, (a), (s))) != 0) { \
    431   1.9      fvdl 			error = t2; \
    432   1.1   mycroft 			m_freem(mreq); \
    433   1.1   mycroft 			goto nfsmout; \
    434   1.1   mycroft 		}
    435   1.1   mycroft 
    436   1.1   mycroft #define	nfsm_srvdone \
    437   1.1   mycroft 		nfsmout: \
    438   1.1   mycroft 		return(error)
    439   1.1   mycroft 
    440   1.1   mycroft #define	nfsm_reply(s) \
    441   1.1   mycroft 		{ \
    442   1.4   mycroft 		nfsd->nd_repstat = error; \
    443   1.9      fvdl 		if (error && !(nfsd->nd_flag & ND_NFSV3)) \
    444   1.9      fvdl 		   (void) nfs_rephead(0, nfsd, slp, error, cache, &frev, \
    445   1.4   mycroft 			mrq, &mb, &bpos); \
    446   1.1   mycroft 		else \
    447   1.9      fvdl 		   (void) nfs_rephead((s), nfsd, slp, error, cache, &frev, \
    448   1.4   mycroft 			mrq, &mb, &bpos); \
    449  1.58       rin 		m_freem(mrep); \
    450  1.58       rin 		mrep = NULL; \
    451   1.1   mycroft 		mreq = *mrq; \
    452   1.9      fvdl 		if (error && (!(nfsd->nd_flag & ND_NFSV3) || \
    453  1.51    bouyer 			error == EBADRPC)) {\
    454  1.51    bouyer 			error = 0; \
    455  1.51    bouyer 			goto nfsmout; \
    456  1.51    bouyer 			} \
    457   1.1   mycroft 		}
    458   1.1   mycroft 
    459   1.9      fvdl #define	nfsm_writereply(s, v3) \
    460   1.9      fvdl 		{ \
    461   1.9      fvdl 		nfsd->nd_repstat = error; \
    462   1.9      fvdl 		if (error && !(v3)) \
    463   1.9      fvdl 		   (void) nfs_rephead(0, nfsd, slp, error, cache, &frev, \
    464   1.9      fvdl 			&mreq, &mb, &bpos); \
    465   1.9      fvdl 		else \
    466   1.9      fvdl 		   (void) nfs_rephead((s), nfsd, slp, error, cache, &frev, \
    467   1.9      fvdl 			&mreq, &mb, &bpos); \
    468   1.9      fvdl 		}
    469   1.9      fvdl 
    470   1.1   mycroft #define	nfsm_adv(s) \
    471  1.50  christos 		{ t1 = mtod(md, char *) + md->m_len - dpos; \
    472   1.1   mycroft 		if (t1 >= (s)) { \
    473   1.1   mycroft 			dpos += (s); \
    474   1.9      fvdl 		} else if ((t1 = nfs_adv(&md, &dpos, (s), t1)) != 0) { \
    475   1.9      fvdl 			error = t1; \
    476   1.1   mycroft 			m_freem(mrep); \
    477   1.1   mycroft 			goto nfsmout; \
    478   1.9      fvdl 		} }
    479   1.1   mycroft 
    480  1.47      yamt #define nfsm_srvmtofh(nsfh) \
    481  1.56  riastrad 	{ uint32_t fhlen = NFSX_V3FH; \
    482  1.16      fvdl 		if (nfsd->nd_flag & ND_NFSV3) { \
    483  1.56  riastrad 			nfsm_dissect(tl, uint32_t *, NFSX_UNSIGNED); \
    484  1.56  riastrad 			fhlen = fxdr_unsigned(uint32_t, *tl); \
    485  1.56  riastrad 			CTASSERT(NFSX_V3FHMAX <= FHANDLE_SIZE_MAX); \
    486  1.47      yamt 			if (fhlen > NFSX_V3FHMAX || \
    487  1.47      yamt 			    (fhlen < FHANDLE_SIZE_MIN && fhlen > 0)) { \
    488   1.9      fvdl 				error = EBADRPC; \
    489   1.9      fvdl 				nfsm_reply(0); \
    490   1.9      fvdl 			} \
    491  1.47      yamt 		} else { \
    492  1.56  riastrad 			CTASSERT(NFSX_V2FH >= FHANDLE_SIZE_MIN); \
    493  1.47      yamt 			fhlen = NFSX_V2FH; \
    494   1.9      fvdl 		} \
    495  1.47      yamt 		(nsfh)->nsfh_size = fhlen; \
    496  1.16      fvdl 		if (fhlen != 0) { \
    497  1.56  riastrad 			KASSERT(fhlen >= FHANDLE_SIZE_MIN); \
    498  1.56  riastrad 			KASSERT(fhlen <= FHANDLE_SIZE_MAX); \
    499  1.47      yamt 			nfsm_dissect(tl, u_int32_t *, fhlen); \
    500  1.47      yamt 			memcpy(NFSRVFH_DATA(nsfh), tl, fhlen); \
    501  1.16      fvdl 		} \
    502  1.16      fvdl 	}
    503   1.1   mycroft 
    504   1.1   mycroft #define	nfsm_clget \
    505   1.1   mycroft 		if (bp >= be) { \
    506   1.4   mycroft 			if (mp == mb) \
    507   1.4   mycroft 				mp->m_len += bp-bpos; \
    508  1.23      matt 			mp = m_get(M_WAIT, MT_DATA); \
    509  1.24      matt 			MCLAIM(mp, &nfs_mowner); \
    510  1.23      matt 			m_clget(mp, M_WAIT); \
    511   1.1   mycroft 			mp->m_len = NFSMSIZ(mp); \
    512   1.4   mycroft 			mp2->m_next = mp; \
    513   1.4   mycroft 			mp2 = mp; \
    514  1.50  christos 			bp = mtod(mp, char *); \
    515   1.1   mycroft 			be = bp+mp->m_len; \
    516   1.1   mycroft 		} \
    517   1.7       cgd 		tl = (u_int32_t *)bp
    518   1.1   mycroft 
    519   1.9      fvdl #define	nfsm_srvfillattr(a, f) \
    520   1.9      fvdl 		nfsm_srvfattr(nfsd, (a), (f))
    521   1.9      fvdl 
    522   1.9      fvdl #define nfsm_srvwcc_data(br, b, ar, a) \
    523   1.9      fvdl 		nfsm_srvwcc(nfsd, (br), (b), (ar), (a), &mb, &bpos)
    524   1.9      fvdl 
    525   1.9      fvdl #define nfsm_srvpostop_attr(r, a) \
    526   1.9      fvdl 		nfsm_srvpostopattr(nfsd, (r), (a), &mb, &bpos)
    527   1.9      fvdl 
    528   1.9      fvdl #define nfsm_srvsattr(a) \
    529  1.42      yamt 		{ \
    530  1.42      yamt 		nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
    531   1.9      fvdl 		if (*tl == nfs_true) { \
    532   1.9      fvdl 			nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
    533   1.9      fvdl 			(a)->va_mode = nfstov_mode(*tl); \
    534   1.9      fvdl 		} \
    535   1.9      fvdl 		nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
    536   1.9      fvdl 		if (*tl == nfs_true) { \
    537   1.9      fvdl 			nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
    538   1.9      fvdl 			(a)->va_uid = fxdr_unsigned(uid_t, *tl); \
    539   1.9      fvdl 		} \
    540   1.9      fvdl 		nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
    541   1.9      fvdl 		if (*tl == nfs_true) { \
    542   1.9      fvdl 			nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
    543   1.9      fvdl 			(a)->va_gid = fxdr_unsigned(gid_t, *tl); \
    544   1.9      fvdl 		} \
    545   1.9      fvdl 		nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
    546   1.9      fvdl 		if (*tl == nfs_true) { \
    547   1.9      fvdl 			nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED); \
    548  1.19      fair 			(a)->va_size = fxdr_hyper(tl); \
    549   1.9      fvdl 		} \
    550   1.9      fvdl 		nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
    551   1.9      fvdl 		switch (fxdr_unsigned(int, *tl)) { \
    552   1.9      fvdl 		case NFSV3SATTRTIME_TOCLIENT: \
    553   1.9      fvdl 			nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED); \
    554   1.9      fvdl 			fxdr_nfsv3time(tl, &(a)->va_atime); \
    555   1.9      fvdl 			break; \
    556   1.9      fvdl 		case NFSV3SATTRTIME_TOSERVER: \
    557  1.45    kardel 			getnanotime(&(a)->va_atime); \
    558  1.21  wrstuden 			(a)->va_vaflags |= VA_UTIMES_NULL; \
    559   1.9      fvdl 			break; \
    560   1.9      fvdl 		}; \
    561   1.9      fvdl 		nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
    562   1.9      fvdl 		switch (fxdr_unsigned(int, *tl)) { \
    563   1.9      fvdl 		case NFSV3SATTRTIME_TOCLIENT: \
    564   1.9      fvdl 			nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED); \
    565   1.9      fvdl 			fxdr_nfsv3time(tl, &(a)->va_mtime); \
    566  1.21  wrstuden 			(a)->va_vaflags &= ~VA_UTIMES_NULL; \
    567   1.9      fvdl 			break; \
    568   1.9      fvdl 		case NFSV3SATTRTIME_TOSERVER: \
    569  1.45    kardel 			getnanotime(&(a)->va_mtime); \
    570  1.21  wrstuden 			(a)->va_vaflags |= VA_UTIMES_NULL; \
    571   1.9      fvdl 			break; \
    572   1.9      fvdl 		}; }
    573   1.1   mycroft 
    574  1.59  riastrad #endif	/* _NFS_NFSM_SUBS_H_ */
    575