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