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