Home | History | Annotate | Line # | Download | only in nfs
nfsm_subs.h revision 1.12
      1 /*	$NetBSD: nfsm_subs.h,v 1.12 1997/02/22 03:11:12 fvdl Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1989, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * This code is derived from software contributed to Berkeley by
      8  * Rick Macklem at The University of Guelph.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by the University of
     21  *	California, Berkeley and its contributors.
     22  * 4. Neither the name of the University nor the names of its contributors
     23  *    may be used to endorse or promote products derived from this software
     24  *    without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36  * SUCH DAMAGE.
     37  *
     38  *	@(#)nfsm_subs.h	8.2 (Berkeley) 3/30/95
     39  */
     40 
     41 
     42 #ifndef _NFS_NFSM_SUBS_H_
     43 #define _NFS_NFSM_SUBS_H_
     44 
     45 
     46 /*
     47  * These macros do strange and peculiar things to mbuf chains for
     48  * the assistance of the nfs code. To attempt to use them for any
     49  * other purpose will be dangerous. (they make weird assumptions)
     50  */
     51 
     52 /*
     53  * First define what the actual subs. return
     54  */
     55 
     56 #define	M_HASCL(m)	((m)->m_flags & M_EXT)
     57 #define	NFSMINOFF(m) \
     58 		if (M_HASCL(m)) \
     59 			(m)->m_data = (m)->m_ext.ext_buf; \
     60 		else if ((m)->m_flags & M_PKTHDR) \
     61 			(m)->m_data = (m)->m_pktdat; \
     62 		else \
     63 			(m)->m_data = (m)->m_dat
     64 #define	NFSMADV(m, s)	(m)->m_data += (s)
     65 #define	NFSMSIZ(m)	((M_HASCL(m))?MCLBYTES: \
     66 				(((m)->m_flags & M_PKTHDR)?MHLEN:MLEN))
     67 
     68 /*
     69  * Now for the macros that do the simple stuff and call the functions
     70  * for the hard stuff.
     71  * These macros use several vars. declared in nfsm_reqhead and these
     72  * vars. must not be used elsewhere unless you are careful not to corrupt
     73  * them. The vars. starting with pN and tN (N=1,2,3,..) are temporaries
     74  * that may be used so long as the value is not expected to retained
     75  * after a macro.
     76  * I know, this is kind of dorkey, but it makes the actual op functions
     77  * fairly clean and deals with the mess caused by the xdr discriminating
     78  * unions.
     79  */
     80 
     81 #define	nfsm_build(a,c,s) \
     82 		{ if ((s) > M_TRAILINGSPACE(mb)) { \
     83 			MGET(mb2, M_WAIT, MT_DATA); \
     84 			if ((s) > MLEN) \
     85 				panic("build > MLEN"); \
     86 			mb->m_next = mb2; \
     87 			mb = mb2; \
     88 			mb->m_len = 0; \
     89 			bpos = mtod(mb, caddr_t); \
     90 		} \
     91 		(a) = (c)(bpos); \
     92 		mb->m_len += (s); \
     93 		bpos += (s); }
     94 
     95 /*
     96  * XXX this is wrong for 64bit architectures, but we're dealing with 32bit
     97  * chunks only here and assume (safely) that they can be addressed on
     98  * 32bit boundaries. This needs to be changed if 64bit quantities will
     99  * be extracted from the mbufs. Also, this isn't even needed for the i386,
    100  * it handles misaligned accesses. So the ALIGN macro doesn't quite cut
    101  * it here.
    102  */
    103 #ifdef __i386__
    104 #define nfsm_aligned(p) 1
    105 #else
    106 #define nfsm_aligned(p) (((int)(p) & 3) == 0)
    107 #endif
    108 
    109 #define	nfsm_dissect(a, c, s) \
    110 		{ t1 = mtod(md, caddr_t)+md->m_len-dpos; \
    111 		if (t1 >= (s) && nfsm_aligned(dpos)) { \
    112 			(a) = (c)(dpos); \
    113 			dpos += (s); \
    114 		} else if ((t1 = nfsm_disct(&md, &dpos, (s), t1, &cp2)) != 0){ \
    115 			error = t1; \
    116 			m_freem(mrep); \
    117 			goto nfsmout; \
    118 		} else { \
    119 			(a) = (c)cp2; \
    120 		} }
    121 
    122 #define nfsm_fhtom(v, v3) \
    123 	      { if (v3) { \
    124 			t2 = nfsm_rndup(VTONFS(v)->n_fhsize) + NFSX_UNSIGNED; \
    125 			if (t2 <= M_TRAILINGSPACE(mb)) { \
    126 				nfsm_build(tl, u_int32_t *, t2); \
    127 				*tl++ = txdr_unsigned(VTONFS(v)->n_fhsize); \
    128 				*(tl + ((t2>>2) - 2)) = 0; \
    129 				bcopy((caddr_t)VTONFS(v)->n_fhp,(caddr_t)tl, \
    130 					VTONFS(v)->n_fhsize); \
    131 			} else if ((t2 = nfsm_strtmbuf(&mb, &bpos, \
    132 				(caddr_t)VTONFS(v)->n_fhp, \
    133 				  VTONFS(v)->n_fhsize)) != 0) { \
    134 				error = t2; \
    135 				m_freem(mreq); \
    136 				goto nfsmout; \
    137 			} \
    138 		} else { \
    139 			nfsm_build(cp, caddr_t, NFSX_V2FH); \
    140 			bcopy((caddr_t)VTONFS(v)->n_fhp, cp, NFSX_V2FH); \
    141 		} }
    142 
    143 #define nfsm_srvfhtom(f, v3) \
    144 		{ if (v3) { \
    145 			nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED + NFSX_V3FH); \
    146 			*tl++ = txdr_unsigned(NFSX_V3FH); \
    147 			bcopy((caddr_t)(f), (caddr_t)tl, NFSX_V3FH); \
    148 		} else { \
    149 			nfsm_build(cp, caddr_t, NFSX_V2FH); \
    150 			bcopy((caddr_t)(f), cp, NFSX_V2FH); \
    151 		} }
    152 
    153 #define nfsm_srvpostop_fh(f) \
    154 		{ nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED + NFSX_V3FH); \
    155 		*tl++ = nfs_true; \
    156 		*tl++ = txdr_unsigned(NFSX_V3FH); \
    157 		bcopy((caddr_t)(f), (caddr_t)tl, NFSX_V3FH); \
    158 		}
    159 
    160 #define nfsm_mtofh(d, v, v3, f) \
    161 		{ struct nfsnode *ttnp; nfsfh_t *ttfhp; int ttfhsize; \
    162 		if (v3) { \
    163 			nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
    164 			(f) = fxdr_unsigned(int, *tl); \
    165 		} else \
    166 			(f) = 1; \
    167 		if (f) { \
    168 			nfsm_getfh(ttfhp, ttfhsize, (v3)); \
    169 			if ((t1 = nfs_nget((d)->v_mount, ttfhp, ttfhsize, \
    170 				&ttnp)) != 0) { \
    171 				error = t1; \
    172 				m_freem(mrep); \
    173 				goto nfsmout; \
    174 			} \
    175 			(v) = NFSTOV(ttnp); \
    176 		} \
    177 		if (v3) { \
    178 			nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
    179 			if (f) \
    180 				(f) = fxdr_unsigned(int, *tl); \
    181 			else if (fxdr_unsigned(int, *tl)) \
    182 				nfsm_adv(NFSX_V3FATTR); \
    183 		} \
    184 		if (f) \
    185 			nfsm_loadattr((v), (struct vattr *)0); \
    186 		}
    187 
    188 #define nfsm_getfh(f, s, v3) \
    189 		{ if (v3) { \
    190 			nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
    191 			if (((s) = fxdr_unsigned(int, *tl)) <= 0 || \
    192 				(s) > NFSX_V3FHMAX) { \
    193 				m_freem(mrep); \
    194 				error = EBADRPC; \
    195 				goto nfsmout; \
    196 			} \
    197 		} else \
    198 			(s) = NFSX_V2FH; \
    199 		nfsm_dissect((f), nfsfh_t *, nfsm_rndup(s)); }
    200 
    201 #define	nfsm_loadattr(v, a) \
    202 		{ struct vnode *ttvp = (v); \
    203 		if ((t1 = nfs_loadattrcache(&ttvp, &md, &dpos, (a))) != 0) { \
    204 			error = t1; \
    205 			m_freem(mrep); \
    206 			goto nfsmout; \
    207 		} \
    208 		(v) = ttvp; }
    209 
    210 #define	nfsm_postop_attr(v, f) \
    211 		{ struct vnode *ttvp = (v); \
    212 		nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
    213 		if (((f) = fxdr_unsigned(int, *tl)) != 0) { \
    214 			if ((t1 = nfs_loadattrcache(&ttvp, &md, &dpos, \
    215 				(struct vattr *)0)) != 0) { \
    216 				error = t1; \
    217 				(f) = 0; \
    218 				m_freem(mrep); \
    219 				goto nfsmout; \
    220 			} \
    221 			(v) = ttvp; \
    222 		} }
    223 
    224 /* Used as (f) for nfsm_wcc_data() */
    225 #define NFSV3_WCCRATTR	0
    226 #define NFSV3_WCCCHK	1
    227 
    228 #define	nfsm_wcc_data(v, f) \
    229 		{ int ttattrf, ttretf = 0; \
    230 		nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
    231 		if (*tl == nfs_true) { \
    232 			nfsm_dissect(tl, u_int32_t *, 6 * NFSX_UNSIGNED); \
    233 			if (f) \
    234 				ttretf = (VTONFS(v)->n_mtime == \
    235 					fxdr_unsigned(u_int32_t, *(tl + 2))); \
    236 		} \
    237 		nfsm_postop_attr((v), ttattrf); \
    238 		if (f) { \
    239 			(f) = ttretf; \
    240 		} else { \
    241 			(f) = ttattrf; \
    242 		} }
    243 
    244 #define nfsm_v3sattr(s, a) \
    245 		{ (s)->sa_modetrue = nfs_true; \
    246 		(s)->sa_mode = vtonfsv3_mode((a)->va_mode); \
    247 		(s)->sa_uidfalse = nfs_false; \
    248 		(s)->sa_gidfalse = nfs_false; \
    249 		(s)->sa_sizefalse = nfs_false; \
    250 		(s)->sa_atimetype = txdr_unsigned(NFSV3SATTRTIME_TOCLIENT); \
    251 		txdr_nfsv3time(&(a)->va_atime, &(s)->sa_atime); \
    252 		(s)->sa_mtimetype = txdr_unsigned(NFSV3SATTRTIME_TOCLIENT); \
    253 		txdr_nfsv3time(&(a)->va_mtime, &(s)->sa_mtime); \
    254 		}
    255 
    256 #define	nfsm_strsiz(s,m) \
    257 		{ nfsm_dissect(tl,u_int32_t *,NFSX_UNSIGNED); \
    258 		if (((s) = fxdr_unsigned(int32_t,*tl)) > (m)) { \
    259 			m_freem(mrep); \
    260 			error = EBADRPC; \
    261 			goto nfsmout; \
    262 		} }
    263 
    264 #define	nfsm_srvstrsiz(s,m) \
    265 		{ nfsm_dissect(tl,u_int32_t *,NFSX_UNSIGNED); \
    266 		if (((s) = fxdr_unsigned(int32_t,*tl)) > (m) || (s) <= 0) { \
    267 			error = EBADRPC; \
    268 			nfsm_reply(0); \
    269 		} }
    270 
    271 #define	nfsm_srvnamesiz(s) \
    272 		{ nfsm_dissect(tl,u_int32_t *,NFSX_UNSIGNED); \
    273 		if (((s) = fxdr_unsigned(int32_t,*tl)) > NFS_MAXNAMLEN) \
    274 			error = NFSERR_NAMETOL; \
    275 		if ((s) <= 0) \
    276 			error = EBADRPC; \
    277 		if (error) \
    278 			nfsm_reply(0); \
    279 		}
    280 
    281 #define nfsm_mtouio(p,s) \
    282 		if ((s) > 0 && \
    283 		   (t1 = nfsm_mbuftouio(&md,(p),(s),&dpos)) != 0) { \
    284 			error = t1; \
    285 			m_freem(mrep); \
    286 			goto nfsmout; \
    287 		}
    288 
    289 #define nfsm_uiotom(p,s) \
    290 		if ((t1 = nfsm_uiotombuf((p),&mb,(s),&bpos)) != 0) { \
    291 			error = t1; \
    292 			m_freem(mreq); \
    293 			goto nfsmout; \
    294 		}
    295 
    296 #define	nfsm_reqhead(v,a,s) \
    297 		mb = mreq = nfsm_reqh((v),(a),(s),&bpos)
    298 
    299 #define nfsm_reqdone	m_freem(mrep); \
    300 		nfsmout:
    301 
    302 #define nfsm_rndup(a)	(((a)+3)&(~0x3))
    303 
    304 #define	nfsm_request(v, t, p, c)	\
    305 		if ((error = nfs_request((v), mreq, (t), (p), \
    306 		   (c), &mrep, &md, &dpos)) != 0) { \
    307 			if (error & NFSERR_RETERR) \
    308 				error &= ~NFSERR_RETERR; \
    309 			else \
    310 				goto nfsmout; \
    311 		}
    312 
    313 #define	nfsm_strtom(a,s,m) \
    314 		if ((s) > (m)) { \
    315 			m_freem(mreq); \
    316 			error = ENAMETOOLONG; \
    317 			goto nfsmout; \
    318 		} \
    319 		t2 = nfsm_rndup(s)+NFSX_UNSIGNED; \
    320 		if (t2 <= M_TRAILINGSPACE(mb)) { \
    321 			nfsm_build(tl,u_int32_t *,t2); \
    322 			*tl++ = txdr_unsigned(s); \
    323 			*(tl+((t2>>2)-2)) = 0; \
    324 			bcopy((const char *)(a), (caddr_t)tl, (s)); \
    325 		} else if ((t2 = nfsm_strtmbuf(&mb, &bpos, (a), (s))) != 0) { \
    326 			error = t2; \
    327 			m_freem(mreq); \
    328 			goto nfsmout; \
    329 		}
    330 
    331 #define	nfsm_srvdone \
    332 		nfsmout: \
    333 		return(error)
    334 
    335 #define	nfsm_reply(s) \
    336 		{ \
    337 		nfsd->nd_repstat = error; \
    338 		if (error && !(nfsd->nd_flag & ND_NFSV3)) \
    339 		   (void) nfs_rephead(0, nfsd, slp, error, cache, &frev, \
    340 			mrq, &mb, &bpos); \
    341 		else \
    342 		   (void) nfs_rephead((s), nfsd, slp, error, cache, &frev, \
    343 			mrq, &mb, &bpos); \
    344 		if (mrep != NULL) { \
    345 			m_freem(mrep); \
    346 			mrep = NULL; \
    347 		} \
    348 		mreq = *mrq; \
    349 		if (error && (!(nfsd->nd_flag & ND_NFSV3) || \
    350 			error == EBADRPC)) \
    351 			return(0); \
    352 		}
    353 
    354 #define	nfsm_writereply(s, v3) \
    355 		{ \
    356 		nfsd->nd_repstat = error; \
    357 		if (error && !(v3)) \
    358 		   (void) nfs_rephead(0, nfsd, slp, error, cache, &frev, \
    359 			&mreq, &mb, &bpos); \
    360 		else \
    361 		   (void) nfs_rephead((s), nfsd, slp, error, cache, &frev, \
    362 			&mreq, &mb, &bpos); \
    363 		}
    364 
    365 #define	nfsm_adv(s) \
    366 		{ t1 = mtod(md, caddr_t)+md->m_len-dpos; \
    367 		if (t1 >= (s)) { \
    368 			dpos += (s); \
    369 		} else if ((t1 = nfs_adv(&md, &dpos, (s), t1)) != 0) { \
    370 			error = t1; \
    371 			m_freem(mrep); \
    372 			goto nfsmout; \
    373 		} }
    374 
    375 #define nfsm_srvmtofh(f) \
    376 		{ if (nfsd->nd_flag & ND_NFSV3) { \
    377 			nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
    378 			if (fxdr_unsigned(int, *tl) != NFSX_V3FH) { \
    379 				error = EBADRPC; \
    380 				nfsm_reply(0); \
    381 			} \
    382 		} \
    383 		nfsm_dissect(tl, u_int32_t *, NFSX_V3FH); \
    384 		bcopy((caddr_t)tl, (caddr_t)(f), NFSX_V3FH); \
    385 		if ((nfsd->nd_flag & ND_NFSV3) == 0) \
    386 			nfsm_adv(NFSX_V2FH - NFSX_V3FH); \
    387 		}
    388 
    389 #define	nfsm_clget \
    390 		if (bp >= be) { \
    391 			if (mp == mb) \
    392 				mp->m_len += bp-bpos; \
    393 			MGET(mp, M_WAIT, MT_DATA); \
    394 			MCLGET(mp, M_WAIT); \
    395 			mp->m_len = NFSMSIZ(mp); \
    396 			mp2->m_next = mp; \
    397 			mp2 = mp; \
    398 			bp = mtod(mp, caddr_t); \
    399 			be = bp+mp->m_len; \
    400 		} \
    401 		tl = (u_int32_t *)bp
    402 
    403 #define	nfsm_srvfillattr(a, f) \
    404 		nfsm_srvfattr(nfsd, (a), (f))
    405 
    406 #define nfsm_srvwcc_data(br, b, ar, a) \
    407 		nfsm_srvwcc(nfsd, (br), (b), (ar), (a), &mb, &bpos)
    408 
    409 #define nfsm_srvpostop_attr(r, a) \
    410 		nfsm_srvpostopattr(nfsd, (r), (a), &mb, &bpos)
    411 
    412 #define nfsm_srvsattr(a) \
    413 		{ nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
    414 		if (*tl == nfs_true) { \
    415 			nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
    416 			(a)->va_mode = nfstov_mode(*tl); \
    417 		} \
    418 		nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
    419 		if (*tl == nfs_true) { \
    420 			nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
    421 			(a)->va_uid = fxdr_unsigned(uid_t, *tl); \
    422 		} \
    423 		nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
    424 		if (*tl == nfs_true) { \
    425 			nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
    426 			(a)->va_gid = fxdr_unsigned(gid_t, *tl); \
    427 		} \
    428 		nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
    429 		if (*tl == nfs_true) { \
    430 			nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED); \
    431 			fxdr_hyper(tl, &(a)->va_size); \
    432 		} \
    433 		nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
    434 		switch (fxdr_unsigned(int, *tl)) { \
    435 		case NFSV3SATTRTIME_TOCLIENT: \
    436 			nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED); \
    437 			fxdr_nfsv3time(tl, &(a)->va_atime); \
    438 			break; \
    439 		case NFSV3SATTRTIME_TOSERVER: \
    440 			(a)->va_atime.tv_sec = time.tv_sec; \
    441 			(a)->va_atime.tv_nsec = time.tv_usec * 1000; \
    442 			break; \
    443 		}; \
    444 		nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
    445 		switch (fxdr_unsigned(int, *tl)) { \
    446 		case NFSV3SATTRTIME_TOCLIENT: \
    447 			nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED); \
    448 			fxdr_nfsv3time(tl, &(a)->va_mtime); \
    449 			break; \
    450 		case NFSV3SATTRTIME_TOSERVER: \
    451 			(a)->va_mtime.tv_sec = time.tv_sec; \
    452 			(a)->va_mtime.tv_nsec = time.tv_usec * 1000; \
    453 			break; \
    454 		}; }
    455 
    456 #endif
    457