heartbeat.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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. if (ocfs2_mount_local(osb))
  130. return 0;
  131. status = o2hb_register_callback(&osb->osb_hb_down);
  132. if (status < 0) {
  133. mlog_errno(status);
  134. goto bail;
  135. }
  136. status = o2hb_register_callback(&osb->osb_hb_up);
  137. if (status < 0)
  138. mlog_errno(status);
  139. bail:
  140. return status;
  141. }
  142. void ocfs2_clear_hb_callbacks(struct ocfs2_super *osb)
  143. {
  144. int status;
  145. if (ocfs2_mount_local(osb))
  146. return;
  147. status = o2hb_unregister_callback(&osb->osb_hb_down);
  148. if (status < 0)
  149. mlog_errno(status);
  150. status = o2hb_unregister_callback(&osb->osb_hb_up);
  151. if (status < 0)
  152. mlog_errno(status);
  153. }
  154. void ocfs2_stop_heartbeat(struct ocfs2_super *osb)
  155. {
  156. int ret;
  157. char *argv[5], *envp[3];
  158. if (ocfs2_mount_local(osb))
  159. return;
  160. if (!osb->uuid_str) {
  161. /* This can happen if we don't get far enough in mount... */
  162. mlog(0, "No UUID with which to stop heartbeat!\n\n");
  163. return;
  164. }
  165. argv[0] = (char *)o2nm_get_hb_ctl_path();
  166. argv[1] = "-K";
  167. argv[2] = "-u";
  168. argv[3] = osb->uuid_str;
  169. argv[4] = NULL;
  170. mlog(0, "Run: %s %s %s %s\n", argv[0], argv[1], argv[2], argv[3]);
  171. /* minimal command environment taken from cpu_run_sbin_hotplug */
  172. envp[0] = "HOME=/";
  173. envp[1] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
  174. envp[2] = NULL;
  175. ret = call_usermodehelper(argv[0], argv, envp, 1);
  176. if (ret < 0)
  177. mlog_errno(ret);
  178. }
  179. /* special case -1 for now
  180. * TODO: should *really* make sure the calling func never passes -1!! */
  181. void ocfs2_node_map_init(struct ocfs2_node_map *map)
  182. {
  183. map->num_nodes = OCFS2_NODE_MAP_MAX_NODES;
  184. memset(map->map, 0, BITS_TO_LONGS(OCFS2_NODE_MAP_MAX_NODES) *
  185. sizeof(unsigned long));
  186. }
  187. static inline void __ocfs2_node_map_set_bit(struct ocfs2_node_map *map,
  188. int bit)
  189. {
  190. set_bit(bit, map->map);
  191. }
  192. void ocfs2_node_map_set_bit(struct ocfs2_super *osb,
  193. struct ocfs2_node_map *map,
  194. int bit)
  195. {
  196. if (bit==-1)
  197. return;
  198. BUG_ON(bit >= map->num_nodes);
  199. spin_lock(&osb->node_map_lock);
  200. __ocfs2_node_map_set_bit(map, bit);
  201. spin_unlock(&osb->node_map_lock);
  202. }
  203. static inline void __ocfs2_node_map_clear_bit(struct ocfs2_node_map *map,
  204. int bit)
  205. {
  206. clear_bit(bit, map->map);
  207. }
  208. void ocfs2_node_map_clear_bit(struct ocfs2_super *osb,
  209. struct ocfs2_node_map *map,
  210. int bit)
  211. {
  212. if (bit==-1)
  213. return;
  214. BUG_ON(bit >= map->num_nodes);
  215. spin_lock(&osb->node_map_lock);
  216. __ocfs2_node_map_clear_bit(map, bit);
  217. spin_unlock(&osb->node_map_lock);
  218. }
  219. int ocfs2_node_map_test_bit(struct ocfs2_super *osb,
  220. struct ocfs2_node_map *map,
  221. int bit)
  222. {
  223. int ret;
  224. if (bit >= map->num_nodes) {
  225. mlog(ML_ERROR, "bit=%d map->num_nodes=%d\n", bit, map->num_nodes);
  226. BUG();
  227. }
  228. spin_lock(&osb->node_map_lock);
  229. ret = test_bit(bit, map->map);
  230. spin_unlock(&osb->node_map_lock);
  231. return ret;
  232. }
  233. static inline int __ocfs2_node_map_is_empty(struct ocfs2_node_map *map)
  234. {
  235. int bit;
  236. bit = find_next_bit(map->map, map->num_nodes, 0);
  237. if (bit < map->num_nodes)
  238. return 0;
  239. return 1;
  240. }
  241. int ocfs2_node_map_is_empty(struct ocfs2_super *osb,
  242. struct ocfs2_node_map *map)
  243. {
  244. int ret;
  245. BUG_ON(map->num_nodes == 0);
  246. spin_lock(&osb->node_map_lock);
  247. ret = __ocfs2_node_map_is_empty(map);
  248. spin_unlock(&osb->node_map_lock);
  249. return ret;
  250. }
  251. static void __ocfs2_node_map_dup(struct ocfs2_node_map *target,
  252. struct ocfs2_node_map *from)
  253. {
  254. BUG_ON(from->num_nodes == 0);
  255. ocfs2_node_map_init(target);
  256. __ocfs2_node_map_set(target, from);
  257. }
  258. /* returns 1 if bit is the only bit set in target, 0 otherwise */
  259. int ocfs2_node_map_is_only(struct ocfs2_super *osb,
  260. struct ocfs2_node_map *target,
  261. int bit)
  262. {
  263. struct ocfs2_node_map temp;
  264. int ret;
  265. spin_lock(&osb->node_map_lock);
  266. __ocfs2_node_map_dup(&temp, target);
  267. __ocfs2_node_map_clear_bit(&temp, bit);
  268. ret = __ocfs2_node_map_is_empty(&temp);
  269. spin_unlock(&osb->node_map_lock);
  270. return ret;
  271. }
  272. static void __ocfs2_node_map_set(struct ocfs2_node_map *target,
  273. struct ocfs2_node_map *from)
  274. {
  275. int num_longs, i;
  276. BUG_ON(target->num_nodes != from->num_nodes);
  277. BUG_ON(target->num_nodes == 0);
  278. num_longs = BITS_TO_LONGS(target->num_nodes);
  279. for (i = 0; i < num_longs; i++)
  280. target->map[i] = from->map[i];
  281. }
  282. /* Returns whether the recovery bit was actually set - it may not be
  283. * if a node is still marked as needing recovery */
  284. int ocfs2_recovery_map_set(struct ocfs2_super *osb,
  285. int num)
  286. {
  287. int set = 0;
  288. spin_lock(&osb->node_map_lock);
  289. __ocfs2_node_map_clear_bit(&osb->mounted_map, num);
  290. if (!test_bit(num, osb->recovery_map.map)) {
  291. __ocfs2_node_map_set_bit(&osb->recovery_map, num);
  292. set = 1;
  293. }
  294. spin_unlock(&osb->node_map_lock);
  295. return set;
  296. }
  297. void ocfs2_recovery_map_clear(struct ocfs2_super *osb,
  298. int num)
  299. {
  300. ocfs2_node_map_clear_bit(osb, &osb->recovery_map, num);
  301. }
  302. int ocfs2_node_map_iterate(struct ocfs2_super *osb,
  303. struct ocfs2_node_map *map,
  304. int idx)
  305. {
  306. int i = idx;
  307. idx = O2NM_INVALID_NODE_NUM;
  308. spin_lock(&osb->node_map_lock);
  309. if ((i != O2NM_INVALID_NODE_NUM) &&
  310. (i >= 0) &&
  311. (i < map->num_nodes)) {
  312. while(i < map->num_nodes) {
  313. if (test_bit(i, map->map)) {
  314. idx = i;
  315. break;
  316. }
  317. i++;
  318. }
  319. }
  320. spin_unlock(&osb->node_map_lock);
  321. return idx;
  322. }