xref: /linux/fs/ocfs2/heartbeat.c (revision 24bce201d79807b668bf9d9e0aca801c5c0d5f78)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * heartbeat.c
4  *
5  * Register ourselves with the heartbaet service, keep our node maps
6  * up to date, and fire off recovery when needed.
7  *
8  * Copyright (C) 2002, 2004 Oracle.  All rights reserved.
9  */
10 
11 #include <linux/fs.h>
12 #include <linux/types.h>
13 #include <linux/highmem.h>
14 
15 #include <cluster/masklog.h>
16 
17 #include "ocfs2.h"
18 
19 #include "alloc.h"
20 #include "heartbeat.h"
21 #include "inode.h"
22 #include "journal.h"
23 #include "ocfs2_trace.h"
24 
25 #include "buffer_head_io.h"
26 
27 static inline void __ocfs2_node_map_set_bit(struct ocfs2_node_map *map,
28 					    int bit);
29 static inline void __ocfs2_node_map_clear_bit(struct ocfs2_node_map *map,
30 					      int bit);
31 
32 /* special case -1 for now
33  * TODO: should *really* make sure the calling func never passes -1!!  */
34 static void ocfs2_node_map_init(struct ocfs2_node_map *map)
35 {
36 	map->num_nodes = OCFS2_NODE_MAP_MAX_NODES;
37 	memset(map->map, 0, BITS_TO_LONGS(OCFS2_NODE_MAP_MAX_NODES) *
38 	       sizeof(unsigned long));
39 }
40 
41 void ocfs2_init_node_maps(struct ocfs2_super *osb)
42 {
43 	spin_lock_init(&osb->node_map_lock);
44 	ocfs2_node_map_init(&osb->osb_recovering_orphan_dirs);
45 }
46 
47 void ocfs2_do_node_down(int node_num, void *data)
48 {
49 	struct ocfs2_super *osb = data;
50 
51 	BUG_ON(osb->node_num == node_num);
52 
53 	trace_ocfs2_do_node_down(node_num);
54 
55 	if (!osb->cconn) {
56 		/*
57 		 * No cluster connection means we're not even ready to
58 		 * participate yet.  We check the slots after the cluster
59 		 * comes up, so we will notice the node death then.  We
60 		 * can safely ignore it here.
61 		 */
62 		return;
63 	}
64 
65 	ocfs2_recovery_thread(osb, node_num);
66 }
67 
68 static inline void __ocfs2_node_map_set_bit(struct ocfs2_node_map *map,
69 					    int bit)
70 {
71 	set_bit(bit, map->map);
72 }
73 
74 void ocfs2_node_map_set_bit(struct ocfs2_super *osb,
75 			    struct ocfs2_node_map *map,
76 			    int bit)
77 {
78 	if (bit==-1)
79 		return;
80 	BUG_ON(bit >= map->num_nodes);
81 	spin_lock(&osb->node_map_lock);
82 	__ocfs2_node_map_set_bit(map, bit);
83 	spin_unlock(&osb->node_map_lock);
84 }
85 
86 static inline void __ocfs2_node_map_clear_bit(struct ocfs2_node_map *map,
87 					      int bit)
88 {
89 	clear_bit(bit, map->map);
90 }
91 
92 void ocfs2_node_map_clear_bit(struct ocfs2_super *osb,
93 			      struct ocfs2_node_map *map,
94 			      int bit)
95 {
96 	if (bit==-1)
97 		return;
98 	BUG_ON(bit >= map->num_nodes);
99 	spin_lock(&osb->node_map_lock);
100 	__ocfs2_node_map_clear_bit(map, bit);
101 	spin_unlock(&osb->node_map_lock);
102 }
103 
104 int ocfs2_node_map_test_bit(struct ocfs2_super *osb,
105 			    struct ocfs2_node_map *map,
106 			    int bit)
107 {
108 	int ret;
109 	if (bit >= map->num_nodes) {
110 		mlog(ML_ERROR, "bit=%d map->num_nodes=%d\n", bit, map->num_nodes);
111 		BUG();
112 	}
113 	spin_lock(&osb->node_map_lock);
114 	ret = test_bit(bit, map->map);
115 	spin_unlock(&osb->node_map_lock);
116 	return ret;
117 }
118 
119