heartbeat.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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 <dlm/dlmapi.h>
  32. #define MLOG_MASK_PREFIX ML_SUPER
  33. #include <cluster/masklog.h>
  34. #include "ocfs2.h"
  35. #include "alloc.h"
  36. #include "heartbeat.h"
  37. #include "inode.h"
  38. #include "journal.h"
  39. #include "buffer_head_io.h"
  40. static inline void __ocfs2_node_map_set_bit(struct ocfs2_node_map *map,
  41. int bit);
  42. static inline void __ocfs2_node_map_clear_bit(struct ocfs2_node_map *map,
  43. int bit);
  44. static inline int __ocfs2_node_map_is_empty(struct ocfs2_node_map *map);
  45. /* special case -1 for now
  46. * TODO: should *really* make sure the calling func never passes -1!! */
  47. static void ocfs2_node_map_init(struct ocfs2_node_map *map)
  48. {
  49. map->num_nodes = OCFS2_NODE_MAP_MAX_NODES;
  50. memset(map->map, 0, BITS_TO_LONGS(OCFS2_NODE_MAP_MAX_NODES) *
  51. sizeof(unsigned long));
  52. }
  53. void ocfs2_init_node_maps(struct ocfs2_super *osb)
  54. {
  55. spin_lock_init(&osb->node_map_lock);
  56. ocfs2_node_map_init(&osb->recovery_map);
  57. ocfs2_node_map_init(&osb->osb_recovering_orphan_dirs);
  58. }
  59. static void ocfs2_do_node_down(int node_num,
  60. struct ocfs2_super *osb)
  61. {
  62. BUG_ON(osb->node_num == node_num);
  63. mlog(0, "ocfs2: node down event for %d\n", node_num);
  64. if (!osb->dlm) {
  65. /*
  66. * No DLM means we're not even ready to participate yet.
  67. * We check the slots after the DLM comes up, so we will
  68. * notice the node death then. We can safely ignore it
  69. * here.
  70. */
  71. return;
  72. }
  73. ocfs2_recovery_thread(osb, node_num);
  74. }
  75. /* Called from the dlm when it's about to evict a node. We may also
  76. * get a heartbeat callback later. */
  77. static void ocfs2_dlm_eviction_cb(int node_num,
  78. void *data)
  79. {
  80. struct ocfs2_super *osb = (struct ocfs2_super *) data;
  81. struct super_block *sb = osb->sb;
  82. mlog(ML_NOTICE, "device (%u,%u): dlm has evicted node %d\n",
  83. MAJOR(sb->s_dev), MINOR(sb->s_dev), node_num);
  84. ocfs2_do_node_down(node_num, osb);
  85. }
  86. void ocfs2_setup_hb_callbacks(struct ocfs2_super *osb)
  87. {
  88. /* Not exactly a heartbeat callback, but leads to essentially
  89. * the same path so we set it up here. */
  90. dlm_setup_eviction_cb(&osb->osb_eviction_cb,
  91. ocfs2_dlm_eviction_cb,
  92. osb);
  93. }
  94. void ocfs2_stop_heartbeat(struct ocfs2_super *osb)
  95. {
  96. int ret;
  97. char *argv[5], *envp[3];
  98. if (ocfs2_mount_local(osb))
  99. return;
  100. if (!osb->uuid_str) {
  101. /* This can happen if we don't get far enough in mount... */
  102. mlog(0, "No UUID with which to stop heartbeat!\n\n");
  103. return;
  104. }
  105. argv[0] = (char *)o2nm_get_hb_ctl_path();
  106. argv[1] = "-K";
  107. argv[2] = "-u";
  108. argv[3] = osb->uuid_str;
  109. argv[4] = NULL;
  110. mlog(0, "Run: %s %s %s %s\n", argv[0], argv[1], argv[2], argv[3]);
  111. /* minimal command environment taken from cpu_run_sbin_hotplug */
  112. envp[0] = "HOME=/";
  113. envp[1] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
  114. envp[2] = NULL;
  115. ret = call_usermodehelper(argv[0], argv, envp, UMH_WAIT_PROC);
  116. if (ret < 0)
  117. mlog_errno(ret);
  118. }
  119. static inline void __ocfs2_node_map_set_bit(struct ocfs2_node_map *map,
  120. int bit)
  121. {
  122. set_bit(bit, map->map);
  123. }
  124. void ocfs2_node_map_set_bit(struct ocfs2_super *osb,
  125. struct ocfs2_node_map *map,
  126. int bit)
  127. {
  128. if (bit==-1)
  129. return;
  130. BUG_ON(bit >= map->num_nodes);
  131. spin_lock(&osb->node_map_lock);
  132. __ocfs2_node_map_set_bit(map, bit);
  133. spin_unlock(&osb->node_map_lock);
  134. }
  135. static inline void __ocfs2_node_map_clear_bit(struct ocfs2_node_map *map,
  136. int bit)
  137. {
  138. clear_bit(bit, map->map);
  139. }
  140. void ocfs2_node_map_clear_bit(struct ocfs2_super *osb,
  141. struct ocfs2_node_map *map,
  142. int bit)
  143. {
  144. if (bit==-1)
  145. return;
  146. BUG_ON(bit >= map->num_nodes);
  147. spin_lock(&osb->node_map_lock);
  148. __ocfs2_node_map_clear_bit(map, bit);
  149. spin_unlock(&osb->node_map_lock);
  150. }
  151. int ocfs2_node_map_test_bit(struct ocfs2_super *osb,
  152. struct ocfs2_node_map *map,
  153. int bit)
  154. {
  155. int ret;
  156. if (bit >= map->num_nodes) {
  157. mlog(ML_ERROR, "bit=%d map->num_nodes=%d\n", bit, map->num_nodes);
  158. BUG();
  159. }
  160. spin_lock(&osb->node_map_lock);
  161. ret = test_bit(bit, map->map);
  162. spin_unlock(&osb->node_map_lock);
  163. return ret;
  164. }
  165. static inline int __ocfs2_node_map_is_empty(struct ocfs2_node_map *map)
  166. {
  167. int bit;
  168. bit = find_next_bit(map->map, map->num_nodes, 0);
  169. if (bit < map->num_nodes)
  170. return 0;
  171. return 1;
  172. }
  173. int ocfs2_node_map_is_empty(struct ocfs2_super *osb,
  174. struct ocfs2_node_map *map)
  175. {
  176. int ret;
  177. BUG_ON(map->num_nodes == 0);
  178. spin_lock(&osb->node_map_lock);
  179. ret = __ocfs2_node_map_is_empty(map);
  180. spin_unlock(&osb->node_map_lock);
  181. return ret;
  182. }
  183. #if 0
  184. static void __ocfs2_node_map_dup(struct ocfs2_node_map *target,
  185. struct ocfs2_node_map *from)
  186. {
  187. BUG_ON(from->num_nodes == 0);
  188. ocfs2_node_map_init(target);
  189. __ocfs2_node_map_set(target, from);
  190. }
  191. /* returns 1 if bit is the only bit set in target, 0 otherwise */
  192. int ocfs2_node_map_is_only(struct ocfs2_super *osb,
  193. struct ocfs2_node_map *target,
  194. int bit)
  195. {
  196. struct ocfs2_node_map temp;
  197. int ret;
  198. spin_lock(&osb->node_map_lock);
  199. __ocfs2_node_map_dup(&temp, target);
  200. __ocfs2_node_map_clear_bit(&temp, bit);
  201. ret = __ocfs2_node_map_is_empty(&temp);
  202. spin_unlock(&osb->node_map_lock);
  203. return ret;
  204. }
  205. static void __ocfs2_node_map_set(struct ocfs2_node_map *target,
  206. struct ocfs2_node_map *from)
  207. {
  208. int num_longs, i;
  209. BUG_ON(target->num_nodes != from->num_nodes);
  210. BUG_ON(target->num_nodes == 0);
  211. num_longs = BITS_TO_LONGS(target->num_nodes);
  212. for (i = 0; i < num_longs; i++)
  213. target->map[i] = from->map[i];
  214. }
  215. #endif /* 0 */
  216. /* Returns whether the recovery bit was actually set - it may not be
  217. * if a node is still marked as needing recovery */
  218. int ocfs2_recovery_map_set(struct ocfs2_super *osb,
  219. int num)
  220. {
  221. int set = 0;
  222. spin_lock(&osb->node_map_lock);
  223. if (!test_bit(num, osb->recovery_map.map)) {
  224. __ocfs2_node_map_set_bit(&osb->recovery_map, num);
  225. set = 1;
  226. }
  227. spin_unlock(&osb->node_map_lock);
  228. return set;
  229. }
  230. void ocfs2_recovery_map_clear(struct ocfs2_super *osb,
  231. int num)
  232. {
  233. ocfs2_node_map_clear_bit(osb, &osb->recovery_map, num);
  234. }
  235. int ocfs2_node_map_iterate(struct ocfs2_super *osb,
  236. struct ocfs2_node_map *map,
  237. int idx)
  238. {
  239. int i = idx;
  240. idx = O2NM_INVALID_NODE_NUM;
  241. spin_lock(&osb->node_map_lock);
  242. if ((i != O2NM_INVALID_NODE_NUM) &&
  243. (i >= 0) &&
  244. (i < map->num_nodes)) {
  245. while(i < map->num_nodes) {
  246. if (test_bit(i, map->map)) {
  247. idx = i;
  248. break;
  249. }
  250. i++;
  251. }
  252. }
  253. spin_unlock(&osb->node_map_lock);
  254. return idx;
  255. }