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