nfsm_subs.h revision 1.19
1/*	$NetBSD: nfsm_subs.h,v 1.19 1999/03/06 05:34:41 fair 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)) ? (m)->m_ext.ext_size : \
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				memcpy((caddr_t)tl,(caddr_t)VTONFS(v)->n_fhp, \
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			memcpy(cp, (caddr_t)VTONFS(v)->n_fhp, 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			memcpy((caddr_t)tl, (caddr_t)(f), NFSX_V3FH); \
136		} else { \
137			nfsm_build(cp, caddr_t, NFSX_V2FH); \
138			memcpy(cp, (caddr_t)(f), 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		memcpy((caddr_t)tl, (caddr_t)(f), 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 = nfsm_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 = nfsm_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			memcpy((caddr_t)tl, (const char *)(a), (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	{ int fhlen = NFSX_V3FH; \
365		if (nfsd->nd_flag & ND_NFSV3) { \
366			nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
367			fhlen = fxdr_unsigned(int, *tl); \
368			if (fhlen == 0) { \
369				memset((caddr_t)(f), 0, NFSX_V3FH); \
370			} else if (fhlen != NFSX_V3FH) { \
371				error = EBADRPC; \
372				nfsm_reply(0); \
373			} \
374		} \
375		if (fhlen != 0) { \
376			nfsm_dissect(tl, u_int32_t *, NFSX_V3FH); \
377			memcpy( (caddr_t)(f), (caddr_t)tl, NFSX_V3FH); \
378			if ((nfsd->nd_flag & ND_NFSV3) == 0) \
379				nfsm_adv(NFSX_V2FH - NFSX_V3FH); \
380		} \
381	}
382
383#define	nfsm_clget \
384		if (bp >= be) { \
385			if (mp == mb) \
386				mp->m_len += bp-bpos; \
387			MGET(mp, M_WAIT, MT_DATA); \
388			MCLGET(mp, M_WAIT); \
389			mp->m_len = NFSMSIZ(mp); \
390			mp2->m_next = mp; \
391			mp2 = mp; \
392			bp = mtod(mp, caddr_t); \
393			be = bp+mp->m_len; \
394		} \
395		tl = (u_int32_t *)bp
396
397#define	nfsm_srvfillattr(a, f) \
398		nfsm_srvfattr(nfsd, (a), (f))
399
400#define nfsm_srvwcc_data(br, b, ar, a) \
401		nfsm_srvwcc(nfsd, (br), (b), (ar), (a), &mb, &bpos)
402
403#define nfsm_srvpostop_attr(r, a) \
404		nfsm_srvpostopattr(nfsd, (r), (a), &mb, &bpos)
405
406#define nfsm_srvsattr(a) \
407		{ nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
408		if (*tl == nfs_true) { \
409			nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
410			(a)->va_mode = nfstov_mode(*tl); \
411		} \
412		nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
413		if (*tl == nfs_true) { \
414			nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
415			(a)->va_uid = fxdr_unsigned(uid_t, *tl); \
416		} \
417		nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
418		if (*tl == nfs_true) { \
419			nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
420			(a)->va_gid = fxdr_unsigned(gid_t, *tl); \
421		} \
422		nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
423		if (*tl == nfs_true) { \
424			nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED); \
425			(a)->va_size = fxdr_hyper(tl); \
426		} \
427		nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
428		switch (fxdr_unsigned(int, *tl)) { \
429		case NFSV3SATTRTIME_TOCLIENT: \
430			nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED); \
431			fxdr_nfsv3time(tl, &(a)->va_atime); \
432			break; \
433		case NFSV3SATTRTIME_TOSERVER: \
434			(a)->va_atime.tv_sec = time.tv_sec; \
435			(a)->va_atime.tv_nsec = time.tv_usec * 1000; \
436			break; \
437		}; \
438		nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
439		switch (fxdr_unsigned(int, *tl)) { \
440		case NFSV3SATTRTIME_TOCLIENT: \
441			nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED); \
442			fxdr_nfsv3time(tl, &(a)->va_mtime); \
443			break; \
444		case NFSV3SATTRTIME_TOSERVER: \
445			(a)->va_mtime.tv_sec = time.tv_sec; \
446			(a)->va_mtime.tv_nsec = time.tv_usec * 1000; \
447			break; \
448		}; }
449
450#endif
451