nfsm_subs.h revision 1.7 1 /* $NetBSD: nfsm_subs.h,v 1.7 1995/12/19 23:08:00 cgd 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.1 (Berkeley) 6/16/93
39 */
40
41 /*
42 * These macros do strange and peculiar things to mbuf chains for
43 * the assistance of the nfs code. To attempt to use them for any
44 * other purpose will be dangerous. (they make weird assumptions)
45 */
46
47 /*
48 * First define what the actual subs. return
49 */
50 extern struct mbuf *nfsm_reqh();
51
52 #define M_HASCL(m) ((m)->m_flags & M_EXT)
53 #define NFSMINOFF(m) \
54 if (M_HASCL(m)) \
55 (m)->m_data = (m)->m_ext.ext_buf; \
56 else if ((m)->m_flags & M_PKTHDR) \
57 (m)->m_data = (m)->m_pktdat; \
58 else \
59 (m)->m_data = (m)->m_dat
60 #define NFSMADV(m, s) (m)->m_data += (s)
61 #define NFSMSIZ(m) ((M_HASCL(m))?MCLBYTES: \
62 (((m)->m_flags & M_PKTHDR)?MHLEN:MLEN))
63
64 /*
65 * Now for the macros that do the simple stuff and call the functions
66 * for the hard stuff.
67 * These macros use several vars. declared in nfsm_reqhead and these
68 * vars. must not be used elsewhere unless you are careful not to corrupt
69 * them. The vars. starting with pN and tN (N=1,2,3,..) are temporaries
70 * that may be used so long as the value is not expected to retained
71 * after a macro.
72 * I know, this is kind of dorkey, but it makes the actual op functions
73 * fairly clean and deals with the mess caused by the xdr discriminating
74 * unions.
75 */
76
77 #define nfsm_build(a,c,s) \
78 { if ((s) > M_TRAILINGSPACE(mb)) { \
79 MGET(mb2, M_WAIT, MT_DATA); \
80 if ((s) > MLEN) \
81 panic("build > MLEN"); \
82 mb->m_next = mb2; \
83 mb = mb2; \
84 mb->m_len = 0; \
85 bpos = mtod(mb, caddr_t); \
86 } \
87 (a) = (c)(bpos); \
88 mb->m_len += (s); \
89 bpos += (s); }
90
91 #define nfsm_dissect(a,c,s) \
92 { t1 = mtod(md, caddr_t)+md->m_len-dpos; \
93 if (t1 >= (s)) { \
94 (a) = (c)(dpos); \
95 dpos += (s); \
96 } else if (error = nfsm_disct(&md, &dpos, (s), t1, &cp2)) { \
97 m_freem(mrep); \
98 goto nfsmout; \
99 } else { \
100 (a) = (c)cp2; \
101 } }
102
103 #define nfsm_fhtom(v) \
104 nfsm_build(cp,caddr_t,NFSX_FH); \
105 bcopy((caddr_t)&(VTONFS(v)->n_fh), cp, NFSX_FH)
106
107 #define nfsm_srvfhtom(f) \
108 nfsm_build(cp,caddr_t,NFSX_FH); \
109 bcopy((caddr_t)(f), cp, NFSX_FH)
110
111 #define nfsm_mtofh(d,v) \
112 { struct nfsnode *np; nfsv2fh_t *fhp; \
113 nfsm_dissect(fhp,nfsv2fh_t *,NFSX_FH); \
114 if (error = nfs_nget((d)->v_mount, fhp, &np)) { \
115 m_freem(mrep); \
116 goto nfsmout; \
117 } \
118 (v) = NFSTOV(np); \
119 nfsm_loadattr(v, (struct vattr *)0); \
120 }
121
122 #define nfsm_loadattr(v,a) \
123 { struct vnode *tvp = (v); \
124 if (error = nfs_loadattrcache(&tvp, &md, &dpos, (a))) { \
125 m_freem(mrep); \
126 goto nfsmout; \
127 } \
128 (v) = tvp; }
129
130 #define nfsm_strsiz(s,m) \
131 { nfsm_dissect(tl,u_int32_t *,NFSX_UNSIGNED); \
132 if (((s) = fxdr_unsigned(int32_t,*tl)) > (m)) { \
133 m_freem(mrep); \
134 error = EBADRPC; \
135 goto nfsmout; \
136 } }
137
138 #define nfsm_srvstrsiz(s,m) \
139 { nfsm_dissect(tl,u_int32_t *,NFSX_UNSIGNED); \
140 if (((s) = fxdr_unsigned(int32_t,*tl)) > (m) || (s) <= 0) { \
141 error = EBADRPC; \
142 nfsm_reply(0); \
143 } }
144
145 #define nfsm_mtouio(p,s) \
146 if ((s) > 0 && \
147 (error = nfsm_mbuftouio(&md,(p),(s),&dpos))) { \
148 m_freem(mrep); \
149 goto nfsmout; \
150 }
151
152 #define nfsm_uiotom(p,s) \
153 if (error = nfsm_uiotombuf((p),&mb,(s),&bpos)) { \
154 m_freem(mreq); \
155 goto nfsmout; \
156 }
157
158 #define nfsm_reqhead(v,a,s) \
159 mb = mreq = nfsm_reqh((v),(a),(s),&bpos)
160
161 #define nfsm_reqdone m_freem(mrep); \
162 nfsmout:
163
164 #define nfsm_rndup(a) (((a)+3)&(~0x3))
165
166 #define nfsm_request(v, t, p, c) \
167 if (error = nfs_request((v), mreq, (t), (p), \
168 (c), &mrep, &md, &dpos)) \
169 goto nfsmout
170
171 #define nfsm_strtom(a,s,m) \
172 if ((s) > (m)) { \
173 m_freem(mreq); \
174 error = ENAMETOOLONG; \
175 goto nfsmout; \
176 } \
177 t2 = nfsm_rndup(s)+NFSX_UNSIGNED; \
178 if (t2 <= M_TRAILINGSPACE(mb)) { \
179 nfsm_build(tl,u_int32_t *,t2); \
180 *tl++ = txdr_unsigned(s); \
181 *(tl+((t2>>2)-2)) = 0; \
182 bcopy((caddr_t)(a), (caddr_t)tl, (s)); \
183 } else if (error = nfsm_strtmbuf(&mb, &bpos, (a), (s))) { \
184 m_freem(mreq); \
185 goto nfsmout; \
186 }
187
188 #define nfsm_srvdone \
189 nfsmout: \
190 return(error)
191
192 #define nfsm_reply(s) \
193 { \
194 nfsd->nd_repstat = error; \
195 if (error) \
196 (void) nfs_rephead(0, nfsd, error, cache, &frev, \
197 mrq, &mb, &bpos); \
198 else \
199 (void) nfs_rephead((s), nfsd, error, cache, &frev, \
200 mrq, &mb, &bpos); \
201 m_freem(mrep); \
202 mreq = *mrq; \
203 if (error) \
204 return(0); \
205 }
206
207 #define nfsm_adv(s) \
208 t1 = mtod(md, caddr_t)+md->m_len-dpos; \
209 if (t1 >= (s)) { \
210 dpos += (s); \
211 } else if (error = nfs_adv(&md, &dpos, (s), t1)) { \
212 m_freem(mrep); \
213 goto nfsmout; \
214 }
215
216 #define nfsm_srvmtofh(f) \
217 nfsm_dissect(tl, u_int32_t *, NFSX_FH); \
218 bcopy((caddr_t)tl, (caddr_t)f, NFSX_FH)
219
220 #define nfsm_clget \
221 if (bp >= be) { \
222 if (mp == mb) \
223 mp->m_len += bp-bpos; \
224 MGET(mp, M_WAIT, MT_DATA); \
225 MCLGET(mp, M_WAIT); \
226 mp->m_len = NFSMSIZ(mp); \
227 mp2->m_next = mp; \
228 mp2 = mp; \
229 bp = mtod(mp, caddr_t); \
230 be = bp+mp->m_len; \
231 } \
232 tl = (u_int32_t *)bp
233
234 #define nfsm_srvfillattr \
235 fp->fa_type = vtonfs_type(va.va_type); \
236 fp->fa_mode = vtonfs_mode(va.va_type, va.va_mode); \
237 fp->fa_nlink = txdr_unsigned(va.va_nlink); \
238 fp->fa_uid = txdr_unsigned(va.va_uid); \
239 fp->fa_gid = txdr_unsigned(va.va_gid); \
240 if (nfsd->nd_nqlflag == NQL_NOVAL) { \
241 fp->fa_nfsblocksize = txdr_unsigned(va.va_blocksize); \
242 if (va.va_type == VFIFO) \
243 fp->fa_nfsrdev = 0xffffffff; \
244 else \
245 fp->fa_nfsrdev = txdr_unsigned(va.va_rdev); \
246 fp->fa_nfsfsid = txdr_unsigned(va.va_fsid); \
247 fp->fa_nfsfileid = txdr_unsigned(va.va_fileid); \
248 fp->fa_nfssize = txdr_unsigned(va.va_size); \
249 fp->fa_nfsblocks = txdr_unsigned(va.va_bytes / NFS_FABLKSIZE); \
250 txdr_nfstime(&va.va_atime, &fp->fa_nfsatime); \
251 txdr_nfstime(&va.va_mtime, &fp->fa_nfsmtime); \
252 txdr_nfstime(&va.va_ctime, &fp->fa_nfsctime); \
253 } else { \
254 fp->fa_nqblocksize = txdr_unsigned(va.va_blocksize); \
255 if (va.va_type == VFIFO) \
256 fp->fa_nqrdev = 0xffffffff; \
257 else \
258 fp->fa_nqrdev = txdr_unsigned(va.va_rdev); \
259 fp->fa_nqfsid = txdr_unsigned(va.va_fsid); \
260 fp->fa_nqfileid = txdr_unsigned(va.va_fileid); \
261 txdr_hyper(&va.va_size, &fp->fa_nqsize); \
262 txdr_hyper(&va.va_bytes, &fp->fa_nqbytes); \
263 txdr_nqtime(&va.va_atime, &fp->fa_nqatime); \
264 txdr_nqtime(&va.va_mtime, &fp->fa_nqmtime); \
265 txdr_nqtime(&va.va_ctime, &fp->fa_nqctime); \
266 fp->fa_nqflags = txdr_unsigned(va.va_flags); \
267 fp->fa_nqgen = txdr_unsigned(va.va_gen); \
268 txdr_hyper(&va.va_filerev, &fp->fa_nqfilerev); \
269 }
270
271