1 /* $NetBSD: nfsm_subs.h,v 1.22 2002/10/21 12:52:36 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. 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 MGET(mb2, M_WAIT, MT_DATA); \ 84 if ((s) > MLEN) \ 85 panic("build > MLEN"); \ 86 mb->m_next = mb2; \ 87 mb = mb2; \ 88 mb->m_len = 0; \ 89 bpos = mtod(mb, caddr_t); \ 90 } \ 91 (a) = (c)(bpos); \ 92 mb->m_len += (s); \ 93 bpos += (s); } 94 95 #define nfsm_aligned(p) ALIGNED_POINTER(p,u_int32_t) 96 97 #define nfsm_dissect(a, c, s) \ 98 { t1 = mtod(md, caddr_t)+md->m_len-dpos; \ 99 if (t1 >= (s) && nfsm_aligned(dpos)) { \ 100 (a) = (c)(dpos); \ 101 dpos += (s); \ 102 } else if ((t1 = nfsm_disct(&md, &dpos, (s), t1, &cp2)) != 0){ \ 103 error = t1; \ 104 m_freem(mrep); \ 105 goto nfsmout; \ 106 } else { \ 107 (a) = (c)cp2; \ 108 } } 109 110 #define nfsm_fhtom(v, v3) \ 111 { if (v3) { \ 112 t2 = nfsm_rndup(VTONFS(v)->n_fhsize) + NFSX_UNSIGNED; \ 113 if (t2 <= M_TRAILINGSPACE(mb)) { \ 114 nfsm_build(tl, u_int32_t *, t2); \ 115 *tl++ = txdr_unsigned(VTONFS(v)->n_fhsize); \ 116 *(tl + ((t2>>2) - 2)) = 0; \ 117 memcpy((caddr_t)tl,(caddr_t)VTONFS(v)->n_fhp, \ 118 VTONFS(v)->n_fhsize); \ 119 } else if ((t2 = nfsm_strtmbuf(&mb, &bpos, \ 120 (caddr_t)VTONFS(v)->n_fhp, \ 121 VTONFS(v)->n_fhsize)) != 0) { \ 122 error = t2; \ 123 m_freem(mreq); \ 124 goto nfsmout; \ 125 } \ 126 } else { \ 127 nfsm_build(cp, caddr_t, NFSX_V2FH); \ 128 memcpy(cp, (caddr_t)VTONFS(v)->n_fhp, NFSX_V2FH); \ 129 } } 130 131 #define nfsm_srvfhtom(f, v3) \ 132 { if (v3) { \ 133 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED + NFSX_V3FH); \ 134 *tl++ = txdr_unsigned(NFSX_V3FH); \ 135 memcpy((caddr_t)tl, (caddr_t)(f), NFSX_V3FH); \ 136 } else { \ 137 nfsm_build(cp, caddr_t, NFSX_V2FH); \ 138 memcpy(cp, (caddr_t)(f), NFSX_V2FH); \ 139 } } 140 141 #define nfsm_srvpostop_fh(f) \ 142 { nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED + NFSX_V3FH); \ 143 *tl++ = nfs_true; \ 144 *tl++ = txdr_unsigned(NFSX_V3FH); \ 145 memcpy((caddr_t)tl, (caddr_t)(f), NFSX_V3FH); \ 146 } 147 148 #define nfsm_mtofh(d, v, v3, f) \ 149 { struct nfsnode *ttnp; nfsfh_t *ttfhp; int ttfhsize; \ 150 if (v3) { \ 151 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \ 152 (f) = fxdr_unsigned(int, *tl); \ 153 } else \ 154 (f) = 1; \ 155 if (f) { \ 156 nfsm_getfh(ttfhp, ttfhsize, (v3)); \ 157 if ((t1 = nfs_nget((d)->v_mount, ttfhp, ttfhsize, \ 158 &ttnp)) != 0) { \ 159 error = t1; \ 160 m_freem(mrep); \ 161 goto nfsmout; \ 162 } \ 163 (v) = NFSTOV(ttnp); \ 164 } \ 165 if (v3) { \ 166 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \ 167 if (f) \ 168 (f) = fxdr_unsigned(int, *tl); \ 169 else if (fxdr_unsigned(int, *tl)) \ 170 nfsm_adv(NFSX_V3FATTR); \ 171 } \ 172 if (f) \ 173 nfsm_loadattr((v), (struct vattr *)0, 0); \ 174 } 175 176 #define nfsm_getfh(f, s, v3) \ 177 { if (v3) { \ 178 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \ 179 if (((s) = fxdr_unsigned(int, *tl)) <= 0 || \ 180 (s) > NFSX_V3FHMAX) { \ 181 m_freem(mrep); \ 182 error = EBADRPC; \ 183 goto nfsmout; \ 184 } \ 185 } else \ 186 (s) = NFSX_V2FH; \ 187 nfsm_dissect((f), nfsfh_t *, nfsm_rndup(s)); } 188 189 #define nfsm_loadattr(v, a, flags) \ 190 { struct vnode *ttvp = (v); \ 191 if ((t1 = nfsm_loadattrcache(&ttvp, &md, &dpos, (a), (flags))) \ 192 != 0) { \ 193 error = t1; \ 194 m_freem(mrep); \ 195 goto nfsmout; \ 196 } \ 197 (v) = ttvp; } 198 199 #define nfsm_postop_attr(v, f, flags) \ 200 { struct vnode *ttvp = (v); \ 201 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \ 202 if (((f) = fxdr_unsigned(int, *tl)) != 0) { \ 203 if ((t1 = nfsm_loadattrcache(&ttvp, &md, &dpos, \ 204 (struct vattr *)0, (flags))) != 0) { \ 205 error = t1; \ 206 (f) = 0; \ 207 m_freem(mrep); \ 208 goto nfsmout; \ 209 } \ 210 (v) = ttvp; \ 211 } } 212 213 /* Used as (f) for nfsm_wcc_data() */ 214 #define NFSV3_WCCRATTR 0 215 #define NFSV3_WCCCHK 1 216 217 #define nfsm_wcc_data(v, f, flags) \ 218 { int ttattrf, ttretf = 0; \ 219 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \ 220 if (*tl == nfs_true) { \ 221 nfsm_dissect(tl, u_int32_t *, 6 * NFSX_UNSIGNED); \ 222 if (f) \ 223 ttretf = (VTONFS(v)->n_mtime == \ 224 fxdr_unsigned(u_int32_t, *(tl + 2))); \ 225 } \ 226 nfsm_postop_attr((v), ttattrf, (flags)); \ 227 if (f) { \ 228 (f) = ttretf; \ 229 } else { \ 230 (f) = ttattrf; \ 231 } } 232 233 /* If full is true, set all fields, otherwise just set mode and time fields */ 234 #define nfsm_v3attrbuild(a, full) \ 235 { if ((a)->va_mode != (mode_t)VNOVAL) { \ 236 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED); \ 237 *tl++ = nfs_true; \ 238 *tl = txdr_unsigned((a)->va_mode); \ 239 } else { \ 240 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED); \ 241 *tl = nfs_false; \ 242 } \ 243 if ((full) && (a)->va_uid != (uid_t)VNOVAL) { \ 244 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED); \ 245 *tl++ = nfs_true; \ 246 *tl = txdr_unsigned((a)->va_uid); \ 247 } else { \ 248 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED); \ 249 *tl = nfs_false; \ 250 } \ 251 if ((full) && (a)->va_gid != (gid_t)VNOVAL) { \ 252 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED); \ 253 *tl++ = nfs_true; \ 254 *tl = txdr_unsigned((a)->va_gid); \ 255 } else { \ 256 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED); \ 257 *tl = nfs_false; \ 258 } \ 259 if ((full) && (a)->va_size != VNOVAL) { \ 260 nfsm_build(tl, u_int32_t *, 3 * NFSX_UNSIGNED); \ 261 *tl++ = nfs_true; \ 262 txdr_hyper((a)->va_size, tl); \ 263 } else { \ 264 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED); \ 265 *tl = nfs_false; \ 266 } \ 267 if ((a)->va_atime.tv_sec != VNOVAL) { \ 268 if ((a)->va_atime.tv_sec != time.tv_sec) { \ 269 nfsm_build(tl, u_int32_t *, 3 * NFSX_UNSIGNED); \ 270 *tl++ = txdr_unsigned(NFSV3SATTRTIME_TOCLIENT); \ 271 txdr_nfsv3time(&(a)->va_atime, tl); \ 272 } else { \ 273 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED); \ 274 *tl = txdr_unsigned(NFSV3SATTRTIME_TOSERVER); \ 275 } \ 276 } else { \ 277 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED); \ 278 *tl = txdr_unsigned(NFSV3SATTRTIME_DONTCHANGE); \ 279 } \ 280 if ((a)->va_mtime.tv_sec != VNOVAL) { \ 281 if ((a)->va_mtime.tv_sec != time.tv_sec) { \ 282 nfsm_build(tl, u_int32_t *, 3 * NFSX_UNSIGNED); \ 283 *tl++ = txdr_unsigned(NFSV3SATTRTIME_TOCLIENT); \ 284 txdr_nfsv3time(&(a)->va_mtime, tl); \ 285 } else { \ 286 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED); \ 287 *tl = txdr_unsigned(NFSV3SATTRTIME_TOSERVER); \ 288 } \ 289 } else { \ 290 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED); \ 291 *tl = txdr_unsigned(NFSV3SATTRTIME_DONTCHANGE); \ 292 } \ 293 } 294 295 296 #define nfsm_strsiz(s,m) \ 297 { nfsm_dissect(tl,u_int32_t *,NFSX_UNSIGNED); \ 298 if (((s) = fxdr_unsigned(int32_t,*tl)) > (m)) { \ 299 m_freem(mrep); \ 300 error = EBADRPC; \ 301 goto nfsmout; \ 302 } } 303 304 #define nfsm_srvstrsiz(s,m) \ 305 { nfsm_dissect(tl,u_int32_t *,NFSX_UNSIGNED); \ 306 if (((s) = fxdr_unsigned(int32_t,*tl)) > (m) || (s) <= 0) { \ 307 error = EBADRPC; \ 308 nfsm_reply(0); \ 309 } } 310 311 #define nfsm_srvnamesiz(s) \ 312 { nfsm_dissect(tl,u_int32_t *,NFSX_UNSIGNED); \ 313 if (((s) = fxdr_unsigned(int32_t,*tl)) > NFS_MAXNAMLEN) \ 314 error = NFSERR_NAMETOL; \ 315 if ((s) <= 0) \ 316 error = EBADRPC; \ 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(v,a,s) \ 337 mb = mreq = nfsm_reqh((v),(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 MGET(mp, M_WAIT, MT_DATA); \ 440 MCLGET(mp, M_WAIT); \ 441 mp->m_len = NFSMSIZ(mp); \ 442 mp2->m_next = mp; \ 443 mp2 = mp; \ 444 bp = mtod(mp, caddr_t); \ 445 be = bp+mp->m_len; \ 446 } \ 447 tl = (u_int32_t *)bp 448 449 #define nfsm_srvfillattr(a, f) \ 450 nfsm_srvfattr(nfsd, (a), (f)) 451 452 #define nfsm_srvwcc_data(br, b, ar, a) \ 453 nfsm_srvwcc(nfsd, (br), (b), (ar), (a), &mb, &bpos) 454 455 #define nfsm_srvpostop_attr(r, a) \ 456 nfsm_srvpostopattr(nfsd, (r), (a), &mb, &bpos) 457 458 #define nfsm_srvsattr(a) \ 459 { nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \ 460 if (*tl == nfs_true) { \ 461 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \ 462 (a)->va_mode = nfstov_mode(*tl); \ 463 } \ 464 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \ 465 if (*tl == nfs_true) { \ 466 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \ 467 (a)->va_uid = fxdr_unsigned(uid_t, *tl); \ 468 } \ 469 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \ 470 if (*tl == nfs_true) { \ 471 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \ 472 (a)->va_gid = fxdr_unsigned(gid_t, *tl); \ 473 } \ 474 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \ 475 if (*tl == nfs_true) { \ 476 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED); \ 477 (a)->va_size = fxdr_hyper(tl); \ 478 } \ 479 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \ 480 switch (fxdr_unsigned(int, *tl)) { \ 481 case NFSV3SATTRTIME_TOCLIENT: \ 482 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED); \ 483 fxdr_nfsv3time(tl, &(a)->va_atime); \ 484 break; \ 485 case NFSV3SATTRTIME_TOSERVER: \ 486 (a)->va_atime.tv_sec = time.tv_sec; \ 487 (a)->va_atime.tv_nsec = time.tv_usec * 1000; \ 488 (a)->va_vaflags |= VA_UTIMES_NULL; \ 489 break; \ 490 }; \ 491 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \ 492 switch (fxdr_unsigned(int, *tl)) { \ 493 case NFSV3SATTRTIME_TOCLIENT: \ 494 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED); \ 495 fxdr_nfsv3time(tl, &(a)->va_mtime); \ 496 (a)->va_vaflags &= ~VA_UTIMES_NULL; \ 497 break; \ 498 case NFSV3SATTRTIME_TOSERVER: \ 499 (a)->va_mtime.tv_sec = time.tv_sec; \ 500 (a)->va_mtime.tv_nsec = time.tv_usec * 1000; \ 501 (a)->va_vaflags |= VA_UTIMES_NULL; \ 502 break; \ 503 }; } 504 505 #endif 506