Home | History | Annotate | Line # | Download | only in nfs
nfsm_subs.h revision 1.14
      1 /*	$NetBSD: nfsm_subs.h,v 1.14 1997/02/24 23:26:20 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 #define nfsm_aligned(p) ALIGNED_POINTER(p,u_int32_t)
     96 
     97 #define	nfsm_dissect(a, c, s) \
     98 		{ t1 = mtod(md, caddr_t)+md->m_len-dpos; \
     99 		if (t1 >= (s) && nfsm_aligned(dpos)) { \
    100 			(a) = (c)(dpos); \
    101 			dpos += (s); \
    102 		} else if ((t1 = nfsm_disct(&md, &dpos, (s), t1, &cp2)) != 0){ \
    103 			error = t1; \
    104 			m_freem(mrep); \
    105 			goto nfsmout; \
    106 		} else { \
    107 			(a) = (c)cp2; \
    108 		} }
    109 
    110 #define nfsm_fhtom(v, v3) \
    111 	      { if (v3) { \
    112 			t2 = nfsm_rndup(VTONFS(v)->n_fhsize) + NFSX_UNSIGNED; \
    113 			if (t2 <= M_TRAILINGSPACE(mb)) { \
    114 				nfsm_build(tl, u_int32_t *, t2); \
    115 				*tl++ = txdr_unsigned(VTONFS(v)->n_fhsize); \
    116 				*(tl + ((t2>>2) - 2)) = 0; \
    117 				bcopy((caddr_t)VTONFS(v)->n_fhp,(caddr_t)tl, \
    118 					VTONFS(v)->n_fhsize); \
    119 			} else if ((t2 = nfsm_strtmbuf(&mb, &bpos, \
    120 				(caddr_t)VTONFS(v)->n_fhp, \
    121 				  VTONFS(v)->n_fhsize)) != 0) { \
    122 				error = t2; \
    123 				m_freem(mreq); \
    124 				goto nfsmout; \
    125 			} \
    126 		} else { \
    127 			nfsm_build(cp, caddr_t, NFSX_V2FH); \
    128 			bcopy((caddr_t)VTONFS(v)->n_fhp, cp, NFSX_V2FH); \
    129 		} }
    130 
    131 #define nfsm_srvfhtom(f, v3) \
    132 		{ if (v3) { \
    133 			nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED + NFSX_V3FH); \
    134 			*tl++ = txdr_unsigned(NFSX_V3FH); \
    135 			bcopy((caddr_t)(f), (caddr_t)tl, NFSX_V3FH); \
    136 		} else { \
    137 			nfsm_build(cp, caddr_t, NFSX_V2FH); \
    138 			bcopy((caddr_t)(f), cp, NFSX_V2FH); \
    139 		} }
    140 
    141 #define nfsm_srvpostop_fh(f) \
    142 		{ nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED + NFSX_V3FH); \
    143 		*tl++ = nfs_true; \
    144 		*tl++ = txdr_unsigned(NFSX_V3FH); \
    145 		bcopy((caddr_t)(f), (caddr_t)tl, NFSX_V3FH); \
    146 		}
    147 
    148 #define nfsm_mtofh(d, v, v3, f) \
    149 		{ struct nfsnode *ttnp; nfsfh_t *ttfhp; int ttfhsize; \
    150 		if (v3) { \
    151 			nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
    152 			(f) = fxdr_unsigned(int, *tl); \
    153 		} else \
    154 			(f) = 1; \
    155 		if (f) { \
    156 			nfsm_getfh(ttfhp, ttfhsize, (v3)); \
    157 			if ((t1 = nfs_nget((d)->v_mount, ttfhp, ttfhsize, \
    158 				&ttnp)) != 0) { \
    159 				error = t1; \
    160 				m_freem(mrep); \
    161 				goto nfsmout; \
    162 			} \
    163 			(v) = NFSTOV(ttnp); \
    164 		} \
    165 		if (v3) { \
    166 			nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
    167 			if (f) \
    168 				(f) = fxdr_unsigned(int, *tl); \
    169 			else if (fxdr_unsigned(int, *tl)) \
    170 				nfsm_adv(NFSX_V3FATTR); \
    171 		} \
    172 		if (f) \
    173 			nfsm_loadattr((v), (struct vattr *)0); \
    174 		}
    175 
    176 #define nfsm_getfh(f, s, v3) \
    177 		{ if (v3) { \
    178 			nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
    179 			if (((s) = fxdr_unsigned(int, *tl)) <= 0 || \
    180 				(s) > NFSX_V3FHMAX) { \
    181 				m_freem(mrep); \
    182 				error = EBADRPC; \
    183 				goto nfsmout; \
    184 			} \
    185 		} else \
    186 			(s) = NFSX_V2FH; \
    187 		nfsm_dissect((f), nfsfh_t *, nfsm_rndup(s)); }
    188 
    189 #define	nfsm_loadattr(v, a) \
    190 		{ struct vnode *ttvp = (v); \
    191 		if ((t1 = nfs_loadattrcache(&ttvp, &md, &dpos, (a))) != 0) { \
    192 			error = t1; \
    193 			m_freem(mrep); \
    194 			goto nfsmout; \
    195 		} \
    196 		(v) = ttvp; }
    197 
    198 #define	nfsm_postop_attr(v, f) \
    199 		{ struct vnode *ttvp = (v); \
    200 		nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
    201 		if (((f) = fxdr_unsigned(int, *tl)) != 0) { \
    202 			if ((t1 = nfs_loadattrcache(&ttvp, &md, &dpos, \
    203 				(struct vattr *)0)) != 0) { \
    204 				error = t1; \
    205 				(f) = 0; \
    206 				m_freem(mrep); \
    207 				goto nfsmout; \
    208 			} \
    209 			(v) = ttvp; \
    210 		} }
    211 
    212 /* Used as (f) for nfsm_wcc_data() */
    213 #define NFSV3_WCCRATTR	0
    214 #define NFSV3_WCCCHK	1
    215 
    216 #define	nfsm_wcc_data(v, f) \
    217 		{ int ttattrf, ttretf = 0; \
    218 		nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
    219 		if (*tl == nfs_true) { \
    220 			nfsm_dissect(tl, u_int32_t *, 6 * NFSX_UNSIGNED); \
    221 			if (f) \
    222 				ttretf = (VTONFS(v)->n_mtime == \
    223 					fxdr_unsigned(u_int32_t, *(tl + 2))); \
    224 		} \
    225 		nfsm_postop_attr((v), ttattrf); \
    226 		if (f) { \
    227 			(f) = ttretf; \
    228 		} else { \
    229 			(f) = ttattrf; \
    230 		} }
    231 
    232 #define nfsm_v3sattr(s, a) \
    233 		{ (s)->sa_modetrue = nfs_true; \
    234 		(s)->sa_mode = vtonfsv3_mode((a)->va_mode); \
    235 		(s)->sa_uidfalse = nfs_false; \
    236 		(s)->sa_gidfalse = nfs_false; \
    237 		(s)->sa_sizefalse = nfs_false; \
    238 		(s)->sa_atimetype = txdr_unsigned(NFSV3SATTRTIME_TOCLIENT); \
    239 		txdr_nfsv3time(&(a)->va_atime, &(s)->sa_atime); \
    240 		(s)->sa_mtimetype = txdr_unsigned(NFSV3SATTRTIME_TOCLIENT); \
    241 		txdr_nfsv3time(&(a)->va_mtime, &(s)->sa_mtime); \
    242 		}
    243 
    244 #define	nfsm_strsiz(s,m) \
    245 		{ nfsm_dissect(tl,u_int32_t *,NFSX_UNSIGNED); \
    246 		if (((s) = fxdr_unsigned(int32_t,*tl)) > (m)) { \
    247 			m_freem(mrep); \
    248 			error = EBADRPC; \
    249 			goto nfsmout; \
    250 		} }
    251 
    252 #define	nfsm_srvstrsiz(s,m) \
    253 		{ nfsm_dissect(tl,u_int32_t *,NFSX_UNSIGNED); \
    254 		if (((s) = fxdr_unsigned(int32_t,*tl)) > (m) || (s) <= 0) { \
    255 			error = EBADRPC; \
    256 			nfsm_reply(0); \
    257 		} }
    258 
    259 #define	nfsm_srvnamesiz(s) \
    260 		{ nfsm_dissect(tl,u_int32_t *,NFSX_UNSIGNED); \
    261 		if (((s) = fxdr_unsigned(int32_t,*tl)) > NFS_MAXNAMLEN) \
    262 			error = NFSERR_NAMETOL; \
    263 		if ((s) <= 0) \
    264 			error = EBADRPC; \
    265 		if (error) \
    266 			nfsm_reply(0); \
    267 		}
    268 
    269 #define nfsm_mtouio(p,s) \
    270 		if ((s) > 0 && \
    271 		   (t1 = nfsm_mbuftouio(&md,(p),(s),&dpos)) != 0) { \
    272 			error = t1; \
    273 			m_freem(mrep); \
    274 			goto nfsmout; \
    275 		}
    276 
    277 #define nfsm_uiotom(p,s) \
    278 		if ((t1 = nfsm_uiotombuf((p),&mb,(s),&bpos)) != 0) { \
    279 			error = t1; \
    280 			m_freem(mreq); \
    281 			goto nfsmout; \
    282 		}
    283 
    284 #define	nfsm_reqhead(v,a,s) \
    285 		mb = mreq = nfsm_reqh((v),(a),(s),&bpos)
    286 
    287 #define nfsm_reqdone	m_freem(mrep); \
    288 		nfsmout:
    289 
    290 #define nfsm_rndup(a)	(((a)+3)&(~0x3))
    291 
    292 #define	nfsm_request(v, t, p, c)	\
    293 		if ((error = nfs_request((v), mreq, (t), (p), \
    294 		   (c), &mrep, &md, &dpos)) != 0) { \
    295 			if (error & NFSERR_RETERR) \
    296 				error &= ~NFSERR_RETERR; \
    297 			else \
    298 				goto nfsmout; \
    299 		}
    300 
    301 #define	nfsm_strtom(a,s,m) \
    302 		if ((s) > (m)) { \
    303 			m_freem(mreq); \
    304 			error = ENAMETOOLONG; \
    305 			goto nfsmout; \
    306 		} \
    307 		t2 = nfsm_rndup(s)+NFSX_UNSIGNED; \
    308 		if (t2 <= M_TRAILINGSPACE(mb)) { \
    309 			nfsm_build(tl,u_int32_t *,t2); \
    310 			*tl++ = txdr_unsigned(s); \
    311 			*(tl+((t2>>2)-2)) = 0; \
    312 			bcopy((const char *)(a), (caddr_t)tl, (s)); \
    313 		} else if ((t2 = nfsm_strtmbuf(&mb, &bpos, (a), (s))) != 0) { \
    314 			error = t2; \
    315 			m_freem(mreq); \
    316 			goto nfsmout; \
    317 		}
    318 
    319 #define	nfsm_srvdone \
    320 		nfsmout: \
    321 		return(error)
    322 
    323 #define	nfsm_reply(s) \
    324 		{ \
    325 		nfsd->nd_repstat = error; \
    326 		if (error && !(nfsd->nd_flag & ND_NFSV3)) \
    327 		   (void) nfs_rephead(0, nfsd, slp, error, cache, &frev, \
    328 			mrq, &mb, &bpos); \
    329 		else \
    330 		   (void) nfs_rephead((s), nfsd, slp, error, cache, &frev, \
    331 			mrq, &mb, &bpos); \
    332 		if (mrep != NULL) { \
    333 			m_freem(mrep); \
    334 			mrep = NULL; \
    335 		} \
    336 		mreq = *mrq; \
    337 		if (error && (!(nfsd->nd_flag & ND_NFSV3) || \
    338 			error == EBADRPC)) \
    339 			return(0); \
    340 		}
    341 
    342 #define	nfsm_writereply(s, v3) \
    343 		{ \
    344 		nfsd->nd_repstat = error; \
    345 		if (error && !(v3)) \
    346 		   (void) nfs_rephead(0, nfsd, slp, error, cache, &frev, \
    347 			&mreq, &mb, &bpos); \
    348 		else \
    349 		   (void) nfs_rephead((s), nfsd, slp, error, cache, &frev, \
    350 			&mreq, &mb, &bpos); \
    351 		}
    352 
    353 #define	nfsm_adv(s) \
    354 		{ t1 = mtod(md, caddr_t)+md->m_len-dpos; \
    355 		if (t1 >= (s)) { \
    356 			dpos += (s); \
    357 		} else if ((t1 = nfs_adv(&md, &dpos, (s), t1)) != 0) { \
    358 			error = t1; \
    359 			m_freem(mrep); \
    360 			goto nfsmout; \
    361 		} }
    362 
    363 #define nfsm_srvmtofh(f) \
    364 		{ if (nfsd->nd_flag & ND_NFSV3) { \
    365 			nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
    366 			if (fxdr_unsigned(int, *tl) != NFSX_V3FH) { \
    367 				error = EBADRPC; \
    368 				nfsm_reply(0); \
    369 			} \
    370 		} \
    371 		nfsm_dissect(tl, u_int32_t *, NFSX_V3FH); \
    372 		bcopy((caddr_t)tl, (caddr_t)(f), NFSX_V3FH); \
    373 		if ((nfsd->nd_flag & ND_NFSV3) == 0) \
    374 			nfsm_adv(NFSX_V2FH - NFSX_V3FH); \
    375 		}
    376 
    377 #define	nfsm_clget \
    378 		if (bp >= be) { \
    379 			if (mp == mb) \
    380 				mp->m_len += bp-bpos; \
    381 			MGET(mp, M_WAIT, MT_DATA); \
    382 			MCLGET(mp, M_WAIT); \
    383 			mp->m_len = NFSMSIZ(mp); \
    384 			mp2->m_next = mp; \
    385 			mp2 = mp; \
    386 			bp = mtod(mp, caddr_t); \
    387 			be = bp+mp->m_len; \
    388 		} \
    389 		tl = (u_int32_t *)bp
    390 
    391 #define	nfsm_srvfillattr(a, f) \
    392 		nfsm_srvfattr(nfsd, (a), (f))
    393 
    394 #define nfsm_srvwcc_data(br, b, ar, a) \
    395 		nfsm_srvwcc(nfsd, (br), (b), (ar), (a), &mb, &bpos)
    396 
    397 #define nfsm_srvpostop_attr(r, a) \
    398 		nfsm_srvpostopattr(nfsd, (r), (a), &mb, &bpos)
    399 
    400 #define nfsm_srvsattr(a) \
    401 		{ nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
    402 		if (*tl == nfs_true) { \
    403 			nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
    404 			(a)->va_mode = nfstov_mode(*tl); \
    405 		} \
    406 		nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
    407 		if (*tl == nfs_true) { \
    408 			nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
    409 			(a)->va_uid = fxdr_unsigned(uid_t, *tl); \
    410 		} \
    411 		nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
    412 		if (*tl == nfs_true) { \
    413 			nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
    414 			(a)->va_gid = fxdr_unsigned(gid_t, *tl); \
    415 		} \
    416 		nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
    417 		if (*tl == nfs_true) { \
    418 			nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED); \
    419 			fxdr_hyper(tl, &(a)->va_size); \
    420 		} \
    421 		nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
    422 		switch (fxdr_unsigned(int, *tl)) { \
    423 		case NFSV3SATTRTIME_TOCLIENT: \
    424 			nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED); \
    425 			fxdr_nfsv3time(tl, &(a)->va_atime); \
    426 			break; \
    427 		case NFSV3SATTRTIME_TOSERVER: \
    428 			(a)->va_atime.tv_sec = time.tv_sec; \
    429 			(a)->va_atime.tv_nsec = time.tv_usec * 1000; \
    430 			break; \
    431 		}; \
    432 		nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
    433 		switch (fxdr_unsigned(int, *tl)) { \
    434 		case NFSV3SATTRTIME_TOCLIENT: \
    435 			nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED); \
    436 			fxdr_nfsv3time(tl, &(a)->va_mtime); \
    437 			break; \
    438 		case NFSV3SATTRTIME_TOSERVER: \
    439 			(a)->va_mtime.tv_sec = time.tv_sec; \
    440 			(a)->va_mtime.tv_nsec = time.tv_usec * 1000; \
    441 			break; \
    442 		}; }
    443 
    444 #endif
    445