heartbeat.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. /* special case -1 for now
  45. * TODO: should *really* make sure the calling func never passes -1!! */
  46. static void ocfs2_node_map_init(struct ocfs2_node_map *map)
  47. {
  48. map->num_nodes = OCFS2_NODE_MAP_MAX_NODES;
  49. memset(map->map, 0, BITS_TO_LONGS(OCFS2_NODE_MAP_MAX_NODES) *
  50. sizeof(unsigned long));
  51. }
  52. void ocfs2_init_node_maps(struct ocfs2_super *osb)
  53. {
  54. spin_lock_init(&osb->node_map_lock);
  55. ocfs2_node_map_init(&osb->osb_recovering_orphan_dirs);
  56. }
  57. static void ocfs2_do_node_down(int node_num,
  58. struct ocfs2_super *osb)
  59. {
  60. BUG_ON(osb->node_num == node_num);
  61. mlog(0, "ocfs2: node down event for %d\n", node_num);
  62. if (!osb->dlm) {
  63. /*
  64. * No DLM means we're not even ready to participate yet.
  65. * We check the slots after the DLM comes up, so we will
  66. * notice the node death then. We can safely ignore it
  67. * here.
  68. */
  69. return;
  70. }
  71. ocfs2_recovery_thread(osb, node_num);
  72. }
  73. /* Called from the dlm when it's about to evict a node. We may also
  74. * get a heartbeat callback later. */
  75. static void ocfs2_dlm_eviction_cb(int node_num,
  76. void *data)
  77. {
  78. struct ocfs2_super *osb = (struct ocfs2_super *) data;
  79. struct super_block *sb = osb->sb;
  80. mlog(ML_NOTICE, "device (%u,%u): dlm has evicted node %d\n",
  81. MAJOR(sb->s_dev), MINOR(sb->s_dev), node_num);
  82. ocfs2_do_node_down(node_num, osb);
  83. }
  84. void ocfs2_setup_hb_callbacks(struct ocfs2_super *osb)
  85. {
  86. /* Not exactly a heartbeat callback, but leads to essentially
  87. * the same path so we set it up here. */
  88. dlm_setup_eviction_cb(&osb->osb_eviction_cb,
  89. ocfs2_dlm_eviction_cb,
  90. osb);
  91. }
  92. void ocfs2_stop_heartbeat(struct ocfs2_super *osb)
  93. {
  94. int ret;
  95. char *argv[5], *envp[3];
  96. if (ocfs2_mount_local(osb))
  97. return;
  98. if (!osb->uuid_str) {
  99. /* This can happen if we don't get far enough in mount... */
  100. mlog(0, "No UUID with which to stop heartbeat!\n\n");
  101. return;
  102. }
  103. argv[0] = (char *)o2nm_get_hb_ctl_path();
  104. argv[1] = "-K";
  105. argv[2] = "-u";
  106. argv[3] = osb->uuid_str;
  107. argv[4] = NULL;
  108. mlog(0, "Run: %s %s %s %s\n", argv[0], argv[1], argv[2], argv[3]);
  109. /* minimal command environment taken from cpu_run_sbin_hotplug */
  110. envp[0] = "HOME=/";
  111. envp[1] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
  112. envp[2] = NULL;
  113. ret = call_usermodehelper(argv[0], argv, envp, UMH_WAIT_PROC);
  114. if (ret < 0)
  115. mlog_errno(ret);
  116. }
  117. static inline void __ocfs2_node_map_set_bit(struct ocfs2_node_map *map,
  118. int bit)
  119. {
  120. set_bit(bit, map->map);
  121. }
  122. void ocfs2_node_map_set_bit(struct ocfs2_super *osb,
  123. struct ocfs2_node_map *map,
  124. int bit)
  125. {
  126. if (bit==-1)
  127. return;
  128. BUG_ON(bit >= map->num_nodes);
  129. spin_lock(&osb->node_map_lock);
  130. __ocfs2_node_map_set_bit(map, bit);
  131. spin_unlock(&osb->node_map_lock);
  132. }
  133. static inline void __ocfs2_node_map_clear_bit(struct ocfs2_node_map *map,
  134. int bit)
  135. {
  136. clear_bit(bit, map->map);
  137. }
  138. void ocfs2_node_map_clear_bit(struct ocfs2_super *osb,
  139. struct ocfs2_node_map *map,
  140. int bit)
  141. {
  142. if (bit==-1)
  143. return;
  144. BUG_ON(bit >= map->num_nodes);
  145. spin_lock(&osb->node_map_lock);
  146. __ocfs2_node_map_clear_bit(map, bit);
  147. spin_unlock(&osb->node_map_lock);
  148. }
  149. int ocfs2_node_map_test_bit(struct ocfs2_super *osb,
  150. struct ocfs2_node_map *map,
  151. int bit)
  152. {
  153. int ret;
  154. if (bit >= map->num_nodes) {
  155. mlog(ML_ERROR, "bit=%d map->num_nodes=%d\n", bit, map->num_nodes);
  156. BUG();
  157. }
  158. spin_lock(&osb->node_map_lock);
  159. ret = test_bit(bit, map->map);
  160. spin_unlock(&osb->node_map_lock);
  161. return ret;
  162. }