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