stack_o2cb.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. /* -*- mode: c; c-basic-offset: 8; -*-
  2. * vim: noexpandtab sw=8 ts=8 sts=0:
  3. *
  4. * stack_o2cb.c
  5. *
  6. * Code which interfaces ocfs2 with the o2cb stack.
  7. *
  8. * Copyright (C) 2007 Oracle. All rights reserved.
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public
  12. * License as published by the Free Software Foundation, version 2.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * General Public License for more details.
  18. */
  19. #include <linux/crc32.h>
  20. #include <linux/module.h>
  21. /* Needed for AOP_TRUNCATED_PAGE in mlog_errno() */
  22. #include <linux/fs.h>
  23. #include "cluster/masklog.h"
  24. #include "cluster/nodemanager.h"
  25. #include "cluster/heartbeat.h"
  26. #include "stackglue.h"
  27. struct o2dlm_private {
  28. struct dlm_eviction_cb op_eviction_cb;
  29. };
  30. static struct ocfs2_stack_plugin o2cb_stack;
  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: -EBUSY
  93. * DLM_CANCEL: -DLM_ECANCEL
  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] = -EACCES,
  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] = -EBUSY, /* 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_ECANCEL, /* 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(o2cb_stack.sp_proto == NULL);
  148. o2cb_stack.sp_proto->lp_lock_ast(astarg);
  149. }
  150. static void o2dlm_blocking_ast_wrapper(void *astarg, int level)
  151. {
  152. BUG_ON(o2cb_stack.sp_proto == NULL);
  153. o2cb_stack.sp_proto->lp_blocking_ast(astarg, level);
  154. }
  155. static void o2dlm_unlock_ast_wrapper(void *astarg, enum dlm_status status)
  156. {
  157. int error = dlm_status_to_errno(status);
  158. BUG_ON(o2cb_stack.sp_proto == NULL);
  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.
  165. *
  166. * There's no need for the double-ast. If we see DLM_CANCELGRANT,
  167. * we just ignore it. We expect the lock_ast() to handle the
  168. * granted lock.
  169. */
  170. if (status == DLM_CANCELGRANT)
  171. return;
  172. o2cb_stack.sp_proto->lp_unlock_ast(astarg, error);
  173. }
  174. static int o2cb_dlm_lock(struct ocfs2_cluster_connection *conn,
  175. int mode,
  176. union ocfs2_dlm_lksb *lksb,
  177. u32 flags,
  178. void *name,
  179. unsigned int namelen,
  180. void *astarg)
  181. {
  182. enum dlm_status status;
  183. int o2dlm_mode = mode_to_o2dlm(mode);
  184. int o2dlm_flags = flags_to_o2dlm(flags);
  185. int ret;
  186. status = dlmlock(conn->cc_lockspace, o2dlm_mode, &lksb->lksb_o2dlm,
  187. o2dlm_flags, name, namelen,
  188. o2dlm_lock_ast_wrapper, astarg,
  189. o2dlm_blocking_ast_wrapper);
  190. ret = dlm_status_to_errno(status);
  191. return ret;
  192. }
  193. static int o2cb_dlm_unlock(struct ocfs2_cluster_connection *conn,
  194. union ocfs2_dlm_lksb *lksb,
  195. u32 flags,
  196. void *astarg)
  197. {
  198. enum dlm_status status;
  199. int o2dlm_flags = flags_to_o2dlm(flags);
  200. int ret;
  201. status = dlmunlock(conn->cc_lockspace, &lksb->lksb_o2dlm,
  202. o2dlm_flags, o2dlm_unlock_ast_wrapper, astarg);
  203. ret = dlm_status_to_errno(status);
  204. return ret;
  205. }
  206. static int o2cb_dlm_lock_status(union ocfs2_dlm_lksb *lksb)
  207. {
  208. return dlm_status_to_errno(lksb->lksb_o2dlm.status);
  209. }
  210. /*
  211. * o2dlm aways has a "valid" LVB. If the dlm loses track of the LVB
  212. * contents, it will zero out the LVB. Thus the caller can always trust
  213. * the contents.
  214. */
  215. static int o2cb_dlm_lvb_valid(union ocfs2_dlm_lksb *lksb)
  216. {
  217. return 1;
  218. }
  219. static void *o2cb_dlm_lvb(union ocfs2_dlm_lksb *lksb)
  220. {
  221. return (void *)(lksb->lksb_o2dlm.lvb);
  222. }
  223. static void o2cb_dump_lksb(union ocfs2_dlm_lksb *lksb)
  224. {
  225. dlm_print_one_lock(lksb->lksb_o2dlm.lockid);
  226. }
  227. /*
  228. * Called from the dlm when it's about to evict a node. This is how the
  229. * classic stack signals node death.
  230. */
  231. static void o2dlm_eviction_cb(int node_num, void *data)
  232. {
  233. struct ocfs2_cluster_connection *conn = data;
  234. mlog(ML_NOTICE, "o2dlm has evicted node %d from group %.*s\n",
  235. node_num, conn->cc_namelen, conn->cc_name);
  236. conn->cc_recovery_handler(node_num, conn->cc_recovery_data);
  237. }
  238. static int o2cb_cluster_connect(struct ocfs2_cluster_connection *conn)
  239. {
  240. int rc = 0;
  241. u32 dlm_key;
  242. struct dlm_ctxt *dlm;
  243. struct o2dlm_private *priv;
  244. struct dlm_protocol_version dlm_version;
  245. BUG_ON(conn == NULL);
  246. BUG_ON(o2cb_stack.sp_proto == NULL);
  247. /* for now we only have one cluster/node, make sure we see it
  248. * in the heartbeat universe */
  249. if (!o2hb_check_local_node_heartbeating()) {
  250. rc = -EINVAL;
  251. goto out;
  252. }
  253. priv = kzalloc(sizeof(struct o2dlm_private), GFP_KERNEL);
  254. if (!priv) {
  255. rc = -ENOMEM;
  256. goto out_free;
  257. }
  258. /* This just fills the structure in. It is safe to pass conn. */
  259. dlm_setup_eviction_cb(&priv->op_eviction_cb, o2dlm_eviction_cb,
  260. conn);
  261. conn->cc_private = priv;
  262. /* used by the dlm code to make message headers unique, each
  263. * node in this domain must agree on this. */
  264. dlm_key = crc32_le(0, conn->cc_name, conn->cc_namelen);
  265. dlm_version.pv_major = conn->cc_version.pv_major;
  266. dlm_version.pv_minor = conn->cc_version.pv_minor;
  267. dlm = dlm_register_domain(conn->cc_name, dlm_key, &dlm_version);
  268. if (IS_ERR(dlm)) {
  269. rc = PTR_ERR(dlm);
  270. mlog_errno(rc);
  271. goto out_free;
  272. }
  273. conn->cc_version.pv_major = dlm_version.pv_major;
  274. conn->cc_version.pv_minor = dlm_version.pv_minor;
  275. conn->cc_lockspace = dlm;
  276. dlm_register_eviction_cb(dlm, &priv->op_eviction_cb);
  277. out_free:
  278. if (rc && conn->cc_private)
  279. kfree(conn->cc_private);
  280. out:
  281. return rc;
  282. }
  283. static int o2cb_cluster_disconnect(struct ocfs2_cluster_connection *conn)
  284. {
  285. struct dlm_ctxt *dlm = conn->cc_lockspace;
  286. struct o2dlm_private *priv = conn->cc_private;
  287. dlm_unregister_eviction_cb(&priv->op_eviction_cb);
  288. conn->cc_private = NULL;
  289. kfree(priv);
  290. dlm_unregister_domain(dlm);
  291. conn->cc_lockspace = NULL;
  292. return 0;
  293. }
  294. static int o2cb_cluster_this_node(unsigned int *node)
  295. {
  296. int node_num;
  297. node_num = o2nm_this_node();
  298. if (node_num == O2NM_INVALID_NODE_NUM)
  299. return -ENOENT;
  300. if (node_num >= O2NM_MAX_NODES)
  301. return -EOVERFLOW;
  302. *node = node_num;
  303. return 0;
  304. }
  305. static struct ocfs2_stack_operations o2cb_stack_ops = {
  306. .connect = o2cb_cluster_connect,
  307. .disconnect = o2cb_cluster_disconnect,
  308. .this_node = o2cb_cluster_this_node,
  309. .dlm_lock = o2cb_dlm_lock,
  310. .dlm_unlock = o2cb_dlm_unlock,
  311. .lock_status = o2cb_dlm_lock_status,
  312. .lvb_valid = o2cb_dlm_lvb_valid,
  313. .lock_lvb = o2cb_dlm_lvb,
  314. .dump_lksb = o2cb_dump_lksb,
  315. };
  316. static struct ocfs2_stack_plugin o2cb_stack = {
  317. .sp_name = "o2cb",
  318. .sp_ops = &o2cb_stack_ops,
  319. .sp_owner = THIS_MODULE,
  320. };
  321. static int __init o2cb_stack_init(void)
  322. {
  323. return ocfs2_stack_glue_register(&o2cb_stack);
  324. }
  325. static void __exit o2cb_stack_exit(void)
  326. {
  327. ocfs2_stack_glue_unregister(&o2cb_stack);
  328. }
  329. MODULE_AUTHOR("Oracle");
  330. MODULE_DESCRIPTION("ocfs2 driver for the classic o2cb stack");
  331. MODULE_LICENSE("GPL");
  332. module_init(o2cb_stack_init);
  333. module_exit(o2cb_stack_exit);