stackglue.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /* -*- mode: c; c-basic-offset: 8; -*-
  2. * vim: noexpandtab sw=8 ts=8 sts=0:
  3. *
  4. * stackglue.c
  5. *
  6. * Code which implements an OCFS2 specific interface to underlying
  7. * cluster stacks.
  8. *
  9. * Copyright (C) 2007 Oracle. All rights reserved.
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public
  13. * License as published by the Free Software Foundation, version 2.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * General Public License for more details.
  19. */
  20. #include "cluster/masklog.h"
  21. #include "stackglue.h"
  22. static struct ocfs2_locking_protocol *lproto;
  23. /* These should be identical */
  24. #if (DLM_LOCK_IV != LKM_IVMODE)
  25. # error Lock modes do not match
  26. #endif
  27. #if (DLM_LOCK_NL != LKM_NLMODE)
  28. # error Lock modes do not match
  29. #endif
  30. #if (DLM_LOCK_CR != LKM_CRMODE)
  31. # error Lock modes do not match
  32. #endif
  33. #if (DLM_LOCK_CW != LKM_CWMODE)
  34. # error Lock modes do not match
  35. #endif
  36. #if (DLM_LOCK_PR != LKM_PRMODE)
  37. # error Lock modes do not match
  38. #endif
  39. #if (DLM_LOCK_PW != LKM_PWMODE)
  40. # error Lock modes do not match
  41. #endif
  42. #if (DLM_LOCK_EX != LKM_EXMODE)
  43. # error Lock modes do not match
  44. #endif
  45. static inline int mode_to_o2dlm(int mode)
  46. {
  47. BUG_ON(mode > LKM_MAXMODE);
  48. return mode;
  49. }
  50. #define map_flag(_generic, _o2dlm) \
  51. if (flags & (_generic)) { \
  52. flags &= ~(_generic); \
  53. o2dlm_flags |= (_o2dlm); \
  54. }
  55. static int flags_to_o2dlm(u32 flags)
  56. {
  57. int o2dlm_flags = 0;
  58. map_flag(DLM_LKF_NOQUEUE, LKM_NOQUEUE);
  59. map_flag(DLM_LKF_CANCEL, LKM_CANCEL);
  60. map_flag(DLM_LKF_CONVERT, LKM_CONVERT);
  61. map_flag(DLM_LKF_VALBLK, LKM_VALBLK);
  62. map_flag(DLM_LKF_IVVALBLK, LKM_INVVALBLK);
  63. map_flag(DLM_LKF_ORPHAN, LKM_ORPHAN);
  64. map_flag(DLM_LKF_FORCEUNLOCK, LKM_FORCE);
  65. map_flag(DLM_LKF_TIMEOUT, LKM_TIMEOUT);
  66. map_flag(DLM_LKF_LOCAL, LKM_LOCAL);
  67. /* map_flag() should have cleared every flag passed in */
  68. BUG_ON(flags != 0);
  69. return o2dlm_flags;
  70. }
  71. #undef map_flag
  72. /*
  73. * Map an o2dlm status to standard errno values.
  74. *
  75. * o2dlm only uses a handful of these, and returns even fewer to the
  76. * caller. Still, we try to assign sane values to each error.
  77. *
  78. * The following value pairs have special meanings to dlmglue, thus
  79. * the right hand side needs to stay unique - never duplicate the
  80. * mapping elsewhere in the table!
  81. *
  82. * DLM_NORMAL: 0
  83. * DLM_NOTQUEUED: -EAGAIN
  84. * DLM_CANCELGRANT: -DLM_ECANCEL
  85. * DLM_CANCEL: -DLM_EUNLOCK
  86. */
  87. /* Keep in sync with dlmapi.h */
  88. static int status_map[] = {
  89. [DLM_NORMAL] = 0, /* Success */
  90. [DLM_GRANTED] = -EINVAL,
  91. [DLM_DENIED] = -EACCES,
  92. [DLM_DENIED_NOLOCKS] = -EACCES,
  93. [DLM_WORKING] = -EBUSY,
  94. [DLM_BLOCKED] = -EINVAL,
  95. [DLM_BLOCKED_ORPHAN] = -EINVAL,
  96. [DLM_DENIED_GRACE_PERIOD] = -EACCES,
  97. [DLM_SYSERR] = -ENOMEM, /* It is what it is */
  98. [DLM_NOSUPPORT] = -EPROTO,
  99. [DLM_CANCELGRANT] = -DLM_ECANCEL, /* Cancel after grant */
  100. [DLM_IVLOCKID] = -EINVAL,
  101. [DLM_SYNC] = -EINVAL,
  102. [DLM_BADTYPE] = -EINVAL,
  103. [DLM_BADRESOURCE] = -EINVAL,
  104. [DLM_MAXHANDLES] = -ENOMEM,
  105. [DLM_NOCLINFO] = -EINVAL,
  106. [DLM_NOLOCKMGR] = -EINVAL,
  107. [DLM_NOPURGED] = -EINVAL,
  108. [DLM_BADARGS] = -EINVAL,
  109. [DLM_VOID] = -EINVAL,
  110. [DLM_NOTQUEUED] = -EAGAIN, /* Trylock failed */
  111. [DLM_IVBUFLEN] = -EINVAL,
  112. [DLM_CVTUNGRANT] = -EPERM,
  113. [DLM_BADPARAM] = -EINVAL,
  114. [DLM_VALNOTVALID] = -EINVAL,
  115. [DLM_REJECTED] = -EPERM,
  116. [DLM_ABORT] = -EINVAL,
  117. [DLM_CANCEL] = -DLM_EUNLOCK, /* Successful cancel */
  118. [DLM_IVRESHANDLE] = -EINVAL,
  119. [DLM_DEADLOCK] = -EDEADLK,
  120. [DLM_DENIED_NOASTS] = -EINVAL,
  121. [DLM_FORWARD] = -EINVAL,
  122. [DLM_TIMEOUT] = -ETIMEDOUT,
  123. [DLM_IVGROUPID] = -EINVAL,
  124. [DLM_VERS_CONFLICT] = -EOPNOTSUPP,
  125. [DLM_BAD_DEVICE_PATH] = -ENOENT,
  126. [DLM_NO_DEVICE_PERMISSION] = -EPERM,
  127. [DLM_NO_CONTROL_DEVICE] = -ENOENT,
  128. [DLM_RECOVERING] = -ENOTCONN,
  129. [DLM_MIGRATING] = -ERESTART,
  130. [DLM_MAXSTATS] = -EINVAL,
  131. };
  132. static int dlm_status_to_errno(enum dlm_status status)
  133. {
  134. BUG_ON(status > (sizeof(status_map) / sizeof(status_map[0])));
  135. return status_map[status];
  136. }
  137. static void o2dlm_lock_ast_wrapper(void *astarg)
  138. {
  139. BUG_ON(lproto == NULL);
  140. lproto->lp_lock_ast(astarg);
  141. }
  142. static void o2dlm_blocking_ast_wrapper(void *astarg, int level)
  143. {
  144. BUG_ON(lproto == NULL);
  145. lproto->lp_blocking_ast(astarg, level);
  146. }
  147. static void o2dlm_unlock_ast_wrapper(void *astarg, enum dlm_status status)
  148. {
  149. int error;
  150. BUG_ON(lproto == NULL);
  151. /*
  152. * XXX: CANCEL values are sketchy.
  153. *
  154. * Currently we have preserved the o2dlm paradigm. You can get
  155. * unlock_ast() whether the cancel succeded or not.
  156. *
  157. * First, we're going to pass DLM_EUNLOCK just like fs/dlm does for
  158. * successful unlocks. That is a clean behavior.
  159. *
  160. * In o2dlm, you can get both the lock_ast() for the lock being
  161. * granted and the unlock_ast() for the CANCEL failing. A
  162. * successful cancel sends DLM_NORMAL here. If the
  163. * lock grant happened before the cancel arrived, you get
  164. * DLM_CANCELGRANT. For now, we'll use DLM_ECANCEL to signify
  165. * CANCELGRANT - the CANCEL was supposed to happen but didn't. We
  166. * can then use DLM_EUNLOCK to signify a successful CANCEL -
  167. * effectively, the CANCEL caused the lock to roll back.
  168. *
  169. * In the future, we will likely move the o2dlm to send only one
  170. * ast - either unlock_ast() for a successful CANCEL or lock_ast()
  171. * when the grant succeeds. At that point, we'll send DLM_ECANCEL
  172. * for all cancel results (CANCELGRANT will no longer exist).
  173. */
  174. error = dlm_status_to_errno(status);
  175. /* Successful unlock is DLM_EUNLOCK */
  176. if (!error)
  177. error = -DLM_EUNLOCK;
  178. lproto->lp_unlock_ast(astarg, error);
  179. }
  180. int ocfs2_dlm_lock(struct dlm_ctxt *dlm,
  181. int mode,
  182. union ocfs2_dlm_lksb *lksb,
  183. u32 flags,
  184. void *name,
  185. unsigned int namelen,
  186. void *astarg)
  187. {
  188. enum dlm_status status;
  189. int o2dlm_mode = mode_to_o2dlm(mode);
  190. int o2dlm_flags = flags_to_o2dlm(flags);
  191. int ret;
  192. BUG_ON(lproto == NULL);
  193. status = dlmlock(dlm, o2dlm_mode, &lksb->lksb_o2dlm, o2dlm_flags,
  194. name, namelen,
  195. o2dlm_lock_ast_wrapper, astarg,
  196. o2dlm_blocking_ast_wrapper);
  197. ret = dlm_status_to_errno(status);
  198. return ret;
  199. }
  200. int ocfs2_dlm_unlock(struct dlm_ctxt *dlm,
  201. union ocfs2_dlm_lksb *lksb,
  202. u32 flags,
  203. void *astarg)
  204. {
  205. enum dlm_status status;
  206. int o2dlm_flags = flags_to_o2dlm(flags);
  207. int ret;
  208. BUG_ON(lproto == NULL);
  209. status = dlmunlock(dlm, &lksb->lksb_o2dlm, o2dlm_flags,
  210. o2dlm_unlock_ast_wrapper, astarg);
  211. ret = dlm_status_to_errno(status);
  212. return ret;
  213. }
  214. int ocfs2_dlm_lock_status(union ocfs2_dlm_lksb *lksb)
  215. {
  216. return dlm_status_to_errno(lksb->lksb_o2dlm.status);
  217. }
  218. /*
  219. * Why don't we cast to ocfs2_meta_lvb? The "clean" answer is that we
  220. * don't cast at the glue level. The real answer is that the header
  221. * ordering is nigh impossible.
  222. */
  223. void *ocfs2_dlm_lvb(union ocfs2_dlm_lksb *lksb)
  224. {
  225. return (void *)(lksb->lksb_o2dlm.lvb);
  226. }
  227. void o2cb_get_stack(struct ocfs2_locking_protocol *proto)
  228. {
  229. BUG_ON(proto == NULL);
  230. lproto = proto;
  231. }
  232. void o2cb_put_stack(void)
  233. {
  234. lproto = NULL;
  235. }