stack_o2cb.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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/kmod.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. /* These should be identical */
  31. #if (DLM_LOCK_IV != LKM_IVMODE)
  32. # error Lock modes do not match
  33. #endif
  34. #if (DLM_LOCK_NL != LKM_NLMODE)
  35. # error Lock modes do not match
  36. #endif
  37. #if (DLM_LOCK_CR != LKM_CRMODE)
  38. # error Lock modes do not match
  39. #endif
  40. #if (DLM_LOCK_CW != LKM_CWMODE)
  41. # error Lock modes do not match
  42. #endif
  43. #if (DLM_LOCK_PR != LKM_PRMODE)
  44. # error Lock modes do not match
  45. #endif
  46. #if (DLM_LOCK_PW != LKM_PWMODE)
  47. # error Lock modes do not match
  48. #endif
  49. #if (DLM_LOCK_EX != LKM_EXMODE)
  50. # error Lock modes do not match
  51. #endif
  52. static inline int mode_to_o2dlm(int mode)
  53. {
  54. BUG_ON(mode > LKM_MAXMODE);
  55. return mode;
  56. }
  57. #define map_flag(_generic, _o2dlm) \
  58. if (flags & (_generic)) { \
  59. flags &= ~(_generic); \
  60. o2dlm_flags |= (_o2dlm); \
  61. }
  62. static int flags_to_o2dlm(u32 flags)
  63. {
  64. int o2dlm_flags = 0;
  65. map_flag(DLM_LKF_NOQUEUE, LKM_NOQUEUE);
  66. map_flag(DLM_LKF_CANCEL, LKM_CANCEL);
  67. map_flag(DLM_LKF_CONVERT, LKM_CONVERT);
  68. map_flag(DLM_LKF_VALBLK, LKM_VALBLK);
  69. map_flag(DLM_LKF_IVVALBLK, LKM_INVVALBLK);
  70. map_flag(DLM_LKF_ORPHAN, LKM_ORPHAN);
  71. map_flag(DLM_LKF_FORCEUNLOCK, LKM_FORCE);
  72. map_flag(DLM_LKF_TIMEOUT, LKM_TIMEOUT);
  73. map_flag(DLM_LKF_LOCAL, LKM_LOCAL);
  74. /* map_flag() should have cleared every flag passed in */
  75. BUG_ON(flags != 0);
  76. return o2dlm_flags;
  77. }
  78. #undef map_flag
  79. /*
  80. * Map an o2dlm status to standard errno values.
  81. *
  82. * o2dlm only uses a handful of these, and returns even fewer to the
  83. * caller. Still, we try to assign sane values to each error.
  84. *
  85. * The following value pairs have special meanings to dlmglue, thus
  86. * the right hand side needs to stay unique - never duplicate the
  87. * mapping elsewhere in the table!
  88. *
  89. * DLM_NORMAL: 0
  90. * DLM_NOTQUEUED: -EAGAIN
  91. * DLM_CANCELGRANT: -EBUSY
  92. * DLM_CANCEL: -DLM_ECANCEL
  93. */
  94. /* Keep in sync with dlmapi.h */
  95. static int status_map[] = {
  96. [DLM_NORMAL] = 0, /* Success */
  97. [DLM_GRANTED] = -EINVAL,
  98. [DLM_DENIED] = -EACCES,
  99. [DLM_DENIED_NOLOCKS] = -EACCES,
  100. [DLM_WORKING] = -EACCES,
  101. [DLM_BLOCKED] = -EINVAL,
  102. [DLM_BLOCKED_ORPHAN] = -EINVAL,
  103. [DLM_DENIED_GRACE_PERIOD] = -EACCES,
  104. [DLM_SYSERR] = -ENOMEM, /* It is what it is */
  105. [DLM_NOSUPPORT] = -EPROTO,
  106. [DLM_CANCELGRANT] = -EBUSY, /* Cancel after grant */
  107. [DLM_IVLOCKID] = -EINVAL,
  108. [DLM_SYNC] = -EINVAL,
  109. [DLM_BADTYPE] = -EINVAL,
  110. [DLM_BADRESOURCE] = -EINVAL,
  111. [DLM_MAXHANDLES] = -ENOMEM,
  112. [DLM_NOCLINFO] = -EINVAL,
  113. [DLM_NOLOCKMGR] = -EINVAL,
  114. [DLM_NOPURGED] = -EINVAL,
  115. [DLM_BADARGS] = -EINVAL,
  116. [DLM_VOID] = -EINVAL,
  117. [DLM_NOTQUEUED] = -EAGAIN, /* Trylock failed */
  118. [DLM_IVBUFLEN] = -EINVAL,
  119. [DLM_CVTUNGRANT] = -EPERM,
  120. [DLM_BADPARAM] = -EINVAL,
  121. [DLM_VALNOTVALID] = -EINVAL,
  122. [DLM_REJECTED] = -EPERM,
  123. [DLM_ABORT] = -EINVAL,
  124. [DLM_CANCEL] = -DLM_ECANCEL, /* Successful cancel */
  125. [DLM_IVRESHANDLE] = -EINVAL,
  126. [DLM_DEADLOCK] = -EDEADLK,
  127. [DLM_DENIED_NOASTS] = -EINVAL,
  128. [DLM_FORWARD] = -EINVAL,
  129. [DLM_TIMEOUT] = -ETIMEDOUT,
  130. [DLM_IVGROUPID] = -EINVAL,
  131. [DLM_VERS_CONFLICT] = -EOPNOTSUPP,
  132. [DLM_BAD_DEVICE_PATH] = -ENOENT,
  133. [DLM_NO_DEVICE_PERMISSION] = -EPERM,
  134. [DLM_NO_CONTROL_DEVICE] = -ENOENT,
  135. [DLM_RECOVERING] = -ENOTCONN,
  136. [DLM_MIGRATING] = -ERESTART,
  137. [DLM_MAXSTATS] = -EINVAL,
  138. };
  139. static int dlm_status_to_errno(enum dlm_status status)
  140. {
  141. BUG_ON(status > (sizeof(status_map) / sizeof(status_map[0])));
  142. return status_map[status];
  143. }
  144. static void o2dlm_lock_ast_wrapper(void *astarg)
  145. {
  146. BUG_ON(stack_glue_lproto == NULL);
  147. stack_glue_lproto->lp_lock_ast(astarg);
  148. }
  149. static void o2dlm_blocking_ast_wrapper(void *astarg, int level)
  150. {
  151. BUG_ON(stack_glue_lproto == NULL);
  152. stack_glue_lproto->lp_blocking_ast(astarg, level);
  153. }
  154. static void o2dlm_unlock_ast_wrapper(void *astarg, enum dlm_status status)
  155. {
  156. int error = dlm_status_to_errno(status);
  157. BUG_ON(stack_glue_lproto == NULL);
  158. /*
  159. * In o2dlm, you can get both the lock_ast() for the lock being
  160. * granted and the unlock_ast() for the CANCEL failing. A
  161. * successful cancel sends DLM_NORMAL here. If the
  162. * lock grant happened before the cancel arrived, you get
  163. * DLM_CANCELGRANT.
  164. *
  165. * There's no need for the double-ast. If we see DLM_CANCELGRANT,
  166. * we just ignore it. We expect the lock_ast() to handle the
  167. * granted lock.
  168. */
  169. if (status == DLM_CANCELGRANT)
  170. return;
  171. stack_glue_lproto->lp_unlock_ast(astarg, error);
  172. }
  173. static int o2cb_dlm_lock(struct ocfs2_cluster_connection *conn,
  174. int mode,
  175. union ocfs2_dlm_lksb *lksb,
  176. u32 flags,
  177. void *name,
  178. unsigned int namelen,
  179. void *astarg)
  180. {
  181. enum dlm_status status;
  182. int o2dlm_mode = mode_to_o2dlm(mode);
  183. int o2dlm_flags = flags_to_o2dlm(flags);
  184. int ret;
  185. status = dlmlock(conn->cc_lockspace, o2dlm_mode, &lksb->lksb_o2dlm,
  186. o2dlm_flags, name, namelen,
  187. o2dlm_lock_ast_wrapper, astarg,
  188. o2dlm_blocking_ast_wrapper);
  189. ret = dlm_status_to_errno(status);
  190. return ret;
  191. }
  192. static int o2cb_dlm_unlock(struct ocfs2_cluster_connection *conn,
  193. union ocfs2_dlm_lksb *lksb,
  194. u32 flags,
  195. void *astarg)
  196. {
  197. enum dlm_status status;
  198. int o2dlm_flags = flags_to_o2dlm(flags);
  199. int ret;
  200. status = dlmunlock(conn->cc_lockspace, &lksb->lksb_o2dlm,
  201. o2dlm_flags, o2dlm_unlock_ast_wrapper, astarg);
  202. ret = dlm_status_to_errno(status);
  203. return ret;
  204. }
  205. static int o2cb_dlm_lock_status(union ocfs2_dlm_lksb *lksb)
  206. {
  207. return dlm_status_to_errno(lksb->lksb_o2dlm.status);
  208. }
  209. static void *o2cb_dlm_lvb(union ocfs2_dlm_lksb *lksb)
  210. {
  211. return (void *)(lksb->lksb_o2dlm.lvb);
  212. }
  213. static void o2cb_dump_lksb(union ocfs2_dlm_lksb *lksb)
  214. {
  215. dlm_print_one_lock(lksb->lksb_o2dlm.lockid);
  216. }
  217. /*
  218. * Called from the dlm when it's about to evict a node. This is how the
  219. * classic stack signals node death.
  220. */
  221. static void o2dlm_eviction_cb(int node_num, void *data)
  222. {
  223. struct ocfs2_cluster_connection *conn = data;
  224. mlog(ML_NOTICE, "o2dlm has evicted node %d from group %.*s\n",
  225. node_num, conn->cc_namelen, conn->cc_name);
  226. conn->cc_recovery_handler(node_num, conn->cc_recovery_data);
  227. }
  228. static int o2cb_cluster_connect(struct ocfs2_cluster_connection *conn)
  229. {
  230. int rc = 0;
  231. u32 dlm_key;
  232. struct dlm_ctxt *dlm;
  233. struct o2dlm_private *priv;
  234. struct dlm_protocol_version dlm_version;
  235. BUG_ON(conn == NULL);
  236. /* for now we only have one cluster/node, make sure we see it
  237. * in the heartbeat universe */
  238. if (!o2hb_check_local_node_heartbeating()) {
  239. rc = -EINVAL;
  240. goto out;
  241. }
  242. priv = kzalloc(sizeof(struct o2dlm_private), GFP_KERNEL);
  243. if (!priv) {
  244. rc = -ENOMEM;
  245. goto out_free;
  246. }
  247. /* This just fills the structure in. It is safe to pass conn. */
  248. dlm_setup_eviction_cb(&priv->op_eviction_cb, o2dlm_eviction_cb,
  249. conn);
  250. conn->cc_private = priv;
  251. /* used by the dlm code to make message headers unique, each
  252. * node in this domain must agree on this. */
  253. dlm_key = crc32_le(0, conn->cc_name, conn->cc_namelen);
  254. dlm_version.pv_major = conn->cc_version.pv_major;
  255. dlm_version.pv_minor = conn->cc_version.pv_minor;
  256. dlm = dlm_register_domain(conn->cc_name, dlm_key, &dlm_version);
  257. if (IS_ERR(dlm)) {
  258. rc = PTR_ERR(dlm);
  259. mlog_errno(rc);
  260. goto out_free;
  261. }
  262. conn->cc_version.pv_major = dlm_version.pv_major;
  263. conn->cc_version.pv_minor = dlm_version.pv_minor;
  264. conn->cc_lockspace = dlm;
  265. dlm_register_eviction_cb(dlm, &priv->op_eviction_cb);
  266. out_free:
  267. if (rc && conn->cc_private)
  268. kfree(conn->cc_private);
  269. out:
  270. return rc;
  271. }
  272. static int o2cb_cluster_disconnect(struct ocfs2_cluster_connection *conn)
  273. {
  274. struct dlm_ctxt *dlm = conn->cc_lockspace;
  275. struct o2dlm_private *priv = conn->cc_private;
  276. dlm_unregister_eviction_cb(&priv->op_eviction_cb);
  277. conn->cc_private = NULL;
  278. kfree(priv);
  279. dlm_unregister_domain(dlm);
  280. conn->cc_lockspace = NULL;
  281. return 0;
  282. }
  283. static void o2hb_stop(const char *group)
  284. {
  285. int ret;
  286. char *argv[5], *envp[3];
  287. argv[0] = (char *)o2nm_get_hb_ctl_path();
  288. argv[1] = "-K";
  289. argv[2] = "-u";
  290. argv[3] = (char *)group;
  291. argv[4] = NULL;
  292. mlog(0, "Run: %s %s %s %s\n", argv[0], argv[1], argv[2], argv[3]);
  293. /* minimal command environment taken from cpu_run_sbin_hotplug */
  294. envp[0] = "HOME=/";
  295. envp[1] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
  296. envp[2] = NULL;
  297. ret = call_usermodehelper(argv[0], argv, envp, UMH_WAIT_PROC);
  298. if (ret < 0)
  299. mlog_errno(ret);
  300. }
  301. /*
  302. * Hangup is a hack for tools compatibility. Older ocfs2-tools software
  303. * expects the filesystem to call "ocfs2_hb_ctl" during unmount. This
  304. * happens regardless of whether the DLM got started, so we can't do it
  305. * in ocfs2_cluster_disconnect(). We bring the o2hb_stop() function into
  306. * the glue and provide a "hangup" API for super.c to call.
  307. *
  308. * Other stacks will eventually provide a NULL ->hangup() pointer.
  309. */
  310. static void o2cb_cluster_hangup(const char *group, int grouplen)
  311. {
  312. o2hb_stop(group);
  313. }
  314. static int o2cb_cluster_this_node(unsigned int *node)
  315. {
  316. int node_num;
  317. node_num = o2nm_this_node();
  318. if (node_num == O2NM_INVALID_NODE_NUM)
  319. return -ENOENT;
  320. if (node_num >= O2NM_MAX_NODES)
  321. return -EOVERFLOW;
  322. *node = node_num;
  323. return 0;
  324. }
  325. struct ocfs2_stack_operations o2cb_stack_ops = {
  326. .connect = o2cb_cluster_connect,
  327. .disconnect = o2cb_cluster_disconnect,
  328. .hangup = o2cb_cluster_hangup,
  329. .this_node = o2cb_cluster_this_node,
  330. .dlm_lock = o2cb_dlm_lock,
  331. .dlm_unlock = o2cb_dlm_unlock,
  332. .lock_status = o2cb_dlm_lock_status,
  333. .lock_lvb = o2cb_dlm_lvb,
  334. .dump_lksb = o2cb_dump_lksb,
  335. };