nfsm_subs.h revision 1.1 1 1.1 mycroft /*
2 1.1 mycroft * Copyright (c) 1989 The Regents of the University of California.
3 1.1 mycroft * All rights reserved.
4 1.1 mycroft *
5 1.1 mycroft * This code is derived from software contributed to Berkeley by
6 1.1 mycroft * Rick Macklem at The University of Guelph.
7 1.1 mycroft *
8 1.1 mycroft * Redistribution and use in source and binary forms, with or without
9 1.1 mycroft * modification, are permitted provided that the following conditions
10 1.1 mycroft * are met:
11 1.1 mycroft * 1. Redistributions of source code must retain the above copyright
12 1.1 mycroft * notice, this list of conditions and the following disclaimer.
13 1.1 mycroft * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 mycroft * notice, this list of conditions and the following disclaimer in the
15 1.1 mycroft * documentation and/or other materials provided with the distribution.
16 1.1 mycroft * 3. All advertising materials mentioning features or use of this software
17 1.1 mycroft * must display the following acknowledgement:
18 1.1 mycroft * This product includes software developed by the University of
19 1.1 mycroft * California, Berkeley and its contributors.
20 1.1 mycroft * 4. Neither the name of the University nor the names of its contributors
21 1.1 mycroft * may be used to endorse or promote products derived from this software
22 1.1 mycroft * without specific prior written permission.
23 1.1 mycroft *
24 1.1 mycroft * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 1.1 mycroft * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 1.1 mycroft * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 1.1 mycroft * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 1.1 mycroft * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 1.1 mycroft * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 1.1 mycroft * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 1.1 mycroft * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 1.1 mycroft * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 1.1 mycroft * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 1.1 mycroft * SUCH DAMAGE.
35 1.1 mycroft *
36 1.1 mycroft * @(#)nfsm_subs.h 7.11 (Berkeley) 4/16/91
37 1.1 mycroft */
38 1.1 mycroft
39 1.1 mycroft #ifndef _NFS_NFSM_SUBS_H_
40 1.1 mycroft #define _NFS_NFSM_SUBS_H_
41 1.1 mycroft
42 1.1 mycroft /*
43 1.1 mycroft * These macros do strange and peculiar things to mbuf chains for
44 1.1 mycroft * the assistance of the nfs code. To attempt to use them for any
45 1.1 mycroft * other purpose will be dangerous. (they make weird assumptions)
46 1.1 mycroft */
47 1.1 mycroft
48 1.1 mycroft /*
49 1.1 mycroft * First define what the actual subs. return
50 1.1 mycroft */
51 1.1 mycroft extern struct mbuf *nfsm_reqh();
52 1.1 mycroft
53 1.1 mycroft #define M_HASCL(m) ((m)->m_flags & M_EXT)
54 1.1 mycroft #define NFSMGETHDR(m) \
55 1.1 mycroft MGETHDR(m, M_WAIT, MT_DATA); \
56 1.1 mycroft (m)->m_pkthdr.len = 0; \
57 1.1 mycroft (m)->m_pkthdr.rcvif = (struct ifnet *)0
58 1.1 mycroft #define NFSMINOFF(m) \
59 1.1 mycroft if (M_HASCL(m)) \
60 1.1 mycroft (m)->m_data = (m)->m_ext.ext_buf; \
61 1.1 mycroft else \
62 1.1 mycroft (m)->m_data = (m)->m_dat
63 1.1 mycroft #define NFSMADV(m, s) (m)->m_data += (s)
64 1.1 mycroft #define NFSMSIZ(m) ((M_HASCL(m))?MCLBYTES: \
65 1.1 mycroft (((m)->m_flags & M_PKTHDR)?MHLEN:MLEN))
66 1.1 mycroft
67 1.1 mycroft /*
68 1.1 mycroft * Now for the macros that do the simple stuff and call the functions
69 1.1 mycroft * for the hard stuff.
70 1.1 mycroft * These macros use several vars. declared in nfsm_reqhead and these
71 1.1 mycroft * vars. must not be used elsewhere unless you are careful not to corrupt
72 1.1 mycroft * them. The vars. starting with pN and tN (N=1,2,3,..) are temporaries
73 1.1 mycroft * that may be used so long as the value is not expected to retained
74 1.1 mycroft * after a macro.
75 1.1 mycroft * I know, this is kind of dorkey, but it makes the actual op functions
76 1.1 mycroft * fairly clean and deals with the mess caused by the xdr discriminating
77 1.1 mycroft * unions.
78 1.1 mycroft */
79 1.1 mycroft
80 1.1 mycroft #ifndef lint
81 1.1 mycroft #define nfsm_build(a,c,s) \
82 1.1 mycroft t1 = NFSMSIZ(mb); \
83 1.1 mycroft if ((s) > (t1-mb->m_len)) { \
84 1.1 mycroft MGET(mb2, M_WAIT, MT_DATA); \
85 1.1 mycroft if ((s) > MLEN) \
86 1.1 mycroft panic("build > MLEN"); \
87 1.1 mycroft mb->m_next = mb2; \
88 1.1 mycroft mb = mb2; \
89 1.1 mycroft mb->m_len = 0; \
90 1.1 mycroft bpos = mtod(mb, caddr_t); \
91 1.1 mycroft } \
92 1.1 mycroft (a) = (c)(bpos); \
93 1.1 mycroft mb->m_len += (s); \
94 1.1 mycroft bpos += (s)
95 1.1 mycroft #else /* lint */
96 1.1 mycroft #define nfsm_build(a,c,s) \
97 1.1 mycroft t1 = NFSMSIZ(mb); \
98 1.1 mycroft if ((s) > (t1-mb->m_len)) { \
99 1.1 mycroft MGET(mb2, M_WAIT, MT_DATA); \
100 1.1 mycroft mb->m_next = mb2; \
101 1.1 mycroft mb = mb2; \
102 1.1 mycroft mb->m_len = 0; \
103 1.1 mycroft bpos = mtod(mb, caddr_t); \
104 1.1 mycroft } \
105 1.1 mycroft (a) = (c)(bpos); \
106 1.1 mycroft mb->m_len += (s); \
107 1.1 mycroft bpos += (s)
108 1.1 mycroft #endif /* lint */
109 1.1 mycroft
110 1.1 mycroft #define nfsm_disect(a,c,s) \
111 1.1 mycroft t1 = mtod(md, caddr_t)+md->m_len-dpos; \
112 1.1 mycroft if (t1 >= (s)) { \
113 1.1 mycroft (a) = (c)(dpos); \
114 1.1 mycroft dpos += (s); \
115 1.1 mycroft } else if (error = nfsm_disct(&md, &dpos, (s), t1, TRUE, &cp2)) { \
116 1.1 mycroft m_freem(mrep); \
117 1.1 mycroft goto nfsmout; \
118 1.1 mycroft } else { \
119 1.1 mycroft (a) = (c)cp2; \
120 1.1 mycroft }
121 1.1 mycroft
122 1.1 mycroft #define nfsm_disecton(a,c,s) \
123 1.1 mycroft t1 = mtod(md, caddr_t)+md->m_len-dpos; \
124 1.1 mycroft if (t1 >= (s)) { \
125 1.1 mycroft (a) = (c)(dpos); \
126 1.1 mycroft dpos += (s); \
127 1.1 mycroft } else if (error = nfsm_disct(&md, &dpos, (s), t1, FALSE, &cp2)) { \
128 1.1 mycroft m_freem(mrep); \
129 1.1 mycroft goto nfsmout; \
130 1.1 mycroft } else { \
131 1.1 mycroft (a) = (c)cp2; \
132 1.1 mycroft }
133 1.1 mycroft
134 1.1 mycroft #define nfsm_fhtom(v) \
135 1.1 mycroft nfsm_build(cp,caddr_t,NFSX_FH); \
136 1.1 mycroft bcopy((caddr_t)&(VTONFS(v)->n_fh), cp, NFSX_FH)
137 1.1 mycroft
138 1.1 mycroft #define nfsm_srvfhtom(f) \
139 1.1 mycroft nfsm_build(cp,caddr_t,NFSX_FH); \
140 1.1 mycroft bcopy((caddr_t)(f), cp, NFSX_FH)
141 1.1 mycroft
142 1.1 mycroft #define nfsm_mtofh(d,v) \
143 1.1 mycroft { struct nfsnode *np; nfsv2fh_t *fhp; \
144 1.1 mycroft nfsm_disect(fhp,nfsv2fh_t *,NFSX_FH); \
145 1.1 mycroft if (error = nfs_nget((d)->v_mount, fhp, &np)) { \
146 1.1 mycroft m_freem(mrep); \
147 1.1 mycroft goto nfsmout; \
148 1.1 mycroft } \
149 1.1 mycroft (v) = NFSTOV(np); \
150 1.1 mycroft nfsm_loadattr(v, (struct vattr *)0); \
151 1.1 mycroft }
152 1.1 mycroft
153 1.1 mycroft #define nfsm_loadattr(v,a) \
154 1.1 mycroft { struct vnode *tvp = (v); \
155 1.1 mycroft if (error = nfs_loadattrcache(&tvp, &md, &dpos, (a))) { \
156 1.1 mycroft m_freem(mrep); \
157 1.1 mycroft goto nfsmout; \
158 1.1 mycroft } \
159 1.1 mycroft (v) = tvp; }
160 1.1 mycroft
161 1.1 mycroft #define nfsm_strsiz(s,m) \
162 1.1 mycroft nfsm_disect(tl,u_long *,NFSX_UNSIGNED); \
163 1.1 mycroft if (((s) = fxdr_unsigned(long,*tl)) > (m)) { \
164 1.1 mycroft m_freem(mrep); \
165 1.1 mycroft error = EBADRPC; \
166 1.1 mycroft goto nfsmout; \
167 1.1 mycroft }
168 1.1 mycroft
169 1.1 mycroft #define nfsm_srvstrsiz(s,m) \
170 1.1 mycroft nfsm_disect(tl,u_long *,NFSX_UNSIGNED); \
171 1.1 mycroft if (((s) = fxdr_unsigned(long,*tl)) > (m) || (s) <= 0) { \
172 1.1 mycroft error = EBADRPC; \
173 1.1 mycroft nfsm_reply(0); \
174 1.1 mycroft }
175 1.1 mycroft
176 1.1 mycroft #define nfsm_mtouio(p,s) \
177 1.1 mycroft if ((s) > 0 && \
178 1.1 mycroft (error = nfsm_mbuftouio(&md,(p),(s),&dpos))) { \
179 1.1 mycroft m_freem(mrep); \
180 1.1 mycroft goto nfsmout; \
181 1.1 mycroft }
182 1.1 mycroft
183 1.1 mycroft #define nfsm_uiotom(p,s) \
184 1.1 mycroft if (error = nfsm_uiotombuf((p),&mb,(s),&bpos)) { \
185 1.1 mycroft m_freem(mreq); \
186 1.1 mycroft goto nfsmout; \
187 1.1 mycroft }
188 1.1 mycroft
189 1.1 mycroft #define nfsm_reqhead(a,c,s) \
190 1.1 mycroft if ((mreq = nfsm_reqh(nfs_prog,nfs_vers,(a),(c),(s),&bpos,&mb,&xid)) == NULL) { \
191 1.1 mycroft error = ENOBUFS; \
192 1.1 mycroft goto nfsmout; \
193 1.1 mycroft }
194 1.1 mycroft
195 1.1 mycroft #define nfsm_reqdone m_freem(mrep); \
196 1.1 mycroft nfsmout:
197 1.1 mycroft
198 1.1 mycroft #define nfsm_rndup(a) (((a)+3)&(~0x3))
199 1.1 mycroft
200 1.1 mycroft #define nfsm_request(v, t, p, h) \
201 1.1 mycroft if (error = nfs_request((v), mreq, xid, (t), (p), (h), \
202 1.1 mycroft (v)->v_mount, &mrep, &md, &dpos)) \
203 1.1 mycroft goto nfsmout
204 1.1 mycroft
205 1.1 mycroft #define nfsm_strtom(a,s,m) \
206 1.1 mycroft if ((s) > (m)) { \
207 1.1 mycroft m_freem(mreq); \
208 1.1 mycroft error = ENAMETOOLONG; \
209 1.1 mycroft goto nfsmout; \
210 1.1 mycroft } \
211 1.1 mycroft t2 = nfsm_rndup(s)+NFSX_UNSIGNED; \
212 1.1 mycroft if(t2<=(NFSMSIZ(mb)-mb->m_len)){ \
213 1.1 mycroft nfsm_build(tl,u_long *,t2); \
214 1.1 mycroft *tl++ = txdr_unsigned(s); \
215 1.1 mycroft *(tl+((t2>>2)-2)) = 0; \
216 1.1 mycroft bcopy((caddr_t)(a), (caddr_t)tl, (s)); \
217 1.1 mycroft } else if (error = nfsm_strtmbuf(&mb, &bpos, (a), (s))) { \
218 1.1 mycroft m_freem(mreq); \
219 1.1 mycroft goto nfsmout; \
220 1.1 mycroft }
221 1.1 mycroft
222 1.1 mycroft #define nfsm_srvdone \
223 1.1 mycroft nfsmout: \
224 1.1 mycroft return(error)
225 1.1 mycroft
226 1.1 mycroft #ifndef lint
227 1.1 mycroft #define nfsm_reply(s) \
228 1.1 mycroft { \
229 1.1 mycroft *repstat = error; \
230 1.1 mycroft if (error) \
231 1.1 mycroft nfs_rephead(0, xid, error, mrq, &mb, &bpos); \
232 1.1 mycroft else \
233 1.1 mycroft nfs_rephead((s), xid, error, mrq, &mb, &bpos); \
234 1.1 mycroft m_freem(mrep); \
235 1.1 mycroft mreq = *mrq; \
236 1.1 mycroft if (error) \
237 1.1 mycroft return(0); \
238 1.1 mycroft }
239 1.1 mycroft #else /* lint */
240 1.1 mycroft #define nfsm_reply(s) \
241 1.1 mycroft { \
242 1.1 mycroft *repstat = error; \
243 1.1 mycroft if (error) \
244 1.1 mycroft nfs_rephead(0, xid, error, mrq, &mb, &bpos); \
245 1.1 mycroft else \
246 1.1 mycroft nfs_rephead((s), xid, error, mrq, &mb, &bpos); \
247 1.1 mycroft m_freem(mrep); \
248 1.1 mycroft mreq = *mrq; \
249 1.1 mycroft mrep = mreq; \
250 1.1 mycroft if (error) \
251 1.1 mycroft return(0); \
252 1.1 mycroft }
253 1.1 mycroft #endif /* lint */
254 1.1 mycroft
255 1.1 mycroft #define nfsm_adv(s) \
256 1.1 mycroft t1 = mtod(md, caddr_t)+md->m_len-dpos; \
257 1.1 mycroft if (t1 >= (s)) { \
258 1.1 mycroft dpos += (s); \
259 1.1 mycroft } else if (error = nfs_adv(&md, &dpos, (s), t1)) { \
260 1.1 mycroft m_freem(mrep); \
261 1.1 mycroft goto nfsmout; \
262 1.1 mycroft }
263 1.1 mycroft
264 1.1 mycroft #define nfsm_srvmtofh(f) \
265 1.1 mycroft nfsm_disecton(tl, u_long *, NFSX_FH); \
266 1.1 mycroft bcopy((caddr_t)tl, (caddr_t)f, NFSX_FH)
267 1.1 mycroft
268 1.1 mycroft #define nfsm_clget \
269 1.1 mycroft if (bp >= be) { \
270 1.1 mycroft MGET(mp, M_WAIT, MT_DATA); \
271 1.1 mycroft MCLGET(mp, M_WAIT); \
272 1.1 mycroft mp->m_len = NFSMSIZ(mp); \
273 1.1 mycroft if (mp3 == NULL) \
274 1.1 mycroft mp3 = mp2 = mp; \
275 1.1 mycroft else { \
276 1.1 mycroft mp2->m_next = mp; \
277 1.1 mycroft mp2 = mp; \
278 1.1 mycroft } \
279 1.1 mycroft bp = mtod(mp, caddr_t); \
280 1.1 mycroft be = bp+mp->m_len; \
281 1.1 mycroft } \
282 1.1 mycroft tl = (u_long *)bp
283 1.1 mycroft
284 1.1 mycroft #define nfsm_srvfillattr \
285 1.1 mycroft fp->fa_type = vtonfs_type(vap->va_type); \
286 1.1 mycroft fp->fa_mode = vtonfs_mode(vap->va_type, vap->va_mode); \
287 1.1 mycroft fp->fa_nlink = txdr_unsigned(vap->va_nlink); \
288 1.1 mycroft fp->fa_uid = txdr_unsigned(vap->va_uid); \
289 1.1 mycroft fp->fa_gid = txdr_unsigned(vap->va_gid); \
290 1.1 mycroft fp->fa_size = txdr_unsigned(vap->va_size); \
291 1.1 mycroft fp->fa_blocksize = txdr_unsigned(vap->va_blocksize); \
292 1.1 mycroft if (vap->va_type == VFIFO) \
293 1.1 mycroft fp->fa_rdev = 0xffffffff; \
294 1.1 mycroft else \
295 1.1 mycroft fp->fa_rdev = txdr_unsigned(vap->va_rdev); \
296 1.1 mycroft fp->fa_blocks = txdr_unsigned(vap->va_bytes / NFS_FABLKSIZE); \
297 1.1 mycroft fp->fa_fsid = txdr_unsigned(vap->va_fsid); \
298 1.1 mycroft fp->fa_fileid = txdr_unsigned(vap->va_fileid); \
299 1.1 mycroft fp->fa_atime.tv_sec = txdr_unsigned(vap->va_atime.tv_sec); \
300 1.1 mycroft fp->fa_atime.tv_usec = txdr_unsigned(vap->va_flags); \
301 1.1 mycroft txdr_time(&vap->va_mtime, &fp->fa_mtime); \
302 1.1 mycroft fp->fa_ctime.tv_sec = txdr_unsigned(vap->va_ctime.tv_sec); \
303 1.1 mycroft fp->fa_ctime.tv_usec = txdr_unsigned(vap->va_gen)
304 1.1 mycroft
305 1.1 mycroft #endif /* !_NFS_NFSM_SUBS_H_ */
306