xref: /csrg-svn/sys/nfs/nfsm_subs.h (revision 38414)
1*38414Smckusick /*
2*38414Smckusick  * Copyright (c) 1989 The Regents of the University of California.
3*38414Smckusick  * All rights reserved.
4*38414Smckusick  *
5*38414Smckusick  * This code is derived from software contributed to Berkeley by
6*38414Smckusick  * Rick Macklem at The University of Guelph.
7*38414Smckusick  *
8*38414Smckusick  * Redistribution and use in source and binary forms are permitted
9*38414Smckusick  * provided that the above copyright notice and this paragraph are
10*38414Smckusick  * duplicated in all such forms and that any documentation,
11*38414Smckusick  * advertising materials, and other materials related to such
12*38414Smckusick  * distribution and use acknowledge that the software was developed
13*38414Smckusick  * by the University of California, Berkeley.  The name of the
14*38414Smckusick  * University may not be used to endorse or promote products derived
15*38414Smckusick  * from this software without specific prior written permission.
16*38414Smckusick  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
17*38414Smckusick  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
18*38414Smckusick  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
19*38414Smckusick  *
20*38414Smckusick  *	@(#)nfsm_subs.h	7.1 (Berkeley) 07/05/89
21*38414Smckusick  */
22*38414Smckusick 
23*38414Smckusick /*
24*38414Smckusick  * These macros do strange and peculiar things to mbuf chains for
25*38414Smckusick  * the assistance of the nfs code. To attempt to use them for any
26*38414Smckusick  * other purpose will be dangerous. (they make weird assumptions)
27*38414Smckusick  */
28*38414Smckusick 
29*38414Smckusick /*
30*38414Smckusick  * First define what the actual subs. return
31*38414Smckusick  */
32*38414Smckusick struct mbuf *nfsm_reqh();
33*38414Smckusick struct vnode *nfs_fhtovp();
34*38414Smckusick 
35*38414Smckusick /*
36*38414Smckusick  * To try and deal with different variants of mbuf.h, I have used the
37*38414Smckusick  * following defs. If M_HASCL is not defined in an older the 4.4bsd mbuf.h,
38*38414Smckusick  * you will have to use a different ifdef
39*38414Smckusick  */
40*38414Smckusick #ifdef M_HASCL
41*38414Smckusick #define	NFSMCLGET(m, w)	MCLGET(m)
42*38414Smckusick #define	NFSMGETHDR(m)	MGET(m, M_WAIT, MT_DATA)
43*38414Smckusick #define	MHLEN		MLEN
44*38414Smckusick #define	NFSMINOFF(m) \
45*38414Smckusick 		if (M_HASCL(m)) \
46*38414Smckusick 			(m)->m_off = ((int)MTOCL(m))-(int)(m); \
47*38414Smckusick 		else \
48*38414Smckusick 			(m)->m_off = MMINOFF
49*38414Smckusick #define	NFSMADV(m, s)	(m)->m_off += (s)
50*38414Smckusick #define	NFSMSIZ(m)	((M_HASCL(m))?MCLBYTES:MLEN)
51*38414Smckusick #define	m_nextpkt	m_act
52*38414Smckusick #define	NFSMCOPY(m, o, l, w)	m_copy((m), (o), (l))
53*38414Smckusick #else
54*38414Smckusick #define	M_HASCL(m)	((m)->m_flags & M_EXT)
55*38414Smckusick #define	NFSMCLGET	MCLGET
56*38414Smckusick #define	NFSMGETHDR(m) \
57*38414Smckusick 		MGETHDR(m, M_WAIT, MT_DATA); \
58*38414Smckusick 		(m)->m_pkthdr.len = 0; \
59*38414Smckusick 		(m)->m_pkthdr.rcvif = (struct ifnet *)0
60*38414Smckusick #define	NFSMINOFF(m) \
61*38414Smckusick 		if (M_HASCL(m)) \
62*38414Smckusick 			(m)->m_data = (m)->m_ext.ext_buf; \
63*38414Smckusick 		else \
64*38414Smckusick 			(m)->m_data = (m)->m_dat
65*38414Smckusick #define	NFSMADV(m, s)	(m)->m_data += (s)
66*38414Smckusick #define	NFSMSIZ(m)	((M_HASCL(m))?(MCLBYTES-max_hdr): \
67*38414Smckusick 				(((m)->m_flags & M_PKTHDR)?MHLEN:MLEN))
68*38414Smckusick #define	NFSMCOPY	m_copym
69*38414Smckusick #endif
70*38414Smckusick 
71*38414Smckusick #ifndef MCLBYTES
72*38414Smckusick #define	MCLBYTES	CLBYTES
73*38414Smckusick #endif
74*38414Smckusick 
75*38414Smckusick #ifndef MT_CONTROL
76*38414Smckusick #define	MT_CONTROL	MT_RIGHTS
77*38414Smckusick #endif
78*38414Smckusick 
79*38414Smckusick /*
80*38414Smckusick  * Now for the macros that do the simple stuff and call the functions
81*38414Smckusick  * for the hard stuff.
82*38414Smckusick  * These macros use several vars. declared in nfsm_reqhead and these
83*38414Smckusick  * vars. must not be used elsewhere unless you are careful not to corrupt
84*38414Smckusick  * them. The vars. starting with pN and tN (N=1,2,3,..) are temporaries
85*38414Smckusick  * that may be used so long as the value is not expected to retained
86*38414Smckusick  * after a macro.
87*38414Smckusick  * I know, this is kind of dorkey, but it makes the actual op functions
88*38414Smckusick  * fairly clean and deals with the mess caused by the xdr discriminating
89*38414Smckusick  * unions.
90*38414Smckusick  */
91*38414Smckusick 
92*38414Smckusick #define	nfsm_build(a,c,s) \
93*38414Smckusick 		t1 = NFSMSIZ(mb); \
94*38414Smckusick 		if ((s) > (t1-mb->m_len)) { \
95*38414Smckusick 			MGET(mb2, M_WAIT, MT_DATA); \
96*38414Smckusick 			if ((s) > MLEN) \
97*38414Smckusick 				panic("build > MLEN"); \
98*38414Smckusick 			mb->m_next = mb2; \
99*38414Smckusick 			mb = mb2; \
100*38414Smckusick 			mb->m_len = 0; \
101*38414Smckusick 			bpos = mtod(mb, caddr_t); \
102*38414Smckusick 		} \
103*38414Smckusick 		(a) = (c)(bpos); \
104*38414Smckusick 		mb->m_len += (s); \
105*38414Smckusick 		bpos += (s)
106*38414Smckusick 
107*38414Smckusick #define	nfsm_disect(a,c,s) \
108*38414Smckusick 		t1 = mtod(md, caddr_t)+md->m_len-dpos; \
109*38414Smckusick 		if (t1 >= (s)) { \
110*38414Smckusick 			(a) = (c)(dpos); \
111*38414Smckusick 			dpos += (s); \
112*38414Smckusick 		} else if (error = nfsm_disct(&md, &dpos, (s), t1, TRUE, &cp2)) { \
113*38414Smckusick 			m_freem(mrep); \
114*38414Smckusick 			goto nfsmout; \
115*38414Smckusick 		} else { \
116*38414Smckusick 			(a) = (c)cp2; \
117*38414Smckusick 		}
118*38414Smckusick 
119*38414Smckusick #define	nfsm_disecton(a,c,s) \
120*38414Smckusick 		t1 = mtod(md, caddr_t)+md->m_len-dpos; \
121*38414Smckusick 		if (t1 >= (s)) { \
122*38414Smckusick 			(a) = (c)(dpos); \
123*38414Smckusick 			dpos += (s); \
124*38414Smckusick 		} else if (error = nfsm_disct(&md, &dpos, (s), t1, FALSE, &cp2)) { \
125*38414Smckusick 			m_freem(mrep); \
126*38414Smckusick 			goto nfsmout; \
127*38414Smckusick 		} else { \
128*38414Smckusick 			(a) = (c)cp2; \
129*38414Smckusick 		}
130*38414Smckusick 
131*38414Smckusick #define nfsm_fhtom(v) \
132*38414Smckusick 		nfsm_build(cp,caddr_t,NFSX_FH); \
133*38414Smckusick 		bcopy((caddr_t)&(VTONFS(v)->n_fh), cp, NFSX_FH)
134*38414Smckusick 
135*38414Smckusick #define nfsm_srvfhtom(f) \
136*38414Smckusick 		nfsm_build(cp,caddr_t,NFSX_FH); \
137*38414Smckusick 		bcopy((caddr_t)(f), cp, NFSX_FH)
138*38414Smckusick 
139*38414Smckusick #define nfsm_mtofh(d,v) \
140*38414Smckusick 		{ struct nfsnode *np; nfsv2fh_t *fhp; \
141*38414Smckusick 		nfsm_disect(fhp,nfsv2fh_t *,NFSX_FH); \
142*38414Smckusick 		if (error = nfs_nget((d)->v_mount, fhp, &np)) { \
143*38414Smckusick 			m_freem(mrep); \
144*38414Smckusick 			goto nfsmout; \
145*38414Smckusick 		} \
146*38414Smckusick 		(v) = NFSTOV(np); \
147*38414Smckusick 		nfsm_loadattr(v, (struct vattr *)0); \
148*38414Smckusick 		(v)->v_type = np->n_vattr.va_type; \
149*38414Smckusick 		}
150*38414Smckusick 
151*38414Smckusick #define	nfsm_loadattr(v,a) \
152*38414Smckusick 		if (error = nfs_loadattrcache((v), &md, &dpos, (a))) { \
153*38414Smckusick 			m_freem(mrep); \
154*38414Smckusick 			goto nfsmout; \
155*38414Smckusick 		}
156*38414Smckusick 
157*38414Smckusick #define	nfsm_strsiz(s,m) \
158*38414Smckusick 		nfsm_disect(p,u_long *,NFSX_UNSIGNED); \
159*38414Smckusick 		if (((s) = fxdr_unsigned(long,*p)) > (m)) { \
160*38414Smckusick 			m_freem(mrep); \
161*38414Smckusick 			error = EBADRPC; \
162*38414Smckusick 			goto nfsmout; \
163*38414Smckusick 		}
164*38414Smckusick 
165*38414Smckusick #define	nfsm_srvstrsiz(s,m) \
166*38414Smckusick 		nfsm_disect(p,u_long *,NFSX_UNSIGNED); \
167*38414Smckusick 		if (((s) = fxdr_unsigned(long,*p)) > (m) || (s) <= 0) { \
168*38414Smckusick 			error = EBADRPC; \
169*38414Smckusick 			nfsm_reply(0); \
170*38414Smckusick 		}
171*38414Smckusick 
172*38414Smckusick #define	nfsm_srvstrsizon(s,m) \
173*38414Smckusick 		nfsm_disecton(p,u_long *,NFSX_UNSIGNED); \
174*38414Smckusick 		if (((s) = fxdr_unsigned(long,*p)) > (m)) { \
175*38414Smckusick 			error = EBADRPC; \
176*38414Smckusick 			nfsm_reply(0); \
177*38414Smckusick 		}
178*38414Smckusick 
179*38414Smckusick #define nfsm_mtouio(p,s) \
180*38414Smckusick 		if ((s) > 0 && \
181*38414Smckusick 		   (error = nfsm_mbuftouio(&md,(p),(s),&dpos))) { \
182*38414Smckusick 			m_freem(mrep); \
183*38414Smckusick 			goto nfsmout; \
184*38414Smckusick 		}
185*38414Smckusick 
186*38414Smckusick #define nfsm_uiotom(p,s) \
187*38414Smckusick 		if (error = nfsm_uiotombuf((p),&mb,(s),&bpos)) { \
188*38414Smckusick 			m_freem(mreq); \
189*38414Smckusick 			goto nfsmout; \
190*38414Smckusick 		}
191*38414Smckusick 
192*38414Smckusick #define	nfsm_reqhead(a,c,s) \
193*38414Smckusick 		if ((mreq = nfsm_reqh(nfs_prog,nfs_vers,(a),(c),(s),&bpos,&mb,&xid)) == NULL) { \
194*38414Smckusick 			error = ENOBUFS; \
195*38414Smckusick 			goto nfsmout; \
196*38414Smckusick 		}
197*38414Smckusick 
198*38414Smckusick #define	nfsm_vars \
199*38414Smckusick 		register u_long *p; \
200*38414Smckusick 		register caddr_t cp; \
201*38414Smckusick 		register long t1, t2; \
202*38414Smckusick 		caddr_t bpos, dpos, cp2; \
203*38414Smckusick 		u_long xid; \
204*38414Smckusick 		int error = 0; \
205*38414Smckusick 		long offs = 0; \
206*38414Smckusick 		struct mbuf *mreq, *mrep, *md, *mb, *mb2
207*38414Smckusick 
208*38414Smckusick #define nfsm_reqdone	m_freem(mrep); \
209*38414Smckusick 		nfsmout:
210*38414Smckusick 
211*38414Smckusick #define nfsm_rndup(a)	(((a)+3)&(~0x3))
212*38414Smckusick 
213*38414Smckusick #define	nfsm_request(v)	\
214*38414Smckusick 		if (error = nfs_request((v), mreq, xid, \
215*38414Smckusick 		   (v)->v_mount, &mrep, &md, &dpos)) \
216*38414Smckusick 			goto nfsmout
217*38414Smckusick 
218*38414Smckusick #define	nfsm_strtom(a,s,m) \
219*38414Smckusick 		if ((s) > (m)) { \
220*38414Smckusick 			m_freem(mreq); \
221*38414Smckusick 			error = ENAMETOOLONG; \
222*38414Smckusick 			goto nfsmout; \
223*38414Smckusick 		} \
224*38414Smckusick 		t2 = nfsm_rndup(s)+NFSX_UNSIGNED; \
225*38414Smckusick 		if(t2<=(NFSMSIZ(mb)-mb->m_len)){ \
226*38414Smckusick 			nfsm_build(p,u_long *,t2); \
227*38414Smckusick 			*p++ = txdr_unsigned(s); \
228*38414Smckusick 			*(p+((t2>>2)-2)) = 0; \
229*38414Smckusick 			bcopy((caddr_t)(a), (caddr_t)p, (s)); \
230*38414Smckusick 		} else if (error = nfsm_strtmbuf(&mb, &bpos, (a), (s))) { \
231*38414Smckusick 			m_freem(mreq); \
232*38414Smckusick 			goto nfsmout; \
233*38414Smckusick 		}
234*38414Smckusick 
235*38414Smckusick #define	nfsm_srverr \
236*38414Smckusick 		{ \
237*38414Smckusick 			m_freem(mrep); \
238*38414Smckusick 			return(ENOBUFS); \
239*38414Smckusick 		}
240*38414Smckusick 
241*38414Smckusick #define	nfsm_srvars \
242*38414Smckusick 		register caddr_t cp; \
243*38414Smckusick 		register u_long *p; \
244*38414Smckusick 		register long t1, t2; \
245*38414Smckusick 		caddr_t bpos; \
246*38414Smckusick 		long offs = 0; \
247*38414Smckusick 		int error = 0; \
248*38414Smckusick 		char *cp2; \
249*38414Smckusick 		struct mbuf *mb, *mb2, *mreq
250*38414Smckusick 
251*38414Smckusick #define	nfsm_srvdone \
252*38414Smckusick 		nfsmout: \
253*38414Smckusick 		return(error)
254*38414Smckusick 
255*38414Smckusick #define	nfsm_reply(s) \
256*38414Smckusick 		{ \
257*38414Smckusick 		if (error) \
258*38414Smckusick 			nfs_rephead(0, xid, error, mrq, &mb, &bpos); \
259*38414Smckusick 		else \
260*38414Smckusick 			nfs_rephead((s), xid, error, mrq, &mb, &bpos); \
261*38414Smckusick 		m_freem(mrep); \
262*38414Smckusick 		mreq = *mrq; \
263*38414Smckusick 		if (error) \
264*38414Smckusick 			return(0); \
265*38414Smckusick 		}
266*38414Smckusick 
267*38414Smckusick #define	nfsm_adv(s) \
268*38414Smckusick 		t1 = mtod(md, caddr_t)+md->m_len-dpos; \
269*38414Smckusick 		if (t1 >= (s)) { \
270*38414Smckusick 			dpos += (s); \
271*38414Smckusick 		} else if (error = nfs_adv(&md, &dpos, (s), t1)) { \
272*38414Smckusick 			m_freem(mrep); \
273*38414Smckusick 			goto nfsmout; \
274*38414Smckusick 		}
275*38414Smckusick 
276*38414Smckusick #define nfsm_srvmtofh(f) \
277*38414Smckusick 		nfsm_disecton(p, u_long *, NFSX_FH); \
278*38414Smckusick 		bcopy((caddr_t)p, (caddr_t)f, NFSX_FH)
279*38414Smckusick 
280*38414Smckusick #define	nfsm_clget \
281*38414Smckusick 		if (bp >= be) { \
282*38414Smckusick 			MGET(mp, M_WAIT, MT_DATA); \
283*38414Smckusick 			NFSMCLGET(mp, M_WAIT); \
284*38414Smckusick 			mp->m_len = NFSMSIZ(mp); \
285*38414Smckusick 			if (mp3 == NULL) \
286*38414Smckusick 				mp3 = mp2 = mp; \
287*38414Smckusick 			else { \
288*38414Smckusick 				mp2->m_next = mp; \
289*38414Smckusick 				mp2 = mp; \
290*38414Smckusick 			} \
291*38414Smckusick 			bp = mtod(mp, caddr_t); \
292*38414Smckusick 			be = bp+mp->m_len; \
293*38414Smckusick 		} \
294*38414Smckusick 		p = (u_long *)bp
295*38414Smckusick 
296