nfsm_subs.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /* $OpenBSD: nfsm_subs.h,v 1.44 2014/12/18 20:59:21 tedu Exp $ */
  2. /* $NetBSD: nfsm_subs.h,v 1.10 1996/03/20 21:59:56 fvdl Exp $ */
  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. #ifndef _NFS_NFSM_SUBS_H_
  37. #define _NFS_NFSM_SUBS_H_
  38. struct nfsm_info {
  39. struct mbuf *nmi_mreq;
  40. struct mbuf *nmi_mrep;
  41. struct proc *nmi_procp; /* XXX XXX XXX */
  42. struct ucred *nmi_cred; /* XXX XXX XXX */
  43. /* Setting up / Tearing down. */
  44. struct mbuf *nmi_md;
  45. struct mbuf *nmi_mb;
  46. caddr_t nmi_dpos;
  47. int nmi_v3;
  48. };
  49. #define nfsm_dissect(a, c, s) { \
  50. t1 = mtod(info.nmi_md, caddr_t) + info.nmi_md->m_len - \
  51. info.nmi_dpos; \
  52. if (t1 >= (s)) { \
  53. (a) = (c)(info.nmi_dpos); \
  54. info.nmi_dpos += (s); \
  55. } else if ((t1 = \
  56. nfsm_disct(&info.nmi_md, &info.nmi_dpos, (s), t1, \
  57. &cp2)) != 0) { \
  58. error = t1; \
  59. m_freem(info.nmi_mrep); \
  60. goto nfsmout; \
  61. } else { \
  62. (a) = (c)cp2; \
  63. } \
  64. }
  65. #define nfsm_srvpostop_fh(f) { \
  66. tl = nfsm_build(&info.nmi_mb, 2 * NFSX_UNSIGNED + NFSX_V3FH); \
  67. *tl++ = nfs_true; \
  68. *tl++ = txdr_unsigned(NFSX_V3FH); \
  69. bcopy((f), tl, NFSX_V3FH); \
  70. }
  71. #define nfsm_mtofh(d, v, v3, f) { \
  72. struct nfsnode *ttnp; nfsfh_t *ttfhp; int ttfhsize; \
  73. if (v3) { \
  74. nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
  75. (f) = fxdr_unsigned(int, *tl); \
  76. } else \
  77. (f) = 1; \
  78. if (f) { \
  79. nfsm_getfh(ttfhp, ttfhsize, (v3)); \
  80. if ((t1 = nfs_nget((d)->v_mount, ttfhp, ttfhsize, \
  81. &ttnp)) != 0) { \
  82. error = t1; \
  83. m_freem(info.nmi_mrep); \
  84. goto nfsmout; \
  85. } \
  86. (v) = NFSTOV(ttnp); \
  87. } \
  88. if (v3) { \
  89. nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
  90. if (f) \
  91. (f) = fxdr_unsigned(int, *tl); \
  92. else if (fxdr_unsigned(int, *tl)) \
  93. nfsm_adv(NFSX_V3FATTR); \
  94. } \
  95. if (f) \
  96. nfsm_loadattr((v), NULL); \
  97. }
  98. #define nfsm_getfh(f, s, v3) { \
  99. if (v3) { \
  100. nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
  101. if (((s) = fxdr_unsigned(int, *tl)) <= 0 || \
  102. (s) > NFSX_V3FHMAX) { \
  103. m_freem(info.nmi_mrep); \
  104. error = EBADRPC; \
  105. goto nfsmout; \
  106. } \
  107. } else \
  108. (s) = NFSX_V2FH; \
  109. nfsm_dissect((f), nfsfh_t *, nfsm_rndup(s)); \
  110. }
  111. #define nfsm_loadattr(v, a) { \
  112. struct vnode *ttvp = (v); \
  113. if ((t1 = nfs_loadattrcache(&ttvp, &info.nmi_md, \
  114. &info.nmi_dpos, (a))) != 0) { \
  115. error = t1; \
  116. m_freem(info.nmi_mrep); \
  117. goto nfsmout; \
  118. } \
  119. (v) = ttvp; \
  120. }
  121. #define nfsm_postop_attr(v, f) { if (info.nmi_mrep != NULL) { \
  122. struct vnode *ttvp = (v); \
  123. nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
  124. if (((f) = fxdr_unsigned(int, *tl)) != 0) { \
  125. if ((t1 = nfs_loadattrcache(&ttvp, &info.nmi_md, \
  126. &info.nmi_dpos, NULL)) != 0) { \
  127. error = t1; \
  128. (f) = 0; \
  129. m_freem(info.nmi_mrep); \
  130. goto nfsmout; \
  131. } \
  132. (v) = ttvp; \
  133. } \
  134. } }
  135. /* Used as (f) for nfsm_wcc_data() */
  136. #define NFSV3_WCCRATTR 0
  137. #define NFSV3_WCCCHK 1
  138. #define nfsm_wcc_data(v, f) do { if (info.nmi_mrep != NULL) { \
  139. struct timespec _mtime; \
  140. int ttattrf, ttretf = 0; \
  141. \
  142. nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
  143. if (*tl == nfs_true) { \
  144. nfsm_dissect(tl, u_int32_t *, 6 * NFSX_UNSIGNED); \
  145. fxdr_nfsv3time(tl + 2, &_mtime); \
  146. if (f) { \
  147. ttretf = timespeccmp(&VTONFS(v)->n_mtime, \
  148. &_mtime, !=); \
  149. } \
  150. } \
  151. nfsm_postop_attr((v), ttattrf); \
  152. if (f) { \
  153. (f) = ttretf; \
  154. } else { \
  155. (f) = ttattrf; \
  156. } \
  157. } } while (0)
  158. #define nfsm_strsiz(s,m) { \
  159. nfsm_dissect(tl,u_int32_t *,NFSX_UNSIGNED); \
  160. if (((s) = fxdr_unsigned(int32_t,*tl)) > (m)) { \
  161. m_freem(info.nmi_mrep); \
  162. error = EBADRPC; \
  163. goto nfsmout; \
  164. } \
  165. }
  166. #define nfsm_srvnamesiz(s) { \
  167. nfsm_dissect(tl,u_int32_t *,NFSX_UNSIGNED); \
  168. if (((s) = fxdr_unsigned(int32_t,*tl)) > NFS_MAXNAMLEN) \
  169. error = NFSERR_NAMETOL; \
  170. if ((s) <= 0) \
  171. error = EBADRPC; \
  172. if (error) \
  173. nfsm_reply(0); \
  174. }
  175. #define nfsm_mtouio(p,s) \
  176. if ((s) > 0 && \
  177. (t1 = nfsm_mbuftouio(&info.nmi_md,(p),(s), \
  178. &info.nmi_dpos)) != 0) { \
  179. error = t1; \
  180. m_freem(info.nmi_mrep); \
  181. goto nfsmout; \
  182. }
  183. #define nfsm_rndup(a) (((a)+3)&(~0x3))
  184. #define nfsm_strtom(a,s,m) \
  185. if ((s) > (m)) { \
  186. m_freem(info.nmi_mreq); \
  187. error = ENAMETOOLONG; \
  188. goto nfsmout; \
  189. } \
  190. nfsm_strtombuf(&info.nmi_mb, (a), (s))
  191. #define nfsm_reply(s) { \
  192. nfsd->nd_repstat = error; \
  193. if (error && !(nfsd->nd_flag & ND_NFSV3)) \
  194. (void) nfs_rephead(0, nfsd, slp, error, \
  195. &info.nmi_mreq, &info.nmi_mb); \
  196. else \
  197. (void) nfs_rephead((s), nfsd, slp, error, \
  198. &info.nmi_mreq, &info.nmi_mb); \
  199. if (info.nmi_mrep != NULL) { \
  200. m_freem(info.nmi_mrep); \
  201. info.nmi_mrep = NULL; \
  202. } \
  203. *mrq = info.nmi_mreq; \
  204. if (error && (!(nfsd->nd_flag & ND_NFSV3) || error == EBADRPC)) \
  205. return(0); \
  206. }
  207. #define nfsm_writereply(s, v3) { \
  208. nfsd->nd_repstat = error; \
  209. if (error && !(v3)) \
  210. (void) nfs_rephead(0, nfsd, slp, error, &info.nmi_mreq, \
  211. &info.nmi_mb); \
  212. else \
  213. (void) nfs_rephead((s), nfsd, slp, error, &info.nmi_mreq, \
  214. &info.nmi_mb); \
  215. }
  216. #define nfsm_adv(s) { \
  217. t1 = mtod(info.nmi_md, caddr_t) + info.nmi_md->m_len - \
  218. info.nmi_dpos; \
  219. if (t1 >= (s)) { \
  220. info.nmi_dpos += (s); \
  221. } else if ((t1 = nfs_adv(&info.nmi_md, &info.nmi_dpos, \
  222. (s), t1)) != 0) { \
  223. error = t1; \
  224. m_freem(info.nmi_mrep); \
  225. goto nfsmout; \
  226. } \
  227. }
  228. #define nfsm_srvmtofh(f) { \
  229. if (nfsd->nd_flag & ND_NFSV3) { \
  230. nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
  231. if (fxdr_unsigned(int, *tl) != NFSX_V3FH) { \
  232. error = EBADRPC; \
  233. nfsm_reply(0); \
  234. } \
  235. } \
  236. nfsm_dissect(tl, u_int32_t *, NFSX_V3FH); \
  237. bcopy(tl, (f), NFSX_V3FH); \
  238. if ((nfsd->nd_flag & ND_NFSV3) == 0) \
  239. nfsm_adv(NFSX_V2FH - NFSX_V3FH); \
  240. }
  241. #endif