1 /* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
6 * Register ourselves with the heartbaet service, keep our node maps
7 * up to date, and fire off recovery when needed.
9 * Copyright (C) 2002, 2004 Oracle. All rights reserved.
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.
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.
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.
28 #include <linux/types.h>
29 #include <linux/slab.h>
30 #include <linux/highmem.h>
31 #include <linux/kmod.h>
33 #include <cluster/heartbeat.h>
34 #include <cluster/nodemanager.h>
36 #include <dlm/dlmapi.h>
38 #define MLOG_MASK_PREFIX ML_SUPER
39 #include <cluster/masklog.h>
44 #include "heartbeat.h"
49 #include "buffer_head_io.h"
51 #define OCFS2_HB_NODE_DOWN_PRI (0x0000002)
52 #define OCFS2_HB_NODE_UP_PRI OCFS2_HB_NODE_DOWN_PRI
54 static inline void __ocfs2_node_map_set_bit(struct ocfs2_node_map
*map
,
56 static inline void __ocfs2_node_map_clear_bit(struct ocfs2_node_map
*map
,
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
);
64 void ocfs2_init_node_maps(struct ocfs2_super
*osb
)
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
);
73 static void ocfs2_do_node_down(int node_num
,
74 struct ocfs2_super
*osb
)
76 BUG_ON(osb
->node_num
== node_num
);
78 mlog(0, "ocfs2: node down event for %d\n", node_num
);
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
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
);
98 ocfs2_recovery_thread(osb
, node_num
);
100 ocfs2_remove_node_from_vote_queues(osb
, node_num
);
103 static void ocfs2_hb_node_down_cb(struct o2nm_node
*node
,
107 ocfs2_do_node_down(node_num
, (struct ocfs2_super
*) data
);
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
,
115 struct ocfs2_super
*osb
= (struct ocfs2_super
*) data
;
116 struct super_block
*sb
= osb
->sb
;
118 mlog(ML_NOTICE
, "device (%u,%u): dlm has evicted node %d\n",
119 MAJOR(sb
->s_dev
), MINOR(sb
->s_dev
), node_num
);
121 ocfs2_do_node_down(node_num
, osb
);
124 static void ocfs2_hb_node_up_cb(struct o2nm_node
*node
,
128 struct ocfs2_super
*osb
= data
;
130 BUG_ON(osb
->node_num
== node_num
);
132 mlog(0, "node up event for %d\n", node_num
);
133 ocfs2_node_map_clear_bit(osb
, &osb
->umount_map
, node_num
);
136 void ocfs2_setup_hb_callbacks(struct ocfs2_super
*osb
)
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
);
142 o2hb_setup_callback(&osb
->osb_hb_up
, O2HB_NODE_UP_CB
,
143 ocfs2_hb_node_up_cb
, osb
, OCFS2_HB_NODE_UP_PRI
);
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
,
152 /* Most functions here are just stubs for now... */
153 int ocfs2_register_hb_callbacks(struct ocfs2_super
*osb
)
157 if (ocfs2_mount_local(osb
))
160 status
= o2hb_register_callback(&osb
->osb_hb_down
);
166 status
= o2hb_register_callback(&osb
->osb_hb_up
);
174 void ocfs2_clear_hb_callbacks(struct ocfs2_super
*osb
)
178 if (ocfs2_mount_local(osb
))
181 status
= o2hb_unregister_callback(&osb
->osb_hb_down
);
185 status
= o2hb_unregister_callback(&osb
->osb_hb_up
);
190 void ocfs2_stop_heartbeat(struct ocfs2_super
*osb
)
193 char *argv
[5], *envp
[3];
195 if (ocfs2_mount_local(osb
))
198 if (!osb
->uuid_str
) {
199 /* This can happen if we don't get far enough in mount... */
200 mlog(0, "No UUID with which to stop heartbeat!\n\n");
204 argv
[0] = (char *)o2nm_get_hb_ctl_path();
207 argv
[3] = osb
->uuid_str
;
210 mlog(0, "Run: %s %s %s %s\n", argv
[0], argv
[1], argv
[2], argv
[3]);
212 /* minimal command environment taken from cpu_run_sbin_hotplug */
214 envp
[1] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
217 ret
= call_usermodehelper(argv
[0], argv
, envp
, 1);
222 /* special case -1 for now
223 * TODO: should *really* make sure the calling func never passes -1!! */
224 void ocfs2_node_map_init(struct ocfs2_node_map
*map
)
226 map
->num_nodes
= OCFS2_NODE_MAP_MAX_NODES
;
227 memset(map
->map
, 0, BITS_TO_LONGS(OCFS2_NODE_MAP_MAX_NODES
) *
228 sizeof(unsigned long));
231 static inline void __ocfs2_node_map_set_bit(struct ocfs2_node_map
*map
,
234 set_bit(bit
, map
->map
);
237 void ocfs2_node_map_set_bit(struct ocfs2_super
*osb
,
238 struct ocfs2_node_map
*map
,
243 BUG_ON(bit
>= map
->num_nodes
);
244 spin_lock(&osb
->node_map_lock
);
245 __ocfs2_node_map_set_bit(map
, bit
);
246 spin_unlock(&osb
->node_map_lock
);
249 static inline void __ocfs2_node_map_clear_bit(struct ocfs2_node_map
*map
,
252 clear_bit(bit
, map
->map
);
255 void ocfs2_node_map_clear_bit(struct ocfs2_super
*osb
,
256 struct ocfs2_node_map
*map
,
261 BUG_ON(bit
>= map
->num_nodes
);
262 spin_lock(&osb
->node_map_lock
);
263 __ocfs2_node_map_clear_bit(map
, bit
);
264 spin_unlock(&osb
->node_map_lock
);
267 int ocfs2_node_map_test_bit(struct ocfs2_super
*osb
,
268 struct ocfs2_node_map
*map
,
272 if (bit
>= map
->num_nodes
) {
273 mlog(ML_ERROR
, "bit=%d map->num_nodes=%d\n", bit
, map
->num_nodes
);
276 spin_lock(&osb
->node_map_lock
);
277 ret
= test_bit(bit
, map
->map
);
278 spin_unlock(&osb
->node_map_lock
);
282 static inline int __ocfs2_node_map_is_empty(struct ocfs2_node_map
*map
)
285 bit
= find_next_bit(map
->map
, map
->num_nodes
, 0);
286 if (bit
< map
->num_nodes
)
291 int ocfs2_node_map_is_empty(struct ocfs2_super
*osb
,
292 struct ocfs2_node_map
*map
)
295 BUG_ON(map
->num_nodes
== 0);
296 spin_lock(&osb
->node_map_lock
);
297 ret
= __ocfs2_node_map_is_empty(map
);
298 spin_unlock(&osb
->node_map_lock
);
302 static void __ocfs2_node_map_dup(struct ocfs2_node_map
*target
,
303 struct ocfs2_node_map
*from
)
305 BUG_ON(from
->num_nodes
== 0);
306 ocfs2_node_map_init(target
);
307 __ocfs2_node_map_set(target
, from
);
310 /* returns 1 if bit is the only bit set in target, 0 otherwise */
311 int ocfs2_node_map_is_only(struct ocfs2_super
*osb
,
312 struct ocfs2_node_map
*target
,
315 struct ocfs2_node_map temp
;
318 spin_lock(&osb
->node_map_lock
);
319 __ocfs2_node_map_dup(&temp
, target
);
320 __ocfs2_node_map_clear_bit(&temp
, bit
);
321 ret
= __ocfs2_node_map_is_empty(&temp
);
322 spin_unlock(&osb
->node_map_lock
);
327 static void __ocfs2_node_map_set(struct ocfs2_node_map
*target
,
328 struct ocfs2_node_map
*from
)
332 BUG_ON(target
->num_nodes
!= from
->num_nodes
);
333 BUG_ON(target
->num_nodes
== 0);
335 num_longs
= BITS_TO_LONGS(target
->num_nodes
);
336 for (i
= 0; i
< num_longs
; i
++)
337 target
->map
[i
] = from
->map
[i
];
340 /* Returns whether the recovery bit was actually set - it may not be
341 * if a node is still marked as needing recovery */
342 int ocfs2_recovery_map_set(struct ocfs2_super
*osb
,
347 spin_lock(&osb
->node_map_lock
);
349 __ocfs2_node_map_clear_bit(&osb
->mounted_map
, num
);
351 if (!test_bit(num
, osb
->recovery_map
.map
)) {
352 __ocfs2_node_map_set_bit(&osb
->recovery_map
, num
);
356 spin_unlock(&osb
->node_map_lock
);
361 void ocfs2_recovery_map_clear(struct ocfs2_super
*osb
,
364 ocfs2_node_map_clear_bit(osb
, &osb
->recovery_map
, num
);
367 int ocfs2_node_map_iterate(struct ocfs2_super
*osb
,
368 struct ocfs2_node_map
*map
,
373 idx
= O2NM_INVALID_NODE_NUM
;
374 spin_lock(&osb
->node_map_lock
);
375 if ((i
!= O2NM_INVALID_NODE_NUM
) &&
377 (i
< map
->num_nodes
)) {
378 while(i
< map
->num_nodes
) {
379 if (test_bit(i
, map
->map
)) {
386 spin_unlock(&osb
->node_map_lock
);