nfsm_subs.h revision 1.31 1 /* $NetBSD: nfsm_subs.h,v 1.31 2003/08/07 16:33:56 agc 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. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
34 * @(#)nfsm_subs.h 8.2 (Berkeley) 3/30/95
35 */
36
37
38 #ifndef _NFS_NFSM_SUBS_H_
39 #define _NFS_NFSM_SUBS_H_
40
41
42 /*
43 * These macros do strange and peculiar things to mbuf chains for
44 * the assistance of the nfs code. To attempt to use them for any
45 * other purpose will be dangerous. (they make weird assumptions)
46 */
47
48 /*
49 * First define what the actual subs. return
50 */
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)) ? (m)->m_ext.ext_size : \
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 struct mbuf *mb2; \
80 mb2 = m_get(M_WAIT, MT_DATA); \
81 MCLAIM(mb2, &nfs_mowner); \
82 if ((s) > MLEN) \
83 panic("build > MLEN"); \
84 mb->m_next = mb2; \
85 mb = mb2; \
86 mb->m_len = 0; \
87 bpos = mtod(mb, caddr_t); \
88 } \
89 (a) = (c)(bpos); \
90 mb->m_len += (s); \
91 bpos += (s); }
92
93 #define nfsm_aligned(p) ALIGNED_POINTER(p,u_int32_t)
94
95 #define nfsm_dissect(a, c, s) \
96 { t1 = mtod(md, caddr_t)+md->m_len-dpos; \
97 if (t1 >= (s) && nfsm_aligned(dpos)) { \
98 (a) = (c)(dpos); \
99 dpos += (s); \
100 } else if ((t1 = nfsm_disct(&md, &dpos, (s), t1, &cp2)) != 0){ \
101 error = t1; \
102 m_freem(mrep); \
103 goto nfsmout; \
104 } else { \
105 (a) = (c)cp2; \
106 } }
107
108 #define nfsm_fhtom(n, v3) \
109 { if (v3) { \
110 t2 = nfsm_rndup((n)->n_fhsize) + NFSX_UNSIGNED; \
111 if (t2 <= M_TRAILINGSPACE(mb)) { \
112 nfsm_build(tl, u_int32_t *, t2); \
113 *tl++ = txdr_unsigned((n)->n_fhsize); \
114 *(tl + ((t2>>2) - 2)) = 0; \
115 memcpy((caddr_t)tl,(caddr_t)(n)->n_fhp, \
116 (n)->n_fhsize); \
117 } else if ((t2 = nfsm_strtmbuf(&mb, &bpos, \
118 (caddr_t)(n)->n_fhp, \
119 (n)->n_fhsize)) != 0) { \
120 error = t2; \
121 m_freem(mreq); \
122 goto nfsmout; \
123 } \
124 } else { \
125 nfsm_build(cp, caddr_t, NFSX_V2FH); \
126 memcpy(cp, (caddr_t)(n)->n_fhp, NFSX_V2FH); \
127 } }
128
129 #define nfsm_srvfhtom(f, v3) \
130 { if (v3) { \
131 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED + NFSX_V3FH); \
132 *tl++ = txdr_unsigned(NFSX_V3FH); \
133 memcpy((caddr_t)tl, (caddr_t)(f), NFSX_V3FH); \
134 } else { \
135 nfsm_build(cp, caddr_t, NFSX_V2FH); \
136 memcpy(cp, (caddr_t)(f), NFSX_V2FH); \
137 } }
138
139 #define nfsm_srvpostop_fh(f) \
140 { nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED + NFSX_V3FH); \
141 *tl++ = nfs_true; \
142 *tl++ = txdr_unsigned(NFSX_V3FH); \
143 memcpy((caddr_t)tl, (caddr_t)(f), NFSX_V3FH); \
144 }
145
146 #define nfsm_mtofh(d, v, v3, f) \
147 { struct nfsnode *ttnp; nfsfh_t *ttfhp; int ttfhsize; \
148 if (v3) { \
149 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
150 (f) = fxdr_unsigned(int, *tl); \
151 } else \
152 (f) = 1; \
153 if (f) { \
154 nfsm_getfh(ttfhp, ttfhsize, (v3)); \
155 if ((t1 = nfs_nget((d)->v_mount, ttfhp, ttfhsize, \
156 &ttnp)) != 0) { \
157 error = t1; \
158 m_freem(mrep); \
159 goto nfsmout; \
160 } \
161 (v) = NFSTOV(ttnp); \
162 } \
163 if (v3) { \
164 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
165 if (f) \
166 (f) = fxdr_unsigned(int, *tl); \
167 else if (fxdr_unsigned(int, *tl)) \
168 nfsm_adv(NFSX_V3FATTR); \
169 } \
170 if (f) \
171 nfsm_loadattr((v), (struct vattr *)0, 0); \
172 }
173
174 #define nfsm_getfh(f, s, v3) \
175 { if (v3) { \
176 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
177 if (((s) = fxdr_unsigned(int, *tl)) <= 0 || \
178 (s) > NFSX_V3FHMAX) { \
179 m_freem(mrep); \
180 error = EBADRPC; \
181 goto nfsmout; \
182 } \
183 } else \
184 (s) = NFSX_V2FH; \
185 nfsm_dissect((f), nfsfh_t *, nfsm_rndup(s)); }
186
187 #define nfsm_loadattr(v, a, flags) \
188 { struct vnode *ttvp = (v); \
189 if ((t1 = nfsm_loadattrcache(&ttvp, &md, &dpos, (a), (flags))) \
190 != 0) { \
191 error = t1; \
192 m_freem(mrep); \
193 goto nfsmout; \
194 } \
195 (v) = ttvp; }
196
197 #define nfsm_postop_attr(v, f, flags) \
198 { struct vnode *ttvp = (v); \
199 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
200 if (((f) = fxdr_unsigned(int, *tl)) != 0) { \
201 if ((t1 = nfsm_loadattrcache(&ttvp, &md, &dpos, \
202 (struct vattr *)0, (flags))) != 0) { \
203 error = t1; \
204 (f) = 0; \
205 m_freem(mrep); \
206 goto nfsmout; \
207 } \
208 (v) = ttvp; \
209 } }
210
211 /* Used as (f) for nfsm_wcc_data() */
212 #define NFSV3_WCCRATTR 0
213 #define NFSV3_WCCCHK 1
214
215 #define nfsm_wcc_data(v, f, flags) \
216 { int ttattrf, ttretf = 0; \
217 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
218 if (*tl == nfs_true) { \
219 nfsm_dissect(tl, u_int32_t *, 6 * NFSX_UNSIGNED); \
220 if (f) \
221 ttretf = (VTONFS(v)->n_mtime == \
222 fxdr_unsigned(u_int32_t, *(tl + 2))); \
223 } \
224 nfsm_postop_attr((v), ttattrf, (flags)); \
225 if (f) { \
226 (f) = ttretf; \
227 } else { \
228 (f) = ttattrf; \
229 } }
230
231 /* If full is true, set all fields, otherwise just set mode and time fields */
232 #define nfsm_v3attrbuild(a, full) \
233 { if ((a)->va_mode != (mode_t)VNOVAL) { \
234 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED); \
235 *tl++ = nfs_true; \
236 *tl = txdr_unsigned((a)->va_mode); \
237 } else { \
238 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED); \
239 *tl = nfs_false; \
240 } \
241 if ((full) && (a)->va_uid != (uid_t)VNOVAL) { \
242 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED); \
243 *tl++ = nfs_true; \
244 *tl = txdr_unsigned((a)->va_uid); \
245 } else { \
246 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED); \
247 *tl = nfs_false; \
248 } \
249 if ((full) && (a)->va_gid != (gid_t)VNOVAL) { \
250 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED); \
251 *tl++ = nfs_true; \
252 *tl = txdr_unsigned((a)->va_gid); \
253 } else { \
254 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED); \
255 *tl = nfs_false; \
256 } \
257 if ((full) && (a)->va_size != VNOVAL) { \
258 nfsm_build(tl, u_int32_t *, 3 * NFSX_UNSIGNED); \
259 *tl++ = nfs_true; \
260 txdr_hyper((a)->va_size, tl); \
261 } else { \
262 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED); \
263 *tl = nfs_false; \
264 } \
265 if ((a)->va_atime.tv_sec != VNOVAL) { \
266 if ((a)->va_atime.tv_sec != time.tv_sec) { \
267 nfsm_build(tl, u_int32_t *, 3 * NFSX_UNSIGNED); \
268 *tl++ = txdr_unsigned(NFSV3SATTRTIME_TOCLIENT); \
269 txdr_nfsv3time(&(a)->va_atime, tl); \
270 } else { \
271 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED); \
272 *tl = txdr_unsigned(NFSV3SATTRTIME_TOSERVER); \
273 } \
274 } else { \
275 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED); \
276 *tl = txdr_unsigned(NFSV3SATTRTIME_DONTCHANGE); \
277 } \
278 if ((a)->va_mtime.tv_sec != VNOVAL) { \
279 if ((a)->va_mtime.tv_sec != time.tv_sec) { \
280 nfsm_build(tl, u_int32_t *, 3 * NFSX_UNSIGNED); \
281 *tl++ = txdr_unsigned(NFSV3SATTRTIME_TOCLIENT); \
282 txdr_nfsv3time(&(a)->va_mtime, tl); \
283 } else { \
284 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED); \
285 *tl = txdr_unsigned(NFSV3SATTRTIME_TOSERVER); \
286 } \
287 } else { \
288 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED); \
289 *tl = txdr_unsigned(NFSV3SATTRTIME_DONTCHANGE); \
290 } \
291 }
292
293
294 #define nfsm_strsiz(s,m) \
295 { nfsm_dissect(tl,uint32_t *,NFSX_UNSIGNED); \
296 if (((s) = fxdr_unsigned(uint32_t,*tl)) > (m)) { \
297 m_freem(mrep); \
298 error = EBADRPC; \
299 goto nfsmout; \
300 } }
301
302 #define nfsm_srvnamesiz(s) \
303 { nfsm_dissect(tl,uint32_t *,NFSX_UNSIGNED); \
304 if (((s) = fxdr_unsigned(uint32_t,*tl)) > NFS_MAXNAMLEN) \
305 error = NFSERR_NAMETOL; \
306 if (error) \
307 nfsm_reply(0); \
308 }
309
310 #define nfsm_mtouio(p,s) \
311 if ((s) > 0 && \
312 (t1 = nfsm_mbuftouio(&md,(p),(s),&dpos)) != 0) { \
313 error = t1; \
314 m_freem(mrep); \
315 goto nfsmout; \
316 }
317
318 #define nfsm_uiotom(p,s) \
319 if ((t1 = nfsm_uiotombuf((p),&mb,(s),&bpos)) != 0) { \
320 error = t1; \
321 m_freem(mreq); \
322 goto nfsmout; \
323 }
324
325 #define nfsm_reqhead(n,a,s) \
326 mb = mreq = nfsm_reqh((n),(a),(s),&bpos)
327
328 #define nfsm_reqdone m_freem(mrep); \
329 nfsmout:
330
331 #define nfsm_rndup(a) (((a)+3)&(~0x3))
332 #define nfsm_padlen(a) (nfsm_rndup(a) - (a))
333
334 #define nfsm_request(v, t, p, c) \
335 if ((error = nfs_request((v), mreq, (t), (p), \
336 (c), &mrep, &md, &dpos)) != 0) { \
337 if (error & NFSERR_RETERR) \
338 error &= ~NFSERR_RETERR; \
339 else \
340 goto nfsmout; \
341 }
342
343 #define nfsm_strtom(a,s,m) \
344 if ((s) > (m)) { \
345 m_freem(mreq); \
346 error = ENAMETOOLONG; \
347 goto nfsmout; \
348 } \
349 t2 = nfsm_rndup(s)+NFSX_UNSIGNED; \
350 if (t2 <= M_TRAILINGSPACE(mb)) { \
351 nfsm_build(tl,u_int32_t *,t2); \
352 *tl++ = txdr_unsigned(s); \
353 *(tl+((t2>>2)-2)) = 0; \
354 memcpy((caddr_t)tl, (const char *)(a), (s)); \
355 } else if ((t2 = nfsm_strtmbuf(&mb, &bpos, (a), (s))) != 0) { \
356 error = t2; \
357 m_freem(mreq); \
358 goto nfsmout; \
359 }
360
361 #define nfsm_srvdone \
362 nfsmout: \
363 return(error)
364
365 #define nfsm_reply(s) \
366 { \
367 nfsd->nd_repstat = error; \
368 if (error && !(nfsd->nd_flag & ND_NFSV3)) \
369 (void) nfs_rephead(0, nfsd, slp, error, cache, &frev, \
370 mrq, &mb, &bpos); \
371 else \
372 (void) nfs_rephead((s), nfsd, slp, error, cache, &frev, \
373 mrq, &mb, &bpos); \
374 if (mrep != NULL) { \
375 m_freem(mrep); \
376 mrep = NULL; \
377 } \
378 mreq = *mrq; \
379 if (error && (!(nfsd->nd_flag & ND_NFSV3) || \
380 error == EBADRPC)) \
381 return(0); \
382 }
383
384 #define nfsm_writereply(s, v3) \
385 { \
386 nfsd->nd_repstat = error; \
387 if (error && !(v3)) \
388 (void) nfs_rephead(0, nfsd, slp, error, cache, &frev, \
389 &mreq, &mb, &bpos); \
390 else \
391 (void) nfs_rephead((s), nfsd, slp, error, cache, &frev, \
392 &mreq, &mb, &bpos); \
393 }
394
395 #define nfsm_adv(s) \
396 { t1 = mtod(md, caddr_t)+md->m_len-dpos; \
397 if (t1 >= (s)) { \
398 dpos += (s); \
399 } else if ((t1 = nfs_adv(&md, &dpos, (s), t1)) != 0) { \
400 error = t1; \
401 m_freem(mrep); \
402 goto nfsmout; \
403 } }
404
405 #define nfsm_srvmtofh(f) \
406 { int fhlen = NFSX_V3FH; \
407 if (nfsd->nd_flag & ND_NFSV3) { \
408 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
409 fhlen = fxdr_unsigned(int, *tl); \
410 if (fhlen == 0) { \
411 memset((caddr_t)(f), 0, NFSX_V3FH); \
412 } else if (fhlen != NFSX_V3FH) { \
413 error = EBADRPC; \
414 nfsm_reply(0); \
415 } \
416 } \
417 if (fhlen != 0) { \
418 nfsm_dissect(tl, u_int32_t *, NFSX_V3FH); \
419 memcpy( (caddr_t)(f), (caddr_t)tl, NFSX_V3FH); \
420 if ((nfsd->nd_flag & ND_NFSV3) == 0) \
421 nfsm_adv(NFSX_V2FH - NFSX_V3FH); \
422 } \
423 }
424
425 #define nfsm_clget \
426 if (bp >= be) { \
427 if (mp == mb) \
428 mp->m_len += bp-bpos; \
429 mp = m_get(M_WAIT, MT_DATA); \
430 MCLAIM(mp, &nfs_mowner); \
431 m_clget(mp, M_WAIT); \
432 mp->m_len = NFSMSIZ(mp); \
433 mp2->m_next = mp; \
434 mp2 = mp; \
435 bp = mtod(mp, caddr_t); \
436 be = bp+mp->m_len; \
437 } \
438 tl = (u_int32_t *)bp
439
440 #define nfsm_srvfillattr(a, f) \
441 nfsm_srvfattr(nfsd, (a), (f))
442
443 #define nfsm_srvwcc_data(br, b, ar, a) \
444 nfsm_srvwcc(nfsd, (br), (b), (ar), (a), &mb, &bpos)
445
446 #define nfsm_srvpostop_attr(r, a) \
447 nfsm_srvpostopattr(nfsd, (r), (a), &mb, &bpos)
448
449 #define nfsm_srvsattr(a) \
450 { nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
451 if (*tl == nfs_true) { \
452 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
453 (a)->va_mode = nfstov_mode(*tl); \
454 } \
455 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
456 if (*tl == nfs_true) { \
457 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
458 (a)->va_uid = fxdr_unsigned(uid_t, *tl); \
459 } \
460 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
461 if (*tl == nfs_true) { \
462 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
463 (a)->va_gid = fxdr_unsigned(gid_t, *tl); \
464 } \
465 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
466 if (*tl == nfs_true) { \
467 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED); \
468 (a)->va_size = fxdr_hyper(tl); \
469 } \
470 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
471 switch (fxdr_unsigned(int, *tl)) { \
472 case NFSV3SATTRTIME_TOCLIENT: \
473 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED); \
474 fxdr_nfsv3time(tl, &(a)->va_atime); \
475 break; \
476 case NFSV3SATTRTIME_TOSERVER: \
477 (a)->va_atime.tv_sec = time.tv_sec; \
478 (a)->va_atime.tv_nsec = time.tv_usec * 1000; \
479 (a)->va_vaflags |= VA_UTIMES_NULL; \
480 break; \
481 }; \
482 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
483 switch (fxdr_unsigned(int, *tl)) { \
484 case NFSV3SATTRTIME_TOCLIENT: \
485 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED); \
486 fxdr_nfsv3time(tl, &(a)->va_mtime); \
487 (a)->va_vaflags &= ~VA_UTIMES_NULL; \
488 break; \
489 case NFSV3SATTRTIME_TOSERVER: \
490 (a)->va_mtime.tv_sec = time.tv_sec; \
491 (a)->va_mtime.tv_nsec = time.tv_usec * 1000; \
492 (a)->va_vaflags |= VA_UTIMES_NULL; \
493 break; \
494 }; }
495
496 #endif
497