stackglue.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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 <linux/slab.h>
  21. #include <linux/crc32.h>
  22. /* Needed for AOP_TRUNCATED_PAGE in mlog_errno() */
  23. #include <linux/fs.h>
  24. #include "cluster/masklog.h"
  25. #include "cluster/nodemanager.h"
  26. #include "stackglue.h"
  27. static struct ocfs2_locking_protocol *lproto;
  28. struct o2dlm_private {
  29. struct dlm_eviction_cb op_eviction_cb;
  30. };
  31. /* These should be identical */
  32. #if (DLM_LOCK_IV != LKM_IVMODE)
  33. # error Lock modes do not match
  34. #endif
  35. #if (DLM_LOCK_NL != LKM_NLMODE)
  36. # error Lock modes do not match
  37. #endif
  38. #if (DLM_LOCK_CR != LKM_CRMODE)
  39. # error Lock modes do not match
  40. #endif
  41. #if (DLM_LOCK_CW != LKM_CWMODE)
  42. # error Lock modes do not match
  43. #endif
  44. #if (DLM_LOCK_PR != LKM_PRMODE)
  45. # error Lock modes do not match
  46. #endif
  47. #if (DLM_LOCK_PW != LKM_PWMODE)
  48. # error Lock modes do not match
  49. #endif
  50. #if (DLM_LOCK_EX != LKM_EXMODE)
  51. # error Lock modes do not match
  52. #endif
  53. static inline int mode_to_o2dlm(int mode)
  54. {
  55. BUG_ON(mode > LKM_MAXMODE);
  56. return mode;
  57. }
  58. #define map_flag(_generic, _o2dlm) \
  59. if (flags & (_generic)) { \
  60. flags &= ~(_generic); \
  61. o2dlm_flags |= (_o2dlm); \
  62. }
  63. static int flags_to_o2dlm(u32 flags)
  64. {
  65. int o2dlm_flags = 0;
  66. map_flag(DLM_LKF_NOQUEUE, LKM_NOQUEUE);
  67. map_flag(DLM_LKF_CANCEL, LKM_CANCEL);
  68. map_flag(DLM_LKF_CONVERT, LKM_CONVERT);
  69. map_flag(DLM_LKF_VALBLK, LKM_VALBLK);
  70. map_flag(DLM_LKF_IVVALBLK, LKM_INVVALBLK);
  71. map_flag(DLM_LKF_ORPHAN, LKM_ORPHAN);
  72. map_flag(DLM_LKF_FORCEUNLOCK, LKM_FORCE);
  73. map_flag(DLM_LKF_TIMEOUT, LKM_TIMEOUT);
  74. map_flag(DLM_LKF_LOCAL, LKM_LOCAL);
  75. /* map_flag() should have cleared every flag passed in */
  76. BUG_ON(flags != 0);
  77. return o2dlm_flags;
  78. }
  79. #undef map_flag
  80. /*
  81. * Map an o2dlm status to standard errno values.
  82. *
  83. * o2dlm only uses a handful of these, and returns even fewer to the
  84. * caller. Still, we try to assign sane values to each error.
  85. *
  86. * The following value pairs have special meanings to dlmglue, thus
  87. * the right hand side needs to stay unique - never duplicate the
  88. * mapping elsewhere in the table!
  89. *
  90. * DLM_NORMAL: 0
  91. * DLM_NOTQUEUED: -EAGAIN
  92. * DLM_CANCELGRANT: -DLM_ECANCEL
  93. * DLM_CANCEL: -DLM_EUNLOCK
  94. */
  95. /* Keep in sync with dlmapi.h */
  96. static int status_map[] = {
  97. [DLM_NORMAL] = 0, /* Success */
  98. [DLM_GRANTED] = -EINVAL,
  99. [DLM_DENIED] = -EACCES,
  100. [DLM_DENIED_NOLOCKS] = -EACCES,
  101. [DLM_WORKING] = -EBUSY,
  102. [DLM_BLOCKED] = -EINVAL,
  103. [DLM_BLOCKED_ORPHAN] = -EINVAL,
  104. [DLM_DENIED_GRACE_PERIOD] = -EACCES,
  105. [DLM_SYSERR] = -ENOMEM, /* It is what it is */
  106. [DLM_NOSUPPORT] = -EPROTO,
  107. [DLM_CANCELGRANT] = -DLM_ECANCEL, /* Cancel after grant */
  108. [DLM_IVLOCKID] = -EINVAL,
  109. [DLM_SYNC] = -EINVAL,
  110. [DLM_BADTYPE] = -EINVAL,
  111. [DLM_BADRESOURCE] = -EINVAL,
  112. [DLM_MAXHANDLES] = -ENOMEM,
  113. [DLM_NOCLINFO] = -EINVAL,
  114. [DLM_NOLOCKMGR] = -EINVAL,
  115. [DLM_NOPURGED] = -EINVAL,
  116. [DLM_BADARGS] = -EINVAL,
  117. [DLM_VOID] = -EINVAL,
  118. [DLM_NOTQUEUED] = -EAGAIN, /* Trylock failed */
  119. [DLM_IVBUFLEN] = -EINVAL,
  120. [DLM_CVTUNGRANT] = -EPERM,
  121. [DLM_BADPARAM] = -EINVAL,
  122. [DLM_VALNOTVALID] = -EINVAL,
  123. [DLM_REJECTED] = -EPERM,
  124. [DLM_ABORT] = -EINVAL,
  125. [DLM_CANCEL] = -DLM_EUNLOCK, /* Successful cancel */
  126. [DLM_IVRESHANDLE] = -EINVAL,
  127. [DLM_DEADLOCK] = -EDEADLK,
  128. [DLM_DENIED_NOASTS] = -EINVAL,
  129. [DLM_FORWARD] = -EINVAL,
  130. [DLM_TIMEOUT] = -ETIMEDOUT,
  131. [DLM_IVGROUPID] = -EINVAL,
  132. [DLM_VERS_CONFLICT] = -EOPNOTSUPP,
  133. [DLM_BAD_DEVICE_PATH] = -ENOENT,
  134. [DLM_NO_DEVICE_PERMISSION] = -EPERM,
  135. [DLM_NO_CONTROL_DEVICE] = -ENOENT,
  136. [DLM_RECOVERING] = -ENOTCONN,
  137. [DLM_MIGRATING] = -ERESTART,
  138. [DLM_MAXSTATS] = -EINVAL,
  139. };
  140. static int dlm_status_to_errno(enum dlm_status status)
  141. {
  142. BUG_ON(status > (sizeof(status_map) / sizeof(status_map[0])));
  143. return status_map[status];
  144. }
  145. static void o2dlm_lock_ast_wrapper(void *astarg)
  146. {
  147. BUG_ON(lproto == NULL);
  148. lproto->lp_lock_ast(astarg);
  149. }
  150. static void o2dlm_blocking_ast_wrapper(void *astarg, int level)
  151. {
  152. BUG_ON(lproto == NULL);
  153. lproto->lp_blocking_ast(astarg, level);
  154. }
  155. static void o2dlm_unlock_ast_wrapper(void *astarg, enum dlm_status status)
  156. {
  157. int error;
  158. BUG_ON(lproto == NULL);
  159. /*
  160. * XXX: CANCEL values are sketchy.
  161. *
  162. * Currently we have preserved the o2dlm paradigm. You can get
  163. * unlock_ast() whether the cancel succeded or not.
  164. *
  165. * First, we're going to pass DLM_EUNLOCK just like fs/dlm does for
  166. * successful unlocks. That is a clean behavior.
  167. *
  168. * In o2dlm, you can get both the lock_ast() for the lock being
  169. * granted and the unlock_ast() for the CANCEL failing. A
  170. * successful cancel sends DLM_NORMAL here. If the
  171. * lock grant happened before the cancel arrived, you get
  172. * DLM_CANCELGRANT. For now, we'll use DLM_ECANCEL to signify
  173. * CANCELGRANT - the CANCEL was supposed to happen but didn't. We
  174. * can then use DLM_EUNLOCK to signify a successful CANCEL -
  175. * effectively, the CANCEL caused the lock to roll back.
  176. *
  177. * In the future, we will likely move the o2dlm to send only one
  178. * ast - either unlock_ast() for a successful CANCEL or lock_ast()
  179. * when the grant succeeds. At that point, we'll send DLM_ECANCEL
  180. * for all cancel results (CANCELGRANT will no longer exist).
  181. */
  182. error = dlm_status_to_errno(status);
  183. /* Successful unlock is DLM_EUNLOCK */
  184. if (!error)
  185. error = -DLM_EUNLOCK;
  186. lproto->lp_unlock_ast(astarg, error);
  187. }
  188. int ocfs2_dlm_lock(struct ocfs2_cluster_connection *conn,
  189. int mode,
  190. union ocfs2_dlm_lksb *lksb,
  191. u32 flags,
  192. void *name,
  193. unsigned int namelen,
  194. void *astarg)
  195. {
  196. enum dlm_status status;
  197. int o2dlm_mode = mode_to_o2dlm(mode);
  198. int o2dlm_flags = flags_to_o2dlm(flags);
  199. int ret;
  200. BUG_ON(lproto == NULL);
  201. status = dlmlock(conn->cc_lockspace, o2dlm_mode, &lksb->lksb_o2dlm,
  202. o2dlm_flags, name, namelen,
  203. o2dlm_lock_ast_wrapper, astarg,
  204. o2dlm_blocking_ast_wrapper);
  205. ret = dlm_status_to_errno(status);
  206. return ret;
  207. }
  208. int ocfs2_dlm_unlock(struct ocfs2_cluster_connection *conn,
  209. union ocfs2_dlm_lksb *lksb,
  210. u32 flags,
  211. void *astarg)
  212. {
  213. enum dlm_status status;
  214. int o2dlm_flags = flags_to_o2dlm(flags);
  215. int ret;
  216. BUG_ON(lproto == NULL);
  217. status = dlmunlock(conn->cc_lockspace, &lksb->lksb_o2dlm,
  218. o2dlm_flags, o2dlm_unlock_ast_wrapper, astarg);
  219. ret = dlm_status_to_errno(status);
  220. return ret;
  221. }
  222. int ocfs2_dlm_lock_status(union ocfs2_dlm_lksb *lksb)
  223. {
  224. return dlm_status_to_errno(lksb->lksb_o2dlm.status);
  225. }
  226. /*
  227. * Why don't we cast to ocfs2_meta_lvb? The "clean" answer is that we
  228. * don't cast at the glue level. The real answer is that the header
  229. * ordering is nigh impossible.
  230. */
  231. void *ocfs2_dlm_lvb(union ocfs2_dlm_lksb *lksb)
  232. {
  233. return (void *)(lksb->lksb_o2dlm.lvb);
  234. }
  235. /*
  236. * Called from the dlm when it's about to evict a node. This is how the
  237. * classic stack signals node death.
  238. */
  239. static void o2dlm_eviction_cb(int node_num, void *data)
  240. {
  241. struct ocfs2_cluster_connection *conn = data;
  242. mlog(ML_NOTICE, "o2dlm has evicted node %d from group %.*s\n",
  243. node_num, conn->cc_namelen, conn->cc_name);
  244. conn->cc_recovery_handler(node_num, conn->cc_recovery_data);
  245. }
  246. int ocfs2_cluster_connect(const char *group,
  247. int grouplen,
  248. void (*recovery_handler)(int node_num,
  249. void *recovery_data),
  250. void *recovery_data,
  251. struct ocfs2_cluster_connection **conn)
  252. {
  253. int rc = 0;
  254. struct ocfs2_cluster_connection *new_conn;
  255. u32 dlm_key;
  256. struct dlm_ctxt *dlm;
  257. struct o2dlm_private *priv;
  258. struct dlm_protocol_version dlm_version;
  259. BUG_ON(group == NULL);
  260. BUG_ON(conn == NULL);
  261. BUG_ON(recovery_handler == NULL);
  262. if (grouplen > GROUP_NAME_MAX) {
  263. rc = -EINVAL;
  264. goto out;
  265. }
  266. new_conn = kzalloc(sizeof(struct ocfs2_cluster_connection),
  267. GFP_KERNEL);
  268. if (!new_conn) {
  269. rc = -ENOMEM;
  270. goto out;
  271. }
  272. memcpy(new_conn->cc_name, group, grouplen);
  273. new_conn->cc_namelen = grouplen;
  274. new_conn->cc_recovery_handler = recovery_handler;
  275. new_conn->cc_recovery_data = recovery_data;
  276. /* Start the new connection at our maximum compatibility level */
  277. new_conn->cc_version = lproto->lp_max_version;
  278. priv = kzalloc(sizeof(struct o2dlm_private), GFP_KERNEL);
  279. if (!priv) {
  280. rc = -ENOMEM;
  281. goto out_free;
  282. }
  283. /* This just fills the structure in. It is safe to use new_conn. */
  284. dlm_setup_eviction_cb(&priv->op_eviction_cb, o2dlm_eviction_cb,
  285. new_conn);
  286. new_conn->cc_private = priv;
  287. /* used by the dlm code to make message headers unique, each
  288. * node in this domain must agree on this. */
  289. dlm_key = crc32_le(0, group, grouplen);
  290. dlm_version.pv_major = new_conn->cc_version.pv_major;
  291. dlm_version.pv_minor = new_conn->cc_version.pv_minor;
  292. dlm = dlm_register_domain(group, dlm_key, &dlm_version);
  293. if (IS_ERR(dlm)) {
  294. rc = PTR_ERR(dlm);
  295. mlog_errno(rc);
  296. goto out_free;
  297. }
  298. new_conn->cc_version.pv_major = dlm_version.pv_major;
  299. new_conn->cc_version.pv_minor = dlm_version.pv_minor;
  300. new_conn->cc_lockspace = dlm;
  301. dlm_register_eviction_cb(dlm, &priv->op_eviction_cb);
  302. *conn = new_conn;
  303. out_free:
  304. if (rc) {
  305. kfree(new_conn->cc_private);
  306. kfree(new_conn);
  307. }
  308. out:
  309. return rc;
  310. }
  311. int ocfs2_cluster_disconnect(struct ocfs2_cluster_connection *conn)
  312. {
  313. struct dlm_ctxt *dlm = conn->cc_lockspace;
  314. struct o2dlm_private *priv = conn->cc_private;
  315. dlm_unregister_eviction_cb(&priv->op_eviction_cb);
  316. dlm_unregister_domain(dlm);
  317. kfree(priv);
  318. kfree(conn);
  319. return 0;
  320. }
  321. int ocfs2_cluster_this_node(unsigned int *node)
  322. {
  323. int node_num;
  324. node_num = o2nm_this_node();
  325. if (node_num == O2NM_INVALID_NODE_NUM)
  326. return -ENOENT;
  327. if (node_num >= O2NM_MAX_NODES)
  328. return -EOVERFLOW;
  329. *node = node_num;
  330. return 0;
  331. }
  332. void o2cb_get_stack(struct ocfs2_locking_protocol *proto)
  333. {
  334. BUG_ON(proto == NULL);
  335. lproto = proto;
  336. }
  337. void o2cb_put_stack(void)
  338. {
  339. lproto = NULL;
  340. }