heartbeat.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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. ocfs2_node_map_init(&osb->osb_recovering_orphan_dirs);
  61. }
  62. static void ocfs2_do_node_down(int node_num,
  63. struct ocfs2_super *osb)
  64. {
  65. BUG_ON(osb->node_num == node_num);
  66. mlog(0, "ocfs2: node down event for %d\n", node_num);
  67. if (!osb->dlm) {
  68. /*
  69. * No DLM means we're not even ready to participate yet.
  70. * We check the slots after the DLM comes up, so we will
  71. * notice the node death then. We can safely ignore it
  72. * here.
  73. */
  74. return;
  75. }
  76. if (ocfs2_node_map_test_bit(osb, &osb->umount_map, node_num)) {
  77. /* If a node is in the umount map, then we've been
  78. * expecting him to go down and we know ahead of time
  79. * that recovery is not necessary. */
  80. ocfs2_node_map_clear_bit(osb, &osb->umount_map, node_num);
  81. return;
  82. }
  83. ocfs2_recovery_thread(osb, node_num);
  84. ocfs2_remove_node_from_vote_queues(osb, node_num);
  85. }
  86. static void ocfs2_hb_node_down_cb(struct o2nm_node *node,
  87. int node_num,
  88. void *data)
  89. {
  90. ocfs2_do_node_down(node_num, (struct ocfs2_super *) data);
  91. }
  92. /* Called from the dlm when it's about to evict a node. We may also
  93. * get a heartbeat callback later. */
  94. static void ocfs2_dlm_eviction_cb(int node_num,
  95. void *data)
  96. {
  97. struct ocfs2_super *osb = (struct ocfs2_super *) data;
  98. struct super_block *sb = osb->sb;
  99. mlog(ML_NOTICE, "device (%u,%u): dlm has evicted node %d\n",
  100. MAJOR(sb->s_dev), MINOR(sb->s_dev), node_num);
  101. ocfs2_do_node_down(node_num, osb);
  102. }
  103. static void ocfs2_hb_node_up_cb(struct o2nm_node *node,
  104. int node_num,
  105. void *data)
  106. {
  107. struct ocfs2_super *osb = data;
  108. BUG_ON(osb->node_num == node_num);
  109. mlog(0, "node up event for %d\n", node_num);
  110. ocfs2_node_map_clear_bit(osb, &osb->umount_map, node_num);
  111. }
  112. void ocfs2_setup_hb_callbacks(struct ocfs2_super *osb)
  113. {
  114. o2hb_setup_callback(&osb->osb_hb_down, O2HB_NODE_DOWN_CB,
  115. ocfs2_hb_node_down_cb, osb,
  116. OCFS2_HB_NODE_DOWN_PRI);
  117. o2hb_setup_callback(&osb->osb_hb_up, O2HB_NODE_UP_CB,
  118. ocfs2_hb_node_up_cb, osb, OCFS2_HB_NODE_UP_PRI);
  119. /* Not exactly a heartbeat callback, but leads to essentially
  120. * the same path so we set it up here. */
  121. dlm_setup_eviction_cb(&osb->osb_eviction_cb,
  122. ocfs2_dlm_eviction_cb,
  123. osb);
  124. }
  125. /* Most functions here are just stubs for now... */
  126. int ocfs2_register_hb_callbacks(struct ocfs2_super *osb)
  127. {
  128. int status;
  129. status = o2hb_register_callback(&osb->osb_hb_down);
  130. if (status < 0) {
  131. mlog_errno(status);
  132. goto bail;
  133. }
  134. status = o2hb_register_callback(&osb->osb_hb_up);
  135. if (status < 0)
  136. mlog_errno(status);
  137. bail:
  138. return status;
  139. }
  140. void ocfs2_clear_hb_callbacks(struct ocfs2_super *osb)
  141. {
  142. int status;
  143. status = o2hb_unregister_callback(&osb->osb_hb_down);
  144. if (status < 0)
  145. mlog_errno(status);
  146. status = o2hb_unregister_callback(&osb->osb_hb_up);
  147. if (status < 0)
  148. mlog_errno(status);
  149. }
  150. void ocfs2_stop_heartbeat(struct ocfs2_super *osb)
  151. {
  152. int ret;
  153. char *argv[5], *envp[3];
  154. if (!osb->uuid_str) {
  155. /* This can happen if we don't get far enough in mount... */
  156. mlog(0, "No UUID with which to stop heartbeat!\n\n");
  157. return;
  158. }
  159. argv[0] = (char *)o2nm_get_hb_ctl_path();
  160. argv[1] = "-K";
  161. argv[2] = "-u";
  162. argv[3] = osb->uuid_str;
  163. argv[4] = NULL;
  164. mlog(0, "Run: %s %s %s %s\n", argv[0], argv[1], argv[2], argv[3]);
  165. /* minimal command environment taken from cpu_run_sbin_hotplug */
  166. envp[0] = "HOME=/";
  167. envp[1] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
  168. envp[2] = NULL;
  169. ret = call_usermodehelper(argv[0], argv, envp, 1);
  170. if (ret < 0)
  171. mlog_errno(ret);
  172. }
  173. /* special case -1 for now
  174. * TODO: should *really* make sure the calling func never passes -1!! */
  175. void ocfs2_node_map_init(struct ocfs2_node_map *map)
  176. {
  177. map->num_nodes = OCFS2_NODE_MAP_MAX_NODES;
  178. memset(map->map, 0, BITS_TO_LONGS(OCFS2_NODE_MAP_MAX_NODES) *
  179. sizeof(unsigned long));
  180. }
  181. static inline void __ocfs2_node_map_set_bit(struct ocfs2_node_map *map,
  182. int bit)
  183. {
  184. set_bit(bit, map->map);
  185. }
  186. void ocfs2_node_map_set_bit(struct ocfs2_super *osb,
  187. struct ocfs2_node_map *map,
  188. int bit)
  189. {
  190. if (bit==-1)
  191. return;
  192. BUG_ON(bit >= map->num_nodes);
  193. spin_lock(&osb->node_map_lock);
  194. __ocfs2_node_map_set_bit(map, bit);
  195. spin_unlock(&osb->node_map_lock);
  196. }
  197. static inline void __ocfs2_node_map_clear_bit(struct ocfs2_node_map *map,
  198. int bit)
  199. {
  200. clear_bit(bit, map->map);
  201. }
  202. void ocfs2_node_map_clear_bit(struct ocfs2_super *osb,
  203. struct ocfs2_node_map *map,
  204. int bit)
  205. {
  206. if (bit==-1)
  207. return;
  208. BUG_ON(bit >= map->num_nodes);
  209. spin_lock(&osb->node_map_lock);
  210. __ocfs2_node_map_clear_bit(map, bit);
  211. spin_unlock(&osb->node_map_lock);
  212. }
  213. int ocfs2_node_map_test_bit(struct ocfs2_super *osb,
  214. struct ocfs2_node_map *map,
  215. int bit)
  216. {
  217. int ret;
  218. if (bit >= map->num_nodes) {
  219. mlog(ML_ERROR, "bit=%d map->num_nodes=%d\n", bit, map->num_nodes);
  220. BUG();
  221. }
  222. spin_lock(&osb->node_map_lock);
  223. ret = test_bit(bit, map->map);
  224. spin_unlock(&osb->node_map_lock);
  225. return ret;
  226. }
  227. static inline int __ocfs2_node_map_is_empty(struct ocfs2_node_map *map)
  228. {
  229. int bit;
  230. bit = find_next_bit(map->map, map->num_nodes, 0);
  231. if (bit < map->num_nodes)
  232. return 0;
  233. return 1;
  234. }
  235. int ocfs2_node_map_is_empty(struct ocfs2_super *osb,
  236. struct ocfs2_node_map *map)
  237. {
  238. int ret;
  239. BUG_ON(map->num_nodes == 0);
  240. spin_lock(&osb->node_map_lock);
  241. ret = __ocfs2_node_map_is_empty(map);
  242. spin_unlock(&osb->node_map_lock);
  243. return ret;
  244. }
  245. static void __ocfs2_node_map_dup(struct ocfs2_node_map *target,
  246. struct ocfs2_node_map *from)
  247. {
  248. BUG_ON(from->num_nodes == 0);
  249. ocfs2_node_map_init(target);
  250. __ocfs2_node_map_set(target, from);
  251. }
  252. /* returns 1 if bit is the only bit set in target, 0 otherwise */
  253. int ocfs2_node_map_is_only(struct ocfs2_super *osb,
  254. struct ocfs2_node_map *target,
  255. int bit)
  256. {
  257. struct ocfs2_node_map temp;
  258. int ret;
  259. spin_lock(&osb->node_map_lock);
  260. __ocfs2_node_map_dup(&temp, target);
  261. __ocfs2_node_map_clear_bit(&temp, bit);
  262. ret = __ocfs2_node_map_is_empty(&temp);
  263. spin_unlock(&osb->node_map_lock);
  264. return ret;
  265. }
  266. static void __ocfs2_node_map_set(struct ocfs2_node_map *target,
  267. struct ocfs2_node_map *from)
  268. {
  269. int num_longs, i;
  270. BUG_ON(target->num_nodes != from->num_nodes);
  271. BUG_ON(target->num_nodes == 0);
  272. num_longs = BITS_TO_LONGS(target->num_nodes);
  273. for (i = 0; i < num_longs; i++)
  274. target->map[i] = from->map[i];
  275. }
  276. /* Returns whether the recovery bit was actually set - it may not be
  277. * if a node is still marked as needing recovery */
  278. int ocfs2_recovery_map_set(struct ocfs2_super *osb,
  279. int num)
  280. {
  281. int set = 0;
  282. spin_lock(&osb->node_map_lock);
  283. __ocfs2_node_map_clear_bit(&osb->mounted_map, num);
  284. if (!test_bit(num, osb->recovery_map.map)) {
  285. __ocfs2_node_map_set_bit(&osb->recovery_map, num);
  286. set = 1;
  287. }
  288. spin_unlock(&osb->node_map_lock);
  289. return set;
  290. }
  291. void ocfs2_recovery_map_clear(struct ocfs2_super *osb,
  292. int num)
  293. {
  294. ocfs2_node_map_clear_bit(osb, &osb->recovery_map, num);
  295. }
  296. int ocfs2_node_map_iterate(struct ocfs2_super *osb,
  297. struct ocfs2_node_map *map,
  298. int idx)
  299. {
  300. int i = idx;
  301. idx = O2NM_INVALID_NODE_NUM;
  302. spin_lock(&osb->node_map_lock);
  303. if ((i != O2NM_INVALID_NODE_NUM) &&
  304. (i >= 0) &&
  305. (i < map->num_nodes)) {
  306. while(i < map->num_nodes) {
  307. if (test_bit(i, map->map)) {
  308. idx = i;
  309. break;
  310. }
  311. i++;
  312. }
  313. }
  314. spin_unlock(&osb->node_map_lock);
  315. return idx;
  316. }