heartbeat.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. /* -*- mode: c; c-basic-offset: 8; -*-
  2. * vim: noexpandtab sw=8 ts=8 sts=0:
  3. *
  4. * heartbeat.c
  5. *
  6. * Register ourselves with the heartbaet service, keep our node maps
  7. * up to date, and fire off recovery when needed.
  8. *
  9. * Copyright (C) 2002, 2004 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; either
  14. * version 2 of the License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public
  22. * License along with this program; if not, write to the
  23. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  24. * Boston, MA 021110-1307, USA.
  25. */
  26. #include <linux/fs.h>
  27. #include <linux/types.h>
  28. #include <linux/slab.h>
  29. #include <linux/highmem.h>
  30. #include <linux/kmod.h>
  31. #include <cluster/heartbeat.h>
  32. #include <cluster/nodemanager.h>
  33. #include <dlm/dlmapi.h>
  34. #define MLOG_MASK_PREFIX ML_SUPER
  35. #include <cluster/masklog.h>
  36. #include "ocfs2.h"
  37. #include "alloc.h"
  38. #include "heartbeat.h"
  39. #include "inode.h"
  40. #include "journal.h"
  41. #include "vote.h"
  42. #include "buffer_head_io.h"
  43. #define OCFS2_HB_NODE_DOWN_PRI (0x0000002)
  44. #define OCFS2_HB_NODE_UP_PRI OCFS2_HB_NODE_DOWN_PRI
  45. static inline void __ocfs2_node_map_set_bit(struct ocfs2_node_map *map,
  46. int bit);
  47. static inline void __ocfs2_node_map_clear_bit(struct ocfs2_node_map *map,
  48. int bit);
  49. static inline int __ocfs2_node_map_is_empty(struct ocfs2_node_map *map);
  50. static void __ocfs2_node_map_dup(struct ocfs2_node_map *target,
  51. struct ocfs2_node_map *from);
  52. static void __ocfs2_node_map_set(struct ocfs2_node_map *target,
  53. struct ocfs2_node_map *from);
  54. void ocfs2_init_node_maps(struct ocfs2_super *osb)
  55. {
  56. spin_lock_init(&osb->node_map_lock);
  57. ocfs2_node_map_init(&osb->mounted_map);
  58. ocfs2_node_map_init(&osb->recovery_map);
  59. ocfs2_node_map_init(&osb->umount_map);
  60. }
  61. static void ocfs2_do_node_down(int node_num,
  62. struct ocfs2_super *osb)
  63. {
  64. BUG_ON(osb->node_num == node_num);
  65. mlog(0, "ocfs2: node down event for %d\n", node_num);
  66. if (!osb->dlm) {
  67. /*
  68. * No DLM means we're not even ready to participate yet.
  69. * We check the slots after the DLM comes up, so we will
  70. * notice the node death then. We can safely ignore it
  71. * here.
  72. */
  73. return;
  74. }
  75. if (ocfs2_node_map_test_bit(osb, &osb->umount_map, node_num)) {
  76. /* If a node is in the umount map, then we've been
  77. * expecting him to go down and we know ahead of time
  78. * that recovery is not necessary. */
  79. ocfs2_node_map_clear_bit(osb, &osb->umount_map, node_num);
  80. return;
  81. }
  82. ocfs2_recovery_thread(osb, node_num);
  83. ocfs2_remove_node_from_vote_queues(osb, node_num);
  84. }
  85. static void ocfs2_hb_node_down_cb(struct o2nm_node *node,
  86. int node_num,
  87. void *data)
  88. {
  89. ocfs2_do_node_down(node_num, (struct ocfs2_super *) data);
  90. }
  91. /* Called from the dlm when it's about to evict a node. We may also
  92. * get a heartbeat callback later. */
  93. static void ocfs2_dlm_eviction_cb(int node_num,
  94. void *data)
  95. {
  96. struct ocfs2_super *osb = (struct ocfs2_super *) data;
  97. struct super_block *sb = osb->sb;
  98. mlog(ML_NOTICE, "device (%u,%u): dlm has evicted node %d\n",
  99. MAJOR(sb->s_dev), MINOR(sb->s_dev), node_num);
  100. ocfs2_do_node_down(node_num, osb);
  101. }
  102. static void ocfs2_hb_node_up_cb(struct o2nm_node *node,
  103. int node_num,
  104. void *data)
  105. {
  106. struct ocfs2_super *osb = data;
  107. BUG_ON(osb->node_num == node_num);
  108. mlog(0, "node up event for %d\n", node_num);
  109. ocfs2_node_map_clear_bit(osb, &osb->umount_map, node_num);
  110. }
  111. void ocfs2_setup_hb_callbacks(struct ocfs2_super *osb)
  112. {
  113. o2hb_setup_callback(&osb->osb_hb_down, O2HB_NODE_DOWN_CB,
  114. ocfs2_hb_node_down_cb, osb,
  115. OCFS2_HB_NODE_DOWN_PRI);
  116. o2hb_setup_callback(&osb->osb_hb_up, O2HB_NODE_UP_CB,
  117. ocfs2_hb_node_up_cb, osb, OCFS2_HB_NODE_UP_PRI);
  118. /* Not exactly a heartbeat callback, but leads to essentially
  119. * the same path so we set it up here. */
  120. dlm_setup_eviction_cb(&osb->osb_eviction_cb,
  121. ocfs2_dlm_eviction_cb,
  122. osb);
  123. }
  124. /* Most functions here are just stubs for now... */
  125. int ocfs2_register_hb_callbacks(struct ocfs2_super *osb)
  126. {
  127. int status;
  128. status = o2hb_register_callback(&osb->osb_hb_down);
  129. if (status < 0) {
  130. mlog_errno(status);
  131. goto bail;
  132. }
  133. status = o2hb_register_callback(&osb->osb_hb_up);
  134. if (status < 0)
  135. mlog_errno(status);
  136. bail:
  137. return status;
  138. }
  139. void ocfs2_clear_hb_callbacks(struct ocfs2_super *osb)
  140. {
  141. int status;
  142. status = o2hb_unregister_callback(&osb->osb_hb_down);
  143. if (status < 0)
  144. mlog_errno(status);
  145. status = o2hb_unregister_callback(&osb->osb_hb_up);
  146. if (status < 0)
  147. mlog_errno(status);
  148. }
  149. void ocfs2_stop_heartbeat(struct ocfs2_super *osb)
  150. {
  151. int ret;
  152. char *argv[5], *envp[3];
  153. if (!osb->uuid_str) {
  154. /* This can happen if we don't get far enough in mount... */
  155. mlog(0, "No UUID with which to stop heartbeat!\n\n");
  156. return;
  157. }
  158. argv[0] = (char *)o2nm_get_hb_ctl_path();
  159. argv[1] = "-K";
  160. argv[2] = "-u";
  161. argv[3] = osb->uuid_str;
  162. argv[4] = NULL;
  163. mlog(0, "Run: %s %s %s %s\n", argv[0], argv[1], argv[2], argv[3]);
  164. /* minimal command environment taken from cpu_run_sbin_hotplug */
  165. envp[0] = "HOME=/";
  166. envp[1] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
  167. envp[2] = NULL;
  168. ret = call_usermodehelper(argv[0], argv, envp, 1);
  169. if (ret < 0)
  170. mlog_errno(ret);
  171. }
  172. /* special case -1 for now
  173. * TODO: should *really* make sure the calling func never passes -1!! */
  174. void ocfs2_node_map_init(struct ocfs2_node_map *map)
  175. {
  176. map->num_nodes = OCFS2_NODE_MAP_MAX_NODES;
  177. memset(map->map, 0, BITS_TO_LONGS(OCFS2_NODE_MAP_MAX_NODES) *
  178. sizeof(unsigned long));
  179. }
  180. static inline void __ocfs2_node_map_set_bit(struct ocfs2_node_map *map,
  181. int bit)
  182. {
  183. set_bit(bit, map->map);
  184. }
  185. void ocfs2_node_map_set_bit(struct ocfs2_super *osb,
  186. struct ocfs2_node_map *map,
  187. int bit)
  188. {
  189. if (bit==-1)
  190. return;
  191. BUG_ON(bit >= map->num_nodes);
  192. spin_lock(&osb->node_map_lock);
  193. __ocfs2_node_map_set_bit(map, bit);
  194. spin_unlock(&osb->node_map_lock);
  195. }
  196. static inline void __ocfs2_node_map_clear_bit(struct ocfs2_node_map *map,
  197. int bit)
  198. {
  199. clear_bit(bit, map->map);
  200. }
  201. void ocfs2_node_map_clear_bit(struct ocfs2_super *osb,
  202. struct ocfs2_node_map *map,
  203. int bit)
  204. {
  205. if (bit==-1)
  206. return;
  207. BUG_ON(bit >= map->num_nodes);
  208. spin_lock(&osb->node_map_lock);
  209. __ocfs2_node_map_clear_bit(map, bit);
  210. spin_unlock(&osb->node_map_lock);
  211. }
  212. int ocfs2_node_map_test_bit(struct ocfs2_super *osb,
  213. struct ocfs2_node_map *map,
  214. int bit)
  215. {
  216. int ret;
  217. if (bit >= map->num_nodes) {
  218. mlog(ML_ERROR, "bit=%d map->num_nodes=%d\n", bit, map->num_nodes);
  219. BUG();
  220. }
  221. spin_lock(&osb->node_map_lock);
  222. ret = test_bit(bit, map->map);
  223. spin_unlock(&osb->node_map_lock);
  224. return ret;
  225. }
  226. static inline int __ocfs2_node_map_is_empty(struct ocfs2_node_map *map)
  227. {
  228. int bit;
  229. bit = find_next_bit(map->map, map->num_nodes, 0);
  230. if (bit < map->num_nodes)
  231. return 0;
  232. return 1;
  233. }
  234. int ocfs2_node_map_is_empty(struct ocfs2_super *osb,
  235. struct ocfs2_node_map *map)
  236. {
  237. int ret;
  238. BUG_ON(map->num_nodes == 0);
  239. spin_lock(&osb->node_map_lock);
  240. ret = __ocfs2_node_map_is_empty(map);
  241. spin_unlock(&osb->node_map_lock);
  242. return ret;
  243. }
  244. static void __ocfs2_node_map_dup(struct ocfs2_node_map *target,
  245. struct ocfs2_node_map *from)
  246. {
  247. BUG_ON(from->num_nodes == 0);
  248. ocfs2_node_map_init(target);
  249. __ocfs2_node_map_set(target, from);
  250. }
  251. /* returns 1 if bit is the only bit set in target, 0 otherwise */
  252. int ocfs2_node_map_is_only(struct ocfs2_super *osb,
  253. struct ocfs2_node_map *target,
  254. int bit)
  255. {
  256. struct ocfs2_node_map temp;
  257. int ret;
  258. spin_lock(&osb->node_map_lock);
  259. __ocfs2_node_map_dup(&temp, target);
  260. __ocfs2_node_map_clear_bit(&temp, bit);
  261. ret = __ocfs2_node_map_is_empty(&temp);
  262. spin_unlock(&osb->node_map_lock);
  263. return ret;
  264. }
  265. static void __ocfs2_node_map_set(struct ocfs2_node_map *target,
  266. struct ocfs2_node_map *from)
  267. {
  268. int num_longs, i;
  269. BUG_ON(target->num_nodes != from->num_nodes);
  270. BUG_ON(target->num_nodes == 0);
  271. num_longs = BITS_TO_LONGS(target->num_nodes);
  272. for (i = 0; i < num_longs; i++)
  273. target->map[i] = from->map[i];
  274. }
  275. /* Returns whether the recovery bit was actually set - it may not be
  276. * if a node is still marked as needing recovery */
  277. int ocfs2_recovery_map_set(struct ocfs2_super *osb,
  278. int num)
  279. {
  280. int set = 0;
  281. spin_lock(&osb->node_map_lock);
  282. __ocfs2_node_map_clear_bit(&osb->mounted_map, num);
  283. if (!test_bit(num, osb->recovery_map.map)) {
  284. __ocfs2_node_map_set_bit(&osb->recovery_map, num);
  285. set = 1;
  286. }
  287. spin_unlock(&osb->node_map_lock);
  288. return set;
  289. }
  290. void ocfs2_recovery_map_clear(struct ocfs2_super *osb,
  291. int num)
  292. {
  293. ocfs2_node_map_clear_bit(osb, &osb->recovery_map, num);
  294. }
  295. int ocfs2_node_map_iterate(struct ocfs2_super *osb,
  296. struct ocfs2_node_map *map,
  297. int idx)
  298. {
  299. int i = idx;
  300. idx = O2NM_INVALID_NODE_NUM;
  301. spin_lock(&osb->node_map_lock);
  302. if ((i != O2NM_INVALID_NODE_NUM) &&
  303. (i >= 0) &&
  304. (i < map->num_nodes)) {
  305. while(i < map->num_nodes) {
  306. if (test_bit(i, map->map)) {
  307. idx = i;
  308. break;
  309. }
  310. i++;
  311. }
  312. }
  313. spin_unlock(&osb->node_map_lock);
  314. return idx;
  315. }