nfsm_subs.h revision 1.26
1/* $NetBSD: nfsm_subs.h,v 1.26 2003/04/24 21:21:07 drochner 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(n, v3) \ 113 { if (v3) { \ 114 t2 = nfsm_rndup((n)->n_fhsize) + NFSX_UNSIGNED; \ 115 if (t2 <= M_TRAILINGSPACE(mb)) { \ 116 nfsm_build(tl, u_int32_t *, t2); \ 117 *tl++ = txdr_unsigned((n)->n_fhsize); \ 118 *(tl + ((t2>>2) - 2)) = 0; \ 119 memcpy((caddr_t)tl,(caddr_t)(n)->n_fhp, \ 120 (n)->n_fhsize); \ 121 } else if ((t2 = nfsm_strtmbuf(&mb, &bpos, \ 122 (caddr_t)(n)->n_fhp, \ 123 (n)->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)(n)->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,uint32_t *,NFSX_UNSIGNED); \ 300 if (((s) = fxdr_unsigned(uint32_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,uint32_t *,NFSX_UNSIGNED); \ 308 if (((s) = fxdr_unsigned(uint32_t,*tl)) > (m) || (s) <= 0) { \ 309 error = EBADRPC; \ 310 nfsm_reply(0); \ 311 } } 312 313#define nfsm_srvnamesiz(s) \ 314 { nfsm_dissect(tl,uint32_t *,NFSX_UNSIGNED); \ 315 if (((s) = fxdr_unsigned(uint32_t,*tl)) > NFS_MAXNAMLEN) \ 316 error = NFSERR_NAMETOL; \ 317 if (error) \ 318 nfsm_reply(0); \ 319 } 320 321#define nfsm_mtouio(p,s) \ 322 if ((s) > 0 && \ 323 (t1 = nfsm_mbuftouio(&md,(p),(s),&dpos)) != 0) { \ 324 error = t1; \ 325 m_freem(mrep); \ 326 goto nfsmout; \ 327 } 328 329#define nfsm_uiotom(p,s) \ 330 if ((t1 = nfsm_uiotombuf((p),&mb,(s),&bpos)) != 0) { \ 331 error = t1; \ 332 m_freem(mreq); \ 333 goto nfsmout; \ 334 } 335 336#define nfsm_reqhead(n,a,s) \ 337 mb = mreq = nfsm_reqh((n),(a),(s),&bpos) 338 339#define nfsm_reqdone m_freem(mrep); \ 340 nfsmout: 341 342#define nfsm_rndup(a) (((a)+3)&(~0x3)) 343 344#define nfsm_request(v, t, p, c) \ 345 if ((error = nfs_request((v), mreq, (t), (p), \ 346 (c), &mrep, &md, &dpos)) != 0) { \ 347 if (error & NFSERR_RETERR) \ 348 error &= ~NFSERR_RETERR; \ 349 else \ 350 goto nfsmout; \ 351 } 352 353#define nfsm_strtom(a,s,m) \ 354 if ((s) > (m)) { \ 355 m_freem(mreq); \ 356 error = ENAMETOOLONG; \ 357 goto nfsmout; \ 358 } \ 359 t2 = nfsm_rndup(s)+NFSX_UNSIGNED; \ 360 if (t2 <= M_TRAILINGSPACE(mb)) { \ 361 nfsm_build(tl,u_int32_t *,t2); \ 362 *tl++ = txdr_unsigned(s); \ 363 *(tl+((t2>>2)-2)) = 0; \ 364 memcpy((caddr_t)tl, (const char *)(a), (s)); \ 365 } else if ((t2 = nfsm_strtmbuf(&mb, &bpos, (a), (s))) != 0) { \ 366 error = t2; \ 367 m_freem(mreq); \ 368 goto nfsmout; \ 369 } 370 371#define nfsm_srvdone \ 372 nfsmout: \ 373 return(error) 374 375#define nfsm_reply(s) \ 376 { \ 377 nfsd->nd_repstat = error; \ 378 if (error && !(nfsd->nd_flag & ND_NFSV3)) \ 379 (void) nfs_rephead(0, nfsd, slp, error, cache, &frev, \ 380 mrq, &mb, &bpos); \ 381 else \ 382 (void) nfs_rephead((s), nfsd, slp, error, cache, &frev, \ 383 mrq, &mb, &bpos); \ 384 if (mrep != NULL) { \ 385 m_freem(mrep); \ 386 mrep = NULL; \ 387 } \ 388 mreq = *mrq; \ 389 if (error && (!(nfsd->nd_flag & ND_NFSV3) || \ 390 error == EBADRPC)) \ 391 return(0); \ 392 } 393 394#define nfsm_writereply(s, v3) \ 395 { \ 396 nfsd->nd_repstat = error; \ 397 if (error && !(v3)) \ 398 (void) nfs_rephead(0, nfsd, slp, error, cache, &frev, \ 399 &mreq, &mb, &bpos); \ 400 else \ 401 (void) nfs_rephead((s), nfsd, slp, error, cache, &frev, \ 402 &mreq, &mb, &bpos); \ 403 } 404 405#define nfsm_adv(s) \ 406 { t1 = mtod(md, caddr_t)+md->m_len-dpos; \ 407 if (t1 >= (s)) { \ 408 dpos += (s); \ 409 } else if ((t1 = nfs_adv(&md, &dpos, (s), t1)) != 0) { \ 410 error = t1; \ 411 m_freem(mrep); \ 412 goto nfsmout; \ 413 } } 414 415#define nfsm_srvmtofh(f) \ 416 { int fhlen = NFSX_V3FH; \ 417 if (nfsd->nd_flag & ND_NFSV3) { \ 418 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \ 419 fhlen = fxdr_unsigned(int, *tl); \ 420 if (fhlen == 0) { \ 421 memset((caddr_t)(f), 0, NFSX_V3FH); \ 422 } else if (fhlen != NFSX_V3FH) { \ 423 error = EBADRPC; \ 424 nfsm_reply(0); \ 425 } \ 426 } \ 427 if (fhlen != 0) { \ 428 nfsm_dissect(tl, u_int32_t *, NFSX_V3FH); \ 429 memcpy( (caddr_t)(f), (caddr_t)tl, NFSX_V3FH); \ 430 if ((nfsd->nd_flag & ND_NFSV3) == 0) \ 431 nfsm_adv(NFSX_V2FH - NFSX_V3FH); \ 432 } \ 433 } 434 435#define nfsm_clget \ 436 if (bp >= be) { \ 437 if (mp == mb) \ 438 mp->m_len += bp-bpos; \ 439 mp = m_get(M_WAIT, MT_DATA); \ 440 MCLAIM(mp, &nfs_mowner); \ 441 m_clget(mp, M_WAIT); \ 442 mp->m_len = NFSMSIZ(mp); \ 443 mp2->m_next = mp; \ 444 mp2 = mp; \ 445 bp = mtod(mp, caddr_t); \ 446 be = bp+mp->m_len; \ 447 } \ 448 tl = (u_int32_t *)bp 449 450#define nfsm_srvfillattr(a, f) \ 451 nfsm_srvfattr(nfsd, (a), (f)) 452 453#define nfsm_srvwcc_data(br, b, ar, a) \ 454 nfsm_srvwcc(nfsd, (br), (b), (ar), (a), &mb, &bpos) 455 456#define nfsm_srvpostop_attr(r, a) \ 457 nfsm_srvpostopattr(nfsd, (r), (a), &mb, &bpos) 458 459#define nfsm_srvsattr(a) \ 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_mode = nfstov_mode(*tl); \ 464 } \ 465 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \ 466 if (*tl == nfs_true) { \ 467 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \ 468 (a)->va_uid = fxdr_unsigned(uid_t, *tl); \ 469 } \ 470 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \ 471 if (*tl == nfs_true) { \ 472 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \ 473 (a)->va_gid = fxdr_unsigned(gid_t, *tl); \ 474 } \ 475 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \ 476 if (*tl == nfs_true) { \ 477 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED); \ 478 (a)->va_size = fxdr_hyper(tl); \ 479 } \ 480 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \ 481 switch (fxdr_unsigned(int, *tl)) { \ 482 case NFSV3SATTRTIME_TOCLIENT: \ 483 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED); \ 484 fxdr_nfsv3time(tl, &(a)->va_atime); \ 485 break; \ 486 case NFSV3SATTRTIME_TOSERVER: \ 487 (a)->va_atime.tv_sec = time.tv_sec; \ 488 (a)->va_atime.tv_nsec = time.tv_usec * 1000; \ 489 (a)->va_vaflags |= VA_UTIMES_NULL; \ 490 break; \ 491 }; \ 492 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \ 493 switch (fxdr_unsigned(int, *tl)) { \ 494 case NFSV3SATTRTIME_TOCLIENT: \ 495 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED); \ 496 fxdr_nfsv3time(tl, &(a)->va_mtime); \ 497 (a)->va_vaflags &= ~VA_UTIMES_NULL; \ 498 break; \ 499 case NFSV3SATTRTIME_TOSERVER: \ 500 (a)->va_mtime.tv_sec = time.tv_sec; \ 501 (a)->va_mtime.tv_nsec = time.tv_usec * 1000; \ 502 (a)->va_vaflags |= VA_UTIMES_NULL; \ 503 break; \ 504 }; } 505 506#endif 507