stack_o2cb.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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 "stackglue.h"
  29. struct o2dlm_private {
  30. struct dlm_eviction_cb op_eviction_cb;
  31. };
  32. static struct ocfs2_stack_plugin o2cb_stack;
  33. /* These should be identical */
  34. #if (DLM_LOCK_IV != LKM_IVMODE)
  35. # error Lock modes do not match
  36. #endif
  37. #if (DLM_LOCK_NL != LKM_NLMODE)
  38. # error Lock modes do not match
  39. #endif
  40. #if (DLM_LOCK_CR != LKM_CRMODE)
  41. # error Lock modes do not match
  42. #endif
  43. #if (DLM_LOCK_CW != LKM_CWMODE)
  44. # error Lock modes do not match
  45. #endif
  46. #if (DLM_LOCK_PR != LKM_PRMODE)
  47. # error Lock modes do not match
  48. #endif
  49. #if (DLM_LOCK_PW != LKM_PWMODE)
  50. # error Lock modes do not match
  51. #endif
  52. #if (DLM_LOCK_EX != LKM_EXMODE)
  53. # error Lock modes do not match
  54. #endif
  55. static inline int mode_to_o2dlm(int mode)
  56. {
  57. BUG_ON(mode > LKM_MAXMODE);
  58. return mode;
  59. }
  60. #define map_flag(_generic, _o2dlm) \
  61. if (flags & (_generic)) { \
  62. flags &= ~(_generic); \
  63. o2dlm_flags |= (_o2dlm); \
  64. }
  65. static int flags_to_o2dlm(u32 flags)
  66. {
  67. int o2dlm_flags = 0;
  68. map_flag(DLM_LKF_NOQUEUE, LKM_NOQUEUE);
  69. map_flag(DLM_LKF_CANCEL, LKM_CANCEL);
  70. map_flag(DLM_LKF_CONVERT, LKM_CONVERT);
  71. map_flag(DLM_LKF_VALBLK, LKM_VALBLK);
  72. map_flag(DLM_LKF_IVVALBLK, LKM_INVVALBLK);
  73. map_flag(DLM_LKF_ORPHAN, LKM_ORPHAN);
  74. map_flag(DLM_LKF_FORCEUNLOCK, LKM_FORCE);
  75. map_flag(DLM_LKF_TIMEOUT, LKM_TIMEOUT);
  76. map_flag(DLM_LKF_LOCAL, LKM_LOCAL);
  77. /* map_flag() should have cleared every flag passed in */
  78. BUG_ON(flags != 0);
  79. return o2dlm_flags;
  80. }
  81. #undef map_flag
  82. /*
  83. * Map an o2dlm status to standard errno values.
  84. *
  85. * o2dlm only uses a handful of these, and returns even fewer to the
  86. * caller. Still, we try to assign sane values to each error.
  87. *
  88. * The following value pairs have special meanings to dlmglue, thus
  89. * the right hand side needs to stay unique - never duplicate the
  90. * mapping elsewhere in the table!
  91. *
  92. * DLM_NORMAL: 0
  93. * DLM_NOTQUEUED: -EAGAIN
  94. * DLM_CANCELGRANT: -EBUSY
  95. * DLM_CANCEL: -DLM_ECANCEL
  96. */
  97. /* Keep in sync with dlmapi.h */
  98. static int status_map[] = {
  99. [DLM_NORMAL] = 0, /* Success */
  100. [DLM_GRANTED] = -EINVAL,
  101. [DLM_DENIED] = -EACCES,
  102. [DLM_DENIED_NOLOCKS] = -EACCES,
  103. [DLM_WORKING] = -EACCES,
  104. [DLM_BLOCKED] = -EINVAL,
  105. [DLM_BLOCKED_ORPHAN] = -EINVAL,
  106. [DLM_DENIED_GRACE_PERIOD] = -EACCES,
  107. [DLM_SYSERR] = -ENOMEM, /* It is what it is */
  108. [DLM_NOSUPPORT] = -EPROTO,
  109. [DLM_CANCELGRANT] = -EBUSY, /* Cancel after grant */
  110. [DLM_IVLOCKID] = -EINVAL,
  111. [DLM_SYNC] = -EINVAL,
  112. [DLM_BADTYPE] = -EINVAL,
  113. [DLM_BADRESOURCE] = -EINVAL,
  114. [DLM_MAXHANDLES] = -ENOMEM,
  115. [DLM_NOCLINFO] = -EINVAL,
  116. [DLM_NOLOCKMGR] = -EINVAL,
  117. [DLM_NOPURGED] = -EINVAL,
  118. [DLM_BADARGS] = -EINVAL,
  119. [DLM_VOID] = -EINVAL,
  120. [DLM_NOTQUEUED] = -EAGAIN, /* Trylock failed */
  121. [DLM_IVBUFLEN] = -EINVAL,
  122. [DLM_CVTUNGRANT] = -EPERM,
  123. [DLM_BADPARAM] = -EINVAL,
  124. [DLM_VALNOTVALID] = -EINVAL,
  125. [DLM_REJECTED] = -EPERM,
  126. [DLM_ABORT] = -EINVAL,
  127. [DLM_CANCEL] = -DLM_ECANCEL, /* Successful cancel */
  128. [DLM_IVRESHANDLE] = -EINVAL,
  129. [DLM_DEADLOCK] = -EDEADLK,
  130. [DLM_DENIED_NOASTS] = -EINVAL,
  131. [DLM_FORWARD] = -EINVAL,
  132. [DLM_TIMEOUT] = -ETIMEDOUT,
  133. [DLM_IVGROUPID] = -EINVAL,
  134. [DLM_VERS_CONFLICT] = -EOPNOTSUPP,
  135. [DLM_BAD_DEVICE_PATH] = -ENOENT,
  136. [DLM_NO_DEVICE_PERMISSION] = -EPERM,
  137. [DLM_NO_CONTROL_DEVICE] = -ENOENT,
  138. [DLM_RECOVERING] = -ENOTCONN,
  139. [DLM_MIGRATING] = -ERESTART,
  140. [DLM_MAXSTATS] = -EINVAL,
  141. };
  142. static int dlm_status_to_errno(enum dlm_status status)
  143. {
  144. BUG_ON(status < 0 || status >= ARRAY_SIZE(status_map));
  145. return status_map[status];
  146. }
  147. static void o2dlm_lock_ast_wrapper(void *astarg)
  148. {
  149. struct ocfs2_dlm_lksb *lksb = astarg;
  150. lksb->lksb_conn->cc_proto->lp_lock_ast(lksb);
  151. }
  152. static void o2dlm_blocking_ast_wrapper(void *astarg, int level)
  153. {
  154. struct ocfs2_dlm_lksb *lksb = astarg;
  155. lksb->lksb_conn->cc_proto->lp_blocking_ast(lksb, level);
  156. }
  157. static void o2dlm_unlock_ast_wrapper(void *astarg, enum dlm_status status)
  158. {
  159. struct ocfs2_dlm_lksb *lksb = astarg;
  160. int error = dlm_status_to_errno(status);
  161. /*
  162. * In o2dlm, you can get both the lock_ast() for the lock being
  163. * granted and the unlock_ast() for the CANCEL failing. A
  164. * successful cancel sends DLM_NORMAL here. If the
  165. * lock grant happened before the cancel arrived, you get
  166. * DLM_CANCELGRANT.
  167. *
  168. * There's no need for the double-ast. If we see DLM_CANCELGRANT,
  169. * we just ignore it. We expect the lock_ast() to handle the
  170. * granted lock.
  171. */
  172. if (status == DLM_CANCELGRANT)
  173. return;
  174. lksb->lksb_conn->cc_proto->lp_unlock_ast(lksb, error);
  175. }
  176. static int o2cb_dlm_lock(struct ocfs2_cluster_connection *conn,
  177. int mode,
  178. struct ocfs2_dlm_lksb *lksb,
  179. u32 flags,
  180. void *name,
  181. unsigned int namelen)
  182. {
  183. enum dlm_status status;
  184. int o2dlm_mode = mode_to_o2dlm(mode);
  185. int o2dlm_flags = flags_to_o2dlm(flags);
  186. int ret;
  187. status = dlmlock(conn->cc_lockspace, o2dlm_mode, &lksb->lksb_o2dlm,
  188. o2dlm_flags, name, namelen,
  189. o2dlm_lock_ast_wrapper, lksb,
  190. o2dlm_blocking_ast_wrapper);
  191. ret = dlm_status_to_errno(status);
  192. return ret;
  193. }
  194. static int o2cb_dlm_unlock(struct ocfs2_cluster_connection *conn,
  195. struct ocfs2_dlm_lksb *lksb,
  196. u32 flags)
  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, lksb);
  203. ret = dlm_status_to_errno(status);
  204. return ret;
  205. }
  206. static int o2cb_dlm_lock_status(struct 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(struct ocfs2_dlm_lksb *lksb)
  216. {
  217. return 1;
  218. }
  219. static void *o2cb_dlm_lvb(struct ocfs2_dlm_lksb *lksb)
  220. {
  221. return (void *)(lksb->lksb_o2dlm.lvb);
  222. }
  223. static void o2cb_dump_lksb(struct 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 fs_version;
  245. BUG_ON(conn == NULL);
  246. BUG_ON(conn->cc_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. if (o2hb_global_heartbeat_active())
  251. mlog(ML_ERROR, "Global heartbeat not started\n");
  252. rc = -EINVAL;
  253. goto out;
  254. }
  255. priv = kzalloc(sizeof(struct o2dlm_private), GFP_KERNEL);
  256. if (!priv) {
  257. rc = -ENOMEM;
  258. goto out_free;
  259. }
  260. /* This just fills the structure in. It is safe to pass conn. */
  261. dlm_setup_eviction_cb(&priv->op_eviction_cb, o2dlm_eviction_cb,
  262. conn);
  263. conn->cc_private = priv;
  264. /* used by the dlm code to make message headers unique, each
  265. * node in this domain must agree on this. */
  266. dlm_key = crc32_le(0, conn->cc_name, conn->cc_namelen);
  267. fs_version.pv_major = conn->cc_version.pv_major;
  268. fs_version.pv_minor = conn->cc_version.pv_minor;
  269. dlm = dlm_register_domain(conn->cc_name, dlm_key, &fs_version);
  270. if (IS_ERR(dlm)) {
  271. rc = PTR_ERR(dlm);
  272. mlog_errno(rc);
  273. goto out_free;
  274. }
  275. conn->cc_version.pv_major = fs_version.pv_major;
  276. conn->cc_version.pv_minor = fs_version.pv_minor;
  277. conn->cc_lockspace = dlm;
  278. dlm_register_eviction_cb(dlm, &priv->op_eviction_cb);
  279. out_free:
  280. if (rc && conn->cc_private)
  281. kfree(conn->cc_private);
  282. out:
  283. return rc;
  284. }
  285. static int o2cb_cluster_disconnect(struct ocfs2_cluster_connection *conn)
  286. {
  287. struct dlm_ctxt *dlm = conn->cc_lockspace;
  288. struct o2dlm_private *priv = conn->cc_private;
  289. dlm_unregister_eviction_cb(&priv->op_eviction_cb);
  290. conn->cc_private = NULL;
  291. kfree(priv);
  292. dlm_unregister_domain(dlm);
  293. conn->cc_lockspace = NULL;
  294. return 0;
  295. }
  296. static int o2cb_cluster_this_node(unsigned int *node)
  297. {
  298. int node_num;
  299. node_num = o2nm_this_node();
  300. if (node_num == O2NM_INVALID_NODE_NUM)
  301. return -ENOENT;
  302. if (node_num >= O2NM_MAX_NODES)
  303. return -EOVERFLOW;
  304. *node = node_num;
  305. return 0;
  306. }
  307. static struct ocfs2_stack_operations o2cb_stack_ops = {
  308. .connect = o2cb_cluster_connect,
  309. .disconnect = o2cb_cluster_disconnect,
  310. .this_node = o2cb_cluster_this_node,
  311. .dlm_lock = o2cb_dlm_lock,
  312. .dlm_unlock = o2cb_dlm_unlock,
  313. .lock_status = o2cb_dlm_lock_status,
  314. .lvb_valid = o2cb_dlm_lvb_valid,
  315. .lock_lvb = o2cb_dlm_lvb,
  316. .dump_lksb = o2cb_dump_lksb,
  317. };
  318. static struct ocfs2_stack_plugin o2cb_stack = {
  319. .sp_name = "o2cb",
  320. .sp_ops = &o2cb_stack_ops,
  321. .sp_owner = THIS_MODULE,
  322. };
  323. static int __init o2cb_stack_init(void)
  324. {
  325. return ocfs2_stack_glue_register(&o2cb_stack);
  326. }
  327. static void __exit o2cb_stack_exit(void)
  328. {
  329. ocfs2_stack_glue_unregister(&o2cb_stack);
  330. }
  331. MODULE_AUTHOR("Oracle");
  332. MODULE_DESCRIPTION("ocfs2 driver for the classic o2cb stack");
  333. MODULE_LICENSE("GPL");
  334. module_init(o2cb_stack_init);
  335. module_exit(o2cb_stack_exit);