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