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 27 #include <linux/fs.h> 28 #include <linux/types.h> 29 #include <linux/slab.h> 30 #include <linux/highmem.h> 31 #include <linux/kmod.h> 32 33 #include <cluster/heartbeat.h> 34 #include <cluster/nodemanager.h> 35 36 #include <dlm/dlmapi.h> 37 38 #define MLOG_MASK_PREFIX ML_SUPER 39 #include <cluster/masklog.h> 40 41 #include "ocfs2.h" 42 43 #include "alloc.h" 44 #include "heartbeat.h" 45 #include "inode.h" 46 #include "journal.h" 47 #include "vote.h" 48 49 #include "buffer_head_io.h" 50 51 #define OCFS2_HB_NODE_DOWN_PRI (0x0000002) 52 #define OCFS2_HB_NODE_UP_PRI OCFS2_HB_NODE_DOWN_PRI 53 54 static inline void __ocfs2_node_map_set_bit(struct ocfs2_node_map *map, 55 int bit); 56 static inline void __ocfs2_node_map_clear_bit(struct ocfs2_node_map *map, 57 int bit); 58 static inline int __ocfs2_node_map_is_empty(struct ocfs2_node_map *map); 59 static void __ocfs2_node_map_dup(struct ocfs2_node_map *target, 60 struct ocfs2_node_map *from); 61 static void __ocfs2_node_map_set(struct ocfs2_node_map *target, 62 struct ocfs2_node_map *from); 63 64 void ocfs2_init_node_maps(struct ocfs2_super *osb) 65 { 66 spin_lock_init(&osb->node_map_lock); 67 ocfs2_node_map_init(&osb->mounted_map); 68 ocfs2_node_map_init(&osb->recovery_map); 69 ocfs2_node_map_init(&osb->umount_map); 70 ocfs2_node_map_init(&osb->osb_recovering_orphan_dirs); 71 } 72 73 static void ocfs2_do_node_down(int node_num, 74 struct ocfs2_super *osb) 75 { 76 BUG_ON(osb->node_num == node_num); 77 78 mlog(0, "ocfs2: node down event for %d\n", node_num); 79 80 if (!osb->dlm) { 81 /* 82 * No DLM means we're not even ready to participate yet. 83 * We check the slots after the DLM comes up, so we will 84 * notice the node death then. We can safely ignore it 85 * here. 86 */ 87 return; 88 } 89 90 if (ocfs2_node_map_test_bit(osb, &osb->umount_map, node_num)) { 91 /* If a node is in the umount map, then we've been 92 * expecting him to go down and we know ahead of time 93 * that recovery is not necessary. */ 94 ocfs2_node_map_clear_bit(osb, &osb->umount_map, node_num); 95 return; 96 } 97 98 ocfs2_recovery_thread(osb, node_num); 99 100 ocfs2_remove_node_from_vote_queues(osb, node_num); 101 } 102 103 static void ocfs2_hb_node_down_cb(struct o2nm_node *node, 104 int node_num, 105 void *data) 106 { 107 ocfs2_do_node_down(node_num, (struct ocfs2_super *) data); 108 } 109 110 /* Called from the dlm when it's about to evict a node. We may also 111 * get a heartbeat callback later. */ 112 static void ocfs2_dlm_eviction_cb(int node_num, 113 void *data) 114 { 115 struct ocfs2_super *osb = (struct ocfs2_super *) data; 116 struct super_block *sb = osb->sb; 117 118 mlog(ML_NOTICE, "device (%u,%u): dlm has evicted node %d\n", 119 MAJOR(sb->s_dev), MINOR(sb->s_dev), node_num); 120 121 ocfs2_do_node_down(node_num, osb); 122 } 123 124 static void ocfs2_hb_node_up_cb(struct o2nm_node *node, 125 int node_num, 126 void *data) 127 { 128 struct ocfs2_super *osb = data; 129 130 BUG_ON(osb->node_num == node_num); 131 132 mlog(0, "node up event for %d\n", node_num); 133 ocfs2_node_map_clear_bit(osb, &osb->umount_map, node_num); 134 } 135 136 void ocfs2_setup_hb_callbacks(struct ocfs2_super *osb) 137 { 138 o2hb_setup_callback(&osb->osb_hb_down, O2HB_NODE_DOWN_CB, 139 ocfs2_hb_node_down_cb, osb, 140 OCFS2_HB_NODE_DOWN_PRI); 141 142 o2hb_setup_callback(&osb->osb_hb_up, O2HB_NODE_UP_CB, 143 ocfs2_hb_node_up_cb, osb, OCFS2_HB_NODE_UP_PRI); 144 145 /* Not exactly a heartbeat callback, but leads to essentially 146 * the same path so we set it up here. */ 147 dlm_setup_eviction_cb(&osb->osb_eviction_cb, 148 ocfs2_dlm_eviction_cb, 149 osb); 150 } 151 152 /* Most functions here are just stubs for now... */ 153 int ocfs2_register_hb_callbacks(struct ocfs2_super *osb) 154 { 155 int status; 156 157 status = o2hb_register_callback(&osb->osb_hb_down); 158 if (status < 0) { 159 mlog_errno(status); 160 goto bail; 161 } 162 163 status = o2hb_register_callback(&osb->osb_hb_up); 164 if (status < 0) 165 mlog_errno(status); 166 167 bail: 168 return status; 169 } 170 171 void ocfs2_clear_hb_callbacks(struct ocfs2_super *osb) 172 { 173 int status; 174 175 status = o2hb_unregister_callback(&osb->osb_hb_down); 176 if (status < 0) 177 mlog_errno(status); 178 179 status = o2hb_unregister_callback(&osb->osb_hb_up); 180 if (status < 0) 181 mlog_errno(status); 182 } 183 184 void ocfs2_stop_heartbeat(struct ocfs2_super *osb) 185 { 186 int ret; 187 char *argv[5], *envp[3]; 188 189 if (!osb->uuid_str) { 190 /* This can happen if we don't get far enough in mount... */ 191 mlog(0, "No UUID with which to stop heartbeat!\n\n"); 192 return; 193 } 194 195 argv[0] = (char *)o2nm_get_hb_ctl_path(); 196 argv[1] = "-K"; 197 argv[2] = "-u"; 198 argv[3] = osb->uuid_str; 199 argv[4] = NULL; 200 201 mlog(0, "Run: %s %s %s %s\n", argv[0], argv[1], argv[2], argv[3]); 202 203 /* minimal command environment taken from cpu_run_sbin_hotplug */ 204 envp[0] = "HOME=/"; 205 envp[1] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin"; 206 envp[2] = NULL; 207 208 ret = call_usermodehelper(argv[0], argv, envp, 1); 209 if (ret < 0) 210 mlog_errno(ret); 211 } 212 213 /* special case -1 for now 214 * TODO: should *really* make sure the calling func never passes -1!! */ 215 void ocfs2_node_map_init(struct ocfs2_node_map *map) 216 { 217 map->num_nodes = OCFS2_NODE_MAP_MAX_NODES; 218 memset(map->map, 0, BITS_TO_LONGS(OCFS2_NODE_MAP_MAX_NODES) * 219 sizeof(unsigned long)); 220 } 221 222 static inline void __ocfs2_node_map_set_bit(struct ocfs2_node_map *map, 223 int bit) 224 { 225 set_bit(bit, map->map); 226 } 227 228 void ocfs2_node_map_set_bit(struct ocfs2_super *osb, 229 struct ocfs2_node_map *map, 230 int bit) 231 { 232 if (bit==-1) 233 return; 234 BUG_ON(bit >= map->num_nodes); 235 spin_lock(&osb->node_map_lock); 236 __ocfs2_node_map_set_bit(map, bit); 237 spin_unlock(&osb->node_map_lock); 238 } 239 240 static inline void __ocfs2_node_map_clear_bit(struct ocfs2_node_map *map, 241 int bit) 242 { 243 clear_bit(bit, map->map); 244 } 245 246 void ocfs2_node_map_clear_bit(struct ocfs2_super *osb, 247 struct ocfs2_node_map *map, 248 int bit) 249 { 250 if (bit==-1) 251 return; 252 BUG_ON(bit >= map->num_nodes); 253 spin_lock(&osb->node_map_lock); 254 __ocfs2_node_map_clear_bit(map, bit); 255 spin_unlock(&osb->node_map_lock); 256 } 257 258 int ocfs2_node_map_test_bit(struct ocfs2_super *osb, 259 struct ocfs2_node_map *map, 260 int bit) 261 { 262 int ret; 263 if (bit >= map->num_nodes) { 264 mlog(ML_ERROR, "bit=%d map->num_nodes=%d\n", bit, map->num_nodes); 265 BUG(); 266 } 267 spin_lock(&osb->node_map_lock); 268 ret = test_bit(bit, map->map); 269 spin_unlock(&osb->node_map_lock); 270 return ret; 271 } 272 273 static inline int __ocfs2_node_map_is_empty(struct ocfs2_node_map *map) 274 { 275 int bit; 276 bit = find_next_bit(map->map, map->num_nodes, 0); 277 if (bit < map->num_nodes) 278 return 0; 279 return 1; 280 } 281 282 int ocfs2_node_map_is_empty(struct ocfs2_super *osb, 283 struct ocfs2_node_map *map) 284 { 285 int ret; 286 BUG_ON(map->num_nodes == 0); 287 spin_lock(&osb->node_map_lock); 288 ret = __ocfs2_node_map_is_empty(map); 289 spin_unlock(&osb->node_map_lock); 290 return ret; 291 } 292 293 static void __ocfs2_node_map_dup(struct ocfs2_node_map *target, 294 struct ocfs2_node_map *from) 295 { 296 BUG_ON(from->num_nodes == 0); 297 ocfs2_node_map_init(target); 298 __ocfs2_node_map_set(target, from); 299 } 300 301 /* returns 1 if bit is the only bit set in target, 0 otherwise */ 302 int ocfs2_node_map_is_only(struct ocfs2_super *osb, 303 struct ocfs2_node_map *target, 304 int bit) 305 { 306 struct ocfs2_node_map temp; 307 int ret; 308 309 spin_lock(&osb->node_map_lock); 310 __ocfs2_node_map_dup(&temp, target); 311 __ocfs2_node_map_clear_bit(&temp, bit); 312 ret = __ocfs2_node_map_is_empty(&temp); 313 spin_unlock(&osb->node_map_lock); 314 315 return ret; 316 } 317 318 static void __ocfs2_node_map_set(struct ocfs2_node_map *target, 319 struct ocfs2_node_map *from) 320 { 321 int num_longs, i; 322 323 BUG_ON(target->num_nodes != from->num_nodes); 324 BUG_ON(target->num_nodes == 0); 325 326 num_longs = BITS_TO_LONGS(target->num_nodes); 327 for (i = 0; i < num_longs; i++) 328 target->map[i] = from->map[i]; 329 } 330 331 /* Returns whether the recovery bit was actually set - it may not be 332 * if a node is still marked as needing recovery */ 333 int ocfs2_recovery_map_set(struct ocfs2_super *osb, 334 int num) 335 { 336 int set = 0; 337 338 spin_lock(&osb->node_map_lock); 339 340 __ocfs2_node_map_clear_bit(&osb->mounted_map, num); 341 342 if (!test_bit(num, osb->recovery_map.map)) { 343 __ocfs2_node_map_set_bit(&osb->recovery_map, num); 344 set = 1; 345 } 346 347 spin_unlock(&osb->node_map_lock); 348 349 return set; 350 } 351 352 void ocfs2_recovery_map_clear(struct ocfs2_super *osb, 353 int num) 354 { 355 ocfs2_node_map_clear_bit(osb, &osb->recovery_map, num); 356 } 357 358 int ocfs2_node_map_iterate(struct ocfs2_super *osb, 359 struct ocfs2_node_map *map, 360 int idx) 361 { 362 int i = idx; 363 364 idx = O2NM_INVALID_NODE_NUM; 365 spin_lock(&osb->node_map_lock); 366 if ((i != O2NM_INVALID_NODE_NUM) && 367 (i >= 0) && 368 (i < map->num_nodes)) { 369 while(i < map->num_nodes) { 370 if (test_bit(i, map->map)) { 371 idx = i; 372 break; 373 } 374 i++; 375 } 376 } 377 spin_unlock(&osb->node_map_lock); 378 return idx; 379 } 380