1 /** $MirOS: src/sys/nfs/nfsm_subs.h,v 1.2 2005/03/06 21:28:29 tg Exp $ */ 2 /* $OpenBSD: nfsm_subs.h,v 1.13 2003/06/02 23:28:20 millert Exp $ */ 3 /* $NetBSD: nfsm_subs.h,v 1.10 1996/03/20 21:59:56 fvdl Exp $ */ 4 5 /* 6 * Copyright (c) 1989, 1993 7 * The Regents of the University of California. All rights reserved. 8 * 9 * This code is derived from software contributed to Berkeley by 10 * Rick Macklem at The University of Guelph. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 3. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 * 36 * @(#)nfsm_subs.h 8.2 (Berkeley) 3/30/95 37 */ 38 39 40 #ifndef _NFS_NFSM_SUBS_H_ 41 #define _NFS_NFSM_SUBS_H_ 42 43 44 /* 45 * These macros do strange and peculiar things to mbuf chains for 46 * the assistance of the nfs code. To attempt to use them for any 47 * other purpose will be dangerous. (they make weird assumptions) 48 */ 49 50 /* 51 * First define what the actual subs. return 52 */ 53 54 #define M_HASCL(m) ((m)->m_flags & M_EXT) 55 #define NFSMINOFF(m) \ 56 if (M_HASCL(m)) \ 57 (m)->m_data = (m)->m_ext.ext_buf; \ 58 else if ((m)->m_flags & M_PKTHDR) \ 59 (m)->m_data = (m)->m_pktdat; \ 60 else \ 61 (m)->m_data = (m)->m_dat 62 #define NFSMADV(m, s) (m)->m_data += (s) 63 #define NFSMSIZ(m) ((M_HASCL(m)) ? (m)->m_ext.ext_size : \ 64 (((m)->m_flags & M_PKTHDR) ? MHLEN : MLEN)) 65 66 /* 67 * Now for the macros that do the simple stuff and call the functions 68 * for the hard stuff. 69 * These macros use several vars. declared in nfsm_reqhead and these 70 * vars. must not be used elsewhere unless you are careful not to corrupt 71 * them. The vars. starting with pN and tN (N=1,2,3,..) are temporaries 72 * that may be used so long as the value is not expected to retained 73 * after a macro. 74 * I know, this is kind of dorkey, but it makes the actual op functions 75 * fairly clean and deals with the mess caused by the xdr discriminating 76 * unions. 77 */ 78 79 #define nfsm_build(a,c,s) \ 80 { if ((s) > M_TRAILINGSPACE(mb)) { \ 81 MGET(mb2, M_WAIT, MT_DATA); \ 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_dissect(a, c, s) \ 94 { t1 = mtod(md, caddr_t)+md->m_len-dpos; \ 95 if (t1 >= (s)) { \ 96 (a) = (c)(dpos); \ 97 dpos += (s); \ 98 } else if ((t1 = nfsm_disct(&md, &dpos, (s), t1, &cp2)) != 0){ \ 99 error = t1; \ 100 m_freem(mrep); \ 101 goto nfsmout; \ 102 } else { \ 103 (a) = (c)cp2; \ 104 } } 105 106 #define nfsm_fhtom(v, v3) \ 107 { if (v3) { \ 108 t2 = nfsm_rndup(VTONFS(v)->n_fhsize) + NFSX_UNSIGNED; \ 109 if (t2 <= M_TRAILINGSPACE(mb)) { \ 110 nfsm_build(tl, u_int32_t *, t2); \ 111 *tl++ = txdr_unsigned(VTONFS(v)->n_fhsize); \ 112 *(tl + ((t2>>2) - 2)) = 0; \ 113 memmove((caddr_t)tl,(caddr_t)VTONFS(v)->n_fhp, \ 114 VTONFS(v)->n_fhsize); \ 115 } else if ((t2 = nfsm_strtmbuf(&mb, &bpos, \ 116 (caddr_t)VTONFS(v)->n_fhp, \ 117 VTONFS(v)->n_fhsize)) != 0) { \ 118 error = t2; \ 119 m_freem(mreq); \ 120 goto nfsmout; \ 121 } \ 122 } else { \ 123 nfsm_build(cp, caddr_t, NFSX_V2FH); \ 124 memmove(cp, (caddr_t)VTONFS(v)->n_fhp, NFSX_V2FH); \ 125 } } 126 127 #define nfsm_srvfhtom(f, v3) \ 128 { if (v3) { \ 129 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED + NFSX_V3FH); \ 130 *tl++ = txdr_unsigned(NFSX_V3FH); \ 131 memmove((caddr_t)tl, (caddr_t)(f), NFSX_V3FH); \ 132 } else { \ 133 nfsm_build(cp, caddr_t, NFSX_V2FH); \ 134 memmove(cp, (caddr_t)(f), NFSX_V2FH); \ 135 } } 136 137 #define nfsm_srvpostop_fh(f) \ 138 { nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED + NFSX_V3FH); \ 139 *tl++ = nfs_true; \ 140 *tl++ = txdr_unsigned(NFSX_V3FH); \ 141 memmove((caddr_t)tl, (caddr_t)(f), NFSX_V3FH); \ 142 } 143 144 #define nfsm_mtofh(d, v, v3, f) \ 145 { struct nfsnode *ttnp; nfsfh_t *ttfhp; int ttfhsize; \ 146 if (v3) { \ 147 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \ 148 (f) = fxdr_unsigned(int, *tl); \ 149 } else \ 150 (f) = 1; \ 151 if (f) { \ 152 nfsm_getfh(ttfhp, ttfhsize, (v3)); \ 153 if ((t1 = nfs_nget((d)->v_mount, ttfhp, ttfhsize, \ 154 &ttnp)) != 0) { \ 155 error = t1; \ 156 m_freem(mrep); \ 157 goto nfsmout; \ 158 } \ 159 (v) = NFSTOV(ttnp); \ 160 } \ 161 if (v3) { \ 162 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \ 163 if (f) \ 164 (f) = fxdr_unsigned(int, *tl); \ 165 else if (fxdr_unsigned(int, *tl)) \ 166 nfsm_adv(NFSX_V3FATTR); \ 167 } \ 168 if (f) \ 169 nfsm_loadattr((v), (struct vattr *)0); \ 170 } 171 172 #define nfsm_getfh(f, s, v3) \ 173 { if (v3) { \ 174 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \ 175 if (((s) = fxdr_unsigned(int, *tl)) <= 0 || \ 176 (s) > NFSX_V3FHMAX) { \ 177 m_freem(mrep); \ 178 error = EBADRPC; \ 179 goto nfsmout; \ 180 } \ 181 } else \ 182 (s) = NFSX_V2FH; \ 183 nfsm_dissect((f), nfsfh_t *, nfsm_rndup(s)); } 184 185 #define nfsm_loadattr(v, a) \ 186 { struct vnode *ttvp = (v); \ 187 if ((t1 = nfs_loadattrcache(&ttvp, &md, &dpos, (a))) != 0) { \ 188 error = t1; \ 189 m_freem(mrep); \ 190 goto nfsmout; \ 191 } \ 192 (v) = ttvp; } 193 194 #define nfsm_postop_attr(v, f) \ 195 { struct vnode *ttvp = (v); \ 196 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \ 197 if (((f) = fxdr_unsigned(int, *tl)) != 0) { \ 198 if ((t1 = nfs_loadattrcache(&ttvp, &md, &dpos, \ 199 (struct vattr *)0)) != 0) { \ 200 error = t1; \ 201 (f) = 0; \ 202 m_freem(mrep); \ 203 goto nfsmout; \ 204 } \ 205 (v) = ttvp; \ 206 } } 207 208 /* Used as (f) for nfsm_wcc_data() */ 209 #define NFSV3_WCCRATTR 0 210 #define NFSV3_WCCCHK 1 211 212 #define nfsm_wcc_data(v, f) \ 213 { int ttattrf, ttretf = 0; \ 214 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \ 215 if (*tl == nfs_true) { \ 216 nfsm_dissect(tl, u_int32_t *, 6 * NFSX_UNSIGNED); \ 217 if (f) \ 218 ttretf = (VTONFS(v)->n_mtime == \ 219 fxdr_unsigned(u_int32_t, *(tl + 2))); \ 220 } \ 221 nfsm_postop_attr((v), ttattrf); \ 222 if (f) { \ 223 (f) = ttretf; \ 224 } else { \ 225 (f) = ttattrf; \ 226 } } 227 228 /* If full is true, set all fields, otherwise just set mode and time fields */ 229 #define nfsm_v3attrbuild(a, full) \ 230 { if ((a)->va_mode != (mode_t)VNOVAL) { \ 231 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED); \ 232 *tl++ = nfs_true; \ 233 *tl = txdr_unsigned((a)->va_mode); \ 234 } else { \ 235 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED); \ 236 *tl = nfs_false; \ 237 } \ 238 if ((full) && (a)->va_uid != (uid_t)VNOVAL) { \ 239 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED); \ 240 *tl++ = nfs_true; \ 241 *tl = txdr_unsigned((a)->va_uid); \ 242 } else { \ 243 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED); \ 244 *tl = nfs_false; \ 245 } \ 246 if ((full) && (a)->va_gid != (gid_t)VNOVAL) { \ 247 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED); \ 248 *tl++ = nfs_true; \ 249 *tl = txdr_unsigned((a)->va_gid); \ 250 } else { \ 251 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED); \ 252 *tl = nfs_false; \ 253 } \ 254 if ((full) && (a)->va_size != VNOVAL) { \ 255 nfsm_build(tl, u_int32_t *, 3 * NFSX_UNSIGNED); \ 256 *tl++ = nfs_true; \ 257 txdr_hyper((a)->va_size, tl); \ 258 } else { \ 259 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED); \ 260 *tl = nfs_false; \ 261 } \ 262 if ((a)->va_atime.tv_sec != VNOVAL) { \ 263 if ((a)->va_atime.tv_sec != time.tv_sec) { \ 264 nfsm_build(tl, u_int32_t *, 3 * NFSX_UNSIGNED); \ 265 *tl++ = txdr_unsigned(NFSV3SATTRTIME_TOCLIENT); \ 266 txdr_nfsv3time(&(a)->va_atime, tl); \ 267 } else { \ 268 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED); \ 269 *tl = txdr_unsigned(NFSV3SATTRTIME_TOSERVER); \ 270 } \ 271 } else { \ 272 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED); \ 273 *tl = txdr_unsigned(NFSV3SATTRTIME_DONTCHANGE); \ 274 } \ 275 if ((a)->va_mtime.tv_sec != VNOVAL) { \ 276 if ((a)->va_mtime.tv_sec != time.tv_sec) { \ 277 nfsm_build(tl, u_int32_t *, 3 * NFSX_UNSIGNED); \ 278 *tl++ = txdr_unsigned(NFSV3SATTRTIME_TOCLIENT); \ 279 txdr_nfsv3time(&(a)->va_mtime, tl); \ 280 } else { \ 281 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED); \ 282 *tl = txdr_unsigned(NFSV3SATTRTIME_TOSERVER); \ 283 } \ 284 } else { \ 285 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED); \ 286 *tl = txdr_unsigned(NFSV3SATTRTIME_DONTCHANGE); \ 287 } \ 288 } 289 290 291 #define nfsm_strsiz(s,m) \ 292 { nfsm_dissect(tl,u_int32_t *,NFSX_UNSIGNED); \ 293 if (((s) = fxdr_unsigned(int32_t,*tl)) > (m)) { \ 294 m_freem(mrep); \ 295 error = EBADRPC; \ 296 goto nfsmout; \ 297 } } 298 299 #define nfsm_srvstrsiz(s,m) \ 300 { nfsm_dissect(tl,u_int32_t *,NFSX_UNSIGNED); \ 301 if (((s) = fxdr_unsigned(int32_t,*tl)) > (m) || (s) <= 0) { \ 302 error = EBADRPC; \ 303 nfsm_reply(0); \ 304 } } 305 306 #define nfsm_srvnamesiz(s) \ 307 { nfsm_dissect(tl,u_int32_t *,NFSX_UNSIGNED); \ 308 if (((s) = fxdr_unsigned(int32_t,*tl)) > NFS_MAXNAMLEN) \ 309 error = NFSERR_NAMETOL; \ 310 if ((s) <= 0) \ 311 error = EBADRPC; \ 312 if (error) \ 313 nfsm_reply(0); \ 314 } 315 316 #define nfsm_mtouio(p,s) \ 317 if ((s) > 0 && \ 318 (t1 = nfsm_mbuftouio(&md,(p),(s),&dpos)) != 0) { \ 319 error = t1; \ 320 m_freem(mrep); \ 321 goto nfsmout; \ 322 } 323 324 #define nfsm_uiotom(p,s) \ 325 if ((t1 = nfsm_uiotombuf((p),&mb,(s),&bpos)) != 0) { \ 326 error = t1; \ 327 m_freem(mreq); \ 328 goto nfsmout; \ 329 } 330 331 #define nfsm_reqhead(v,a,s) \ 332 mb = mreq = nfsm_reqh((v),(a),(s),&bpos) 333 334 #define nfsm_reqdone m_freem(mrep); \ 335 nfsmout: 336 337 #define nfsm_rndup(a) (((a)+3)&(~0x3)) 338 339 #define nfsm_request(v, t, p, c) \ 340 if ((error = nfs_request((v), mreq, (t), (p), \ 341 (c), &mrep, &md, &dpos)) != 0) { \ 342 if (error & NFSERR_RETERR) \ 343 error &= ~NFSERR_RETERR; \ 344 else \ 345 goto nfsmout; \ 346 } 347 348 #define nfsm_strtom(a,s,m) \ 349 if ((s) > (m)) { \ 350 m_freem(mreq); \ 351 error = ENAMETOOLONG; \ 352 goto nfsmout; \ 353 } \ 354 t2 = nfsm_rndup(s)+NFSX_UNSIGNED; \ 355 if (t2 <= M_TRAILINGSPACE(mb)) { \ 356 nfsm_build(tl,u_int32_t *,t2); \ 357 *tl++ = txdr_unsigned(s); \ 358 *(tl+((t2>>2)-2)) = 0; \ 359 memmove((caddr_t)tl, (caddr_t)(a), (s)); \ 360 } else if ((t2 = nfsm_strtmbuf(&mb, &bpos, (a), (s))) != 0) { \ 361 error = t2; \ 362 m_freem(mreq); \ 363 goto nfsmout; \ 364 } 365 366 #define nfsm_srvdone \ 367 nfsmout: \ 368 return(error) 369 370 #define nfsm_reply(s) \ 371 { \ 372 nfsd->nd_repstat = error; \ 373 if (error && !(nfsd->nd_flag & ND_NFSV3)) \ 374 (void) nfs_rephead(0, nfsd, slp, error, &frev, \ 375 mrq, &mb, &bpos); \ 376 else \ 377 (void) nfs_rephead((s), nfsd, slp, error, &frev, \ 378 mrq, &mb, &bpos); \ 379 if (mrep != NULL) { \ 380 m_freem(mrep); \ 381 mrep = NULL; \ 382 } \ 383 mreq = *mrq; \ 384 if (error && (!(nfsd->nd_flag & ND_NFSV3) || \ 385 error == EBADRPC)) \ 386 return(0); \ 387 } 388 389 #define nfsm_writereply(s, v3) \ 390 { \ 391 nfsd->nd_repstat = error; \ 392 if (error && !(v3)) \ 393 (void) nfs_rephead(0, nfsd, slp, error, &frev, \ 394 &mreq, &mb, &bpos); \ 395 else \ 396 (void) nfs_rephead((s), nfsd, slp, error, &frev, \ 397 &mreq, &mb, &bpos); \ 398 } 399 400 #define nfsm_adv(s) \ 401 { t1 = mtod(md, caddr_t)+md->m_len-dpos; \ 402 if (t1 >= (s)) { \ 403 dpos += (s); \ 404 } else if ((t1 = nfs_adv(&md, &dpos, (s), t1)) != 0) { \ 405 error = t1; \ 406 m_freem(mrep); \ 407 goto nfsmout; \ 408 } } 409 410 #define nfsm_srvmtofh(f) \ 411 { if (nfsd->nd_flag & ND_NFSV3) { \ 412 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \ 413 if (fxdr_unsigned(int, *tl) != NFSX_V3FH) { \ 414 error = EBADRPC; \ 415 nfsm_reply(0); \ 416 } \ 417 } \ 418 nfsm_dissect(tl, u_int32_t *, NFSX_V3FH); \ 419 memmove((caddr_t)(f), (caddr_t)tl, NFSX_V3FH); \ 420 if ((nfsd->nd_flag & ND_NFSV3) == 0) \ 421 nfsm_adv(NFSX_V2FH - NFSX_V3FH); \ 422 } 423 424 #define nfsm_clget \ 425 if (bp >= be) { \ 426 if (mp == mb) \ 427 mp->m_len += bp-bpos; \ 428 MGET(mp, M_WAIT, MT_DATA); \ 429 MCLGET(mp, M_WAIT); \ 430 mp->m_len = NFSMSIZ(mp); \ 431 mp2->m_next = mp; \ 432 mp2 = mp; \ 433 bp = mtod(mp, caddr_t); \ 434 be = bp+mp->m_len; \ 435 } \ 436 tl = (u_int32_t *)bp 437 438 #define nfsm_srvfillattr(a, f) \ 439 nfsm_srvfattr(nfsd, (a), (f)) 440 441 #define nfsm_srvwcc_data(br, b, ar, a) \ 442 nfsm_srvwcc(nfsd, (br), (b), (ar), (a), &mb, &bpos) 443 444 #define nfsm_srvpostop_attr(r, a) \ 445 nfsm_srvpostopattr(nfsd, (r), (a), &mb, &bpos) 446 447 #define nfsm_srvsattr(a) \ 448 { nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \ 449 if (*tl == nfs_true) { \ 450 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \ 451 (a)->va_mode = nfstov_mode(*tl); \ 452 } \ 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_uid = fxdr_unsigned(uid_t, *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_gid = fxdr_unsigned(gid_t, *tl); \ 462 } \ 463 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \ 464 if (*tl == nfs_true) { \ 465 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED); \ 466 (a)->va_size = fxdr_hyper(tl); \ 467 } \ 468 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \ 469 switch (fxdr_unsigned(int, *tl)) { \ 470 case NFSV3SATTRTIME_TOCLIENT: \ 471 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED); \ 472 fxdr_nfsv3time(tl, &(a)->va_atime); \ 473 break; \ 474 case NFSV3SATTRTIME_TOSERVER: \ 475 (a)->va_atime.tv_sec = time.tv_sec; \ 476 (a)->va_atime.tv_nsec = time.tv_usec * 1000; \ 477 break; \ 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_mtime); \ 484 break; \ 485 case NFSV3SATTRTIME_TOSERVER: \ 486 (a)->va_mtime.tv_sec = time.tv_sec; \ 487 (a)->va_mtime.tv_nsec = time.tv_usec * 1000; \ 488 break; \ 489 }; } 490 491 #endif 492