nfsm_subs.h revision 1.32 1 /* $NetBSD: nfsm_subs.h,v 1.32 2003/09/26 11:51:53 yamt 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 struct timespec mtime; \
222 fxdr_nfsv3time(tl + 2, &mtime); \
223 ttretf = timespeccmp(&VTONFS(v)->n_mtime, \
224 &mtime, ==); \
225 } \
226 } \
227 nfsm_postop_attr((v), ttattrf, (flags)); \
228 if (f) { \
229 (f) = ttretf; \
230 } else { \
231 (f) = ttattrf; \
232 } }
233
234 /* If full is true, set all fields, otherwise just set mode and time fields */
235 #define nfsm_v3attrbuild(a, full) \
236 { if ((a)->va_mode != (mode_t)VNOVAL) { \
237 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED); \
238 *tl++ = nfs_true; \
239 *tl = txdr_unsigned((a)->va_mode); \
240 } else { \
241 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED); \
242 *tl = nfs_false; \
243 } \
244 if ((full) && (a)->va_uid != (uid_t)VNOVAL) { \
245 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED); \
246 *tl++ = nfs_true; \
247 *tl = txdr_unsigned((a)->va_uid); \
248 } else { \
249 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED); \
250 *tl = nfs_false; \
251 } \
252 if ((full) && (a)->va_gid != (gid_t)VNOVAL) { \
253 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED); \
254 *tl++ = nfs_true; \
255 *tl = txdr_unsigned((a)->va_gid); \
256 } else { \
257 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED); \
258 *tl = nfs_false; \
259 } \
260 if ((full) && (a)->va_size != VNOVAL) { \
261 nfsm_build(tl, u_int32_t *, 3 * NFSX_UNSIGNED); \
262 *tl++ = nfs_true; \
263 txdr_hyper((a)->va_size, tl); \
264 } else { \
265 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED); \
266 *tl = nfs_false; \
267 } \
268 if ((a)->va_atime.tv_sec != VNOVAL) { \
269 if ((a)->va_atime.tv_sec != time.tv_sec) { \
270 nfsm_build(tl, u_int32_t *, 3 * NFSX_UNSIGNED); \
271 *tl++ = txdr_unsigned(NFSV3SATTRTIME_TOCLIENT); \
272 txdr_nfsv3time(&(a)->va_atime, tl); \
273 } else { \
274 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED); \
275 *tl = txdr_unsigned(NFSV3SATTRTIME_TOSERVER); \
276 } \
277 } else { \
278 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED); \
279 *tl = txdr_unsigned(NFSV3SATTRTIME_DONTCHANGE); \
280 } \
281 if ((a)->va_mtime.tv_sec != VNOVAL) { \
282 if ((a)->va_mtime.tv_sec != time.tv_sec) { \
283 nfsm_build(tl, u_int32_t *, 3 * NFSX_UNSIGNED); \
284 *tl++ = txdr_unsigned(NFSV3SATTRTIME_TOCLIENT); \
285 txdr_nfsv3time(&(a)->va_mtime, tl); \
286 } else { \
287 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED); \
288 *tl = txdr_unsigned(NFSV3SATTRTIME_TOSERVER); \
289 } \
290 } else { \
291 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED); \
292 *tl = txdr_unsigned(NFSV3SATTRTIME_DONTCHANGE); \
293 } \
294 }
295
296
297 #define nfsm_strsiz(s,m) \
298 { nfsm_dissect(tl,uint32_t *,NFSX_UNSIGNED); \
299 if (((s) = fxdr_unsigned(uint32_t,*tl)) > (m)) { \
300 m_freem(mrep); \
301 error = EBADRPC; \
302 goto nfsmout; \
303 } }
304
305 #define nfsm_srvnamesiz(s) \
306 { nfsm_dissect(tl,uint32_t *,NFSX_UNSIGNED); \
307 if (((s) = fxdr_unsigned(uint32_t,*tl)) > NFS_MAXNAMLEN) \
308 error = NFSERR_NAMETOL; \
309 if (error) \
310 nfsm_reply(0); \
311 }
312
313 #define nfsm_mtouio(p,s) \
314 if ((s) > 0 && \
315 (t1 = nfsm_mbuftouio(&md,(p),(s),&dpos)) != 0) { \
316 error = t1; \
317 m_freem(mrep); \
318 goto nfsmout; \
319 }
320
321 #define nfsm_uiotom(p,s) \
322 if ((t1 = nfsm_uiotombuf((p),&mb,(s),&bpos)) != 0) { \
323 error = t1; \
324 m_freem(mreq); \
325 goto nfsmout; \
326 }
327
328 #define nfsm_reqhead(n,a,s) \
329 mb = mreq = nfsm_reqh((n),(a),(s),&bpos)
330
331 #define nfsm_reqdone m_freem(mrep); \
332 nfsmout:
333
334 #define nfsm_rndup(a) (((a)+3)&(~0x3))
335 #define nfsm_padlen(a) (nfsm_rndup(a) - (a))
336
337 #define nfsm_request(v, t, p, c) \
338 if ((error = nfs_request((v), mreq, (t), (p), \
339 (c), &mrep, &md, &dpos)) != 0) { \
340 if (error & NFSERR_RETERR) \
341 error &= ~NFSERR_RETERR; \
342 else \
343 goto nfsmout; \
344 }
345
346 #define nfsm_strtom(a,s,m) \
347 if ((s) > (m)) { \
348 m_freem(mreq); \
349 error = ENAMETOOLONG; \
350 goto nfsmout; \
351 } \
352 t2 = nfsm_rndup(s)+NFSX_UNSIGNED; \
353 if (t2 <= M_TRAILINGSPACE(mb)) { \
354 nfsm_build(tl,u_int32_t *,t2); \
355 *tl++ = txdr_unsigned(s); \
356 *(tl+((t2>>2)-2)) = 0; \
357 memcpy((caddr_t)tl, (const char *)(a), (s)); \
358 } else if ((t2 = nfsm_strtmbuf(&mb, &bpos, (a), (s))) != 0) { \
359 error = t2; \
360 m_freem(mreq); \
361 goto nfsmout; \
362 }
363
364 #define nfsm_srvdone \
365 nfsmout: \
366 return(error)
367
368 #define nfsm_reply(s) \
369 { \
370 nfsd->nd_repstat = error; \
371 if (error && !(nfsd->nd_flag & ND_NFSV3)) \
372 (void) nfs_rephead(0, nfsd, slp, error, cache, &frev, \
373 mrq, &mb, &bpos); \
374 else \
375 (void) nfs_rephead((s), nfsd, slp, error, cache, &frev, \
376 mrq, &mb, &bpos); \
377 if (mrep != NULL) { \
378 m_freem(mrep); \
379 mrep = NULL; \
380 } \
381 mreq = *mrq; \
382 if (error && (!(nfsd->nd_flag & ND_NFSV3) || \
383 error == EBADRPC)) \
384 return(0); \
385 }
386
387 #define nfsm_writereply(s, v3) \
388 { \
389 nfsd->nd_repstat = error; \
390 if (error && !(v3)) \
391 (void) nfs_rephead(0, nfsd, slp, error, cache, &frev, \
392 &mreq, &mb, &bpos); \
393 else \
394 (void) nfs_rephead((s), nfsd, slp, error, cache, &frev, \
395 &mreq, &mb, &bpos); \
396 }
397
398 #define nfsm_adv(s) \
399 { t1 = mtod(md, caddr_t)+md->m_len-dpos; \
400 if (t1 >= (s)) { \
401 dpos += (s); \
402 } else if ((t1 = nfs_adv(&md, &dpos, (s), t1)) != 0) { \
403 error = t1; \
404 m_freem(mrep); \
405 goto nfsmout; \
406 } }
407
408 #define nfsm_srvmtofh(f) \
409 { int fhlen = NFSX_V3FH; \
410 if (nfsd->nd_flag & ND_NFSV3) { \
411 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
412 fhlen = fxdr_unsigned(int, *tl); \
413 if (fhlen == 0) { \
414 memset((caddr_t)(f), 0, NFSX_V3FH); \
415 } else if (fhlen != NFSX_V3FH) { \
416 error = EBADRPC; \
417 nfsm_reply(0); \
418 } \
419 } \
420 if (fhlen != 0) { \
421 nfsm_dissect(tl, u_int32_t *, NFSX_V3FH); \
422 memcpy( (caddr_t)(f), (caddr_t)tl, NFSX_V3FH); \
423 if ((nfsd->nd_flag & ND_NFSV3) == 0) \
424 nfsm_adv(NFSX_V2FH - NFSX_V3FH); \
425 } \
426 }
427
428 #define nfsm_clget \
429 if (bp >= be) { \
430 if (mp == mb) \
431 mp->m_len += bp-bpos; \
432 mp = m_get(M_WAIT, MT_DATA); \
433 MCLAIM(mp, &nfs_mowner); \
434 m_clget(mp, M_WAIT); \
435 mp->m_len = NFSMSIZ(mp); \
436 mp2->m_next = mp; \
437 mp2 = mp; \
438 bp = mtod(mp, caddr_t); \
439 be = bp+mp->m_len; \
440 } \
441 tl = (u_int32_t *)bp
442
443 #define nfsm_srvfillattr(a, f) \
444 nfsm_srvfattr(nfsd, (a), (f))
445
446 #define nfsm_srvwcc_data(br, b, ar, a) \
447 nfsm_srvwcc(nfsd, (br), (b), (ar), (a), &mb, &bpos)
448
449 #define nfsm_srvpostop_attr(r, a) \
450 nfsm_srvpostopattr(nfsd, (r), (a), &mb, &bpos)
451
452 #define nfsm_srvsattr(a) \
453 { nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
454 if (*tl == nfs_true) { \
455 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
456 (a)->va_mode = nfstov_mode(*tl); \
457 } \
458 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
459 if (*tl == nfs_true) { \
460 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
461 (a)->va_uid = fxdr_unsigned(uid_t, *tl); \
462 } \
463 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
464 if (*tl == nfs_true) { \
465 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
466 (a)->va_gid = fxdr_unsigned(gid_t, *tl); \
467 } \
468 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
469 if (*tl == nfs_true) { \
470 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED); \
471 (a)->va_size = fxdr_hyper(tl); \
472 } \
473 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
474 switch (fxdr_unsigned(int, *tl)) { \
475 case NFSV3SATTRTIME_TOCLIENT: \
476 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED); \
477 fxdr_nfsv3time(tl, &(a)->va_atime); \
478 break; \
479 case NFSV3SATTRTIME_TOSERVER: \
480 (a)->va_atime.tv_sec = time.tv_sec; \
481 (a)->va_atime.tv_nsec = time.tv_usec * 1000; \
482 (a)->va_vaflags |= VA_UTIMES_NULL; \
483 break; \
484 }; \
485 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
486 switch (fxdr_unsigned(int, *tl)) { \
487 case NFSV3SATTRTIME_TOCLIENT: \
488 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED); \
489 fxdr_nfsv3time(tl, &(a)->va_mtime); \
490 (a)->va_vaflags &= ~VA_UTIMES_NULL; \
491 break; \
492 case NFSV3SATTRTIME_TOSERVER: \
493 (a)->va_mtime.tv_sec = time.tv_sec; \
494 (a)->va_mtime.tv_nsec = time.tv_usec * 1000; \
495 (a)->va_vaflags |= VA_UTIMES_NULL; \
496 break; \
497 }; }
498
499 #endif
500