nfsm_subs.h revision 1.23 1 /* $NetBSD: nfsm_subs.h,v 1.23 2003/02/26 06:31:20 matt 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.2 (Berkeley) 3/30/95
39 */
40
41
42 #ifndef _NFS_NFSM_SUBS_H_
43 #define _NFS_NFSM_SUBS_H_
44
45
46 /*
47 * These macros do strange and peculiar things to mbuf chains for
48 * the assistance of the nfs code. To attempt to use them for any
49 * other purpose will be dangerous. (they make weird assumptions)
50 */
51
52 /*
53 * First define what the actual subs. return
54 */
55
56 #define M_HASCL(m) ((m)->m_flags & M_EXT)
57 #define NFSMINOFF(m) \
58 if (M_HASCL(m)) \
59 (m)->m_data = (m)->m_ext.ext_buf; \
60 else if ((m)->m_flags & M_PKTHDR) \
61 (m)->m_data = (m)->m_pktdat; \
62 else \
63 (m)->m_data = (m)->m_dat
64 #define NFSMADV(m, s) (m)->m_data += (s)
65 #define NFSMSIZ(m) ((M_HASCL(m)) ? (m)->m_ext.ext_size : \
66 (((m)->m_flags & M_PKTHDR) ? MHLEN : MLEN))
67
68 /*
69 * Now for the macros that do the simple stuff and call the functions
70 * for the hard stuff.
71 * These macros use several vars. declared in nfsm_reqhead and these
72 * vars. must not be used elsewhere unless you are careful not to corrupt
73 * them. The vars. starting with pN and tN (N=1,2,3,..) are temporaries
74 * that may be used so long as the value is not expected to retained
75 * after a macro.
76 * I know, this is kind of dorkey, but it makes the actual op functions
77 * fairly clean and deals with the mess caused by the xdr discriminating
78 * unions.
79 */
80
81 #define nfsm_build(a,c,s) \
82 { if ((s) > M_TRAILINGSPACE(mb)) { \
83 struct mbuf *mb2; \
84 mb2 = m_get(M_WAIT, MT_DATA); \
85 MCLAIM(mb2, &nfs_mowner); \
86 if ((s) > MLEN) \
87 panic("build > MLEN"); \
88 mb->m_next = mb2; \
89 mb = mb2; \
90 mb->m_len = 0; \
91 bpos = mtod(mb, caddr_t); \
92 } \
93 (a) = (c)(bpos); \
94 mb->m_len += (s); \
95 bpos += (s); }
96
97 #define nfsm_aligned(p) ALIGNED_POINTER(p,u_int32_t)
98
99 #define nfsm_dissect(a, c, s) \
100 { t1 = mtod(md, caddr_t)+md->m_len-dpos; \
101 if (t1 >= (s) && nfsm_aligned(dpos)) { \
102 (a) = (c)(dpos); \
103 dpos += (s); \
104 } else if ((t1 = nfsm_disct(&md, &dpos, (s), t1, &cp2)) != 0){ \
105 error = t1; \
106 m_freem(mrep); \
107 goto nfsmout; \
108 } else { \
109 (a) = (c)cp2; \
110 } }
111
112 #define nfsm_fhtom(v, v3) \
113 { if (v3) { \
114 t2 = nfsm_rndup(VTONFS(v)->n_fhsize) + NFSX_UNSIGNED; \
115 if (t2 <= M_TRAILINGSPACE(mb)) { \
116 nfsm_build(tl, u_int32_t *, t2); \
117 *tl++ = txdr_unsigned(VTONFS(v)->n_fhsize); \
118 *(tl + ((t2>>2) - 2)) = 0; \
119 memcpy((caddr_t)tl,(caddr_t)VTONFS(v)->n_fhp, \
120 VTONFS(v)->n_fhsize); \
121 } else if ((t2 = nfsm_strtmbuf(&mb, &bpos, \
122 (caddr_t)VTONFS(v)->n_fhp, \
123 VTONFS(v)->n_fhsize)) != 0) { \
124 error = t2; \
125 m_freem(mreq); \
126 goto nfsmout; \
127 } \
128 } else { \
129 nfsm_build(cp, caddr_t, NFSX_V2FH); \
130 memcpy(cp, (caddr_t)VTONFS(v)->n_fhp, NFSX_V2FH); \
131 } }
132
133 #define nfsm_srvfhtom(f, v3) \
134 { if (v3) { \
135 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED + NFSX_V3FH); \
136 *tl++ = txdr_unsigned(NFSX_V3FH); \
137 memcpy((caddr_t)tl, (caddr_t)(f), NFSX_V3FH); \
138 } else { \
139 nfsm_build(cp, caddr_t, NFSX_V2FH); \
140 memcpy(cp, (caddr_t)(f), NFSX_V2FH); \
141 } }
142
143 #define nfsm_srvpostop_fh(f) \
144 { nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED + NFSX_V3FH); \
145 *tl++ = nfs_true; \
146 *tl++ = txdr_unsigned(NFSX_V3FH); \
147 memcpy((caddr_t)tl, (caddr_t)(f), NFSX_V3FH); \
148 }
149
150 #define nfsm_mtofh(d, v, v3, f) \
151 { struct nfsnode *ttnp; nfsfh_t *ttfhp; int ttfhsize; \
152 if (v3) { \
153 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
154 (f) = fxdr_unsigned(int, *tl); \
155 } else \
156 (f) = 1; \
157 if (f) { \
158 nfsm_getfh(ttfhp, ttfhsize, (v3)); \
159 if ((t1 = nfs_nget((d)->v_mount, ttfhp, ttfhsize, \
160 &ttnp)) != 0) { \
161 error = t1; \
162 m_freem(mrep); \
163 goto nfsmout; \
164 } \
165 (v) = NFSTOV(ttnp); \
166 } \
167 if (v3) { \
168 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
169 if (f) \
170 (f) = fxdr_unsigned(int, *tl); \
171 else if (fxdr_unsigned(int, *tl)) \
172 nfsm_adv(NFSX_V3FATTR); \
173 } \
174 if (f) \
175 nfsm_loadattr((v), (struct vattr *)0, 0); \
176 }
177
178 #define nfsm_getfh(f, s, v3) \
179 { if (v3) { \
180 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
181 if (((s) = fxdr_unsigned(int, *tl)) <= 0 || \
182 (s) > NFSX_V3FHMAX) { \
183 m_freem(mrep); \
184 error = EBADRPC; \
185 goto nfsmout; \
186 } \
187 } else \
188 (s) = NFSX_V2FH; \
189 nfsm_dissect((f), nfsfh_t *, nfsm_rndup(s)); }
190
191 #define nfsm_loadattr(v, a, flags) \
192 { struct vnode *ttvp = (v); \
193 if ((t1 = nfsm_loadattrcache(&ttvp, &md, &dpos, (a), (flags))) \
194 != 0) { \
195 error = t1; \
196 m_freem(mrep); \
197 goto nfsmout; \
198 } \
199 (v) = ttvp; }
200
201 #define nfsm_postop_attr(v, f, flags) \
202 { struct vnode *ttvp = (v); \
203 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
204 if (((f) = fxdr_unsigned(int, *tl)) != 0) { \
205 if ((t1 = nfsm_loadattrcache(&ttvp, &md, &dpos, \
206 (struct vattr *)0, (flags))) != 0) { \
207 error = t1; \
208 (f) = 0; \
209 m_freem(mrep); \
210 goto nfsmout; \
211 } \
212 (v) = ttvp; \
213 } }
214
215 /* Used as (f) for nfsm_wcc_data() */
216 #define NFSV3_WCCRATTR 0
217 #define NFSV3_WCCCHK 1
218
219 #define nfsm_wcc_data(v, f, flags) \
220 { int ttattrf, ttretf = 0; \
221 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
222 if (*tl == nfs_true) { \
223 nfsm_dissect(tl, u_int32_t *, 6 * NFSX_UNSIGNED); \
224 if (f) \
225 ttretf = (VTONFS(v)->n_mtime == \
226 fxdr_unsigned(u_int32_t, *(tl + 2))); \
227 } \
228 nfsm_postop_attr((v), ttattrf, (flags)); \
229 if (f) { \
230 (f) = ttretf; \
231 } else { \
232 (f) = ttattrf; \
233 } }
234
235 /* If full is true, set all fields, otherwise just set mode and time fields */
236 #define nfsm_v3attrbuild(a, full) \
237 { if ((a)->va_mode != (mode_t)VNOVAL) { \
238 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED); \
239 *tl++ = nfs_true; \
240 *tl = txdr_unsigned((a)->va_mode); \
241 } else { \
242 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED); \
243 *tl = nfs_false; \
244 } \
245 if ((full) && (a)->va_uid != (uid_t)VNOVAL) { \
246 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED); \
247 *tl++ = nfs_true; \
248 *tl = txdr_unsigned((a)->va_uid); \
249 } else { \
250 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED); \
251 *tl = nfs_false; \
252 } \
253 if ((full) && (a)->va_gid != (gid_t)VNOVAL) { \
254 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED); \
255 *tl++ = nfs_true; \
256 *tl = txdr_unsigned((a)->va_gid); \
257 } else { \
258 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED); \
259 *tl = nfs_false; \
260 } \
261 if ((full) && (a)->va_size != VNOVAL) { \
262 nfsm_build(tl, u_int32_t *, 3 * NFSX_UNSIGNED); \
263 *tl++ = nfs_true; \
264 txdr_hyper((a)->va_size, tl); \
265 } else { \
266 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED); \
267 *tl = nfs_false; \
268 } \
269 if ((a)->va_atime.tv_sec != VNOVAL) { \
270 if ((a)->va_atime.tv_sec != time.tv_sec) { \
271 nfsm_build(tl, u_int32_t *, 3 * NFSX_UNSIGNED); \
272 *tl++ = txdr_unsigned(NFSV3SATTRTIME_TOCLIENT); \
273 txdr_nfsv3time(&(a)->va_atime, tl); \
274 } else { \
275 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED); \
276 *tl = txdr_unsigned(NFSV3SATTRTIME_TOSERVER); \
277 } \
278 } else { \
279 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED); \
280 *tl = txdr_unsigned(NFSV3SATTRTIME_DONTCHANGE); \
281 } \
282 if ((a)->va_mtime.tv_sec != VNOVAL) { \
283 if ((a)->va_mtime.tv_sec != time.tv_sec) { \
284 nfsm_build(tl, u_int32_t *, 3 * NFSX_UNSIGNED); \
285 *tl++ = txdr_unsigned(NFSV3SATTRTIME_TOCLIENT); \
286 txdr_nfsv3time(&(a)->va_mtime, tl); \
287 } else { \
288 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED); \
289 *tl = txdr_unsigned(NFSV3SATTRTIME_TOSERVER); \
290 } \
291 } else { \
292 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED); \
293 *tl = txdr_unsigned(NFSV3SATTRTIME_DONTCHANGE); \
294 } \
295 }
296
297
298 #define nfsm_strsiz(s,m) \
299 { nfsm_dissect(tl,u_int32_t *,NFSX_UNSIGNED); \
300 if (((s) = fxdr_unsigned(int32_t,*tl)) > (m)) { \
301 m_freem(mrep); \
302 error = EBADRPC; \
303 goto nfsmout; \
304 } }
305
306 #define nfsm_srvstrsiz(s,m) \
307 { nfsm_dissect(tl,u_int32_t *,NFSX_UNSIGNED); \
308 if (((s) = fxdr_unsigned(int32_t,*tl)) > (m) || (s) <= 0) { \
309 error = EBADRPC; \
310 nfsm_reply(0); \
311 } }
312
313 #define nfsm_srvnamesiz(s) \
314 { nfsm_dissect(tl,u_int32_t *,NFSX_UNSIGNED); \
315 if (((s) = fxdr_unsigned(int32_t,*tl)) > NFS_MAXNAMLEN) \
316 error = NFSERR_NAMETOL; \
317 if ((s) <= 0) \
318 error = EBADRPC; \
319 if (error) \
320 nfsm_reply(0); \
321 }
322
323 #define nfsm_mtouio(p,s) \
324 if ((s) > 0 && \
325 (t1 = nfsm_mbuftouio(&md,(p),(s),&dpos)) != 0) { \
326 error = t1; \
327 m_freem(mrep); \
328 goto nfsmout; \
329 }
330
331 #define nfsm_uiotom(p,s) \
332 if ((t1 = nfsm_uiotombuf((p),&mb,(s),&bpos)) != 0) { \
333 error = t1; \
334 m_freem(mreq); \
335 goto nfsmout; \
336 }
337
338 #define nfsm_reqhead(v,a,s) \
339 mb = mreq = nfsm_reqh((v),(a),(s),&bpos)
340
341 #define nfsm_reqdone m_freem(mrep); \
342 nfsmout:
343
344 #define nfsm_rndup(a) (((a)+3)&(~0x3))
345
346 #define nfsm_request(v, t, p, c) \
347 if ((error = nfs_request((v), mreq, (t), (p), \
348 (c), &mrep, &md, &dpos)) != 0) { \
349 if (error & NFSERR_RETERR) \
350 error &= ~NFSERR_RETERR; \
351 else \
352 goto nfsmout; \
353 }
354
355 #define nfsm_strtom(a,s,m) \
356 if ((s) > (m)) { \
357 m_freem(mreq); \
358 error = ENAMETOOLONG; \
359 goto nfsmout; \
360 } \
361 t2 = nfsm_rndup(s)+NFSX_UNSIGNED; \
362 if (t2 <= M_TRAILINGSPACE(mb)) { \
363 nfsm_build(tl,u_int32_t *,t2); \
364 *tl++ = txdr_unsigned(s); \
365 *(tl+((t2>>2)-2)) = 0; \
366 memcpy((caddr_t)tl, (const char *)(a), (s)); \
367 } else if ((t2 = nfsm_strtmbuf(&mb, &bpos, (a), (s))) != 0) { \
368 error = t2; \
369 m_freem(mreq); \
370 goto nfsmout; \
371 }
372
373 #define nfsm_srvdone \
374 nfsmout: \
375 return(error)
376
377 #define nfsm_reply(s) \
378 { \
379 nfsd->nd_repstat = error; \
380 if (error && !(nfsd->nd_flag & ND_NFSV3)) \
381 (void) nfs_rephead(0, nfsd, slp, error, cache, &frev, \
382 mrq, &mb, &bpos); \
383 else \
384 (void) nfs_rephead((s), nfsd, slp, error, cache, &frev, \
385 mrq, &mb, &bpos); \
386 if (mrep != NULL) { \
387 m_freem(mrep); \
388 mrep = NULL; \
389 } \
390 mreq = *mrq; \
391 if (error && (!(nfsd->nd_flag & ND_NFSV3) || \
392 error == EBADRPC)) \
393 return(0); \
394 }
395
396 #define nfsm_writereply(s, v3) \
397 { \
398 nfsd->nd_repstat = error; \
399 if (error && !(v3)) \
400 (void) nfs_rephead(0, nfsd, slp, error, cache, &frev, \
401 &mreq, &mb, &bpos); \
402 else \
403 (void) nfs_rephead((s), nfsd, slp, error, cache, &frev, \
404 &mreq, &mb, &bpos); \
405 }
406
407 #define nfsm_adv(s) \
408 { t1 = mtod(md, caddr_t)+md->m_len-dpos; \
409 if (t1 >= (s)) { \
410 dpos += (s); \
411 } else if ((t1 = nfs_adv(&md, &dpos, (s), t1)) != 0) { \
412 error = t1; \
413 m_freem(mrep); \
414 goto nfsmout; \
415 } }
416
417 #define nfsm_srvmtofh(f) \
418 { int fhlen = NFSX_V3FH; \
419 if (nfsd->nd_flag & ND_NFSV3) { \
420 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
421 fhlen = fxdr_unsigned(int, *tl); \
422 if (fhlen == 0) { \
423 memset((caddr_t)(f), 0, NFSX_V3FH); \
424 } else if (fhlen != NFSX_V3FH) { \
425 error = EBADRPC; \
426 nfsm_reply(0); \
427 } \
428 } \
429 if (fhlen != 0) { \
430 nfsm_dissect(tl, u_int32_t *, NFSX_V3FH); \
431 memcpy( (caddr_t)(f), (caddr_t)tl, NFSX_V3FH); \
432 if ((nfsd->nd_flag & ND_NFSV3) == 0) \
433 nfsm_adv(NFSX_V2FH - NFSX_V3FH); \
434 } \
435 }
436
437 #define nfsm_clget \
438 if (bp >= be) { \
439 if (mp == mb) \
440 mp->m_len += bp-bpos; \
441 mp = m_get(M_WAIT, MT_DATA); \
442 MCLAIM(mb2, &nfs_mowner); \
443 m_clget(mp, M_WAIT); \
444 mp->m_len = NFSMSIZ(mp); \
445 mp2->m_next = mp; \
446 mp2 = mp; \
447 bp = mtod(mp, caddr_t); \
448 be = bp+mp->m_len; \
449 } \
450 tl = (u_int32_t *)bp
451
452 #define nfsm_srvfillattr(a, f) \
453 nfsm_srvfattr(nfsd, (a), (f))
454
455 #define nfsm_srvwcc_data(br, b, ar, a) \
456 nfsm_srvwcc(nfsd, (br), (b), (ar), (a), &mb, &bpos)
457
458 #define nfsm_srvpostop_attr(r, a) \
459 nfsm_srvpostopattr(nfsd, (r), (a), &mb, &bpos)
460
461 #define nfsm_srvsattr(a) \
462 { nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
463 if (*tl == nfs_true) { \
464 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
465 (a)->va_mode = nfstov_mode(*tl); \
466 } \
467 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
468 if (*tl == nfs_true) { \
469 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
470 (a)->va_uid = fxdr_unsigned(uid_t, *tl); \
471 } \
472 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
473 if (*tl == nfs_true) { \
474 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
475 (a)->va_gid = fxdr_unsigned(gid_t, *tl); \
476 } \
477 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
478 if (*tl == nfs_true) { \
479 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED); \
480 (a)->va_size = fxdr_hyper(tl); \
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_atime); \
487 break; \
488 case NFSV3SATTRTIME_TOSERVER: \
489 (a)->va_atime.tv_sec = time.tv_sec; \
490 (a)->va_atime.tv_nsec = time.tv_usec * 1000; \
491 (a)->va_vaflags |= VA_UTIMES_NULL; \
492 break; \
493 }; \
494 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
495 switch (fxdr_unsigned(int, *tl)) { \
496 case NFSV3SATTRTIME_TOCLIENT: \
497 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED); \
498 fxdr_nfsv3time(tl, &(a)->va_mtime); \
499 (a)->va_vaflags &= ~VA_UTIMES_NULL; \
500 break; \
501 case NFSV3SATTRTIME_TOSERVER: \
502 (a)->va_mtime.tv_sec = time.tv_sec; \
503 (a)->va_mtime.tv_nsec = time.tv_usec * 1000; \
504 (a)->va_vaflags |= VA_UTIMES_NULL; \
505 break; \
506 }; }
507
508 #endif
509