heartbeat.c 9.5 KB

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