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