stack_o2cb.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  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/kernel.h>
  20. #include <linux/crc32.h>
  21. #include <linux/slab.h>
  22. #include <linux/module.h>
  23. /* Needed for AOP_TRUNCATED_PAGE in mlog_errno() */
  24. #include <linux/fs.h>
  25. #include "cluster/masklog.h"
  26. #include "cluster/nodemanager.h"
  27. #include "cluster/heartbeat.h"
  28. #include "cluster/tcp.h"
  29. #include "stackglue.h"
  30. struct o2dlm_private {
  31. struct dlm_eviction_cb op_eviction_cb;
  32. };
  33. static struct ocfs2_stack_plugin o2cb_stack;
  34. /* These should be identical */
  35. #if (DLM_LOCK_IV != LKM_IVMODE)
  36. # error Lock modes do not match
  37. #endif
  38. #if (DLM_LOCK_NL != LKM_NLMODE)
  39. # error Lock modes do not match
  40. #endif
  41. #if (DLM_LOCK_CR != LKM_CRMODE)
  42. # error Lock modes do not match
  43. #endif
  44. #if (DLM_LOCK_CW != LKM_CWMODE)
  45. # error Lock modes do not match
  46. #endif
  47. #if (DLM_LOCK_PR != LKM_PRMODE)
  48. # error Lock modes do not match
  49. #endif
  50. #if (DLM_LOCK_PW != LKM_PWMODE)
  51. # error Lock modes do not match
  52. #endif
  53. #if (DLM_LOCK_EX != LKM_EXMODE)
  54. # error Lock modes do not match
  55. #endif
  56. static inline int mode_to_o2dlm(int mode)
  57. {
  58. BUG_ON(mode > LKM_MAXMODE);
  59. return mode;
  60. }
  61. #define map_flag(_generic, _o2dlm) \
  62. if (flags & (_generic)) { \
  63. flags &= ~(_generic); \
  64. o2dlm_flags |= (_o2dlm); \
  65. }
  66. static int flags_to_o2dlm(u32 flags)
  67. {
  68. int o2dlm_flags = 0;
  69. map_flag(DLM_LKF_NOQUEUE, LKM_NOQUEUE);
  70. map_flag(DLM_LKF_CANCEL, LKM_CANCEL);
  71. map_flag(DLM_LKF_CONVERT, LKM_CONVERT);
  72. map_flag(DLM_LKF_VALBLK, LKM_VALBLK);
  73. map_flag(DLM_LKF_IVVALBLK, LKM_INVVALBLK);
  74. map_flag(DLM_LKF_ORPHAN, LKM_ORPHAN);
  75. map_flag(DLM_LKF_FORCEUNLOCK, LKM_FORCE);
  76. map_flag(DLM_LKF_TIMEOUT, LKM_TIMEOUT);
  77. map_flag(DLM_LKF_LOCAL, LKM_LOCAL);
  78. /* map_flag() should have cleared every flag passed in */
  79. BUG_ON(flags != 0);
  80. return o2dlm_flags;
  81. }
  82. #undef map_flag
  83. /*
  84. * Map an o2dlm status to standard errno values.
  85. *
  86. * o2dlm only uses a handful of these, and returns even fewer to the
  87. * caller. Still, we try to assign sane values to each error.
  88. *
  89. * The following value pairs have special meanings to dlmglue, thus
  90. * the right hand side needs to stay unique - never duplicate the
  91. * mapping elsewhere in the table!
  92. *
  93. * DLM_NORMAL: 0
  94. * DLM_NOTQUEUED: -EAGAIN
  95. * DLM_CANCELGRANT: -EBUSY
  96. * DLM_CANCEL: -DLM_ECANCEL
  97. */
  98. /* Keep in sync with dlmapi.h */
  99. static int status_map[] = {
  100. [DLM_NORMAL] = 0, /* Success */
  101. [DLM_GRANTED] = -EINVAL,
  102. [DLM_DENIED] = -EACCES,
  103. [DLM_DENIED_NOLOCKS] = -EACCES,
  104. [DLM_WORKING] = -EACCES,
  105. [DLM_BLOCKED] = -EINVAL,
  106. [DLM_BLOCKED_ORPHAN] = -EINVAL,
  107. [DLM_DENIED_GRACE_PERIOD] = -EACCES,
  108. [DLM_SYSERR] = -ENOMEM, /* It is what it is */
  109. [DLM_NOSUPPORT] = -EPROTO,
  110. [DLM_CANCELGRANT] = -EBUSY, /* Cancel after grant */
  111. [DLM_IVLOCKID] = -EINVAL,
  112. [DLM_SYNC] = -EINVAL,
  113. [DLM_BADTYPE] = -EINVAL,
  114. [DLM_BADRESOURCE] = -EINVAL,
  115. [DLM_MAXHANDLES] = -ENOMEM,
  116. [DLM_NOCLINFO] = -EINVAL,
  117. [DLM_NOLOCKMGR] = -EINVAL,
  118. [DLM_NOPURGED] = -EINVAL,
  119. [DLM_BADARGS] = -EINVAL,
  120. [DLM_VOID] = -EINVAL,
  121. [DLM_NOTQUEUED] = -EAGAIN, /* Trylock failed */
  122. [DLM_IVBUFLEN] = -EINVAL,
  123. [DLM_CVTUNGRANT] = -EPERM,
  124. [DLM_BADPARAM] = -EINVAL,
  125. [DLM_VALNOTVALID] = -EINVAL,
  126. [DLM_REJECTED] = -EPERM,
  127. [DLM_ABORT] = -EINVAL,
  128. [DLM_CANCEL] = -DLM_ECANCEL, /* Successful cancel */
  129. [DLM_IVRESHANDLE] = -EINVAL,
  130. [DLM_DEADLOCK] = -EDEADLK,
  131. [DLM_DENIED_NOASTS] = -EINVAL,
  132. [DLM_FORWARD] = -EINVAL,
  133. [DLM_TIMEOUT] = -ETIMEDOUT,
  134. [DLM_IVGROUPID] = -EINVAL,
  135. [DLM_VERS_CONFLICT] = -EOPNOTSUPP,
  136. [DLM_BAD_DEVICE_PATH] = -ENOENT,
  137. [DLM_NO_DEVICE_PERMISSION] = -EPERM,
  138. [DLM_NO_CONTROL_DEVICE] = -ENOENT,
  139. [DLM_RECOVERING] = -ENOTCONN,
  140. [DLM_MIGRATING] = -ERESTART,
  141. [DLM_MAXSTATS] = -EINVAL,
  142. };
  143. static int dlm_status_to_errno(enum dlm_status status)
  144. {
  145. BUG_ON(status < 0 || status >= ARRAY_SIZE(status_map));
  146. return status_map[status];
  147. }
  148. static void o2dlm_lock_ast_wrapper(void *astarg)
  149. {
  150. struct ocfs2_dlm_lksb *lksb = astarg;
  151. lksb->lksb_conn->cc_proto->lp_lock_ast(lksb);
  152. }
  153. static void o2dlm_blocking_ast_wrapper(void *astarg, int level)
  154. {
  155. struct ocfs2_dlm_lksb *lksb = astarg;
  156. lksb->lksb_conn->cc_proto->lp_blocking_ast(lksb, level);
  157. }
  158. static void o2dlm_unlock_ast_wrapper(void *astarg, enum dlm_status status)
  159. {
  160. struct ocfs2_dlm_lksb *lksb = astarg;
  161. int error = dlm_status_to_errno(status);
  162. /*
  163. * In o2dlm, you can get both the lock_ast() for the lock being
  164. * granted and the unlock_ast() for the CANCEL failing. A
  165. * successful cancel sends DLM_NORMAL here. If the
  166. * lock grant happened before the cancel arrived, you get
  167. * DLM_CANCELGRANT.
  168. *
  169. * There's no need for the double-ast. If we see DLM_CANCELGRANT,
  170. * we just ignore it. We expect the lock_ast() to handle the
  171. * granted lock.
  172. */
  173. if (status == DLM_CANCELGRANT)
  174. return;
  175. lksb->lksb_conn->cc_proto->lp_unlock_ast(lksb, error);
  176. }
  177. static int o2cb_dlm_lock(struct ocfs2_cluster_connection *conn,
  178. int mode,
  179. struct ocfs2_dlm_lksb *lksb,
  180. u32 flags,
  181. void *name,
  182. unsigned int namelen)
  183. {
  184. enum dlm_status status;
  185. int o2dlm_mode = mode_to_o2dlm(mode);
  186. int o2dlm_flags = flags_to_o2dlm(flags);
  187. int ret;
  188. status = dlmlock(conn->cc_lockspace, o2dlm_mode, &lksb->lksb_o2dlm,
  189. o2dlm_flags, name, namelen,
  190. o2dlm_lock_ast_wrapper, lksb,
  191. o2dlm_blocking_ast_wrapper);
  192. ret = dlm_status_to_errno(status);
  193. return ret;
  194. }
  195. static int o2cb_dlm_unlock(struct ocfs2_cluster_connection *conn,
  196. struct ocfs2_dlm_lksb *lksb,
  197. u32 flags)
  198. {
  199. enum dlm_status status;
  200. int o2dlm_flags = flags_to_o2dlm(flags);
  201. int ret;
  202. status = dlmunlock(conn->cc_lockspace, &lksb->lksb_o2dlm,
  203. o2dlm_flags, o2dlm_unlock_ast_wrapper, lksb);
  204. ret = dlm_status_to_errno(status);
  205. return ret;
  206. }
  207. static int o2cb_dlm_lock_status(struct ocfs2_dlm_lksb *lksb)
  208. {
  209. return dlm_status_to_errno(lksb->lksb_o2dlm.status);
  210. }
  211. /*
  212. * o2dlm aways has a "valid" LVB. If the dlm loses track of the LVB
  213. * contents, it will zero out the LVB. Thus the caller can always trust
  214. * the contents.
  215. */
  216. static int o2cb_dlm_lvb_valid(struct ocfs2_dlm_lksb *lksb)
  217. {
  218. return 1;
  219. }
  220. static void *o2cb_dlm_lvb(struct ocfs2_dlm_lksb *lksb)
  221. {
  222. return (void *)(lksb->lksb_o2dlm.lvb);
  223. }
  224. static void o2cb_dump_lksb(struct ocfs2_dlm_lksb *lksb)
  225. {
  226. dlm_print_one_lock(lksb->lksb_o2dlm.lockid);
  227. }
  228. /*
  229. * Check if this node is heartbeating and is connected to all other
  230. * heartbeating nodes.
  231. */
  232. static int o2cb_cluster_check(void)
  233. {
  234. u8 node_num;
  235. int i;
  236. unsigned long hbmap[BITS_TO_LONGS(O2NM_MAX_NODES)];
  237. unsigned long netmap[BITS_TO_LONGS(O2NM_MAX_NODES)];
  238. node_num = o2nm_this_node();
  239. if (node_num == O2NM_MAX_NODES) {
  240. printk(KERN_ERR "o2cb: This node has not been configured.\n");
  241. return -EINVAL;
  242. }
  243. /*
  244. * o2dlm expects o2net sockets to be created. If not, then
  245. * dlm_join_domain() fails with a stack of errors which are both cryptic
  246. * and incomplete. The idea here is to detect upfront whether we have
  247. * managed to connect to all nodes or not. If not, then list the nodes
  248. * to allow the user to check the configuration (incorrect IP, firewall,
  249. * etc.) Yes, this is racy. But its not the end of the world.
  250. */
  251. #define O2CB_MAP_STABILIZE_COUNT 60
  252. for (i = 0; i < O2CB_MAP_STABILIZE_COUNT; ++i) {
  253. o2hb_fill_node_map(hbmap, sizeof(hbmap));
  254. if (!test_bit(node_num, hbmap)) {
  255. printk(KERN_ERR "o2cb: %s heartbeat has not been "
  256. "started.\n", (o2hb_global_heartbeat_active() ?
  257. "Global" : "Local"));
  258. return -EINVAL;
  259. }
  260. o2net_fill_node_map(netmap, sizeof(netmap));
  261. /* Force set the current node to allow easy compare */
  262. set_bit(node_num, netmap);
  263. if (!memcmp(hbmap, netmap, sizeof(hbmap)))
  264. return 0;
  265. if (i < O2CB_MAP_STABILIZE_COUNT - 1)
  266. msleep(1000);
  267. }
  268. printk(KERN_ERR "o2cb: This node could not connect to nodes:");
  269. i = -1;
  270. while ((i = find_next_bit(hbmap, O2NM_MAX_NODES,
  271. i + 1)) < O2NM_MAX_NODES) {
  272. if (!test_bit(i, netmap))
  273. printk(" %u", i);
  274. }
  275. printk(".\n");
  276. return -ENOTCONN;
  277. }
  278. /*
  279. * Called from the dlm when it's about to evict a node. This is how the
  280. * classic stack signals node death.
  281. */
  282. static void o2dlm_eviction_cb(int node_num, void *data)
  283. {
  284. struct ocfs2_cluster_connection *conn = data;
  285. printk(KERN_NOTICE "o2cb: o2dlm has evicted node %d from domain %.*s\n",
  286. node_num, conn->cc_namelen, conn->cc_name);
  287. conn->cc_recovery_handler(node_num, conn->cc_recovery_data);
  288. }
  289. static int o2cb_cluster_connect(struct ocfs2_cluster_connection *conn)
  290. {
  291. int rc = 0;
  292. u32 dlm_key;
  293. struct dlm_ctxt *dlm;
  294. struct o2dlm_private *priv;
  295. struct dlm_protocol_version fs_version;
  296. BUG_ON(conn == NULL);
  297. BUG_ON(conn->cc_proto == NULL);
  298. /* Ensure cluster stack is up and all nodes are connected */
  299. rc = o2cb_cluster_check();
  300. if (rc) {
  301. printk(KERN_ERR "o2cb: Cluster check failed. Fix errors "
  302. "before retrying.\n");
  303. goto out;
  304. }
  305. priv = kzalloc(sizeof(struct o2dlm_private), GFP_KERNEL);
  306. if (!priv) {
  307. rc = -ENOMEM;
  308. goto out_free;
  309. }
  310. /* This just fills the structure in. It is safe to pass conn. */
  311. dlm_setup_eviction_cb(&priv->op_eviction_cb, o2dlm_eviction_cb,
  312. conn);
  313. conn->cc_private = priv;
  314. /* used by the dlm code to make message headers unique, each
  315. * node in this domain must agree on this. */
  316. dlm_key = crc32_le(0, conn->cc_name, conn->cc_namelen);
  317. fs_version.pv_major = conn->cc_version.pv_major;
  318. fs_version.pv_minor = conn->cc_version.pv_minor;
  319. dlm = dlm_register_domain(conn->cc_name, dlm_key, &fs_version);
  320. if (IS_ERR(dlm)) {
  321. rc = PTR_ERR(dlm);
  322. mlog_errno(rc);
  323. goto out_free;
  324. }
  325. conn->cc_version.pv_major = fs_version.pv_major;
  326. conn->cc_version.pv_minor = fs_version.pv_minor;
  327. conn->cc_lockspace = dlm;
  328. dlm_register_eviction_cb(dlm, &priv->op_eviction_cb);
  329. out_free:
  330. if (rc)
  331. kfree(conn->cc_private);
  332. out:
  333. return rc;
  334. }
  335. static int o2cb_cluster_disconnect(struct ocfs2_cluster_connection *conn)
  336. {
  337. struct dlm_ctxt *dlm = conn->cc_lockspace;
  338. struct o2dlm_private *priv = conn->cc_private;
  339. dlm_unregister_eviction_cb(&priv->op_eviction_cb);
  340. conn->cc_private = NULL;
  341. kfree(priv);
  342. dlm_unregister_domain(dlm);
  343. conn->cc_lockspace = NULL;
  344. return 0;
  345. }
  346. static int o2cb_cluster_this_node(struct ocfs2_cluster_connection *conn,
  347. unsigned int *node)
  348. {
  349. int node_num;
  350. node_num = o2nm_this_node();
  351. if (node_num == O2NM_INVALID_NODE_NUM)
  352. return -ENOENT;
  353. if (node_num >= O2NM_MAX_NODES)
  354. return -EOVERFLOW;
  355. *node = node_num;
  356. return 0;
  357. }
  358. static struct ocfs2_stack_operations o2cb_stack_ops = {
  359. .connect = o2cb_cluster_connect,
  360. .disconnect = o2cb_cluster_disconnect,
  361. .this_node = o2cb_cluster_this_node,
  362. .dlm_lock = o2cb_dlm_lock,
  363. .dlm_unlock = o2cb_dlm_unlock,
  364. .lock_status = o2cb_dlm_lock_status,
  365. .lvb_valid = o2cb_dlm_lvb_valid,
  366. .lock_lvb = o2cb_dlm_lvb,
  367. .dump_lksb = o2cb_dump_lksb,
  368. };
  369. static struct ocfs2_stack_plugin o2cb_stack = {
  370. .sp_name = "o2cb",
  371. .sp_ops = &o2cb_stack_ops,
  372. .sp_owner = THIS_MODULE,
  373. };
  374. static int __init o2cb_stack_init(void)
  375. {
  376. return ocfs2_stack_glue_register(&o2cb_stack);
  377. }
  378. static void __exit o2cb_stack_exit(void)
  379. {
  380. ocfs2_stack_glue_unregister(&o2cb_stack);
  381. }
  382. MODULE_AUTHOR("Oracle");
  383. MODULE_DESCRIPTION("ocfs2 driver for the classic o2cb stack");
  384. MODULE_LICENSE("GPL");
  385. module_init(o2cb_stack_init);
  386. module_exit(o2cb_stack_exit);