stackglue.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  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. #include <linux/kmod.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. static struct ocfs2_locking_protocol *lproto;
  30. struct o2dlm_private {
  31. struct dlm_eviction_cb op_eviction_cb;
  32. };
  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 > (sizeof(status_map) / sizeof(status_map[0])));
  145. return status_map[status];
  146. }
  147. static void o2dlm_lock_ast_wrapper(void *astarg)
  148. {
  149. BUG_ON(lproto == NULL);
  150. lproto->lp_lock_ast(astarg);
  151. }
  152. static void o2dlm_blocking_ast_wrapper(void *astarg, int level)
  153. {
  154. BUG_ON(lproto == NULL);
  155. lproto->lp_blocking_ast(astarg, level);
  156. }
  157. static void o2dlm_unlock_ast_wrapper(void *astarg, enum dlm_status status)
  158. {
  159. int error = dlm_status_to_errno(status);
  160. BUG_ON(lproto == NULL);
  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. lproto->lp_unlock_ast(astarg, error);
  175. }
  176. static int o2cb_dlm_lock(struct ocfs2_cluster_connection *conn,
  177. int mode,
  178. union ocfs2_dlm_lksb *lksb,
  179. u32 flags,
  180. void *name,
  181. unsigned int namelen,
  182. void *astarg)
  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, astarg,
  191. o2dlm_blocking_ast_wrapper);
  192. ret = dlm_status_to_errno(status);
  193. return ret;
  194. }
  195. int ocfs2_dlm_lock(struct ocfs2_cluster_connection *conn,
  196. int mode,
  197. union ocfs2_dlm_lksb *lksb,
  198. u32 flags,
  199. void *name,
  200. unsigned int namelen,
  201. void *astarg)
  202. {
  203. BUG_ON(lproto == NULL);
  204. return o2cb_dlm_lock(conn, mode, lksb, flags,
  205. name, namelen, astarg);
  206. }
  207. static int o2cb_dlm_unlock(struct ocfs2_cluster_connection *conn,
  208. union ocfs2_dlm_lksb *lksb,
  209. u32 flags,
  210. void *astarg)
  211. {
  212. enum dlm_status status;
  213. int o2dlm_flags = flags_to_o2dlm(flags);
  214. int ret;
  215. status = dlmunlock(conn->cc_lockspace, &lksb->lksb_o2dlm,
  216. o2dlm_flags, o2dlm_unlock_ast_wrapper, astarg);
  217. ret = dlm_status_to_errno(status);
  218. return ret;
  219. }
  220. int ocfs2_dlm_unlock(struct ocfs2_cluster_connection *conn,
  221. union ocfs2_dlm_lksb *lksb,
  222. u32 flags,
  223. void *astarg)
  224. {
  225. BUG_ON(lproto == NULL);
  226. return o2cb_dlm_unlock(conn, lksb, flags, astarg);
  227. }
  228. static int o2cb_dlm_lock_status(union ocfs2_dlm_lksb *lksb)
  229. {
  230. return dlm_status_to_errno(lksb->lksb_o2dlm.status);
  231. }
  232. int ocfs2_dlm_lock_status(union ocfs2_dlm_lksb *lksb)
  233. {
  234. return o2cb_dlm_lock_status(lksb);
  235. }
  236. /*
  237. * Why don't we cast to ocfs2_meta_lvb? The "clean" answer is that we
  238. * don't cast at the glue level. The real answer is that the header
  239. * ordering is nigh impossible.
  240. */
  241. static void *o2cb_dlm_lvb(union ocfs2_dlm_lksb *lksb)
  242. {
  243. return (void *)(lksb->lksb_o2dlm.lvb);
  244. }
  245. void *ocfs2_dlm_lvb(union ocfs2_dlm_lksb *lksb)
  246. {
  247. return o2cb_dlm_lvb(lksb);
  248. }
  249. static void o2cb_dlm_dump_lksb(union ocfs2_dlm_lksb *lksb)
  250. {
  251. dlm_print_one_lock(lksb->lksb_o2dlm.lockid);
  252. }
  253. void ocfs2_dlm_dump_lksb(union ocfs2_dlm_lksb *lksb)
  254. {
  255. o2cb_dlm_dump_lksb(lksb);
  256. }
  257. /*
  258. * Called from the dlm when it's about to evict a node. This is how the
  259. * classic stack signals node death.
  260. */
  261. static void o2dlm_eviction_cb(int node_num, void *data)
  262. {
  263. struct ocfs2_cluster_connection *conn = data;
  264. mlog(ML_NOTICE, "o2dlm has evicted node %d from group %.*s\n",
  265. node_num, conn->cc_namelen, conn->cc_name);
  266. conn->cc_recovery_handler(node_num, conn->cc_recovery_data);
  267. }
  268. static int o2cb_cluster_connect(struct ocfs2_cluster_connection *conn)
  269. {
  270. int rc = 0;
  271. u32 dlm_key;
  272. struct dlm_ctxt *dlm;
  273. struct o2dlm_private *priv;
  274. struct dlm_protocol_version dlm_version;
  275. BUG_ON(conn == NULL);
  276. /* for now we only have one cluster/node, make sure we see it
  277. * in the heartbeat universe */
  278. if (!o2hb_check_local_node_heartbeating()) {
  279. rc = -EINVAL;
  280. goto out;
  281. }
  282. priv = kzalloc(sizeof(struct o2dlm_private), GFP_KERNEL);
  283. if (!priv) {
  284. rc = -ENOMEM;
  285. goto out_free;
  286. }
  287. /* This just fills the structure in. It is safe to pass conn. */
  288. dlm_setup_eviction_cb(&priv->op_eviction_cb, o2dlm_eviction_cb,
  289. conn);
  290. conn->cc_private = priv;
  291. /* used by the dlm code to make message headers unique, each
  292. * node in this domain must agree on this. */
  293. dlm_key = crc32_le(0, conn->cc_name, conn->cc_namelen);
  294. dlm_version.pv_major = conn->cc_version.pv_major;
  295. dlm_version.pv_minor = conn->cc_version.pv_minor;
  296. dlm = dlm_register_domain(conn->cc_name, dlm_key, &dlm_version);
  297. if (IS_ERR(dlm)) {
  298. rc = PTR_ERR(dlm);
  299. mlog_errno(rc);
  300. goto out_free;
  301. }
  302. conn->cc_version.pv_major = dlm_version.pv_major;
  303. conn->cc_version.pv_minor = dlm_version.pv_minor;
  304. conn->cc_lockspace = dlm;
  305. dlm_register_eviction_cb(dlm, &priv->op_eviction_cb);
  306. out_free:
  307. if (rc && conn->cc_private)
  308. kfree(conn->cc_private);
  309. out:
  310. return rc;
  311. }
  312. int ocfs2_cluster_connect(const char *group,
  313. int grouplen,
  314. void (*recovery_handler)(int node_num,
  315. void *recovery_data),
  316. void *recovery_data,
  317. struct ocfs2_cluster_connection **conn)
  318. {
  319. int rc = 0;
  320. struct ocfs2_cluster_connection *new_conn;
  321. BUG_ON(group == NULL);
  322. BUG_ON(conn == NULL);
  323. BUG_ON(recovery_handler == NULL);
  324. if (grouplen > GROUP_NAME_MAX) {
  325. rc = -EINVAL;
  326. goto out;
  327. }
  328. new_conn = kzalloc(sizeof(struct ocfs2_cluster_connection),
  329. GFP_KERNEL);
  330. if (!new_conn) {
  331. rc = -ENOMEM;
  332. goto out;
  333. }
  334. memcpy(new_conn->cc_name, group, grouplen);
  335. new_conn->cc_namelen = grouplen;
  336. new_conn->cc_recovery_handler = recovery_handler;
  337. new_conn->cc_recovery_data = recovery_data;
  338. /* Start the new connection at our maximum compatibility level */
  339. new_conn->cc_version = lproto->lp_max_version;
  340. rc = o2cb_cluster_connect(new_conn);
  341. if (rc) {
  342. mlog_errno(rc);
  343. goto out_free;
  344. }
  345. *conn = new_conn;
  346. out_free:
  347. if (rc)
  348. kfree(new_conn);
  349. out:
  350. return rc;
  351. }
  352. static int o2cb_cluster_disconnect(struct ocfs2_cluster_connection *conn)
  353. {
  354. struct dlm_ctxt *dlm = conn->cc_lockspace;
  355. struct o2dlm_private *priv = conn->cc_private;
  356. dlm_unregister_eviction_cb(&priv->op_eviction_cb);
  357. conn->cc_private = NULL;
  358. kfree(priv);
  359. dlm_unregister_domain(dlm);
  360. conn->cc_lockspace = NULL;
  361. return 0;
  362. }
  363. int ocfs2_cluster_disconnect(struct ocfs2_cluster_connection *conn)
  364. {
  365. int ret;
  366. BUG_ON(conn == NULL);
  367. ret = o2cb_cluster_disconnect(conn);
  368. /* XXX Should we free it anyway? */
  369. if (!ret)
  370. kfree(conn);
  371. return ret;
  372. }
  373. static void o2hb_stop(const char *group)
  374. {
  375. int ret;
  376. char *argv[5], *envp[3];
  377. argv[0] = (char *)o2nm_get_hb_ctl_path();
  378. argv[1] = "-K";
  379. argv[2] = "-u";
  380. argv[3] = (char *)group;
  381. argv[4] = NULL;
  382. mlog(0, "Run: %s %s %s %s\n", argv[0], argv[1], argv[2], argv[3]);
  383. /* minimal command environment taken from cpu_run_sbin_hotplug */
  384. envp[0] = "HOME=/";
  385. envp[1] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
  386. envp[2] = NULL;
  387. ret = call_usermodehelper(argv[0], argv, envp, UMH_WAIT_PROC);
  388. if (ret < 0)
  389. mlog_errno(ret);
  390. }
  391. /*
  392. * Hangup is a hack for tools compatibility. Older ocfs2-tools software
  393. * expects the filesystem to call "ocfs2_hb_ctl" during unmount. This
  394. * happens regardless of whether the DLM got started, so we can't do it
  395. * in ocfs2_cluster_disconnect(). We bring the o2hb_stop() function into
  396. * the glue and provide a "hangup" API for super.c to call.
  397. *
  398. * Other stacks will eventually provide a NULL ->hangup() pointer.
  399. */
  400. static void o2cb_cluster_hangup(const char *group, int grouplen)
  401. {
  402. o2hb_stop(group);
  403. }
  404. void ocfs2_cluster_hangup(const char *group, int grouplen)
  405. {
  406. BUG_ON(group == NULL);
  407. BUG_ON(group[grouplen] != '\0');
  408. o2cb_cluster_hangup(group, grouplen);
  409. }
  410. static int o2cb_cluster_this_node(unsigned int *node)
  411. {
  412. int node_num;
  413. node_num = o2nm_this_node();
  414. if (node_num == O2NM_INVALID_NODE_NUM)
  415. return -ENOENT;
  416. if (node_num >= O2NM_MAX_NODES)
  417. return -EOVERFLOW;
  418. *node = node_num;
  419. return 0;
  420. }
  421. int ocfs2_cluster_this_node(unsigned int *node)
  422. {
  423. return o2cb_cluster_this_node(node);
  424. }
  425. void ocfs2_stack_glue_set_locking_protocol(struct ocfs2_locking_protocol *proto)
  426. {
  427. BUG_ON(proto != NULL);
  428. lproto = proto;
  429. }