util.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /******************************************************************************
  2. *******************************************************************************
  3. **
  4. ** Copyright (C) 2005 Red Hat, Inc. All rights reserved.
  5. **
  6. ** This copyrighted material is made available to anyone wishing to use,
  7. ** modify, copy, or redistribute it subject to the terms and conditions
  8. ** of the GNU General Public License v.2.
  9. **
  10. *******************************************************************************
  11. ******************************************************************************/
  12. #include "dlm_internal.h"
  13. #include "rcom.h"
  14. #include "util.h"
  15. #define DLM_ERRNO_EDEADLK 35
  16. #define DLM_ERRNO_EBADR 53
  17. #define DLM_ERRNO_EBADSLT 57
  18. #define DLM_ERRNO_EPROTO 71
  19. #define DLM_ERRNO_EOPNOTSUPP 95
  20. #define DLM_ERRNO_ETIMEDOUT 110
  21. #define DLM_ERRNO_EINPROGRESS 115
  22. static void header_out(struct dlm_header *hd)
  23. {
  24. hd->h_version = cpu_to_le32(hd->h_version);
  25. hd->h_lockspace = cpu_to_le32(hd->h_lockspace);
  26. hd->h_nodeid = cpu_to_le32(hd->h_nodeid);
  27. hd->h_length = cpu_to_le16(hd->h_length);
  28. }
  29. static void header_in(struct dlm_header *hd)
  30. {
  31. hd->h_version = le32_to_cpu(hd->h_version);
  32. hd->h_lockspace = le32_to_cpu(hd->h_lockspace);
  33. hd->h_nodeid = le32_to_cpu(hd->h_nodeid);
  34. hd->h_length = le16_to_cpu(hd->h_length);
  35. }
  36. /* higher errno values are inconsistent across architectures, so select
  37. one set of values for on the wire */
  38. static int to_dlm_errno(int err)
  39. {
  40. switch (err) {
  41. case -EDEADLK:
  42. return -DLM_ERRNO_EDEADLK;
  43. case -EBADR:
  44. return -DLM_ERRNO_EBADR;
  45. case -EBADSLT:
  46. return -DLM_ERRNO_EBADSLT;
  47. case -EPROTO:
  48. return -DLM_ERRNO_EPROTO;
  49. case -EOPNOTSUPP:
  50. return -DLM_ERRNO_EOPNOTSUPP;
  51. case -ETIMEDOUT:
  52. return -DLM_ERRNO_ETIMEDOUT;
  53. case -EINPROGRESS:
  54. return -DLM_ERRNO_EINPROGRESS;
  55. }
  56. return err;
  57. }
  58. static int from_dlm_errno(int err)
  59. {
  60. switch (err) {
  61. case -DLM_ERRNO_EDEADLK:
  62. return -EDEADLK;
  63. case -DLM_ERRNO_EBADR:
  64. return -EBADR;
  65. case -DLM_ERRNO_EBADSLT:
  66. return -EBADSLT;
  67. case -DLM_ERRNO_EPROTO:
  68. return -EPROTO;
  69. case -DLM_ERRNO_EOPNOTSUPP:
  70. return -EOPNOTSUPP;
  71. case -DLM_ERRNO_ETIMEDOUT:
  72. return -ETIMEDOUT;
  73. case -DLM_ERRNO_EINPROGRESS:
  74. return -EINPROGRESS;
  75. }
  76. return err;
  77. }
  78. void dlm_message_out(struct dlm_message *ms)
  79. {
  80. struct dlm_header *hd = (struct dlm_header *) ms;
  81. header_out(hd);
  82. ms->m_type = cpu_to_le32(ms->m_type);
  83. ms->m_nodeid = cpu_to_le32(ms->m_nodeid);
  84. ms->m_pid = cpu_to_le32(ms->m_pid);
  85. ms->m_lkid = cpu_to_le32(ms->m_lkid);
  86. ms->m_remid = cpu_to_le32(ms->m_remid);
  87. ms->m_parent_lkid = cpu_to_le32(ms->m_parent_lkid);
  88. ms->m_parent_remid = cpu_to_le32(ms->m_parent_remid);
  89. ms->m_exflags = cpu_to_le32(ms->m_exflags);
  90. ms->m_sbflags = cpu_to_le32(ms->m_sbflags);
  91. ms->m_flags = cpu_to_le32(ms->m_flags);
  92. ms->m_lvbseq = cpu_to_le32(ms->m_lvbseq);
  93. ms->m_hash = cpu_to_le32(ms->m_hash);
  94. ms->m_status = cpu_to_le32(ms->m_status);
  95. ms->m_grmode = cpu_to_le32(ms->m_grmode);
  96. ms->m_rqmode = cpu_to_le32(ms->m_rqmode);
  97. ms->m_bastmode = cpu_to_le32(ms->m_bastmode);
  98. ms->m_asts = cpu_to_le32(ms->m_asts);
  99. ms->m_result = cpu_to_le32(to_dlm_errno(ms->m_result));
  100. }
  101. void dlm_message_in(struct dlm_message *ms)
  102. {
  103. struct dlm_header *hd = (struct dlm_header *) ms;
  104. header_in(hd);
  105. ms->m_type = le32_to_cpu(ms->m_type);
  106. ms->m_nodeid = le32_to_cpu(ms->m_nodeid);
  107. ms->m_pid = le32_to_cpu(ms->m_pid);
  108. ms->m_lkid = le32_to_cpu(ms->m_lkid);
  109. ms->m_remid = le32_to_cpu(ms->m_remid);
  110. ms->m_parent_lkid = le32_to_cpu(ms->m_parent_lkid);
  111. ms->m_parent_remid = le32_to_cpu(ms->m_parent_remid);
  112. ms->m_exflags = le32_to_cpu(ms->m_exflags);
  113. ms->m_sbflags = le32_to_cpu(ms->m_sbflags);
  114. ms->m_flags = le32_to_cpu(ms->m_flags);
  115. ms->m_lvbseq = le32_to_cpu(ms->m_lvbseq);
  116. ms->m_hash = le32_to_cpu(ms->m_hash);
  117. ms->m_status = le32_to_cpu(ms->m_status);
  118. ms->m_grmode = le32_to_cpu(ms->m_grmode);
  119. ms->m_rqmode = le32_to_cpu(ms->m_rqmode);
  120. ms->m_bastmode = le32_to_cpu(ms->m_bastmode);
  121. ms->m_asts = le32_to_cpu(ms->m_asts);
  122. ms->m_result = from_dlm_errno(le32_to_cpu(ms->m_result));
  123. }
  124. static void rcom_lock_out(struct rcom_lock *rl)
  125. {
  126. rl->rl_ownpid = cpu_to_le32(rl->rl_ownpid);
  127. rl->rl_lkid = cpu_to_le32(rl->rl_lkid);
  128. rl->rl_remid = cpu_to_le32(rl->rl_remid);
  129. rl->rl_parent_lkid = cpu_to_le32(rl->rl_parent_lkid);
  130. rl->rl_parent_remid = cpu_to_le32(rl->rl_parent_remid);
  131. rl->rl_exflags = cpu_to_le32(rl->rl_exflags);
  132. rl->rl_flags = cpu_to_le32(rl->rl_flags);
  133. rl->rl_lvbseq = cpu_to_le32(rl->rl_lvbseq);
  134. rl->rl_result = cpu_to_le32(rl->rl_result);
  135. rl->rl_wait_type = cpu_to_le16(rl->rl_wait_type);
  136. rl->rl_namelen = cpu_to_le16(rl->rl_namelen);
  137. }
  138. static void rcom_lock_in(struct rcom_lock *rl)
  139. {
  140. rl->rl_ownpid = le32_to_cpu(rl->rl_ownpid);
  141. rl->rl_lkid = le32_to_cpu(rl->rl_lkid);
  142. rl->rl_remid = le32_to_cpu(rl->rl_remid);
  143. rl->rl_parent_lkid = le32_to_cpu(rl->rl_parent_lkid);
  144. rl->rl_parent_remid = le32_to_cpu(rl->rl_parent_remid);
  145. rl->rl_exflags = le32_to_cpu(rl->rl_exflags);
  146. rl->rl_flags = le32_to_cpu(rl->rl_flags);
  147. rl->rl_lvbseq = le32_to_cpu(rl->rl_lvbseq);
  148. rl->rl_result = le32_to_cpu(rl->rl_result);
  149. rl->rl_wait_type = le16_to_cpu(rl->rl_wait_type);
  150. rl->rl_namelen = le16_to_cpu(rl->rl_namelen);
  151. }
  152. static void rcom_config_out(struct rcom_config *rf)
  153. {
  154. rf->rf_lvblen = cpu_to_le32(rf->rf_lvblen);
  155. rf->rf_lsflags = cpu_to_le32(rf->rf_lsflags);
  156. }
  157. static void rcom_config_in(struct rcom_config *rf)
  158. {
  159. rf->rf_lvblen = le32_to_cpu(rf->rf_lvblen);
  160. rf->rf_lsflags = le32_to_cpu(rf->rf_lsflags);
  161. }
  162. void dlm_rcom_out(struct dlm_rcom *rc)
  163. {
  164. struct dlm_header *hd = (struct dlm_header *) rc;
  165. int type = rc->rc_type;
  166. header_out(hd);
  167. rc->rc_type = cpu_to_le32(rc->rc_type);
  168. rc->rc_result = cpu_to_le32(rc->rc_result);
  169. rc->rc_id = cpu_to_le64(rc->rc_id);
  170. rc->rc_seq = cpu_to_le64(rc->rc_seq);
  171. rc->rc_seq_reply = cpu_to_le64(rc->rc_seq_reply);
  172. if ((type == DLM_RCOM_LOCK) || (type == DLM_RCOM_LOCK_REPLY))
  173. rcom_lock_out((struct rcom_lock *) rc->rc_buf);
  174. else if (type == DLM_RCOM_STATUS_REPLY)
  175. rcom_config_out((struct rcom_config *) rc->rc_buf);
  176. }
  177. void dlm_rcom_in(struct dlm_rcom *rc)
  178. {
  179. struct dlm_header *hd = (struct dlm_header *) rc;
  180. int type;
  181. header_in(hd);
  182. rc->rc_type = le32_to_cpu(rc->rc_type);
  183. rc->rc_result = le32_to_cpu(rc->rc_result);
  184. rc->rc_id = le64_to_cpu(rc->rc_id);
  185. rc->rc_seq = le64_to_cpu(rc->rc_seq);
  186. rc->rc_seq_reply = le64_to_cpu(rc->rc_seq_reply);
  187. type = rc->rc_type;
  188. if ((type == DLM_RCOM_LOCK) || (type == DLM_RCOM_LOCK_REPLY))
  189. rcom_lock_in((struct rcom_lock *) rc->rc_buf);
  190. else if (type == DLM_RCOM_STATUS_REPLY)
  191. rcom_config_in((struct rcom_config *) rc->rc_buf);
  192. }